Skip to content

Challenge 28: Verify flt2dec safety with Kani - #818

Closed
fromtaoyuanhsinchuuuu wants to merge 3 commits into
model-checking:mainfrom
fromtaoyuanhsinchuuuu:ch28/flt2dec
Closed

fromtaoyuanhsinchuuuu wants to merge 3 commits into
model-checking:mainfrom
fromtaoyuanhsinchuuuu:ch28/flt2dec

Conversation

@fromtaoyuanhsinchuuuu

@fromtaoyuanhsinchuuuu fromtaoyuanhsinchuuuu commented Dec 3, 2026 •

Copy link
Copy Markdown

Challenge 28: Verify the flt2dec module

Closes #524.

This PR adds automated Kani harnesses for all twelve functions listed in
Challenge 28:

Function Harnesses
digits_to_dec_str, digits_to_exp_str 2
to_shortest_str, to_shortest_exp_str, to_exact_exp_str, to_exact_fixed_str (f32, f64, f16) 12
flt2dec::strategy::grisu::{format_shortest_opt, format_shortest, format_exact_opt, format_exact} 5
flt2dec::strategy::dragon::{format_shortest, format_exact} 3
shared ghost/bound helpers 2

24 harnesses in total, all of which pass locally with Kani 0.68.0.

Approach

  • flt2dec::mod (ghost witnesses). The formatting entry points are driven
    with symbolic Decoded values and a callback that over-approximates
    format_* returning an arbitrary non-empty digit slice. To prove the
    assume_init calls only expose written digits, the added flt2dec::ghost
    module allocates uninitialized buffers with malloc (CBMC with
    --object-bits 12) and checks a symbolic witness index with
    assert_unchecked, which is a non-panic UB check, so #[kani::should_panic]
    cannot mask an uninitialized byte.
  • dragon. Big arithmetic is over-approximated by unconstrained
    stub_set functions; the digit loop and every assume_init site are real.
    A #[cfg_attr(kani, safety::loop_invariant(true))] keeps div_2pow10
    tractable.
  • grisu. The digit-generation loops carry Kani loop contracts, and the
    hoisted round_and_weed helper is checked through a verified contract and
    #[kani::stub_verified]. The expensive 64-bit scaling steps are abstracted
    only under cfg(kani) to their numeric contract (a decimal digit / remainder
    bound); the buffer writes and all assume_init sites remain the real code.

The Grisu verification strategy is adapted from @v3risec's Challenge 28
solution in #606 (rebased onto the current library/core/src/num/imp layout);
the commit is co-authored accordingly.

Safety obligations

The proofs cover, for the harnessed preconditions (the documented
assert!/debug_assert! contracts of each function):

  • every MaybeUninit::assume_init* only sees written memory;
  • every buffer write is in bounds;
  • the lifetime laundering in format_shortest / format_exact does not
    introduce UB;
  • no dangling/misaligned access, invalid value, immutable-byte mutation, or
    problematic intrinsic use is reachable.

Verification performed locally

scripts/run-kani.sh --kani-args --harness 'flt2dec::' -j 4
  • Kani: 24/24 harnesses verified, 0 failures (~2.5 min on 20 cores).
  • cargo check -p core: passes.
  • rustfmt --check with the upstream style_edition = "2024" config: clean.

No non-#[cfg(kani)] logic is changed beyond hoisting the private
round_and_weed helper out of format_shortest_opt so it can be named by
#[kani::stub_verified].

Resolves #524.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

fromtaoyuanhsinchuuuu and others added 3 commits December 4, 2026 02:04
Use loop contracts and a verified round_and_weed contract to prove that the
Grisu digit-generation loops only expose initialized ASCII digits. The
approach is adapted from @v3risec's Challenge 28 solution (PR model-checking#606) to the
current library/core/src/num/imp layout.

Co-authored-by: v3risec <phalange123@126.com>
@fromtaoyuanhsinchuuuu
fromtaoyuanhsinchuuuu requested a review from a team as a code owner December 3, 2026 18:09
@fromtaoyuanhsinchuuuu

Copy link
Copy Markdown
Author

Closing to rework the strategy proofs: the Grisu harnesses currently follow the cfg(kani) body-swap / assume-the-conclusion approach that was already rejected on #606. I will reopen with production bodies unchanged, machine-checked loop invariants, and no assumed digit bounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Challenge 28: Safety of float to decimal conversion module

1 participant