interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_physics_dir.h

136 lines · 6055 bytes Raw Blame History
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#ifndef OMEGA_PHYSICS_DIR_H
18#define OMEGA_PHYSICS_DIR_H
19
20/* Run-time resolution of the physics checkout used by the omegatool gate runners
21 * (M17 physics authority, M18 gate 16, M19 clean-clone qualification).
22 *
23 * The physics location is NOT compiled into the binary (it used to be
24 * -DOMEGA_PHYSICS_DIR, which made omegatool's bytes depend on where physics was
25 * checked out). Rule, in order:
26 * 1. the PHYSICS_DIR environment variable, if set and non-empty (make exports
27 * command-line variables, so `make PHYSICS_DIR=/x test-m17` passes it down);
28 * 2. otherwise the project default "../physics", relative to the current directory
29 * (the same default as Makefile `PHYSICS_DIR ?= ../physics`, run from the omega root).
30 * The chosen directory must contain m16/m16_native.h; its realpath is returned.
31 * On failure nothing is guessed: the function returns 0 and writes a message that names
32 * the value tried, where it came from, and how to fix it. */
33
34#include <limits.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <strings.h>
39#include <unistd.h>
40
41#define OMEGA_PHYSICS_ERR_SIZE (2 * PATH_MAX + 256)
42
43static inline int omega_physics_dir_resolve(char *out, size_t out_size, char *err, size_t err_size) {
44 const char *env = getenv("PHYSICS_DIR");
45 const char *src = (env && env[0] != '\0') ? "PHYSICS_DIR environment variable" : "default ../physics";
46 const char *pd = (env && env[0] != '\0') ? env : "../physics";
47 char real[PATH_MAX];
48 char probe[PATH_MAX + 32];
49 if (!realpath(pd, real)) {
50 snprintf(err, err_size,
51 "physics checkout not found: %s (%s) does not resolve; set PHYSICS_DIR to the physics checkout",
52 pd, src);
53 return 0;
54 }
55 if (strchr(real, '\'') != NULL || strlen(real) + 1 > out_size) {
56 snprintf(err, err_size, "physics checkout path unusable (quote or too long): %s (%s)", real, src);
57 return 0;
58 }
59 snprintf(probe, sizeof(probe), "%s/m16/m16_native.h", real);
60 if (access(probe, R_OK) != 0) {
61 snprintf(err, err_size,
62 "not a physics checkout: %s (%s) has no m16/m16_native.h; set PHYSICS_DIR to the physics checkout",
63 real, src);
64 return 0;
65 }
66 snprintf(out, out_size, "%s", real);
67 return 1;
68}
69
70
71/* Authority check for a resolved physics dir (the run-time dir may differ from the one make checked at
72 * build time, so the gate must re-check it). `lock_path` holds the pinned commit: first line, trimmed,
73 * full 40 hex digits (malformed is refused). `git -C <pd> rev-parse HEAD` must equal it and, when
74 * `require_clean`, `git -C <pd> status --porcelain` must succeed and print nothing. Any git failure
75 * (not a repository, git missing) is a refusal; nothing is hidden. */
76static inline int omega_physics_dir_verify(const char *pd, const char *lock_path, int require_clean,
77 char *err, size_t err_size) {
78 char want[128] = {0}, got[256] = {0}, cmd[PATH_MAX + 128], line[256];
79 FILE *f = fopen(lock_path, "r");
80 if (!f) {
81 snprintf(err, err_size, "cannot read %s (cwd-relative; run from the omega root): physics commit pin unknown", lock_path);
82 return 0;
83 }
84 if (!fgets(want, sizeof(want), f)) want[0] = '\0';
85 fclose(f);
86 size_t n = strlen(want);
87 while (n > 0 && (want[n - 1] == '\n' || want[n - 1] == '\r' || want[n - 1] == ' ' || want[n - 1] == '\t')) want[--n] = '\0';
88 int ok = (n == 40);
89 for (size_t i = 0; ok && i < 40; i++) {
90 char c = want[i];
91 ok = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
92 }
93 if (!ok) {
94 snprintf(err, err_size, "%s is malformed: first line must be one full 40-hex commit", lock_path);
95 return 0;
96 }
97 snprintf(cmd, sizeof(cmd), "git -C '%s' rev-parse HEAD 2>&1", pd);
98 FILE *p = popen(cmd, "r");
99 int rc = -1;
100 if (p) {
101 if (!fgets(got, sizeof(got), p)) got[0] = '\0';
102 rc = pclose(p);
103 }
104 n = strlen(got);
105 while (n > 0 && (got[n - 1] == '\n' || got[n - 1] == '\r')) got[--n] = '\0';
106 if (rc != 0 || strcasecmp(got, want) != 0) {
107 snprintf(err, err_size, "physics dir %s is not the pinned checkout: expected commit %s, observed %s",
108 pd, want, rc != 0 ? "(not a git checkout or git failed)" : got);
109 return 0;
110 }
111 if (require_clean) {
112 snprintf(cmd, sizeof(cmd), "git -C '%s' status --porcelain 2>&1", pd);
113 p = popen(cmd, "r");
114 int dirty = 1;
115 rc = -1;
116 if (p) {
117 dirty = (fgets(line, sizeof(line), p) != NULL);
118 rc = pclose(p);
119 }
120 if (rc != 0 || dirty) {
121 snprintf(err, err_size, "physics dir %s at pinned commit %s is %s", pd, want,
122 rc != 0 ? "unreadable by git status" : "dirty (git status --porcelain not empty)");
123 return 0;
124 }
125 }
126 return 1;
127}
128
129/* resolve + verify against ./physics.lock (cwd-relative, like src/omega_evidence.c). */
130static inline int omega_physics_dir_resolve_pinned(char *out, size_t out_size, int require_clean,
131 char *err, size_t err_size) {
132 return omega_physics_dir_resolve(out, out_size, err, err_size) &&
133 omega_physics_dir_verify(out, "physics.lock", require_clean, err, err_size);
134}
135
136#endif