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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/builtins/builtins-utils-inl.h"
#include "src/builtins/builtins.h"
#include "src/handles/maybe-handles-inl.h"
#include "src/heap/heap-inl.h" // For ToBoolean. TODO(jkummerow): Drop.
#include "src/logging/counters.h"
#include "src/numbers/conversions.h"
#include "src/objects/js-array-buffer-inl.h"
#include "src/objects/objects-inl.h"
namespace v8 {
namespace internal {
#define CHECK_SHARED(expected, name, method) \
if (name->is_shared() != expected) { \
THROW_NEW_ERROR_RETURN_FAILURE( \
isolate, \
NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \
isolate->factory()->NewStringFromAsciiChecked(method), \
name)); \
}
#define CHECK_RESIZABLE(expected, name, method) \
if (name->is_resizable() != expected) { \
THROW_NEW_ERROR_RETURN_FAILURE( \
isolate, \
NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \
isolate->factory()->NewStringFromAsciiChecked(method), \
name)); \
}
// -----------------------------------------------------------------------------
// ES#sec-arraybuffer-objects
namespace {
bool RoundUpToPageSize(size_t byte_length, size_t page_size,
size_t max_allowed_byte_length, size_t* pages) {
size_t bytes_wanted = RoundUp(byte_length, page_size);
if (bytes_wanted > max_allowed_byte_length) {
return false;
}
*pages = bytes_wanted / page_size;
return true;
}
Object ConstructBuffer(Isolate* isolate, Handle<JSFunction> target,
Handle<JSReceiver> new_target, Handle<Object> length,
Handle<Object> max_length, InitializedFlag initialized) {
SharedFlag shared =
(*target != target->native_context().array_buffer_fun() &&
*target != target->native_context().resizable_array_buffer_fun())
? SharedFlag::kShared
: SharedFlag::kNotShared;
ResizableFlag resizable =
(*target == target->native_context().resizable_array_buffer_fun() ||
*target == target->native_context().growable_shared_array_buffer_fun())
? ResizableFlag::kResizable
: ResizableFlag::kNotResizable;
Handle<JSObject> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, result,
JSObject::New(target, new_target, Handle<AllocationSite>::null()));
auto array_buffer = Handle<JSArrayBuffer>::cast(result);
// Ensure that all fields are initialized because BackingStore::Allocate is
// allowed to GC. Note that we cannot move the allocation of the ArrayBuffer
// after BackingStore::Allocate because of the spec.
array_buffer->Setup(shared, resizable, nullptr);
size_t byte_length;
size_t max_byte_length = 0;
if (!TryNumberToSize(*length, &byte_length) ||
byte_length > JSArrayBuffer::kMaxByteLength) {
// ToNumber failed.
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
}
std::unique_ptr<BackingStore> backing_store;
if (resizable == ResizableFlag::kNotResizable) {
backing_store =
BackingStore::Allocate(isolate, byte_length, shared, initialized);
} else {
Handle<Object> number_max_length;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_max_length,
Object::ToInteger(isolate, max_length));
if (!TryNumberToSize(*number_max_length, &max_byte_length)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewRangeError(MessageTemplate::kInvalidArrayBufferMaxLength));
}
if (byte_length > max_byte_length) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewRangeError(MessageTemplate::kInvalidArrayBufferMaxLength));
}
size_t page_size = AllocatePageSize();
size_t initial_pages;
if (!RoundUpToPageSize(byte_length, page_size,
JSArrayBuffer::kMaxByteLength, &initial_pages)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
}
size_t max_pages;
if (!RoundUpToPageSize(max_byte_length, page_size,
JSArrayBuffer::kMaxByteLength, &max_pages)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewRangeError(MessageTemplate::kInvalidArrayBufferMaxLength));
}
constexpr bool kIsWasmMemory = false;
backing_store = BackingStore::TryAllocateAndPartiallyCommitMemory(
isolate, byte_length, page_size, initial_pages, max_pages,
kIsWasmMemory, shared);
}
if (!backing_store) {
// Allocation of backing store failed.
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed));
}
array_buffer->Attach(std::move(backing_store));
array_buffer->set_max_byte_length(max_byte_length);
return *array_buffer;
}
} // namespace
// ES #sec-arraybuffer-constructor
BUILTIN(ArrayBufferConstructor) {
HandleScope scope(isolate);
Handle<JSFunction> target = args.target();
DCHECK(*target == target->native_context().array_buffer_fun() ||
*target == target->native_context().shared_array_buffer_fun() ||
*target == target->native_context().resizable_array_buffer_fun() ||
*target ==
target->native_context().growable_shared_array_buffer_fun());
if (args.new_target()->IsUndefined(isolate)) { // [[Call]]
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
handle(target->shared().Name(), isolate)));
}
// [[Construct]]
Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
Handle<Object> length = args.atOrUndefined(isolate, 1);
Handle<Object> number_length;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_length,
Object::ToInteger(isolate, length));
if (number_length->Number() < 0.0) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
}
Handle<Object> max_length = args.atOrUndefined(isolate, 2);
return ConstructBuffer(isolate, target, new_target, number_length, max_length,
InitializedFlag::kZeroInitialized);
}
// This is a helper to construct an ArrayBuffer with uinitialized memory.
// This means the caller must ensure the buffer is totally initialized in
// all cases, or we will expose uinitialized memory to user code.
BUILTIN(ArrayBufferConstructor_DoNotInitialize) {
HandleScope scope(isolate);
Handle<JSFunction> target(isolate->native_context()->array_buffer_fun(),
isolate);
Handle<Object> length = args.atOrUndefined(isolate, 1);
return ConstructBuffer(isolate, target, target, length, Handle<Object>(),
InitializedFlag::kUninitialized);
}
static Object SliceHelper(BuiltinArguments args, Isolate* isolate,
const char* kMethodName, bool is_shared) {
HandleScope scope(isolate);
Handle<Object> start = args.at(1);
Handle<Object> end = args.atOrUndefined(isolate, 2);
// * If Type(O) is not Object, throw a TypeError exception.
// * If O does not have an [[ArrayBufferData]] internal slot, throw a
// TypeError exception.
CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
// * [AB] If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
// * [SAB] If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
CHECK_SHARED(is_shared, array_buffer, kMethodName);
CHECK_RESIZABLE(false, array_buffer, kMethodName);
// * [AB] If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
if (!is_shared && array_buffer->was_detached()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// * [AB] Let len be O.[[ArrayBufferByteLength]].
// * [SAB] Let len be O.[[ArrayBufferByteLength]].
double const len = array_buffer->byte_length();
// * Let relativeStart be ? ToInteger(start).
Handle<Object> relative_start;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start,
Object::ToInteger(isolate, start));
// * If relativeStart < 0, let first be max((len + relativeStart), 0); else
// let first be min(relativeStart, len).
double const first = (relative_start->Number() < 0)
? std::max(len + relative_start->Number(), 0.0)
: std::min(relative_start->Number(), len);
Handle<Object> first_obj = isolate->factory()->NewNumber(first);
// * If end is undefined, let relativeEnd be len; else let relativeEnd be ?
// ToInteger(end).
double relative_end;
if (end->IsUndefined(isolate)) {
relative_end = len;
} else {
Handle<Object> relative_end_obj;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj,
Object::ToInteger(isolate, end));
relative_end = relative_end_obj->Number();
}
// * If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let
// final be min(relativeEnd, len).
double const final_ = (relative_end < 0) ? std::max(len + relative_end, 0.0)
: std::min(relative_end, len);
// * Let newLen be max(final-first, 0).
double const new_len = std::max(final_ - first, 0.0);
Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len);
// * [AB] Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
// * [SAB] Let ctor be ? SpeciesConstructor(O, %SharedArrayBuffer%).
Handle<JSFunction> constructor_fun = is_shared
? isolate->shared_array_buffer_fun()
: isolate->array_buffer_fun();
Handle<Object> ctor;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, ctor,
Object::SpeciesConstructor(
isolate, Handle<JSReceiver>::cast(args.receiver()), constructor_fun));
// * Let new be ? Construct(ctor, newLen).
Handle<JSReceiver> new_;
{
const int argc = 1;
ScopedVector<Handle<Object>> argv(argc);
argv[0] = new_len_obj;
Handle<Object> new_obj;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, new_obj, Execution::New(isolate, ctor, argc, argv.begin()));
new_ = Handle<JSReceiver>::cast(new_obj);
}
// * If new does not have an [[ArrayBufferData]] internal slot, throw a
// TypeError exception.
if (!new_->IsJSArrayBuffer()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
isolate->factory()->NewStringFromAsciiChecked(kMethodName),
new_));
}
// * [AB] If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
// * [SAB] If IsSharedArrayBuffer(new) is false, throw a TypeError exception.
Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_);
CHECK_SHARED(is_shared, new_array_buffer, kMethodName);
// * [AB] If IsDetachedBuffer(new) is true, throw a TypeError exception.
if (!is_shared && new_array_buffer->was_detached()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// * [AB] If SameValue(new, O) is true, throw a TypeError exception.
if (!is_shared && new_->SameValue(*args.receiver())) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis));
}
// * [SAB] If new.[[ArrayBufferData]] and O.[[ArrayBufferData]] are the same
// Shared Data Block values, throw a TypeError exception.
if (is_shared &&
new_array_buffer->backing_store() == array_buffer->backing_store()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kSharedArrayBufferSpeciesThis));
}
// * If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
if (new_array_buffer->byte_length() < new_len) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(is_shared ? MessageTemplate::kSharedArrayBufferTooShort
: MessageTemplate::kArrayBufferTooShort));
}
// * [AB] NOTE: Side-effects of the above steps may have detached O.
// * [AB] If IsDetachedBuffer(O) is true, throw a TypeError exception.
if (!is_shared && array_buffer->was_detached()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// * Let fromBuf be O.[[ArrayBufferData]].
// * Let toBuf be new.[[ArrayBufferData]].
// * Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
size_t first_size = 0, new_len_size = 0;
CHECK(TryNumberToSize(*first_obj, &first_size));
CHECK(TryNumberToSize(*new_len_obj, &new_len_size));
DCHECK(new_array_buffer->byte_length() >= new_len_size);
if (new_len_size != 0) {
size_t from_byte_length = array_buffer->byte_length();
USE(from_byte_length);
DCHECK(first_size <= from_byte_length);
DCHECK(from_byte_length - first_size >= new_len_size);
uint8_t* from_data =
reinterpret_cast<uint8_t*>(array_buffer->backing_store());
uint8_t* to_data =
reinterpret_cast<uint8_t*>(new_array_buffer->backing_store());
CopyBytes(to_data, from_data + first_size, new_len_size);
}
return *new_;
}
// ES #sec-sharedarraybuffer.prototype.slice
BUILTIN(SharedArrayBufferPrototypeSlice) {
const char* const kMethodName = "SharedArrayBuffer.prototype.slice";
return SliceHelper(args, isolate, kMethodName, true);
}
// ES #sec-arraybuffer.prototype.slice
// ArrayBuffer.prototype.slice ( start, end )
BUILTIN(ArrayBufferPrototypeSlice) {
const char* const kMethodName = "ArrayBuffer.prototype.slice";
return SliceHelper(args, isolate, kMethodName, false);
}
static Object ResizeHelper(BuiltinArguments args, Isolate* isolate,
const char* kMethodName, bool is_shared) {
HandleScope scope(isolate);
// 1 Let O be the this value.
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
CHECK_RESIZABLE(true, array_buffer, kMethodName);
// [RAB] 3. If IsSharedArrayBuffer(O) is true, throw a *TypeError* exception
// [GSAB] 3. If IsSharedArrayBuffer(O) is false, throw a *TypeError* exception
CHECK_SHARED(is_shared, array_buffer, kMethodName);
// Let newByteLength to ? ToIntegerOrInfinity(newLength).
Handle<Object> new_length = args.at(1);
Handle<Object> number_new_byte_length;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_new_byte_length,
Object::ToInteger(isolate, new_length));
// [RAB] If IsDetachedBuffer(O) is true, throw a TypeError exception.
if (!is_shared && array_buffer->was_detached()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// [RAB] If newByteLength < 0 or newByteLength >
// O.[[ArrayBufferMaxByteLength]], throw a RangeError exception.
// [GSAB] If newByteLength < currentByteLength or newByteLength >
// O.[[ArrayBufferMaxByteLength]], throw a RangeError exception.
size_t new_byte_length;
if (!TryNumberToSize(*number_new_byte_length, &new_byte_length)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferResizeLength,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
if (is_shared && new_byte_length < array_buffer->byte_length()) {
// GrowableSharedArrayBuffer is only allowed to grow.
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferResizeLength,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
if (new_byte_length > array_buffer->max_byte_length()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferResizeLength,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
size_t page_size = AllocatePageSize();
size_t new_committed_pages;
bool round_return_value =
RoundUpToPageSize(new_byte_length, page_size,
JSArrayBuffer::kMaxByteLength, &new_committed_pages);
CHECK(round_return_value);
// [RAB] Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength).
// [GSAB] Let hostHandled be ? HostGrowArrayBuffer(O, newByteLength).
// If hostHandled is handled, return undefined.
// TODO(v8:11111): Wasm integration.
if (!is_shared) {
// [RAB] Let oldBlock be O.[[ArrayBufferData]].
// [RAB] Let newBlock be ? CreateByteDataBlock(newByteLength).
// [RAB] Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]).
// [RAB] Perform CopyDataBlockBytes(newBlock, 0, oldBlock, 0, copyLength).
// [RAB] NOTE: Neither creation of the new Data Block nor copying from the
// old Data Block are observable. Implementations reserve the right to
// implement this method as in-place growth or shrinkage.
if (array_buffer->GetBackingStore()->ResizeInPlace(
isolate, new_byte_length, new_committed_pages * page_size) !=
BackingStore::ResizeOrGrowResult::kSuccess) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kOutOfMemory,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// [RAB] Set O.[[ArrayBufferByteLength]] to newLength.
array_buffer->set_byte_length(new_byte_length);
} else {
// [GSAB] (Detailed description of the algorithm omitted.)
auto result = array_buffer->GetBackingStore()->GrowInPlace(
isolate, new_byte_length, new_committed_pages * page_size);
if (result == BackingStore::ResizeOrGrowResult::kFailure) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kOutOfMemory,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
if (result == BackingStore::ResizeOrGrowResult::kRace) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewRangeError(
MessageTemplate::kInvalidArrayBufferResizeLength,
isolate->factory()->NewStringFromAsciiChecked(kMethodName)));
}
// Invariant: byte_length for a GSAB is 0 (it needs to be read from the
// BackingStore).
CHECK_EQ(0, array_buffer->byte_length());
}
return ReadOnlyRoots(isolate).undefined_value();
}
// ES #sec-get-growablesharedarraybuffer.prototype.bytelength
// get GrowableSharedArrayBuffer.prototype.byteLength
BUILTIN(GrowableSharedArrayBufferPrototypeGetByteLength) {
const char* const kMethodName =
"get GrowableSharedArrayBuffer.prototype.byteLength";
HandleScope scope(isolate);
// 1. Let O be the this value.
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxLength]]).
CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
CHECK_RESIZABLE(true, array_buffer, kMethodName);
// 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
CHECK_SHARED(true, array_buffer, kMethodName);
// 4. Let length be ArrayBufferByteLength(O, SeqCst).
// Invariant: byte_length for GSAB is 0 (it needs to be read from the
// BackingStore).
DCHECK_EQ(0, array_buffer->byte_length());
size_t byte_length =
array_buffer->GetBackingStore()->byte_length(std::memory_order_seq_cst);
// 5. Return length.
return *isolate->factory()->NewNumberFromSize(byte_length);
}
// ES #sec-resizablearraybuffer.prototype.resize
// ResizableArrayBuffer.prototype.resize(new_size))
BUILTIN(ResizableArrayBufferPrototypeResize) {
const char* const kMethodName = "ResizableArrayBuffer.prototype.resize";
constexpr bool kIsShared = false;
return ResizeHelper(args, isolate, kMethodName, kIsShared);
}
// ES #sec-growablesharedarraybuffer.prototype.grow
// GrowableSharedArrayBuffer.prototype.grow(new_size))
BUILTIN(GrowableSharedArrayBufferPrototypeGrow) {
const char* const kMethodName = "GrowableSharedArrayBuffer.prototype.grow";
constexpr bool kIsShared = true;
return ResizeHelper(args, isolate, kMethodName, kIsShared);
}
} // namespace internal
} // namespace v8
|