Repository navigation
Lower native assertion failures through the GOTO panic hook - #5203
Conversation
Skip core assertion formatting during reachability for GOTO. Preserve user stubs and evaluate projected call operands before emitting the panic property.
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
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
assertionclass. - The path after a failed
assert_eq!/assert_ne!is still cut (a laterkani::cover!is unsatisfiable). should_panicoutcomes are unchanged, including the "no panic encountered" case, contracts withshould_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!andcore::assert_eq!without--no-assert-overrides. In particular,core::assert_eq!andassert_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.
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.
b54a91f to
dc85d0d
Compare
|
Thanks for the thorough review and helpful suggestions! I've pushed the revisions in The changes include:
Validation on the revised commit:
The GOTO symbol table measurements remain:
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
left a comment
There was a problem hiding this comment.
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.
9834c96
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_backharness on its pinned toolchain motivated the change.Additional implementation notes
assert_failed,assert_failed_inner, andassert_matches_failedin the same core module aspanic_fmt. Apply the rewrite before reachability in GOTO, which provides thePanicStubhook.assert_failed_innerstub when the assertion calls an outer entry first. The formatter helper guard also uses the enabled stub map.RustcIntrinsicsPassprepends a static diagnostic and keeps the original MIR operands in the rewritten call topanic_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.panic_stub. This also applies to the existing rewrite of generic panic functions such asbegin_panic::<M>.assert_ne!anddebug_assert_eq!report the comparison expression.core::assert_eq!andassert_matches!report the new fixed diagnostics at the assertion lines.Diagnostics
Rewritten comparison assertions report
assertion failed. Rewritten pattern assertions reportpattern assertion failed. These fixed diagnostics also apply when a literal custom message is supplied. The base reports the runtime-formatting placeholder fromcore::panickingfor the same native assertions.The model retains the caller's construction of
fmt::Argumentsand evaluates the original operands before emitting the panic property and ending the path. Formatting callbacks such asDebug::fmtare outside the modeled panic path.Validation
Validated revision
dc85d0dagainst upstream basec25a57bcwithnightly-2026-11-18, CBMC 6.11.0, and CVC5 1.3.0.assert_failedandassert_failed_inner. Separateassert_eq!andassert_matches!harnesses check the inner stub with distinct expected messages.should_panicharness.Assert,Stubbing, andFmtDerivegroups report 59 passes with two existing fixme cases ignored. All 53 expected-output cases selected byassert panic stuband all 25 LLBC cases pass.Validation clarification
The standalone CVC5 harness passes on upstream base
c25a57bcand 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.rsandnative-assert-projected-argument.rspass 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_checkoutandcandidate_checkoutto 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.check_failed_equalitycheck_failed_patternExtends 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.