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/tests/ampere_cubin_test.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 / tests / ampere_cubin_test.c` 99 lines · 5281 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Host-only ELF/container checks. No driver, libcuda or device access. */ 3 #include "omega_cubin.h" 4 #include 5 #include 6 #include 7 #include 8 #include 9 10 #define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); return 1; } } while (0) 11 static uint16_t u16(const uint8_t *p) { return (uint16_t)(p[0] | (uint16_t)p[1] << 8); } 12 static uint32_t u32(const uint8_t *p) { return (uint32_t)u16(p) | (uint32_t)u16(p + 2) << 16; } 13 static uint64_t u64(const uint8_t *p) { return (uint64_t)u32(p) | (uint64_t)u32(p + 4) << 32; } 14 15 /* Independent metadata reader, with no inferred target from a filename. */ 16 static int target_matches(const uint8_t *p, size_t n, uint32_t sm) { 17 if (n < 64 || memcmp(p, "\177ELF", 4) || p[4] != 2 || p[5] != 1 || u16(p + 18) != 190) return 0; 18 uint32_t flags = sm == 86 ? ((86u << 16) | 0x500u | 86u) : OMEGA_CUBIN_EFLAGS_SM121; 19 if (sm != 86 && sm != 121) return 0; 20 if (u32(p + 48) != flags || p[7] != (sm == 86 ? 0x33 : 0x41) || p[8] != (sm == 86 ? 7 : 8)) return 0; 21 if (u32(p + 20) != (sm == 86 ? 130u : 1u)) return 0; 22 uint64_t sh = u64(p + 40); 23 size_t count = u16(p + 60), step = u16(p + 58); 24 if (step != 64 || sh > n || count > (n - (size_t)sh) / step) return 0; 25 unsigned note_seen = 0, bank_seen = 0, tk_seen = 0; 26 for (size_t i = 1; i < count; ++i) { 27 const uint8_t *s = p + (size_t)sh + i * step; 28 uint32_t ty = u32(s + 4); 29 uint64_t off = u64(s + 24), len = u64(s + 32); 30 if (ty != 8 && (off > n || len > n - off)) return 0; 31 /* Blackwell-only capability/mercury sections must not leak into Ampere. */ 32 if (sm == 86 && (ty == 0x70000016u || ty == 0x70000082u || ty == 0x70000083u || ty == 0x70000085u)) return 0; 33 if (ty == 0x70000000u) { 34 for (uint64_t j = 0; j + 12 <= len; j += 4) { 35 const uint8_t *attr = p + off + j; 36 if (u16(attr) == 0x0a04 && u16(attr + 2) == 8) { 37 if (u32(attr + 4) != 3 || u16(attr + 8) != 38 (sm == 86 ? OMEGA_CUBIN_PARAM_BASE_SM86 : OMEGA_CUBIN_PARAM_BASE)) return 0; 39 ++bank_seen; 40 } 41 } 42 } 43 if (ty == 7 && len == 120 && u32(p + off + 8) == 2000) { 44 /* .note.nv.tkinfo: sm_121 only (CUDApp#68); never in an sm_86 image. */ 45 if (sm != 121 || u32(p + off) != 12 || u32(p + off + 4) != 96 || 46 memcmp(p + off + 12, "NVIDIA Corp", 11)) return 0; 47 ++tk_seen; 48 } else if (ty == 7) { 49 if (len != 32 || u32(p + off) != 12 || u32(p + off + 4) != 8 || 50 memcmp(p + off + 12, "NVIDIA Corp", 11) || u16(p + off + 26) != sm) return 0; 51 ++note_seen; 52 } 53 } 54 return note_seen == 1 && bank_seen == 1 && tk_seen == (sm == 121 ? 1u : 0u); 55 } 56 57 int main(int argc, char **argv) { 58 /* Our EXIT word; offline nvdisasm is the independent decoding oracle. 59 * No NVIDIA binary or listing is embedded in this test. */ 60 const uint8_t text[16] = {0x4d,0x79,0,0, 0,0,0,0, 0,0,0x80,3, 0,0xea,0x0f,0}; 61 OmegaCubinParam param = {OMEGA_CUBIN_PARAM_BASE_SM86, 8, 1}; 62 uint8_t *image = NULL, *old = NULL; 63 size_t n = 0, oldn = 0; 64 char err[256]; 65 int rc = omega_cubin_pack_target(86, "ampere_exit", text, sizeof text, ¶m, 1, 1024, 8, 0, &image, &n, err, sizeof err); 66 CHECK(rc == OMEGA_CUBIN_OK); 67 CHECK(target_matches(image, n, 86)); 68 CHECK(!target_matches(image, n, 121)); 69 image[48] ^= 1; 70 CHECK(!target_matches(image, n, 86)); /* negative control: wrong e_flags */ 71 image[48] ^= 1; 72 image[8] = 8; 73 CHECK(!target_matches(image, n, 86)); /* negative control: wrong ABI */ 74 image[8] = 7; 75 if (argc == 2) { 76 /* Create new offline fixtures privately even with a permissive umask. */ 77 int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); 78 FILE *f = fd >= 0 ? fdopen(fd, "wb") : NULL; 79 if (!f && fd >= 0) close(fd); 80 CHECK(f != NULL); 81 int wrote = fwrite(image, 1, n, f) == n; 82 int closed = fclose(f) == 0; 83 CHECK(wrote && closed); 84 } else CHECK(argc == 1); 85 omega_cubin_free(image); 86 image = NULL; 87 CHECK(omega_cubin_pack_target(0, "bad", text, sizeof text, NULL, 0, 1024, 8, 0, &image, &n, err, sizeof err) == OMEGA_CUBIN_E_TARGET); 88 CHECK(image == NULL && n == 0); 89 CHECK(omega_cubin_pack_target(89, "bad", text, sizeof text, NULL, 0, 1024, 8, 0, &image, &n, err, sizeof err) == OMEGA_CUBIN_E_TARGET); 90 CHECK(omega_cubin_pack_target(121, "bad", text, sizeof text, ¶m, 1, 1024, 8, 0, &image, &n, err, sizeof err) == OMEGA_CUBIN_E_PARAMS); 91 param.offset = OMEGA_CUBIN_PARAM_BASE; 92 CHECK(omega_cubin_pack("legacy", text, sizeof text, ¶m, 1, 1024, 8, 0, &old, &oldn, err, sizeof err) == 0); 93 CHECK(omega_cubin_pack_target(121, "legacy", text, sizeof text, ¶m, 1, 1024, 8, 0, &image, &n, err, sizeof err) == 0); 94 CHECK(n == oldn && memcmp(image, old, n) == 0 && target_matches(image, n, 121)); 95 omega_cubin_free(image); 96 omega_cubin_free(old); 97 puts("AMPERE CUBIN HOST PASS; device NOT_RUN"); 98 return 0; 99 }