-
Notifications
You must be signed in to change notification settings - Fork 4
/
WeakMap.js
454 lines (427 loc) · 15.8 KB
/
WeakMap.js
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
// Copyright (C) 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Install a leaky WeakMap emulation on platforms that
* don't provide a built-in one.
*
* <p>Assumes that an ES5 platform where, if {@code WeakMap} is
* already present, then it conforms to the anticipated ES6
* specification. To run this file on an ES5 or almost ES5
* implementation where the {@code WeakMap} specification does not
* quite conform, run <code>repairES5.js</code> first.
*
* @author Mark S. Miller
* @requires ses, crypto, ArrayBuffer, Uint8Array
* @overrides WeakMap, WeakMapModule
*/
/**
* This {@code WeakMap} emulation is observably equivalent to the
* ES-Harmony WeakMap, but with leakier garbage collection properties.
*
* <p>As with true WeakMaps, in this emulation, a key does not
* retain maps indexed by that key and (crucially) a map does not
* retain the keys it indexes. A map by itself also does not retain
* the values associated with that map.
*
* <p>However, the values associated with a key in some map are
* retained so long as that key is retained and those associations are
* not overridden. For example, when used to support membranes, all
* values exported from a given membrane will live for the lifetime
* they would have had in the absence of an interposed membrane. Even
* when the membrane is revoked, all objects that would have been
* reachable in the absence of revocation will still be reachable, as
* far as the GC can tell, even though they will no longer be relevant
* to ongoing computation.
*
* <p>The API implemented here is approximately the API as implemented
* in FF6.0a1 and agreed to by MarkM, Andreas Gal, and Dave Herman,
* rather than the offially approved proposal page. TODO(erights):
* upgrade the ecmascript WeakMap proposal page to explain this API
* change and present to EcmaScript committee for their approval.
*
* <p>The first difference between the emulation here and that in
* FF6.0a1 is the presence of non enumerable {@code get___, has___,
* set___, and delete___} methods on WeakMap instances to represent
* what would be the hidden internal properties of a primitive
* implementation. Whereas the FF6.0a1 WeakMap.prototype methods
* require their {@code this} to be a genuine WeakMap instance (i.e.,
* an object of {@code [[Class]]} "WeakMap}), since there is nothing
* unforgeable about the pseudo-internal method names used here,
* nothing prevents these emulated prototype methods from being
* applied to non-WeakMaps with pseudo-internal methods of the same
* names.
*
* <p>Another difference is that our emulated {@code
* WeakMap.prototype} is not itself a WeakMap. A problem with the
* current FF6.0a1 API is that WeakMap.prototype is itself a WeakMap
* providing ambient mutability and an ambient communications
* channel. Thus, if a WeakMap is already present and has this
* problem, repairES5.js wraps it in a safe wrappper in order to
* prevent access to this channel. (See
* PATCH_MUTABLE_FROZEN_WEAKMAP_PROTO in repairES5.js).
*/
var WeakMap;
/**
* If this is a full <a href=
* "http://code.google.com/p/es-lab/wiki/SecureableES5"
* >secureable ES5</a> platform and the ES-Harmony {@code WeakMap} is
* absent, install an approximate emulation.
*
* <p>If this is almost a secureable ES5 platform, then WeakMap.js
* should be run after repairES5.js.
*
* <p>See {@code WeakMap} for documentation of the garbage collection
* properties of this WeakMap emulation.
*/
(function WeakMapModule() {
"use strict";
if (typeof ses !== 'undefined' && ses.ok && !ses.ok()) {
// already too broken, so give up
return;
}
if (typeof WeakMap === 'function') {
// assumed fine, so we're done.
return;
}
var hop = Object.prototype.hasOwnProperty;
var gopn = Object.getOwnPropertyNames;
var defProp = Object.defineProperty;
/**
* Holds the orginal static properties of the Object constructor,
* after repairES5 fixes these if necessary to be a more complete
* secureable ES5 environment, but before installing the following
* WeakMap emulation overrides and before any untrusted code runs.
*/
var originalProps = {};
gopn(Object).forEach(function(name) {
originalProps[name] = Object[name];
});
/**
* Security depends on HIDDEN_NAME being both <i>unguessable</i> and
* <i>undiscoverable</i> by untrusted code.
*
* <p>Given the known weaknesses of Math.random() on existing
* browsers, it does not generate unguessability we can be confident
* of.
*
* <p>It is the monkey patching logic in this file that is intended
* to ensure undiscoverability. The basic idea is that there are
* three fundamental means of discovering properties of an object:
* The for/in loop, Object.keys(), and Object.getOwnPropertyNames(),
* as well as some proposed ES6 extensions that appear on our
* whitelist. The first two only discover enumerable properties, and
* we only use HIDDEN_NAME to name a non-enumerable property, so the
* only remaining threat should be getOwnPropertyNames and some
* proposed ES6 extensions that appear on our whitelist. We monkey
* patch them to remove HIDDEN_NAME from the list of properties they
* returns.
*
* <p>TODO(erights): On a platform with built-in Proxies, proxies
* could be used to trap and thereby discover the HIDDEN_NAME, so we
* need to monkey patch Proxy.create, Proxy.createFunction, etc, in
* order to wrap the provided handler with the real handler which
* filters out all traps using HIDDEN_NAME.
*
* <p>TODO(erights): Revisit Mike Stay's suggestion that we use an
* encapsulated function at a not-necessarily-secret name, which
* uses the Stiegler shared-state rights amplification pattern to
* reveal the associated value only to the WeakMap in which this key
* is associated with that value. Since only the key retains the
* function, the function can also remember the key without causing
* leakage of the key, so this doesn't violate our general gc
* goals. In addition, because the name need not be a guarded
* secret, we could efficiently handle cross-frame frozen keys.
*/
var HIDDEN_NAME_PREFIX = 'weakmap:';
var HIDDEN_NAME = HIDDEN_NAME_PREFIX + 'ident:' + Math.random() + '___';
if (typeof crypto !== 'undefined' &&
typeof crypto.getRandomValues === 'function' &&
typeof ArrayBuffer === 'function' &&
typeof Uint8Array === 'function') {
var ab = new ArrayBuffer(25);
var u8s = new Uint8Array(ab);
crypto.getRandomValues(u8s);
HIDDEN_NAME = HIDDEN_NAME_PREFIX + 'rand:' +
Array.prototype.map.call(u8s, function(u8) {
return (u8 % 36).toString(36);
}).join('') + '___';
}
/**
* Monkey patch getOwnPropertyNames to avoid revealing the
* HIDDEN_NAME.
*
* <p>The ES5.1 spec requires each name to appear only once, but as
* of this writing, this requirement is controversial for ES6, so we
* made this code robust against this case. If the resulting extra
* search turns out to be expensive, we can probably relax this once
* ES6 is adequately supported on all major browsers, iff no browser
* versions we support at that time have relaxed this constraint
* without providing built-in ES6 WeakMaps.
*/
defProp(Object, 'getOwnPropertyNames', {
value: function fakeGetOwnPropertyNames(obj) {
return gopn(obj).filter(function(name) {
return !(
name.substr(0, HIDDEN_NAME_PREFIX.length) == HIDDEN_NAME_PREFIX &&
name.substr(name.length - 3) === '___');
});
}
});
/**
* getPropertyNames is not in ES5 but it is proposed for ES6 and
* does appear in our whitelist, so we need to clean it too.
*/
if ('getPropertyNames' in Object) {
defProp(Object, 'getPropertyNames', {
value: function fakeGetPropertyNames(obj) {
return originalProps.getPropertyNames(obj).filter(function(name) {
return name !== HIDDEN_NAME;
});
}
});
}
/**
* <p>To treat objects as identity-keys with reasonable efficiency
* on ES5 by itself (i.e., without any object-keyed collections), we
* need to add a hidden property to such key objects when we
* can. This raises several issues:
* <ul>
* <li>Arranging to add this property to objects before we lose the
* chance, and
* <li>Hiding the existence of this new property from most
* JavaScript code.
* <li>Preventing <i>certification theft</i>, where one object is
* created falsely claiming to be the key of an association
* actually keyed by another object.
* <li>Preventing <i>value theft</i>, where untrusted code with
* access to a key object but not a weak map nevertheless
* obtains access to the value associated with that key in that
* weak map.
* </ul>
* We do so by
* <ul>
* <li>Making the name of the hidden property unguessable, so "[]"
* indexing, which we cannot intercept, cannot be used to access
* a property without knowing the name.
* <li>Making the hidden property non-enumerable, so we need not
* worry about for-in loops or {@code Object.keys},
* <li>monkey patching those reflective methods that would
* prevent extensions, to add this hidden property first,
* <li>monkey patching those methods that would reveal this
* hidden property.
* </ul>
* Unfortunately, because of same-origin iframes, we cannot reliably
* add this hidden property before an object becomes
* non-extensible. Instead, if we encounter a non-extensible object
* without a hidden record that we can detect (whether or not it has
* a hidden record stored under a name secret to us), then we just
* use the key object itself to represent its identity in a brute
* force leaky map stored in the weak map, losing all the advantages
* of weakness for these.
*/
function getHiddenRecord(key) {
if (key !== Object(key)) {
throw new TypeError('Not an object: ' + key);
}
var hiddenRecord = key[HIDDEN_NAME];
if (hiddenRecord && hiddenRecord.key === key) { return hiddenRecord; }
if (!originalProps.isExtensible(key)) {
// Weak map must brute force, as explained in doc-comment above.
return void 0;
}
var gets = [];
var vals = [];
hiddenRecord = {
key: key, // self pointer for quick own check above.
gets: gets, // get___ methods identifying weak maps
vals: vals // values associated with this key in each
// corresponding weak map.
};
defProp(key, HIDDEN_NAME, {
value: hiddenRecord,
writable: false,
enumerable: false,
configurable: false
});
return hiddenRecord;
}
/**
* Monkey patch operations that would make their argument
* non-extensible.
*
* <p>The monkey patched versions throw a TypeError if their
* argument is not an object, so it should only be done to functions
* that should throw a TypeError anyway if their argument is not an
* object.
*/
(function(){
var oldFreeze = Object.freeze;
defProp(Object, 'freeze', {
value: function identifyingFreeze(obj) {
getHiddenRecord(obj);
return oldFreeze(obj);
}
});
var oldSeal = Object.seal;
defProp(Object, 'seal', {
value: function identifyingSeal(obj) {
getHiddenRecord(obj);
return oldSeal(obj);
}
});
var oldPreventExtensions = Object.preventExtensions;
defProp(Object, 'preventExtensions', {
value: function identifyingPreventExtensions(obj) {
getHiddenRecord(obj);
return oldPreventExtensions(obj);
}
});
})();
function constFunc(func) {
func.prototype = null;
return Object.freeze(func);
}
// Right now (12/25/2012) the histogram supports the current
// representation. We should check this occasionally, as a true
// constant time representation is easy.
// var histogram = [];
WeakMap = function() {
// We are currently (12/25/2012) never encountering any prematurely
// non-extensible keys.
var keys = []; // brute force for prematurely non-extensible keys.
var vals = []; // brute force for corresponding values.
function get___(key, opt_default) {
var hr = getHiddenRecord(key);
var i, vs;
if (hr) {
i = hr.gets.indexOf(get___);
vs = hr.vals;
} else {
i = keys.indexOf(key);
vs = vals;
}
return (i >= 0) ? vs[i] : opt_default;
}
function has___(key) {
var hr = getHiddenRecord(key);
var i;
if (hr) {
i = hr.gets.indexOf(get___);
} else {
i = keys.indexOf(key);
}
return i >= 0;
}
function set___(key, value) {
var hr = getHiddenRecord(key);
var i;
if (hr) {
i = hr.gets.indexOf(get___);
if (i >= 0) {
hr.vals[i] = value;
} else {
// i = hr.gets.length;
// histogram[i] = (histogram[i] || 0) + 1;
hr.gets.push(get___);
hr.vals.push(value);
}
} else {
i = keys.indexOf(key);
if (i >= 0) {
vals[i] = value;
} else {
keys.push(key);
vals.push(value);
}
}
}
function delete___(key) {
var hr = getHiddenRecord(key);
var i;
if (hr) {
i = hr.gets.indexOf(get___);
if (i >= 0) {
hr.gets.splice(i, 1);
hr.vals.splice(i, 1);
}
} else {
i = keys.indexOf(key);
if (i >= 0) {
keys.splice(i, 1);
vals.splice(i, 1);
}
}
return true;
}
return Object.create(WeakMap.prototype, {
get___: { value: constFunc(get___) },
has___: { value: constFunc(has___) },
set___: { value: constFunc(set___) },
delete___: { value: constFunc(delete___) }
});
};
WeakMap.prototype = Object.create(Object.prototype, {
get: {
/**
* Return the value most recently associated with key, or
* opt_default if none.
*/
value: function get(key, opt_default) {
return this.get___(key, opt_default);
},
writable: true,
configurable: true
},
has: {
/**
* Is there a value associated with key in this WeakMap?
*/
value: function has(key) {
return this.has___(key);
},
writable: true,
configurable: true
},
set: {
/**
* Associate value with key in this WeakMap, overwriting any
* previous association if present.
*/
value: function set(key, value) {
this.set___(key, value);
},
writable: true,
configurable: true
},
'delete': {
/**
* Remove any association for key in this WeakMap, returning
* whether there was one.
*
* <p>Note that the boolean return here does not work like the
* {@code delete} operator. The {@code delete} operator returns
* whether the deletion succeeds at bringing about a state in
* which the deleted property is absent. The {@code delete}
* operator therefore returns true if the property was already
* absent, whereas this {@code delete} method returns false if
* the association was already absent.
*/
value: function remove(key) {
return this.delete___(key);
},
writable: true,
configurable: true
}
});
})();