Definition
Deterministic replay is a technique that records, during one run of a program, every nondeterministic event, meaning every value or choice that could turn out differently in another run, and later runs the program again while supplying the recorded events in place of fresh ones. The second run, the replay, then follows the same path and reproduces the same behavior, including a failure that occurs only rarely. It needs the program, its initial state and the log of events. The technique is also called record and replay.
The idea
A test fails once in a thousand runs. The tester reruns it with the same input file, and it passes. The input file was not the whole cause. The failure also depended on things that changed between runs: the order in which two threads ran, the moment an interrupt §6.11 arrived, the value a clock returned, a random number, or a reply from a server. Such a failure is nicknamed a Heisenbug, after the physicist Heisenberg, because it tends to vanish when someone tries to observe it.
The key observation is that a program is deterministic between the points where outside choices enter. Given the same complete state, the same instruction always does the same thing. Nondeterminism enters only through a few doors: reads of inputs, clocks and random sources, thread switches, interrupts, and, on a processor with several cores, the order in which the cores reach shared memory. Record what came through each door, and the whole run can be rebuilt from its starting state. A chess score sheet works the same way: it records only the moves, and replaying them from the starting position recreates every position of the game. The analogy stops at the doors. A chess game has one door, the moves. A program has many, and one door that nobody recorded, such as a read of uninitialized memory or of a hardware register, makes the replay diverge, often without any sign.
How it works
1. List the sources of nondeterminism that can affect the behavior under study.
Initial state: memory contents, files, persistent storage, configuration, environment variables, and the state left behind by earlier tests or earlier messages.
Inputs: file contents, network packets, sensor readings, user actions and replies from external services.
Time: every read of a clock, and every decision made because a timer expired.
Randomness: the seed of each pseudorandom number generator, a deterministic algorithm §14.1 that produces random-looking numbers from a starting value called the seed, and every read of a hardware random source.
Scheduling: which thread runs when, and at which instruction each interrupt arrives.
Memory order between cores: which core’s write to a shared variable takes effect first.
Identity of code and tools: the exact program binary, libraries, compiler, generator algorithm and configuration.
Platform effects: for example address space layout randomization, which places the program’s memory at different addresses on each start, so that anything ordered by address can change order.
2. Record or control each source. Recording captures the value that occurred, for example the result of every system call (a request from the program to the operating system), together with its position in the run. Controlling removes the choice instead: a fixed seed, a simulated clock that advances only when the test says so, or a scheduler that runs one thread at a time in an order the test chooses. Deterministic simulation testing takes control to its end: the whole system, including network and storage, runs in one thread against simulated components, so that one seed determines the entire run.
3. Choose the fidelity. A replay can reproduce the same outputs for the same inputs; the same logical order of all events that synchronize threads or touch shared data; or the same sequence of instructions, with every interrupt and thread switch at the same instruction. No software replay reproduces physical timing: a replayed run is slower or faster than the original, so a property that depends on real elapsed time is not reproduced.
4. Replay and check for divergence. Start from the recorded initial state with the identical binary. At every door, return the recorded value instead of a fresh one, and force thread switches and interrupts at the recorded points. Compare checkpoints, such as outputs or register values at recorded events, with the log, and stop at the first difference: beyond that point the replay no longer shows the original run.
The debugger rr for Linux is a widely used example. It records the results of system calls and the signals, notifications the operating system delivers to a program, runs all threads of the program on a single processor core so that their interleaving becomes a sequence of switches it can record, and uses the processor’s hardware performance counters, which count events such as executed branches, to deliver each asynchronous event at the same point on replay.
Worked example
A missing prefix. A server receives OPEN 5, which opens session 5, and then WRITE 5 with 10 bytes. The write crashes the server because session 5 was opened with a buffer of size 0. Replaying only the second message from a fresh start gives the answer “no such session” and no crash. The failure needs the state that the first message created. The record must hold both messages from a known fresh start, or a snapshot of the server’s state taken just before the second message.
A seed is not enough. A test generates its input bytes from seed 42, so every run with seed 42 sends the same bytes. The program checks that a pointer is not null and uses it two instructions later, and an interrupt handler §6.12 can set that pointer to null. The program crashes only if the interrupt arrives between the check and the use. Assume the processor executes 64 million instructions per second, one per clock cycle. The window then lasts ns. The interrupt fires once every 1 ms, at a moment unrelated to the check, so one check falls in the window with probability . With 100 checks per run, treated as independent, a run crashes with probability about , roughly 3 runs in 1000. Once one recorded run crashes, a replay that recorded the interrupt’s arrival as “after 1,234,567 instructions” and delivers it at exactly that instruction reproduces the crash every time.
Order across two clocks. A log holds two events about one buffer: a thread frees the buffer, and a callback §14.22 reads it. If the free comes first, the read is a use-after-free §4.6, a read of memory that has already been released; if the read comes first, nothing goes wrong. The firmware logs the free with the device’s clock, and an external trace probe logs the read with its own clock. The two clocks are known to agree only within 1 ms, and the events are stamped 0.2 ms apart. Because 0.2 ms is less than the 1 ms uncertainty, the timestamps cannot say which event came first, and a replay that orders events by timestamp may build the harmless order. The log needs one order for both sources: a single shared sequence counter, or recorded causal links such as “this read saw the state left by that free”.
Stated precisely
Model a run as a sequence of steps. Let be the complete state before step : the program’s memory and everything it can read. Let be the nondeterministic event at step : the value read from an input, clock or random source, the thread chosen to run next, or whether an interrupt arrives. Let be the step function defined by the program’s code and the hardware that runs it, so that A run of steps is then fully determined by , the initial state and the events . Suppose a replay uses the same , starts from and supplies at every step. Then for every , by induction on : equal states and equal events give equal next states.
The guarantee rests on three assumptions, and a replay that diverges has broken at least one of them.
Complete capture: every nondeterministic event that the run depends on is in the log.
Equal initial state: equals in every part the run reads.
Same step function: the same binary, libraries and configuration, executed by hardware that behaves the same way.
A replay that aims only at the same outputs needs the three assumptions only for the part of the state and the events that determine those outputs.
Limits and common mistakes
A seed reproduces one generator’s output and nothing else. A seed alone suffices only when that generator is the only source of nondeterminism that affects the outcome, and only with the same generator algorithm: a library update that changes the algorithm turns the same seed into different numbers.
Recording changes the run it records. Logging slows the program and shifts its timing, so a failure that depends on timing may stop appearing while it is recorded; this is instrumentation perturbation §18.1.7, and for concurrency §9.1 failures it is also called the probe effect. A recorder that runs all threads on one core, as rr does, never shows effects that need two cores running at the same moment, such as two cores observing each other’s writes in different orders.
Replay reproduces the run, not its explanation. Replaying a crash shows where the program failed. Finding the original logical or physical cause is still analysis.
A deterministic simulator replays its own model exactly. Effects the model does not contain, such as electrical noise, flash timing or a real radio, are missing from the simulated run and from its replay alike.
A replay of several devices needs every device’s events in one consistent order. Timestamps from separate clocks give that order only when clock synchronization §9.36 keeps the clocks’ disagreement smaller than the gap between the events.
Logs can be large, and they can contain secrets, such as keys or personal data read during the run. A replay log needs the same protection as the most sensitive data it captured.
Questions answered
What state, clock, random generator, and external events must be captured?
Everything that can differ between two runs and can affect the failure must be captured or put under the test’s control, because a run is determined only by its code, its initial state and the sequence of nondeterministic events it reads. In detail:
State: the initial contents of memory that the run reads, files, persistent storage, configuration and environment variables, and the state left by earlier messages or tests, captured as a snapshot or as a recorded prefix of messages from a known reset.
Clock: the value returned by every clock read and the moment every timer expired, or else a simulated clock that the test advances.
Random generator: for each pseudorandom number generator, the seed and the exact algorithm and version; and every value read from a hardware random source.
External events: every input with its content and its position in the run, such as packets, sensor readings, replies from services and signals; and, for each interrupt and thread switch, the instruction at which it happened, or at least the order of all events that touch shared data.
Identity: the exact binary, libraries, tool versions and configuration.
Also state the fidelity: whether the replay reproduces the outputs, the logical order of events, or the exact instruction sequence. Physical timing is never reproduced. A seed alone is enough only when the random generator is the only source of nondeterminism that affects the result.
Related entries: Fuzz harness (§10.13), Nondeterministic test (§10.38), Time-of-check to time-of-use (TOCTOU) (§9.26), Wall clock and clock synchronization (§9.36), Data race (§9.3), Use-after-free (§4.6), Instrumentation perturbation and observer effects (§18.1.7).