-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Condvar and Mutex in std::sys::pal::unix::sync violate aliasing rules #160815
Copy link
Copy link
Open
Labels
A-pinArea: PinArea: PinA-threadArea: `std::thread`Area: `std::thread`C-bugCategory: This is a bug.Category: This is a bug.I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamRelevant to the opsem teamneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.This issue may need triage. Remove it if it has been sufficiently triaged.
Description
Metadata
Metadata
Assignees
Labels
A-pinArea: PinArea: PinA-threadArea: `std::thread`Area: `std::thread`C-bugCategory: This is a bug.Category: This is a bug.I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamRelevant to the opsem teamneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.This issue may need triage. Remove it if it has been sufficiently triaged.
Initially found by @RalfJung while discussing code surrounding #160219 on zulip, and expanded on in this issue.
Note that the text of this issue is overly focused on
Parker::new_in_place's usage ofCondvar. It turns out (see comments) that due to theDropimpl ofCondvar(andMutex), it is impossible to use these APIs correctly in their current state.To construct a pinned
Parkerin-place,Thread::newdoes so behindMaybeUninitby usingArc::get_mut_uncheckedand deriving a pointer from the mutable reference:rust/library/std/src/thread/thread.rs
Lines 101 to 108 in 4667d75
This is needed to initialize a pinned
Condvaron platforms whereParkeruses one, since it cannot be moved after being initialized:rust/library/std/src/sys/sync/thread_parking/pthread.rs
Lines 24 to 32 in 4667d75
Then
Condvar::initends up callingphread_cond_init:rust/library/std/src/sys/pal/unix/sync/condvar.rs
Lines 152 to 175 in 4667d75
As discussed on zulip,
pthread_cond_initis allowed to store the pointer for later use (this is the reason they need to be pinned). For example, on IBM AIX (which this code is run on), condvars are documented as being held in an intrusive linked list.Now, this means that the condvar's stored pointer (which was derived from a mutable reference) is invalidated when the
ArcinsideThreadis borrowed from the next time, e.g. throughThread::id. Any access to it fromlibclater on will violate the aliasing rules.@rustbot label T-opsem T-libs I-unsound A-thread -T-bootstrap A-pin