js-array-buffer-inl.h 13.3 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 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.

#ifndef V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_
#define V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_

8
#include "src/heap/heap-write-barrier-inl.h"
9
#include "src/objects/js-array-buffer.h"
10
#include "src/objects/js-objects-inl.h"
11
#include "src/objects/objects-inl.h"
12 13 14 15 16 17 18

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

19 20
#include "torque-generated/src/objects/js-array-buffer-tq-inl.inc"

21 22 23 24
TQ_OBJECT_CONSTRUCTORS_IMPL(JSArrayBuffer)
TQ_OBJECT_CONSTRUCTORS_IMPL(JSArrayBufferView)
TQ_OBJECT_CONSTRUCTORS_IMPL(JSTypedArray)
TQ_OBJECT_CONSTRUCTORS_IMPL(JSDataView)
25

26 27 28 29
ACCESSORS(JSTypedArray, base_pointer, Object, kBasePointerOffset)
RELEASE_ACQUIRE_ACCESSORS(JSTypedArray, base_pointer, Object,
                          kBasePointerOffset)

30
size_t JSArrayBuffer::byte_length() const {
31
  return ReadField<size_t>(kByteLengthOffset);
32 33 34
}

void JSArrayBuffer::set_byte_length(size_t value) {
35
  WriteField<size_t>(kByteLengthOffset, value);
36 37
}

38
DEF_GETTER(JSArrayBuffer, backing_store, void*) {
Samuel Groß's avatar
Samuel Groß committed
39
  Address value = ReadSandboxedPointerField(kBackingStoreOffset, cage_base);
40
  return reinterpret_cast<void*>(value);
41 42
}

43 44
void JSArrayBuffer::set_backing_store(Isolate* isolate, void* value) {
  Address addr = reinterpret_cast<Address>(value);
Samuel Groß's avatar
Samuel Groß committed
45
  WriteSandboxedPointerField(kBackingStoreOffset, isolate, addr);
46 47
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
std::shared_ptr<BackingStore> JSArrayBuffer::GetBackingStore() const {
  if (!extension()) return nullptr;
  return extension()->backing_store();
}

size_t JSArrayBuffer::GetByteLength() const {
  if V8_UNLIKELY (is_shared() && is_resizable()) {
    // Invariant: byte_length for GSAB is 0 (it needs to be read from the
    // BackingStore).
    DCHECK_EQ(0, byte_length());

    return GetBackingStore()->byte_length(std::memory_order_seq_cst);
  }
  return byte_length();
}

64
uint32_t JSArrayBuffer::GetBackingStoreRefForDeserialization() const {
65
  return static_cast<uint32_t>(ReadField<Address>(kBackingStoreOffset));
66 67 68
}

void JSArrayBuffer::SetBackingStoreRefForSerialization(uint32_t ref) {
69
  WriteField<Address>(kBackingStoreOffset, static_cast<Address>(ref));
70 71
}

72
ArrayBufferExtension* JSArrayBuffer::extension() const {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#if V8_COMPRESS_POINTERS
    // With pointer compression the extension-field might not be
    // pointer-aligned. However on ARM64 this field needs to be aligned to
    // perform atomic operations on it. Therefore we split the pointer into two
    // 32-bit words that we update atomically. We don't have an ABA problem here
    // since there can never be an Attach() after Detach() (transitions only
    // from NULL --> some ptr --> NULL).

    // Synchronize with publishing release store of non-null extension
    uint32_t lo = base::AsAtomic32::Acquire_Load(extension_lo());
    if (lo & kUninitializedTagMask) return nullptr;

    // Synchronize with release store of null extension
    uint32_t hi = base::AsAtomic32::Acquire_Load(extension_hi());
    uint32_t verify_lo = base::AsAtomic32::Relaxed_Load(extension_lo());
    if (lo != verify_lo) return nullptr;

    uintptr_t address = static_cast<uintptr_t>(lo);
    address |= static_cast<uintptr_t>(hi) << 32;
    return reinterpret_cast<ArrayBufferExtension*>(address);
#else
94
    return base::AsAtomicPointer::Acquire_Load(extension_location());
95
#endif
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
void JSArrayBuffer::set_extension(ArrayBufferExtension* extension) {
#if V8_COMPRESS_POINTERS
    if (extension != nullptr) {
      uintptr_t address = reinterpret_cast<uintptr_t>(extension);
      base::AsAtomic32::Relaxed_Store(extension_hi(),
                                      static_cast<uint32_t>(address >> 32));
      base::AsAtomic32::Release_Store(extension_lo(),
                                      static_cast<uint32_t>(address));
    } else {
      base::AsAtomic32::Relaxed_Store(extension_lo(),
                                      0 | kUninitializedTagMask);
      base::AsAtomic32::Release_Store(extension_hi(), 0);
    }
#else
    base::AsAtomicPointer::Release_Store(extension_location(), extension);
#endif
114
    WriteBarrier::Marking(*this, extension);
115 116
}

117 118 119 120 121
ArrayBufferExtension** JSArrayBuffer::extension_location() const {
  Address location = field_address(kExtensionOffset);
  return reinterpret_cast<ArrayBufferExtension**>(location);
}

122 123 124 125 126 127 128 129 130
#if V8_COMPRESS_POINTERS
uint32_t* JSArrayBuffer::extension_lo() const {
  Address location = field_address(kExtensionOffset);
  return reinterpret_cast<uint32_t*>(location);
}

uint32_t* JSArrayBuffer::extension_hi() const {
  Address location = field_address(kExtensionOffset) + sizeof(uint32_t);
  return reinterpret_cast<uint32_t*>(location);
131
}
132
#endif
133

134
void JSArrayBuffer::clear_padding() {
135
  if (FIELD_SIZE(kOptionalPaddingOffset) != 0) {
136 137 138
    DCHECK_EQ(4, FIELD_SIZE(kOptionalPaddingOffset));
    memset(reinterpret_cast<void*>(address() + kOptionalPaddingOffset), 0,
           FIELD_SIZE(kOptionalPaddingOffset));
139
  }
140 141 142
}

void JSArrayBuffer::set_bit_field(uint32_t bits) {
143
  RELAXED_WRITE_UINT32_FIELD(*this, kBitFieldOffset, bits);
144 145 146
}

uint32_t JSArrayBuffer::bit_field() const {
147
  return RELAXED_READ_UINT32_FIELD(*this, kBitFieldOffset);
148 149
}

150 151 152
// |bit_field| fields.
BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_external,
                    JSArrayBuffer::IsExternalBit)
153 154 155 156
BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_detachable,
                    JSArrayBuffer::IsDetachableBit)
BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, was_detached,
                    JSArrayBuffer::WasDetachedBit)
157 158
BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_asmjs_memory,
                    JSArrayBuffer::IsAsmJsMemoryBit)
159 160
BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_shared,
                    JSArrayBuffer::IsSharedBit)
161 162
BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_resizable,
                    JSArrayBuffer::IsResizableBit)
163

164 165 166 167 168 169 170
bool JSArrayBuffer::IsEmpty() const {
  auto backing_store = GetBackingStore();
  bool is_empty = !backing_store || backing_store->IsEmpty();
  DCHECK_IMPLIES(is_empty, byte_length() == 0);
  return is_empty;
}

171
size_t JSArrayBufferView::byte_offset() const {
172
  return ReadField<size_t>(kByteOffsetOffset);
173 174
}

175
void JSArrayBufferView::set_byte_offset(size_t value) {
176
  WriteField<size_t>(kByteOffsetOffset, value);
177 178
}

179
size_t JSArrayBufferView::byte_length() const {
180
  return ReadField<size_t>(kByteLengthOffset);
181 182
}

183
void JSArrayBufferView::set_byte_length(size_t value) {
184
  WriteField<size_t>(kByteLengthOffset, value);
185 186
}

187
bool JSArrayBufferView::WasDetached() const {
188
  return JSArrayBuffer::cast(buffer()).was_detached();
189 190
}

191 192 193 194
BIT_FIELD_ACCESSORS(JSArrayBufferView, bit_field, is_length_tracking,
                    JSArrayBufferView::IsLengthTrackingBit)
BIT_FIELD_ACCESSORS(JSArrayBufferView, bit_field, is_backed_by_rab,
                    JSArrayBufferView::IsBackedByRabBit)
195

196
bool JSArrayBufferView::IsVariableLength() const {
197 198 199
  return is_length_tracking() || is_backed_by_rab();
}

200 201
size_t JSTypedArray::GetLengthOrOutOfBounds(bool& out_of_bounds) const {
  DCHECK(!out_of_bounds);
202
  if (WasDetached()) return 0;
203 204
  if (IsVariableLength()) {
    return GetVariableLengthOrOutOfBounds(out_of_bounds);
205
  }
206
  return LengthUnchecked();
207 208
}

209 210 211 212 213
size_t JSTypedArray::GetLength() const {
  bool out_of_bounds = false;
  return GetLengthOrOutOfBounds(out_of_bounds);
}

214 215 216 217 218 219 220 221 222 223
size_t JSTypedArray::GetByteLength() const {
  return GetLength() * element_size();
}

bool JSTypedArray::IsOutOfBounds() const {
  bool out_of_bounds = false;
  GetLengthOrOutOfBounds(out_of_bounds);
  return out_of_bounds;
}

224 225 226 227 228 229 230 231 232
bool JSTypedArray::IsDetachedOrOutOfBounds() const {
  if (WasDetached()) {
    return true;
  }
  bool out_of_bounds = false;
  GetLengthOrOutOfBounds(out_of_bounds);
  return out_of_bounds;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
// static
inline void JSTypedArray::ForFixedTypedArray(ExternalArrayType array_type,
                                             size_t* element_size,
                                             ElementsKind* element_kind) {
  switch (array_type) {
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype) \
  case kExternal##Type##Array:                    \
    *element_size = sizeof(ctype);                \
    *element_kind = TYPE##_ELEMENTS;              \
    return;

    TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE
  }
  UNREACHABLE();
}

250 251 252 253 254 255 256 257 258
size_t JSTypedArray::length() const {
  DCHECK(!is_length_tracking());
  DCHECK(!is_backed_by_rab());
  return ReadField<size_t>(kLengthOffset);
}

size_t JSTypedArray::LengthUnchecked() const {
  return ReadField<size_t>(kLengthOffset);
}
259

260
void JSTypedArray::set_length(size_t value) {
261
  WriteField<size_t>(kLengthOffset, value);
262 263
}

264
DEF_GETTER(JSTypedArray, external_pointer, Address) {
Samuel Groß's avatar
Samuel Groß committed
265
  return ReadSandboxedPointerField(kExternalPointerOffset, cage_base);
266 267
}

268
void JSTypedArray::set_external_pointer(Isolate* isolate, Address value) {
Samuel Groß's avatar
Samuel Groß committed
269
  WriteSandboxedPointerField(kExternalPointerOffset, isolate, value);
270 271 272
}

Address JSTypedArray::ExternalPointerCompensationForOnHeapArray(
273
    PtrComprCageBase cage_base) {
274
#ifdef V8_COMPRESS_POINTERS
275
  return cage_base.address();
276 277 278 279 280
#else
  return 0;
#endif
}

281 282
uint32_t JSTypedArray::GetExternalBackingStoreRefForDeserialization() const {
  DCHECK(!is_on_heap());
283
  return static_cast<uint32_t>(ReadField<Address>(kExternalPointerOffset));
284 285 286 287
}

void JSTypedArray::SetExternalBackingStoreRefForSerialization(uint32_t ref) {
  DCHECK(!is_on_heap());
288
  WriteField<Address>(kExternalPointerOffset, static_cast<Address>(ref));
289 290 291 292
}

void JSTypedArray::RemoveExternalPointerCompensationForSerialization(
    Isolate* isolate) {
293
  DCHECK(is_on_heap());
294 295
  Address offset =
      external_pointer() - ExternalPointerCompensationForOnHeapArray(isolate);
296 297 298 299 300 301 302 303 304
  WriteField<Address>(kExternalPointerOffset, offset);
}

void JSTypedArray::AddExternalPointerCompensationForDeserialization(
    Isolate* isolate) {
  DCHECK(is_on_heap());
  Address pointer = ReadField<Address>(kExternalPointerOffset) +
                    ExternalPointerCompensationForOnHeapArray(isolate);
  set_external_pointer(isolate, pointer);
305 306 307
}

void* JSTypedArray::DataPtr() {
308
  // Zero-extend Tagged_t to Address according to current compression scheme
309 310 311
  // so that the addition with |external_pointer| (which already contains
  // compensated offset value) will decompress the tagged value.
  // See JSTypedArray::ExternalPointerCompensationForOnHeapArray() for details.
312
  static_assert(kOffHeapDataPtrEqualsExternalPointer);
313 314
  return reinterpret_cast<void*>(external_pointer() +
                                 static_cast<Tagged_t>(base_pointer().ptr()));
315 316
}

317 318
void JSTypedArray::SetOffHeapDataPtr(Isolate* isolate, void* base,
                                     Address offset) {
319
  Address address = reinterpret_cast<Address>(base) + offset;
320
  set_external_pointer(isolate, address);
321 322 323 324 325 326 327
  // This is the only spot in which the `base_pointer` field can be mutated
  // after object initialization. Note this can happen at most once, when
  // `JSTypedArray::GetBuffer` transitions from an on- to off-heap
  // representation.
  // To play well with Turbofan concurrency requirements, `base_pointer` is set
  // with a release store, after external_pointer has been set.
  set_base_pointer(Smi::zero(), kReleaseStore, SKIP_WRITE_BARRIER);
328 329 330
  DCHECK_EQ(address, reinterpret_cast<Address>(DataPtr()));
}

331
bool JSTypedArray::is_on_heap() const {
332 333 334 335 336 337 338 339 340
  // Keep synced with `is_on_heap(AcquireLoadTag)`.
  DisallowGarbageCollection no_gc;
  return base_pointer() != Smi::zero();
}

bool JSTypedArray::is_on_heap(AcquireLoadTag tag) const {
  // Keep synced with `is_on_heap()`.
  // Note: For Turbofan concurrency requirements, it's important that this
  // function reads only `base_pointer`.
341
  DisallowGarbageCollection no_gc;
342
  return base_pointer(tag) != Smi::zero();
343 344 345 346 347 348 349
}

// static
MaybeHandle<JSTypedArray> JSTypedArray::Validate(Isolate* isolate,
                                                 Handle<Object> receiver,
                                                 const char* method_name) {
  if (V8_UNLIKELY(!receiver->IsJSTypedArray())) {
350
    const MessageTemplate message = MessageTemplate::kNotTypedArray;
351 352 353 354
    THROW_NEW_ERROR(isolate, NewTypeError(message), JSTypedArray);
  }

  Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver);
355
  if (V8_UNLIKELY(array->WasDetached())) {
356
    const MessageTemplate message = MessageTemplate::kDetachedOperation;
357 358 359 360 361
    Handle<String> operation =
        isolate->factory()->NewStringFromAsciiChecked(method_name);
    THROW_NEW_ERROR(isolate, NewTypeError(message, operation), JSTypedArray);
  }

362 363 364 365 366
  if (V8_UNLIKELY(array->IsVariableLength() && array->IsOutOfBounds())) {
    const MessageTemplate message = MessageTemplate::kDetachedOperation;
    Handle<String> operation =
        isolate->factory()->NewStringFromAsciiChecked(method_name);
    THROW_NEW_ERROR(isolate, NewTypeError(message, operation), JSTypedArray);
367 368
  }

369 370 371 372 373
  // spec describes to return `buffer`, but it may disrupt current
  // implementations, and it's much useful to return array for now.
  return array;
}

374
DEF_GETTER(JSDataView, data_pointer, void*) {
Samuel Groß's avatar
Samuel Groß committed
375
  Address value = ReadSandboxedPointerField(kDataPointerOffset, cage_base);
376
  return reinterpret_cast<void*>(value);
377 378
}

379 380
void JSDataView::set_data_pointer(Isolate* isolate, void* ptr) {
  Address value = reinterpret_cast<Address>(ptr);
Samuel Groß's avatar
Samuel Groß committed
381
  WriteSandboxedPointerField(kDataPointerOffset, isolate, value);
382 383
}

384 385 386 387 388 389
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_