Repository navigation
Autoharness: support MultiCharEq searcher arguments under --bounded-arguments - #5064
Conversation
…rguments Generate `CharArraySearcher<'_, N>`, `CharArrayRefSearcher<'_, '_, N>` and `CharSliceSearcher<'_, '_>` arguments (by value, `&` or `&mut`) when `--bounded-arguments` is passed. The new `kani_core` models build the searcher with `Pattern::into_searcher` over a bounded nondeterministic haystack (the string bound) and a nondeterministic needle (`[char; N]`, or a `&[char]` of up to the slice bound for `CharSliceSearcher`), then advance it a nondeterministic number of steps from each end so that searchers in partially advanced states are also generated. `multi_char_eq_searcher_kind` identifies the three types by definition path and is shared by the eligibility check and harness generation. The searchers' `Debug::fmt` methods are verified as ordinary functions rather than through the formatting-trait model, since the searcher needs harness-owned storage. Also factor the storage-array construction of the slice/str branches into `build_any_array_storage_ref`, and document the new argument types and the extended scope of `--slice-bound`/`--string-bound`.
…areq-searcher # Conflicts: # docs/src/reference/experimental/autoharness.md
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
6 open issues
ForCharArrayRefSearcher<'_, '_, N>,needle_lencomes directly from the const genericNin… · New ForCharArrayRefSearcher<'_, '_, N>,needle_lencomes directly from the const genericNin… · New This test appears to rely on the default--string-boundbeing 4 (also referenced in the test… · New The comment mentionsstd::str::pattern::CharArraySearcher, but the added models are built against… · Newset -edoes not reliably propagate failures across pipelines unlesspipefailis set. Consider… · Newset -edoes not reliably propagate failures across pipelines unlesspipefailis set. Consider… · New
What changed in this PR
Adds autoharness support for core::str::pattern MultiCharEq-backed searcher arguments under --bounded-arguments, including new generation models, compiler support to recognize these types, and a new regression test + documentation updates.
Changes:
- Add
kani_corebounded generation models forCharArraySearcher,CharArrayRefSearcher, andCharSliceSearcher. - Extend autoharness argument generation/eligibility logic to recognize these searcher ADTs and generate harness-owned storage for haystack/needle.
- Add a new script-based regression test and update docs/help text to document the new supported argument types and
Debug::fmthandling.
| File | Description |
|---|---|
| tests/script-based-pre/cargo_autoharness_multichareq_searcher/src/lib.rs | New regression test crate exercising searcher arguments by value/&/&mut plus one expected-fail harness. |
| tests/script-based-pre/cargo_autoharness_multichareq_searcher/multichareq-searcher.sh | Script to run kani autoharness with/without --bounded-arguments and validate output. |
| tests/script-based-pre/cargo_autoharness_multichareq_searcher/multichareq-searcher.expected | Expected output for the new regression test run. |
| tests/script-based-pre/cargo_autoharness_multichareq_searcher/config.yml | Wires the script + expected output and declares expected non-zero exit status. |
| tests/script-based-pre/cargo_autoharness_multichareq_searcher/Cargo.toml | Defines the new test crate configuration and lints. |
| library/kani_core/src/arbitrary.rs | Adds bounded models to construct/advance searchers using Pattern::into_searcher. |
| library/kani/src/lib.rs | Enables the pattern feature to support compiling the new searcher-related code paths. |
| kani-driver/src/args/autoharness_args.rs | Updates CLI help text for --slice-bound and --string-bound to mention searcher needle/haystack. |
| kani-compiler/src/kani_middle/transform/automatic.rs | Adds array-storage helper and generates bounded values for the three searcher ADTs via dedicated models. |
| kani-compiler/src/kani_middle/mod.rs | Adds multi_char_eq_searcher_kind helper, adjusts fmt-model detection, and extends bounded-arg eligibility. |
| kani-compiler/src/kani_middle/kani_functions.rs | Registers new KaniModel variants for the searcher generation models. |
| kani-compiler/src/kani_middle/codegen_units.rs | Notes the exception for searcher fmt methods not being treated as formatting-model harnesses. |
| docs/src/reference/experimental/autoharness.md | Documents searcher-argument support and Debug::fmt handling, including verify-std UTF-8 caveat. |
Review effort: Lite
feliperodri
left a comment
There was a problem hiding this comment.
The models go through the public into_searcher API, and the eligibility check and harness generation share one identifier. The verify-std results are strong evidence.
Before merge:
- Large
Ngives a large harness array (CharArrayRefSearcher<'_, '_, N>, see Copilot's threads). Cap it, or document the limit. - In
multichareq-searcher.sh, useset -euo pipefailand pass--string-bound 4explicitly. Note the ~7-minute runtime in the PR. - The
lib.rscomment saysstd::str::pattern, but the models usecore::str::pattern. regression (macos-15-intel)fails. Please confirm it's the macOS runner.
…unds - Generate the needle storage of CharArrayRefSearcher and CharSliceSearcher arguments with a single kani::any::<[char; M]>() call (build_any_whole_array_storage_ref) instead of one call per element, so a large N in CharArrayRefSearcher<'_, '_, N> no longer grows the harness body. The models already overwrite this storage, so behaviour is unchanged. Document that a large N gives a large nondeterministic needle, as for any [T; N] argument. - Test: keep the full set of cover checks on the by-value harness of each searcher type and a single cover on the &mut variants; drop the & variants (CharSliceSearcher's & path is still covered by haystack_len_is_not_three). 10 -> 7 harnesses, 90 -> 33 covers, about 7 min -> about 4 min locally. - Script: set -euo pipefail, grep via a here-string, pass --string-bound 4 explicitly, and raise --harness-timeout to 10m for this test (slowest harness is ~70s locally and approached the 5m limit on macos-15-intel). - lib.rs: the pattern feature comment now names core::str::pattern.
|
Thanks for the review! All four points are addressed in the latest commit:
|
feliperodri
left a comment
There was a problem hiding this comment.
Thanks, all four points are addressed: the needle is one kani::any(), the script uses pipefail with explicit bounds, the lib.rs comment says core, and the macos-15-intel timeout is explained and widened. The models reach every state the public API can reach, and the only assumption (len <= M) can't hide bugs.
- automatic.rs: replace unwrap() on the searcher needle length with expect() stating that autoharness instantiates const generics before generating harnesses. - kani_core: the three searcher models now share a private advance_nondet::<_, N>(&mut searcher) helper instead of three copies of the advance loop. N stays a const generic, so each loop keeps a constant bound; verification times are unchanged. - autoharness.md: reflow the searcher paragraph.
|
Addressed the three nits in 55ab544 (expect messages, shared |
1d99701



This fixes #4811, an issue in the Autoharness milestone.
Priority: Low (L3, a coverage gap under 100 functions). This adds automatic harnesses, under
--bounded-arguments, for 27 standard-library functions that takeCharArraySearcher,CharArrayRefSearcherorCharSliceSearcherarguments; nothing fails while it waits.With
--bounded-arguments, autoharness can now generateCharArraySearcher<'_, N>,CharArrayRefSearcher<'_, '_, N>andCharSliceSearcher<'_, '_>arguments, by value,&or&mut. These are the searchers for[char; N],&[char; N]and&[char]patterns incore::str::pattern.Changes
kani_coremodels build the searcher through the publicPattern::into_searcherAPI over a bounded nondeterministic haystack (fromany_str_ref, string bound) and a nondeterministic needle ([char; N], or a&[char]of up to the slice bound forCharSliceSearcher). They then advance it a nondeterministic number of steps from each end, so that searchers in partially advanced states are also generated.multi_char_eq_searcher_kindidentifies the three types by definition path and is shared by the eligibility check and harness generation.Debug::fmtmethods are verified as ordinary functions rather than through the formatting-trait model, since the searcher needs harness-owned storage.build_any_array_storage_ref. The needle storage ofCharArrayRefSearcherandCharSliceSearcheris generated with a singlekani::any()call (build_any_whole_array_storage_ref), so a largeNdoes not grow the harness body.--slice-bound/--string-boundhelp text document the new argument types.Testing
cargo_autoharness_multichareq_searcher: 7 harnesses, 6 pass and 1 fails as expected, and all 33 cover properties are satisfied. It takes about 4 minutes locally with--string-bound 4 --slice-bound 4; the slowest harness (char_slice_value) takes about 70s. The script uses--harness-timeout 10mrather than the usual 5m because that harness came close to the 5m limit onmacos-15-intel. Timing is driven by the string bound (haystack size): lowering the slice bound from 4 to 2 made no measurable difference, and a string bound of 3 would drop 4-byte UTF-8 characters from the haystack.cargo fmt --checkandcargo clippy --workspace --tests -- -D warningspass.Results on verify-std
--bounded-arguments, all 27 functions listed in Autoharness: support theMultiCharEqsearcher types incore::str::pattern#4811 now get harnesses (9 per searcher type). The other 23 functions inpattern.rs(CharPredicateSearcher,CharSearcher,StrSearcher) are still skipped and are out of scope here.char::from_u32_uncheckedandhint::unreachable_unchecked, caused by Autoharness: &str arguments can be invalid UTF-8 under verify-std, causing spurious failures #5030 (&strfromany_str_refcan be invalid UTF-8 under verify-std). CBMC traces forCharArraySearcher::haystackandCharArraySearcher::nextconfirm this: the haystack bytes are mis-validated byfrom_utf8, and the failures occur while the model advances the searcher, before the function under test runs. Fornext, the call graph shows theunreachable_uncheckedcheck is reachable only from UTF-8 decoding. The other 6 fail on the same two checks and very likely share the cause, but I did not trace them individually.Notes
docs/src/reference/experimental/autoharness.md. I'll resolve the conflict in whichever merges second.encode_utf8.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.