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/aienux/blob/main/native/store/tests/torn_slot_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. 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 `aienux / native / store / tests / torn_slot_test.c` 773 lines ยท 32864 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * torn_slot_test.c -- host block-device emulation and exhaustive torn-write 19 * fault injection for torn_slot.c. 20 * 21 * This is NOT QEMU and NOT a real SSD. The emulated device keeps a durable 22 * image and records every write as individual logical-block writes, split 23 * into flush epochs. A crash image is built from the durable image before the 24 * commit plus a chosen fate for each block written in the epoch that was cut: 25 * the block lands NEW, stays OLD, or is TORN inside the block. Epochs before 26 * the cut are durable (FLUSH returned), epochs after it never happened. 27 * 28 * For every crash image the test runs recovery and asserts it returns the 29 * complete old record or the complete new record, byte for byte, and never 30 * anything else. It then commits a further record on the crash image and 31 * asserts that one is recovered (a crash never wedges the region). 32 */ 33 #include "../torn_slot.h" 34 35 #include 36 #include 37 #include 38 #include 39 40 static unsigned long long checks, failures; 41 42 #define CHECK(cond) \ 43 do { \ 44 checks++; \ 45 if (!(cond)) { \ 46 failures++; \ 47 if (failures < 20) \ 48 fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ 49 } \ 50 } while (0) 51 52 /* ---------------- emulated device ---------------- */ 53 54 #define MAX_TRACE 64 55 56 typedef struct { 57 uint64_t lba; /* absolute block */ 58 uint32_t epoch; /* flush epoch index within the traced operation */ 59 uint8_t *data; /* block_size bytes */ 60 } trace_write; 61 62 typedef struct { 63 uint32_t bs; 64 uint64_t nblocks; 65 uint8_t *media; /* what reads see (and, untraced, what is durable) */ 66 int tracing; 67 uint32_t epoch; 68 unsigned ntrace; 69 trace_write trace[MAX_TRACE]; 70 unsigned long reads, writes, flushes; 71 int fail_read_at; /* -1 = never; else fail the Nth read */ 72 } emu; 73 74 static int emu_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf) 75 { 76 emu *e = ctx; 77 if (e->fail_read_at >= 0 && (unsigned long)e->fail_read_at == e->reads) { e->reads++; return 1; } 78 e->reads++; 79 if (lba + count > e->nblocks) return 1; 80 memcpy(buf, e->media + lba * e->bs, (size_t)count * e->bs); 81 return 0; 82 } 83 84 static int emu_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf) 85 { 86 emu *e = ctx; 87 e->writes++; 88 if (lba + count > e->nblocks) return 1; 89 for (uint32_t i = 0; i < count; i++) { 90 if (e->tracing) { 91 if (e->ntrace >= MAX_TRACE) abort(); 92 trace_write *t = &e->trace[e->ntrace++]; 93 t->lba = lba + i; 94 t->epoch = e->epoch; 95 t->data = malloc(e->bs); 96 if (!t->data) abort(); 97 memcpy(t->data, buf + (size_t)i * e->bs, e->bs); 98 } 99 memcpy(e->media + (lba + i) * e->bs, buf + (size_t)i * e->bs, e->bs); 100 } 101 return 0; 102 } 103 104 static int emu_flush(void *ctx) 105 { 106 emu *e = ctx; 107 e->flushes++; 108 if (e->tracing) e->epoch++; 109 return 0; 110 } 111 112 static void emu_init(emu *e, uint32_t bs, uint64_t nblocks) 113 { 114 memset(e, 0, sizeof *e); 115 e->bs = bs; 116 e->nblocks = nblocks; 117 e->media = calloc(nblocks, bs); 118 if (!e->media) abort(); 119 e->fail_read_at = -1; 120 } 121 122 static void emu_clear_trace(emu *e) 123 { 124 for (unsigned i = 0; i < e->ntrace; i++) free(e->trace[i].data); 125 e->ntrace = 0; 126 e->epoch = 0; 127 e->tracing = 0; 128 } 129 130 static ts_device emu_dev(emu *e) 131 { 132 ts_device d = {e, e->bs, e->nblocks, emu_read, emu_write, emu_flush}; 133 return d; 134 } 135 136 /* ---------------- records ---------------- */ 137 138 static uint64_t xs_state; 139 static uint64_t xs(void) 140 { 141 xs_state ^= xs_state << 13; 142 xs_state ^= xs_state >> 7; 143 xs_state ^= xs_state << 17; 144 return xs_state; 145 } 146 147 static void make_record(uint8_t *r, unsigned gen) 148 { 149 /* gen 3 repeats gen 2 (same bytes, new seq); gen 4 is all zero. */ 150 if (gen == 4) { memset(r, 0, TS_RECORD_BYTES); return; } 151 if (gen == 3) gen = 2; 152 uint64_t s = 0x9E3779B97F4A7C15ull * (gen + 1); 153 for (unsigned i = 0; i < TS_RECORD_BYTES; i++) { 154 s ^= s << 13; s ^= s >> 7; s ^= s << 17; 155 r[i] = (uint8_t)(s >> 24); 156 } 157 } 158 159 /* ---------------- crash images ---------------- */ 160 161 enum { FATE_OLD = 0, FATE_NEW = 1, FATE_TORN_PREFIX = 2, FATE_TORN_WORDS = 3 }; 162 163 typedef struct { 164 uint32_t cut_epoch; /* epoch in which power was cut */ 165 unsigned n; /* writes in that epoch */ 166 uint8_t fate[MAX_TRACE]; /* per write in that epoch */ 167 uint32_t torn_bytes; /* FATE_TORN_PREFIX: new bytes persisted from offset 0 */ 168 uint64_t word_seed; /* FATE_TORN_WORDS: 8-byte words picked old/new */ 169 } crash_spec; 170 171 /* Build the crash image into img from pre (durable before the commit) and 172 * the trace in e. */ 173 static void build_crash(const emu *e, const uint8_t *pre, uint8_t *img, const crash_spec *c) 174 { 175 size_t bytes = (size_t)e->nblocks * e->bs; 176 memcpy(img, pre, bytes); 177 unsigned idx = 0; 178 for (unsigned i = 0; i < e->ntrace; i++) { 179 const trace_write *t = &e->trace[i]; 180 uint8_t *dst = img + t->lba * e->bs; 181 if (t->epoch < c->cut_epoch) { memcpy(dst, t->data, e->bs); continue; } 182 if (t->epoch > c->cut_epoch) continue; 183 uint8_t fate = c->fate[idx++]; 184 if (fate == FATE_NEW) { 185 memcpy(dst, t->data, e->bs); 186 } else if (fate == FATE_TORN_PREFIX) { 187 memcpy(dst, t->data, c->torn_bytes); 188 } else if (fate == FATE_TORN_WORDS) { 189 uint64_t s = c->word_seed | 1; 190 for (uint32_t w = 0; w < e->bs; w += 8) { 191 s ^= s << 13; s ^= s >> 7; s ^= s << 17; 192 if (s & 1) memcpy(dst + w, t->data + w, 8); 193 } 194 } 195 } 196 } 197 198 static unsigned epoch_writes(const emu *e, uint32_t epoch) 199 { 200 unsigned n = 0; 201 for (unsigned i = 0; i < e->ntrace; i++) n += e->trace[i].epoch == epoch; 202 return n; 203 } 204 205 /* ---------------- one geometry campaign ---------------- */ 206 207 typedef struct { 208 unsigned long long cases, as_old, as_new, mixed, bad_code, relive_fail; 209 unsigned long long prefix_cases, subset_cases, torn_prefix_cases, torn_word_cases, nested_cases; 210 unsigned long long nested_after_keep; 211 } tally; 212 213 /* Recover from img and classify against old/new. old_gen 0 means "no record". */ 214 static int classify(uint32_t bs, uint64_t nblocks, uint64_t base, uint8_t *img, 215 const uint8_t *oldr, unsigned old_gen, const uint8_t *newr, 216 unsigned new_gen, tally *t, int relive, unsigned relive_gen) 217 { 218 emu v; 219 memset(&v, 0, sizeof v); 220 v.bs = bs; v.nblocks = nblocks; v.media = img; v.fail_read_at = -1; 221 ts_device d = emu_dev(&v); 222 ts_region r; 223 uint8_t got[TS_RECORD_BYTES]; 224 uint64_t seq = 0; 225 int outcome = -1; /* 0 old, 1 new */ 226 227 CHECK(ts_open(&r, &d, base) == TS_OK); 228 int rc = ts_recover(&r, got, &seq, NULL); 229 CHECK(v.writes == 0); /* recovery never writes */ 230 t->cases++; 231 if (rc == TS_EMPTY && old_gen == 0) { 232 outcome = 0; 233 } else if (rc == TS_OK) { 234 if (seq == old_gen && old_gen != 0 && memcmp(got, oldr, TS_RECORD_BYTES) == 0) outcome = 0; 235 else if (seq == new_gen && memcmp(got, newr, TS_RECORD_BYTES) == 0) outcome = 1; 236 } 237 if (outcome == 0) t->as_old++; 238 else if (outcome == 1) t->as_new++; 239 else if (rc == TS_OK || rc == TS_EMPTY) { t->mixed++; CHECK(!"recovered neither complete old nor complete new"); } 240 else { t->bad_code++; CHECK(!"recovery returned an error code after a crash"); } 241 242 if (relive && outcome >= 0) { 243 uint8_t rec[TS_RECORD_BYTES], back[TS_RECORD_BYTES]; 244 make_record(rec, relive_gen); 245 rec[0] ^= 0x5A; rec[4095] ^= 0xA5; /* distinct from every campaign record */ 246 int ok = ts_commit(&r, rec) == TS_OK; 247 ts_region r2; 248 uint64_t s2 = 0; 249 ok = ok && ts_open(&r2, &d, base) == TS_OK && ts_recover(&r2, back, &s2, NULL) == TS_OK; 250 ok = ok && memcmp(back, rec, TS_RECORD_BYTES) == 0 && s2 == (outcome ? new_gen : old_gen) + 1; 251 if (!ok) { t->relive_fail++; CHECK(!"commit after crash recovery did not stick"); } 252 } 253 return outcome; 254 } 255 256 static void run_crash(emu *e, const uint8_t *pre, uint8_t *img, const crash_spec *c, 257 uint64_t base, const uint8_t *oldr, unsigned old_gen, 258 const uint8_t *newr, unsigned new_gen, tally *t) 259 { 260 build_crash(e, pre, img, c); 261 classify(e->bs, e->nblocks, base, img, oldr, old_gen, newr, new_gen, t, 1, new_gen + 50); 262 } 263 264 static int campaign(uint32_t bs, uint64_t base, unsigned gens, int nested) 265 { 266 uint64_t nblocks = base + (uint64_t)TS_UNITS * (TS_RECORD_BYTES / bs) + 3; /* spare blocks after */ 267 size_t bytes = (size_t)nblocks * bs; 268 emu e; 269 emu_init(&e, bs, nblocks); 270 uint8_t *pre = malloc(bytes), *img = malloc(bytes), *img2 = malloc(bytes); 271 uint8_t oldr[TS_RECORD_BYTES], newr[TS_RECORD_BYTES]; 272 if (!pre || !img || !img2) abort(); 273 /* Blocks outside the region carry a pattern that must never change. */ 274 for (size_t i = 0; i < bytes; i++) e.media[i] = (uint8_t)(0xC3 ^ i); 275 memset(e.media + base * bs, 0, (size_t)TS_UNITS * TS_RECORD_BYTES); 276 tally t; 277 memset(&t, 0, sizeof t); 278 279 ts_device d = emu_dev(&e); 280 ts_region r; 281 uint8_t got[TS_RECORD_BYTES]; 282 uint64_t seq; 283 CHECK(ts_open(&r, &d, base) == TS_OK); 284 CHECK(ts_recover(&r, got, &seq, NULL) == TS_EMPTY); 285 memset(oldr, 0, sizeof oldr); 286 287 for (unsigned g = 1; g <= gens; g++) { 288 make_record(newr, g); 289 memcpy(pre, e.media, bytes); 290 e.tracing = 1; 291 CHECK(ts_commit(&r, newr) == TS_OK); 292 e.tracing = 0; 293 /* The commit is exactly: one unit, FLUSH, one unit, FLUSH. */ 294 unsigned m = TS_RECORD_BYTES / bs; 295 CHECK(e.ntrace == 2 * m && e.epoch == 2); 296 CHECK(epoch_writes(&e, 0) == m && epoch_writes(&e, 1) == m); 297 298 for (uint32_t ep = 0; ep < 2; ep++) { 299 crash_spec c; 300 memset(&c, 0, sizeof c); 301 c.cut_epoch = ep; 302 c.n = m; 303 /* 1. Prefix cuts: the first k blocks landed, k = 0..m. */ 304 for (unsigned k = 0; k <= m; k++) { 305 for (unsigned i = 0; i < m; i++) c.fate[i] = i < k ? FATE_NEW : FATE_OLD; 306 run_crash(&e, pre, img, &c, base, oldr, g - 1, newr, g, &t); 307 t.prefix_cases++; 308 } 309 /* 2. Every subset of the m blocks (reordered completion). */ 310 for (unsigned mask = 0; mask < (1u << m); mask++) { 311 for (unsigned i = 0; i < m; i++) c.fate[i] = (mask >> i) & 1 ? FATE_NEW : FATE_OLD; 312 run_crash(&e, pre, img, &c, base, oldr, g - 1, newr, g, &t); 313 t.subset_cases++; 314 } 315 /* 3. Torn inside block k at every byte offset, blocks before k 316 * landed, blocks after k did not. */ 317 for (unsigned k = 0; k < m; k++) { 318 for (uint32_t b = 1; b < bs; b++) { 319 for (unsigned i = 0; i < m; i++) c.fate[i] = i < k ? FATE_NEW : FATE_OLD; 320 c.fate[k] = FATE_TORN_PREFIX; 321 c.torn_bytes = b; 322 run_crash(&e, pre, img, &c, base, oldr, g - 1, newr, g, &t); 323 t.torn_prefix_cases++; 324 } 325 } 326 /* 4. Torn inside block k as a scatter of 8-byte words, the other 327 * blocks a random subset. */ 328 for (unsigned k = 0; k < m; k++) { 329 for (unsigned trial = 0; trial < 64; trial++) { 330 uint64_t rs = xs(); 331 for (unsigned i = 0; i < m; i++) c.fate[i] = (rs >> i) & 1 ? FATE_NEW : FATE_OLD; 332 c.fate[k] = FATE_TORN_WORDS; 333 c.word_seed = xs(); 334 run_crash(&e, pre, img, &c, base, oldr, g - 1, newr, g, &t); 335 t.torn_word_cases++; 336 } 337 } 338 } 339 340 /* 5. Double crash: crash the commit (prefix cut), recover, then run 341 * the next commit on the crash image and crash that too (prefix 342 * cut), and check a further commit sticks. If the first crash lost 343 * the new record, the next commit retries it (seq g, same slot). 344 * If the first crash kept it, the next commit is a distinct record 345 * (seq g+1, opposite slot). */ 346 if (nested && g >= 2) { 347 uint8_t nextr[TS_RECORD_BYTES]; 348 make_record(nextr, g + 20); 349 nextr[7] ^= 0x3C; /* distinct from every campaign record */ 350 uint8_t *img3 = malloc(bytes); 351 if (!img3) abort(); 352 for (uint32_t ep1 = 0; ep1 < 2; ep1++) { 353 for (unsigned k1 = 0; k1 <= m; k1++) { 354 crash_spec c1; 355 memset(&c1, 0, sizeof c1); 356 c1.cut_epoch = ep1; c1.n = m; 357 for (unsigned i = 0; i < m; i++) c1.fate[i] = i < k1 ? FATE_NEW : FATE_OLD; 358 build_crash(&e, pre, img, &c1); 359 emu v; 360 memset(&v, 0, sizeof v); 361 v.bs = bs; v.nblocks = nblocks; v.media = img2; v.fail_read_at = -1; 362 memcpy(img2, img, bytes); 363 ts_device vd = emu_dev(&v); 364 ts_region vr; 365 uint64_t s1 = 0; 366 CHECK(ts_open(&vr, &vd, base) == TS_OK); 367 int rc1 = ts_recover(&vr, got, &s1, NULL); 368 CHECK(rc1 == TS_OK); 369 CHECK(s1 == g - 1 || s1 == g); 370 const uint8_t *o2 = s1 == g ? newr : oldr; 371 const uint8_t *n2 = s1 == g ? nextr : newr; 372 v.tracing = 1; 373 CHECK(ts_commit(&vr, n2) == TS_OK); 374 v.tracing = 0; 375 for (uint32_t ep2 = 0; ep2 < 2; ep2++) { 376 for (unsigned k2 = 0; k2 <= m; k2++) { 377 crash_spec c2; 378 memset(&c2, 0, sizeof c2); 379 c2.cut_epoch = ep2; c2.n = m; 380 for (unsigned i = 0; i < m; i++) c2.fate[i] = i < k2 ? FATE_NEW : FATE_OLD; 381 build_crash(&v, img, img3, &c2); 382 classify(bs, nblocks, base, img3, o2, (unsigned)s1, n2, (unsigned)s1 + 1, &t, 383 1, g + 60); 384 t.nested_cases++; 385 if (s1 == g) t.nested_after_keep++; 386 } 387 } 388 emu_clear_trace(&v); 389 } 390 } 391 free(img3); 392 } 393 394 emu_clear_trace(&e); 395 /* Live state still recovers the new record; outside blocks untouched. */ 396 { 397 ts_region r3; 398 CHECK(ts_open(&r3, &d, base) == TS_OK); 399 CHECK(ts_recover(&r3, got, &seq, NULL) == TS_OK); 400 CHECK(seq == g && memcmp(got, newr, TS_RECORD_BYTES) == 0); 401 for (size_t i = 0; i < bytes; i++) { 402 if (i >= base * bs && i < base * bs + (size_t)TS_UNITS * TS_RECORD_BYTES) continue; 403 if (e.media[i] != (uint8_t)(0xC3 ^ i)) { CHECK(!"write outside the region"); break; } 404 } 405 } 406 memcpy(oldr, newr, sizeof oldr); 407 } 408 409 printf(" bs=%u base_lba=%" PRIu64 " gens=%u: cases=%llu (prefix=%llu subset=%llu torn_prefix=%llu " 410 "torn_words=%llu nested=%llu nested_after_keep=%llu) as_old=%llu as_new=%llu mixed=%llu error=%llu relive_fail=%llu\n", 411 bs, base, gens, t.cases, t.prefix_cases, t.subset_cases, t.torn_prefix_cases, 412 t.torn_word_cases, t.nested_cases, t.nested_after_keep, t.as_old, t.as_new, t.mixed, t.bad_code, 413 t.relive_fail); 414 int ok = t.mixed == 0 && t.bad_code == 0 && t.relive_fail == 0 && t.as_old > 0 && t.as_new > 0 && 415 (!nested || t.nested_after_keep > 0); 416 free(pre); free(img); free(img2); free(e.media); 417 return ok; 418 } 419 420 /* ---------------- barrier lost ---------------- */ 421 422 /* A device that acknowledges FLUSH without ordering (outside the protocol's 423 * device contract). Body and header writes then form one epoch and may land 424 * in any combination. The full-record digest must still keep recovery to 425 * complete old or complete new. Every subset of all 2m blocks is enumerated, 426 * plus a torn first header block over every body subset. */ 427 static int barrier_lost(uint32_t bs, unsigned gens) 428 { 429 unsigned m = TS_RECORD_BYTES / bs; 430 uint64_t base = 0, nblocks = (uint64_t)TS_UNITS * m; 431 size_t bytes = (size_t)nblocks * bs; 432 emu e; 433 emu_init(&e, bs, nblocks); 434 uint8_t *pre = malloc(bytes), *img = malloc(bytes); 435 uint8_t oldr[TS_RECORD_BYTES], newr[TS_RECORD_BYTES], got[TS_RECORD_BYTES]; 436 uint64_t seq; 437 if (!pre || !img) abort(); 438 tally t; 439 memset(&t, 0, sizeof t); 440 ts_device d = emu_dev(&e); 441 ts_region r; 442 CHECK(ts_open(&r, &d, base) == TS_OK); 443 CHECK(ts_recover(&r, got, &seq, NULL) == TS_EMPTY); 444 memset(oldr, 0, sizeof oldr); 445 for (unsigned g = 1; g <= gens; g++) { 446 make_record(newr, g); 447 memcpy(pre, e.media, bytes); 448 e.tracing = 1; 449 CHECK(ts_commit(&r, newr) == TS_OK); 450 e.tracing = 0; 451 for (unsigned i = 0; i < e.ntrace; i++) e.trace[i].epoch = 0; /* barrier lost */ 452 crash_spec c; 453 memset(&c, 0, sizeof c); 454 c.cut_epoch = 0; 455 c.n = 2 * m; 456 for (uint32_t mask = 0; mask < (1u << (2 * m)); mask++) { 457 for (unsigned i = 0; i < 2 * m; i++) c.fate[i] = (mask >> i) & 1 ? FATE_NEW : FATE_OLD; 458 build_crash(&e, pre, img, &c); 459 classify(bs, nblocks, base, img, oldr, g - 1, newr, g, &t, 1, g + 50); 460 t.subset_cases++; 461 } 462 for (uint32_t mask = 0; mask < (1u << m); mask++) { 463 for (uint32_t b = 8; b < bs; b += 8) { 464 for (unsigned i = 0; i < m; i++) c.fate[i] = (mask >> i) & 1 ? FATE_NEW : FATE_OLD; 465 for (unsigned i = m; i < 2 * m; i++) c.fate[i] = FATE_OLD; 466 c.fate[m] = FATE_TORN_PREFIX; 467 c.torn_bytes = b; 468 build_crash(&e, pre, img, &c); 469 classify(bs, nblocks, base, img, oldr, g - 1, newr, g, &t, 1, g + 50); 470 t.torn_prefix_cases++; 471 } 472 } 473 emu_clear_trace(&e); 474 memcpy(oldr, newr, sizeof oldr); 475 } 476 printf(" barrier lost bs=%u gens=%u: cases=%llu (subset=%llu torn_header=%llu) as_old=%llu as_new=%llu " 477 "mixed=%llu error=%llu relive_fail=%llu\n", bs, gens, t.cases, t.subset_cases, t.torn_prefix_cases, t.as_old, 478 t.as_new, t.mixed, t.bad_code, t.relive_fail); 479 free(pre); free(img); free(e.media); 480 return t.mixed == 0 && t.bad_code == 0 && t.relive_fail == 0 && t.as_old > 0 && t.as_new > 0; 481 } 482 483 /* ---------------- negative control ---------------- */ 484 485 /* A naive in-place write of the 4096-byte record, no slots, no digest, under 486 * the same injector. The injector must find mixed records here, or it is not 487 * testing anything. */ 488 static unsigned long long naive_control(uint32_t bs) 489 { 490 /* The naive write goes through the same emulated device, trace and 491 * build_crash() as the protocol campaigns, so this control fails if the 492 * injector stops producing any one kind of tear. */ 493 unsigned m = TS_RECORD_BYTES / bs; 494 uint8_t oldr[TS_RECORD_BYTES], newr[TS_RECORD_BYTES], pre[TS_RECORD_BYTES], img[TS_RECORD_BYTES]; 495 emu e; 496 emu_init(&e, bs, m); 497 make_record(oldr, 1); 498 make_record(newr, 2); 499 ts_device d = emu_dev(&e); 500 CHECK(d.write(d.ctx, 0, m, oldr) == 0 && d.flush(d.ctx) == 0); 501 memcpy(pre, e.media, sizeof pre); 502 e.tracing = 1; 503 CHECK(d.write(d.ctx, 0, m, newr) == 0); 504 e.tracing = 0; 505 CHECK(e.ntrace == m); 506 crash_spec c; 507 memset(&c, 0, sizeof c); 508 c.cut_epoch = 0; 509 c.n = m; 510 unsigned long long mix_subset = 0, mix_prefix = 0, mix_words = 0; 511 for (unsigned mask = 0; mask < (1u << m); mask++) { 512 for (unsigned i = 0; i < m; i++) c.fate[i] = (mask >> i) & 1 ? FATE_NEW : FATE_OLD; 513 build_crash(&e, pre, img, &c); 514 if (memcmp(img, oldr, sizeof img) && memcmp(img, newr, sizeof img)) mix_subset++; 515 } 516 for (unsigned k = 0; k < m; k++) { 517 for (uint32_t b = 1; b < bs; b++) { 518 for (unsigned i = 0; i < m; i++) c.fate[i] = i < k ? FATE_NEW : FATE_OLD; 519 c.fate[k] = FATE_TORN_PREFIX; 520 c.torn_bytes = b; 521 build_crash(&e, pre, img, &c); 522 if (memcmp(img, oldr, sizeof img) && memcmp(img, newr, sizeof img)) mix_prefix++; 523 } 524 for (unsigned trial = 0; trial < 64; trial++) { 525 for (unsigned i = 0; i < m; i++) c.fate[i] = FATE_OLD; 526 c.fate[k] = FATE_TORN_WORDS; 527 c.word_seed = xs(); 528 build_crash(&e, pre, img, &c); 529 if (memcmp(img, oldr, sizeof img) && memcmp(img, newr, sizeof img)) mix_words++; 530 } 531 } 532 emu_clear_trace(&e); 533 free(e.media); 534 printf(" naive bs=%u: mixed subset=%llu torn_prefix=%llu torn_words=%llu\n", bs, mix_subset, 535 mix_prefix, mix_words); 536 /* bs=4096 has one block, so a whole-block subset cannot mix there. */ 537 if ((m > 1 && mix_subset == 0) || mix_prefix == 0 || mix_words == 0) return 0; 538 return mix_subset + mix_prefix + mix_words; 539 } 540 541 /* ---------------- rule tests ---------------- */ 542 543 static void rule_tests(void) 544 { 545 emu e; 546 ts_region r; 547 ts_device d; 548 uint8_t got[TS_RECORD_BYTES], a[TS_RECORD_BYTES], b[TS_RECORD_BYTES]; 549 uint64_t seq; 550 ts_slot_state st[2]; 551 552 /* Geometry: only 512 and 4096; region must fit. */ 553 uint32_t bad_bs[] = {0, 520, 1024, 2048, 8192}; 554 for (unsigned i = 0; i < sizeof bad_bs / sizeof bad_bs[0]; i++) { 555 emu_init(&e, 512, 64); 556 d = emu_dev(&e); 557 d.block_size = bad_bs[i]; 558 CHECK(ts_open(&r, &d, 0) == TS_EGEOMETRY); 559 free(e.media); 560 } 561 emu_init(&e, 512, 31); d = emu_dev(&e); CHECK(ts_open(&r, &d, 0) == TS_EGEOMETRY); free(e.media); 562 emu_init(&e, 512, 32); d = emu_dev(&e); CHECK(ts_open(&r, &d, 0) == TS_OK); 563 CHECK(ts_open(&r, &d, 1) == TS_EGEOMETRY); free(e.media); 564 emu_init(&e, 4096, 4); d = emu_dev(&e); CHECK(ts_open(&r, &d, 0) == TS_OK); free(e.media); 565 566 /* Commit before recover is refused. */ 567 emu_init(&e, 512, 32); d = emu_dev(&e); 568 CHECK(ts_open(&r, &d, 0) == TS_OK); 569 make_record(a, 1); 570 CHECK(ts_commit(&r, a) == TS_ESTATE); 571 CHECK(e.writes == 0); 572 573 /* Blank recovers EMPTY with both slots blank. */ 574 CHECK(ts_recover(&r, got, &seq, st) == TS_EMPTY); 575 CHECK(st[0] == TS_SLOT_BLANK && st[1] == TS_SLOT_BLANK); 576 CHECK(ts_commit(&r, a) == TS_OK); 577 CHECK(e.flushes == 2); 578 make_record(b, 2); 579 CHECK(ts_commit(&r, b) == TS_OK); 580 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 2 && !memcmp(got, b, sizeof b)); 581 CHECK(st[0] == TS_SLOT_VALID && st[1] == TS_SLOT_VALID); 582 583 /* Media damage in the newest body: falls back to the older complete record. */ 584 uint8_t *body_b = e.media + 2 * TS_RECORD_BYTES; 585 body_b[1234] ^= 1; 586 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 1 && !memcmp(got, a, sizeof a)); 587 CHECK(st[1] == TS_SLOT_INVALID); 588 /* The next commit overwrites the damaged slot, never the good one. */ 589 make_record(b, 5); 590 CHECK(ts_commit(&r, b) == TS_OK); 591 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 2 && !memcmp(got, b, sizeof b)); 592 593 /* Non-zero byte in the commit unit tail invalidates the slot. */ 594 e.media[3 * TS_RECORD_BYTES + 100] = 1; 595 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 1 && st[1] == TS_SLOT_INVALID); 596 e.media[3 * TS_RECORD_BYTES + 100] = 0; 597 598 /* A header whose own digest does not verify is rejected. */ 599 e.media[3 * TS_RECORD_BYTES + 60] ^= 1; 600 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 1 && st[1] == TS_SLOT_INVALID); 601 e.media[3 * TS_RECORD_BYTES + 60] ^= 1; 602 603 /* Both commit units damaged: CORRUPT, and commit is refused. */ 604 e.media[1 * TS_RECORD_BYTES + 20] ^= 1; 605 e.media[3 * TS_RECORD_BYTES + 20] ^= 1; 606 CHECK(ts_recover(&r, got, &seq, st) == TS_CORRUPT); 607 unsigned long w = e.writes; 608 CHECK(ts_commit(&r, a) == TS_ESTATE && e.writes == w); 609 e.media[1 * TS_RECORD_BYTES + 20] ^= 1; 610 e.media[3 * TS_RECORD_BYTES + 20] ^= 1; 611 612 /* Slot A blank but slot B non-blank and invalid: a crash cannot do that. */ 613 memset(e.media + 1 * TS_RECORD_BYTES, 0, TS_RECORD_BYTES); 614 e.media[3 * TS_RECORD_BYTES + 20] ^= 1; 615 CHECK(ts_recover(&r, got, &seq, st) == TS_CORRUPT); 616 e.media[3 * TS_RECORD_BYTES + 20] ^= 1; 617 618 /* Equal seq in two valid slots: CONFLICT. */ 619 ts_encode_commit(e.media + 1 * TS_RECORD_BYTES, 0, 7, e.media); 620 ts_encode_commit(e.media + 3 * TS_RECORD_BYTES, 1, 7, e.media + 2 * TS_RECORD_BYTES); 621 CHECK(ts_recover(&r, got, &seq, st) == TS_CONFLICT); 622 623 /* A header copied into the other slot fails the slot id check. */ 624 ts_encode_commit(e.media + 1 * TS_RECORD_BYTES, 0, 9, e.media); 625 memcpy(e.media + 3 * TS_RECORD_BYTES, e.media + 1 * TS_RECORD_BYTES, TS_RECORD_BYTES); 626 memcpy(e.media + 2 * TS_RECORD_BYTES, e.media, TS_RECORD_BYTES); 627 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 9 && st[1] == TS_SLOT_INVALID); 628 629 /* Read error is EIO, never a verdict about the media. */ 630 e.fail_read_at = (int)e.reads; 631 CHECK(ts_recover(&r, got, &seq, st) == TS_EIO); 632 CHECK(ts_commit(&r, a) == TS_ESTATE); 633 e.fail_read_at = -1; 634 free(e.media); 635 636 /* Sequence exhaustion is refused, not wrapped. */ 637 emu_init(&e, 4096, 4); d = emu_dev(&e); 638 CHECK(ts_open(&r, &d, 0) == TS_OK); 639 ts_encode_commit(e.media + TS_RECORD_BYTES, 0, UINT64_MAX, e.media); 640 ts_encode_commit(e.media + 3 * TS_RECORD_BYTES, 1, UINT64_MAX - 1, e.media + 2 * TS_RECORD_BYTES); 641 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == UINT64_MAX); 642 CHECK(ts_commit(&r, a) == TS_ESTATE); 643 free(e.media); 644 645 /* EMPTY only for what a crash in the first commit can leave in slot A. */ 646 emu_init(&e, 512, 64); d = emu_dev(&e); 647 CHECK(ts_open(&r, &d, 0) == TS_OK); 648 CHECK(ts_recover(&r, got, &seq, st) == TS_EMPTY); 649 CHECK(ts_commit(&r, a) == TS_OK); 650 e.media[TS_RECORD_BYTES + 11] = 1; /* reserved byte: zero before and after */ 651 CHECK(ts_recover(&r, got, &seq, st) == TS_CORRUPT); 652 CHECK(ts_commit(&r, a) == TS_ESTATE); 653 e.media[TS_RECORD_BYTES + 11] = 0; 654 e.media[TS_RECORD_BYTES + 200] = 0x77; /* past the header */ 655 e.media[TS_RECORD_BYTES + 60] ^= 1; 656 CHECK(ts_recover(&r, got, &seq, st) == TS_CORRUPT); 657 e.media[TS_RECORD_BYTES + 200] = 0; 658 CHECK(ts_recover(&r, got, &seq, st) == TS_EMPTY); /* damaged digest only */ 659 memset(e.media + TS_RECORD_BYTES + 40, 0, 48); /* torn first header */ 660 CHECK(ts_recover(&r, got, &seq, st) == TS_EMPTY); 661 /* A valid slot A beside a blank slot B must hold seq 1. */ 662 ts_encode_commit(e.media + TS_RECORD_BYTES, 0, 5, e.media); 663 CHECK(ts_recover(&r, got, &seq, st) == TS_CORRUPT); 664 ts_encode_commit(e.media + TS_RECORD_BYTES, 0, 1, e.media); 665 CHECK(ts_recover(&r, got, &seq, st) == TS_OK && seq == 1); 666 free(e.media); 667 } 668 669 /* ---------------- impossible slot states ---------------- */ 670 671 /* Region at LBA 0: unit u starts at byte u * 4096 for either block size. */ 672 static void forge_slot(uint8_t *media, int slot, uint64_t seq, unsigned gen) 673 { 674 uint8_t *body = media + (size_t)(2 * slot) * TS_RECORD_BYTES; 675 make_record(body, gen); 676 ts_encode_commit(media + (size_t)(2 * slot + 1) * TS_RECORD_BYTES, slot, seq, body); 677 } 678 679 static void damage_slot(uint8_t *media, int slot, uint64_t seq) 680 { 681 forge_slot(media, slot, seq, 9); 682 media[(size_t)(2 * slot + 1) * TS_RECORD_BYTES + 60] ^= 0x01; /* header digest */ 683 } 684 685 /* Slot states no sequence of commits and crashes can produce, each built so 686 * that exactly one lineage rule in ts_recover refuses it. Recovery must fail 687 * closed and a commit after it must be refused. Run at 512 and 4096. */ 688 static void impossible_states(uint32_t bs) 689 { 690 for (int c = 0; c < 6; c++) { 691 emu e; 692 emu_init(&e, bs, (uint64_t)TS_UNITS * (TS_RECORD_BYTES / bs)); 693 ts_device d = emu_dev(&e); 694 ts_region r; 695 uint8_t got[TS_RECORD_BYTES], rec[TS_RECORD_BYTES]; 696 uint64_t seq = 0; 697 CHECK(ts_open(&r, &d, 0) == TS_OK); 698 /* Start from a real lineage: two commits through the protocol. */ 699 CHECK(ts_recover(&r, got, &seq, NULL) == TS_EMPTY); 700 make_record(rec, 1); CHECK(ts_commit(&r, rec) == TS_OK); 701 make_record(rec, 2); CHECK(ts_commit(&r, rec) == TS_OK); 702 CHECK(ts_recover(&r, got, &seq, NULL) == TS_OK && seq == 2); 703 switch (c) { 704 case 0: /* b-needs-a: slot A wiped back to blank, slot B valid */ 705 memset(e.media, 0, 2 * TS_RECORD_BYTES); 706 break; 707 case 1: /* b-needs-a: only A's commit unit wiped */ 708 memset(e.media + TS_RECORD_BYTES, 0, TS_RECORD_BYTES); 709 break; 710 case 2: /* parity-b: B holds an odd seq, A damaged (not blank) */ 711 damage_slot(e.media, 0, 1); 712 forge_slot(e.media, 1, 3, 7); 713 break; 714 case 3: /* parity-a: A holds an even seq, B damaged (not blank) */ 715 forge_slot(e.media, 0, 4, 7); 716 damage_slot(e.media, 1, 2); 717 break; 718 case 4: /* adjacent: both valid, right parity, seqs 5 and 2 */ 719 forge_slot(e.media, 0, 5, 7); 720 break; 721 case 5: /* a-alone-seq1: A valid with seq 3 beside a blank B */ 722 forge_slot(e.media, 0, 3, 7); 723 memset(e.media + 2 * TS_RECORD_BYTES, 0, 2 * TS_RECORD_BYTES); 724 break; 725 } 726 int rc = ts_recover(&r, got, &seq, NULL); 727 if (rc != TS_CORRUPT) printf(" impossible state %d bs=%u: recover returned %d\n", c, bs, rc); 728 CHECK(rc == TS_CORRUPT); 729 CHECK(ts_commit(&r, rec) == TS_ESTATE); 730 free(e.media); 731 } 732 } 733 734 int main(int argc, char **argv) 735 { 736 xs_state = 0x243F6A8885A308D3ull; 737 int all_ok = 1; 738 739 rule_tests(); 740 impossible_states(512); 741 impossible_states(4096); 742 unsigned long long rule_fail = failures; 743 printf("TORN_SLOT_RULES: %s (%llu checks)\n", rule_fail ? "FAIL" : "PASS", checks); 744 all_ok &= rule_fail == 0; 745 if (argc > 1 && strcmp(argv[1], "--rules-only") == 0) return rule_fail ? 1 : 0; 746 747 unsigned long long n512 = naive_control(512), n4096 = naive_control(4096); 748 printf("NAIVE_IN_PLACE_CONTROL: mixed records found bs=512: %llu, bs=4096: %llu (expected > 0)\n", 749 n512, n4096); 750 int ctl = n512 > 0 && n4096 > 0; 751 printf("TORN_INJECTOR_DETECTS_MIXES: %s\n", ctl ? "PASS" : "FAIL"); 752 all_ok &= ctl; 753 754 struct { uint32_t bs; uint64_t base; } geo[] = {{512, 0}, {512, 3}, {512, 8}, {4096, 0}, {4096, 1}}; 755 int ok512 = 1, ok4096 = 1; 756 for (unsigned i = 0; i < sizeof geo / sizeof geo[0]; i++) { 757 int ok = campaign(geo[i].bs, geo[i].base, 6, 1); 758 if (geo[i].bs == 512) ok512 &= ok; else ok4096 &= ok; 759 } 760 printf("TORN_SLOT_FAULT_INJECTION_512: %s\n", ok512 ? "PASS" : "FAIL"); 761 printf("TORN_SLOT_FAULT_INJECTION_4096: %s\n", ok4096 ? "PASS" : "FAIL"); 762 all_ok &= ok512 && ok4096; 763 764 int bl = barrier_lost(512, 3) & barrier_lost(4096, 6); 765 printf("TORN_SLOT_BARRIER_LOST_512_4096: %s (device ignores FLUSH ordering; outside the device contract)\n", 766 bl ? "PASS" : "FAIL"); 767 all_ok &= bl; 768 769 printf("checks=%llu failures=%llu\n", checks, failures); 770 printf("TORN_SLOT_HOST_EMULATION: %s (host block-device emulation, not QEMU, not hardware)\n", 771 all_ok && failures == 0 ? "PASS" : "FAIL"); 772 return all_ok && failures == 0 ? 0 : 1; 773 }