[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 |
---|
[33e9b87] | 11 | // Last Modified By : Peter A. Buhr |
---|
[13600e8] | 12 | // Last Modified On : Thu Aug 10 16:45:22 2023 |
---|
| 13 | // Update Count : 69 |
---|
[fa4805f] | 14 | // |
---|
| 15 | |
---|
[9640813] | 16 | // Normally we would get this from the CFA prelude. |
---|
[cbce272] | 17 | #include <stddef.h> // for size_t |
---|
| 18 | |
---|
[c960331] | 19 | #include <unwind.h> // for struct _Unwind_Exception {...}; |
---|
| 20 | |
---|
[fa4805f] | 21 | #include "exception.h" |
---|
| 22 | |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include <stdio.h> |
---|
[73abe95] | 25 | #include <bits/debug.hfa> |
---|
[80ec409] | 26 | #include "concurrency/invoke.h" |
---|
[a201f7b] | 27 | #include "stdhdr/assert.h" |
---|
[ecfd758] | 28 | #include "virtual.h" |
---|
[8108ba8] | 29 | |
---|
[13600e8] | 30 | extern void __cabi_abort( const char fmt[], ... ); |
---|
| 31 | |
---|
[8108ba8] | 32 | #pragma GCC visibility push(default) |
---|
| 33 | |
---|
[fa4805f] | 34 | #include "lsda.h" |
---|
| 35 | |
---|
[73530d9] | 36 | /* The exception class for our exceptions. Because of the vendor component |
---|
| 37 | * its value would not be standard. |
---|
| 38 | * Vendor: UWPL |
---|
| 39 | * Language: CFA\0 |
---|
| 40 | */ |
---|
| 41 | const _Unwind_Exception_Class __cfaehm_exception_class = 0x4c50575500414643; |
---|
[cbce272] | 42 | |
---|
[ecfd758] | 43 | // Base Exception type id: |
---|
[8f910430] | 44 | struct __cfavir_type_info __cfatid_exception_t = { |
---|
[ecfd758] | 45 | NULL, |
---|
[cbce272] | 46 | }; |
---|
| 47 | |
---|
| 48 | |
---|
[86d5ba7c] | 49 | // Get the current exception context. |
---|
| 50 | // There can be a single global until multithreading occurs, then each stack |
---|
[5715d43] | 51 | // needs its own. We get this from libcfathreads (no weak attribute). |
---|
| 52 | __attribute__((weak)) struct exception_context_t * this_exception_context() { |
---|
| 53 | static struct exception_context_t shared_stack = {NULL, NULL}; |
---|
[86d5ba7c] | 54 | return &shared_stack; |
---|
| 55 | } |
---|
[fa4805f] | 56 | |
---|
| 57 | |
---|
| 58 | // RESUMPTION ================================================================ |
---|
| 59 | |
---|
[f1b6671] | 60 | static void reset_top_resume(struct __cfaehm_try_resume_node ** store) { |
---|
| 61 | this_exception_context()->top_resume = *store; |
---|
| 62 | } |
---|
| 63 | |
---|
[046a890] | 64 | void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) { |
---|
[2a3b019] | 65 | struct exception_context_t * context = this_exception_context(); |
---|
[fa4805f] | 66 | |
---|
[851fd92] | 67 | __cfadbg_print_safe(exception, "Throwing resumption exception\n"); |
---|
[fa4805f] | 68 | |
---|
[381132b] | 69 | { |
---|
| 70 | __attribute__((cleanup(reset_top_resume))) |
---|
| 71 | struct __cfaehm_try_resume_node * original_head = context->top_resume; |
---|
| 72 | struct __cfaehm_try_resume_node * current = context->top_resume; |
---|
| 73 | |
---|
| 74 | for ( ; current ; current = current->next) { |
---|
| 75 | context->top_resume = current->next; |
---|
| 76 | if (current->handler(except)) { |
---|
| 77 | return; |
---|
| 78 | } |
---|
[fa4805f] | 79 | } |
---|
[381132b] | 80 | } // End the search and return to the top of the stack. |
---|
[fa4805f] | 81 | |
---|
[046a890] | 82 | // No handler found, fall back to the default operation. |
---|
[851fd92] | 83 | __cfadbg_print_safe(exception, "Unhandled exception\n"); |
---|
[046a890] | 84 | defaultHandler(except); |
---|
[fa4805f] | 85 | } |
---|
| 86 | |
---|
[eb46fdf] | 87 | // Do we control where exceptions get thrown even with concurency? |
---|
| 88 | // If not these are not quite thread safe, the cleanup hook has to |
---|
| 89 | // be added after the node is built but before it is made the top node. |
---|
[b947fb2] | 90 | |
---|
[3090127] | 91 | void __cfaehm_try_resume_setup(struct __cfaehm_try_resume_node * node, |
---|
[0304215a] | 92 | _Bool (*handler)(exception_t * except)) { |
---|
[2a3b019] | 93 | struct exception_context_t * context = this_exception_context(); |
---|
| 94 | node->next = context->top_resume; |
---|
[307a732] | 95 | node->handler = handler; |
---|
[2a3b019] | 96 | context->top_resume = node; |
---|
[fa4805f] | 97 | } |
---|
| 98 | |
---|
[3090127] | 99 | void __cfaehm_try_resume_cleanup(struct __cfaehm_try_resume_node * node) { |
---|
[2a3b019] | 100 | struct exception_context_t * context = this_exception_context(); |
---|
| 101 | context->top_resume = node->next; |
---|
[fa4805f] | 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
[2922871] | 105 | // MEMORY MANAGEMENT ========================================================= |
---|
[ff7ff14a] | 106 | |
---|
[980fb4e] | 107 | #define NODE_TO_EXCEPT(node) ((exception_t *)(1 + (node))) |
---|
| 108 | #define EXCEPT_TO_NODE(except) ((struct __cfaehm_node *)(except) - 1) |
---|
| 109 | #define UNWIND_TO_NODE(unwind) ((struct __cfaehm_node *)(unwind)) |
---|
| 110 | #define NULL_MAP(map, ptr) ((ptr) ? (map(ptr)) : NULL) |
---|
| 111 | |
---|
[73530d9] | 112 | // How to clean up an exception in various situations. |
---|
| 113 | static void __cfaehm_exception_cleanup( |
---|
| 114 | _Unwind_Reason_Code reason, |
---|
| 115 | struct _Unwind_Exception * exception) { |
---|
| 116 | switch (reason) { |
---|
| 117 | case _URC_FOREIGN_EXCEPTION_CAUGHT: |
---|
| 118 | // This one we could clean-up to allow cross-language exceptions. |
---|
| 119 | case _URC_FATAL_PHASE1_ERROR: |
---|
| 120 | case _URC_FATAL_PHASE2_ERROR: |
---|
| 121 | default: |
---|
| 122 | abort(); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
[86d5ba7c] | 126 | // Creates a copy of the indicated exception and sets current_exception to it. |
---|
[57fd66d] | 127 | void __cfaehm_allocate_exception( exception_t * except ) { |
---|
[86d5ba7c] | 128 | struct exception_context_t * context = this_exception_context(); |
---|
| 129 | |
---|
[ff7ff14a] | 130 | // Allocate memory for the exception. |
---|
[3090127] | 131 | struct __cfaehm_node * store = malloc( |
---|
| 132 | sizeof( struct __cfaehm_node ) + except->virtual_table->size ); |
---|
[ff7ff14a] | 133 | |
---|
| 134 | if ( ! store ) { |
---|
| 135 | // Failure: cannot allocate exception. Terminate thread. |
---|
| 136 | abort(); // <- Although I think it might be the process. |
---|
[86d5ba7c] | 137 | } |
---|
| 138 | |
---|
[980fb4e] | 139 | // Initialize the node: |
---|
| 140 | exception_t * except_store = NODE_TO_EXCEPT(store); |
---|
| 141 | store->unwind_exception.exception_class = __cfaehm_exception_class; |
---|
| 142 | store->unwind_exception.exception_cleanup = __cfaehm_exception_cleanup; |
---|
| 143 | store->handler_index = 0; |
---|
| 144 | except->virtual_table->copy( except_store, except ); |
---|
[73530d9] | 145 | |
---|
[980fb4e] | 146 | // Add the node to the list: |
---|
| 147 | store->next = NULL_MAP(EXCEPT_TO_NODE, context->current_exception); |
---|
| 148 | context->current_exception = except_store; |
---|
[86d5ba7c] | 149 | } |
---|
| 150 | |
---|
| 151 | // Delete the provided exception, unsetting current_exception if relivant. |
---|
[3090127] | 152 | static void __cfaehm_delete_exception( exception_t * except ) { |
---|
[86d5ba7c] | 153 | struct exception_context_t * context = this_exception_context(); |
---|
| 154 | |
---|
[851fd92] | 155 | __cfadbg_print_safe(exception, "Deleting Exception\n"); |
---|
[ff7ff14a] | 156 | |
---|
| 157 | // Remove the exception from the list. |
---|
[3090127] | 158 | struct __cfaehm_node * to_free = EXCEPT_TO_NODE(except); |
---|
| 159 | struct __cfaehm_node * node; |
---|
[86d5ba7c] | 160 | |
---|
| 161 | if ( context->current_exception == except ) { |
---|
[ff7ff14a] | 162 | node = to_free->next; |
---|
[980fb4e] | 163 | context->current_exception = NULL_MAP(NODE_TO_EXCEPT, node); |
---|
[86d5ba7c] | 164 | } else { |
---|
[ff7ff14a] | 165 | node = EXCEPT_TO_NODE(context->current_exception); |
---|
| 166 | // It may always be in the first or second position. |
---|
[9cb89b87] | 167 | while ( to_free != node->next ) { |
---|
[ff7ff14a] | 168 | node = node->next; |
---|
| 169 | } |
---|
| 170 | node->next = to_free->next; |
---|
[86d5ba7c] | 171 | } |
---|
[ff7ff14a] | 172 | |
---|
| 173 | // Free the old exception node. |
---|
[cbce272] | 174 | except->virtual_table->free( except ); |
---|
[ff7ff14a] | 175 | free( to_free ); |
---|
[86d5ba7c] | 176 | } |
---|
| 177 | |
---|
[2922871] | 178 | // CANCELLATION ============================================================== |
---|
[fa4805f] | 179 | |
---|
| 180 | // Function needed by force unwind |
---|
| 181 | // It basically says to unwind the whole stack and then exit when we reach the end of the stack |
---|
| 182 | static _Unwind_Reason_Code _Stop_Fn( |
---|
| 183 | int version, |
---|
| 184 | _Unwind_Action actions, |
---|
[3090127] | 185 | _Unwind_Exception_Class exception_class, |
---|
[fa4805f] | 186 | struct _Unwind_Exception * unwind_exception, |
---|
[2a3b019] | 187 | struct _Unwind_Context * unwind_context, |
---|
| 188 | void * stop_param) { |
---|
[a201f7b] | 189 | // Verify actions follow the rules we expect. |
---|
[d119d613] | 190 | verify(actions & _UA_CLEANUP_PHASE); |
---|
| 191 | verify(actions & _UA_FORCE_UNWIND); |
---|
| 192 | verify(!(actions & _UA_SEARCH_PHASE)); |
---|
| 193 | verify(!(actions & _UA_HANDLER_FRAME)); |
---|
[fa4805f] | 194 | |
---|
[a201f7b] | 195 | if ( actions & _UA_END_OF_STACK ) { |
---|
[13600e8] | 196 | __cabi_abort( |
---|
| 197 | "Propagation failed to find a matching handler.\n" |
---|
| 198 | "Possible cause is a missing try block with appropriate catch clause for specified exception type.\n" |
---|
| 199 | "Last exception name or message: %s.\n", |
---|
| 200 | NODE_TO_EXCEPT( UNWIND_TO_NODE( unwind_exception ) )-> |
---|
| 201 | virtual_table->msg( NODE_TO_EXCEPT( UNWIND_TO_NODE( unwind_exception ) ) ) |
---|
| 202 | ); |
---|
[a201f7b] | 203 | } else { |
---|
| 204 | return _URC_NO_REASON; |
---|
| 205 | } |
---|
[fa4805f] | 206 | } |
---|
| 207 | |
---|
[d119d613] | 208 | __attribute__((weak)) _Unwind_Reason_Code |
---|
| 209 | __cfaehm_cancellation_unwind( struct _Unwind_Exception * exception ) { |
---|
| 210 | return _Unwind_ForcedUnwind( exception, _Stop_Fn, (void*)0x22 ); |
---|
| 211 | } |
---|
[980fb4e] | 212 | |
---|
[2922871] | 213 | // Cancel the current stack, prefroming approprate clean-up and messaging. |
---|
[979df46] | 214 | void __cfaehm_cancel_stack( exception_t * exception ) { |
---|
[d119d613] | 215 | __cfaehm_allocate_exception( exception ); |
---|
| 216 | |
---|
| 217 | struct exception_context_t * context = this_exception_context(); |
---|
| 218 | struct __cfaehm_node * node = EXCEPT_TO_NODE(context->current_exception); |
---|
| 219 | |
---|
[2922871] | 220 | _Unwind_Reason_Code ret; |
---|
[d119d613] | 221 | ret = __cfaehm_cancellation_unwind( &node->unwind_exception ); |
---|
[2922871] | 222 | printf("UNWIND ERROR %d after force unwind\n", ret); |
---|
| 223 | abort(); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | // TERMINATION =============================================================== |
---|
| 228 | |
---|
| 229 | // If this isn't a rethrow (*except==0), delete the provided exception. |
---|
| 230 | void __cfaehm_cleanup_terminate( void * except ) { |
---|
| 231 | if ( *(void**)except ) __cfaehm_delete_exception( *(exception_t **)except ); |
---|
| 232 | } |
---|
| 233 | |
---|
[8ad5752] | 234 | static void __cfaehm_cleanup_default( exception_t ** except ) { |
---|
| 235 | __cfaehm_delete_exception( *except ); |
---|
| 236 | *except = NULL; |
---|
| 237 | } |
---|
| 238 | |
---|
[86d5ba7c] | 239 | // The exception that is being thrown must already be stored. |
---|
[57fd66d] | 240 | void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) { |
---|
[8ad5752] | 241 | struct exception_context_t * context = this_exception_context(); |
---|
| 242 | if ( NULL == context->current_exception ) { |
---|
[86d5ba7c] | 243 | printf("UNWIND ERROR missing exception in begin unwind\n"); |
---|
| 244 | abort(); |
---|
| 245 | } |
---|
[980fb4e] | 246 | struct _Unwind_Exception * storage = |
---|
| 247 | &EXCEPT_TO_NODE(context->current_exception)->unwind_exception; |
---|
[fa4805f] | 248 | |
---|
| 249 | // Call stdlibc to raise the exception |
---|
[8ad5752] | 250 | __cfadbg_print_safe(exception, "Begin unwinding (storage &p, context %p)\n", storage, context); |
---|
| 251 | _Unwind_Reason_Code ret = _Unwind_RaiseException( storage ); |
---|
[fa4805f] | 252 | |
---|
[eb46fdf] | 253 | // If we reach here it means something happened. For resumption to work we need to find a way |
---|
| 254 | // to return back to here. Most of them will probably boil down to setting a global flag and |
---|
| 255 | // making the phase 1 either stop or fail. Causing an error on purpose may help avoiding |
---|
| 256 | // unnecessary work but it might have some weird side effects. If we just pretend no handler |
---|
| 257 | // was found that would work but may be expensive for no reason since we will always search |
---|
| 258 | // the whole stack. |
---|
[fa4805f] | 259 | |
---|
[ed395761] | 260 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
[8ad5752] | 261 | // We did not simply reach the end of the stack without finding a handler. This is an error. |
---|
| 262 | if ( ret != _URC_END_OF_STACK ) { |
---|
[ed395761] | 263 | #else // defined( __ARM_ARCH ) |
---|
| 264 | // The return code from _Unwind_RaiseException seems to be corrupt on ARM at end of stack. |
---|
[8108ba8] | 265 | // This workaround tries to keep default exception handling working. |
---|
[ed395761] | 266 | if ( ret == _URC_FATAL_PHASE1_ERROR || ret == _URC_FATAL_PHASE2_ERROR ) { |
---|
| 267 | #endif |
---|
[8ad5752] | 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 | } |
---|
[ba70330] | 299 | |
---|
[e8261bb] | 300 | #if defined( __x86_64 ) || defined( __i386 ) || defined( __ARM_ARCH ) |
---|
[eb46fdf] | 301 | // This is our personality routine. For every stack frame annotated with |
---|
| 302 | // ".cfi_personality 0x3,__gcfa_personality_v0" this function will be called twice when unwinding. |
---|
| 303 | // Once in the search phase and once in the cleanup phase. |
---|
[3090127] | 304 | _Unwind_Reason_Code __gcfa_personality_v0( |
---|
| 305 | int version, |
---|
| 306 | _Unwind_Action actions, |
---|
| 307 | unsigned long long exception_class, |
---|
| 308 | struct _Unwind_Exception * unwind_exception, |
---|
| 309 | struct _Unwind_Context * unwind_context) |
---|
[fa4805f] | 310 | { |
---|
[142930b] | 311 | //! __cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(unwind_context)); |
---|
[851fd92] | 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 | |
---|
[67ca73e] | 411 | _Unwind_Word match_pos = |
---|
[9cb89b87] | 412 | # if defined( __x86_64 ) |
---|
[2554f24] | 413 | _Unwind_GetCFA(unwind_context) + 8; |
---|
[c40157e] | 414 | # elif defined( __i386 ) |
---|
| 415 | _Unwind_GetCFA(unwind_context) + 24; |
---|
[67ca73e] | 416 | # elif defined( __ARM_ARCH ) |
---|
[c40157e] | 417 | _Unwind_GetCFA(unwind_context) + 40; |
---|
[9cb89b87] | 418 | # endif |
---|
| 419 | int (*matcher)(exception_t *) = *(int(**)(exception_t *))match_pos; |
---|
| 420 | |
---|
| 421 | int index = matcher(context->current_exception); |
---|
| 422 | _Unwind_Reason_Code ret = (0 == index) |
---|
| 423 | ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND; |
---|
[980fb4e] | 424 | UNWIND_TO_NODE(unwind_exception)->handler_index = index; |
---|
[9cb89b87] | 425 | |
---|
| 426 | // Based on the return value, check if we matched the exception |
---|
| 427 | if (ret == _URC_HANDLER_FOUND) { |
---|
[851fd92] | 428 | __cfadbg_print_safe(exception, " handler found\n"); |
---|
[9cb89b87] | 429 | } else { |
---|
[980fb4e] | 430 | // TODO: Continue the search if there is more in the table. |
---|
[851fd92] | 431 | __cfadbg_print_safe(exception, " no handler\n"); |
---|
[fa4805f] | 432 | } |
---|
[9cb89b87] | 433 | return ret; |
---|
[fa4805f] | 434 | } |
---|
| 435 | |
---|
[9cb89b87] | 436 | // This is only a cleanup handler, ignore it |
---|
[851fd92] | 437 | __cfadbg_print_safe(exception, " no action"); |
---|
[a201f7b] | 438 | } else { |
---|
[9cb89b87] | 439 | // In clean-up phase, no destructors here but this could be the handler. |
---|
| 440 | |
---|
| 441 | if ( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){ |
---|
| 442 | // If this is a potential exception handler |
---|
| 443 | // but not the one that matched the exception in the seach phase, |
---|
| 444 | // just ignore it |
---|
| 445 | goto UNWIND; |
---|
| 446 | } |
---|
[fa4805f] | 447 | |
---|
[9cb89b87] | 448 | // We need to run some clean-up or a handler |
---|
| 449 | // These statment do the right thing but I don't know any specifics at all |
---|
| 450 | _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(0), |
---|
| 451 | (_Unwind_Ptr)unwind_exception ); |
---|
| 452 | _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(1), 0 ); |
---|
[fa4805f] | 453 | |
---|
[9cb89b87] | 454 | // I assume this sets the instruction pointer to the adress of the landing pad |
---|
| 455 | // It doesn't actually set it, it only state the value that needs to be set once we |
---|
| 456 | // return _URC_INSTALL_CONTEXT |
---|
| 457 | _Unwind_SetIP( unwind_context, ((lsd_info.LPStart) + (callsite_landing_pad)) ); |
---|
[fa4805f] | 458 | |
---|
[851fd92] | 459 | __cfadbg_print_safe(exception, " action\n"); |
---|
[fa4805f] | 460 | |
---|
[9cb89b87] | 461 | // Return have some action to run |
---|
| 462 | return _URC_INSTALL_CONTEXT; |
---|
[fa4805f] | 463 | } |
---|
| 464 | } |
---|
| 465 | // No handling found |
---|
[851fd92] | 466 | __cfadbg_print_safe(exception, " table end reached"); |
---|
[fa4805f] | 467 | |
---|
| 468 | UNWIND: |
---|
[851fd92] | 469 | __cfadbg_print_safe(exception, " unwind\n"); |
---|
[fa4805f] | 470 | |
---|
| 471 | // Keep unwinding the stack |
---|
| 472 | return _URC_CONTINUE_UNWIND; |
---|
| 473 | } |
---|
| 474 | |
---|
[0f6ac828] | 475 | #pragma GCC push_options |
---|
[f017af6] | 476 | #pragma GCC optimize(0) |
---|
[0f6ac828] | 477 | |
---|
[eb46fdf] | 478 | // Try statements are hoisted out see comments for details. While this could probably be unique |
---|
| 479 | // and simply linked from libcfa but there is one problem left, see the exception table for details |
---|
[fa4805f] | 480 | __attribute__((noinline)) |
---|
[142930b] | 481 | void __cfaehm_try_terminate(void (*try_block)(), |
---|
| 482 | void (*catch_block)(int index, exception_t * except), |
---|
[0304215a] | 483 | __attribute__((unused)) int (*match_block)(exception_t * except)) { |
---|
[fa4805f] | 484 | //! volatile int xy = 0; |
---|
[142930b] | 485 | //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy); |
---|
[fa4805f] | 486 | |
---|
[eb46fdf] | 487 | // Setup the personality routine and exception table. |
---|
[9cb89b87] | 488 | // Unforturnately these clobber gcc cancellation support which means we can't get access to |
---|
| 489 | // the attribute cleanup tables at the same time. We would have to inspect the assembly to |
---|
| 490 | // create a new set ourselves. |
---|
[3b9c674] | 491 | #ifdef __PIC__ |
---|
| 492 | asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0"); |
---|
| 493 | asm volatile (".cfi_lsda 0x1b, .LLSDACFA2"); |
---|
| 494 | #else |
---|
[fa4805f] | 495 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0"); |
---|
| 496 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2"); |
---|
[3b9c674] | 497 | #endif |
---|
[fa4805f] | 498 | |
---|
[b947fb2] | 499 | // Label which defines the start of the area for which the handler is setup. |
---|
[fa4805f] | 500 | asm volatile (".TRYSTART:"); |
---|
| 501 | |
---|
| 502 | // The actual statements of the try blocks |
---|
| 503 | try_block(); |
---|
| 504 | |
---|
| 505 | // asm statement to prevent deadcode removal |
---|
| 506 | asm volatile goto ("" : : : : CATCH ); |
---|
| 507 | |
---|
[eb46fdf] | 508 | // Normal return for when there is no throw. |
---|
[142930b] | 509 | return; |
---|
[fa4805f] | 510 | |
---|
| 511 | // Exceptionnal path |
---|
| 512 | CATCH : __attribute__(( unused )); |
---|
[b947fb2] | 513 | // Label which defines the end of the area for which the handler is setup. |
---|
[fa4805f] | 514 | asm volatile (".TRYEND:"); |
---|
[9cb89b87] | 515 | // Label which defines the start of the exception landing pad. Basically what is called when |
---|
| 516 | // the exception is caught. Note, if multiple handlers are given, the multiplexing should be |
---|
| 517 | // done by the generated code, not the exception runtime. |
---|
[fa4805f] | 518 | asm volatile (".CATCH:"); |
---|
| 519 | |
---|
[142930b] | 520 | // Exception handler |
---|
| 521 | // Note: Saving the exception context on the stack breaks termination exceptions. |
---|
| 522 | catch_block( EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index, |
---|
| 523 | this_exception_context()->current_exception ); |
---|
[fa4805f] | 524 | } |
---|
| 525 | |
---|
[eb46fdf] | 526 | // Exception table data we need to generate. While this is almost generic, the custom data refers |
---|
| 527 | // to {*}try_terminate, which is no way generic. Some more works need to be done if we want to |
---|
| 528 | // have a single call to the try routine. |
---|
[b947fb2] | 529 | |
---|
[3b9c674] | 530 | #ifdef __PIC__ |
---|
| 531 | asm ( |
---|
| 532 | // HEADER |
---|
| 533 | ".LFECFA1:\n" |
---|
[e8261bb] | 534 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
[3b9c674] | 535 | " .globl __gcfa_personality_v0\n" |
---|
[e8261bb] | 536 | #else // defined( __ARM_ARCH ) |
---|
| 537 | " .global __gcfa_personality_v0\n" |
---|
| 538 | #endif |
---|
[3b9c674] | 539 | " .section .gcc_except_table,\"a\",@progbits\n" |
---|
| 540 | // TABLE HEADER (important field is the BODY length at the end) |
---|
| 541 | ".LLSDACFA2:\n" |
---|
| 542 | " .byte 0xff\n" |
---|
| 543 | " .byte 0xff\n" |
---|
| 544 | " .byte 0x1\n" |
---|
| 545 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" |
---|
| 546 | // BODY (language specific data) |
---|
| 547 | // This uses language specific data and can be modified arbitrarily |
---|
| 548 | // We use handled area offset, handled area length, |
---|
| 549 | // handler landing pad offset and 1 (action code, gcc seems to use 0). |
---|
| 550 | ".LLSDACSBCFA2:\n" |
---|
[3090127] | 551 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
[3b9c674] | 552 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
[3090127] | 553 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
[3b9c674] | 554 | " .uleb128 1\n" |
---|
| 555 | ".LLSDACSECFA2:\n" |
---|
| 556 | // TABLE FOOTER |
---|
| 557 | " .text\n" |
---|
[3090127] | 558 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
[3b9c674] | 559 | ); |
---|
| 560 | |
---|
| 561 | // Somehow this piece of helps with the resolution of debug symbols. |
---|
| 562 | __attribute__((unused)) static const int dummy = 0; |
---|
| 563 | |
---|
| 564 | asm ( |
---|
| 565 | // Add a hidden symbol which points at the function. |
---|
| 566 | " .hidden CFA.ref.__gcfa_personality_v0\n" |
---|
| 567 | " .weak CFA.ref.__gcfa_personality_v0\n" |
---|
[e8261bb] | 568 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
[3b9c674] | 569 | " .align 8\n" |
---|
[e8261bb] | 570 | #else // defined( __ARM_ARCH ) |
---|
| 571 | " .align 3\n" |
---|
| 572 | #endif |
---|
[3b9c674] | 573 | " .type CFA.ref.__gcfa_personality_v0, @object\n" |
---|
| 574 | " .size CFA.ref.__gcfa_personality_v0, 8\n" |
---|
| 575 | "CFA.ref.__gcfa_personality_v0:\n" |
---|
| 576 | #if defined( __x86_64 ) |
---|
| 577 | " .quad __gcfa_personality_v0\n" |
---|
[e8261bb] | 578 | #elif defined( __i386 ) |
---|
[e045590] | 579 | " .long __gcfa_personality_v0\n" |
---|
[e8261bb] | 580 | #else // defined( __ARM_ARCH ) |
---|
| 581 | " .xword __gcfa_personality_v0\n" |
---|
[3b9c674] | 582 | #endif |
---|
| 583 | ); |
---|
| 584 | #else // __PIC__ |
---|
[fa4805f] | 585 | asm ( |
---|
[eb46fdf] | 586 | // HEADER |
---|
[fa4805f] | 587 | ".LFECFA1:\n" |
---|
[e8261bb] | 588 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
[fa4805f] | 589 | " .globl __gcfa_personality_v0\n" |
---|
[e8261bb] | 590 | #else // defined( __ARM_ARCH ) |
---|
| 591 | " .global __gcfa_personality_v0\n" |
---|
| 592 | #endif |
---|
[fa4805f] | 593 | " .section .gcc_except_table,\"a\",@progbits\n" |
---|
[eb46fdf] | 594 | // TABLE HEADER (important field is the BODY length at the end) |
---|
| 595 | ".LLSDACFA2:\n" |
---|
[fa4805f] | 596 | " .byte 0xff\n" |
---|
| 597 | " .byte 0xff\n" |
---|
| 598 | " .byte 0x1\n" |
---|
[eb46fdf] | 599 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" |
---|
| 600 | // BODY (language specific data) |
---|
| 601 | ".LLSDACSBCFA2:\n" |
---|
| 602 | // Handled area start (relative to start of function) |
---|
[3090127] | 603 | " .uleb128 .TRYSTART-__cfaehm_try_terminate\n" |
---|
[eb46fdf] | 604 | // Handled area length |
---|
| 605 | " .uleb128 .TRYEND-.TRYSTART\n" |
---|
| 606 | // Handler landing pad address (relative to start of function) |
---|
[3090127] | 607 | " .uleb128 .CATCH-__cfaehm_try_terminate\n" |
---|
[eb46fdf] | 608 | // Action code, gcc seems to always use 0. |
---|
| 609 | " .uleb128 1\n" |
---|
| 610 | // TABLE FOOTER |
---|
| 611 | ".LLSDACSECFA2:\n" |
---|
| 612 | " .text\n" |
---|
[3090127] | 613 | " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" |
---|
[fa4805f] | 614 | " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n" |
---|
[3b9c674] | 615 | " .section .note.GNU-stack,\"x\",@progbits\n" |
---|
[fa4805f] | 616 | ); |
---|
[3b9c674] | 617 | #endif // __PIC__ |
---|
| 618 | |
---|
| 619 | #pragma GCC pop_options |
---|
[915aa11] | 620 | |
---|
| 621 | #else |
---|
| 622 | #error unsupported hardware architecture |
---|
[e8261bb] | 623 | #endif // __x86_64 || __i386 || __ARM_ARCH |
---|