forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix spurious overflow counter examples. (rust-lang#558) (rust-lang#647)
We cannot easily specify which arithmetic operations are wrapping ones to CBMC. Thus, CBMC was generating overflow checks for wrapping operations which were generating spurious failures. This change disables some of those CBMC checks. We replace them by using rustc overflow checks for regular operations and by explicitly adding checks to unchecked operations.
- Loading branch information
Showing
25 changed files
with
305 additions
and
65 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
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
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
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,4 +1,4 @@ | ||
rmc-rustc -Z symbol-mangling-version=v0 -Z symbol_table_passes= | ||
symtab2gb | ||
goto-cc --function main | ||
cbmc --bounds-check --pointer-check --pointer-primitive-check --conversion-check --div-by-zero-check --float-overflow-check --nan-check --pointer-overflow-check --signed-overflow-check --undefined-shift-check --unsigned-overflow-check --unwinding-assertions --object-bits 16 --function main | ||
cbmc --bounds-check --pointer-check --pointer-primitive-check --conversion-check --div-by-zero-check --float-overflow-check --nan-check --pointer-overflow-check --undefined-shift-check --unwinding-assertions --object-bits 16 --function main |
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,22 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that none of these operations trigger spurious overflow checks. | ||
#![feature(unchecked_math)] | ||
|
||
macro_rules! verify_no_overflow { | ||
($cf: ident, $uf: ident) => {{ | ||
let a: u8 = rmc::nondet(); | ||
let b: u8 = rmc::nondet(); | ||
let checked = a.$cf(b); | ||
rmc::assume(checked.is_some()); | ||
let unchecked = unsafe { a.$uf(b) }; | ||
assert!(checked.unwrap() == unchecked); | ||
}}; | ||
} | ||
|
||
fn main() { | ||
verify_no_overflow!(checked_add, unchecked_add); | ||
verify_no_overflow!(checked_sub, unchecked_sub); | ||
verify_no_overflow!(checked_mul, unchecked_mul); | ||
} |
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,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that unchecked add trigger overflow checks. | ||
// rmc-verify-fail | ||
|
||
#![feature(unchecked_math)] | ||
|
||
pub fn main() { | ||
let a: u8 = rmc::nondet(); | ||
let b: u8 = rmc::nondet(); | ||
unsafe { a.unchecked_add(b) }; | ||
} |
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,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that unchecked mul trigger overflow checks. | ||
// rmc-verify-fail | ||
|
||
#![feature(unchecked_math)] | ||
|
||
pub fn main() { | ||
let a: u8 = rmc::nondet(); | ||
let b: u8 = rmc::nondet(); | ||
unsafe { a.unchecked_mul(b) }; | ||
} |
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,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that unchecked sub trigger overflow checks. | ||
// rmc-verify-fail | ||
|
||
#![feature(unchecked_math)] | ||
|
||
fn main() { | ||
let a: u8 = rmc::nondet(); | ||
let b: u8 = rmc::nondet(); | ||
unsafe { a.unchecked_sub(b) }; | ||
} |
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,21 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that none of these operations trigger spurious overflow checks. | ||
|
||
macro_rules! verify_no_overflow { | ||
($cf: ident, $uf: tt) => {{ | ||
let a: u8 = rmc::nondet(); | ||
let b: u8 = rmc::nondet(); | ||
let checked = a.$cf(b); | ||
rmc::assume(checked.is_some()); | ||
let unchecked = unsafe { a $uf b }; | ||
assert!(checked.unwrap() == unchecked); | ||
}}; | ||
} | ||
|
||
fn main() { | ||
verify_no_overflow!(checked_add, +); | ||
verify_no_overflow!(checked_sub, -); | ||
verify_no_overflow!(checked_mul, *); | ||
} |
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,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that regular arithmetic operations in unsafe blocks still trigger overflow checks. | ||
// rmc-verify-fail | ||
// rmc-flags: --function check_add | ||
// compile-flags: --crate-type lib | ||
|
||
pub fn check_add(a: u8, b: u8) { | ||
unsafe { | ||
a + b; | ||
} | ||
} |
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,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that regular arithmetic operations in unsafe blocks still trigger overflow checks. | ||
// rmc-verify-fail | ||
// rmc-flags: --function check_mul | ||
// compile-flags: --crate-type lib | ||
|
||
pub fn check_add(a: u8, b: u8) { | ||
unsafe { | ||
a * b; | ||
} | ||
} |
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,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that regular arithmetic operations in unsafe blocks still trigger overflow checks. | ||
// rmc-verify-fail | ||
// rmc-flags: --function check_sub | ||
// compile-flags: --crate-type lib | ||
|
||
pub fn check_sub(a: u8, b: u8) { | ||
unsafe { | ||
a - b; | ||
} | ||
} |
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,16 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Check that none of these operations trigger spurious overflow checks. | ||
#![feature(core_intrinsics)] | ||
|
||
fn main() { | ||
let a: u8 = rmc::nondet(); | ||
let b: u8 = rmc::nondet(); | ||
let sum0 = core::intrinsics::wrapping_add(a, b); | ||
let sum1 = a.wrapping_add(b); | ||
let sum2 = a.checked_add(b); | ||
assert!(sum0 == sum1); | ||
assert!(sum1 >= b || sum2.is_none()); | ||
assert!(sum1 >= a || sum2.is_none()); | ||
} |
Oops, something went wrong.