Skip to content

JIT: Report precise exceptions for non-IndexOutOfRange bounds checks - #137649

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-137637-boundschk-exceptions
Nov 17, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-137637-boundschk-exceptions

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 16, 2026

Copy link
Copy Markdown
Member

GenTree::OperExceptions reported IndexOutOfRangeException for every GT_BOUNDS_CHECK, ignoring gtThrowKind. Bounds checks created for SIMD/HW intrinsics (e.g. Vector128<T>.GetElement(int) or out-of-range immediates) use SCK_ARG_RNG_EXCPN and throw ArgumentOutOfRangeException instead. Consumers that allow reordering trees throwing "the same single exception" (CallArgs::ArgsComplete, forward substitution, LIR side effect sets) could therefore reorder such a check with a real array/string bounds check, changing which exception is observed. E.g. in Callee("abc"[i], v.GetElement(j) * ...) the call args got sorted so that GetElement(j) threw ArgumentOutOfRangeException before "abc"[i] threw IndexOutOfRangeException.

Add ArgumentException and ArgumentOutOfRangeException to ExceptionSetFlags and map each bounds check throw kind to its precise exception. The debug-code check in ArgsComplete that treats inline-throwing args like calls is updated to keep covering these bounds checks.

Fixes #137637

`GenTree::OperExceptions` reported `IndexOutOfRangeException` for every `GT_BOUNDS_CHECK`, ignoring `gtThrowKind`. Bounds checks created for SIMD/HW intrinsics (e.g. `Vector128<T>.GetElement(int)` or out-of-range immediates) use `SCK_ARG_RNG_EXCPN` and throw `ArgumentOutOfRangeException` instead. Consumers that allow reordering trees throwing "the same single exception" (`CallArgs::ArgsComplete`, forward substitution, LIR side effect sets) could therefore reorder such a check with a real array/string bounds check, changing which exception is observed. E.g. in `Callee("abc"[i], v.GetElement(j) * ...)` the call args got sorted so that `GetElement(j)` threw `ArgumentOutOfRangeException` before `"abc"[i]` threw `IndexOutOfRangeException`.

Add `ArgumentException` and `ArgumentOutOfRangeException` to `ExceptionSetFlags` and map each bounds check throw kind to its precise exception. The debug-code check in `ArgsComplete` that treats inline-throwing args like calls is updated to keep covering these bounds checks.

Fixes dotnet#137637

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3e6c4fb6-77fd-4a5a-9011-1cf5506af1bb
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@EgorBo
EgorBo merged commit a134a71 into dotnet:main Nov 17, 2026
126 of 130 checks passed
@EgorBo
EgorBo deleted the fix-137637-boundschk-exceptions branch November 17, 2026 16:03
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) SIMD GetElement(int) range check is reordered before an earlier-evaluated "abc"[i] bounds check in call arguments

2 participants