Repository navigation
Check slice element reads under -Z valid-value-checks #5073
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
2
commits into
model-checking:main
Choose a base branch
from
kasimte:fix-5022-slice-element-validity
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| Checking harness check_valid_slice_elem... | ||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Checking harness check_invalid_slice_elem_should_fail... | ||
| Failed Checks: Undefined Behavior: Invalid value of type `bool` | ||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_invalid_slice_elem_symbolic_should_fail... | ||
| Failed Checks: Undefined Behavior: Invalid value of type `bool` | ||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness check_raw_ref_slice_elem_no_read... | ||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Checking harness check_slice_elem_partial_read... | ||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Checking harness check_slice_elem_nested_write... | ||
| VERIFICATION:- SUCCESSFUL | ||
|
|
||
| Complete - 4 successfully verified harnesses, 2 failures, 6 total. |
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,71 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Z valid-value-checks | ||
| //! Regression test for https://github.com/model-checking/kani/issues/5022: | ||
| //! reading an invalid element through a slice pointer must be reported (a slice | ||
| //! has no static length, so the whole-slice deref checks no elements). The check | ||
| //! fires only on a direct value read of the whole element, so forming a pointer | ||
| //! (`&raw const`), reading a sub-element, and writing are not reported. `Index` | ||
| //! and `ConstantIndex` share one arm, so the `Index` harnesses cover both. | ||
| //! `black_box` keeps each read from being removed as dead code. | ||
|
|
||
| /// Negative control: byte 2 read as a `bool` element is invalid. | ||
| #[kani::proof] | ||
| fn check_invalid_slice_elem_should_fail() { | ||
| let bytes = [2u8; 4]; | ||
| let p = &bytes as *const [u8] as *const [bool]; | ||
| let _ = std::hint::black_box(unsafe { (*p)[1] }); | ||
| } | ||
|
|
||
| /// Negative control, symbolic index: only index 2 holds an invalid `bool` | ||
| /// (byte 2); the rest are valid (byte 1). A symbolic index must still catch | ||
| /// the one bad element, which proves the check tracks the actual index rather | ||
| /// than a fixed position. | ||
| #[kani::proof] | ||
| fn check_invalid_slice_elem_symbolic_should_fail() { | ||
| let bytes = [1u8, 1u8, 2u8, 1u8]; | ||
| let p = &bytes as *const [u8] as *const [bool]; | ||
| let i: usize = kani::any(); | ||
| kani::assume(i < 4); | ||
| let _ = std::hint::black_box(unsafe { (*p)[i] }); | ||
| } | ||
|
|
||
| /// Positive control: valid `bool` elements (byte 1) at any index verify. | ||
| #[kani::proof] | ||
| fn check_valid_slice_elem() { | ||
| let bytes = [1u8; 4]; | ||
| let p = &bytes as *const [u8] as *const [bool]; | ||
| let i: usize = kani::any(); | ||
| kani::assume(i < 4); | ||
| let _ = std::hint::black_box(unsafe { (*p)[i] }); | ||
| } | ||
|
|
||
| /// Positive: forming a raw pointer to an element (`&raw const`) loads no `bool`, | ||
| /// so an element that would be invalid if read must not be reported. | ||
| #[kani::proof] | ||
| fn check_raw_ref_slice_elem_no_read() { | ||
| let bytes = [2u8; 4]; | ||
| let p = &bytes as *const [u8] as *const [bool]; | ||
| let _ = std::hint::black_box(unsafe { &raw const (*p)[0] }); | ||
| } | ||
|
|
||
| /// Positive: reading one `bool` of a `[bool; 2]` slice element checks only that | ||
| /// sub-byte. Byte 0 is a valid `bool`; the unread byte 1 is invalid but must not | ||
| /// cause a failure. | ||
| #[kani::proof] | ||
| fn check_slice_elem_partial_read() { | ||
| let bytes = [[1u8, 2u8]]; | ||
| let p = &bytes as *const [[u8; 2]] as *const [[bool; 2]]; | ||
| let _ = std::hint::black_box(unsafe { (*p)[0][0] }); | ||
| } | ||
|
|
||
| /// Positive: writing one `bool` of a `[bool; 2]` slice element must not validate | ||
| /// the element's old (invalid) contents, since a write is not UB until read. | ||
| #[kani::proof] | ||
| fn check_slice_elem_nested_write() { | ||
| let mut bytes = [[2u8, 2u8]]; | ||
| let p = &mut bytes as *mut [[u8; 2]] as *mut [[bool; 2]]; | ||
| unsafe { | ||
| (*p)[0][0] = true; | ||
| } | ||
| } |
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.