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