interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / synapse / corpus_guard.sh

58 lines · 2589 bytes Raw Blame History
1#!/bin/sh
2# corpus_guard.sh: the J1 driver refuses a corpus it must not trust, with a typed line.
3# corpus_guard.sh DRIVER UNIT CORPUS CASE_OFFSET WORKDIR
4# CASE_OFFSET is the "case section at byte N" the driver printed for the real corpus. Each guard
5# edits a copy and expects one "corpus refused: CODE" line and exit 3:
6# G1 trailer byte flipped -> CORPUS_FINGERPRINT
7# G2 label byte changed, trailer recomputed -> CORPUS_PIN (parity alone would still pass)
8# G3 first case position set to 0xFFFFFFF0 -> CORPUS_RANGE (pin overridden to reach the check)
9# G4 first case base index set to 0xFFFF -> CORPUS_RANGE (pin overridden)
10# POSIX sh, dd, head, sha256sum, xxd.
11set -u
12drv=${1:?driver} unit=${2:?unit} corpus=${3:?corpus} off=${4:?case offset} dir=${5:?workdir}
13mkdir -p "$dir"
14fails=0
15
16poke() { # file offset octal-escaped-bytes
17 printf "$3" | dd of="$1" bs=1 seek="$2" conv=notrunc status=none
18}
19reseal() { # recompute the SHA-256 trailer of a copy; prints the new whole-file hash (the pin form)
20 n=$(($(wc -c < "$1") - 32))
21 h=$(head -c "$n" "$1" | sha256sum | cut -c1-64)
22 printf '%s' "$h" | xxd -r -p | dd of="$1" bs=1 seek="$n" conv=notrunc status=none
23 sha256sum "$1" | cut -c1-64
24}
25expect() { # name code file [pin override]
26 name=$1 code=$2 file=$3 ov=${4:-}
27 SYN_CORPUS_PIN_OVERRIDE=$ov "$drv" "$unit" "$file" 0 > "$dir/$name.out" 2>&1
28 rc=$?
29 line=$(grep '^corpus refused: ' "$dir/$name.out" | head -1)
30 case "$rc:$line" in
31 "3:corpus refused: $code:"*) echo "$name: OK exit 3, $line" ;;
32 *) echo "$name: WRONG exit $rc, want $code; got: $(grep -v '^FAIL' "$dir/$name.out" | tail -2 | tr '\n' ' ')"; fails=$((fails + 1)) ;;
33 esac
34}
35
36n=$(wc -c < "$corpus")
37cp "$corpus" "$dir/g1.bin"
38last=$(tail -c 1 "$dir/g1.bin" | xxd -p)
39if [ "$last" = 00 ]; then poke "$dir/g1.bin" $((n - 1)) '\001'; else poke "$dir/g1.bin" $((n - 1)) '\000'; fi
40expect G1 CORPUS_FINGERPRINT "$dir/g1.bin"
41
42cp "$corpus" "$dir/g2.bin"
43lab=$(grep -obUa 'fixture record 0' "$dir/g2.bin" | head -1 | cut -d: -f1)
44poke "$dir/g2.bin" "$lab" 'F'
45reseal "$dir/g2.bin" > /dev/null
46expect G2 CORPUS_PIN "$dir/g2.bin"
47
48cp "$corpus" "$dir/g3.bin"
49poke "$dir/g3.bin" $((off + 3)) '\360\377\377\377'
50h3=$(reseal "$dir/g3.bin")
51expect G3 CORPUS_RANGE "$dir/g3.bin" "$h3"
52
53cp "$corpus" "$dir/g4.bin"
54poke "$dir/g4.bin" "$off" '\377\377'
55h4=$(reseal "$dir/g4.bin")
56expect G4 CORPUS_RANGE "$dir/g4.bin" "$h4"
57
58if [ "$fails" -eq 0 ]; then echo "corpus guard: PASS (4 of 4 refused with the expected code)"; else echo "corpus guard: FAIL ($fails wrong)"; exit 1; fi