Skip to content

Commit 4d9f4b8

Browse files
committed
Remove SEH interactions between the JIT and the EE.
This change replaces the final uses of SEH between the JIT and the EE with a new method on ICorJitInfo, `ICorJitInfo::runWithErrorTrap`. This method runs a given function under an error trap that prevents recoverable errors from propagating out of the call. This allows the JIT to make calls to JIT/EE interface functions that may throw exceptions without needing to be aware of the EH ABI, exception types, etc. The method returns true if the given function completed successfully and false otherwise. This change deprecates all other SEH-related functionality on the JIT/EE interface (e.g. `ICJI::FilterException` and `ICJI::HandleException`). This change does not completely eliminate SEH over the JIT/EE interface: there are still a handful of places where the JIT expects to be able to catch exceptions thrown by SuperPMI. These uses of SEH will be removed later on. Fixes dotnet/coreclr#3058 and dotnet/coreclr#4130. [tfs-changeset: 1601288] Commit migrated from dotnet/coreclr@8f71b15
1 parent adbb50d commit 4d9f4b8

11 files changed

Lines changed: 160 additions & 32 deletions

File tree

‎src/coreclr/src/inc/corinfo.h‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ TODO: Talk about initializing strutures before use
231231
#if COR_JIT_EE_VERSION > 460
232232

233233
// Update this one
234-
SELECTANY const GUID JITEEVersionIdentifier = { /* 8c8e61ca-2b88-4bc5-b03f-d390acdc7fc3 */
235-
0x8c8e61ca,
236-
0x2b88,
237-
0x4bc5,
238-
{ 0xb0, 0x3f, 0xd3, 0x90, 0xac, 0xdc, 0x7f, 0xc3 }
234+
SELECTANY const GUID JITEEVersionIdentifier = { /* 57813506-0058-41df-8b1b-e0b68c3a9da3 */
235+
0x57813506,
236+
0x58,
237+
0x41df,
238+
{ 0x8b, 0x1b, 0xe0, 0xb6, 0x8c, 0x3a, 0x9d, 0xa3 }
239239
};
240240

241241
#else
@@ -2723,6 +2723,17 @@ class ICorStaticInfo
27232723
virtual void ThrowExceptionForHelper(
27242724
const CORINFO_HELPER_DESC * throwHelper) = 0;
27252725

2726+
#if COR_JIT_EE_VERSION > 460
2727+
// Runs the given function under an error trap. This allows the JIT to make calls
2728+
// to interface functions that may throw exceptions without needing to be aware of
2729+
// the EH ABI, exception types, etc. Returns true if the given function completed
2730+
// successfully and false otherwise.
2731+
virtual bool runWithErrorTrap(
2732+
void (*function)(void*), // The function to run
2733+
void* parameter // The context parameter that will be passed to the function and the handler
2734+
) = 0;
2735+
#endif
2736+
27262737
/*****************************************************************************
27272738
* ICorStaticInfo contains EE interface methods which return values that are
27282739
* constant from invocation to invocation. Thus they may be embedded in

‎src/coreclr/src/jit/compiler.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6456,6 +6456,14 @@ public :
64566456

64576457
bool eeTryResolveToken(CORINFO_RESOLVED_TOKEN* resolvedToken);
64586458

6459+
template<typename ParamType>
6460+
bool eeRunWithErrorTrap(void (*function)(ParamType*), ParamType* param)
6461+
{
6462+
return eeRunWithErrorTrapImp(reinterpret_cast<void (*)(void*)>(function), reinterpret_cast<void*>(param));
6463+
}
6464+
6465+
bool eeRunWithErrorTrapImp(void (*function)(void*), void* param);
6466+
64596467
// Utility functions
64606468

64616469
#if defined(DEBUG)

‎src/coreclr/src/jit/ee_il_dll.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,13 +1227,57 @@ bool Compiler::eeTryResolveToken(CORINFO_RESOLVED_TOKEN* resolvedToken)
12271227
return param.m_success;
12281228
}
12291229

1230+
struct TrapParam
1231+
{
1232+
ICorJitInfo* m_corInfo;
1233+
EXCEPTION_POINTERS m_exceptionPointers;
1234+
1235+
void (*m_function)(void*);
1236+
void* m_param;
1237+
bool m_success;
1238+
};
1239+
1240+
static LONG __EEFilter(PEXCEPTION_POINTERS exceptionPointers, void* param)
1241+
{
1242+
auto* trapParam = reinterpret_cast<TrapParam*>(param);
1243+
trapParam->m_exceptionPointers = *exceptionPointers;
1244+
return trapParam->m_corInfo->FilterException(exceptionPointers);
1245+
}
1246+
1247+
bool Compiler::eeRunWithErrorTrapImp(void (*function)(void*), void* param)
1248+
{
1249+
TrapParam trapParam;
1250+
trapParam.m_corInfo = info.compCompHnd;
1251+
trapParam.m_function = function;
1252+
trapParam.m_param = param;
1253+
trapParam.m_success = true;
1254+
1255+
PAL_TRY(TrapParam*, __trapParam, &trapParam)
1256+
{
1257+
__trapParam->m_function(__trapParam->m_param);
1258+
}
1259+
PAL_EXCEPT_FILTER(__EEFilter)
1260+
{
1261+
trapParam.m_corInfo->HandleException(&trapParam.m_exceptionPointers);
1262+
trapParam.m_success = false;
1263+
}
1264+
PAL_ENDTRY
1265+
1266+
return trapParam.m_success;
1267+
}
1268+
12301269
#else // CORJIT_EE_VER <= 460
12311270

12321271
bool Compiler::eeTryResolveToken(CORINFO_RESOLVED_TOKEN* resolvedToken)
12331272
{
12341273
return info.compCompHnd->tryResolveToken(resolvedToken);
12351274
}
12361275

1276+
bool Compiler::eeRunWithErrorTrapImp(void (*function)(void*), void* param)
1277+
{
1278+
return info.compCompHnd->runWithErrorTrap(function, param);
1279+
}
1280+
12371281
#endif // CORJIT_EE_VER > 460
12381282

12391283
/*****************************************************************************

‎src/coreclr/src/jit/error.cpp‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,6 @@ void notYetImplemented(const char * msg, const char * filename, unsigned line)
196196

197197
#endif // #if !defined(_TARGET_X86_) || !defined(LEGACY_BACKEND)
198198

199-
/*****************************************************************************/
200-
LONG __EEfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam)
201-
{
202-
ErrorTrapParam *pErrorTrapParam = (ErrorTrapParam *)lpvParam;
203-
ICorJitInfo * m_jitInfo = pErrorTrapParam->jitInfo;
204-
pErrorTrapParam->exceptionPointers = *pExceptionPointers;
205-
return m_jitInfo->FilterException(pExceptionPointers);
206-
}
207-
208199
/*****************************************************************************/
209200
LONG __JITfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam)
210201
{

‎src/coreclr/src/jit/error.h‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ struct ErrorTrapParam
2121
ErrorTrapParam() { jitInfo = NULL; }
2222
};
2323

24-
extern LONG __EEfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam);
25-
// Only catch JIT internal errors (will not catch EE generated Errors)
24+
// Only catch JIT internal errors (will not catch EE generated Errors)
2625
extern LONG __JITfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam);
2726

2827
#define setErrorTrap(compHnd, ParamType, paramDef, paramRef) \
@@ -37,20 +36,13 @@ extern LONG __JITfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVO
3736
{ \
3837
ParamType paramDef = __JITpParam->param;
3938

40-
// Catch only JitGeneratedErrors
39+
// Only catch JIT internal errors (will not catch EE generated Errors)
4140
#define impJitErrorTrap() \
4241
} \
4342
PAL_EXCEPT_FILTER(__JITfilter) \
4443
{ \
4544
int __errc = __JITparam.errc; (void) __errc;
4645

47-
// Catch all errors (including recoverable ones from the EE)
48-
#define impErrorTrap() \
49-
} \
50-
PAL_EXCEPT_FILTER(__EEfilter) \
51-
{ \
52-
__JITparam.jitInfo->HandleException(&__JITparam.exceptionPointers);
53-
5446
#define endErrorTrap() \
5547
} \
5648
PAL_ENDTRY

‎src/coreclr/src/jit/flowgraph.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21922,7 +21922,7 @@ void Compiler::fgInvokeInlineeCompiler(GenTreeCall* call,
2192221922
param.fncHandle = fncHandle;
2192321923
param.inlineCandidateInfo = inlineCandidateInfo;
2192421924
param.inlineInfo = &inlineInfo;
21925-
setErrorTrap(info.compCompHnd, Param*, pParam, &param)
21925+
bool success = eeRunWithErrorTrap<Param>([](Param* pParam)
2192621926
{
2192721927
// Init the local var info of the inlinee
2192821928
pParam->pThis->impInlineInitVars(pParam->inlineInfo);
@@ -21988,8 +21988,8 @@ void Compiler::fgInvokeInlineeCompiler(GenTreeCall* call,
2198821988
}
2198921989
}
2199021990
}
21991-
}
21992-
impErrorTrap()
21991+
}, &param);
21992+
if (!success)
2199321993
{
2199421994
#ifdef DEBUG
2199521995
if (verbose)
@@ -22006,7 +22006,6 @@ void Compiler::fgInvokeInlineeCompiler(GenTreeCall* call,
2200622006
inlineResult->NoteFatal(InlineObservation::CALLSITE_COMPILATION_ERROR);
2200722007
}
2200822008
}
22009-
endErrorTrap();
2201022009

2201122010
if (inlineResult->IsFailure())
2201222011
{

‎src/coreclr/src/jit/importer.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15605,7 +15605,7 @@ void Compiler::impCheckCanInline(GenTreePtr call,
1560515605
param.result = inlineResult;
1560615606
param.ppInlineCandidateInfo = ppInlineCandidateInfo;
1560715607

15608-
setErrorTrap(info.compCompHnd, Param *, pParam, &param)
15608+
bool success = eeRunWithErrorTrap<Param>([](Param* pParam)
1560915609
{
1561015610
DWORD dwRestrictions = 0;
1561115611
CorInfoInitClassResult initClassResult;
@@ -15741,12 +15741,11 @@ void Compiler::impCheckCanInline(GenTreePtr call,
1574115741

1574215742
_exit:
1574315743
;
15744-
}
15745-
impErrorTrap()
15744+
}, &param);
15745+
if (!success)
1574615746
{
1574715747
param.result->NoteFatal(InlineObservation::CALLSITE_COMPILATION_ERROR);
1574815748
}
15749-
endErrorTrap();
1575015749
}
1575115750

1575215751
void Compiler::impInlineRecordArgInfo(InlineInfo * pInlineInfo,

‎src/coreclr/src/vm/jitinterface.cpp‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10062,6 +10062,81 @@ DWORD CEEInfo::getJitFlags(CORJIT_FLAGS* jitFlags, DWORD sizeInBytes)
1006210062
return 0;
1006310063
}
1006410064

10065+
/*********************************************************************/
10066+
#if !defined(PLATFORM_UNIX)
10067+
10068+
struct RunWithErrorTrapFilterParam
10069+
{
10070+
ICorDynamicInfo* m_corInfo;
10071+
void (*m_function)(void*);
10072+
void* m_param;
10073+
EXCEPTION_POINTERS m_exceptionPointers;
10074+
};
10075+
10076+
static LONG RunWithErrorTrapFilter(struct _EXCEPTION_POINTERS* exceptionPointers, void* theParam)
10077+
{
10078+
WRAPPER_NO_CONTRACT;
10079+
10080+
auto* param = reinterpret_cast<RunWithErrorTrapFilterParam*>(theParam);
10081+
param->m_exceptionPointers = *exceptionPointers;
10082+
return param->m_corInfo->FilterException(exceptionPointers);
10083+
}
10084+
10085+
#endif // !defined(PLATFORM_UNIX)
10086+
10087+
bool CEEInfo::runWithErrorTrap(void (*function)(void*), void* param)
10088+
{
10089+
// No dynamic contract here because SEH is used
10090+
STATIC_CONTRACT_THROWS;
10091+
STATIC_CONTRACT_GC_TRIGGERS;
10092+
STATIC_CONTRACT_SO_INTOLERANT;
10093+
STATIC_CONTRACT_MODE_PREEMPTIVE;
10094+
10095+
// NOTE: the lack of JIT/EE transition markers in this method is intentional. Any
10096+
// transitions into the EE proper should occur either via the call to
10097+
// `EEFilterException` (which is appropraitely marked) or via JIT/EE
10098+
// interface calls made by `function`.
10099+
10100+
bool success = true;
10101+
10102+
#if !defined(PLATFORM_UNIX)
10103+
10104+
RunWithErrorTrapFilterParam trapParam;
10105+
trapParam.m_corInfo = m_pOverride == nullptr ? this : m_pOverride;
10106+
trapParam.m_function = function;
10107+
trapParam.m_param = param;
10108+
10109+
PAL_TRY(RunWithErrorTrapFilterParam*, pTrapParam, &trapParam)
10110+
{
10111+
pTrapParam->m_function(pTrapParam->m_param);
10112+
}
10113+
PAL_EXCEPT_FILTER(RunWithErrorTrapFilter)
10114+
{
10115+
HandleException(&trapParam.m_exceptionPointers);
10116+
success = false;
10117+
}
10118+
PAL_ENDTRY
10119+
10120+
#else // !defined(PLATFORM_UNIX)
10121+
10122+
// We shouldn't need PAL_TRY on *nix: any exceptions that we are able to catch
10123+
// ought to originate from the runtime itself and should be catchable inside of
10124+
// EX_TRY/EX_CATCH, including emulated SEH exceptions.
10125+
EX_TRY
10126+
{
10127+
function(param);
10128+
}
10129+
EX_CATCH
10130+
{
10131+
success = false;
10132+
}
10133+
EX_END_CATCH(RethrowTerminalExceptions);
10134+
10135+
#endif
10136+
10137+
return success;
10138+
}
10139+
1006510140
/*********************************************************************/
1006610141
IEEMemoryManager* CEEInfo::getMemoryManager()
1006710142
{

‎src/coreclr/src/vm/jitinterface.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,8 @@ class CEEInfo : public ICorJitInfo
10971097

10981098
DWORD getJitFlags(CORJIT_FLAGS* jitFlags, DWORD sizeInBytes);
10991099

1100+
bool runWithErrorTrap(void (*function)(void*), void* param);
1101+
11001102
private:
11011103
// Shrinking these buffers drastically reduces the amount of stack space
11021104
// required for each instance of the interpreter, and thereby reduces SOs.

‎src/coreclr/src/zap/zapinfo.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,11 @@ IEEMemoryManager* ZapInfo::getMemoryManager()
867867
{
868868
return GetEEMemoryManager();
869869
}
870+
871+
bool ZapInfo::runWithErrorTrap(void (*function)(void*), void* param)
872+
{
873+
return m_pEEJitInfo->runWithErrorTrap(function, param);
874+
}
870875

871876
HRESULT ZapInfo::allocBBProfileBuffer (
872877
ULONG cBlock,

0 commit comments

Comments
 (0)