Commit fcdf35e6 authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

Skip global registration of [Shared]ArrayBuffer backing stores

Previously we needed to register the backing stores globally because
the embedder could create them from a raw pointer. This is no longer
possible after the removal of the old API.

The global backing store registry now keeps track only of wasm memory
backing stores.

Bug: v8:9380
Change-Id: Iffefbf14dcafc1f9ce0dc3613335c754c9cb649a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2763874Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73493}
parent 63661ce7
...@@ -3785,7 +3785,6 @@ std::shared_ptr<v8::BackingStore> v8::ArrayBuffer::GetBackingStore() { ...@@ -3785,7 +3785,6 @@ std::shared_ptr<v8::BackingStore> v8::ArrayBuffer::GetBackingStore() {
backing_store = backing_store =
i::BackingStore::EmptyBackingStore(i::SharedFlag::kNotShared); i::BackingStore::EmptyBackingStore(i::SharedFlag::kNotShared);
} }
i::GlobalBackingStoreRegistry::Register(backing_store);
std::shared_ptr<i::BackingStoreBase> bs_base = backing_store; std::shared_ptr<i::BackingStoreBase> bs_base = backing_store;
return std::static_pointer_cast<v8::BackingStore>(bs_base); return std::static_pointer_cast<v8::BackingStore>(bs_base);
} }
...@@ -3796,7 +3795,6 @@ std::shared_ptr<v8::BackingStore> v8::SharedArrayBuffer::GetBackingStore() { ...@@ -3796,7 +3795,6 @@ std::shared_ptr<v8::BackingStore> v8::SharedArrayBuffer::GetBackingStore() {
if (!backing_store) { if (!backing_store) {
backing_store = i::BackingStore::EmptyBackingStore(i::SharedFlag::kShared); backing_store = i::BackingStore::EmptyBackingStore(i::SharedFlag::kShared);
} }
i::GlobalBackingStoreRegistry::Register(backing_store);
std::shared_ptr<i::BackingStoreBase> bs_base = backing_store; std::shared_ptr<i::BackingStoreBase> bs_base = backing_store;
return std::static_pointer_cast<v8::BackingStore>(bs_base); return std::static_pointer_cast<v8::BackingStore>(bs_base);
} }
......
...@@ -685,17 +685,8 @@ inline GlobalBackingStoreRegistryImpl* impl() { ...@@ -685,17 +685,8 @@ inline GlobalBackingStoreRegistryImpl* impl() {
void GlobalBackingStoreRegistry::Register( void GlobalBackingStoreRegistry::Register(
std::shared_ptr<BackingStore> backing_store) { std::shared_ptr<BackingStore> backing_store) {
if (!backing_store || !backing_store->buffer_start()) return; if (!backing_store || !backing_store->buffer_start()) return;
// Only wasm memory backing stores need to be registered globally.
if (!backing_store->free_on_destruct()) { CHECK(backing_store->is_wasm_memory());
// If the backing store buffer is managed by the embedder,
// then we don't have to guarantee that there is single unique
// BackingStore per buffer_start() because the destructor of
// of the BackingStore will be a no-op in that case.
// All Wasm memory has to be registered.
CHECK(!backing_store->is_wasm_memory());
return;
}
base::MutexGuard scope_lock(&impl()->mutex_); base::MutexGuard scope_lock(&impl()->mutex_);
if (backing_store->globally_registered_) return; if (backing_store->globally_registered_) return;
...@@ -711,6 +702,8 @@ void GlobalBackingStoreRegistry::Register( ...@@ -711,6 +702,8 @@ void GlobalBackingStoreRegistry::Register(
void GlobalBackingStoreRegistry::Unregister(BackingStore* backing_store) { void GlobalBackingStoreRegistry::Unregister(BackingStore* backing_store) {
if (!backing_store->globally_registered_) return; if (!backing_store->globally_registered_) return;
CHECK(backing_store->is_wasm_memory());
DCHECK_NOT_NULL(backing_store->buffer_start()); DCHECK_NOT_NULL(backing_store->buffer_start());
base::MutexGuard scope_lock(&impl()->mutex_); base::MutexGuard scope_lock(&impl()->mutex_);
...@@ -722,26 +715,6 @@ void GlobalBackingStoreRegistry::Unregister(BackingStore* backing_store) { ...@@ -722,26 +715,6 @@ void GlobalBackingStoreRegistry::Unregister(BackingStore* backing_store) {
backing_store->globally_registered_ = false; backing_store->globally_registered_ = false;
} }
std::shared_ptr<BackingStore> GlobalBackingStoreRegistry::Lookup(
void* buffer_start, size_t length) {
base::MutexGuard scope_lock(&impl()->mutex_);
TRACE_BS("BS:lookup mem=%p (%zu bytes)\n", buffer_start, length);
const auto& result = impl()->map_.find(buffer_start);
if (result == impl()->map_.end()) {
return std::shared_ptr<BackingStore>();
}
auto backing_store = result->second.lock();
CHECK_EQ(buffer_start, backing_store->buffer_start());
if (backing_store->is_wasm_memory()) {
// Grow calls to shared WebAssembly threads can be triggered from different
// workers, length equality cannot be guaranteed here.
CHECK_LE(length, backing_store->byte_length());
} else {
CHECK_EQ(length, backing_store->byte_length());
}
return backing_store;
}
void GlobalBackingStoreRegistry::Purge(Isolate* isolate) { void GlobalBackingStoreRegistry::Purge(Isolate* isolate) {
// We need to keep a reference to all backing stores that are inspected // We need to keep a reference to all backing stores that are inspected
// in the purging loop below. Otherwise, we might get a deadlock // in the purging loop below. Otherwise, we might get a deadlock
...@@ -755,7 +728,7 @@ void GlobalBackingStoreRegistry::Purge(Isolate* isolate) { ...@@ -755,7 +728,7 @@ void GlobalBackingStoreRegistry::Purge(Isolate* isolate) {
auto backing_store = entry.second.lock(); auto backing_store = entry.second.lock();
prevent_destruction_under_lock.emplace_back(backing_store); prevent_destruction_under_lock.emplace_back(backing_store);
if (!backing_store) continue; // skip entries where weak ptr is null if (!backing_store) continue; // skip entries where weak ptr is null
if (!backing_store->is_wasm_memory()) continue; // skip non-wasm memory CHECK(backing_store->is_wasm_memory());
if (!backing_store->is_shared()) continue; // skip non-shared memory if (!backing_store->is_shared()) continue; // skip non-shared memory
SharedWasmMemoryData* shared_data = SharedWasmMemoryData* shared_data =
backing_store->get_shared_wasm_memory_data(); backing_store->get_shared_wasm_memory_data();
......
...@@ -219,21 +219,16 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase { ...@@ -219,21 +219,16 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase {
#endif // V8_ENABLE_WEBASSEMBLY #endif // V8_ENABLE_WEBASSEMBLY
}; };
// A global, per-process mapping from buffer addresses to backing stores. // A global, per-process mapping from buffer addresses to backing stores
// This is generally only used for dealing with an embedder that has not // of wasm memory objects.
// migrated to the new API which should use proper pointers to manage
// backing stores.
class GlobalBackingStoreRegistry { class GlobalBackingStoreRegistry {
public: public:
// Register a backing store in the global registry. A mapping from the // Register a backing store in the global registry. A mapping from the
// {buffer_start} to the backing store object will be added. The backing // {buffer_start} to the backing store object will be added. The backing
// store will automatically unregister itself upon destruction. // store will automatically unregister itself upon destruction.
// Only wasm memory backing stores are supported.
static void Register(std::shared_ptr<BackingStore> backing_store); static void Register(std::shared_ptr<BackingStore> backing_store);
// Look up a backing store based on the {buffer_start} pointer.
static std::shared_ptr<BackingStore> Lookup(void* buffer_start,
size_t length);
private: private:
friend class BackingStore; friend class BackingStore;
// Unregister a backing store in the global registry. // Unregister a backing store in the global registry.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment