interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / native_artifact_manifest_test.c

331 lines · 18352 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Host-only canonical-manifest admission tests. The synthetic instruction bytes
3 * are never executed and make no device correctness claim. */
4#include <fcntl.h>
5#include <sys/stat.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10#include "omega_native_artifact.h"
11#include "omega_native_artifact_snapshot.h"
12#include "sha256.h"
13
14static int checks, failures;
15#define CHECK(c, msg) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL: %s\n", msg); } } while (0)
16/* Linker observation only: all semantic admission remains the real runtime. */
17static char snapshot_paths[OMEGA_SNAPSHOT_FILES][280];
18static unsigned snapshot_reads[OMEGA_SNAPSHOT_FILES];
19static int snapshot_watch;
20FILE *__real_fopen(const char *, const char *);
21FILE *__wrap_fopen(const char *path, const char *mode) {
22 if (snapshot_watch)
23 for (unsigned i = 0; i < OMEGA_SNAPSHOT_FILES; ++i)
24 if (!strcmp(path, snapshot_paths[i])) ++snapshot_reads[i];
25 return __real_fopen(path, mode);
26}
27static void save(const char *path, const char *s, size_t n) {
28 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
29 if (fd < 0) exit(2);
30 FILE *f = fdopen(fd, "wb");
31 if (!f) { close(fd); exit(2); }
32 if (fwrite(s, 1, n, f) != n || fclose(f)) exit(2);
33}
34/* Test mutations must fail as fixture errors, not dereference a missing field. */
35static char *field(char *m, const char *key) {
36 char *p = strstr(m, key);
37 if (!p) { fprintf(stderr, "fixture missing %s\n", key); exit(2); }
38 return p;
39}
40static char *next_line(char *p) {
41 char *nl = strchr(p, '\n');
42 if (!nl) { fprintf(stderr, "fixture missing newline\n"); exit(2); }
43 return nl + 1;
44}
45static void insert_record(char *m, size_t cap, const char *key, const char *record) {
46 char *p = field(m, key);
47 size_t n = strlen(m), extra = strlen(record);
48 if (n >= cap || extra >= cap - n) exit(2);
49 size_t off = (size_t)(p - m);
50 if (off > n) exit(2);
51 memmove(m + off + extra, m + off, n - off + 1);
52 memcpy(m + off, record, extra);
53}
54static void rehash(char *m) {
55 char *start = field(m, "\nop=") + 1, *end = field(start, "provenance_tool=");
56 char *id = field(m, "artifact_id=") + 12;
57 if (end <= start || (size_t)(next_line(id) - id) != 65) exit(2);
58 static const char hex[] = "0123456789abcdef";
59 uint8_t digest[32];
60 sha256_hash((uint8_t *)start, (size_t)(end - start), digest);
61 for (int i = 0; i < 32; i++) { id[2*i] = hex[digest[i] >> 4]; id[2*i+1] = hex[digest[i] & 15]; }
62}
63static void refuse(const char *prefix, const char *path, char *m, size_t n, const char *msg) {
64 char id[65], err[256];
65 save(path, m, n);
66 int rc = omega_native_artifact_admit(prefix, "580.173.02", id, err, sizeof err);
67 CHECK(rc == OMEGA_ARTIFACT_E_FORMAT, msg);
68}
69static const char *const suffixes[] = {".text.bin",".cubin",".qmd0.bin",".qmd1.bin",".cbank_args.bin",".manifest",".json",".sha256"};
70static void remove_artifact(const char *prefix) {
71 char p[1100];
72 for (size_t i = 0; i < sizeof suffixes / sizeof suffixes[0]; i++) {
73 snprintf(p, sizeof p, "%s%s", prefix, suffixes[i]);
74 unlink(p);
75 }
76}
77/* The QMD 1 launch words omega_blackwell_build_qmd1 writes for this launch. */
78static void set_launch(uint32_t *q1, uint32_t gx, uint32_t gy, uint32_t bx, uint32_t by, uint32_t bz,
79 uint32_t shared, uint32_t gpr) {
80 q1[34] = bx | (by << 16);
81 q1[35] = bz | ((gpr < 16 ? 16 : gpr) << 8) | (1u << 17);
82 q1[36] = 0x04b44808u & ~0x7ffu;
83 q1[36] |= shared ? (shared + 127) / 128 : 8;
84 q1[39] = gx; q1[40] = gy; q1[41] = 1;
85}
86static int file_text(const char *path, char *buf, size_t cap) {
87 FILE *f = fopen(path, "rb");
88 if (!f) return -1;
89 size_t n = fread(buf, 1, cap - 1, f);
90 fclose(f);
91 buf[n] = 0;
92 return (int)n;
93}
94/* Replace the value of key in manifest m (one line), then re-bind the id. */
95static void set_field(char *m, const char *key, const char *val) {
96 char *v = field(m, key) + strlen(key), *nl = next_line(v) - 1;
97 size_t vl = strlen(val);
98 memmove(v + vl, nl, strlen(nl) + 1);
99 memcpy(v, val, vl);
100 rehash(m);
101}
102static void hex_digest(const void *b, size_t n, char out[65]) {
103 static const char hex[] = "0123456789abcdef";
104 uint8_t d[32];
105 sha256_hash((const uint8_t *)b, n, d);
106 for (int i = 0; i < 32; i++) { out[2*i] = hex[d[i] >> 4]; out[2*i+1] = hex[d[i] & 15]; }
107 out[64] = 0;
108}
109static int admit_rc(const char *prefix, const char *driver, char id[65]) {
110 char err[256];
111 int rc = omega_native_artifact_admit(prefix, driver, id, err, sizeof err);
112 if (rc) fprintf(stderr, " (refused as expected? rc=%d %s)\n", rc, err);
113 return rc;
114}
115int main(void) {
116 char dir[] = "/tmp/cudapp-manifest-XXXXXX", prefix[256], path[280], id[65], err[256];
117 if (!mkdtemp(dir)) return 2;
118 /* Even a permissive invoking umask must not create shared test files. */
119 snprintf(path, sizeof path, "%s/mode-check", dir);
120 mode_t mask = umask(0);
121 save(path, "test", 4);
122 umask(mask);
123 struct stat st;
124 CHECK(stat(path, &st) == 0 && (st.st_mode & 0777) == 0600, "fixture file mode is not private");
125 unlink(path);
126 snprintf(prefix, sizeof prefix, "%s/fixture", dir);
127 snprintf(path, sizeof path, "%s.manifest", prefix);
128 uint8_t text[16] = {0}; uint32_t q0[96] = {0}, q1[96] = {0}, cb[2] = {0};
129 q1[52] = 0x300u >> 6;
130 set_launch(q1, 1, 1, 1, 1, 1, 0, 1);
131 OmegaCubinParam param = {0x380, 8, 1};
132 char hash[65]; memset(hash, 'a', 64); hash[64] = 0;
133 OmegaNativeArtifact a = {
134 .op="matmul", .shape="m=1,k=1,n=1", .bounds="m<=1,k<=1,n<=1", .contract_name="test",
135 .contract_id_hex=hash, .semantic_spec_id_hex=hash, .kernel_function="test",
136 .precision="bf16", .accumulation="fp32", .tolerance="0", .text=text, .text_len=sizeof text,
137 .params=&param, .nparams=1, .gpr_count=1, .grid_x=1, .grid_y=1, .block_x=1, .block_y=1, .block_z=1,
138 .cbank_offset=0x380, .cbank_args=cb, .cbank_words=2, .qmd0=q0, .qmd1=q1,
139 .min_driver="580.173.02", .requires="sm_121", .source_commit="UNKNOWN", .tool_version="test/1",
140 .qualification="device-load=NOT_RUN"
141 };
142 CHECK(omega_native_artifact_export(&a,prefix,id,err,sizeof err)==0, "export fixture");
143 CHECK(omega_native_artifact_admit(prefix,"580.173.02",id,err,sizeof err)==0, "admit canonical fixture");
144 {
145 static const char *const files[] = {
146 ".manifest", ".text.bin", ".cubin", ".qmd0.bin", ".qmd1.bin", ".cbank_args.bin"
147 };
148 for (unsigned i = 0; i < OMEGA_SNAPSHOT_FILES; ++i)
149 snprintf(snapshot_paths[i], sizeof snapshot_paths[i], "%s%s", prefix, files[i]);
150 OmegaNativeArtifactSnapshot snapshot = {0}, refused = {0};
151 snapshot_watch = 1;
152 int rc = omega_native_artifact_snapshot(prefix, "580.173.02", id, &snapshot, err, sizeof err);
153 snapshot_watch = 0;
154 CHECK(rc == 0, "snapshot uses the existing canonical admission");
155 for (unsigned i = 0; i < OMEGA_SNAPSHOT_FILES; ++i)
156 CHECK(snapshot_reads[i] == 1, "checked artifact file must be read exactly once");
157 CHECK(snapshot.sizes[OMEGA_SNAPSHOT_TEXT] == sizeof text &&
158 !memcmp(snapshot.bytes[OMEGA_SNAPSHOT_TEXT], text, sizeof text),
159 "snapshot retains exactly the checked code bytes");
160 uint8_t changed[sizeof text]; memcpy(changed, text, sizeof changed); changed[0] ^= 1;
161 save(snapshot_paths[OMEGA_SNAPSHOT_TEXT], (const char *)changed, sizeof changed);
162 CHECK(snapshot.sizes[OMEGA_SNAPSHOT_TEXT] == sizeof text &&
163 !memcmp(snapshot.bytes[OMEGA_SNAPSHOT_TEXT], text, sizeof text),
164 "path mutation cannot replace retained checked bytes");
165 CHECK(omega_native_artifact_snapshot(prefix, "580.173.02", id, &refused, err, sizeof err) ==
166 OMEGA_ARTIFACT_E_DIGEST, "new snapshot refuses changed code under the old manifest");
167 for (unsigned i = 0; i < OMEGA_SNAPSHOT_FILES; ++i)
168 CHECK(refused.bytes[i] == NULL && refused.sizes[i] == 0,
169 "refused admission transfers no byte custody");
170 omega_native_artifact_snapshot_free(&snapshot);
171 for (unsigned i = 0; i < OMEGA_SNAPSHOT_FILES; ++i)
172 CHECK(snapshot.bytes[i] == NULL && snapshot.sizes[i] == 0,
173 "snapshot release clears host custody");
174 save(snapshot_paths[OMEGA_SNAPSHOT_TEXT], (const char *)text, sizeof text);
175 }
176 char original[8192], m[16384];
177 FILE *f=fopen(path,"rb"); if (!f) return 2;
178 size_t n=fread(original,1,sizeof original-1,f);
179 if (ferror(f) || n == 0 || n == sizeof original-1) { fclose(f); return 2; }
180 fclose(f); original[n]=0;
181
182 /* Move a checked policy after the identity terminator; the old parser
183 * accepts both policies under exactly the same artifact id. */
184 char *line=field(original,"min_driver="), *nl=next_line(line);
185 size_t before=(size_t)(line-original), len=(size_t)(nl-line);
186 memcpy(m, original, before); strcpy(m+before, nl);
187 strncat(m, line, len); rehash(m);
188 refuse(prefix,path,m,strlen(m),"minimum driver outside identity accepted");
189 char *policy=field(m,"min_driver=")+11;
190 if ((size_t)(next_line(policy) - policy) != 11) return 2;
191 memcpy(policy,"000.000.00",10);
192 refuse(prefix,path,m,strlen(m),"changed unhashed minimum driver accepted under same identity");
193
194 strcpy(m,original); strcat(m,"isa=sm_120\n");
195 refuse(prefix,path,m,strlen(m),"duplicate key after terminator accepted");
196 strcpy(m,original); insert_record(m,sizeof m,"shape=","shape=shadow\n"); rehash(m);
197 refuse(prefix,path,m,strlen(m),"duplicate key inside identity accepted");
198 strcpy(m,original); line=field(m,"qualification="); nl=next_line(line); memmove(line,nl,strlen(nl)+1); rehash(m);
199 refuse(prefix,path,m,strlen(m),"missing qualification accepted");
200 strcpy(m,original); line=field(m,"shape="); nl=next_line(line);
201 char *next=next_line(nl); char swap[256]; size_t l1=(size_t)(nl-line),l2=(size_t)(next-nl);
202 if (l1 > sizeof swap) return 2;
203 memcpy(swap,line,l1); memmove(line,nl,l2); memcpy(line+l2,swap,l1); rehash(m);
204 refuse(prefix,path,m,strlen(m),"reordered canonical fields accepted");
205 strcpy(m,original); m[n]='\0'; memcpy(m+n+1,"isa=sm_120\n",11);
206 refuse(prefix,path,m,n+12,"embedded NUL with trailing policy accepted");
207 strcpy(m,original); line=field(m,"provenance_tool="); *line=0;
208 refuse(prefix,path,m,strlen(m),"missing identity boundary accepted");
209 strcpy(m,original); m[n-1]=0;
210 refuse(prefix,path,m,n-1,"missing final newline accepted");
211
212 save(path,original,n);
213 CHECK(omega_native_artifact_admit(prefix,"580.173.02",id,err,sizeof err)==0,"canonical fixture still accepted");
214 remove_artifact(prefix);
215
216 /* ---- audit CudaAuditFable F5/F3/F7/F8 (2026-10-26): admission and export fail closed ---- */
217 char man[16384], val[128], pfx[256];
218 snprintf(pfx, sizeof pfx, "%s/adm", dir);
219 CHECK(omega_native_artifact_export(&a,pfx,id,err,sizeof err)==0, "export admission fixture");
220 snprintf(path, sizeof path, "%s.manifest", pfx);
221 if (file_text(path, original, sizeof original) <= 0) return 2;
222 /* F5: a minimum driver that does not parse was read as 0.0.0 and admitted. */
223 const char *bad_versions[] = {"garbage", "x999.0.0", "", "9999999999.0.0", "580.173", "580.173.02x", "-1.0.0"};
224 for (size_t i = 0; i < sizeof bad_versions / sizeof bad_versions[0]; i++) {
225 strcpy(m, original); set_field(m, "min_driver=", bad_versions[i]); save(path, m, strlen(m));
226 snprintf(val, sizeof val, "unparsable manifest min_driver '%s' admitted", bad_versions[i]);
227 CHECK(admit_rc(pfx, "580.173.02", id) == OMEGA_ARTIFACT_E_FORMAT && id[0] == 0, val);
228 }
229 save(path, original, strlen(original));
230 CHECK(admit_rc(pfx, "garbage", id) == OMEGA_ARTIFACT_E_ARG, "unparsable current driver admitted");
231 CHECK(admit_rc(pfx, "580.173.01", id) == OMEGA_ARTIFACT_E_DRIVER && id[0] == 0, "older driver admitted or id reported");
232 CHECK(admit_rc(pfx, "580.173.02", id) == 0 && id[0] != 0, "admission fixture refused after driver checks");
233 { /* out_id may be NULL (the entry check allows it): a passing admit must not write through it. */
234 char nerr[256];
235 CHECK(omega_native_artifact_admit(pfx, "580.173.02", NULL, nerr, sizeof nerr) == 0,
236 "admit with a NULL out_id refused a valid artifact");
237 }
238 /* F5b: cbank_words with a trailing suffix was read by strtoul and admitted. */
239 strcpy(m, original); set_field(m, "cbank_words=", "2abc"); save(path, m, strlen(m));
240 CHECK(admit_rc(pfx, "580.173.02", id) == OMEGA_ARTIFACT_E_FORMAT, "cbank_words '2abc' admitted");
241 strcpy(m, original); set_field(m, "cbank_words=", "+2"); save(path, m, strlen(m));
242 CHECK(admit_rc(pfx, "580.173.02", id) == OMEGA_ARTIFACT_E_FORMAT, "cbank_words '+2' admitted");
243 /* F3 (cbank): 33 words exceed the 32 the launch path accepts, even with a matching file and digest. */
244 {
245 uint32_t big[33] = {0};
246 char cbp[1100], dg[65];
247 snprintf(cbp, sizeof cbp, "%s.cbank_args.bin", pfx);
248 save(cbp, (const char *)big, sizeof big);
249 hex_digest(big, sizeof big, dg);
250 strcpy(m, original); set_field(m, "cbank_words=", "33"); set_field(m, "cbank_args_sha256=", dg);
251 save(path, m, strlen(m));
252 CHECK(admit_rc(pfx, "580.173.02", id) == OMEGA_ARTIFACT_E_FORMAT, "33-word constant bank admitted");
253 save(cbp, (const char *)cb, sizeof cb);
254 }
255 /* F3 (QMD): every digest matches but QMD 1 launches 2^31-1 CTAs with 511 registers. */
256 {
257 uint32_t hostile[96];
258 char qp[1100], dg[65];
259 memcpy(hostile, q1, sizeof hostile);
260 hostile[39] = 0x7fffffffu;
261 hostile[35] = (hostile[35] & ~(0x1ffu << 8)) | (511u << 8);
262 snprintf(qp, sizeof qp, "%s.qmd1.bin", pfx);
263 save(qp, (const char *)hostile, sizeof hostile);
264 hex_digest(hostile, sizeof hostile, dg);
265 strcpy(m, original); set_field(m, "qmd1_sha256=", dg); save(path, m, strlen(m));
266 CHECK(admit_rc(pfx, "580.173.02", id) == OMEGA_ARTIFACT_E_CONTRACT && id[0] == 0,
267 "QMD launching 0x7fffffff CTAs under a 1x1 manifest admitted");
268 save(qp, (const char *)q1, sizeof q1);
269 }
270 save(path, original, strlen(original));
271 CHECK(admit_rc(pfx, "580.173.02", id) == 0, "admission fixture refused after restore");
272
273 /* F8: an existing artifact is never overwritten, and a planted symlink is not followed. */
274 CHECK(omega_native_artifact_export(&a,pfx,id,err,sizeof err)==OMEGA_ARTIFACT_E_IO, "export overwrote an existing artifact");
275 remove_artifact(pfx);
276 {
277 char victim[1100], link[1100], vbuf[64];
278 snprintf(victim, sizeof victim, "%s/victim", dir);
279 snprintf(link, sizeof link, "%s.text.bin", pfx);
280 save(victim, "keep", 4);
281 CHECK(symlink(victim, link) == 0, "fixture symlink");
282 CHECK(omega_native_artifact_export(&a,pfx,id,err,sizeof err)==OMEGA_ARTIFACT_E_IO, "export followed a planted symlink");
283 CHECK(file_text(victim, vbuf, sizeof vbuf) == 4 && strcmp(vbuf, "keep") == 0, "symlink target was written");
284 unlink(link); unlink(victim); remove_artifact(pfx);
285 }
286 /* F8: a 1023-byte prefix truncated every suffix away; all eight writes hit the bare prefix. */
287 {
288 char longp[1024];
289 size_t at = (size_t)snprintf(longp, sizeof longp, "%s/", dir);
290 while (at < 1021) { longp[at++] = '.'; longp[at++] = '/'; }
291 while (at < 1023) longp[at++] = 'x';
292 longp[at] = 0;
293 CHECK(strlen(longp) == 1023, "fixture long prefix length");
294 CHECK(omega_native_artifact_export(&a,longp,id,err,sizeof err)==OMEGA_ARTIFACT_E_ARG, "1023-byte prefix accepted");
295 CHECK(access(longp, F_OK) != 0, "a file was written at the truncated prefix");
296 unlink(longp);
297 /* Positive control: the longest prefix whose every path fits is accepted. */
298 size_t fit = 1023 - strlen(".cbank_args.bin");
299 longp[fit] = 0;
300 if (longp[fit - 1] == '/') longp[fit - 1] = 'x';
301 CHECK(omega_native_artifact_export(&a,longp,id,err,sizeof err)==0, "longest fitting prefix refused");
302 CHECK(admit_rc(longp, "580.173.02", id) == 0, "longest fitting prefix not admitted");
303 remove_artifact(longp);
304 }
305 /* F5/F7: export refuses what admission would refuse, and escapes JSON. */
306 {
307 OmegaNativeArtifact b = a;
308 b.min_driver = "garbage";
309 CHECK(omega_native_artifact_export(&b,pfx,id,err,sizeof err)==OMEGA_ARTIFACT_E_ARG, "export accepted min_driver 'garbage'");
310 b = a; b.tolerance = "0.1\nisa=sm_120";
311 CHECK(omega_native_artifact_export(&b,pfx,id,err,sizeof err)==OMEGA_ARTIFACT_E_ARG, "export accepted a newline in a field");
312 b = a; uint32_t cb33[33] = {0}; b.cbank_args = cb33; b.cbank_words = 33;
313 CHECK(omega_native_artifact_export(&b,pfx,id,err,sizeof err)!=0, "export accepted 33 constant-bank words");
314 b = a; uint32_t hq[96]; memcpy(hq, q1, sizeof hq); hq[39] = 0x7fffffffu; b.qmd1 = hq;
315 CHECK(omega_native_artifact_export(&b,pfx,id,err,sizeof err)==OMEGA_ARTIFACT_E_CONTRACT, "export accepted a QMD that disagrees with grid");
316 b = a; b.shared_bytes = 640;
317 CHECK(omega_native_artifact_export(&b,pfx,id,err,sizeof err)==OMEGA_ARTIFACT_E_CONTRACT, "export accepted a QMD that disagrees with shared_bytes");
318 CHECK(access(pfx, F_OK) != 0, "refused export left a file");
319 snprintf(path, sizeof path, "%s.text.bin", pfx);
320 CHECK(access(path, F_OK) != 0, "refused export left text.bin");
321 b = a; b.tolerance = "a\"b\\c";
322 CHECK(omega_native_artifact_export(&b,pfx,id,err,sizeof err)==0, "export refused a quote in tolerance");
323 snprintf(path, sizeof path, "%s.json", pfx);
324 CHECK(file_text(path, man, sizeof man) > 0 && strstr(man, "\"tolerance\": \"a\\\"b\\\\c\"") != NULL,
325 "JSON string not escaped");
326 remove_artifact(pfx);
327 }
328 rmdir(dir);
329 printf("native_artifact_manifest: %d checks, %d failed\n",checks,failures);
330 return failures ? 1 : 0;
331}