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 / check / audit.sh
| 1 | #!/bin/sh |
| 2 | # audit.sh <file.hex> <file.bin> <a64check> -- REFERENCE CHECK ONLY (never makes a byte of seed/). |
| 3 | # For every word of a hand-written hex file: the word as written, the instruction named in its comment, |
| 4 | # what our own decoder (seed/check/a64check.om) reads from it, what GNU objdump |
| 5 | # (-M no-aliases, a second independent decoder, reference only) reads from it, and whether each agrees |
| 6 | # with the name. Words whose comment does not start with [n] are data (ELF fields); their value is |
| 7 | # checked by the self-rebuild and by readelf. objdump spells LO as CC and HS as CS (the same codes, Table C1-1); |
| 8 | # those two spellings are mapped to LO and HS before comparing. Prints a Markdown table, then a summary line |
| 9 | # "AUDIT words=W code=C data=D decoder_mismatch=M objdump_mismatch=O byte_mismatch=B". Exit 0 iff all 0. |
| 10 | set -eu |
| 11 | hex=$1 bin=$2 chk=$3 |
| 12 | t=$(mktemp -d /tmp/seed-audit.XXXXXX) |
| 13 | trap 'rm -f "$t"/w "$t"/wl "$t"/b "$t"/d "$t"/o; rmdir "$t" 2>/dev/null' EXIT |
| 14 | # 1. words + comments in file order. In a hex1-language file, ":c" names the next word and "word@c" |
| 15 | # leaves the distance field (bits 5..23) for hex1 to fill: the comment names the operand "@c", which is |
| 16 | # replaced by the distance hex1 must have put there, and only the other bits are compared to the file. |
| 17 | awk 'FNR==NR { l=$0; i=index(l,"#"); if (i) l=substr(l,1,i-1); gsub(/[ \t\r]/,"",l); if (l ~ /^:/) lab[substr(l,2,1)]=n; else if (l!="") n++; next } |
| 18 | { l=$0; c=""; i=index(l,"#"); if (i) { c=substr(l,i+1); l=substr(l,1,i-1) } gsub(/[ \t\r]/,"",l); if (l=="" || l ~ /^:/) next |
| 19 | m=0; if (index(l,"@")) { t=substr(l,index(l,"@")+1,1); l=substr(l,1,8); m=1; d=lab[t]-k; j=index(c,"@" t); if (j) c=substr(c,1,j-1) sprintf("%+d",d) substr(c,j+2) } |
| 20 | k++; print l "\t" c "\t" m }' "$hex" "$hex" > "$t/w" |
| 21 | # 2. the words actually in the binary, read back with od (4-byte little-endian units) |
| 22 | od -An -v -tx4 -w4 "$bin" | tr -d ' ' > "$t/b" |
| 23 | cut -f1 "$t/w" | tr 'A-F' 'a-f' > "$t/wl" |
| 24 | "$chk" decode < "$t/b" > "$t/d" |
| 25 | objdump -D -z -b binary -m aarch64 -M no-aliases "$bin" | awk -F'\t' '/^ *[0-9a-f]+:\t/ { o=$3; if ($4!="") o=o "\t" $4; sub(/ *\/\/.*$/,"",o); sub(/^b\.cc\t/,"b.lo\t",o); sub(/^b\.cs\t/,"b.hs\t",o); print o }' > "$t/o" |
| 26 | paste "$t/w" "$t/b" "$t/d" "$t/o" | awk -F'\t' ' |
| 27 | function hx(v) { return sprintf("0x%x", v) } |
| 28 | function num(s) { s=s; if (s ~ /^#/) s=substr(s,2); return (s ~ /^0x/) ? strtonum(s) : s+0 } |
| 29 | function reg(r) { if (r=="xzr"||r=="wzr"||r=="sp"||r=="wsp") return 31; return substr(r,2)+0 } |
| 30 | function sf(r) { return (substr(r,1,1)=="x") ? 1 : 0 } |
| 31 | BEGIN { cc["eq"]=0;cc["ne"]=1;cc["hs"]=2;cc["cs"]=2;cc["lo"]=3;cc["cc"]=3;cc["mi"]=4;cc["pl"]=5;cc["vs"]=6;cc["vc"]=7; |
| 32 | cc["hi"]=8;cc["ls"]=9;cc["ge"]=10;cc["lt"]=11;cc["gt"]=12;cc["le"]=13;cc["al"]=14;cc["nv"]=15 |
| 33 | print "| Offset | Word | Bytes in file | Named (hand-decoded, from the comment) | Our decoder | ours | objdump -M no-aliases | objdump | Class |" |
| 34 | print "| :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |" } |
| 35 | { w=$1; com=$2; pat=$3; fb=$4; dec=$5; ob=$6; if ($7!="") ob=ob " " $7; gsub(/\t/," ",ob); gsub(/ +/," ",ob) |
| 36 | off=(NR-1)*4; words++; if (pat ? (and(strtonum("0x" fb), 0xFF00001F) != strtonum("0x" w)) : (fb!=tolower(w))) bytebad++ |
| 37 | if (com !~ /^ *\[[0-9]+\]/) { data++; c=com; sub(/^ */,"",c); gsub(/\|/,"/",c) |
| 38 | printf "| `0x%04x` | `%s` | `%s` | %s | - | n/a | - | n/a | DATA |\n", off, w, fb, c; next } |
| 39 | code++ |
| 40 | n=com; sub(/^ *\[[0-9]+\] */,"",n); i=index(n," "); if (i) n=substr(n,1,i-1) |
| 41 | i=index(n," ("); if (i) n=substr(n,1,i-1); i=index(n," ->"); if (i) n=substr(n,1,i-1) |
| 42 | sp=index(n," "); m=(sp?substr(n,1,sp-1):n); rest=(sp?substr(n,sp+1):"") |
| 43 | k=split(rest,a,/, */) |
| 44 | # expected objdump text |
| 45 | eo=m; ops="" |
| 46 | for (j=1;j<=k;j++) { x=a[j] |
| 47 | if ((m=="b"||m ~ /^b\./||m=="cbz"||m=="cbnz"||m=="adr") && j==k) x=hx(off+4*(x+0)) |
| 48 | else if (x ~ /^#-?[0-9]/ && x !~ /^#0x/ && x !~ /\]$/) { match(x,/^#-?[0-9]+/); x="#" hx(substr(x,2,RLENGTH-1)+0) substr(x,RLENGTH+1) } |
| 49 | else if (x ~ /^lsl #/) x=x |
| 50 | ops=ops (j>1?", ":"") x } |
| 51 | if (ops!="") eo=eo " " ops |
| 52 | # expected decoder fields |
| 53 | E_op="INVALID"; E_sf=0; E_rd=0; E_rn=0; E_rm=0; E_imm=0; E_hw=0; E_b=0; E_c=0 |
| 54 | if (m=="movz"||m=="movk") { E_op=toupper(m); E_sf=sf(a[1]); E_rd=reg(a[1]); E_imm=num(a[2]); if (k>=3) { s=a[3]; sub(/lsl #/,"",s); E_hw=s/16 } } |
| 55 | else if ((m=="add"||m=="sub") && a[3] ~ /^#/) { E_op="INVALID" } |
| 56 | else if (m=="add"||m=="sub"||m=="orr"||m=="and"||m=="eor"||m=="mul") { E_op=toupper(m); E_sf=sf(a[1]); E_rd=reg(a[1]); E_rn=reg(a[2]); E_rm=reg(a[3]) } |
| 57 | else if (m=="subs") { E_op="SUBS"; E_sf=sf(a[1]); E_rd=reg(a[1]); E_rn=reg(a[2]); if (a[3] !~ /^#/) E_rm=reg(a[3]) } |
| 58 | else if (m ~ /^b\./) { E_op="B_COND"; E_c=cc[substr(m,3)]; E_b=(a[1]+524288)%524288 } |
| 59 | else if (m=="b") { E_op="B"; E_b=(a[1]+67108864)%67108864 } |
| 60 | else if (m=="cbz"||m=="cbnz") { E_op=toupper(m); E_sf=sf(a[1]); E_rd=reg(a[1]); E_b=(a[2]+524288)%524288 } |
| 61 | else if (m=="ldrb"||m=="strb") { E_op=toupper(m); E_rd=reg(a[1]); r=a[2]; gsub(/[\[\]]/,"",r); E_rn=reg(r) } |
| 62 | else if (m=="ldr"||m=="str") { E_op=toupper(m); E_sf=sf(a[1]); E_rd=reg(a[1]); r=a[2]; gsub(/[\[\]]/,"",r); E_rn=reg(r) } |
| 63 | else if (m=="ret") { E_op="RET"; E_rn=30 } |
| 64 | else if (m=="adr") { E_op="ADR"; E_rd=reg(a[1]) } |
| 65 | else if (m=="madd" && a[4]=="xzr") { E_op="MUL"; E_sf=sf(a[1]); E_rd=reg(a[1]); E_rn=reg(a[2]); E_rm=reg(a[3]) } |
| 66 | ed=sprintf("op=%s sf=%d rd=%d rn=%d rm=%d imm16=%d hw=%d bimm=%d cond=%d",E_op,E_sf,E_rd,E_rn,E_rm,E_imm,E_hw,E_b,E_c) |
| 67 | dok=(ed==dec)?"agree":"MISMATCH"; if (E_op=="INVALID" && dec ~ /^op=INVALID/) dok="not covered (agree: INVALID)" |
| 68 | if (dok=="MISMATCH") dbad++ |
| 69 | ook=(eo==ob)?"agree":"MISMATCH"; if (ook=="MISMATCH") obad++ |
| 70 | printf "| `0x%04x` | `%s` | `%s` | `%s` | `%s` | %s | `%s` | %s | INSTRUCTION |\n", off, w, fb, n, dec, dok, ob, ook } |
| 71 | END { printf "\nAUDIT words=%d code=%d data=%d decoder_mismatch=%d objdump_mismatch=%d byte_mismatch=%d\n", words, code, data, dbad+0, obad+0, bytebad+0 |
| 72 | exit (dbad+obad+bytebad) ? 1 : 0 }' |
| 73 | rc=$? |
| 74 | nw=$(wc -l < "$t/w"); nb=$(wc -l < "$t/b") |
| 75 | [ "$nw" = "$nb" ] || { echo "AUDIT FAIL: $nw words written, $nb words in the binary"; rc=1; } |
| 76 | # (the temporary folder goes on exit, see trap) |
| 77 | exit $rc |