From 1eb603dfdc58d74b7b2da3631158ef8532e16cd1 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 21:40:58 -0500 Subject: [PATCH 1/4] fix: require canonical artifact manifest admission --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b820f8f9..cba34015 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,12 @@ $(OUT_DIR)/cubin_probe: tools/interop/cubin_probe.c $(OUT_DIR)/libomega_gpu.a cubin-probe: $(OUT_DIR)/cubin_probe test-native-artifact: $(ARTIFACT_EXPORT) $(ARTIFACT_TEST) ./$(ARTIFACT_TEST) $(OUT_DIR)/artifact-test +# Pure artifact-format boundary: portable host test, no driver or device objects. +.PHONY: test-native-artifact-manifest +$(OUT_DIR)/native_artifact_manifest_test: tests/native_artifact_manifest_test.c src/omega_native_artifact.c src/omega_cubin.c src/omega_blackwell_realize.c src/sha256.c | $(OUT_DIR) + $(CC) $(CFLAGS) -ffunction-sections -fdata-sections -Wl,--gc-sections -o $@ $^ +test-native-artifact-manifest: $(OUT_DIR)/native_artifact_manifest_test + ./$(OUT_DIR)/native_artifact_manifest_test .PHONY: libomega_gpu test-gpu-matmul-api # The two gate-runner objects (omegatool's M17/M18/M19 and World gates) are NOT part of the # library: nothing in the library or in the Rust FFI crate calls them (checked with nm), and @@ -290,7 +296,7 @@ HOST_TESTS = test-gpu-matmul-api test-gpu-elementwise test-gpu-reconv test-gpu-a test-gpu-engine-mutants test-gpu-engine-backend-compile test-gpu-engine-prod-build test-gpu-engine-sim \ test-gpu-engine-backend-mutants test-gpu-engine-gb10-compile test-gpu-serving-alloc test-gpu-engine-freestanding \ test-codegen-freestanding test-gpu-session-platform test-gpu-wait test-gpu-wait-mutants host-tests-m16 \ - test-runtime-api test-runtime-portability test-doctor test-hello-sim + test-runtime-api test-runtime-portability test-doctor test-hello-sim test-native-artifact-manifest test: $(HOST_TESTS) @echo "CUDA++ HOST TESTS PASS ($(words $(HOST_TESTS)) targets)" From 95d54013d9a7d62871be68806348f325617bc3b1 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 21:41:02 -0500 Subject: [PATCH 2/4] fix: require canonical artifact manifest admission --- src/omega_native_artifact.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/omega_native_artifact.c b/src/omega_native_artifact.c index afc963f0..9362b000 100644 --- a/src/omega_native_artifact.c +++ b/src/omega_native_artifact.c @@ -354,6 +354,36 @@ static int cubin_text_matches(const uint8_t *cub, size_t n, const char *fn, return 0; } +/* Version 1 has one canonical ordered record per field. Validate the whole + * byte span before any string lookup: otherwise find_line can select policies + * after provenance_tool even though the identity hash stops at that boundary. + * Unknown/duplicate/reordered fields require a new format, not reinterpretation + * of an existing artifact id. provenance_tool is deliberately outside the id. */ +static int manifest_layout_ok(const uint8_t *buf, size_t n) { + static const char *const keys[] = { + "artifact_id=", "op=", "shape=", "bounds=", "contract=", + "contract_id=", "semantic_spec_id=", "isa=", "machine_profile=", + "kernel_format=", "kernel_function=", "text_sha256=", "text_size=", + "cubin_sha256=", "cubin_size=", "qmd0_sha256=", "qmd1_sha256=", + "qmd_template=", "cbank_offset=", "cbank_words=", "cbank_args=", + "cbank_args_sha256=", "grid=", "block=", "shared_bytes=", "gpr_count=", + "precision=", "accumulation=", "tolerance=", "min_driver=", "requires=", + "qualification=", "provenance_source_commit=", "provenance_tool=" + }; + const size_t header = sizeof(OMEGA_ARTIFACT_FORMAT "\n") - 1; + if (n < header || n >= MANIFEST_CAP || memchr(buf, 0, n) || + memchr(buf, '\r', n) || memcmp(buf, OMEGA_ARTIFACT_FORMAT "\n", header)) return 0; + size_t off = header; + for (size_t i = 0; i < sizeof keys / sizeof keys[0]; i++) { + size_t k = strlen(keys[i]); + if (k > n - off || memcmp(buf + off, keys[i], k)) return 0; + const uint8_t *nl = memchr(buf + off + k, '\n', n - off - k); + if (!nl) return 0; + off = (size_t)(nl - buf) + 1; + } + return off == n; +} + static char *find_line(char *buf, const char *key) { size_t k = strlen(key); char *p = buf; @@ -403,7 +433,7 @@ int omega_native_artifact_admit(const char *out_prefix, const char *current_driv snprintf(path, sizeof path, "%s.manifest", out_prefix); man = read_file(path, &mn); if (!man) { if (err) snprintf(err, err_cap, "cannot read %s", path); return OMEGA_ARTIFACT_E_IO; } - if (strncmp((char *)man, OMEGA_ARTIFACT_FORMAT "\n", strlen(OMEGA_ARTIFACT_FORMAT) + 1) != 0) { + if (!manifest_layout_ok(man, mn)) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } { From 16cefed932dac7d18adfa0a5ecee876b563634d5 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 21:41:06 -0500 Subject: [PATCH 3/4] test: reject noncanonical artifact identity boundaries --- tests/native_artifact_manifest_test.c | 115 ++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/native_artifact_manifest_test.c diff --git a/tests/native_artifact_manifest_test.c b/tests/native_artifact_manifest_test.c new file mode 100644 index 00000000..8caabef5 --- /dev/null +++ b/tests/native_artifact_manifest_test.c @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* Host-only canonical-manifest admission tests. The synthetic instruction bytes + * are never executed and make no device correctness claim. */ +#include +#include +#include +#include +#include "omega_native_artifact.h" +#include "sha256.h" + +static int checks, failures; +#define CHECK(c, msg) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL: %s\n", msg); } } while (0) +static void save(const char *path, const char *s, size_t n) { + FILE *f = fopen(path, "wb"); + if (!f || fwrite(s, 1, n, f) != n || fclose(f)) exit(2); +} +/* Test mutations must fail as fixture errors, not dereference a missing field. */ +static char *field(char *m, const char *key) { + char *p = strstr(m, key); + if (!p) { fprintf(stderr, "fixture missing %s\n", key); exit(2); } + return p; +} +static char *next_line(char *p) { + char *nl = strchr(p, '\n'); + if (!nl) { fprintf(stderr, "fixture missing newline\n"); exit(2); } + return nl + 1; +} +static void insert_record(char *m, size_t cap, const char *key, const char *record) { + char *p = field(m, key); + size_t n = strlen(m), extra = strlen(record); + if (n >= cap || extra >= cap - n) exit(2); + size_t off = (size_t)(p - m); + if (off > n) exit(2); + memmove(m + off + extra, m + off, n - off + 1); + memcpy(m + off, record, extra); +} +static void rehash(char *m) { + char *start = field(m, "\nop=") + 1, *end = field(start, "provenance_tool="); + char *id = field(m, "artifact_id=") + 12; + if (end <= start || (size_t)(next_line(id) - id) != 65) exit(2); + static const char hex[] = "0123456789abcdef"; + uint8_t digest[32]; + sha256_hash((uint8_t *)start, (size_t)(end - start), digest); + for (int i = 0; i < 32; i++) { id[2*i] = hex[digest[i] >> 4]; id[2*i+1] = hex[digest[i] & 15]; } +} +static void refuse(const char *prefix, const char *path, char *m, size_t n, const char *msg) { + char id[65], err[256]; + save(path, m, n); + int rc = omega_native_artifact_admit(prefix, "580.173.02", id, err, sizeof err); + CHECK(rc == OMEGA_ARTIFACT_E_FORMAT, msg); +} +int main(void) { + char dir[] = "/tmp/cudapp-manifest-XXXXXX", prefix[256], path[280], id[65], err[256]; + if (!mkdtemp(dir)) return 2; + snprintf(prefix, sizeof prefix, "%s/fixture", dir); + snprintf(path, sizeof path, "%s.manifest", prefix); + uint8_t text[16] = {0}; uint32_t q0[96] = {0}, q1[96] = {0}, cb[2] = {0}; + q1[52] = 0x300u >> 6; + OmegaCubinParam param = {0x380, 8, 1}; + char hash[65]; memset(hash, 'a', 64); hash[64] = 0; + OmegaNativeArtifact a = { + .op="matmul", .shape="m=1,k=1,n=1", .bounds="m<=1,k<=1,n<=1", .contract_name="test", + .contract_id_hex=hash, .semantic_spec_id_hex=hash, .kernel_function="test", + .precision="bf16", .accumulation="fp32", .tolerance="0", .text=text, .text_len=sizeof text, + .params=¶m, .nparams=1, .gpr_count=1, .grid_x=1, .grid_y=1, .block_x=1, .block_y=1, .block_z=1, + .cbank_offset=0x380, .cbank_args=cb, .cbank_words=2, .qmd0=q0, .qmd1=q1, + .min_driver="580.173.02", .requires="sm_121", .source_commit="UNKNOWN", .tool_version="test/1", + .qualification="device-load=NOT_RUN" + }; + CHECK(omega_native_artifact_export(&a,prefix,id,err,sizeof err)==0, "export fixture"); + CHECK(omega_native_artifact_admit(prefix,"580.173.02",id,err,sizeof err)==0, "admit canonical fixture"); + char original[8192], m[16384]; + FILE *f=fopen(path,"rb"); if (!f) return 2; + size_t n=fread(original,1,sizeof original-1,f); + if (ferror(f) || n == 0 || n == sizeof original-1) { fclose(f); return 2; } + fclose(f); original[n]=0; + + /* Move a checked policy after the identity terminator; the old parser + * accepts both policies under exactly the same artifact id. */ + char *line=field(original,"min_driver="), *nl=next_line(line); + size_t before=(size_t)(line-original), len=(size_t)(nl-line); + memcpy(m, original, before); strcpy(m+before, nl); + strncat(m, line, len); rehash(m); + refuse(prefix,path,m,strlen(m),"minimum driver outside identity accepted"); + char *policy=field(m,"min_driver=")+11; + if ((size_t)(next_line(policy) - policy) != 11) return 2; + memcpy(policy,"000.000.00",10); + refuse(prefix,path,m,strlen(m),"changed unhashed minimum driver accepted under same identity"); + + strcpy(m,original); strcat(m,"isa=sm_120\n"); + refuse(prefix,path,m,strlen(m),"duplicate key after terminator accepted"); + strcpy(m,original); insert_record(m,sizeof m,"shape=","shape=shadow\n"); rehash(m); + refuse(prefix,path,m,strlen(m),"duplicate key inside identity accepted"); + strcpy(m,original); line=field(m,"qualification="); nl=next_line(line); memmove(line,nl,strlen(nl)+1); rehash(m); + refuse(prefix,path,m,strlen(m),"missing qualification accepted"); + strcpy(m,original); line=field(m,"shape="); nl=next_line(line); + char *next=next_line(nl); char swap[256]; size_t l1=(size_t)(nl-line),l2=(size_t)(next-nl); + if (l1 > sizeof swap) return 2; + memcpy(swap,line,l1); memmove(line,nl,l2); memcpy(line+l2,swap,l1); rehash(m); + refuse(prefix,path,m,strlen(m),"reordered canonical fields accepted"); + strcpy(m,original); m[n]='\0'; memcpy(m+n+1,"isa=sm_120\n",11); + refuse(prefix,path,m,n+12,"embedded NUL with trailing policy accepted"); + strcpy(m,original); line=field(m,"provenance_tool="); *line=0; + refuse(prefix,path,m,strlen(m),"missing identity boundary accepted"); + strcpy(m,original); m[n-1]=0; + refuse(prefix,path,m,n-1,"missing final newline accepted"); + + save(path,original,n); + CHECK(omega_native_artifact_admit(prefix,"580.173.02",id,err,sizeof err)==0,"canonical fixture still accepted"); + const char *suffix[]={".text.bin",".cubin",".qmd0.bin",".qmd1.bin",".cbank_args.bin",".manifest",".json",".sha256"}; + for (size_t i=0;i Date: Sat, 24 Oct 2026 21:47:52 -0500 Subject: [PATCH 4/4] test: create private artifact fixtures under permissive umask --- tests/native_artifact_manifest_test.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/native_artifact_manifest_test.c b/tests/native_artifact_manifest_test.c index 8caabef5..7e30b258 100644 --- a/tests/native_artifact_manifest_test.c +++ b/tests/native_artifact_manifest_test.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ /* Host-only canonical-manifest admission tests. The synthetic instruction bytes * are never executed and make no device correctness claim. */ +#include +#include #include #include #include @@ -11,8 +13,11 @@ static int checks, failures; #define CHECK(c, msg) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL: %s\n", msg); } } while (0) static void save(const char *path, const char *s, size_t n) { - FILE *f = fopen(path, "wb"); - if (!f || fwrite(s, 1, n, f) != n || fclose(f)) exit(2); + int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (fd < 0) exit(2); + FILE *f = fdopen(fd, "wb"); + if (!f) { close(fd); exit(2); } + if (fwrite(s, 1, n, f) != n || fclose(f)) exit(2); } /* Test mutations must fail as fixture errors, not dereference a missing field. */ static char *field(char *m, const char *key) { @@ -52,6 +57,14 @@ static void refuse(const char *prefix, const char *path, char *m, size_t n, cons int main(void) { char dir[] = "/tmp/cudapp-manifest-XXXXXX", prefix[256], path[280], id[65], err[256]; if (!mkdtemp(dir)) return 2; + /* Even a permissive invoking umask must not create shared test files. */ + snprintf(path, sizeof path, "%s/mode-check", dir); + mode_t mask = umask(0); + save(path, "test", 4); + umask(mask); + struct stat st; + CHECK(stat(path, &st) == 0 && (st.st_mode & 0777) == 0600, "fixture file mode is not private"); + unlink(path); snprintf(prefix, sizeof prefix, "%s/fixture", dir); snprintf(path, sizeof path, "%s.manifest", prefix); uint8_t text[16] = {0}; uint32_t q0[96] = {0}, q1[96] = {0}, cb[2] = {0};