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