Skip to content

Commit cf38c44

Browse files
author
Fadi Hanna
authored
Save stack arguments size in InlinedCallFrame.m_Datum on x86 to handle callee's popping of arguments (#33249)
* Save stack arguments size in InlinedCallFrame.m_Datum on x86 to handle callee's popping of arguments
1 parent 73268c7 commit cf38c44

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/coreclr/src/jit/lower.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,7 @@ GenTree* Lowering::LowerDelegateInvoke(GenTreeCall* call)
31723172
#else // !TARGET_X86
31733173
// In case of helper dispatched tail calls, "thisptr" will be the third arg.
31743174
// The first two args are: real call target and addr of args copy routine.
3175-
const unsigned argNum = 2;
3175+
const unsigned argNum = 2;
31763176
#endif // !TARGET_X86
31773177

31783178
fgArgTabEntry* thisArgTabEntry = comp->gtArgEntryByArgNum(call, argNum);
@@ -3439,7 +3439,7 @@ void Lowering::InsertPInvokeMethodProlog()
34393439
#if defined(TARGET_X86) || defined(TARGET_ARM)
34403440
GenTreeCall::Use* argList = comp->gtNewCallArgs(frameAddr);
34413441
#else
3442-
GenTreeCall::Use* argList = comp->gtNewCallArgs(frameAddr, PhysReg(REG_SECRET_STUB_PARAM));
3442+
GenTreeCall::Use* argList = comp->gtNewCallArgs(frameAddr, PhysReg(REG_SECRET_STUB_PARAM));
34433443
#endif
34443444

34453445
GenTree* call = comp->gtNewHelperCallNode(CORINFO_HELP_INIT_PINVOKE_FRAME, TYP_I_IMPL, argList);
@@ -3620,9 +3620,18 @@ void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
36203620
GenTree* frameAddr =
36213621
new (comp, GT_LCL_VAR_ADDR) GenTreeLclVar(GT_LCL_VAR_ADDR, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar);
36223622

3623+
#if defined(TARGET_X86) && !defined(UNIX_X86_ABI)
3624+
// On x86 targets, PInvoke calls need the size of the stack args in InlinedCallFrame.m_Datum.
3625+
// This is because the callee pops stack arguments, and we need to keep track of this during stack
3626+
// walking
3627+
const unsigned numStkArgBytes = call->fgArgInfo->GetNextSlotNum() * TARGET_POINTER_SIZE;
3628+
GenTree* stackBytes = comp->gtNewIconNode(numStkArgBytes, TYP_INT);
3629+
GenTreeCall::Use* args = comp->gtNewCallArgs(frameAddr, stackBytes);
3630+
#else
3631+
GenTreeCall::Use* args = comp->gtNewCallArgs(frameAddr);
3632+
#endif
36233633
// Insert call to CORINFO_HELP_JIT_PINVOKE_BEGIN
3624-
GenTree* helperCall =
3625-
comp->gtNewHelperCallNode(CORINFO_HELP_JIT_PINVOKE_BEGIN, TYP_VOID, comp->gtNewCallArgs(frameAddr));
3634+
GenTree* helperCall = comp->gtNewHelperCallNode(CORINFO_HELP_JIT_PINVOKE_BEGIN, TYP_VOID, args);
36263635

36273636
comp->fgMorphTree(helperCall);
36283637
BlockRange().InsertBefore(insertBefore, LIR::SeqTree(comp, helperCall));

src/coreclr/src/vm/i386/PInvokeStubs.asm

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ extern @JIT_PInvokeEndRarePath@0:proc
3232

3333
;
3434
; in:
35-
; InlinedCallFrame (ecx) = pointer to the InlinedCallFrame data, including the GS cookie slot (GS cookie right
36-
; before actual InlinedCallFrame data)
37-
;
35+
; InlinedCallFrame (ecx) = pointer to the InlinedCallFrame data, including the GS cookie slot (GS cookie right
36+
; before actual InlinedCallFrame data)
37+
; StackArgumentsSize (edx) = Number of argument bytes pushed on the stack, which will be popped by the callee
3838
;
3939
_JIT_PInvokeBegin@4 PROC public
4040

@@ -46,7 +46,7 @@ _JIT_PInvokeBegin@4 PROC public
4646
lea eax,[??_7InlinedCallFrame@@6B@]
4747
mov dword ptr [ecx], eax
4848

49-
mov dword ptr [ecx + InlinedCallFrame__m_Datum], 0
49+
mov dword ptr [ecx + InlinedCallFrame__m_Datum], edx
5050

5151

5252
mov eax, esp

src/coreclr/src/vm/i386/cgenx86.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,10 @@ void InlinedCallFrame::UpdateRegDisplay(const PREGDISPLAY pRD)
612612
return;
613613
}
614614

615-
DWORD stackArgSize = (DWORD) dac_cast<TADDR>(m_Datum);
615+
DWORD stackArgSize = 0;
616+
617+
#if !defined(UNIX_X86_ABI)
618+
stackArgSize = (DWORD) dac_cast<TADDR>(m_Datum);
616619

617620
if (stackArgSize & ~0xFFFF)
618621
{
@@ -624,6 +627,7 @@ void InlinedCallFrame::UpdateRegDisplay(const PREGDISPLAY pRD)
624627

625628
stackArgSize = pMD->GetStackArgumentSize();
626629
}
630+
#endif
627631

628632
/* The return address is just above the "ESP" */
629633
pRD->PCTAddr = PTR_HOST_MEMBER_TADDR(InlinedCallFrame, this,

0 commit comments

Comments
 (0)