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 : Andrew Beach |
---|
12 | // Last Modified On : Wed Feb 24 13:40:00 2021 |
---|
13 | // Update Count : 36 |
---|
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 | #if defined( __ARM_ARCH ) |
---|
31 | #warning FIX ME: temporary hack to keep ARM build working |
---|
32 | #ifndef _URC_FATAL_PHASE1_ERROR |
---|
33 | #define _URC_FATAL_PHASE1_ERROR 3 |
---|
34 | #endif // ! _URC_FATAL_PHASE1_ERROR |
---|
35 | #ifndef _URC_FATAL_PHASE2_ERROR |
---|
36 | #define _URC_FATAL_PHASE2_ERROR 2 |
---|
37 | #endif // ! _URC_FATAL_PHASE2_ERROR |
---|
38 | #endif // __ARM_ARCH |
---|
39 | |
---|
40 | #include "lsda.h" |
---|
41 | |
---|
42 | /* The exception class for our exceptions. Because of the vendor component |
---|
43 | * its value would not be standard. |
---|
44 | * Vendor: UWPL |
---|
45 | * Language: CFA\0 |
---|
46 | */ |
---|
47 | const _Unwind_Exception_Class __cfaehm_exception_class = 0x4c50575500414643; |
---|
48 | |
---|
49 | // Base Exception type id: |
---|
50 | struct __cfa__parent_vtable __cfatid_exception_t = { |
---|
51 | NULL, |
---|
52 | }; |
---|
53 | |
---|
54 | |
---|
55 | // Get the current exception context. |
---|
56 | // There can be a single global until multithreading occurs, then each stack |
---|
57 | // needs its own. We get this from libcfathreads (no weak attribute). |
---|
58 | __attribute__((weak)) struct exception_context_t * this_exception_context() { |
---|
59 | static struct exception_context_t shared_stack = {NULL, NULL}; |
---|
60 | return &shared_stack; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | // RESUMPTION ================================================================ |
---|
65 | |
---|
66 | static void reset_top_resume(struct __cfaehm_try_resume_node ** store) { |
---|
67 | this_exception_context()->top_resume = *store; |
---|
68 | } |
---|
69 | |
---|
70 | void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) { |
---|
71 | struct exception_context_t * context = this_exception_context(); |
---|
72 | |
---|
73 | __cfadbg_print_safe(exception, "Throwing resumption exception\n"); |
---|
74 | |
---|
75 | { |
---|
76 | __attribute__((cleanup(reset_top_resume))) |
---|
77 | struct __cfaehm_try_resume_node * original_head = context->top_resume; |
---|
78 | struct __cfaehm_try_resume_node * current = context->top_resume; |
---|
79 | |
---|
80 | for ( ; current ; current = current->next) { |
---|
81 | context->top_resume = current->next; |
---|
82 | if (current->handler(except)) { |
---|
83 | return; |
---|
84 | } |
---|
85 | } |
---|
86 | } // End the search and return to the top of the stack. |
---|
87 | |
---|
88 | // No handler found, fall back to the default operation. |
---|
89 | __cfadbg_print_safe(exception, "Unhandled exception\n"); |
---|
90 | defaultHandler(except); |
---|
91 | } |
---|
92 | |
---|
93 | // Do we control where exceptions get thrown even with concurency? |
---|
94 | // If not these are not quite thread safe, the cleanup hook has to |
---|
95 | // be added after the node is built but before it is made the top node. |
---|
96 | |
---|
97 | void __cfaehm_try_resume_setup(struct __cfaehm_try_resume_node * node, |
---|
98 | _Bool (*handler)(exception_t * except)) { |
---|
99 | struct exception_context_t * context = this_exception_context(); |
---|
100 | node->next = context->top_resume; |
---|
101 | node->handler = handler; |
---|
102 | context->top_resume = node; |
---|
103 | } |
---|
104 | |
---|
105 | void __cfaehm_try_resume_cleanup(struct __cfaehm_try_resume_node * node) { |
---|
106 | struct exception_context_t * context = this_exception_context(); |
---|
107 | context->top_resume = node->next; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | // MEMORY MANAGEMENT ========================================================= |
---|
112 | |
---|
113 | #define NODE_TO_EXCEPT(node) ((exception_t *)(1 + (node))) |
---|
114 | #define EXCEPT_TO_NODE(except) ((struct __cfaehm_node *)(except) - 1) |
---|
115 | #define UNWIND_TO_NODE(unwind) ((struct __cfaehm_node *)(unwind)) |
---|
116 | #define NULL_MAP(map, ptr) ((ptr) ? (map(ptr)) : NULL) |
---|
117 | |
---|
118 | // How to clean up an exception in various situations. |
---|
119 | static void __cfaehm_exception_cleanup( |
---|
120 | _Unwind_Reason_Code reason, |
---|
121 | struct _Unwind_Exception * exception) { |
---|
122 | switch (reason) { |
---|
123 | case _URC_FOREIGN_EXCEPTION_CAUGHT: |
---|
124 | // This one we could clean-up to allow cross-language exceptions. |
---|
125 | case _URC_FATAL_PHASE1_ERROR: |
---|
126 | case _URC_FATAL_PHASE2_ERROR: |
---|
127 | default: |
---|
128 | abort(); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | // Creates a copy of the indicated exception and sets current_exception to it. |
---|
133 | static void __cfaehm_allocate_exception( exception_t * except ) { |
---|
134 | struct exception_context_t * context = this_exception_context(); |
---|
135 | |
---|
136 | // Allocate memory for the exception. |
---|
137 | struct __cfaehm_node * store = malloc( |
---|
138 | sizeof( struct __cfaehm_node ) + except->virtual_table->size ); |
---|
139 | |
---|
140 | if ( ! store ) { |
---|
141 | // Failure: cannot allocate exception. Terminate thread. |
---|
142 | abort(); // <- Although I think it might be the process. |
---|
143 | } |
---|
144 | |
---|
145 | // Initialize the node: |
---|
146 | exception_t * except_store = NODE_TO_EXCEPT(store); |
---|
147 | store->unwind_exception.exception_class = __cfaehm_exception_class; |
---|
148 | store->unwind_exception.exception_cleanup = __cfaehm_exception_cleanup; |
---|
149 | store->handler_index = 0; |
---|
150 | except->virtual_table->copy( except_store, except ); |
---|
151 | |
---|
152 | // Add the node to the list: |
---|
153 | store->next = NULL_MAP(EXCEPT_TO_NODE, context->current_exception); |
---|
154 | context->current_exception = except_store; |
---|
155 | } |
---|
156 | |
---|
157 | // Delete the provided exception, unsetting current_exception if relivant. |
---|
158 | static void __cfaehm_delete_exception( exception_t * except ) { |
---|
159 | struct exception_context_t * context = this_exception_context(); |
---|
160 | |
---|
161 | __cfadbg_print_safe(exception, "Deleting Exception\n"); |
---|
162 | |
---|
163 | // Remove the exception from the list. |
---|
164 | struct __cfaehm_node * to_free = EXCEPT_TO_NODE(except); |
---|
165 | struct __cfaehm_node * node; |
---|
166 | |
---|
167 | if ( context->current_exception == except ) { |
---|
168 | node = to_free->next; |
---|
169 | context->current_exception = NULL_MAP(NODE_TO_EXCEPT, node); |
---|
170 | } else { |
---|
171 | node = EXCEPT_TO_NODE(context->current_exception); |
---|
172 | // It may always be in the first or second position. |
---|
173 | while ( to_free != node->next ) { |
---|
174 | node = node->next; |
---|
175 | } |
---|
176 | node->next = to_free->next; |
---|
177 | } |
---|
178 | |
---|
179 | // Free the old exception node. |
---|
180 | except->virtual_table->free( except ); |
---|
181 | free( to_free ); |
---|
182 | } |
---|
183 | |
---|
184 | // CANCELLATION ============================================================== |
---|
185 | |
---|
186 | // Function needed by force unwind |
---|
187 | // It basically says to unwind the whole stack and then exit when we reach the end of the stack |
---|
188 | static _Unwind_Reason_Code _Stop_Fn( |
---|
189 | int version, |
---|
190 | _Unwind_Action actions, |
---|
191 | _Unwind_Exception_Class exception_class, |
---|
192 | struct _Unwind_Exception * unwind_exception, |
---|
193 | struct _Unwind_Context * unwind_context, |
---|
194 | void * stop_param) { |
---|
195 | // Verify actions follow the rules we expect. |
---|
196 | verify(actions & _UA_CLEANUP_PHASE); |
---|
197 | verify(actions & _UA_FORCE_UNWIND); |
---|
198 | verify(!(actions & _UA_SEARCH_PHASE)); |
---|
199 | verify(!(actions & _UA_HANDLER_FRAME)); |
---|
200 | |
---|
201 | if ( actions & _UA_END_OF_STACK ) { |
---|
202 | abort(); |
---|
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 | // Preform clean-up of any extra active exceptions. |
---|
221 | while ( node->next ) { |
---|
222 | struct __cfaehm_node * to_free = node->next; |
---|
223 | node->next = to_free->next; |
---|
224 | exception_t * except = NODE_TO_EXCEPT( to_free ); |
---|
225 | except->virtual_table->free( except ); |
---|
226 | free( to_free ); |
---|
227 | } |
---|
228 | |
---|
229 | _Unwind_Reason_Code ret; |
---|
230 | ret = __cfaehm_cancellation_unwind( &node->unwind_exception ); |
---|
231 | printf("UNWIND ERROR %d after force unwind\n", ret); |
---|
232 | abort(); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | // TERMINATION =============================================================== |
---|
237 | |
---|
238 | // If this isn't a rethrow (*except==0), delete the provided exception. |
---|
239 | void __cfaehm_cleanup_terminate( void * except ) { |
---|
240 | if ( *(void**)except ) __cfaehm_delete_exception( *(exception_t **)except ); |
---|
241 | } |
---|
242 | |
---|
243 | static void __cfaehm_cleanup_default( exception_t ** except ) { |
---|
244 | __cfaehm_delete_exception( *except ); |
---|
245 | *except = NULL; |
---|
246 | } |
---|
247 | |
---|
248 | // The exception that is being thrown must already be stored. |
---|
249 | static void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) { |
---|
250 | struct exception_context_t * context = this_exception_context(); |
---|
251 | if ( NULL == context->current_exception ) { |
---|
252 | printf("UNWIND ERROR missing exception in begin unwind\n"); |
---|
253 | abort(); |
---|
254 | } |
---|
255 | struct _Unwind_Exception * storage = |
---|
256 | &EXCEPT_TO_NODE(context->current_exception)->unwind_exception; |
---|
257 | |
---|
258 | // Call stdlibc to raise the exception |
---|
259 | __cfadbg_print_safe(exception, "Begin unwinding (storage &p, context %p)\n", storage, context); |
---|
260 | _Unwind_Reason_Code ret = _Unwind_RaiseException( storage ); |
---|
261 | |
---|
262 | // If we reach here it means something happened. For resumption to work we need to find a way |
---|
263 | // to return back to here. Most of them will probably boil down to setting a global flag and |
---|
264 | // making the phase 1 either stop or fail. Causing an error on purpose may help avoiding |
---|
265 | // unnecessary work but it might have some weird side effects. If we just pretend no handler |
---|
266 | // was found that would work but may be expensive for no reason since we will always search |
---|
267 | // the whole stack. |
---|
268 | |
---|
269 | // We did not simply reach the end of the stack without finding a handler. This is an error. |
---|
270 | if ( ret != _URC_END_OF_STACK ) { |
---|
271 | printf("UNWIND ERROR %d after raise exception\n", ret); |
---|
272 | abort(); |
---|
273 | } |
---|
274 | |
---|
275 | // No handler found, go to the default operation. |
---|
276 | __cfadbg_print_safe(exception, "Uncaught exception %p\n", storage); |
---|
277 | |
---|
278 | __attribute__((cleanup(__cfaehm_cleanup_default))) |
---|
279 | exception_t * exception = context->current_exception; |
---|
280 | defaultHandler( exception ); |
---|
281 | } |
---|
282 | |
---|
283 | void __cfaehm_throw_terminate( exception_t * val, void (*defaultHandler)(exception_t *) ) { |
---|
284 | __cfadbg_print_safe(exception, "Throwing termination exception\n"); |
---|
285 | |
---|
286 | __cfaehm_allocate_exception( val ); |
---|
287 | __cfaehm_begin_unwind( defaultHandler ); |
---|
288 | } |
---|
289 | |
---|
290 | static __attribute__((noreturn)) void __cfaehm_rethrow_adapter( exception_t * except ) { |
---|
291 | // TODO: Print some error message. |
---|
292 | (void)except; |
---|
293 | abort(); |
---|
294 | } |
---|
295 | |
---|
296 | void __cfaehm_rethrow_terminate(void) { |
---|
297 | __cfadbg_print_safe(exception, "Rethrowing termination exception\n"); |
---|
298 | |
---|
299 | __cfaehm_begin_unwind( __cfaehm_rethrow_adapter ); |
---|
300 | abort(); |
---|
301 | } |
---|
302 | |
---|
303 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
304 | // This is our personality routine. For every stack frame annotated with |
---|
305 | // ".cfi_personality 0x3,__gcfa_personality_v0" this function will be called twice when unwinding. |
---|
306 | // Once in the search phase and once in the cleanup phase. |
---|
307 | _Unwind_Reason_Code __gcfa_personality_v0( |
---|
308 | int version, |
---|
309 | _Unwind_Action actions, |
---|
310 | unsigned long long exception_class, |
---|
311 | struct _Unwind_Exception * unwind_exception, |
---|
312 | struct _Unwind_Context * unwind_context) |
---|
313 | { |
---|
314 | |
---|
315 | //__cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(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) + 8; |
---|
418 | # elif defined( __i386 ) |
---|
419 | _Unwind_GetCFA(unwind_context) + 24; |
---|
420 | # elif defined( __ARM_ARCH ) |
---|
421 | # warning FIX ME: check if anything needed for ARM |
---|
422 | 42; |
---|
423 | # endif |
---|
424 | int (*matcher)(exception_t *) = *(int(**)(exception_t *))match_pos; |
---|
425 | |
---|
426 | int index = matcher(context->current_exception); |
---|
427 | _Unwind_Reason_Code ret = (0 == index) |
---|
428 | ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND; |
---|
429 | UNWIND_TO_NODE(unwind_exception)->handler_index = index; |
---|
430 | |
---|
431 | // Based on the return value, check if we matched the exception |
---|
432 | if (ret == _URC_HANDLER_FOUND) { |
---|
433 | __cfadbg_print_safe(exception, " handler found\n"); |
---|
434 | } else { |
---|
435 | // TODO: Continue the search if there is more in the table. |
---|
436 | __cfadbg_print_safe(exception, " no handler\n"); |
---|
437 | } |
---|
438 | return ret; |
---|
439 | } |
---|
440 | |
---|
441 | // This is only a cleanup handler, ignore it |
---|
442 | __cfadbg_print_safe(exception, " no action"); |
---|
443 | } else { |
---|
444 | // In clean-up phase, no destructors here but this could be the handler. |
---|
445 | |
---|
446 | if ( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){ |
---|
447 | // If this is a potential exception handler |
---|
448 | // but not the one that matched the exception in the seach phase, |
---|
449 | // just ignore it |
---|
450 | goto UNWIND; |
---|
451 | } |
---|
452 | |
---|
453 | // We need to run some clean-up or a handler |
---|
454 | // These statment do the right thing but I don't know any specifics at all |
---|
455 | _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(0), |
---|
456 | (_Unwind_Ptr)unwind_exception ); |
---|
457 | _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(1), 0 ); |
---|
458 | |
---|
459 | // I assume this sets the instruction pointer to the adress of the landing pad |
---|
460 | // It doesn't actually set it, it only state the value that needs to be set once we |
---|
461 | // return _URC_INSTALL_CONTEXT |
---|
462 | _Unwind_SetIP( unwind_context, ((lsd_info.LPStart) + (callsite_landing_pad)) ); |
---|
463 | |
---|
464 | __cfadbg_print_safe(exception, " action\n"); |
---|
465 | |
---|
466 | // Return have some action to run |
---|
467 | return _URC_INSTALL_CONTEXT; |
---|
468 | } |
---|
469 | } |
---|
470 | // No handling found |
---|
471 | __cfadbg_print_safe(exception, " table end reached"); |
---|
472 | |
---|
473 | UNWIND: |
---|
474 | __cfadbg_print_safe(exception, " unwind\n"); |
---|
475 | |
---|
476 | // Keep unwinding the stack |
---|
477 | return _URC_CONTINUE_UNWIND; |
---|
478 | } |
---|
479 | |
---|
480 | #pragma GCC push_options |
---|
481 | #pragma GCC optimize(0) |
---|
482 | |
---|
483 | // Try statements are hoisted out see comments for details. While this could probably be unique |
---|
484 | // and simply linked from libcfa but there is one problem left, see the exception table for details |
---|
485 | __attribute__((noinline)) |
---|
486 | void __cfaehm_try_terminate(void (*try_block)(), |
---|
487 | void (*catch_block)(int index, exception_t * except), |
---|
488 | __attribute__((unused)) int (*match_block)(exception_t * except)) { |
---|
489 | //! volatile int xy = 0; |
---|
490 | //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy); |
---|
491 | |
---|
492 | // Setup the personality routine and exception table. |
---|
493 | // Unforturnately these clobber gcc cancellation support which means we can't get access to |
---|
494 | // the attribute cleanup tables at the same time. We would have to inspect the assembly to |
---|
495 | // create a new set ourselves. |
---|
496 | #ifdef __PIC__ |
---|
497 | asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0"); |
---|
498 | asm volatile (".cfi_lsda 0x1b, .LLSDACFA2"); |
---|
499 | #else |
---|
500 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0"); |
---|
501 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2"); |
---|
502 | #endif |
---|
503 | |
---|
504 | // Label which defines the start of the area for which the handler is setup. |
---|
505 | asm volatile (".TRYSTART:"); |
---|
506 | |
---|
507 | // The actual statements of the try blocks |
---|
508 | try_block(); |
---|
509 | |
---|
510 | // asm statement to prevent deadcode removal |
---|
511 | asm volatile goto ("" : : : : CATCH ); |
---|
512 | |
---|
513 | // Normal return for when there is no throw. |
---|
514 | return; |
---|
515 | |
---|
516 | // Exceptionnal path |
---|
517 | CATCH : __attribute__(( unused )); |
---|
518 | // Label which defines the end of the area for which the handler is setup. |
---|
519 | asm volatile (".TRYEND:"); |
---|
520 | // Label which defines the start of the exception landing pad. Basically what is called when |
---|
521 | // the exception is caught. Note, if multiple handlers are given, the multiplexing should be |
---|
522 | // done by the generated code, not the exception runtime. |
---|
523 | asm volatile (".CATCH:"); |
---|
524 | |
---|
525 | // Exception handler |
---|
526 | // Note: Saving the exception context on the stack breaks termination exceptions. |
---|
527 | catch_block( EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index, |
---|
528 | this_exception_context()->current_exception ); |
---|
529 | } |
---|
530 | |
---|
531 | // Exception table data we need to generate. While this is almost generic, the custom data refers |
---|
532 | // to {*}try_terminate, which is no way generic. Some more works need to be done if we want to |
---|
533 | // have a single call to the try routine. |
---|
534 | |
---|
535 | #ifdef __PIC__ |
---|
536 | asm ( |
---|
537 | // HEADER |
---|
538 | ".LFECFA1:\n" |
---|
539 | " .globl __gcfa_personality_v0\n" |
---|
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 | // No clue what this does specifically |
---|
570 | " .section .data.rel.local.CFA.ref.__gcfa_personality_v0,\"awG\",@progbits,CFA.ref.__gcfa_personality_v0,comdat\n" |
---|
571 | " .align 8\n" |
---|
572 | " .type CFA.ref.__gcfa_personality_v0, @object\n" |
---|
573 | " .size CFA.ref.__gcfa_personality_v0, 8\n" |
---|
574 | "CFA.ref.__gcfa_personality_v0:\n" |
---|
575 | #if defined( __x86_64 ) |
---|
576 | " .quad __gcfa_personality_v0\n" |
---|
577 | #else // then __i386 |
---|
578 | " .long __gcfa_personality_v0\n" |
---|
579 | #endif |
---|
580 | ); |
---|
581 | #else // __PIC__ |
---|
582 | asm ( |
---|
583 | // HEADER |
---|
584 | ".LFECFA1:\n" |
---|
585 | " .globl __gcfa_personality_v0\n" |
---|
586 | " .section .gcc_except_table,\"a\",@progbits\n" |
---|
587 | // TABLE HEADER (important field is the BODY length at the end) |
---|
588 | ".LLSDACFA2:\n" |
---|
589 | " .byte 0xff\n" |
---|
590 | " .byte 0xff\n" |
---|
591 | " .byte 0x1\n" |
---|
592 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" |
---|
593 | // BODY (language specific data) |
---|
594 | ".LLSDACSBCFA2:\n" |
---|
595 | // Handled area start (relative to start of function) |
---|
596 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
597 | // Handled area length |
---|
598 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
599 | // Handler landing pad address (relative to start of function) |
---|
600 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
601 | // Action code, gcc seems to always use 0. |
---|
602 | " .uleb128 1\n" |
---|
603 | // TABLE FOOTER |
---|
604 | ".LLSDACSECFA2:\n" |
---|
605 | " .text\n" |
---|
606 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
607 | " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n" |
---|
608 | " .section .note.GNU-stack,\"x\",@progbits\n" |
---|
609 | ); |
---|
610 | #endif // __PIC__ |
---|
611 | |
---|
612 | #pragma GCC pop_options |
---|
613 | |
---|
614 | #elif defined( __ARM_ARCH ) |
---|
615 | _Unwind_Reason_Code __gcfa_personality_v0( |
---|
616 | int version, |
---|
617 | _Unwind_Action actions, |
---|
618 | unsigned long long exception_class, |
---|
619 | struct _Unwind_Exception * unwind_exception, |
---|
620 | struct _Unwind_Context * unwind_context) { |
---|
621 | return _URC_CONTINUE_UNWIND; |
---|
622 | } |
---|
623 | |
---|
624 | __attribute__((noinline)) |
---|
625 | void __cfaehm_try_terminate(void (*try_block)(), |
---|
626 | void (*catch_block)(int index, exception_t * except), |
---|
627 | __attribute__((unused)) int (*match_block)(exception_t * except)) { |
---|
628 | } |
---|
629 | #else |
---|
630 | #error unsupported hardware architecture |
---|
631 | #endif // __x86_64 || __i386 |
---|