Repository navigation
Autoharness: verify Debug and Display implementations - #4701
Merged
feliperodri merged 1 commit intoAug 24, 2026
Merged
Conversation
feliperodri
force-pushed
the
autoharness-fmt-impls
branch
from
August 24, 2026 19:45
c52c099 to
3cac4a0
Compare
feliperodri
approved these changes
Aug 24, 2026
The fmt methods of Debug/Display implementations were always skipped, since their '&mut Formatter' argument has no Arbitrary implementation (Formatter wraps private state and a dyn Write sink and cannot be synthesized). In the top-100 crates.io evaluation, Formatter arguments accounted for ~1900 skipped functions, the single largest 'Missing Arbitrary' type. Rather than generating a Formatter, generate the harness the way user code exercises these implementations: format a nondeterministic value of the implementing type into a sink that discards the output, via two new models (check_debug_fmt/check_display_fmt) that call core::fmt::write(&mut Sink, format_args!(..., value)). The Formatter is constructed by the core formatting machinery, so it is always valid, and panics or undefined behavior inside the fmt implementation are detected as usual; reachability follows the format_args reference to the impl. The models are pure core and live in kani_core, so they are available in both library flavors. Detection uses trait_impl_of_assoc plus the Debug/Display diagnostic items; eligibility requires the self type to implement or be able to derive Arbitrary, with the usual skip reason otherwise. Towards model-checking#3832 Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
feliperodri
force-pushed
the
autoharness-fmt-impls
branch
from
August 24, 2026 20:48
3cac4a0 to
d035634
Compare
Member
|
Good catch — addressed. Added |
feliperodri
enabled auto-merge
August 24, 2026 20:53
This was referenced Sep 18, 2026
feliperodri
pushed a commit
to feliperodri/kani
that referenced
this pull request
Sep 25, 2026
…ing#4802) This gives the other seven formatting traits the automatic harness that `Debug` and `Display` get from model-checking#4701: `Binary`, `Octal`, `LowerHex`, `UpperHex`, `LowerExp`, `UpperExp` and `Pointer`. Their `fmt` methods take a `&mut Formatter`, which Kani cannot generate, so they were skipped. model-checking#4701 handles `Debug` and `Display` by formatting a nondeterministic value of the self type into a discarding sink. This PR sends the seven other traits through the same path. As before, the self type must implement or derive `Arbitrary`. On a whole-library `verify-std` run with `--bounded-arguments`, this adds 116 harnesses: - the primitive integer and float implementations (78); - references to them, `&i32` and `&mut i32` (14); - `NonZero`, `Wrapping` and `Saturating` (14); - `Box`, `Rc`, `Arc`, `UniqueRc` and `UniqueArc` of a primitive (5); - compiler_builtins' numeric types (3); - `NonNull` and `Unique` (2). Raw-pointer self types stay skipped, now reported for the self type. `Pointer` needs one extra step. `format_args!("{value:p}")` on a `&T` picks `impl Pointer for &T`, which prints the reference's own address and never calls `T`'s implementation. So its model formats through a local wrapper whose `Pointer::fmt` calls `<T as Pointer>::fmt` directly. Only `Debug`, `Display` and `Pointer` have diagnostic items. The other five traits are matched by name inside `core::fmt`. Changes: - `FmtTrait` gains the seven variants and a `model()` mapping. `AutomaticHarnessPass` holds the models in one map. - Seven `check_*_fmt` models in `kani_core`, beside the existing two. - `cargo_autoharness_fmt_impls` gains one type per trait, each failing on its own assert. A model sent to the wrong trait does not resolve for that type, so a swap fails at generation and cannot pass. - Docs. Part of model-checking#3832; follows model-checking#4701. 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: Srivatsan Samraj <321934658+srivatsansamraj@users.noreply.github.com>
srivatsansamraj
added a commit
to srivatsansamraj/kani
that referenced
this pull request
Sep 28, 2026
…r --bounded-arguments (model-checking#4824) This adds a model for `&Formatter` and `&mut Formatter` arguments under `--bounded-arguments`. Before it, 184 functions on a whole-library `verify-std` run were skipped for a missing Arbitrary implementation, with the `Formatter` as their only blocker. `Formatter::new` and the `FormattingOptions` builders are public under `formatting_options`. So `any_formatter` builds a real `Formatter` over the discarding sink `kani_core` already has for the fmt-trait models. Every option is nondeterministic: width and precision (unset, or up to the slice bound and at most `u16::MAX`), fill, alignment, sign, the alternate and zero-padding flags, and debug hex mode. Every combination is a valid `Formatter`, so nothing is assumed away. The sink never fails, so paths that handle a write error are not reached; the model's doc and the reference say so. The harness keeps the sink in a local, as it does a slice's storage. It calls the model into a `Formatter` local and passes `&Formatter` or `&mut Formatter` as the function expects. The type is found by its diagnostic item. `Debug` and `Display` implementations keep model-checking#4701's path. A `fmt` method under a contract goes through the contract harness instead. That harness now takes its `Formatter` from this model, so the method is reported as requiring `--bounded-arguments`. `cargo_autoharness_fmt_impls` is updated to match. The `kani` crate gains `#![feature(formatting_options)]`. Tests, in `cargo_autoharness_formatter` (same shape as `cargo_autoharness_slices`): - without `--bounded-arguments`, the functions are reported as requiring it; - with it, `pad` verifies for every option; - covers show that no width, the maximum width, center alignment, alternate with a plus sign, and a `*` fill are each generated; - an assert that no width or precision is set fails. Resolves model-checking#4823 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: Srivatsan Samraj <321934658+srivatsansamraj@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
fmtmethods ofDebug/Displayimplementations were always skipped by autoharness: their&mut Formatterargument has noArbitraryimplementation, andFormattercannot be synthesized (it wraps private state and adyn Writesink). In the top-100 crates.io evaluation,Formatterarguments were the single largest "Missing Arbitrary" type (~1,900 skipped functions).Rather than generating a
Formatter, generate the harness the way user code exercises these implementations: format a nondeterministic value of the implementing type into a sink that discards the output, via two new models that callcore::fmt::write(&mut DiscardingSink, format_args!(..., value)). TheFormatteris constructed by the core formatting machinery — so it is always valid — and panics or undefined behavior inside the user'sfmtare detected as usual (reachability follows theformat_args!reference to the impl). The models are purecoreand live inkani_core, available in both library flavors — no optional-model machinery needed.Detection uses
trait_impl_of_assocplus theDebug/Displaydiagnostic items; eligibility requires the self type to implement (or be able to derive)Arbitrary. The self value is generated withkani::anythrough the sharedcall_kani_any_for_tyhelper, so these harnesses are unbounded (full coverage of the self type — no--bounded-argumentsinterplay) and the self type'sInvariant, if any, is assumed just like for a regular argument.Both the eligibility check (
automatic_harness_partition) and the harness generation (AutomaticHarnessPass) go through the samefmt_impl_self_typredicate, so the two cannot disagree about which functions the formatting models handle.A
fmtmethod carrying a function contract is deliberately excluded: the automatic contract harness calls the function directly (and would therefore need aFormatter), and the formatting models reachfmtthrough a function pointer, where contract dispatch does not apply. Such a method is reported as skipped for its&mut Formatterargument, exactly as before this change.Compiler-derived
Debugimplementations are verified too. That is consistent with how autoharness already treats other derived implementations (Clone::clone,PartialEq::eq,PartialOrd::partial_cmp,Default::defaultall get harnesses today), so no separate filter was added.The documented limitations of the approach: the
Formattercarries the default formatting parameters (so paths taken only for a non-default width/precision/fill/alignment/sign or the alternate{:#?}flag are not covered), the sink never fails (so?write-error paths are not covered), and failures may point at a location insidecore::fmtsince the core formatting machinery is verified along with thefmtimplementation.Testing
New script-based test
cargo_autoharness_fmt_impls, covering:Debugimpl (assertion reachable — fails, with the panic message asserted in the expected output),Displayimpl (assertion reachable — fails), so that theDisplaymodel and its compiler branch are exercised by a failing harness too, not only by passing ones,Displayimpl on a compiler-derived self type (passes),Displayimpl on a self type with anInvariant(passes, i.e. the invariant is assumed — verified separately that the body is reached by negating the assertion),Debugimpl (passes),fmtmethod under contract (skipped for its&mut Formatterargument).Full
script-based-presuite (64 tests, including all 20 autoharness tests andverify_std_cmd, sincekani_coreis shared with theno_coreflow),cargo testforkani-compiler/kani-driver/kani_metadata/cprover_bindings,clippy --all-targets, andkani-fmt --checkall pass.Towards #3832
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.