Data Race Detector
Prerequisites: Threads, Shared State, and Data Races, Mutexes, Lock Scope, and Contention, Atomics and Memory Ordering Intro
ThreadSanitizer, the -fsanitize=thread detector from the data races article, reports a data race when two threads access the same memory location, at least one access writes, and nothing synchronizes them. Build the core of such a detector.
- Trace: the operations of one test run of the
AAPLpipeline, one per line, in the single order in which they ran. The detector reads a trace instead of instrumenting a running program, replays it on one thread, and reports every access that races with an earlier one.
What to implement
detect_races(trace)returns the printed lines, in trace order.- Each line of
tracehas the formT OP NAME: the threadTthat performed the operation, the operation, and the name it applies to. - Lines are numbered in trace order, and the first trace line is line 1.
- On stdin, the first line is the number of trace lines
N, and the nextNlines are the trace. - There are three parts, and each adds lines to the trace. One solution must pass all eleven tests.
| Line | Meaning | Part |
|---|---|---|
T READ x | Thread T reads the plain variable x | 1 |
T WRITE x | Thread T writes the plain variable x | 1 |
T LOCK m | Thread T locks the mutex m | 1 |
T UNLOCK m | Thread T unlocks the mutex m | 1 |
T SPAWN U | Thread T starts a new thread U | 2 |
T JOIN U | Thread T waits until thread U has finished | 2 |
T ATOMIC_STORE a | Thread T stores to the atomic variable a with release order | 3 |
T ATOMIC_LOAD a | Thread T loads the atomic variable a with acquire order | 3 |
Races and happens-before
- Accesses: the
READandWRITElines. - Conflict: two accesses touch the same variable from different threads, and at least one is a
WRITE. Two reads never conflict, and a thread never conflicts with itself. - Happens-before: line
ahappens-before a later linebwhen a chain of ordering edges leads fromatob, each edge pointing forward in the trace. It decides whether two conflicting accesses are synchronized. - Race: an access races with an earlier conflicting access when the earlier access does not happen-before it.
Part 1 has two kinds of edge (Parts 2 and 3 add edges for threads and atomics):
- Program order: a line happens-before every later line of the same thread.
- Unlock to lock:
T UNLOCK mhappens-before every laterU LOCK mof the same mutex.
Edges chain. If T1 writes x and then unlocks m, and T2 later locks m and then reads x, the write happens-before the read through program order, the unlock-to-lock edge, and program order again.
The trace shows one order, but nothing forced it: on another run the two accesses could have run the other way around. A race is reported even when this run's order happened to give the right answer.
Output: when an access races with at least one earlier access, print one line at that access.
RACE x line L T OP vs line E U EOP
xis the variable.L,T, andOPare the current access's line number, thread, and operation.E,U, andEOPare the same three fields of the earlier access.- If the access races with several earlier accesses, report only the most recent one, the one with the largest line number.
- An access that races with nothing prints nothing, and so does every other line.
Vector clocks
Searching backward for chains of edges would be slow. A vector clock answers the happens-before question with a few integer compares.
- It maps each thread to a counter. A thread it does not list reads as 0.
- Each thread
Thas a clockC[T]. Its own entryC[T][T]is the thread's current epoch, which starts at 1 whenTfirst appears in the trace. - Another entry
C[T][U]is the latest epoch ofUwhose lines all happen-beforeT's current line. - Each mutex
mhas a clockL[m]that starts empty.
Two lines update the clocks:
T UNLOCK mcopiesC[T]intoL[m]and then adds 1 toC[T][T]. The linesTruns after the unlock belong to a new epoch, which the unlock does not cover.T LOCK msets each entry ofC[T]to the larger of itself and the matching entry ofL[m]. This entry-by-entry maximum is called a join of the two clocks.
With these rules, an access that thread U made in epoch e happens-before the current line of thread T exactly when e <= C[T][U].
Only the printed lines are checked, so any correct way of computing happens-before passes. The vector clock costs O(P) per line for P threads.
Part 1: Locks
- Only
READ,WRITE,LOCK, andUNLOCKappear, and every thread is already running when the trace starts. Two threads are therefore ordered only through a mutex they both use. - An unlock of
mgives no edge to a lock ofn. - A
LOCKof a mutex that has never been unlocked orders nothing, because its clock is still empty. - An access made without the lock gets no protection from the other threads' locking: the mutex protects only the paths that take it.
Limits and guarantees
Nis between 1 and 2 · 104, and at most 50 distinct threads appear.- Every name is 1 to 12 letters, digits, or underscores. Each name has one role: a thread, a mutex, a plain variable, or an atomic variable.
- The trace is well formed, as a recorded run is:
LOCK mappears only while no thread holdsm, and only the thread that holdsmunlocks it. - Parts 2 and 3 add their own guarantees.
Tests 1–4 and 11 cover this part.
Part 2: Spawn and join
Add SPAWN and JOIN:
T SPAWN Ustarts threadU, and the spawn line happens-before every line ofU. With program order, everythingTdid before the spawn happens-before everythingUdoes. The linesTruns after the spawn are not ordered withU.T JOIN Uwaits forUto finish. Every line ofUhappens-before the join line, so everythingUdid happens-before everythingTdoes after the join.
Guarantees
SPAWN Unames a thread that has not appeared in the trace yet.- A thread whose first appearance is not as the target of a
SPAWNwas already running when the trace started, as in Part 1. T JOIN Unames a thread other thanTthat has already appeared and has not been joined.Udoes not appear again after it is joined.- A thread that was running from the start can be joined too.
Work out how each of the two lines changes the vector clocks.
Tests 5–7 cover this part.
Part 3: Atomic flags
Add atomic variables, the ready flag from the atomics article:
T ATOMIC_STORE astands fora.store(v, std::memory_order_release).T ATOMIC_LOAD astands fora.load(std::memory_order_acquire).- Because the trace is one order, a load reads the value of the most recent
ATOMIC_STOREto the same variable before it, or the initial value if there is none.
The rules:
- Atomic accesses never race, so they are never checked, never print, and never appear in a
RACEline. - They add one kind of edge: an
ATOMIC_LOADsynchronizes with the store whose value it reads, so that store happens-before the load. - A load that reads the initial value orders nothing.
- An older store whose value was overwritten gives the load no edge of its own. It is ordered before the load only if it happens-before the newest store, for example because the same thread made both stores.
- A store is a release only: it does not order the storing thread after earlier stores by other threads.
- A load is an acquire only: it publishes nothing, so a later load by another thread still synchronizes with the most recent store, not with the earlier load.
- A name used with
ATOMIC_STOREandATOMIC_LOADis never used withREADorWRITE.
Tests 8–10 cover this part.
16 T1 READ count T2 READ count T1 WRITE count T2 WRITE count T1 READ px T2 READ px T1 LOCK m T1 READ total T1 WRITE total T1 UNLOCK m T2 LOCK m T2 READ total T2 WRITE total T2 UNLOCK m T2 READ count T1 READ total
RACE count line 3 T1 WRITE vs line 2 T2 READ RACE count line 4 T2 WRITE vs line 3 T1 WRITE RACE total line 16 T1 READ vs line 13 T2 WRITE
Lines 1 to 4 are the lost update from the data races article: both threads read count before either writes it. T1's write on line 3 conflicts with T2's read on line 2, which does not happen-before it.
T2's write on line 4 conflicts with both of T1's accesses, and the more recent one is the write on line 3. The two reads of px do not conflict.
Lines 7 to 14 update total under m, and T2's lock on line 11 comes after T1's unlock on line 10, so nothing there races.
The same edge orders T1's write of count on line 3 before T2's read on line 15, so that read is clean. On line 16, T1 reads total without taking m, and T2's write on line 13 does not happen-before it.