Skip to content

miri: make read_discriminant UB when the tag is not in the validity range of the tag field#153408

Open
RalfJung wants to merge 1 commit intorust-lang:mainfrom
RalfJung:tag-read-must-be-valid
Open

miri: make read_discriminant UB when the tag is not in the validity range of the tag field#153408
RalfJung wants to merge 1 commit intorust-lang:mainfrom
RalfJung:tag-read-must-be-valid

Conversation

@RalfJung
Copy link
Member

@RalfJung RalfJung commented Mar 4, 2026

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:

fn main() {
    unsafe {
        let x = 12u8;
        let x_ptr: *const u8 = &x;
        let cast_ptr = x_ptr as *const Option<bool>;
        // Reading the discriminant should fail since the tag value is not in the valid
        // range for the tag field.
        let _val = matches!(*cast_ptr, None);
        //~^ ERROR: invalid tag
    }
}

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.

@rustbot
Copy link
Collaborator

rustbot commented Mar 4, 2026

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 4, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 4, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir
  • compiler, mir expanded to 69 candidates
  • Random selection from 16 candidates

@RalfJung RalfJung force-pushed the tag-read-must-be-valid branch from 4153e62 to b4b2429 Compare March 4, 2026 15:31
@RalfJung
Copy link
Member Author

RalfJung commented Mar 4, 2026

r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned JohnTitor Mar 4, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 4, 2026

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the tag-read-must-be-valid branch from b4b2429 to f2ff029 Compare March 4, 2026 16:22
@RalfJung RalfJung force-pushed the tag-read-must-be-valid branch from f2ff029 to afff350 Compare March 4, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants