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 / sha / stack.py

112 lines · 4464 bytes Raw Blame History
1#!/usr/bin/python3
2# SPDX-License-Identifier: Apache-2.0
3# ORACLE (outside the delivered closure), OM-2 qualification L836 (#748), phaseB/sha-f5d14cb2.
4# Static code-size, rotate-instruction and stack-bound report for one omc function-mode artifact,
5# from GNU objdump 2.42 text (-D -b binary -m aarch64) plus the raw words.
6# Stack method: functions are the export entry targets (the leading B words) and every BL target.
7# Each function's CFG is walked from its entry (fallthrough, B, both edges of B.cond/CBZ/CBNZ/TBZ/
8# TBNZ; RET and BR end a path) while tracking the SP depth below the function's entry SP from
9# `sub sp, sp, #imm` / `add sp, sp, #imm` (the only SP writes the artifact contains; anything else
10# aborts). Depths must agree at joins and be 0 at RET. bound(f) = max over reached instructions of
11# depth, and of depth + bound(callee) at each BL (the call graph must be acyclic).
12# Usage: stack.py ARTIFACT
13import re
14import struct
15import subprocess
16import sys
17
18art = open(sys.argv[1], "rb").read()
19words = struct.unpack(f"<{len(art) // 4}I", art)
20dis = subprocess.run(["objdump", "-D", "-b", "binary", "-m", "aarch64", sys.argv[1]], check=True,
21 capture_output=True, text=True).stdout
22ins = {}
23for line in dis.splitlines():
24 m = re.match(r"^\s*([0-9a-f]+):\t[0-9a-f]{8} \t(\S+)\s*(.*)$", line)
25 if m:
26 ins[int(m.group(1), 16)] = (m.group(2), m.group(3))
27
28# rotates: by encoding (independent of objdump's alias choice) and by objdump mnemonic
29extr = sum(1 for w in words if w & 0x7FA00000 == 0x13800000) # EXTR (ROR immediate alias when Rn==Rm)
30ror_imm = sum(1 for w in words if w & 0x7FA00000 == 0x13800000 and (w >> 5) & 31 == (w >> 16) & 31)
31rorv = sum(1 for w in words if w & 0x7FE0FC00 == 0x1AC02C00) # RORV (DP 2-source, opcode 001011)
32obj_ror = sum(1 for (mn, _) in ins.values() if mn == "ror")
33obj_extr = sum(1 for (mn, _) in ins.values() if mn == "extr")
34obj_rorv = sum(1 for (mn, _) in ins.values() if mn == "rorv")
35print(f"SIZE bytes={len(art)} words={len(words)}")
36print(f"ROTATE encoding: EXTR={extr} (of which ROR-immediate alias Rn==Rm: {ror_imm}) RORV={rorv}; "
37 f"objdump mnemonics: ror={obj_ror} extr={obj_extr} rorv={obj_rorv}")
38
39entries = []
40for k in range(32):
41 w = words[k]
42 if w & 0xFC000000 != 0x14000000:
43 break
44 imm = w & 0x03FFFFFF
45 imm = imm - (1 << 26) if imm & (1 << 25) else imm
46 entries.append(4 * k + 4 * imm)
47
48
49def tgt(ops):
50 t = ops.split("//")[0].replace(",", " ").split()[-1]
51 return int(t, 16)
52
53
54memo = {}
55
56
57def bound(f, stack=()):
58 if f in memo:
59 return memo[f]
60 if f in stack:
61 sys.exit(f"call cycle at {f:#x}")
62 depth = {f: 0}
63 work = [f]
64 best = 0
65 calls = []
66 while work:
67 pc = work.pop()
68 d = depth[pc]
69 mn, ops = ins[pc]
70 nd = d
71 m = re.match(r"^(sub|add)$", mn) and re.match(r"^sp, sp, #(0x[0-9a-f]+|\d+)$", ops)
72 if m:
73 v = int(m.group(1), 0)
74 nd = d + v if mn == "sub" else d - v
75 elif re.search(r"(^|, )sp(,|$)", ops.split("[")[0]) and not ops.startswith("x") and not ops.startswith("w"):
76 sys.exit(f"unmodelled SP write at {pc:#x}: {mn} {ops}")
77 if re.search(r"\[sp[^\]]*\]!|\[sp\], #", ops):
78 sys.exit(f"SP writeback at {pc:#x}")
79 best = max(best, nd)
80 succ = []
81 if mn == "ret":
82 if nd != 0:
83 sys.exit(f"RET at {pc:#x} with depth {nd}")
84 elif mn == "br":
85 pass
86 elif mn == "b":
87 succ = [tgt(ops)]
88 elif mn.startswith("b.") or mn in ("cbz", "cbnz", "tbz", "tbnz"):
89 succ = [tgt(ops), pc + 4]
90 elif mn == "bl":
91 calls.append((pc, nd, tgt(ops)))
92 succ = [pc + 4]
93 else:
94 succ = [pc + 4]
95 for s in succ:
96 if s in depth:
97 if depth[s] != nd:
98 sys.exit(f"depth conflict at {s:#x}: {depth[s]} vs {nd}")
99 else:
100 depth[s] = nd
101 work.append(s)
102 for pc, d, c in calls:
103 best = max(best, d + bound(c, stack + (f,)))
104 memo[f] = best
105 return best
106
107
108names = ["sha256_init", "sha256_update", "sha256_final", "sha256_hash"]
109for k, e in enumerate(entries):
110 print(f"STACK export {k} ({names[k] if k < len(names) else '?'}) entry={e:#x} bound={bound(e)} bytes")
111for f in sorted(memo):
112 print(f"FRAME fn@{f:#x} bound-incl-callees={memo[f]}")