blob: d105c67d43e6927044200113f5d4ce0326c60875 [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"
Dave Allison8ce6b902014-08-26 11:07:58 -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"
Dave Allisonf9439142014-03-27 15:10:22 -070024#include "base/hex_dump.h"
Dave Allisonb373e092014-02-20 16:06:36 -080025#include "globals.h"
26#include "base/logging.h"
27#include "base/hex_dump.h"
28#include "thread.h"
29#include "thread-inl.h"
30
31//
32// ARM specific fault handler functions.
33//
34
35namespace art {
36
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010037extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Dave Allison648d7112014-07-25 16:15:27 -070038extern "C" void art_quick_throw_stack_overflow();
Dave Allison83252962014-04-03 16:33:48 -070039extern "C" void art_quick_implicit_suspend();
Dave Allisonb373e092014-02-20 16:06:36 -080040
Dave Allisonf9439142014-03-27 15:10:22 -070041// Get the size of a thumb2 instruction in bytes.
42static uint32_t GetInstructionSize(uint8_t* pc) {
43 uint16_t instr = pc[0] | pc[1] << 8;
44 bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800);
45 uint32_t instr_size = is_32bit ? 4 : 2;
46 return instr_size;
47}
48
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080049void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
50 void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -070051 // Note that in this handler we set up the registers and return to
52 // longjmp directly rather than going through an assembly language stub. The
53 // reason for this is that longjmp is (currently) in ARM mode and that would
54 // require switching modes in the stub - incurring an unwanted relocation.
55
56 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
57 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
58 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -070060
61 sc->arm_r0 = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
62 sc->arm_r1 = 1;
63 sc->arm_pc = reinterpret_cast<uintptr_t>(longjmp);
64 VLOG(signals) << "longjmp address: " << reinterpret_cast<void*>(sc->arm_pc);
65}
66
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080067void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -070068 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070069 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Dave Allison69dfe512014-07-11 17:11:58 +000070 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonb373e092014-02-20 16:06:36 -080071 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070072 *out_sp = static_cast<uintptr_t>(sc->arm_sp);
Narayan Kamathf561aab2015-06-19 15:23:46 +010073 VLOG(signals) << "sp: " << std::hex << *out_sp;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070074 if (*out_sp == 0) {
Dave Allisonb373e092014-02-20 16:06:36 -080075 return;
76 }
77
Dave Allisonf9439142014-03-27 15:10:22 -070078 // In the case of a stack overflow, the stack is not valid and we can't
79 // get the method from the top of the stack. However it's in r0.
80 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
81 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
Andreas Gampe7ea6f792014-07-14 16:21:44 -070082 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kArm));
Dave Allisonf9439142014-03-27 15:10:22 -070083 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 *out_method = reinterpret_cast<ArtMethod*>(sc->arm_r0);
Dave Allisonf9439142014-03-27 15:10:22 -070085 } else {
86 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -070087 *out_method = reinterpret_cast<ArtMethod*>(reinterpret_cast<uintptr_t*>(*out_sp)[0]);
Dave Allisonf9439142014-03-27 15:10:22 -070088 }
89
Dave Allisonb373e092014-02-20 16:06:36 -080090 // Work out the return PC. This will be the address of the instruction
91 // following the faulting ldr/str instruction. This is in thumb mode so
92 // the instruction might be a 16 or 32 bit one. Also, the GC map always
93 // has the bottom bit of the PC set so we also need to set that.
94
95 // Need to work out the size of the instruction that caused the exception.
96 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
Dave Allison5cd33752014-04-15 15:57:58 -070097 VLOG(signals) << "pc: " << std::hex << static_cast<void*>(ptr);
Andreas Gampe94158862015-04-03 02:17:06 -070098
99 if (ptr == nullptr) {
100 // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below.
101 *out_method = nullptr;
102 return;
103 }
104
Dave Allisonf9439142014-03-27 15:10:22 -0700105 uint32_t instr_size = GetInstructionSize(ptr);
Dave Allisonb373e092014-02-20 16:06:36 -0800106
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700107 *out_return_pc = (sc->arm_pc + instr_size) | 1;
Dave Allisonb373e092014-02-20 16:06:36 -0800108}
109
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100110bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
111 if (!IsValidImplicitCheck(info)) {
112 return false;
113 }
Dave Allisonb373e092014-02-20 16:06:36 -0800114 // The code that looks for the catch location needs to know the value of the
115 // ARM PC at the point of call. For Null checks we insert a GC map that is immediately after
116 // the load/store instruction that might cause the fault. However the mapping table has
117 // the low bits set for thumb mode so we need to set the bottom bit for the LR
118 // register in order to find the mapping.
119
120 // Need to work out the size of the instruction that caused the exception.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700121 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonb373e092014-02-20 16:06:36 -0800122 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
123 uint8_t* ptr = reinterpret_cast<uint8_t*>(sc->arm_pc);
124
Dave Allisonf9439142014-03-27 15:10:22 -0700125 uint32_t instr_size = GetInstructionSize(ptr);
Dave Allisonb373e092014-02-20 16:06:36 -0800126 sc->arm_lr = (sc->arm_pc + instr_size) | 1; // LR needs to point to gc map location
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100127 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
128 // Pass the faulting address as the first argument of
129 // art_quick_throw_null_pointer_exception_from_signal.
130 sc->arm_r0 = reinterpret_cast<uintptr_t>(info->si_addr);
Dave Allison5cd33752014-04-15 15:57:58 -0700131 VLOG(signals) << "Generating null pointer exception";
Dave Allisonb373e092014-02-20 16:06:36 -0800132 return true;
133}
134
135// A suspend check is done using the following instruction sequence:
136// 0xf723c0b2: f8d902c0 ldr.w r0, [r9, #704] ; suspend_trigger_
137// .. some intervening instruction
138// 0xf723c0b6: 6800 ldr r0, [r0, #0]
139
140// The offset from r9 is Thread::ThreadSuspendTriggerOffset().
141// To check for a suspend check, we examine the instructions that caused
142// the fault (at PC-4 and PC).
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -0800143bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
144 void* context) {
Dave Allisonb373e092014-02-20 16:06:36 -0800145 // These are the instructions to check for. The first one is the ldr r0,[r9,#xxx]
146 // where xxx is the offset of the suspend trigger.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700147 uint32_t checkinst1 = 0xf8d90000 + Thread::ThreadSuspendTriggerOffset<4>().Int32Value();
Dave Allisonb373e092014-02-20 16:06:36 -0800148 uint16_t checkinst2 = 0x6800;
149
Dave Allison69dfe512014-07-11 17:11:58 +0000150 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonb373e092014-02-20 16:06:36 -0800151 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
152 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->arm_pc);
153 uint8_t* ptr1 = ptr2 - 4;
Dave Allison5cd33752014-04-15 15:57:58 -0700154 VLOG(signals) << "checking suspend";
Dave Allisonb373e092014-02-20 16:06:36 -0800155
156 uint16_t inst2 = ptr2[0] | ptr2[1] << 8;
Dave Allison5cd33752014-04-15 15:57:58 -0700157 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
Dave Allisonb373e092014-02-20 16:06:36 -0800158 if (inst2 != checkinst2) {
159 // Second instruction is not good, not ours.
160 return false;
161 }
162
163 // The first instruction can a little bit up the stream due to load hoisting
164 // in the compiler.
165 uint8_t* limit = ptr1 - 40; // Compiler will hoist to a max of 20 instructions.
166 bool found = false;
167 while (ptr1 > limit) {
168 uint32_t inst1 = ((ptr1[0] | ptr1[1] << 8) << 16) | (ptr1[2] | ptr1[3] << 8);
Dave Allison5cd33752014-04-15 15:57:58 -0700169 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
Dave Allisonb373e092014-02-20 16:06:36 -0800170 if (inst1 == checkinst1) {
171 found = true;
172 break;
173 }
174 ptr1 -= 2; // Min instruction size is 2 bytes.
175 }
176 if (found) {
Dave Allison5cd33752014-04-15 15:57:58 -0700177 VLOG(signals) << "suspend check match";
Dave Allisonb373e092014-02-20 16:06:36 -0800178 // This is a suspend check. Arrange for the signal handler to return to
Dave Allison83252962014-04-03 16:33:48 -0700179 // art_quick_implicit_suspend. Also set LR so that after the suspend check it
Dave Allisonb373e092014-02-20 16:06:36 -0800180 // will resume the instruction (current PC + 2). PC points to the
181 // ldr r0,[r0,#0] instruction (r0 will be 0, set by the trigger).
182
183 // NB: remember that we need to set the bottom bit of the LR register
184 // to switch to thumb mode.
Dave Allison5cd33752014-04-15 15:57:58 -0700185 VLOG(signals) << "arm lr: " << std::hex << sc->arm_lr;
186 VLOG(signals) << "arm pc: " << std::hex << sc->arm_pc;
Dave Allisonb373e092014-02-20 16:06:36 -0800187 sc->arm_lr = sc->arm_pc + 3; // +2 + 1 (for thumb)
Dave Allison83252962014-04-03 16:33:48 -0700188 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
Dave Allisonb373e092014-02-20 16:06:36 -0800189
190 // Now remove the suspend trigger that caused this fault.
Dave Allisond6ed6422014-04-09 23:36:15 +0000191 Thread::Current()->RemoveSuspendTrigger();
Dave Allison5cd33752014-04-15 15:57:58 -0700192 VLOG(signals) << "removed suspend trigger invoking test suspend";
Dave Allisonb373e092014-02-20 16:06:36 -0800193 return true;
194 }
195 return false;
196}
197
Dave Allisonf9439142014-03-27 15:10:22 -0700198// Stack overflow fault handler.
199//
200// This checks that the fault address is equal to the current stack pointer
201// minus the overflow region size (16K typically). The instruction sequence
202// that generates this signal is:
203//
204// sub r12,sp,#16384
205// ldr.w r12,[r12,#0]
206//
207// The second instruction will fault if r12 is inside the protected region
208// on the stack.
209//
210// If we determine this is a stack overflow we need to move the stack pointer
Dave Allison5cd33752014-04-15 15:57:58 -0700211// to the overflow region below the protected region.
Dave Allisonf9439142014-03-27 15:10:22 -0700212
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -0800213bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
214 void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +0000215 struct ucontext* uc = reinterpret_cast<struct ucontext*>(context);
Dave Allisonf9439142014-03-27 15:10:22 -0700216 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
Dave Allison5cd33752014-04-15 15:57:58 -0700217 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
218 VLOG(signals) << "sigcontext: " << std::hex << sc;
Dave Allisonf9439142014-03-27 15:10:22 -0700219
Dave Allison5cd33752014-04-15 15:57:58 -0700220 uintptr_t sp = sc->arm_sp;
221 VLOG(signals) << "sp: " << std::hex << sp;
Dave Allisonf9439142014-03-27 15:10:22 -0700222
Dave Allison5cd33752014-04-15 15:57:58 -0700223 uintptr_t fault_addr = sc->fault_address;
224 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
225 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
Dave Allisonf9439142014-03-27 15:10:22 -0700226 ", fault_addr: " << fault_addr;
Dave Allison5cd33752014-04-15 15:57:58 -0700227
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700228 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm);
Dave Allison5cd33752014-04-15 15:57:58 -0700229
Dave Allisonf9439142014-03-27 15:10:22 -0700230 // Check that the fault address is the value expected for a stack overflow.
231 if (fault_addr != overflow_addr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700232 VLOG(signals) << "Not a stack overflow";
Dave Allisonf9439142014-03-27 15:10:22 -0700233 return false;
234 }
235
Dave Allison648d7112014-07-25 16:15:27 -0700236 VLOG(signals) << "Stack overflow found";
Dave Allisonf9439142014-03-27 15:10:22 -0700237
Dave Allison648d7112014-07-25 16:15:27 -0700238 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from.
Dave Allison5cd33752014-04-15 15:57:58 -0700239 // The value of LR must be the same as it was when we entered the code that
240 // caused this fault. This will be inserted into a callee save frame by
Dave Allison648d7112014-07-25 16:15:27 -0700241 // the function to which this handler returns (art_quick_throw_stack_overflow).
242 sc->arm_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Dave Allison5cd33752014-04-15 15:57:58 -0700243
244 // The kernel will now return to the address in sc->arm_pc.
Dave Allisonf9439142014-03-27 15:10:22 -0700245 return true;
Dave Allisonb373e092014-02-20 16:06:36 -0800246}
247} // namespace art