Skip to content

Validate constant tensor byte size in DML OnnxTensorWrapper - #31665

Merged
Dwayne Robinson (fdwr) merged 2 commits into
mainfrom
adrastogi/dml-constant-tensor-bounds-validation
Aug 8, 2026
Merged

Dwayne Robinson (fdwr) merged 2 commits into
mainfrom
adrastogi/dml-constant-tensor-bounds-validation

Conversation

@adrastogi

Copy link
Copy Markdown
Contributor

Description

Adds a bounds check to the DML EP's OnnxTensorWrapper so that a TensorProto whose declared shape implies more data than its backing buffer actually holds is rejected at construction, instead of being handed to operator kernels.

  • New helper VerifyTensorProtoFitsInBuffer in MLOperatorAuthorImpl.cpp computes the byte size implied by the tensor's dims and element type, and fails with E_INVALIDARG when the buffer is smaller
  • Called at the end of the OnnxTensorWrapper constructor, covering the raw_data, typed-field, and external-data paths
  • Adds onnxruntime/test/providers/dml_onnx_tensor_wrapper_test.cc with positive / negative cases.

Motivation and Context

DML reliability improvement.

OnnxTensorWrapper wraps an onnx::TensorProto and hands out a raw pointer
via GetData() together with dimensions from GetShape(). Consumers size
their reads from the shape and the declared element type, never from the
actual size of the backing buffer. When a TensorProto declares dims and a
data type whose implied byte size exceeds the bytes actually present, the
consumer reads past the end of the heap allocation.

For graph initializers this is already mitigated: ONNX shape inference
calls ParseData, which rejects a raw_data field shorter than
product(dims) * sizeof(element). That covers ops whose inference reads
constant inputs (Slice, Pad, Tile, Expand, Reshape, Resize, Split).

Attribute tensors are not covered by any of that. OpNodeInfoWrapper::
GetTensorAttribute builds an OnnxTensorWrapper directly from
AttributeProto::t(). An attribute tensor is never a graph initializer, so
Graph::ConvertInitializersIntoOrtValues and its embedded-data validation
never see it, and ConstantOfShape's ONNX shape inference only reads the
attribute's data_type - never its data. DmlOperatorConstantOfShape then
checks elementCount == 1 from the dims and memcpy's a byte count derived
from the declared data type out of GetData(). A `value` attribute of
{INT64, dims=[1], raw_data = 1 byte} produces a 7-byte heap over-read
whose contents become the GPU fill pattern, and therefore appear directly
in the model output.

Add VerifyTensorProtoFitsInBuffer, called at the end of the
OnnxTensorWrapper constructor so it covers the external-data, raw_data,
and typed-field branches alike. It computes the required byte size with
GetSizeInBytesFromTensorProto<0> and throws E_INVALIDARG when the backing
buffer is smaller. STRING is exempt because its size query returns
elem_count * sizeof(std::string) rather than a serialized byte size, and
the complex types are sized inline because that helper reports them as
NOT_IMPLEMENTED even though ToMLTensorDataType maps them. Every other
size-query failure - unrepresentable dims, byte-count overflow, an
element type this EP does not admit - is treated as malformed rather than
skipped, so the bounds check is total over the types that can reach a
consumer.

The check is a lower bound only. ValidateEmbeddedTensorProtoDataSizeAndShape
was deliberately not reused because it also enforces exact equality and a
2 GiB cap; RegisterDynamicKernel re-packs arbitrarily large weights into
raw_data protos that reach this constructor, so the cap risks regressing
legitimate models.

Tests: onnxruntime/test/providers/dml_onnx_tensor_wrapper_test.cc builds a
ConstantOfShape model in memory with a truncated `value` attribute and
asserts that session initialization now fails; with the constructor check
removed that test fails, confirming the session previously initialized
while over-reading. A positive control loads, initializes, and runs a
well-formed equivalent and checks the output. ConstantFolding is disabled
in both so the node reaches the DML EP instead of being folded on CPU at
Level1. The constructor's typed-field branch is guarded too but has no
test, because every malformed proto that would reach it is rejected
earlier by the ONNX checker or by DmlOperatorConstantOfShape's own
element-count assertion; the file documents this.

Verified with onnxruntime_test_all (1896 tests, 1879 passed, 0 failed) and
onnxruntime_provider_test --gtest_filter=*Dml* (14 passed) on a machine
with a real DirectML device.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the DirectML (DML) execution provider by validating that a TensorProto’s backing buffer is large enough for the element count implied by its declared dimensions, preventing out-of-bounds reads when DML kernels consume attribute tensors via OnnxTensorWrapper.

Changes:

  • Add VerifyTensorProtoFitsInBuffer and call it from OnnxTensorWrapper construction to reject undersized payloads (E_INVALIDARG).
  • Cover raw_data, typed-field, and file-backed external-data TensorProto payload cases.
  • Add a DML-focused regression test that exercises ConstantOfShape attribute tensors with truncated raw_data, plus a positive control.
Show a summary per file
File Description
onnxruntime/test/providers/dml_onnx_tensor_wrapper_test.cc Adds regression + positive-control tests for malformed vs well-formed attribute tensors reaching DML via OnnxTensorWrapper.
onnxruntime/core/providers/dml/DmlExecutionProvider/src/MLOperatorAuthorImpl.cpp Adds buffer-size validation helper and enforces it in OnnxTensorWrapper constructor to prevent OOB reads.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread onnxruntime/core/providers/dml/DmlExecutionProvider/src/MLOperatorAuthorImpl.cpp Outdated

@fdwr Dwayne Robinson (fdwr) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for securing.

Comment thread onnxruntime/core/providers/dml/DmlExecutionProvider/src/MLOperatorAuthorImpl.cpp Outdated
Comment thread onnxruntime/core/providers/dml/DmlExecutionProvider/src/MLOperatorAuthorImpl.cpp Outdated
Comment thread onnxruntime/test/providers/dml_onnx_tensor_wrapper_test.cc Outdated
- Drop the COMPLEX64/COMPLEX128 special case and let
  GetSizeInBytesFromTensorProto be the single source of truth. No DML
  operator registers complex types, so the branch was unreachable: they
  appear in OperatorRegistration.cpp's SupportedTensorDataTypes only as
  unused enum values, and DmlCommon maps both to
  DML_TENSOR_DATA_TYPE_UNKNOWN. Complex protos now fail closed via
  NOT_IMPLEMENTED rather than being sized by hand. The negative-dimension
  guard goes with it, since GetSizeInBytesFromTensorProto already
  rejects negative dims.

- Use uint64_t rather than unsigned long long for the message arguments,
  resolving the cpplint [runtime/int] warnings on both lines.

- Simplify the test's little-endian byte extraction by dropping the
  redundant 0xFF mask; the static_cast<char> already truncates.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@fdwr Dwayne Robinson (fdwr) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fdwr
Dwayne Robinson (fdwr) merged commit 4dfcacc into main Aug 8, 2026
86 of 87 checks passed
@fdwr
Dwayne Robinson (fdwr) deleted the adrastogi/dml-constant-tensor-bounds-validation branch August 8, 2026 07:23
This was referenced Aug 12, 2026
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.

3 participants