Skip to content

Can shared references and raw pointers concurrently access a memory region? #463

Open
@joshlf

Description

@joshlf

The ptr module docs say:

The result of casting a reference to a pointer is valid for as long as the underlying object is live and no reference (just raw pointers) is used to access the same memory. That is, reference and pointer accesses cannot be interleaved.

This seems to imply that accessing a *const T while a &T is live is illegal, which is surprisingly restrictive. The following code seems intuitively fine, and is accepted by Miri, but seems to violate this rule:

fn main() {
    let x = 0usize;
    let x_ref = &x;
    let x_copy = unsafe { core::ptr::read(&x as *const usize) };
    println!("{x}, {x_ref}, {x_copy}");
}

Am I misinterpreting this text? Is this intended to be unsound and Miri just needs to be taught to catch it? Should the text be relaxed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions