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