blob: f591fccde219bb27b4167f67397e9223177b857f [file] [log] [blame]
Stuart Monteithb95a5342014-03-12 13:32:32 +00001/*
2 * Copyright (C) 2014 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
Stuart Monteithb95a5342014-03-12 13:32:32 +000020#include <sys/ucontext.h>
Mathieu Chartiere401d142015-04-22 13:56:20 -070021
22#include "art_method-inl.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000023#include "base/macros.h"
24#include "globals.h"
25#include "base/logging.h"
26#include "base/hex_dump.h"
Stuart Monteithd5c78f42014-06-11 16:44:46 +010027#include "registers_arm64.h"
Stuart Monteithd5c78f42014-06-11 16:44:46 +010028#include "thread.h"
29#include "thread-inl.h"
Stuart Monteithb95a5342014-03-12 13:32:32 +000030
Dave Allison648d7112014-07-25 16:15:27 -070031extern "C" void art_quick_throw_stack_overflow();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010032extern "C" void art_quick_throw_null_pointer_exception_from_signal();
Stuart Monteithd5c78f42014-06-11 16:44:46 +010033extern "C" void art_quick_implicit_suspend();
Stuart Monteithb95a5342014-03-12 13:32:32 +000034
35//
36// ARM64 specific fault handler functions.
37//
38
39namespace art {
40
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080041void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
42 void* context) {
Dave Allison8ce6b902014-08-26 11:07:58 -070043 // To match the case used in ARM we return directly to the longjmp function
44 // rather than through a trivial assembly language stub.
45
46 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
47 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
48 Thread* self = Thread::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 CHECK(self != nullptr); // This will cause a SIGABRT if self is null.
Dave Allison8ce6b902014-08-26 11:07:58 -070050
51 sc->regs[0] = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState());
52 sc->regs[1] = 1;
53 sc->pc = reinterpret_cast<uintptr_t>(longjmp);
54}
55
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -080056void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, void* context,
Mathieu Chartiere401d142015-04-22 13:56:20 -070057 ArtMethod** out_method,
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070058 uintptr_t* out_return_pc, uintptr_t* out_sp) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +010059 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
60 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
61 *out_sp = static_cast<uintptr_t>(sc->sp);
62 VLOG(signals) << "sp: " << *out_sp;
63 if (*out_sp == 0) {
64 return;
65 }
66
67 // In the case of a stack overflow, the stack is not valid and we can't
68 // get the method from the top of the stack. However it's in x0.
69 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
70 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
71 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kArm64));
72 if (overflow_addr == fault_addr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 *out_method = reinterpret_cast<ArtMethod*>(sc->regs[0]);
Stuart Monteithd5c78f42014-06-11 16:44:46 +010074 } else {
75 // The method is at the top of the stack.
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
Stuart Monteithd5c78f42014-06-11 16:44:46 +010077 }
78
79 // Work out the return PC. This will be the address of the instruction
80 // following the faulting ldr/str instruction.
81 VLOG(signals) << "pc: " << std::hex
82 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->pc));
83
84 *out_return_pc = sc->pc + 4;
Stuart Monteithb95a5342014-03-12 13:32:32 +000085}
86
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010087bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
88 if (!IsValidImplicitCheck(info)) {
89 return false;
90 }
Stuart Monteithd5c78f42014-06-11 16:44:46 +010091 // The code that looks for the catch location needs to know the value of the
92 // PC at the point of call. For Null checks we insert a GC map that is immediately after
93 // the load/store instruction that might cause the fault.
94
95 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
96 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
97
98 sc->regs[30] = sc->pc + 4; // LR needs to point to gc map location
99
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100100 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
101 // Pass the faulting address as the first argument of
102 // art_quick_throw_null_pointer_exception_from_signal.
103 sc->regs[0] = reinterpret_cast<uintptr_t>(info->si_addr);
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100104 VLOG(signals) << "Generating null pointer exception";
105 return true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000106}
107
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100108// A suspend check is done using the following instruction sequence:
109// 0xf7223228: f9405640 ldr x0, [x18, #168]
110// .. some intervening instructions
111// 0xf7223230: f9400000 ldr x0, [x0]
112
113// The offset from r18 is Thread::ThreadSuspendTriggerOffset().
114// To check for a suspend check, we examine the instructions that caused
115// the fault (at PC-4 and PC).
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -0800116bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
117 void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100118 // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx]
119 // where xxx is the offset of the suspend trigger.
120 uint32_t checkinst1 = 0xf9400240 | (Thread::ThreadSuspendTriggerOffset<8>().Int32Value() << 7);
121 uint32_t checkinst2 = 0xf9400000;
122
123 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
124 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
125 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc);
126 uint8_t* ptr1 = ptr2 - 4;
127 VLOG(signals) << "checking suspend";
128
129 uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2);
130 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
131 if (inst2 != checkinst2) {
132 // Second instruction is not good, not ours.
133 return false;
134 }
135
136 // The first instruction can a little bit up the stream due to load hoisting
137 // in the compiler.
138 uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions.
139 bool found = false;
140 while (ptr1 > limit) {
141 uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1);
142 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
143 if (inst1 == checkinst1) {
144 found = true;
145 break;
146 }
147 ptr1 -= 4;
148 }
149 if (found) {
150 VLOG(signals) << "suspend check match";
151 // This is a suspend check. Arrange for the signal handler to return to
152 // art_quick_implicit_suspend. Also set LR so that after the suspend check it
153 // will resume the instruction (current PC + 4). PC points to the
154 // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger).
155
156 sc->regs[30] = sc->pc + 4;
157 sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
158
159 // Now remove the suspend trigger that caused this fault.
160 Thread::Current()->RemoveSuspendTrigger();
161 VLOG(signals) << "removed suspend trigger invoking test suspend";
162 return true;
163 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000164 return false;
165}
166
Chih-Hung Hsiehc4f990e2014-11-04 14:39:03 -0800167bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
168 void* context) {
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100169 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
170 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
171 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
172 VLOG(signals) << "sigcontext: " << std::hex << sc;
173
174 uintptr_t sp = sc->sp;
175 VLOG(signals) << "sp: " << std::hex << sp;
176
177 uintptr_t fault_addr = sc->fault_address;
178 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
179 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
180 ", fault_addr: " << fault_addr;
181
182 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm64);
183
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100184 // Check that the fault address is the value expected for a stack overflow.
185 if (fault_addr != overflow_addr) {
186 VLOG(signals) << "Not a stack overflow";
187 return false;
188 }
189
Dave Allison648d7112014-07-25 16:15:27 -0700190 VLOG(signals) << "Stack overflow found";
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100191
Dave Allison648d7112014-07-25 16:15:27 -0700192 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100193 // The value of LR must be the same as it was when we entered the code that
194 // caused this fault. This will be inserted into a callee save frame by
Dave Allison648d7112014-07-25 16:15:27 -0700195 // the function to which this handler returns (art_quick_throw_stack_overflow).
196 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100197
198 // The kernel will now return to the address in sc->pc.
199 return true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000200}
201} // namespace art
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100202