blob: 7d8abb8cc528adcc2928e945689af663bc56628c [file] [log] [blame]
Dave Allisonb373e092014-02-20 16:06:36 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "fault_handler.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070019
Dave Allisonb373e092014-02-20 16:06:36 -080020#include <sys/ucontext.h>
Mathieu Chartiere401d142015-04-22 13:56:20 -070021
Andreas Gampec6ea7d02017-02-01 16:46:28 -080022#include "art_method.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Dave Allisonb373e092014-02-20 16:06:36 -080024#include "base/hex_dump.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "base/logging.h"
26#include "base/macros.h"
Josh Gao91119d62017-04-18 11:18:56 -070027#include "base/safe_copy.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080028#include "globals.h"
Dave Allison69dfe512014-07-11 17:11:58 +000029#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080030
Dave Allison69dfe512014-07-11 17:11:58 +000031#if defined(__APPLE__)
32#define ucontext __darwin_ucontext
Dan Albert85fa7962014-08-10 10:14:59 -070033
34#if defined(__x86_64__)
35// 64 bit mac build.
36#define CTX_ESP uc_mcontext->__ss.__rsp
37#define CTX_EIP uc_mcontext->__ss.__rip
38#define CTX_EAX uc_mcontext->__ss.__rax
Dave Allison648d7112014-07-25 16:15:27 -070039#define CTX_METHOD uc_mcontext->__ss.__rdi
Nicolas Geoffray5b643062016-06-29 14:34:21 +010040#define CTX_RDI uc_mcontext->__ss.__rdi
Dave Allison8ce6b902014-08-26 11:07:58 -070041#define CTX_JMP_BUF uc_mcontext->__ss.__rdi
Dan Albert85fa7962014-08-10 10:14:59 -070042#else
43// 32 bit mac build.
Dave Allison69dfe512014-07-11 17:11:58 +000044#define CTX_ESP uc_mcontext->__ss.__esp
45#define CTX_EIP uc_mcontext->__ss.__eip
46#define CTX_EAX uc_mcontext->__ss.__eax
Dave Allisondfd3b472014-07-16 16:04:32 -070047#define CTX_METHOD uc_mcontext->__ss.__eax
Dave Allison8ce6b902014-08-26 11:07:58 -070048#define CTX_JMP_BUF uc_mcontext->__ss.__eax
Dan Albert85fa7962014-08-10 10:14:59 -070049#endif
50
Dave Allisondfd3b472014-07-16 16:04:32 -070051#elif defined(__x86_64__)
Dan Albert85fa7962014-08-10 10:14:59 -070052// 64 bit linux build.
Dave Allisondfd3b472014-07-16 16:04:32 -070053#define CTX_ESP uc_mcontext.gregs[REG_RSP]
54#define CTX_EIP uc_mcontext.gregs[REG_RIP]
55#define CTX_EAX uc_mcontext.gregs[REG_RAX]
56#define CTX_METHOD uc_mcontext.gregs[REG_RDI]
Dave Allison8ce6b902014-08-26 11:07:58 -070057#define CTX_RDI uc_mcontext.gregs[REG_RDI]
58#define CTX_JMP_BUF uc_mcontext.gregs[REG_RDI]
Dave Allison69dfe512014-07-11 17:11:58 +000059#else
Dan Albert85fa7962014-08-10 10:14:59 -070060// 32 bit linux build.
Dave Allison69dfe512014-07-11 17:11:58 +000061#define CTX_ESP uc_mcontext.gregs[REG_ESP]
62#define CTX_EIP uc_mcontext.gregs[REG_EIP]
63#define CTX_EAX uc_mcontext.gregs[REG_EAX]
Dave Allisondfd3b472014-07-16 16:04:32 -070064#define CTX_METHOD uc_mcontext.gregs[REG_EAX]
Dave Allison8ce6b902014-08-26 11:07:58 -070065#define CTX_JMP_BUF uc_mcontext.gregs[REG_EAX]
Dave Allison69dfe512014-07-11 17:11:58 +000066#endif
Dave Allisonb373e092014-02-20 16:06:36 -080067
68//
Dave Allisondfd3b472014-07-16 16:04:32 -070069// X86 (and X86_64) specific fault handler functions.
Dave Allisonb373e092014-02-20 16:06:36 -080070//
71
72namespace art {
73
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010074extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Dave Allison648d7112014-07-25 16:15:27 -070075extern "C" void art_quick_throw_stack_overflow();
Dave Allison69dfe512014-07-11 17:11:58 +000076extern "C" void art_quick_test_suspend();
77
Dave Allison69dfe512014-07-11 17:11:58 +000078// Get the size of an instruction in bytes.
Dave Allisondfd3b472014-07-16 16:04:32 -070079// Return 0 if the instruction is not handled.
80static uint32_t GetInstructionSize(const uint8_t* pc) {
Josh Gao91119d62017-04-18 11:18:56 -070081 // Don't segfault if pc points to garbage.
82 char buf[15]; // x86/x86-64 have a maximum instruction length of 15 bytes.
83 ssize_t bytes = SafeCopy(buf, pc, sizeof(buf));
84
85 if (bytes == 0) {
86 // Nothing was readable.
87 return 0;
88 }
89
90 if (bytes == -1) {
91 // SafeCopy not supported, assume that the entire range is readable.
92 bytes = 16;
93 } else {
94 pc = reinterpret_cast<uint8_t*>(buf);
95 }
96
97#define INCREMENT_PC() \
98 do { \
99 pc++; \
100 if (pc - startpc > bytes) { \
101 return 0; \
102 } \
103 } while (0)
104
Dave Allisondfd3b472014-07-16 16:04:32 -0700105#if defined(__x86_64)
106 const bool x86_64 = true;
107#else
108 const bool x86_64 = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000109#endif
110
Dave Allisondfd3b472014-07-16 16:04:32 -0700111 const uint8_t* startpc = pc;
Dave Allison69dfe512014-07-11 17:11:58 +0000112
Josh Gao91119d62017-04-18 11:18:56 -0700113 uint8_t opcode = *pc;
114 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700115 uint8_t modrm;
116 bool has_modrm = false;
117 bool two_byte = false;
118 uint32_t displacement_size = 0;
119 uint32_t immediate_size = 0;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400120 bool operand_size_prefix = false;
Dave Allisondfd3b472014-07-16 16:04:32 -0700121
122 // Prefixes.
123 while (true) {
124 bool prefix_present = false;
125 switch (opcode) {
Mark Mendell5daf8e12014-09-25 15:13:39 -0400126 // Group 3
127 case 0x66:
128 operand_size_prefix = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700129 FALLTHROUGH_INTENDED;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400130
Dave Allisondfd3b472014-07-16 16:04:32 -0700131 // Group 1
132 case 0xf0:
133 case 0xf2:
134 case 0xf3:
135
136 // Group 2
137 case 0x2e:
138 case 0x36:
139 case 0x3e:
140 case 0x26:
141 case 0x64:
142 case 0x65:
143
Dave Allisondfd3b472014-07-16 16:04:32 -0700144 // Group 4
145 case 0x67:
Josh Gao91119d62017-04-18 11:18:56 -0700146 opcode = *pc;
147 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700148 prefix_present = true;
149 break;
150 }
151 if (!prefix_present) {
152 break;
153 }
154 }
155
156 if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) {
Josh Gao91119d62017-04-18 11:18:56 -0700157 opcode = *pc;
158 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700159 }
160
161 if (opcode == 0x0f) {
162 // Two byte opcode
Dave Allison69dfe512014-07-11 17:11:58 +0000163 two_byte = true;
Josh Gao91119d62017-04-18 11:18:56 -0700164 opcode = *pc;
165 INCREMENT_PC();
Dave Allison69dfe512014-07-11 17:11:58 +0000166 }
167
Dave Allisondfd3b472014-07-16 16:04:32 -0700168 bool unhandled_instruction = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000169
Dave Allison69dfe512014-07-11 17:11:58 +0000170 if (two_byte) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700171 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700172 case 0x10: // vmovsd/ss
173 case 0x11: // vmovsd/ss
Dave Allisondfd3b472014-07-16 16:04:32 -0700174 case 0xb6: // movzx
175 case 0xb7:
176 case 0xbe: // movsx
177 case 0xbf:
Josh Gao91119d62017-04-18 11:18:56 -0700178 modrm = *pc;
179 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700180 has_modrm = true;
181 break;
182 default:
183 unhandled_instruction = true;
184 break;
185 }
186 } else {
187 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700188 case 0x88: // mov byte
189 case 0x89: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700190 case 0x8b:
191 case 0x38: // cmp with memory.
192 case 0x39:
193 case 0x3a:
194 case 0x3b:
195 case 0x3c:
196 case 0x3d:
197 case 0x85: // test.
Josh Gao91119d62017-04-18 11:18:56 -0700198 modrm = *pc;
199 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700200 has_modrm = true;
201 break;
Dave Allison69dfe512014-07-11 17:11:58 +0000202
Dave Allisondfd3b472014-07-16 16:04:32 -0700203 case 0x80: // group 1, byte immediate.
204 case 0x83:
Mark Mendella9f36ee2014-10-01 08:02:43 -0400205 case 0xc6:
Josh Gao91119d62017-04-18 11:18:56 -0700206 modrm = *pc;
207 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700208 has_modrm = true;
209 immediate_size = 1;
Dave Allison69dfe512014-07-11 17:11:58 +0000210 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700211
212 case 0x81: // group 1, word immediate.
Mark Mendell40741f32015-04-20 22:10:34 -0400213 case 0xc7: // mov
Josh Gao91119d62017-04-18 11:18:56 -0700214 modrm = *pc;
215 INCREMENT_PC();
Dave Allisondfd3b472014-07-16 16:04:32 -0700216 has_modrm = true;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400217 immediate_size = operand_size_prefix ? 2 : 4;
Dave Allison69dfe512014-07-11 17:11:58 +0000218 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700219
Vladimir Marko953437b2016-08-24 08:30:46 +0000220 case 0xf6:
221 case 0xf7:
Josh Gao91119d62017-04-18 11:18:56 -0700222 modrm = *pc;
223 INCREMENT_PC();
Vladimir Marko953437b2016-08-24 08:30:46 +0000224 has_modrm = true;
225 switch ((modrm >> 3) & 7) { // Extract "reg/opcode" from "modr/m".
226 case 0: // test
227 immediate_size = (opcode == 0xf6) ? 1 : (operand_size_prefix ? 2 : 4);
228 break;
229 case 2: // not
230 case 3: // neg
231 case 4: // mul
232 case 5: // imul
233 case 6: // div
234 case 7: // idiv
235 break;
236 default:
237 unhandled_instruction = true;
238 break;
239 }
240 break;
241
Dave Allisondfd3b472014-07-16 16:04:32 -0700242 default:
243 unhandled_instruction = true;
Dave Allison69dfe512014-07-11 17:11:58 +0000244 break;
245 }
246 }
247
Dave Allisondfd3b472014-07-16 16:04:32 -0700248 if (unhandled_instruction) {
249 VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode);
250 return 0;
251 }
252
253 if (has_modrm) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700254 uint8_t mod = (modrm >> 6) & 3U /* 0b11 */;
Dave Allisondfd3b472014-07-16 16:04:32 -0700255
256 // Check for SIB.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700257 if (mod != 3U /* 0b11 */ && (modrm & 7U /* 0b111 */) == 4) {
Josh Gao91119d62017-04-18 11:18:56 -0700258 INCREMENT_PC(); // SIB
Dave Allisondfd3b472014-07-16 16:04:32 -0700259 }
260
261 switch (mod) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700262 case 0U /* 0b00 */: break;
263 case 1U /* 0b01 */: displacement_size = 1; break;
264 case 2U /* 0b10 */: displacement_size = 4; break;
265 case 3U /* 0b11 */:
Dave Allisondfd3b472014-07-16 16:04:32 -0700266 break;
267 }
268 }
269
270 // Skip displacement and immediate.
271 pc += displacement_size + immediate_size;
272
273 VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc);
Josh Gao91119d62017-04-18 11:18:56 -0700274 if (pc - startpc > bytes) {
275 return 0;
276 }
Dave Allisondfd3b472014-07-16 16:04:32 -0700277 return pc - startpc;
Dave Allison69dfe512014-07-11 17:11:58 +0000278}
279
Dave Allisondfd3b472014-07-16 16:04:32 -0700280void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700282 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +0000283 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
284 *out_sp = static_cast<uintptr_t>(uc->CTX_ESP);
285 VLOG(signals) << "sp: " << std::hex << *out_sp;
286 if (*out_sp == 0) {
287 return;
288 }
289
290 // In the case of a stack overflow, the stack is not valid and we can't
Dave Allisondfd3b472014-07-16 16:04:32 -0700291 // get the method from the top of the stack. However it's in EAX(x86)/RDI(x86_64).
Dave Allison69dfe512014-07-11 17:11:58 +0000292 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr);
293 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Dave Allisondfd3b472014-07-16 16:04:32 -0700294#if defined(__x86_64__)
295 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64));
296#else
Dave Allison69dfe512014-07-11 17:11:58 +0000297 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86));
Dave Allisondfd3b472014-07-16 16:04:32 -0700298#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000299 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 *out_method = reinterpret_cast<ArtMethod*>(uc->CTX_METHOD);
Dave Allison69dfe512014-07-11 17:11:58 +0000301 } else {
302 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000304 }
305
306 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
307 VLOG(signals) << HexDump(pc, 32, true, "PC ");
308
Andreas Gampe94158862015-04-03 02:17:06 -0700309 if (pc == nullptr) {
310 // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below.
311 *out_method = nullptr;
312 return;
313 }
314
Dave Allison69dfe512014-07-11 17:11:58 +0000315 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700316 if (instr_size == 0) {
317 // Unknown instruction, tell caller it's not ours.
318 *out_method = nullptr;
319 return;
320 }
Dave Allison69dfe512014-07-11 17:11:58 +0000321 *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800322}
323
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100324bool NullPointerHandler::Action(int, siginfo_t* sig, void* context) {
325 if (!IsValidImplicitCheck(sig)) {
326 return false;
327 }
Dave Allison69dfe512014-07-11 17:11:58 +0000328 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
329 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
330 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
331
332 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700333 if (instr_size == 0) {
334 // Unknown instruction, can't really happen.
335 return false;
336 }
337
Dave Allison69dfe512014-07-11 17:11:58 +0000338 // We need to arrange for the signal handler to return to the null pointer
339 // exception generator. The return address must be the address of the
340 // next instruction (this instruction + instruction size). The return address
341 // is on the stack at the top address of the current frame.
342
Vladimir Marko3b7537b2016-09-13 11:56:01 +0000343 // Push the return address and fault address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700344 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size);
Vladimir Marko3b7537b2016-09-13 11:56:01 +0000345 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - 2 * sizeof(uintptr_t));
346 next_sp[1] = retaddr;
347 next_sp[0] = reinterpret_cast<uintptr_t>(sig->si_addr);
Dave Allisondfd3b472014-07-16 16:04:32 -0700348 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000349
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100350 uc->CTX_EIP = reinterpret_cast<uintptr_t>(
Andreas Gampeeba7e522016-08-04 11:16:52 -0700351 art_quick_throw_null_pointer_exception_from_signal);
Dave Allison69dfe512014-07-11 17:11:58 +0000352 VLOG(signals) << "Generating null pointer exception";
353 return true;
354}
355
356// A suspend check is done using the following instruction sequence:
Dave Allisondfd3b472014-07-16 16:04:32 -0700357// (x86)
Dave Allison69dfe512014-07-11 17:11:58 +0000358// 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger
359// .. some intervening instructions.
360// 0xf720f1e6: 8500 test eax, [eax]
Dave Allisondfd3b472014-07-16 16:04:32 -0700361// (x86_64)
362// 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger
363// .. some intervening instructions.
364// 0x7f579de45da7: 8500 test eax, [eax]
Dave Allison69dfe512014-07-11 17:11:58 +0000365
366// The offset from fs is Thread::ThreadSuspendTriggerOffset().
367// To check for a suspend check, we examine the instructions that caused
368// the fault.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700369bool SuspensionHandler::Action(int, siginfo_t*, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000370 // These are the instructions to check for. The first one is the mov eax, fs:[xxx]
371 // where xxx is the offset of the suspend trigger.
Andreas Gampe542451c2016-07-26 09:02:02 -0700372 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<kRuntimePointerSize>().Int32Value();
Dave Allison69dfe512014-07-11 17:11:58 +0000373
374 VLOG(signals) << "Checking for suspension point";
Dave Allisondfd3b472014-07-16 16:04:32 -0700375#if defined(__x86_64__)
376 uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff),
377 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
378#else
Dave Allison69dfe512014-07-11 17:11:58 +0000379 uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff),
380 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
Dave Allisondfd3b472014-07-16 16:04:32 -0700381#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000382 uint8_t checkinst2[] = {0x85, 0x00};
383
384 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
385 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
386 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
387
388 if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) {
389 // Second instruction is not correct (test eax,[eax]).
390 VLOG(signals) << "Not a suspension point";
391 return false;
392 }
393
394 // The first instruction can a little bit up the stream due to load hoisting
395 // in the compiler.
396 uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions.
397 uint8_t* ptr = pc - sizeof(checkinst1);
398 bool found = false;
399 while (ptr > limit) {
400 if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) {
401 found = true;
402 break;
403 }
404 ptr -= 1;
405 }
406
407 if (found) {
408 VLOG(signals) << "suspend check match";
409
410 // We need to arrange for the signal handler to return to the null pointer
411 // exception generator. The return address must be the address of the
412 // next instruction (this instruction + 2). The return address
413 // is on the stack at the top address of the current frame.
414
415 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700416 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2);
417 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000418 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700419 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000420
Andreas Gampeeba7e522016-08-04 11:16:52 -0700421 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_quick_test_suspend);
Dave Allison69dfe512014-07-11 17:11:58 +0000422
423 // Now remove the suspend trigger that caused this fault.
424 Thread::Current()->RemoveSuspendTrigger();
425 VLOG(signals) << "removed suspend trigger invoking test suspend";
426 return true;
427 }
428 VLOG(signals) << "Not a suspend check match, first instruction mismatch";
Dave Allisonb373e092014-02-20 16:06:36 -0800429 return false;
430}
431
Dave Allison69dfe512014-07-11 17:11:58 +0000432// The stack overflow check is done using the following instruction:
433// test eax, [esp+ -xxx]
434// where 'xxx' is the size of the overflow area.
435//
436// This is done before any frame is established in the method. The return
437// address for the previous method is on the stack at ESP.
Dave Allisonb373e092014-02-20 16:06:36 -0800438
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700439bool StackOverflowHandler::Action(int, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000440 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
441 uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP);
442
443 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
444 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
445 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
446 ", fault_addr: " << fault_addr;
447
Dave Allisondfd3b472014-07-16 16:04:32 -0700448#if defined(__x86_64__)
449 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64);
450#else
Dave Allison69dfe512014-07-11 17:11:58 +0000451 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86);
Dave Allisondfd3b472014-07-16 16:04:32 -0700452#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000453
Dave Allison69dfe512014-07-11 17:11:58 +0000454 // Check that the fault address is the value expected for a stack overflow.
455 if (fault_addr != overflow_addr) {
456 VLOG(signals) << "Not a stack overflow";
457 return false;
458 }
459
Dave Allison648d7112014-07-25 16:15:27 -0700460 VLOG(signals) << "Stack overflow found";
Dave Allison69dfe512014-07-11 17:11:58 +0000461
462 // Since the compiler puts the implicit overflow
463 // check before the callee save instructions, the SP is already pointing to
464 // the previous frame.
465
Dave Allison648d7112014-07-25 16:15:27 -0700466 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Andreas Gampeeba7e522016-08-04 11:16:52 -0700467 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Dave Allison69dfe512014-07-11 17:11:58 +0000468
469 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800470}
471} // namespace art