1 //===--------------------------- Unwind-EHABI.cpp -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 // 9 // Implements ARM zero-cost C++ exceptions 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "Unwind-EHABI.h" 14 15 #if LIBCXXABI_ARM_EHABI 16 17 #include <stdbool.h> 18 #include <stdint.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include <type_traits> 24 25 #include "config.h" 26 #include "libunwind.h" 27 #include "libunwind_ext.h" 28 #include "unwind.h" 29 #include "../private_typeinfo.h" 30 31 namespace { 32 33 // Strange order: take words in order, but inside word, take from most to least 34 // signinficant byte. 35 uint8_t getByte(const uint32_t* data, size_t offset) { 36 const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data); 37 return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))]; 38 } 39 40 const char* getNextWord(const char* data, uint32_t* out) { 41 *out = *reinterpret_cast<const uint32_t*>(data); 42 return data + 4; 43 } 44 45 const char* getNextNibble(const char* data, uint32_t* out) { 46 *out = *reinterpret_cast<const uint16_t*>(data); 47 return data + 2; 48 } 49 50 struct Descriptor { 51 // See # 9.2 52 typedef enum { 53 SU16 = 0, // Short descriptor, 16-bit entries 54 LU16 = 1, // Long descriptor, 16-bit entries 55 LU32 = 3, // Long descriptor, 32-bit entries 56 RESERVED0 = 4, RESERVED1 = 5, RESERVED2 = 6, RESERVED3 = 7, 57 RESERVED4 = 8, RESERVED5 = 9, RESERVED6 = 10, RESERVED7 = 11, 58 RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15 59 } Format; 60 61 // See # 9.2 62 typedef enum { 63 CLEANUP = 0x0, 64 FUNC = 0x1, 65 CATCH = 0x2, 66 INVALID = 0x4 67 } Kind; 68 }; 69 70 _Unwind_Reason_Code ProcessDescriptors( 71 _Unwind_State state, 72 _Unwind_Control_Block* ucbp, 73 struct _Unwind_Context* context, 74 Descriptor::Format format, 75 const char* descriptorStart, 76 uint32_t flags) { 77 78 // EHT is inlined in the index using compact form. No descriptors. #5 79 if (flags & 0x1) 80 return _URC_CONTINUE_UNWIND; 81 82 // TODO: We should check the state here, and determine whether we need to 83 // perform phase1 or phase2 unwinding. 84 (void)state; 85 86 const char* descriptor = descriptorStart; 87 uint32_t descriptorWord; 88 getNextWord(descriptor, &descriptorWord); 89 while (descriptorWord) { 90 // Read descriptor based on # 9.2. 91 uint32_t length; 92 uint32_t offset; 93 switch (format) { 94 case Descriptor::LU32: 95 descriptor = getNextWord(descriptor, &length); 96 descriptor = getNextWord(descriptor, &offset); 97 case Descriptor::LU16: 98 descriptor = getNextNibble(descriptor, &length); 99 descriptor = getNextNibble(descriptor, &offset); 100 default: 101 assert(false); 102 return _URC_FAILURE; 103 } 104 105 // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value. 106 Descriptor::Kind kind = 107 static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1)); 108 109 // Clear off flag from last bit. 110 length &= ~1u; 111 offset &= ~1u; 112 uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset; 113 uintptr_t scopeEnd = scopeStart + length; 114 uintptr_t pc = _Unwind_GetIP(context); 115 bool isInScope = (scopeStart <= pc) && (pc < scopeEnd); 116 117 switch (kind) { 118 case Descriptor::CLEANUP: { 119 // TODO(ajwong): Handle cleanup descriptors. 120 break; 121 } 122 case Descriptor::FUNC: { 123 // TODO(ajwong): Handle function descriptors. 124 break; 125 } 126 case Descriptor::CATCH: { 127 // Catch descriptors require gobbling one more word. 128 uint32_t landing_pad; 129 descriptor = getNextWord(descriptor, &landing_pad); 130 131 if (isInScope) { 132 // TODO(ajwong): This is only phase1 compatible logic. Implement 133 // phase2. 134 landing_pad = signExtendPrel31(landing_pad & ~0x80000000); 135 if (landing_pad == 0xffffffff) { 136 return _URC_HANDLER_FOUND; 137 } else if (landing_pad == 0xfffffffe) { 138 return _URC_FAILURE; 139 } else { 140 /* 141 bool is_reference_type = landing_pad & 0x80000000; 142 void* matched_object; 143 if (__cxxabiv1::__cxa_type_match( 144 ucbp, reinterpret_cast<const std::type_info *>(landing_pad), 145 is_reference_type, 146 &matched_object) != __cxxabiv1::ctm_failed) 147 return _URC_HANDLER_FOUND; 148 */ 149 _LIBUNWIND_ABORT("Type matching not implemented"); 150 } 151 } 152 break; 153 } 154 default: 155 _LIBUNWIND_ABORT("Invalid descriptor kind found."); 156 } 157 158 getNextWord(descriptor, &descriptorWord); 159 } 160 161 return _URC_CONTINUE_UNWIND; 162 } 163 164 static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state, 165 _Unwind_Control_Block* ucbp, 166 struct _Unwind_Context* context) { 167 // Read the compact model EHT entry's header # 6.3 168 const uint32_t* unwindingData = ucbp->pr_cache.ehtp; 169 assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry"); 170 Descriptor::Format format = 171 static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24); 172 size_t len = 0; 173 size_t off = 0; 174 unwindingData = decode_eht_entry(unwindingData, &off, &len); 175 if (unwindingData == nullptr) { 176 return _URC_FAILURE; 177 } 178 179 // Handle descriptors before unwinding so they are processed in the context 180 // of the correct stack frame. 181 _Unwind_Reason_Code result = 182 ProcessDescriptors( 183 state, ucbp, context, format, 184 reinterpret_cast<const char*>(ucbp->pr_cache.ehtp) + len, 185 ucbp->pr_cache.additional); 186 187 if (result != _URC_CONTINUE_UNWIND) 188 return result; 189 190 return _Unwind_VRS_Interpret(context, unwindingData, off, len); 191 } 192 193 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE / 194 // _UVRSD_UINT32. 195 uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) { 196 return ((1U << (count_minus_one + 1)) - 1) << start; 197 } 198 199 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP / 200 // _UVRSD_DOUBLE. 201 uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) { 202 return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1); 203 } 204 205 } // end anonymous namespace 206 207 /** 208 * Decodes an EHT entry. 209 * 210 * @param data Pointer to EHT. 211 * @param[out] off Offset from return value (in bytes) to begin interpretation. 212 * @param[out] len Number of bytes in unwind code. 213 * @return Pointer to beginning of unwind code. 214 */ 215 extern "C" const uint32_t* 216 decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) { 217 assert((*data & 0x80000000) != 0 && 218 "decode_eht_entry() does not support user-defined personality"); 219 220 // 6.3: ARM Compact Model 221 // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded 222 // by format: 223 Descriptor::Format format = 224 static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24); 225 switch (format) { 226 case Descriptor::SU16: 227 *len = 4; 228 *off = 1; 229 break; 230 case Descriptor::LU16: 231 case Descriptor::LU32: 232 *len = 4 + 4 * ((*data & 0x00ff0000) >> 16); 233 *off = 2; 234 break; 235 default: 236 return nullptr; 237 } 238 return data; 239 } 240 241 _Unwind_Reason_Code _Unwind_VRS_Interpret( 242 _Unwind_Context* context, 243 const uint32_t* data, 244 size_t offset, 245 size_t len) { 246 bool wrotePC = false; 247 bool finish = false; 248 while (offset < len && !finish) { 249 uint8_t byte = getByte(data, offset++); 250 if ((byte & 0x80) == 0) { 251 uint32_t sp; 252 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp); 253 if (byte & 0x40) 254 sp -= (((uint32_t)byte & 0x3f) << 2) + 4; 255 else 256 sp += ((uint32_t)byte << 2) + 4; 257 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp); 258 } else { 259 switch (byte & 0xf0) { 260 case 0x80: { 261 if (offset >= len) 262 return _URC_FAILURE; 263 uint32_t registers = 264 (((uint32_t)byte & 0x0f) << 12) | 265 (((uint32_t)getByte(data, offset++)) << 4); 266 if (!registers) 267 return _URC_FAILURE; 268 if (registers & (1 << 15)) 269 wrotePC = true; 270 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32); 271 break; 272 } 273 case 0x90: { 274 uint8_t reg = byte & 0x0f; 275 if (reg == 13 || reg == 15) 276 return _URC_FAILURE; 277 uint32_t sp; 278 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg, 279 _UVRSD_UINT32, &sp); 280 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 281 &sp); 282 break; 283 } 284 case 0xa0: { 285 uint32_t registers = RegisterMask(4, byte & 0x07); 286 if (byte & 0x08) 287 registers |= 1 << 14; 288 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32); 289 break; 290 } 291 case 0xb0: { 292 switch (byte) { 293 case 0xb0: 294 finish = true; 295 break; 296 case 0xb1: { 297 if (offset >= len) 298 return _URC_FAILURE; 299 uint8_t registers = getByte(data, offset++); 300 if (registers & 0xf0 || !registers) 301 return _URC_FAILURE; 302 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32); 303 break; 304 } 305 case 0xb2: { 306 uint32_t addend = 0; 307 uint32_t shift = 0; 308 // This decodes a uleb128 value. 309 while (true) { 310 if (offset >= len) 311 return _URC_FAILURE; 312 uint32_t v = getByte(data, offset++); 313 addend |= (v & 0x7f) << shift; 314 if ((v & 0x80) == 0) 315 break; 316 shift += 7; 317 } 318 uint32_t sp; 319 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 320 &sp); 321 sp += 0x204 + (addend << 2); 322 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 323 &sp); 324 break; 325 } 326 case 0xb3: { 327 uint8_t v = getByte(data, offset++); 328 _Unwind_VRS_Pop(context, _UVRSC_VFP, 329 RegisterRange(static_cast<uint8_t>(v >> 4), 330 v & 0x0f), _UVRSD_VFPX); 331 break; 332 } 333 case 0xb4: 334 case 0xb5: 335 case 0xb6: 336 case 0xb7: 337 return _URC_FAILURE; 338 default: 339 _Unwind_VRS_Pop(context, _UVRSC_VFP, 340 RegisterRange(8, byte & 0x07), _UVRSD_VFPX); 341 break; 342 } 343 break; 344 } 345 case 0xc0: { 346 switch (byte) { 347 case 0xc0: 348 case 0xc1: 349 case 0xc2: 350 case 0xc3: 351 case 0xc4: 352 case 0xc5: 353 _Unwind_VRS_Pop(context, _UVRSC_WMMXD, 354 RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE); 355 break; 356 case 0xc6: { 357 uint8_t v = getByte(data, offset++); 358 uint8_t start = static_cast<uint8_t>(v >> 4); 359 uint8_t count_minus_one = v & 0xf; 360 if (start + count_minus_one >= 16) 361 return _URC_FAILURE; 362 _Unwind_VRS_Pop(context, _UVRSC_WMMXD, 363 RegisterRange(start, count_minus_one), 364 _UVRSD_DOUBLE); 365 break; 366 } 367 case 0xc7: { 368 uint8_t v = getByte(data, offset++); 369 if (!v || v & 0xf0) 370 return _URC_FAILURE; 371 _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE); 372 break; 373 } 374 case 0xc8: 375 case 0xc9: { 376 uint8_t v = getByte(data, offset++); 377 uint8_t start = 378 static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4)); 379 uint8_t count_minus_one = v & 0xf; 380 if (start + count_minus_one >= 32) 381 return _URC_FAILURE; 382 _Unwind_VRS_Pop(context, _UVRSC_VFP, 383 RegisterRange(start, count_minus_one), 384 _UVRSD_DOUBLE); 385 break; 386 } 387 default: 388 return _URC_FAILURE; 389 } 390 break; 391 } 392 case 0xd0: { 393 if (byte & 0x08) 394 return _URC_FAILURE; 395 _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7), 396 _UVRSD_DOUBLE); 397 break; 398 } 399 default: 400 return _URC_FAILURE; 401 } 402 } 403 } 404 if (!wrotePC) { 405 uint32_t lr; 406 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr); 407 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr); 408 } 409 return _URC_CONTINUE_UNWIND; 410 } 411 412 extern "C" _Unwind_Reason_Code __aeabi_unwind_cpp_pr0( 413 _Unwind_State state, 414 _Unwind_Control_Block *ucbp, 415 _Unwind_Context *context) { 416 return unwindOneFrame(state, ucbp, context); 417 } 418 419 extern "C" _Unwind_Reason_Code __aeabi_unwind_cpp_pr1( 420 _Unwind_State state, 421 _Unwind_Control_Block *ucbp, 422 _Unwind_Context *context) { 423 return unwindOneFrame(state, ucbp, context); 424 } 425 426 extern "C" _Unwind_Reason_Code __aeabi_unwind_cpp_pr2( 427 _Unwind_State state, 428 _Unwind_Control_Block *ucbp, 429 _Unwind_Context *context) { 430 return unwindOneFrame(state, ucbp, context); 431 } 432 433 static _Unwind_Reason_Code 434 unwind_phase1(unw_context_t *uc, _Unwind_Exception *exception_object) { 435 // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during 436 // phase 1 and then restoring it to the "primary VRS" for phase 2. The 437 // effect is phase 2 doesn't see any of the VRS manipulations from phase 1. 438 // In this implementation, the phases don't share the VRS backing store. 439 // Instead, they are passed the original |uc| and they create a new VRS 440 // from scratch thus achieving the same effect. 441 unw_cursor_t cursor1; 442 unw_init_local(&cursor1, uc); 443 444 // Walk each frame looking for a place to stop. 445 for (bool handlerNotFound = true; handlerNotFound;) { 446 447 // Ask libuwind to get next frame (skip over first which is 448 // _Unwind_RaiseException). 449 int stepResult = unw_step(&cursor1); 450 if (stepResult == 0) { 451 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step() reached " 452 "bottom => _URC_END_OF_STACK\n", 453 static_cast<void *>(exception_object)); 454 return _URC_END_OF_STACK; 455 } else if (stepResult < 0) { 456 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step failed => " 457 "_URC_FATAL_PHASE1_ERROR\n", 458 static_cast<void *>(exception_object)); 459 return _URC_FATAL_PHASE1_ERROR; 460 } 461 462 // See if frame has code to run (has personality routine). 463 unw_proc_info_t frameInfo; 464 if (unw_get_proc_info(&cursor1, &frameInfo) != UNW_ESUCCESS) { 465 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info " 466 "failed => _URC_FATAL_PHASE1_ERROR\n", 467 static_cast<void *>(exception_object)); 468 return _URC_FATAL_PHASE1_ERROR; 469 } 470 471 // When tracing, print state information. 472 if (_LIBUNWIND_TRACING_UNWINDING) { 473 char functionBuf[512]; 474 const char *functionName = functionBuf; 475 unw_word_t offset; 476 if ((unw_get_proc_name(&cursor1, functionBuf, sizeof(functionBuf), 477 &offset) != UNW_ESUCCESS) || 478 (frameInfo.start_ip + offset > frameInfo.end_ip)) 479 functionName = ".anonymous."; 480 unw_word_t pc; 481 unw_get_reg(&cursor1, UNW_REG_IP, &pc); 482 _LIBUNWIND_TRACE_UNWINDING( 483 "unwind_phase1(ex_ojb=%p): pc=0x%llX, start_ip=0x%llX, func=%s, " 484 "lsda=0x%llX, personality=0x%llX\n", 485 static_cast<void *>(exception_object), (long long)pc, 486 (long long)frameInfo.start_ip, functionName, 487 (long long)frameInfo.lsda, (long long)frameInfo.handler); 488 } 489 490 // If there is a personality routine, ask it if it will want to stop at 491 // this frame. 492 if (frameInfo.handler != 0) { 493 __personality_routine p = 494 (__personality_routine)(long)(frameInfo.handler); 495 _LIBUNWIND_TRACE_UNWINDING( 496 "unwind_phase1(ex_ojb=%p): calling personality function %p\n", 497 static_cast<void *>(exception_object), 498 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p))); 499 struct _Unwind_Context *context = (struct _Unwind_Context *)(&cursor1); 500 exception_object->pr_cache.fnstart = frameInfo.start_ip; 501 exception_object->pr_cache.ehtp = 502 (_Unwind_EHT_Header *)frameInfo.unwind_info; 503 exception_object->pr_cache.additional = frameInfo.flags; 504 _Unwind_Reason_Code personalityResult = 505 (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context); 506 _LIBUNWIND_TRACE_UNWINDING( 507 "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p " 508 "additional %x\n", 509 static_cast<void *>(exception_object), personalityResult, 510 exception_object->pr_cache.fnstart, 511 static_cast<void *>(exception_object->pr_cache.ehtp), 512 exception_object->pr_cache.additional); 513 switch (personalityResult) { 514 case _URC_HANDLER_FOUND: 515 // found a catch clause or locals that need destructing in this frame 516 // stop search and remember stack pointer at the frame 517 handlerNotFound = false; 518 // p should have initialized barrier_cache. EHABI #7.3.5 519 _LIBUNWIND_TRACE_UNWINDING( 520 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND \n", 521 static_cast<void *>(exception_object)); 522 return _URC_NO_REASON; 523 524 case _URC_CONTINUE_UNWIND: 525 _LIBUNWIND_TRACE_UNWINDING( 526 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND\n", 527 static_cast<void *>(exception_object)); 528 // continue unwinding 529 break; 530 531 // EHABI #7.3.3 532 case _URC_FAILURE: 533 return _URC_FAILURE; 534 535 default: 536 // something went wrong 537 _LIBUNWIND_TRACE_UNWINDING( 538 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR\n", 539 static_cast<void *>(exception_object)); 540 return _URC_FATAL_PHASE1_ERROR; 541 } 542 } 543 } 544 return _URC_NO_REASON; 545 } 546 547 static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, 548 _Unwind_Exception *exception_object, 549 bool resume) { 550 // See comment at the start of unwind_phase1 regarding VRS integrity. 551 unw_cursor_t cursor2; 552 unw_init_local(&cursor2, uc); 553 554 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)\n", 555 static_cast<void *>(exception_object)); 556 int frame_count = 0; 557 558 // Walk each frame until we reach where search phase said to stop. 559 while (true) { 560 // Ask libuwind to get next frame (skip over first which is 561 // _Unwind_RaiseException or _Unwind_Resume). 562 // 563 // Resume only ever makes sense for 1 frame. 564 _Unwind_State state = 565 resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING; 566 if (resume && frame_count == 1) { 567 // On a resume, first unwind the _Unwind_Resume() frame. The next frame 568 // is now the landing pad for the cleanup from a previous execution of 569 // phase2. To continue unwindingly correctly, replace VRS[15] with the 570 // IP of the frame that the previous run of phase2 installed the context 571 // for. After this, continue unwinding as if normal. 572 // 573 // See #7.4.6 for details. 574 unw_set_reg(&cursor2, UNW_REG_IP, 575 exception_object->unwinder_cache.reserved2); 576 resume = false; 577 } 578 579 int stepResult = unw_step(&cursor2); 580 if (stepResult == 0) { 581 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached " 582 "bottom => _URC_END_OF_STACK\n", 583 static_cast<void *>(exception_object)); 584 return _URC_END_OF_STACK; 585 } else if (stepResult < 0) { 586 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step failed => " 587 "_URC_FATAL_PHASE1_ERROR\n", 588 static_cast<void *>(exception_object)); 589 return _URC_FATAL_PHASE2_ERROR; 590 } 591 592 // Get info about this frame. 593 unw_word_t sp; 594 unw_proc_info_t frameInfo; 595 unw_get_reg(&cursor2, UNW_REG_SP, &sp); 596 if (unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) { 597 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info " 598 "failed => _URC_FATAL_PHASE1_ERROR\n", 599 static_cast<void *>(exception_object)); 600 return _URC_FATAL_PHASE2_ERROR; 601 } 602 603 // When tracing, print state information. 604 if (_LIBUNWIND_TRACING_UNWINDING) { 605 char functionBuf[512]; 606 const char *functionName = functionBuf; 607 unw_word_t offset; 608 if ((unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf), 609 &offset) != UNW_ESUCCESS) || 610 (frameInfo.start_ip + offset > frameInfo.end_ip)) 611 functionName = ".anonymous."; 612 _LIBUNWIND_TRACE_UNWINDING( 613 "unwind_phase2(ex_ojb=%p): start_ip=0x%llX, func=%s, sp=0x%llX, " 614 "lsda=0x%llX, personality=0x%llX\n", 615 static_cast<void *>(exception_object), (long long)frameInfo.start_ip, 616 functionName, (long long)sp, (long long)frameInfo.lsda, 617 (long long)frameInfo.handler); 618 } 619 620 // If there is a personality routine, tell it we are unwinding. 621 if (frameInfo.handler != 0) { 622 __personality_routine p = 623 (__personality_routine)(long)(frameInfo.handler); 624 struct _Unwind_Context *context = (struct _Unwind_Context *)(&cursor2); 625 // EHABI #7.2 626 exception_object->pr_cache.fnstart = frameInfo.start_ip; 627 exception_object->pr_cache.ehtp = 628 (_Unwind_EHT_Header *)frameInfo.unwind_info; 629 exception_object->pr_cache.additional = frameInfo.flags; 630 _Unwind_Reason_Code personalityResult = 631 (*p)(state, exception_object, context); 632 switch (personalityResult) { 633 case _URC_CONTINUE_UNWIND: 634 // Continue unwinding 635 _LIBUNWIND_TRACE_UNWINDING( 636 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND\n", 637 static_cast<void *>(exception_object)); 638 // EHABI #7.2 639 if (sp == exception_object->barrier_cache.sp) { 640 // Phase 1 said we would stop at this frame, but we did not... 641 _LIBUNWIND_ABORT("during phase1 personality function said it would " 642 "stop here, but now in phase2 it did not stop here"); 643 } 644 break; 645 case _URC_INSTALL_CONTEXT: 646 _LIBUNWIND_TRACE_UNWINDING( 647 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT\n", 648 static_cast<void *>(exception_object)); 649 // Personality routine says to transfer control to landing pad. 650 // We may get control back if landing pad calls _Unwind_Resume(). 651 if (_LIBUNWIND_TRACING_UNWINDING) { 652 unw_word_t pc; 653 unw_get_reg(&cursor2, UNW_REG_IP, &pc); 654 unw_get_reg(&cursor2, UNW_REG_SP, &sp); 655 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering " 656 "user code with ip=0x%llX, sp=0x%llX\n", 657 static_cast<void *>(exception_object), 658 (long long)pc, (long long)sp); 659 } 660 661 { 662 // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume 663 // is called back, to find this same frame. 664 unw_word_t pc; 665 unw_get_reg(&cursor2, UNW_REG_IP, &pc); 666 exception_object->unwinder_cache.reserved2 = (uint32_t)pc; 667 } 668 unw_resume(&cursor2); 669 // unw_resume() only returns if there was an error. 670 return _URC_FATAL_PHASE2_ERROR; 671 672 // # EHABI #7.4.3 673 case _URC_FAILURE: 674 abort(); 675 676 default: 677 // Personality routine returned an unknown result code. 678 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d", 679 personalityResult); 680 return _URC_FATAL_PHASE2_ERROR; 681 } 682 } 683 frame_count++; 684 } 685 686 // Clean up phase did not resume at the frame that the search phase 687 // said it would... 688 return _URC_FATAL_PHASE2_ERROR; 689 } 690 691 /// Called by __cxa_throw. Only returns if there is a fatal error. 692 _LIBUNWIND_EXPORT _Unwind_Reason_Code 693 _Unwind_RaiseException(_Unwind_Exception *exception_object) { 694 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)\n", 695 static_cast<void *>(exception_object)); 696 unw_context_t uc; 697 unw_getcontext(&uc); 698 699 // This field for is for compatibility with GCC to say this isn't a forced 700 // unwind. EHABI #7.2 701 exception_object->unwinder_cache.reserved1 = 0; 702 703 // phase 1: the search phase 704 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, exception_object); 705 if (phase1 != _URC_NO_REASON) 706 return phase1; 707 708 // phase 2: the clean up phase 709 return unwind_phase2(&uc, exception_object, false); 710 } 711 712 _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) { 713 // This is to be called when exception handling completes to give us a chance 714 // to perform any housekeeping. EHABI #7.2. But we have nothing to do here. 715 (void)exception_object; 716 } 717 718 /// When _Unwind_RaiseException() is in phase2, it hands control 719 /// to the personality function at each frame. The personality 720 /// may force a jump to a landing pad in that function, the landing 721 /// pad code may then call _Unwind_Resume() to continue with the 722 /// unwinding. Note: the call to _Unwind_Resume() is from compiler 723 /// geneated user code. All other _Unwind_* routines are called 724 /// by the C++ runtime __cxa_* routines. 725 /// 726 /// Note: re-throwing an exception (as opposed to continuing the unwind) 727 /// is implemented by having the code call __cxa_rethrow() which 728 /// in turn calls _Unwind_Resume_or_Rethrow(). 729 _LIBUNWIND_EXPORT void 730 _Unwind_Resume(_Unwind_Exception *exception_object) { 731 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)\n", 732 static_cast<void *>(exception_object)); 733 unw_context_t uc; 734 unw_getcontext(&uc); 735 736 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0, 737 // which is in the same position as private_1 below. 738 // TODO(ajwong): Who wronte the above? Why is it true? 739 unwind_phase2(&uc, exception_object, true); 740 741 // Clients assume _Unwind_Resume() does not return, so all we can do is abort. 742 _LIBUNWIND_ABORT("_Unwind_Resume() can't return"); 743 } 744 745 /// Called by personality handler during phase 2 to get LSDA for current frame. 746 _LIBUNWIND_EXPORT uintptr_t 747 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { 748 unw_cursor_t *cursor = (unw_cursor_t *)context; 749 unw_proc_info_t frameInfo; 750 uintptr_t result = 0; 751 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) 752 result = (uintptr_t)frameInfo.lsda; 753 _LIBUNWIND_TRACE_API( 754 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx\n", 755 static_cast<void *>(context), (long long)result); 756 if (result != 0) { 757 if (*((uint8_t *)result) != 0xFF) 758 _LIBUNWIND_DEBUG_LOG("lsda at 0x%llx does not start with 0xFF\n", 759 (long long)result); 760 } 761 return result; 762 } 763 764 static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation, 765 void* valuep) { 766 uint64_t value = 0; 767 switch (representation) { 768 case _UVRSD_UINT32: 769 case _UVRSD_FLOAT: 770 memcpy(&value, valuep, sizeof(uint32_t)); 771 break; 772 773 case _UVRSD_VFPX: 774 case _UVRSD_UINT64: 775 case _UVRSD_DOUBLE: 776 memcpy(&value, valuep, sizeof(uint64_t)); 777 break; 778 } 779 return value; 780 } 781 782 _Unwind_VRS_Result 783 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass, 784 uint32_t regno, _Unwind_VRS_DataRepresentation representation, 785 void *valuep) { 786 _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, " 787 "rep=%d, value=0x%llX)\n", 788 static_cast<void *>(context), regclass, regno, 789 representation, 790 ValueAsBitPattern(representation, valuep)); 791 unw_cursor_t *cursor = (unw_cursor_t *)context; 792 switch (regclass) { 793 case _UVRSC_CORE: 794 if (representation != _UVRSD_UINT32 || regno > 15) 795 return _UVRSR_FAILED; 796 return unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno), 797 *(unw_word_t *)valuep) == UNW_ESUCCESS 798 ? _UVRSR_OK 799 : _UVRSR_FAILED; 800 case _UVRSC_WMMXC: 801 if (representation != _UVRSD_UINT32 || regno > 3) 802 return _UVRSR_FAILED; 803 return unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno), 804 *(unw_word_t *)valuep) == UNW_ESUCCESS 805 ? _UVRSR_OK 806 : _UVRSR_FAILED; 807 case _UVRSC_VFP: 808 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) 809 return _UVRSR_FAILED; 810 if (representation == _UVRSD_VFPX) { 811 // Can only touch d0-15 with FSTMFDX. 812 if (regno > 15) 813 return _UVRSR_FAILED; 814 unw_save_vfp_as_X(cursor); 815 } else { 816 if (regno > 31) 817 return _UVRSR_FAILED; 818 } 819 return unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno), 820 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS 821 ? _UVRSR_OK 822 : _UVRSR_FAILED; 823 case _UVRSC_WMMXD: 824 if (representation != _UVRSD_DOUBLE || regno > 31) 825 return _UVRSR_FAILED; 826 return unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno), 827 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS 828 ? _UVRSR_OK 829 : _UVRSR_FAILED; 830 } 831 _LIBUNWIND_ABORT("unsupported register class"); 832 } 833 834 static _Unwind_VRS_Result 835 _Unwind_VRS_Get_Internal(_Unwind_Context *context, 836 _Unwind_VRS_RegClass regclass, uint32_t regno, 837 _Unwind_VRS_DataRepresentation representation, 838 void *valuep) { 839 unw_cursor_t *cursor = (unw_cursor_t *)context; 840 switch (regclass) { 841 case _UVRSC_CORE: 842 if (representation != _UVRSD_UINT32 || regno > 15) 843 return _UVRSR_FAILED; 844 return unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno), 845 (unw_word_t *)valuep) == UNW_ESUCCESS 846 ? _UVRSR_OK 847 : _UVRSR_FAILED; 848 case _UVRSC_WMMXC: 849 if (representation != _UVRSD_UINT32 || regno > 3) 850 return _UVRSR_FAILED; 851 return unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno), 852 (unw_word_t *)valuep) == UNW_ESUCCESS 853 ? _UVRSR_OK 854 : _UVRSR_FAILED; 855 case _UVRSC_VFP: 856 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) 857 return _UVRSR_FAILED; 858 if (representation == _UVRSD_VFPX) { 859 // Can only touch d0-15 with FSTMFDX. 860 if (regno > 15) 861 return _UVRSR_FAILED; 862 unw_save_vfp_as_X(cursor); 863 } else { 864 if (regno > 31) 865 return _UVRSR_FAILED; 866 } 867 return unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno), 868 (unw_fpreg_t *)valuep) == UNW_ESUCCESS 869 ? _UVRSR_OK 870 : _UVRSR_FAILED; 871 case _UVRSC_WMMXD: 872 if (representation != _UVRSD_DOUBLE || regno > 31) 873 return _UVRSR_FAILED; 874 return unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno), 875 (unw_fpreg_t *)valuep) == UNW_ESUCCESS 876 ? _UVRSR_OK 877 : _UVRSR_FAILED; 878 } 879 _LIBUNWIND_ABORT("unsupported register class"); 880 } 881 882 _Unwind_VRS_Result _Unwind_VRS_Get( 883 _Unwind_Context *context, 884 _Unwind_VRS_RegClass regclass, 885 uint32_t regno, 886 _Unwind_VRS_DataRepresentation representation, 887 void *valuep) { 888 _Unwind_VRS_Result result = 889 _Unwind_VRS_Get_Internal(context, regclass, regno, representation, 890 valuep); 891 _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, " 892 "rep=%d, value=0x%llX, result = %d)\n", 893 static_cast<void *>(context), regclass, regno, 894 representation, 895 ValueAsBitPattern(representation, valuep), result); 896 return result; 897 } 898 899 _Unwind_VRS_Result 900 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass, 901 uint32_t discriminator, 902 _Unwind_VRS_DataRepresentation representation) { 903 _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, " 904 "discriminator=%d, representation=%d)\n", 905 static_cast<void *>(context), regclass, discriminator, 906 representation); 907 switch (regclass) { 908 case _UVRSC_CORE: 909 case _UVRSC_WMMXC: { 910 if (representation != _UVRSD_UINT32) 911 return _UVRSR_FAILED; 912 // When popping SP from the stack, we don't want to override it from the 913 // computed new stack location. See EHABI #7.5.4 table 3. 914 bool poppedSP = false; 915 uint32_t* sp; 916 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, 917 _UVRSD_UINT32, &sp) != _UVRSR_OK) { 918 return _UVRSR_FAILED; 919 } 920 for (uint32_t i = 0; i < 16; ++i) { 921 if (!(discriminator & static_cast<uint32_t>(1 << i))) 922 continue; 923 uint32_t value = *sp++; 924 if (regclass == _UVRSC_CORE && i == 13) 925 poppedSP = true; 926 if (_Unwind_VRS_Set(context, regclass, i, 927 _UVRSD_UINT32, &value) != _UVRSR_OK) { 928 return _UVRSR_FAILED; 929 } 930 } 931 if (!poppedSP) { 932 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, 933 _UVRSD_UINT32, &sp); 934 } 935 return _UVRSR_OK; 936 } 937 case _UVRSC_VFP: 938 case _UVRSC_WMMXD: { 939 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) 940 return _UVRSR_FAILED; 941 uint32_t first = discriminator >> 16; 942 uint32_t count = discriminator & 0xffff; 943 uint32_t end = first+count; 944 uint32_t* sp; 945 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, 946 _UVRSD_UINT32, &sp) != _UVRSR_OK) { 947 return _UVRSR_FAILED; 948 } 949 // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard 950 // format 1", which is equivalent to FSTMD + a padding word. 951 for (uint32_t i = first; i < end; ++i) { 952 // SP is only 32-bit aligned so don't copy 64-bit at a time. 953 uint64_t value = *sp++; 954 value |= ((uint64_t)(*sp++)) << 32; 955 if (_Unwind_VRS_Set(context, regclass, i, representation, &value) != 956 _UVRSR_OK) 957 return _UVRSR_FAILED; 958 } 959 if (representation == _UVRSD_VFPX) 960 ++sp; 961 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 962 &sp); 963 } 964 } 965 _LIBUNWIND_ABORT("unsupported register class"); 966 } 967 968 /// Called by personality handler during phase 2 to find the start of the 969 /// function. 970 _LIBUNWIND_EXPORT uintptr_t 971 _Unwind_GetRegionStart(struct _Unwind_Context *context) { 972 unw_cursor_t *cursor = (unw_cursor_t *)context; 973 unw_proc_info_t frameInfo; 974 uintptr_t result = 0; 975 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) 976 result = (uintptr_t)frameInfo.start_ip; 977 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX\n", 978 static_cast<void *>(context), (long long)result); 979 return result; 980 } 981 982 983 /// Called by personality handler during phase 2 if a foreign exception 984 // is caught. 985 _LIBUNWIND_EXPORT void 986 _Unwind_DeleteException(_Unwind_Exception *exception_object) { 987 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)\n", 988 static_cast<void *>(exception_object)); 989 if (exception_object->exception_cleanup != NULL) 990 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, 991 exception_object); 992 } 993 994 #endif // LIBCXXABI_ARM_EHABI 995