-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Clarify offset safety rules #44688
Comments
One way of looking at this might be: does "allocated object" mean "allocated by any |
I think the precise definition depends on memory model work, but my intuitive model is "thing you got from malloc". |
Note that stack allocations are fine as well |
I'm curious about this definition because I can't imagine how Rust or LLVM would formalize the notion of an allocation. Does anybody know what LLVM |
@joshlf You're looking for this part of trans: rust/src/librustc_trans/intrinsic.rs Lines 198 to 202 in 0701b37
So basically, the invariants of |
Awesome, thanks so much @jonas-schievink ! I'm still a little unsure of what the semantics are, though. Quoting from that link:
I'm not sure on how LLVM defines "allocated object." Do you have any insight on this? |
I don't, sorry. Not really an LLVM expert. |
@rust-lang/lang and/or @rust-lang/compiler , some guidance here on the wording would be great. We could always just do what we do with aliasing and link to that LLVM page, but that's not as great. |
Someone who knows more about LLVM could probably answer this better, but I'd expect that LLVM is using "allocated object" to mean that the starting and resulting pointer must both point to memory that can be legally dereferenced (without e.g. segfaulting). I'd imagine LLVM is using this additional knowledge to know that it can safely prefetch the memory behind the pointer. Edit: I was wrong! See below. |
No, I don't think the important part is that the result can be accessed right away, but rather that the pointer doesn't point at something seemingly unrelated. I believe this condition is designed to avoid having to treat every pointer offset as "welp, now it can point to anything, give up on all alias analysis". For instance, if I modify an element of a Vec, it sure as heck shouldn't invalidate the values of my local stack variables! But without |
Yeah @gankro is right. Code like this is UB:
(even if the |
That makes sense-- thanks for the clarification! |
Revisiting this as part of a triage run. There is a link added to the docs which links you to this part. So it should be fine to resolve this issue and if anything is lacking, a new issue might be a better idea than this being buried deep into the issue tracker. |
According to the nightly
offset
docs, in order to avoid UB, "both the starting and resulting pointer must be either in bounds or one byte past the end of an allocated object."However, it's not clear to me what constitutes an "allocated object." This confusion caused this issue about whether it's safe to use
offset
in the implementation of an allocator.cc @kennytm @gankro @japaric @ezrosent
The text was updated successfully, but these errors were encountered: