Commit 7c61c3ce authored by Eric Holk's avatar Eric Holk Committed by Commit Bot

[wasm] Add new API for enabling trap handler

The new API supersedes the old `RegisterDefaultSignalHandler` and flag
combination. Now the embedder must explicitly call
`EnableWebAssemblyTrapHandler` to activate the trap handler and optionally
install the default signal handler. The old flag is now used only by D8 to
decide whether to call this function.

Bug: v8:5277

Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: I05fbb2138138bfc95b14361aabd712db84789b4a
Reviewed-on: https://chromium-review.googlesource.com/963179
Commit-Queue: Eric Holk <eholk@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52081}
parent f866af42
......@@ -8037,7 +8037,16 @@ class V8_EXPORT V8 {
* Enable the default signal handler rather than using one provided by the
* embedder.
*/
static bool RegisterDefaultSignalHandler();
V8_DEPRECATE_SOON("Use EnableWebAssemblyTrapHandler",
static bool RegisterDefaultSignalHandler());
/**
* Activate trap-based bounds checking for WebAssembly.
*
* \param use_v8_signal_handler Whether V8 should install its own signal
* handler or rely on the embedder's.
*/
static bool EnableWebAssemblyTrapHandler(bool use_v8_signal_handler);
private:
V8();
......
......@@ -6105,6 +6105,10 @@ bool V8::RegisterDefaultSignalHandler() {
return v8::internal::trap_handler::RegisterDefaultSignalHandler();
}
bool V8::EnableWebAssemblyTrapHandler(bool use_v8_signal_handler) {
return v8::internal::trap_handler::EnableTrapHandler(use_v8_signal_handler);
}
void v8::V8::SetEntropySource(EntropySource entropy_source) {
base::RandomNumberGenerator::SetEntropySource(entropy_source);
}
......
......@@ -3354,8 +3354,9 @@ int Shell::Main(int argc, char* argv[]) {
create_params.add_histogram_sample_callback = AddHistogramSample;
}
if (i::trap_handler::IsTrapHandlerEnabled()) {
if (!v8::V8::RegisterDefaultSignalHandler()) {
if (V8_TRAP_HANDLER_SUPPORTED && i::FLAG_wasm_trap_handler) {
constexpr bool use_default_signal_handler = true;
if (!v8::V8::EnableWebAssemblyTrapHandler(use_default_signal_handler)) {
fprintf(stderr, "Could not register signal handler");
exit(1);
}
......
......@@ -45,8 +45,8 @@ namespace v8 {
namespace internal {
namespace trap_handler {
const size_t kInitialCodeObjectSize = 1024;
const size_t kCodeObjectGrowthFactor = 2;
constexpr size_t kInitialCodeObjectSize = 1024;
constexpr size_t kCodeObjectGrowthFactor = 2;
constexpr size_t HandlerDataSize(size_t num_protected_instructions) {
return offsetof(CodeProtectionInfo, instructions) +
......@@ -275,6 +275,20 @@ size_t GetRecoveredTrapCount() {
return gRecoveredTrapCount.load(std::memory_order_relaxed);
}
bool g_is_trap_handler_enabled;
bool EnableTrapHandler(bool use_v8_signal_handler) {
if (!V8_TRAP_HANDLER_SUPPORTED) {
return false;
}
if (use_v8_signal_handler) {
g_is_trap_handler_enabled = RegisterDefaultSignalHandler();
return g_is_trap_handler_enabled;
}
g_is_trap_handler_enabled = true;
return true;
}
} // namespace trap_handler
} // namespace internal
} // namespace v8
......@@ -66,8 +66,17 @@ void ReleaseHandlerData(int index);
#define THREAD_LOCAL __thread
#endif
extern bool g_is_trap_handler_enabled;
// Enables trap handling for WebAssembly bounds checks.
//
// use_v8_signal_handler indicates that V8 should install its own signal handler
// rather than relying on the embedder to do it.
bool EnableTrapHandler(bool use_v8_signal_handler);
inline bool IsTrapHandlerEnabled() {
return FLAG_wasm_trap_handler && V8_TRAP_HANDLER_SUPPORTED;
DCHECK_IMPLIES(g_is_trap_handler_enabled, V8_TRAP_HANDLER_SUPPORTED);
return (V8_TRAP_HANDLER_SUPPORTED && FLAG_wasm_trap_handler) ||
g_is_trap_handler_enabled;
}
extern THREAD_LOCAL int g_thread_in_wasm_code;
......
......@@ -269,8 +269,9 @@ int main(int argc, char* argv[]) {
v8::V8::Initialize();
v8::V8::InitializeExternalStartupData(argv[0]);
if (i::trap_handler::IsTrapHandlerEnabled()) {
v8::V8::RegisterDefaultSignalHandler();
if (V8_TRAP_HANDLER_SUPPORTED && i::FLAG_wasm_trap_handler) {
constexpr bool use_default_signal_handler = true;
CHECK(v8::V8::EnableWebAssemblyTrapHandler(use_default_signal_handler));
}
CcTest::set_array_buffer_allocator(
......
......@@ -13,7 +13,7 @@
namespace {
#if V8_OS_POSIX
#if V8_TRAP_HANDLER_SUPPORTED
void CrashOnPurpose() { *reinterpret_cast<volatile int*>(42); }
......@@ -52,7 +52,8 @@ sigjmp_buf SignalHandlerFallbackTest::continuation_;
TEST_F(SignalHandlerFallbackTest, DoTest) {
const int save_sigs = 1;
if (!sigsetjmp(continuation_, save_sigs)) {
v8::V8::RegisterDefaultSignalHandler();
constexpr bool use_default_signal_handler = true;
CHECK(v8::V8::EnableWebAssemblyTrapHandler(use_default_signal_handler));
CrashOnPurpose();
FAIL();
} else {
......
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