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
omega-mv / seed / host / cpufence.sh
| 1 | #!/bin/sh |
| 2 | # cpufence.sh -- host CPU ordering words (#939): cpufence (keyword 199) and cpurelax (keyword 200). |
| 3 | # 1. seed/host/tests/cpufence.om runs (exit 42) natively on AArch64 and, with the x86 selector, under |
| 4 | # qemu-x86_64. |
| 5 | # 2. GNU objdump (reference only, never on the seed path) reads each binary back. Against the same program |
| 6 | # with the two words deleted, the AArch64 binary gains exactly one DSB SY (d5033f9f) per cpufence and one |
| 7 | # YIELD (d503203f) per cpurelax; the x86-64 binary gains one MFENCE (0f ae f0) and one PAUSE (f3 90). |
| 8 | # 3. Misuse is refused at compile time. |
| 9 | # 4. Negative control: a compiler built from omc.om with both cpufence encodings swapped for NOPs |
| 10 | # (AArch64 d503201f, x86 90 90 90) still runs the program to exit 42; check 2 must catch it. |
| 11 | set -eu |
| 12 | cd "$(dirname "$0")/../.." |
| 13 | root=$PWD |
| 14 | OMC=${OMC:-"$root/seed/out/omc"} |
| 15 | command -v qemu-x86_64 >/dev/null || { echo 'NOT_RUN cpufence: requires qemu-x86_64'; exit 3; } |
| 16 | command -v x86_64-linux-gnu-objdump >/dev/null || { echo 'NOT_RUN cpufence: requires x86_64-linux-gnu-objdump'; exit 3; } |
| 17 | mkdir -p build/seed/host |
| 18 | T=$(mktemp -d "$root/build/seed/host/cpufence.XXXXXX") |
| 19 | trap 'rm -rf "$T"' EXIT HUP INT TERM |
| 20 | bad() { printf 'FAIL cpufence: %s\n' "$*" >&2; exit 1; } |
| 21 | SRC=seed/host/tests/cpufence.om |
| 22 | nf=$(grep -c -x cpufence "$SRC") |
| 23 | nr=$(grep -c -x cpurelax "$SRC") |
| 24 | grep -v -x -e cpufence -e cpurelax "$SRC" > "$T/base.om" |
| 25 | printf 'target x86linux\n' | cat - "$SRC" > "$T/x86.om" |
| 26 | printf 'target x86linux\n' | cat - "$T/base.om" > "$T/x86base.om" |
| 27 | |
| 28 | a64n() { objdump -D -z -b binary -m aarch64 "$1" | awk -F'\t' -v w="$2" -v m="$3" -v o="$4" \ |
| 29 | '$2 ~ "^" w && $3 == m && $4 == o {n++} END {print n + 0}'; } |
| 30 | x86n() { x86_64-linux-gnu-objdump -D -z -b binary -m i386:x86-64 "$1" | awk -F'\t' -v w="$2" -v m="$3" \ |
| 31 | '{b=$2; sub(/ +$/, "", b); a=$3; sub(/ +$/, "", a)} b == w && a == m {n++} END {print n + 0}'; } |
| 32 | run() { # compiler source out arch -> exit status |
| 33 | "$1" < "$2" > "$3" |
| 34 | chmod +x "$3" |
| 35 | set +e |
| 36 | if [ "$4" = x86 ]; then qemu-x86_64 "$3"; else "$3"; fi |
| 37 | rc=$? |
| 38 | set -e |
| 39 | echo "$rc" |
| 40 | } |
| 41 | # measure compiler tag: prints "dsb yield mfence pause" deltas over the word-free program |
| 42 | measure() { |
| 43 | for f in prog base; do |
| 44 | src=$SRC; [ $f = base ] && src=$T/base.om |
| 45 | rc=$(run "$1" "$src" "$T/$2-$f.a64" a64) |
| 46 | [ "$rc" = 42 ] || bad "$2 $f AArch64 exit $rc, wanted 42" |
| 47 | xs=$T/x86.om; [ $f = base ] && xs=$T/x86base.om |
| 48 | rc=$(run "$1" "$xs" "$T/$2-$f.x86" x86) |
| 49 | [ "$rc" = 42 ] || bad "$2 $f x86-64 exit $rc, wanted 42" |
| 50 | done |
| 51 | d=$(( $(a64n "$T/$2-prog.a64" d5033f9f dsb sy) - $(a64n "$T/$2-base.a64" d5033f9f dsb sy) )) |
| 52 | y=$(( $(a64n "$T/$2-prog.a64" d503203f yield '') - $(a64n "$T/$2-base.a64" d503203f yield '') )) |
| 53 | m=$(( $(x86n "$T/$2-prog.x86" '0f ae f0' mfence) - $(x86n "$T/$2-base.x86" '0f ae f0' mfence) )) |
| 54 | p=$(( $(x86n "$T/$2-prog.x86" 'f3 90' pause) - $(x86n "$T/$2-base.x86" 'f3 90' pause) )) |
| 55 | echo "$d $y $m $p" |
| 56 | } |
| 57 | want="$nf $nr $nf $nr" |
| 58 | got=$(measure "$OMC" real) |
| 59 | [ "$got" = "$want" ] || bad "instruction deltas (dsb yield mfence pause) $got, wanted $want" |
| 60 | printf 'PASS cpufence runs exit 42 on AArch64 and x86-64 (qemu); objdump deltas dsb=%s yield=%s mfence=%s pause=%s\n' $got |
| 61 | |
| 62 | refuse() { # name want-exit source-line... |
| 63 | name=$1; wantc=$2; shift 2 |
| 64 | printf '%s\n' "$@" > "$T/$name.om" |
| 65 | set +e |
| 66 | "$OMC" < "$T/$name.om" > "$T/$name.out" 2> "$T/$name.err" |
| 67 | c=$? |
| 68 | set -e |
| 69 | [ "$c" = "$wantc" ] && [ ! -s "$T/$name.out" ] || bad "refusal $name exit $c, wanted $wantc" |
| 70 | } |
| 71 | refuse operand 5 'fn main to int' 'cpufence 1' 'ret 0' 'end' |
| 72 | refuse as-value 16 'fn main to int' 'x = cpufence' 'ret 0' 'end' |
| 73 | refuse as-name 5 'fn main to int' 'cpurelax = int 1' 'ret 0' 'end' |
| 74 | refuse top-level 16 'cpufence' 'fn main to int' 'ret 0' 'end' |
| 75 | refuse dead 17 'fn main to int' 'ret 0' 'cpufence' 'end' |
| 76 | printf '%s\n' 'fn f x ulong to ulong' 'cpufence' 'cpurelax' 'ret x' 'end' > "$T/bare.om" |
| 77 | "$OMC" < "$T/bare.om" > "$T/bare.bin" |
| 78 | [ "$(a64n "$T/bare.bin" d5033f9f dsb sy) $(a64n "$T/bare.bin" d503203f yield '')" = "1 1" ] || |
| 79 | bad "function-mode unit lacks exactly one dsb sy and one yield" |
| 80 | printf 'PASS cpufence/cpurelax in a function-mode unit: one dsb sy, one yield\n' |
| 81 | printf 'PASS cpufence refusals: operand, value use, name, top level, dead code\n' |
| 82 | |
| 83 | awk '/^if eq c 199$/{f=1} f && /^emit 3573759903$/{$0="emit 3573751839"} f && /^xb 15 174 240$/{$0="xb 144 144 144"} |
| 84 | f && /^ret 0$/{f=0} {print}' seed/omc/omc.om > "$T/mutant.om" |
| 85 | cmp -s seed/omc/omc.om "$T/mutant.om" && bad "mutant site not found" |
| 86 | "$OMC" < "$T/mutant.om" > "$T/omc-mutant" |
| 87 | chmod +x "$T/omc-mutant" |
| 88 | mgot=$(measure "$T/omc-mutant" mutant) |
| 89 | [ "$mgot" = "0 $nr 0 $nr" ] || bad "nop mutant gave deltas $mgot, wanted 0 $nr 0 $nr" |
| 90 | printf 'PASS negative control: nop cpufence mutant still exits 42 but objdump deltas %s are caught (wanted %s)\n' "$mgot" "$want" |