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 / test_omega_gpu_platform_linux.c

625 lines · 35275 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* test_omega_gpu_platform_linux: the Linux REFERENCE platform (src/omega_gpu_platform_linux.c, reference only) on the
3 * simulated driver (tests/fake_m16_native.c). Host program, hosted build, opens no device.
4 *
5 * What it checks: the reference claims exactly the session service set (no MMIO, no IRQ: polling is never claimed as
6 * an interrupt) and passes the contract check; the hosted session installs it by default; the session makes the same
7 * driver calls in the same order as before P07 cut C (channel, then the four session buffers through
8 * nvrm_alloc_gpu_uncached, one submit with the C3 tail, both marker waits, close); the reference refuses a GPU-cached
9 * buffer, a poll of a buffer that is not HOST_POLLED, freeing the live queue ring, an oversize submit and every handle
10 * of a closed device; the driver's own failure text survives an open failure; a marker that never lands latches the
11 * session, which then frees nothing and closes nothing. Each case runs in its own child process.
12 */
13#include "fake_m16_native.h"
14#include "nvrm.h"
15#include "omega_blackwell_submit.h"
16#include "omega_gpu_cmdlist.h"
17#include "omega_gpu_mem_cached.h"
18#include "omega_gpu_platform_linux.h"
19#include "omega_gpu_session.h"
20#include "cudapp_plat_effects.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/wait.h>
27#include <unistd.h>
28#include "test_out_file.h"
29
30/* The three driver symbols the session path needs that the fake (written for the vector engine) lacks. */
31static int g_open_fail;
32static int g_discrete;
33extern uint64_t fake_vram_free, fake_vram_total;
34extern int fake_vram_query_fail, fake_vram_allocs;
35int nvrm_open(Nvrm *rm) {
36 if (g_open_fail) { snprintf(rm->err, sizeof rm->err, "fake RM: open refused"); return -1; }
37 rm->target_sm = g_discrete ? 86 : 121; rm->discrete = (uint8_t)g_discrete;
38 fk.device_open = 1; return 0;
39}
40int nvrm_close(Nvrm *rm) { (void)rm; return 0; }
41static int g_retires;
42void nvrm_retire(Nvrm *rm, uint32_t upto) { (void)rm; (void)upto; g_retires++; }
43
44static unsigned g_map_calls, g_map_bad_args;
45uint32_t __real_omega2_ctx_plat_wait_ge(omega_native_context *, uint64_t *,
46 uint64_t, uint64_t, uint64_t, uint64_t);
47uint32_t __wrap_omega2_ctx_plat_wait_ge(omega_native_context *context, uint64_t *result,
48 uint64_t handle, uint64_t off, uint64_t want, uint64_t polls) {
49 ++g_map_calls;
50 if (!context || !handle || polls != 1) ++g_map_bad_args;
51 return __real_omega2_ctx_plat_wait_ge(context, result, handle, off, want, polls);
52}
53
54static int case_ok;
55#define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0)
56
57static const uint32_t ARGS[4] = {1, 2, 3, 4};
58static OmegaGpuLaunch launch_desc(uint64_t timeout_ms) {
59 OmegaGpuLaunch L = { .code_va = 0x200000000ull, .gpr_count = 64, .threads_x = 32, .grid_x = 1, .grid_y = 1,
60 .num_elements = 32, .args = ARGS, .n_args = 4, .timeout_ms = timeout_ms };
61 return L;
62}
63
64static void c_caps(void) {
65 OmegaGpuPlatformCaps caps;
66 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
67 EXPECT(p != NULL);
68 EXPECT(caps.services == OMEGA_GPU_SVC_REQUIRED_SESSION);
69 EXPECT(!(caps.services & OMEGA_GPU_SVC_IRQ) && !(caps.services & OMEGA_GPU_SVC_MMIO));
70 EXPECT(p->irq_wait == NULL && p->mmio_read32 == NULL && p->mmio_write32 == NULL);
71 EXPECT(omega_gpu_platform_check_for(p, &caps, OMEGA_GPU_SVC_REQUIRED_SESSION) == OMEGA_GPU_PLAT_OK);
72 EXPECT(caps.abi_major == 1 && caps.abi_minor == 2);
73}
74
75static void c_default_install(void) {
76 fake_reset();
77 EXPECT(omega_gpu_session_platform() == omega_gpu_platform_linux(NULL)); /* hosted: the reference by default */
78 EXPECT(omega_gpu_session_completion_mode() == OMEGA_GPU_COMPLETION_POLLING);
79}
80
81static void c_same_driver_calls(void) {
82 fake_reset();
83 fk.kernel_mode = FK_KERNEL_SKIP;
84 omega_gpu_session_lock();
85 OmegaGpuSession *s = omega_gpu_session_open();
86 EXPECT(s != NULL);
87 if (!s) { printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); return; }
88 EXPECT(fk.channel_calls == 1 && fk.uncached_calls == 4 && fk.alloc_calls == 4 && fk.live == 4);
89 const int ch = fake_nth(FK_EV_CHANNEL, 1), a1 = fake_nth(FK_EV_ALLOC, 1);
90 EXPECT(ch >= 0 && a1 > ch); /* channel first, then the session buffers */
91 EXPECT(fk.ev[fake_nth(FK_EV_ALLOC, 1)].a == 0x10000 && fk.ev[fake_nth(FK_EV_ALLOC, 2)].a == 0x1000 &&
92 fk.ev[fake_nth(FK_EV_ALLOC, 3)].a == 0x1000 && fk.ev[fake_nth(FK_EV_ALLOC, 4)].a == 0x10000);
93 OmegaGpuLaunch L = launch_desc(1000);
94 uint32_t mk = 0;
95 EXPECT(omega_gpu_session_launch(&L, NULL, &mk) == 0);
96 EXPECT(fk.submit_calls == 1 && fk.pb_mem_size == 0x10000);
97 EXPECT(fake_count(FK_EV_PB_RELEASE) == 2 && fake_count(FK_EV_PB_FLUSH) == 1); /* the C3 tail */
98 EXPECT(fk.marker1_va == s->marker.va && fk.marker2_va == s->marker.va + 0x10);
99 EXPECT(fk.sem_va == s->qmd.va + 0x2000 && fk.sem_init_value == OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT);
100 EXPECT(mk == OMEGA_BW_MARKER_COMPLETION_PAYLOAD && g_retires == 1);
101 omega_gpu_session_unlock();
102 omega_gpu_session_close();
103 EXPECT(fk.free_calls == 4 && fk.close_calls == 1 && fk.live_at_close == 0 && fk.double_free == 0);
104}
105
106static void c_reference_refusals(void) {
107 fake_reset();
108 OmegaGpuPlatformCaps caps;
109 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
110 OmegaGpuDeviceHandle d;
111 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK);
112 OmegaGpuDeviceHandle d2;
113 EXPECT(p->device_open(p->ctx, 0, &d2) == OMEGA_GPU_PLAT_REFUSED_LIMIT); /* one device context */
114 OmegaGpuBufferDesc cached = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
115 .gpu_cache = OMEGA_GPU_CACHE_GPU_CACHED, .bytes = 4096};
116 OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus;
117 const int allocs0 = fk.alloc_calls;
118 EXPECT(p->dma_alloc(p->ctx, d, &cached, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY);
119 cached.rights |= OMEGA_GPU_RIGHT_HOST_POLLED; /* polled and cached: the contract refuses it too */
120 EXPECT(p->dma_alloc(p->ctx, d, &cached, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_CACHE_POLICY);
121 EXPECT(fk.alloc_calls == allocs0); /* refused before the driver */
122 OmegaGpuBufferDesc data = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
123 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
124 EXPECT(p->dma_alloc(p->ctx, d, &data, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
125 EXPECT(cpu.ptr != NULL && cpu.len >= 4096 && bus.iova == 0);
126 uint32_t seen = 0;
127 EXPECT(p->poll_word(p->ctx, h, 0, 1, 1, &seen) == OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS); /* not HOST_POLLED */
128 OmegaGpuBufferDesc ring = {.kind = OMEGA_GPU_HANDLE_BUF_QUEUE, .rights = OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ,
129 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
130 OmegaGpuBufferHandle rh; OmegaGpuQueueHandle q;
131 EXPECT(p->dma_alloc(p->ctx, d, &ring, &rh, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
132 EXPECT(p->queue_open(p->ctx, d, h, &q) == OMEGA_GPU_PLAT_REFUSED_BAD_KIND); /* a DATA buffer is not a ring */
133 EXPECT(p->queue_open(p->ctx, d, rh, &q) == OMEGA_GPU_PLAT_OK);
134 EXPECT(p->dma_free(p->ctx, rh) == OMEGA_GPU_PLAT_REFUSED_BUSY); /* the open queue still uses it */
135 static uint32_t big[2048];
136 EXPECT(p->queue_submit(p->ctx, q, big, 2048) == OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH); /* 8 KiB into a 4 KiB ring */
137 EXPECT(fk.submit_calls == 0);
138 const int frees0 = fk.free_calls;
139 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK);
140 /* every handle of the closed device is stale */
141 EXPECT(p->dma_free(p->ctx, h) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
142 EXPECT(p->sync_cpu_to_gpu(p->ctx, h, 0, 4) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
143 EXPECT(p->queue_submit(p->ctx, q, big, 4) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
144 EXPECT(p->dma_alloc(p->ctx, d, &data, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE);
145 EXPECT(fk.free_calls == frees0);
146 EXPECT(strstr(p->diag_text(p->ctx), "STALE") != NULL);
147}
148
149static void c_open_failure_text(void) {
150 fake_reset();
151 fk.channel_fail = 1;
152 omega_gpu_session_lock();
153 EXPECT(omega_gpu_session_open() == NULL);
154 EXPECT(strncmp(omega_gpu_session_last_error(), "m16_native_create_channel: ", 27) == 0);
155 EXPECT(strstr(omega_gpu_session_last_error(), FK_TEXT) != NULL); /* the driver's own text survives */
156 fk.channel_fail = 0;
157 g_open_fail = 1;
158 EXPECT(omega_gpu_session_open() == NULL);
159 EXPECT(strcmp(omega_gpu_session_last_error(), "nvrm_open: fake RM: open refused") == 0);
160 g_open_fail = 0;
161 EXPECT(omega_gpu_session_open() != NULL && !omega_gpu_session_is_blocked());
162 omega_gpu_session_unlock();
163 omega_gpu_session_close();
164}
165
166static void c_marker_never_latches(void) {
167 fake_reset();
168 fk.kernel_mode = FK_KERNEL_SKIP;
169 fk.marker_mode = FK_SYNC_NEVER;
170 omega_gpu_session_lock();
171 EXPECT(omega_gpu_session_open() != NULL);
172 OmegaGpuLaunch L = launch_desc(5);
173 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1);
174 EXPECT(omega_gpu_session_is_blocked());
175 EXPECT(g_map_calls > 0 && g_map_bad_args == 0);
176 EXPECT(fk.wait_calls == 0); /* Real memfd capture must use Omega mapget, not the C fallback. */
177 EXPECT(g_retires == 0);
178 omega_gpu_session_unlock();
179 omega_gpu_session_close();
180 EXPECT(fk.free_calls == 0 && fk.close_calls == 0 && fk.live == 4); /* parked: nothing released, nothing closed */
181 OmegaGpuParkStats ps; omega_gpu_session_parked(&ps);
182 EXPECT(ps.buffers == 4);
183}
184
185/* omega #457: the L2_SYSMEM_NCOH_INVALIDATE prefix, MEM_OP_A..D = 0, 0, 0, 0x11 << 27 (clc96f.h:30-73). The fake logs
186 * every MEM_OP as FK_EV_PB_FLUSH with a = the operation word. */
187#define L2_INVAL_OP (0x11u << 27)
188static int count_l2_inval(void) {
189 int n = 0;
190 for (int i = 0; i < fk.nev; i++) n += fk.ev[i].kind == FK_EV_PB_FLUSH && fk.ev[i].a == L2_INVAL_OP;
191 return n;
192}
193
194static void c_l2_inval_prefix_once(void) {
195 fake_reset();
196 fk.kernel_mode = FK_KERNEL_SKIP;
197 OmegaGpuPlatformCaps caps;
198 (void)omega_gpu_platform_linux(&caps);
199 EXPECT(caps.submit_prefix_words == 5u); /* the cost the platform publishes is the prefix it writes */
200 omega_gpu_session_lock();
201 OmegaGpuSession *s = omega_gpu_session_open();
202 EXPECT(s != NULL);
203 if (!s) { omega_gpu_session_unlock(); return; }
204 const OmegaGpuPlatform *p = omega_gpu_session_platform();
205 OmegaGpuMem cached;
206 EXPECT(omega_gpu_session_alloc_cached(4096, &cached) == 0);
207 OmegaGpuLaunch L = launch_desc(1000);
208 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
209 EXPECT(count_l2_inval() == 0); /* no host write into a GPU-cached buffer yet: no prefix */
210 const size_t plain = fk.pb_len;
211 EXPECT(p->sync_cpu_to_gpu(p->ctx, s->marker.handle, 0, 4) == OMEGA_GPU_PLAT_OK); /* an uncached buffer: no prefix */
212 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
213 EXPECT(count_l2_inval() == 0 && fk.pb_len == plain);
214 EXPECT(p->sync_cpu_to_gpu(p->ctx, cached.handle, 0, 4096) == OMEGA_GPU_PLAT_OK);
215 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
216 EXPECT(count_l2_inval() == 1); /* exactly once, at the head of the next submission */
217 EXPECT(fk.pb_len == plain + 5u);
218 EXPECT(fk.pb[0] == ((2u << 28) | (4u << 16) | (0x0028u >> 2)) && fk.pb[1] == 0 && fk.pb[2] == 0 && fk.pb[3] == 0 &&
219 fk.pb[4] == L2_INVAL_OP);
220 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
221 EXPECT(count_l2_inval() == 1 && fk.pb_len == plain); /* consumed: the launch after it has none */
222 EXPECT(p->sync_gpu_to_cpu(p->ctx, cached.handle, 0, 4096) == OMEGA_GPU_PLAT_OK); /* GPU to CPU is no host write */
223 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
224 EXPECT(count_l2_inval() == 1 && fk.pb_len == plain);
225 EXPECT(fk.pb_parse_error == 0 && fk.ev_overflow == 0);
226 omega_gpu_session_unlock();
227}
228
229/* Fable review of omega #459: a stamped standalone list of >= 1167 launches filled one submission to 16381 words; with
230 * the 5-word prefix pending that is 16386 > 16384 ring words, refused BAD_LENGTH on every retry. The command list must
231 * budget the prefix the platform publishes (caps.submit_prefix_words). */
232/* 2048 launches (about 14 ring words each) fill the 16384-word ring so the list splits into exactly 2 submissions.
233 * OMEGA_GPU_CMDLIST_MAX_LAUNCHES is 4096 since L312, which would split into 4, so the count is pinned here. */
234#define NEAR_CAP_LAUNCHES 2048u
235static void c_cmdlist_near_cap_with_l2_inval_pending(void) {
236 fake_reset();
237 fk.kernel_mode = FK_KERNEL_SKIP;
238 fk.alloc_max = 4u << 20; /* the 2048-launch bank and QMD buffers are 2 MiB each */
239 OmegaGpuCmdList *cl = NULL;
240 EXPECT(omega_gpu_cmdlist_create(NEAR_CAP_LAUNCHES, &cl) == 0);
241 if (!cl) { printf(" %s\n", omega_gpu_session_last_error()); return; }
242 omega_gpu_session_lock();
243 OmegaGpuLaunch L = launch_desc(1000);
244 for (uint32_t i = 0; i < NEAR_CAP_LAUNCHES; i++) EXPECT(omega_gpu_cmdlist_add(cl, &L) == (int)i);
245 const OmegaGpuPlatform *p = omega_gpu_session_platform();
246 OmegaGpuMem cached;
247 EXPECT(omega_gpu_session_alloc_cached(4096, &cached) == 0);
248 EXPECT(p->sync_cpu_to_gpu(p->ctx, cached.handle, 0, 4096) == OMEGA_GPU_PLAT_OK); /* the prefix is now pending */
249 const int submits0 = fk.submit_calls;
250 OmegaGpuCmdListOpts o = { .mode = OMEGA_GPU_CMDLIST_PCAS, .barrier = OMEGA_GPU_CMDLIST_BARRIER_WFI, .timeout_ms = 1000, .stamps = 1 };
251 OmegaGpuCmdListStats st;
252 const int rc = omega_gpu_cmdlist_submit(cl, &o, &st);
253 if (rc != 0) printf(" cmdlist submit: %s (platform: %s)\n", omega_gpu_session_last_error(), p->diag_text(p->ctx));
254 EXPECT(rc == 0);
255 EXPECT(st.sems_done == NEAR_CAP_LAUNCHES && st.first_missing == -1);
256 EXPECT(st.submits == 2 && fk.submit_calls - submits0 == 2);
257 EXPECT(count_l2_inval() == 1); /* the first submission only */
258 for (int i = 0; i < fk.nev; i++)
259 if (fk.ev[i].kind == FK_EV_SUBMIT) EXPECT(fk.ev[i].a * 4u <= fk.pb_mem_size); /* prefix included, inside the ring */
260 EXPECT(fk.pb_parse_error == 0 && fk.ev_overflow == 0);
261 EXPECT(!omega_gpu_session_is_blocked() && !omega_gpu_cmdlist_is_latched());
262 omega_gpu_session_unlock();
263}
264
265/* Developer bar D1 (docs/PAIN_POINTS.md): on the GB10 every CUDA++ buffer is the machine's own memory, and running
266 * it out freezes the whole box. The reference refuses an allocation that would leave less than the reserve free,
267 * before the driver is called, with a sentence that names the bytes asked, the bytes free and the setting.
268 * omega_gpu_platform_linux_test_set_meminfo points the platform at a fake /proc/meminfo; CUDAPP_MEM_RESERVE_MIB sets the
269 * reserve (0 = off). CUDAPP_MEMINFO is ignored. */
270static void write_meminfo(const char *path, unsigned long long avail_kb) {
271 FILE *f = test_out_open(path);
272 if (!f) { case_ok = 0; return; }
273 fprintf(f, "MemTotal: 127535484 kB\nMemFree: 100000 kB\nMemAvailable: %llu kB\n", avail_kb);
274 fclose(f);
275}
276static void c_memory_reserve(void) {
277 fake_reset();
278 fk.alloc_max = 8u << 20; /* the fake driver may hand out up to 8 MiB, so only our check can refuse */
279 char path[] = "/tmp/cudapp-meminfo-XXXXXX";
280 int fd = mkstemp(path); if (fd >= 0) close(fd);
281 write_meminfo(path, 4097ull * 1024ull); /* 4097 MiB free: 1 MiB above the default 4096 MiB reserve */
282 EXPECT(omega_gpu_platform_linux_test_set_meminfo(path) == 0);
283 unsetenv("CUDAPP_MEM_RESERVE_MIB");
284 OmegaGpuPlatformCaps caps;
285 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
286 OmegaGpuDeviceHandle d;
287 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK);
288 OmegaGpuBufferDesc small = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
289 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
290 OmegaGpuBufferDesc big = small; big.bytes = 2u << 20;
291 OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus;
292 EXPECT(p->dma_alloc(p->ctx, d, &small, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK); /* leaves more than the reserve */
293 const int allocs0 = fk.alloc_calls;
294 EXPECT(p->dma_alloc(p->ctx, d, &big, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_LIMIT);
295 EXPECT(fk.alloc_calls == allocs0); /* refused before the driver */
296 const char *t = p->diag_text(p->ctx);
297 EXPECT(strstr(t, "dma_alloc: refused 2.0 MiB") != NULL);
298 EXPECT(strstr(t, "4.0 GiB of memory is free") != NULL);
299 EXPECT(strstr(t, "CUDAPP_MEM_RESERVE_MIB=4096") != NULL);
300 EXPECT(strstr(t, "freezes the machine") != NULL);
301 printf(" refusal: %s\n", t);
302 setenv("CUDAPP_MEM_RESERVE_MIB", "0", 1); /* the reserve can be turned off */
303 EXPECT(p->dma_alloc(p->ctx, d, &big, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
304 setenv("CUDAPP_MEM_RESERVE_MIB", "1", 1); /* and set: 1 MiB reserve, 2 MiB fits in 4097 MiB */
305 EXPECT(p->dma_alloc(p->ctx, d, &big, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
306 unsetenv("CUDAPP_MEM_RESERVE_MIB");
307 /* A missing test path falls back to /proc/meminfo; that case is D1_MEMINFO_TEST_PATH_REGULAR_ENV_IGNORED. */
308 EXPECT(omega_gpu_platform_linux_test_set_meminfo("") == 0);
309 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK);
310 /* through the session, the same sentence reaches omega_gpu_session_last_error() */
311 EXPECT(omega_gpu_platform_linux_test_set_meminfo(path) == 0);
312 omega_gpu_session_lock();
313 EXPECT(omega_gpu_session_open() != NULL);
314 OmegaGpuMem m;
315 EXPECT(omega_gpu_session_alloc(2u << 20, &m) != 0);
316 EXPECT(strstr(omega_gpu_session_last_error(), "dma_alloc: refused 2.0 MiB") != NULL);
317 printf(" session: %s\n", omega_gpu_session_last_error());
318 EXPECT(omega_gpu_session_alloc(4096, &m) == 0);
319 omega_gpu_session_unlock();
320 unlink(path);
321}
322
323/* Audit Q6 (CudaAuditFable REPORT.md): caps.max_inflight is 1, so the reference refuses a second submission while
324 * the first is unretired, before the ring is overwritten or the doorbell rung; queue_retire then releases it. */
325static void c_submit_refused_while_unretired(void) {
326 fake_reset();
327 fk.kernel_mode = FK_KERNEL_SKIP;
328 OmegaGpuPlatformCaps caps;
329 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
330 EXPECT(caps.max_inflight == 1u);
331 omega_gpu_session_lock();
332 OmegaGpuSession *s = omega_gpu_session_open();
333 EXPECT(s != NULL);
334 if (!s) { omega_gpu_session_unlock(); return; }
335 static const uint32_t NOP[2] = { 0, 0 };
336 const int submits0 = fk.submit_calls, retires0 = g_retires;
337 EXPECT(p->queue_submit(p->ctx, s->queue, NOP, 2) == OMEGA_GPU_PLAT_OK);
338 EXPECT(p->queue_submit(p->ctx, s->queue, NOP, 2) == OMEGA_GPU_PLAT_REFUSED_BUSY); /* first one not retired */
339 EXPECT(fk.submit_calls - submits0 == 1); /* the refusal reached no driver call: no copy, no doorbell */
340 EXPECT(strstr(p->diag_text(p->ctx), "queue_submit") != NULL);
341 EXPECT(p->queue_retire(p->ctx, s->queue) == OMEGA_GPU_PLAT_OK);
342 EXPECT(g_retires - retires0 == 1);
343 EXPECT(p->queue_retire(p->ctx, s->queue) == OMEGA_GPU_PLAT_OK); /* nothing in flight: no driver retire */
344 EXPECT(g_retires - retires0 == 1);
345 EXPECT(p->queue_submit(p->ctx, s->queue, NOP, 2) == OMEGA_GPU_PLAT_OK);
346 EXPECT(fk.submit_calls - submits0 == 2);
347 EXPECT(p->queue_retire(p->ctx, s->queue) == OMEGA_GPU_PLAT_OK);
348 OmegaGpuLaunch L = launch_desc(1000); /* the session's own launch path still runs submit, wait, retire */
349 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
350 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
351 omega_gpu_session_unlock();
352}
353
354/* Audit Q6 on an L136 extra queue: its ring is also rewritten from offset 0, and caps.max_inflight is per queue, so
355 * a second submission on the extra queue while the first is unretired is refused REFUSED_BUSY before any copy or
356 * doorbell; the two queues' in-flight counts are independent; queue_retire on the extra queue releases it. */
357static void c_extra_queue_submit_refused_while_unretired(void) {
358 fake_reset();
359 fk.kernel_mode = FK_KERNEL_SKIP;
360 fk.extra_channels = 1;
361 setenv("AIEN_OMEGA_GPU_CHANNELS", "2", 1);
362 OmegaGpuPlatformCaps caps;
363 const OmegaGpuPlatform *p = omega_gpu_platform_linux(&caps);
364 omega_gpu_session_lock();
365 OmegaGpuSession *s = omega_gpu_session_open();
366 EXPECT(s != NULL);
367 OmegaGpuQueueHandle q1;
368 const int xq = s ? omega_gpu_session_extra_queue(1, &q1) : -1;
369 EXPECT(xq == 0);
370 if (!s || xq != 0) { printf(" session: %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); return; }
371 static const uint32_t NOP[2] = { 0, 0 };
372 const int submits0 = fk.submit_calls;
373 EXPECT(p->queue_submit(p->ctx, q1, NOP, 2) == OMEGA_GPU_PLAT_OK);
374 EXPECT(fk.chan_submit_calls == 1);
375 EXPECT(p->queue_submit(p->ctx, q1, NOP, 2) == OMEGA_GPU_PLAT_REFUSED_BUSY); /* first one not retired */
376 EXPECT(fk.chan_submit_calls == 1); /* the refusal reached no driver call: no copy, no doorbell */
377 EXPECT(strstr(p->diag_text(p->ctx), "queue_submit") != NULL);
378 EXPECT(p->queue_submit(p->ctx, s->queue, NOP, 2) == OMEGA_GPU_PLAT_OK); /* queue 0 is independent */
379 EXPECT(p->queue_submit(p->ctx, s->queue, NOP, 2) == OMEGA_GPU_PLAT_REFUSED_BUSY);
380 EXPECT(fk.submit_calls - submits0 == 1);
381 EXPECT(p->queue_retire(p->ctx, s->queue) == OMEGA_GPU_PLAT_OK);
382 EXPECT(p->queue_submit(p->ctx, q1, NOP, 2) == OMEGA_GPU_PLAT_REFUSED_BUSY); /* retiring queue 0 does not free q1 */
383 EXPECT(p->queue_retire(p->ctx, q1) == OMEGA_GPU_PLAT_OK);
384 EXPECT(fk.chan_retire_calls == 1);
385 EXPECT(p->queue_retire(p->ctx, q1) == OMEGA_GPU_PLAT_OK); /* nothing in flight: no driver retire */
386 EXPECT(fk.chan_retire_calls == 1);
387 EXPECT(p->queue_submit(p->ctx, q1, NOP, 2) == OMEGA_GPU_PLAT_OK);
388 EXPECT(fk.chan_submit_calls == 2);
389 EXPECT(p->queue_retire(p->ctx, q1) == OMEGA_GPU_PLAT_OK);
390 omega_gpu_session_unlock();
391}
392
393/* Audit Q6 / top-10 item 4: a command list whose completion is uncertain must latch the process without queueing
394 * anything behind the stuck list. Before the fix force_latch launched a 1 ms nop, which copied its methods over the
395 * ring the stuck list may still be reading and rang a second doorbell. */
396static void c_cmdlist_timeout_latches_without_launch(void) {
397 fake_reset();
398 fk.kernel_mode = FK_KERNEL_SKIP;
399 OmegaGpuCmdList *cl = NULL;
400 EXPECT(omega_gpu_cmdlist_create(4, &cl) == 0);
401 if (!cl) { printf(" %s\n", omega_gpu_session_last_error()); return; }
402 omega_gpu_session_lock();
403 OmegaGpuLaunch L = launch_desc(1000);
404 EXPECT(omega_gpu_cmdlist_add(cl, &L) == 0);
405 fk.marker_mode = FK_SYNC_NEVER;
406 const int submits0 = fk.submit_calls, retires0 = g_retires;
407 OmegaGpuCmdListOpts o = { .mode = OMEGA_GPU_CMDLIST_PCAS, .barrier = OMEGA_GPU_CMDLIST_BARRIER_WFI, .timeout_ms = 5 };
408 OmegaGpuCmdListStats st;
409 EXPECT(omega_gpu_cmdlist_submit(cl, &o, &st) == -1);
410 printf(" submits %d, error: %s\n", fk.submit_calls - submits0, omega_gpu_session_last_error());
411 EXPECT(fk.submit_calls - submits0 == 1); /* the stuck list only: nothing queued behind it */
412 EXPECT(g_retires == retires0);
413 EXPECT(omega_gpu_cmdlist_is_latched());
414 EXPECT(omega_gpu_session_is_blocked());
415 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1); /* every later launch is refused */
416 EXPECT(fk.submit_calls - submits0 == 1);
417 omega_gpu_session_unlock();
418}
419
420/* Audit F15: timeout_ms 0 made the session submit and then latch after a single poll. It is refused as an argument,
421 * before anything is submitted, and the session stays usable. */
422static void c_zero_timeout_refused(void) {
423 fake_reset();
424 fk.kernel_mode = FK_KERNEL_SKIP;
425 omega_gpu_session_lock();
426 EXPECT(omega_gpu_session_open() != NULL);
427 const int submits0 = fk.submit_calls;
428 OmegaGpuLaunch L = launch_desc(0);
429 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1);
430 EXPECT(fk.submit_calls == submits0);
431 EXPECT(!omega_gpu_session_is_blocked());
432 EXPECT(strcmp(omega_gpu_session_last_error(), "launch arguments") == 0);
433 L = launch_desc(1000);
434 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
435 omega_gpu_session_unlock();
436}
437
438/* Audit F9: CUDAPP_MEM_RESERVE_MIB = 2^44 shifted left by 20 is 2^64, which wrapped to a 0 reserve and allowed
439 * everything; a MemAvailable past 2^54 kB wrapped the same way and refused everything. Both saturate. */
440static void c_reserve_overflow(void) {
441 fake_reset();
442 fk.alloc_max = 8u << 20;
443 char path[] = "/tmp/cudapp-meminfo-XXXXXX";
444 int fd = mkstemp(path); if (fd >= 0) close(fd);
445 write_meminfo(path, 4097ull * 1024ull);
446 EXPECT(omega_gpu_platform_linux_test_set_meminfo(path) == 0);
447 setenv("CUDAPP_MEM_RESERVE_MIB", "17592186044416", 1); /* 2^44 MiB */
448 const OmegaGpuPlatform *p = omega_gpu_platform_linux(NULL);
449 OmegaGpuDeviceHandle d;
450 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK);
451 OmegaGpuBufferDesc small = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
452 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
453 OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus;
454 const int allocs0 = fk.alloc_calls;
455 EXPECT(p->dma_alloc(p->ctx, d, &small, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_REFUSED_LIMIT);
456 EXPECT(fk.alloc_calls == allocs0);
457 printf(" refusal: %s\n", p->diag_text(p->ctx));
458 unsetenv("CUDAPP_MEM_RESERVE_MIB");
459 write_meminfo(path, 18014398509481984ull); /* 2^54 kB: 2^64 bytes, past uint64 */
460 EXPECT(p->dma_alloc(p->ctx, d, &small, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
461 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK);
462 EXPECT(omega_gpu_platform_linux_test_set_meminfo("") == 0);
463 unlink(path);
464}
465
466static void c_discrete_vram_reserve(void) {
467 fake_reset(); g_discrete = 1;
468 setenv("CUDAPP_MEM_RESERVE_MIB", "0", 1);
469 setenv("CUDAPP_GPU_MIN_FREE_BYTES", "8192", 1);
470 EXPECT(omega_gpu_platform_linux_test_set_meminfo("/nonexistent/host-memory-must-not-be-used") == 0);
471 fake_vram_free = 4096; fake_vram_total = 8ull << 30;
472 const OmegaGpuPlatform *p = omega_gpu_platform_linux(NULL);
473 OmegaGpuDeviceHandle d;
474 EXPECT(p->device_open(p->ctx, 0, &d) == 0);
475 EXPECT(omega_gpu_platform_linux_target_sm() == 86);
476 OmegaGpuBufferDesc desc = {.kind = OMEGA_GPU_HANDLE_BUF_DATA,
477 .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
478 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
479 OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va;
480 const int before = fk.alloc_calls;
481 EXPECT(p->dma_alloc(p->ctx, d, &desc, &h, &cpu, &va, NULL) == OMEGA_GPU_PLAT_REFUSED_LIMIT);
482 EXPECT(fk.alloc_calls == before && fake_vram_allocs == 0 && fake_vram_free == 4096);
483 EXPECT(strstr(p->diag_text(p->ctx), "host RAM is not VRAM") != NULL);
484 /* Exact byte override is read again, not cached at open. */
485 setenv("CUDAPP_GPU_MIN_FREE_BYTES", "0", 1);
486 EXPECT(p->dma_alloc(p->ctx, d, &desc, &h, &cpu, &va, NULL) == 0);
487 EXPECT(fake_vram_allocs == 1);
488 fk.free_fail_nth = fk.free_calls + 1;
489 EXPECT(p->dma_free(p->ctx, h) == OMEGA_GPU_PLAT_REFUSED_DEVICE);
490 fk.free_fail_nth = 0;
491 EXPECT(p->dma_free(p->ctx, h) == 0); /* handle survived failed driver cleanup */
492 fake_vram_query_fail = 1;
493 setenv("CUDAPP_GPU_MIN_FREE_BYTES", "1", 1);
494 const int allocated = fk.alloc_calls;
495 EXPECT(p->dma_alloc(p->ctx, d, &desc, &h, &cpu, &va, NULL) == OMEGA_GPU_PLAT_REFUSED_LIMIT);
496 EXPECT(fk.alloc_calls == allocated);
497 fake_vram_query_fail = 0;
498 setenv("CUDAPP_GPU_MIN_FREE_BYTES", "18446744073709551616", 1);
499 EXPECT(p->dma_alloc(p->ctx, d, &desc, &h, &cpu, &va, NULL) == OMEGA_GPU_PLAT_REFUSED_LIMIT);
500 EXPECT(fk.alloc_calls == allocated);
501 EXPECT(p->device_close(p->ctx, d) == 0);
502 EXPECT(omega_gpu_platform_linux_test_set_meminfo(NULL) == 0);
503}
504
505static void c_ampere_target_refusal(void) {
506 fake_reset(); g_discrete = 1;
507 setenv("CUDAPP_MEM_RESERVE_MIB", "0", 1);
508 setenv("CUDAPP_GPU_MIN_FREE_BYTES", "0", 1);
509 omega_gpu_session_lock();
510 EXPECT(omega_gpu_session_open() != NULL);
511 EXPECT(omega_gpu_session_target_sm() == 86);
512 const int before = fk.submit_calls;
513 OmegaGpuLaunch L = launch_desc(1000);
514 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) != 0);
515 L.target_sm = 121;
516 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) != 0);
517 L.target_sm = 999;
518 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) != 0);
519 EXPECT(fk.submit_calls == before && !omega_gpu_session_is_blocked());
520 omega_gpu_session_unlock();
521}
522
523/* CodeQL cpp/path-injection #40: the meminfo file is set by code (omega_gpu_platform_linux_test_set_meminfo), never by
524 * the environment, and is honoured only when it opens, without following a final symlink and without blocking, to a
525 * regular file; anything else falls back to the real /proc/meminfo. The reserve is 2^30 MiB (1 PiB), which the real
526 * /proc/meminfo cannot cover, so a fallback shows as REFUSED_LIMIT, while the fake file claims 2^52 kB (4 EiB) free and
527 * shows as OK when honoured. The former CUDAPP_MEMINFO variable naming that same fake file must change nothing.
528 * alarm() turns an open that blocks (the FIFO, before O_NONBLOCK) into a failed case instead of a hung run. */
529static void c_meminfo_test_path(void) {
530 fake_reset();
531 fk.alloc_max = 8u << 20;
532 alarm(10);
533 char dir[] = "/tmp/cudapp-meminfo-dir-XXXXXX";
534 if (!mkdtemp(dir)) { case_ok = 0; return; }
535 char fake[64], lnk[64], fifo[64], sub[64], missing[64];
536 snprintf(fake, sizeof fake, "%s/meminfo", dir);
537 snprintf(lnk, sizeof lnk, "%s/link", dir);
538 snprintf(fifo, sizeof fifo, "%s/fifo", dir);
539 snprintf(sub, sizeof sub, "%s/sub", dir);
540 snprintf(missing, sizeof missing, "%s/missing", dir);
541 write_meminfo(fake, 1ull << 52);
542 EXPECT(symlink(fake, lnk) == 0);
543 EXPECT(mkfifo(fifo, 0600) == 0);
544 EXPECT(mkdir(sub, 0700) == 0);
545 setenv("CUDAPP_MEM_RESERVE_MIB", "1073741824", 1); /* 2^30 MiB */
546 const OmegaGpuPlatform *p = omega_gpu_platform_linux(NULL);
547 OmegaGpuDeviceHandle d;
548 EXPECT(p->device_open(p->ctx, 0, &d) == OMEGA_GPU_PLAT_OK);
549 OmegaGpuBufferDesc small = {.kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_READ | OMEGA_GPU_RIGHT_GPU_WRITE,
550 .gpu_cache = OMEGA_GPU_CACHE_GPU_UNCACHED, .bytes = 4096};
551 OmegaGpuBufferHandle h; OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus;
552 const struct { const char *path; int want; const char *what; } t[] = {
553 {fake, OMEGA_GPU_PLAT_OK, "regular file (honoured)"},
554 {lnk, OMEGA_GPU_PLAT_REFUSED_LIMIT, "symlink to that file"},
555 {fifo, OMEGA_GPU_PLAT_REFUSED_LIMIT, "FIFO"},
556 {sub, OMEGA_GPU_PLAT_REFUSED_LIMIT, "directory"},
557 {missing, OMEGA_GPU_PLAT_REFUSED_LIMIT, "missing file"},
558 {"", OMEGA_GPU_PLAT_REFUSED_LIMIT, "empty (restores /proc/meminfo)"},
559 {fake, OMEGA_GPU_PLAT_OK, "regular file again (honoured)"},
560 {NULL, OMEGA_GPU_PLAT_REFUSED_LIMIT, "NULL (restores /proc/meminfo)"},
561 };
562 for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) {
563 EXPECT(omega_gpu_platform_linux_test_set_meminfo(t[i].path) == 0);
564 const int rc = p->dma_alloc(p->ctx, d, &small, &h, &cpu, &va, &bus);
565 EXPECT(rc == t[i].want);
566 printf(" test meminfo %s: %s\n", t[i].what,
567 rc == OMEGA_GPU_PLAT_OK ? "read" : rc == OMEGA_GPU_PLAT_REFUSED_LIMIT ? "/proc/meminfo read, refused" : "other");
568 }
569 /* the environment no longer chooses the file */
570 EXPECT(omega_gpu_platform_linux_test_set_meminfo(NULL) == 0);
571 setenv("CUDAPP_MEMINFO", fake, 1);
572 int rc = p->dma_alloc(p->ctx, d, &small, &h, &cpu, &va, &bus);
573 EXPECT(rc == OMEGA_GPU_PLAT_REFUSED_LIMIT);
574 printf(" env CUDAPP_MEMINFO=<fake>: %s\n", rc == OMEGA_GPU_PLAT_REFUSED_LIMIT ? "ignored, refused" : "HONOURED");
575 unsetenv("CUDAPP_MEMINFO");
576 /* a path too long for the buffer is refused and leaves the setting unchanged */
577 char longp[4200];
578 memset(longp, 'a', sizeof longp - 1);
579 longp[0] = '/'; longp[sizeof longp - 1] = '\0';
580 EXPECT(omega_gpu_platform_linux_test_set_meminfo(fake) == 0);
581 EXPECT(omega_gpu_platform_linux_test_set_meminfo(longp) == -1);
582 EXPECT(p->dma_alloc(p->ctx, d, &small, &h, &cpu, &va, &bus) == OMEGA_GPU_PLAT_OK);
583 unsetenv("CUDAPP_MEM_RESERVE_MIB");
584 EXPECT(omega_gpu_platform_linux_test_set_meminfo("") == 0);
585 EXPECT(p->device_close(p->ctx, d) == OMEGA_GPU_PLAT_OK);
586 unlink(lnk); unlink(fifo); unlink(fake); rmdir(sub); rmdir(dir);
587 alarm(0);
588}
589
590typedef struct { const char *id; void (*fn)(void); } Case;
591static const Case CASES[] = {
592 {"LINUX_REF_CAPS_NO_MMIO_NO_IRQ", c_caps},
593 {"HOSTED_DEFAULT_IS_LINUX_REFERENCE", c_default_install},
594 {"SAME_DRIVER_CALLS_AND_ORDER_AS_BEFORE", c_same_driver_calls},
595 {"REFERENCE_REFUSALS_CACHED_POLLED_RING_STALE", c_reference_refusals},
596 {"OPEN_FAILURE_KEEPS_DRIVER_TEXT", c_open_failure_text},
597 {"MARKER_NEVER_LATCHES_AND_PARKS", c_marker_never_latches},
598 {"L2_INVAL_PREFIX_ONCE_AFTER_CACHED_HOST_WRITE", c_l2_inval_prefix_once},
599 {"CMDLIST_NEAR_CAP_SUBMITS_WITH_L2_INVAL_PENDING", c_cmdlist_near_cap_with_l2_inval_pending},
600 {"D1_MEMORY_RESERVE_REFUSES_BEFORE_THE_DRIVER", c_memory_reserve},
601 {"QUEUE_SUBMIT_REFUSED_WHILE_UNRETIRED", c_submit_refused_while_unretired},
602 {"EXTRA_QUEUE_SUBMIT_REFUSED_WHILE_UNRETIRED", c_extra_queue_submit_refused_while_unretired},
603 {"CMDLIST_TIMEOUT_LATCHES_WITHOUT_LAUNCH_BEHIND_IT", c_cmdlist_timeout_latches_without_launch},
604 {"ZERO_TIMEOUT_REFUSED_BEFORE_SUBMIT", c_zero_timeout_refused},
605 {"D1_RESERVE_AND_MEMINFO_OVERFLOW_SATURATE", c_reserve_overflow},
606 {"AMPERE_D1_USES_VRAM_REFUSES_BEFORE_RM_ALLOC", c_discrete_vram_reserve},
607 {"AMPERE_REJECTS_IMPLICIT_OR_MISMATCHED_LAUNCH_TARGET", c_ampere_target_refusal},
608 {"D1_MEMINFO_TEST_PATH_REGULAR_ENV_IGNORED", c_meminfo_test_path},
609};
610
611int main(void) {
612 int pass = 0, fail = 0;
613 for (size_t i = 0; i < sizeof CASES / sizeof CASES[0]; i++) {
614 fflush(stdout);
615 pid_t pid = fork();
616 if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); }
617 int st = 0;
618 waitpid(pid, &st, 0);
619 const int ok = WIFEXITED(st) && WEXITSTATUS(st) == 0;
620 printf("%s %s\n", ok ? "PASS" : "FAIL", CASES[i].id);
621 if (ok) pass++; else fail++;
622 }
623 printf("test_omega_gpu_platform_linux (reference only, simulated driver): %d passed, %d failed\n", pass, fail);
624 return fail ? 1 : 0;
625}