48namespace trap_handler {
50#if V8_TRAP_HANDLER_SUPPORTED
52#if V8_OS_LINUX && V8_HOST_ARCH_ARM64
53#define CONTEXT_REG(reg, REG) &uc->uc_mcontext.regs[REG]
54#elif V8_OS_LINUX && (V8_HOST_ARCH_LOONG64 || V8_HOST_ARCH_RISCV64)
55#define CONTEXT_REG(reg, REG) &uc->uc_mcontext.__gregs[REG]
57#define CONTEXT_REG(reg, REG) &uc->uc_mcontext.gregs[REG_##REG]
58#elif V8_OS_DARWIN && V8_HOST_ARCH_ARM64
59#define CONTEXT_REG(reg, REG) &uc->uc_mcontext->__ss.__x[REG]
61#define CONTEXT_REG(reg, REG) &uc->uc_mcontext->__ss.__##reg
63#define CONTEXT_REG(reg, REG) &uc->uc_mcontext.mc_##reg
65#error "Unsupported platform."
68#if V8_OS_LINUX && V8_HOST_ARCH_ARM64
69#define CONTEXT_PC() &uc->uc_mcontext.pc
70#elif V8_OS_DARWIN && V8_HOST_ARCH_ARM64
71#define CONTEXT_PC() &uc->uc_mcontext->__ss.__pc
72#elif V8_OS_LINUX && V8_HOST_ARCH_LOONG64
73#define CONTEXT_PC() &uc->uc_mcontext.__pc
74#elif V8_OS_LINUX && V8_HOST_ARCH_RISCV64
75#define CONTEXT_PC() &uc->uc_mcontext.__gregs[REG_PC]
78bool IsKernelGeneratedSignal(siginfo_t* info) {
82 return info->si_code > 0 && info->si_code != SI_USER &&
83 info->si_code != SI_QUEUE && info->si_code != SI_TIMER &&
84 info->si_code != SI_ASYNCIO && info->si_code != SI_MESGQ;
87class UnmaskOobSignalScope {
89 UnmaskOobSignalScope() {
94 sigaddset(&sigs, kOobSignal);
95 pthread_sigmask(SIG_UNBLOCK, &sigs, &old_mask_);
98 UnmaskOobSignalScope(
const UnmaskOobSignalScope&) =
delete;
99 void operator=(
const UnmaskOobSignalScope&) =
delete;
101 ~UnmaskOobSignalScope() { pthread_sigmask(SIG_SETMASK, &old_mask_,
nullptr); }
107#ifdef V8_TRAP_HANDLER_VIA_SIMULATOR
110extern char probe_memory_continuation[]
112 asm(
"_v8_simulator_probe_memory_continuation");
114 asm(
"v8_simulator_probe_memory_continuation");
132 if (signum != kOobSignal)
return false;
135 if (!IsKernelGeneratedSignal(info))
return false;
140 uintptr_t access_addr =
reinterpret_cast<uintptr_t
>(info->si_addr);
151 UnmaskOobSignalScope unmask_oob_signal;
153 ucontext_t* uc =
reinterpret_cast<ucontext_t*
>(
context);
155 auto* context_ip = CONTEXT_REG(rip, RIP);
156#elif V8_HOST_ARCH_ARM64
157 auto* context_ip = CONTEXT_PC();
158#elif V8_HOST_ARCH_LOONG64
159 auto* context_ip = CONTEXT_PC();
160#elif V8_HOST_ARCH_RISCV64
161 auto* context_ip = CONTEXT_PC();
163#error "Unsupported architecture."
166 uintptr_t fault_addr = *context_ip;
167#ifdef V8_TRAP_HANDLER_VIA_SIMULATOR
169 if (fault_addr !=
reinterpret_cast<uintptr_t
>(&ProbeMemory)) {
174 auto* simulated_ip_reg = CONTEXT_REG(rsi, RSI);
178 auto* return_reg = CONTEXT_REG(rax, RAX);
183 *context_ip =
reinterpret_cast<uintptr_t
>(&probe_memory_continuation);
191 auto* fault_address_reg = CONTEXT_REG(r10, R10);
192#elif V8_HOST_ARCH_ARM64
193 auto* fault_address_reg = CONTEXT_REG(x16, 16);
194#elif V8_HOST_ARCH_LOONG64
195 auto* fault_address_reg = CONTEXT_REG(t6, 18);
196#elif V8_HOST_ARCH_RISCV64
197 auto* fault_address_reg = CONTEXT_REG(t6, 31);
199#error "Unsupported architecture."
201 *fault_address_reg = fault_addr;
212void HandleSignal(
int signum, siginfo_t* info,
void* context) {
223 if (!IsKernelGeneratedSignal(info)) {