CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tools / runtime / clean-user-hello.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # clean-user-hello.sh BRANCH LOG [URL] (bar C2: a stranger runs hello-GPU from a clean machine) |
| 4 | # |
| 5 | # Creates a brand-new local Linux user (empty home, no ~/workspace, empty environment), runs ONLY the commands of |
| 6 | # the README "Run your first kernel" bash block of BRANCH as that user, and deletes the user again. Needs sudo; every |
| 7 | # privileged command is printed with a leading "+" before it runs. The whole README block runs under strace (as |
| 8 | # root, following the stranger's processes) so the log can show that no CUDA file was opened or executed; a red |
| 9 | # control (`nvcc --version` as the same user, same trace) shows the trace check can see one. |
| 10 | # |
| 11 | # The GPU is used only while holding the lock ($CUDAPP_GPU_LOCK, default /tmp/aien-gb10.lock). If $CUDAPP_BOARD |
| 12 | # names a file, GPU start and end lines are appended to it. Exit code: that of the README block (0 = every command |
| 13 | # passed), 3 = setup failure, 4 = the trace saw a CUDA file opened or executed, 5 = the trace red control failed. |
| 14 | set -uo pipefail |
| 15 | branch=${1:?usage: clean-user-hello.sh BRANCH LOG [URL]} |
| 16 | log=${2:?usage: clean-user-hello.sh BRANCH LOG [URL]} |
| 17 | url=${3:-https://github.com/aien-dev/CUDApp} |
| 18 | lock=${CUDAPP_GPU_LOCK:-/tmp/aien-gb10.lock} |
| 19 | board=${CUDAPP_BOARD:-} |
| 20 | user_created=0 |
| 21 | cd / || exit 3 |
| 22 | run() { echo "+ $*"; "$@"; } |
| 23 | note() { [ -n "$board" ] && echo "$(date -u +%FT%TZ) $*" >> "$board"; return 0; } |
| 24 | tr_dir=$(mktemp -d "${TMPDIR:-/tmp}/clean-user-trace.XXXXXX") || exit 3 |
| 25 | # Reuse the unique temporary-directory suffix instead of a clock-only name. |
| 26 | # useradd is the atomic ownership check: an existing account is never ours. |
| 27 | U=c2stranger${tr_dir##*.} |
| 28 | exec > >(tee "$log") 2>&1 |
| 29 | echo "# clean-user-hello: branch=$branch url=$url user=$U" |
| 30 | echo "# date: $(date -u +%FT%TZ) host: $(uname -srm) loadavg: $(cut -d' ' -f1-3 /proc/loadavg)" |
| 31 | echo "# operator: $(id -un) nvidia module: $(cat /sys/module/nvidia/version 2>/dev/null || echo none)" |
| 32 | cleanup() { |
| 33 | if sudo test -s "$tr_dir/readme.trace"; then sudo gzip -9c "$tr_dir/readme.trace" > "$log.strace.gz" && echo "# full trace of the README block kept: $log.strace.gz"; fi |
| 34 | if [ "$user_created" -eq 1 ]; then |
| 35 | echo "# --- delete the temporary user ---" |
| 36 | run sudo pkill -u "$U" || true |
| 37 | run sudo userdel -r "$U" 2>&1 | grep -v 'mail spool' || true |
| 38 | if getent passwd "$U" >/dev/null || [ -e "/home/$U" ]; then echo "# CLEANUP FAILED: $U still present"; else echo "# user $U and /home/$U removed"; fi |
| 39 | else |
| 40 | echo "# no account created by this run; account cleanup skipped" |
| 41 | fi |
| 42 | sudo rm -rf "$tr_dir" |
| 43 | } |
| 44 | trap cleanup EXIT |
| 45 | echo "# --- create a brand-new user ---" |
| 46 | run sudo useradd -m -s /bin/bash "$U" || exit 3 |
| 47 | user_created=1 |
| 48 | # the stranger's shell: an empty environment except what login(1) sets, then the distribution's login profile |
| 49 | S() { sudo -u "$U" env -i HOME="/home/$U" USER="$U" LOGNAME="$U" SHELL=/bin/bash TERM=dumb LANG=C.UTF-8 PATH=/usr/local/bin:/usr/bin:/bin bash -lc "cd ~; $1"; } |
| 50 | echo "# new user:" |
| 51 | S 'echo "# id: $(id)"; echo "# home: [$(ls -A ~ | tr "\n" " ")]"; [ -e ~/workspace ] && echo "# ~/workspace EXISTS" || echo "# ~/workspace absent"' |
| 52 | echo "# environment after login (env | sort):" |
| 53 | S 'env | sort | sed "s/^/# /"' |
| 54 | echo "# --- the README of $branch, as a stranger reads it on GitHub ---" |
| 55 | readme=$(curl -fsSL "https://raw.githubusercontent.com/aien-dev/CUDApp/$branch/README.md") || { echo "FAIL: cannot fetch the README"; exit 3; } |
| 56 | cmds=$(printf '%s\n' "$readme" | awk '/^## Run your first kernel/{s=1;next} s&&/^```bash/{b=1;next} s&&b&&/^```/{exit} s&&b{print}') |
| 57 | [ -n "$cmds" ] || { echo "FAIL: no bash block under 'Run your first kernel'"; exit 3; } |
| 58 | echo "# README commands, verbatim:"; printf '%s\n' "$cmds" | sed 's/^/# /' |
| 59 | # the one change: the clone takes the branch under test (none for main) |
| 60 | [ "$branch" = main ] || cmds=$(printf '%s\n' "$cmds" | sed -E "s#^git clone #git clone --branch $branch #") |
| 61 | script=$(printf 'set -e\nmkdir empty && cd empty\necho "# cwd: $(pwd), contents: [$(ls -A)]"\n' |
| 62 | printf '%s\n' "$cmds" | awk -v sq="'" '{ q = $0; gsub(sq, sq "\\" sq sq, q); print "echo " sq "$ " q sq; print $0 |
| 63 | if ($0 ~ /^cd /) print "echo \"# commit: $(git rev-parse HEAD)\"" }') |
| 64 | # traced: execve and every file open, all processes of the stranger, recorded by root |
| 65 | T() { sudo strace -f -qq -e trace=execve,openat -o "$2" sudo -u "$U" env -i HOME="/home/$U" USER="$U" LOGNAME="$U" SHELL=/bin/bash TERM=dumb LANG=C.UTF-8 PATH=/usr/local/bin:/usr/bin:/bin bash -lc "cd ~; $1"; } |
| 66 | # a CUDA file that was really opened or executed: a successful call (no "= -1") on the CUDA toolkit tree, a CUDA or |
| 67 | # NVIDIA JIT user-space library, or an nvcc. The zero-CUDA gate's own red control builds a stub libcudart.so in a |
| 68 | # mktemp directory (/tmp/tmp.*) on purpose; those calls are counted apart and are not a CUDA use. |
| 69 | CUDA_RE='"(/usr/local/cuda[^"]*|/opt/nvidia[^"]*|[^"]*/(libcuda|libcudart|libcublas|libcudnn|libnvrtc|libnvJitLink|libnvidia-ptxjitcompiler)[^"/]*|[^"]*/nvcc)"' |
| 70 | cuda_hits() { sudo grep -E '(execve|openat)\(' "$1" | grep -vE '= -1 ' | grep -E "$CUDA_RE" | grep -vE '"/tmp/tmp\.[A-Za-z0-9]+/' || true; } |
| 71 | stub_hits() { sudo grep -E '(execve|openat)\(' "$1" | grep -vE '= -1 ' | grep -E "$CUDA_RE" | grep -cE '"/tmp/tmp\.[A-Za-z0-9]+/' || true; } |
| 72 | echo "# --- run the README block as $U, holding $lock ---" |
| 73 | note "GPU-START clean-user-hello $U branch=$branch (flock $lock)" |
| 74 | t0=$(date +%s) |
| 75 | echo "+ flock -w 900 $lock strace -f ... sudo -u $U (README block)" |
| 76 | export -f T; export U |
| 77 | flock -w 900 "$lock" bash -c 'T "$1" "$2"' _ "$script" "$tr_dir/readme.trace" |
| 78 | rc=$? |
| 79 | echo "# README block exit code: $rc ($(( $(date +%s) - t0 )) s including the lock wait)" |
| 80 | note "GPU-END clean-user-hello $U rc=$rc, GPU released" |
| 81 | echo "# --- trace check: CUDA files opened or executed during the README block ---" |
| 82 | echo "# traced calls: $(sudo grep -cE '(execve|openat)\(' "$tr_dir/readme.trace"), programs executed: $(sudo grep -E 'execve\(' "$tr_dir/readme.trace" | grep -vcE '= -1 ')" |
| 83 | hits=$(cuda_hits "$tr_dir/readme.trace") |
| 84 | if [ -n "$hits" ]; then echo "# TRACE FAIL: CUDA files used:"; printf '%s\n' "$hits" | head -20 | sed 's/^/# /'; [ $rc -eq 0 ] && rc=4 |
| 85 | else echo "# TRACE PASS: no CUDA file (/usr/local/cuda*, libcuda*, libnvrtc*, nvcc, ...) was opened or executed"; fi |
| 86 | echo "# (the zero-CUDA gate's own stub libcudart in /tmp/tmp.*: $(stub_hits "$tr_dir/readme.trace") successful calls, its red control)" |
| 87 | echo "# --- trace red control: the same check on 'nvcc --version' run by the same user ---" |
| 88 | nvcc_bin=$(S 'command -v nvcc' 2>/dev/null); [ -n "$nvcc_bin" ] || { [ -x /usr/local/cuda/bin/nvcc ] && nvcc_bin=/usr/local/cuda/bin/nvcc; } |
| 89 | if [ -z "$nvcc_bin" ]; then echo "# TRACE RED CONTROL: NOT_RUN (no CUDA toolkit on this machine to trace)"; else |
| 90 | T "$nvcc_bin --version" "$tr_dir/red.trace" > /dev/null 2>&1 |
| 91 | red=$(cuda_hits "$tr_dir/red.trace") |
| 92 | if [ -n "$red" ]; then echo "# TRACE RED CONTROL: caught as it must ($(printf '%s\n' "$red" | wc -l) CUDA file uses, first: $(printf '%s\n' "$red" | head -1 | cut -c1-160))" |
| 93 | else echo "# TRACE RED CONTROL BROKEN: the run of $nvcc_bin was not seen"; [ $rc -eq 0 ] && rc=5; fi; fi |
| 94 | echo "# clean-user-hello exit code: $rc" |
| 95 | exit $rc |