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 / evidence / NVML-2026-11-07 / seeded / BRIDGE-EXTENSION.md

147 lines · 14402 bytes Raw Blame History Rendered
1# NVML C ABI emitter extension
2
3Status: PROPOSED, not an existing Omega syntax claim. Owner of integration: Main. Consumer: aien-dev/CUDApp#365, branch feat/omega-nvml-replacement, baseline 4bf58894d1985d41240736145de7b5fe03e377ee. Compiler: omega-mv e5b32567, omc SHA256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f. CUDApp bridge lock: bc11b30b1faa9af8b875ae6fec24e5f49cf03ac4. Current newer bridge documentation at d8174edd still refuses globals/text/host words in public native ABI with exit 110. This extension does not authorize weakening those existing modes: add an explicit library-emission mode.
4
5## Current boundary and work available
6
7`compat/nvml/cudapp_nvml.c` is 167 physical lines, not 167 lines of pure ABI marshalling. Its handle mapping (43–47), requested-action/call protocol (49–100), wrappers (102–148), and string cache (150–167) remain in the production path. `tests/nvml/host.om` is 942 physical lines: its lines 225–444 port the action/call protocol, wrappers and cache; lines 144–223 port the fake transport; the remaining code contains test state, helpers and the same 476 checks. This is an exercised model port, not a deployed .so. Do not describe the production C protocol as already removed.
8
9Observed CPU behavior: pure Omega host has the same 476 passing checks and the same failure/check counts for all 22 source mutants as the prior C caller. The new seeded conductor additionally retains generated-source equality, CUDA-header ABI comparison, thin-shell guard and oracle negative control. Live RM, concurrent ABI calls and the generated .so remain NOT_RUN.
10
11The deliverable of this extension is removal of the production C file, not generated boilerplate around that handwritten file. Lift the exercised protocol into `native/nvml/api.om`; bind its transport operations to the existing `native/nvrm/transport.om` functions in production and `rmfake.om` in the device-free gate. Keep the same NVML state machine, codes, copy decisions and fake faults. The bridge must lower the resulting program-mode globals and calls; it must not translate a C protocol into a second handwritten implementation.
12
13## Declaration format
14
15Use ordinary seeded `.om` declarations plus machine-readable `note cabi` metadata. Every file remains accepted by the pinned compiler because these are notes; the new bridge mode must validate and consume them. Unknown or incomplete `cabi` notes must be refused, not silently ignored in library mode. The following grammar is the proposed build contract, not already implemented:
16
17```text
18note cabi library libnvidia-ml.so.1 header nvml.h
19note cabi lock NvLock process mutex
20note cabi status nvmlReturn_t int32 valid 0..30,999 trap 999
21note cabi enum nvmlTemperatureSensors_t uint32
22note cabi handle nvmlDevice_t nvmlDevice_st pool NvDev count 8 invalid 18446744073709551615 lock NvLock
23note cabi record nvmlMemory_t size 24 align 8 total:u64:0 free:u64:8 used:u64:16
24note cabi record nvmlUtilization_t size 8 align 4 gpu:u32:0 memory:u32:4
25note cabi record nvmlProcessInfo_t size 24 align 8 pid:u32:0 usedGpuMemory:u64:8 gpuInstanceId:u32:16 computeInstanceId:u32:20
26note cabi state NvState process lock NvLock
27note cabi state NvBytes process lock NvLock
28note cabi state NvTexts process lock NvLock
29note cabi state NvReady process lock NvLock
30note cabi cstr NvTexts slots 32 width 64 fallback "Unknown Error" lock NvLock
31note cabi export nvmlInit_v2 nv_init status nvmlReturn_t lock NvLock
32note cabi export nvmlDeviceGetCount_v2 nv_count status nvmlReturn_t lock NvLock
33note cabi arg nvmlDeviceGetCount_v2 deviceCount nullable out uint32 commit 0
34note cabi export nvmlDeviceGetName nv_name status nvmlReturn_t lock NvLock
35note cabi arg nvmlDeviceGetName device handle nvmlDevice_t
36note cabi arg nvmlDeviceGetName name nullable out char length length commit 0
37note cabi arg nvmlDeviceGetName length uint32
38note cabi export nvmlErrorString nv_error cstr NvTexts lock NvLock
39note cabi arg nvmlErrorString result signed int32
40```
41
42Types refer to existing Omega `uint`, `ulong`, `uchar`, `global`, `text` and bounded array parameters. No raw pointer enters Omega. Public names are metadata and need not satisfy the seeded eight-character local-name limit. The generator owns header and ELF export-list emission; headers are generated, not hand-edited. All remaining NVML entry points in `compat/nvml/nvml.h` use these same constructs: status, nullable scalar in/out, fixed record out, counted record-array out, handle in/out and cstr return. Add `nullable inout uint32` for `infoCount` and `nullable out nvmlProcessInfo_t count infoCount commit 1` for `infos`. Read an inout value only when non-null. Preserve input/output on errors unless Omega's commit bit explicitly allows a write (notably the count on INSUFFICIENT_SIZE).
43
44### Explicit lowering contract
45
46The bridge derives a bounded call frame from declarations. The implementation entry points use normal seeded signatures:
47
48```text
49global NvState ulong 128
50global NvBytes uchar 83296
51global NvTexts uchar 2048
52global NvReady uchar 32
53
54note Frame for nv_init: [status, commit_bits], no output bytes.
55fn nv_init frame edit ulong 2 to ulong
56note BODY is the existing exercised protocol port, not a new C body.
57end
58
59note Frame for nv_count: [status, commit_bits, count]. present is 0 or 1.
60fn nv_count present ulong frame edit ulong 3 to ulong
61end
62
63note Frame for nv_name: [device_index, present, length, status, commit_bits, bytes_to_copy].
64note bytes receives at most 6144 bytes. The state machine decides required length/error codes.
65fn nv_name frame edit ulong 6 bytes edit uchar 6144 to ulong
66end
67
68note Frame for nv_error: [signed-code-bits, slot, string_length].
69note Writes/checks NvTexts/NvReady in Omega and returns success 0; slot 32 means fallback.
70fn nv_error frame edit ulong 3 to ulong
71end
72```
73
74These signature sketches deliberately omit bodies: the bridge owner must connect the exercised implementations, not commit these empty sketches. Return value 0 means the Omega protocol completed; a nonzero return or a compiler trap becomes the declared NVML trap status without process abort. The emitter's generated checked call uses the existing `omega_*` status convention underneath. Frames are private, initialized, and never exposed to the foreign caller. The generator must document and enforce field indices above so metadata and Omega implementation cannot disagree silently.
75
76Nullable out pointers lower to a presence flag and scratch memory. They are never eagerly dereferenced and never converted to integer pointers inside Omega. Only declared commit bits permit publication. Required span/capacity checks and successful scalar narrowing precede *all* publication, preventing partially modified caller outputs on adapter failure. A `NULL` buffer with a nonzero length is passed as `present=0`, not dereferenced or given fake capacity.
77
78Opaque handles are addresses of immutable library-issued table entries. The generated adapter compares pointer identity with table addresses without dereferencing foreign pointers. Unknown/NULL handles lower to UINT64_MAX; Omega decides the NVML return status. Handle outputs validate the returned index before publishing a table address. No arbitrary pointer arithmetic or dereference of an untrusted handle.
79
80Process globals are zero-initialized once per loaded library and persist across calls. A generated process mutex serializes *all* entries sharing the state and string cache. Library unload follows normal dlclose caller obligations. Error strings live in process-global static storage and cannot be freed or reused; returned pointer identity stays stable across repeated calls, init/shutdown and other error-string requests. No per-call allocation is required. `pthread_mutex_t` is an explicit temporary host dependency, with exact libc/toolchain versions recorded by the consumer; the production replacement is an Omega process-lock host primitive. This proposal must not silently adopt it as Omega implementation.
81
82## Required generated C goldens
83
84The following are expected output shapes, not handwritten source files to ship. Boilerplate helpers are generated once by the bridge. `nv_ok_status` checks the declared enum domain. `nv_handle_index` performs safe table-identity mapping. `omega_nv_*` denotes bridge-generated checked implementations of the seeded Omega entry points, including process-global storage. No call to the former handwritten `nvml_run` or `nvml_act` is permitted.
85
86```c
87/* GENERATED by omega-bridge <full source revision>; source SHA256 <digest>. */
88nvmlReturn_t nvmlDeviceGetCount_v2(unsigned int *deviceCount) {
89 uint64_t frame[3] = {999, 0, 0}, done = 1;
90 nvmlReturn_t result = NVML_ERROR_UNKNOWN;
91 if (pthread_mutex_lock(&NvLock) != 0) return result;
92 uint32_t st = omega_nv_count(&done, deviceCount != NULL, frame, 3);
93 if (st == OMEGA_OK && done == 0 && nv_ok_status(frame[0]) &&
94 (frame[1] & ~UINT64_C(1)) == 0 &&
95 (!(frame[1] & 1) || (deviceCount != NULL && frame[2] <= UINT32_MAX))) {
96 if (frame[1] & 1) *deviceCount = (unsigned int)frame[2];
97 result = (nvmlReturn_t)frame[0];
98 }
99 pthread_mutex_unlock(&NvLock);
100 return result;
101}
102
103nvmlReturn_t nvmlDeviceGetName(nvmlDevice_t device, char *name, unsigned int length) {
104 uint64_t frame[6] = {nv_handle_index(device), name != NULL, length, 999, 0, 0};
105 uint8_t bytes[6144] = {0};
106 uint64_t done = 1;
107 nvmlReturn_t result = NVML_ERROR_UNKNOWN;
108 if (pthread_mutex_lock(&NvLock) != 0) return result;
109 uint32_t st = omega_nv_name(&done, frame, 6, bytes, sizeof bytes);
110 if (st == OMEGA_OK && done == 0 && nv_ok_status(frame[3]) &&
111 (frame[4] & ~UINT64_C(1)) == 0 &&
112 (!(frame[4] & 1) || (frame[5] <= length && frame[5] <= sizeof bytes &&
113 (frame[5] == 0 || name != NULL)))) {
114 if ((frame[4] & 1) && frame[5]) memcpy(name, bytes, (size_t)frame[5]);
115 result = (nvmlReturn_t)frame[3];
116 }
117 pthread_mutex_unlock(&NvLock);
118 return result;
119}
120
121const char *nvmlErrorString(nvmlReturn_t code) {
122 uint64_t frame[3] = {(uint64_t)(int64_t)code, 32, 0}, done = 1;
123 const char *result = "Unknown Error";
124 if (pthread_mutex_lock(&NvLock) != 0) return result;
125 uint32_t st = omega_nv_error(&done, frame, 3);
126 if (st == OMEGA_OK && done == 0 && frame[1] < 32 && frame[2] < 64 &&
127 NvTexts[frame[1] * 64 + frame[2]] == 0)
128 result = (const char *)&NvTexts[frame[1] * 64];
129 pthread_mutex_unlock(&NvLock);
130 return result;
131}
132```
133
134This is the minimal readable golden, not a required inefficient code shape: do not zero the entire byte scratch if the checked Omega writer can establish the written range before publication. Preserve the semantics and bounds; use compiler lifetime/initialization analysis to avoid avoidable copies/initialization. `NvTexts` is generated static state and the checked Omega function must address that same object, not a duplicate array. All generated C exports have default visibility; all internal Omega/helper symbols are hidden. Emit a version script enumerating precisely the 11 symbols currently declared in the NVML header, and link with `-Wl,-soname,libnvidia-ml.so.1`. Reject duplicate/unknown symbols and any source drift against the manifest. The test checks the actual symbol list rather than trusting the link command.
135
136## Smallest acceptance and negative controls
137
1381. Add a seeded `.om` bridge fixture defining the declarations and three complete functions for count/name/error-string, with no NVML RM access. Compile it with the pinned seeded compiler; emit C/header/export map twice and require byte equality and source/generator provenance. Build an isolated shared library with the exact soname and only declared exports. Verify metadata/exports using pinned readelf/nm as read-only oracles.
1392. Use an Omega test driver to generate/build and invoke a generated foreign caller for the fixture. It must check null out-pointers, untouched error outputs, length-minus-one/exact/oversized buffers with sentinels, forged handles without dereference, signed negative enum inputs, repeated stable error-string pointers and process-state persistence. Emit the foreign caller through omega-bridge too; do not introduce a handwritten C driver. Until the callback/caller emitter exists, the existing CUDApp C caller remains an explicitly labelled ORACLE only.
1403. Integrate the full API `.om` protocol against the existing RM fake. The generated .so/header must pass the original C-ABI ORACLE's 476 checks, the 55 CUDA-header ABI facts, compile against NVIDIA prototypes, and reject all 22 identical source mutants with identical failure/check counts. The same pure-Omega model must retain those counts. Run in a /dev-isolated CPU-only sandbox; no GPU is needed.
1414. Mandatory generated-wrapper negative control: remove the `deviceCount != NULL` publication guard in a *generated-output copy* and force `commit_bits=1` with a null output. The subprocess must fail; a separate mutant suppressing the count publication with a non-null pointer must fail the observable count test without crashing. A surviving mutant makes the gate fail. Neither compiler failure nor exec failure counts as a caught behavioral mutant.
1425. Declaration-refusal negative controls: undeclared global, duplicate export, unknown lock, invalid record offset/size, unsupported raw-pointer result, out-span lacking a length, and cstr offset/length outside the declared storage. Each must refuse before writing a usable .so; never silently generate a partial library.
1436. Concurrency is a separate ABI gate: overlapping calls must preserve state and immutable string pointers; label NOT_RUN until a real generated concurrent caller is available. The previous 476 checks are not concurrency evidence. Live hardware is separately QUALIFIED only under an authorized GPU window.
144
145## Extension completion contract
146
147The bridge owner returns the exact source revision, new emitter invocation and generated-artifact hashes; CUDApp repins it, removes `compat/nvml/cudapp_nvml.c`, removes the handwritten header in favor of the generated header, and rewires all library callers/build paths. Only then is the pure-Omega HOLD cleared. A compile-only implementation, three-entry fixture alone, stale generated code, or leaving nvml_run/nvml_act handwritten does not complete #365.