From afdd009ff95ea3da2e5b34f09f4a00c548f9be33 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Tue, 25 Oct 2022 15:30:53 +0200 Subject: [PATCH] SPMI: Implement missing NAOT support (and fix garbage values passed to JIT in expandRawHandleIntrinsic) (#76996) Implement expandRawHandleIntrinsic in SPMI Also fix NAOT passing garbage back to JIT. Fix #76925 --- src/coreclr/inc/jiteeversionguid.h | 10 ++--- .../JitInterface/CorInfoImpl.RyuJit.cs | 8 ++++ .../tools/superpmi/superpmi-shared/lwmlist.h | 1 + .../superpmi-shared/methodcontext.cpp | 41 +++++++++++++++++++ .../superpmi/superpmi-shared/methodcontext.h | 5 +++ .../superpmi-shim-collector/icorjitinfo.cpp | 1 + .../tools/superpmi/superpmi/icorjitinfo.cpp | 3 +- 7 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/coreclr/inc/jiteeversionguid.h b/src/coreclr/inc/jiteeversionguid.h index 55b34f9db6682..90c5520ebfed6 100644 --- a/src/coreclr/inc/jiteeversionguid.h +++ b/src/coreclr/inc/jiteeversionguid.h @@ -43,11 +43,11 @@ typedef const GUID *LPCGUID; #define GUID_DEFINED #endif // !GUID_DEFINED -constexpr GUID JITEEVersionIdentifier = { /* fd11cceb-ac62-4a6e-892c-dc54b677cacd */ - 0xfd11cceb, - 0xac62, - 0x4a6e, - { 0x89, 0x2c, 0xdc, 0x54, 0xb6, 0x77, 0xca, 0xcd } +constexpr GUID JITEEVersionIdentifier = { /* 2ed4cd12-48ed-4a0e-892e-d24d004a5e4c */ + 0x2ed4cd12, + 0x48ed, + 0x4a0e, + {0x89, 0x2e, 0xd2, 0x4d, 0x00, 0x4a, 0x5e, 0x4c} }; ////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs index 72ee00ccd76da..bd923c96617e4 100644 --- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs +++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs @@ -1818,17 +1818,25 @@ private void expandRawHandleIntrinsic(ref CORINFO_RESOLVED_TOKEN pResolvedToken, // Resolved token as a potentially RuntimeDetermined object. MethodDesc method = (MethodDesc)GetRuntimeDeterminedObjectForToken(ref pResolvedToken); + pResult.compileTimeHandle = null; + switch (method.Name) { case "EETypePtrOf": case "Of": ComputeLookup(ref pResolvedToken, method.Instantiation[0], ReadyToRunHelperId.TypeHandle, ref pResult.lookup); + pResult.handleType = CorInfoGenericHandleType.CORINFO_HANDLETYPE_CLASS; break; case "DefaultConstructorOf": ComputeLookup(ref pResolvedToken, method.Instantiation[0], ReadyToRunHelperId.DefaultConstructor, ref pResult.lookup); + pResult.handleType = CorInfoGenericHandleType.CORINFO_HANDLETYPE_METHOD; break; case "AllocatorOf": ComputeLookup(ref pResolvedToken, method.Instantiation[0], ReadyToRunHelperId.ObjectAllocator, ref pResult.lookup); + pResult.handleType = CorInfoGenericHandleType.CORINFO_HANDLETYPE_UNKNOWN; + break; + default: + Debug.Fail("Unexpected raw handle intrinsic"); break; } } diff --git a/src/coreclr/tools/superpmi/superpmi-shared/lwmlist.h b/src/coreclr/tools/superpmi/superpmi-shared/lwmlist.h index 9e5c752f94b60..2b1eb255951cc 100644 --- a/src/coreclr/tools/superpmi/superpmi-shared/lwmlist.h +++ b/src/coreclr/tools/superpmi/superpmi-shared/lwmlist.h @@ -148,6 +148,7 @@ LWM(InitClass, Agnostic_InitClass, DWORD) LWM(IsCompatibleDelegate, Agnostic_IsCompatibleDelegate, DD) LWM(IsDelegateCreationAllowed, DLDL, DWORD) LWM(IsFieldStatic, DWORDLONG, DWORD) +LWM(ExpandRawHandleIntrinsic, Agnostic_CORINFO_RESOLVED_TOKENin, Agnostic_CORINFO_GENERICHANDLE_RESULT) LWM(IsIntrinsicType, DWORDLONG, DWORD) LWM(IsSDArray, DWORDLONG, DWORD) LWM(IsValidStringRef, DLD, DWORD) diff --git a/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.cpp b/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.cpp index 32c39304e7aaa..3afd927f7b416 100644 --- a/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.cpp +++ b/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.cpp @@ -1705,6 +1705,47 @@ void MethodContext::repGetCallInfoFromMethodHandle(CORINFO_METHOD_HANDLE methodH LogException(EXCEPTIONCODE_MC, "Didn't find key %016llX.", methodHandle); } +void MethodContext::recExpandRawHandleIntrinsic(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_GENERICHANDLE_RESULT* pResult) +{ + if (ExpandRawHandleIntrinsic == nullptr) + ExpandRawHandleIntrinsic = new LightWeightMap; + + Agnostic_CORINFO_RESOLVED_TOKENin key; + ZeroMemory(&key, sizeof(key)); // Zero key including any struct padding + key = SpmiRecordsHelper::CreateAgnostic_CORINFO_RESOLVED_TOKENin(pResolvedToken); + + Agnostic_CORINFO_GENERICHANDLE_RESULT value; + value.lookup = SpmiRecordsHelper::StoreAgnostic_CORINFO_LOOKUP(&pResult->lookup); + value.compileTimeHandle = CastHandle(pResult->compileTimeHandle); + value.handleType = (DWORD)pResult->handleType; + + ExpandRawHandleIntrinsic->Add(key, value); + DEBUG_REC(dmpExpandRawHandleIntrinsic(key, value)); +} +void MethodContext::dmpExpandRawHandleIntrinsic(const Agnostic_CORINFO_RESOLVED_TOKENin& key, const Agnostic_CORINFO_GENERICHANDLE_RESULT& result) +{ + printf("ExpandRawHandleIntrinsic key: %s, value %s cth-%016llx ht-%u", + SpmiDumpHelper::DumpAgnostic_CORINFO_RESOLVED_TOKENin(key).c_str(), + SpmiDumpHelper::DumpAgnostic_CORINFO_LOOKUP(result.lookup).c_str(), + result.compileTimeHandle, + result.handleType); +} +void MethodContext::repExpandRawHandleIntrinsic(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_GENERICHANDLE_RESULT* pResult) +{ + Agnostic_CORINFO_RESOLVED_TOKENin key; + ZeroMemory(&key, sizeof(key)); // Zero key including any struct padding + key = SpmiRecordsHelper::CreateAgnostic_CORINFO_RESOLVED_TOKENin(pResolvedToken); + + AssertMapAndKeyExist(ExpandRawHandleIntrinsic, key, ": key %x", pResolvedToken->token); + + Agnostic_CORINFO_GENERICHANDLE_RESULT value = ExpandRawHandleIntrinsic->Get(key); + DEBUG_REP(dmpExpandRawHandleIntrinsic(key, value)); + + pResult->lookup = SpmiRecordsHelper::RestoreCORINFO_LOOKUP(value.lookup); + pResult->compileTimeHandle = (CORINFO_GENERIC_HANDLE)value.compileTimeHandle; + pResult->handleType = (CorInfoGenericHandleType)value.handleType; +} + void MethodContext::recIsIntrinsicType(CORINFO_CLASS_HANDLE cls, bool result) { if (IsIntrinsicType == nullptr) diff --git a/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.h b/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.h index eba327a304cb5..e28f4ed09f044 100644 --- a/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.h +++ b/src/coreclr/tools/superpmi/superpmi-shared/methodcontext.h @@ -290,6 +290,10 @@ class MethodContext void dmpIsSDArray(DWORDLONG key, DWORD value); bool repIsSDArray(CORINFO_CLASS_HANDLE cls); + void recExpandRawHandleIntrinsic(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_GENERICHANDLE_RESULT* pResult); + void dmpExpandRawHandleIntrinsic(const Agnostic_CORINFO_RESOLVED_TOKENin& key, const Agnostic_CORINFO_GENERICHANDLE_RESULT& result); + void repExpandRawHandleIntrinsic(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_GENERICHANDLE_RESULT* pResult); + void recIsIntrinsicType(CORINFO_CLASS_HANDLE cls, bool result); void dmpIsIntrinsicType(DWORDLONG key, DWORD value); bool repIsIntrinsicType(CORINFO_CLASS_HANDLE cls); @@ -1144,6 +1148,7 @@ enum mcPackets Packet_GetReadonlyStaticFieldValue = 198, Packet_GetObjectType = 199, Packet_IsObjectImmutable = 200, + Packet_ExpandRawHandleIntrinsic = 201, }; void SetDebugDumpVariables(); diff --git a/src/coreclr/tools/superpmi/superpmi-shim-collector/icorjitinfo.cpp b/src/coreclr/tools/superpmi/superpmi-shim-collector/icorjitinfo.cpp index 44f4096848cf4..cee845252fcfe 100644 --- a/src/coreclr/tools/superpmi/superpmi-shim-collector/icorjitinfo.cpp +++ b/src/coreclr/tools/superpmi/superpmi-shim-collector/icorjitinfo.cpp @@ -253,6 +253,7 @@ void interceptor_ICJI::expandRawHandleIntrinsic(CORINFO_RESOLVED_TOKEN* pR { mc->cr->AddCall("expandRawHandleIntrinsic"); original_ICorJitInfo->expandRawHandleIntrinsic(pResolvedToken, pResult); + mc->recExpandRawHandleIntrinsic(pResolvedToken, pResult); } // Is the given type in System.Private.Corelib and marked with IntrinsicAttribute? diff --git a/src/coreclr/tools/superpmi/superpmi/icorjitinfo.cpp b/src/coreclr/tools/superpmi/superpmi/icorjitinfo.cpp index 90446dd738cbb..e84c8f7988366 100644 --- a/src/coreclr/tools/superpmi/superpmi/icorjitinfo.cpp +++ b/src/coreclr/tools/superpmi/superpmi/icorjitinfo.cpp @@ -215,8 +215,7 @@ CORINFO_CLASS_HANDLE MyICJI::getDefaultEqualityComparerClass(CORINFO_CLASS_HANDL void MyICJI::expandRawHandleIntrinsic(CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_GENERICHANDLE_RESULT* pResult) { jitInstance->mc->cr->AddCall("expandRawHandleIntrinsic"); - LogError("Hit unimplemented expandRawHandleIntrinsic"); - DebugBreakorAV(129); + jitInstance->mc->repExpandRawHandleIntrinsic(pResolvedToken, pResult); } // Is the given type in System.Private.Corelib and marked with IntrinsicAttribute?