-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
dbgappdomain.h
379 lines (311 loc) · 13.2 KB
/
dbgappdomain.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef DbgAppDomain_H
#define DbgAppDomain_H
// Forward declaration
class AppDomain;
// AppDomainInfo contains information about an AppDomain
// All pointers are for the left side, and we do not own any of the memory
struct AppDomainInfo
{
DWORD m_id = 0; // UNUSED, only present to maintain the shape of this structure
int m_iNameLengthInBytes;
LPCWSTR m_szAppDomainName;
AppDomain *m_pAppDomain; // only used by LS
// NOTE: These functions are just helpers and must not add a VTable
// to this struct (since we need to read this out-of-proc)
// Provide a clean definition of an empty entry
inline bool IsEmpty() const
{
return m_szAppDomainName == NULL;
}
#ifndef RIGHT_SIDE_COMPILE
// Mark this entry as empty.
inline void FreeEntry()
{
m_szAppDomainName = NULL;
}
// Set the string name and length.
// If szName is null, it is adjusted to a global constant.
// This also causes the entry to be considered valid
inline void SetName(LPCWSTR szName)
{
if (szName != NULL)
m_szAppDomainName = szName;
else
m_szAppDomainName = W("<NoName>");
m_iNameLengthInBytes = (int) (u16_strlen(m_szAppDomainName) + 1) * sizeof(WCHAR);
}
#endif
};
// Enforce the AppDomain IPC block binary layout doesn't change between versions.
// Only an issue for x86 since that's the only platform w/ multiple versions.
#if defined(TARGET_X86)
static_assert_no_msg(offsetof(AppDomainInfo, m_id) == 0x0);
static_assert_no_msg(offsetof(AppDomainInfo, m_iNameLengthInBytes) == 0x4);
static_assert_no_msg(offsetof(AppDomainInfo, m_szAppDomainName) == 0x8);
static_assert_no_msg(offsetof(AppDomainInfo, m_pAppDomain) == 0xc);
#endif
// The RemoteHANDLE encapsulates the PAL specific handling of handles to avoid PAL specific ifdefs
// everywhere else in the code.
// There are two common initialization patterns:
//
// 1. Publishing of local handle for other processes, the value of the wrapper is a local handle
// in *this* process at the end:
// - In this process, call SetLocal(hHandle) to initialize the handle.
// - In the other processes, call DuplicateToLocalProcess to get a local copy of the handle.
//
// 2. Injecting of local handle into other process, the value of the wrapper is a local handle
// in the *other* process at the end:
// - In this process, call DuplicateToRemoteProcess(hProcess, hHandle) to initialize the handle.
// - In the other process, call ImportToOtherProcess() to finish the initialization of the wrapper
// with a local copy of the handle.
//
// Once initialized, the wrapper can be used the same way as a regular HANDLE in the process
// it was initialized for. There is casting operator HANDLE to achieve that.
struct RemoteHANDLE {
HANDLE m_hLocal;
operator HANDLE& ()
{
return m_hLocal;
}
void Close()
{
HANDLE hHandle = m_hLocal;
if (hHandle != NULL) {
m_hLocal = NULL;
CloseHandle(hHandle);
}
}
// Sets the local value of the handle. DuplicateToLocalProcess can be used later
// by the remote process to acquire the remote handle.
BOOL SetLocal(HANDLE hHandle)
{
m_hLocal = hHandle;
return TRUE;
}
// Duplicates the current handle value to remote process. ImportToLocalProcess
// should be called in the remote process before the handle is used in the remote process.
// NOTE: right now this is used for duplicating the debugger's process handle into the LS so
// that the LS can know when the RS has exited; thus we are only specifying SYNCHRONIZE
// access to mitigate any security concerns.
BOOL DuplicateToRemoteProcess(HANDLE hProcess, HANDLE hHandle)
{
return DuplicateHandle(GetCurrentProcess(), hHandle, hProcess, &m_hLocal,
SYNCHRONIZE, FALSE, 0);
}
// Duplicates the current handle value to local process. To be used in combination with SetLocal.
BOOL DuplicateToLocalProcess(HANDLE hProcess, HANDLE* pHandle)
{
return DuplicateHandle(hProcess, m_hLocal, GetCurrentProcess(), pHandle,
NULL, FALSE, DUPLICATE_SAME_ACCESS);
}
void CloseInRemoteProcess(HANDLE hProcess)
{
HANDLE hHandle = m_hLocal;
m_hLocal = NULL;
HANDLE hTmp;
if (DuplicateHandle(hProcess, hHandle, GetCurrentProcess(), &hTmp,
NULL, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
{
CloseHandle(hTmp);
}
}
// Imports the handle to local process. To be used in combination with DuplicateToRemoteProcess.
HANDLE ImportToLocalProcess()
{
return m_hLocal;
}
};
// AppDomain publishing server support:
// Information about all appdomains in the process will be maintained
// in the shared memory block for use by the debugger, etc.
// This structure defines the layout of the information that will
// be maintained.
struct AppDomainEnumerationIPCBlock
{
// !!! The binary format of this layout must remain the same across versions so that
// !!! a V2.0 publisher can inspect a v1.0 app.
// lock for serialization while manipulating AppDomain list.
RemoteHANDLE m_hMutex;
// Number of slots in AppDomainListElement array
int m_iTotalSlots;
int m_iNumOfUsedSlots;
int m_iLastFreedSlot;
int m_iSizeInBytes; // Size of AppDomainInfo in bytes
// We can use psapi!GetModuleFileNameEx to get the module name.
// This provides an alternative.
int m_iProcessNameLengthInBytes;
WCHAR *m_szProcessName;
AppDomainInfo *m_rgListOfAppDomains;
BOOL m_fLockInvalid;
#ifndef RIGHT_SIDE_COMPILE
/*************************************************************************
* Locks the list
*************************************************************************/
BOOL Lock()
{
DWORD dwRes = WaitForSingleObject(m_hMutex, 3000);
if (dwRes == WAIT_TIMEOUT)
{
// Nobody should get stuck holding this lock.
// If we timeout on the wait, then either:
// - it's a really bad race and somebody got preempted for a long time
// - perhaps somebody's doing a DOS attack and holding onto the mutex.
m_fLockInvalid = TRUE;
}
// The only time this can happen is if we're in shutdown and a thread
// that held this lock is killed. If this happens, assume that this
// IPC block is in an invalid state and return FALSE to indicate
// that people shouldn't do anything with the block anymore.
if (dwRes == WAIT_ABANDONED)
{
m_fLockInvalid = TRUE;
}
if (m_fLockInvalid)
{
Unlock();
}
return (dwRes == WAIT_OBJECT_0 && !m_fLockInvalid);
}
/*************************************************************************
* Unlocks the list
*************************************************************************/
void Unlock()
{
// Lock may or may not be valid at this point. Thus Release may fail,
// but we'll just ignore that.
ReleaseMutex(m_hMutex);
}
/*************************************************************************
* Gets a free AppDomainInfo entry, and will allocate room if there are
* no free slots left.
*************************************************************************/
AppDomainInfo *GetFreeEntry()
{
// first check to see if there is space available. If not, then realloc.
if (m_iNumOfUsedSlots == m_iTotalSlots)
{
// need to realloc
AppDomainInfo *pTemp =
new (nothrow) AppDomainInfo [m_iTotalSlots*2];
if (pTemp == NULL)
{
return (NULL);
}
memcpy (pTemp, m_rgListOfAppDomains, m_iSizeInBytes);
delete [] m_rgListOfAppDomains;
// Initialize the increased portion of the realloced memory
int iNewSlotSize = m_iTotalSlots * 2;
for (int iIndex = m_iTotalSlots; iIndex < iNewSlotSize; iIndex++)
pTemp[iIndex].FreeEntry();
m_rgListOfAppDomains = pTemp;
m_iTotalSlots = iNewSlotSize;
m_iSizeInBytes *= 2;
}
// Walk the list looking for an empty slot. Start from the last
// one which was freed.
{
int i = m_iLastFreedSlot;
do
{
// Pointer to the entry being examined
AppDomainInfo *pADInfo = &(m_rgListOfAppDomains[i]);
// is the slot available?
if (pADInfo->IsEmpty())
return (pADInfo);
i = (i + 1) % m_iTotalSlots;
} while (i != m_iLastFreedSlot);
}
_ASSERTE(!"ADInfo::GetFreeEntry: should never get here.");
return (NULL);
}
/*************************************************************************
* Returns an AppDomainInfo slot to the free list.
*************************************************************************/
void FreeEntry(AppDomainInfo *pADInfo)
{
_ASSERTE(pADInfo >= m_rgListOfAppDomains &&
pADInfo < m_rgListOfAppDomains + m_iSizeInBytes);
_ASSERTE(((size_t)pADInfo - (size_t)m_rgListOfAppDomains) %
sizeof(AppDomainInfo) == 0);
// Mark this slot as free
pADInfo->FreeEntry();
#ifdef _DEBUG
*pADInfo = {};
#endif
// decrement the used slot count
m_iNumOfUsedSlots--;
// Save the last freed slot.
m_iLastFreedSlot = (int)((size_t)pADInfo - (size_t)m_rgListOfAppDomains) /
sizeof(AppDomainInfo);
}
/*************************************************************************
* Finds an AppDomainInfo entry corresponding to the AppDomain pointer.
* Returns NULL if no such entry exists.
*************************************************************************/
AppDomainInfo *FindEntry(AppDomain *pAD)
{
// Walk the list looking for a matching entry
for (int i = 0; i < m_iTotalSlots; i++)
{
AppDomainInfo *pADInfo = &(m_rgListOfAppDomains[i]);
if (!pADInfo->IsEmpty() &&
pADInfo->m_pAppDomain == pAD)
return pADInfo;
}
return (NULL);
}
/*************************************************************************
* Returns the first AppDomainInfo entry in the list. Returns NULL if
* no such entry exists.
*************************************************************************/
AppDomainInfo *FindFirst()
{
// Walk the list looking for a non-empty entry
for (int i = 0; i < m_iTotalSlots; i++)
{
AppDomainInfo *pADInfo = &(m_rgListOfAppDomains[i]);
if (!pADInfo->IsEmpty())
return pADInfo;
}
return (NULL);
}
/*************************************************************************
* Returns the next AppDomainInfo entry after pADInfo. Returns NULL if
* pADInfo was the last in the list.
*************************************************************************/
AppDomainInfo *FindNext(AppDomainInfo *pADInfo)
{
_ASSERTE(pADInfo >= m_rgListOfAppDomains &&
pADInfo < m_rgListOfAppDomains + m_iSizeInBytes);
_ASSERTE(((size_t)pADInfo - (size_t)m_rgListOfAppDomains) %
sizeof(AppDomainInfo) == 0);
// Walk the list looking for the next non-empty entry
for (int i = (int)((size_t)pADInfo - (size_t)m_rgListOfAppDomains)
/ sizeof(AppDomainInfo) + 1;
i < m_iTotalSlots;
i++)
{
AppDomainInfo *pADInfoTemp = &(m_rgListOfAppDomains[i]);
if (!pADInfoTemp->IsEmpty())
return pADInfoTemp;
}
return (NULL);
}
#endif // RIGHT_SIDE_COMPILE
};
// Enforce the AppDomain IPC block binary layout doesn't change between versions.
// Only an issue for x86 since that's the only platform w/ multiple versions.
#if defined(TARGET_X86)
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_hMutex) == 0x0);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_iTotalSlots) == 0x4);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_iNumOfUsedSlots) == 0x8);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_iLastFreedSlot) == 0xc);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_iSizeInBytes) == 0x10);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_iProcessNameLengthInBytes) == 0x14);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_szProcessName) == 0x18);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_rgListOfAppDomains) == 0x1c);
static_assert_no_msg(offsetof(AppDomainEnumerationIPCBlock, m_fLockInvalid) == 0x20);
#endif
#endif //DbgAppDomain_H