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