Skip to content

Fix unbounded lifetime on WithOutputTensor in Rust bindings - #29251

Merged
sayanshaw24 merged 1 commit into
mainfrom
sayanshaw/rust-binding-fix
Jun 26, 2026
Merged

Fix unbounded lifetime on WithOutputTensor in Rust bindings#29251
sayanshaw24 merged 1 commit into
mainfrom
sayanshaw/rust-binding-fix

Conversation

@sayanshaw24

Copy link
Copy Markdown
Contributor

Fix unbounded lifetime on WithOutputTensor in Rust bindings

Description

The WithOutputTensor<'a, T> struct had a free lifetime parameter 'a on its TryFrom<OrtOutputTensor> impl that was unconstrained by any input. Combined with the Deref impl (whose Target = ArrayView<'a, T, IxDyn> exposed a Clone-able view), it was possible for the ArrayView to outlive the underlying OrtOutputTensor buffer owner.

This change restructures WithOutputTensor to eliminate the unbounded lifetime:

  • Removes the 'a lifetime parameter from WithOutputTensor, OrtOutput, and Session::run
  • Removes the Deref impl (the escape hatch)
  • Replaces the stored ArrayView<'a, T> with a raw pointer + shape
  • Adds a view(&self) method returning ArrayView<'_, T, IxDyn> — the view lifetime is now tied to &self
  • Updates all call sites (examples, integration tests) to use .view()

Motivation

The C API contract (onnxruntime_c_api.h) explicitly bounds the data pointer lifetime to the OrtValue: the pointer is only valid until the value is destroyed. The Rust type system must enforce this invariant. Previously it did not — the ArrayView could be cloned out and observed after the OrtValue was freed.

API Change

// Before: Deref-based access
let output = outputs[0].float_array().unwrap();
let sum: f32 = output.iter().sum();

// After: explicit view() call
let output = outputs[0].float_array().unwrap();
let sum: f32 = output.view().iter().sum();

Testing

Existing integration tests updated to use the new view() API. The fix is enforced at compile time by the borrow checker — the previously problematic pattern now produces a lifetime error.

@sayanshaw24
sayanshaw24 enabled auto-merge (squash) June 25, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants