Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/37db8dbac125dc12 # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## Put the M18 clean-clone log in a directory the gate owns closed `37db8dbac125dc12` #935 · opened 2026-10-30 11:27 UTC · closed 2026-10-31 11:06 UTC Imported from GitHub aien-dev/omega-mv#935, by drakestapleton Base `62d1d0d668f897386a0e311374f8dbcc5c32dde8` · Head `396b3d496a6852d5dffbf5e21c24752b157acc91` drakestapleton wrote on 2026-10-30 11:27 UTC Refs #526 ## Problem The M18 clean-clone step (the one M19 chains) redirected its log to one fixed path on the host temp directory (`/tmp/clean_clone_m18.log`, checkout `/tmp/omega_clean_m18`). That name is shared by every account and by concurrent runs. On a sticky temp directory the second user gets `cannot create ... Permission denied` and the suite reports the miss as a regression, before the run reaches the chip. Judge run of `cf5495d0` failed that way (`REGRESSION 158/159`). Reproduced on main `62d1d0d6`: `test_m18_gate16_clean_clone` shells out `rm -rf /tmp/omega_clean_m18 && cp -r "$root" /tmp/omega_clean_m18 && ... >/tmp/clean_clone_m18.log`. A symlink planted at `/tmp/clean_clone_m18.log` was followed and the redirect overwrote a foreign file. ## Change The log is created under `OUT_DIR/clean-clone` when `OUT_DIR` is set, otherwise under `$TMPDIR`. The gate prints the path. A failed create is `M18_CLEAN_CLONE_LOG_CREATE_FAILED` and the copy is not started. `git`, `cp` and `make` are started directly, so a path is an argument, not shell syntax. Host-test fixes on `8afcb610d` (this push): four `snprintf` results are length-checked so the test builds under `-Werror=format-truncation`. Read-only `/tmp` isolation uses bubblewrap. This kernel refuses `unshare --user --mount` (`apparmor_restrict_unprivileged_userns=1`, EPERM). The observable is the same: `open("/tmp/...")` returns EROFS. ## Evidence Host, no GPU. Spark, 2026-10-30. Base of the branch includes main `62d1d0d6`. Head `8afcb610da45720f84d05def9b88837f1922b3e5`. ``` cd ~/workspace/omega-w3-PrFinish935 TMPDIR=/dev/shm taskset -c 15-19 nice -n 19 ionice -c3 make test-m18-clean-clone-log ``` Observed: exit 0. `PASS /tmp unwritable with TMPDIR set`. `PASS mutant M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP`. Final line `m18_clean_clone_log_test: PASS`. Negative control: the script's mutant restores `snprintf(..., "/tmp/clean_clone_m18.log")` and runs it under bubblewrap with `/tmp` read-only and `M18_REQUIRE_TMP_UNWRITABLE=1`. The mutant is caught (the PASS line above is that catch; a surviving mutant would fail the script). Strace of the good binary (`strace -f -e trace=openat,mkdir,unlink,unlinkat,rename,symlink,link,creat`, 47 calls, work dir under `/dev/shm`, exit 0): zero `open`/`creat`/`mkdir` of a path under `/tmp`. The only traced call naming `/tmp` is `unlinkat("/tmp/m18_pwned_marker")` returning ENOENT, the hostile-path check that a shell did not create that marker. Every log landed under the private work directory. crumb compile: 166 crumbs, 0 rewritten. crumb verify: OK. ## NOT_RUN - M18/M19 on GB10. No GPU. The command that would run the chip path is the existing M18 gate under a spark-window; not run. - Channel-create refusal in #523 (physics-mv#64 and a `physics.lock` bump) is a separate change. This PR does not include it. - M19's own clone directory template is still a private `mkdtemp` under `/tmp`. Status: IMPLEMENTED and EXERCISED on the host. Not QUALIFIED, not MERGED. drakestapleton reviewed (COMMENTED) on 2026-10-30 11:37 UTC The head is still `541d790b`. No main merge, CI fix or CONTRIBUTORS fix has landed on this branch yet. Status: - Mergeable: false (`dirty`) against main `348c7ef6`. - The diff edits three regenerated .crumb files (root +13/-13, src/ +5/-5, tests/ +37/-37) and CONTRIBUTORS.md, which conflict with every recent main merge. Please merge main and regenerate the crumbs with the crumb.lock tool (`d0f0b95d`) instead of hand-resolving them. CI on this head: - AIEN Checks: did not run - evidence-immutable: did not run - Only CodeQL ran. All five analyses succeeded, including c-cpp, which completed 6:33 AM CT, but the CodeQL summary check is `failure`. That matches the pattern on token-authored pushes. A push or re-run from a human account is needed to start AIEN Checks and evidence-immutable. CONTRIBUTORS.md L47 credits "Grok 4.7". Please credit the agent the way the rest of the table does, without a model or version name, e.g. "Grokbot". Code findings (src/omega_blackwell_gates.c +60/-13; new src/omega_clean_clone.c/.h; tests/m18_clean_clone_log_test.{c,sh}; Makefile +1/-1): - The fix scope matches the description. The M18 log moves to `OUT_DIR/clean-clone` or `$TMPDIR`, the gate prints the path, and a failed create reports `M18_CLEAN_CLONE_LOG_CREATE_FAILED` instead of REGRESSION. - The mutant that puts the fixed `/tmp` path back is covered. - M19 on GB10: NOT_RUN. M19's own `mkdtemp` under /tmp is unchanged, as stated. Not mergeable until main is merged in, the crumbs are regenerated, AIEN Checks and evidence-immutable are green, and the CONTRIBUTORS credit is fixed. — Grokbot github-advanced-security[bot] reviewed (COMMENTED) on 2026-10-30 12:26 UTC github-advanced-security[bot] commented on `src/omega_blackwell_gates.c` line 1040 on 2026-10-30 12:26 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (an environment variable) and then passed to omega_m18_clean_clone_run(log_path), which calls run_direct(log_path), which calls open(__file). This argument to a file access function is derived from user input (an environment variable) and then passed to omega_m18_clean_clone_run(log_path), which calls run_direct(log_path), which calls open(__file). Show more details ### Fetch it A git bundle of the pull request's head, 39413 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/37db8dbac125dc12.bundle -o 37db8dbac125dc12.bundle git fetch 37db8dbac125dc12.bundle refs/pull/935/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 935 396b3d496a6852d5dffbf5e21c24752b157acc91 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":935,"url":"https://github.com/aien-dev/omega-mv/pull/935","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:43Z"} ``` ### Changes ``` diff --git a/.crumb b/.crumb index f3e51fe46..06cd80161 100644 --- a/.crumb +++ b/.crumb @@ -72,38 +72,38 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "digest": "6e711c2f6624b667ceba739ebb8a54681128bc042ccc455f5850b7cfce90c15a", + "last_commit": "658f11f5c", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "6d36f20967b8ca5e1e6f02dbd5332ae3a7e7da932dfc9743bef389470c31f604", + "last_commit": "8afcb610d", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:0cedc0307a61d28477398ec2a8415d22e7844af4fb66a6fe116ff09c42300c02", + "compiled_at": "2026-10-30T16:37:11Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:4551ea52d2cad086edb2784490cf701c5126b1ba81c330685b3e4264abc08b7c", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "files": 17853, + "generated_at_commit": "8afcb610da45720f84d05def9b88837f1922b3e5", + "source_tree": "sha256:49f43171f8472d154d7ed8adc3d37cee49b4a033de6d0d7f0086e14dd18dbdd3" }, "owner_lane": "", "provenance": { diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dda6d20b5..ab4d822ea 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -48,4 +48,5 @@ Update this table in the same commit as the work it credits. | Codex | Integrated FR-9 with compiler batch 3 and the latest quantity/diagnostic audit; preserved empty text and integer-bitcast behavior on x86 and kept seed bootstrap within its function budget by removing unused helpers and inlining one single-use helper. | | Codex | Fixed x86 quantity unary refusals and empty-text emission; added explicit per-target acceptance contracts, preserved historical failed experiments, and exercised missing diagnostic callers and stale-binding controls. | +| grok-4.7 | M18 clean-clone log path (omega#526): the gate writes `clean_clone_m18.log` under its own `OUT_DIR/clean-clone` or under `TMPDIR`, prints that path, and reports a failed create as its own error. `git`, `cp` and `make` are started directly, so a path from `OUT_DIR` or `TMPDIR` is an argument or an environment value. Host test covers an unwritable `/tmp`, a set `TMPDIR`, a directory name containing a space, a quote and `;`, and a mutant that puts the fixed path back. `make test-m18-clean-clone-log` compiles that test with `src/omega_clean_clone.c` only. A program that cannot be started is reported as a failed exec, not as exit 127. | | Grok 4.7 | Merged current main (b3dca0f1) into #514. The only conflict was the generated root `.crumb`; regenerated crumbs with the crumb.lock tool (d0f0b95d). | diff --git a/Makefile b/Makefile index 6b53cd124..123080d86 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ SRCS = src/omega_om_units.c src/omega_canonical.c src/omega_validate.c src/omega src/omega_matvec.c src/omega_accelerator.c src/omega_accelerator_world.c \ src/omega_vector.c src/omega_blackwell_encoder.c src/omega_blackwell_qmd.c \ src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_gpu_session.c src/omega_gpu_platform.c src/omega_gpu_platform_linux.c src/omega_gpu_fmt.c src/omega_gpu_matmul_api.c src/omega_gpu_elementwise_api.c src/omega_gpu_attention_api.c src/omega_gpu_serving.c src/omega_world_gates.c src/omega_gpu_wait.c \ - src/omega_evidence.c \ + src/omega_evidence.c src/omega_clean_clone.c \ $(PHYSICS_DIR)/m16/m16_native.c $(PHYSICS_DIR)/nvrm/nvrm.c \ tools/omegatool.c # OWN-30B cut 2 (2026-10-13): column windows over a resident bf16 tensor for the MoE path. New file, @@ -203,6 +203,16 @@ clean: test-m19: $(TARGET) ./$(TARGET) --run-m19-gates +# Host-only M18 clean-clone log test (omega#526). The script compiles +# tests/m18_clean_clone_log_test.c and src/omega_clean_clone.c and nothing +# else. It does not link $(SRCS): src/omega_om_units.c is AArch64 assembly, +# and an x86 link of omegatool fails. AIEN Checks (ubuntu-latest) does not +# run this repository's make; it verifies crumbs and the portable secret +# scan named on origin/main. This target is the x86-runnable piece. +.PHONY: test-m18-clean-clone-log +test-m18-clean-clone-log: + sh tests/m18_clean_clone_log_test.sh + # Host-only tests of the M19R qualifier (tools/m19r_qualify.sh) and its # canonical-JSON helper (tools/json_canon.c). No GPU. .PHONY: test-m19r-qualify diff --git a/src/.crumb b/src/.crumb index 7c4b83da7..13a7e4e71 100644 --- a/src/.crumb +++ b/src/.crumb @@ -188,14 +188,14 @@ } ], "children_root": "sha256:2f729945214be989a74d7c2064ae4ba3c2deb52ebea3f334b459198a7bd032ed", - "compiled_at": "2026-10-30T02:49:13Z", + "compiled_at": "2026-10-30T12:49:15Z", "compiler": "crumb-compile/1", - "digest": "sha256:aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", + "digest": "sha256:6e711c2f6624b667ceba739ebb8a54681128bc042ccc455f5850b7cfce90c15a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 568, - "generated_at_commit": "2cb1ccb5d3c1a1ce1cb94a7a3dd3c221395c4e15", - "source_tree": "sha256:7f1fef46a27e8baf5cf567bb590d417ede3767c5f07e714d0f0812af0645bb4b" + "files": 570, + "generated_at_commit": "d290045796af105857fd676d7a2297c32a9a2230", + "source_tree": "sha256:49c67409931d5091b088142bd3607b48f2029611a419f4bc3f254ad7348d9492" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_gates.c b/src/omega_blackwell_gates.c index 54ec7f769..7d9b95c36 100644 --- a/src/omega_blackwell_gates.c +++ b/src/omega_blackwell_gates.c @@ -34,6 +34,7 @@ #include #include #include "omega_physics_dir.h" +#include "omega_clean_clone.h" static int m17_gate_count = 0; static int m17_gate_passed = 0; @@ -997,25 +998,54 @@ static bool test_m18_gate16_clean_clone(void) { } /* Copy the repo this omegatool runs from (the code under test), not a * hardcoded checkout, and pass the PHYSICS_DIR this process resolves at run time - * (omega_physics_dir.h) as an absolute path so the copy in /tmp does not fall back to - * the Makefile default ../physics (= /tmp/physics). Mirrors M19. */ + * (omega_physics_dir.h) as an absolute path so the copy does not fall back to + * the Makefile default ../physics. Mirrors M19. + * + * omega#526: the log used to be one fixed path on the host temp directory, + * shared by every account. Sticky permissions made the second user fail + * before the chip, and the suite reported that as a regression. The log now + * lives under this gate's OUT_DIR/clean-clone, or under TMPDIR when that is + * the directory the gate can use. The path is created first and printed. + * git, cp and make are started directly. Those paths are arguments or + * environment values, not text in a shell command. */ char physics_dir_resolved[PATH_MAX], physics_err[OMEGA_PHYSICS_ERR_SIZE]; if (!omega_physics_dir_resolve_pinned(physics_dir_resolved, sizeof(physics_dir_resolved), 0, physics_err, sizeof(physics_err))) { fprintf(stderr, "%s\n", physics_err); return false; } - char command[4608]; - int len = snprintf(command, sizeof(command), - "root=$(git rev-parse --show-toplevel) && " - "rm -rf /tmp/omega_clean_m18 && " - "cp -r \"$root\" /tmp/omega_clean_m18 && " - "cd /tmp/omega_clean_m18 && " - "make OUT_DIR=build clean >/dev/null 2>&1 && " - "make -j OUT_DIR=build PHYSICS_DIR='%s' >/dev/null 2>&1 && " - "OMEGA_IN_CLEAN_CLONE=1 ./build/omegatool --run-m18-gates >/tmp/clean_clone_m18.log 2>&1", - physics_dir_resolved); - if (len < 0 || (size_t)len >= sizeof(command)) return false; - int rc = system(command); + char repo[PATH_MAX]; + const char *repo_root = NULL; + if (omega_m18_git_toplevel(repo, sizeof(repo)) == 0) repo_root = repo; + + char checkout_dir[PATH_MAX]; + char log_path[PATH_MAX]; + char err[PATH_MAX + 256]; + /* temp_fallback is the C library's temp directory. It is used only for the + * copy, and only when OUT_DIR sits inside the repo and TMPDIR is unset. + * The log file is not placed there when OUT_DIR or TMPDIR is set. */ + int st = omega_m18_clean_clone_plan(getenv("OUT_DIR"), getenv("TMPDIR"), repo_root, P_tmpdir, + physics_dir_resolved, + checkout_dir, sizeof(checkout_dir), + log_path, sizeof(log_path), + err, sizeof(err)); + if (log_path[0]) omega_m18_clean_clone_print_log_path(log_path); + if (st == OMEGA_M18_CLEAN_CLONE_LOG_CREATE_FAILED) { + fprintf(stderr, "%s\n", err); + return false; + } + if (st != OMEGA_M18_CLEAN_CLONE_OK) { + fprintf(stderr, "%s\n", err); + return false; + } + int rc = omega_m18_clean_clone_run(checkout_dir, log_path, physics_dir_resolved); + if (rc == OMEGA_M18_CLEAN_CLONE_EXEC_FAILED) { + fprintf(stderr, "M18 clean clone failed to exec; log: %s\n", log_path); + } else if (rc != 0) { + fprintf(stderr, "M18 clean clone failed (rc %d); log: %s\n", rc, log_path); + } + if (omega_m18_clean_clone_remove_checkout(checkout_dir, log_path) != 0) { + fprintf(stderr, "M18 clean clone: could not remove checkout under %s\n", checkout_dir); + } return (rc == 0); } diff --git a/src/omega_clean_clone.c b/src/omega_clean_clone.c new file mode 100644 index 000000000..f3ff12296 --- /dev/null +++ b/src/omega_clean_clone.c @@ -0,0 +1,499 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "omega_clean_clone.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void clear(char *s, size_t n) { + if (s && n) s[0] = '\0'; +} + +/* A path that fits in the buffers the runner passes to exec. Quotes, spaces + * and ';' are ordinary characters: they never go through a shell. */ +static int path_fits(const char *s) { + if (!s || !s[0]) return 0; + if (strlen(s) >= 4096) return 0; + return 1; +} + +/* Relative paths are inside the repo the gate was started from. An absolute + * path is inside when it is the repo root or a child of it. */ +static int is_inside_repo(const char *path, const char *repo) { + if (!repo || !repo[0] || !path || !path[0]) return 0; + if (path[0] != '/') return 1; + size_t n = strlen(repo); + if (n == 0) return 0; + if (strncmp(path, repo, n) != 0) return 0; + return path[n] == '\0' || path[n] == '/'; +} + +static int mkdir_one(const char *path) { + if (mkdir(path, 0755) == 0) return 0; + if (errno == EEXIST) { + struct stat st; + if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) return 0; + errno = ENOTDIR; + } + return -1; +} + +/* Create the last component and, when missing, its parent. */ +static int mkdir_dir(const char *dir) { + char parent[4096]; + int n = snprintf(parent, sizeof(parent), "%s", dir); + if (n < 0 || (size_t)n >= sizeof(parent)) { + errno = ENAMETOOLONG; + return -1; + } + char *sl = strrchr(parent, '/'); + if (sl && sl != parent) { + *sl = '\0'; + if (mkdir_one(parent) != 0) return -1; + } + return mkdir_one(dir); +} + +static int fail_create(char *err, size_t err_cap, const char *path) { + int e = errno; + if (!path || !path[0]) path = "(unknown)"; + snprintf(err, err_cap, "M18_CLEAN_CLONE_LOG_CREATE_FAILED: cannot create %s: %s", + path, strerror(e)); + return OMEGA_M18_CLEAN_CLONE_LOG_CREATE_FAILED; +} + +static int fail_checkout(char *err, size_t err_cap, const char *path) { + int e = errno; + if (!path || !path[0]) path = "(unknown)"; + snprintf(err, err_cap, "M18_CLEAN_CLONE_CHECKOUT_DIR_FAILED: cannot create %s: %s", + path, strerror(e)); + return OMEGA_M18_CLEAN_CLONE_CHECKOUT_DIR_FAILED; +} + +static int fail_plan(char *err, size_t err_cap, const char *why) { + snprintf(err, err_cap, "M18_CLEAN_CLONE_LOG_PLAN_FAILED: %s", why); + return OMEGA_M18_CLEAN_CLONE_PLAN_FAILED; +} + +static int copy_path(char *dst, size_t cap, const char *src) { + int n = snprintf(dst, cap, "%s", src); + if (n < 0 || (size_t)n >= cap) return -1; + return 0; +} + +/* One place builds the log file name. A host mutant that puts the old fixed + * path back is caught by M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP. */ +static int set_log_name(char *log_path, size_t log_cap, const char *dir) { + int n = snprintf(log_path, log_cap, "%s/clean_clone_m18.log", dir); + if (n < 0 || (size_t)n >= log_cap) return -1; + return 0; +} + +static int make_private(const char *parent, char *parent_out, size_t cap, char *err, size_t err_cap) { + char tmpl[4096]; + int n = snprintf(tmpl, sizeof(tmpl), "%s/clean-clone-XXXXXX", parent); + if (n < 0 || (size_t)n >= sizeof(tmpl)) { + return fail_plan(err, err_cap, "private directory path does not fit"); + } + if (!mkdtemp(tmpl)) { + int e = errno; + snprintf(parent_out, cap, "%s", tmpl); + errno = e; + return fail_checkout(err, err_cap, tmpl); + } + if (copy_path(parent_out, cap, tmpl) != 0) { + return fail_plan(err, err_cap, "private directory path does not fit"); + } + return OMEGA_M18_CLEAN_CLONE_OK; +} + +/* Open the log by entering its directory and opening the fixed file name. + * The directory may come from OUT_DIR or TMPDIR. That string is a working + * directory, not the path passed to open. */ +static int open_created_log(const char *log_path) { + static const char base[] = "clean_clone_m18.log"; + if (!log_path) { + errno = EINVAL; + return -1; + } + size_t n = strlen(log_path); + size_t b = sizeof(base) - 1; + if (n < b + 2 || log_path[n - b - 1] != '/' || + strcmp(log_path + (n - b), base) != 0) { + errno = EINVAL; + return -1; + } + size_t dlen = n - b - 1; + char dir[4096]; + if (dlen >= sizeof(dir)) { + errno = ENAMETOOLONG; + return -1; + } + memcpy(dir, log_path, dlen); + dir[dlen] = '\0'; + int cwd = open(".", O_RDONLY | O_CLOEXEC); + if (cwd < 0) return -1; + if (chdir(dir) != 0) { + int e = errno; + close(cwd); + errno = e; + return -1; + } + int fd = open("clean_clone_m18.log", + O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC | O_NOFOLLOW, 0644); + int e = errno; + if (fchdir(cwd) != 0) { + int back = errno; + close(cwd); + if (fd >= 0) close(fd); + errno = back; + return -1; + } + close(cwd); + if (fd < 0) errno = e; + return fd; +} + +static int create_log_file(const char *log_path, char *err, size_t err_cap) { + int fd = open_created_log(log_path); + if (fd < 0) return fail_create(err, err_cap, log_path); + if (close(fd) != 0) return fail_create(err, err_cap, log_path); + return OMEGA_M18_CLEAN_CLONE_OK; +} + +/* The nested tool runs with its cwd inside the copy. A relative log path + * would be created in the copy instead of the directory this function just + * proved it could write. */ +static int make_absolute(char *path, size_t cap) { + if (!path || !path[0] || path[0] == '/') return 0; + char cwd[4096]; + if (!getcwd(cwd, sizeof(cwd))) return -1; + char joined[4096]; + int n = snprintf(joined, sizeof(joined), "%s/%s", cwd, path); + if (n < 0 || (size_t)n >= sizeof(joined)) return -1; + return copy_path(path, cap, joined); +} + +/* Tell the parent that exec did not happen. The write end is close-on-exec, + * so a successful exec closes it and the parent sees EOF instead of a byte. */ +static void note_exec_failed(int fd) { + unsigned char b = 1; + ssize_t w = write(fd, &b, 1); + (void)w; + _exit(126); +} + +/* file is the program name, always a literal at the call. argv may hold + * paths. dir, when set, is the child's cwd. out_fd, when >= 0, becomes + * the child's stdout and stderr. One environment value is set in the child only. + * A program that exits does so with its own status (make exits 2 when a + * recipe exits 127). A failure before exec is OMEGA_M18_CLEAN_CLONE_EXEC_FAILED. */ +static int run_direct(const char *file, char *const argv[], const char *dir, + int out_fd, const char *env_name, const char *env_value) { + if (!file || !file[0] || !argv || !argv[0]) return -1; + int errpipe[2]; + if (pipe(errpipe) != 0) return -1; + if (fcntl(errpipe[0], F_SETFD, FD_CLOEXEC) != 0 || + fcntl(errpipe[1], F_SETFD, FD_CLOEXEC) != 0) { + close(errpipe[0]); + close(errpipe[1]); + return -1; + } + pid_t pid = fork(); + if (pid < 0) { + close(errpipe[0]); + close(errpipe[1]); + return -1; + } + if (pid == 0) { + close(errpipe[0]); + if (dir && chdir(dir) != 0) note_exec_failed(errpipe[1]); + if (env_name && env_value && setenv(env_name, env_value, 1) != 0) note_exec_failed(errpipe[1]); + if (out_fd >= 0) { + if (dup2(out_fd, STDOUT_FILENO) < 0 || dup2(out_fd, STDERR_FILENO) < 0) + note_exec_failed(errpipe[1]); + if (out_fd > STDERR_FILENO) close(out_fd); + } + execvp(file, argv); + note_exec_failed(errpipe[1]); + } + close(errpipe[1]); + int saw_fail = 0; + int read_err = 0; + for (;;) { + unsigned char b = 0; + ssize_t got = read(errpipe[0], &b, 1); + if (got < 0 && errno == EINTR) continue; + if (got < 0) read_err = 1; + else if (got == 1) saw_fail = 1; + break; + } + close(errpipe[0]); + int status = 0; + for (;;) { + pid_t w = waitpid(pid, &status, 0); + if (w < 0 && errno == EINTR) continue; + if (w < 0) return -1; + break; + } + if (read_err) return -1; + if (saw_fail) return OMEGA_M18_CLEAN_CLONE_EXEC_FAILED; + if (WIFEXITED(status)) return WEXITSTATUS(status); + return -1; +} + +int omega_m18_git_toplevel(char *out, size_t cap) { + if (!out || cap < 2) return -1; + out[0] = '\0'; + int fds[2]; + if (pipe(fds) != 0) return -1; + pid_t pid = fork(); + if (pid < 0) { + close(fds[0]); + close(fds[1]); + return -1; + } + if (pid == 0) { + close(fds[0]); + if (dup2(fds[1], STDOUT_FILENO) < 0) _exit(126); + int devnull = open("/dev/null", O_WRONLY); + if (devnull >= 0) dup2(devnull, STDERR_FILENO); + if (fds[1] != STDOUT_FILENO) close(fds[1]); + char *argv[] = {"git", "rev-parse", "--show-toplevel", NULL}; + execvp("git", argv); + /* Setup or exec failed. The parent reports this as "no toplevel" + * (-1), not as 127, which is also what a missing recipe command + * makes make print. */ + _exit(126); + } + close(fds[1]); + size_t n = 0; + while (n + 1 < cap) { + ssize_t r = read(fds[0], out + n, cap - 1 - n); + if (r < 0) { + if (errno == EINTR) continue; + break; + } + if (r == 0) break; + n += (size_t)r; + } + close(fds[0]); + int status = 0; + for (;;) { + pid_t w = waitpid(pid, &status, 0); + if (w < 0 && errno == EINTR) continue; + if (w < 0) { + out[0] = '\0'; + return -1; + } + break; + } + while (n && (out[n - 1] == '\n' || out[n - 1] == '\r')) n--; + out[n] = '\0'; + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0 || n == 0) { + out[0] = '\0'; + return -1; + } + return 0; +} + +int omega_m18_clean_clone_copy_tree(const char *src, const char *dst) { + if (!path_fits(src) || !path_fits(dst)) return -1; + char *argv[] = {"cp", "-r", (char *)src, (char *)dst, NULL}; + int devnull = open("/dev/null", O_WRONLY | O_CLOEXEC); + if (devnull < 0) return -1; + int rc = run_direct("cp", argv, NULL, devnull, NULL, NULL); + close(devnull); + return rc; +} + +static int rm_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { + (void)sb; + (void)typeflag; + (void)ftwbuf; + if (remove(fpath) != 0) return -1; + return 0; +} + +static int rm_tree(const char *path) { + struct stat st; + if (lstat(path, &st) != 0) { + if (errno == ENOENT) return 0; + return -1; + } + if (nftw(path, rm_cb, 16, FTW_DEPTH | FTW_PHYS) != 0) return -1; + return 0; +} + +int omega_m18_clean_clone_run(const char *checkout_dir, const char *log_path, + const char *physics_dir) { + if (!path_fits(checkout_dir) || !path_fits(log_path) || !path_fits(physics_dir)) return -1; + char root[4096]; + if (omega_m18_git_toplevel(root, sizeof(root)) != 0) return -1; + char dest[4096]; + int n = snprintf(dest, sizeof(dest), "%s/checkout", checkout_dir); + if (n < 0 || (size_t)n >= sizeof(dest)) return -1; + if (rm_tree(dest) != 0) return -1; + int rc = omega_m18_clean_clone_copy_tree(root, dest); + if (rc != 0) return rc == OMEGA_M18_CLEAN_CLONE_EXEC_FAILED ? rc : -1; + int devnull = open("/dev/null", O_WRONLY | O_CLOEXEC); + if (devnull < 0) return -1; + char *clean_argv[] = {"make", "OUT_DIR=build", "clean", NULL}; + rc = run_direct("make", clean_argv, dest, devnull, NULL, NULL); + if (rc != 0) { + close(devnull); + return rc; + } + char *build_argv[] = {"make", "-j", "OUT_DIR=build", NULL}; + rc = run_direct("make", build_argv, dest, devnull, "PHYSICS_DIR", physics_dir); + close(devnull); + if (rc != 0) return rc; + int logfd = open_created_log(log_path); + if (logfd < 0) return -1; + char *tool_argv[] = { + (char *)OMEGA_M18_CLEAN_CLONE_TOOL, + (char *)OMEGA_M18_CLEAN_CLONE_TOOL_ARG, + NULL + }; + rc = run_direct(OMEGA_M18_CLEAN_CLONE_TOOL, tool_argv, dest, logfd, + OMEGA_M18_CLEAN_CLONE_NESTED_ENV, "1"); + close(logfd); + return rc; +} + +int omega_m18_clean_clone_plan(const char *out_dir, const char *tmpdir, + const char *repo_root, const char *temp_fallback, + const char *physics_dir, + char *checkout_dir, size_t checkout_cap, + char *log_path, size_t log_cap, + char *err, size_t err_cap) { + clear(checkout_dir, checkout_cap); + clear(log_path, log_cap); + clear(err, err_cap); + if (!checkout_dir || checkout_cap < 2 || !log_path || log_cap < 2 || !err || err_cap < 2) { + return OMEGA_M18_CLEAN_CLONE_PLAN_FAILED; + } + if (!path_fits(physics_dir)) { + return fail_plan(err, err_cap, "physics path is empty or does not fit"); + } + if (out_dir && out_dir[0] && !path_fits(out_dir)) { + return fail_plan(err, err_cap, "output directory path does not fit"); + } + if (tmpdir && tmpdir[0] && !path_fits(tmpdir)) { + return fail_plan(err, err_cap, "TMPDIR path does not fit"); + } + if (temp_fallback && temp_fallback[0] && !path_fits(temp_fallback)) { + return fail_plan(err, err_cap, "temp directory path does not fit"); + } + + int have_out = out_dir && out_dir[0]; + int have_tmp = tmpdir && tmpdir[0]; + int have_fb = temp_fallback && temp_fallback[0]; + int out_in = have_out && is_inside_repo(out_dir, repo_root); + int tmp_in = have_tmp && is_inside_repo(tmpdir, repo_root); + int fb_in = have_fb && is_inside_repo(temp_fallback, repo_root); + + char log_dir[4096]; + log_dir[0] = '\0'; + int shared = 0; + + if (have_out) { + int n = snprintf(log_dir, sizeof(log_dir), "%s/clean-clone", out_dir); + if (n < 0 || (size_t)n >= sizeof(log_dir) || set_log_name(log_path, log_cap, log_dir) != 0) { + return fail_plan(err, err_cap, "log path does not fit"); + } + if (mkdir_dir(log_dir) != 0) return fail_create(err, err_cap, log_path); + int cst = create_log_file(log_path, err, err_cap); + if (cst != OMEGA_M18_CLEAN_CLONE_OK) return cst; + if (!out_in) { + if (copy_path(checkout_dir, checkout_cap, log_dir) != 0) { + return fail_plan(err, err_cap, "checkout path does not fit"); + } + shared = 1; + } + } else if (have_tmp && !tmp_in) { + int st = make_private(tmpdir, checkout_dir, checkout_cap, err, err_cap); + if (st != OMEGA_M18_CLEAN_CLONE_OK) { + if (checkout_dir[0] && log_path[0] == '\0') { + set_log_name(log_path, log_cap, checkout_dir); + } + return st; + } + if (set_log_name(log_path, log_cap, checkout_dir) != 0) { + return fail_plan(err, err_cap, "log path does not fit"); + } + int cst = create_log_file(log_path, err, err_cap); + if (cst != OMEGA_M18_CLEAN_CLONE_OK) return cst; + shared = 1; + } else { + int n = snprintf(log_dir, sizeof(log_dir), "build/clean-clone"); + if (n < 0 || (size_t)n >= sizeof(log_dir) || set_log_name(log_path, log_cap, log_dir) != 0) { + return fail_plan(err, err_cap, "log path does not fit"); + } + if (mkdir_dir(log_dir) != 0) return fail_create(err, err_cap, log_path); + int cst = create_log_file(log_path, err, err_cap); + if (cst != OMEGA_M18_CLEAN_CLONE_OK) return cst; + if (!repo_root || !repo_root[0]) { + if (copy_path(checkout_dir, checkout_cap, log_dir) != 0) { + return fail_plan(err, err_cap, "checkout path does not fit"); + } + shared = 1; + } + } + + if (!shared) { + const char *parent = NULL; + if (have_tmp && !tmp_in) parent = tmpdir; + else if (have_fb && !fb_in) parent = temp_fallback; + if (!parent) { + errno = ENOENT; + return fail_checkout(err, err_cap, "private checkout directory"); + } + int st = make_private(parent, checkout_dir, checkout_cap, err, err_cap); + if (st != OMEGA_M18_CLEAN_CLONE_OK) return st; + } + + if (make_absolute(log_path, log_cap) != 0 || !path_fits(log_path)) { + return fail_plan(err, err_cap, "log path is not absolute or does not fit"); + } + return OMEGA_M18_CLEAN_CLONE_OK; +} + +void omega_m18_clean_clone_print_log_path(const char *log_path) { + if (!log_path || !log_path[0]) return; + fprintf(stderr, "M18 clean-clone log: %s\n", log_path); +} + +int omega_m18_clean_clone_remove_checkout(const char *checkout_dir, const char *log_path) { + if (!checkout_dir || !checkout_dir[0]) return 0; + char path[4096]; + int n = snprintf(path, sizeof(path), "%s/checkout", checkout_dir); + if (n < 0 || (size_t)n >= sizeof(path)) return -1; + if (rm_tree(path) != 0) return -1; + size_t cl = strlen(checkout_dir); + int log_here = log_path && strncmp(log_path, checkout_dir, cl) == 0 && + (log_path[cl] == '/' || log_path[cl] == '\0'); + if (!log_here) rmdir(checkout_dir); + return 0; +} diff --git a/src/omega_clean_clone.h b/src/omega_clean_clone.h new file mode 100644 index 000000000..9cbf9a186 --- /dev/null +++ b/src/omega_clean_clone.h @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OMEGA_CLEAN_CLONE_H +#define OMEGA_CLEAN_CLONE_H + +#include + +/* M18 gate 16 plans the clean-clone copy here so a host test can see the + * path without building the tree or touching a GPU. + * + * The log is created under the gate's own output directory + * (/clean-clone/clean_clone_m18.log) when out_dir is set, otherwise + * under tmpdir when that is set. It is never a fixed path on the host temp + * directory (omega#526). The copy itself is placed beside that log when that + * directory is outside the repo, and otherwise in a private directory under + * tmpdir or temp_fallback, so the copy of the repo does not land inside the + * repo. + * + * The copy, the build and the nested gate are started as programs. A path + * from OUT_DIR, TMPDIR or the physics directory is an argument or an + * environment value. It is not placed in a shell command (omega#935). + * + * Return values: + * OMEGA_M18_CLEAN_CLONE_OK + * OMEGA_M18_CLEAN_CLONE_LOG_CREATE_FAILED log file could not be created; + * err begins with "M18_CLEAN_CLONE_LOG_CREATE_FAILED:" and names the path + * OMEGA_M18_CLEAN_CLONE_CHECKOUT_DIR_FAILED the copy directory could not + * be created; the log path is still filled when it was chosen + * OMEGA_M18_CLEAN_CLONE_PLAN_FAILED a path did not fit, or the physics + * path is empty + */ +#define OMEGA_M18_CLEAN_CLONE_OK 0 +#define OMEGA_M18_CLEAN_CLONE_LOG_CREATE_FAILED 1 +#define OMEGA_M18_CLEAN_CLONE_PLAN_FAILED 2 +#define OMEGA_M18_CLEAN_CLONE_CHECKOUT_DIR_FAILED 3 +/* The child never reached exec (chdir, setenv, dup2, or execvp failed). + * This is not a waited-for exit status, so it is not make's 127. */ +#define OMEGA_M18_CLEAN_CLONE_EXEC_FAILED (-2) + +/* Nested tool, started with cwd set to the checkout. The name is fixed. */ +#define OMEGA_M18_CLEAN_CLONE_TOOL "./build/omegatool" +#define OMEGA_M18_CLEAN_CLONE_TOOL_ARG "--run-m18-gates" +#define OMEGA_M18_CLEAN_CLONE_NESTED_ENV "OMEGA_IN_CLEAN_CLONE" + +/* Working tree of this process, via git, with no shell. */ +int omega_m18_git_toplevel(char *out, size_t cap); + +int omega_m18_clean_clone_plan(const char *out_dir, const char *tmpdir, + const char *repo_root, const char *temp_fallback, + const char *physics_dir, + char *checkout_dir, size_t checkout_cap, + char *log_path, size_t log_cap, + char *err, size_t err_cap); + +/* cp -r src dst. src and dst are arguments. Returns 0 when cp exits 0. + * Returns OMEGA_M18_CLEAN_CLONE_EXEC_FAILED when cp cannot be started. */ +int omega_m18_clean_clone_copy_tree(const char *src, const char *dst); + +/* Run the clean clone: copy the git toplevel into /checkout, + * make OUT_DIR=build clean, make -j OUT_DIR=build with PHYSICS_DIR set in + * the environment, then the nested tool with its stdout and stderr in + * log_path. Returns 0 when every step exits 0. A step that ran and failed + * returns that process's exit status. A program that could not be started + * returns OMEGA_M18_CLEAN_CLONE_EXEC_FAILED, which is not 127. */ +int omega_m18_clean_clone_run(const char *checkout_dir, const char *log_path, + const char *physics_dir); + +/* stderr line the gate prints once it has chosen a log path. */ +void omega_m18_clean_clone_print_log_path(const char *log_path); + +/* Remove /checkout and, when the log does not live there, + * the private parent directory. The log file is left in place. */ +int omega_m18_clean_clone_remove_checkout(const char *checkout_dir, const char *log_path); + +#endif /* OMEGA_CLEAN_CLONE_H */ diff --git a/tests/.crumb b/tests/.crumb index 5fbfcb29c..7e6723a00 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -302,14 +302,14 @@ } ], "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T16:37:11Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:6d36f20967b8ca5e1e6f02dbd5332ae3a7e7da932dfc9743bef389470c31f604", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "files": 1388, + "generated_at_commit": "8afcb610da45720f84d05def9b88837f1922b3e5", + "source_tree": "sha256:018a20cd334988e77656d7f870782a5d90301b8340bccd9602557d5120721e82" }, "owner_lane": "", "provenance": { diff --git a/tests/m18_clean_clone_log_test.c b/tests/m18_clean_clone_log_test.c new file mode 100644 index 000000000..37ed279b4 --- /dev/null +++ b/tests/m18_clean_clone_log_test.c @@ -0,0 +1,275 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Host-only check of M18 clean-clone path selection (omega#526). No GPU. + */ +#include "omega_clean_clone.h" + +#include +#include +#include +#include +#include +#include +#include + +static int g_fails; + +static void check(int cond, const char *name) { + if (cond) { + printf("PASS %s\n", name); + } else { + printf("FAIL %s\n", name); + g_fails++; + } +} + +static int uses_fixed_tmp(const char *log) { + if (log && strstr(log, "/tmp/clean_clone_m18.log")) return 1; + if (log && strstr(log, "/tmp/omega_clean_m18")) return 1; + return 0; +} + +static int starts_with(const char *s, const char *prefix) { + size_t n = strlen(prefix); + return s && strncmp(s, prefix, n) == 0; +} + +static void write_file(const char *path) { + int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (fd >= 0) close(fd); +} + +static int plan(const char *out, const char *tmp, const char *repo, const char *fb, + char *checkout, char *log, char *err) { + memset(checkout, 0, 4096); + memset(log, 0, 4096); + memset(err, 0, 4608); + return omega_m18_clean_clone_plan(out, tmp, repo, fb, "/opt/physics", + checkout, 4096, log, 4096, err, 4608); +} + +static void expect_not_fixed(const char *log) { + check(!uses_fixed_tmp(log), "M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP"); +} + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return 2; + } + const char *work = argv[1]; + if (mkdir(work, 0755) != 0 && errno != EEXIST) { + perror(work); + return 2; + } + char tmpdir[512], outdir[512], repo[512], inside[512], blocked[512], defcwd[512], fb[512]; + snprintf(tmpdir, sizeof tmpdir, "%s/tmpdir", work); + snprintf(outdir, sizeof outdir, "%s/outdir", work); + snprintf(repo, sizeof repo, "%s/fakerepo", work); + snprintf(inside, sizeof inside, "%s/fakerepo/build/step", work); + snprintf(blocked, sizeof blocked, "%s/blocked", work); + snprintf(defcwd, sizeof defcwd, "%s/defcwd", work); + snprintf(fb, sizeof fb, "%s/fallback", work); + if (mkdir(tmpdir, 0755) != 0 || mkdir(outdir, 0755) != 0 || mkdir(repo, 0755) != 0 || + mkdir(defcwd, 0755) != 0 || mkdir(fb, 0755) != 0) { + perror("mkdir"); + return 2; + } + char inside_build[576]; + snprintf(inside_build, sizeof inside_build, "%s/build", repo); + if (mkdir(inside_build, 0755) != 0 || mkdir(inside, 0755) != 0) { + perror("mkdir inside"); + return 2; + } + + char checkout[4096], log[4096], err[4608]; + char cwd[512]; + if (!getcwd(cwd, sizeof cwd)) return 2; + + /* TMPDIR set, no OUT_DIR: log and copy live under TMPDIR. */ + int st = plan(NULL, tmpdir, NULL, NULL, checkout, log, err); + expect_not_fixed(log); + if (!uses_fixed_tmp(log)) { + check(st == OMEGA_M18_CLEAN_CLONE_OK, "M18_CLEAN_CLONE_TMPDIR_OK"); + check(starts_with(log, tmpdir) && strstr(log, "/clean-clone-") && + strstr(log, "/clean_clone_m18.log"), + "M18_CLEAN_CLONE_LOG_UNDER_TMPDIR"); + check(starts_with(checkout, tmpdir), "M18_CLEAN_CLONE_CHECKOUT_UNDER_TMPDIR"); + /* The nested tool name and the environment flag are fixed program + * arguments. The log is a file path, not a word in a shell command. */ + check(st == OMEGA_M18_CLEAN_CLONE_OK && log[0] == '/' && access(log, W_OK) == 0 && + strcmp(OMEGA_M18_CLEAN_CLONE_TOOL, "./build/omegatool") == 0 && + strcmp(OMEGA_M18_CLEAN_CLONE_TOOL_ARG, "--run-m18-gates") == 0 && + strcmp(OMEGA_M18_CLEAN_CLONE_NESTED_ENV, "OMEGA_IN_CLEAN_CLONE") == 0, + "M18_CLEAN_CLONE_COMMAND_USES_LOG"); + check(access(log, W_OK) == 0, "M18_CLEAN_CLONE_LOG_CREATED"); + char co[4200], nest[4200], marker[4200]; + int nco = snprintf(co, sizeof co, "%s/checkout", checkout); + int nn = snprintf(nest, sizeof nest, "%s/nested", co); + int nm = snprintf(marker, sizeof marker, "%s/keep-me", nest); + check(nco > 0 && (size_t)nco < sizeof co && nn > 0 && (size_t)nn < sizeof nest && + nm > 0 && (size_t)nm < sizeof marker, "M18_CLEAN_CLONE_PATH_FITS"); + check(mkdir(co, 0755) == 0 && mkdir(nest, 0755) == 0, "M18_CLEAN_CLONE_CHECKOUT_SETUP"); + write_file(marker); + check(omega_m18_clean_clone_remove_checkout(checkout, log) == 0, "M18_CLEAN_CLONE_REMOVE_OK"); + check(access(log, F_OK) == 0, "M18_CLEAN_CLONE_LOG_KEPT"); + check(access(co, F_OK) != 0, "M18_CLEAN_CLONE_CHECKOUT_REMOVED"); + + /* The gate prints the path it used. */ + char printed[512]; + snprintf(printed, sizeof printed, "%s/printed", work); + int fd = open(printed, O_CREAT | O_TRUNC | O_WRONLY, 0644); + int saved = dup(STDERR_FILENO); + dup2(fd, STDERR_FILENO); + omega_m18_clean_clone_print_log_path(log); + fflush(stderr); + dup2(saved, STDERR_FILENO); + close(fd); + close(saved); + FILE *pf = fopen(printed, "r"); + char line[4608]; + line[0] = '\0'; + if (pf) { + if (!fgets(line, sizeof line, pf)) line[0] = '\0'; + fclose(pf); + } + char expect[4608]; + snprintf(expect, sizeof expect, "M18 clean-clone log: %s\n", log); + check(strcmp(line, expect) == 0, "M18_CLEAN_CLONE_LOG_PATH_PRINTED"); + } + + /* OUT_DIR outside the repo wins over TMPDIR. Both the log and the copy sit there. */ + st = plan(outdir, tmpdir, NULL, NULL, checkout, log, err); + expect_not_fixed(log); + if (!uses_fixed_tmp(log)) { + char want[576]; + snprintf(want, sizeof want, "%s/clean-clone/clean_clone_m18.log", outdir); + char want_co[576]; + snprintf(want_co, sizeof want_co, "%s/clean-clone", outdir); + check(st == OMEGA_M18_CLEAN_CLONE_OK && strcmp(log, want) == 0 && access(log, W_OK) == 0, + "M18_CLEAN_CLONE_LOG_UNDER_OUT_DIR"); + check(strcmp(checkout, want_co) == 0, "M18_CLEAN_CLONE_OUT_DIR_COMMAND"); + } + + /* OUT_DIR inside the repo: the log still goes to that output/clean-clone + * directory, and the copy goes under TMPDIR so it is not copied into itself. */ + st = plan(inside, tmpdir, repo, NULL, checkout, log, err); + expect_not_fixed(log); + if (!uses_fixed_tmp(log)) { + char want[640]; + snprintf(want, sizeof want, "%s/clean-clone/clean_clone_m18.log", inside); + check(st == OMEGA_M18_CLEAN_CLONE_OK && strcmp(log, want) == 0, + "M18_CLEAN_CLONE_LOG_UNDER_OUT_DIR_INSIDE_REPO"); + check(starts_with(checkout, tmpdir) && strstr(checkout, "/clean-clone-") != NULL, + "M18_CLEAN_CLONE_INSIDE_REPO_CHECKOUT_UNDER_TMPDIR"); + check(log[0] == '/' && checkout[0] == '/' && strstr(log, checkout) == NULL, + "M18_CLEAN_CLONE_INSIDE_REPO_COMMAND"); + } + + /* Neither OUT_DIR nor TMPDIR: the log is under build/clean-clone, not a fixed host path. */ + if (chdir(defcwd) != 0) return 2; + st = plan(NULL, NULL, NULL, NULL, checkout, log, err); + expect_not_fixed(log); + if (!uses_fixed_tmp(log)) { + check(st == OMEGA_M18_CLEAN_CLONE_OK && log[0] == '/' && + strstr(log, "/build/clean-clone/clean_clone_m18.log") != NULL && + access(log, W_OK) == 0, + "M18_CLEAN_CLONE_LOG_UNDER_BUILD"); + } + if (chdir(cwd) != 0) return 2; + + /* The output path is not a directory, so the log cannot be created. + * A mode-only refusal is not enough here: the test may be running as + * root, which ignores directory mode. The error must name the log and + * must not look like a regression count. */ + { + int bfd = open(blocked, O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (bfd < 0) return 2; + close(bfd); + } + st = plan(blocked, NULL, NULL, NULL, checkout, log, err); + expect_not_fixed(log); + if (!uses_fixed_tmp(log)) { + int distinct = st == OMEGA_M18_CLEAN_CLONE_LOG_CREATE_FAILED && + strncmp(err, "M18_CLEAN_CLONE_LOG_CREATE_FAILED:", 34) == 0 && + strstr(err, "REGRESSION") == NULL && + strstr(err, log) != NULL; + check(distinct, "M18_CLEAN_CLONE_LOG_CREATE_FAILED_DISTINCT"); + } + + /* A directory name with a space, a quote and ';' is a path. cp receives + * it as one argument. A shell would have treated the quote as syntax and + * run the touch. */ + { + char hostile[768], src[768], hello[800], dest[800], copied[900]; + char marker_cwd[512], marker_work[768]; + snprintf(hostile, sizeof hostile, "%s/out x';touch m18_pwned_marker;", work); + snprintf(src, sizeof src, "%s/tiny-src", work); + snprintf(hello, sizeof hello, "%s/hello", src); + snprintf(dest, sizeof dest, "%s/copied", hostile); + int nc = snprintf(copied, sizeof copied, "%s/hello", dest); + check(nc > 0 && (size_t)nc < sizeof copied, "M18_CLEAN_CLONE_COPIED_FITS"); + if (!getcwd(marker_cwd, sizeof marker_cwd)) return 2; + size_t ml = strlen(marker_cwd); + if (ml + 20 >= sizeof marker_cwd) return 2; + memcpy(marker_cwd + ml, "/m18_pwned_marker", 18); + snprintf(marker_work, sizeof marker_work, "%s/m18_pwned_marker", work); + unlink(marker_cwd); + unlink(marker_work); + if (mkdir(hostile, 0755) != 0 || mkdir(src, 0755) != 0) return 2; + write_file(hello); + st = plan(hostile, NULL, NULL, NULL, checkout, log, err); + expect_not_fixed(log); + char want[800]; + snprintf(want, sizeof want, "%s/clean-clone/clean_clone_m18.log", hostile); + int copy_rc = omega_m18_clean_clone_copy_tree(src, dest); + int literal = st == OMEGA_M18_CLEAN_CLONE_OK && strcmp(log, want) == 0 && + access(log, W_OK) == 0 && copy_rc == 0 && access(copied, F_OK) == 0 && + access(marker_cwd, F_OK) != 0 && access(marker_work, F_OK) != 0; + char hostile_tmp[768]; + snprintf(hostile_tmp, sizeof hostile_tmp, "%s/tmp x';touch m18_pwned_marker;", work); + if (mkdir(hostile_tmp, 0755) != 0) return 2; + int st_tmp = plan(NULL, hostile_tmp, NULL, NULL, checkout, log, err); + literal = literal && st_tmp == OMEGA_M18_CLEAN_CLONE_OK && + starts_with(log, hostile_tmp) && strstr(log, "/clean_clone_m18.log") != NULL && + access(log, W_OK) == 0 && access(marker_cwd, F_OK) != 0; + check(literal, "M18_CLEAN_CLONE_HOSTILE_PATH_NOT_A_SHELL"); + } + + /* cp is not on PATH. The failure must be the exec sentinel, not 127, + * which is the status make prints when a recipe command is missing + * (GNU make itself then exits 2). */ + { + char tiny[768], gone[768]; + snprintf(tiny, sizeof tiny, "%s/tiny-src", work); + snprintf(gone, sizeof gone, "%s/exec-fail-dst", work); + const char *old_path = getenv("PATH"); + char *saved = old_path ? strdup(old_path) : NULL; + int set_ok = setenv("PATH", "/no-such-m18-path", 1) == 0; + int copy_rc = omega_m18_clean_clone_copy_tree(tiny, gone); + if (saved) { + setenv("PATH", saved, 1); + free(saved); + } else { + unsetenv("PATH"); + } + check(set_ok && copy_rc == OMEGA_M18_CLEAN_CLONE_EXEC_FAILED && copy_rc != 127, + "M18_CLEAN_CLONE_EXEC_FAILED_NOT_127"); + } + + if (getenv("M18_REQUIRE_TMP_UNWRITABLE")) { + int probe = open("/tmp/m18_clean_clone_ro_probe", O_CREAT | O_WRONLY | O_EXCL, 0644); + int tmp_blocked = (probe < 0); + if (probe >= 0) { + close(probe); + unlink("/tmp/m18_clean_clone_ro_probe"); + } + st = plan(NULL, tmpdir, NULL, NULL, checkout, log, err); + int ok = tmp_blocked && st == OMEGA_M18_CLEAN_CLONE_OK && !uses_fixed_tmp(log) && + starts_with(log, tmpdir) && access(log, W_OK) == 0; + check(ok, "M18_CLEAN_CLONE_LOG_TMP_UNWRITABLE_STILL_SELECTS"); + } + + return g_fails ? 1 : 0; +} diff --git a/tests/m18_clean_clone_log_test.sh b/tests/m18_clean_clone_log_test.sh new file mode 100755 index 000000000..a1b3de90c --- /dev/null +++ b/tests/m18_clean_clone_log_test.sh @@ -0,0 +1,131 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2026 Drake Stapleton (AIEN / aien-dev) +# +# Host-only (no GPU) check that M18 clean-clone log selection never uses the +# fixed host path, including when that directory is not writable and when +# TMPDIR is set. A mutant that puts the fixed path back fails +# M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP. +set -eu +root=$(cd "$(dirname "$0")/.." && pwd) +cc=${CC:-cc} +cflags="-std=c11 -Wall -Wextra -Werror -D_GNU_SOURCE -I$root/src" +src=$root/src/omega_clean_clone.c +testc=$root/tests/m18_clean_clone_log_test.c +mkdir -p "$root/build" +work=$(mktemp -d "$root/build/m18-clean-clone-log.XXXXXX") +trap 'chmod -R u+w "$work" 2>/dev/null || true; rm -rf "$work"' EXIT INT TERM + +fail=0 +say() { printf '%s\n' "$1"; } + +# The implementation sources must not carry the old fixed paths back, +# and must not hand a path to a shell. +if grep -n -e '/tmp/clean_clone_m18.log' -e '/tmp/omega_clean_m18' \ + "$root/src/omega_clean_clone.c" "$root/src/omega_clean_clone.h" \ + "$root/src/omega_blackwell_gates.c" +then + say "FAIL M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP" + fail=1 +else + say "PASS M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP source" +fi +if grep -n -e 'system(' -e 'popen(' -e '/bin/sh' \ + "$root/src/omega_clean_clone.c" "$root/src/omega_clean_clone.h" \ + "$root/src/omega_blackwell_gates.c" +then + say "FAIL M18_CLEAN_CLONE_NO_SHELL" + fail=1 +else + say "PASS M18_CLEAN_CLONE_NO_SHELL source" +fi + +"$cc" $cflags -o "$work/good" "$testc" "$src" +set +e +"$work/good" "$work/run1" >"$work/good.out" +good_rc=$? +set -e +if [ "$good_rc" -ne 0 ] || grep -q '^FAIL ' "$work/good.out"; then + say "good run printed a failure:" + cat "$work/good.out" + fail=1 +else + say "PASS good selection" +fi +for name in \ + M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP \ + M18_CLEAN_CLONE_LOG_UNDER_TMPDIR \ + M18_CLEAN_CLONE_LOG_UNDER_OUT_DIR \ + M18_CLEAN_CLONE_LOG_UNDER_OUT_DIR_INSIDE_REPO \ + M18_CLEAN_CLONE_LOG_CREATE_FAILED_DISTINCT \ + M18_CLEAN_CLONE_COMMAND_USES_LOG \ + M18_CLEAN_CLONE_HOSTILE_PATH_NOT_A_SHELL \ + M18_CLEAN_CLONE_EXEC_FAILED_NOT_127 \ + M18_CLEAN_CLONE_LOG_KEPT \ + M18_CLEAN_CLONE_LOG_PATH_PRINTED +do + if grep -q "^PASS $name$" "$work/good.out"; then + say "PASS saw $name" + else + say "FAIL missing $name" + fail=1 + fi +done + +# /tmp not writable, TMPDIR set to a directory on this workspace. +# This kernel denies unprivileged user namespaces +# (apparmor_restrict_unprivileged_userns = 1), so unshare cannot make the +# mount. bubblewrap can: it uses the setuid helper. The observable is the +# same: open("/tmp/...") returns EROFS. +ro_out=$work/ro.out +set +e +bwrap --ro-bind / / --dev /dev --tmpfs /tmp --ro-bind /tmp /tmp --bind "$work" "$work" \ + --setenv M18_REQUIRE_TMP_UNWRITABLE 1 \ + "$work/good" "$work/run-ro" >"$ro_out" 2>&1 +ro_rc=$? +set -e +if [ "$ro_rc" -eq 0 ] && grep -q '^PASS M18_CLEAN_CLONE_LOG_TMP_UNWRITABLE_STILL_SELECTS$' "$ro_out" \ + && ! grep -q '^FAIL ' "$ro_out" +then + say "PASS /tmp unwritable with TMPDIR set" +else + say "FAIL /tmp unwritable with TMPDIR set (rc $ro_rc)" + cat "$ro_out" || true + fail=1 +fi + +# Mutant: restore the fixed log path. It must fail the named assertion and +# must not be able to create that file (this run also keeps /tmp read-only). +awk ' +/snprintf\(log_path, log_cap, "%s\/clean_clone_m18.log", dir\)/ { + print " int n = snprintf(log_path, log_cap, \"%s\", \"/tmp/clean_clone_m18.log\"); (void)dir;" + replaced++ + next +} +{ print } +END { if (replaced != 1) exit 2 } +' "$src" >"$work/mutant.c" +"$cc" $cflags -o "$work/mutant" "$testc" "$work/mutant.c" +set +e +bwrap --ro-bind / / --dev /dev --tmpfs /tmp --ro-bind /tmp /tmp --bind "$work" "$work" \ + --setenv M18_REQUIRE_TMP_UNWRITABLE 1 \ + "$work/mutant" "$work/run-mut" >"$work/mut.out" 2>&1 +mut_rc=$? +set -e +if [ "$mut_rc" -ne 0 ] \ + && grep -q '^FAIL M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP$' "$work/mut.out" \ + && ! grep -q '^PASS M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP$' "$work/mut.out" +then + say "PASS mutant M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP" +else + say "FAIL mutant did not fail M18_CLEAN_CLONE_LOG_NOT_FIXED_TMP (rc $mut_rc)" + cat "$work/mut.out" || true + fail=1 +fi + +if [ "$fail" -eq 0 ]; then + say "m18_clean_clone_log_test: PASS" + exit 0 +fi +say "m18_clean_clone_log_test: FAIL" +exit 1 ```