Skip to content

Autoharness: support MultiCharEq searcher arguments under --bounded-arguments - #5064

Merged
feliperodri merged 6 commits into
model-checking:mainfrom
CYJ904:autoharness-multichareq-searcher
Nov 9, 2026
Merged

feliperodri merged 6 commits into
model-checking:mainfrom
CYJ904:autoharness-multichareq-searcher

Conversation

@CYJ904

@CYJ904 CYJ904 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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 take CharArraySearcher, CharArrayRefSearcher or CharSliceSearcher arguments; nothing fails while it waits.

With --bounded-arguments, autoharness can now generate CharArraySearcher<'_, N>, CharArrayRefSearcher<'_, '_, N> and CharSliceSearcher<'_, '_> arguments, by value, & or &mut. These are the searchers for [char; N], &[char; N] and &[char] patterns in core::str::pattern.

Changes

  1. New kani_core models build the searcher through the public Pattern::into_searcher API over a bounded nondeterministic haystack (from any_str_ref, string bound) and a nondeterministic needle ([char; N], or a &[char] of up to the slice bound for CharSliceSearcher). They then advance it a nondeterministic number of steps from each end, so that searchers in partially advanced states are also generated.
  2. multi_char_eq_searcher_kind identifies the three types by definition path and is shared by the eligibility check and harness generation.
  3. The searchers' Debug::fmt methods are verified as ordinary functions rather than through the formatting-trait model, since the searcher needs harness-owned storage.
  4. The storage-array construction of the slice/str branches is factored into build_any_array_storage_ref. The needle storage of CharArrayRefSearcher and CharSliceSearcher is generated with a single kani::any() call (build_any_whole_array_storage_ref), so a large N does not grow the harness body.
  5. The reference page and the --slice-bound/--string-bound help text document the new argument types.

Testing

  1. New test 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 10m rather than the usual 5m because that harness came close to the 5m limit on macos-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.
  2. All 42 autoharness regression tests pass. cargo fmt --check and cargo clippy --workspace --tests -- -D warnings pass.

Results on verify-std

  1. With --bounded-arguments, all 27 functions listed in Autoharness: support the MultiCharEq searcher types in core::str::pattern #4811 now get harnesses (9 per searcher type). The other 23 functions in pattern.rs (CharPredicateSearcher, CharSearcher, StrSearcher) are still skipped and are out of scope here.
  2. Verifying the 27 harnesses (release build, string bound 4, slice bound 16, 10-minute timeout each): 8 fail and 19 time out.
  3. The 8 failures are the preconditions of char::from_u32_unchecked and hint::unreachable_unchecked, caused by Autoharness: &str arguments can be invalid UTF-8 under verify-std, causing spurious failures #5030 (&str from any_str_ref can be invalid UTF-8 under verify-std). CBMC traces for CharArraySearcher::haystack and CharArraySearcher::next confirm this: the haystack bytes are mis-validated by from_utf8, and the failures occur while the model advances the searcher, before the function under test runs. For next, the call graph shows the unreachable_unchecked check 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

  1. This PR and Restructure and update the autoharness reference page #4991 both edit docs/src/reference/experimental/autoharness.md. I'll resolve the conflict in whichever merges second.
  2. Related: Challenge 20: Verify char-class Searcher safety in str::pattern with Kani verify-rust-std#722 adds hand-written harnesses for these searchers and builds haystacks with 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.

…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`.
@CYJ904
CYJ904 requested review from a team as code owners October 26, 2026 00:00
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Oct 26, 2026
…areq-searcher

# Conflicts:
#	docs/src/reference/experimental/autoharness.md
@feliperodri feliperodri added [C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-Autoharness Issue related to autoharness subcommand labels Nov 6, 2026
@feliperodri feliperodri added this to the Autoharness milestone Nov 6, 2026
@feliperodri
feliperodri requested a balanced review from Copilot November 6, 2026 18:59
@feliperodri feliperodri self-assigned this Nov 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
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_core bounded generation models for CharArraySearcher, CharArrayRefSearcher, and CharSliceSearcher.
  • 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::fmt handling.
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

Comment thread library/kani/src/lib.rs Resolved

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Large N gives a large harness array (CharArrayRefSearcher<'_, '_, N>, see Copilot's threads). Cap it, or document the limit.
  2. In multichareq-searcher.sh, use set -euo pipefail and pass --string-bound 4 explicitly. Note the ~7-minute runtime in the PR.
  3. The lib.rs comment says std::str::pattern, but the models use core::str::pattern.
  4. regression (macos-15-intel) fails. Please confirm it's the macOS runner.

@feliperodri feliperodri assigned CYJ904 and unassigned feliperodri Nov 7, 2026
CYJ904 and others added 2 commits November 7, 2026 18:56
…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.
@CYJ904

CYJ904 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! All four points are addressed in the latest commit:

  1. Large N: the needle storage of CharArrayRefSearcher (and CharSliceSearcher) is now generated with a single kani::any::<[char; M]>() call instead of one call per element, so the harness body no longer grows with N. The models already overwrite that storage with kani::any(), so behaviour is unchanged. A large N still gives a large nondeterministic needle, as for any [T; N] argument; the reference page now says so. In practice this only affects user code with a concrete large N: for generic functions (all 27 in verify-std), N is instantiated with the usual const-generic value of 2.
  2. The script now uses set -euo pipefail and passes --string-bound 4 explicitly. I also trimmed the test from 10 to 7 harnesses (full covers on each by-value harness, a single cover on the &mut variants), which brings it from about 7 to about 4 minutes locally. The PR description is updated.
  3. The lib.rs comment now refers to core::str::pattern.
  4. The macos-15-intel failure was this PR's new test, not the runner: it took 1567s there, while main passes on the same runner. The slowest harness takes about 70s locally and that runner is roughly 4x slower, so it came close to the 5m --harness-timeout. Besides trimming the test, this test now uses --harness-timeout 10m. I measured alternatives: lowering --slice-bound made no measurable difference, and --string-bound 3 would drop 4-byte UTF-8 characters from the haystack.

@CYJ904
CYJ904 requested a review from feliperodri November 8, 2026 04:51

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread library/kani_core/src/arbitrary.rs Resolved
CYJ904 and others added 2 commits November 8, 2026 16:35
- 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.
@CYJ904

CYJ904 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the three nits in 55ab544 (expect messages, shared advance_nondet helper, reflowed docs); the multichareq test's timings are unchanged locally. Could you approve the workflow runs when you get a chance? Thanks!

@feliperodri

Copy link
Copy Markdown
Member

Thanks @CYJ904, all three nits are in. The workflows already ran on 55ab544: everything passes except the macos-14 jobs, which GitHub cancels on its side. Nothing else is needed from you.

@feliperodri
feliperodri added this pull request to the merge queue Nov 9, 2026
Merged via the queue into model-checking:main with commit 1d99701 Nov 9, 2026
23 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-Autoharness Issue related to autoharness subcommand Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autoharness: support the MultiCharEq searcher types in core::str::pattern

3 participants