interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tools / runtime / clean-user-hello.sh

95 lines · 7222 bytes Raw Blame History
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.
14set -uo pipefail
15branch=${1:?usage: clean-user-hello.sh BRANCH LOG [URL]}
16log=${2:?usage: clean-user-hello.sh BRANCH LOG [URL]}
17url=${3:-https://github.com/aien-dev/CUDApp}
18lock=${CUDAPP_GPU_LOCK:-/tmp/aien-gb10.lock}
19board=${CUDAPP_BOARD:-}
20user_created=0
21cd / || exit 3
22run() { echo "+ $*"; "$@"; }
23note() { [ -n "$board" ] && echo "$(date -u +%FT%TZ) $*" >> "$board"; return 0; }
24tr_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.
27U=c2stranger${tr_dir##*.}
28exec > >(tee "$log") 2>&1
29echo "# clean-user-hello: branch=$branch url=$url user=$U"
30echo "# date: $(date -u +%FT%TZ) host: $(uname -srm) loadavg: $(cut -d' ' -f1-3 /proc/loadavg)"
31echo "# operator: $(id -un) nvidia module: $(cat /sys/module/nvidia/version 2>/dev/null || echo none)"
32cleanup() {
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}
44trap cleanup EXIT
45echo "# --- create a brand-new user ---"
46run sudo useradd -m -s /bin/bash "$U" || exit 3
47user_created=1
48# the stranger's shell: an empty environment except what login(1) sets, then the distribution's login profile
49S() { 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"; }
50echo "# new user:"
51S 'echo "# id: $(id)"; echo "# home: [$(ls -A ~ | tr "\n" " ")]"; [ -e ~/workspace ] && echo "# ~/workspace EXISTS" || echo "# ~/workspace absent"'
52echo "# environment after login (env | sort):"
53S 'env | sort | sed "s/^/# /"'
54echo "# --- the README of $branch, as a stranger reads it on GitHub ---"
55readme=$(curl -fsSL "https://raw.githubusercontent.com/aien-dev/CUDApp/$branch/README.md") || { echo "FAIL: cannot fetch the README"; exit 3; }
56cmds=$(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; }
58echo "# 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 #")
61script=$(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
65T() { 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.
69CUDA_RE='"(/usr/local/cuda[^"]*|/opt/nvidia[^"]*|[^"]*/(libcuda|libcudart|libcublas|libcudnn|libnvrtc|libnvJitLink|libnvidia-ptxjitcompiler)[^"/]*|[^"]*/nvcc)"'
70cuda_hits() { sudo grep -E '(execve|openat)\(' "$1" | grep -vE '= -1 ' | grep -E "$CUDA_RE" | grep -vE '"/tmp/tmp\.[A-Za-z0-9]+/' || true; }
71stub_hits() { sudo grep -E '(execve|openat)\(' "$1" | grep -vE '= -1 ' | grep -E "$CUDA_RE" | grep -cE '"/tmp/tmp\.[A-Za-z0-9]+/' || true; }
72echo "# --- run the README block as $U, holding $lock ---"
73note "GPU-START clean-user-hello $U branch=$branch (flock $lock)"
74t0=$(date +%s)
75echo "+ flock -w 900 $lock strace -f ... sudo -u $U (README block)"
76export -f T; export U
77flock -w 900 "$lock" bash -c 'T "$1" "$2"' _ "$script" "$tr_dir/readme.trace"
78rc=$?
79echo "# README block exit code: $rc ($(( $(date +%s) - t0 )) s including the lock wait)"
80note "GPU-END clean-user-hello $U rc=$rc, GPU released"
81echo "# --- trace check: CUDA files opened or executed during the README block ---"
82echo "# traced calls: $(sudo grep -cE '(execve|openat)\(' "$tr_dir/readme.trace"), programs executed: $(sudo grep -E 'execve\(' "$tr_dir/readme.trace" | grep -vcE '= -1 ')"
83hits=$(cuda_hits "$tr_dir/readme.trace")
84if [ -n "$hits" ]; then echo "# TRACE FAIL: CUDA files used:"; printf '%s\n' "$hits" | head -20 | sed 's/^/# /'; [ $rc -eq 0 ] && rc=4
85else echo "# TRACE PASS: no CUDA file (/usr/local/cuda*, libcuda*, libnvrtc*, nvcc, ...) was opened or executed"; fi
86echo "# (the zero-CUDA gate's own stub libcudart in /tmp/tmp.*: $(stub_hits "$tr_dir/readme.trace") successful calls, its red control)"
87echo "# --- trace red control: the same check on 'nvcc --version' run by the same user ---"
88nvcc_bin=$(S 'command -v nvcc' 2>/dev/null); [ -n "$nvcc_bin" ] || { [ -x /usr/local/cuda/bin/nvcc ] && nvcc_bin=/usr/local/cuda/bin/nvcc; }
89if [ -z "$nvcc_bin" ]; then echo "# TRACE RED CONTROL: NOT_RUN (no CUDA toolkit on this machine to trace)"; else
90T "$nvcc_bin --version" "$tr_dir/red.trace" > /dev/null 2>&1
91red=$(cuda_hits "$tr_dir/red.trace")
92if [ -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))"
93else echo "# TRACE RED CONTROL BROKEN: the run of $nvcc_bin was not seen"; [ $rc -eq 0 ] && rc=5; fi; fi
94echo "# clean-user-hello exit code: $rc"
95exit $rc