Repository navigation
Conversation
Reading an out-of-range discriminant for an enum whose layout is an aggregate (not a scalar or scalar pair) was reported as valid. The validity requirement was derived only from the type's top-level scalar, which an aggregate layout lacks, so the discriminant tag went unchecked. Build the tag's range check directly from the tag scalar in the VariantsShape::Multiple arm, at the tag's offset. Scalar and scalar-pair enums already get this from their top-level scalar, so skip them there. Resolves model-checking#5023
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The aggregate-layout enum validity-checking change has no active review issues.
No open issues 🎉
What changed in this PR
Fixes #5023 by extending Kani’s -Z valid-value-checks to detect invalid discriminants in aggregate-layout enums, closing a gap in enum validity checking.
Changes:
- Check the tag scalar’s valid range at its layout offset for aggregate-layout enums.
- Add regression coverage for out-of-range tags, the first invalid boundary value, and valid tags.
| File | Description |
|---|---|
tests/expected/valid-value-checks/aggregate_enum.rs |
Adds two invalid-tag cases and a valid-tag control. |
tests/expected/valid-value-checks/aggregate_enum.expected |
Records expected validity failures and successful verification. |
kani-compiler/src/kani_middle/transform/check_values.rs |
Adds tag validity requirements for aggregate-layout enums. |
Review effort: Balanced
feliperodri
reviewed
Nov 7, 2026
Member
There was a problem hiding this comment.
The fix goes where the scalar tag is already checked, and the test covers out-of-range and one-past-the-end tags.
Before merge:
- The comment says the tag is the layout's sole field. Please assert it, or cite where rustc guarantees it.
- Does the test cover an aggregate enum nested inside a struct? That's where
current_offset + offsets[tag_field]matters. If not, please add one.
Member
|
ping @kasimte |
feliperodri
added a commit
to feliperodri/kani
that referenced
this pull request
Nov 25, 2026
…odel-checking#5205) `-Z valid-value-checks` reports UB in the wrong place for byte-level writes, and misses some UB altogether. Writing bytes that aren't a valid value of the memory's type is not UB; producing or reading such a value is (Rust Reference, "Behavior considered undefined"; the `ptr::write_bytes` docs). Before this change: - `write_bytes` of an invalid pattern was reported at the write, even when nothing read it, and even with `count == 0` (model-checking#5190). - `copy_nonoverlapping` and mutable casts to other pointee types were reported as unsupported, so every harness that reached them failed (model-checking#3762). - `ptr::copy` of invalid bytes into a `char` was not checked at all (a missed UB that Miri reports). - A store through a `*const u8` round-trip or a transmuted `&mut u8` that wrote an invalid `bool` was not reported (model-checking#5021). This change tracks one nondeterministically chosen byte, as `-Z uninit-checks` does, and records whether its last write was untyped: copies, `write_bytes`, volatile copies and sets, `typed_swap_nonoverlapping`, and stores through a pointer to a type without validity requirements mark it; typed stores of restricted types clear it. A read of a restricted type through a dereference, or of a local whose address is taken, checks validity when the bytes it reads are marked. Types whose validity Kani can't express yet (e.g. `Option<char>`) are reported as unsupported only when the bytes are marked, instead of on every copy. The model is in `library/kani_core/src/valid_value.rs`. It also fixes model-checking#5194: the pass inserted only the first check of each basic block, because the instrumentation loop used the block count from before it started splitting blocks. That bug hid model-checking#5021's reproducers, and fixing it is part of what makes the new checks effective. And it stops reporting `ptr::write_unaligned::<bool>` as unsupported: a struct whose layout is a single scalar, like `Unaligned<bool>`, listed that scalar's requirement twice, so its validity compared unequal to `bool`'s. Effect on verify-rust-std's Challenge 2 harnesses (PR model-checking#649, filters `intrinsics`, `ptr`, `mem` and `copy_from_slice`; 721 harnesses; with `--object-bits 12` as in its CI, on Kani c5e65fd with model-checking#5057 and this change ported): | | Verified | Failed | Wall time | |---|---|---|---| | Kani c5e65fd, `-Z valid-value-checks` | 652 | 69 | 33 min | | With this change, `-Z valid-value-checks` | 717 | 4 | 51 min | | With this change, no flag | 721 | 0 | 27 min | Most of the 65 harnesses that now verify failed on unsupported `copy_nonoverlapping` or mutable casts. The four that still fail (`check_transmute{,_unchecked}_{ref_address,slice_metadata}`) take a reference to `PointerGenerator` memory that may be uninitialized; that's UB under Miri's recursive validation and needs `kani::assume(arb.is_initialized)` in the harnesses. Stock Kani looks cheaper because many of its harnesses stop at the first unsupported check. Where I'd like input: - Reads of closure captures (`env.field`, `(*env).field`) are not checked inside the closure's body. Instead, borrowing or moving a closure value is checked, which is how every call reaches it, so quantifier predicates don't contain checks. Predicates that read restricted types through a captured reference are still rejected (model-checking#5193). - Forming a reference to bytes that may be untyped is checked, as Kani already does for `&*ptr`. That's stricter than Miri's default and matches `-Zmiri-recursive-validation`. Stores of restricted types through a pointer mark the bytes too, because the pointer may alias memory of another restricted type; that adds checks on reads after such stores. - Tracking one nondeterministic byte means each run checks one byte per path. That matches `-Z uninit-checks`, and it's what keeps the cost to about 1.9x of an unchecked run on Challenge 2. - The validity and initialization models don't instrument each other (`is_instrumentation_model` and the uninit pass's `SKIPPED_ITEMS`); without that, the quantifier tests didn't terminate. Not fixed here: slice element reads (model-checking#5022, PR model-checking#5073) and aggregate-layout enum discriminants (model-checking#5023, PR model-checking#5069). Resolves model-checking#3762, model-checking#5021, model-checking#5190, model-checking#5194. Testing: new `tests/expected/valid-value-checks/copies.rs` (13 harnesses, Miri-checked: 4 report UB, 9 verify) and `untyped_store.rs` (model-checking#5021), updated `write_bytes.rs`, `write_invalid.rs` and `custom_niche.rs`, `tests/kani/ValidValues/write_unaligned_scalar_struct.rs`, and a UI test for the quantifier rejection. On the rebased branch: the `expected`, `ui`, `kani` and `cargo-kani` suites pass, as do fmt and clippy. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #5023.
-Z valid-value-checksdid not report an invalid discriminant when an enum's layout is an aggregate rather than a scalar or scalar pair.The fix reads the tag scalar's valid range in the
VariantsShape::Multiplearm and checks it at the tag's offset. Aggregate-layout enums now get the same validity treatment as scalar and scalar-pair enums, including the mutable-cast restriction.Testing
tests/expected/valid-value-checks/aggregate_enum.rs: a raw-pointer read with an out-of-range tag failsInvalid value of type `G`, a tag one past the valid range fails too, and valid tags verify.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.