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_evidence.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 | /* Lane 4: visor_evidence tests (fixtures + read-only pass over the real evidence/ tree). |
| 18 | * Run from the repo root. Prints "PASS n/n" on success. */ |
| 19 | #include <dirent.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "omega_canonical.h" |
| 28 | #include "visor_evidence.h" |
| 29 | |
| 30 | static int g_total, g_pass; |
| 31 | #define CHECK(cond, msg) do { g_total++; if (cond) g_pass++; else fprintf(stderr, "FAIL: %s (line %d)\n", msg, __LINE__); } while (0) |
| 32 | |
| 33 | #define FIX "tests/visor/fixtures/evidence" |
| 34 | |
| 35 | static VisorEvidenceView v, v2; |
| 36 | static char txt_a[65536], txt_b[65536], js[65536]; |
| 37 | |
| 38 | static const VisorEvidenceRecord *find(const VisorEvidenceView *view, const char *suffix) { |
| 39 | size_t L = strlen(suffix); |
| 40 | for (size_t i = 0; i < view->count; ++i) { |
| 41 | size_t P = strlen(view->items[i].path); |
| 42 | if (P >= L && strcmp(view->items[i].path + P - L, suffix) == 0) return &view->items[i]; |
| 43 | } |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | /* --- omega-mv#404: a tree larger than the old 1024-file cap, built twice with the same |
| 48 | * names in opposite creation orders. tmpfs lists entries by creation order (newest first on |
| 49 | * Linux 7.0), so the two trees hand the walker opposite readdir orders; the note line prints |
| 50 | * where readdir put zzz_last_receipt.json in each. The view must be the same for both. --- */ |
| 51 | #define BIG_TXT 1500 |
| 52 | #define BIG_FILES (BIG_TXT + 2) |
| 53 | static const char *const big_receipts[2] = {"a_first_receipt.json", "zzz_last_receipt.json"}; |
| 54 | static char receipt_bytes[65536]; |
| 55 | static size_t receipt_len; |
| 56 | |
| 57 | static int big_name(int k, char *out, size_t n) { /* k-th name in ascending order */ |
| 58 | if (k == 0) return snprintf(out, n, "%s", big_receipts[0]); |
| 59 | if (k == BIG_FILES - 1) return snprintf(out, n, "%s", big_receipts[1]); |
| 60 | return snprintf(out, n, "f%04d.txt", k - 1); |
| 61 | } |
| 62 | |
| 63 | static int build_big_tree(const char *dir, int descending) { |
| 64 | if (mkdir(dir, 0700) != 0) return -1; |
| 65 | for (int i = 0; i < BIG_FILES; ++i) { |
| 66 | char name[64], p[512]; |
| 67 | big_name(descending ? BIG_FILES - 1 - i : i, name, sizeof(name)); |
| 68 | snprintf(p, sizeof(p), "%s/%s", dir, name); |
| 69 | /* Owner-only, never through an existing file or link (CodeQL world-writable-file-creation). */ |
| 70 | int fd = open(p, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600); |
| 71 | if (fd < 0) return -1; |
| 72 | FILE *f = fdopen(fd, "wb"); |
| 73 | if (!f) { close(fd); return -1; } |
| 74 | int json = strstr(name, ".json") != NULL; |
| 75 | size_t w = json ? fwrite(receipt_bytes, 1, receipt_len, f) : fwrite("not a receipt\n", 1, 14, f); |
| 76 | if (fclose(f) != 0 || w != (json ? receipt_len : 14)) return -1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void remove_big_tree(const char *dir) { |
| 82 | for (int i = 0; i < BIG_FILES; ++i) { |
| 83 | char name[64], p[512]; |
| 84 | big_name(i, name, sizeof(name)); |
| 85 | snprintf(p, sizeof(p), "%s/%s", dir, name); |
| 86 | unlink(p); |
| 87 | } |
| 88 | rmdir(dir); |
| 89 | } |
| 90 | |
| 91 | static long readdir_pos(const char *dir, const char *name) { |
| 92 | DIR *d = opendir(dir); |
| 93 | if (!d) return -1; |
| 94 | long i = 0, at = -1; |
| 95 | struct dirent *e; |
| 96 | while ((e = readdir(d)) != NULL) { |
| 97 | if (e->d_name[0] == '.') continue; |
| 98 | if (strcmp(e->d_name, name) == 0) at = i; |
| 99 | ++i; |
| 100 | } |
| 101 | closedir(d); |
| 102 | return at; |
| 103 | } |
| 104 | |
| 105 | static int same_view(const VisorEvidenceView *a, const VisorEvidenceView *b, size_t ra, size_t rb) { |
| 106 | if (a->scanned != b->scanned || a->matched != b->matched || a->count != b->count || |
| 107 | a->unparsed != b->unparsed || a->truncated != b->truncated || a->unparsed_listed != b->unparsed_listed) |
| 108 | return 0; |
| 109 | for (size_t i = 0; i < a->count; ++i) |
| 110 | if (strcmp(a->items[i].path + ra, b->items[i].path + rb) != 0 || |
| 111 | strcmp(a->items[i].receipt_id, b->items[i].receipt_id) != 0) return 0; |
| 112 | for (size_t i = 0; i < a->unparsed_listed; ++i) |
| 113 | if (strcmp(a->unparsed_paths[i] + ra, b->unparsed_paths[i] + rb) != 0) return 0; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | static void check_large_tree(void) { |
| 118 | FILE *f = fopen(FIX "/HOST/host_run_receipt.json", "rb"); |
| 119 | receipt_len = f ? fread(receipt_bytes, 1, sizeof(receipt_bytes), f) : 0; |
| 120 | if (f) fclose(f); |
| 121 | CHECK(receipt_len > 0 && receipt_len < sizeof(receipt_bytes), "large tree: receipt fixture read"); |
| 122 | |
| 123 | /* Fixed bases only: no directory from the environment reaches the scanner (CodeQL path-injection). */ |
| 124 | struct stat st; |
| 125 | const char *base = stat("/dev/shm", &st) == 0 && S_ISDIR(st.st_mode) && access("/dev/shm", W_OK) == 0 |
| 126 | ? "/dev/shm" : "/tmp"; |
| 127 | char root[512], asc[600], desc[600]; |
| 128 | snprintf(root, sizeof(root), "%s/visor-evidence-404-XXXXXX", base); |
| 129 | if (!mkdtemp(root)) { CHECK(0, "large tree: mkdtemp"); return; } |
| 130 | snprintf(asc, sizeof(asc), "%s/asc", root); |
| 131 | snprintf(desc, sizeof(desc), "%s/desc", root); |
| 132 | CHECK(build_big_tree(asc, 0) == 0 && build_big_tree(desc, 1) == 0, "large tree: built"); |
| 133 | fprintf(stderr, "note: %s: %s at readdir position %ld (asc) and %ld (desc) of %d\n", base, big_receipts[1], |
| 134 | readdir_pos(asc, big_receipts[1]), readdir_pos(desc, big_receipts[1]), BIG_FILES); |
| 135 | size_t ra = strlen(asc), rb = strlen(desc); |
| 136 | |
| 137 | /* Default cap: every file is scanned in both trees. */ |
| 138 | CHECK(visor_evidence_scan(asc, &v) == 0 && visor_evidence_scan(desc, &v2) == 0, "large tree: scan rc"); |
| 139 | CHECK(v.scanned == BIG_FILES && v.matched == 2 && v.count == 2 && v.unparsed == BIG_TXT && !v.truncated, |
| 140 | "large tree: every file scanned, both receipts found, not truncated"); |
| 141 | CHECK(v.count == 2 && strstr(v.items[0].path, big_receipts[0]) && strstr(v.items[1].path, big_receipts[1]), |
| 142 | "large tree: receipts in path order"); |
| 143 | CHECK(same_view(&v, &v2, ra, rb), "large tree: same view for opposite readdir orders"); |
| 144 | |
| 145 | /* The old cap (1024) applied to the same trees: the kept files are the 1024 smallest paths, |
| 146 | * so a_first is kept and zzz_last is not, in both orders. */ |
| 147 | size_t prev = visor_evidence_set_max_files(1024); |
| 148 | CHECK(visor_evidence_scan(asc, &v) == 0 && visor_evidence_scan(desc, &v2) == 0, "capped: scan rc"); |
| 149 | CHECK(v.scanned == 1024 && v.truncated && v.matched == 1 && v.count == 1 && v.unparsed == 1023 && |
| 150 | strstr(v.items[0].path, big_receipts[0]), "capped: smallest 1024 paths kept, truncated"); |
| 151 | CHECK(v.unparsed_listed == VISOR_EVIDENCE_MAX_UNPARSED_PATHS && strstr(v.unparsed_paths[0], "/f0000.txt") && |
| 152 | strstr(v.unparsed_paths[VISOR_EVIDENCE_MAX_UNPARSED_PATHS - 1], "/f0007.txt"), "capped: first unparsed paths"); |
| 153 | CHECK(same_view(&v, &v2, ra, rb), "capped: same view for opposite readdir orders"); |
| 154 | SemanticId aid; |
| 155 | memset(aid.bytes, 0xaa, sizeof(aid.bytes)); |
| 156 | CHECK(visor_evidence_for_id(asc, &aid, &v) == 0 && visor_evidence_for_id(desc, &aid, &v2) == 0 && |
| 157 | v.count == 1 && v.truncated && same_view(&v, &v2, ra, rb), "capped: for_id same view for both orders"); |
| 158 | visor_evidence_set_max_files(0); |
| 159 | CHECK(prev == VISOR_EVIDENCE_MAX_FILES && visor_evidence_set_max_files(0) == VISOR_EVIDENCE_MAX_FILES, |
| 160 | "cap restored to default"); |
| 161 | |
| 162 | remove_big_tree(asc); |
| 163 | remove_big_tree(desc); |
| 164 | CHECK(rmdir(root) == 0, "large tree: removed"); |
| 165 | } |
| 166 | |
| 167 | int main(void) { |
| 168 | /* --- scan fixtures --- */ |
| 169 | CHECK(visor_evidence_scan(FIX, &v) == 0, "scan fixtures rc"); |
| 170 | CHECK(v.scanned == 8 && v.count == 6 && v.matched == 6 && v.unparsed == 2 && !v.truncated, "scan counts"); |
| 171 | bool sorted = true; |
| 172 | for (size_t i = 1; i < v.count; ++i) if (strcmp(v.items[i - 1].path, v.items[i].path) >= 0) sorted = false; |
| 173 | CHECK(sorted, "sorted by path"); |
| 174 | CHECK(v.unparsed_listed == 2 && strstr(v.unparsed_paths[0], "digests.txt") && |
| 175 | strstr(v.unparsed_paths[1], "malformed_receipt.json"), "unparsed paths listed"); |
| 176 | |
| 177 | const VisorEvidenceRecord *h = find(&v, "HOST/host_run_receipt.json"); |
| 178 | CHECK(h && h->scope == VISOR_QUAL_HOST && strcmp(h->scope_name, "HOST") == 0 && |
| 179 | strcmp(h->scope_basis, "hardware_scope") == 0, "host scope"); |
| 180 | CHECK(h && strcmp(h->commit, "1111111111111111111111111111111111111111") == 0 && |
| 181 | strcmp(h->run_id, "20260928T000000Z-fixturehost") == 0, "host commit/run"); |
| 182 | CHECK(h && h->tree_state_known && !h->tree_dirty && h->gates_run == 3 && h->gates_passed == 2, "host tree/gates"); |
| 183 | CHECK(h && strcmp(h->claim, "FIXTURE_HOST_RUN_V1: FIXTURE_HOST_PASS=PASS") == 0, "host claim"); |
| 184 | CHECK(h && strcmp(h->machine, "os=Linux arch=aarch64 kernel=7.0.0-fixture") == 0, "host machine"); |
| 185 | CHECK(h && strncmp(h->receipt_id, "sha256:", 7) == 0 && strlen(h->receipt_id) == 71 && |
| 186 | h->name_hash_match == -1, "receipt id"); |
| 187 | |
| 188 | const VisorEvidenceRecord *q = find(&v, "qemu_milestone_receipt.json"); |
| 189 | CHECK(q && q->scope == VISOR_QUAL_QEMU && q->gates_run == 3 && q->gates_passed == 3, "qemu scope/gates"); |
| 190 | CHECK(q && strcmp(q->realization_id, "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") == 0 && |
| 191 | !q->tree_state_known && strcmp(q->claim, "FIXTURE MILESTONE - QEMU: QUALIFIED / PASS") == 0, "qemu realization/claim"); |
| 192 | |
| 193 | const VisorEvidenceRecord *s = find(&v, "SILICON/silicon_run_receipt.json"); |
| 194 | CHECK(s && s->scope == VISOR_QUAL_SILICON && strcmp(s->scope_basis, "silicon_observed=true") == 0, "silicon scope"); |
| 195 | CHECK(s && s->tree_dirty && strcmp(s->physics_commit, "5555555555555555555555555555555555555555") == 0 && |
| 196 | strcmp(s->machine, "node=spark-fixture arch=aarch64 kernel=7.0.0-fixture") == 0 && |
| 197 | strncmp(s->realization_id, "sha256:cccc", 11) == 0, "silicon fields"); |
| 198 | |
| 199 | const VisorEvidenceRecord *n = find(&v, "SILICON/standin_no_upgrade_receipt.json"); |
| 200 | CHECK(n && n->scope == VISOR_QUAL_HOST, "silicon_observed=false + gpu_uuid is NOT upgraded"); |
| 201 | |
| 202 | const VisorEvidenceRecord *u = find(&v, "unknown_scope_receipt.json"); |
| 203 | CHECK(u && u->scope == VISOR_QUAL_UNKNOWN && u->scope_basis[0] == 0 && u->gates_run == 0, |
| 204 | "host{} + gpu_architecture + prose -> UNKNOWN"); |
| 205 | |
| 206 | const VisorEvidenceRecord *c = NULL; |
| 207 | for (size_t i = 0; i < v.count; ++i) if (strstr(v.items[i].path, "/CA/")) c = &v.items[i]; |
| 208 | CHECK(c && c->name_hash_match == 1 && c->scope == VISOR_QUAL_HOST && |
| 209 | strcmp(c->commit, "8888888888888888888888888888888888888888") == 0, "content-addressed name verified"); |
| 210 | |
| 211 | /* --- load fails closed --- */ |
| 212 | VisorEvidenceRecord r; |
| 213 | CHECK(visor_evidence_load(FIX "/malformed_receipt.json", &r) == -1, "malformed fails closed"); |
| 214 | CHECK(visor_evidence_load(FIX "/digests.txt", &r) == -1, "non-JSON fails closed"); |
| 215 | CHECK(visor_evidence_load(FIX "/does_not_exist.json", &r) == -1, "missing file fails closed"); |
| 216 | CHECK(visor_evidence_load(FIX "/SILICON/silicon_run_receipt.json", &r) == 0 && r.scope == VISOR_QUAL_SILICON, |
| 217 | "load single"); |
| 218 | CHECK(visor_evidence_scan("tests/visor/fixtures/no_such_dir", &v2) == -1, "missing root is an error"); |
| 219 | |
| 220 | /* --- for_id --- */ |
| 221 | SemanticId aid, missing; |
| 222 | memset(aid.bytes, 0xaa, sizeof(aid.bytes)); |
| 223 | CHECK(visor_evidence_for_id(FIX, &aid, &v2) == 0, "for_id rc"); |
| 224 | CHECK(v2.count == 2 && find(&v2, "HOST/host_run_receipt.json") && find(&v2, "qemu_milestone_receipt.json") && |
| 225 | v2.unparsed == 2, "for_id finds mentions; unparsed mentions listed"); |
| 226 | SemanticId rid; |
| 227 | memset(rid.bytes, 0xbb, sizeof(rid.bytes)); |
| 228 | visor_evidence_for_id(FIX, &rid, &v2); |
| 229 | CHECK(v2.count == 1 && find(&v2, "qemu_milestone_receipt.json"), "for_id by realization id"); |
| 230 | memset(missing.bytes, 0x12, sizeof(missing.bytes)); |
| 231 | CHECK(visor_evidence_for_id(FIX, &missing, &v2) == 0 && v2.count == 0 && v2.unparsed == 0, "missing id -> count 0"); |
| 232 | CHECK(visor_evidence_format_text(&v2, txt_a, sizeof(txt_a)) > 0 && strstr(txt_a, "no evidence"), "no evidence text"); |
| 233 | |
| 234 | /* --- rendering deterministic --- */ |
| 235 | visor_evidence_scan(FIX, &v); |
| 236 | visor_evidence_scan(FIX, &v2); |
| 237 | CHECK(visor_evidence_format_text(&v, txt_a, sizeof(txt_a)) > 0 && |
| 238 | visor_evidence_format_text(&v2, txt_b, sizeof(txt_b)) > 0 && strcmp(txt_a, txt_b) == 0, "text deterministic"); |
| 239 | CHECK(strstr(txt_a, "UNPARSED") && strstr(txt_a, "scope SILICON (from silicon_observed=true)"), "text content"); |
| 240 | CHECK(visor_evidence_format_json(&v, js, sizeof(js)) > 0 && js[0] == '{' && strstr(js, "\"scope\":\"QEMU\""), "json"); |
| 241 | CHECK(visor_evidence_format_text(&v, txt_b, 32) == -1, "truncation fails closed"); |
| 242 | |
| 243 | check_large_tree(); |
| 244 | |
| 245 | /* --- real evidence/ tree, read-only: format coverage and no upgrade --- */ |
| 246 | if (visor_evidence_scan("evidence", &v) == 0) { |
| 247 | CHECK(v.scanned >= 70 && v.count == VISOR_EVIDENCE_MAX_ITEMS && v.truncated && v.matched >= 50, |
| 248 | "real tree scanned (54 JSON of 76 files at ceb68d6)"); |
| 249 | CHECK(visor_evidence_load("evidence/PLAN_REUSE/d8c8246cf4f176a8327d31774cf3e31f39049606fe29af240f2d5a5dcd43de6f.json", &r) == 0 && |
| 250 | r.scope == VISOR_QUAL_HOST && r.name_hash_match == 1 && r.tree_state_known && !r.tree_dirty && |
| 251 | strcmp(r.commit, "7d562020abba34185ae180aa3db1ad5a7676c4b8") == 0 && |
| 252 | strstr(r.claim, "OMEGA_PLAN_REUSE_PASS=FAIL"), "real run receipt (host, content-addressed)"); |
| 253 | CHECK(visor_evidence_load("evidence/R12/af6c21f81303a0396ef66ffce5b7f37c85e3d780749095691bdadedef908ff75.json", &r) == 0 && |
| 254 | r.scope == VISOR_QUAL_SILICON && r.name_hash_match == 1, "real silicon receipt"); |
| 255 | CHECK(visor_evidence_load("evidence/R12/795e29fc23e5d3c8a3a5b742e859bd01c3aa07200219cd03d58817b599ff03fe.json", &r) == 0 && |
| 256 | r.scope == VISOR_QUAL_HOST, "real stand-in receipt stays HOST"); |
| 257 | CHECK(visor_evidence_load("evidence/omega_verify_qualification_receipt.json", &r) == 0 && |
| 258 | r.gates_run == 10 && r.gates_passed == 10 && |
| 259 | strcmp(r.commit, "8033c3805ce953d111c58c4da33a60678e15401d") == 0, "real milestone receipt"); |
| 260 | CHECK(visor_evidence_load("evidence/omega_blackwell_vector_qualification_receipt.json", &r) == 0 && |
| 261 | r.scope != VISOR_QUAL_SILICON, "gpu_architecture alone is not a silicon claim"); |
| 262 | CHECK(visor_evidence_load("evidence/omega_aarch64_qualification_receipt.json", &r) == 0 && |
| 263 | r.scope == VISOR_QUAL_QEMU && strcmp(r.scope_basis, "qemu_target") == 0, "real qemu_target receipt"); |
| 264 | CHECK(visor_evidence_load("evidence/omega_realization_synthesis_qualification_receipt.json", &r) == 0 && |
| 265 | r.scope == VISOR_QUAL_UNKNOWN, "QEMU gate name / modeled profile is not a QEMU claim"); |
| 266 | } else { |
| 267 | fprintf(stderr, "note: evidence/ not found from cwd; real-tree checks skipped\n"); |
| 268 | } |
| 269 | |
| 270 | printf("%s %d/%d\n", g_pass == g_total ? "PASS" : "FAIL", g_pass, g_total); |
| 271 | return g_pass == g_total ? 0 : 1; |
| 272 | } |