Skip to content

Commit 6be9ca4

Browse files
Remove spurious comments about the need for quantifiers (#457)
None of the (three) cases where we previously claimed a need for quantifiers actually require them. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Co-authored-by: Felipe R. Monteiro <[email protected]>
1 parent 53e9e1b commit 6be9ca4

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

library/core/src/intrinsics/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,14 +2839,16 @@ pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + PointeeSized, M>(ptr:
28392839
/// initialization state.
28402840
///
28412841
/// This is used for contracts only.
2842-
///
2843-
/// FIXME: Change this once we add support to quantifiers.
28442842
#[allow(dead_code)]
28452843
#[allow(unused_variables)]
28462844
fn check_copy_untyped<T>(src: *const T, dst: *mut T, count: usize) -> bool {
28472845
#[cfg(kani)]
28482846
if count > 0 {
2847+
// Inspect a non-deterministically chosen byte in the copy.
28492848
let byte = kani::any_where(|sz: &usize| *sz < size_of::<T>());
2849+
// Instead of checking each of the `count`-many copies, non-deterministically pick one of
2850+
// them and check it. Using quantifiers would not add value as we can rely on the solver to
2851+
// pick an uninitialized element if such an element exists.
28502852
let elem = kani::any_where(|val: &usize| *val < count);
28512853
let src_data = src as *const u8;
28522854
let dst_data = unsafe { dst.add(elem) } as *const u8;

library/core/src/ptr/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,9 +2207,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
22072207
}
22082208
22092209
// Checking if the answer should indeed be usize::MAX when a % stride != 0
2210-
// requires computing gcd(a, stride), which is too expensive without
2211-
// quantifiers (https://model-checking.github.io/kani/rfc/rfcs/0010-quantifiers.html).
2212-
// This should be updated once quantifiers are available.
2210+
// requires computing gcd(a, stride), which could be done using cttz as the implementation
2211+
// does.
22132212
if (a % stride != 0 && *result == usize::MAX) {
22142213
return true;
22152214
}
@@ -2240,12 +2239,10 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
22402239
/// Implementation of this function shall not panic. Ever.
22412240
#[safety::requires(m.is_power_of_two())]
22422241
#[safety::requires(x < m)]
2243-
// TODO: add ensures contract to check that the answer is indeed correct
2244-
// This will require quantifiers (https://model-checking.github.io/kani/rfc/rfcs/0010-quantifiers.html)
2245-
// so that we can add a precondition that gcd(x, m) = 1 like so:
2246-
// ∀d, d > 0 ∧ x % d = 0 ∧ m % d = 0 → d = 1
2247-
// With this precondition, we can then write this postcondition to check the correctness of the answer:
2248-
// #[safety::ensures(|result| wrapping_mul(*result, x) % m == 1)]
2242+
#[safety::requires(x % 2 != 0)]
2243+
// for Kani (v0.65.0), the below multiplication is too costly to prove
2244+
#[cfg_attr(not(kani),
2245+
safety::ensures(|result| wrapping_mul(*result, x) % m == 1))]
22492246
#[inline]
22502247
const unsafe fn mod_inv(x: usize, m: usize) -> usize {
22512248
/// Multiplicative modular inverse table modulo 2⁴ = 16.

library/core/src/ptr/non_null.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,10 +1441,9 @@ impl<T: PointeeSized> NonNull<T> {
14411441
if (align % stride == 0) && (self.pointer.addr() % stride != 0) {
14421442
return *result == usize::MAX;
14431443
}
1444-
// Checking if the answer should indeed be usize::MAX when align % stride != 0
1445-
// requires computing gcd(a, stride), which is too expensive without
1446-
// quantifiers (https://model-checking.github.io/kani/rfc/rfcs/0010-quantifiers.html).
1447-
// This should be updated once quantifiers are available.
1444+
// Checking if the answer should indeed be usize::MAX when a % stride != 0 requires
1445+
// computing gcd(align, stride), which could be done using cttz as the implementation of
1446+
// ptr::align_offset does.
14481447
if (align % stride != 0 && *result == usize::MAX) {
14491448
return true;
14501449
}

0 commit comments

Comments
 (0)