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 / visor / test_visor_verify.c

193 lines · 10314 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/* Lane 4: visor_verify tests. Prints "PASS n/n" on success. */
18#include <stdio.h>
19#include <string.h>
20
21#include "omega_core.h"
22#include "omega_machine.h"
23#include "omega_program.h"
24#include "omega_realize.h"
25#include "visor_verify.h"
26
27static int g_total, g_pass;
28#define CHECK(cond, msg) do { g_total++; if (cond) g_pass++; else fprintf(stderr, "FAIL: %s (line %d)\n", msg, __LINE__); } while (0)
29
30static char txt_a[8192], txt_b[8192], json_buf[8192];
31
32typedef struct { OmegaGraph *g; SemanticId t, a, b, op, ap; } AddGraph;
33
34static int build_add(AddGraph *x, OpCode opc) {
35 x->g = omega_graph_create();
36 if (!x->g) return -1;
37 OmegaObject *t = omega_build_type_uint(x->g, 64);
38 x->t = t->id;
39 x->a = omega_build_val_uint(x->g, &x->t, 64, 7)->id;
40 x->b = omega_build_val_uint(x->g, &x->t, 64, 11)->id;
41 x->op = omega_build_op_binary(x->g, opc, OVERFLOW_WRAP, &x->t)->id;
42 x->ap = omega_build_apply(x->g, &x->op, &x->a, &x->b)->id;
43 return 0;
44}
45
46int main(void) {
47 VisorVerifyReport r;
48 AddGraph x;
49 CHECK(build_add(&x, OP_ADD) == 0, "build 7+11 graph");
50
51 /* 1. No realization: V0 PASS, V1 NOT_RUN (not an error), overall PASS. */
52 CHECK(visor_verify_object(x.g, &x.ap, NULL, NULL, &r) == 0, "verify apply rc");
53 CHECK(r.row_count == 6, "six rows");
54 CHECK(strcmp(r.rows[0].name, "STRUCTURAL") == 0 && strcmp(r.rows[5].name, "MACHINE") == 0, "row order");
55 CHECK(r.rows[VISOR_ROW_STRUCTURAL].status == VISOR_CHECK_PASS && r.rows[VISOR_ROW_STRUCTURAL].checks > 0, "V0 pass");
56 CHECK(r.rows[VISOR_ROW_TYPE].status == VISOR_CHECK_PASS, "type pass");
57 CHECK(r.rows[VISOR_ROW_INVARIANTS].status == VISOR_CHECK_PASS, "V2 pass");
58 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_NOT_RUN &&
59 strcmp(r.rows[VISOR_ROW_REALIZATION].detail, "no realization built") == 0, "V1 not run w/o realization");
60 CHECK(r.rows[VISOR_ROW_AUTHORITY].status == VISOR_CHECK_PASS && !r.authority_required &&
61 strncmp(r.rows[VISOR_ROW_AUTHORITY].detail, "NONE", 4) == 0, "authority NONE");
62 CHECK(r.rows[VISOR_ROW_MACHINE].status == VISOR_CHECK_NOT_RUN, "machine not run");
63 CHECK(r.passed && r.first_violation[0] == 0, "passed without realization");
64
65 /* Deterministic text: two independent runs render identically. */
66 CHECK(visor_verify_format_text(&r, txt_a, sizeof(txt_a)) > 0, "format text");
67 VisorVerifyReport r2;
68 visor_verify_object(x.g, &x.ap, NULL, NULL, &r2);
69 CHECK(visor_verify_format_text(&r2, txt_b, sizeof(txt_b)) > 0 && strcmp(txt_a, txt_b) == 0, "text deterministic");
70 CHECK(strncmp(txt_a, "STRUCTURAL PASS", 20) == 0, "aligned STRUCTURAL line");
71 CHECK(visor_verify_format_json(&r, json_buf, sizeof(json_buf)) > 0 && json_buf[0] == '{' &&
72 strstr(json_buf, "\"failure\":null") && strstr(json_buf, "\"granted\":false"), "json render");
73 CHECK(visor_verify_format_text(&r, txt_b, 16) == -1, "truncation fails closed");
74
75 /* 2. With a pure-binary ADD realization and a machine model: V1 PASS, MACHINE PASS. */
76 RealizationObject real;
77 OmegaMachineGraph mg;
78 CHECK(omega_realize_pure_binary(x.g, &x.op, &real) == 0, "realize ADD");
79 CHECK(omega_machine_build_dgx_spark(&mg) == 0, "machine model");
80 visor_verify_object(x.g, &x.ap, &real, &mg, &r);
81 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_PASS &&
82 r.rows[VISOR_ROW_REALIZATION].checks >= 11, "V1 differential pass with realization");
83 CHECK(r.rows[VISOR_ROW_MACHINE].status == VISOR_CHECK_PASS, "machine compat pass");
84 CHECK(r.passed, "passed with realization");
85
86 /* 3. Corrupted realization: V1 (which runs V0 on the realization) fails closed. */
87 RealizationObject bad = real;
88 bad.code_bytes[0] ^= 0x01;
89 visor_verify_object(x.g, &x.ap, &bad, &mg, &r);
90 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_FAIL && !r.passed, "corrupt realization fails");
91 CHECK(r.first_violation[0] != 0 && r.object_involved[0] != 0, "failure block filled");
92 CHECK(visor_verify_format_text(&r, txt_a, sizeof(txt_a)) > 0 && strstr(txt_a, "first violation:"), "text failure block");
93
94 /* 4. Machine profile mismatch. */
95 OmegaMachineGraph mg2 = mg;
96 mg2.target_profile = 0x02;
97 visor_verify_object(x.g, &x.ap, &real, &mg2, &r);
98 CHECK(r.rows[VISOR_ROW_MACHINE].status == VISOR_CHECK_FAIL && !r.passed && r.expected[0] && r.observed[0],
99 "machine mismatch fails with expected/observed");
100
101 /* 5. Realization for a different object is refused. */
102 visor_verify_object(x.g, &x.a, &real, NULL, &r);
103 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_FAIL && !r.passed, "binding mismatch fails");
104
105 /* 6. Structural failure: object not in graph. */
106 SemanticId missing;
107 memset(&missing, 0xAB, sizeof(missing));
108 CHECK(visor_verify_object(x.g, &missing, NULL, NULL, &r) == 0, "missing id rc 0");
109 CHECK(r.rows[VISOR_ROW_STRUCTURAL].status == VISOR_CHECK_FAIL && !r.passed &&
110 strcmp(r.observed, "absent") == 0, "missing object fails structural");
111
112 /* 7. Invalid arguments. */
113 CHECK(visor_verify_object(NULL, &x.ap, NULL, NULL, &r) == -1 && !r.passed, "null graph -1");
114
115 /* 8. SUB realization: the V1 reference does not model SUB -> NOT_RUN, not a fake verdict. */
116 AddGraph s;
117 CHECK(build_add(&s, OP_SUB) == 0, "build sub graph");
118 RealizationObject sreal;
119 CHECK(omega_realize_pure_binary(s.g, &s.op, &sreal) == 0, "realize SUB");
120 visor_verify_object(s.g, &s.ap, &sreal, NULL, &r);
121 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_NOT_RUN &&
122 strstr(r.rows[VISOR_ROW_REALIZATION].detail, "not applicable"), "SUB V1 not applicable");
123 CHECK(r.passed && r.rows[VISOR_ROW_REALIZATION].checks > 0, "SUB realization still V0-checked");
124 RealizationObject sbad = sreal;
125 sbad.code_bytes[0] ^= 0x01;
126 visor_verify_object(s.g, &s.ap, &sbad, NULL, &r);
127 CHECK(!r.passed && r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_FAIL &&
128 strstr(r.first_violation, "REALIZATION_ID"), "tampered SUB realization fails closed");
129 /* SUB code re-bound (with a valid realization id) to the ADD op: V1 catches the divergence. */
130 RealizationObject lie = sreal;
131 lie.semantic_id = x.op;
132 omega_compute_realization_id(&lie);
133 visor_verify_object(x.g, &x.ap, &lie, NULL, &r);
134 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_FAIL && !r.passed &&
135 strstr(r.first_violation, "Mismatch") && r.expected[0] && r.observed[0] &&
136 strcmp(r.expected, r.observed) != 0, "V1 mismatch fills expected/observed");
137 omega_graph_destroy(s.g);
138
139 /* 9. Malformed graph: a dangling relation makes V0 fail. */
140 OmegaObject *ap = omega_graph_find_object(x.g, &x.ap);
141 ap->relations[0].kind = REL_DEPENDS_ON;
142 memset(&ap->relations[0].target_id, 0xCD, sizeof(SemanticId));
143 ap->rel_count = 1;
144 CHECK(visor_verify_object(x.g, &x.ap, NULL, NULL, &r) == 0, "malformed rc 0");
145 CHECK(r.rows[VISOR_ROW_STRUCTURAL].status == VISOR_CHECK_FAIL && !r.passed && r.first_violation[0],
146 "malformed graph fails V0");
147 omega_graph_destroy(x.g);
148
149 /* 10. f(a,b,c)=(a+b)-c apply realization: V1 default vectors apply directly. */
150 OmegaGraph *fg = omega_graph_create();
151 SemanticId root;
152 RealizationObject freal;
153 CHECK(fg && omega_build_f_add_sub_graph(fg, &root) == 0 && omega_realize_f_add_sub(fg, &root, &freal) == 0,
154 "build f_add_sub");
155 visor_verify_object(fg, &root, &freal, NULL, &r);
156 CHECK(r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_PASS && r.passed, "f_add_sub V1 pass");
157
158 /* 11. Effect object: authority requirement reported, never granted. */
159 OmegaObject *eff = omega_build_effect(fg, 1, 1, 3, 1);
160 SemanticId eid = eff->id;
161 visor_verify_object(fg, &eid, NULL, NULL, &r);
162 CHECK(r.authority_required && r.effect_count == 1 &&
163 strncmp(r.rows[VISOR_ROW_AUTHORITY].detail, "REQUIRED", 8) == 0, "effect -> REQUIRED");
164 visor_verify_format_json(&r, json_buf, sizeof(json_buf));
165 CHECK(strstr(json_buf, "\"required\":true") && strstr(json_buf, "\"granted\":false"), "json authority");
166 omega_graph_destroy(fg);
167
168 /* 12. Program verify. omega_program_realize is declared in omega_program.h
169 * but has no definition in the tree; omega_program_build_unary_op already
170 * emits the realization and sets is_realized, so that is what we use. */
171 OmegaProgram p;
172 CHECK(omega_program_build_unary_op(&p, "add5", OP_ADD, 5) == 0 && p.is_realized, "build unary program (realized)");
173 CHECK(visor_verify_program(&p, &r) == 0 && r.passed, "program verify pass");
174 CHECK(r.rows[VISOR_ROW_STRUCTURAL].status == VISOR_CHECK_PASS &&
175 r.rows[VISOR_ROW_INVARIANTS].status == VISOR_CHECK_PASS &&
176 r.rows[VISOR_ROW_REALIZATION].status == VISOR_CHECK_NOT_RUN, "program rows");
177 /* Agreement with the existing program verifier. */
178 OmegaProgram copy = p;
179 VerifyReport vr;
180 CHECK(omega_program_verify(&copy, &vr) == 0, "omega_program_verify agrees");
181 OmegaProgram unreal = p;
182 unreal.is_realized = false;
183 visor_verify_program(&unreal, &r);
184 CHECK(!r.passed && strcmp(r.first_violation, "program not realized") == 0, "unrealized program fails closed");
185 OmegaProgram corrupt = p;
186 corrupt.realization.code_bytes[0] ^= 0x01;
187 visor_verify_program(&corrupt, &r);
188 CHECK(!r.passed && r.rows[VISOR_ROW_STRUCTURAL].status == VISOR_CHECK_FAIL, "corrupt program fails V0");
189 omega_program_destroy(&p);
190
191 printf("%s %d/%d\n", g_pass == g_total ? "PASS" : "FAIL", g_pass, g_total);
192 return g_pass == g_total ? 0 : 1;
193}