| 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 : Thu Feb 22 18:17:34 2018
 | 
|---|
| 13 | // Update Count     : 11
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <stddef.h> // for size_t
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "exception.h"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | // Implementation of the secret header.
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <stdlib.h>
 | 
|---|
| 23 | #include <stdio.h>
 | 
|---|
| 24 | #include <unwind.h>
 | 
|---|
| 25 | #include <bits/debug.hfa>
 | 
|---|
| 26 | 
 | 
|---|
| 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 | 
 | 
|---|
| 35 | #include "lsda.h"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // Base exception vtable is abstract, you should not have base exceptions.
 | 
|---|
| 39 | struct __cfaabi_ehm__base_exception_t_vtable
 | 
|---|
| 40 |                 ___cfaabi_ehm__base_exception_t_vtable_instance = {
 | 
|---|
| 41 |         .parent = NULL,
 | 
|---|
| 42 |         .size = 0,
 | 
|---|
| 43 |         .copy = NULL,
 | 
|---|
| 44 |         .free = NULL,
 | 
|---|
| 45 |         .msg = NULL
 | 
|---|
| 46 | };
 | 
|---|
| 47 | 
 | 
|---|
| 48 | 
 | 
|---|
| 49 | // Temperary global exception context. Does not work with concurency.
 | 
|---|
| 50 | struct exception_context_t {
 | 
|---|
| 51 |     struct __cfaabi_ehm__try_resume_node * top_resume;
 | 
|---|
| 52 |     struct __cfaabi_ehm__try_resume_node * current_resume;
 | 
|---|
| 53 | 
 | 
|---|
| 54 |     exception_t * current_exception;
 | 
|---|
| 55 |     int current_handler_index;
 | 
|---|
| 56 | } shared_stack = {NULL, NULL, 0, 0};
 | 
|---|
| 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 | //}
 | 
|---|
| 69 | 
 | 
|---|
| 70 | 
 | 
|---|
| 71 | // This macro should be the only thing that needs to change across machines.
 | 
|---|
| 72 | // Used in the personality function, way down in termination.
 | 
|---|
| 73 | // struct _Unwind_Context * -> _Unwind_Reason_Code(*)(exception_t *)
 | 
|---|
| 74 | #if defined( __x86_64 )
 | 
|---|
| 75 | #define MATCHER_FROM_CONTEXT(ptr_to_context) \
 | 
|---|
| 76 |         (*(_Unwind_Reason_Code(**)(exception_t *))(_Unwind_GetCFA(ptr_to_context) + 8))
 | 
|---|
| 77 | #elif defined( __i386 )
 | 
|---|
| 78 | #define MATCHER_FROM_CONTEXT(ptr_to_context) \
 | 
|---|
| 79 |         (*(_Unwind_Reason_Code(**)(exception_t *))(_Unwind_GetCFA(ptr_to_context) + 24))
 | 
|---|
| 80 | #endif
 | 
|---|
| 81 | 
 | 
|---|
| 82 | // RESUMPTION ================================================================
 | 
|---|
| 83 | 
 | 
|---|
| 84 | void __cfaabi_ehm__throw_resume(exception_t * except) {
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         __cfaabi_dbg_print_safe("Throwing resumption exception\n");
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         struct __cfaabi_ehm__try_resume_node * original_head = shared_stack.current_resume;
 | 
|---|
| 89 |         struct __cfaabi_ehm__try_resume_node * current =
 | 
|---|
| 90 |                 (original_head) ? original_head->next : shared_stack.top_resume;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         for ( ; current ; current = current->next) {
 | 
|---|
| 93 |                 shared_stack.current_resume = current;
 | 
|---|
| 94 |                 if (current->handler(except)) {
 | 
|---|
| 95 |                         shared_stack.current_resume = original_head;
 | 
|---|
| 96 |                         return;
 | 
|---|
| 97 |                 }
 | 
|---|
| 98 |         }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         __cfaabi_dbg_print_safe("Unhandled exception\n");
 | 
|---|
| 101 |         shared_stack.current_resume = original_head;
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         // Fall back to termination:
 | 
|---|
| 104 |         __cfaabi_ehm__throw_terminate(except);
 | 
|---|
| 105 |         // TODO: Default handler for resumption.
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | // Do we control where exceptions get thrown even with concurency?
 | 
|---|
| 109 | // If not these are not quite thread safe, the cleanup hook has to
 | 
|---|
| 110 | // be added after the node is built but before it is made the top node.
 | 
|---|
| 111 | 
 | 
|---|
| 112 | void __cfaabi_ehm__try_resume_setup(struct __cfaabi_ehm__try_resume_node * node,
 | 
|---|
| 113 |                         _Bool (*handler)(exception_t * except)) {
 | 
|---|
| 114 |         node->next = shared_stack.top_resume;
 | 
|---|
| 115 |         node->handler = handler;
 | 
|---|
| 116 |         shared_stack.top_resume = node;
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | void __cfaabi_ehm__try_resume_cleanup(struct __cfaabi_ehm__try_resume_node * node) {
 | 
|---|
| 120 |         shared_stack.top_resume = node->next;
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | 
 | 
|---|
| 124 | // TERMINATION ===============================================================
 | 
|---|
| 125 | 
 | 
|---|
| 126 | // MEMORY MANAGEMENT (still for integers)
 | 
|---|
| 127 | // May have to move to cfa for constructors and destructors (references).
 | 
|---|
| 128 | 
 | 
|---|
| 129 | struct __cfaabi_ehm__node {
 | 
|---|
| 130 |         struct __cfaabi_ehm__node * next;
 | 
|---|
| 131 | };
 | 
|---|
| 132 | 
 | 
|---|
| 133 | #define NODE_TO_EXCEPT(node) ((exception_t *)(1 + (node)))
 | 
|---|
| 134 | #define EXCEPT_TO_NODE(except) ((struct __cfaabi_ehm__node *)(except) - 1)
 | 
|---|
| 135 | 
 | 
|---|
| 136 | // Creates a copy of the indicated exception and sets current_exception to it.
 | 
|---|
| 137 | static void __cfaabi_ehm__allocate_exception( exception_t * except ) {
 | 
|---|
| 138 |         struct exception_context_t * context = this_exception_context();
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         // Allocate memory for the exception.
 | 
|---|
| 141 |         struct __cfaabi_ehm__node * store = malloc(
 | 
|---|
| 142 |                 sizeof( struct __cfaabi_ehm__node ) + except->virtual_table->size );
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         if ( ! store ) {
 | 
|---|
| 145 |                 // Failure: cannot allocate exception. Terminate thread.
 | 
|---|
| 146 |                 abort(); // <- Although I think it might be the process.
 | 
|---|
| 147 |         }
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         // Add the node to the list:
 | 
|---|
| 150 |         store->next = EXCEPT_TO_NODE(context->current_exception);
 | 
|---|
| 151 |         context->current_exception = NODE_TO_EXCEPT(store);
 | 
|---|
| 152 | 
 | 
|---|
| 153 |         // Copy the exception to storage.
 | 
|---|
| 154 |         except->virtual_table->copy( context->current_exception, except );
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | // Delete the provided exception, unsetting current_exception if relivant.
 | 
|---|
| 158 | static void __cfaabi_ehm__delete_exception( exception_t * except ) {
 | 
|---|
| 159 |         struct exception_context_t * context = this_exception_context();
 | 
|---|
| 160 | 
 | 
|---|
| 161 |         __cfaabi_dbg_print_safe("Deleting Exception\n");
 | 
|---|
| 162 | 
 | 
|---|
| 163 |         // Remove the exception from the list.
 | 
|---|
| 164 |         struct __cfaabi_ehm__node * to_free = EXCEPT_TO_NODE(except);
 | 
|---|
| 165 |         struct __cfaabi_ehm__node * node;
 | 
|---|
| 166 | 
 | 
|---|
| 167 |         if ( context->current_exception == except ) {
 | 
|---|
| 168 |                 node = to_free->next;
 | 
|---|
| 169 |                 context->current_exception = (node) ? NODE_TO_EXCEPT(node) : 0;
 | 
|---|
| 170 |         } else {
 | 
|---|
| 171 |                 node = EXCEPT_TO_NODE(context->current_exception);
 | 
|---|
| 172 |                 // It may always be in the first or second position.
 | 
|---|
| 173 |                 while( to_free != node->next ) {
 | 
|---|
| 174 |                         node = node->next;
 | 
|---|
| 175 |                 }
 | 
|---|
| 176 |                 node->next = to_free->next;
 | 
|---|
| 177 |         }
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         // Free the old exception node.
 | 
|---|
| 180 |         except->virtual_table->free( except );
 | 
|---|
| 181 |         free( to_free );
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | // If this isn't a rethrow (*except==0), delete the provided exception.
 | 
|---|
| 185 | void __cfaabi_ehm__cleanup_terminate( void * except ) {
 | 
|---|
| 186 |         if ( *(void**)except ) __cfaabi_ehm__delete_exception( *(exception_t **)except );
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | 
 | 
|---|
| 190 | // We need a piece of storage to raise the exception
 | 
|---|
| 191 | struct _Unwind_Exception this_exception_storage;
 | 
|---|
| 192 | 
 | 
|---|
| 193 | // Function needed by force unwind
 | 
|---|
| 194 | // It basically says to unwind the whole stack and then exit when we reach the end of the stack
 | 
|---|
| 195 | static _Unwind_Reason_Code _Stop_Fn(
 | 
|---|
| 196 |                 int version,
 | 
|---|
| 197 |                 _Unwind_Action actions,
 | 
|---|
| 198 |                 _Unwind_Exception_Class exceptionClass,
 | 
|---|
| 199 |                 struct _Unwind_Exception * unwind_exception,
 | 
|---|
| 200 |                 struct _Unwind_Context * context,
 | 
|---|
| 201 |                 void * some_param) {
 | 
|---|
| 202 |         if( actions & _UA_END_OF_STACK  ) exit(1);
 | 
|---|
| 203 |         if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
 | 
|---|
| 204 | 
 | 
|---|
| 205 |         return _URC_FATAL_PHASE2_ERROR;
 | 
|---|
| 206 | }
 | 
|---|
| 207 | 
 | 
|---|
| 208 | // The exception that is being thrown must already be stored.
 | 
|---|
| 209 | __attribute__((noreturn)) void __cfaabi_ehm__begin_unwind(void) {
 | 
|---|
| 210 |         if ( ! this_exception_context()->current_exception ) {
 | 
|---|
| 211 |                 printf("UNWIND ERROR missing exception in begin unwind\n");
 | 
|---|
| 212 |                 abort();
 | 
|---|
| 213 |         }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | 
 | 
|---|
| 216 |         // Call stdlibc to raise the exception
 | 
|---|
| 217 |         _Unwind_Reason_Code ret = _Unwind_RaiseException( &this_exception_storage );
 | 
|---|
| 218 | 
 | 
|---|
| 219 |         // If we reach here it means something happened. For resumption to work we need to find a way
 | 
|---|
| 220 |         // to return back to here. Most of them will probably boil down to setting a global flag and
 | 
|---|
| 221 |         // making the phase 1 either stop or fail. Causing an error on purpose may help avoiding
 | 
|---|
| 222 |         // unnecessary work but it might have some weird side effects. If we just pretend no handler
 | 
|---|
| 223 |         // was found that would work but may be expensive for no reason since we will always search
 | 
|---|
| 224 |         // the whole stack.
 | 
|---|
| 225 | 
 | 
|---|
| 226 |         if( ret == _URC_END_OF_STACK ) {
 | 
|---|
| 227 |                 // No proper handler was found. This can be handled in many ways, C++ calls std::terminate.
 | 
|---|
| 228 |                 // Here we force unwind the stack, basically raising a cancellation.
 | 
|---|
| 229 |                 printf("Uncaught exception %p\n", &this_exception_storage);
 | 
|---|
| 230 | 
 | 
|---|
| 231 |                 ret = _Unwind_ForcedUnwind( &this_exception_storage, _Stop_Fn, (void*)0x22 );
 | 
|---|
| 232 |                 printf("UNWIND ERROR %d after force unwind\n", ret);
 | 
|---|
| 233 |                 abort();
 | 
|---|
| 234 |         }
 | 
|---|
| 235 | 
 | 
|---|
| 236 |         // We did not simply reach the end of the stack without finding a handler. This is an error.
 | 
|---|
| 237 |         printf("UNWIND ERROR %d after raise exception\n", ret);
 | 
|---|
| 238 |         abort();
 | 
|---|
| 239 | }
 | 
|---|
| 240 | 
 | 
|---|
| 241 | void __cfaabi_ehm__throw_terminate( exception_t * val ) {
 | 
|---|
| 242 |         __cfaabi_dbg_print_safe("Throwing termination exception\n");
 | 
|---|
| 243 | 
 | 
|---|
| 244 |         __cfaabi_ehm__allocate_exception( val );
 | 
|---|
| 245 |         __cfaabi_ehm__begin_unwind();
 | 
|---|
| 246 | }
 | 
|---|
| 247 | 
 | 
|---|
| 248 | void __cfaabi_ehm__rethrow_terminate(void) {
 | 
|---|
| 249 |         __cfaabi_dbg_print_safe("Rethrowing termination exception\n");
 | 
|---|
| 250 | 
 | 
|---|
| 251 |         __cfaabi_ehm__begin_unwind();
 | 
|---|
| 252 | }
 | 
|---|
| 253 | 
 | 
|---|
| 254 | #pragma GCC push_options
 | 
|---|
| 255 | #pragma GCC optimize("O0")
 | 
|---|
| 256 | 
 | 
|---|
| 257 | // This is our personality routine. For every stack frame annotated with
 | 
|---|
| 258 | // ".cfi_personality 0x3,__gcfa_personality_v0" this function will be called twice when unwinding.
 | 
|---|
| 259 | //  Once in the search phase and once in the cleanup phase.
 | 
|---|
| 260 | _Unwind_Reason_Code __gcfa_personality_v0 (
 | 
|---|
| 261 |                 int version, _Unwind_Action actions, unsigned long long exceptionClass,
 | 
|---|
| 262 |                 struct _Unwind_Exception* unwind_exception,
 | 
|---|
| 263 |                 struct _Unwind_Context* context)
 | 
|---|
| 264 | {
 | 
|---|
| 265 | 
 | 
|---|
| 266 |         //__cfaabi_dbg_print_safe("CFA: 0x%lx\n", _Unwind_GetCFA(context));
 | 
|---|
| 267 |         __cfaabi_dbg_print_safe("Personality function (%d, %x, %llu, %p, %p):",
 | 
|---|
| 268 |                         version, actions, exceptionClass, unwind_exception, context);
 | 
|---|
| 269 | 
 | 
|---|
| 270 |         // If we've reached the end of the stack then there is nothing much we can do...
 | 
|---|
| 271 |         if( actions & _UA_END_OF_STACK ) return _URC_END_OF_STACK;
 | 
|---|
| 272 | 
 | 
|---|
| 273 |         if (actions & _UA_SEARCH_PHASE) {
 | 
|---|
| 274 |                 __cfaabi_dbg_print_safe(" lookup phase");
 | 
|---|
| 275 |         }
 | 
|---|
| 276 |         else if (actions & _UA_CLEANUP_PHASE) {
 | 
|---|
| 277 |                 __cfaabi_dbg_print_safe(" cleanup phase");
 | 
|---|
| 278 |         }
 | 
|---|
| 279 |         // Just in case, probably can't actually happen
 | 
|---|
| 280 |         else {
 | 
|---|
| 281 |                 printf(" error\n");
 | 
|---|
| 282 |                 return _URC_FATAL_PHASE1_ERROR;
 | 
|---|
| 283 |         }
 | 
|---|
| 284 | 
 | 
|---|
| 285 |         // Get a pointer to the language specific data from which we will read what we need
 | 
|---|
| 286 |         const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( context );
 | 
|---|
| 287 | 
 | 
|---|
| 288 |         if( !lsd ) {    //Nothing to do, keep unwinding
 | 
|---|
| 289 |                 printf(" no LSD");
 | 
|---|
| 290 |                 goto UNWIND;
 | 
|---|
| 291 |         }
 | 
|---|
| 292 | 
 | 
|---|
| 293 |         // Get the instuction pointer and a reading pointer into the exception table
 | 
|---|
| 294 |         lsda_header_info lsd_info;
 | 
|---|
| 295 |         const unsigned char * cur_ptr = parse_lsda_header(context, lsd, &lsd_info);
 | 
|---|
| 296 |         _Unwind_Ptr instruction_ptr = _Unwind_GetIP( context );
 | 
|---|
| 297 | 
 | 
|---|
| 298 |         // Linearly search the table for stuff to do
 | 
|---|
| 299 |         while( cur_ptr < lsd_info.action_table ) {
 | 
|---|
| 300 |                 _Unwind_Ptr callsite_start;
 | 
|---|
| 301 |                 _Unwind_Ptr callsite_len;
 | 
|---|
| 302 |                 _Unwind_Ptr callsite_landing_pad;
 | 
|---|
| 303 |                 _uleb128_t  callsite_action;
 | 
|---|
| 304 | 
 | 
|---|
| 305 |                 // Decode the common stuff we have in here
 | 
|---|
| 306 |                 cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_start);
 | 
|---|
| 307 |                 cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_len);
 | 
|---|
| 308 |                 cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad);
 | 
|---|
| 309 |                 cur_ptr = read_uleb128(cur_ptr, &callsite_action);
 | 
|---|
| 310 | 
 | 
|---|
| 311 |                 // Have we reach the correct frame info yet?
 | 
|---|
| 312 |                 if( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) {
 | 
|---|
| 313 | #ifdef __CFA_DEBUG_PRINT__
 | 
|---|
| 314 |                         void * ls = (void*)lsd_info.Start;
 | 
|---|
| 315 |                         void * cs = (void*)callsite_start;
 | 
|---|
| 316 |                         void * cl = (void*)callsite_len;
 | 
|---|
| 317 |                         void * bp = (void*)lsd_info.Start + callsite_start;
 | 
|---|
| 318 |                         void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
 | 
|---|
| 319 |                         void * ip = (void*)instruction_ptr;
 | 
|---|
| 320 |                         __cfaabi_dbg_print_safe("\nfound %p - %p (%p, %p, %p), looking for %p\n",
 | 
|---|
| 321 |                                         bp, ep, ls, cs, cl, ip);
 | 
|---|
| 322 | #endif // __CFA_DEBUG_PRINT__
 | 
|---|
| 323 |                         continue;
 | 
|---|
| 324 |                 }
 | 
|---|
| 325 | 
 | 
|---|
| 326 |                 // Have we gone too far?
 | 
|---|
| 327 |                 if( lsd_info.Start + callsite_start > instruction_ptr ) {
 | 
|---|
| 328 |                         printf(" gone too far");
 | 
|---|
| 329 |                         break;
 | 
|---|
| 330 |                 }
 | 
|---|
| 331 | 
 | 
|---|
| 332 |                 // Something to do?
 | 
|---|
| 333 |                 if( callsite_landing_pad ) {
 | 
|---|
| 334 |                         // Which phase are we in
 | 
|---|
| 335 |                         if (actions & _UA_SEARCH_PHASE) {
 | 
|---|
| 336 |                                 // In search phase, these means we found a potential handler we must check.
 | 
|---|
| 337 | 
 | 
|---|
| 338 |                                 // We have arbitrarily decided that 0 means nothing to do and 1 means there is
 | 
|---|
| 339 |                                 // a potential handler. This doesn't seem to conflict the gcc default behavior.
 | 
|---|
| 340 |                                 if (callsite_action != 0) {
 | 
|---|
| 341 |                                         // Now we want to run some code to see if the handler matches
 | 
|---|
| 342 |                                         // This is the tricky part where we want to the power to run arbitrary code
 | 
|---|
| 343 |                                         // However, generating a new exception table entry and try routine every time
 | 
|---|
| 344 |                                         // is way more expansive than we might like
 | 
|---|
| 345 |                                         // The information we have is :
 | 
|---|
| 346 |                                         //  - The GR (Series of registers)
 | 
|---|
| 347 |                                         //    GR1=GP Global Pointer of frame ref by context
 | 
|---|
| 348 |                                         //  - The instruction pointer
 | 
|---|
| 349 |                                         //  - The instruction pointer info (???)
 | 
|---|
| 350 |                                         //  - The CFA (Canonical Frame Address)
 | 
|---|
| 351 |                                         //  - The BSP (Probably the base stack pointer)
 | 
|---|
| 352 | 
 | 
|---|
| 353 | 
 | 
|---|
| 354 |                                         // The current apprach uses one exception table entry per try block
 | 
|---|
| 355 |                                         _uleb128_t imatcher;
 | 
|---|
| 356 |                                         // Get the relative offset to the {...}?
 | 
|---|
| 357 |                                         cur_ptr = read_uleb128(cur_ptr, &imatcher);
 | 
|---|
| 358 | 
 | 
|---|
| 359 |                                         _Unwind_Reason_Code (*matcher)(exception_t *) =
 | 
|---|
| 360 |                                                 MATCHER_FROM_CONTEXT(context);
 | 
|---|
| 361 |                                         int index = matcher(shared_stack.current_exception);
 | 
|---|
| 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
 | 
|---|
| 367 |                                         if( ret == _URC_HANDLER_FOUND) {
 | 
|---|
| 368 |                                                 __cfaabi_dbg_print_safe(" handler found\n");
 | 
|---|
| 369 |                                         } else {
 | 
|---|
| 370 |                                                 __cfaabi_dbg_print_safe(" no handler\n");
 | 
|---|
| 371 |                                         }
 | 
|---|
| 372 |                                         return ret;
 | 
|---|
| 373 |                                 }
 | 
|---|
| 374 | 
 | 
|---|
| 375 |                                 // This is only a cleanup handler, ignore it
 | 
|---|
| 376 |                                 __cfaabi_dbg_print_safe(" no action");
 | 
|---|
| 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
 | 
|---|
| 394 |                                 _Unwind_SetIP( context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
 | 
|---|
| 395 | 
 | 
|---|
| 396 |                                 __cfaabi_dbg_print_safe(" action\n");
 | 
|---|
| 397 | 
 | 
|---|
| 398 |                                 // Return have some action to run
 | 
|---|
| 399 |                                 return _URC_INSTALL_CONTEXT;
 | 
|---|
| 400 |                         }
 | 
|---|
| 401 |                 }
 | 
|---|
| 402 | 
 | 
|---|
| 403 |                 // Nothing to do, move along
 | 
|---|
| 404 |                 __cfaabi_dbg_print_safe(" no landing pad");
 | 
|---|
| 405 |         }
 | 
|---|
| 406 |         // No handling found
 | 
|---|
| 407 |         __cfaabi_dbg_print_safe(" table end reached\n");
 | 
|---|
| 408 | 
 | 
|---|
| 409 |         UNWIND:
 | 
|---|
| 410 |         __cfaabi_dbg_print_safe(" unwind\n");
 | 
|---|
| 411 | 
 | 
|---|
| 412 |         // Keep unwinding the stack
 | 
|---|
| 413 |         return _URC_CONTINUE_UNWIND;
 | 
|---|
| 414 | }
 | 
|---|
| 415 | 
 | 
|---|
| 416 | // Try statements are hoisted out see comments for details. While this could probably be unique
 | 
|---|
| 417 | // and simply linked from libcfa but there is one problem left, see the exception table for details
 | 
|---|
| 418 | __attribute__((noinline))
 | 
|---|
| 419 | void __cfaabi_ehm__try_terminate(void (*try_block)(),
 | 
|---|
| 420 |                 void (*catch_block)(int index, exception_t * except),
 | 
|---|
| 421 |                 __attribute__((unused)) int (*match_block)(exception_t * except)) {
 | 
|---|
| 422 |         //! volatile int xy = 0;
 | 
|---|
| 423 |         //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy);
 | 
|---|
| 424 | 
 | 
|---|
| 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 | 
 | 
|---|
| 430 |         // Setup the personality routine and exception table.
 | 
|---|
| 431 | #ifdef __PIC__
 | 
|---|
| 432 |         asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0");
 | 
|---|
| 433 |         asm volatile (".cfi_lsda 0x1b, .LLSDACFA2");
 | 
|---|
| 434 | #else
 | 
|---|
| 435 |         asm volatile (".cfi_personality 0x3,__gcfa_personality_v0");
 | 
|---|
| 436 |         asm volatile (".cfi_lsda 0x3, .LLSDACFA2");
 | 
|---|
| 437 | #endif
 | 
|---|
| 438 | 
 | 
|---|
| 439 |         // Label which defines the start of the area for which the handler is setup.
 | 
|---|
| 440 |         asm volatile (".TRYSTART:");
 | 
|---|
| 441 | 
 | 
|---|
| 442 |         // The actual statements of the try blocks
 | 
|---|
| 443 |         try_block();
 | 
|---|
| 444 | 
 | 
|---|
| 445 |         // asm statement to prevent deadcode removal
 | 
|---|
| 446 |         asm volatile goto ("" : : : : CATCH );
 | 
|---|
| 447 | 
 | 
|---|
| 448 |         // Normal return for when there is no throw.
 | 
|---|
| 449 |         return;
 | 
|---|
| 450 | 
 | 
|---|
| 451 |         // Exceptionnal path
 | 
|---|
| 452 |         CATCH : __attribute__(( unused ));
 | 
|---|
| 453 |         // Label which defines the end of the area for which the handler is setup.
 | 
|---|
| 454 |         asm volatile (".TRYEND:");
 | 
|---|
| 455 |         // Label which defines the start of the exception landing pad.  Basically what is called when the exception is
 | 
|---|
| 456 |         // caught.  Note, if multiple handlers are given, the multiplexing should be done by the generated code, not the
 | 
|---|
| 457 |         // exception runtime.
 | 
|---|
| 458 |         asm volatile (".CATCH:");
 | 
|---|
| 459 | 
 | 
|---|
| 460 |         // Exception handler
 | 
|---|
| 461 |         catch_block( shared_stack.current_handler_index,
 | 
|---|
| 462 |                      shared_stack.current_exception );
 | 
|---|
| 463 | }
 | 
|---|
| 464 | 
 | 
|---|
| 465 | // Exception table data we need to generate. While this is almost generic, the custom data refers
 | 
|---|
| 466 | // to {*}try_terminate, which is no way generic. Some more works need to be done if we want to
 | 
|---|
| 467 | // have a single call to the try routine.
 | 
|---|
| 468 | 
 | 
|---|
| 469 | #ifdef __PIC__
 | 
|---|
| 470 | #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
| 471 | asm (
 | 
|---|
| 472 |         // HEADER
 | 
|---|
| 473 |         ".LFECFA1:\n"
 | 
|---|
| 474 |         "       .globl  __gcfa_personality_v0\n"
 | 
|---|
| 475 |         "       .section        .gcc_except_table,\"a\",@progbits\n"
 | 
|---|
| 476 |         // TABLE HEADER (important field is the BODY length at the end)
 | 
|---|
| 477 |         ".LLSDACFA2:\n"
 | 
|---|
| 478 |         "       .byte   0xff\n"
 | 
|---|
| 479 |         "       .byte   0xff\n"
 | 
|---|
| 480 |         "       .byte   0x1\n"
 | 
|---|
| 481 |         "       .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n"
 | 
|---|
| 482 |         // BODY (language specific data)
 | 
|---|
| 483 |         // This uses language specific data and can be modified arbitrarily
 | 
|---|
| 484 |         // We use handled area offset, handled area length,
 | 
|---|
| 485 |         // handler landing pad offset and 1 (action code, gcc seems to use 0).
 | 
|---|
| 486 |         ".LLSDACSBCFA2:\n"
 | 
|---|
| 487 |         "       .uleb128 .TRYSTART-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| 488 |         "       .uleb128 .TRYEND-.TRYSTART\n"
 | 
|---|
| 489 |         "       .uleb128 .CATCH-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| 490 |         "       .uleb128 1\n"
 | 
|---|
| 491 |         ".LLSDACSECFA2:\n"
 | 
|---|
| 492 |         // TABLE FOOTER
 | 
|---|
| 493 |         "       .text\n"
 | 
|---|
| 494 |         "       .size   __cfaabi_ehm__try_terminate, .-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| 495 | );
 | 
|---|
| 496 | 
 | 
|---|
| 497 | // Somehow this piece of helps with the resolution of debug symbols.
 | 
|---|
| 498 | __attribute__((unused)) static const int dummy = 0;
 | 
|---|
| 499 | 
 | 
|---|
| 500 | asm (
 | 
|---|
| 501 |         // Add a hidden symbol which points at the function.
 | 
|---|
| 502 |         "       .hidden CFA.ref.__gcfa_personality_v0\n"
 | 
|---|
| 503 |         "       .weak   CFA.ref.__gcfa_personality_v0\n"
 | 
|---|
| 504 |         // No clue what this does specifically
 | 
|---|
| 505 |         "       .section        .data.rel.local.CFA.ref.__gcfa_personality_v0,\"awG\",@progbits,CFA.ref.__gcfa_personality_v0,comdat\n"
 | 
|---|
| 506 |         "       .align 8\n"
 | 
|---|
| 507 |         "       .type CFA.ref.__gcfa_personality_v0, @object\n"
 | 
|---|
| 508 |         "       .size CFA.ref.__gcfa_personality_v0, 8\n"
 | 
|---|
| 509 |         "CFA.ref.__gcfa_personality_v0:\n"
 | 
|---|
| 510 | #if defined( __x86_64 )
 | 
|---|
| 511 |         "       .quad __gcfa_personality_v0\n"
 | 
|---|
| 512 | #else // then __i386
 | 
|---|
| 513 |         "   .long __gcfa_personality_v0\n"
 | 
|---|
| 514 | #endif
 | 
|---|
| 515 | );
 | 
|---|
| 516 | #else
 | 
|---|
| 517 | #error Exception Handling: unknown architecture for position independent code.
 | 
|---|
| 518 | #endif // __i386 || __x86_64
 | 
|---|
| 519 | #else // __PIC__
 | 
|---|
| 520 | #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
| 521 | asm (
 | 
|---|
| 522 |         // HEADER
 | 
|---|
| 523 |         ".LFECFA1:\n"
 | 
|---|
| 524 |         "       .globl  __gcfa_personality_v0\n"
 | 
|---|
| 525 |         "       .section        .gcc_except_table,\"a\",@progbits\n"
 | 
|---|
| 526 |         // TABLE HEADER (important field is the BODY length at the end)
 | 
|---|
| 527 |         ".LLSDACFA2:\n"
 | 
|---|
| 528 |         "       .byte   0xff\n"
 | 
|---|
| 529 |         "       .byte   0xff\n"
 | 
|---|
| 530 |         "       .byte   0x1\n"
 | 
|---|
| 531 |         "       .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n"
 | 
|---|
| 532 |         // BODY (language specific data)
 | 
|---|
| 533 |         ".LLSDACSBCFA2:\n"
 | 
|---|
| 534 |         //      Handled area start (relative to start of function)
 | 
|---|
| 535 |         "       .uleb128 .TRYSTART-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| 536 |         //      Handled area length
 | 
|---|
| 537 |         "       .uleb128 .TRYEND-.TRYSTART\n"
 | 
|---|
| 538 |         //      Handler landing pad address (relative to start of function)
 | 
|---|
| 539 |         "       .uleb128 .CATCH-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| 540 |         //      Action code, gcc seems to always use 0.
 | 
|---|
| 541 |         "       .uleb128 1\n"
 | 
|---|
| 542 |         // TABLE FOOTER
 | 
|---|
| 543 |         ".LLSDACSECFA2:\n"
 | 
|---|
| 544 |         "       .text\n"
 | 
|---|
| 545 |         "       .size   __cfaabi_ehm__try_terminate, .-__cfaabi_ehm__try_terminate\n"
 | 
|---|
| 546 |         "       .ident  \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
 | 
|---|
| 547 |         "       .section        .note.GNU-stack,\"x\",@progbits\n"
 | 
|---|
| 548 | );
 | 
|---|
| 549 | #else
 | 
|---|
| 550 | #error Exception Handling: unknown architecture for position dependent code.
 | 
|---|
| 551 | #endif // __i386 || __x86_64
 | 
|---|
| 552 | #endif // __PIC__
 | 
|---|
| 553 | 
 | 
|---|
| 554 | #pragma GCC pop_options
 | 
|---|