@@ -1046,6 +1046,45 @@ static ExitCode InitializeNodeWithArgsInternal(
10461046 return ExitCode::kNoFailure ;
10471047}
10481048
1049+ #if NODE_USE_V8_WASM_TRAP_HANDLER
1050+ bool CanEnableWebAssemblyTrapHandler () {
1051+ // On POSIX, the machine may have a limit on the amount of virtual memory
1052+ // available, if it's not enough to allocate at least one cage for WASM,
1053+ // then the trap-handler-based bound checks cannot be used.
1054+ #ifdef __POSIX__
1055+ struct rlimit lim;
1056+ if (getrlimit (RLIMIT_AS, &lim) != 0 || lim.rlim_cur == RLIM_INFINITY) {
1057+ // Can't get the limit or there's no limit, assume trap handler can be
1058+ // enabled.
1059+ return true ;
1060+ }
1061+ uint64_t virtual_memory_available = static_cast <uint64_t >(lim.rlim_cur );
1062+
1063+ size_t byte_capacity = 64 * 1024 ; // 64KB, the minimum size of a WASM memory.
1064+ uint64_t cage_size_needed_32 = V8::GetWasmMemoryReservationSizeInBytes (
1065+ V8::WasmMemoryType::kMemory32 , byte_capacity);
1066+ uint64_t cage_size_needed_64 = V8::GetWasmMemoryReservationSizeInBytes (
1067+ V8::WasmMemoryType::kMemory64 , byte_capacity);
1068+ uint64_t cage_size_needed =
1069+ std::max (cage_size_needed_32, cage_size_needed_64);
1070+ bool can_enable = virtual_memory_available >= cage_size_needed;
1071+ per_process::Debug (DebugCategory::BOOTSTRAP,
1072+ " Virtual memory available: %" PRIu64 " bytes,\n "
1073+ " cage size needed for 32-bit: %" PRIu64 " bytes,\n "
1074+ " cage size needed for 64-bit: %" PRIu64 " bytes,\n "
1075+ " Can%senable WASM trap handler\n " ,
1076+ virtual_memory_available,
1077+ cage_size_needed_32,
1078+ cage_size_needed_64,
1079+ can_enable ? " " : " not " );
1080+
1081+ return can_enable;
1082+ #else
1083+ return false ;
1084+ #endif // __POSIX__
1085+ }
1086+ #endif // NODE_USE_V8_WASM_TRAP_HANDLER
1087+
10491088static std::shared_ptr<InitializationResultImpl>
10501089InitializeOncePerProcessInternal (const std::vector<std::string>& args,
10511090 ProcessInitializationFlags::Flags flags =
@@ -1248,7 +1287,9 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
12481287 bool use_wasm_trap_handler =
12491288 !per_process::cli_options->disable_wasm_trap_handler ;
12501289 if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling ) &&
1251- use_wasm_trap_handler) {
1290+ use_wasm_trap_handler && CanEnableWebAssemblyTrapHandler ()) {
1291+ per_process::Debug (DebugCategory::BOOTSTRAP,
1292+ " Enabling WebAssembly trap handler for bounds checks\n " );
12521293#if defined(_WIN32)
12531294 constexpr ULONG first = TRUE ;
12541295 per_process::old_vectored_exception_handler =
0 commit comments