-
Notifications
You must be signed in to change notification settings - Fork 169
Detect stub_verified/Arbitrary recursion at compile time
#4571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
feliperodri
wants to merge
1
commit into
model-checking:main
Choose a base branch
from
feliperodri:fix-stub-verified-arbitrary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
tests/expected/function-contract/stub_verified_arbitrary_cycle.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| error: `Wrapper::normalize` is used as a verified stub, but generating an arbitrary value of its return type `Wrapper` calls `Wrapper::normalize` again | ||
| note: the contract replacement havocs its return value with `kani::any::<Wrapper>()`, so this forms an unbounded recursion: | ||
| -> <stub_verified_arbitrary_cycle::Wrapper as kani::Arbitrary>::any | ||
| -> stub_verified_arbitrary_cycle::Wrapper::new | ||
| -> stub_verified_arbitrary_cycle::Wrapper::normalize | ||
| help: derive `Arbitrary` for `Wrapper` instead of implementing it manually, or avoid calling `Wrapper::normalize` from the `Arbitrary` implementation |
56 changes: 56 additions & 0 deletions
56
tests/expected/function-contract/stub_verified_arbitrary_cycle.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts -Zstubbing | ||
|
|
||
| //! Test that Kani detects the cycle between a verified stub and the `Arbitrary` | ||
| //! implementation of its return type, and reports it at compile time. | ||
| //! | ||
| //! A contract replacement havocs its own return value with `kani::any::<Ret>()`. | ||
| //! Here `<Wrapper as Arbitrary>::any` calls `Wrapper::new`, which calls the | ||
| //! stubbed `Wrapper::normalize`, so the replacement re-enters itself through | ||
| //! `Arbitrary::any`. Without this check, CBMC unwinds the recursion until it | ||
| //! exhausts memory. | ||
| //! | ||
| //! See https://github.com/model-checking/kani/pull/4571 | ||
|
|
||
| const LIMIT: u64 = 1000; | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct Wrapper { | ||
| value: u64, | ||
| } | ||
|
|
||
| impl Wrapper { | ||
| #[kani::ensures(|result: &Self| result.value <= LIMIT)] | ||
| fn normalize(self) -> Self { | ||
| if self.value > LIMIT { Wrapper { value: LIMIT } } else { self } | ||
| } | ||
|
|
||
| fn new(v: u64) -> Self { | ||
| Wrapper { value: v }.normalize() | ||
| } | ||
|
|
||
| fn process(self) -> u64 { | ||
| self.normalize().value * 2 | ||
| } | ||
| } | ||
|
|
||
| // This `Arbitrary` implementation calls `normalize`, which closes the cycle. | ||
| impl kani::Arbitrary for Wrapper { | ||
| fn any() -> Self { | ||
| Wrapper::new(kani::any()) | ||
| } | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(Wrapper::normalize)] | ||
| fn check_normalize_contract() { | ||
| Wrapper { value: kani::any() }.normalize(); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub_verified(Wrapper::normalize)] | ||
| fn check_process_with_stub() { | ||
| let w: Wrapper = kani::any(); | ||
| let result = w.process(); | ||
| assert!(result <= LIMIT * 2); | ||
| } |
5 changes: 5 additions & 0 deletions
5
tests/expected/function-contract/stub_verified_arbitrary_cycle_generic.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| error: `clamp` is used as a verified stub, but generating an arbitrary value of its return type `Cyclic` calls `clamp` again | ||
| note: the contract replacement havocs its return value with `kani::any::<Cyclic>()`, so this forms an unbounded recursion: | ||
| -> <stub_verified_arbitrary_cycle_generic::Cyclic as kani::Arbitrary>::any | ||
| -> stub_verified_arbitrary_cycle_generic::clamp::<stub_verified_arbitrary_cycle_generic::Cyclic> | ||
| help: derive `Arbitrary` for `Cyclic` instead of implementing it manually, or avoid calling `clamp` from the `Arbitrary` implementation |
74 changes: 74 additions & 0 deletions
74
tests/expected/function-contract/stub_verified_arbitrary_cycle_generic.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts -Zstubbing | ||
|
|
||
| //! Test that the `stub_verified` / `Arbitrary` cycle check inspects every | ||
| //! monomorphization of a generic target, not just the first one. | ||
| //! | ||
| //! `clamp` is instantiated at both `Safe` and `Cyclic`. Only `Cyclic` has an | ||
| //! `Arbitrary` implementation that calls back into `clamp`, and `clamp::<Safe>` | ||
| //! is transformed first. Deduplicating the check per `FnDef` rather than per | ||
| //! monomorphized instance would let the acyclic `Safe` instantiation consume the | ||
| //! only slot, skipping `Cyclic` entirely and reintroducing the hang. | ||
| //! | ||
| //! See https://github.com/model-checking/kani/pull/4571 | ||
|
|
||
| trait Clampable: Copy { | ||
| fn val(self) -> u64; | ||
| fn make(v: u64) -> Self; | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, kani::Arbitrary)] | ||
| struct Safe { | ||
| v: u64, | ||
| } | ||
|
|
||
| impl Clampable for Safe { | ||
| fn val(self) -> u64 { | ||
| self.v | ||
| } | ||
| fn make(v: u64) -> Self { | ||
| Safe { v } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct Cyclic { | ||
| v: u64, | ||
| } | ||
|
|
||
| impl Clampable for Cyclic { | ||
| fn val(self) -> u64 { | ||
| self.v | ||
| } | ||
| fn make(v: u64) -> Self { | ||
| Cyclic { v } | ||
| } | ||
| } | ||
|
|
||
| // This `Arbitrary` implementation routes back into the generic stubbed function. | ||
| impl kani::Arbitrary for Cyclic { | ||
| fn any() -> Self { | ||
| clamp(Cyclic { v: kani::any() }) | ||
| } | ||
| } | ||
|
|
||
| #[kani::ensures(|r: &T| r.val() <= 10)] | ||
| fn clamp<T: Clampable>(x: T) -> T { | ||
| if x.val() > 10 { T::make(10) } else { x } | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(clamp)] | ||
| fn check_clamp_contract() { | ||
| clamp(Safe { v: kani::any() }); | ||
| } | ||
|
|
||
| // The acyclic `Safe` instantiation is used before the cyclic `Cyclic` one. | ||
| #[kani::proof] | ||
| #[kani::stub_verified(clamp)] | ||
| fn check_both_instantiations() { | ||
| let s: Safe = kani::any(); | ||
| assert!(clamp(s).val() <= 10); | ||
| let c: Cyclic = kani::any(); | ||
| assert!(clamp(c).val() <= 10); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.