Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-helix/blob/main/tests/test_evo_jit.py # omega-helix This project has not described itself yet. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 5 Verifies: 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 17 Usage: 18 python3 -W error::ResourceWarning tests/test_evo_jit.py 19 """ 20 import hashlib 21 import os 22 import platform 23 import shutil 24 import subprocess 25 import sys 26 import tempfile 27 28 HERE = os.path.dirname(os.path.abspath(__file__)) 29 REPO = os.path.dirname(HERE) 30 EJIT_OM = os.path.join(REPO, "native", "gpu", "ejit.om") 31 32 PASSES = [] 33 FAILS = [] 34 35 36 def 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 45 def 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 52 def 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 72 def 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 76 def 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 80 def 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 84 def 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 88 def 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 92 def enc_i2f(rd, rb, st=6, yld=0): 93 return (29253 | (rd << 16), rb, 0, cw(st, 7, 7, 0, yld)) 94 95 96 def 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 100 def 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 104 def 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 108 def 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 118 def enc_nop(st=4, wt=0): 119 return (2328, 0, 0, cw(st, 7, 7, wt, 0)) 120 121 122 def enc_bssy(st=2): 123 return (2373, 0, 0, cw(st, 7, 7, 0, 0)) 124 125 126 def enc_bsync(ra=255, st=2): 127 return (2447 | (ra << 24), 0, 0, cw(st, 7, 7, 0, 0)) 128 129 130 def enc_exit(st=5): 131 return (2381, 0, `58720256`, cw(st, 7, 7, 0, 1)) 132 133 134 def 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 146 def 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""" 228 text hexTbl "`0123456789abcdef`" 229 text tSp " " 230 text tNl "\n" 231 text tSha "SHA_ABC " 232 text kLoadK1 "LOAD_K1" 233 text kInitAct "INIT_ACTIVE" 234 text kG6Rc "TICK_G6_RC" 235 text kG6Act "TICK_G6_ACTIVE" 236 text kG6Swp "TICK_G6_SWAPS" 237 text kG6Yld "TICK_G6_YIELDS" 238 text kG6Flp "TICK_G6_FLIPS" 239 text kG6YI "TICK_G6_YLD_I2F" 240 text kG6YF "TICK_G6_YLD_FFMA" 241 text kG6YM "TICK_G6_YLD_MUFU" 242 text kG6SI "TICK_G6_STALL_I2F" 243 text kG6SN "TICK_G6_STALL_NOP" 244 text kG1Rc "TICK_G1_RC" 245 text kG1Act "TICK_G1_ACTIVE" 246 text kG1Swp "TICK_G1_SWAPS" 247 text kG1Flp "TICK_G1_FLIPS" 248 text kG1SR "TICK_G1_STALL_REF" 249 text kG1SC "TICK_G1_STALL_CND" 250 text kG2Rc "TICK_G2_RC" 251 text kG2Act "TICK_G2_ACTIVE" 252 text kG2Yld "TICK_G2_YIELDS" 253 text kG2Flp "TICK_G2_FLIPS" 254 text kG5Rc "TICK_G5_RC" 255 text kG5Act "TICK_G5_ACTIVE" 256 text kG5Flp "TICK_G5_FLIPS" 257 text kParBad "PARITY_CORRUPT_OK" 258 text kAb1 "AB1_DIGEST" 259 text kAb2 "AB2_BIJECT" 260 text kAb3 "AB3_SMUGGLE" 261 text kAb4 "AB4_ANCHOR" 262 text kAb5 "AB5_DEP" 263 text kAb6 "AB6_HAZARD" 264 text kAb7 "AB7_LENGTH" 265 text kAb8 "AB8_SIG" 266 text kAb9 "AB9_TRAIL" 267 text kAb10 "AB10_QMD" 268 text kAb11 "AB11_GEN" 269 text kAb12 "AB12_OPCODE" 270 text kAb13a "AB13_PC0" 271 text kAb13b "AB13_BRATGT" 272 text kAb13c "AB13_BRAFALL" 273 text kAb14 "AB14_FLOOR6" 274 text kAb15 "AB15_I2F" 275 text kBsOk "BSSY_FLAT" 276 text kAb16 "AB16_BSSY" 277 text kAb17a "AB17_GPR62" 278 text kAb17b "AB17_GPR126" 279 text kGpr125 "GPR125_VALID" 280 text kEg0 "EGP2_K0" 281 text kEg1 "EGP2_K1" 282 text kEgN1 "EGP2_NEG_RULE99" 283 text kEgN2 "EGP2_NEG_SPOOF" 284 text kL3Self "LOCK3_SELFTEST" 285 text kL3R0 "LOCK3_RCPT_INIT" 286 text kL3R4 "LOCK3_RCPT_AFTER4" 287 text kL3Rs "LOCK3_RESET_OK" 288 text kL3RpOk "LOCK3_REPLAY_OK" 289 text kL3BufEq "LOCK3_BUF_EXACT" 290 text kL3ActEq "LOCK3_ACT_EXACT" 291 text kL3ParEq "LOCK3_PAR_EXACT" 292 text kL3FlpEq "LOCK3_FLIP_COUNT" 293 text kL3Stp4Rc "LOCK3_STEP4_RC" 294 text kL3Stp4Flp "LOCK3_STEP4_FLIPS" 295 text kL3TmpRc "LOCK3_TAMPER_RC" 296 text kL3TmpCd "LOCK3_TAMPER_LASTCODE" 297 text kL3TmpAct "LOCK3_TAMPER_NOFLIP" 298 text kL3Ord1 "LOCK3_ORDER_SKIP" 299 text kL3Ord2 "LOCK3_ORDER_BACK" 300 text kL3Ord3 "LOCK3_ORDER_CURSOR" 301 text kL3R0S "LOCK3_R0_STEP" 302 text kL3R0G "LOCK3_R0_GEN" 303 text kL3R1S "LOCK3_R1_STEP" 304 text kL3R1G "LOCK3_R1_GEN" 305 text kL3R2S "LOCK3_R2_STEP" 306 text kL3R2G "LOCK3_R2_GEN" 307 text kL3R3S "LOCK3_R3_STEP" 308 text kL3R3G "LOCK3_R3_GEN" 309 text kL3Sha3 "LOCK3_RCPT3_SHA " 310 text kL3Words "LOCK3_FINAL_WORDS " 311 312 fn wrt s view uchar * to void 313 r = write 1 s 0 len s 314 end 315 316 fn wrtNum v ulong to void 317 b = new uchar 24 0 318 p = ulong 24 319 x = v 320 while true bound 24 321 p = sub p 1 322 put b p uchar add 48 rem x 10 323 x = div x 10 324 if eq x 0 325 break 326 end 327 end 328 r = write 1 b p sub 24 p 329 end 330 331 fn prLine tag view uchar * v ulong to void 332 wrt tag 333 wrt tSp 334 wrtNum v 335 wrt tNl 336 end 337 338 fn printSha to void 339 hx = new uchar 64 0 340 for i ulong 32 341 b = uint get ejShaDig i 342 put hx mul i 2 get hexTbl shr b 4 343 put hx add mul i 2 1 get hexTbl and b 15 344 end 345 wrt tSha 346 r = write 1 hx 0 64 347 wrt tNl 348 end 349 350 fn setStall pc ulong nst ulong to void 351 idx = add mul pc 4 3 352 w3 = get ejCand idx 353 nw3 = or and w3 not shl ulong 15 9 shl and nst 15 9 354 put ejCand idx nw3 355 end 356 357 fn b2u b bool to ulong 358 if b 359 ret 1 360 end 361 ret 0 362 end 363 364 fn checkYld pc ulong to ulong 365 ret and shr get ejCand add mul pc 4 3 13 1 366 end 367 368 fn runPositives to void 369 loadK1 370 ok = ejLoadKernelRef 15 64 371 prLine kLoadK1 b2u ok 372 prLine kInitAct get ejState 0 373 374 note Step t=0: G6 Ascetic on A -> B 375 rc0 = ejit_step_tick 6 376 prLine kG6Rc rc0 377 prLine kG6Act get ejState 0 378 prLine kG6Swp get ejState 10 379 prLine kG6Yld get ejState 11 380 prLine kG6Flp get ejState 4 381 prLine kG6YI checkYld 5 382 prLine kG6YF checkYld 6 383 prLine kG6YM checkYld 7 384 prLine kG6SI ejCndStall 5 385 prLine kG6SN ejCndStall 2 386 387 note Step t=1: G1 Predator on B -> A 388 rc1 = ejit_step_tick 1 389 prLine kG1Rc rc1 390 prLine kG1Act get ejState 0 391 prLine kG1Swp get ejState 10 392 prLine kG1Flp get ejState 4 393 prLine kG1SR get ejState 12 394 prLine kG1SC get ejState 13 395 396 note Step t=2: G2 Mitosis Hybrid on A -> B 397 rc2 = ejit_step_tick 2 398 prLine kG2Rc rc2 399 prLine kG2Act get ejState 0 400 prLine kG2Yld get ejState 11 401 prLine kG2Flp get ejState 4 402 403 note Step t=3: G5 Trickster on B -> A 404 rc3 = ejit_step_tick 5 405 prLine kG5Rc rc3 406 prLine kG5Act get ejState 0 407 prLine kG5Flp get ejState 4 408 409 note Fail-closed hot-swap test: corrupt BufB, verify parity check fails 410 put ejBufB add mul ulong 6 4 0 29216 411 parBad = ejit_parity_check 2 15 412 prLine kParBad b2u parBad 413 end 414 415 fn runNegatives1 to void 416 loadK1 417 ok2 = ejLoadKernelRef 15 64 418 419 note Antibody #1: Digest spoof 420 ejCopyRefToCand 15 421 ejHashWords true 15 422 for i ulong 32 423 put ejCertShaCnd i get ejCndSha i 424 end 425 put ejCertShaCnd 0 xor get ejCertShaCnd 0 1 426 prLine kAb1 ejVerifySigned 15 240 240 64 427 428 note Antibody #2: Non-bijection (duplicate perm[3]=perm[4]) 429 ejCopyRefToCand 15 430 put ejPerm 3 get ejPerm 4 431 prLine kAb2 ejTestVerifyCand 15 64 432 433 note Antibody #3: Opcode smuggle (flip functional bit in w0 at pc=3) 434 ejCopyRefToCand 15 435 put ejCand mul ulong 3 4 xor get ejCand mul ulong 3 4 1 436 prLine kAb3 ejTestVerifyCand 15 64 437 438 note Antibody #4: Non-leader anchor escape (swap ISETP pc=8 with pc=7) 439 ejCopyRefToCand 15 440 ejSwapCand 7 8 441 prLine kAb4 ejTestVerifyCand 15 64 442 443 note Antibody #5: RAW/WAR/WAW dependency inversion (swap pc=3 IADD3 R11<-R10 with pc=5 I2F R12<-R11) 444 ejCopyRefToCand 15 445 ejSwapCand 3 5 446 prLine kAb5 ejTestVerifyCand 15 64 447 448 note Antibody #6: Scoreboard wait-barrier strip (clear wait mask on pc=2 NOP waiting on sb0) 449 ejCopyRefToCand 15 450 w3nop = get ejCand add mul ulong 2 4 3 451 put ejCand add mul ulong 2 4 3 and w3nop not shl ulong 63 20 452 prLine kAb6 ejTestVerifyCand 15 64 453 454 note Antibody #7: Length mismatch 455 ejCopyRefToCand 15 456 ejHashWords true 15 457 for i ulong 32 458 put ejCertShaCnd i get ejCndSha i 459 end 460 prLine kAb7 ejVerifySigned 15 240 224 64 461 462 note Antibodies #8-#11: uor-gpu Launch Gate 463 prLine kAb8 ejCheckLaunchGate 0 512 512 4096 384 1 464 prLine kAb9 ejCheckLaunchGate 1 512 513 4096 384 1 465 prLine kAb10 ejCheckLaunchGate 1 512 512 4100 384 1 466 prLine kAb11 ejCheckLaunchGate 1 512 512 4096 384 0 467 end 468 469 fn runNegatives2 to void 470 loadK1 471 ok3 = ejLoadKernelRef 15 64 472 ejCopyRefToCand 15 473 w3bad = get ejCand add mul ulong 3 4 3 474 w3bad = or and w3bad not shl ulong 7 14 shl ulong 6 14 475 put ejCand add mul ulong 3 4 3 w3bad 476 prLine kAb12 ejTestVerifyCand 15 64 477 478 note Antibody #13a: BB leader pc=0 escape (swap pc=0 and pc=3) 479 loadK1 480 ok4 = ejLoadKernelRef 15 64 481 ejCopyRefToCand 15 482 ejSwapCand 0 3 483 prLine kAb13a ejTestVerifyCand 15 64 484 485 note Antibody #13b: BB leader BRA target pc=12 escape (swap pc=12 and pc=13) 486 ejCopyRefToCand 15 487 ejSwapCand 12 13 488 prLine kAb13b ejTestVerifyCand 15 64 489 490 note Antibody #13c: BB leader BRA fall-through pc=10 escape (swap pc=10 and pc=11) 491 ejCopyRefToCand 15 492 ejSwapCand 10 11 493 prLine kAb13c ejTestVerifyCand 15 64 494 495 note Antibody #14: Fixed-latency ALU RAW floor < 6 (shave pc=0 IADD3 st=6 -> 5; even with flr=4 passed!) 496 loadKAlu6 497 oka6 = ejLoadKernelRef 3 64 498 ejCopyRefToCand 3 499 setStall 0 5 500 ejHashWords true 3 501 for i ulong 32 502 put ejCertShaCnd i get ejCndSha i 503 end 504 prLine kAb14 ejMonkVerify 3 4 64 505 506 note Antibody #15: I2F (op 581, nm=29) stall shave 4 -> 1 even when I2F+EXIT sum=6 507 loadKI2f 508 oki = ejLoadKernelRef 2 64 509 ejCopyRefToCand 2 510 setStall 0 1 511 prLine kAb15 ejTestVerifyCand 2 64 512 513 note Antibody #16: Nested BSSY vs flat BSSY 514 loadKBssyOk 515 okbo = ejLoadKernelRef 4 64 516 prLine kBsOk ejTestVerifyCand 4 64 517 loadKBssyBad 518 okbb = ejLoadKernelRef 4 64 519 prLine kAb16 ejTestVerifyCand 4 64 520 521 note Antibody #17: GPR ceiling (R62 in 64-reg, R126 in 128-reg, R125 valid in 128-reg) 522 loadKGpr62 523 okg1 = ejLoadKernelRef 2 64 524 prLine kAb17a ejTestVerifyCand 2 64 525 loadKGpr126 526 okg2 = ejLoadKernelRef 2 128 527 prLine kAb17b ejTestVerifyCand 2 128 528 loadKGpr125 529 okg3 = ejLoadKernelRef 2 128 530 prLine kGpr125 ejTestVerifyCand 2 128 531 532 note EGP2 Structural Proof + Negative Controls 533 prLine kEg0 ejEgVerifyKernel 0 534 prLine kEg1 ejEgVerifyKernel 1 535 ejEgReset 536 n0 = ejEgAdd 2 0 0 0 0 537 n1 = ejEgAdd 1 0 0 0 1 538 n2 = ejEgAdd 3 n0 n1 0 0 539 n3 = ejEgAdd 5 n0 n1 0 0 540 prLine kEgN1 b2u ejEgVerifyStep 99 n2 n3 541 prLine kEgN2 b2u ejEgVerifyStep 3 n2 n0 542 end 543 544 fn printRcptSha idx ulong to void 545 hx = new uchar 64 0 546 for i ulong 32 547 b = uint ejit_rcpt_sha_byte idx i 548 put hx mul i 2 get hexTbl shr b 4 549 put hx add mul i 2 1 get hexTbl and b 15 550 end 551 wrt kL3Sha3 552 r = write 1 hx 0 64 553 wrt tNl 554 end 555 556 fn printBufWords act ulong cntWords ulong to void 557 hx = new uchar 8 0 558 wrt kL3Words 559 for i ulong cntWords bound 64 560 w = ejSimWord add act 1 i 561 for k ulong 8 562 sh = mul sub 7 k 4 563 put hx k get hexTbl and shr w sh 15 564 end 565 r = write 1 hx 0 8 566 end 567 wrt tNl 568 end 569 570 fn runLock3Tests to void 571 wantBuf = new ulong 60 0 572 st = ejit_selftest 573 prLine kL3Self ulong st 574 575 loadK1 576 okL3 = ejLoadKernelRef 15 64 577 prLine kL3R0 ejit_rcpt_count 578 rcT = ejit_step_tick 6 579 rcT = ejit_step_tick 1 580 rcT = ejit_step_tick 2 581 rcT = ejit_step_tick 5 582 prLine kL3R4 ejit_rcpt_count 583 prLine kL3R0S ejit_rcpt_step 0 584 prLine kL3R0G ejit_rcpt_gen 0 585 prLine kL3R1S ejit_rcpt_step 1 586 prLine kL3R1G ejit_rcpt_gen 1 587 prLine kL3R2S ejit_rcpt_step 2 588 prLine kL3R2G ejit_rcpt_gen 2 589 prLine kL3R3S ejit_rcpt_step 3 590 prLine kL3R3G ejit_rcpt_gen 3 591 printRcptSha 3 592 593 wantAct = get ejState 0 594 wantPar = get ejState 14 595 for i ulong 60 596 put wantBuf i ejSimWord add wantAct 1 i 597 end 598 printBufWords wantAct 60 599 600 ejResetForReplay 601 rstOk = ulong 1 602 if ne get ejState 0 0 603 rstOk = ulong 0 604 end 605 if ne get ejState 3 0 606 rstOk = ulong 0 607 end 608 if ne get ejState 4 0 609 rstOk = ulong 0 610 end 611 if ne ejit_rcpt_count 4 612 rstOk = ulong 0 613 end 614 prLine kL3Rs rstOk 615 616 rplyOk = ulong 1 617 for s ulong 4 618 if ne ejit_replay_step s s 0 619 rplyOk = ulong 0 620 end 621 end 622 prLine kL3RpOk rplyOk 623 prLine kL3ActEq b2u eq get ejState 0 wantAct 624 prLine kL3ParEq b2u eq get ejState 14 wantPar 625 prLine kL3FlpEq get ejState 4 626 627 bufEq = ulong 1 628 for i ulong 60 629 if ne ejSimWord add wantAct 1 i get wantBuf i 630 bufEq = ulong 0 631 end 632 end 633 prLine kL3BufEq bufEq 634 635 rcT = ejit_replay_step 4 4 636 prLine kL3Stp4Rc rcT 637 prLine kL3Stp4Flp get ejState 4 638 end 639 640 fn runLock3Negatives to void 641 note Negative control 1: 1-bit SHA tamper on receipt 2 -> EJ_ERR_REPLAY_DIGEST (20) 642 ejResetForReplay 643 rcN = ejit_replay_step 0 0 644 rcN = ejit_replay_step 1 1 645 actBefore = get ejState 0 646 ejit_tamper_rcpt_sha 2 15 uchar 4 647 rcTmp = ejit_replay_step 2 2 648 ejit_tamper_rcpt_sha 2 15 uchar 4 649 prLine kL3TmpRc rcTmp 650 prLine kL3TmpCd get ejState 6 651 prLine kL3TmpAct b2u eq get ejState 0 actBefore 652 653 note Negative control 2: out-of-order replay -> EJ_ERR_REPLAY_ORDER (21) 654 ejResetForReplay 655 prLine kL3Ord1 ejit_replay_step 1 0 656 ejResetForReplay 657 rcN = ejit_replay_step 0 0 658 prLine kL3Ord2 ejit_replay_step 0 1 659 ejResetForReplay 660 prLine kL3Ord3 ejit_replay_step 0 1 661 end 662 663 fn main to int 664 note 1. Self-contained SHA-256 check on "abc" 665 put ejShaDat 0 97 666 put ejShaDat 1 98 667 put ejShaDat 2 99 668 ejSha256 3 669 printSha 670 671 runPositives 672 runNegatives1 673 runNegatives2 674 runLock3Tests 675 runLock3Negatives 676 ret 0 677 end 678 """) 679 return "\n".join(driver), want_sha 680 681 682 def 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 842 if __name__ == "__main__": 843 main()