1349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric // 80b57cec5SDimitry Andric // Implements C++ ABI Exception Handling Level 1 as documented at: 90b57cec5SDimitry Andric // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html 100b57cec5SDimitry Andric // using libunwind 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric // ARM EHABI does not specify _Unwind_{Get,Set}{GR,IP}(). Thus, we are 150b57cec5SDimitry Andric // defining inline functions to delegate the function calls to 160b57cec5SDimitry Andric // _Unwind_VRS_{Get,Set}(). However, some applications might declare the 170b57cec5SDimitry Andric // function protetype directly (instead of including <unwind.h>), thus we need 180b57cec5SDimitry Andric // to export these functions from libunwind.so as well. 190b57cec5SDimitry Andric #define _LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE 1 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric #include <inttypes.h> 220b57cec5SDimitry Andric #include <stdint.h> 230b57cec5SDimitry Andric #include <stdbool.h> 240b57cec5SDimitry Andric #include <stdlib.h> 250b57cec5SDimitry Andric #include <stdio.h> 260b57cec5SDimitry Andric #include <string.h> 270b57cec5SDimitry Andric 28349cc55cSDimitry Andric #include "cet_unwind.h" 290b57cec5SDimitry Andric #include "config.h" 300b57cec5SDimitry Andric #include "libunwind.h" 310b57cec5SDimitry Andric #include "libunwind_ext.h" 320b57cec5SDimitry Andric #include "unwind.h" 330b57cec5SDimitry Andric 340fca6ea1SDimitry Andric #if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) && \ 350fca6ea1SDimitry Andric !defined(__wasm__) 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric #ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND 380b57cec5SDimitry Andric 39349cc55cSDimitry Andric // When CET is enabled, each "call" instruction will push return address to 40349cc55cSDimitry Andric // CET shadow stack, each "ret" instruction will pop current CET shadow stack 41349cc55cSDimitry Andric // top and compare it with target address which program will return. 42349cc55cSDimitry Andric // In exception handing, some stack frames will be skipped before jumping to 43349cc55cSDimitry Andric // landing pad and we must adjust CET shadow stack accordingly. 44349cc55cSDimitry Andric // _LIBUNWIND_POP_CET_SSP is used to adjust CET shadow stack pointer and we 45bdd1243dSDimitry Andric // directly jump to __libunwind_Registers_x86/x86_64_jumpto instead of using 46349cc55cSDimitry Andric // a regular function call to avoid pushing to CET shadow stack again. 47*62987288SDimitry Andric #if !defined(_LIBUNWIND_USE_CET) && !defined(_LIBUNWIND_USE_GCS) 4881ad6265SDimitry Andric #define __unw_phase2_resume(cursor, fn) \ 4981ad6265SDimitry Andric do { \ 5081ad6265SDimitry Andric (void)fn; \ 5181ad6265SDimitry Andric __unw_resume((cursor)); \ 5281ad6265SDimitry Andric } while (0) 53349cc55cSDimitry Andric #elif defined(_LIBUNWIND_TARGET_I386) 54bdd1243dSDimitry Andric #define __cet_ss_step_size 4 55349cc55cSDimitry Andric #define __unw_phase2_resume(cursor, fn) \ 56349cc55cSDimitry Andric do { \ 57349cc55cSDimitry Andric _LIBUNWIND_POP_CET_SSP((fn)); \ 58349cc55cSDimitry Andric void *cetRegContext = __libunwind_cet_get_registers((cursor)); \ 59349cc55cSDimitry Andric void *cetJumpAddress = __libunwind_cet_get_jump_target(); \ 60349cc55cSDimitry Andric __asm__ volatile("push %%edi\n\t" \ 61349cc55cSDimitry Andric "sub $4, %%esp\n\t" \ 62349cc55cSDimitry Andric "jmp *%%edx\n\t" :: "D"(cetRegContext), \ 63349cc55cSDimitry Andric "d"(cetJumpAddress)); \ 64349cc55cSDimitry Andric } while (0) 65349cc55cSDimitry Andric #elif defined(_LIBUNWIND_TARGET_X86_64) 66bdd1243dSDimitry Andric #define __cet_ss_step_size 8 67349cc55cSDimitry Andric #define __unw_phase2_resume(cursor, fn) \ 68349cc55cSDimitry Andric do { \ 69349cc55cSDimitry Andric _LIBUNWIND_POP_CET_SSP((fn)); \ 70349cc55cSDimitry Andric void *cetRegContext = __libunwind_cet_get_registers((cursor)); \ 71349cc55cSDimitry Andric void *cetJumpAddress = __libunwind_cet_get_jump_target(); \ 72349cc55cSDimitry Andric __asm__ volatile("jmpq *%%rdx\n\t" :: "D"(cetRegContext), \ 73349cc55cSDimitry Andric "d"(cetJumpAddress)); \ 74349cc55cSDimitry Andric } while (0) 75*62987288SDimitry Andric #elif defined(_LIBUNWIND_TARGET_AARCH64) 76*62987288SDimitry Andric #define __cet_ss_step_size 8 77*62987288SDimitry Andric #define __unw_phase2_resume(cursor, fn) \ 78*62987288SDimitry Andric do { \ 79*62987288SDimitry Andric _LIBUNWIND_POP_CET_SSP((fn)); \ 80*62987288SDimitry Andric void *cetRegContext = __libunwind_cet_get_registers((cursor)); \ 81*62987288SDimitry Andric void *cetJumpAddress = __libunwind_cet_get_jump_target(); \ 82*62987288SDimitry Andric __asm__ volatile("mov x0, %0\n\t" \ 83*62987288SDimitry Andric "br %1\n\t" \ 84*62987288SDimitry Andric : \ 85*62987288SDimitry Andric : "r"(cetRegContext), "r"(cetJumpAddress) \ 86*62987288SDimitry Andric : "x0"); \ 87*62987288SDimitry Andric } while (0) 88349cc55cSDimitry Andric #endif 89349cc55cSDimitry Andric 900b57cec5SDimitry Andric static _Unwind_Reason_Code 910b57cec5SDimitry Andric unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { 920b57cec5SDimitry Andric __unw_init_local(cursor, uc); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric // Walk each frame looking for a place to stop. 95e8d8bef9SDimitry Andric while (true) { 960b57cec5SDimitry Andric // Ask libunwind to get next frame (skip over first which is 970b57cec5SDimitry Andric // _Unwind_RaiseException). 980b57cec5SDimitry Andric int stepResult = __unw_step(cursor); 990b57cec5SDimitry Andric if (stepResult == 0) { 1000b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 101bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): __unw_step() reached " 1020b57cec5SDimitry Andric "bottom => _URC_END_OF_STACK", 1030b57cec5SDimitry Andric (void *)exception_object); 1040b57cec5SDimitry Andric return _URC_END_OF_STACK; 1050b57cec5SDimitry Andric } else if (stepResult < 0) { 1060b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 107bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): __unw_step failed => " 1080b57cec5SDimitry Andric "_URC_FATAL_PHASE1_ERROR", 1090b57cec5SDimitry Andric (void *)exception_object); 1100b57cec5SDimitry Andric return _URC_FATAL_PHASE1_ERROR; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric // See if frame has code to run (has personality routine). 1140b57cec5SDimitry Andric unw_proc_info_t frameInfo; 1150b57cec5SDimitry Andric unw_word_t sp; 1160b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { 1170b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 118bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): __unw_get_proc_info " 1190b57cec5SDimitry Andric "failed => _URC_FATAL_PHASE1_ERROR", 1200b57cec5SDimitry Andric (void *)exception_object); 1210b57cec5SDimitry Andric return _URC_FATAL_PHASE1_ERROR; 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 124349cc55cSDimitry Andric #ifndef NDEBUG 1250b57cec5SDimitry Andric // When tracing, print state information. 1260b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 1270b57cec5SDimitry Andric char functionBuf[512]; 1280b57cec5SDimitry Andric const char *functionName = functionBuf; 1290b57cec5SDimitry Andric unw_word_t offset; 1300b57cec5SDimitry Andric if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), 1310b57cec5SDimitry Andric &offset) != UNW_ESUCCESS) || 1320b57cec5SDimitry Andric (frameInfo.start_ip + offset > frameInfo.end_ip)) 1330b57cec5SDimitry Andric functionName = ".anonymous."; 1340b57cec5SDimitry Andric unw_word_t pc; 1350b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &pc); 1360b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 137bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR 1380b57cec5SDimitry Andric ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "", 1390b57cec5SDimitry Andric (void *)exception_object, pc, frameInfo.start_ip, functionName, 1400b57cec5SDimitry Andric frameInfo.lsda, frameInfo.handler); 1410b57cec5SDimitry Andric } 142349cc55cSDimitry Andric #endif 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric // If there is a personality routine, ask it if it will want to stop at 1450b57cec5SDimitry Andric // this frame. 1460b57cec5SDimitry Andric if (frameInfo.handler != 0) { 1475ffd83dbSDimitry Andric _Unwind_Personality_Fn p = 1485ffd83dbSDimitry Andric (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler); 1490b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 150bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): calling personality function %p", 1510b57cec5SDimitry Andric (void *)exception_object, (void *)(uintptr_t)p); 1520b57cec5SDimitry Andric _Unwind_Reason_Code personalityResult = 1530b57cec5SDimitry Andric (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class, 1540b57cec5SDimitry Andric exception_object, (struct _Unwind_Context *)(cursor)); 1550b57cec5SDimitry Andric switch (personalityResult) { 1560b57cec5SDimitry Andric case _URC_HANDLER_FOUND: 1570b57cec5SDimitry Andric // found a catch clause or locals that need destructing in this frame 1580b57cec5SDimitry Andric // stop search and remember stack pointer at the frame 1590b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_SP, &sp); 1600b57cec5SDimitry Andric exception_object->private_2 = (uintptr_t)sp; 1610b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 162bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): _URC_HANDLER_FOUND", 1630b57cec5SDimitry Andric (void *)exception_object); 1640b57cec5SDimitry Andric return _URC_NO_REASON; 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric case _URC_CONTINUE_UNWIND: 1670b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 168bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): _URC_CONTINUE_UNWIND", 1690b57cec5SDimitry Andric (void *)exception_object); 1700b57cec5SDimitry Andric // continue unwinding 1710b57cec5SDimitry Andric break; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric default: 1740b57cec5SDimitry Andric // something went wrong 1750b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 176bdd1243dSDimitry Andric "unwind_phase1(ex_obj=%p): _URC_FATAL_PHASE1_ERROR", 1770b57cec5SDimitry Andric (void *)exception_object); 1780b57cec5SDimitry Andric return _URC_FATAL_PHASE1_ERROR; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric return _URC_NO_REASON; 1830b57cec5SDimitry Andric } 184bdd1243dSDimitry Andric extern int __unw_step_stage2(unw_cursor_t *); 1850b57cec5SDimitry Andric 186*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_GCS) 187*62987288SDimitry Andric // Enable the GCS target feature to permit gcspop instructions to be used. 188*62987288SDimitry Andric __attribute__((target("gcs"))) 189*62987288SDimitry Andric #endif 1900b57cec5SDimitry Andric static _Unwind_Reason_Code 1910b57cec5SDimitry Andric unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { 1920b57cec5SDimitry Andric __unw_init_local(cursor, uc); 1930b57cec5SDimitry Andric 194bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_obj=%p)", 1950b57cec5SDimitry Andric (void *)exception_object); 1960b57cec5SDimitry Andric 197349cc55cSDimitry Andric // uc is initialized by __unw_getcontext in the parent frame. The first stack 198349cc55cSDimitry Andric // frame walked is unwind_phase2. 199349cc55cSDimitry Andric unsigned framesWalked = 1; 200*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_CET) 201bdd1243dSDimitry Andric unsigned long shadowStackTop = _get_ssp(); 202*62987288SDimitry Andric #elif defined(_LIBUNWIND_USE_GCS) 203*62987288SDimitry Andric unsigned long shadowStackTop = 0; 204*62987288SDimitry Andric if (__chkfeat(_CHKFEAT_GCS)) 205*62987288SDimitry Andric shadowStackTop = (unsigned long)__gcspr(); 206bdd1243dSDimitry Andric #endif 2070b57cec5SDimitry Andric // Walk each frame until we reach where search phase said to stop. 2080b57cec5SDimitry Andric while (true) { 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric // Ask libunwind to get next frame (skip over first which is 2110b57cec5SDimitry Andric // _Unwind_RaiseException). 212bdd1243dSDimitry Andric int stepResult = __unw_step_stage2(cursor); 2130b57cec5SDimitry Andric if (stepResult == 0) { 2140b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 215bdd1243dSDimitry Andric "unwind_phase2(ex_obj=%p): __unw_step_stage2() reached " 2160b57cec5SDimitry Andric "bottom => _URC_END_OF_STACK", 2170b57cec5SDimitry Andric (void *)exception_object); 2180b57cec5SDimitry Andric return _URC_END_OF_STACK; 2190b57cec5SDimitry Andric } else if (stepResult < 0) { 2200b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 221bdd1243dSDimitry Andric "unwind_phase2(ex_obj=%p): __unw_step_stage2 failed => " 2220b57cec5SDimitry Andric "_URC_FATAL_PHASE1_ERROR", 2230b57cec5SDimitry Andric (void *)exception_object); 2240b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric // Get info about this frame. 2280b57cec5SDimitry Andric unw_word_t sp; 2290b57cec5SDimitry Andric unw_proc_info_t frameInfo; 2300b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_SP, &sp); 2310b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { 2320b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 233bdd1243dSDimitry Andric "unwind_phase2(ex_obj=%p): __unw_get_proc_info " 2340b57cec5SDimitry Andric "failed => _URC_FATAL_PHASE1_ERROR", 2350b57cec5SDimitry Andric (void *)exception_object); 2360b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 239349cc55cSDimitry Andric #ifndef NDEBUG 2400b57cec5SDimitry Andric // When tracing, print state information. 2410b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 2420b57cec5SDimitry Andric char functionBuf[512]; 2430b57cec5SDimitry Andric const char *functionName = functionBuf; 2440b57cec5SDimitry Andric unw_word_t offset; 2450b57cec5SDimitry Andric if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), 2460b57cec5SDimitry Andric &offset) != UNW_ESUCCESS) || 2470b57cec5SDimitry Andric (frameInfo.start_ip + offset > frameInfo.end_ip)) 2480b57cec5SDimitry Andric functionName = ".anonymous."; 249bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_obj=%p): start_ip=0x%" PRIxPTR 2500b57cec5SDimitry Andric ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR 2510b57cec5SDimitry Andric ", personality=0x%" PRIxPTR, 2520b57cec5SDimitry Andric (void *)exception_object, frameInfo.start_ip, 2530b57cec5SDimitry Andric functionName, sp, frameInfo.lsda, 2540b57cec5SDimitry Andric frameInfo.handler); 2550b57cec5SDimitry Andric } 256349cc55cSDimitry Andric #endif 2570b57cec5SDimitry Andric 258bdd1243dSDimitry Andric // In CET enabled environment, we check return address stored in normal stack 259bdd1243dSDimitry Andric // against return address stored in CET shadow stack, if the 2 addresses don't 260bdd1243dSDimitry Andric // match, it means return address in normal stack has been corrupted, we return 261bdd1243dSDimitry Andric // _URC_FATAL_PHASE2_ERROR. 262*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS) 263bdd1243dSDimitry Andric if (shadowStackTop != 0) { 264bdd1243dSDimitry Andric unw_word_t retInNormalStack; 265bdd1243dSDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &retInNormalStack); 266bdd1243dSDimitry Andric unsigned long retInShadowStack = *( 267bdd1243dSDimitry Andric unsigned long *)(shadowStackTop + __cet_ss_step_size * framesWalked); 268bdd1243dSDimitry Andric if (retInNormalStack != retInShadowStack) 269bdd1243dSDimitry Andric return _URC_FATAL_PHASE2_ERROR; 270bdd1243dSDimitry Andric } 271bdd1243dSDimitry Andric #endif 272349cc55cSDimitry Andric ++framesWalked; 2730b57cec5SDimitry Andric // If there is a personality routine, tell it we are unwinding. 2740b57cec5SDimitry Andric if (frameInfo.handler != 0) { 2755ffd83dbSDimitry Andric _Unwind_Personality_Fn p = 2765ffd83dbSDimitry Andric (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler); 2770b57cec5SDimitry Andric _Unwind_Action action = _UA_CLEANUP_PHASE; 2780b57cec5SDimitry Andric if (sp == exception_object->private_2) { 2790b57cec5SDimitry Andric // Tell personality this was the frame it marked in phase 1. 2800b57cec5SDimitry Andric action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME); 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric _Unwind_Reason_Code personalityResult = 2830b57cec5SDimitry Andric (*p)(1, action, exception_object->exception_class, exception_object, 2840b57cec5SDimitry Andric (struct _Unwind_Context *)(cursor)); 2850b57cec5SDimitry Andric switch (personalityResult) { 2860b57cec5SDimitry Andric case _URC_CONTINUE_UNWIND: 2870b57cec5SDimitry Andric // Continue unwinding 2880b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 289bdd1243dSDimitry Andric "unwind_phase2(ex_obj=%p): _URC_CONTINUE_UNWIND", 2900b57cec5SDimitry Andric (void *)exception_object); 2910b57cec5SDimitry Andric if (sp == exception_object->private_2) { 2920b57cec5SDimitry Andric // Phase 1 said we would stop at this frame, but we did not... 2930b57cec5SDimitry Andric _LIBUNWIND_ABORT("during phase1 personality function said it would " 2940b57cec5SDimitry Andric "stop here, but now in phase2 it did not stop here"); 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric break; 2970b57cec5SDimitry Andric case _URC_INSTALL_CONTEXT: 2980b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 299bdd1243dSDimitry Andric "unwind_phase2(ex_obj=%p): _URC_INSTALL_CONTEXT", 3000b57cec5SDimitry Andric (void *)exception_object); 3010b57cec5SDimitry Andric // Personality routine says to transfer control to landing pad. 3020b57cec5SDimitry Andric // We may get control back if landing pad calls _Unwind_Resume(). 3030b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 3040b57cec5SDimitry Andric unw_word_t pc; 3050b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &pc); 3060b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_SP, &sp); 307bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_obj=%p): re-entering " 3080b57cec5SDimitry Andric "user code with ip=0x%" PRIxPTR 3090b57cec5SDimitry Andric ", sp=0x%" PRIxPTR, 3100b57cec5SDimitry Andric (void *)exception_object, pc, sp); 3110b57cec5SDimitry Andric } 312349cc55cSDimitry Andric 313349cc55cSDimitry Andric __unw_phase2_resume(cursor, framesWalked); 314349cc55cSDimitry Andric // __unw_phase2_resume() only returns if there was an error. 3150b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 3160b57cec5SDimitry Andric default: 3170b57cec5SDimitry Andric // Personality routine returned an unknown result code. 3180b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d", 3190b57cec5SDimitry Andric personalityResult); 3200b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric // Clean up phase did not resume at the frame that the search phase 3260b57cec5SDimitry Andric // said it would... 3270b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric 330*62987288SDimitry Andric #if defined(_LIBUNWIND_USE_GCS) 331*62987288SDimitry Andric // Enable the GCS target feature to permit gcspop instructions to be used. 332*62987288SDimitry Andric __attribute__((target("gcs"))) 333*62987288SDimitry Andric #endif 3340b57cec5SDimitry Andric static _Unwind_Reason_Code 3350b57cec5SDimitry Andric unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor, 3360b57cec5SDimitry Andric _Unwind_Exception *exception_object, 3370b57cec5SDimitry Andric _Unwind_Stop_Fn stop, void *stop_parameter) { 3380b57cec5SDimitry Andric __unw_init_local(cursor, uc); 3390b57cec5SDimitry Andric 340349cc55cSDimitry Andric // uc is initialized by __unw_getcontext in the parent frame. The first stack 341349cc55cSDimitry Andric // frame walked is unwind_phase2_forced. 342349cc55cSDimitry Andric unsigned framesWalked = 1; 3430b57cec5SDimitry Andric // Walk each frame until we reach where search phase said to stop 344bdd1243dSDimitry Andric while (__unw_step_stage2(cursor) > 0) { 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Update info about this frame. 3470b57cec5SDimitry Andric unw_proc_info_t frameInfo; 3480b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { 349bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 35006c3fb27SDimitry Andric "unwind_phase2_forced(ex_obj=%p): __unw_get_proc_info " 3510b57cec5SDimitry Andric "failed => _URC_END_OF_STACK", 3520b57cec5SDimitry Andric (void *)exception_object); 3530b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 356349cc55cSDimitry Andric #ifndef NDEBUG 3570b57cec5SDimitry Andric // When tracing, print state information. 3580b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 3590b57cec5SDimitry Andric char functionBuf[512]; 3600b57cec5SDimitry Andric const char *functionName = functionBuf; 3610b57cec5SDimitry Andric unw_word_t offset; 3620b57cec5SDimitry Andric if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), 3630b57cec5SDimitry Andric &offset) != UNW_ESUCCESS) || 3640b57cec5SDimitry Andric (frameInfo.start_ip + offset > frameInfo.end_ip)) 3650b57cec5SDimitry Andric functionName = ".anonymous."; 3660b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 367bdd1243dSDimitry Andric "unwind_phase2_forced(ex_obj=%p): start_ip=0x%" PRIxPTR 3680b57cec5SDimitry Andric ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR, 3690b57cec5SDimitry Andric (void *)exception_object, frameInfo.start_ip, functionName, 3700b57cec5SDimitry Andric frameInfo.lsda, frameInfo.handler); 3710b57cec5SDimitry Andric } 372349cc55cSDimitry Andric #endif 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // Call stop function at each frame. 3750b57cec5SDimitry Andric _Unwind_Action action = 3760b57cec5SDimitry Andric (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE); 3770b57cec5SDimitry Andric _Unwind_Reason_Code stopResult = 3780b57cec5SDimitry Andric (*stop)(1, action, exception_object->exception_class, exception_object, 3790b57cec5SDimitry Andric (struct _Unwind_Context *)(cursor), stop_parameter); 3800b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 381bdd1243dSDimitry Andric "unwind_phase2_forced(ex_obj=%p): stop function returned %d", 3820b57cec5SDimitry Andric (void *)exception_object, stopResult); 3830b57cec5SDimitry Andric if (stopResult != _URC_NO_REASON) { 3840b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 385bdd1243dSDimitry Andric "unwind_phase2_forced(ex_obj=%p): stopped by stop function", 3860b57cec5SDimitry Andric (void *)exception_object); 3870b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 390349cc55cSDimitry Andric ++framesWalked; 3910b57cec5SDimitry Andric // If there is a personality routine, tell it we are unwinding. 3920b57cec5SDimitry Andric if (frameInfo.handler != 0) { 3935ffd83dbSDimitry Andric _Unwind_Personality_Fn p = 3945ffd83dbSDimitry Andric (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler); 3950b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 396bdd1243dSDimitry Andric "unwind_phase2_forced(ex_obj=%p): calling personality function %p", 3970b57cec5SDimitry Andric (void *)exception_object, (void *)(uintptr_t)p); 3980b57cec5SDimitry Andric _Unwind_Reason_Code personalityResult = 3990b57cec5SDimitry Andric (*p)(1, action, exception_object->exception_class, exception_object, 4000b57cec5SDimitry Andric (struct _Unwind_Context *)(cursor)); 4010b57cec5SDimitry Andric switch (personalityResult) { 4020b57cec5SDimitry Andric case _URC_CONTINUE_UNWIND: 403bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): " 4040b57cec5SDimitry Andric "personality returned " 4050b57cec5SDimitry Andric "_URC_CONTINUE_UNWIND", 4060b57cec5SDimitry Andric (void *)exception_object); 4070b57cec5SDimitry Andric // Destructors called, continue unwinding 4080b57cec5SDimitry Andric break; 4090b57cec5SDimitry Andric case _URC_INSTALL_CONTEXT: 410bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): " 4110b57cec5SDimitry Andric "personality returned " 4120b57cec5SDimitry Andric "_URC_INSTALL_CONTEXT", 4130b57cec5SDimitry Andric (void *)exception_object); 4140b57cec5SDimitry Andric // We may get control back if landing pad calls _Unwind_Resume(). 415349cc55cSDimitry Andric __unw_phase2_resume(cursor, framesWalked); 4160b57cec5SDimitry Andric break; 4170b57cec5SDimitry Andric default: 4180b57cec5SDimitry Andric // Personality routine returned an unknown result code. 419bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): " 4200b57cec5SDimitry Andric "personality returned %d, " 4210b57cec5SDimitry Andric "_URC_FATAL_PHASE2_ERROR", 4220b57cec5SDimitry Andric (void *)exception_object, personalityResult); 4230b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 4240b57cec5SDimitry Andric } 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric // Call stop function one last time and tell it we've reached the end 4290b57cec5SDimitry Andric // of the stack. 430bdd1243dSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_obj=%p): calling stop " 4310b57cec5SDimitry Andric "function with _UA_END_OF_STACK", 4320b57cec5SDimitry Andric (void *)exception_object); 4330b57cec5SDimitry Andric _Unwind_Action lastAction = 4340b57cec5SDimitry Andric (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK); 4350b57cec5SDimitry Andric (*stop)(1, lastAction, exception_object->exception_class, exception_object, 4360b57cec5SDimitry Andric (struct _Unwind_Context *)(cursor), stop_parameter); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric // Clean up phase did not resume at the frame that the search phase said it 4390b57cec5SDimitry Andric // would. 4400b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric /// Called by __cxa_throw. Only returns if there is a fatal error. 4450b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code 4460b57cec5SDimitry Andric _Unwind_RaiseException(_Unwind_Exception *exception_object) { 4470b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)", 4480b57cec5SDimitry Andric (void *)exception_object); 4490b57cec5SDimitry Andric unw_context_t uc; 4500b57cec5SDimitry Andric unw_cursor_t cursor; 4510b57cec5SDimitry Andric __unw_getcontext(&uc); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric // Mark that this is a non-forced unwind, so _Unwind_Resume() 4540b57cec5SDimitry Andric // can do the right thing. 4550b57cec5SDimitry Andric exception_object->private_1 = 0; 4560b57cec5SDimitry Andric exception_object->private_2 = 0; 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric // phase 1: the search phase 4590b57cec5SDimitry Andric _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object); 4600b57cec5SDimitry Andric if (phase1 != _URC_NO_REASON) 4610b57cec5SDimitry Andric return phase1; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric // phase 2: the clean up phase 4640b57cec5SDimitry Andric return unwind_phase2(&uc, &cursor, exception_object); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric /// When _Unwind_RaiseException() is in phase2, it hands control 4700b57cec5SDimitry Andric /// to the personality function at each frame. The personality 4710b57cec5SDimitry Andric /// may force a jump to a landing pad in that function, the landing 4720b57cec5SDimitry Andric /// pad code may then call _Unwind_Resume() to continue with the 4730b57cec5SDimitry Andric /// unwinding. Note: the call to _Unwind_Resume() is from compiler 474bdd1243dSDimitry Andric /// generated user code. All other _Unwind_* routines are called 4750b57cec5SDimitry Andric /// by the C++ runtime __cxa_* routines. 4760b57cec5SDimitry Andric /// 4770b57cec5SDimitry Andric /// Note: re-throwing an exception (as opposed to continuing the unwind) 4780b57cec5SDimitry Andric /// is implemented by having the code call __cxa_rethrow() which 4790b57cec5SDimitry Andric /// in turn calls _Unwind_Resume_or_Rethrow(). 4800b57cec5SDimitry Andric _LIBUNWIND_EXPORT void 4810b57cec5SDimitry Andric _Unwind_Resume(_Unwind_Exception *exception_object) { 4820b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object); 4830b57cec5SDimitry Andric unw_context_t uc; 4840b57cec5SDimitry Andric unw_cursor_t cursor; 4850b57cec5SDimitry Andric __unw_getcontext(&uc); 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric if (exception_object->private_1 != 0) 4880b57cec5SDimitry Andric unwind_phase2_forced(&uc, &cursor, exception_object, 4890b57cec5SDimitry Andric (_Unwind_Stop_Fn) exception_object->private_1, 4900b57cec5SDimitry Andric (void *)exception_object->private_2); 4910b57cec5SDimitry Andric else 4920b57cec5SDimitry Andric unwind_phase2(&uc, &cursor, exception_object); 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric // Clients assume _Unwind_Resume() does not return, so all we can do is abort. 4950b57cec5SDimitry Andric _LIBUNWIND_ABORT("_Unwind_Resume() can't return"); 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric /// Not used by C++. 5010b57cec5SDimitry Andric /// Unwinds stack, calling "stop" function at each frame. 5020b57cec5SDimitry Andric /// Could be used to implement longjmp(). 5030b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code 5040b57cec5SDimitry Andric _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, 5050b57cec5SDimitry Andric _Unwind_Stop_Fn stop, void *stop_parameter) { 5060b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)", 5070b57cec5SDimitry Andric (void *)exception_object, (void *)(uintptr_t)stop); 5080b57cec5SDimitry Andric unw_context_t uc; 5090b57cec5SDimitry Andric unw_cursor_t cursor; 5100b57cec5SDimitry Andric __unw_getcontext(&uc); 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric // Mark that this is a forced unwind, so _Unwind_Resume() can do 5130b57cec5SDimitry Andric // the right thing. 5140b57cec5SDimitry Andric exception_object->private_1 = (uintptr_t) stop; 5150b57cec5SDimitry Andric exception_object->private_2 = (uintptr_t) stop_parameter; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric // do it 5180b57cec5SDimitry Andric return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter); 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric /// Called by personality handler during phase 2 to get LSDA for current frame. 5230b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t 5240b57cec5SDimitry Andric _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { 5250b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 5260b57cec5SDimitry Andric unw_proc_info_t frameInfo; 5270b57cec5SDimitry Andric uintptr_t result = 0; 5280b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) 5290b57cec5SDimitry Andric result = (uintptr_t)frameInfo.lsda; 5300b57cec5SDimitry Andric _LIBUNWIND_TRACE_API( 5310b57cec5SDimitry Andric "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR, 5320b57cec5SDimitry Andric (void *)context, result); 53381ad6265SDimitry Andric #if !defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND) 5340b57cec5SDimitry Andric if (result != 0) { 5350b57cec5SDimitry Andric if (*((uint8_t *)result) != 0xFF) 5360b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF", 5370b57cec5SDimitry Andric result); 5380b57cec5SDimitry Andric } 53981ad6265SDimitry Andric #endif 5400b57cec5SDimitry Andric return result; 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric /// Called by personality handler during phase 2 to find the start of the 5450b57cec5SDimitry Andric /// function. 5460b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t 5470b57cec5SDimitry Andric _Unwind_GetRegionStart(struct _Unwind_Context *context) { 5480b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 5490b57cec5SDimitry Andric unw_proc_info_t frameInfo; 5500b57cec5SDimitry Andric uintptr_t result = 0; 5510b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) 5520b57cec5SDimitry Andric result = (uintptr_t)frameInfo.start_ip; 5530b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR, 5540b57cec5SDimitry Andric (void *)context, result); 5550b57cec5SDimitry Andric return result; 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric #endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric /// Called by personality handler during phase 2 if a foreign exception 5610b57cec5SDimitry Andric // is caught. 5620b57cec5SDimitry Andric _LIBUNWIND_EXPORT void 5630b57cec5SDimitry Andric _Unwind_DeleteException(_Unwind_Exception *exception_object) { 5640b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)", 5650b57cec5SDimitry Andric (void *)exception_object); 5660b57cec5SDimitry Andric if (exception_object->exception_cleanup != NULL) 5670b57cec5SDimitry Andric (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, 5680b57cec5SDimitry Andric exception_object); 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric /// Called by personality handler during phase 2 to get register values. 5720b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t 5730b57cec5SDimitry Andric _Unwind_GetGR(struct _Unwind_Context *context, int index) { 5740b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 5750b57cec5SDimitry Andric unw_word_t result; 5760b57cec5SDimitry Andric __unw_get_reg(cursor, index, &result); 5770b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR, 5780b57cec5SDimitry Andric (void *)context, index, result); 5790b57cec5SDimitry Andric return (uintptr_t)result; 5800b57cec5SDimitry Andric } 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric /// Called by personality handler during phase 2 to alter register values. 5830b57cec5SDimitry Andric _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index, 5840b57cec5SDimitry Andric uintptr_t value) { 5850b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR 5860b57cec5SDimitry Andric ")", 5870b57cec5SDimitry Andric (void *)context, index, value); 5880b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 5890b57cec5SDimitry Andric __unw_set_reg(cursor, index, value); 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric /// Called by personality handler during phase 2 to get instruction pointer. 5930b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) { 5940b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 5950b57cec5SDimitry Andric unw_word_t result; 5960b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &result); 5970b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR, 5980b57cec5SDimitry Andric (void *)context, result); 5990b57cec5SDimitry Andric return (uintptr_t)result; 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric /// Called by personality handler during phase 2 to alter instruction pointer, 6030b57cec5SDimitry Andric /// such as setting where the landing pad is, so _Unwind_Resume() will 6040b57cec5SDimitry Andric /// start executing in the landing pad. 6050b57cec5SDimitry Andric _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context, 6060b57cec5SDimitry Andric uintptr_t value) { 6070b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")", 6080b57cec5SDimitry Andric (void *)context, value); 6090b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 6100b57cec5SDimitry Andric __unw_set_reg(cursor, UNW_REG_IP, value); 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric #endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) 614