summaryrefslogtreecommitdiff
path: root/js/src/jit-test/tests/wasm/table-gc.js
blob: 0cce3edf8567b473d3198b9d35c143402d77902d (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
// |jit-test| --no-baseline
// Turn off baseline and since it messes up the GC finalization assertions by
// adding spurious edges to the GC graph.

load(libdir + 'wasm.js');

const Module = WebAssembly.Module;
const Instance = WebAssembly.Instance;
const Table = WebAssembly.Table;
const RuntimeError = WebAssembly.RuntimeError;

var caller = `(type $v2i (func (result i32))) (func $call (param $i i32) (result i32) (call_indirect $v2i (get_local $i))) (export "call" $call)`
var callee = i => `(func $f${i} (type $v2i) (i32.const ${i}))`;

// A table should not hold exported functions alive and exported functions
// should not hold their originating table alive. Live exported functions should
// hold instances alive and instances hold imported tables alive. Nothing
// should hold the export object alive.
resetFinalizeCount();
var i = wasmEvalText(`(module (table 2 anyfunc) (export "tbl" table) (elem (i32.const 0) $f0) ${callee(0)} ${caller})`);
var e = i.exports;
var t = e.tbl;
var f = t.get(0);
assertEq(f(), e.call(0));
assertErrorMessage(() => e.call(1), RuntimeError, /indirect call to null/);
assertErrorMessage(() => e.call(2), RuntimeError, /index out of bounds/);
assertEq(finalizeCount(), 0);
i.edge = makeFinalizeObserver();
t.edge = makeFinalizeObserver();
f.edge = makeFinalizeObserver();
gc();
assertEq(finalizeCount(), 0);
f = null;
gc();
assertEq(finalizeCount(), 1);
f = t.get(0);
f.edge = makeFinalizeObserver();
gc();
assertEq(finalizeCount(), 1);
i.exports = null;
e = null;
gc();
assertEq(finalizeCount(), 1);
t = null;
gc();
assertEq(finalizeCount(), 1);
i = null;
gc();
assertEq(finalizeCount(), 1);
assertEq(f(), 0);
f = null;
gc();
assertEq(finalizeCount(), 4);

// A table should hold the instance of any of its elements alive.
resetFinalizeCount();
var i = wasmEvalText(`(module (table 1 anyfunc) (export "tbl" table) (elem (i32.const 0) $f0) ${callee(0)} ${caller})`);
var e = i.exports;
var t = e.tbl;
var f = t.get(0);
i.edge = makeFinalizeObserver();
t.edge = makeFinalizeObserver();
f.edge = makeFinalizeObserver();
gc();
assertEq(finalizeCount(), 0);
i.exports = null;
e = null;
gc();
assertEq(finalizeCount(), 0);
f = null;
gc();
assertEq(finalizeCount(), 1);
i = null;
gc();
assertEq(finalizeCount(), 1);
t = null;
gc();
assertEq(finalizeCount(), 3);

// Null elements shouldn't keep anything alive.
resetFinalizeCount();
var i = wasmEvalText(`(module (table 2 anyfunc) (export "tbl" table) ${caller})`);
var e = i.exports;
var t = e.tbl;
i.edge = makeFinalizeObserver();
t.edge = makeFinalizeObserver();
gc();
assertEq(finalizeCount(), 0);
i.exports = null;
e = null;
gc();
assertEq(finalizeCount(), 0);
i = null;
gc();
assertEq(finalizeCount(), 1);
t = null;
gc();
assertEq(finalizeCount(), 2);

// Before initialization, a table is not bound to any instance.
resetFinalizeCount();
var i = wasmEvalText(`(module (func $f0 (result i32) (i32.const 0)) (export "f0" $f0))`);
var t = new Table({initial:4, element:"anyfunc"});
i.edge = makeFinalizeObserver();
t.edge = makeFinalizeObserver();
gc();
assertEq(finalizeCount(), 0);
i = null;
gc();
assertEq(finalizeCount(), 1);
t = null;
gc();
assertEq(finalizeCount(), 2);

// When a Table is created (uninitialized) and then first assigned, it keeps the
// first element's Instance alive (as above).
resetFinalizeCount();
var i = wasmEvalText(`(module (func $f (result i32) (i32.const 42)) (export "f" $f))`);
var f = i.exports.f;
var t = new Table({initial:1, element:"anyfunc"});
i.edge = makeFinalizeObserver();
f.edge = makeFinalizeObserver();
t.edge = makeFinalizeObserver();
t.set(0, f);
assertEq(t.get(0), f);
assertEq(t.get(0)(), 42);
gc();
assertEq(finalizeCount(), 0);
f = null;
i.exports = null;
gc();
assertEq(finalizeCount(), 1);
assertEq(t.get(0)(), 42);
t.get(0).edge = makeFinalizeObserver();
gc();
assertEq(finalizeCount(), 2);
i = null;
gc();
assertEq(finalizeCount(), 2);
t.set(0, null);
assertEq(t.get(0), null);
gc();
assertEq(finalizeCount(), 3);
t = null;
gc();
assertEq(finalizeCount(), 4);

// Once all of an instance's elements in a Table have been clobbered, the
// Instance should not be reachable.
resetFinalizeCount();
var i1 = wasmEvalText(`(module (func $f1 (result i32) (i32.const 13)) (export "f1" $f1))`);
var i2 = wasmEvalText(`(module (func $f2 (result i32) (i32.const 42)) (export "f2" $f2))`);
var f1 = i1.exports.f1;
var f2 = i2.exports.f2;
var t = new Table({initial:2, element:"anyfunc"});
i1.edge = makeFinalizeObserver();
i2.edge = makeFinalizeObserver();
f1.edge = makeFinalizeObserver();
f2.edge = makeFinalizeObserver();
t.edge = makeFinalizeObserver();
t.set(0, f1);
t.set(1, f2);
gc();
assertEq(finalizeCount(), 0);
f1 = f2 = null;
i1.exports = null;
i2.exports = null;
gc();
assertEq(finalizeCount(), 2);
i1 = null;
i2 = null;
gc();
assertEq(finalizeCount(), 2);
t.set(0, t.get(1));
gc();
assertEq(finalizeCount(), 3);
t = null;
gc();
assertEq(finalizeCount(), 5);

// Ensure that an instance that is only live on the stack cannot be GC even if
// there are no outstanding references.
resetFinalizeCount();
const N = 10;
var tbl = new Table({initial:N, element:"anyfunc"});
tbl.edge = makeFinalizeObserver();
function runTest() {
    tbl = null;
    gc();
    assertEq(finalizeCount(), 0);
    return 100;
}
var i = wasmEvalText(
    `(module
        (import $imp "a" "b" (result i32))
        (func $f (param i32) (result i32) (call $imp))
        (export "f" $f)
    )`,
    {a:{b:runTest}}
);
i.edge = makeFinalizeObserver();
tbl.set(0, i.exports.f);
var m = new Module(wasmTextToBinary(`(module
    (import "a" "b" (table ${N} anyfunc))
    (type $i2i (func (param i32) (result i32)))
    (func $f (param $i i32) (result i32)
        (set_local $i (i32.sub (get_local $i) (i32.const 1)))
        (i32.add
            (i32.const 1)
            (call_indirect $i2i (get_local $i) (get_local $i))))
    (export "f" $f)
)`));
for (var i = 1; i < N; i++) {
    var inst = new Instance(m, {a:{b:tbl}});
    inst.edge = makeFinalizeObserver();
    tbl.set(i, inst.exports.f);
}
inst = null;
assertEq(tbl.get(N - 1)(N - 1), 109);
gc();
assertEq(finalizeCount(), N + 1);