-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathsos.cpp
505 lines (409 loc) · 13.8 KB
/
sos.cpp
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "strike.h"
#include "util.h"
#include "sos.h"
#include "gcdesc.h"
namespace sos
{
template <class T>
static bool MemOverlap(T beg1, T end1, // first range
T beg2, T end2) // second range
{
if (beg1 >= end1 || beg2 >= end2) // one of the ranges is empty
return false;
if (beg2 >= beg1 && beg2 <= end1) // second range starts within first range
return true;
else if (end2 >= beg1 && end2 <= end1) // second range ends within first range
return true;
else if (beg1 >= beg2 && beg1 <= end2) // first range starts within second range
return true;
else if (end1 >= beg2 && end1 <= end2) // first range ends within second range
return true;
else
return false;
}
Object::Object(TADDR addr)
: mAddress(addr), mMT(0), mSize(~0), mPointers(false), mMTData(0), mTypeName(0)
{
if ((mAddress & ~ALIGNCONST) != mAddress)
sos::Throw<Exception>("Object %p is misaligned.", mAddress);
}
Object::Object(TADDR addr, TADDR mt)
: mAddress(addr), mMT(mt & ~3), mSize(~0), mPointers(false), mMTData(0), mTypeName(0)
{
if ((mAddress & ~ALIGNCONST) != mAddress)
sos::Throw<Exception>("Object %p is misaligned.", mAddress);
}
Object::Object(const Object &rhs)
: mAddress(rhs.mAddress), mMT(rhs.mMT), mSize(rhs.mSize), mPointers(rhs.mPointers), mMTData(rhs.mMTData), mTypeName(rhs.mTypeName)
{
rhs.mMTData = 0;
rhs.mTypeName = 0;
}
const Object &Object::operator=(TADDR addr)
{
if (mMTData)
delete mMTData;
if (mTypeName)
delete mTypeName;
mAddress = addr;
mMT = 0;
mSize = ~0;
mMTData = 0;
mTypeName = 0;
return *this;
}
bool Object::TryGetHeader(ULONG &outHeader) const
{
struct ObjectHeader
{
#ifdef _WIN64
ULONG _alignpad;
#endif
ULONG SyncBlockValue; // the Index and the Bits
};
ObjectHeader header;
if (SUCCEEDED(rvCache->Read(TO_TADDR(GetAddress() - sizeof(ObjectHeader)), &header, sizeof(ObjectHeader), NULL)))
{
outHeader = header.SyncBlockValue;
return true;
}
return false;
}
ULONG Object::GetHeader() const
{
ULONG toReturn = 0;
if (!TryGetHeader(toReturn))
sos::Throw<DataRead>("Failed to get header for object %p.", GetAddress());
return toReturn;
}
TADDR Object::GetMT() const
{
if (mMT == (TADDR)0)
{
TADDR temp;
if (FAILED(MOVE(temp, mAddress)))
sos::Throw<DataRead>("Object %s has an invalid method table.", DMLListNearObj(mAddress));
if (temp == (TADDR)0)
sos::Throw<HeapCorruption>("Object %s has an invalid method table.", DMLListNearObj(mAddress));
mMT = temp & ~METHODTABLE_PTR_LOW_BITMASK;
}
return mMT;
}
TADDR Object::GetComponentMT() const
{
if (mMT != (TADDR)0 && mMT != sos::MethodTable::GetArrayMT())
return (TADDR)0;
DacpObjectData objData;
if (FAILED(objData.Request(g_sos, TO_CDADDR(mAddress))))
sos::Throw<DataRead>("Failed to request object data for %s.", DMLListNearObj(mAddress));
if (mMT == (TADDR)0)
mMT = TO_TADDR(objData.MethodTable) & ~METHODTABLE_PTR_LOW_BITMASK;
return TO_TADDR(objData.ElementTypeHandle);
}
const WCHAR *Object::GetTypeName() const
{
if (mTypeName == NULL)
mTypeName = CreateMethodTableName(GetMT(), GetComponentMT());
if (mTypeName == NULL)
return W("<error>");
return mTypeName;
}
void Object::FillMTData() const
{
if (mMTData == NULL)
{
mMTData = new DacpMethodTableData;
if (FAILED(mMTData->Request(g_sos, GetMT())))
{
delete mMTData;
mMTData = NULL;
sos::Throw<DataRead>("Could not request method table data for object %p (MethodTable: %p).", mAddress, mMT);
}
}
}
void Object::CalculateSizeAndPointers() const
{
TADDR mt = GetMT();
MethodTableInfo* info = g_special_mtCache.Lookup((DWORD_PTR)mt);
if (!info->IsInitialized())
{
// this is the first time we see this method table, so we need to get the information
// from the target
FillMTData();
info->BaseSize = mMTData->BaseSize;
info->ComponentSize = mMTData->ComponentSize;
info->bContainsPointers = mMTData->bContainsPointers;
// The following request doesn't work on older runtimes. For those, the
// objects would just look like non-collectible, which is acceptable.
DacpMethodTableCollectibleData mtcd;
if (SUCCEEDED(mtcd.Request(g_sos, GetMT())))
{
info->bCollectible = mtcd.bCollectible;
info->LoaderAllocatorObjectHandle = TO_TADDR(mtcd.LoaderAllocatorObjectHandle);
}
}
if (mSize == (size_t)~0)
{
mSize = info->BaseSize;
if (info->ComponentSize)
{
// this is an array, so the size has to include the size of the components. We read the number
// of components from the target and multiply by the component size to get the size.
mSize += info->ComponentSize * GetNumComponents(GetAddress());
}
// On x64 we do an optimization to save 4 bytes in almost every string we create.
#ifdef _WIN64
// Pad to min object size if necessary
if (mSize < min_obj_size)
mSize = min_obj_size;
#endif // _WIN64
}
mPointers = info->bContainsPointers != FALSE;
}
size_t Object::GetSize() const
{
if (mSize == (size_t)~0) // poison value
{
CalculateSizeAndPointers();
}
SOS_Assert(mSize != (size_t)~0);
return mSize;
}
bool Object::HasPointers() const
{
if (mSize == (size_t)~0)
CalculateSizeAndPointers();
SOS_Assert(mSize != (size_t)~0);
return mPointers;
}
bool Object::VerifyMemberFields(TADDR pMT, TADDR obj)
{
WORD numInstanceFields = 0;
return VerifyMemberFields(pMT, obj, numInstanceFields);
}
bool Object::VerifyMemberFields(TADDR pMT, TADDR obj, WORD &numInstanceFields)
{
DacpMethodTableData vMethTable;
if (FAILED(vMethTable.Request(g_sos, pMT)))
return false;
// Recursively verify the parent (this updates numInstanceFields)
if (vMethTable.ParentMethodTable)
{
if (!VerifyMemberFields(TO_TADDR(vMethTable.ParentMethodTable), obj, numInstanceFields))
return false;
}
DacpMethodTableFieldData vMethodTableFields;
// Verify all fields on the object.
CLRDATA_ADDRESS dwAddr = vMethodTableFields.FirstField;
DacpFieldDescData vFieldDesc;
while (numInstanceFields < vMethodTableFields.wNumInstanceFields)
{
CheckInterrupt();
if (FAILED(vFieldDesc.Request(g_sos, dwAddr)))
return false;
if (vFieldDesc.Type >= ELEMENT_TYPE_MAX)
return false;
dwAddr = vFieldDesc.NextField;
if (!vFieldDesc.bIsStatic)
{
numInstanceFields++;
TADDR dwTmp = TO_TADDR(obj + vFieldDesc.dwOffset + sizeof(BaseObject));
if (vFieldDesc.Type == ELEMENT_TYPE_CLASS)
{
// Is it a valid object?
if (FAILED(MOVE(dwTmp, dwTmp)))
return false;
if (dwTmp != (TADDR)0)
{
DacpObjectData objData;
if (FAILED(objData.Request(g_sos, TO_CDADDR(dwTmp))))
return false;
}
}
}
}
return true;
}
bool MethodTable::IsZombie(TADDR addr)
{
// Zombie objects are objects that reside in an unloaded AppDomain.
MethodTable mt = addr;
return _wcscmp(mt.GetName(), W("<Unloaded Type>")) == 0;
}
void MethodTable::Clear()
{
if (mName)
{
delete [] mName;
mName = NULL;
}
}
const WCHAR *MethodTable::GetName() const
{
if (mName == NULL)
mName = CreateMethodTableName(mMT);
if (mName == NULL)
return W("<error>");
return mName;
}
bool Object::IsValid(TADDR address, bool verifyFields)
{
DacpObjectData objectData;
if (FAILED(objectData.Request(g_sos, TO_CDADDR(address))))
return false;
if (verifyFields &&
objectData.MethodTable != g_special_usefulGlobals.FreeMethodTable &&
!MethodTable::IsZombie(TO_TADDR(objectData.MethodTable)))
{
return VerifyMemberFields(TO_TADDR(objectData.MethodTable), address);
}
return true;
}
bool Object::GetThinLock(ThinLockInfo &out) const
{
ULONG header = GetHeader();
if (header & (BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX | BIT_SBLK_SPIN_LOCK))
{
return false;
}
out.ThreadId = header & SBLK_MASK_LOCK_THREADID;
out.Recursion = (header & SBLK_MASK_LOCK_RECLEVEL) >> SBLK_RECLEVEL_SHIFT;
CLRDATA_ADDRESS threadPtr = (TADDR)0;
if (g_sos->GetThreadFromThinlockID(out.ThreadId, &threadPtr) != S_OK)
{
out.ThreadPtr = (TADDR)0;
}
else
{
out.ThreadPtr = TO_TADDR(threadPtr);
}
return out.ThreadId != 0 && out.ThreadPtr != (TADDR)0;
}
bool Object::GetStringData(__out_ecount(size) WCHAR *buffer, size_t size) const
{
SOS_Assert(IsString());
SOS_Assert(buffer);
SOS_Assert(size > 0);
return SUCCEEDED(g_sos->GetObjectStringData(mAddress, (ULONG32)size, buffer, NULL));
}
size_t Object::GetStringLength() const
{
SOS_Assert(IsString());
strobjInfo stInfo;
if (FAILED(MOVE(stInfo, mAddress)))
sos::Throw<DataRead>("Failed to read object data at %p.", mAddress);
// We get the method table for free here, if we don't have it already.
SOS_Assert((mMT == (TADDR)0) || (mMT == TO_TADDR(stInfo.methodTable)));
if (mMT == (TADDR)0)
mMT = TO_TADDR(stInfo.methodTable);
return (size_t)stInfo.m_StringLength;
}
// SyncBlk class
SyncBlk::SyncBlk()
: mIndex(0)
{
}
SyncBlk::SyncBlk(int index)
: mIndex(index)
{
Init();
}
const SyncBlk &SyncBlk::operator=(int index)
{
mIndex = index;
Init();
return *this;
}
void SyncBlk::Init()
{
if (FAILED(mData.Request(g_sos, mIndex)))
sos::Throw<DataRead>("Failed to request SyncBlk at index %d.", mIndex);
}
TADDR SyncBlk::GetAddress() const
{
SOS_Assert(mIndex);
return TO_TADDR(mData.SyncBlockPointer);
}
TADDR SyncBlk::GetObject() const
{
SOS_Assert(mIndex);
return TO_TADDR(mData.Object);
}
int SyncBlk::GetIndex() const
{
return mIndex;
}
bool SyncBlk::IsFree() const
{
SOS_Assert(mIndex);
return mData.bFree != FALSE;
}
unsigned int SyncBlk::GetMonitorHeldCount() const
{
SOS_Assert(mIndex);
return mData.MonitorHeld;
}
unsigned int SyncBlk::GetRecursion() const
{
SOS_Assert(mIndex);
return mData.Recursion;
}
DWORD SyncBlk::GetCOMFlags() const
{
SOS_Assert(mIndex);
#ifdef FEATURE_COMINTEROP
return mData.COMFlags;
#else
return 0;
#endif
}
unsigned int SyncBlk::GetAdditionalThreadCount() const
{
SOS_Assert(mIndex);
return mData.AdditionalThreadCount;
}
TADDR SyncBlk::GetHoldingThread() const
{
SOS_Assert(mIndex);
return TO_TADDR(mData.HoldingThread);
}
TADDR SyncBlk::GetAppDomain() const
{
SOS_Assert(mIndex);
return TO_TADDR(mData.appDomainPtr);
}
void BuildTypeWithExtraInfo(TADDR addr, unsigned int size, __inout_ecount(size) WCHAR *buffer)
{
try
{
sos::Object obj(addr);
TADDR mtAddr = obj.GetMT();
bool isArray = sos::MethodTable::IsArrayMT(mtAddr);
bool isString = obj.IsString();
sos::MethodTable mt(isArray ? obj.GetComponentMT() : mtAddr);
if (isArray)
{
swprintf_s(buffer, size, W("%s[]"), mt.GetName());
}
else if (isString)
{
WCHAR str[32];
obj.GetStringData(str, ARRAY_SIZE(str));
_snwprintf_s(buffer, size, _TRUNCATE, W("%s: \"%s\""), mt.GetName(), str);
}
else
{
_snwprintf_s(buffer, size, _TRUNCATE, W("%s"), mt.GetName());
}
}
catch (const sos::Exception &e)
{
int len = MultiByteToWideChar(CP_ACP, 0, e.what(), -1, NULL, 0);
ArrayHolder<WCHAR> tmp = new WCHAR[len];
MultiByteToWideChar(CP_ACP, 0, e.what(), -1, (WCHAR*)tmp, len);
swprintf_s(buffer, size, W("<invalid object: '%s'>"), (WCHAR*)tmp);
}
}
}