Skip to content

Conversation

@gksato
Copy link

@gksato gksato commented Nov 2, 2025

Fixes #148083.

Due to impl<T: Sync> Sync for MaybeUninit<T>,
MaybeUninit::assume_init_read is prone to the illegal use transferring ownership of !Send + Sync value across threads. This PR adds a warning on this thread safety problem to the method's Safety section. This PR also adds two code examples related to this problem.

On the bad case code example: I wish I could use something better than MutexGuard<'_, u32>, because I wasn't able to produce Miri-detectable UB. core::sync::Exclusive<&Cell<u32>> would be nice, but it's unstable.

@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 Nov 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 2, 2025

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

This comment has been minimized.

Due to `impl<T: Sync> Sync for MaybeUninit<T>`,
`MaybeUninit::assume_init_read` is prone to the illegal use transferring
ownership of `!Send + Sync` value across threads. This commit adds a
warning on this thread safety problem to the method's Safety section.
This commit also adds two code examples related to this problem.
@gksato gksato force-pushed the 148083_maybe_uninit_read_thread_safety_doc branch from bf0ca0c to 1033c71 Compare November 2, 2025 12:06
@gksato gksato changed the title Doc: MaybeUninit::assume_init_read Safety: warn on thread safety Doc: MaybeUninit::assume_init_read Safety: warn on thread safety (fix of #148083) Nov 2, 2025
@gksato gksato changed the title Doc: MaybeUninit::assume_init_read Safety: warn on thread safety (fix of #148083) Doc: MaybeUninit::assume_init_read Safety: warn on thread safety Nov 2, 2025
@gksato
Copy link
Author

gksato commented Nov 4, 2025

@rustbot label A-docs I-unsound

@rustbot rustbot added A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 4, 2025
@gksato
Copy link
Author

gksato commented Nov 4, 2025

@rustbot label -I-unsound -I-prioritize

@rustbot rustbot removed I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add MaybeUninit::assume_init_read Safety constraint: it can easily break !Send invariant

3 participants