1 /*
2  * Copyright (C) 2023 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 "base/logging.h"  // For VLOG.
23 
24 extern "C" void art_quick_throw_stack_overflow();
25 extern "C" void art_quick_throw_null_pointer_exception_from_signal();
26 extern "C" void art_quick_implicit_suspend();
27 
28 // RISCV64 specific fault handler functions (or stubs if unimplemented yet).
29 
30 namespace art HIDDEN {
31 
GetFaultPc(siginfo_t *,void * context)32 uintptr_t FaultManager::GetFaultPc(siginfo_t*, void* context) {
33   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
34   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
35   if (mc->__gregs[REG_SP] == 0) {
36     VLOG(signals) << "Missing SP";
37     return 0u;
38   }
39   return mc->__gregs[REG_PC];
40 }
41 
GetFaultSp(void * context)42 uintptr_t FaultManager::GetFaultSp(void* context) {
43   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
44   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
45   return mc->__gregs[REG_SP];
46 }
47 
Action(int sig,siginfo_t * info,void * context)48 bool NullPointerHandler::Action([[maybe_unused]] int sig, siginfo_t* info, void* context) {
49   uintptr_t fault_address = reinterpret_cast<uintptr_t>(info->si_addr);
50   if (!IsValidFaultAddress(fault_address)) {
51     return false;
52   }
53 
54   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
55   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
56   ArtMethod** sp = reinterpret_cast<ArtMethod**>(mc->__gregs[REG_SP]);
57   if (!IsValidMethod(*sp)) {
58     return false;
59   }
60 
61   // For null checks in compiled code we insert a stack map that is immediately
62   // after the load/store instruction that might cause the fault and we need to
63   // pass the return PC to the handler. For null checks in Nterp, we similarly
64   // need the return PC to recognize that this was a null check in Nterp, so
65   // that the handler can get the needed data from the Nterp frame.
66 
67   // Need to work out the size of the instruction that caused the exception.
68   uintptr_t old_pc = mc->__gregs[REG_PC];
69   uintptr_t instr_size = (reinterpret_cast<uint16_t*>(old_pc)[0] & 3u) == 3u ? 4u : 2u;
70   uintptr_t return_pc = old_pc + instr_size;
71   if (!IsValidReturnPc(sp, return_pc)) {
72     return false;
73   }
74 
75   // Push the return PC to the stack and pass the fault address in RA.
76   mc->__gregs[REG_SP] -= sizeof(uintptr_t);
77   *reinterpret_cast<uintptr_t*>(mc->__gregs[REG_SP]) = return_pc;
78   mc->__gregs[REG_RA] = fault_address;
79 
80   // Arrange for the signal handler to return to the NPE entrypoint.
81   mc->__gregs[REG_PC] =
82       reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal);
83   VLOG(signals) << "Generating null pointer exception";
84   return true;
85 }
86 
Action(int,siginfo_t *,void *)87 bool SuspensionHandler::Action(int, siginfo_t*, void*) {
88   LOG(FATAL) << "SuspensionHandler::Action is not implemented for RISC-V";
89   return false;
90 }
91 
Action(int sig,siginfo_t * info,void * context)92 bool StackOverflowHandler::Action([[maybe_unused]] int sig,
93                                   siginfo_t* info,
94                                   void* context) {
95   ucontext_t* uc = reinterpret_cast<ucontext_t*>(context);
96   mcontext_t* mc = reinterpret_cast<mcontext_t*>(&uc->uc_mcontext);
97   VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc;
98   VLOG(signals) << "sigcontext: " << std::hex << mc;
99 
100   uintptr_t sp = mc->__gregs[REG_SP];
101   VLOG(signals) << "sp: " << std::hex << sp;
102 
103   uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
104   VLOG(signals) << "fault_addr: " << std::hex << fault_addr;
105   VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp <<
106       ", fault_addr: " << fault_addr;
107 
108   uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(InstructionSet::kRiscv64);
109 
110   // Check that the fault address is the value expected for a stack overflow.
111   if (fault_addr != overflow_addr) {
112     VLOG(signals) << "Not a stack overflow";
113     return false;
114   }
115 
116   VLOG(signals) << "Stack overflow found";
117 
118   // Now arrange for the signal handler to return to art_quick_throw_stack_overflow.
119   // The value of RA must be the same as it was when we entered the code that
120   // caused this fault.  This will be inserted into a callee save frame by
121   // the function to which this handler returns (art_quick_throw_stack_overflow).
122   mc->__gregs[REG_PC] = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow);
123 
124   // The kernel will now return to the address in `mc->__gregs[REG_PC]`.
125   return true;
126 }
127 
128 }  // namespace art
129