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-mv/blob/main/aienux/store/torn_slot.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / store / torn_slot.om` 510 lines ยท 10815 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/store/torn_slot.om: Pure-Omega dual-slot torn-write-safe 4096-byte record. 3 note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux 4 note native/store/torn_slot.{h,c}. 5 note Compose with: si/directives/om2.om aienux/store/torn_slot.om om/tls/sha2.om 6 7 const TS_RECORD_BYTES ulong 4096 8 const TS_UNITS ulong 4 9 const TS_HEADER_BYTES ulong 88 10 const TS_MEDIA_UNITS ulong 8 11 const TS_MEDIA_BYTES ulong 32768 12 const TS_U64_MAX ulong 0xffffffffffffffff 13 14 note Result codes 15 const TS_OK int 0 16 const TS_EMPTY int 1 17 const TS_CORRUPT int -1 18 const TS_CONFLICT int -2 19 const TS_EIO int -3 20 const TS_EGEOMETRY int -4 21 const TS_ESTATE int -5 22 23 note Per-slot diagnosis states 24 const TS_SLOT_BLANK uint 0 25 const TS_SLOT_VALID uint 1 26 const TS_SLOT_INVALID uint 2 27 28 note Header field offsets 29 const TS_OFF_VERSION ulong 8 30 const TS_OFF_SLOT ulong 10 31 const TS_OFF_RESERVED ulong 11 32 const TS_OFF_RECBYTES ulong 12 33 const TS_OFF_SEQ ulong 16 34 const TS_OFF_RECDIGEST ulong 24 35 const TS_OFF_HDRDIGEST ulong 56 36 37 note Region metadata indices (global ts_reg ulong 12) 38 const TS_R_BLKSIZE ulong 0 39 const TS_R_BLKCNT ulong 1 40 const TS_R_BASE_LBA ulong 2 41 const TS_R_BPU ulong 3 42 const TS_R_KNOWN ulong 4 43 const TS_R_NEWEST_SLOT ulong 5 44 const TS_R_NEWEST_SEQ ulong 6 45 const TS_R_HAS_NEWEST ulong 7 46 const TS_R_FAULT_WRITE ulong 8 47 const TS_R_FAULT_FLUSH ulong 9 48 const TS_R_TEAR_AFTER ulong 10 49 const TS_R_WRITE_COUNT ulong 11 50 51 text ts_magic "AIENTWS1" 52 text ts_rec_dom "AIENOS-TORN-SLOT-RECORD-V1" 53 text ts_hdr_dom "AIENOS-TORN-SLOT-HEADER-V1" 54 55 global ts_reg ulong 12 56 global ts_media uchar 32768 57 global ts_scratch_cu uchar 4096 58 global ts_scratch_b0 uchar 4096 59 global ts_scratch_b1 uchar 4096 60 61 note ---- Little-endian helpers ---- 62 63 fn ts_put16 buf edit uchar * off ulong v ushort to void 64 put buf off uchar and v 0xff 65 put buf add off 1 uchar and shr v 8 0xff 66 end 67 68 fn ts_put32 buf edit uchar * off ulong v uint to void 69 put buf off uchar and v 0xff 70 put buf add off 1 uchar and shr v 8 0xff 71 put buf add off 2 uchar and shr v 16 0xff 72 put buf add off 3 uchar and shr v 24 0xff 73 end 74 75 fn ts_put64 buf edit uchar * off ulong v ulong to void 76 for i ulong 8 unroll 77 put buf add off i uchar and shr v mul i 8 0xff 78 end 79 end 80 81 fn ts_get16 buf view uchar * off ulong to ushort 82 b0 = ushort get buf off 83 b1 = ushort get buf add off 1 84 ret or b0 shl b1 8 85 end 86 87 fn ts_get32 buf view uchar * off ulong to uint 88 b0 = uint get buf off 89 b1 = uint get buf add off 1 90 b2 = uint get buf add off 2 91 b3 = uint get buf add off 3 92 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 93 end 94 95 fn ts_get64 buf view uchar * off ulong to ulong 96 v = ulong 0 97 for i ulong 8 unroll 98 b = ulong get buf add off i 99 v = or v shl b mul i 8 100 end 101 ret v 102 end 103 104 fn ts_all_zero buf view uchar * off ulong cnt ulong to bool 105 acc = uchar 0 106 i = ulong 0 107 while lt i cnt bound 4096 108 acc = or acc get buf add off i 109 i = add i 1 110 end 111 ret eq acc 0 112 end 113 114 fn ts_digest_eq a view uchar * aoff ulong b view uchar * boff ulong to bool 115 d = uchar 0 116 for i ulong 32 117 d = or d xor get a add aoff i get b add boff i 118 end 119 ret eq d 0 120 end 121 122 note ---- Domain-separated SHA-256 digests ---- 123 124 fn ts_record_digest slot uchar seq ulong rec view uchar 4096 dst edit uchar 32 to void 125 ctx = new ulong 40 0 126 z1 = new uchar 1 0 127 sb = new uchar 9 0 128 put sb 0 slot 129 ts_put64 sb 1 seq 130 sha256_init ctx 131 sha256_update ctx ts_rec_dom 132 sha256_update ctx z1 133 sha256_update ctx sb 134 sha256_update ctx rec 135 sha256_final ctx dst 136 end 137 138 fn ts_header_digest hdr view uchar 4096 dst edit uchar 32 to void 139 ctx = new ulong 40 0 140 z1 = new uchar 1 0 141 sha256_init ctx 142 sha256_update ctx ts_hdr_dom 143 sha256_update ctx z1 144 sha256_update ctx part hdr 0 56 145 sha256_final ctx dst 146 end 147 148 fn ts_encode_commit unit edit uchar 4096 slot uchar seq ulong rec view uchar 4096 to void 149 for i ulong 4096 150 put unit i 0 151 end 152 for i ulong 8 153 put unit i get ts_magic i 154 end 155 ts_put16 unit TS_OFF_VERSION 1 156 put unit TS_OFF_SLOT slot 157 put unit TS_OFF_RESERVED 0 158 ts_put32 unit TS_OFF_RECBYTES uint TS_RECORD_BYTES 159 ts_put64 unit TS_OFF_SEQ seq 160 rd = new uchar 32 0 161 ts_record_digest slot seq rec rd 162 for i ulong 32 163 put unit add TS_OFF_RECDIGEST i get rd i 164 end 165 hd = new uchar 32 0 166 ts_header_digest unit hd 167 for i ulong 32 168 put unit add TS_OFF_HDRDIGEST i get hd i 169 end 170 end 171 172 note ---- Bounded media & fault-injection layer ---- 173 174 fn ts_media_zero to void 175 for i ulong TS_MEDIA_BYTES 176 put ts_media i 0 177 end 178 for i ulong 12 179 put ts_reg i 0 180 end 181 end 182 183 fn ts_set_faults fail_write ulong fail_flush ulong tear_after ulong to void 184 put ts_reg TS_R_FAULT_WRITE fail_write 185 put ts_reg TS_R_FAULT_FLUSH fail_flush 186 put ts_reg TS_R_TEAR_AFTER tear_after 187 put ts_reg TS_R_WRITE_COUNT 0 188 end 189 190 fn ts_media_read_unit unit ulong dst edit uchar 4096 to int 191 bpu = get ts_reg TS_R_BPU 192 bsize = get ts_reg TS_R_BLKSIZE 193 lba = add get ts_reg TS_R_BASE_LBA mul unit bpu 194 byte_off = mul lba bsize 195 if gt add byte_off TS_RECORD_BYTES TS_MEDIA_BYTES 196 ret TS_EIO 197 end 198 for i ulong 4096 199 put dst i get ts_media add byte_off i 200 end 201 ret TS_OK 202 end 203 204 fn ts_media_write_unit unit ulong src view uchar 4096 to int 205 bpu = get ts_reg TS_R_BPU 206 bsize = get ts_reg TS_R_BLKSIZE 207 lba = add get ts_reg TS_R_BASE_LBA mul unit bpu 208 byte_off = mul lba bsize 209 if gt add byte_off TS_RECORD_BYTES TS_MEDIA_BYTES 210 ret TS_EIO 211 end 212 fw = get ts_reg TS_R_FAULT_WRITE 213 wcnt = add get ts_reg TS_R_WRITE_COUNT 1 214 put ts_reg TS_R_WRITE_COUNT wcnt 215 if and ne fw 0 eq wcnt fw 216 ret TS_EIO 217 end 218 tear = get ts_reg TS_R_TEAR_AFTER 219 if and ne tear 0 le tear TS_RECORD_BYTES 220 i = ulong 0 221 while lt i tear bound 4096 222 put ts_media add byte_off i get src i 223 i = add i 1 224 end 225 ret TS_EIO 226 end 227 for i ulong 4096 228 put ts_media add byte_off i get src i 229 end 230 ret TS_OK 231 end 232 233 fn ts_media_flush to int 234 if ne get ts_reg TS_R_FAULT_FLUSH 0 235 ret TS_EIO 236 end 237 ret TS_OK 238 end 239 240 fn ts_media_poke unit ulong off ulong val uchar to void 241 bpu = get ts_reg TS_R_BPU 242 bsize = get ts_reg TS_R_BLKSIZE 243 lba = add get ts_reg TS_R_BASE_LBA mul unit bpu 244 byte_off = add mul lba bsize off 245 if lt byte_off TS_MEDIA_BYTES 246 put ts_media byte_off val 247 end 248 end 249 250 note ---- Public torn_slot API ---- 251 252 fn ts_open blk_size ulong blk_cnt ulong base_lba ulong to int 253 put ts_reg TS_R_KNOWN 0 254 put ts_reg TS_R_HAS_NEWEST 0 255 put ts_reg TS_R_NEWEST_SLOT 0 256 put ts_reg TS_R_NEWEST_SEQ 0 257 put ts_reg TS_R_BPU 0 258 if and ne blk_size 512 ne blk_size 4096 259 ret TS_EGEOMETRY 260 end 261 bpu = div TS_RECORD_BYTES blk_size 262 need = mul TS_UNITS bpu 263 if or gt base_lba blk_cnt lt sub blk_cnt base_lba need 264 ret TS_EGEOMETRY 265 end 266 put ts_reg TS_R_BLKSIZE blk_size 267 put ts_reg TS_R_BLKCNT blk_cnt 268 put ts_reg TS_R_BASE_LBA base_lba 269 put ts_reg TS_R_BPU bpu 270 ret TS_OK 271 end 272 273 fn ts_check_slot slot ulong body edit uchar 4096 seq_out edit ulong 1 st_out edit uint 1 to int 274 cu = new uchar 4096 0 275 d = new uchar 32 0 276 e = ts_media_read_unit add mul 2 slot 1 cu 277 if ne e TS_OK 278 ret e 279 end 280 put st_out 0 TS_SLOT_INVALID 281 if ts_all_zero cu 0 TS_RECORD_BYTES 282 put st_out 0 TS_SLOT_BLANK 283 ret TS_OK 284 end 285 for i ulong 8 286 if ne get cu i get ts_magic i 287 ret TS_OK 288 end 289 end 290 if ne ts_get16 cu TS_OFF_VERSION 1 291 ret TS_OK 292 end 293 if or ne get cu TS_OFF_SLOT uchar slot ne get cu TS_OFF_RESERVED 0 294 ret TS_OK 295 end 296 if ne ts_get32 cu TS_OFF_RECBYTES uint TS_RECORD_BYTES 297 ret TS_OK 298 end 299 if not ts_all_zero cu TS_HEADER_BYTES sub TS_RECORD_BYTES TS_HEADER_BYTES 300 ret TS_OK 301 end 302 ts_header_digest cu d 303 if not ts_digest_eq d 0 cu TS_OFF_HDRDIGEST 304 ret TS_OK 305 end 306 s = ts_get64 cu TS_OFF_SEQ 307 if eq s 0 308 ret TS_OK 309 end 310 e = ts_media_read_unit mul 2 slot body 311 if ne e TS_OK 312 ret e 313 end 314 ts_record_digest uchar slot s body d 315 if not ts_digest_eq d 0 cu TS_OFF_RECDIGEST 316 ret TS_OK 317 end 318 put seq_out 0 s 319 put st_out 0 TS_SLOT_VALID 320 ret TS_OK 321 end 322 323 fn ts_unfinished_first_commit plaus edit uchar 1 to int 324 put plaus 0 0 325 cu = new uchar 4096 0 326 e = ts_media_read_unit 1 cu 327 if ne e TS_OK 328 ret e 329 end 330 t = new uchar 24 0 331 for i ulong 8 332 put t i get ts_magic i 333 end 334 ts_put16 t TS_OFF_VERSION 1 335 put t TS_OFF_SLOT 0 336 put t TS_OFF_RESERVED 0 337 ts_put32 t TS_OFF_RECBYTES uint TS_RECORD_BYTES 338 ts_put64 t TS_OFF_SEQ 1 339 for i ulong 24 340 b = get cu i 341 if and ne b 0 ne b get t i 342 ret TS_OK 343 end 344 end 345 if not ts_all_zero cu TS_HEADER_BYTES sub TS_RECORD_BYTES TS_HEADER_BYTES 346 ret TS_OK 347 end 348 put plaus 0 1 349 ret TS_OK 350 end 351 352 fn ts_check_lineage v0 uint v1 uint q0 ulong q1 ulong to int 353 if eq v0 TS_SLOT_VALID 354 if eq v1 TS_SLOT_VALID 355 if eq q0 q1 356 ret TS_CONFLICT 357 end 358 end 359 if ne and q0 1 1 360 ret TS_CORRUPT 361 end 362 end 363 if eq v1 TS_SLOT_VALID 364 if ne and q1 1 0 365 ret TS_CORRUPT 366 end 367 if eq v0 TS_SLOT_BLANK 368 ret TS_CORRUPT 369 end 370 end 371 ret TS_OK 372 end 373 374 fn ts_pick_slot v0 uint v1 uint q0 ulong q1 ulong to int 375 if and eq v0 TS_SLOT_VALID eq v1 TS_SLOT_VALID 376 if gt q0 q1 377 if ne sub q0 q1 1 378 ret -2 379 end 380 ret 0 381 end 382 if ne sub q1 q0 1 383 ret -2 384 end 385 ret 1 386 end 387 if eq v0 TS_SLOT_VALID 388 ret 0 389 end 390 if eq v1 TS_SLOT_VALID 391 ret 1 392 end 393 ret -1 394 end 395 396 fn ts_recover_empty v1 uint to int 397 if ne v1 TS_SLOT_BLANK 398 ret TS_CORRUPT 399 end 400 plaus = new uchar 1 0 401 ef = ts_unfinished_first_commit plaus 402 if ne ef TS_OK 403 ret ef 404 end 405 if eq get plaus 0 0 406 ret TS_CORRUPT 407 end 408 put ts_reg TS_R_KNOWN 1 409 ret TS_EMPTY 410 end 411 412 fn ts_recover_finish dst edit uchar 4096 src view uchar 4096 slot ulong seq ulong seq_out edit ulong 1 to int 413 for i ulong 4096 414 put dst i get src i 415 end 416 put seq_out 0 seq 417 put ts_reg TS_R_NEWEST_SLOT slot 418 put ts_reg TS_R_NEWEST_SEQ seq 419 put ts_reg TS_R_HAS_NEWEST 1 420 put ts_reg TS_R_KNOWN 1 421 ret TS_OK 422 end 423 424 fn ts_recover dst edit uchar 4096 seq_out edit ulong 1 slot_states edit uint 2 to int 425 put ts_reg TS_R_KNOWN 0 426 put ts_reg TS_R_HAS_NEWEST 0 427 put ts_reg TS_R_NEWEST_SLOT 0 428 put ts_reg TS_R_NEWEST_SEQ 0 429 for i ulong 4096 430 put dst i 0 431 end 432 put seq_out 0 0 433 if eq get ts_reg TS_R_BPU 0 434 ret TS_EGEOMETRY 435 end 436 b0 = new uchar 4096 0 437 b1 = new uchar 4096 0 438 sq = new ulong 1 0 439 st = new uint 1 0 440 e = ts_check_slot 0 b0 sq st 441 if ne e TS_OK 442 ret e 443 end 444 v0 = get st 0 445 q0 = get sq 0 446 e = ts_check_slot 1 b1 sq st 447 if ne e TS_OK 448 ret e 449 end 450 v1 = get st 0 451 q1 = get sq 0 452 put slot_states 0 v0 453 put slot_states 1 v1 454 455 e = ts_check_lineage v0 v1 q0 q1 456 if ne e TS_OK 457 ret e 458 end 459 pick = ts_pick_slot v0 v1 q0 q1 460 if eq pick -2 461 ret TS_CORRUPT 462 end 463 if lt pick 0 464 ret ts_recover_empty v1 465 end 466 if eq pick 0 467 if and eq v1 TS_SLOT_BLANK ne q0 1 468 ret TS_CORRUPT 469 end 470 ret ts_recover_finish dst b0 0 q0 seq_out 471 end 472 ret ts_recover_finish dst b1 1 q1 seq_out 473 end 474 475 fn ts_commit rec view uchar 4096 to int 476 if eq get ts_reg TS_R_KNOWN 0 477 ret TS_ESTATE 478 end 479 cur_seq = get ts_reg TS_R_NEWEST_SEQ 480 if eq cur_seq TS_U64_MAX 481 ret TS_ESTATE 482 end 483 target = ulong 0 484 if ne get ts_reg TS_R_HAS_NEWEST 0 485 target = sub 1 get ts_reg TS_R_NEWEST_SLOT 486 end 487 nxt_seq = add cur_seq 1 488 put ts_reg TS_R_KNOWN 0 489 e = ts_media_write_unit mul 2 target rec 490 if ne e TS_OK 491 ret e 492 end 493 if ne ts_media_flush TS_OK 494 ret TS_EIO 495 end 496 cu = new uchar 4096 0 497 ts_encode_commit cu uchar target nxt_seq rec 498 e = ts_media_write_unit add mul 2 target 1 cu 499 if ne e TS_OK 500 ret e 501 end 502 if ne ts_media_flush TS_OK 503 ret TS_EIO 504 end 505 put ts_reg TS_R_HAS_NEWEST 1 506 put ts_reg TS_R_NEWEST_SLOT target 507 put ts_reg TS_R_NEWEST_SEQ nxt_seq 508 put ts_reg TS_R_KNOWN 1 509 ret TS_OK 510 end