[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
|
---|
[86d5ba7c] | 11 | // Last Modified By : Andrew Beach
|
---|
[cbce272] | 12 | // Last Modified On : Fri Aug 4 15:20:00 2017
|
---|
| 13 | // Update Count : 6
|
---|
[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>
|
---|
| 25 |
|
---|
[b947fb2] | 26 | // FIX ME: temporary hack to keep ARM build working
|
---|
| 27 | #ifndef _URC_FATAL_PHASE1_ERROR
|
---|
| 28 | #define _URC_FATAL_PHASE1_ERROR 2
|
---|
| 29 | #endif // ! _URC_FATAL_PHASE1_ERROR
|
---|
| 30 | #ifndef _URC_FATAL_PHASE2_ERROR
|
---|
| 31 | #define _URC_FATAL_PHASE2_ERROR 2
|
---|
| 32 | #endif // ! _URC_FATAL_PHASE2_ERROR
|
---|
| 33 |
|
---|
[fa4805f] | 34 | #include "lsda.h"
|
---|
| 35 |
|
---|
[cbce272] | 36 |
|
---|
| 37 | // Base exception vtable is abstract, you should not have base exceptions.
|
---|
| 38 | struct __cfaehm__base_exception_t_vtable
|
---|
| 39 | ___cfaehm__base_exception_t_vtable_instance = {
|
---|
| 40 | .parent = NULL,
|
---|
| 41 | .size = 0,
|
---|
| 42 | .copy = NULL,
|
---|
| 43 | .free = NULL,
|
---|
| 44 | .msg = NULL
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 |
|
---|
[fa4805f] | 48 | // Temperary global exception context. Does not work with concurency.
|
---|
[86d5ba7c] | 49 | struct exception_context_t {
|
---|
[fa4805f] | 50 | struct __cfaehm__try_resume_node * top_resume;
|
---|
| 51 | struct __cfaehm__try_resume_node * current_resume;
|
---|
| 52 |
|
---|
[86d5ba7c] | 53 | exception * current_exception;
|
---|
[fa4805f] | 54 | int current_handler_index;
|
---|
[cbce272] | 55 | } shared_stack = {NULL, NULL, 0, 0};
|
---|
[86d5ba7c] | 56 |
|
---|
| 57 | // Get the current exception context.
|
---|
| 58 | // There can be a single global until multithreading occurs, then each stack
|
---|
| 59 | // needs its own. It will have to be updated to handle that.
|
---|
| 60 | struct exception_context_t * this_exception_context() {
|
---|
| 61 | return &shared_stack;
|
---|
| 62 | }
|
---|
| 63 | //#define SAVE_EXCEPTION_CONTEXT(to_name)
|
---|
| 64 | //struct exception_context_t * to_name = this_exception_context();
|
---|
| 65 | //exception * this_exception() {
|
---|
| 66 | // return this_exception_context()->current_exception;
|
---|
| 67 | //}
|
---|
[fa4805f] | 68 |
|
---|
| 69 |
|
---|
[b947fb2] | 70 | // This macro should be the only thing that needs to change across machines. Used in the personality function, way down
|
---|
| 71 | // in termination.
|
---|
[ad0be81] | 72 | // struct _Unwind_Context * -> _Unwind_Reason_Code(*)(exception *)
|
---|
[fa4805f] | 73 | #define MATCHER_FROM_CONTEXT(ptr_to_context) \
|
---|
[ad0be81] | 74 | (*(_Unwind_Reason_Code(**)(exception*))(_Unwind_GetCFA(ptr_to_context) + 8))
|
---|
[fa4805f] | 75 |
|
---|
| 76 |
|
---|
| 77 | // RESUMPTION ================================================================
|
---|
| 78 |
|
---|
[38ac6ec] | 79 | void __cfaehm__throw_resume(exception * except) {
|
---|
[fa4805f] | 80 |
|
---|
| 81 | // DEBUG
|
---|
[cbce272] | 82 | printf("Throwing resumption exception\n");
|
---|
[fa4805f] | 83 |
|
---|
[307a732] | 84 | struct __cfaehm__try_resume_node * original_head = shared_stack.current_resume;
|
---|
| 85 | struct __cfaehm__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 |
|
---|
[cbce272] | 96 | printf("Unhandled exception\n");
|
---|
[fa4805f] | 97 | shared_stack.current_resume = original_head;
|
---|
| 98 |
|
---|
| 99 | // Fall back to termination:
|
---|
[38ac6ec] | 100 | __cfaehm__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 |
|
---|
[307a732] | 107 | void __cfaehm__try_resume_setup(struct __cfaehm__try_resume_node * node,
|
---|
| 108 | int (*handler)(exception * except)) {
|
---|
[fa4805f] | 109 | node->next = shared_stack.top_resume;
|
---|
[307a732] | 110 | node->handler = handler;
|
---|
[fa4805f] | 111 | shared_stack.top_resume = node;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[307a732] | 114 | void __cfaehm__try_resume_cleanup(struct __cfaehm__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 |
|
---|
| 124 | struct __cfaehm__node {
|
---|
| 125 | struct __cfaehm__node * next;
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | #define NODE_TO_EXCEPT(node) ((exception *)(1 + (node)))
|
---|
| 129 | #define EXCEPT_TO_NODE(except) ((struct __cfaehm__node *)(except) - 1)
|
---|
[86d5ba7c] | 130 |
|
---|
| 131 | // Creates a copy of the indicated exception and sets current_exception to it.
|
---|
| 132 | static void __cfaehm__allocate_exception( exception * except ) {
|
---|
| 133 | struct exception_context_t * context = this_exception_context();
|
---|
| 134 |
|
---|
[ff7ff14a] | 135 | // Allocate memory for the exception.
|
---|
| 136 | struct __cfaehm__node * store = malloc(
|
---|
[cbce272] | 137 | sizeof( struct __cfaehm__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.
|
---|
| 153 | static void __cfaehm__delete_exception( exception * except ) {
|
---|
| 154 | struct exception_context_t * context = this_exception_context();
|
---|
| 155 |
|
---|
| 156 | // DEBUG
|
---|
[cbce272] | 157 | printf( "Deleting Exception\n");
|
---|
[ff7ff14a] | 158 |
|
---|
| 159 | // Remove the exception from the list.
|
---|
| 160 | struct __cfaehm__node * to_free = EXCEPT_TO_NODE(except);
|
---|
| 161 | struct __cfaehm__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.
|
---|
[cbce272] | 181 | void __cfaehm__cleanup_terminate( void * except ) {
|
---|
| 182 | if ( *(void**)except ) __cfaehm__delete_exception( *(exception**)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.
|
---|
| 205 | __attribute__((noreturn)) void __cfaehm__begin_unwind(void) {
|
---|
| 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 |
|
---|
[b947fb2] | 215 | // If we reach here it means something happened. For resumption to work we need to find a way to return back to
|
---|
| 216 | // here. Most of them will probably boil down to setting a global flag and making the phase 1 either stop or
|
---|
| 217 | // fail. Causing an error on purpose may help avoiding unnecessary work but it might have some weird side
|
---|
| 218 | // effects. If we just pretend no handler was found that would work but may be expensive for no reason since we
|
---|
| 219 | // will always search the whole stack.
|
---|
[fa4805f] | 220 |
|
---|
| 221 | if( ret == _URC_END_OF_STACK ) {
|
---|
[b947fb2] | 222 | // No proper handler was found. This can be handled in several way. C++ calls std::terminate Here we
|
---|
| 223 | // force unwind the stack, basically raising a cancellation.
|
---|
[fa4805f] | 224 | printf("Uncaught exception %p\n", &this_exception_storage);
|
---|
| 225 |
|
---|
| 226 | ret = _Unwind_ForcedUnwind( &this_exception_storage, _Stop_Fn, (void*)0x22 );
|
---|
| 227 | printf("UNWIND ERROR %d after force unwind\n", ret);
|
---|
| 228 | abort();
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[b947fb2] | 231 | // We did not simply reach the end of the stack without finding a handler. Something wen't wrong
|
---|
[fa4805f] | 232 | printf("UNWIND ERROR %d after raise exception\n", ret);
|
---|
| 233 | abort();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[86d5ba7c] | 236 | void __cfaehm__throw_terminate( exception * val ) {
|
---|
| 237 | // DEBUG
|
---|
| 238 | printf("Throwing termination exception\n");
|
---|
| 239 |
|
---|
| 240 | __cfaehm__allocate_exception( val );
|
---|
| 241 | __cfaehm__begin_unwind();
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[38ac6ec] | 244 | void __cfaehm__rethrow_terminate(void) {
|
---|
[fa4805f] | 245 | // DEBUG
|
---|
| 246 | printf("Rethrowing termination exception\n");
|
---|
| 247 |
|
---|
[86d5ba7c] | 248 | __cfaehm__begin_unwind();
|
---|
[fa4805f] | 249 | }
|
---|
| 250 |
|
---|
[b947fb2] | 251 | // This is our personality routine. For every stack frame anotated with ".cfi_personality 0x3,__gcfa_personality_v0".
|
---|
| 252 | // This function will be called twice when unwinding. Once in the search phased and once in the cleanup phase.
|
---|
[fa4805f] | 253 | _Unwind_Reason_Code __gcfa_personality_v0 (
|
---|
| 254 | int version, _Unwind_Action actions, unsigned long long exceptionClass,
|
---|
| 255 | struct _Unwind_Exception* unwind_exception,
|
---|
| 256 | struct _Unwind_Context* context)
|
---|
| 257 | {
|
---|
| 258 |
|
---|
| 259 | // DEBUG
|
---|
| 260 | //printf("CFA: 0x%lx\n", _Unwind_GetCFA(context));
|
---|
| 261 | printf("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
|
---|
| 262 |
|
---|
| 263 | // If we've reached the end of the stack then there is nothing much we can do...
|
---|
| 264 | if( actions & _UA_END_OF_STACK ) return _URC_END_OF_STACK;
|
---|
| 265 |
|
---|
| 266 | // DEBUG
|
---|
| 267 | if (actions & _UA_SEARCH_PHASE) {
|
---|
| 268 | printf(" lookup phase");
|
---|
| 269 | }
|
---|
| 270 | // DEBUG
|
---|
| 271 | else if (actions & _UA_CLEANUP_PHASE) {
|
---|
| 272 | printf(" cleanup phase");
|
---|
| 273 | }
|
---|
| 274 | // Just in case, probably can't actually happen
|
---|
| 275 | else {
|
---|
| 276 | printf(" error\n");
|
---|
| 277 | return _URC_FATAL_PHASE1_ERROR;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | // Get a pointer to the language specific data from which we will read what we need
|
---|
| 281 | const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( context );
|
---|
| 282 |
|
---|
| 283 | if( !lsd ) { //Nothing to do, keep unwinding
|
---|
| 284 | printf(" no LSD");
|
---|
| 285 | goto UNWIND;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | // Get the instuction pointer and a reading pointer into the exception table
|
---|
| 289 | lsda_header_info lsd_info;
|
---|
| 290 | const unsigned char * cur_ptr = parse_lsda_header( context, lsd, &lsd_info);
|
---|
| 291 | _Unwind_Ptr instruction_ptr = _Unwind_GetIP( context );
|
---|
| 292 |
|
---|
| 293 | // Linearly search the table for stuff to do
|
---|
| 294 | while( cur_ptr < lsd_info.action_table ) {
|
---|
| 295 | _Unwind_Ptr callsite_start;
|
---|
| 296 | _Unwind_Ptr callsite_len;
|
---|
| 297 | _Unwind_Ptr callsite_landing_pad;
|
---|
| 298 | _uleb128_t callsite_action;
|
---|
| 299 |
|
---|
| 300 | // Decode the common stuff we have in here
|
---|
| 301 | cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_start);
|
---|
| 302 | cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_len);
|
---|
| 303 | cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad);
|
---|
| 304 | cur_ptr = read_uleb128 (cur_ptr, &callsite_action);
|
---|
| 305 |
|
---|
| 306 | // Have we reach the correct frame info yet?
|
---|
| 307 | if( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) {
|
---|
| 308 | //DEBUG BEGIN
|
---|
| 309 | void * ls = (void*)lsd_info.Start;
|
---|
| 310 | void * cs = (void*)callsite_start;
|
---|
| 311 | void * cl = (void*)callsite_len;
|
---|
| 312 | void * bp = (void*)lsd_info.Start + callsite_start;
|
---|
| 313 | void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
|
---|
| 314 | void * ip = (void*)instruction_ptr;
|
---|
| 315 | printf("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
|
---|
| 316 | //DEBUG END
|
---|
| 317 | continue;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | // Have we gone too far
|
---|
| 321 | if( lsd_info.Start + callsite_start > instruction_ptr ) {
|
---|
| 322 | printf(" gone too far");
|
---|
| 323 | break;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | // Something to do?
|
---|
| 327 | if( callsite_landing_pad ) {
|
---|
| 328 | // Which phase are we in
|
---|
| 329 | if (actions & _UA_SEARCH_PHASE) {
|
---|
| 330 | // Search phase, this means we probably found a potential handler and must check if it is a match
|
---|
| 331 |
|
---|
| 332 | // If we have arbitrarily decided that 0 means nothing to do and 1 means there is a potential handler
|
---|
| 333 | // This doesn't seem to conflict the gcc default behavior
|
---|
| 334 | if (callsite_action != 0) {
|
---|
| 335 | // Now we want to run some code to see if the handler matches
|
---|
| 336 | // This is the tricky part where we want to the power to run arbitrary code
|
---|
| 337 | // However, generating a new exception table entry and try routine every time
|
---|
| 338 | // is way more expansive than we might like
|
---|
| 339 | // The information we have is :
|
---|
| 340 | // - The GR (Series of registers)
|
---|
| 341 | // GR1=GP Global Pointer of frame ref by context
|
---|
| 342 | // - The instruction pointer
|
---|
| 343 | // - The instruction pointer info (???)
|
---|
| 344 | // - The CFA (Canonical Frame Address)
|
---|
| 345 | // - The BSP (Probably the base stack pointer)
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 | // The current apprach uses one exception table entry per try block
|
---|
| 349 | _uleb128_t imatcher;
|
---|
| 350 | // Get the relative offset to the
|
---|
| 351 | cur_ptr = read_uleb128 (cur_ptr, &imatcher);
|
---|
| 352 |
|
---|
| 353 | // Get a function pointer from the relative offset and call it
|
---|
| 354 | // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;
|
---|
| 355 |
|
---|
[ad0be81] | 356 | _Unwind_Reason_Code (*matcher)(exception *) =
|
---|
[fa4805f] | 357 | MATCHER_FROM_CONTEXT(context);
|
---|
[86d5ba7c] | 358 | int index = matcher(shared_stack.current_exception);
|
---|
[fa4805f] | 359 | _Unwind_Reason_Code ret = (0 == index)
|
---|
| 360 | ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;
|
---|
| 361 | shared_stack.current_handler_index = index;
|
---|
| 362 |
|
---|
| 363 | // Based on the return value, check if we matched the exception
|
---|
| 364 | if( ret == _URC_HANDLER_FOUND) printf(" handler found\n");
|
---|
| 365 | else printf(" no handler\n");
|
---|
| 366 | return ret;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | // This is only a cleanup handler, ignore it
|
---|
| 370 | printf(" no action");
|
---|
| 371 | }
|
---|
| 372 | else if (actions & _UA_CLEANUP_PHASE) {
|
---|
| 373 |
|
---|
| 374 | if( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){
|
---|
| 375 | // If this is a potential exception handler
|
---|
| 376 | // but not the one that matched the exception in the seach phase,
|
---|
| 377 | // just ignore it
|
---|
| 378 | goto UNWIND;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | // We need to run some clean-up or a handler
|
---|
| 382 | // These statment do the right thing but I don't know any specifics at all
|
---|
| 383 | _Unwind_SetGR( context, __builtin_eh_return_data_regno(0), (_Unwind_Ptr) unwind_exception );
|
---|
| 384 | _Unwind_SetGR( context, __builtin_eh_return_data_regno(1), 0 );
|
---|
| 385 |
|
---|
| 386 | // I assume this sets the instruction pointer to the adress of the landing pad
|
---|
| 387 | // It doesn't actually set it, it only state the value that needs to be set once we return _URC_INSTALL_CONTEXT
|
---|
[b947fb2] | 388 | _Unwind_SetIP( context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
|
---|
[fa4805f] | 389 |
|
---|
| 390 | // DEBUG
|
---|
| 391 | printf(" action\n");
|
---|
| 392 |
|
---|
| 393 | // Return have some action to run
|
---|
| 394 | return _URC_INSTALL_CONTEXT;
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | // Nothing to do, move along
|
---|
| 399 | printf(" no landing pad");
|
---|
| 400 | }
|
---|
| 401 | // No handling found
|
---|
| 402 | printf(" table end reached\n");
|
---|
| 403 |
|
---|
| 404 | // DEBUG
|
---|
| 405 | UNWIND:
|
---|
| 406 | printf(" unwind\n");
|
---|
| 407 |
|
---|
| 408 | // Keep unwinding the stack
|
---|
| 409 | return _URC_CONTINUE_UNWIND;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[b947fb2] | 412 | // Try statements are hoisted out see comments for details. With this could probably be unique and simply linked from
|
---|
| 413 | // libcfa but there is one problem left, see the exception table for details
|
---|
[fa4805f] | 414 | __attribute__((noinline))
|
---|
[307a732] | 415 | void __cfaehm__try_terminate(void (*try_block)(),
|
---|
| 416 | void (*catch_block)(int index, exception * except),
|
---|
| 417 | __attribute__((unused)) int (*match_block)(exception * 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 |
|
---|
[fa4805f] | 426 | // Setup the personality routine
|
---|
| 427 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0");
|
---|
| 428 | // Setup the exception table
|
---|
| 429 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2");
|
---|
| 430 |
|
---|
[b947fb2] | 431 | // Label which defines the start of the area for which the handler is setup.
|
---|
[fa4805f] | 432 | asm volatile (".TRYSTART:");
|
---|
| 433 |
|
---|
| 434 | // The actual statements of the try blocks
|
---|
| 435 | try_block();
|
---|
| 436 |
|
---|
| 437 | // asm statement to prevent deadcode removal
|
---|
| 438 | asm volatile goto ("" : : : : CATCH );
|
---|
| 439 |
|
---|
| 440 | // Normal return
|
---|
| 441 | return;
|
---|
| 442 |
|
---|
| 443 | // Exceptionnal path
|
---|
| 444 | CATCH : __attribute__(( unused ));
|
---|
[b947fb2] | 445 | // Label which defines the end of the area for which the handler is setup.
|
---|
[fa4805f] | 446 | asm volatile (".TRYEND:");
|
---|
[b947fb2] | 447 | // Label which defines the start of the exception landing pad. Basically what is called when the exception is
|
---|
| 448 | // caught. Note, if multiple handlers are given, the multiplexing should be done by the generated code, not the
|
---|
| 449 | // exception runtime.
|
---|
[fa4805f] | 450 | asm volatile (".CATCH:");
|
---|
| 451 |
|
---|
| 452 | // Exception handler
|
---|
[b947fb2] | 453 | catch_block( shared_stack.current_handler_index,
|
---|
[86d5ba7c] | 454 | shared_stack.current_exception );
|
---|
[fa4805f] | 455 | }
|
---|
| 456 |
|
---|
[b947fb2] | 457 | // Exception table data we need to generate. While this is almost generic, the custom data refers to foo_try_match try
|
---|
| 458 | // 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.
|
---|
| 459 |
|
---|
| 460 | #if defined( __x86_64__ ) || defined( __i386__ )
|
---|
[fa4805f] | 461 | asm (
|
---|
| 462 | //HEADER
|
---|
| 463 | ".LFECFA1:\n"
|
---|
| 464 | " .globl __gcfa_personality_v0\n"
|
---|
| 465 | " .section .gcc_except_table,\"a\",@progbits\n"
|
---|
| 466 | ".LLSDACFA2:\n" //TABLE header
|
---|
| 467 | " .byte 0xff\n"
|
---|
| 468 | " .byte 0xff\n"
|
---|
| 469 | " .byte 0x1\n"
|
---|
| 470 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" // BODY length
|
---|
| 471 | // Body uses language specific data and therefore could be modified arbitrarily
|
---|
| 472 | ".LLSDACSBCFA2:\n" // BODY start
|
---|
[307a732] | 473 | " .uleb128 .TRYSTART-__cfaehm__try_terminate\n" // Handled area start (relative to start of function)
|
---|
[fa4805f] | 474 | " .uleb128 .TRYEND-.TRYSTART\n" // Handled area length
|
---|
[307a732] | 475 | " .uleb128 .CATCH-__cfaehm__try_terminate\n" // Hanlder landing pad adress (relative to start of function)
|
---|
[fa4805f] | 476 | " .uleb128 1\n" // Action code, gcc seems to use always 0
|
---|
| 477 | ".LLSDACSECFA2:\n" // BODY end
|
---|
| 478 | " .text\n" // TABLE footer
|
---|
[307a732] | 479 | " .size __cfaehm__try_terminate, .-__cfaehm__try_terminate\n"
|
---|
[fa4805f] | 480 | " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
|
---|
| 481 | // " .section .note.GNU-stack,\"x\",@progbits\n"
|
---|
| 482 | );
|
---|
[b947fb2] | 483 | #endif // __x86_64__ || __i386__
|
---|