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