@@ -1046,6 +1046,44 @@ 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 =
1065+ V8::GetWasmMemoryReservationSizeInBytes (V8::WasmMemoryType::kMemory32 , byte_capacity);
1066+ uint64_t cage_size_needed_64 =
1067+ V8::GetWasmMemoryReservationSizeInBytes (V8::WasmMemoryType::kMemory64 , byte_capacity);
1068+ uint64_t cage_size_needed = std::max (cage_size_needed_32, cage_size_needed_64);
1069+ bool can_enable = virtual_memory_available >= cage_size_needed;
1070+ per_process::Debug (DebugCategory::BOOTSTRAP,
1071+ " Virtual memory available: %" PRIu64 " bytes,\n "
1072+ " cage size needed for 32-bit: %" PRIu64 " bytes,\n "
1073+ " cage size needed for 64-bit: %" PRIu64 " bytes,\n "
1074+ " Can%senable WASM trap handler\n " ,
1075+ virtual_memory_available,
1076+ cage_size_needed_32,
1077+ cage_size_needed_64,
1078+ can_enable ? " " : " not " );
1079+
1080+ return can_enable;
1081+ #else
1082+ return false ;
1083+ #endif // __POSIX__
1084+ }
1085+ #endif // NODE_USE_V8_WASM_TRAP_HANDLER
1086+
10491087static std::shared_ptr<InitializationResultImpl>
10501088InitializeOncePerProcessInternal (const std::vector<std::string>& args,
10511089 ProcessInitializationFlags::Flags flags =
@@ -1248,7 +1286,9 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
12481286 bool use_wasm_trap_handler =
12491287 !per_process::cli_options->disable_wasm_trap_handler ;
12501288 if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling ) &&
1251- use_wasm_trap_handler) {
1289+ use_wasm_trap_handler && CanEnableWebAssemblyTrapHandler ()) {
1290+ per_process::Debug (DebugCategory::BOOTSTRAP,
1291+ " Enabling WebAssembly trap handler for bounds checks\n " );
12521292#if defined(_WIN32)
12531293 constexpr ULONG first = TRUE ;
12541294 per_process::old_vectored_exception_handler =
0 commit comments