@@ -425,6 +425,7 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { |
425 | 425 | typedef void (*sigaction_cb)(int signo, siginfo_t* info, void* ucontext); |
426 | 426 | #endif |
427 | 427 | #if NODE_USE_V8_WASM_TRAP_HANDLER |
| 428 | +static std::atomic<bool> is_wasm_trap_handler_configured{false}; |
428 | 429 | #if defined(_WIN32) |
429 | 430 | static LONG WINAPI TrapWebAssemblyOrContinue(EXCEPTION_POINTERS* exception) { |
430 | 431 | if (v8::TryHandleWebAssemblyTrapWindows(exception)) { |
@@ -470,15 +471,17 @@ void RegisterSignalHandler(int signal, |
470 | 471 | bool reset_handler) { |
471 | 472 | CHECK_NOT_NULL(handler); |
472 | 473 | #if NODE_USE_V8_WASM_TRAP_HANDLER |
473 | | - if (signal == SIGSEGV) { |
| 474 | + // Stash the user-registered handlers for TrapWebAssemblyOrContinue |
| 475 | + // to call out to when the signal is not coming from a WASM OOM. |
| 476 | + if (signal == SIGSEGV && is_wasm_trap_handler_configured.load()) { |
474 | 477 | CHECK(previous_sigsegv_action.is_lock_free()); |
475 | 478 | CHECK(!reset_handler); |
476 | 479 | previous_sigsegv_action.store(handler); |
477 | 480 | return; |
478 | 481 | } |
479 | | -// TODO(align behavior between macos and other in next major version) |
| 482 | + // TODO(align behavior between macos and other in next major version) |
480 | 483 | #if defined(__APPLE__) |
481 | | - if (signal == SIGBUS) { |
| 484 | + if (signal == SIGBUS && is_wasm_trap_handler_configured.load()) { |
482 | 485 | CHECK(previous_sigbus_action.is_lock_free()); |
483 | 486 | CHECK(!reset_handler); |
484 | 487 | previous_sigbus_action.store(handler); |
@@ -630,25 +633,6 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { |
630 | 633 | if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) { |
631 | 634 | RegisterSignalHandler(SIGINT, SignalExit, true); |
632 | 635 | RegisterSignalHandler(SIGTERM, SignalExit, true); |
633 | | - |
634 | | -#if NODE_USE_V8_WASM_TRAP_HANDLER |
635 | | - // Tell V8 to disable emitting WebAssembly |
636 | | - // memory bounds checks. This means that we have |
637 | | - // to catch the SIGSEGV/SIGBUS in TrapWebAssemblyOrContinue |
638 | | - // and pass the signal context to V8. |
639 | | - { |
640 | | - struct sigaction sa; |
641 | | - memset(&sa, 0, sizeof(sa)); |
642 | | - sa.sa_sigaction = TrapWebAssemblyOrContinue; |
643 | | - sa.sa_flags = SA_SIGINFO; |
644 | | - CHECK_EQ(sigaction(SIGSEGV, &sa, nullptr), 0); |
645 | | -// TODO(align behavior between macos and other in next major version) |
646 | | -#if defined(__APPLE__) |
647 | | - CHECK_EQ(sigaction(SIGBUS, &sa, nullptr), 0); |
648 | | -#endif |
649 | | - } |
650 | | - V8::EnableWebAssemblyTrapHandler(false); |
651 | | -#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
652 | 636 | } |
653 | 637 |
|
654 | 638 | if (!(flags & ProcessInitializationFlags::kNoAdjustResourceLimits)) { |
@@ -675,14 +659,6 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { |
675 | 659 | } |
676 | 660 | #endif // __POSIX__ |
677 | 661 | #ifdef _WIN32 |
678 | | -#ifdef NODE_USE_V8_WASM_TRAP_HANDLER |
679 | | - { |
680 | | - constexpr ULONG first = TRUE; |
681 | | - per_process::old_vectored_exception_handler = |
682 | | - AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); |
683 | | - } |
684 | | - V8::EnableWebAssemblyTrapHandler(false); |
685 | | -#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
686 | 662 | if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { |
687 | 663 | for (int fd = 0; fd <= 2; ++fd) { |
688 | 664 | auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); |
@@ -1225,6 +1201,37 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args, |
1225 | 1201 | cppgc::InitializeProcess(allocator); |
1226 | 1202 | } |
1227 | 1203 |
|
| 1204 | +#if NODE_USE_V8_WASM_TRAP_HANDLER |
| 1205 | + bool use_wasm_trap_handler = |
| 1206 | + !per_process::cli_options->disable_wasm_trap_handler; |
| 1207 | + if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling) && |
| 1208 | + use_wasm_trap_handler) { |
| 1209 | +#if defined(_WIN32) |
| 1210 | + constexpr ULONG first = TRUE; |
| 1211 | + per_process::old_vectored_exception_handler = |
| 1212 | + AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); |
| 1213 | +#else |
| 1214 | + // Tell V8 to disable emitting WebAssembly |
| 1215 | + // memory bounds checks. This means that we have |
| 1216 | + // to catch the SIGSEGV/SIGBUS in TrapWebAssemblyOrContinue |
| 1217 | + // and pass the signal context to V8. |
| 1218 | + { |
| 1219 | + struct sigaction sa; |
| 1220 | + memset(&sa, 0, sizeof(sa)); |
| 1221 | + sa.sa_sigaction = TrapWebAssemblyOrContinue; |
| 1222 | + sa.sa_flags = SA_SIGINFO; |
| 1223 | + CHECK_EQ(sigaction(SIGSEGV, &sa, nullptr), 0); |
| 1224 | +// TODO(align behavior between macos and other in next major version) |
| 1225 | +#if defined(__APPLE__) |
| 1226 | + CHECK_EQ(sigaction(SIGBUS, &sa, nullptr), 0); |
| 1227 | +#endif |
| 1228 | + } |
| 1229 | +#endif // defined(_WIN32) |
| 1230 | + is_wasm_trap_handler_configured.store(true); |
| 1231 | + V8::EnableWebAssemblyTrapHandler(false); |
| 1232 | + } |
| 1233 | +#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
| 1234 | + |
1228 | 1235 | performance::performance_v8_start = PERFORMANCE_NOW(); |
1229 | 1236 | per_process::v8_initialized = true; |
1230 | 1237 |
|
@@ -1254,7 +1261,7 @@ void TearDownOncePerProcess() { |
1254 | 1261 | } |
1255 | 1262 |
|
1256 | 1263 | #if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32) |
1257 | | - if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) { |
| 1264 | + if (is_wasm_trap_handler_configured.load()) { |
1258 | 1265 | RemoveVectoredExceptionHandler(per_process::old_vectored_exception_handler); |
1259 | 1266 | } |
1260 | 1267 | #endif |
|