-
Notifications
You must be signed in to change notification settings - Fork 54
/
SharedArrayObject.cpp
449 lines (365 loc) · 14.3 KB
/
SharedArrayObject.cpp
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/SharedArrayObject.h"
#include "mozilla/Atomics.h"
#include "mozilla/CheckedInt.h"
#include "jsfriendapi.h"
#include "gc/FreeOp.h"
#include "jit/AtomicOperations.h"
#include "js/PropertySpec.h"
#include "js/Wrapper.h"
#include "vm/SharedMem.h"
#include "wasm/WasmSignalHandlers.h"
#include "wasm/WasmTypes.h"
#include "vm/JSObject-inl.h"
#include "vm/NativeObject-inl.h"
using mozilla::CheckedInt;
using mozilla::Maybe;
using mozilla::Nothing;
using namespace js;
static size_t SharedArrayMappedSizeForWasm(size_t declaredMaxSize) {
#ifdef WASM_HUGE_MEMORY
return wasm::HugeMappedSize;
#else
return wasm::ComputeMappedSize(declaredMaxSize);
#endif
}
static uint32_t SharedArrayAccessibleSize(uint32_t length) {
return AlignBytes(length, gc::SystemPageSize());
}
// `max` must be something for wasm, nothing for other cases.
SharedArrayRawBuffer* SharedArrayRawBuffer::Allocate(
uint32_t length, const Maybe<uint32_t>& max) {
MOZ_RELEASE_ASSERT(length <= ArrayBufferObject::MaxBufferByteLength);
bool preparedForWasm = max.isSome();
uint32_t accessibleSize = SharedArrayAccessibleSize(length);
if (accessibleSize < length) {
return nullptr;
}
uint32_t maxSize = max.isSome() ? *max : accessibleSize;
size_t mappedSize;
if (preparedForWasm) {
mappedSize = SharedArrayMappedSizeForWasm(maxSize);
} else {
mappedSize = accessibleSize;
}
uint64_t mappedSizeWithHeader = mappedSize + gc::SystemPageSize();
uint64_t accessibleSizeWithHeader = accessibleSize + gc::SystemPageSize();
void* p = MapBufferMemory(mappedSizeWithHeader, accessibleSizeWithHeader);
if (!p) {
return nullptr;
}
uint8_t* buffer = reinterpret_cast<uint8_t*>(p) + gc::SystemPageSize();
uint8_t* base = buffer - sizeof(SharedArrayRawBuffer);
SharedArrayRawBuffer* rawbuf = new (base) SharedArrayRawBuffer(
buffer, length, maxSize, mappedSize, preparedForWasm);
MOZ_ASSERT(rawbuf->length_ == length); // Deallocation needs this
return rawbuf;
}
#ifndef WASM_HUGE_MEMORY
void SharedArrayRawBuffer::tryGrowMaxSizeInPlace(uint32_t deltaMaxSize) {
CheckedInt<uint32_t> newMaxSize = maxSize_;
newMaxSize += deltaMaxSize;
MOZ_ASSERT(newMaxSize.isValid());
MOZ_ASSERT(newMaxSize.value() % wasm::PageSize == 0);
size_t newMappedSize = SharedArrayMappedSizeForWasm(newMaxSize.value());
MOZ_ASSERT(mappedSize_ <= newMappedSize);
if (mappedSize_ == newMappedSize) {
return;
}
if (!ExtendBufferMapping(basePointer(), mappedSize_, newMappedSize)) {
return;
}
mappedSize_ = newMappedSize;
maxSize_ = newMaxSize.value();
}
#endif
bool SharedArrayRawBuffer::wasmGrowToSizeInPlace(const Lock&,
uint32_t newLength) {
if (newLength > ArrayBufferObject::MaxBufferByteLength) {
return false;
}
MOZ_ASSERT(newLength >= length_);
if (newLength == length_) {
return true;
}
uint32_t delta = newLength - length_;
MOZ_ASSERT(delta % wasm::PageSize == 0);
uint8_t* dataEnd = dataPointerShared().unwrap(/* for resize */) + length_;
MOZ_ASSERT(uintptr_t(dataEnd) % gc::SystemPageSize() == 0);
// The ordering of committing memory and changing length does not matter
// since all clients take the lock.
if (!CommitBufferMemory(dataEnd, delta)) {
return false;
}
length_ = newLength;
return true;
}
bool SharedArrayRawBuffer::addReference() {
MOZ_RELEASE_ASSERT(refcount_ > 0);
// Be careful never to overflow the refcount field.
for (;;) {
uint32_t old_refcount = refcount_;
uint32_t new_refcount = old_refcount + 1;
if (new_refcount == 0) {
return false;
}
if (refcount_.compareExchange(old_refcount, new_refcount)) {
return true;
}
}
}
void SharedArrayRawBuffer::dropReference() {
// Normally if the refcount is zero then the memory will have been unmapped
// and this test may just crash, but if the memory has been retained for any
// reason we will catch the underflow here.
MOZ_RELEASE_ASSERT(refcount_ > 0);
// Drop the reference to the buffer.
uint32_t new_refcount = --refcount_; // Atomic.
if (new_refcount) {
return;
}
size_t mappedSizeWithHeader = mappedSize_ + gc::SystemPageSize();
// This was the final reference, so release the buffer.
UnmapBufferMemory(basePointer(), mappedSizeWithHeader);
}
MOZ_ALWAYS_INLINE bool SharedArrayBufferObject::byteLengthGetterImpl(
JSContext* cx, const CallArgs& args) {
MOZ_ASSERT(IsSharedArrayBuffer(args.thisv()));
args.rval().setInt32(
args.thisv().toObject().as<SharedArrayBufferObject>().byteLength());
return true;
}
bool SharedArrayBufferObject::byteLengthGetter(JSContext* cx, unsigned argc,
Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
return CallNonGenericMethod<IsSharedArrayBuffer, byteLengthGetterImpl>(cx,
args);
}
// ES2017 draft rev 6390c2f1b34b309895d31d8c0512eac8660a0210
// 24.2.2.1 SharedArrayBuffer( length )
bool SharedArrayBufferObject::class_constructor(JSContext* cx, unsigned argc,
Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
// Step 1.
if (!ThrowIfNotConstructing(cx, args, "SharedArrayBuffer")) {
return false;
}
// Step 2.
uint64_t byteLength;
if (!ToIndex(cx, args.get(0), &byteLength)) {
return false;
}
// Step 3 (Inlined 24.2.1.1 AllocateSharedArrayBuffer).
// 24.2.1.1, step 1 (Inlined 9.1.14 OrdinaryCreateFromConstructor).
RootedObject proto(cx);
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_SharedArrayBuffer,
&proto)) {
return false;
}
// 24.2.1.1, step 3 (Inlined 6.2.7.2 CreateSharedByteDataBlock, step 2).
// Refuse to allocate too large buffers, currently limited to ~2 GiB.
if (byteLength > INT32_MAX) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_SHARED_ARRAY_BAD_LENGTH);
return false;
}
// 24.2.1.1, steps 1 and 4-6.
JSObject* bufobj = New(cx, uint32_t(byteLength), proto);
if (!bufobj) {
return false;
}
args.rval().setObject(*bufobj);
return true;
}
SharedArrayBufferObject* SharedArrayBufferObject::New(JSContext* cx,
uint32_t length,
HandleObject proto) {
SharedArrayRawBuffer* buffer =
SharedArrayRawBuffer::Allocate(length, Nothing());
if (!buffer) {
return nullptr;
}
SharedArrayBufferObject* obj = New(cx, buffer, length, proto);
if (!obj) {
buffer->dropReference();
return nullptr;
}
return obj;
}
SharedArrayBufferObject* SharedArrayBufferObject::New(
JSContext* cx, SharedArrayRawBuffer* buffer, uint32_t length,
HandleObject proto) {
MOZ_ASSERT(cx->realm()->creationOptions().getSharedMemoryAndAtomicsEnabled());
AutoSetNewObjectMetadata metadata(cx);
Rooted<SharedArrayBufferObject*> obj(
cx, NewObjectWithClassProto<SharedArrayBufferObject>(cx, proto));
if (!obj) {
return nullptr;
}
MOZ_ASSERT(obj->getClass() == &class_);
obj->acceptRawBuffer(buffer, length);
return obj;
}
void SharedArrayBufferObject::acceptRawBuffer(SharedArrayRawBuffer* buffer,
uint32_t length) {
setReservedSlot(RAWBUF_SLOT, PrivateValue(buffer));
setReservedSlot(LENGTH_SLOT, PrivateUint32Value(length));
}
void SharedArrayBufferObject::dropRawBuffer() {
setReservedSlot(RAWBUF_SLOT, UndefinedValue());
}
SharedArrayRawBuffer* SharedArrayBufferObject::rawBufferObject() const {
Value v = getReservedSlot(RAWBUF_SLOT);
MOZ_ASSERT(!v.isUndefined());
return reinterpret_cast<SharedArrayRawBuffer*>(v.toPrivate());
}
void SharedArrayBufferObject::Finalize(FreeOp* fop, JSObject* obj) {
MOZ_ASSERT(fop->maybeOnHelperThread());
SharedArrayBufferObject& buf = obj->as<SharedArrayBufferObject>();
// Detect the case of failure during SharedArrayBufferObject creation,
// which causes a SharedArrayRawBuffer to never be attached.
Value v = buf.getReservedSlot(RAWBUF_SLOT);
if (!v.isUndefined()) {
buf.rawBufferObject()->dropReference();
buf.dropRawBuffer();
}
}
#ifndef WASM_HUGE_MEMORY
uint32_t SharedArrayBufferObject::wasmBoundsCheckLimit() const {
if (isWasm()) {
return rawBufferObject()->boundsCheckLimit();
}
return byteLength();
}
#endif
/* static */ void SharedArrayBufferObject::addSizeOfExcludingThis(
JSObject* obj, mozilla::MallocSizeOf mallocSizeOf, JS::ClassInfo* info) {
// Divide the buffer size by the refcount to get the fraction of the buffer
// owned by this thread. It's conceivable that the refcount might change in
// the middle of memory reporting, in which case the amount reported for
// some threads might be to high (if the refcount goes up) or too low (if
// the refcount goes down). But that's unlikely and hard to avoid, so we
// just live with the risk.
const SharedArrayBufferObject& buf = obj->as<SharedArrayBufferObject>();
info->objectsNonHeapElementsShared +=
buf.byteLength() / buf.rawBufferObject()->refcount();
}
/* static */ void SharedArrayBufferObject::copyData(
Handle<SharedArrayBufferObject*> toBuffer, uint32_t toIndex,
Handle<SharedArrayBufferObject*> fromBuffer, uint32_t fromIndex,
uint32_t count) {
MOZ_ASSERT(toBuffer->byteLength() >= count);
MOZ_ASSERT(toBuffer->byteLength() >= toIndex + count);
MOZ_ASSERT(fromBuffer->byteLength() >= fromIndex);
MOZ_ASSERT(fromBuffer->byteLength() >= fromIndex + count);
jit::AtomicOperations::memcpySafeWhenRacy(
toBuffer->dataPointerShared() + toIndex,
fromBuffer->dataPointerShared() + fromIndex, count);
}
SharedArrayBufferObject* SharedArrayBufferObject::createFromNewRawBuffer(
JSContext* cx, SharedArrayRawBuffer* buffer, uint32_t initialSize) {
MOZ_ASSERT(cx->realm()->creationOptions().getSharedMemoryAndAtomicsEnabled());
AutoSetNewObjectMetadata metadata(cx);
SharedArrayBufferObject* obj =
NewBuiltinClassInstance<SharedArrayBufferObject>(cx);
if (!obj) {
buffer->dropReference();
return nullptr;
}
obj->acceptRawBuffer(buffer, initialSize);
return obj;
}
static const ClassOps SharedArrayBufferObjectClassOps = {
nullptr, /* addProperty */
nullptr, /* delProperty */
nullptr, /* enumerate */
nullptr, /* newEnumerate */
nullptr, /* resolve */
nullptr, /* mayResolve */
SharedArrayBufferObject::Finalize,
nullptr, /* call */
nullptr, /* hasInstance */
nullptr, /* construct */
nullptr, /* trace */
};
static const JSFunctionSpec sharedarrray_functions[] = {JS_FS_END};
static const JSPropertySpec sharedarrray_properties[] = {
JS_SELF_HOSTED_SYM_GET(species, "SharedArrayBufferSpecies", 0), JS_PS_END};
static const JSFunctionSpec sharedarray_proto_functions[] = {
JS_SELF_HOSTED_FN("slice", "SharedArrayBufferSlice", 2, 0), JS_FS_END};
static const JSPropertySpec sharedarray_proto_properties[] = {
JS_PSG("byteLength", SharedArrayBufferObject::byteLengthGetter, 0),
JS_STRING_SYM_PS(toStringTag, "SharedArrayBuffer", JSPROP_READONLY),
JS_PS_END};
static const ClassSpec SharedArrayBufferObjectClassSpec = {
GenericCreateConstructor<SharedArrayBufferObject::class_constructor, 1,
gc::AllocKind::FUNCTION>,
GenericCreatePrototype<SharedArrayBufferObject>,
sharedarrray_functions,
sharedarrray_properties,
sharedarray_proto_functions,
sharedarray_proto_properties};
const Class SharedArrayBufferObject::class_ = {
"SharedArrayBuffer",
JSCLASS_DELAY_METADATA_BUILDER |
JSCLASS_HAS_RESERVED_SLOTS(SharedArrayBufferObject::RESERVED_SLOTS) |
JSCLASS_HAS_CACHED_PROTO(JSProto_SharedArrayBuffer) |
JSCLASS_BACKGROUND_FINALIZE,
&SharedArrayBufferObjectClassOps, &SharedArrayBufferObjectClassSpec,
JS_NULL_CLASS_EXT};
const Class SharedArrayBufferObject::protoClass_ = {
"SharedArrayBufferPrototype",
JSCLASS_HAS_CACHED_PROTO(JSProto_SharedArrayBuffer), JS_NULL_CLASS_OPS,
&SharedArrayBufferObjectClassSpec};
bool js::IsSharedArrayBuffer(HandleValue v) {
return v.isObject() && v.toObject().is<SharedArrayBufferObject>();
}
bool js::IsSharedArrayBuffer(HandleObject o) {
return o->is<SharedArrayBufferObject>();
}
bool js::IsSharedArrayBuffer(JSObject* o) {
return o->is<SharedArrayBufferObject>();
}
SharedArrayBufferObject& js::AsSharedArrayBuffer(HandleObject obj) {
MOZ_ASSERT(IsSharedArrayBuffer(obj));
return obj->as<SharedArrayBufferObject>();
}
JS_FRIEND_API uint32_t JS_GetSharedArrayBufferByteLength(JSObject* obj) {
obj = CheckedUnwrap(obj);
return obj ? obj->as<SharedArrayBufferObject>().byteLength() : 0;
}
JS_FRIEND_API void js::GetSharedArrayBufferLengthAndData(JSObject* obj,
uint32_t* length,
bool* isSharedMemory,
uint8_t** data) {
MOZ_ASSERT(obj->is<SharedArrayBufferObject>());
*length = obj->as<SharedArrayBufferObject>().byteLength();
*data = obj->as<SharedArrayBufferObject>().dataPointerShared().unwrap(
/*safe - caller knows*/);
*isSharedMemory = true;
}
JS_FRIEND_API JSObject* JS_NewSharedArrayBuffer(JSContext* cx,
uint32_t nbytes) {
MOZ_ASSERT(cx->realm()->creationOptions().getSharedMemoryAndAtomicsEnabled());
MOZ_ASSERT(nbytes <= INT32_MAX);
return SharedArrayBufferObject::New(cx, nbytes, /* proto = */ nullptr);
}
JS_FRIEND_API bool JS_IsSharedArrayBufferObject(JSObject* obj) {
obj = CheckedUnwrap(obj);
return obj ? obj->is<SharedArrayBufferObject>() : false;
}
JS_FRIEND_API uint8_t* JS_GetSharedArrayBufferData(JSObject* obj,
bool* isSharedMemory,
const JS::AutoRequireNoGC&) {
obj = CheckedUnwrap(obj);
if (!obj) {
return nullptr;
}
*isSharedMemory = true;
return obj->as<SharedArrayBufferObject>().dataPointerShared().unwrap(
/*safe - caller knows*/);
}