1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Craton Software Company
//! `cuda-oxide` host-runtime adapter (dep-less scaffold).
//!
//! This module is the v0.5 cust-successor migration tracked in [RFC
//! 0001](../../../../rfcs/0001-cuda-oxide-integration.md).
//!
//! **`cuda-oxide-backend` (scaffold, dep-less)** — [`Self::allocate`]
//! returns the documented `NOT_YET_WIRED` sentinel error. No git
//! dependency is pulled into the resolved graph; `cargo check --features
//! cuda-oxide-backend` builds on any contributor host even without a CUDA
//! Toolkit or `libclang`, and the crate stays publishable to crates.io.
//! The scaffold's only job is to keep the public surface
//! (`CudaOxideUnifiedBuffer`, `apply_advice`, `CudaOxideAdvice`) stable so
//! call-sites in `tensor-wasm-jit` / `tensor-wasm-tenant` written today
//! need no re-typing once the real host port lands.
//!
//! NOTE: the real `cuMemAllocManaged`/`cuMemAdvise`/`cuMemPrefetchAsync`/
//! `cuMemFree_v2` host port previously lived in this file behind an
//! `experimental-cuda-oxide-host-backend` feature that pulled in the
//! git-pinned cuda-oxide host/device crates (`cuda-host`, `cuda-core`,
//! `cuda-async`, `cuda-device`, `cuda-macros`). crates.io rejects any
//! manifest with a `git` dependency — even an optional, feature-gated one
//! — so both that feature and those deps were REMOVED to unblock
//! publishing. The host port never actually compiled (its module opened
//! with a `compile_error!`), so nothing that built before is lost. It can
//! be re-added once cuda-oxide ships its workspace members to crates.io;
//! see RFC 0001 and `docs/CUDA-OXIDE-CUTOVER.md` for the cutover plan.
//!
//! # What this module IS NOT (yet)
//!
//! * A binding to cuda-oxide's `dialect-mir` / `dialect-llvm` lowering
//! pipeline. The Wasm→PTX kernel-compilation lever (per RFC 0001
//! "Pliron lever and the auto-offload pipeline") lives in
//! `tensor-wasm-jit::pliron_*` and is gated separately.
//! * A real driver-backed allocation surface. Every call returns the
//! `NOT_YET_WIRED` sentinel until the host port is restored.
use HashSet;
use fmt;
use OnceLock;
use Mutex;
use crate;
/// Process-global set of raw CUDA device-pointer values orphaned by a failed
/// free on the cuda-oxide path. Mirrors
/// [`crate::cudarc_backend::leaked_cuda_allocations`]'s contract so the v0.4
/// port inherits an instrumentation harness when the real `cuda_host` free
/// call lands here. Today no construction succeeds, so the set stays empty
/// in practice — but the public accessor exists so operator tooling can wire
/// against a stable surface now. See `cudarc_backend` for the threat-model
/// write-up; this backend inherits the same mem H4 invariant once it grows
/// a real free path.
static LEAKED_CUDA_ALLOCATIONS: = new;
// `record_leak` will be called from the real host-backend `Drop` on every
// `cuMemFree_v2` failure (mem H4 invariant) once the cuda-oxide host port
// is restored. In the current scaffold-only build no construction
// succeeds, so the call site is unreachable and the function is dead code;
// the unconditional allow keeps `-D warnings` happy on contributor boxes.
/// Snapshot of the process-global set of CUDA pointers orphaned by a failed
/// free on the cuda-oxide path. See
/// [`crate::cudarc_backend::leaked_cuda_allocations`] for the full audit
/// contract; this accessor is the cuda-oxide-path mirror so operator
/// tooling has a single shape across backends.
/// Sentinel error message returned by every stub call in this module while
/// the dep-less `cuda-oxide-backend` scaffold is the only path (the real
/// host port was removed for crates.io publishability — see the module NOTE).
///
/// Exposed `pub(crate)` so the unit + integration tests can assert against
/// the exact string without duplicating it. The future host port should
/// leave this constant in place because it remains observable on
/// contributor boxes that build with `--features cuda-oxide-backend`.
pub const NOT_YET_WIRED: &str =
"cuda-oxide-backend: allocate not yet wired -- see RFC 0001 v0.4 port";
/// Memory-advice hint that the future cuda-oxide host port will pass to
/// `cuMemAdvise`. Kept on the scaffold path so the public surface is stable.
///
/// Mirrors the shape of [`crate::advise::Advice`] but is declared locally so
/// the dep-less scaffold build (without the host crates) still has a
/// concrete enum to compile against. The variants intentionally do not
/// re-export the full [`crate::advise::Advice`] enum so that
/// `cuda_oxide_backend.rs` has zero non-feature-gated dependence on the
/// `crate::advise` module — a regression in the cust path's advice table
/// cannot break a host-backend build.
// =============================================================================
// Scaffold path — the only path today. The real `cuMemAllocManaged` host
// backend was removed for crates.io publishability (it relied on git-pinned
// cuda-oxide crates); see the module-level NOTE and RFC 0001 /
// docs/CUDA-OXIDE-CUTOVER.md.
// =============================================================================
// =============================================================================
// Re-export the scaffold variant under stable names so callers do not need
// to feature-gate their imports.
//
// MEM H4 INVARIANT (contract for the future cuda-oxide host backend):
// - The Drop impl on the real `CudaOxideUnifiedBuffer` MUST never panic.
// - On any non-success return from the real free call, the raw pointer
// MUST be passed to `record_leak()` (in this module) and the failure
// logged at `error!` level -- see `cudarc_backend::Drop` for the
// reference pattern. A failed free is a security-relevant event:
// the driver may recycle the same VA for another tenant carrying
// residual bytes.
// - After a failed free, the inner allocation handle MUST be replaced
// with a sentinel so any hypothetical double-Drop short-circuits.
// - `leaked_cuda_allocations()` above is the operator audit surface.
//
// NOTE: the real `host_backend` module that satisfied these invariants was
// removed for crates.io publishability (it depended on git-pinned
// cuda-oxide crates). Restore it -- and re-export
// `cuda_oxide_free_failures` alongside the names below -- once cuda-oxide
// publishes to crates.io. See RFC 0001 / docs/CUDA-OXIDE-CUTOVER.md.
// =============================================================================
pub use ;