1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/builtins/builtins-utils-inl.h"
6 #include "src/builtins/builtins.h"
7 #include "src/handles/maybe-handles-inl.h"
8 #include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
9 #include "src/logging/counters.h"
10 #include "src/numbers/conversions.h"
11 #include "src/objects/js-array-buffer-inl.h"
12 #include "src/objects/objects-inl.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 #define CHECK_SHARED(expected, name, method)                                \
18   if (name->is_shared() != expected) {                                      \
19     THROW_NEW_ERROR_RETURN_FAILURE(                                         \
20         isolate,                                                            \
21         NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,          \
22                      isolate->factory()->NewStringFromAsciiChecked(method), \
23                      name));                                                \
24   }
25 
26 #define CHECK_RESIZABLE(expected, name, method)                             \
27   if (name->is_resizable() != expected) {                                   \
28     THROW_NEW_ERROR_RETURN_FAILURE(                                         \
29         isolate,                                                            \
30         NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,          \
31                      isolate->factory()->NewStringFromAsciiChecked(method), \
32                      name));                                                \
33   }
34 
35 // -----------------------------------------------------------------------------
36 // ES#sec-arraybuffer-objects
37 
38 namespace {
39 
RoundUpToPageSize(size_t byte_length,size_t page_size,size_t max_allowed_byte_length,size_t * pages)40 bool RoundUpToPageSize(size_t byte_length, size_t page_size,
41                        size_t max_allowed_byte_length, size_t* pages) {
42   size_t bytes_wanted = RoundUp(byte_length, page_size);
43   if (bytes_wanted > max_allowed_byte_length) {
44     return false;
45   }
46   *pages = bytes_wanted / page_size;
47   return true;
48 }
49 
ConstructBuffer(Isolate * isolate,Handle<JSFunction> target,Handle<JSReceiver> new_target,Handle<Object> length,Handle<Object> max_length,InitializedFlag initialized)50 Object ConstructBuffer(Isolate* isolate, Handle<JSFunction> target,
51                        Handle<JSReceiver> new_target, Handle<Object> length,
52                        Handle<Object> max_length, InitializedFlag initialized) {
53   SharedFlag shared = *target != target->native_context().array_buffer_fun()
54                           ? SharedFlag::kShared
55                           : SharedFlag::kNotShared;
56   ResizableFlag resizable = max_length.is_null() ? ResizableFlag::kNotResizable
57                                                  : ResizableFlag::kResizable;
58   Handle<JSObject> result;
59   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
60       isolate, result,
61       JSObject::New(target, new_target, Handle<AllocationSite>::null()));
62   auto array_buffer = Handle<JSArrayBuffer>::cast(result);
63   // Ensure that all fields are initialized because BackingStore::Allocate is
64   // allowed to GC. Note that we cannot move the allocation of the ArrayBuffer
65   // after BackingStore::Allocate because of the spec.
66   array_buffer->Setup(shared, resizable, nullptr);
67 
68   size_t byte_length;
69   size_t max_byte_length = 0;
70   if (!TryNumberToSize(*length, &byte_length) ||
71       byte_length > JSArrayBuffer::kMaxByteLength) {
72     // ToNumber failed.
73     THROW_NEW_ERROR_RETURN_FAILURE(
74         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
75   }
76 
77   std::unique_ptr<BackingStore> backing_store;
78   if (resizable == ResizableFlag::kNotResizable) {
79     backing_store =
80         BackingStore::Allocate(isolate, byte_length, shared, initialized);
81     max_byte_length = byte_length;
82   } else {
83     if (!TryNumberToSize(*max_length, &max_byte_length)) {
84       THROW_NEW_ERROR_RETURN_FAILURE(
85           isolate,
86           NewRangeError(MessageTemplate::kInvalidArrayBufferMaxLength));
87     }
88     if (byte_length > max_byte_length) {
89       THROW_NEW_ERROR_RETURN_FAILURE(
90           isolate,
91           NewRangeError(MessageTemplate::kInvalidArrayBufferMaxLength));
92     }
93 
94     size_t page_size = AllocatePageSize();
95     size_t initial_pages;
96     if (!RoundUpToPageSize(byte_length, page_size,
97                            JSArrayBuffer::kMaxByteLength, &initial_pages)) {
98       THROW_NEW_ERROR_RETURN_FAILURE(
99           isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
100     }
101 
102     size_t max_pages;
103     if (!RoundUpToPageSize(max_byte_length, page_size,
104                            JSArrayBuffer::kMaxByteLength, &max_pages)) {
105       THROW_NEW_ERROR_RETURN_FAILURE(
106           isolate,
107           NewRangeError(MessageTemplate::kInvalidArrayBufferMaxLength));
108     }
109     constexpr bool kIsWasmMemory = false;
110     backing_store = BackingStore::TryAllocateAndPartiallyCommitMemory(
111         isolate, byte_length, max_byte_length, page_size, initial_pages,
112         max_pages, kIsWasmMemory, shared);
113   }
114   if (!backing_store) {
115     // Allocation of backing store failed.
116     THROW_NEW_ERROR_RETURN_FAILURE(
117         isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed));
118   }
119 
120   array_buffer->Attach(std::move(backing_store));
121   array_buffer->set_max_byte_length(max_byte_length);
122   return *array_buffer;
123 }
124 
125 }  // namespace
126 
127 // ES #sec-arraybuffer-constructor
BUILTIN(ArrayBufferConstructor)128 BUILTIN(ArrayBufferConstructor) {
129   HandleScope scope(isolate);
130   Handle<JSFunction> target = args.target();
131   DCHECK(*target == target->native_context().array_buffer_fun() ||
132          *target == target->native_context().shared_array_buffer_fun());
133   if (args.new_target()->IsUndefined(isolate)) {  // [[Call]]
134     THROW_NEW_ERROR_RETURN_FAILURE(
135         isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
136                               handle(target->shared().Name(), isolate)));
137   }
138   // [[Construct]]
139   Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
140   Handle<Object> length = args.atOrUndefined(isolate, 1);
141 
142   Handle<Object> number_length;
143   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_length,
144                                      Object::ToInteger(isolate, length));
145   if (number_length->Number() < 0.0) {
146     THROW_NEW_ERROR_RETURN_FAILURE(
147         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
148   }
149 
150   Handle<Object> number_max_length;
151   if (FLAG_harmony_rab_gsab) {
152     Handle<Object> max_length;
153     Handle<Object> options = args.atOrUndefined(isolate, 2);
154     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
155         isolate, max_length,
156         JSObject::ReadFromOptionsBag(
157             options, isolate->factory()->max_byte_length_string(), isolate));
158 
159     if (!max_length->IsUndefined(isolate)) {
160       ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
161           isolate, number_max_length, Object::ToInteger(isolate, max_length));
162     }
163   }
164   return ConstructBuffer(isolate, target, new_target, number_length,
165                          number_max_length, InitializedFlag::kZeroInitialized);
166 }
167 
168 // This is a helper to construct an ArrayBuffer with uinitialized memory.
169 // This means the caller must ensure the buffer is totally initialized in
170 // all cases, or we will expose uinitialized memory to user code.
BUILTIN(ArrayBufferConstructor_DoNotInitialize)171 BUILTIN(ArrayBufferConstructor_DoNotInitialize) {
172   HandleScope scope(isolate);
173   Handle<JSFunction> target(isolate->native_context()->array_buffer_fun(),
174                             isolate);
175   Handle<Object> length = args.atOrUndefined(isolate, 1);
176   return ConstructBuffer(isolate, target, target, length, Handle<Object>(),
177                          InitializedFlag::kUninitialized);
178 }
179 
SliceHelper(BuiltinArguments args,Isolate * isolate,const char * kMethodName,bool is_shared)180 static Object SliceHelper(BuiltinArguments args, Isolate* isolate,
181                           const char* kMethodName, bool is_shared) {
182   HandleScope scope(isolate);
183   Handle<Object> start = args.at(1);
184   Handle<Object> end = args.atOrUndefined(isolate, 2);
185 
186   // * If Type(O) is not Object, throw a TypeError exception.
187   // * If O does not have an [[ArrayBufferData]] internal slot, throw a
188   //   TypeError exception.
189   CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
190   // * [AB] If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
191   // * [SAB] If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
192   CHECK_SHARED(is_shared, array_buffer, kMethodName);
193 
194   // * [AB] If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
195   if (!is_shared && array_buffer->was_detached()) {
196     THROW_NEW_ERROR_RETURN_FAILURE(
197         isolate, NewTypeError(MessageTemplate::kDetachedOperation,
198                               isolate->factory()->NewStringFromAsciiChecked(
199                                   kMethodName)));
200   }
201 
202   // * [AB] Let len be O.[[ArrayBufferByteLength]].
203   // * [SAB] Let len be O.[[ArrayBufferByteLength]].
204   double const len = array_buffer->GetByteLength();
205 
206   // * Let relativeStart be ? ToInteger(start).
207   Handle<Object> relative_start;
208   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start,
209                                      Object::ToInteger(isolate, start));
210 
211   // * If relativeStart < 0, let first be max((len + relativeStart), 0); else
212   //   let first be min(relativeStart, len).
213   double const first = (relative_start->Number() < 0)
214                            ? std::max(len + relative_start->Number(), 0.0)
215                            : std::min(relative_start->Number(), len);
216 
217   // * If end is undefined, let relativeEnd be len; else let relativeEnd be ?
218   //   ToInteger(end).
219   double relative_end;
220   if (end->IsUndefined(isolate)) {
221     relative_end = len;
222   } else {
223     Handle<Object> relative_end_obj;
224     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj,
225                                        Object::ToInteger(isolate, end));
226     relative_end = relative_end_obj->Number();
227   }
228 
229   // * If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let
230   //   final be min(relativeEnd, len).
231   double const final_ = (relative_end < 0) ? std::max(len + relative_end, 0.0)
232                                            : std::min(relative_end, len);
233 
234   // * Let newLen be max(final-first, 0).
235   double const new_len = std::max(final_ - first, 0.0);
236   Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len);
237 
238   // * [AB] Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
239   // * [SAB] Let ctor be ? SpeciesConstructor(O, %SharedArrayBuffer%).
240   Handle<JSFunction> constructor_fun = is_shared
241                                            ? isolate->shared_array_buffer_fun()
242                                            : isolate->array_buffer_fun();
243   Handle<Object> ctor;
244   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
245       isolate, ctor,
246       Object::SpeciesConstructor(
247           isolate, Handle<JSReceiver>::cast(args.receiver()), constructor_fun));
248 
249   // * Let new be ? Construct(ctor, newLen).
250   Handle<JSReceiver> new_;
251   {
252     const int argc = 1;
253 
254     base::ScopedVector<Handle<Object>> argv(argc);
255     argv[0] = new_len_obj;
256 
257     Handle<Object> new_obj;
258     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
259         isolate, new_obj, Execution::New(isolate, ctor, argc, argv.begin()));
260 
261     new_ = Handle<JSReceiver>::cast(new_obj);
262   }
263 
264   // * If new does not have an [[ArrayBufferData]] internal slot, throw a
265   //   TypeError exception.
266   if (!new_->IsJSArrayBuffer()) {
267     THROW_NEW_ERROR_RETURN_FAILURE(
268         isolate,
269         NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
270                      isolate->factory()->NewStringFromAsciiChecked(kMethodName),
271                      new_));
272   }
273 
274   // * [AB] If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
275   // * [SAB] If IsSharedArrayBuffer(new) is false, throw a TypeError exception.
276   Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_);
277   CHECK_SHARED(is_shared, new_array_buffer, kMethodName);
278 
279   // The created ArrayBuffer might or might not be resizable, since the species
280   // constructor might return a non-resizable or a resizable buffer.
281 
282   // * [AB] If IsDetachedBuffer(new) is true, throw a TypeError exception.
283   if (!is_shared && new_array_buffer->was_detached()) {
284     THROW_NEW_ERROR_RETURN_FAILURE(
285         isolate, NewTypeError(MessageTemplate::kDetachedOperation,
286                               isolate->factory()->NewStringFromAsciiChecked(
287                                   kMethodName)));
288   }
289 
290   // * [AB] If SameValue(new, O) is true, throw a TypeError exception.
291   if (!is_shared && new_->SameValue(*args.receiver())) {
292     THROW_NEW_ERROR_RETURN_FAILURE(
293         isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis));
294   }
295 
296   // * [SAB] If new.[[ArrayBufferData]] and O.[[ArrayBufferData]] are the same
297   //         Shared Data Block values, throw a TypeError exception.
298   if (is_shared &&
299       new_array_buffer->backing_store() == array_buffer->backing_store()) {
300     THROW_NEW_ERROR_RETURN_FAILURE(
301         isolate, NewTypeError(MessageTemplate::kSharedArrayBufferSpeciesThis));
302   }
303 
304   // * If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
305   size_t new_array_buffer_byte_length = new_array_buffer->GetByteLength();
306   if (new_array_buffer_byte_length < new_len) {
307     THROW_NEW_ERROR_RETURN_FAILURE(
308         isolate,
309         NewTypeError(is_shared ? MessageTemplate::kSharedArrayBufferTooShort
310                                : MessageTemplate::kArrayBufferTooShort));
311   }
312 
313   // * [AB] NOTE: Side-effects of the above steps may have detached O.
314   // * [AB] If IsDetachedBuffer(O) is true, throw a TypeError exception.
315   if (!is_shared && array_buffer->was_detached()) {
316     THROW_NEW_ERROR_RETURN_FAILURE(
317         isolate, NewTypeError(MessageTemplate::kDetachedOperation,
318                               isolate->factory()->NewStringFromAsciiChecked(
319                                   kMethodName)));
320   }
321 
322   // * Let fromBuf be O.[[ArrayBufferData]].
323   // * Let toBuf be new.[[ArrayBufferData]].
324   // * Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
325   size_t first_size = first;
326   size_t new_len_size = new_len;
327   DCHECK(new_array_buffer_byte_length >= new_len_size);
328 
329   if (new_len_size != 0) {
330     size_t from_byte_length = array_buffer->GetByteLength();
331     if (V8_UNLIKELY(!is_shared && array_buffer->is_resizable())) {
332       // The above steps might have resized the underlying buffer. In that case,
333       // only copy the still-accessible portion of the underlying data.
334       if (first_size > from_byte_length) {
335         return *new_;  // Nothing to copy.
336       }
337       if (new_len_size > from_byte_length - first_size) {
338         new_len_size = from_byte_length - first_size;
339       }
340     }
341     DCHECK(first_size <= from_byte_length);
342     DCHECK(from_byte_length - first_size >= new_len_size);
343     uint8_t* from_data =
344         reinterpret_cast<uint8_t*>(array_buffer->backing_store()) + first_size;
345     uint8_t* to_data =
346         reinterpret_cast<uint8_t*>(new_array_buffer->backing_store());
347     if (is_shared) {
348       base::Relaxed_Memcpy(reinterpret_cast<base::Atomic8*>(to_data),
349                            reinterpret_cast<base::Atomic8*>(from_data),
350                            new_len_size);
351     } else {
352       CopyBytes(to_data, from_data, new_len_size);
353     }
354   }
355 
356   return *new_;
357 }
358 
359 // ES #sec-sharedarraybuffer.prototype.slice
BUILTIN(SharedArrayBufferPrototypeSlice)360 BUILTIN(SharedArrayBufferPrototypeSlice) {
361   const char* const kMethodName = "SharedArrayBuffer.prototype.slice";
362   return SliceHelper(args, isolate, kMethodName, true);
363 }
364 
365 // ES #sec-arraybuffer.prototype.slice
366 // ArrayBuffer.prototype.slice ( start, end )
BUILTIN(ArrayBufferPrototypeSlice)367 BUILTIN(ArrayBufferPrototypeSlice) {
368   const char* const kMethodName = "ArrayBuffer.prototype.slice";
369   return SliceHelper(args, isolate, kMethodName, false);
370 }
371 
ResizeHelper(BuiltinArguments args,Isolate * isolate,const char * kMethodName,bool is_shared)372 static Object ResizeHelper(BuiltinArguments args, Isolate* isolate,
373                            const char* kMethodName, bool is_shared) {
374   HandleScope scope(isolate);
375 
376   // 1 Let O be the this value.
377   // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
378   CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
379   CHECK_RESIZABLE(true, array_buffer, kMethodName);
380 
381   // [RAB] 3. If IsSharedArrayBuffer(O) is true, throw a *TypeError* exception
382   // [GSAB] 3. If IsSharedArrayBuffer(O) is false, throw a *TypeError* exception
383   CHECK_SHARED(is_shared, array_buffer, kMethodName);
384 
385   // Let newByteLength to ? ToIntegerOrInfinity(newLength).
386   Handle<Object> new_length = args.at(1);
387   Handle<Object> number_new_byte_length;
388   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_new_byte_length,
389                                      Object::ToInteger(isolate, new_length));
390 
391   // [RAB] If IsDetachedBuffer(O) is true, throw a TypeError exception.
392   if (!is_shared && array_buffer->was_detached()) {
393     THROW_NEW_ERROR_RETURN_FAILURE(
394         isolate, NewTypeError(MessageTemplate::kDetachedOperation,
395                               isolate->factory()->NewStringFromAsciiChecked(
396                                   kMethodName)));
397   }
398 
399   // [RAB] If newByteLength < 0 or newByteLength >
400   // O.[[ArrayBufferMaxByteLength]], throw a RangeError exception.
401 
402   // [GSAB] If newByteLength < currentByteLength or newByteLength >
403   // O.[[ArrayBufferMaxByteLength]], throw a RangeError exception.
404   size_t new_byte_length;
405   if (!TryNumberToSize(*number_new_byte_length, &new_byte_length)) {
406     THROW_NEW_ERROR_RETURN_FAILURE(
407         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferResizeLength,
408                                isolate->factory()->NewStringFromAsciiChecked(
409                                    kMethodName)));
410   }
411 
412   if (is_shared && new_byte_length < array_buffer->byte_length()) {
413     // GrowableSharedArrayBuffer is only allowed to grow.
414     THROW_NEW_ERROR_RETURN_FAILURE(
415         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferResizeLength,
416                                isolate->factory()->NewStringFromAsciiChecked(
417                                    kMethodName)));
418   }
419 
420   if (new_byte_length > array_buffer->max_byte_length()) {
421     THROW_NEW_ERROR_RETURN_FAILURE(
422         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferResizeLength,
423                                isolate->factory()->NewStringFromAsciiChecked(
424                                    kMethodName)));
425   }
426 
427   size_t page_size = AllocatePageSize();
428   size_t new_committed_pages;
429   bool round_return_value =
430       RoundUpToPageSize(new_byte_length, page_size,
431                         JSArrayBuffer::kMaxByteLength, &new_committed_pages);
432   CHECK(round_return_value);
433 
434   // [RAB] Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength).
435   // [GSAB] Let hostHandled be ? HostGrowArrayBuffer(O, newByteLength).
436   // If hostHandled is handled, return undefined.
437 
438   // TODO(v8:11111): Wasm integration.
439 
440   if (!is_shared) {
441     // [RAB] Let oldBlock be O.[[ArrayBufferData]].
442     // [RAB] Let newBlock be ? CreateByteDataBlock(newByteLength).
443     // [RAB] Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]).
444     // [RAB] Perform CopyDataBlockBytes(newBlock, 0, oldBlock, 0, copyLength).
445     // [RAB] NOTE: Neither creation of the new Data Block nor copying from the
446     // old Data Block are observable. Implementations reserve the right to
447     // implement this method as in-place growth or shrinkage.
448     if (array_buffer->GetBackingStore()->ResizeInPlace(
449             isolate, new_byte_length, new_committed_pages * page_size) !=
450         BackingStore::ResizeOrGrowResult::kSuccess) {
451       THROW_NEW_ERROR_RETURN_FAILURE(
452           isolate, NewRangeError(MessageTemplate::kOutOfMemory,
453                                  isolate->factory()->NewStringFromAsciiChecked(
454                                      kMethodName)));
455     }
456     // [RAB] Set O.[[ArrayBufferByteLength]] to newLength.
457     array_buffer->set_byte_length(new_byte_length);
458   } else {
459     // [GSAB] (Detailed description of the algorithm omitted.)
460     auto result = array_buffer->GetBackingStore()->GrowInPlace(
461         isolate, new_byte_length, new_committed_pages * page_size);
462     if (result == BackingStore::ResizeOrGrowResult::kFailure) {
463       THROW_NEW_ERROR_RETURN_FAILURE(
464           isolate, NewRangeError(MessageTemplate::kOutOfMemory,
465                                  isolate->factory()->NewStringFromAsciiChecked(
466                                      kMethodName)));
467     }
468     if (result == BackingStore::ResizeOrGrowResult::kRace) {
469       THROW_NEW_ERROR_RETURN_FAILURE(
470           isolate,
471           NewRangeError(
472               MessageTemplate::kInvalidArrayBufferResizeLength,
473               isolate->factory()->NewStringFromAsciiChecked(kMethodName)));
474     }
475     // Invariant: byte_length for a GSAB is 0 (it needs to be read from the
476     // BackingStore).
477     CHECK_EQ(0, array_buffer->byte_length());
478   }
479   return ReadOnlyRoots(isolate).undefined_value();
480 }
481 
482 // ES #sec-get-sharedarraybuffer.prototype.bytelength
483 // get SharedArrayBuffer.prototype.byteLength
BUILTIN(SharedArrayBufferPrototypeGetByteLength)484 BUILTIN(SharedArrayBufferPrototypeGetByteLength) {
485   const char* const kMethodName = "get SharedArrayBuffer.prototype.byteLength";
486   HandleScope scope(isolate);
487   // 1. Let O be the this value.
488   // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
489   CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
490   // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
491   CHECK_SHARED(true, array_buffer, kMethodName);
492 
493   DCHECK_EQ(array_buffer->max_byte_length(),
494             array_buffer->GetBackingStore()->max_byte_length());
495 
496   // 4. Let length be ArrayBufferByteLength(O, SeqCst).
497   size_t byte_length = array_buffer->GetByteLength();
498   // 5. Return F(length).
499   return *isolate->factory()->NewNumberFromSize(byte_length);
500 }
501 
502 // ES #sec-arraybuffer.prototype.resize
503 // ArrayBuffer.prototype.resize(new_size))
BUILTIN(ArrayBufferPrototypeResize)504 BUILTIN(ArrayBufferPrototypeResize) {
505   const char* const kMethodName = "ArrayBuffer.prototype.resize";
506   constexpr bool kIsShared = false;
507   return ResizeHelper(args, isolate, kMethodName, kIsShared);
508 }
509 
510 // ES #sec-sharedarraybuffer.prototype.grow
511 // SharedArrayBuffer.prototype.grow(new_size))
BUILTIN(SharedArrayBufferPrototypeGrow)512 BUILTIN(SharedArrayBufferPrototypeGrow) {
513   const char* const kMethodName = "SharedArrayBuffer.prototype.grow";
514   constexpr bool kIsShared = true;
515   return ResizeHelper(args, isolate, kMethodName, kIsShared);
516 }
517 
518 }  // namespace internal
519 }  // namespace v8
520