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
omega-mv / aienux / atlas / main.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/atlas/main.om: Atlas Stage-0 Boot Verification CLI. |
| 3 | note Reads either a raw binary Atlas boot image (64-byte header + stage payload) or |
| 4 | note an ATLAS_MANIFEST text manifest on stdin, verifies alignment, bounds, epoch |
| 5 | note anti-rollback, memory-map disjointness, and constant-time SHA-256 digest. |
| 6 | note Emits canonical ATLAS checkpoints and verified handoff descriptor, or halts |
| 7 | note fail-closed with nonzero exit status on any violation. |
| 8 | note Compose with: si/directives/om2.om aienux/atlas/lib.om aienux/atlas/main.om |
| 9 | |
| 10 | text m_awaken "ATLAS: AWAKEN\n" |
| 11 | text m_verify "ATLAS: VERIFY\n" |
| 12 | text m_handoff "ATLAS: HANDOFF\n" |
| 13 | text m_refuse "ATLAS: REFUSE\n" |
| 14 | text m_halt "ATLAS: HALT code=" |
| 15 | text m_reason " reason=" |
| 16 | text m_nl "\n" |
| 17 | |
| 18 | text m_r_short "short_header" |
| 19 | text m_r_magic "bad_magic" |
| 20 | text m_r_ver "bad_version" |
| 21 | text m_r_slen "invalid_stage_len" |
| 22 | text m_r_align "bad_alignment" |
| 23 | text m_r_entry "entry_out_of_bounds" |
| 24 | text m_r_trunc "truncated_payload" |
| 25 | text m_r_roll "epoch_rollback" |
| 26 | text m_r_mmap "memmap_violation" |
| 27 | text m_r_hash "digest_mismatch" |
| 28 | text m_r_parse "malformed_manifest" |
| 29 | text m_r_io "stdin_read_error" |
| 30 | |
| 31 | text m_k_desc "desc_addr=" |
| 32 | text m_k_ram " ram_base=" |
| 33 | text m_k_rsz " ram_size=" |
| 34 | text m_k_uart "\nuart_base=" |
| 35 | text m_k_pbase " payload_base=" |
| 36 | text m_k_psize " payload_size=" |
| 37 | text m_k_entry "\nentry_addr=" |
| 38 | text m_k_epoch " epoch=" |
| 39 | text m_k_cookie " cookie=" |
| 40 | text m_k_sha "\nsha256=" |
| 41 | |
| 42 | text m_hdr_man "ATLAS_MANIFEST" |
| 43 | text m_arg_me "--min-epoch" |
| 44 | |
| 45 | text m_p_ver "version=" |
| 46 | text m_p_ep "epoch=" |
| 47 | text m_p_mep "min_epoch=" |
| 48 | text m_p_eoff "entry_off=" |
| 49 | text m_p_sha "sha256=" |
| 50 | text m_p_pay "payload_hex=" |
| 51 | |
| 52 | fn atlas_eq_slice a view uchar * b view uchar * to bool |
| 53 | if ne len a len b |
| 54 | ret false |
| 55 | end |
| 56 | i = ulong 0 |
| 57 | n = len a |
| 58 | while lt i n bound 65536 |
| 59 | if ne get a i get b i |
| 60 | ret false |
| 61 | end |
| 62 | i = add i 1 |
| 63 | end |
| 64 | ret true |
| 65 | end |
| 66 | |
| 67 | fn atlas_starts_with a view uchar * pfx view uchar * to bool |
| 68 | if lt len a len pfx |
| 69 | ret false |
| 70 | end |
| 71 | ret atlas_eq_slice part a 0 len pfx pfx |
| 72 | end |
| 73 | |
| 74 | fn atlas_parse_u64 s view uchar * dst edit ulong 1 to bool |
| 75 | n = len s |
| 76 | if eq n 0 |
| 77 | ret false |
| 78 | end |
| 79 | if gt n 20 |
| 80 | ret false |
| 81 | end |
| 82 | acc = ulong 0 |
| 83 | i = ulong 0 |
| 84 | while lt i n bound 24 |
| 85 | c = get s i |
| 86 | if lt c 48 |
| 87 | ret false |
| 88 | end |
| 89 | if gt c 57 |
| 90 | ret false |
| 91 | end |
| 92 | d = ulong sub c 48 |
| 93 | if gt acc 1844674407370955161 |
| 94 | ret false |
| 95 | end |
| 96 | m = mul acc 10 |
| 97 | if atlas_add_overflow m d |
| 98 | ret false |
| 99 | end |
| 100 | acc = add m d |
| 101 | i = add i 1 |
| 102 | end |
| 103 | put dst 0 acc |
| 104 | ret true |
| 105 | end |
| 106 | |
| 107 | fn atlas_print_reason fd int rc uint to void |
| 108 | ok = false |
| 109 | if eq rc ATLAS_ERR_SHORT_HDR |
| 110 | ok = atlas_write_all fd m_r_short |
| 111 | else |
| 112 | if eq rc ATLAS_ERR_MAGIC |
| 113 | ok = atlas_write_all fd m_r_magic |
| 114 | else |
| 115 | if eq rc ATLAS_ERR_VERSION |
| 116 | ok = atlas_write_all fd m_r_ver |
| 117 | else |
| 118 | if eq rc ATLAS_ERR_STAGE_LEN |
| 119 | ok = atlas_write_all fd m_r_slen |
| 120 | else |
| 121 | if eq rc ATLAS_ERR_ALIGN |
| 122 | ok = atlas_write_all fd m_r_align |
| 123 | else |
| 124 | if eq rc ATLAS_ERR_ENTRY_OOB |
| 125 | ok = atlas_write_all fd m_r_entry |
| 126 | else |
| 127 | if eq rc ATLAS_ERR_TRUNCATED |
| 128 | ok = atlas_write_all fd m_r_trunc |
| 129 | else |
| 130 | if eq rc ATLAS_ERR_ROLLBACK |
| 131 | ok = atlas_write_all fd m_r_roll |
| 132 | else |
| 133 | if eq rc ATLAS_ERR_MEMMAP |
| 134 | ok = atlas_write_all fd m_r_mmap |
| 135 | else |
| 136 | if eq rc ATLAS_ERR_DIGEST |
| 137 | ok = atlas_write_all fd m_r_hash |
| 138 | else |
| 139 | ok = atlas_write_all fd m_r_parse |
| 140 | end |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | end |
| 145 | end |
| 146 | end |
| 147 | end |
| 148 | end |
| 149 | end |
| 150 | if not ok |
| 151 | exit 2 |
| 152 | end |
| 153 | end |
| 154 | |
| 155 | fn atlas_print_halt rc uint to void |
| 156 | ok = atlas_write_all 1 m_refuse |
| 157 | if not ok |
| 158 | exit 2 |
| 159 | end |
| 160 | ok = atlas_write_all 1 m_halt |
| 161 | if not ok |
| 162 | exit 2 |
| 163 | end |
| 164 | ok = atlas_write_u64 1 ulong rc |
| 165 | if not ok |
| 166 | exit 2 |
| 167 | end |
| 168 | ok = atlas_write_all 1 m_reason |
| 169 | if not ok |
| 170 | exit 2 |
| 171 | end |
| 172 | atlas_print_reason 1 rc |
| 173 | ok = atlas_write_all 1 m_nl |
| 174 | if not ok |
| 175 | exit 2 |
| 176 | end |
| 177 | end |
| 178 | |
| 179 | fn atlas_print_handoff desc view AtlasBootDesc comp view uchar 32 to bool |
| 180 | if not atlas_write_all 1 m_handoff |
| 181 | ret false |
| 182 | end |
| 183 | if not atlas_write_all 1 m_k_desc |
| 184 | ret false |
| 185 | end |
| 186 | if not atlas_write_hex64 1 get desc desc_addr |
| 187 | ret false |
| 188 | end |
| 189 | if not atlas_write_all 1 m_k_ram |
| 190 | ret false |
| 191 | end |
| 192 | if not atlas_write_hex64 1 get desc ram_base |
| 193 | ret false |
| 194 | end |
| 195 | if not atlas_write_all 1 m_k_rsz |
| 196 | ret false |
| 197 | end |
| 198 | if not atlas_write_hex64 1 get desc ram_size |
| 199 | ret false |
| 200 | end |
| 201 | if not atlas_write_all 1 m_k_uart |
| 202 | ret false |
| 203 | end |
| 204 | if not atlas_write_hex64 1 get desc uart_base |
| 205 | ret false |
| 206 | end |
| 207 | if not atlas_write_all 1 m_k_pbase |
| 208 | ret false |
| 209 | end |
| 210 | if not atlas_write_hex64 1 get desc payload_base |
| 211 | ret false |
| 212 | end |
| 213 | if not atlas_write_all 1 m_k_psize |
| 214 | ret false |
| 215 | end |
| 216 | if not atlas_write_u64 1 get desc payload_size |
| 217 | ret false |
| 218 | end |
| 219 | if not atlas_write_all 1 m_k_entry |
| 220 | ret false |
| 221 | end |
| 222 | if not atlas_write_hex64 1 get desc entry_addr |
| 223 | ret false |
| 224 | end |
| 225 | if not atlas_write_all 1 m_k_epoch |
| 226 | ret false |
| 227 | end |
| 228 | if not atlas_write_u64 1 get desc epoch |
| 229 | ret false |
| 230 | end |
| 231 | if not atlas_write_all 1 m_k_cookie |
| 232 | ret false |
| 233 | end |
| 234 | if not atlas_write_hex64 1 get desc cookie |
| 235 | ret false |
| 236 | end |
| 237 | if not atlas_write_all 1 m_k_sha |
| 238 | ret false |
| 239 | end |
| 240 | if not atlas_write_digest 1 comp |
| 241 | ret false |
| 242 | end |
| 243 | ret atlas_write_all 1 m_nl |
| 244 | end |
| 245 | |
| 246 | fn atlas_read_stdin dst edit uchar * got edit ulong 1 to bool |
| 247 | cap = len dst |
| 248 | tot = ulong 0 |
| 249 | while lt tot cap bound 65536 |
| 250 | k = read 0 dst tot sub cap tot |
| 251 | if lt k 0 |
| 252 | ret false |
| 253 | end |
| 254 | if eq k 0 |
| 255 | break |
| 256 | end |
| 257 | tot = add tot ulong k |
| 258 | end |
| 259 | put got 0 tot |
| 260 | ret true |
| 261 | end |
| 262 | |
| 263 | fn atlas_parse_line_meta line view uchar * hdr edit AtlasHeader exp edit uchar 32 meta edit ulong 4 to int |
| 264 | note meta[0]=total_len, meta[1]=min_epoch, meta[2]=has_sha, meta[3]=has_pay |
| 265 | tmp = new ulong 1 0 |
| 266 | llen = len line |
| 267 | if atlas_starts_with line m_p_ver |
| 268 | if not atlas_parse_u64 part line 8 sub llen 8 tmp |
| 269 | ret -1 |
| 270 | end |
| 271 | if gt get tmp 0 0xffffffff |
| 272 | ret -1 |
| 273 | end |
| 274 | put hdr ver uint get tmp 0 |
| 275 | ret 1 |
| 276 | end |
| 277 | if atlas_starts_with line m_p_ep |
| 278 | if not atlas_parse_u64 part line 6 sub llen 6 tmp |
| 279 | ret -1 |
| 280 | end |
| 281 | put hdr epoch get tmp 0 |
| 282 | ret 1 |
| 283 | end |
| 284 | if atlas_starts_with line m_p_mep |
| 285 | if not atlas_parse_u64 part line 10 sub llen 10 tmp |
| 286 | ret -1 |
| 287 | end |
| 288 | put meta 1 get tmp 0 |
| 289 | ret 1 |
| 290 | end |
| 291 | if atlas_starts_with line m_p_eoff |
| 292 | if not atlas_parse_u64 part line 10 sub llen 10 tmp |
| 293 | ret -1 |
| 294 | end |
| 295 | put hdr entry_off get tmp 0 |
| 296 | ret 1 |
| 297 | end |
| 298 | if atlas_starts_with line m_p_sha |
| 299 | if ne sub llen 7 64 |
| 300 | ret -1 |
| 301 | end |
| 302 | if not atlas_parse_hex_bytes part line 7 64 exp |
| 303 | ret -1 |
| 304 | end |
| 305 | put meta 2 1 |
| 306 | ret 1 |
| 307 | end |
| 308 | ret 0 |
| 309 | end |
| 310 | |
| 311 | fn atlas_parse_line_pay line view uchar * img edit uchar * hdr edit AtlasHeader meta edit ulong 4 to int |
| 312 | llen = len line |
| 313 | if not atlas_starts_with line m_p_pay |
| 314 | ret 0 |
| 315 | end |
| 316 | hlen = sub llen 12 |
| 317 | if ne and hlen 1 0 |
| 318 | ret -1 |
| 319 | end |
| 320 | plen = shr hlen 1 |
| 321 | if gt add ATLAS_HDR_SIZE plen len img |
| 322 | ret -1 |
| 323 | end |
| 324 | if not atlas_parse_hex_bytes part line 12 hlen part img ATLAS_HDR_SIZE plen |
| 325 | ret -1 |
| 326 | end |
| 327 | put hdr stage_len plen |
| 328 | put meta 0 add ATLAS_HDR_SIZE plen |
| 329 | put meta 3 1 |
| 330 | ret 1 |
| 331 | end |
| 332 | |
| 333 | fn atlas_find_nl src view uchar * start ulong to ulong |
| 334 | n = len src |
| 335 | p = start |
| 336 | while lt p n bound 65536 |
| 337 | if eq get src p 10 |
| 338 | ret p |
| 339 | end |
| 340 | p = add p 1 |
| 341 | end |
| 342 | ret n |
| 343 | end |
| 344 | |
| 345 | fn atlas_parse_manifest src view uchar * img edit uchar * meta edit ulong 4 to bool |
| 346 | exp = new uchar 32 0 |
| 347 | hdr = new AtlasHeader ATLAS_MAGIC_LE ATLAS_HDR_VER 0 0 0 |
| 348 | n = len src |
| 349 | pos = atlas_find_nl src 0 |
| 350 | if lt pos n |
| 351 | pos = add pos 1 |
| 352 | end |
| 353 | while lt pos n bound 1024 |
| 354 | lend = atlas_find_nl src pos |
| 355 | nextp = lend |
| 356 | if lt nextp n |
| 357 | nextp = add nextp 1 |
| 358 | end |
| 359 | if gt lend pos |
| 360 | if eq get src sub lend 1 13 |
| 361 | lend = sub lend 1 |
| 362 | end |
| 363 | end |
| 364 | llen = sub lend pos |
| 365 | if gt llen 0 |
| 366 | r1 = atlas_parse_line_meta part src pos llen hdr exp meta |
| 367 | if lt r1 0 |
| 368 | ret false |
| 369 | end |
| 370 | if eq r1 0 |
| 371 | r2 = atlas_parse_line_pay part src pos llen img hdr meta |
| 372 | if lt r2 0 |
| 373 | ret false |
| 374 | end |
| 375 | end |
| 376 | end |
| 377 | pos = nextp |
| 378 | end |
| 379 | if ne get meta 2 1 |
| 380 | ret false |
| 381 | end |
| 382 | if ne get meta 3 1 |
| 383 | ret false |
| 384 | end |
| 385 | ret atlas_header_encode img hdr exp |
| 386 | end |
| 387 | |
| 388 | fn atlas_read_min_epoch dst edit ulong 1 to bool |
| 389 | abuf = new uchar 64 0 |
| 390 | vbuf = new uchar 64 0 |
| 391 | put dst 0 0 |
| 392 | ac = argcnt |
| 393 | if le ac 1 |
| 394 | ret true |
| 395 | end |
| 396 | if ne ac 3 |
| 397 | ret false |
| 398 | end |
| 399 | k1 = argread 1 abuf 0 64 |
| 400 | if le k1 0 |
| 401 | ret false |
| 402 | end |
| 403 | if not atlas_eq_slice part abuf 0 ulong k1 m_arg_me |
| 404 | ret false |
| 405 | end |
| 406 | k2 = argread 2 vbuf 0 64 |
| 407 | if le k2 0 |
| 408 | ret false |
| 409 | end |
| 410 | ret atlas_parse_u64 part vbuf 0 ulong k2 dst |
| 411 | end |
| 412 | |
| 413 | fn atlas_exec_image img view uchar * min_ep ulong to int |
| 414 | hdr = new AtlasHeader 0 0 0 0 0 |
| 415 | desc = new AtlasBootDesc 0 0 0 0 0 0 0 0 0 0 0 1 |
| 416 | comp = new uchar 32 0 |
| 417 | wire = new uchar 64 0 |
| 418 | rc = atlas_verify_image img min_ep hdr desc comp |
| 419 | if ne rc ATLAS_OK |
| 420 | atlas_print_halt rc |
| 421 | ret int rc |
| 422 | end |
| 423 | if not atlas_emit_desc_bytes desc wire |
| 424 | atlas_print_halt ATLAS_ERR_MEMMAP |
| 425 | ret int ATLAS_ERR_MEMMAP |
| 426 | end |
| 427 | if not atlas_print_handoff desc comp |
| 428 | ret 2 |
| 429 | end |
| 430 | ret 0 |
| 431 | end |
| 432 | |
| 433 | fn atlas_exec_manifest raw view uchar * min_ep ulong to int |
| 434 | img = new uchar 65536 0 |
| 435 | meta = new ulong 4 0 |
| 436 | put meta 1 min_ep |
| 437 | if not atlas_parse_manifest raw img meta |
| 438 | atlas_print_halt 11 |
| 439 | ret 11 |
| 440 | end |
| 441 | ret atlas_exec_image part img 0 get meta 0 get meta 1 |
| 442 | end |
| 443 | |
| 444 | fn main to int |
| 445 | raw = new uchar 65536 0 |
| 446 | got = new ulong 1 0 |
| 447 | mep = new ulong 1 0 |
| 448 | if not atlas_write_all 1 m_awaken |
| 449 | ret 2 |
| 450 | end |
| 451 | if not atlas_read_min_epoch mep |
| 452 | atlas_print_halt 11 |
| 453 | ret 11 |
| 454 | end |
| 455 | if not atlas_read_stdin raw got |
| 456 | ok = atlas_write_all 2 m_r_io |
| 457 | if ok |
| 458 | ret 12 |
| 459 | end |
| 460 | ret 12 |
| 461 | end |
| 462 | if not atlas_write_all 1 m_verify |
| 463 | ret 2 |
| 464 | end |
| 465 | rlen = get got 0 |
| 466 | min_ep = get mep 0 |
| 467 | if atlas_starts_with part raw 0 rlen m_hdr_man |
| 468 | ret atlas_exec_manifest part raw 0 rlen min_ep |
| 469 | end |
| 470 | ret atlas_exec_image part raw 0 rlen min_ep |
| 471 | end |