js/xpconnect/tests/chrome/test_weakmaps.xul

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
michael@0 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=668855
michael@0 6 -->
michael@0 7 <window title="Mozilla Bug "
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
michael@0 10
michael@0 11 <!-- test results are displayed in the html:body -->
michael@0 12 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 13 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id="
michael@0 14 target="_blank">Mozilla Bug 668855</a>
michael@0 15 </body>
michael@0 16
michael@0 17 <!-- test code goes here -->
michael@0 18 <script type="application/javascript">
michael@0 19 <![CDATA[
michael@0 20 /** Test for Bug 668855 **/
michael@0 21
michael@0 22 let Cu = Components.utils;
michael@0 23 let Ci = Components.interfaces;
michael@0 24
michael@0 25 /* Create a weak reference, with a single-element weak map. */
michael@0 26 let make_weak_ref = function (obj) {
michael@0 27 let m = new WeakMap;
michael@0 28 m.set(obj, {});
michael@0 29 return m;
michael@0 30 };
michael@0 31
michael@0 32 /* Check to see if a weak reference is dead. */
michael@0 33 let weak_ref_dead = function (r) {
michael@0 34 return Cu.nondeterministicGetWeakMapKeys(r).length == 0;
michael@0 35 }
michael@0 36
michael@0 37 /* Deterministically grab an arbitrary DOM element. */
michael@0 38 let get_live_dom = function () {
michael@0 39 let elems = document.getElementsByTagName("a");
michael@0 40 return elems[0];
michael@0 41 };
michael@0 42
michael@0 43
michael@0 44 /* Test case from bug 653248, adapted into a standard test.
michael@0 45
michael@0 46 This is a dead cycle involving a DOM edge, so the cycle collector can free it. Keys and
michael@0 47 values reachable only from XPConnect must be marked gray for this to work, and the cycle collector
michael@0 48 must know the proper structure of the heap.
michael@0 49
michael@0 50 */
michael@0 51 let make_gray_loop = function () {
michael@0 52 let map = new WeakMap;
michael@0 53 let div = document.createElement("div");
michael@0 54 let key = {};
michael@0 55 div.setUserData("entrain", {m:map, k:key}, null);
michael@0 56 //div.entrain = {m:map, k:key}; This is not sufficient to cause a leak in Fx9
michael@0 57 map.set(key, div);
michael@0 58 return make_weak_ref(map);
michael@0 59 };
michael@0 60
michael@0 61 let weakref = make_gray_loop();
michael@0 62
michael@0 63
michael@0 64 /* Combinations of live and dead gray maps/keys. */
michael@0 65 let basic_weak_ref = null;
michael@0 66 let basic_map_weak_ref = null;
michael@0 67 let black_map = new WeakMap;
michael@0 68 let black_key = {};
michael@0 69
michael@0 70 let basic_unit_tests = function () {
michael@0 71 let live_dom = get_live_dom();
michael@0 72 let dead_dom = document.createElement("div");
michael@0 73 let live_map = new WeakMap;
michael@0 74 let dead_map = new WeakMap;
michael@0 75 let live_key = {};
michael@0 76 let dead_key = {};
michael@0 77
michael@0 78 // put the live/dead maps/keys into the appropriate DOM elements
michael@0 79 live_dom.basic_unit_tests = {m:live_map, k:live_key};
michael@0 80 dead_dom.setUserData("hook", {m:dead_map, k:dead_key}, null);
michael@0 81 // dead_dom.hook = {m:dead_map, k:dead_key};
michael@0 82
michael@0 83 // Create a dead value, and a weak ref to it.
michael@0 84 // The loop keeps dead_dom alive unless the CC is smart enough to kill it.
michael@0 85 let dead_val = {loop:dead_dom};
michael@0 86 basic_weak_ref = make_weak_ref(dead_val);
michael@0 87 basic_map_weak_ref = make_weak_ref(dead_map);
michael@0 88
michael@0 89 // set up the actual entries. most will die.
michael@0 90 live_map.set(live_key, {my_key:'live_live'});
michael@0 91 live_map.set(dead_key, dead_val);
michael@0 92 live_map.set(black_key, {my_key:'live_black'});
michael@0 93
michael@0 94 dead_map.set(live_key, dead_val);
michael@0 95 dead_map.set(dead_key, dead_val);
michael@0 96 dead_map.set(black_key, dead_val);
michael@0 97
michael@0 98 black_map.set(live_key, {my_key:'black_live'});
michael@0 99 black_map.set(dead_key, dead_val);
michael@0 100 black_map.set(black_key, {my_key:'black_black'});
michael@0 101
michael@0 102 };
michael@0 103
michael@0 104 basic_unit_tests();
michael@0 105
michael@0 106
michael@0 107 let check_basic_unit = function () {
michael@0 108 let live_dom = get_live_dom();
michael@0 109 let live_map = live_dom.basic_unit_tests.m;
michael@0 110 let live_key = live_dom.basic_unit_tests.k;
michael@0 111
michael@0 112 // check the dead elements
michael@0 113 ok(weak_ref_dead(basic_weak_ref), "Dead value was kept alive.");
michael@0 114 ok(weak_ref_dead(basic_map_weak_ref), "Dead map was kept alive.");
michael@0 115
michael@0 116 // check the live gray map
michael@0 117 is(live_map.get(live_key).my_key, 'live_live',
michael@0 118 "Live key should have the same value in live map.");
michael@0 119 is(live_map.get(black_key).my_key, 'live_black',
michael@0 120 "Black key should have the same value in live map.");
michael@0 121 is(Cu.nondeterministicGetWeakMapKeys(live_map).length, 2,
michael@0 122 "Live map should have two entries.");
michael@0 123
michael@0 124 // check the live black map
michael@0 125 is(black_map.get(live_key).my_key, 'black_live',
michael@0 126 "Live key should have the same value in black map.");
michael@0 127 is(black_map.get(black_key).my_key, 'black_black',
michael@0 128 "Black key should have the same value in black map.");
michael@0 129 is(Cu.nondeterministicGetWeakMapKeys(black_map).length, 2,
michael@0 130 "Black map should have two entries.");
michael@0 131
michael@0 132 };
michael@0 133
michael@0 134
michael@0 135 /* live gray chained weak map entries, involving the cycle collector. */
michael@0 136 let chainm = new WeakMap;
michael@0 137 let num_chains = 5;
michael@0 138
michael@0 139 let nested_cc_maps = function () {
michael@0 140 let dom = get_live_dom();
michael@0 141 for(let i = 0; i < num_chains; i++) {
michael@0 142 let k = {count:i};
michael@0 143 dom.key = k;
michael@0 144 dom0 = document.createElement("div");
michael@0 145 chainm.set(k, {d:dom0});
michael@0 146 dom = document.createElement("div");
michael@0 147 dom0.appendChild(dom);
michael@0 148 };
michael@0 149 };
michael@0 150
michael@0 151 let check_nested_cc_maps = function () {
michael@0 152 let dom = get_live_dom();
michael@0 153 let all_ok = true;
michael@0 154 for(let i = 0; i < num_chains; i++) {
michael@0 155 let k = dom.key;
michael@0 156 all_ok = all_ok && k.count == i;
michael@0 157 dom = chainm.get(k).d.firstChild;
michael@0 158 };
michael@0 159 ok(all_ok, "Count was invalid on a key in chained weak map entries.");
michael@0 160 };
michael@0 161
michael@0 162 nested_cc_maps();
michael@0 163
michael@0 164
michael@0 165 /* black weak map, chained garbage cycle involving DOM */
michael@0 166 let garbage_map = new WeakMap;
michael@0 167
michael@0 168 let chained_garbage_maps = function () {
michael@0 169 let dom0 = document.createElement("div");
michael@0 170 let dom = dom0;
michael@0 171 for(let i = 0; i < num_chains; i++) {
michael@0 172 let k = {};
michael@0 173 dom.key = k;
michael@0 174 let new_dom = document.createElement("div");
michael@0 175 garbage_map.set(k, {val_child:new_dom});
michael@0 176 dom = document.createElement("div");
michael@0 177 new_dom.appendChild(dom);
michael@0 178 };
michael@0 179 // tie the knot
michael@0 180 dom.appendChild(dom0);
michael@0 181 };
michael@0 182
michael@0 183 chained_garbage_maps();
michael@0 184
michael@0 185
michael@0 186 /* black weak map, chained garbage cycle involving DOM, XPCWN keys */
michael@0 187 let wn_garbage_map = new WeakMap;
michael@0 188
michael@0 189 let wn_chained_garbage_maps = function () {
michael@0 190 let dom0 = document.createElement("div");
michael@0 191 let dom = dom0;
michael@0 192 for(let i = 0; i < num_chains; i++) {
michael@0 193 let new_dom = document.createElement("div");
michael@0 194 wn_garbage_map.set(dom, {wn_val_child:new_dom});
michael@0 195 dom = document.createElement("div");
michael@0 196 new_dom.appendChild(dom);
michael@0 197 };
michael@0 198 // tie the knot
michael@0 199 dom.appendChild(dom0);
michael@0 200 };
michael@0 201
michael@0 202 wn_chained_garbage_maps();
michael@0 203
michael@0 204
michael@0 205 /* The cycle collector shouldn't remove a live wrapped native key. */
michael@0 206
michael@0 207 let wn_live_map = new WeakMap;
michael@0 208
michael@0 209 let make_live_map = function () {
michael@0 210 let live = get_live_dom();
michael@0 211 wn_live_map.set(live, {});
michael@0 212 ok(wn_live_map.has(get_live_dom()), "Live map should have live DOM node before GC.");
michael@0 213 }
michael@0 214
michael@0 215 make_live_map();
michael@0 216
michael@0 217 let unpreservable_native_key = function () {
michael@0 218 // We only allow natives that support wrapper preservation to be used as weak
michael@0 219 // map keys. We should be able to try to add unpreservable natives as keys without
michael@0 220 // crashing (bug 711616), but we should throw an error (bug 761620).
michael@0 221
michael@0 222 let dummy_test_map = new WeakMap;
michael@0 223
michael@0 224 let rule_fail = false;
michael@0 225 let got_rule = false;
michael@0 226 try {
michael@0 227 var rule = document.styleSheets[0].cssRules[0];
michael@0 228 got_rule = true;
michael@0 229 dummy_test_map.set(rule, 1);
michael@0 230 } catch (e) {
michael@0 231 rule_fail = true;
michael@0 232 }
michael@0 233 ok(got_rule, "Got the CSS rule");
michael@0 234 ok(rule_fail, "Using a CSS rule as a weak map key should produce an exception because it can't be wrapper preserved.");
michael@0 235
michael@0 236 }
michael@0 237
michael@0 238 unpreservable_native_key();
michael@0 239
michael@0 240 /* set up for running precise GC/CC then checking the results */
michael@0 241
michael@0 242 SimpleTest.waitForExplicitFinish();
michael@0 243
michael@0 244 Cu.schedulePreciseGC(function () {
michael@0 245 SpecialPowers.DOMWindowUtils.cycleCollect();
michael@0 246 SpecialPowers.DOMWindowUtils.garbageCollect();
michael@0 247 SpecialPowers.DOMWindowUtils.garbageCollect();
michael@0 248
michael@0 249 ok(weak_ref_dead(weakref), "Garbage gray cycle should be collected.");
michael@0 250
michael@0 251 check_nested_cc_maps();
michael@0 252
michael@0 253 is(Cu.nondeterministicGetWeakMapKeys(garbage_map).length, 0, "Chained garbage weak map entries should not leak.");
michael@0 254
michael@0 255 check_basic_unit();
michael@0 256
michael@0 257 // fixed by Bug 680937
michael@0 258 is(Cu.nondeterministicGetWeakMapKeys(wn_garbage_map).length, 0,
michael@0 259 "Chained garbage WN weak map entries should not leak.");
michael@0 260
michael@0 261 // fixed by Bug 680937
michael@0 262 is(Cu.nondeterministicGetWeakMapKeys(wn_live_map).length, 1,
michael@0 263 "Live weak map wrapped native key should not be removed.");
michael@0 264
michael@0 265 ok(wn_live_map.has(get_live_dom()), "Live map should have live dom.");
michael@0 266
michael@0 267 SimpleTest.finish();
michael@0 268 });
michael@0 269
michael@0 270 ]]>
michael@0 271 </script>
michael@0 272 </window>

mercurial