Skip to content

Fix 4 bugs in TypePreinit IL interpreter - #125947

Merged
MichalStrehovsky merged 1 commit into
dotnet:mainfrom
MichalStrehovsky:fix/typepreinit-bugs
Mar 23, 2026
Merged

MichalStrehovsky merged 1 commit into
dotnet:mainfrom
MichalStrehovsky:fix/typepreinit-bugs

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Asked Opus 4.6 to review TypePreinit.cs for correctness. It found bugs. It fixed bugs. It almost wrote good tests (couldn't make a good one for TryLoadElement so that one is mine)

They're not very concerning, but bugs nevertheless.

  • Fix ceq opcode for float NaN: use IEEE 754 equality instead of byte-wise comparison so NaN == NaN correctly returns false
  • Fix TryLoadElement off-by-one bounds check: change > to >= to match TryStoreElement and prevent out-of-bounds access at index == Length
  • Fix int.MinValue / -1 and long.MinValue / -1 compiler crash: return Status.Fail for signed division/remainder overflow instead of letting OverflowException propagate and crash the AOT compiler
  • Add shl to Int64 arithmetic switch to prevent NotImplementedException crash when both operands normalize to Int64 (e.g. nint << nint)

- Fix ceq opcode for float NaN: use IEEE 754 equality instead of
  byte-wise comparison so NaN == NaN correctly returns false
- Fix TryLoadElement off-by-one bounds check: change > to >= to match
  TryStoreElement and prevent out-of-bounds access at index == Length
- Fix int.MinValue / -1 and long.MinValue / -1 compiler crash: return
  Status.Fail for signed division/remainder overflow instead of letting
  OverflowException propagate and crash the AOT compiler
- Add shl to Int64 arithmetic switch to prevent NotImplementedException
  crash when both operands normalize to Int64 (e.g. nint << nint)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@MichalStrehovsky
MichalStrehovsky requested a review from sbomer March 23, 2026 07:16
Copilot AI review requested due to automatic review settings March 23, 2026 07:16
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
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.

Pull request overview

This PR fixes multiple correctness issues in the NativeAOT TypePreinit IL interpreter to avoid incorrect constant-folding and prevent AOT compiler crashes during preinitialization.

Changes:

  • Fix ceq behavior for floating-point NaN comparisons to follow IEEE 754 equality semantics.
  • Fix an off-by-one bounds check in TryLoadElement to prevent out-of-range reads at index == Length.
  • Prevent AOT compiler crashes on signed division/remainder overflow (MinValue / -1) and implement missing shl handling for Int64-normalized operands.
  • Add new smoke tests covering array load bounds, float NaN comparison, and division overflow scenarios.

Reviewed changes

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

File Description
src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs Adds new smoke tests that validate the fixed interpreter behaviors during type preinitialization.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs Fixes float ceq, signed div/rem overflow handling, adds Int64 shl, and corrects array element load bounds checking.

Comment thread src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g timeout in unrelated test

@MichalStrehovsky
MichalStrehovsky merged commit e861dc8 into dotnet:main Mar 23, 2026
111 of 115 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the fix/typepreinit-bugs branch March 23, 2026 21:40
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants