2018-11-27 13:58:18 +00:00
|
|
|
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "include/v8config.h"
|
|
|
|
|
2019-11-06 18:22:58 +00:00
|
|
|
#if V8_OS_LINUX || V8_OS_FREEBSD
|
2018-11-27 13:58:18 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <ucontext.h>
|
2022-02-17 10:40:49 +00:00
|
|
|
#elif V8_OS_DARWIN
|
2018-11-27 13:58:18 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#elif V8_OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
#if V8_OS_POSIX
|
|
|
|
#include "include/v8-wasm-trap-handler-posix.h"
|
|
|
|
#elif V8_OS_WIN
|
|
|
|
#include "include/v8-wasm-trap-handler-win.h"
|
|
|
|
#endif
|
|
|
|
#include "src/base/page-allocator.h"
|
2021-06-17 15:43:55 +00:00
|
|
|
#include "src/base/vector.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/assembler-inl.h"
|
|
|
|
#include "src/codegen/macro-assembler-inl.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/simulator.h"
|
2019-09-09 10:19:34 +00:00
|
|
|
#include "src/objects/backing-store.h"
|
2018-11-27 13:58:18 +00:00
|
|
|
#include "src/trap-handler/trap-handler.h"
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/allocation.h"
|
2018-11-27 13:58:18 +00:00
|
|
|
#include "src/wasm/wasm-engine.h"
|
|
|
|
#include "test/common/assembler-tester.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr Register scratch = r10;
|
|
|
|
bool g_test_handler_executed = false;
|
2022-02-17 10:40:49 +00:00
|
|
|
#if V8_OS_LINUX || V8_OS_DARWIN || V8_OS_FREEBSD
|
2018-11-27 13:58:18 +00:00
|
|
|
struct sigaction g_old_segv_action;
|
|
|
|
struct sigaction g_old_fpe_action;
|
|
|
|
struct sigaction g_old_bus_action; // We get SIGBUS on Mac sometimes.
|
|
|
|
#elif V8_OS_WIN
|
|
|
|
void* g_registered_handler = nullptr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The recovery address allows us to recover from an intentional crash.
|
|
|
|
Address g_recovery_address;
|
|
|
|
// Flag to indicate if the test handler should call the trap handler as a first
|
|
|
|
// chance handler.
|
|
|
|
bool g_use_as_first_chance_handler = false;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#define __ masm.
|
|
|
|
|
|
|
|
enum TrapHandlerStyle : int {
|
|
|
|
// The test uses the default trap handler of V8.
|
|
|
|
kDefault = 0,
|
|
|
|
// The test installs the trap handler callback in its own test handler.
|
|
|
|
kCallback = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string PrintTrapHandlerTestParam(
|
|
|
|
::testing::TestParamInfo<TrapHandlerStyle> info) {
|
|
|
|
switch (info.param) {
|
|
|
|
case kDefault:
|
|
|
|
return "DefaultTrapHandler";
|
|
|
|
case kCallback:
|
|
|
|
return "Callback";
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
class TrapHandlerTest : public TestWithIsolate,
|
|
|
|
public ::testing::WithParamInterface<TrapHandlerStyle> {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
2020-10-26 13:50:02 +00:00
|
|
|
InstallFallbackHandler();
|
|
|
|
SetupTrapHandler(GetParam());
|
2022-07-27 14:47:26 +00:00
|
|
|
backing_store_ = BackingStore::AllocateWasmMemory(
|
|
|
|
i_isolate(), 1, 1, WasmMemoryFlag::kWasmMemory32,
|
|
|
|
SharedFlag::kNotShared);
|
2019-09-09 10:19:34 +00:00
|
|
|
CHECK(backing_store_);
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(backing_store_->has_guard_regions());
|
2019-09-09 10:19:34 +00:00
|
|
|
// The allocated backing store ends with a guard page.
|
|
|
|
crash_address_ = reinterpret_cast<Address>(backing_store_->buffer_start()) +
|
|
|
|
backing_store_->byte_length() + 32;
|
2018-11-27 13:58:18 +00:00
|
|
|
// Allocate a buffer for the generated code.
|
2019-11-13 16:58:58 +00:00
|
|
|
buffer_ = AllocateAssemblerBuffer(AssemblerBase::kDefaultBufferSize,
|
2019-01-17 10:23:16 +00:00
|
|
|
GetRandomMmapAddr());
|
2018-11-27 13:58:18 +00:00
|
|
|
|
|
|
|
InitRecoveryCode();
|
2020-10-26 13:50:02 +00:00
|
|
|
}
|
2018-11-27 13:58:18 +00:00
|
|
|
|
2020-10-26 13:50:02 +00:00
|
|
|
void InstallFallbackHandler() {
|
2022-02-17 10:40:49 +00:00
|
|
|
#if V8_OS_LINUX || V8_OS_DARWIN || V8_OS_FREEBSD
|
2018-11-27 13:58:18 +00:00
|
|
|
// Set up a signal handler to recover from the expected crash.
|
|
|
|
struct sigaction action;
|
|
|
|
action.sa_sigaction = SignalHandler;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
|
|
|
// SIGSEGV happens for wasm oob memory accesses on Linux.
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(0, sigaction(SIGSEGV, &action, &g_old_segv_action));
|
2018-11-27 13:58:18 +00:00
|
|
|
// SIGBUS happens for wasm oob memory accesses on macOS.
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(0, sigaction(SIGBUS, &action, &g_old_bus_action));
|
2018-11-27 13:58:18 +00:00
|
|
|
// SIGFPE to simulate crashes which are not handled by the trap handler.
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(0, sigaction(SIGFPE, &action, &g_old_fpe_action));
|
2018-11-27 13:58:18 +00:00
|
|
|
#elif V8_OS_WIN
|
|
|
|
g_registered_handler =
|
|
|
|
AddVectoredExceptionHandler(/*first=*/0, TestHandler);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
// We should always have left wasm code.
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(!GetThreadInWasmFlag());
|
2019-01-17 10:23:16 +00:00
|
|
|
buffer_.reset();
|
|
|
|
recovery_buffer_.reset();
|
2019-09-09 10:19:34 +00:00
|
|
|
backing_store_.reset();
|
2018-11-27 13:58:18 +00:00
|
|
|
|
|
|
|
// Clean up the trap handler
|
|
|
|
trap_handler::RemoveTrapHandler();
|
|
|
|
if (!g_test_handler_executed) {
|
2022-02-17 10:40:49 +00:00
|
|
|
#if V8_OS_LINUX || V8_OS_DARWIN || V8_OS_FREEBSD
|
2018-11-27 13:58:18 +00:00
|
|
|
// The test handler cleans up the signal handler setup in the test. If the
|
|
|
|
// test handler was not called, we have to do the cleanup ourselves.
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(0, sigaction(SIGSEGV, &g_old_segv_action, nullptr));
|
|
|
|
EXPECT_EQ(0, sigaction(SIGFPE, &g_old_fpe_action, nullptr));
|
|
|
|
EXPECT_EQ(0, sigaction(SIGBUS, &g_old_bus_action, nullptr));
|
2018-11-27 13:58:18 +00:00
|
|
|
#elif V8_OS_WIN
|
|
|
|
RemoveVectoredExceptionHandler(g_registered_handler);
|
|
|
|
g_registered_handler = nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitRecoveryCode() {
|
|
|
|
// Create a code snippet where we can jump to to recover from a signal or
|
|
|
|
// exception. The code snippet only consists of a return statement.
|
2019-01-17 10:23:16 +00:00
|
|
|
recovery_buffer_ = AllocateAssemblerBuffer(
|
2019-11-13 16:58:58 +00:00
|
|
|
AssemblerBase::kDefaultBufferSize, GetRandomMmapAddr());
|
2018-11-27 13:58:18 +00:00
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
recovery_buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
int recovery_offset = __ pc_offset();
|
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
2019-01-17 10:23:16 +00:00
|
|
|
recovery_buffer_->MakeExecutable();
|
2018-11-27 13:58:18 +00:00
|
|
|
g_recovery_address =
|
|
|
|
reinterpret_cast<Address>(desc.buffer + recovery_offset);
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:40:49 +00:00
|
|
|
#if V8_OS_LINUX || V8_OS_DARWIN || V8_OS_FREEBSD
|
2018-11-27 13:58:18 +00:00
|
|
|
static void SignalHandler(int signal, siginfo_t* info, void* context) {
|
|
|
|
if (g_use_as_first_chance_handler) {
|
|
|
|
if (v8::TryHandleWebAssemblyTrapPosix(signal, info, context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the signal handler, to avoid that this signal handler is called
|
|
|
|
// repeatedly.
|
|
|
|
sigaction(SIGSEGV, &g_old_segv_action, nullptr);
|
|
|
|
sigaction(SIGFPE, &g_old_fpe_action, nullptr);
|
|
|
|
sigaction(SIGBUS, &g_old_bus_action, nullptr);
|
|
|
|
|
|
|
|
g_test_handler_executed = true;
|
|
|
|
// Set the $rip to the recovery code.
|
|
|
|
ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
|
|
|
|
#if V8_OS_LINUX
|
|
|
|
uc->uc_mcontext.gregs[REG_RIP] = g_recovery_address;
|
2022-02-17 10:40:49 +00:00
|
|
|
#elif V8_OS_DARWIN
|
2018-11-27 13:58:18 +00:00
|
|
|
uc->uc_mcontext->__ss.__rip = g_recovery_address;
|
2019-11-06 18:22:58 +00:00
|
|
|
#elif V8_OS_FREEBSD
|
|
|
|
uc->uc_mcontext.mc_rip = g_recovery_address;
|
|
|
|
#else
|
|
|
|
#error Unsupported platform
|
2018-11-27 13:58:18 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if V8_OS_WIN
|
|
|
|
static LONG WINAPI TestHandler(EXCEPTION_POINTERS* exception) {
|
|
|
|
if (g_use_as_first_chance_handler) {
|
|
|
|
if (v8::TryHandleWebAssemblyTrapWindows(exception)) {
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RemoveVectoredExceptionHandler(g_registered_handler);
|
|
|
|
g_registered_handler = nullptr;
|
|
|
|
g_test_handler_executed = true;
|
|
|
|
exception->ContextRecord->Rip = g_recovery_address;
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SetupTrapHandler(TrapHandlerStyle style) {
|
|
|
|
bool use_default_handler = style == kDefault;
|
|
|
|
g_use_as_first_chance_handler = !use_default_handler;
|
|
|
|
CHECK(v8::V8::EnableWebAssemblyTrapHandler(use_default_handler));
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:50:02 +00:00
|
|
|
public:
|
2018-11-27 13:58:18 +00:00
|
|
|
void GenerateSetThreadInWasmFlagCode(MacroAssembler* masm) {
|
|
|
|
masm->Move(scratch,
|
2018-12-05 13:52:50 +00:00
|
|
|
i_isolate()->thread_local_top()->thread_in_wasm_flag_address_,
|
2021-11-17 18:14:02 +00:00
|
|
|
RelocInfo::NO_INFO);
|
2018-11-27 13:58:18 +00:00
|
|
|
masm->movl(MemOperand(scratch, 0), Immediate(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateResetThreadInWasmFlagCode(MacroAssembler* masm) {
|
|
|
|
masm->Move(scratch,
|
2018-12-05 13:52:50 +00:00
|
|
|
i_isolate()->thread_local_top()->thread_in_wasm_flag_address_,
|
2021-11-17 18:14:02 +00:00
|
|
|
RelocInfo::NO_INFO);
|
2018-11-27 13:58:18 +00:00
|
|
|
masm->movl(MemOperand(scratch, 0), Immediate(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetThreadInWasmFlag() {
|
|
|
|
return *reinterpret_cast<int*>(
|
|
|
|
trap_handler::GetThreadInWasmThreadLocalAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the code in buffer.
|
2019-01-17 10:23:16 +00:00
|
|
|
void ExecuteBuffer() {
|
|
|
|
buffer_->MakeExecutable();
|
|
|
|
GeneratedCode<void>::FromAddress(
|
|
|
|
i_isolate(), reinterpret_cast<Address>(buffer_->start()))
|
2018-11-27 13:58:18 +00:00
|
|
|
.Call();
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_FALSE(g_test_handler_executed);
|
2018-11-27 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the code in buffer. We expect a crash which we recover from in the
|
|
|
|
// test handler.
|
2019-01-17 10:23:16 +00:00
|
|
|
void ExecuteExpectCrash(TestingAssemblerBuffer* buffer,
|
|
|
|
bool check_wasm_flag = true) {
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_FALSE(g_test_handler_executed);
|
2019-01-17 10:23:16 +00:00
|
|
|
buffer->MakeExecutable();
|
2018-12-05 13:52:50 +00:00
|
|
|
GeneratedCode<void>::FromAddress(i_isolate(),
|
2019-01-17 10:23:16 +00:00
|
|
|
reinterpret_cast<Address>(buffer->start()))
|
2018-12-05 13:52:50 +00:00
|
|
|
.Call();
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(g_test_handler_executed);
|
2018-11-27 13:58:18 +00:00
|
|
|
g_test_handler_executed = false;
|
2021-04-30 16:22:54 +00:00
|
|
|
if (check_wasm_flag) {
|
|
|
|
EXPECT_FALSE(GetThreadInWasmFlag());
|
|
|
|
}
|
2018-11-27 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool test_handler_executed() { return g_test_handler_executed; }
|
|
|
|
|
2019-09-09 10:19:34 +00:00
|
|
|
// The backing store used for testing the trap handler.
|
|
|
|
std::unique_ptr<BackingStore> backing_store_;
|
|
|
|
|
2018-11-27 13:58:18 +00:00
|
|
|
// Address within the guard region of the wasm memory. Accessing this memory
|
|
|
|
// address causes a signal or exception.
|
|
|
|
Address crash_address_;
|
|
|
|
|
|
|
|
// Buffer for generated code.
|
2019-01-17 10:23:16 +00:00
|
|
|
std::unique_ptr<TestingAssemblerBuffer> buffer_;
|
2018-11-27 13:58:18 +00:00
|
|
|
// Buffer for the code for the landing pad of the test handler.
|
2019-01-17 10:23:16 +00:00
|
|
|
std::unique_ptr<TestingAssemblerBuffer> recovery_buffer_;
|
2018-11-27 13:58:18 +00:00
|
|
|
};
|
|
|
|
|
2019-12-05 13:30:12 +00:00
|
|
|
// TODO(almuthanna): These tests were skipped because they cause a crash when
|
|
|
|
// they are ran on Fuchsia. This issue should be solved later on
|
|
|
|
// Ticket: https://crbug.com/1028617
|
|
|
|
#if !defined(V8_TARGET_OS_FUCHSIA)
|
2018-11-27 13:58:18 +00:00
|
|
|
TEST_P(TrapHandlerTest, TestTrapHandlerRecovery) {
|
|
|
|
// Test that the wasm trap handler can recover a memory access violation in
|
|
|
|
// wasm code (we fake the wasm code and the access violation).
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Push(scratch);
|
|
|
|
GenerateSetThreadInWasmFlagCode(&masm);
|
2021-11-17 18:14:02 +00:00
|
|
|
__ Move(scratch, crash_address_, RelocInfo::NO_INFO);
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t crash_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ testl(MemOperand(scratch, 0), Immediate(1));
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t recovery_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
GenerateResetThreadInWasmFlagCode(&masm);
|
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
|
|
|
|
|
|
|
trap_handler::ProtectedInstructionData protected_instruction{crash_offset,
|
|
|
|
recovery_offset};
|
|
|
|
trap_handler::RegisterHandlerData(reinterpret_cast<Address>(desc.buffer),
|
|
|
|
desc.instr_size, 1, &protected_instruction);
|
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
ExecuteBuffer();
|
2018-11-27 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(TrapHandlerTest, TestReleaseHandlerData) {
|
|
|
|
// Test that after we release handler data in the trap handler, it cannot
|
|
|
|
// recover from the specific memory access violation anymore.
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Push(scratch);
|
|
|
|
GenerateSetThreadInWasmFlagCode(&masm);
|
2021-11-17 18:14:02 +00:00
|
|
|
__ Move(scratch, crash_address_, RelocInfo::NO_INFO);
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t crash_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ testl(MemOperand(scratch, 0), Immediate(1));
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t recovery_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
GenerateResetThreadInWasmFlagCode(&masm);
|
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
|
|
|
|
|
|
|
trap_handler::ProtectedInstructionData protected_instruction{crash_offset,
|
|
|
|
recovery_offset};
|
|
|
|
int handler_id = trap_handler::RegisterHandlerData(
|
|
|
|
reinterpret_cast<Address>(desc.buffer), desc.instr_size, 1,
|
|
|
|
&protected_instruction);
|
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
ExecuteBuffer();
|
2018-11-27 13:58:18 +00:00
|
|
|
|
|
|
|
// Deregister from the trap handler. The trap handler should not do the
|
|
|
|
// recovery now.
|
|
|
|
trap_handler::ReleaseHandlerData(handler_id);
|
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
ExecuteExpectCrash(buffer_.get());
|
2018-11-27 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(TrapHandlerTest, TestNoThreadInWasmFlag) {
|
|
|
|
// That that if the thread_in_wasm flag is not set, the trap handler does not
|
|
|
|
// get active.
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Push(scratch);
|
2021-11-17 18:14:02 +00:00
|
|
|
__ Move(scratch, crash_address_, RelocInfo::NO_INFO);
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t crash_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ testl(MemOperand(scratch, 0), Immediate(1));
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t recovery_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
|
|
|
|
|
|
|
trap_handler::ProtectedInstructionData protected_instruction{crash_offset,
|
|
|
|
recovery_offset};
|
|
|
|
trap_handler::RegisterHandlerData(reinterpret_cast<Address>(desc.buffer),
|
|
|
|
desc.instr_size, 1, &protected_instruction);
|
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
ExecuteExpectCrash(buffer_.get());
|
2018-11-27 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(TrapHandlerTest, TestCrashInWasmNoProtectedInstruction) {
|
|
|
|
// Test that if the crash in wasm happened at an instruction which is not
|
|
|
|
// protected, then the trap handler does not handle it.
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Push(scratch);
|
|
|
|
GenerateSetThreadInWasmFlagCode(&masm);
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t no_crash_offset = __ pc_offset();
|
2021-11-17 18:14:02 +00:00
|
|
|
__ Move(scratch, crash_address_, RelocInfo::NO_INFO);
|
2018-11-27 13:58:18 +00:00
|
|
|
__ testl(MemOperand(scratch, 0), Immediate(1));
|
|
|
|
// Offset where the crash is not happening.
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t recovery_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
GenerateResetThreadInWasmFlagCode(&masm);
|
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
|
|
|
|
|
|
|
trap_handler::ProtectedInstructionData protected_instruction{no_crash_offset,
|
|
|
|
recovery_offset};
|
|
|
|
trap_handler::RegisterHandlerData(reinterpret_cast<Address>(desc.buffer),
|
|
|
|
desc.instr_size, 1, &protected_instruction);
|
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
ExecuteExpectCrash(buffer_.get());
|
2018-11-27 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(TrapHandlerTest, TestCrashInWasmWrongCrashType) {
|
|
|
|
// Test that if the crash reason is not a memory access violation, then the
|
|
|
|
// wasm trap handler does not handle it.
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Push(scratch);
|
|
|
|
GenerateSetThreadInWasmFlagCode(&masm);
|
|
|
|
__ xorq(scratch, scratch);
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t crash_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ divq(scratch);
|
|
|
|
// Offset where the crash is not happening.
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t recovery_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
GenerateResetThreadInWasmFlagCode(&masm);
|
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
|
|
|
|
|
|
|
trap_handler::ProtectedInstructionData protected_instruction{crash_offset,
|
|
|
|
recovery_offset};
|
|
|
|
trap_handler::RegisterHandlerData(reinterpret_cast<Address>(desc.buffer),
|
|
|
|
desc.instr_size, 1, &protected_instruction);
|
|
|
|
|
|
|
|
#if V8_OS_POSIX
|
2019-03-22 19:23:09 +00:00
|
|
|
// On Posix, the V8 default trap handler does not register for SIGFPE,
|
|
|
|
// therefore the thread-in-wasm flag is never reset in this test. We
|
|
|
|
// therefore do not check the value of this flag.
|
2018-11-27 13:58:18 +00:00
|
|
|
bool check_wasm_flag = GetParam() != kDefault;
|
2019-03-22 19:23:09 +00:00
|
|
|
#elif V8_OS_WIN
|
|
|
|
// On Windows, the trap handler returns immediately if not an exception of
|
|
|
|
// interest.
|
|
|
|
bool check_wasm_flag = false;
|
2018-11-27 13:58:18 +00:00
|
|
|
#else
|
|
|
|
bool check_wasm_flag = true;
|
|
|
|
#endif
|
2019-01-17 10:23:16 +00:00
|
|
|
ExecuteExpectCrash(buffer_.get(), check_wasm_flag);
|
2018-11-27 13:58:18 +00:00
|
|
|
if (!check_wasm_flag) {
|
|
|
|
// Reset the thread-in-wasm flag because it was probably not reset in the
|
|
|
|
// trap handler.
|
|
|
|
*trap_handler::GetThreadInWasmThreadLocalAddress() = 0;
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 13:30:12 +00:00
|
|
|
#endif
|
2018-11-27 13:58:18 +00:00
|
|
|
|
|
|
|
class CodeRunner : public v8::base::Thread {
|
|
|
|
public:
|
2019-01-17 10:23:16 +00:00
|
|
|
CodeRunner(TrapHandlerTest* test, TestingAssemblerBuffer* buffer)
|
2018-11-27 13:58:18 +00:00
|
|
|
: Thread(Options("CodeRunner")), test_(test), buffer_(buffer) {}
|
|
|
|
|
|
|
|
void Run() override { test_->ExecuteExpectCrash(buffer_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
TrapHandlerTest* test_;
|
2019-01-17 10:23:16 +00:00
|
|
|
TestingAssemblerBuffer* buffer_;
|
2018-11-27 13:58:18 +00:00
|
|
|
};
|
|
|
|
|
2019-12-05 13:30:12 +00:00
|
|
|
// TODO(almuthanna): This test was skipped because it causes a crash when it is
|
|
|
|
// ran on Fuchsia. This issue should be solved later on
|
|
|
|
// Ticket: https://crbug.com/1028617
|
|
|
|
#if !defined(V8_TARGET_OS_FUCHSIA)
|
2018-11-27 13:58:18 +00:00
|
|
|
TEST_P(TrapHandlerTest, TestCrashInOtherThread) {
|
|
|
|
// Test setup:
|
|
|
|
// The current thread enters wasm land (sets the thread_in_wasm flag)
|
|
|
|
// A second thread crashes at a protected instruction without having the flag
|
|
|
|
// set.
|
2019-01-17 10:23:16 +00:00
|
|
|
MacroAssembler masm(nullptr, AssemblerOptions{}, CodeObjectRequired::kNo,
|
|
|
|
buffer_->CreateView());
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Push(scratch);
|
2021-11-17 18:14:02 +00:00
|
|
|
__ Move(scratch, crash_address_, RelocInfo::NO_INFO);
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t crash_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ testl(MemOperand(scratch, 0), Immediate(1));
|
2021-06-09 22:13:04 +00:00
|
|
|
uint32_t recovery_offset = __ pc_offset();
|
2018-11-27 13:58:18 +00:00
|
|
|
__ Pop(scratch);
|
|
|
|
__ Ret();
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(nullptr, &desc);
|
|
|
|
|
|
|
|
trap_handler::ProtectedInstructionData protected_instruction{crash_offset,
|
|
|
|
recovery_offset};
|
|
|
|
trap_handler::RegisterHandlerData(reinterpret_cast<Address>(desc.buffer),
|
|
|
|
desc.instr_size, 1, &protected_instruction);
|
|
|
|
|
2019-01-17 10:23:16 +00:00
|
|
|
CodeRunner runner(this, buffer_.get());
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_FALSE(GetThreadInWasmFlag());
|
2018-11-27 13:58:18 +00:00
|
|
|
// Set the thread-in-wasm flag manually in this thread.
|
|
|
|
*trap_handler::GetThreadInWasmThreadLocalAddress() = 1;
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(runner.Start());
|
2018-11-27 13:58:18 +00:00
|
|
|
runner.Join();
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(GetThreadInWasmFlag());
|
2018-11-27 13:58:18 +00:00
|
|
|
// Reset the thread-in-wasm flag.
|
|
|
|
*trap_handler::GetThreadInWasmThreadLocalAddress() = 0;
|
|
|
|
}
|
2019-12-05 13:30:12 +00:00
|
|
|
#endif
|
2018-11-27 13:58:18 +00:00
|
|
|
|
2021-04-23 14:08:09 +00:00
|
|
|
#if !V8_OS_FUCHSIA
|
2019-09-30 15:10:13 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(Traps, TrapHandlerTest,
|
2019-02-15 16:53:29 +00:00
|
|
|
::testing::Values(kDefault, kCallback),
|
|
|
|
PrintTrapHandlerTestParam);
|
2021-04-23 14:08:09 +00:00
|
|
|
#endif // !V8_OS_FUCHSIA
|
2018-11-27 13:58:18 +00:00
|
|
|
|
|
|
|
#undef __
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|