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-room-sim.sh

81 lines · 6080 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# clean-room-sim.sh <branch> [log-file] (bar C2 without a GPU: hello-GPU on the simulator from an empty folder)
4#
5# Clones <branch> into a fresh temporary folder, takes the commands of the README section "Run your first kernel
6# without a GPU" (its first bash block) from that clone, and runs only those, one by one, in a new empty folder with an
7# empty environment: env -i with HOME set to a new empty directory, PATH=/usr/local/bin:/usr/bin:/bin, LANG=C.UTF-8
8# and nothing else. No sudo, no GPU lock: the block must open no /dev/nvidia node, which the script checks. The whole
9# block runs under strace (execve and openat, following every child); the check fails on a successful open of a
10# /dev/nvidia* node or of a CUDA file (toolkit tree, CUDA or NVIDIA JIT user-space libraries, nvcc). Two red controls
11# show the check can see one: a real traced read of a CUDA toolkit file, and a synthetic /dev/nvidia0 open line given to
12# the same checker (the script never opens a device, not even as a control).
13# CUDAPP_URL overrides the repository (a local path or file:// URL works; the clone line of the README is pointed at it).
14# Exit code: that of the first README command that fails (0 when all pass), 3 setup failure, 4 the trace saw a device
15# node or CUDA file opened, 5 a red control was not caught.
16set -uo pipefail
17branch=${1:?usage: clean-room-sim.sh <branch> [log-file]}
18log=${2:-/dev/stdout}
19url=${CUDAPP_URL:-https://github.com/aien-dev/CUDApp}
20work=$(mktemp -d "${TMPDIR:-/tmp}/cudapp-cleanroom.XXXXXX") || exit 3
21cleanup() { rm -rf "$work"; }
22trap cleanup EXIT
23CLEAN_PATH=/usr/local/bin:/usr/bin:/bin
24# A successful execve/openat (no "= -1") of a device node or a CUDA file. The zero-CUDA gate's own red control builds a
25# stub libcudart.so in a mktemp directory (/tmp/tmp.*) on purpose; those calls are counted apart and are not a CUDA use.
26BAD_RE='"(/dev/nvidia[^"]*|/usr/local/cuda[^"]*|/opt/nvidia[^"]*|[^"]*/(libcuda|libcudart|libcublas|libcudnn|libnvrtc|libnvJitLink|libnvidia-ptxjitcompiler)[^"/]*|[^"]*/nvcc)"'
27bad_hits() { grep -E '(execve|openat)\(' "$1" | grep -vE '= -1 ' | grep -E "$BAD_RE" | grep -vE '"/tmp/tmp\.[A-Za-z0-9]+/' || true; }
28stub_hits() { grep -E '(execve|openat)\(' "$1" | grep -vE '= -1 ' | grep -E "$BAD_RE" | grep -cE '"/tmp/tmp\.[A-Za-z0-9]+/' || true; }
29{
30 echo "# clean-room-sim: branch=$branch url=$url"
31 echo "# date: $(date -u +%FT%TZ) host: $(uname -srm) loadavg: $(cut -d' ' -f1-3 /proc/loadavg)"
32 echo "# tools: $(git --version), $(make --version | sed -n 1p), $(cc --version | sed -n 1p), $(strace -V | sed -n 1p)"
33 git clone -q --branch "$branch" "$url" "$work/src.readme" 2>&1 || { echo "FAIL: cannot clone $branch"; exit 3; }
34 cmds=$(awk '/^## Run your first kernel without a GPU/{s=1;next} s&&/^```bash/{b=1;next} s&&b&&/^```/{exit} s&&b{print}' "$work/src.readme/README.md")
35 [ -n "$cmds" ] || { echo "FAIL: no bash block under 'Run your first kernel without a GPU' in README.md"; exit 3; }
36 echo "# README commands, verbatim:"; printf '%s\n' "$cmds" | sed 's/^/# /'
37 rm -rf "$work/src.readme"
38 # the one change: the clone takes the branch under test from $url
39 cmds=$(printf '%s\n' "$cmds" | sed -E "s#^git clone #git clone --branch $branch #; s#https://github.com/aien-dev/CUDApp([^A-Za-z0-9_.-]|\$)#$url\\1#")
40 mkdir "$work/home" "$work/empty" || exit 3
41 E() { env -i HOME="$work/home" PATH="$CLEAN_PATH" LANG=C.UTF-8 "$@"; }
42 echo "# environment of every command (env -i, then exactly these):"
43 E env | sort | sed 's/^/# /'
44 echo "# cwd: $work/empty, contents: [$(ls -A "$work/empty")]"
45 rc=0; dir="$work/empty"; n=0; t0=$(date +%s)
46 while IFS= read -r line; do
47 [ -n "$line" ] || continue
48 n=$((n+1))
49 echo "\$ $line"
50 (cd "$dir" && E strace -f -qq -e trace=execve,openat -o "$work/trace.$n" bash -c "$line") 2>&1 || { rc=$?; echo "# command exited $rc"; }
51 [ "$rc" -eq 0 ] || break
52 case $line in "cd "*) dir="$dir/${line#cd }"; echo "# commit: $(git -C "$dir" rev-parse HEAD)";; esac
53 done <<<"$cmds"
54 echo "# README block exit code: $rc ($(( $(date +%s) - t0 )) s)"
55 echo "# --- trace check: device nodes and CUDA files opened or executed during the README block ---"
56 cat "$work"/trace.* > "$work/readme.trace" 2>/dev/null
57 hits=$(bad_hits "$work/readme.trace")
58 echo "# traced calls: $(wc -l < "$work/readme.trace"), programs executed: $(grep -c 'execve(' "$work/readme.trace")"
59 echo "# /dev/nvidia* paths tried and refused or absent (= -1): $(grep -E '(execve|openat)\(' "$work/readme.trace" | grep -E '"/dev/nvidia' | grep -cE '= -1 ' || true)"
60 if [ -n "$hits" ]; then
61 echo "# TRACE FAIL: a device node or CUDA file was opened or executed:"; printf '%s\n' "$hits" | sed -n 1,10p | sed 's/^/# /'
62 [ "$rc" -eq 0 ] && rc=4
63 else
64 echo "# TRACE PASS: no /dev/nvidia node and no CUDA file was opened or executed"
65 echo "# (the zero-CUDA gate's own stub libcudart in /tmp/tmp.*: $(stub_hits "$work/readme.trace") successful calls, its red control)"
66 fi
67 echo "# --- red control 1: the same trace and check on a real read of a CUDA toolkit file ---"
68 ctl=/usr/local/cuda/version.json
69 if [ -r "$ctl" ]; then
70 E strace -f -qq -e trace=execve,openat -o "$work/control.trace" cat "$ctl" > /dev/null 2>&1
71 c=$(bad_hits "$work/control.trace")
72 if [ -n "$c" ]; then echo "# TRACE RED CONTROL 1: caught as it must ($(printf '%s\n' "$c" | wc -l) uses, first: $(printf '%s\n' "$c" | sed -n 1p))"
73 else echo "# TRACE RED CONTROL 1 NOT CAUGHT"; [ "$rc" -eq 0 ] && rc=5; fi
74 else echo "# TRACE RED CONTROL 1 NOT_RUN: $ctl is absent on this machine"; fi
75 echo "# --- red control 2: a synthetic successful /dev/nvidia0 open given to the same checker ---"
76 printf '%s\n' '4242 openat(AT_FDCWD, "/dev/nvidia0", O_RDWR|O_CLOEXEC) = 3' > "$work/synthetic.trace"
77 if [ -n "$(bad_hits "$work/synthetic.trace")" ]; then echo "# TRACE RED CONTROL 2: caught as it must"
78 else echo "# TRACE RED CONTROL 2 NOT CAUGHT"; [ "$rc" -eq 0 ] && rc=5; fi
79 echo "# clean-room-sim exit code: $rc"
80 exit $rc
81} > >(tee "$log") 2>&1