blob: 667d200732c0b5ab7d8e1dcbab6c9783e38fb892 [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"
Dave Allisonb373e092014-02-20 16:06:36 -080023#include "base/macros.h"
24#include "globals.h"
25#include "base/logging.h"
26#include "base/hex_dump.h"
Dave Allison69dfe512014-07-11 17:11:58 +000027#include "thread.h"
28#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080029
Dave Allison69dfe512014-07-11 17:11:58 +000030#if defined(__APPLE__)
31#define ucontext __darwin_ucontext
Dan Albert85fa7962014-08-10 10:14:59 -070032
33#if defined(__x86_64__)
34// 64 bit mac build.
35#define CTX_ESP uc_mcontext->__ss.__rsp
36#define CTX_EIP uc_mcontext->__ss.__rip
37#define CTX_EAX uc_mcontext->__ss.__rax
Dave Allison648d7112014-07-25 16:15:27 -070038#define CTX_METHOD uc_mcontext->__ss.__rdi
Dave Allison8ce6b902014-08-26 11:07:58 -070039#define CTX_JMP_BUF uc_mcontext->__ss.__rdi
Dan Albert85fa7962014-08-10 10:14:59 -070040#else
41// 32 bit mac build.
Dave Allison69dfe512014-07-11 17:11:58 +000042#define CTX_ESP uc_mcontext->__ss.__esp
43#define CTX_EIP uc_mcontext->__ss.__eip
44#define CTX_EAX uc_mcontext->__ss.__eax
Dave Allisondfd3b472014-07-16 16:04:32 -070045#define CTX_METHOD uc_mcontext->__ss.__eax
Dave Allison8ce6b902014-08-26 11:07:58 -070046#define CTX_JMP_BUF uc_mcontext->__ss.__eax
Dan Albert85fa7962014-08-10 10:14:59 -070047#endif
48
Dave Allisondfd3b472014-07-16 16:04:32 -070049#elif defined(__x86_64__)
Dan Albert85fa7962014-08-10 10:14:59 -070050// 64 bit linux build.
Dave Allisondfd3b472014-07-16 16:04:32 -070051#define CTX_ESP uc_mcontext.gregs[REG_RSP]
52#define CTX_EIP uc_mcontext.gregs[REG_RIP]
53#define CTX_EAX uc_mcontext.gregs[REG_RAX]
54#define CTX_METHOD uc_mcontext.gregs[REG_RDI]
Dave Allison8ce6b902014-08-26 11:07:58 -070055#define CTX_RDI uc_mcontext.gregs[REG_RDI]
56#define CTX_JMP_BUF uc_mcontext.gregs[REG_RDI]
Dave Allison69dfe512014-07-11 17:11:58 +000057#else
Dan Albert85fa7962014-08-10 10:14:59 -070058// 32 bit linux build.
Dave Allison69dfe512014-07-11 17:11:58 +000059#define CTX_ESP uc_mcontext.gregs[REG_ESP]
60#define CTX_EIP uc_mcontext.gregs[REG_EIP]
61#define CTX_EAX uc_mcontext.gregs[REG_EAX]
Dave Allisondfd3b472014-07-16 16:04:32 -070062#define CTX_METHOD uc_mcontext.gregs[REG_EAX]
Dave Allison8ce6b902014-08-26 11:07:58 -070063#define CTX_JMP_BUF uc_mcontext.gregs[REG_EAX]
Dave Allison69dfe512014-07-11 17:11:58 +000064#endif
Dave Allisonb373e092014-02-20 16:06:36 -080065
66//
Dave Allisondfd3b472014-07-16 16:04:32 -070067// X86 (and X86_64) specific fault handler functions.
Dave Allisonb373e092014-02-20 16:06:36 -080068//
69
70namespace art {
71
Dan Albert85fa7962014-08-10 10:14:59 -070072#if defined(__APPLE__) && defined(__x86_64__)
73// mac symbols have a prefix of _ on x86_64
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010074extern "C" void _art_quick_throw_null_pointer_exception_from_signal();
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -070075extern "C" void _art_quick_throw_stack_overflow();
Dan Albert85fa7962014-08-10 10:14:59 -070076extern "C" void _art_quick_test_suspend();
77#define EXT_SYM(sym) _ ## sym
78#else
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010079extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Dave Allison648d7112014-07-25 16:15:27 -070080extern "C" void art_quick_throw_stack_overflow();
Dave Allison69dfe512014-07-11 17:11:58 +000081extern "C" void art_quick_test_suspend();
Dan Albert85fa7962014-08-10 10:14:59 -070082#define EXT_SYM(sym) sym
83#endif
Dave Allison69dfe512014-07-11 17:11:58 +000084
Dave Allison8ce6b902014-08-26 11:07:58 -070085// Note this is different from the others (no underscore on 64 bit mac) due to
86// the way the symbol is defined in the .S file.
87// TODO: fix the symbols for 64 bit mac - there is a double underscore prefix for some
88// of them.
89extern "C" void art_nested_signal_return();
90
Dave Allison69dfe512014-07-11 17:11:58 +000091// Get the size of an instruction in bytes.
Dave Allisondfd3b472014-07-16 16:04:32 -070092// Return 0 if the instruction is not handled.
93static uint32_t GetInstructionSize(const uint8_t* pc) {
94#if defined(__x86_64)
95 const bool x86_64 = true;
96#else
97 const bool x86_64 = false;
Dave Allison69dfe512014-07-11 17:11:58 +000098#endif
99
Dave Allisondfd3b472014-07-16 16:04:32 -0700100 const uint8_t* startpc = pc;
Dave Allison69dfe512014-07-11 17:11:58 +0000101
Dave Allison69dfe512014-07-11 17:11:58 +0000102 uint8_t opcode = *pc++;
Dave Allisondfd3b472014-07-16 16:04:32 -0700103 uint8_t modrm;
104 bool has_modrm = false;
105 bool two_byte = false;
106 uint32_t displacement_size = 0;
107 uint32_t immediate_size = 0;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400108 bool operand_size_prefix = false;
Dave Allisondfd3b472014-07-16 16:04:32 -0700109
110 // Prefixes.
111 while (true) {
112 bool prefix_present = false;
113 switch (opcode) {
Mark Mendell5daf8e12014-09-25 15:13:39 -0400114 // Group 3
115 case 0x66:
116 operand_size_prefix = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700117 FALLTHROUGH_INTENDED;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400118
Dave Allisondfd3b472014-07-16 16:04:32 -0700119 // Group 1
120 case 0xf0:
121 case 0xf2:
122 case 0xf3:
123
124 // Group 2
125 case 0x2e:
126 case 0x36:
127 case 0x3e:
128 case 0x26:
129 case 0x64:
130 case 0x65:
131
Dave Allisondfd3b472014-07-16 16:04:32 -0700132 // Group 4
133 case 0x67:
134 opcode = *pc++;
135 prefix_present = true;
136 break;
137 }
138 if (!prefix_present) {
139 break;
140 }
141 }
142
143 if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) {
144 opcode = *pc++;
145 }
146
147 if (opcode == 0x0f) {
148 // Two byte opcode
Dave Allison69dfe512014-07-11 17:11:58 +0000149 two_byte = true;
150 opcode = *pc++;
151 }
152
Dave Allisondfd3b472014-07-16 16:04:32 -0700153 bool unhandled_instruction = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000154
Dave Allison69dfe512014-07-11 17:11:58 +0000155 if (two_byte) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700156 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700157 case 0x10: // vmovsd/ss
158 case 0x11: // vmovsd/ss
Dave Allisondfd3b472014-07-16 16:04:32 -0700159 case 0xb6: // movzx
160 case 0xb7:
161 case 0xbe: // movsx
162 case 0xbf:
163 modrm = *pc++;
164 has_modrm = true;
165 break;
166 default:
167 unhandled_instruction = true;
168 break;
169 }
170 } else {
171 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700172 case 0x88: // mov byte
173 case 0x89: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700174 case 0x8b:
175 case 0x38: // cmp with memory.
176 case 0x39:
177 case 0x3a:
178 case 0x3b:
179 case 0x3c:
180 case 0x3d:
181 case 0x85: // test.
182 modrm = *pc++;
183 has_modrm = true;
184 break;
Dave Allison69dfe512014-07-11 17:11:58 +0000185
Dave Allisondfd3b472014-07-16 16:04:32 -0700186 case 0x80: // group 1, byte immediate.
187 case 0x83:
Mark Mendella9f36ee2014-10-01 08:02:43 -0400188 case 0xc6:
Dave Allisondfd3b472014-07-16 16:04:32 -0700189 modrm = *pc++;
190 has_modrm = true;
191 immediate_size = 1;
Dave Allison69dfe512014-07-11 17:11:58 +0000192 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700193
194 case 0x81: // group 1, word immediate.
Mark Mendell40741f32015-04-20 22:10:34 -0400195 case 0xc7: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700196 modrm = *pc++;
197 has_modrm = true;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400198 immediate_size = operand_size_prefix ? 2 : 4;
Dave Allison69dfe512014-07-11 17:11:58 +0000199 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700200
201 default:
202 unhandled_instruction = true;
Dave Allison69dfe512014-07-11 17:11:58 +0000203 break;
204 }
205 }
206
Dave Allisondfd3b472014-07-16 16:04:32 -0700207 if (unhandled_instruction) {
208 VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode);
209 return 0;
210 }
211
212 if (has_modrm) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700213 uint8_t mod = (modrm >> 6) & 3U /* 0b11 */;
Dave Allisondfd3b472014-07-16 16:04:32 -0700214
215 // Check for SIB.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700216 if (mod != 3U /* 0b11 */ && (modrm & 7U /* 0b111 */) == 4) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700217 ++pc; // SIB
218 }
219
220 switch (mod) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700221 case 0U /* 0b00 */: break;
222 case 1U /* 0b01 */: displacement_size = 1; break;
223 case 2U /* 0b10 */: displacement_size = 4; break;
224 case 3U /* 0b11 */:
Dave Allisondfd3b472014-07-16 16:04:32 -0700225 break;
226 }
227 }
228
229 // Skip displacement and immediate.
230 pc += displacement_size + immediate_size;
231
232 VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc);
233 return pc - startpc;
Dave Allison69dfe512014-07-11 17:11:58 +0000234}
235
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700236void FaultManager::HandleNestedSignal(int, siginfo_t*, void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700237 // For the Intel architectures we need to go to an assembly language
238 // stub. This is because the 32 bit call to longjmp is much different
239 // from the 64 bit ABI call and pushing things onto the stack inside this
240 // handler was unwieldy and ugly. The use of the stub means we can keep
241 // this code the same for both 32 and 64 bit.
242
243 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -0700245
246 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
247 uc->CTX_JMP_BUF = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
248 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_nested_signal_return);
249}
250
Dave Allisondfd3b472014-07-16 16:04:32 -0700251void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700253 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +0000254 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
255 *out_sp = static_cast<uintptr_t>(uc->CTX_ESP);
256 VLOG(signals) << "sp: " << std::hex << *out_sp;
257 if (*out_sp == 0) {
258 return;
259 }
260
261 // In the case of a stack overflow, the stack is not valid and we can't
Dave Allisondfd3b472014-07-16 16:04:32 -0700262 // 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 +0000263 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr);
264 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Dave Allisondfd3b472014-07-16 16:04:32 -0700265#if defined(__x86_64__)
266 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64));
267#else
Dave Allison69dfe512014-07-11 17:11:58 +0000268 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86));
Dave Allisondfd3b472014-07-16 16:04:32 -0700269#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000270 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 *out_method = reinterpret_cast<ArtMethod*>(uc->CTX_METHOD);
Dave Allison69dfe512014-07-11 17:11:58 +0000272 } else {
273 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000275 }
276
277 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
278 VLOG(signals) << HexDump(pc, 32, true, "PC ");
279
Andreas Gampe94158862015-04-03 02:17:06 -0700280 if (pc == nullptr) {
281 // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below.
282 *out_method = nullptr;
283 return;
284 }
285
Dave Allison69dfe512014-07-11 17:11:58 +0000286 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700287 if (instr_size == 0) {
288 // Unknown instruction, tell caller it's not ours.
289 *out_method = nullptr;
290 return;
291 }
Dave Allison69dfe512014-07-11 17:11:58 +0000292 *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800293}
294
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100295bool NullPointerHandler::Action(int, siginfo_t* sig, void* context) {
296 if (!IsValidImplicitCheck(sig)) {
297 return false;
298 }
Dave Allison69dfe512014-07-11 17:11:58 +0000299 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
300 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
301 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
302
303 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700304 if (instr_size == 0) {
305 // Unknown instruction, can't really happen.
306 return false;
307 }
308
Dave Allison69dfe512014-07-11 17:11:58 +0000309 // We need to arrange for the signal handler to return to the null pointer
310 // exception generator. The return address must be the address of the
311 // next instruction (this instruction + instruction size). The return address
312 // is on the stack at the top address of the current frame.
313
314 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700315 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size);
316 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000317 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700318 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000319
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100320 uc->CTX_EIP = reinterpret_cast<uintptr_t>(
321 EXT_SYM(art_quick_throw_null_pointer_exception_from_signal));
322 // Pass the faulting address as the first argument of
323 // art_quick_throw_null_pointer_exception_from_signal.
324#if defined(__x86_64__)
325 uc->CTX_RDI = reinterpret_cast<uintptr_t>(sig->si_addr);
326#else
327 uc->CTX_EAX = reinterpret_cast<uintptr_t>(sig->si_addr);
328#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000329 VLOG(signals) << "Generating null pointer exception";
330 return true;
331}
332
333// A suspend check is done using the following instruction sequence:
Dave Allisondfd3b472014-07-16 16:04:32 -0700334// (x86)
Dave Allison69dfe512014-07-11 17:11:58 +0000335// 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger
336// .. some intervening instructions.
337// 0xf720f1e6: 8500 test eax, [eax]
Dave Allisondfd3b472014-07-16 16:04:32 -0700338// (x86_64)
339// 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger
340// .. some intervening instructions.
341// 0x7f579de45da7: 8500 test eax, [eax]
Dave Allison69dfe512014-07-11 17:11:58 +0000342
343// The offset from fs is Thread::ThreadSuspendTriggerOffset().
344// To check for a suspend check, we examine the instructions that caused
345// the fault.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700346bool SuspensionHandler::Action(int, siginfo_t*, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000347 // These are the instructions to check for. The first one is the mov eax, fs:[xxx]
348 // where xxx is the offset of the suspend trigger.
Dave Allisondfd3b472014-07-16 16:04:32 -0700349#if defined(__x86_64__)
350 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<8>().Int32Value();
351#else
Dave Allison69dfe512014-07-11 17:11:58 +0000352 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<4>().Int32Value();
Dave Allisondfd3b472014-07-16 16:04:32 -0700353#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000354
355 VLOG(signals) << "Checking for suspension point";
Dave Allisondfd3b472014-07-16 16:04:32 -0700356#if defined(__x86_64__)
357 uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff),
358 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
359#else
Dave Allison69dfe512014-07-11 17:11:58 +0000360 uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff),
361 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
Dave Allisondfd3b472014-07-16 16:04:32 -0700362#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000363 uint8_t checkinst2[] = {0x85, 0x00};
364
365 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
366 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
367 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
368
369 if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) {
370 // Second instruction is not correct (test eax,[eax]).
371 VLOG(signals) << "Not a suspension point";
372 return false;
373 }
374
375 // The first instruction can a little bit up the stream due to load hoisting
376 // in the compiler.
377 uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions.
378 uint8_t* ptr = pc - sizeof(checkinst1);
379 bool found = false;
380 while (ptr > limit) {
381 if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) {
382 found = true;
383 break;
384 }
385 ptr -= 1;
386 }
387
388 if (found) {
389 VLOG(signals) << "suspend check match";
390
391 // We need to arrange for the signal handler to return to the null pointer
392 // exception generator. The return address must be the address of the
393 // next instruction (this instruction + 2). The return address
394 // is on the stack at the top address of the current frame.
395
396 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700397 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2);
398 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000399 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700400 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000401
Dan Albert85fa7962014-08-10 10:14:59 -0700402 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_test_suspend));
Dave Allison69dfe512014-07-11 17:11:58 +0000403
404 // Now remove the suspend trigger that caused this fault.
405 Thread::Current()->RemoveSuspendTrigger();
406 VLOG(signals) << "removed suspend trigger invoking test suspend";
407 return true;
408 }
409 VLOG(signals) << "Not a suspend check match, first instruction mismatch";
Dave Allisonb373e092014-02-20 16:06:36 -0800410 return false;
411}
412
Dave Allison69dfe512014-07-11 17:11:58 +0000413// The stack overflow check is done using the following instruction:
414// test eax, [esp+ -xxx]
415// where 'xxx' is the size of the overflow area.
416//
417// This is done before any frame is established in the method. The return
418// address for the previous method is on the stack at ESP.
Dave Allisonb373e092014-02-20 16:06:36 -0800419
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700420bool StackOverflowHandler::Action(int, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000421 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
422 uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP);
423
424 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
425 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
426 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
427 ", fault_addr: " << fault_addr;
428
Dave Allisondfd3b472014-07-16 16:04:32 -0700429#if defined(__x86_64__)
430 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64);
431#else
Dave Allison69dfe512014-07-11 17:11:58 +0000432 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86);
Dave Allisondfd3b472014-07-16 16:04:32 -0700433#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000434
Dave Allison69dfe512014-07-11 17:11:58 +0000435 // Check that the fault address is the value expected for a stack overflow.
436 if (fault_addr != overflow_addr) {
437 VLOG(signals) << "Not a stack overflow";
438 return false;
439 }
440
Dave Allison648d7112014-07-25 16:15:27 -0700441 VLOG(signals) << "Stack overflow found";
Dave Allison69dfe512014-07-11 17:11:58 +0000442
443 // Since the compiler puts the implicit overflow
444 // check before the callee save instructions, the SP is already pointing to
445 // the previous frame.
446
Dave Allison648d7112014-07-25 16:15:27 -0700447 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -0700448 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_stack_overflow));
Dave Allison69dfe512014-07-11 17:11:58 +0000449
450 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800451}
452} // namespace art