generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 129
Fix the bug: Loop contracts are not composable with function contracts #3979
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
Merged
thanhnguyen-aws
merged 20 commits into
model-checking:main
from
thanhnguyen-aws:loopfunction
May 9, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
feec92f
save file
thanhnguyen-aws d9974c9
support for funtion + loop contract
thanhnguyen-aws 06439f7
add expected tests
thanhnguyen-aws a566617
fix clippy
thanhnguyen-aws 246b82f
fix format
thanhnguyen-aws fd2d98b
fix command flag
thanhnguyen-aws 830f0d5
Merge branch 'main' into loopfunction
thanhnguyen-aws 52d8ee8
Merge branch 'main' into loopfunction
thanhnguyen-aws da00904
Merge branch 'main' into loopfunction
thanhnguyen-aws b681408
Update kani-compiler/src/kani_middle/transform/loop_contracts.rs
thanhnguyen-aws 46cbe09
Update tests/expected/loop-contract/contract_proof_function_with_loop.rs
thanhnguyen-aws 7871e6d
Merge branch 'main' into loopfunction
thanhnguyen-aws 098242d
add new test and comments
thanhnguyen-aws fd3758f
fix expected
thanhnguyen-aws 609c656
fix expected
thanhnguyen-aws 4509c43
add comments
thanhnguyen-aws a92ee6d
Merge branch 'main' into loopfunction
thanhnguyen-aws 29b99b1
Merge branch 'main' into loopfunction
thanhnguyen-aws 187201d
Merge branch 'main' into loopfunction
thanhnguyen-aws 3838278
update loop contract doc
thanhnguyen-aws 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
13 changes: 13 additions & 0 deletions
13
tests/expected/loop-contract/contract_proof_function_with_loop.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,13 @@ | ||
| has_loop::{closure#2}::{closure#1}.loop_invariant_step.1\ | ||
| - Status: SUCCESS\ | ||
| - Description: "Check invariant after step for loop has_loop::{closure#2}::{closure#1}.0"\ | ||
| in function has_loop::{closure#2}::{closure#1} | ||
|
|
||
|
|
||
|
|
||
| has_loop::{closure#2}::{closure#1}.precondition_instance.1\ | ||
| - Status: SUCCESS\ | ||
| - Description: "free argument must be NULL or valid pointer"\ | ||
| in function has_loop::{closure#2}::{closure#1} | ||
|
|
||
| VERIFICATION:- SUCCESSFUL |
25 changes: 25 additions & 0 deletions
25
tests/expected/loop-contract/contract_proof_function_with_loop.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,25 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts -Z function-contracts | ||
|
|
||
| //! Test that we can prove a function contract and a loop contract in tandem. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| #[kani::requires(i>=2)] | ||
| #[kani::ensures(|ret| *ret == 2)] | ||
| pub fn has_loop(mut i: u16) -> u16 { | ||
| #[kani::loop_invariant(i>=2)] | ||
| while i > 2 { | ||
| i = i - 1 | ||
| } | ||
| i | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(has_loop)] | ||
| fn contract_proof() { | ||
| let i: u16 = kani::any(); | ||
| let j = has_loop(i); | ||
| } |
11 changes: 11 additions & 0 deletions
11
tests/expected/loop-contract/function_call_with_loop.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,11 @@ | ||
| has_loop::{closure#3}::{closure#0}.loop_invariant_base.1\ | ||
| - Status: SUCCESS\ | ||
| - Description: "Check invariant before entry for loop has_loop::{closure#3}::{closure#0}.0"\ | ||
| in function has_loop::{closure#3}::{closure#0} | ||
|
|
||
| has_loop::{closure#3}::{closure#0}.precondition_instance.5\ | ||
| - Status: SUCCESS\ | ||
| - Description: "free called for new[] object"\ | ||
| in function has_loop::{closure#3}::{closure#0} | ||
|
|
||
| Failed Checks: i>=2 |
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,25 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts -Z function-contracts | ||
|
|
||
| //! Calling a function that contains loops and test that using a #[kani::proof] harness fails because the function's precondition gets asserted. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| #[kani::requires(i>=2)] | ||
| #[kani::ensures(|ret| *ret == 2)] | ||
| pub fn has_loop(mut i: u16) -> u16 { | ||
| #[kani::loop_invariant(i>=2)] | ||
| while i > 2 { | ||
| i = i - 1 | ||
| } | ||
| i | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn call_has_loop() { | ||
| let i: u16 = kani::any(); | ||
| let j = has_loop(i); | ||
| } |
8 changes: 8 additions & 0 deletions
8
tests/expected/loop-contract/function_with_loop_no_assertion.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,8 @@ | ||
| has_loop.loop_invariant_base.1\ | ||
| - Status: SUCCESS\ | ||
| - Description: "Check invariant before entry for loop has_loop.0"\ | ||
| in function has_loop | ||
|
|
||
|
|
||
|
|
||
| VERIFICATION:- SUCCESSFUL |
25 changes: 25 additions & 0 deletions
25
tests/expected/loop-contract/function_with_loop_no_assertion.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,25 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts -Z function-contracts --no-assert-contracts | ||
|
|
||
| //Call a function with loop without checking the contract. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| #[kani::requires(i>=2)] | ||
| #[kani::ensures(|ret| *ret == 2)] | ||
| pub fn has_loop(mut i: u16) -> u16 { | ||
| #[kani::loop_invariant(i>=2)] | ||
| while i > 2 { | ||
| i = i - 1 | ||
| } | ||
| i | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn contract_proof() { | ||
| let i: u16 = kani::any(); | ||
| let j = has_loop(i); | ||
| } |
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.