Miri not erroring on aliased mutable pointer: sound or UB?

Here is the code in question:

fn use_mut(r: &mut i32) {
    *r += 1;
    let _ = &mut *r;

fn main() {
    let r = &mut 5;
    let p = r as *mut _;
    use_mut(unsafe { &mut *p });

It seems like a pretty textbook case of UB - there are multiple mutable references to the data in the second call to use_mut. However, Miri does not report any errors, even with -Zmiri-track-raw-pointers enabled.

The strange thing is that if I write &mut *r in the main function (for example if I call use_mut the first time with use_mut(&mut *r)), Miri will detect UB (including without -Zmiri-track-raw-pointers), which really confuses me since I was under the impression that passing a mutable reference to a function will automatically reborrow it.

Am I just not understanding Stacked Borrows or is this a Miri bug?


I believe this is a bug, and it should be reported if it isn't already reported.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.