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 / evidence / SECURITY-673 / historical / gate669.sh.txt

137 lines · 7091 bytes Raw Blame History
1#!/bin/sh
2# gate669.sh -- qualification for Omega issue 669: AArch64 19-bit branch displacement.
3# Run from the repo root: sh seed/qualification/security673/gate669.sh [compiler]
4# The fixture generator is seeded .om (branch-corpus.om). The independent decoder is GNU objdump,
5# outside the seed compiler closure. The expected target is the unique mov x1, #12345 marker emitted
6# immediately after the conditional block. Results: evidence/SECURITY-673/.
7set -eu
8C=${1:-build/security673/fix/stage2}
9G=seed/qualification/security673/branch-corpus.om
10B=${B:-build/security673/gate669}
11E=evidence/SECURITY-673
12OBJDUMP=${OBJDUMP:-objdump}
13mkdir -p "$B" "$E"
14log=$E/gate669.log
15: > "$log"
16fail=0
17cases=0
18say() { printf '%s\n' "$*" | tee -a "$log"; }
19bad() { say "FAIL: $*"; fail=1; }
20sha() { sha256sum "$1" | cut -d' ' -f1; }
21norm() { h=${1#0x}; while [ "${h#0}" != "$h" ]; do h=${h#0}; done; [ -z "$h" ] && h=0; printf '%s' "$h"; }
22
23say "gate669 $(date -u +%FT%TZ)"
24say "source_head $(git rev-parse HEAD)"
25say "omc_source sha256 $(sha seed/omc/omc.om)"
26say "compiler $C sha256 $(sha "$C")"
27say "decoder $($OBJDUMP --version | head -1)"
28say "generator sha256 $(sha "$G")"
29
30# read count as 4 little-endian bytes without command substitution (NUL bytes would be dropped)
31le32() {
32 a=$(( $1 & 255 )); b=$(( ($1 >> 8) & 255 )); c=$(( ($1 >> 16) & 255 )); d=$(( ($1 >> 24) & 255 ))
33 printf "\\$(printf '%03o' "$a")\\$(printf '%03o' "$b")\\$(printf '%03o' "$c")\\$(printf '%03o' "$d")"
34}
35gen() { # mode count file
36 { printf "\\$(printf '%03o' "$1")"; le32 "$2"; } | ./$B/gen > "$3"
37}
38compile() { # source output error -> rc
39 set +e
40 "$C" < "$1" > "$2" 2> "$3"
41 rc=$?
42 set -e
43 printf '%s' "$rc"
44}
45expected_target() { # binary -> "branch_addr marker_addr"
46 tail -c +177 "$1" > "$B/dec.text"
47 "$OBJDUMP" -D -b binary -m aarch64 "$B/dec.text" > "$B/dec.dis" 2> "$B/dec.err" || return 1
48 m=$(awk '/#12345/ { a=$1; sub(":", "", a); print a; exit }' "$B/dec.dis")
49 b=$(awk '$3 ~ /^b\./ { print $4; exit }' "$B/dec.dis")
50 a=$(awk '$3 ~ /^b\./ { x=$1; sub(":", "", x); print x; exit }' "$B/dec.dis")
51 [ -n "$m" ] && [ -n "$b" ] && [ -n "$a" ] || return 1
52 printf '%s %s %s' "$(norm "$a")" "$(norm "$b")" "$(norm "$m")"
53}
54
55./"$C" < "$G" > "$B/gen" 2> "$B/gen.err" || { bad "generator did not build: $(cat "$B/gen.err")"; say "GATE669 FAIL"; exit 1; }
56chmod +x "$B/gen"
57say "built generator with compiler: $(sha "$B/gen")"
58
59# 1. sanity: the fixture is executable and the branch lands on the marker
60gen 0 16 "$B/small.om"
61rc=$(compile "$B/small.om" "$B/small.bin" "$B/small.err"); cases=$((cases + 1))
62if [ "$rc" = 0 ] && [ -s "$B/small.bin" ]; then
63 chmod +x "$B/small.bin"; set +e; ./$B/small.bin; rr=$?; set -e
64 got=$(expected_target "$B/small.bin" || true)
65 set -- $got; a=${1:-} t=${2:-} m=${3:-}
66 if [ "$rr" = 0 ] && [ -n "$t" ] && [ "$t" = "$m" ]; then
67 d=$(( (0x$m - 0x$a) / 4 ))
68 say "PASS sanity: N=16 exit 0, branch at 0x$a targets 0x$t = marker, displacement $d words"
69 else bad "sanity: exit $rr, branch 0x${t:-?} marker 0x${m:-?}"; fi
70else bad "sanity fixture did not compile: rc=$rc $(cat "$B/small.err")"; fi
71
72# 2. exact forward maximum: 2^18 - 1 words = 1,048,572 bytes
73gen 0 262142 "$B/max.om"
74rc=$(compile "$B/max.om" "$B/max.bin" "$B/max.err"); cases=$((cases + 1))
75if [ "$rc" = 0 ] && [ -s "$B/max.bin" ]; then
76 chmod +x "$B/max.bin"; set +e; ./$B/max.bin; rr=$?; set -e
77 got=$(expected_target "$B/max.bin" || true)
78 set -- $got; a=${1:-} t=${2:-} m=${3:-}
79 d=$(( (0x$m - 0x$a) / 4 ))
80 if [ "$rr" = 0 ] && [ "$t" = "$m" ] && [ "$d" = 262143 ]; then
81 say "PASS exact maximum: displacement $d words = 1048572 bytes, exit 0, objdump target = marker"
82 else bad "maximum: exit $rr, branch 0x${t:-?} marker 0x${m:-?}, d=$d"; fi
83else bad "maximum fixture did not compile: rc=$rc $(cat "$B/max.err")"; fi
84
85# 3. one word beyond the maximum must be refused with error 24 and no artifact
86gen 0 262143 "$B/over.om"
87rc=$(compile "$B/over.om" "$B/over.bin" "$B/over.err"); cases=$((cases + 1))
88want="omc: line 262148: error 24"
89if [ "$rc" = 24 ] && [ ! -s "$B/over.bin" ] && [ "$(cat "$B/over.err")" = "$want" ]; then
90 say "PASS one beyond: rc 24, empty stdout, stderr '$want'"
91else bad "one beyond: rc=$rc size=$(wc -c < "$B/over.bin") err=$(cat "$B/over.err")"; fi
92
93# 4. a displacement that fits 19 raw bits but not the signed range is also refused
94gen 0 524287 "$B/wrap.om"
95rc=$(compile "$B/wrap.om" "$B/wrap.bin" "$B/wrap.err"); cases=$((cases + 1))
96if [ "$rc" = 24 ] && [ ! -s "$B/wrap.bin" ] && grep -q 'error 24$' "$B/wrap.err"; then
97 say "PASS signed range: N=524287 (would wrap inside 19 bits) refused: $(cat "$B/wrap.err")"
98else bad "signed range: rc=$rc size=$(wc -c < "$B/wrap.bin") err=$(cat "$B/wrap.err")"; fi
99
100# 5. other reachable encoders: nested if (outer and inner), while exit, and a trap-site branch
101for spec in "1 nested-if" "2 while-exit" "3 trap-site"; do
102 set -- $spec; mode=$1; name=$2
103 gen "$mode" 300000 "$B/$name.om"
104 rc=$(compile "$B/$name.om" "$B/$name.bin" "$B/$name.err"); cases=$((cases + 1))
105 if [ "$rc" = 24 ] && [ ! -s "$B/$name.bin" ] && grep -q 'error 24$' "$B/$name.err"; then
106 say "PASS $name: 300000 statements refused before an artifact: $(cat "$B/$name.err")"
107 else bad "$name: rc=$rc size=$(wc -c < "$B/$name.bin") err=$(cat "$B/$name.err")"; fi
108done
109
110# 6. negative control: a compiler with the bound moved by one word accepts N=262144; the independent
111# decoder must see the branch miss the marker. The program itself exits 0, so the decoder is the oracle.
112sed 's/gt diff 1048572/gt diff 1048580/' seed/omc/omc.om > "$B/omc-mutant.om"
113cmp -s "$B/omc-mutant.om" seed/omc/omc.om && bad "mutant source did not change"
114./seed/rung4/om4.bin < "$B/omc-mutant.om" > "$B/mutant" 2> "$B/mutant.build.err" || bad "mutant did not build"
115chmod +x "$B/mutant" 2>/dev/null || true
116say "mutant compiler sha256 $(sha "$B/mutant")"
117gen 0 262144 "$B/mutant-case.om"
118rc=$( "$B/mutant" < "$B/mutant-case.om" > "$B/mutant-case.bin" 2> "$B/mutant-case.err"; echo $? ); cases=$((cases + 1))
119if [ "$rc" = 0 ] && [ -s "$B/mutant-case.bin" ]; then
120 chmod +x "$B/mutant-case.bin"
121 got=$(expected_target "$B/mutant-case.bin" || true)
122 set -- $got; a=${1:-} t=${2:-} m=${3:-}
123 set +e; ./$B/mutant-case.bin; rr=$?; set -e
124 if [ -n "$t" ] && [ "$t" != "$m" ]; then
125 say "PASS mutant caught by independent decoder: target 0x$t != marker 0x$m (program exit $rr; exit status alone is not the oracle)"
126 else bad "mutant escaped: decoder target 0x${t:-?} = marker 0x${m:-?}"; fi
127else bad "mutant case did not compile: rc=$rc $(cat "$B/mutant-case.err")"; fi
128
129# 7. fixed compiler refuses the same mutant case
130rc=$(compile "$B/mutant-case.om" "$B/fixed-case.bin" "$B/fixed-case.err"); cases=$((cases + 1))
131if [ "$rc" = 24 ] && [ ! -s "$B/fixed-case.bin" ]; then
132 say "PASS fixed compiler refuses the mutant case: $(cat "$B/fixed-case.err")"
133else bad "fixed compiler on mutant case: rc=$rc size=$(wc -c < "$B/fixed-case.bin")"; fi
134
135say "cases run: $cases"
136if [ "$fail" = 0 ]; then say "GATE669 PASS"; else say "GATE669 FAIL"; fi
137exit "$fail"