Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/exception.c

    r73530d9 r851fd92  
    1010// Created On       : Mon Jun 26 15:13:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Apr 03 11:57:00 2020
    13 // Update Count     : 14
     12// Last Modified On : Tue Apr 14 12:01:00 2020
     13// Update Count     : 18
    1414//
    1515
     
    2828#include <unwind.h>
    2929#include <bits/debug.hfa>
     30#include "stdhdr/assert.h"
    3031
    3132// FIX ME: temporary hack to keep ARM build working
     
    7576// RESUMPTION ================================================================
    7677
     78static void reset_top_resume(struct __cfaehm_try_resume_node ** store) {
     79        this_exception_context()->top_resume = *store;
     80}
     81
    7782void __cfaehm_throw_resume(exception_t * except) {
    7883        struct exception_context_t * context = this_exception_context();
    7984
    80         __cfaabi_dbg_print_safe("Throwing resumption exception\n");
    81 
     85        __cfadbg_print_safe(exception, "Throwing resumption exception\n");
     86
     87        __attribute__((cleanup(reset_top_resume)))
    8288        struct __cfaehm_try_resume_node * original_head = context->top_resume;
    8389        struct __cfaehm_try_resume_node * current = context->top_resume;
     
    8692                context->top_resume = current->next;
    8793                if (current->handler(except)) {
    88                         context->top_resume = original_head;
    8994                        return;
    9095                }
    9196        }
    9297
    93         __cfaabi_dbg_print_safe("Unhandled exception\n");
    94         context->top_resume = original_head;
     98        __cfadbg_print_safe(exception, "Unhandled exception\n");
    9599
    96100        // Fall back to termination:
     
    176180        struct exception_context_t * context = this_exception_context();
    177181
    178         __cfaabi_dbg_print_safe("Deleting Exception\n");
     182        __cfadbg_print_safe(exception, "Deleting Exception\n");
    179183
    180184        // Remove the exception from the list.
     
    213217                struct _Unwind_Context * unwind_context,
    214218                void * stop_param) {
    215         if ( actions & _UA_END_OF_STACK  ) exit(1);
    216         if ( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
    217 
    218         return _URC_FATAL_PHASE2_ERROR;
     219        // Verify actions follow the rules we expect.
     220        verify((actions & _UA_CLEANUP_PHASE) && (actions & _UA_FORCE_UNWIND));
     221        verify(!(actions & (_UA_SEARCH_PHASE | _UA_HANDER_FRAME)));
     222
     223        if ( actions & _UA_END_OF_STACK ) {
     224                exit(1);
     225        } else {
     226                return _URC_NO_REASON;
     227        }
    219228}
    220229
     
    252261
    253262void __cfaehm_throw_terminate( exception_t * val ) {
    254         __cfaabi_dbg_print_safe("Throwing termination exception\n");
     263        __cfadbg_print_safe(exception, "Throwing termination exception\n");
    255264
    256265        __cfaehm_allocate_exception( val );
     
    259268
    260269void __cfaehm_rethrow_terminate(void) {
    261         __cfaabi_dbg_print_safe("Rethrowing termination exception\n");
     270        __cfadbg_print_safe(exception, "Rethrowing termination exception\n");
    262271
    263272        __cfaehm_begin_unwind();
     
    275284{
    276285
    277         //__cfaabi_dbg_print_safe("CFA: 0x%lx\n", _Unwind_GetCFA(context));
    278         __cfaabi_dbg_print_safe("Personality function (%d, %x, %llu, %p, %p):",
     286        //__cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(context));
     287        __cfadbg_print_safe(exception, "Personality function (%d, %x, %llu, %p, %p):",
    279288                        version, actions, exception_class, unwind_exception, unwind_context);
    280289
    281         // If we've reached the end of the stack then there is nothing much we can do...
    282         if (actions & _UA_END_OF_STACK) return _URC_END_OF_STACK;
    283 
     290        // Verify that actions follow the rules we expect.
     291        // This function should never be called at the end of the stack.
     292        verify(!(actions & _UA_END_OF_STACK));
     293        // Either only the search phase flag is set or...
    284294        if (actions & _UA_SEARCH_PHASE) {
    285                 __cfaabi_dbg_print_safe(" lookup phase");
    286         }
    287         else if (actions & _UA_CLEANUP_PHASE) {
    288                 __cfaabi_dbg_print_safe(" cleanup phase");
    289         }
    290         // Just in case, probably can't actually happen
    291         else {
    292                 printf(" error\n");
    293                 return _URC_FATAL_PHASE1_ERROR;
     295                verify(actions == _UA_SEARCH_PHASE);
     296                __cfadbg_print_safe(exception, " lookup phase");
     297        // ... we are in clean-up phase.
     298        } else {
     299                verify(actions & _UA_CLEANUP_PHASE);
     300                __cfadbg_print_safe(exception, " cleanup phase");
     301                // We shouldn't be the handler frame during forced unwind.
     302                if (actions & _UA_HANDLER_FRAME) {
     303                        verify(!(actions & _UA_FORCE_UNWIND));
     304                        __cfadbg_print_safe(exception, " (handler frame)");
     305                } else if (actions & _UA_FORCE_UNWIND) {
     306                        __cfadbg_print_safe(exception, " (force unwind)");
     307                }
    294308        }
    295309
     
    331345                        void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
    332346                        void * ip = (void*)instruction_ptr;
    333                         __cfaabi_dbg_print_safe("\nfound %p - %p (%p, %p, %p), looking for %p\n",
     347                        __cfadbg_print_safe(exception, "\nfound %p - %p (%p, %p, %p), looking for %p\n",
    334348                                        bp, ep, ls, cs, cl, ip);
    335349#endif // __CFA_DEBUG_PRINT__
     
    346360                if ( 0 == callsite_landing_pad ) {
    347361                        // Nothing to do, move along
    348                         __cfaabi_dbg_print_safe(" no landing pad");
     362                        __cfadbg_print_safe(exception, " no landing pad");
    349363                } else if (actions & _UA_SEARCH_PHASE) {
    350364                        // In search phase, these means we found a potential handler we must check.
     
    384398                                // Based on the return value, check if we matched the exception
    385399                                if (ret == _URC_HANDLER_FOUND) {
    386                                         __cfaabi_dbg_print_safe(" handler found\n");
     400                                        __cfadbg_print_safe(exception, " handler found\n");
    387401                                } else {
    388                                         __cfaabi_dbg_print_safe(" no handler\n");
     402                                        __cfadbg_print_safe(exception, " no handler\n");
    389403                                }
    390404                                return ret;
     
    392406
    393407                        // This is only a cleanup handler, ignore it
    394                         __cfaabi_dbg_print_safe(" no action");
    395                 } else if (actions & _UA_CLEANUP_PHASE) {
     408                        __cfadbg_print_safe(exception, " no action");
     409                } else {
    396410                        // In clean-up phase, no destructors here but this could be the handler.
    397411
     
    414428                        _Unwind_SetIP( unwind_context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
    415429
    416                         __cfaabi_dbg_print_safe(" action\n");
     430                        __cfadbg_print_safe(exception, " action\n");
    417431
    418432                        // Return have some action to run
     
    421435        }
    422436        // No handling found
    423         __cfaabi_dbg_print_safe(" table end reached\n");
     437        __cfadbg_print_safe(exception, " table end reached");
    424438
    425439        UNWIND:
    426         __cfaabi_dbg_print_safe(" unwind\n");
     440        __cfadbg_print_safe(exception, " unwind\n");
    427441
    428442        // Keep unwinding the stack
     
    431445
    432446#pragma GCC push_options
    433 #pragma GCC optimize("O0")
     447#pragma GCC optimize(0)
    434448
    435449// Try statements are hoisted out see comments for details. While this could probably be unique
     
    528542        "       .quad __gcfa_personality_v0\n"
    529543#else // then __i386
    530         "   .long __gcfa_personality_v0\n"
     544        "       .long __gcfa_personality_v0\n"
    531545#endif
    532546);
Note: See TracChangeset for help on using the changeset viewer.