Skip to content

Commit d1fd184

Browse files
committed
Instead of carrying around a token map, introduce a new kind of "internal modifier" type for modifiers in internal signatures.
1 parent 4371de2 commit d1fd184

22 files changed

+484
-553
lines changed

src/coreclr/ildasm/dasm.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,9 @@ BYTE* PrettyPrintCABlobValue(PCCOR_SIGNATURE &typePtr,
20882088

20892089
#ifdef LOGGING
20902090
case ELEMENT_TYPE_INTERNAL :
2091+
case ELEMENT_TYPE_CMOD_INTERNAL :
2092+
typePtr += 1;
2093+
Reiterate = TRUE;
20912094
#endif // LOGGING
20922095
return NULL;
20932096

src/coreclr/inc/corhdr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,9 +911,10 @@ typedef enum CorElementType
911911

912912
// This is for signatures generated internally (which will not be persisted in any way).
913913
ELEMENT_TYPE_INTERNAL = 0x21, // INTERNAL <typehandle>
914+
ELEMENT_TYPE_CMOD_INTERNAL = 0x22, // CMOD_INTERNAL <reqd> <typehandle>
914915

915916
// Note that this is the max of base type excluding modifiers
916-
ELEMENT_TYPE_MAX = 0x22, // first invalid element type
917+
ELEMENT_TYPE_MAX = 0x23, // first invalid element type
917918

918919

919920
ELEMENT_TYPE_MODIFIER = 0x40,

src/coreclr/inc/cortypeinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ TYPEINFO(ELEMENT_TYPE_MVAR, NULL, NULL, TARGET_POINTER_SI
5454
TYPEINFO(ELEMENT_TYPE_CMOD_REQD, NULL, NULL, 0, TYPE_GC_NONE, false, false, false, false, false) // 0x1f
5555
TYPEINFO(ELEMENT_TYPE_CMOD_OPT, NULL, NULL, 0, TYPE_GC_NONE, false, false, false, false, false) // 0x20
5656
TYPEINFO(ELEMENT_TYPE_INTERNAL, NULL, NULL, 0, TYPE_GC_OTHER, false, false, false, false, false) // 0x21
57+
TYPEINFO(ELEMENT_TYPE_CMOD_INTERNAL,NULL, NULL, 0, TYPE_GC_NONE, false, false, false, false, false) // 0x22

src/coreclr/inc/formattype.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,44 @@ PCCOR_SIGNATURE PrettyPrintType(
694694
appendStr(out, sz);
695695
break;
696696
}
697+
case ELEMENT_TYPE_CMOD_INTERNAL :
698+
{
699+
// ELEMENT_TYPE_CMOD_INTERNAL <required> <TypeHandle>
700+
bool required = *typePtr++ != 0;
701+
_ASSERTE(sizeof(TypeHandle) == sizeof(void *));
702+
TypeHandle typeHandle;
703+
typePtr += CorSigUncompressPointer(typePtr, (void **)&typeHandle);
704+
705+
MethodTable *pMT = NULL;
706+
if (typeHandle.IsTypeDesc())
707+
{
708+
pMT = typeHandle.AsTypeDesc()->GetMethodTable();
709+
if (pMT)
710+
{
711+
PrettyPrintClass(out, pMT->GetCl(), pMT->GetMDImport());
712+
713+
// It could be a "native version" of the managed type used in interop
714+
if (typeHandle.AsTypeDesc()->IsNativeValueType())
715+
appendStr(out, "_NativeValueType");
716+
}
717+
else
718+
appendStr(out, "(null)");
719+
}
720+
else
721+
{
722+
pMT = typeHandle.AsMethodTable();
723+
if (pMT)
724+
PrettyPrintClass(out, pMT->GetCl(), pMT->GetMDImport());
725+
else
726+
appendStr(out, "(null)");
727+
}
728+
729+
const char fmt[] = " mod%s(/* MT: %p */)";
730+
char sz[Max64BitHexString + ARRAY_SIZE(fmt) + ARRAY_SIZE("req")];
731+
sprintf_s(sz, ARRAY_SIZE(sz), fmt, required ? "req" : "opt", pMT);
732+
appendStr(out, sz);
733+
break;
734+
}
697735
#endif
698736

699737

src/coreclr/inc/sigparser.h

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -582,19 +582,32 @@ class SigParser
582582
return hr;
583583

584584
while ((ELEMENT_TYPE_CMOD_REQD == bElementType) ||
585-
(ELEMENT_TYPE_CMOD_OPT == bElementType))
585+
(ELEMENT_TYPE_CMOD_OPT == bElementType) ||
586+
(ELEMENT_TYPE_CMOD_INTERNAL == bElementType))
586587
{
587588
sigTemp.SkipBytes(1);
589+
if (ELEMENT_TYPE_CMOD_INTERNAL == bElementType)
590+
{
591+
void * pMT;
592+
uint8_t required;
593+
if (FAILED(hr = sigTemp.GetByte(&required)))
594+
return hr;
595+
596+
if (FAILED(hr = sigTemp.GetPointer(&pMT)))
597+
return hr;
598+
599+
}
600+
else
601+
{
602+
mdToken token;
588603

589-
mdToken token;
590-
591-
hr = sigTemp.GetToken(&token);
604+
hr = sigTemp.GetToken(&token);
592605

593-
if (FAILED(hr))
594-
return hr;
606+
if (FAILED(hr))
607+
return hr;
608+
}
595609

596-
hr = sigTemp.PeekByte(&bElementType);
597-
if (FAILED(hr))
610+
if (FAILED(hr = sigTemp.PeekByte(&bElementType)))
598611
return hr;
599612
}
600613

@@ -643,19 +656,32 @@ class SigParser
643656
while (ELEMENT_TYPE_CMOD_REQD == bElementType ||
644657
ELEMENT_TYPE_CMOD_OPT == bElementType ||
645658
ELEMENT_TYPE_MODIFIER == bElementType ||
646-
ELEMENT_TYPE_PINNED == bElementType)
659+
ELEMENT_TYPE_PINNED == bElementType ||
660+
ELEMENT_TYPE_CMOD_INTERNAL == bElementType)
647661
{
648662
sigTemp.SkipBytes(1);
663+
if (ELEMENT_TYPE_CMOD_INTERNAL == bElementType)
664+
{
665+
void * pMT;
666+
uint8_t required;
667+
if (FAILED(hr = sigTemp.GetByte(&required)))
668+
return hr;
669+
670+
if (FAILED(hr = sigTemp.GetPointer(&pMT)))
671+
return hr;
672+
673+
}
674+
else
675+
{
676+
mdToken token;
649677

650-
mdToken token;
651-
652-
hr = sigTemp.GetToken(&token);
678+
hr = sigTemp.GetToken(&token);
653679

654-
if (FAILED(hr))
655-
return hr;
680+
if (FAILED(hr))
681+
return hr;
682+
}
656683

657-
hr = sigTemp.PeekByte(&bElementType);
658-
if (FAILED(hr))
684+
if (FAILED(hr = sigTemp.PeekByte(&bElementType)))
659685
return hr;
660686
}
661687

src/coreclr/utilcode/prettyprintsig.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,18 @@ static HRESULT PrettyPrintTypeA(
674674
sprintf_s(tempBuffer, 64, "pMT: %p", pMT);
675675
IfFailGo(appendStrA(out, tempBuffer));
676676
break;
677+
678+
case ELEMENT_TYPE_CMOD_INTERNAL:
679+
{
680+
bool required = *typePtr++ != 0;
681+
void* pMT;
682+
pMT = *((void* UNALIGNED *)typePtr);
683+
typePtr += sizeof(void*);
684+
CHAR tempBuffer[64];
685+
sprintf_s(tempBuffer, 64, "pMT: %p", pMT);
686+
IfFailGo(appendStrA(out, tempBuffer));
687+
break;
688+
}
677689

678690
case ELEMENT_TYPE_VALUETYPE:
679691
str = "value class ";

src/coreclr/vm/callconvbuilder.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,25 +358,49 @@ HRESULT CallConv::TryGetUnmanagedCallingConventionFromModOpt(
358358
_ASSERTE(pWalk <= pSig + cSig);
359359

360360
CallConvBuilder& callConvBuilder = *builder;
361-
while ((pWalk < (pSig + cSig)) && ((*pWalk == ELEMENT_TYPE_CMOD_OPT) || (*pWalk == ELEMENT_TYPE_CMOD_REQD)))
361+
while ((pWalk < (pSig + cSig)) && ((*pWalk == ELEMENT_TYPE_CMOD_OPT) || (*pWalk == ELEMENT_TYPE_CMOD_REQD) || (*pWalk == ELEMENT_TYPE_CMOD_INTERNAL)))
362362
{
363-
BOOL fIsOptional = (*pWalk == ELEMENT_TYPE_CMOD_OPT);
364-
365-
pWalk++;
366-
if (pWalk + CorSigUncompressedDataSize(pWalk) > pSig + cSig)
363+
CORINFO_MODULE_HANDLE tokenLookupModule = pModule;
364+
mdToken tk;
365+
LPCSTR typeNamespace;
366+
LPCSTR typeName;
367+
if (*pWalk == ELEMENT_TYPE_CMOD_INTERNAL)
367368
{
368-
*errorResID = BFA_BAD_SIGNATURE;
369-
return COR_E_BADIMAGEFORMAT; // Bad formatting
369+
// Skip internal modifiers
370+
pWalk++;
371+
if (pWalk + 1 + sizeof(void*) > pSig + cSig)
372+
{
373+
*errorResID = BFA_BAD_SIGNATURE;
374+
return COR_E_BADIMAGEFORMAT; // Bad formatting
375+
}
376+
377+
BOOL required = *pWalk++ != 0;
378+
void* pType;
379+
pWalk += CorSigUncompressPointer(pWalk, &pType);
380+
TypeHandle type = TypeHandle::FromPtr(pType);
381+
382+
if (!required)
383+
continue;
384+
385+
tokenLookupModule = GetScopeHandle(type.GetModule());
386+
tk = type.GetCl();
370387
}
388+
else
389+
{
390+
BOOL fIsOptional = (*pWalk == ELEMENT_TYPE_CMOD_OPT);
371391

372-
mdToken tk;
373-
pWalk += CorSigUncompressToken(pWalk, &tk);
392+
pWalk++;
393+
if (pWalk + CorSigUncompressedDataSize(pWalk) > pSig + cSig)
394+
{
395+
*errorResID = BFA_BAD_SIGNATURE;
396+
return COR_E_BADIMAGEFORMAT; // Bad formatting
397+
}
374398

375-
if (!fIsOptional)
376-
continue;
399+
pWalk += CorSigUncompressToken(pWalk, &tk);
377400

378-
LPCSTR typeNamespace;
379-
LPCSTR typeName;
401+
if (!fIsOptional)
402+
continue;
403+
}
380404

381405
// Check for CallConv types specified in modopt
382406
if (FAILED(GetNameOfTypeRefOrDef(pModule, tk, &typeNamespace, &typeName)))

src/coreclr/vm/dllimport.cpp

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

409409
{
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-
}
410+
// Convert to a module independent signature
411+
SigPointer sigPtr(pStubMD->GetSig());
412+
sigPtr.ConvertToInternalSignature(pStubMD->GetModule(), NULL, &sigBuilder, /* bSkipCustomModifier */ FALSE);
423413
}
424414

425415
//
@@ -440,7 +430,6 @@ class ILStubState : public StubState
440430
memcpyNoGCRefs((void *)pNewSig, GetStubTargetMethodSig(), cbNewSig);
441431

442432
pStubMD->AsDynamicMethodDesc()->SetStoredMethodSig(pNewSig, cbNewSig);
443-
pStubMD->AsDynamicMethodDesc()->SetFlags(DynamicMethodDesc::FlagIndependentSig);
444433

445434
SigPointer sigPtr(pNewSig, cbNewSig);
446435
uint32_t callConvInfo;
@@ -480,20 +469,8 @@ class ILStubState : public StubState
480469
{
481470
SigBuilder sigBuilder;
482471
_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-
472+
SigPointer sigPtr(pStubMD->GetSig());
473+
sigPtr.ConvertToInternalSignature(pStubMD->GetModule(), NULL, &sigBuilder, /* bSkipCustomModifier */ FALSE);
497474

498475
//
499476
// make a domain-local copy of the sig so that this state can outlive the
@@ -505,8 +482,7 @@ class ILStubState : public StubState
505482

506483
memcpyNoGCRefs((void *)pNewSig, pNewSigBuffer, cbNewSig);
507484

508-
pDMD->SetStoredMethodSig(pNewSig, cbNewSig);
509-
pDMD->SetFlags(DynamicMethodDesc::FlagIndependentSig);
485+
pStubMD->AsDynamicMethodDesc()->SetStoredMethodSig(pNewSig, cbNewSig);
510486
}
511487

512488
void EmitInvokeTarget(MethodDesc *pStubMD)
@@ -4022,7 +3998,6 @@ namespace
40223998
DWORD m_StubFlags;
40233999

40244000
INT32 m_iLCIDArg;
4025-
INT32 m_tokenMapHash;
40264001
INT32 m_nParams;
40274002
BYTE m_rgbSigAndParamData[1];
40284003
// (dwParamAttr, cbNativeType) // length: number of parameters
@@ -4078,11 +4053,9 @@ namespace
40784053

40794054
// note that ConvertToInternalSignature also resolves generics so different instantiations will get different
40804055
// hash blobs for methods that have generic parameters in their signature
4081-
// The signature may have custom modifiers, so provide a token lookup map to resolve them.
4082-
// We'll include a hash of the token lookup map in the hash blob.
4083-
TokenLookupMap tokenLookupMap;
4056+
// The signature may have custom modifiers that influence behavior, so don't skip them
40844057
SigBuilder sigBuilder;
4085-
sigPtr.ConvertToInternalSignature(pParams->m_pModule, pParams->m_pTypeContext, &sigBuilder, &tokenLookupMap);
4058+
sigPtr.ConvertToInternalSignature(pParams->m_pModule, pParams->m_pTypeContext, &sigBuilder, /* bSkipCustomModifier */ FALSE);
40864059

40874060
DWORD cbSig;
40884061
PVOID pSig = sigBuilder.GetSignature(&cbSig);
@@ -4117,7 +4090,6 @@ namespace
41174090
pBlob->m_nlFlags = static_cast<BYTE>(pParams->m_nlFlags & ~nlfNoMangle); // this flag does not affect the stub
41184091
pBlob->m_iLCIDArg = pParams->m_iLCIDArg;
41194092

4120-
pBlob->m_tokenMapHash = tokenLookupMap.GetHashValue();
41214093
pBlob->m_StubFlags = pParams->m_dwStubFlags;
41224094
pBlob->m_nParams = pParams->m_nParamTokens;
41234095

src/coreclr/vm/gdbjit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static constexpr const int CorElementTypeToDWEncoding[] =
7474
/* ELEMENT_TYPE_CMOD_REQD */ DW_ATE_address,
7575
/* ELEMENT_TYPE_CMOD_OPT */ DW_ATE_address,
7676
/* ELEMENT_TYPE_INTERNAL */ DW_ATE_address,
77+
/* ELEMENT_TYPE_CMOD_INTERNAL */DW_ATE_address,
7778
/* ELEMENT_TYPE_MAX */ DW_ATE_address,
7879
};
7980

src/coreclr/vm/ilmarshalers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3465,7 +3465,7 @@ MarshalerOverrideStatus ILBlittableValueClassWithCopyCtorMarshaler::ArgumentOver
34653465
// but on other platforms it comes by-reference
34663466
#ifdef TARGET_X86
34673467
LocalDesc locDesc(pargs->mm.m_pMT);
3468-
locDesc.AddModifier(true, pslIL->GetToken(pargs->mm.m_pSigMod));
3468+
locDesc.AddModifier(true, pargs->mm.m_pSigMod);
34693469
pslIL->SetStubTargetArgType(&locDesc);
34703470

34713471
pslILDispatch->EmitLDARGA(argidx);

0 commit comments

Comments
 (0)