summaryrefslogtreecommitdiff
path: root/src/vm/typehash.cpp
blob: 4c3b953dbe6d688a4f3a32bfebdaf5567d7a1ccc (plain)
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//
// File: typehash.cpp
//

//

#include "common.h"
#include "excep.h"
#include "typehash.h"
#include "eeconfig.h"
#include "generics.h"
#include "typestring.h"
#include "typedesc.h"
#include "typekey.h"
#ifdef FEATURE_PREJIT
#include "zapsig.h"
#include "compile.h"
#endif
#include "ngenhash.inl"

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4244)
#endif // _MSC_VER

#ifndef DACCESS_COMPILE

// ============================================================================
// Class hash table methods
// ============================================================================
/* static */
EETypeHashTable *EETypeHashTable::Create(LoaderAllocator* pAllocator, Module *pModule, DWORD dwNumBuckets, AllocMemTracker *pamTracker)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM(););
    }
    CONTRACTL_END

    LoaderHeap *pHeap = pAllocator->GetLowFrequencyHeap();
    EETypeHashTable *pThis = (EETypeHashTable*)pamTracker->Track(pHeap->AllocMem((S_SIZE_T)sizeof(EETypeHashTable)));

    new (pThis) EETypeHashTable(pModule, pHeap, dwNumBuckets);

#ifdef _DEBUG
    pThis->InitUnseal();
#endif

    pThis->m_pAllocator = pAllocator;

    return pThis;
}

LoaderAllocator *EETypeHashTable::GetLoaderAllocator()
{
    WRAPPER_NO_CONTRACT;

    if (m_pAllocator)
    {
        return m_pAllocator;
    }
    else
    {
        _ASSERTE(m_pModule != NULL);
        return m_pModule->GetLoaderAllocator();
    }
}

#endif // #ifdef DACCESS_COMPILE

void EETypeHashTable::Iterator::Reset()
{
    WRAPPER_NO_CONTRACT;

    if (m_pTable)
    {
#ifdef _DEBUG
        m_pTable->Unseal();
#endif
        m_pTable = NULL;
    }

    Init();
}

void EETypeHashTable::Iterator::Init()
{
    WRAPPER_NO_CONTRACT;

#ifdef _DEBUG
    if (m_pTable)
        m_pTable->Seal(); // The table cannot be changing while it is being iterated
#endif

    m_fIterating = false;
}

EETypeHashTable::Iterator::Iterator()
{
    WRAPPER_NO_CONTRACT; 
    m_pTable = NULL;
    Init(); 
}

EETypeHashTable::Iterator::Iterator(EETypeHashTable * pTable)
{
    WRAPPER_NO_CONTRACT;
    m_pTable = pTable;
    Init();
}

EETypeHashTable::Iterator::~Iterator()
{
    WRAPPER_NO_CONTRACT;

#ifdef _DEBUG
    if (m_pTable)
        m_pTable->Unseal(); // Done with the iterator so we unseal
#endif
}

BOOL EETypeHashTable::FindNext(Iterator *it, EETypeHashEntry **ppEntry)
{
    LIMITED_METHOD_CONTRACT;

    if (!it->m_fIterating)
    {
        BaseInitIterator(&it->m_sIterator);
        it->m_fIterating = true;
    }

    *ppEntry = it->m_sIterator.Next();
    return *ppEntry ? TRUE : FALSE;
}

DWORD EETypeHashTable::GetCount()
{
    LIMITED_METHOD_CONTRACT;

    return BaseGetElementCount();
}

static DWORD HashTypeHandle(DWORD level, TypeHandle t);

// Calculate hash value for a type def or instantiated type def
static DWORD HashPossiblyInstantiatedType(DWORD level, mdTypeDef token, Instantiation inst)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(TypeFromToken(token) == mdtTypeDef);
        SUPPORTS_DAC;
    }
    CONTRACTL_END

    INT_PTR dwHash = 5381;
    
    dwHash = ((dwHash << 5) + dwHash) ^ token;
    if (!inst.IsEmpty())
    {
        dwHash = ((dwHash << 5) + dwHash) ^ inst.GetNumArgs();

        // Hash two levels of the hiearchy. A simple nesting of generics instantiations is 
        // pretty common in generic collections, e.g.: ICollection<KeyValuePair<TKey, TValue>>
        if (level < 2)
        {
            // Hash n type parameters
            for (DWORD i = 0; i < inst.GetNumArgs(); i++)
            {
                dwHash = ((dwHash << 5) + dwHash) ^ HashTypeHandle(level+1, inst[i]);
            }
        }
    }

    return dwHash;
}

// Calculate hash value for a function pointer type
static DWORD HashFnPtrType(DWORD level, BYTE callConv, DWORD numArgs, TypeHandle *retAndArgTypes)
{
    WRAPPER_NO_CONTRACT;
    SUPPORTS_DAC;
    INT_PTR dwHash = 5381;
    
    dwHash = ((dwHash << 5) + dwHash) ^ ELEMENT_TYPE_FNPTR;
    dwHash = ((dwHash << 5) + dwHash) ^ callConv;
    dwHash = ((dwHash << 5) + dwHash) ^ numArgs;
    if (level < 1)
    {
        for (DWORD i = 0; i <= numArgs; i++)
        {
            dwHash = ((dwHash << 5) + dwHash) ^ HashTypeHandle(level+1, retAndArgTypes[i]);
        }
    }

    return dwHash;
}

// Calculate hash value for an array/pointer/byref type
static DWORD HashParamType(DWORD level, CorElementType kind, TypeHandle typeParam)
{
    WRAPPER_NO_CONTRACT;
    INT_PTR dwHash = 5381;
    
    dwHash = ((dwHash << 5) + dwHash) ^ kind;
    dwHash = ((dwHash << 5) + dwHash) ^ HashTypeHandle(level, typeParam);

    return dwHash;
}

// Calculate hash value from type handle
static DWORD HashTypeHandle(DWORD level, TypeHandle t)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SO_TOLERANT;
        PRECONDITION(CheckPointer(t));
        PRECONDITION(!t.IsEncodedFixup());
        SUPPORTS_DAC;
    }
    CONTRACTL_END;

    DWORD retVal = 0;
    
    INTERIOR_STACK_PROBE_NOTHROW_CHECK_THREAD(goto Exit;);

    if (t.HasTypeParam())
    {
        retVal =  HashParamType(level, t.GetInternalCorElementType(), t.GetTypeParam());
    }
    else if (t.IsGenericVariable())
    {
        retVal = (dac_cast<PTR_TypeVarTypeDesc>(t.AsTypeDesc())->GetToken());
    }
    else if (t.HasInstantiation())
    {
        retVal = HashPossiblyInstantiatedType(level, t.GetCl(), t.GetInstantiation());
    }
    else if (t.IsFnPtrType())
    {
        FnPtrTypeDesc* pTD = t.AsFnPtrType();
        retVal = HashFnPtrType(level, pTD->GetCallConv(), pTD->GetNumArgs(), pTD->GetRetAndArgTypesPointer());
    }
    else
        retVal = HashPossiblyInstantiatedType(level, t.GetCl(), Instantiation());

#if defined(FEATURE_STACK_PROBE) && !defined(DACCESS_COMPILE)
Exit: 
    ;
#endif
    END_INTERIOR_STACK_PROBE;
    
    return retVal;
}

// Calculate hash value from key
static DWORD HashTypeKey(TypeKey* pKey)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(pKey));
        SUPPORTS_DAC;
    }
    CONTRACTL_END;

    if (pKey->GetKind() == ELEMENT_TYPE_CLASS)
    {
        return HashPossiblyInstantiatedType(0, pKey->GetTypeToken(), pKey->GetInstantiation());
    }
    else if (pKey->GetKind() == ELEMENT_TYPE_FNPTR)
    {
        return HashFnPtrType(0, pKey->GetCallConv(), pKey->GetNumArgs(), pKey->GetRetAndArgTypes());
    }
    else
    {
        return HashParamType(0, pKey->GetKind(), pKey->GetElementType());
    }
}

// Look up a value in the hash table 
//
// The logic is subtle: type handles in the hash table may not be
// restored, but we need to compare components of the types (rank and
// element type for arrays, generic type and instantiation for
// instantiated types) against pKey
// 
// We avoid restoring types during search by cracking the signature
// encoding used by the zapper for out-of-module types e.g. in the
// instantiation of an instantiated type.
EETypeHashEntry_t *EETypeHashTable::FindItem(TypeKey* pKey)
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(pKey));
        SUPPORTS_DAC;
    }
    CONTRACTL_END;

    EETypeHashEntry_t *  result = NULL;

    DWORD           dwHash = HashTypeKey(pKey);
    EETypeHashEntry_t * pSearch;
    CorElementType kind = pKey->GetKind();
    LookupContext sContext;

    if (kind == ELEMENT_TYPE_CLASS)
    {
        pSearch = BaseFindFirstEntryByHash(dwHash, &sContext);
        while (pSearch)
        {
            if (CompareInstantiatedType(pSearch->GetTypeHandle(), pKey->GetModule(), pKey->GetTypeToken(), pKey->GetInstantiation()))
            {
                result = pSearch;
                break;
            }

            pSearch = BaseFindNextEntryByHash(&sContext);
        }
    }
    else if (kind == ELEMENT_TYPE_FNPTR) 
    {
        BYTE callConv = pKey->GetCallConv();
        DWORD numArgs = pKey->GetNumArgs();
        TypeHandle *retAndArgTypes = pKey->GetRetAndArgTypes();

        pSearch = BaseFindFirstEntryByHash(dwHash, &sContext);
        while (pSearch)
        {
            if (CompareFnPtrType(pSearch->GetTypeHandle(), callConv, numArgs, retAndArgTypes))
            {
                result = pSearch;
                break;
            }

            pSearch = BaseFindNextEntryByHash(&sContext);
        }
    }
    else
    {
        // Type parameters for array and pointer types are necessarily in the same loader module
        // as the constructed type itself, so we can just do handle comparisons
        // Unfortunately the rank of the array might live elsewhere

        for (pSearch = BaseFindFirstEntryByHash(dwHash, &sContext);
             pSearch != NULL;
             pSearch = BaseFindNextEntryByHash(&sContext))
        {
            if (!pSearch->GetTypeHandle().IsRestored())
            {
                // workaround: If we encounter an unrestored MethodTable, then it
                // isn't the type for which we are looking (plus, it will crash
                // in GetSignatureCorElementType).  However TypeDescs can be
                // accessed when unrestored.  Also they are accessed in that
                // manner at startup when we're loading the global types
                // (i.e. System.Object).
                
                if (!pSearch->GetTypeHandle().IsTypeDesc())
                {
                    // Not a match
                   continue;
                }
                else
                {
                    // We have an unrestored TypeDesc
                }
            }

            if (pSearch->GetTypeHandle().GetSignatureCorElementType() != kind)
                continue;

            if (pSearch->GetTypeHandle().GetTypeParam() != pKey->GetElementType())
                continue;

            if (pSearch->GetTypeHandle().IsTypeDesc() == pKey->IsTemplateMethodTable())
                continue;

            if (kind == ELEMENT_TYPE_ARRAY)
            {
                if (pKey->IsTemplateMethodTable())
                {
                    if (pSearch->GetTypeHandle().AsMethodTable()->GetRank() != pKey->GetRank())
                        continue;
                }
                else
                {
                    ArrayTypeDesc *pATD = pSearch->GetTypeHandle().AsArray();   
#ifdef FEATURE_PREJIT
                    // This ensures that GetAssemblyIfLoaded operations that may be triggered by signature walks will succeed if at all possible.
                    ClrFlsThreadTypeSwitch genericInstantionCompareHolder(ThreadType_GenericInstantiationCompare); 

                    TADDR fixup = pATD->GetTemplateMethodTableMaybeTagged();
                    if (!CORCOMPILE_IS_POINTER_TAGGED(fixup))
                    {
                        TADDR canonFixup = pATD->GetTemplateMethodTable()->GetCanonicalMethodTableFixup();
                        if (CORCOMPILE_IS_POINTER_TAGGED(canonFixup))
                            fixup = canonFixup;
                    }

                    if (CORCOMPILE_IS_POINTER_TAGGED(fixup))
                    {
                        Module *pDefiningModule;
                        PCCOR_SIGNATURE pSig = m_pModule->GetEncodedSigIfLoaded(CORCOMPILE_UNTAG_TOKEN(fixup), &pDefiningModule);
                        if (pDefiningModule == NULL)
                            break;

                        _ASSERTE(*pSig == ELEMENT_TYPE_NATIVE_ARRAY_TEMPLATE_ZAPSIG);
                        pSig++;
                        _ASSERTE(*pSig == ELEMENT_TYPE_ARRAY);
                        pSig++;
                        SigPointer sp(pSig);
                        if (FAILED(sp.SkipExactlyOne()))
                            break; // return NULL;

                        ULONG data;
                        if (FAILED(sp.GetData(&data)))
                            break; // return NULL;
                        
                        if (data != pKey->GetRank())
                            continue;
                    }
                    else
#endif //FEATURE_PREJIT
                    {
                        if (pATD->GetRank() != pKey->GetRank())
                            continue;
                    }
                }
            }

            result = pSearch;
            break;
        }
    }

    return result;
}

BOOL EETypeHashTable::CompareInstantiatedType(TypeHandle t, Module *pModule, mdTypeDef token, Instantiation inst)
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(t));
        PRECONDITION(CheckPointer(pModule));
        PRECONDITION(!inst.IsEmpty());
        SUPPORTS_DAC;
    }
    CONTRACTL_END

    if (t.IsTypeDesc())
        return FALSE;
    
    // Even the EEClass pointer might be encoded
    MethodTable * pMT = t.AsMethodTable();

    if (pMT->GetNumGenericArgs() != inst.GetNumArgs())
        return FALSE;

#ifdef FEATURE_PREJIT
    // This ensures that GetAssemblyIfLoaded operations that may be triggered by signature walks will succeed if at all possible.
    ClrFlsThreadTypeSwitch genericInstantionCompareHolder(ThreadType_GenericInstantiationCompare); 

    TADDR fixup = pMT->GetCanonicalMethodTableFixup();
        
    // The EEClass pointer is actually an encoding. 
    if (CORCOMPILE_IS_POINTER_TAGGED(fixup))
    {
        Module *pDefiningModule;
        PCCOR_SIGNATURE pSig = m_pModule->GetEncodedSigIfLoaded(CORCOMPILE_UNTAG_TOKEN(fixup), &pDefiningModule);

        // First check that the modules for the generic type defs match
        if (dac_cast<TADDR>(pDefiningModule) !=
            dac_cast<TADDR>(pModule))
            return FALSE;

        // Now crack the signature encoding, expected to be an instantiated type
        _ASSERTE(*pSig == ELEMENT_TYPE_GENERICINST);
        pSig++;
        _ASSERTE(*pSig == ELEMENT_TYPE_CLASS || *pSig == ELEMENT_TYPE_VALUETYPE);
        pSig++;

        // Check that the tokens of the generic type def match
        if (CorSigUncompressToken(pSig) != token)
            return FALSE;
    }

    // The EEClass pointer is a real pointer
    else 
#endif //FEATURE_PREJIT
    {
        // First check that the typedef tokens match
        if (pMT->GetCl() != token)
            return FALSE;

        // The class might not be restored, and its metadata module pointer might be encoded.
        // This will return NULL if the module for the corresponding generic class
        // is not loaded.
        Module *pGenericModuleIfLoaded = pMT->GetModuleIfLoaded();

        // Now check that the modules match 
        if (!pGenericModuleIfLoaded ||
            dac_cast<TADDR>(pGenericModuleIfLoaded) !=
            dac_cast<TADDR>(pModule))
            return FALSE;

    }

    Instantiation candidateInst = t.GetInstantiation();

    // Now check the instantiations. Some type arguments might be encoded.
    for (DWORD i = 0; i < inst.GetNumArgs(); i++)
    {
        // Fetch the type handle as TADDR. It may be may be encoded fixup - TypeHandle debug-only validation 
        // asserts on encoded fixups.
        DACCOP_IGNORE(CastOfMarshalledType, "Dual mode DAC problem, but since the size is the same, the cast is safe");
        TADDR candidateArg = ((FixupPointer<TADDR> *)candidateInst.GetRawArgs())[i].GetValue();

        if (!ZapSig::CompareTaggedPointerToTypeHandle(m_pModule, candidateArg, inst[i]))
        {
            return FALSE;
        }
    }
    
    return TRUE;
}

BOOL EETypeHashTable::CompareFnPtrType(TypeHandle t, BYTE callConv, DWORD numArgs, TypeHandle *retAndArgTypes)
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(t));
        PRECONDITION(CheckPointer(retAndArgTypes));
        SUPPORTS_DAC;
    }
    CONTRACTL_END

    if (!t.IsFnPtrType())
        return FALSE;
    
#ifndef DACCESS_COMPILE
#ifdef FEATURE_PREJIT
    // This ensures that GetAssemblyIfLoaded operations that may be triggered by signature walks will succeed if at all possible.
    ClrFlsThreadTypeSwitch genericInstantionCompareHolder(ThreadType_GenericInstantiationCompare); 
#endif

    FnPtrTypeDesc* pTD = t.AsFnPtrType();

    if (pTD->GetNumArgs() != numArgs || pTD->GetCallConv() != callConv)
        return FALSE;

    // Now check the return and argument types. Some type arguments might be encoded.
    TypeHandle *retAndArgTypes2 = pTD->GetRetAndArgTypesPointer();
    for (DWORD i = 0; i <= numArgs; i++)
    {
        TADDR candidateArg = retAndArgTypes2[i].AsTAddr();
        if (!ZapSig::CompareTaggedPointerToTypeHandle(m_pModule, candidateArg, retAndArgTypes[i]))
        {
            return FALSE;
        }
    }
    
    return TRUE;

#else
    DacNotImpl();
    return FALSE;
#endif // #ifndef DACCESS_COMPILE
}

TypeHandle EETypeHashTable::GetValue(TypeKey *pKey)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SUPPORTS_DAC;
    }
    CONTRACTL_END;

    EETypeHashEntry_t *pItem = FindItem(pKey);

    if (pItem)
    {
        TypeHandle th = pItem->GetTypeHandle();
        g_IBCLogger.LogTypeHashTableAccess(&th);
        return pItem->GetTypeHandle();
    }
    else
        return TypeHandle();
}

#ifndef DACCESS_COMPILE

BOOL EETypeHashTable::ContainsValue(TypeHandle th)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_INTOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;

    TypeKey typeKey = th.GetTypeKey();
    return !GetValue(&typeKey).IsNull();
}

// Insert a value not already in the hash table
VOID EETypeHashTable::InsertValue(TypeHandle data)
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM(););
        PRECONDITION(IsUnsealed());          // If we are sealed then we should not be adding to this hashtable
        PRECONDITION(CheckPointer(data));
        PRECONDITION(!data.IsEncodedFixup());
        PRECONDITION(!data.IsGenericTypeDefinition()); // Generic type defs live in typedef table (availableClasses)
        PRECONDITION(data.HasInstantiation() || data.HasTypeParam() || data.IsFnPtrType()); // It's an instantiated type or an array/ptr/byref type
        PRECONDITION(!m_pModule || m_pModule->IsTenured()); // Destruct won't destruct m_pAvailableParamTypes for non-tenured modules - so make sure no one tries to insert one before the Module has been tenured
    }
    CONTRACTL_END

    EETypeHashEntry_t * pNewEntry = (EETypeHashEntry_t*)BaseAllocateEntry(NULL);

    pNewEntry->SetTypeHandle(data);

    BaseInsertEntry(HashTypeHandle(0, data), pNewEntry);
}

#ifdef FEATURE_NATIVE_IMAGE_GENERATION

#ifdef _DEBUG
void EETypeHashTableSeal(EETypeHashTable * pTable) { WRAPPER_NO_CONTRACT; pTable->Seal(); }
void EETypeHashTableUnseal(EETypeHashTable * pTable) { WRAPPER_NO_CONTRACT; pTable->Unseal(); }
typedef  Wrapper<EETypeHashTable *, EETypeHashTableSeal, EETypeHashTableUnseal> EETypeHashTableSealHolder;
#endif

// Save the hash table and any type descriptors referenced by it
// Method tables must be saved separately
void EETypeHashTable::Save(DataImage *image, Module *module, CorProfileData *profileData)
{
    CONTRACTL
    {
        STANDARD_VM_CHECK;
        PRECONDITION(image->GetModule() == m_pModule);
    }
    CONTRACTL_END;

#ifdef _DEBUG
    // The table should not change while we are walking the buckets
    EETypeHashTableSealHolder h(this);
#endif

    // The base class will call us back for every entry to see if it's considered hot. To determine this we
    // have to walk through the profiling data. It's very inefficient for us to do this every time. Instead
    // we'll walk the data once just now and mark each hot entry as we find it.
    CORBBTPROF_TOKEN_INFO * pTypeProfilingData = profileData->GetTokenFlagsData(TypeProfilingData);
    DWORD                   cTypeProfilingData = profileData->GetTokenFlagsCount(TypeProfilingData);

    for (unsigned int i = 0; i < cTypeProfilingData; i++)
    {
        CORBBTPROF_TOKEN_INFO *entry = &pTypeProfilingData[i];
        mdToken token = entry->token;
        DWORD   flags = entry->flags;
                
        if (TypeFromToken(token) != ibcTypeSpec)
            continue;

        if ((flags & (1 << ReadTypeHashTable)) == 0)
            continue;

        CORBBTPROF_BLOB_ENTRY *pBlobEntry = profileData->GetBlobStream();
        if (pBlobEntry)
        {
            while (pBlobEntry->TypeIsValid())
            {
                if (TypeFromToken(pBlobEntry->token) == ibcTypeSpec)
                {
                    _ASSERTE(pBlobEntry->type == ParamTypeSpec);
                            
                    CORBBTPROF_BLOB_PARAM_SIG_ENTRY *pBlobSigEntry = (CORBBTPROF_BLOB_PARAM_SIG_ENTRY *) pBlobEntry;
                            
                    if (pBlobEntry->token == token)
                    {
                        if (flags & (1<<ReadTypeHashTable))
                        {
                            TypeHandle th = m_pModule->LoadIBCTypeHelper(pBlobSigEntry);
#if defined(_DEBUG) && !defined(DACCESS_COMPILE)
                            g_pConfig->DebugCheckAndForceIBCFailure(EEConfig::CallSite_8);
#endif
                            if (!th.IsNull())
                            {
                                // Found a hot type. See if we have it in our table.
                                DWORD dwHash = HashTypeHandle(0, th);
                                LookupContext sContext;
                                EETypeHashEntry_t *pSearch = BaseFindFirstEntryByHash(dwHash, &sContext);
                                while (pSearch)
                                {
                                    if (pSearch->GetTypeHandle() == th)
                                    {
                                        // Found the corresponding entry in the table. Mark it as hot.
                                        pSearch->MarkAsHot();
                                        break;
                                    }

                                    pSearch = BaseFindNextEntryByHash(&sContext);
                                }
                            }
                        }
                    }
                }
                pBlobEntry = pBlobEntry->GetNextEntry();
            }
        }
    }

    BaseSave(image, profileData);
}

bool EETypeHashTable::ShouldSave(DataImage *pImage, EETypeHashEntry_t *pEntry)
{
    STANDARD_VM_CONTRACT;

    return !!pImage->GetPreloader()->IsTypeInTransitiveClosureOfInstantiations(CORINFO_CLASS_HANDLE(pEntry->GetTypeHandle().AsPtr()));
}

bool EETypeHashTable::IsHotEntry(EETypeHashEntry_t *pEntry, CorProfileData *pProfileData)
{
    STANDARD_VM_CONTRACT;

    // EETypeHashTable::Save() will have marked the entry as hot if the profile data indicated this.
    return pEntry->IsHot();
}

bool EETypeHashTable::SaveEntry(DataImage *pImage, CorProfileData *pProfileData, EETypeHashEntry_t *pOldEntry, EETypeHashEntry_t *pNewEntry, EntryMappingTable *pMap)
{
    LIMITED_METHOD_CONTRACT;

    return false;
}

void EETypeHashTable::Fixup(DataImage *image)
{
    STANDARD_VM_CONTRACT;

    BaseFixup(image);

    image->ZeroPointerField(this, offsetof(EETypeHashTable, m_pAllocator));

#ifdef _DEBUG
    // The persisted table should be unsealed.
    EETypeHashTable *pNewTable = (EETypeHashTable*) image->GetImagePointer(this);
    pNewTable->InitUnseal();
#endif
}

void EETypeHashTable::FixupEntry(DataImage *pImage, EETypeHashEntry_t *pEntry, void *pFixupBase, DWORD cbFixupOffset)
{
    STANDARD_VM_CONTRACT;

    TypeHandle pType = pEntry->GetTypeHandle();
    _ASSERTE(!pType.IsNull());

    // Clear any hot entry marking in the data, it's not needed after the Save phase.
    pEntry->SetTypeHandle(pType);

    if (pType.IsTypeDesc())
    {
        pImage->FixupField(pFixupBase, cbFixupOffset + offsetof(EETypeHashEntry_t, m_data),
                           pType.AsTypeDesc(), 2);

        pType.AsTypeDesc()->Fixup(pImage);
    }
    else
    {
        pImage->FixupField(pFixupBase, cbFixupOffset + offsetof(EETypeHashEntry_t, m_data),
                           pType.AsMethodTable());

        pType.AsMethodTable()->Fixup(pImage);
    }
}
#endif // FEATURE_NATIVE_IMAGE_GENERATION

#endif // #ifndef DACCESS_COMPILE

#ifdef DACCESS_COMPILE

void
EETypeHashTable::EnumMemoryRegions(CLRDataEnumMemoryFlags flags)
{
    SUPPORTS_DAC;
    
    BaseEnumMemoryRegions(flags);
}

void EETypeHashTable::EnumMemoryRegionsForEntry(EETypeHashEntry_t *pEntry, CLRDataEnumMemoryFlags flags)
{
    SUPPORTS_DAC;

    pEntry->GetTypeHandle().EnumMemoryRegions(flags);
}

#endif // #ifdef DACCESS_COMPILE

TypeHandle EETypeHashEntry::GetTypeHandle()
{
    LIMITED_METHOD_DAC_CONTRACT;

    // Remove any hot entry indicator bit that may have been set as the result of Ngen saving.
    return TypeHandle::FromTAddr(m_data & ~0x1);
}

void EETypeHashEntry::SetTypeHandle(TypeHandle handle)
{
    LIMITED_METHOD_DAC_CONTRACT;

    // We plan to steal the low-order bit of the handle for ngen purposes.
    _ASSERTE((handle.AsTAddr() & 0x1) == 0);
    m_data = handle.AsTAddr();
}

#ifdef FEATURE_PREJIT
bool EETypeHashEntry::IsHot()
{
    LIMITED_METHOD_CONTRACT;

    // Low order bit of data field indicates a hot entry.
    return (m_data & 1) != 0;
}

void EETypeHashEntry::MarkAsHot()
{
    LIMITED_METHOD_CONTRACT;

    // Low order bit of data field indicates a hot entry.
    m_data |= 0x1;
}
#endif // FEATURE_PREJIT

#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER: warning C4244