[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:44 PST 2026


================
@@ -0,0 +1,154 @@
+//===-- tsan_simulate.h -----------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of ThreadSanitizer (TSan), a race detector.
+//
+// Simulation scheduler for systematic thread interleaving exploration.
+// Inspired by Relacy Race Detector's random scheduler:
+// https://github.com/dvyukov/relacy
+//
+// When simulation is active, exactly one application thread runs at a time.
+// Other threads are parked on internal semaphores. At each sync point
+// (pthread_* calls, atomic operations), the running thread may yield to another
+// thread chosen by the scheduler.
+//===----------------------------------------------------------------------===//
+
+#ifndef TSAN_SIMULATE_H
+#define TSAN_SIMULATE_H
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+namespace __tsan {
+
+// TODO: Simulation would be more useful with the following features
+//  - Read/write mutex support
+//  - Timed pthread* API support
+//  - std::atomic::wait/notify* support (doesn't work today, because these APIs
+//    rely on direct OS futex calls which TSAN does not observe)
+//  - Alternate scheduling algorithms like full search or other random
+//    distributions
+
+// Run the simulation: invoke `callback(arg)` for `simulate_iterations`
+// iterations, exploring thread interleavings using the configured scheduler.
+// Returns 0 on success, -1 on error.
+//
+// Errors include
+//  - Pre-existing threads when simulation was started
+//  - Unsupported interceptor
+//  - Max simulation depth hit
+//  - Race detected
+//  - Deadlock detected (all simulated threads were blocked)
+//    Deadlock results in program termination via Die()
+//
+// If an unsupported interceptor is invoked, the simlulation enters undefined
+// behavior from the ThreadSanitizer simulation perspective. The interceptor
+// may lead to the simulation being unable to advance (deadlocked), or the
+// simulation may eventually be able to return out from SimulateRun.
+int SimulateRun(void (*callback)(void*), void* arg);
+
+extern bool sim_active;
+
+ALWAYS_INLINE bool SimulateIsActive() { return sim_active; }
+
+void SimulateScheduleImpl();
+void SimulateReportUnsupportedImpl(const char* func_name);
+void SimulateReportRaceImpl();
+void SimulateThreadRegisterImpl(uptr thread_handle);
+void SimulateBeforeChildThreadRunsImpl();
+void SimulateThreadFinishImpl();
+
+// SimulateSchedule is the key hook for simulation. It's called at each
+// scheduling point (atomic op, mutex/cv op, thread create/join). When
+// simulation is active, SimulateSchedule will check if another thread should
+// run, and if so, context switch to that thread.
+ALWAYS_INLINE void SimulateSchedule() {
+  if (!SimulateIsActive())
----------------
vitalybuka wrote:

SimulateSchedule runs on every atomic operation in AtomicImpl, so marking the active check UNLIKELY helps keep the disabled fast path out of line.
```suggestion
  if (LIKELY(!SimulateIsActive()))
```

https://github.com/llvm/llvm-project/pull/183200


More information about the cfe-commits mailing list