1 /*
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 #include "fault_handler.h"
18
19 #include <sys/ucontext.h>
20
21 #include "arch/instruction_set.h"
22 #include "art_method.h"
23 #include "base/enums.h"
24 #include "base/hex_dump.h"
25 #include "base/logging.h" // For VLOG.
26 #include "base/macros.h"
27 #include "registers_arm64.h"
28 #include "runtime_globals.h"
29 #include "thread-current-inl.h"
30
31 extern "C" void art_quick_throw_stack_overflow();
32 extern "C" void art_quick_throw_null_pointer_exception_from_signal();
33 extern "C" void art_quick_implicit_suspend();
34
35 //
36 // ARM64 specific fault handler functions.
37 //
38
39 namespace art {
40
GetMethodAndReturnPcAndSp(siginfo_t * siginfo ATTRIBUTE_UNUSED,void * context,ArtMethod ** out_method,uintptr_t * out_return_pc,uintptr_t * out_sp,bool * out_is_stack_overflow)41 void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED,
42 void* context,
43 ArtMethod** out_method,
44 uintptr_t* out_return_pc,
45 uintptr_t* out_sp,
46 bool* out_is_stack_overflow) {
47 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
48 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
49 *out_sp = static_cast<uintptr_t>(sc->sp);
50 VLOG(signals) << "sp: " << *out_sp;
51 if (*out_sp == 0) {
52 return;
53 }
54
55 // In the case of a stack overflow, the stack is not valid and we can't
56 // get the method from the top of the stack. However it's in x0.
57 uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(sc->fault_address);
58 uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>(
59 reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(InstructionSet::kArm64));
60 if (overflow_addr == fault_addr) {
61 *out_method = reinterpret_cast<ArtMethod*>(sc->regs[0]);
62 *out_is_stack_overflow = true;
63 } else {
64 // The method is at the top of the stack.
65 *out_method = *reinterpret_cast<ArtMethod**>(*out_sp);
66 *out_is_stack_overflow = false;
67 }
68
69 // Work out the return PC. This will be the address of the instruction
70 // following the faulting ldr/str instruction.
71 VLOG(signals) << "pc: " << std::hex
72 << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->pc));
73
74 *out_return_pc = sc->pc + 4;
75 }
76
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info,void * context)77 bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
78 if (!IsValidImplicitCheck(info)) {
79 return false;
80 }
81 // The code that looks for the catch location needs to know the value of the
82 // PC at the point of call. For Null checks we insert a GC map that is immediately after
83 // the load/store instruction that might cause the fault.
84
85 struct ucontext *uc = reinterpret_cast<struct ucontext*>(context);
86 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
87
88 // Push the gc map location to the stack and pass the fault address in LR.
89 sc->sp -= sizeof(uintptr_t);
90 *reinterpret_cast<uintptr_t*>(sc->sp) = sc->pc + 4;
91 sc->regs[30] = reinterpret_cast<uintptr_t>(info->si_addr);
92
93 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
94 VLOG(signals) << "Generating null pointer exception";
95 return true;
96 }
97
98 // A suspend check is done using the following instruction sequence:
99 // 0xf7223228: f9405640 ldr x0, [x18, #168]
100 // .. some intervening instructions
101 // 0xf7223230: f9400000 ldr x0, [x0]
102
103 // The offset from r18 is Thread::ThreadSuspendTriggerOffset().
104 // To check for a suspend check, we examine the instructions that caused
105 // the fault (at PC-4 and PC).
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context)106 bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
107 void* context) {
108 // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx]
109 // where xxx is the offset of the suspend trigger.
110 uint32_t checkinst1 = 0xf9400240 |
111 (Thread::ThreadSuspendTriggerOffset<PointerSize::k64>().Int32Value() << 7);
112 uint32_t checkinst2 = 0xf9400000;
113
114 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
115 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
116 uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc);
117 uint8_t* ptr1 = ptr2 - 4;
118 VLOG(signals) << "checking suspend";
119
120 uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2);
121 VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2;
122 if (inst2 != checkinst2) {
123 // Second instruction is not good, not ours.
124 return false;
125 }
126
127 // The first instruction can a little bit up the stream due to load hoisting
128 // in the compiler.
129 uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions.
130 bool found = false;
131 while (ptr1 > limit) {
132 uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1);
133 VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1;
134 if (inst1 == checkinst1) {
135 found = true;
136 break;
137 }
138 ptr1 -= 4;
139 }
140 if (found) {
141 VLOG(signals) << "suspend check match";
142 // This is a suspend check. Arrange for the signal handler to return to
143 // art_quick_implicit_suspend. Also set LR so that after the suspend check it
144 // will resume the instruction (current PC + 4). PC points to the
145 // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger).
146
147 sc->regs[30] = sc->pc + 4;
148 sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
149
150 // Now remove the suspend trigger that caused this fault.
151 Thread::Current()->RemoveSuspendTrigger();
152 VLOG(signals) << "removed suspend trigger invoking test suspend";
153 return true;
154 }
155 return false;
156 }
157
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context)158 bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
159 void* context) {
160 struct ucontext *uc = reinterpret_cast<struct ucontext *>(context);
161 struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext);
162 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
163 VLOG(signals) << "sigcontext: " << std::hex << sc;
164
165 uintptr_t sp = sc->sp;
166 VLOG(signals) << "sp: " << std::hex << sp;
167
168 uintptr_t fault_addr = sc->fault_address;
169 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
170 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
171 ", fault_addr: " << fault_addr;
172
173 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kArm64);
174
175 // Check that the fault address is the value expected for a stack overflow.
176 if (fault_addr != overflow_addr) {
177 VLOG(signals) << "Not a stack overflow";
178 return false;
179 }
180
181 VLOG(signals) << "Stack overflow found";
182
183 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
184 // The value of LR must be the same as it was when we entered the code that
185 // caused this fault. This will be inserted into a callee save frame by
186 // the function to which this handler returns (art_quick_throw_stack_overflow).
187 sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
188
189 // The kernel will now return to the address in sc->pc.
190 return true;
191 }
192 } // namespace art
193
194