Skip to content

JIT: correct assertion for negative constant array dimensions - #133578

Open
EgorBo with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-negative-constant-dimension
Open

EgorBo with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-negative-constant-dimension

Conversation

Copilot AI commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Calls such as array.GetLength(-1) trigger a Checked/Debug JIT assertion instead of throwing IndexOutOfRangeException.

  • Importer: Assert signed int representability, preserving the existing unsigned bounds check and Release behavior.
  • Regression coverage: Extend existing multidimensional-array tests with -1 and int.MinValue for GetLength, GetLowerBound, and GetUpperBound.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
16 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: @agocke
See info in area-owners.md if you want to be subscribed.

Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix assertion failure when passing negative dimensions to Array.GetLength JIT: correct assertion for negative constant array dimensions Sep 10, 2026
Copilot AI requested a review from EgorBo September 10, 2026 10:04
@EgorBo
EgorBo marked this pull request as ready for review September 10, 2026 10:16
Copilot AI lite review requested due to automatic review settings September 10, 2026 10:16
@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.

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 to the failing assertion and adds focused regression coverage for the negative-constant cases that previously asserted in Checked/Debug.

Pull request overview

This PR fixes a Checked/Debug JIT importer assertion that incorrectly fires when System.Array.GetLength/GetLowerBound/GetUpperBound is called with a negative constant dimension (e.g., -1), ensuring the call behaves like Release builds and throws IndexOutOfRangeException instead of asserting.

Changes:

  • JIT importer: change the debug assertion to validate signed int representability of the constant dimension (so negative constants no longer trip the assertion).
  • Tests: extend an existing multidimensional-array regression test to cover -1 and int.MinValue for GetLength, GetLowerBound, and GetUpperBound.
File summaries
File Description
src/coreclr/jit/importercalls.cpp Adjusts importer assertion for constant MD-array dimension arguments to avoid asserting on negative constants while preserving the existing unsigned rank bounds check.
src/tests/JIT/Regression/JitBlue/Runtime_60957/Runtime_60957.cs Adds xUnit coverage ensuring negative constant dimensions throw IndexOutOfRangeException for GetLength/GetLowerBound/GetUpperBound.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

@EgorBo

EgorBo commented Sep 10, 2026

Copy link
Copy Markdown
Member

PTAL @dotnet/jit-contrib trivial fix of a bad assert

@EgorBo
EgorBo requested a review from a team September 10, 2026 14:59
// is `int` sized.
INT64 dimValue = gtDim->AsIntConCommon()->IntegralValue();
assert((unsigned int)dimValue == dimValue);
assert((int)dimValue == dimValue);

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.

Does the (unsigned int)dimValue below need to be changed?

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.

Well, the code can be rewritten, but currently it's correct, (unsigned int)dimValue (for negative dimValue) overflows and doesn't pass the rank check

@EgorBo

EgorBo commented Sep 17, 2026

Copy link
Copy Markdown
Member

@copilot fix merge conflict.

…constant-dimension

Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 17, 2026 20:41

Copilot AI commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix merge conflict.

Resolved in 7ef7939 by merging main, preserving the assertion fix and tests at their relocated path. Debug build and all 3 targeted tests passed.

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 fix is narrowly scoped, preserves the existing bounds check, and includes targeted coverage for the reported assertion failure.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) a negative constant dimension passed to Array.GetLength asserts in the importer

4 participants