Skip to content

Stub panics during MIR transformation - #4169

Merged
AlexanderPortland merged 4 commits into
model-checking:mainfrom
AlexanderPortland:panic-stubbing
Jun 23, 2025
Merged

AlexanderPortland merged 4 commits into
model-checking:mainfrom
AlexanderPortland:panic-stubbing

Conversation

@AlexanderPortland

Copy link
Copy Markdown
Contributor

Modifies the RustcIntrinsicsPass to stub out panic functions during body transformation at the MIR level. This avoids having to bring in (and do transformation passes on) the large amounts of code they use for string formatting. Our panic hook has been modified to then detect these stubs during codegen and still generate the same Goto code for them.

The results of this change on the MIR bloat of panic!() and its callers is shown below:

test lines of MIR (before) lines of MIR (w/ panic stubbing)
Option<usize>::unwrap() 292 217
Option<usize>::expect() 8664 225
Result<usize, usize>::unwrap() 20461 20374
Result<usize, usize>::expect() 20463 20376
panic!("hello!") 8498 41
panic!("hello {x}!") (x being a &'static str) 8502 45

The lack of significant improvement on the two Result functions is elaborated on in #4168 and will take additional consideration to fix.

Resolves #2835

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

@AlexanderPortland
AlexanderPortland requested a review from a team as a code owner June 19, 2025 21:08
@github-actions github-actions Bot added the Z-EndToEndBenchCI Tag a PR to run benchmark CI label Jun 19, 2025

@zhassan-aws zhassan-aws 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.

Thanks! Can we add a script-based test that checks the size of the MIR to ensure the stub is properly applied?

@AlexanderPortland

Copy link
Copy Markdown
Contributor Author

@zhassan-aws I just added a test that runs kani on three of the panic!() examples from above and checks to make sure that the emitted MIR properly contains our panic stub.

to ensure that each test is stubbed and not just any of the 3

@zhassan-aws zhassan-aws 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.

Thanks!

@AlexanderPortland
AlexanderPortland added this pull request to the merge queue Jun 23, 2025
Merged via the queue into model-checking:main with commit 314d459 Jun 23, 2025
@AlexanderPortland
AlexanderPortland deleted the panic-stubbing branch June 23, 2025 20:00
github-merge-queue Bot pushed a commit that referenced this pull request Jul 3, 2025
Auto generated release notes:

## What's Changed
* Edit quantifiers' documentation. by @thanhnguyen-aws in
#4142
* Fix the bug of using multiple hidden variables for the prev of the
same Expr by @thanhnguyen-aws in
#4150
* Remove `assess` subcommand by @carolynzech in
#4111
* Optimize goto binary exporting in `cprover_bindings` by
@AlexanderPortland in #4148
* Add the option to generate performance flamegraphs by
@AlexanderPortland in #4138
* Fix the bug: Static union values can panic Kani by @thanhnguyen-aws in
#4112
* Update toolchain to 2025-06-13 by @carolynzech in
#4152
* Automatic cargo update to 2025-06-16 by @github-actions in
#4156
* Major-version update cargo dependencies by @tautschnig in
#4158
* Upgrade Rust toolchain to 2025-06-16 by @tautschnig in
#4157
* Bump tests/perf/s2n-quic from `3129ad5` to `c6e694e` by @dependabot in
#4160
* Bump tests/perf/s2n-quic from `c6e694e` to `b1b5bf8` by @dependabot in
#4164
* Upgrade Rust toolchain to 2025-06-17 by @tautschnig in
#4163
* Automatic cargo update to 2025-06-23 by @github-actions in
#4172
* Stub panics during MIR transformation by @AlexanderPortland in
#4169
* Bump tests/perf/s2n-quic from `b1b5bf8` to `32ba87d` by @dependabot in
#4175
* Handle enums with zero or one variants by @zhassan-aws in
#4171
* Introduce compiler timing script & CI job by @AlexanderPortland in
#4154
* Upgrade Rust toolchain to 2025-06-18 by @tautschnig in
#4166
* Cache dependencies for CI jobs by @AlexanderPortland in
#4181
* Autoharness: Derive `Arbitrary` for structs and enums by @carolynzech
in #4167
* Upgrade Rust toolchain to 2025-06-27 by @tautschnig in
#4182
* Include wget in dependencies by @zhassan-aws in
#4183
* Automatic cargo update to 2025-06-30 by @github-actions in
#4186
* Add support for loop assigns in loop contracts by @thanhnguyen-aws in
#4174
* Upgrade toolchain to 06/30 by @carolynzech in
#4188
* Optimize reachability with non-mutating global passes by
@AlexanderPortland in #4177
* Bump tests/perf/s2n-quic from `32ba87d` to `b8f8cca` by @dependabot in
#4190
* Bump ncipollo/release-action from 1.16.0 to 1.18.0 by @dependabot in
#4191
* Upgrade toolchain to 07/02 by @carolynzech in
#4195
* Automatic Derivation Fixes by @carolynzech in
#4194


**Full Changelog**:
kani-0.63.0...kani-0.64.0

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
feliperodri pushed a commit to tautschnig/kani that referenced this pull request Nov 20, 2026
…ecking#5203)

## 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.

```sh
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 model-checking#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.

---------

Co-authored-by: Mingzhi Gao <gmz563527968@126.com>
Co-authored-by: v3risec <272978695+v3risec@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bloated MIR due to panic code in standard library

2 participants