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