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