Skip to content

Lower native assertion failures through the GOTO panic hook - #5203

Merged
feliperodri merged 2 commits into
model-checking:mainfrom
v3risec:fix/native-assert-panic-lowering
Nov 20, 2026
Merged

feliperodri merged 2 commits into
model-checking:mainfrom
v3risec:fix/native-assert-panic-lowering

Conversation

@v3risec

@v3risec v3risec commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Lower core's native comparison and pattern assertion failures through Kani's existing GOTO panic model. On the current PR base, this reduces the GOTO program and reports failures at the user's assertion with a fixed diagnostic. A CBMC SMT conversion failure in the Challenge 18 ChunksExactMut::nth_back harness on its pinned toolchain motivated the change.

Additional implementation notes

  • Match assert_failed, assert_failed_inner, and assert_matches_failed in the same core module as panic_fmt. Apply the rewrite before reachability in GOTO, which provides the PanicStub hook.
  • Check the enabled user-stub map once per codegen unit. An active stub for any of the three assertion entries disables their lowering for that unit. This preserves an assert_failed_inner stub when the assertion calls an outer entry first. The formatter helper guard also uses the enabled stub map.
  • RustcIntrinsicsPass prepends a static diagnostic and keeps the original MIR operands in the rewritten call to panic_stub. The GOTO hook reads operand 0 as the message and evaluates the other operands in order for their safety checks before emitting the assertion property.
  • Clear the original generic arguments when redirecting to the non-generic panic_stub. This also applies to the existing rewrite of generic panic functions such as begin_panic::<M>.
  • With the default assertion configuration, assert_ne! and debug_assert_eq! report the comparison expression. core::assert_eq! and assert_matches! report the new fixed diagnostics at the assertion lines.

Diagnostics

Rewritten comparison assertions report assertion failed. Rewritten pattern assertions report pattern assertion failed. These fixed diagnostics also apply when a literal custom message is supplied. The base reports the runtime-formatting placeholder from core::panicking for the same native assertions.

The model retains the caller's construction of fmt::Arguments and evaluates the original operands before emitting the panic property and ending the path. Formatting callbacks such as Debug::fmt are outside the modeled panic path.

Validation

Validated revision dc85d0d against upstream base c25a57bc with nightly-2026-11-18, CBMC 6.11.0, and CVC5 1.3.0.

  • The diagnostics regression observes the user's own messages for stubs of assert_failed and assert_failed_inner. Separate assert_eq! and assert_matches! harnesses check the inner stub with distinct expected messages.
  • The defensive inactive-stub regression uses raw compiler flags to allow the annotation while leaving the stub pass disabled. The explicit MIR regression checks that a null pointer dereference in a projected operand is reported in a should_panic harness.
  • The Assert, Stubbing, and FmtDerive groups report 59 passes with two existing fixme cases ignored. All 53 expected-output cases selected by assert panic stub and all 25 LLBC cases pass.

Validation clarification

The standalone CVC5 harness passes on upstream base c25a57bc and does not reproduce the original Challenge 18 SMT conversion failure there. On that base the demonstrated benefits are a smaller GOTO program and improved assertion diagnostics. assert_failed_native.rs and native-assert-projected-argument.rs pass on both the base and the revision as regression guards for assertion, panic, and safety-check behavior.

The original Challenge 18 end-to-end result uses its project pin. The PR base's nightly stops while compiling that project's core, before the Challenge 18 harness can run.

GOTO program size

Set main_checkout and candidate_checkout to the absolute paths of the independently built base and PR checkouts, then run the script with Bash. It runs the same fixture for both harnesses in fresh temporary directories.

set -eu
KANI_MAIN="$main_checkout/scripts/kani"
KANI_CANDIDATE="$candidate_checkout/scripts/kani"
fixture="$candidate_checkout/tests/kani/Assert/assert_failed_native.rs"

for KANI in "$KANI_MAIN" "$KANI_CANDIDATE"; do
  for harness in check_failed_equality check_failed_pattern; do
    (
      run_dir="$(mktemp -d)"
      trap 'rm -rf -- "$run_dir"' EXIT
      cp "$fixture" "$run_dir/native.rs"
      cd "$run_dir"

      printf '%s %s\n' "$KANI" "$harness"
      "$KANI" native.rs -Z unstable-options --no-assert-overrides --exact \
        --harness "$harness" --only-codegen --keep-temps
      wc -c ./*.symtab.out
      python3 -c 'import json, sys; print(len(json.load(open(sys.argv[1]))))' ./*.pretty_name_map.json
    )
  done
done
Harness Base symbol table bytes Revised symbol table bytes Base pretty-name entries Revised pretty-name entries
check_failed_equality 2,210,210 24,478 7,424 160
check_failed_pattern 2,207,361 17,297 7,413 133

Extends the panic lowering from #4169. The pinned-toolchain SMT failure is related to diffblue/cbmc#9159.

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

Skip core assertion formatting during reachability for GOTO. Preserve user stubs and evaluate projected call operands before emitting the panic property.

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.

🟢 Approval recommended

No active review issues remain for the native assertion-lowering changes.

No open issues 🎉

What changed in this PR

This PR extends Kani’s existing panic lowering to native comparison and pattern assertion failures, avoiding unnecessary formatter code in GOTO programs.

Changes:

  • Rewrite native assertion failures before GOTO reachability while preserving enabled user-stub precedence.
  • Emit fixed diagnostics after evaluating the original call operands, retaining safety checks.
  • Add regressions for native assertions, diagnostics, stub activation, and projected operand reads.
File Description
tests/​kani/​Assert/​assert_failed_native.rs Tests native assertion failures and skipped message evaluation on success.
tests/​expected/​native-assert-projected-argument.rs Tests that projected operand reads retain null-pointer checks.
tests/​expected/​native-assert-projected-argument.expected Requires the safety failure rather than an accepted panic.
tests/​expected/​native-assert-inactive-stub.rs Tests that inactive stub mappings do not suppress assertion lowering.
tests/​expected/​native-assert-inactive-stub.expected Requires the native pattern-assertion diagnostic.
tests/​expected/​native-assert-diagnostics.rs Tests fixed diagnostics and active user-stub precedence.
tests/​expected/​native-assert-diagnostics.expected Records expected assertion and user-stub messages.
kani-compiler/​src/​kani_middle/​transform/​rustc_intrinsics.rs Adds GOTO-only assertion lowering and enabled-stub guards.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​assert.rs Evaluates call operands before emitting the panic property.

Review effort: Balanced

@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 for this. Lowering the native assertion entry points through the existing panic hook is a contained extension of #4169. It cuts the GOTO program a lot: on an assert_eq! failure I measured a 2.2 MB to 35 KB symbol table. It also gives a real diagnostic and points the failure at the user's assert_eq! line instead of core/src/panicking.rs. I checked soundness against the base commit:

  • Failures are still reported with the assertion class.
  • The path after a failed assert_eq!/assert_ne! is still cut (a later kani::cover! is unsatisfiable).
  • should_panic outcomes are unchanged, including the "no panic encountered" case, contracts with should_panic, stub_verified, uninit and valid-value checks, and --prove-safety-only.

Evaluating the call operands in codegen_panic is a good catch for projected operands.

One behaviour change I'd like fixed before merging. The guard only checks whether the direct callee is stubbed, so a user stub of core::panicking::assert_failed_inner is now silently bypassed: on main the stub's message is reported, with this PR it isn't. Could you skip the lowering when any of the three assertion entries is in the stub map, and add that case to native-assert-diagnostics.rs?

Smaller items:

  • Please document that the rewritten call deliberately passes extra operands to panic_stub.
  • args.0.clear() now also applies to the existing panic-function path; a comment and a note in the description would help.
  • It would be good to cover assert_ne!, debug_assert_eq! and core::assert_eq! without --no-assert-overrides. In particular, core::assert_eq! and assert_matches! reach this path under default flags.

Also, assert_failed_native.rs and native-assert-projected-argument.rs pass on main as well. That's fine as regression guards, but the cvc5 harness doesn't reproduce the original SMT failure, so the description should say the benefit on current main is program size and diagnostics.

@feliperodri feliperodri added the Z-Kani Compiler Issues that require some changes to the compiler label Nov 20, 2026
@feliperodri feliperodri assigned v3risec and unassigned feliperodri Nov 20, 2026
Keep the original assertion call chain when an assertion entry has an active user stub. Cover comparison and pattern failures and document the panic hook's extra operands.
@v3risec
v3risec force-pushed the fix/native-assert-panic-lowering branch from b54a91f to dc85d0d Compare November 20, 2026 16:13
@v3risec

v3risec commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and helpful suggestions! I've pushed the revisions in dc85d0d.

The changes include:

  • User stub precedence: Disabled assertion lowering for the entire codegen unit if any of the three native assertion failure entries has an active user stub. Added regression tests for both comparison and pattern assertions using an assert_failed_inner stub.
  • Panic hook documentation: Documented the extra-operand contract in panic_stub and the GOTO Panic hook, and clarified why generic arguments are cleared when redirecting to the non-generic PanicStub.
  • Backend and test documentation: Clarified that assertion lowering is GOTO-only and documented the inactive-stub test as a defensive test.
  • Default assertion coverage: Added tests for assert_ne!, debug_assert_eq!, core::assert_eq!, and assert_matches! under the default assertion configuration.

Validation on the revised commit:

  • 53 expected-output tests passed.
  • 59 Assert, Stubbing, and FmtDerive tests passed (2 existing fixme cases ignored).
  • All 25 LLBC non-regression tests passed.

The GOTO symbol table measurements remain:

Harness Base Revised
Equality 2,210,210 bytes 24,478 bytes
Pattern 2,207,361 bytes 17,297 bytes

The PR description has also been updated to clarify the validation scope: the standalone CVC5 harness does not reproduce the original Challenge 18 SMT failure on current main. The demonstrated benefits are reduced GOTO program size and improved assertion diagnostics. The original Challenge 18 end-to-end result remains based on the project's pinned toolchain.

Thanks again for the careful review. I believe all requested changes have been addressed. I appreciate your taking another look.

@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 for the quick turnaround. I re-checked dc85d0d: a stub of assert_failed_inner now runs again (my probe reports the stub's message, as on main), and since the check covers all three entry points, it also subsumes the old per-call contains_key guard. The operand contract is documented at both ends, and the new tests pass locally along with clippy and fmt. Approving.

Minor, no action needed: in native-assert-default.rs, assert_ne! and debug_assert_eq! are handled by Kani's assert overrides (hence the 0u8 != 0u8 message), so only core::assert_eq! and assert_matches! go through the new lowering. They're still useful as output checks.

@feliperodri feliperodri removed Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Nov 20, 2026
@feliperodri feliperodri assigned feliperodri and unassigned v3risec Nov 20, 2026
@feliperodri feliperodri added Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI labels Nov 20, 2026
@feliperodri feliperodri removed their assignment Nov 20, 2026
@feliperodri
feliperodri added this pull request to the merge queue Nov 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 20, 2026
@feliperodri
feliperodri added this pull request to the merge queue Nov 20, 2026
Merged via the queue into model-checking:main with commit 9834c96 Nov 20, 2026
34 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-Kani Compiler Issues that require some changes to the compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants