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 f{32,64}::to_int_unchecked #134

Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
95d11f3
f32 test harness & contracts
Yenyun035 Oct 22, 2024
88f7363
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-f32-…
Yenyun035 Oct 23, 2024
227d9ac
Fix contracts && Initial draft of macro and harnesses
Yenyun035 Oct 24, 2024
b875696
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-f32-…
Yenyun035 Oct 24, 2024
70fe5d9
Merge main
Yenyun035 Oct 26, 2024
5742a89
Merge branch 'main' of github.com:rajathkotyal/verify-rust-std into c…
Yenyun035 Oct 28, 2024
082ea5a
f128_float_to_int proofs
rajathkotyal Oct 30, 2024
bb00a72
Fix comment
Yenyun035 Oct 30, 2024
88c3411
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-f32-…
Yenyun035 Oct 30, 2024
a661c16
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Oct 30, 2024
c85ce2a
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-f32-…
Yenyun035 Oct 31, 2024
573e0a8
f64_float_to_int proofs
MWDZ Nov 7, 2024
39c6d63
Merge branch 'main' of github.com:rajathkotyal/verify-rust-std into c…
Yenyun035 Nov 7, 2024
1171a9c
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 8, 2024
869fe66
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 9, 2024
47b31f4
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 11, 2024
13b1c22
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 12, 2024
667edb8
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 13, 2024
454b66b
Remove f128 proofs; will be added later
Yenyun035 Nov 13, 2024
8bff73a
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 18, 2024
c2f7931
remove f128 contract; will added in PR#163
Yenyun035 Nov 19, 2024
64d2671
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 20, 2024
02229a1
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 21, 2024
ab18a71
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 22, 2024
a9def8b
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 22, 2024
05f6e40
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 23, 2024
0646ad2
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 25, 2024
277216c
Remove incorrect contract code
Yenyun035 Nov 25, 2024
713a7ff
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 26, 2024
2efb024
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 27, 2024
26a02d9
Add placeholder for f32,f64 to_int_unchecked contract
Yenyun035 Nov 27, 2024
149fe91
Fix contracts
Yenyun035 Nov 28, 2024
8342aec
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 28, 2024
3590276
Fix code and comments
Yenyun035 Nov 28, 2024
91989c5
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 28, 2024
8151c17
Remove unused import
Yenyun035 Nov 28, 2024
1cccd09
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 29, 2024
d57dde0
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Nov 29, 2024
6898b71
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Dec 1, 2024
1b08327
Enable float-lib in kani script
Yenyun035 Dec 2, 2024
895f055
Add float_to_int_in_range to ub_checks && Fix contracts
Yenyun035 Dec 2, 2024
8aefc7c
Fix dummy float_to_int_in_range
Yenyun035 Dec 2, 2024
e9200b2
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Dec 3, 2024
0fec08b
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
Yenyun035 Dec 3, 2024
e4d69a7
Fix import and format
Yenyun035 Dec 3, 2024
3afbb9d
Fix import
Yenyun035 Dec 3, 2024
7b978eb
Add -Z float-lib to list command
zhassan-aws Dec 4, 2024
5efede8
Fix import
Yenyun035 Dec 4, 2024
6e7838f
Merge branch 'main' into c-0011-core-nums-yenyunw-f32-to-int-unchecked
zhassan-aws Dec 4, 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
8 changes: 8 additions & 0 deletions library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ use crate::mem;
use crate::num::FpCategory;
use crate::panic::const_assert;

use safety::requires;
use crate::ub_checks::float_to_int_in_range;

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

/// The radix or base of the internal representation of `f32`.
/// Use [`f32::RADIX`] instead.
///
Expand Down Expand Up @@ -1054,6 +1060,8 @@ impl f32 {
without modifying the original"]
#[stable(feature = "float_approx_unchecked_to", since = "1.44.0")]
#[inline]
// is_finite() checks if the given float is neither infinite nor NaN.
#[requires(self.is_finite() && float_to_int_in_range::<Self, Int>(self))]
pub unsafe fn to_int_unchecked<Int>(self) -> Int
where
Self: FloatToInt<Int>,
Expand Down
7 changes: 7 additions & 0 deletions library/core/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ use crate::intrinsics;
use crate::mem;
use crate::num::FpCategory;
use crate::panic::const_assert;
use safety::requires;
use crate::ub_checks::float_to_int_in_range;

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

/// The radix or base of the internal representation of `f64`.
/// Use [`f64::RADIX`] instead.
Expand Down Expand Up @@ -1055,6 +1060,8 @@ impl f64 {
without modifying the original"]
#[stable(feature = "float_approx_unchecked_to", since = "1.44.0")]
#[inline]
// is_finite() checks if the given float is neither infinite nor NaN.
#[requires(self.is_finite() && float_to_int_in_range::<Self, Int>(self))]
pub unsafe fn to_int_unchecked<Int>(self) -> Int
where
Self: FloatToInt<Int>,
Expand Down
57 changes: 57 additions & 0 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,21 @@ mod verify {
}
}

// Part 3: Float to Integer Conversion function Harness Generation Macro
macro_rules! generate_to_int_unchecked_harness {
($floatType:ty, $($intType:ty, $harness_name:ident),+) => {
$(
#[kani::proof_for_contract($floatType::to_int_unchecked)]
pub fn $harness_name() {
let num1: $floatType = kani::any::<$floatType>();
let result = unsafe { num1.to_int_unchecked::<$intType>() };

assert_eq!(result, num1 as $intType);
}
)+
}
}

// `unchecked_add` proofs
//
// Target types:
Expand Down Expand Up @@ -2128,4 +2143,46 @@ mod verify {
generate_wrapping_shift_harness!(u128, wrapping_shr, checked_wrapping_shr_u128);
generate_wrapping_shift_harness!(usize, wrapping_shr, checked_wrapping_shr_usize);

// `f{16,32,64,128}::to_int_unchecked` proofs
//
// Target integer types:
// i{8,16,32,64,128,size} and u{8,16,32,64,128,size} -- 12 types in total
//
// Target contracts:
// 1. Float is not `NaN` and infinite
// 2. Float is representable in the return type `Int`, after truncating
// off its fractional part
// [requires(self.is_finite() && kani::float::float_to_int_in_range::<Self, Int>(self))]
//
// Target function:
// pub unsafe fn to_int_unchecked<Int>(self) -> Int where Self: FloatToInt<Int>
generate_to_int_unchecked_harness!(f32,
i8, checked_f32_to_int_unchecked_i8,
i16, checked_f32_to_int_unchecked_i16,
i32, checked_f32_to_int_unchecked_i32,
i64, checked_f32_to_int_unchecked_i64,
i128, checked_f32_to_int_unchecked_i128,
isize, checked_f32_to_int_unchecked_isize,
u8, checked_f32_to_int_unchecked_u8,
u16, checked_f32_to_int_unchecked_u16,
u32, checked_f32_to_int_unchecked_u32,
u64, checked_f32_to_int_unchecked_u64,
u128, checked_f32_to_int_unchecked_u128,
usize, checked_f32_to_int_unchecked_usize
);

generate_to_int_unchecked_harness!(f64,
i8, checked_f64_to_int_unchecked_i8,
i16, checked_f64_to_int_unchecked_i16,
i32, checked_f64_to_int_unchecked_i32,
i64, checked_f64_to_int_unchecked_i64,
i128, checked_f64_to_int_unchecked_i128,
isize, checked_f64_to_int_unchecked_isize,
u8, checked_f64_to_int_unchecked_u8,
u16, checked_f64_to_int_unchecked_u16,
u32, checked_f64_to_int_unchecked_u32,
u64, checked_f64_to_int_unchecked_u64,
u128, checked_f64_to_int_unchecked_u128,
usize, checked_f64_to_int_unchecked_usize
);
}
10 changes: 10 additions & 0 deletions library/core/src/ub_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! common preconditions.

use crate::intrinsics::{self, const_eval_select};
use crate::convert::FloatToInt;
Yenyun035 marked this conversation as resolved.
Show resolved Hide resolved

/// Checks that the preconditions of an unsafe function are followed.
///
Expand Down Expand Up @@ -219,12 +220,21 @@ mod predicates {
let _ = (src, dst);
true
}

/// Check if a float is representable in the given integer type
pub fn float_to_int_in_range<Float, Int>(value: Float) -> bool
where
Float: core::convert::FloatToInt<Int> {
Yenyun035 marked this conversation as resolved.
Show resolved Hide resolved
let _ = value;
true
}
}

#[cfg(kani)]
mod predicates {
pub use crate::kani::mem::{can_dereference, can_write, can_read_unaligned, can_write_unaligned,
same_allocation};
pub use crate::kani::float::float_to_int_in_range;
}

/// This trait should be used to specify and check type safety invariants for a
Expand Down
1 change: 1 addition & 0 deletions scripts/run-kani.sh
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ main() {
-Z function-contracts \
-Z mem-predicates \
-Z loop-contracts \
-Z float-lib \
--output-format=terse \
$command_args \
--enable-unstable \
Expand Down
Loading