summaryrefslogtreecommitdiff
path: root/src/debug/daccess/fntableaccess.cpp
blob: 6dcd5844e44e6d1cc950eb0fd25f0e6c1a2ba381 (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
// 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.
// ===========================================================================

// 
// File: DebugSupport.cpp
//
// Support routines for debugging the CLR
// ===========================================================================

#include "stdafx.h"

#ifndef FEATURE_PAL
#ifndef _TARGET_X86_

//
//
// @TODO: This is old code that should be easy to implement on top of the existing DAC support.
//        This code was originally written prior to DAC.
//
//

#include <winwrap.h>
#include <windows.h>
#include <winnt.h>
#include <clrnt.h>
#include <stddef.h> // offsetof
#include "nibblemapmacros.h"
#include "stdmacros.h"

#include "fntableaccess.h"

#define move(dst, src)  \
{ \
   if (!fpReadMemory(pUserContext, (LPCVOID)(src), &(dst), sizeof(dst), NULL))  \
   { \
      _ASSERTE(!"MSCORDBG ERROR: ReadProcessMemory failed!!"); \
      return STATUS_UNSUCCESSFUL; \
   } \
}

#define move_field(dst, src, cls, fld) \
    move(dst, (SIZE_T)(src) + FIELD_OFFSET(cls, fld))

static NTSTATUS OutOfProcessFindHeader(ReadMemoryFunction fpReadMemory,PVOID pUserContext, DWORD_PTR pMapIn, DWORD_PTR addr, DWORD_PTR &codeHead)
{
    codeHead = 0;
    
    DWORD       tmp;                              // must be a DWORD, not a DWORD_PTR
    DWORD_PTR   startPos  = ADDR2POS(addr);       // align to  128 byte buckets ( == index into the array of nibbles)
    DWORD_PTR   offset    = ADDR2OFFS(addr);      // this is the offset inside the bucket + 1
    DWORD *     pMap      = (DWORD *) pMapIn;     // make this a pointer type so our pointer math is correct w/o adding sizeof(DWORD) everywhere

    _ASSERTE(offset == (offset & NIBBLE_MASK));   // the offset must fit in a nibble

    pMap += (startPos >> LOG2_NIBBLES_PER_DWORD); // points to the proper DWORD of the map

    //
    // get DWORD and shift down our nibble
    //
    move(tmp, pMap);
    tmp = tmp >> POS2SHIFTCOUNT(startPos);

    // don't allow equality in the next check (tmp & NIBBLE_MASK == offset)
    // there are code blocks that terminate with a call instruction
    // (like call throwobject), i.e. their return address is
    // right behind the code block. If the memory manager allocates
    // heap blocks w/o gaps, we could find the next header in such
    // cases. Therefore we exclude the first DWORD of the header
    // from our search, but since we call this function for code
    // anyway (which starts at the end of the header) this is not
    // a problem.
    if ((tmp & NIBBLE_MASK) && ((tmp & NIBBLE_MASK) < offset) )
    {
        codeHead = POSOFF2ADDR(startPos, tmp & NIBBLE_MASK) - sizeof(CodeHeader);
        return STATUS_SUCCESS;
    }

    // is there a header in the remainder of the DWORD ?
    tmp = tmp >> NIBBLE_SIZE;

    if (tmp)
    {
        startPos--;
        while (!(tmp & NIBBLE_MASK))
        {
            tmp = tmp >> NIBBLE_SIZE;
            startPos--;
        }

        codeHead = POSOFF2ADDR(startPos, tmp & NIBBLE_MASK) - sizeof(CodeHeader);
        return STATUS_SUCCESS;
    }

    // we skipped the remainder of the DWORD,
    // so we must set startPos to the highest position of
    // previous DWORD

    startPos = ((startPos >> LOG2_NIBBLES_PER_DWORD) << LOG2_NIBBLES_PER_DWORD) - 1;

    if ((INT_PTR)startPos < 0)
    {
        return STATUS_SUCCESS;
    }

    // skip "headerless" DWORDS

    pMap--;
    move(tmp, pMap);
    while (!tmp)
    {
        startPos -= NIBBLES_PER_DWORD;
        if ((INT_PTR)startPos < 0)
        {
            return STATUS_SUCCESS;
        }
        pMap--;
        move (tmp, pMap);
    }


    while (!(tmp & NIBBLE_MASK))
    {
        tmp = tmp >> NIBBLE_SIZE;
        startPos--;
    }

    codeHead = POSOFF2ADDR(startPos, tmp & NIBBLE_MASK) - sizeof(CodeHeader);
    return STATUS_SUCCESS;
}

#define CODE_HEADER FakeRealCodeHeader
#define ResolveCodeHeader(pHeader)                          \
    if (pHeader)                                            \
    {                                                       \
        DWORD_PTR tmp = pHeader;                            \
        tmp += offsetof (FakeCodeHeader, pRealCodeHeader);  \
        move (tmp, tmp);                                    \
        pHeader = tmp;                                      \
    }

static NTSTATUS OutOfProcessFunctionTableCallback_JIT(IN  ReadMemoryFunction    fpReadMemory,
                                                      IN  PVOID                 pUserContext,
                                                      IN  PVOID                 TableAddress,
                                                      OUT PULONG                pnEntries,
                                                      OUT PT_RUNTIME_FUNCTION*    ppFunctions)
{
    if (NULL == pnEntries)      { return STATUS_INVALID_PARAMETER_3; }
    if (NULL == ppFunctions)    { return STATUS_INVALID_PARAMETER_4; }

    DYNAMIC_FUNCTION_TABLE * pTable = (DYNAMIC_FUNCTION_TABLE *) TableAddress;

    PVOID pvContext;
    move(pvContext, &pTable->Context);

    DWORD_PTR  JitMan      = (((DWORD_PTR)pvContext) & ~3);

    DWORD_PTR  MinAddress  = (DWORD_PTR) &(pTable->MinimumAddress);
    move(MinAddress, MinAddress);

    *ppFunctions = 0;
    *pnEntries   = 0;

    DWORD_PTR  pHp = JitMan + (DWORD_PTR)offsetof(FakeEEJitManager, m_pCodeHeap);

    move(pHp, pHp);

    while (pHp)
    {
        FakeHeapList Hp;

        move(Hp, pHp);

        if (pHp == MinAddress)
        {
            DWORD_PTR          pThisHeader;
            DWORD_PTR          hdrOffset;
            DWORD_PTR          hdrOffsetInitial;
            DWORD              nEntries;
            DWORD              index;
            DWORD_PTR          pUnwindInfo;
            PT_RUNTIME_FUNCTION  pFunctions;
            LONG64             lSmallestOffset;

            //
            // walk the header map and count functions with unwind info
            //
            nEntries  = 0;
            hdrOffset = Hp.endAddress - Hp.mapBase;
            lSmallestOffset = (LONG64)(Hp.startAddress - Hp.mapBase);

            // Save the initial offset at which we start our enumeration (from the end to the beginning).
            // The target process could be running when this function is called.  New methods could be 
            // added after we have started our enumeration, but their code headers would be added after
            // this initial offset.  Methods could also be deleted, but the memory would still be there.
            // It just wouldn't be marked as the beginning of a method, and we would collect fewer entries 
            // than we have anticipated.
            hdrOffsetInitial = hdrOffset;

            _ASSERTE(((LONG64)hdrOffset) >= lSmallestOffset);
            OutOfProcessFindHeader(fpReadMemory, pUserContext, Hp.pHdrMap, hdrOffset, hdrOffset);

            while (((LONG64)hdrOffset) >= lSmallestOffset)  // MUST BE A SIGNED COMPARISON
            {
                pThisHeader = Hp.mapBase + hdrOffset;
                ResolveCodeHeader(pThisHeader);

                if (pThisHeader > FAKE_STUB_CODE_BLOCK_LAST)
                {
                    DWORD nUnwindInfos;
                    move_field(nUnwindInfos, pThisHeader, CODE_HEADER, nUnwindInfos);

                    nEntries += nUnwindInfos;
                }

                _ASSERTE(((LONG64)hdrOffset) >= lSmallestOffset);
                OutOfProcessFindHeader(fpReadMemory, pUserContext, Hp.pHdrMap, hdrOffset, hdrOffset);
            }

            pFunctions   = (PT_RUNTIME_FUNCTION)ClrHeapAlloc(ClrGetProcessHeap(), HEAP_ZERO_MEMORY, S_SIZE_T(nEntries) * S_SIZE_T(sizeof(T_RUNTIME_FUNCTION)));
            *ppFunctions = pFunctions;
            *pnEntries   = nEntries;

            //
            // walk the header map and copy the function tables
            //

            index     = 0;
            hdrOffset = hdrOffsetInitial;

            _ASSERTE(((LONG64)hdrOffset) >= lSmallestOffset);
            OutOfProcessFindHeader(fpReadMemory, pUserContext, Hp.pHdrMap, hdrOffset, hdrOffset);

            while (((LONG64)hdrOffset) >= lSmallestOffset)  // MUST BE A SIGNED COMPARISON
            {
                pThisHeader = Hp.mapBase + hdrOffset;
                ResolveCodeHeader(pThisHeader);

                if (pThisHeader > FAKE_STUB_CODE_BLOCK_LAST)
                {
                    DWORD nUnwindInfos;
                    move_field(nUnwindInfos, pThisHeader, CODE_HEADER, nUnwindInfos);

                    if ((index + nUnwindInfos) > nEntries)
                    {
                        break;
                    }
                    for (DWORD iUnwindInfo = 0; iUnwindInfo < nUnwindInfos; iUnwindInfo++)
                    {
                        move(pFunctions[index], pThisHeader + offsetof(CODE_HEADER, unwindInfos[iUnwindInfo]));
                        index++;
                    }
                }

                _ASSERTE(((LONG64)hdrOffset) >= lSmallestOffset);
                OutOfProcessFindHeader(fpReadMemory, pUserContext, Hp.pHdrMap, hdrOffset, hdrOffset);
            }

            // Return the final count.
            *pnEntries = index;
            break;
        }

        pHp = (DWORD_PTR)Hp.hpNext;
    }

    return STATUS_SUCCESS;
}


#ifdef DEBUGSUPPORT_STUBS_HAVE_UNWIND_INFO

static NTSTATUS OutOfProcessFunctionTableCallback_Stub(IN  ReadMemoryFunction    fpReadMemory,
                                                       IN  PVOID                 pUserContext,
                                                       IN  PVOID                 TableAddress,
                                                       OUT PULONG                pnEntries,
                                                       OUT PT_RUNTIME_FUNCTION*    ppFunctions)
{
    if (NULL == pnEntries)      { return STATUS_INVALID_PARAMETER_3; }
    if (NULL == ppFunctions)    { return STATUS_INVALID_PARAMETER_4; }

    *ppFunctions = 0;
    *pnEntries   = 0;

    PVOID pvContext;
    move_field(pvContext, TableAddress, DYNAMIC_FUNCTION_TABLE, Context);

    SIZE_T pStubHeapSegment = ((SIZE_T)pvContext & ~3);

    FakeStubUnwindInfoHeapSegment stubHeapSegment;
    move(stubHeapSegment, pStubHeapSegment);

    UINT nEntries = 0;
    UINT nEntriesAllocated = 0;
    PT_RUNTIME_FUNCTION rgFunctions = NULL;

    for (int pass = 1; pass <= 2; pass++)
    {
        // Use the same initial header for both passes.  The process may still be running,
        // and so new entries could be added at the beginning of the list.  Using the initial header
        // makes sure new entries are not picked up in the second pass.  Entries could also be deleted,
        // and there is a small time window here where we could read invalid memory.  This just means
        // that ReadProcessMemory() may fail.  As long as we don't crash the host process (e.g. WER)
        // we are fine.
        SIZE_T pHeader = (SIZE_T)stubHeapSegment.pUnwindHeaderList;

        while (pHeader)
        {
            FakeStubUnwindInfoHeader unwindInfoHeader;
            move(unwindInfoHeader, pHeader);
#if defined(_TARGET_AMD64_) 
            // Consistency checks to detect corrupted process state
            if (unwindInfoHeader.FunctionEntry.BeginAddress > unwindInfoHeader.FunctionEntry.EndAddress ||
                unwindInfoHeader.FunctionEntry.EndAddress > stubHeapSegment.cbSegment)
            {
                _ASSERTE(1 == pass);
                return STATUS_UNSUCCESSFUL;
            }

            if ((SIZE_T)stubHeapSegment.pbBaseAddress + unwindInfoHeader.FunctionEntry.UnwindData != 
                    pHeader + FIELD_OFFSET(FakeStubUnwindInfoHeader, UnwindInfo))
            {
                _ASSERTE(1 == pass);
                return STATUS_UNSUCCESSFUL;
            }
#elif defined(_TARGET_ARM_)
            
            // Skip checking the corrupted process stateon ARM

#elif defined(_TARGET_ARM64_)
            // Compute the function length
            ULONG64 functionLength = 0;
            ULONG64 unwindData = unwindInfoHeader.FunctionEntry.UnwindData;
            if (( unwindData & 3) != 0) {
                // the unwindData contains the function length, retrieve it directly from unwindData
                functionLength = (unwindInfoHeader.FunctionEntry.UnwindData >> 2) & 0x7ff;
            } else {
                // the unwindData is an RVA to the .xdata record which contains the function length
                DWORD xdataHeader=0;
                if ((SIZE_T)stubHeapSegment.pbBaseAddress + unwindData != pHeader + FIELD_OFFSET(FakeStubUnwindInfoHeader, UnwindInfo))
                {
                    _ASSERTE(1 == pass);
                    return STATUS_UNSUCCESSFUL;
                }
                move(xdataHeader, stubHeapSegment.pbBaseAddress + unwindData);
                functionLength = (xdataHeader & 0x3ffff) << 2;
            }
            if (unwindInfoHeader.FunctionEntry.BeginAddress + functionLength > stubHeapSegment.cbSegment)
            {
                _ASSERTE(1 == pass);
                return STATUS_UNSUCCESSFUL;
            }
#else
            PORTABILITY_ASSERT("OutOfProcessFunctionTableCallback_Stub");
#endif 
            if (nEntriesAllocated)
            {
                if (nEntries >= nEntriesAllocated)
                    break;
                rgFunctions[nEntries] = unwindInfoHeader.FunctionEntry;
            }
            nEntries++;

            pHeader = (SIZE_T)unwindInfoHeader.pNext;
        }

        if (1 == pass)
        {
            if (!nEntries)
                break;

            _ASSERTE(!nEntriesAllocated);
            nEntriesAllocated = nEntries;
            rgFunctions = (PT_RUNTIME_FUNCTION)ClrHeapAlloc(ClrGetProcessHeap(), HEAP_ZERO_MEMORY, S_SIZE_T(nEntries) * S_SIZE_T(sizeof(T_RUNTIME_FUNCTION)));
            nEntries = 0;
        }
        else
        {
            _ASSERTE(nEntriesAllocated >= nEntries);
        }
    }

    *ppFunctions = rgFunctions;
    *pnEntries   = nEntries;        // return the final count

    return STATUS_SUCCESS;
}

#endif // DEBUGSUPPORT_STUBS_HAVE_UNWIND_INFO


BOOL ReadMemory(PVOID pUserContext, LPCVOID lpBaseAddress, PVOID lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesRead)
{
    HANDLE hProcess = (HANDLE)pUserContext;
    return ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead);
}

extern "C" NTSTATUS OutOfProcessFunctionTableCallback(IN  HANDLE                hProcess,
                                                      IN  PVOID                 TableAddress,
                                                      OUT PULONG                pnEntries,
                                                      OUT PT_RUNTIME_FUNCTION*    ppFunctions)
{
    return OutOfProcessFunctionTableCallbackEx(&ReadMemory, hProcess, TableAddress, pnEntries, ppFunctions);
}

extern "C" NTSTATUS OutOfProcessFunctionTableCallbackEx(IN  ReadMemoryFunction    fpReadMemory,
                                                        IN  PVOID				  pUserContext,
                                                        IN  PVOID                 TableAddress,
                                                        OUT PULONG                pnEntries,
                                                        OUT PT_RUNTIME_FUNCTION*    ppFunctions)
{
    if (NULL == pnEntries)      { return STATUS_INVALID_PARAMETER_3; }
    if (NULL == ppFunctions)    { return STATUS_INVALID_PARAMETER_4; }

    DYNAMIC_FUNCTION_TABLE * pTable = (DYNAMIC_FUNCTION_TABLE *) TableAddress;
    PVOID pvContext;

    move(pvContext, &pTable->Context);

    FakeEEDynamicFunctionTableType type = (FakeEEDynamicFunctionTableType)((SIZE_T)pvContext & 3);

    switch (type)
    {
    case FAKEDYNFNTABLE_JIT:
        return OutOfProcessFunctionTableCallback_JIT(
                fpReadMemory,
                pUserContext,
                TableAddress,
                pnEntries,
                ppFunctions);

#ifdef DEBUGSUPPORT_STUBS_HAVE_UNWIND_INFO
    case FAKEDYNFNTABLE_STUB:
        return OutOfProcessFunctionTableCallback_Stub(
                fpReadMemory,
                pUserContext,
                TableAddress,
                pnEntries,
                ppFunctions);
#endif // DEBUGSUPPORT_STUBS_HAVE_UNWIND_INFO
    default:
        break;
    }

    return STATUS_UNSUCCESSFUL;
}

#else

extern "C" NTSTATUS OutOfProcessFunctionTableCallback()
{
    return STATUS_UNSUCCESSFUL;
}

extern "C" NTSTATUS OutOfProcessFunctionTableCallbackEx()
{
    return STATUS_UNSUCCESSFUL;
}

#endif // !_TARGET_X86_
#endif // !FEATURE_PAL