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

Clarify atomic bit validity #121943

Merged
merged 6 commits into from
Mar 28, 2024
Merged

Clarify atomic bit validity #121943

merged 6 commits into from
Mar 28, 2024

Conversation

joshlf
Copy link
Contributor

@joshlf joshlf commented Mar 3, 2024

The previous definition used the phrase "representation", which is ambiguous given the current state of memory model nomenclature in Rust. For integer types and for AtomicPtr<T>, the new wording clarifies that size and bit validity are guaranteed to match the corresponding native integer type/*mut T. For AtomicBool, the new wording clarifies that size, alignment, and bit validity are guaranteed to match bool.

Note that we use the phrase "size and alignment" rather than "layout" since the latter term also implies that the field types are the same. This isn't true - AtomicXxx doesn't store an xxx, but rather an UnsafeCell<xxx>. This distinction is important for some unsafe code, which needs to reason about the presence or absence of interior mutability in order to ensure that their code is sound (see e.g. google/zerocopy#251).

The previous definition used the phrase "representation", which is ambiguous given the current state of memory model nomenclature in Rust. The new wording clarifies that size and bit validity are guaranteed to match the corresponding native integer type.
@rustbot
Copy link
Collaborator

rustbot commented Mar 3, 2024

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 3, 2024
Co-authored-by: Taiki Endo <te316e89@gmail.com>
Co-authored-by: Taiki Endo <te316e89@gmail.com>
@joshlf joshlf mentioned this pull request Mar 3, 2024
20 tasks
@@ -243,7 +243,7 @@ const EMULATE_ATOMIC_BOOL: bool =

/// A boolean type which can be safely shared between threads.
///
/// This type has the same in-memory representation as a [`bool`].
/// This type has the same size, alignment, and bit validity as a [`bool`].
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, it's a bit odd to see "alignment" here when it's not in the others.

I guess it's a consequence of the other things, though: bool is size 1 per https://doc.rust-lang.org/std/mem/fn.size_of.html, which means the alignment can't be more than 1, and alignment is strictly positive so it's squeezed to have only one possible value.

One thing I'm unsure about: this clearly has a safety invariant that it's zero or one, but is that strictly a validity invariant? How do those interact with atomic ops? Or is the API restricted enough that it never hits a scenario where the question would come up?

Copy link
Member

@taiki-e taiki-e Mar 3, 2024

Choose a reason for hiding this comment

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

Hmm, it's a bit odd to see "alignment" here when it's not in the others.

Ideally AtomicI8&AtomicU8 docs should mention that as well, but I don't know how easy it would be to do that due to macro.

One thing I'm unsure about: this clearly has a safety invariant that it's zero or one, but is that strictly a validity invariant? How do those interact with atomic ops? Or is the API restricted enough that it never hits a scenario where the question would come up?

In my understanding, the stabilization of from_ptr (done in 1.75) establishes a requirement that AtomicBool must have strictly the same validity invariant as bool.

https://doc.rust-lang.org/stable/std/sync/atomic/struct.AtomicBool.html#method.from_ptr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it's a bit odd to see "alignment" here when it's not in the others.

I guess it's a consequence of the other things, though: bool is size 1 per https://doc.rust-lang.org/std/mem/fn.size_of.html, which means the alignment can't be more than 1, and alignment is strictly positive so it's squeezed to have only one possible value.

I could also just explicitly document that the alignment is 1 if you'd prefer.

One thing I'm unsure about: this clearly has a safety invariant that it's zero or one, but is that strictly a validity invariant? How do those interact with atomic ops? Or is the API restricted enough that it never hits a scenario where the question would come up?

Does @taiki-e's argument convince you? If not, I'd be happy to omit this for AtomicBool in particular (which is the only type here with a non-trivial bit validity requirement). Alternatively, I could say something like "it's guaranteed that all bit-valid bools can be soundly transmuted to AtomicBool, but the inverse is not currently guaranteed."

Copy link
Member

@taiki-e taiki-e Mar 3, 2024

Choose a reason for hiding this comment

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

Alternatively, I could say something like "it's guaranteed that all bit-valid bools can be soundly transmuted to AtomicBool, but the inverse is not currently guaranteed."

I only mentioned from_ptr (bool -> AtomicBool) because as_ptr (AtomicBool -> bool) was already stable at the time, but this should be guaranteed in both directions.

https://doc.rust-lang.org/stable/std/sync/atomic/struct.AtomicBool.html#method.as_ptr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree. I just meant that the proposed wording could work as an alternative if @scottmcm isn't convinced by your argument that we can rely on bit validity being equal between the two types. In particular, it sounds like their concern is about whether atomic operations could introduce the possibility of an AtomicBool temporarily being in some in-between state. Unless I'm misunderstanding, that concern does not imply that 0x00 or 0x01 would be invalid AtomicBool instances, but rather that there might be more valid AtomicBool instances than just those two.

Copy link
Member

Choose a reason for hiding this comment

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

whether atomic operations could introduce the possibility of an AtomicBool temporarily being in some in-between state.

Hmm, if an atomic operation has an observable in-between state, it is not "atomic", right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree; I'm just trying to steel-man @scottmcm's argument. I am convinced by your argument that bit validity is completely equivalent, but I want to make sure to leave the door open for other language changes on this PR if they aren't convinced.

Copy link
Member

Choose a reason for hiding this comment

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

I think you've convinced me here. If someone's doing a typed move of AtomicBool, it better be 0/1.

Things are complicated behind a reference, but this isn't actually guaranteeing anything there, so I think it's fine as-is.

@scottmcm
Copy link
Member

I definitely like having more specific words here.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 27, 2024

📌 Commit a6e3e02 has been approved by scottmcm

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 27, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 27, 2024
Clarify atomic bit validity

The previous definition used the phrase "representation", which is ambiguous given the current state of memory model nomenclature in Rust. For integer types and for `AtomicPtr<T>`, the new wording clarifies that size and bit validity are guaranteed to match the corresponding native integer type/`*mut T`. For `AtomicBool`, the new wording clarifies that size, alignment, and bit validity are guaranteed to match `bool`.

Note that we use the phrase "size and alignment" rather than "layout" since the latter term also implies that the field types are the same. This isn't true - `AtomicXxx` doesn't store an `xxx`, but rather an `UnsafeCell<xxx>`. This distinction is important for some `unsafe` code, which needs to reason about the presence or absence of interior mutability in order to ensure that their code is sound (see e.g. google/zerocopy#251).
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 27, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#121943 (Clarify atomic bit validity)
 - rust-lang#123089 (Add invariant to VecDeque::pop_* that len < cap if pop successful)
 - rust-lang#123101 (Delegation: fix ICE on wrong `Self` instantiation)
 - rust-lang#123130 (Load missing type of impl associated constant from trait definition)
 - rust-lang#123133 (chore: fix some comments)
 - rust-lang#123136 (Some wording improvement)
 - rust-lang#123139 (`num::NonZero::get` can be 1 transmute instead of 2)
 - rust-lang#123142 (Let nils know about changes to target docs)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 28, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#121943 (Clarify atomic bit validity)
 - rust-lang#123075 (CFI: Fix drop and drop_in_place)
 - rust-lang#123101 (Delegation: fix ICE on wrong `Self` instantiation)
 - rust-lang#123130 (Load missing type of impl associated constant from trait definition)
 - rust-lang#123133 (chore: fix some comments)
 - rust-lang#123136 (Some wording improvement)
 - rust-lang#123139 (`num::NonZero::get` can be 1 transmute instead of 2)
 - rust-lang#123142 (Let nils know about changes to target docs)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a9ed9fb into rust-lang:master Mar 28, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Mar 28, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 28, 2024
Rollup merge of rust-lang#121943 - joshlf:patch-11, r=scottmcm

Clarify atomic bit validity

The previous definition used the phrase "representation", which is ambiguous given the current state of memory model nomenclature in Rust. For integer types and for `AtomicPtr<T>`, the new wording clarifies that size and bit validity are guaranteed to match the corresponding native integer type/`*mut T`. For `AtomicBool`, the new wording clarifies that size, alignment, and bit validity are guaranteed to match `bool`.

Note that we use the phrase "size and alignment" rather than "layout" since the latter term also implies that the field types are the same. This isn't true - `AtomicXxx` doesn't store an `xxx`, but rather an `UnsafeCell<xxx>`. This distinction is important for some `unsafe` code, which needs to reason about the presence or absence of interior mutability in order to ensure that their code is sound (see e.g. google/zerocopy#251).
@joshlf joshlf deleted the patch-11 branch April 30, 2024 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants