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