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
GetFaultPc(siginfo_t * siginfo,void * context)41 uintptr_t FaultManager::GetFaultPc(siginfo_t* siginfo, void* context) {
42 // SEGV_MTEAERR (Async MTE fault) is delivered at an arbitrary point after the actual fault.
43 // Register contents, including PC and SP, are unrelated to the fault and can only confuse ART
44 // signal handlers.
45 if (siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_MTEAERR) {
46 VLOG(signals) << "Async MTE fault";
47 return 0u;
48 }
49
50 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
51 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
52 if (mc->sp == 0) {
53 VLOG(signals) << "Missing SP";
54 return 0u;
55 }
56 return mc->pc;
57 }
58
GetFaultSp(void * context)59 uintptr_t FaultManager::GetFaultSp(void* context) {
60 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
61 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
62 return mc->sp;
63 }
64
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info,void * context)65 bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) {
66 uintptr_t fault_address = reinterpret_cast<uintptr_t>(info->si_addr);
67 if (!IsValidFaultAddress(fault_address)) {
68 return false;
69 }
70
71 // For null checks in compiled code we insert a stack map that is immediately
72 // after the load/store instruction that might cause the fault and we need to
73 // pass the return PC to the handler. For null checks in Nterp, we similarly
74 // need the return PC to recognize that this was a null check in Nterp, so
75 // that the handler can get the needed data from the Nterp frame.
76
77 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
78 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
79 ArtMethod** sp = reinterpret_cast<ArtMethod**>(mc->sp);
80 uintptr_t return_pc = mc->pc + 4u;
81 if (!IsValidMethod(*sp) || !IsValidReturnPc(sp, return_pc)) {
82 return false;
83 }
84
85 // Push the return PC to the stack and pass the fault address in LR.
86 mc->sp -= sizeof(uintptr_t);
87 *reinterpret_cast<uintptr_t*>(mc->sp) = return_pc;
88 mc->regs[30] = fault_address;
89
90 // Arrange for the signal handler to return to the NPE entrypoint.
91 mc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
92 VLOG(signals) << "Generating null pointer exception";
93 return true;
94 }
95
96 // A suspend check is done using the following instruction:
97 // 0x...: f94002b5 ldr x21, [x21, #0]
98 // To check for a suspend check, we examine the instruction that caused the fault (at PC).
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context)99 bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
100 void* context) {
101 constexpr uint32_t kSuspendCheckRegister = 21;
102 constexpr uint32_t checkinst =
103 0xf9400000 | (kSuspendCheckRegister << 5) | (kSuspendCheckRegister << 0);
104
105 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
106 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
107
108 uint32_t inst = *reinterpret_cast<uint32_t*>(mc->pc);
109 VLOG(signals) << "checking suspend; inst: " << std::hex << inst << " checkinst: " << checkinst;
110 if (inst != checkinst) {
111 // The instruction is not good, not ours.
112 return false;
113 }
114
115 // This is a suspend check.
116 VLOG(signals) << "suspend check match";
117
118 // Set LR so that after the suspend check it will resume after the
119 // `ldr x21, [x21,#0]` instruction that triggered the suspend check.
120 mc->regs[30] = mc->pc + 4;
121 // Arrange for the signal handler to return to `art_quick_implicit_suspend()`.
122 mc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
123
124 // Now remove the suspend trigger that caused this fault.
125 Thread::Current()->RemoveSuspendTrigger();
126 VLOG(signals) << "removed suspend trigger invoking test suspend";
127
128 return true;
129 }
130
Action(int sig ATTRIBUTE_UNUSED,siginfo_t * info ATTRIBUTE_UNUSED,void * context)131 bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED,
132 void* context) {
133 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
134 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
135 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
136 VLOG(signals) << "sigcontext: " << std::hex << mc;
137
138 uintptr_t sp = mc->sp;
139 VLOG(signals) << "sp: " << std::hex << sp;
140
141 uintptr_t fault_addr = mc->fault_address;
142 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
143 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
144 ", fault_addr: " << fault_addr;
145
146 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kArm64);
147
148 // Check that the fault address is the value expected for a stack overflow.
149 if (fault_addr != overflow_addr) {
150 VLOG(signals) << "Not a stack overflow";
151 return false;
152 }
153
154 VLOG(signals) << "Stack overflow found";
155
156 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
157 // The value of LR must be the same as it was when we entered the code that
158 // caused this fault. This will be inserted into a callee save frame by
159 // the function to which this handler returns (art_quick_throw_stack_overflow).
160 mc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
161
162 // The kernel will now return to the address in sc->pc.
163 return true;
164 }
165 } // namespace art
166