diff --git a/library/core/src/num/imp/bignum.rs b/library/core/src/num/imp/bignum.rs index 5c2a19f39dfce..6a8ab2b1cf227 100644 --- a/library/core/src/num/imp/bignum.rs +++ b/library/core/src/num/imp/bignum.rs @@ -336,6 +336,16 @@ macro_rules! define_bignum { } } + /// Any bit pattern, including ones no operation produces (e.g. `size > n`), so that + /// proofs using it do not depend on the invariants of the representation. The operations + /// are safe code and have to cope with (i.e., at worst panic on) every value. + #[cfg(kani)] + impl crate::kani::Arbitrary for $name { + fn any() -> Self { + $name { size: crate::kani::any(), base: crate::kani::any() } + } + } + impl crate::cmp::PartialEq for $name { fn eq(&self, other: &$name) -> bool { self.base[..] == other.base[..] diff --git a/library/core/src/num/imp/flt2dec/mod.rs b/library/core/src/num/imp/flt2dec/mod.rs index cf895b3e89e66..b6a8fe340d115 100644 --- a/library/core/src/num/imp/flt2dec/mod.rs +++ b/library/core/src/num/imp/flt2dec/mod.rs @@ -135,6 +135,82 @@ pub mod strategy { pub mod grisu; } +/// Ghost state shared by the proof harnesses, loop invariants and safety checks of the +/// digit-generation strategies. +/// +/// What those proofs must establish is that `buf[..n]` has been written by the time it is passed to +/// `assume_init_*`. Neither a loop invariant nor Kani's own initialization checks (which do not +/// support slices of `MaybeUninit`) can say "this memory is initialized" directly, so the proofs +/// track a property of the bytes instead: every byte the strategies store is at least `b'0'`, +/// while a byte that was never stored keeps the value the harness gave it, and the harnesses +/// start from buffers with arbitrary contents (`any_buf`). The index `w` is picked +/// nondeterministically by the harness before the call, which keeps the invariants free of +/// quantifiers. What Kani proves is thus: whatever the buffer contains initially, +/// `buf[w] >= b'0'` holds for every `w < n` at every `assume_init_*` call on `buf[..n]`. +/// +/// This implies that a real run, on a buffer that is uninitialized, never reads a byte it has +/// not written. The strategies read the buffer only through those `assume_init_*` calls, so up +/// to the first such read the run does not depend on what the buffer contained initially, and +/// it is also a run of the harness for every choice of initial contents. If that read covered a +/// byte `w` that was never written, the choice with a zero at `w` would violate the property. +/// So the read covers only bytes the run has written, the run still does not depend on the +/// initial contents after it, and the same reasoning applies to the next read. +/// +/// "A run of the harness" has two qualifications. The harnesses replace some safe arithmetic +/// helpers by models (see the `verify` modules); these allow everything the helpers can do, so +/// the real run is still among the runs of the harness. And Kani checks arithmetic overflow and +/// `debug_assert!`, so the argument does not cover a run that continues past either of them, as +/// a release build would. +#[cfg(kani)] +pub(crate) mod ghost { + use crate::mem::MaybeUninit; + + /// The index whose initialization is being tracked. Set once per harness to `kani::any()`. + static mut WITNESS: usize = 0; + + /// Starts tracking an arbitrary index. + pub(crate) fn pick_witness() { + // SAFETY: the harnesses are single-threaded. + unsafe { WITNESS = crate::kani::any() }; + } + + /// The tracked index. + pub(crate) fn witness() -> usize { + // SAFETY: the harnesses are single-threaded. + unsafe { WITNESS } + } + + /// A digit buffer for a harness: `N` bytes with arbitrary contents. + /// + /// The bytes are deliberately not `MaybeUninit::uninit()`: the proofs then hold for every + /// initial contents by construction, and do not depend on how Kani models memory that was + /// never written. + pub(crate) fn any_buf() -> [MaybeUninit; N] { + let bytes: [u8; N] = crate::kani::any(); + // SAFETY: `[u8; N]` and `[MaybeUninit; N]` have the same layout, and every `u8` is a + // valid `MaybeUninit`. + unsafe { (&raw const bytes).cast::<[MaybeUninit; N]>().read() } + } + + /// `buf[..n]` has been written, as far as the witness index can tell. + pub(crate) fn written(buf: &[MaybeUninit], n: usize) -> bool { + let w = witness(); + // SAFETY: the harnesses make their buffers with `any_buf`, so every byte holds a value. + w >= n || w >= buf.len() || unsafe { buf[w].assume_init() } >= b'0' + } + + /// The proof obligation of an `assume_init_*` call on `buf[..n]`. + /// + /// A violation is reported through `assert_unchecked` rather than `assert!`, so that it is + /// undefined behavior and not a panic: the strategy harnesses are `#[kani::should_panic]`, + /// which accepts panics but nothing else. + pub(crate) fn assert_written(buf: &[MaybeUninit], n: usize) { + // SAFETY: none is claimed. This is the property the harnesses ask Kani to prove, and + // Kani reports a violation as a failed check. + unsafe { crate::hint::assert_unchecked(written(buf, n)) }; + } +} + /// The minimum size of buffer necessary for the shortest mode. /// /// It is a bit non-trivial to derive, but this is one plus the maximal number of @@ -683,3 +759,358 @@ where } } } + +#[cfg(kani)] +#[unstable(feature = "kani", issue = "none")] +pub(crate) mod verify { + use super::*; + use crate::kani; + + /// Any `Decoded` at all, including ones that `decode` never produces. + pub(crate) fn any_decoded() -> Decoded { + Decoded { + mant: kani::any(), + minus: kani::any(), + plus: kani::any(), + exp: kani::any(), + inclusive: kani::any(), + } + } + + /// Stands in for `round_up` in the harnesses of the digit generation strategies. `round_up` + /// is safe code working on bytes that are already initialized, but the iterator and `fill` + /// machinery it pulls into the callers' loops makes instrumenting their loop contracts several + /// times as expensive. This model lets every byte keep its value, be incremented (panicking on + /// overflow, as the real addition does), or become `b'0'` or `b'1'`, independently of the + /// others, and returns `None`, `Some(b'0')` or `Some(b'1')`. `check_round_up_model` proves + /// that this covers everything `round_up` does. + /// + /// The loop has a contract so that its cost does not grow with the length of the buffer. The + /// invariant keeps what the proofs need, the possible values of the byte at the witness index + /// (see `ghost`); the other bytes become arbitrary, which only adds behaviors. + pub(crate) fn any_round_up(d: &mut [u8]) -> Option { + let w = ghost::witness(); + let old = if w < d.len() { d[w] } else { 0 }; + let mut i = 0; + #[kani::loop_invariant(i <= d.len() + && (w >= d.len() + || if i <= w { + d[w] == old + } else { + d[w] == old + || (old < u8::MAX && d[w] == old + 1) + || d[w] == b'0' + || d[w] == b'1' + }))] + #[kani::loop_modifies(&i, &mut d[..])] + while i < d.len() { + match kani::any::() { + 0 => {} + 1 => d[i] += 1, + 2 => d[i] = b'0', + _ => d[i] = b'1', + } + i += 1; + } + match kani::any::() { + 0 => None, + 1 => Some(b'0'), + _ => Some(b'1'), + } + } + + /// `any_round_up` over-approximates `round_up`: whatever the digits (or non-digits) are, + /// each byte of the buffer ends up with one of the values the model allows for it, and so does + /// the returned digit. The byte `w` is arbitrary, so this holds for all of them, and since the + /// model chooses independently for each byte, it allows every combination. The real function + /// panics when it increments a `0xff` byte, hence `should_panic`; the two properties are + /// stated with `assert_unchecked` so that their violation would not count as a panic. + /// + /// This is checked for slices of up to 32 bytes, which covers the Dragon harnesses. + /// `grisu::verify::check_format_exact_opt` passes the model longer slices; checking those + /// here is too expensive, and for them the model rests on reading `round_up`: it looks for + /// the last byte that is not `b'9'`, increments it and overwrites what follows with `b'0'`, + /// or writes `b'1'` and `b'0'`s if there is none, whatever the length. + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(34)] + fn check_round_up_model() { + const CAP: usize = 32; + let mut d: [u8; CAP] = kani::any(); + let len: usize = kani::any(); + kani::assume(len <= CAP); + let w: usize = kani::any(); + kani::assume(w < CAP); + let old = d[w]; + let ret = round_up(&mut d[..len]); + let new = d[w]; + // SAFETY: none is claimed. These are the properties this harness asks Kani to prove. + unsafe { + crate::hint::assert_unchecked( + new == old || (old < u8::MAX && new == old + 1) || new == b'0' || new == b'1', + ); + crate::hint::assert_unchecked(matches!(ret, None | Some(b'0') | Some(b'1'))); + } + } + + /// Capacity of the digit buffers used by the harnesses. The lengths actually passed to the + /// functions under verification are symbolic, anywhere up to this capacity. It exceeds the + /// largest scratch buffer `to_exact_fixed_str` can ask for with an `f64` (828 bytes). + const BUF_CAP: usize = 1024; + /// Capacity of the `parts` arrays; the functions need at most 6. + const PARTS_CAP: usize = 8; + + /// What every element of a `parts` array points to before the call. The functions under + /// verification cannot produce a part that points here, so a returned part that does was + /// never written. + static POISON: [u8; 1] = [0]; + + /// A `parts` array for a harness. Its elements hold a value, so that the check in + /// `inspect_parts` does not depend on how Kani models memory that was never written. + fn poisoned_parts<'a>() -> [MaybeUninit>; PARTS_CAP] { + [MaybeUninit::new(Part::Copy(&POISON)); PARTS_CAP] + } + + /// Checks that every returned part has been written (see `POISON`), and reads the two ends of + /// the slices they refer to. + /// + /// The loop runs a fixed number of times instead of `parts.len()` times, so that no unwinding + /// assertion depends on the symbolic length. + fn inspect_parts(parts: &[Part<'_>]) { + assert!(parts.len() <= 6); + let mut i = 0; + while i < 6 { + if i < parts.len() { + if let Part::Copy(bytes) = parts[i] { + assert!(!crate::ptr::eq(bytes.as_ptr(), POISON.as_ptr())); + if let (Some(&first), Some(&last)) = (bytes.first(), bytes.last()) { + let _ = (first, last); + } + } + } + i += 1; + } + } + + /// Any digit string a caller may pass to `digits_to_dec_str` and `digits_to_exp_str` without + /// tripping their leading `assert!`s: non-empty, with a non-zero first digit. The remaining + /// bytes are unconstrained. + fn any_digits(storage: &[u8; BUF_CAP]) -> &[u8] { + let len: usize = kani::any(); + kani::assume(len >= 1 && len <= BUF_CAP); + kani::assume(storage[0] > b'0'); + &storage[..len] + } + + #[kani::proof] + #[kani::unwind(8)] + fn check_digits_to_dec_str() { + let storage: [u8; BUF_CAP] = kani::any(); + let buf = any_digits(&storage); + let mut parts = poisoned_parts(); + let nparts: usize = kani::any(); + kani::assume(nparts >= 4 && nparts <= PARTS_CAP); + + let out = digits_to_dec_str(buf, kani::any(), kani::any(), &mut parts[..nparts]); + assert!(out.len() >= 2 && out.len() <= 4); + inspect_parts(out); + } + + #[kani::proof] + #[kani::unwind(8)] + fn check_digits_to_exp_str() { + let storage: [u8; BUF_CAP] = kani::any(); + let buf = any_digits(&storage); + let mut parts = poisoned_parts(); + let nparts: usize = kani::any(); + kani::assume(nparts >= 6 && nparts <= PARTS_CAP); + + let out = + digits_to_exp_str(buf, kani::any(), kani::any(), kani::any(), &mut parts[..nparts]); + assert!(out.len() >= 3 && out.len() <= 6); + inspect_parts(out); + } + + fn any_sign() -> Sign { + if kani::any() { Sign::Minus } else { Sign::MinusPlus } + } + + /// Models an arbitrary digit-generation callback that honors what the `to_*_str` functions + /// document about it: it returns the part of `buf` it initialized, and that part is a digit + /// string acceptable to `digits_to_dec_str`/`digits_to_exp_str` (see `any_digits`). How many + /// digits are written (anywhere from 1 to `buf.len()`), which bytes they are, and the returned + /// exponent are all nondeterministic. + fn any_nonempty_digits<'a>(buf: &'a mut [MaybeUninit]) -> &'a [u8] { + let len: usize = kani::any(); + kani::assume(len >= 1 && len <= buf.len()); + let digits: [u8; BUF_CAP] = kani::any(); + kani::assume(digits[0] > b'0'); + // SAFETY: the buffers of the harnesses hold at most `BUF_CAP` bytes, so `len` bytes can be + // read from `digits` and written to `buf`, and the two do not overlap. + unsafe { + crate::ptr::copy_nonoverlapping(digits.as_ptr(), buf.as_mut_ptr().cast::(), len) + }; + // SAFETY: `buf[..len]` was initialized just above. + unsafe { buf[..len].assume_init_ref() } + } + + fn any_shortest<'a>(_: &Decoded, buf: &'a mut [MaybeUninit]) -> (&'a [u8], i16) { + (any_nonempty_digits(buf), kani::any()) + } + + /// Like `any_shortest`, for the exact mode as `to_exact_exp_str` uses it: it asks for digits + /// without a limit on the last one (`limit == i16::MIN`), so there are always some. + fn any_exact_unlimited<'a>( + _: &Decoded, + buf: &'a mut [MaybeUninit], + _: i16, + ) -> (&'a [u8], i16) { + (any_nonempty_digits(buf), kani::any()) + } + + /// Like `any_shortest`, for the exact mode as `to_exact_fixed_str` uses it: when the + /// requested last-digit `limit` cannot be met, the callback reports `exp <= limit` together + /// with no digits at all. + fn any_exact<'a>(_: &Decoded, buf: &'a mut [MaybeUninit], limit: i16) -> (&'a [u8], i16) { + let exp: i16 = kani::any(); + if exp <= limit { + // SAFETY: an empty prefix has nothing to initialize. + (unsafe { buf[..0].assume_init_ref() }, exp) + } else { + (any_nonempty_digits(buf), exp) + } + } + + macro_rules! check_to_str { + ($float:ty, $shortest:ident, $shortest_exp:ident, $exact_exp:ident, $exact_fixed:ident) => { + #[kani::proof] + #[kani::unwind(8)] + fn $shortest() { + let mut buf = [MaybeUninit::::uninit(); BUF_CAP]; + let mut parts = poisoned_parts(); + let (buf_len, nparts): (usize, usize) = kani::any(); + kani::assume(buf_len >= MAX_SIG_DIGITS && buf_len <= BUF_CAP); + kani::assume(nparts >= 4 && nparts <= PARTS_CAP); + + let out = to_shortest_str( + any_shortest, + kani::any::<$float>(), + any_sign(), + kani::any(), + &mut buf[..buf_len], + &mut parts[..nparts], + ); + assert!(out.parts.len() >= 1 && out.parts.len() <= 4); + inspect_parts(out.parts); + } + + #[kani::proof] + #[kani::unwind(8)] + fn $shortest_exp() { + let mut buf = [MaybeUninit::::uninit(); BUF_CAP]; + let mut parts = poisoned_parts(); + let (buf_len, nparts): (usize, usize) = kani::any(); + kani::assume(buf_len >= MAX_SIG_DIGITS && buf_len <= BUF_CAP); + kani::assume(nparts >= 6 && nparts <= PARTS_CAP); + let dec_bounds: (i16, i16) = kani::any(); + kani::assume(dec_bounds.0 <= dec_bounds.1); + + let out = to_shortest_exp_str( + any_shortest, + kani::any::<$float>(), + any_sign(), + dec_bounds, + kani::any(), + &mut buf[..buf_len], + &mut parts[..nparts], + ); + assert!(out.parts.len() >= 1 && out.parts.len() <= 6); + inspect_parts(out.parts); + } + + #[kani::proof] + #[kani::unwind(8)] + fn $exact_exp() { + let mut buf = [MaybeUninit::::uninit(); BUF_CAP]; + let mut parts = poisoned_parts(); + let (buf_len, nparts): (usize, usize) = kani::any(); + kani::assume(buf_len <= BUF_CAP); + kani::assume(nparts >= 6 && nparts <= PARTS_CAP); + let v: $float = kani::any(); + let frac_digits: usize = kani::any(); + // The function asserts that the scratch buffer is large enough for the digits it + // is going to request. + if let (_, FullDecoded::Finite(decoded)) = decode(v) { + let maxlen = estimate_max_buf_len(decoded.exp); + kani::assume( + buf_len >= if frac_digits < maxlen { frac_digits + 1 } else { maxlen }, + ); + } + + let out = to_exact_exp_str( + any_exact_unlimited, + v, + any_sign(), + frac_digits, + kani::any(), + &mut buf[..buf_len], + &mut parts[..nparts], + ); + assert!(out.parts.len() >= 1 && out.parts.len() <= 6); + inspect_parts(out.parts); + } + + #[kani::proof] + #[kani::unwind(8)] + fn $exact_fixed() { + let mut buf = [MaybeUninit::::uninit(); BUF_CAP]; + let mut parts = poisoned_parts(); + let (buf_len, nparts): (usize, usize) = kani::any(); + kani::assume(buf_len <= BUF_CAP); + kani::assume(nparts >= 4 && nparts <= PARTS_CAP); + let v: $float = kani::any(); + // The function asserts that the scratch buffer is large enough for the digits it + // is going to request. + if let (_, FullDecoded::Finite(decoded)) = decode(v) { + kani::assume(buf_len >= estimate_max_buf_len(decoded.exp)); + } + + let out = to_exact_fixed_str( + any_exact, + v, + any_sign(), + kani::any(), + &mut buf[..buf_len], + &mut parts[..nparts], + ); + assert!(out.parts.len() >= 1 && out.parts.len() <= 4); + inspect_parts(out.parts); + } + }; + } + + // `f16` only implements `DecodableFloat` on targets with reliable `f16` support, and `f128` + // does not implement it at all. + #[cfg(target_has_reliable_f16)] + check_to_str!( + f16, + check_to_shortest_str_f16, + check_to_shortest_exp_str_f16, + check_to_exact_exp_str_f16, + check_to_exact_fixed_str_f16 + ); + check_to_str!( + f32, + check_to_shortest_str_f32, + check_to_shortest_exp_str_f32, + check_to_exact_exp_str_f32, + check_to_exact_fixed_str_f32 + ); + check_to_str!( + f64, + check_to_shortest_str_f64, + check_to_shortest_exp_str_f64, + check_to_exact_exp_str_f64, + check_to_exact_fixed_str_f64 + ); +} diff --git a/library/core/src/num/imp/flt2dec/strategy/dragon.rs b/library/core/src/num/imp/flt2dec/strategy/dragon.rs index b63b93034d57f..716a8e5333223 100644 --- a/library/core/src/num/imp/flt2dec/strategy/dragon.rs +++ b/library/core/src/num/imp/flt2dec/strategy/dragon.rs @@ -280,6 +280,8 @@ pub fn format_shortest<'a>( // if rounding up changes the length, the exponent should also change. // it seems that this condition is very hard to satisfy (possibly impossible), // but we are just being safe and consistent here. + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, i); // SAFETY: we initialized that memory above. if let Some(c) = round_up(unsafe { buf[..i].assume_init_mut() }) { buf[i] = MaybeUninit::new(c); @@ -288,6 +290,8 @@ pub fn format_shortest<'a>( } } + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, i); // SAFETY: we initialized that memory above. (unsafe { buf[..i].assume_init_ref() }, k) } @@ -366,6 +370,8 @@ pub fn format_exact<'a>( for c in &mut buf[i..len] { *c = MaybeUninit::new(b'0'); } + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); // SAFETY: we initialized that memory above. return (unsafe { buf[..len].assume_init_ref() }, k); } @@ -398,6 +404,8 @@ pub fn format_exact<'a>( // if the following digits are exactly 5000..., check the prior digit and try to // round to even (i.e., avoid rounding up when the prior digit is even). let order = mant.cmp(scale.mul_small(5)); + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); if order == Ordering::Greater || (order == Ordering::Equal // SAFETY: `buf[len-1]` is initialized. @@ -405,6 +413,8 @@ pub fn format_exact<'a>( { // if rounding up changes the length, the exponent should also change. // but we've been requested a fixed number of digits, so do not alter the buffer... + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); // SAFETY: we initialized that memory above. if let Some(c) = round_up(unsafe { buf[..len].assume_init_mut() }) { // ...unless we've been requested the fixed precision instead. @@ -418,6 +428,128 @@ pub fn format_exact<'a>( } } + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); // SAFETY: we initialized that memory above. (unsafe { buf[..len].assume_init_ref() }, k) } + +#[cfg(kani)] +#[unstable(feature = "kani", issue = "none")] +pub(crate) mod verify { + use flt2dec::ghost; + + use super::*; + use crate::kani; + // `kani::stub` does not look through the `Big` type alias. Kani only targets 64-bit + // platforms, where `Big` is `Big64x20`. + use crate::num::imp::bignum::Big64x20; + + /// Capacity of the digit buffers; the length actually passed is symbolic, up to this. The + /// digit loop of `format_shortest` is unwound, because it writes to a temporary + /// (`mant.clone().add(&plus)`) that a `loop_modifies` clause cannot name. The callers of the + /// shortest mode pass `MAX_SIG_DIGITS` (17) bytes. + const BUF_CAP: usize = 32; + /// `format_exact` gets a smaller one: its digit loop contains a second loop over the rest of + /// the buffer, and Kani's loop contracts cannot describe either of them (the outer one returns + /// from inside using the loop variable, the inner one iterates over `&mut` elements), so both + /// are unwound and the cost is quadratic in this bound. + /// + /// TODO: raise this once Kani has https://github.com/model-checking/kani/pull/5071, which + /// lets the outer loop carry a contract and leaves only the inner one unwound. Raising it to + /// the 1024 used for the formatting functions also needs contracts for loops over `&mut` + /// elements. + pub(crate) const EXACT_BUF_CAP: usize = 17; + + // Models of the bignum operations the two functions use. `Big` is implemented entirely in + // safe code, and an operation can only touch the bignum it is called on, so "the receiver ends + // up holding an arbitrary bignum" covers everything the real operation can do short of + // panicking. Nothing about the values is needed for memory safety, and with the real + // multi-limb arithmetic in place the verification conditions do not fit in memory. + + pub(crate) fn any_mul_small(x: &mut Big, _: Digit) -> &mut Big { + *x = kani::any(); + x + } + + pub(crate) fn any_mul_pow2(x: &mut Big, _: usize) -> &mut Big { + *x = kani::any(); + x + } + + pub(crate) fn any_mul_digits<'a>(x: &'a mut Big, _: &[Digit]) -> &'a mut Big { + *x = kani::any(); + x + } + + pub(crate) fn any_add<'a>(x: &'a mut Big, _: &Big) -> &'a mut Big { + *x = kani::any(); + x + } + + pub(crate) fn any_sub<'a>(x: &'a mut Big, _: &Big) -> &'a mut Big { + *x = kani::any(); + x + } + + pub(crate) fn any_div_rem_small(x: &mut Big, _: Digit) -> (&mut Big, Digit) { + *x = kani::any(); + (x, kani::any()) + } + + pub(crate) fn any_cmp(_: &Big, _: &Big) -> Ordering { + if kani::any() { + Ordering::Less + } else if kani::any() { + Ordering::Equal + } else { + Ordering::Greater + } + } + + pub(crate) fn any_is_zero(_: &Big) -> bool { + kani::any() + } + + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(34)] + #[kani::stub(Big64x20::mul_small, any_mul_small)] + #[kani::stub(Big64x20::mul_pow2, any_mul_pow2)] + #[kani::stub(Big64x20::mul_digits, any_mul_digits)] + #[kani::stub(Big64x20::add, any_add)] + #[kani::stub(Big64x20::sub, any_sub)] + #[kani::stub(Big64x20::div_rem_small, any_div_rem_small)] + #[kani::stub(::cmp, any_cmp)] + #[kani::stub(Big64x20::is_zero, any_is_zero)] + #[kani::stub(flt2dec::round_up, flt2dec::verify::any_round_up)] + fn check_format_shortest() { + let d = flt2dec::verify::any_decoded(); + let mut buf = ghost::any_buf::<{ BUF_CAP }>(); + let len: usize = kani::any(); + kani::assume(len <= BUF_CAP); + ghost::pick_witness(); + let _ = format_shortest(&d, &mut buf[..len]); + } + + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(19)] + #[kani::stub(Big64x20::mul_small, any_mul_small)] + #[kani::stub(Big64x20::mul_pow2, any_mul_pow2)] + #[kani::stub(Big64x20::mul_digits, any_mul_digits)] + #[kani::stub(Big64x20::add, any_add)] + #[kani::stub(Big64x20::sub, any_sub)] + #[kani::stub(Big64x20::div_rem_small, any_div_rem_small)] + #[kani::stub(::cmp, any_cmp)] + #[kani::stub(Big64x20::is_zero, any_is_zero)] + #[kani::stub(flt2dec::round_up, flt2dec::verify::any_round_up)] + fn check_format_exact() { + let d = flt2dec::verify::any_decoded(); + let mut buf = ghost::any_buf::<{ EXACT_BUF_CAP }>(); + let len: usize = kani::any(); + kani::assume(len <= EXACT_BUF_CAP); + ghost::pick_witness(); + let _ = format_exact(&d, &mut buf[..len], kani::any()); + } +} diff --git a/library/core/src/num/imp/flt2dec/strategy/grisu.rs b/library/core/src/num/imp/flt2dec/strategy/grisu.rs index f7ee465829340..5ef5e4602ed2b 100644 --- a/library/core/src/num/imp/flt2dec/strategy/grisu.rs +++ b/library/core/src/num/imp/flt2dec/strategy/grisu.rs @@ -257,6 +257,17 @@ pub fn format_shortest_opt<'a>( // render integral parts, while checking for the accuracy at each step. let mut ten_kappa = max_ten_kappa; // 10^kappa let mut remainder = plus1int; // digits yet to be rendered + // TODO: make `plus1rem` a `let` in the loop again once Kani has + // https://github.com/model-checking/kani/pull/5071. Until then a variable that an exit path + // of the loop reads must be named in `loop_modifies`, so it is declared before the loop; the + // assignment under `cfg(kani)` exists only so that the clause may borrow it. + let mut plus1rem; + #[cfg(kani)] + { + plus1rem = 0; + } + #[safety::loop_invariant(i <= buf.len() && flt2dec::ghost::written(buf, i))] + #[cfg_attr(kani, kani::loop_modifies(&i, &ten_kappa, &remainder, &plus1rem, &mut buf[..]))] loop { // we always have at least one digit to render, as `plus1 >= 10^kappa` // invariants: @@ -271,10 +282,12 @@ pub fn format_shortest_opt<'a>( buf[i] = MaybeUninit::new(b'0' + q as u8); i += 1; - let plus1rem = ((r as u64) << e) + plus1frac; // == (plus1 % 10^kappa) * 2^e + plus1rem = ((r as u64) << e) + plus1frac; // == (plus1 % 10^kappa) * 2^e if plus1rem < delta1 { // `plus1 % 10^kappa < delta1 = plus1 - minus1`; we've found the correct `kappa`. let ten_kappa = (ten_kappa as u64) << e; // scale 10^kappa back to the shared exponent + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, i); return round_and_weed( // SAFETY: we initialized that memory above. unsafe { buf[..i].assume_init_mut() }, @@ -304,6 +317,17 @@ pub fn format_shortest_opt<'a>( let mut remainder = plus1frac; let mut threshold = delta1frac; let mut ulp = 1; + // TODO: make `r` a `let` in the loop again once Kani has + // https://github.com/model-checking/kani/pull/5071. Until then a variable that an exit path + // of the loop reads must be named in `loop_modifies`, so it is declared before the loop; the + // assignment under `cfg(kani)` exists only so that the clause may borrow it. + let mut r; + #[cfg(kani)] + { + r = 0; + } + #[safety::loop_invariant(i <= buf.len() && flt2dec::ghost::written(buf, i))] + #[cfg_attr(kani, kani::loop_modifies(&i, &remainder, &threshold, &ulp, &r, &mut buf[..]))] loop { // the next digit should be significant as we've tested that before breaking out // invariants, where `m = max_kappa + 1` (# of digits in the integral part): @@ -317,13 +341,15 @@ pub fn format_shortest_opt<'a>( // divide `remainder` by `10^kappa`. // both are scaled by `2^e / 10^kappa`, so the latter is implicit here. let q = remainder >> e; - let r = remainder & ((1 << e) - 1); + r = remainder & ((1 << e) - 1); debug_assert!(q < 10); buf[i] = MaybeUninit::new(b'0' + q as u8); i += 1; if r < threshold { let ten_kappa = 1 << e; // implicit divisor + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, i); return round_and_weed( // SAFETY: we initialized that memory above. unsafe { buf[..i].assume_init_mut() }, @@ -419,6 +445,8 @@ pub fn format_shortest_opt<'a>( // // consequently, we should stop when `TC1 || TC2 || (TC3a && TC3b)`. the following is // equal to its inverse, `!TC1 && !TC2 && (!TC3a || !TC3b)`. + #[safety::loop_invariant(plus1w >= remainder)] + #[cfg_attr(kani, kani::loop_modifies(&plus1w, &raw const *last))] while plus1w < plus1v_up && threshold - plus1w >= ten_kappa && (plus1w + ten_kappa < plus1v_up @@ -554,6 +582,17 @@ pub fn format_exact_opt<'a>( let mut kappa = max_kappa as i16; let mut ten_kappa = max_ten_kappa; // 10^kappa let mut remainder = vint; // digits yet to be rendered + // TODO: make `r` a `let` in the loop again once Kani has + // https://github.com/model-checking/kani/pull/5071. Until then a variable that an exit path + // of the loop reads must be named in `loop_modifies`, so it is declared before the loop; the + // assignment under `cfg(kani)` exists only so that the clause may borrow it. + let mut r; + #[cfg(kani)] + { + r = 0; + } + #[safety::loop_invariant(i < len && len <= buf.len() && flt2dec::ghost::written(buf, i))] + #[cfg_attr(kani, kani::loop_modifies(&i, &kappa, &ten_kappa, &remainder, &r, &mut buf[..]))] loop { // we always have at least one digit to render // invariants: @@ -563,7 +602,7 @@ pub fn format_exact_opt<'a>( // divide `remainder` by `10^kappa`. both are scaled by `2^-e`. let q = remainder / ten_kappa; - let r = remainder % ten_kappa; + r = remainder % ten_kappa; debug_assert!(q < 10); buf[i] = MaybeUninit::new(b'0' + q as u8); i += 1; @@ -605,6 +644,17 @@ pub fn format_exact_opt<'a>( // the first two comparisons from `possibly_round`, for the reference. let mut remainder = vfrac; let maxerr = 1 << (e - 1); + // TODO: make `r` a `let` in the loop again once Kani has + // https://github.com/model-checking/kani/pull/5071. Until then a variable that an exit path + // of the loop reads must be named in `loop_modifies`, so it is declared before the loop; the + // assignment under `cfg(kani)` exists only so that the clause may borrow it. + let mut r; + #[cfg(kani)] + { + r = 0; + } + #[safety::loop_invariant(i < len && len <= buf.len() && flt2dec::ghost::written(buf, i))] + #[cfg_attr(kani, kani::loop_modifies(&i, &err, &remainder, &r, &mut buf[..]))] while err < maxerr { // invariants, where `m = max_kappa + 1` (# of digits in the integral part): // - `remainder < 2^e` @@ -617,7 +667,7 @@ pub fn format_exact_opt<'a>( // divide `remainder` by `10^kappa`. // both are scaled by `2^e / 10^kappa`, so the latter is implicit here. let q = remainder >> e; - let r = remainder & ((1 << e) - 1); + r = remainder & ((1 << e) - 1); debug_assert!(q < 10); buf[i] = MaybeUninit::new(b'0' + q as u8); i += 1; @@ -714,6 +764,8 @@ pub fn format_exact_opt<'a>( // we've already verified that `ulp < 10^kappa / 2`, so as long as // `10^kappa` did not overflow after all, the second check is fine. if ten_kappa - remainder > remainder && ten_kappa - 2 * remainder >= 2 * ulp { + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); // SAFETY: our caller initialized that memory. return Some((unsafe { buf[..len].assume_init_ref() }, exp)); } @@ -736,6 +788,8 @@ pub fn format_exact_opt<'a>( // as `10^kappa` is never zero). also note that `remainder - ulp <= 10^kappa`, // so the second check does not overflow. if remainder > ulp && ten_kappa - (remainder - ulp) <= remainder - ulp { + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); if let Some(c) = // SAFETY: our caller must have initialized that memory. round_up(unsafe { buf[..len].assume_init_mut() }) @@ -749,6 +803,8 @@ pub fn format_exact_opt<'a>( len += 1; } } + #[cfg(kani)] + flt2dec::ghost::assert_written(buf, len); // SAFETY: we and our caller initialized that memory. return Some((unsafe { buf[..len].assume_init_ref() }, exp)); } @@ -776,3 +832,105 @@ pub fn format_exact<'a>( None => fallback(d, buf, limit), } } + +#[cfg(kani)] +#[unstable(feature = "kani", issue = "none")] +mod verify { + use flt2dec::ghost; + use flt2dec::strategy::dragon::verify as dragon; + + use super::*; + use crate::kani; + use crate::num::imp::bignum::Big64x20; + + /// Capacity of the digit buffers of the two wrappers; the length actually passed is symbolic, + /// up to this. See `dragon::verify` for what limits it. + const BUF_CAP: usize = 32; + /// `format_shortest_opt` and `format_exact_opt` get a larger one, beyond the largest buffer + /// that the formatting functions ask them to fill (828 bytes, for an `f64` in exact mode): + /// all the loops they run are under contract, so the bound costs solver time only. Kissat + /// needs far less memory than the default solver for these two. + const OPT_BUF_CAP: usize = 1024; + + /// Stands in for `Fp::mul`, which is safe code without side effects: any `Fp` at all. Nothing + /// about the products is needed for memory safety, whereas the real 128-bit multiplications + /// make the solver prove (or refute) arithmetic facts about them that are irrelevant here. + fn any_fp_mul(_: Fp, _: Fp) -> Fp { + Fp { f: kani::any(), e: kani::any() } + } + + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(2)] + #[kani::solver(kissat)] + #[kani::stub(Fp::mul, any_fp_mul)] + fn check_format_shortest_opt() { + let d = flt2dec::verify::any_decoded(); + let mut buf = ghost::any_buf::<{ OPT_BUF_CAP }>(); + let len: usize = kani::any(); + kani::assume(len <= OPT_BUF_CAP); + ghost::pick_witness(); + let _ = format_shortest_opt(&d, &mut buf[..len]); + } + + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(2)] + #[kani::solver(kissat)] + #[kani::stub(flt2dec::round_up, flt2dec::verify::any_round_up)] + fn check_format_exact_opt() { + let d = flt2dec::verify::any_decoded(); + let mut buf = ghost::any_buf::<{ OPT_BUF_CAP }>(); + let len: usize = kani::any(); + kani::assume(len <= OPT_BUF_CAP); + ghost::pick_witness(); + let _ = format_exact_opt(&d, &mut buf[..len], kani::any()); + } + + // The two wrappers below run the Grisu attempt and then, on the same buffer, the Dragon + // fallback. Both run for real here, with the same models of the safe arithmetic helpers as + // in their own harnesses. + + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(34)] + #[kani::stub(Fp::mul, any_fp_mul)] + #[kani::stub(Big64x20::mul_small, dragon::any_mul_small)] + #[kani::stub(Big64x20::mul_pow2, dragon::any_mul_pow2)] + #[kani::stub(Big64x20::mul_digits, dragon::any_mul_digits)] + #[kani::stub(Big64x20::add, dragon::any_add)] + #[kani::stub(Big64x20::sub, dragon::any_sub)] + #[kani::stub(Big64x20::div_rem_small, dragon::any_div_rem_small)] + #[kani::stub(::cmp, dragon::any_cmp)] + #[kani::stub(Big64x20::is_zero, dragon::any_is_zero)] + #[kani::stub(flt2dec::round_up, flt2dec::verify::any_round_up)] + fn check_format_shortest() { + let d = flt2dec::verify::any_decoded(); + let mut buf = ghost::any_buf::<{ BUF_CAP }>(); + let len: usize = kani::any(); + kani::assume(len <= BUF_CAP); + ghost::pick_witness(); + let _ = format_shortest(&d, &mut buf[..len]); + } + + #[kani::proof] + #[kani::should_panic] + #[kani::unwind(19)] + #[kani::stub(Big64x20::mul_small, dragon::any_mul_small)] + #[kani::stub(Big64x20::mul_pow2, dragon::any_mul_pow2)] + #[kani::stub(Big64x20::mul_digits, dragon::any_mul_digits)] + #[kani::stub(Big64x20::add, dragon::any_add)] + #[kani::stub(Big64x20::sub, dragon::any_sub)] + #[kani::stub(Big64x20::div_rem_small, dragon::any_div_rem_small)] + #[kani::stub(::cmp, dragon::any_cmp)] + #[kani::stub(Big64x20::is_zero, dragon::any_is_zero)] + #[kani::stub(flt2dec::round_up, flt2dec::verify::any_round_up)] + fn check_format_exact() { + let d = flt2dec::verify::any_decoded(); + let mut buf = ghost::any_buf::<{ dragon::EXACT_BUF_CAP }>(); + let len: usize = kani::any(); + kani::assume(len <= dragon::EXACT_BUF_CAP); + ghost::pick_witness(); + let _ = format_exact(&d, &mut buf[..len], kani::any()); + } +}