rust-lang / rust-lang/reference
The reference does not allow in-bounds reads of "partially dangling" pointers, whereas Miri does.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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