-
Notifications
You must be signed in to change notification settings - Fork 15
Clarify which inputs are permitted for the offset intrinsic (and the MIR binop) #38
Comments
Note that, in the model I'm thinking about, any non-null pointer with the correct alignment should be valid for zero sized types. |
That's fair enough. We also have to be careful to emit the right code for LLVM. One consequence of this could be that in codegen, when we emit GEP, we don't set "inbounds" when we are working on a ZST. |
ZSTs are weird, and I'm not sure what llvm would want from them. Having |
We should just better be sure LLVM doesn't derive UB from "you said this was inbounds, but I can prove it is not". |
yeah, agreed. |
It turns out rustc stretches the rules even more: Below https://github.com/rust-lang/rust/blob/445077963c55297ef1e196a3525723090fe80b22/src/libcore/slice/mod.rs#L777, the No idea if LLVM permits this. Maybe |
offset on what is llterally a ZST is a nop. We don't even have to lower it to LLVM if it turns out there's a problem. |
Also see @arielb1's comment at rust-lang/rust#42789 (comment) |
And another one:
|
Closing in favor of younger issues: rust-lang/unsafe-code-guidelines#93, rust-lang/unsafe-code-guidelines#299 |
The offset intrinsic compiles to LLVM getelementpointer with
inbounds
attribute, so the offset must stay in-bounds (or one-past-the-end). However, rustc currently also relies on "offset by 0" to always be legal, even if the pointer passed as input is dangling:(void*) alignment
, but the iterator will still add the length (0
) to it.So the rules for
offset
currently seem to be:The text was updated successfully, but these errors were encountered: