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/omega-bridge/blob/main/tests/cabi/caller.c # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings 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 `omega-bridge / tests / cabi / caller.c` 183 lines ยท 9238 bytes Raw Blame History 1 /* ORACLE: independent C caller for the omega-bridge cabi-1 fixture library. 2 Test-only, never shipped. Built by tests/cabi/gate.om against the generated header and .so. */ 3 #include "ombrfx.h" 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 11 static int checks, failures; 12 13 static void check(int ok, const char *what) { 14 checks++; 15 if (!ok) { 16 failures++; 17 printf("FAIL %s\n", what); 18 } 19 } 20 21 /* Run fn in a child so a wrapper that writes through a null out pointer is a failed check, not a crashed caller. */ 22 static int isolated(int (*fn)(void)) { 23 fflush(stdout); 24 pid_t p = fork(); 25 if (p < 0) return 0; 26 if (p == 0) _exit(fn() ? 0 : 1); 27 int st = 0; 28 if (waitpid(p, &st, 0) != p) return 0; 29 return WIFEXITED(st) && WEXITSTATUS(st) == 0; 30 } 31 32 static int null_count(void) { return nvmlDeviceGetCount_v2(NULL) == NVML_ERROR_INVALID_ARGUMENT; } 33 static int null_handle(void) { return nvmlDeviceGetHandleByIndex_v2(0, NULL) == NVML_ERROR_INVALID_ARGUMENT; } 34 static int null_name(void) { 35 nvmlDevice_t d; 36 return nvmlDeviceGetHandleByIndex_v2(0, &d) == NVML_SUCCESS && 37 nvmlDeviceGetName(d, NULL, 64) == NVML_ERROR_INVALID_ARGUMENT; 38 } 39 static int null_memory(void) { 40 nvmlDevice_t d; 41 return nvmlDeviceGetHandleByIndex_v2(0, &d) == NVML_SUCCESS && 42 nvmlDeviceGetMemoryInfo(d, NULL) == NVML_ERROR_INVALID_ARGUMENT; 43 } 44 static int null_procs(void) { 45 nvmlDevice_t d; 46 nvmlProcessInfo_t p[2]; 47 return nvmlDeviceGetHandleByIndex_v2(0, &d) == NVML_SUCCESS && 48 nvmlDeviceGetComputeRunningProcesses_v2(d, NULL, p) == NVML_ERROR_INVALID_ARGUMENT; 49 } 50 static int null_bump(void) { return ombrFxBump(NULL) == NVML_ERROR_INVALID_ARGUMENT; } 51 static int null_sum(void) { return ombrFxSum(1, NULL) == NVML_ERROR_INVALID_ARGUMENT; } 52 53 enum { THREADS = 8, ROUNDS = 2000 }; 54 static const char *stable_invalid; 55 static nvmlDevice_t dev0; 56 static int thread_bad[THREADS]; 57 58 static void *worker(void *arg) { 59 int t = (int)(long)arg; 60 char name[64]; 61 for (int i = 0; i < ROUNDS; i++) { 62 unsigned long long v = 0; 63 if (ombrFxBump(&v) != NVML_SUCCESS || v == 0) thread_bad[t]++; 64 if (nvmlErrorString(NVML_ERROR_INVALID_ARGUMENT) != stable_invalid) thread_bad[t]++; 65 if (strcmp(stable_invalid, "Invalid Argument") != 0) thread_bad[t]++; 66 if ((i & 63) == 0) { 67 memset(name, 0, sizeof name); 68 if (nvmlDeviceGetName(dev0, name, sizeof name) != NVML_SUCCESS || 69 strcmp(name, "Omega Fixture GPU 0") != 0) 70 thread_bad[t]++; 71 } 72 } 73 return NULL; 74 } 75 76 int main(int argc, char **argv) { 77 int only_concurrency = argc > 1 && strcmp(argv[1], "concurrency") == 0; 78 if (!only_concurrency) { 79 /* Count: null and out-parameter publication. */ 80 check(isolated(null_count), "count null out refused without commit"); 81 unsigned int count = 0xdeadbeefu; 82 check(nvmlDeviceGetCount_v2(&count) == NVML_SUCCESS, "count status"); 83 check(count == 2, "count published"); 84 check(nvmlDeviceGetCount(&count) == NVML_SUCCESS, "unversioned alias"); 85 86 /* Handles: publication, reuse, out-of-range, null. */ 87 nvmlDevice_t h0 = NULL, h0b = NULL, h1 = NULL, untouched = (nvmlDevice_t)&count; 88 check(nvmlDeviceGetHandleByIndex_v2(0, &h0) == NVML_SUCCESS && h0 != NULL, "handle 0"); 89 check(nvmlDeviceGetHandleByIndex_v2(0, &h0b) == NVML_SUCCESS && h0b == h0, "handle reuse"); 90 check(nvmlDeviceGetHandleByIndex_v2(1, &h1) == NVML_SUCCESS && h1 != NULL && h1 != h0, "handle 1"); 91 check(nvmlDeviceGetHandleByIndex_v2(2, &untouched) == NVML_ERROR_INVALID_ARGUMENT && 92 untouched == (nvmlDevice_t)&count, 93 "out-of-range index refused without commit"); 94 check(isolated(null_handle), "handle null out refused"); 95 96 /* Names: out char with length, short buffer, null, forged handles. */ 97 char name[64], small[5]; 98 memset(name, 'x', sizeof name); 99 check(nvmlDeviceGetName(h0, name, sizeof name) == NVML_SUCCESS && strcmp(name, "Omega Fixture GPU 0") == 0, 100 "name 0"); 101 check(nvmlDeviceGetName(h1, name, sizeof name) == NVML_SUCCESS && strcmp(name, "Omega Fixture GPU 1") == 0, 102 "name 1"); 103 memset(small, 'y', sizeof small); 104 check(nvmlDeviceGetName(h0, small, sizeof small) == NVML_ERROR_INSUFFICIENT_SIZE && 105 memcmp(small, "yyyyy", 5) == 0, 106 "short name buffer refused without commit"); 107 check(isolated(null_name), "name null out refused"); 108 nvmlDevice_t forged[4] = {NULL, (nvmlDevice_t)name, (nvmlDevice_t)((char *)h0 + 1), 109 (nvmlDevice_t)((char *)h1 + 2 * ((char *)h1 - (char *)h0))}; 110 for (int i = 0; i < 4; i++) { 111 memset(name, 'z', sizeof name); 112 check(nvmlDeviceGetName(forged[i], name, sizeof name) == NVML_ERROR_INVALID_ARGUMENT && name[0] == 'z', 113 "forged handle refused"); 114 } 115 116 /* Records. */ 117 nvmlMemory_t m; 118 memset(&m, 0, sizeof m); 119 check(nvmlDeviceGetMemoryInfo(h1, &m) == NVML_SUCCESS, "memory status"); 120 unsigned long long total = 2ull * 1073741824ull, used = 2ull * 4096ull; 121 check(m.total == total && m.used == used && m.free == total - used, "memory record fields"); 122 check(isolated(null_memory), "memory null out refused"); 123 124 nvmlProcessInfo_t p[5]; 125 memset(p, 0xa5, sizeof p); 126 unsigned int n = 0; 127 check(nvmlDeviceGetComputeRunningProcesses_v2(h1, &n, p) == NVML_ERROR_INSUFFICIENT_SIZE && n == 2, 128 "process count probe publishes required count"); 129 unsigned char guard[sizeof p]; 130 memset(guard, 0xa5, sizeof guard); 131 check(memcmp(p, guard, sizeof p) == 0, "probe leaves records untouched"); 132 n = 2; 133 check(nvmlDeviceGetComputeRunningProcesses_v2(h1, &n, NULL) == NVML_ERROR_INVALID_ARGUMENT, 134 "null record array refused"); 135 n = 5; 136 check(nvmlDeviceGetComputeRunningProcesses_v2(h1, &n, p) == NVML_SUCCESS && n == 2, "process list"); 137 for (unsigned int i = 0; i < 2; i++) 138 check(p[i].pid == 1010 + i && p[i].usedGpuMemory == (i + 1ull) * 1048576ull && p[i].gpuInstanceId == 7 && 139 p[i].computeInstanceId == 9, 140 "process record fields"); 141 check(memcmp(&p[2], guard, 3 * sizeof p[0]) == 0, "records past the published count untouched"); 142 check(isolated(null_procs), "process null count refused"); 143 144 /* Stable strings. */ 145 const char *s0 = nvmlErrorString(NVML_SUCCESS), *s2 = nvmlErrorString(NVML_ERROR_INVALID_ARGUMENT); 146 check(strcmp(s0, "Success") == 0, "error string success"); 147 check(strcmp(s2, "Invalid Argument") == 0, "error string invalid"); 148 check(strcmp(nvmlErrorString(NVML_ERROR_INSUFFICIENT_SIZE), "Insufficient Size") == 0, "error string size"); 149 check(strcmp(nvmlErrorString(NVML_ERROR_UNKNOWN), "Unknown Error") == 0, "error string fallback"); 150 check(strcmp(nvmlErrorString((nvmlReturn_t)-5), "Unknown Error") == 0, "error string negative fallback"); 151 check(nvmlErrorString(NVML_SUCCESS) == s0 && nvmlErrorString(NVML_ERROR_INVALID_ARGUMENT) == s2 && 152 strcmp(s0, "Success") == 0, 153 "error string pointers stable"); 154 155 /* Import of a separately generated Omega unit with a uchar view span, a ulong edit span at element 156 offset 0 and a ulong view span at element offset 3 of {1,...,6}: k=3, 4+5+6 read, three 7s written back. */ 157 unsigned long long sum = 0, want = 5 + 3 + 15 + 21; 158 for (unsigned int j = 0; j < 512; j++) want += j & 255u; 159 check(ombrFxSum(5, &sum) == NVML_SUCCESS && sum == want, "import through omega_fx_sum with uchar and ulong spans"); 160 check(isolated(null_sum), "sum null out refused"); 161 check(isolated(null_bump), "bump null out refused"); 162 } 163 164 /* Concurrency: every caller serialises through FxLock; lost updates mean no lock. */ 165 unsigned long long before = 0, after = 0; 166 check(ombrFxBump(&before) == NVML_SUCCESS, "bump before"); 167 check(nvmlDeviceGetHandleByIndex_v2(0, &dev0) == NVML_SUCCESS, "worker handle"); 168 stable_invalid = nvmlErrorString(NVML_ERROR_INVALID_ARGUMENT); 169 pthread_t th[THREADS]; 170 for (long t = 0; t < THREADS; t++) check(pthread_create(&th[t], NULL, worker, (void *)t) == 0, "thread start"); 171 for (int t = 0; t < THREADS; t++) { 172 pthread_join(th[t], NULL); 173 check(thread_bad[t] == 0, "concurrent results and string pointers"); 174 } 175 check(ombrFxBump(&after) == NVML_SUCCESS, "bump after"); 176 unsigned long long lost = before + (unsigned long long)THREADS * ROUNDS + 1 - after; 177 printf("concurrency before=%llu after=%llu expected=%llu lost=%llu\n", before, after, 178 before + (unsigned long long)THREADS * ROUNDS + 1, lost); 179 check(lost == 0, "no lost updates under concurrent calls"); 180 181 printf("%s checks=%d failures=%d\n", failures ? "CALLER FAIL" : "CALLER PASS", checks, failures); 182 return failures ? 1 : 0; 183 }