Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intptrcast: Never allocate two objects directly adjecent #1930

Merged
merged 2 commits into from
Dec 6, 2021

Conversation

tavianator
Copy link
Contributor

When two objects directly follow each other in memory, what is the
provenance of an integer cast to a pointer that points directly between
them? For a zero-size region, it could point into the end of the first
object, or the start of the second.

We can avoid answering this difficult question by simply never
allocating two objects directly beside each other. This fixes some of
the false positives from #1866.

src/intptrcast.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

RalfJung commented Dec 3, 2021

Thanks for the MR!

I am not very happy about this work-around, since it is just that -- it doesn't actually solve the problem, it just makes it less likely to occur in practice. But that is probably still better than the status quo. I opened rust-lang/unsafe-code-guidelines#313 to track the fundamental problem that causes this.

When two objects directly follow each other in memory, what is the
provenance of an integer cast to a pointer that points directly between
them?  For a zero-size region, it could point into the end of the first
object, or the start of the second.

We can avoid answering this difficult question by simply never
allocating two objects directly beside each other.  This fixes some of
the false positives from rust-lang#1866.
@tavianator
Copy link
Contributor Author

I am not very happy about this work-around, since it is just that -- it doesn't actually solve the problem, it just makes it less likely to occur in practice.

Agreed, although depending on what “the problem“ is, I expect it's either very difficult or impossible to solve :). I'm not sure there's a consistent semantics at all for a language with both pointer provenance and pointer<->integer casts. Though I haven't been following the C provenance working group stuff.

@RalfJung
Copy link
Member

RalfJung commented Dec 6, 2021

I'm not sure there's a consistent semantics at all for a language with both pointer provenance and pointer<->integer casts.

I sure hope there is, because otherwise I have to either remove int-to-ptr casts from Rust or live with the fact that Rust is internally inconsistent and fundamentally impossible to get right. ;) But if there is a counterexample that demonstrates that no such semantics can exist, that would sure be very interesting.

src/intptrcast.rs Outdated Show resolved Hide resolved
src/intptrcast.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

RalfJung commented Dec 6, 2021

@bors r+

Thanks again for the fix. :)

@bors
Copy link
Contributor

bors commented Dec 6, 2021

📌 Commit 6a98c64 has been approved by RalfJung

@bors
Copy link
Contributor

bors commented Dec 6, 2021

⌛ Testing commit 6a98c64 with merge 81e59e6...

@bors
Copy link
Contributor

bors commented Dec 6, 2021

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing 81e59e6 to master...

@bors bors merged commit 81e59e6 into rust-lang:master Dec 6, 2021
@tavianator tavianator deleted the avoid-adjacent-allocations branch December 6, 2021 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants