interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / test_omega_gpu_session_platform.c

609 lines · 30107 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_omega_gpu_session_platform: the portable GPU session (native v1 P07 cut C, session part; aienos#286) on a
18 * FAKE OmegaGpuPlatform. Host program; links the FREESTANDING partial-link object (build/gpu_fs/
19 * omega_gpu_freestanding.o: session, matmul and attention APIs, serving, wait, codegen, platform contract), so what
20 * runs here is the code the native build would run. Opens no device, touches no GPU.
21 *
22 * The fake device "completes" a launch at submit time by writing the intermediate semaphore DONE and both marker
23 * payloads, or (hang mode) writes nothing. Its clock advances only when asked (plus 1 us per read), so timeouts are
24 * exact and instant. Each case runs in its own child process because an uncertain completion latches the process
25 * for good (that is the rule under test).
26 *
27 * Cases: platform install refusals; freestanding build has no default platform; open/launch/close with typed
28 * buffers, explicit rights and GPU-uncached policy (host-polled marker); polling only (irq never called, mode is
29 * POLLING); cancel before submit (nothing submitted, not latched); cancel after submit (latched, parked); timeout
30 * (latched, session buffers parked, later frees park, close/reopen/install refused, nothing released); stale handle
31 * refusal after close and reopen; close/reopen (new device generation, opens 2); serving reservation refusal (no
32 * platform allocation); matmul API code buffer is a CODE buffer encoded in place; wait helper refuses without a clock;
33 * the bounded formatter matches snprintf and never writes past its cap.
34 */
35#include "omega_gpu_session.h"
36#include "omega_gpu_matmul_api.h"
37#include "omega_gpu_wait.h"
38#include "omega_gpu_fmt.h"
39#include "omega_blackwell_submit.h"
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <sys/wait.h>
45#include <unistd.h>
46
47static int case_ok;
48#define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0)
49
50/* ---- the fake platform ---- */
51#define NB OMEGA_GPU_HANDLE_SLOTS
52enum { MODE_COMPLETE = 0, MODE_HANG = 1 };
53static struct {
54 OmegaGpuHandleTable t;
55 void *mem[NB];
56 OmegaGpuBufferDesc desc[NB];
57 uint32_t dev_of[NB]; /* device generation a buffer belongs to */
58 OmegaGpuDeviceHandle dev;
59 OmegaGpuQueueHandle q;
60 OmegaGpuBufferHandle ring;
61 int mode;
62 int cancel_on_poll; /* poll_word calls omega_gpu_session_cancel() on its first call */
63 uint64_t ns;
64 unsigned allocs, frees, free_refusals, submits, polls, retires, syncs, irq_calls, dev_opens, dev_closes;
65 unsigned locks_held;
66 unsigned cached_requests; /* buffers asked GPU-cached (must stay 0) */
67 unsigned allocs_unlocked; /* allocations without the session lock (must stay 0) */
68 int fail_open;
69 int fail_close; /* device_close closes, then reports a driver teardown fault (C5) */
70} fk;
71
72static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; }
73static uint64_t f_now_ms(void *c) { return f_now_ns(c) / 1000000u; }
74static int f_wait(void *c, uint64_t ms) { (void)c; fk.ns += ms * 1000000u; return 0; }
75static int f_lock(void *c, uint32_t id) { (void)c; if (id != OMEGA_GPU_SESSION_LOCK_ID) return OMEGA_GPU_PLAT_REFUSED_ARGS; fk.locks_held++; return 0; }
76static int f_unlock(void *c, uint32_t id) { (void)c; if (id != OMEGA_GPU_SESSION_LOCK_ID || !fk.locks_held) return OMEGA_GPU_PLAT_REFUSED_ARGS; fk.locks_held--; return 0; }
77static const char *f_diag(void *c) { (void)c; return fk.fail_open ? "fake device: no GB10 here" : ""; }
78
79static int f_dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *out) {
80 (void)c;
81 if (fk.fail_open) return OMEGA_GPU_PLAT_REFUSED_DEVICE;
82 if (ord != 0 || fk.dev.generation) return OMEGA_GPU_PLAT_REFUSED_LIMIT;
83 int rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation);
84 if (rc == 0) { fk.dev = *out; fk.dev_opens++; }
85 return rc;
86}
87static int f_dev_close(void *c, OmegaGpuDeviceHandle d) {
88 (void)c;
89 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE);
90 if (rc) return rc;
91 for (uint32_t i = 0; i < NB; i++) /* every handle of the device goes stale */
92 if (fk.t.live[i] && i != d.index) { (void)omega_gpu_handles_close(&fk.t, i, fk.t.generation[i], fk.t.kind[i]); free(fk.mem[i]); fk.mem[i] = NULL; }
93 (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE);
94 memset(&fk.dev, 0, sizeof fk.dev); memset(&fk.q, 0, sizeof fk.q);
95 fk.dev_closes++;
96 return fk.fail_close ? OMEGA_GPU_PLAT_REFUSED_DEVICE : 0;
97}
98
99static OmegaGpuPlatformCaps g_caps = {
100 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR,
101 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 1000, .max_queues = 1, .max_inflight = 1,
102 .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32,
103};
104
105static int f_alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *o,
106 OmegaCpuView *cpu, OmegaGpuVa *g, OmegaGpuBusAddr *b) {
107 (void)c;
108 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE);
109 if (!rc) rc = omega_gpu_platform_check_buffer(desc, &g_caps);
110 if (rc) return rc;
111 if (desc->gpu_cache != OMEGA_GPU_CACHE_GPU_UNCACHED) fk.cached_requests++;
112 if (!fk.locks_held) fk.allocs_unlocked++;
113 rc = omega_gpu_handles_open(&fk.t, desc->kind, desc->bytes, 1000, 1ull << 32, &o->index, &o->generation);
114 if (rc) return rc;
115 void *m = calloc(1, desc->bytes);
116 fk.mem[o->index] = m; fk.desc[o->index] = *desc; fk.dev_of[o->index] = d.generation;
117 cpu->ptr = m; cpu->len = desc->bytes; g->va = 0x100000000ull + (uint64_t)o->index * 0x1000000ull; b->iova = 0;
118 fk.allocs++;
119 return 0;
120}
121static int f_free(void *c, OmegaGpuBufferHandle h) {
122 (void)c;
123 if (h.index >= NB) { fk.free_refusals++; return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; }
124 int rc = omega_gpu_handles_close(&fk.t, h.index, h.generation, fk.t.kind[h.index]);
125 if (rc) { fk.free_refusals++; return rc; }
126 free(fk.mem[h.index]); fk.mem[h.index] = NULL;
127 fk.frees++;
128 return 0;
129}
130static int buf_live(OmegaGpuBufferHandle h) {
131 return h.index < NB && fk.t.live[h.index] && fk.t.generation[h.index] == h.generation;
132}
133static int f_sync(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) {
134 (void)c;
135 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE;
136 if (len == 0 || off > fk.desc[h.index].bytes || len > fk.desc[h.index].bytes - off) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH;
137 fk.syncs++;
138 return 0;
139}
140static int f_poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) {
141 (void)c;
142 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE;
143 if (!(fk.desc[h.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS;
144 fk.polls++;
145 if (fk.cancel_on_poll) { fk.cancel_on_poll = 0; omega_gpu_session_cancel(); }
146 volatile uint32_t *w = (volatile uint32_t *)((uint8_t *)fk.mem[h.index] + off);
147 *seen = *w;
148 if ((int32_t)(*w - want) >= 0) return 0;
149 fk.ns += ms * 1000000u;
150 return OMEGA_GPU_POLL_TIMED_OUT;
151}
152static int f_irq(void *c, OmegaGpuDeviceHandle d, uint64_t t) { (void)c; (void)d; (void)t; fk.irq_calls++; return 0; }
153static int f_qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *o) {
154 (void)c;
155 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE);
156 if (!rc && (!buf_live(ring) || fk.t.kind[ring.index] != OMEGA_GPU_HANDLE_BUF_QUEUE)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND;
157 if (!rc) rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &o->index, &o->generation);
158 if (!rc) { fk.q = *o; fk.ring = ring; }
159 return rc;
160}
161static int f_qclose(void *c, OmegaGpuQueueHandle q) {
162 (void)c;
163 int rc = omega_gpu_handles_close(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE);
164 if (!rc) memset(&fk.q, 0, sizeof fk.q);
165 return rc;
166}
167/* The fake device: the launch completes at submit (both markers and the semaphore), or never (hang). The marker and
168 * semaphore addresses are read back from the method words: the QMD page is the buffer whose VA the QMD0 header
169 * names, the marker page the one whose VA the first release names. */
170static void *va_to_cpu(uint64_t va) {
171 for (uint32_t i = 0; i < NB; i++) {
172 if (!fk.mem[i] || !fk.t.live[i]) continue;
173 uint64_t base = 0x100000000ull + (uint64_t)i * 0x1000000ull;
174 if (va >= base && va < base + fk.desc[i].bytes) return (uint8_t *)fk.mem[i] + (va - base);
175 }
176 return NULL;
177}
178static int f_submit(void *c, OmegaGpuQueueHandle q, const uint32_t *w, uint32_t n) {
179 (void)c;
180 int rc = omega_gpu_handles_check(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE);
181 if (rc) return rc;
182 if ((uint64_t)n * 4u > fk.desc[fk.ring.index].bytes) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH;
183 memcpy(fk.mem[fk.ring.index], w, (size_t)n * 4u);
184 fk.submits++;
185 if (fk.mode != MODE_COMPLETE) return 0;
186 for (uint32_t i = 0; i + 5 < n; i++) {
187 if (w[i] == ((2u << 28) | (5u << 16) | (0u << 13) | (0x005cu >> 2))) { /* release: va lo, va hi, payload */
188 uint64_t va = (uint64_t)w[i + 1] | ((uint64_t)w[i + 2] << 32);
189 uint32_t *m = va_to_cpu(va);
190 if (m) *m = w[i + 3];
191 }
192 if (w[i] == ((2u << 28) | (2u << 16) | (1u << 13) | (0x0188u >> 2)) && w[i + 3] == ((2u << 28) | (2u << 16) | (1u << 13) | (0x0180u >> 2)) &&
193 w[i + 4] == 4u) { /* the semaphore init upload: the kernel ran, the semaphore says DONE */
194 uint32_t *s = va_to_cpu(((uint64_t)w[i + 1] << 32) | w[i + 2]);
195 if (s) *s = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE;
196 }
197 }
198 return 0;
199}
200static int f_retire(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; fk.retires++; return 0; }
201static void *f_halloc(void *c, size_t b) { (void)c; return malloc(b); }
202static void f_hfree(void *c, void *p) { (void)c; free(p); }
203
204static OmegaGpuPlatform g_plat = {
205 .ctx = &fk, .time_now_ms = f_now_ms, .wait_ms = f_wait, .lock = f_lock, .unlock = f_unlock,
206 .dma_alloc = f_alloc, .dma_free = f_free, .sync_cpu_to_gpu = f_sync, .sync_gpu_to_cpu = f_sync, .poll_word = f_poll,
207 .device_open = f_dev_open, .device_close = f_dev_close, .diag_text = f_diag,
208 .queue_open = f_qopen, .queue_close = f_qclose, .queue_submit = f_submit, .queue_retire = f_retire,
209 .time_now_ns = f_now_ns, .host_alloc = f_halloc, .host_free = f_hfree,
210};
211
212static void fk_reset(void) { memset(&fk, 0, sizeof fk); omega_gpu_handles_init(&fk.t); }
213
214static int install(void) { return omega_gpu_session_set_platform(&g_plat, &g_caps); }
215
216static const uint32_t ARGS[4] = {1, 2, 3, 4};
217static OmegaGpuLaunch launch_desc(uint64_t timeout_ms) {
218 OmegaGpuLaunch L = { .code_va = 0x200000000ull, .gpr_count = 64, .threads_x = 32, .grid_x = 1, .grid_y = 1,
219 .num_elements = 32, .args = ARGS, .n_args = 4, .timeout_ms = timeout_ms };
220 return L;
221}
222
223/* ---- cases ---- */
224
225static void c_install_refusals(void) {
226 fk_reset();
227 OmegaGpuPlatform p = g_plat;
228 OmegaGpuPlatformCaps caps = g_caps;
229 caps.services &= ~OMEGA_GPU_SVC_CLOCK_NS; p.time_now_ns = NULL;
230 EXPECT(omega_gpu_session_set_platform(&p, &caps) == OMEGA_GPU_PLAT_REFUSED_MISSING_SERVICE);
231 p = g_plat; caps = g_caps;
232 p.irq_wait = f_irq; /* a function for a service it does not claim: refused, never used */
233 const int rc_irq = omega_gpu_session_set_platform(&p, &caps);
234 EXPECT(rc_irq != OMEGA_GPU_PLAT_OK && omega_gpu_session_platform() == NULL);
235 caps = g_caps; caps.abi_minor = 1; /* a 1.1 platform predates caps.submit_prefix_words: refused */
236 EXPECT(omega_gpu_session_set_platform(&g_plat, &caps) == OMEGA_GPU_PLAT_REFUSED_ABI);
237 EXPECT(omega_gpu_session_platform() == NULL); /* nothing was installed */
238 EXPECT(install() == OMEGA_GPU_PLAT_OK);
239 EXPECT(omega_gpu_session_platform() == &g_plat);
240 EXPECT(omega_gpu_session_completion_mode() == OMEGA_GPU_COMPLETION_POLLING);
241}
242
243static void c_no_default_platform(void) {
244 fk_reset();
245 /* the freestanding build has no Linux fallback: nothing installed means refused, not emulated */
246 omega_gpu_session_lock();
247 EXPECT(omega_gpu_session_open() == NULL);
248 EXPECT(strstr(omega_gpu_session_last_error(), "no GPU platform installed") != NULL);
249 omega_gpu_session_unlock();
250 EXPECT(omega_gpu_host_alloc(16) == NULL);
251 EXPECT(omega_gpu_session_now_ns() == 0);
252}
253
254static void c_open_launch_close(void) {
255 fk_reset();
256 EXPECT(install() == 0);
257 omega_gpu_session_lock();
258 EXPECT(fk.locks_held == 1);
259 OmegaGpuSession *s = omega_gpu_session_open();
260 EXPECT(s != NULL);
261 if (!s) { omega_gpu_session_unlock(); return; }
262 EXPECT(fk.allocs == 4 && fk.dev_opens == 1);
263 /* typed buffers, explicit rights, all GPU-uncached; the polled marker is HOST_POLLED */
264 EXPECT(fk.t.kind[s->pb.handle.index] == OMEGA_GPU_HANDLE_BUF_QUEUE);
265 EXPECT(fk.desc[s->marker.handle.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED);
266 EXPECT(!(fk.desc[s->cbank.handle.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED));
267 EXPECT(fk.cached_requests == 0 && fk.allocs_unlocked == 0);
268 EXPECT(s->pb.va != s->cbank.va && s->marker.size >= 0x1000 && s->qmd.size >= 0x10000);
269 OmegaGpuLaunch L = launch_desc(100);
270 uint64_t el = 0; uint32_t mk = 0;
271 EXPECT(omega_gpu_session_launch(&L, &el, &mk) == 0);
272 EXPECT(mk == OMEGA_BW_MARKER_COMPLETION_PAYLOAD && fk.submits == 1 && fk.retires == 1 && el > 0);
273 EXPECT(fk.syncs >= 3); /* explicit CPU->GPU and GPU->CPU visibility, never implicit */
274 EXPECT(fk.irq_calls == 0); /* polling only */
275 EXPECT(!omega_gpu_session_is_blocked());
276 /* a CODE buffer: CPU writes, GPU executes, never GPU-written, prefetch tail zeroed */
277 OmegaGpuMem code;
278 EXPECT(omega_gpu_session_alloc_code(100, &code) == 0);
279 EXPECT(fk.t.kind[code.handle.index] == OMEGA_GPU_HANDLE_BUF_CODE);
280 EXPECT((fk.desc[code.handle.index].rights & OMEGA_GPU_RIGHT_GPU_EXEC) && !(fk.desc[code.handle.index].rights & OMEGA_GPU_RIGHT_GPU_WRITE));
281 omega_gpu_session_free(&code);
282 EXPECT(code.cpu == NULL && fk.frees == 1);
283 omega_gpu_session_unlock();
284 EXPECT(fk.locks_held == 0);
285 omega_gpu_session_close();
286 EXPECT(fk.dev_closes == 1 && fk.frees == 5 && fk.t.nlive == 0);
287 EXPECT(!omega_gpu_session_is_open());
288}
289
290static void c_cancel_before_submit(void) {
291 fk_reset();
292 EXPECT(install() == 0);
293 omega_gpu_session_lock();
294 EXPECT(omega_gpu_session_open() != NULL);
295 omega_gpu_session_cancel();
296 OmegaGpuLaunch L = launch_desc(100);
297 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == OMEGA_GPU_SESSION_CANCELLED);
298 EXPECT(fk.submits == 0); /* nothing went to the device */
299 EXPECT(!omega_gpu_session_is_blocked()); /* so nothing is uncertain */
300 OmegaGpuParkStats ps; omega_gpu_session_parked(&ps);
301 EXPECT(ps.buffers == 0);
302 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* the request was consumed; the session still works */
303 omega_gpu_session_unlock();
304 omega_gpu_session_close();
305 EXPECT(fk.dev_closes == 1);
306}
307
308static void check_latched(unsigned frees_before) {
309 EXPECT(omega_gpu_session_is_blocked());
310 OmegaGpuParkStats ps; omega_gpu_session_parked(&ps);
311 EXPECT(ps.buffers == 4 && ps.untracked == 0 && ps.bytes >= 0x10000 + 0x1000 + 0x1000 + 0x10000);
312 /* a buffer the caller frees now is parked too, never released: the device may still be using it */
313 OmegaGpuMem m = {0};
314 omega_gpu_session_lock();
315 EXPECT(omega_gpu_session_alloc(64, &m) != 0); /* no new allocation after a latch */
316 omega_gpu_session_unlock();
317 EXPECT(fk.frees == frees_before);
318 omega_gpu_session_close();
319 EXPECT(fk.dev_closes == 0 && fk.frees == frees_before); /* close refused: nothing released */
320 omega_gpu_session_lock();
321 EXPECT(omega_gpu_session_open() == NULL && strstr(omega_gpu_session_last_error(), "latched") != NULL);
322 OmegaGpuLaunch L = launch_desc(10);
323 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1);
324 EXPECT(strstr(omega_gpu_session_last_error(), "process latched") != NULL);
325 omega_gpu_session_unlock();
326 EXPECT(omega_gpu_session_set_platform(&g_plat, &g_caps) == OMEGA_GPU_PLAT_REFUSED_BUSY);
327 EXPECT(fk.irq_calls == 0);
328}
329
330static void c_timeout_parks(void) {
331 fk_reset();
332 EXPECT(install() == 0);
333 omega_gpu_session_lock();
334 OmegaGpuSession *s = omega_gpu_session_open();
335 EXPECT(s != NULL);
336 OmegaGpuMem user;
337 EXPECT(omega_gpu_session_alloc(4096, &user) == 0);
338 fk.mode = MODE_HANG;
339 OmegaGpuLaunch L = launch_desc(25);
340 const uint64_t t0 = fk.ns;
341 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1);
342 EXPECT(strstr(omega_gpu_session_last_error(), "timed out") != NULL);
343 EXPECT(fk.polls >= 25 && fk.ns - t0 >= 25ull * 1000000u); /* polled in 1 ms slices for the whole timeout */
344 EXPECT(fk.retires == 0); /* never retired: the entry may still be in flight */
345 OmegaGpuBufferHandle uh = user.handle;
346 omega_gpu_session_free(&user);
347 EXPECT(user.cpu == NULL && omega_gpu_session_is_parked(uh));
348 EXPECT(omega_gpu_session_is_parked(s->marker.handle) && omega_gpu_session_is_parked(s->pb.handle));
349 omega_gpu_session_unlock();
350 OmegaGpuParkStats ps; omega_gpu_session_parked(&ps);
351 EXPECT(ps.buffers == 5);
352 EXPECT(fk.frees == 0 && fk.t.nlive == 1 + 1 + 5); /* device, queue, 5 buffers: all still allocated */
353 /* the generic latch checks expect the four session buffers; the user buffer made it five */
354 EXPECT(omega_gpu_session_is_blocked());
355 omega_gpu_session_close();
356 EXPECT(fk.dev_closes == 0 && fk.frees == 0);
357}
358
359static void c_cancel_after_submit(void) {
360 fk_reset();
361 EXPECT(install() == 0);
362 omega_gpu_session_lock();
363 EXPECT(omega_gpu_session_open() != NULL);
364 fk.mode = MODE_HANG;
365 fk.cancel_on_poll = 1; /* the cancel arrives while the host is polling */
366 OmegaGpuLaunch L = launch_desc(60000);
367 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == OMEGA_GPU_SESSION_CANCELLED);
368 EXPECT(fk.submits == 1 && fk.polls <= 2); /* seen within one poll slice, not after the 60 s timeout */
369 EXPECT(strstr(omega_gpu_session_last_error(), "cancelled after submit") != NULL);
370 omega_gpu_session_unlock();
371 check_latched(0);
372}
373
374static void c_timeout_latch_rules(void) {
375 fk_reset();
376 EXPECT(install() == 0);
377 omega_gpu_session_lock();
378 EXPECT(omega_gpu_session_open() != NULL);
379 fk.mode = MODE_HANG;
380 OmegaGpuLaunch L = launch_desc(3);
381 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1);
382 omega_gpu_session_unlock();
383 check_latched(0);
384}
385
386/* C5: recover lifts the latch in the same process. Refused when nothing is latched; after a timeout it closes the device
387 * once (the platform releases every buffer, parked ones included; the session itself frees nothing), clears the parked
388 * list, and the next open is a new device on which a launch completes. */
389static void c_timeout_recover(void) {
390 fk_reset();
391 EXPECT(install() == 0);
392 EXPECT(omega_gpu_session_recover() == -1 && strstr(omega_gpu_session_last_error(), "not latched") != NULL);
393 omega_gpu_session_lock();
394 OmegaGpuSession *s = omega_gpu_session_open();
395 EXPECT(s != NULL);
396 const OmegaGpuDeviceHandle old_dev = s ? s->dev : (OmegaGpuDeviceHandle){0};
397 OmegaGpuLaunch L = launch_desc(5);
398 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* a working launch first */
399 omega_gpu_session_unlock();
400 EXPECT(omega_gpu_session_recover() == -1 && fk.dev_closes == 0); /* open but not latched: refused, device kept */
401 omega_gpu_session_lock();
402 OmegaGpuMem user;
403 EXPECT(omega_gpu_session_alloc(4096, &user) == 0);
404 fk.mode = MODE_HANG;
405 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1 && omega_gpu_session_is_blocked());
406 omega_gpu_session_free(&user); /* parked */
407 omega_gpu_session_unlock();
408 EXPECT(omega_gpu_session_recoveries() == 0);
409 EXPECT(omega_gpu_session_recover() == 0);
410 EXPECT(!omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 1);
411 EXPECT(fk.dev_closes == 1 && fk.frees == 0 && fk.t.nlive == 0); /* the platform close released all of it */
412 OmegaGpuParkStats ps; omega_gpu_session_parked(&ps);
413 EXPECT(ps.buffers == 0 && ps.bytes == 0);
414 EXPECT(omega_gpu_session_recover() == -1); /* nothing latched any more */
415 fk.mode = MODE_COMPLETE;
416 omega_gpu_session_lock();
417 s = omega_gpu_session_open();
418 EXPECT(s != NULL && s->dev.generation != old_dev.generation && fk.dev_opens == 2);
419 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0);
420 omega_gpu_session_unlock();
421 omega_gpu_session_close();
422 EXPECT(fk.dev_closes == 2 && fk.t.nlive == 0);
423}
424
425/* C5: a close that reports a driver teardown fault leaves the process latched for good (the old grid may still run). */
426static void c_recover_teardown_fault(void) {
427 fk_reset();
428 EXPECT(install() == 0);
429 omega_gpu_session_lock();
430 EXPECT(omega_gpu_session_open() != NULL);
431 fk.mode = MODE_HANG;
432 OmegaGpuLaunch L = launch_desc(5);
433 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1 && omega_gpu_session_is_blocked());
434 omega_gpu_session_unlock();
435 fk.fail_close = 1;
436 EXPECT(omega_gpu_session_recover() == -1 && strstr(omega_gpu_session_last_error(), "teardown fault") != NULL);
437 EXPECT(omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 0 && fk.dev_closes == 1);
438 fk.fail_close = 0;
439 EXPECT(omega_gpu_session_recover() == -1 && omega_gpu_session_is_blocked()); /* the handle is gone: no second try */
440 omega_gpu_session_lock();
441 EXPECT(omega_gpu_session_open() == NULL);
442 omega_gpu_session_unlock();
443}
444
445/* C5: a module latch (the command lists after a timeout whose check launch completed) is recoverable too */
446static void c_recover_module_latch(void) {
447 fk_reset();
448 EXPECT(install() == 0);
449 omega_gpu_session_lock();
450 EXPECT(omega_gpu_session_open() != NULL);
451 omega_gpu_session_unlock();
452 omega_gpu_session_note_module_latch();
453 EXPECT(!omega_gpu_session_is_blocked()); /* other APIs keep working */
454 EXPECT(omega_gpu_session_recover() == 0 && fk.dev_closes == 1 && omega_gpu_session_recoveries() == 1);
455 EXPECT(omega_gpu_session_recover() == -1); /* lifted */
456}
457
458static void c_stale_after_reopen(void) {
459 fk_reset();
460 EXPECT(install() == 0);
461 omega_gpu_session_lock();
462 OmegaGpuSession *s = omega_gpu_session_open();
463 EXPECT(s != NULL);
464 const OmegaGpuDeviceHandle d1 = s->dev;
465 OmegaGpuMem old;
466 EXPECT(omega_gpu_session_alloc(8192, &old) == 0);
467 omega_gpu_session_unlock();
468 omega_gpu_session_close(); /* the caller kept `old` past the close */
469 EXPECT(fk.dev_closes == 1);
470 omega_gpu_session_lock();
471 s = omega_gpu_session_open();
472 EXPECT(s != NULL && omega_gpu_session_open_count() == 2);
473 EXPECT(s->dev.index != d1.index || s->dev.generation != d1.generation); /* a new device generation */
474 const unsigned frees = fk.frees;
475 OmegaGpuAllocStats st0, st1;
476 omega_gpu_session_alloc_stats(&st0);
477 omega_gpu_session_free(&old); /* a handle of the closed device: refused, nothing released */
478 omega_gpu_session_alloc_stats(&st1);
479 EXPECT(st1.stale_refusals == st0.stale_refusals + 1 && st1.frees == st0.frees);
480 EXPECT(fk.frees == frees && fk.free_refusals == 1);
481 EXPECT(strstr(omega_gpu_session_last_error(), "STALE") != NULL || strstr(omega_gpu_session_last_error(), "stale") != NULL);
482 EXPECT(old.cpu == NULL);
483 OmegaGpuLaunch L = launch_desc(100);
484 EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* the reopened session works */
485 omega_gpu_session_unlock();
486 omega_gpu_session_close();
487 EXPECT(fk.dev_closes == 2 && fk.t.nlive == 0);
488}
489
490static void c_open_failure_text(void) {
491 fk_reset();
492 EXPECT(install() == 0);
493 fk.fail_open = 1;
494 omega_gpu_session_lock();
495 EXPECT(omega_gpu_session_open() == NULL);
496 EXPECT(strstr(omega_gpu_session_last_error(), "fake device: no GB10 here") != NULL); /* the driver text survives */
497 EXPECT(!omega_gpu_session_is_blocked() && fk.allocs == 0);
498 fk.fail_open = 0;
499 EXPECT(omega_gpu_session_open() != NULL); /* a failed open is not a latch */
500 omega_gpu_session_unlock();
501 omega_gpu_session_close();
502}
503
504static void c_reservation_refusal(void) {
505 fk_reset();
506 EXPECT(install() == 0);
507 omega_gpu_session_lock();
508 EXPECT(omega_gpu_session_open() != NULL);
509 OmegaGpuScratch sc; memset(&sc, 0, sizeof sc);
510 EXPECT(omega_gpu_session_scratch_reserve(&sc, 10000) == 0);
511 const unsigned allocs = fk.allocs;
512 EXPECT(omega_gpu_session_scratch(&sc, 9000) == 0);
513 EXPECT(omega_gpu_session_scratch(&sc, sc.cap + 1) == OMEGA_GPU_SESSION_SCRATCH_RESERVED);
514 EXPECT(fk.allocs == allocs); /* refused loudly, nothing allocated */
515 EXPECT(strstr(omega_gpu_session_last_error(), "serving reservation exceeded") != NULL);
516 omega_gpu_session_scratch_free(&sc);
517 omega_gpu_session_unlock();
518 omega_gpu_session_close();
519 EXPECT(fk.t.nlive == 0);
520}
521
522static void c_matmul_code_buffer(void) {
523 fk_reset();
524 EXPECT(install() == 0);
525 uint16_t b[32 * 16];
526 memset(b, 0, sizeof b);
527 OmegaGpuTensor *t = NULL;
528 EXPECT(omega_gpu_tensor_upload_bf16(32, 16, b, &t) == OMEGA_GPU_MATMUL_OK);
529 uint16_t a[32]; memset(a, 0, sizeof a);
530 float c[16];
531 OmegaGpuMatmulInfo info;
532 /* the fake device runs no kernel, so the result sentinels survive: CHIP_FAIL is the expected verdict here */
533 int rc = omega_gpu_matmul_resident_bf16(1, a, t, c, &info);
534 EXPECT(rc == OMEGA_GPU_MATMUL_CHIP_FAIL && fk.submits >= 1);
535 unsigned code_bufs = 0;
536 for (uint32_t i = 0; i < NB; i++)
537 if (fk.t.live[i] && fk.t.kind[i] == OMEGA_GPU_HANDLE_BUF_CODE) {
538 code_bufs++;
539 const uint8_t *p = fk.mem[i];
540 unsigned nz = 0;
541 for (uint32_t j = 0; j < 64; j++) nz += p[j] != 0;
542 EXPECT(nz > 0); /* the kernel was encoded in place into the device code buffer */
543 EXPECT((fk.desc[i].rights & OMEGA_GPU_RIGHT_GPU_EXEC) && !(fk.desc[i].rights & OMEGA_GPU_RIGHT_GPU_WRITE));
544 }
545 EXPECT(code_bufs == 1);
546 EXPECT(fk.cached_requests == 0 && fk.allocs_unlocked == 0 && fk.irq_calls == 0);
547 omega_gpu_tensor_free(t);
548 omega_gpu_session_close();
549 EXPECT(fk.t.nlive == 0);
550}
551
552static void c_wait_needs_clock(void) {
553 volatile uint32_t word = 0;
554 omega_gpu_wait_report_t r;
555 EXPECT(!omega_gpu_wait_fixed(&word, 1, &r, NULL));
556 EXPECT(r.result == OMEGA_GPU_WAIT_INVALID_STATE); /* no clock in the freestanding build: refused, not unbounded */
557}
558
559static void c_fmt_matches_snprintf(void) {
560 char a[96], b[96];
561 const long long big = -9223372036854775807LL - 1;
562 omega_gpu_fmt(a, sizeof a, "%s:%c:%d:%u:%x:%ld:%lu:%lld:%llu:%zu:%%", "stage", 'x', -42, 4000000000u, 0xbeefu,
563 -7L, 9UL, big, 18446744073709551615ULL, (size_t)12345);
564 snprintf(b, sizeof b, "%s:%c:%d:%u:%x:%ld:%lu:%lld:%llu:%zu:%%", "stage", 'x', -42, 4000000000u, 0xbeefu,
565 -7L, 9UL, big, 18446744073709551615ULL, (size_t)12345);
566 EXPECT(strcmp(a, b) == 0);
567 char s[8];
568 memset(s, 'Z', sizeof s);
569 omega_gpu_fmt(s, 6, "%s", "truncate me");
570 EXPECT(strcmp(s, "trunc") == 0 && s[6] == 'Z' && s[7] == 'Z'); /* bounded: NUL inside cap, nothing past it */
571 omega_gpu_fmt(s, 1, "%d", 123);
572 EXPECT(s[0] == '\0');
573}
574
575typedef struct { const char *id; void (*fn)(void); } Case;
576static const Case CASES[] = {
577 {"INSTALL_REFUSALS_ABI_1_1", c_install_refusals},
578 {"FREESTANDING_NO_DEFAULT_PLATFORM", c_no_default_platform},
579 {"OPEN_LAUNCH_CLOSE_TYPED_UNCACHED_POLLING", c_open_launch_close},
580 {"OPEN_FAILURE_KEEPS_DRIVER_TEXT", c_open_failure_text},
581 {"CANCEL_BEFORE_SUBMIT_NOT_LATCHED", c_cancel_before_submit},
582 {"CANCEL_AFTER_SUBMIT_LATCHES_AND_PARKS", c_cancel_after_submit},
583 {"TIMEOUT_PARKS_NEVER_RELEASES", c_timeout_parks},
584 {"TIMEOUT_LATCH_REFUSES_CLOSE_REOPEN_INSTALL", c_timeout_latch_rules},
585 {"TIMEOUT_RECOVER_SAME_PROCESS", c_timeout_recover},
586 {"RECOVER_TEARDOWN_FAULT_STAYS_LATCHED", c_recover_teardown_fault},
587 {"RECOVER_MODULE_LATCH", c_recover_module_latch},
588 {"STALE_HANDLE_REFUSED_AFTER_CLOSE_REOPEN", c_stale_after_reopen},
589 {"SERVING_RESERVATION_REFUSES_WITHOUT_ALLOC", c_reservation_refusal},
590 {"MATMUL_CODE_BUFFER_ENCODED_IN_PLACE", c_matmul_code_buffer},
591 {"WAIT_REFUSES_WITHOUT_CLOCK", c_wait_needs_clock},
592 {"FMT_BOUNDED_MATCHES_SNPRINTF", c_fmt_matches_snprintf},
593};
594
595int main(void) {
596 int pass = 0, fail = 0;
597 for (size_t i = 0; i < sizeof CASES / sizeof CASES[0]; i++) {
598 fflush(stdout);
599 pid_t pid = fork();
600 if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); }
601 int st = 0;
602 waitpid(pid, &st, 0);
603 const int ok = WIFEXITED(st) && WEXITSTATUS(st) == 0;
604 printf("%s %s\n", ok ? "PASS" : "FAIL", CASES[i].id);
605 if (ok) pass++; else fail++;
606 }
607 printf("test_omega_gpu_session_platform: %d passed, %d failed\n", pass, fail);
608 return fail ? 1 : 0;
609}