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 / qualification / om2 / oracle / leak / xcheck.py

128 lines · 5230 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# seed/qualification/om2/oracle/leak/xcheck.py: independent ORACLE (outside the delivered closure) for
4# seed/qualification/om2/leak/leakscan.om. It disassembles the same function-mode artifact with GNU
5# objdump (-D -b binary -m aarch64) and compares, word offset by word offset:
6# 1. the set of conditional branches (b.<cond>, cbz, cbnz, tbz, tbnz) and their targets,
7# 2. the set of loads/stores whose address involves a register other than sp/x29
8# (a base other than sp/x29, or any register index), from objdump's operand text,
9# 3. the words objdump cannot decode (.inst / undefined) against leakscan's UNKNOWN lines.
10# It uses only objdump's text, never leakscan's decoder. Usage:
11# xcheck.py ARTIFACT REPORT exit 0 when all three sets agree, 1 otherwise
12# xcheck.py --subset ARTIFACT REPORT decoder differential (fuzz.py): a word leakscan lists as
13# UNKNOWN may be anything to objdump (leakscan decodes a
14# subset); every other disagreement still fails
15import re
16import subprocess
17import sys
18
19
20def objdump_listing(path):
21 out = subprocess.run(["objdump", "-D", "-b", "binary", "-m", "aarch64", path],
22 check=True, capture_output=True, text=True).stdout
23 rows = {}
24 for line in out.splitlines():
25 m = re.match(r"^\s*([0-9a-f]+):\t([0-9a-f]{8}) \t(.*)$", line)
26 if not m:
27 m2 = re.match(r"^\s*([0-9a-f]+):\t([0-9a-f]{8}) \t?$", line)
28 if m2:
29 rows[int(m2.group(1), 16) // 4] = ("", "")
30 continue
31 text = m.group(3).split("//")[0].strip()
32 parts = text.split(None, 1)
33 rows[int(m.group(1), 16) // 4] = (parts[0], parts[1] if len(parts) > 1 else "")
34 return rows
35
36
37COND = re.compile(r"^(b\.[a-z]{2}|cbz|cbnz|tbz|tbnz)$")
38MEM = re.compile(r"^(ld|st)[a-z0-9]*$")
39
40
41def oracle_sets(rows):
42 br, mem, unk, spmem = {}, set(), set(), 0
43 for w, (mn, ops) in rows.items():
44 if mn in (".inst", "") or "undefined" in ops:
45 unk.add(w)
46 continue
47 if COND.match(mn):
48 addr = int(ops.split(",")[-1].strip(), 16)
49 if addr >= 1 << 63:
50 addr -= 1 << 64
51 br[w] = addr // 4
52 continue
53 if MEM.match(mn) and "[" in ops:
54 inside = ops[ops.index("[") + 1:ops.index("]")]
55 regs = [r.strip() for r in inside.split(",")]
56 base = regs[0]
57 index = [r for r in regs[1:] if re.match(r"^[xw][0-9]+$", r)]
58 if (base not in ("sp", "x29")) or index:
59 mem.add(w)
60 else:
61 spmem += 1
62 elif MEM.match(mn):
63 # a literal load ldr xN, <address>: no register in the address
64 pass
65 return br, mem, unk, spmem
66
67
68def tool_sets(report):
69 br, mem, unk = {}, set(), set()
70 with open(report) as f:
71 for line in f:
72 t = line.split()
73 if not t:
74 continue
75 if t[0] == "BRANCH":
76 tgt = int(t[3][3:])
77 if tgt >= 1 << 63:
78 tgt -= 1 << 64
79 br[int(t[1][1:])] = tgt
80 elif t[0] == "MEM":
81 mem.add(int(t[1][1:]))
82 elif t[0] == "UNKNOWN":
83 unk.add(int(t[1][1:]))
84 return br, mem, unk
85
86
87def main():
88 args = sys.argv[1:]
89 subset = bool(args) and args[0] == "--subset"
90 if subset:
91 args = args[1:]
92 if len(args) != 2:
93 print("usage: xcheck.py [--subset] ARTIFACT REPORT", file=sys.stderr)
94 return 90
95 ver = subprocess.run(["objdump", "--version"], capture_output=True, text=True).stdout.splitlines()[0]
96 rows = objdump_listing(args[0])
97 obr, omem, ounk, spmem = oracle_sets(rows)
98 tbr, tmem, tunk = tool_sets(args[1])
99 ok = True
100 tolerated = 0
101
102 def differ(w, text):
103 nonlocal ok, tolerated
104 if subset and w in tunk:
105 tolerated += 1
106 return
107 ok = False
108 print(text)
109
110 print(f"XCHECK oracle={ver!r} words={len(rows)}{' mode=subset' if subset else ''}")
111 print(f"XCHECK branches objdump={len(obr)} leakscan={len(tbr)}")
112 for w in sorted(set(obr) | set(tbr)):
113 if obr.get(w) != tbr.get(w):
114 differ(w, f" branch @{w}: objdump target={obr.get(w)} leakscan target={tbr.get(w)} {rows.get(w)}")
115 print(f"XCHECK mem-non-sp objdump={len(omem)} leakscan={len(tmem)} (sp/x29-only accesses not listed: {spmem})")
116 for w in sorted(omem ^ tmem):
117 differ(w, f" mem @{w}: objdump={'yes' if w in omem else 'no'} leakscan={'yes' if w in tmem else 'no'} {rows.get(w)}")
118 print(f"XCHECK undecodable objdump={len(ounk)} leakscan-unknown={len(tunk)}")
119 for w in sorted(ounk ^ tunk):
120 differ(w, f" unknown @{w}: objdump={'yes' if w in ounk else 'no'} leakscan={'yes' if w in tunk else 'no'} {rows.get(w)}")
121 if subset:
122 print(f"XCHECK tolerated={tolerated} (words leakscan lists as UNKNOWN that objdump decodes)")
123 print("XCHECK AGREE" if ok else "XCHECK DIFFER")
124 return 0 if ok else 1
125
126
127if __name__ == "__main__":
128 sys.exit(main())