Skip to content

Backport: Fix GatherCopyData Integer Truncation Leading to Heap Out-of-Bounds R… - #32490

Merged
hualruba merged 1 commit into
release-os-1.17.2from
user/hualruba/msrc_OCT2016
Sep 9, 2026
Merged

hualruba merged 1 commit into
release-os-1.17.2from
user/hualruba/msrc_OCT2016

Conversation

@hualruba

@hualruba hualruba commented Sep 8, 2026

Copy link
Copy Markdown

backport this PR (#27444) to release-os-1.17.2

This pull request improves the robustness and correctness of the CPU implementation of the Gather operator in ONNX Runtime. The key changes focus on preventing integer overflow issues in parallel processing and output shape calculations, as well as enhancing test coverage to verify these safeguards.

Enhancements to overflow handling and parallel processing:

  • Changed the lambda function in GatherCopyData to use ptrdiff_t instead of int64_t for the index, and explicitly cast batch and i variables, ensuring safer arithmetic for large tensor sizes.
  • Updated the parallel loop in GatherCopyData to iterate using ptrdiff_t indices, preventing potential overflow when processing large tensors.

Testing improvements:

  • Added a new unit test Gather_overflow_check in gather_op_test.cc to verify that the Gather operator correctly handles very large output shapes without overflowing, specifically testing dimensions that exceed the 32-bit integer limit.

Description

Motivation and Context

…ead/Write (#27444)

This pull request improves the robustness and correctness of the CPU
implementation of the Gather operator in ONNX Runtime. The key changes
focus on preventing integer overflow issues in parallel processing and
output shape calculations, as well as enhancing test coverage to verify
these safeguards.

Enhancements to overflow handling and parallel processing:

* Changed the lambda function in `GatherCopyData` to use `ptrdiff_t`
instead of `int64_t` for the index, and explicitly cast batch and i
variables, ensuring safer arithmetic for large tensor sizes.
* Updated the parallel loop in `GatherCopyData` to iterate using
`ptrdiff_t` indices, preventing potential overflow when processing large
tensors.

Testing improvements:

* Added a new unit test `Gather_overflow_check` in `gather_op_test.cc`
to verify that the Gather operator correctly handles very large output
shapes without overflowing, specifically testing dimensions that exceed
the 32-bit integer limit.
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
@hualruba
hualruba merged commit 090fbbf into release-os-1.17.2 Sep 9, 2026
7 of 12 checks passed
@hualruba
hualruba deleted the user/hualruba/msrc_OCT2016 branch September 9, 2026 20:23
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