summaryrefslogtreecommitdiff
path: root/src/vm/methoddescbackpatchinfo.cpp
blob: 386786c6bab92ea9ed73601f7d800859a1641e47 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "common.h"

#include "excep.h"
#include "log.h"
#include "methoddescbackpatchinfo.h"

#ifdef CROSSGEN_COMPILE
    #error This file is not expected to be included into CrossGen
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EntryPointSlots

#ifndef DACCESS_COMPILE

void EntryPointSlots::Backpatch_Locked(PCODE entryPoint)
{
    WRAPPER_NO_CONTRACT;
    static_assert_no_msg(SlotType_Count <= sizeof(INT32));
    _ASSERTE(MethodDescBackpatchInfoTracker::IsLockedByCurrentThread());
    _ASSERTE(entryPoint != NULL);

    TADDR *slots = m_slots.GetElements();
    COUNT_T slotCount = m_slots.GetCount();
    for (COUNT_T i = 0; i < slotCount; ++i)
    {
        TADDR slot = slots[i];
        SlotType slotType = (SlotType)(slot & SlotType_Mask);
        slot ^= slotType;
        Backpatch_Locked(slot, slotType, entryPoint);
    }
}

void EntryPointSlots::Backpatch_Locked(TADDR slot, SlotType slotType, PCODE entryPoint)
{
    WRAPPER_NO_CONTRACT;
    static_assert_no_msg(SlotType_Count <= sizeof(INT32));
    _ASSERTE(MethodDescBackpatchInfoTracker::IsLockedByCurrentThread());
    _ASSERTE(slot != NULL);
    _ASSERTE(!(slot & SlotType_Mask));
    _ASSERTE(slotType >= SlotType_Normal);
    _ASSERTE(slotType < SlotType_Count);
    _ASSERTE(entryPoint != NULL);
    _ASSERTE(IS_ALIGNED((SIZE_T)slot, GetRequiredSlotAlignment(slotType)));

    switch (slotType)
    {
        case SlotType_Normal:
            *(PCODE *)slot = entryPoint;
            break;

        case SlotType_Vtable:
            ((MethodTable::VTableIndir2_t *)slot)->SetValue(entryPoint);
            break;

        case SlotType_Executable:
            *(PCODE *)slot = entryPoint;
            goto Flush;

        case SlotType_ExecutableRel32:
            // A rel32 may require a jump stub on some architectures, and is currently not supported
            _ASSERTE(sizeof(void *) <= 4);

            *(PCODE *)slot = entryPoint - ((PCODE)slot + sizeof(PCODE));
            // fall through

        Flush:
            ClrFlushInstructionCache((LPCVOID)slot, sizeof(PCODE));
            break;

        default:
            UNREACHABLE();
            break;
    }
}

#endif // !DACCESS_COMPILE

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MethodDescBackpatchInfo

#ifndef DACCESS_COMPILE

void MethodDescBackpatchInfo::AddDependentLoaderAllocator_Locked(LoaderAllocator *dependentLoaderAllocator)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(MethodDescBackpatchInfoTracker::IsLockedByCurrentThread());
    _ASSERTE(m_methodDesc != nullptr);
    _ASSERTE(dependentLoaderAllocator != nullptr);
    _ASSERTE(dependentLoaderAllocator != m_methodDesc->GetLoaderAllocator());

    LoaderAllocatorSet *set = m_dependentLoaderAllocators;
    if (set != nullptr)
    {
        if (set->Lookup(dependentLoaderAllocator) != nullptr)
        {
            return;
        }
        set->Add(dependentLoaderAllocator);
        return;
    }

    NewHolder<LoaderAllocatorSet> setHolder = new LoaderAllocatorSet();
    setHolder->Add(dependentLoaderAllocator);
    m_dependentLoaderAllocators = setHolder.Extract();
}

void MethodDescBackpatchInfo::RemoveDependentLoaderAllocator_Locked(LoaderAllocator *dependentLoaderAllocator)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(MethodDescBackpatchInfoTracker::IsLockedByCurrentThread());
    _ASSERTE(m_methodDesc != nullptr);
    _ASSERTE(dependentLoaderAllocator != nullptr);
    _ASSERTE(dependentLoaderAllocator != m_methodDesc->GetLoaderAllocator());
    _ASSERTE(m_dependentLoaderAllocators != nullptr);
    _ASSERTE(m_dependentLoaderAllocators->Lookup(dependentLoaderAllocator) == dependentLoaderAllocator);

    m_dependentLoaderAllocators->Remove(dependentLoaderAllocator);
}

#endif // !DACCESS_COMPILE

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MethodDescBackpatchInfoTracker

CrstStatic MethodDescBackpatchInfoTracker::s_lock;

#ifndef DACCESS_COMPILE

void MethodDescBackpatchInfoTracker::StaticInitialize()
{
    WRAPPER_NO_CONTRACT;
    s_lock.Init(CrstMethodDescBackpatchInfoTracker);
}

#endif // DACCESS_COMPILE

#ifdef _DEBUG

bool MethodDescBackpatchInfoTracker::IsLockedByCurrentThread()
{
    WRAPPER_NO_CONTRACT;

#ifndef DACCESS_COMPILE
    return !!s_lock.OwnedByCurrentThread();
#else
    return true;
#endif
}

bool MethodDescBackpatchInfoTracker::MayHaveEntryPointSlotsToBackpatch(PTR_MethodDesc methodDesc)
{
    // The only purpose of this method is to allow asserts in inline functions defined in the .h file, by which time MethodDesc
    // is not fully defined

    WRAPPER_NO_CONTRACT;
    return methodDesc->MayHaveEntryPointSlotsToBackpatch();
}

#endif // _DEBUG

#ifndef DACCESS_COMPILE

MethodDescBackpatchInfo *MethodDescBackpatchInfoTracker::AddBackpatchInfo_Locked(MethodDesc *methodDesc)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(IsLockedByCurrentThread());
    _ASSERTE(methodDesc != nullptr);
    _ASSERTE(methodDesc->MayHaveEntryPointSlotsToBackpatch());
    _ASSERTE(m_backpatchInfoHash.Lookup(methodDesc) == nullptr);

    NewHolder<MethodDescBackpatchInfo> backpatchInfoHolder = new MethodDescBackpatchInfo(methodDesc);
    m_backpatchInfoHash.Add(backpatchInfoHolder);
    return backpatchInfoHolder.Extract();
}

EntryPointSlots *MethodDescBackpatchInfoTracker::GetDependencyMethodDescEntryPointSlots_Locked(MethodDesc *methodDesc)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(IsLockedByCurrentThread());
    _ASSERTE(methodDesc != nullptr);
    _ASSERTE(methodDesc->MayHaveEntryPointSlotsToBackpatch());

    MethodDescEntryPointSlots *methodDescSlots =
        m_dependencyMethodDescEntryPointSlotsHash.Lookup(methodDesc);
    return methodDescSlots == nullptr ? nullptr : methodDescSlots->GetSlots();
}

EntryPointSlots *MethodDescBackpatchInfoTracker::GetOrAddDependencyMethodDescEntryPointSlots_Locked(MethodDesc *methodDesc)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(IsLockedByCurrentThread());
    _ASSERTE(methodDesc != nullptr);
    _ASSERTE(methodDesc->MayHaveEntryPointSlotsToBackpatch());

    MethodDescEntryPointSlots *methodDescSlots = m_dependencyMethodDescEntryPointSlotsHash.Lookup(methodDesc);
    if (methodDescSlots != nullptr)
    {
        return methodDescSlots->GetSlots();
    }

    NewHolder<MethodDescEntryPointSlots> methodDescSlotsHolder = new MethodDescEntryPointSlots(methodDesc);
    m_dependencyMethodDescEntryPointSlotsHash.Add(methodDescSlotsHolder);
    return methodDescSlotsHolder.Extract()->GetSlots();
}

void MethodDescBackpatchInfoTracker::ClearDependencyMethodDescEntryPointSlots(LoaderAllocator *loaderAllocator)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(loaderAllocator != nullptr);
    _ASSERTE(loaderAllocator->GetMethodDescBackpatchInfoTracker() == this);

    ConditionalLockHolder lockHolder;

    for (MethodDescEntryPointSlotsHash::Iterator
            it = m_dependencyMethodDescEntryPointSlotsHash.Begin(),
            itEnd = m_dependencyMethodDescEntryPointSlotsHash.End();
        it != itEnd;
        ++it)
    {
        MethodDesc *methodDesc = (*it)->GetMethodDesc();
        MethodDescBackpatchInfo *backpatchInfo = methodDesc->GetBackpatchInfoTracker()->GetBackpatchInfo_Locked(methodDesc);
        if (backpatchInfo != nullptr)
        {
            backpatchInfo->RemoveDependentLoaderAllocator_Locked(loaderAllocator);
        }
    }

    m_dependencyMethodDescEntryPointSlotsHash.RemoveAll();
}

#endif // DACCESS_COMPILE

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////