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 / sm121dis / negctl.sh
| 1 | # ORACLE harness: NVIDIA nvdisasm 13.0.85 is the offline reference here; nothing it prints enters a kernel. |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # negctl.sh <sm121dis> <kernel-dir> <work-dir>: the flipped-bit negative control of the C6 parity check (DRA-475). |
| 4 | # Run after tools/sm121dis/check.sh <sm121dis> <kernel-dir> <work-dir>, whose <name>.want files (nvdisasm's normalised |
| 5 | # text of every kernel) it reads. For every kernel: copy it, flip bit 16 of word 0 (the lowest destination-register |
| 6 | # bit, or a fixed bit of forms without one), decode the copy with our disassembler and compare with nvdisasm's text of |
| 7 | # the ORIGINAL kernel. The control holds only when every kernel's text changes and parity breaks in exactly word 0. |
| 8 | # It also decodes the flipped copy with nvdisasm: "tracked" counts kernels where we still agree with the oracle on the |
| 9 | # flipped word, "reported" those where the flipped word is a form we do not know (printed ?? and counted, never guessed). |
| 10 | # Kernels run NEGCTL_JOBS at a time (default: every CPU this process may use); each writes <work-dir>/negctl/<name>.neg. |
| 11 | # Exit 0 when the control holds for every kernel, 1 otherwise, 2 when nvdisasm or a .want file is missing. |
| 12 | set -u |
| 13 | NVDISASM=${NVDISASM:-/usr/local/cuda/bin/nvdisasm} |
| 14 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 15 | |
| 16 | if [ "${1:-}" = --one ]; then |
| 17 | # negctl.sh --one <sm121dis> <work-dir> <kernel.bin>: first line of <name>.neg: BROKE|FAIL TRACKED|REPORTED|- |
| 18 | DIS=$2; WORK=$3; f=$4; F=$WORK/negctl |
| 19 | b=$(basename "$f" .bin) |
| 20 | out=$F/$b.neg.tmp |
| 21 | if [ ! -f "$WORK/$b.want" ]; then echo "FAIL - $b: $WORK/$b.want missing" > "$out"; mv "$out" "$F/$b.neg"; exit 0; fi |
| 22 | cp "$f" "$F/$b.bin" |
| 23 | v=$(od -An -tu1 -j2 -N1 "$f" | tr -d ' ') |
| 24 | printf "\\$(printf '%03o' $((v ^ 1)))" | dd of="$F/$b.bin" bs=1 seek=2 conv=notrunc status=none |
| 25 | if cmp -s "$f" "$F/$b.bin"; then echo "FAIL - $b: the flip changed no byte" > "$out"; mv "$out" "$F/$b.neg"; exit 0; fi |
| 26 | "$DIS" --pairs "$F/$b.bin" 2> /dev/null | sed 's/[[:space:]]\{1,\}/ /g' > "$F/$b.got" |
| 27 | d=$(diff "$WORK/$b.want" "$F/$b.got" | grep -c '^<') |
| 28 | w0=$(diff "$WORK/$b.want" "$F/$b.got" | grep '^[0-9]' | head -1) |
| 29 | if [ "$d" != 1 ] || [ "$w0" != 1c1 ]; then |
| 30 | echo "FAIL - $b: flipped bit broke parity in $d words ($w0), want exactly word 0" > "$out"; mv "$out" "$F/$b.neg"; exit 0 |
| 31 | fi |
| 32 | t=REPORTED |
| 33 | if "$NVDISASM" -b SM121 -hex "$F/$b.bin" > "$F/$b.nv" 2>&1; then |
| 34 | sh "$HERE/oracle_pairs.sh" "$F/$b.nv" > "$F/$b.fwant" |
| 35 | if cmp -s "$F/$b.fwant" "$F/$b.got"; then t=TRACKED |
| 36 | elif ! head -1 "$F/$b.got" | grep -q ' ?? 0x'; then |
| 37 | echo "FAIL - $b: on the flipped word we print '$(head -1 "$F/$b.got" | cut -d' ' -f4-)', nvdisasm '$(head -1 "$F/$b.fwant" | cut -d' ' -f4-)'" > "$out" |
| 38 | mv "$out" "$F/$b.neg"; exit 0 |
| 39 | fi |
| 40 | fi |
| 41 | echo "BROKE $t $b" > "$out"; mv "$out" "$F/$b.neg" |
| 42 | exit 0 |
| 43 | fi |
| 44 | |
| 45 | DIS=$1; DIR=$2; WORK=$3 |
| 46 | [ -x "$NVDISASM" ] || { echo "VERDICT NOT_RUN: $NVDISASM missing"; exit 2; } |
| 47 | ls "$WORK"/*.want > /dev/null 2>&1 || { echo "VERDICT NOT_RUN: no .want files in $WORK (run check.sh first)"; exit 2; } |
| 48 | F=$WORK/negctl |
| 49 | mkdir -p "$F" |
| 50 | find "$F/." -maxdepth 1 \( -name '*.neg' -o -name '*.neg.tmp' \) -exec rm -f {} + |
| 51 | JOBS=${NEGCTL_JOBS:-$(nproc 2>/dev/null || echo 1)} |
| 52 | for f in "$DIR"/*.bin; do printf '%s\0' "$f"; done | xargs -0 -n 1 -P "$JOBS" sh "$HERE/negctl.sh" --one "$DIS" "$WORK" |
| 53 | k=0; broke=0; tracked=0; reported=0; bad=0 |
| 54 | for f in "$DIR"/*.bin; do |
| 55 | b=$(basename "$f" .bin); k=$((k + 1)) |
| 56 | if [ -f "$F/$b.neg" ] && read -r st t rest < "$F/$b.neg"; then |
| 57 | case "$st $t" in |
| 58 | "BROKE TRACKED") broke=$((broke + 1)); tracked=$((tracked + 1)) ;; |
| 59 | "BROKE REPORTED") broke=$((broke + 1)); reported=$((reported + 1)) ;; |
| 60 | *) echo "[FAIL] $rest"; bad=$((bad + 1)) ;; |
| 61 | esac |
| 62 | else echo "[FAIL] $b: no result"; bad=$((bad + 1)); fi |
| 63 | done |
| 64 | echo "flipped-bit control: kernels $k, parity broken in exactly word 0: $broke, of those tracking nvdisasm on the flipped word: $tracked, flipped word reported unknown (??): $reported, failures: $bad" |
| 65 | [ "$bad" = 0 ] && [ "$broke" = "$k" ] && { echo "VERDICT PASS: one flipped bit changes our text and breaks parity on every kernel"; exit 0; } |
| 66 | echo "VERDICT FAIL"; exit 1 |