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