1 | #include "exception.h"
|
---|
2 |
|
---|
3 | // Implementation of the secret header.
|
---|
4 |
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <unwind.h>
|
---|
8 |
|
---|
9 | #include "lsda.h"
|
---|
10 |
|
---|
11 | struct shared_stack_t shared_stack;
|
---|
12 |
|
---|
13 |
|
---|
14 | // This macro should be the only thing that needs to change across machines.
|
---|
15 | // Used in the personality function, way down in termination.
|
---|
16 | // struct _Unwind_Context * -> _Unwind_Reason_Code(*)()
|
---|
17 | #define MATCHER_FROM_CONTEXT(ptr_to_context) \
|
---|
18 | (*(_Unwind_Reason_Code(**)())(_Unwind_GetCFA(ptr_to_context) + 8))
|
---|
19 |
|
---|
20 |
|
---|
21 | // RESUMPTION ================================================================
|
---|
22 |
|
---|
23 | void __throw_resume(exception except) {
|
---|
24 | void noop() { printf("do nothing\n");}
|
---|
25 | struct __cleanup_hook __blarg __attribute__((cleanup(noop)));
|
---|
26 |
|
---|
27 | struct __try_resume_node * original_head = shared_stack.current_resume;
|
---|
28 | struct __try_resume_node * current =
|
---|
29 | (original_head) ? original_head->next : shared_stack.top_resume;
|
---|
30 |
|
---|
31 | for ( ; current ; current = current->next) {
|
---|
32 | shared_stack.current_resume = current;
|
---|
33 | if (current->try_to_handle(except)) {
|
---|
34 | shared_stack.current_resume = original_head;
|
---|
35 | return;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | printf("Unhandled exception %d\n", except);
|
---|
40 | shared_stack.current_resume = original_head;
|
---|
41 |
|
---|
42 | // Fall back to termination:
|
---|
43 | __throw_terminate(except);
|
---|
44 | // TODO: Default handler for resumption.
|
---|
45 | }
|
---|
46 |
|
---|
47 | /* __try_resume_node functions:
|
---|
48 | * Currently I was planning to generate this code inline, and even if I don't
|
---|
49 | * putting it in the header so it can be inlined might be worth while.
|
---|
50 | * And if we put them in Cforall code we can use the operators.
|
---|
51 | */
|
---|
52 | void __try_resume_node_new(struct __try_resume_node * node,
|
---|
53 | _Bool (*handler)(exception except)) {
|
---|
54 | node->next = shared_stack.top_resume;
|
---|
55 | shared_stack.top_resume = node;
|
---|
56 | node->try_to_handle = handler;
|
---|
57 | }
|
---|
58 |
|
---|
59 | void __try_resume_node_delete(struct __try_resume_node * node) {
|
---|
60 | shared_stack.top_resume = node->next;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | // TERMINATION ===============================================================
|
---|
65 |
|
---|
66 | // Requires -fexceptions to work.
|
---|
67 |
|
---|
68 | // Try statements are hoisted out see comments for details
|
---|
69 | // With this could probably be unique and simply linked from
|
---|
70 | // libcfa but there is one problem left, see the exception table
|
---|
71 | // for details
|
---|
72 | __attribute__((noinline))
|
---|
73 | void __try_terminate(void (*try_block)(),
|
---|
74 | void (*catch_block)(int index, exception except),
|
---|
75 | __attribute__((unused)) int (*match_block)(exception except)) {
|
---|
76 | //! volatile int xy = 0;
|
---|
77 | //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy);
|
---|
78 |
|
---|
79 | // Setup statments
|
---|
80 | // These 2 statments won't actually result in any code,
|
---|
81 | // they only setup global tables.
|
---|
82 | // However, they clobber gcc cancellation support from gcc.
|
---|
83 | // We can replace the personality routine but replacing the exception
|
---|
84 | // table gcc generates is not really doable, it generates labels based
|
---|
85 | // on how the assembly works.
|
---|
86 | // Setup the personality routine
|
---|
87 | asm volatile (".cfi_personality 0x3,__gcfa_personality_v0");
|
---|
88 | // Setup the exception table
|
---|
89 | asm volatile (".cfi_lsda 0x3, .LLSDACFA2");
|
---|
90 |
|
---|
91 | // Label which defines the start of the area for which the handler is setup
|
---|
92 | asm volatile (".TRYSTART:");
|
---|
93 |
|
---|
94 | // The actual statements of the try blocks
|
---|
95 | try_block();
|
---|
96 |
|
---|
97 | // asm statement to prevent deadcode removal
|
---|
98 | asm volatile goto ("" : : : : CATCH );
|
---|
99 |
|
---|
100 | // Normal return
|
---|
101 | return;
|
---|
102 |
|
---|
103 | // Exceptionnal path
|
---|
104 | CATCH : __attribute__(( unused ));
|
---|
105 | // Label which defines the end of the area for which the handler is setup
|
---|
106 | asm volatile (".TRYEND:");
|
---|
107 | // Label which defines the start of the exception landing pad
|
---|
108 | // basically what will be called when the exception is caught
|
---|
109 | // Note, if multiple handlers are given, the multiplexing should be done
|
---|
110 | // by the generated code, not the exception runtime
|
---|
111 | asm volatile (".CATCH:");
|
---|
112 |
|
---|
113 | // Exception handler
|
---|
114 | catch_block(shared_stack.current_handler_index,
|
---|
115 | shared_stack.current_exception);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Exception table data we need to generate
|
---|
119 | // While this is almost generic, the custom data refers to
|
---|
120 | // foo_try_match try match, which is no way generic
|
---|
121 | // Some more works need to be done if we want to have a single
|
---|
122 | // call to the try routine
|
---|
123 | asm (
|
---|
124 | //HEADER
|
---|
125 | ".LFECFA1:\n"
|
---|
126 | " .globl __gcfa_personality_v0\n"
|
---|
127 | " .section .gcc_except_table,\"a\",@progbits\n"
|
---|
128 | ".LLSDACFA2:\n" //TABLE header
|
---|
129 | " .byte 0xff\n"
|
---|
130 | " .byte 0xff\n"
|
---|
131 | " .byte 0x1\n"
|
---|
132 | " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n" // BODY length
|
---|
133 | // Body uses language specific data and therefore could be modified arbitrarily
|
---|
134 | ".LLSDACSBCFA2:\n" // BODY start
|
---|
135 | " .uleb128 .TRYSTART-__try_terminate\n" // Handled area start (relative to start of function)
|
---|
136 | " .uleb128 .TRYEND-.TRYSTART\n" // Handled area length
|
---|
137 | " .uleb128 .CATCH-__try_terminate\n" // Hanlder landing pad adress (relative to start of function)
|
---|
138 | " .uleb128 1\n" // Action code, gcc seems to use always 0
|
---|
139 | ".LLSDACSECFA2:\n" // BODY end
|
---|
140 | " .text\n" // TABLE footer
|
---|
141 | " .size __try_terminate, .-__try_terminate\n"
|
---|
142 | " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
|
---|
143 | " .section .note.GNU-stack,\"x\",@progbits\n"
|
---|
144 | );
|
---|
145 |
|
---|
146 | // Global which defines the current exception
|
---|
147 | // Currently an int just to make matching easier
|
---|
148 | int this_exception;
|
---|
149 |
|
---|
150 | // We need a piece of storage to raise the exception
|
---|
151 | struct _Unwind_Exception this_exception_storage;
|
---|
152 |
|
---|
153 | // Function needed by force unwind
|
---|
154 | // It basically says to unwind the whole stack and then exit when we reach the end of the stack
|
---|
155 | static _Unwind_Reason_Code _Stop_Fn(
|
---|
156 | int version,
|
---|
157 | _Unwind_Action actions,
|
---|
158 | _Unwind_Exception_Class exceptionClass,
|
---|
159 | struct _Unwind_Exception * unwind_exception,
|
---|
160 | struct _Unwind_Context * context,
|
---|
161 | void * some_param) {
|
---|
162 | if( actions & _UA_END_OF_STACK ) exit(1);
|
---|
163 | if( actions & _UA_CLEANUP_PHASE ) return _URC_NO_REASON;
|
---|
164 |
|
---|
165 | return _URC_FATAL_PHASE2_ERROR;
|
---|
166 | }
|
---|
167 |
|
---|
168 | // Example throw routine
|
---|
169 | void __throw_terminate( int val ) {
|
---|
170 | // Store the current exception
|
---|
171 | this_exception = val;
|
---|
172 |
|
---|
173 | // DEBUG
|
---|
174 | printf("Throwing exception %d\n", this_exception);
|
---|
175 |
|
---|
176 | // Call stdlibc to raise the exception
|
---|
177 | _Unwind_Reason_Code ret = _Unwind_RaiseException( &this_exception_storage );
|
---|
178 |
|
---|
179 | // If we reach here it means something happened
|
---|
180 | // For resumption to work we need to find a way to return back to here
|
---|
181 | // Most of them will probably boil down to setting a global flag and making the phase 1 either stop or fail.
|
---|
182 | // Causing an error on purpose may help avoiding unnecessary work but it might have some weird side effects.
|
---|
183 | // If we just pretend no handler was found that would work but may be expensive for no reason since we will always
|
---|
184 | // search the whole stack
|
---|
185 |
|
---|
186 | if( ret == _URC_END_OF_STACK ) {
|
---|
187 | // No proper handler was found
|
---|
188 | // This can be handled in several way
|
---|
189 | // C++ calls std::terminate
|
---|
190 | // Here we force unwind the stack, basically raising a cancellation
|
---|
191 | printf("Uncaught exception %p\n", &this_exception_storage);
|
---|
192 |
|
---|
193 | ret = _Unwind_ForcedUnwind( &this_exception_storage, _Stop_Fn, (void*)0x22 );
|
---|
194 | printf("UNWIND ERROR %d after force unwind\n", ret);
|
---|
195 | abort();
|
---|
196 | }
|
---|
197 |
|
---|
198 | // We did not simply reach the end of the stack without finding a handler,
|
---|
199 | // Something wen't wrong
|
---|
200 | printf("UNWIND ERROR %d after raise exception\n", ret);
|
---|
201 | abort();
|
---|
202 | }
|
---|
203 |
|
---|
204 | // This is our personality routine
|
---|
205 | // For every stack frame anotated with ".cfi_personality 0x3,__gcfa_personality_v0"
|
---|
206 | // This function will be called twice when unwinding
|
---|
207 | // Once in the search phased and once in the cleanup phase
|
---|
208 | _Unwind_Reason_Code __gcfa_personality_v0 (
|
---|
209 | int version, _Unwind_Action actions, unsigned long long exceptionClass,
|
---|
210 | struct _Unwind_Exception* unwind_exception,
|
---|
211 | struct _Unwind_Context* context)
|
---|
212 | {
|
---|
213 | printf("CFA: 0x%lx\n", _Unwind_GetCFA(context));
|
---|
214 |
|
---|
215 | // DEBUG
|
---|
216 | printf("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
|
---|
217 |
|
---|
218 | // If we've reached the end of the stack then there is nothing much we can do...
|
---|
219 | if( actions & _UA_END_OF_STACK ) return _URC_END_OF_STACK;
|
---|
220 |
|
---|
221 | // DEBUG
|
---|
222 | if (actions & _UA_SEARCH_PHASE) {
|
---|
223 | printf(" lookup phase");
|
---|
224 | }
|
---|
225 | // DEBUG
|
---|
226 | else if (actions & _UA_CLEANUP_PHASE) {
|
---|
227 | printf(" cleanup phase");
|
---|
228 | }
|
---|
229 | // Just in case, probably can't actually happen
|
---|
230 | else {
|
---|
231 | printf(" error\n");
|
---|
232 | return _URC_FATAL_PHASE1_ERROR;
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Get a pointer to the language specific data from which we will read what we need
|
---|
236 | const unsigned char * lsd = (const unsigned char*) _Unwind_GetLanguageSpecificData( context );
|
---|
237 |
|
---|
238 | if( !lsd ) { //Nothing to do, keep unwinding
|
---|
239 | printf(" no LSD");
|
---|
240 | goto UNWIND;
|
---|
241 | }
|
---|
242 |
|
---|
243 | // Get the instuction pointer and a reading pointer into the exception table
|
---|
244 | lsda_header_info lsd_info;
|
---|
245 | const unsigned char * cur_ptr = parse_lsda_header( context, lsd, &lsd_info);
|
---|
246 | _Unwind_Ptr instruction_ptr = _Unwind_GetIP( context );
|
---|
247 |
|
---|
248 | // Linearly search the table for stuff to do
|
---|
249 | while( cur_ptr < lsd_info.action_table ) {
|
---|
250 | _Unwind_Ptr callsite_start;
|
---|
251 | _Unwind_Ptr callsite_len;
|
---|
252 | _Unwind_Ptr callsite_landing_pad;
|
---|
253 | _uleb128_t callsite_action;
|
---|
254 |
|
---|
255 | // Decode the common stuff we have in here
|
---|
256 | cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_start);
|
---|
257 | cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_len);
|
---|
258 | cur_ptr = read_encoded_value (0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad);
|
---|
259 | cur_ptr = read_uleb128 (cur_ptr, &callsite_action);
|
---|
260 |
|
---|
261 | // Have we reach the correct frame info yet?
|
---|
262 | if( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) {
|
---|
263 | //DEBUG BEGIN
|
---|
264 | void * ls = (void*)lsd_info.Start;
|
---|
265 | void * cs = (void*)callsite_start;
|
---|
266 | void * cl = (void*)callsite_len;
|
---|
267 | void * bp = (void*)lsd_info.Start + callsite_start;
|
---|
268 | void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
|
---|
269 | void * ip = (void*)instruction_ptr;
|
---|
270 | printf("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
|
---|
271 | //DEBUG END
|
---|
272 | continue;
|
---|
273 | }
|
---|
274 |
|
---|
275 | // Have we gone too far
|
---|
276 | if( lsd_info.Start + callsite_start > instruction_ptr ) {
|
---|
277 | printf(" gone too far");
|
---|
278 | break;
|
---|
279 | }
|
---|
280 |
|
---|
281 | // Something to do?
|
---|
282 | if( callsite_landing_pad ) {
|
---|
283 | // Which phase are we in
|
---|
284 | if (actions & _UA_SEARCH_PHASE) {
|
---|
285 | // Search phase, this means we probably found a potential handler and must check if it is a match
|
---|
286 |
|
---|
287 | // If we have arbitrarily decided that 0 means nothing to do and 1 means there is a potential handler
|
---|
288 | // This doesn't seem to conflict the gcc default behavior
|
---|
289 | if (callsite_action != 0) {
|
---|
290 | // Now we want to run some code to see if the handler matches
|
---|
291 | // This is the tricky part where we want to the power to run arbitrary code
|
---|
292 | // However, generating a new exception table entry and try routine every time
|
---|
293 | // is way more expansive than we might like
|
---|
294 | // The information we have is :
|
---|
295 | // - The GR (Series of registers)
|
---|
296 | // GR1=GP Global Pointer of frame ref by context
|
---|
297 | // - The instruction pointer
|
---|
298 | // - The instruction pointer info (???)
|
---|
299 | // - The CFA (Canonical Frame Address)
|
---|
300 | // - The BSP (Probably the base stack pointer)
|
---|
301 |
|
---|
302 |
|
---|
303 | // The current apprach uses one exception table entry per try block
|
---|
304 | _uleb128_t imatcher;
|
---|
305 | // Get the relative offset to the
|
---|
306 | cur_ptr = read_uleb128 (cur_ptr, &imatcher);
|
---|
307 |
|
---|
308 | // Get a function pointer from the relative offset and call it
|
---|
309 | // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;
|
---|
310 |
|
---|
311 | _Unwind_Reason_Code (*matcher)() =
|
---|
312 | MATCHER_FROM_CONTEXT(context);
|
---|
313 | int index = matcher(shared_stack.current_exception);
|
---|
314 | _Unwind_Reason_Code ret = (0 == index)
|
---|
315 | ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;
|
---|
316 | shared_stack.current_handler_index = index;
|
---|
317 |
|
---|
318 |
|
---|
319 | // Based on the return value, check if we matched the exception
|
---|
320 | if( ret == _URC_HANDLER_FOUND) printf(" handler found\n");
|
---|
321 | else printf(" no handler\n");
|
---|
322 | return ret;
|
---|
323 | }
|
---|
324 |
|
---|
325 | // This is only a cleanup handler, ignore it
|
---|
326 | printf(" no action");
|
---|
327 | }
|
---|
328 | else if (actions & _UA_CLEANUP_PHASE) {
|
---|
329 |
|
---|
330 | if( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){
|
---|
331 | // If this is a potential exception handler
|
---|
332 | // but not the one that matched the exception in the seach phase,
|
---|
333 | // just ignore it
|
---|
334 | goto UNWIND;
|
---|
335 | }
|
---|
336 |
|
---|
337 | // We need to run some clean-up or a handler
|
---|
338 | // These statment do the right thing but I don't know any specifics at all
|
---|
339 | _Unwind_SetGR( context, __builtin_eh_return_data_regno(0), (_Unwind_Ptr) unwind_exception );
|
---|
340 | _Unwind_SetGR( context, __builtin_eh_return_data_regno(1), 0 );
|
---|
341 |
|
---|
342 | // I assume this sets the instruction pointer to the adress of the landing pad
|
---|
343 | // It doesn't actually set it, it only state the value that needs to be set once we return _URC_INSTALL_CONTEXT
|
---|
344 | _Unwind_SetIP( context, lsd_info.LPStart + callsite_landing_pad );
|
---|
345 |
|
---|
346 | // DEBUG
|
---|
347 | printf(" action\n");
|
---|
348 |
|
---|
349 | // Return have some action to run
|
---|
350 | return _URC_INSTALL_CONTEXT;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | // Nothing to do, move along
|
---|
355 | printf(" no landing pad");
|
---|
356 | }
|
---|
357 | // No handling found
|
---|
358 | printf(" table end reached\n");
|
---|
359 |
|
---|
360 | // DEBUG
|
---|
361 | UNWIND:
|
---|
362 | printf(" unwind\n");
|
---|
363 |
|
---|
364 | // Keep unwinding the stack
|
---|
365 | return _URC_CONTINUE_UNWIND;
|
---|
366 | }
|
---|