Skip to content

Commit 4371de2

Browse files
committed
When resolving the tokens for the stub, re-translate signatures that already depend on a resolver to depend on the new token map
1 parent 8e68287 commit 4371de2

File tree

8 files changed

+310
-37
lines changed

8 files changed

+310
-37
lines changed

src/coreclr/vm/dllimport.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,19 @@ class ILStubState : public StubState
407407
SigBuilder sigBuilder;
408408

409409
{
410-
SigPointer sigPtr(pStubMD->GetSig());
411-
sigPtr.ConvertToInternalSignature(pStubMD->GetModule(), NULL, &sigBuilder, GetTokenLookupMap());
410+
if (pStubMD->IsNoMetadata() && pStubMD->AsDynamicMethodDesc()->HasFlags(DynamicMethodDesc::FlagIndependentSig))
411+
{
412+
// We already have a module-independent signature, but it is based on the current resolver in the stub method desc.
413+
// We need to convert it based on our token lookup map.
414+
SigPointer sigPtr(pStubMD->GetSig());
415+
sigPtr.ConvertToInternalSignature(pStubMD->AsDynamicMethodDesc()->GetResolver(), pStubMD->GetModule(), NULL, &sigBuilder, GetTokenLookupMap());
416+
}
417+
else
418+
{
419+
// Convert to a module independent signature
420+
SigPointer sigPtr(pStubMD->GetSig());
421+
sigPtr.ConvertToInternalSignature(pStubMD->GetModule(), NULL, &sigBuilder, GetTokenLookupMap());
422+
}
412423
}
413424

414425
//
@@ -429,6 +440,7 @@ class ILStubState : public StubState
429440
memcpyNoGCRefs((void *)pNewSig, GetStubTargetMethodSig(), cbNewSig);
430441

431442
pStubMD->AsDynamicMethodDesc()->SetStoredMethodSig(pNewSig, cbNewSig);
443+
pStubMD->AsDynamicMethodDesc()->SetFlags(DynamicMethodDesc::FlagIndependentSig);
432444

433445
SigPointer sigPtr(pNewSig, cbNewSig);
434446
uint32_t callConvInfo;
@@ -467,8 +479,21 @@ class ILStubState : public StubState
467479
void ConvertMethodDescSigToModuleIndependentSig(MethodDesc* pStubMD)
468480
{
469481
SigBuilder sigBuilder;
470-
SigPointer sigPtr(pStubMD->GetSig());
471-
sigPtr.ConvertToInternalSignature(pStubMD->GetModule(), NULL, &sigBuilder, GetTokenLookupMap());
482+
_ASSERTE(pStubMD->IsNoMetadata());
483+
DynamicMethodDesc* pDMD = pStubMD->AsDynamicMethodDesc();
484+
if (pDMD->HasFlags(DynamicMethodDesc::FlagIndependentSig))
485+
{
486+
// We already have a module-independent signature, but it is based on the current resolver in the stub method desc.
487+
// We need to convert it based on our token lookup map.
488+
SigPointer sigPtr(pStubMD->GetSig());
489+
sigPtr.ConvertToInternalSignature(pDMD->GetResolver(), pStubMD->GetModule(), NULL, &sigBuilder, GetTokenLookupMap());
490+
}
491+
else
492+
{
493+
SigPointer sigPtr(pStubMD->GetSig());
494+
sigPtr.ConvertToInternalSignature(pStubMD->GetModule(), NULL, &sigBuilder, GetTokenLookupMap());
495+
}
496+
472497

473498
//
474499
// make a domain-local copy of the sig so that this state can outlive the
@@ -480,7 +505,8 @@ class ILStubState : public StubState
480505

481506
memcpyNoGCRefs((void *)pNewSig, pNewSigBuffer, cbNewSig);
482507

483-
pStubMD->AsDynamicMethodDesc()->SetStoredMethodSig(pNewSig, cbNewSig);
508+
pDMD->SetStoredMethodSig(pNewSig, cbNewSig);
509+
pDMD->SetFlags(DynamicMethodDesc::FlagIndependentSig);
484510
}
485511

486512
void EmitInvokeTarget(MethodDesc *pStubMD)

src/coreclr/vm/ilstubcache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ MethodDesc* ILStubCache::CreateNewMethodDesc(LoaderHeap* pCreationHeap, MethodTa
216216
}
217217
else
218218
{
219+
pMD->SetFlags(DynamicMethodDesc::FlagIndependentSig);
219220
CreateModuleIndependentSignature(pCreationHeap, pamTracker, pSigModule, pSig, cbSig, pTypeContext, &tokenLookupMap, &pNewSig, &cbNewSig);
220221
}
221222
pMD->SetStoredMethodSig(pNewSig, cbNewSig);

src/coreclr/vm/ilstubresolver.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void ILStubResolver::ResolveToken(mdToken token, ResolvedToken* resolvedToken)
142142
{
143143
case mdtMethodDef:
144144
{
145-
MethodDesc* pMD = m_pCompileTimeState->m_tokenLookupMap.LookupMethodDef(token);
145+
MethodDesc* pMD = m_tokenLookupMap.LookupMethodDef(token);
146146
_ASSERTE(pMD);
147147
resolvedToken->Method = pMD;
148148
resolvedToken->TypeHandle = TypeHandle(pMD->GetMethodTable());
@@ -151,15 +151,15 @@ void ILStubResolver::ResolveToken(mdToken token, ResolvedToken* resolvedToken)
151151

152152
case mdtTypeDef:
153153
{
154-
TypeHandle typeHnd = m_pCompileTimeState->m_tokenLookupMap.LookupTypeDef(token);
154+
TypeHandle typeHnd = m_tokenLookupMap.LookupTypeDef(token);
155155
_ASSERTE(!typeHnd.IsNull());
156156
resolvedToken->TypeHandle = typeHnd;
157157
}
158158
break;
159159

160160
case mdtFieldDef:
161161
{
162-
FieldDesc* pFD = m_pCompileTimeState->m_tokenLookupMap.LookupFieldDef(token);
162+
FieldDesc* pFD = m_tokenLookupMap.LookupFieldDef(token);
163163
_ASSERTE(pFD);
164164
resolvedToken->Field = pFD;
165165
resolvedToken->TypeHandle = TypeHandle(pFD->GetEnclosingMethodTable());
@@ -169,13 +169,13 @@ void ILStubResolver::ResolveToken(mdToken token, ResolvedToken* resolvedToken)
169169
#if !defined(DACCESS_COMPILE)
170170
case mdtMemberRef:
171171
{
172-
TokenLookupMap::MemberRefEntry entry = m_pCompileTimeState->m_tokenLookupMap.LookupMemberRef(token);
172+
TokenLookupMap::MemberRefEntry entry = m_tokenLookupMap.LookupMemberRef(token);
173173
if (entry.Type == mdtFieldDef)
174174
{
175175
_ASSERTE(entry.Entry.Field != NULL);
176176

177177
if (entry.ClassSignatureToken != mdTokenNil)
178-
resolvedToken->TypeSignature = m_pCompileTimeState->m_tokenLookupMap.LookupSig(entry.ClassSignatureToken);
178+
resolvedToken->TypeSignature = m_tokenLookupMap.LookupSig(entry.ClassSignatureToken);
179179

180180
resolvedToken->Field = entry.Entry.Field;
181181
resolvedToken->TypeHandle = TypeHandle(entry.Entry.Field->GetApproxEnclosingMethodTable());
@@ -186,7 +186,7 @@ void ILStubResolver::ResolveToken(mdToken token, ResolvedToken* resolvedToken)
186186
_ASSERTE(entry.Entry.Method != NULL);
187187

188188
if (entry.ClassSignatureToken != mdTokenNil)
189-
resolvedToken->TypeSignature = m_pCompileTimeState->m_tokenLookupMap.LookupSig(entry.ClassSignatureToken);
189+
resolvedToken->TypeSignature = m_tokenLookupMap.LookupSig(entry.ClassSignatureToken);
190190

191191
resolvedToken->Method = entry.Entry.Method;
192192
MethodTable* pMT = entry.Entry.Method->GetMethodTable();
@@ -198,14 +198,14 @@ void ILStubResolver::ResolveToken(mdToken token, ResolvedToken* resolvedToken)
198198

199199
case mdtMethodSpec:
200200
{
201-
TokenLookupMap::MethodSpecEntry entry = m_pCompileTimeState->m_tokenLookupMap.LookupMethodSpec(token);
201+
TokenLookupMap::MethodSpecEntry entry = m_tokenLookupMap.LookupMethodSpec(token);
202202
_ASSERTE(entry.Method != NULL);
203203

204204
if (entry.ClassSignatureToken != mdTokenNil)
205-
resolvedToken->TypeSignature = m_pCompileTimeState->m_tokenLookupMap.LookupSig(entry.ClassSignatureToken);
205+
resolvedToken->TypeSignature = m_tokenLookupMap.LookupSig(entry.ClassSignatureToken);
206206

207207
if (entry.MethodSignatureToken != mdTokenNil)
208-
resolvedToken->MethodSignature = m_pCompileTimeState->m_tokenLookupMap.LookupSig(entry.MethodSignatureToken);
208+
resolvedToken->MethodSignature = m_tokenLookupMap.LookupSig(entry.MethodSignatureToken);
209209

210210
resolvedToken->Method = entry.Method;
211211
MethodTable* pMT = entry.Method->GetMethodTable();
@@ -231,7 +231,7 @@ ILStubResolver::ResolveSignature(
231231
if (token == TOKEN_ILSTUB_TARGET_SIG)
232232
return m_pCompileTimeState->m_StubTargetMethodSig;
233233

234-
return m_pCompileTimeState->m_tokenLookupMap.LookupSig(token);
234+
return m_tokenLookupMap.LookupSig(token);
235235
}
236236

237237
//---------------------------------------------------------------------------------------
@@ -500,15 +500,9 @@ void
500500
ILStubResolver::SetTokenLookupMap(
501501
TokenLookupMap * pMap)
502502
{
503-
CONTRACTL
504-
{
505-
STANDARD_VM_CHECK;
506-
PRECONDITION(CheckPointer(m_pCompileTimeState));
507-
}
508-
CONTRACTL_END;
503+
STANDARD_VM_CONTRACT;
509504

510-
// run copy ctor
511-
new (&m_pCompileTimeState->m_tokenLookupMap) TokenLookupMap(pMap);
505+
m_tokenLookupMap = TokenLookupMap(pMap);
512506
}
513507

514508
bool ILStubResolver::IsCompiled()

src/coreclr/vm/ilstubresolver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class ILStubResolver : DynamicResolver
9494
COR_ILMETHOD_DECODER m_ILHeader;
9595
COR_ILMETHOD_SECT_EH * m_pEHSect;
9696
SigPointer m_StubTargetMethodSig;
97-
TokenLookupMap m_tokenLookupMap;
9897
};
9998
typedef DPTR(struct CompileTimeState) PTR_CompileTimeState;
10099

@@ -104,6 +103,7 @@ class ILStubResolver : DynamicResolver
104103
PTR_MethodDesc m_pStubTargetMD;
105104
CORJIT_FLAGS m_jitFlags;
106105
PTR_LoaderHeap m_loaderHeap;
106+
TokenLookupMap m_tokenLookupMap;
107107
};
108108

109109
typedef Holder<ILStubResolver*, DoNothing<ILStubResolver*>, ILStubResolver::StubGenFailed, 0> ILStubGenHolder;

src/coreclr/vm/method.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2500,14 +2500,17 @@ class DynamicMethodDesc : public StoredSigMethodDesc
25002500
// Flags for DynamicMethodDesc
25012501
// Define new flags in descending order. This allows the IL type enumeration to increase naturally.
25022502
FlagNone = 0x00000000,
2503+
FlagIndependentSig = 0x00000400, // The signature stored for this MethodDesc is already module independent.
2504+
// The tokens stored in the signature should be resolved through the Resolver,
2505+
// not whatever module this MethodDesc is attached to.
25032506
FlagPublic = 0x00000800,
25042507
FlagStatic = 0x00001000,
25052508
FlagRequiresCOM = 0x00002000,
25062509
FlagIsLCGMethod = 0x00004000,
25072510
FlagIsILStub = 0x00008000,
25082511
FlagIsDelegate = 0x00010000,
25092512
FlagIsCALLI = 0x00020000,
2510-
FlagMask = 0x0003f800,
2513+
FlagMask = 0x0003fc00,
25112514
StackArgSizeMask = 0xfffc0000, // native stack arg size for IL stubs
25122515
ILStubTypeMask = ~(FlagMask | StackArgSizeMask)
25132516
};

0 commit comments

Comments
 (0)