Skip to content

[interpreter] Fix crash on empty switch instruction#126017

Open
radekdoulik wants to merge 1 commit intodotnet:mainfrom
radekdoulik:fix-interpreter-empty-switch
Open

[interpreter] Fix crash on empty switch instruction#126017
radekdoulik wants to merge 1 commit intodotnet:mainfrom
radekdoulik:fix-interpreter-empty-switch

Conversation

@radekdoulik
Copy link
Member

Note

This PR description was AI/Copilot-generated.

Summary

The interpreter's CEE_SWITCH handler crashes when compiling a switch instruction with zero targets (e.g., switch () in IL). The handler unconditionally calls allocate(n) with n=0, which triggers assert(size != 0) in the arena allocator.

Fix

Bail out early when n == 0 since an empty switch is a no-op that just consumes the stack value — no INTOP_SWITCH instruction needs to be emitted.

Test

Fixes crash in JIT/Regression/CLR-x86-JIT/V1-M11-Beta1/b44946/b44946.il which contains switch() with zero targets. Verified the Regression_6 merged runner passes: 290 passed, 0 failed.

The CEE_SWITCH handler called allocate(n) with n=0 for switch
instructions with zero targets, triggering an assert in the arena
allocator. Bail out early when n=0 since an empty switch is a no-op
that just consumes the stack value.

Fixes crash in JIT/Regression/CLR-x86-JIT/V1-M11-Beta1/b44946/b44946.il
which contains a switch() with zero targets.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@radekdoulik radekdoulik added this to the Future milestone Mar 24, 2026
Copilot AI review requested due to automatic review settings March 24, 2026 10:05
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
See info in area-owners.md if you want to be subscribed.

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 crash in the CoreCLR interpreter compiler when encountering a CEE_SWITCH IL instruction with zero targets by avoiding a zero-sized allocation during switch table construction.

Changes:

  • Add an early exit in the CEE_SWITCH handler when n == 0 (empty switch), treating it as a stack pop + fallthrough.
  • Prevent getAllocator(...).allocate<T>(0) from being reached for empty switches.

@radekdoulik radekdoulik changed the title Fix interpreter crash on empty switch instruction [interpreter] Fix interpreter crash on empty switch instruction Mar 24, 2026
@radekdoulik radekdoulik changed the title [interpreter] Fix interpreter crash on empty switch instruction [interpreter] Fix crash on empty switch instruction Mar 24, 2026
@radekdoulik radekdoulik enabled auto-merge (squash) March 24, 2026 10:25
Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants