| 1 | // Licensed to the .NET Foundation under one or more agreements. | 
|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license. | 
|---|
| 3 | // See the LICENSE file in the project root for more information. | 
|---|
| 4 |  | 
|---|
| 5 |  | 
|---|
| 6 |  | 
|---|
| 7 | #include "common.h" | 
|---|
| 8 | #include "fptrstubs.h" | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 | // ------------------------------------------------------- | 
|---|
| 12 | // FuncPtr stubs | 
|---|
| 13 | // ------------------------------------------------------- | 
|---|
| 14 |  | 
|---|
| 15 | Precode* FuncPtrStubs::Lookup(MethodDesc * pMD, PrecodeType type) | 
|---|
| 16 | { | 
|---|
| 17 | CONTRACTL | 
|---|
| 18 | { | 
|---|
| 19 | NOTHROW; | 
|---|
| 20 | GC_TRIGGERS; | 
|---|
| 21 | } | 
|---|
| 22 | CONTRACTL_END; | 
|---|
| 23 |  | 
|---|
| 24 | Precode* pPrecode = NULL; | 
|---|
| 25 | { | 
|---|
| 26 | CrstHolder ch(&m_hashTableCrst); | 
|---|
| 27 | pPrecode = m_hashTable.Lookup(PrecodeKey(pMD, type)); | 
|---|
| 28 | } | 
|---|
| 29 | return pPrecode; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | #ifndef DACCESS_COMPILE | 
|---|
| 34 | // | 
|---|
| 35 | // FuncPtrStubs | 
|---|
| 36 | // | 
|---|
| 37 |  | 
|---|
| 38 | FuncPtrStubs::FuncPtrStubs() | 
|---|
| 39 | : m_hashTableCrst(CrstFuncPtrStubs, CRST_UNSAFE_ANYMODE) | 
|---|
| 40 | { | 
|---|
| 41 | WRAPPER_NO_CONTRACT; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | PrecodeType FuncPtrStubs::GetDefaultType(MethodDesc* pMD) | 
|---|
| 45 | { | 
|---|
| 46 | WRAPPER_NO_CONTRACT; | 
|---|
| 47 |  | 
|---|
| 48 | PrecodeType type = PRECODE_STUB; | 
|---|
| 49 |  | 
|---|
| 50 | #ifdef HAS_FIXUP_PRECODE | 
|---|
| 51 | // Use the faster fixup precode if it is available | 
|---|
| 52 | type = PRECODE_FIXUP; | 
|---|
| 53 | #endif // HAS_FIXUP_PRECODE | 
|---|
| 54 |  | 
|---|
| 55 | return type; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | // | 
|---|
| 59 | // Returns an existing stub, or creates a new one | 
|---|
| 60 | // | 
|---|
| 61 |  | 
|---|
| 62 | PCODE FuncPtrStubs::GetFuncPtrStub(MethodDesc * pMD, PrecodeType type) | 
|---|
| 63 | { | 
|---|
| 64 | CONTRACTL | 
|---|
| 65 | { | 
|---|
| 66 | THROWS; | 
|---|
| 67 | GC_TRIGGERS; | 
|---|
| 68 | SO_INTOLERANT; | 
|---|
| 69 | INJECT_FAULT(ThrowOutOfMemory();); | 
|---|
| 70 | } | 
|---|
| 71 | CONTRACTL_END | 
|---|
| 72 |  | 
|---|
| 73 | Precode* pPrecode = NULL; | 
|---|
| 74 | { | 
|---|
| 75 | CrstHolder ch(&m_hashTableCrst); | 
|---|
| 76 | pPrecode = m_hashTable.Lookup(PrecodeKey(pMD, type)); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | if (pPrecode != NULL) | 
|---|
| 80 | { | 
|---|
| 81 | return pPrecode->GetEntryPoint(); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | PCODE target = NULL; | 
|---|
| 85 |  | 
|---|
| 86 | if (type != GetDefaultType(pMD) && | 
|---|
| 87 | // Always use stable entrypoint for LCG. If the cached precode pointed directly to JITed code, | 
|---|
| 88 | // we would not be able to reuse it when the DynamicMethodDesc got reused for a new DynamicMethod. | 
|---|
| 89 | !pMD->IsLCGMethod()) | 
|---|
| 90 | { | 
|---|
| 91 | // Set the target if precode is not of the default type. We are patching the precodes of the default type only. | 
|---|
| 92 | target = pMD->GetMultiCallableAddrOfCode(); | 
|---|
| 93 | } | 
|---|
| 94 | else | 
|---|
| 95 | if (pMD->HasStableEntryPoint()) | 
|---|
| 96 | { | 
|---|
| 97 | // Set target | 
|---|
| 98 | target = pMD->GetStableEntryPoint(); | 
|---|
| 99 | } | 
|---|
| 100 | else | 
|---|
| 101 | { | 
|---|
| 102 | // Set the target if method is methodimpled. We would not get to patch it otherwise. | 
|---|
| 103 | MethodDesc* pMDImpl = MethodTable::MapMethodDeclToMethodImpl(pMD); | 
|---|
| 104 |  | 
|---|
| 105 | if (pMDImpl != pMD) | 
|---|
| 106 | target = pMDImpl->GetMultiCallableAddrOfCode(); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | // | 
|---|
| 110 | // We currently do not have a precode for this MethodDesc, so we will allocate one. | 
|---|
| 111 | // We allocate outside of the lock and then take the lock (m_hashTableCrst) and | 
|---|
| 112 | // if we still do not have a precode we Add the one that we just allocated and | 
|---|
| 113 | // call SuppressRelease to keep our allocation | 
|---|
| 114 | // If another thread beat us in adding the precode we don't call SuppressRelease | 
|---|
| 115 | // so the AllocMemTracker destructor will free the memory that we allocated | 
|---|
| 116 | // | 
|---|
| 117 | { | 
|---|
| 118 | AllocMemTracker amt; | 
|---|
| 119 | Precode* pNewPrecode = Precode::Allocate(type, pMD, pMD->GetLoaderAllocatorForCode(), &amt); | 
|---|
| 120 |  | 
|---|
| 121 | if (target != NULL) | 
|---|
| 122 | { | 
|---|
| 123 | pNewPrecode->SetTargetInterlocked(target); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | { | 
|---|
| 127 | CrstHolder ch(&m_hashTableCrst); | 
|---|
| 128 |  | 
|---|
| 129 | // Was an entry added in the meantime? | 
|---|
| 130 | // Is the entry still NULL? | 
|---|
| 131 | pPrecode = m_hashTable.Lookup(PrecodeKey(pMD, type)); | 
|---|
| 132 |  | 
|---|
| 133 | if (pPrecode == NULL) | 
|---|
| 134 | { | 
|---|
| 135 | // Use the one we allocated above | 
|---|
| 136 | pPrecode = pNewPrecode; | 
|---|
| 137 | m_hashTable.Add(pPrecode); | 
|---|
| 138 | amt.SuppressRelease(); | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | return pPrecode->GetEntryPoint(); | 
|---|
| 144 | } | 
|---|
| 145 | #endif // DACCESS_COMPILE | 
|---|
| 146 |  | 
|---|