Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 AAPL pipeline, 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 trace has the form T OP NAME: the thread T that 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 next N lines are the trace.
  • There are three parts, and each adds lines to the trace. One solution must pass all eleven tests.
LineMeaningPart
T READ xThread T reads the plain variable x1
T WRITE xThread T writes the plain variable x1
T LOCK mThread T locks the mutex m1
T UNLOCK mThread T unlocks the mutex m1
T SPAWN UThread T starts a new thread U2
T JOIN UThread T waits until thread U has finished2
T ATOMIC_STORE aThread T stores to the atomic variable a with release order3
T ATOMIC_LOAD aThread T loads the atomic variable a with acquire order3

Races and happens-before

  • Accesses: the READ and WRITE lines.
  • 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 a happens-before a later line b when a chain of ordering edges leads from a to b, 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):

  1. Program order: a line happens-before every later line of the same thread.
  2. Unlock to lock: T UNLOCK m happens-before every later U LOCK m of 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
  • x is the variable.
  • L, T, and OP are the current access's line number, thread, and operation.
  • E, U, and EOP are 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 T has a clock C[T]. Its own entry C[T][T] is the thread's current epoch, which starts at 1 when T first appears in the trace.
  • Another entry C[T][U] is the latest epoch of U whose lines all happen-before T's current line.
  • Each mutex m has a clock L[m] that starts empty.

Two lines update the clocks:

  1. T UNLOCK m copies C[T] into L[m] and then adds 1 to C[T][T]. The lines T runs after the unlock belong to a new epoch, which the unlock does not cover.
  2. T LOCK m sets each entry of C[T] to the larger of itself and the matching entry of L[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

  1. Only READ, WRITE, LOCK, and UNLOCK appear, and every thread is already running when the trace starts. Two threads are therefore ordered only through a mutex they both use.
  2. An unlock of m gives no edge to a lock of n.
  3. A LOCK of a mutex that has never been unlocked orders nothing, because its clock is still empty.
  4. 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

  • N is 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 m appears only while no thread holds m, and only the thread that holds m unlocks 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:

  1. T SPAWN U starts thread U, and the spawn line happens-before every line of U. With program order, everything T did before the spawn happens-before everything U does. The lines T runs after the spawn are not ordered with U.
  2. T JOIN U waits for U to finish. Every line of U happens-before the join line, so everything U did happens-before everything T does after the join.

Guarantees

  • SPAWN U names a thread that has not appeared in the trace yet.
  • A thread whose first appearance is not as the target of a SPAWN was already running when the trace started, as in Part 1.
  • T JOIN U names a thread other than T that has already appeared and has not been joined. U does 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 a stands for a.store(v, std::memory_order_release).
  • T ATOMIC_LOAD a stands for a.load(std::memory_order_acquire).
  • Because the trace is one order, a load reads the value of the most recent ATOMIC_STORE to the same variable before it, or the initial value if there is none.

The rules:

  1. Atomic accesses never race, so they are never checked, never print, and never appear in a RACE line.
  2. They add one kind of edge: an ATOMIC_LOAD synchronizes with the store whose value it reads, so that store happens-before the load.
  3. A load that reads the initial value orders nothing.
  4. 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.
  5. A store is a release only: it does not order the storing thread after earlier stores by other threads.
  6. 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.
  7. A name used with ATOMIC_STORE and ATOMIC_LOAD is never used with READ or WRITE.

Tests 8–10 cover this part.

Example
Input
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
Output
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
Explanation

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.