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 / synapse / test_syn_codec.c

885 lines · 37846 bytes Raw Blame History
1/* test_syn_codec.c: J1 cross-check of the Omega synapse codec (src/synapse/syn_codec.osc) against the corpus
2 * the Rust codec sealed (tests/synapse/j1-corpus/corpus.bin, written by synapse
3 * crates/synapse-store/src/j1_corpus.rs; format in synapse docs/J1_OMEGA_CODEC.md).
4 *
5 * test_syn_codec SYN_CODEC_OSC CORPUS_BIN [FUZZ_COUNT] seed: SYN_FUZZ_SEED
6 *
7 * Every call runs the unit twice, in the OSC interpreter and as native AArch64, on exact-size copies of the
8 * input and an exact-size workspace, and requires the same status, refusal cells and output from both.
9 * 1. corpus integrity: SHA-256 trailer, then the pinned SHA-256 (SYN_CORPUS_PIN), header, Rust layout sizes ==
10 * syn_layout.lst, name tables. Every case position is range-checked before its input is built. A corpus
11 * that fails any of these is refused with one typed line (corpus_refuse) and exit 3.
12 * 2. bases (Rust -> Omega): decoding each accepted base gives its Rust logical form exactly.
13 * 3. bases (Omega -> Rust): encoding each Rust logical form gives the Rust bytes exactly (exact-size output;
14 * one byte less is OUT_FULL).
15 * 4. cases: every A1 mutation and hand-made refusal decodes to the Rust outcome tuple (kind, at, x, y,
16 * field); every accepted mutation re-encodes from the Omega logical form to the same bytes.
17 * 5. encode refusals on hand-broken logical forms, ABI header refusals, the input LIMIT.
18 * 6. fuzz: random logical-form edits (accepted encodes decode back to the same logical form, or to a
19 * decoder-only refusal) and random byte edits (no trap; accepts are canonical).
20 * No literal key or nonce material: keys in the corpus are derived on the Rust side from labels. */
21#include <stdarg.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <time.h>
27
28#include "osc_cg.h"
29#include "osc_front.h"
30#include "osc_interp.h"
31#include "osc_native.h"
32#include "osc_rt.h"
33#include "sha256.h"
34#include "syn_layout.h"
35
36static unsigned long n_checks, n_fail;
37static unsigned long n_calls[2], n_traps, n_engine_cmp;
38static unsigned long n_base_dec, n_base_enc, n_case, n_case_acc, n_case_ref, n_case_reenc;
39static unsigned long n_kind[10], n_by_op[4][2], n_enc_neg, n_abi, n_fuzz_lf, n_fuzz_lf_acc, n_fuzz_lf_deconly;
40static unsigned long n_fuzz_b, n_fuzz_b_acc, n_rec_kind[16], n_base_dec_ok, n_base_enc_ok, n_case_match;
41
42static void check(int cond, const char *fmt, ...)
43{
44 n_checks++;
45 if (cond) return;
46 n_fail++;
47 if (n_fail <= 40) {
48 va_list ap;
49 va_start(ap, fmt);
50 fprintf(stderr, "FAIL: ");
51 vfprintf(stderr, fmt, ap);
52 fprintf(stderr, "\n");
53 va_end(ap);
54 }
55}
56
57/* The corpus this driver accepts: SHA-256 of the whole corpus.bin (trailer included), the value in
58 * tests/synapse/j1-corpus/PROVENANCE and synapse j1_corpus.rs CORPUS_SHA256 (`sha256sum corpus.bin`). */
59#define SYN_CORPUS_PIN "b2aba356b7dd58b48b6cd7139fdeaa059ee5ab8f496d3ba03f6e4a5aa94db50b"
60_Static_assert(sizeof SYN_CORPUS_PIN == 65, "SYN_CORPUS_PIN is 64 hex digits");
61/* emb_open packs positions into 20-bit fields; syn_gen.sh refuses this too, the build refuses it here. */
62_Static_assert(SYN_MASK20 == (1ULL << 20) - 1, "MASK20 is the 20-bit field mask");
63_Static_assert(SYN_INPUT_MAX <= SYN_MASK20, "INPUT_MAX must fit the 20-bit packing in emb_open");
64
65/* A corpus the driver will not run: one typed line on stdout, SYN_J1_FAIL, exit 3. Codes: CORPUS_SHORT,
66 * CORPUS_FINGERPRINT (trailer != content hash), CORPUS_PIN (file hash != SYN_CORPUS_PIN), CORPUS_PARSE
67 * (a length runs past the end), CORPUS_RANGE (a base index, op, position or outcome field out of range). */
68static void corpus_refuse(const char *code, const char *fmt, ...)
69{
70 va_list ap;
71 va_start(ap, fmt);
72 printf("corpus refused: %s: ", code);
73 vprintf(fmt, ap);
74 printf("\nSYN_J1_FAIL\n");
75 va_end(ap);
76 fflush(stdout);
77 exit(3);
78}
79
80static void *xmalloc(size_t n)
81{
82 void *p = malloc(n ? n : 1);
83 if (!p) { fprintf(stderr, "oom\n"); exit(2); }
84 return p;
85}
86
87/* ---- the unit ---- */
88static OscUnit *U;
89static OscRt *RI, *RN;
90static OscCode CODE;
91static OscNative NM;
92static int FI = -1;
93
94/* One call's result. */
95typedef struct {
96 uint64_t ret;
97 int trap;
98 uint64_t err, at, x, y, field;
99 uint64_t *lf; /* decode: the logical form */
100 size_t lfn;
101 uint8_t *out; /* encode: the bytes */
102 size_t outn;
103} Res;
104
105static void res_free(Res *r)
106{
107 free(r->lf);
108 free(r->out);
109 memset(r, 0, sizeof *r);
110}
111
112/* Header-only overrides for ABI tests. */
113typedef struct {
114 int set;
115 size_t idx;
116 uint64_t val;
117} Poke;
118
119/* One call of syn_codec with engine e. Decode: inp/inn are the bytes, lfcap cells of LF room.
120 * Encode: lf/lfn is the logical form, outcap cells of output room. The workspace is exactly
121 * W_HDR + lfcap + outcap cells (wcells_override if nonzero, for ABI tests). */
122static void call1(int e, int entry, int mode, uint64_t cap, uint64_t ckseq, const uint8_t *inp, size_t inn,
123 const uint64_t *lf, size_t lfn, size_t lfcap, size_t outcap, const Poke *poke, size_t wover, Res *r)
124{
125 memset(r, 0, sizeof *r);
126 size_t wn = SYN_W_HDR + lfcap + outcap;
127 if (wover) wn = wover;
128 uint64_t *w = calloc(wn, 8);
129 if (!w) { fprintf(stderr, "oom\n"); exit(2); }
130 uint8_t *b = NULL;
131 if (inn) {
132 b = xmalloc(inn);
133 memcpy(b, inp, inn);
134 }
135 if (wn >= SYN_W_HDR) {
136 w[SYN_W_MAGIC] = SYN_ABI_MAGIC;
137 w[SYN_W_VERSION] = SYN_ABI_VERSION;
138 w[SYN_W_CELLS] = wn;
139 w[SYN_W_ENTRY] = (uint64_t)entry;
140 w[SYN_W_MODE] = (uint64_t)mode;
141 w[SYN_W_CAP] = cap;
142 w[SYN_W_CKSEQ] = ckseq;
143 w[SYN_W_LF_BASE] = SYN_W_HDR;
144 w[SYN_W_LF_CAP] = lfcap;
145 w[SYN_W_OUT_BASE] = SYN_W_HDR + lfcap;
146 w[SYN_W_OUT_CAP] = outcap;
147 if (mode == (int)SYN_MODE_ENCODE) {
148 w[SYN_W_LF_N] = lfn;
149 if (lfn) memcpy(w + SYN_W_HDR, lf, lfn * 8);
150 }
151 for (const Poke *p = poke; p && p->set; p++) w[p->idx] = p->val;
152 }
153 uint64_t args[4] = { (uint64_t)(uintptr_t)b, inn, (uint64_t)(uintptr_t)w, wn };
154 check(osc_ir_slice_args_ok(&U->funcs[FI], args, 4) == 0, "host entry refused valid buffers");
155 if (e == 0) {
156 osc_rt_reset(RI);
157 r->trap = osc_interp_run_prevalidated(U, FI, args, 4, RI, &r->ret);
158 } else {
159 osc_rt_reset(RN);
160 r->trap = osc_native_call(U, FI, &NM, CODE.entry[FI], RN, args, 4, &r->ret);
161 }
162 n_calls[e]++;
163 if (r->trap) n_traps++;
164 if (wn >= SYN_W_HDR) {
165 r->err = w[SYN_W_ERR];
166 r->at = w[SYN_W_ERR_AT];
167 r->x = w[SYN_W_ERR_X];
168 r->y = w[SYN_W_ERR_Y];
169 r->field = w[SYN_W_ERR_FIELD];
170 if (!r->trap && r->err == 0 && w[SYN_W_LF_BASE] == SYN_W_HDR) {
171 if (mode == (int)SYN_MODE_DECODE) {
172 r->lfn = w[SYN_W_LF_N];
173 if (r->lfn > lfcap) r->lfn = lfcap;
174 r->lf = xmalloc(r->lfn * 8);
175 memcpy(r->lf, w + SYN_W_HDR, r->lfn * 8);
176 } else {
177 r->outn = w[SYN_W_OUT];
178 if (r->outn > outcap) r->outn = outcap;
179 r->out = xmalloc(r->outn);
180 int okb = 1;
181 for (size_t i = 0; i < r->outn; i++) {
182 uint64_t v = w[SYN_W_HDR + lfcap + i];
183 okb &= v < 256;
184 r->out[i] = (uint8_t)v;
185 }
186 check(okb, "encode wrote a cell above 255");
187 }
188 }
189 }
190 free(w);
191 free(b);
192}
193
194static int same_res(const Res *a, const Res *b)
195{
196 if (a->trap != b->trap || a->ret != b->ret || a->err != b->err || a->at != b->at || a->x != b->x ||
197 a->y != b->y || a->field != b->field || a->lfn != b->lfn || a->outn != b->outn)
198 return 0;
199 if (a->lfn && memcmp(a->lf, b->lf, a->lfn * 8)) return 0;
200 if (a->outn && memcmp(a->out, b->out, a->outn)) return 0;
201 return 1;
202}
203
204/* Both engines; the interpreter result is kept in r. */
205static void call2(int entry, int mode, uint64_t cap, uint64_t ckseq, const uint8_t *inp, size_t inn,
206 const uint64_t *lf, size_t lfn, size_t lfcap, size_t outcap, const Poke *poke, size_t wover,
207 Res *r, const char *what)
208{
209 Res nat;
210 call1(0, entry, mode, cap, ckseq, inp, inn, lf, lfn, lfcap, outcap, poke, wover, r);
211 call1(1, entry, mode, cap, ckseq, inp, inn, lf, lfn, lfcap, outcap, poke, wover, &nat);
212 n_engine_cmp++;
213 check(!r->trap, "%s: interp trap %d", what, r->trap);
214 check(!nat.trap, "%s: native trap %d", what, nat.trap);
215 check(same_res(r, &nat), "%s: interp and native differ (err %llu/%llu at %llu/%llu)", what,
216 (unsigned long long)r->err, (unsigned long long)nat.err, (unsigned long long)r->at,
217 (unsigned long long)nat.at);
218 check(wover && wover < SYN_W_HDR ? 1 : r->ret == r->err, "%s: return %llu != W_ERR %llu", what, (unsigned long long)r->ret,
219 (unsigned long long)r->err);
220 res_free(&nat);
221}
222
223static size_t lfcap_for(size_t n) { return 2 * n + 16; }
224
225static void decode(int entry, uint64_t cap, uint64_t ckseq, const uint8_t *b, size_t n, Res *r, const char *what)
226{
227 call2(entry, (int)SYN_MODE_DECODE, cap, ckseq, b, n, NULL, 0, lfcap_for(n), 0, NULL, 0, r, what);
228}
229
230static void encode(int entry, const uint64_t *lf, size_t lfn, size_t outcap, Res *r, const char *what)
231{
232 call2(entry, (int)SYN_MODE_ENCODE, 0, 0, NULL, 0, lf, lfn, lfn, outcap, NULL, 0, r, what);
233}
234
235/* ---- corpus ---- */
236typedef struct {
237 int entry;
238 uint64_t cap, ckseq;
239 char label[128];
240 const uint8_t *bytes;
241 size_t n;
242 uint64_t *lf; /* NULL: a hand-made refused base */
243 size_t lfn;
244} Base;
245
246static const uint8_t *P, *PE;
247static int bad_parse;
248
249static const uint8_t *take(size_t n)
250{
251 if ((size_t)(PE - P) < n) {
252 bad_parse = 1;
253 static const uint8_t zero[64];
254 return zero;
255 }
256 const uint8_t *q = P;
257 P += n;
258 return q;
259}
260static uint64_t le(const uint8_t *q, int nb)
261{
262 uint64_t v = 0;
263 for (int i = 0; i < nb; i++) v |= (uint64_t)q[i] << (8 * i);
264 return v;
265}
266static uint64_t g8(void) { return le(take(1), 1); }
267static uint64_t g16(void) { return le(take(2), 2); }
268static uint64_t g32(void) { return le(take(4), 4); }
269static uint64_t g64(void) { return le(take(8), 8); }
270
271static void gname(char *dst, size_t cap)
272{
273 size_t n = g16();
274 const uint8_t *q = take(n);
275 if (bad_parse) { dst[0] = 0; return; }
276 size_t k = n < cap - 1 ? n : cap - 1;
277 memcpy(dst, q, k);
278 dst[k] = 0;
279}
280
281static const char *FIELDS[15] = { "bool", "option", "mutation op", "file mode", "record kind",
282 "envelope v2", "chain path", "obsolescence marker", "changesets",
283 "idempotency index", "blob visibility", "projections", "canonical heads",
284 "revision records", "revision record kind" };
285static const char *CODES[2] = { "EnvelopeMalformed", "EnvelopeVersionUnsupported" };
286static const char *KINDS[9] = { "Truncated", "LengthOverCap", "TrailingBytes", "UnknownTag", "InvalidUtf8",
287 "VersionUnsupported", "NotCanonical", "InvalidRepoId", "EmbeddedMalformed" };
288
289static void check_table(const char **want, size_t n, const char *what)
290{
291 size_t got = g32();
292 check(got == n, "%s table has %zu names, want %zu", what, got, n);
293 for (size_t i = 0; i < got && i < 64; i++) {
294 char nm[128];
295 gname(nm, sizeof nm);
296 check(i < n && strcmp(nm, want[i]) == 0, "%s table name %zu is \"%s\"", what, i + 1, nm);
297 }
298}
299
300static uint8_t *slurp(const char *path, size_t *n)
301{
302 FILE *f = fopen(path, "rb");
303 if (!f) return NULL;
304 fseek(f, 0, SEEK_END);
305 long sz = ftell(f);
306 fseek(f, 0, SEEK_SET);
307 uint8_t *b = xmalloc((size_t)sz + 1);
308 size_t got = fread(b, 1, (size_t)sz, f);
309 fclose(f);
310 if (got != (size_t)sz) { free(b); return NULL; }
311 b[sz] = 0;
312 *n = (size_t)sz;
313 return b;
314}
315
316static uint64_t rng_next(uint64_t *s)
317{
318 uint64_t x = *s;
319 x ^= x << 13;
320 x ^= x >> 7;
321 x ^= x << 17;
322 *s = x;
323 return x;
324}
325
326/* The record kind of a logical form (first token is the kind tag). */
327static uint64_t lf_kind(const uint64_t *lf, size_t n) { return n >= 2 ? lf[1] : 0; }
328
329static double now_s(void)
330{
331 struct timespec t;
332 clock_gettime(CLOCK_MONOTONIC, &t);
333 return (double)t.tv_sec + (double)t.tv_nsec * 1e-9;
334}
335
336/* Timing (SYN_BENCH_REPS=N, off by default): native syn_codec only, on the accepted bases of one entry
337 * kind, N calls per base per round, ROUNDS rounds. Buffers are built once; each call rewrites the
338 * W_HDR header cells (part of the timed work) and runs the unit. Reports bytes per second and seconds
339 * per call per round; the bound (synapse ADR 0003 section 6.2) is applied outside this program. */
340#define SYN_BENCH_ROUNDS 7
341static int cmp_d(const void *a, const void *b)
342{
343 double x = *(const double *)a, y = *(const double *)b;
344 return x < y ? -1 : x > y;
345}
346
347static void bench(const Base *B, size_t nb, int entry, int mode, unsigned long reps)
348{
349 size_t m = 0, bytes = 0;
350 for (size_t i = 0; i < nb; i++) m += B[i].lf && B[i].entry == entry;
351 if (!m) return;
352 uint64_t **W = calloc(m, sizeof *W), **H = calloc(m, sizeof *H), (*A)[4] = calloc(m, sizeof *A);
353 uint8_t **I = calloc(m, sizeof *I);
354 const Base **S = calloc(m, sizeof *S);
355 size_t k = 0;
356 for (size_t i = 0; i < nb; i++) {
357 const Base *b = &B[i];
358 if (!b->lf || b->entry != entry) continue;
359 int dec = mode == (int)SYN_MODE_DECODE;
360 size_t lfcap = dec ? lfcap_for(b->n) : b->lfn, outcap = dec ? 0 : b->n, wn = SYN_W_HDR + lfcap + outcap;
361 W[k] = calloc(wn, 8);
362 H[k] = calloc(SYN_W_HDR + (dec ? 0 : b->lfn), 8);
363 uint64_t *h = H[k];
364 h[SYN_W_MAGIC] = SYN_ABI_MAGIC;
365 h[SYN_W_VERSION] = SYN_ABI_VERSION;
366 h[SYN_W_CELLS] = wn;
367 h[SYN_W_ENTRY] = (uint64_t)entry;
368 h[SYN_W_MODE] = (uint64_t)mode;
369 h[SYN_W_CAP] = b->cap;
370 h[SYN_W_CKSEQ] = b->ckseq;
371 h[SYN_W_LF_BASE] = SYN_W_HDR;
372 h[SYN_W_LF_CAP] = lfcap;
373 h[SYN_W_OUT_BASE] = SYN_W_HDR + lfcap;
374 h[SYN_W_OUT_CAP] = outcap;
375 if (!dec) {
376 h[SYN_W_LF_N] = b->lfn;
377 memcpy(h + SYN_W_HDR, b->lf, b->lfn * 8);
378 }
379 memcpy(W[k], h, (SYN_W_HDR + (dec ? 0 : b->lfn)) * 8);
380 I[k] = xmalloc(b->n ? b->n : 1);
381 memcpy(I[k], b->bytes, b->n);
382 A[k][0] = dec ? (uint64_t)(uintptr_t)I[k] : 0;
383 A[k][1] = dec ? b->n : 0;
384 A[k][2] = (uint64_t)(uintptr_t)W[k];
385 A[k][3] = wn;
386 bytes += b->n;
387 S[k] = b;
388 k++;
389 }
390 double gbs[SYN_BENCH_ROUNDS], per[SYN_BENCH_ROUNDS];
391 unsigned long bad = 0;
392 osc_rt_reset(RN);
393 for (int r = 0; r < SYN_BENCH_ROUNDS; r++) {
394 double t0 = now_s();
395 for (size_t j = 0; j < m; j++) {
396 for (unsigned long q = 0; q < reps; q++) {
397 uint64_t ret = 1;
398 memcpy(W[j], H[j], SYN_W_HDR * 8);
399 int tr = osc_native_call(U, FI, &NM, CODE.entry[FI], RN, A[j], 4, &ret);
400 bad += tr != 0 || ret != 0;
401 }
402 }
403 double dt = now_s() - t0;
404 gbs[r] = (double)bytes * (double)reps / dt / 1e9;
405 per[r] = dt / ((double)m * (double)reps);
406 }
407 check(bad == 0, "bench: %lu calls trapped or refused", bad);
408 for (size_t j = 0; j < m; j++) {
409 /* the last timed call's output is still the Rust answer */
410 const Base *b = S[j];
411 const uint64_t *w = W[j];
412 int ok = w[SYN_W_ERR] == 0;
413 if (mode == (int)SYN_MODE_DECODE) {
414 ok &= w[SYN_W_LF_N] == b->lfn && memcmp(w + SYN_W_HDR, b->lf, b->lfn * 8) == 0;
415 } else {
416 ok &= w[SYN_W_OUT] == b->n;
417 for (size_t i = 0; ok && i < b->n; i++) ok &= w[w[SYN_W_OUT_BASE] + i] == b->bytes[i];
418 }
419 check(ok, "bench %s: output after the timed calls differs from Rust", b->label);
420 }
421 qsort(gbs, SYN_BENCH_ROUNDS, sizeof gbs[0], cmp_d);
422 qsort(per, SYN_BENCH_ROUNDS, sizeof per[0], cmp_d);
423 printf("bench %s %s: %zu bases, %zu bytes, %lu calls per base per round, %d rounds: median %.4f GB/s "
424 "(min %.4f, max %.4f), median %.3e s per call\n",
425 mode == (int)SYN_MODE_DECODE ? "decode" : "encode", entry == 1 ? "records" : "checkpoints", m, bytes, reps,
426 SYN_BENCH_ROUNDS, gbs[SYN_BENCH_ROUNDS / 2], gbs[0], gbs[SYN_BENCH_ROUNDS - 1], per[SYN_BENCH_ROUNDS / 2]);
427 for (size_t j = 0; j < m; j++) {
428 free(W[j]);
429 free(H[j]);
430 free(I[j]);
431 }
432 free(W);
433 free(H);
434 free(A);
435 free(I);
436 free(S);
437}
438
439static void expect_enc_ref(int entry, const uint64_t *lf, size_t lfn, size_t outcap, uint64_t want, const char *what)
440{
441 Res r;
442 encode(entry, lf, lfn, outcap, &r, what);
443 check(r.err == want, "%s: encode gave %llu, want %llu", what, (unsigned long long)r.err, (unsigned long long)want);
444 n_enc_neg++;
445 res_free(&r);
446}
447
448static size_t find_token(const uint64_t *lf, size_t n, uint64_t type, size_t minlen)
449{
450 size_t i = 0;
451 while (i < n) {
452 uint64_t h = lf[i], t = h & 255, len = h >> 8;
453 if (t == type && len >= minlen) return i;
454 i += 1 + (size_t)len;
455 }
456 return (size_t)-1;
457}
458
459int main(int argc, char **argv)
460{
461 if (argc < 3) {
462 fprintf(stderr, "usage: test_syn_codec SYN_CODEC_OSC CORPUS_BIN [FUZZ_COUNT]\n");
463 return 2;
464 }
465 unsigned long fuzz = argc > 3 ? strtoul(argv[3], NULL, 10) : 20000;
466 size_t sl, cl;
467 uint8_t *src = slurp(argv[1], &sl);
468 uint8_t *corp = slurp(argv[2], &cl);
469 if (!src || !corp) { fprintf(stderr, "cannot read %s or %s\n", argv[1], argv[2]); return 2; }
470
471 /* 1. corpus integrity: length, internal fingerprint, then the pinned fingerprint */
472 if (cl <= 32) corpus_refuse("CORPUS_SHORT", "%zu bytes, below the 32-byte fingerprint trailer", cl);
473 uint8_t dig[32];
474 sha256_hash(corp, cl - 32, dig);
475 char hex[65];
476 for (int i = 0; i < 32; i++) snprintf(hex + 2 * i, 3, "%02x", dig[i]);
477 if (memcmp(dig, corp + cl - 32, 32) != 0)
478 corpus_refuse("CORPUS_FINGERPRINT", "SHA-256 trailer does not match the content (content hashes to %s)", hex);
479 const char *pin = SYN_CORPUS_PIN;
480 const char *po = getenv("SYN_CORPUS_PIN_OVERRIDE");
481 if (po && *po) {
482 printf("corpus pin OVERRIDDEN by SYN_CORPUS_PIN_OVERRIDE (guard tests only): %s\n", po);
483 pin = po;
484 }
485 uint8_t whole[32];
486 sha256_hash(corp, cl, whole);
487 char wh[65];
488 for (int i = 0; i < 32; i++) snprintf(wh + 2 * i, 3, "%02x", whole[i]);
489 if (strcmp(wh, pin) != 0) corpus_refuse("CORPUS_PIN", "file sha256 %s, pinned %s", wh, pin);
490 P = corp;
491 PE = corp + cl - 32;
492 check(memcmp(take(8), "SYNJ1COD", 8) == 0, "corpus magic");
493 check(g16() == 1 && g16() == 0, "corpus version");
494 uint64_t CAP = g32();
495 check(CAP == 2048, "corpus cap %llu", (unsigned long long)CAP);
496 check(g32() == 4, "layout size count");
497 uint64_t sz[4] = { g32(), g32(), g32(), g32() };
498 check(sz[0] == SYN_SZ_ATTACHMENT && sz[1] == SYN_SZ_MUTATION && sz[2] == SYN_SZ_REVIEW && sz[3] == SYN_SZ_STRING,
499 "Rust layout sizes %llu %llu %llu %llu differ from syn_layout.lst", (unsigned long long)sz[0],
500 (unsigned long long)sz[1], (unsigned long long)sz[2], (unsigned long long)sz[3]);
501 check_table(FIELDS, 15, "field");
502 check_table(CODES, 2, "code");
503 check_table(KINDS, 9, "kind");
504 size_t nb = g32();
505 Base *B = calloc(nb ? nb : 1, sizeof *B);
506 for (size_t i = 0; i < nb && !bad_parse; i++) {
507 B[i].entry = (int)g8();
508 B[i].cap = g64();
509 B[i].ckseq = g64();
510 gname(B[i].label, sizeof B[i].label);
511 B[i].n = g32();
512 B[i].bytes = take(B[i].n);
513 size_t ln = g32();
514 if (ln != 0xFFFFFFFFu) {
515 B[i].lfn = ln;
516 B[i].lf = xmalloc(ln * 8);
517 for (size_t j = 0; j < ln; j++) B[i].lf[j] = g64();
518 }
519 }
520 size_t ncase = g32();
521 const uint8_t *cases = P;
522 if (bad_parse) corpus_refuse("CORPUS_PARSE", "a base or its logical form runs past the end of the corpus");
523 if (bad_parse || n_fail) { printf("SYN_J1_FAIL\n"); return 1; }
524 printf("corpus: %zu bases, %zu cases, case section at byte %zu, pin %s\n", nb, ncase, (size_t)(cases - corp), pin);
525
526 /* compile the unit */
527 U = calloc(1, sizeof *U);
528 RI = calloc(1, sizeof *RI);
529 RN = calloc(1, sizeof *RN);
530 OscDiag d;
531 if (osc_compile((const char *)src, sl, U, &d, NULL) != 0) {
532 fprintf(stderr, "syn_codec.osc refused: %s line %u: %s\n", osc_diag_kind_name(d.kind), d.line, d.message);
533 printf("SYN_J1_FAIL\n");
534 return 1;
535 }
536 char err[256];
537 check(osc_ir_validate(U, err, sizeof err) == 0, "validate: %s", err);
538 for (int f = 0; f < U->nfuncs; f++)
539 if (strcmp(U->funcs[f].name, "syn_codec") == 0) FI = f;
540 check(FI >= 0, "no syn_codec");
541 memset(&CODE, 0, sizeof CODE);
542 int cg = osc_cg_compile(U, &CODE, err, sizeof err);
543 check(cg == 0, "codegen: %s", err);
544 if (cg || FI < 0) { printf("SYN_J1_FAIL\n"); return 1; }
545 int nmr = osc_native_map(&NM, CODE.code, CODE.len);
546 check(nmr == 0, "native map %d", nmr);
547 if (nmr) { printf("SYN_J1_FAIL\n"); return 1; }
548 osc_rt_init(RI);
549 osc_rt_init(RN);
550 printf("unit: %d of %d functions, %zu bytes of AArch64\n", U->nfuncs, OSC_MAX_FUNCS, (size_t)CODE.len);
551
552 /* 2 + 3. bases, both directions */
553 for (size_t i = 0; i < nb; i++) {
554 Base *b = &B[i];
555 if (!b->lf) continue;
556 Res r;
557 decode(b->entry, b->cap, b->ckseq, b->bytes, b->n, &r, b->label);
558 n_base_dec++;
559 check(r.err == 0, "base %s: Omega decode refused kind %llu at %llu", b->label, (unsigned long long)r.err,
560 (unsigned long long)r.at);
561 check(r.err != 0 || (r.lfn == b->lfn && memcmp(r.lf, b->lf, b->lfn * 8) == 0),
562 "base %s: Omega logical form differs from Rust (%zu vs %zu cells)", b->label, r.lfn, b->lfn);
563 n_base_dec_ok += r.err == 0 && r.lfn == b->lfn && memcmp(r.lf, b->lf, b->lfn * 8) == 0;
564 if (b->entry == 1) n_rec_kind[lf_kind(b->lf, b->lfn) & 15]++;
565 res_free(&r);
566 encode(b->entry, b->lf, b->lfn, b->n, &r, b->label);
567 n_base_enc++;
568 check(r.err == 0, "base %s: Omega encode refused kind %llu at %llu", b->label, (unsigned long long)r.err,
569 (unsigned long long)r.at);
570 check(r.err != 0 || (r.outn == b->n && memcmp(r.out, b->bytes, b->n) == 0),
571 "base %s: Omega bytes differ from Rust (%zu vs %zu)", b->label, r.outn, b->n);
572 n_base_enc_ok += r.err == 0 && r.outn == b->n && memcmp(r.out, b->bytes, b->n) == 0;
573 res_free(&r);
574 if (b->n) {
575 encode(b->entry, b->lf, b->lfn, b->n - 1, &r, b->label);
576 check(r.err == SYN_R_OUT_FULL, "base %s: one byte short gave %llu, want OUT_FULL", b->label,
577 (unsigned long long)r.err);
578 res_free(&r);
579 }
580 }
581
582 /* 4. cases */
583 P = cases;
584 for (size_t c = 0; c < ncase && !bad_parse; c++) {
585 size_t bi = g16();
586 unsigned op = (unsigned)g8();
587 size_t pos = g32();
588 uint64_t declared = op == 3 ? g32() : 0;
589 uint64_t k = g8(), at = 0, x = 0, y = 0, fld = 0;
590 if (k) {
591 at = g32();
592 x = g32();
593 y = g32();
594 fld = g8();
595 }
596 if (bad_parse) corpus_refuse("CORPUS_PARSE", "case %zu runs past the end of the corpus", c);
597 /* Positions come from the file: range-check before building the input from them. */
598 if (bi >= nb) corpus_refuse("CORPUS_RANGE", "case %zu: base index %zu, corpus has %zu bases", c, bi, nb);
599 if (op > 3) corpus_refuse("CORPUS_RANGE", "case %zu: op %u above 3", c, op);
600 if ((op == 0) != (B[bi].lf == NULL))
601 corpus_refuse("CORPUS_RANGE", "case %zu: op %u on a base %s a logical form", c, op,
602 B[bi].lf ? "with" : "without");
603 size_t bn = B[bi].n;
604 int pos_ok = op == 0 ? pos == 0
605 : op == 1 ? pos < bn
606 : op == 2 ? pos / 8 < bn
607 : bn >= 4 && pos <= bn - 4;
608 if (!pos_ok)
609 corpus_refuse("CORPUS_RANGE", "case %zu: op %u position %zu outside base %zu (%zu bytes)", c, op, pos, bi,
610 bn);
611 if (k > 9 || fld > 15)
612 corpus_refuse("CORPUS_RANGE", "case %zu: refusal kind %llu or field %llu out of range", c,
613 (unsigned long long)k, (unsigned long long)fld);
614 Base *b = &B[bi];
615 size_t n = b->n;
616 uint8_t *m = xmalloc(n);
617 memcpy(m, b->bytes, n);
618 if (op == 1) n = pos;
619 if (op == 2) m[pos / 8] ^= (uint8_t)(1u << (pos % 8));
620 if (op == 3) for (int j = 0; j < 4; j++) m[pos + j] = (uint8_t)(declared >> (8 * j));
621 char what[192];
622 snprintf(what, sizeof what, "case %zu (%s op %u pos %zu)", c, b->label, op, pos);
623 Res r;
624 decode(b->entry, b->cap, b->ckseq, m, n, &r, what);
625 n_case++;
626 n_kind[k <= 9 ? k : 0]++;
627 n_by_op[op][k != 0]++;
628 n_case_match += r.err == k && r.at == at && r.x == x && r.y == y && r.field == fld;
629 check(r.err == k && r.at == at && r.x == x && r.y == y && r.field == fld,
630 "%s: Omega (%llu,%llu,%llu,%llu,%llu) Rust (%llu,%llu,%llu,%llu,%llu)", what,
631 (unsigned long long)r.err, (unsigned long long)r.at, (unsigned long long)r.x, (unsigned long long)r.y,
632 (unsigned long long)r.field, (unsigned long long)k, (unsigned long long)at, (unsigned long long)x,
633 (unsigned long long)y, (unsigned long long)fld);
634 if (k == 0) {
635 n_case_acc++;
636 if (r.err == 0) {
637 Res e2;
638 encode(b->entry, r.lf, r.lfn, n, &e2, what);
639 check(e2.err == 0 && e2.outn == n && memcmp(e2.out, m, n) == 0,
640 "%s: accepted mutation does not re-encode to itself", what);
641 n_case_reenc += e2.err == 0 && e2.outn == n && memcmp(e2.out, m, n) == 0;
642 res_free(&e2);
643 }
644 } else {
645 n_case_ref++;
646 }
647 res_free(&r);
648 free(m);
649 }
650 if (P != PE) corpus_refuse("CORPUS_PARSE", "%zu bytes after the last case, before the trailer", (size_t)(PE - P));
651
652 /* 5. encode refusals on hand-broken logical forms */
653 {
654 Base *rb = NULL, *cb = NULL;
655 for (size_t i = 0; i < nb; i++) {
656 if (!B[i].lf) continue;
657 if (!rb && B[i].entry == 1 && lf_kind(B[i].lf, B[i].lfn) == 1) rb = &B[i];
658 if (!cb && B[i].entry == 2 && strcmp(B[i].label, "revision checkpoint") == 0) cb = &B[i];
659 }
660 check(rb && cb, "corpus lacks a kind 1 record base or the revision checkpoint");
661 if (rb && cb) {
662 size_t n = rb->lfn;
663 uint64_t *t = xmalloc((n + 1) * 8);
664 memcpy(t, rb->lf, n * 8);
665 t[0] = SYN_T_U64 | 256;
666 expect_enc_ref(1, t, n, rb->n, SYN_R_LF_TYPE, "LF type");
667 memcpy(t, rb->lf, n * 8);
668 expect_enc_ref(1, t, n - 1, rb->n, SYN_R_LF_END, "LF end");
669 t[n] = 0;
670 expect_enc_ref(1, t, n + 1, rb->n + 8, SYN_R_LF_TRAILING, "LF trailing");
671 for (uint64_t kk = 9; kk <= 12; kk++) {
672 t[1] = kk; /* kinds 9..11 are reserved by open synapse PRs: refused until they land */
673 expect_enc_ref(1, t, n, rb->n, SYN_R_LF_VALUE, "record kind 9..12");
674 }
675 t[1] = 1;
676 size_t si = find_token(t, n, SYN_T_STR, 1);
677 check(si != (size_t)-1, "no string token");
678 if (si != (size_t)-1) {
679 t[si + 1] = 0xFF;
680 expect_enc_ref(1, t, n, rb->n, SYN_R_LF_UTF8, "LF utf8");
681 t[si + 1] = 256;
682 expect_enc_ref(1, t, n, rb->n, SYN_R_LF_VALUE, "LF byte above 255");
683 t[si + 1] = rb->lf[si + 1];
684 }
685 size_t ri = find_token(t, n, SYN_T_REPO, 1);
686 check(ri != (size_t)-1, "no repository token");
687 if (ri != (size_t)-1) {
688 t[ri + 1] = '/';
689 expect_enc_ref(1, t, n, rb->n, SYN_R_LF_REPO, "LF repo");
690 t[ri + 1] = rb->lf[ri + 1];
691 }
692 expect_enc_ref(1, t, n, 0, SYN_R_OUT_FULL, "no output room");
693 free(t);
694 /* map order: swap the two blob digests of the revision checkpoint */
695 n = cb->lfn;
696 t = xmalloc(n * 8);
697 memcpy(t, cb->lf, n * 8);
698 size_t hit = (size_t)-1;
699 for (size_t i = 0; i + 71 < n; i++)
700 if (t[i] == SYN_H_COUNT && t[i + 1] == 2 && t[i + 2] == SYN_D_FIX32 && t[i + 35] == (SYN_T_U64 | 256) &&
701 t[i + 37] == SYN_D_FIX32 && t[i + 70] == (SYN_T_U64 | 256))
702 hit = i;
703 check(hit != (size_t)-1, "revision checkpoint blob section not found");
704 if (hit != (size_t)-1) {
705 for (size_t j = 0; j < 32; j++) {
706 uint64_t s = t[hit + 3 + j];
707 t[hit + 3 + j] = t[hit + 38 + j];
708 t[hit + 38 + j] = s;
709 }
710 expect_enc_ref(2, t, n, cb->n, SYN_R_LF_ORDER, "LF map order");
711 }
712 free(t);
713 }
714 }
715
716 /* ABI header refusals */
717 {
718 static const uint8_t one[4] = { 2, 6, 0, 0 };
719 struct {
720 const char *what;
721 Poke p[3];
722 size_t wover;
723 uint64_t want;
724 } abi[] = {
725 { "magic", { { 1, SYN_W_MAGIC, 0 } }, 0, SYN_R_ABI },
726 { "version", { { 1, SYN_W_VERSION, 2 } }, 0, SYN_R_ABI },
727 { "cells", { { 1, SYN_W_CELLS, 99 } }, 0, SYN_R_ABI },
728 { "entry", { { 1, SYN_W_ENTRY, 3 } }, 0, SYN_R_ABI },
729 { "mode", { { 1, SYN_W_MODE, 0 } }, 0, SYN_R_ABI },
730 { "LF region in header", { { 1, SYN_W_LF_BASE, 4 } }, 0, SYN_R_ABI },
731 { "LF region past end", { { 1, SYN_W_LF_CAP, 1000 } }, 0, SYN_R_ABI },
732 { "regions overlap", { { 1, SYN_W_OUT_BASE, SYN_W_HDR }, { 1, SYN_W_OUT_CAP, 4 } }, 0, SYN_R_ABI },
733 };
734 for (size_t i = 0; i < sizeof abi / sizeof abi[0]; i++) {
735 Res r;
736 call2(1, (int)SYN_MODE_DECODE, CAP, 0, one, 4, NULL, 0, 24, 0, abi[i].p, abi[i].wover, &r, abi[i].what);
737 check(r.err == abi[i].want, "ABI %s: %llu", abi[i].what, (unsigned long long)r.err);
738 n_abi++;
739 res_free(&r);
740 }
741 Res r;
742 call2(1, (int)SYN_MODE_DECODE, CAP, 0, one, 4, NULL, 0, 24, 0, NULL, SYN_W_HDR - 1, &r, "short workspace");
743 check(r.ret == SYN_R_ABI, "short workspace returned %llu", (unsigned long long)r.ret);
744 n_abi++;
745 res_free(&r);
746 /* sanity: the same header untouched is valid (kind 6 cut short) */
747 call2(1, (int)SYN_MODE_DECODE, CAP, 0, one, 4, NULL, 0, 24, 0, NULL, 0, &r, "valid header");
748 check(r.err == SYN_R_TRUNCATED, "valid header gave %llu", (unsigned long long)r.err);
749 res_free(&r);
750 size_t big = SYN_INPUT_MAX + 1;
751 uint8_t *bb = calloc(big, 1);
752 call2(1, (int)SYN_MODE_DECODE, CAP, 0, bb, big, NULL, 0, 24, 0, NULL, 0, &r, "input limit");
753 check(r.err == SYN_R_LIMIT, "input above the limit gave %llu", (unsigned long long)r.err);
754 n_abi++;
755 res_free(&r);
756 free(bb);
757 /* 2^20 bytes: the first size the 20-bit packing in emb_open could not hold */
758 size_t p20 = (size_t)SYN_MASK20 + 1;
759 uint8_t *bp = calloc(p20, 1);
760 call2(1, (int)SYN_MODE_DECODE, CAP, 0, bp, p20, NULL, 0, 24, 0, NULL, 0, &r, "input of 2^20 bytes");
761 check(r.err == SYN_R_LIMIT, "input of 2^20 bytes gave %llu", (unsigned long long)r.err);
762 n_abi++;
763 res_free(&r);
764 free(bp);
765 }
766
767 /* 6. fuzz */
768 uint64_t seed = 0x53594e4a31636f64ULL;
769 const char *se = getenv("SYN_FUZZ_SEED");
770 if (se && *se) seed = strtoull(se, NULL, 0);
771 if (seed == 0) seed = 1;
772 printf("fuzz seed: 0x%016llx (override with SYN_FUZZ_SEED)\n", (unsigned long long)seed);
773 uint64_t rng = seed;
774 size_t nl = 0;
775 for (size_t i = 0; i < nb; i++) nl += B[i].lf != NULL;
776 for (unsigned long f = 0; f < fuzz && nl; f++) {
777 size_t pick = rng_next(&rng) % nl, i = 0;
778 for (;; i++)
779 if (B[i].lf && pick-- == 0) break;
780 Base *b = &B[i];
781 char what[192];
782 if (f & 1) {
783 /* logical-form edits */
784 size_t n = b->lfn;
785 uint64_t *t = xmalloc(n * 8);
786 memcpy(t, b->lf, n * 8);
787 int edits = 1 + (int)(rng_next(&rng) % 3);
788 for (int e = 0; e < edits; e++) {
789 size_t j = rng_next(&rng) % n;
790 uint64_t v = rng_next(&rng);
791 switch (v % 4) {
792 case 0: t[j] = (v >> 8) % 300; break;
793 case 1: t[j] ^= 1ULL << ((v >> 8) % 64); break;
794 case 2: t[j] = t[j] + 1; break;
795 default: t[j] = t[j] - 1; break;
796 }
797 }
798 snprintf(what, sizeof what, "LF fuzz %lu (%s)", f, b->label);
799 Res r;
800 encode(b->entry, t, n, 8 * n + 64, &r, what);
801 n_fuzz_lf++;
802 if (r.err == 0) {
803 n_fuzz_lf_acc++;
804 Res r2;
805 decode(b->entry, b->cap, b->ckseq, r.out, r.outn, &r2, what);
806 if (r2.err == 0) {
807 check(r2.lfn == n && memcmp(r2.lf, t, n * 8) == 0, "%s: encode/decode round trip differs", what);
808 } else {
809 /* the Rust encoder does not apply the decoder's caps or the revision log rules */
810 int deconly = r2.err == SYN_R_OVER_CAP || r2.err == SYN_R_EMBEDDED ||
811 (r2.err == SYN_R_NOT_CANON && r2.field == SYN_F_REVS) ||
812 (r2.err == SYN_R_TAG && r2.field == SYN_F_REVKIND);
813 check(deconly, "%s: encoded bytes refused with kind %llu field %llu", what,
814 (unsigned long long)r2.err, (unsigned long long)r2.field);
815 n_fuzz_lf_deconly++;
816 }
817 res_free(&r2);
818 }
819 res_free(&r);
820 free(t);
821 } else {
822 /* byte edits */
823 size_t n = b->n;
824 uint8_t *m = xmalloc(n);
825 memcpy(m, b->bytes, n);
826 int edits = 1 + (int)(rng_next(&rng) % 6);
827 for (int e = 0; e < edits && n; e++) m[rng_next(&rng) % n] = (uint8_t)rng_next(&rng);
828 if (n && rng_next(&rng) % 4 == 0) n = rng_next(&rng) % n;
829 snprintf(what, sizeof what, "byte fuzz %lu (%s)", f, b->label);
830 Res r;
831 decode(b->entry, b->cap, b->ckseq, m, n, &r, what);
832 n_fuzz_b++;
833 check(r.err <= 9, "%s: decode gave non-codec kind %llu", what, (unsigned long long)r.err);
834 if (r.err == 0) {
835 n_fuzz_b_acc++;
836 Res e2;
837 encode(b->entry, r.lf, r.lfn, n, &e2, what);
838 check(e2.err == 0 && e2.outn == n && memcmp(e2.out, m, n) == 0, "%s: accept is not canonical", what);
839 res_free(&e2);
840 }
841 res_free(&r);
842 free(m);
843 }
844 }
845
846 const char *bn = getenv("SYN_BENCH_REPS");
847 unsigned long breps = bn && *bn ? strtoul(bn, NULL, 10) : 0;
848 if (breps) {
849 bench(B, nb, 1, (int)SYN_MODE_DECODE, breps);
850 bench(B, nb, 2, (int)SYN_MODE_DECODE, breps);
851 bench(B, nb, 1, (int)SYN_MODE_ENCODE, breps);
852 bench(B, nb, 2, (int)SYN_MODE_ENCODE, breps);
853 }
854
855 printf("corpus: %zu bases, %zu cases, sha256 trailer and pin OK\n", nb, ncase);
856 printf("bases: %lu of %lu decode to the Rust logical form, %lu of %lu encode to the Rust bytes (record kinds:",
857 n_base_dec_ok, n_base_dec, n_base_enc_ok, n_base_enc);
858 for (int k = 1; k < 16; k++)
859 if (n_rec_kind[k]) printf(" %d:%lu", k, n_rec_kind[k]);
860 printf(")\n");
861 printf("cases: %lu of %lu give the Rust outcome (Rust accepts %lu, refuses %lu); Omega accepts re-encoded %lu\n",
862 n_case_match, n_case, n_case_acc, n_case_ref, n_case_reenc);
863 printf("Rust outcomes by op: as-is %lu/%lu truncate %lu/%lu flip %lu/%lu false-length %lu/%lu (accepted/refused)\n",
864 n_by_op[0][0], n_by_op[0][1], n_by_op[1][0], n_by_op[1][1], n_by_op[2][0], n_by_op[2][1], n_by_op[3][0],
865 n_by_op[3][1]);
866 printf("Rust refusal kinds in the corpus:");
867 for (int k = 1; k <= 9; k++) printf(" %s:%lu", KINDS[k - 1], n_kind[k]);
868 printf("\n");
869 printf("encode refusals: %lu; ABI cases: %lu\n", n_enc_neg, n_abi);
870 printf("fuzz: %lu LF edits (%lu encoded, %lu of those decoder-only refusals), %lu byte edits (%lu accepted)\n",
871 n_fuzz_lf, n_fuzz_lf_acc, n_fuzz_lf_deconly, n_fuzz_b, n_fuzz_b_acc);
872 printf("calls: interp %lu, native %lu, interp==native compares %lu, traps %lu\n", n_calls[0], n_calls[1],
873 n_engine_cmp, n_traps);
874 for (int k = 1; k <= 9; k++) check(n_kind[k] > 0, "no case of refusal kind %s", KINDS[k - 1]);
875 printf("checks %lu, failures %lu\n", n_checks, n_fail);
876 osc_native_unmap(&NM);
877 osc_cg_free(&CODE);
878 for (size_t i = 0; i < nb; i++) free(B[i].lf);
879 free(B);
880 free(src);
881 free(corp);
882 int ok = n_fail == 0;
883 printf(ok ? "SYN_J1_PASS\n" : "SYN_J1_FAIL\n");
884 return ok ? 0 : 1;
885}