Debian Codegraph @

js/src/jit-test/tests/gc/weak-marking-03.js

raw
1 // |jit-test| allow-unhandlable-oom
2
3 // weakmap marking tests that use the testing mark queue to force an ordering
4 // of marking.
5
6 // We are carefully controlling the sequence of GC events.
7 gczeal(0);
8
9 // If a command-line parameter is given, use it as a substring restriction on
10 // the tests to run.
11 var testRestriction = scriptArgs[0];
12 printErr(`testRestriction is ${testRestriction || '(run all tests)'}`);
13
14 function runtest(func) {
15 if (testRestriction && ! func.name.includes(testRestriction)) {
16 print("\Skipping " + func.name);
17 } else {
18 print("\nRunning " + func.name);
19 func();
20 }
21 }
22
23 function reportMarks(prefix = "") {
24 const marks = getMarks();
25 const current = currentgc();
26 const markstr = marks.join("/");
27 print(`${prefix}[${current.incrementalState}/${current.sweepGroup}@${current.queuePos}] ${markstr}`);
28 return markstr;
29 }
30
31 function startGCMarking() {
32 startgc(100000);
33 while (gcstate() === "Prepare" || gcstate() === "MarkRoots") {
34 gcslice(100000);
35 }
36 }
37
38 function purgeKey() {
39 const m = new WeakMap();
40 const vals = {};
41 vals.key = Object.create(null);
42 vals.val = Object.create(null);
43 m.set(vals.key, vals.val);
44
45 minorgc();
46
47 addMarkObservers([m, vals.key, vals.val]);
48
49 enqueueMark(m);
50 enqueueMark("yield");
51
52 enqueueMark(vals.key);
53 enqueueMark("yield");
54
55 vals.key = vals.val = null;
56
57 startGCMarking();
58 // getMarks() returns map/key/value
59 assertEq(getMarks().join("/"), "black/unmarked/unmarked",
60 "marked the map black");
61
62 gcslice(100000);
63 assertEq(getMarks().join("/"), "black/black/unmarked",
64 "key is now marked");
65
66 // Trigger purgeWeakKey: the key is in weakkeys (because it was unmarked when
67 // the map was marked), and now we're removing it.
68 m.delete(nondeterministicGetWeakMapKeys(m)[0]);
69
70 finishgc(); // Finish the GC
71 assertEq(getMarks().join("/"), "black/black/black",
72 "at end, value is marked too");
73
74 clearMarkQueue();
75 clearMarkObservers();
76 }
77
78 if (this.enqueueMark)
79 runtest(purgeKey);
80
81 function removeKey() {
82 reportMarks("removeKey start: ");
83
84 const m = new WeakMap();
85 const vals = {};
86 vals.key = Object.create(null);
87 vals.val = Object.create(null);
88 m.set(vals.key, vals.val);
89
90 minorgc();
91
92 addMarkObservers([m, vals.key, vals.val]);
93
94 enqueueMark(m);
95 enqueueMark("yield");
96
97 startGCMarking();
98 reportMarks("first: ");
99 var marks = getMarks();
100 assertEq(marks[0], "black", "map is black");
101 assertEq(marks[1], "unmarked", "key not marked yet");
102 assertEq(marks[2], "unmarked", "value not marked yet");
103 m.delete(vals.key);
104
105 finishgc(); // Finish the GC
106 reportMarks("done: ");
107 marks = getMarks();
108 assertEq(marks[0], "black", "map is black");
109 assertEq(marks[1], "black", "key is black");
110 assertEq(marks[2], "black", "value is black");
111
112 // Do it again, but this time, remove all other roots.
113 m.set(vals.key, vals.val);
114 vals.key = vals.val = null;
115 startGCMarking();
116 marks = getMarks();
117 assertEq(marks[0], "black", "map is black");
118 assertEq(marks[1], "unmarked", "key not marked yet");
119 assertEq(marks[2], "unmarked", "value not marked yet");
120
121 // This was meant to test the weakmap deletion barrier, which would remove
122 // the key from weakkeys. Unfortunately, JS-exposed WeakMaps now have a read
123 // barrier on lookup that marks the key, and deletion requires a lookup.
124 m.delete(nondeterministicGetWeakMapKeys(m)[0]);
125
126 finishgc();
127 marks = getMarks();
128 assertEq(marks[0], "black", "map is black");
129 assertEq(marks[1], "black", "key was blackened by lookup read barrier during deletion");
130 assertEq(marks[2], "black", "value is black because map and key are black");
131
132 clearMarkQueue();
133 clearMarkObservers();
134 }
135
136 if (this.enqueueMark)
137 runtest(removeKey);
138
139 // Test:
140 // 1. mark the map
141 // - that inserts the delegate into weakKeys
142 // 2. nuke the CCW key
143 // - removes the delegate from weakKeys
144 // 3. mark the key
145 // 4. enter weak marking mode
146 //
147 // The problem it's attempting to recreate is that entering weak marking mode
148 // will no longer mark the value, because there's no delegate to trigger it,
149 // and the key was not added to weakKeys (because at the time the map was
150 // scanned, the key had a delegate, so it was added to weakKeys instead.)
151 function nukeMarking() {
152 const g1 = newGlobal({newCompartment: true});
153
154 const vals = {};
155 vals.map = new WeakMap();
156 vals.key = g1.eval("Object.create(null)");
157 vals.val = Object.create(null);
158 vals.map.set(vals.key, vals.val);
159 vals.val = null;
160 gc();
161
162 // Set up the sequence of marking events.
163 enqueueMark(vals.map);
164 enqueueMark("yield");
165 // We will nuke the key's delegate here.
166 enqueueMark(vals.key);
167 enqueueMark("enter-weak-marking-mode");
168
169 // Okay, run through the GC now.
170 startgc(1000000);
171 while (gcstate() === "Prepare" || gcstate() === "MarkRoots") {
172 gcslice(100000);
173 }
174 assertEq(gcstate(), "Mark", "expected to yield after marking map");
175 // We should have marked the map and then yielded back here.
176 nukeCCW(vals.key);
177 // Finish up the GC.
178 gcslice();
179
180 clearMarkQueue();
181 }
182
183 if (this.enqueueMark)
184 runtest(nukeMarking);
185
186 // Similar to the above, but trying to get a different failure:
187 // - start marking
188 // - find a map, add its key to ephemeronEdges
189 // - nuke the key (and all other CCWs between the key -> delegate zones)
190 // - when sweeping, we will no longer have any edges between the key
191 // and delegate zones. So they will be placed in separate sweep groups.
192 // - for this test, the delegate zone must be swept after the key zone
193 // - make sure we don't try to mark back in the key zone (due to an
194 // ephemeron edge) while sweeping the delegate zone. In a DEBUG build,
195 // this would assert.
196 function nukeMarkingSweepGroups() {
197 // Create g1 before host, because that will result in the right zone
198 // ordering to trigger the bug.
199 const g1 = newGlobal({newCompartment: true});
200 const host = newGlobal({newCompartment: true});
201 host.g1 = g1;
202 host.eval(`
203 const vals = {};
204 vals.map = new WeakMap();
205 vals.key = g1.eval("Object.create(null)");
206 vals.val = Object.create(null);
207 vals.map.set(vals.key, vals.val);
208 vals.val = null;
209 gc();
210
211 // Set up the sequence of marking events.
212 enqueueMark(vals.map);
213 enqueueMark("yield");
214 // We will nuke the key's delegate here.
215 enqueueMark(vals.key);
216 enqueueMark("enter-weak-marking-mode");
217
218 // Okay, run through the GC now.
219 startgc(1);
220 while (gcstate() !== "Mark") {
221 gcslice(100000);
222 }
223 assertEq(gcstate(), "Mark", "expected to yield after marking map");
224 // We should have marked the map and then yielded back here.
225 nukeAllCCWs();
226 // Finish up the GC.
227 while (gcstate() === "Mark") {
228 gcslice(1000);
229 }
230 gcslice();
231
232 clearMarkQueue();
233 `);
234 }
235
236 if (this.enqueueMark)
237 runtest(nukeMarkingSweepGroups);
238
239 function transplantMarking() {
240 const g1 = newGlobal({newCompartment: true});
241
242 const vals = {};
243 vals.map = new WeakMap();
244 let {object, transplant} = transplantableObject();
245 vals.key = object;
246 object = null;
247 vals.val = Object.create(null);
248 vals.map.set(vals.key, vals.val);
249 vals.val = null;
250 gc();
251
252 // Set up the sequence of marking events.
253 enqueueMark(vals.map);
254 enqueueMark("yield");
255 // We will transplant the key here.
256 enqueueMark(vals.key);
257 enqueueMark("enter-weak-marking-mode");
258
259 // Okay, run through the GC now.
260 startgc(1000000);
261 while (gcstate() !== "Mark") {
262 gcslice(100000);
263 }
264 assertEq(gcstate(), "Mark", "expected to yield after marking map");
265 // We should have marked the map and then yielded back here.
266 transplant(g1);
267 // Finish up the GC.
268 gcslice();
269
270 clearMarkQueue();
271 }
272
273 if (this.enqueueMark)
274 runtest(transplantMarking);
275
276 // 1. Mark the map
277 // => add delegate to weakKeys
278 // 2. Mark the delegate black
279 // => do nothing because we are not in weak marking mode
280 // 3. Mark the key gray
281 // => mark value gray, not that we really care
282 // 4. Enter weak marking mode
283 // => black delegate darkens the key from gray to black
284 function grayMarkingMapFirst() {
285 const g = newGlobal({newCompartment: true});
286 const vals = {};
287 vals.map = new WeakMap();
288 vals.key = g.eval("Object.create(null)");
289 vals.val = Object.create(null);
290 vals.map.set(vals.key, vals.val);
291
292 g.delegate = vals.key;
293 g.eval("dummy = Object.create(null)");
294 g.eval("grayRoot().push(delegate, dummy)");
295 addMarkObservers([vals.map, vals.key]);
296 g.addMarkObservers([vals.key, g.dummy]);
297 addMarkObservers([vals.val]);
298
299 gc();
300
301 enqueueMark(vals.map);
302 enqueueMark("yield"); // checkpoint 1
303
304 g.enqueueMark(vals.key);
305 enqueueMark("yield"); // checkpoint 2
306
307 vals.val = null;
308 vals.key = null;
309 g.delegate = null;
310 g.dummy = null;
311
312 const showmarks = () => {
313 print("[map,key,delegate,graydummy,value] marked " + JSON.stringify(getMarks()));
314 };
315
316 print("Starting incremental GC");
317 startGCMarking();
318 // Checkpoint 1, after marking map
319 showmarks();
320 var marks = getMarks();
321 assertEq(marks[0], "black", "map is black");
322 assertEq(marks[1], "unmarked", "key is not marked yet");
323 assertEq(marks[2], "unmarked", "delegate is not marked yet");
324
325 gcslice(100000);
326 // Checkpoint 2, after marking delegate
327 showmarks();
328 marks = getMarks();
329 assertEq(marks[0], "black", "map is black");
330 assertEq(marks[1], "unmarked", "key is not marked yet");
331 assertEq(marks[2], "black", "delegate is black");
332
333 gcslice();
334 // GC complete. Key was marked black (b/c of delegate), then gray marking saw
335 // it was already black and skipped it.
336 showmarks();
337 marks = getMarks();
338 assertEq(marks[0], "black", "map is black");
339 assertEq(marks[1], "black", "delegate marked key black because of weakmap");
340 assertEq(marks[2], "black", "delegate is still black");
341 assertEq(marks[3], "gray", "basic gray marking is working");
342 assertEq(marks[4], "black", "black map + black delegate => black value");
343
344 clearMarkQueue();
345 clearMarkObservers();
346 grayRoot().length = 0;
347 g.eval("grayRoot().length = 0");
348 }
349
350 if (this.enqueueMark)
351 runtest(grayMarkingMapFirst);
352
353 function grayMarkingMapLast() {
354 const g = newGlobal({newCompartment: true});
355 const vals = {};
356 vals.map = new WeakMap();
357 vals.key = g.eval("Object.create(null)");
358 vals.val = Object.create(null);
359 vals.map.set(vals.key, vals.val);
360
361 vals.map2 = new WeakMap();
362 vals.key2 = g.eval("Object.create(null)");
363 vals.val2 = Object.create(null);
364 vals.map2.set(vals.key2, vals.val2);
365
366 g.delegate = vals.key;
367 g.eval("grayRoot().push(delegate)");
368 addMarkObservers([vals.map, vals.key]);
369 g.addMarkObservers([vals.key]);
370 addMarkObservers([vals.val]);
371
372 grayRoot().push(vals.key2);
373 addMarkObservers([vals.map2, vals.key2]);
374 g.addMarkObservers([vals.key2]);
375 addMarkObservers([vals.val2]);
376
377 const labels = ["map", "key", "delegate", "value", "map2", "key2", "delegate2", "value2"];
378
379 gc();
380
381 g.enqueueMark(vals.key);
382 g.enqueueMark(vals.key2);
383 enqueueMark("yield"); // checkpoint 1
384
385 vals.val = null;
386 vals.key = null;
387 g.delegate = null;
388
389 vals.map2 = null; // Important! Second map is never marked, keeps nothing alive.
390 vals.key2 = null;
391 vals.val2 = null;
392 g.delegate2 = null;
393
394 const labeledMarks = () => {
395 const info = {};
396 const marks = getMarks();
397 for (let i = 0; i < labels.length; i++)
398 info[labels[i]] = marks[i];
399 return info;
400 };
401
402 const showmarks = () => {
403 print("Marks:");
404 for (const [label, mark] of Object.entries(labeledMarks()))
405 print(` ${label}: ${mark}`);
406 };
407
408 print("Starting incremental GC");
409 startGCMarking();
410 // Checkpoint 1, after marking key
411 showmarks();
412 var marks = labeledMarks();
413 assertEq(marks.map, "unmarked", "map is unmarked");
414 assertEq(marks.key, "unmarked", "key is not marked yet");
415 assertEq(marks.delegate, "black", "delegate is black");
416 assertEq(marks.map2, "unmarked", "map2 is unmarked");
417 assertEq(marks.key2, "unmarked", "key2 is not marked yet");
418 assertEq(marks.delegate2, "black", "delegate2 is black");
419
420 gcslice();
421 // GC complete. When entering weak marking mode, black delegate propagated to
422 // key.
423 showmarks();
424 marks = labeledMarks();
425 assertEq(marks.map, "black", "map is black");
426 assertEq(marks.key, "black", "delegate marked key black because of weakmap");
427 assertEq(marks.delegate, "black", "delegate is still black");
428 assertEq(marks.value, "black", "black map + black delegate => black value");
429 assertEq(marks.map2, "dead", "map2 is dead");
430 assertEq(marks.key2, "gray", "key2 marked gray, map2 had no effect");
431 assertEq(marks.delegate2, "black", "delegate artificially marked black via mark queue");
432 assertEq(marks.value2, "dead", "dead map + black delegate => dead value");
433
434 clearMarkQueue();
435 clearMarkObservers();
436 grayRoot().length = 0;
437 g.eval("grayRoot().length = 0");
438
439 return vals; // To prevent the JIT from optimizing out vals.
440 }
441
442 if (this.enqueueMark)
443 runtest(grayMarkingMapLast);
444
445 function grayMapKey() {
446 const vals = {};
447 vals.m = new WeakMap();
448 vals.key = Object.create(null);
449 vals.val = Object.create(null);
450 vals.m.set(vals.key, vals.val);
451
452 // Maps are allocated black, so we won't be able to mark it gray during the
453 // first GC.
454 gc();
455
456 addMarkObservers([vals.m, vals.key, vals.val]);
457
458 // Wait until we can mark gray (ie, sweeping). Mark the map gray and yield.
459 // This should happen all in one slice.
460 enqueueMark("set-color-gray");
461 enqueueMark(vals.m);
462 enqueueMark("unset-color");
463 enqueueMark("yield");
464
465 // Make the weakmap no longer reachable from the roots, so we can mark it
466 // gray.
467 vals.m = null;
468
469 enqueueMark(vals.key);
470 enqueueMark("yield");
471
472 vals.key = vals.val = null;
473
474 startGCMarking();
475 assertEq(getMarks().join("/"), "gray/unmarked/unmarked",
476 "marked the map gray");
477
478 gcslice(100000);
479 assertEq(getMarks().join("/"), "gray/black/unmarked",
480 "key is now marked black");
481
482 finishgc(); // Finish the GC
483
484 assertEq(getMarks().join("/"), "gray/black/gray",
485 "at end: black/gray => gray");
486
487 clearMarkQueue();
488 clearMarkObservers();
489 }
490
491 if (this.enqueueMark)
492 runtest(grayMapKey);
493
494 function grayKeyMap() {
495 const vals = {};
496 vals.m = new WeakMap();
497 vals.key = Object.create(null);
498 vals.val = Object.create(null);
499 vals.m.set(vals.key, vals.val);
500
501 addMarkObservers([vals.m, vals.key, vals.val]);
502
503 enqueueMark(vals.key);
504 enqueueMark("yield");
505
506 // Wait until we are gray marking.
507 enqueueMark("set-color-gray");
508 enqueueMark(vals.m);
509 enqueueMark("unset-color");
510 enqueueMark("yield");
511
512 enqueueMark("set-color-black");
513 enqueueMark(vals.m);
514 enqueueMark("unset-color");
515
516 // Make the weakmap no longer reachable from the roots, so we can mark it
517 // gray.
518 vals.m = null;
519
520 vals.key = vals.val = null;
521
522 // Only mark this zone, to avoid interference from other tests that may have
523 // created additional zones.
524 schedulezone(vals);
525
526 startGCMarking();
527 // getMarks() returns map/key/value
528 reportMarks("1: ");
529 assertEq(getMarks().join("/"), "unmarked/black/unmarked",
530 "marked key black");
531
532 // We always yield before sweeping (in the absence of zeal), so we will see
533 // the unmarked state another time.
534 gcslice(100000);
535 reportMarks("2: ");
536 assertEq(getMarks().join("/"), "unmarked/black/unmarked",
537 "marked key black, yield before sweeping");
538
539 gcslice(100000);
540 reportMarks("3: ");
541 assertEq(getMarks().join("/"), "gray/black/gray",
542 "marked the map gray, which marked the value when map scanned");
543
544 finishgc(); // Finish the GC
545 reportMarks("4: ");
546 assertEq(getMarks().join("/"), "black/black/black",
547 "further marked the map black, so value should also be blackened");
548
549 clearMarkQueue();
550 clearMarkObservers();
551 }
552
553 if (this.enqueueMark)
554 runtest(grayKeyMap);
555
556 // Cause a key to be marked black *during gray marking*, by first marking a
557 // delegate black, then marking the map and key gray. When the key is scanned,
558 // it should be seen to be a CCW of a black delegate and so should itself be
559 // marked black.
560 //
561 // The bad behavior being prevented is:
562 //
563 // 1. You wrap an object in a CCW and use it as a weakmap key to some
564 // information.
565 // 2. You keep a strong reference to the object (in its compartment).
566 // 3. The only references to the CCW are gray, and are in fact part of a cycle.
567 // 4. The CC runs and discards the CCW.
568 // 5. You look up the object in the weakmap again. This creates a new wrapper
569 // to use as a key. It is not in the weakmap, so the information you stored
570 // before is not found. (It may have even been collected, if you had no
571 // other references to it.)
572 //
573 function blackDuringGray() {
574 const g = newGlobal({newCompartment: true});
575 const vals = {};
576 vals.map = new WeakMap();
577 vals.key = g.eval("Object.create(null)");
578 vals.val = Object.create(null);
579 vals.map.set(vals.key, vals.val);
580
581 g.delegate = vals.key;
582 addMarkObservers([vals.map, vals.key]);
583 g.addMarkObservers([vals.key]);
584 addMarkObservers([vals.val]);
585 // Mark observers: map, key, delegate, value
586
587 gc();
588
589 g.enqueueMark(vals.key); // Mark the delegate black
590 enqueueMark("yield"); // checkpoint 1
591
592 // Mark the map gray. This will scan through all entries, find our key, and
593 // mark it black because its delegate is black.
594 enqueueMark("set-color-gray");
595 enqueueMark(vals.map); // Mark the map gray
596
597 vals.map = null;
598 vals.val = null;
599 vals.key = null;
600 g.delegate = null;
601
602 const showmarks = () => {
603 print("[map,key,delegate,value] marked " + JSON.stringify(getMarks()));
604 };
605
606 print("Starting incremental GC");
607 startGCMarking();
608 // Checkpoint 1, after marking delegate black
609 showmarks();
610 var marks = getMarks();
611 assertEq(marks[0], "unmarked", "map is not marked yet");
612 assertEq(marks[1], "unmarked", "key is not marked yet");
613 assertEq(marks[2], "black", "delegate is black");
614 assertEq(marks[3], "unmarked", "values is not marked yet");
615
616 finishgc();
617 showmarks();
618 marks = getMarks();
619 assertEq(marks[0], "gray", "map is gray");
620 assertEq(marks[1], "gray", "gray map + black delegate should mark key gray");
621 assertEq(marks[2], "black", "delegate is still black");
622 assertEq(marks[3], "gray", "gray map + gray key => gray value");
623
624 clearMarkQueue();
625 clearMarkObservers();
626 grayRoot().length = 0;
627 g.eval("grayRoot().length = 0");
628 }
629
630 if (this.enqueueMark)
631 runtest(blackDuringGray);
632
633 // Same as above, except relying on the implicit edge from delegate -> key.
634 function blackDuringGrayImplicit() {
635 const g = newGlobal({newCompartment: true});
636 const vals = {};
637 vals.map = new WeakMap();
638 vals.key = g.eval("Object.create(null)");
639 vals.val = Object.create(null);
640 vals.map.set(vals.key, vals.val);
641
642 g.delegate = vals.key;
643 addMarkObservers([vals.map, vals.key]);
644 g.addMarkObservers([vals.key]);
645 addMarkObservers([vals.val]);
646 // Mark observers: map, key, delegate, value
647
648 gc();
649
650 // Mark the map gray. This will scan through all entries, find our key, and
651 // add implicit edges from delegate -> key and delegate -> value.
652 enqueueMark("set-color-gray");
653 enqueueMark(vals.map); // Mark the map gray
654 enqueueMark("yield"); // checkpoint 1
655
656 enqueueMark("set-color-black");
657 g.enqueueMark(vals.key); // Mark the delegate black, propagating to key.
658
659 vals.map = null;
660 vals.val = null;
661 vals.key = null;
662 g.delegate = null;
663
664 const showmarks = () => {
665 print("[map,key,delegate,value] marked " + JSON.stringify(getMarks()));
666 };
667
668 print("Starting incremental GC");
669 startGCMarking();
670 // Checkpoint 1, after marking map gray
671 showmarks();
672 var marks = getMarks();
673 assertEq(marks[0], "gray", "map is gray");
674 assertEq(marks[1], "unmarked", "key is not marked yet");
675 assertEq(marks[2], "unmarked", "delegate is not marked yet");
676 assertEq(marks[3], "unmarked", "value is not marked yet");
677
678 finishgc();
679 showmarks();
680 marks = getMarks();
681 assertEq(marks[0], "gray", "map is gray");
682 assertEq(marks[1], "gray", "gray map + black delegate should mark key gray");
683 assertEq(marks[2], "black", "delegate is black");
684 assertEq(marks[3], "gray", "gray map + gray key => gray value via delegate -> value");
685
686 clearMarkQueue();
687 clearMarkObservers();
688 grayRoot().length = 0;
689 g.eval("grayRoot().length = 0");
690 }
691
692 if (this.enqueueMark)
693 runtest(blackDuringGrayImplicit);