diff --git a/deps/v8/include/v8-initialization.h b/deps/v8/include/v8-initialization.h index 46a21a02cbc..406f1d0ba54 100644 --- a/deps/v8/include/v8-initialization.h +++ b/deps/v8/include/v8-initialization.h @@ -253,6 +253,35 @@ class V8_EXPORT V8 { static size_t GetSandboxReservationSizeInBytes(); #endif // V8_ENABLE_SANDBOX + enum class WasmMemoryType { + kMemory32, + kMemory64, + }; + + /** + * Returns the virtual address space reservation size (in bytes) needed + * for one WebAssembly memory instance of the given capacity. + * + * \param type Whether this is a memory32 or memory64 instance. + * \param byte_capacity The maximum size, in bytes, of the WebAssembly + * memory. Values exceeding the engine's maximum allocatable memory + * size for the given type (determined by max_mem32_pages or + * max_mem64_pages) are clamped. + * + * When trap-based bounds checking is enabled by + * EnableWebAssemblyTrapHandler(), the amount of virtual address space + * that V8 needs to reserve for each WebAssembly memory instance can + * be much bigger than the requested size. If the process does + * not have enough virtual memory available, WebAssembly memory allocation + * would fail. During the initialization of V8, embedders can use this method + * to estimate whether the process has enough virtual memory for their + * usage of WebAssembly, and decide whether to enable the trap handler + * via EnableWebAssemblyTrapHandler(), or to skip it and reduce the amount of + * virtual memory required to keep the application running. + */ + static size_t GetWasmMemoryReservationSizeInBytes(WasmMemoryType type, + size_t byte_capacity); + /** * Activate trap-based bounds checking for WebAssembly. * diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index ef4546de275..7dc060bc87c 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -148,6 +148,7 @@ #include "src/wasm/value-type.h" #include "src/wasm/wasm-engine.h" #include "src/wasm/wasm-js.h" +#include "src/wasm/wasm-limits.h" #include "src/wasm/wasm-objects-inl.h" #include "src/wasm/wasm-result.h" #include "src/wasm/wasm-serialization.h" @@ -6504,6 +6505,26 @@ bool TryHandleWebAssemblyTrapWindows(EXCEPTION_POINTERS* exception) { } #endif +size_t V8::GetWasmMemoryReservationSizeInBytes(WasmMemoryType type, + size_t byte_capacity) { +#if V8_ENABLE_WEBASSEMBLY + bool is_memory64 = type == WasmMemoryType::kMemory64; + uint64_t max_byte_capacity = + is_memory64 ? i::wasm::max_mem64_bytes() : i::wasm::max_mem32_bytes(); + if (byte_capacity > max_byte_capacity) { + byte_capacity = static_cast(max_byte_capacity); + } +#if V8_TRAP_HANDLER_SUPPORTED + if (!is_memory64 || i::v8_flags.wasm_memory64_trap_handling) { + return i::BackingStore::GetWasmReservationSize( + /* has_guard_regions */ true, byte_capacity, + /* is_wasm_memory64 */ is_memory64); + } +#endif // V8_TRAP_HANDLER_SUPPORTED +#endif // V8_ENABLE_WEBASSEMBLY + return byte_capacity; +} + bool V8::EnableWebAssemblyTrapHandler(bool use_v8_signal_handler) { #if V8_ENABLE_WEBASSEMBLY return v8::internal::trap_handler::EnableTrapHandler(use_v8_signal_handler); diff --git a/deps/v8/src/objects/backing-store.cc b/deps/v8/src/objects/backing-store.cc index e75cddf03ae..680e99cdeb4 100644 --- a/deps/v8/src/objects/backing-store.cc +++ b/deps/v8/src/objects/backing-store.cc @@ -51,8 +51,25 @@ enum class AllocationStatus { kOtherFailure // Failed for an unknown reason }; -size_t GetReservationSize(bool has_guard_regions, size_t byte_capacity, - bool is_wasm_memory64) { +base::AddressRegion GetReservedRegion(bool has_guard_regions, + bool is_wasm_memory64, void* buffer_start, + size_t byte_capacity) { + return base::AddressRegion( + reinterpret_cast
(buffer_start), + BackingStore::GetWasmReservationSize(has_guard_regions, byte_capacity, + is_wasm_memory64)); +} + +void RecordStatus(Isolate* isolate, AllocationStatus status) { + isolate->counters()->wasm_memory_allocation_result()->AddSample( + static_cast(status)); +} + +} // namespace + +size_t BackingStore::GetWasmReservationSize(bool has_guard_regions, + size_t byte_capacity, + bool is_wasm_memory64) { #if V8_TARGET_ARCH_64_BIT && V8_ENABLE_WEBASSEMBLY DCHECK_IMPLIES(is_wasm_memory64 && has_guard_regions, v8_flags.wasm_memory64_trap_handling); @@ -73,21 +90,6 @@ size_t GetReservationSize(bool has_guard_regions, size_t byte_capacity, return byte_capacity; } -base::AddressRegion GetReservedRegion(bool has_guard_regions, - bool is_wasm_memory64, void* buffer_start, - size_t byte_capacity) { - return base::AddressRegion( - reinterpret_cast
(buffer_start), - GetReservationSize(has_guard_regions, byte_capacity, is_wasm_memory64)); -} - -void RecordStatus(Isolate* isolate, AllocationStatus status) { - isolate->counters()->wasm_memory_allocation_result()->AddSample( - static_cast(status)); -} - -} // namespace - // The backing store for a Wasm shared memory remembers all the isolates // with which it has been shared. struct SharedWasmMemoryData { @@ -161,7 +163,7 @@ BackingStore::~BackingStore() { #if V8_ENABLE_WEBASSEMBLY if (is_wasm_memory()) { - size_t reservation_size = GetReservationSize( + size_t reservation_size = GetWasmReservationSize( has_guard_regions(), byte_capacity_, is_wasm_memory64()); TRACE_BS( "BSw:free bs=%p mem=%p (length=%zu, capacity=%zu, reservation=%zu)\n", @@ -315,8 +317,8 @@ std::unique_ptr BackingStore::TryAllocateAndPartiallyCommitMemory( }; size_t byte_capacity = maximum_pages * page_size; - size_t reservation_size = - GetReservationSize(has_guard_regions, byte_capacity, is_wasm_memory64); + size_t reservation_size = GetWasmReservationSize( + has_guard_regions, byte_capacity, is_wasm_memory64); //-------------------------------------------------------------------------- // Allocate pages (inaccessible by default). diff --git a/deps/v8/src/objects/backing-store.h b/deps/v8/src/objects/backing-store.h index d587acbb2c3..42d26818760 100644 --- a/deps/v8/src/objects/backing-store.h +++ b/deps/v8/src/objects/backing-store.h @@ -183,6 +183,11 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase { uint32_t id() const { return id_; } + // Return the size of the reservation needed for a wasm backing store. + static size_t GetWasmReservationSize(bool has_guard_regions, + size_t byte_capacity, + bool is_wasm_memory64); + private: friend class GlobalBackingStoreRegistry; diff --git a/deps/v8/test/unittests/api/api-wasm-unittest.cc b/deps/v8/test/unittests/api/api-wasm-unittest.cc index eceef8a5423..6d8b84fff2a 100644 --- a/deps/v8/test/unittests/api/api-wasm-unittest.cc +++ b/deps/v8/test/unittests/api/api-wasm-unittest.cc @@ -122,6 +122,22 @@ void WasmStreamingMoreFunctionsCanBeSerializedCallback( streaming->SetMoreFunctionsCanBeSerializedCallback([](CompiledWasmModule) {}); } +TEST_F(ApiWasmTest, GetWasmMemoryReservationSizeInBytes) { + constexpr size_t kCapacity = 64 * 1024; // 64 KiB + size_t reservation = V8::GetWasmMemoryReservationSizeInBytes( + V8::WasmMemoryType::kMemory32, kCapacity); + size_t reservation64 = V8::GetWasmMemoryReservationSizeInBytes( + V8::WasmMemoryType::kMemory64, kCapacity); + +#if V8_TRAP_HANDLER_SUPPORTED + EXPECT_GE(reservation, kCapacity); + EXPECT_GE(reservation64, kCapacity); +#else + EXPECT_EQ(reservation, kCapacity); + EXPECT_EQ(reservation64, kCapacity); +#endif // V8_TRAP_HANDLER_SUPPORTED +} + TEST_F(ApiWasmTest, WasmStreamingCallback) { TestWasmStreaming(WasmStreamingCallbackTestCallbackIsCalled, Promise::kPending);