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/CUDApp/blob/main/src/omega_native_artifact.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_native_artifact.c` 846 lines · 38157 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_native_artifact: offline digest-bound handoff for one GPU realization. 3 * See the header. No device, no launch, no live address. */ 4 #include "omega_native_artifact.h" 5 #include "omega_native_artifact_snapshot.h" 6 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 14 #include "sha256.h" 15 #include "omega_blackwell_realize.h" 16 #include "cudapp_native.h" 17 18 #ifndef CUDAPP_MIGRATION_PENDING_C_ARTIFACT 19 #define CUDAPP_MIGRATION_PENDING_C_ARTIFACT 0 20 #endif 21 #ifndef CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 22 #define CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 0 23 #endif 24 25 #define MANIFEST_CAP 8192u 26 #if CUDAPP_MIGRATION_PENDING_C_ARTIFACT 27 28 const char *omega_native_artifact_rc_name(int rc) { 29 switch (rc) { 30 case OMEGA_ARTIFACT_OK: return "OK"; 31 case OMEGA_ARTIFACT_E_ARG: return "ARG"; 32 case OMEGA_ARTIFACT_E_FORMAT: return "FORMAT"; 33 case OMEGA_ARTIFACT_E_CONTRACT: return "CONTRACT"; 34 case OMEGA_ARTIFACT_E_ISA: return "ISA"; 35 case OMEGA_ARTIFACT_E_DIGEST: return "DIGEST"; 36 case OMEGA_ARTIFACT_E_IO: return "IO"; 37 case OMEGA_ARTIFACT_E_RELOC: return "RELOC"; 38 case OMEGA_ARTIFACT_E_DRIVER: return "DRIVER"; 39 case OMEGA_ARTIFACT_E_UNSUPPORTED: return "UNSUPPORTED"; 40 case OMEGA_ARTIFACT_E_MEM: return "MEM"; 41 default: return "UNKNOWN"; 42 } 43 } 44 45 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 46 /* Memory shell: only byte loads, pointer stepping and emitted-component stores. 47 * All byte classification, accumulation and termination decisions are Omega. */ 48 static int artifact_text(unsigned mode, const char *text, uint32_t *out, unsigned count) { 49 if (!text) return 0; 50 uint64_t state[8] = {0}, status; 51 state[3] = count; 52 for (const unsigned char *p = (const unsigned char *)text;; ++p) { 53 if (omega_artifact_parse(&status, mode, *p, state, 8) != OMEGA_OK || status == 2) return 0; 54 if (mode >= 2 && (status == 1 || status == 3)) 55 out[state[2] - (status == 3)] = (uint32_t)state[4]; 56 if (status == 1) return 1; 57 } 58 } 59 #endif 60 61 static void hex64(const uint8_t d[32], char out[65]) { 62 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 63 uint64_t state[8] = {0}, pair; 64 for (int i = 0; i < 32; ++i) { 65 if (omega_artifact_parse(&pair, 4, d[i], state, 8) != OMEGA_OK) { 66 out[0] = 0; return; 67 } 68 out[2 * i] = (char)pair; 69 out[2 * i + 1] = (char)(pair >> 8); 70 } 71 out[64] = 0; 72 #else 73 static const char h[] = "`0123456789abcdef`"; 74 for (int i = 0; i < 32; i++) { 75 out[2 * i] = h[d[i] >> 4]; 76 out[2 * i + 1] = h[d[i] & 15]; 77 } 78 out[64] = 0; 79 #endif 80 } 81 82 static int is_hex64(const char *s) { 83 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 84 return artifact_text(1, s, NULL, 0); 85 #else 86 if (!s || strlen(s) != 64) return 0; 87 for (int i = 0; i < 64; i++) { 88 char c = s[i]; 89 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 0; 90 } 91 return 1; 92 #endif 93 } 94 95 static int append(char **p, size_t *rem, const char *fmt, ...) { 96 va_list ap; 97 int n; 98 va_start(ap, fmt); 99 n = vsnprintf(*p, *rem, fmt, ap); 100 va_end(ap); 101 if (n < 0 || (size_t)n >= *rem) return -1; 102 *p += n; 103 *rem -= (size_t)n; 104 return 0; 105 } 106 107 /* Artifact files are opened with an explicit 0644, never left to the umask: a 108 * 0666 mode would make them world-writable (CodeQL cpp/world-writable-file-creation). 109 * An artifact is immutable once written: O_EXCL refuses to clobber an existing 110 * file and O_NOFOLLOW refuses a symlink planted at the target (audit F8). */ 111 static FILE *open_write(const char *path) { 112 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644); 113 if (fd < 0) return NULL; 114 FILE *f = fdopen(fd, "wb"); 115 if (!f) close(fd); 116 return f; 117 } 118 119 /* into out, or -1 when it does not fit. An unchecked 120 * snprintf truncated every suffix away at a 1023-byte prefix and all eight 121 * writes landed on one path (audit F8). */ 122 static int join_path(char *out, size_t cap, const char *prefix, const char *suffix) { 123 int n = snprintf(out, cap, "%s%s", prefix, suffix); 124 return (n < 0 || (size_t)n >= cap) ? -1 : 0; 125 } 126 127 static int write_file(const char *path, const void *data, size_t n) { 128 FILE *f = open_write(path); 129 if (!f) return -1; 130 if (n && fwrite(data, 1, n, f) != n) { fclose(f); return -1; } 131 if (fclose(f) != 0) return -1; 132 return 0; 133 } 134 135 static uint8_t *read_file(const char *path, size_t *n) { 136 FILE *f = fopen(path, "rb"); 137 long sz; 138 uint8_t *b; 139 if (!f) return NULL; 140 if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; } 141 sz = ftell(f); 142 if (sz < 0 || sz > (long)(1u << 26)) { fclose(f); return NULL; } 143 if (fseek(f, 0, SEEK_SET) != 0) { fclose(f); return NULL; } 144 b = malloc((size_t)sz + 1u); 145 if (!b) { fclose(f); return NULL; } 146 if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) { free(b); fclose(f); return NULL; } 147 b[sz] = 0; 148 *n = (size_t)sz; 149 fclose(f); 150 return b; 151 } 152 153 /* The QMD is a template: every device address field must be zero. */ 154 static int qmd_template_ok(const uint32_t q0[OMEGA_ARTIFACT_QMD_WORDS], 155 const uint32_t q1[OMEGA_ARTIFACT_QMD_WORDS]) { 156 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 157 uint64_t result = 0; 158 const uint64_t shape[7] = {0}; 159 return omega_artifact_policy(&result, 0, q0, 256, q1, 256, shape, 7) == OMEGA_OK && result == 1; 160 #else 161 if (q0[12] != 0) return 0; 162 if (q1[12] != 0 || q1[15] != 0 || q1[16] != 0) return 0; 163 if (q1[32] != 0 || (q1[33] & ~(0xa << 21)) != 0) return 0; 164 if (q1[42] != 0 || (q1[43] & 0x7ffffu) != 0) return 0; 165 if (q1[44] != 0 || (q1[45] & 0x7ffffu) != 0) return 0; 166 if (q1[52] != (0x300u >> 6) || (q1[53] & 0x7ffffu) != 0) return 0; 167 /* CB7 scratch VA and the code-prefetch VA must also be templates. */ 168 if (q1[56] != 0 || (q1[57] & 0x7ffffu) != 0 || q1[59] != 0) return 0; 169 return 1; 170 #endif 171 } 172 173 /* The launch fields of QMD 1 must say what the manifest says (audit F3): a QMD 174 * that launches 2^31 CTAs or 511 registers under a manifest declaring 1x1 is 175 * refused. Field positions are those omega_blackwell_build_qmd1 writes: 176 * word 34 CTA x|y, word 35 CTA z (7:0) and REGISTER_COUNT (16:8, at least 16), 177 * word 36 SHARED_MEMORY_SIZE_SHIFTED7 (10:0; 0 bytes means the default 1024), 178 * words 39-41 grid x, y, z (z is always 1 in format 1). */ 179 static int qmd_launch_ok(const uint32_t q1[OMEGA_ARTIFACT_QMD_WORDS], uint32_t gx, uint32_t gy, 180 uint32_t bx, uint32_t by, uint32_t bz, uint32_t shared, uint32_t gpr) { 181 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 182 uint64_t result = 0; 183 const uint64_t shape[7] = {gx, gy, bx, by, bz, shared, gpr}; 184 return omega_artifact_policy(&result, 1, q1, 256, q1, 256, shape, 7) == OMEGA_OK && result == 1; 185 #else 186 uint32_t want_gpr = gpr < 16u ? 16u : gpr; 187 uint32_t want_smem7 = shared ? (shared + 127u) / 128u : 8u; 188 if (gx == 0 || gy == 0 || bx == 0 || by == 0 || bz == 0) return 0; 189 if (bx > 0xffffu || by > 0xffffu || bz > 0xffu || want_gpr > 0x1ffu || want_smem7 > 0x7ffu) return 0; 190 if (q1[34] != (bx | (by << 16))) return 0; 191 if ((q1[35] & 0xffu) != bz || ((q1[35] >> 8) & 0x1ffu) != want_gpr) return 0; 192 if ((q1[36] & 0x7ffu) != want_smem7) return 0; 193 return q1[39] == gx && q1[40] == gy && q1[41] == 1u; 194 #endif 195 } 196 197 /* Strict "A.B.C", decimal components of 1-9 digits, nothing else (audit F5: 198 * sscanf with an unchecked count admitted "garbage" and "x999.0.0"). */ 199 static int parse_semver(const char *s, unsigned v[3]) { 200 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 201 return artifact_text(3, s, v, 3) ? 0 : -1; 202 #else 203 if (!s) return -1; 204 for (int i = 0; i < 3; i++) { 205 unsigned x = 0; 206 int d = 0; 207 while (*s >= '0' && *s <= '9') { 208 if (++d > 9) return -1; 209 x = x * 10u + (unsigned)(*s++ - '0'); 210 } 211 if (d == 0) return -1; 212 v[i] = x; 213 if (i < 2 && *s++ != '.') return -1; 214 } 215 return *s == 0 ? 0 : -1; 216 #endif 217 } 218 219 /* Strict unsigned decimal, 1-9 digits, no sign, space or suffix (audit F5b). */ 220 static int parse_u32(const char *s, uint32_t *out) { 221 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 222 return artifact_text(2, s, out, 1) ? 0 : -1; 223 #else 224 uint32_t x = 0; 225 int d = 0; 226 if (!s) return -1; 227 while (*s >= '0' && *s <= '9') { 228 if (++d > 9) return -1; 229 x = x * 10u + (uint32_t)(*s++ - '0'); 230 } 231 if (d == 0 || *s != 0) return -1; 232 *out = x; 233 return 0; 234 #endif 235 } 236 237 /* "a,b,c" of strict decimals, exactly n of them. */ 238 static int parse_u32_list(const char *s, uint32_t *out, int n) { 239 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 240 return artifact_text(2, s, out, (unsigned)n) ? 0 : -1; 241 #else 242 char tmp[64]; 243 size_t len = strlen(s); 244 if (len >= sizeof tmp) return -1; 245 memcpy(tmp, s, len + 1); 246 char *p = tmp; 247 for (int i = 0; i < n; i++) { 248 char *c = strchr(p, ','); 249 if (i < n - 1) { if (!c) return -1; *c = 0; } 250 else if (c) return -1; 251 if (parse_u32(p, &out[i])) return -1; 252 if (c) p = c + 1; 253 } 254 return 0; 255 #endif 256 } 257 258 /* A manifest value is one line of printable text: a newline would forge a new 259 * record and a quote or control byte would forge JSON (audit F7). */ 260 static int field_text_ok(const char *s) { 261 #if !CUDAPP_MIGRATION_PENDING_C_ARTIFACT 262 return artifact_text(0, s, NULL, 0); 263 #else 264 for (; *s; s++) { 265 unsigned char c = (unsigned char)*s; 266 if (c < 0x20 || c == 0x7f) return 0; 267 } 268 return 1; 269 #endif 270 } 271 272 static void json_str(FILE *f, const char *s) { 273 fputc('"', f); 274 for (; *s; s++) { 275 unsigned char c = (unsigned char)*s; 276 #if CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 277 if (c == '"' || c == '\\') { fputc('\\', f); fputc(c, f); } 278 else if (c < 0x20 || c == 0x7f) fprintf(f, "\\u%04x", c); 279 else fputc(c, f); 280 #else 281 unsigned char bytes[6]; 282 uint64_t count = 0; 283 if (omega_na_json(&count, c, bytes, sizeof bytes) != OMEGA_OK) __builtin_trap(); 284 (void)fwrite(bytes, 1, count, f); 285 #endif 286 } 287 fputc('"', f); 288 } 289 290 static void json_put(FILE *f, const char *key, const char *val, int last) { 291 fputs(" ", f); 292 json_str(f, key); 293 fputs(": ", f); 294 json_str(f, val); 295 fputs(last ? "\n" : ",\n", f); 296 } 297 298 int omega_native_artifact_export(const OmegaNativeArtifact *a, const char *out_prefix, 299 char out_id[OMEGA_ARTIFACT_ID_MAX], char *err, size_t err_cap) { 300 char text_hex[65], cub_hex[65], q0_hex[65], q1_hex[65], cb_hex[65], id[65]; 301 uint8_t td[32], cd[32], q0d[32], q1d[32], cbd[32]; 302 uint8_t *cub = NULL, *manifest = NULL; 303 size_t cub_len = 0, mlen; 304 char machine_hex[65]; 305 SemanticId machine; 306 /* Every output path is built and checked before the first byte is 307 * written, so a prefix that does not fit refuses with nothing on disk. */ 308 static const char *const suffix[8] = { ".text.bin", ".cubin", ".qmd0.bin", ".qmd1.bin", 309 ".cbank_args.bin", ".json", ".manifest", ".sha256" }; 310 enum { P_TEXT, P_CUBIN, P_QMD0, P_QMD1, P_CBANK, P_JSON, P_MANIFEST, P_SHA }; 311 char path[8][1024]; 312 int rc = OMEGA_ARTIFACT_OK; 313 314 if (err && err_cap) err[0] = 0; 315 if (out_id) out_id[0] = 0; 316 if (!a || !out_prefix || !out_id || !a->op || !a->shape || !a->bounds || !a->contract_name || !a->kernel_function || 317 !a->text || !a->text_len || !a->params || !a->qmd0 || !a->qmd1 || !a->cbank_args || 318 !a->semantic_spec_id_hex || !a->contract_id_hex || !a->min_driver || !a->requires || 319 !a->precision || !a->accumulation || !a->tolerance || !a->source_commit || !a->tool_version || 320 !a->qualification) { 321 if (err) snprintf(err, err_cap, "missing required artifact field"); 322 return OMEGA_ARTIFACT_E_ARG; 323 } 324 { 325 const char *texts[] = { a->op, a->shape, a->bounds, a->contract_name, a->kernel_function, a->min_driver, 326 a->requires, a->precision, a->accumulation, a->tolerance, a->source_commit, 327 a->tool_version, a->qualification }; 328 for (size_t i = 0; i < sizeof texts / sizeof texts[0]; i++) 329 if (!field_text_ok(texts[i])) { 330 if (err) snprintf(err, err_cap, "artifact text fields must be one line without control bytes"); 331 return OMEGA_ARTIFACT_E_ARG; 332 } 333 } 334 { 335 unsigned v[3]; 336 if (parse_semver(a->min_driver, v)) { 337 if (err) snprintf(err, err_cap, "min_driver '%s' is not a strict A.B.C version", a->min_driver); 338 return OMEGA_ARTIFACT_E_ARG; 339 } 340 } 341 for (int i = 0; i < 8; i++) 342 if (join_path(path[i], sizeof path[i], out_prefix, suffix[i])) { 343 if (err) snprintf(err, err_cap, "output prefix too long: '%s%s' does not fit %zu bytes", 344 "", suffix[i], sizeof path[i]); 345 return OMEGA_ARTIFACT_E_ARG; 346 } 347 if (!is_hex64(a->contract_id_hex) || !is_hex64(a->semantic_spec_id_hex)) { 348 if (err) snprintf(err, err_cap, "contract_id and semantic_spec_id must be 64 lowercase hex"); 349 return OMEGA_ARTIFACT_E_CONTRACT; 350 } 351 if (strcmp(a->op, "matmul") != 0 && strcmp(a->op, "gqa-causal-attention") != 0) { 352 if (err) snprintf(err, err_cap, "unsupported op '%s'", a->op); 353 return OMEGA_ARTIFACT_E_UNSUPPORTED; 354 } 355 if (!qmd_template_ok(a->qmd0, a->qmd1)) { 356 if (err) snprintf(err, err_cap, "QMD contains a live device address; export a zero-VA template"); 357 return OMEGA_ARTIFACT_E_RELOC; 358 } 359 if (!qmd_launch_ok(a->qmd1, a->grid_x, a->grid_y, a->block_x, a->block_y, a->block_z, a->shared_bytes, 360 a->gpr_count)) { 361 if (err) snprintf(err, err_cap, "QMD launch fields (grid/block/registers/shared) disagree with the descriptor"); 362 return OMEGA_ARTIFACT_E_CONTRACT; 363 } 364 if (omega_blackwell_get_machine_id(&machine) != 0) { 365 if (err) snprintf(err, err_cap, "machine profile unavailable"); 366 return OMEGA_ARTIFACT_E_ARG; 367 } 368 hex64(machine.bytes, machine_hex); 369 370 { 371 uint32_t cbank_end = OMEGA_CUBIN_PARAM_BASE; 372 for (size_t i = 0; i < a->nparams; i++) { 373 uint32_t e = a->params[i].offset + a->params[i].size; 374 if (e > cbank_end) cbank_end = e; 375 } 376 rc = omega_cubin_pack(a->kernel_function, a->text, a->text_len, a->params, a->nparams, 377 cbank_end, a->gpr_count, 64u, &cub, &cub_len, err, err_cap); 378 } 379 if (rc != OMEGA_CUBIN_OK) { 380 if (err && err[0] == 0) snprintf(err, err_cap, "cubin pack refused: %s", omega_cubin_rc_name(rc)); 381 return (rc == OMEGA_CUBIN_E_MEM) ? OMEGA_ARTIFACT_E_MEM : OMEGA_ARTIFACT_E_UNSUPPORTED; 382 } 383 if (a->cbank_offset != OMEGA_CUBIN_PARAM_BASE || a->cbank_words == 0 || 384 a->cbank_words > OMEGA_ARTIFACT_MAX_CBANK_WORDS) { 385 free(cub); 386 if (err) snprintf(err, err_cap, "cbank offset/words outside the supported layout"); 387 return OMEGA_ARTIFACT_E_UNSUPPORTED; 388 } 389 390 sha256_hash(a->text, a->text_len, td); 391 sha256_hash(cub, cub_len, cd); 392 sha256_hash((const uint8_t *)a->qmd0, OMEGA_ARTIFACT_QMD_WORDS * 4u, q0d); 393 sha256_hash((const uint8_t *)a->qmd1, OMEGA_ARTIFACT_QMD_WORDS * 4u, q1d); 394 sha256_hash((const uint8_t *)a->cbank_args, a->cbank_words * 4u, cbd); 395 hex64(td, text_hex); hex64(cd, cub_hex); hex64(q0d, q0_hex); hex64(q1d, q1_hex); hex64(cbd, cb_hex); 396 397 /* Identity block: exactly these lines, in this order, each newline-terminated. */ 398 { 399 char idbuf[MANIFEST_CAP], *p = idbuf; 400 size_t rem = sizeof idbuf; 401 if (append(&p, &rem, "op=%s\n", a->op) || 402 append(&p, &rem, "shape=%s\n", a->shape) || 403 append(&p, &rem, "bounds=%s\n", a->bounds) || 404 append(&p, &rem, "contract=%s\n", a->contract_name) || 405 append(&p, &rem, "contract_id=%s\n", a->contract_id_hex) || 406 append(&p, &rem, "semantic_spec_id=%s\n", a->semantic_spec_id_hex) || 407 append(&p, &rem, "isa=sm_121\n") || 408 append(&p, &rem, "machine_profile=%s\n", machine_hex) || 409 append(&p, &rem, "kernel_format=nvidia-cubin-elf64\n") || 410 append(&p, &rem, "kernel_function=%s\n", a->kernel_function) || 411 append(&p, &rem, "text_sha256=%s\n", text_hex) || 412 append(&p, &rem, "text_size=%zu\n", a->text_len) || 413 append(&p, &rem, "cubin_sha256=%s\n", cub_hex) || 414 append(&p, &rem, "cubin_size=%zu\n", cub_len) || 415 append(&p, &rem, "qmd0_sha256=%s\n", q0_hex) || 416 append(&p, &rem, "qmd1_sha256=%s\n", q1_hex) || 417 append(&p, &rem, "qmd_template=zero-va-v1\n") || 418 append(&p, &rem, "cbank_offset=0x%x\n", a->cbank_offset) || 419 append(&p, &rem, "cbank_words=%zu\n", a->cbank_words) || 420 append(&p, &rem, "cbank_args=template\n") || 421 append(&p, &rem, "cbank_args_sha256=%s\n", cb_hex) || 422 append(&p, &rem, "grid=%u,%u,1\n", a->grid_x, a->grid_y) || 423 append(&p, &rem, "block=%u,%u,%u\n", a->block_x, a->block_y, a->block_z) || 424 append(&p, &rem, "shared_bytes=%u\n", a->shared_bytes) || 425 append(&p, &rem, "gpr_count=%u\n", a->gpr_count) || 426 append(&p, &rem, "precision=%s\n", a->precision) || 427 append(&p, &rem, "accumulation=%s\n", a->accumulation) || 428 append(&p, &rem, "tolerance=%s\n", a->tolerance) || 429 append(&p, &rem, "min_driver=%s\n", a->min_driver) || 430 append(&p, &rem, "requires=%s\n", a->requires) || 431 append(&p, &rem, "qualification=%s\n", a->qualification) || 432 append(&p, &rem, "provenance_source_commit=%s\n", a->source_commit)) { 433 free(cub); 434 if (err) snprintf(err, err_cap, "identity block overflow"); 435 return OMEGA_ARTIFACT_E_ARG; 436 } 437 mlen = (size_t)(p - idbuf); 438 sha256_hash((const uint8_t *)idbuf, mlen, cd); 439 hex64(cd, id); 440 manifest = malloc(MANIFEST_CAP); 441 if (!manifest) { free(cub); return OMEGA_ARTIFACT_E_MEM; } 442 { 443 char *mp = (char *)manifest; 444 size_t mr = MANIFEST_CAP; 445 if (append(&mp, &mr, "%s\n", OMEGA_ARTIFACT_FORMAT) || 446 append(&mp, &mr, "artifact_id=%s\n", id) || 447 append(&mp, &mr, "%s", idbuf) || 448 append(&mp, &mr, "provenance_tool=%s\n", a->tool_version)) { 449 free(cub); free(manifest); 450 if (err) snprintf(err, err_cap, "manifest overflow"); 451 return OMEGA_ARTIFACT_E_ARG; 452 } 453 mlen = (size_t)(mp - (char *)manifest); 454 } 455 } 456 457 /* Files. Every write is checked; the manifest is written last. */ 458 if (write_file(path[P_TEXT], a->text, a->text_len)) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 459 if (write_file(path[P_CUBIN], cub, cub_len)) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 460 if (write_file(path[P_QMD0], a->qmd0, OMEGA_ARTIFACT_QMD_WORDS * 4u)) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 461 if (write_file(path[P_QMD1], a->qmd1, OMEGA_ARTIFACT_QMD_WORDS * 4u)) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 462 if (write_file(path[P_CBANK], a->cbank_args, a->cbank_words * 4u)) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 463 464 { 465 FILE *f = open_write(path[P_JSON]); 466 if (!f) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 467 fprintf(f, "{\n \"format\": \"%s\",\n", OMEGA_ARTIFACT_FORMAT); 468 json_put(f, "artifact_id", id, 0); 469 json_put(f, "op", a->op, 0); 470 json_put(f, "shape", a->shape, 0); 471 json_put(f, "bounds", a->bounds, 0); 472 json_put(f, "contract", a->contract_name, 0); 473 json_put(f, "contract_id", a->contract_id_hex, 0); 474 json_put(f, "semantic_spec_id", a->semantic_spec_id_hex, 0); 475 json_put(f, "isa", "sm_121", 0); 476 json_put(f, "machine_profile", machine_hex, 0); 477 json_put(f, "kernel_format", "nvidia-cubin-elf64", 0); 478 json_put(f, "kernel_function", a->kernel_function, 0); 479 json_put(f, "text_sha256", text_hex, 0); 480 json_put(f, "cubin_sha256", cub_hex, 0); 481 fprintf(f, " \"text_size\": %zu,\n \"cubin_size\": %zu,\n", a->text_len, cub_len); 482 fprintf(f, " \"cbank_offset\": %u,\n \"cbank_words\": %zu,\n", a->cbank_offset, a->cbank_words); 483 fprintf(f, " \"grid\": [%u,%u,1],\n \"block\": [%u,%u,%u],\n", 484 a->grid_x, a->grid_y, a->block_x, a->block_y, a->block_z); 485 fprintf(f, " \"shared_bytes\": %u,\n \"gpr_count\": %u,\n", a->shared_bytes, a->gpr_count); 486 json_put(f, "qmd0_sha256", q0_hex, 0); 487 json_put(f, "qmd1_sha256", q1_hex, 0); 488 json_put(f, "cbank_args_sha256", cb_hex, 0); 489 json_put(f, "qmd_template", "zero-va-v1", 0); 490 json_put(f, "precision", a->precision, 0); 491 json_put(f, "accumulation", a->accumulation, 0); 492 json_put(f, "tolerance", a->tolerance, 0); 493 json_put(f, "min_driver", a->min_driver, 0); 494 json_put(f, "requires", a->requires, 0); 495 json_put(f, "provenance_tool", a->tool_version, 0); 496 json_put(f, "provenance_source_commit", a->source_commit, 0); 497 json_put(f, "qualification", a->qualification, 1); 498 fprintf(f, "}\n"); 499 if (fclose(f) != 0) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 500 } 501 if (write_file(path[P_MANIFEST], manifest, mlen)) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 502 { 503 uint8_t md[32]; char mhex[65]; 504 sha256_hash(manifest, mlen, md); hex64(md, mhex); 505 FILE *f = open_write(path[P_SHA]); 506 if (!f) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 507 /* Role names, never absolute or caller-chosen paths: the listing is 508 * byte-identical for the same artifact exported under any prefix. */ 509 fprintf(f, "%s artifact_id\n", id); 510 fprintf(f, "%s manifest\n", mhex); 511 fprintf(f, "%s cubin\n", cub_hex); 512 fprintf(f, "%s text\n", text_hex); 513 fprintf(f, "%s qmd0\n", q0_hex); 514 fprintf(f, "%s qmd1\n", q1_hex); 515 fprintf(f, "%s cbank_args\n", cb_hex); 516 if (fclose(f) != 0) { rc = OMEGA_ARTIFACT_E_IO; goto done; } 517 } 518 snprintf(out_id, OMEGA_ARTIFACT_ID_MAX, "%s", id); 519 done: 520 free(cub); 521 free(manifest); 522 return rc; 523 } 524 525 /* ---- admission ------------------------------------------------------- */ 526 527 #if CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 528 static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } 529 static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } 530 static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } 531 #endif 532 533 /* Confirm that the cubin's .text. bytes are exactly the bound text 534 * and that its section table is well formed. The digest of text.bin alone says 535 * nothing about what the cubin actually contains (REVIEW C-21). */ 536 static int cubin_text_matches(const uint8_t *cub, size_t n, const char *fn, 537 const uint8_t *text, size_t tn) { 538 #if CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 539 if (n < 64 || memcmp(cub, "\177ELF", 4) != 0) return 0; 540 if (rd16(cub + 18) != 190) return 0; 541 uint64_t shoff = rd64(cub + 40); 542 uint16_t shentsize = rd16(cub + 58), shnum = rd16(cub + 60), shstrndx = rd16(cub + 62); 543 if (shentsize != 64 || shnum == 0 || shstrndx >= shnum) return 0; 544 if (shoff > n || (uint64_t)shnum * 64 > n - shoff) return 0; 545 const uint8_t *shstr = cub + shoff + (uint64_t)shstrndx * 64; 546 uint64_t stroff = rd64(shstr + 24), strsz = rd64(shstr + 32); 547 if (stroff > n || strsz > n - stroff) return 0; 548 char want[128]; 549 int wl = snprintf(want, sizeof want, ".text.%s", fn); 550 if (wl <= 0 || (size_t)wl >= sizeof want) return 0; 551 for (uint16_t i = 0; i < shnum; i++) { 552 const uint8_t *sh = cub + shoff + (uint64_t)i * 64; 553 uint32_t name = rd32(sh); 554 if (name >= strsz) continue; 555 const char *s = (const char *)cub + stroff + name; 556 size_t max = (size_t)(strsz - name); 557 if (strnlen(s, max) != (size_t)wl || memcmp(s, want, (size_t)wl) != 0) continue; 558 uint64_t off = rd64(sh + 24), sz = rd64(sh + 32); 559 if (off > n || sz != tn || sz > n - off) return 0; 560 return memcmp(cub + off, text, tn) == 0; 561 } 562 return 0; 563 #else 564 uint64_t r = 0; 565 if (omega_na_cubin(&r, cub, n, fn, strlen(fn), text, tn) != OMEGA_OK) __builtin_trap(); 566 return (int)r; 567 #endif 568 } 569 570 /* Version 1 has one canonical ordered record per field. Validate the whole 571 * byte span before any string lookup: otherwise find_line can select policies 572 * after provenance_tool even though the identity hash stops at that boundary. 573 * Unknown/duplicate/reordered fields require a new format, not reinterpretation 574 * of an existing artifact id. provenance_tool is deliberately outside the id. */ 575 static int manifest_layout_ok(const uint8_t *buf, size_t n) { 576 #if CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 577 static const char *const keys[] = { 578 "artifact_id=", "op=", "shape=", "bounds=", "contract=", 579 "contract_id=", "semantic_spec_id=", "isa=", "machine_profile=", 580 "kernel_format=", "kernel_function=", "text_sha256=", "text_size=", 581 "cubin_sha256=", "cubin_size=", "qmd0_sha256=", "qmd1_sha256=", 582 "qmd_template=", "cbank_offset=", "cbank_words=", "cbank_args=", 583 "cbank_args_sha256=", "grid=", "block=", "shared_bytes=", "gpr_count=", 584 "precision=", "accumulation=", "tolerance=", "min_driver=", "requires=", 585 "qualification=", "provenance_source_commit=", "provenance_tool=" 586 }; 587 const size_t header = sizeof(OMEGA_ARTIFACT_FORMAT "\n") - 1; 588 if (n < header || n >= MANIFEST_CAP || memchr(buf, 0, n) || 589 memchr(buf, '\r', n) || memcmp(buf, OMEGA_ARTIFACT_FORMAT "\n", header)) return 0; 590 size_t off = header; 591 for (size_t i = 0; i < sizeof keys / sizeof keys[0]; i++) { 592 size_t k = strlen(keys[i]); 593 if (k > n - off || memcmp(buf + off, keys[i], k)) return 0; 594 const uint8_t *nl = memchr(buf + off + k, '\n', n - off - k); 595 if (!nl) return 0; 596 off = (size_t)(nl - buf) + 1; 597 } 598 return off == n; 599 #else 600 static const char keys[] = 601 OMEGA_ARTIFACT_FORMAT "\n\0" 602 "artifact_id=\0" "op=\0" "shape=\0" "bounds=\0" "contract=\0" 603 "contract_id=\0" "semantic_spec_id=\0" "isa=\0" "machine_profile=\0" 604 "kernel_format=\0" "kernel_function=\0" "text_sha256=\0" "text_size=\0" 605 "cubin_sha256=\0" "cubin_size=\0" "qmd0_sha256=\0" "qmd1_sha256=\0" 606 "qmd_template=\0" "cbank_offset=\0" "cbank_words=\0" "cbank_args=\0" 607 "cbank_args_sha256=\0" "grid=\0" "block=\0" "shared_bytes=\0" "gpr_count=\0" 608 "precision=\0" "accumulation=\0" "tolerance=\0" "min_driver=\0" "requires=\0" 609 "qualification=\0" "provenance_source_commit=\0" "provenance_tool=\0"; 610 uint64_t r = 0; 611 if (omega_na_layout(&r, buf, n, keys, sizeof keys - 1, MANIFEST_CAP) != OMEGA_OK) __builtin_trap(); 612 return (int)r; 613 #endif 614 } 615 616 static char *find_line(char *buf, const char *key) { 617 size_t k = strlen(key); 618 char *p = buf; 619 while (*p) { 620 if (strncmp(p, key, k) == 0) return p; 621 char *nl = strchr(p, '\n'); 622 if (!nl) break; 623 p = nl + 1; 624 } 625 return NULL; 626 } 627 628 static int copy_value(const char *line, char *out, size_t cap) { 629 const char *eq = strchr(line, '='); 630 const char *nl; 631 size_t n; 632 if (!eq) return -1; 633 eq++; 634 nl = strchr(eq, '\n'); 635 n = nl ? (size_t)(nl - eq) : strlen(eq); 636 if (n + 1 > cap) return -1; 637 memcpy(out, eq, n); 638 out[n] = 0; 639 return 0; 640 } 641 642 /* Both versions strict A.B.C; returns 1 when have >= need, 0 when older, 643 * -1 when either does not parse (refused, never read as 0.0.0). */ 644 static int semver_ge(const char *have, const char *need) { 645 unsigned h[3], n[3]; 646 if (parse_semver(have, h) || parse_semver(need, n)) return -1; 647 #if CUDAPP_MIGRATION_PENDING_C_NATIVE_ARTIFACT_REST 648 for (int i = 0; i < 3; i++) { 649 if (h[i] != n[i]) return h[i] > n[i]; 650 } 651 return 1; 652 #else 653 uint64_t r = 0; 654 if (omega_na_ver(&r, h, 3, n, 3) != OMEGA_OK) __builtin_trap(); 655 return (int)r; 656 #endif 657 } 658 659 static int artifact_admit(const char *out_prefix, const char *current_driver, 660 char out_id[OMEGA_ARTIFACT_ID_MAX], 661 OmegaNativeArtifactSnapshot *snapshot, char *err, size_t err_cap) { 662 enum { A_MANIFEST, A_TEXT, A_CUBIN, A_QMD0, A_QMD1, A_CBANK, A_N }; 663 static const char *const suffix[A_N] = { ".manifest", ".text.bin", ".cubin", ".qmd0.bin", ".qmd1.bin", 664 ".cbank_args.bin" }; 665 char path[A_N][1024], id[65], kernel[64] = {0}; 666 size_t mn = 0; 667 uint8_t *files[A_N] = {0}; 668 size_t sizes[A_N] = {0}; 669 uint8_t *man; 670 char *ident, *end, *p; 671 int rc = OMEGA_ARTIFACT_OK; 672 if (err && err_cap) err[0] = 0; 673 if (out_id) out_id[0] = 0; 674 if (snapshot) memset(snapshot, 0, sizeof *snapshot); 675 if (!out_prefix) return OMEGA_ARTIFACT_E_ARG; 676 for (int i = 0; i < A_N; i++) 677 if (join_path(path[i], sizeof path[i], out_prefix, suffix[i])) { 678 if (err) snprintf(err, err_cap, "artifact prefix too long for '%s'", suffix[i]); 679 return OMEGA_ARTIFACT_E_ARG; 680 } 681 files[A_MANIFEST] = man = read_file(path[A_MANIFEST], &mn); 682 sizes[A_MANIFEST] = mn; 683 if (!man) { if (err) snprintf(err, err_cap, "cannot read %s", path[A_MANIFEST]); return OMEGA_ARTIFACT_E_IO; } 684 if (!manifest_layout_ok(man, mn)) { 685 rc = OMEGA_ARTIFACT_E_FORMAT; goto done; 686 } 687 { 688 char *l = find_line((char *)man, "artifact_id="); 689 if (!l || copy_value(l, id, sizeof id) || !is_hex64(id)) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 690 } 691 ident = find_line((char *)man, "op="); 692 if (!ident) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 693 end = find_line(ident, "provenance_tool="); 694 if (!end) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 695 { 696 uint8_t d[32]; char want[65]; 697 sha256_hash((const uint8_t *)ident, (size_t)(end - ident), d); 698 hex64(d, want); 699 if (strcmp(want, id) != 0) { 700 if (err) snprintf(err, err_cap, "artifact id mismatch: manifest %s recomputed %s", id, want); 701 rc = OMEGA_ARTIFACT_E_DIGEST; goto done; 702 } 703 } 704 /* Fields and their files. */ 705 { 706 struct { const char *key; int file; size_t fixed; } checks[] = { 707 { "text_sha256=", A_TEXT, 0 }, 708 { "cubin_sha256=", A_CUBIN, 0 }, 709 { "qmd0_sha256=", A_QMD0, OMEGA_ARTIFACT_QMD_WORDS * 4u }, 710 { "qmd1_sha256=", A_QMD1, OMEGA_ARTIFACT_QMD_WORDS * 4u }, 711 { "cbank_args_sha256=", A_CBANK, 0 }, 712 }; 713 for (size_t i = 0; i < sizeof checks / sizeof checks[0]; i++) { 714 char v[65]; 715 const char *fn = path[checks[i].file]; 716 uint8_t *b, d[32]; 717 char got[65]; 718 size_t n = 0; 719 p = find_line(ident, checks[i].key); 720 if (!p || copy_value(p, v, sizeof v) || !is_hex64(v)) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 721 files[checks[i].file] = b = read_file(fn, &n); 722 sizes[checks[i].file] = n; 723 if (!b) { if (err) snprintf(err, err_cap, "cannot read %s", fn); rc = OMEGA_ARTIFACT_E_IO; goto done; } 724 if (checks[i].fixed && n != checks[i].fixed) { rc = OMEGA_ARTIFACT_E_DIGEST; goto done; } 725 sha256_hash(b, n, d); hex64(d, got); 726 if (strcmp(got, v) != 0) { 727 if (err) snprintf(err, err_cap, "%s digest mismatch for %s", checks[i].key, fn); 728 rc = OMEGA_ARTIFACT_E_DIGEST; goto done; 729 } 730 } 731 } 732 { 733 char v[64]; 734 const uint8_t *t = files[A_TEXT], *c = files[A_CUBIN], *q = NULL; 735 size_t tn = sizes[A_TEXT], cn = sizes[A_CUBIN], qn = 0; 736 int ok = 0; 737 p = find_line(ident, "kernel_function="); 738 if (!p || copy_value(p, v, sizeof v) || v[0] == 0) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 739 memcpy(kernel, v, strlen(v) + 1); 740 if (!t || !c) { rc = OMEGA_ARTIFACT_E_IO; goto cross_done; } 741 ok = cubin_text_matches(c, cn, v, t, tn); 742 if (!ok) { if (err) snprintf(err, err_cap, "cubin text does not match text.bin"); rc = OMEGA_ARTIFACT_E_DIGEST; goto cross_done; } 743 { 744 uint32_t a[OMEGA_ARTIFACT_QMD_WORDS], b[OMEGA_ARTIFACT_QMD_WORDS]; 745 uint32_t grid[3], block[3], shared, gpr; 746 q = files[A_QMD0]; qn = sizes[A_QMD0]; 747 if (!q || qn != sizeof a) { rc = OMEGA_ARTIFACT_E_FORMAT; goto cross_done; } 748 memcpy(a, q, sizeof a); 749 q = files[A_QMD1]; qn = sizes[A_QMD1]; 750 if (!q || qn != sizeof b) { rc = OMEGA_ARTIFACT_E_FORMAT; goto cross_done; } 751 memcpy(b, q, sizeof b); 752 if (!qmd_template_ok(a, b)) { if (err) snprintf(err, err_cap, "QMD contains a live address"); rc = OMEGA_ARTIFACT_E_RELOC; goto cross_done; } 753 /* The digests bind the QMD bytes; this binds what they launch to the 754 * manifest's declared launch (audit F3). */ 755 if (!(p = find_line(ident, "grid=")) || copy_value(p, v, sizeof v) || parse_u32_list(v, grid, 3) || 756 !(p = find_line(ident, "block=")) || copy_value(p, v, sizeof v) || parse_u32_list(v, block, 3) || 757 !(p = find_line(ident, "shared_bytes=")) || copy_value(p, v, sizeof v) || parse_u32(v, &shared) || 758 !(p = find_line(ident, "gpr_count=")) || copy_value(p, v, sizeof v) || parse_u32(v, &gpr) || 759 grid[2] != 1u) { 760 rc = OMEGA_ARTIFACT_E_FORMAT; goto cross_done; 761 } 762 if (!qmd_launch_ok(b, grid[0], grid[1], block[0], block[1], block[2], shared, gpr)) { 763 if (err) snprintf(err, err_cap, "QMD launch fields disagree with the manifest grid/block/shared/gpr"); 764 rc = OMEGA_ARTIFACT_E_CONTRACT; goto cross_done; 765 } 766 } 767 p = find_line(ident, "cbank_words="); 768 if (!p || copy_value(p, v, sizeof v)) { rc = OMEGA_ARTIFACT_E_FORMAT; goto cross_done; } 769 { 770 uint32_t w; 771 q = files[A_CBANK]; qn = sizes[A_CBANK]; 772 if (!q || parse_u32(v, &w) || w == 0 || w > OMEGA_ARTIFACT_MAX_CBANK_WORDS || qn != (size_t)w * 4u) { 773 if (err) snprintf(err, err_cap, "cbank_words '%s' is not a decimal in 1..%u matching cbank_args.bin", 774 v, OMEGA_ARTIFACT_MAX_CBANK_WORDS); 775 rc = OMEGA_ARTIFACT_E_FORMAT; goto cross_done; 776 } 777 } 778 cross_done: 779 if (rc) goto done; 780 } 781 { 782 char v[128]; 783 p = find_line(ident, "shape="); 784 if (!p || copy_value(p, v, sizeof v) || v[0] == 0) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 785 p = find_line(ident, "bounds="); 786 if (!p || copy_value(p, v, sizeof v) || v[0] == 0) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 787 p = find_line(ident, "isa="); 788 if (!p || copy_value(p, v, sizeof v) || strcmp(v, "sm_121") != 0) { rc = OMEGA_ARTIFACT_E_ISA; goto done; } 789 p = find_line(ident, "kernel_format="); 790 if (!p || copy_value(p, v, sizeof v) || strcmp(v, "nvidia-cubin-elf64") != 0) { rc = OMEGA_ARTIFACT_E_FORMAT; goto done; } 791 p = find_line(ident, "qmd_template="); 792 if (!p || copy_value(p, v, sizeof v) || strcmp(v, "zero-va-v1") != 0) { rc = OMEGA_ARTIFACT_E_RELOC; goto done; } 793 p = find_line(ident, "contract_id="); 794 if (!p || copy_value(p, v, sizeof v) || !is_hex64(v)) { rc = OMEGA_ARTIFACT_E_CONTRACT; goto done; } 795 p = find_line(ident, "min_driver="); 796 { 797 unsigned need[3]; 798 if (!p || copy_value(p, v, sizeof v) || parse_semver(v, need)) { 799 if (err) snprintf(err, err_cap, "manifest min_driver is not a strict A.B.C version"); 800 rc = OMEGA_ARTIFACT_E_FORMAT; goto done; 801 } 802 } 803 if (current_driver) { 804 int ge = semver_ge(current_driver, v); 805 if (ge < 0) { 806 if (err) snprintf(err, err_cap, "current driver '%s' is not a strict A.B.C version", current_driver); 807 rc = OMEGA_ARTIFACT_E_ARG; goto done; 808 } 809 if (!ge) { 810 if (err) snprintf(err, err_cap, "driver %s is older than required %s", current_driver, v); 811 rc = OMEGA_ARTIFACT_E_DRIVER; goto done; 812 } 813 } 814 } 815 /* The id is reported only for an artifact that passed every check. */ 816 if (out_id) snprintf(out_id, OMEGA_ARTIFACT_ID_MAX, "%s", id); 817 done: 818 if (!rc && snapshot) { 819 memcpy(snapshot->bytes, files, sizeof files); 820 memcpy(snapshot->sizes, sizes, sizeof sizes); 821 memcpy(snapshot->kernel_function, kernel, sizeof kernel); 822 memcpy(snapshot->id, id, sizeof id); 823 } else { 824 for (int i = 0; i < A_N; ++i) free(files[i]); 825 } 826 return rc; 827 } 828 829 int omega_native_artifact_admit(const char *prefix, const char *driver, 830 char out_id[OMEGA_ARTIFACT_ID_MAX], char *err, size_t err_cap) { 831 return artifact_admit(prefix, driver, out_id, NULL, err, err_cap); 832 } 833 int omega_native_artifact_snapshot(const char *prefix, const char *driver, 834 char out_id[OMEGA_ARTIFACT_ID_MAX], OmegaNativeArtifactSnapshot *out, 835 char *err, size_t err_cap) { 836 if (!out) return OMEGA_ARTIFACT_E_ARG; 837 return artifact_admit(prefix, driver, out_id, out, err, err_cap); 838 } 839 void omega_native_artifact_snapshot_free(OmegaNativeArtifactSnapshot *snapshot) { 840 if (!snapshot) return; 841 for (unsigned i = 0; i < OMEGA_SNAPSHOT_FILES; ++i) free(snapshot->bytes[i]); 842 memset(snapshot, 0, sizeof *snapshot); 843 } 844 #else 845 #include "omega_native_artifact_om.inc" 846 #endif