blob: 24e3a0d53f375556f4a96014fafac5c3ef18680e [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
Nicolas Geoffray5b643062016-06-29 14:34:21 +010039#define CTX_RDI uc_mcontext->__ss.__rdi
Dave Allison8ce6b902014-08-26 11:07:58 -070040#define CTX_JMP_BUF uc_mcontext->__ss.__rdi
Dan Albert85fa7962014-08-10 10:14:59 -070041#else
42// 32 bit mac build.
Dave Allison69dfe512014-07-11 17:11:58 +000043#define CTX_ESP uc_mcontext->__ss.__esp
44#define CTX_EIP uc_mcontext->__ss.__eip
45#define CTX_EAX uc_mcontext->__ss.__eax
Dave Allisondfd3b472014-07-16 16:04:32 -070046#define CTX_METHOD uc_mcontext->__ss.__eax
Dave Allison8ce6b902014-08-26 11:07:58 -070047#define CTX_JMP_BUF uc_mcontext->__ss.__eax
Dan Albert85fa7962014-08-10 10:14:59 -070048#endif
49
Dave Allisondfd3b472014-07-16 16:04:32 -070050#elif defined(__x86_64__)
Dan Albert85fa7962014-08-10 10:14:59 -070051// 64 bit linux build.
Dave Allisondfd3b472014-07-16 16:04:32 -070052#define CTX_ESP uc_mcontext.gregs[REG_RSP]
53#define CTX_EIP uc_mcontext.gregs[REG_RIP]
54#define CTX_EAX uc_mcontext.gregs[REG_RAX]
55#define CTX_METHOD uc_mcontext.gregs[REG_RDI]
Dave Allison8ce6b902014-08-26 11:07:58 -070056#define CTX_RDI uc_mcontext.gregs[REG_RDI]
57#define CTX_JMP_BUF uc_mcontext.gregs[REG_RDI]
Dave Allison69dfe512014-07-11 17:11:58 +000058#else
Dan Albert85fa7962014-08-10 10:14:59 -070059// 32 bit linux build.
Dave Allison69dfe512014-07-11 17:11:58 +000060#define CTX_ESP uc_mcontext.gregs[REG_ESP]
61#define CTX_EIP uc_mcontext.gregs[REG_EIP]
62#define CTX_EAX uc_mcontext.gregs[REG_EAX]
Dave Allisondfd3b472014-07-16 16:04:32 -070063#define CTX_METHOD uc_mcontext.gregs[REG_EAX]
Dave Allison8ce6b902014-08-26 11:07:58 -070064#define CTX_JMP_BUF uc_mcontext.gregs[REG_EAX]
Dave Allison69dfe512014-07-11 17:11:58 +000065#endif
Dave Allisonb373e092014-02-20 16:06:36 -080066
67//
Dave Allisondfd3b472014-07-16 16:04:32 -070068// X86 (and X86_64) specific fault handler functions.
Dave Allisonb373e092014-02-20 16:06:36 -080069//
70
71namespace art {
72
Dan Albert85fa7962014-08-10 10:14:59 -070073#if defined(__APPLE__) && defined(__x86_64__)
74// mac symbols have a prefix of _ on x86_64
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010075extern "C" void _art_quick_throw_null_pointer_exception_from_signal();
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -070076extern "C" void _art_quick_throw_stack_overflow();
Dan Albert85fa7962014-08-10 10:14:59 -070077extern "C" void _art_quick_test_suspend();
78#define EXT_SYM(sym) _ ## sym
79#else
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010080extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Dave Allison648d7112014-07-25 16:15:27 -070081extern "C" void art_quick_throw_stack_overflow();
Dave Allison69dfe512014-07-11 17:11:58 +000082extern "C" void art_quick_test_suspend();
Dan Albert85fa7962014-08-10 10:14:59 -070083#define EXT_SYM(sym) sym
84#endif
Dave Allison69dfe512014-07-11 17:11:58 +000085
Dave Allison8ce6b902014-08-26 11:07:58 -070086// Note this is different from the others (no underscore on 64 bit mac) due to
87// the way the symbol is defined in the .S file.
88// TODO: fix the symbols for 64 bit mac - there is a double underscore prefix for some
89// of them.
90extern "C" void art_nested_signal_return();
91
Dave Allison69dfe512014-07-11 17:11:58 +000092// Get the size of an instruction in bytes.
Dave Allisondfd3b472014-07-16 16:04:32 -070093// Return 0 if the instruction is not handled.
94static uint32_t GetInstructionSize(const uint8_t* pc) {
95#if defined(__x86_64)
96 const bool x86_64 = true;
97#else
98 const bool x86_64 = false;
Dave Allison69dfe512014-07-11 17:11:58 +000099#endif
100
Dave Allisondfd3b472014-07-16 16:04:32 -0700101 const uint8_t* startpc = pc;
Dave Allison69dfe512014-07-11 17:11:58 +0000102
Dave Allison69dfe512014-07-11 17:11:58 +0000103 uint8_t opcode = *pc++;
Dave Allisondfd3b472014-07-16 16:04:32 -0700104 uint8_t modrm;
105 bool has_modrm = false;
106 bool two_byte = false;
107 uint32_t displacement_size = 0;
108 uint32_t immediate_size = 0;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400109 bool operand_size_prefix = false;
Dave Allisondfd3b472014-07-16 16:04:32 -0700110
111 // Prefixes.
112 while (true) {
113 bool prefix_present = false;
114 switch (opcode) {
Mark Mendell5daf8e12014-09-25 15:13:39 -0400115 // Group 3
116 case 0x66:
117 operand_size_prefix = true;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700118 FALLTHROUGH_INTENDED;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400119
Dave Allisondfd3b472014-07-16 16:04:32 -0700120 // Group 1
121 case 0xf0:
122 case 0xf2:
123 case 0xf3:
124
125 // Group 2
126 case 0x2e:
127 case 0x36:
128 case 0x3e:
129 case 0x26:
130 case 0x64:
131 case 0x65:
132
Dave Allisondfd3b472014-07-16 16:04:32 -0700133 // Group 4
134 case 0x67:
135 opcode = *pc++;
136 prefix_present = true;
137 break;
138 }
139 if (!prefix_present) {
140 break;
141 }
142 }
143
144 if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) {
145 opcode = *pc++;
146 }
147
148 if (opcode == 0x0f) {
149 // Two byte opcode
Dave Allison69dfe512014-07-11 17:11:58 +0000150 two_byte = true;
151 opcode = *pc++;
152 }
153
Dave Allisondfd3b472014-07-16 16:04:32 -0700154 bool unhandled_instruction = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000155
Dave Allison69dfe512014-07-11 17:11:58 +0000156 if (two_byte) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700157 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700158 case 0x10: // vmovsd/ss
159 case 0x11: // vmovsd/ss
Dave Allisondfd3b472014-07-16 16:04:32 -0700160 case 0xb6: // movzx
161 case 0xb7:
162 case 0xbe: // movsx
163 case 0xbf:
164 modrm = *pc++;
165 has_modrm = true;
166 break;
167 default:
168 unhandled_instruction = true;
169 break;
170 }
171 } else {
172 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700173 case 0x88: // mov byte
174 case 0x89: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700175 case 0x8b:
176 case 0x38: // cmp with memory.
177 case 0x39:
178 case 0x3a:
179 case 0x3b:
180 case 0x3c:
181 case 0x3d:
182 case 0x85: // test.
183 modrm = *pc++;
184 has_modrm = true;
185 break;
Dave Allison69dfe512014-07-11 17:11:58 +0000186
Dave Allisondfd3b472014-07-16 16:04:32 -0700187 case 0x80: // group 1, byte immediate.
188 case 0x83:
Mark Mendella9f36ee2014-10-01 08:02:43 -0400189 case 0xc6:
Dave Allisondfd3b472014-07-16 16:04:32 -0700190 modrm = *pc++;
191 has_modrm = true;
192 immediate_size = 1;
Dave Allison69dfe512014-07-11 17:11:58 +0000193 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700194
195 case 0x81: // group 1, word immediate.
Mark Mendell40741f32015-04-20 22:10:34 -0400196 case 0xc7: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700197 modrm = *pc++;
198 has_modrm = true;
Mark Mendell5daf8e12014-09-25 15:13:39 -0400199 immediate_size = operand_size_prefix ? 2 : 4;
Dave Allison69dfe512014-07-11 17:11:58 +0000200 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700201
202 default:
203 unhandled_instruction = true;
Dave Allison69dfe512014-07-11 17:11:58 +0000204 break;
205 }
206 }
207
Dave Allisondfd3b472014-07-16 16:04:32 -0700208 if (unhandled_instruction) {
209 VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode);
210 return 0;
211 }
212
213 if (has_modrm) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700214 uint8_t mod = (modrm >> 6) & 3U /* 0b11 */;
Dave Allisondfd3b472014-07-16 16:04:32 -0700215
216 // Check for SIB.
Andreas Gampec8ccf682014-09-29 20:07:43 -0700217 if (mod != 3U /* 0b11 */ && (modrm & 7U /* 0b111 */) == 4) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700218 ++pc; // SIB
219 }
220
221 switch (mod) {
Andreas Gampec8ccf682014-09-29 20:07:43 -0700222 case 0U /* 0b00 */: break;
223 case 1U /* 0b01 */: displacement_size = 1; break;
224 case 2U /* 0b10 */: displacement_size = 4; break;
225 case 3U /* 0b11 */:
Dave Allisondfd3b472014-07-16 16:04:32 -0700226 break;
227 }
228 }
229
230 // Skip displacement and immediate.
231 pc += displacement_size + immediate_size;
232
233 VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc);
234 return pc - startpc;
Dave Allison69dfe512014-07-11 17:11:58 +0000235}
236
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700237void FaultManager::HandleNestedSignal(int, siginfo_t*, void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700238 // For the Intel architectures we need to go to an assembly language
239 // stub. This is because the 32 bit call to longjmp is much different
240 // from the 64 bit ABI call and pushing things onto the stack inside this
241 // handler was unwieldy and ugly. The use of the stub means we can keep
242 // this code the same for both 32 and 64 bit.
243
244 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700245 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -0700246
247 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
248 uc->CTX_JMP_BUF = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
249 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_nested_signal_return);
250}
251
Dave Allisondfd3b472014-07-16 16:04:32 -0700252void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700254 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +0000255 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
256 *out_sp = static_cast<uintptr_t>(uc->CTX_ESP);
257 VLOG(signals) << "sp: " << std::hex << *out_sp;
258 if (*out_sp == 0) {
259 return;
260 }
261
262 // In the case of a stack overflow, the stack is not valid and we can't
Dave Allisondfd3b472014-07-16 16:04:32 -0700263 // 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 +0000264 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr);
265 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Dave Allisondfd3b472014-07-16 16:04:32 -0700266#if defined(__x86_64__)
267 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64));
268#else
Dave Allison69dfe512014-07-11 17:11:58 +0000269 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86));
Dave Allisondfd3b472014-07-16 16:04:32 -0700270#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000271 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 *out_method = reinterpret_cast<ArtMethod*>(uc->CTX_METHOD);
Dave Allison69dfe512014-07-11 17:11:58 +0000273 } else {
274 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000276 }
277
278 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
279 VLOG(signals) << HexDump(pc, 32, true, "PC ");
280
Andreas Gampe94158862015-04-03 02:17:06 -0700281 if (pc == nullptr) {
282 // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below.
283 *out_method = nullptr;
284 return;
285 }
286
Dave Allison69dfe512014-07-11 17:11:58 +0000287 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700288 if (instr_size == 0) {
289 // Unknown instruction, tell caller it's not ours.
290 *out_method = nullptr;
291 return;
292 }
Dave Allison69dfe512014-07-11 17:11:58 +0000293 *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800294}
295
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100296bool NullPointerHandler::Action(int, siginfo_t* sig, void* context) {
297 if (!IsValidImplicitCheck(sig)) {
298 return false;
299 }
Dave Allison69dfe512014-07-11 17:11:58 +0000300 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
301 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
302 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
303
304 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700305 if (instr_size == 0) {
306 // Unknown instruction, can't really happen.
307 return false;
308 }
309
Dave Allison69dfe512014-07-11 17:11:58 +0000310 // We need to arrange for the signal handler to return to the null pointer
311 // exception generator. The return address must be the address of the
312 // next instruction (this instruction + instruction size). The return address
313 // is on the stack at the top address of the current frame.
314
315 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700316 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size);
317 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000318 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700319 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000320
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100321 uc->CTX_EIP = reinterpret_cast<uintptr_t>(
322 EXT_SYM(art_quick_throw_null_pointer_exception_from_signal));
323 // Pass the faulting address as the first argument of
324 // art_quick_throw_null_pointer_exception_from_signal.
325#if defined(__x86_64__)
326 uc->CTX_RDI = reinterpret_cast<uintptr_t>(sig->si_addr);
327#else
328 uc->CTX_EAX = reinterpret_cast<uintptr_t>(sig->si_addr);
329#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000330 VLOG(signals) << "Generating null pointer exception";
331 return true;
332}
333
334// A suspend check is done using the following instruction sequence:
Dave Allisondfd3b472014-07-16 16:04:32 -0700335// (x86)
Dave Allison69dfe512014-07-11 17:11:58 +0000336// 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger
337// .. some intervening instructions.
338// 0xf720f1e6: 8500 test eax, [eax]
Dave Allisondfd3b472014-07-16 16:04:32 -0700339// (x86_64)
340// 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger
341// .. some intervening instructions.
342// 0x7f579de45da7: 8500 test eax, [eax]
Dave Allison69dfe512014-07-11 17:11:58 +0000343
344// The offset from fs is Thread::ThreadSuspendTriggerOffset().
345// To check for a suspend check, we examine the instructions that caused
346// the fault.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700347bool SuspensionHandler::Action(int, siginfo_t*, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000348 // These are the instructions to check for. The first one is the mov eax, fs:[xxx]
349 // where xxx is the offset of the suspend trigger.
Dave Allisondfd3b472014-07-16 16:04:32 -0700350#if defined(__x86_64__)
351 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<8>().Int32Value();
352#else
Dave Allison69dfe512014-07-11 17:11:58 +0000353 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<4>().Int32Value();
Dave Allisondfd3b472014-07-16 16:04:32 -0700354#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000355
356 VLOG(signals) << "Checking for suspension point";
Dave Allisondfd3b472014-07-16 16:04:32 -0700357#if defined(__x86_64__)
358 uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff),
359 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
360#else
Dave Allison69dfe512014-07-11 17:11:58 +0000361 uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff),
362 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
Dave Allisondfd3b472014-07-16 16:04:32 -0700363#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000364 uint8_t checkinst2[] = {0x85, 0x00};
365
366 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
367 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
368 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
369
370 if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) {
371 // Second instruction is not correct (test eax,[eax]).
372 VLOG(signals) << "Not a suspension point";
373 return false;
374 }
375
376 // The first instruction can a little bit up the stream due to load hoisting
377 // in the compiler.
378 uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions.
379 uint8_t* ptr = pc - sizeof(checkinst1);
380 bool found = false;
381 while (ptr > limit) {
382 if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) {
383 found = true;
384 break;
385 }
386 ptr -= 1;
387 }
388
389 if (found) {
390 VLOG(signals) << "suspend check match";
391
392 // We need to arrange for the signal handler to return to the null pointer
393 // exception generator. The return address must be the address of the
394 // next instruction (this instruction + 2). The return address
395 // is on the stack at the top address of the current frame.
396
397 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700398 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2);
399 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000400 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700401 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000402
Dan Albert85fa7962014-08-10 10:14:59 -0700403 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_test_suspend));
Dave Allison69dfe512014-07-11 17:11:58 +0000404
405 // Now remove the suspend trigger that caused this fault.
406 Thread::Current()->RemoveSuspendTrigger();
407 VLOG(signals) << "removed suspend trigger invoking test suspend";
408 return true;
409 }
410 VLOG(signals) << "Not a suspend check match, first instruction mismatch";
Dave Allisonb373e092014-02-20 16:06:36 -0800411 return false;
412}
413
Dave Allison69dfe512014-07-11 17:11:58 +0000414// The stack overflow check is done using the following instruction:
415// test eax, [esp+ -xxx]
416// where 'xxx' is the size of the overflow area.
417//
418// This is done before any frame is established in the method. The return
419// address for the previous method is on the stack at ESP.
Dave Allisonb373e092014-02-20 16:06:36 -0800420
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700421bool StackOverflowHandler::Action(int, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000422 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
423 uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP);
424
425 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
426 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
427 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
428 ", fault_addr: " << fault_addr;
429
Dave Allisondfd3b472014-07-16 16:04:32 -0700430#if defined(__x86_64__)
431 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64);
432#else
Dave Allison69dfe512014-07-11 17:11:58 +0000433 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86);
Dave Allisondfd3b472014-07-16 16:04:32 -0700434#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000435
Dave Allison69dfe512014-07-11 17:11:58 +0000436 // Check that the fault address is the value expected for a stack overflow.
437 if (fault_addr != overflow_addr) {
438 VLOG(signals) << "Not a stack overflow";
439 return false;
440 }
441
Dave Allison648d7112014-07-25 16:15:27 -0700442 VLOG(signals) << "Stack overflow found";
Dave Allison69dfe512014-07-11 17:11:58 +0000443
444 // Since the compiler puts the implicit overflow
445 // check before the callee save instructions, the SP is already pointing to
446 // the previous frame.
447
Dave Allison648d7112014-07-25 16:15:27 -0700448 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -0700449 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_stack_overflow));
Dave Allison69dfe512014-07-11 17:11:58 +0000450
451 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800452}
453} // namespace art