-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor overflow checks in intrinsics code (#1131)
* Modify expected tests * Final changes * Change expectation and add comment * Use `count` instead of `number` * Add comment above `v_wrap` assignment * Review overflow check in `ptr_offset_from` * Update test that triggers failure, add a new one for wrap-around * Remove instance argument Co-authored-by: Celina G. Val <celinval@amazon.com>
- Loading branch information
1 parent
0ccd0a0
commit 1a61de2
Showing
7 changed files
with
94 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
FAILURE\ | ||
write_bytes: attempt to compute `bytes` which would overflow | ||
write_bytes: attempt to compute number in bytes which would overflow |
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
FAILURE\ | ||
attempt to compute offset in bytes which would overflow | ||
offset: attempt to compute number in bytes which would overflow |
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
FAILURE\ | ||
attempt to compute offset in bytes which would overflow | ||
attempt to compute offset which would overflow | ||
FAILURE\ | ||
attempt to compute offset in bytes which would overflow an `isize` |
This file contains 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 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\ | ||
assertion failed: high_offset == wrapped_offset.try_into().unwrap() |
This file contains 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,35 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
// Check that a high offset causes a "wrapping around" behavior in CBMC. | ||
|
||
// This example can be really confusing. This program works fine in Rust and | ||
// it's okay to assert that the value coming from `offset_from` is equal to | ||
// `high_offset`. But CBMC's memory model is going to cause a "wrapping around" | ||
// behavior in `v_wrap`, so any values that depend on it are going to show a | ||
// strange behavior as well. | ||
use std::convert::TryInto; | ||
|
||
#[kani::proof] | ||
fn main() { | ||
let v: &[u128] = &[0; 10]; | ||
let v_0: *const u128 = &v[0]; | ||
let high_offset = usize::MAX / (std::mem::size_of::<u128>() * 4); | ||
unsafe { | ||
// Adding `high offset` to `v_0` is undefined behavior, but Kani's | ||
// default behavior does not report it. This kind of operations | ||
// are quite common in the standard library, and we disabled such | ||
// checks in order to avoid spurious verification failures. | ||
// | ||
// Note that this instance of undefined behavior will be reported | ||
// by `miri` and also by Kani with `--extra-pointer-checks`. | ||
// Also, dereferencing the pointer will also be reported by Kani's | ||
// default behavior. | ||
let v_wrap: *const u128 = v_0.add(high_offset.try_into().unwrap()); | ||
let wrapped_offset = v_wrap.offset_from(v_0); | ||
// Both offsets should be the same, but because of the "wrapping around" | ||
// behavior in CBMC, `wrapped_offset` does not equal `high_offset` | ||
// https://github.com/model-checking/kani/issues/1150 | ||
assert!(high_offset == wrapped_offset.try_into().unwrap()); | ||
} | ||
} |