Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 23 | #include "base/enums.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
| 25 | #include "globals.h" |
| 26 | #include "base/logging.h" |
| 27 | #include "base/hex_dump.h" |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 28 | #include "thread.h" |
| 29 | #include "thread-inl.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 30 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 31 | #if defined(__APPLE__) |
| 32 | #define ucontext __darwin_ucontext |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 33 | |
| 34 | #if defined(__x86_64__) |
| 35 | // 64 bit mac build. |
| 36 | #define CTX_ESP uc_mcontext->__ss.__rsp |
| 37 | #define CTX_EIP uc_mcontext->__ss.__rip |
| 38 | #define CTX_EAX uc_mcontext->__ss.__rax |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 39 | #define CTX_METHOD uc_mcontext->__ss.__rdi |
Nicolas Geoffray | 5b64306 | 2016-06-29 14:34:21 +0100 | [diff] [blame] | 40 | #define CTX_RDI uc_mcontext->__ss.__rdi |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 41 | #define CTX_JMP_BUF uc_mcontext->__ss.__rdi |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 42 | #else |
| 43 | // 32 bit mac build. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 44 | #define CTX_ESP uc_mcontext->__ss.__esp |
| 45 | #define CTX_EIP uc_mcontext->__ss.__eip |
| 46 | #define CTX_EAX uc_mcontext->__ss.__eax |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 47 | #define CTX_METHOD uc_mcontext->__ss.__eax |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 48 | #define CTX_JMP_BUF uc_mcontext->__ss.__eax |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 49 | #endif |
| 50 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 51 | #elif defined(__x86_64__) |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 52 | // 64 bit linux build. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 53 | #define CTX_ESP uc_mcontext.gregs[REG_RSP] |
| 54 | #define CTX_EIP uc_mcontext.gregs[REG_RIP] |
| 55 | #define CTX_EAX uc_mcontext.gregs[REG_RAX] |
| 56 | #define CTX_METHOD uc_mcontext.gregs[REG_RDI] |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 57 | #define CTX_RDI uc_mcontext.gregs[REG_RDI] |
| 58 | #define CTX_JMP_BUF uc_mcontext.gregs[REG_RDI] |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 59 | #else |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 60 | // 32 bit linux build. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 61 | #define CTX_ESP uc_mcontext.gregs[REG_ESP] |
| 62 | #define CTX_EIP uc_mcontext.gregs[REG_EIP] |
| 63 | #define CTX_EAX uc_mcontext.gregs[REG_EAX] |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 64 | #define CTX_METHOD uc_mcontext.gregs[REG_EAX] |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 65 | #define CTX_JMP_BUF uc_mcontext.gregs[REG_EAX] |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 66 | #endif |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 67 | |
| 68 | // |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 69 | // X86 (and X86_64) specific fault handler functions. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 70 | // |
| 71 | |
| 72 | namespace art { |
| 73 | |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 74 | #if defined(__APPLE__) && defined(__x86_64__) |
| 75 | // mac symbols have a prefix of _ on x86_64 |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 76 | extern "C" void _art_quick_throw_null_pointer_exception_from_signal(); |
Andreas Gampe | eb0ab9e | 2014-08-14 11:15:22 -0700 | [diff] [blame] | 77 | extern "C" void _art_quick_throw_stack_overflow(); |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 78 | extern "C" void _art_quick_test_suspend(); |
| 79 | #define EXT_SYM(sym) _ ## sym |
| 80 | #else |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 81 | extern "C" void art_quick_throw_null_pointer_exception_from_signal(); |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 82 | extern "C" void art_quick_throw_stack_overflow(); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 83 | extern "C" void art_quick_test_suspend(); |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 84 | #define EXT_SYM(sym) sym |
| 85 | #endif |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 86 | |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 87 | // Note this is different from the others (no underscore on 64 bit mac) due to |
| 88 | // the way the symbol is defined in the .S file. |
| 89 | // TODO: fix the symbols for 64 bit mac - there is a double underscore prefix for some |
| 90 | // of them. |
| 91 | extern "C" void art_nested_signal_return(); |
| 92 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 93 | // Get the size of an instruction in bytes. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 94 | // Return 0 if the instruction is not handled. |
| 95 | static uint32_t GetInstructionSize(const uint8_t* pc) { |
| 96 | #if defined(__x86_64) |
| 97 | const bool x86_64 = true; |
| 98 | #else |
| 99 | const bool x86_64 = false; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 102 | const uint8_t* startpc = pc; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 103 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 104 | uint8_t opcode = *pc++; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 105 | uint8_t modrm; |
| 106 | bool has_modrm = false; |
| 107 | bool two_byte = false; |
| 108 | uint32_t displacement_size = 0; |
| 109 | uint32_t immediate_size = 0; |
Mark Mendell | 5daf8e1 | 2014-09-25 15:13:39 -0400 | [diff] [blame] | 110 | bool operand_size_prefix = false; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 111 | |
| 112 | // Prefixes. |
| 113 | while (true) { |
| 114 | bool prefix_present = false; |
| 115 | switch (opcode) { |
Mark Mendell | 5daf8e1 | 2014-09-25 15:13:39 -0400 | [diff] [blame] | 116 | // Group 3 |
| 117 | case 0x66: |
| 118 | operand_size_prefix = true; |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 119 | FALLTHROUGH_INTENDED; |
Mark Mendell | 5daf8e1 | 2014-09-25 15:13:39 -0400 | [diff] [blame] | 120 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 121 | // Group 1 |
| 122 | case 0xf0: |
| 123 | case 0xf2: |
| 124 | case 0xf3: |
| 125 | |
| 126 | // Group 2 |
| 127 | case 0x2e: |
| 128 | case 0x36: |
| 129 | case 0x3e: |
| 130 | case 0x26: |
| 131 | case 0x64: |
| 132 | case 0x65: |
| 133 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 134 | // Group 4 |
| 135 | case 0x67: |
| 136 | opcode = *pc++; |
| 137 | prefix_present = true; |
| 138 | break; |
| 139 | } |
| 140 | if (!prefix_present) { |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (x86_64 && opcode >= 0x40 && opcode <= 0x4f) { |
| 146 | opcode = *pc++; |
| 147 | } |
| 148 | |
| 149 | if (opcode == 0x0f) { |
| 150 | // Two byte opcode |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 151 | two_byte = true; |
| 152 | opcode = *pc++; |
| 153 | } |
| 154 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 155 | bool unhandled_instruction = false; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 156 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 157 | if (two_byte) { |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 158 | switch (opcode) { |
Serguei Katkov | e2d596e | 2014-09-08 17:48:25 +0700 | [diff] [blame] | 159 | case 0x10: // vmovsd/ss |
| 160 | case 0x11: // vmovsd/ss |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 161 | case 0xb6: // movzx |
| 162 | case 0xb7: |
| 163 | case 0xbe: // movsx |
| 164 | case 0xbf: |
| 165 | modrm = *pc++; |
| 166 | has_modrm = true; |
| 167 | break; |
| 168 | default: |
| 169 | unhandled_instruction = true; |
| 170 | break; |
| 171 | } |
| 172 | } else { |
| 173 | switch (opcode) { |
Serguei Katkov | e2d596e | 2014-09-08 17:48:25 +0700 | [diff] [blame] | 174 | case 0x88: // mov byte |
| 175 | case 0x89: // mov |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 176 | case 0x8b: |
| 177 | case 0x38: // cmp with memory. |
| 178 | case 0x39: |
| 179 | case 0x3a: |
| 180 | case 0x3b: |
| 181 | case 0x3c: |
| 182 | case 0x3d: |
| 183 | case 0x85: // test. |
| 184 | modrm = *pc++; |
| 185 | has_modrm = true; |
| 186 | break; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 187 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 188 | case 0x80: // group 1, byte immediate. |
| 189 | case 0x83: |
Mark Mendell | a9f36ee | 2014-10-01 08:02:43 -0400 | [diff] [blame] | 190 | case 0xc6: |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 191 | modrm = *pc++; |
| 192 | has_modrm = true; |
| 193 | immediate_size = 1; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 194 | break; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 195 | |
| 196 | case 0x81: // group 1, word immediate. |
Mark Mendell | 40741f3 | 2015-04-20 22:10:34 -0400 | [diff] [blame] | 197 | case 0xc7: // mov |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 198 | modrm = *pc++; |
| 199 | has_modrm = true; |
Mark Mendell | 5daf8e1 | 2014-09-25 15:13:39 -0400 | [diff] [blame] | 200 | immediate_size = operand_size_prefix ? 2 : 4; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 201 | break; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 202 | |
| 203 | default: |
| 204 | unhandled_instruction = true; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 209 | if (unhandled_instruction) { |
| 210 | VLOG(signals) << "Unhandled x86 instruction with opcode " << static_cast<int>(opcode); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | if (has_modrm) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 215 | uint8_t mod = (modrm >> 6) & 3U /* 0b11 */; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 216 | |
| 217 | // Check for SIB. |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 218 | if (mod != 3U /* 0b11 */ && (modrm & 7U /* 0b111 */) == 4) { |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 219 | ++pc; // SIB |
| 220 | } |
| 221 | |
| 222 | switch (mod) { |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 223 | case 0U /* 0b00 */: break; |
| 224 | case 1U /* 0b01 */: displacement_size = 1; break; |
| 225 | case 2U /* 0b10 */: displacement_size = 4; break; |
| 226 | case 3U /* 0b11 */: |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Skip displacement and immediate. |
| 232 | pc += displacement_size + immediate_size; |
| 233 | |
| 234 | VLOG(signals) << "x86 instruction length calculated as " << (pc - startpc); |
| 235 | return pc - startpc; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 238 | void FaultManager::HandleNestedSignal(int, siginfo_t*, void* context) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 239 | // For the Intel architectures we need to go to an assembly language |
| 240 | // stub. This is because the 32 bit call to longjmp is much different |
| 241 | // from the 64 bit ABI call and pushing things onto the stack inside this |
| 242 | // handler was unwieldy and ugly. The use of the stub means we can keep |
| 243 | // this code the same for both 32 and 64 bit. |
| 244 | |
| 245 | Thread* self = Thread::Current(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 246 | CHECK(self != nullptr); // This will cause a SIGABRT if self is null. |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 247 | |
| 248 | struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); |
| 249 | uc->CTX_JMP_BUF = reinterpret_cast<uintptr_t>(*self->GetNestedSignalState()); |
| 250 | uc->CTX_EIP = reinterpret_cast<uintptr_t>(art_nested_signal_return); |
| 251 | } |
| 252 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 253 | void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 254 | ArtMethod** out_method, |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 255 | uintptr_t* out_return_pc, uintptr_t* out_sp) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 256 | struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); |
| 257 | *out_sp = static_cast<uintptr_t>(uc->CTX_ESP); |
| 258 | VLOG(signals) << "sp: " << std::hex << *out_sp; |
| 259 | if (*out_sp == 0) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | // In the case of a stack overflow, the stack is not valid and we can't |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 264 | // get the method from the top of the stack. However it's in EAX(x86)/RDI(x86_64). |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 265 | uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); |
| 266 | uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 267 | #if defined(__x86_64__) |
| 268 | reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86_64)); |
| 269 | #else |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 270 | reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kX86)); |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 271 | #endif |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 272 | if (overflow_addr == fault_addr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 273 | *out_method = reinterpret_cast<ArtMethod*>(uc->CTX_METHOD); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 274 | } else { |
| 275 | // The method is at the top of the stack. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 276 | *out_method = *reinterpret_cast<ArtMethod**>(*out_sp); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP); |
| 280 | VLOG(signals) << HexDump(pc, 32, true, "PC "); |
| 281 | |
Andreas Gampe | 9415886 | 2015-04-03 02:17:06 -0700 | [diff] [blame] | 282 | if (pc == nullptr) { |
| 283 | // Somebody jumped to 0x0. Definitely not ours, and will definitely segfault below. |
| 284 | *out_method = nullptr; |
| 285 | return; |
| 286 | } |
| 287 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 288 | uint32_t instr_size = GetInstructionSize(pc); |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 289 | if (instr_size == 0) { |
| 290 | // Unknown instruction, tell caller it's not ours. |
| 291 | *out_method = nullptr; |
| 292 | return; |
| 293 | } |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 294 | *out_return_pc = reinterpret_cast<uintptr_t>(pc + instr_size); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 297 | bool NullPointerHandler::Action(int, siginfo_t* sig, void* context) { |
| 298 | if (!IsValidImplicitCheck(sig)) { |
| 299 | return false; |
| 300 | } |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 301 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 302 | uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP); |
| 303 | uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP); |
| 304 | |
| 305 | uint32_t instr_size = GetInstructionSize(pc); |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 306 | if (instr_size == 0) { |
| 307 | // Unknown instruction, can't really happen. |
| 308 | return false; |
| 309 | } |
| 310 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 311 | // We need to arrange for the signal handler to return to the null pointer |
| 312 | // exception generator. The return address must be the address of the |
| 313 | // next instruction (this instruction + instruction size). The return address |
| 314 | // is on the stack at the top address of the current frame. |
| 315 | |
| 316 | // Push the return address onto the stack. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 317 | uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + instr_size); |
| 318 | uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t)); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 319 | *next_sp = retaddr; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 320 | uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 321 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 322 | uc->CTX_EIP = reinterpret_cast<uintptr_t>( |
| 323 | EXT_SYM(art_quick_throw_null_pointer_exception_from_signal)); |
| 324 | // Pass the faulting address as the first argument of |
| 325 | // art_quick_throw_null_pointer_exception_from_signal. |
| 326 | #if defined(__x86_64__) |
| 327 | uc->CTX_RDI = reinterpret_cast<uintptr_t>(sig->si_addr); |
| 328 | #else |
| 329 | uc->CTX_EAX = reinterpret_cast<uintptr_t>(sig->si_addr); |
| 330 | #endif |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 331 | VLOG(signals) << "Generating null pointer exception"; |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | // A suspend check is done using the following instruction sequence: |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 336 | // (x86) |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 337 | // 0xf720f1df: 648B058C000000 mov eax, fs:[0x8c] ; suspend_trigger |
| 338 | // .. some intervening instructions. |
| 339 | // 0xf720f1e6: 8500 test eax, [eax] |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 340 | // (x86_64) |
| 341 | // 0x7f579de45d9e: 65488B0425A8000000 movq rax, gs:[0xa8] ; suspend_trigger |
| 342 | // .. some intervening instructions. |
| 343 | // 0x7f579de45da7: 8500 test eax, [eax] |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 344 | |
| 345 | // The offset from fs is Thread::ThreadSuspendTriggerOffset(). |
| 346 | // To check for a suspend check, we examine the instructions that caused |
| 347 | // the fault. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 348 | bool SuspensionHandler::Action(int, siginfo_t*, void* context) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 349 | // These are the instructions to check for. The first one is the mov eax, fs:[xxx] |
| 350 | // where xxx is the offset of the suspend trigger. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 351 | uint32_t trigger = Thread::ThreadSuspendTriggerOffset<kRuntimePointerSize>().Int32Value(); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 352 | |
| 353 | VLOG(signals) << "Checking for suspension point"; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 354 | #if defined(__x86_64__) |
| 355 | uint8_t checkinst1[] = {0x65, 0x48, 0x8b, 0x04, 0x25, static_cast<uint8_t>(trigger & 0xff), |
| 356 | static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0}; |
| 357 | #else |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 358 | uint8_t checkinst1[] = {0x64, 0x8b, 0x05, static_cast<uint8_t>(trigger & 0xff), |
| 359 | static_cast<uint8_t>((trigger >> 8) & 0xff), 0, 0}; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 360 | #endif |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 361 | uint8_t checkinst2[] = {0x85, 0x00}; |
| 362 | |
| 363 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 364 | uint8_t* pc = reinterpret_cast<uint8_t*>(uc->CTX_EIP); |
| 365 | uint8_t* sp = reinterpret_cast<uint8_t*>(uc->CTX_ESP); |
| 366 | |
| 367 | if (pc[0] != checkinst2[0] || pc[1] != checkinst2[1]) { |
| 368 | // Second instruction is not correct (test eax,[eax]). |
| 369 | VLOG(signals) << "Not a suspension point"; |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // The first instruction can a little bit up the stream due to load hoisting |
| 374 | // in the compiler. |
| 375 | uint8_t* limit = pc - 100; // Compiler will hoist to a max of 20 instructions. |
| 376 | uint8_t* ptr = pc - sizeof(checkinst1); |
| 377 | bool found = false; |
| 378 | while (ptr > limit) { |
| 379 | if (memcmp(ptr, checkinst1, sizeof(checkinst1)) == 0) { |
| 380 | found = true; |
| 381 | break; |
| 382 | } |
| 383 | ptr -= 1; |
| 384 | } |
| 385 | |
| 386 | if (found) { |
| 387 | VLOG(signals) << "suspend check match"; |
| 388 | |
| 389 | // We need to arrange for the signal handler to return to the null pointer |
| 390 | // exception generator. The return address must be the address of the |
| 391 | // next instruction (this instruction + 2). The return address |
| 392 | // is on the stack at the top address of the current frame. |
| 393 | |
| 394 | // Push the return address onto the stack. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 395 | uintptr_t retaddr = reinterpret_cast<uintptr_t>(pc + 2); |
| 396 | uintptr_t* next_sp = reinterpret_cast<uintptr_t*>(sp - sizeof(uintptr_t)); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 397 | *next_sp = retaddr; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 398 | uc->CTX_ESP = reinterpret_cast<uintptr_t>(next_sp); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 399 | |
Dan Albert | 85fa796 | 2014-08-10 10:14:59 -0700 | [diff] [blame] | 400 | uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_test_suspend)); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 401 | |
| 402 | // Now remove the suspend trigger that caused this fault. |
| 403 | Thread::Current()->RemoveSuspendTrigger(); |
| 404 | VLOG(signals) << "removed suspend trigger invoking test suspend"; |
| 405 | return true; |
| 406 | } |
| 407 | VLOG(signals) << "Not a suspend check match, first instruction mismatch"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 411 | // The stack overflow check is done using the following instruction: |
| 412 | // test eax, [esp+ -xxx] |
| 413 | // where 'xxx' is the size of the overflow area. |
| 414 | // |
| 415 | // This is done before any frame is established in the method. The return |
| 416 | // address for the previous method is on the stack at ESP. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 417 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 418 | bool StackOverflowHandler::Action(int, siginfo_t* info, void* context) { |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 419 | struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); |
| 420 | uintptr_t sp = static_cast<uintptr_t>(uc->CTX_ESP); |
| 421 | |
| 422 | uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); |
| 423 | VLOG(signals) << "fault_addr: " << std::hex << fault_addr; |
| 424 | VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << |
| 425 | ", fault_addr: " << fault_addr; |
| 426 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 427 | #if defined(__x86_64__) |
| 428 | uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86_64); |
| 429 | #else |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 430 | uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kX86); |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 431 | #endif |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 432 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 433 | // Check that the fault address is the value expected for a stack overflow. |
| 434 | if (fault_addr != overflow_addr) { |
| 435 | VLOG(signals) << "Not a stack overflow"; |
| 436 | return false; |
| 437 | } |
| 438 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 439 | VLOG(signals) << "Stack overflow found"; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 440 | |
| 441 | // Since the compiler puts the implicit overflow |
| 442 | // check before the callee save instructions, the SP is already pointing to |
| 443 | // the previous frame. |
| 444 | |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 445 | // Now arrange for the signal handler to return to art_quick_throw_stack_overflow. |
Andreas Gampe | eb0ab9e | 2014-08-14 11:15:22 -0700 | [diff] [blame] | 446 | uc->CTX_EIP = reinterpret_cast<uintptr_t>(EXT_SYM(art_quick_throw_stack_overflow)); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 447 | |
| 448 | return true; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 449 | } |
| 450 | } // namespace art |