Commit 9d5dd6dd authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Adjust the traphandler implementation for MacOS

In the chrome-side implementation I currently use the default
trap handlers of V8, see https://crrev.com/c/1290955

Bug: chromium:906565

Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: I74c5a18c479ad1c69303d104ad4f040de436c4e7
Reviewed-on: https://chromium-review.googlesource.com/c/1282960
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57636}
parent e036883b
......@@ -1534,7 +1534,7 @@ v8_header_set("v8_headers") {
"include/v8config.h",
]
if (is_linux) {
if (is_linux || is_mac) {
sources += [ "include/v8-wasm-trap-handler-posix.h" ]
}
......@@ -2759,7 +2759,7 @@ v8_source_set("v8_base") {
"src/x64/simulator-x64.h",
"src/x64/sse-instr.h",
]
if (is_linux) {
if (is_linux || is_mac) {
sources += [
"src/trap-handler/handler-inside-posix.cc",
"src/trap-handler/handler-inside-posix.h",
......
......@@ -102,7 +102,7 @@
#include "src/wasm/wasm-result.h"
#include "src/wasm/wasm-serialization.h"
#ifdef V8_OS_POSIX
#if V8_OS_LINUX || V8_OS_MACOSX
#include <signal.h>
#include "src/trap-handler/handler-inside-posix.h"
#endif
......@@ -5873,12 +5873,12 @@ bool v8::V8::Initialize() {
return true;
}
#if V8_OS_POSIX
#if V8_OS_LINUX || V8_OS_MACOSX
bool TryHandleWebAssemblyTrapPosix(int sig_code, siginfo_t* info,
void* context) {
#if V8_OS_LINUX && V8_TARGET_ARCH_X64 && !V8_OS_ANDROID
#if V8_TARGET_ARCH_X64 && !V8_OS_ANDROID
return i::trap_handler::TryHandleSignal(sig_code, info, context);
#else // V8_OS_LINUX && V8_TARGET_ARCH_X64 && !V8_OS_ANDROID
#else
return false;
#endif
}
......
......@@ -14,6 +14,10 @@ specific_include_rules = {
"+src/globals.h",
"+src/flags.h",
],
"handler-inside-posix.h": [
# To access V8_OS_LINUX. This file is already included in build_config.h.
"+include/v8config.h",
],
"handler-inside-win.h": [
"+src/base/macros.h",
]
......
......@@ -26,6 +26,13 @@
#include "src/trap-handler/handler-inside-posix.h"
#include <signal.h>
#ifdef V8_OS_LINUX
#include <ucontext.h>
#elif V8_OS_MACOSX
#include <sys/ucontext.h>
#endif
#include <stddef.h>
#include <stdlib.h>
......@@ -37,6 +44,9 @@ namespace internal {
namespace trap_handler {
bool IsKernelGeneratedSignal(siginfo_t* info) {
// On macOS, only `info->si_code > 0` is relevant, because macOS leaves
// si_code at its default of 0 for signals that don’t originate in hardware.
// The other conditions are only relevant for Linux.
return info->si_code > 0 && info->si_code != SI_USER &&
info->si_code != SI_QUEUE && info->si_code != SI_TIMER &&
info->si_code != SI_ASYNCIO && info->si_code != SI_MESGQ;
......@@ -63,7 +73,8 @@ class SigUnmaskStack {
bool TryHandleSignal(int signum, siginfo_t* info, void* context) {
// Bail out early in case we got called for the wrong kind of signal.
if (signum != SIGSEGV) {
if (signum != kOobSignal) {
return false;
}
......@@ -94,11 +105,18 @@ bool TryHandleSignal(int signum, siginfo_t* info, void* context) {
SigUnmaskStack unmask(sigs);
ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
uintptr_t fault_addr = uc->uc_mcontext.gregs[REG_RIP];
#if V8_OS_LINUX
auto* context_rip = &uc->uc_mcontext.gregs[REG_RIP];
#elif V8_OS_MACOSX
auto* context_rip = &uc->uc_mcontext->__ss.__rip;
#else
#error Unsupported platform
#endif
uintptr_t fault_addr = *context_rip;
uintptr_t landing_pad = 0;
if (TryFindLandingPad(fault_addr, &landing_pad)) {
// Tell the caller to return to the landing pad.
uc->uc_mcontext.gregs[REG_RIP] = landing_pad;
*context_rip = landing_pad;
// We will return to wasm code, so restore the g_thread_in_wasm_code flag.
g_thread_in_wasm_code = true;
return true;
......
......@@ -6,11 +6,20 @@
#define V8_TRAP_HANDLER_HANDLER_INSIDE_POSIX_H_
#include <signal.h>
#include "include/v8config.h"
namespace v8 {
namespace internal {
namespace trap_handler {
#if V8_OS_LINUX
constexpr int kOobSignal = SIGSEGV;
#elif V8_OS_MACOSX
constexpr int kOobSignal = SIGBUS;
#else
#error Posix trap handlers are only supported on Linux and MacOSX.
#endif
void HandleSignal(int signum, siginfo_t* info, void* context);
bool TryHandleSignal(int signum, siginfo_t* info, void* context);
......
......@@ -48,7 +48,7 @@ bool RegisterDefaultTrapHandler() {
// {sigaction} installs a new custom segfault handler. On success, it returns
// 0. If we get a nonzero value, we report an error to the caller by returning
// false.
if (sigaction(SIGSEGV, &action, &g_old_handler) != 0) {
if (sigaction(kOobSignal, &action, &g_old_handler) != 0) {
return false;
}
......@@ -61,7 +61,7 @@ bool RegisterDefaultTrapHandler() {
defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
defined(UNDEFINED_SANITIZER)
struct sigaction installed_handler;
CHECK_EQ(sigaction(SIGSEGV, NULL, &installed_handler), 0);
CHECK_EQ(sigaction(kOobSignal, NULL, &installed_handler), 0);
// If the installed handler does not point to HandleSignal, then
// allow_user_segv_handler is 0.
if (installed_handler.sa_sigaction != HandleSignal) {
......@@ -78,7 +78,7 @@ bool RegisterDefaultTrapHandler() {
void RemoveTrapHandler() {
if (g_is_default_signal_handler_registered) {
if (sigaction(SIGSEGV, &g_old_handler, nullptr) == 0) {
if (sigaction(kOobSignal, &g_old_handler, nullptr) == 0) {
g_is_default_signal_handler_registered = false;
}
}
......
......@@ -21,6 +21,8 @@ namespace trap_handler {
#define V8_TRAP_HANDLER_SUPPORTED true
#elif V8_TARGET_ARCH_X64 && V8_OS_WIN
#define V8_TRAP_HANDLER_SUPPORTED true
#elif V8_TARGET_ARCH_X64 && V8_OS_MACOSX
#define V8_TRAP_HANDLER_SUPPORTED true
#else
#define V8_TRAP_HANDLER_SUPPORTED false
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment