Skip to content

Specify bit validity and padding of some types - #1392

Merged
ehuss merged 14 commits into
rust-lang:masterfrom
joshlf:joshlf-patch-1
Sep 9, 2023
Merged

Specify bit validity and padding of some types#1392
ehuss merged 14 commits into
rust-lang:masterfrom
joshlf:joshlf-patch-1

Conversation

@joshlf

@joshlf joshlf commented Aug 11, 2023

Copy link
Copy Markdown
Contributor

Specify the bit validity and padding of the primitive numeric types, bool, char, and pointer and reference types.

Closes #1291

Joshua Liebow-Feeser added 2 commits August 10, 2023 21:54
Specify the bit validity and padding of the primitive numeric 
types, bool, char, and pointer and reference types.

Closes rust-lang#1291
Comment thread src/type-layout.md Outdated
@scottmcm scottmcm self-assigned this Aug 14, 2023
Comment thread src/type-layout.md Outdated
Comment thread src/type-layout.md Outdated
Comment thread src/type-layout.md Outdated
Joshua Liebow-Feeser and others added 2 commits August 21, 2023 13:43
Co-authored-by: Ralf Jung <post@ralfj.de>

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I'm confident this is covered by the FCP in rust-lang/unsafe-code-guidelines#439.

@joshlf

ghost commented Aug 21, 2023

Copy link
Copy Markdown
Contributor Author

Thanks for all of the discussion on this, @RalfJung!

@joshlf

ghost commented Aug 24, 2023

Copy link
Copy Markdown
Contributor Author

Is merging this blocked on anything?

@RalfJung

ghost commented Aug 24, 2023

Copy link
Copy Markdown
Member

I don't think so, but review capacity on reference PRs is very low.

@joshlf

ghost commented Aug 24, 2023

Copy link
Copy Markdown
Contributor Author

Oh does this require more than just your LGTM?

@RalfJung

ghost commented Aug 24, 2023

Copy link
Copy Markdown
Member

With the reference I am never sure.^^ @ehuss is our main editor here.

@ehuss

ghost commented Aug 28, 2023

Copy link
Copy Markdown
Contributor

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).

Comment thread src/type-layout.md Outdated
Comment on lines +63 to +64
pointer type may not be a valid `u8` (the semantics of transmuting a reference or
pointer to a non-pointer type is currently undecided).

ghost Aug 28, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "may" seems confusing to me. When is it valid or not valid?

ghost Aug 28, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those details are undecided as of now.

ghost Aug 28, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, thanks!

@joshlf

ghost commented Aug 28, 2023

Copy link
Copy Markdown
Contributor Author

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 and 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).

I can do that, it'd just require somewhat more text since the second paragraph (about going from T to [u8; size_of::<T>()]) would need to be duplicated on the pages for bools, chars, and numeric types. If that's your preference, I'm happy to do that.

@ehuss

ghost commented Aug 28, 2023

Copy link
Copy Markdown
Contributor

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!

@joshlf

ghost commented Aug 28, 2023

Copy link
Copy Markdown
Contributor Author

Where do you think this text should go? On the page for pointer types?

A byte at any offset in a reference or pointer type may not be a valid u8 (the semantics of transmuting a reference or pointer to a non-pointer type is currently undecided).

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:

Bit validity

Despite pointers and references being similar to usizes 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>()].

@joshlf

ghost commented Aug 28, 2023

Copy link
Copy Markdown
Contributor Author

Okay, I updated to this text in pointer.md, so the PR is ready for review. Happy to edit this as requested.

Bit validity

Despite pointers and references being similar to usizes 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>()].

@joshlf

ghost commented Aug 29, 2023

Copy link
Copy Markdown
Contributor Author

One follow-up thought inspired by google/zerocopy#294: @RalfJung and @ehuss, would you be on board with me also adding the following to pointer.md? If this is at all controversial, I'll just follow up in a separate PR to avoid holding this one up.

It is always sound to produce a thin null pointer (i.e., for T: Sized and P = *const T or P = *mut T, transmute::<_, P>([0u8; size_of::<P>()])). Note that this can be done without unsafe code using the ptr::null or ptr::null_mut functions.

Comment thread src/types/pointer.md
Joshua Liebow-Feeser and others added 2 commits September 6, 2023 14:50
Co-authored-by: Ralf Jung <post@ralfj.de>
@RalfJung

ghost commented Sep 7, 2023 via email

Copy link
Copy Markdown
Member

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ehuss
ehuss added this pull request to the merge queue Sep 9, 2023
Merged via the queue into rust-lang:master with commit ee7c676 Sep 9, 2023
Comment thread src/types/pointer.md

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>()]`.

ghost Sep 13, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RalfJung For my own edification, how does this interact with pointer-to-integer as casts? I can imagine a few possibilities (not mutually exclusive):

  • as casts are sound, but transmutations are not necessarily sound (even though you can emulate a transmutation by doing an as cast followed by a transmutation from usize)
  • the first bullet is true and pointer-to-integer as casts probably shouldn't have been allowed in safe Rust, but it's too late

ghost Sep 14, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current working model is that as casts are special:

In contrast:

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.

ghost Sep 14, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

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.

Describe bit validity and padding for primitive types

8 participants