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 / tests / coder / lib.sh

91 lines · 3644 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2# Shared helpers for the ERC1 tests. Sourced by synthetic.sh, real.sh and mutants.sh.
3# Needs: bash, od, dd, head, cmp, gzip, openssl, sha256sum, shuf. No other language.
4
5PASS=0
6FAIL=0
7ok() { PASS=$((PASS + 1)); echo "PASS $*"; }
8bad() {
9 FAIL=$((FAIL + 1))
10 echo "FAIL $*"
11 if [ "${ERC_MUTANT_FIRST_FAILURE:-0}" = 1 ]; then
12 echo "ERC1 MUTATION WITNESS: $*"
13 exit 42
14 fi
15}
16# check NAME CMD... passes when CMD exits 0
17check() { local n=$1; shift; if "$@"; then ok "$n"; else bad "$n"; fi; }
18
19# prng SEED BYTES: deterministic bytes (AES-256-CTR keystream of zeros).
20prng() { head -c "$2" /dev/zero | openssl enc -aes-256-ctr -pass "pass:$1" -pbkdf2 -nosalt 2>/dev/null; }
21
22# pin_check OMC: the compiler must be the one the repository PIN names (the omc-sha256 line), else exit 3.
23pin_check() {
24 local want got
25 want=$(sed -n 's/^omc-sha256 \([0-9a-f]\{64\}\)$/\1/p' "$ROOT/PIN")
26 got=$(sha256sum < "$1" | cut -d' ' -f1)
27 if [ -z "$want" ] || [ "$want" != "$got" ]; then
28 echo "NOT_RUN: the compiler $1 has sha256 $got, PIN names $want"
29 exit 3
30 fi
31}
32# build OMC SRC OUT: compile the unit with the compiler the PIN names.
33build() { pin_check "$1" && "$1" < "$2" > "$3" && chmod +x "$3"; }
34
35u32at() { od -An -tu4 -j "$2" -N4 "$1" | tr -d ' '; }
36u8at() { od -An -tu1 -j "$2" -N1 "$1" | tr -d ' '; }
37
38# flipbit FILE BYTE BIT: flip one bit in place.
39flipbit() {
40 local v nv
41 v=$(u8at "$1" "$2")
42 nv=$((v ^ (1 << $3)))
43 printf "$(printf '\\x%02x' "$nv")" | dd of="$1" bs=1 seek="$2" conv=notrunc 2>/dev/null
44}
45# setbyte FILE BYTE VALUE
46setbyte() { printf "$(printf '\\x%02x' "$3")" | dd of="$1" bs=1 seek="$2" conv=notrunc 2>/dev/null; }
47# setu32 FILE BYTE VALUE: little-endian
48setu32() {
49 local i
50 for i in 0 1 2 3; do setbyte "$1" $(($2 + i)) $((($3 >> (8 * i)) & 255)); done
51}
52
53# Layout (om/coder/FORMAT.md): rows at 8, cols at 12, offsets at 544, header CRC after them.
54rows_of() { u32at "$1" 8; }
55hcrc_pos() { echo $((548 + 6 * $(rows_of "$1"))); }
56sbase() { echo $(($(hcrc_pos "$1") + 4)); }
57# row_start FILE ROW and row_end FILE ROW: absolute byte positions of a stream.
58row_start() { echo $(($(sbase "$1") + $(u32at "$1" $((544 + 4 * $2))))); }
59row_end() { echo $(($(sbase "$1") + $(u32at "$1" $((544 + 4 * ($2 + 1)))))); }
60
61# refuses NAME CODE ROW CMD...: CMD must exit 3, name CODE (and "row ROW" when ROW is not -) on
62# stderr, and print nothing on stdout.
63refuses() {
64 local n=$1 code=$2 row=$3 so se rc
65 shift 3
66 so=$(mktemp); se=$(mktemp)
67 "$@" > "$so" 2> "$se"; rc=$?
68 local good=1
69 [ "$rc" = 3 ] || good=0
70 grep -q "refused $code\b" "$se" || good=0
71 if [ "$row" != - ]; then grep -q "refused $code row $row\$" "$se" || good=0; fi
72 [ -s "$so" ] && good=0
73 if [ "$good" = 1 ]; then ok "$n (exit 3, $(cat "$se"))"; else bad "$n: rc=$rc want refused $code row $row; stderr=$(cat "$se") stdout=$(head -c 80 "$so")"; fi
74 rm -f "$so" "$se"
75}
76# refuses_any NAME "CODE CODE ..." ROW CMD...: as refuses, the code may be any of the listed ones.
77refuses_any() {
78 local n=$1 codes=$2 row=$3 so se rc c hit=0
79 shift 3
80 so=$(mktemp); se=$(mktemp)
81 "$@" > "$so" 2> "$se"; rc=$?
82 for c in $codes; do
83 if [ "$row" = - ]; then
84 grep -q "refused $c\b" "$se" && hit=1
85 else
86 grep -q "refused $c row $row\$" "$se" && hit=1
87 fi
88 done
89 if [ "$rc" = 3 ] && [ "$hit" = 1 ] && [ ! -s "$so" ]; then ok "$n (exit 3, $(cat "$se"))"; else bad "$n: rc=$rc want refused {$codes} row $row; stderr=$(cat "$se")"; fi
90 rm -f "$so" "$se"
91}