4 https://bugzilla.mozilla.org/show_bug.cgi?id=668855
8 <title>Test Cross-Compartment DOM WeakMaps
</title>
9 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css"/>
11 <script type=
"application/javascript">
12 /** Test for Bug
668855 **/
14 SimpleTest.waitForExplicitFinish();
16 // We wait to run this until the load event because it needs to access an element.
19 /* Create a weak reference, with a single-element weak map. */
20 let make_weak_ref = function (obj) {
26 /* Check to see if a weak reference is dead. */
27 let weak_ref_dead = function (r) {
28 return SpecialPowers.nondeterministicGetWeakMapKeys(r).length ==
0;
31 /* Deterministically grab an arbitrary DOM element. */
32 let get_live_dom = function () {
33 let elems = document.getElementsByTagName(
"a");
38 /* Test case from bug
653248, adapted into a standard test.
40 This is a dead cycle involving a DOM edge, so the cycle collector can free it. Keys and
41 values reachable only from XPConnect must be marked gray for this to work, and the cycle collector
42 must know the proper structure of the heap.
45 let make_gray_loop = function () {
46 let map = new WeakMap;
47 let div = document.createElement(
"div");
49 let obj = {m:map, k:key};
50 div.addEventListener(
"foo", function() {
51 // The code below doesn't matter (it won't run). Just pull a
56 //div.entrain = {m:map, k:key}; This is not sufficient to cause a leak in Fx9
58 return make_weak_ref(map);
61 let weakref = make_gray_loop();
64 /* Combinations of live and dead gray maps/keys. */
65 let basic_weak_ref = null;
66 let basic_map_weak_ref = null;
67 let black_map = new WeakMap;
70 let basic_unit_tests = function () {
71 let live_dom = get_live_dom();
72 let dead_dom = document.createElement(
"div");
73 let live_map = new WeakMap;
74 let dead_map = new WeakMap;
78 // put the live/dead maps/keys into the appropriate DOM elements
79 live_dom.basic_unit_tests = {m:live_map, k:live_key};
81 let obj = {m:dead_map, k:dead_key};
82 // dead_dom.hook = {m:dead_map, k:dead_key};
83 dead_dom.addEventListener(
"foo", function() {
84 // The code below doesn't matter (it won't run). Just pull a
90 // Create a dead value, and a weak ref to it.
91 // The loop keeps dead_dom alive unless the CC is smart enough to kill it.
92 let dead_val = {loop:dead_dom};
93 basic_weak_ref = make_weak_ref(dead_val);
94 basic_map_weak_ref = make_weak_ref(dead_map);
96 // set up the actual entries. most will die.
97 live_map.set(live_key, {my_key:'live_live'});
98 live_map.set(dead_key, dead_val);
99 live_map.set(black_key, {my_key:'live_black'});
101 dead_map.set(live_key, dead_val);
102 dead_map.set(dead_key, dead_val);
103 dead_map.set(black_key, dead_val);
105 black_map.set(live_key, {my_key:'black_live'});
106 black_map.set(dead_key, dead_val);
107 black_map.set(black_key, {my_key:'black_black'});
114 let check_basic_unit = function () {
115 let live_dom = get_live_dom();
116 let live_map = live_dom.basic_unit_tests.m;
117 let live_key = live_dom.basic_unit_tests.k;
119 // check the dead elements
120 ok(weak_ref_dead(basic_weak_ref),
"Dead value was kept alive.");
121 ok(weak_ref_dead(basic_map_weak_ref),
"Dead map was kept alive.");
123 // check the live gray map
124 is(live_map.get(live_key).my_key, 'live_live',
125 "Live key should have the same value in live map.");
126 is(live_map.get(black_key).my_key, 'live_black',
127 "Black key should have the same value in live map.");
128 is(SpecialPowers.nondeterministicGetWeakMapKeys(live_map).length,
2,
129 "Live map should have two entries.");
131 // check the live black map
132 is(black_map.get(live_key).my_key, 'black_live',
133 "Live key should have the same value in black map.");
134 is(black_map.get(black_key).my_key, 'black_black',
135 "Black key should have the same value in black map.");
136 is(SpecialPowers.nondeterministicGetWeakMapKeys(black_map).length,
2,
137 "Black map should have two entries.");
142 /* live gray chained weak map entries, involving the cycle collector. */
143 let chainm = new WeakMap;
146 let nested_cc_maps = function () {
147 let dom = get_live_dom();
148 for(let i =
0; i < num_chains; i++) {
151 dom0 = document.createElement(
"div");
152 chainm.set(k, {d:dom0});
153 dom = document.createElement(
"div");
154 dom0.appendChild(dom);
158 let check_nested_cc_maps = function () {
159 let dom = get_live_dom();
161 for(let i =
0; i < num_chains; i++) {
163 all_ok = all_ok && k.count == i;
164 dom = chainm.get(k).d.firstChild;
166 ok(all_ok,
"Count was invalid on a key in chained weak map entries.");
172 /* black weak map, chained garbage cycle involving DOM */
173 let garbage_map = new WeakMap;
175 let chained_garbage_maps = function () {
176 let dom0 = document.createElement(
"div");
178 for(let i =
0; i < num_chains; i++) {
181 let new_dom = document.createElement(
"div");
182 garbage_map.set(k, {val_child:new_dom});
183 dom = document.createElement(
"div");
184 new_dom.appendChild(dom);
187 dom.appendChild(dom0);
190 chained_garbage_maps();
193 /* black weak map, chained garbage cycle involving DOM, XPCWN keys */
194 let wn_garbage_map = new WeakMap;
196 let wn_chained_garbage_maps = function () {
197 let dom0 = document.createElement(
"div");
199 for(let i =
0; i < num_chains; i++) {
200 let new_dom = document.createElement(
"div");
201 wn_garbage_map.set(dom, {wn_val_child:new_dom});
202 dom = document.createElement(
"div");
203 new_dom.appendChild(dom);
206 dom.appendChild(dom0);
209 wn_chained_garbage_maps();
212 /* The cycle collector shouldn't remove a live wrapped native key. */
214 let wn_live_map = new WeakMap;
216 let make_live_map = function () {
217 let live = get_live_dom();
218 wn_live_map.set(live, {});
219 ok(wn_live_map.has(get_live_dom()),
"Live map should have live DOM node before GC.");
224 // We're out of ideas for unpreservable natives, now that just about
225 // everything is on webidl, so just don't test those.
227 /* set up for running precise GC/CC then checking the results */
229 SpecialPowers.exactGC(function () {
230 SpecialPowers.forceCC();
231 SpecialPowers.forceGC();
232 SpecialPowers.forceGC();
234 ok(weak_ref_dead(weakref),
"Garbage gray cycle should be collected.");
236 check_nested_cc_maps();
238 is(SpecialPowers.nondeterministicGetWeakMapKeys(garbage_map).length,
0,
"Chained garbage weak map entries should not leak.");
242 // fixed by Bug
680937
243 is(SpecialPowers.nondeterministicGetWeakMapKeys(wn_garbage_map).length,
0,
244 "Chained garbage WN weak map entries should not leak.");
246 // fixed by Bug
680937
247 is(SpecialPowers.nondeterministicGetWeakMapKeys(wn_live_map).length,
1,
248 "Live weak map wrapped native key should not be removed.");
250 ok(wn_live_map.has(get_live_dom()),
"Live map should have live dom.");
259 <div id=
"mydivname"></div>
260 <body onload=
"go()";
>
261 <a href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=668855" target=
"_blank">Mozilla Bug
668855</a>