Files
lotherk 21287b88a9 agent: free the reply of a terminal failure, and make the tests leak-clean
ASAN over the unit tests found a real leak the normal gate cannot see: a
transport failure that is not retried (the fourth 500, a 4xx, a dead stream)
returned -1 with the last reply's body and calls still allocated. The caller
only reads the reply on success, so it leaked on every terminal failure -- a
long-running daemon with a flapping provider grows by one reply per failure.
test_agent's four-500 case leaked exactly the last "boom"; it is clean now.

The same run showed the tests leaking their own fixtures: test_anthropic and
test_tasks never freed the registry they filled, test_cli forgot host4 and
host5, test_confblock forgot its error buffer, test_mcpclient its registries.
Fixed; all 39 unit binaries are now clean under ASAN+UBSAN with leak detection.

That gate is `make check-asan`: it rebuilds the tree with the sanitizers, runs
the unit binaries with LSAN, and rebuilds normally afterwards. Three MCP-plugin
teardown allocations are suppressed in tests/asan.supp: its server thread is
detached, shutdown kills the child but cannot join the thread, so the line
buffer (bounded at RPC_MAX_LINE) and the tools registered by that thread can
outlive the host. Bounded, teardown-only, tracked separately.

make check: all tests passed. make check-asan: all 39 test binaries clean.
2026-11-18 23:16:26 +00:00

52 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# The unit tests under AddressSanitizer + UndefinedBehaviorSanitizer.
#
# `make check` proves behaviour; this proves the memory the behaviour runs on.
# It found two real defects the normal run cannot see: the agent never freed
# the reply of a terminal transport failure, and the mock transport leaked a
# queued body. Both are fixed; the remaining suppressions are the MCP plugin's
# teardown allocations (tests/asan.supp explains why).
#
# The whole tree is rebuilt with sanitizers and then rebuilt normally, because
# the Makefile builds in place. Run it when the tree is idle.
set -e
cd "$(dirname "$0")/.."
ACF="-std=c99 -O1 -g -Wall -Wextra -fPIC -D_POSIX_C_SOURCE=200809L \
-fsanitize=address,undefined -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="$ACF" all $TESTS > /tmp/check_asan_build.log 2>&1; then
tail -20 /tmp/check_asan_build.log >&2
echo "asan build failed" >&2
exit 1
fi
failed=0
for t in $TESTS; do
if ! ASAN_OPTIONS=detect_leaks=1:abort_on_error=1 \
UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 \
LSAN_OPTIONS=suppressions=tests/asan.supp \
LD_LIBRARY_PATH=. "./$t" > /tmp/check_asan_one.log 2>&1; then
echo "FAIL $t"
tail -20 /tmp/check_asan_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_asan: all $(echo "$TESTS" | wc -w) test binaries clean"
else
echo "check_asan: FAILURES" >&2
fi
exit "$failed"