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
| 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 | /* |
| 18 | * oscv_main.c -- `oscv`: the VERIFIED OSC driver (VC1 stage 4, ADR 0029). |
| 19 | * |
| 20 | * oscv [--domain dev|build] [--lock FILE] [--store FILE] [--receipts DIR] [--blobs DIR] |
| 21 | * [--meta-out FILE] <file.osc> |
| 22 | * |
| 23 | * Compiles an OSC unit exactly as oscc does and ALSO resolves its `import NAME;` lines, only |
| 24 | * through omega.lock -> semantic id -> Verified Crumb Store -> receipt check -> closure. Defaults |
| 25 | * sit next to the source file: omega.lock, omega.vcstore, receipts/ and (build domain only) |
| 26 | * blobs/. There is no option that skips a check, and nothing is read from the environment. In |
| 27 | * the build domain the source/IR store is MANDATORY (SPEC 6 step 3): every imported record's |
| 28 | * blob must be there, hash to its digest and, for an IR digest, recompute to its semantic id, |
| 29 | * else the import is refused (UNVERIFIED_DEPENDENCY). The dev domain may leave --blobs out; its |
| 30 | * output is tainted either way. There is no genesis option: a BOOTSTRAP record satisfies an |
| 31 | * import only if its id is a member of the pinned set VC-GENESIS-1 compiled into the resolver |
| 32 | * (docs/osc/VC-GENESIS-1.md). The only way to change what is legal is to change the closure. |
| 33 | * |
| 34 | * Output (success): ir_sha256= code_sha256= funcs= domain= tainted= imports= closure_entries= |
| 35 | * closure_sha256= build_id= (build_id folds the domain and the closure digest into the identity). |
| 36 | * Refusal: the oscc "error kind=..." lines, plus resolve_code=<CODE> for an import refusal. |
| 37 | * Exit 0 ok, 1 refused, 2 usage or I/O. |
| 38 | * OSC-1 slice; not a general Omega compiler; no self-hosting. |
| 39 | */ |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | |
| 44 | #include "omega_resolve.h" |
| 45 | #include "omega_resolve_osc.h" |
| 46 | #include "osc_cg.h" |
| 47 | #include "osc_front.h" |
| 48 | #include "sha256.h" |
| 49 | |
| 50 | static void hexs(const uint8_t *d, char *out) |
| 51 | { |
| 52 | static const char h[] = "0123456789abcdef"; |
| 53 | for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } |
| 54 | out[64] = 0; |
| 55 | } |
| 56 | |
| 57 | static char *slurp(const char *path, size_t *len, int *missing) |
| 58 | { |
| 59 | FILE *fp = fopen(path, "rb"); |
| 60 | *missing = 0; |
| 61 | if (!fp) { *missing = 1; return NULL; } |
| 62 | size_t cap = 1 << 16, n = 0; |
| 63 | char *b = malloc(cap); |
| 64 | for (;;) { |
| 65 | if (!b) { fclose(fp); return NULL; } |
| 66 | n += fread(b + n, 1, cap - n, fp); |
| 67 | if (n < cap) break; |
| 68 | if (cap >= (1u << 24)) { fclose(fp); free(b); return NULL; } |
| 69 | cap *= 2; |
| 70 | char *t = realloc(b, cap); |
| 71 | if (!t) free(b); |
| 72 | b = t; |
| 73 | } |
| 74 | fclose(fp); |
| 75 | *len = n; |
| 76 | return b; |
| 77 | } |
| 78 | |
| 79 | static void join_dir(char *out, size_t cap, const char *file, const char *leaf) |
| 80 | { |
| 81 | const char *slash = strrchr(file, '/'); |
| 82 | if (slash) snprintf(out, cap, "%.*s/%s", (int)(slash - file), file, leaf); |
| 83 | else snprintf(out, cap, "%s", leaf); |
| 84 | } |
| 85 | |
| 86 | static int refuse_resolve(const OmegaResolveError *e) |
| 87 | { |
| 88 | printf("error kind=IMPORT_REFUSED line=0 col=0 object=%s origin_line=0 other= transition=%s\n", e->subject, omega_resolve_code_name(e->code)); |
| 89 | printf("message=%s\n", e->message); |
| 90 | printf("resolve_code=%s\n", omega_resolve_code_name(e->code)); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | int main(int argc, char **argv) |
| 95 | { |
| 96 | OmegaDomain domain = OMEGA_DOMAIN_BUILD; |
| 97 | const char *lockp = NULL, *storep = NULL, *recdir = NULL, *blobdir = NULL, *metaout = NULL, *file = NULL; |
| 98 | for (int i = 1; i < argc; i++) { |
| 99 | const char *a = argv[i]; |
| 100 | if (strcmp(a, "--domain") == 0 && i + 1 < argc) { |
| 101 | const char *v = argv[++i]; |
| 102 | if (strcmp(v, "dev") == 0) domain = OMEGA_DOMAIN_DEV; |
| 103 | else if (strcmp(v, "build") == 0) domain = OMEGA_DOMAIN_BUILD; |
| 104 | else { fprintf(stderr, "oscv: --domain must be dev or build\n"); return 2; } |
| 105 | } else if (strcmp(a, "--lock") == 0 && i + 1 < argc) lockp = argv[++i]; |
| 106 | else if (strcmp(a, "--store") == 0 && i + 1 < argc) storep = argv[++i]; |
| 107 | else if (strcmp(a, "--receipts") == 0 && i + 1 < argc) recdir = argv[++i]; |
| 108 | else if (strcmp(a, "--blobs") == 0 && i + 1 < argc) blobdir = argv[++i]; |
| 109 | else if (strcmp(a, "--meta-out") == 0 && i + 1 < argc) metaout = argv[++i]; |
| 110 | else if (a[0] == '-' || file) { fprintf(stderr, "oscv: unknown or repeated argument '%s'\n", a); return 2; } |
| 111 | else file = a; |
| 112 | } |
| 113 | if (!file) { |
| 114 | fprintf(stderr, "usage: oscv [--domain dev|build] [--lock F] [--store F] [--receipts D] [--blobs D] [--meta-out F] <file.osc>\n"); |
| 115 | return 2; |
| 116 | } |
| 117 | char lockd[4096], stored[4096], recd[4096], blobd[4096]; |
| 118 | if (!lockp) { join_dir(lockd, sizeof lockd, file, "omega.lock"); lockp = lockd; } |
| 119 | if (!storep) { join_dir(stored, sizeof stored, file, "omega.vcstore"); storep = stored; } |
| 120 | if (!recdir) { join_dir(recd, sizeof recd, file, "receipts"); recdir = recd; } |
| 121 | if (!blobdir && domain == OMEGA_DOMAIN_BUILD) { join_dir(blobd, sizeof blobd, file, "blobs"); blobdir = blobd; } |
| 122 | |
| 123 | size_t slen = 0; |
| 124 | int missing; |
| 125 | char *src = slurp(file, &slen, &missing); |
| 126 | if (!src) { fprintf(stderr, "oscv: cannot read %s\n", file); return 2; } |
| 127 | |
| 128 | OmegaResolveError err; |
| 129 | OmegaLock lock = { NULL, 0 }; |
| 130 | int have_lock = 0; |
| 131 | size_t llen = 0; |
| 132 | char *ltext = slurp(lockp, &llen, &missing); |
| 133 | if (ltext) { |
| 134 | if (omega_lock_parse(ltext, llen, &lock, &err)) { free(ltext); free(src); return refuse_resolve(&err); } |
| 135 | have_lock = 1; |
| 136 | } else if (!missing) { free(src); fprintf(stderr, "oscv: cannot read %s\n", lockp); return 2; } |
| 137 | free(ltext); |
| 138 | |
| 139 | OmegaVcStore store; |
| 140 | omega_vcstore_init(&store); |
| 141 | FILE *probe = fopen(storep, "rb"); |
| 142 | if (probe) { |
| 143 | fclose(probe); |
| 144 | int lrc = omega_vcstore_load(&store, storep); |
| 145 | if (lrc) { |
| 146 | memset(&err, 0, sizeof err); |
| 147 | err.code = OMEGA_RES_UNVERIFIED_DEPENDENCY; |
| 148 | err.store_code = lrc; |
| 149 | snprintf(err.subject, sizeof err.subject, "store"); |
| 150 | snprintf(err.message, sizeof err.message, "the Verified Crumb Store file is not intact (store says %s)", omega_vcstore_code_name(lrc)); |
| 151 | omega_vcstore_destroy(&store); omega_lock_free(&lock); free(src); |
| 152 | return refuse_resolve(&err); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | OmegaOscImports h; |
| 157 | memset(&h, 0, sizeof h); |
| 158 | h.resolver.store = &store; |
| 159 | h.resolver.domain = domain; |
| 160 | h.resolver.fetch_receipt = omega_receipt_dir_fetch; |
| 161 | h.resolver.fetch_ctx = (void *)recdir; |
| 162 | if (blobdir) { h.resolver.fetch_blob = omega_blob_dir_fetch; h.resolver.blob_ctx = (void *)blobdir; } |
| 163 | /* a BOOTSTRAP record is accepted only if the resolver's pinned VC-GENESIS-1 lists it: no option, file or variable turns that on */ |
| 164 | h.lock = have_lock ? &lock : NULL; |
| 165 | |
| 166 | OscUnit *u = malloc(sizeof *u); |
| 167 | OscDiag d; |
| 168 | int rc = 1; |
| 169 | if (!u) { fprintf(stderr, "oscv: out of memory\n"); rc = 2; goto out; } |
| 170 | if (osc_compile_imports(src, slen, u, &d, NULL, omega_osc_import_hook, &h)) { |
| 171 | printf("error kind=%s line=%u col=%u object=%s origin_line=%u other=%s transition=%s\n", |
| 172 | osc_diag_kind_name(d.kind), d.line, d.col, d.object, d.origin_line, d.other, d.transition); |
| 173 | printf("message=%s\n", d.message); |
| 174 | if (d.kind == OSC_DIAG_IMPORT_REFUSED) printf("resolve_code=%s\n", d.transition); |
| 175 | goto out; |
| 176 | } |
| 177 | { |
| 178 | uint8_t dig[32], codedig[32]; |
| 179 | char hx[65], err2[160]; |
| 180 | OscCode code; |
| 181 | memset(&code, 0, sizeof code); |
| 182 | if (osc_ir_digest(u, dig) || osc_cg_compile(u, &code, err2, sizeof err2)) { |
| 183 | printf("error kind=backend message=%s\n", err2); |
| 184 | goto out; |
| 185 | } |
| 186 | sha256_hash(code.code, code.len, codedig); |
| 187 | OmegaArtifactMeta meta; |
| 188 | omega_artifact_meta_make(&meta, domain, dig, h.closure.digest); |
| 189 | hexs(dig, hx); |
| 190 | printf("ir_sha256=%s\n", hx); |
| 191 | hexs(codedig, hx); |
| 192 | printf("code_sha256=%s\n", hx); |
| 193 | printf("funcs=%u\n", (unsigned)u->nfuncs); |
| 194 | printf("domain=%s\n", omega_domain_name(domain)); |
| 195 | printf("tainted=%d\n", meta.tainted); |
| 196 | printf("imports=%u\n", (unsigned)h.n_imports); |
| 197 | printf("closure_entries=%zu\n", h.closure.n); |
| 198 | hexs(h.closure.digest, hx); |
| 199 | printf("closure_sha256=%s\n", hx); |
| 200 | hexs(meta.build_id, hx); |
| 201 | printf("build_id=%s\n", hx); |
| 202 | osc_cg_free(&code); |
| 203 | rc = 0; |
| 204 | if (metaout) { |
| 205 | char text[512]; |
| 206 | FILE *mf = omega_artifact_meta_text(&meta, text, sizeof text) > 0 ? fopen(metaout, "wb") : NULL; |
| 207 | if (!mf || fputs(text, mf) < 0 || fclose(mf)) { fprintf(stderr, "oscv: cannot write %s\n", metaout); rc = 2; } |
| 208 | } |
| 209 | } |
| 210 | out: |
| 211 | omega_closure_free(&h.closure); |
| 212 | omega_vcstore_destroy(&store); |
| 213 | omega_lock_free(&lock); |
| 214 | free(u); |
| 215 | free(src); |
| 216 | return rc; |
| 217 | } |