Skip to content

Commit 0577127

Browse files
devsneknodejs-github-bot
authored andcommitted
src: enable wasm trap handler on windows
PR-URL: #35033 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Shelley Vohr <[email protected]>
1 parent d24eecd commit 0577127

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/node.cc

+30-2
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@
6969

7070
#include "large_pages/node_large_page.h"
7171

72-
#if defined(__APPLE__) || defined(__linux__)
72+
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
7373
#define NODE_USE_V8_WASM_TRAP_HANDLER 1
7474
#else
7575
#define NODE_USE_V8_WASM_TRAP_HANDLER 0
7676
#endif
7777

7878
#if NODE_USE_V8_WASM_TRAP_HANDLER
79+
#if defined(_WIN32)
80+
#include "v8-wasm-trap-handler-win.h"
81+
#else
7982
#include <atomic>
8083
#include "v8-wasm-trap-handler-posix.h"
84+
#endif
8185
#endif // NODE_USE_V8_WASM_TRAP_HANDLER
8286

8387
// ========== global C headers ==========
@@ -149,6 +153,10 @@ bool v8_initialized = false;
149153
// process-relative uptime base in nanoseconds, initialized in node::Start()
150154
uint64_t node_start_time;
151155

156+
#if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32)
157+
PVOID old_vectored_exception_handler;
158+
#endif
159+
152160
// node_v8_platform-inl.h
153161
struct V8Platform v8_platform;
154162
} // namespace per_process
@@ -506,6 +514,14 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
506514
typedef void (*sigaction_cb)(int signo, siginfo_t* info, void* ucontext);
507515
#endif
508516
#if NODE_USE_V8_WASM_TRAP_HANDLER
517+
#if defined(_WIN32)
518+
static LONG TrapWebAssemblyOrContinue(EXCEPTION_POINTERS* exception) {
519+
if (v8::TryHandleWebAssemblyTrapWindows(exception)) {
520+
return EXCEPTION_CONTINUE_EXECUTION;
521+
}
522+
return EXCEPTION_CONTINUE_SEARCH;
523+
}
524+
#else
509525
static std::atomic<sigaction_cb> previous_sigsegv_action;
510526

511527
void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) {
@@ -525,6 +541,7 @@ void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) {
525541
}
526542
}
527543
}
544+
#endif // defined(_WIN32)
528545
#endif // NODE_USE_V8_WASM_TRAP_HANDLER
529546

530547
#ifdef __POSIX__
@@ -547,7 +564,6 @@ void RegisterSignalHandler(int signal,
547564
sigfillset(&sa.sa_mask);
548565
CHECK_EQ(sigaction(signal, &sa, nullptr), 0);
549566
}
550-
551567
#endif // __POSIX__
552568

553569
#ifdef __POSIX__
@@ -630,6 +646,13 @@ inline void PlatformInit() {
630646
RegisterSignalHandler(SIGTERM, SignalExit, true);
631647

632648
#if NODE_USE_V8_WASM_TRAP_HANDLER
649+
#if defined(_WIN32)
650+
{
651+
constexpr ULONG first = TRUE;
652+
per_process::old_vectored_exception_handler =
653+
AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue);
654+
}
655+
#else
633656
// Tell V8 to disable emitting WebAssembly
634657
// memory bounds checks. This means that we have
635658
// to catch the SIGSEGV in TrapWebAssemblyOrContinue
@@ -640,6 +663,7 @@ inline void PlatformInit() {
640663
sa.sa_sigaction = TrapWebAssemblyOrContinue;
641664
CHECK_EQ(sigaction(SIGSEGV, &sa, nullptr), 0);
642665
}
666+
#endif // defined(_WIN32)
643667
V8::EnableWebAssemblyTrapHandler(false);
644668
#endif // NODE_USE_V8_WASM_TRAP_HANDLER
645669

@@ -1050,6 +1074,10 @@ void TearDownOncePerProcess() {
10501074
per_process::v8_initialized = false;
10511075
V8::Dispose();
10521076

1077+
#if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32)
1078+
RemoveVectoredExceptionHandler(per_process::old_vectored_exception_handler);
1079+
#endif
1080+
10531081
// uv_run cannot be called from the time before the beforeExit callback
10541082
// runs until the program exits unless the event loop has any referenced
10551083
// handles after beforeExit terminates. This prevents unrefed timers

0 commit comments

Comments
 (0)