| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6e2f7e6 commit 8b37af8
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -253,6 +253,35 @@ class V8_EXPORT V8 { | |||
| 253 | 253 | static size_t GetSandboxReservationSizeInBytes(); | |
| 254 | 254 | #endif // V8_ENABLE_SANDBOX | |
| 255 | 255 | ||
| 256 | + enum class WasmMemoryType { | ||
| 257 | + kMemory32, | ||
| 258 | + kMemory64, | ||
| 259 | + }; | ||
| 260 | + | ||
| 261 | + /** | ||
| 262 | + * Returns the virtual address space reservation size (in bytes) needed | ||
| 263 | + * for one WebAssembly memory instance of the given capacity. | ||
| 264 | + * | ||
| 265 | + * \param type Whether this is a memory32 or memory64 instance. | ||
| 266 | + * \param byte_capacity The maximum size, in bytes, of the WebAssembly | ||
| 267 | + * memory. Values exceeding the engine's maximum allocatable memory | ||
| 268 | + * size for the given type (determined by max_mem32_pages or | ||
| 269 | + * max_mem64_pages) are clamped. | ||
| 270 | + * | ||
| 271 | + * When trap-based bounds checking is enabled by | ||
| 272 | + * EnableWebAssemblyTrapHandler(), the amount of virtual address space | ||
| 273 | + * that V8 needs to reserve for each WebAssembly memory instance can | ||
| 274 | + * be much bigger than the requested size. If the process does | ||
| 275 | + * not have enough virtual memory available, WebAssembly memory allocation | ||
| 276 | + * would fail. During the initialization of V8, embedders can use this method | ||
| 277 | + * to estimate whether the process has enough virtual memory for their | ||
| 278 | + * usage of WebAssembly, and decide whether to enable the trap handler | ||
| 279 | + * via EnableWebAssemblyTrapHandler(), or to skip it and reduce the amount of | ||
| 280 | + * virtual memory required to keep the application running. | ||
| 281 | + */ | ||
| 282 | + static size_t GetWasmMemoryReservationSizeInBytes(WasmMemoryType type, | ||
| 283 | + size_t byte_capacity); | ||
| 284 | + | ||
| 256 | 285 | /** | |
| 257 | 286 | * Activate trap-based bounds checking for WebAssembly. | |
| 258 | 287 | * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,6 +148,7 @@ | |||
| 148 | 148 | #include "src/wasm/value-type.h" | |
| 149 | 149 | #include "src/wasm/wasm-engine.h" | |
| 150 | 150 | #include "src/wasm/wasm-js.h" | |
| 151 | + #include "src/wasm/wasm-limits.h" | ||
| 151 | 152 | #include "src/wasm/wasm-objects-inl.h" | |
| 152 | 153 | #include "src/wasm/wasm-result.h" | |
| 153 | 154 | #include "src/wasm/wasm-serialization.h" | |
@@ -6504,6 +6505,26 @@ bool TryHandleWebAssemblyTrapWindows(EXCEPTION_POINTERS* exception) { | |||
| 6504 | 6505 | } | |
| 6505 | 6506 | #endif | |
| 6506 | 6507 | ||
| 6508 | + size_t V8::GetWasmMemoryReservationSizeInBytes(WasmMemoryType type, | ||
| 6509 | + size_t byte_capacity) { | ||
| 6510 | + #if V8_ENABLE_WEBASSEMBLY | ||
| 6511 | + bool is_memory64 = type == WasmMemoryType::kMemory64; | ||
| 6512 | + uint64_t max_byte_capacity = | ||
| 6513 | + is_memory64 ? i::wasm::max_mem64_bytes() : i::wasm::max_mem32_bytes(); | ||
| 6514 | + if (byte_capacity > max_byte_capacity) { | ||
| 6515 | + byte_capacity = static_cast<size_t>(max_byte_capacity); | ||
| 6516 | + } | ||
| 6517 | + #if V8_TRAP_HANDLER_SUPPORTED | ||
| 6518 | + if (!is_memory64 || i::v8_flags.wasm_memory64_trap_handling) { | ||
| 6519 | + return i::BackingStore::GetWasmReservationSize( | ||
| 6520 | + /* has_guard_regions */ true, byte_capacity, | ||
| 6521 | + /* is_wasm_memory64 */ is_memory64); | ||
| 6522 | + } | ||
| 6523 | + #endif // V8_TRAP_HANDLER_SUPPORTED | ||
| 6524 | + #endif // V8_ENABLE_WEBASSEMBLY | ||
| 6525 | + return byte_capacity; | ||
| 6526 | + } | ||
| 6527 | + | ||
| 6507 | 6528 | bool V8::EnableWebAssemblyTrapHandler(bool use_v8_signal_handler) { | |
| 6508 | 6529 | #if V8_ENABLE_WEBASSEMBLY | |
| 6509 | 6530 | return v8::internal::trap_handler::EnableTrapHandler(use_v8_signal_handler); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,8 +51,25 @@ enum class AllocationStatus { | |||
| 51 | 51 | kOtherFailure // Failed for an unknown reason | |
| 52 | 52 | }; | |
| 53 | 53 | ||
| 54 | - size_t GetReservationSize(bool has_guard_regions, size_t byte_capacity, | ||
| 55 | - bool is_wasm_memory64) { | ||
| 54 | + base::AddressRegion GetReservedRegion(bool has_guard_regions, | ||
| 55 | + bool is_wasm_memory64, void* buffer_start, | ||
| 56 | + size_t byte_capacity) { | ||
| 57 | + return base::AddressRegion( | ||
| 58 | + reinterpret_cast<Address>(buffer_start), | ||
| 59 | + BackingStore::GetWasmReservationSize(has_guard_regions, byte_capacity, | ||
| 60 | + is_wasm_memory64)); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + void RecordStatus(Isolate* isolate, AllocationStatus status) { | ||
| 64 | + isolate->counters()->wasm_memory_allocation_result()->AddSample( | ||
| 65 | + static_cast<int>(status)); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + } // namespace | ||
| 69 | + | ||
| 70 | + size_t BackingStore::GetWasmReservationSize(bool has_guard_regions, | ||
| 71 | + size_t byte_capacity, | ||
| 72 | + bool is_wasm_memory64) { | ||
| 56 | 73 | #if V8_TARGET_ARCH_64_BIT && V8_ENABLE_WEBASSEMBLY | |
| 57 | 74 | DCHECK_IMPLIES(is_wasm_memory64 && has_guard_regions, | |
| 58 | 75 | v8_flags.wasm_memory64_trap_handling); | |
@@ -73,21 +90,6 @@ size_t GetReservationSize(bool has_guard_regions, size_t byte_capacity, | |||
| 73 | 90 | return byte_capacity; | |
| 74 | 91 | } | |
| 75 | 92 | ||
| 76 | - base::AddressRegion GetReservedRegion(bool has_guard_regions, | ||
| 77 | - bool is_wasm_memory64, void* buffer_start, | ||
| 78 | - size_t byte_capacity) { | ||
| 79 | - return base::AddressRegion( | ||
| 80 | - reinterpret_cast<Address>(buffer_start), | ||
| 81 | - GetReservationSize(has_guard_regions, byte_capacity, is_wasm_memory64)); | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - void RecordStatus(Isolate* isolate, AllocationStatus status) { | ||
| 85 | - isolate->counters()->wasm_memory_allocation_result()->AddSample( | ||
| 86 | - static_cast<int>(status)); | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - } // namespace | ||
| 90 | - | ||
| 91 | 93 | // The backing store for a Wasm shared memory remembers all the isolates | |
| 92 | 94 | // with which it has been shared. | |
| 93 | 95 | struct SharedWasmMemoryData { | |
@@ -161,7 +163,7 @@ BackingStore::~BackingStore() { | |||
| 161 | 163 | ||
| 162 | 164 | #if V8_ENABLE_WEBASSEMBLY | |
| 163 | 165 | if (is_wasm_memory()) { | |
| 164 | - size_t reservation_size = GetReservationSize( | ||
| 166 | + size_t reservation_size = GetWasmReservationSize( | ||
| 165 | 167 | has_guard_regions(), byte_capacity_, is_wasm_memory64()); | |
| 166 | 168 | TRACE_BS( | |
| 167 | 169 | "BSw:free bs=%p mem=%p (length=%zu, capacity=%zu, reservation=%zu)\n", | |
@@ -315,8 +317,8 @@ std::unique_ptr<BackingStore> BackingStore::TryAllocateAndPartiallyCommitMemory( | |||
| 315 | 317 | }; | |
| 316 | 318 | ||
| 317 | 319 | size_t byte_capacity = maximum_pages * page_size; | |
| 318 | - size_t reservation_size = | ||
| 319 | - GetReservationSize(has_guard_regions, byte_capacity, is_wasm_memory64); | ||
| 320 | + size_t reservation_size = GetWasmReservationSize( | ||
| 321 | + has_guard_regions, byte_capacity, is_wasm_memory64); | ||
| 320 | 322 | ||
| 321 | 323 | //-------------------------------------------------------------------------- | |
| 322 | 324 | // Allocate pages (inaccessible by default). | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -183,6 +183,11 @@ class V8_EXPORT_PRIVATE BackingStore : public BackingStoreBase { | |||
| 183 | 183 | ||
| 184 | 184 | uint32_t id() const { return id_; } | |
| 185 | 185 | ||
| 186 | + // Return the size of the reservation needed for a wasm backing store. | ||
| 187 | + static size_t GetWasmReservationSize(bool has_guard_regions, | ||
| 188 | + size_t byte_capacity, | ||
| 189 | + bool is_wasm_memory64); | ||
| 190 | + | ||
| 186 | 191 | private: | |
| 187 | 192 | friend class GlobalBackingStoreRegistry; | |
| 188 | 193 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -122,6 +122,22 @@ void WasmStreamingMoreFunctionsCanBeSerializedCallback( | |||
| 122 | 122 | streaming->SetMoreFunctionsCanBeSerializedCallback([](CompiledWasmModule) {}); | |
| 123 | 123 | } | |
| 124 | 124 | ||
| 125 | + TEST_F(ApiWasmTest, GetWasmMemoryReservationSizeInBytes) { | ||
| 126 | + constexpr size_t kCapacity = 64 * 1024; // 64 KiB | ||
| 127 | + size_t reservation = V8::GetWasmMemoryReservationSizeInBytes( | ||
| 128 | + V8::WasmMemoryType::kMemory32, kCapacity); | ||
| 129 | + size_t reservation64 = V8::GetWasmMemoryReservationSizeInBytes( | ||
| 130 | + V8::WasmMemoryType::kMemory64, kCapacity); | ||
| 131 | + | ||
| 132 | + #if V8_TRAP_HANDLER_SUPPORTED | ||
| 133 | + EXPECT_GE(reservation, kCapacity); | ||
| 134 | + EXPECT_GE(reservation64, kCapacity); | ||
| 135 | + #else | ||
| 136 | + EXPECT_EQ(reservation, kCapacity); | ||
| 137 | + EXPECT_EQ(reservation64, kCapacity); | ||
| 138 | + #endif // V8_TRAP_HANDLER_SUPPORTED | ||
| 139 | + } | ||
| 140 | + | ||
| 125 | 141 | TEST_F(ApiWasmTest, WasmStreamingCallback) { | |
| 126 | 142 | TestWasmStreaming(WasmStreamingCallbackTestCallbackIsCalled, | |
| 127 | 143 | Promise::kPending); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments