#!/bin/sh # x86emu-selftest — does this machine run x86-64 Linux programs? # # Runs every x86-64 test program staged under /usr/libexec/x86emu/tests (the # list is tests/LIST, written at build time) plus a few checks of the staged # x86-64 busybox, and compares each one's stdout + exit status with the output # the SAME binary produced natively on an x86-64 build host # (/usr/libexec/x86emu/expected). Every test is started by direct exec, so it # goes through the kernel's x86-64 exec hook exactly as a user's program # would; --via-blink starts each one as `/usr/bin/blink ` instead, for a # kernel that does not have the hook yet. # # Output: one `PASS ` or `FAIL ` line per test, then # x86emu-selftest: / PASS # Exit 0 iff every selected test passed; 1 if any failed; 2 on a usage error # or when the suite itself is not there (never a vacuous pass: zero selected # tests is an error, and a test listed in LIST whose binary or expected output # is missing is a FAIL, not a skip). # # PORTABILITY IS THE POINT. The same file runs under busybox ash (the # musl-busybox rig), bash 5.2 (the two bash rigs) and dash (the build host, # --record), and the bash rigs' userland is coreutils: there is no diff, cmp, # grep, sed or awk on them. So: POSIX sh only, outputs compared line by line # with `read` (no fork per comparison — under emulation every fork is costly), # and the only external commands are mktemp, rm and timeout, plus sha256sum # for the integrity check and date for -t. All three rigs ship each of them # (/usr/bin applets on musl-busybox, coreutils in /bin on the bash rigs). # # Usage: x86emu-selftest [-v] [-t] [-k] [--via-blink] [--list] [PATTERN...] # PATTERN shell glob(s) over test names, e.g. 'musl-*/t_hello' 'busybox/*' # -v on failure, show the differing lines and the test's stderr # -t print `TIME ` after each test # -k keep the scratch directory even when everything passed # --via-blink run each test as "$X86EMU_BLINK " (default /usr/bin/blink) # --list print the selected test names, then `x86emu-selftest: tests`, # and exit (a gate reads N to know how many PASS lines to demand) # Environment: # X86EMU_TIMEOUT per-test timeout in seconds (default 120; 0 = unbounded). # A test still running then gets SIGTERM, and SIGKILL 5 s # later, so a hung test is a FAIL (exit=124 or 137), never a # hung selftest. Needs timeout(1): without it the selftest # refuses unless X86EMU_TIMEOUT=0 says unbounded on purpose. # X86EMU_BLINK the emulator for --via-blink (default /usr/bin/blink) # X86EMU_LIBEXEC where the suite lives (default /usr/libexec/x86emu); the # build uses this, together with --record DIR, to produce # the expected outputs natively from the same invocations. # Relative paths (here, in X86EMU_BLINK and in --record) are # taken against the directory the selftest was started in. # The expected outputs were recorded under umask 022 (t_file prints the mode # of a file it creates 0644); a caller's 027 or 077 turned a correct emulator # into a t_file FAIL in every variant (verified: mode=640, mode=600). umask 022 ME=x86emu-selftest # abspath P — P against the starting directory. The selftest cd's into its # scratch directory before running anything, so a relative X86EMU_LIBEXEC made # every test FAIL as "missing" and a relative --record DIR wrote nothing and # still exited 0. $PWD is the shell's own (every POSIX shell sets it at # startup); pwd only if something unset it. here=${PWD:-$(pwd)} abspath() { case $1 in /*) printf '%s' "$1" ;; *) printf '%s/%s' "$here" "$1" ;; esac } LIBEXEC=$(abspath "${X86EMU_LIBEXEC:-/usr/libexec/x86emu}") TESTS=$LIBEXEC/tests EXPECTED=$LIBEXEC/expected BB=$LIBEXEC/bin/busybox BLINK=${X86EMU_BLINK:-/usr/bin/blink} # A bare name is a PATH lookup and works from any directory; only a relative # path with a slash in it depends on the cwd. case $BLINK in /*) ;; */*) BLINK=$(abspath "$BLINK") ;; esac # 120 s per test is headroom, not a budget: in-guest under Blink on all three # rigs (2026-10-28) the slowest single test took 15 s (a t_thread) and all 68 # checks 83-104 s. All 100 checks: 64-143 s over 18 runs (Blink be85ef7 and # 2cb66a4, one rig at a time and three at once), the slowest test a t_thread # at up to 20 s; one more run took 212 s, a glibc t_cxx hanging until its 90 s # timeout — Blink stored a new thread's CLONE_PARENT_SETTID word after the # thread could already have exited and cleared it, so pthread_join waited # forever (upstream since f006a4f; fixed in Blink be6b8c7: 0 of 1000 runs # hung). It was 600 s, so one hang cost ten minutes. TIMEOUT=${X86EMU_TIMEOUT:-120} case $TIMEOUT in '' | *[!0-9]*) printf '%s: X86EMU_TIMEOUT=%s is not a whole number of seconds\n' "$ME" "$TIMEOUT" >&2 exit 2 ;; esac verbose=0 times=0 keep=0 viablink=0 list=0 record= usage() { printf 'usage: %s [-v] [-t] [-k] [--via-blink] [--list] [--record DIR] [PATTERN...]\n' "$ME" } while [ $# -gt 0 ]; do case $1 in -v) verbose=1 ;; -t) times=1 ;; -k) keep=1 ;; --via-blink) viablink=1 ;; --list) list=1 ;; --record) [ $# -ge 2 ] && [ -n "$2" ] || { usage >&2; exit 2; } shift; record=$(abspath "$1") ;; -h | --help) usage; exit 0 ;; --) shift; break ;; -*) printf '%s: unknown option %s\n' "$ME" "$1" >&2; usage >&2; exit 2 ;; *) break ;; esac shift done [ -f "$TESTS/LIST" ] || { printf '%s: no test list at %s/LIST — the x86emu runtime is not staged in this image\n' "$ME" "$TESTS" >&2 exit 2 } # The busybox checks: a real-world static x86-64 program, and the only part of # the suite that has an x86-64 process fork and exec OTHER x86-64 processes # (the pipeline), which is the path a user's shell script takes. BB_CHECKS="busybox/sh-arith busybox/uname busybox/pipeline busybox/sha256" # The integrity check: every file the build staged, against its own manifest. # It needs the image's root as /, so it is not part of a --record run. INTEGRITY=runtime/sha256sums selected() { # $1 = name; true if it matches any PATTERN (all when none given) [ -z "$patterns_given" ] && return 0 for p in $patterns; do # Unquoted on purpose: the pattern is a glob. case $1 in $p) return 0 ;; esac done return 1 } # Patterns are globs; keep them from being expanded against the cwd. set -f patterns=$* patterns_given=${1+x} names= while IFS= read -r n || [ -n "$n" ]; do case $n in '' | '#'*) continue ;; esac selected "$n" && names="$names $n" done <"$TESTS/LIST" for n in $BB_CHECKS; do selected "$n" && names="$names $n"; done if [ -z "$record" ]; then selected "$INTEGRITY" && names="$names $INTEGRITY"; fi set +f if [ -z "$names" ]; then printf '%s: no test matches %s\n' "$ME" "$patterns" >&2 exit 2 fi if [ "$list" = 1 ]; then count=0 for n in $names; do printf '%s\n' "$n" count=$((count + 1)) done printf '%s: %d tests\n' "$ME" "$count" exit 0 fi # The prefix every test command runs under: [timeout -k 5 N] [blink]. # # -k 5 is what makes the bound real. Plain `timeout N` only sends SIGTERM and # then waits for the child however long it takes: a test that ignores SIGTERM # and blocks in pause() hung the whole selftest — zero lines printed — under # coreutils timeout, busybox timeout and no timeout alike (the verifier's # repro, X86EMU_TIMEOUT=3, killed from outside at 25 s). With -k the child # gets SIGKILL 5 s after the SIGTERM; both coreutils and busybox 1.37 take it. # Measured with -k: that program is one FAIL (exit=137) and the run goes on — # 8.0 s natively under coreutils timeout, 8.8 s under busybox's (TIMEOUT=3), # 7 s in-guest on each of the three rigs (TIMEOUT=2). use_timeout=0 if [ "$TIMEOUT" != 0 ]; then command -v timeout >/dev/null 2>&1 || { printf '%s: no timeout(1) in PATH, so a hung test would hang the selftest; set X86EMU_TIMEOUT=0 to run every test unbounded on purpose\n' "$ME" >&2 exit 2 } use_timeout=1 fi if [ -n "$record" ]; then mkdir -p "$record" || exit 2 W=$record else W=$(mktemp -d /tmp/x86emu-selftest.XXXXXX) || { printf '%s: cannot create a scratch directory under /tmp\n' "$ME" >&2 exit 2 } fi cd "$W" || exit 2 # run_test NAME OUT ERR — run one test, stdout to OUT (+ an `exit=N` line), # stderr to ERR. The invocation shapes here are the ONLY place they are # written down: the build runs this same function (via --record) to produce # the expected outputs, so the two cannot drift apart. run_test() { name=$1 out=$2 err=$3 case $name in busybox/*) prog=$BB ;; *) prog=$TESTS/$name ;; esac if [ ! -f "$prog" ]; then printf 'missing: %s\n' "$prog" >"$err" printf 'exit=missing\n' >"$out" return fi set -- [ "$use_timeout" = 1 ] && set -- timeout -k 5 "$TIMEOUT" [ "$viablink" = 1 ] && set -- "$@" "$BLINK" case $name in */t_args) X86T=hello "$@" "$prog" a "b c" fork+exec x86 seq | fork+exec x86 awk, all by absolute path # (PATH in the image finds the wasm tools, which is not what is tested). busybox/pipeline) "$@" "$prog" sh -c '"$0" seq 1 1000 | "$0" awk "{ s += \$1 } END { print s }"' "$prog" "$out" 2>"$err" printf 'exit=%s\n' "$?" >>"$out" } # same A B — true iff files A and B hold the same lines. Pure shell (read on # two descriptors), so a comparison costs no process. same() { exec 3<"$1" 4<"$2" r=0 while :; do IFS= read -r a <&3; ea=$? IFS= read -r b <&4; eb=$? if [ $ea -ne 0 ] || [ $eb -ne 0 ]; then { [ $ea -eq $eb ] && [ "$a" = "$b" ]; } || r=1 break fi [ "$a" = "$b" ] || { r=1; break; } done exec 3<&- 4<&- return $r } # show_diff EXPECTED GOT — the differing lines, numbered; diff(1) is not # available on every rig. show_diff() { exec 3<"$1" 4<"$2" i=0 while :; do i=$((i + 1)) IFS= read -r a <&3; ea=$? IFS= read -r b <&4; eb=$? [ $ea -ne 0 ] && [ $eb -ne 0 ] && [ -z "$a" ] && [ -z "$b" ] && break [ $ea -ne 0 ] && [ -z "$a" ] && a='' [ $eb -ne 0 ] && [ -z "$b" ] && b='' if [ "$a" != "$b" ]; then printf ' line %d: expected [%s]\n' "$i" "$a" printf ' line %d: got [%s]\n' "$i" "$b" fi [ $ea -ne 0 ] && [ $eb -ne 0 ] && break done exec 3<&- 4<&- } # expected_for NAME — busybox/foo -> busybox-foo.out; /t_x -> t_x.out # (a test's output is the same for every libc and link mode; the build refuses # to stage the suite when that is not true natively). expected_for() { case $1 in busybox/*) printf '%s/busybox-%s.out' "$EXPECTED" "${1#busybox/}" ;; *) printf '%s/%s.out' "$EXPECTED" "${1#*/}" ;; esac } now() { date +%s 2>/dev/null || echo 0; } pass=0 total=0 unrecorded=0 for name in $names; do total=$((total + 1)) # Every name is /; flatten it without a fork. safe=${name%%/*}.${name#*/} out=$W/$safe.out err=$W/$safe.err [ "$times" = 1 ] && t0=$(now) if [ "$name" = "$INTEGRITY" ]; then # Paths in the manifest are ./-relative to the image root. set -- [ "$use_timeout" = 1 ] && set -- timeout -k 5 "$TIMEOUT" if ( cd / && "$@" sha256sum -c "$LIBEXEC/SHA256SUMS" ) >"$out" 2>"$err"; then ok=1; else ok=0; fi else run_test "$name" "$out" "$err" if [ -n "$record" ]; then # A recording that did not land (an unwritable DIR) is an error, # not a quiet empty expected output. last= if [ -f "$out" ]; then while IFS= read -r l; do last=$l; done <"$out" fi case $last in exit=[0-9]*) printf 'RECORD %s %s\n' "$name" "$out" ;; *) printf '%s: recording %s into %s failed\n' "$ME" "$name" "$out" >&2 unrecorded=$((unrecorded + 1)) ;; esac continue fi exp=$(expected_for "$name") if [ -f "$exp" ] && same "$exp" "$out"; then ok=1; else ok=0; fi fi [ "$times" = 1 ] && printf 'TIME %s %s\n' "$name" "$(($(now) - t0))" if [ "$ok" = 1 ]; then pass=$((pass + 1)) printf 'PASS %s\n' "$name" else printf 'FAIL %s\n' "$name" if [ "$verbose" = 1 ]; then if [ "$name" = "$INTEGRITY" ]; then # Only the lines that are not OK. while IFS= read -r l; do case $l in *': OK') ;; *) printf ' %s\n' "$l" ;; esac done <"$out" elif [ ! -f "$exp" ]; then printf ' no expected output %s\n' "$exp" else show_diff "$exp" "$out" fi if [ -s "$err" ]; then printf ' stderr:\n' while IFS= read -r l || [ -n "$l" ]; do printf ' %s\n' "$l"; done <"$err" fi fi fi done if [ -n "$record" ]; then [ "$unrecorded" = 0 ] || exit 1 exit 0 fi printf '%s: %d/%d PASS\n' "$ME" "$pass" "$total" if [ "$pass" = "$total" ] && [ "$keep" = 0 ]; then cd / && rm -rf "$W" else printf '%s: outputs kept in %s\n' "$ME" "$W" fi [ "$pass" = "$total" ]