Commit b429dff
Fix Inverse kernel rank underflow before indexing trailing dims (microsoft#28400)
### 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.
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Co-authored-by: Tianlei Wu <tlwu@microsoft.com>1 parent eb63cc6 commit b429dff
3 files changed
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
| 145 | + | |
| 146 | + | |
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
89 | 104 | | |
90 | 105 | | |
0 commit comments