xref: /aosp_15_r20/art/runtime/arch/arm64/fault_handler_arm64.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "fault_handler.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <sys/ucontext.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
22*795d594fSAndroid Build Coastguard Worker #include "art_method.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/hex_dump.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"  // For VLOG.
25*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
27*795d594fSAndroid Build Coastguard Worker #include "registers_arm64.h"
28*795d594fSAndroid Build Coastguard Worker #include "runtime_globals.h"
29*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_throw_stack_overflow();
32*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_throw_null_pointer_exception_from_signal();
33*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_implicit_suspend();
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker //
36*795d594fSAndroid Build Coastguard Worker // ARM64 specific fault handler functions.
37*795d594fSAndroid Build Coastguard Worker //
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
40*795d594fSAndroid Build Coastguard Worker 
GetFaultPc(siginfo_t * siginfo,void * context)41*795d594fSAndroid Build Coastguard Worker uintptr_t FaultManager::GetFaultPc(siginfo_t* siginfo, void* context) {
42*795d594fSAndroid Build Coastguard Worker   // SEGV_MTEAERR (Async MTE fault) is delivered at an arbitrary point after the actual fault.
43*795d594fSAndroid Build Coastguard Worker   // Register contents, including PC and SP, are unrelated to the fault and can only confuse ART
44*795d594fSAndroid Build Coastguard Worker   // signal handlers.
45*795d594fSAndroid Build Coastguard Worker   if (siginfo->si_signo == SIGSEGV && siginfo->si_code == SEGV_MTEAERR) {
46*795d594fSAndroid Build Coastguard Worker     VLOG(signals) << "Async MTE fault";
47*795d594fSAndroid Build Coastguard Worker     return 0u;
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
51*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
52*795d594fSAndroid Build Coastguard Worker   if (mc->sp == 0) {
53*795d594fSAndroid Build Coastguard Worker     VLOG(signals) << "Missing SP";
54*795d594fSAndroid Build Coastguard Worker     return 0u;
55*795d594fSAndroid Build Coastguard Worker   }
56*795d594fSAndroid Build Coastguard Worker   return mc->pc;
57*795d594fSAndroid Build Coastguard Worker }
58*795d594fSAndroid Build Coastguard Worker 
GetFaultSp(void * context)59*795d594fSAndroid Build Coastguard Worker uintptr_t FaultManager::GetFaultSp(void* context) {
60*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
61*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
62*795d594fSAndroid Build Coastguard Worker   return mc->sp;
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker 
Action(int sig,siginfo_t * info,void * context)65*795d594fSAndroid Build Coastguard Worker bool NullPointerHandler::Action([[maybe_unused]] int sig, siginfo_t* info, void* context) {
66*795d594fSAndroid Build Coastguard Worker   uintptr_t fault_address = reinterpret_cast<uintptr_t>(info->si_addr);
67*795d594fSAndroid Build Coastguard Worker   if (!IsValidFaultAddress(fault_address)) {
68*795d594fSAndroid Build Coastguard Worker     return false;
69*795d594fSAndroid Build Coastguard Worker   }
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker   // For null checks in compiled code we insert a stack map that is immediately
72*795d594fSAndroid Build Coastguard Worker   // after the load/store instruction that might cause the fault and we need to
73*795d594fSAndroid Build Coastguard Worker   // pass the return PC to the handler. For null checks in Nterp, we similarly
74*795d594fSAndroid Build Coastguard Worker   // need the return PC to recognize that this was a null check in Nterp, so
75*795d594fSAndroid Build Coastguard Worker   // that the handler can get the needed data from the Nterp frame.
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
78*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
79*795d594fSAndroid Build Coastguard Worker   ArtMethod** sp = reinterpret_cast<ArtMethod**>(mc->sp);
80*795d594fSAndroid Build Coastguard Worker   uintptr_t return_pc = mc->pc + 4u;
81*795d594fSAndroid Build Coastguard Worker   if (!IsValidMethod(*sp) || !IsValidReturnPc(sp, return_pc)) {
82*795d594fSAndroid Build Coastguard Worker     return false;
83*795d594fSAndroid Build Coastguard Worker   }
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   // Push the return PC to the stack and pass the fault address in LR.
86*795d594fSAndroid Build Coastguard Worker   mc->sp -= sizeof(uintptr_t);
87*795d594fSAndroid Build Coastguard Worker   *reinterpret_cast<uintptr_t*>(mc->sp) = return_pc;
88*795d594fSAndroid Build Coastguard Worker   mc->regs[30] = fault_address;
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker   // Arrange for the signal handler to return to the NPE entrypoint.
91*795d594fSAndroid Build Coastguard Worker   mc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
92*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "Generating null pointer exception";
93*795d594fSAndroid Build Coastguard Worker   return true;
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker // A suspend check is done using the following instruction:
97*795d594fSAndroid Build Coastguard Worker //      0x...: f94002b5  ldr x21, [x21, #0]
98*795d594fSAndroid Build Coastguard Worker // 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*795d594fSAndroid Build Coastguard Worker bool SuspensionHandler::Action([[maybe_unused]] int sig,
100*795d594fSAndroid Build Coastguard Worker                                [[maybe_unused]] siginfo_t* info,
101*795d594fSAndroid Build Coastguard Worker                                void* context) {
102*795d594fSAndroid Build Coastguard Worker   constexpr uint32_t kSuspendCheckRegister = 21;
103*795d594fSAndroid Build Coastguard Worker   constexpr uint32_t checkinst =
104*795d594fSAndroid Build Coastguard Worker       0xf9400000 | (kSuspendCheckRegister << 5) | (kSuspendCheckRegister << 0);
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
107*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   uint32_t inst = *reinterpret_cast<uint32_t*>(mc->pc);
110*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "checking suspend; inst: " << std::hex << inst << " checkinst: " << checkinst;
111*795d594fSAndroid Build Coastguard Worker   if (inst != checkinst) {
112*795d594fSAndroid Build Coastguard Worker     // The instruction is not good, not ours.
113*795d594fSAndroid Build Coastguard Worker     return false;
114*795d594fSAndroid Build Coastguard Worker   }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker   // This is a suspend check.
117*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "suspend check match";
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker   // Set LR so that after the suspend check it will resume after the
120*795d594fSAndroid Build Coastguard Worker   // `ldr x21, [x21,#0]` instruction that triggered the suspend check.
121*795d594fSAndroid Build Coastguard Worker   mc->regs[30] = mc->pc + 4;
122*795d594fSAndroid Build Coastguard Worker   // Arrange for the signal handler to return to `art_quick_implicit_suspend()`.
123*795d594fSAndroid Build Coastguard Worker   mc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend);
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker   // Now remove the suspend trigger that caused this fault.
126*795d594fSAndroid Build Coastguard Worker   Thread::Current()->RemoveSuspendTrigger();
127*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "removed suspend trigger invoking test suspend";
128*795d594fSAndroid Build Coastguard Worker 
129*795d594fSAndroid Build Coastguard Worker   return true;
130*795d594fSAndroid Build Coastguard Worker }
131*795d594fSAndroid Build Coastguard Worker 
Action(int sig,siginfo_t * info,void * context)132*795d594fSAndroid Build Coastguard Worker bool StackOverflowHandler::Action([[maybe_unused]] int sig,
133*795d594fSAndroid Build Coastguard Worker                                   [[maybe_unused]] siginfo_t* info,
134*795d594fSAndroid Build Coastguard Worker                                   void* context) {
135*795d594fSAndroid Build Coastguard Worker   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
136*795d594fSAndroid Build Coastguard Worker   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
137*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
138*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "sigcontext: " << std::hex << mc;
139*795d594fSAndroid Build Coastguard Worker 
140*795d594fSAndroid Build Coastguard Worker   uintptr_t sp = mc->sp;
141*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "sp: " << std::hex << sp;
142*795d594fSAndroid Build Coastguard Worker 
143*795d594fSAndroid Build Coastguard Worker   uintptr_t fault_addr = mc->fault_address;
144*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
145*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
146*795d594fSAndroid Build Coastguard Worker       ", fault_addr: " << fault_addr;
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kArm64);
149*795d594fSAndroid Build Coastguard Worker 
150*795d594fSAndroid Build Coastguard Worker   // Check that the fault address is the value expected for a stack overflow.
151*795d594fSAndroid Build Coastguard Worker   if (fault_addr != overflow_addr) {
152*795d594fSAndroid Build Coastguard Worker     VLOG(signals) << "Not a stack overflow";
153*795d594fSAndroid Build Coastguard Worker     return false;
154*795d594fSAndroid Build Coastguard Worker   }
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker   VLOG(signals) << "Stack overflow found";
157*795d594fSAndroid Build Coastguard Worker 
158*795d594fSAndroid Build Coastguard Worker   // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
159*795d594fSAndroid Build Coastguard Worker   // The value of LR must be the same as it was when we entered the code that
160*795d594fSAndroid Build Coastguard Worker   // caused this fault.  This will be inserted into a callee save frame by
161*795d594fSAndroid Build Coastguard Worker   // the function to which this handler returns (art_quick_throw_stack_overflow).
162*795d594fSAndroid Build Coastguard Worker   mc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker   // The kernel will now return to the address in sc->pc.
165*795d594fSAndroid Build Coastguard Worker   return true;
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker }       // namespace art
168