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 / seed / ng / a64 / tests / macho / run.sh

124 lines · 8353 bytes Raw Blame History
1#!/bin/sh
2# The Mach-O mode of ng a64 (`@ng a64 macho`, DESIGN.md section 11), checked in two steps, as the output is for
3# another system than the one the compiler runs on.
4# sh seed/ng/a64/tests/macho/run.sh bundle NG DIR on Linux AArch64, from the omega-mv root. NG is the ng
5# built from seed/ng/ng.units. Every seed/ng/tests/ir/x-*.om, seed/ng/a64/tests/front/*.om and
6# seed/ng/a64/tests/macho/*.om goes through front-ir and opt, then `@ng a64` and `@ng a64 macho`. A unit
7# either target refuses must be refused by both with the same line. Each ELF runs here; its status and
8# stdout sha256 go to DIR/linux.txt, the Mach-O files to DIR/m/. Two mutant compilers (built here with
9# seed/out/omc from a changed a64.om) write DIR/mut1/ and DIR/mut2/ for the controls below.
10# The library fixtures seed/ng/a64/tests/macho/lib/*.om (`target macos`: libfn, libptr, libcall; DESIGN.md
11# section 13) have no ELF: `@ng a64` must refuse each (exit 5, `a64: unsupported 241..243`). Their Mach-O
12# files go to DIR/lib/, what each must give (lib/expect.txt: name, status, stdout sha256) to DIR/lib.txt,
13# and four more mutant compilers write DIR/mut3/ .. DIR/mut6/. libback and libbackobjc are the callback
14# fixtures (DESIGN.md section 14): library code calls the Omega function libback.
15# sh run.sh run DIR on an arm64 Mac, from anywhere (copy DIR and this file).
16# Signs every Mach-O ad hoc, runs it and compares with linux.txt. Negative controls: mut1 (read and write
17# without the CSNEG, so a failed call is plus errno) must change badfd; mut2 (a class-1 global address
18# taken as x28 plus, not minus) must change gtext. Then the library fixtures against lib.txt, with their
19# controls: mut3 (libcall does not store d0 and d1 back) must change libmath; mut4 (libfn gives dlopen for
20# every number) must change libwrite; mut5 (the callback body does not pass x0 on) and mut6 (libcall does
21# not leave its stack pointer for a callback) must change libback.
22# Last line: NG-A64-MACHO-BUNDLE OK n, or NG-A64-MACHO PASS / FAIL n. Only OK and PASS exit 0.
23set -eu
24mode=${1:?usage: run.sh bundle NG DIR | run DIR}
25sha() { if command -v sha256sum > /dev/null; then sha256sum | cut -c1-64; else shasum -a 256 | cut -c1-64; fi; }
26if [ "$mode" = bundle ]; then
27 NG=${2:?}; D=${3:?}
28 [ "$(uname -sm)" = "Linux aarch64" ] || { echo "NG-A64-MACHO-BUNDLE NOT_RUN (host is $(uname -sm))"; exit 1; }
29 rm -rf "$D"; mkdir -p "$D/m" "$D/mut1" "$D/mut2" "$D/mut3" "$D/mut4" "$D/mut5" "$D/mut6" "$D/lib" "$D/w"
30 # the mutant compilers
31 mk() { # mk N SED-EXPRESSION
32 mkdir -p "$D/w/src$1"; : > "$D/w/ng$1.om"
33 while read -r _ p; do
34 if [ "$p" = seed/ng/src/a64.om ]; then sed "$2" "$p" > "$D/w/a64-$1.om"
35 ! cmp -s "$p" "$D/w/a64-$1.om" || { echo "mutant $1 not applied" >&2; exit 1; }
36 cat "$D/w/a64-$1.om" >> "$D/w/ng$1.om"
37 else cat "$p" >> "$D/w/ng$1.om"; fi
38 done < seed/ng/ng.units
39 seed/out/omc < "$D/w/ng$1.om" > "$D/w/ng$1"; chmod +x "$D/w/ng$1"
40 }
41 mk 1 '/^a6_ew a_csnegx 0 0 0 3 0$/d'
42 mk 2 's/^a6_ew a_subx d 28 d 0 0$/a6_ew a_addx d 28 d 0 0/'
43 mk 3 '/^a6_emitw 0x6d040680$/d'
44 mk 4 's/^a6_emitw 0xf8707a20$/a6_emitw 0xf9400220/'
45 mk 5 '/^a6_emitw 0xaa0003e1$/d'
46 mk 6 '/^a6_emitw 0xf9000d33$/d'
47 n=0; : > "$D/linux.txt"; : > "$D/refused.txt"
48 for f in seed/ng/tests/ir/x-*.om seed/ng/a64/tests/front/*.om seed/ng/a64/tests/macho/*.om; do
49 b=$(basename "$f" .om)
50 { printf '@ng front-ir\n'; cat "$f"; } | "$NG" > "$D/w/$b.ir" 2> /dev/null || { echo "$b front" >> "$D/refused.txt"; continue; }
51 { printf '@ng opt\n'; cat "$D/w/$b.ir"; } | "$NG" > "$D/w/$b.o.ir"
52 r1=0; { printf '@ng a64\n'; cat "$D/w/$b.o.ir"; } | "$NG" > "$D/w/$b.elf" 2> "$D/w/$b.e1" || r1=$?
53 r2=0; { printf '@ng a64 macho\n'; cat "$D/w/$b.o.ir"; } | "$NG" > "$D/m/$b" 2> "$D/w/$b.e2" || r2=$?
54 if [ $r1 -ne 0 ] || [ $r2 -ne 0 ]; then
55 rm -f "$D/m/$b"
56 [ $r1 -eq $r2 ] && cmp -s "$D/w/$b.e1" "$D/w/$b.e2" || { echo "FAIL $b: elf $r1, macho $r2, or another line"; exit 1; }
57 echo "$b a64 $r1 $(cat "$D/w/$b.e1")" >> "$D/refused.txt"; continue
58 fi
59 chmod +x "$D/w/$b.elf"
60 st=0; timeout 60 "$D/w/$b.elf" < /dev/null > "$D/w/$b.out" 2> /dev/null || st=$?
61 echo "$b $st $(sha < "$D/w/$b.out")" >> "$D/linux.txt"; n=$((n + 1))
62 for k in 1 2; do { printf '@ng a64 macho\n'; cat "$D/w/$b.o.ir"; } | "$D/w/ng$k" > "$D/mut$k/$b" 2> /dev/null || rm -f "$D/mut$k/$b"; done
63 done
64 for b in badfd gtext; do grep -q "^$b " "$D/linux.txt" || { echo "FAIL: control fixture $b missing"; exit 1; }; done
65 # the library fixtures: Mach-O only
66 nl=0
67 for f in seed/ng/a64/tests/macho/lib/*.om; do
68 b=$(basename "$f" .om)
69 { printf '@ng front-ir\n'; cat "$f"; } | "$NG" > "$D/w/$b.ir" 2> /dev/null || { echo "FAIL lib $b: front"; exit 1; }
70 { printf '@ng opt\n'; cat "$D/w/$b.ir"; } | "$NG" > "$D/w/$b.o.ir"
71 r1=0; { printf '@ng a64\n'; cat "$D/w/$b.o.ir"; } | "$NG" > /dev/null 2> "$D/w/$b.e1" || r1=$?
72 [ $r1 -eq 5 ] && grep -Eq '^a64: unsupported 24[123]$' "$D/w/$b.e1" || { echo "FAIL lib $b: the ELF target must refuse it (exit $r1)"; exit 1; }
73 { printf '@ng a64 macho\n'; cat "$D/w/$b.o.ir"; } | "$NG" > "$D/lib/$b" 2> /dev/null || { echo "FAIL lib $b: macho"; exit 1; }
74 for k in 3 4 5 6; do { printf '@ng a64 macho\n'; cat "$D/w/$b.o.ir"; } | "$D/w/ng$k" > "$D/mut$k/$b" 2> /dev/null || rm -f "$D/mut$k/$b"; done
75 grep -q "^$b " seed/ng/a64/tests/macho/lib/expect.txt || { echo "FAIL lib $b: no line in expect.txt"; exit 1; }
76 nl=$((nl + 1))
77 done
78 cp seed/ng/a64/tests/macho/lib/expect.txt "$D/lib.txt"
79 [ "$(wc -l < "$D/lib.txt" | tr -d ' ')" = "$nl" ] || { echo "FAIL: expect.txt lines and library fixtures differ"; exit 1; }
80 rm -rf "$D/w"
81 echo "NG-A64-MACHO-BUNDLE OK $n ($(wc -l < "$D/refused.txt" | tr -d ' ') refused by both targets; $nl library fixtures)"
82 exit 0
83fi
84D=${2:?}
85[ "$(uname -sm)" = "Darwin arm64" ] || { echo "NG-A64-MACHO NOT_RUN (host is $(uname -sm))"; exit 1; }
86echo "host $(uname -sm) $(sw_vers -productVersion)"
87fails=0; same=0
88one() { # one DIR NAME -> "status sha"
89 cp "$D/$1/$2" "$D/run.bin"; chmod 755 "$D/run.bin"
90 /usr/bin/codesign --force --sign - "$D/run.bin" 2> /dev/null || { echo "sign-failed -"; return; }
91 st=0; /usr/bin/perl -e 'alarm 60; exec @ARGV' "$D/run.bin" < /dev/null > "$D/run.out" 2> /dev/null || st=$?
92 echo "$st $(sha < "$D/run.out")"
93}
94while read -r b st h; do
95 got=$(one m "$b")
96 if [ "$got" = "$st $h" ]; then same=$((same + 1)); else fails=$((fails + 1)); echo "FAIL $b: linux $st $h, mac $got"; fi
97done < "$D/linux.txt"
98echo "PASS-COUNT $same of $(wc -l < "$D/linux.txt" | tr -d ' ') equal their ELF (status and stdout)"
99ctl() { # ctl MUTDIR NAME WHAT
100 want=$(grep "^$2 " "$D/linux.txt" | cut -d' ' -f2-)
101 got=$(one "$1" "$2")
102 if [ "$got" != "$want" ]; then echo "PASS control $1 ($3): $2 gives $got, not $want"
103 else fails=$((fails + 1)); echo "FAIL control $1 ($3): $2 unchanged"; fi
104}
105ctl mut1 badfd "no CSNEG after write"
106ctl mut2 gtext "class-1 global address added, not subtracted"
107ls=0
108while read -r b st h; do
109 got=$(one lib "$b")
110 if [ "$got" = "$st $h" ]; then ls=$((ls + 1)); else fails=$((fails + 1)); echo "FAIL lib $b: want $st $h, mac $got"; fi
111done < "$D/lib.txt"
112echo "PASS-COUNT $ls of $(wc -l < "$D/lib.txt" | tr -d ' ') library fixtures give their status and stdout"
113lctl() { # lctl MUTDIR NAME WHAT
114 want=$(grep "^$2 " "$D/lib.txt" | cut -d' ' -f2-)
115 got=$(one "$1" "$2")
116 if [ "$got" != "$want" ]; then echo "PASS control $1 ($3): $2 gives $got, not $want"
117 else fails=$((fails + 1)); echo "FAIL control $1 ($3): $2 unchanged"; fi
118}
119lctl mut3 libmath "libcall does not store d0 and d1 back"
120lctl mut4 libwrite "libfn gives dlopen for every number"
121lctl mut5 libback "the callback body does not pass x0 on"
122lctl mut6 libback "libcall does not leave its stack pointer for a callback"
123rm -f "$D/run.bin" "$D/run.out"
124if [ $fails -eq 0 ]; then echo "NG-A64-MACHO PASS"; else echo "NG-A64-MACHO FAIL $fails"; exit 1; fi