Skip to content

Commit

Permalink
src: enable wasm trap handler on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Sep 6, 2020
1 parent d2c5e89 commit c92a4fe
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@

#include "large_pages/node_large_page.h"

#if defined(__APPLE__) || defined(__linux__)
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32)
#define NODE_USE_V8_WASM_TRAP_HANDLER 1
#else
#define NODE_USE_V8_WASM_TRAP_HANDLER 0
#endif

#if NODE_USE_V8_WASM_TRAP_HANDLER
#if defined(_WIN32)
#include "v8-wasm-trap-handler-win.h"
#else
#include <atomic>
#include "v8-wasm-trap-handler-posix.h"
#endif
#endif // NODE_USE_V8_WASM_TRAP_HANDLER

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

#if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32)
PVOID old_vectored_exception_handler;
#endif

// node_v8_platform-inl.h
struct V8Platform v8_platform;
} // namespace per_process
Expand Down Expand Up @@ -506,6 +514,14 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
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) {
if (v8::TryHandleWebAssemblyTrapWindows(exception)) {
return EXCEPTION_CONTINUE_EXECUTION;
}
return EXCEPTION_CONTINUE_SEARCH;
}
#else
static std::atomic<sigaction_cb> previous_sigsegv_action;

void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) {
Expand All @@ -525,6 +541,7 @@ void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) {
}
}
}
#endif // defined(_WIN32)
#endif // NODE_USE_V8_WASM_TRAP_HANDLER

#ifdef __POSIX__
Expand All @@ -547,7 +564,6 @@ void RegisterSignalHandler(int signal,
sigfillset(&sa.sa_mask);
CHECK_EQ(sigaction(signal, &sa, nullptr), 0);
}

#endif // __POSIX__

#ifdef __POSIX__
Expand Down Expand Up @@ -630,6 +646,13 @@ inline void PlatformInit() {
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 in TrapWebAssemblyOrContinue
Expand All @@ -640,6 +663,7 @@ inline void PlatformInit() {
sa.sa_sigaction = TrapWebAssemblyOrContinue;
CHECK_EQ(sigaction(SIGSEGV, &sa, nullptr), 0);
}
#endif // defined(_WIN32)
V8::EnableWebAssemblyTrapHandler(false);
#endif // NODE_USE_V8_WASM_TRAP_HANDLER

Expand Down Expand Up @@ -1050,6 +1074,10 @@ void TearDownOncePerProcess() {
per_process::v8_initialized = false;
V8::Dispose();

#if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32)
RemoveVectoredExceptionHandler(per_process::old_vectored_exception_handler);
#endif

// uv_run cannot be called from the time before the beforeExit callback
// runs until the program exits unless the event loop has any referenced
// handles after beforeExit terminates. This prevents unrefed timers
Expand Down

0 comments on commit c92a4fe

Please sign in to comment.