Skip to content

Fix Inverse kernel rank underflow before indexing trailing dims#28400

Merged
tianleiwu merged 2 commits into
mainfrom
copilot/fix-inverse-rank-guard
May 15, 2026
Merged

Fix Inverse kernel rank underflow before indexing trailing dims#28400
tianleiwu merged 2 commits into
mainfrom
copilot/fix-inverse-rank-guard

Conversation

Copilot AI commented May 7, 2026

Copy link
Copy Markdown
Contributor

Description

Inverse::Compute (CPU and CUDA) read dims[num_dim - 2] and dims[num_dim - 1] before validating num_dim >= 2. Because num_dim is size_t, scalar or 1D inputs silently underflow the subtraction and produce out-of-bounds indices.

  • contrib_ops/cpu/inverse.cc — add ORT_RETURN_IF_NOT(num_dim >= 2, ...) immediately after shape retrieval, before any dimension indexing.
  • contrib_ops/cuda/inverse.cc — same guard in ComputeInternal.
  • test/contrib_ops/inverse_test.cc — add scalar_input_fails and one_dim_input_fails test cases.

Motivation and Context

Without the guard, passing a 0-D or 1-D tensor to the Inverse op causes size_t underflow on num_dim - 2, reading garbage memory before any validation occurs. The fix is a defense-in-depth kernel-level check that mirrors the existing ONNX shape-inference rejection.

@tianleiwu tianleiwu force-pushed the copilot/fix-inverse-rank-guard branch from 66377d5 to 6b3bcff Compare May 7, 2026 22:04
@tianleiwu tianleiwu marked this pull request as ready for review May 7, 2026 22:04
@tianleiwu tianleiwu merged commit b429dff into main May 15, 2026
97 of 102 checks passed
@tianleiwu tianleiwu deleted the copilot/fix-inverse-rank-guard branch May 15, 2026 23:59
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