diff --git a/.github/workflows/kani.yml b/.github/workflows/kani.yml index d93b0ecc26b16..9815a55b0e18e 100644 --- a/.github/workflows/kani.yml +++ b/.github/workflows/kani.yml @@ -73,6 +73,17 @@ jobs: # Step 3: Run Kani on the std library (default configuration) - name: Run Kani Verification run: head/scripts/run-kani.sh --path ${{github.workspace}}/head + + # Challenge 30 also requires detecting invalid values. Reuse the verifier + # built above, but run this focused suite once per OS without sharding. + - name: Verify integer logarithms with invalid-value checks + if: matrix.partition == 1 + env: + WORKER_INDEX: '' + WORKER_TOTAL: '' + run: >- + head/scripts/run-kani.sh --path ${{github.workspace}}/head + --kani-args --harness num::imp::int_log -Z valid-value-checks kani_autoharness: name: Verify std library using autoharness diff --git a/doc/src/challenges/0030-integer-logarithms.md b/doc/src/challenges/0030-integer-logarithms.md index 2671a69855a22..299773d9e22b4 100644 --- a/doc/src/challenges/0030-integer-logarithms.md +++ b/doc/src/challenges/0030-integer-logarithms.md @@ -66,3 +66,69 @@ All proofs must automatically ensure the absence of the following Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md) in addition to the ones listed above. + +## Kani proof implementation + +The proposed Kani implementation lives in `library/core/src/num/imp/int_log10.rs` +and the verification-only module `library/core/src/num/imp/int_log2.rs`. +It does not change the runtime algorithms. The challenge remains open until the +solution is reviewed and accepted. + +| Proof group | Harnesses | Coverage | +|------------|-----------|----------| +| Decimal helpers | 6 | Each helper's entire documented positive input domain | +| Decimal wrappers | 11 | Five unsigned, five signed, and `usize` wrappers | +| Supporting contracts | 1 | `hint::assert_unchecked` safety contract | +| Public decimal APIs | 18 | Checked and positive panicking APIs for 12 integer types; six unsigned `NonZero` types | +| Decimal invalid inputs | 13 | Twelve invalid-input routing proofs and the shared panic helper | +| Public binary APIs | 18 | Checked and positive panicking APIs for 12 integer types; six unsigned `NonZero` types | +| Binary invalid inputs | 12 | Invalid-input routing for all 12 integer types | + +### Specifications and proof dependencies + +The decimal specification compares the input with explicit powers of ten through +`10^38`; the final interval ends at `u128::MAX`. It neither calls a logarithm API +nor computes the unrepresentable value `10^39`. Helpers and wrappers have exact +result contracts. Caller harnesses use `kani::stub_verified` to check callee +preconditions and reuse independently verified postconditions. This includes +checking the condition passed to each wrapper's `assert_unchecked` call. + +The binary specification requires `r < 128` and `n >> r == 1` for positive input +`n` widened to `u128`. These conditions mean exactly `2^r <= n < 2^(r+1)`, +including the interval with `r == 127`. Binary API bodies are verified directly, +including the nonzero preconditions of `ctlz_nonzero` and arithmetic safety. + +Checked APIs are exercised over their entire primitive input domain. Separate +proofs check the panicking APIs on positive inputs and on nonpositive inputs. +For the latter, only the shared, no-argument panic helper is replaced by a +terminal path (`kani::assume(false)`). An assertion after the API call proves +that no invalid input returns normally; other panics, undefined behavior, or +unbounded loops remain verification failures. A separate `kani::should_panic` +harness checks the actual straight-line panic helper. This avoids using an +existential symbolic `should_panic` check as evidence for all invalid inputs. + +### Pointer widths + +The `usize` conversion selects `u16`, `u32`, or `u64` with exactly the same width +as the input, so casting preserves its positive value. Consequently, +`NonZero::new_unchecked` receives a nonzero value on each supported pointer width. +The selected unsigned wrappers and the `i16`/`i32` wrappers used by narrower +`isize` configurations have independent proofs. Pointer-sized APIs and the +actual `usize` conversion are run on the 64-bit CI configuration, as permitted +by this challenge; this is not a claim of local execution on 16- or 32-bit targets. + +### Reproducing the proofs + +From the repository root, run: + +```sh +./scripts/run-kani.sh --kani-args --harness num::imp::int_log +./scripts/run-kani.sh --kani-args --harness num::imp::int_log -Z valid-value-checks +``` + +The filter selects all 79 challenge harnesses, including the supporting contract +and actual panic-helper proof. The existing Kani workflow discovers them without +a separate harness allowlist and verifies them across its partitions on Linux +and macOS. An additional step in partition 1 runs the complete logarithm suite +with `-Z valid-value-checks` on each OS, without partitioning, to explicitly check +for invalid values. Normal Rust regression checks remain covered by the Rust Tests workflow. diff --git a/library/core/src/num/imp/int_log10.rs b/library/core/src/num/imp/int_log10.rs index 1da029c6a21c4..db4570cdd0382 100644 --- a/library/core/src/num/imp/int_log10.rs +++ b/library/core/src/num/imp/int_log10.rs @@ -1,9 +1,15 @@ //! These functions compute the integer logarithm of their type, assuming //! that someone has already checked that the value is strictly positive. +use safety::{ensures, requires}; + +#[cfg(kani)] +use crate::kani; use crate::num::NonZero; // 0 < val <= u8::MAX +#[requires(val > 0)] +#[ensures(|result: &u32| *result == verify::decimal_log(val as u128))] #[inline] const fn u8_impl(val: u8) -> u32 { let val = val as u32; @@ -25,6 +31,8 @@ const fn u8_impl(val: u8) -> u32 { } // 0 < val < 100_000 +#[requires(val > 0 && val < 100_000)] +#[ensures(|result: &u32| *result == verify::decimal_log(val as u128))] #[inline] const fn less_than_5(val: u32) -> u32 { // Similar to u8, when adding one of these constants to val, @@ -46,12 +54,16 @@ const fn less_than_5(val: u32) -> u32 { } // 0 < val <= u16::MAX +#[requires(val > 0)] +#[ensures(|result: &u32| *result == verify::decimal_log(val as u128))] #[inline] const fn u16_impl(val: u16) -> u32 { less_than_5(val as u32) } // 0 < val <= u32::MAX +#[requires(val > 0)] +#[ensures(|result: &u32| *result == verify::decimal_log(val as u128))] #[inline] const fn u32_impl(mut val: u32) -> u32 { let mut log = 0; @@ -63,6 +75,8 @@ const fn u32_impl(mut val: u32) -> u32 { } // 0 < val <= u64::MAX +#[requires(val > 0)] +#[ensures(|result: &u32| *result == verify::decimal_log(val as u128))] #[inline] const fn u64_impl(mut val: u64) -> u32 { let mut log = 0; @@ -78,6 +92,8 @@ const fn u64_impl(mut val: u64) -> u32 { } // 0 < val <= u128::MAX +#[requires(val > 0)] +#[ensures(|result: &u32| *result == verify::decimal_log(val as u128))] #[inline] const fn u128_impl(mut val: u128) -> u32 { let mut log = 0; @@ -96,6 +112,7 @@ const fn u128_impl(mut val: u128) -> u32 { macro_rules! define_unsigned_ilog10 { ($($ty:ident => $impl_fn:ident,)*) => {$( #[inline] + #[ensures(|result: &u32| *result == verify::decimal_log(val.get() as u128))] pub(in crate::num) const fn $ty(val: NonZero<$ty>) -> u32 { let result = $impl_fn(val.get()); @@ -116,6 +133,7 @@ define_unsigned_ilog10! { u128 => u128_impl, } +#[ensures(|result: &u32| *result == verify::decimal_log(val.get() as u128))] #[inline] pub(in crate::num) const fn usize(val: NonZero) -> u32 { #[cfg(target_pointer_width = "16")] @@ -127,8 +145,10 @@ pub(in crate::num) const fn usize(val: NonZero) -> u32 { #[cfg(target_pointer_width = "64")] let impl_fn = u64; - // SAFETY: We have selected the correct `impl_fn`, so the converting `val` to the argument is - // safe. + // SAFETY: Each configuration selects an unsigned type with the same width as usize + // (u16, u32, or u64). The cast therefore preserves the positive value, so the + // NonZero conversion is safe on all three pointer widths. The u16/u32/u64 + // wrapper contracts are verified separately; CI verifies this conversion on 64-bit targets. impl_fn(unsafe { NonZero::new_unchecked(val.get() as _) }) } @@ -136,6 +156,10 @@ macro_rules! define_signed_ilog10 { ($($ty:ident => $impl_fn:ident,)*) => {$( // 0 < val <= $ty::MAX #[inline] + #[ensures(|result: &Option| match *result { + Some(log) => val > 0 && log == verify::decimal_log(val as u128), + None => val <= 0, + })] pub(in crate::num) const fn $ty(val: $ty) -> Option { if val > 0 { let result = $impl_fn(val.cast_unsigned()); @@ -169,3 +193,286 @@ define_signed_ilog10! { pub(in crate::num) const fn panic_for_nonpositive_argument() -> ! { panic!("argument of integer logarithm must be positive") } + +#[cfg(kani)] +mod verify { + use crate::kani; + + // Verify the safety contract used to discharge the wrappers' bound assertions. + #[kani::proof_for_contract(crate::hint::assert_unchecked)] + fn check_assert_unchecked_contract() { + unsafe { crate::hint::assert_unchecked(kani::any()) }; + } + + // Independent specification for positive inputs across all integer widths. + // Thresholds avoid computing 10^39, which would overflow u128. + pub(super) fn decimal_log(n: u128) -> u32 { + if n < 10 { + 0 + } else if n < 100 { + 1 + } else if n < 1_000 { + 2 + } else if n < 10_000 { + 3 + } else if n < 100_000 { + 4 + } else if n < 1_000_000 { + 5 + } else if n < 10_000_000 { + 6 + } else if n < 100_000_000 { + 7 + } else if n < 1_000_000_000 { + 8 + } else if n < 10_000_000_000 { + 9 + } else if n < 100_000_000_000 { + 10 + } else if n < 1_000_000_000_000 { + 11 + } else if n < 10_000_000_000_000 { + 12 + } else if n < 100_000_000_000_000 { + 13 + } else if n < 1_000_000_000_000_000 { + 14 + } else if n < 10_000_000_000_000_000 { + 15 + } else if n < 100_000_000_000_000_000 { + 16 + } else if n < 1_000_000_000_000_000_000 { + 17 + } else if n < 10_000_000_000_000_000_000 { + 18 + } else if n < 100_000_000_000_000_000_000 { + 19 + } else if n < 1_000_000_000_000_000_000_000 { + 20 + } else if n < 10_000_000_000_000_000_000_000 { + 21 + } else if n < 100_000_000_000_000_000_000_000 { + 22 + } else if n < 1_000_000_000_000_000_000_000_000 { + 23 + } else if n < 10_000_000_000_000_000_000_000_000 { + 24 + } else if n < 100_000_000_000_000_000_000_000_000 { + 25 + } else if n < 1_000_000_000_000_000_000_000_000_000 { + 26 + } else if n < 10_000_000_000_000_000_000_000_000_000 { + 27 + } else if n < 100_000_000_000_000_000_000_000_000_000 { + 28 + } else if n < 1_000_000_000_000_000_000_000_000_000_000 { + 29 + } else if n < 10_000_000_000_000_000_000_000_000_000_000 { + 30 + } else if n < 100_000_000_000_000_000_000_000_000_000_000 { + 31 + } else if n < 1_000_000_000_000_000_000_000_000_000_000_000 { + 32 + } else if n < 10_000_000_000_000_000_000_000_000_000_000_000 { + 33 + } else if n < 100_000_000_000_000_000_000_000_000_000_000_000 { + 34 + } else if n < 1_000_000_000_000_000_000_000_000_000_000_000_000 { + 35 + } else if n < 10_000_000_000_000_000_000_000_000_000_000_000_000 { + 36 + } else if n < 100_000_000_000_000_000_000_000_000_000_000_000_000 { + 37 + } else { + 38 + } + } + + #[kani::proof_for_contract(super::u8_impl)] + fn check_u8_impl() { + super::u8_impl(kani::any()); + } + + #[kani::proof_for_contract(super::less_than_5)] + fn check_less_than_5() { + super::less_than_5(kani::any()); + } + + #[kani::proof_for_contract(super::u16_impl)] + #[kani::stub_verified(super::less_than_5)] + fn check_u16_impl() { + super::u16_impl(kani::any()); + } + + #[kani::proof_for_contract(super::u32_impl)] + #[kani::stub_verified(super::less_than_5)] + fn check_u32_impl() { + super::u32_impl(kani::any()); + } + + #[kani::proof_for_contract(super::u64_impl)] + #[kani::stub_verified(super::less_than_5)] + fn check_u64_impl() { + super::u64_impl(kani::any()); + } + + #[kani::proof_for_contract(super::u128_impl)] + #[kani::stub_verified(super::u32_impl)] + #[kani::stub_verified(super::u64_impl)] + fn check_u128_impl() { + super::u128_impl(kani::any()); + } + + // Construct every valid NonZero input from an unconstrained primitive. + // The assumption expresses a type invariant, not a restricted wrapper domain. + macro_rules! check_unsigned_wrapper { + ($ty:ident, $helper:ident, $name:ident) => { + #[kani::proof_for_contract(super::$ty)] + #[kani::stub_verified(super::$helper)] + #[kani::stub_verified(crate::hint::assert_unchecked)] + fn $name() { + let n: $ty = kani::any(); + kani::assume(n > 0); + super::$ty(crate::num::NonZero::new(n).unwrap()); + } + }; + } + + macro_rules! check_signed_wrapper { + ($ty:ident, $helper:ident, $name:ident) => { + #[kani::proof_for_contract(super::$ty)] + #[kani::stub_verified(super::$helper)] + #[kani::stub_verified(crate::hint::assert_unchecked)] + fn $name() { + super::$ty(kani::any()); + } + }; + } + check_unsigned_wrapper!(u8, u8_impl, check_u8_wrapper); + check_unsigned_wrapper!(u16, u16_impl, check_u16_wrapper); + check_unsigned_wrapper!(u32, u32_impl, check_u32_wrapper); + check_unsigned_wrapper!(u64, u64_impl, check_u64_wrapper); + check_unsigned_wrapper!(u128, u128_impl, check_u128_wrapper); + check_signed_wrapper!(i8, u8_impl, check_i8_wrapper); + check_signed_wrapper!(i16, u16_impl, check_i16_wrapper); + check_signed_wrapper!(i32, u32_impl, check_i32_wrapper); + check_signed_wrapper!(i64, u64_impl, check_i64_wrapper); + check_signed_wrapper!(i128, u128_impl, check_i128_wrapper); + + #[kani::proof_for_contract(super::usize)] + #[cfg_attr(target_pointer_width = "16", kani::stub_verified(super::u16))] + #[cfg_attr(target_pointer_width = "32", kani::stub_verified(super::u32))] + #[cfg_attr(target_pointer_width = "64", kani::stub_verified(super::u64))] + fn check_usize_wrapper() { + let n: usize = kani::any(); + kani::assume(n > 0); + super::usize(crate::num::NonZero::new(n).unwrap()); + } + + // Verify public bodies while reusing the corresponding internal wrapper contract. + macro_rules! check_decimal_public { + ($ty:ident, $wrapper:ident, $name:ident) => { + #[kani::proof] + #[kani::stub_verified(super::$wrapper)] + fn $name() { + let n: $ty = kani::any(); + let expected = if n > 0 { + Some(decimal_log(n as u128)) + } else { + None + }; + assert_eq!(n.checked_ilog10(), expected); + if n > 0 { + assert_eq!(n.ilog10(), decimal_log(n as u128)); + } + } + }; + } + + macro_rules! check_decimal_nonzero { + ($ty:ident, $name:ident) => { + #[kani::proof] + #[kani::stub_verified(super::$ty)] + fn $name() { + let n: $ty = kani::any(); + kani::assume(n > 0); + let nz = crate::num::NonZero::new(n).unwrap(); + assert_eq!(nz.ilog10(), decimal_log(n as u128)); + } + }; + } + + // This no-argument, straight-line function always panics. There is no + // symbolic input for should_panic to select a favorable case from. + #[kani::proof] + #[kani::should_panic] + fn check_decimal_panic_helper() { + super::panic_for_nonpositive_argument(); + } + + // Model the proven panic helper as a terminal path. Only this helper is + // replaced: any other panic or UB remains a verification failure. + fn decimal_panic_terminal() -> ! { + kani::assume(false); + unreachable!() + } + + macro_rules! check_decimal_invalid { + ($ty:ident, $wrapper:ident, $name:ident) => { + #[kani::proof] + #[kani::stub_verified(super::$wrapper)] + #[kani::stub(super::panic_for_nonpositive_argument, decimal_panic_terminal)] + fn $name() { + let n: $ty = kani::any(); + kani::assume(n <= 0); + let _ = n.ilog10(); + assert!( + false, + "every nonpositive input must reach the decimal panic helper" + ); + } + }; + } + check_decimal_public!(u8, u8, check_public_decimal_u8); + check_decimal_invalid!(u8, u8, check_invalid_decimal_u8); + check_decimal_nonzero!(u8, check_nonzero_decimal_u8); + check_decimal_public!(u16, u16, check_public_decimal_u16); + check_decimal_invalid!(u16, u16, check_invalid_decimal_u16); + check_decimal_nonzero!(u16, check_nonzero_decimal_u16); + check_decimal_public!(u32, u32, check_public_decimal_u32); + check_decimal_invalid!(u32, u32, check_invalid_decimal_u32); + check_decimal_nonzero!(u32, check_nonzero_decimal_u32); + check_decimal_public!(u64, u64, check_public_decimal_u64); + check_decimal_invalid!(u64, u64, check_invalid_decimal_u64); + check_decimal_nonzero!(u64, check_nonzero_decimal_u64); + check_decimal_public!(u128, u128, check_public_decimal_u128); + check_decimal_invalid!(u128, u128, check_invalid_decimal_u128); + check_decimal_nonzero!(u128, check_nonzero_decimal_u128); + check_decimal_public!(usize, usize, check_public_decimal_usize); + check_decimal_invalid!(usize, usize, check_invalid_decimal_usize); + check_decimal_nonzero!(usize, check_nonzero_decimal_usize); + check_decimal_public!(i8, i8, check_public_decimal_i8); + check_decimal_invalid!(i8, i8, check_invalid_decimal_i8); + check_decimal_public!(i16, i16, check_public_decimal_i16); + check_decimal_invalid!(i16, i16, check_invalid_decimal_i16); + check_decimal_public!(i32, i32, check_public_decimal_i32); + check_decimal_invalid!(i32, i32, check_invalid_decimal_i32); + check_decimal_public!(i64, i64, check_public_decimal_i64); + check_decimal_invalid!(i64, i64, check_invalid_decimal_i64); + check_decimal_public!(i128, i128, check_public_decimal_i128); + check_decimal_invalid!(i128, i128, check_invalid_decimal_i128); + // isize delegates to the signed wrapper matching the target pointer width. + #[cfg(target_pointer_width = "16")] + check_decimal_public!(isize, i16, check_public_decimal_isize); + #[cfg(target_pointer_width = "32")] + check_decimal_public!(isize, i32, check_public_decimal_isize); + #[cfg(target_pointer_width = "64")] + check_decimal_public!(isize, i64, check_public_decimal_isize); + // isize delegates to the signed wrapper matching the target pointer width. + #[cfg(target_pointer_width = "16")] + check_decimal_invalid!(isize, i16, check_invalid_decimal_isize); + #[cfg(target_pointer_width = "32")] + check_decimal_invalid!(isize, i32, check_invalid_decimal_isize); + #[cfg(target_pointer_width = "64")] + check_decimal_invalid!(isize, i64, check_invalid_decimal_isize); +} diff --git a/library/core/src/num/imp/int_log2.rs b/library/core/src/num/imp/int_log2.rs new file mode 100644 index 0000000000000..c98fad4b89071 --- /dev/null +++ b/library/core/src/num/imp/int_log2.rs @@ -0,0 +1,99 @@ +//! Binary-logarithm proofs. Public method bodies and their intrinsics are checked directly. + +use crate::kani; + +// n >> r == 1 means exactly 2^r <= n < 2^(r+1). Unlike computing +// the upper power in u128, this remains representable when r == 127. +fn assert_binary_log(n: u128, r: u32) { + assert!(r < 128); + assert_eq!(n >> r, 1); +} + +macro_rules! check_public { + ($ty:ident, $name:ident) => { + #[kani::proof] + fn $name() { + let n: $ty = kani::any(); + match n.checked_ilog2() { + Some(r) => { + assert!(n > 0); + assert_binary_log(n as u128, r); + } + None => assert!(n <= 0), + } + if n > 0 { + // Also verify the panicking API on its positive input domain. + assert_binary_log(n as u128, n.ilog2()); + } + } + }; +} + +macro_rules! check_nonzero { + ($ty:ident, $name:ident) => { + #[kani::proof] + fn $name() { + let n: $ty = kani::any(); + kani::assume(n > 0); + let nz = crate::num::NonZero::new(n).unwrap(); + assert_binary_log(n as u128, nz.ilog2()); + } + }; +} + +// The no-argument panic helper is checked by int_log10::verify::check_decimal_panic_helper. +// This terminal model proves that every invalid input reaches that helper, +// rather than merely finding one input that panics with should_panic. +fn panic_terminal() -> ! { + kani::assume(false); + unreachable!() +} + +macro_rules! check_invalid { + ($ty:ident, $name:ident) => { + #[kani::proof] + #[kani::stub(super::int_log10::panic_for_nonpositive_argument, panic_terminal)] + fn $name() { + let n: $ty = kani::any(); + kani::assume(n <= 0); + let _ = n.ilog2(); + assert!( + false, + "every nonpositive input must reach the logarithm panic helper" + ); + } + }; +} + +check_public!(u8, check_public_u8); +check_public!(u16, check_public_u16); +check_public!(u32, check_public_u32); +check_public!(u64, check_public_u64); +check_public!(u128, check_public_u128); +check_public!(usize, check_public_usize); +check_public!(i8, check_public_i8); +check_public!(i16, check_public_i16); +check_public!(i32, check_public_i32); +check_public!(i64, check_public_i64); +check_public!(i128, check_public_i128); +check_public!(isize, check_public_isize); + +check_nonzero!(u8, check_nonzero_u8); +check_nonzero!(u16, check_nonzero_u16); +check_nonzero!(u32, check_nonzero_u32); +check_nonzero!(u64, check_nonzero_u64); +check_nonzero!(u128, check_nonzero_u128); +check_nonzero!(usize, check_nonzero_usize); + +check_invalid!(u8, check_invalid_u8); +check_invalid!(u16, check_invalid_u16); +check_invalid!(u32, check_invalid_u32); +check_invalid!(u64, check_invalid_u64); +check_invalid!(u128, check_invalid_u128); +check_invalid!(usize, check_invalid_usize); +check_invalid!(i8, check_invalid_i8); +check_invalid!(i16, check_invalid_i16); +check_invalid!(i32, check_invalid_i32); +check_invalid!(i64, check_invalid_i64); +check_invalid!(i128, check_invalid_i128); +check_invalid!(isize, check_invalid_isize); diff --git a/library/core/src/num/imp/mod.rs b/library/core/src/num/imp/mod.rs index 843a800267ffb..de344c28585d9 100644 --- a/library/core/src/num/imp/mod.rs +++ b/library/core/src/num/imp/mod.rs @@ -17,6 +17,8 @@ pub mod x87_f80; pub(crate) mod int_bits; pub(crate) mod int_log10; +#[cfg(kani)] +mod int_log2; pub(crate) mod int_sqrt; pub(crate) mod libm; pub(crate) mod overflow_panic;