Skip to content

JIT: Give up parsing array addresses with a non-multiple constant offset - #135852

Open
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:fix-jit-134192
Open

EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:fix-jit-134192

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

ParseArrayAddress accumulates the address offset with wrapping arithmetic, so a[x + C] with a 3-byte element can end up as x * 3 + 2, whose constant part is not a multiple of the element size. The assert fired, and without it the truncated division would produce a wrong index VN. Bail out (unknown array address) instead.

Fixes #134192

No diffs

ParseArrayAddress accumulates the address offset with wrapping arithmetic, so a[x + C] with a 3-byte element can end up as x * 3 + 2, whose constant part is not a multiple of the element size. The assert fired, and without it the truncated division would produce a wrong index VN. Bail out (unknown array address) instead.

Fixes dotnet#134192

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 88502efb-96e2-4483-b698-59f5003a2e2c
@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 Oct 15, 2026
@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.

@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 narrow guard follows the existing fallback contract, and the regression test targets the reported failure.

0 open findings

What changed in this PR

Prevents incorrect JIT array-index value numbering when a wrapped constant offset is not divisible by the element size, addressing #134192.

Changes:

  • Replaces the divisibility assertion with a conservative unknown-address fallback.
  • Adds a regression test using a three-byte array element and a large index constant.

Builds and tests were not run during this review.

File Description
src/​tests/​JIT/​Regression_ro_2/​Runtime_134192.cs Adds the reported regression scenario.
src/​coreclr/​jit/​gentree.cpp Rejects non-divisible offsets before computing the constant index.

🧠 Review effort: Balanced

@EgorBo

EgorBo commented Oct 15, 2026

Copy link
Copy Markdown
Member Author

PTAL @dotnet/jit-contrib, trivial change, no diffs

// This should be divisible by the element size...
assert((constIndexOffset % elemSize) == 0);
// The offset was accumulated with wrapping arithmetic, e.g. (x + C) * 3 becomes x * 3 + 3C where 3C may wrap
// to a value that isn't a multiple of the element size. We can't recover the constant index then, give up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the value is a multiple of the element size, we won't be able to recover the exact original index either. What makes it work in that case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jkotas This is only used for VN phase. If the function fails to extract the components, we conservatively assign a fresh, unique VN to the array address, so we only lose optimization.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"We can't recover the constant index then, give up." part of the comment looks confusing to me. If there is a wrap around, you cannot recover the original constant index, irrespective of what the element size is,

What happens if there is wraparound, the bogus value happens to be a multiple of element size, and we extract a bogus constant index here?

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.

Yeah, I do think we need to account for the case where there is wraparound that is a multiple of the elem size. Could we try and detect if overflow occurred in ParseArrayAddressWork?

This branch has not been deployed

No deployments
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) Assert (constIndexOffset % elemSize) == 0 in GenTreeArrAddr::ParseArrayAddress for 3-byte element arrays

4 participants