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