Commit 46f6e24a authored by Eric Holk (eholk)'s avatar Eric Holk (eholk) Committed by Commit Bot

[wasm] trap handlers: Factor out landing pad search code

This is the first of a series of refactoring CLs to make way for
Windows trap handling support.

See https://chromium-review.googlesource.com/c/v8/v8/+/626558 as well.

Bug: 
Change-Id: I5fe9ef9c1cec58a81e51fcffbbe4419e0e298ab7
Reviewed-on: https://chromium-review.googlesource.com/644104Reviewed-by: 's avatarBrad Nelson <bradnelson@chromium.org>
Commit-Queue: Eric Holk <eholk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48191}
parent 70b79c95
......@@ -98,49 +98,58 @@ bool TryHandleSignal(int signum, siginfo_t* info, ucontext_t* context) {
SigUnmaskStack unmask(sigs);
uintptr_t fault_addr = context->uc_mcontext.gregs[REG_RIP];
uintptr_t landing_pad = 0;
if (TryFindLandingPad(fault_addr, &landing_pad)) {
// Tell the caller to return to the landing pad.
context->uc_mcontext.gregs[REG_RIP] = landing_pad;
return true;
}
} // end signal mask scope
// TODO(eholk): broad code range check
// Taking locks in a signal handler is risky because a fault in the signal
// handler could lead to a deadlock when attempting to acquire the lock
// again. We guard against this case with g_thread_in_wasm_code. The lock
// may only be taken when not executing Wasm code (an assert in
// MetadataLock's constructor ensures this). This signal handler will bail
// out before trying to take the lock if g_thread_in_wasm_code is not set.
MetadataLock lock_holder;
// If we get here, it's not a recoverable wasm fault, so we go to the next
// handler.
g_thread_in_wasm_code = true;
return false;
}
for (size_t i = 0; i < gNumCodeObjects; ++i) {
const CodeProtectionInfo* data = gCodeObjects[i].code_info;
if (data == nullptr) {
continue;
}
const uintptr_t base = reinterpret_cast<uintptr_t>(data->base);
// This function contains the platform independent portions of fault
// classification.
bool TryFindLandingPad(uintptr_t fault_addr, uintptr_t* landing_pad) {
// TODO(eholk): broad code range check
// Taking locks in a signal handler is risky because a fault in the signal
// handler could lead to a deadlock when attempting to acquire the lock
// again. We guard against this case with g_thread_in_wasm_code. The lock
// may only be taken when not executing Wasm code (an assert in
// MetadataLock's constructor ensures this). This signal handler will bail
// out before trying to take the lock if g_thread_in_wasm_code is not set.
MetadataLock lock_holder;
for (size_t i = 0; i < gNumCodeObjects; ++i) {
const CodeProtectionInfo* data = gCodeObjects[i].code_info;
if (data == nullptr) {
continue;
}
const uintptr_t base = reinterpret_cast<uintptr_t>(data->base);
if (fault_addr >= base && fault_addr < base + data->size) {
// Hurray, we found the code object. Check for protected addresses.
const ptrdiff_t offset = fault_addr - base;
if (fault_addr >= base && fault_addr < base + data->size) {
// Hurray, we found the code object. Check for protected addresses.
const ptrdiff_t offset = fault_addr - base;
for (unsigned i = 0; i < data->num_protected_instructions; ++i) {
if (data->instructions[i].instr_offset == offset) {
// Hurray again, we found the actual instruction. Tell the caller to
// return to the landing pad.
context->uc_mcontext.gregs[REG_RIP] =
data->instructions[i].landing_offset + base;
for (unsigned i = 0; i < data->num_protected_instructions; ++i) {
if (data->instructions[i].instr_offset == offset) {
// Hurray again, we found the actual instruction.
*landing_pad = data->instructions[i].landing_offset + base;
gRecoveredTrapCount.store(
gRecoveredTrapCount.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
gRecoveredTrapCount.store(
gRecoveredTrapCount.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
return true;
}
return true;
}
}
}
} // end signal mask scope
// If we get here, it's not a recoverable wasm fault, so we go to the next
// handler.
g_thread_in_wasm_code = true;
}
return false;
}
#endif // V8_TRAP_HANDLER_SUPPORTED && V8_OS_LINUX
......
......@@ -62,6 +62,12 @@ extern CodeProtectionInfoListEntry* gCodeObjects;
extern std::atomic_size_t gRecoveredTrapCount;
// Searches the fault location table for an entry matching fault_addr. If found,
// returns true and sets landing_pad to the address of a fragment of code that
// can recover from this fault. Otherwise, returns false and leaves offset
// unchanged.
bool TryFindLandingPad(uintptr_t fault_addr, uintptr_t* landing_pad);
} // namespace trap_handler
} // namespace internal
} // namespace v8
......
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