Skip to content
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

Contracts & Harnesses for unchecked_add #91

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1ae4c6d
Add Kani proofs of unchecked_add for all integer types
Sep 11, 2024
b553678
Format proof files & Add verification function templates
Sep 11, 2024
daaaf7e
Experiment with two verification approaches in mod.rs of core::num
Sep 16, 2024
7bd0e3c
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-unsa…
Yenyun035 Sep 18, 2024
1c9dbde
Add contracts to unchecked_add && Add i8::unchecked_add proof
Sep 18, 2024
894231f
Format core::num mod.rs
Sep 18, 2024
2bc4faf
Add comment for unchecked_add proofs
Sep 18, 2024
d9d4b5f
Add harnesses for i16,i32,i64,i128 unchecked_add
Sep 19, 2024
07c8b4a
Add harnesses for u8,u16,u32,u64,u128 unchecked_add
Sep 19, 2024
1fd4c6a
Cleanup misplaced proofs
Sep 19, 2024
b360311
Clean up comment
Sep 20, 2024
8cbca87
Format comment
Sep 20, 2024
0eef858
Remove before contracts. Fix import in
Sep 20, 2024
4858a53
Fix comment
Sep 20, 2024
483bdf5
Merge branch 'main' into c-0011-core-nums-yenyunw-unsafe-ints
Yenyun035 Sep 23, 2024
ce35002
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-unsa…
Yenyun035 Sep 24, 2024
d99844d
Remove ensures contracts && Undo formatting on existing code
Sep 24, 2024
fbcf49e
Add {isize, usize}::unchecked_add harnesses
Sep 24, 2024
457a2b5
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-unsa…
Yenyun035 Sep 27, 2024
54a03ef
Add harness generation macros for unchecked methods && Update uncheck…
Sep 27, 2024
8ee5682
Remove unused import safety::ensures
Sep 30, 2024
02d706a
unchecked_mul and unchecked_shr proofs (#7)
rajathkotyal Sep 30, 2024
dce9e83
Add comments. Fix spacing
Sep 30, 2024
3880fc7
Revert "Add comments. Fix spacing"
rajathkotyal Oct 2, 2024
781cb87
Revert "unchecked_mul and unchecked_shr proofs (#7)"
rajathkotyal Oct 2, 2024
715e940
Merge branch 'main' into c-0011-core-nums-yenyunw-unsafe-ints
Yenyun035 Oct 2, 2024
a3bf6d1
Change generate_unchecked_neg_harness macro
Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ macro_rules! int_impl {
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[requires(!self.overflowing_add(rhs).1)]
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
Expand Down
74 changes: 74 additions & 0 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use crate::str::FromStr;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{ascii, intrinsics, mem};
use safety::requires;

#[cfg(kani)]
use crate::kani;

// Used because the `?` operator is not allowed in a const context.
macro_rules! try_opt {
Expand Down Expand Up @@ -1581,3 +1585,73 @@ from_str_radix_size_impl! { i16 isize, u16 usize }
from_str_radix_size_impl! { i32 isize, u32 usize }
#[cfg(target_pointer_width = "64")]
from_str_radix_size_impl! { i64 isize, u64 usize }

#[cfg(kani)]
#[unstable(feature = "kani", issue = "none")]
Yenyun035 marked this conversation as resolved.
Show resolved Hide resolved
mod verify {
Yenyun035 marked this conversation as resolved.
Show resolved Hide resolved
use super::*;

macro_rules! generate_unchecked_math_harness {
($type:ty, $method:ident, $harness_name:ident) => {
#[kani::proof_for_contract($type::$method)]
pub fn $harness_name() {
let num1: $type = kani::any::<$type>();
let num2: $type = kani::any::<$type>();

unsafe {
num1.$method(num2);
}
}
}
}

macro_rules! generate_unchecked_shift_harness {
($type:ty, $method:ident, $harness_name:ident) => {
#[kani::proof_for_contract($type::$method)]
pub fn $harness_name() {
let num1: $type = kani::any::<$type>();
let num2: u32 = kani::any::<u32>();

unsafe {
num1.$method(num2);
}
}
}
}

macro_rules! generate_unchecked_neg_harness {
($type:ty, $harness_name:ident) => {
#[kani::proof_for_contract($type::unchecked_neg)]
pub fn $harness_name() {
let num1: $type = kani::any::<$type>();

unsafe {
num1.unchecked_neg();
}
}
}
}

// `unchecked_add` proofs
//
// Target types:
// i{8,16,32,64,128, size} and u{8,16,32,64,128, size} -- 12 types in total
//
// Target contracts:
// #[requires(!self.overflowing_add(rhs).1)]
//
// Target function:
// pub const unsafe fn unchecked_add(self, rhs: Self) -> Self
generate_unchecked_math_harness!(i8, unchecked_add, checked_unchecked_add_i8);
generate_unchecked_math_harness!(i16, unchecked_add, checked_unchecked_add_i16);
generate_unchecked_math_harness!(i32, unchecked_add, checked_unchecked_add_i32);
generate_unchecked_math_harness!(i64, unchecked_add, checked_unchecked_add_i64);
generate_unchecked_math_harness!(i128, unchecked_add, checked_unchecked_add_i128);
generate_unchecked_math_harness!(isize, unchecked_add, checked_unchecked_add_isize);
generate_unchecked_math_harness!(u8, unchecked_add, checked_unchecked_add_u8);
generate_unchecked_math_harness!(u16, unchecked_add, checked_unchecked_add_u16);
generate_unchecked_math_harness!(u32, unchecked_add, checked_unchecked_add_u32);
generate_unchecked_math_harness!(u64, unchecked_add, checked_unchecked_add_u64);
generate_unchecked_math_harness!(u128, unchecked_add, checked_unchecked_add_u128);
generate_unchecked_math_harness!(usize, unchecked_add, checked_unchecked_add_usize);
}
1 change: 1 addition & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[requires(!self.overflowing_add(rhs).1)]
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
Expand Down
Loading