Skip to content

Commit fe1742a

Browse files
committed
sync::mpsc: add miri reproducer of double free
Signed-off-by: Petros Angelatos <[email protected]>
1 parent 48f89e7 commit fe1742a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

library/std/src/sync/mpmc/list.rs

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ impl<T> Channel<T> {
213213
.compare_exchange(block, new, Ordering::Release, Ordering::Relaxed)
214214
.is_ok()
215215
{
216+
#[cfg(miri)]
217+
crate::thread::yield_now();
216218
self.head.block.store(new, Ordering::Release);
217219
block = new;
218220
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@compile-flags: -Zmiri-preemption-rate=0 -Zmiri-compare-exchange-weak-failure-rate=0
2+
use std::sync::mpsc::channel;
3+
use std::thread;
4+
5+
/// This test aims to trigger a race condition that causes a double free in the unbounded channel
6+
/// implementation. The test relies on a particular thread scheduling to happen as annotated by the
7+
/// comments below.
8+
fn main() {
9+
let (s1, r) = channel::<u64>();
10+
let s2 = s1.clone();
11+
12+
let t1 = thread::spawn(move || {
13+
// 1. The first action executed is an attempt to send the first value in the channel. This
14+
// will begin to initialize the channel but will stop at a critical momement as
15+
// indicated by the `yield_now()` call in the `start_send` method of the implementation.
16+
let _ = s1.send(42);
17+
// 4. The sender is re-scheduled and it finishes the initialization of the channel by
18+
// setting head.block to the same value as tail.block. It then proceeds to publish its
19+
// value but observes that the channel has already disconnected (due to the concurrent
20+
// call of `discard_all_messages`) and aborts the send.
21+
});
22+
std::thread::yield_now();
23+
24+
// 2. A second sender attempts to send a value while the channel is in a half-initialized
25+
// state. Here, half-initialized means that the `tail.block` pointer points to a valid block
26+
// but `head.block` is still null. This condition is ensured by the yield of step 1. When
27+
// this call returns the channel state has tail.index != head.index, tail.block != NULL, and
28+
// head.block = NULL.
29+
s2.send(42).unwrap();
30+
// 3. This thread continues with dropping the one and only receiver. When all receivers are
31+
// gone `discard_all_messages` will attempt to drop all currently sent values and
32+
// de-allocate all the blocks. If `tail.block != NULL` but `head.block = NULL` the
33+
// implementation waits for the initializing sender to finish by spinning/yielding.
34+
drop(r);
35+
// 5. This thread is rescheduled and `discard_all_messages` observes the head.block pointer set
36+
// by step 4 and proceeds with deallocation. In the problematic version of the code
37+
// `head.block` is simply read via an `Acquire` load and not swapped with NULL. After this
38+
// call returns the channel state has tail.index = head.index, tail.block = NULL, and
39+
// head.block != NULL.
40+
t1.join().unwrap();
41+
// 6. The last sender (s1) is dropped here which also attempts to cleanup any data in the
42+
// channel. It observes `tail.index = head.index` and so it doesn't attempt to cleanup any
43+
// messages but it also observes that `head.block != NULL` and attempts to deallocate it.
44+
// This is however already deallocated by `discard_all_messages`, leading to a double free.
45+
}

0 commit comments

Comments
 (0)