| [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 | 
|---|
| [c960331] | 11 | // Last Modified By : Andrew Beach | 
|---|
| [ecfd758] | 12 | // Last Modified On : Wed Feb 24 13:40:00 2021 | 
|---|
|  | 13 | // Update Count     : 36 | 
|---|
| [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 |  | 
|---|
|  | 30 | #pragma GCC visibility push(default) | 
|---|
|  | 31 |  | 
|---|
| [fa4805f] | 32 | #include "lsda.h" | 
|---|
|  | 33 |  | 
|---|
| [73530d9] | 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; | 
|---|
| [cbce272] | 40 |  | 
|---|
| [ecfd758] | 41 | // Base Exception type id: | 
|---|
| [8f910430] | 42 | struct __cfavir_type_info __cfatid_exception_t = { | 
|---|
| [ecfd758] | 43 | NULL, | 
|---|
| [cbce272] | 44 | }; | 
|---|
|  | 45 |  | 
|---|
|  | 46 |  | 
|---|
| [86d5ba7c] | 47 | // Get the current exception context. | 
|---|
|  | 48 | // There can be a single global until multithreading occurs, then each stack | 
|---|
| [5715d43] | 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}; | 
|---|
| [86d5ba7c] | 52 | return &shared_stack; | 
|---|
|  | 53 | } | 
|---|
| [fa4805f] | 54 |  | 
|---|
|  | 55 |  | 
|---|
|  | 56 | // RESUMPTION ================================================================ | 
|---|
|  | 57 |  | 
|---|
| [f1b6671] | 58 | static void reset_top_resume(struct __cfaehm_try_resume_node ** store) { | 
|---|
|  | 59 | this_exception_context()->top_resume = *store; | 
|---|
|  | 60 | } | 
|---|
|  | 61 |  | 
|---|
| [046a890] | 62 | void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) { | 
|---|
| [2a3b019] | 63 | struct exception_context_t * context = this_exception_context(); | 
|---|
| [fa4805f] | 64 |  | 
|---|
| [851fd92] | 65 | __cfadbg_print_safe(exception, "Throwing resumption exception\n"); | 
|---|
| [fa4805f] | 66 |  | 
|---|
| [381132b] | 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 | } | 
|---|
| [fa4805f] | 77 | } | 
|---|
| [381132b] | 78 | } // End the search and return to the top of the stack. | 
|---|
| [fa4805f] | 79 |  | 
|---|
| [046a890] | 80 | // No handler found, fall back to the default operation. | 
|---|
| [851fd92] | 81 | __cfadbg_print_safe(exception, "Unhandled exception\n"); | 
|---|
| [046a890] | 82 | defaultHandler(except); | 
|---|
| [fa4805f] | 83 | } | 
|---|
|  | 84 |  | 
|---|
| [eb46fdf] | 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. | 
|---|
| [b947fb2] | 88 |  | 
|---|
| [3090127] | 89 | void __cfaehm_try_resume_setup(struct __cfaehm_try_resume_node * node, | 
|---|
| [0304215a] | 90 | _Bool (*handler)(exception_t * except)) { | 
|---|
| [2a3b019] | 91 | struct exception_context_t * context = this_exception_context(); | 
|---|
|  | 92 | node->next = context->top_resume; | 
|---|
| [307a732] | 93 | node->handler = handler; | 
|---|
| [2a3b019] | 94 | context->top_resume = node; | 
|---|
| [fa4805f] | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [3090127] | 97 | void __cfaehm_try_resume_cleanup(struct __cfaehm_try_resume_node * node) { | 
|---|
| [2a3b019] | 98 | struct exception_context_t * context = this_exception_context(); | 
|---|
|  | 99 | context->top_resume = node->next; | 
|---|
| [fa4805f] | 100 | } | 
|---|
|  | 101 |  | 
|---|
|  | 102 |  | 
|---|
| [2922871] | 103 | // MEMORY MANAGEMENT ========================================================= | 
|---|
| [ff7ff14a] | 104 |  | 
|---|
| [980fb4e] | 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 |  | 
|---|
| [73530d9] | 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 |  | 
|---|
| [86d5ba7c] | 124 | // Creates a copy of the indicated exception and sets current_exception to it. | 
|---|
| [3090127] | 125 | static void __cfaehm_allocate_exception( exception_t * except ) { | 
|---|
| [86d5ba7c] | 126 | struct exception_context_t * context = this_exception_context(); | 
|---|
|  | 127 |  | 
|---|
| [ff7ff14a] | 128 | // Allocate memory for the exception. | 
|---|
| [3090127] | 129 | struct __cfaehm_node * store = malloc( | 
|---|
|  | 130 | sizeof( struct __cfaehm_node ) + except->virtual_table->size ); | 
|---|
| [ff7ff14a] | 131 |  | 
|---|
|  | 132 | if ( ! store ) { | 
|---|
|  | 133 | // Failure: cannot allocate exception. Terminate thread. | 
|---|
|  | 134 | abort(); // <- Although I think it might be the process. | 
|---|
| [86d5ba7c] | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [980fb4e] | 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 ); | 
|---|
| [73530d9] | 143 |  | 
|---|
| [980fb4e] | 144 | // Add the node to the list: | 
|---|
|  | 145 | store->next = NULL_MAP(EXCEPT_TO_NODE, context->current_exception); | 
|---|
|  | 146 | context->current_exception = except_store; | 
|---|
| [86d5ba7c] | 147 | } | 
|---|
|  | 148 |  | 
|---|
|  | 149 | // Delete the provided exception, unsetting current_exception if relivant. | 
|---|
| [3090127] | 150 | static void __cfaehm_delete_exception( exception_t * except ) { | 
|---|
| [86d5ba7c] | 151 | struct exception_context_t * context = this_exception_context(); | 
|---|
|  | 152 |  | 
|---|
| [851fd92] | 153 | __cfadbg_print_safe(exception, "Deleting Exception\n"); | 
|---|
| [ff7ff14a] | 154 |  | 
|---|
|  | 155 | // Remove the exception from the list. | 
|---|
| [3090127] | 156 | struct __cfaehm_node * to_free = EXCEPT_TO_NODE(except); | 
|---|
|  | 157 | struct __cfaehm_node * node; | 
|---|
| [86d5ba7c] | 158 |  | 
|---|
|  | 159 | if ( context->current_exception == except ) { | 
|---|
| [ff7ff14a] | 160 | node = to_free->next; | 
|---|
| [980fb4e] | 161 | context->current_exception = NULL_MAP(NODE_TO_EXCEPT, node); | 
|---|
| [86d5ba7c] | 162 | } else { | 
|---|
| [ff7ff14a] | 163 | node = EXCEPT_TO_NODE(context->current_exception); | 
|---|
|  | 164 | // It may always be in the first or second position. | 
|---|
| [9cb89b87] | 165 | while ( to_free != node->next ) { | 
|---|
| [ff7ff14a] | 166 | node = node->next; | 
|---|
|  | 167 | } | 
|---|
|  | 168 | node->next = to_free->next; | 
|---|
| [86d5ba7c] | 169 | } | 
|---|
| [ff7ff14a] | 170 |  | 
|---|
|  | 171 | // Free the old exception node. | 
|---|
| [cbce272] | 172 | except->virtual_table->free( except ); | 
|---|
| [ff7ff14a] | 173 | free( to_free ); | 
|---|
| [86d5ba7c] | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [2922871] | 176 | // CANCELLATION ============================================================== | 
|---|
| [fa4805f] | 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, | 
|---|
| [3090127] | 183 | _Unwind_Exception_Class exception_class, | 
|---|
| [fa4805f] | 184 | struct _Unwind_Exception * unwind_exception, | 
|---|
| [2a3b019] | 185 | struct _Unwind_Context * unwind_context, | 
|---|
|  | 186 | void * stop_param) { | 
|---|
| [a201f7b] | 187 | // Verify actions follow the rules we expect. | 
|---|
| [d119d613] | 188 | verify(actions & _UA_CLEANUP_PHASE); | 
|---|
|  | 189 | verify(actions & _UA_FORCE_UNWIND); | 
|---|
|  | 190 | verify(!(actions & _UA_SEARCH_PHASE)); | 
|---|
|  | 191 | verify(!(actions & _UA_HANDLER_FRAME)); | 
|---|
| [fa4805f] | 192 |  | 
|---|
| [a201f7b] | 193 | if ( actions & _UA_END_OF_STACK ) { | 
|---|
| [d119d613] | 194 | abort(); | 
|---|
| [a201f7b] | 195 | } else { | 
|---|
|  | 196 | return _URC_NO_REASON; | 
|---|
|  | 197 | } | 
|---|
| [fa4805f] | 198 | } | 
|---|
|  | 199 |  | 
|---|
| [d119d613] | 200 | __attribute__((weak)) _Unwind_Reason_Code | 
|---|
|  | 201 | __cfaehm_cancellation_unwind( struct _Unwind_Exception * exception ) { | 
|---|
|  | 202 | return _Unwind_ForcedUnwind( exception, _Stop_Fn, (void*)0x22 ); | 
|---|
|  | 203 | } | 
|---|
| [980fb4e] | 204 |  | 
|---|
| [2922871] | 205 | // Cancel the current stack, prefroming approprate clean-up and messaging. | 
|---|
| [979df46] | 206 | void __cfaehm_cancel_stack( exception_t * exception ) { | 
|---|
| [d119d613] | 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 |  | 
|---|
| [2922871] | 221 | _Unwind_Reason_Code ret; | 
|---|
| [d119d613] | 222 | ret = __cfaehm_cancellation_unwind( &node->unwind_exception ); | 
|---|
| [2922871] | 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 |  | 
|---|
| [8ad5752] | 235 | static void __cfaehm_cleanup_default( exception_t ** except ) { | 
|---|
|  | 236 | __cfaehm_delete_exception( *except ); | 
|---|
|  | 237 | *except = NULL; | 
|---|
|  | 238 | } | 
|---|
|  | 239 |  | 
|---|
| [86d5ba7c] | 240 | // The exception that is being thrown must already be stored. | 
|---|
| [046a890] | 241 | static void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) { | 
|---|
| [8ad5752] | 242 | struct exception_context_t * context = this_exception_context(); | 
|---|
|  | 243 | if ( NULL == context->current_exception ) { | 
|---|
| [86d5ba7c] | 244 | printf("UNWIND ERROR missing exception in begin unwind\n"); | 
|---|
|  | 245 | abort(); | 
|---|
|  | 246 | } | 
|---|
| [980fb4e] | 247 | struct _Unwind_Exception * storage = | 
|---|
|  | 248 | &EXCEPT_TO_NODE(context->current_exception)->unwind_exception; | 
|---|
| [fa4805f] | 249 |  | 
|---|
|  | 250 | // Call stdlibc to raise the exception | 
|---|
| [8ad5752] | 251 | __cfadbg_print_safe(exception, "Begin unwinding (storage &p, context %p)\n", storage, context); | 
|---|
|  | 252 | _Unwind_Reason_Code ret = _Unwind_RaiseException( storage ); | 
|---|
| [fa4805f] | 253 |  | 
|---|
| [eb46fdf] | 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. | 
|---|
| [fa4805f] | 260 |  | 
|---|
| [ed395761] | 261 | #if defined( __x86_64 ) || defined( __i386 ) | 
|---|
| [8ad5752] | 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 ) { | 
|---|
| [ed395761] | 264 | #else // defined( __ARM_ARCH ) | 
|---|
|  | 265 | // The return code from _Unwind_RaiseException seems to be corrupt on ARM at end of stack. | 
|---|
| [8108ba8] | 266 | // This workaround tries to keep default exception handling working. | 
|---|
| [ed395761] | 267 | if ( ret == _URC_FATAL_PHASE1_ERROR || ret == _URC_FATAL_PHASE2_ERROR ) { | 
|---|
|  | 268 | #endif | 
|---|
| [8ad5752] | 269 | printf("UNWIND ERROR %d after raise exception\n", ret); | 
|---|
|  | 270 | abort(); | 
|---|
| [fa4805f] | 271 | } | 
|---|
|  | 272 |  | 
|---|
| [8ad5752] | 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 ); | 
|---|
| [fa4805f] | 279 | } | 
|---|
|  | 280 |  | 
|---|
| [046a890] | 281 | void __cfaehm_throw_terminate( exception_t * val, void (*defaultHandler)(exception_t *) ) { | 
|---|
| [851fd92] | 282 | __cfadbg_print_safe(exception, "Throwing termination exception\n"); | 
|---|
| [86d5ba7c] | 283 |  | 
|---|
| [3090127] | 284 | __cfaehm_allocate_exception( val ); | 
|---|
| [046a890] | 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(); | 
|---|
| [86d5ba7c] | 292 | } | 
|---|
|  | 293 |  | 
|---|
| [3090127] | 294 | void __cfaehm_rethrow_terminate(void) { | 
|---|
| [851fd92] | 295 | __cfadbg_print_safe(exception, "Rethrowing termination exception\n"); | 
|---|
| [fa4805f] | 296 |  | 
|---|
| [046a890] | 297 | __cfaehm_begin_unwind( __cfaehm_rethrow_adapter ); | 
|---|
|  | 298 | abort(); | 
|---|
| [fa4805f] | 299 | } | 
|---|
| [ba70330] | 300 |  | 
|---|
| [e8261bb] | 301 | #if defined( __x86_64 ) || defined( __i386 ) || defined( __ARM_ARCH ) | 
|---|
| [eb46fdf] | 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. | 
|---|
| [3090127] | 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) | 
|---|
| [fa4805f] | 311 | { | 
|---|
|  | 312 |  | 
|---|
| [851fd92] | 313 | //__cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(context)); | 
|---|
|  | 314 | __cfadbg_print_safe(exception, "Personality function (%d, %x, %llu, %p, %p):", | 
|---|
| [3090127] | 315 | version, actions, exception_class, unwind_exception, unwind_context); | 
|---|
| [fa4805f] | 316 |  | 
|---|
| [a201f7b] | 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... | 
|---|
| [fa4805f] | 321 | if (actions & _UA_SEARCH_PHASE) { | 
|---|
| [a201f7b] | 322 | verify(actions == _UA_SEARCH_PHASE); | 
|---|
| [851fd92] | 323 | __cfadbg_print_safe(exception, " lookup phase"); | 
|---|
| [a201f7b] | 324 | // ... we are in clean-up phase. | 
|---|
|  | 325 | } else { | 
|---|
|  | 326 | verify(actions & _UA_CLEANUP_PHASE); | 
|---|
| [851fd92] | 327 | __cfadbg_print_safe(exception, " cleanup phase"); | 
|---|
| [a201f7b] | 328 | // We shouldn't be the handler frame during forced unwind. | 
|---|
|  | 329 | if (actions & _UA_HANDLER_FRAME) { | 
|---|
|  | 330 | verify(!(actions & _UA_FORCE_UNWIND)); | 
|---|
| [851fd92] | 331 | __cfadbg_print_safe(exception, " (handler frame)"); | 
|---|
| [a201f7b] | 332 | } else if (actions & _UA_FORCE_UNWIND) { | 
|---|
| [851fd92] | 333 | __cfadbg_print_safe(exception, " (force unwind)"); | 
|---|
| [a201f7b] | 334 | } | 
|---|
| [fa4805f] | 335 | } | 
|---|
|  | 336 |  | 
|---|
|  | 337 | // Get a pointer to the language specific data from which we will read what we need | 
|---|
| [9cb89b87] | 338 | const unsigned char * lsd = _Unwind_GetLanguageSpecificData( unwind_context ); | 
|---|
| [fa4805f] | 339 |  | 
|---|
| [9cb89b87] | 340 | if ( !lsd ) {   //Nothing to do, keep unwinding | 
|---|
| [fa4805f] | 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; | 
|---|
| [2a3b019] | 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(); | 
|---|
| [fa4805f] | 351 |  | 
|---|
|  | 352 | // Linearly search the table for stuff to do | 
|---|
| [9cb89b87] | 353 | while ( cur_ptr < lsd_info.action_table ) { | 
|---|
| [fa4805f] | 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 | 
|---|
| [eb46fdf] | 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); | 
|---|
| [fa4805f] | 364 |  | 
|---|
|  | 365 | // Have we reach the correct frame info yet? | 
|---|
| [9cb89b87] | 366 | if ( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) { | 
|---|
| [e9145a3] | 367 | #ifdef __CFA_DEBUG_PRINT__ | 
|---|
| [fa4805f] | 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; | 
|---|
| [851fd92] | 374 | __cfadbg_print_safe(exception, "\nfound %p - %p (%p, %p, %p), looking for %p\n", | 
|---|
| [eb46fdf] | 375 | bp, ep, ls, cs, cl, ip); | 
|---|
| [e9145a3] | 376 | #endif // __CFA_DEBUG_PRINT__ | 
|---|
| [fa4805f] | 377 | continue; | 
|---|
|  | 378 | } | 
|---|
|  | 379 |  | 
|---|
| [eb46fdf] | 380 | // Have we gone too far? | 
|---|
| [9cb89b87] | 381 | if ( lsd_info.Start + callsite_start > instruction_ptr ) { | 
|---|
| [fa4805f] | 382 | printf(" gone too far"); | 
|---|
|  | 383 | break; | 
|---|
|  | 384 | } | 
|---|
|  | 385 |  | 
|---|
| [9cb89b87] | 386 | // Check for what we must do: | 
|---|
| [2a3b019] | 387 | if ( 0 == callsite_landing_pad ) { | 
|---|
|  | 388 | // Nothing to do, move along | 
|---|
| [851fd92] | 389 | __cfadbg_print_safe(exception, " no landing pad"); | 
|---|
| [9cb89b87] | 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 |  | 
|---|
| [67ca73e] | 413 | _Unwind_Word match_pos = | 
|---|
| [9cb89b87] | 414 | #                               if defined( __x86_64 ) | 
|---|
| [67ca73e] | 415 | _Unwind_GetCFA(unwind_context) + 8; | 
|---|
| [9cb89b87] | 416 | #                               elif defined( __i386 ) | 
|---|
| [67ca73e] | 417 | _Unwind_GetCFA(unwind_context) + 24; | 
|---|
|  | 418 | #                               elif defined( __ARM_ARCH ) | 
|---|
| [e8261bb] | 419 | _Unwind_GetCFA(unwind_context) + 40; | 
|---|
| [9cb89b87] | 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; | 
|---|
| [980fb4e] | 426 | UNWIND_TO_NODE(unwind_exception)->handler_index = index; | 
|---|
| [9cb89b87] | 427 |  | 
|---|
|  | 428 | // Based on the return value, check if we matched the exception | 
|---|
|  | 429 | if (ret == _URC_HANDLER_FOUND) { | 
|---|
| [851fd92] | 430 | __cfadbg_print_safe(exception, " handler found\n"); | 
|---|
| [9cb89b87] | 431 | } else { | 
|---|
| [980fb4e] | 432 | // TODO: Continue the search if there is more in the table. | 
|---|
| [851fd92] | 433 | __cfadbg_print_safe(exception, " no handler\n"); | 
|---|
| [fa4805f] | 434 | } | 
|---|
| [9cb89b87] | 435 | return ret; | 
|---|
| [fa4805f] | 436 | } | 
|---|
|  | 437 |  | 
|---|
| [9cb89b87] | 438 | // This is only a cleanup handler, ignore it | 
|---|
| [851fd92] | 439 | __cfadbg_print_safe(exception, " no action"); | 
|---|
| [a201f7b] | 440 | } else { | 
|---|
| [9cb89b87] | 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 | } | 
|---|
| [fa4805f] | 449 |  | 
|---|
| [9cb89b87] | 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 ); | 
|---|
| [fa4805f] | 455 |  | 
|---|
| [9cb89b87] | 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)) ); | 
|---|
| [fa4805f] | 460 |  | 
|---|
| [851fd92] | 461 | __cfadbg_print_safe(exception, " action\n"); | 
|---|
| [fa4805f] | 462 |  | 
|---|
| [9cb89b87] | 463 | // Return have some action to run | 
|---|
|  | 464 | return _URC_INSTALL_CONTEXT; | 
|---|
| [fa4805f] | 465 | } | 
|---|
|  | 466 | } | 
|---|
|  | 467 | // No handling found | 
|---|
| [851fd92] | 468 | __cfadbg_print_safe(exception, " table end reached"); | 
|---|
| [fa4805f] | 469 |  | 
|---|
|  | 470 | UNWIND: | 
|---|
| [851fd92] | 471 | __cfadbg_print_safe(exception, " unwind\n"); | 
|---|
| [fa4805f] | 472 |  | 
|---|
|  | 473 | // Keep unwinding the stack | 
|---|
|  | 474 | return _URC_CONTINUE_UNWIND; | 
|---|
|  | 475 | } | 
|---|
|  | 476 |  | 
|---|
| [0f6ac828] | 477 | #pragma GCC push_options | 
|---|
| [f017af6] | 478 | #pragma GCC optimize(0) | 
|---|
| [0f6ac828] | 479 |  | 
|---|
| [eb46fdf] | 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 | 
|---|
| [fa4805f] | 482 | __attribute__((noinline)) | 
|---|
| [3090127] | 483 | void __cfaehm_try_terminate(void (*try_block)(), | 
|---|
| [0304215a] | 484 | void (*catch_block)(int index, exception_t * except), | 
|---|
|  | 485 | __attribute__((unused)) int (*match_block)(exception_t * except)) { | 
|---|
| [fa4805f] | 486 | //! volatile int xy = 0; | 
|---|
|  | 487 | //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy); | 
|---|
|  | 488 |  | 
|---|
| [eb46fdf] | 489 | // Setup the personality routine and exception table. | 
|---|
| [9cb89b87] | 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. | 
|---|
| [3b9c674] | 493 | #ifdef __PIC__ | 
|---|
|  | 494 | asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0"); | 
|---|
|  | 495 | asm volatile (".cfi_lsda 0x1b, .LLSDACFA2"); | 
|---|
|  | 496 | #else | 
|---|
| [fa4805f] | 497 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0"); | 
|---|
|  | 498 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2"); | 
|---|
| [3b9c674] | 499 | #endif | 
|---|
| [fa4805f] | 500 |  | 
|---|
| [b947fb2] | 501 | // Label which defines the start of the area for which the handler is setup. | 
|---|
| [fa4805f] | 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 |  | 
|---|
| [eb46fdf] | 510 | // Normal return for when there is no throw. | 
|---|
| [fa4805f] | 511 | return; | 
|---|
|  | 512 |  | 
|---|
|  | 513 | // Exceptionnal path | 
|---|
|  | 514 | CATCH : __attribute__(( unused )); | 
|---|
| [b947fb2] | 515 | // Label which defines the end of the area for which the handler is setup. | 
|---|
| [fa4805f] | 516 | asm volatile (".TRYEND:"); | 
|---|
| [9cb89b87] | 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. | 
|---|
| [fa4805f] | 520 | asm volatile (".CATCH:"); | 
|---|
|  | 521 |  | 
|---|
|  | 522 | // Exception handler | 
|---|
| [2a3b019] | 523 | // Note: Saving the exception context on the stack breaks termination exceptions. | 
|---|
| [980fb4e] | 524 | catch_block( EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index, | 
|---|
| [2a3b019] | 525 | this_exception_context()->current_exception ); | 
|---|
| [fa4805f] | 526 | } | 
|---|
|  | 527 |  | 
|---|
| [eb46fdf] | 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. | 
|---|
| [b947fb2] | 531 |  | 
|---|
| [3b9c674] | 532 | #ifdef __PIC__ | 
|---|
|  | 533 | asm ( | 
|---|
|  | 534 | // HEADER | 
|---|
|  | 535 | ".LFECFA1:\n" | 
|---|
| [e8261bb] | 536 | #if defined( __x86_64 ) || defined( __i386 ) | 
|---|
| [3b9c674] | 537 | "       .globl  __gcfa_personality_v0\n" | 
|---|
| [e8261bb] | 538 | #else // defined( __ARM_ARCH ) | 
|---|
|  | 539 | "       .global __gcfa_personality_v0\n" | 
|---|
|  | 540 | #endif | 
|---|
| [3b9c674] | 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" | 
|---|
| [3090127] | 553 | "       .uleb128 .TRYSTART-__cfaehm_try_terminate\n" | 
|---|
| [3b9c674] | 554 | "       .uleb128 .TRYEND-.TRYSTART\n" | 
|---|
| [3090127] | 555 | "       .uleb128 .CATCH-__cfaehm_try_terminate\n" | 
|---|
| [3b9c674] | 556 | "       .uleb128 1\n" | 
|---|
|  | 557 | ".LLSDACSECFA2:\n" | 
|---|
|  | 558 | // TABLE FOOTER | 
|---|
|  | 559 | "       .text\n" | 
|---|
| [3090127] | 560 | "       .size   __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" | 
|---|
| [3b9c674] | 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" | 
|---|
| [e8261bb] | 572 | #if defined( __x86_64 ) || defined( __i386 ) | 
|---|
| [3b9c674] | 573 | "       .align 8\n" | 
|---|
| [e8261bb] | 574 | #else // defined( __ARM_ARCH ) | 
|---|
|  | 575 | "       .align 3\n" | 
|---|
|  | 576 | #endif | 
|---|
| [3b9c674] | 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" | 
|---|
| [e8261bb] | 582 | #elif defined( __i386 ) | 
|---|
| [e045590] | 583 | "       .long __gcfa_personality_v0\n" | 
|---|
| [e8261bb] | 584 | #else // defined( __ARM_ARCH ) | 
|---|
|  | 585 | "       .xword __gcfa_personality_v0\n" | 
|---|
| [3b9c674] | 586 | #endif | 
|---|
|  | 587 | ); | 
|---|
|  | 588 | #else // __PIC__ | 
|---|
| [fa4805f] | 589 | asm ( | 
|---|
| [eb46fdf] | 590 | // HEADER | 
|---|
| [fa4805f] | 591 | ".LFECFA1:\n" | 
|---|
| [e8261bb] | 592 | #if defined( __x86_64 ) || defined( __i386 ) | 
|---|
| [fa4805f] | 593 | "       .globl  __gcfa_personality_v0\n" | 
|---|
| [e8261bb] | 594 | #else // defined( __ARM_ARCH ) | 
|---|
|  | 595 | "       .global __gcfa_personality_v0\n" | 
|---|
|  | 596 | #endif | 
|---|
| [fa4805f] | 597 | "       .section        .gcc_except_table,\"a\",@progbits\n" | 
|---|
| [eb46fdf] | 598 | // TABLE HEADER (important field is the BODY length at the end) | 
|---|
|  | 599 | ".LLSDACFA2:\n" | 
|---|
| [fa4805f] | 600 | "       .byte   0xff\n" | 
|---|
|  | 601 | "       .byte   0xff\n" | 
|---|
|  | 602 | "       .byte   0x1\n" | 
|---|
| [eb46fdf] | 603 | "       .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" | 
|---|
|  | 604 | // BODY (language specific data) | 
|---|
|  | 605 | ".LLSDACSBCFA2:\n" | 
|---|
|  | 606 | //      Handled area start (relative to start of function) | 
|---|
| [3090127] | 607 | "       .uleb128 .TRYSTART-__cfaehm_try_terminate\n" | 
|---|
| [eb46fdf] | 608 | //      Handled area length | 
|---|
|  | 609 | "       .uleb128 .TRYEND-.TRYSTART\n" | 
|---|
|  | 610 | //      Handler landing pad address (relative to start of function) | 
|---|
| [3090127] | 611 | "       .uleb128 .CATCH-__cfaehm_try_terminate\n" | 
|---|
| [eb46fdf] | 612 | //      Action code, gcc seems to always use 0. | 
|---|
|  | 613 | "       .uleb128 1\n" | 
|---|
|  | 614 | // TABLE FOOTER | 
|---|
|  | 615 | ".LLSDACSECFA2:\n" | 
|---|
|  | 616 | "       .text\n" | 
|---|
| [3090127] | 617 | "       .size   __cfaehm_try_terminate, .-__cfaehm_try_terminate\n" | 
|---|
| [fa4805f] | 618 | "       .ident  \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n" | 
|---|
| [3b9c674] | 619 | "       .section        .note.GNU-stack,\"x\",@progbits\n" | 
|---|
| [fa4805f] | 620 | ); | 
|---|
| [3b9c674] | 621 | #endif // __PIC__ | 
|---|
|  | 622 |  | 
|---|
|  | 623 | #pragma GCC pop_options | 
|---|
| [915aa11] | 624 |  | 
|---|
|  | 625 | #else | 
|---|
|  | 626 | #error unsupported hardware architecture | 
|---|
| [e8261bb] | 627 | #endif // __x86_64 || __i386 || __ARM_ARCH | 
|---|