Skip to main content

edgefirst_tensor/tensor_dyn/
derived.rs

1// SPDX-FileCopyrightText: Copyright 2026 Au-Zone Technologies
2// SPDX-License-Identifier: Apache-2.0
3
4//! Methods expressible purely over the primitive `TensorDyn` API, written
5//! once and compiled into both the `static` and `dynamic` backends.
6//!
7//! [`TensorDyn::import_descriptor`] is the first resident: the consumer
8//! half of the cross-package tensor protocol. It was `static`-only until
9//! `edgefirst-python-common` -- the first consumer to call `TensorDyn` at
10//! the Rust level under `dynamic` -- needed it, and almost all of it is
11//! backend-independent. Only the per-kind *construction* switch names
12//! constructors and therefore differs; that stays in each backend as
13//! `import_storage`, and the validation before it and the metadata restore
14//! after it live here, once. Writing the whole function twice was the
15//! alternative and it is the shape this branch exists to delete: the
16//! stride-restore reasoning below is forty lines of load-bearing
17//! commentary that a second copy would eventually contradict.
18
19use crate::{DType, Result, TensorDesc, TensorDyn};
20
21impl TensorDyn {
22    /// Rebuild a tensor addressing the memory an [`crate::TensorDesc`]
23    /// describes — the consumer half of the cross-package tensor protocol
24    /// (see [`crate::protocol`]'s module docs).
25    ///
26    /// **Borrow semantics.** The result *aliases* the producer's memory; it
27    /// is valid only while the producer keeps that memory alive. For the
28    /// capsule protocol, that is the capsule's own lifetime: the capsule's
29    /// keepalive holds the producing tensor (and, for a `HOST` descriptor,
30    /// its pin) for as long as the capsule itself is alive. A `DMABUF`
31    /// import is the one exception with its own, narrower guarantee — this
32    /// function `dup`s the fd, so the imported tensor's fd stays valid even
33    /// after the producer's tensor drops (closing its own copy); the
34    /// underlying dma-buf allocation itself is still only alive as long as
35    /// *some* fd or GPU import references it. `D3D11_TEXTURE` behaves the
36    /// same way for the same reason: the import duplicates the texture and
37    /// fence NT handles it keeps, so the result outlives the producer's
38    /// tensor while the texture itself lives as long as some handle or view
39    /// references it. Both of the descriptor's handle values must be valid
40    /// **in this process** — the protocol duplicates, it does not reach into
41    /// another process's handle table (the blob transport does; see
42    /// [`crate::blob::import`]). A consumer needing to survive past the
43    /// capsule for any other kind (`HOST`, `IOSURFACE`) must take its own
44    /// reference explicitly — this function does not.
45    ///
46    /// # Errors
47    ///
48    /// * [`Error::NotImplemented`](crate::Error::NotImplemented) — an ABI
49    ///   major this build does not understand ([`desc.version`](crate::TensorDesc::version)),
50    ///   a dtype/kind code this build does not recognise, or a kind this
51    ///   platform cannot import (e.g. dma-buf off Linux).
52    /// * [`Error::InvalidArgument`](crate::Error::InvalidArgument) — a
53    ///   malformed descriptor: a missing fd/surface id/host address for the
54    ///   kind it claims to be, or (macOS/iOS) a surface id that no longer
55    ///   resolves to a live `IOSurface`.
56    pub fn import_descriptor(desc: &TensorDesc) -> Result<Self> {
57        let (dtype, shape) = validate_descriptor(desc)?;
58        let mut t = Self::import_storage(desc, &shape, dtype)?;
59        restore_descriptor_metadata(&mut t, desc, &shape)?;
60        Ok(t)
61    }
62}
63
64/// Refuse a descriptor this build cannot honour, and decode the two facts
65/// every `import_storage` arm needs from one that it can.
66///
67/// Separate from the construction switch so the refusals apply identically
68/// on both backends -- a descriptor is untrusted cross-package input, and
69/// "which producers do we refuse" is exactly the decision that must not
70/// have two implementations.
71fn validate_descriptor(desc: &TensorDesc) -> Result<(DType, Vec<usize>)> {
72    if desc.version != crate::ABI_VERSION {
73        return Err(crate::Error::NotImplemented(format!(
74            "tensor interop ABI v{} (this build understands v{})",
75            desc.version,
76            crate::ABI_VERSION
77        )));
78    }
79
80    if desc.ndim as usize > crate::TensorDesc::MAX_NDIM {
81        return Err(crate::Error::NotImplemented(format!(
82            "descriptor declares rank {}, but this build's descriptor \
83             carries only {} shape slots; importing would address a \
84             prefix of a larger allocation and silently read wrong data",
85            desc.ndim,
86            crate::TensorDesc::MAX_NDIM
87        )));
88    }
89    // A producer advertising a fence we cannot wait on is a correctness
90    // problem, not a compatibility nicety: importing anyway would alias
91    // memory whose contents are still being written by the producer's
92    // device, and the corruption would be timing-dependent.
93    //
94    // `D3D11_TEXTURE` is the one kind with a wait path: its descriptor names
95    // an `ID3D11Fence` plus a value, and `from_d3d11_shared_handle` issues
96    // the GPU-side wait before the import is usable. Every other kind still
97    // has nowhere to wait, so a producer advertising a fence is refused.
98    if desc.flags & crate::protocol::flags::SYNC_PRESENT != 0
99        && desc.kind != crate::protocol::kind::D3D11_TEXTURE
100    {
101        return Err(crate::Error::NotImplemented(
102            "descriptor advertises SYNC_PRESENT, but waiting on a \
103             producer fence is not implemented in this build; importing \
104             would alias memory with device work still in flight"
105                .to_owned(),
106        ));
107    }
108    // The same refusal for a `D3D11_TEXTURE` descriptor that advertises a
109    // fence value with no fence to read it on: `ptr` is where that kind
110    // carries the fence handle, so a null one leaves a completion nobody can
111    // wait on, which is the in-flight-write hazard again rather than a
112    // missing feature.
113    if desc.flags & crate::protocol::flags::SYNC_PRESENT != 0
114        && desc.kind == crate::protocol::kind::D3D11_TEXTURE
115        && desc.ptr.is_null()
116    {
117        return Err(crate::Error::InvalidArgument(
118            "D3D11_TEXTURE descriptor advertises SYNC_PRESENT but carries no \
119             fence handle in `ptr`; the completion it names cannot be waited on"
120                .to_owned(),
121        ));
122    }
123    let dtype = crate::protocol::dtype_to_dtype(desc.dtype).ok_or_else(|| {
124        crate::Error::NotImplemented(format!("tensor interop dtype code {}", desc.dtype))
125    })?;
126    let shape: Vec<usize> = desc.shape().iter().map(|d| *d as usize).collect();
127    Ok((dtype, shape))
128}
129
130/// Restore the producer's format, row pitch and colorimetry onto a
131/// freshly-imported tensor.
132///
133/// Runs after `import_storage` on both backends, over the public setters
134/// (`set_format`, `set_row_stride`, `set_colorimetry`) both already have --
135/// which is what makes it shareable at all.
136fn restore_descriptor_metadata(
137    t: &mut TensorDyn,
138    desc: &TensorDesc,
139    shape: &[usize],
140) -> Result<()> {
141    // `format` is the HAL-aware format code; it can represent every
142    // `PixelFormat` (including Planar, which has no FourCC). Fall back
143    // to `fourcc` only when the producer left `format` at NONE, for
144    // third-party/DRM-only producers that never set it.
145    let format = if desc.format != crate::protocol::format::NONE {
146        crate::protocol::format_from_code(desc.format)
147    } else if desc.fourcc != 0 {
148        crate::PixelFormat::from_fourcc(desc.fourcc)
149    } else {
150        None
151    };
152    if let Some(fmt) = format {
153        t.set_format(fmt)?;
154        restore_imported_row_stride(t, desc, shape, fmt);
155    }
156
157    // Colorimetry does not participate in `set_format`'s validation, so
158    // this can be unconditional: `0` unpacks to all-`None`, matching a
159    // freshly-imported tensor's default and costing nothing extra.
160    t.set_colorimetry(if desc.colorimetry == 0 {
161        None
162    } else {
163        Some(crate::Colorimetry::unpack(desc.colorimetry))
164    });
165
166    restore_d3d11_logical_shape(t, desc, shape)?;
167
168    Ok(())
169}
170
171/// Give a `kind::D3D11_TEXTURE` import the shape its producer had.
172///
173/// The import opens the texture at the format's *allocation* shape, because
174/// that is what `from_d3d11_shared_handle` builds from width and height. A
175/// producer that called `set_logical_shape` was carrying the *addressing*
176/// shape instead (`[h, w]` for a semi-planar image rather than
177/// `[combined_h, w]`), and the descriptor faithfully reported it; without
178/// this the consumer would see a shape its producer did not have. The same
179/// narrowing rebuilds a `view()`: its descriptor carries the window's shape,
180/// the import opens the whole texture, and this cuts it down to the window
181/// (`Tensor::set_logical_shape` keeps the texture's pitch as the row stride
182/// while it does, or `view()`'s tight stride for a single-row window, so
183/// the window maps in the texture's last row). Where the window sits is the
184/// plane offset, which the
185/// descriptor cannot carry and the capsule restores afterwards. The import
186/// arm has already checked `shape` is one of those three spellings of the
187/// geometry the texture itself reports, so nothing untrusted reaches
188/// `set_logical_shape` here.
189///
190/// After the format restore, not before: `set_format` validates the shape it
191/// finds, and an addressing shape is not one it accepts for a semi-planar
192/// format (`[481, 640]` is an unreachable combined-plane height).
193///
194/// No other kind needs this. Every one of them is imported at exactly the
195/// shape the descriptor carries.
196fn restore_d3d11_logical_shape(
197    t: &mut TensorDyn,
198    desc: &TensorDesc,
199    shape: &[usize],
200) -> Result<()> {
201    if desc.kind != crate::protocol::kind::D3D11_TEXTURE || t.shape() == shape {
202        return Ok(());
203    }
204    t.set_logical_shape(shape)
205}
206
207/// Restore the producer's physical row pitch (the row dimension
208/// convention documented on `protocol::from_parts`) so a decode
209/// that reconfigures this import within its allocation --
210/// `configure_image`'s pool-reuse path -- keeps writing at the
211/// producer's real stride instead of recomputing a tighter one
212/// that only fits today's logical shape. Without this, `capacity`
213/// alone is not enough: `configure_image` only *prefers* a prior
214/// stride when one is recorded, so a fresh import with no stride
215/// still picks a freshly-computed (and possibly narrower) pitch.
216///
217/// `HOST` and `DMABUF`. Historical note: restoring a stride for an
218/// imported `DMABUF` used to be actively harmful -- `Tensor::map`'s
219/// strided path rejected any imported (non-self-allocated) DMA-BUF
220/// outright, so recording one here would have made a foreign
221/// DMA-BUF's CPU map fail with `InvalidOperation`. That was fixed
222/// at the map site instead (a strided imported DMA-BUF is now
223/// accepted there, bounds-checked against `buf_size` exactly like
224/// a self-allocated one is -- `fstat`-derived kernel truth when
225/// the kernel reports a usable size, the declared logical size in
226/// the rare fallback, and either way an out-of-range mmap is still
227/// rejected by the kernel), which is what makes restoring it here
228/// safe: the same argument that justified loosening `Tensor::map`
229/// applies verbatim to the value being restored here -- it is
230/// bounds-checked again by `set_row_stride`'s caller
231/// (`configure_image`, later) and by `Tensor::map` itself at write
232/// time, so an oversized or malformed producer-reported stride
233/// cannot cause an OOB write even though it is trusted without
234/// independent verification.
235/// Confirmed load-bearing, not just anticipated: a pool-sized
236/// (oversized) DMA-BUF-backed destination decoding a smaller
237/// image lost its real pitch without this, recomputing a
238/// tighter one instead -- silent misalignment for any GPU
239/// consumer reading at the true (wider) physical pitch.
240///
241/// `IOSURFACE` is included for the same reason `DMABUF` is, and with the
242/// same bound. An IOSurface's `bytesPerRow` is a property of the surface
243/// itself -- the same in every process that maps it -- so the producer's
244/// stride is the consumer's, and `IoSurfaceTensor::capacity_bytes` is the
245/// whole surface, so the `stride * rows <= capacity` check below bounds
246/// it the same way. Without it a view of a pitched surface, reopened at
247/// the window's shape, was read at the window's tight row over rows the
248/// surface stores a pitch apart (issue #161; pinned by
249/// `descriptor_import_restores_the_iosurface_pitch_onto_a_view`).
250///
251/// `D3D11_TEXTURE` is deliberately excluded: a texture import has a pitch
252/// of its own -- `from_d3d11_shared_handle` records the one this device's
253/// staging copy reports -- and the producer's is a fact about the
254/// producer's driver, not about the texture as this process sees it.
255/// `PBO` is included for the same reason `DMABUF` and `IOSURFACE` are, and
256/// with the same bound. A GL buffer has no pitch of its own -- it is bytes
257/// -- so the producer's stride is the only description of how its rows are
258/// spaced, and `PboTensor::capacity_bytes` is the whole GL allocation, so
259/// the `stride * rows <= capacity` check below bounds it the same way.
260/// Without it a `view()` of a PBO, whose recorded stride is the PARENT's
261/// pitch, was rebuilt at the window's own tight row and every row after the
262/// first read the wrong columns -- the shear half of issue #162, which the
263/// storage-kind half (a view demoting to a host placeholder) hid until
264/// Stage B fixed it. Excluded before Stage B for a reason that was true
265/// then: `import_descriptor` had no `kind::PBO` arm at all, so no PBO
266/// descriptor ever reached this function.
267///
268/// `CUDA_DEVICE` is excluded because that import does not reconstruct a
269/// strided image today.
270fn restore_imported_row_stride(
271    t: &mut TensorDyn,
272    desc: &TensorDesc,
273    shape: &[usize],
274    fmt: crate::PixelFormat,
275) {
276    if !matches!(
277        desc.kind,
278        crate::protocol::kind::HOST
279            | crate::protocol::kind::DMABUF
280            | crate::protocol::kind::IOSURFACE
281            | crate::protocol::kind::PBO
282    ) {
283        return;
284    }
285    let row_dim = match fmt.layout() {
286        crate::PixelLayout::Planar if desc.ndim as usize >= 3 => 1,
287        _ => 0,
288    };
289    if (desc.ndim as usize) < 2 {
290        return;
291    }
292    // `strides` is signed to leave room for a future flip
293    // (negative stride); nothing produces one today, and
294    // `set_row_stride` has no way to express it, so a
295    // negative value here is simply skipped rather than
296    // reinterpreted as a huge unsigned pitch.
297    let Some(&stride_bytes) = desc.strides().get(row_dim) else {
298        return;
299    };
300    let Ok(stride_bytes) = usize::try_from(stride_bytes) else {
301        return;
302    };
303    // `set_row_stride` enforces only a minimum
304    // (`stride >= min_stride`); it explicitly does no
305    // size validation, by design (it is pure layout
306    // metadata, reused by `strides_follow_row_stride
307    // _not_shape`-style tests that never touch a real
308    // allocation). Import is not that case: `desc` is
309    // an untrusted cross-package payload, and an
310    // oversized stride here would otherwise be
311    // caught only downstream, by whichever consumer
312    // happens to multiply it out first
313    // (`Tensor::map`'s strided path, or
314    // `configure_image`'s own capacity check). The
315    // multiply below is checked -- the descriptor's
316    // stride is untrusted input, so it must not wrap
317    // into a small-looking value that passes a
318    // capacity comparison it should fail.
319    //
320    // `desc.strides()` is in bytes, so there is no
321    // element->byte conversion here (and no second
322    // multiply that could overflow) any more.
323    let rows: usize = match fmt.layout() {
324        crate::PixelLayout::Planar if shape.len() >= 2 => shape[0].saturating_mul(shape[1]),
325        _ => shape.first().copied().unwrap_or(0),
326    };
327    apply_imported_row_stride(t, fmt, stride_bytes, rows);
328}
329
330fn apply_imported_row_stride(
331    t: &mut TensorDyn,
332    fmt: crate::PixelFormat,
333    stride_bytes: usize,
334    rows: usize,
335) {
336    let capacity = t.capacity_bytes();
337    match stride_bytes.checked_mul(rows) {
338        Some(needed) if needed <= capacity => {
339            if let Err(e) = t.set_row_stride(stride_bytes) {
340                log::warn!(
341                    "import_descriptor: producer row_stride \
342                         {stride_bytes} bytes rejected for {fmt:?} \
343                         ({e}); keeping the freshly-imported tight \
344                         pitch instead"
345                );
346            }
347        }
348        Some(needed) => {
349            log::warn!(
350                "import_descriptor: producer row_stride \
351                     {stride_bytes} bytes × {rows} rows needs {needed} \
352                     bytes, exceeding this import's capacity \
353                     {capacity}; keeping the freshly-imported tight \
354                     pitch instead of trusting an oversized descriptor"
355            );
356        }
357        None => {
358            log::warn!(
359                "import_descriptor: producer row_stride \
360                     {stride_bytes} bytes × {rows} rows overflows \
361                     usize computing bytes needed; keeping the \
362                     freshly-imported tight pitch instead of trusting \
363                     a malformed descriptor"
364            );
365        }
366    }
367}