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