forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasscompat.cpp
3611 lines (3109 loc) · 144 KB
/
classcompat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ===========================================================================
// File: CLASSCOMPAT.CPP
// ===========================================================================
// This file contains backward compatibility functionality for COM Interop.
// ===========================================================================
//
#include "common.h"
#ifndef DACCESS_COMPILE
#include "clsload.hpp"
#include "method.hpp"
#include "class.h"
#include "classcompat.h"
#include "object.h"
#include "field.h"
#include "util.hpp"
#include "excep.h"
#include "threads.h"
#include "stublink.h"
#include "dllimport.h"
#include "jitinterface.h"
#include "eeconfig.h"
#include "log.h"
#include "cgensys.h"
#include "gcheaputilities.h"
#include "dbginterface.h"
#include "comdelegate.h"
#include "sigformat.h"
#include "eeprofinterfaces.h"
#include "dllimportcallback.h"
#include "listlock.h"
#include "methodimpl.h"
#include "guidfromname.h"
#include "encee.h"
#include "encee.h"
#include "comsynchronizable.h"
#include "customattribute.h"
#include "virtualcallstub.h"
#include "eeconfig.h"
#include "contractimpl.h"
#include "prettyprintsig.h"
#include "comcallablewrapper.h"
#include "clrtocomcall.h"
#include "runtimecallablewrapper.h"
#include "generics.h"
#include "contractimpl.h"
//////////////////////////////////////////////////////////////////////////////////////////////
ClassCompat::InterfaceInfo_t* InteropMethodTableData::FindInterface(MethodTable *pInterface)
{
WRAPPER_NO_CONTRACT;
for (DWORD i = 0; i < cInterfaceMap; i++)
{
ClassCompat::InterfaceInfo_t *iMap = &pInterfaceMap[i];
if (iMap->m_pMethodTable->IsEquivalentTo(pInterface))
{
// Extensible RCW's need to be handled specially because they can have interfaces
// in their map that are added at runtime. These interfaces will have a start offset
// of -1 to indicate this. We cannot take for granted that every instance of this
// COM object has this interface so FindInterface on these interfaces is made to fail.
//
// However, we are only considering the statically available slots here
// (m_wNumInterface doesn't contain the dynamic slots), so we can safely
// ignore this detail.
return iMap;
}
}
return NULL;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// get start slot for interface
// returns -1 if interface not found
WORD InteropMethodTableData::GetStartSlotForInterface(MethodTable* pInterface)
{
WRAPPER_NO_CONTRACT;
ClassCompat::InterfaceInfo_t* pInfo = FindInterface(pInterface);
if (pInfo != NULL)
{
WORD startSlot = pInfo->GetInteropStartSlot();
_ASSERTE(startSlot != MethodTable::NO_SLOT);
return startSlot;
}
return MethodTable::NO_SLOT;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// This will return the interop slot for pMD in pMT. It will traverse the inheritance tree
// to find a match.
/*static*/ WORD InteropMethodTableData::GetSlotForMethodDesc(MethodTable *pMT, MethodDesc *pMD)
{
while (pMT)
{
InteropMethodTableData *pData = pMT->LookupComInteropData();
_ASSERTE(pData);
for (DWORD i = 0; i < pData->cVTable; i++)
{
if (pData->pVTable[i].pMD == pMD)
return (WORD) i;
}
pMT = pMT->GetParentMethodTable();
}
return MethodTable::NO_SLOT;
}
//////////////////////////////////////////////////////////////////////////////////////////////
InteropMethodTableSlotDataMap::InteropMethodTableSlotDataMap(InteropMethodTableSlotData *pSlotData, DWORD cSlotData)
{
m_pSlotData = pSlotData;
m_cSlotData = cSlotData;
m_iCurSlot = 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
InteropMethodTableSlotData *InteropMethodTableSlotDataMap::Exists_Helper(MethodDesc *pMD)
{
LIMITED_METHOD_CONTRACT;
for (DWORD i = 0; i < m_cSlotData; i++)
{
if (m_pSlotData[i].pDeclMD == pMD)
{
return (&m_pSlotData[i]);
}
}
return (NULL);
}
//////////////////////////////////////////////////////////////////////////////////////////////
BOOL InteropMethodTableSlotDataMap::Exists(MethodDesc *pMD)
{
return (Exists_Helper(pMD) != NULL);
}
//////////////////////////////////////////////////////////////////////////////////////////////
InteropMethodTableSlotData *InteropMethodTableSlotDataMap::GetData(MethodDesc *pMD)
{
LIMITED_METHOD_CONTRACT;
InteropMethodTableSlotData *pEntry = Exists_Helper(pMD);
if (pEntry)
return pEntry;
pEntry = GetNewEntry();
pEntry->pMD = pMD;
pEntry->pDeclMD = pMD;
return (pEntry);
}
//////////////////////////////////////////////////////////////////////////////////////////////
InteropMethodTableSlotData *InteropMethodTableSlotDataMap::GetNewEntry()
{
WRAPPER_NO_CONTRACT;
_ASSERTE(m_iCurSlot < m_cSlotData);
InteropMethodTableSlotData *pEntry = &m_pSlotData[m_iCurSlot++];
pEntry->pMD = NULL;
pEntry->wFlags = 0;
pEntry->wSlot = MethodTable::NO_SLOT;
pEntry->pDeclMD = NULL;
return (pEntry);
}
namespace ClassCompat
{
//////////////////////////////////////////////////////////////////////////////////////////////
InteropMethodTableData *MethodTableBuilder::BuildInteropVTable(AllocMemTracker *pamTracker)
{
CONTRACTL {
STANDARD_VM_CHECK;
INSTANCE_CHECK;
} CONTRACTL_END;
MethodTable * pThisMT = GetHalfBakedMethodTable();
// This should never be called for interfaces or for generic types.
_ASSERTE(!pThisMT->IsInterface());
_ASSERTE(!pThisMT->ContainsGenericVariables());
_ASSERTE(!pThisMT->HasGenericClassInstantiationInHierarchy());
// Array method tables are created quite differently
if (pThisMT->IsArray())
return BuildInteropVTableForArray(pamTracker);
#ifdef _DEBUG
BOOL fDump = FALSE;
LPCUTF8 fullName = pThisMT->GetDebugClassName();
if (fullName) {
LPWSTR wszRegName = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_BreakOnInteropVTableBuild);
if (wszRegName) {
{ // Poor man's narrow
LPWSTR fromPtr = wszRegName;
LPUTF8 toPtr = (LPUTF8) wszRegName;
LPUTF8 result = toPtr;
while(*fromPtr != 0)
*toPtr++ = (char) *fromPtr++;
*toPtr = 0;
}
LPCUTF8 regName = (LPCUTF8) wszRegName;
LPCUTF8 bracket = (LPCUTF8) strchr(fullName, '[');
size_t len = strlen(fullName);
if (bracket != NULL)
len = bracket - fullName;
if (strncmp(fullName, regName, len) == 0) {
_ASSERTE(!"BreakOnInteropVTableBuild");
fDump = TRUE;
}
delete [] wszRegName;
}
}
#endif // _DEBUG
//Get Check Point for the thread-based allocator
HRESULT hr = S_OK;
Module *pModule = pThisMT->GetModule();
mdToken cl = pThisMT->GetCl();
MethodTable *pParentMethodTable = pThisMT->GetParentMethodTable();
// The following structs, defined as private members of MethodTableBuilder, contain the necessary local
// parameters needed for MethodTableBuilder
// Look at the struct definitions for a detailed list of all parameters available
// to MethodTableBuilder.
bmtErrorInfo bmtError;
bmtProperties bmtProp;
bmtVtable bmtVT;
bmtParentInfo bmtParent;
bmtInterfaceInfo bmtInterface;
bmtMethodInfo bmtMethod(pModule->GetMDImport());
bmtTypeInfo bmtType;
bmtMethodImplInfo bmtMethodImpl(pModule->GetMDImport());
//Initialize structs
bmtError.resIDWhy = IDS_CLASSLOAD_GENERAL; // Set the reason and the offending method def. If the method information
bmtError.pThrowable = NULL;
bmtError.pModule = pModule;
bmtError.cl = cl;
bmtType.pMDImport = pModule->GetMDImport();
bmtType.pModule = pModule;
bmtType.cl = cl;
bmtParent.parentSubst = GetHalfBakedMethodTable()->GetSubstitutionForParent(NULL);
if (FAILED(bmtType.pMDImport->GetTypeDefProps(
bmtType.cl,
&(bmtType.dwAttr),
&(bmtParent.token))))
{
BuildMethodTableThrowException(IDS_CLASSLOAD_BADFORMAT);
}
SetBMTData(
&bmtError,
&bmtProp,
&bmtVT,
&bmtParent,
&bmtInterface,
&bmtMethod,
&bmtType,
&bmtMethodImpl);
// Populate the BMT data structures from the attributes of the incoming MT
if (pThisMT->IsValueType()) SetIsValueClass();
if (pThisMT->IsEnum()) SetEnum();
if (pThisMT->HasLayout()) SetHasLayout();
if (pThisMT->IsDelegate()) SetIsDelegate();
#ifdef FEATURE_COMINTEROP
if(pThisMT->GetClass()->IsComClassInterface()) SetIsComClassInterface();
#endif
// Populate the interface list - these are allocated on the thread's stacking allocator
//@TODO: This doesn't work for generics - fix if generics will be exposed to COM
BuildingInterfaceInfo_t *pBuildingInterfaceList;
WORD wNumInterfaces;
BuildInteropVTable_InterfaceList(&pBuildingInterfaceList, &wNumInterfaces);
bmtInterface.wInterfaceMapSize = wNumInterfaces;
WORD i;
// Interfaces have a parent class of Object, but we don't really want to inherit all of
// Object's virtual methods, so pretend we don't have a parent class - at the bottom of this
// function we reset the parent method table
if (IsInterface())
{
pParentMethodTable = NULL;
}
bmtParent.pParentMethodTable = pParentMethodTable;
// Com Import classes are special
if (IsComImport() && !IsEnum() && !IsInterface() && !IsValueClass() && !IsDelegate())
{
_ASSERTE(pParentMethodTable == g_pBaseCOMObject);
_ASSERTE(!(HasLayout()));
// if the current class is imported
bmtProp.fIsComObjectType = TRUE;
}
bmtParent.pParentMethodTable = pParentMethodTable;
if (pParentMethodTable != NULL)
{
if (pParentMethodTable->IsComObjectType())
{
// if the parent class is of ComObjectType
// so is the child
bmtProp.fIsComObjectType = TRUE;
}
}
// resolve unresolved interfaces, determine an upper bound on the size of the interface map,
// and determine the size of the largest interface (in # slots)
BuildInteropVTable_ResolveInterfaces(pBuildingInterfaceList, &bmtType, &bmtInterface, &bmtVT, &bmtParent, bmtError);
// Enumerate this class's members
EnumerateMethodImpls();
// Enumerate this class's members
EnumerateClassMethods();
AllocateMethodWorkingMemory();
// Allocate the working memory for the interop data
{
////////////////
// The interop data for the VTable for COM Interop backward compatibility
// Allocate space to hold on to the MethodDesc for each entry
bmtVT.ppSDVtable = new (GetStackingAllocator()) InteropMethodTableSlotData*[bmtVT.dwMaxVtableSize];
ZeroMemory(bmtVT.ppSDVtable, bmtVT.dwMaxVtableSize * sizeof(InteropMethodTableSlotData*));
// Allocate space to hold on to the MethodDesc for each entry
bmtVT.ppSDNonVtable = new (GetStackingAllocator()) InteropMethodTableSlotData*[NumDeclaredMethods()];
ZeroMemory(bmtVT.ppSDNonVtable , sizeof(InteropMethodTableSlotData*)*NumDeclaredMethods());
DWORD cMaxEntries = (bmtVT.dwMaxVtableSize * 2) + (NumDeclaredMethods() * 2);
InteropMethodTableSlotData *pInteropData = new (GetStackingAllocator()) InteropMethodTableSlotData[cMaxEntries];
memset(pInteropData, 0, cMaxEntries * sizeof(InteropMethodTableSlotData));
bmtVT.pInteropData = new (GetStackingAllocator()) InteropMethodTableSlotDataMap(pInteropData, cMaxEntries);
// Initialize the map with parent information
if (bmtParent.pParentMethodTable != NULL)
{
InteropMethodTableData *pParentInteropData = bmtParent.pParentMethodTable->LookupComInteropData();
_ASSERTE(pParentInteropData);
for ( i = 0; i < pParentInteropData->cVTable; i++)
{
InteropMethodTableSlotData *pParentSlot = &pParentInteropData->pVTable[i];
InteropMethodTableSlotData *pNewEntry = bmtVT.pInteropData->GetData(pParentSlot->pDeclMD);
pNewEntry->pMD = pParentSlot->pMD;
pNewEntry->pDeclMD = pParentSlot->pDeclMD;
pNewEntry->wFlags = pParentSlot->wFlags;
pNewEntry->wSlot = pParentSlot->wSlot;
bmtVT.ppSDVtable[i] = pNewEntry;
}
}
}
// Determine vtable placement for each member in this class
BuildInteropVTable_PlaceMembers(&bmtType, wNumInterfaces, pBuildingInterfaceList, &bmtMethod,
&bmtError, &bmtProp, &bmtParent, &bmtInterface, &bmtMethodImpl, &bmtVT);
// First copy what we can leverage from the parent's interface map.
// The parent's interface map will be identical to the beginning of this class's interface map (i.e.
// the interfaces will be listed in the identical order).
if (bmtParent.wNumParentInterfaces > 0)
{
PREFIX_ASSUME(pParentMethodTable != NULL); // We have to have parent to have parent interfaces
_ASSERTE(pParentMethodTable->LookupComInteropData());
_ASSERTE(bmtParent.wNumParentInterfaces == pParentMethodTable->LookupComInteropData()->cInterfaceMap);
InterfaceInfo_t *pParentInterfaceList = pParentMethodTable->LookupComInteropData()->pInterfaceMap;
for (i = 0; i < bmtParent.wNumParentInterfaces; i++)
{
#ifdef _DEBUG
_ASSERTE(pParentInterfaceList[i].m_pMethodTable == bmtInterface.pInterfaceMap[i].m_pMethodTable);
MethodTable *pMT = pParentInterfaceList[i].m_pMethodTable;
// If the interface resides entirely inside the parent's class methods (i.e. no duplicate
// slots), then we can place this interface in an identical spot to in the parent.
//
// Note carefully: the vtable for this interface could start within the first GetNumVirtuals()
// entries, but could actually extend beyond it, if we were particularly efficient at placing
// this interface, so check that the end of the interface vtable is before
// pParentMethodTable->GetNumVirtuals().
_ASSERTE(pParentInterfaceList[i].GetInteropStartSlot() + pMT->GetNumVirtuals() <=
pParentMethodTable->LookupComInteropData()->cVTable);
#endif // _DEBUG
// Interface lies inside parent's methods, so we can place it
bmtInterface.pInterfaceMap[i].SetInteropStartSlot(pParentInterfaceList[i].GetInteropStartSlot());
}
}
//
// If we are a class, then there may be some unplaced vtable methods (which are by definition
// interface methods, otherwise they'd already have been placed). Place as many unplaced methods
// as possible, in the order preferred by interfaces. However, do not allow any duplicates - once
// a method has been placed, it cannot be placed again - if we are unable to neatly place an interface,
// create duplicate slots for it starting at dwCurrentDuplicateVtableSlot. Fill out the interface
// map for all interfaces as they are placed.
//
// If we are an interface, then all methods are already placed. Fill out the interface map for
// interfaces as they are placed.
//
if (!IsInterface())
{
BuildInteropVTable_PlaceVtableMethods(
&bmtInterface,
wNumInterfaces,
pBuildingInterfaceList,
&bmtVT,
&bmtMethod,
&bmtType,
&bmtError,
&bmtProp,
&bmtParent);
BuildInteropVTable_PlaceMethodImpls(
&bmtType,
&bmtMethodImpl,
&bmtError,
&bmtInterface,
&bmtVT,
&bmtParent);
}
#ifdef _DEBUG
if (IsInterface() == FALSE)
{
for (i = 0; i < bmtInterface.wInterfaceMapSize; i++)
{
_ASSERTE(bmtInterface.pInterfaceMap[i].GetInteropStartSlot() != MethodTable::NO_SLOT);
}
}
#endif // _DEBUG
// Place all non vtable methods
for (i = 0; i < bmtVT.wCurrentNonVtableSlot; i++)
{
bmtVT.SetMethodDescForSlot(bmtVT.wCurrentVtableSlot + i, bmtVT.ppSDNonVtable[i]->pMD);
CONSISTENCY_CHECK(bmtVT.ppSDNonVtable[i]->wSlot != MethodTable::NO_SLOT);
bmtVT.ppSDVtable[bmtVT.wCurrentVtableSlot + i] = bmtVT.ppSDNonVtable[i];
}
// Must copy overridden slots to duplicate entries in the vtable
BuildInteropVTable_PropagateInheritance(&bmtVT);
// ensure we didn't overflow the temporary vtable
_ASSERTE(bmtVT.wCurrentNonVtableSlot <= bmtVT.dwMaxVtableSize);
// Finalize.
InteropMethodTableData *pInteropMT = NULL;
FinalizeInteropVTable(
pamTracker,
pThisMT->GetLoaderAllocator(),
&bmtVT,
&bmtInterface,
&bmtType,
&bmtProp,
&bmtMethod,
&bmtError,
&bmtParent,
&pInteropMT);
_ASSERTE(pInteropMT);
#ifdef _DEBUG
if (fDump)
{
CQuickBytes qb;
DWORD cb = 0;
PCCOR_SIGNATURE pSig;
ULONG cbSig;
minipal_log_print_info("InteropMethodTable\n--------------\nVTable\n------\n");
StackSString message;
for (DWORD i = 0; i < pInteropMT->cVTable; i++)
{
// Print the method name
InteropMethodTableSlotData *pInteropMD = &pInteropMT->pVTable[i];
message.AppendUTF8(pInteropMD->pMD->GetName());
message.AppendUTF8(" ");
// Print the sig
if (FAILED(pInteropMD->pMD->GetMDImport()->GetSigOfMethodDef(pInteropMD->pMD->GetMemberDef(), &cbSig, &pSig)))
{
pSig = NULL;
cbSig = 0;
}
PrettyPrintSigInternalLegacy(pSig, cbSig, "", &qb, pInteropMD->pMD->GetMDImport());
message.AppendUTF8((LPCUTF8) qb.Ptr());
message.AppendUTF8("\n");
minipal_log_print_info(message.GetUTF8());
message.Clear();
}
}
#endif // _DEBUG
NullBMTData();
return pInteropMT;
}
//---------------------------------------------------------------------------------------
InteropMethodTableData *MethodTableBuilder::BuildInteropVTableForArray(AllocMemTracker *pamTracker)
{
CONTRACTL {
STANDARD_VM_CHECK;
INSTANCE_CHECK;
PRECONDITION(GetHalfBakedMethodTable()->IsArray());
PRECONDITION(GetHalfBakedMethodTable()->GetNumVirtuals() == GetHalfBakedMethodTable()->GetParentMethodTable()->GetNumVirtuals());
} CONTRACTL_END;
MethodTable * pThisMT = GetHalfBakedMethodTable();
// Get the interop data for the parent
MethodTable *pParentMT = pThisMT->GetParentMethodTable();
InteropMethodTableData *pParentMTData = pParentMT->GetComInteropData();
CONSISTENCY_CHECK(pParentMTData != NULL);
// Allocate in the same heap as the array itself
LoaderHeap *pHeap = pThisMT->GetLoaderAllocator()->GetLowFrequencyHeap();
// Allocate the overall structure
InteropMethodTableData *pMTData = (InteropMethodTableData *)(void *) pamTracker->Track(pHeap->AllocMem(S_SIZE_T(sizeof(InteropMethodTableData))));
memset(pMTData, 0, sizeof(InteropMethodTableData));
// Allocate the vtable - this is just a copy from System.Array
pMTData->cVTable = pParentMTData->cVTable;
if (pMTData->cVTable != 0)
{
pMTData->pVTable = (InteropMethodTableSlotData *)(void *)
pamTracker->Track(pHeap->AllocMem(S_SIZE_T(sizeof(InteropMethodTableSlotData)) * S_SIZE_T(pMTData->cVTable)));
// Copy the vtable
for (DWORD i = 0; i < pMTData->cVTable; i++)
pMTData->pVTable[i] = pParentMTData->pVTable[i];
}
// Allocate the non-vtable
pMTData->cNonVTable = pThisMT->GetNumMethods() - pThisMT->GetNumVirtuals();
if (pMTData->cNonVTable != 0)
{
pMTData->pNonVTable = (InteropMethodTableSlotData *)(void *)
pamTracker->Track(pHeap->AllocMem(S_SIZE_T(sizeof(InteropMethodTableSlotData)) * S_SIZE_T(pMTData->cNonVTable)));
// Copy the non-vtable
UINT32 iCurRealSlot = pThisMT->GetNumVirtuals();
WORD iCurInteropSlot = pMTData->cVTable;
for (DWORD i = 0; i < pMTData->cNonVTable; i++, iCurRealSlot++, iCurInteropSlot++)
{
pMTData->pNonVTable[i].wSlot = iCurInteropSlot;
pMTData->pNonVTable[i].pMD = pThisMT->GetMethodDescForSlot(iCurRealSlot);
}
}
// Allocate the interface map
pMTData->cInterfaceMap = pParentMTData->cInterfaceMap;
if (pMTData->cInterfaceMap != 0)
{
pMTData->pInterfaceMap = (InterfaceInfo_t *)(void *)
pamTracker->Track(pHeap->AllocMem(S_SIZE_T(sizeof(InterfaceInfo_t)) * S_SIZE_T(pMTData->cInterfaceMap)));
// Copy the interface map
for (DWORD i = 0; i < pMTData->cInterfaceMap; i++)
pMTData->pInterfaceMap[i] = pParentMTData->pInterfaceMap[i];
}
return pMTData;
}
//---------------------------------------------------------------------------------------
VOID MethodTableBuilder::BuildInteropVTable_InterfaceList(
BuildingInterfaceInfo_t **ppBuildingInterfaceList,
WORD *pcBuildingInterfaceList)
{
STANDARD_VM_CONTRACT;
// Initialize arguments
*pcBuildingInterfaceList = 0;
*ppBuildingInterfaceList = NULL;
// Get the metadata for enumerating the interfaces of the class
IMDInternalImport *pMDImport = GetModule()->GetMDImport();
// Now load all the interfaces
HENUMInternalHolder hEnumInterfaceImpl(pMDImport);
hEnumInterfaceImpl.EnumInit(mdtInterfaceImpl, GetCl());
// Get the count for the number of interfaces from metadata
DWORD cAllInterfaces = pMDImport->EnumGetCount(&hEnumInterfaceImpl);
WORD cNonGenericItfs = 0;
// Iterate through each interface token and get the type for the interface and put
// it into the BuildingInterfaceInfo_t struct.
if (cAllInterfaces != 0)
{
mdInterfaceImpl ii;
Module *pModule = GetModule();
// Allocate the BuildingInterfaceList table
*ppBuildingInterfaceList = new(GetStackingAllocator()) BuildingInterfaceInfo_t[cAllInterfaces];
BuildingInterfaceInfo_t *pInterfaceBuildInfo = *ppBuildingInterfaceList;
while (pMDImport->EnumNext(&hEnumInterfaceImpl, &ii))
{
mdTypeRef crInterface;
TypeHandle intType;
// Get properties on this interface
if (FAILED(pMDImport->GetTypeOfInterfaceImpl(ii, &crInterface)))
{
BuildMethodTableThrowException(IDS_CLASSLOAD_BADFORMAT);
}
SigTypeContext typeContext = SigTypeContext(TypeHandle(GetHalfBakedMethodTable()));
intType = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, crInterface, &typeContext,
ClassLoader::ThrowIfNotFound,
ClassLoader::FailIfUninstDefOrRef);
// At this point, the interface should never have any non instantiated generic parameters.
_ASSERTE(!intType.IsGenericTypeDefinition());
// Skip any generic interfaces.
if (intType.GetNumGenericArgs() != 0)
continue;
pInterfaceBuildInfo[cNonGenericItfs].m_pMethodTable = intType.AsMethodTable();
_ASSERTE(pInterfaceBuildInfo[cNonGenericItfs].m_pMethodTable != NULL);
_ASSERTE(pInterfaceBuildInfo[cNonGenericItfs].m_pMethodTable->IsInterface());
cNonGenericItfs++;
}
_ASSERTE(cNonGenericItfs <= cAllInterfaces);
}
*pcBuildingInterfaceList = cNonGenericItfs;
}
//---------------------------------------------------------------------------------------
// Used by BuildInteropVTable
//
// Determine vtable placement for each member in this class
//
#ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:21000) // Suppress PREFast warning about overly large function
#endif
VOID MethodTableBuilder::BuildInteropVTable_PlaceMembers(
bmtTypeInfo* bmtType,
DWORD numDeclaredInterfaces,
BuildingInterfaceInfo_t *pBuildingInterfaceList,
bmtMethodInfo* bmtMethod,
bmtErrorInfo* bmtError,
bmtProperties* bmtProp,
bmtParentInfo* bmtParent,
bmtInterfaceInfo* bmtInterface,
bmtMethodImplInfo* bmtMethodImpl,
bmtVtable* bmtVT)
{
CONTRACTL
{
STANDARD_VM_CHECK;
PRECONDITION(CheckPointer(this));
PRECONDITION(CheckPointer(bmtType));
PRECONDITION(CheckPointer(bmtMethod));
PRECONDITION(CheckPointer(bmtError));
PRECONDITION(CheckPointer(bmtProp));
PRECONDITION(CheckPointer(bmtInterface));
PRECONDITION(CheckPointer(bmtParent));
PRECONDITION(CheckPointer(bmtMethodImpl));
PRECONDITION(CheckPointer(bmtVT));
}
CONTRACTL_END;
_ASSERTE(!IsInterface());
Module * pModule = GetModule();
#ifdef _DEBUG
LPCUTF8 pszDebugName,pszDebugNamespace;
if (FAILED(bmtType->pModule->GetMDImport()->GetNameOfTypeDef(GetCl(), &pszDebugName, &pszDebugNamespace)))
{
pszDebugName = pszDebugNamespace = "Invalid TypeDef record";
}
#endif // _DEBUG
HRESULT hr = S_OK;
DWORD i, j;
DWORD dwClassDeclFlags = 0xffffffff;
DWORD dwClassNullDeclFlags = 0xffffffff;
for (i = 0; i < NumDeclaredMethods(); i++)
{
LPCUTF8 szMemberName = NULL;
PCCOR_SIGNATURE pMemberSignature = NULL;
DWORD cMemberSignature = 0;
mdToken tokMember;
DWORD dwMemberAttrs;
DWORD dwDescrOffset;
DWORD dwImplFlags;
BOOL fMethodImplementsInterface = FALSE;
DWORD dwMDImplementsInterfaceNum = 0;
DWORD dwMDImplementsSlotNum = 0;
DWORD dwParentAttrs;
tokMember = bmtMethod->rgMethodTokens[i];
dwMemberAttrs = bmtMethod->rgMethodAttrs[i];
dwDescrOffset = bmtMethod->rgMethodRVA[i];
dwImplFlags = bmtMethod->rgMethodImplFlags[i];
DWORD Classification = bmtMethod->rgMethodClassifications[i];
// If this member is a method which overrides a parent method, it will be set to non-NULL
MethodDesc *pParentMethodDesc = NULL;
szMemberName = bmtMethod->rgszMethodName[i];
// constructors and class initialisers are special
if (!IsMdRTSpecialName(dwMemberAttrs))
{
// The method does not have the special marking
if (IsMdVirtual(dwMemberAttrs))
{
// Hash that a method with this name exists in this class
// Note that ctors and static ctors are not added to the table
DWORD dwHashName = HashStringA(szMemberName);
// If the member is marked with a new slot we do not need to find it
// in the parent
if (!IsMdNewSlot(dwMemberAttrs))
{
// If we're not doing sanity checks, then assume that any method declared static
// does not attempt to override some virtual parent.
if (!IsMdStatic(dwMemberAttrs) && bmtParent->pParentMethodTable != NULL)
{
// Attempt to find the method with this name and signature in the parent class.
// This method may or may not create pParentMethodHash (if it does not already exist).
// It also may or may not fill in pMemberSignature/cMemberSignature.
// An error is only returned when we can not create the hash.
// NOTE: This operation touches metadata
{
BOOL fMethodConstraintsMatch = FALSE;
VERIFY(SUCCEEDED(LoaderFindMethodInClass(
szMemberName,
bmtType->pModule,
tokMember,
&pParentMethodDesc,
&pMemberSignature, &cMemberSignature,
dwHashName,
&fMethodConstraintsMatch)));
//this assert should hold because interop methods cannot be generic
_ASSERTE(pParentMethodDesc == NULL || fMethodConstraintsMatch);
}
if (pParentMethodDesc != NULL)
{
dwParentAttrs = pParentMethodDesc->GetAttrs();
_ASSERTE(IsMdVirtual(dwParentAttrs) && "Non virtual methods should not be searched");
_ASSERTE(!(IsMdFinal(dwParentAttrs)));
}
}
}
}
}
if(pParentMethodDesc == NULL) {
// This method does not exist in the parent. If we are a class, check whether this
// method implements any interface. If true, we can't place this method now.
if ((!IsInterface()) &&
( IsMdPublic(dwMemberAttrs) &&
IsMdVirtual(dwMemberAttrs) &&
!IsMdStatic(dwMemberAttrs) &&
!IsMdRTSpecialName(dwMemberAttrs))) {
// Don't check parent class interfaces - if the parent class had to implement an interface,
// then it is already guaranteed that we inherited that method.
_ASSERTE(!bmtParent->pParentMethodTable || bmtParent->pParentMethodTable->LookupComInteropData());
DWORD numInheritedInts = (bmtParent->pParentMethodTable ?
(DWORD) bmtParent->pParentMethodTable->LookupComInteropData()->cInterfaceMap: 0);
for (j = numInheritedInts; j < bmtInterface->wInterfaceMapSize; j++)
{
MethodTable *pInterface = bmtInterface->pInterfaceMap[j].m_pMethodTable;
if (pMemberSignature == NULL)
{ // We've been trying to avoid asking for the signature - now we need it
if (FAILED(bmtType->pMDImport->GetSigOfMethodDef(tokMember, &cMemberSignature, &pMemberSignature)))
{
BuildMethodTableThrowException(IDS_CLASSLOAD_BADFORMAT);
}
}
WORD slotNum = (WORD) (-1);
MethodDesc *pItfMD = MemberLoader::FindMethod(pInterface,
szMemberName, pMemberSignature, cMemberSignature, bmtType->pModule);
if (pItfMD != NULL)
{
// This method implements an interface - don't place it
fMethodImplementsInterface = TRUE;
// Keep track of this fact and use it while placing the interface
slotNum = (WORD) pItfMD->GetSlot();
if (bmtInterface->pppInterfaceImplementingMD[j] == NULL)
{
bmtInterface->pppInterfaceImplementingMD[j] = new (GetStackingAllocator()) MethodDesc * [pInterface->GetNumVirtuals()];
memset(bmtInterface->pppInterfaceImplementingMD[j], 0, sizeof(MethodDesc *) * pInterface->GetNumVirtuals());
bmtInterface->pppInterfaceDeclaringMD[j] = new (GetStackingAllocator()) MethodDesc * [pInterface->GetNumVirtuals()];
memset(bmtInterface->pppInterfaceDeclaringMD[j], 0, sizeof(MethodDesc *) * pInterface->GetNumVirtuals());
}
bmtInterface->pppInterfaceDeclaringMD[j][slotNum] = pItfMD;
dwMDImplementsInterfaceNum = j;
dwMDImplementsSlotNum = slotNum;
break;
}
}
}
}
// Now find the MethodDesc associated with this method
MethodDesc *pNewMD = MemberLoader::FindMethod(GetHalfBakedMethodTable(), tokMember);
_ASSERTE(!bmtVT->pInteropData->Exists(pNewMD));
InteropMethodTableSlotData *pNewMDData = bmtVT->pInteropData->GetData(pNewMD);
_ASSERTE(pNewMD != NULL);
_ASSERTE(dwMemberAttrs == pNewMD->GetAttrs());
_ASSERTE(bmtParent->ppParentMethodDescBufPtr != NULL);
_ASSERTE(((bmtParent->ppParentMethodDescBufPtr - bmtParent->ppParentMethodDescBuf) / sizeof(MethodDesc*))
< NumDeclaredMethods());
*(bmtParent->ppParentMethodDescBufPtr++) = pParentMethodDesc;
*(bmtParent->ppParentMethodDescBufPtr++) = pNewMD;
if (fMethodImplementsInterface && IsMdVirtual(dwMemberAttrs))
{
bmtInterface->pppInterfaceImplementingMD[dwMDImplementsInterfaceNum][dwMDImplementsSlotNum] = pNewMD;
}
// Set the MethodDesc value
bmtMethod->ppMethodDescList[i] = pNewMD;
// Make sure that fcalls have a 0 rva. This is assumed by the prejit fixup logic
_ASSERTE(((Classification & ~mdfMethodImpl) != mcFCall) || dwDescrOffset == 0);
// Non-virtual method
if (IsMdStatic(dwMemberAttrs) ||
!IsMdVirtual(dwMemberAttrs) ||
IsMdRTSpecialName(dwMemberAttrs))
{
// Non-virtual method (doesn't go into the vtable)
_ASSERTE(bmtVT->pNonVtableMD[bmtVT->wCurrentNonVtableSlot] == NULL);
// Set the data for the method
pNewMDData->wSlot = bmtVT->wCurrentNonVtableSlot;
// Add the slot into the non-virtual method table
bmtVT->pNonVtableMD[bmtVT->wCurrentNonVtableSlot] = pNewMD;
bmtVT->ppSDNonVtable[bmtVT->wCurrentNonVtableSlot] = pNewMDData;
// Increment the current non-virtual method table slot
bmtVT->wCurrentNonVtableSlot++;
}
// Virtual method
else
{
if (IsInterface())
{ // (shouldn't happen for this codepath)
UNREACHABLE();
}
else if (pParentMethodDesc != NULL)
{ // We are overriding a parent's vtable slot
CONSISTENCY_CHECK(bmtVT->pInteropData->Exists(pParentMethodDesc));
WORD slotNumber = bmtVT->pInteropData->GetData(pParentMethodDesc)->wSlot;
// If the MethodDesc was inherited by an interface but not implemented,
// then the interface's MethodDesc is sitting in the slot and will not reflect
// the true slot number. Need to find the starting slot of the interface in
// the parent class to figure out the true slot (starting slot + itf slot)
if (pParentMethodDesc->IsInterface())
{
MethodTable *pItfMT = pParentMethodDesc->GetMethodTable();
WORD startSlot = bmtParent->pParentMethodTable->LookupComInteropData()->GetStartSlotForInterface(pItfMT);
_ASSERTE(startSlot != (WORD) -1);
slotNumber += startSlot;
}
// we are overriding a parent method, so place this method now
bmtVT->SetMethodDescForSlot(slotNumber, pNewMD);
bmtVT->ppSDVtable[slotNumber] = pNewMDData;
pNewMDData->wSlot = slotNumber;
}
else if (!fMethodImplementsInterface)
{ // Place it unless we will do it when laying out an interface or it is a body to
// a method impl. If it is an impl then we will use the slots used by the definition.
// Store the slot for this method
pNewMDData->wSlot = bmtVT->wCurrentVtableSlot;
// Now copy the method into the vtable, and interop data
bmtVT->SetMethodDescForSlot(bmtVT->wCurrentVtableSlot, pNewMD);
bmtVT->ppSDVtable[bmtVT->wCurrentVtableSlot] = pNewMDData;
// Increment current vtable slot, since we're not overriding a parent slot
bmtVT->wCurrentVtableSlot++;
}
}
if (Classification & mdfMethodImpl)
{ // If this method serves as the BODY of a MethodImpl specification, then
// we should iterate all the MethodImpl's for this class and see just how many
// of them this method participates in as the BODY.
for(DWORD m = 0; m < bmtMethodImpl->dwNumberMethodImpls; m++)
{
if(tokMember == bmtMethodImpl->rgMethodImplTokens[m].methodBody)
{
MethodDesc* desc = NULL;
mdToken mdDecl = bmtMethodImpl->rgMethodImplTokens[m].methodDecl;
Substitution *pDeclSubst = &bmtMethodImpl->pMethodDeclSubsts[m];
// Get the parent
mdToken tkParent = mdTypeDefNil;
if (TypeFromToken(mdDecl) == mdtMethodDef || TypeFromToken(mdDecl) == mdtMemberRef)
{
hr = bmtType->pMDImport->GetParentToken(mdDecl,&tkParent);
if (FAILED(hr))
{
BuildMethodTableThrowException(hr, *bmtError);
}
}
if (GetCl() == tkParent)
{ // The DECL has been declared
// within the class that we're currently building.
hr = S_OK;
if(bmtError->pThrowable != NULL)
*(bmtError->pThrowable) = NULL;
// <TODO>Verify that the substitution doesn't change for this case </TODO>
if(TypeFromToken(mdDecl) != mdtMethodDef) {
hr = FindMethodDeclarationForMethodImpl(
bmtType->pMDImport,
GetCl(),
mdDecl,
&mdDecl);
_ASSERTE(SUCCEEDED(hr));
// Make sure the virtual states are the same
DWORD dwDescAttrs;
if (FAILED(bmtType->pMDImport->GetMethodDefProps(mdDecl, &dwDescAttrs)))
{
BuildMethodTableThrowException(IDS_CLASSLOAD_BADFORMAT);
}
_ASSERTE(IsMdVirtual(dwMemberAttrs) == IsMdVirtual(dwDescAttrs));
}
}
else
{
SigTypeContext typeContext;
desc = MemberLoader::GetMethodDescFromMemberDefOrRefOrSpec(bmtType->pModule,
mdDecl,
&typeContext,