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 / src / omega_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 | #include "omega_evidence.h" |
| 18 | #include "sha256.h" |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | static bool trim_line(char *buf) { |
| 31 | size_t len = strlen(buf); |
| 32 | while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r' || buf[len - 1] == ' ')) { |
| 33 | buf[--len] = '\0'; |
| 34 | } |
| 35 | return len > 0; |
| 36 | } |
| 37 | |
| 38 | static bool popen_read_line(const char *cmd, char *out, size_t n) { |
| 39 | FILE *p = popen(cmd, "r"); |
| 40 | if (!p) return false; |
| 41 | char buf[256] = {0}; |
| 42 | bool got = (fgets(buf, sizeof(buf), p) != NULL); |
| 43 | pclose(p); |
| 44 | if (!got || !trim_line(buf)) return false; |
| 45 | size_t len = strlen(buf); |
| 46 | if (len >= n) return false; |
| 47 | memcpy(out, buf, len + 1); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /* c when it is a lower-case hex digit, -1 otherwise */ |
| 52 | static int hex_byte(int c) { |
| 53 | return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ? c : -1; |
| 54 | } |
| 55 | |
| 56 | /* ---- run_id -------------------------------------------------------------- */ |
| 57 | |
| 58 | static char g_run_id[96] = {0}; |
| 59 | static bool g_run_id_ready = false; |
| 60 | |
| 61 | const char *omega_evidence_run_id(void) { |
| 62 | if (!g_run_id_ready) { |
| 63 | time_t now = time(NULL); |
| 64 | struct tm tm_info; |
| 65 | gmtime_r(&now, &tm_info); |
| 66 | char ts[32]; |
| 67 | strftime(ts, sizeof(ts), "%Y%m%dT%H%M%SZ", &tm_info); |
| 68 | |
| 69 | /* The run id names the evidence directory (omega_evidence_path), so git's answer is kept only as |
| 70 | * lower-case hex, copied byte by byte; anything else is "unknown". */ |
| 71 | char line[64], sha[64]; |
| 72 | size_t k = 0; |
| 73 | if (popen_read_line("git rev-parse --short=12 HEAD 2>/dev/null", line, sizeof(line))) { |
| 74 | for (; line[k]; k++) { |
| 75 | int c = hex_byte((unsigned char)line[k]); |
| 76 | if (c < 0) break; |
| 77 | sha[k] = (char)c; |
| 78 | } |
| 79 | if (line[k]) k = 0; |
| 80 | } |
| 81 | sha[k] = '\0'; |
| 82 | if (k == 0) snprintf(sha, sizeof(sha), "unknown"); |
| 83 | snprintf(g_run_id, sizeof(g_run_id), "%s-%s", ts, sha); |
| 84 | g_run_id_ready = true; |
| 85 | } |
| 86 | return g_run_id; |
| 87 | } |
| 88 | |
| 89 | /* ---- evidence path --------------------------------------------------- */ |
| 90 | |
| 91 | static void mkdir_p_for_file(const char *filepath) { |
| 92 | char tmp[1024]; |
| 93 | snprintf(tmp, sizeof(tmp), "%s", filepath); |
| 94 | for (char *p = tmp + 1; *p; p++) { |
| 95 | if (*p == '/') { |
| 96 | *p = '\0'; |
| 97 | mkdir(tmp, 0755); |
| 98 | *p = '/'; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | int omega_evidence_path(const char *relpath, char *out, size_t n) { |
| 104 | if (!relpath || !out || n == 0) return -1; |
| 105 | |
| 106 | const char *run_id = omega_evidence_run_id(); |
| 107 | char candidate[1024]; |
| 108 | |
| 109 | int len = snprintf(candidate, sizeof(candidate), "build/qual-runs/%s/%s", run_id, relpath); |
| 110 | if (len < 0 || (size_t)len >= sizeof(candidate)) return -1; |
| 111 | |
| 112 | mkdir_p_for_file(candidate); |
| 113 | |
| 114 | if (strlen(candidate) >= n) return -1; |
| 115 | strcpy(out, candidate); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int omega_evidence_write_digest(const char *relstem, const char *ext, const void *buf, |
| 120 | size_t len, char *out, size_t n) { |
| 121 | if (!relstem || !ext || (!buf && len) || !out || n == 0) return -1; |
| 122 | uint8_t d[SHA256_DIGEST_SIZE]; |
| 123 | sha256_hash((const uint8_t *)buf, len, d); |
| 124 | char hex[2 * SHA256_DIGEST_SIZE + 1]; |
| 125 | for (int i = 0; i < SHA256_DIGEST_SIZE; i++) snprintf(hex + 2 * i, 3, "%02x", d[i]); |
| 126 | char rel[768]; |
| 127 | int k = snprintf(rel, sizeof rel, "%s.%s.%s", relstem, hex, ext); |
| 128 | if (k < 0 || (size_t)k >= sizeof rel) return -1; |
| 129 | if (omega_evidence_path(rel, out, n) != 0) return -1; |
| 130 | int fd = open(out, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0444); |
| 131 | if (fd < 0) return errno == EEXIST ? 1 : -1; |
| 132 | const uint8_t *p = buf; |
| 133 | size_t left = len; |
| 134 | while (left) { |
| 135 | ssize_t w = write(fd, p, left); |
| 136 | if (w < 0 && errno == EINTR) continue; |
| 137 | if (w <= 0) { |
| 138 | close(fd); |
| 139 | unlink(out); |
| 140 | return -1; |
| 141 | } |
| 142 | p += w; |
| 143 | left -= (size_t)w; |
| 144 | } |
| 145 | if (close(fd) != 0) { |
| 146 | unlink(out); |
| 147 | return -1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | FILE *omega_evidence_create(const char *path) { |
| 153 | if (!path) return NULL; |
| 154 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 155 | if (fd < 0) return NULL; |
| 156 | FILE *f = fdopen(fd, "w"); |
| 157 | if (!f) close(fd); |
| 158 | return f; |
| 159 | } |
| 160 | |
| 161 | /* ---- commit identity --------------------------------------------------- */ |
| 162 | |
| 163 | bool omega_evidence_run_commit(char out[41]) { |
| 164 | char sha[64]; |
| 165 | if (!popen_read_line("git rev-parse HEAD 2>/dev/null", sha, sizeof(sha))) return false; |
| 166 | if (strlen(sha) != 40) return false; |
| 167 | memcpy(out, sha, 41); |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | bool omega_evidence_tree_dirty(void) { |
| 172 | FILE *p = popen("git status --porcelain --untracked-files=normal 2>/dev/null", "r"); |
| 173 | if (!p) return true; /* fail safe: unknown treated as dirty */ |
| 174 | char buf[256]; |
| 175 | bool any = (fgets(buf, sizeof(buf), p) != NULL); |
| 176 | pclose(p); |
| 177 | return any; |
| 178 | } |
| 179 | |
| 180 | bool omega_evidence_physics_commit(char *out, size_t n) { |
| 181 | FILE *f = fopen("physics.lock", "r"); |
| 182 | if (!f) return false; |
| 183 | char buf[256] = {0}; |
| 184 | bool got = (fgets(buf, sizeof(buf), f) != NULL); |
| 185 | fclose(f); |
| 186 | if (!got || !trim_line(buf)) return false; |
| 187 | size_t len = strlen(buf); |
| 188 | if (len >= n) return false; |
| 189 | memcpy(out, buf, len + 1); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /* ---- hardware identity --------------------------------------------------- */ |
| 194 | |
| 195 | void omega_evidence_hardware_from_nvrm(const Nvrm *rm, OmegaEvidenceHardware *out) { |
| 196 | if (!out) return; |
| 197 | memset(out, 0, sizeof(*out)); |
| 198 | out->alias = "sm_121"; |
| 199 | if (!rm) return; |
| 200 | out->compute_class = rm->compute_class; |
| 201 | out->rm_sm_version = rm->sm_version; |
| 202 | for (int i = 0; i < 16; i++) { |
| 203 | snprintf(&out->gpu_uuid_hex[i * 2], 3, "%02x", rm->gpu_uuid[i]); |
| 204 | } |
| 205 | out->gpu_uuid_hex[32] = '\0'; |
| 206 | } |
| 207 | |
| 208 | bool omega_evidence_self_binary(char *out, size_t n) { |
| 209 | if (!out || n == 0) return false; |
| 210 | /* The program name comes from the running executable, never a literal: |
| 211 | * this file is linked into production binaries (rx_r13_living_host), and |
| 212 | * tools/r16_authpath.sh refuses any legacy program name in their strings. */ |
| 213 | char self[4096]; |
| 214 | ssize_t r = readlink("/proc/self/exe", self, sizeof(self) - 1); |
| 215 | if (r <= 0) { |
| 216 | out[0] = '\0'; |
| 217 | return false; |
| 218 | } |
| 219 | self[r] = '\0'; |
| 220 | const char *slash = strrchr(self, '/'); |
| 221 | const char *name = slash ? slash + 1 : self; |
| 222 | const char *out_dir = getenv("OUT_DIR"); |
| 223 | if (!out_dir || !*out_dir) out_dir = "build"; |
| 224 | int len = snprintf(out, n, "%s/%s", out_dir, name); |
| 225 | if (len > 0 && (size_t)len < n && access(out, R_OK) == 0) return true; |
| 226 | len = snprintf(out, n, "%s", self); |
| 227 | if (len <= 0 || (size_t)len >= n) { |
| 228 | out[0] = '\0'; |
| 229 | return false; |
| 230 | } |
| 231 | return access(out, R_OK) == 0; |
| 232 | } |