miri icon indicating copy to clipboard operation
miri copied to clipboard

PlaceMention-style UB not detected when in closure

Open meithecatte opened this issue 10 months ago • 2 comments

use std::mem::transmute;

fn main() {
    let r: &&u32 = unsafe {
        let x = 42;
        transmute(&&x)
    };

    // no UB detected
    let f = || { let _ = **r; };
    f();
    
    // UB due to the inner deref
    let _ = **r;
}

Playground

Found this when prompted by @Nadrieril:

In fact the other calls to {try_}to_place in match MIR lowering are potential places where we mess things up. E.g. here, we want to add a PlaceMention to the scrutinee which we can't do if it's not captured, could that have weird consequences maybe?

meithecatte avatar Apr 09 '25 16:04 meithecatte

Now that https://github.com/rust-lang/rust/pull/138961 has landed... @Nadrieril is there some idea you have for making the two cases above behave consistently? Closure capturing and lowering is largely a black box to me, and this issue is more about what exact MIR gets built for the program than about what Miri does, I think.

The good news is that this means there also is no de-facto UB here as codegen sees the same MIR that Miri sees. I should find a way to reflect that in the labels...

RalfJung avatar Dec 19 '25 07:12 RalfJung

Actually I think that's the correct behavior per the rules in the Reference. I filed an issue to the Reference.

Nadrieril avatar Dec 19 '25 11:12 Nadrieril