summaryrefslogtreecommitdiffstats
path: root/js/src/devtools/rootAnalysis/computeGCTypes.js
blob: eb327da5d2cacdf68f7c9b6455a13d2f3cddd729 (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
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
506
507
508
509
510
511
512
513
514
515
516
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */

"use strict";

loadRelativeToScript('utility.js');
loadRelativeToScript('annotations.js');

var options = parse_options([
    { name: "gcTypes", default: "gcTypes.txt" },
    { name: "typeInfo", default: "typeInfo.txt" }
]);

var typeInfo = {
    'GCPointers': [],
    'GCThings': [],
    'GCInvalidated': [],
    'GCRefs': [],
    'NonGCTypes': {}, // unused
    'NonGCPointers': {},
    'RootedGCThings': {},
    'RootedPointers': {},
    'RootedBases': {'JS::AutoGCRooter': true},
    'InheritFromTemplateArgs': {},
    'OtherCSUTags': {},
    'OtherFieldTags': {},

    // RAII types within which we should assume GC is suppressed, eg
    // AutoSuppressGC.
    'GCSuppressors': {},
};

var gDescriptors = new Map; // Map from descriptor string => Set of typeName

var structureParents = {}; // Map from field => list of <parent, fieldName>
var pointerParents = {}; // Map from field => list of <parent, fieldName>
var baseClasses = {}; // Map from struct name => list of base class name strings
var subClasses = {}; // Map from struct name => list of subclass  name strings

var gcTypes = {}; // map from parent struct => Set of GC typed children
var gcPointers = {}; // map from parent struct => Set of GC typed children
var gcFields = new Map;

var rootedPointers = {};

// Accumulate the base GC types before propagating info through the type graph,
// so that we can include edges from types processed later
// (eg MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS).
var pendingGCTypes = []; // array of [name, reason, ptrdness]

function processCSU(csu, body)
{
    for (let { 'Name': [ annType, tag ] } of (body.Annotation || [])) {
        if (annType != 'annotate')
            continue;

        if (tag == 'GC Pointer')
            typeInfo.GCPointers.push(csu);
        else if (tag == 'Invalidated by GC')
            typeInfo.GCInvalidated.push(csu);
        else if (tag == 'GC Pointer or Reference')
            typeInfo.GCRefs.push(csu);
        else if (tag == 'GC Thing')
            typeInfo.GCThings.push(csu);
        else if (tag == 'Suppressed GC Pointer')
            typeInfo.NonGCPointers[csu] = true;
        else if (tag == 'Rooted Pointer')
            typeInfo.RootedPointers[csu] = true;
        else if (tag == 'Rooted Base')
            typeInfo.RootedBases[csu] = true;
        else if (tag == 'Suppress GC')
            typeInfo.GCSuppressors[csu] = true;
        else if (tag == 'moz_inherit_type_annotations_from_template_args')
            typeInfo.InheritFromTemplateArgs[csu] = true;
        else
            addToKeyedList(typeInfo.OtherCSUTags, csu, tag);
    }

    for (let { 'Base': base } of (body.CSUBaseClass || []))
        addBaseClass(csu, base);

    for (const field of (body.DataField || [])) {
        var type = field.Field.Type;
        var fieldName = field.Field.Name[0];
        if (type.Kind == "Pointer") {
            var target = type.Type;
            if (target.Kind == "CSU")
                addNestedPointer(csu, target.Name, fieldName);
        }
        if (type.Kind == "Array") {
            var target = type.Type;
            if (target.Kind == "CSU")
                addNestedStructure(csu, target.Name, fieldName);
        }
        if (type.Kind == "CSU")
            addNestedStructure(csu, type.Name, fieldName);

        for (const { 'Name': [ annType, tag ] } of (field.Annotation || [])) {
            if (!(csu in typeInfo.OtherFieldTags))
                typeInfo.OtherFieldTags[csu] = [];
            addToKeyedList(typeInfo.OtherFieldTags[csu], fieldName, tag);
        }
    }

    for (const funcfield of (body.FunctionField || [])) {
        const fields = funcfield.Field;
        // Pure virtual functions will not have field.Variable; others will.
        for (const field of funcfield.Field) {
            for (const {'Name': [annType, tag]} of (field.Annotation || [])) {
                if (!(csu in typeInfo.OtherFieldTags))
                    typeInfo.OtherFieldTags[csu] = {};
                addToKeyedList(typeInfo.OtherFieldTags[csu], field.Name[0], tag);
            }
        }
    }
}

// csu.field is of type inner
function addNestedStructure(csu, inner, field)
{
    if (!(inner in structureParents))
        structureParents[inner] = [];

    // Skip fields that are really base classes, to avoid duplicating the base
    // fields; addBaseClass already added a "base-N" name.
    if (field.match(/^field:\d+$/) && (csu in baseClasses) && (baseClasses[csu].indexOf(inner) != -1))
        return;

    structureParents[inner].push([ csu, field ]);
}

function addBaseClass(csu, base) {
    if (!(csu in baseClasses))
        baseClasses[csu] = [];
    baseClasses[csu].push(base);
    if (!(base in subClasses))
        subClasses[base] = [];
    subClasses[base].push(csu);
    var k = baseClasses[csu].length;
    addNestedStructure(csu, base, `<base-${k}>`);
}

function addNestedPointer(csu, inner, field)
{
    if (!(inner in pointerParents))
        pointerParents[inner] = [];
    pointerParents[inner].push([ csu, field ]);
}

var xdb = xdbLibrary();
xdb.open("src_comp.xdb");

var minStream = xdb.min_data_stream();
var maxStream = xdb.max_data_stream();

for (var csuIndex = minStream; csuIndex <= maxStream; csuIndex++) {
    var csu = xdb.read_key(csuIndex);
    var data = xdb.read_entry(csu);
    var json = JSON.parse(data.readString());
    assert(json.length == 1);
    processCSU(csu.readString(), json[0]);

    xdb.free_string(csu);
    xdb.free_string(data);
}

for (const typename of extraRootedGCThings())
    typeInfo.RootedGCThings[typename] = true;

for (const typename of extraRootedPointers())
    typeInfo.RootedPointers[typename] = true;

// Everything that inherits from a "Rooted Base" is considered to be rooted.
// This is for things like CustomAutoRooter and its subclasses.
var basework = Object.keys(typeInfo.RootedBases);
while (basework.length) {
    const base = basework.pop();
    typeInfo.RootedPointers[base] = true;
    if (base in subClasses)
        basework.push(...subClasses[base]);
}

// Now that we have the whole hierarchy set up, add all the types and propagate
// info.
for (const csu of typeInfo.GCThings)
    addGCType(csu);
for (const csu of typeInfo.GCPointers)
    addGCPointer(csu);
for (const csu of typeInfo.GCInvalidated)
    addGCPointer(csu);

function parseTemplateType(typeName, validate=false) {
    // We only want templatized types. `Foo<U, T>::Member` doesn't count.
    // Foo<U, T>::Bar<X, Y> does count. Which turns out to be a simple rule:
    // check whether the type ends in '>'.
    if (!typeName.endsWith(">")) {
        return [typeName, undefined];
    }

    // "Tokenize" into angle brackets, commas, and everything else. We store
    // match objects as tokens because we'll need the string offset after we
    // finish grabbing the template parameters.
    const tokens = [];
    const tokenizer = /[<>,]|[^<>,]+/g;
    let match;
    while ((match = tokenizer.exec(typeName)) !== null) {
    	tokens.push(match);
    }

    // Walk backwards through the tokens, stopping when we find the matching
    // open bracket.
    const args = [];
    let depth = 0;
    let arg;
    let first_result;
    for (const match of tokens.reverse()) {
        const token = match[0];
        if (depth == 1 && (token == ',' || token == '<')) {
            // We've walked back to the beginning of a template parameter,
            // where we will see either a comma or open bracket.
            args.unshift(arg);
            arg = '';
        } else if (depth == 0 && token == '>') {
            arg = ''; // We just started.
        } else {
            arg = token + arg;
        }

        // Maintain the depth.
        if (token == '<') {
            // This could be bug 1728151.
            assert(depth > 0, `Invalid type: too many '<' signs in '${typeName}'`);
            depth--;
        } else if (token == '>') {
            depth++;
        }

        if (depth == 0) {
            // We've walked out of the template parameter list.
            // Record the results.
            assert(args.length > 0);
            const templateName = typeName.substr(0, match.index);
            const result = [templateName, args.map(arg => arg.trim())];
            if (!validate) {
                // Normal processing is to return the result the first time we
                // get to the '<' that matches the terminal '>', without validating
                // that the rest of the type name is balanced.
                return result;
            } else if (!first_result) {
                // If we are validating, remember the result when we hit the
                // first matching '<', but then keep processing the rest of
                // the input string to count brackets.
                first_result = result;
            }
        }
    }

    // This could be bug 1728151.
    assert(depth == 0, `Invalid type: too many '>' signs in '${typeName}'`);
    return first_result;
}

if (os.getenv("HAZARD_RUN_INTERNAL_TESTS")) {
    function check_parse(typeName, result) {
        assertEq(JSON.stringify(parseTemplateType(typeName)), JSON.stringify(result));
    }

    check_parse("int", ["int", undefined]);
    check_parse("Type<int>", ["Type", ["int"]]);
    check_parse("Container<int, double>", ["Container", ["int", "double"]]);
    check_parse("Container<Container<void, void>, double>", ["Container", ["Container<void, void>", "double"]]);
    check_parse("Foo<Bar<a,b>,Bar<a,b>>::Container<Container<void, void>, double>", ["Foo<Bar<a,b>,Bar<a,b>>::Container", ["Container<void, void>", "double"]]);
    check_parse("AlignedStorage2<TypedArray<foo>>", ["AlignedStorage2", ["TypedArray<foo>"]]);
    check_parse("mozilla::AlignedStorage2<mozilla::dom::TypedArray<unsigned char, JS::UnwrapArrayBufferMaybeShared, JS::GetArrayBufferMaybeSharedData, JS::GetArrayBufferMaybeSharedLengthAndData, JS::NewArrayBuffer> >",
        [
            "mozilla::AlignedStorage2",
            [
                "mozilla::dom::TypedArray<unsigned char, JS::UnwrapArrayBufferMaybeShared, JS::GetArrayBufferMaybeSharedData, JS::GetArrayBufferMaybeSharedLengthAndData, JS::NewArrayBuffer>"
            ]
        ]
    );
    check_parse(
        "mozilla::ArrayIterator<const mozilla::dom::binding_detail::RecordEntry<nsTString<char16_t>, mozilla::dom::Nullable<mozilla::dom::TypedArray<unsigned char, JS::UnwrapArrayBufferMaybeShared, JS::GetArrayBufferMaybeSharedData, JS::GetArrayBufferMaybeSharedLengthAndData, JS::NewArrayBuffer> > >&, nsTArray_Impl<mozilla::dom::binding_detail::RecordEntry<nsTString<char16_t>, mozilla::dom::Nullable<mozilla::dom::TypedArray<unsigned char, JS::UnwrapArrayBufferMaybeShared, JS::GetArrayBufferMaybeSharedData, JS::GetArrayBufferMaybeSharedLengthAndData, JS::NewArrayBuffer> > >, nsTArrayInfallibleAllocator> >",
        [
            "mozilla::ArrayIterator",
            [
                "const mozilla::dom::binding_detail::RecordEntry<nsTString<char16_t>, mozilla::dom::Nullable<mozilla::dom::TypedArray<unsigned char, JS::UnwrapArrayBufferMaybeShared, JS::GetArrayBufferMaybeSharedData, JS::GetArrayBufferMaybeSharedLengthAndData, JS::NewArrayBuffer> > >&",
                "nsTArray_Impl<mozilla::dom::binding_detail::RecordEntry<nsTString<char16_t>, mozilla::dom::Nullable<mozilla::dom::TypedArray<unsigned char, JS::UnwrapArrayBufferMaybeShared, JS::GetArrayBufferMaybeSharedData, JS::GetArrayBufferMaybeSharedLengthAndData, JS::NewArrayBuffer> > >, nsTArrayInfallibleAllocator>"
            ]
        ]
    );

    function check_throws(f, exc) {
        try {
            f();
        } catch (e) {
            assertEq(e.message.includes(exc), true, "incorrect exception: " + e.message);
            return;
        }
        assertEq(undefined, exc);
    }
    // Note that these need to end in '>' or the whole thing will be ignored.
    check_throws(() => parseTemplateType("foo>", true), "too many '>' signs");
    check_throws(() => parseTemplateType("foo<<>", true), "too many '<' signs");
    check_throws(() => parseTemplateType("foo<a::bar<a,b>", true), "too many '<' signs");
    check_throws(() => parseTemplateType("foo<a>*>::bar<a,b>", true), "too many '>' signs");
}

// GC Thing and GC Pointer annotations can be inherited from template args if
// this annotation is used. Think of Maybe<T> for example: Maybe<JSObject*> has
// the same GC rules as JSObject*.

var inheritors = Object.keys(typeInfo.InheritFromTemplateArgs).sort((a, b) => a.length - b.length);
for (const csu of inheritors) {
    const [templateName, templateArgs] = parseTemplateType(csu);
    for (const param of templateArgs) {
        const pos = param.search(/\**$/);
        const ptrdness = param.length - pos;
        const core_type = param.substr(0, pos);
        if (ptrdness == 0) {
            addToKeyedList(structureParents, core_type, [csu, "template-param-" + param]);
        } else if (ptrdness == 1) {
            addToKeyedList(pointerParents, core_type, [csu, "template-param-" + param]);
        }
    }
}

// "typeName is a (pointer to a)^'typePtrLevel' GC type because it contains a field
// named 'child' of type 'why' (or pointer to 'why' if fieldPtrLevel == 1), which is
// itself a GCThing or GCPointer."
function markGCType(typeName, child, why, typePtrLevel, fieldPtrLevel, indent)
{
    // Some types, like UniquePtr, do not mark/trace/relocate their contained
    // pointers and so should not hold them live across a GC. UniquePtr in
    // particular should be the only thing pointing to a structure containing a
    // GCPointer, so nothing else can possibly trace it and it'll die when the
    // UniquePtr goes out of scope. So we say that memory pointed to by a
    // UniquePtr is just as unsafe as the stack for storing GC pointers.
    if (!fieldPtrLevel && isUnsafeStorage(typeName)) {
        // The UniquePtr itself is on the stack but when you dereference the
        // contained pointer, you get to the unsafe memory that we are treating
        // as if it were the stack (aka ptrLevel 0). Note that
        // UniquePtr<UniquePtr<JSObject*>> is fine, so we don't want to just
        // hardcode the ptrLevel.
        fieldPtrLevel = -1;
    }

    // Example: with:
    //    struct Pair { JSObject* foo; int bar; };
    //    struct { Pair** info }***
    // make a call to:
    //    child='info' typePtrLevel=3 fieldPtrLevel=2
    // for a final ptrLevel of 5, used to later call:
    //    child='foo' typePtrLevel=5 fieldPtrLevel=1
    //
    var ptrLevel = typePtrLevel + fieldPtrLevel;

    // ...except when > 2 levels of pointers away from an actual GC thing, stop
    // searching the graph. (This would just be > 1, except that a UniquePtr
    // field might still have a GC pointer.)
    if (ptrLevel > 2)
        return;

    if (isRootedGCPointerTypeName(typeName) && !(typeName in typeInfo.RootedPointers))
        printErr("FIXME: use in-source annotation for " + typeName);

    if (ptrLevel == 0 && (typeName in typeInfo.RootedGCThings))
        return;
    if (ptrLevel == 1 && (isRootedGCPointerTypeName(typeName) || (typeName in typeInfo.RootedPointers)))
        return;

    if (ptrLevel == 0) {
        if (typeName in typeInfo.NonGCTypes)
            return;
        if (!(typeName in gcTypes))
            gcTypes[typeName] = new Set();
        gcTypes[typeName].add(why);
    } else if (ptrLevel == 1) {
        if (typeName in typeInfo.NonGCPointers)
            return;
        if (!(typeName in gcPointers))
            gcPointers[typeName] = new Set();
        gcPointers[typeName].add(why);
    }

    if (ptrLevel < 2) {
        if (!gcFields.has(typeName))
            gcFields.set(typeName, new Map());
        gcFields.get(typeName).set(child, [ why, fieldPtrLevel ]);
    }

    if (typeName in structureParents) {
        for (var field of structureParents[typeName]) {
            var [ holderType, fieldName ] = field;
            markGCType(holderType, fieldName, typeName, ptrLevel, 0, indent + "  ");
        }
    }
    if (typeName in pointerParents) {
        for (var field of pointerParents[typeName]) {
            var [ holderType, fieldName ] = field;
            markGCType(holderType, fieldName, typeName, ptrLevel, 1, indent + "  ");
        }
    }
}

function addGCType(typeName, child, why, depth, fieldPtrLevel)
{
    pendingGCTypes.push([typeName, '<annotation>', '(annotation)', 0, 0]);
}

function addGCPointer(typeName)
{
    pendingGCTypes.push([typeName, '<pointer-annotation>', '(annotation)', 1, 0]);
}

for (const pending of pendingGCTypes) {
    markGCType(...pending);
}

// Call a function for a type and every type that contains the type in a field
// or as a base class (which internally is pretty much the same thing --
// subclasses are structs beginning with the base class and adding on their
// local fields.)
function foreachContainingStruct(typeName, func, seen = new Set())
{
    function recurse(container, typeName) {
        if (seen.has(typeName))
            return;
        seen.add(typeName);

        func(container, typeName);

        if (typeName in subClasses) {
            for (const sub of subClasses[typeName])
                recurse("subclass of " + typeName, sub);
        }
        if (typeName in structureParents) {
            for (const [holder, field] of structureParents[typeName])
                recurse(field + " : " + typeName, holder);
        }
    }

    recurse('<annotation>', typeName);
}

for (var type of listNonGCPointers())
    typeInfo.NonGCPointers[type] = true;

function explain(csu, indent, seen) {
    if (!seen)
        seen = new Set();
    seen.add(csu);
    if (!gcFields.has(csu))
        return;
    var fields = gcFields.get(csu);

    if (fields.has('<annotation>')) {
        print(indent + "which is annotated as a GCThing");
        return;
    }
    if (fields.has('<pointer-annotation>')) {
        print(indent + "which is annotated as a GCPointer");
        return;
    }
    for (var [ field, [ child, ptrdness ] ] of fields) {
        var msg = indent;
        if (field[0] == '<')
            msg += "inherits from ";
        else {
            if (field.startsWith("template-param-")) {
                msg += "inherits annotations from template parameter '" + field.substr(15) + "' ";
            } else {
                msg += "contains field '" + field + "' ";
            }
            if (ptrdness == -1)
                msg += "(with a pointer to unsafe storage) holding a ";
            else if (ptrdness == 0)
                msg += "of type ";
            else
                msg += "pointing to type ";
        }
        msg += child;
        print(msg);
        if (!seen.has(child))
            explain(child, indent + "  ", seen);
    }
}

var origOut = os.file.redirect(options.gcTypes);

for (var csu in gcTypes) {
    print("GCThing: " + csu);
    explain(csu, "  ");
}
for (var csu in gcPointers) {
    print("GCPointer: " + csu);
    explain(csu, "  ");
}

// Redirect output to the typeInfo file and close the gcTypes file.
os.file.close(os.file.redirect(options.typeInfo));

// Compute the set of types that suppress GC within their RAII scopes (eg
// AutoSuppressGC, AutoSuppressGCForAnalysis).
var seen = new Set();
for (let csu in typeInfo.GCSuppressors)
    foreachContainingStruct(csu,
                            (holder, typeName) => { typeInfo.GCSuppressors[typeName] = holder },
                            seen);

print(JSON.stringify(typeInfo, null, 4));

os.file.close(os.file.redirect(origOut));