interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / test_omega_tensor_store.c

636 lines · 28147 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 * M20 OMEGA_TENSOR crash-safe storage lifetime tests
19 * (src/tensor/omega_tensor_store.h, docs/tensor/M20_OMEGA_TENSOR.md row
20 * "Crash-safe storage lifetime"). Host only, no device.
21 *
22 * round trip put/get/release across close and reopen, views, F16,
23 * rank 0, NaN canonical form, dedup, orphan sweep, busy
24 * lock, lost journal refused, generation monotonicity
25 * crash fork a child that crashes (_exit(86) via the test-only
26 * hook) at every commit phase of a put and of a release;
27 * the parent recovers and must see exactly the old state
28 * (before the journal rename) or the new state (after it)
29 * torn writes the journal truncated at every byte and with every byte
30 * flipped, the payload truncated at every byte, extended,
31 * and flipped: open must refuse with a typed error; a
32 * payload corrupted after open must be refused by get
33 * crafted journal an independent encoder of the documented format: the
34 * implementation's bytes match it, and records that do not
35 * follow from the state before them are refused
36 * Every refusal returns no data. Expected values come from the test's own
37 * tensors, never from the store.
38 */
39#include <dirent.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sys/stat.h>
46#include <sys/wait.h>
47#include <unistd.h>
48
49#ifndef OMEGA_TENSOR_TEST_HOOKS
50#error "test_omega_tensor_store.c needs -DOMEGA_TENSOR_TEST_HOOKS (set by mk/tensor.mk test build)"
51#endif
52#include "omega_numeric.h"
53#include "omega_tensor.h"
54#include "omega_tensor_store.h"
55#include "sha256.h"
56
57static int g_pass, g_fail;
58#define CHECK(cond, ...) do { if (cond) g_pass++; else { g_fail++; \
59 printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
60
61static OmegaTensorCtx *g;
62static char g_root[256];
63static int g_ndir;
64
65/* ---- files ------------------------------------------------------------- */
66
67static void new_dir(char *out, size_t n) { snprintf(out, n, "%s/s%d", g_root, g_ndir++); }
68
69static void rm_dir(const char *dir) {
70 DIR *d = opendir(dir);
71 if (!d) return;
72 struct dirent *e;
73 char p[1024];
74 while ((e = readdir(d)) != NULL) {
75 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
76 snprintf(p, sizeof p, "%s/%s", dir, e->d_name);
77 unlink(p);
78 }
79 closedir(d);
80 rmdir(dir);
81}
82
83static int read_file(const char *path, uint8_t **buf, size_t *n) {
84 struct stat sb;
85 *buf = NULL;
86 *n = 0;
87 int fd = open(path, O_RDONLY);
88 if (fd < 0) return -1;
89 if (fstat(fd, &sb) != 0) { close(fd); return -1; }
90 *buf = malloc((size_t)sb.st_size + 1);
91 size_t got = 0;
92 while (*buf && got < (size_t)sb.st_size) {
93 ssize_t r = read(fd, *buf + got, (size_t)sb.st_size - got);
94 if (r <= 0) break;
95 got += (size_t)r;
96 }
97 close(fd);
98 *n = got;
99 return (*buf && got == (size_t)sb.st_size) ? 0 : -1;
100}
101
102static int write_file(const char *path, const uint8_t *buf, size_t n) {
103 int fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
104 if (fd < 0) return -1;
105 size_t off = 0;
106 while (off < n) {
107 ssize_t w = write(fd, buf + off, n - off);
108 if (w <= 0) { close(fd); return -1; }
109 off += (size_t)w;
110 }
111 close(fd);
112 return 0;
113}
114
115static bool exists(const char *dir, const char *name) {
116 char p[1024];
117 snprintf(p, sizeof p, "%s/%s", dir, name);
118 return access(p, F_OK) == 0;
119}
120
121static void payload_name(const uint8_t id[32], char out[69]) {
122 for (int i = 0; i < 32; i++) snprintf(out + 2 * i, 3, "%02x", id[i]);
123 snprintf(out + 64, 5, ".bin");
124}
125
126/* ---- tensors ----------------------------------------------------------- */
127
128static OmegaTensor mk_bits(uint32_t rank, const uint64_t *shape, const uint32_t *bits) {
129 uint64_t n = 1;
130 for (uint32_t d = 0; d < rank; d++) n *= shape[d];
131 float *f = malloc(n * sizeof(float));
132 memcpy(f, bits, n * sizeof(float));
133 OmegaTensor t = {0, 0};
134 int rc = omega_tensor_from_f32(g, rank, shape, f, &t);
135 CHECK(rc == OMEGA_TENSOR_OK, "setup: from_f32 rc %d", rc);
136 free(f);
137 return t;
138}
139
140static OmegaTensor mk_vals(uint32_t rank, const uint64_t *shape, uint32_t seed) {
141 uint32_t bits[64];
142 uint64_t n = 1;
143 for (uint32_t d = 0; d < rank; d++) n *= shape[d];
144 for (uint64_t i = 0; i < n && i < 64; i++) bits[i] = 0x3f800000U + seed * 0x10000U + (uint32_t)i * 0x100U;
145 return mk_bits(rank, shape, bits);
146}
147
148/* Loads h and compares with expect (value equality and exact bytes). */
149static bool got_equals(OmegaTensorStore *st, OmegaStorageHandle h, OmegaTensor expect) {
150 OmegaTensor t;
151 if (omega_tensor_store_get(st, h, g, &t) != OMEGA_TENSOR_OK) return false;
152 bool eq = false;
153 int rc = omega_tensor_value_equal(g, t, expect, &eq);
154 omega_tensor_release(g, t);
155 return rc == OMEGA_TENSOR_OK && eq;
156}
157
158static void release_t(OmegaTensor t) { omega_tensor_release(g, t); }
159
160/* ---- round trip -------------------------------------------------------- */
161
162static void test_round_trip(void) {
163 char dir[512];
164 new_dir(dir, sizeof dir);
165 OmegaTensorStore *st = NULL, *st2 = NULL;
166 CHECK(omega_tensor_store_open(NULL, 4, &st) == OMEGA_TENSOR_ERR_BAD_ARGS, "open NULL dir refused");
167 CHECK(omega_tensor_store_open(dir, 0, &st) == OMEGA_TENSOR_ERR_BAD_ARGS, "open capacity 0 refused");
168 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK, "open fresh store");
169 if (!st) return;
170 CHECK(omega_tensor_store_open(dir, 8, &st2) == OMEGA_TSTORE_ERR_BUSY && !st2, "second opener refused (busy)");
171
172 uint64_t s23[2] = {2, 3}, s5[1] = {5};
173 uint32_t abits[6] = {0x3f800000U, 0x80000000U, 0x7fa00001U, 0xff800000U, 0x00000001U, 0x40490fdbU};
174 OmegaTensor a = mk_bits(2, s23, abits);
175 uint16_t hbits[5] = {0x3c00U, 0x8000U, 0x7d01U, 0xfc00U, 0x0001U};
176 OmegaTensor b;
177 CHECK(omega_tensor_from_data(g, OMEGA_DT_F16, 1, s5, hbits, &b) == OMEGA_TENSOR_OK, "F16 tensor");
178 uint32_t cbits[1] = {0xc0000000U};
179 OmegaTensor c = mk_bits(0, NULL, cbits);
180 OmegaTensor at;
181 CHECK(omega_tensor_transpose(g, a, &at) == OMEGA_TENSOR_OK, "transpose view");
182
183 OmegaStorageHandle ha, hb, hc, hat;
184 CHECK(omega_tensor_store_put(st, g, a, &ha) == OMEGA_TENSOR_OK, "put F32 [2,3]");
185 CHECK(omega_tensor_store_put(st, g, b, &hb) == OMEGA_TENSOR_OK, "put F16 [5]");
186 CHECK(omega_tensor_store_put(st, g, c, &hc) == OMEGA_TENSOR_OK, "put rank 0");
187 CHECK(omega_tensor_store_put(st, g, at, &hat) == OMEGA_TENSOR_OK, "put transposed view");
188 CHECK(ha.slot == 0 && ha.generation == 1 && hat.slot == 3 && hat.generation == 1,
189 "first handles are slot 0..3 at generation 1");
190 CHECK(got_equals(st, ha, a) && got_equals(st, hb, b) && got_equals(st, hc, c) && got_equals(st, hat, at),
191 "every stored value reads back equal");
192
193 /* NaN payload is not semantic: stored as the canonical quiet NaN. */
194 OmegaTensor ga;
195 float out[6];
196 if (omega_tensor_store_get(st, ha, g, &ga) == OMEGA_TENSOR_OK) {
197 uint32_t ob[6];
198 omega_tensor_read_f32(g, ga, out, 6);
199 memcpy(ob, out, sizeof ob);
200 CHECK(ob[2] == 0x7fc00000U && ob[1] == 0x80000000U && ob[4] == 0x00000001U,
201 "NaN canonical, -0 and subnormal kept (got %08x %08x %08x)", ob[2], ob[1], ob[4]);
202 release_t(ga);
203 } else CHECK(0, "get a");
204 uint8_t vid[32], svid[32];
205 omega_tensor_value_id(g, a, vid);
206 CHECK(omega_tensor_store_value_id(st, ha, svid) == OMEGA_TENSOR_OK && !memcmp(vid, svid, 32),
207 "store value id equals the tensor value id");
208 char pname[69];
209 payload_name(vid, pname);
210 CHECK(exists(dir, pname), "payload file is named by the value id");
211
212 omega_tensor_store_close(st);
213 st = NULL;
214 CHECK(omega_tensor_store_open(dir, 9, &st) == OMEGA_TENSOR_ERR_BAD_ARGS && !st, "reopen with other capacity refused");
215 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK, "reopen");
216 if (!st) return;
217 CHECK(omega_tensor_store_live_count(st) == 4, "4 live after reopen");
218 CHECK(got_equals(st, ha, a) && got_equals(st, hb, b) && got_equals(st, hc, c) && got_equals(st, hat, at),
219 "every value reads back equal after reopen");
220
221 /* release: stale now and after recovery; generation never returns */
222 CHECK(omega_tensor_store_release(st, ha) == OMEGA_TENSOR_OK, "release a");
223 OmegaTensor tmp;
224 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE, "get after release: stale");
225 CHECK(omega_tensor_store_release(st, ha) == OMEGA_TENSOR_ERR_STALE, "double release: stale");
226 CHECK(!omega_tensor_store_valid(st, ha), "released handle invalid");
227 CHECK(!exists(dir, pname), "unshared payload removed on release");
228 OmegaStorageHandle wrong = hb;
229 wrong.generation++;
230 CHECK(omega_tensor_store_get(st, wrong, g, &tmp) == OMEGA_TENSOR_ERR_STALE, "wrong generation: stale");
231 omega_tensor_store_close(st);
232 st = NULL;
233 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK, "reopen after release");
234 if (!st) return;
235 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE, "released handle stale after recovery");
236 OmegaTensor d = mk_vals(1, s5, 7);
237 OmegaStorageHandle hd;
238 CHECK(omega_tensor_store_put(st, g, d, &hd) == OMEGA_TENSOR_OK, "put d");
239 CHECK(hd.slot == ha.slot && hd.generation == ha.generation + 1,
240 "slot reuse gets the next generation (slot %u gen %llu)", hd.slot, (unsigned long long)hd.generation);
241 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE, "old handle stale after slot reuse");
242 CHECK(got_equals(st, hd, d), "d reads back");
243
244 /* dedup: two slots, one payload; releasing one keeps the other */
245 OmegaStorageHandle hd2;
246 CHECK(omega_tensor_store_put(st, g, d, &hd2) == OMEGA_TENSOR_OK && hd2.slot != hd.slot, "put d twice");
247 CHECK(omega_tensor_store_release(st, hd) == OMEGA_TENSOR_OK, "release first copy");
248 CHECK(got_equals(st, hd2, d), "shared payload kept for the other copy");
249
250 /* capacity */
251 OmegaStorageHandle hx;
252 int n = 0;
253 while (omega_tensor_store_put(st, g, d, &hx) == OMEGA_TENSOR_OK && n < 20) n++;
254 CHECK(n > 0 && omega_tensor_store_live_count(st) == 8,
255 "store fills to capacity then refuses");
256 CHECK(omega_tensor_store_put(st, g, d, &hx) == OMEGA_TENSOR_ERR_CAPACITY, "full store: capacity");
257 omega_tensor_store_close(st);
258 st = NULL;
259
260 /* orphan sweep and leftover temp files */
261 char orphan[69];
262 uint8_t zid[32];
263 memset(zid, 0xab, 32);
264 payload_name(zid, orphan);
265 char p[1024];
266 snprintf(p, sizeof p, "%s/%s", dir, orphan);
267 write_file(p, (const uint8_t *)"x", 1);
268 snprintf(p, sizeof p, "%s/payload.tmp", dir);
269 write_file(p, (const uint8_t *)"junk", 4);
270 snprintf(p, sizeof p, "%s/journal.tmp", dir);
271 write_file(p, (const uint8_t *)"junk", 4);
272 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK, "open with leftovers");
273 CHECK(!exists(dir, orphan) && !exists(dir, "payload.tmp") && !exists(dir, "journal.tmp"),
274 "orphan payload and temp files removed");
275 CHECK(got_equals(st, hd2, d) && got_equals(st, hb, b), "values intact after sweep");
276 omega_tensor_store_close(st);
277 st = NULL;
278
279 /* lost journal: refused, never a silent fresh store */
280 char jp[1024], jp2[1024];
281 snprintf(jp, sizeof jp, "%s/journal", dir);
282 snprintf(jp2, sizeof jp2, "%s.moved", dir);
283 CHECK(rename(jp, jp2) == 0, "move journal away");
284 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TSTORE_ERR_TORN && !st, "missing journal with payloads refused");
285 CHECK(rename(jp2, jp) == 0, "restore journal");
286 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK, "open after restore");
287 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE && got_equals(st, hd2, d),
288 "state intact after restore");
289 omega_tensor_store_close(st);
290 release_t(at);
291 release_t(a);
292 release_t(b);
293 release_t(c);
294 release_t(d);
295 rm_dir(dir);
296}
297
298/* ---- crash at every phase ---------------------------------------------- */
299
300static int run_child_crash(const char *dir, int step, int release, OmegaStorageHandle h, OmegaTensor v) {
301 fflush(stdout);
302 pid_t pid = fork();
303 if (pid < 0) return -1;
304 if (pid == 0) {
305 OmegaTensorStore *st = NULL;
306 if (omega_tensor_store_open(dir, 8, &st) != OMEGA_TENSOR_OK) _exit(3);
307 if (omega_tensor_store_test_set_crash_step(st, step) != OMEGA_TENSOR_OK) _exit(4);
308 OmegaStorageHandle out;
309 int rc = release ? omega_tensor_store_release(st, h) : omega_tensor_store_put(st, g, v, &out);
310 _exit(rc == OMEGA_TENSOR_OK ? 0 : 5);
311 }
312 int status = 0;
313 if (waitpid(pid, &status, 0) != pid) return -1;
314 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
315}
316
317/* variant 0: put a new value; 1: put a value equal to a live one (shared
318 * payload); 2: release. */
319static void crash_case(int variant, int step) {
320 char dir[512];
321 new_dir(dir, sizeof dir);
322 uint64_t s3[1] = {3};
323 OmegaTensor a = mk_vals(1, s3, 1), b = mk_vals(1, s3, 2), c = mk_vals(1, s3, 3);
324 OmegaStorageHandle ha, hb;
325 OmegaTensorStore *st = NULL;
326 if (omega_tensor_store_open(dir, 8, &st) != OMEGA_TENSOR_OK) { CHECK(0, "crash setup open"); return; }
327 omega_tensor_store_put(st, g, a, &ha);
328 omega_tensor_store_put(st, g, b, &hb);
329 if (variant != 2) omega_tensor_store_release(st, ha); /* slot 0 free at generation 2 */
330 omega_tensor_store_close(st);
331 st = NULL;
332
333 OmegaTensor v = variant == 1 ? b : c;
334 int ex = run_child_crash(dir, step, variant == 2, ha, v);
335 CHECK(ex == 86, "variant %d step %d: child crashed at the hook (exit %d)", variant, step, ex);
336 int rc = omega_tensor_store_open(dir, 8, &st);
337 CHECK(rc == OMEGA_TENSOR_OK, "variant %d step %d: recovery opens (rc %d)", variant, step, rc);
338 if (!st) { release_t(a); release_t(b); release_t(c); rm_dir(dir); return; }
339 bool committed = step >= OMEGA_TSTORE_CRASH_JOURNAL_RENAMED;
340 CHECK(got_equals(st, hb, b), "variant %d step %d: untouched value intact", variant, step);
341 CHECK(!exists(dir, "payload.tmp") && !exists(dir, "journal.tmp"), "variant %d step %d: temp files swept",
342 variant, step);
343 OmegaTensor tmp;
344 if (variant == 2) {
345 if (committed) {
346 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE &&
347 omega_tensor_store_live_count(st) == 1, "release step %d: committed, handle stale", step);
348 } else {
349 CHECK(got_equals(st, ha, a) && omega_tensor_store_live_count(st) == 2,
350 "release step %d: not committed, value intact", step);
351 }
352 } else {
353 OmegaStorageHandle hv = {0, 2};
354 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE, "put step %d: released a stays stale", step);
355 if (committed) {
356 CHECK(got_equals(st, hv, v) && omega_tensor_store_live_count(st) == 2,
357 "put v%d step %d: committed, new value at slot 0 gen 2", variant, step);
358 } else {
359 CHECK(!omega_tensor_store_valid(st, hv) && omega_tensor_store_live_count(st) == 1,
360 "put v%d step %d: not committed, nothing new", variant, step);
361 }
362 }
363 /* the recovered store keeps working, and its next handle never reuses a
364 * released generation */
365 OmegaStorageHandle hn;
366 CHECK(omega_tensor_store_put(st, g, c, &hn) == OMEGA_TENSOR_OK && got_equals(st, hn, c),
367 "variant %d step %d: put after recovery", variant, step);
368 CHECK(!(hn.slot == ha.slot && hn.generation <= ha.generation), "variant %d step %d: no generation reuse",
369 variant, step);
370 omega_tensor_store_close(st);
371 st = NULL;
372 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK && got_equals(st, hn, c) && got_equals(st, hb, b),
373 "variant %d step %d: second recovery stable", variant, step);
374 omega_tensor_store_close(st);
375 release_t(a);
376 release_t(b);
377 release_t(c);
378 rm_dir(dir);
379}
380
381static void test_crash(void) {
382 for (int step = OMEGA_TSTORE_CRASH_PAYLOAD_PARTIAL; step < OMEGA_TSTORE_CRASH_COUNT; step++) {
383 crash_case(0, step);
384 crash_case(1, step);
385 }
386 for (int step = OMEGA_TSTORE_CRASH_JOURNAL_PARTIAL; step < OMEGA_TSTORE_CRASH_COUNT; step++)
387 crash_case(2, step);
388}
389
390/* ---- torn writes ------------------------------------------------------- */
391
392/* Store with history: put a, put b, release a. Returns the handles. */
393static void torn_setup(const char *dir, OmegaTensor a, OmegaTensor b, OmegaStorageHandle *ha,
394 OmegaStorageHandle *hb) {
395 OmegaTensorStore *st = NULL;
396 if (omega_tensor_store_open(dir, 8, &st) != OMEGA_TENSOR_OK) { CHECK(0, "torn setup"); return; }
397 omega_tensor_store_put(st, g, a, ha);
398 omega_tensor_store_put(st, g, b, hb);
399 omega_tensor_store_release(st, *ha);
400 omega_tensor_store_close(st);
401}
402
403static void test_torn(void) {
404 char dir[512], jp[1024], pp[1024];
405 new_dir(dir, sizeof dir);
406 uint64_t s3[1] = {3};
407 OmegaTensor a = mk_vals(1, s3, 4), b = mk_vals(1, s3, 5);
408 OmegaStorageHandle ha = {0, 0}, hb = {0, 0};
409 torn_setup(dir, a, b, &ha, &hb);
410 snprintf(jp, sizeof jp, "%s/journal", dir);
411 uint8_t vid[32];
412 char pname[69];
413 omega_tensor_value_id(g, b, vid);
414 payload_name(vid, pname);
415 snprintf(pp, sizeof pp, "%s/%s", dir, pname);
416 uint8_t *jr = NULL, *pr = NULL;
417 size_t jn = 0, pn = 0;
418 CHECK(read_file(jp, &jr, &jn) == 0 && jn == 56 + 3 * 200, "journal is header + 3 records (%zu bytes)", jn);
419 CHECK(read_file(pp, &pr, &pn) == 0 && pn == 12, "payload is 12 bytes (%zu)", pn);
420 if (!jr || !pr || jn != 656 || pn != 12) { free(jr); free(pr); return; }
421 OmegaTensorStore *st = NULL;
422 uint32_t lt0, ls0, lt1, ls1;
423 omega_tensor_live_counts(g, &lt0, &ls0);
424
425 /* journal truncated at every byte boundary: refuse */
426 int bad = 0;
427 for (size_t L = 0; L < jn; L++) {
428 write_file(jp, jr, L);
429 int rc = omega_tensor_store_open(dir, 8, &st);
430 if (rc != OMEGA_TSTORE_ERR_TORN || st) {
431 if (bad++ < 3) printf(" journal cut at %zu: rc %d\n", L, rc);
432 omega_tensor_store_close(st);
433 st = NULL;
434 }
435 }
436 CHECK(bad == 0, "journal truncated at all %zu lengths: all refused TORN (%d not)", jn, bad);
437 /* journal extended by one byte and by one full zero record: refuse */
438 uint8_t *big = calloc(1, jn + 200);
439 memcpy(big, jr, jn);
440 write_file(jp, big, jn + 1);
441 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TSTORE_ERR_TORN && !st, "journal + 1 byte refused");
442 write_file(jp, big, jn + 200);
443 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TSTORE_ERR_TORN && !st, "journal + zero record refused");
444 free(big);
445
446 /* every byte of the journal flipped: refuse */
447 bad = 0;
448 for (size_t i = 0; i < jn; i++) {
449 jr[i] ^= (uint8_t)(1u << (i % 8));
450 write_file(jp, jr, jn);
451 jr[i] ^= (uint8_t)(1u << (i % 8));
452 int rc = omega_tensor_store_open(dir, 8, &st);
453 if ((rc != OMEGA_TSTORE_ERR_TORN && rc != OMEGA_TSTORE_ERR_CORRUPT) || st) {
454 if (bad++ < 3) printf(" journal byte %zu flipped: rc %d\n", i, rc);
455 omega_tensor_store_close(st);
456 st = NULL;
457 }
458 }
459 CHECK(bad == 0, "journal bit flip at each of %zu bytes: all refused (%d not)", jn, bad);
460 write_file(jp, jr, jn);
461
462 /* payload truncated at every byte, extended, flipped: refuse */
463 bad = 0;
464 for (size_t L = 0; L < pn; L++) {
465 write_file(pp, pr, L);
466 int rc = omega_tensor_store_open(dir, 8, &st);
467 if (rc != OMEGA_TSTORE_ERR_CORRUPT || st) {
468 if (bad++ < 3) printf(" payload cut at %zu: rc %d\n", L, rc);
469 omega_tensor_store_close(st);
470 st = NULL;
471 }
472 }
473 CHECK(bad == 0, "payload truncated at all %zu lengths: all refused CORRUPT (%d not)", pn, bad);
474 uint8_t ext[13];
475 memcpy(ext, pr, 12);
476 ext[12] = 0;
477 write_file(pp, ext, 13);
478 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TSTORE_ERR_CORRUPT && !st, "payload + 1 byte refused");
479 bad = 0;
480 for (size_t i = 0; i < pn * 8; i++) {
481 pr[i / 8] ^= (uint8_t)(1u << (i % 8));
482 write_file(pp, pr, pn);
483 pr[i / 8] ^= (uint8_t)(1u << (i % 8));
484 int rc = omega_tensor_store_open(dir, 8, &st);
485 if (rc != OMEGA_TSTORE_ERR_CORRUPT || st) {
486 if (bad++ < 3) printf(" payload bit %zu flipped: rc %d\n", i, rc);
487 omega_tensor_store_close(st);
488 st = NULL;
489 }
490 }
491 CHECK(bad == 0, "payload: each of %zu bits flipped refused (%d not)", pn * 8, bad);
492 unlink(pp);
493 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TSTORE_ERR_CORRUPT && !st, "missing payload refused");
494 write_file(pp, pr, pn);
495
496 /* restored files: the committed state comes back exactly */
497 CHECK(omega_tensor_store_open(dir, 8, &st) == OMEGA_TENSOR_OK, "restored store opens");
498 if (st) {
499 OmegaTensor tmp;
500 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE && got_equals(st, hb, b),
501 "restored: released stays stale, live value intact");
502 /* corruption after open: get re-verifies and returns nothing */
503 pr[5] ^= 0x10;
504 write_file(pp, pr, pn);
505 pr[5] ^= 0x10;
506 omega_tensor_live_counts(g, &lt1, &ls1);
507 tmp.slot = 0xffffffffU;
508 CHECK(omega_tensor_store_get(st, hb, g, &tmp) == OMEGA_TSTORE_ERR_CORRUPT, "payload corrupted after open: get refuses");
509 uint32_t lt2, ls2;
510 omega_tensor_live_counts(g, &lt2, &ls2);
511 CHECK(lt2 == lt1 && ls2 == ls1 && tmp.slot == 0xffffffffU, "refused get created no tensor");
512 omega_tensor_store_close(st);
513 }
514 omega_tensor_live_counts(g, &lt1, &ls1);
515 CHECK(lt1 == lt0 && ls1 == ls0, "torn checks leaked no tensors");
516 free(jr);
517 free(pr);
518 release_t(a);
519 release_t(b);
520 rm_dir(dir);
521}
522
523/* ---- crafted journals (independent encoder of the documented format) ---- */
524
525static void le32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
526static void le64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
527
528static void craft_header(uint8_t *h, uint32_t cap, uint64_t count) {
529 memset(h, 0, 56);
530 memcpy(h, "OTSJRN1", 8);
531 le32(h + 8, 1);
532 le32(h + 12, cap);
533 le64(h + 16, count);
534 sha256_hash(h, 24, h + 24);
535}
536
537static void craft_record(uint8_t *r, uint32_t kind, uint32_t slot, uint64_t gen, uint64_t seq,
538 uint32_t dtype, uint32_t rank, const uint64_t *shape, const uint8_t *vid,
539 const uint8_t *psha) {
540 memset(r, 0, 200);
541 memcpy(r, "OTSREC1", 8);
542 le32(r + 8, kind);
543 le32(r + 12, slot);
544 le64(r + 16, gen);
545 le64(r + 24, seq);
546 le32(r + 32, dtype);
547 le32(r + 36, rank);
548 for (uint32_t d = 0; d < rank; d++) le64(r + 40 + 8 * d, shape[d]);
549 if (vid) memcpy(r + 104, vid, 32);
550 if (psha) memcpy(r + 136, psha, 32);
551 sha256_hash(r, 168, r + 168);
552}
553
554static void test_crafted(void) {
555 char dir[512], jp[1024];
556 new_dir(dir, sizeof dir);
557 snprintf(jp, sizeof jp, "%s/journal", dir);
558 uint64_t s3[1] = {3};
559 uint32_t bits[3] = {0x3f800000U, 0x7fa00001U, 0x80000000U};
560 OmegaTensor a = mk_bits(1, s3, bits);
561 OmegaStorageHandle ha;
562 OmegaTensorStore *st = NULL;
563 if (omega_tensor_store_open(dir, 4, &st) != OMEGA_TENSOR_OK) { CHECK(0, "crafted open"); return; }
564 CHECK(omega_tensor_store_put(st, g, a, &ha) == OMEGA_TENSOR_OK, "crafted put");
565 omega_tensor_store_close(st);
566 st = NULL;
567
568 /* expected bytes from the test's own encoder */
569 uint8_t vid[32], psha[32], canon[12];
570 omega_tensor_value_id(g, a, vid);
571 uint32_t cb[3] = {0x3f800000U, 0x7fc00000U, 0x80000000U};
572 for (int i = 0; i < 3; i++) le32(canon + 4 * i, cb[i]);
573 sha256_hash(canon, 12, psha);
574 uint8_t want[56 + 200 * 2];
575 craft_header(want, 4, 1);
576 craft_record(want + 56, 1, 0, 1, 0, OMEGA_DT_F32, 1, s3, vid, psha);
577 uint8_t *jr = NULL;
578 size_t jn = 0;
579 CHECK(read_file(jp, &jr, &jn) == 0 && jn == 256 && !memcmp(jr, want, 256),
580 "journal bytes match the documented format exactly");
581 free(jr);
582
583 struct { const char *what; uint32_t kind, slot; uint64_t gen, seq; int expect; } cases[] = {
584 {"release of a free slot", 2, 1, 1, 1, OMEGA_TSTORE_ERR_TORN},
585 {"release with wrong generation", 2, 0, 2, 1, OMEGA_TSTORE_ERR_TORN},
586 {"record out of sequence", 2, 0, 1, 5, OMEGA_TSTORE_ERR_TORN},
587 {"put on a live slot", 1, 0, 1, 1, OMEGA_TSTORE_ERR_TORN},
588 {"slot beyond capacity", 2, 4, 1, 1, OMEGA_TSTORE_ERR_TORN},
589 {"unknown record kind", 3, 0, 1, 1, OMEGA_TSTORE_ERR_TORN},
590 {"valid release (control)", 2, 0, 1, 1, OMEGA_TENSOR_OK},
591 };
592 for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
593 craft_header(want, 4, 2);
594 craft_record(want + 256, cases[i].kind, cases[i].slot, cases[i].gen, cases[i].seq,
595 OMEGA_DT_F32, 1, s3, vid, psha);
596 write_file(jp, want, sizeof want);
597 int rc = omega_tensor_store_open(dir, 4, &st);
598 CHECK(rc == cases[i].expect, "crafted %s: rc %d, want %d", cases[i].what, rc, cases[i].expect);
599 if (st) {
600 OmegaTensor tmp;
601 CHECK(omega_tensor_store_get(st, ha, g, &tmp) == OMEGA_TENSOR_ERR_STALE, "crafted release took effect");
602 omega_tensor_store_close(st);
603 st = NULL;
604 }
605 }
606 /* a bad shape inside a correctly checksummed put record */
607 uint64_t bad_shape[1] = {0};
608 craft_header(want, 4, 1);
609 craft_record(want + 56, 1, 0, 1, 0, OMEGA_DT_F32, 1, bad_shape, vid, psha);
610 write_file(jp, want, 256);
611 CHECK(omega_tensor_store_open(dir, 4, &st) == OMEGA_TSTORE_ERR_TORN && !st, "crafted zero dimension refused");
612 craft_record(want + 56, 1, 0, 1, 0, 9, 1, s3, vid, psha);
613 write_file(jp, want, 256);
614 CHECK(omega_tensor_store_open(dir, 4, &st) == OMEGA_TSTORE_ERR_TORN && !st, "crafted unknown dtype refused");
615 release_t(a);
616 rm_dir(dir);
617}
618
619int main(void) {
620 if (!omega_numeric_fpenv_ok()) { printf("FPCR not RNE/no-FTZ: refusing to run\n"); return 2; }
621 if (omega_tensor_ctx_create(256, omega_tensor_cpu_realization(), &g)) { printf("ctx\n"); return 2; }
622 snprintf(g_root, sizeof g_root, "%s/tensor-store.XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
623 if (!mkdtemp(g_root)) { printf("mkdtemp failed\n"); return 2; }
624 test_round_trip();
625 test_crash();
626 test_torn();
627 test_crafted();
628 uint32_t lt, ls;
629 omega_tensor_live_counts(g, &lt, &ls);
630 CHECK(lt == 0 && ls == 0, "no leaked tensors (%u) or storage (%u)", lt, ls);
631 omega_tensor_ctx_destroy(g);
632 rmdir(g_root);
633 printf("M20 OMEGA_TENSOR store tests: %d pass, %d fail\n", g_pass, g_fail);
634 printf("M20 verdict: NOT QUALIFIED (crash-safe storage lifetime host tier only)\n");
635 return g_fail ? 1 : 0;
636}