miri: make read_discriminant UB when the tag is not in the validity range of the tag field#153408
Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom Mar 11, 2026
Merged
Conversation
Collaborator
Collaborator
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
4153e62 to
b4b2429
Compare
Member
Author
|
r? @oli-obk |
Collaborator
|
|
This comment has been minimized.
This comment has been minimized.
f2ff029 to
afff350
Compare
This comment has been minimized.
This comment has been minimized.
…ange of the tag field
afff350 to
b840338
Compare
Contributor
rust-bors bot
pushed a commit
that referenced
this pull request
Mar 11, 2026
…uwer Rollup of 4 pull requests Successful merges: - #153072 (Allow merging all libcore/alloc doctests into a single binary) - #153408 (miri: make read_discriminant UB when the tag is not in the validity range of the tag field) - #153674 (Detect inherent method behind deref being shadowed by trait method) - #153689 (Eliminate `QueryLatchInfo`.)
github-actions bot
pushed a commit
to rust-lang/miri
that referenced
this pull request
Mar 12, 2026
…uwer Rollup of 4 pull requests Successful merges: - rust-lang/rust#153072 (Allow merging all libcore/alloc doctests into a single binary) - rust-lang/rust#153408 (miri: make read_discriminant UB when the tag is not in the validity range of the tag field) - rust-lang/rust#153674 (Detect inherent method behind deref being shadowed by trait method) - rust-lang/rust#153689 (Eliminate `QueryLatchInfo`.)
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.
Arguably, reading an enum discriminant is an operation that uses the "type" of the discriminant field -- and therefore it should fail when the value in that field isn't valid at that type. Therefore, code like this should be UB:
However, Miri currently sees no UB here. (MiniRust does see UB.) This is because we never actually check whether the tag we read is in the validity range for its field. So let's add such a check, and a corresponding test.
In fact, we have to do this check, since the codegen backend adds range metadata on the discriminant load, as can be seen in this example. In other words, the above code has UB in LLVM IR but not in Miri, which is a critical Miri bug.