Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [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 | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 6 | // |
| 7 | // |
| 8 | // Implements setjump-longjump based C++ exceptions |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include <unwind.h> |
| 13 | |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 14 | #include <inttypes.h> |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include "config.h" |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 20 | |
Saleem Abdulrasool | 9ebbda8 | 2017-10-01 20:06:48 | [diff] [blame] | 21 | /// With SJLJ based exceptions, any function that has a catch clause or needs to |
| 22 | /// do any clean up when an exception propagates through it, needs to call |
| 23 | /// \c _Unwind_SjLj_Register at the start of the function and |
| 24 | /// \c _Unwind_SjLj_Unregister at the end. The register function is called with |
| 25 | /// the address of a block of memory in the function's stack frame. The runtime |
| 26 | /// keeps a linked list (stack) of these blocks - one per thread. The calling |
| 27 | /// function also sets the personality and lsda fields of the block. |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 28 | |
Ranjeet Singh | 5808011 | 2017-03-31 15:28:06 | [diff] [blame] | 29 | #if defined(_LIBUNWIND_BUILD_SJLJ_APIS) |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 30 | |
| 31 | struct _Unwind_FunctionContext { |
| 32 | // next function in stack of handlers |
| 33 | struct _Unwind_FunctionContext *prev; |
| 34 | |
Kazushi (Jam) Marukawa | 3cbd476 | 2020-12-26 13:50:17 | [diff] [blame] | 35 | #if defined(__ve__) |
Gabriel Ravier | dad00da | 2022-08-21 01:09:03 | [diff] [blame] | 36 | // VE requires to store 64 bit pointers in the buffer for SjLj exception. |
Kazushi (Jam) Marukawa | 3cbd476 | 2020-12-26 13:50:17 | [diff] [blame] | 37 | // We expand the size of values defined here. This size must be matched |
| 38 | // to the size returned by TargetMachine::getSjLjDataSize(). |
| 39 | |
| 40 | // set by calling function before registering to be the landing pad |
| 41 | uint64_t resumeLocation; |
| 42 | |
| 43 | // set by personality handler to be parameters passed to landing pad function |
| 44 | uint64_t resumeParameters[4]; |
| 45 | #else |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 46 | // set by calling function before registering to be the landing pad |
Martin Storsjo | 3b6ea6a | 2017-09-26 08:07:17 | [diff] [blame] | 47 | uint32_t resumeLocation; |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 48 | |
| 49 | // set by personality handler to be parameters passed to landing pad function |
Martin Storsjo | 3b6ea6a | 2017-09-26 08:07:17 | [diff] [blame] | 50 | uint32_t resumeParameters[4]; |
Kazushi (Jam) Marukawa | 3cbd476 | 2020-12-26 13:50:17 | [diff] [blame] | 51 | #endif |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 52 | |
| 53 | // set by calling function before registering |
Saleem Abdulrasool | 14798b4 | 2020-02-10 16:52:31 | [diff] [blame] | 54 | _Unwind_Personality_Fn personality; // arm offset=24 |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 55 | uintptr_t lsda; // arm offset=28 |
| 56 | |
| 57 | // variable length array, contains registers to restore |
| 58 | // 0 = r7, 1 = pc, 2 = sp |
| 59 | void *jbuf[]; |
| 60 | }; |
| 61 | |
Saleem Abdulrasool | 9ebbda8 | 2017-10-01 20:06:48 | [diff] [blame] | 62 | #if defined(_LIBUNWIND_HAS_NO_THREADS) |
| 63 | # define _LIBUNWIND_THREAD_LOCAL |
| 64 | #else |
| 65 | # if __STDC_VERSION__ >= 201112L |
| 66 | # define _LIBUNWIND_THREAD_LOCAL _Thread_local |
Martin Storsjo | e563e08 | 2019-01-18 20:31:12 | [diff] [blame] | 67 | # elif defined(_MSC_VER) |
Saleem Abdulrasool | 9ebbda8 | 2017-10-01 20:06:48 | [diff] [blame] | 68 | # define _LIBUNWIND_THREAD_LOCAL __declspec(thread) |
| 69 | # elif defined(__GNUC__) || defined(__clang__) |
| 70 | # define _LIBUNWIND_THREAD_LOCAL __thread |
| 71 | # else |
| 72 | # error Unable to create thread local storage |
| 73 | # endif |
| 74 | #endif |
| 75 | |
| 76 | |
| 77 | #if !defined(FOR_DYLD) |
| 78 | |
| 79 | #if defined(__APPLE__) |
| 80 | #include <System/pthread_machdep.h> |
| 81 | #else |
Martin Storsjo | 59a5004 | 2017-10-01 20:22:40 | [diff] [blame] | 82 | static _LIBUNWIND_THREAD_LOCAL struct _Unwind_FunctionContext *stack = NULL; |
Saleem Abdulrasool | 9ebbda8 | 2017-10-01 20:06:48 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | static struct _Unwind_FunctionContext *__Unwind_SjLj_GetTopOfFunctionStack() { |
| 86 | #if defined(__APPLE__) |
| 87 | return _pthread_getspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key); |
| 88 | #else |
| 89 | return stack; |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext *fc) { |
| 95 | #if defined(__APPLE__) |
| 96 | _pthread_setspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key, fc); |
| 97 | #else |
| 98 | stack = fc; |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | #endif |
| 103 | |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 104 | |
| 105 | /// Called at start of each function that catches exceptions |
| 106 | _LIBUNWIND_EXPORT void |
| 107 | _Unwind_SjLj_Register(struct _Unwind_FunctionContext *fc) { |
| 108 | fc->prev = __Unwind_SjLj_GetTopOfFunctionStack(); |
| 109 | __Unwind_SjLj_SetTopOfFunctionStack(fc); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /// Called at end of each function that catches exceptions |
| 114 | _LIBUNWIND_EXPORT void |
| 115 | _Unwind_SjLj_Unregister(struct _Unwind_FunctionContext *fc) { |
| 116 | __Unwind_SjLj_SetTopOfFunctionStack(fc->prev); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static _Unwind_Reason_Code |
| 121 | unwind_phase1(struct _Unwind_Exception *exception_object) { |
| 122 | _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack(); |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 123 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: initial function-context=%p", |
| 124 | (void *)c); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 125 | |
| 126 | // walk each frame looking for a place to stop |
| 127 | for (bool handlerNotFound = true; handlerNotFound; c = c->prev) { |
| 128 | |
| 129 | // check for no more frames |
| 130 | if (c == NULL) { |
| 131 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): reached " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 132 | "bottom => _URC_END_OF_STACK", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 133 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 134 | return _URC_END_OF_STACK; |
| 135 | } |
| 136 | |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 137 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: function-context=%p", (void *)c); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 138 | // if there is a personality routine, ask it if it will want to stop at this |
| 139 | // frame |
| 140 | if (c->personality != NULL) { |
| 141 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): calling " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 142 | "personality function %p", |
| 143 | (void *)exception_object, |
| 144 | (void *)c->personality); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 145 | _Unwind_Reason_Code personalityResult = (*c->personality)( |
| 146 | 1, _UA_SEARCH_PHASE, exception_object->exception_class, |
| 147 | exception_object, (struct _Unwind_Context *)c); |
| 148 | switch (personalityResult) { |
| 149 | case _URC_HANDLER_FOUND: |
| 150 | // found a catch clause or locals that need destructing in this frame |
| 151 | // stop search and remember function context |
| 152 | handlerNotFound = false; |
| 153 | exception_object->private_2 = (uintptr_t) c; |
| 154 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 155 | "_URC_HANDLER_FOUND", |
| 156 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 157 | return _URC_NO_REASON; |
| 158 | |
| 159 | case _URC_CONTINUE_UNWIND: |
| 160 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 161 | "_URC_CONTINUE_UNWIND", |
| 162 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 163 | // continue unwinding |
| 164 | break; |
| 165 | |
| 166 | default: |
| 167 | // something went wrong |
| 168 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 169 | "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 170 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 171 | return _URC_FATAL_PHASE1_ERROR; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return _URC_NO_REASON; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static _Unwind_Reason_Code |
| 180 | unwind_phase2(struct _Unwind_Exception *exception_object) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 181 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)", |
| 182 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 183 | |
| 184 | // walk each frame until we reach where search phase said to stop |
| 185 | _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack(); |
| 186 | while (true) { |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 187 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2s(ex_ojb=%p): context=%p", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 188 | (void *)exception_object, (void *)c); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 189 | |
| 190 | // check for no more frames |
| 191 | if (c == NULL) { |
Petr Hosek | e369a98 | 2019-04-03 21:50:03 | [diff] [blame] | 192 | _LIBUNWIND_TRACE_UNWINDING( |
| 193 | "unwind_phase2(ex_ojb=%p): __unw_step() reached " |
| 194 | "bottom => _URC_END_OF_STACK", |
| 195 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 196 | return _URC_END_OF_STACK; |
| 197 | } |
| 198 | |
| 199 | // if there is a personality routine, tell it we are unwinding |
| 200 | if (c->personality != NULL) { |
| 201 | _Unwind_Action action = _UA_CLEANUP_PHASE; |
| 202 | if ((uintptr_t) c == exception_object->private_2) |
| 203 | action = (_Unwind_Action)( |
| 204 | _UA_CLEANUP_PHASE | |
| 205 | _UA_HANDLER_FRAME); // tell personality this was the frame it marked |
| 206 | // in phase 1 |
| 207 | _Unwind_Reason_Code personalityResult = |
| 208 | (*c->personality)(1, action, exception_object->exception_class, |
| 209 | exception_object, (struct _Unwind_Context *)c); |
| 210 | switch (personalityResult) { |
| 211 | case _URC_CONTINUE_UNWIND: |
| 212 | // continue unwinding |
| 213 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 214 | "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 215 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 216 | if ((uintptr_t) c == exception_object->private_2) { |
| 217 | // phase 1 said we would stop at this frame, but we did not... |
| 218 | _LIBUNWIND_ABORT("during phase1 personality function said it would " |
| 219 | "stop here, but now if phase2 it did not stop here"); |
| 220 | } |
| 221 | break; |
| 222 | case _URC_INSTALL_CONTEXT: |
| 223 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 224 | "_URC_INSTALL_CONTEXT, will resume at " |
| 225 | "landing pad %p", |
| 226 | (void *)exception_object, c->jbuf[1]); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 227 | // personality routine says to transfer control to landing pad |
| 228 | // we may get control back if landing pad calls _Unwind_Resume() |
| 229 | __Unwind_SjLj_SetTopOfFunctionStack(c); |
| 230 | __builtin_longjmp(c->jbuf, 1); |
Petr Hosek | e369a98 | 2019-04-03 21:50:03 | [diff] [blame] | 231 | // __unw_resume() only returns if there was an error |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 232 | return _URC_FATAL_PHASE2_ERROR; |
| 233 | default: |
| 234 | // something went wrong |
| 235 | _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d", |
| 236 | personalityResult); |
| 237 | return _URC_FATAL_PHASE2_ERROR; |
| 238 | } |
| 239 | } |
| 240 | c = c->prev; |
| 241 | } |
| 242 | |
| 243 | // clean up phase did not resume at the frame that the search phase said it |
| 244 | // would |
| 245 | return _URC_FATAL_PHASE2_ERROR; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | static _Unwind_Reason_Code |
| 250 | unwind_phase2_forced(struct _Unwind_Exception *exception_object, |
| 251 | _Unwind_Stop_Fn stop, void *stop_parameter) { |
| 252 | // walk each frame until we reach where search phase said to stop |
| 253 | _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack(); |
| 254 | while (true) { |
| 255 | |
| 256 | // get next frame (skip over first which is _Unwind_RaiseException) |
| 257 | if (c == NULL) { |
Petr Hosek | e369a98 | 2019-04-03 21:50:03 | [diff] [blame] | 258 | _LIBUNWIND_TRACE_UNWINDING( |
| 259 | "unwind_phase2(ex_ojb=%p): __unw_step() reached " |
| 260 | "bottom => _URC_END_OF_STACK", |
| 261 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 262 | return _URC_END_OF_STACK; |
| 263 | } |
| 264 | |
| 265 | // call stop function at each frame |
| 266 | _Unwind_Action action = |
| 267 | (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE); |
| 268 | _Unwind_Reason_Code stopResult = |
| 269 | (*stop)(1, action, exception_object->exception_class, exception_object, |
| 270 | (struct _Unwind_Context *)c, stop_parameter); |
| 271 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 272 | "stop function returned %d", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 273 | (void *)exception_object, stopResult); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 274 | if (stopResult != _URC_NO_REASON) { |
| 275 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 276 | "stopped by stop function", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 277 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 278 | return _URC_FATAL_PHASE2_ERROR; |
| 279 | } |
| 280 | |
| 281 | // if there is a personality routine, tell it we are unwinding |
| 282 | if (c->personality != NULL) { |
Saleem Abdulrasool | 14798b4 | 2020-02-10 16:52:31 | [diff] [blame] | 283 | _Unwind_Personality_Fn p = (_Unwind_Personality_Fn)c->personality; |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 284 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 285 | "calling personality function %p", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 286 | (void *)exception_object, (void *)p); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 287 | _Unwind_Reason_Code personalityResult = |
| 288 | (*p)(1, action, exception_object->exception_class, exception_object, |
| 289 | (struct _Unwind_Context *)c); |
| 290 | switch (personalityResult) { |
| 291 | case _URC_CONTINUE_UNWIND: |
| 292 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 293 | "personality returned _URC_CONTINUE_UNWIND", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 294 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 295 | // destructors called, continue unwinding |
| 296 | break; |
| 297 | case _URC_INSTALL_CONTEXT: |
| 298 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 299 | "personality returned _URC_INSTALL_CONTEXT", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 300 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 301 | // we may get control back if landing pad calls _Unwind_Resume() |
| 302 | __Unwind_SjLj_SetTopOfFunctionStack(c); |
| 303 | __builtin_longjmp(c->jbuf, 1); |
| 304 | break; |
| 305 | default: |
| 306 | // something went wrong |
| 307 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 308 | "personality returned %d, " |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 309 | "_URC_FATAL_PHASE2_ERROR", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 310 | (void *)exception_object, personalityResult); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 311 | return _URC_FATAL_PHASE2_ERROR; |
| 312 | } |
| 313 | } |
| 314 | c = c->prev; |
| 315 | } |
| 316 | |
| 317 | // call stop function one last time and tell it we've reached the end of the |
| 318 | // stack |
| 319 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 320 | "function with _UA_END_OF_STACK", |
| 321 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 322 | _Unwind_Action lastAction = |
| 323 | (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK); |
| 324 | (*stop)(1, lastAction, exception_object->exception_class, exception_object, |
| 325 | (struct _Unwind_Context *)c, stop_parameter); |
| 326 | |
| 327 | // clean up phase did not resume at the frame that the search phase said it |
| 328 | // would |
| 329 | return _URC_FATAL_PHASE2_ERROR; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | /// Called by __cxa_throw. Only returns if there is a fatal error |
| 334 | _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 335 | _Unwind_SjLj_RaiseException(struct _Unwind_Exception *exception_object) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 336 | _LIBUNWIND_TRACE_API("_Unwind_SjLj_RaiseException(ex_obj=%p)", |
| 337 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 338 | |
| 339 | // mark that this is a non-forced unwind, so _Unwind_Resume() can do the right |
| 340 | // thing |
| 341 | exception_object->private_1 = 0; |
| 342 | exception_object->private_2 = 0; |
| 343 | |
| 344 | // phase 1: the search phase |
| 345 | _Unwind_Reason_Code phase1 = unwind_phase1(exception_object); |
| 346 | if (phase1 != _URC_NO_REASON) |
| 347 | return phase1; |
| 348 | |
| 349 | // phase 2: the clean up phase |
| 350 | return unwind_phase2(exception_object); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | |
| 355 | /// When _Unwind_RaiseException() is in phase2, it hands control |
| 356 | /// to the personality function at each frame. The personality |
| 357 | /// may force a jump to a landing pad in that function, the landing |
| 358 | /// pad code may then call _Unwind_Resume() to continue with the |
| 359 | /// unwinding. Note: the call to _Unwind_Resume() is from compiler |
Gabriel Ravier | dad00da | 2022-08-21 01:09:03 | [diff] [blame] | 360 | /// generated user code. All other _Unwind_* routines are called |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 361 | /// by the C++ runtime __cxa_* routines. |
| 362 | /// |
| 363 | /// Re-throwing an exception is implemented by having the code call |
| 364 | /// __cxa_rethrow() which in turn calls _Unwind_Resume_or_Rethrow() |
| 365 | _LIBUNWIND_EXPORT void |
| 366 | _Unwind_SjLj_Resume(struct _Unwind_Exception *exception_object) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 367 | _LIBUNWIND_TRACE_API("_Unwind_SjLj_Resume(ex_obj=%p)", |
| 368 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 369 | |
| 370 | if (exception_object->private_1 != 0) |
| 371 | unwind_phase2_forced(exception_object, |
| 372 | (_Unwind_Stop_Fn) exception_object->private_1, |
| 373 | (void *)exception_object->private_2); |
| 374 | else |
| 375 | unwind_phase2(exception_object); |
| 376 | |
| 377 | // clients assume _Unwind_Resume() does not return, so all we can do is abort. |
| 378 | _LIBUNWIND_ABORT("_Unwind_SjLj_Resume() can't return"); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /// Called by __cxa_rethrow(). |
| 383 | _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 384 | _Unwind_SjLj_Resume_or_Rethrow(struct _Unwind_Exception *exception_object) { |
| 385 | _LIBUNWIND_TRACE_API("__Unwind_SjLj_Resume_or_Rethrow(ex_obj=%p), " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 386 | "private_1=%" PRIuPTR, |
| 387 | (void *)exception_object, exception_object->private_1); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 388 | // If this is non-forced and a stopping place was found, then this is a |
| 389 | // re-throw. |
| 390 | // Call _Unwind_RaiseException() as if this was a new exception. |
| 391 | if (exception_object->private_1 == 0) { |
| 392 | return _Unwind_SjLj_RaiseException(exception_object); |
| 393 | // should return if there is no catch clause, so that __cxa_rethrow can call |
| 394 | // std::terminate() |
| 395 | } |
| 396 | |
Gabriel Ravier | dad00da | 2022-08-21 01:09:03 | [diff] [blame] | 397 | // Call through to _Unwind_Resume() which distinguishes between forced and |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 398 | // regular exceptions. |
| 399 | _Unwind_SjLj_Resume(exception_object); |
| 400 | _LIBUNWIND_ABORT("__Unwind_SjLj_Resume_or_Rethrow() called " |
| 401 | "_Unwind_SjLj_Resume() which unexpectedly returned"); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /// Called by personality handler during phase 2 to get LSDA for current frame. |
| 406 | _LIBUNWIND_EXPORT uintptr_t |
| 407 | _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { |
| 408 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
| 409 | _LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) " |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 410 | "=> 0x%" PRIuPTR, |
| 411 | (void *)context, ufc->lsda); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 412 | return ufc->lsda; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /// Called by personality handler during phase 2 to get register values. |
| 417 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, |
| 418 | int index) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 419 | _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d)", (void *)context, |
| 420 | index); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 421 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
| 422 | return ufc->resumeParameters[index]; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /// Called by personality handler during phase 2 to alter register values. |
| 427 | _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index, |
| 428 | uintptr_t new_value) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 429 | _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%" PRIuPTR |
| 430 | ")", |
| 431 | (void *)context, index, new_value); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 432 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
| 433 | ufc->resumeParameters[index] = new_value; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /// Called by personality handler during phase 2 to get instruction pointer. |
| 438 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) { |
| 439 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 440 | _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIu32, |
| 441 | (void *)context, ufc->resumeLocation + 1); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 442 | return ufc->resumeLocation + 1; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /// Called by personality handler during phase 2 to get instruction pointer. |
| 447 | /// ipBefore is a boolean that says if IP is already adjusted to be the call |
| 448 | /// site address. Normally IP is the return address. |
| 449 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context, |
| 450 | int *ipBefore) { |
| 451 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
| 452 | *ipBefore = 0; |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 453 | _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p, %p) => 0x%" PRIu32, |
| 454 | (void *)context, (void *)ipBefore, |
| 455 | ufc->resumeLocation + 1); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 456 | return ufc->resumeLocation + 1; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /// Called by personality handler during phase 2 to alter instruction pointer. |
| 461 | _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context, |
| 462 | uintptr_t new_value) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 463 | _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%" PRIuPTR ")", |
| 464 | (void *)context, new_value); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 465 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
| 466 | ufc->resumeLocation = new_value - 1; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /// Called by personality handler during phase 2 to find the start of the |
| 471 | /// function. |
| 472 | _LIBUNWIND_EXPORT uintptr_t |
| 473 | _Unwind_GetRegionStart(struct _Unwind_Context *context) { |
| 474 | // Not supported or needed for sjlj based unwinding |
| 475 | (void)context; |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 476 | _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p)", (void *)context); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /// Called by personality handler during phase 2 if a foreign exception |
| 482 | /// is caught. |
| 483 | _LIBUNWIND_EXPORT void |
| 484 | _Unwind_DeleteException(struct _Unwind_Exception *exception_object) { |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 485 | _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)", |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 486 | (void *)exception_object); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 487 | if (exception_object->exception_cleanup != NULL) |
| 488 | (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, |
| 489 | exception_object); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | |
| 494 | /// Called by personality handler during phase 2 to get base address for data |
| 495 | /// relative encodings. |
| 496 | _LIBUNWIND_EXPORT uintptr_t |
| 497 | _Unwind_GetDataRelBase(struct _Unwind_Context *context) { |
| 498 | // Not supported or needed for sjlj based unwinding |
| 499 | (void)context; |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 500 | _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 501 | _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented"); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | /// Called by personality handler during phase 2 to get base address for text |
| 506 | /// relative encodings. |
| 507 | _LIBUNWIND_EXPORT uintptr_t |
| 508 | _Unwind_GetTextRelBase(struct _Unwind_Context *context) { |
| 509 | // Not supported or needed for sjlj based unwinding |
| 510 | (void)context; |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 511 | _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 512 | _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented"); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /// Called by personality handler to get "Call Frame Area" for current frame. |
| 517 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) { |
Martin Storsjo | 94adf43 | 2019-01-22 20:50:39 | [diff] [blame] | 518 | _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p)", (void *)context); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 519 | if (context != NULL) { |
| 520 | _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context; |
| 521 | // Setjmp/longjmp based exceptions don't have a true CFA. |
| 522 | // Instead, the SP in the jmpbuf is the closest approximation. |
| 523 | return (uintptr_t) ufc->jbuf[2]; |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
Ranjeet Singh | 5808011 | 2017-03-31 15:28:06 | [diff] [blame] | 528 | #endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS) |