generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 142
Audit for write_bytes
#1102
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
Merged
Audit for write_bytes
#1102
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a5463b
Audit for `write_bytes`
adpaco-aws a67180d
Add tests for `write_bytes`
adpaco-aws bc20e54
Update comment
adpaco-aws 4bba955
Remove loc clone
adpaco-aws 3c33ffe
Check overflows when computing `bytes`
adpaco-aws 7bbc16b
Add test for expected overflows
adpaco-aws e448686
Remove `loc` cloning
adpaco-aws d11deaf
Merge branch 'main' into write_bytes-audit
danielsn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -622,16 +622,9 @@ impl<'tcx> GotocCtx<'tcx> { | |
| "wrapping_mul" => codegen_wrapping_op!(mul), | ||
| "wrapping_sub" => codegen_wrapping_op!(sub), | ||
| "write_bytes" => { | ||
| let dst = fargs.remove(0).cast_to(Type::void_pointer()); | ||
| let val = fargs.remove(0).cast_to(Type::c_int()); | ||
| let count = fargs.remove(0); | ||
| let ty = self.monomorphize(instance.substs.type_at(0)); | ||
| let layout = self.layout_of(ty); | ||
| let sz = Expr::int_constant(layout.size.bytes(), Type::size_t()); | ||
| let e = BuiltinFn::Memset.call(vec![dst, val, count.mul(sz)], loc); | ||
| self.codegen_expr_to_place(p, e) | ||
| assert!(self.place_ty(p).is_unit()); | ||
| self.codegen_write_bytes(instance, intrinsic, fargs, loc) | ||
| } | ||
|
|
||
| // Unimplemented | ||
| _ => codegen_unimplemented_intrinsic!( | ||
| "https://github.com/model-checking/kani/issues/new/choose" | ||
|
|
@@ -1188,4 +1181,48 @@ impl<'tcx> GotocCtx<'tcx> { | |
| let expr = dst.dereference().assign(src, loc.clone()); | ||
| Stmt::block(vec![align_check, expr], loc) | ||
| } | ||
|
|
||
| /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to `val` | ||
| /// https://doc.rust-lang.org/std/ptr/fn.write_bytes.html | ||
| /// | ||
| /// Undefined behavior if any of these conditions are violated: | ||
| /// * `dst` must be valid for writes (done by memset writable check) | ||
| /// * `dst` must be properly aligned (done by `align_check` below) | ||
| /// In addition, we check that computing `bytes` (i.e., the third argument | ||
| /// for the `memset` call) would not overflow | ||
| fn codegen_write_bytes( | ||
| &mut self, | ||
| instance: Instance<'tcx>, | ||
| intrinsic: &str, | ||
| mut fargs: Vec<Expr>, | ||
| loc: Location, | ||
| ) -> Stmt { | ||
| let dst = fargs.remove(0).cast_to(Type::void_pointer()); | ||
| let val = fargs.remove(0).cast_to(Type::c_int()); | ||
| let count = fargs.remove(0); | ||
|
|
||
| // Check that `dst` is properly aligned | ||
| let ty = self.monomorphize(instance.substs.type_at(0)); | ||
| let align = self.is_aligned(ty, dst.clone()); | ||
| let align_check = self.codegen_assert( | ||
| align, | ||
| PropertyClass::DefaultAssertion, | ||
| "`dst` is properly aligned", | ||
| loc, | ||
| ); | ||
|
|
||
| // Check that computing `bytes` would not overflow | ||
| let layout = self.layout_of(ty); | ||
| let size = Expr::int_constant(layout.size.bytes(), Type::size_t()); | ||
| let bytes = count.mul_overflow(size); | ||
| let overflow_check = self.codegen_assert( | ||
| bytes.overflowed.not(), | ||
| PropertyClass::ArithmeticOverflow, | ||
| format!("{}: attempt to compute `bytes` which would overflow", intrinsic).as_str(), | ||
| loc, | ||
| ); | ||
|
Comment on lines
+1214
to
+1223
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR, but we've done this check > 1 times, which means a helper fn might be good |
||
|
|
||
| let memset_call = BuiltinFn::Memset.call(vec![dst, val, bytes.result], loc); | ||
| Stmt::block(vec![align_check, overflow_check, memset_call.as_stmt(loc)], loc) | ||
| } | ||
| } | ||
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,2 @@ | ||
| FAILURE\ | ||
| memset destination region writeable | ||
danielsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
19 changes: 19 additions & 0 deletions
19
tests/expected/intrinsics/write_bytes/out-of-bounds/main.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,19 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Checks that `write_bytes` fails if an out-of-bounds write is made. | ||
|
|
||
| // This test is a modified version of the example in | ||
| // https://doc.rust-lang.org/std/ptr/fn.write_bytes.html | ||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::write_bytes; | ||
|
|
||
| #[kani::proof] | ||
| fn main() { | ||
| let mut vec = vec![0u32; 4]; | ||
| unsafe { | ||
| let vec_ptr = vec.as_mut_ptr().add(4); | ||
| write_bytes(vec_ptr, 0xfe, 1); | ||
| } | ||
| assert_eq!(vec, [0, 0, 0, 0]); | ||
| } |
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,2 @@ | ||
| FAILURE\ | ||
| write_bytes: attempt to compute `bytes` which would overflow |
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,20 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Checks that `write_bytes` triggers the overflow check. | ||
|
|
||
| // This test is a modified version of the example in | ||
| // https://doc.rust-lang.org/std/ptr/fn.write_bytes.html | ||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::write_bytes; | ||
|
|
||
| #[kani::proof] | ||
| fn main() { | ||
| let mut vec = vec![0u32; 4]; | ||
| unsafe { | ||
| let vec_ptr = vec.as_mut_ptr(); | ||
| // Passing `usize::MAX + 1` is guaranteed to | ||
| // overflow when computing the number of bytes | ||
| write_bytes(vec_ptr, 0xfe, usize::MAX / 4 + 1); | ||
| } | ||
| } |
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,2 @@ | ||
| FAILURE\ | ||
| `dst` is properly aligned |
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,22 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Checks that `write_bytes` fails when `dst` is not aligned. | ||
|
|
||
| // This test is a modified version of the example in | ||
| // https://doc.rust-lang.org/std/ptr/fn.write_bytes.html | ||
| use std::intrinsics::write_bytes; | ||
|
|
||
| #[kani::proof] | ||
| fn main() { | ||
| let mut vec = vec![0u32; 4]; | ||
| unsafe { | ||
| let vec_ptr = vec.as_mut_ptr(); | ||
| // Obtain an unaligned pointer by casting into `*mut u8`, | ||
| // adding an offset of 1 and casting back into `*mut u32` | ||
| let vec_ptr_u8: *mut u8 = vec_ptr as *mut u8; | ||
| let unaligned_ptr = vec_ptr_u8.add(1) as *mut u32; | ||
| write_bytes(unaligned_ptr, 0xfe, 2); | ||
| } | ||
| assert_eq!(vec, [0xfefefe00, 0xfefefefe, 0x000000fe, 0]); | ||
| } |
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,19 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Checks that `write_bytes` works as expected. | ||
|
|
||
| // This test is a modified version of the example in | ||
| // https://doc.rust-lang.org/std/ptr/fn.write_bytes.html | ||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::write_bytes; | ||
|
|
||
| #[kani::proof] | ||
| fn main() { | ||
| let mut vec = vec![0u32; 4]; | ||
| unsafe { | ||
| let vec_ptr = vec.as_mut_ptr(); | ||
| write_bytes(vec_ptr, 0xfe, 2); | ||
| } | ||
| assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like the callout to the documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄