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 / tests / coder / synthetic.sh
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # ERC1 host tests on generated matrices: round trips, size identity, row independence, every |
| 4 | # negative control, flip scan. No GPU, no checkpoint. |
| 5 | # sh tests/coder/synthetic.sh OMC [WORKDIR] OMC = the compiler the repository PIN names |
| 6 | # QUICK=1 skips the flip scan and the order tests (mutants.sh uses it). |
| 7 | # Ends with "ERC1 SYNTHETIC PASS n checks" and exit 0, or "ERC1 SYNTHETIC FAIL ..." and exit 1. |
| 8 | export LC_ALL=C |
| 9 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 10 | ROOT=$(cd "$HERE/../.." && pwd) |
| 11 | . "$HERE/lib.sh" |
| 12 | OMC=${1:?usage: synthetic.sh OMC [WORKDIR]} |
| 13 | W=${2:-$(mktemp -d)} |
| 14 | mkdir -p "$W" |
| 15 | SRC=${ERC_SRC:-$ROOT/om/coder/erc.om} |
| 16 | ERC=$W/erc |
| 17 | build "$OMC" "$SRC" "$ERC" || { echo "ERC1 SYNTHETIC FAIL: compile"; exit 1; } |
| 18 | |
| 19 | gen() { # NAME ROWS COLS KIND SEED |
| 20 | local f=$W/$1.raw n=$(($2 * $3)) |
| 21 | case $4 in |
| 22 | uniform) prng "$5" "$n" > "$f" ;; |
| 23 | zeros) head -c "$n" /dev/zero > "$f" ;; |
| 24 | two) prng "$5" "$n" | tr '\000-\177' '\060' | tr '\200-\377' '\061' > "$f" ;; |
| 25 | skew) prng "$5" "$n" | tr '\000-\357' '\000' | tr '\360-\377' '\001-\020' > "$f" ;; |
| 26 | onediff) { head -c $((n - 1)) /dev/zero; printf '\007'; } > "$f" ;; |
| 27 | esac |
| 28 | } |
| 29 | |
| 30 | # roundtrip NAME ROWS COLS: encode, decode, compare, size identity, CRC against gzip, every row alone. |
| 31 | roundtrip() { |
| 32 | local n=$1 r=$2 c=$3 raw=$W/$1.raw enc=$W/$1.erc out |
| 33 | out=$("$ERC" encode "$raw" 0 "$r" "$c" "$enc") || { bad "$n: encode"; return; } |
| 34 | "$ERC" decode "$enc" "$W/$n.dec" > /dev/null || { bad "$n: decode"; return; } |
| 35 | check "$n: ${r}x${c} decode is byte-identical" cmp -s "$raw" "$W/$n.dec" |
| 36 | local size hdr streams want |
| 37 | size=$(stat -c %s "$enc") |
| 38 | want=$(echo "$out" | sed 's/.*coded_bytes=\([0-9]*\).*/\1/') |
| 39 | check "$n: coded_bytes printed equals file size ($size)" [ "$size" = "$want" ] |
| 40 | hdr=$((552 + 6 * r)) |
| 41 | streams=$(echo "$out" | sed 's/.*stream_bytes=\([0-9]*\).*/\1/') |
| 42 | check "$n: file = 552 + 6 rows + stream bytes" [ "$size" = $((hdr + streams)) ] |
| 43 | local crc |
| 44 | crc=$(gzip -c "$raw" | tail -c8 | od -An -tx4 -N4 | tr -d ' ') |
| 45 | check "$n: matrix_crc32 equals the gzip trailer CRC32 ($crc)" [ "$("$ERC" info "$enc" | sed 's/.*matrix_crc32=//')" = "$crc" ] |
| 46 | local row okrows=1 step=1 |
| 47 | [ "$r" -gt 300 ] && step=$((r / 97 + 1)) |
| 48 | for ((row = 0; row < r; row += step)); do |
| 49 | "$ERC" decode-row "$enc" "$row" "$W/row.out" 2> /dev/null && dd if="$raw" of="$W/row.ref" bs="$c" skip="$row" count=1 2> /dev/null && cmp -s "$W/row.out" "$W/row.ref" || { okrows=0; echo " row $row differs"; } |
| 50 | done |
| 51 | check "$n: decode-row alone equals the raw row (every $step-th row)" [ "$okrows" = 1 ] |
| 52 | } |
| 53 | |
| 54 | gen s11a 1 1 skew 1; roundtrip s11a 1 1 |
| 55 | printf '\070' > "$W/s11c.raw"; roundtrip s11c 1 1 |
| 56 | gen s1x4096 1 4096 skew 2; roundtrip s1x4096 1 4096 |
| 57 | gen s4096x1 4096 1 skew 3; roundtrip s4096x1 4096 1 |
| 58 | gen s7x13 7 13 uniform 4; roundtrip s7x13 7 13 |
| 59 | gen s100z 100 100 zeros 5; roundtrip s100z 100 100 |
| 60 | gen s33x77 33 77 two 6; roundtrip s33x77 33 77 |
| 61 | gen s256u 256 256 uniform 7; roundtrip s256u 256 256 |
| 62 | gen s50d 50 50 onediff 8; roundtrip s50d 50 50 |
| 63 | gen s200d 200 200 onediff 9; roundtrip s200d 200 200 |
| 64 | gen s64k 64 512 skew 10; roundtrip s64k 64 512 |
| 65 | gen s640x2560 640 2560 skew 11; roundtrip s640x2560 640 2560 |
| 66 | # all 256 byte values once each (a table of 256 equal entries of 64) |
| 67 | for i in $(seq 0 255); do printf "$(printf '\\x%02x' "$i")"; done > "$W/sall.raw"; roundtrip sall 1 256 |
| 68 | |
| 69 | check "uniform bytes do not shrink (256x256 coded >= raw)" [ "$(stat -c %s "$W/s256u.erc")" -ge 65536 ] |
| 70 | check "skewed bytes do shrink (64x512 coded < raw)" [ "$(stat -c %s "$W/s64k.erc")" -lt 32768 ] |
| 71 | |
| 72 | # encode from a byte offset of a larger file (how the checkpoint matrices are read) |
| 73 | { prng 12 1000; cat "$W/s64k.raw"; prng 13 77; } > "$W/shard.bin" |
| 74 | "$ERC" encode "$W/shard.bin" 1000 64 512 "$W/off.erc" > /dev/null |
| 75 | check "encode at a byte offset equals encode of the slice" cmp -s "$W/off.erc" "$W/s64k.erc" |
| 76 | |
| 77 | # ---------------------------------------------------------------- argument and I/O refusals |
| 78 | "$ERC" > /dev/null 2>&1; check "no arguments exit 2" [ $? = 2 ] |
| 79 | "$ERC" encode "$W/s64k.raw" 0 0 512 "$W/x.erc" > /dev/null 2>&1; check "ROWS 0 exit 2" [ $? = 2 ] |
| 80 | "$ERC" encode "$W/s64k.raw" 0 64 513 "$W/x.erc" > /dev/null 2>&1; check "raw shorter than ROWS*COLS exit 4" [ $? = 4 ] |
| 81 | "$ERC" decode "$W/none.erc" "$W/x.out" > /dev/null 2>&1; check "missing input exit 4" [ $? = 4 ] |
| 82 | "$ERC" decode-row "$W/s64k.erc" 64 "$W/x.out" > /dev/null 2>&1; check "ROW past the last row exit 2" [ $? = 2 ] |
| 83 | "$ERC" encode "$W/s64k.raw" 9999999999999999999 1 1 "$W/x.erc" > /dev/null 2>&1; check "OFFSET beyond a signed file offset exit 2 (not a trap)" [ $? = 2 ] |
| 84 | "$ERC" encode "$W/s64k.raw" 0 4294967295 4294967295 "$W/x.erc" > /dev/null 2>&1; check "ROWS * COLS beyond a signed file offset exit 2 (not a trap)" [ $? = 2 ] |
| 85 | "$ERC" encode "$W/s64k.raw" 0 64 5x2 "$W/x.erc" > /dev/null 2>&1; check "COLS that is not a number exit 2" [ $? = 2 ] |
| 86 | "$ERC" encode "$W/s64k.raw" 0 4294967296 1 "$W/x.erc" > /dev/null 2>&1; check "ROWS 2^32 exit 2" [ $? = 2 ] |
| 87 | |
| 88 | # ---------------------------------------------------------------- damaged files, each for its reason |
| 89 | G=$W/s64k.erc # 64 rows x 512 cols |
| 90 | K=20 # the row that gets damaged |
| 91 | cp "$G" "$W/g.erc" |
| 92 | check "undamaged copy decodes" "$ERC" decode "$W/g.erc" "$W/g.out" |
| 93 | : > "$W/empty.erc" |
| 94 | refuses "empty file" TRUNC - "$ERC" decode "$W/empty.erc" "$W/x.out" |
| 95 | prng 14 3000 > "$W/noise.erc" |
| 96 | refuses "random bytes" MAGIC - "$ERC" decode "$W/noise.erc" "$W/x.out" |
| 97 | |
| 98 | # a flipped bit in a stream |
| 99 | mk() { cp "$G" "$W/$1.erc"; } |
| 100 | ks=$(row_start "$G" $K); ke=$(row_end "$G" $K); mid=$(((ks + ke) / 2)) |
| 101 | mk fs; flipbit "$W/fs.erc" "$mid" 3 |
| 102 | refuses "stream bit flip (row $K), decode-row" ROWCRC $K "$ERC" decode-row "$W/fs.erc" $K "$W/x.out" |
| 103 | refuses "stream bit flip (row $K), decode" ROWCRC $K "$ERC" decode "$W/fs.erc" "$W/x.out" |
| 104 | check "decode left no output file after the refusal" [ ! -e "$W/x.out" ] |
| 105 | other=0 |
| 106 | for row in 0 19 21 63; do |
| 107 | "$ERC" decode-row "$W/fs.erc" $row "$W/o.out" || other=1 |
| 108 | dd if="$W/s64k.raw" of="$W/o.ref" bs=512 skip=$row count=1 2> /dev/null |
| 109 | cmp -s "$W/o.out" "$W/o.ref" || other=1 |
| 110 | done |
| 111 | check "rows other than $K still decode to their original bytes after the flip" [ "$other" = 0 ] |
| 112 | # every byte outside row K's stream replaced by noise: row K alone still decodes, and only it is read |
| 113 | mk iso; sb=$(sbase "$G"); fsz=$(stat -c %s "$G") |
| 114 | prng 77 $((ks - sb)) | dd of="$W/iso.erc" bs=1 seek="$sb" conv=notrunc 2> /dev/null |
| 115 | prng 78 $((fsz - ke)) | dd of="$W/iso.erc" bs=1 seek="$ke" conv=notrunc 2> /dev/null |
| 116 | "$ERC" decode-row "$W/iso.erc" $K "$W/o.out"; dd if="$W/s64k.raw" of="$W/o.ref" bs=512 skip=$K count=1 2> /dev/null |
| 117 | check "all other rows' streams replaced by noise: row $K alone decodes to its original bytes" cmp -s "$W/o.out" "$W/o.ref" |
| 118 | refuses "the same file, whole-matrix decode" ROWCRC 0 "$ERC" decode "$W/iso.erc" "$W/x.out" |
| 119 | # flip the very first and the very last byte of a stream, and one bit of every byte of the state |
| 120 | mk fs0; flipbit "$W/fs0.erc" "$ks" 0 |
| 121 | refuses "first stream byte bit flip" ROWCRC $K "$ERC" decode-row "$W/fs0.erc" $K "$W/x.out" |
| 122 | mk fs1; flipbit "$W/fs1.erc" $((ke - 1)) 7 |
| 123 | refuses "last stream byte bit flip" ROWCRC $K "$ERC" decode-row "$W/fs1.erc" $K "$W/x.out" |
| 124 | # the stream-level checks, reached by recomputing the row CRCs (fixrows) so only they can refuse |
| 125 | fixrows() { "$ERC" fixrows "$W/$1.erc" > /dev/null; } |
| 126 | mk st0; setu32 "$W/st0.erc" "$ks" 4294967295; fixrows st0 |
| 127 | refuses "state 0xffffffff" STATE $K "$ERC" decode-row "$W/st0.erc" $K "$W/x.out" |
| 128 | mk st1; setu32 "$W/st1.erc" "$ks" 8388607; fixrows st1 |
| 129 | refuses "state 2^23 - 1" STATE $K "$ERC" decode-row "$W/st1.erc" $K "$W/x.out" |
| 130 | ko=$((544 + 4 * (K + 1))); kv=$(u32at "$G" $ko) |
| 131 | mk ex; setu32 "$W/ex.erc" $ko $((kv - 1)); fixrows ex |
| 132 | refuses "stream one byte short" EXHAUST $K "$ERC" decode-row "$W/ex.erc" $K "$W/x.out" |
| 133 | mk lo; setu32 "$W/lo.erc" $ko $((kv + 1)); fixrows lo |
| 134 | refuses "stream one byte long" LEFTOVER $K "$ERC" decode-row "$W/lo.erc" $K "$W/x.out" |
| 135 | # a flipped bit that leaves the stream consumed exactly but ends in the wrong state |
| 136 | fin=0 |
| 137 | for ((b = 4; b < 60 && fin == 0; b++)); do for bit in 0 1 2 3 4 5 6 7; do |
| 138 | mk fn; flipbit "$W/fn.erc" $((ks + b)) $bit; fixrows fn |
| 139 | if "$ERC" decode-row "$W/fn.erc" $K "$W/x.out" 2>&1 | grep -q "refused FINAL row $K\$"; then fin=1; break; fi |
| 140 | done; done |
| 141 | check "a stream bit flip exists that only the final-state check refuses (FINAL)" [ "$fin" = 1 ] |
| 142 | # a forged row CRC: the stream is fine, the stored CRC is wrong |
| 143 | mk rc; rp=$((548 + 4 * 64 + 2 * K)); setbyte "$W/rc.erc" $rp 170; setbyte "$W/rc.erc" $((rp + 1)) 170; "$ERC" fixcrc "$W/rc.erc" > /dev/null |
| 144 | refuses "row CRC-16 entry changed, header CRC recomputed" ROWCRC $K "$ERC" decode-row "$W/rc.erc" $K "$W/x.out" |
| 145 | |
| 146 | # frequency table, row offset, header fields: the header CRC covers them |
| 147 | mk ft; flipbit "$W/ft.erc" $((32 + 2 * 17 + 1)) 2 |
| 148 | refuses "frequency table bit flip" HDRCRC - "$ERC" decode "$W/ft.erc" "$W/x.out" |
| 149 | refuses "frequency table bit flip, decode-row" HDRCRC - "$ERC" decode-row "$W/ft.erc" 3 "$W/x.out" |
| 150 | refuses "frequency table bit flip, info" HDRCRC - "$ERC" info "$W/ft.erc" |
| 151 | mk ro; flipbit "$W/ro.erc" $((544 + 4 * 30)) 4 |
| 152 | refuses "row offset bit flip" HDRCRC - "$ERC" decode "$W/ro.erc" "$W/x.out" |
| 153 | refuses "row offset bit flip, decode-row" HDRCRC - "$ERC" decode-row "$W/ro.erc" 30 "$W/x.out" |
| 154 | mk mc; flipbit "$W/mc.erc" 22 1 |
| 155 | refuses "matrix CRC field bit flip" HDRCRC - "$ERC" info "$W/mc.erc" |
| 156 | mk hc; flipbit "$W/hc.erc" "$(hcrc_pos "$G")" 5 |
| 157 | refuses "header CRC field bit flip" HDRCRC - "$ERC" info "$W/hc.erc" |
| 158 | mk co; flipbit "$W/co.erc" 12 0 |
| 159 | refuses "cols field bit flip" HDRCRC - "$ERC" info "$W/co.erc" |
| 160 | mk ma; flipbit "$W/ma.erc" 2 6 |
| 161 | refuses "magic bit flip" MAGIC - "$ERC" info "$W/ma.erc" |
| 162 | mk ve; setbyte "$W/ve.erc" 4 2 |
| 163 | refuses "version 2" VERSION - "$ERC" info "$W/ve.erc" |
| 164 | mk fl; setbyte "$W/fl.erc" 6 1 |
| 165 | refuses "flags nonzero" VERSION - "$ERC" info "$W/fl.erc" |
| 166 | mk pb; setu32 "$W/pb.erc" 16 15 |
| 167 | refuses "prob_bits 15" FIELD - "$ERC" info "$W/pb.erc" |
| 168 | mk r0; setu32 "$W/r0.erc" 8 0 |
| 169 | refuses "rows 0" FIELD - "$ERC" info "$W/r0.erc" |
| 170 | mk tl; flipbit "$W/tl.erc" 24 4 |
| 171 | refuses "total_len bit flip" LENGTH - "$ERC" info "$W/tl.erc" |
| 172 | |
| 173 | # truncation and extension |
| 174 | sz=$(stat -c %s "$G") |
| 175 | head -c $((sz - 1)) "$G" > "$W/t1.erc" |
| 176 | refuses "truncated by one byte" LENGTH - "$ERC" decode "$W/t1.erc" "$W/x.out" |
| 177 | head -c $((sz / 2)) "$G" > "$W/t2.erc" |
| 178 | refuses "truncated to half" LENGTH - "$ERC" decode "$W/t2.erc" "$W/x.out" |
| 179 | head -c 600 "$G" > "$W/t3.erc" |
| 180 | refuses "truncated inside the row table" TRUNC - "$ERC" decode "$W/t3.erc" "$W/x.out" |
| 181 | head -c 20 "$G" > "$W/t4.erc" |
| 182 | refuses "truncated inside the header" TRUNC - "$ERC" decode "$W/t4.erc" "$W/x.out" |
| 183 | { cat "$G"; printf 'x'; } > "$W/t5.erc" |
| 184 | refuses "one byte appended" LENGTH - "$ERC" decode "$W/t5.erc" "$W/x.out" |
| 185 | |
| 186 | # forged files: the header CRC is recomputed, so only the second-layer checks can refuse |
| 187 | fixed() { "$ERC" fixcrc "$W/$1.erc" > /dev/null; } |
| 188 | mk fq; setu32 "$W/fq.erc" $((32 + 2 * 5)) 1000; fixed fq # table entry 5 becomes 1000: sum is wrong |
| 189 | refuses "forged table, sum is not 2^14" FREQ - "$ERC" info "$W/fq.erc" |
| 190 | mk fo1; setu32 "$W/fo1.erc" $((544 + 4 * 64)) $(($(u32at "$G" $((544 + 4 * 64))) + 1)); fixed fo1 |
| 191 | refuses "forged last offset +1" OFFSET - "$ERC" info "$W/fo1.erc" |
| 192 | mk fo2; setu32 "$W/fo2.erc" $((544 + 4 * 2)) 3; fixed fo2 |
| 193 | refuses "forged offset: stream of 3 bytes" OFFSET - "$ERC" info "$W/fo2.erc" |
| 194 | mk fo3; setu32 "$W/fo3.erc" 544 4; fixed fo3 |
| 195 | refuses "forged first offset not 0" OFFSET - "$ERC" info "$W/fo3.erc" |
| 196 | mk fo4; setu32 "$W/fo4.erc" $((544 + 4 * 30)) $(($(u32at "$G" $((544 + 4 * 29))) + 2)); fixed fo4 |
| 197 | refuses "forged offset: stream shorter than a state" OFFSET - "$ERC" info "$W/fo4.erc" |
| 198 | mk fo5; setu32 "$W/fo5.erc" $((544 + 4 * 30)) $(($(u32at "$G" $((544 + 4 * 30))) + 5)); fixed fo5 |
| 199 | refuses "forged offset moved 5 bytes into the next stream, row CRCs kept" ROWCRC 29 "$ERC" decode-row "$W/fo5.erc" 29 "$W/x.out" |
| 200 | mk fo6; setu32 "$W/fo6.erc" $((544 + 4 * 30)) $(($(u32at "$G" $((544 + 4 * 30))) + 5)); fixrows fo6 |
| 201 | refuses "forged offset moved 5 bytes into the next stream, row CRCs recomputed" LEFTOVER 29 "$ERC" decode-row "$W/fo6.erc" 29 "$W/x.out" |
| 202 | # a table that still sums to 2^14 but is wrong: streams decode under the wrong model |
| 203 | mk fm |
| 204 | f0=$(od -An -tu2 -j 32 -N2 "$G" | tr -d ' '); f1=$(od -An -tu2 -j 34 -N2 "$G" | tr -d ' ') |
| 205 | setbyte "$W/fm.erc" 32 $(((f0 + 1) & 255)); setbyte "$W/fm.erc" 33 $(((f0 + 1) >> 8)); setbyte "$W/fm.erc" 34 $(((f1 - 1) & 255)); setbyte "$W/fm.erc" 35 $(((f1 - 1) >> 8)); fixed fm |
| 206 | refuses_any "forged table, count moved between two symbols, decode" "STATE EXHAUST LEFTOVER FINAL MATCRC" - "$ERC" decode "$W/fm.erc" "$W/x.out" |
| 207 | # a forged matrix CRC: every stream is fine, only the whole-matrix check can refuse |
| 208 | mk fc; setu32 "$W/fc.erc" 20 305419896; fixed fc |
| 209 | refuses "forged matrix CRC, decode" MATCRC - "$ERC" decode "$W/fc.erc" "$W/x.out" |
| 210 | "$ERC" decode-row "$W/fc.erc" 7 "$W/o.out"; dd if="$W/s64k.raw" of="$W/o.ref" bs=512 skip=7 count=1 2> /dev/null |
| 211 | check "forged matrix CRC: one row alone still decodes (the matrix CRC is a whole-matrix check)" cmp -s "$W/o.out" "$W/o.ref" |
| 212 | |
| 213 | # known answers for the two checksums |
| 214 | printf 123456789 > "$W/ka.bin" |
| 215 | check "CRC-32 of 123456789 is cbf43926 and CRC-16/CCITT-FALSE is 29b1" [ "$("$ERC" sum "$W/ka.bin")" = "crc32=cbf43926 crc16=000029b1" ] |
| 216 | |
| 217 | # absurd fields must give a refusal, never a trap or a crash |
| 218 | mk big1; setu32 "$W/big1.erc" 8 4294967295 |
| 219 | refuses "rows 2^32 - 1 in a small file" TRUNC - "$ERC" decode "$W/big1.erc" "$W/x.out" |
| 220 | mk big2; setu32 "$W/big2.erc" 12 4294967295; fixed big2 |
| 221 | refuses_any "cols 2^32 - 1 with valid CRCs" "EXHAUST ALLOC STATE LEFTOVER FINAL ROWCRC" - "$ERC" decode "$W/big2.erc" "$W/x.out" |
| 222 | |
| 223 | # random damage, with the header CRC fixed, with the row CRCs fixed too, and with neither: the tool must |
| 224 | # exit 0 or 3 (never a trap or a crash), and every exit 0 must have produced the original bytes |
| 225 | FUZZ_N=${FUZZ_N:-300} |
| 226 | nums=($(prng fuzz $((FUZZ_N * 12 + 12)) | od -An -v -tu4)) |
| 227 | fsz=$(stat -c %s "$G"); fzbad=0; fzok=0; fzref=0 |
| 228 | for ((i = 0; i < FUZZ_N; i++)); do |
| 229 | pos=$((nums[i * 3] % fsz)); bit=$((nums[i * 3 + 1] % 8)); md=$((nums[i * 3 + 2] % 3)); rw=$((nums[i * 3 + 2] / 3 % 64)) |
| 230 | cp "$G" "$W/fz.erc"; flipbit "$W/fz.erc" "$pos" "$bit" |
| 231 | [ "$md" = 1 ] && "$ERC" fixcrc "$W/fz.erc" > /dev/null 2>&1 |
| 232 | [ "$md" = 2 ] && "$ERC" fixrows "$W/fz.erc" > /dev/null 2>&1 |
| 233 | rm -f "$W/fz.out" "$W/fz.row" |
| 234 | "$ERC" decode "$W/fz.erc" "$W/fz.out" > /dev/null 2>&1; rc=$? |
| 235 | case $rc in |
| 236 | 0) if cmp -s "$W/fz.out" "$W/s64k.raw"; then fzok=$((fzok + 1)); else fzbad=$((fzbad + 1)); echo " fuzz $i: exit 0 with wrong bytes (byte $pos bit $bit mode $md)"; fi ;; |
| 237 | 3) fzref=$((fzref + 1)) ;; |
| 238 | *) fzbad=$((fzbad + 1)); echo " fuzz $i: decode exit $rc (byte $pos bit $bit mode $md)" ;; |
| 239 | esac |
| 240 | "$ERC" decode-row "$W/fz.erc" "$rw" "$W/fz.row" > /dev/null 2>&1; rc=$? |
| 241 | case $rc in |
| 242 | 0) dd if="$W/s64k.raw" of="$W/fz.ref" bs=512 skip="$rw" count=1 2> /dev/null; cmp -s "$W/fz.row" "$W/fz.ref" || { fzbad=$((fzbad + 1)); echo " fuzz $i: decode-row exit 0 with wrong bytes (byte $pos bit $bit mode $md row $rw)"; } ;; |
| 243 | 3) ;; |
| 244 | *) fzbad=$((fzbad + 1)); echo " fuzz $i: decode-row exit $rc (byte $pos bit $bit mode $md)" ;; |
| 245 | esac |
| 246 | done |
| 247 | check "$FUZZ_N random single-bit damages: exit 0 or 3 only, every exit 0 has the original bytes (refused $fzref, accepted $fzok)" [ "$fzbad" = 0 ] |
| 248 | |
| 249 | # ---------------------------------------------------------------- rows are independent |
| 250 | if [ -z "${QUICK:-}" ]; then |
| 251 | for m in s64k s256u; do |
| 252 | r=$(rows_of "$W/$m.erc"); c=$(u32at "$W/$m.erc" 12) |
| 253 | mkdir -p "$W/ord_$m"; rm -f "$W/ord_$m"/* |
| 254 | for row in $(seq 0 $((r - 1)) | shuf --random-source=<(prng ord 100000)); do |
| 255 | "$ERC" decode-row "$W/$m.erc" "$row" "$W/ord_$m/$(printf %05d "$row")" || bad "$m: row $row" |
| 256 | done |
| 257 | cat "$W/ord_$m"/* > "$W/ord_$m.joined" |
| 258 | check "$m: all $r rows decoded one process each in shuffled order equal the raw matrix" cmp -s "$W/ord_$m.joined" "$W/$m.raw" |
| 259 | rm -f "$W/ord_$m"/* |
| 260 | for row in $(seq $((r - 1)) -1 0); do "$ERC" decode-row "$W/$m.erc" "$row" "$W/ord_$m/$(printf %05d "$row")"; done |
| 261 | cat "$W/ord_$m"/* > "$W/ord_$m.joined" |
| 262 | check "$m: all rows in reverse order equal the raw matrix" cmp -s "$W/ord_$m.joined" "$W/$m.raw" |
| 263 | done |
| 264 | # flip every stream bit of a small matrix once; the damaged row alone must never decode silently wrong |
| 265 | gen s16 16 1024 skew 15; "$ERC" encode "$W/s16.raw" 0 16 1024 "$W/s16.erc" > /dev/null |
| 266 | fs=$("$ERC" flipscan "$W/s16.erc"); echo " flipscan 16x1024: $fs" |
| 267 | check "flipscan: with the row CRC no flipped stream bit is undetected" [ "$(echo "$fs" | sed 's/.*checked_undetected=\([0-9]*\).*/\1/')" = 0 ] |
| 268 | check "flipscan: with the row CRC no flipped stream bit gives a wrong row" [ "$(echo "$fs" | sed 's/.*checked_undetected_wrong_output=\([0-9]*\).*/\1/')" = 0 ] |
| 269 | fi |
| 270 | |
| 271 | if [ "$FAIL" = 0 ]; then echo "ERC1 SYNTHETIC PASS $PASS checks"; exit 0; fi |
| 272 | echo "ERC1 SYNTHETIC FAIL $FAIL of $((PASS + FAIL)) checks" |
| 273 | exit 1 |