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 / runtime / rx_cortex_journal.c

220 lines · 9612 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 * rx_cortex_journal.c -- the journal backend seam (native v1 P06 cut 1).
19 *
20 * Links the CX_NO_POSIX build of rx_cortex.c (no file, lock or libc
21 * allocation symbols; see the nm check in the Makefile) against a memory
22 * backend and proves the contract a backend is held to: one writer, every
23 * record either wholly in the journal or not at all (memory never runs
24 * ahead), replay and chain identical on reopen, a torn tail refused or
25 * repaired only on request, a changed byte never repaired.
26 */
27#include "runtime/rx_cortex.h"
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33static int g_fail, g_checks;
34#define CHECK(cond, ...) do { \
35 g_checks++; \
36 if (!(cond)) { \
37 g_fail++; \
38 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
39 fprintf(stderr, __VA_ARGS__); \
40 fputc('\n', stderr); \
41 } \
42 } while (0)
43
44/* The allocation hooks named by cx_config_freestanding.h. */
45static long g_live;
46void *cx_fs_alloc(size_t n) { g_live++; return malloc(n); }
47void *cx_fs_realloc(void *p, size_t n) { if (!p) g_live++; return realloc(p, n); }
48void cx_fs_free(void *p) { if (p) g_live--; free(p); }
49
50/* A memory journal: one byte buffer, a writer flag, fault switches. */
51typedef struct {
52 uint8_t *b;
53 size_t len, cap;
54 int writer; /* 1 while a writable handle is open */
55 int fail_append; /* next append fails after a partial copy (undone) */
56 int fail_truncate;
57 int opens, appends, truncates, syncs;
58} MemJournal;
59
60static int mj_open(void *ctx, const char *name, int ro, void **h) {
61 MemJournal *j = ctx;
62 (void)name;
63 j->opens++;
64 if (!ro) {
65 if (j->writer) return CX_ERR_WRITER;
66 j->writer = 1;
67 }
68 *h = ro ? (void *)&j->len : (void *)j; /* distinct handles, same journal */
69 return CX_OK;
70}
71static int mj_size(void *ctx, void *h, size_t *len) { (void)h; *len = ((MemJournal *)ctx)->len; return CX_OK; }
72static int mj_read(void *ctx, void *h, size_t off, uint8_t *out, size_t n) {
73 MemJournal *j = ctx;
74 (void)h;
75 if (off + n > j->len) return CX_ERR_IO;
76 memcpy(out, j->b + off, n);
77 return CX_OK;
78}
79static int mj_append(void *ctx, void *h, const uint8_t *b, size_t n, int sync) {
80 MemJournal *j = ctx;
81 if (h != (void *)j) return CX_ERR_IO; /* read-only handle */
82 j->appends++;
83 if (j->len + n > j->cap) {
84 size_t c = j->cap ? j->cap * 2 : 4096;
85 while (c < j->len + n) c *= 2;
86 uint8_t *q = realloc(j->b, c);
87 if (!q) return CX_ERR_IO;
88 j->b = q;
89 j->cap = c;
90 }
91 if (j->fail_append) {
92 /* A partial write happened; the backend undoes it before returning. */
93 memcpy(j->b + j->len, b, n / 2);
94 memset(j->b + j->len, 0xee, n / 2);
95 j->fail_append = 0;
96 return CX_ERR_IO;
97 }
98 memcpy(j->b + j->len, b, n);
99 j->len += n;
100 if (sync) j->syncs++;
101 return CX_OK;
102}
103static int mj_truncate(void *ctx, void *h, size_t len) {
104 MemJournal *j = ctx;
105 if (h != (void *)j) return CX_ERR_IO;
106 j->truncates++;
107 if (j->fail_truncate) return CX_ERR_IO;
108 if (len > j->len) return CX_ERR_IO;
109 j->len = len;
110 return CX_OK;
111}
112static void mj_close(void *ctx, void *h) {
113 MemJournal *j = ctx;
114 if (h == (void *)j) j->writer = 0;
115}
116static const CxJournalOps mem_ops = { mj_open, mj_size, mj_read, mj_append, mj_truncate, mj_close };
117
118static CxHeader hdr(uint32_t cls, uint32_t kind, uint64_t subject, uint64_t t) {
119 CxHeader h;
120 memset(&h, 0, sizeof h);
121 h.cls = cls;
122 h.kind = kind;
123 h.subject = subject;
124 h.t = t;
125 return h;
126}
127
128int main(void) {
129 MemJournal j;
130 memset(&j, 0, sizeof j);
131 CxStore s, t;
132
133 /* 1. create: a header is written durably; a read-only open of nothing is refused */
134 CHECK(cx_open_with(&t, &mem_ops, &j, "m", 16, CX_OPEN_READONLY) == CX_ERR_FORMAT, "ro open of empty");
135 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_SYNC) == CX_OK, "create");
136 CHECK(j.len == 32 && j.syncs == 1 && j.writer == 1, "header len=%zu syncs=%d", j.len, j.syncs);
137 CHECK(cx_open_with(&t, &mem_ops, &j, "m", 16, 0) == CX_ERR_WRITER, "second writer admitted");
138 CHECK(cx_open_with(&t, &mem_ops, &j, "m", 0, CX_OPEN_READONLY) == CX_OK && t.n == 0, "ro while written");
139 cx_close(&t);
140 CHECK(j.writer == 1, "ro close dropped the writer");
141
142 /* 2. append: each record is one atomic unit, synced under CX_OPEN_SYNC */
143 for (uint64_t k = 1; k <= 5; k++) {
144 CxHeader h = hdr(CX_CLAIM, CX_K_CLAIM, k % 3, k);
145 uint64_t pay[3] = { k, k + 100, k + 200 };
146 CHECK(cx_append(&s, &h, pay, 3, NULL) == CX_OK, "append %llu", (unsigned long long)k);
147 }
148 CHECK(s.n == 5 && j.appends == 6 && j.syncs == 6, "appends=%d syncs=%d", j.appends, j.syncs);
149 size_t len5 = j.len;
150 uint8_t chain5[32];
151 memcpy(chain5, s.chain, 32);
152
153 /* 3. a failed append leaves memory and journal exactly as before */
154 j.fail_append = 1;
155 CxHeader h = hdr(CX_OBSERVATION, CX_K_EVIDENCE_REF, 1, 6);
156 uint64_t pay[1] = { 42 };
157 CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_ERR_IO, "failed append not reported");
158 CHECK(s.n == 5 && j.len == len5 && memcmp(s.chain, chain5, 32) == 0, "memory ran ahead of the journal");
159 CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_OK && s.n == 6, "append after failure");
160 uint8_t chain6[32];
161 memcpy(chain6, s.chain, 32);
162 cx_close(&s);
163 CHECK(j.writer == 0, "close kept the writer lock");
164
165 /* 4. reopen replays every record; the chain is rebuilt, not read */
166 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 0, 0) == CX_OK, "reopen");
167 CHECK(s.n == 6 && s.n_subjects == 16 && memcmp(s.chain, chain6, 32) == 0 &&
168 cx_verify_chain(&s) == CX_OK, "replayed state differs");
169 CxRecord r;
170 CHECK(cx_recall_id(&s, 4, &r) == CX_OK && r.payload[2] == 204, "recalled record differs");
171 CHECK(cx_open_with(&t, &mem_ops, &j, "m", 17, CX_OPEN_READONLY) == CX_ERR_FORMAT, "subject mismatch");
172 cx_close(&s);
173
174 /* 5. a torn tail is refused, repaired only on request, and only by the backend */
175 size_t full = j.len;
176 j.len = full - 5;
177 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, 0) == CX_ERR_TORN, "torn tail accepted");
178 CHECK(j.writer == 0, "failed open kept the writer lock");
179 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_READONLY) == CX_ERR_TORN, "ro torn tail accepted");
180 j.fail_truncate = 1;
181 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_REPAIR_TAIL) == CX_ERR_IO, "repair without truncate");
182 j.fail_truncate = 0;
183 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_REPAIR_TAIL) == CX_OK && s.n == 5 &&
184 j.truncates == 2 && j.len < full - 5, "repair tail");
185 CHECK(memcmp(s.chain, chain5, 32) == 0, "repaired chain differs");
186 cx_close(&s);
187
188 /* 6. a complete record with a changed byte is never repaired */
189 j.b[32 + 8 * 14] ^= 1; /* first record, payload word 1 */
190 CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_REPAIR_TAIL) == CX_ERR_DIGEST, "tampered journal opened");
191 j.b[32 + 8 * 14] ^= 1;
192
193 /* 7. memory-only stores never touch a backend; nothing leaks */
194 int appends_before = j.appends;
195 CHECK(cx_init(&s, 4) == CX_OK && s.jops == NULL, "memory-only init");
196 h = hdr(CX_CLAIM, CX_K_CLAIM, 1, 1);
197 CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_OK && j.appends == appends_before,
198 "memory-only append hit the backend");
199 cx_close(&s);
200
201 /* 8. a subject count too large to index is refused, never wrapped into a small allocation */
202 const uint64_t wraps = (uint64_t)(SIZE_MAX / sizeof(CxIdList)) + 1; /* * size wraps to one entry */
203 CHECK(cx_init(&t, UINT64_MAX) == CX_ERR_NOMEM, "oversized subject count initialised");
204 CHECK(cx_init(&t, wraps) == CX_ERR_NOMEM, "wrapping subject count initialised");
205 uint8_t saved[8];
206 memcpy(saved, j.b + 16, 8);
207 const uint64_t hostile[2] = { UINT64_MAX, wraps };
208 for (int k = 0; k < 2; k++) {
209 for (int i = 0; i < 8; i++) j.b[16 + i] = (uint8_t)(hostile[k] >> (8 * i)); /* header word 2 */
210 CHECK(cx_open_with(&t, &mem_ops, &j, "m", 0, CX_OPEN_READONLY) == CX_ERR_FORMAT,
211 "journal with subject count %llu opened", (unsigned long long)hostile[k]);
212 }
213 memcpy(j.b + 16, saved, 8);
214 CHECK(g_live == 0, "live allocations after close: %ld", g_live);
215
216 free(j.b);
217 printf("%d checks, %d failures\n", g_checks, g_fail);
218 printf(g_fail ? "M20 CORTEX JOURNAL SEAM: FAIL\n" : "M20 CORTEX JOURNAL SEAM: PASS\n");
219 return g_fail ? 1 : 0;
220}