Skip to content

Commit

Permalink
Fix storage of stack trace of exception from reflection (#107093)
Browse files Browse the repository at this point in the history
There was one more case where we have saved the stack trace into the _remoteStackTraceString
field in the exception when the exception was passing from reflection invoked code
to the caller of that code. While there is no visible difference in the Exception.ToString,
a SOS test was failing due to that. And it is not necessary to save the stack trace
there.

I have thought about the cases when we really need the stack trace saved into the
_remoteStackTraceString and I believe that actually the only case is when an existing
exception is thrown again in managed code. So I have removed the option to save the stack
trace from all the variants of the DispatchManagedException.

Co-authored-by: Jan Vorlicek <janvorli@microsoft.com>
Co-authored-by: Jeff Schwartz <jeffschw@microsoft.com>
  • Loading branch information
3 people committed Aug 29, 2024
1 parent c827c27 commit 13c6579
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/vm/excep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7504,7 +7504,7 @@ VOID DECLSPEC_NORETURN UnwindAndContinueRethrowHelperAfterCatch(Frame* pEntryFra
}
else
{
DispatchManagedException(orThrowable, /* preserveStackTrace */ false);
DispatchManagedException(orThrowable);
}
}
else
Expand Down
17 changes: 5 additions & 12 deletions src/coreclr/vm/exceptionhandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ ProcessCLRExceptionNew(IN PEXCEPTION_RECORD pExceptionRecord,
else
{
OBJECTREF oref = ExceptionTracker::CreateThrowable(pExceptionRecord, FALSE);
DispatchManagedException(oref, pContextRecord, /* preserveStackTrace */ false);
DispatchManagedException(oref, pContextRecord);
}
}
#endif // !HOST_UNIX
Expand Down Expand Up @@ -5649,7 +5649,7 @@ void FirstChanceExceptionNotification()
#endif // TARGET_UNIX
}

VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT* pExceptionContext, bool preserveStackTrace)
VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT* pExceptionContext)
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_TRIGGERS;
Expand All @@ -5661,19 +5661,12 @@ VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT* pE

Thread *pThread = GetThread();

if (preserveStackTrace)
{
pThread->IncPreventAbort();
ExceptionPreserveStackTrace(throwable);
pThread->DecPreventAbort();
}

ULONG_PTR hr = GetHRFromThrowable(throwable);

EXCEPTION_RECORD exceptionRecord;
exceptionRecord.ExceptionCode = EXCEPTION_COMPLUS;
exceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE | EXCEPTION_SOFTWARE_ORIGINATE;
exceptionRecord.ExceptionAddress = (void *)(void (*)(OBJECTREF, bool))&DispatchManagedException;
exceptionRecord.ExceptionAddress = (void *)(void (*)(OBJECTREF))&DispatchManagedException;
exceptionRecord.NumberParameters = MarkAsThrownByUs(exceptionRecord.ExceptionInformation, hr);
exceptionRecord.ExceptionRecord = NULL;

Expand Down Expand Up @@ -5709,7 +5702,7 @@ VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT* pE
UNREACHABLE();
}

VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, bool preserveStackTrace)
VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable)
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_TRIGGERS;
Expand All @@ -5718,7 +5711,7 @@ VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, bool preser
CONTEXT exceptionContext;
RtlCaptureContext(&exceptionContext);

DispatchManagedException(throwable, &exceptionContext, preserveStackTrace);
DispatchManagedException(throwable, &exceptionContext);
UNREACHABLE();
}

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/exceptionhandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ ProcessCLRException(IN PEXCEPTION_RECORD pExceptionRecord,
IN OUT PT_CONTEXT pContextRecord,
IN OUT PT_DISPATCHER_CONTEXT pDispatcherContext);

VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT *pExceptionContext, bool preserveStackTrace = true);
VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, bool preserveStackTrace = true);
VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable, CONTEXT *pExceptionContext);
VOID DECLSPEC_NORETURN DispatchManagedException(OBJECTREF throwable);
VOID DECLSPEC_NORETURN DispatchManagedException(RuntimeExceptionKind reKind);

enum CLRUnwindStatus { UnwindPending, FirstPassComplete, SecondPassComplete };
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3060,7 +3060,7 @@ void ThrowNew(OBJECTREF oref)
}
}

DispatchManagedException(oref, /* preserveStackTrace */ false);
DispatchManagedException(oref);
}
#endif // FEATURE_EH_FUNCLETS

Expand Down

0 comments on commit 13c6579

Please sign in to comment.