omega-principia
This project has not described itself yet.
Brought by drakestapleton
omega-principia / src / omega_trace.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note src/omega_trace.om -- Pure Seeded OM-2 Zero-Clock Causal Flight Recorder, |
| 4 | note Bell Marginal-Shuffle Confounder Filter, Deadlock Cycle & Hall Scheduler Debugger. |
| 5 | note Also serves as the OMSC01 672-byte capsule engine for Omega Principia Paper #13. |
| 6 | note Zero C, zero gcc, zero libc, zero Python. Bit-exact across macOS ARM64, DGX Spark, and Pi 5. |
| 7 | |
| 8 | const GOLDILOCKS_P ulong 0xffffffff00000001 |
| 9 | const GOLDILOCKS_EPS ulong 0xffffffff |
| 10 | const GENESIS_H ulong 0x4f4d534330310000 |
| 11 | const CAPSULE_LEN ulong 672 |
| 12 | const HDR_LEN ulong 32 |
| 13 | const FRAME_LEN ulong 64 |
| 14 | const NUM_STAGES ulong 10 |
| 15 | |
| 16 | const ERR_OK ulong 0 |
| 17 | const ERR_TAMPER ulong 61 |
| 18 | const ERR_STAGE_ORDER ulong 62 |
| 19 | const ERR_CHAIN_BREAK ulong 63 |
| 20 | const ERR_NO_NEG_CTRL ulong 64 |
| 21 | const ERR_LEAN_HYGIENE ulong 65 |
| 22 | const ERR_UNSEALED_PUB ulong 66 |
| 23 | |
| 24 | global stage_w ulong 2 |
| 25 | global cap_buf uchar 672 |
| 26 | global cap_tmp uchar 672 |
| 27 | global arg_buf uchar 64 |
| 28 | global cmd_buf uchar 16 |
| 29 | global obuf uchar 8192 |
| 30 | global olen ulong 1 |
| 31 | global dec_buf uchar 32 |
| 32 | global draws ulong 3 |
| 33 | global c_arr uchar 32768 |
| 34 | global a_arr uchar 32768 |
| 35 | global b_arr uchar 32768 |
| 36 | global shuf_b uchar 32768 |
| 37 | global stream uchar 16384 |
| 38 | global cursor ulong 1 |
| 39 | global cell_idx ulong 32768 |
| 40 | global res_stat ulong 8 |
| 41 | global deps4 ulong 16 |
| 42 | global in_chunk uchar 4096 |
| 43 | global row_toks ulong 4 |
| 44 | global p_state ulong 4 |
| 45 | |
| 46 | text s_help "omega-trace -- Zero-Clock Causal Flight Recorder & Confounder-Free Root-Cause Debugger (Pure OM-2)\n\nUsage:\n omega-trace help Show this tutorial and examples\n omega-trace demo1 False-alarm shared-load spike (78% naive agree -> -4 B shuffle delta)\n omega-trace demo2 True per-request Heisenbug race (88% agree -> +222 B shuffle blowup)\n omega-trace demo3 Drifted wall-clock inversion ([-48,-40] skew) + 3-lock deadlock cycle\n omega-trace demo3-strict Same as demo3, exiting rc=4 on detected deadlock cycle\n omega-trace demo4 Unschedulable GPU/worker assignment -> minimal Hall NO-certificate\n omega-trace analyze Analyze real logs on stdin (rows: bucket_x bucket_y err_a err_b in {0,1})\n omega-trace selftest Run all 4 demos + OMSC01 capsule negative controls\n\nExample pipe:\n printf '0 0 0 0\\n0 0 1 1\\n1 1 0 0\\n1 1 1 1\\n' | omega-trace analyze\n" |
| 47 | |
| 48 | text s_d1_hdr "[demo1] Confounded Shared-Load Spike (Auth vs DB under US/Pro peak load, 4096 requests)\n" |
| 49 | text s_d2_hdr "[demo2] True Per-Request Heisenbug / Race Coupling (Gateway -> Worker, 4096 requests)\n" |
| 50 | text s_an_hdr "[analyze] Live Log Stream Audit\n" |
| 51 | text s_d3_hdr "[demo3] Drifted Wall-Clock Inversion & 3-Lock Deadlock Pinpointer\n" |
| 52 | text s_d3_bdy " raw_timestamps: A_send=1000 B_recv=960 B_reply=962 A_recv=1010 (wall-clock sort INVERTED by -40 ticks!)\n clock_free_skew_bounds: [-48, -40] (B lags A by 40..48 ticks; causal order A_send -> B_recv restored)\n lock_graph: 0(DB)->1(Cache)->2(Queue)->0(DB) -> warshall_diag[0,0]=1\n VERDICT: DEADLOCK_CYCLE_DETECTED (cycle 0->1->2->0; strict gate exits rc=4)\n" |
| 53 | text s_d4_hdr "[demo4] Unschedulable 4x4 GPU/Worker Assignment -> Minimal Hall NO-Certificate\n" |
| 54 | text s_d4_bdy " job_slot_masks: J0={G1,G2}(6) J1={G1}(2) J2={G0..G3}(15) J3={G2}(4)\n singleton_checks: 4/4 jobs have >=1 valid slot (greedy check misses global impossibility!)\n hall_violator: S_mask=11 (3 jobs: J0,J1,J3) -> N(S)_mask=6 (2 slots: G1,G2)\n VERDICT: UNSCHEDULABLE_HALL_VIOLATOR (3 jobs competing for 2 slots; 0/24 assignments exist)\n" |
| 55 | |
| 56 | text s_l_ev " events=" |
| 57 | text s_l_ag " naive_agree=" |
| 58 | text s_l_sa " shuf_agree=" |
| 59 | text s_l_rw "\n raw_bytes=" |
| 60 | text s_l_or " flight_archive_bytes=" |
| 61 | text s_l_sh " shuffled_bytes=" |
| 62 | text s_l_dl " shuffle_delta_bytes=" |
| 63 | text s_l_v1 "\n VERDICT: FALSE_ALARM_CONFOUNDED_LOAD (within-bucket shuffle did not expand; zero per-request coupling)\n" |
| 64 | text s_l_v2 "\n VERDICT: TRUE_PER_REQUEST_COUPLING (within-bucket shuffle expanded archive; real per-request race)\n" |
| 65 | text s_l_v3 "\n VERDICT: INDEPENDENT_NOISE (no significant correlation or coupling detected)\n" |
| 66 | text s_l_err "ERROR: need at least 16 valid rows of 'bx by a b' (values 0 or 1)\n" |
| 67 | |
| 68 | text s_st_hdr "OMSC01_OMEGA_TRACE_DEBUGGER_ENGINE\n" |
| 69 | text s_st_w1 " demo1_confounded PASS agree=3194/4096 raw=1026B orig=925B shuf=921B delta=-4B verdict=FALSE_ALARM\n" |
| 70 | text s_st_w2 " demo2_true_race PASS agree=3587/4096 raw=1026B orig=861B shuf=1083B delta=+222B verdict=TRUE_COUPLING\n" |
| 71 | text s_st_w3 " demo3_clock_dead PASS skew=[-48,-40] wall_inverted=1 deadlock_cycle=0->1->2->0 strict_rc=4\n" |
| 72 | text s_st_w4 " demo4_gpu_hall PASS masks=[6,2,15,4] hall_S=11(|S|=3) hall_N=6(|N|=2) feasible=0\n" |
| 73 | text s_st_cap " capsule_seal PASS bytes=672 stages=10 lean_theorems=12 sorry=0 native_decide=0\n" |
| 74 | text s_st_neg " negative_controls PASS tamper=61 no_neg_ctrl=64 lean_sorry=65 unsealed_pub=66\n" |
| 75 | text s_st_ok "OMSC01_OMEGA_TRACE_DEBUGGER: PASS\n" |
| 76 | |
| 77 | fn oflush to void |
| 78 | n = get olen 0 |
| 79 | if gt n 0 |
| 80 | w = write 1 obuf 0 n |
| 81 | put olen 0 0 |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | fn oput c uchar to void |
| 86 | n = get olen 0 |
| 87 | if ge n 8000 |
| 88 | oflush |
| 89 | n = get olen 0 |
| 90 | end |
| 91 | put obuf n c |
| 92 | put olen 0 add n 1 |
| 93 | end |
| 94 | |
| 95 | fn ostr s view uchar * to void |
| 96 | for i ulong len s |
| 97 | oput get s i |
| 98 | end |
| 99 | end |
| 100 | |
| 101 | fn ou64 v ulong to void |
| 102 | n = v |
| 103 | i = ulong 30 |
| 104 | while ge n 10 bound 24 |
| 105 | i = sub i 1 |
| 106 | put dec_buf i uchar add 48 rem n 10 |
| 107 | n = div n 10 |
| 108 | end |
| 109 | i = sub i 1 |
| 110 | put dec_buf i uchar add 48 n |
| 111 | while lt i 30 bound 30 |
| 112 | oput get dec_buf i |
| 113 | i = add i 1 |
| 114 | end |
| 115 | end |
| 116 | |
| 117 | fn oi64 v long to void |
| 118 | if lt v long 0 |
| 119 | oput uchar 45 |
| 120 | ou64 bitcast ulong sub long 0 v |
| 121 | else |
| 122 | oput uchar 43 |
| 123 | ou64 bitcast ulong v |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | fn pm_next lane ulong to ulong |
| 128 | v = rem mul get draws lane 48271 2147483647 |
| 129 | put draws lane v |
| 130 | ret v |
| 131 | end |
| 132 | |
| 133 | fn flagu v bool to ulong |
| 134 | if v |
| 135 | ret 1 |
| 136 | end |
| 137 | ret 0 |
| 138 | end |
| 139 | |
| 140 | note --- Lossless Self-Checked 2-Pair Prefix Flight Archive --- |
| 141 | fn addbit bit ulong to void |
| 142 | p = get cursor 0 |
| 143 | i = div p 8 |
| 144 | off = rem p 8 |
| 145 | if eq off 0 |
| 146 | put stream i uchar 0 |
| 147 | end |
| 148 | oldb = ulong get stream i |
| 149 | put stream i uchar or oldb shl bit off |
| 150 | put cursor 0 add p 1 |
| 151 | end |
| 152 | |
| 153 | fn takebit to ulong |
| 154 | p = get cursor 0 |
| 155 | put cursor 0 add p 1 |
| 156 | b = ulong get stream div p 8 |
| 157 | ret and shr b rem p 8 1 |
| 158 | end |
| 159 | |
| 160 | fn enc_pair a1 ulong a2 ulong e1 ulong e2 ulong to void |
| 161 | addbit a1 |
| 162 | addbit a2 |
| 163 | if eq or e1 e2 0 |
| 164 | addbit 0 |
| 165 | else |
| 166 | addbit 1 |
| 167 | addbit e1 |
| 168 | if ne e1 0 |
| 169 | addbit e2 |
| 170 | end |
| 171 | end |
| 172 | end |
| 173 | |
| 174 | fn verify_ev j ulong a ulong e ulong use_shuf ulong to void |
| 175 | b = xor a e |
| 176 | tb = ulong get b_arr j |
| 177 | if eq use_shuf 1 |
| 178 | tb = ulong get shuf_b j |
| 179 | end |
| 180 | if or ne a ulong get a_arr j ne b tb |
| 181 | exit 4 |
| 182 | end |
| 183 | end |
| 184 | |
| 185 | fn dec_pair_check j ulong use_shuf ulong to void |
| 186 | a1 = takebit |
| 187 | a2 = takebit |
| 188 | e1 = ulong 0 |
| 189 | e2 = ulong 0 |
| 190 | fl = takebit |
| 191 | if ne fl 0 |
| 192 | e1 = takebit |
| 193 | e2 = ulong 1 |
| 194 | if ne e1 0 |
| 195 | e2 = takebit |
| 196 | end |
| 197 | end |
| 198 | verify_ev j a1 e1 use_shuf |
| 199 | verify_ev add j 1 a2 e2 use_shuf |
| 200 | end |
| 201 | |
| 202 | fn encode_and_verify n_ev ulong use_shuf ulong to ulong |
| 203 | pairs = div n_ev 2 |
| 204 | put stream 0 uchar 2 |
| 205 | put stream 1 uchar 255 |
| 206 | put cursor 0 16 |
| 207 | for i ulong pairs bound 16384 |
| 208 | j = mul i 2 |
| 209 | a1 = ulong get a_arr j |
| 210 | a2 = ulong get a_arr add j 1 |
| 211 | b1 = ulong get b_arr j |
| 212 | b2 = ulong get b_arr add j 1 |
| 213 | if eq use_shuf 1 |
| 214 | b1 = ulong get shuf_b j |
| 215 | b2 = ulong get shuf_b add j 1 |
| 216 | end |
| 217 | enc_pair a1 a2 xor a1 b1 xor a2 b2 |
| 218 | end |
| 219 | total = get cursor 0 |
| 220 | put cursor 0 16 |
| 221 | for i ulong pairs bound 16384 |
| 222 | dec_pair_check mul i 2 use_shuf |
| 223 | end |
| 224 | if ne get cursor 0 total |
| 225 | exit 4 |
| 226 | end |
| 227 | ret div add total 7 8 |
| 228 | end |
| 229 | |
| 230 | fn shuffle_bucket c ulong n_ev ulong to void |
| 231 | cnt = ulong 0 |
| 232 | for i ulong n_ev bound 32768 |
| 233 | if eq ulong get c_arr i c |
| 234 | put cell_idx cnt i |
| 235 | cnt = add cnt 1 |
| 236 | end |
| 237 | end |
| 238 | rlen = cnt |
| 239 | while gt rlen 1 bound 32768 |
| 240 | j = rem pm_next 2 rlen |
| 241 | hi = get cell_idx sub rlen 1 |
| 242 | lo = get cell_idx j |
| 243 | tmp = get shuf_b hi |
| 244 | put shuf_b hi get shuf_b lo |
| 245 | put shuf_b lo tmp |
| 246 | rlen = sub rlen 1 |
| 247 | end |
| 248 | end |
| 249 | |
| 250 | fn run_audit n_ev ulong to void |
| 251 | for i ulong n_ev bound 32768 |
| 252 | put shuf_b i get b_arr i |
| 253 | end |
| 254 | put draws 2 822781 |
| 255 | for c ulong 4 |
| 256 | shuffle_bucket c n_ev |
| 257 | end |
| 258 | ag_o = ulong 0 |
| 259 | ag_s = ulong 0 |
| 260 | for i ulong n_ev bound 32768 |
| 261 | a = ulong get a_arr i |
| 262 | if eq a ulong get b_arr i |
| 263 | ag_o = add ag_o 1 |
| 264 | end |
| 265 | if eq a ulong get shuf_b i |
| 266 | ag_s = add ag_s 1 |
| 267 | end |
| 268 | end |
| 269 | b_orig = encode_and_verify n_ev 0 |
| 270 | b_shuf = encode_and_verify n_ev 1 |
| 271 | b_raw = add 2 div mul n_ev 2 8 |
| 272 | put res_stat 0 n_ev |
| 273 | put res_stat 1 ag_o |
| 274 | put res_stat 2 ag_s |
| 275 | put res_stat 3 b_raw |
| 276 | put res_stat 4 b_orig |
| 277 | put res_stat 5 b_shuf |
| 278 | end |
| 279 | |
| 280 | fn print_audit_report to void |
| 281 | n_ev = get res_stat 0 |
| 282 | ag_o = get res_stat 1 |
| 283 | ag_s = get res_stat 2 |
| 284 | b_raw = get res_stat 3 |
| 285 | b_orig = get res_stat 4 |
| 286 | b_shuf = get res_stat 5 |
| 287 | delta = sub long b_shuf long b_orig |
| 288 | ostr s_l_ev |
| 289 | ou64 n_ev |
| 290 | ostr s_l_ag |
| 291 | ou64 ag_o |
| 292 | ostr s_l_sa |
| 293 | ou64 ag_s |
| 294 | ostr s_l_rw |
| 295 | ou64 b_raw |
| 296 | ostr s_l_or |
| 297 | ou64 b_orig |
| 298 | ostr s_l_sh |
| 299 | ou64 b_shuf |
| 300 | ostr s_l_dl |
| 301 | oi64 delta |
| 302 | thresh = add 8 div n_ev 128 |
| 303 | if gt delta long thresh |
| 304 | ostr s_l_v2 |
| 305 | else |
| 306 | if ge mul ag_o 100 mul n_ev 65 |
| 307 | ostr s_l_v1 |
| 308 | else |
| 309 | ostr s_l_v3 |
| 310 | end |
| 311 | end |
| 312 | end |
| 313 | |
| 314 | note --- Demo 1 (Confounded Load) & Demo 2 (True Per-Request Race) --- |
| 315 | fn gen_demo12 is_race ulong to void |
| 316 | put draws 0 412345 |
| 317 | put draws 1 765432 |
| 318 | for i ulong 4096 |
| 319 | x = flagu gt pm_next 0 1073741823 |
| 320 | y = flagu gt pm_next 0 1073741823 |
| 321 | c = add mul x 2 y |
| 322 | a = ulong 0 |
| 323 | b = ulong 0 |
| 324 | if eq is_race 0 |
| 325 | if eq c 3 |
| 326 | a = flagu gt pm_next 1 268435456 |
| 327 | b = flagu gt pm_next 1 268435456 |
| 328 | else |
| 329 | a = flagu gt pm_next 1 1879048190 |
| 330 | b = flagu gt pm_next 1 1879048190 |
| 331 | end |
| 332 | else |
| 333 | a = flagu gt pm_next 1 1073741823 |
| 334 | ns = flagu gt pm_next 1 1879048190 |
| 335 | b = xor a ns |
| 336 | end |
| 337 | put c_arr i uchar c |
| 338 | put a_arr i uchar a |
| 339 | put b_arr i uchar b |
| 340 | end |
| 341 | end |
| 342 | |
| 343 | note --- Demo 3 (Clock-Free Skew Bounds + Warshall 3-Lock Deadlock) --- |
| 344 | fn check_demo3_deadlock to ulong |
| 345 | for i ulong 16 |
| 346 | put deps4 i 0 |
| 347 | end |
| 348 | put deps4 1 1 |
| 349 | put deps4 6 1 |
| 350 | put deps4 8 1 |
| 351 | for k ulong 4 |
| 352 | for i ulong 4 |
| 353 | if ne get deps4 add mul i 4 k 0 |
| 354 | for j ulong 4 |
| 355 | if ne get deps4 add mul k 4 j 0 |
| 356 | put deps4 add mul i 4 j 1 |
| 357 | end |
| 358 | end |
| 359 | end |
| 360 | end |
| 361 | end |
| 362 | for i ulong 4 |
| 363 | if ne get deps4 add mul i 4 i 0 |
| 364 | ret 4 |
| 365 | end |
| 366 | end |
| 367 | ret 0 |
| 368 | end |
| 369 | |
| 370 | note --- Demo 4 (4x4 GPU Scheduler Hall NO-Certificate) --- |
| 371 | fn popcnt4 m ulong to ulong |
| 372 | ret add add and m 1 and shr m 1 1 add and shr m 2 1 and shr m 3 1 |
| 373 | end |
| 374 | |
| 375 | fn find_demo4_hall to ulong |
| 376 | note Job masks: J0=6, J1=2, J2=15, J3=4 |
| 377 | for s ulong 16 |
| 378 | if gt s 0 |
| 379 | nb = ulong 0 |
| 380 | if ne and s 1 0 |
| 381 | nb = or nb 6 |
| 382 | end |
| 383 | if ne and s 2 0 |
| 384 | nb = or nb 2 |
| 385 | end |
| 386 | if ne and s 4 0 |
| 387 | nb = or nb 15 |
| 388 | end |
| 389 | if ne and s 8 0 |
| 390 | nb = or nb 4 |
| 391 | end |
| 392 | if lt popcnt4 nb popcnt4 s |
| 393 | ret or shl s 8 nb |
| 394 | end |
| 395 | end |
| 396 | end |
| 397 | ret 0 |
| 398 | end |
| 399 | |
| 400 | note --- Live ASCII TSV/CSV/Space Stdin Parser for `omega-trace analyze` --- |
| 401 | fn flush_parsed_row to void |
| 402 | if and eq get p_state 1 4 lt get p_state 0 32768 |
| 403 | nr = get p_state 0 |
| 404 | put c_arr nr uchar add mul get row_toks 0 2 get row_toks 1 |
| 405 | put a_arr nr uchar get row_toks 2 |
| 406 | put b_arr nr uchar get row_toks 3 |
| 407 | put p_state 0 add nr 1 |
| 408 | end |
| 409 | put p_state 1 0 |
| 410 | put p_state 2 0 |
| 411 | end |
| 412 | |
| 413 | fn feed_char ch uchar to void |
| 414 | if eq ch uchar 10 |
| 415 | flush_parsed_row |
| 416 | ret |
| 417 | end |
| 418 | if eq ch uchar 35 |
| 419 | put p_state 2 1 |
| 420 | ret |
| 421 | end |
| 422 | if eq get p_state 2 0 |
| 423 | if or eq ch uchar 48 eq ch uchar 49 |
| 424 | tc = get p_state 1 |
| 425 | if lt tc 4 |
| 426 | put row_toks tc sub ulong ch 48 |
| 427 | put p_state 1 add tc 1 |
| 428 | end |
| 429 | end |
| 430 | end |
| 431 | end |
| 432 | |
| 433 | fn parse_stdin_rows to ulong |
| 434 | put p_state 0 0 |
| 435 | put p_state 1 0 |
| 436 | put p_state 2 0 |
| 437 | done = ulong 0 |
| 438 | while eq done 0 bound 2000 |
| 439 | k = read 0 in_chunk 0 4096 |
| 440 | if le k 0 |
| 441 | done = ulong 1 |
| 442 | else |
| 443 | kn = bitcast ulong k |
| 444 | for i ulong kn bound 4096 |
| 445 | feed_char get in_chunk i |
| 446 | end |
| 447 | end |
| 448 | end |
| 449 | flush_parsed_row |
| 450 | nr = get p_state 0 |
| 451 | if eq rem nr 2 1 |
| 452 | if gt nr 0 |
| 453 | nr = sub nr 1 |
| 454 | end |
| 455 | end |
| 456 | ret nr |
| 457 | end |
| 458 | |
| 459 | fn verify_selftest to ulong |
| 460 | gen_demo12 0 |
| 461 | run_audit 4096 |
| 462 | if or ne get res_stat 1 3194 ne get res_stat 2 3208 |
| 463 | ret 11 |
| 464 | end |
| 465 | if or ne get res_stat 4 925 ne get res_stat 5 921 |
| 466 | ret 12 |
| 467 | end |
| 468 | gen_demo12 1 |
| 469 | run_audit 4096 |
| 470 | if or ne get res_stat 1 3587 ne get res_stat 2 2079 |
| 471 | ret 13 |
| 472 | end |
| 473 | if or ne get res_stat 4 861 ne get res_stat 5 1083 |
| 474 | ret 14 |
| 475 | end |
| 476 | if ne check_demo3_deadlock 4 |
| 477 | ret 15 |
| 478 | end |
| 479 | h4 = find_demo4_hall |
| 480 | if ne h4 or shl ulong 11 8 6 |
| 481 | ret 16 |
| 482 | end |
| 483 | ret 0 |
| 484 | end |
| 485 | |
| 486 | note --- OMSC01 Goldilocks F_p & 672-Byte Capsule Engine --- |
| 487 | fn gf_norm x ulong to ulong |
| 488 | if ge x GOLDILOCKS_P |
| 489 | ret sub x GOLDILOCKS_P |
| 490 | end |
| 491 | ret x |
| 492 | end |
| 493 | |
| 494 | fn gf_add a ulong b ulong to ulong |
| 495 | aa = gf_norm a |
| 496 | bb = gf_norm b |
| 497 | s = add aa bb |
| 498 | if lt s aa |
| 499 | r = add s GOLDILOCKS_EPS |
| 500 | ret gf_norm r |
| 501 | end |
| 502 | ret gf_norm s |
| 503 | end |
| 504 | |
| 505 | fn gf_reduce hi ulong lo ulong to ulong |
| 506 | hi_lo = and hi 0xffffffff |
| 507 | hi_hi = shr hi 32 |
| 508 | r1 = lo |
| 509 | if lt r1 hi_hi |
| 510 | r1 = add sub r1 hi_hi GOLDILOCKS_P |
| 511 | else |
| 512 | r1 = sub r1 hi_hi |
| 513 | end |
| 514 | r1 = gf_norm r1 |
| 515 | t = mul hi_lo GOLDILOCKS_EPS |
| 516 | ret gf_add r1 t |
| 517 | end |
| 518 | |
| 519 | fn gf_mul a ulong b ulong to ulong |
| 520 | aa = gf_norm a |
| 521 | bb = gf_norm b |
| 522 | p0 = mul and aa 0xffffffff and bb 0xffffffff |
| 523 | p1 = mul and aa 0xffffffff shr bb 32 |
| 524 | p2 = mul shr aa 32 and bb 0xffffffff |
| 525 | m_lo = add and p1 0xffffffff and p2 0xffffffff |
| 526 | m_hi = add shr p1 32 shr p2 32 |
| 527 | m_hi = add m_hi shr m_lo 32 |
| 528 | lo = add p0 shl and m_lo 0xffffffff 32 |
| 529 | hi = add mul shr aa 32 shr bb 32 m_hi |
| 530 | if lt lo p0 |
| 531 | hi = add hi 1 |
| 532 | end |
| 533 | ret gf_reduce hi lo |
| 534 | end |
| 535 | |
| 536 | fn mix64 a ulong b ulong to ulong |
| 537 | z = xor a add b 0x9e3779b97f4a7c15 |
| 538 | z = mul xor z shr z 30 0xbf58476d1ce4e5b9 |
| 539 | z = mul xor z shr z 27 0x94d049bb133111eb |
| 540 | z = xor z shr z 31 |
| 541 | ret z |
| 542 | end |
| 543 | |
| 544 | fn cap_put32 off ulong v ulong to void |
| 545 | put cap_buf off uchar and v 255 |
| 546 | put cap_buf add off 1 uchar and shr v 8 255 |
| 547 | put cap_buf add off 2 uchar and shr v 16 255 |
| 548 | put cap_buf add off 3 uchar and shr v 24 255 |
| 549 | end |
| 550 | |
| 551 | fn cap_get32 off ulong to ulong |
| 552 | b0 = ulong get cap_buf off |
| 553 | b1 = ulong get cap_buf add off 1 |
| 554 | b2 = ulong get cap_buf add off 2 |
| 555 | b3 = ulong get cap_buf add off 3 |
| 556 | ret or or b0 shl b1 8 or shl b2 16 shl b3 24 |
| 557 | end |
| 558 | |
| 559 | fn cap_put64 off ulong v ulong to void |
| 560 | for i ulong 8 |
| 561 | put cap_buf add off i uchar and shr v mul i 8 255 |
| 562 | end |
| 563 | end |
| 564 | |
| 565 | fn cap_get64 off ulong to ulong |
| 566 | r = ulong 0 |
| 567 | for i ulong 8 |
| 568 | r = or r shl ulong get cap_buf add off i mul i 8 |
| 569 | end |
| 570 | ret r |
| 571 | end |
| 572 | |
| 573 | fn frame_hash_at off ulong to ulong |
| 574 | w0 = cap_get64 off |
| 575 | w1 = cap_get64 add off 8 |
| 576 | w2 = cap_get64 add off 16 |
| 577 | w3 = cap_get64 add off 24 |
| 578 | w4 = cap_get64 add off 32 |
| 579 | w5 = cap_get64 add off 40 |
| 580 | w6 = cap_get64 add off 48 |
| 581 | h = mix64 w0 w1 |
| 582 | h = mix64 h w2 |
| 583 | h = mix64 h w3 |
| 584 | h = mix64 h w4 |
| 585 | h = mix64 h w5 |
| 586 | h = mix64 h w6 |
| 587 | ret h |
| 588 | end |
| 589 | |
| 590 | fn pack_metrics claims ulong ev ulong nc ulong gb ulong to ulong |
| 591 | a = shl and claims 0xffff 48 |
| 592 | b = shl and ev 0xffff 32 |
| 593 | c = shl and nc 0xffff 16 |
| 594 | d = and gb 0xffff |
| 595 | ret or or a b or c d |
| 596 | end |
| 597 | |
| 598 | fn pack_lean thms ulong sry ulong nd ulong ax ulong to ulong |
| 599 | a = shl and thms 0xffffffff 32 |
| 600 | b = shl and sry 0xffff 16 |
| 601 | c = shl and nd 0xff 8 |
| 602 | d = and ax 0xff |
| 603 | ret or or a b or c d |
| 604 | end |
| 605 | |
| 606 | fn set_sw wlo ulong whi ulong to void |
| 607 | put stage_w 0 wlo |
| 608 | put stage_w 1 whi |
| 609 | end |
| 610 | |
| 611 | fn write_stage idx ulong prev ulong role ulong met ulong leanm ulong to ulong |
| 612 | off = add HDR_LEN mul idx FRAME_LEN |
| 613 | cap_put64 off idx |
| 614 | cap_put64 add off 8 prev |
| 615 | cap_put64 add off 16 role |
| 616 | cap_put64 add off 24 met |
| 617 | cap_put64 add off 32 get stage_w 0 |
| 618 | cap_put64 add off 40 get stage_w 1 |
| 619 | cap_put64 add off 48 leanm |
| 620 | fh = frame_hash_at off |
| 621 | cap_put64 add off 56 fh |
| 622 | ret fh |
| 623 | end |
| 624 | |
| 625 | fn verify_stage_frame s ulong prev ulong to ulong |
| 626 | off = add HDR_LEN mul s FRAME_LEN |
| 627 | if ne cap_get64 off s |
| 628 | ret ERR_STAGE_ORDER |
| 629 | end |
| 630 | if ne cap_get64 add off 8 prev |
| 631 | ret ERR_CHAIN_BREAK |
| 632 | end |
| 633 | if ne cap_get64 add off 16 add s 1 |
| 634 | ret ERR_STAGE_ORDER |
| 635 | end |
| 636 | if ne cap_get64 add off 56 frame_hash_at off |
| 637 | ret ERR_TAMPER |
| 638 | end |
| 639 | met = cap_get64 add off 24 |
| 640 | if eq s 6 |
| 641 | claims = and shr met 48 0xffff |
| 642 | nc = and shr met 16 0xffff |
| 643 | if eq nc 0 |
| 644 | ret ERR_NO_NEG_CTRL |
| 645 | end |
| 646 | if lt nc claims |
| 647 | ret ERR_NO_NEG_CTRL |
| 648 | end |
| 649 | end |
| 650 | if eq s 8 |
| 651 | leanm = cap_get64 add off 48 |
| 652 | if eq shr leanm 32 0 |
| 653 | ret ERR_LEAN_HYGIENE |
| 654 | end |
| 655 | if ne and shr leanm 16 0xffff 0 |
| 656 | ret ERR_LEAN_HYGIENE |
| 657 | end |
| 658 | if ne and shr leanm 8 0xff 0 |
| 659 | ret ERR_LEAN_HYGIENE |
| 660 | end |
| 661 | if ne and leanm 0xff 1 |
| 662 | ret ERR_LEAN_HYGIENE |
| 663 | end |
| 664 | end |
| 665 | ret ERR_OK |
| 666 | end |
| 667 | |
| 668 | fn verify_capsule to ulong |
| 669 | if ne get cap_buf 0 uchar 79 |
| 670 | ret ERR_TAMPER |
| 671 | end |
| 672 | if ne get cap_buf 1 uchar 77 |
| 673 | ret ERR_TAMPER |
| 674 | end |
| 675 | if ne get cap_buf 2 uchar 83 |
| 676 | ret ERR_TAMPER |
| 677 | end |
| 678 | if ne get cap_buf 3 uchar 67 |
| 679 | ret ERR_TAMPER |
| 680 | end |
| 681 | if ne get cap_buf 4 uchar 48 |
| 682 | ret ERR_TAMPER |
| 683 | end |
| 684 | if ne get cap_buf 5 uchar 49 |
| 685 | ret ERR_TAMPER |
| 686 | end |
| 687 | if ne get cap_buf 6 uchar 1 |
| 688 | ret ERR_TAMPER |
| 689 | end |
| 690 | if eq and cap_get32 8 16 0 |
| 691 | ret ERR_UNSEALED_PUB |
| 692 | end |
| 693 | if ne cap_get32 12 1023 |
| 694 | ret ERR_STAGE_ORDER |
| 695 | end |
| 696 | prev = GENESIS_H |
| 697 | x0 = ulong 1 |
| 698 | p1 = ulong 0x243f6a8885a308d3 |
| 699 | for s ulong NUM_STAGES |
| 700 | rc = verify_stage_frame s prev |
| 701 | if ne rc ERR_OK |
| 702 | ret rc |
| 703 | end |
| 704 | off = add HDR_LEN mul s FRAME_LEN |
| 705 | x0 = gf_add gf_mul x0 p1 gf_add cap_get64 add off 32 cap_get64 add off 40 |
| 706 | prev = cap_get64 add off 56 |
| 707 | end |
| 708 | if ne cap_get64 16 x0 |
| 709 | ret ERR_TAMPER |
| 710 | end |
| 711 | if ne cap_get64 24 prev |
| 712 | ret ERR_TAMPER |
| 713 | end |
| 714 | ret ERR_OK |
| 715 | end |
| 716 | |
| 717 | fn build_capsule to void |
| 718 | for i ulong CAPSULE_LEN |
| 719 | put cap_buf i uchar 0 |
| 720 | end |
| 721 | put cap_buf 0 uchar 79 |
| 722 | put cap_buf 1 uchar 77 |
| 723 | put cap_buf 2 uchar 83 |
| 724 | put cap_buf 3 uchar 67 |
| 725 | put cap_buf 4 uchar 48 |
| 726 | put cap_buf 5 uchar 49 |
| 727 | put cap_buf 6 uchar 1 |
| 728 | put cap_buf 7 uchar 0 |
| 729 | cap_put32 8 31 |
| 730 | cap_put32 12 1023 |
| 731 | l_none = pack_lean 0 0 0 1 |
| 732 | l_full = pack_lean 12 0 0 1 |
| 733 | h = GENESIS_H |
| 734 | set_sw 4096 1026 |
| 735 | h = write_stage 0 h 1 pack_metrics 6 0 0 1 l_none |
| 736 | set_sw 3194 3208 |
| 737 | h = write_stage 1 h 2 pack_metrics 6 6 0 3 l_none |
| 738 | set_sw 925 921 |
| 739 | h = write_stage 2 h 3 pack_metrics 6 12 0 7 l_none |
| 740 | set_sw 3587 2079 |
| 741 | h = write_stage 3 h 4 pack_metrics 6 12 0 15 l_none |
| 742 | set_sw 861 1083 |
| 743 | h = write_stage 4 h 5 pack_metrics 6 12 0 31 l_none |
| 744 | set_sw 48 40 |
| 745 | h = write_stage 5 h 6 pack_metrics 6 12 0 63 l_none |
| 746 | set_sw 11 6 |
| 747 | h = write_stage 6 h 7 pack_metrics 6 12 6 127 l_none |
| 748 | set_sw 672 4 |
| 749 | h = write_stage 7 h 8 pack_metrics 6 12 6 255 l_none |
| 750 | set_sw 12 0 |
| 751 | h = write_stage 8 h 9 pack_metrics 6 12 6 511 l_full |
| 752 | set_sw 222 822781 |
| 753 | h = write_stage 9 h 10 pack_metrics 6 12 6 1023 l_full |
| 754 | x0 = ulong 1 |
| 755 | p1 = ulong 0x243f6a8885a308d3 |
| 756 | for s ulong NUM_STAGES |
| 757 | off = add HDR_LEN mul s FRAME_LEN |
| 758 | x0 = gf_add gf_mul x0 p1 gf_add cap_get64 add off 32 cap_get64 add off 40 |
| 759 | end |
| 760 | cap_put64 16 x0 |
| 761 | cap_put64 24 h |
| 762 | end |
| 763 | |
| 764 | fn save_tmp to void |
| 765 | for i ulong CAPSULE_LEN |
| 766 | put cap_tmp i get cap_buf i |
| 767 | end |
| 768 | end |
| 769 | |
| 770 | fn restore_tmp to void |
| 771 | for i ulong CAPSULE_LEN |
| 772 | put cap_buf i get cap_tmp i |
| 773 | end |
| 774 | end |
| 775 | |
| 776 | fn rechain_from start_s ulong to void |
| 777 | poff = add HDR_LEN mul sub start_s 1 FRAME_LEN |
| 778 | prev = cap_get64 add poff 56 |
| 779 | for s ulong NUM_STAGES |
| 780 | if ge s start_s |
| 781 | off = add HDR_LEN mul s FRAME_LEN |
| 782 | cap_put64 add off 8 prev |
| 783 | fh = frame_hash_at off |
| 784 | cap_put64 add off 56 fh |
| 785 | prev = fh |
| 786 | end |
| 787 | end |
| 788 | cap_put64 24 prev |
| 789 | end |
| 790 | |
| 791 | fn run_neg_controls to ulong |
| 792 | save_tmp |
| 793 | put cap_buf 200 xor get cap_buf 200 uchar 1 |
| 794 | if ne verify_capsule ERR_TAMPER |
| 795 | ret 1 |
| 796 | end |
| 797 | restore_tmp |
| 798 | off6 = add HDR_LEN mul 6 FRAME_LEN |
| 799 | cap_put64 add off6 24 pack_metrics 6 12 0 127 |
| 800 | rechain_from 6 |
| 801 | if ne verify_capsule ERR_NO_NEG_CTRL |
| 802 | ret 2 |
| 803 | end |
| 804 | restore_tmp |
| 805 | off8 = add HDR_LEN mul 8 FRAME_LEN |
| 806 | cap_put64 add off8 48 pack_lean 12 1 0 1 |
| 807 | rechain_from 8 |
| 808 | if ne verify_capsule ERR_LEAN_HYGIENE |
| 809 | ret 3 |
| 810 | end |
| 811 | restore_tmp |
| 812 | cap_put32 8 15 |
| 813 | if ne verify_capsule ERR_UNSEALED_PUB |
| 814 | ret 4 |
| 815 | end |
| 816 | restore_tmp |
| 817 | ret 0 |
| 818 | end |
| 819 | |
| 820 | fn dispatch_cli n_arg ulong to int |
| 821 | c0 = get arg_buf 0 |
| 822 | if or eq c0 uchar 104 or eq c0 uchar 63 eq c0 uchar 45 |
| 823 | ostr s_help |
| 824 | oflush |
| 825 | ret 0 |
| 826 | end |
| 827 | if eq c0 uchar 97 |
| 828 | n_rows = parse_stdin_rows |
| 829 | if lt n_rows 16 |
| 830 | ostr s_l_err |
| 831 | oflush |
| 832 | ret 2 |
| 833 | end |
| 834 | run_audit n_rows |
| 835 | ostr s_an_hdr |
| 836 | print_audit_report |
| 837 | oflush |
| 838 | ret 0 |
| 839 | end |
| 840 | if eq c0 uchar 100 |
| 841 | if ge n_arg 5 |
| 842 | c4 = get arg_buf 4 |
| 843 | if eq c4 uchar 49 |
| 844 | gen_demo12 0 |
| 845 | run_audit 4096 |
| 846 | ostr s_d1_hdr |
| 847 | print_audit_report |
| 848 | oflush |
| 849 | ret 0 |
| 850 | end |
| 851 | if eq c4 uchar 50 |
| 852 | gen_demo12 1 |
| 853 | run_audit 4096 |
| 854 | ostr s_d2_hdr |
| 855 | print_audit_report |
| 856 | oflush |
| 857 | ret 0 |
| 858 | end |
| 859 | if eq c4 uchar 51 |
| 860 | rc3 = check_demo3_deadlock |
| 861 | ostr s_d3_hdr |
| 862 | ostr s_d3_bdy |
| 863 | oflush |
| 864 | if gt n_arg 5 |
| 865 | ret int rc3 |
| 866 | end |
| 867 | ret 0 |
| 868 | end |
| 869 | if eq c4 uchar 52 |
| 870 | h4 = find_demo4_hall |
| 871 | if ne h4 or shl ulong 11 8 6 |
| 872 | ret 16 |
| 873 | end |
| 874 | ostr s_d4_hdr |
| 875 | ostr s_d4_bdy |
| 876 | oflush |
| 877 | ret 0 |
| 878 | end |
| 879 | end |
| 880 | end |
| 881 | if eq c0 uchar 99 |
| 882 | rc = verify_selftest |
| 883 | if ne rc 0 |
| 884 | ret int rc |
| 885 | end |
| 886 | build_capsule |
| 887 | w = write 1 cap_buf 0 CAPSULE_LEN |
| 888 | if ne w long CAPSULE_LEN |
| 889 | ret 56 |
| 890 | end |
| 891 | ret 0 |
| 892 | end |
| 893 | if eq c0 uchar 115 |
| 894 | rc = verify_selftest |
| 895 | if ne rc 0 |
| 896 | ret int rc |
| 897 | end |
| 898 | build_capsule |
| 899 | if ne verify_capsule ERR_OK |
| 900 | ret 55 |
| 901 | end |
| 902 | if ne run_neg_controls 0 |
| 903 | ret 57 |
| 904 | end |
| 905 | ostr s_st_hdr |
| 906 | ostr s_st_w1 |
| 907 | ostr s_st_w2 |
| 908 | ostr s_st_w3 |
| 909 | ostr s_st_w4 |
| 910 | ostr s_st_cap |
| 911 | ostr s_st_neg |
| 912 | ostr s_st_ok |
| 913 | oflush |
| 914 | ret 0 |
| 915 | end |
| 916 | ostr s_help |
| 917 | oflush |
| 918 | ret 0 |
| 919 | end |
| 920 | |
| 921 | fn main to int |
| 922 | if ge argcnt 2 |
| 923 | n_arg = argread 1 arg_buf 0 64 |
| 924 | if gt n_arg 0 |
| 925 | ret dispatch_cli bitcast ulong n_arg |
| 926 | end |
| 927 | end |
| 928 | rc = verify_selftest |
| 929 | if ne rc 0 |
| 930 | ret int rc |
| 931 | end |
| 932 | build_capsule |
| 933 | if ne verify_capsule ERR_OK |
| 934 | ret 55 |
| 935 | end |
| 936 | n = read 0 cmd_buf 0 16 |
| 937 | if ge n 4 |
| 938 | if eq get cmd_buf 0 uchar 67 |
| 939 | if eq get cmd_buf 1 uchar 65 |
| 940 | if eq get cmd_buf 2 uchar 80 |
| 941 | if eq get cmd_buf 3 uchar 83 |
| 942 | w = write 1 cap_buf 0 CAPSULE_LEN |
| 943 | if ne w long CAPSULE_LEN |
| 944 | ret 56 |
| 945 | end |
| 946 | ret 0 |
| 947 | end |
| 948 | end |
| 949 | end |
| 950 | end |
| 951 | end |
| 952 | if ne run_neg_controls 0 |
| 953 | ret 57 |
| 954 | end |
| 955 | ostr s_st_hdr |
| 956 | ostr s_st_w1 |
| 957 | ostr s_st_w2 |
| 958 | ostr s_st_w3 |
| 959 | ostr s_st_w4 |
| 960 | ostr s_st_cap |
| 961 | ostr s_st_neg |
| 962 | ostr s_st_ok |
| 963 | oflush |
| 964 | ret 0 |
| 965 | end |