Validate constant tensor byte size in DML OnnxTensorWrapper - #31665
Merged
Dwayne Robinson (fdwr) merged 2 commits intoAug 8, 2026
Merged
Dwayne Robinson (fdwr) merged 2 commits into
Dwayne Robinson (fdwr) merged 2 commits into
Conversation
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>
Contributor
There was a problem hiding this comment.
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
VerifyTensorProtoFitsInBufferand call it fromOnnxTensorWrapperconstruction 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
ConstantOfShapeattribute tensors with truncatedraw_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
Dwayne Robinson (fdwr)
left a comment
Contributor
There was a problem hiding this comment.
Thanks for securing.
- 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>
Dwayne Robinson (fdwr)
deleted the
adrastogi/dml-constant-tensor-bounds-validation
branch
August 8, 2026 07:23
This was referenced Aug 12, 2026
This was referenced Aug 27, 2026
This was referenced Sep 3, 2026
This was referenced Sep 10, 2026
Open
This was referenced Sep 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a bounds check to the DML EP's
OnnxTensorWrapperso that aTensorProtowhose declared shape implies more data than its backing buffer actually holds is rejected at construction, instead of being handed to operator kernels.VerifyTensorProtoFitsInBufferinMLOperatorAuthorImpl.cppcomputes the byte size implied by the tensor's dims and element type, and fails withE_INVALIDARGwhen the buffer is smallerOnnxTensorWrapperconstructor, covering theraw_data, typed-field, and external-data pathsonnxruntime/test/providers/dml_onnx_tensor_wrapper_test.ccwith positive / negative cases.Motivation and Context
DML reliability improvement.