interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / store / tests / store_engine_test.c

853 lines · 30851 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 * store_engine_test.c -- port of every engine test in
19 * crates/aienos-kernel/src/store/engine.rs (mount classification, history,
20 * transaction errors, checkpoint order, fail-closed I/O, peer conditions)
21 * plus C-only cases (GraphBadOlder, equal-generation graph damage,
22 * TransactionLimit, read_object, flush failure) and the disk adapter.
23 */
24#include "store_test_util.h"
25#include "disk_file.h"
26
27static st_workspace ws;
28
29static const uint8_t U5A[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a,
30 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a};
31
32static void seed_genesis(memdev *m)
33{
34 static uint8_t g[4][SV1_UNIT];
35 if (sv1_genesis_units(U5A, m->n, g)) {
36 fprintf(stderr, "genesis failed\n");
37 exit(2);
38 }
39 memcpy(m->units[0], g[0], SV1_UNIT);
40 memcpy(m->units[2], g[2], SV1_UNIT);
41 memcpy(m->units[3], g[3], SV1_UNIT);
42}
43
44static st_object one(const char *s)
45{
46 st_object o = {3, 1, (const uint8_t *)s, strlen(s)};
47 return o;
48}
49
50static int open_mem(st_store *s, memdev *m)
51{
52 st_dev d = mem_dev(m);
53 return st_open(s, &d, &ws);
54}
55
56static void seed_generation_two(memdev *m)
57{
58 st_store s;
59 mem_init(m, 32);
60 seed_genesis(m);
61 CHECK_EQ(open_mem(&s, m), 0, "setup open");
62 st_object o = one("next generation");
63 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "seed gen 2");
64 mem_clear_log(m);
65}
66
67static void write_commit_unit(memdev *m, uint64_t unit, const sv1_commit *c)
68{
69 memset(m->units[unit], 0, SV1_UNIT);
70 sv1_commit_encode(c, m->units[unit]);
71}
72
73static void rewrite_commit(memdev *m, uint32_t slot, uint64_t unit, const sv1_commit *c)
74{
75 sv1_superblock sb;
76 write_commit_unit(m, unit, c);
77 sv1_superblock_decode(m->units[slot], SV1_UNIT, slot, &sb);
78 sb.generation = c->generation;
79 sv1_commit_object_id(c, sb.commit_record_id);
80 sv1_superblock_encode(&sb, m->units[slot]);
81}
82
83static void sb_from_commit(const sv1_commit *c, uint32_t slot, uint64_t cu, uint8_t *out)
84{
85 sv1_superblock sb;
86 memset(&sb, 0, sizeof sb);
87 memcpy(sb.store_uuid, c->store_uuid, 16);
88 sb.slot_id = slot;
89 sb.region_units = c->region_units;
90 sb.generation = c->generation;
91 sv1_commit_object_id(c, sb.commit_record_id);
92 sb.commit_record_unit = cu;
93 memcpy(sb.catalog_id, c->catalog_id, 32);
94 sb.catalog_first_unit = c->catalog_first_unit;
95 sb.catalog_byte_length = c->catalog_byte_length;
96 sb.catalog_unit_count = c->catalog_unit_count;
97 sb.catalog_entry_count = c->catalog_entry_count;
98 sb.committed_high_water = c->committed_high_water;
99 sv1_superblock_encode(&sb, out);
100}
101
102static void empty_catalog_at(memdev *m, uint64_t unit, uint8_t id[32])
103{
104 size_t len;
105 memset(m->units[unit], 0, SV1_UNIT);
106 sv1_catalog_encode(NULL, 0, m->units[unit], SV1_UNIT, &len);
107 sv1_object_id(1, 1, m->units[unit], len, id);
108}
109
110static void t_open_side_effect_free(void)
111{
112 memdev m;
113 st_store s;
114 mem_init(&m, 32);
115 seed_genesis(&m);
116 CHECK_EQ(open_mem(&s, &m), 0, "open genesis");
117 CHECK(m.nwrites == 0 && m.flushes == 0, "open writes nothing");
118 int r0 = 0, r1 = 0;
119 for (size_t i = 0; i < m.nreads; i++) {
120 r0 |= m.reads[i] == 0;
121 r1 |= m.reads[i] == 1;
122 }
123 CHECK(r0 && r1, "open reads both slots");
124 mem_free(&m);
125}
126
127static void t_full_graph_validation(void)
128{
129 memdev m;
130 st_store s;
131 mem_init(&m, 32);
132 const char *value = "payload";
133 sv1_entry e;
134 memset(&e, 0, sizeof e);
135 sv1_object_id(3, 1, (const uint8_t *)value, 7, e.object_id);
136 e.kind = 3;
137 e.version = 1;
138 e.first_unit = 2;
139 e.byte_length = 7;
140 e.unit_count = 1;
141 size_t cl;
142 sv1_catalog_encode(&e, 1, m.units[3], SV1_UNIT, &cl);
143 sv1_commit c;
144 memset(&c, 0, sizeof c);
145 memcpy(c.store_uuid, U5A, 16);
146 c.region_units = 32;
147 c.generation = 1;
148 sv1_object_id(1, 1, m.units[3], cl, c.catalog_id);
149 c.catalog_first_unit = 3;
150 c.catalog_byte_length = cl;
151 c.catalog_unit_count = 1;
152 c.catalog_entry_count = 1;
153 c.committed_high_water = 5;
154 memcpy(m.units[2], value, 7);
155 write_commit_unit(&m, 4, &c);
156 sb_from_commit(&c, 0, 4, m.units[0]);
157 CHECK_EQ(open_mem(&s, &m), 0, "one-object genesis mounts");
158 m.units[2][SV1_UNIT - 1] = 1;
159 CHECK_EQ(open_mem(&s, &m), ST_M_CORRUPT_RECOVERY_REQUIRED, "damaged payload padding");
160 m.fail_read = 4;
161 CHECK_EQ(open_mem(&s, &m), ST_M_IO, "I/O stays distinct");
162 mem_free(&m);
163}
164
165static void t_history_matrix(void)
166{
167 memdev m;
168 st_store s;
169 /* conflicting same-generation roots */
170 mem_init(&m, 32);
171 seed_genesis(&m);
172 {
173 uint8_t id[32];
174 empty_catalog_at(&m, 4, id);
175 sv1_commit c;
176 memset(&c, 0, sizeof c);
177 memset(c.store_uuid, 0x6b, 16);
178 c.region_units = 32;
179 c.generation = 1;
180 memcpy(c.catalog_id, id, 32);
181 c.catalog_first_unit = 4;
182 c.catalog_byte_length = 16;
183 c.catalog_unit_count = 1;
184 c.committed_high_water = 6;
185 write_commit_unit(&m, 5, &c);
186 sb_from_commit(&c, 1, 5, m.units[1]);
187 }
188 CHECK_EQ(open_mem(&s, &m), ST_M_CONFLICTING_ROOTS, "conflicting roots");
189 mem_free(&m);
190
191 /* equivalent peer, then a transaction, then reopen */
192 mem_init(&m, 32);
193 seed_genesis(&m);
194 {
195 sv1_superblock sb;
196 sv1_superblock_decode(m.units[0], SV1_UNIT, 0, &sb);
197 sb.slot_id = 1;
198 sv1_superblock_encode(&sb, m.units[1]);
199 }
200 CHECK_EQ(open_mem(&s, &m), 0, "equivalent roots");
201 CHECK(st_generation(&s) == 1 && s.peer == ST_PEER_VALID, "equivalent: gen 1, peer valid");
202 st_object o = one("next");
203 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "transact after equivalent");
204 CHECK_EQ(open_mem(&s, &m), 0, "reopen");
205 CHECK(st_generation(&s) == 2, "reopen gen 2");
206 mem_free(&m);
207
208 /* wrong previous commit id */
209 sv1_commit rec;
210 seed_generation_two(&m);
211 sv1_commit_decode(m.units[6], SV1_COMMIT_BYTES, &rec);
212 memset(rec.previous_commit_id, 0x33, 32);
213 rewrite_commit(&m, 1, 6, &rec);
214 CHECK_EQ(open_mem(&s, &m), ST_M_INCONSISTENT_HISTORY, "wrong previous");
215 mem_free(&m);
216
217 /* non-adjacent generation */
218 seed_generation_two(&m);
219 sv1_commit_decode(m.units[6], SV1_COMMIT_BYTES, &rec);
220 rec.generation = 3;
221 rec.previous_generation = 2;
222 rewrite_commit(&m, 1, 6, &rec);
223 CHECK_EQ(open_mem(&s, &m), ST_M_INCONSISTENT_HISTORY, "non adjacent");
224 mem_free(&m);
225
226 /* damaged new payload: degraded read-only, peer GraphBadNewer */
227 seed_generation_two(&m);
228 m.units[4][0] ^= 1;
229 CHECK_EQ(open_mem(&s, &m), 0, "damaged new mounts");
230 CHECK(s.state == ST_DEGRADED_RECOVERY && s.peer == ST_PEER_GRAPH_BAD_NEWER,
231 "damaged new: degraded, peer GraphBadNewer");
232 o = one("read only");
233 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_READ_ONLY_DEGRADED, "read only degraded");
234 CHECK(m.nwrites == 0, "degraded: no writes");
235 mem_free(&m);
236
237 /* damaged old catalog: valid gen 2, peer GraphBadOlder */
238 seed_generation_two(&m);
239 m.units[2][0] ^= 1;
240 CHECK_EQ(open_mem(&s, &m), 0, "damaged old mounts");
241 CHECK(s.state == ST_VALID && st_generation(&s) == 2 && s.peer == ST_PEER_GRAPH_BAD_OLDER,
242 "damaged old: valid gen 2, peer GraphBadOlder");
243 mem_free(&m);
244
245 /* C only: equal-generation graph-bad peer is not a recovery choice */
246 mem_init(&m, 32);
247 seed_genesis(&m);
248 {
249 sv1_superblock sb;
250 sv1_superblock_decode(m.units[0], SV1_UNIT, 0, &sb);
251 sb.slot_id = 1;
252 sv1_superblock_encode(&sb, m.units[1]);
253 sv1_commit c;
254 sv1_commit_decode(m.units[3], SV1_COMMIT_BYTES, &c);
255 c.committed_high_water = 6;
256 c.catalog_first_unit = 4;
257 uint8_t id[32];
258 empty_catalog_at(&m, 4, id);
259 write_commit_unit(&m, 5, &c);
260 sb_from_commit(&c, 1, 5, m.units[1]);
261 m.units[4][0] ^= 1; /* B's catalog is damaged, same generation as A */
262 }
263 CHECK_EQ(open_mem(&s, &m), ST_M_CORRUPT_RECOVERY_REQUIRED, "equal generation graph-bad peer");
264 mem_free(&m);
265}
266
267static void t_history_deleting_entry(void)
268{
269 memdev m;
270 st_store s;
271 sv1_commit prev, c;
272 uint8_t id[32];
273 seed_generation_two(&m);
274 sv1_commit_decode(m.units[6], SV1_COMMIT_BYTES, &prev);
275 empty_catalog_at(&m, 7, id);
276 memset(&c, 0, sizeof c);
277 memcpy(c.store_uuid, prev.store_uuid, 16);
278 c.region_units = prev.region_units;
279 c.generation = 3;
280 c.previous_generation = 2;
281 sv1_commit_object_id(&prev, c.previous_commit_id);
282 memcpy(c.previous_catalog_id, prev.catalog_id, 32);
283 memcpy(c.catalog_id, id, 32);
284 c.catalog_first_unit = 7;
285 c.catalog_byte_length = 16;
286 c.catalog_unit_count = 1;
287 c.committed_high_water = 9;
288 write_commit_unit(&m, 8, &c);
289 sb_from_commit(&c, 0, 8, m.units[0]);
290 CHECK_EQ(open_mem(&s, &m), ST_M_INCONSISTENT_HISTORY, "deleting an old entry");
291 mem_free(&m);
292
293 /* C only: a gap between old high water and the new objects */
294 seed_generation_two(&m);
295 {
296 st_store t;
297 CHECK_EQ(open_mem(&t, &m), 0, "setup open");
298 /* hand-build gen 3 with the object at unit 8 instead of 7 */
299 const char *v = "gap";
300 sv1_entry e[2];
301 sv1_entry *old = ws.cat[t.root.cat_index];
302 e[0] = old[0];
303 memset(&e[1], 0, sizeof e[1]);
304 sv1_object_id(3, 1, (const uint8_t *)v, 3, e[1].object_id);
305 e[1].kind = 3;
306 e[1].version = 1;
307 e[1].first_unit = 8;
308 e[1].byte_length = 3;
309 e[1].unit_count = 1;
310 sv1_sort_entries(e, 2);
311 memset(m.units[8], 0, SV1_UNIT);
312 memcpy(m.units[8], v, 3);
313 size_t cl;
314 memset(m.units[9], 0, SV1_UNIT);
315 sv1_catalog_encode(e, 2, m.units[9], SV1_UNIT, &cl);
316 sv1_commit_decode(m.units[6], SV1_COMMIT_BYTES, &prev);
317 memset(&c, 0, sizeof c);
318 memcpy(c.store_uuid, prev.store_uuid, 16);
319 c.region_units = 32;
320 c.generation = 3;
321 c.previous_generation = 2;
322 sv1_commit_object_id(&prev, c.previous_commit_id);
323 memcpy(c.previous_catalog_id, prev.catalog_id, 32);
324 sv1_object_id(1, 1, m.units[9], cl, c.catalog_id);
325 c.catalog_first_unit = 9;
326 c.catalog_byte_length = cl;
327 c.catalog_unit_count = 1;
328 c.catalog_entry_count = 2;
329 c.committed_high_water = 11;
330 write_commit_unit(&m, 10, &c);
331 sb_from_commit(&c, 0, 10, m.units[0]);
332 }
333 CHECK_EQ(open_mem(&s, &m), ST_M_INCONSISTENT_HISTORY, "gap before new objects");
334 mem_free(&m);
335}
336
337/* C only: hand-built generation 3 over seed_generation_two (gen 2 high water
338 * 7) with one new 1-unit object at obj_unit and the catalog at cat_unit. */
339static void build_gen3(memdev *m, uint64_t obj_unit, uint64_t cat_unit)
340{
341 st_store t;
342 sv1_commit prev, c;
343 seed_generation_two(m);
344 CHECK_EQ(open_mem(&t, m), 0, "setup open");
345 const char *v = "lay";
346 sv1_entry e[2];
347 e[0] = ws.cat[t.root.cat_index][0];
348 memset(&e[1], 0, sizeof e[1]);
349 sv1_object_id(3, 1, (const uint8_t *)v, 3, e[1].object_id);
350 e[1].kind = 3;
351 e[1].version = 1;
352 e[1].first_unit = obj_unit;
353 e[1].byte_length = 3;
354 e[1].unit_count = 1;
355 sv1_sort_entries(e, 2);
356 memset(m->units[obj_unit], 0, SV1_UNIT);
357 memcpy(m->units[obj_unit], v, 3);
358 size_t cl;
359 memset(m->units[cat_unit], 0, SV1_UNIT);
360 sv1_catalog_encode(e, 2, m->units[cat_unit], SV1_UNIT, &cl);
361 sv1_commit_decode(m->units[6], SV1_COMMIT_BYTES, &prev);
362 memset(&c, 0, sizeof c);
363 memcpy(c.store_uuid, prev.store_uuid, 16);
364 c.region_units = 32;
365 c.generation = 3;
366 c.previous_generation = 2;
367 sv1_commit_object_id(&prev, c.previous_commit_id);
368 memcpy(c.previous_catalog_id, prev.catalog_id, 32);
369 sv1_object_id(1, 1, m->units[cat_unit], cl, c.catalog_id);
370 c.catalog_first_unit = cat_unit;
371 c.catalog_byte_length = cl;
372 c.catalog_unit_count = 1;
373 c.catalog_entry_count = 2;
374 c.committed_high_water = cat_unit + 2;
375 write_commit_unit(m, cat_unit + 1, &c);
376 sb_from_commit(&c, 0, cat_unit + 1, m->units[0]);
377}
378
379static void t_append_layout(void)
380{
381 memdev m;
382 st_store s;
383 build_gen3(&m, 7, 8);
384 CHECK_EQ(open_mem(&s, &m), 0, "hand-built gen 3 (control)");
385 CHECK(st_generation(&s) == 3 && s.state == ST_VALID, "control: valid gen 3");
386 mem_free(&m);
387 build_gen3(&m, 7, 9);
388 CHECK_EQ(open_mem(&s, &m), ST_M_INCONSISTENT_HISTORY, "gap between new objects and catalog");
389 mem_free(&m);
390 build_gen3(&m, 2, 8);
391 CHECK_EQ(open_mem(&s, &m), ST_M_INCONSISTENT_HISTORY, "new object below old high water, hole at 7");
392 mem_free(&m);
393 /* bytes after the commit record in its unit */
394 seed_generation_two(&m);
395 m.units[6][SV1_COMMIT_BYTES + 5] = 1;
396 CHECK_EQ(open_mem(&s, &m), 0, "dirty commit padding mounts the older root");
397 CHECK(s.state == ST_DEGRADED_RECOVERY && s.peer == ST_PEER_GRAPH_BAD_NEWER,
398 "dirty commit padding: degraded, peer GraphBadNewer");
399 mem_free(&m);
400}
401
402static void t_classification(void)
403{
404 memdev m;
405 st_store s;
406 mem_init(&m, 8);
407 CHECK_EQ(open_mem(&s, &m), ST_M_UNFORMATTED, "blank");
408 m.units[0][0] = 0x77;
409 CHECK_EQ(open_mem(&s, &m), ST_M_FOREIGN, "foreign");
410 mem_free(&m);
411 mem_init(&m, 8);
412 seed_genesis(&m);
413 sv1_put16(m.units[0] + 8, 2);
414 rechecksum(m.units[0]);
415 CHECK_EQ(open_mem(&s, &m), ST_M_UNSUPPORTED_VERSION, "unsupported");
416 mem_free(&m);
417 mem_init(&m, 8);
418 seed_genesis(&m);
419 m.units[0][168] ^= 1;
420 CHECK_EQ(open_mem(&s, &m), ST_M_CORRUPT_RECOVERY_REQUIRED, "bad crc");
421 mem_free(&m);
422 /* C only: region outside 4..=2^32 */
423 mem_init(&m, 3);
424 CHECK_EQ(open_mem(&s, &m), ST_M_CORRUPT_RECOVERY_REQUIRED, "region too small");
425 mem_free(&m);
426 /* C only: superblock region differs from the device */
427 mem_init(&m, 16);
428 seed_genesis(&m);
429 m.n = 15;
430 CHECK_EQ(open_mem(&s, &m), ST_M_CORRUPT_RECOVERY_REQUIRED, "region mismatch");
431 m.n = 16;
432 mem_free(&m);
433}
434
435static void t_zero_write_errors(void)
436{
437 memdev m;
438 st_store s;
439 st_object o;
440 mem_init(&m, 6);
441 seed_genesis(&m);
442 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
443 o = one("cannot fit");
444 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_NO_SPACE, "no space");
445 CHECK(m.nwrites == 0 && m.flushes == 0, "no space: zero writes");
446 mem_free(&m);
447
448 /* full catalog */
449 uint64_t region = 4168;
450 mem_init(&m, region);
451 sv1_entry *e = calloc(SV1_MAX_CATALOG_ENTRIES, sizeof *e);
452 for (uint32_t i = 0; i < SV1_MAX_CATALOG_ENTRIES; i++) {
453 uint16_t kind = (uint16_t)(3 + i);
454 uint8_t sem = (uint8_t)kind;
455 sv1_object_id(kind, 1, &sem, 1, e[i].object_id);
456 e[i].kind = kind;
457 e[i].version = 1;
458 e[i].first_unit = 2 + i;
459 e[i].byte_length = 1;
460 e[i].unit_count = 1;
461 m.units[2 + i][0] = sem;
462 }
463 sv1_sort_entries(e, SV1_MAX_CATALOG_ENTRIES);
464 size_t cl;
465 uint64_t cf = 2 + SV1_MAX_CATALOG_ENTRIES;
466 sv1_catalog_encode(e, SV1_MAX_CATALOG_ENTRIES, m.units[cf], 65 * SV1_UNIT, &cl);
467 uint32_t cu;
468 sv1_unit_count(cl, &cu);
469 CHECK_EQ(cu, 65, "full catalog spans 65 units");
470 sv1_commit c;
471 memset(&c, 0, sizeof c);
472 memcpy(c.store_uuid, U5A, 16);
473 c.region_units = region;
474 c.generation = 1;
475 sv1_object_id(1, 1, m.units[cf], cl, c.catalog_id);
476 c.catalog_first_unit = cf;
477 c.catalog_byte_length = cl;
478 c.catalog_unit_count = cu;
479 c.catalog_entry_count = SV1_MAX_CATALOG_ENTRIES;
480 c.committed_high_water = cf + cu + 1;
481 write_commit_unit(&m, cf + cu, &c);
482 sb_from_commit(&c, 0, cf + cu, m.units[0]);
483 free(e);
484 CHECK_EQ(open_mem(&s, &m), 0, "full catalog mounts");
485 mem_clear_log(&m);
486 o = one("new object");
487 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_CATALOG_FULL, "catalog full");
488 CHECK(m.nwrites == 0 && m.flushes == 0, "catalog full: zero writes");
489 /* the same full catalog dedups an existing object without growing */
490 uint8_t sem3 = 3;
491 st_object ex = {3, 1, &sem3, 1};
492 CHECK_EQ(st_transact(&s, &ex, 1, NULL, NULL), ST_E_NO_SPACE, "full region: dedup still needs units");
493 mem_free(&m);
494
495 /* generation exhausted */
496 mem_init(&m, 32);
497 seed_genesis(&m);
498 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
499 mem_clear_log(&m);
500 s.root.commit.generation = UINT64_MAX;
501 s.root.sb.generation = UINT64_MAX;
502 o = one("overflow");
503 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_GENERATION_EXHAUSTED, "generation exhausted");
504 CHECK(m.nwrites == 0 && m.flushes == 0, "exhausted: zero writes");
505 mem_free(&m);
506
507 /* C only: TransactionLimit by count and by units */
508 mem_init(&m, 8);
509 {
510 static uint8_t g[4][SV1_UNIT];
511 sv1_genesis_units(U5A, 40000, g);
512 memcpy(m.units[0], g[0], SV1_UNIT);
513 memcpy(m.units[2], g[2], SV1_UNIT);
514 memcpy(m.units[3], g[3], SV1_UNIT);
515 }
516 st_dev d = mem_dev(&m);
517 d.region_units = 40000;
518 CHECK_EQ(st_open(&s, &d, &ws), 0, "large-region genesis mounts");
519 mem_clear_log(&m);
520 static st_object many[SV1_MAX_TRANSACTION_OBJECTS + 1];
521 static uint8_t bytes[SV1_MAX_TRANSACTION_OBJECTS + 1][2];
522 for (int i = 0; i <= (int)SV1_MAX_TRANSACTION_OBJECTS; i++) {
523 bytes[i][0] = (uint8_t)i;
524 bytes[i][1] = (uint8_t)(i >> 8);
525 many[i].kind = 3;
526 many[i].version = 1;
527 many[i].bytes = bytes[i];
528 many[i].len = 2;
529 }
530 CHECK_EQ(st_transact(&s, many, SV1_MAX_TRANSACTION_OBJECTS + 1, NULL, NULL),
531 ST_E_TRANSACTION_LIMIT, "257 objects");
532 size_t big = (size_t)SV1_MAX_OBJECT_BYTES + 1;
533 uint8_t *buf = malloc(big);
534 for (size_t i = 0; i < big; i++)
535 buf[i] = (uint8_t)(i * 7 + (i >> 12));
536 st_object huge[3] = {{3, 1, buf, SV1_MAX_OBJECT_BYTES},
537 {3, 1, buf + 1, SV1_MAX_OBJECT_BYTES},
538 {3, 1, buf, 1}};
539 CHECK_EQ(st_transact(&s, huge, 3, NULL, NULL), ST_E_TRANSACTION_LIMIT, "32769 units");
540 st_object toolarge = {3, 1, buf, big};
541 CHECK_EQ(st_transact(&s, &toolarge, 1, NULL, NULL), SV1_E_OBJECT_TOO_LARGE, "object too large");
542 st_object empty = {3, 1, buf, 0};
543 CHECK_EQ(st_transact(&s, &empty, 1, NULL, NULL), SV1_E_INVALID_OBJECT, "empty object");
544 st_object kind0 = {0, 1, buf, 1};
545 CHECK_EQ(st_transact(&s, &kind0, 1, NULL, NULL), SV1_E_INVALID_OBJECT, "kind zero");
546 CHECK(m.nwrites == 0 && m.flushes == 0, "limits: zero writes");
547 free(buf);
548 mem_free(&m);
549}
550
551static void t_dedup_and_tail_reuse(void)
552{
553 memdev m;
554 st_store s;
555 mem_init(&m, 32);
556 seed_genesis(&m);
557 memset(m.units[4], 0xa5, SV1_UNIT);
558 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
559 const char *value = "committed payload";
560 st_object o = one(value);
561 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "first commit");
562 CHECK(st_generation(&s) == 2, "gen 2");
563 int w4 = 0;
564 for (size_t i = 0; i < m.nwrites; i++)
565 w4 |= m.writes[i] == 4;
566 CHECK(w4 && m.flushes == 2, "tail unit reused, two flushes");
567 CHECK_EQ(open_mem(&s, &m), 0, "reopen");
568 mem_clear_log(&m);
569 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "dedup commit");
570 CHECK(m.nwrites == 3 && m.writes[0] == 7 && m.writes[1] == 8 && m.writes[2] == 0,
571 "dedup writes catalog, commit record, inactive root only");
572 uint8_t id[32], out[64];
573 size_t len;
574 sv1_object_id(3, 1, (const uint8_t *)value, strlen(value), id);
575 CHECK_EQ(st_read_object(&s, id, out, sizeof out, &len), 0, "read object");
576 CHECK(len == strlen(value) && memcmp(out, value, len) == 0, "read object bytes");
577 id[0] ^= 1;
578 CHECK_EQ(st_read_object(&s, id, out, sizeof out, &len), ST_E_INVALID_OBJECT, "unknown object");
579 id[0] ^= 1;
580 CHECK_EQ(st_read_object(&s, id, out, 4, &len), ST_E_ARG, "small buffer");
581 m.units[4][1] ^= 1;
582 CHECK_EQ(st_read_object(&s, id, out, sizeof out, &len), ST_E_CORRUPT, "read corrupt");
583 m.units[4][1] ^= 1;
584 m.fail_read = 4;
585 CHECK_EQ(st_read_object(&s, id, out, sizeof out, &len), ST_E_IO, "read io");
586 mem_free(&m);
587
588 /* in-request duplicates: identical is folded, different kind conflicts */
589 mem_init(&m, 32);
590 seed_genesis(&m);
591 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
592 st_object two[2] = {one("same"), one("same")};
593 CHECK_EQ(st_transact(&s, two, 2, NULL, NULL), 0, "identical duplicates fold");
594 CHECK(s.root.n == 1, "one entry");
595 /* Rust quirk (see store_engine.c): [new, existing] dedups in C */
596 st_object mixed[2] = {one("brand new"), one("same")};
597 CHECK_EQ(st_transact(&s, mixed, 2, NULL, NULL), 0, "new + existing");
598 CHECK(s.root.n == 2, "existing object not appended twice");
599 CHECK_EQ(open_mem(&s, &m), 0, "reopen after mixed");
600 mem_free(&m);
601}
602
603static int cps[16], ncps;
604static void record_cp(void *arg, int cp)
605{
606 (void)arg;
607 if (ncps < 16)
608 cps[ncps++] = cp;
609}
610
611static void t_checkpoint_order(void)
612{
613 memdev a, b;
614 st_store s;
615 mem_init(&a, 32);
616 seed_genesis(&a);
617 mem_clone(&b, &a);
618 CHECK_EQ(open_mem(&s, &a), 0, "setup open");
619 st_object o = one("checkpointed");
620 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "setup transaction");
621 CHECK_EQ(open_mem(&s, &b), 0, "setup open");
622 mem_clear_log(&a);
623 ncps = 0;
624 CHECK_EQ(st_transact(&s, &o, 1, record_cp, NULL), 0, "hooked transact");
625 int ordered = ncps == 7;
626 for (int i = 0; i < ncps && ordered; i++)
627 ordered = cps[i] == i;
628 CHECK(ordered, "checkpoints in Rust order");
629 CHECK(memcmp(a.units, b.units, 32 * SV1_UNIT) == 0, "hook does not change bytes");
630 for (int i = 0; i < 7; i++)
631 CHECK(strcmp(st_checkpoint_name(i), "unknown") != 0, "checkpoint name %d", i);
632 mem_free(&a);
633 mem_free(&b);
634}
635
636static void t_dedup_corruption_conflict(void)
637{
638 memdev m;
639 st_store s;
640 mem_init(&m, 32);
641 seed_genesis(&m);
642 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
643 st_object o = one("dedup target");
644 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "setup transaction");
645 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
646 mem_clear_log(&m);
647 m.units[4][0] ^= 1;
648 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_CORRUPT, "dedup corruption");
649 CHECK(m.nwrites == 0 && m.flushes == 0, "corrupt: zero writes");
650 m.units[4][0] ^= 1;
651 m.fail_read = 4;
652 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_IO, "dedup read io");
653 m.fail_read = -1;
654 mem_free(&m);
655
656 seed_generation_two(&m);
657 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
658 mem_clear_log(&m);
659 sv1_entry *e = &ws.cat[s.root.cat_index][0];
660 uint8_t id[32];
661 memcpy(id, e->object_id, 32);
662 e->kind++;
663 o = one("next generation");
664 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_DUPLICATE_OBJECT_CONFLICT, "descriptor conflict");
665 CHECK(memcmp(ws.cat[s.root.cat_index][0].object_id, id, 32) == 0, "catalog untouched");
666 CHECK(m.nwrites == 0 && m.flushes == 0, "conflict: zero writes");
667 mem_free(&m);
668
669 /* C only: same id, stored bytes differ (hash collision stand-in) */
670 seed_generation_two(&m);
671 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
672 e = &ws.cat[s.root.cat_index][0];
673 const char *other = "next generatioN";
674 sv1_object_id(3, 1, (const uint8_t *)other, 15, e->object_id);
675 st_object o2 = one(other);
676 mem_clear_log(&m);
677 int r = st_transact(&s, &o2, 1, NULL, NULL);
678 CHECK(r == ST_E_CORRUPT || r == ST_E_DUPLICATE_OBJECT_CONFLICT, "forged id refused (%d)", r);
679 CHECK(m.nwrites == 0, "forged id: zero writes");
680 mem_free(&m);
681}
682
683static void t_io_fail_closed(void)
684{
685 memdev m;
686 st_store s;
687 mem_init(&m, 32);
688 seed_genesis(&m);
689 m.fail_read = 3;
690 CHECK_EQ(open_mem(&s, &m), ST_M_IO, "open io");
691 m.fail_read = -1;
692 CHECK_EQ(open_mem(&s, &m), 0, "open");
693 m.fail_write = 4;
694 st_object o = one("fault");
695 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_IO, "write io");
696 o = one("again");
697 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_NEEDS_REOPEN, "needs reopen");
698 m.fail_write = -1;
699 m.fail_flush = 1;
700 CHECK_EQ(open_mem(&s, &m), 0, "reopen");
701 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_IO, "flush io");
702 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_NEEDS_REOPEN, "needs reopen after flush");
703 m.fail_flush = 0;
704 CHECK_EQ(open_mem(&s, &m), 0, "reopen after failed flush");
705 CHECK(st_generation(&s) == 1, "failed flush before root: still gen 1");
706 mem_free(&m);
707}
708
709static void t_peer_condition(void)
710{
711 memdev m;
712 st_store s;
713 mem_init(&m, 32);
714 seed_genesis(&m);
715 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
716 CHECK(s.peer == ST_PEER_ZERO, "peer zero");
717 st_object o = one("second generation");
718 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "setup transaction");
719 CHECK(s.peer == ST_PEER_VALID, "peer valid after commit");
720 CHECK_EQ(open_mem(&s, &m), 0, "setup open");
721 CHECK(s.state == ST_VALID && s.peer == ST_PEER_VALID, "reopen valid/valid");
722 sv1_superblock nb;
723 sv1_superblock_decode(m.units[1], SV1_UNIT, 1, &nb);
724 CHECK(nb.generation == 2, "newer in slot 1");
725 memset(m.units[nb.commit_record_unit], 0, SV1_UNIT);
726 CHECK_EQ(open_mem(&s, &m), 0, "open with lost newer commit");
727 CHECK(st_generation(&s) == 1 && s.state == ST_DEGRADED_RECOVERY &&
728 s.peer == ST_PEER_GRAPH_BAD_NEWER,
729 "older root, degraded, GraphBadNewer");
730 mem_free(&m);
731}
732
733static void t_malformed_peer_matrix(void)
734{
735 memdev m;
736 st_store s;
737 st_object o;
738 /* 1 bad CRC peer */
739 mem_init(&m, 32);
740 seed_genesis(&m);
741 memcpy(m.units[1], "AIENSTR1", 8);
742 m.units[1][168] ^= 1;
743 CHECK_EQ(open_mem(&s, &m), 0, "bad crc peer");
744 CHECK(s.state == ST_DEGRADED_RECOVERY && s.peer == ST_PEER_MALFORMED && st_generation(&s) == 1,
745 "bad crc peer: degraded/malformed");
746 mem_free(&m);
747 /* 2 wrong magic peer */
748 mem_init(&m, 32);
749 seed_genesis(&m);
750 memcpy(m.units[1], "BADMAGIC", 8);
751 CHECK_EQ(open_mem(&s, &m), 0, "wrong magic peer");
752 CHECK(s.state == ST_DEGRADED_RECOVERY && s.peer == ST_PEER_MALFORMED, "wrong magic: malformed");
753 /* 7 transact on degraded */
754 o = one("blocked mutation");
755 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), ST_E_READ_ONLY_DEGRADED, "degraded transact");
756 mem_free(&m);
757 /* 3 nonzero reserved peer */
758 mem_init(&m, 32);
759 seed_genesis(&m);
760 {
761 sv1_superblock sb;
762 sv1_superblock_decode(m.units[0], SV1_UNIT, 0, &sb);
763 sb.slot_id = 1;
764 sv1_superblock_encode(&sb, m.units[1]);
765 m.units[1][200] = 0xff;
766 rechecksum(m.units[1]);
767 }
768 CHECK_EQ(open_mem(&s, &m), 0, "reserved peer");
769 CHECK(s.state == ST_DEGRADED_RECOVERY && s.peer == ST_PEER_MALFORMED, "reserved: malformed");
770 /* 4 unsupported version peer */
771 sv1_put16(m.units[1] + 8, 2);
772 m.units[1][200] = 0;
773 rechecksum(m.units[1]);
774 CHECK_EQ(open_mem(&s, &m), ST_M_UNSUPPORTED_VERSION, "unsupported peer");
775 mem_free(&m);
776 /* 5 io on peer */
777 mem_init(&m, 32);
778 seed_genesis(&m);
779 m.fail_read = 1;
780 CHECK_EQ(open_mem(&s, &m), ST_M_IO, "peer read io");
781 mem_free(&m);
782 /* 6 two bad roots */
783 mem_init(&m, 32);
784 seed_genesis(&m);
785 m.units[0][168] ^= 1;
786 memcpy(m.units[1], "AIENSTR1", 8);
787 m.units[1][168] ^= 1;
788 CHECK_EQ(open_mem(&s, &m), ST_M_CORRUPT_RECOVERY_REQUIRED, "two bad roots");
789 mem_free(&m);
790}
791
792/* ---- disk adapter over native/disk disk_file ---- */
793static void t_disk_adapter(const char *dir)
794{
795 char path[512];
796 snprintf(path, sizeof path, "%s/adapter.img", dir);
797 uint32_t sizes[2] = {512, 4096};
798 for (int k = 0; k < 2; k++) {
799 disk_file f;
800 disk_dev d;
801 st_disk a;
802 st_dev sd;
803 st_store s;
804 uint32_t bpu = SV1_UNIT / sizes[k];
805 uint64_t base = 2 * bpu, units = 16;
806 CHECK_EQ(disk_file_open(&f, &d, path, sizes[k], base + units * bpu + bpu, 1), 0, "disk open");
807 CHECK_EQ(st_disk_bind(&a, &d, base, units, &sd), 0, "bind");
808 CHECK(a.blocks_per_unit == bpu && sd.region_units == units, "geometry %u", sizes[k]);
809 CHECK_EQ(st_format(&sd, U5A), 0, "format");
810 CHECK_EQ(st_open(&s, &sd, &ws), 0, "open formatted");
811 uint64_t before = f.blocks_written;
812 st_object o = one("on disk");
813 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "disk transact");
814 CHECK(f.blocks_written - before == 4 * (uint64_t)bpu, "whole units only (no RMW): %llu blocks",
815 (unsigned long long)(f.blocks_written - before));
816 CHECK_EQ(st_open(&s, &sd, &ws), 0, "reopen");
817 CHECK(st_generation(&s) == 2, "disk gen 2");
818 /* region 0 = rest of the device */
819 CHECK_EQ(st_disk_bind(&a, &d, base, 0, &sd), 0, "bind rest");
820 CHECK(sd.region_units == units + 1, "rest of device");
821 CHECK_EQ(st_disk_bind(&a, &d, base, units + 2, &sd), ST_E_GEOMETRY, "region past end");
822 if (bpu > 1)
823 CHECK_EQ(st_disk_bind(&a, &d, 1, 4, &sd), ST_E_GEOMETRY, "unaligned base");
824 CHECK_EQ(st_disk_bind(&a, &d, d.block_count, 0, &sd), ST_E_GEOMETRY, "base at end");
825 disk_dev odd = d;
826 odd.block_size = 1024;
827 CHECK_EQ(st_disk_bind(&a, &odd, 0, 0, &sd), ST_E_GEOMETRY, "1024-byte blocks refused");
828 disk_file_close(&f);
829 }
830 remove(path);
831}
832
833int main(int argc, char **argv)
834{
835 const char *dir = argc > 1 ? argv[1] : ".";
836 t_open_side_effect_free();
837 t_full_graph_validation();
838 t_history_matrix();
839 t_history_deleting_entry();
840 t_append_layout();
841 t_classification();
842 t_zero_write_errors();
843 t_dedup_and_tail_reuse();
844 t_checkpoint_order();
845 t_dedup_corruption_conflict();
846 t_io_fail_closed();
847 t_peer_condition();
848 t_malformed_peer_matrix();
849 t_disk_adapter(dir);
850 printf("store_engine_test: %d checks, %d failed\n", g_checks, g_failed);
851 printf("STORE_ENGINE: %s\n", g_failed ? "FAIL" : "PASS");
852 return g_failed ? 1 : 0;
853}