crossbeam-skiplist: unsound Send/Sync impl for RefRange permits a data race from safe code
Bug Report
What version of TiKV are you using?
TiKV 9.0.0-beta.2 at commit 115edc6e14a6a3bd88e0dfc603686d8bbb5848c8, which is also the head of the default branch at the time of writing (2026-09-23). The affected component is the in-tree fork of crossbeam-skiplist under components/crossbeam-skiplist (publish = false), and the two unsafe impl blocks discussed below are still unbounded in that snapshot (src/base.rs:1981-1995).
That fork records its baseline in components/crossbeam-skiplist/README.md: crossbeam commit 407375cb (2024-05-19), carried while the iterator transfer work from crossbeam-rs/crossbeam#1091 remains unmerged. Upstream fixed this defect in crossbeam-rs/crossbeam#1287 ("skiplist: Fix Send/Sync impl of RefRange", merged 2026-07-10), and the vendored copy still carries the earlier code: the four commits that touch base.rs since the vendoring are the vendoring itself, the in-memory engine integration in #17257, the leak fix in #19284, and a toolchain update, none of which adds the bounds. The crates.io release that the same crate tracks through crossbeam-skiplist-offical (0.1.3) predates the fix as well, and the upstream changelog lists no later release, so a version bump alone cannot resolve the issue.
What operating system and CPU are you using?
Linux x86_64 with 32 logical CPUs, rustc nightly-2026-03-21, targeting x86_64-unknown-linux-gnu. The verdict rests on two tools: Miri under Tree Borrows (MIRIFLAGS="-Zmiri-tree-borrows") and ThreadSanitizer (-Zsanitizer=thread). The Tree Borrows flag is required rather than merely recommended, because crossbeam-epoch, on which the skiplist depends for deferred reclamation, is incompatible with Stacked Borrows and aborts inside its own internals under the default settings (crossbeam-rs/crossbeam#545, and the matching comment in that repository's ci/miri.sh); the abort is unrelated to the defect reported here.
Steps to reproduce
The reproduction is a single integration test that can be pasted without changes. No TiKV source file is modified.
- Save the source from the first collapsible block below as
components/crossbeam-skiplist/tests/f2_refrange_unsound_send.rs. The crate renames three existing test targets inCargo.tomlto satisfy the TiKV jemalloc check, butautotestsstays enabled, so a new file undertests/is discovered automatically. - Verify that the type-level test compiles, which is itself part of the evidence:
cargo +nightly-2026-03-21 test \
--manifest-path components/crossbeam-skiplist/Cargo.toml \
--test f2_refrange_unsound_send -- --nocapture- Run the racing test under Miri; it aborts at the first pair of accesses that no happens-before relation orders:
MIRIFLAGS="-Zmiri-tree-borrows" cargo +nightly-2026-03-21 miri test \
--manifest-path components/crossbeam-skiplist/Cargo.toml \
--test f2_refrange_unsound_send -- --ignored --nocapture- Alternatively, run the same test under ThreadSanitizer, which reports the race and exits with status 66:
CARGO_TARGET_DIR=target/tsan \
RUSTFLAGS="-Zsanitizer=thread -Cunsafe-allow-abi-mismatch=sanitizer" \
cargo +nightly-2026-03-21 test --target x86_64-unknown-linux-gnu \
--manifest-path components/crossbeam-skiplist/Cargo.toml \
--test f2_refrange_unsound_send -- --ignored --nocapture --test-threads=1Two details of the invocation deserve a note. First, the workspace contains both the vendored package and the crates.io crossbeam-skiplist 0.1.3 that crossbeam-skiplist-offical pulls in, so -p crossbeam-skiplist fails as ambiguous, whereas --manifest-path selects the vendored copy unambiguously. Second, AddressSanitizer cannot decide this class of defect, because both racing accesses are legal on their own; a tool that enforces happens-before is needed instead.
components/crossbeam-skiplist/tests/f2_refrange_unsound_send.rs (safe Rust, no unsafe)//! PoC: vendored `crossbeam-skiplist` `RefRange`/`SkipMap::range` is
//! unconditionally `Send`/`Sync`, so safe code can move `!Sync` values (and
//! `!Send` range-bounds) across threads and race on them.
//!
//! Integration location:
//! `components/crossbeam-skiplist/tests/f2_refrange_unsound_send.rs`
//! (paste as-is; `autotests` is on, so Cargo discovers `tests/*.rs` without
//! changing `Cargo.toml` or any source file.)
//!
//! Defect:
//! `base.rs:1981-1996` implements `Send`/`Sync` for `RefRange` with only
//! `K: Ord + Borrow<Q>` / `R: RangeBounds<Q>` bounds, and `map.rs:338`
//! `SkipMap::range` (impl block `K: Ord` at `map.rs:78-81`) returns that
//! iterator as the public `Range` (auto-`Send` via the inner type). Upstream
//! fixed this in crossbeam-rs/crossbeam#1287 (merged 2026-07-10, whose body
//! contains this exact Miri reproducer); TiKV's vendored fork (README
//! baseline 407375cb, 2024-05-19) never backported it.
//!
//! Verify with Miri (decisive) or TSan. ASan cannot detect a data race.
//! The workspace also contains the crates.io `crossbeam-skiplist 0.1.3`
//! (via `crossbeam-skiplist-offical`), so `-p crossbeam-skiplist` is ambiguous;
//! use `--manifest-path` (or the package-id spec cargo prints):
//! cargo +nightly-2026-03-21 miri test \
//! --manifest-path components/crossbeam-skiplist/Cargo.toml \
//! --test f2_refrange_unsound_send -- --ignored --nocapture
//! CARGO_TARGET_DIR=target/tsan \
//! RUSTFLAGS="-Zsanitizer=thread -Cunsafe-allow-abi-mismatch=sanitizer" \
//! cargo +nightly-2026-03-21 test --target x86_64-unknown-linux-gnu \
//! --manifest-path components/crossbeam-skiplist/Cargo.toml \
//! --test f2_refrange_unsound_send -- --ignored --nocapture --test-threads=1
use std::{cell::Cell, thread};
use crossbeam_skiplist::SkipMap;
fn assert_send<T: Send>(_: &T) {}
/// Type-level proof: the safe API hands out a `Send` iterator whose value type
/// is `!Sync`. This test *compiles* only because the `Send` impl is
/// unbounded; backporting crossbeam#1287 makes it a compile error.
#[test]
fn f2_range_is_send_with_non_sync_value() {
let map = SkipMap::new();
map.insert(0u64, Cell::new(0i32));
let range = map.range(..);
assert_send(&range);
}
/// Upstream's Miri reproducer: two safe threads mutate the same `Cell` through
/// `&V`, which the unbounded `Sync` impl allows to be shared.
#[test]
#[ignore = "triggers a data race on purpose; run under Miri or TSan"]
fn f2_range_non_sync_value_data_race() {
let map = SkipMap::new();
map.insert(0u64, Cell::new(0i32));
thread::scope(|scope| {
let range = map.range(..);
scope.spawn(move || {
let entry = range.into_iter().next().unwrap();
let cell = entry.value();
// Bounded so a plain run cannot hang; Miri/TSan abort at the first
// pair of accesses that are unordered by happens-before.
for _ in 0..10_000_000 {
cell.set(1);
}
});
let entry = map.get(&0).unwrap();
let cell = entry.value();
for _ in 0..10_000_000 {
cell.set(2);
}
});
}What did you expect?
SkipMap::range is a safe function, and neither its documentation nor that of Range or Entry::value constrains the value type to be Sync or the range bound to be Send. A map from u64 to Cell<i32>, a type that is Send but not Sync, is therefore a legal and documented instantiation, and so is every other combination that the insert methods accept. Because Cell permits mutation through a shared reference, an iterator carrying &SkipList<u64, Cell<i32>> or handing out &Cell<i32> must not be Send; the bounds on the iterator implementation are what should express that requirement, in the same way that the bounds on SkipList itself express it. With proper bounds in place, the type-level test below fails to compile, and the racing test cannot be written at all.
What did happened?
Both tools report a data race between two threads that reach the same Cell<i32> through safe code, one through the iterator returned by range(..) and the other through map.get(&0); the PoC contains no unsafe block, and neither thread holds a lock or an atomic that could order the accesses.
Miri (Tree Borrows) aborts with the following verdict:
error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `f2_range_non_sy` and (2) retag read of type `i32` on thread `unnamed-2` at alloc40660
--> .../core/src/cell.rs:513:31
|
513 | mem::replace(unsafe { &mut *self.value.get() }, val)
| ^^^^^^^^^^^^^^^^^^^^^^ (2) just happened here
|
help: and (1) occurred earlier here
--> components/crossbeam-skiplist/tests/f2_refrange_unsound_send.rs:72:13
|
72 | cell.set(2);
| ^^^^^^^^^^^Access (1) is cell.set(2) on the main thread, access (2) is cell.set(1) at line 65 on the scoped thread, and alloc40660 is the node that SkipMap::insert allocated. The verdict does not depend on timing, since Miri derives it from the absence of a happens-before edge rather than from an observed interleaving.
ThreadSanitizer reports a write-write race on the same four-byte cell, with both stacks inside <core::cell::Cell<i32>>::set, and it identifies the enclosing allocation:
WARNING: ThreadSanitizer: data race (pid=3265669)
Write of size 4 at 0x720800001000 by thread T2:
#0 <core::cell::Cell<i32>>::set
#1 f2_refrange_unsound_send::f2_range_non_sync_value_data_race::{closure#0}::{closure#0}
Previous write of size 4 at 0x720800001000 by thread T1:
#0 <core::cell::Cell<i32>>::set
#1 f2_refrange_unsound_send::f2_range_non_sync_value_data_race::{closure#0}
Location is heap block of size 32 at 0x720800001000 allocated by thread T1:
#2 <crossbeam_skiplist::base::Node<u64, core::cell::Cell<i32>>>::alloc
#3 <crossbeam_skiplist::base::SkipList<u64, core::cell::Cell<i32>>>::insert_internal
#4 <crossbeam_skiplist::base::SkipList<u64, core::cell::Cell<i32>>>::insert
#5 <crossbeam_skiplist::map::SkipMap<u64, core::cell::Cell<i32>>>::insert
SUMMARY: ThreadSanitizer: data race ... in <core::cell::Cell<i32>>::set
ThreadSanitizer: reported 3 warnings
error: test failed, to rerun pass `--test f2_refrange_unsound_send`
Caused by:
process didn't exit successfully: `.../f2_refrange_unsound_send-9087cd8f0277cf40 --ignored --nocapture --test-threads=1` (exit status: 66)Two further reports appear in the same run, and neither changes the verdict: one names the same address as a free that the reclaiming thread deferred and that collides with the cross-thread write, and one lands inside the libtest completion channel. Both are ordinary consequences of running the whole crate under the sanitizer, and the complete log below shows them in full.
The complete logs follow.
Complete Miri (Tree Borrows) log; local paths, the shell prompt, and the incidental test target prefix were normalized, nothing else was edited$ cd /path/to/tikv
MIRIFLAGS="-Zmiri-tree-borrows" cargo +nightly-2026-03-21 miri test \
--manifest-path components/crossbeam-skiplist/Cargo.toml \
--test f2_refrange_unsound_send -- --ignored --nocapture
Finished `test` profile [unoptimized] target(s) in 0.65s
Running tests/f2_refrange_unsound_send.rs (target/miri/x86_64-unknown-linux-gnu/debug/deps/f2_refrange_unsound_send-8fc768fafec47ba3)
running 1 test
test f2_range_non_sync_value_data_race ... warning: integer-to-pointer cast
--> /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/atomic.rs:204:11
|
204 | &*(ptr as *const T)
| ^^^^^^^^^^^^^^^^^ integer-to-pointer cast
|
= help: this program is using integer-to-pointer casts or (equivalently) `ptr::with_exposed_provenance`, which means that Miri might miss pointer bugs in this program
= help: see https://doc.rust-lang.org/nightly/std/ptr/fn.with_exposed_provenance.html for more details on that operation
= help: to ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead
= help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics
= help: Tree Borrows does not support integer-to-pointer casts, so the program is likely to go wrong when this pointer gets used
= note: this is on thread `f2_range_non_sy`
= note: stack backtrace:
0: <crossbeam_epoch::internal::Local as crossbeam_epoch::atomic::Pointable>::deref::<'_>
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/atomic.rs:204:11: 204:28
1: crossbeam_epoch::atomic::Shared::<'_, crossbeam_epoch::internal::Local>::deref
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/atomic.rs:1420:9: 1420:22
2: crossbeam_epoch::sync::list::List::<crossbeam_epoch::internal::Local>::insert
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/sync/list.rs:176:41: 176:58
3: crossbeam_epoch::internal::Local::register
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/internal.rs:343:13: 343:65
4: crossbeam_epoch::collector::Collector::register
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/collector.rs:45:9: 45:30
5: crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/default.rs:34:34: 34:56
6: <fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn} as std::ops::FnOnce<()>>::call_once - shim(fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn})
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
7: std::thread::local_impl::LazyStorage::<crossbeam_epoch::collector::LocalHandle, ()>::get_or_init_slow::<fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn}>
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/thread_local/native/lazy.rs:80:17: 80:59
8: std::thread::local_impl::LazyStorage::<crossbeam_epoch::collector::LocalHandle, ()>::get_or_init::<fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn}>
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/thread_local/native/lazy.rs:62:22: 62:49
9: crossbeam_epoch::default::HANDLE::{constant#0}::{closure#0}
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/thread_local/native/mod.rs:97:25: 97:115
10: <{closure@crossbeam_epoch::default::HANDLE::{constant#0}::{closure#0}} as std::ops::FnOnce<(std::option::Option<&mut std::option::Option<crossbeam_epoch::collector::LocalHandle>>,)>>::call_once - shim
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
11: std::thread::LocalKey::<crossbeam_epoch::collector::LocalHandle>::try_with::<{closure@crossbeam_epoch::default::with_handle<{closure@crossbeam_epoch::default::pin::{closure#0}}, crossbeam_epoch::guard::Guard>::{closure#0}}, crossbeam_epoch::guard::Guard>
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:512:37: 512:55
12: crossbeam_epoch::default::with_handle::<{closure@crossbeam_epoch::default::pin::{closure#0}}, crossbeam_epoch::guard::Guard>
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/default.rs:59:5: 60:28
13: crossbeam_epoch::default::pin
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/default.rs:40:5: 40:39
14: crossbeam_skiplist::SkipMap::<u64, std::cell::Cell<i32>>::insert
at components/crossbeam-skiplist/src/map.rs:373:22: 373:34
15: f2_range_non_sync_value_data_race
at components/crossbeam-skiplist/tests/f2_refrange_unsound_send.rs:55:5: 55:38
16: f2_range_non_sync_value_data_race::{closure#0}
at components/crossbeam-skiplist/tests/f2_refrange_unsound_send.rs:53:39: 53:39
warning: integer-to-pointer cast
--> /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/atomic.rs:1344:9
|
1344 | raw as *const _
| ^^^^^^^^^^^^^^^ integer-to-pointer cast
|
= note: this is on thread `unnamed-2`
= note: stack backtrace:
0: crossbeam_epoch::atomic::Shared::<'_, crossbeam_epoch::internal::Local>::as_raw
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/atomic.rs:1344:9: 1344:24
1: crossbeam_epoch::internal::Local::register
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/internal.rs:345:24: 345:38
2: crossbeam_epoch::collector::Collector::register
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/collector.rs:45:9: 45:30
3: crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn
at /path/to/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/crossbeam-epoch-0.9.20/src/default.rs:34:34: 34:56
4: <fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn} as std::ops::FnOnce<()>>::call_once - shim(fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn})
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
5: std::thread::local_impl::LazyStorage::<crossbeam_epoch::collector::LocalHandle, ()>::get_or_init_slow::<fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn}>
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/thread_local/native/lazy.rs:80:17: 80:59
6: std::thread::local_impl::LazyStorage::<crossbeam_epoch::collector::LocalHandle, ()>::get_or_init::<fn() -> crossbeam_epoch::collector::LocalHandle {crossbeam_epoch::default::HANDLE::__rust_std_internal_init_fn}>
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/thread_local/native/lazy.rs:62:22: 62:49
7: crossbeam_epoch::default::HANDLE::{constant#0}::{closure#0}
at /path/to/rustup/toolchains/nightly-2026-03-21-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/thread_local/native/mod.rs:97:25: 97:115
8: <{closure@crossbeam_epocSource: tikv/tikv