#!/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"