blob: 3efeb406e99a51e5c21075131adb7a581f645e16 [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
22#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Dave Allisonb373e092014-02-20 16:06:36 -080024#include "base/macros.h"
25#include "globals.h"
26#include "base/logging.h"
27#include "base/hex_dump.h"
Dave Allison69dfe512014-07-11 17:11:58 +000028#include "thread.h"
29#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 Allison8ce6b902014-08-26 11:07:58 -070078// Note this is different from the others (no underscore on 64 bit mac) due to
79// the way the symbol is defined in the .S file.
80// TODO: fix the symbols for 64 bit mac - there is a double underscore prefix for some
81// of them.
82extern "C" void art_nested_signal_return();
83
Dave Allison69dfe512014-07-11 17:11:58 +000084// Get the size of an instruction in bytes.
Dave Allisondfd3b472014-07-16 16:04:32 -070085// Return 0 if the instruction is not handled.
86static uint32_t GetInstructionSize(const uint8_t* pc) {
87#if defined(__x86_64)
88 const bool x86_64 = true;
89#else
90 const bool x86_64 = false;
Dave Allison69dfe512014-07-11 17:11:58 +000091#endif
92
Dave Allisondfd3b472014-07-16 16:04:32 -070093 const uint8_t* startpc = pc;
Dave Allison69dfe512014-07-11 17:11:58 +000094
Dave Allison69dfe512014-07-11 17:11:58 +000095 uint8_t opcode = *pc++;
Dave Allisondfd3b472014-07-16 16:04:32 -070096 uint8_t modrm;
97 bool has_modrm = false;
98 bool two_byte = false;
99 uint32_t displacement_size = 0;
100 uint32_t immediate_size = 0;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400101 bool operand_size_prefix = false;
Dave Allisondfd3b472014-07-16 16:04:32 -0700102
103 // Prefixes.
104 while (true) {
105 bool prefix_present = false;
106 switch (opcode) {
Mark Mendell5daf8e12014-09-25 15:13:39 -0400107 // Group 3
108 case 0x66:
109 operand_size_prefix = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700110 FALLTHROUGH_INTENDED;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400111
Dave Allisondfd3b472014-07-16 16:04:32 -0700112 // Group 1
113 case 0xf0:
114 case 0xf2:
115 case 0xf3:
116
117 // Group 2
118 case 0x2e:
119 case 0x36:
120 case 0x3e:
121 case 0x26:
122 case 0x64:
123 case 0x65:
124
Dave Allisondfd3b472014-07-16 16:04:32 -0700125 // Group 4
126 case 0x67:
127 opcode = *pc++;
128 prefix_present = true;
129 break;
130 }
131 if (!prefix_present) {
132 break;
133 }
134 }
135
136 if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) {
137 opcode = *pc++;
138 }
139
140 if (opcode == 0x0f) {
141 // Two byte opcode
Dave Allison69dfe512014-07-11 17:11:58 +0000142 two_byte = true;
143 opcode = *pc++;
144 }
145
Dave Allisondfd3b472014-07-16 16:04:32 -0700146 bool unhandled_instruction = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000147
Dave Allison69dfe512014-07-11 17:11:58 +0000148 if (two_byte) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700149 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700150 case 0x10: // vmovsd/ss
151 case 0x11: // vmovsd/ss
Dave Allisondfd3b472014-07-16 16:04:32 -0700152 case 0xb6: // movzx
153 case 0xb7:
154 case 0xbe: // movsx
155 case 0xbf:
156 modrm = *pc++;
157 has_modrm = true;
158 break;
159 default:
160 unhandled_instruction = true;
161 break;
162 }
163 } else {
164 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700165 case 0x88: // mov byte
166 case 0x89: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700167 case 0x8b:
168 case 0x38: // cmp with memory.
169 case 0x39:
170 case 0x3a:
171 case 0x3b:
172 case 0x3c:
173 case 0x3d:
174 case 0x85: // test.
175 modrm = *pc++;
176 has_modrm = true;
177 break;
Dave Allison69dfe512014-07-11 17:11:58 +0000178
Dave Allisondfd3b472014-07-16 16:04:32 -0700179 case 0x80: // group 1, byte immediate.
180 case 0x83:
Mark Mendella9f36ee2014-10-01 08:02:43 -0400181 case 0xc6:
Dave Allisondfd3b472014-07-16 16:04:32 -0700182 modrm = *pc++;
183 has_modrm = true;
184 immediate_size = 1;
Dave Allison69dfe512014-07-11 17:11:58 +0000185 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700186
187 case 0x81: // group 1, word immediate.
Mark Mendell40741f32015-04-20 22:10:34 -0400188 case 0xc7: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700189 modrm = *pc++;
190 has_modrm = true;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400191 immediate_size = operand_size_prefix ? 2 : 4;
Dave Allison69dfe512014-07-11 17:11:58 +0000192 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700193
194 default:
195 unhandled_instruction = true;
Dave Allison69dfe512014-07-11 17:11:58 +0000196 break;
197 }
198 }
199
Dave Allisondfd3b472014-07-16 16:04:32 -0700200 if (unhandled_instruction) {
201 VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode);
202 return 0;
203 }
204
205 if (has_modrm) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700206 uint8_t mod = (modrm >> 6) & 3U /* 0b11 */;
Dave Allisondfd3b472014-07-16 16:04:32 -0700207
208 // Check for SIB.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700209 if (mod != 3U /* 0b11 */ && (modrm & 7U /* 0b111 */) == 4) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700210 ++pc; // SIB
211 }
212
213 switch (mod) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700214 case 0U /* 0b00 */: break;
215 case 1U /* 0b01 */: displacement_size = 1; break;
216 case 2U /* 0b10 */: displacement_size = 4; break;
217 case 3U /* 0b11 */:
Dave Allisondfd3b472014-07-16 16:04:32 -0700218 break;
219 }
220 }
221
222 // Skip displacement and immediate.
223 pc += displacement_size + immediate_size;
224
225 VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc);
226 return pc - startpc;
Dave Allison69dfe512014-07-11 17:11:58 +0000227}
228
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700229void FaultManager::HandleNestedSignal(int, siginfo_t*, void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700230 // For the Intel architectures we need to go to an assembly language
231 // stub. This is because the 32 bit call to longjmp is much different
232 // from the 64 bit ABI call and pushing things onto the stack inside this
233 // handler was unwieldy and ugly. The use of the stub means we can keep
234 // this code the same for both 32 and 64 bit.
235
236 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -0700238
239 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
240 uc->CTX_JMP_BUF = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
241 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_nested_signal_return);
242}
243
Dave Allisondfd3b472014-07-16 16:04:32 -0700244void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700245 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700246 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +0000247 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
248 *out_sp = static_cast<uintptr_t>(uc->CTX_ESP);
249 VLOG(signals) << "sp: " << std::hex << *out_sp;
250 if (*out_sp == 0) {
251 return;
252 }
253
254 // In the case of a stack overflow, the stack is not valid and we can't
Dave Allisondfd3b472014-07-16 16:04:32 -0700255 // 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 +0000256 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr);
257 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Dave Allisondfd3b472014-07-16 16:04:32 -0700258#if defined(__x86_64__)
259 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64));
260#else
Dave Allison69dfe512014-07-11 17:11:58 +0000261 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86));
Dave Allisondfd3b472014-07-16 16:04:32 -0700262#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000263 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 *out_method = reinterpret_cast<ArtMethod*>(uc->CTX_METHOD);
Dave Allison69dfe512014-07-11 17:11:58 +0000265 } else {
266 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000268 }
269
270 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
271 VLOG(signals) << HexDump(pc, 32, true, "PC ");
272
Andreas Gampe94158862015-04-03 02:17:06 -0700273 if (pc == nullptr) {
274 // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below.
275 *out_method = nullptr;
276 return;
277 }
278
Dave Allison69dfe512014-07-11 17:11:58 +0000279 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700280 if (instr_size == 0) {
281 // Unknown instruction, tell caller it's not ours.
282 *out_method = nullptr;
283 return;
284 }
Dave Allison69dfe512014-07-11 17:11:58 +0000285 *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800286}
287
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100288bool NullPointerHandler::Action(int, siginfo_t* sig, void* context) {
289 if (!IsValidImplicitCheck(sig)) {
290 return false;
291 }
Dave Allison69dfe512014-07-11 17:11:58 +0000292 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
293 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
294 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
295
296 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700297 if (instr_size == 0) {
298 // Unknown instruction, can't really happen.
299 return false;
300 }
301
Dave Allison69dfe512014-07-11 17:11:58 +0000302 // We need to arrange for the signal handler to return to the null pointer
303 // exception generator. The return address must be the address of the
304 // next instruction (this instruction + instruction size). The return address
305 // is on the stack at the top address of the current frame.
306
307 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700308 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size);
309 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000310 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700311 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000312
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100313 uc->CTX_EIP = reinterpret_cast<uintptr_t>(
Andreas Gampeeba7e522016-08-04 11:16:52 -0700314 art_quick_throw_null_pointer_exception_from_signal);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100315 // Pass the faulting address as the first argument of
316 // art_quick_throw_null_pointer_exception_from_signal.
317#if defined(__x86_64__)
318 uc->CTX_RDI = reinterpret_cast<uintptr_t>(sig->si_addr);
319#else
320 uc->CTX_EAX = reinterpret_cast<uintptr_t>(sig->si_addr);
321#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000322 VLOG(signals) << "Generating null pointer exception";
323 return true;
324}
325
326// A suspend check is done using the following instruction sequence:
Dave Allisondfd3b472014-07-16 16:04:32 -0700327// (x86)
Dave Allison69dfe512014-07-11 17:11:58 +0000328// 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger
329// .. some intervening instructions.
330// 0xf720f1e6: 8500 test eax, [eax]
Dave Allisondfd3b472014-07-16 16:04:32 -0700331// (x86_64)
332// 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger
333// .. some intervening instructions.
334// 0x7f579de45da7: 8500 test eax, [eax]
Dave Allison69dfe512014-07-11 17:11:58 +0000335
336// The offset from fs is Thread::ThreadSuspendTriggerOffset().
337// To check for a suspend check, we examine the instructions that caused
338// the fault.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700339bool SuspensionHandler::Action(int, siginfo_t*, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000340 // These are the instructions to check for. The first one is the mov eax, fs:[xxx]
341 // where xxx is the offset of the suspend trigger.
Andreas Gampe542451c2016-07-26 09:02:02 -0700342 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<kRuntimePointerSize>().Int32Value();
Dave Allison69dfe512014-07-11 17:11:58 +0000343
344 VLOG(signals) << "Checking for suspension point";
Dave Allisondfd3b472014-07-16 16:04:32 -0700345#if defined(__x86_64__)
346 uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff),
347 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
348#else
Dave Allison69dfe512014-07-11 17:11:58 +0000349 uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff),
350 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
Dave Allisondfd3b472014-07-16 16:04:32 -0700351#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000352 uint8_t checkinst2[] = {0x85, 0x00};
353
354 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
355 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
356 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
357
358 if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) {
359 // Second instruction is not correct (test eax,[eax]).
360 VLOG(signals) << "Not a suspension point";
361 return false;
362 }
363
364 // The first instruction can a little bit up the stream due to load hoisting
365 // in the compiler.
366 uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions.
367 uint8_t* ptr = pc - sizeof(checkinst1);
368 bool found = false;
369 while (ptr > limit) {
370 if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) {
371 found = true;
372 break;
373 }
374 ptr -= 1;
375 }
376
377 if (found) {
378 VLOG(signals) << "suspend check match";
379
380 // We need to arrange for the signal handler to return to the null pointer
381 // exception generator. The return address must be the address of the
382 // next instruction (this instruction + 2). The return address
383 // is on the stack at the top address of the current frame.
384
385 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700386 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2);
387 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000388 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700389 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000390
Andreas Gampeeba7e522016-08-04 11:16:52 -0700391 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_quick_test_suspend);
Dave Allison69dfe512014-07-11 17:11:58 +0000392
393 // Now remove the suspend trigger that caused this fault.
394 Thread::Current()->RemoveSuspendTrigger();
395 VLOG(signals) << "removed suspend trigger invoking test suspend";
396 return true;
397 }
398 VLOG(signals) << "Not a suspend check match, first instruction mismatch";
Dave Allisonb373e092014-02-20 16:06:36 -0800399 return false;
400}
401
Dave Allison69dfe512014-07-11 17:11:58 +0000402// The stack overflow check is done using the following instruction:
403// test eax, [esp+ -xxx]
404// where 'xxx' is the size of the overflow area.
405//
406// This is done before any frame is established in the method. The return
407// address for the previous method is on the stack at ESP.
Dave Allisonb373e092014-02-20 16:06:36 -0800408
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700409bool StackOverflowHandler::Action(int, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000410 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
411 uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP);
412
413 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
414 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
415 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
416 ", fault_addr: " << fault_addr;
417
Dave Allisondfd3b472014-07-16 16:04:32 -0700418#if defined(__x86_64__)
419 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64);
420#else
Dave Allison69dfe512014-07-11 17:11:58 +0000421 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86);
Dave Allisondfd3b472014-07-16 16:04:32 -0700422#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000423
Dave Allison69dfe512014-07-11 17:11:58 +0000424 // Check that the fault address is the value expected for a stack overflow.
425 if (fault_addr != overflow_addr) {
426 VLOG(signals) << "Not a stack overflow";
427 return false;
428 }
429
Dave Allison648d7112014-07-25 16:15:27 -0700430 VLOG(signals) << "Stack overflow found";
Dave Allison69dfe512014-07-11 17:11:58 +0000431
432 // Since the compiler puts the implicit overflow
433 // check before the callee save instructions, the SP is already pointing to
434 // the previous frame.
435
Dave Allison648d7112014-07-25 16:15:27 -0700436 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Andreas Gampeeba7e522016-08-04 11:16:52 -0700437 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Dave Allison69dfe512014-07-11 17:11:58 +0000438
439 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800440}
441} // namespace art