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
omega-mv / tests / visor / test_visor_semantic.c
| 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_visor_semantic.c -- Omega Visor V1 lane 1: read-only semantic API tests. */ |
| 18 | #include "visor_semantic.h" |
| 19 | #include "visor.h" |
| 20 | #include "omega_core.h" |
| 21 | #include "omega_canonical.h" |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | static int g_pass = 0, g_total = 0; |
| 28 | |
| 29 | #define CHECK(cond) do { \ |
| 30 | g_total++; \ |
| 31 | if (cond) g_pass++; \ |
| 32 | else fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ |
| 33 | } while (0) |
| 34 | |
| 35 | static char txt1[65536], txt2[65536], js1[65536], js2[65536]; |
| 36 | static VisorObjectView v1, v2; |
| 37 | static OmegaGraph snapshot; |
| 38 | |
| 39 | static void recompute(OmegaObject *o) { omega_compute_semantic_id(o); } |
| 40 | |
| 41 | int main(void) { |
| 42 | OmegaGraph *g = omega_graph_create(); |
| 43 | if (!g) return 2; |
| 44 | |
| 45 | OmegaObject *t64 = omega_build_type_uint(g, 64); |
| 46 | OmegaObject *tu8 = omega_build_type_uint(g, 8); |
| 47 | OmegaObject *tbool = omega_build_type_bool(g); |
| 48 | OmegaObject *tbv = omega_build_type_bitvector(g, 32); |
| 49 | OmegaObject *tseq = omega_build_type_sequence(g, &tu8->id, 16); |
| 50 | OmegaObject *v7 = omega_build_val_uint(g, &t64->id, 64, 7); |
| 51 | OmegaObject *v11 = omega_build_val_uint(g, &t64->id, 64, 11); |
| 52 | OmegaObject *vmax = omega_build_val_uint(g, &t64->id, 64, 0xFFFFFFFFFFFFFFFFULL); |
| 53 | OmegaObject *v1o = omega_build_val_uint(g, &t64->id, 64, 1); |
| 54 | OmegaObject *vtrue = omega_build_val_bool(g, &tbool->id, true); |
| 55 | OmegaObject *vbv = omega_build_val_uint(g, &tbv->id, 32, 0xDEADBEEF); |
| 56 | OmegaObject *add = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &t64->id); |
| 57 | OmegaObject *addfc = omega_build_op_binary(g, OP_ADD, OVERFLOW_FAIL_CLOSED, &t64->id); |
| 58 | OmegaObject *ap = omega_build_apply(g, &add->id, &v7->id, &v11->id); |
| 59 | OmegaObject *apwrap = omega_build_apply(g, &add->id, &vmax->id, &v1o->id); |
| 60 | OmegaObject *apnest = omega_build_apply(g, &add->id, &ap->id, &v7->id); |
| 61 | OmegaObject *apfc = omega_build_apply(g, &addfc->id, &vmax->id, &v1o->id); |
| 62 | OmegaObject *apbad = omega_build_apply(g, &add->id, &vtrue->id, &v7->id); |
| 63 | |
| 64 | /* annotated value: attribute, relation, constraint (then re-id as a builder would) */ |
| 65 | OmegaObject *ann = omega_build_val_uint(g, &t64->id, 64, 42); |
| 66 | omega_object_add_attribute(ann, "name", (const uint8_t *)"answer", 6); |
| 67 | uint8_t raw[3] = { 0x00, 0xff, 0x10 }; |
| 68 | omega_object_add_attribute(ann, "blob", raw, 3); |
| 69 | omega_object_add_relation(ann, REL_DERIVED_FROM, &v7->id); |
| 70 | uint8_t range[8] = { 0, 0, 0, 0, 0, 0, 0, 100 }; |
| 71 | omega_object_add_constraint(ann, CONST_RANGE, range, 8); |
| 72 | recompute(ann); |
| 73 | |
| 74 | /* dangling relation */ |
| 75 | SemanticId ghost; |
| 76 | memset(ghost.bytes, 0xAB, sizeof(ghost.bytes)); |
| 77 | OmegaObject *dang = omega_build_val_uint(g, &t64->id, 64, 5); |
| 78 | omega_object_add_relation(dang, REL_DEPENDS_ON, &ghost); |
| 79 | recompute(dang); |
| 80 | |
| 81 | /* depth chain: c0 = 7, c_k = add(c_{k-1}, 1) */ |
| 82 | SemanticId chain[72]; |
| 83 | chain[0] = v7->id; |
| 84 | for (int k = 1; k < 72; ++k) { |
| 85 | OmegaObject *c = omega_build_apply(g, &add->id, &chain[k - 1], &v1o->id); |
| 86 | if (!c) { fprintf(stderr, "FAIL: graph full building chain\n"); return 1; } |
| 87 | chain[k] = c->id; |
| 88 | } |
| 89 | |
| 90 | memcpy(&snapshot, g, sizeof(OmegaGraph)); |
| 91 | SemanticId ap_id_before = ap->id; |
| 92 | |
| 93 | /* 1. known type */ |
| 94 | CHECK(visor_semantic_inspect(g, &t64->id, &v1) == 0); |
| 95 | CHECK(strcmp(v1.kind_name, "TYPE") == 0 && strcmp(v1.type_text, "u64") == 0); |
| 96 | CHECK(v1.canonical_id_matches && v1.canonical_len > 0); |
| 97 | CHECK(visor_semantic_inspect(g, &tseq->id, &v1) == 0 && strcmp(v1.type_text, "seq<u8,16>") == 0); |
| 98 | CHECK(v1.dep_count == 1 && strcmp(v1.deps[0].role, "elem") == 0); |
| 99 | CHECK(visor_semantic_inspect(g, &tbv->id, &v1) == 0 && strcmp(v1.type_text, "bitvector<32>") == 0); |
| 100 | CHECK(visor_semantic_inspect(g, &tbool->id, &v1) == 0 && strcmp(v1.type_text, "bool") == 0); |
| 101 | CHECK(visor_semantic_type_of(g, &t64->id, NULL, NULL, 0) == -1); |
| 102 | |
| 103 | /* 2. known value */ |
| 104 | CHECK(visor_semantic_inspect(g, &v7->id, &v1) == 0); |
| 105 | CHECK(v1.shape == VISOR_SHAPE_VALUE && v1.has_u64 && v1.value_u64 == 7); |
| 106 | CHECK(strcmp(v1.type_text, "u64") == 0 && v1.has_type_id && memcmp(&v1.type_id, &t64->id, 32) == 0); |
| 107 | uint64_t u = 0; |
| 108 | CHECK(visor_semantic_value_u64(g, &v11->id, &u) == 0 && u == 11); |
| 109 | CHECK(visor_semantic_value_u64(g, &vbv->id, &u) == 0 && u == 0xDEADBEEF); |
| 110 | CHECK(visor_semantic_inspect(g, &vtrue->id, &v1) == 0 && v1.value_is_bool && v1.value_u64 == 1); |
| 111 | char tt[64]; |
| 112 | SemanticId tid; |
| 113 | CHECK(visor_semantic_type_of(g, &v7->id, &tid, tt, sizeof(tt)) == 0 && strcmp(tt, "u64") == 0 && |
| 114 | memcmp(&tid, &t64->id, 32) == 0); |
| 115 | CHECK(visor_semantic_value_u64(g, &add->id, &u) == -1); |
| 116 | |
| 117 | /* 3. known operation */ |
| 118 | CHECK(visor_semantic_inspect(g, &add->id, &v1) == 0); |
| 119 | CHECK(v1.shape == VISOR_SHAPE_OPERATION && strcmp(v1.opcode_name, "ADD") == 0 && |
| 120 | strcmp(v1.overflow_name, "WRAP") == 0 && v1.arity == 2); |
| 121 | CHECK(strcmp(v1.type_text, "fn(u64,u64)->u64") == 0); |
| 122 | CHECK(v1.dep_count == 1); /* type/input/output all the same id: deduped */ |
| 123 | CHECK(visor_semantic_type_of(g, &add->id, NULL, tt, sizeof(tt)) == 0 && strcmp(tt, "u64") == 0); |
| 124 | CHECK(visor_semantic_format_text(&v1, txt1, sizeof(txt1)) > 0 && strstr(txt1, "fn(u64,u64)->u64") != NULL && |
| 125 | strstr(txt1, "input0: sha256:") != NULL); |
| 126 | CHECK(visor_semantic_format_json(&v1, js1, sizeof(js1)) > 0 && strstr(js1, "\"inputs\":[") != NULL); |
| 127 | CHECK(visor_semantic_inspect(g, &t64->id, &v1) == 0 && visor_semantic_format_text(&v1, txt1, sizeof(txt1)) > 0 && |
| 128 | strstr(txt1, "tag: 3\n") != NULL && strstr(txt1, "width: 64\n") != NULL); |
| 129 | CHECK(visor_semantic_format_json(&v1, js1, sizeof(js1)) > 0 && strstr(js1, "\"width\":64") != NULL); |
| 130 | |
| 131 | /* 4. apply inspection */ |
| 132 | CHECK(visor_semantic_inspect(g, &ap->id, &v1) == 0); |
| 133 | CHECK(strcmp(v1.kind_name, "OPERATION") == 0 && v1.shape == VISOR_SHAPE_APPLY && |
| 134 | strcmp(v1.shape_name, "APPLY") == 0); |
| 135 | CHECK(memcmp(&v1.apply_op, &add->id, 32) == 0 && v1.operand_count == 2); |
| 136 | CHECK(memcmp(&v1.operands[0], &v7->id, 32) == 0 && memcmp(&v1.operands[1], &v11->id, 32) == 0); |
| 137 | CHECK(strcmp(v1.opcode_name, "ADD") == 0 && strcmp(v1.type_text, "u64") == 0); |
| 138 | CHECK(v1.dep_count == 3 && strcmp(v1.deps[0].role, "op") == 0 && strcmp(v1.deps[2].role, "operand1") == 0); |
| 139 | SemanticId opid, operands[4]; |
| 140 | size_t cnt = 0; |
| 141 | CHECK(visor_semantic_apply_parts(g, &ap->id, &opid, operands, &cnt) == 0 && cnt == 2 && |
| 142 | memcmp(&opid, &add->id, 32) == 0 && memcmp(&operands[1], &v11->id, 32) == 0); |
| 143 | CHECK(visor_semantic_apply_parts(g, &v7->id, &opid, operands, &cnt) == -1); |
| 144 | CHECK(visor_semantic_type_of(g, &ap->id, &tid, tt, sizeof(tt)) == 0 && strcmp(tt, "u64") == 0); |
| 145 | |
| 146 | /* 5. relation traversal + attributes + 6. constraint display */ |
| 147 | CHECK(visor_semantic_inspect(g, &ann->id, &v1) == 0); |
| 148 | CHECK(v1.attr_count == 2 && strcmp(v1.attrs[0].key, "name") == 0 && v1.attrs[0].is_utf8 && |
| 149 | strcmp(v1.attrs[1].key, "blob") == 0 && !v1.attrs[1].is_utf8); |
| 150 | CHECK(v1.rel_count == 1 && strcmp(v1.rels[0].kind_name, "DERIVED_FROM") == 0 && v1.rels[0].resolved); |
| 151 | CHECK(v1.dep_count == 2 && memcmp(&v1.deps[0].id, &v7->id, 32) == 0 && |
| 152 | strcmp(v1.deps[0].role, "rel:DERIVED_FROM") == 0 && strcmp(v1.deps[1].role, "type") == 0); |
| 153 | CHECK(v1.const_count == 1 && strcmp(v1.consts[0].kind_name, "RANGE") == 0 && v1.consts[0].len == 8); |
| 154 | CHECK(visor_semantic_format_text(&v1, txt1, sizeof(txt1)) > 0); |
| 155 | CHECK(strstr(txt1, "RANGE len=8 hex:0000000000000064") != NULL); |
| 156 | CHECK(strstr(txt1, "name = hex:616e73776572 utf8:\"answer\"") != NULL); |
| 157 | CHECK(strstr(txt1, "blob = hex:00ff10\n") != NULL); |
| 158 | CHECK(strstr(txt1, "DERIVED_FROM sha256:") != NULL); |
| 159 | CHECK(visor_semantic_format_json(&v1, js1, sizeof(js1)) > 0); |
| 160 | CHECK(strstr(js1, "\"constraints\":[{\"kind\":\"RANGE\",\"len\":8,\"hex\":\"0000000000000064\"}]") != NULL); |
| 161 | CHECK(visor_semantic_graph_text(g, &ann->id, txt2, sizeof(txt2)) > 0); |
| 162 | { |
| 163 | char h7[65], h64[65]; |
| 164 | omega_hex_semantic_id(&v7->id, h7); |
| 165 | omega_hex_semantic_id(&t64->id, h64); |
| 166 | char *p7 = strstr(txt2, h7), *p64 = strstr(txt2, h64); |
| 167 | CHECK(p7 && p64 && p7 < p64); /* relation target before payload type */ |
| 168 | CHECK(strstr(p64 + 1, h64) == NULL); /* t64 reachable twice, printed once */ |
| 169 | CHECK(strstr(txt2, " VALUE sha256:") != NULL); /* indented child */ |
| 170 | } |
| 171 | CHECK(visor_semantic_graph_text(g, &apnest->id, txt2, sizeof(txt2)) > 0); |
| 172 | CHECK(strncmp(txt2, "OPERATION sha256:", 17) == 0 && strstr(txt2, "apply ADD(") != NULL && |
| 173 | strstr(txt2, "op ADD WRAP arity=2 -> u64") != NULL && strstr(txt2, "value 11 : u64") != NULL); |
| 174 | |
| 175 | /* dangling relation: reported, not an error for inspect; MISSING in graph text */ |
| 176 | CHECK(visor_semantic_inspect(g, &dang->id, &v1) == 0 && v1.dangling_count == 1 && !v1.rels[0].resolved); |
| 177 | CHECK(visor_semantic_format_text(&v1, txt1, sizeof(txt1)) > 0 && strstr(txt1, "(DANGLING)") != NULL); |
| 178 | CHECK(visor_semantic_graph_text(g, &dang->id, txt2, sizeof(txt2)) > 0 && strstr(txt2, "MISSING sha256:abab") != NULL); |
| 179 | |
| 180 | /* 7. missing SemanticId fails closed */ |
| 181 | CHECK(visor_semantic_inspect(g, &ghost, &v1) == -1); |
| 182 | CHECK(visor_semantic_type_of(g, &ghost, &tid, tt, sizeof(tt)) == -1); |
| 183 | CHECK(visor_semantic_eval_u64(g, &ghost, &u) == -1); |
| 184 | CHECK(visor_semantic_graph_text(g, &ghost, txt2, sizeof(txt2)) == -1); |
| 185 | CHECK(visor_semantic_apply_parts(g, &ghost, &opid, operands, &cnt) == -1); |
| 186 | CHECK(visor_semantic_value_u64(g, &ghost, &u) == -1); |
| 187 | CHECK(visor_semantic_inspect(NULL, &v7->id, &v1) == -1 && visor_semantic_inspect(g, NULL, &v1) == -1); |
| 188 | |
| 189 | /* 8. stable ordering: inspect twice -> identical text and json */ |
| 190 | CHECK(visor_semantic_inspect(g, &ann->id, &v1) == 0 && visor_semantic_inspect(g, &ann->id, &v2) == 0); |
| 191 | CHECK(visor_semantic_format_text(&v1, txt1, sizeof(txt1)) > 0 && visor_semantic_format_text(&v2, txt2, sizeof(txt2)) > 0); |
| 192 | CHECK(strcmp(txt1, txt2) == 0); |
| 193 | CHECK(visor_semantic_format_json(&v1, js1, sizeof(js1)) > 0 && visor_semantic_format_json(&v2, js2, sizeof(js2)) > 0); |
| 194 | CHECK(strcmp(js1, js2) == 0 && js1[0] == '{' && js1[strlen(js1) - 1] == '}'); |
| 195 | CHECK(visor_semantic_inspect(g, &ap->id, &v1) == 0 && visor_semantic_format_json(&v1, js1, sizeof(js1)) > 0); |
| 196 | CHECK(visor_semantic_inspect(g, &ap->id, &v2) == 0 && visor_semantic_format_json(&v2, js2, sizeof(js2)) > 0); |
| 197 | CHECK(strcmp(js1, js2) == 0); |
| 198 | /* too-small buffer: -1, never a truncated result */ |
| 199 | CHECK(visor_semantic_format_text(&v1, txt1, 40) == -1 && txt1[0] == '\0'); |
| 200 | CHECK(visor_semantic_format_json(&v1, js1, 40) == -1); |
| 201 | |
| 202 | /* 10. eval */ |
| 203 | CHECK(visor_semantic_eval_u64(g, &ap->id, &u) == 0 && u == 18); |
| 204 | CHECK(visor_semantic_eval_u64(g, &apwrap->id, &u) == 0 && u == 0); |
| 205 | CHECK(visor_semantic_eval_u64(g, &apnest->id, &u) == 0 && u == 25); |
| 206 | CHECK(visor_semantic_eval_u64(g, &v11->id, &u) == 0 && u == 11); |
| 207 | CHECK(visor_semantic_eval_u64(g, &apfc->id, &u) == -3); /* FAIL_CLOSED overflow refused */ |
| 208 | CHECK(visor_semantic_eval_u64(g, &apbad->id, &u) == -1); /* bool operand into u64 ADD */ |
| 209 | CHECK(visor_semantic_eval_u64(g, &add->id, &u) == -1); /* bare operation not evaluable */ |
| 210 | CHECK(visor_semantic_eval_u64(g, &chain[60], &u) == 0 && u == 67); |
| 211 | CHECK(visor_semantic_eval_u64(g, &chain[71], &u) == -1); /* depth > 64 fails closed */ |
| 212 | |
| 213 | /* 9. canonical id unchanged by inspection; whole graph unchanged */ |
| 214 | CHECK(memcmp(&ap->id, &ap_id_before, sizeof(SemanticId)) == 0); |
| 215 | CHECK(memcmp(&snapshot, g, sizeof(OmegaGraph)) == 0); |
| 216 | |
| 217 | /* 11. malformed objects: reported, never dereferenced */ |
| 218 | OmegaGraph *m = omega_graph_create(); |
| 219 | OmegaObject *mt = omega_build_type_uint(m, 64); |
| 220 | OmegaObject *mv = omega_build_val_uint(m, &mt->id, 64, 9); |
| 221 | OmegaObject *mk = omega_build_val_uint(m, &mt->id, 64, 10); |
| 222 | OmegaObject *mid = omega_build_val_uint(m, &mt->id, 64, 12); |
| 223 | OmegaObject *mop = omega_build_op_binary(m, OP_ADD, OVERFLOW_WRAP, &mt->id); |
| 224 | OmegaObject *mbig = omega_build_val_uint(m, &mt->id, 64, 13); |
| 225 | OmegaObject *map = omega_build_apply(m, &mop->id, &mv->id, &mv->id); |
| 226 | OmegaObject *mdef = omega_build_op_binary(m, OP_ADD, OVERFLOW_DEFAULT, &mt->id); |
| 227 | CHECK(visor_semantic_inspect(m, &mdef->id, &v1) == -2 && strstr(v1.malformed_reason, "overflow") != NULL); |
| 228 | mv->payload_len = 3; /* wrong payload_len */ |
| 229 | mk->kind = (SemanticKind)0x7F; /* invalid kind */ |
| 230 | mid->payload[40] ^= 0x01; /* tampered after id computed */ |
| 231 | mbig->payload_len = 5000; /* beyond OMEGA_MAX_PAYLOAD_LEN */ |
| 232 | CHECK(visor_semantic_inspect(m, &mv->id, &v1) == -2 && v1.status == -2 && |
| 233 | strstr(v1.malformed_reason, "VALUE payload_len 3") != NULL); |
| 234 | CHECK(visor_semantic_format_text(&v1, txt1, sizeof(txt1)) > 0 && strstr(txt1, "status: MALFORMED") != NULL); |
| 235 | CHECK(visor_semantic_format_json(&v1, js1, sizeof(js1)) > 0 && strstr(js1, "\"status\":\"malformed\"") != NULL); |
| 236 | CHECK(visor_semantic_inspect(m, &mk->id, &v1) == -2 && strstr(v1.malformed_reason, "invalid kind 0x7f") != NULL); |
| 237 | CHECK(visor_semantic_inspect(m, &mid->id, &v1) == -2 && strstr(v1.malformed_reason, "stored id") != NULL); |
| 238 | CHECK(visor_semantic_inspect(m, &mbig->id, &v1) == -2 && strstr(v1.malformed_reason, "exceeds") != NULL); |
| 239 | CHECK(visor_semantic_value_u64(m, &mv->id, &u) == -2); |
| 240 | CHECK(visor_semantic_type_of(m, &mk->id, &tid, tt, sizeof(tt)) == -2); |
| 241 | CHECK(visor_semantic_eval_u64(m, &map->id, &u) == -2); /* operand malformed -> fails closed */ |
| 242 | CHECK(visor_semantic_graph_text(m, &map->id, txt2, sizeof(txt2)) > 0 && strstr(txt2, "MALFORMED sha256:") != NULL); |
| 243 | omega_graph_destroy(m); |
| 244 | |
| 245 | /* shared helpers coexist with visor.c */ |
| 246 | char idt[72]; |
| 247 | visor_format_id(&v7->id, idt); |
| 248 | CHECK(visor_semantic_inspect(g, &v7->id, &v1) == 0 && strcmp(idt, v1.id_text) == 0); |
| 249 | |
| 250 | omega_graph_destroy(g); |
| 251 | printf("PASS %d/%d\n", g_pass, g_total); |
| 252 | return g_pass == g_total ? 0 : 1; |
| 253 | } |