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