Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 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" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 20 | #include <sys/ucontext.h> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | |
| 22 | #include "art_method-inl.h" |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 23 | #include "base/macros.h" |
| 24 | #include "globals.h" |
| 25 | #include "base/logging.h" |
| 26 | #include "base/hex_dump.h" |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 27 | #include "registers_arm64.h" |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 28 | #include "thread.h" |
| 29 | #include "thread-inl.h" |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 30 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 31 | extern "C" void art_quick_throw_stack_overflow(); |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 32 | extern "C" void art_quick_throw_null_pointer_exception_from_signal(); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 33 | extern "C" void art_quick_implicit_suspend(); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 34 | |
| 35 | // |
| 36 | // ARM64 specific fault handler functions. |
| 37 | // |
| 38 | |
| 39 | namespace art { |
| 40 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 41 | void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 42 | void* context) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 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(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 49 | CHECK(self != nullptr); // This will cause a SIGABRT if self is null. |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 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 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 56 | void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, void* context, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 57 | ArtMethod** out_method, |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 58 | uintptr_t* out_return_pc, uintptr_t* out_sp) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 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) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | *out_method = reinterpret_cast<ArtMethod*>(sc->regs[0]); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 74 | } else { |
| 75 | // The method is at the top of the stack. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 76 | *out_method = *reinterpret_cast<ArtMethod**>(*out_sp); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 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; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 87 | bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) { |
| 88 | if (!IsValidImplicitCheck(info)) { |
| 89 | return false; |
| 90 | } |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 91 | // The code that looks for the catch location needs to know the value of the |
| 92 | // PC at the point of call. For Null checks we insert a GC map that is immediately after |
| 93 | // the load/store instruction that might cause the fault. |
| 94 | |
| 95 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 96 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 97 | |
| 98 | sc->regs[30] = sc->pc + 4; // LR needs to point to gc map location |
| 99 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 100 | sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception_from_signal); |
| 101 | // Pass the faulting address as the first argument of |
| 102 | // art_quick_throw_null_pointer_exception_from_signal. |
| 103 | sc->regs[0] = reinterpret_cast<uintptr_t>(info->si_addr); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 104 | VLOG(signals) << "Generating null pointer exception"; |
| 105 | return true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 108 | // A suspend check is done using the following instruction sequence: |
| 109 | // 0xf7223228: f9405640 ldr x0, [x18, #168] |
| 110 | // .. some intervening instructions |
| 111 | // 0xf7223230: f9400000 ldr x0, [x0] |
| 112 | |
| 113 | // The offset from r18 is Thread::ThreadSuspendTriggerOffset(). |
| 114 | // To check for a suspend check, we examine the instructions that caused |
| 115 | // the fault (at PC-4 and PC). |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 116 | bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 117 | void* context) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 118 | // These are the instructions to check for. The first one is the ldr x0,[r18,#xxx] |
| 119 | // where xxx is the offset of the suspend trigger. |
| 120 | uint32_t checkinst1 = 0xf9400240 | (Thread::ThreadSuspendTriggerOffset<8>().Int32Value() << 7); |
| 121 | uint32_t checkinst2 = 0xf9400000; |
| 122 | |
| 123 | struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); |
| 124 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 125 | uint8_t* ptr2 = reinterpret_cast<uint8_t*>(sc->pc); |
| 126 | uint8_t* ptr1 = ptr2 - 4; |
| 127 | VLOG(signals) << "checking suspend"; |
| 128 | |
| 129 | uint32_t inst2 = *reinterpret_cast<uint32_t*>(ptr2); |
| 130 | VLOG(signals) << "inst2: " << std::hex << inst2 << " checkinst2: " << checkinst2; |
| 131 | if (inst2 != checkinst2) { |
| 132 | // Second instruction is not good, not ours. |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // The first instruction can a little bit up the stream due to load hoisting |
| 137 | // in the compiler. |
| 138 | uint8_t* limit = ptr1 - 80; // Compiler will hoist to a max of 20 instructions. |
| 139 | bool found = false; |
| 140 | while (ptr1 > limit) { |
| 141 | uint32_t inst1 = *reinterpret_cast<uint32_t*>(ptr1); |
| 142 | VLOG(signals) << "inst1: " << std::hex << inst1 << " checkinst1: " << checkinst1; |
| 143 | if (inst1 == checkinst1) { |
| 144 | found = true; |
| 145 | break; |
| 146 | } |
| 147 | ptr1 -= 4; |
| 148 | } |
| 149 | if (found) { |
| 150 | VLOG(signals) << "suspend check match"; |
| 151 | // This is a suspend check. Arrange for the signal handler to return to |
| 152 | // art_quick_implicit_suspend. Also set LR so that after the suspend check it |
| 153 | // will resume the instruction (current PC + 4). PC points to the |
| 154 | // ldr x0,[x0,#0] instruction (r0 will be 0, set by the trigger). |
| 155 | |
| 156 | sc->regs[30] = sc->pc + 4; |
| 157 | sc->pc = reinterpret_cast<uintptr_t>(art_quick_implicit_suspend); |
| 158 | |
| 159 | // Now remove the suspend trigger that caused this fault. |
| 160 | Thread::Current()->RemoveSuspendTrigger(); |
| 161 | VLOG(signals) << "removed suspend trigger invoking test suspend"; |
| 162 | return true; |
| 163 | } |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
Chih-Hung Hsieh | c4f990e | 2014-11-04 14:39:03 -0800 | [diff] [blame] | 167 | bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, |
| 168 | void* context) { |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 169 | struct ucontext *uc = reinterpret_cast<struct ucontext *>(context); |
| 170 | struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); |
| 171 | VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; |
| 172 | VLOG(signals) << "sigcontext: " << std::hex << sc; |
| 173 | |
| 174 | uintptr_t sp = sc->sp; |
| 175 | VLOG(signals) << "sp: " << std::hex << sp; |
| 176 | |
| 177 | uintptr_t fault_addr = sc->fault_address; |
| 178 | VLOG(signals) << "fault_addr: " << std::hex << fault_addr; |
| 179 | VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << |
| 180 | ", fault_addr: " << fault_addr; |
| 181 | |
| 182 | uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kArm64); |
| 183 | |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 184 | // Check that the fault address is the value expected for a stack overflow. |
| 185 | if (fault_addr != overflow_addr) { |
| 186 | VLOG(signals) << "Not a stack overflow"; |
| 187 | return false; |
| 188 | } |
| 189 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 190 | VLOG(signals) << "Stack overflow found"; |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 191 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 192 | // Now arrange for the signal handler to return to art_quick_throw_stack_overflow. |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 193 | // The value of LR must be the same as it was when we entered the code that |
| 194 | // caused this fault. This will be inserted into a callee save frame by |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 195 | // the function to which this handler returns (art_quick_throw_stack_overflow). |
| 196 | sc->pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow); |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 197 | |
| 198 | // The kernel will now return to the address in sc->pc. |
| 199 | return true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 200 | } |
| 201 | } // namespace art |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 202 | |