Skip to content

What does -Zmiri-disable-validation mean for pointers from transmutes (now?) #2163

Closed
rust-lang/rust
#98860
@saethlin

Description

@saethlin

The README says

-Zmiri-disable-validation disables enforcing validity invariants, which are enforced by default.

Which to me suggests that I shouldn't see errors about how "0xsomething is not a valid pointer", but I do. This program:

fn main() {
    let x = 0u8;
    let ptr = &x as *const u8;
    let val = ptr as usize;
    unsafe {
        let ptr = std::mem::transmute::<usize, *const u8>(val);
        dbg!(*ptr);
    }
}

produces this error, with -Zmiri-disable-stacked-borrows -Zmiri-disable-validation:

error: Undefined Behavior: dereferencing pointer failed: 0x24881 is not a valid pointer
 --> src/main.rs:7:9
  |
7 |         dbg!(*ptr);
  |         ^^^^^^^^^^ dereferencing pointer failed: 0x24881 is not a valid pointer
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
          
  = note: inside `main` at rustc_src/src/macros.rs:315:13
  = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error

So for me the problem here is that

  1. The documentation seems wrong, I disabled validity checks so why is Miri complaining about validity?
  2. This is exactly the same error message that Miri emits for a wild pointer dereference; if I transmute any old usize instead of val I see the same error. It should be possible to distinguish between "that's an invalid pointer" and "there's no memory at that address". When I as a somewhat expert Miri user see that error I immediately guess that there is a transmute happening, but when I shared that error message with people some assumed it was a wild pointer access. Perhaps the problem is that the validity issue here has to do with provenance not the address, but Miri prints the address in the diagnostic, absent the lack of provenance which is what makes this invalid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: affects documentationA-validationArea: This affects enforcing the validity invariant, and related UB checkingC-enhancementCategory: a PR with an enhancement or an issue tracking an accepted enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions