Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8ea2fbd

Browse files
committed
Protect SuperPMI from crashes calling jitStartup
When we call jitStartup, we pass a JitHost interface that the JIT calls to query for data. These queries look up in the recorded MCH data, and could fail (and throw an exception) if data is missing, which it can be for running non-matching altjit against a collection. Protect these calls with exception handling.
1 parent 4eb8b37 commit 8ea2fbd

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

src/ToolBox/superpmi/superpmi/jitinstance.cpp

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ HRESULT JitInstance::StartUp(char* PathToJit,
190190
{
191191
mc = firstContext;
192192
jitHost = new JitHost(*this);
193-
pnjitStartup(jitHost);
193+
if (!callJitStartup(jitHost))
194+
{
195+
LogError("jitStartup failed");
196+
return -1;
197+
}
194198
}
195199

196200
pJitInstance = pngetJit();
@@ -256,7 +260,11 @@ bool JitInstance::reLoad(MethodContext* firstContext)
256260
{
257261
mc = firstContext;
258262
jitHost = new JitHost(*this);
259-
pnjitStartup(jitHost);
263+
if (!callJitStartup(jitHost))
264+
{
265+
LogError("jitStartup failed");
266+
return false;
267+
}
260268
}
261269

262270
pJitInstance = pngetJit();
@@ -465,17 +473,56 @@ void JitInstance::freeLongLivedArray(void* array)
465473
HeapFree(ourHeap, 0, array);
466474
}
467475

476+
// Helper for calling pnjitStartup. Needed to allow SEH here.
477+
bool JitInstance::callJitStartup(ICorJitHost* jithost)
478+
{
479+
// Calling into the collection, which could fail, especially
480+
// for altjits. So protect the call.
481+
482+
struct Param : FilterSuperPMIExceptionsParam_CaptureException
483+
{
484+
JitInstance* pThis;
485+
ICorJitHost* jithost;
486+
bool result;
487+
} param;
488+
param.pThis = this;
489+
param.jithost = jithost;
490+
param.result = false;
491+
492+
PAL_TRY(Param*, pParam, &param)
493+
{
494+
pParam->pThis->pnjitStartup(pParam->jithost);
495+
pParam->result = true;
496+
}
497+
PAL_EXCEPT_FILTER(FilterSuperPMIExceptions_CaptureExceptionAndStop)
498+
{
499+
SpmiException e(&param.exceptionPointers);
500+
501+
LogError("failed to call jitStartup.");
502+
e.ShowAndDeleteMessage();
503+
}
504+
PAL_ENDTRY
505+
506+
return param.result;
507+
}
508+
468509
// Reset JitConfig, that stores Enviroment variables.
469510
bool JitInstance::resetConfig(MethodContext* firstContext)
470511
{
471-
if (pnjitStartup != nullptr)
512+
if (pnjitStartup == nullptr)
513+
{
514+
return false;
515+
}
516+
517+
mc = firstContext;
518+
ICorJitHost* newHost = new JitHost(*this);
519+
520+
if (!callJitStartup(newHost))
472521
{
473-
mc = firstContext;
474-
ICorJitHost* newHost = new JitHost(*this);
475-
pnjitStartup(newHost);
476-
delete static_cast<JitHost*>(jitHost);
477-
jitHost = newHost;
478-
return true;
522+
return false;
479523
}
480-
return false;
481-
}
524+
525+
delete static_cast<JitHost*>(jitHost);
526+
jitHost = newHost;
527+
return true;
528+
}

src/ToolBox/superpmi/superpmi/jitinstance.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class JitInstance
5454
HRESULT StartUp(char* PathToJit, bool copyJit, bool breakOnDebugBreakorAV, MethodContext* firstContext);
5555
bool reLoad(MethodContext* firstContext);
5656

57+
bool callJitStartup(ICorJitHost* newHost);
58+
5759
bool resetConfig(MethodContext* firstContext);
5860

5961
Result CompileMethod(MethodContext* MethodToCompile, int mcIndex, bool collectThroughput);

0 commit comments

Comments
 (0)