[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 |
---|
[ecfd758] | 12 | // Last Modified On : Wed Feb 24 13:40:00 2021 |
---|
| 13 | // Update Count : 36 |
---|
[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" |
---|
[ecfd758] | 28 | #include "virtual.h" |
---|
[fa4805f] | 29 | |
---|
[915aa11] | 30 | #if defined( __ARM_ARCH ) |
---|
| 31 | #warning FIX ME: temporary hack to keep ARM build working |
---|
[b947fb2] | 32 | #ifndef _URC_FATAL_PHASE1_ERROR |
---|
[73530d9] | 33 | #define _URC_FATAL_PHASE1_ERROR 3 |
---|
[b947fb2] | 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 |
---|
[915aa11] | 38 | #endif // __ARM_ARCH |
---|
[b947fb2] | 39 | |
---|
[fa4805f] | 40 | #include "lsda.h" |
---|
| 41 | |
---|
[73530d9] | 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; |
---|
[cbce272] | 48 | |
---|
[ecfd758] | 49 | // Base Exception type id: |
---|
| 50 | struct __cfa__parent_vtable __cfatid_exception_t = { |
---|
| 51 | NULL, |
---|
[cbce272] | 52 | }; |
---|
| 53 | |
---|
| 54 | |
---|
[86d5ba7c] | 55 | // Get the current exception context. |
---|
| 56 | // There can be a single global until multithreading occurs, then each stack |
---|
[5715d43] | 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}; |
---|
[86d5ba7c] | 60 | return &shared_stack; |
---|
| 61 | } |
---|
[fa4805f] | 62 | |
---|
| 63 | |
---|
| 64 | // RESUMPTION ================================================================ |
---|
| 65 | |
---|
[f1b6671] | 66 | static void reset_top_resume(struct __cfaehm_try_resume_node ** store) { |
---|
| 67 | this_exception_context()->top_resume = *store; |
---|
| 68 | } |
---|
| 69 | |
---|
[046a890] | 70 | void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) { |
---|
[2a3b019] | 71 | struct exception_context_t * context = this_exception_context(); |
---|
[fa4805f] | 72 | |
---|
[851fd92] | 73 | __cfadbg_print_safe(exception, "Throwing resumption exception\n"); |
---|
[fa4805f] | 74 | |
---|
[381132b] | 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 | } |
---|
[fa4805f] | 85 | } |
---|
[381132b] | 86 | } // End the search and return to the top of the stack. |
---|
[fa4805f] | 87 | |
---|
[046a890] | 88 | // No handler found, fall back to the default operation. |
---|
[851fd92] | 89 | __cfadbg_print_safe(exception, "Unhandled exception\n"); |
---|
[046a890] | 90 | defaultHandler(except); |
---|
[fa4805f] | 91 | } |
---|
| 92 | |
---|
[eb46fdf] | 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. |
---|
[b947fb2] | 96 | |
---|
[3090127] | 97 | void __cfaehm_try_resume_setup(struct __cfaehm_try_resume_node * node, |
---|
[0304215a] | 98 | _Bool (*handler)(exception_t * except)) { |
---|
[2a3b019] | 99 | struct exception_context_t * context = this_exception_context(); |
---|
| 100 | node->next = context->top_resume; |
---|
[307a732] | 101 | node->handler = handler; |
---|
[2a3b019] | 102 | context->top_resume = node; |
---|
[fa4805f] | 103 | } |
---|
| 104 | |
---|
[3090127] | 105 | void __cfaehm_try_resume_cleanup(struct __cfaehm_try_resume_node * node) { |
---|
[2a3b019] | 106 | struct exception_context_t * context = this_exception_context(); |
---|
| 107 | context->top_resume = node->next; |
---|
[fa4805f] | 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
[2922871] | 111 | // MEMORY MANAGEMENT ========================================================= |
---|
[ff7ff14a] | 112 | |
---|
[980fb4e] | 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 | |
---|
[73530d9] | 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 | |
---|
[86d5ba7c] | 132 | // Creates a copy of the indicated exception and sets current_exception to it. |
---|
[3090127] | 133 | static void __cfaehm_allocate_exception( exception_t * except ) { |
---|
[86d5ba7c] | 134 | struct exception_context_t * context = this_exception_context(); |
---|
| 135 | |
---|
[ff7ff14a] | 136 | // Allocate memory for the exception. |
---|
[3090127] | 137 | struct __cfaehm_node * store = malloc( |
---|
| 138 | sizeof( struct __cfaehm_node ) + except->virtual_table->size ); |
---|
[ff7ff14a] | 139 | |
---|
| 140 | if ( ! store ) { |
---|
| 141 | // Failure: cannot allocate exception. Terminate thread. |
---|
| 142 | abort(); // <- Although I think it might be the process. |
---|
[86d5ba7c] | 143 | } |
---|
| 144 | |
---|
[980fb4e] | 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 ); |
---|
[73530d9] | 151 | |
---|
[980fb4e] | 152 | // Add the node to the list: |
---|
| 153 | store->next = NULL_MAP(EXCEPT_TO_NODE, context->current_exception); |
---|
| 154 | context->current_exception = except_store; |
---|
[86d5ba7c] | 155 | } |
---|
| 156 | |
---|
| 157 | // Delete the provided exception, unsetting current_exception if relivant. |
---|
[3090127] | 158 | static void __cfaehm_delete_exception( exception_t * except ) { |
---|
[86d5ba7c] | 159 | struct exception_context_t * context = this_exception_context(); |
---|
| 160 | |
---|
[851fd92] | 161 | __cfadbg_print_safe(exception, "Deleting Exception\n"); |
---|
[ff7ff14a] | 162 | |
---|
| 163 | // Remove the exception from the list. |
---|
[3090127] | 164 | struct __cfaehm_node * to_free = EXCEPT_TO_NODE(except); |
---|
| 165 | struct __cfaehm_node * node; |
---|
[86d5ba7c] | 166 | |
---|
| 167 | if ( context->current_exception == except ) { |
---|
[ff7ff14a] | 168 | node = to_free->next; |
---|
[980fb4e] | 169 | context->current_exception = NULL_MAP(NODE_TO_EXCEPT, node); |
---|
[86d5ba7c] | 170 | } else { |
---|
[ff7ff14a] | 171 | node = EXCEPT_TO_NODE(context->current_exception); |
---|
| 172 | // It may always be in the first or second position. |
---|
[9cb89b87] | 173 | while ( to_free != node->next ) { |
---|
[ff7ff14a] | 174 | node = node->next; |
---|
| 175 | } |
---|
| 176 | node->next = to_free->next; |
---|
[86d5ba7c] | 177 | } |
---|
[ff7ff14a] | 178 | |
---|
| 179 | // Free the old exception node. |
---|
[cbce272] | 180 | except->virtual_table->free( except ); |
---|
[ff7ff14a] | 181 | free( to_free ); |
---|
[86d5ba7c] | 182 | } |
---|
| 183 | |
---|
[2922871] | 184 | // CANCELLATION ============================================================== |
---|
[fa4805f] | 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, |
---|
[3090127] | 191 | _Unwind_Exception_Class exception_class, |
---|
[fa4805f] | 192 | struct _Unwind_Exception * unwind_exception, |
---|
[2a3b019] | 193 | struct _Unwind_Context * unwind_context, |
---|
| 194 | void * stop_param) { |
---|
[a201f7b] | 195 | // Verify actions follow the rules we expect. |
---|
[d119d613] | 196 | verify(actions & _UA_CLEANUP_PHASE); |
---|
| 197 | verify(actions & _UA_FORCE_UNWIND); |
---|
| 198 | verify(!(actions & _UA_SEARCH_PHASE)); |
---|
| 199 | verify(!(actions & _UA_HANDLER_FRAME)); |
---|
[fa4805f] | 200 | |
---|
[a201f7b] | 201 | if ( actions & _UA_END_OF_STACK ) { |
---|
[d119d613] | 202 | abort(); |
---|
[a201f7b] | 203 | } else { |
---|
| 204 | return _URC_NO_REASON; |
---|
| 205 | } |
---|
[fa4805f] | 206 | } |
---|
| 207 | |
---|
[d119d613] | 208 | __attribute__((weak)) _Unwind_Reason_Code |
---|
| 209 | __cfaehm_cancellation_unwind( struct _Unwind_Exception * exception ) { |
---|
| 210 | return _Unwind_ForcedUnwind( exception, _Stop_Fn, (void*)0x22 ); |
---|
| 211 | } |
---|
[980fb4e] | 212 | |
---|
[2922871] | 213 | // Cancel the current stack, prefroming approprate clean-up and messaging. |
---|
[979df46] | 214 | void __cfaehm_cancel_stack( exception_t * exception ) { |
---|
[d119d613] | 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 | |
---|
[2922871] | 229 | _Unwind_Reason_Code ret; |
---|
[d119d613] | 230 | ret = __cfaehm_cancellation_unwind( &node->unwind_exception ); |
---|
[2922871] | 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 | |
---|
[8ad5752] | 243 | static void __cfaehm_cleanup_default( exception_t ** except ) { |
---|
| 244 | __cfaehm_delete_exception( *except ); |
---|
| 245 | *except = NULL; |
---|
| 246 | } |
---|
| 247 | |
---|
[86d5ba7c] | 248 | // The exception that is being thrown must already be stored. |
---|
[046a890] | 249 | static void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) { |
---|
[8ad5752] | 250 | struct exception_context_t * context = this_exception_context(); |
---|
| 251 | if ( NULL == context->current_exception ) { |
---|
[86d5ba7c] | 252 | printf("UNWIND ERROR missing exception in begin unwind\n"); |
---|
| 253 | abort(); |
---|
| 254 | } |
---|
[980fb4e] | 255 | struct _Unwind_Exception * storage = |
---|
| 256 | &EXCEPT_TO_NODE(context->current_exception)->unwind_exception; |
---|
[fa4805f] | 257 | |
---|
| 258 | // Call stdlibc to raise the exception |
---|
[8ad5752] | 259 | __cfadbg_print_safe(exception, "Begin unwinding (storage &p, context %p)\n", storage, context); |
---|
| 260 | _Unwind_Reason_Code ret = _Unwind_RaiseException( storage ); |
---|
[fa4805f] | 261 | |
---|
[eb46fdf] | 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. |
---|
[fa4805f] | 268 | |
---|
[8ad5752] | 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(); |
---|
[fa4805f] | 273 | } |
---|
| 274 | |
---|
[8ad5752] | 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 ); |
---|
[fa4805f] | 281 | } |
---|
| 282 | |
---|
[046a890] | 283 | void __cfaehm_throw_terminate( exception_t * val, void (*defaultHandler)(exception_t *) ) { |
---|
[851fd92] | 284 | __cfadbg_print_safe(exception, "Throwing termination exception\n"); |
---|
[86d5ba7c] | 285 | |
---|
[3090127] | 286 | __cfaehm_allocate_exception( val ); |
---|
[046a890] | 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(); |
---|
[86d5ba7c] | 294 | } |
---|
| 295 | |
---|
[3090127] | 296 | void __cfaehm_rethrow_terminate(void) { |
---|
[851fd92] | 297 | __cfadbg_print_safe(exception, "Rethrowing termination exception\n"); |
---|
[fa4805f] | 298 | |
---|
[046a890] | 299 | __cfaehm_begin_unwind( __cfaehm_rethrow_adapter ); |
---|
| 300 | abort(); |
---|
[fa4805f] | 301 | } |
---|
[ba70330] | 302 | |
---|
| 303 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
[eb46fdf] | 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. |
---|
[3090127] | 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) |
---|
[fa4805f] | 313 | { |
---|
| 314 | |
---|
[851fd92] | 315 | //__cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(context)); |
---|
| 316 | __cfadbg_print_safe(exception, "Personality function (%d, %x, %llu, %p, %p):", |
---|
[3090127] | 317 | version, actions, exception_class, unwind_exception, unwind_context); |
---|
[fa4805f] | 318 | |
---|
[a201f7b] | 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... |
---|
[fa4805f] | 323 | if (actions & _UA_SEARCH_PHASE) { |
---|
[a201f7b] | 324 | verify(actions == _UA_SEARCH_PHASE); |
---|
[851fd92] | 325 | __cfadbg_print_safe(exception, " lookup phase"); |
---|
[a201f7b] | 326 | // ... we are in clean-up phase. |
---|
| 327 | } else { |
---|
| 328 | verify(actions & _UA_CLEANUP_PHASE); |
---|
[851fd92] | 329 | __cfadbg_print_safe(exception, " cleanup phase"); |
---|
[a201f7b] | 330 | // We shouldn't be the handler frame during forced unwind. |
---|
| 331 | if (actions & _UA_HANDLER_FRAME) { |
---|
| 332 | verify(!(actions & _UA_FORCE_UNWIND)); |
---|
[851fd92] | 333 | __cfadbg_print_safe(exception, " (handler frame)"); |
---|
[a201f7b] | 334 | } else if (actions & _UA_FORCE_UNWIND) { |
---|
[851fd92] | 335 | __cfadbg_print_safe(exception, " (force unwind)"); |
---|
[a201f7b] | 336 | } |
---|
[fa4805f] | 337 | } |
---|
| 338 | |
---|
| 339 | // Get a pointer to the language specific data from which we will read what we need |
---|
[9cb89b87] | 340 | const unsigned char * lsd = _Unwind_GetLanguageSpecificData( unwind_context ); |
---|
[fa4805f] | 341 | |
---|
[9cb89b87] | 342 | if ( !lsd ) { //Nothing to do, keep unwinding |
---|
[fa4805f] | 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; |
---|
[2a3b019] | 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(); |
---|
[fa4805f] | 353 | |
---|
| 354 | // Linearly search the table for stuff to do |
---|
[9cb89b87] | 355 | while ( cur_ptr < lsd_info.action_table ) { |
---|
[fa4805f] | 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 |
---|
[eb46fdf] | 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); |
---|
[fa4805f] | 366 | |
---|
| 367 | // Have we reach the correct frame info yet? |
---|
[9cb89b87] | 368 | if ( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) { |
---|
[e9145a3] | 369 | #ifdef __CFA_DEBUG_PRINT__ |
---|
[fa4805f] | 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; |
---|
[851fd92] | 376 | __cfadbg_print_safe(exception, "\nfound %p - %p (%p, %p, %p), looking for %p\n", |
---|
[eb46fdf] | 377 | bp, ep, ls, cs, cl, ip); |
---|
[e9145a3] | 378 | #endif // __CFA_DEBUG_PRINT__ |
---|
[fa4805f] | 379 | continue; |
---|
| 380 | } |
---|
| 381 | |
---|
[eb46fdf] | 382 | // Have we gone too far? |
---|
[9cb89b87] | 383 | if ( lsd_info.Start + callsite_start > instruction_ptr ) { |
---|
[fa4805f] | 384 | printf(" gone too far"); |
---|
| 385 | break; |
---|
| 386 | } |
---|
| 387 | |
---|
[9cb89b87] | 388 | // Check for what we must do: |
---|
[2a3b019] | 389 | if ( 0 == callsite_landing_pad ) { |
---|
| 390 | // Nothing to do, move along |
---|
[851fd92] | 391 | __cfadbg_print_safe(exception, " no landing pad"); |
---|
[9cb89b87] | 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 | |
---|
[67ca73e] | 415 | _Unwind_Word match_pos = |
---|
[9cb89b87] | 416 | # if defined( __x86_64 ) |
---|
[67ca73e] | 417 | _Unwind_GetCFA(unwind_context) + 8; |
---|
[9cb89b87] | 418 | # elif defined( __i386 ) |
---|
[67ca73e] | 419 | _Unwind_GetCFA(unwind_context) + 24; |
---|
| 420 | # elif defined( __ARM_ARCH ) |
---|
| 421 | # warning FIX ME: check if anything needed for ARM |
---|
| 422 | 42; |
---|
[9cb89b87] | 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; |
---|
[980fb4e] | 429 | UNWIND_TO_NODE(unwind_exception)->handler_index = index; |
---|
[9cb89b87] | 430 | |
---|
| 431 | // Based on the return value, check if we matched the exception |
---|
| 432 | if (ret == _URC_HANDLER_FOUND) { |
---|
[851fd92] | 433 | __cfadbg_print_safe(exception, " handler found\n"); |
---|
[9cb89b87] | 434 | } else { |
---|
[980fb4e] | 435 | // TODO: Continue the search if there is more in the table. |
---|
[851fd92] | 436 | __cfadbg_print_safe(exception, " no handler\n"); |
---|
[fa4805f] | 437 | } |
---|
[9cb89b87] | 438 | return ret; |
---|
[fa4805f] | 439 | } |
---|
| 440 | |
---|
[9cb89b87] | 441 | // This is only a cleanup handler, ignore it |
---|
[851fd92] | 442 | __cfadbg_print_safe(exception, " no action"); |
---|
[a201f7b] | 443 | } else { |
---|
[9cb89b87] | 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 | } |
---|
[fa4805f] | 452 | |
---|
[9cb89b87] | 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 ); |
---|
[fa4805f] | 458 | |
---|
[9cb89b87] | 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)) ); |
---|
[fa4805f] | 463 | |
---|
[851fd92] | 464 | __cfadbg_print_safe(exception, " action\n"); |
---|
[fa4805f] | 465 | |
---|
[9cb89b87] | 466 | // Return have some action to run |
---|
| 467 | return _URC_INSTALL_CONTEXT; |
---|
[fa4805f] | 468 | } |
---|
| 469 | } |
---|
| 470 | // No handling found |
---|
[851fd92] | 471 | __cfadbg_print_safe(exception, " table end reached"); |
---|
[fa4805f] | 472 | |
---|
| 473 | UNWIND: |
---|
[851fd92] | 474 | __cfadbg_print_safe(exception, " unwind\n"); |
---|
[fa4805f] | 475 | |
---|
| 476 | // Keep unwinding the stack |
---|
| 477 | return _URC_CONTINUE_UNWIND; |
---|
| 478 | } |
---|
| 479 | |
---|
[0f6ac828] | 480 | #pragma GCC push_options |
---|
[f017af6] | 481 | #pragma GCC optimize(0) |
---|
[0f6ac828] | 482 | |
---|
[eb46fdf] | 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 |
---|
[fa4805f] | 485 | __attribute__((noinline)) |
---|
[3090127] | 486 | void __cfaehm_try_terminate(void (*try_block)(), |
---|
[0304215a] | 487 | void (*catch_block)(int index, exception_t * except), |
---|
| 488 | __attribute__((unused)) int (*match_block)(exception_t * except)) { |
---|
[fa4805f] | 489 | //! volatile int xy = 0; |
---|
| 490 | //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy); |
---|
| 491 | |
---|
[eb46fdf] | 492 | // Setup the personality routine and exception table. |
---|
[9cb89b87] | 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. |
---|
[3b9c674] | 496 | #ifdef __PIC__ |
---|
| 497 | asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0"); |
---|
| 498 | asm volatile (".cfi_lsda 0x1b, .LLSDACFA2"); |
---|
| 499 | #else |
---|
[fa4805f] | 500 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0"); |
---|
| 501 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2"); |
---|
[3b9c674] | 502 | #endif |
---|
[fa4805f] | 503 | |
---|
[b947fb2] | 504 | // Label which defines the start of the area for which the handler is setup. |
---|
[fa4805f] | 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 | |
---|
[eb46fdf] | 513 | // Normal return for when there is no throw. |
---|
[fa4805f] | 514 | return; |
---|
| 515 | |
---|
| 516 | // Exceptionnal path |
---|
| 517 | CATCH : __attribute__(( unused )); |
---|
[b947fb2] | 518 | // Label which defines the end of the area for which the handler is setup. |
---|
[fa4805f] | 519 | asm volatile (".TRYEND:"); |
---|
[9cb89b87] | 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. |
---|
[fa4805f] | 523 | asm volatile (".CATCH:"); |
---|
| 524 | |
---|
| 525 | // Exception handler |
---|
[2a3b019] | 526 | // Note: Saving the exception context on the stack breaks termination exceptions. |
---|
[980fb4e] | 527 | catch_block( EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index, |
---|
[2a3b019] | 528 | this_exception_context()->current_exception ); |
---|
[fa4805f] | 529 | } |
---|
| 530 | |
---|
[eb46fdf] | 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. |
---|
[b947fb2] | 534 | |
---|
[3b9c674] | 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" |
---|
[3090127] | 552 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
[3b9c674] | 553 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
[3090127] | 554 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
[3b9c674] | 555 | " .uleb128 1\n" |
---|
| 556 | ".LLSDACSECFA2:\n" |
---|
| 557 | // TABLE FOOTER |
---|
| 558 | " .text\n" |
---|
[3090127] | 559 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
[3b9c674] | 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 |
---|
[e045590] | 578 | " .long __gcfa_personality_v0\n" |
---|
[3b9c674] | 579 | #endif |
---|
| 580 | ); |
---|
| 581 | #else // __PIC__ |
---|
[fa4805f] | 582 | asm ( |
---|
[eb46fdf] | 583 | // HEADER |
---|
[fa4805f] | 584 | ".LFECFA1:\n" |
---|
| 585 | " .globl __gcfa_personality_v0\n" |
---|
| 586 | " .section .gcc_except_table,\"a\",@progbits\n" |
---|
[eb46fdf] | 587 | // TABLE HEADER (important field is the BODY length at the end) |
---|
| 588 | ".LLSDACFA2:\n" |
---|
[fa4805f] | 589 | " .byte 0xff\n" |
---|
| 590 | " .byte 0xff\n" |
---|
| 591 | " .byte 0x1\n" |
---|
[eb46fdf] | 592 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" |
---|
| 593 | // BODY (language specific data) |
---|
| 594 | ".LLSDACSBCFA2:\n" |
---|
| 595 | // Handled area start (relative to start of function) |
---|
[3090127] | 596 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
[eb46fdf] | 597 | // Handled area length |
---|
| 598 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
| 599 | // Handler landing pad address (relative to start of function) |
---|
[3090127] | 600 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
[eb46fdf] | 601 | // Action code, gcc seems to always use 0. |
---|
| 602 | " .uleb128 1\n" |
---|
| 603 | // TABLE FOOTER |
---|
| 604 | ".LLSDACSECFA2:\n" |
---|
| 605 | " .text\n" |
---|
[3090127] | 606 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
[fa4805f] | 607 | " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n" |
---|
[3b9c674] | 608 | " .section .note.GNU-stack,\"x\",@progbits\n" |
---|
[fa4805f] | 609 | ); |
---|
[3b9c674] | 610 | #endif // __PIC__ |
---|
| 611 | |
---|
| 612 | #pragma GCC pop_options |
---|
[915aa11] | 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 |
---|
[ba70330] | 631 | #endif // __x86_64 || __i386 |
---|