Debian Codegraph @

util/check-common.sh

raw
1 #!/bin/bash
2 #
3 # Shared setup for the util/check-*.sh syscall verification scripts
4 # (check-safe-traversal.sh, check-toctou.sh, check-libc-interposition.sh).
5 #
6 # Source it after setting CHECK_UTILS to the utilities the caller exercises:
7 #
8 # CHECK_UTILS="mkfifo touch head"
9 # . "$(dirname "${BASH_SOURCE[0]}")/check-common.sh"
10 #
11 # It provides $PROJECT_ROOT, a $TEMP_DIR cleaned up on exit, fail_immediately(),
12 # require_command(), and util_cmd()/have_util() to resolve a utility to a
13 # runnable command whether the tree was built as individual binaries or as the
14 # multicall binary.
15
16 : "${PROFILE:=release-small}"
17 export PROFILE
18
19 PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
20 BIN_DIR="$PROJECT_ROOT/target/$PROFILE"
21 TEMP_DIR=$(mktemp -d)
22
23 cleanup() {
24 rm -rf "$TEMP_DIR"
25 }
26 trap cleanup EXIT
27
28 fail_immediately() {
29 echo "❌ FAILED: $1"
30 # Only the strace-based checks leave logs behind; mention them if they exist.
31 if compgen -G "$TEMP_DIR/strace_*.log" >/dev/null; then
32 echo ""
33 echo "Debug information available in: $TEMP_DIR/strace_*.log"
34 fi
35 exit 1
36 }
37
38 require_command() {
39 if ! command -v "$1" >/dev/null 2>&1; then
40 echo "Error: $1 is required to run these checks"
41 exit 1
42 fi
43 }
44
45 # Prefer individual binaries: they are what CI builds, and tracing them avoids
46 # the multicall dispatch noise. Fall back to the multicall binary otherwise.
47 detect_binaries() {
48 local util
49 for util in $CHECK_UTILS; do
50 if [ -f "$BIN_DIR/$util" ]; then
51 echo "Using individual binaries"
52 USE_MULTICALL=0
53 return
54 fi
55 done
56
57 if [ -f "$BIN_DIR/coreutils" ]; then
58 echo "Using multicall binary"
59 USE_MULTICALL=1
60 COREUTILS_BIN="$BIN_DIR/coreutils"
61 MULTICALL_UTILS=$("$COREUTILS_BIN" --list)
62 return
63 fi
64
65 echo "Error: No binaries found. Please build first with 'cargo build --profile=$PROFILE'"
66 exit 1
67 }
68
69 # Resolve a utility to a runnable command, or return 1 when it was not built.
70 # A multicall binary built without the unix feature set has no chmod or chown
71 # in it, so ask it what it actually dispatches.
72 util_cmd() {
73 local util="$1"
74 if [ "$USE_MULTICALL" -eq 1 ]; then
75 grep -qx "$util" <<<"$MULTICALL_UTILS" || return 1
76 echo "$COREUTILS_BIN $util"
77 else
78 [ -f "$BIN_DIR/$util" ] || return 1
79 echo "$BIN_DIR/$util"
80 fi
81 }
82
83 have_util() {
84 util_cmd "$1" >/dev/null
85 }
86
87 detect_binaries