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 / test_omega_vc_bridge.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_omega_vc_bridge.c: the bridge (src/omega_vc_bridge.c) is not a self-minting escape hatch |
| 18 | * (VC1 stage 6 fix, from the inspector's hand test on omega PR #224). |
| 19 | * 1. The bridge runs omega_program_verify itself and recomputes the program id itself. A program |
| 20 | * whose real verification fails is refused even when the caller flips is_verified by hand; a |
| 21 | * program whose id was forged is refused. |
| 22 | * 2. A record the bridge mints lists OMEGA_BRIDGE_SELFMINTED_CAPABILITY, so a build-domain import |
| 23 | * of it is refused. The dev domain still accepts it. |
| 24 | * Each guard carries a VC1B tag; the Makefile target test-vc-bridge breaks each tag in a copy and |
| 25 | * the named check must FAIL. CPU only. Exit 0 only when every check passed. */ |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "omega_library.h" |
| 31 | #include "omega_program.h" |
| 32 | #include "omega_resolve.h" |
| 33 | #include "omega_vc_bridge.h" |
| 34 | #include "omega_vcstore.h" |
| 35 | |
| 36 | static int g_total, g_failed; |
| 37 | static void check(const char *name, int ok) { |
| 38 | printf("%s %s\n", ok ? "PASS" : "FAIL", name); |
| 39 | g_total++; |
| 40 | if (!ok) g_failed++; |
| 41 | } |
| 42 | static void hx(const uint8_t *d, char out[65]) { |
| 43 | static const char h[] = "0123456789abcdef"; |
| 44 | for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } |
| 45 | out[64] = 0; |
| 46 | } |
| 47 | static OmegaProgram *mkprog(uint64_t imm) { |
| 48 | OmegaProgram *p = calloc(1, sizeof *p); |
| 49 | if (!p || omega_program_build_unary_op(p, "bridge-test", OP_ADD, imm) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } |
| 50 | if (!p->is_realized && omega_program_realize(p) != 0) { fprintf(stderr, "realize failed\n"); exit(2); } |
| 51 | return p; |
| 52 | } |
| 53 | typedef struct { OmegaVcBridge br; OmegaLibrary lib; } Rig; |
| 54 | static Rig *rig_new(void) { |
| 55 | Rig *r = calloc(1, sizeof *r); |
| 56 | if (!r || omega_vc_bridge_init(&r->br) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } |
| 57 | omega_library_init(&r->lib); |
| 58 | return r; |
| 59 | } |
| 60 | static void rig_free(Rig *r) { omega_library_destroy(&r->lib); omega_vc_bridge_destroy(&r->br); free(r); } |
| 61 | |
| 62 | /* resolve one import of the program in the bridge's own store, in domain d */ |
| 63 | static int import_one(Rig *r, const OmegaProgram *p, OmegaDomain d, OmegaClosure *cl, OmegaResolveError *e) { |
| 64 | uint8_t rcpt[32]; |
| 65 | if (omega_vcstore_receipt_of(&r->br.store, p->program_id.bytes, rcpt) != 0) return -99; |
| 66 | char sh[65], rh[65], text[400]; |
| 67 | hx(p->program_id.bytes, sh); hx(rcpt, rh); |
| 68 | snprintf(text, sizeof text, "omega.lock v1\nfoo = semantic %s receipt %s\n", sh, rh); |
| 69 | OmegaLock lock; |
| 70 | if (omega_lock_parse(text, strlen(text), &lock, e) != 0) return -98; |
| 71 | OmegaResolver rv; |
| 72 | memset(&rv, 0, sizeof rv); |
| 73 | rv.store = &r->br.store; rv.domain = d; |
| 74 | rv.fetch_receipt = omega_vc_bridge_fetch_receipt; rv.fetch_ctx = &r->br; |
| 75 | rv.fetch_blob = omega_vc_bridge_fetch_blob; rv.blob_ctx = &r->br; |
| 76 | const char *names[1] = { "foo" }; |
| 77 | memset(cl, 0, sizeof *cl); |
| 78 | int rc = omega_resolve_imports(&rv, &lock, names, 1, cl, e); |
| 79 | omega_lock_free(&lock); |
| 80 | return rc; |
| 81 | } |
| 82 | |
| 83 | int main(void) { |
| 84 | static const uint8_t ev[32] = { 7 }; |
| 85 | |
| 86 | /* the genuine program is admitted */ |
| 87 | { |
| 88 | Rig *r = rig_new(); |
| 89 | OmegaProgram *p = mkprog(5); |
| 90 | check("bridge-admits-a-genuine-program", omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev) == 0 && |
| 91 | r->lib.count == 1 && omega_vcstore_count(&r->br.store) == 1); |
| 92 | OmegaClosure cl; OmegaResolveError e; |
| 93 | memset(&e, 0, sizeof e); |
| 94 | int rc = import_one(r, p, OMEGA_DOMAIN_BUILD, &cl, &e); |
| 95 | check("refuse-bridge-record-in-build-domain", rc == OMEGA_RES_UNVERIFIED_DEPENDENCY && cl.n == 0 && strstr(e.message, "bridge") != NULL); |
| 96 | omega_closure_free(&cl); |
| 97 | memset(&e, 0, sizeof e); |
| 98 | rc = import_one(r, p, OMEGA_DOMAIN_DEV, &cl, &e); |
| 99 | check("accept-bridge-record-in-dev-domain", rc == 0 && cl.n == 1); |
| 100 | omega_closure_free(&cl); |
| 101 | omega_program_destroy(p); free(p); rig_free(r); |
| 102 | } |
| 103 | /* the caller never needs to set is_verified: the bridge verifies for itself */ |
| 104 | { |
| 105 | Rig *r = rig_new(); |
| 106 | OmegaProgram *p = mkprog(6); |
| 107 | p->is_verified = false; |
| 108 | check("bridge-does-not-need-the-callers-verified-flag", omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev) == 0); |
| 109 | omega_program_destroy(p); free(p); rig_free(r); |
| 110 | } |
| 111 | /* the inspector's hand test: corrupt the machine code, flip the flags, ask the bridge */ |
| 112 | { |
| 113 | Rig *r = rig_new(); |
| 114 | OmegaProgram *p = mkprog(5); |
| 115 | memset(p->realization.code_bytes, 0xFF, p->realization.code_len); |
| 116 | OmegaProgram *probe = calloc(1, sizeof *probe); |
| 117 | *probe = *p; |
| 118 | VerifyReport rep; |
| 119 | int premise = omega_program_verify(probe, &rep) != 0; /* real verification fails on this program */ |
| 120 | free(probe); |
| 121 | p->is_realized = true; p->is_verified = true; /* the caller's lie */ |
| 122 | int rc = omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev); |
| 123 | check("premise-corrupted-program-fails-real-verification", premise); |
| 124 | check("refuse-corrupted-program-even-with-flags-set", rc == -1 && r->lib.count == 0 && omega_vcstore_count(&r->br.store) == 0); |
| 125 | omega_program_destroy(p); free(p); rig_free(r); |
| 126 | } |
| 127 | /* a forged program id with the flags set */ |
| 128 | { |
| 129 | Rig *r = rig_new(); |
| 130 | OmegaProgram *p = mkprog(5); |
| 131 | p->is_realized = true; p->is_verified = true; |
| 132 | p->program_id.bytes[0] ^= 1; |
| 133 | int rc = omega_vc_bridge_admit(&r->br, &r->lib, p, NULL, 0, ev); |
| 134 | check("refuse-forged-program-id-at-admit", rc == -1 && r->lib.count == 0 && omega_vcstore_count(&r->br.store) == 0); |
| 135 | omega_program_destroy(p); free(p); rig_free(r); |
| 136 | } |
| 137 | printf("%d checks, %d failed\n", g_total, g_failed); |
| 138 | return g_failed ? 1 : 0; |
| 139 | } |