[9d944b2] | 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 | // interpose.c --
|
---|
| 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Wed Mar 29 16:10:31 2017
|
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[a20be49] | 12 | // Last Modified On : Thu Jan 5 22:23:57 2023
|
---|
| 13 | // Update Count : 180
|
---|
[9d944b2] | 14 | //
|
---|
| 15 |
|
---|
[7867eb9] | 16 | #include <stdarg.h> // va_start, va_end
|
---|
[851fd92] | 17 | #include <stdio.h>
|
---|
[7867eb9] | 18 | #include <string.h> // strlen
|
---|
| 19 | #include <unistd.h> // _exit, getpid
|
---|
[58b6d1b] | 20 | #include <signal.h>
|
---|
[67db067] | 21 | extern "C" {
|
---|
[7867eb9] | 22 | #include <dlfcn.h> // dlopen, dlsym
|
---|
| 23 | #include <execinfo.h> // backtrace, messages
|
---|
[9d944b2] | 24 | }
|
---|
| 25 |
|
---|
[73abe95] | 26 | #include "bits/debug.hfa"
|
---|
| 27 | #include "bits/defs.hfa"
|
---|
| 28 | #include "bits/signal.hfa" // sigHandler_?
|
---|
| 29 | #include "startup.hfa" // STARTUP_PRIORITY_CORE
|
---|
[1d94116] | 30 | #include <assert.h>
|
---|
[9d944b2] | 31 |
|
---|
[3d5f2ef1] | 32 | //=============================================================================================
|
---|
| 33 | // Interposing helpers
|
---|
| 34 | //=============================================================================================
|
---|
| 35 |
|
---|
[032234bd] | 36 | static void preload_libgcc(void) {
|
---|
[ad64520] | 37 | dlopen( "libgcc_s.so.1", RTLD_NOW );
|
---|
| 38 | if ( const char * error = dlerror() ) abort( "interpose_symbol : internal error pre-loading libgcc, %s\n", error );
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[67db067] | 41 | typedef void (* generic_fptr_t)(void);
|
---|
[95dab9e] | 42 | static generic_fptr_t do_interpose_symbol( void * library, const char symbol[], const char version[] ) {
|
---|
| 43 | const char * error;
|
---|
| 44 |
|
---|
| 45 | union { generic_fptr_t fptr; void * ptr; } originalFunc;
|
---|
| 46 |
|
---|
| 47 | #if defined( _GNU_SOURCE )
|
---|
| 48 | if ( version ) {
|
---|
| 49 | originalFunc.ptr = dlvsym( library, symbol, version );
|
---|
| 50 | } else {
|
---|
| 51 | originalFunc.ptr = dlsym( library, symbol );
|
---|
| 52 | }
|
---|
| 53 | #else
|
---|
| 54 | originalFunc.ptr = dlsym( library, symbol );
|
---|
| 55 | #endif // _GNU_SOURCE
|
---|
| 56 |
|
---|
| 57 | error = dlerror();
|
---|
| 58 | if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
|
---|
| 59 |
|
---|
| 60 | return originalFunc.fptr;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[032234bd] | 63 | static generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
|
---|
[9d944b2] | 64 | const char * error;
|
---|
| 65 |
|
---|
| 66 | static void * library;
|
---|
[a7d696f] | 67 | static void * pthread_library;
|
---|
[9d944b2] | 68 | if ( ! library ) {
|
---|
| 69 | #if defined( RTLD_NEXT )
|
---|
| 70 | library = RTLD_NEXT;
|
---|
| 71 | #else
|
---|
| 72 | // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
|
---|
| 73 | library = dlopen( "libc.so.6", RTLD_LAZY );
|
---|
| 74 | error = dlerror();
|
---|
| 75 | if ( error ) {
|
---|
[169d944] | 76 | abort( "interpose_symbol : failed to open libc, %s\n", error );
|
---|
[9d944b2] | 77 | }
|
---|
| 78 | #endif
|
---|
| 79 | } // if
|
---|
[a7d696f] | 80 | if ( ! pthread_library ) {
|
---|
| 81 | #if defined( RTLD_NEXT )
|
---|
| 82 | pthread_library = RTLD_NEXT;
|
---|
| 83 | #else
|
---|
| 84 | // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
|
---|
| 85 | pthread_library = dlopen( "libpthread.so", RTLD_LAZY );
|
---|
| 86 | error = dlerror();
|
---|
| 87 | if ( error ) {
|
---|
| 88 | abort( "interpose_symbol : failed to open libpthread, %s\n", error );
|
---|
| 89 | }
|
---|
| 90 | #endif
|
---|
| 91 | } // if
|
---|
[9d944b2] | 92 |
|
---|
[95dab9e] | 93 | return do_interpose_symbol(library, symbol, version);
|
---|
[9d944b2] | 94 | }
|
---|
| 95 |
|
---|
[67db067] | 96 | #define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver )
|
---|
[3d5f2ef1] | 97 |
|
---|
| 98 | //=============================================================================================
|
---|
[67db067] | 99 | // Interposition Startup logic
|
---|
[3d5f2ef1] | 100 | //=============================================================================================
|
---|
[9d944b2] | 101 |
|
---|
[032234bd] | 102 | static void sigHandler_segv( __CFA_SIGPARMS__ );
|
---|
| 103 | static void sigHandler_ill ( __CFA_SIGPARMS__ );
|
---|
| 104 | static void sigHandler_fpe ( __CFA_SIGPARMS__ );
|
---|
| 105 | static void sigHandler_abrt( __CFA_SIGPARMS__ );
|
---|
| 106 | static void sigHandler_term( __CFA_SIGPARMS__ );
|
---|
[dbe9b08] | 107 |
|
---|
[032234bd] | 108 | static struct {
|
---|
[d7312ac] | 109 | void (* exit)( int ) __attribute__(( __noreturn__ ));
|
---|
| 110 | void (* abort)( void ) __attribute__(( __noreturn__ ));
|
---|
[3d5f2ef1] | 111 | } __cabi_libc;
|
---|
| 112 |
|
---|
[032234bd] | 113 | libcfa_public int cfa_main_returned;
|
---|
[7dd98b6] | 114 |
|
---|
[6bfe5cc] | 115 | extern "C" {
|
---|
[95dab9e] | 116 | void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) __attribute__((weak));
|
---|
[6bfe5cc] | 117 | void __cfaabi_interpose_startup( void ) {
|
---|
[8a13c47] | 118 | const char *version = 0p;
|
---|
[7dd98b6] | 119 | cfa_main_returned = 0;
|
---|
[9d944b2] | 120 |
|
---|
[ad64520] | 121 | preload_libgcc();
|
---|
| 122 |
|
---|
[d7312ac] | 123 | #pragma GCC diagnostic push
|
---|
| 124 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
|
---|
[3d5f2ef1] | 125 | INTERPOSE_LIBC( abort, version );
|
---|
| 126 | INTERPOSE_LIBC( exit , version );
|
---|
[d7312ac] | 127 | #pragma GCC diagnostic pop
|
---|
[dbe9b08] | 128 |
|
---|
[95dab9e] | 129 | if(__cfathreadabi_interpose_startup) __cfathreadabi_interpose_startup( do_interpose_symbol );
|
---|
| 130 |
|
---|
[8a13c47] | 131 | // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because
|
---|
| 132 | // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean
|
---|
| 133 | // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only)
|
---|
| 134 | // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort
|
---|
| 135 | // print a stack trace that uses substantial stack space.
|
---|
| 136 |
|
---|
| 137 | #define MINSTKSZ SIGSTKSZ * 8
|
---|
| 138 | static char stack[MINSTKSZ] __attribute__(( aligned (16) ));
|
---|
| 139 | static stack_t ss;
|
---|
| 140 |
|
---|
| 141 | ss.ss_sp = stack;
|
---|
| 142 | ss.ss_size = MINSTKSZ;
|
---|
| 143 | ss.ss_flags = 0;
|
---|
| 144 | if ( sigaltstack( &ss, 0p ) == -1 ) {
|
---|
| 145 | abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) );
|
---|
| 146 | } // if
|
---|
| 147 |
|
---|
[de94a60] | 148 | // Failure handler
|
---|
[92bfda0] | 149 | // internal errors
|
---|
| 150 | __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Invalid memory reference (default: Core)
|
---|
| 151 | __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Bus error, bad memory access (default: Core)
|
---|
| 152 | __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK ); // Illegal Instruction (default: Core)
|
---|
| 153 | __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK ); // Floating-point exception (default: Core)
|
---|
| 154 |
|
---|
| 155 | // handlers to outside errors
|
---|
| 156 | // reset in-case they insist and send it over and over
|
---|
| 157 | __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Termination signal (default: Term)
|
---|
| 158 | __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Interrupt from keyboard (default: Term)
|
---|
| 159 | __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Hangup detected on controlling terminal or death of controlling process (default: Term)
|
---|
| 160 | __cfaabi_sigaction( SIGQUIT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Quit from keyboard (default: Core)
|
---|
| 161 | __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Abort signal from abort(3) (default: Core)
|
---|
[6bfe5cc] | 162 | }
|
---|
[9d944b2] | 163 | }
|
---|
| 164 |
|
---|
[dbe9b08] | 165 | //=============================================================================================
|
---|
| 166 | // Terminating Signals logic
|
---|
| 167 | //=============================================================================================
|
---|
| 168 |
|
---|
[3d5f2ef1] | 169 | // Forward declare abort after the __typeof__ call to avoid ambiguities
|
---|
[032234bd] | 170 | libcfa_public void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
|
---|
| 171 | libcfa_public void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
|
---|
| 172 | libcfa_public void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
|
---|
| 173 | libcfa_public void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ ));
|
---|
[3d5f2ef1] | 174 |
|
---|
[9d944b2] | 175 | extern "C" {
|
---|
[032234bd] | 176 | libcfa_public void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
|
---|
[1d94116] | 177 | abort( false, "%s", "" );
|
---|
[9d944b2] | 178 | }
|
---|
| 179 |
|
---|
[032234bd] | 180 | libcfa_public void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
|
---|
[3d5f2ef1] | 181 | va_list argp;
|
---|
| 182 | va_start( argp, fmt );
|
---|
[d870df3] | 183 | __abort( false, fmt, argp );
|
---|
[3d5f2ef1] | 184 | va_end( argp );
|
---|
[9d944b2] | 185 | }
|
---|
| 186 |
|
---|
[032234bd] | 187 | libcfa_public void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
|
---|
[169d944] | 188 | __cabi_libc.exit( status );
|
---|
[3d5f2ef1] | 189 | }
|
---|
[9d944b2] | 190 | }
|
---|
| 191 |
|
---|
[92bfda0] | 192 | // See concurrency/kernel.cfa and concurrency/preemption.cfa for strong definition used in multi-processor mode.
|
---|
| 193 | void __kernel_abort_lock( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
|
---|
| 194 | void __kernel_abort_msg( char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
|
---|
| 195 | int __kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
|
---|
[9d944b2] | 196 |
|
---|
| 197 | enum { abort_text_size = 1024 };
|
---|
| 198 | static char abort_text[ abort_text_size ];
|
---|
| 199 |
|
---|
[8a13c47] | 200 | static void __cfaabi_backtrace( int start ) {
|
---|
[74330e7] | 201 | enum { Frames = 50, }; // maximum number of stack frames
|
---|
[92bfda0] | 202 | int last = __kernel_abort_lastframe(); // skip last N stack frames
|
---|
[dbe9b08] | 203 |
|
---|
| 204 | void * array[Frames];
|
---|
[6bfe5cc] | 205 | size_t size = backtrace( array, Frames );
|
---|
[74330e7] | 206 | char ** messages = backtrace_symbols( array, size ); // does not demangle names
|
---|
[dbe9b08] | 207 |
|
---|
[8a13c47] | 208 | *index( messages[0], '(' ) = '\0'; // find executable name
|
---|
[1c40091] | 209 | __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]);
|
---|
[dbe9b08] | 210 |
|
---|
[8a13c47] | 211 | for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) {
|
---|
[524627e] | 212 | char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
|
---|
[dbe9b08] | 213 |
|
---|
[74330e7] | 214 | for ( char * p = messages[i]; *p; p += 1 ) { // find parantheses and +offset
|
---|
[1c40091] | 215 | //__cfaabi_bits_print_nolock( "X %s\n", p);
|
---|
[dbe9b08] | 216 | if ( *p == '(' ) {
|
---|
| 217 | name = p;
|
---|
[4f37255] | 218 | } else if ( *p == '+' ) {
|
---|
[dbe9b08] | 219 | offset_begin = p;
|
---|
[4f37255] | 220 | } else if ( *p == ')' ) {
|
---|
[dbe9b08] | 221 | offset_end = p;
|
---|
| 222 | break;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[8a13c47] | 226 | // if line contains symbol, print it
|
---|
| 227 | int frameNo = i - start;
|
---|
[dbe9b08] | 228 | if ( name && offset_begin && offset_end && name < offset_begin ) {
|
---|
[8a13c47] | 229 | *name++ = '\0'; // delimit strings
|
---|
[dbe9b08] | 230 | *offset_begin++ = '\0';
|
---|
| 231 | *offset_end++ = '\0';
|
---|
| 232 |
|
---|
[1c40091] | 233 | __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
|
---|
[4f37255] | 234 | } else { // otherwise, print the whole line
|
---|
[1c40091] | 235 | __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] );
|
---|
[dbe9b08] | 236 | }
|
---|
| 237 | }
|
---|
| 238 | free( messages );
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[8a13c47] | 241 | void exit( int status, const char fmt[], ... ) {
|
---|
| 242 | va_list args;
|
---|
| 243 | va_start( args, fmt );
|
---|
| 244 | vfprintf( stderr, fmt, args );
|
---|
| 245 | va_end( args );
|
---|
| 246 | __cabi_libc.exit( status );
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[92bfda0] | 249 | static volatile bool __abort_first = 0;
|
---|
[6011658] | 250 |
|
---|
[d870df3] | 251 | // Cannot forward va_list.
|
---|
| 252 | void __abort( bool signalAbort, const char fmt[], va_list args ) {
|
---|
[92bfda0] | 253 | // Multiple threads can come here from multiple paths
|
---|
| 254 | // To make sure this is safe any concurrent/subsequent call to abort is redirected to libc-abort
|
---|
| 255 | bool first = ! __atomic_test_and_set( &__abort_first, __ATOMIC_SEQ_CST);
|
---|
[8a13c47] | 256 |
|
---|
[92bfda0] | 257 | // Prevent preemption from kicking-in and messing with the abort
|
---|
| 258 | __kernel_abort_lock();
|
---|
[8a13c47] | 259 |
|
---|
[92bfda0] | 260 | // first to abort ?
|
---|
| 261 | if ( !first ) {
|
---|
| 262 | // We aren't the first to abort just let C handle it
|
---|
| 263 | signal( SIGABRT, SIG_DFL ); // restore default in case we came here through the function.
|
---|
| 264 | __cabi_libc.abort();
|
---|
| 265 | }
|
---|
[8a13c47] | 266 |
|
---|
[92bfda0] | 267 | int len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
|
---|
| 268 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
[8a13c47] | 269 |
|
---|
[92bfda0] | 270 | // print the cause of the error
|
---|
| 271 | assert( fmt );
|
---|
| 272 | len = vsnprintf( abort_text, abort_text_size, fmt, args );
|
---|
| 273 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
[6011658] | 274 |
|
---|
[92bfda0] | 275 | // add optional newline if missing at the end of the format text
|
---|
| 276 | if ( fmt[strlen( fmt ) - 1] != '\n' ) {
|
---|
| 277 | __cfaabi_bits_write( STDERR_FILENO, "\n", 1 );
|
---|
| 278 | } // if
|
---|
[6011658] | 279 |
|
---|
[92bfda0] | 280 | // Give the kernel the chance to add some data in here
|
---|
| 281 | __kernel_abort_msg( abort_text, abort_text_size );
|
---|
[6011658] | 282 |
|
---|
[92bfda0] | 283 | // print stack trace in handler
|
---|
| 284 | __cfaabi_backtrace( signalAbort ? 4 : 2 );
|
---|
[6011658] | 285 |
|
---|
[92bfda0] | 286 | // Finally call abort
|
---|
| 287 | __cabi_libc.abort();
|
---|
[5ccee64] | 288 |
|
---|
[8a13c47] | 289 | }
|
---|
| 290 |
|
---|
| 291 | void abort( const char fmt[], ... ) {
|
---|
| 292 | va_list args;
|
---|
| 293 | va_start( args, fmt );
|
---|
[d870df3] | 294 | __abort( false, fmt, args );
|
---|
| 295 | // CONTROL NEVER REACHES HERE!
|
---|
[8a13c47] | 296 | va_end( args );
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[d870df3] | 299 | void abort( bool signalAbort, const char fmt[], ... ) {
|
---|
| 300 | va_list args;
|
---|
| 301 | va_start( args, fmt );
|
---|
| 302 | __abort( signalAbort, fmt, args );
|
---|
| 303 | // CONTROL NEVER REACHES HERE!
|
---|
| 304 | va_end( args );
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[dbe9b08] | 307 | void sigHandler_segv( __CFA_SIGPARMS__ ) {
|
---|
[8a13c47] | 308 | if ( sfp->si_addr == 0p ) {
|
---|
| 309 | abort( true, "Null pointer (0p) dereference.\n" );
|
---|
[1469a8a] | 310 | } else {
|
---|
[8a13c47] | 311 | abort( true, "%s at memory location %p.\n"
|
---|
[1469a8a] | 312 | "Possible cause is reading outside the address space or writing to a protected area within the address space with an invalid pointer or subscript.\n",
|
---|
| 313 | (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr );
|
---|
| 314 | }
|
---|
[2b8bc41] | 315 | }
|
---|
| 316 |
|
---|
| 317 | void sigHandler_ill( __CFA_SIGPARMS__ ) {
|
---|
[8a13c47] | 318 | abort( true, "Executing illegal instruction at location %p.\n"
|
---|
[2b8bc41] | 319 | "Possible cause is stack corruption.\n",
|
---|
| 320 | sfp->si_addr );
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | void sigHandler_fpe( __CFA_SIGPARMS__ ) {
|
---|
| 324 | const char * msg;
|
---|
| 325 |
|
---|
[a424315d] | 326 | choose( sfp->si_code ) {
|
---|
| 327 | case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
|
---|
| 328 | case FPE_FLTOVF: msg = "overflow";
|
---|
| 329 | case FPE_FLTUND: msg = "underflow";
|
---|
| 330 | case FPE_FLTRES: msg = "inexact result";
|
---|
| 331 | case FPE_FLTINV: msg = "invalid operation";
|
---|
[2b8bc41] | 332 | default: msg = "unknown";
|
---|
[a424315d] | 333 | } // choose
|
---|
[8a13c47] | 334 | abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr );
|
---|
[dbe9b08] | 335 | }
|
---|
| 336 |
|
---|
[94dea96] | 337 | void sigHandler_term( __CFA_SIGPARMS__ ) {
|
---|
[8a13c47] | 338 | abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) );
|
---|
[94dea96] | 339 | }
|
---|
| 340 |
|
---|
[6b0b624] | 341 | // Local Variables: //
|
---|
| 342 | // mode: c //
|
---|
| 343 | // tab-width: 4 //
|
---|
| 344 | // End: //
|
---|