-
Notifications
You must be signed in to change notification settings - Fork 13.3k
sync::mpsc: prevent double free on Drop
#139553
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//@compile-flags: -Zmiri-preemption-rate=0 -Zmiri-compare-exchange-weak-failure-rate=0 | ||
use std::sync::mpsc::channel; | ||
use std::thread; | ||
|
||
/// This test aims to trigger a race condition that causes a double free in the unbounded channel | ||
/// implementation. The test relies on a particular thread scheduling to happen as annotated by the | ||
/// comments below. | ||
fn main() { | ||
let (s1, r) = channel::<u64>(); | ||
let s2 = s1.clone(); | ||
|
||
let t1 = thread::spawn(move || { | ||
// 1. The first action executed is an attempt to send the first value in the channel. This | ||
// will begin to initialize the channel but will stop at a critical momement as | ||
// indicated by the `yield_now()` call in the `start_send` method of the implementation. | ||
let _ = s1.send(42); | ||
// 4. The sender is re-scheduled and it finishes the initialization of the channel by | ||
// setting head.block to the same value as tail.block. It then proceeds to publish its | ||
// value but observes that the channel has already disconnected (due to the concurrent | ||
// call of `discard_all_messages`) and aborts the send. | ||
}); | ||
std::thread::yield_now(); | ||
|
||
// 2. A second sender attempts to send a value while the channel is in a half-initialized | ||
// state. Here, half-initialized means that the `tail.block` pointer points to a valid block | ||
// but `head.block` is still null. This condition is ensured by the yield of step 1. When | ||
// this call returns the channel state has tail.index != head.index, tail.block != NULL, and | ||
// head.block = NULL. | ||
s2.send(42).unwrap(); | ||
// 3. This thread continues with dropping the one and only receiver. When all receivers are | ||
// gone `discard_all_messages` will attempt to drop all currently sent values and | ||
// de-allocate all the blocks. If `tail.block != NULL` but `head.block = NULL` the | ||
// implementation waits for the initializing sender to finish by spinning/yielding. | ||
drop(r); | ||
// 5. This thread is rescheduled and `discard_all_messages` observes the head.block pointer set | ||
// by step 4 and proceeds with deallocation. In the problematic version of the code | ||
// `head.block` is simply read via an `Acquire` load and not swapped with NULL. After this | ||
// call returns the channel state has tail.index = head.index, tail.block = NULL, and | ||
// head.block != NULL. | ||
t1.join().unwrap(); | ||
// 6. The last sender (s2) is dropped here which also attempts to cleanup any data in the | ||
// channel. It observes `tail.index = head.index` and so it doesn't attempt to cleanup any | ||
// messages but it also observes that `head.block != NULL` and attempts to deallocate it. | ||
// This is however already deallocated by `discard_all_messages`, leading to a double free. | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this is that this will then always be present when Miri executes this code, even outside the test. Ideally we'd only enable this on our CI. This will require some new flag though...
Also there should be a comment explaining the point of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment explaining why this point is interesting and a pointer to the test that uses it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I guess now the main question is whether it's worth leaving in this
yield
for all Miri users or whether it should be somehow only applied for the test suite. It probably doesn't harm to always have it...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also lean towards leaving there. It does yield at a very interesting point of the channel in general so having it there might catch future regressions sooner