Specify bit validity and padding of some types - #1392
Conversation
Specify the bit validity and padding of the primitive numeric types, bool, char, and pointer and reference types. Closes rust-lang#1291
Co-authored-by: Ralf Jung <post@ralfj.de>
left a comment
There was a problem hiding this comment.
LGTM! I'm confident this is covered by the FCP in rust-lang/unsafe-code-guidelines#439.
commented
Aug 21, 2023
|
Thanks for all of the discussion on this, @RalfJung! |
commented
Aug 24, 2023
|
Is merging this blocked on anything? |
commented
Aug 24, 2023
|
I don't think so, but review capacity on reference PRs is very low. |
commented
Aug 24, 2023
|
Oh does this require more than just your LGTM? |
commented
Aug 24, 2023
|
With the reference I am never sure.^^ @ehuss is our main editor here. |
|
Can you say why this is placed in the type-layout chapter? In general, that chapter is focused on the size and alignment of types, and not what constitutes a valid type. Valid bit patterns for types are generally documented in their corresponding type pages (like bool specifies that only 0 and 1 are valid bytes). |
| pointer type may not be a valid `u8` (the semantics of transmuting a reference or | ||
| pointer to a non-pointer type is currently undecided). |
There was a problem hiding this comment.
This "may" seems confusing to me. When is it valid or not valid?
commented
Aug 28, 2023
I can do that, it'd just require somewhat more text since the second paragraph (about going from |
commented
Aug 28, 2023
|
Yea, I'm fine with duplicating it. For the bool case, that could also be worded a little different since "every byte" is a little confusing since there is only one byte. Thanks! |
commented
Aug 28, 2023
|
Where do you think this text should go? On the page for pointer types?
Given that it would be lacking the context of the paragraph it's currently in, maybe we could rewrite it to something like the following, in its own section:
|
commented
Aug 28, 2023
|
Okay, I updated to this text in
|
commented
Aug 29, 2023
|
One follow-up thought inspired by google/zerocopy#294: @RalfJung and @ehuss, would you be on board with me also adding the following to
|
Co-authored-by: Ralf Jung <post@ralfj.de>
commented
Sep 7, 2023
via email
|
Yeah this is fine for me, it just will affect people working with extern types (but those are still unstable anyway).
|
|
|
||
| Despite pointers and references being similar to `usize`s in the machine code emitted on most platforms, | ||
| the semantics of transmuting a reference or pointer type to a non-pointer type is currently undecided. | ||
| Thus, it may not be valid to transmute a pointer or reference type, `P`, to a `[u8; size_of::<P>()]`. |
There was a problem hiding this comment.
@RalfJung For my own edification, how does this interact with pointer-to-integer as casts? I can imagine a few possibilities (not mutually exclusive):
ascasts are sound, but transmutations are not necessarily sound (even though you can emulate a transmutation by doing anascast followed by a transmutation fromusize)- the first bullet is true and pointer-to-integer
ascasts probably shouldn't have been allowed in safe Rust, but it's too late
There was a problem hiding this comment.
The current working model is that as casts are special:
ptr as usizeis like https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.expose_addrusize as ptris like https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html
In contrast:
- "transmute ptr to usize" is like https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.addr
- "transmute usize to ptr" is like https://doc.rust-lang.org/nightly/std/ptr/fn.invalid.html
Here, "transmute" refers to transmute but also any other way to re-interpret bytes at a different type, such as type punning through union field accesses and raw pointer casts.
All of these operations are safe and sound, but as their docs show, they behave quite differently and that can affect the soundness of later code that uses the values they produce.
Note that this is a prototype model, which has some unresolved problems and which is very much not stabilized.
There was a problem hiding this comment.
Okay that all makes sense, thanks!
Note that this is a prototype model, which has some unresolved problems and which is very much not stabilized.
I always assume that of everything in this space unless documented otherwise :)
Specify the bit validity and padding of the primitive numeric types, bool, char, and pointer and reference types.
Closes #1291