Skip to content

Commit 1e7effd

Browse files
marjakhV8 LUCI CQ
authored andcommitted
[rab/gsab] Fix gsab maxByteLength after transferring to worker
Bug: v8:11111 Change-Id: I41a318d3858e48035ae67e937420e2963a13d871 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3035091 Commit-Queue: Marja Hölttä <[email protected]> Reviewed-by: Shu-yu Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#75878}
1 parent 66856ba commit 1e7effd

5 files changed

Lines changed: 46 additions & 24 deletions

File tree

src/builtins/builtins-arraybuffer.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ Object ConstructBuffer(Isolate* isolate, Handle<JSFunction> target,
108108
}
109109
constexpr bool kIsWasmMemory = false;
110110
backing_store = BackingStore::TryAllocateAndPartiallyCommitMemory(
111-
isolate, byte_length, page_size, initial_pages, max_pages,
112-
kIsWasmMemory, shared);
111+
isolate, byte_length, max_byte_length, page_size, initial_pages,
112+
max_pages, kIsWasmMemory, shared);
113113
}
114114
if (!backing_store) {
115115
// Allocation of backing store failed.
@@ -475,6 +475,9 @@ BUILTIN(SharedArrayBufferPrototypeGetByteLength) {
475475
// 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
476476
CHECK_SHARED(true, array_buffer, kMethodName);
477477

478+
DCHECK_EQ(array_buffer->max_byte_length(),
479+
array_buffer->GetBackingStore()->max_byte_length());
480+
478481
// 4. Let length be ArrayBufferByteLength(O, SeqCst).
479482
size_t byte_length;
480483
if (array_buffer->is_resizable()) {

src/objects/backing-store.cc

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ std::unique_ptr<BackingStore> BackingStore::Allocate(
267267

268268
auto result = new BackingStore(buffer_start, // start
269269
byte_length, // length
270+
byte_length, // max length
270271
byte_length, // capacity
271272
shared, // shared
272273
ResizableFlag::kNotResizable, // resizable
@@ -305,8 +306,9 @@ std::unique_ptr<BackingStore> BackingStore::TryAllocateWasmMemory(
305306
maximum_pages = std::min(engine_max_pages, maximum_pages);
306307

307308
auto result = TryAllocateAndPartiallyCommitMemory(
308-
isolate, initial_pages * wasm::kWasmPageSize, wasm::kWasmPageSize,
309-
initial_pages, maximum_pages, true, shared);
309+
isolate, initial_pages * wasm::kWasmPageSize,
310+
maximum_pages * wasm::kWasmPageSize, wasm::kWasmPageSize, initial_pages,
311+
maximum_pages, true, shared);
310312
// Shared Wasm memories need an anchor for the memory object list.
311313
if (result && shared == SharedFlag::kShared) {
312314
result->type_specific_data_.shared_wasm_memory_data =
@@ -336,9 +338,9 @@ void BackingStore::ReleaseReservation(uint64_t num_bytes) {
336338
}
337339

338340
std::unique_ptr<BackingStore> BackingStore::TryAllocateAndPartiallyCommitMemory(
339-
Isolate* isolate, size_t byte_length, size_t page_size,
340-
size_t initial_pages, size_t maximum_pages, bool is_wasm_memory,
341-
SharedFlag shared) {
341+
Isolate* isolate, size_t byte_length, size_t max_byte_length,
342+
size_t page_size, size_t initial_pages, size_t maximum_pages,
343+
bool is_wasm_memory, SharedFlag shared) {
342344
// Enforce engine limitation on the maximum number of pages.
343345
if (maximum_pages > std::numeric_limits<size_t>::max() / page_size) {
344346
return nullptr;
@@ -445,16 +447,17 @@ std::unique_ptr<BackingStore> BackingStore::TryAllocateAndPartiallyCommitMemory(
445447
ResizableFlag resizable =
446448
is_wasm_memory ? ResizableFlag::kNotResizable : ResizableFlag::kResizable;
447449

448-
auto result = new BackingStore(buffer_start, // start
449-
byte_length, // length
450-
byte_capacity, // capacity
451-
shared, // shared
452-
resizable, // resizable
453-
is_wasm_memory, // is_wasm_memory
454-
true, // free_on_destruct
455-
guards, // has_guard_regions
456-
false, // custom_deleter
457-
false); // empty_deleter
450+
auto result = new BackingStore(buffer_start, // start
451+
byte_length, // length
452+
max_byte_length, // max_byte_length
453+
byte_capacity, // capacity
454+
shared, // shared
455+
resizable, // resizable
456+
is_wasm_memory, // is_wasm_memory
457+
true, // free_on_destruct
458+
guards, // has_guard_regions
459+
false, // custom_deleter
460+
false); // empty_deleter
458461

459462
TRACE_BS(
460463
"BSw:alloc bs=%p mem=%p (length=%zu, capacity=%zu, reservation=%zu)\n",
@@ -707,6 +710,7 @@ std::unique_ptr<BackingStore> BackingStore::WrapAllocation(
707710
SharedFlag shared, bool free_on_destruct) {
708711
auto result = new BackingStore(allocation_base, // start
709712
allocation_length, // length
713+
allocation_length, // max length
710714
allocation_length, // capacity
711715
shared, // shared
712716
ResizableFlag::kNotResizable, // resizable
@@ -728,6 +732,7 @@ std::unique_ptr<BackingStore> BackingStore::WrapAllocation(
728732
bool is_empty_deleter = (deleter == v8::BackingStore::EmptyDeleter);
729733
auto result = new BackingStore(allocation_base, // start
730734
allocation_length, // length
735+
allocation_length, // max length
731736
allocation_length, // capacity
732737
shared, // shared
733738
ResizableFlag::kNotResizable, // resizable
@@ -746,6 +751,7 @@ std::unique_ptr<BackingStore> BackingStore::EmptyBackingStore(
746751
SharedFlag shared) {
747752
auto result = new BackingStore(nullptr, // start
748753
0, // length
754+
0, // max length
749755
0, // capacity
750756
shared, // shared
751757
ResizableFlag::kNotResizable, // resizable

src/objects/backing-store.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase {
6161

6262
// Tries to allocate `maximum_pages` of memory and commit `initial_pages`.
6363
static std::unique_ptr<BackingStore> TryAllocateAndPartiallyCommitMemory(
64-
Isolate* isolate, size_t byte_length, size_t page_size,
65-
size_t initial_pages, size_t maximum_pages, bool is_wasm_memory,
66-
SharedFlag shared);
64+
Isolate* isolate, size_t byte_length, size_t max_byte_length,
65+
size_t page_size, size_t initial_pages, size_t maximum_pages,
66+
bool is_wasm_memory, SharedFlag shared);
6767

6868
// Create a backing store that wraps existing allocated memory.
6969
// If {free_on_destruct} is {true}, the memory will be freed using the
@@ -90,6 +90,7 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase {
9090
std::memory_order memory_order = std::memory_order_relaxed) const {
9191
return byte_length_.load(memory_order);
9292
}
93+
size_t max_byte_length() const { return max_byte_length_; }
9394
size_t byte_capacity() const { return byte_capacity_; }
9495
bool is_shared() const { return is_shared_; }
9596
bool is_resizable() const { return is_resizable_; }
@@ -165,12 +166,13 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase {
165166
private:
166167
friend class GlobalBackingStoreRegistry;
167168

168-
BackingStore(void* buffer_start, size_t byte_length, size_t byte_capacity,
169-
SharedFlag shared, ResizableFlag resizable, bool is_wasm_memory,
170-
bool free_on_destruct, bool has_guard_regions,
171-
bool custom_deleter, bool empty_deleter)
169+
BackingStore(void* buffer_start, size_t byte_length, size_t max_byte_length,
170+
size_t byte_capacity, SharedFlag shared, ResizableFlag resizable,
171+
bool is_wasm_memory, bool free_on_destruct,
172+
bool has_guard_regions, bool custom_deleter, bool empty_deleter)
172173
: buffer_start_(buffer_start),
173174
byte_length_(byte_length),
175+
max_byte_length_(max_byte_length),
174176
byte_capacity_(byte_capacity),
175177
is_shared_(shared == SharedFlag::kShared),
176178
is_resizable_(resizable == ResizableFlag::kResizable),
@@ -185,13 +187,18 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase {
185187
DCHECK_IMPLIES(is_wasm_memory_, !is_resizable_);
186188
DCHECK_IMPLIES(is_resizable_, !custom_deleter_);
187189
DCHECK_IMPLIES(is_resizable_, free_on_destruct_);
190+
DCHECK_IMPLIES(!is_wasm_memory && !is_resizable_,
191+
byte_length_ == max_byte_length_);
188192
}
189193
BackingStore(const BackingStore&) = delete;
190194
BackingStore& operator=(const BackingStore&) = delete;
191195
void SetAllocatorFromIsolate(Isolate* isolate);
192196

193197
void* buffer_start_ = nullptr;
194198
std::atomic<size_t> byte_length_{0};
199+
// Max byte length of the corresponding JSArrayBuffer(s).
200+
size_t max_byte_length_ = 0;
201+
// Amount of the memory allocated
195202
size_t byte_capacity_ = 0;
196203

197204
struct DeleterInfo {

src/objects/js-array-buffer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void JSArrayBuffer::Setup(SharedFlag shared, ResizableFlag resizable,
5959
if (!backing_store) {
6060
set_backing_store(GetIsolate(), nullptr);
6161
set_byte_length(0);
62+
set_max_byte_length(0);
6263
} else {
6364
Attach(std::move(backing_store));
6465
}
@@ -72,6 +73,9 @@ void JSArrayBuffer::Attach(std::shared_ptr<BackingStore> backing_store) {
7273
DCHECK_NOT_NULL(backing_store);
7374
DCHECK_EQ(is_shared(), backing_store->is_shared());
7475
DCHECK_EQ(is_resizable(), backing_store->is_resizable());
76+
DCHECK_IMPLIES(
77+
!backing_store->is_wasm_memory() && !backing_store->is_resizable(),
78+
backing_store->byte_length() == backing_store->max_byte_length());
7579
DCHECK(!was_detached());
7680
Isolate* isolate = GetIsolate();
7781
set_backing_store(isolate, backing_store->buffer_start());
@@ -82,6 +86,7 @@ void JSArrayBuffer::Attach(std::shared_ptr<BackingStore> backing_store) {
8286
} else {
8387
set_byte_length(backing_store->byte_length());
8488
}
89+
set_max_byte_length(backing_store->max_byte_length());
8590
if (backing_store->is_wasm_memory()) set_is_detachable(false);
8691
if (!backing_store->free_on_destruct()) set_is_external(true);
8792
Heap* heap = isolate->heap();

test/mjsunit/resizablearraybuffer-growablesharedarraybuffer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ const ctors = [[ArrayBuffer, (b) => b.resizable],
534534
assert(!(gsab instanceof ArrayBuffer));
535535
assert(gsab instanceof SharedArrayBuffer);
536536
assert(10 == gsab.byteLength);
537+
assert(20 == gsab.maxByteLength);
537538
gsab.grow(15);
538539
postMessage('ok');
539540
}

0 commit comments

Comments
 (0)