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