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