|
| 1 | +#!/bin/sh |
| 2 | +# This file is part of KASLD - https://github.com/bcoles/kasld |
| 3 | +# |
| 4 | +# check-test-staging — a test binary stages its filesystem through |
| 5 | +# test_sysroot.h, and that header removes what it made. |
| 6 | +# |
| 7 | +# A test that consults a fact source needs a tree to point KASLD_SYSROOT at, and |
| 8 | +# for a long time each one built its own: mkdtemp, a few mkdir calls, a private |
| 9 | +# write_file. Fifteen binaries carried a copy, and eleven of them removed |
| 10 | +# nothing, so every passing suite run left a tree per binary under /tmp to |
| 11 | +# accumulate indefinitely. Nothing failed. A leak that only ever costs disk is |
| 12 | +# invisible to a green suite, which is why it ran for so long and why it needs a |
| 13 | +# guard rather than a fix. |
| 14 | +# |
| 15 | +# The fix was one owner: test_sysroot.h makes the root, names it after the |
| 16 | +# binary, and registers its own removal. This keeps that the only owner. |
| 17 | +# |
| 18 | +# 1. mkdtemp() is called in test_sysroot.h and nowhere else. A second caller |
| 19 | +# is a second root with no owner -- the shape that leaked. |
| 20 | +# 2. KASLD_SYSROOT is set in test_sysroot.h and nowhere else. Pointing the I/O |
| 21 | +# layer at a directory the header did not make escapes its lifetime just as |
| 22 | +# completely, without naming mkdtemp. |
| 23 | +# 3. The header registers removal. Both checks above pass against a header |
| 24 | +# that never cleans up, which is what the tree looked like before. |
| 25 | +# |
| 26 | +# WHAT THIS DOES NOT PROVE: that no test leaks. A test can still create a file |
| 27 | +# by any other means; the guard names the three ways the tree actually leaked. |
| 28 | +# The measurement that settles it is a `make test` bracketed by a count of |
| 29 | +# /tmp/kasld* -- which is how the leak was found, and is not something grep can |
| 30 | +# do. |
| 31 | +# |
| 32 | +# Pure text, so it needs no build. |
| 33 | +# --- |
| 34 | +# <bcoles@gmail.com> |
| 35 | + |
| 36 | +set -u |
| 37 | +ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) |
| 38 | +TESTS="$ROOT/tests" |
| 39 | +HDR="$TESTS/test_sysroot.h" |
| 40 | + |
| 41 | +if [ -t 1 ]; then |
| 42 | + RED=$(printf '\033[31m') |
| 43 | + GREEN=$(printf '\033[32m') |
| 44 | + RESET=$(printf '\033[0m') |
| 45 | +else |
| 46 | + RED= |
| 47 | + GREEN= |
| 48 | + RESET= |
| 49 | +fi |
| 50 | + |
| 51 | +fail=0 |
| 52 | +note() { |
| 53 | + printf '%scheck-test-staging: FAIL%s — %s\n' "$RED" "$RESET" "$1" >&2 |
| 54 | + fail=1 |
| 55 | +} |
| 56 | + |
| 57 | +[ -f "$HDR" ] || { |
| 58 | + note "test_sysroot.h is missing" |
| 59 | + exit 1 |
| 60 | +} |
| 61 | + |
| 62 | +# --- 1 & 2. one owner of the root ------------------------------------------- |
| 63 | +# The CALL, not the word: two tests name mkdtemp in a feature-test-macro comment |
| 64 | +# because they include this header, and those are accurate. |
| 65 | +n=0 |
| 66 | +for f in "$TESTS"/*.c "$TESTS"/*.h; do |
| 67 | + [ -f "$f" ] || continue |
| 68 | + [ "$f" = "$HDR" ] && continue |
| 69 | + n=$((n + 1)) |
| 70 | + if grep -q 'mkdtemp(' "$f"; then |
| 71 | + note "$(basename "$f") calls mkdtemp(); stage through th_sysroot_init() so the tree is removed" |
| 72 | + fi |
| 73 | + if grep -q 'setenv("KASLD_SYSROOT"' "$f"; then |
| 74 | + note "$(basename "$f") sets KASLD_SYSROOT itself; th_sysroot_init() owns the root and its lifetime" |
| 75 | + fi |
| 76 | +done |
| 77 | + |
| 78 | +# --- 3. the owner actually cleans up ---------------------------------------- |
| 79 | +# Checks 1 and 2 are satisfied by a header that leaks exactly as the copies did, |
| 80 | +# so the removal is asserted rather than assumed. |
| 81 | +init=$(sed -n '/^static void th_sysroot_init/,/^}/p' "$HDR") |
| 82 | +printf '%s\n' "$init" | grep -q 'atexit(th_sysroot_fini)' || |
| 83 | + note "th_sysroot_init() does not register th_sysroot_fini(); a passing run would leave its tree" |
| 84 | + |
| 85 | +sed -n '/^static void th_sysroot_fini/,/^}/p' "$HDR" | grep -q 'rmdir(th_sysroot_root)' || |
| 86 | + note "th_sysroot_fini() does not remove the root directory" |
| 87 | + |
| 88 | +if [ "$fail" -ne 0 ]; then |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | +printf '%scheck-test-staging: OK%s (%s test files stage through test_sysroot.h, which removes its tree)\n' \ |
| 92 | + "$GREEN" "$RESET" "$n" |
0 commit comments