-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the computation of the number of bytes of a pointer offset (#3584)
This PR fixes the logic that computes the number of bytes of a pointer offset. The logic was incorrectly using the size of the pointer as opposed to the size of the pointee. This fixes the soundness issue in #3582 (but not the spurious failures). By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
- Loading branch information
1 parent
f17cc4d
commit c645752
Showing
6 changed files
with
41 additions
and
6 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\ | ||
attempt to compute offset which would overflow | ||
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 which would overflow | ||
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
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,5 @@ | ||
std::ptr::const_ptr::<impl *const Foo>::offset.arithmetic_overflow\ | ||
Status: FAILURE\ | ||
Description: "offset: attempt to compute number in bytes which would overflow" | ||
|
||
VERIFICATION:- FAILED |
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,27 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! This test checks that Kani detects the overflow in pointer offset | ||
|
||
use std::convert::TryFrom; | ||
|
||
struct Foo { | ||
arr: [i32; 4096], | ||
} | ||
|
||
#[cfg_attr(kani, kani::proof)] | ||
fn main() { | ||
let f = Foo { arr: [0; 4096] }; | ||
assert_eq!(std::mem::size_of::<Foo>(), 16384); | ||
// a large enough count that causes `count * size_of::<Foo>()` to overflow | ||
// `isize` without overflowing `usize` | ||
let count: usize = 562949953421320; | ||
// the `unwrap` ensures that it indeed doesn't overflow `usize` | ||
let bytes = count.checked_mul(std::mem::size_of::<Foo>()).unwrap(); | ||
// ensure that it overflows `isize`: | ||
assert!(isize::try_from(bytes).is_err()); | ||
|
||
let ptr: *const Foo = &f as *const Foo; | ||
// this should fail because `count * size_of::<Foo>` overflows `isize` | ||
let _p = unsafe { ptr.offset(count as isize) }; | ||
} |