Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Jan 15, 2026

Fixes #122288

We deal with 1 >= (2147483646 + X) expression. Since getting the range of the whole op2 (2147483646 + X) didn't get us anything, we have this hack where we peel the offset (so 2147483646) and try to find the range of X. (I'll move this logic into ryGetRangeFromAssertions itself in the future).

We end up with [0...65535] range of X which clearly may overflow when 2147483646 is added. The previous logic was trying to just move the constant to the left 1 - 2147483646 >= X where Subtract didn't overflow.

I want to do some refactoring around overflow handling. Today, e.g. for range1=[0..int.MaxValue] and range2=[10..20] the RangeOps::Add(range1, range2) returns [10..unknown], I want it to return [unknown..unknown] (or maybe [-2147483639..0] ?). It seems to be not a problem today except for 2 places in global assertion prop.

This fix doesn't contain the refactoring to be backport-friendly.

A few diffs correctness related.

@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 Jan 15, 2026
@dotnet-policy-service
Copy link
Contributor

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

@EgorBo EgorBo marked this pull request as ready for review January 15, 2026 18:24
Copilot AI review requested due to automatic review settings January 15, 2026 18:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a potential overflow issue in the JIT compiler's assertion propagation optimizer when performing range analysis on relational operators.

Changes:

  • Enhanced the Limit::AddConstant method to optionally report overflow conditions via an out parameter
  • Updated optAssertionProp_RangeProperties to explicitly check for overflow when adding constants to range bounds
  • Fixed optAssertionPropGlobal_RelOp to detect and bail out when adding peeled offsets would overflow range limits
  • Added a regression test that reproduces the overflow scenario

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/coreclr/jit/rangecheck.h Added optional overflows parameter to AddConstant method to report overflow conditions
src/coreclr/jit/assertionprop.cpp Updated two call sites to use explicit overflow detection instead of relying on return values, improving code clarity and correctness
src/tests/JIT/Regression/JitBlue/Runtime_122288/Runtime_122288.cs Added regression test for the overflow scenario with a comparison involving large constants

@EgorBo
Copy link
Member Author

EgorBo commented Jan 15, 2026

PTAL @jakobbotsch @dotnet/jit-contrib

@EgorBo EgorBo requested a review from jakobbotsch January 15, 2026 18:35
@EgorBo
Copy link
Member Author

EgorBo commented Jan 20, 2026

Closed in favor of #123233

@EgorBo EgorBo closed this Jan 20, 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: Bad codegen with comparisons close to int.MaxValue

1 participant