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 / realize / test_program_realize.c

542 lines · 29945 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/* test_program_realize.c -- realization is driven by the actual Omega program
18 * (spec/program-realization.md).
19 *
20 * Gates (each prints "GATE <name> PASS|FAIL <passed>/<total>"):
21 * OMEGA_PROGRAM_REALIZE_PASS omega_program_realize exists, compiles the canonical
22 * body, binds the program id, and fails closed (with a
23 * reason) on every unsupported semantic
24 * OMEGA_REALIZATION_PROGRAM_DRIVEN_PASS the machine-aware synthesizer's code is derived from
25 * the program: different programs -> different code
26 * and ids; sequential code lifts back to the body;
27 * the old fixed-3x-2 synthesizer fails this gate
28 * OMEGA_REALIZATION_DIFFERENTIAL_PASS native execution of the realized bytes == the
29 * independent semantic evaluator, boundary + seeded
30 * random inputs, named + seeded random pipelines,
31 * u8/u16/u32/u64, both canonical machines
32 * OMEGA_REALIZATION_TRIPLE_BIND_PASS SEMANTIC_ID + MACHINE_ID + code -> REALIZATION_ID;
33 * rebinding, code and machine tampering all fail
34 *
35 * Physics-free. Executes generated code natively: AArch64 hosts only (on any other host every
36 * execution check fails; it never passes silently).
37 */
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <stdint.h>
42
43#include "omega_core.h"
44#include "omega_canonical.h"
45#include "omega_program.h"
46#include "omega_machine.h"
47#include "omega_realize.h"
48#include "omega_realize_synth.h"
49#include "omega_exec.h"
50#include "aarch64_encoder.h"
51#include "aarch64_decoder.h"
52
53enum { G_REAL, G_DRIVEN, G_DIFF, G_TRIPLE, G_COUNT };
54static const char *const GATE_NAME[G_COUNT] = {
55 "OMEGA_PROGRAM_REALIZE_PASS", "OMEGA_REALIZATION_PROGRAM_DRIVEN_PASS",
56 "OMEGA_REALIZATION_DIFFERENTIAL_PASS", "OMEGA_REALIZATION_TRIPLE_BIND_PASS"
57};
58static long g_pass[G_COUNT], g_total[G_COUNT];
59static long g_fail_printed;
60
61#define CHECK(gate, cond, ...) \
62 do { \
63 g_total[gate]++; \
64 if (cond) g_pass[gate]++; \
65 else if (g_fail_printed++ < 40) { \
66 fprintf(stderr, "FAIL [%s] %s:%d ", GATE_NAME[gate], __FILE__, __LINE__); \
67 fprintf(stderr, __VA_ARGS__); \
68 fprintf(stderr, "\n"); \
69 } \
70 } while (0)
71
72static int ids_equal(const SemanticId *a, const SemanticId *b) {
73 return memcmp(a->bytes, b->bytes, OMEGA_ID_BYTES) == 0;
74}
75
76static uint64_t g_rng = 0x0123456789ABCDEFull; /* fixed seed: reproducible */
77static uint64_t rnd(void) {
78 uint64_t x = g_rng;
79 x ^= x >> 12; x ^= x << 25; x ^= x >> 27;
80 g_rng = x;
81 return x * 0x2545F4914F6CDD1Dull;
82}
83
84static uint64_t wmask(uint16_t w) { return w >= 64 ? ~0ull : ((1ull << w) - 1ull); }
85
86/* A program built directly from a body (any width, any 64-bit constant): the canonical
87 * structures only, no builder template. */
88static int make_prog(OmegaProgram *p, const char *name, uint16_t w, const uint8_t *ops, const uint64_t *imms,
89 size_t n) {
90 omega_program_init(p, name);
91 p->contract.input_type = p->contract.output_type = TYPE_UNSIGNED_INT;
92 p->contract.input_width = p->contract.output_width = w;
93 snprintf(p->contract.precondition, sizeof p->contract.precondition, "true");
94 snprintf(p->contract.postcondition, sizeof p->contract.postcondition, "true");
95 omega_build_constraint_id(CONST_PRECONDITION, "true", &p->contract.precondition_id);
96 omega_build_constraint_id(CONST_POSTCONDITION, "true", &p->contract.postcondition_id);
97 p->body.has_body = true;
98 p->body.step_count = (uint16_t)n;
99 for (size_t i = 0; i < n; ++i) { p->body.steps[i].op = ops[i]; p->body.steps[i].imm = imms[i]; }
100 return omega_program_compute_id(p);
101}
102
103static int compose2(OmegaProgram *out, OpCode o1, uint64_t i1, OpCode o2, uint64_t i2) {
104 OmegaProgram a, b;
105 char err[160];
106 if (omega_program_build_unary_op(&a, "a", o1, i1) != 0 || omega_program_build_unary_op(&b, "b", o2, i2) != 0)
107 return -1;
108 return omega_program_compose(&a, &b, out, err, sizeof err);
109}
110
111/* ---- the named programs (built through the real builders) ------------------ */
112
113#define NAMED_MAX 24
114static OmegaProgram g_named[NAMED_MAX];
115static const char *g_named_label[NAMED_MAX];
116static size_t g_named_n;
117
118static void add_named(const char *label, int rc, const OmegaProgram *p) {
119 CHECK(G_REAL, rc == 0, "build %s", label);
120 if (rc == 0 && g_named_n < NAMED_MAX) { g_named[g_named_n] = *p; g_named_label[g_named_n++] = label; }
121}
122
123static void build_named(void) {
124 OmegaProgram p;
125 add_named("x+1", omega_program_build_unary_op(&p, "f", OP_ADD, 1), &p);
126 add_named("x+11", omega_program_build_unary_op(&p, "f", OP_ADD, 11), &p);
127 add_named("x-2", omega_program_build_unary_op(&p, "f", OP_SUB, 2), &p);
128 add_named("x*3", omega_program_build_unary_op(&p, "f", OP_MUL, 3), &p);
129 add_named("(x*3)-2", compose2(&p, OP_MUL, 3, OP_SUB, 2), &p);
130 add_named("(x+7)*5", compose2(&p, OP_ADD, 7, OP_MUL, 5), &p);
131 add_named("x&0xFF00FF", omega_program_build_unary_op(&p, "f", OP_AND, 0xFF00FF), &p);
132 add_named("x|0x80000001", omega_program_build_unary_op(&p, "f", OP_OR, 0x80000001u), &p);
133 add_named("x&0xFFFF", omega_program_build_unary_op(&p, "f", OP_AND, 0xFFFF), &p);
134 add_named("x|0x10", omega_program_build_unary_op(&p, "f", OP_OR, 0x10), &p);
135 { /* wide constants: movz + movk up to lsl 48 */
136 uint8_t o[] = { OP_ADD }; uint64_t i[] = { 0x123456789ABCDEF0ull };
137 add_named("x+0x123456789ABCDEF0", make_prog(&p, "w", 64, o, i, 1), &p);
138 uint8_t o2[] = { OP_MUL, OP_AND, OP_OR }; uint64_t i2[] = { 0x100000001ull, 0xFFFF0000FFFF0000ull, 0x0001000000000000ull };
139 add_named("((x*0x100000001)&0xFFFF0000FFFF0000)|1<<48", make_prog(&p, "w", 64, o2, i2, 3), &p);
140 }
141 { /* declared widths below 64 (wrap mod 2^w) */
142 uint8_t o[] = { OP_ADD }; uint64_t i[] = { 1 };
143 add_named("u8 x+1", make_prog(&p, "n", 8, o, i, 1), &p);
144 uint8_t o2[] = { OP_SUB }; uint64_t i2[] = { 2 };
145 add_named("u16 x-2", make_prog(&p, "n", 16, o2, i2, 1), &p);
146 uint8_t o3[] = { OP_MUL, OP_SUB }; uint64_t i3[] = { 3, 2 };
147 add_named("u32 (x*3)-2", make_prog(&p, "n", 32, o3, i3, 2), &p);
148 uint8_t o4[] = { OP_ADD, OP_MUL }; uint64_t i4[] = { 0xFFFFFFFFull, 0xFFFFFFFFull };
149 add_named("u32 (x+max)*max", make_prog(&p, "n", 32, o4, i4, 2), &p);
150 }
151 { /* identity (empty chain) and the 64-step boundary */
152 add_named("u64 x (no steps)", make_prog(&p, "id", 64, NULL, NULL, 0), &p);
153 uint8_t o[64]; uint64_t i[64];
154 for (int k = 0; k < 64; ++k) { o[k] = (uint8_t)(k % 2 ? OP_MUL : OP_ADD); i[k] = 0xFEDCBA9876543210ull ^ (uint64_t)k; }
155 add_named("64-step wide pipeline", make_prog(&p, "long", 64, o, i, 64), &p);
156 }
157}
158
159/* ---- evaluator sanity (hand-derived wraparound facts, not the oracle) ------ */
160
161static void test_evaluator_sanity(void) {
162 OmegaProgram p;
163 uint64_t xs[4], ys[4];
164 omega_program_build_unary_op(&p, "f", OP_ADD, 1);
165 xs[0] = 0; xs[1] = ~0ull;
166 CHECK(G_DIFF, omega_program_eval(&p, xs, 2, ys) == 0 && ys[0] == 1 && ys[1] == 0, "eval x+1 at 0 and wrap at max");
167 omega_program_build_unary_op(&p, "f", OP_SUB, 2);
168 xs[0] = 0; xs[1] = 1;
169 CHECK(G_DIFF, omega_program_eval(&p, xs, 2, ys) == 0 && ys[0] == ~0ull - 1 && ys[1] == ~0ull, "eval x-2 wraps below 0");
170 uint8_t o[] = { OP_ADD }; uint64_t i[] = { 1 };
171 make_prog(&p, "n", 8, o, i, 1);
172 xs[0] = 255; xs[1] = 256;
173 CHECK(G_DIFF, omega_program_eval(&p, xs, 1, ys) == 0 && ys[0] == 0, "eval u8 255+1 == 0");
174 CHECK(G_DIFF, omega_program_eval(&p, xs + 1, 1, ys) != 0, "eval refuses an input outside u8");
175}
176
177/* ---- OMEGA_PROGRAM_REALIZE_PASS ------------------------------------------ */
178
179/* The retired fusion rule of omega_program_compose, kept here only to prove the compiled
180 * composite is byte-identical to it for template parts. */
181static int legacy_splice(const OmegaProgram *a, const OmegaProgram *b, uint8_t *out, size_t *len) {
182 size_t al = a->realization.code_len - 4;
183 memcpy(out, a->realization.code_bytes, al);
184 memcpy(out + al, b->realization.code_bytes, b->realization.code_len);
185 *len = al + b->realization.code_len;
186 return 0;
187}
188
189static void expect_refused(OmegaProgram *p, const char *what, const char *needle) {
190 char why[192] = "";
191 OmegaProgram c = *p;
192 int rc = omega_program_realize_ex(&c, why, sizeof why);
193 CHECK(G_REAL, rc != 0 && !c.is_realized && why[0] && (!needle || strstr(why, needle)),
194 "%s must fail closed with a reason (rc=%d why=%s)", what, rc, why);
195 RealizationSynthesisResult res;
196 rc = omega_synthesize_for_dgx_spark(p, &res);
197 CHECK(G_REAL, rc == -2 && !res.solved && res.realization.code_len == 0 && res.why[0],
198 "%s: synthesizer fails closed too (rc=%d)", what, rc);
199 printf("FAIL_CLOSED %-34s %s\n", what, why);
200}
201
202static void test_realize(void) {
203 for (size_t k = 0; k < g_named_n; ++k) {
204 OmegaProgram p = g_named[k];
205 p.is_realized = false;
206 memset(&p.realization, 0, sizeof p.realization);
207 char why[192] = "";
208 CHECK(G_REAL, omega_program_realize_ex(&p, why, sizeof why) == 0 && p.is_realized && !p.is_verified,
209 "realize %s (%s)", g_named_label[k], why);
210 CHECK(G_REAL, ids_equal(&p.realization.semantic_id, &p.program_id), "%s realization binds program id",
211 g_named_label[k]);
212 RealizationObject r = p.realization;
213 omega_compute_realization_id(&r);
214 CHECK(G_REAL, ids_equal(&r.realization_id, &p.realization.realization_id) && !p.realization.has_machine_id,
215 "%s OMG_R0 id recomputes", g_named_label[k]);
216 VerifyReport rep;
217 CHECK(G_REAL, omega_verify_v0_structural(NULL, &p.realization, &rep) == 0, "%s V0", g_named_label[k]);
218 uint8_t code[AARCH64_MAX_CODE_BYTES];
219 size_t len = 0;
220 CHECK(G_REAL, omega_program_emit_schedule(&p.body, p.contract.input_width, OMEGA_SCHED_SEQUENTIAL, code, &len,
221 sizeof code) == 0 &&
222 len == p.realization.code_len && memcmp(code, p.realization.code_bytes, len) == 0,
223 "%s realize == SEQUENTIAL schedule of its body", g_named_label[k]);
224 /* the builder path now goes through omega_program_realize: same bytes */
225 if (g_named[k].is_realized)
226 CHECK(G_REAL, g_named[k].realization.code_len == p.realization.code_len &&
227 memcmp(g_named[k].realization.code_bytes, p.realization.code_bytes, len) == 0 &&
228 ids_equal(&g_named[k].realization.realization_id, &p.realization.realization_id),
229 "%s builder realization == omega_program_realize", g_named_label[k]);
230 /* realize output executes as the program means */
231 uint64_t xs[OMEGA_REALIZE_DIFF_INPUTS], ys[OMEGA_REALIZE_DIFF_INPUTS];
232 size_t n = omega_realize_differential_inputs(&p, xs, OMEGA_REALIZE_DIFF_INPUTS);
233 int erc = omega_program_eval(&p, xs, n, ys);
234 CHECK(G_REAL, n == OMEGA_REALIZE_DIFF_INPUTS && erc == 0, "%s eval", g_named_label[k]);
235 for (size_t i = 0; erc == 0 && i < n; ++i) {
236 uint64_t got = 0;
237 int rc = omega_program_exec(&p, xs[i], &got);
238 CHECK(G_REAL, rc == 0 && got == ys[i], "%s realize exec x=0x%llx got 0x%llx want 0x%llx (rc=%d)",
239 g_named_label[k], (unsigned long long)xs[i], (unsigned long long)got, (unsigned long long)ys[i], rc);
240 }
241 }
242
243 /* compose now compiles the composite body: byte-identical to the retired splice */
244 OmegaProgram a, b, c;
245 char err[160];
246 for (int t = 0; t < 50; ++t) {
247 static const OpCode ops[] = { OP_ADD, OP_SUB, OP_MUL, OP_AND, OP_OR };
248 omega_program_build_unary_op(&a, "a", ops[rnd() % 5], rnd() & 0xFFFFFFFFull);
249 omega_program_build_unary_op(&b, "b", ops[rnd() % 5], rnd() & (t % 2 ? 0xFFFF : 0xFFFFFFFFull));
250 uint8_t sp[AARCH64_MAX_CODE_BYTES];
251 size_t sl = 0;
252 legacy_splice(&a, &b, sp, &sl);
253 CHECK(G_REAL, omega_program_compose(&a, &b, &c, err, sizeof err) == 0 && c.is_realized &&
254 c.realization.code_len == sl && memcmp(c.realization.code_bytes, sp, sl) == 0,
255 "compose realization == legacy splice bytes (t=%d)", t);
256 }
257
258 /* fail closed, each with a reason */
259 OmegaProgram p;
260 omega_program_init(&p, "empty");
261 expect_refused(&p, "no body", "no semantic body");
262 uint8_t o1[] = { OP_DIV }; uint64_t i1[] = { 3 };
263 make_prog(&p, "div", 64, o1, i1, 1); /* no id: DIV is outside the body op set */
264 p.program_id.bytes[0] = 1;
265 expect_refused(&p, "DIV step", "DIV");
266 uint8_t o2[] = { OP_NOT }; uint64_t i2[] = { 0 };
267 make_prog(&p, "not", 64, o2, i2, 1);
268 expect_refused(&p, "NOT step", "not in V0 body op set");
269 uint8_t o3[] = { OP_ADD }; uint64_t i3[] = { 1 };
270 make_prog(&p, "s", 64, o3, i3, 1);
271 p.contract.input_type = p.contract.output_type = TYPE_SIGNED_INT;
272 omega_program_compute_id(&p);
273 expect_refused(&p, "signed i64", "unsigned integers only");
274 make_prog(&p, "bv", 64, o3, i3, 1);
275 p.contract.input_type = p.contract.output_type = TYPE_BITVECTOR;
276 omega_program_compute_id(&p);
277 expect_refused(&p, "bitvector", "unsigned integers only");
278 make_prog(&p, "w12", 12, o3, i3, 1);
279 expect_refused(&p, "width u12", "width 12");
280 make_prog(&p, "wmix", 64, o3, i3, 1);
281 p.contract.output_width = 32;
282 omega_program_compute_id(&p);
283 expect_refused(&p, "u64 -> u32", "never converts");
284 uint64_t i4[] = { 256 };
285 make_prog(&p, "u8wide", 8, o3, i4, 1);
286 expect_refused(&p, "u8 constant 256", "does not fit");
287 make_prog(&p, "stale", 64, o3, i3, 1);
288 p.body.steps[0].imm = 2; /* body changed after the id was computed */
289 expect_refused(&p, "stale program id", "stale");
290 make_prog(&p, "over", 64, o3, i3, 1);
291 p.body.step_count = OMEGA_PROGRAM_MAX_STEPS + 1;
292 expect_refused(&p, "65 steps", "64 steps");
293 make_prog(&p, "noid", 64, o3, i3, 1);
294 memset(&p.program_id, 0, sizeof p.program_id);
295 expect_refused(&p, "zero program id", "stale");
296}
297
298/* ---- DIFFERENTIAL + PROGRAM_DRIVEN over named and random pipelines --------- */
299
300static OmegaMachineGraph g_spark, g_qemu;
301
302#define POOL_MAX 600
303static RealizationSynthesisResult g_res_s[POOL_MAX], g_res_q[POOL_MAX];
304static OmegaProgram g_pool[POOL_MAX];
305static size_t g_pool_n;
306
307static void diff_one(const OmegaProgram *p, const char *label, RealizationSynthesisResult *rs,
308 RealizationSynthesisResult *rq) {
309 int a = omega_synthesize_for_dgx_spark(p, rs);
310 int b = omega_synthesize_for_qemu_virt(p, rq);
311 CHECK(G_DIFF, a == 0 && rs->solved && rs->inputs_checked == OMEGA_REALIZE_DIFF_INPUTS,
312 "%s spark synth rc=%d %s", label, a, rs->why);
313 CHECK(G_DIFF, b == 0 && rq->solved && rq->inputs_checked == OMEGA_REALIZE_DIFF_INPUTS,
314 "%s qemu synth rc=%d %s", label, b, rq->why);
315 CHECK(G_DRIVEN, rs->schedule == OMEGA_SCHED_PRELOAD && rq->schedule == OMEGA_SCHED_SEQUENTIAL,
316 "%s schedule chosen from machine", label);
317 /* own loop: boundary + extra random inputs in the declared domain */
318 uint16_t w = p->contract.input_width;
319 uint64_t xs[OMEGA_REALIZE_DIFF_INPUTS + 32], ys[OMEGA_REALIZE_DIFF_INPUTS + 32];
320 size_t n = omega_realize_differential_inputs(p, xs, OMEGA_REALIZE_DIFF_INPUTS);
321 for (int i = 0; i < 32; ++i) xs[n++] = rnd() & wmask(w);
322 if (omega_program_eval(p, xs, n, ys) != 0) { CHECK(G_DIFF, 0, "%s eval refused", label); return; }
323 for (size_t i = 0; i < n; ++i) {
324 uint64_t gs = 0, gq = 0;
325 int e1 = omega_exec_native_f3(&rs->realization, xs[i], 0, 0, &gs);
326 int e2 = omega_exec_native_f3(&rq->realization, xs[i], 0, 0, &gq);
327 CHECK(G_DIFF, e1 == 0 && gs == ys[i], "%s spark x=0x%llx native=0x%llx semantic=0x%llx", label,
328 (unsigned long long)xs[i], (unsigned long long)gs, (unsigned long long)ys[i]);
329 CHECK(G_DIFF, e2 == 0 && gq == ys[i], "%s qemu x=0x%llx native=0x%llx semantic=0x%llx", label,
330 (unsigned long long)xs[i], (unsigned long long)gq, (unsigned long long)ys[i]);
331 }
332 /* sequential code of a u64 program lifts back to exactly its body */
333 OmegaProgramBody lifted;
334 if (w == 64)
335 CHECK(G_DRIVEN, omega_program_lift_body(rq->realization.code_bytes, rq->realization.code_len, &lifted) == 0 &&
336 lifted.step_count == p->body.step_count &&
337 memcmp(lifted.steps, p->body.steps, p->body.step_count * sizeof(OmegaProgramStep)) == 0,
338 "%s sequential code lifts back to the program body", label);
339}
340
341static void random_body(OmegaProgram *p, int t) {
342 static const uint16_t widths[] = { 8, 16, 32, 64 };
343 static const uint8_t ops[] = { OP_ADD, OP_SUB, OP_MUL, OP_AND, OP_OR };
344 uint16_t w = widths[rnd() % 4];
345 size_t n = 1 + (size_t)(rnd() % (t % 10 == 0 ? 64 : 8));
346 uint8_t o[64];
347 uint64_t im[64];
348 for (size_t i = 0; i < n; ++i) {
349 o[i] = ops[rnd() % 5];
350 uint64_t v = rnd();
351 switch (rnd() % 4) { /* constant size classes: 16, 32, 48, 64 bits */
352 case 0: v &= 0xFFFF; break;
353 case 1: v &= 0xFFFFFFFFull; break;
354 case 2: v &= 0xFFFFFFFFFFFFull; break;
355 default: break;
356 }
357 im[i] = v & wmask(w);
358 }
359 make_prog(p, "rand", w, o, im, n);
360}
361
362static void test_differential_and_driven(void) {
363 omega_machine_build_dgx_spark(&g_spark);
364 omega_machine_build_qemu_virt(&g_qemu);
365 for (size_t k = 0; k < g_named_n && g_pool_n < POOL_MAX; ++k, ++g_pool_n) {
366 g_pool[g_pool_n] = g_named[k];
367 diff_one(&g_pool[g_pool_n], g_named_label[k], &g_res_s[g_pool_n], &g_res_q[g_pool_n]);
368 }
369 char label[48];
370 for (int t = 0; g_pool_n < POOL_MAX; ++t, ++g_pool_n) {
371 random_body(&g_pool[g_pool_n], t);
372 snprintf(label, sizeof label, "random#%d", t);
373 diff_one(&g_pool[g_pool_n], label, &g_res_s[g_pool_n], &g_res_q[g_pool_n]);
374 }
375
376 /* PROGRAM_DRIVEN: distinct programs -> distinct code and distinct RealizationIds (per
377 * machine and across machines); identical program -> identical realization. */
378 long dup_prog = 0;
379 for (size_t i = 0; i < g_pool_n; ++i)
380 for (size_t j = i + 1; j < g_pool_n; ++j) {
381 if (ids_equal(&g_pool[i].program_id, &g_pool[j].program_id)) { dup_prog++; continue; }
382 CHECK(G_DRIVEN, !ids_equal(&g_res_s[i].realization_id, &g_res_s[j].realization_id) &&
383 !ids_equal(&g_res_q[i].realization_id, &g_res_q[j].realization_id) &&
384 !ids_equal(&g_res_s[i].realization_id, &g_res_q[j].realization_id),
385 "programs %zu/%zu share a RealizationId", i, j);
386 bool same_body = g_pool[i].contract.input_width == g_pool[j].contract.input_width &&
387 g_pool[i].body.step_count == g_pool[j].body.step_count &&
388 memcmp(g_pool[i].body.steps, g_pool[j].body.steps,
389 g_pool[i].body.step_count * sizeof(OmegaProgramStep)) == 0;
390 bool same_code = g_res_q[i].realization.code_len == g_res_q[j].realization.code_len &&
391 memcmp(g_res_q[i].realization.code_bytes, g_res_q[j].realization.code_bytes,
392 g_res_q[i].realization.code_len) == 0;
393 CHECK(G_DRIVEN, same_body || !same_code, "programs %zu/%zu with different bodies got identical code", i, j);
394 }
395 printf("INFO pool=%zu programs, %ld duplicate-program pairs skipped\n", g_pool_n, dup_prog);
396 for (size_t i = 0; i < 20; ++i) {
397 RealizationSynthesisResult again;
398 omega_synthesize_for_dgx_spark(&g_pool[i], &again);
399 CHECK(G_DRIVEN, ids_equal(&again.realization_id, &g_res_s[i].realization_id) &&
400 again.realization.code_len == g_res_s[i].realization.code_len,
401 "deterministic realization %zu", i);
402 CHECK(G_DRIVEN, !ids_equal(&g_res_s[i].realization_id, &g_res_q[i].realization_id),
403 "same program, different machine -> different RealizationId %zu", i);
404 }
405
406 /* The M14 3x-2 program keeps its historical bytes on both machines (goldens unchanged). */
407 static const uint8_t spark_3x2[20] = { 0x61,0x00,0x80,0xd2, 0x42,0x00,0x80,0xd2, 0x00,0x7c,0x01,0x9b,
408 0x00,0x00,0x02,0xcb, 0xc0,0x03,0x5f,0xd6 };
409 static const uint8_t qemu_3x2[20] = { 0x61,0x00,0x80,0xd2, 0x00,0x7c,0x01,0x9b, 0x41,0x00,0x80,0xd2,
410 0x00,0x00,0x01,0xcb, 0xc0,0x03,0x5f,0xd6 };
411 CHECK(G_DRIVEN, g_res_s[4].realization.code_len == 20 && memcmp(g_res_s[4].realization.code_bytes, spark_3x2, 20) == 0,
412 "(x*3)-2 spark bytes == historical preload schedule");
413 CHECK(G_DRIVEN, g_res_q[4].realization.code_len == 20 && memcmp(g_res_q[4].realization.code_bytes, qemu_3x2, 20) == 0,
414 "(x*3)-2 qemu bytes == historical sequential schedule");
415}
416
417/* Sanity demo: the pre-fix synthesizer (fixed f(x)=3x-2 whatever the program), reproduced
418 * here only. PROGRAM_DRIVEN must reject it; it must not pass verification for x+1. */
419static void legacy_fixed_synth(const OmegaProgram *p, const OmegaMachineGraph *mg, RealizationObject *r) {
420 memset(r, 0, sizeof *r);
421 size_t pos = 0;
422 aarch64_emit_movz(r->code_bytes, &pos, sizeof r->code_bytes, true, REG_X1, 3, 0);
423 aarch64_emit_movz(r->code_bytes, &pos, sizeof r->code_bytes, true, REG_X2, 2, 0);
424 aarch64_emit_mul_reg(r->code_bytes, &pos, sizeof r->code_bytes, true, REG_X0, REG_X0, REG_X1);
425 aarch64_emit_sub_reg(r->code_bytes, &pos, sizeof r->code_bytes, true, REG_X0, REG_X0, REG_X2);
426 aarch64_emit_ret(r->code_bytes, &pos, sizeof r->code_bytes);
427 r->code_len = pos;
428 r->semantic_id = p->program_id;
429 r->machine_id = mg->machine_id;
430 r->has_machine_id = true;
431 r->target_profile = mg->target_profile;
432 omega_realize_compute_triple_id(&p->program_id, &mg->machine_id, r, &r->realization_id);
433 r->has_id = true;
434}
435
436static void test_legacy_sanity(void) {
437 size_t rejected = 0, same_code_pairs = 0, pairs = 0;
438 RealizationObject r0, r;
439 legacy_fixed_synth(&g_named[0], &g_spark, &r0);
440 for (size_t k = 0; k < g_named_n; ++k) {
441 legacy_fixed_synth(&g_named[k], &g_spark, &r);
442 char why[192];
443 if (omega_realization_verify_program(&g_named[k], &g_spark, &r, NULL, why, sizeof why) != 0) rejected++;
444 if (k) { pairs++; if (r.code_len == r0.code_len && memcmp(r.code_bytes, r0.code_bytes, r.code_len) == 0) same_code_pairs++; }
445 }
446 /* Only (x*3)-2 itself is legitimately 3x-2; every other program must be rejected. */
447 CHECK(G_DRIVEN, rejected == g_named_n - 1, "legacy fixed synth rejected for %zu of %zu programs", rejected, g_named_n);
448 CHECK(G_DRIVEN, same_code_pairs == pairs, "legacy emits identical code for every program (not program-driven)");
449 printf("SANITY legacy_fixed_3x2_synth PROGRAM_DRIVEN FAIL (expected): identical code for %zu/%zu other programs, "
450 "verification rejects %zu/%zu\n", same_code_pairs, pairs, rejected, g_named_n);
451}
452
453/* ---- TRIPLE_BIND + hostile ------------------------------------------------- */
454
455static void test_triple(void) {
456 char why[192];
457 for (size_t a = 0; a < g_named_n; ++a) {
458 const OmegaProgram *A = &g_named[a];
459 const RealizationObject *RA = &g_res_s[a].realization;
460 SemanticId t;
461 omega_realize_compute_triple_id(&A->program_id, &g_spark.machine_id, RA, &t);
462 CHECK(G_TRIPLE, ids_equal(&t, &RA->realization_id) && ids_equal(&RA->semantic_id, &A->program_id) &&
463 ids_equal(&RA->machine_id, &g_spark.machine_id), "%s triple recomputes", g_named_label[a]);
464 CHECK(G_TRIPLE, omega_realization_verify_program(A, &g_spark, RA, NULL, why, sizeof why) == 0,
465 "%s honest realization verifies (%s)", g_named_label[a], why);
466 /* each component changes the id */
467 RealizationObject m = *RA;
468 m.code_bytes[0] ^= 0x20;
469 SemanticId t2, t3, t4;
470 omega_realize_compute_triple_id(&A->program_id, &g_spark.machine_id, &m, &t2);
471 omega_realize_compute_triple_id(&A->program_id, &g_qemu.machine_id, RA, &t3);
472 omega_realize_compute_triple_id(&g_named[(a + 1) % g_named_n].program_id, &g_spark.machine_id, RA, &t4);
473 CHECK(G_TRIPLE, !ids_equal(&t, &t2) && !ids_equal(&t, &t3) && !ids_equal(&t, &t4),
474 "%s code / machine / semantic each change the id", g_named_label[a]);
475
476 /* altered code bytes, id left stale -> fails */
477 CHECK(G_TRIPLE, omega_realization_verify_program(A, &g_spark, &m, NULL, why, sizeof why) != 0,
478 "%s altered code (stale id) accepted", g_named_label[a]);
479 /* altered code, triple recomputed -> differential fails. Flip the first constant's
480 * low bit (MOVZ imm16 bit 0 = instruction bit 5). */
481 m = *RA;
482 m.code_bytes[0] ^= 0x20;
483 omega_realize_compute_triple_id(&m.semantic_id, &m.machine_id, &m, &m.realization_id);
484 int rc = omega_realization_verify_program(A, &g_spark, &m, NULL, why, sizeof why);
485 if (A->body.step_count > 0)
486 CHECK(G_TRIPLE, rc != 0 && strstr(why, "MISMATCH"), "%s altered code (re-hashed) accepted: %s",
487 g_named_label[a], why);
488 /* altered machine id */
489 m = *RA;
490 m.machine_id = g_qemu.machine_id;
491 CHECK(G_TRIPLE, omega_realization_verify_program(A, &g_spark, &m, NULL, why, sizeof why) != 0,
492 "%s machine id swapped (stale triple) accepted", g_named_label[a]);
493 omega_realize_compute_triple_id(&m.semantic_id, &m.machine_id, &m, &m.realization_id);
494 CHECK(G_TRIPLE, omega_realization_verify_program(A, &g_spark, &m, NULL, why, sizeof why) != 0,
495 "%s realization re-bound to qemu accepted for spark", g_named_label[a]);
496 /* forged machine graph: content changed, id kept */
497 OmegaMachineGraph forged = g_spark;
498 forged.pipeline.issue_width = 8;
499 CHECK(G_TRIPLE, omega_realization_verify_program(A, &forged, RA, NULL, why, sizeof why) != 0,
500 "%s forged machine graph accepted", g_named_label[a]);
501
502 for (size_t b = 0; b < g_named_n; ++b) {
503 if (b == a) continue;
504 const OmegaProgram *B = &g_named[b];
505 /* A's realization verified as B: semantic id mismatch */
506 CHECK(G_TRIPLE, omega_realization_verify_program(B, &g_spark, RA, NULL, why, sizeof why) != 0,
507 "A=%s realization accepted for B=%s", g_named_label[a], g_named_label[b]);
508 /* the discriminating rebinding: A's code stamped with B's id + consistent triple */
509 RealizationObject rb = *RA;
510 rb.semantic_id = B->program_id;
511 omega_realize_compute_triple_id(&rb.semantic_id, &rb.machine_id, &rb, &rb.realization_id);
512 rc = omega_realization_verify_program(B, &g_spark, &rb, NULL, why, sizeof why);
513 CHECK(G_TRIPLE, rc != 0, "A=%s code rebound to B=%s (consistent triple) accepted", g_named_label[a],
514 g_named_label[b]);
515 /* and it is rejected by the semantic differential or the width gate, not luck */
516 CHECK(G_TRIPLE, rc != 0 && (strstr(why, "MISMATCH") || strstr(why, "V0")),
517 "rebinding %s->%s rejected for the wrong reason: %s", g_named_label[a], g_named_label[b], why);
518 }
519 }
520}
521
522int main(void) {
523#if !defined(__aarch64__)
524 fprintf(stderr, "NOTE: not an AArch64 host: native execution checks will FAIL (never skipped)\n");
525#endif
526 build_named();
527 test_evaluator_sanity();
528 test_realize();
529 test_differential_and_driven();
530 test_legacy_sanity();
531 test_triple();
532 int ok = 1;
533 for (int i = 0; i < G_COUNT; i++) {
534 int pass = g_pass[i] == g_total[i] && g_total[i] > 0;
535 if (!pass) ok = 0;
536 printf("GATE %s %s %ld/%ld\n", GATE_NAME[i], pass ? "PASS" : "FAIL", g_pass[i], g_total[i]);
537 }
538 long p = 0, t = 0;
539 for (int i = 0; i < G_COUNT; i++) { p += g_pass[i]; t += g_total[i]; }
540 printf("%s %ld/%ld\n", ok ? "PASS" : "FAIL", p, t);
541 return ok ? 0 : 1;
542}