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