source: doc/working/exception/except.c @ 4cfcf41

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4cfcf41 was ae6f1ec, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Some work on an exception prototype

  • Property mode set to 100644
File size: 7.5 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <unwind.h>
4
5#include "lsda.h"
6
7//Global which defines the current exception
8//Currently an int just to make matching easier
9int this_exception;
10
11//This is our personality routine
12//For every stack frame anotated with ".cfi_personality 0x3,__gcfa_personality_v0"
13//This function will be called twice when unwinding
14//Once in the search phased and once in the cleanup phase
15_Unwind_Reason_Code __gcfa_personality_v0 (
16                     int version, _Unwind_Action actions, unsigned long long exceptionClass,
17                     struct _Unwind_Exception* unwind_exception, struct _Unwind_Context* context)
18{
19        //DEBUG
20        printf("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
21
22        //If we've reached the end of the stack then there is nothing much we can do...
23        if( actions & _UA_END_OF_STACK ) return _URC_END_OF_STACK;
24       
25        //DEBUG
26        if (actions & _UA_SEARCH_PHASE) {
27                printf(" lookup phase");
28        } 
29        //DEBUG
30        else if (actions & _UA_CLEANUP_PHASE) {
31                printf(" cleanup phase");
32        }
33        //Just in case, probably can't actually happen
34        else {
35                printf(" error\n");
36                return _URC_FATAL_PHASE1_ERROR;
37        }
38       
39        //Get a pointer to the language specific data from which we will read what we need
40        const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( context );
41
42        if( !lsd ) {    //Nothing to do, keep unwinding
43                printf(" no LSD");
44                goto UNWIND;
45        }
46
47        //Get the instuction pointer and a reading pointer into the exception table
48        lsda_header_info lsd_info;
49        const unsigned char * cur_ptr = parse_lsda_header( context, lsd, &lsd_info);
50        _Unwind_Ptr instruction_ptr = _Unwind_GetIP( context );
51
52        //Linearly search the table for stuff to do
53        while( cur_ptr < lsd_info.action_table ) {
54                _Unwind_Ptr callsite_start;
55                _Unwind_Ptr callsite_len;
56                _Unwind_Ptr callsite_landing_pad;
57                _uleb128_t  callsite_action;
58
59                //Decode the common stuff we have in here
60                cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_start);
61                cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_len);
62                cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad);
63                cur_ptr = read_uleb128 (cur_ptr, &callsite_action);
64
65                //Have we reach the correct frame info yet?
66                if( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) {
67                        //DEBUG BEGIN
68                        void * ls = (void*)lsd_info.Start;
69                        void * cs = (void*)callsite_start;
70                        void * cl = (void*)callsite_len;
71                        void * bp = (void*)lsd_info.Start + callsite_start;
72                        void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
73                        void * ip = (void*)instruction_ptr;
74                        printf("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
75                        //DEBUG END
76                        continue;
77                }
78               
79                //Have we gone too far
80                if( lsd_info.Start + callsite_start > instruction_ptr ) {
81                        printf(" gone too far");
82                        break;
83                }
84
85                //Something to do?
86                if( callsite_landing_pad ) {
87                        //Which phase are we in
88                        if (actions & _UA_SEARCH_PHASE) {
89                                //Search phase, this means we probably found a potential handler and must check if it is a match
90
91                                //If we have arbitrarily decided that 0 means nothing to do and 1 means there is a potential handler
92                                //This doesn't seem to conflict the gcc default behavior
93                                if (callsite_action != 0) {
94                                        //Now we want to run some code to see if the handler matches
95                                        //This is the tricky part where we want to the power to run arbitrary code
96                                        //However, generating a new exception table entry and try routine every time
97                                        //is way more expansive than we might like
98                                        //The information we have is :
99                                        //  - The GR (???)
100                                        //  - The instruction pointer
101                                        //  - The instruction pointer info (???)
102                                        //  - The CFA (???)
103                                        //  - The BSP (Probably the base stack pointer)
104
105
106                                        //The current apprach uses one exception table entry per try block
107                                        _uleb128_t imatcher;
108                                        //Get the relative offset to the
109                                        cur_ptr = read_uleb128 (cur_ptr, &imatcher);
110
111                                        //Get a function pointer from the relative offset and call it
112                                        _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;                                     
113                                        _Unwind_Reason_Code ret = matcher();
114
115                                        //Based on the return value, check if we matched the exception
116                                        if( ret == _URC_HANDLER_FOUND) printf(" handler found\n");
117                                        else printf(" no handler\n");
118                                        return ret;
119                                }
120
121                                //This is only a cleanup handler, ignore it
122                                printf(" no action");
123                        } 
124                        else if (actions & _UA_CLEANUP_PHASE) {
125
126                                if( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){
127                                        //If this is a potential exception handler
128                                        //but not the one that matched the exception in the seach phase,
129                                        //just ignore it
130                                        goto UNWIND;
131                                }
132
133                                //We need to run some clean-up or a handler
134                                //These statment do the right thing but I don't know any specifics at all
135                                _Unwind_SetGR( context, __builtin_eh_return_data_regno(0), (_Unwind_Ptr) unwind_exception );
136                                _Unwind_SetGR( context, __builtin_eh_return_data_regno(1), 0 );
137
138                                //I assume this sets the instruction pointer to the adress of the landing pad
139                                //It doesn't actually set it, it only state the value that needs to be set once we return _URC_INSTALL_CONTEXT
140                                _Unwind_SetIP( context, lsd_info.LPStart + callsite_landing_pad );
141
142                                //DEBUG
143                                printf(" action\n");
144
145                                //Return have some action to run
146                                return _URC_INSTALL_CONTEXT;
147                        }
148                }
149
150                //Nothing to do, move along
151                printf(" no landing pad");
152        }
153        //No handling found
154        printf(" table end reached\n");
155
156        //DEBUG
157        UNWIND:
158        printf(" unwind\n");
159
160        //Keep unwinding the stack
161        return _URC_CONTINUE_UNWIND;
162}
163
164//We need a piece of storage to raise the exception
165struct _Unwind_Exception this_exception_storage;
166
167//Function needed by force unwind
168//It basically says to unwind the whole stack and then exit when we reach the end of the stack
169static _Unwind_Reason_Code _Stop_Fn(   
170        int version, 
171        _Unwind_Action actions, 
172        _Unwind_Exception_Class exceptionClass, 
173        struct _Unwind_Exception * unwind_exception, 
174        struct _Unwind_Context * context, 
175        void * some_param
176) {
177        if( actions & _UA_END_OF_STACK  ) exit(1);
178        if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
179
180        return _URC_FATAL_PHASE2_ERROR;
181}
182
183//Example throw routine
184void throw( int val ) {
185        //Store the current exception
186        this_exception = val;
187
188        //DEBUG
189        printf("Throwing exception %d\n", this_exception);
190
191        //Call stdlibc to raise the exception
192        _Unwind_Reason_Code ret = _Unwind_RaiseException( &this_exception_storage );
193
194        //If we reach here it means something happened
195        //For resumption to work we need to find a way to return back to here
196        //Most of them will probably boil down to setting a global flag and making the phase 1 either stop or fail.
197        //Causing an error on purpose may help avoiding unnecessary work but it might have some weird side effects.
198        //If we just pretend no handler was found that would work but may be expensive for no reason since we will always
199        //search the whole stack
200
201        if( ret == _URC_END_OF_STACK ) {
202                //No proper handler was found
203                //This can be handled in several way
204                //C++ calls std::terminate
205                //Here we force unwind the stack, basically raising a cancellation
206                printf("Uncaught exception %p\n", &this_exception_storage);
207               
208                ret = _Unwind_ForcedUnwind( &this_exception_storage, _Stop_Fn, (void*)0x22 );
209                printf("UNWIND ERROR %d after force unwind\n", ret);
210                abort();
211        }
212
213        //We did not simply reach the end of the stack without finding a handler,
214        //Something wen't wrong
215        printf("UNWIND ERROR %d after raise exception\n", ret);
216        abort();
217}
Note: See TracBrowser for help on using the repository browser.