[clang] [compiler-rt] [llvm] [tsan] Add simulation to TSAN (PR #183200)
Vitaly Buka via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 23:02:40 PST 2026
================
@@ -477,3 +477,267 @@ Increase sampling frequency for mutex operations:
```console
$ TSAN_OPTIONS=enable_adaptive_delay=1:adaptive_delay_mutex_sample_rate=5 ./myapp
```
+
+## Simulation Scheduler
+
+### Overview
+
+The Simulation Scheduler is an optional ThreadSanitizer feature that enables
+random exploration of thread interleavings to expose data races that may be
+difficult to trigger in normal execution. Unlike standard ThreadSanitizer which
+detects races as they occur naturally during program execution, the simulation
+scheduler takes control of thread scheduling to deliberately explore different
+execution orderings.
+
+Simulation is particularly useful for:
+
+- Testing concurrent data structure or algorithms during development to ensure
+ correctness (for example, a lock-free queue).
+- Finding races in rarely-executed interleavings that standard ThreadSanitizer
+ may miss
+- Reproducing specific race conditions deterministically
+
+Simulation is not useful for running full applications, and will likely not
+work in these scenarios. The code run in simulation should almost always be a
+small unit test exercising very specific functionality.
+
+When enabled via the `__tsan_simulate()` API, the simulation scheduler runs
+the program's concurrent code multiple times (iterations), choosing different
+thread interleavings in each iteration. The scheduler injects context switches
+at synchronization points (atomic operations, mutex operations, thread lifecycle
+events) to maximize coverage of possible execution orderings. If a data race is
+detected, the simulation stops and reports which iteration exposed the race,
+allowing that specific interleaving to be reproduced.
+
+### Usage
+
+To use simulation, wrap the concurrent code you want to test in a callback
+function and invoke it through the `__tsan_simulate()` API:
+
+```c
+extern "C" int __tsan_simulate(void (*callback)(void *), void *arg);
+
+void test_concurrent_code(void *arg) {
+ // Create threads, run concurrent operations
+ pthread_t t1, t2;
+ pthread_create(&t1, NULL, thread_func, NULL);
+ pthread_create(&t2, NULL, thread_func, NULL);
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+}
+
+int main() {
+ return __tsan_simulate(test_concurrent_code, NULL);
+}
+```
+
+Then compile with ThreadSanitizer and enable the simulation scheduler:
+
+```console
+$ clang -fsanitize=thread -g -O1 mytest.c
+$ TSAN_OPTIONS=simulate_scheduler=random ./a.out
+ThreadSanitizer: simulation starting (iterations 0..999, max_depth=10000, scheduler=random)
+```
+
+### Wrapping Main for Simulation
+
+To avoid modifying existing test code, you can use the linker's `--wrap=main`
+option to automatically run `main()` under simulation.
+
+```c
+// mytest.c
+void *thread_func(void *arg) { /* ... */ }
+
+int main() {
+ // This entire main() runs under simulation automatically
+ pthread_t t1, t2;
+ pthread_create(&t1, NULL, thread_func, NULL);
+ pthread_create(&t2, NULL, thread_func, NULL);
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+ return 0;
+}
+```
+
+Compile and link with `-Wl,--wrap=main`:
+
+```console
+$ clang -fsanitize=thread -g -O1 mytest.c -Wl,--wrap=main
----------------
vitalybuka wrote:
Since __wrap_main is no longer provided by Clang/compiler-rt, this example will fail to link with undefined reference to __wrap_main unless the snippet also shows the user-defined __wrap_main calling __tsan_simulate and __real_main.
https://github.com/llvm/llvm-project/pull/183200
More information about the cfe-commits
mailing list