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