Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1 | //===------------------------- UnwindLevel1.c -----------------------------===// |
| 2 | // |
Chandler Carruth | 61860a5 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 6 | // |
| 7 | // |
| 8 | // Implements C++ ABI Exception Handling Level 1 as documented at: |
Louis Dionne | 59d0c60 | 2019-04-11 16:37:07 +0000 | [diff] [blame] | 9 | // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 10 | // using libunwind |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Logan Chien | 5456e8d | 2015-07-24 00:16:48 +0000 | [diff] [blame] | 14 | // ARM EHABI does not specify _Unwind_{Get,Set}{GR,IP}(). Thus, we are |
| 15 | // defining inline functions to delegate the function calls to |
| 16 | // _Unwind_VRS_{Get,Set}(). However, some applications might declare the |
| 17 | // function protetype directly (instead of including <unwind.h>), thus we need |
| 18 | // to export these functions from libunwind.so as well. |
| 19 | #define _LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE 1 |
| 20 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 28 | #include "config.h" |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 29 | #include "libunwind.h" |
| 30 | #include "libunwind_ext.h" |
| 31 | #include "unwind.h" |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 32 | |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 33 | #if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 34 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 35 | #ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND |
| 36 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 37 | static _Unwind_Reason_Code |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 38 | unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 39 | __unw_init_local(cursor, uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 40 | |
| 41 | // Walk each frame looking for a place to stop. |
Fangrui Song | 45db2df | 2020-11-21 12:38:00 -0800 | [diff] [blame] | 42 | while (true) { |
Ed Maste | 6f72338 | 2016-08-30 13:08:21 +0000 | [diff] [blame] | 43 | // Ask libunwind to get next frame (skip over first which is |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 44 | // _Unwind_RaiseException). |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 45 | int stepResult = __unw_step(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 46 | if (stepResult == 0) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 47 | _LIBUNWIND_TRACE_UNWINDING( |
| 48 | "unwind_phase1(ex_ojb=%p): __unw_step() reached " |
| 49 | "bottom => _URC_END_OF_STACK", |
| 50 | (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 51 | return _URC_END_OF_STACK; |
| 52 | } else if (stepResult < 0) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 53 | _LIBUNWIND_TRACE_UNWINDING( |
| 54 | "unwind_phase1(ex_ojb=%p): __unw_step failed => " |
| 55 | "_URC_FATAL_PHASE1_ERROR", |
| 56 | (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 57 | return _URC_FATAL_PHASE1_ERROR; |
| 58 | } |
| 59 | |
| 60 | // See if frame has code to run (has personality routine). |
| 61 | unw_proc_info_t frameInfo; |
| 62 | unw_word_t sp; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 63 | if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { |
| 64 | _LIBUNWIND_TRACE_UNWINDING( |
| 65 | "unwind_phase1(ex_ojb=%p): __unw_get_proc_info " |
| 66 | "failed => _URC_FATAL_PHASE1_ERROR", |
| 67 | (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 68 | return _URC_FATAL_PHASE1_ERROR; |
| 69 | } |
| 70 | |
| 71 | // When tracing, print state information. |
| 72 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 73 | char functionBuf[512]; |
| 74 | const char *functionName = functionBuf; |
| 75 | unw_word_t offset; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 76 | if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), |
| 77 | &offset) != UNW_ESUCCESS) || |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 78 | (frameInfo.start_ip + offset > frameInfo.end_ip)) |
| 79 | functionName = ".anonymous."; |
| 80 | unw_word_t pc; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 81 | __unw_get_reg(cursor, UNW_REG_IP, &pc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 82 | _LIBUNWIND_TRACE_UNWINDING( |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 83 | "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR |
| 84 | ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 85 | (void *)exception_object, pc, frameInfo.start_ip, functionName, |
| 86 | frameInfo.lsda, frameInfo.handler); |
| 87 | } |
| 88 | |
| 89 | // If there is a personality routine, ask it if it will want to stop at |
| 90 | // this frame. |
| 91 | if (frameInfo.handler != 0) { |
Saleem Abdulrasool | 214b72e | 2020-02-10 08:52:31 -0800 | [diff] [blame] | 92 | _Unwind_Personality_Fn p = |
| 93 | (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 94 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 95 | "unwind_phase1(ex_ojb=%p): calling personality function %p", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 96 | (void *)exception_object, (void *)(uintptr_t)p); |
| 97 | _Unwind_Reason_Code personalityResult = |
| 98 | (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 99 | exception_object, (struct _Unwind_Context *)(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 100 | switch (personalityResult) { |
| 101 | case _URC_HANDLER_FOUND: |
| 102 | // found a catch clause or locals that need destructing in this frame |
| 103 | // stop search and remember stack pointer at the frame |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 104 | __unw_get_reg(cursor, UNW_REG_SP, &sp); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 105 | exception_object->private_2 = (uintptr_t)sp; |
| 106 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 107 | "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 108 | (void *)exception_object); |
| 109 | return _URC_NO_REASON; |
| 110 | |
| 111 | case _URC_CONTINUE_UNWIND: |
| 112 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 113 | "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 114 | (void *)exception_object); |
| 115 | // continue unwinding |
| 116 | break; |
| 117 | |
| 118 | default: |
| 119 | // something went wrong |
| 120 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 121 | "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 122 | (void *)exception_object); |
| 123 | return _URC_FATAL_PHASE1_ERROR; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return _URC_NO_REASON; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static _Unwind_Reason_Code |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 132 | unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 133 | __unw_init_local(cursor, uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 134 | |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 135 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 136 | (void *)exception_object); |
| 137 | |
| 138 | // Walk each frame until we reach where search phase said to stop. |
| 139 | while (true) { |
| 140 | |
Ed Maste | 6f72338 | 2016-08-30 13:08:21 +0000 | [diff] [blame] | 141 | // Ask libunwind to get next frame (skip over first which is |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 142 | // _Unwind_RaiseException). |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 143 | int stepResult = __unw_step(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 144 | if (stepResult == 0) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 145 | _LIBUNWIND_TRACE_UNWINDING( |
| 146 | "unwind_phase2(ex_ojb=%p): __unw_step() reached " |
| 147 | "bottom => _URC_END_OF_STACK", |
| 148 | (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 149 | return _URC_END_OF_STACK; |
| 150 | } else if (stepResult < 0) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 151 | _LIBUNWIND_TRACE_UNWINDING( |
| 152 | "unwind_phase2(ex_ojb=%p): __unw_step failed => " |
| 153 | "_URC_FATAL_PHASE1_ERROR", |
| 154 | (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 155 | return _URC_FATAL_PHASE2_ERROR; |
| 156 | } |
| 157 | |
| 158 | // Get info about this frame. |
| 159 | unw_word_t sp; |
| 160 | unw_proc_info_t frameInfo; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 161 | __unw_get_reg(cursor, UNW_REG_SP, &sp); |
| 162 | if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { |
| 163 | _LIBUNWIND_TRACE_UNWINDING( |
| 164 | "unwind_phase2(ex_ojb=%p): __unw_get_proc_info " |
| 165 | "failed => _URC_FATAL_PHASE1_ERROR", |
| 166 | (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 167 | return _URC_FATAL_PHASE2_ERROR; |
| 168 | } |
| 169 | |
| 170 | // When tracing, print state information. |
| 171 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 172 | char functionBuf[512]; |
| 173 | const char *functionName = functionBuf; |
| 174 | unw_word_t offset; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 175 | if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), |
| 176 | &offset) != UNW_ESUCCESS) || |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 177 | (frameInfo.start_ip + offset > frameInfo.end_ip)) |
| 178 | functionName = ".anonymous."; |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 179 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR |
| 180 | ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR |
| 181 | ", personality=0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 182 | (void *)exception_object, frameInfo.start_ip, |
| 183 | functionName, sp, frameInfo.lsda, |
| 184 | frameInfo.handler); |
| 185 | } |
| 186 | |
| 187 | // If there is a personality routine, tell it we are unwinding. |
| 188 | if (frameInfo.handler != 0) { |
Saleem Abdulrasool | 214b72e | 2020-02-10 08:52:31 -0800 | [diff] [blame] | 189 | _Unwind_Personality_Fn p = |
| 190 | (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 191 | _Unwind_Action action = _UA_CLEANUP_PHASE; |
| 192 | if (sp == exception_object->private_2) { |
| 193 | // Tell personality this was the frame it marked in phase 1. |
| 194 | action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME); |
| 195 | } |
| 196 | _Unwind_Reason_Code personalityResult = |
| 197 | (*p)(1, action, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 198 | (struct _Unwind_Context *)(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 199 | switch (personalityResult) { |
| 200 | case _URC_CONTINUE_UNWIND: |
| 201 | // Continue unwinding |
| 202 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 203 | "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 204 | (void *)exception_object); |
| 205 | if (sp == exception_object->private_2) { |
| 206 | // Phase 1 said we would stop at this frame, but we did not... |
| 207 | _LIBUNWIND_ABORT("during phase1 personality function said it would " |
| 208 | "stop here, but now in phase2 it did not stop here"); |
| 209 | } |
| 210 | break; |
| 211 | case _URC_INSTALL_CONTEXT: |
| 212 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 213 | "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 214 | (void *)exception_object); |
| 215 | // Personality routine says to transfer control to landing pad. |
| 216 | // We may get control back if landing pad calls _Unwind_Resume(). |
| 217 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 218 | unw_word_t pc; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 219 | __unw_get_reg(cursor, UNW_REG_IP, &pc); |
| 220 | __unw_get_reg(cursor, UNW_REG_SP, &sp); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 221 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering " |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 222 | "user code with ip=0x%" PRIxPTR |
| 223 | ", sp=0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 224 | (void *)exception_object, pc, sp); |
| 225 | } |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 226 | __unw_resume(cursor); |
| 227 | // __unw_resume() only returns if there was an error. |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 228 | return _URC_FATAL_PHASE2_ERROR; |
| 229 | default: |
| 230 | // Personality routine returned an unknown result code. |
| 231 | _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d", |
| 232 | personalityResult); |
| 233 | return _URC_FATAL_PHASE2_ERROR; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Clean up phase did not resume at the frame that the search phase |
| 239 | // said it would... |
| 240 | return _URC_FATAL_PHASE2_ERROR; |
| 241 | } |
| 242 | |
| 243 | static _Unwind_Reason_Code |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 244 | unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 245 | _Unwind_Exception *exception_object, |
| 246 | _Unwind_Stop_Fn stop, void *stop_parameter) { |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 247 | __unw_init_local(cursor, uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 248 | |
| 249 | // Walk each frame until we reach where search phase said to stop |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 250 | while (__unw_step(cursor) > 0) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 251 | |
| 252 | // Update info about this frame. |
| 253 | unw_proc_info_t frameInfo; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 254 | if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { |
| 255 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 256 | "failed => _URC_END_OF_STACK", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 257 | (void *)exception_object); |
| 258 | return _URC_FATAL_PHASE2_ERROR; |
| 259 | } |
| 260 | |
| 261 | // When tracing, print state information. |
| 262 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 263 | char functionBuf[512]; |
| 264 | const char *functionName = functionBuf; |
| 265 | unw_word_t offset; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 266 | if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), |
| 267 | &offset) != UNW_ESUCCESS) || |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 268 | (frameInfo.start_ip + offset > frameInfo.end_ip)) |
| 269 | functionName = ".anonymous."; |
| 270 | _LIBUNWIND_TRACE_UNWINDING( |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 271 | "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR |
| 272 | ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 273 | (void *)exception_object, frameInfo.start_ip, functionName, |
| 274 | frameInfo.lsda, frameInfo.handler); |
| 275 | } |
| 276 | |
| 277 | // Call stop function at each frame. |
| 278 | _Unwind_Action action = |
| 279 | (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE); |
| 280 | _Unwind_Reason_Code stopResult = |
| 281 | (*stop)(1, action, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 282 | (struct _Unwind_Context *)(cursor), stop_parameter); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 283 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 284 | "unwind_phase2_forced(ex_ojb=%p): stop function returned %d", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 285 | (void *)exception_object, stopResult); |
| 286 | if (stopResult != _URC_NO_REASON) { |
| 287 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 288 | "unwind_phase2_forced(ex_ojb=%p): stopped by stop function", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 289 | (void *)exception_object); |
| 290 | return _URC_FATAL_PHASE2_ERROR; |
| 291 | } |
| 292 | |
| 293 | // If there is a personality routine, tell it we are unwinding. |
| 294 | if (frameInfo.handler != 0) { |
Saleem Abdulrasool | 214b72e | 2020-02-10 08:52:31 -0800 | [diff] [blame] | 295 | _Unwind_Personality_Fn p = |
| 296 | (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 297 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 298 | "unwind_phase2_forced(ex_ojb=%p): calling personality function %p", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 299 | (void *)exception_object, (void *)(uintptr_t)p); |
| 300 | _Unwind_Reason_Code personalityResult = |
| 301 | (*p)(1, action, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 302 | (struct _Unwind_Context *)(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 303 | switch (personalityResult) { |
| 304 | case _URC_CONTINUE_UNWIND: |
| 305 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 306 | "personality returned " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 307 | "_URC_CONTINUE_UNWIND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 308 | (void *)exception_object); |
| 309 | // Destructors called, continue unwinding |
| 310 | break; |
| 311 | case _URC_INSTALL_CONTEXT: |
| 312 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 313 | "personality returned " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 314 | "_URC_INSTALL_CONTEXT", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 315 | (void *)exception_object); |
| 316 | // We may get control back if landing pad calls _Unwind_Resume(). |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 317 | __unw_resume(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 318 | break; |
| 319 | default: |
| 320 | // Personality routine returned an unknown result code. |
| 321 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 322 | "personality returned %d, " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 323 | "_URC_FATAL_PHASE2_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 324 | (void *)exception_object, personalityResult); |
| 325 | return _URC_FATAL_PHASE2_ERROR; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Call stop function one last time and tell it we've reached the end |
| 331 | // of the stack. |
| 332 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 333 | "function with _UA_END_OF_STACK", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 334 | (void *)exception_object); |
| 335 | _Unwind_Action lastAction = |
| 336 | (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK); |
| 337 | (*stop)(1, lastAction, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 338 | (struct _Unwind_Context *)(cursor), stop_parameter); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 339 | |
| 340 | // Clean up phase did not resume at the frame that the search phase said it |
| 341 | // would. |
| 342 | return _URC_FATAL_PHASE2_ERROR; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /// Called by __cxa_throw. Only returns if there is a fatal error. |
| 347 | _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 348 | _Unwind_RaiseException(_Unwind_Exception *exception_object) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 349 | _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 350 | (void *)exception_object); |
| 351 | unw_context_t uc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 352 | unw_cursor_t cursor; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 353 | __unw_getcontext(&uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 354 | |
| 355 | // Mark that this is a non-forced unwind, so _Unwind_Resume() |
| 356 | // can do the right thing. |
| 357 | exception_object->private_1 = 0; |
| 358 | exception_object->private_2 = 0; |
| 359 | |
| 360 | // phase 1: the search phase |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 361 | _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 362 | if (phase1 != _URC_NO_REASON) |
| 363 | return phase1; |
| 364 | |
| 365 | // phase 2: the clean up phase |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 366 | return unwind_phase2(&uc, &cursor, exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | /// When _Unwind_RaiseException() is in phase2, it hands control |
| 372 | /// to the personality function at each frame. The personality |
| 373 | /// may force a jump to a landing pad in that function, the landing |
| 374 | /// pad code may then call _Unwind_Resume() to continue with the |
| 375 | /// unwinding. Note: the call to _Unwind_Resume() is from compiler |
| 376 | /// geneated user code. All other _Unwind_* routines are called |
| 377 | /// by the C++ runtime __cxa_* routines. |
| 378 | /// |
| 379 | /// Note: re-throwing an exception (as opposed to continuing the unwind) |
| 380 | /// is implemented by having the code call __cxa_rethrow() which |
| 381 | /// in turn calls _Unwind_Resume_or_Rethrow(). |
| 382 | _LIBUNWIND_EXPORT void |
| 383 | _Unwind_Resume(_Unwind_Exception *exception_object) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 384 | _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 385 | unw_context_t uc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 386 | unw_cursor_t cursor; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 387 | __unw_getcontext(&uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 388 | |
| 389 | if (exception_object->private_1 != 0) |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 390 | unwind_phase2_forced(&uc, &cursor, exception_object, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 391 | (_Unwind_Stop_Fn) exception_object->private_1, |
| 392 | (void *)exception_object->private_2); |
| 393 | else |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 394 | unwind_phase2(&uc, &cursor, exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 395 | |
| 396 | // Clients assume _Unwind_Resume() does not return, so all we can do is abort. |
| 397 | _LIBUNWIND_ABORT("_Unwind_Resume() can't return"); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | |
| 402 | /// Not used by C++. |
| 403 | /// Unwinds stack, calling "stop" function at each frame. |
| 404 | /// Could be used to implement longjmp(). |
| 405 | _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 406 | _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, |
| 407 | _Unwind_Stop_Fn stop, void *stop_parameter) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 408 | _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 409 | (void *)exception_object, (void *)(uintptr_t)stop); |
| 410 | unw_context_t uc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 411 | unw_cursor_t cursor; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 412 | __unw_getcontext(&uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 413 | |
| 414 | // Mark that this is a forced unwind, so _Unwind_Resume() can do |
| 415 | // the right thing. |
| 416 | exception_object->private_1 = (uintptr_t) stop; |
| 417 | exception_object->private_2 = (uintptr_t) stop_parameter; |
| 418 | |
| 419 | // do it |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 420 | return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
| 424 | /// Called by personality handler during phase 2 to get LSDA for current frame. |
| 425 | _LIBUNWIND_EXPORT uintptr_t |
| 426 | _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { |
| 427 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 428 | unw_proc_info_t frameInfo; |
| 429 | uintptr_t result = 0; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 430 | if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 431 | result = (uintptr_t)frameInfo.lsda; |
| 432 | _LIBUNWIND_TRACE_API( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 433 | "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 434 | (void *)context, result); |
| 435 | if (result != 0) { |
| 436 | if (*((uint8_t *)result) != 0xFF) |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 437 | _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 438 | result); |
| 439 | } |
| 440 | return result; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | /// Called by personality handler during phase 2 to find the start of the |
| 445 | /// function. |
| 446 | _LIBUNWIND_EXPORT uintptr_t |
| 447 | _Unwind_GetRegionStart(struct _Unwind_Context *context) { |
| 448 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 449 | unw_proc_info_t frameInfo; |
| 450 | uintptr_t result = 0; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 451 | if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 452 | result = (uintptr_t)frameInfo.start_ip; |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 453 | _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 454 | (void *)context, result); |
| 455 | return result; |
| 456 | } |
| 457 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 458 | #endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 459 | |
| 460 | /// Called by personality handler during phase 2 if a foreign exception |
| 461 | // is caught. |
| 462 | _LIBUNWIND_EXPORT void |
| 463 | _Unwind_DeleteException(_Unwind_Exception *exception_object) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 464 | _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 465 | (void *)exception_object); |
| 466 | if (exception_object->exception_cleanup != NULL) |
| 467 | (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, |
| 468 | exception_object); |
| 469 | } |
| 470 | |
| 471 | /// Called by personality handler during phase 2 to get register values. |
| 472 | _LIBUNWIND_EXPORT uintptr_t |
| 473 | _Unwind_GetGR(struct _Unwind_Context *context, int index) { |
| 474 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 475 | unw_word_t result; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 476 | __unw_get_reg(cursor, index, &result); |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 477 | _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR, |
| 478 | (void *)context, index, result); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 479 | return (uintptr_t)result; |
| 480 | } |
| 481 | |
| 482 | /// Called by personality handler during phase 2 to alter register values. |
| 483 | _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index, |
| 484 | uintptr_t value) { |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 485 | _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 486 | ")", |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 487 | (void *)context, index, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 488 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 489 | __unw_set_reg(cursor, index, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /// Called by personality handler during phase 2 to get instruction pointer. |
| 493 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) { |
| 494 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 495 | unw_word_t result; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 496 | __unw_get_reg(cursor, UNW_REG_IP, &result); |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 497 | _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR, |
| 498 | (void *)context, result); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 499 | return (uintptr_t)result; |
| 500 | } |
| 501 | |
| 502 | /// Called by personality handler during phase 2 to alter instruction pointer, |
| 503 | /// such as setting where the landing pad is, so _Unwind_Resume() will |
| 504 | /// start executing in the landing pad. |
| 505 | _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context, |
| 506 | uintptr_t value) { |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 507 | _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")", |
| 508 | (void *)context, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 509 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
Petr Hosek | 9bbfad5 | 2019-04-03 21:50:03 +0000 | [diff] [blame] | 510 | __unw_set_reg(cursor, UNW_REG_IP, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 513 | #endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) |