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 / store / torn_slot_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/store/torn_slot_test.om: Behavioral self-test for dual-slot torn-write-safe record. |
| 3 | note Compose with: si/directives/om2.om aienux/store/torn_slot.om aienux/store/torn_slot_test.om om/tls/sha2.om |
| 4 | |
| 5 | text ts_pass "PASS " |
| 6 | text ts_fail "FAIL " |
| 7 | text ts_nl "\n" |
| 8 | text ts_t01 "geometry_open_and_empty_recover" |
| 9 | text ts_t02 "commit_alternates_slots_and_recovers" |
| 10 | text ts_t03 "torn_first_commit_recovers_empty_not_corrupt" |
| 11 | text ts_t04 "torn_second_commit_preserves_slot_a" |
| 12 | text ts_t05 "lineage_guards_parity_adjacent_conflict_b_needs_a" |
| 13 | text ts_t06 "commit_without_recover_fails_estate" |
| 14 | text ts_sum "TORN_SLOT_TEST_PASS total=6 failed=0\n" |
| 15 | text ts_fsum "TORN_SLOT_TEST_FAIL\n" |
| 16 | |
| 17 | fn ts_write_all fd int buf view uchar * to bool |
| 18 | total = len buf |
| 19 | off = ulong 0 |
| 20 | while lt off total bound 4096 |
| 21 | n = write fd buf off sub total off |
| 22 | if le n 0 |
| 23 | ret false |
| 24 | end |
| 25 | off = add off ulong n |
| 26 | end |
| 27 | ret true |
| 28 | end |
| 29 | |
| 30 | fn ts_report name view uchar * ok bool to uint |
| 31 | if ok |
| 32 | w1 = ts_write_all 1 ts_pass |
| 33 | w2 = ts_write_all 1 name |
| 34 | w3 = ts_write_all 1 ts_nl |
| 35 | if and w1 and w2 w3 |
| 36 | ret 0 |
| 37 | end |
| 38 | ret 1 |
| 39 | end |
| 40 | w1 = ts_write_all 1 ts_fail |
| 41 | w2 = ts_write_all 1 name |
| 42 | w3 = ts_write_all 1 ts_nl |
| 43 | if and w1 and w2 w3 |
| 44 | ret 1 |
| 45 | end |
| 46 | ret 1 |
| 47 | end |
| 48 | |
| 49 | fn fill_rec dst edit uchar 4096 tag uchar to void |
| 50 | for i ulong 4096 |
| 51 | put dst i xor tag uchar and i 0xff |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | fn rec_matches buf view uchar 4096 tag uchar to bool |
| 56 | for i ulong 4096 |
| 57 | if ne get buf i xor tag uchar and i 0xff |
| 58 | ret false |
| 59 | end |
| 60 | end |
| 61 | ret true |
| 62 | end |
| 63 | |
| 64 | fn tt_01_geometry_and_empty to bool |
| 65 | ts_media_zero |
| 66 | note Unsupported block size 1024 refused |
| 67 | if ne ts_open 1024 64 0 TS_EGEOMETRY |
| 68 | ret false |
| 69 | end |
| 70 | note Too few blocks refused |
| 71 | if ne ts_open 512 31 0 TS_EGEOMETRY |
| 72 | ret false |
| 73 | end |
| 74 | note Valid 512-byte geometry (32 blocks = 4 units) |
| 75 | if ne ts_open 512 32 0 TS_OK |
| 76 | ret false |
| 77 | end |
| 78 | out_rec = new uchar 4096 0 |
| 79 | seq = new ulong 1 0 |
| 80 | st = new uint 2 0 |
| 81 | if ne ts_recover out_rec seq st TS_EMPTY |
| 82 | ret false |
| 83 | end |
| 84 | if or ne get st 0 TS_SLOT_BLANK ne get st 1 TS_SLOT_BLANK |
| 85 | ret false |
| 86 | end |
| 87 | ret true |
| 88 | end |
| 89 | |
| 90 | fn tt_02_commit_alternates to bool |
| 91 | ts_media_zero |
| 92 | if ne ts_open 4096 8 0 TS_OK |
| 93 | ret false |
| 94 | end |
| 95 | rec = new uchar 4096 0 |
| 96 | got = new uchar 4096 0 |
| 97 | seq = new ulong 1 0 |
| 98 | st = new uint 2 0 |
| 99 | if ne ts_recover got seq st TS_EMPTY |
| 100 | ret false |
| 101 | end |
| 102 | note Commit 1 -> slot A (seq 1), Commit 2 -> slot B (seq 2), Commit 3 -> slot A (seq 3) |
| 103 | fill_rec rec 0x11 |
| 104 | if ne ts_commit rec TS_OK |
| 105 | ret false |
| 106 | end |
| 107 | fill_rec rec 0x22 |
| 108 | if ne ts_commit rec TS_OK |
| 109 | ret false |
| 110 | end |
| 111 | fill_rec rec 0x33 |
| 112 | if ne ts_commit rec TS_OK |
| 113 | ret false |
| 114 | end |
| 115 | if ne ts_recover got seq st TS_OK |
| 116 | ret false |
| 117 | end |
| 118 | if or ne get seq 0 3 not rec_matches got 0x33 |
| 119 | ret false |
| 120 | end |
| 121 | if or ne get st 0 TS_SLOT_VALID ne get st 1 TS_SLOT_VALID |
| 122 | ret false |
| 123 | end |
| 124 | ret true |
| 125 | end |
| 126 | |
| 127 | fn tt_03_torn_first_commit to bool |
| 128 | ts_media_zero |
| 129 | if ne ts_open 512 64 0 TS_OK |
| 130 | ret false |
| 131 | end |
| 132 | rec = new uchar 4096 0 |
| 133 | got = new uchar 4096 0 |
| 134 | seq = new ulong 1 0 |
| 135 | st = new uint 2 0 |
| 136 | if ne ts_recover got seq st TS_EMPTY |
| 137 | ret false |
| 138 | end |
| 139 | note Simulate partial write of commit unit A (only first 12 bytes written) |
| 140 | fill_rec rec 0x44 |
| 141 | cu = new uchar 4096 0 |
| 142 | ts_encode_commit cu 0 1 rec |
| 143 | for i ulong 12 |
| 144 | ts_media_poke 1 i get cu i |
| 145 | end |
| 146 | if ne ts_recover got seq st TS_EMPTY |
| 147 | ret false |
| 148 | end |
| 149 | note Corrupt byte in slot A commit header past magic -> TS_CORRUPT (first-remnant guard) |
| 150 | ts_media_poke 1 10 0x7f |
| 151 | if ne ts_recover got seq st TS_CORRUPT |
| 152 | ret false |
| 153 | end |
| 154 | ret true |
| 155 | end |
| 156 | |
| 157 | fn tt_04_torn_second_preserves_a to bool |
| 158 | ts_media_zero |
| 159 | if ne ts_open 512 64 0 TS_OK |
| 160 | ret false |
| 161 | end |
| 162 | rec = new uchar 4096 0 |
| 163 | got = new uchar 4096 0 |
| 164 | seq = new ulong 1 0 |
| 165 | st = new uint 2 0 |
| 166 | if ne ts_recover got seq st TS_EMPTY |
| 167 | ret false |
| 168 | end |
| 169 | fill_rec rec 0x55 |
| 170 | if ne ts_commit rec TS_OK |
| 171 | ret false |
| 172 | end |
| 173 | note Tear during second commit (slot B body write fails halfway) |
| 174 | ts_set_faults 0 0 512 |
| 175 | fill_rec rec 0x66 |
| 176 | if ne ts_commit rec TS_EIO |
| 177 | ret false |
| 178 | end |
| 179 | ts_set_faults 0 0 0 |
| 180 | note Recovery must cleanly return slot A (seq 1, 0x55) |
| 181 | if ne ts_recover got seq st TS_OK |
| 182 | ret false |
| 183 | end |
| 184 | if or ne get seq 0 1 not rec_matches got 0x55 |
| 185 | ret false |
| 186 | end |
| 187 | ret true |
| 188 | end |
| 189 | |
| 190 | fn tt_05_lineage_guards to bool |
| 191 | ts_media_zero |
| 192 | if ne ts_open 4096 8 0 TS_OK |
| 193 | ret false |
| 194 | end |
| 195 | rec = new uchar 4096 0 |
| 196 | cu = new uchar 4096 0 |
| 197 | got = new uchar 4096 0 |
| 198 | seq = new ulong 1 0 |
| 199 | st = new uint 2 0 |
| 200 | fill_rec rec 0x77 |
| 201 | note Guard parity-a: slot 0 with even seq 2 -> TS_CORRUPT |
| 202 | ts_encode_commit cu 0 2 rec |
| 203 | if ne ts_media_write_unit 0 rec TS_OK |
| 204 | ret false |
| 205 | end |
| 206 | if ne ts_media_write_unit 1 cu TS_OK |
| 207 | ret false |
| 208 | end |
| 209 | if ne ts_recover got seq st TS_CORRUPT |
| 210 | ret false |
| 211 | end |
| 212 | note Guard b-needs-a: slot 0 blank, slot 1 valid seq 2 -> TS_CORRUPT |
| 213 | ts_media_zero |
| 214 | if ne ts_open 4096 8 0 TS_OK |
| 215 | ret false |
| 216 | end |
| 217 | ts_encode_commit cu 1 2 rec |
| 218 | if ne ts_media_write_unit 2 rec TS_OK |
| 219 | ret false |
| 220 | end |
| 221 | if ne ts_media_write_unit 3 cu TS_OK |
| 222 | ret false |
| 223 | end |
| 224 | if ne ts_recover got seq st TS_CORRUPT |
| 225 | ret false |
| 226 | end |
| 227 | note Guard adjacent: slot 0 valid seq 1, slot 1 valid seq 4 -> TS_CORRUPT |
| 228 | ts_encode_commit cu 0 1 rec |
| 229 | if ne ts_media_write_unit 0 rec TS_OK |
| 230 | ret false |
| 231 | end |
| 232 | if ne ts_media_write_unit 1 cu TS_OK |
| 233 | ret false |
| 234 | end |
| 235 | ts_encode_commit cu 1 4 rec |
| 236 | if ne ts_media_write_unit 3 cu TS_OK |
| 237 | ret false |
| 238 | end |
| 239 | if ne ts_recover got seq st TS_CORRUPT |
| 240 | ret false |
| 241 | end |
| 242 | note Guard conflict: slot 0 valid seq 2, slot 1 valid seq 2 -> TS_CONFLICT |
| 243 | ts_encode_commit cu 0 2 rec |
| 244 | if ne ts_media_write_unit 1 cu TS_OK |
| 245 | ret false |
| 246 | end |
| 247 | ts_encode_commit cu 1 2 rec |
| 248 | if ne ts_media_write_unit 3 cu TS_OK |
| 249 | ret false |
| 250 | end |
| 251 | if ne ts_recover got seq st TS_CONFLICT |
| 252 | ret false |
| 253 | end |
| 254 | note Guard a-alone-seq1: slot 0 valid seq 3, slot 1 blank -> TS_CORRUPT |
| 255 | ts_media_zero |
| 256 | if ne ts_open 4096 8 0 TS_OK |
| 257 | ret false |
| 258 | end |
| 259 | ts_encode_commit cu 0 3 rec |
| 260 | if ne ts_media_write_unit 0 rec TS_OK |
| 261 | ret false |
| 262 | end |
| 263 | if ne ts_media_write_unit 1 cu TS_OK |
| 264 | ret false |
| 265 | end |
| 266 | if ne ts_recover got seq st TS_CORRUPT |
| 267 | ret false |
| 268 | end |
| 269 | ret true |
| 270 | end |
| 271 | |
| 272 | fn tt_06_commit_requires_recover to bool |
| 273 | ts_media_zero |
| 274 | if ne ts_open 4096 8 0 TS_OK |
| 275 | ret false |
| 276 | end |
| 277 | rec = new uchar 4096 0 |
| 278 | fill_rec rec 0x88 |
| 279 | if ne ts_commit rec TS_ESTATE |
| 280 | ret false |
| 281 | end |
| 282 | ret true |
| 283 | end |
| 284 | |
| 285 | fn main to int |
| 286 | fails = uint 0 |
| 287 | fails = add fails ts_report ts_t01 tt_01_geometry_and_empty |
| 288 | fails = add fails ts_report ts_t02 tt_02_commit_alternates |
| 289 | fails = add fails ts_report ts_t03 tt_03_torn_first_commit |
| 290 | fails = add fails ts_report ts_t04 tt_04_torn_second_preserves_a |
| 291 | fails = add fails ts_report ts_t05 tt_05_lineage_guards |
| 292 | fails = add fails ts_report ts_t06 tt_06_commit_requires_recover |
| 293 | if eq fails 0 |
| 294 | w = ts_write_all 1 ts_sum |
| 295 | if w |
| 296 | ret 0 |
| 297 | end |
| 298 | ret 1 |
| 299 | end |
| 300 | w = ts_write_all 1 ts_fsum |
| 301 | if w |
| 302 | ret 1 |
| 303 | end |
| 304 | ret 1 |
| 305 | end |