Skip to content

Commit 4c07f3d

Browse files
authored
Enable inlining P/Invokes into try blocks with no catch or filter clauses (#73032)
1 parent 614e763 commit 4c07f3d

File tree

6 files changed

+126
-46
lines changed

6 files changed

+126
-46
lines changed

src/coreclr/inc/corinfo.h

+5
Original file line numberDiff line numberDiff line change
@@ -3244,4 +3244,9 @@ class ICorDynamicInfo : public ICorStaticInfo
32443244
//
32453245
#define IMAGE_REL_BASED_REL_THUMB_MOV32_PCREL 0x14
32463246

3247+
/**********************************************************************************/
3248+
#ifdef TARGET_64BIT
3249+
#define USE_PER_FRAME_PINVOKE_INIT
3250+
#endif
3251+
32473252
#endif // _COR_INFO_H_

src/coreclr/jit/importer.cpp

+26-24
Original file line numberDiff line numberDiff line change
@@ -8428,41 +8428,43 @@ bool Compiler::impCanPInvokeInlineCallSite(BasicBlock* block)
84288428
return true;
84298429
}
84308430

8431-
#ifdef TARGET_64BIT
8432-
// On 64-bit platforms, we disable pinvoke inlining inside of try regions.
8433-
// Note that this could be needed on other architectures too, but we
8434-
// haven't done enough investigation to know for sure at this point.
8435-
//
8436-
// Here is the comment from JIT64 explaining why:
8437-
// [VSWhidbey: 611015] - because the jitted code links in the
8438-
// Frame (instead of the stub) we rely on the Frame not being
8439-
// 'active' until inside the stub. This normally happens by the
8440-
// stub setting the return address pointer in the Frame object
8441-
// inside the stub. On a normal return, the return address
8442-
// pointer is zeroed out so the Frame can be safely re-used, but
8443-
// if an exception occurs, nobody zeros out the return address
8444-
// pointer. Thus if we re-used the Frame object, it would go
8445-
// 'active' as soon as we link it into the Frame chain.
8446-
//
8447-
// Technically we only need to disable PInvoke inlining if we're
8448-
// in a handler or if we're in a try body with a catch or
8449-
// filter/except where other non-handler code in this method
8450-
// might run and try to re-use the dirty Frame object.
8451-
//
8452-
// A desktop test case where this seems to matter is
8453-
// jit\jit64\ebvts\mcpp\sources2\ijw\__clrcall\vector_ctor_dtor.02\deldtor_clr.exe
8431+
#ifdef USE_PER_FRAME_PINVOKE_INIT
8432+
// For platforms that use per-P/Invoke InlinedCallFrame initialization,
8433+
// we can't inline P/Invokes inside of try blocks where we can resume execution in the same function.
8434+
// The runtime can correctly unwind out of an InlinedCallFrame and out of managed code. However,
8435+
// it cannot correctly unwind out of an InlinedCallFrame and stop at that frame without also unwinding
8436+
// at least one managed frame. In particular, the runtime struggles to restore non-volatile registers
8437+
// from the top-most unmanaged call before the InlinedCallFrame. As a result, the runtime does not support
8438+
// re-entering the same method frame as the InlinedCallFrame after an exception in unmanaged code.
84548439
if (block->hasTryIndex())
84558440
{
84568441
// This does not apply to the raw pinvoke call that is inside the pinvoke
84578442
// ILStub. In this case, we have to inline the raw pinvoke call into the stub,
84588443
// otherwise we would end up with a stub that recursively calls itself, and end
84598444
// up with a stack overflow.
8445+
// This works correctly because the runtime never emits a catch block in a managed-to-native
8446+
// IL stub. If the runtime ever emits a catch block into a managed-to-native stub when using
8447+
// P/Invoke helpers, this condition will need to be revisited.
84608448
if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB) && opts.ShouldUsePInvokeHelpers())
84618449
{
84628450
return true;
84638451
}
84648452

8465-
return false;
8453+
// Check if this block's try block or any containing try blocks have catch handlers.
8454+
// If any of the containing try blocks have catch handlers,
8455+
// we cannot inline a P/Invoke for reasons above. If the handler is a fault or finally handler,
8456+
// we can inline a P/Invoke into this block in the try since the code will not resume execution
8457+
// in the same method after throwing an exception if only fault or finally handlers are executed.
8458+
for (unsigned int ehIndex = block->getTryIndex(); ehIndex != EHblkDsc::NO_ENCLOSING_INDEX;
8459+
ehIndex = ehGetEnclosingTryIndex(ehIndex))
8460+
{
8461+
if (ehGetDsc(ehIndex)->HasCatchHandler())
8462+
{
8463+
return false;
8464+
}
8465+
}
8466+
8467+
return true;
84668468
}
84678469
#endif // TARGET_64BIT
84688470

src/coreclr/jit/lower.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,7 @@ GenTree* Lowering::CreateFrameLinkUpdate(FrameLinkAction action)
42814281
// Return Value:
42824282
// none
42834283
//
4284+
// See the usages for USE_PER_FRAME_PINVOKE_INIT for more information.
42844285
void Lowering::InsertPInvokeMethodProlog()
42854286
{
42864287
noway_assert(comp->info.compUnmanagedCallCountWithGCTransition);
@@ -4377,13 +4378,16 @@ void Lowering::InsertPInvokeMethodProlog()
43774378
// --------------------------------------------------------
43784379
// On 32-bit targets, CORINFO_HELP_INIT_PINVOKE_FRAME initializes the PInvoke frame and then pushes it onto
43794380
// the current thread's Frame stack. On 64-bit targets, it only initializes the PInvoke frame.
4381+
// As a result, don't push the frame onto the frame stack here for any 64-bit targets
43804382
CLANG_FORMAT_COMMENT_ANCHOR;
43814383

43824384
#ifdef TARGET_64BIT
4385+
#ifdef USE_PER_FRAME_PINVOKE_INIT
4386+
// For IL stubs, we push the frame once even when we're doing per-pinvoke init.
43834387
if (comp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB))
4388+
#endif // USE_PER_FRAME_PINVOKE_INIT
43844389
{
4385-
// Push a frame - if we are NOT in an IL stub, this is done right before the call
4386-
// The init routine sets InlinedCallFrame's m_pNext, so we just set the thead's top-of-stack
4390+
// Push a frame. The init routine sets InlinedCallFrame's m_pNext, so we just set the thread's top-of-stack
43874391
GenTree* frameUpd = CreateFrameLinkUpdate(PushFrame);
43884392
firstBlockRange.InsertBefore(insertionPoint, LIR::SeqTree(comp, frameUpd));
43894393
ContainCheckStoreIndir(frameUpd->AsStoreInd());
@@ -4443,9 +4447,10 @@ void Lowering::InsertPInvokeMethodEpilog(BasicBlock* returnBB DEBUGARG(GenTree*
44434447
// this in the epilog for IL stubs; for non-IL stubs the frame is popped after every PInvoke call.
44444448
CLANG_FORMAT_COMMENT_ANCHOR;
44454449

4446-
#ifdef TARGET_64BIT
4450+
#ifdef USE_PER_FRAME_PINVOKE_INIT
4451+
// For IL stubs, we push the frame once even when we're doing per-pinvoke init
44474452
if (comp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB))
4448-
#endif // TARGET_64BIT
4453+
#endif // USE_PER_FRAME_PINVOKE_INIT
44494454
{
44504455
GenTree* frameUpd = CreateFrameLinkUpdate(PopFrame);
44514456
returnBlockRange.InsertBefore(insertionPoint, LIR::SeqTree(comp, frameUpd));
@@ -4601,7 +4606,7 @@ void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
46014606
// contains PInvokes; on 64-bit targets this is necessary in non-stubs.
46024607
CLANG_FORMAT_COMMENT_ANCHOR;
46034608

4604-
#ifdef TARGET_64BIT
4609+
#ifdef USE_PER_FRAME_PINVOKE_INIT
46054610
if (!comp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB))
46064611
{
46074612
// Set the TCB's frame to be the one we just created.
@@ -4613,7 +4618,7 @@ void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
46134618
BlockRange().InsertBefore(insertBefore, LIR::SeqTree(comp, frameUpd));
46144619
ContainCheckStoreIndir(frameUpd->AsStoreInd());
46154620
}
4616-
#endif // TARGET_64BIT
4621+
#endif // USE_PER_FRAME_PINVOKE_INIT
46174622

46184623
// IMPORTANT **** This instruction must be the last real instruction ****
46194624
// It changes the thread's state to Preemptive mode
@@ -4679,7 +4684,7 @@ void Lowering::InsertPInvokeCallEpilog(GenTreeCall* call)
46794684
// this happens after every PInvoke call in non-stubs. 32-bit targets instead mark the frame as inactive.
46804685
CLANG_FORMAT_COMMENT_ANCHOR;
46814686

4682-
#ifdef TARGET_64BIT
4687+
#ifdef USE_PER_FRAME_PINVOKE_INIT
46834688
if (!comp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB))
46844689
{
46854690
tree = CreateFrameLinkUpdate(PopFrame);
@@ -4703,7 +4708,7 @@ void Lowering::InsertPInvokeCallEpilog(GenTreeCall* call)
47034708

47044709
BlockRange().InsertBefore(insertionPoint, constantZero, storeCallSiteTracker);
47054710
ContainCheckStoreLoc(storeCallSiteTracker);
4706-
#endif // TARGET_64BIT
4711+
#endif // USE_PER_FRAME_PINVOKE_INIT
47074712
}
47084713

47094714
//------------------------------------------------------------------------

src/coreclr/vm/exceptionhandling.cpp

+30-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "virtualcallstub.h"
1717
#include "utilcode.h"
1818
#include "interoplibinterface.h"
19+
#include "corinfo.h"
1920

2021
#if defined(TARGET_X86)
2122
#define USE_CURRENT_CONTEXT_IN_FILTER
@@ -1776,8 +1777,10 @@ CLRUnwindStatus ExceptionTracker::ProcessOSExceptionNotification(
17761777
// InlinedCallFrames (ICF) are allocated, initialized and linked to the Frame chain
17771778
// by the code generated by the JIT for a method containing a PInvoke.
17781779
//
1779-
// JIT generates code that links in the ICF at the start of the method and unlinks it towards
1780-
// the method end. Thus, ICF is present on the Frame chain at any given point so long as the
1780+
// On platforms where USE_PER_FRAME_PINVOKE_INIT is not defined,
1781+
// the JIT generates code that links in the ICF
1782+
// at the start of the method and unlinks it towards the method end.
1783+
// Thus, ICF is present on the Frame chain at any given point so long as the
17811784
// method containing the PInvoke is on the stack.
17821785
//
17831786
// Now, if the method containing ICF catches an exception, we will reset the Frame chain
@@ -1815,13 +1818,16 @@ CLRUnwindStatus ExceptionTracker::ProcessOSExceptionNotification(
18151818
// below the callerSP for which we will invoke ExceptionUnwind.
18161819
//
18171820
// Thus, ICF::ExceptionUnwind should not do anything significant. If any of these assumptions
1818-
// break, then the next best thing will be to make the JIT link/unlink the frame dynamically.
1821+
// break, then the next best thing will be to make the JIT link/unlink the frame dynamically
18191822
//
1820-
// If the current method executing is from precompiled ReadyToRun code, then the above is no longer
1821-
// applicable because each PInvoke is wrapped by calls to the JIT_PInvokeBegin and JIT_PInvokeEnd
1822-
// helpers, which push and pop the ICF to the current thread. Unlike jitted code, the ICF is not
1823-
// linked during the method prolog, and unlinked at the epilog (it looks more like the X64 case).
1823+
// If the current method executing is from precompiled ReadyToRun code, each PInvoke is wrapped
1824+
// by calls to the JIT_PInvokeBegin and JIT_PInvokeEnd helpers,
1825+
// which push and pop the ICF to the current thread. The ICF is not
1826+
// linked during the method prolog, and unlinked at the epilog.
18241827
// In that case, we need to unlink the ICF during unwinding here.
1828+
// On platforms where USE_PER_FRAME_PINVOKE_INIT is defined, the JIT generates code that links in
1829+
// the ICF immediately before and after a PInvoke in non-IL-stubs, like ReadyToRun.
1830+
// See the usages for USE_PER_FRAME_PINVOKE_INIT for more information.
18251831

18261832
if (fTargetUnwind && (pFrame->GetVTablePtr() == InlinedCallFrame::GetMethodFrameVPtr()))
18271833
{
@@ -1830,8 +1836,12 @@ CLRUnwindStatus ExceptionTracker::ProcessOSExceptionNotification(
18301836
//
18311837
// 1) ICF address is higher than the current frame's SP (which we get from DispatcherContext), AND
18321838
// 2) ICF address is below callerSP.
1833-
if ((GetSP(pDispatcherContext->ContextRecord) < (TADDR)pICF) &&
1834-
((UINT_PTR)pICF < uCallerSP))
1839+
// 3) ICF is active.
1840+
// - IL stubs link the frame in for the whole stub, so if an exception is thrown during marshalling,
1841+
// the ICF will be on the frame chain and inactive.
1842+
if ((GetSP(pDispatcherContext->ContextRecord) < (TADDR)pICF)
1843+
&& ((UINT_PTR)pICF < uCallerSP)
1844+
&& InlinedCallFrame::FrameHasActiveCall(pICF))
18351845
{
18361846
pICFForUnwindTarget = pFrame;
18371847

@@ -1840,9 +1850,18 @@ CLRUnwindStatus ExceptionTracker::ProcessOSExceptionNotification(
18401850
// to the JIT_PInvokeBegin and JIT_PInvokeEnd helpers, which push and pop the ICF on the thread. The
18411851
// ICF is not linked at the method prolog and unlined at the epilog when running R2R code. Since the
18421852
// JIT_PInvokeEnd helper will be skipped, we need to unlink the ICF here. If the executing method
1843-
// has another pinovoke, it will re-link the ICF again when the JIT_PInvokeBegin helper is called
1853+
// has another pinvoke, it will re-link the ICF again when the JIT_PInvokeBegin helper is called.
18441854

1845-
if (ExecutionManager::IsReadyToRunCode(((InlinedCallFrame*)pFrame)->m_pCallerReturnAddress))
1855+
TADDR returnAddress = ((InlinedCallFrame*)pFrame)->m_pCallerReturnAddress;
1856+
#ifdef USE_PER_FRAME_PINVOKE_INIT
1857+
// If we're setting up the frame for each P/Invoke for the given platform,
1858+
// then we do this for all P/Invokes except ones in IL stubs.
1859+
if (!ExecutionManager::GetCodeMethodDesc(returnAddress)->IsILStub())
1860+
#else
1861+
// If we aren't setting up the frame for each P/Invoke (instead setting up once per method),
1862+
// then ReadyToRun code is the only code using the per-P/Invoke logic.
1863+
if (ExecutionManager::IsReadyToRunCode(returnAddress))
1864+
#endif
18461865
{
18471866
pICFForUnwindTarget = pICFForUnwindTarget->Next();
18481867
}

src/coreclr/vm/i386/excepx86.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "eeconfig.h"
2929
#include "vars.hpp"
3030
#include "generics.h"
31+
#include "corinfo.h"
3132

3233
#include "asmconstants.h"
3334
#include "virtualcallstub.h"
@@ -2970,6 +2971,8 @@ void ResumeAtJitEH(CrawlFrame* pCf,
29702971
// Check that the InlinedCallFrame is in the method with the exception handler. There can be other
29712972
// InlinedCallFrame somewhere up the call chain that is not related to the current exception
29722973
// handling.
2974+
2975+
// See the usages for USE_PER_FRAME_PINVOKE_INIT for more information.
29732976

29742977
#ifdef DEBUG
29752978
TADDR handlerFrameSP = pCf->GetRegisterSet()->SP;
@@ -2982,10 +2985,22 @@ void ResumeAtJitEH(CrawlFrame* pCf,
29822985
NULL /* StackwalkCacheUnwindInfo* */);
29832986
_ASSERTE(unwindSuccess);
29842987

2985-
if (((TADDR)pThread->m_pFrame < pCf->GetRegisterSet()->SP) && ExecutionManager::IsReadyToRunCode(((InlinedCallFrame*)pThread->m_pFrame)->m_pCallerReturnAddress))
2988+
if (((TADDR)pThread->m_pFrame < pCf->GetRegisterSet()->SP))
29862989
{
2987-
_ASSERTE((TADDR)pThread->m_pFrame >= handlerFrameSP);
2988-
pThread->m_pFrame->Pop(pThread);
2990+
TADDR returnAddress = ((InlinedCallFrame*)pThread->m_pFrame)->m_pCallerReturnAddress;
2991+
#ifdef USE_PER_FRAME_PINVOKE_INIT
2992+
// If we're setting up the frame for each P/Invoke for the given platform,
2993+
// then we do this for all P/Invokes except ones in IL stubs.
2994+
if (!ExecutionManager::GetCodeMethodDesc(returnAddress)->IsILStub())
2995+
#else
2996+
// If we aren't setting up the frame for each P/Invoke (instead setting up once per method),
2997+
// then ReadyToRun code is the only code using the per-P/Invoke logic.
2998+
if (ExecutionManager::IsReadyToRunCode(returnAddress))
2999+
#endif
3000+
{
3001+
_ASSERTE((TADDR)pThread->m_pFrame >= handlerFrameSP);
3002+
pThread->m_pFrame->Pop(pThread);
3003+
}
29893004
}
29903005
}
29913006

src/tests/baseservices/exceptions/exceptioninterop/ExceptionInterop.cs

+34
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,38 @@ public static void ThrowNativeExceptionAndCatchInFrameWithFinally()
122122

123123
Assert.True(caughtException);
124124
}
125+
126+
[Fact]
127+
[PlatformSpecific(TestPlatforms.Windows)]
128+
[SkipOnMono("Exception interop not supported on Mono.")]
129+
public static void ThrowNativeExceptionInFrameWithFinallyCatchInOuterFrame()
130+
{
131+
bool caughtException = false;
132+
try
133+
{
134+
ThrowInFrameWithFinally();
135+
}
136+
catch
137+
{
138+
caughtException = true;
139+
}
140+
141+
Assert.True(caughtException);
142+
143+
[MethodImpl(MethodImplOptions.NoInlining)]
144+
static void ThrowInFrameWithFinally()
145+
{
146+
try
147+
{
148+
ThrowException();
149+
}
150+
finally
151+
{
152+
// Try calling another P/Invoke in the finally block before the catch
153+
// to make sure we have everything set up
154+
// to recover from the exceptional control flow.
155+
NativeFunction();
156+
}
157+
}
158+
}
125159
}

0 commit comments

Comments
 (0)