Skip to content

Stacked Borrows: not enough UB to justify noalias on Box #376

Closed
@RalfJung

Description

@RalfJung

The following example passes Miri, but has UB in LLVM:

unsafe fn test(mut x: Box<i32>, y: *const i32) -> i32 {
    // We will call this in a way that x and y alias.
    *x = 5;
    std::mem::forget(x);
    *y // this invalidates x, but that's fine since Box can be invalidated during the function
}

fn main() { unsafe {
    let mut v = 42;
    let ptr = &mut v as *mut i32;
    test(Box::from_raw(ptr), ptr);
} }

The reason for this is that we allow a Box pointer to be invalidated while test runs (which is necessary because the function might deallocate it), so Stacked Borrows says it is fine to use an aliasing pointer (y) while test runs as long as we don't use x again afterwards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-aliasing-modelTopic: Related to the aliasing model (e.g. Stacked/Tree Borrows)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions