Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/visor/visor_verify.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / visor / visor_verify.c` 525 lines ยท 24419 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 /* Omega Visor V1 - lane 4: structured verification report over the existing 18 * verifier. See visor_verify.h for the row contract. */ 19 #include "visor_verify.h" 20 21 #include 22 #include 23 #include 24 25 #include "omega_canonical.h" 26 #include "omega_core.h" 27 #include "omega_validate.h" 28 #include "omega_verify.h" 29 30 static const char *const k_row_names[VISOR_ROW_COUNT] = { 31 "STRUCTURAL", "TYPE", "INVARIANTS", "REALIZATION", "AUTHORITY", "MACHINE" 32 }; 33 34 const char *visor_check_status_name(VisorCheckStatus s) { 35 switch (s) { 36 case VISOR_CHECK_PASS: return "PASS"; 37 case VISOR_CHECK_FAIL: return "FAIL"; 38 case VISOR_CHECK_NOT_RUN: return "NOT_RUN"; 39 } 40 return "UNKNOWN"; 41 } 42 43 static const char *visor_kind_name_local(SemanticKind k) { 44 static const char *const names[] = { 45 "INVALID", "VALUE", "TYPE", "OPERATION", "RELATION", "CONSTRAINT", "MEMORY", 46 "MACHINE", "EFFECT", "REALIZATION", "EVIDENCE", "PROOF" 47 }; 48 return ((unsigned)k < sizeof(names) / sizeof(names[0])) ? names[k] : "INVALID"; 49 } 50 51 static void fmt_id(const SemanticId *id, char out[72]) { 52 char hex[65]; 53 omega_hex_semantic_id(id, hex); 54 snprintf(out, 72, "sha256:%s", hex); 55 } 56 57 static void report_reset(VisorVerifyReport *out) { 58 memset(out, 0, sizeof(*out)); 59 out->row_count = VISOR_ROW_COUNT; 60 for (size_t i = 0; i < VISOR_ROW_COUNT; ++i) { 61 out->rows[i].name = k_row_names[i]; 62 out->rows[i].status = VISOR_CHECK_NOT_RUN; 63 } 64 } 65 66 static void row_set(VisorVerifyReport *out, int row, VisorCheckStatus st, 67 uint32_t checks, uint32_t fails, const char *fmt, ...) { 68 VisorVerifyRow *r = &out->rows[row]; 69 r->status = st; 70 r->checks = checks; 71 r->fails = fails; 72 va_list ap; 73 va_start(ap, fmt); 74 vsnprintf(r->detail, sizeof(r->detail), fmt, ap); 75 va_end(ap); 76 } 77 78 /* Record the first violation only; later failures keep their row detail. */ 79 static void note_violation(VisorVerifyReport *out, const char *violation, 80 const SemanticId *object_or_null, 81 const char *expected, const char *observed) { 82 if (out->first_violation[0]) return; 83 snprintf(out->first_violation, sizeof(out->first_violation), "%.255s", violation ? violation : ""); 84 if (object_or_null) fmt_id(object_or_null, out->object_involved); 85 snprintf(out->expected, sizeof(out->expected), "%.127s", expected ? expected : ""); 86 snprintf(out->observed, sizeof(out->observed), "%.127s", observed ? observed : ""); 87 } 88 89 static void finalize(VisorVerifyReport *out) { 90 bool any_ran = false, any_fail = false; 91 for (size_t i = 0; i < out->row_count; ++i) { 92 if (out->rows[i].status == VISOR_CHECK_PASS) any_ran = true; 93 if (out->rows[i].status == VISOR_CHECK_FAIL) any_fail = true; 94 } 95 out->passed = any_ran && !any_fail; 96 if (!out->passed && !out->first_violation[0]) 97 note_violation(out, "no check ran", NULL, "at least one executed check", "none"); 98 } 99 100 /* --- graph shape helpers (codec convention: >= OperationPayload is an 101 * operation, else >= ApplyPayload is an apply) --- */ 102 static const OperationPayload *as_operation(const OmegaObject *o) { 103 if (!o || o->kind != KIND_OPERATION || o->payload_len < sizeof(OperationPayload)) return NULL; 104 return (const OperationPayload *)o->payload; 105 } 106 static const ApplyPayload *as_apply(const OmegaObject *o) { 107 if (!o || o->kind != KIND_OPERATION || o->payload_len >= sizeof(OperationPayload) || 108 o->payload_len < sizeof(ApplyPayload)) return NULL; 109 return (const ApplyPayload *)o->payload; 110 } 111 static bool id_eq(const SemanticId *a, const SemanticId *b) { 112 return memcmp(a->bytes, b->bytes, OMEGA_ID_BYTES) == 0; 113 } 114 static bool id_zero(const SemanticId *a) { 115 for (size_t i = 0; i < OMEGA_ID_BYTES; ++i) if (a->bytes[i]) return false; 116 return true; 117 } 118 /* Value object -> uint64 (little-endian bytes, up to 8), if it is one. */ 119 static bool value_u64(const OmegaGraph *g, const SemanticId *id, uint64_t *out) { 120 const OmegaObject *o = omega_graph_find_object_const(g, id); 121 if (!o || o->kind != KIND_VALUE || o->payload_len < sizeof(ValuePayload)) return false; 122 const ValuePayload *vp = (const ValuePayload *)o->payload; 123 if (vp->byte_len == 0 || vp->byte_len > 8) return false; 124 uint64_t v = 0; 125 for (uint16_t i = 0; i < vp->byte_len; ++i) v |= (uint64_t)vp->bytes[i] << (8u * i); 126 *out = v; 127 return true; 128 } 129 130 /* --- AUTHORITY: requirement walk (reports, never grants) --- */ 131 static int slot_of(const OmegaGraph *g, const SemanticId *id) { 132 if (id_zero(id)) return -1; 133 for (uint16_t i = 0; i < g->object_count && i < OMEGA_MAX_GRAPH_OBJECTS; ++i) 134 if (g->objects[i].has_id && id_eq(&g->objects[i].id, id)) return (int)i; 135 return -1; 136 } 137 138 static void authority_walk(const OmegaGraph *g, int root, uint32_t *walked, 139 uint32_t *effects, uint32_t *caprefs) { 140 bool visited[OMEGA_MAX_GRAPH_OBJECTS]; 141 int stack[OMEGA_MAX_GRAPH_OBJECTS]; 142 size_t sp = 0; 143 memset(visited, 0, sizeof(visited)); 144 *walked = *effects = *caprefs = 0; 145 if (root < 0) return; 146 stack[sp++] = root; 147 visited[root] = true; 148 while (sp > 0) { 149 const OmegaObject *o = &g->objects[stack[--sp]]; 150 (*walked)++; 151 SemanticId next[OMEGA_MAX_RELATIONS + 8]; 152 size_t nn = 0; 153 if (o->kind == KIND_EFFECT) { 154 (*effects)++; 155 EffectPayload ep; 156 if (omega_effect_read(o, &ep) == OMEGA_EFFECT_OK && !id_zero(&ep.capability_ref)) { 157 (*caprefs)++; 158 next[nn++] = ep.capability_ref; 159 } 160 } else if (o->kind == KIND_TYPE && o->payload_len >= sizeof(TypePayload)) { 161 const TypePayload *tp = (const TypePayload *)o->payload; 162 if (tp->tag == TYPE_CAPABILITY_REF || tp->tag == TYPE_EFFECT_INTENT_REF || 163 tp->tag == TYPE_EFFECT_RECEIPT_REF) (*caprefs)++; 164 if (tp->tag == TYPE_SEQUENCE) next[nn++] = tp->elem_type; 165 } else if (o->kind == KIND_VALUE && o->payload_len >= sizeof(ValuePayload)) { 166 next[nn++] = ((const ValuePayload *)o->payload)->type_id; 167 } else if (as_operation(o)) { 168 const OperationPayload *op = as_operation(o); 169 next[nn++] = op->type_id; 170 next[nn++] = op->output_type; 171 for (int i = 0; i < 4; ++i) next[nn++] = op->input_types[i]; 172 } else if (as_apply(o)) { 173 const ApplyPayload *ap = as_apply(o); 174 next[nn++] = ap->op_id; 175 for (int i = 0; i < 4; ++i) next[nn++] = ap->operands[i]; 176 } 177 for (uint16_t i = 0; i < o->rel_count && i < OMEGA_MAX_RELATIONS && nn < sizeof(next) / sizeof(next[0]); ++i) 178 next[nn++] = o->relations[i].target_id; 179 for (size_t i = 0; i < nn; ++i) { 180 int s = slot_of(g, &next[i]); 181 if (s >= 0 && !visited[s] && sp < OMEGA_MAX_GRAPH_OBJECTS) { visited[s] = true; stack[sp++] = s; } 182 } 183 } 184 } 185 186 static void authority_row(const OmegaGraph *g, int root, VisorVerifyReport *out) { 187 uint32_t walked = 0, eff = 0, cap = 0; 188 authority_walk(g, root, &walked, &eff, &cap); 189 out->effect_count = eff; 190 out->capability_ref_count = cap; 191 out->authority_required = (eff + cap) > 0; 192 if (out->authority_required) 193 row_set(out, VISOR_ROW_AUTHORITY, VISOR_CHECK_PASS, walked, 0, 194 "REQUIRED: %u effect(s), %u capability ref(s) reachable; visor grants nothing", 195 eff, cap); 196 else 197 row_set(out, VISOR_ROW_AUTHORITY, VISOR_CHECK_PASS, walked, 0, 198 "NONE: no effect or capability ref reachable (%u object(s) walked)", walked); 199 } 200 201 /* Pull expected=/observed= numbers out of an existing V1 error_detail. */ 202 static void v1_expected_observed(const char *detail, char exp[128], char obs[128]) { 203 const char *e = strstr(detail, "expected="); 204 const char *o = strstr(detail, "observed="); 205 unsigned long ev = 0, ov = 0; 206 if (e && sscanf(e, "expected=%lu", &ev) == 1) snprintf(exp, 128, "%lu", ev); else exp[0] = 0; 207 if (o && sscanf(o, "observed=%lu", &ov) == 1) snprintf(obs, 128, "%lu", ov); else obs[0] = 0; 208 } 209 210 static void realization_row(const OmegaGraph *g, const SemanticId *id, const OmegaObject *obj, 211 const RealizationObject *real, VisorVerifyReport *out) { 212 if (!real) { 213 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_NOT_RUN, 0, 0, "no realization built"); 214 return; 215 } 216 const ApplyPayload *subject_apply = as_apply(obj); 217 bool bound = id_eq(&real->semantic_id, id) || 218 (subject_apply && id_eq(&real->semantic_id, &subject_apply->op_id)); 219 if (!bound) { 220 char exp[72], obs[72]; 221 fmt_id(id, exp); 222 fmt_id(&real->semantic_id, obs); 223 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_FAIL, 1, 1, 224 "realization is bound to a different semantic object"); 225 note_violation(out, "realization is bound to a different semantic object", id, exp, obs); 226 return; 227 } 228 const OmegaObject *target = omega_graph_find_object_const(g, &real->semantic_id); 229 const OperationPayload *top = as_operation(target); 230 const ApplyPayload *tap = as_apply(target); 231 232 uint64_t vec[3 * 16]; 233 size_t triples = 0; 234 const char *model = NULL; 235 if (top && top->opcode == OP_ADD) { 236 /* Pure-binary ADD realization computes X0+X1; the existing V1 reference 237 * is (a+b)-c, so every triple carries c=0 and both sides mean a+b. */ 238 static const uint64_t base[][2] = { 239 {0, 0}, {0, 1}, {1, 0}, {1, 1}, {UINT64_MAX, 0}, {UINT64_MAX, 1}, 240 {1, UINT64_MAX}, {UINT64_MAX, UINT64_MAX}, {7, 11} 241 }; 242 for (size_t i = 0; i < sizeof(base) / sizeof(base[0]); ++i) { 243 vec[triples * 3] = base[i][0]; vec[triples * 3 + 1] = base[i][1]; vec[triples * 3 + 2] = 0; 244 triples++; 245 } 246 uint64_t a = 0, b = 0; 247 if (subject_apply && value_u64(g, &subject_apply->operands[0], &a) && 248 value_u64(g, &subject_apply->operands[1], &b)) { 249 vec[triples * 3] = a; vec[triples * 3 + 1] = b; vec[triples * 3 + 2] = 0; triples++; 250 vec[triples * 3] = b; vec[triples * 3 + 1] = a; vec[triples * 3 + 2] = 0; triples++; 251 } 252 model = "V1 differential, OP_ADD with c=0"; 253 } else if (tap) { 254 /* Only SUB(ADD(a,b),c) matches the V1 reference model as-is. */ 255 const OperationPayload *outer = as_operation(omega_graph_find_object_const(g, &tap->op_id)); 256 const ApplyPayload *inner = as_apply(omega_graph_find_object_const(g, &tap->operands[0])); 257 const OperationPayload *inner_op = inner ? as_operation(omega_graph_find_object_const(g, &inner->op_id)) : NULL; 258 if (outer && outer->opcode == OP_SUB && inner_op && inner_op->opcode == OP_ADD) 259 model = "V1 differential, (a+b)-c default vectors"; 260 } 261 /* Always check the supplied realization's structure (profile, bounds, 262 * decode, RET, realization id) so tampered code never passes unchecked. */ 263 VerifyReport v0r; 264 if (omega_verify_v0_structural(g, real, &v0r) != 0 || !v0r.passed) { 265 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_FAIL, v0r.check_count, 266 v0r.fail_count ? v0r.fail_count : 1, "%.250s", v0r.error_detail); 267 note_violation(out, v0r.error_detail, &real->semantic_id, "realization passes V0 structural", v0r.error_detail); 268 return; 269 } 270 if (!model) { 271 if (top) 272 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_NOT_RUN, v0r.check_count, 0, 273 "V0 realization structure PASS (%u checks); V1 NOT_RUN: existing reference is (a+b)-c, " 274 "not applicable to op 0x%02x", v0r.check_count, (unsigned)top->opcode); 275 else 276 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_NOT_RUN, v0r.check_count, 0, 277 "V0 realization structure PASS (%u checks); V1 NOT_RUN: existing reference is (a+b)-c, " 278 "subject shape not covered", v0r.check_count); 279 return; 280 } 281 VerifyReport v1; 282 int rc = omega_verify_v1_differential(g, real, triples ? vec : NULL, triples, &v1); 283 if (rc == 0 && v1.passed) { 284 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_PASS, v1.check_count, v1.fail_count, 285 "%s (%u check(s), incl. V0 realization id)", model, v1.check_count); 286 } else { 287 char exp[128], obs[128]; 288 v1_expected_observed(v1.error_detail, exp, obs); 289 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_FAIL, v1.check_count, 290 v1.fail_count ? v1.fail_count : 1, "%.250s", v1.error_detail); 291 note_violation(out, v1.error_detail, &real->semantic_id, exp, obs); 292 } 293 } 294 295 static void machine_row(const RealizationObject *real, const OmegaMachineGraph *mg, 296 VisorVerifyReport *out) { 297 if (!real) { row_set(out, VISOR_ROW_MACHINE, VISOR_CHECK_NOT_RUN, 0, 0, "no realization built"); return; } 298 if (!mg) { row_set(out, VISOR_ROW_MACHINE, VISOR_CHECK_NOT_RUN, 0, 0, "no machine model"); return; } 299 uint32_t checks = 1; 300 if (real->target_profile != mg->target_profile) { 301 char e[128], o[128]; 302 snprintf(e, sizeof(e), "target profile 0x%02x (%.60s)", mg->target_profile, mg->name); 303 snprintf(o, sizeof(o), "realization profile 0x%02x", real->target_profile); 304 row_set(out, VISOR_ROW_MACHINE, VISOR_CHECK_FAIL, 1, 1, "profile mismatch: %s vs %s", o, e); 305 note_violation(out, "realization target profile does not match machine model", NULL, e, o); 306 return; 307 } 308 if (real->has_machine_id) { 309 checks++; 310 if (!id_eq(&real->machine_id, &mg->machine_id)) { 311 char e[72], o[72]; 312 fmt_id(&mg->machine_id, e); 313 fmt_id(&real->machine_id, o); 314 row_set(out, VISOR_ROW_MACHINE, VISOR_CHECK_FAIL, checks, 1, 315 "realization bound to a different machine id"); 316 note_violation(out, "realization bound to a different machine id", NULL, e, o); 317 return; 318 } 319 } 320 row_set(out, VISOR_ROW_MACHINE, VISOR_CHECK_PASS, checks, 0, 321 "profile 0x%02x matches %.60s%s", real->target_profile, mg->name, 322 real->has_machine_id ? "; machine id bound and equal" : "; realization not bound to a machine id"); 323 } 324 325 static void invariants_row(VisorVerifyReport *out) { 326 VerifyReport v2; 327 if (omega_verify_v2_properties(NULL, NULL, &v2) == 0 && v2.passed) { 328 row_set(out, VISOR_ROW_INVARIANTS, VISOR_CHECK_PASS, v2.check_count, v2.fail_count, 329 "V2 reference-evaluator algebra (commutativity, identity, wrap, range); not object-specific"); 330 } else { 331 row_set(out, VISOR_ROW_INVARIANTS, VISOR_CHECK_FAIL, v2.check_count, 332 v2.fail_count ? v2.fail_count : 1, "%.250s", v2.error_detail); 333 note_violation(out, v2.error_detail, NULL, "V2 properties hold", v2.error_detail); 334 } 335 } 336 337 int visor_verify_object(const OmegaGraph *g, const SemanticId *id, 338 const RealizationObject *real_or_null, 339 const OmegaMachineGraph *mg_or_null, 340 VisorVerifyReport *out) { 341 if (!out) return -1; 342 report_reset(out); 343 if (!g || !id || g->object_count > OMEGA_MAX_GRAPH_OBJECTS) { 344 note_violation(out, "invalid arguments or malformed graph header", NULL, "", ""); 345 return -1; 346 } 347 const OmegaObject *obj = omega_graph_find_object_const(g, id); 348 if (!obj) { 349 row_set(out, VISOR_ROW_STRUCTURAL, VISOR_CHECK_FAIL, 1, 1, "object not present in graph"); 350 note_violation(out, "object not present in graph", id, "object present", "absent"); 351 finalize(out); 352 return 0; 353 } 354 355 /* STRUCTURAL: V0 over the graph (realization structure is covered by V1). */ 356 VerifyReport v0; 357 if (omega_verify_v0_structural(g, NULL, &v0) == 0 && v0.passed) { 358 row_set(out, VISOR_ROW_STRUCTURAL, VISOR_CHECK_PASS, v0.check_count, v0.fail_count, 359 "V0 graph typing + DAG integrity (%u object(s))", (unsigned)g->object_count); 360 } else { 361 row_set(out, VISOR_ROW_STRUCTURAL, VISOR_CHECK_FAIL, v0.check_count, 362 v0.fail_count ? v0.fail_count : 1, "%.250s", v0.error_detail); 363 note_violation(out, v0.error_detail, NULL, "graph passes V0 structural", v0.error_detail); 364 } 365 366 /* TYPE: the queried object alone. */ 367 char err[256] = {0}; 368 if (omega_validate_object(g, obj, err, sizeof(err)) == 0) { 369 row_set(out, VISOR_ROW_TYPE, VISOR_CHECK_PASS, 1, 0, "%s object validates", 370 as_apply(obj) ? "OPERATION(apply)" : visor_kind_name_local(obj->kind)); 371 } else { 372 row_set(out, VISOR_ROW_TYPE, VISOR_CHECK_FAIL, 1, 1, "%.250s", err); 373 note_violation(out, err, id, "object validates", err); 374 } 375 376 invariants_row(out); 377 realization_row(g, id, obj, real_or_null, out); 378 authority_row(g, (int)(obj - g->objects), out); 379 machine_row(real_or_null, mg_or_null, out); 380 finalize(out); 381 return 0; 382 } 383 384 /* Program: the same tiers omega_program_verify runs (V0 on the realization, 385 * then V2), called individually so each row keeps its own counts, and 386 * const-clean (omega_program_verify mutates is_verified). */ 387 int visor_verify_program(const OmegaProgram *p, VisorVerifyReport *out) { 388 if (!out) return -1; 389 report_reset(out); 390 if (!p) { 391 note_violation(out, "invalid arguments", NULL, "", ""); 392 return -1; 393 } 394 if (!p->is_realized) { 395 row_set(out, VISOR_ROW_STRUCTURAL, VISOR_CHECK_FAIL, 1, 1, "program not realized; nothing to verify"); 396 note_violation(out, "program not realized", &p->program_id, "realized program", "not realized"); 397 finalize(out); 398 return 0; 399 } 400 401 VerifyReport v0; 402 if (omega_verify_v0_structural(NULL, &p->realization, &v0) == 0 && v0.passed) { 403 row_set(out, VISOR_ROW_STRUCTURAL, VISOR_CHECK_PASS, v0.check_count, v0.fail_count, 404 "V0 realization: profile, bounds, decode, terminal RET, realization id (%zu bytes)", 405 p->realization.code_len); 406 } else { 407 row_set(out, VISOR_ROW_STRUCTURAL, VISOR_CHECK_FAIL, v0.check_count, 408 v0.fail_count ? v0.fail_count : 1, "%.250s", v0.error_detail); 409 note_violation(out, v0.error_detail, &p->program_id, "realization passes V0 structural", v0.error_detail); 410 } 411 412 char err[256] = {0}; 413 if (omega_program_validate_contract(p, err, sizeof(err)) == 0) { 414 row_set(out, VISOR_ROW_TYPE, VISOR_CHECK_PASS, 1, 0, "contract validates (pre \"%.60s\", post \"%.60s\")", 415 p->contract.precondition, p->contract.postcondition); 416 } else { 417 row_set(out, VISOR_ROW_TYPE, VISOR_CHECK_FAIL, 1, 1, "%.250s", err); 418 note_violation(out, err, &p->program_id, "contract validates", err); 419 } 420 421 invariants_row(out); 422 row_set(out, VISOR_ROW_REALIZATION, VISOR_CHECK_NOT_RUN, 0, 0, 423 "program verify runs V0+V2 only; V1 reference (a+b)-c does not model unary programs"); 424 if (p->graph && p->graph->object_count > 0 && p->graph->object_count <= OMEGA_MAX_GRAPH_OBJECTS) { 425 /* No single root: count requirement-bearing objects across the whole program graph. */ 426 uint32_t eff = 0, cap = 0; 427 for (uint16_t i = 0; i < p->graph->object_count; ++i) { 428 const OmegaObject *o = &p->graph->objects[i]; 429 if (o->kind == KIND_EFFECT) eff++; 430 if (o->kind == KIND_TYPE && o->payload_len >= sizeof(TypePayload)) { 431 TypeTag t = ((const TypePayload *)o->payload)->tag; 432 if (t == TYPE_CAPABILITY_REF || t == TYPE_EFFECT_INTENT_REF || t == TYPE_EFFECT_RECEIPT_REF) cap++; 433 } 434 } 435 out->effect_count = eff; 436 out->capability_ref_count = cap; 437 out->authority_required = (eff + cap) > 0; 438 row_set(out, VISOR_ROW_AUTHORITY, VISOR_CHECK_PASS, p->graph->object_count, 0, 439 "%s: %u effect(s), %u capability ref(s) in program graph; visor grants nothing", 440 out->authority_required ? "REQUIRED" : "NONE", eff, cap); 441 } else { 442 row_set(out, VISOR_ROW_AUTHORITY, VISOR_CHECK_NOT_RUN, 0, 0, 443 "program has no semantic graph; requirements not derivable"); 444 } 445 row_set(out, VISOR_ROW_MACHINE, VISOR_CHECK_NOT_RUN, 0, 0, "no machine model"); 446 finalize(out); 447 return 0; 448 } 449 450 /* --- rendering --- */ 451 typedef struct { char *p; size_t n, len; bool overflow; } VBuf; 452 453 static void vb_printf(VBuf *b, const char *fmt, ...) __attribute__((format(printf, 2, 3))); 454 static void vb_printf(VBuf *b, const char *fmt, ...) { 455 if (b->overflow) return; 456 va_list ap; 457 va_start(ap, fmt); 458 int w = vsnprintf(b->p + b->len, b->n - b->len, fmt, ap); 459 va_end(ap); 460 if (w < 0 || (size_t)w >= b->n - b->len) { b->overflow = true; b->p[b->len] = 0; return; } 461 b->len += (size_t)w; 462 } 463 464 static void vb_json_str(VBuf *b, const char *s) { 465 vb_printf(b, "\""); 466 for (const unsigned char *c = (const unsigned char *)(s ? s : ""); *c && !b->overflow; ++c) { 467 if (*c == '"' || *c == '\\') vb_printf(b, "\\%c", *c); 468 else if (*c == '\n') vb_printf(b, "\\n"); 469 else if (*c < 0x20) vb_printf(b, "\\u%04x", *c); 470 else vb_printf(b, "%c", *c); 471 } 472 vb_printf(b, "\""); 473 } 474 475 int visor_verify_format_text(const VisorVerifyReport *r, char *out, size_t n) { 476 if (!r || !out || n == 0) return -1; 477 VBuf b = { out, n, 0, false }; 478 out[0] = 0; 479 for (size_t i = 0; i < r->row_count && i < 8; ++i) { 480 const VisorVerifyRow *row = &r->rows[i]; 481 vb_printf(&b, "%-16s%-8s %s\n", row->name ? row->name : "?", 482 visor_check_status_name(row->status), row->detail); 483 } 484 vb_printf(&b, "%-16s%s\n", "VERDICT", r->passed ? "PASS" : "FAIL"); 485 if (!r->passed && r->first_violation[0]) { 486 vb_printf(&b, "first violation: %s\n", r->first_violation); 487 if (r->object_involved[0]) vb_printf(&b, "object: %s\n", r->object_involved); 488 if (r->expected[0]) vb_printf(&b, "expected: %s\n", r->expected); 489 if (r->observed[0]) vb_printf(&b, "observed: %s\n", r->observed); 490 } 491 return b.overflow ? -1 : (int)b.len; 492 } 493 494 int visor_verify_format_json(const VisorVerifyReport *r, char *out, size_t n) { 495 if (!r || !out || n == 0) return -1; 496 VBuf b = { out, n, 0, false }; 497 out[0] = 0; 498 vb_printf(&b, "{\"passed\":%s,\"rows\":[", r->passed ? "true" : "false"); 499 for (size_t i = 0; i < r->row_count && i < 8; ++i) { 500 const VisorVerifyRow *row = &r->rows[i]; 501 vb_printf(&b, "%s{\"name\":", i ? "," : ""); 502 vb_json_str(&b, row->name); 503 vb_printf(&b, ",\"status\":\"%s\",\"checks\":%u,\"fails\":%u,\"detail\":", 504 visor_check_status_name(row->status), row->checks, row->fails); 505 vb_json_str(&b, row->detail); 506 vb_printf(&b, "}"); 507 } 508 vb_printf(&b, "],\"authority\":{\"required\":%s,\"effects\":%u,\"capability_refs\":%u,\"granted\":false}", 509 r->authority_required ? "true" : "false", r->effect_count, r->capability_ref_count); 510 if (r->first_violation[0]) { 511 vb_printf(&b, ",\"failure\":{\"first_violation\":"); 512 vb_json_str(&b, r->first_violation); 513 vb_printf(&b, ",\"object\":"); 514 vb_json_str(&b, r->object_involved); 515 vb_printf(&b, ",\"expected\":"); 516 vb_json_str(&b, r->expected); 517 vb_printf(&b, ",\"observed\":"); 518 vb_json_str(&b, r->observed); 519 vb_printf(&b, "}"); 520 } else { 521 vb_printf(&b, ",\"failure\":null"); 522 } 523 vb_printf(&b, "}"); 524 return b.overflow ? -1 : (int)b.len; 525 }