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 / check / audit2.sh

98 lines · 5693 bytes Raw Blame History
1#!/bin/sh
2# audit2.sh <prog.s> <prog.bin> -- REFERENCE CHECK ONLY (never makes a byte of seed/).
3# For a program written in asm2's language (seed/rung2) and the bytes asm2 made from it: every
4# instruction line is read back by GNU objdump (-M no-aliases, the manual's base names) and must show
5# 1. the base instruction the asm2 name stands for (map below, names as in the Arm ARM headings),
6# 2. the registers written in the source, in the same order (objdump may show more, e.g. xzr),
7# 3. for a label operand, the address of that label.
8# Data lines (.w, .b) are skipped (counted). Prints a Markdown table of every instruction and a summary
9# line "AUDIT2 instructions=N data_bytes=D mismatch=M"; exit 1 when M > 0.
10set -eu
11s=$1; b=$2
12t=$(mktemp -d); trap 'rm -rf "$t"' EXIT
13objdump -D -z -b binary -m aarch64 -M no-aliases "$b" | grep -E '^ +[0-9a-f]+:' |
14 awk -F"\t" '{a=$1; sub(/^ +/,"",a); sub(/:$/,"",a); print a "\t" $2 "\t" $3 "\t" $4}' > "$t/od"
15awk -M -v od="$t/od" '
16function strip(x) { sub(/;.*/, "", x); gsub(/,/, " ", x); return x }
17BEGIN {
18 # asm2 name (up to the first dot, or the whole name for the forms below) -> objdump base mnemonic
19 m["add"]="add"; m["sub"]="sub"; m["subs"]="subs"; m["cmp"]="subs"; m["and"]="and"; m["orr"]="orr"
20 m["eor"]="eor"; m["mov"]="orr"; m["mul"]="madd"; m["madd"]="madd"; m["msub"]="msub"; m["udiv"]="udiv"
21 m["lslv"]="lslv"; m["lsrv"]="lsrv"; m["asrv"]="asrv"; m["movz"]="movz"; m["movk"]="movk"
22 m["ldr"]="ldr"; m["str"]="str"; m["ldrb"]="ldrb"; m["strb"]="strb"; m["b"]="b"; m["bl"]="bl"
23 m["cbz"]="cbz"; m["cbnz"]="cbnz"; m["adr"]="adr"; m["ret"]="ret"; m["br"]="br"; m["blr"]="blr"
24 m["svc"]="svc"; m["ubfm"]="ubfm"; m["sbfm"]="sbfm"; m["ldp"]="ldp"; m["stp"]="stp"; m["ldur"]="ldur"
25 m["stur"]="stur"; m["rev"]="rev"; m["extr"]="extr"; m["bic"]="bic"; m["adrp"]="adrp"; m["movn"]="movn"
26 m["msr"]="msr"; m["cmn"]="adds"; m["nop"]="hint"; m["wfe"]="hint"
27 while ((getline l < od) > 0) { split(l, f, "\t"); a0 = strtonum("0x" f[1]); txt[a0] = f[3] " " f[4]; wd[a0] = f[2] }
28}
29# pass 1 (first file read): label offsets
30FNR == NR {
31 x = strip($0); n = split(x, w, /[ \t]+/); k = 1; if (w[1] == "") k = 2
32 if (k > n) next
33 if (substr(w[k],1,1) == ":") { lab[substr(w[k],2)] = off; next }
34 if (w[k] == ".b") off += 1; else off += 4
35 next
36}
37# pass 2: check each instruction
38{
39 x = strip($0); n = split(x, w, /[ \t]+/); k = 1; if (w[1] == "") k = 2
40 if (k > n) next
41 if (substr(w[k],1,1) == ":") next
42 if (w[k] == ".b") { off2 += 1; data += 1; next }
43 if (w[k] == ".w") { off2 += 4; data += 4; next }
44 name = w[k]; o = off2 + 0; off2 += 4; ins++
45 d = txt[o]
46 split(d, dw, /[ \t,\[\]!]+/); mn = dw[1]
47 base = name; if (name ~ /^b\./) base = "b.c"; else sub(/\..*/, "", base)
48 want = (base == "b.c") ? substr(name, 1) : m[base]
49 if (base == "b.c") { gsub(/^b\.cs/, "b.hs", mn); gsub(/^b\.cc/, "b.lo", mn) }
50 ok = (mn == want)
51 # registers in the source, in order, must be a subsequence of those in the decode
52 nd = 0; delete dr
53 for (i = 2; i in dw; i++) if (dw[i] ~ /^([xw]([0-9]|[12][0-9]|30)|xzr|wzr|sp|wsp)$/) dr[++nd] = dw[i]
54 j = 1
55 for (i = k + 1; i <= n; i++) {
56 a = w[i]
57 if (a ~ /^([xw]([0-9]|[12][0-9]|30)|xzr|wzr|sp|wsp)$/) {
58 while (j <= nd && dr[j] != a) j++
59 if (j > nd) ok = 0; else j++
60 } else if (a in lab) {
61 tg = ""; for (q = 2; q in dw; q++) if (dw[q] ~ /^0x[0-9a-f]+$/) tg = dw[q]
62 if (tg == "" || strtonum(tg) != lab[a]) ok = 0
63 }
64 }
65 # numbers: the numbers in the source, scaled as the fields of the manual hold them, must equal the
66 # #numbers of the decode, in order (LDR/STR unsigned offsets are in access-size units in the source; MOVZ/MOVK hw
67 # shows as "lsl #16*hw"; objdump leaves out a zero unsigned offset and a zero hw shift)
68 ns = 0; delete sn
69 for (i = k + 1; i <= n; i++) if (w[i] ~ /^-?(0x[0-9a-f]+|[0-9]+)$/) sn[++ns] = strtonum(w[i])
70 # (LDP/STP offsets are in register-size units, shown in bytes, a zero left out only in the plain
71 # signed-offset form; LDR (register) .w shows its fixed LSL #2; NOP and WFE show as HINT #0 / #2;
72 # AND (immediate) shows the mask that N:immr:imms stand for; ADRP shows the page it points at)
73 sc = 0; if (name ~ /^(ldr|str)\.x$/) sc = 8; else if (name ~ /^(ldr|str)\.w$/) sc = 4; else if (name ~ /^(ldrb|strb)$/) sc = 1
74 ne = 0; delete ex
75 if (sc) { if (sn[1] != 0) ex[++ne] = sn[1] * sc }
76 else if (name ~ /^(ldp|stp)\./) { ps = (name ~ /^(ldp|stp)\.x/) ? 8 : 4
77 if (sn[1] != 0 || name ~ /\.(pre|post)$/) ex[++ne] = sn[1] * ps }
78 else if (name ~ /^mov[zkn]\./) { ex[++ne] = sn[1]; if (sn[2] != 0) ex[++ne] = sn[2] * 16 }
79 else if (name == "ldr.w.r") ex[++ne] = 2
80 else if (name == "ldr.x.r" || name == "str.x.r") ex[++ne] = 3
81 else if (name == "nop") ex[++ne] = 0
82 else if (name == "wfe") ex[++ne] = 2
83 else if (name == "and.x.bm") { if (sn[1] != 1) ok = 0
84 v = 2 ^ (sn[3] + 1) - 1; r = sn[2]; ex[++ne] = rshift(v, r) + lshift(v % 2 ^ r, 64 - r) }
85 else if (name == "adrp") { tg = ""; for (q = 2; q in dw; q++) if (dw[q] ~ /^0x[0-9a-f]+$/) tg = dw[q]
86 if (tg == "" || strtonum(tg) != o - o % 4096 + (sn[1] * 4 + sn[2]) * 4096) ok = 0 }
87 else for (i = 1; i <= ns; i++) ex[++ne] = sn[i]
88 nn = 0; delete dn
89 for (i = 2; i in dw; i++) if (dw[i] ~ /^#-?(0x[0-9a-f]+|[0-9]+)$/) { v = substr(dw[i], 2); dn[++nn] = (v ~ /^-/) ? -strtonum(substr(v, 2)) : strtonum(v) }
90 if (nn != ne) ok = 0
91 for (i = 1; i <= ne; i++) if (dn[i] != ex[i]) ok = 0
92 if (!ok) bad++
93 printf("| %d | %s | %s | %s |\n", o, wd[o], x, (ok ? d : "MISMATCH: " d))
94}
95END { printf("\nAUDIT2 instructions=%d data_bytes=%d mismatch=%d\n", ins, data, bad + 0); exit(bad > 0) }
96' "$s" "$s"
97rc=$?
98exit $rc