summaryrefslogtreecommitdiff
path: root/src/utilcode/lazycow.cpp
blob: f4ea267dc75d73fb7d19701e434cd60a77ec709c (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
// 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.
// --------------------------------------------------------------------------------
// LazyCOW.cpp
//

#include "stdafx.h"

#include "pedecoder.h"
#include "volatile.h"
#include "lazycow.h"

#ifdef FEATURE_LAZY_COW_PAGES

// 
// We can't use the hosted ClrVirtualProtect, because EnsureWritablePages is called in places where we can't
// call into the host.
//
#ifdef VirtualProtect
#undef VirtualProtect
#endif

#ifdef VirtualAlloc
#undef VirtualAlloc
#endif

#ifdef VirtualFree
#undef VirtualFree
#endif

LONG* g_pCOWPageMap;    // one bit for each page in the virtual address space
LONG  g_COWPageMapMap;  // one bit for each page in g_pCOWPageMap.

LONG* EnsureCOWPageMapAllocated()
{
    if (g_pCOWPageMap == NULL)
    {
        // We store one bit for every page in the virtual address space.  We may need to revisit this for 64-bit. :)
        MEMORYSTATUSEX stats;
        stats.dwLength = sizeof(stats);
        if (GlobalMemoryStatusEx(&stats))
        {
            _ASSERTE(stats.ullTotalVirtual < 0x100000000ULL);

            SIZE_T mapSize = (SIZE_T)((stats.ullTotalVirtual / GetOsPageSize()) / 8);
            _ASSERTE(mapSize / GetOsPageSize() <= 32); // g_COWPageMapMap can only track 32 pages

            // Note that VirtualAlloc will zero-fill the pages for us.
            LONG* pMap = (LONG*)VirtualAlloc(
                NULL,
                mapSize,
                MEM_RESERVE,
                PAGE_READWRITE);

            if (pMap != NULL)
            {
                if (NULL != InterlockedCompareExchangeT(&g_pCOWPageMap, pMap, NULL))
                    VirtualFree(pMap, 0, MEM_RELEASE);
            }
        }
    }
    return g_pCOWPageMap;
}

bool EnsureCOWPageMapElementAllocated(LONG* elem)
{
    _ASSERTE(elem > g_pCOWPageMap);
    _ASSERTE(g_pCOWPageMap != NULL);

    size_t offset = (size_t)elem - (size_t)g_pCOWPageMap;
    size_t page = offset / GetOsPageSize();
    
    _ASSERTE(page < 32);
    int bit = (int)(1 << page);
    
    if (!(g_COWPageMapMap & bit))
    {
        if (!VirtualAlloc(elem, 1, MEM_COMMIT, PAGE_READWRITE))
            return false;

        InterlockedOr(&g_COWPageMapMap, bit);
    }

    return true;
}

bool IsCOWPageMapElementAllocated(LONG* elem)
{
    _ASSERTE(elem >= g_pCOWPageMap);
    _ASSERTE(g_pCOWPageMap != NULL);

    size_t offset = (size_t)elem - (size_t)g_pCOWPageMap;
    size_t page = offset / GetOsPageSize();
    
    _ASSERTE(page < 32);
    int bit = (int)(1 << page);
    
    return (g_COWPageMapMap & bit) != 0;
}

bool SetCOWPageBits(BYTE* pStart, size_t len, bool value)
{
    _ASSERTE(len > 0);

    // we don't need a barrier here, since:
    //  a) all supported hardware maintains ordering of dependent reads
    //  b) it's ok if additional reads happen, because this never changes
    //     once initialized.
    LONG* pCOWPageMap = g_pCOWPageMap;

    //
    // Write the bits in 32-bit chunks, to avoid doing one interlocked instruction for each bit.
    //
    size_t page = (size_t)pStart / GetOsPageSize();
    size_t lastPage = (size_t)(pStart+len-1) / GetOsPageSize();
    size_t elem = page / 32;
    LONG bits = 0;
    do
    {
        bits |= 1 << (page % 32);

        ++page;

        //
        // if we've moved to a new element of the map, or we've covered every page,
        // we need to write out the already-accumulated element.
        //
        size_t newElem = page / 32;
        if (page > lastPage || newElem != elem)
        {
            LONG* pElem = &pCOWPageMap[elem];
            if (!EnsureCOWPageMapElementAllocated(pElem))
                return false;

            if (value)
                InterlockedOr(&pCOWPageMap[elem], bits);
            else
                InterlockedAnd(&pCOWPageMap[elem], ~bits);

            elem = newElem;
            bits = 0;
        }
    }
    while (page <= lastPage);

    return true;
}

bool SetCOWPageBitsForImage(PEDecoder * pImage, bool value)
{
    if (!pImage->HasNativeHeader())
        return true;

    bool success = true;

    IMAGE_SECTION_HEADER* pSection;
    BYTE * pStart;
    size_t len;

    pSection = pImage->FindSection(".data");
    if (pSection != NULL)
    {
        pStart = (BYTE*) dac_cast<TADDR>(pImage->GetBase()) + pSection->VirtualAddress;
        len = pSection->Misc.VirtualSize;

        if (!SetCOWPageBits(pStart, len, value))
            success = false;
    }

    pSection = pImage->FindSection(".xdata");
    if (pSection != NULL)
    {
        pStart = (BYTE*) dac_cast<TADDR>(pImage->GetBase()) + pSection->VirtualAddress;
        len = pSection->Misc.VirtualSize;

        if (!SetCOWPageBits(pStart, len, value))
            success = false;
    }

    return success;
}

bool AreAnyCOWPageBitsSet(BYTE* pStart, size_t len)
{
    LONG* pCOWPageMap = g_pCOWPageMap;
    if (pCOWPageMap == NULL)
        return false;

    _ASSERTE(len > 0);
    size_t page = (size_t)pStart / GetOsPageSize();
    size_t lastPage = (size_t)(pStart+len-1) / GetOsPageSize();
    do
    {
        LONG* pElem = &pCOWPageMap[page / 32];
        if (IsCOWPageMapElementAllocated(pElem) && 
            (*pElem & (1 << (page %32))))
        {
            return true;
        }
        ++page;
    }
    while (page <= lastPage);

    return false;
}


void AllocateLazyCOWPages(PEDecoder * pImage)
{
    //
    // Note: it's ok to call AllocateLazyCOWPages multiple times for the same loaded image.
    // This will result in any already-writable pages being incorrectly marked as read-only
    // in our records, but that just means we'll call VirtualProtect one more time.
    //
    // However, FreeLazyCOWPages must be called just once per loaded image, when it is actually.  
    // unloaded.  Otherwise we will lose track of the COW pages in that image while it might 
    // still be accessible.
    //

    if (!EnsureCOWPageMapAllocated())
        ThrowOutOfMemory();

    if (!SetCOWPageBitsForImage(pImage, true))
        ThrowOutOfMemory();
}

void FreeLazyCOWPages(PEDecoder * pImage)
{
    //
    // Must be called just once per image; see note in AllocateLazyCOWPages.
    //
    SetCOWPageBitsForImage(pImage, false);
}

bool IsInReadOnlyLazyCOWPage(void* p)
{
    return AreAnyCOWPageBitsSet((BYTE*)p, 1);
}


bool MakeWritable(BYTE* pStart, size_t len, DWORD protect)
{
    DWORD oldProtect;
    if (!VirtualProtect(pStart, len, protect, &oldProtect))
        return false;
    INDEBUG(bool result = ) SetCOWPageBits(pStart, len, false);
    _ASSERTE(result); // we already set these, so we must be able to clear them.
    return true;
}

bool EnsureWritablePagesNoThrow(void* p, size_t len)
{
    BYTE* pStart = (BYTE*)p;

    if (len == 0)
        return true;

    if (!AreAnyCOWPageBitsSet(pStart, len))
        return true;

    return MakeWritable(pStart, len, PAGE_READWRITE);
}

void EnsureWritablePages(void* p, size_t len)
{
    CONTRACTL {
        THROWS;
    } CONTRACTL_END;

    BYTE* pStart = (BYTE*)p;

    if (len == 0)
        return;

    if (!AreAnyCOWPageBitsSet(pStart, len))
        return;

    if (!MakeWritable(pStart, len, PAGE_READWRITE))
        ThrowOutOfMemory();
}

bool EnsureWritableExecutablePagesNoThrow(void* p, size_t len)
{
    BYTE* pStart = (BYTE*) p;

    if (len == 0)
        return true;

    if (!AreAnyCOWPageBitsSet(pStart, len))
        return true;

    return MakeWritable(pStart, len, PAGE_EXECUTE_READWRITE);
}

void EnsureWritableExecutablePages(void* p, size_t len)
{
    CONTRACTL {
        THROWS;
    } CONTRACTL_END;

    BYTE* pStart = (BYTE*) p;

    if (len == 0)
        return;

    if (!AreAnyCOWPageBitsSet(pStart, len))
        return;

    if (!MakeWritable(pStart, len, PAGE_EXECUTE_READWRITE))
        ThrowOutOfMemory();
}

#endif // FEATURE_LAZY_COW_PAGES