-
Notifications
You must be signed in to change notification settings - Fork 74
/
DelegatesAnalysis.cs
287 lines (253 loc) · 9.32 KB
/
DelegatesAnalysis.cs
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
using MemoScope.Core;
using MemoScope.Core.Data;
using Microsoft.Diagnostics.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using WinFwk.UIModules;
using ClrObject = MemoScope.Core.Data.ClrObject;
namespace MemoScope.Modules.Delegates
{
// Thanks to Alois Kraus for his article about delegate internals:
// http://geekswithblogs.net/akraus1/archive/2012/05/20/149699.aspx
public static class DelegatesAnalysis
{
const string TargetFieldName = "_target";
const string InvocationCountFieldName = "_invocationCount";
const string MethodBaseFieldName = "_methodBase";
const string MethodPtrFieldName = "_methodPtr";
const string MethodPtrAuxFieldName = "_methodPtrAux";
const string InvocationListFieldName = "_invocationList";
public static bool IsDelegateType(ClrDumpType clrDumpType)
{
var delegateType = clrDumpType.ClrDump.GetClrType(typeof(MulticastDelegate).FullName);
return clrDumpType.BaseType != null && clrDumpType.BaseType.ClrType == delegateType;
}
public static List<ClrType> GetDelegateTypes(ClrDump clrDump)
{
CancellationTokenSource token = new CancellationTokenSource();
clrDump.MessageBus.BeginTask("Analyzing delegate types...", token);
List<ClrType> delegates = new List<ClrType>();
var delegateType = clrDump.GetClrType(typeof(MulticastDelegate).FullName);
foreach (var type in clrDump.AllTypes)
{
clrDump.MessageBus.Status($"Analyzing delegate type: {type.Name}");
if (token.IsCancellationRequested)
{
break;
}
if (type.BaseType != null && type.BaseType == delegateType)
{
clrDump.MessageBus.Status($"Analyzing delegate type: counting instances for {type.Name}");
int nb = clrDump.CountInstances(type);
if (nb > 0)
{
delegates.Add(type);
}
}
}
clrDump.MessageBus.EndTask("Delegate types analyzed.");
return delegates.GroupBy(t => t.Name).Select(g => g.First()).ToList();
}
internal static IEnumerable<ClrObject> EnumerateHandlers(ClrObject clrObject)
{
ClrObject target = clrObject[TargetFieldName];
ulong targetAddress = target.Address;
if (targetAddress != clrObject.Address)
{
yield return clrObject;
}
else
{
var invocCount = clrObject[InvocationCountFieldName];
var v = invocCount.SimpleValue;
var invocCountValue = (long)v;
var invocList = clrObject[InvocationListFieldName];
if (! invocList.IsNull)
{
for (int i = 0; i < invocCountValue; i++)
{
var targetObject = invocList[i];
yield return targetObject;
}
}
}
}
internal static List<DelegateTargetInformation> GetDelegateTargetInformations(ClrDumpObject clrDumpObject)
{
var targetInformations = new List<DelegateTargetInformation>();
ClrObject clrObject = clrDumpObject.ClrObject;
ClrDump clrDump = clrDumpObject.ClrDump;
foreach (var handlerObject in EnumerateHandlers(clrObject))
{
var target = handlerObject[TargetFieldName];
var methInfo = GetDelegateMethod(clrDump, handlerObject, target);
var targetInfo = new DelegateTargetInformation(target.Address, new ClrDumpType(clrDump, target.Type), methInfo);
targetInformations.Add(targetInfo);
}
return targetInformations;
}
private static ClrMethod GetDelegateMethod(ClrDump clrDump, ClrObject handler, ClrObject target)
{
var targetType = target.Type;
var methPtrObj = handler[MethodPtrFieldName];
var methPtrLong = (long)methPtrObj.SimpleValue;
var methPtr = (ulong)methPtrLong;
var methInfo = GetDelegateMethod(methPtr, clrDump);
return methInfo;
}
// Thanks to Lee Culver and Jeff Cyr
// for the code:
// https://github.com/Microsoft/clrmd/issues/35
public static ClrMethod GetDelegateMethod(ulong methodPtr, ClrDump clrDump)
{
ulong magicPtr = methodPtr + 5;
ulong magicValue1 = clrDump.ReadHeapPointer(magicPtr + 1);
ulong magicValue2 = clrDump.ReadHeapPointer(magicPtr + 2);
ulong mysticPtr = magicPtr + 8 * (magicValue2 & 0xFF) + 3;
ulong mysticOffset = 8 * (magicValue1 & 0xFF);
ulong mysticValue = clrDump.ReadRuntimePointer(mysticPtr);
ulong methodDescriptorPtr = mysticValue + mysticOffset;
ClrMethod method = clrDump.GetMethodByHandle(methodDescriptorPtr);
return method;
}
public static long CountTargets(ClrDump clrDump, ClrType clrType)
{
long count = 0;
var targetField = clrType.GetFieldByName(TargetFieldName);
var invocCountField = clrType.GetFieldByName(InvocationCountFieldName);
foreach (ulong address in clrDump.EnumerateInstances(clrType))
{
count += CountTargets(address, clrType, targetField, invocCountField);
}
return count;
}
internal static List<DelegateInstanceInformation> GetDelegateInstanceInformation(ClrDumpType clrDumpType)
{
var clrType = clrDumpType.ClrType;
var delegateInstanceInformations = new List<DelegateInstanceInformation>();
var targetField = clrType.GetFieldByName(TargetFieldName);
var invocCountField = clrType.GetFieldByName(InvocationCountFieldName);
foreach (var address in clrDumpType.EnumerateInstances())
{
var targets = CountTargets(address, clrType, targetField, invocCountField);
var delegateInstanceInformation = new DelegateInstanceInformation(address, clrDumpType, targets);
delegateInstanceInformations.Add(delegateInstanceInformation);
}
return delegateInstanceInformations;
}
internal static List<DelegateTypeInformation> GetDelegateTypeInformations(ClrDump clrDump)
{
var types = GetDelegateTypes(clrDump);
var typeInformations = new List<DelegateTypeInformation>();
foreach (var type in types)
{
var count = clrDump.CountInstances(type);
var targets = CountTargets(clrDump, type);
var delegateInformation = new DelegateTypeInformation(clrDump, type, count, targets);
typeInformations.Add(delegateInformation);
}
return typeInformations;
}
public static long CountTargets(ulong address, ClrType clrType, ClrInstanceField targetField, ClrInstanceField invocCountField)
{
ClrObject clrObject = new ClrObject(address, clrType);
ClrObject target = clrObject[targetField];
if (target.Address != address)
{
return 1;
}
var invocCount = clrObject[invocCountField];
var value = invocCount.SimpleValue;
return (long)value;
}
public static List<LoneTargetInformation> GetLoneTargetInformations(ClrDump clrDump)
{
CancellationTokenSource token = new CancellationTokenSource();
clrDump.MessageBus.BeginTask("Analyzing lone targets...", token);
Dictionary<ClrObject, ClrObject> loneTargetAddresses = new Dictionary<ClrObject, ClrObject>();
// For each instance of every delegate types
// let's find all the target objects
// and select those with only referenced once
var types = GetDelegateTypes(clrDump);
foreach (var type in types)
{
clrDump.MessageBus.Status($"Analyzing delegate type: {type.Name}");
if (token.IsCancellationRequested)
{
break;
}
int n = 0;
foreach (var address in clrDump.EnumerateInstances(type))
{
if (n++ % 128 == 0)
{
clrDump.MessageBus.Status($"Analyzing delegate type: {type.Name}, instance #{n:###,###,###,##0}");
}
var handlerObject = new ClrObject(address, type);
foreach (var subHandlerObject in EnumerateHandlers(handlerObject))
{
if (token.IsCancellationRequested)
{
break;
}
var target = subHandlerObject[TargetFieldName];
int count = clrDump.CountReferers(target.Address);
if (count == 1)
{
loneTargetAddresses[target] = subHandlerObject;
}
}
}
}
List<LoneTargetInformation> loneTargets = new List<LoneTargetInformation>();
// foreach lone target, in its reference tree, we try to find the first
// object that is not a delegate type or an array of object (ie invocationList)
var delegateType = clrDump.GetClrType(typeof(MulticastDelegate).FullName);
var arrayObjType = clrDump.GetClrType(typeof(object[]).FullName);
HashSet<ulong> visited = new HashSet<ulong>();
foreach (var kvp in loneTargetAddresses)
{
var loneTarget = kvp.Key;
var handler = kvp.Value;
var methInfo = GetDelegateMethod(clrDump, handler, loneTarget);
visited.Clear();
ulong ownerAddress = FindOwner(handler.Address, clrDump, delegateType, arrayObjType, visited);
ClrObject owner = new ClrObject(ownerAddress, clrDump.GetObjectType(ownerAddress));
var loneTargetInformation = new LoneTargetInformation(clrDump, loneTarget, methInfo, owner);
loneTargets.Add(loneTargetInformation);
}
string status = token.IsCancellationRequested ? "cancelled" : "done";
clrDump.MessageBus.EndTask($"Analyzing lone targets: {status}. Found: {loneTargets.Count}");
return loneTargets;
}
public static ulong FindOwner(ulong address, ClrDump clrDump, ClrType delegateType, ClrType arrayObjType, HashSet<ulong> visited)
{
if (visited.Contains(address))
{
return 0;
}
var type = clrDump.GetObjectType(address);
if (type == null)
{
return 0;
}
if (type != arrayObjType && (type.BaseType == null || type.BaseType != delegateType))
{
return address;
}
visited.Add(address);
var refs = clrDump.GetReferers(address);
foreach (var newAddress in refs)
{
var owner = FindOwner(newAddress, clrDump, delegateType, arrayObjType, visited);
if (owner != 0)
{
return owner;
}
}
return 0;
}
}
}