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 and Harnesses for unchecked_neg #102

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 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
1f0f583
Add unchecked_neg contracts & harnesses
Oct 3, 2024
a45a676
Merge branch 'main' of github.com:rajathkotyal/verify-rust-std into c…
Yenyun035 Oct 4, 2024
57c72f0
Merge branch 'main' into c-0011-core-nums-yenyunw-unchecked-neg
rajathkotyal Oct 7, 2024
ef61321
fixed comments
rajathkotyal Oct 7, 2024
5e1c45f
Fix unchecked_neg precondition && Fix comments
Yenyun035 Oct 8, 2024
cb99f76
Add ensures for unchecked_neg && Fix import
Yenyun035 Oct 8, 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
2 changes: 2 additions & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,8 @@ macro_rules! int_impl {
#[rustc_const_unstable(feature = "unchecked_neg", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[requires(self != $SelfT::MIN)]
Yenyun035 marked this conversation as resolved.
Show resolved Hide resolved
#[ensures(|result| *result == -self)]
pub const unsafe fn unchecked_neg(self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
Expand Down
37 changes: 28 additions & 9 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::str::FromStr;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{ascii, intrinsics, mem};
use safety::requires;
use safety::{requires, ensures};

#[cfg(kani)]
use crate::kani;
Expand Down Expand Up @@ -1661,11 +1661,11 @@ mod verify {
// `unchecked_add` proofs
//
// Target types:
// i{8,16,32,64,128} and u{8,16,32,64,128} -- 10 types in total
// i{8,16,32,64,128,size} and u{8,16,32,64,128,size} -- 12 types in total
//
// Target contracts:
//#[requires(!self.overflowing_sub(rhs).1)] // Preconditions: No overflow should occur
//#[ensures(|ret| *ret >= Self::MIN && *ret <= Self::MAX)] // Postconditions: Result must be within valid bounds
// Preconditions: No overflow should occur
// #[requires(!self.overflowing_add(rhs).1)]
//
// Target function:
// pub const unsafe fn unchecked_add(self, rhs: Self) -> Self
Expand All @@ -1682,12 +1682,31 @@ mod verify {
generate_unchecked_math_harness!(u128, unchecked_add, checked_unchecked_add_u128);
generate_unchecked_math_harness!(usize, unchecked_add, checked_unchecked_add_usize);

// `unchecked_neg` proofs
//
// Target types:
// i{8,16,32,64,128,size} -- 6 types in total
//
// Target contracts:
// #[requires(self != $SelfT::MIN)]
//
// Target function:
// pub const unsafe fn unchecked_neg(self) -> Self
generate_unchecked_neg_harness!(i8, checked_unchecked_neg_i8);
generate_unchecked_neg_harness!(i16, checked_unchecked_neg_i16);
generate_unchecked_neg_harness!(i32, checked_unchecked_neg_i32);
generate_unchecked_neg_harness!(i64, checked_unchecked_neg_i64);
generate_unchecked_neg_harness!(i128, checked_unchecked_neg_i128);
generate_unchecked_neg_harness!(isize, checked_unchecked_neg_isize);

// unchecked_mul proofs
//
// Target types:
// i{8,16,32,64,128, size} and u{8,16,32,64,128, size} -- 36 types in total
// i{8,16,32,64,128,size} and u{8,16,32,64,128,size} -- 12 types in total, with different interval checks for each.
// Total types of checks including intervals -- 36
//
// Target contracts:
// Preconditions: No overflow should occur
// #[requires(!self.overflowing_mul(rhs).1)]
//
// Target function:
Expand Down Expand Up @@ -1783,11 +1802,10 @@ mod verify {
// `unchecked_shl` proofs
//
// Target types:
// i{8,16,32,64,128} and u{8,16,32,64,128} -- 12 types in total
// i{8,16,32,64,128,size} and u{8,16,32,64,128,size} -- 12 types in total
//
// Target contracts:
// #[requires(shift < Self::BITS)]
// #[ensures(|ret| *ret == self << shift)]
//
// Target function:
// pub const unsafe fn unchecked_shl(self, shift: u32) -> Self
Expand All @@ -1809,10 +1827,11 @@ mod verify {
// `unchecked_sub` proofs
//
// Target types:
// i{8,16,32,64,128} and u{8,16,32,64,128} -- 12 types in total
// i{8,16,32,64,128,size} and u{8,16,32,64,128,size} -- 12 types in total
//
// Target contracts:
// #[requires(!self.overflowing_sub(rhs).1)] // Preconditions: No overflow should occur
Yenyun035 marked this conversation as resolved.
Show resolved Hide resolved
// Preconditions: No overflow should occur
// #[requires(!self.overflowing_sub(rhs).1)]
//
// Target function:
// pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self
Expand Down
Loading