Repository navigation
Fix projections after a subslice at mir-opt-level 2 - #4973
Conversation
|
@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun for review. When rustc optimises harder ( |
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.
073118d to
617d622
Compare
|
@CYJ904 @Tianshu-Huang @wodex1nhaoIeng @acearyanarun any feedback on this and the subsequent PRs from @srivatsansamraj? |
…slice data in the test
|
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. |
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
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_projectionforProjectionElem::Subsliceso 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=2and 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
left a comment
There was a problem hiding this comment.
The subslice fix is right, and the fat pointer is kept for the length, as Deref does.
Before merge:
- Copilot says
subslice_dst_field.rsmatchesw.databy value, which may be invalid for an unsized field. Please confirm the test compiles, or match on&w.data. - 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.
- Six jobs are cancelled. Please re-run them.
94253e3
…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.


In the
Subslicearm for a slice base,codegen_projectionreturned 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 mergestail = &x[1..]into its usetail[0], and GVN turns that into(*x)[1:][0 of 1]. The next projection then sees a raw pointer and reachesunreachable!in theConstantIndex,IndexorSubslicearm (#2682). rustc uses mir-opt-level 2 wheneveropt-levelis above 0, so a crate withopt-level = 1in its dev profile reaches this with plaincargo kani.The subslice place is now the new data pointer, typed
[T], and the fat pointer stays infat_ptr_goto_exprfor the length: the representationDerefalready 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:
SubSlice/projection_after_subslice.rs(-Zmir-opt-level=2):ConstantIndexfrom both ends,IndexandSubsliceafter aSubslice, checked against a nondeterministic array, and the program from ICE:entered unreachable code: Only expected constant index for arrays and slices: also found it for:*mut [isize]#2682.SubSlice/subslice_dst_field.rs: the field case.Both crash on
mainand verify with this change; a copy with every expected value off by one fails every assertion.SubSlice/subslice1.rsandsubslice2.rsalso crash at-Zmir-opt-level=2onmainand 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.