rust-lang / rust-lang/reference

The reference does not allow in-bounds reads of "partially dangling" pointers, whereas Miri does.

Open
#2,356 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

Miri accepts this code, which prints 42:

#[repr(C)]
struct X {
    a: usize,
}

#[repr(C)]
struct Y {
    a: usize,
    b: usize,
}

fn main() {
    unsafe {
        let x = X { a: 42 };
        let py = &raw const x as *const Y;
        let Y { a, .. } = *py;
        println!("{a}");
    }
}

On the other hand, the reference defines a dangling pointer as follows:

A reference/pointer is “dangling” if not all of the bytes it points to are part of the same live allocation (so in particular they all have to be part of some allocation).

And mentions as undefined behavior:

Accessing (loading from or storing to) a place that is dangling

Clearly, py is dangling by this definition, and clearly, the line let Y { a, .. } = *py; loads from py. So according to the reference, this should be UB. The reference does not have any notion of a "partially dangling" pointer that it's okay to read part of. Who is right: the reference or miri?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust Reference sections on dangling pointers, pointed-to bytes, allocations, and undefined behavior, then reproduce the supplied example in Miri. Compare the Reference wording with Miri's behavior and determine the documentation change needed to resolve whether partially dangling pointers may be read; done means the semantics and example are described consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.