interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / tests / test_evo_jit.py

843 lines · 28002 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3"""Pillar 6 Verification: Live Self-Evolving SM121 SASS JIT (native/gpu/ejit.om).
4
5Verifies:
6 1. Pure-OM2 compilation for BOTH `target a64macos` and `gb10` (`om2`), plus full gb10 stack composition.
7 2. Self-contained SHA-256 bit-exactness against Python hashlib.sha256.
8 3. Positive evolutions:
9 - G6 Ascetic: yield=1 (w3 |= 8192) on FFMA/I2F/MUFU, 0 swaps, safe stall compaction, bit-exact parity.
10 - G1 Predator, G2 Mitosis Hybrid, G5 Trickster: safe swaps, BB leaders pinned, rawFloor>=6, I2F preserved.
11 4. Live Double-Buffer Hot-Swap Protocol (ejit_step_tick):
12 - Executes step t on active buffer A, mutates & proves into standby B, checks parity, flips A <-> B.
13 - Fail-closed rejection keeps active buffer untouched and does not flip activeSlot.
14 5. Planted negative controls for ALL Antibodies #1-#15 + nested BSSY (#16) + GPR ceiling (#17)
15 + EGP2 structural negative controls (#18) + parity mismatch (#19).
16
17Usage:
18 python3 -W error::ResourceWarning tests/test_evo_jit.py
19"""
20import hashlib
21import os
22import platform
23import shutil
24import subprocess
25import sys
26import tempfile
27
28HERE = os.path.dirname(os.path.abspath(__file__))
29REPO = os.path.dirname(HERE)
30EJIT_OM = os.path.join(REPO, "native", "gpu", "ejit.om")
31
32PASSES = []
33FAILS = []
34
35
36def check(cond, name, detail=""):
37 if cond:
38 PASSES.append(name)
39 print("PASS " + name)
40 else:
41 FAILS.append(name)
42 print("FAIL " + name + (": " + detail if detail else ""))
43
44
45def find_omc():
46 cand = os.environ.get("OMC") or shutil.which("omc") or os.path.expanduser("~/.local/bin/omc")
47 if not cand or not os.path.isfile(cand):
48 raise RuntimeError("omc compiler not found")
49 return cand
50
51
52def compile_om(omc, src_text, out_bin, sign_macos=False):
53 with tempfile.NamedTemporaryFile("w", suffix=".om", delete=False) as tf:
54 tf.write(src_text)
55 om_path = tf.name
56 try:
57 with open(om_path, "rb") as fin, open(out_bin + ".tmp", "wb") as fout:
58 p = subprocess.run([omc], stdin=fin, stdout=fout, stderr=subprocess.PIPE)
59 if p.returncode != 0:
60 raise RuntimeError("omc failed rc=%d:\n%s" % (p.returncode, p.stderr.decode("utf-8", "replace")))
61 os.chmod(out_bin + ".tmp", 0o755)
62 os.replace(out_bin + ".tmp", out_bin)
63 if sign_macos and platform.system() == "Darwin" and shutil.which("codesign"):
64 subprocess.run(["codesign", "-s", "-", "-f", out_bin],
65 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
66 finally:
67 if os.path.exists(om_path):
68 os.remove(om_path)
69
70
71# SM121 instruction encoders matching native/gpu/gk.om and bw_hazard.om
72def cw(st, wb=7, rb=7, wt=0, yld=0):
73 return (st << 9) | ((yld & 1) << 13) | ((wb & 7) << 14) | ((rb & 7) << 17) | ((wt & 63) << 20)
74
75
76def enc_movi(rd, imm, st=6, wb=7, rb=7, wt=0, yld=0):
77 return (30722 | (rd << 16), imm & 0xffffffff, 3840, cw(st, wb, rb, wt, yld))
78
79
80def enc_iadd3(rd, ra, rb, rc=255, st=6, wb=7, rb_bar=7, wt=0, yld=0):
81 return (29200 | (rd << 16) | (ra << 24), rb, 134209791 if rc == 255 else rc, cw(st, wb, rb_bar, wt, yld))
82
83
84def enc_imad(rd, ra, rb, rc=255, st=6, wb=7, rb_bar=7, wt=0, yld=0):
85 return (29220 | (rd << 16) | (ra << 24), rb, 126747136 | rc, cw(st, wb, rb_bar, wt, yld))
86
87
88def enc_ffma(rd, ra, rb, rc, st=6, yld=0):
89 return (29219 | (rd << 16) | (ra << 24), rb, rc, cw(st, 7, 7, 0, yld))
90
91
92def enc_i2f(rd, rb, st=6, yld=0):
93 return (29253 | (rd << 16), rb, 0, cw(st, 7, 7, 0, yld))
94
95
96def enc_mufu(rd, ra, op=5120, st=6, wb=0, wt=0, yld=0):
97 return (29448 | (rd << 16), ra, op, cw(st, wb, 7, wt, yld))
98
99
100def enc_ldg(rd, ra, off=0, wb=0, wt=0, st=4):
101 return (2433 | (7 << 12) | (rd << 16) | (ra << 24), 4 | (off << 8), 203299072, cw(st, wb, 7, wt, 1))
102
103
104def enc_isetp(p, ra, rb, cc=6, st=14):
105 return (29196 | (ra << 24), rb, 66060400 | (cc << 12) | (p << 17), cw(st, 7, 7, 0, 0))
106
107
108def enc_bra(here, tgt, p=7, st=5):
109 d = (tgt - here - 1) & 0xffffffff
110 w1 = (d >> 6) << 2
111 c = 58720256
112 if tgt <= here:
113 w1 |= 0xf0000000
114 c = 58982399
115 return (2375 | (p << 12) | ((d & 63) << 18), w1, c, cw(st, 7, 7, 0, 0))
116
117
118def enc_nop(st=4, wt=0):
119 return (2328, 0, 0, cw(st, 7, 7, wt, 0))
120
121
122def enc_bssy(st=2):
123 return (2373, 0, 0, cw(st, 7, 7, 0, 0))
124
125
126def enc_bsync(ra=255, st=2):
127 return (2447 | (ra << 24), 0, 0, cw(st, 7, 7, 0, 0))
128
129
130def enc_exit(st=5):
131 return (2381, 0, 58720256, cw(st, 7, 7, 0, 1))
132
133
134def emit_om_load_insns(fn_name, insns):
135 lines = ["fn %s to void" % fn_name]
136 for i, (w0, w1, w2, w3) in enumerate(insns):
137 b = i * 4
138 lines.append("put ejRef %d %d" % (b, w0))
139 lines.append("put ejRef %d %d" % (b + 1, w1))
140 lines.append("put ejRef %d %d" % (b + 2, w2))
141 lines.append("put ejRef %d %d" % (b + 3, w3))
142 lines.append("end")
143 return "\n".join(lines)
144
145
146def build_test_driver():
147 # Kernel 1: Full realistic SM121 multi-block kernel with:
148 # pc=0: MOVI R8, 10 (BB leader pc=0)
149 # pc=1: LDG R10, [R8] (wb=0, var=1)
150 # pc=2: NOP wait=1, st=4 (wait-NOP, compactible to st=1!)
151 # pc=3: IADD3 R11, R10, R8 (depends on LDG, st=8 -> compactible to 6)
152 # pc=4: IADD3 R20, R21, R22 (independent Tile B, st=7)
153 # pc=5: I2F R12, R11 (nm=29, st=6, yld=0 -> G6 sets yld=1, NEVER shave stall!)
154 # pc=6: FFMA R14, R12, R13, R14 (nm=24, st=6, yld=0 -> G6 sets yld=1!)
155 # pc=7: MUFU R15, R20 (nm=31, st=6, yld=0 -> G6 sets yld=1!)
156 # pc=8: ISETP P0, R11 >= R8 (anchor)
157 # pc=9: BRA P0 -> pc=12 (creates BB target pc=12 and fall-through leader pc=10!)
158 # pc=10: IADD3 R16, R14, R8 (BB fall-through leader!)
159 # pc=11: IADD3 R17, R20, R21 (independent non-anchor)
160 # pc=12: FFMA R18, R15, R16, R17 (BB target leader!)
161 # pc=13: IADD3 R23, R24, R25 (independent non-anchor)
162 # pc=14: EXIT
163 k1 = [
164 enc_movi(8, 10, st=6),
165 enc_ldg(10, 8, off=0, wb=0, wt=0, st=4),
166 enc_nop(st=4, wt=1),
167 enc_iadd3(11, 10, 8, st=8),
168 enc_iadd3(20, 21, 22, st=7),
169 enc_i2f(12, 11, st=6, yld=0),
170 enc_ffma(14, 12, 13, 14, st=6, yld=0),
171 enc_mufu(15, 20, st=6, wb=7, wt=0, yld=0),
172 enc_isetp(0, 11, 8, cc=6, st=14),
173 enc_bra(9, 12, p=0, st=5),
174 enc_iadd3(16, 14, 8, st=6),
175 enc_iadd3(17, 20, 21, st=6),
176 enc_ffma(18, 15, 16, 17, st=6, yld=0),
177 enc_iadd3(23, 24, 25, st=6),
178 enc_exit(st=5),
179 ]
180
181 # Kernel 2: Micro-kernel for I2F Antibody #15 (op 581, nm=29, st=4 + EXIT st=5 -> sum=9)
182 k_i2f = [
183 enc_i2f(12, 11, st=4, yld=0),
184 enc_exit(st=5),
185 ]
186
187 # Kernel 3: Nested BSSY test kernel (pc=0: BSSY, pc=1: BSSY, pc=2: BSYNC, pc=3: EXIT)
188 k_bssy_bad = [
189 enc_bssy(),
190 enc_bssy(),
191 enc_bsync(),
192 enc_exit(),
193 ]
194 k_bssy_ok = [
195 enc_bssy(),
196 enc_iadd3(10, 11, 12, st=6),
197 enc_bsync(),
198 enc_exit(),
199 ]
200
201 # Kernel 4: GPR ceiling tests (R62 in 64-reg kernel, R126 in 128-reg kernel, R125 valid in 128-reg)
202 k_gpr62 = [enc_iadd3(62, 8, 9, st=6), enc_exit()]
203 k_gpr126 = [enc_iadd3(126, 8, 9, st=6), enc_exit()]
204 k_gpr125 = [enc_iadd3(125, 8, 9, st=6), enc_exit()]
205
206 # Kernel 5: Fixed-latency ALU RAW floor=6 test (pc=0 IADD3 R11 -> pc=1 IADD3 R12, R11)
207 k_alu6 = [
208 enc_iadd3(11, 8, 9, st=6),
209 enc_iadd3(12, 11, 10, st=6),
210 enc_exit(st=5),
211 ]
212
213 # SHA-256 test message: "abc" (3 bytes)
214 msg = b"abc"
215 want_sha = hashlib.sha256(msg).hexdigest()
216
217 driver = []
218 driver.append(emit_om_load_insns("loadK1", k1))
219 driver.append(emit_om_load_insns("loadKI2f", k_i2f))
220 driver.append(emit_om_load_insns("loadKBssyBad", k_bssy_bad))
221 driver.append(emit_om_load_insns("loadKBssyOk", k_bssy_ok))
222 driver.append(emit_om_load_insns("loadKGpr62", k_gpr62))
223 driver.append(emit_om_load_insns("loadKGpr126", k_gpr126))
224 driver.append(emit_om_load_insns("loadKGpr125", k_gpr125))
225 driver.append(emit_om_load_insns("loadKAlu6", k_alu6))
226
227 driver.append(r"""
228text hexTbl "0123456789abcdef"
229text tSp " "
230text tNl "\n"
231text tSha "SHA_ABC "
232text kLoadK1 "LOAD_K1"
233text kInitAct "INIT_ACTIVE"
234text kG6Rc "TICK_G6_RC"
235text kG6Act "TICK_G6_ACTIVE"
236text kG6Swp "TICK_G6_SWAPS"
237text kG6Yld "TICK_G6_YIELDS"
238text kG6Flp "TICK_G6_FLIPS"
239text kG6YI "TICK_G6_YLD_I2F"
240text kG6YF "TICK_G6_YLD_FFMA"
241text kG6YM "TICK_G6_YLD_MUFU"
242text kG6SI "TICK_G6_STALL_I2F"
243text kG6SN "TICK_G6_STALL_NOP"
244text kG1Rc "TICK_G1_RC"
245text kG1Act "TICK_G1_ACTIVE"
246text kG1Swp "TICK_G1_SWAPS"
247text kG1Flp "TICK_G1_FLIPS"
248text kG1SR "TICK_G1_STALL_REF"
249text kG1SC "TICK_G1_STALL_CND"
250text kG2Rc "TICK_G2_RC"
251text kG2Act "TICK_G2_ACTIVE"
252text kG2Yld "TICK_G2_YIELDS"
253text kG2Flp "TICK_G2_FLIPS"
254text kG5Rc "TICK_G5_RC"
255text kG5Act "TICK_G5_ACTIVE"
256text kG5Flp "TICK_G5_FLIPS"
257text kParBad "PARITY_CORRUPT_OK"
258text kAb1 "AB1_DIGEST"
259text kAb2 "AB2_BIJECT"
260text kAb3 "AB3_SMUGGLE"
261text kAb4 "AB4_ANCHOR"
262text kAb5 "AB5_DEP"
263text kAb6 "AB6_HAZARD"
264text kAb7 "AB7_LENGTH"
265text kAb8 "AB8_SIG"
266text kAb9 "AB9_TRAIL"
267text kAb10 "AB10_QMD"
268text kAb11 "AB11_GEN"
269text kAb12 "AB12_OPCODE"
270text kAb13a "AB13_PC0"
271text kAb13b "AB13_BRATGT"
272text kAb13c "AB13_BRAFALL"
273text kAb14 "AB14_FLOOR6"
274text kAb15 "AB15_I2F"
275text kBsOk "BSSY_FLAT"
276text kAb16 "AB16_BSSY"
277text kAb17a "AB17_GPR62"
278text kAb17b "AB17_GPR126"
279text kGpr125 "GPR125_VALID"
280text kEg0 "EGP2_K0"
281text kEg1 "EGP2_K1"
282text kEgN1 "EGP2_NEG_RULE99"
283text kEgN2 "EGP2_NEG_SPOOF"
284text kL3Self "LOCK3_SELFTEST"
285text kL3R0 "LOCK3_RCPT_INIT"
286text kL3R4 "LOCK3_RCPT_AFTER4"
287text kL3Rs "LOCK3_RESET_OK"
288text kL3RpOk "LOCK3_REPLAY_OK"
289text kL3BufEq "LOCK3_BUF_EXACT"
290text kL3ActEq "LOCK3_ACT_EXACT"
291text kL3ParEq "LOCK3_PAR_EXACT"
292text kL3FlpEq "LOCK3_FLIP_COUNT"
293text kL3Stp4Rc "LOCK3_STEP4_RC"
294text kL3Stp4Flp "LOCK3_STEP4_FLIPS"
295text kL3TmpRc "LOCK3_TAMPER_RC"
296text kL3TmpCd "LOCK3_TAMPER_LASTCODE"
297text kL3TmpAct "LOCK3_TAMPER_NOFLIP"
298text kL3Ord1 "LOCK3_ORDER_SKIP"
299text kL3Ord2 "LOCK3_ORDER_BACK"
300text kL3Ord3 "LOCK3_ORDER_CURSOR"
301text kL3R0S "LOCK3_R0_STEP"
302text kL3R0G "LOCK3_R0_GEN"
303text kL3R1S "LOCK3_R1_STEP"
304text kL3R1G "LOCK3_R1_GEN"
305text kL3R2S "LOCK3_R2_STEP"
306text kL3R2G "LOCK3_R2_GEN"
307text kL3R3S "LOCK3_R3_STEP"
308text kL3R3G "LOCK3_R3_GEN"
309text kL3Sha3 "LOCK3_RCPT3_SHA "
310text kL3Words "LOCK3_FINAL_WORDS "
311
312fn wrt s view uchar * to void
313r = write 1 s 0 len s
314end
315
316fn wrtNum v ulong to void
317b = new uchar 24 0
318p = ulong 24
319x = v
320while true bound 24
321p = sub p 1
322put b p uchar add 48 rem x 10
323x = div x 10
324if eq x 0
325break
326end
327end
328r = write 1 b p sub 24 p
329end
330
331fn prLine tag view uchar * v ulong to void
332wrt tag
333wrt tSp
334wrtNum v
335wrt tNl
336end
337
338fn printSha to void
339hx = new uchar 64 0
340for i ulong 32
341b = uint get ejShaDig i
342put hx mul i 2 get hexTbl shr b 4
343put hx add mul i 2 1 get hexTbl and b 15
344end
345wrt tSha
346r = write 1 hx 0 64
347wrt tNl
348end
349
350fn setStall pc ulong nst ulong to void
351idx = add mul pc 4 3
352w3 = get ejCand idx
353nw3 = or and w3 not shl ulong 15 9 shl and nst 15 9
354put ejCand idx nw3
355end
356
357fn b2u b bool to ulong
358if b
359ret 1
360end
361ret 0
362end
363
364fn checkYld pc ulong to ulong
365ret and shr get ejCand add mul pc 4 3 13 1
366end
367
368fn runPositives to void
369loadK1
370ok = ejLoadKernelRef 15 64
371prLine kLoadK1 b2u ok
372prLine kInitAct get ejState 0
373
374note Step t=0: G6 Ascetic on A -> B
375rc0 = ejit_step_tick 6
376prLine kG6Rc rc0
377prLine kG6Act get ejState 0
378prLine kG6Swp get ejState 10
379prLine kG6Yld get ejState 11
380prLine kG6Flp get ejState 4
381prLine kG6YI checkYld 5
382prLine kG6YF checkYld 6
383prLine kG6YM checkYld 7
384prLine kG6SI ejCndStall 5
385prLine kG6SN ejCndStall 2
386
387note Step t=1: G1 Predator on B -> A
388rc1 = ejit_step_tick 1
389prLine kG1Rc rc1
390prLine kG1Act get ejState 0
391prLine kG1Swp get ejState 10
392prLine kG1Flp get ejState 4
393prLine kG1SR get ejState 12
394prLine kG1SC get ejState 13
395
396note Step t=2: G2 Mitosis Hybrid on A -> B
397rc2 = ejit_step_tick 2
398prLine kG2Rc rc2
399prLine kG2Act get ejState 0
400prLine kG2Yld get ejState 11
401prLine kG2Flp get ejState 4
402
403note Step t=3: G5 Trickster on B -> A
404rc3 = ejit_step_tick 5
405prLine kG5Rc rc3
406prLine kG5Act get ejState 0
407prLine kG5Flp get ejState 4
408
409note Fail-closed hot-swap test: corrupt BufB, verify parity check fails
410put ejBufB add mul ulong 6 4 0 29216
411parBad = ejit_parity_check 2 15
412prLine kParBad b2u parBad
413end
414
415fn runNegatives1 to void
416loadK1
417ok2 = ejLoadKernelRef 15 64
418
419note Antibody #1: Digest spoof
420ejCopyRefToCand 15
421ejHashWords true 15
422for i ulong 32
423put ejCertShaCnd i get ejCndSha i
424end
425put ejCertShaCnd 0 xor get ejCertShaCnd 0 1
426prLine kAb1 ejVerifySigned 15 240 240 64
427
428note Antibody #2: Non-bijection (duplicate perm[3]=perm[4])
429ejCopyRefToCand 15
430put ejPerm 3 get ejPerm 4
431prLine kAb2 ejTestVerifyCand 15 64
432
433note Antibody #3: Opcode smuggle (flip functional bit in w0 at pc=3)
434ejCopyRefToCand 15
435put ejCand mul ulong 3 4 xor get ejCand mul ulong 3 4 1
436prLine kAb3 ejTestVerifyCand 15 64
437
438note Antibody #4: Non-leader anchor escape (swap ISETP pc=8 with pc=7)
439ejCopyRefToCand 15
440ejSwapCand 7 8
441prLine kAb4 ejTestVerifyCand 15 64
442
443note Antibody #5: RAW/WAR/WAW dependency inversion (swap pc=3 IADD3 R11<-R10 with pc=5 I2F R12<-R11)
444ejCopyRefToCand 15
445ejSwapCand 3 5
446prLine kAb5 ejTestVerifyCand 15 64
447
448note Antibody #6: Scoreboard wait-barrier strip (clear wait mask on pc=2 NOP waiting on sb0)
449ejCopyRefToCand 15
450w3nop = get ejCand add mul ulong 2 4 3
451put ejCand add mul ulong 2 4 3 and w3nop not shl ulong 63 20
452prLine kAb6 ejTestVerifyCand 15 64
453
454note Antibody #7: Length mismatch
455ejCopyRefToCand 15
456ejHashWords true 15
457for i ulong 32
458put ejCertShaCnd i get ejCndSha i
459end
460prLine kAb7 ejVerifySigned 15 240 224 64
461
462note Antibodies #8-#11: uor-gpu Launch Gate
463prLine kAb8 ejCheckLaunchGate 0 512 512 4096 384 1
464prLine kAb9 ejCheckLaunchGate 1 512 513 4096 384 1
465prLine kAb10 ejCheckLaunchGate 1 512 512 4100 384 1
466prLine kAb11 ejCheckLaunchGate 1 512 512 4096 384 0
467end
468
469fn runNegatives2 to void
470loadK1
471ok3 = ejLoadKernelRef 15 64
472ejCopyRefToCand 15
473w3bad = get ejCand add mul ulong 3 4 3
474w3bad = or and w3bad not shl ulong 7 14 shl ulong 6 14
475put ejCand add mul ulong 3 4 3 w3bad
476prLine kAb12 ejTestVerifyCand 15 64
477
478note Antibody #13a: BB leader pc=0 escape (swap pc=0 and pc=3)
479loadK1
480ok4 = ejLoadKernelRef 15 64
481ejCopyRefToCand 15
482ejSwapCand 0 3
483prLine kAb13a ejTestVerifyCand 15 64
484
485note Antibody #13b: BB leader BRA target pc=12 escape (swap pc=12 and pc=13)
486ejCopyRefToCand 15
487ejSwapCand 12 13
488prLine kAb13b ejTestVerifyCand 15 64
489
490note Antibody #13c: BB leader BRA fall-through pc=10 escape (swap pc=10 and pc=11)
491ejCopyRefToCand 15
492ejSwapCand 10 11
493prLine kAb13c ejTestVerifyCand 15 64
494
495note Antibody #14: Fixed-latency ALU RAW floor < 6 (shave pc=0 IADD3 st=6 -> 5; even with flr=4 passed!)
496loadKAlu6
497oka6 = ejLoadKernelRef 3 64
498ejCopyRefToCand 3
499setStall 0 5
500ejHashWords true 3
501for i ulong 32
502put ejCertShaCnd i get ejCndSha i
503end
504prLine kAb14 ejMonkVerify 3 4 64
505
506note Antibody #15: I2F (op 581, nm=29) stall shave 4 -> 1 even when I2F+EXIT sum=6
507loadKI2f
508oki = ejLoadKernelRef 2 64
509ejCopyRefToCand 2
510setStall 0 1
511prLine kAb15 ejTestVerifyCand 2 64
512
513note Antibody #16: Nested BSSY vs flat BSSY
514loadKBssyOk
515okbo = ejLoadKernelRef 4 64
516prLine kBsOk ejTestVerifyCand 4 64
517loadKBssyBad
518okbb = ejLoadKernelRef 4 64
519prLine kAb16 ejTestVerifyCand 4 64
520
521note Antibody #17: GPR ceiling (R62 in 64-reg, R126 in 128-reg, R125 valid in 128-reg)
522loadKGpr62
523okg1 = ejLoadKernelRef 2 64
524prLine kAb17a ejTestVerifyCand 2 64
525loadKGpr126
526okg2 = ejLoadKernelRef 2 128
527prLine kAb17b ejTestVerifyCand 2 128
528loadKGpr125
529okg3 = ejLoadKernelRef 2 128
530prLine kGpr125 ejTestVerifyCand 2 128
531
532note EGP2 Structural Proof + Negative Controls
533prLine kEg0 ejEgVerifyKernel 0
534prLine kEg1 ejEgVerifyKernel 1
535ejEgReset
536n0 = ejEgAdd 2 0 0 0 0
537n1 = ejEgAdd 1 0 0 0 1
538n2 = ejEgAdd 3 n0 n1 0 0
539n3 = ejEgAdd 5 n0 n1 0 0
540prLine kEgN1 b2u ejEgVerifyStep 99 n2 n3
541prLine kEgN2 b2u ejEgVerifyStep 3 n2 n0
542end
543
544fn printRcptSha idx ulong to void
545hx = new uchar 64 0
546for i ulong 32
547b = uint ejit_rcpt_sha_byte idx i
548put hx mul i 2 get hexTbl shr b 4
549put hx add mul i 2 1 get hexTbl and b 15
550end
551wrt kL3Sha3
552r = write 1 hx 0 64
553wrt tNl
554end
555
556fn printBufWords act ulong cntWords ulong to void
557hx = new uchar 8 0
558wrt kL3Words
559for i ulong cntWords bound 64
560w = ejSimWord add act 1 i
561for k ulong 8
562sh = mul sub 7 k 4
563put hx k get hexTbl and shr w sh 15
564end
565r = write 1 hx 0 8
566end
567wrt tNl
568end
569
570fn runLock3Tests to void
571wantBuf = new ulong 60 0
572st = ejit_selftest
573prLine kL3Self ulong st
574
575loadK1
576okL3 = ejLoadKernelRef 15 64
577prLine kL3R0 ejit_rcpt_count
578rcT = ejit_step_tick 6
579rcT = ejit_step_tick 1
580rcT = ejit_step_tick 2
581rcT = ejit_step_tick 5
582prLine kL3R4 ejit_rcpt_count
583prLine kL3R0S ejit_rcpt_step 0
584prLine kL3R0G ejit_rcpt_gen 0
585prLine kL3R1S ejit_rcpt_step 1
586prLine kL3R1G ejit_rcpt_gen 1
587prLine kL3R2S ejit_rcpt_step 2
588prLine kL3R2G ejit_rcpt_gen 2
589prLine kL3R3S ejit_rcpt_step 3
590prLine kL3R3G ejit_rcpt_gen 3
591printRcptSha 3
592
593wantAct = get ejState 0
594wantPar = get ejState 14
595for i ulong 60
596put wantBuf i ejSimWord add wantAct 1 i
597end
598printBufWords wantAct 60
599
600ejResetForReplay
601rstOk = ulong 1
602if ne get ejState 0 0
603rstOk = ulong 0
604end
605if ne get ejState 3 0
606rstOk = ulong 0
607end
608if ne get ejState 4 0
609rstOk = ulong 0
610end
611if ne ejit_rcpt_count 4
612rstOk = ulong 0
613end
614prLine kL3Rs rstOk
615
616rplyOk = ulong 1
617for s ulong 4
618if ne ejit_replay_step s s 0
619rplyOk = ulong 0
620end
621end
622prLine kL3RpOk rplyOk
623prLine kL3ActEq b2u eq get ejState 0 wantAct
624prLine kL3ParEq b2u eq get ejState 14 wantPar
625prLine kL3FlpEq get ejState 4
626
627bufEq = ulong 1
628for i ulong 60
629if ne ejSimWord add wantAct 1 i get wantBuf i
630bufEq = ulong 0
631end
632end
633prLine kL3BufEq bufEq
634
635rcT = ejit_replay_step 4 4
636prLine kL3Stp4Rc rcT
637prLine kL3Stp4Flp get ejState 4
638end
639
640fn runLock3Negatives to void
641note Negative control 1: 1-bit SHA tamper on receipt 2 -> EJ_ERR_REPLAY_DIGEST (20)
642ejResetForReplay
643rcN = ejit_replay_step 0 0
644rcN = ejit_replay_step 1 1
645actBefore = get ejState 0
646ejit_tamper_rcpt_sha 2 15 uchar 4
647rcTmp = ejit_replay_step 2 2
648ejit_tamper_rcpt_sha 2 15 uchar 4
649prLine kL3TmpRc rcTmp
650prLine kL3TmpCd get ejState 6
651prLine kL3TmpAct b2u eq get ejState 0 actBefore
652
653note Negative control 2: out-of-order replay -> EJ_ERR_REPLAY_ORDER (21)
654ejResetForReplay
655prLine kL3Ord1 ejit_replay_step 1 0
656ejResetForReplay
657rcN = ejit_replay_step 0 0
658prLine kL3Ord2 ejit_replay_step 0 1
659ejResetForReplay
660prLine kL3Ord3 ejit_replay_step 0 1
661end
662
663fn main to int
664note 1. Self-contained SHA-256 check on "abc"
665put ejShaDat 0 97
666put ejShaDat 1 98
667put ejShaDat 2 99
668ejSha256 3
669printSha
670
671runPositives
672runNegatives1
673runNegatives2
674runLock3Tests
675runLock3Negatives
676ret 0
677end
678""")
679 return "\n".join(driver), want_sha
680
681
682def main():
683 omc = find_omc()
684 with open(EJIT_OM, "r", encoding="utf-8") as f:
685 ejit_src = f.read()
686
687 driver_src, want_sha = build_test_driver()
688
689 with tempfile.TemporaryDirectory() as tmp:
690 # 1. Verify compilation for gb10 (om2) and host target
691 gb10_om = "om2\n" + ejit_src + "\n" + driver_src
692 gb10_bin = os.path.join(tmp, "ejit_test_gb10")
693 compile_om(omc, gb10_om, gb10_bin, sign_macos=False)
694 check(os.path.isfile(gb10_bin) and os.path.getsize(gb10_bin) > 4096,
695 "compile_gb10_om2", "size=%d" % os.path.getsize(gb10_bin))
696
697 if platform.system() == "Darwin" and platform.machine() == "arm64":
698 mac_om = "target a64macos\nom2\n" + ejit_src + "\n" + driver_src
699 mac_bin = os.path.join(tmp, "ejit_test_a64macos")
700 compile_om(omc, mac_om, mac_bin, sign_macos=True)
701 check(os.path.isfile(mac_bin) and os.path.getsize(mac_bin) > 4096,
702 "compile_a64macos_om2", "size=%d" % os.path.getsize(mac_bin))
703 run_bin = mac_bin
704 elif platform.system() == "Linux" and platform.machine() == "aarch64":
705 run_bin = gb10_bin
706 else:
707 run_bin = None
708
709 if run_bin is None:
710 print("SKIP runtime execution on unsupported host arch:", platform.machine())
711 sys.exit(1 if FAILS else 0)
712
713 p = subprocess.run([run_bin], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
714 out = p.stdout.decode("utf-8", "replace")
715 err = p.stderr.decode("utf-8", "replace")
716 check(p.returncode == 0, "runtime_exit_0", "rc=%d err=%s" % (p.returncode, err))
717
718 kv = {}
719 for ln in out.splitlines():
720 parts = ln.strip().split()
721 if len(parts) == 2:
722 kv[parts[0]] = parts[1]
723
724 # 2. SHA-256 check
725 check(kv.get("SHA_ABC") == want_sha, "sha256_abc_exact",
726 "got=%s want=%s" % (kv.get("SHA_ABC"), want_sha))
727
728 # 3. Positive G6 Ascetic & Live Double-Buffer Hot-Swap ticks
729 check(kv.get("LOAD_K1") == "1" and kv.get("INIT_ACTIVE") == "0", "init_buf_A", str(kv))
730 check(kv.get("TICK_G6_RC") == "0", "tick_g6_pass", "rc=%s" % kv.get("TICK_G6_RC"))
731 check(kv.get("TICK_G6_ACTIVE") == "1" and kv.get("TICK_G6_FLIPS") == "1",
732 "tick_g6_atomic_flip_A_to_B", "active=%s flips=%s" % (kv.get("TICK_G6_ACTIVE"), kv.get("TICK_G6_FLIPS")))
733 check(kv.get("TICK_G6_SWAPS") == "0", "g6_zero_swaps", "swaps=%s" % kv.get("TICK_G6_SWAPS"))
734 check(int(kv.get("TICK_G6_YIELDS", "0")) >= 3, "g6_yields_tuned", "yields=%s" % kv.get("TICK_G6_YIELDS"))
735 check(kv.get("TICK_G6_YLD_I2F") == "1" and kv.get("TICK_G6_YLD_FFMA") == "1" and kv.get("TICK_G6_YLD_MUFU") == "1",
736 "g6_yield1_on_ffma_i2f_mufu")
737 check(kv.get("TICK_G6_STALL_I2F") == "6", "g6_preserves_i2f_stall",
738 "i2f_stall=%s" % kv.get("TICK_G6_STALL_I2F"))
739 check(kv.get("TICK_G6_STALL_NOP") == "1", "g6_compacts_wait_nop_stall",
740 "nop_stall=%s" % kv.get("TICK_G6_STALL_NOP"))
741
742 # G1 Predator, G2 Mitosis Hybrid, G5 Trickster + A <-> B round-trips
743 check(kv.get("TICK_G1_RC") == "0" and kv.get("TICK_G1_ACTIVE") == "0" and kv.get("TICK_G1_FLIPS") == "2",
744 "tick_g1_flip_B_to_A")
745 check(int(kv.get("TICK_G1_SWAPS", "0")) >= 1, "g1_predator_safe_swaps",
746 "swaps=%s" % kv.get("TICK_G1_SWAPS"))
747 check(int(kv.get("TICK_G1_STALL_CND", "999")) < int(kv.get("TICK_G1_STALL_REF", "0")),
748 "g1_compacts_total_stalls", "%s -> %s" % (kv.get("TICK_G1_STALL_REF"), kv.get("TICK_G1_STALL_CND")))
749 check(kv.get("TICK_G2_RC") == "0" and kv.get("TICK_G2_ACTIVE") == "1" and kv.get("TICK_G2_FLIPS") == "3",
750 "tick_g2_mitosis_flip_A_to_B")
751 check(int(kv.get("TICK_G2_YIELDS", "0")) >= 3, "g2_mitosis_yields_tuned")
752 check(kv.get("TICK_G5_RC") == "0" and kv.get("TICK_G5_ACTIVE") == "0" and kv.get("TICK_G5_FLIPS") == "4",
753 "tick_g5_trickster_flip_B_to_A")
754 check(kv.get("PARITY_CORRUPT_OK") == "0", "parity_gate_catches_corrupted_buf",
755 "got=%s" % kv.get("PARITY_CORRUPT_OK"))
756
757 # 4. Exact Antibody Rejection Codes (#1-#17 + EGP2)
758 expected_rejections = [
759 ("AB1_DIGEST", "1", "antibody_1_digest_spoof"),
760 ("AB2_BIJECT", "2", "antibody_2_non_bijection"),
761 ("AB3_SMUGGLE", "3", "antibody_3_opcode_smuggle"),
762 ("AB4_ANCHOR", "4", "antibody_4_anchor_escape"),
763 ("AB5_DEP", "5", "antibody_5_dep_inversion"),
764 ("AB6_HAZARD", "6", "antibody_6_scoreboard_hazard"),
765 ("AB7_LENGTH", "7", "antibody_7_length_mismatch"),
766 ("AB8_SIG", "8", "antibody_8_p0_sig_smuggle"),
767 ("AB9_TRAIL", "9", "antibody_9_trailing_bytes"),
768 ("AB10_QMD", "10", "antibody_10_misaligned_qmd"),
769 ("AB11_GEN", "11", "antibody_11_zero_gen_cap"),
770 ("AB12_OPCODE", "12", "antibody_12_illegal_opcode"),
771 ("AB13_PC0", "13", "antibody_13_bb_leader_pc0"),
772 ("AB13_BRATGT", "13", "antibody_13_bb_leader_bra_target"),
773 ("AB13_BRAFALL", "13", "antibody_13_bb_leader_bra_fallthrough"),
774 ("AB14_FLOOR6", "14", "antibody_14_6_cycle_alu_floor_unconditional"),
775 ("AB15_I2F", "15", "antibody_15_i2f_fixed_latency_shave"),
776 ("BSSY_FLAT", "0", "flat_bssy_accepted"),
777 ("AB16_BSSY", "16", "nested_bssy_rejected"),
778 ("AB17_GPR62", "17", "gpr_ceiling_64reg_r62_rejected"),
779 ("AB17_GPR126", "17", "gpr_ceiling_128reg_r126_rejected"),
780 ("GPR125_VALID", "0", "gpr_ceiling_128reg_r125_accepted"),
781 ("EGP2_K0", "0", "egp2_structural_proof_k0"),
782 ("EGP2_K1", "0", "egp2_structural_proof_k1"),
783 ("EGP2_NEG_RULE99", "0", "egp2_neg_unsound_rule_99_rejected"),
784 ("EGP2_NEG_SPOOF", "0", "egp2_neg_structural_spoof_rejected"),
785 ]
786 for key, want, name in expected_rejections:
787 check(kv.get(key) == want, name, "got=%s want=%s" % (kv.get(key), want))
788
789 # 5. Byte-Traceability Lock 3: Receipt-Logged Promotion & Deterministic Replay
790 check(kv.get("LOCK3_SELFTEST") == "0", "lock3_pure_om_selftest",
791 "rc=%s" % kv.get("LOCK3_SELFTEST"))
792 check(kv.get("LOCK3_RCPT_INIT") == "0" and kv.get("LOCK3_RCPT_AFTER4") == "4",
793 "lock3_receipt_count_0_to_4",
794 "init=%s after4=%s" % (kv.get("LOCK3_RCPT_INIT"), kv.get("LOCK3_RCPT_AFTER4")))
795 want_rcpt_pairs = [("0", "6"), ("1", "1"), ("2", "2"), ("3", "5")]
796 got_rcpt_pairs = [
797 (kv.get("LOCK3_R%d_STEP" % i), kv.get("LOCK3_R%d_GEN" % i)) for i in range(4)
798 ]
799 check(got_rcpt_pairs == want_rcpt_pairs, "lock3_receipt_step_and_genome_log",
800 "got=%s want=%s" % (got_rcpt_pairs, want_rcpt_pairs))
801
802 final_words_hex = kv.get("LOCK3_FINAL_WORDS", "")
803 check(len(final_words_hex) == 60 * 8, "lock3_final_words_len",
804 "len=%d" % len(final_words_hex))
805 if len(final_words_hex) == 60 * 8:
806 words_bytes = b"".join(
807 int(final_words_hex[i * 8:(i + 1) * 8], 16).to_bytes(4, "little")
808 for i in range(60)
809 )
810 want_rcpt3_sha = hashlib.sha256(words_bytes).hexdigest()
811 check(kv.get("LOCK3_RCPT3_SHA") == want_rcpt3_sha, "lock3_receipt_sha256_matches_python",
812 "got=%s want=%s" % (kv.get("LOCK3_RCPT3_SHA"), want_rcpt3_sha))
813
814 check(kv.get("LOCK3_RESET_OK") == "1", "lock3_reset_for_replay_preserves_receipts")
815 check(kv.get("LOCK3_REPLAY_OK") == "1", "lock3_replay_steps_0_to_3_ok")
816 check(kv.get("LOCK3_BUF_EXACT") == "1" and kv.get("LOCK3_ACT_EXACT") == "1"
817 and kv.get("LOCK3_PAR_EXACT") == "1" and kv.get("LOCK3_FLIP_COUNT") == "4",
818 "lock3_replay_bit_exact_buffer_and_parity",
819 "buf=%s act=%s par=%s flips=%s" % (
820 kv.get("LOCK3_BUF_EXACT"), kv.get("LOCK3_ACT_EXACT"),
821 kv.get("LOCK3_PAR_EXACT"), kv.get("LOCK3_FLIP_COUNT")))
822 check(kv.get("LOCK3_STEP4_RC") == "0" and kv.get("LOCK3_STEP4_FLIPS") == "4",
823 "lock3_replay_non_promotion_step_4_no_swap",
824 "rc=%s flips=%s" % (kv.get("LOCK3_STEP4_RC"), kv.get("LOCK3_STEP4_FLIPS")))
825 check(kv.get("LOCK3_TAMPER_RC") == "20" and kv.get("LOCK3_TAMPER_LASTCODE") == "20"
826 and kv.get("LOCK3_TAMPER_NOFLIP") == "1",
827 "lock3_1bit_sha_tamper_rejected_err_20",
828 "rc=%s code=%s noflip=%s" % (
829 kv.get("LOCK3_TAMPER_RC"), kv.get("LOCK3_TAMPER_LASTCODE"),
830 kv.get("LOCK3_TAMPER_NOFLIP")))
831 check(kv.get("LOCK3_ORDER_SKIP") == "21" and kv.get("LOCK3_ORDER_BACK") == "21"
832 and kv.get("LOCK3_ORDER_CURSOR") == "21",
833 "lock3_out_of_order_replay_rejected_err_21",
834 "skip=%s back=%s cur=%s" % (
835 kv.get("LOCK3_ORDER_SKIP"), kv.get("LOCK3_ORDER_BACK"),
836 kv.get("LOCK3_ORDER_CURSOR")))
837
838 print("\nSUMMARY: passed=%d failed=%d" % (len(PASSES), len(FAILS)))
839 sys.exit(1 if FAILS else 0)
840
841
842if __name__ == "__main__":
843 main()