-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzeRoots.js
485 lines (429 loc) · 15 KB
/
analyzeRoots.js
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
/* -*- Mode: Javascript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
"use strict";
load('utility.js');
load('annotations.js');
load('suppressedPoints.js');
var functionName;
var functionBodies;
if (typeof arguments[0] != 'string' || typeof arguments[1] != 'string')
throw "Usage: analyzeRoots.js <gcFunctions.html> <gcTypes.txt>";
var match;
var gcFunctions = {};
var suppressedFunctions = {};
assert(!system("grep 'GC Function' " + arguments[0] + " > tmp.txt"));
var text = snarf("tmp.txt").split('\n');
assert(text.pop().length == 0);
for (var line of text) {
match = /GC Function: (.*)/.exec(line);
gcFunctions[match[1]] = true;
}
assert(!system("grep 'Suppressed Function' " + arguments[0] + " > tmp.txt"));
text = snarf("tmp.txt").split('\n');
assert(text.pop().length == 0);
for (var line of text) {
match = /Suppressed Function: (.*)/.exec(line);
suppressedFunctions[match[1]] = true;
}
text = null;
var gcThings = {};
var gcPointers = {};
var gcTypesText = snarf(arguments[1]).split('\n');
for (var line of gcTypesText) {
if (match = /GCThing: (.*)/.exec(line))
gcThings[match[1]] = true;
if (match = /GCPointer: (.*)/.exec(line))
gcPointers[match[1]] = true;
}
gcTypesText = null;
function isUnrootedType(type)
{
if (type.Kind == "Pointer") {
var target = type.Type;
if (target.Kind == "CSU")
return target.Name in gcThings;
return false;
}
if (type.Kind == "CSU")
return type.Name in gcPointers;
return false;
}
function expressionUsesVariable(exp, variable)
{
if (exp.Kind == "Var" && sameVariable(exp.Variable, variable))
return true;
if (!("Exp" in exp))
return false;
for (var childExp of exp.Exp) {
if (expressionUsesVariable(childExp, variable))
return true;
}
return false;
}
function edgeUsesVariable(edge, variable)
{
if (ignoreEdgeUse(edge, variable))
return false;
switch (edge.Kind) {
case "Assign":
if (expressionUsesVariable(edge.Exp[0], variable))
return true;
return expressionUsesVariable(edge.Exp[1], variable);
case "Assume":
return expressionUsesVariable(edge.Exp[0], variable);
case "Call":
if (expressionUsesVariable(edge.Exp[0], variable))
return true;
if (1 in edge.Exp && expressionUsesVariable(edge.Exp[1], variable))
return true;
if ("PEdgeCallInstance" in edge) {
if (expressionUsesVariable(edge.PEdgeCallInstance.Exp, variable))
return true;
}
if ("PEdgeCallArguments" in edge) {
for (var exp of edge.PEdgeCallArguments.Exp) {
if (expressionUsesVariable(exp, variable))
return true;
}
}
return false;
case "Loop":
return false;
default:
assert(false);
}
}
function edgeKillsVariable(edge, variable)
{
// Direct assignments kill their lhs.
if (edge.Kind == "Assign") {
var lhs = edge.Exp[0];
if (lhs.Kind == "Var" && sameVariable(lhs.Variable, variable))
return true;
}
if (edge.Kind != "Call")
return false;
// Assignments of call results kill their lhs.
if (1 in edge.Exp) {
var lhs = edge.Exp[1];
if (lhs.Kind == "Var" && sameVariable(lhs.Variable, variable))
return true;
}
// Constructor calls kill their 'this' value.
if ("PEdgeCallInstance" in edge) {
do {
var instance = edge.PEdgeCallInstance.Exp;
// Kludge around incorrect dereference on some constructor calls.
if (instance.Kind == "Drf")
instance = instance.Exp[0];
if (instance.Kind != "Var" || !sameVariable(instance.Variable, variable))
break;
var callee = edge.Exp[0];
if (callee.Kind != "Var")
break;
assert(callee.Variable.Kind == "Func");
var calleeName = callee.Variable.Name[0];
// Constructor calls include the text 'Name::Name(' or 'Name<...>::Name('.
var openParen = calleeName.indexOf('(');
if (openParen < 0)
break;
calleeName = calleeName.substring(0, openParen);
var lastColon = calleeName.lastIndexOf('::');
if (lastColon < 0)
break;
var constructorName = calleeName.substr(lastColon + 2);
calleeName = calleeName.substr(0, lastColon);
var lastTemplateOpen = calleeName.lastIndexOf('<');
if (lastTemplateOpen >= 0)
calleeName = calleeName.substr(0, lastTemplateOpen);
if (calleeName.endsWith(constructorName))
return true;
} while (false);
}
return false;
}
function edgeCanGC(edge)
{
if (functionName in suppressedFunctions)
return false;
if (edge.Kind != "Call")
return false;
var callee = edge.Exp[0];
if (callee.Kind == "Var") {
var variable = callee.Variable;
assert(variable.Kind == "Func");
if (variable.Name[0] in gcFunctions)
return "'" + variable.Name[0] + "'";
var otherName = otherDestructorName(variable.Name[0]);
if (otherName in gcFunctions)
return "'" + otherName + "'";
return null;
}
assert(callee.Kind == "Drf");
if (callee.Exp[0].Kind == "Fld") {
var field = callee.Exp[0].Field;
var csuName = field.FieldCSU.Type.Name;
var fieldName = field.Name[0];
return fieldCallCannotGC(csuName, fieldName) ? null : csuName + "." + fieldName;
}
assert(callee.Exp[0].Kind == "Var");
var calleeName = callee.Exp[0].Variable.Name[0];
return indirectCallCannotGC(functionName, calleeName) ? null : "*" + calleeName;
}
function computePredecessors(body)
{
body.predecessors = [];
if (!("PEdge" in body))
return;
for (var edge of body.PEdge) {
var target = edge.Index[1];
if (!(target in body.predecessors))
body.predecessors[target] = [];
body.predecessors[target].push(edge);
}
}
function variableUseFollowsGC(variable, worklist)
{
while (worklist.length) {
var entry = worklist.pop();
var body = entry.body, ppoint = entry.ppoint;
if (body.seen) {
if (ppoint in body.seen) {
var seenEntry = body.seen[ppoint];
if (!entry.gcInfo || seenEntry.gcInfo)
continue;
}
} else {
body.seen = [];
}
body.seen[ppoint] = {body:body, gcInfo:entry.gcInfo};
if (ppoint == body.Index[0]) {
if (body.BlockId.Kind == "Loop") {
// propagate to parents which enter the loop body.
if ("BlockPPoint" in body) {
for (var parent of body.BlockPPoint) {
var found = false;
for (var xbody of functionBodies) {
if (sameBlockId(xbody.BlockId, parent.BlockId)) {
assert(!found);
found = true;
worklist.push({body:xbody, ppoint:parent.Index,
gcInfo:entry.gcInfo, why:entry});
}
}
assert(found);
}
}
} else if (variable.Kind == "Arg" && entry.gcInfo) {
return {gcInfo:entry.gcInfo, why:entry};
}
}
if (!body.predecessors)
computePredecessors(body);
if (!(ppoint in body.predecessors))
continue;
for (var edge of body.predecessors[ppoint]) {
var source = edge.Index[0];
if (edgeKillsVariable(edge, variable)) {
if (entry.gcInfo)
return {gcInfo:entry.gcInfo, why:entry};
if (!body.minimumUse || source < body.minimumUse)
body.minimumUse = source;
continue;
}
var gcInfo = entry.gcInfo;
if (!gcInfo && !(edge.Index[0] in body.suppressed)) {
var gcName = edgeCanGC(edge);
if (gcName)
gcInfo = {name:gcName, body:body, ppoint:source};
}
if (edgeUsesVariable(edge, variable)) {
if (gcInfo)
return {gcInfo:gcInfo, why:entry};
if (!body.minimumUse || source < body.minimumUse)
body.minimumUse = source;
}
if (edge.Kind == "Loop") {
// propagate to exit points of the loop body, in addition to the
// predecessor of the loop edge itself.
var found = false;
for (var xbody of functionBodies) {
if (sameBlockId(xbody.BlockId, edge.BlockId)) {
assert(!found);
found = true;
worklist.push({body:xbody, ppoint:xbody.Index[1],
gcInfo:gcInfo, why:entry});
}
}
assert(found);
break;
}
worklist.push({body:body, ppoint:source, gcInfo:gcInfo, why:entry});
}
}
return null;
}
function variableLiveAcrossGC(variable)
{
for (var body of functionBodies) {
body.seen = null;
body.minimumUse = 0;
}
for (var body of functionBodies) {
if (!("PEdge" in body))
continue;
for (var edge of body.PEdge) {
if (edgeUsesVariable(edge, variable) && !edgeKillsVariable(edge, variable)) {
var worklist = [{body:body, ppoint:edge.Index[0], gcInfo:null, why:null}];
var call = variableUseFollowsGC(variable, worklist);
if (call)
return call;
}
}
}
return null;
}
function computePrintedLines()
{
assert(!system("xdbfind src_body.xdb '" + functionName + "' > tmp.txt"));
var lines = snarf("tmp.txt").split('\n');
for (var body of functionBodies)
body.lines = [];
// Distribute lines of output to the block they originate from.
var currentBody = null;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (/^block:/.test(line)) {
if (match = /:(loop#[\d#]+)/.exec(line)) {
var loop = match[1];
var found = false;
for (var body of functionBodies) {
if (body.BlockId.Kind == "Loop" && body.BlockId.Loop == loop) {
assert(!found);
found = true;
currentBody = body;
}
}
assert(found);
} else {
for (var body of functionBodies) {
if (body.BlockId.Kind == "Function")
currentBody = body;
}
}
}
if (currentBody)
currentBody.lines.push(line);
}
}
function findLocation(body, ppoint)
{
var location = body.PPoint[ppoint - 1].Location;
var text = location.CacheString + ":" + location.Line;
if (match = /.*?mozilla-inbound\/js\/src\/(.*)/.exec(text))
return match[1];
if (match = /.*?mozilla-inbound\/(.*)/.exec(text))
return match[1];
return text;
}
function locationLine(text)
{
if (match = /:(\d+)$/.exec(text))
return match[1];
return 0;
}
function printEntryTrace(entry)
{
if (!functionBodies[0].lines)
computePrintedLines();
while (entry) {
var ppoint = entry.ppoint;
var lineText = findLocation(entry.body, ppoint);
var edgeText = null;
if (entry.why && entry.why.body == entry.body) {
// If the next point in the trace is in the same block, look for an edge between them.
var next = entry.why.ppoint;
for (var line of entry.body.lines) {
if (match = /\((\d+),(\d+),/.exec(line)) {
if (match[1] == ppoint && match[2] == next)
edgeText = line;
}
}
assert(edgeText);
} else {
// Look for any outgoing edge from the chosen point.
for (var line of entry.body.lines) {
if (match = /\((\d+),/.exec(line)) {
if (match[1] == ppoint) {
edgeText = line;
break;
}
}
}
}
print(" " + lineText + (edgeText ? ": " + edgeText : ""));
entry = entry.why;
}
}
function isRootedType(type)
{
return type.Kind == "CSU" && isRootedTypeName(type.Name);
}
function processBodies()
{
if (!("DefineVariable" in functionBodies[0]))
return;
for (var variable of functionBodies[0].DefineVariable) {
if (variable.Variable.Kind == "Return")
continue;
var name;
if (variable.Variable.Kind == "This")
name = "this";
else
name = variable.Variable.Name[0];
if (isRootedType(variable.Type)) {
if (!variableLiveAcrossGC(variable.Variable)) {
// The earliest use of the variable should be its constructor.
var lineText;
for (var body of functionBodies) {
if (body.minimumUse) {
var text = findLocation(body, body.minimumUse);
if (!lineText || locationLine(lineText) > locationLine(text))
lineText = text;
}
}
//print("\nFunction '" + functionName + "'" +
// " has unnecessary root '" + name + "' at " + lineText);
}
} else if (isUnrootedType(variable.Type)) {
var result = variableLiveAcrossGC(variable.Variable);
if (result) {
var lineText = findLocation(result.gcInfo.body, result.gcInfo.ppoint);
print("\nFunction '" + functionName + "'" +
" has unrooted '" + name + "'" +
" live across GC call " + result.gcInfo.name +
" at " + lineText);
printEntryTrace(result.why);
}
}
}
}
print("<html><pre>");
print("Time: " + new Date);
var xdb = xdbLibrary();
xdb.open("src_body.xdb");
var minStream = xdb.min_data_stream();
var maxStream = xdb.max_data_stream();
for (var nameIndex = minStream; nameIndex <= maxStream; nameIndex++) {
var name = xdb.read_key(nameIndex);
functionName = name.readString();
var data = xdb.read_entry(name);
functionBodies = JSON.parse(data.readString());
for (var body of functionBodies)
body.suppressed = [];
for (var body of functionBodies)
computeSuppressedPoints(body);
processBodies();
xdb.free_string(name);
xdb.free_string(data);
}
print("\n</pre></html>");