Repository navigation
before_top_unchecked race #195
Description
Fable 5.2 xhigh:
before_top_unchecked runs make_mut on the zipper root through &self: data race when a write zipper is shared
MutNodeStack::before_top_unchecked(&self) is used when the zipper's focus is inside a child of its root node. In that case it does this:
unsafe{ self.root.unwrap().as_mut().make_mut().cast() }The GOAT note on the method asks the same question: "Why can't we just use the same logic as get_focus??". With this change, every check above passes and cargo test --lib passes (1253 tests). I have not run the full Miri suite on it.
That creates a &mut to the root handle from &self and runs make_mut through it. Two public &self methods reach it through WriteZipperCore::focus_parent: fork_read_zipper and get_focus_at. WriteZipperCore is Sync, so two threads holding a &WriteZipper* can do this to the same root at the same time. The method's comment already says it "can cause UB and must be fixed".
Reproduction
tests/race_min.rs:
use pathmap::PathMap;
use pathmap::zipper::*;
#[test]
fn fork_read_zipper_race() {
let mut map = PathMap::<()>::new();
map.set_val_at(b"abc", ());
map.set_val_at(b"abd", ());
let mut wz = map.write_zipper();
wz.descend_to(b"abc"); // the focus is now inside a child of the zipper's root node
let wz = &wz;
std::thread::scope(|s| {
s.spawn(|| { let _ = wz.fork_read_zipper(); });
let _ = wz.fork_read_zipper();
});
}cargo +nightly miri test --test race_min
error: Undefined Behavior: Data race detected between (1) retag write on thread `fork_read_zippe`
and (2) retag write of type `TrieNodeODRc<(), ()>` on thread `unnamed-2`
1: MutNodeStack::before_top_unchecked at src/write_zipper.rs:3175
2: WriteZipperCore::focus_parent at src/write_zipper.rs:1504
3: WriteZipperCore::fork_read_zipper at src/write_zipper.rs:1112
On master the root node can be destroyed while another thread reads it
make_unique takes the count with compare_exchange(1, 0) and puts it back with store(1). Another thread's CAS can land between those two calls. That thread then sees 0 and concludes the root is shared. It clones the root, assigns the clone to the root slot, and drops the old handle, which can destroy the node the first thread is reading.
The first two rows below are a native stress version of the repro under ThreadSanitizer: two threads, 500 fork_read_zipper calls each, on 5,000 maps. On master, all 6 runs report a race on the map's original root node. Five runs catch LineListNode::drop, called from before_top_unchecked, either writing that node while the other thread reads it or just before the other thread reads it. The sixth catches free of that node, from before_top_unchecked, concurrent with the other thread's store(1) to its count.
Since PR #190, make_unique only does a load(Acquire). That closes this window, but the aliasing violation remains.
| check | master de125bb |
PR #190 edfb4316 |
master + fix below |
|---|---|---|---|
| TSan, stress test | race in 6/6 runs | clean | clean |
Miri, race_min |
UB, 32/32 seeds | UB, 32/32 seeds | passes, 32/32 seeds |
Miri -Zmiri-tree-borrows, race_min |
UB: data race between make_unique's CAS and a read of the node |
passes | passes |
| Miri, single thread holding one fork while making a second | UB with Stacked Borrows, passes with Tree Borrows | same as master | passes with both |
Stress test and TSan command
#[test]
fn fork_read_zipper_stress() {
for _ in 0..5_000 {
let mut map = PathMap::<()>::new();
map.set_val_at(b"abc", ());
map.set_val_at(b"abd", ());
let mut wz = map.write_zipper();
wz.descend_to(b"abc");
let wz = &wz;
let barrier = std::sync::Barrier::new(2);
std::thread::scope(|s| {
for _ in 0..2 {
s.spawn(|| { barrier.wait(); for _ in 0..500 { let _ = wz.fork_read_zipper(); } });
}
});
}
}RUSTFLAGS="-Zsanitizer=thread -C target-cpu=native" cargo +nightly test -Zbuild-std \
--target x86_64-unknown-linux-gnu --release --test race_stress
In the single-thread case, the second fork's &mut to the root node invalidates the first read zipper's borrow of the child handle stored in that node.
Fix
The parent node is only read, so it can be taken the way top() takes it:
- unsafe{ self.root.unwrap().as_mut().make_mut().cast() }
+ unsafe{ self.root.unwrap().as_ref().as_tagged() }
Activity