Repository navigation
Copy a loop-body local to the loop head only when a nested loop needs it and it can be moved #4957
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
kasimte
wants to merge
10
commits into
model-checking:main
Choose a base branch
from
kasimte:fix-4911-loophead-call-move
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
427a313
Don't copy a loop body call to the loop head when it reads loop locals
kasimte d6cb998
Skip moving declaration groups with non-retargetable terminators
kasimte 3db2abb
Count only whole-local assignments as definition sites
kasimte 0d7c470
Analyze declaration groups with an ordered MirVisitor walk
kasimte fafa03b
Track locals the hoists have already made live at each loop head
kasimte 5e96a7f
Don't count call destinations as definition sites in the fallback
kasimte 50b760f
Scope the gate comments to liveness and link the head-copy effects issue
kasimte 446a8ba
Loop contracts: gate the loop-head copy on need, not on liveness
kasimte b2710c2
Merge branch 'main' into fix-4911-loophead-call-move
kasimte bcc4045
Merge remote-tracking branch 'upstream/main' into fix-4911-loophead-c…
kasimte 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
10 changes: 10 additions & 0 deletions
10
tests/expected/loop-contract/loop_head_call_computed_index.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,10 @@ | ||
| - Status: SUCCESS\ | ||
| - Description: "Check invariant before entry for loop search::<{closure@ | ||
|
|
||
| - Status: SUCCESS\ | ||
| - Description: "Check invariant after step for loop search::<{closure@ | ||
|
|
||
| <usize as std::slice::SliceIndex<[u8]>>::get_unchecked.assume.1\ | ||
| - Status: SUCCESS | ||
|
|
||
| Complete - 3 successfully verified harnesses, 0 failures, 3 total. |
91 changes: 91 additions & 0 deletions
91
tests/expected/loop-contract/loop_head_call_computed_index.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,91 @@ | ||
| // Copyright Kani Contributors | ||
|
kasimte marked this conversation as resolved.
Resolved
|
||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts | ||
|
|
||
| //! A call in the loop body whose index is computed in the body (a binary-search-shaped loop | ||
| //! passing `s.get_unchecked(mid)` into a closure) must not have its initialization copied to | ||
| //! the loop head: at the head the computed index does not exist yet and `get_unchecked`'s | ||
| //! precondition check fails spuriously. The two sibling harnesses pin the shapes that already | ||
| //! worked: an element discarded with no binding, and the value copied out before the call. | ||
|
|
||
| #![feature(stmt_expr_attributes)] | ||
| #![feature(proc_macro_hygiene)] | ||
|
|
||
| use std::cmp::Ordering; | ||
|
|
||
| // The #4911 shape: computed index, element ref flows into the closure. | ||
| fn search<F: FnMut(&u8) -> Ordering>(s: &[u8], mut f: F) { | ||
| let len = s.len(); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let mut size = len; | ||
| let mut base = 0usize; | ||
| #[kani::loop_invariant(1 <= size && size <= len && base <= len - size)] | ||
| while size > 1 { | ||
| let half = size / 2; | ||
| let mid = base + half; | ||
| let cmp = f(unsafe { s.get_unchecked(mid) }); | ||
| base = if cmp == Ordering::Greater { base } else { mid }; | ||
| size -= half; | ||
| } | ||
| } | ||
|
|
||
| // Sibling pin 1: element discarded, no user binding starts a group. | ||
| fn search_no_closure(s: &[u8]) { | ||
| let len = s.len(); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let mut size = len; | ||
| let mut base = 0usize; | ||
| #[kani::loop_invariant(1 <= size && size <= len && base <= len - size)] | ||
| while size > 1 { | ||
| let half = size / 2; | ||
| let mid = base + half; | ||
| let _ = unsafe { s.get_unchecked(mid) }; | ||
| base = if kani::any() { base } else { mid }; | ||
| size -= half; | ||
| } | ||
| } | ||
|
|
||
| // Sibling pin 2: value copied out before the closure call. | ||
| fn search_copyval<F: FnMut(&u8) -> Ordering>(s: &[u8], mut f: F) { | ||
| let len = s.len(); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let mut size = len; | ||
| let mut base = 0usize; | ||
| #[kani::loop_invariant(1 <= size && size <= len && base <= len - size)] | ||
| while size > 1 { | ||
| let half = size / 2; | ||
| let mid = base + half; | ||
| let v = unsafe { *s.get_unchecked(mid) }; | ||
| let cmp = f(&v); | ||
| base = if cmp == Ordering::Greater { base } else { mid }; | ||
| size -= half; | ||
| } | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_with_closure() { | ||
| let a: [u8; 8] = kani::any(); | ||
| let s: &[u8] = kani::slice::any_slice_of_array(&a); | ||
| search(s, |_| if kani::any() { Ordering::Less } else { Ordering::Greater }); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_no_closure() { | ||
| let a: [u8; 8] = kani::any(); | ||
| let s: &[u8] = kani::slice::any_slice_of_array(&a); | ||
| search_no_closure(s); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_copyval() { | ||
| let a: [u8; 8] = kani::any(); | ||
| let s: &[u8] = kani::slice::any_slice_of_array(&a); | ||
| search_copyval(s, |_| if kani::any() { Ordering::Less } else { Ordering::Greater }); | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
tests/expected/loop-contract/loop_head_call_computed_index_fail.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,3 @@ | ||
| Rust intrinsic assumption failed | ||
|
|
||
| VERIFICATION:- FAILED |
38 changes: 38 additions & 0 deletions
38
tests/expected/loop-contract/loop_head_call_computed_index_fail.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,38 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts | ||
|
|
||
| //! Negative twin of loop_head_call_computed_index.rs: with the spurious loop-head copy gone, a | ||
| //! genuinely out-of-bounds `get_unchecked` in the loop body must still fail its precondition | ||
| //! check at the real call site. | ||
|
|
||
| #![feature(stmt_expr_attributes)] | ||
| #![feature(proc_macro_hygiene)] | ||
|
|
||
| use std::cmp::Ordering; | ||
|
|
||
| fn search_oob<F: FnMut(&u8) -> Ordering>(s: &[u8], mut f: F) { | ||
| let len = s.len(); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let mut size = len; | ||
| let mut base = 0usize; | ||
| #[kani::loop_invariant(1 <= size && size <= len && base <= len - size)] | ||
| while size > 1 { | ||
| let half = size / 2; | ||
| let mid = base + half; | ||
| // mid + 1 can equal len: a real precondition violation at the body call site. | ||
| let cmp = f(unsafe { s.get_unchecked(mid + 1) }); | ||
| base = if cmp == Ordering::Greater { base } else { mid }; | ||
| size -= half; | ||
| } | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_oob_still_fails() { | ||
| let a: [u8; 8] = kani::any(); | ||
| let s: &[u8] = kani::slice::any_slice_of_array(&a); | ||
| search_oob(s, |_| if kani::any() { Ordering::Less } else { Ordering::Greater }); | ||
| } |
1 change: 1 addition & 0 deletions
1
tests/expected/loop-contract/loop_local_var_branching_init.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 @@ | ||
| VERIFICATION:- SUCCESSFUL |
30 changes: 30 additions & 0 deletions
30
tests/expected/loop-contract/loop_local_var_branching_init.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,30 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts | ||
|
|
||
| //! Regression: moving a loop-local's declaration blocks to the loop head panicked with | ||
| //! "Kani can only insert instructions after terminators that have a `target` field" | ||
| //! when the local is initialized through branching control flow (the collected init | ||
| //! group spans a `SwitchInt`). Such groups are now left in place. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| fn pick(x: u32) -> u32 { | ||
| x + 1 | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn loop_local_var_branching_init() { | ||
| let n: u32 = kani::any_where(|x| *x <= 8); | ||
| let mut i: u32 = 0; | ||
| #[kani::loop_invariant(i <= n)] | ||
| while i < n { | ||
| // Both arms yield 1 so the loop stays provable; the `if` is only here to | ||
| // force a branching (SwitchInt) initializer for `step`. | ||
| let step = if i % 2 == 0 { pick(0) } else { pick(0) }; | ||
| i += step; | ||
| } | ||
| assert!(i == n); | ||
| } |
1 change: 1 addition & 0 deletions
1
tests/expected/loop-contract/loop_local_var_branching_init_external_cond.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 @@ | ||
| VERIFICATION:- SUCCESSFUL |
31 changes: 31 additions & 0 deletions
31
tests/expected/loop-contract/loop_local_var_branching_init_external_cond.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,31 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts | ||
|
|
||
| //! Same panic as loop_local_var_branching_init, with the branch condition reading | ||
| //! a variable declared before the loop. The collected init group mentions no loop | ||
| //! local, so the reads-loop-locals skip does not apply; only the terminator-shape | ||
| //! check keeps the group from being moved and panicking. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| fn pick(x: u32) -> u32 { | ||
| x + 1 | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn loop_local_var_branching_init_external_cond() { | ||
| let gate: bool = kani::any(); | ||
| let n: u32 = kani::any_where(|x| *x <= 8); | ||
| let mut i: u32 = 0; | ||
| #[kani::loop_invariant(i <= n)] | ||
| while i < n { | ||
| // Both arms yield 1 so the loop stays provable; the `if` is only here to | ||
| // force a branching (SwitchInt) initializer for `step`. | ||
| let step = if gate { pick(0) } else { pick(0) }; | ||
| i += step; | ||
| } | ||
| assert!(i == n); | ||
| } |
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
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.