blob: 2de69aa679e14ddb0f1b8d7e69ea45dd848a1c13 [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"
19#include <sys/ucontext.h>
20#include "base/macros.h"
21#include "globals.h"
22#include "base/logging.h"
23#include "base/hex_dump.h"
Dave Allison69dfe512014-07-11 17:11:58 +000024#include "mirror/art_method.h"
25#include "mirror/art_method-inl.h"
26#include "thread.h"
27#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080028
Dave Allison69dfe512014-07-11 17:11:58 +000029#if defined(__APPLE__)
30#define ucontext __darwin_ucontext
Dan Albert85fa7962014-08-10 10:14:59 -070031
32#if defined(__x86_64__)
33// 64 bit mac build.
34#define CTX_ESP uc_mcontext->__ss.__rsp
35#define CTX_EIP uc_mcontext->__ss.__rip
36#define CTX_EAX uc_mcontext->__ss.__rax
Dave Allison648d7112014-07-25 16:15:27 -070037#define CTX_METHOD uc_mcontext->__ss.__rdi
Dave Allison8ce6b902014-08-26 11:07:58 -070038#define CTX_JMP_BUF uc_mcontext->__ss.__rdi
Dan Albert85fa7962014-08-10 10:14:59 -070039#else
40// 32 bit mac build.
Dave Allison69dfe512014-07-11 17:11:58 +000041#define CTX_ESP uc_mcontext->__ss.__esp
42#define CTX_EIP uc_mcontext->__ss.__eip
43#define CTX_EAX uc_mcontext->__ss.__eax
Dave Allisondfd3b472014-07-16 16:04:32 -070044#define CTX_METHOD uc_mcontext->__ss.__eax
Dave Allison8ce6b902014-08-26 11:07:58 -070045#define CTX_JMP_BUF uc_mcontext->__ss.__eax
Dan Albert85fa7962014-08-10 10:14:59 -070046#endif
47
Dave Allisondfd3b472014-07-16 16:04:32 -070048#elif defined(__x86_64__)
Dan Albert85fa7962014-08-10 10:14:59 -070049// 64 bit linux build.
Dave Allisondfd3b472014-07-16 16:04:32 -070050#define CTX_ESP uc_mcontext.gregs[REG_RSP]
51#define CTX_EIP uc_mcontext.gregs[REG_RIP]
52#define CTX_EAX uc_mcontext.gregs[REG_RAX]
53#define CTX_METHOD uc_mcontext.gregs[REG_RDI]
Dave Allison8ce6b902014-08-26 11:07:58 -070054#define CTX_RDI uc_mcontext.gregs[REG_RDI]
55#define CTX_JMP_BUF uc_mcontext.gregs[REG_RDI]
Dave Allison69dfe512014-07-11 17:11:58 +000056#else
Dan Albert85fa7962014-08-10 10:14:59 -070057// 32 bit linux build.
Dave Allison69dfe512014-07-11 17:11:58 +000058#define CTX_ESP uc_mcontext.gregs[REG_ESP]
59#define CTX_EIP uc_mcontext.gregs[REG_EIP]
60#define CTX_EAX uc_mcontext.gregs[REG_EAX]
Dave Allisondfd3b472014-07-16 16:04:32 -070061#define CTX_METHOD uc_mcontext.gregs[REG_EAX]
Dave Allison8ce6b902014-08-26 11:07:58 -070062#define CTX_JMP_BUF uc_mcontext.gregs[REG_EAX]
Dave Allison69dfe512014-07-11 17:11:58 +000063#endif
Dave Allisonb373e092014-02-20 16:06:36 -080064
65//
Dave Allisondfd3b472014-07-16 16:04:32 -070066// X86 (and X86_64) specific fault handler functions.
Dave Allisonb373e092014-02-20 16:06:36 -080067//
68
69namespace art {
70
Dan Albert85fa7962014-08-10 10:14:59 -070071#if defined(__APPLE__) && defined(__x86_64__)
72// mac symbols have a prefix of _ on x86_64
73extern "C" void _art_quick_throw_null_pointer_exception();
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -070074extern "C" void _art_quick_throw_stack_overflow();
Dan Albert85fa7962014-08-10 10:14:59 -070075extern "C" void _art_quick_test_suspend();
76#define EXT_SYM(sym) _ ## sym
77#else
Dave Allison69dfe512014-07-11 17:11:58 +000078extern "C" void art_quick_throw_null_pointer_exception();
Dave Allison648d7112014-07-25 16:15:27 -070079extern "C" void art_quick_throw_stack_overflow();
Dave Allison69dfe512014-07-11 17:11:58 +000080extern "C" void art_quick_test_suspend();
Dan Albert85fa7962014-08-10 10:14:59 -070081#define EXT_SYM(sym) sym
82#endif
Dave Allison69dfe512014-07-11 17:11:58 +000083
Dave Allison8ce6b902014-08-26 11:07:58 -070084// Note this is different from the others (no underscore on 64 bit mac) due to
85// the way the symbol is defined in the .S file.
86// TODO: fix the symbols for 64 bit mac - there is a double underscore prefix for some
87// of them.
88extern "C" void art_nested_signal_return();
89
Dave Allison69dfe512014-07-11 17:11:58 +000090// Get the size of an instruction in bytes.
Dave Allisondfd3b472014-07-16 16:04:32 -070091// Return 0 if the instruction is not handled.
92static uint32_t GetInstructionSize(const uint8_t* pc) {
93#if defined(__x86_64)
94 const bool x86_64 = true;
95#else
96 const bool x86_64 = false;
Dave Allison69dfe512014-07-11 17:11:58 +000097#endif
98
Dave Allisondfd3b472014-07-16 16:04:32 -070099 const uint8_t* startpc = pc;
Dave Allison69dfe512014-07-11 17:11:58 +0000100
Dave Allison69dfe512014-07-11 17:11:58 +0000101 uint8_t opcode = *pc++;
Dave Allisondfd3b472014-07-16 16:04:32 -0700102 uint8_t modrm;
103 bool has_modrm = false;
104 bool two_byte = false;
105 uint32_t displacement_size = 0;
106 uint32_t immediate_size = 0;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400107 bool operand_size_prefix = false;
Dave Allisondfd3b472014-07-16 16:04:32 -0700108
109 // Prefixes.
110 while (true) {
111 bool prefix_present = false;
112 switch (opcode) {
Mark Mendell5daf8e12014-09-25 15:13:39 -0400113 // Group 3
114 case 0x66:
115 operand_size_prefix = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700116 FALLTHROUGH_INTENDED;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400117
Dave Allisondfd3b472014-07-16 16:04:32 -0700118 // Group 1
119 case 0xf0:
120 case 0xf2:
121 case 0xf3:
122
123 // Group 2
124 case 0x2e:
125 case 0x36:
126 case 0x3e:
127 case 0x26:
128 case 0x64:
129 case 0x65:
130
Dave Allisondfd3b472014-07-16 16:04:32 -0700131 // Group 4
132 case 0x67:
133 opcode = *pc++;
134 prefix_present = true;
135 break;
136 }
137 if (!prefix_present) {
138 break;
139 }
140 }
141
142 if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) {
143 opcode = *pc++;
144 }
145
146 if (opcode == 0x0f) {
147 // Two byte opcode
Dave Allison69dfe512014-07-11 17:11:58 +0000148 two_byte = true;
149 opcode = *pc++;
150 }
151
Dave Allisondfd3b472014-07-16 16:04:32 -0700152 bool unhandled_instruction = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000153
Dave Allison69dfe512014-07-11 17:11:58 +0000154 if (two_byte) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700155 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700156 case 0x10: // vmovsd/ss
157 case 0x11: // vmovsd/ss
Dave Allisondfd3b472014-07-16 16:04:32 -0700158 case 0xb6: // movzx
159 case 0xb7:
160 case 0xbe: // movsx
161 case 0xbf:
162 modrm = *pc++;
163 has_modrm = true;
164 break;
165 default:
166 unhandled_instruction = true;
167 break;
168 }
169 } else {
170 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700171 case 0x88: // mov byte
172 case 0x89: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700173 case 0x8b:
174 case 0x38: // cmp with memory.
175 case 0x39:
176 case 0x3a:
177 case 0x3b:
178 case 0x3c:
179 case 0x3d:
180 case 0x85: // test.
181 modrm = *pc++;
182 has_modrm = true;
183 break;
Dave Allison69dfe512014-07-11 17:11:58 +0000184
Dave Allisondfd3b472014-07-16 16:04:32 -0700185 case 0x80: // group 1, byte immediate.
186 case 0x83:
Mark Mendella9f36ee2014-10-01 08:02:43 -0400187 case 0xc6:
Dave Allisondfd3b472014-07-16 16:04:32 -0700188 modrm = *pc++;
189 has_modrm = true;
190 immediate_size = 1;
Dave Allison69dfe512014-07-11 17:11:58 +0000191 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700192
193 case 0x81: // group 1, word immediate.
Mark Mendell40741f32015-04-20 22:10:34 -0400194 case 0xc7: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700195 modrm = *pc++;
196 has_modrm = true;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400197 immediate_size = operand_size_prefix ? 2 : 4;
Dave Allison69dfe512014-07-11 17:11:58 +0000198 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700199
200 default:
201 unhandled_instruction = true;
Dave Allison69dfe512014-07-11 17:11:58 +0000202 break;
203 }
204 }
205
Dave Allisondfd3b472014-07-16 16:04:32 -0700206 if (unhandled_instruction) {
207 VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode);
208 return 0;
209 }
210
211 if (has_modrm) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700212 uint8_t mod = (modrm >> 6) & 3U /* 0b11 */;
Dave Allisondfd3b472014-07-16 16:04:32 -0700213
214 // Check for SIB.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700215 if (mod != 3U /* 0b11 */ && (modrm & 7U /* 0b111 */) == 4) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700216 ++pc; // SIB
217 }
218
219 switch (mod) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700220 case 0U /* 0b00 */: break;
221 case 1U /* 0b01 */: displacement_size = 1; break;
222 case 2U /* 0b10 */: displacement_size = 4; break;
223 case 3U /* 0b11 */:
Dave Allisondfd3b472014-07-16 16:04:32 -0700224 break;
225 }
226 }
227
228 // Skip displacement and immediate.
229 pc += displacement_size + immediate_size;
230
231 VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc);
232 return pc - startpc;
Dave Allison69dfe512014-07-11 17:11:58 +0000233}
234
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700235void FaultManager::HandleNestedSignal(int, siginfo_t*, void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700236 // For the Intel architectures we need to go to an assembly language
237 // stub. This is because the 32 bit call to longjmp is much different
238 // from the 64 bit ABI call and pushing things onto the stack inside this
239 // handler was unwieldy and ugly. The use of the stub means we can keep
240 // this code the same for both 32 and 64 bit.
241
242 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -0700244
245 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
246 uc->CTX_JMP_BUF = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
247 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_nested_signal_return);
248}
249
Dave Allisondfd3b472014-07-16 16:04:32 -0700250void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Dave Allison69dfe512014-07-11 17:11:58 +0000251 mirror::ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700252 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +0000253 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
254 *out_sp = static_cast<uintptr_t>(uc->CTX_ESP);
255 VLOG(signals) << "sp: " << std::hex << *out_sp;
256 if (*out_sp == 0) {
257 return;
258 }
259
260 // In the case of a stack overflow, the stack is not valid and we can't
Dave Allisondfd3b472014-07-16 16:04:32 -0700261 // 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 +0000262 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr);
263 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Dave Allisondfd3b472014-07-16 16:04:32 -0700264#if defined(__x86_64__)
265 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64));
266#else
Dave Allison69dfe512014-07-11 17:11:58 +0000267 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86));
Dave Allisondfd3b472014-07-16 16:04:32 -0700268#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000269 if (overflow_addr == fault_addr) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700270 *out_method = reinterpret_cast<mirror::ArtMethod*>(uc->CTX_METHOD);
Dave Allison69dfe512014-07-11 17:11:58 +0000271 } else {
272 // The method is at the top of the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700273 *out_method = (reinterpret_cast<StackReference<mirror::ArtMethod>* >(*out_sp)[0]).AsMirrorPtr();
Dave Allison69dfe512014-07-11 17:11:58 +0000274 }
275
276 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
277 VLOG(signals) << HexDump(pc, 32, true, "PC ");
278
Andreas Gampe94158862015-04-03 02:17:06 -0700279 if (pc == nullptr) {
280 // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below.
281 *out_method = nullptr;
282 return;
283 }
284
Dave Allison69dfe512014-07-11 17:11:58 +0000285 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700286 if (instr_size == 0) {
287 // Unknown instruction, tell caller it's not ours.
288 *out_method = nullptr;
289 return;
290 }
Dave Allison69dfe512014-07-11 17:11:58 +0000291 *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800292}
293
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700294bool NullPointerHandler::Action(int, siginfo_t*, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000295 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
296 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
297 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
298
299 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700300 if (instr_size == 0) {
301 // Unknown instruction, can't really happen.
302 return false;
303 }
304
Dave Allison69dfe512014-07-11 17:11:58 +0000305 // We need to arrange for the signal handler to return to the null pointer
306 // exception generator. The return address must be the address of the
307 // next instruction (this instruction + instruction size). The return address
308 // is on the stack at the top address of the current frame.
309
310 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700311 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size);
312 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000313 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700314 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000315
Dan Albert85fa7962014-08-10 10:14:59 -0700316 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_null_pointer_exception));
Dave Allison69dfe512014-07-11 17:11:58 +0000317 VLOG(signals) << "Generating null pointer exception";
318 return true;
319}
320
321// A suspend check is done using the following instruction sequence:
Dave Allisondfd3b472014-07-16 16:04:32 -0700322// (x86)
Dave Allison69dfe512014-07-11 17:11:58 +0000323// 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger
324// .. some intervening instructions.
325// 0xf720f1e6: 8500 test eax, [eax]
Dave Allisondfd3b472014-07-16 16:04:32 -0700326// (x86_64)
327// 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger
328// .. some intervening instructions.
329// 0x7f579de45da7: 8500 test eax, [eax]
Dave Allison69dfe512014-07-11 17:11:58 +0000330
331// The offset from fs is Thread::ThreadSuspendTriggerOffset().
332// To check for a suspend check, we examine the instructions that caused
333// the fault.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700334bool SuspensionHandler::Action(int, siginfo_t*, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000335 // These are the instructions to check for. The first one is the mov eax, fs:[xxx]
336 // where xxx is the offset of the suspend trigger.
Dave Allisondfd3b472014-07-16 16:04:32 -0700337#if defined(__x86_64__)
338 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<8>().Int32Value();
339#else
Dave Allison69dfe512014-07-11 17:11:58 +0000340 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<4>().Int32Value();
Dave Allisondfd3b472014-07-16 16:04:32 -0700341#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000342
343 VLOG(signals) << "Checking for suspension point";
Dave Allisondfd3b472014-07-16 16:04:32 -0700344#if defined(__x86_64__)
345 uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff),
346 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
347#else
Dave Allison69dfe512014-07-11 17:11:58 +0000348 uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff),
349 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
Dave Allisondfd3b472014-07-16 16:04:32 -0700350#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000351 uint8_t checkinst2[] = {0x85, 0x00};
352
353 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
354 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
355 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
356
357 if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) {
358 // Second instruction is not correct (test eax,[eax]).
359 VLOG(signals) << "Not a suspension point";
360 return false;
361 }
362
363 // The first instruction can a little bit up the stream due to load hoisting
364 // in the compiler.
365 uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions.
366 uint8_t* ptr = pc - sizeof(checkinst1);
367 bool found = false;
368 while (ptr > limit) {
369 if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) {
370 found = true;
371 break;
372 }
373 ptr -= 1;
374 }
375
376 if (found) {
377 VLOG(signals) << "suspend check match";
378
379 // We need to arrange for the signal handler to return to the null pointer
380 // exception generator. The return address must be the address of the
381 // next instruction (this instruction + 2). The return address
382 // is on the stack at the top address of the current frame.
383
384 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700385 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2);
386 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000387 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700388 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000389
Dan Albert85fa7962014-08-10 10:14:59 -0700390 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_test_suspend));
Dave Allison69dfe512014-07-11 17:11:58 +0000391
392 // Now remove the suspend trigger that caused this fault.
393 Thread::Current()->RemoveSuspendTrigger();
394 VLOG(signals) << "removed suspend trigger invoking test suspend";
395 return true;
396 }
397 VLOG(signals) << "Not a suspend check match, first instruction mismatch";
Dave Allisonb373e092014-02-20 16:06:36 -0800398 return false;
399}
400
Dave Allison69dfe512014-07-11 17:11:58 +0000401// The stack overflow check is done using the following instruction:
402// test eax, [esp+ -xxx]
403// where 'xxx' is the size of the overflow area.
404//
405// This is done before any frame is established in the method. The return
406// address for the previous method is on the stack at ESP.
Dave Allisonb373e092014-02-20 16:06:36 -0800407
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700408bool StackOverflowHandler::Action(int, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000409 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
410 uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP);
411
412 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
413 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
414 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
415 ", fault_addr: " << fault_addr;
416
Dave Allisondfd3b472014-07-16 16:04:32 -0700417#if defined(__x86_64__)
418 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64);
419#else
Dave Allison69dfe512014-07-11 17:11:58 +0000420 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86);
Dave Allisondfd3b472014-07-16 16:04:32 -0700421#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000422
Dave Allison69dfe512014-07-11 17:11:58 +0000423 // Check that the fault address is the value expected for a stack overflow.
424 if (fault_addr != overflow_addr) {
425 VLOG(signals) << "Not a stack overflow";
426 return false;
427 }
428
Dave Allison648d7112014-07-25 16:15:27 -0700429 VLOG(signals) << "Stack overflow found";
Dave Allison69dfe512014-07-11 17:11:58 +0000430
431 // Since the compiler puts the implicit overflow
432 // check before the callee save instructions, the SP is already pointing to
433 // the previous frame.
434
Dave Allison648d7112014-07-25 16:15:27 -0700435 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -0700436 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_stack_overflow));
Dave Allison69dfe512014-07-11 17:11:58 +0000437
438 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800439}
440} // namespace art