From 2b3d7b0da2bd00d0503d9031aec70a7a3bbd41ef Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Mon, 15 Apr 2024 23:56:01 +0500 Subject: [PATCH 1/5] src: remove misplaced windows code under posix guard in node.cc The V8 WebAssembly trap handler setup for Windows was incorrectly nested within a POSIX conditional compilation block in src/node.cc. This caused the related functions to be effectively non-operational on Windows. The changes involve removing the Windows-specific code from the POSIX section and correctly placing it under the WIN32 check. This fix will ensure that the intended exception handling is active on Windows builds. Fixes: https://github.com/nodejs/node/issues/52404 Refs: https://github.com/nodejs/node/pull/35033 --- src/node.cc | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/node.cc b/src/node.cc index 2395f28c7b8cb7..bd19cb28e400ff 100644 --- a/src/node.cc +++ b/src/node.cc @@ -635,17 +635,6 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { RegisterSignalHandler(SIGTERM, SignalExit, true); #if NODE_USE_V8_WASM_TRAP_HANDLER -#if defined(_WIN32) - { - constexpr ULONG first = TRUE; - per_process::old_vectored_exception_handler = - AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); - } -#else - // Tell V8 to disable emitting WebAssembly - // memory bounds checks. This means that we have - // to catch the SIGSEGV/SIGBUS in TrapWebAssemblyOrContinue - // and pass the signal context to V8. { struct sigaction sa; memset(&sa, 0, sizeof(sa)); @@ -657,7 +646,6 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { CHECK_EQ(sigaction(SIGBUS, &sa, nullptr), 0); #endif } -#endif // defined(_WIN32) V8::EnableWebAssemblyTrapHandler(false); #endif // NODE_USE_V8_WASM_TRAP_HANDLER } @@ -686,6 +674,11 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { } #endif // __POSIX__ #ifdef _WIN32 +{ + constexpr ULONG first = TRUE; + per_process::old_vectored_exception_handler = + AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); +} if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { for (int fd = 0; fd <= 2; ++fd) { auto handle = reinterpret_cast(_get_osfhandle(fd)); From ce83aae50b3f7a18ec7152dc80f7e7a6d950cfe7 Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Tue, 16 Apr 2024 00:07:03 +0500 Subject: [PATCH 2/5] src: format src/node --- src/node.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/node.cc b/src/node.cc index bd19cb28e400ff..b6e4f5338ae433 100644 --- a/src/node.cc +++ b/src/node.cc @@ -674,11 +674,11 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { } #endif // __POSIX__ #ifdef _WIN32 -{ - constexpr ULONG first = TRUE; - per_process::old_vectored_exception_handler = - AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); -} + { + constexpr ULONG first = TRUE; + per_process::old_vectored_exception_handler = + AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); + } if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { for (int fd = 0; fd <= 2; ++fd) { auto handle = reinterpret_cast(_get_osfhandle(fd)); From c3166e818a7e5ac747a34c37736aa28a1c3337ab Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Fri, 19 Apr 2024 12:17:49 +0500 Subject: [PATCH 3/5] src: resolve feedback --- src/node.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/node.cc b/src/node.cc index b6e4f5338ae433..9125fa9e791779 100644 --- a/src/node.cc +++ b/src/node.cc @@ -635,6 +635,10 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { RegisterSignalHandler(SIGTERM, SignalExit, true); #if NODE_USE_V8_WASM_TRAP_HANDLER + // Tell V8 to disable emitting WebAssembly + // memory bounds checks. This means that we have + // to catch the SIGSEGV/SIGBUS in TrapWebAssemblyOrContinue + // and pass the signal context to V8. { struct sigaction sa; memset(&sa, 0, sizeof(sa)); @@ -674,11 +678,13 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { } #endif // __POSIX__ #ifdef _WIN32 +#ifdef NODE_USE_V8_WASM_TRAP_HANDLER { constexpr ULONG first = TRUE; per_process::old_vectored_exception_handler = AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); } +#endif // NODE_USE_V8_WASM_TRAP_HANDLER if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { for (int fd = 0; fd <= 2; ++fd) { auto handle = reinterpret_cast(_get_osfhandle(fd)); From e16d6f01a90799bc6c18c9d3ae9662b1c1227584 Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Fri, 19 Apr 2024 22:37:24 +0500 Subject: [PATCH 4/5] src: resolve feedback --- src/node.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node.cc b/src/node.cc index 9125fa9e791779..f1c4b2b932dd4c 100644 --- a/src/node.cc +++ b/src/node.cc @@ -684,6 +684,7 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { per_process::old_vectored_exception_handler = AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); } + V8::EnableWebAssemblyTrapHandler(false); #endif // NODE_USE_V8_WASM_TRAP_HANDLER if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { for (int fd = 0; fd <= 2; ++fd) { From 2c68fcdc722f387898a8a0be145351fb54b104ed Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Tue, 23 Apr 2024 02:32:23 +0500 Subject: [PATCH 5/5] src: fix build error on win --- src/node.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index f1c4b2b932dd4c..36edb1e84e5cde 100644 --- a/src/node.cc +++ b/src/node.cc @@ -429,7 +429,7 @@ typedef void (*sigaction_cb)(int signo, siginfo_t* info, void* ucontext); #endif #if NODE_USE_V8_WASM_TRAP_HANDLER #if defined(_WIN32) -static LONG TrapWebAssemblyOrContinue(EXCEPTION_POINTERS* exception) { +static LONG WINAPI TrapWebAssemblyOrContinue(EXCEPTION_POINTERS* exception) { if (v8::TryHandleWebAssemblyTrapWindows(exception)) { return EXCEPTION_CONTINUE_EXECUTION; }