Repository navigation
Ignore min_length of a constant index into an array - #4974
Conversation
|
@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun for review. At the same optimisation level, rustc can rewrite an array index that is always out of bounds into a form that broke an assertion inside Kani, so the compiler aborted. Kani now ignores that field the way rustc's own codegen does, and the out-of-bounds check is reported as an ordinary failure. The standard-library runs never hit this, so their numbers don't change. |
6f24d88 to
7af26c8
Compare
Tianshu-Huang
left a comment
There was a problem hiding this comment.
LGTM, just one comment update
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
1 open issue
What changed in this PR
Fixes Kani codegen for MIR ConstantIndex on arrays by ignoring min_length (which can become inconsistent after GVN at -Zmir-opt-level=2), preventing compile-time aborts and ensuring out-of-bounds checks are reported as expected.
Changes:
- Update array
ConstantIndexcodegen to ignoremin_lengthand drop the now-unused parameter. - Add a regression test for issue #2732 and its expected output at
-Zmir-opt-level=2. - Keep slice handling consistent with rustc by not relying on
min_length.
| File | Description |
|---|---|
| tests/expected/issue-2732/issue_2732.rs | Adds a regression test harness covering always-failing and unreachable out-of-bounds indexing. |
| tests/expected/issue-2732/issue_2732.expected | Captures expected Kani results (FAILURE and UNREACHABLE) for the new regression test. |
| kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs | Ignores min_length for array ConstantIndex and removes the parameter from codegen. |
Review effort: Lite
feliperodri
left a comment
There was a problem hiding this comment.
Dropping the length >= min_length assert follows rustc's codegen, and the explanation of the always-failing bounds check is clear.
Before merge:
length - offsetcan panic in the compiler whenfrom_endis set andoffset > length, on a path behind an always-failing bounds check. Usechecked_sub(orsaturating_sub) and handle the unreachable case explicitly.- Nine jobs are cancelled. Please re-run them.
GVN rewrites an `Index` with a known value into `ConstantIndex { offset,
min_length: offset + 1 }` without comparing it with the array length, so
for `C[10]` on a 5-element array the `length >= min_length` assertion in
`codegen_constant_index` aborted Kani at `-Zmir-opt-level=2` (model-checking#2732).
Like rustc's codegen, Kani now ignores `min_length` for arrays and drops
the unused parameter; an out-of-bounds index that is reached fails CBMC's
array bounds check.
With `min_length` ignored, an offset from the end can exceed the array length on the same paths that are never taken, and `length - offset` would then panic in the compiler. A wrapping subtraction gives an out-of-bounds index instead, which CBMC's bounds check reports if the access is reached.
a5244bd to
3d531c8
Compare
There was a problem hiding this comment.
Thanks. wrapping_sub and the dropped TODO address my points, and the issue's reproducer is covered.
One gap, not blocking: wrapping_sub only runs for a from-end index larger than the array, and the test only has a forward index (C[10]). If you can, add a case that reaches it (a from-end pattern on a constant array where the optimisation makes the offset exceed the length), and confirm CBMC reports the bounds check for the wrapped value. If you can't build one, a note in the PR is enough.
The three cancelled macos-14 jobs are the runner, not this change. Approving.
|
@feliperodri I could not build one: a from-end index into an array comes from an array pattern, whose offsets the type checker keeps within the array length, and with a slice pattern on a constant array rustc removes the dead branch at |
c821f9a
…of crashing (model-checking#5119) This fixes model-checking#3596, an issue in the Autoharness milestone. Until this is fixed, users of `kani autoharness` cannot get any verdicts for a crate that contains a by-reference downcast into an enum like `enum E { A(!) }`: the compiler panics and the whole run aborts. It affects place codegen (the Downcast arm of `codegen_projection`) and fixes the crate-wide abort of model-checking#3596. ### Root cause rustc gives an enum whose variants are all uninhabited and contain only zero-sized fields (e.g. `enum E { A(!) }`) the `Variants::Empty` layout of `!`, so Kani generates a goto struct with no fields for it. MIR still projects `Downcast(A)` then `Field(0)`. The Downcast arm treated `Empty` like `Single` and passed the expression through, so the Field projection called `.member("0")` on an empty struct and hit `unreachable!` in `Expr::member` (`cprover_bindings/src/goto_program/expr.rs:914`). Codegen runs for the whole crate before any harness is checked, so under `autoharness` one such function aborts the entire run and every other function in the crate gets no result. ### Fix The Downcast arm now returns an `UnimplementedData` error for `Variants::Empty`. The existing flow turns it into an unsupported-construct check that only fails verification if it is reachable. No program that can fail is reported as successful. ### Testing 1. `tests/expected/uninhabited-enum-downcast`: the issue's reproducer (`let E::A(ref _a) = ...`) and a `match` by-reference variant. Both now fail with the unsupported-construct check instead of crashing. 2. `tests/script-based-pre/autoharness_uninhabited_enum_downcast`: an autoharness run over a crate with the crashing function plus an ordinary `ok_fn`. `ok_fn` is verified and the run completes with a summary. 3. Both tests fail (compiler panic at `expr.rs:914`) with the compiler change reverted, and pass with it. 4. By-value forms (`let E::A(a) = ...`, `match ... { E::A(a) => a }`) never reached the Downcast, since rustc lowers them to `unreachable`, and behave as before. ### Notes for reviewers 1. The unsupported message links to model-checking#3596, which this PR closes. Happy to point it at a different issue if you prefer. 2. Reading through a reference to an uninhabited type is UB under Miri's current validity rules. A possible follow-up is to report it as a safety check (like transmute to an uninhabited type in `intrinsic.rs`) rather than as unsupported, but since the opsem question about reference validity is still open, this PR keeps to the conservative fix. 3. model-checking#4973 and model-checking#4974 also touch `codegen_projection`, in different arms (subslice and constant index). They are independent of this change. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

codegen_constant_indexassertedlength >= min_lengthfor arrays, following the MIR documentation, which saysmin_lengthis the exact length for arrays. GVN does not keep that: it rewrites anIndexwith a known value intoConstantIndex { offset, min_length: offset + 1 }without comparing it with the array length. ForC[10]on a 5-element array, the bounds check becomesassert(const false)and the unreachable access after it becomesC[10 of 11], so Kani aborted at compile time (#2732). rustc uses mir-opt-level 2 wheneveropt-levelis above 0, so a crate withopt-level = 1in its dev profile reached this with plaincargo kani.rustc's codegen ignores
min_length(rustc_codegen_ssa/src/mir/place.rs), and so does Kani's slice arm. This ignores it for arrays too and removes the parameter that becomes unused.The issue's program now reports the always-failing bounds check instead of aborting. Kani emits that check as assert then assume, so the access after it is unreachable; were such an access reached, CBMC's
array_boundscheck would fail. With--cbmc-args --no-assumptions,test.array_bounds.1goes from SUCCESS to FAILURE.The new test
expected/issue-2732(-Zmir-opt-level=2) calls the issue's function directly, expecting the bounds-check failure, and behind a branch that is never taken, expecting it unreachable. It panics onmain.Resolves #2732
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.