Skip to content

Emit conv.i for pointer constants in instantiating and unboxing IL stubs - #137931

Merged
lewing merged 2 commits into
dotnet:mainfrom
lewing:lewing-interp-widen-i4-calli-target
Nov 20, 2026
Merged

lewing merged 2 commits into
dotnet:mainfrom
lewing:lewing-interp-widen-i4-calli-target

Conversation

@lewing

@lewing lewing commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

EmitLDC(DWORD_PTR) emits ldc.i8 on 64-bit, which the stub linker lowers to ldc.i4 when the value fits in 32 bits. The unboxing and instantiating IL stubs pushed their calli target and generic context this way without conv.i, so the consumer saw an int32 rather than a native int. On wasm64, where most pointers fit in 32 bits, the interpreter then left the upper half of the slot stale and crashed. Follow the constants with conv.i, as the other IL stubs do.

Note

This PR description was generated with GitHub Copilot.

…ontexts

On 64-bit targets the interpreter used an int32 stack value directly as the
native int calli target or SetNextCallGenericContext argument, leaving the
upper half of the slot stale. Widen it as other native int consumers do.

Instantiating and unboxing IL stubs hit this: the stub linker lowers their
pointer constants to ldc.i4 when they fit in 32 bits. That is common on
wasm64, where it crashed on the first shared generic method called through
such a stub.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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, @BrzVlad, @janvorli
See info in area-owners.md if you want to be subscribed.

@jakobbotsch

Copy link
Copy Markdown
Member

the stub linker lowers their pointer constants to ldc.i4 when they fit in 32 bits

This sounds like a bug in the stub linker. int32 and int64 are not interchangable in IL.

EmitLDC(DWORD_PTR) emits ldc.i8 on 64-bit, which the stub linker lowers to
ldc.i4 when the value fits in 32 bits. The calli target and the
SetNextCallGenericContext argument then reach their consumer as int32,
not native int. Follow the pointer constant with conv.i like the other
IL stubs do, and revert the interpreter-side widening.

Co-authored-by: Copilot App <[email protected]>
@lewing lewing changed the title Interpreter: Widen int32 operands used as calli targets and generic contexts Emit conv.i for pointer constants in instantiating and unboxing IL stubs Nov 20, 2026
@lewing

lewing commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

This sounds like a bug in the stub linker. int32 and int64 are not interchangable in IL.

@jakobbotsch Agreed, the lowering turns an int64 into an int32, and those aren't interchangeable. I've moved the fix into the stubs: the pointer constants are now followed by conv.i, as the other IL stub sites do, and the interpreter change is reverted.

Note

This reply was generated with GitHub Copilot.

@lewing

lewing commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

I audited the other EmitLDC sites in src/coreclr/vm for the same issue. Two more pointer constants in the JIT's async resumption stub (CEEJitInfo::getAsyncResumptionStub) have no conv.i:

Only the JIT consumes this stub; the interpreter uses CInterpreterJitInfo::getAsyncResumptionStub, which generates no IL. The JIT accepts an int32 in both places: its calli import asserts TYP_I_IMPL || TYP_INT with a comment citing LowerOpcode, and its ldind import sign-extends int32 addresses. So I left them out of this PR. Every other site is either followed by conv.i already, or is a small offset, flag, size or token where an int32 is valid.

Note

This comment was generated with GitHub Copilot.

@lewing
lewing requested review from jakobbotsch and a balanced review from Copilot November 20, 2026 03:15

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

No active review issues are reported for the IL-stub pointer-typing changes.

No open issues 🎉

What changed in this PR

This PR corrects pointer typing in CoreCLR-generated IL stubs by emitting conv.i after pointer constants, addressing the reported wasm64 interpreter crash when those constants are encoded as 32-bit integers.

Changes:

  • Converts call targets to native integers in unboxing and instantiating stubs.
  • Converts the hidden generic-context argument to a native integer before passing it to the runtime helper.
File Description
src/​coreclr/​vm/​prestub.cpp Adds three EmitCONV_I() calls after pointer constants in the IL stub generators.

Review effort: Balanced

@BrzVlad

BrzVlad commented Nov 20, 2026

Copy link
Copy Markdown
Member

I believe the initial state of the PR was ok to match JIT behavior, allowing implicit conversion from int32 to ftnptr (int32 to nint should already be the case according to the spec).

I believe @jakobbotsch comment is about not blindly transforming ldc.i8 into ldc.i4 in general. This PR still allows this transformation to happen and then works around it by emitting a conv.i to make it back to i8, which seems weird.

@jakobbotsch jakobbotsch left a comment

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.

This looks good to me, and I believe other places use this pattern too.

@lewing
lewing merged commit e18acd0 into dotnet:main Nov 20, 2026
100 of 102 checks passed
@jakobbotsch

Copy link
Copy Markdown
Member

I believe the initial state of the PR was ok to match JIT behavior, allowing implicit conversion from int32 to ftnptr (int32 to nint should already be the case according to the spec).

I agree, matching JIT behavior here also sounds fine to me. But it's not totally clear to me from the spec that this implicit coercion for the target to calli is allowed, the implicit coercions are normally only for arguments.

I believe @jakobbotsch comment is about not blindly transforming ldc.i8 into ldc.i4 in general. This PR still allows this transformation to happen and then works around it by emitting a conv.i to make it back to i8, which seems weird.

Yeah, if the stub linker is doing that it seems odd. But I see we have a large number of places using EmitLDC with int32 constants that expect this rewrite to happen, so this may take a bit of refactoring. Let me try to submit a change that changes all these places and gets rid of the odd looking CONV_I in these places.

@lewing

lewing commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

I believe the initial state of the PR was ok to match JIT behavior, allowing implicit conversion from int32 to ftnptr (int32 to nint should already be the case according to the spec).

I believe @jakobbotsch comment is about not blindly transforming ldc.i8 into ldc.i4 in general. This PR still allows this transformation to happen and then works around it by emitting a conv.i to make it back to i8, which seems weird.

yeah, it is clear the jit does this and it is also clear that most stubs add the conv_i making a call on what is the "correct" thing to do can happen separate from fixing the immediate problem.

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.

4 participants