-
Notifications
You must be signed in to change notification settings - Fork 120
Implement memory initialization state copy functionality #3350
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
artemagvanian
merged 18 commits into
model-checking:main
from
artemagvanian:reset-mem-init
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
11400b6
Tentative implementation of memory initialization copy
artemagvanian bd07b24
Merge branch 'main' into reset-mem-init
artemagvanian c14d65c
Fix `copy` intrinsic test to reflect the semantic change
artemagvanian 82abe74
Add extra copy-specific tests
artemagvanian 998002d
Code formatting
artemagvanian ec06a22
Merge branch 'main' into reset-mem-init
artemagvanian b8f454a
Merge branch 'main' into reset-mem-init
artemagvanian 0cb0a90
Merge branch 'main' into reset-mem-init
artemagvanian 9b6099f
Merge branch 'main' into reset-mem-init
artemagvanian cc85af8
Merge branch 'main' into reset-mem-init
artemagvanian ae3e20d
Merge branch 'main' into reset-mem-init
artemagvanian d7d26dc
Merge branch 'main' into reset-mem-init
artemagvanian 753262c
Add comment stating that copy is untyped
artemagvanian ce02e28
Code formatting
artemagvanian f46f1cb
Merge branch 'main' into reset-mem-init
artemagvanian 2eca14d
Add extra tests for copy + delayed ub, split copy tests into multiple
artemagvanian 4cb2ccf
Formatting change
artemagvanian 569d4f3
Delete accidentally committed file
artemagvanian 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
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 @@ | ||
Complete - 1 successfully verified harnesses, 0 failures, 1 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,23 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z uninit-checks | ||
|
||
#[repr(C)] | ||
#[derive(kani::Arbitrary)] | ||
struct S(u32, u8); // 5 bytes of data + 3 bytes of padding. | ||
|
||
#[kani::proof] | ||
/// This checks that reading copied initialized bytes verifies correctly. | ||
unsafe fn copy_without_padding() { | ||
let from: S = kani::any(); | ||
let mut to: u64 = kani::any(); | ||
|
||
let from_ptr = &from as *const S; | ||
let to_ptr = &mut to as *mut u64; | ||
|
||
// This should not cause UB since `copy` is untyped. | ||
std::ptr::copy(from_ptr as *const u8, to_ptr as *mut u8, std::mem::size_of::<u32>()); | ||
|
||
// Since the previous copy only copied 4 bytes, no padding was copied, so no padding is read. | ||
let data: u64 = std::ptr::read(to_ptr); | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/expected/uninit/copy/expose_padding_via_copy.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 @@ | ||
std::ptr::read::<u64>.assertion.1\ | ||
- Status: FAILURE\ | ||
- Description: "Undefined Behavior: Reading from an uninitialized pointer of type `*const u64`"\ | ||
|
||
std::ptr::read::<u64>.assertion.2\ | ||
- Status: FAILURE\ | ||
- Description: "Undefined Behavior: Reading from an uninitialized pointer of type `u64`"\ | ||
|
||
Summary: | ||
Verification failed for - expose_padding_via_copy | ||
Complete - 0 successfully verified harnesses, 1 failures, 1 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,23 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z uninit-checks | ||
|
||
#[repr(C)] | ||
#[derive(kani::Arbitrary)] | ||
struct S(u32, u8); // 5 bytes of data + 3 bytes of padding. | ||
|
||
/// This checks that reading copied uninitialized bytes fails an assertion. | ||
#[kani::proof] | ||
unsafe fn expose_padding_via_copy() { | ||
let from: S = kani::any(); | ||
let mut to: u64 = kani::any(); | ||
|
||
let from_ptr = &from as *const S; | ||
let to_ptr = &mut to as *mut u64; | ||
|
||
// This should not cause UB since `copy` is untyped. | ||
std::ptr::copy(from_ptr as *const u8, to_ptr as *mut u8, std::mem::size_of::<S>()); | ||
|
||
// This reads uninitialized bytes, which is UB. | ||
let padding: u64 = std::ptr::read(to_ptr); | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/expected/uninit/copy/expose_padding_via_copy_convoluted.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 @@ | ||
std::ptr::read::<u64>.assertion.1\ | ||
- Status: FAILURE\ | ||
- Description: "Undefined Behavior: Reading from an uninitialized pointer of type `*const u64`"\ | ||
|
||
std::ptr::read::<u64>.assertion.2\ | ||
- Status: FAILURE\ | ||
- Description: "Undefined Behavior: Reading from an uninitialized pointer of type `u64`"\ | ||
|
||
Summary: | ||
Verification failed for - expose_padding_via_copy_convoluted | ||
Complete - 0 successfully verified harnesses, 1 failures, 1 total. |
42 changes: 42 additions & 0 deletions
42
tests/expected/uninit/copy/expose_padding_via_copy_convoluted.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,42 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z uninit-checks | ||
|
||
#[repr(C)] | ||
#[derive(kani::Arbitrary)] | ||
struct S(u32, u8); // 5 bytes of data + 3 bytes of padding. | ||
|
||
/// This checks that reading copied uninitialized bytes fails an assertion even if pointer are | ||
/// passed around different functions. | ||
#[kani::proof] | ||
unsafe fn expose_padding_via_copy_convoluted() { | ||
unsafe fn copy_and_read_helper(from_ptr: *const S, to_ptr: *mut u64) -> u64 { | ||
// This should not cause UB since `copy` is untyped. | ||
std::ptr::copy(from_ptr as *const u8, to_ptr as *mut u8, std::mem::size_of::<S>()); | ||
// This reads uninitialized bytes, which is UB. | ||
let padding: u64 = std::ptr::read(to_ptr); | ||
padding | ||
} | ||
|
||
unsafe fn partial_copy_and_read_helper(from_ptr: *const S, to_ptr: *mut u64) -> u32 { | ||
// This should not cause UB since `copy` is untyped. | ||
std::ptr::copy(from_ptr as *const u8, to_ptr as *mut u8, std::mem::size_of::<u32>()); | ||
// This does not read uninitialized bytes. | ||
let not_padding: u32 = std::ptr::read(to_ptr as *mut u32); | ||
not_padding | ||
} | ||
|
||
let flag: bool = kani::any(); | ||
|
||
let from: S = kani::any(); | ||
let mut to: u64 = kani::any(); | ||
|
||
let from_ptr = &from as *const S; | ||
let to_ptr = &mut to as *mut u64; | ||
|
||
if flag { | ||
copy_and_read_helper(from_ptr, to_ptr); | ||
} else { | ||
partial_copy_and_read_helper(from_ptr, to_ptr); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/expected/uninit/copy/expose_padding_via_non_byte_copy.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 @@ | ||
std::ptr::read::<u64>.assertion.1\ | ||
- Status: FAILURE\ | ||
- Description: "Undefined Behavior: Reading from an uninitialized pointer of type `*const u64`"\ | ||
|
||
std::ptr::read::<u64>.assertion.2\ | ||
- Status: FAILURE\ | ||
- Description: "Undefined Behavior: Reading from an uninitialized pointer of type `u64`"\ | ||
|
||
Summary: | ||
Verification failed for - expose_padding_via_non_byte_copy | ||
Complete - 0 successfully verified harnesses, 1 failures, 1 total. |
23 changes: 23 additions & 0 deletions
23
tests/expected/uninit/copy/expose_padding_via_non_byte_copy.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,23 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z uninit-checks | ||
|
||
#[repr(C)] | ||
#[derive(kani::Arbitrary)] | ||
struct S(u32, u8); // 5 bytes of data + 3 bytes of padding. | ||
|
||
/// This checks that reading copied uninitialized bytes after a multi-byte copy fails an assertion. | ||
#[kani::proof] | ||
unsafe fn expose_padding_via_non_byte_copy() { | ||
let from: S = kani::any(); | ||
let mut to: u64 = kani::any(); | ||
|
||
let from_ptr = &from as *const S; | ||
let to_ptr = &mut to as *mut u64; | ||
|
||
// This should not cause UB since `copy` is untyped. | ||
std::ptr::copy(from_ptr as *const u64, to_ptr as *mut u64, 1); | ||
|
||
// This reads uninitialized bytes, which is UB. | ||
let padding: u64 = std::ptr::read(to_ptr); | ||
} |
1 change: 1 addition & 0 deletions
1
tests/expected/uninit/copy/non_byte_copy_without_padding.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 @@ | ||
Complete - 1 successfully verified harnesses, 0 failures, 1 total. |
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.