Skip to content

Stacked Borrows: too much raw pointer retagging hurts #1608

Closed
rust-lang/rust
#78597
@RalfJung

Description

@RalfJung

Consider the following example (due to @ssomers):

#![feature(raw_ref_macros)]
use std::ptr;

struct Part {
    _lame: i32,
}

#[repr(C)]
struct Whole {
    part: Part,
    extra: i32,
}

fn main() {
    let it = Box::new(Whole { part: Part { _lame: 0 }, extra: 42 });
    let whole = ptr::raw_mut!(*Box::leak(it));
    let part = unsafe { ptr::raw_mut!((*whole).part) };
    let typed = unsafe { &mut *(part as *mut Whole) };
    assert!(typed.extra == 42);
}

Arguably, this is using raw_mut! correctly, but Miri flags this code as UB when using -Zmiri-track-raw-pointers. The problem is that at ptr::raw_mut!((*whole).part), a raw retag happens, which generates a fresh tag for part that is only valid for the memory of the part field, not for the extra field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-aliasingArea: This affects the aliasing model (Stacked/Tree Borrows)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions