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_library.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_library.c -- VC1-LIB: the verified program library as the only admission gate. |
| 18 | * |
| 19 | * Usage: |
| 20 | * test_omega_library run every check; exit 0 only if all pass |
| 21 | * test_omega_library <mutant> the binary was linked against a deliberately broken copy of |
| 22 | * src/omega_library.c (built by `make test-library`); run every |
| 23 | * check and require the mapped check to FAIL. Exit 1 = mutant |
| 24 | * killed (what the Makefile asserts), 3 = mutant survived, |
| 25 | * 2 = unknown mutant name. |
| 26 | * |
| 27 | * Programs are built the way tests/program/test_program_id.c builds them: |
| 28 | * omega_program_build_unary_op(), then is_verified = true. Physics-free, host-portable. |
| 29 | */ |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #include "omega_core.h" |
| 36 | #include "omega_program.h" |
| 37 | #include "omega_library.h" |
| 38 | |
| 39 | /* check -> mutant that must kill it. Each mutant is one deleted/weakened guard line in |
| 40 | * omega_library.c (see the sed table in the Makefile test-library target). */ |
| 41 | static const struct { const char *mutant, *check; } MUTANTS[] = { |
| 42 | { "allow-null-receipt", "null-receipt-refused" }, |
| 43 | { "allow-zero-receipt", "zero-receipt-refused" }, |
| 44 | { "allow-unknown-dep", "unknown-dep-refused" }, |
| 45 | { "truncate-deps", "dep-overflow-refused" }, |
| 46 | { "allow-duplicate", "duplicate-refused" }, |
| 47 | { "allow-unverified", "unverified-refused" }, |
| 48 | { "digest-ignores-kind", "bootstrap-vs-verified-digest-differ" }, |
| 49 | { "bootstrap-as-verified", "bootstrap-kind-recorded" }, |
| 50 | { "refuse-valid", "valid-verified-accepted" }, |
| 51 | { "refuse-deps", "existing-dep-accepted" }, |
| 52 | { "max-deps-off-by-one", "max-deps-accepted" }, |
| 53 | { "bootstrap-null-audit", "bootstrap-null-audit-refused" }, |
| 54 | { "bootstrap-zero-audit", "bootstrap-zero-audit-refused" }, |
| 55 | }; |
| 56 | #define N_MUT (sizeof MUTANTS / sizeof MUTANTS[0]) |
| 57 | |
| 58 | static const char *g_mutant; /* NULL in normal mode */ |
| 59 | static const char *g_expect_check; /* check that must fail under the mutant */ |
| 60 | static int g_failed, g_expect_failed; |
| 61 | |
| 62 | static void check(const char *name, int ok) { |
| 63 | printf("%s %s\n", ok ? "PASS" : "FAIL", name); |
| 64 | if (!ok) { |
| 65 | g_failed++; |
| 66 | if (g_expect_check && strcmp(name, g_expect_check) == 0) g_expect_failed = 1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static const uint8_t RECEIPT[32] = { 0xA5, 0x5A, 0xC3, 0x3C, 1 }; |
| 71 | static const uint8_t ZERO[32]; |
| 72 | |
| 73 | static OmegaLibrary *new_lib(void) { |
| 74 | OmegaLibrary *l = calloc(1, sizeof *l); |
| 75 | if (!l || omega_library_init(l) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } |
| 76 | return l; |
| 77 | } |
| 78 | |
| 79 | static OmegaLibrary *reset(OmegaLibrary *l) { |
| 80 | omega_library_destroy(l); |
| 81 | free(l); |
| 82 | return new_lib(); |
| 83 | } |
| 84 | |
| 85 | static void mk(OmegaProgram *p, const char *name, OpCode op, uint64_t imm) { |
| 86 | if (omega_program_build_unary_op(p, name, op, imm) != 0) { fprintf(stderr, "build failed\n"); exit(2); } |
| 87 | p->is_verified = true; |
| 88 | } |
| 89 | |
| 90 | int main(int argc, char **argv) { |
| 91 | if (argc > 1) { |
| 92 | g_mutant = argv[1]; |
| 93 | for (size_t i = 0; i < N_MUT; i++) |
| 94 | if (strcmp(MUTANTS[i].mutant, g_mutant) == 0) g_expect_check = MUTANTS[i].check; |
| 95 | if (!g_expect_check) { fprintf(stderr, "unknown mutant '%s'\n", g_mutant); return 2; } |
| 96 | } |
| 97 | |
| 98 | OmegaLibrary *lib = new_lib(); |
| 99 | OmegaProgram a, b, unv; |
| 100 | mk(&a, "add7", OP_ADD, 7); |
| 101 | mk(&b, "add8", OP_ADD, 8); |
| 102 | mk(&unv, "add9", OP_ADD, 9); |
| 103 | unv.is_verified = false; |
| 104 | |
| 105 | /* Evidence is mandatory. Each refusal runs on a fresh library so a weakened guard in one |
| 106 | * path cannot hide behind state left by another (count must stay 0). */ |
| 107 | check("null-receipt-refused", omega_library_insert(lib, &a, NULL, 0, NULL) != 0 && lib->count == 0); |
| 108 | lib = reset(lib); |
| 109 | check("zero-receipt-refused", omega_library_insert(lib, &a, NULL, 0, ZERO) != 0 && lib->count == 0); |
| 110 | lib = reset(lib); |
| 111 | check("bootstrap-null-audit-refused", omega_library_insert_bootstrap(lib, &a, NULL, 0, NULL) != 0 && lib->count == 0); |
| 112 | lib = reset(lib); |
| 113 | check("bootstrap-zero-audit-refused", omega_library_insert_bootstrap(lib, &a, NULL, 0, ZERO) != 0 && lib->count == 0); |
| 114 | lib = reset(lib); |
| 115 | check("unverified-refused", omega_library_insert(lib, &unv, NULL, 0, RECEIPT) != 0 && lib->count == 0); |
| 116 | lib = reset(lib); |
| 117 | |
| 118 | /* valid verified insert */ |
| 119 | int rc = omega_library_insert(lib, &a, NULL, 0, RECEIPT); |
| 120 | check("valid-verified-accepted", rc == 0 && lib->count == 1 && |
| 121 | lib->entries[0].admission_kind == OMEGA_LIB_ADMISSION_VERIFIED && |
| 122 | memcmp(lib->entries[0].evidence_receipt_hash, RECEIPT, 32) == 0); |
| 123 | |
| 124 | /* duplicate */ |
| 125 | size_t before = lib->count; |
| 126 | check("duplicate-refused", omega_library_insert(lib, &a, NULL, 0, RECEIPT) != 0 && lib->count == before); |
| 127 | |
| 128 | /* dependencies must exist */ |
| 129 | SemanticId unknown; |
| 130 | memset(&unknown, 0xEE, sizeof unknown); |
| 131 | before = lib->count; |
| 132 | check("unknown-dep-refused", omega_library_insert(lib, &b, &unknown, 1, RECEIPT) != 0 && lib->count == before); |
| 133 | SemanticId dep = a.program_id; |
| 134 | rc = omega_library_insert(lib, &b, &dep, 1, RECEIPT); |
| 135 | check("existing-dep-accepted", rc == 0 && lib->count == before + 1 && lib->entries[before].dep_count == 1 && |
| 136 | memcmp(lib->entries[before].dependency_ids[0].bytes, dep.bytes, OMEGA_ID_BYTES) == 0); |
| 137 | |
| 138 | /* dep_count boundary: MAX accepted, MAX+1 refused (not truncated). OMEGA_LIB_MAX_DEPS distinct |
| 139 | * base programs + 2 composites; the deps array repeats existing ids, which is legal input. */ |
| 140 | OmegaLibrary *l2 = new_lib(); |
| 141 | SemanticId ids[OMEGA_LIB_MAX_DEPS + 1]; |
| 142 | for (int i = 0; i < OMEGA_LIB_MAX_DEPS; i++) { |
| 143 | OmegaProgram p; |
| 144 | mk(&p, "base", OP_ADD, 100 + (uint64_t)i); |
| 145 | ids[i] = p.program_id; |
| 146 | if (omega_library_insert(l2, &p, NULL, 0, RECEIPT) != 0) { fprintf(stderr, "setup insert failed\n"); return 2; } |
| 147 | } |
| 148 | ids[OMEGA_LIB_MAX_DEPS] = ids[0]; |
| 149 | OmegaProgram top; |
| 150 | mk(&top, "top", OP_MUL, 3); |
| 151 | size_t c2 = l2->count; |
| 152 | check("dep-overflow-refused", |
| 153 | omega_library_insert(l2, &top, ids, OMEGA_LIB_MAX_DEPS + 1, RECEIPT) != 0 && l2->count == c2); |
| 154 | rc = omega_library_insert(l2, &top, ids, OMEGA_LIB_MAX_DEPS, RECEIPT); |
| 155 | check("max-deps-accepted", rc == 0 && l2->count == c2 + 1 && l2->entries[c2].dep_count == OMEGA_LIB_MAX_DEPS); |
| 156 | |
| 157 | /* bootstrap vs verified: same program bytes, same hash bytes, different admission kind */ |
| 158 | OmegaLibrary *lv = new_lib(), *lb = new_lib(); |
| 159 | int rv = omega_library_insert(lv, &a, NULL, 0, RECEIPT); |
| 160 | int rb = omega_library_insert_bootstrap(lb, &a, NULL, 0, RECEIPT); |
| 161 | check("bootstrap-kind-recorded", rv == 0 && rb == 0 && |
| 162 | lb->entries[0].admission_kind == OMEGA_LIB_ADMISSION_BOOTSTRAP && |
| 163 | lv->entries[0].admission_kind == OMEGA_LIB_ADMISSION_VERIFIED); |
| 164 | check("bootstrap-vs-verified-digest-differ", rv == 0 && rb == 0 && |
| 165 | memcmp(lv->state_digest, lb->state_digest, 32) != 0); |
| 166 | |
| 167 | omega_library_destroy(lib); free(lib); |
| 168 | omega_library_destroy(l2); free(l2); |
| 169 | omega_library_destroy(lv); free(lv); |
| 170 | omega_library_destroy(lb); free(lb); |
| 171 | |
| 172 | if (!g_mutant) { |
| 173 | printf("%s: %d check(s) failed\n", g_failed ? "FAIL" : "PASS", g_failed); |
| 174 | return g_failed ? 1 : 0; |
| 175 | } |
| 176 | if (g_expect_failed) { printf("MUTANT %s KILLED by %s\n", g_mutant, g_expect_check); return 1; } |
| 177 | printf("MUTANT %s SURVIVED (check %s did not fail)\n", g_mutant, g_expect_check); |
| 178 | return 3; |
| 179 | } |