Skip to content

JIT: guard negative-offset bounds inference against underflow - #133272

Open
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:fix-133271-negative-offset-assertions
Open

JIT: guard negative-offset bounds inference against underflow#133272
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:fix-133271-negative-offset-assertions

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 4, 2026

Copy link
Copy Markdown
Member

Fixes #133271. Require proof that subtraction cannot underflow before inferring the index bound.

Copilot-generated.

Fixes dotnet#133271.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 56f763ef-1e54-421c-9e79-fe14ed2b906a
Copilot AI lite review requested due to automatic review settings September 4, 2026 22:44
@azure-pipelines

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

@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 Sep 4, 2026
@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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrowly scoped, improves correctness by guarding an unsafe inference, and includes a targeted regression test that would fail without the fix.

Pull request overview

This PR fixes a JIT range-check optimization bug where an unsigned compare on an expression with a negative constant offset could be used to infer an incorrect non-negative lower bound even when the addition/subtraction could underflow and wrap. The change strengthens the inference logic to require proof that the offset addition cannot underflow before using it to derive a lower-bound assertion, and adds a regression test covering the reported scenario.

Changes:

  • Tighten MergeEdgeAssertionsWorker negative-offset inference by requiring a constant lower bound and proving lower + addOpCns cannot overflow/underflow before inferring normalLclVN >= -addOpCns.
  • Add an xUnit regression test for the underflow/wrap case that previously led to a missing IndexOutOfRangeException.
  • Register the new regression test in Regression_ro_2.csproj.
File summaries
File Description
src/coreclr/jit/rangecheck.cpp Adds an underflow-safety precondition before inferring a lower bound from an unsigned compare involving a negative constant offset.
src/tests/JIT/Regression/JitBlue/Runtime_133271/Runtime_133271.cs New regression test validating the bounds check is not incorrectly removed in the underflow/wrap scenario.
src/tests/JIT/Regression/Regression_ro_2.csproj Includes the new test source file in the regression test project.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 7, 2026 11:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The JIT change is a conservative correctness guard and is paired with a focused regression test that would fail under the reported miscompilation.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@EgorBo

EgorBo commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

PTAL @jakobbotsch @dotnet/jit-contrib - a small correctness issue found by AI.

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.

Missing IndexOutOfRangeException in optimized code

2 participants