| [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
 | 
|---|
| [381fdee] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [0304215a] | 12 | // Last Modified On : Thu Feb 22 18:17:34 2018
 | 
|---|
 | 13 | // Update Count     : 11
 | 
|---|
| [fa4805f] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [cbce272] | 16 | #include <stddef.h> // for size_t
 | 
|---|
 | 17 | 
 | 
|---|
| [fa4805f] | 18 | #include "exception.h"
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | // Implementation of the secret header.
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include <stdlib.h>
 | 
|---|
 | 23 | #include <stdio.h>
 | 
|---|
 | 24 | #include <unwind.h>
 | 
|---|
| [73abe95] | 25 | #include <bits/debug.hfa>
 | 
|---|
| [fa4805f] | 26 | 
 | 
|---|
| [b947fb2] | 27 | // FIX ME: temporary hack to keep ARM build working
 | 
|---|
 | 28 | #ifndef _URC_FATAL_PHASE1_ERROR
 | 
|---|
 | 29 | #define _URC_FATAL_PHASE1_ERROR 2
 | 
|---|
 | 30 | #endif // ! _URC_FATAL_PHASE1_ERROR
 | 
|---|
 | 31 | #ifndef _URC_FATAL_PHASE2_ERROR
 | 
|---|
 | 32 | #define _URC_FATAL_PHASE2_ERROR 2
 | 
|---|
 | 33 | #endif // ! _URC_FATAL_PHASE2_ERROR
 | 
|---|
 | 34 | 
 | 
|---|
| [fa4805f] | 35 | #include "lsda.h"
 | 
|---|
 | 36 | 
 | 
|---|
| [cbce272] | 37 | 
 | 
|---|
 | 38 | // Base exception vtable is abstract, you should not have base exceptions.
 | 
|---|
| [36982fc] | 39 | struct __cfaabi_ehm__base_exception_t_vtable
 | 
|---|
 | 40 |                 ___cfaabi_ehm__base_exception_t_vtable_instance = {
 | 
|---|
| [cbce272] | 41 |         .parent = NULL,
 | 
|---|
 | 42 |         .size = 0,
 | 
|---|
 | 43 |         .copy = NULL,
 | 
|---|
 | 44 |         .free = NULL,
 | 
|---|
 | 45 |         .msg = NULL
 | 
|---|
 | 46 | };
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | 
 | 
|---|
| [fa4805f] | 49 | // Temperary global exception context. Does not work with concurency.
 | 
|---|
| [86d5ba7c] | 50 | struct exception_context_t {
 | 
|---|
| [36982fc] | 51 |     struct __cfaabi_ehm__try_resume_node * top_resume;
 | 
|---|
 | 52 |     struct __cfaabi_ehm__try_resume_node * current_resume;
 | 
|---|
| [fa4805f] | 53 | 
 | 
|---|
| [0304215a] | 54 |     exception_t * current_exception;
 | 
|---|
| [fa4805f] | 55 |     int current_handler_index;
 | 
|---|
| [cbce272] | 56 | } shared_stack = {NULL, NULL, 0, 0};
 | 
|---|
| [86d5ba7c] | 57 | 
 | 
|---|
 | 58 | // Get the current exception context.
 | 
|---|
 | 59 | // There can be a single global until multithreading occurs, then each stack
 | 
|---|
 | 60 | // needs its own. It will have to be updated to handle that.
 | 
|---|
 | 61 | struct exception_context_t * this_exception_context() {
 | 
|---|
 | 62 |         return &shared_stack;
 | 
|---|
 | 63 | }
 | 
|---|
 | 64 | //#define SAVE_EXCEPTION_CONTEXT(to_name)
 | 
|---|
 | 65 | //struct exception_context_t * to_name = this_exception_context();
 | 
|---|
 | 66 | //exception * this_exception() {
 | 
|---|
 | 67 | //    return this_exception_context()->current_exception;
 | 
|---|
 | 68 | //}
 | 
|---|
| [fa4805f] | 69 | 
 | 
|---|
 | 70 | 
 | 
|---|
| [b947fb2] | 71 | // This macro should be the only thing that needs to change across machines.  Used in the personality function, way down
 | 
|---|
 | 72 | // in termination.
 | 
|---|
| [0304215a] | 73 | // struct _Unwind_Context * -> _Unwind_Reason_Code(*)(exception_t *)
 | 
|---|
| [fa4805f] | 74 | #define MATCHER_FROM_CONTEXT(ptr_to_context) \
 | 
|---|
| [0304215a] | 75 |         (*(_Unwind_Reason_Code(**)(exception_t *))(_Unwind_GetCFA(ptr_to_context) + 8))
 | 
|---|
| [fa4805f] | 76 | 
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | // RESUMPTION ================================================================
 | 
|---|
 | 79 | 
 | 
|---|
| [0304215a] | 80 | void __cfaabi_ehm__throw_resume(exception_t * except) {
 | 
|---|
| [fa4805f] | 81 | 
 | 
|---|
| [36982fc] | 82 |         __cfaabi_dbg_print_safe("Throwing resumption exception\n");
 | 
|---|
| [fa4805f] | 83 | 
 | 
|---|
| [36982fc] | 84 |         struct __cfaabi_ehm__try_resume_node * original_head = shared_stack.current_resume;
 | 
|---|
 | 85 |         struct __cfaabi_ehm__try_resume_node * current =
 | 
|---|
| [fa4805f] | 86 |                 (original_head) ? original_head->next : shared_stack.top_resume;
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |         for ( ; current ; current = current->next) {
 | 
|---|
 | 89 |                 shared_stack.current_resume = current;
 | 
|---|
| [307a732] | 90 |                 if (current->handler(except)) {
 | 
|---|
| [fa4805f] | 91 |                         shared_stack.current_resume = original_head;
 | 
|---|
 | 92 |                         return;
 | 
|---|
 | 93 |                 }
 | 
|---|
 | 94 |         }
 | 
|---|
 | 95 | 
 | 
|---|
| [36982fc] | 96 |         __cfaabi_dbg_print_safe("Unhandled exception\n");
 | 
|---|
| [fa4805f] | 97 |         shared_stack.current_resume = original_head;
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 |         // Fall back to termination:
 | 
|---|
| [36982fc] | 100 |         __cfaabi_ehm__throw_terminate(except);
 | 
|---|
| [fa4805f] | 101 |         // TODO: Default handler for resumption.
 | 
|---|
 | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
| [b947fb2] | 104 | // Do we control where exceptions get thrown even with concurency?  If not these are not quite thread safe, the cleanup
 | 
|---|
 | 105 | // hook has to be added after the node is built but before it is made the top node.
 | 
|---|
 | 106 | 
 | 
|---|
| [36982fc] | 107 | void __cfaabi_ehm__try_resume_setup(struct __cfaabi_ehm__try_resume_node * node,
 | 
|---|
| [0304215a] | 108 |                         _Bool (*handler)(exception_t * except)) {
 | 
|---|
| [fa4805f] | 109 |         node->next = shared_stack.top_resume;
 | 
|---|
| [307a732] | 110 |         node->handler = handler;
 | 
|---|
| [fa4805f] | 111 |         shared_stack.top_resume = node;
 | 
|---|
 | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
| [36982fc] | 114 | void __cfaabi_ehm__try_resume_cleanup(struct __cfaabi_ehm__try_resume_node * node) {
 | 
|---|
| [fa4805f] | 115 |         shared_stack.top_resume = node->next;
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | // TERMINATION ===============================================================
 | 
|---|
 | 120 | 
 | 
|---|
| [86d5ba7c] | 121 | // MEMORY MANAGEMENT (still for integers)
 | 
|---|
| [ff7ff14a] | 122 | // May have to move to cfa for constructors and destructors (references).
 | 
|---|
 | 123 | 
 | 
|---|
| [36982fc] | 124 | struct __cfaabi_ehm__node {
 | 
|---|
 | 125 |         struct __cfaabi_ehm__node * next;
 | 
|---|
| [ff7ff14a] | 126 | };
 | 
|---|
 | 127 | 
 | 
|---|
| [0304215a] | 128 | #define NODE_TO_EXCEPT(node) ((exception_t *)(1 + (node)))
 | 
|---|
| [36982fc] | 129 | #define EXCEPT_TO_NODE(except) ((struct __cfaabi_ehm__node *)(except) - 1)
 | 
|---|
| [86d5ba7c] | 130 | 
 | 
|---|
 | 131 | // Creates a copy of the indicated exception and sets current_exception to it.
 | 
|---|
| [0304215a] | 132 | static void __cfaabi_ehm__allocate_exception( exception_t * except ) {
 | 
|---|
| [86d5ba7c] | 133 |         struct exception_context_t * context = this_exception_context();
 | 
|---|
 | 134 | 
 | 
|---|
| [ff7ff14a] | 135 |         // Allocate memory for the exception.
 | 
|---|
| [36982fc] | 136 |         struct __cfaabi_ehm__node * store = malloc(
 | 
|---|
 | 137 |                 sizeof( struct __cfaabi_ehm__node ) + except->virtual_table->size );
 | 
|---|
| [ff7ff14a] | 138 | 
 | 
|---|
 | 139 |         if ( ! store ) {
 | 
|---|
 | 140 |                 // Failure: cannot allocate exception. Terminate thread.
 | 
|---|
 | 141 |                 abort(); // <- Although I think it might be the process.
 | 
|---|
| [86d5ba7c] | 142 |         }
 | 
|---|
 | 143 | 
 | 
|---|
| [ff7ff14a] | 144 |         // Add the node to the list:
 | 
|---|
 | 145 |         store->next = EXCEPT_TO_NODE(context->current_exception);
 | 
|---|
 | 146 |         context->current_exception = NODE_TO_EXCEPT(store);
 | 
|---|
 | 147 | 
 | 
|---|
| [86d5ba7c] | 148 |         // Copy the exception to storage.
 | 
|---|
| [cbce272] | 149 |         except->virtual_table->copy( context->current_exception, except );
 | 
|---|
| [86d5ba7c] | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 | // Delete the provided exception, unsetting current_exception if relivant.
 | 
|---|
| [0304215a] | 153 | static void __cfaabi_ehm__delete_exception( exception_t * except ) {
 | 
|---|
| [86d5ba7c] | 154 |         struct exception_context_t * context = this_exception_context();
 | 
|---|
 | 155 | 
 | 
|---|
| [36982fc] | 156 |         __cfaabi_dbg_print_safe("Deleting Exception\n");
 | 
|---|
| [ff7ff14a] | 157 | 
 | 
|---|
 | 158 |         // Remove the exception from the list.
 | 
|---|
| [36982fc] | 159 |         struct __cfaabi_ehm__node * to_free = EXCEPT_TO_NODE(except);
 | 
|---|
 | 160 |         struct __cfaabi_ehm__node * node;
 | 
|---|
| [86d5ba7c] | 161 | 
 | 
|---|
 | 162 |         if ( context->current_exception == except ) {
 | 
|---|
| [ff7ff14a] | 163 |                 node = to_free->next;
 | 
|---|
 | 164 |                 context->current_exception = (node) ? NODE_TO_EXCEPT(node) : 0;
 | 
|---|
| [86d5ba7c] | 165 |         } else {
 | 
|---|
| [ff7ff14a] | 166 |                 node = EXCEPT_TO_NODE(context->current_exception);
 | 
|---|
 | 167 |                 // It may always be in the first or second position.
 | 
|---|
 | 168 |                 while( to_free != node->next ) {
 | 
|---|
 | 169 |                         node = node->next;
 | 
|---|
 | 170 |                 }
 | 
|---|
 | 171 |                 node->next = to_free->next;
 | 
|---|
| [86d5ba7c] | 172 |         }
 | 
|---|
| [ff7ff14a] | 173 | 
 | 
|---|
 | 174 |         // Free the old exception node.
 | 
|---|
| [cbce272] | 175 |         except->virtual_table->free( except );
 | 
|---|
| [ff7ff14a] | 176 |         free( to_free );
 | 
|---|
| [86d5ba7c] | 177 | }
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 | // If this isn't a rethrow (*except==0), delete the provided exception.
 | 
|---|
| [36982fc] | 180 | void __cfaabi_ehm__cleanup_terminate( void * except ) {
 | 
|---|
| [0304215a] | 181 |         if ( *(void**)except ) __cfaabi_ehm__delete_exception( *(exception_t **)except );
 | 
|---|
| [86d5ba7c] | 182 | }
 | 
|---|
| [fa4805f] | 183 | 
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 | // We need a piece of storage to raise the exception
 | 
|---|
 | 186 | struct _Unwind_Exception this_exception_storage;
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 | // Function needed by force unwind
 | 
|---|
 | 189 | // It basically says to unwind the whole stack and then exit when we reach the end of the stack
 | 
|---|
 | 190 | static _Unwind_Reason_Code _Stop_Fn(
 | 
|---|
 | 191 |                 int version,
 | 
|---|
 | 192 |                 _Unwind_Action actions,
 | 
|---|
 | 193 |                 _Unwind_Exception_Class exceptionClass,
 | 
|---|
 | 194 |                 struct _Unwind_Exception * unwind_exception,
 | 
|---|
 | 195 |                 struct _Unwind_Context * context,
 | 
|---|
 | 196 |                 void * some_param) {
 | 
|---|
 | 197 |         if( actions & _UA_END_OF_STACK  ) exit(1);
 | 
|---|
 | 198 |         if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 |         return _URC_FATAL_PHASE2_ERROR;
 | 
|---|
 | 201 | }
 | 
|---|
 | 202 | 
 | 
|---|
| [86d5ba7c] | 203 | // The exception that is being thrown must already be stored.
 | 
|---|
| [36982fc] | 204 | __attribute__((noreturn)) void __cfaabi_ehm__begin_unwind(void) {
 | 
|---|
| [86d5ba7c] | 205 |         if ( ! this_exception_context()->current_exception ) {
 | 
|---|
 | 206 |                 printf("UNWIND ERROR missing exception in begin unwind\n");
 | 
|---|
 | 207 |                 abort();
 | 
|---|
 | 208 |         }
 | 
|---|
| [fa4805f] | 209 | 
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 |         // Call stdlibc to raise the exception
 | 
|---|
 | 212 |         _Unwind_Reason_Code ret = _Unwind_RaiseException( &this_exception_storage );
 | 
|---|
 | 213 | 
 | 
|---|
| [b947fb2] | 214 |         // If we reach here it means something happened.  For resumption to work we need to find a way to return back to
 | 
|---|
 | 215 |         // here.  Most of them will probably boil down to setting a global flag and making the phase 1 either stop or
 | 
|---|
 | 216 |         // fail.  Causing an error on purpose may help avoiding unnecessary work but it might have some weird side
 | 
|---|
 | 217 |         // effects.  If we just pretend no handler was found that would work but may be expensive for no reason since we
 | 
|---|
 | 218 |         // will always search the whole stack.
 | 
|---|
| [fa4805f] | 219 | 
 | 
|---|
 | 220 |         if( ret == _URC_END_OF_STACK ) {
 | 
|---|
| [b947fb2] | 221 |                 // No proper handler was found.  This can be handled in several way.  C++ calls std::terminate Here we
 | 
|---|
 | 222 |                 // force unwind the stack, basically raising a cancellation.
 | 
|---|
| [fa4805f] | 223 |                 printf("Uncaught exception %p\n", &this_exception_storage);
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 |                 ret = _Unwind_ForcedUnwind( &this_exception_storage, _Stop_Fn, (void*)0x22 );
 | 
|---|
 | 226 |                 printf("UNWIND ERROR %d after force unwind\n", ret);
 | 
|---|
 | 227 |                 abort();
 | 
|---|
 | 228 |         }
 | 
|---|
 | 229 | 
 | 
|---|
| [b947fb2] | 230 |         // We did not simply reach the end of the stack without finding a handler.  Something wen't wrong
 | 
|---|
| [fa4805f] | 231 |         printf("UNWIND ERROR %d after raise exception\n", ret);
 | 
|---|
 | 232 |         abort();
 | 
|---|
 | 233 | }
 | 
|---|
 | 234 | 
 | 
|---|
| [0304215a] | 235 | void __cfaabi_ehm__throw_terminate( exception_t * val ) {
 | 
|---|
| [36982fc] | 236 |         __cfaabi_dbg_print_safe("Throwing termination exception\n");
 | 
|---|
| [86d5ba7c] | 237 | 
 | 
|---|
| [36982fc] | 238 |         __cfaabi_ehm__allocate_exception( val );
 | 
|---|
 | 239 |         __cfaabi_ehm__begin_unwind();
 | 
|---|
| [86d5ba7c] | 240 | }
 | 
|---|
 | 241 | 
 | 
|---|
| [36982fc] | 242 | void __cfaabi_ehm__rethrow_terminate(void) {
 | 
|---|
 | 243 |         __cfaabi_dbg_print_safe("Rethrowing termination exception\n");
 | 
|---|
| [fa4805f] | 244 | 
 | 
|---|
| [36982fc] | 245 |         __cfaabi_ehm__begin_unwind();
 | 
|---|
| [fa4805f] | 246 | }
 | 
|---|
 | 247 | 
 | 
|---|
| [d43ed2c] | 248 | #if defined(PIC)
 | 
|---|
 | 249 | #warning Exceptions not yet supported when using Position-Independent Code
 | 
|---|
 | 250 | __attribute__((noinline))
 | 
|---|
 | 251 | void __cfaabi_ehm__try_terminate(void (*try_block)(),
 | 
|---|
 | 252 |                 void (*catch_block)(int index, exception_t * except),
 | 
|---|
 | 253 |                 __attribute__((unused)) int (*match_block)(exception_t * except)) {
 | 
|---|
 | 254 |         abort();
 | 
|---|
 | 255 | }
 | 
|---|
 | 256 | #else
 | 
|---|
| [b947fb2] | 257 | // This is our personality routine.  For every stack frame anotated with ".cfi_personality 0x3,__gcfa_personality_v0".
 | 
|---|
 | 258 | // This function will be called twice when unwinding.  Once in the search phased and once in the cleanup phase.
 | 
|---|
| [fa4805f] | 259 | _Unwind_Reason_Code __gcfa_personality_v0 (
 | 
|---|
 | 260 |                 int version, _Unwind_Action actions, unsigned long long exceptionClass,
 | 
|---|
 | 261 |                 struct _Unwind_Exception* unwind_exception,
 | 
|---|
 | 262 |                 struct _Unwind_Context* context)
 | 
|---|
 | 263 | {
 | 
|---|
 | 264 | 
 | 
|---|
| [36982fc] | 265 |         //__cfaabi_dbg_print_safe("CFA: 0x%lx\n", _Unwind_GetCFA(context));
 | 
|---|
 | 266 |         __cfaabi_dbg_print_safe("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
 | 
|---|
| [fa4805f] | 267 | 
 | 
|---|
 | 268 |         // If we've reached the end of the stack then there is nothing much we can do...
 | 
|---|
 | 269 |         if( actions & _UA_END_OF_STACK ) return _URC_END_OF_STACK;
 | 
|---|
 | 270 | 
 | 
|---|
 | 271 |         if (actions & _UA_SEARCH_PHASE) {
 | 
|---|
| [36982fc] | 272 |                 __cfaabi_dbg_print_safe(" lookup phase");
 | 
|---|
| [fa4805f] | 273 |         }
 | 
|---|
 | 274 |         else if (actions & _UA_CLEANUP_PHASE) {
 | 
|---|
| [36982fc] | 275 |                 __cfaabi_dbg_print_safe(" cleanup phase");
 | 
|---|
| [fa4805f] | 276 |         }
 | 
|---|
 | 277 |         // Just in case, probably can't actually happen
 | 
|---|
 | 278 |         else {
 | 
|---|
 | 279 |                 printf(" error\n");
 | 
|---|
 | 280 |                 return _URC_FATAL_PHASE1_ERROR;
 | 
|---|
 | 281 |         }
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 |         // Get a pointer to the language specific data from which we will read what we need
 | 
|---|
 | 284 |         const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( context );
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 |         if( !lsd ) {    //Nothing to do, keep unwinding
 | 
|---|
 | 287 |                 printf(" no LSD");
 | 
|---|
 | 288 |                 goto UNWIND;
 | 
|---|
 | 289 |         }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 |         // Get the instuction pointer and a reading pointer into the exception table
 | 
|---|
 | 292 |         lsda_header_info lsd_info;
 | 
|---|
 | 293 |         const unsigned char * cur_ptr = parse_lsda_header( context, lsd, &lsd_info);
 | 
|---|
 | 294 |         _Unwind_Ptr instruction_ptr = _Unwind_GetIP( context );
 | 
|---|
 | 295 | 
 | 
|---|
 | 296 |         // Linearly search the table for stuff to do
 | 
|---|
 | 297 |         while( cur_ptr < lsd_info.action_table ) {
 | 
|---|
 | 298 |                 _Unwind_Ptr callsite_start;
 | 
|---|
 | 299 |                 _Unwind_Ptr callsite_len;
 | 
|---|
 | 300 |                 _Unwind_Ptr callsite_landing_pad;
 | 
|---|
 | 301 |                 _uleb128_t  callsite_action;
 | 
|---|
 | 302 | 
 | 
|---|
 | 303 |                 // Decode the common stuff we have in here
 | 
|---|
 | 304 |                 cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_start);
 | 
|---|
 | 305 |                 cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_len);
 | 
|---|
 | 306 |                 cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad);
 | 
|---|
 | 307 |                 cur_ptr = read_uleb128 (cur_ptr, &callsite_action);
 | 
|---|
 | 308 | 
 | 
|---|
 | 309 |                 // Have we reach the correct frame info yet?
 | 
|---|
 | 310 |                 if( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) {
 | 
|---|
| [e9145a3] | 311 | #ifdef __CFA_DEBUG_PRINT__
 | 
|---|
| [fa4805f] | 312 |                         void * ls = (void*)lsd_info.Start;
 | 
|---|
 | 313 |                         void * cs = (void*)callsite_start;
 | 
|---|
 | 314 |                         void * cl = (void*)callsite_len;
 | 
|---|
 | 315 |                         void * bp = (void*)lsd_info.Start + callsite_start;
 | 
|---|
 | 316 |                         void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
 | 
|---|
 | 317 |                         void * ip = (void*)instruction_ptr;
 | 
|---|
| [36982fc] | 318 |                         __cfaabi_dbg_print_safe("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
 | 
|---|
| [e9145a3] | 319 | #endif // __CFA_DEBUG_PRINT__
 | 
|---|
| [fa4805f] | 320 |                         continue;
 | 
|---|
 | 321 |                 }
 | 
|---|
 | 322 | 
 | 
|---|
 | 323 |                 // Have we gone too far
 | 
|---|
 | 324 |                 if( lsd_info.Start + callsite_start > instruction_ptr ) {
 | 
|---|
 | 325 |                         printf(" gone too far");
 | 
|---|
 | 326 |                         break;
 | 
|---|
 | 327 |                 }
 | 
|---|
 | 328 | 
 | 
|---|
 | 329 |                 // Something to do?
 | 
|---|
 | 330 |                 if( callsite_landing_pad ) {
 | 
|---|
 | 331 |                         // Which phase are we in
 | 
|---|
 | 332 |                         if (actions & _UA_SEARCH_PHASE) {
 | 
|---|
 | 333 |                                 // Search phase, this means we probably found a potential handler and must check if it is a match
 | 
|---|
 | 334 | 
 | 
|---|
 | 335 |                                 // If we have arbitrarily decided that 0 means nothing to do and 1 means there is a potential handler
 | 
|---|
 | 336 |                                 // This doesn't seem to conflict the gcc default behavior
 | 
|---|
 | 337 |                                 if (callsite_action != 0) {
 | 
|---|
 | 338 |                                         // Now we want to run some code to see if the handler matches
 | 
|---|
 | 339 |                                         // This is the tricky part where we want to the power to run arbitrary code
 | 
|---|
 | 340 |                                         // However, generating a new exception table entry and try routine every time
 | 
|---|
 | 341 |                                         // is way more expansive than we might like
 | 
|---|
 | 342 |                                         // The information we have is :
 | 
|---|
 | 343 |                                         //  - The GR (Series of registers)
 | 
|---|
 | 344 |                                         //    GR1=GP Global Pointer of frame ref by context
 | 
|---|
 | 345 |                                         //  - The instruction pointer
 | 
|---|
 | 346 |                                         //  - The instruction pointer info (???)
 | 
|---|
 | 347 |                                         //  - The CFA (Canonical Frame Address)
 | 
|---|
 | 348 |                                         //  - The BSP (Probably the base stack pointer)
 | 
|---|
 | 349 | 
 | 
|---|
 | 350 | 
 | 
|---|
 | 351 |                                         // The current apprach uses one exception table entry per try block
 | 
|---|
 | 352 |                                         _uleb128_t imatcher;
 | 
|---|
 | 353 |                                         // Get the relative offset to the
 | 
|---|
 | 354 |                                         cur_ptr = read_uleb128 (cur_ptr, &imatcher);
 | 
|---|
 | 355 | 
 | 
|---|
 | 356 |                                         // Get a function pointer from the relative offset and call it
 | 
|---|
| [36982fc] | 357 |                                         // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;
 | 
|---|
| [fa4805f] | 358 | 
 | 
|---|
| [0304215a] | 359 |                                         _Unwind_Reason_Code (*matcher)(exception_t *) =
 | 
|---|
| [fa4805f] | 360 |                                                 MATCHER_FROM_CONTEXT(context);
 | 
|---|
| [86d5ba7c] | 361 |                                         int index = matcher(shared_stack.current_exception);
 | 
|---|
| [fa4805f] | 362 |                                         _Unwind_Reason_Code ret = (0 == index)
 | 
|---|
 | 363 |                                                 ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;
 | 
|---|
 | 364 |                                         shared_stack.current_handler_index = index;
 | 
|---|
 | 365 | 
 | 
|---|
 | 366 |                                         // Based on the return value, check if we matched the exception
 | 
|---|
| [e9145a3] | 367 |                                         if( ret == _URC_HANDLER_FOUND) {
 | 
|---|
| [36982fc] | 368 |                                                 __cfaabi_dbg_print_safe(" handler found\n");
 | 
|---|
| [e9145a3] | 369 |                                         } else {
 | 
|---|
| [36982fc] | 370 |                                                 __cfaabi_dbg_print_safe(" no handler\n");
 | 
|---|
| [e9145a3] | 371 |                                         }
 | 
|---|
| [fa4805f] | 372 |                                         return ret;
 | 
|---|
 | 373 |                                 }
 | 
|---|
 | 374 | 
 | 
|---|
 | 375 |                                 // This is only a cleanup handler, ignore it
 | 
|---|
| [36982fc] | 376 |                                 __cfaabi_dbg_print_safe(" no action");
 | 
|---|
| [fa4805f] | 377 |                         }
 | 
|---|
 | 378 |                         else if (actions & _UA_CLEANUP_PHASE) {
 | 
|---|
 | 379 | 
 | 
|---|
 | 380 |                                 if( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){
 | 
|---|
 | 381 |                                         // If this is a potential exception handler
 | 
|---|
 | 382 |                                         // but not the one that matched the exception in the seach phase,
 | 
|---|
 | 383 |                                         // just ignore it
 | 
|---|
 | 384 |                                         goto UNWIND;
 | 
|---|
 | 385 |                                 }
 | 
|---|
 | 386 | 
 | 
|---|
 | 387 |                                 // We need to run some clean-up or a handler
 | 
|---|
 | 388 |                                 // These statment do the right thing but I don't know any specifics at all
 | 
|---|
 | 389 |                                 _Unwind_SetGR( context, __builtin_eh_return_data_regno(0), (_Unwind_Ptr) unwind_exception );
 | 
|---|
 | 390 |                                 _Unwind_SetGR( context, __builtin_eh_return_data_regno(1), 0 );
 | 
|---|
 | 391 | 
 | 
|---|
 | 392 |                                 // I assume this sets the instruction pointer to the adress of the landing pad
 | 
|---|
 | 393 |                                 // It doesn't actually set it, it only state the value that needs to be set once we return _URC_INSTALL_CONTEXT
 | 
|---|
| [b947fb2] | 394 |                                 _Unwind_SetIP( context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
 | 
|---|
| [fa4805f] | 395 | 
 | 
|---|
| [36982fc] | 396 |                                 __cfaabi_dbg_print_safe(" action\n");
 | 
|---|
| [fa4805f] | 397 | 
 | 
|---|
 | 398 |                                 // Return have some action to run
 | 
|---|
 | 399 |                                 return _URC_INSTALL_CONTEXT;
 | 
|---|
 | 400 |                         }
 | 
|---|
 | 401 |                 }
 | 
|---|
 | 402 | 
 | 
|---|
 | 403 |                 // Nothing to do, move along
 | 
|---|
| [36982fc] | 404 |                 __cfaabi_dbg_print_safe(" no landing pad");
 | 
|---|
| [fa4805f] | 405 |         }
 | 
|---|
 | 406 |         // No handling found
 | 
|---|
| [36982fc] | 407 |         __cfaabi_dbg_print_safe(" table end reached\n");
 | 
|---|
| [fa4805f] | 408 | 
 | 
|---|
 | 409 |         UNWIND:
 | 
|---|
| [36982fc] | 410 |         __cfaabi_dbg_print_safe(" unwind\n");
 | 
|---|
| [fa4805f] | 411 | 
 | 
|---|
 | 412 |         // Keep unwinding the stack
 | 
|---|
 | 413 |         return _URC_CONTINUE_UNWIND;
 | 
|---|
 | 414 | }
 | 
|---|
 | 415 | 
 | 
|---|
| [b947fb2] | 416 | // Try statements are hoisted out see comments for details.  With this could probably be unique and simply linked from
 | 
|---|
 | 417 | // libcfa but there is one problem left, see the exception table for details
 | 
|---|
| [fa4805f] | 418 | __attribute__((noinline))
 | 
|---|
| [36982fc] | 419 | void __cfaabi_ehm__try_terminate(void (*try_block)(),
 | 
|---|
| [0304215a] | 420 |                 void (*catch_block)(int index, exception_t * except),
 | 
|---|
 | 421 |                 __attribute__((unused)) int (*match_block)(exception_t * except)) {
 | 
|---|
| [fa4805f] | 422 |         //! volatile int xy = 0;
 | 
|---|
 | 423 |         //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy);
 | 
|---|
 | 424 | 
 | 
|---|
| [b947fb2] | 425 |         // Setup statments: These 2 statments won't actually result in any code, they only setup global tables.
 | 
|---|
 | 426 |         // However, they clobber gcc cancellation support from gcc.  We can replace the personality routine but
 | 
|---|
 | 427 |         // replacing the exception table gcc generates is not really doable, it generates labels based on how the
 | 
|---|
 | 428 |         // assembly works.
 | 
|---|
 | 429 | 
 | 
|---|
| [fa4805f] | 430 |         // Setup the personality routine
 | 
|---|
 | 431 |         asm volatile (".cfi_personality 0x3,__gcfa_personality_v0");
 | 
|---|
 | 432 |         // Setup the exception table
 | 
|---|
 | 433 |         asm volatile (".cfi_lsda 0x3, .LLSDACFA2");
 | 
|---|
 | 434 | 
 | 
|---|
| [b947fb2] | 435 |         // Label which defines the start of the area for which the handler is setup.
 | 
|---|
| [fa4805f] | 436 |         asm volatile (".TRYSTART:");
 | 
|---|
 | 437 | 
 | 
|---|
 | 438 |         // The actual statements of the try blocks
 | 
|---|
 | 439 |         try_block();
 | 
|---|
 | 440 | 
 | 
|---|
 | 441 |         // asm statement to prevent deadcode removal
 | 
|---|
 | 442 |         asm volatile goto ("" : : : : CATCH );
 | 
|---|
 | 443 | 
 | 
|---|
 | 444 |         // Normal return
 | 
|---|
 | 445 |         return;
 | 
|---|
 | 446 | 
 | 
|---|
 | 447 |         // Exceptionnal path
 | 
|---|
 | 448 |         CATCH : __attribute__(( unused ));
 | 
|---|
| [b947fb2] | 449 |         // Label which defines the end of the area for which the handler is setup.
 | 
|---|
| [fa4805f] | 450 |         asm volatile (".TRYEND:");
 | 
|---|
| [b947fb2] | 451 |         // Label which defines the start of the exception landing pad.  Basically what is called when the exception is
 | 
|---|
 | 452 |         // caught.  Note, if multiple handlers are given, the multiplexing should be done by the generated code, not the
 | 
|---|
 | 453 |         // exception runtime.
 | 
|---|
| [fa4805f] | 454 |         asm volatile (".CATCH:");
 | 
|---|
 | 455 | 
 | 
|---|
 | 456 |         // Exception handler
 | 
|---|
| [b947fb2] | 457 |         catch_block( shared_stack.current_handler_index,
 | 
|---|
| [86d5ba7c] | 458 |                      shared_stack.current_exception );
 | 
|---|
| [fa4805f] | 459 | }
 | 
|---|
 | 460 | 
 | 
|---|
| [b947fb2] | 461 | // Exception table data we need to generate.  While this is almost generic, the custom data refers to foo_try_match try
 | 
|---|
 | 462 | // match, which is no way generic.  Some more works need to be done if we want to have a single call to the try routine.
 | 
|---|
 | 463 | 
 | 
|---|
| [381fdee] | 464 | #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
| [fa4805f] | 465 | asm (
 | 
|---|
 | 466 |         //HEADER
 | 
|---|
 | 467 |         ".LFECFA1:\n"
 | 
|---|
 | 468 |         "       .globl  __gcfa_personality_v0\n"
 | 
|---|
 | 469 |         "       .section        .gcc_except_table,\"a\",@progbits\n"
 | 
|---|
 | 470 |         ".LLSDACFA2:\n"                                                 //TABLE header
 | 
|---|
 | 471 |         "       .byte   0xff\n"
 | 
|---|
 | 472 |         "       .byte   0xff\n"
 | 
|---|
 | 473 |         "       .byte   0x1\n"
 | 
|---|
 | 474 |         "       .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n"         // BODY length
 | 
|---|
 | 475 |         // Body uses language specific data and therefore could be modified arbitrarily
 | 
|---|
 | 476 |         ".LLSDACSBCFA2:\n"                                              // BODY start
 | 
|---|
| [36982fc] | 477 |         "       .uleb128 .TRYSTART-__cfaabi_ehm__try_terminate\n"               // Handled area start  (relative to start of function)
 | 
|---|
| [fa4805f] | 478 |         "       .uleb128 .TRYEND-.TRYSTART\n"                           // Handled area length
 | 
|---|
| [36982fc] | 479 |         "       .uleb128 .CATCH-__cfaabi_ehm__try_terminate\n"                          // Hanlder landing pad adress  (relative to start of function)
 | 
|---|
| [fa4805f] | 480 |         "       .uleb128 1\n"                                           // Action code, gcc seems to use always 0
 | 
|---|
 | 481 |         ".LLSDACSECFA2:\n"                                              // BODY end
 | 
|---|
 | 482 |         "       .text\n"                                                        // TABLE footer
 | 
|---|
| [36982fc] | 483 |         "       .size   __cfaabi_ehm__try_terminate, .-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| [fa4805f] | 484 |         "       .ident  \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
 | 
|---|
 | 485 | //      "       .section        .note.GNU-stack,\"x\",@progbits\n"
 | 
|---|
 | 486 | );
 | 
|---|
| [381fdee] | 487 | #endif // __i386 || __x86_64
 | 
|---|
| [d43ed2c] | 488 | #endif //PIC
 | 
|---|