Stream: t-lang/wg-unsafe-code-guidelines

Topic: evil examples


RalfJ (Nov 15 2018 at 15:22, on Zulip):

Today is "evil example" day: The following isn't yet considered UB by my model, but hopefully will be when I implemented barriers. It wasn't easy to tweak the example to make miri accept it, with all the retagging and validation that are in place. ;)

use std::mem;

pub fn safe(x: &i32, y: &mut i32) {} // should be "ERROR does not exist on the stack" but is not (yet)

fn main() {
    let mut x = 0;
    let xref = &mut x;
    let xraw: *mut i32 = unsafe { mem::transmute_copy(&xref) };
    let xshr = &*xref;
    // transmute fn ptr around so that we can avoid retagging
    let safe_raw: fn(x: *const i32, y: *mut i32) = unsafe {
        mem::transmute::<fn(&i32, &mut i32), _>(safe)
    };
    safe_raw(xshr, xraw);
}
nikomatsakis (Nov 15 2018 at 16:06, on Zulip):

lol

Last update: Nov 19 2019 at 17:40UTC