@@ -1055,6 +1055,44 @@ static ExitCode InitializeNodeWithArgsInternal(
10551055 return ExitCode::kNoFailure ;
10561056}
10571057
1058+ #if NODE_USE_V8_WASM_TRAP_HANDLER
1059+ bool CanEnableWebAssemblyTrapHandler () {
1060+ // On POSIX, the machine may have a limit on the amount of virtual memory
1061+ // available, if it's not enough to allocate at least one cage for WASM,
1062+ // then the trap-handler-based bound checks cannot be used.
1063+ #ifdef __POSIX__
1064+ struct rlimit lim;
1065+ if (getrlimit (RLIMIT_AS, &lim) != 0 || lim.rlim_cur == RLIM_INFINITY) {
1066+ // Can't get the limit or there's no limit, assume trap handler can be
1067+ // enabled.
1068+ return true ;
1069+ }
1070+ uint64_t virtual_memory_available = static_cast <uint64_t >(lim.rlim_cur );
1071+
1072+ size_t byte_capacity = 64 * 1024 ; // 64KB, the minimum size of a WASM memory.
1073+ uint64_t cage_size_needed_32 =
1074+ V8::GetWasmMemoryReservationSizeInBytes (V8::WasmMemoryType::kMemory32 , byte_capacity);
1075+ uint64_t cage_size_needed_64 =
1076+ V8::GetWasmMemoryReservationSizeInBytes (V8::WasmMemoryType::kMemory64 , byte_capacity);
1077+ uint64_t cage_size_needed = std::max (cage_size_needed_32, cage_size_needed_64);
1078+ bool can_enable = virtual_memory_available >= cage_size_needed;
1079+ per_process::Debug (DebugCategory::BOOTSTRAP,
1080+ " Virtual memory available: %" PRIu64 " bytes,\n "
1081+ " cage size needed for 32-bit: %" PRIu64 " bytes,\n "
1082+ " cage size needed for 64-bit: %" PRIu64 " bytes,\n "
1083+ " Can%senable WASM trap handler\n " ,
1084+ virtual_memory_available,
1085+ cage_size_needed_32,
1086+ cage_size_needed_64,
1087+ can_enable ? " " : " not " );
1088+
1089+ return can_enable;
1090+ #else
1091+ return false ;
1092+ #endif // __POSIX__
1093+ }
1094+ #endif // NODE_USE_V8_WASM_TRAP_HANDLER
1095+
10581096static std::shared_ptr<InitializationResultImpl>
10591097InitializeOncePerProcessInternal (const std::vector<std::string>& args,
10601098 ProcessInitializationFlags::Flags flags =
@@ -1257,7 +1295,9 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
12571295 bool use_wasm_trap_handler =
12581296 !per_process::cli_options->disable_wasm_trap_handler ;
12591297 if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling ) &&
1260- use_wasm_trap_handler) {
1298+ use_wasm_trap_handler && CanEnableWebAssemblyTrapHandler ()) {
1299+ per_process::Debug (DebugCategory::BOOTSTRAP,
1300+ " Enabling WebAssembly trap handler for bounds checks\n " );
12611301#if defined(_WIN32)
12621302 constexpr ULONG first = TRUE ;
12631303 per_process::old_vectored_exception_handler =
0 commit comments