-
Notifications
You must be signed in to change notification settings - Fork 13.3k
MutexGuard<Cell<i32>> must not be Sync #41622
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Btw, this bug was found while trying to prove soundnes of (an idealized version of) Also, this hints at a more general problem: OIBITS for types with custom invariants ("unsafe types") are dangerous. |
To elaborate on this (from a formal proof perspective): To prove correctness of Send/Sync for an unsafe type (i.e., a type with its own invariant), I need to know exactly under which bounds the type is Send/Sync -- this defines the theorem I have to prove. However, if the type does not have an explicit impl for Send/Sync, I don't know how to even figure this out -- I would have to chase all types (including safe ones, and across all abstractions) of all fields recursively and then check when they are Send/Sync... that's way too error-prone. What I do instead is I try to compile small programs that only work e.g. if Also, even if there is an impl, there are still some subtleties: First of all, does giving a restrictive positive impl disable the more liberal automatic impl? (Tests indicate yes.) What if there is a restrictve negative impl? If I find |
MutexGuard<T> may be Sync only if T is Sync Fixes #41622 This is a breaking change. Does that imply any further process? I am not sure whether I wrote that "compilation must fail"-test correctly, but at least it is passing here with the patch applied. Same for the `since = "1.18.0"`, I just picked it because I had to pick something.
This example only makes sense (and is therefore easier to understand logically :) ) if T is Sync. See rust-lang/rust#41622 - this used to be a bug initially.
This example only makes sense (and is therefore easier to understand logically :) ) if T is Sync. See rust-lang/rust#41622 - this used to be a bug initially.
Right now,
MutexGuard<Cell<i32>>
satisfies theSync
bound. That is rather bad, because it lets me write a program that has a data race:The
get
andset
calls in the two threads are unsynchronized (as usual for aCell
), and they are racing. This is a soundness bug.The cause for this is that
MutexGuard<T>
implementsSync
wheneverT
implementsSend
, which is plain wrong. The fix is to letMutexGuard<T>
implementSync
wheneverT
implementsSync
. I will submit a PR soon.The text was updated successfully, but these errors were encountered: