Tutorial · Nix

Tutorial: a flaky Nix build

This tutorial takes a derivation whose tests fail now and then, has Rewind VM find a failing run, looks at the crash, and checks the fix across 64 thread interleavings.

The derivation is mylib, a small C thread pool in examples/mylib. Its pool_shutdown frees the job queue before joining the workers, so a worker still finishing a job can write through a freed, nulled pointer.

Every transcript below is real output from rewind on a 16 core AMD laptop.

Install

You need x86_64 Linux with KVM and Nix with flakes enabled.

$ nix profile install github:fzakaria/rewindvm
$ ls -l /dev/kvm
crw-rw-rw- 1 root kvm 10, 232 Oct  1 20:16 /dev/kvm

The build comes from rewindvm.cachix.org; say yes when Nix asks to trust it. The desktop app is github:fzakaria/rewindvm#app. On NixOS, use the module instead:

inputs.rewind.url = "github:fzakaria/rewindvm";

# with inputs.rewind.nixosModules.default imported
programs.rewind.enable = true;
programs.rewind.app.enable = true;
# AMD only: make the branch counter exact at every boot
programs.rewind.amdBranchCounterWorkaround = true;

If /dev/kvm is not yours to use, add yourself to the kvm group. On AMD, run sudo rewind pmu enable once after each boot; Counter time says why.

The flaky build

On the host the build usually passes. Rebuilt 45 times, it failed twice:

$ nix build --rebuild -L github:fzakaria/rewindvm#mylib
...
mylib> running tests/test_pool_shutdown
...
mylib> job 16 done: 5986
mylib> worker picked job 18
mylib> job 17 done: 43360
mylib> /nix/store/...-bash-5.3p15/bin/bash: line 1:   133 Segmentation fault         (core dumped) ./$t
mylib> make: *** [Makefile:18: check] Error 1
error: Cannot build '/nix/store/hvp2d0h9l97d19d3xp5k3vf6xwhg4axr-mylib-0.3.0.drv'.

Run it again and it usually passes, leaving nothing to look at.

Build it in Rewind VM

rewind nix builds a derivation as the Nix sandbox would, inside a deterministic virtual machine:

$ rewind nix --epoch 1790985600 github:fzakaria/rewindvm#mylib
rewind: packing 62 store paths for mylib-0.3.0
...
rewind: run 631eccb8b884c543 exited:0 after 6029 steps, 0.215s virtual, 0.637s wall (poweroff)
/nix/store/f6a9gy362szw6nxx3ikrklr8glr6rdln-mylib-0.3.0 a9d703ba89774f3d  matches your store, rewindvm.cachix.org

It passes, in the same 6029 steps every time. A step is a point where the VM stops and hands control to Rewind, such as a system call that reports a file or a process; steps number a run's events and are where you look inside it. --epoch fixes the VM's clock at boot, one of the run's inputs, so your runs are the ones shown here. The last line says the output matches the copy in your store.

Find a failing interleaving

rewind check builds it again under perturbed schedules, which reschedule and stall the VM's threads at different points, and stops at the first batch in which a build ends differently:

$ rewind check --epoch 1790985600 github:fzakaria/rewindvm#mylib
schedule   0: exited:0             6029 steps  a9d703ba8977  run 631eccb8b884c543
schedule   1: exited:2             5364 steps    run 9fccda14e210e3fc
schedule   2: exited:0             7531 steps  a9d703ba8977  run ef15dc11b4faec58
schedule   3: exited:0             7805 steps  a9d703ba8977  run 738949862947fdf0
schedule   4: exited:0             7794 steps  a9d703ba8977  run 0ed44e064c81907c
schedule   5: exited:0             7623 steps  a9d703ba8977  run 369dfd1a991983fd
schedule   6: exited:0             7619 steps  a9d703ba8977  run 96754451c2fd0381
schedule   7: exited:0             7643 steps  a9d703ba8977  run e83d73ccda2d7099
schedule   8: exited:2             5842 steps    run a3ee4417684ac3a9
schedule   9: exited:2             5377 steps    run 9b22abccbb510e75
schedule  10: exited:0             7564 steps  a9d703ba8977  run 19f15f3eb578490b
schedule  11: exited:2             6150 steps    run 56079885561e2364
schedule  12: exited:0             7780 steps  a9d703ba8977  run df981a51b11ecaa7
schedule  13: exited:2             5953 steps    run 524f666c28d69310
schedule  14: exited:2             6270 steps    run e247fda3d0f2ed2c
schedule  15: exited:0             7813 steps  a9d703ba8977  run 4416879da8882a17
schedule  16: exited:0             7612 steps  a9d703ba8977  run 1686d4df28d14b77

schedule 1 ends differently from schedule 0; finding the step that decides it
schedule 1 limited to steps 4784..4969 still ends differently
step 4968 decides it: a request to switch threads there makes the run fail

passing: run c2edb2940353d54f, schedule 1 over steps 4784..4968
failing: run 11d4fbb7f2b822f5, schedule 1 over steps 4784..4969
the two are the same run until step 4968

where ./tests/test_pool_shutdown first behaves differently:
  both           4956   166/173   write(1, "job 15 done: 42559\n")
  both           4957   166/173   write(1, "worker picked job 18\n")
  both           4968   166/174   write(1, "job 17 done: 43360\n")
  failing        4972   166/173   write(1, "job 18 done: 26744\n")
  failing        4973   166/173   write(1, "worker picked job 19\n")
  failing        4982   166/174   SIGSEGV code=1 addr=0x108
  failing        4989   166/174   thread exit(test_pool_shutd) killed:SIGSEGV
  passing        4969   166/174   write(1, "worker picked job 19\n")
  passing        4975   166/173   write(1, "job 18 done: 26744\n")
  passing        4976   166/173   write(1, "worker picked job 20\n")
  passing        4989   166/174   thread exit(test_pool_shutd) exited:0

open both in the desktop app: rewind open 11d4fbb7f2b822f5 4972 --compare c2edb2940353d54f

Schedules 1, 8, 9, 11, 13 and 14 fail. check narrows schedule 1's failure to one step, 4968. The passing run perturbs the same steps less that one, so the two runs are the same until 4968, and only the failing one gets a request to switch threads there. The search took 12 seconds.

The last block shows where the test's own events then differ. The first difference is the cause, showing up before the symptom. At the deciding step thread 174 has reported job 17 and has yet to count it, which a worker does outside the lock. In the failing run it is held there: at step 4972 the other worker, thread 173, reports job 18 and goes on to pick job 19, the test sees half the jobs done and shuts the pool down, and thread 174 counts its job through the queue the shutdown freed. In the passing run thread 174 counts job 17 and picks job 19 first. Thread 174's SIGSEGV comes 10 steps later, at step 4982.

--all tries every schedule, which measures how flaky a build is:

$ rewind check --all --epoch 1790985600 github:fzakaria/rewindvm#mylib | grep 'ended differently'
18 of 64 other schedules ended differently

Look at the failure

The failing run keeps every event with its step:

$ rewind log 11d4fbb7 --steps | tail -4
      4972   166  job 18 done: 26744
      4973   166  worker picked job 19
      5000   162  /nix/store/...-bash-5.3p15/bin/bash: line 1:   166 Segmentation fault         ./$t
      5004   161  make: *** [Makefile:18: check] Error 1

$ rewind events 11d4fbb7 --from 4968 --to 4984
      4968   166/174   write(1, "job 17 done: 43360\n")
      4972   166/173   write(1, "job 18 done: 26744\n")
      4973   166/173   write(1, "worker picked job 19\n")
      4980     0/0     console "[    0.200335] test_pool_shutd[174]: segfault at 108 ip 000055c06fa1d437 sp 00007fda39950e10 error 6 in test_pool_shutdown[1437,55c06fa1d000+1000] likely on CPU 0 (core 0, socket 0)"
      4981     0/0     console "[    0.200340] Code: fa 48 8d 35 2a 0c 00 00 bf 02 00 00 00 b8 00 00 00 00 e8 ac fc ff ff 48 8b 05 b5 2b 00 00 48 8b 38 e8 8d fc ff ff 49 8b 46 58 <83> 80 08 01 00 00 01 e9 b5 fe ff ff 55 48 89 e5 41 54 53 be 78 00"
      4982   166/174   SIGSEGV code=1 addr=0x108

The kernel's report marks the faulting instruction, <83> 80 08 01 00 00 01: addl $1, 0x108(%rax), which is p->queue->completed++ with p->queue null. The processes alive at the crash:

$ rewind ps 11d4fbb7 4982
     1 /init
    34   bash -e /nix/store/...-source-stdenv.sh /nix/store/...-default-builder.sh
   161     make SHELL=/nix/store/...-bash-5.3p15/bin/bash PREFIX=$(out) VERBOSE=y check
   162       /nix/store/...-bash-5.3p15/bin/bash -c for t in tests/test_pool_basic tests/test_pool_shutdown; do echo "running $t"; ./$t || exit 1; done
   166         ./tests/test_pool_shutdown
   173           (thread)
   174           (thread)

Look inside the VM

rewind cat, rewind shell and rewind gdb work on a throwaway fork of the run at a step. At the SIGSEGV, step 4982:

$ rewind cat 11d4fbb7 4982 src/pool.c --pid 166 | sed -n '/^void pool_shutdown/,/^}/p'
void pool_shutdown(struct pool *p)
{
	pthread_mutex_lock(&p->lock);
	p->stopping = 1;
	pthread_cond_broadcast(&p->ready);
	pthread_mutex_unlock(&p->lock);

	/* The bug: the queue goes before the workers are joined. */
	free(p->queue);
	p->queue = NULL;

	for (int i = 0; i < POOL_WORKERS; i++)
		pthread_join(p->workers[i], NULL);
	pthread_cond_destroy(&p->ready);
	pthread_mutex_destroy(&p->lock);
	free(p);
}

$ printf 'pwd; ls; exit\n' | rewind shell 11d4fbb7 4982 --pid 166
rewind: a shell at step 4982 of 11d4fbb7f2b822f5; exit it to leave
[rewind] /build/mylib # pwd; ls; exit
/build/mylib
Containerfile  Makefile  libmylib.a  src  tests

rewind gdb loads the symbols of the VM's kernel and of the process running at the step. From step 4968, thread 174's last write, continue to the line that faulted:

$ rewind gdb 11d4fbb7 4968 -- -batch -ex 'break src/pool.c:77 if p->queue == 0' -ex continue -ex 'bt 3' -ex 'p p->queue' -ex list
rewind: step 4968 ran in process 166; loading symbols for 4 of its files
rewind: fetched 3 source files from the VM
rewind: gdb at step 4968 of 11d4fbb7f2b822f5
Downloading 10.70 K separate debug info for /nix/store/h4wfwic161kxrr74jlzla5lsm28hgary-glibc-2.44-25/lib/libpthread.so.0...
Downloading 4.35 M separate debug info for /nix/store/h4wfwic161kxrr74jlzla5lsm28hgary-glibc-2.44-25/lib/libc.so.6...
Downloading 658.16 K separate debug info for /nix/store/h4wfwic161kxrr74jlzla5lsm28hgary-glibc-2.44-25/lib/ld-linux-x86-64.so.2...
Downloading 3.12 K source file /build/linux-7.2.8/./arch/x86/include/asm/irqflags.h...
arch_local_irq_restore (flags=518) at ./arch/x86/include/asm/irqflags.h:146
146		return !(flags & X86_EFLAGS_IF);
[Switching to thread 4 (Thread 1.174)]
Downloading 1.79 K source file /build/glibc-2.44/nptl/../sysdeps/unix/sysv/linux/x86_64/syscall_cancel.S...
#0  __syscall_cancel_arch () at ../sysdeps/unix/sysv/linux/x86_64/syscall_cancel.S:56
56		ret
Breakpoint 1 at 0x55c06fa1d433: file src/pool.c, line 77.

Thread 4 hit Breakpoint 1, worker (arg=0x55c074530010) at src/pool.c:77
77				p->queue->completed++;
#0  worker (arg=0x55c074530010) at src/pool.c:77
#1  0x00007fda399f47d1 in start_thread (arg=<optimized out>) at pthread_create.c:454
#2  0x00007fda39a80b1c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
$1 = (struct queue *) 0x0
72			 * the bug. */
73			long result = run_job(job);
74			if (!p->stopping) {
75				printf("job %d done: %ld\n", job, result & 0xffff);
76				fflush(stdout);
77				p->queue->completed++;
78			}
79		}
80	}
81
[Inferior 1 (process 1) detached]

The breakpoint is one of the CPU's debug registers, so the fork runs unchanged until line 77 with p->queue null. Without --, gdb stays open for you to type into.

Replay it

$ rewind replay 11d4fbb7
identical to the recording: 1730 events, 1147 context switches and 1476 read and write records over 5029 steps, and the whole machine at its 4 keyframes

$ rewind replay 11d4fbb7 --from 4784
identical to the recording from the keyframe at step 256 to the end, and the whole machine at its 3 keyframes (0.53s)

--from starts at the nearest keyframe before the step. A keyframe is a copy of the VM's whole state, registers and memory, that recording saves every so many steps, so a replay can start there instead of at boot. A run replays on any machine with the same CPU vendor that has the features of the run's CPU model.

Fork it

A fork is its parent up to a step, then another schedule:

$ rewind fork 631eccb8 4784 --schedule 1 --quiet
rewind: run fac516809f2a33ea exited:2 after 5042 steps, 0.201s virtual, 0.056s wall (poweroff)
rewind: the fork first differs from its parent at step 4788
rewind: open it beside its parent in the desktop app: rewind open fac516809f2a33ea 4788 --compare 631eccb8b884c543

$ rewind fork 631eccb8 4784 --schedule 2 --quiet
rewind: run 1fcbc0bd80fc7f25 exited:0 after 6562 steps, 0.219s virtual, 0.100s wall (poweroff)
rewind: the fork first differs from its parent at step 4798
rewind: open it beside its parent in the desktop app: rewind open 1fcbc0bd80fc7f25 4798 --compare 631eccb8b884c543

$ rewind fork 631eccb8 4784 --schedule 3 --quiet
rewind: run 955fc449fa964017 exited:0 after 6593 steps, 0.224s virtual, 0.100s wall (poweroff)
rewind: the fork first differs from its parent at step 4798
rewind: open it beside its parent in the desktop app: rewind open 955fc449fa964017 4798 --compare 631eccb8b884c543

$ rewind fork 631eccb8 4784 --schedule 4 --quiet
rewind: run 44e9c34aaf2bd992 exited:0 after 6485 steps, 0.219s virtual, 0.100s wall (poweroff)
rewind: the fork first differs from its parent at step 4788
rewind: open it beside its parent in the desktop app: rewind open 44e9c34aaf2bd992 4788 --compare 631eccb8b884c543

From step 4784 of the schedule 0 build, schedule 1 crashes and schedules 2, 3 and 4 pass. Of schedules 1 to 32 from that step, 4 crash. check --run asks many schedules at once; the advanced tutorial shows it.

Scrub it in the app

The desktop app shows a run on a timeline: drag the playhead to any step for the output, processes, files and event there, jump to the failure or to where the run left the passing one, and fork from the playhead. Opened on check's two runs, a dashed blue mark on the timeline is step 4968, where only the failing run gets a request to switch threads.

The Rewind desktop app on a failing mylib run: the timeline with the playhead in the check phase, the build log, the process tree, files written, and the SIGSEGV at 0x108 next to where the run diverged from its parent
The app on a failing mylib run, compared with the passing run it was forked from.
$ rewind-app ~/.local/share/rewind/runs/11d4fbb7f2b822f5 --compare ~/.local/share/rewind/runs/c2edb2940353d54f

Press f to jump to the failure at step 4982, then s to open the source panel. After a few seconds it shows worker at src/pool.c:77, with p->queue->completed++; marked: the line that read the queue after pool_shutdown had set it to NULL. The t key shows which thread held the CPU around where the two runs part.

Fix it and check the fix

Clone the repository to edit the flake's mylib:

$ git clone https://github.com/fzakaria/rewindvm
$ cd rewindvm

In examples/mylib/src/pool.c, count the job under the lock, and free the queue after the workers are joined:

@@ -71,11 +71,13 @@
 		 * stopping. The check and the count are not under the lock:
 		 * the bug. */
 		long result = run_job(job);
+		pthread_mutex_lock(&p->lock);
 		if (!p->stopping) {
 			printf("job %d done: %ld\n", job, result & 0xffff);
 			fflush(stdout);
 			p->queue->completed++;
 		}
+		pthread_mutex_unlock(&p->lock);
 	}
 }

@@ -120,12 +122,10 @@
 	pthread_cond_broadcast(&p->ready);
 	pthread_mutex_unlock(&p->lock);

-	/* The bug: the queue goes before the workers are joined. */
-	free(p->queue);
-	p->queue = NULL;
-
 	for (int i = 0; i < POOL_WORKERS; i++)
 		pthread_join(p->workers[i], NULL);
+	free(p->queue);
+	p->queue = NULL;
 	pthread_cond_destroy(&p->ready);
 	pthread_mutex_destroy(&p->lock);
 	free(p);
$ rewind check --all --epoch 1790985600 .#mylib
...
0 of 64 other schedules ended differently
same result under all 65 schedules

Before the fix, 18 of the same 64 schedules crashed.

Limits

The VM has one vCPU, so threads interleave but never run at the same instant: a race between plain loads and stores with no system call between them is out of reach, while races across a system call, a lock or a sleep, like this one, are in reach. Design has the full list.

Get Rewind VM

The engine is free and MIT licensed. The desktop app is $49 personal or $99 per seat.