Skip to content

Fix projections after a subslice at mir-opt-level 2 - #4973

Merged
feliperodri merged 4 commits into
model-checking:mainfrom
srivatsansamraj:subslice-projection
Nov 7, 2026
Merged

feliperodri merged 4 commits into
model-checking:mainfrom
srivatsansamraj:subslice-projection

Conversation

@srivatsansamraj

@srivatsansamraj srivatsansamraj commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

In the Subslice arm for a slice base, codegen_projection returned the new fat pointer as the place, typed *const [T], while MIR types that place [T]. MIR built from source only ever takes a reference to a subslice, so the wrong type went unnoticed. Optimised runtime MIR can project further: at -Zmir-opt-level=2, ReferencePropagation merges tail = &x[1..] into its use tail[0], and GVN turns that into (*x)[1:][0 of 1]. The next projection then sees a raw pointer and reaches unreachable! in the ConstantIndex, Index or Subslice arm (#2682). rustc uses mir-opt-level 2 whenever opt-level is above 0, so a crate with opt-level = 1 in its dev profile reaches this with plain cargo kani.

The subslice place is now the new data pointer, typed [T], and the fat pointer stays in fat_ptr_goto_expr for the length: the representation Deref already produces for a slice. This also fixes a crash at every opt level: a reference to a subslice of an unsized struct's trailing slice field took the address of the fat-pointer struct (Can't take address of Expr).

Tests:

Both crash on main and verify with this change; a copy with every expected value off by one fails every assertion. SubSlice/subslice1.rs and subslice2.rs also crash at -Zmir-opt-level=2 on main and pass with it.

Resolves #2682

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

@srivatsansamraj

Copy link
Copy Markdown
Contributor Author

@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun for review. When rustc optimises harder (-Zmir-opt-level=2, which it uses for any build with opt-level above 0), it can index straight into a subslice, as in x[1..][0]. Kani gave the subslice a pointer type where MIR has a slice, and the next step crashed the compiler. Kani now builds the subslice the way it builds a slice behind a reference, which also fixes a crash at any level for a subslice of a struct's trailing slice field. The standard-library runs never hit this, so their numbers don't change.

@srivatsansamraj
srivatsansamraj requested review from a team as code owners October 5, 2026 21:09
@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 5, 2026
The `Subslice` arm of `codegen_projection` returned the subslice's fat
pointer as the place, typed `*const [T]`, so a projection that optimized
MIR applies after it, such as `(*x)[1:][0 of 1]` at `-Zmir-opt-level=2`,
reached `unreachable!` (model-checking#2682). The place is now the subslice's data
pointer typed `[T]`, with the fat pointer kept in `fat_ptr_goto_expr` for
the length as after a `Deref` of a slice, which also fixes a reference to
a subslice of an unsized struct's trailing slice field.
@feliperodri feliperodri self-assigned this Oct 22, 2026
@feliperodri

Copy link
Copy Markdown
Member

@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun any feedback on this and the subsequent PRs from @srivatsansamraj?

@Tianshu-Huang Tianshu-Huang 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.

LGTM

@acearyanarun

Copy link
Copy Markdown
Contributor

I noticed a couple of things worth checking:

With the newer JSON exporter on main, the vacuity cover appears to be excluded from failed_properties. This could result in a harness reporting Failure without exposing the vacuity reason. Could we make sure that information is preserved?

In call_cbmc.rs, setting failed_properties to Other unconditionally could overwrite an existing failure classification. It may be safer to update it only when the classification is None.

@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed Z-Autoharness Issue related to autoharness subcommand labels Nov 6, 2026
@feliperodri feliperodri added this to the Autoharness milestone Nov 6, 2026
@feliperodri feliperodri removed the Z-Autoharness Issue related to autoharness subcommand label Nov 6, 2026
@feliperodri feliperodri removed this from the Autoharness milestone Nov 6, 2026
@feliperodri
feliperodri requested a balanced review from Copilot November 6, 2026 18:43

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

2 open issues
What changed in this PR

Fixes Kani codegen for subslice projections in optimized MIR so further projections after a Subslice don’t hit unreachable!, and avoids a crash when taking references to subslices of DST trailing slice fields.

Changes:

  • Adjust codegen_projection for ProjectionElem::Subslice so the place becomes the subslice’s data pointer typed as [T], keeping the fat pointer only for length.
  • Add regression tests for projections after subslices at -Zmir-opt-level=2 and for subslicing a DST trailing slice field.
File Description
tests/​kani/​SubSlice/​subslice_dst_field.rs Adds a regression test for taking a reference to a subslice of a DST trailing slice field.
tests/​kani/​SubSlice/​projection_after_subslice.rs Adds regression tests for further projections after Subslice under -Zmir-opt-level=2.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​place.rs Changes subslice projection codegen to return a slice place (data ptr + separate fat ptr for len) to match MIR behavior.

Review effort: Lite

@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 subslice fix is right, and the fat pointer is kept for the length, as Deref does.

Before merge:

  1. Copilot says subslice_dst_field.rs matches w.data by value, which may be invalid for an unsized field. Please confirm the test compiles, or match on &w.data.
  2. A comment from acearyanarun on this PR is about the vacuity cover in the JSON exporter, which belongs to #4978. Please ask them to move it there.
  3. Six jobs are cancelled. Please re-run them.

@feliperodri
feliperodri enabled auto-merge November 7, 2026 17:58
@feliperodri
feliperodri added this pull request to the merge queue Nov 7, 2026
Merged via the queue into model-checking:main with commit 94253e3 Nov 7, 2026
23 of 26 checks passed
CYJ904 added a commit to CYJ904/kani that referenced this pull request Nov 9, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed 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.

ICE: entered unreachable code: Only expected constant index for arrays and slices: also found it for: *mut [isize]

5 participants