Skip to content
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

SPMI: Gracefully handle managed exceptions during replay #89654

Merged
merged 2 commits into from
Aug 2, 2023
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
17 changes: 11 additions & 6 deletions src/coreclr/tools/superpmi/superpmi-shared/errorhandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@

void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode)
{
if (BreakOnException())
__debugbreak();

RaiseException(exceptionCode, 0, 0, nullptr);
}

// Allocating memory here seems moderately dangerous: we'll probably leak like a sieve...
void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode, va_list args, const char* message)
void MSC_ONLY(__declspec(noreturn)) ThrowSpmiException(DWORD exceptionCode, va_list args, const char* message)
{
assert(IsSuperPMIException(exceptionCode));

char* buffer = new char[8192];
ULONG_PTR* ptr = new ULONG_PTR();
*ptr = (ULONG_PTR)buffer;
_vsnprintf_s(buffer, 8192, 8191, message, args);

if (BreakOnException())
__debugbreak();

RaiseException(exceptionCode, 0, 1, ptr);
ULONG_PTR exArgs[1];
exArgs[0] = (ULONG_PTR)buffer;
RaiseException(exceptionCode, 0, ArrLen(exArgs), exArgs);
}

void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode, const char* msg, ...)
void MSC_ONLY(__declspec(noreturn)) ThrowSpmiException(DWORD exceptionCode, const char* msg, ...)
{
va_list ap;
va_start(ap, msg);
ThrowException(exceptionCode, ap, msg);
ThrowSpmiException(exceptionCode, ap, msg);
}

SpmiException::SpmiException(FilterSuperPMIExceptionsParam_CaptureException* e)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/tools/superpmi/superpmi-shared/errorhandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define _ErrorHandling

#include "logging.h"
#include "corexcep.h"

// EXCEPTIONCODE_DebugBreakorAV is just the base exception number; calls to DebugBreakorAV()
// pass a unique number to add to this. EXCEPTIONCODE_DebugBreakorAV_MAX is the maximum number
Expand All @@ -20,10 +21,11 @@
#define EXCEPTIONCODE_CALLUTILS 0xe0426000
#define EXCEPTIONCODE_TYPEUTILS 0xe0427000
#define EXCEPTIONCODE_ASSERT 0xe0440000
#define EXCEPTIONCODE_COMPLUS EXCEPTION_COMPLUS

// RaiseException wrappers
void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode);
void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode, const char* message, ...);
void MSC_ONLY(__declspec(noreturn)) ThrowSpmiException(DWORD exceptionCode, const char* message, ...);

// Assert stuff
#define AssertCodeMsg(expr, exCode, msg, ...) \
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/superpmi/superpmi-shared/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
do \
{ \
Logger::LogExceptionMessage(__FUNCTION__, __FILE__, __LINE__, exCode, msg, __VA_ARGS__); \
ThrowException(exCode, msg, __VA_ARGS__); \
ThrowSpmiException(exCode, msg, __VA_ARGS__); \
} while (0)

// These are specified as flags so subsets of the logging functionality can be enabled/disabled at once
Expand Down
18 changes: 17 additions & 1 deletion src/coreclr/tools/superpmi/superpmi/jitinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ JitInstance::Result JitInstance::CompileMethod(MethodContext* MethodToCompile, i
pParam->pThis->mc->cr->recAllocMemCapture();
pParam->pThis->mc->cr->recAllocGCInfoCapture();

pParam->pThis->mc->cr->recMessageLog("Successful Compile");
pParam->pThis->mc->cr->recMessageLog(jitResult == CORJIT_OK ? "Successful Compile" : "Successful Compile (BADCODE)");

pParam->metrics->NumCodeBytes += NCodeSizeBlock;
}
Expand All @@ -435,6 +435,22 @@ JitInstance::Result JitInstance::CompileMethod(MethodContext* MethodToCompile, i
e.DeleteMessage();
param.result = RESULT_MISSING;
}
else if (e.GetCode() == EXCEPTIONCODE_COMPLUS)
{
// We assume that managed exceptions are never JIT bugs and were
// thrown by the EE during recording. Various EE APIs can throw
// managed exceptions and replay will faithfully rethrow these. The
// JIT itself will sometimes catch them (e.g. during inlining), but
// if they make it out of the JIT then we assume that they are not
// JIT bugs. The typical scenario is something like
// MissingFieldException thrown from resolveToken.

// Call these methods to capture that no code/GC info was generated.
mc->cr->recAllocMemCapture();
mc->cr->recAllocGCInfoCapture();

mc->cr->recMessageLog("Successful Compile (EE API exception)");
}
else
{
e.ShowAndDeleteMessage();
Expand Down
Loading