1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // exception.c -- |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Mon Jun 26 15:13:00 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Aug 10 16:45:22 2023 |
---|
13 | // Update Count : 69 |
---|
14 | // |
---|
15 | |
---|
16 | // Normally we would get this from the CFA prelude. |
---|
17 | #include <stddef.h> // for size_t |
---|
18 | |
---|
19 | #include <unwind.h> // for struct _Unwind_Exception {...}; |
---|
20 | |
---|
21 | #include "exception.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <bits/debug.hfa> |
---|
26 | #include "concurrency/invoke.h" |
---|
27 | #include "stdhdr/assert.h" |
---|
28 | #include "virtual.h" |
---|
29 | |
---|
30 | extern void __cabi_abort( const char fmt[], ... ); |
---|
31 | |
---|
32 | #pragma GCC visibility push(default) |
---|
33 | |
---|
34 | #include "lsda.h" |
---|
35 | |
---|
36 | /* The exception class for our exceptions. Because of the vendor component |
---|
37 | * its value would not be standard. |
---|
38 | * Vendor: UWPL |
---|
39 | * Language: CFA\0 |
---|
40 | */ |
---|
41 | const _Unwind_Exception_Class __cfaehm_exception_class = 0x4c50575500414643; |
---|
42 | |
---|
43 | // Base Exception type id: |
---|
44 | struct __cfavir_type_info __cfatid_exception_t = { |
---|
45 | NULL, |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | // Get the current exception context. |
---|
50 | // There can be a single global until multithreading occurs, then each stack |
---|
51 | // needs its own. We get this from libcfathreads (no weak attribute). |
---|
52 | __attribute__((weak)) struct exception_context_t * this_exception_context() { |
---|
53 | static struct exception_context_t shared_stack = {NULL, NULL}; |
---|
54 | return &shared_stack; |
---|
55 | } |
---|
56 | |
---|
57 | struct __cfaehm_base_exception_t * __cfaehm_get_current_termination(void) { |
---|
58 | return this_exception_context()->current_exception; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | // RESUMPTION ================================================================ |
---|
63 | |
---|
64 | static void reset_top_resume(struct __cfaehm_try_resume_node ** store) { |
---|
65 | this_exception_context()->top_resume = *store; |
---|
66 | } |
---|
67 | |
---|
68 | void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) { |
---|
69 | struct exception_context_t * context = this_exception_context(); |
---|
70 | |
---|
71 | __cfadbg_print_safe(exception, "Throwing resumption exception\n"); |
---|
72 | |
---|
73 | { |
---|
74 | __attribute__((cleanup(reset_top_resume))) |
---|
75 | struct __cfaehm_try_resume_node * original_head = context->top_resume; |
---|
76 | struct __cfaehm_try_resume_node * current = context->top_resume; |
---|
77 | |
---|
78 | for ( ; current ; current = current->next) { |
---|
79 | context->top_resume = current->next; |
---|
80 | if (current->handler(except)) { |
---|
81 | return; |
---|
82 | } |
---|
83 | } |
---|
84 | } // End the search and return to the top of the stack. |
---|
85 | |
---|
86 | // No handler found, fall back to the default operation. |
---|
87 | __cfadbg_print_safe(exception, "Unhandled exception\n"); |
---|
88 | defaultHandler(except); |
---|
89 | } |
---|
90 | |
---|
91 | // Do we control where exceptions get thrown even with concurency? |
---|
92 | // If not these are not quite thread safe, the cleanup hook has to |
---|
93 | // be added after the node is built but before it is made the top node. |
---|
94 | |
---|
95 | void __cfaehm_try_resume_setup(struct __cfaehm_try_resume_node * node, |
---|
96 | _Bool (*handler)(exception_t * except)) { |
---|
97 | struct exception_context_t * context = this_exception_context(); |
---|
98 | node->next = context->top_resume; |
---|
99 | node->handler = handler; |
---|
100 | context->top_resume = node; |
---|
101 | } |
---|
102 | |
---|
103 | void __cfaehm_try_resume_cleanup(struct __cfaehm_try_resume_node * node) { |
---|
104 | struct exception_context_t * context = this_exception_context(); |
---|
105 | context->top_resume = node->next; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | // MEMORY MANAGEMENT ========================================================= |
---|
110 | |
---|
111 | #define NODE_TO_EXCEPT(node) ((exception_t *)(1 + (node))) |
---|
112 | #define EXCEPT_TO_NODE(except) ((struct __cfaehm_node *)(except) - 1) |
---|
113 | #define UNWIND_TO_NODE(unwind) ((struct __cfaehm_node *)(unwind)) |
---|
114 | #define NULL_MAP(map, ptr) ((ptr) ? (map(ptr)) : NULL) |
---|
115 | |
---|
116 | // How to clean up an exception in various situations. |
---|
117 | static void __cfaehm_exception_cleanup( |
---|
118 | _Unwind_Reason_Code reason, |
---|
119 | struct _Unwind_Exception * exception) { |
---|
120 | switch (reason) { |
---|
121 | case _URC_FOREIGN_EXCEPTION_CAUGHT: |
---|
122 | // This one we could clean-up to allow cross-language exceptions. |
---|
123 | case _URC_FATAL_PHASE1_ERROR: |
---|
124 | case _URC_FATAL_PHASE2_ERROR: |
---|
125 | default: |
---|
126 | abort(); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // Creates a copy of the indicated exception and sets current_exception to it. |
---|
131 | void __cfaehm_allocate_exception( exception_t * except ) { |
---|
132 | struct exception_context_t * context = this_exception_context(); |
---|
133 | |
---|
134 | // Allocate memory for the exception. |
---|
135 | struct __cfaehm_node * store = malloc( |
---|
136 | sizeof( struct __cfaehm_node ) + except->virtual_table->size ); |
---|
137 | |
---|
138 | if ( ! store ) { |
---|
139 | // Failure: cannot allocate exception. Terminate thread. |
---|
140 | abort(); // <- Although I think it might be the process. |
---|
141 | } |
---|
142 | |
---|
143 | // Initialize the node: |
---|
144 | exception_t * except_store = NODE_TO_EXCEPT(store); |
---|
145 | store->unwind_exception.exception_class = __cfaehm_exception_class; |
---|
146 | store->unwind_exception.exception_cleanup = __cfaehm_exception_cleanup; |
---|
147 | store->handler_index = 0; |
---|
148 | except->virtual_table->copy( except_store, except ); |
---|
149 | |
---|
150 | // Add the node to the list: |
---|
151 | store->next = NULL_MAP(EXCEPT_TO_NODE, context->current_exception); |
---|
152 | context->current_exception = except_store; |
---|
153 | } |
---|
154 | |
---|
155 | // Delete the provided exception, unsetting current_exception if relivant. |
---|
156 | static void __cfaehm_delete_exception( exception_t * except ) { |
---|
157 | struct exception_context_t * context = this_exception_context(); |
---|
158 | |
---|
159 | __cfadbg_print_safe(exception, "Deleting Exception\n"); |
---|
160 | |
---|
161 | // Remove the exception from the list. |
---|
162 | struct __cfaehm_node * to_free = EXCEPT_TO_NODE(except); |
---|
163 | struct __cfaehm_node * node; |
---|
164 | |
---|
165 | if ( context->current_exception == except ) { |
---|
166 | node = to_free->next; |
---|
167 | context->current_exception = NULL_MAP(NODE_TO_EXCEPT, node); |
---|
168 | } else { |
---|
169 | node = EXCEPT_TO_NODE(context->current_exception); |
---|
170 | // It may always be in the first or second position. |
---|
171 | while ( to_free != node->next ) { |
---|
172 | node = node->next; |
---|
173 | } |
---|
174 | node->next = to_free->next; |
---|
175 | } |
---|
176 | |
---|
177 | // Free the old exception node. |
---|
178 | except->virtual_table->free( except ); |
---|
179 | free( to_free ); |
---|
180 | } |
---|
181 | |
---|
182 | // CANCELLATION ============================================================== |
---|
183 | |
---|
184 | // Function needed by force unwind |
---|
185 | // It basically says to unwind the whole stack and then exit when we reach the end of the stack |
---|
186 | static _Unwind_Reason_Code _Stop_Fn( |
---|
187 | int version, |
---|
188 | _Unwind_Action actions, |
---|
189 | _Unwind_Exception_Class exception_class, |
---|
190 | struct _Unwind_Exception * unwind_exception, |
---|
191 | struct _Unwind_Context * unwind_context, |
---|
192 | void * stop_param) { |
---|
193 | // Verify actions follow the rules we expect. |
---|
194 | verify(actions & _UA_CLEANUP_PHASE); |
---|
195 | verify(actions & _UA_FORCE_UNWIND); |
---|
196 | verify(!(actions & _UA_SEARCH_PHASE)); |
---|
197 | verify(!(actions & _UA_HANDLER_FRAME)); |
---|
198 | |
---|
199 | if ( actions & _UA_END_OF_STACK ) { |
---|
200 | __cabi_abort( |
---|
201 | "Propagation failed to find a matching handler.\n" |
---|
202 | "Possible cause is a missing try block with appropriate catch clause for specified exception type.\n" |
---|
203 | "Last exception name or message: %s.\n", |
---|
204 | NODE_TO_EXCEPT( UNWIND_TO_NODE( unwind_exception ) )-> |
---|
205 | virtual_table->msg( NODE_TO_EXCEPT( UNWIND_TO_NODE( unwind_exception ) ) ) |
---|
206 | ); |
---|
207 | } else { |
---|
208 | return _URC_NO_REASON; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | __attribute__((weak)) _Unwind_Reason_Code |
---|
213 | __cfaehm_cancellation_unwind( struct _Unwind_Exception * exception ) { |
---|
214 | return _Unwind_ForcedUnwind( exception, _Stop_Fn, (void*)0x22 ); |
---|
215 | } |
---|
216 | |
---|
217 | // Cancel the current stack, prefroming approprate clean-up and messaging. |
---|
218 | void __cfaehm_cancel_stack( exception_t * exception ) { |
---|
219 | __cfaehm_allocate_exception( exception ); |
---|
220 | |
---|
221 | struct exception_context_t * context = this_exception_context(); |
---|
222 | struct __cfaehm_node * node = EXCEPT_TO_NODE(context->current_exception); |
---|
223 | |
---|
224 | _Unwind_Reason_Code ret; |
---|
225 | ret = __cfaehm_cancellation_unwind( &node->unwind_exception ); |
---|
226 | printf("UNWIND ERROR %d after force unwind\n", ret); |
---|
227 | abort(); |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | // TERMINATION =============================================================== |
---|
232 | |
---|
233 | // If this isn't a rethrow (*except==0), delete the provided exception. |
---|
234 | void __cfaehm_cleanup_terminate( void * except ) { |
---|
235 | if ( *(void**)except ) __cfaehm_delete_exception( *(exception_t **)except ); |
---|
236 | } |
---|
237 | |
---|
238 | static void __cfaehm_cleanup_default( exception_t ** except ) { |
---|
239 | __cfaehm_delete_exception( *except ); |
---|
240 | *except = NULL; |
---|
241 | } |
---|
242 | |
---|
243 | // The exception that is being thrown must already be stored. |
---|
244 | void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) { |
---|
245 | struct exception_context_t * context = this_exception_context(); |
---|
246 | if ( NULL == context->current_exception ) { |
---|
247 | printf("UNWIND ERROR missing exception in begin unwind\n"); |
---|
248 | abort(); |
---|
249 | } |
---|
250 | struct _Unwind_Exception * storage = |
---|
251 | &EXCEPT_TO_NODE(context->current_exception)->unwind_exception; |
---|
252 | |
---|
253 | // Call stdlibc to raise the exception |
---|
254 | __cfadbg_print_safe(exception, "Begin unwinding (storage &p, context %p)\n", storage, context); |
---|
255 | _Unwind_Reason_Code ret = _Unwind_RaiseException( storage ); |
---|
256 | |
---|
257 | // If we reach here it means something happened. For resumption to work we need to find a way |
---|
258 | // to return back to here. Most of them will probably boil down to setting a global flag and |
---|
259 | // making the phase 1 either stop or fail. Causing an error on purpose may help avoiding |
---|
260 | // unnecessary work but it might have some weird side effects. If we just pretend no handler |
---|
261 | // was found that would work but may be expensive for no reason since we will always search |
---|
262 | // the whole stack. |
---|
263 | |
---|
264 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
265 | // We did not simply reach the end of the stack without finding a handler. This is an error. |
---|
266 | if ( ret != _URC_END_OF_STACK ) { |
---|
267 | #else // defined( __ARM_ARCH ) |
---|
268 | // The return code from _Unwind_RaiseException seems to be corrupt on ARM at end of stack. |
---|
269 | // This workaround tries to keep default exception handling working. |
---|
270 | if ( ret == _URC_FATAL_PHASE1_ERROR || ret == _URC_FATAL_PHASE2_ERROR ) { |
---|
271 | #endif |
---|
272 | printf("UNWIND ERROR %d after raise exception\n", ret); |
---|
273 | abort(); |
---|
274 | } |
---|
275 | |
---|
276 | // No handler found, go to the default operation. |
---|
277 | __cfadbg_print_safe(exception, "Uncaught exception %p\n", storage); |
---|
278 | |
---|
279 | __attribute__((cleanup(__cfaehm_cleanup_default))) |
---|
280 | exception_t * exception = context->current_exception; |
---|
281 | defaultHandler( exception ); |
---|
282 | } |
---|
283 | |
---|
284 | void __cfaehm_throw_terminate( exception_t * val, void (*defaultHandler)(exception_t *) ) { |
---|
285 | __cfadbg_print_safe(exception, "Throwing termination exception\n"); |
---|
286 | |
---|
287 | __cfaehm_allocate_exception( val ); |
---|
288 | __cfaehm_begin_unwind( defaultHandler ); |
---|
289 | } |
---|
290 | |
---|
291 | static __attribute__((noreturn)) void __cfaehm_rethrow_adapter( exception_t * except ) { |
---|
292 | // TODO: Print some error message. |
---|
293 | (void)except; |
---|
294 | abort(); |
---|
295 | } |
---|
296 | |
---|
297 | void __cfaehm_rethrow_terminate(void) { |
---|
298 | __cfadbg_print_safe(exception, "Rethrowing termination exception\n"); |
---|
299 | |
---|
300 | __cfaehm_begin_unwind( __cfaehm_rethrow_adapter ); |
---|
301 | abort(); |
---|
302 | } |
---|
303 | |
---|
304 | #if defined( __x86_64 ) || defined( __i386 ) || defined( __ARM_ARCH ) |
---|
305 | // This is our personality routine. For every stack frame annotated with |
---|
306 | // ".cfi_personality 0x3,__gcfa_personality_v0" this function will be called twice when unwinding. |
---|
307 | // Once in the search phase and once in the cleanup phase. |
---|
308 | _Unwind_Reason_Code __gcfa_personality_v0( |
---|
309 | int version, |
---|
310 | _Unwind_Action actions, |
---|
311 | unsigned long long exception_class, |
---|
312 | struct _Unwind_Exception * unwind_exception, |
---|
313 | struct _Unwind_Context * unwind_context) |
---|
314 | { |
---|
315 | __cfadbg_print_safe(exception, "CFA: 0x%p\n", (void*)_Unwind_GetCFA(unwind_context)); |
---|
316 | __cfadbg_print_safe(exception, "Personality function (%d, %x, %llu, %p, %p):", |
---|
317 | version, actions, exception_class, unwind_exception, unwind_context); |
---|
318 | |
---|
319 | // Verify that actions follow the rules we expect. |
---|
320 | // This function should never be called at the end of the stack. |
---|
321 | verify(!(actions & _UA_END_OF_STACK)); |
---|
322 | // Either only the search phase flag is set or... |
---|
323 | if (actions & _UA_SEARCH_PHASE) { |
---|
324 | verify(actions == _UA_SEARCH_PHASE); |
---|
325 | __cfadbg_print_safe(exception, " lookup phase"); |
---|
326 | // ... we are in clean-up phase. |
---|
327 | } else { |
---|
328 | verify(actions & _UA_CLEANUP_PHASE); |
---|
329 | __cfadbg_print_safe(exception, " cleanup phase"); |
---|
330 | // We shouldn't be the handler frame during forced unwind. |
---|
331 | if (actions & _UA_HANDLER_FRAME) { |
---|
332 | verify(!(actions & _UA_FORCE_UNWIND)); |
---|
333 | __cfadbg_print_safe(exception, " (handler frame)"); |
---|
334 | } else if (actions & _UA_FORCE_UNWIND) { |
---|
335 | __cfadbg_print_safe(exception, " (force unwind)"); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | // Get a pointer to the language specific data from which we will read what we need |
---|
340 | const unsigned char * lsd = _Unwind_GetLanguageSpecificData( unwind_context ); |
---|
341 | |
---|
342 | if ( !lsd ) { //Nothing to do, keep unwinding |
---|
343 | printf(" no LSD"); |
---|
344 | goto UNWIND; |
---|
345 | } |
---|
346 | |
---|
347 | // Get the instuction pointer and a reading pointer into the exception table |
---|
348 | lsda_header_info lsd_info; |
---|
349 | const unsigned char * cur_ptr = parse_lsda_header(unwind_context, lsd, &lsd_info); |
---|
350 | _Unwind_Ptr instruction_ptr = _Unwind_GetIP(unwind_context); |
---|
351 | |
---|
352 | struct exception_context_t * context = this_exception_context(); |
---|
353 | |
---|
354 | // Linearly search the table for stuff to do |
---|
355 | while ( cur_ptr < lsd_info.action_table ) { |
---|
356 | _Unwind_Ptr callsite_start; |
---|
357 | _Unwind_Ptr callsite_len; |
---|
358 | _Unwind_Ptr callsite_landing_pad; |
---|
359 | _uleb128_t callsite_action; |
---|
360 | |
---|
361 | // Decode the common stuff we have in here |
---|
362 | cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_start); |
---|
363 | cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_len); |
---|
364 | cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad); |
---|
365 | cur_ptr = read_uleb128(cur_ptr, &callsite_action); |
---|
366 | |
---|
367 | // Have we reach the correct frame info yet? |
---|
368 | if ( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) { |
---|
369 | #ifdef __CFA_DEBUG_PRINT__ |
---|
370 | void * ls = (void*)lsd_info.Start; |
---|
371 | void * cs = (void*)callsite_start; |
---|
372 | void * cl = (void*)callsite_len; |
---|
373 | void * bp = (void*)lsd_info.Start + callsite_start; |
---|
374 | void * ep = (void*)lsd_info.Start + callsite_start + callsite_len; |
---|
375 | void * ip = (void*)instruction_ptr; |
---|
376 | __cfadbg_print_safe(exception, "\nfound %p - %p (%p, %p, %p), looking for %p\n", |
---|
377 | bp, ep, ls, cs, cl, ip); |
---|
378 | #endif // __CFA_DEBUG_PRINT__ |
---|
379 | continue; |
---|
380 | } |
---|
381 | |
---|
382 | // Have we gone too far? |
---|
383 | if ( lsd_info.Start + callsite_start > instruction_ptr ) { |
---|
384 | printf(" gone too far"); |
---|
385 | break; |
---|
386 | } |
---|
387 | |
---|
388 | // Check for what we must do: |
---|
389 | if ( 0 == callsite_landing_pad ) { |
---|
390 | // Nothing to do, move along |
---|
391 | __cfadbg_print_safe(exception, " no landing pad"); |
---|
392 | } else if (actions & _UA_SEARCH_PHASE) { |
---|
393 | // In search phase, these means we found a potential handler we must check. |
---|
394 | |
---|
395 | // We have arbitrarily decided that 0 means nothing to do and 1 means there is |
---|
396 | // a potential handler. This doesn't seem to conflict the gcc default behavior. |
---|
397 | if (callsite_action != 0) { |
---|
398 | // Now we want to run some code to see if the handler matches |
---|
399 | // This is the tricky part where we want to the power to run arbitrary code |
---|
400 | // However, generating a new exception table entry and try routine every time |
---|
401 | // is way more expansive than we might like |
---|
402 | // The information we have is : |
---|
403 | // - The GR (Series of registers) |
---|
404 | // GR1=GP Global Pointer of frame ref by context |
---|
405 | // - The instruction pointer |
---|
406 | // - The instruction pointer info (???) |
---|
407 | // - The CFA (Canonical Frame Address) |
---|
408 | // - The BSP (Probably the base stack pointer) |
---|
409 | |
---|
410 | // The current apprach uses one exception table entry per try block |
---|
411 | _uleb128_t imatcher; |
---|
412 | // Get the relative offset to the {...}? |
---|
413 | cur_ptr = read_uleb128(cur_ptr, &imatcher); |
---|
414 | |
---|
415 | _Unwind_Word match_pos = |
---|
416 | # if defined( __x86_64 ) |
---|
417 | _Unwind_GetCFA(unwind_context); |
---|
418 | # elif defined( __i386 ) |
---|
419 | _Unwind_GetCFA(unwind_context) + 20; |
---|
420 | # elif defined( __ARM_ARCH ) |
---|
421 | _Unwind_GetCFA(unwind_context) + 16; |
---|
422 | # endif |
---|
423 | //! printf("match_pos: %p\n", (void*)match_pos); |
---|
424 | //! fflush(stdout); |
---|
425 | int (*matcher)(exception_t *) = *(int(**)(exception_t *))match_pos; |
---|
426 | |
---|
427 | int index = matcher(context->current_exception); |
---|
428 | _Unwind_Reason_Code ret = (0 == index) |
---|
429 | ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND; |
---|
430 | UNWIND_TO_NODE(unwind_exception)->handler_index = index; |
---|
431 | |
---|
432 | // Based on the return value, check if we matched the exception |
---|
433 | if (ret == _URC_HANDLER_FOUND) { |
---|
434 | __cfadbg_print_safe(exception, " handler found\n"); |
---|
435 | } else { |
---|
436 | // TODO: Continue the search if there is more in the table. |
---|
437 | __cfadbg_print_safe(exception, " no handler\n"); |
---|
438 | } |
---|
439 | return ret; |
---|
440 | } |
---|
441 | |
---|
442 | // This is only a cleanup handler, ignore it |
---|
443 | __cfadbg_print_safe(exception, " no action"); |
---|
444 | } else { |
---|
445 | // In clean-up phase, no destructors here but this could be the handler. |
---|
446 | |
---|
447 | if ( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){ |
---|
448 | // If this is a potential exception handler |
---|
449 | // but not the one that matched the exception in the seach phase, |
---|
450 | // just ignore it |
---|
451 | goto UNWIND; |
---|
452 | } |
---|
453 | |
---|
454 | // We need to run some clean-up or a handler |
---|
455 | // These statment do the right thing but I don't know any specifics at all |
---|
456 | _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(0), |
---|
457 | (_Unwind_Ptr)unwind_exception ); |
---|
458 | _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(1), 0 ); |
---|
459 | |
---|
460 | // I assume this sets the instruction pointer to the adress of the landing pad |
---|
461 | // It doesn't actually set it, it only state the value that needs to be set once we |
---|
462 | // return _URC_INSTALL_CONTEXT |
---|
463 | _Unwind_SetIP( unwind_context, ((lsd_info.LPStart) + (callsite_landing_pad)) ); |
---|
464 | |
---|
465 | __cfadbg_print_safe(exception, " action\n"); |
---|
466 | |
---|
467 | // Return have some action to run |
---|
468 | return _URC_INSTALL_CONTEXT; |
---|
469 | } |
---|
470 | } |
---|
471 | // No handling found |
---|
472 | __cfadbg_print_safe(exception, " table end reached"); |
---|
473 | |
---|
474 | UNWIND: |
---|
475 | __cfadbg_print_safe(exception, " unwind\n"); |
---|
476 | |
---|
477 | // Keep unwinding the stack |
---|
478 | return _URC_CONTINUE_UNWIND; |
---|
479 | } |
---|
480 | |
---|
481 | #pragma GCC push_options |
---|
482 | #pragma GCC optimize(0) |
---|
483 | |
---|
484 | // Try statements are hoisted out see comments for details. While this could probably be unique |
---|
485 | // and simply linked from libcfa but there is one problem left, see the exception table for details |
---|
486 | __attribute__((noinline)) |
---|
487 | int __cfaehm_try_terminate(void (*try_block)(), |
---|
488 | __attribute__((unused)) int (*match_block)(exception_t * except)) { |
---|
489 | //! volatile int xy = 0; |
---|
490 | //! printf("%p %p %p\n", &try_block, &match_block, &xy); |
---|
491 | //! fflush(stdout); |
---|
492 | |
---|
493 | // Setup the personality routine and exception table. |
---|
494 | // Unforturnately these clobber gcc cancellation support which means we can't get access to |
---|
495 | // the attribute cleanup tables at the same time. We would have to inspect the assembly to |
---|
496 | // create a new set ourselves. |
---|
497 | #ifdef __PIC__ |
---|
498 | asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0"); |
---|
499 | asm volatile (".cfi_lsda 0x1b, .LLSDACFA2"); |
---|
500 | #else |
---|
501 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0"); |
---|
502 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2"); |
---|
503 | #endif |
---|
504 | |
---|
505 | // Label which defines the start of the area for which the handler is setup. |
---|
506 | asm volatile (".TRYSTART:"); |
---|
507 | |
---|
508 | // The actual statements of the try blocks |
---|
509 | try_block(); |
---|
510 | |
---|
511 | // asm statement to prevent deadcode removal |
---|
512 | asm volatile goto ("" : : : : CATCH ); |
---|
513 | |
---|
514 | // Normal return for when there is no throw. |
---|
515 | return 0; |
---|
516 | |
---|
517 | // Exceptionnal path |
---|
518 | CATCH : __attribute__(( unused )); |
---|
519 | // Label which defines the end of the area for which the handler is setup. |
---|
520 | asm volatile (".TRYEND:"); |
---|
521 | // Label which defines the start of the exception landing pad. Basically what is called when |
---|
522 | // the exception is caught. Note, if multiple handlers are given, the multiplexing should be |
---|
523 | // done by the generated code, not the exception runtime. |
---|
524 | asm volatile (".CATCH:"); |
---|
525 | |
---|
526 | return EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index; |
---|
527 | } |
---|
528 | |
---|
529 | // Exception table data we need to generate. While this is almost generic, the custom data refers |
---|
530 | // to {*}try_terminate, which is no way generic. Some more works need to be done if we want to |
---|
531 | // have a single call to the try routine. |
---|
532 | |
---|
533 | #ifdef __PIC__ |
---|
534 | asm ( |
---|
535 | // HEADER |
---|
536 | ".LFECFA1:\n" |
---|
537 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
538 | " .globl __gcfa_personality_v0\n" |
---|
539 | #else // defined( __ARM_ARCH ) |
---|
540 | " .global __gcfa_personality_v0\n" |
---|
541 | #endif |
---|
542 | " .section .gcc_except_table,\"a\",@progbits\n" |
---|
543 | // TABLE HEADER (important field is the BODY length at the end) |
---|
544 | ".LLSDACFA2:\n" |
---|
545 | " .byte 0xff\n" |
---|
546 | " .byte 0xff\n" |
---|
547 | " .byte 0x1\n" |
---|
548 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" |
---|
549 | // BODY (language specific data) |
---|
550 | // This uses language specific data and can be modified arbitrarily |
---|
551 | // We use handled area offset, handled area length, |
---|
552 | // handler landing pad offset and 1 (action code, gcc seems to use 0). |
---|
553 | ".LLSDACSBCFA2:\n" |
---|
554 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
555 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
556 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
557 | " .uleb128 1\n" |
---|
558 | ".LLSDACSECFA2:\n" |
---|
559 | // TABLE FOOTER |
---|
560 | " .text\n" |
---|
561 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
562 | ); |
---|
563 | |
---|
564 | // Somehow this piece of helps with the resolution of debug symbols. |
---|
565 | __attribute__((unused)) static const int dummy = 0; |
---|
566 | |
---|
567 | asm ( |
---|
568 | // Add a hidden symbol which points at the function. |
---|
569 | " .hidden CFA.ref.__gcfa_personality_v0\n" |
---|
570 | " .weak CFA.ref.__gcfa_personality_v0\n" |
---|
571 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
572 | " .align 8\n" |
---|
573 | #else // defined( __ARM_ARCH ) |
---|
574 | " .align 3\n" |
---|
575 | #endif |
---|
576 | " .type CFA.ref.__gcfa_personality_v0, @object\n" |
---|
577 | " .size CFA.ref.__gcfa_personality_v0, 8\n" |
---|
578 | "CFA.ref.__gcfa_personality_v0:\n" |
---|
579 | #if defined( __x86_64 ) |
---|
580 | " .quad __gcfa_personality_v0\n" |
---|
581 | #elif defined( __i386 ) |
---|
582 | " .long __gcfa_personality_v0\n" |
---|
583 | #else // defined( __ARM_ARCH ) |
---|
584 | " .xword __gcfa_personality_v0\n" |
---|
585 | #endif |
---|
586 | ); |
---|
587 | #else // __PIC__ |
---|
588 | asm ( |
---|
589 | // HEADER |
---|
590 | ".LFECFA1:\n" |
---|
591 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
592 | " .globl __gcfa_personality_v0\n" |
---|
593 | #else // defined( __ARM_ARCH ) |
---|
594 | " .global __gcfa_personality_v0\n" |
---|
595 | #endif |
---|
596 | " .section .gcc_except_table,\"a\",@progbits\n" |
---|
597 | // TABLE HEADER (important field is the BODY length at the end) |
---|
598 | ".LLSDACFA2:\n" |
---|
599 | " .byte 0xff\n" |
---|
600 | " .byte 0xff\n" |
---|
601 | " .byte 0x1\n" |
---|
602 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" |
---|
603 | // BODY (language specific data) |
---|
604 | ".LLSDACSBCFA2:\n" |
---|
605 | // Handled area start (relative to start of function) |
---|
606 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
607 | // Handled area length |
---|
608 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
609 | // Handler landing pad address (relative to start of function) |
---|
610 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
611 | // Action code, gcc seems to always use 0. |
---|
612 | " .uleb128 1\n" |
---|
613 | // TABLE FOOTER |
---|
614 | ".LLSDACSECFA2:\n" |
---|
615 | " .text\n" |
---|
616 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
617 | " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n" |
---|
618 | " .section .note.GNU-stack,\"x\",@progbits\n" |
---|
619 | ); |
---|
620 | #endif // __PIC__ |
---|
621 | |
---|
622 | #pragma GCC pop_options |
---|
623 | |
---|
624 | #else |
---|
625 | #error unsupported hardware architecture |
---|
626 | #endif // __x86_64 || __i386 || __ARM_ARCH |
---|