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/hex_dump.h"
24 #include "base/logging.h" // For VLOG.
25 #include "base/macros.h"
26 #include "base/pointer_size.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 HIDDEN {
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,siginfo_t * info,void * context)65 bool NullPointerHandler::Action([[maybe_unused]] int sig, 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,siginfo_t * info,void * context)99 bool SuspensionHandler::Action([[maybe_unused]] int sig,
100 [[maybe_unused]] siginfo_t* info,
101 void* context) {
102 constexpr uint32_t kSuspendCheckRegister = 21;
103 constexpr uint32_t checkinst =
104 0xf9400000 | (kSuspendCheckRegister << 5) | (kSuspendCheckRegister << 0);
105
106 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
107 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
108
109 uint32_t inst = *reinterpret_cast<uint32_t*>(mc->pc);
110 VLOG(signals) << "checking suspend; inst: " << std::hex << inst << " checkinst: " << checkinst;
111 if (inst != checkinst) {
112 // The instruction is not good, not ours.
113 return false;
114 }
115
116 // This is a suspend check.
117 VLOG(signals) << "suspend check match";
118
119 // Set LR so that after the suspend check it will resume after the
120 // `ldr x21, [x21,#0]` instruction that triggered the suspend check.
121 mc->regs[30] = mc->pc + 4;
122 // Arrange for the signal handler to return to `art_quick_implicit_suspend()`.
123 mc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
124
125 // Now remove the suspend trigger that caused this fault.
126 Thread::Current()->RemoveSuspendTrigger();
127 VLOG(signals) << "removed suspend trigger invoking test suspend";
128
129 return true;
130 }
131
Action(int sig,siginfo_t * info,void * context)132 bool StackOverflowHandler::Action([[maybe_unused]] int sig,
133 [[maybe_unused]] siginfo_t* info,
134 void* context) {
135 ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
136 mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
137 VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
138 VLOG(signals) << "sigcontext: " << std::hex << mc;
139
140 uintptr_t sp = mc->sp;
141 VLOG(signals) << "sp: " << std::hex << sp;
142
143 uintptr_t fault_addr = mc->fault_address;
144 VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
145 VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
146 ", fault_addr: " << fault_addr;
147
148 uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kArm64);
149
150 // Check that the fault address is the value expected for a stack overflow.
151 if (fault_addr != overflow_addr) {
152 VLOG(signals) << "Not a stack overflow";
153 return false;
154 }
155
156 VLOG(signals) << "Stack overflow found";
157
158 // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
159 // The value of LR must be the same as it was when we entered the code that
160 // caused this fault. This will be inserted into a callee save frame by
161 // the function to which this handler returns (art_quick_throw_stack_overflow).
162 mc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
163
164 // The kernel will now return to the address in sc->pc.
165 return true;
166 }
167 } // namespace art
168