annotate libunwind/src/Unwind-sjlj.c @ 182:0f533c0a1429

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