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/zero_cuda_tool_argv_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 / zero_cuda_tool_argv_test.c` 290 lines · 9810 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Host test for omega_blackwell_verify_zero_libcuda_linkage and 3 * omega_blackwell_verify_zero_cuda_symbols. binary_path is one pathname, including 4 * when it contains spaces or shell metacharacters. A shell must not split it or run it. 5 * 6 * The tools on PATH are copies of tests/zero_cuda_tool_stub.c. The stub prints a CUDA 7 * line only when that exact file contains a marker, and it appends the argv it received. */ 8 #include "omega_blackwell_submit.h" 9 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 19 static char g_cleanup_dir[128]; 20 21 static int rm_tree_entry(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { 22 (void)sb; 23 (void)typeflag; 24 (void)ftwbuf; 25 return remove(path); 26 } 27 28 static void cleanup_tmpdir(void) { 29 int left; 30 if (!g_cleanup_dir[0]) return; 31 left = chdir("/tmp"); 32 if (left != 0) left = chdir("/"); 33 (void)left; 34 nftw(g_cleanup_dir, rm_tree_entry, 16, FTW_DEPTH | FTW_PHYS); 35 g_cleanup_dir[0] = '\0'; 36 } 37 38 static int g_failed; 39 40 static void expect_int(const char *name, int got, int want) { 41 if (got != want) { 42 fprintf(stderr, "FAIL %s: got %d want %d\n", name, got, want); 43 g_failed++; 44 } else { 45 printf("PASS %s\n", name); 46 } 47 } 48 49 static FILE *open_mode(const char *path, int flags, mode_t mode, const char *fdmode) { 50 int fd = open(path, flags, mode); 51 FILE *f; 52 if (fd < 0) return NULL; 53 f = fdopen(fd, fdmode); 54 if (!f) close(fd); 55 return f; 56 } 57 58 static int write_file(const char *path, const char *text) { 59 FILE *f = open_mode(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600, "w"); 60 if (!f) { 61 fprintf(stderr, "FAIL open %s: %s\n", path, strerror(errno)); 62 return -1; 63 } 64 if (fputs(text, f) < 0) { 65 fclose(f); 66 return -1; 67 } 68 if (fclose(f) != 0) return -1; 69 return 0; 70 } 71 72 static int copy_file(const char *src, const char *dst) { 73 FILE *in = fopen(src, "rb"); 74 FILE *out; 75 char buf[8192]; 76 size_t n; 77 78 if (!in) return -1; 79 out = open_mode(dst, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0700, "wb"); 80 if (!out) { 81 fclose(in); 82 return -1; 83 } 84 while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { 85 if (fwrite(buf, 1, n, out) != n) { 86 fclose(in); 87 fclose(out); 88 return -1; 89 } 90 } 91 fclose(in); 92 if (fclose(out) != 0) return -1; 93 return 0; 94 } 95 96 static int parse_log(const char *path, int *argc_out, char **args, int max_args) { 97 FILE *f = fopen(path, "r"); 98 int argc = 0; 99 int i; 100 101 if (!f) return -1; 102 if (fscanf(f, "argc %d\n", &argc) != 1 || argc < 0 || argc > max_args) { 103 fclose(f); 104 return -1; 105 } 106 for (i = 0; i < argc; i++) { 107 int idx = -1; 108 size_t len = 0; 109 int nl; 110 if (fscanf(f, "arg %d %zu\n", &idx, &len) != 2 || idx != i || len > 4095) { 111 fclose(f); 112 return -1; 113 } 114 args[i] = malloc(len + 1); 115 if (!args[i] || fread(args[i], 1, len, f) != len) { 116 fclose(f); 117 return -1; 118 } 119 args[i][len] = '\0'; 120 nl = fgetc(f); 121 if (nl != '\n') { 122 fclose(f); 123 return -1; 124 } 125 } 126 fclose(f); 127 *argc_out = argc; 128 return 0; 129 } 130 131 static void free_args(char **args, int argc) { 132 int i; 133 for (i = 0; i < argc; i++) free(args[i]); 134 } 135 136 static void expect_argv(const char *name, const char *log_path, int want_argc, const char **want) { 137 char *args[8]; 138 int argc = 0; 139 int i; 140 141 memset(args, 0, sizeof(args)); 142 if (parse_log(log_path, &argc, args, 8) != 0) { 143 fprintf(stderr, "FAIL %s: argv log missing or unreadable\n", name); 144 g_failed++; 145 free_args(args, 8); 146 return; 147 } 148 if (argc != want_argc) { 149 fprintf(stderr, "FAIL %s: argc %d want %d\n", name, argc, want_argc); 150 g_failed++; 151 free_args(args, argc); 152 return; 153 } 154 for (i = 0; i < argc; i++) { 155 if (strcmp(args[i], want[i]) != 0) { 156 fprintf(stderr, "FAIL %s: arg %d got [%s] want [%s]\n", name, i, args[i], want[i]); 157 g_failed++; 158 free_args(args, argc); 159 return; 160 } 161 } 162 printf("PASS %s\n", name); 163 free_args(args, argc); 164 } 165 166 static void reset_log(const char *log_path) { 167 FILE *f = open_mode(log_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600, "w"); 168 if (f) fclose(f); 169 } 170 171 int main(void) { 172 const char *stub = getenv("ZERO_CUDA_TOOL_STUB"); 173 char dir[] = "/tmp/cudapp-argv-XXXXXX"; 174 char bindir[256], workdir[256], log_path[256], path_env[4096]; 175 char ldd_path[300], nm_path[300]; 176 char spaced_cuda[1024], spaced_clean[1024], dollar_path[1024], nm_spaced[1024]; 177 char pwn[512], dollar_pwn[512]; 178 const char *ldd_cuda_argv[2]; 179 const char *ldd_clean_argv[2]; 180 const char *ldd_dollar_argv[2]; 181 const char *nm_spaced_argv[3]; 182 const char *nm_inject = "nosuch; printf ' U cuInit\n'"; 183 const char *nm_inject_argv[3]; 184 const char *old_path; 185 int rc; 186 187 if (!stub || !stub[0]) { 188 fprintf(stderr, "FAIL ZERO_CUDA_TOOL_STUB is unset\n"); 189 return 1; 190 } 191 if (!mkdtemp(dir)) { 192 fprintf(stderr, "FAIL mkdtemp: %s\n", strerror(errno)); 193 return 1; 194 } 195 snprintf(g_cleanup_dir, sizeof(g_cleanup_dir), "%s", dir); 196 atexit(cleanup_tmpdir); 197 snprintf(bindir, sizeof(bindir), "%s/bin", dir); 198 snprintf(workdir, sizeof(workdir), "%s/space dir", dir); 199 snprintf(log_path, sizeof(log_path), "%s/argv.log", dir); 200 snprintf(ldd_path, sizeof(ldd_path), "%s/ldd", bindir); 201 snprintf(nm_path, sizeof(nm_path), "%s/nm", bindir); 202 snprintf(pwn, sizeof(pwn), "%s/pwned", dir); 203 snprintf(dollar_pwn, sizeof(dollar_pwn), "%s/dollar_pwned", dir); 204 snprintf(spaced_cuda, sizeof(spaced_cuda), "%s/lib with cuda", workdir); 205 snprintf(spaced_clean, sizeof(spaced_clean), "%s/ok; touch pwned", workdir); 206 snprintf(dollar_path, sizeof(dollar_path), "%s/$(touch dollar_pwned)", workdir); 207 snprintf(nm_spaced, sizeof(nm_spaced), "%s/has cu symbol", workdir); 208 if (mkdir(bindir, 0755) != 0 || mkdir(workdir, 0755) != 0) { 209 fprintf(stderr, "FAIL mkdir: %s\n", strerror(errno)); 210 return 1; 211 } 212 if (copy_file(stub, ldd_path) != 0 || copy_file(stub, nm_path) != 0) { 213 fprintf(stderr, "FAIL copy stub\n"); 214 return 1; 215 } 216 if (write_file(spaced_cuda, "LIBCUDA_NEEDED\n") != 0 || 217 write_file(spaced_clean, "clean elf stand-in\n") != 0 || 218 write_file(dollar_path, "clean elf stand-in\n") != 0 || 219 write_file(nm_spaced, "CU_SYMBOL\n") != 0) { 220 return 1; 221 } 222 rc = omega_blackwell_verify_zero_libcuda_linkage("/bin/true"); 223 expect_int("real ldd on /bin/true reports no libcuda", rc, 0); 224 rc = omega_blackwell_verify_zero_cuda_symbols("/bin/true"); 225 expect_int("real nm -u on /bin/true reports no cu symbol", rc, 0); 226 227 old_path = getenv("PATH"); 228 if (!old_path) old_path = "/usr/bin:/bin"; 229 snprintf(path_env, sizeof(path_env), "%s:%s", bindir, old_path); 230 if (setenv("PATH", path_env, 1) != 0 || setenv("ZERO_CUDA_ARGV_LOG", log_path, 1) != 0) { 231 fprintf(stderr, "FAIL setenv\n"); 232 return 1; 233 } 234 if (chdir(dir) != 0) { 235 fprintf(stderr, "FAIL chdir %s: %s\n", dir, strerror(errno)); 236 return 1; 237 } 238 239 reset_log(log_path); 240 rc = omega_blackwell_verify_zero_libcuda_linkage(spaced_cuda); 241 expect_int("ldd reads a path with spaces and reports libcuda.so", rc, -1); 242 ldd_cuda_argv[0] = "ldd"; 243 ldd_cuda_argv[1] = spaced_cuda; 244 expect_argv("ldd argv keeps the spaced path", log_path, 2, ldd_cuda_argv); 245 246 reset_log(log_path); 247 rc = omega_blackwell_verify_zero_libcuda_linkage(spaced_clean); 248 expect_int("ldd on a clean file with spaces and a semicolon returns 0", rc, 0); 249 ldd_clean_argv[0] = "ldd"; 250 ldd_clean_argv[1] = spaced_clean; 251 expect_argv("ldd argv keeps the semicolon path", log_path, 2, ldd_clean_argv); 252 expect_int("semicolon path did not run touch", access(pwn, F_OK) == 0, 0); 253 254 reset_log(log_path); 255 rc = omega_blackwell_verify_zero_libcuda_linkage(dollar_path); 256 expect_int("ldd on a clean command-substitution path returns 0", rc, 0); 257 ldd_dollar_argv[0] = "ldd"; 258 ldd_dollar_argv[1] = dollar_path; 259 expect_argv("ldd argv keeps the command-substitution path", log_path, 2, ldd_dollar_argv); 260 expect_int("command substitution did not run touch", access(dollar_pwn, F_OK) == 0, 0); 261 262 reset_log(log_path); 263 rc = omega_blackwell_verify_zero_cuda_symbols(nm_spaced); 264 expect_int("nm reads a path with spaces and reports cuInit", rc, -1); 265 nm_spaced_argv[0] = "nm"; 266 nm_spaced_argv[1] = "-u"; 267 nm_spaced_argv[2] = nm_spaced; 268 expect_argv("nm argv keeps -u and the spaced path", log_path, 3, nm_spaced_argv); 269 270 reset_log(log_path); 271 rc = omega_blackwell_verify_zero_cuda_symbols(nm_inject); 272 expect_int("nm does not treat a printf injection as a CUDA symbol", rc, 0); 273 nm_inject_argv[0] = "nm"; 274 nm_inject_argv[1] = "-u"; 275 nm_inject_argv[2] = nm_inject; 276 expect_argv("nm argv keeps the printf injection as one path", log_path, 3, nm_inject_argv); 277 278 if (g_failed) { 279 fprintf(stderr, "test-zero-cuda-tool-argv: FAIL (%d)\n", g_failed); 280 return 1; 281 } 282 cleanup_tmpdir(); 283 if (access(dir, F_OK) == 0) { 284 fprintf(stderr, "FAIL tmpdir still present: %s\n", dir); 285 return 1; 286 } 287 printf("PASS tmpdir removed\n"); 288 printf("test-zero-cuda-tool-argv: PASS\n"); 289 return 0; 290 }