Files
lotherk 80f055711d races: five data races fixed, make check-tsan to keep them fixed
Found with ThreadSanitizer over the whole suite, not by reading:

- ack: sp_ack_dir() returned one static buffer. The waiter and the
  answerer (the gateway is both) sprintf'd into each other's path, so an
  approval request could be looked for in the wrong place. One buffer per
  thread now; test_ack asserts the two threads get different addresses.
- mcp: `stopping` was a plain volatile int with no happens-before, so a
  server thread could read 0 after shutdown killed its child and keep
  using buffers that teardown frees. Acquire/release accessors.
- foreign: fr.ready was set before the paths it guards were filled, so a
  second thread returned early and read empty paths; the flag and the host
  handle are now an atomic pair (publish) plus a mutex (the init itself).
- embed: catch_up() read c->path, c->offset and c->id outside the lock. A
  session switch mid-index published the old file's byte offset into the
  new session's index - everything below that offset was never searched
  again. A generation counter makes that run drop its batch, and the
  search path takes its snapshot under the same lock.

Proof, both directions:
- make check-tsan: 45/45 test binaries free of races (new target; the
  tree is rebuilt under TSan and back, like check-asan).
- test_embed's new gated test (a server that holds the batch until the
  session has been switched): without the generation check short.emb ends
  with 4 records, 1 of them leaked from the other session; with it, 1
  and 0. The test is deterministic, so it fails on a plain build too.
- test_ack: reverting to `static char root[]` fails
  "the two threads do not share one buffer".
- make check: all tests passed; make check-asan: 45/45 clean.
2026-11-23 08:30:29 +00:00

73 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# The unit tests under ThreadSanitizer.
#
# `make check` proves behaviour and `make check-asan` proves the memory it runs
# on; this proves the threads. Splinter has more of them than it looks: the turn
# thread, the UI thread, one widget thread per plugin that asks for one, and one
# thread per MCP server. Between them sit process globals that were written by
# whichever thread got there first.
#
# It found these real data races, all fixed: sp_ack_dir() returned one static
# buffer that a waiter and an answerer sprintf'd into each other, the MCP
# plugin's shutdown flag was a plain `volatile int` with no happens-before, the
# foreign plugin set its init flag before filling the paths it guards, and that
# plugin's host handle was written from two threads unsynchronised. The fifth
# was the expensive one: catch_up() read c->path, c->offset and c->id outside
# the lock, so a session switch mid-index published the old file's byte offset
# into the new session's index - everything below that offset was never
# searched again. A generation counter now makes that run drop its batch.
#
# A race is not deterministic, so this is not part of `make check`: it costs a
# full rebuild, and it needs a toolchain with TSan (Debian's gcc has it). Run it
# when the tree is idle - like check-asan, it rebuilds in place and back.
set -e
cd "$(dirname "$0")/.."
TCF="-std=c99 -O1 -g -Wall -Wextra -fPIC -D_POSIX_C_SOURCE=200809L \
-fsanitize=thread -fno-omit-frame-pointer \
-Iinclude -Isrc/backend -Isrc/frontend -Ivendor/libmcp \
-Ivendor/netbsd-curses/libcurses"
TESTS=$(make -s print-tests)
[ -n "$TESTS" ] || { echo "no test binaries to run" >&2; exit 1; }
make clean >/dev/null
if ! make -j"$(nproc)" CFLAGS="$TCF" all $TESTS > /tmp/check_tsan_build.log 2>&1; then
tail -20 /tmp/check_tsan_build.log >&2
echo "tsan build failed" >&2
exit 1
fi
failed=0
races=0
for t in $TESTS; do
if TSAN_OPTIONS=halt_on_error=1:second_deadlock_stack=1 \
LD_LIBRARY_PATH=. "./$t" > /tmp/check_tsan_one.log 2>&1; then
continue
fi
# A test can fail for its own reasons under TSan's slowdown; only a
# report counts here. Anything else is reported, not counted as a race.
if grep -q "WARNING: ThreadSanitizer" /tmp/check_tsan_one.log; then
echo "RACE $t"
grep -A 14 "WARNING: ThreadSanitizer" /tmp/check_tsan_one.log |
sed 's/^/ /'
races=$((races + 1))
failed=1
else
echo "FAIL $t (not a race report)"
tail -10 /tmp/check_tsan_one.log | sed 's/^/ /'
failed=1
fi
done
# leave the tree as it was found: a normal build
make clean >/dev/null
make -j"$(nproc)" >/dev/null
if [ "$failed" -eq 0 ]; then
echo "check_tsan: all $(echo "$TESTS" | wc -w) test binaries free of data races"
else
echo "check_tsan: $races race report(s)" >&2
fi
exit "$failed"