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