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