blob: ee005e8f6659976f4ad688cea8645f5afba7eb85 [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;
107
108 // Prefixes.
109 while (true) {
110 bool prefix_present = false;
111 switch (opcode) {
112 // 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
125 // Group 3
126 case 0x66:
127
128 // Group 4
129 case 0x67:
130 opcode = *pc++;
131 prefix_present = true;
132 break;
133 }
134 if (!prefix_present) {
135 break;
136 }
137 }
138
139 if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) {
140 opcode = *pc++;
141 }
142
143 if (opcode == 0x0f) {
144 // Two byte opcode
Dave Allison69dfe512014-07-11 17:11:58 +0000145 two_byte = true;
146 opcode = *pc++;
147 }
148
Dave Allisondfd3b472014-07-16 16:04:32 -0700149 bool unhandled_instruction = false;
Dave Allison69dfe512014-07-11 17:11:58 +0000150
Dave Allison69dfe512014-07-11 17:11:58 +0000151 if (two_byte) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700152 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700153 case 0x10: // vmovsd/ss
154 case 0x11: // vmovsd/ss
Dave Allisondfd3b472014-07-16 16:04:32 -0700155 case 0xb6: // movzx
156 case 0xb7:
157 case 0xbe: // movsx
158 case 0xbf:
159 modrm = *pc++;
160 has_modrm = true;
161 break;
162 default:
163 unhandled_instruction = true;
164 break;
165 }
166 } else {
167 switch (opcode) {
Serguei Katkove2d596e2014-09-08 17:48:25 +0700168 case 0x88: // mov byte
169 case 0x89: // mov
Dave Allisondfd3b472014-07-16 16:04:32 -0700170 case 0x8b:
171 case 0x38: // cmp with memory.
172 case 0x39:
173 case 0x3a:
174 case 0x3b:
175 case 0x3c:
176 case 0x3d:
177 case 0x85: // test.
178 modrm = *pc++;
179 has_modrm = true;
180 break;
Dave Allison69dfe512014-07-11 17:11:58 +0000181
Dave Allisondfd3b472014-07-16 16:04:32 -0700182 case 0x80: // group 1, byte immediate.
183 case 0x83:
184 modrm = *pc++;
185 has_modrm = true;
186 immediate_size = 1;
Dave Allison69dfe512014-07-11 17:11:58 +0000187 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700188
189 case 0x81: // group 1, word immediate.
190 modrm = *pc++;
191 has_modrm = true;
192 immediate_size = 4;
Dave Allison69dfe512014-07-11 17:11:58 +0000193 break;
Dave Allisondfd3b472014-07-16 16:04:32 -0700194
195 default:
196 unhandled_instruction = true;
Dave Allison69dfe512014-07-11 17:11:58 +0000197 break;
198 }
199 }
200
Dave Allisondfd3b472014-07-16 16:04:32 -0700201 if (unhandled_instruction) {
202 VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode);
203 return 0;
204 }
205
206 if (has_modrm) {
207 uint8_t mod = (modrm >> 6) & 0b11;
208
209 // Check for SIB.
210 if (mod != 0b11 && (modrm & 0b111) == 4) {
211 ++pc; // SIB
212 }
213
214 switch (mod) {
215 case 0b00: break;
216 case 0b01: displacement_size = 1; break;
217 case 0b10: displacement_size = 4; break;
218 case 0b11:
219 break;
220 }
221 }
222
223 // Skip displacement and immediate.
224 pc += displacement_size + immediate_size;
225
226 VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc);
227 return pc - startpc;
Dave Allison69dfe512014-07-11 17:11:58 +0000228}
229
Dave Allison8ce6b902014-08-26 11:07:58 -0700230void FaultManager::HandleNestedSignal(int sig, siginfo_t* info, void* context) {
231 // For the Intel architectures we need to go to an assembly language
232 // stub. This is because the 32 bit call to longjmp is much different
233 // from the 64 bit ABI call and pushing things onto the stack inside this
234 // handler was unwieldy and ugly. The use of the stub means we can keep
235 // this code the same for both 32 and 64 bit.
236
237 Thread* self = Thread::Current();
238 CHECK(self != nullptr); // This will cause a SIGABRT if self is nullptr.
239
240 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
241 uc->CTX_JMP_BUF = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
242 uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_nested_signal_return);
243}
244
Dave Allisondfd3b472014-07-16 16:04:32 -0700245void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context,
Dave Allison69dfe512014-07-11 17:11:58 +0000246 mirror::ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700247 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +0000248 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
249 *out_sp = static_cast<uintptr_t>(uc->CTX_ESP);
250 VLOG(signals) << "sp: " << std::hex << *out_sp;
251 if (*out_sp == 0) {
252 return;
253 }
254
255 // In the case of a stack overflow, the stack is not valid and we can't
Dave Allisondfd3b472014-07-16 16:04:32 -0700256 // 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 +0000257 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr);
258 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Dave Allisondfd3b472014-07-16 16:04:32 -0700259#if defined(__x86_64__)
260 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64));
261#else
Dave Allison69dfe512014-07-11 17:11:58 +0000262 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86));
Dave Allisondfd3b472014-07-16 16:04:32 -0700263#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000264 if (overflow_addr == fault_addr) {
Dave Allisondfd3b472014-07-16 16:04:32 -0700265 *out_method = reinterpret_cast<mirror::ArtMethod*>(uc->CTX_METHOD);
Dave Allison69dfe512014-07-11 17:11:58 +0000266 } else {
267 // The method is at the top of the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700268 *out_method = (reinterpret_cast<StackReference<mirror::ArtMethod>* >(*out_sp)[0]).AsMirrorPtr();
Dave Allison69dfe512014-07-11 17:11:58 +0000269 }
270
271 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
272 VLOG(signals) << HexDump(pc, 32, true, "PC ");
273
274 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700275 if (instr_size == 0) {
276 // Unknown instruction, tell caller it's not ours.
277 *out_method = nullptr;
278 return;
279 }
Dave Allison69dfe512014-07-11 17:11:58 +0000280 *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800281}
282
283bool NullPointerHandler::Action(int sig, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000284 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
285 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
286 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
287
288 uint32_t instr_size = GetInstructionSize(pc);
Dave Allisondfd3b472014-07-16 16:04:32 -0700289 if (instr_size == 0) {
290 // Unknown instruction, can't really happen.
291 return false;
292 }
293
Dave Allison69dfe512014-07-11 17:11:58 +0000294 // We need to arrange for the signal handler to return to the null pointer
295 // exception generator. The return address must be the address of the
296 // next instruction (this instruction + instruction size). The return address
297 // is on the stack at the top address of the current frame.
298
299 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700300 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size);
301 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000302 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700303 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000304
Dan Albert85fa7962014-08-10 10:14:59 -0700305 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_null_pointer_exception));
Dave Allison69dfe512014-07-11 17:11:58 +0000306 VLOG(signals) << "Generating null pointer exception";
307 return true;
308}
309
310// A suspend check is done using the following instruction sequence:
Dave Allisondfd3b472014-07-16 16:04:32 -0700311// (x86)
Dave Allison69dfe512014-07-11 17:11:58 +0000312// 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger
313// .. some intervening instructions.
314// 0xf720f1e6: 8500 test eax, [eax]
Dave Allisondfd3b472014-07-16 16:04:32 -0700315// (x86_64)
316// 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger
317// .. some intervening instructions.
318// 0x7f579de45da7: 8500 test eax, [eax]
Dave Allison69dfe512014-07-11 17:11:58 +0000319
320// The offset from fs is Thread::ThreadSuspendTriggerOffset().
321// To check for a suspend check, we examine the instructions that caused
322// the fault.
323bool SuspensionHandler::Action(int sig, siginfo_t* info, void* context) {
324 // These are the instructions to check for. The first one is the mov eax, fs:[xxx]
325 // where xxx is the offset of the suspend trigger.
Dave Allisondfd3b472014-07-16 16:04:32 -0700326#if defined(__x86_64__)
327 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<8>().Int32Value();
328#else
Dave Allison69dfe512014-07-11 17:11:58 +0000329 uint32_t trigger = Thread::ThreadSuspendTriggerOffset<4>().Int32Value();
Dave Allisondfd3b472014-07-16 16:04:32 -0700330#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000331
332 VLOG(signals) << "Checking for suspension point";
Dave Allisondfd3b472014-07-16 16:04:32 -0700333#if defined(__x86_64__)
334 uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff),
335 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
336#else
Dave Allison69dfe512014-07-11 17:11:58 +0000337 uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff),
338 static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0};
Dave Allisondfd3b472014-07-16 16:04:32 -0700339#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000340 uint8_t checkinst2[] = {0x85, 0x00};
341
342 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
343 uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP);
344 uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP);
345
346 if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) {
347 // Second instruction is not correct (test eax,[eax]).
348 VLOG(signals) << "Not a suspension point";
349 return false;
350 }
351
352 // The first instruction can a little bit up the stream due to load hoisting
353 // in the compiler.
354 uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions.
355 uint8_t* ptr = pc - sizeof(checkinst1);
356 bool found = false;
357 while (ptr > limit) {
358 if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) {
359 found = true;
360 break;
361 }
362 ptr -= 1;
363 }
364
365 if (found) {
366 VLOG(signals) << "suspend check match";
367
368 // We need to arrange for the signal handler to return to the null pointer
369 // exception generator. The return address must be the address of the
370 // next instruction (this instruction + 2). The return address
371 // is on the stack at the top address of the current frame.
372
373 // Push the return address onto the stack.
Dave Allisondfd3b472014-07-16 16:04:32 -0700374 uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2);
375 uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t));
Dave Allison69dfe512014-07-11 17:11:58 +0000376 *next_sp = retaddr;
Dave Allisondfd3b472014-07-16 16:04:32 -0700377 uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp);
Dave Allison69dfe512014-07-11 17:11:58 +0000378
Dan Albert85fa7962014-08-10 10:14:59 -0700379 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_test_suspend));
Dave Allison69dfe512014-07-11 17:11:58 +0000380
381 // Now remove the suspend trigger that caused this fault.
382 Thread::Current()->RemoveSuspendTrigger();
383 VLOG(signals) << "removed suspend trigger invoking test suspend";
384 return true;
385 }
386 VLOG(signals) << "Not a suspend check match, first instruction mismatch";
Dave Allisonb373e092014-02-20 16:06:36 -0800387 return false;
388}
389
Dave Allison69dfe512014-07-11 17:11:58 +0000390// The stack overflow check is done using the following instruction:
391// test eax, [esp+ -xxx]
392// where 'xxx' is the size of the overflow area.
393//
394// This is done before any frame is established in the method. The return
395// address for the previous method is on the stack at ESP.
Dave Allisonb373e092014-02-20 16:06:36 -0800396
397bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000398 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
399 uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP);
400
401 uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
402 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
403 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
404 ", fault_addr: " << fault_addr;
405
Dave Allisondfd3b472014-07-16 16:04:32 -0700406#if defined(__x86_64__)
407 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64);
408#else
Dave Allison69dfe512014-07-11 17:11:58 +0000409 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86);
Dave Allisondfd3b472014-07-16 16:04:32 -0700410#endif
Dave Allison69dfe512014-07-11 17:11:58 +0000411
Dave Allison69dfe512014-07-11 17:11:58 +0000412 // Check that the fault address is the value expected for a stack overflow.
413 if (fault_addr != overflow_addr) {
414 VLOG(signals) << "Not a stack overflow";
415 return false;
416 }
417
Dave Allison648d7112014-07-25 16:15:27 -0700418 VLOG(signals) << "Stack overflow found";
Dave Allison69dfe512014-07-11 17:11:58 +0000419
420 // Since the compiler puts the implicit overflow
421 // check before the callee save instructions, the SP is already pointing to
422 // the previous frame.
423
Dave Allison648d7112014-07-25 16:15:27 -0700424 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Andreas Gampeeb0ab9e2014-08-14 11:15:22 -0700425 uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_stack_overflow));
Dave Allison69dfe512014-07-11 17:11:58 +0000426
427 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800428}
429} // namespace art