Skip to content

Use [UN]INSTALL_MANAGED_EXCEPTION_DISPATCHER_EX to backpatch CallDescrWorkerInternal SEH record on x86/funclets #114600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/coreclr/vm/exceptionhandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ ProcessCLRException(IN PEXCEPTION_RECORD pExceptionRecord,
IN OUT PT_CONTEXT pContextRecord,
IN OUT PT_DISPATCHER_CONTEXT pDispatcherContext);

EXTERN_C EXCEPTION_DISPOSITION __cdecl
CallDescrWorkerUnwindFrameChainHandler(IN PEXCEPTION_RECORD pExceptionRecord,
IN PVOID pEstablisherFrame,
IN OUT PT_CONTEXT pContextRecord,
IN OUT PT_DISPATCHER_CONTEXT pDispatcherContext);

VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT *pExceptionContext, EXCEPTION_RECORD *pExceptionRecord = NULL);
VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable);
VOID DECLSPEC_NORETURN DispatchManagedException(RuntimeExceptionKind reKind);
Expand Down
35 changes: 35 additions & 0 deletions src/coreclr/vm/exceptmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,41 @@ VOID DECLSPEC_NORETURN DispatchManagedException(PAL_SEHException& ex, bool isHar
UNREACHABLE(); \
}

#elif defined(TARGET_X86) && defined(TARGET_WINDOWS) && defined(FEATURE_EH_FUNCLETS)

#define INSTALL_MANAGED_EXCEPTION_DISPATCHER
#define UNINSTALL_MANAGED_EXCEPTION_DISPATCHER

#define INSTALL_UNHANDLED_MANAGED_EXCEPTION_TRAP
#define UNINSTALL_UNHANDLED_MANAGED_EXCEPTION_TRAP

// We use [UN]INSTALL_MANAGED_EXCEPTION_DISPATCHER_EX to backpatch the SEH record installed
// in CallDescrWorkerInternal from ProcessCLRException to CallDescrWorkerUnwindFrameChainHandler
// when throwing an exception. This ensures that class loading exceptions are propagated through
// unmanaged code before being forwarded to the managed one.

#define INSTALL_MANAGED_EXCEPTION_DISPATCHER_EX \
try \
{

#define UNINSTALL_MANAGED_EXCEPTION_DISPATCHER_EX(nativeRethrow) \
} \
catch (...) \
{ \
if (nativeRethrow) \
{ \
PEXCEPTION_REGISTRATION_RECORD pExceptionRecord = GetCurrentSEHRecord(); \
_ASSERTE(pExceptionRecord != EXCEPTION_CHAIN_END); \
while (pExceptionRecord->Handler != (PEXCEPTION_ROUTINE)ProcessCLRException) \
{ \
pExceptionRecord = pExceptionRecord->Next; \
_ASSERTE(pExceptionRecord != EXCEPTION_CHAIN_END); \
} \
pExceptionRecord->Handler = (PEXCEPTION_ROUTINE)CallDescrWorkerUnwindFrameChainHandler; \
} \
throw; \
}

#else // TARGET_UNIX

#define INSTALL_MANAGED_EXCEPTION_DISPATCHER
Expand Down
22 changes: 0 additions & 22 deletions src/coreclr/vm/i386/asmhelpers.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1215,30 +1215,8 @@ _ThePreStub@0 proc public

push esi

ifdef FEATURE_EH_FUNCLETS
cmp [esi + 24], CallDescrWorkerInternalReturnAddress
jne NoSEHReplace

; If we were called from CallDescrWorkerInternal then swap the last
; SEH registration for _CallDescrWorkerUnwindFrameChainHandler to ensure
; that class loading exceptions are propagated through unmanaged code
; before being forwarded to the managed one.
mov edi, fs:[0]
; mov esi, [edi]
; mov fs:[0], esi
mov [edi + 4], _CallDescrWorkerUnwindFrameChainHandler
call _PreStubWorker@8
mov [edi + 4], _ProcessCLRException
; mov fs:[0], edi
jmp AfterPreStubWorker

NoSEHReplace:
endif ; FEATURE_EH_FUNCLETS

call _PreStubWorker@8

AfterPreStubWorker:

; eax now contains replacement stub. PreStubWorker will never return
; NULL (it throws an exception if stub creation fails.)

Expand Down