| 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
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Mon Feb 17 10:18:53 2020
 | 
|---|
| 13 | // Update Count     : 166
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <stdarg.h>                                                                             // va_start, va_end
 | 
|---|
| 17 | #include <string.h>                                                                             // strlen
 | 
|---|
| 18 | #include <unistd.h>                                                                             // _exit, getpid
 | 
|---|
| 19 | #define __USE_GNU
 | 
|---|
| 20 | #include <signal.h>
 | 
|---|
| 21 | #undef __USE_GNU
 | 
|---|
| 22 | extern "C" {
 | 
|---|
| 23 | #include <dlfcn.h>                                                                              // dlopen, dlsym
 | 
|---|
| 24 | #include <execinfo.h>                                                                   // backtrace, messages
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "bits/debug.hfa"
 | 
|---|
| 28 | #include "bits/defs.hfa"
 | 
|---|
| 29 | #include "bits/signal.hfa"                                                              // sigHandler_?
 | 
|---|
| 30 | #include "startup.hfa"                                                                  // STARTUP_PRIORITY_CORE
 | 
|---|
| 31 | #include <assert.h>
 | 
|---|
| 32 | 
 | 
|---|
| 33 | //=============================================================================================
 | 
|---|
| 34 | // Interposing helpers
 | 
|---|
| 35 | //=============================================================================================
 | 
|---|
| 36 | 
 | 
|---|
| 37 | void preload_libgcc(void) {
 | 
|---|
| 38 |         dlopen( "libgcc_s.so.1", RTLD_NOW );
 | 
|---|
| 39 |         if ( const char * error = dlerror() ) abort( "interpose_symbol : internal error pre-loading libgcc, %s\n", error );
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | typedef void (* generic_fptr_t)(void);
 | 
|---|
| 43 | generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
 | 
|---|
| 44 |         const char * error;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |         static void * library;
 | 
|---|
| 47 |         if ( ! library ) {
 | 
|---|
| 48 |                 #if defined( RTLD_NEXT )
 | 
|---|
| 49 |                         library = RTLD_NEXT;
 | 
|---|
| 50 |                 #else
 | 
|---|
| 51 |                         // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
 | 
|---|
| 52 |                         library = dlopen( "libc.so.6", RTLD_LAZY );
 | 
|---|
| 53 |                         error = dlerror();
 | 
|---|
| 54 |                         if ( error ) {
 | 
|---|
| 55 |                                 abort( "interpose_symbol : failed to open libc, %s\n", error );
 | 
|---|
| 56 |                         }
 | 
|---|
| 57 |                 #endif
 | 
|---|
| 58 |         } // if
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         union { generic_fptr_t fptr; void * ptr; } originalFunc;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         #if defined( _GNU_SOURCE )
 | 
|---|
| 63 |                 if ( version ) {
 | 
|---|
| 64 |                         originalFunc.ptr = dlvsym( library, symbol, version );
 | 
|---|
| 65 |                 } else {
 | 
|---|
| 66 |                         originalFunc.ptr = dlsym( library, symbol );
 | 
|---|
| 67 |                 }
 | 
|---|
| 68 |         #else
 | 
|---|
| 69 |                 originalFunc.ptr = dlsym( library, symbol );
 | 
|---|
| 70 |         #endif // _GNU_SOURCE
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         error = dlerror();
 | 
|---|
| 73 |         if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         return originalFunc.fptr;
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | #define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver )
 | 
|---|
| 79 | 
 | 
|---|
| 80 | //=============================================================================================
 | 
|---|
| 81 | // Interposition Startup logic
 | 
|---|
| 82 | //=============================================================================================
 | 
|---|
| 83 | 
 | 
|---|
| 84 | void sigHandler_segv( __CFA_SIGPARMS__ );
 | 
|---|
| 85 | void sigHandler_ill ( __CFA_SIGPARMS__ );
 | 
|---|
| 86 | void sigHandler_fpe ( __CFA_SIGPARMS__ );
 | 
|---|
| 87 | void sigHandler_abrt( __CFA_SIGPARMS__ );
 | 
|---|
| 88 | void sigHandler_term( __CFA_SIGPARMS__ );
 | 
|---|
| 89 | 
 | 
|---|
| 90 | struct {
 | 
|---|
| 91 |         void (* exit)( int ) __attribute__(( __noreturn__ ));
 | 
|---|
| 92 |         void (* abort)( void ) __attribute__(( __noreturn__ ));
 | 
|---|
| 93 | } __cabi_libc;
 | 
|---|
| 94 | 
 | 
|---|
| 95 | extern "C" {
 | 
|---|
| 96 |         void __cfaabi_interpose_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
 | 
|---|
| 97 |         void __cfaabi_interpose_startup( void ) {
 | 
|---|
| 98 |                 const char *version = 0p;
 | 
|---|
| 99 | 
 | 
|---|
| 100 |                 preload_libgcc();
 | 
|---|
| 101 | 
 | 
|---|
| 102 | #pragma GCC diagnostic push
 | 
|---|
| 103 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
 | 
|---|
| 104 |                 INTERPOSE_LIBC( abort, version );
 | 
|---|
| 105 |                 INTERPOSE_LIBC( exit , version );
 | 
|---|
| 106 | #pragma GCC diagnostic pop
 | 
|---|
| 107 | 
 | 
|---|
| 108 |                 // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because
 | 
|---|
| 109 |                 // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean
 | 
|---|
| 110 |                 // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only)
 | 
|---|
| 111 |                 // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort
 | 
|---|
| 112 |                 // print a stack trace that uses substantial stack space.
 | 
|---|
| 113 | 
 | 
|---|
| 114 |                 #define MINSTKSZ SIGSTKSZ * 8
 | 
|---|
| 115 |                 static char stack[MINSTKSZ] __attribute__(( aligned (16) ));
 | 
|---|
| 116 |                 static stack_t ss;
 | 
|---|
| 117 | 
 | 
|---|
| 118 |                 ss.ss_sp = stack;
 | 
|---|
| 119 |                 ss.ss_size = MINSTKSZ;
 | 
|---|
| 120 |                 ss.ss_flags = 0;
 | 
|---|
| 121 |                 if ( sigaltstack( &ss, 0p ) == -1 ) {
 | 
|---|
| 122 |                         abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) );
 | 
|---|
| 123 |                 } // if
 | 
|---|
| 124 | 
 | 
|---|
| 125 |                 // Failure handler
 | 
|---|
| 126 |                 __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK );
 | 
|---|
| 127 |                 __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK );
 | 
|---|
| 128 |                 __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK );
 | 
|---|
| 129 |                 __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK );
 | 
|---|
| 130 |                 __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // one shot handler, return to default
 | 
|---|
| 131 |                 __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND );
 | 
|---|
| 132 |                 __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND );
 | 
|---|
| 133 |                 __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // terminal hangup
 | 
|---|
| 134 |         }
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | //=============================================================================================
 | 
|---|
| 138 | // Terminating Signals logic
 | 
|---|
| 139 | //=============================================================================================
 | 
|---|
| 140 | 
 | 
|---|
| 141 | // Forward declare abort after the __typeof__ call to avoid ambiguities
 | 
|---|
| 142 | void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
 | 
|---|
| 143 | void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
 | 
|---|
| 144 | void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
 | 
|---|
| 145 | 
 | 
|---|
| 146 | extern "C" {
 | 
|---|
| 147 |         void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 148 |                 abort( false, "%s", "" );
 | 
|---|
| 149 |         }
 | 
|---|
| 150 | 
 | 
|---|
| 151 |         void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 152 |                 va_list argp;
 | 
|---|
| 153 |                 va_start( argp, fmt );
 | 
|---|
| 154 |                 abort( false, fmt, argp );
 | 
|---|
| 155 |                 va_end( argp );
 | 
|---|
| 156 |         }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 159 |                 __cabi_libc.exit( status );
 | 
|---|
| 160 |         }
 | 
|---|
| 161 | }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | void * kernel_abort( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 0p; }
 | 
|---|
| 164 | void kernel_abort_msg( void * data, char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
 | 
|---|
| 165 | // See concurrency/kernel.cfa for strong definition used in multi-processor mode.
 | 
|---|
| 166 | int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | enum { abort_text_size = 1024 };
 | 
|---|
| 169 | static char abort_text[ abort_text_size ];
 | 
|---|
| 170 | 
 | 
|---|
| 171 | static void __cfaabi_backtrace( int start ) {
 | 
|---|
| 172 |         enum { Frames = 50, };                                                          // maximum number of stack frames
 | 
|---|
| 173 |         int last = kernel_abort_lastframe();                            // skip last N stack frames
 | 
|---|
| 174 | 
 | 
|---|
| 175 |         void * array[Frames];
 | 
|---|
| 176 |         size_t size = backtrace( array, Frames );
 | 
|---|
| 177 |         char ** messages = backtrace_symbols( array, size ); // does not demangle names
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         *index( messages[0], '(' ) = '\0';                                      // find executable name
 | 
|---|
| 180 |         __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]);
 | 
|---|
| 181 | 
 | 
|---|
| 182 |         for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) {
 | 
|---|
| 183 |                 char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
 | 
|---|
| 184 | 
 | 
|---|
| 185 |                 for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset
 | 
|---|
| 186 |                         //__cfaabi_bits_print_nolock( "X %s\n", p);
 | 
|---|
| 187 |                         if ( *p == '(' ) {
 | 
|---|
| 188 |                                 name = p;
 | 
|---|
| 189 |                         } else if ( *p == '+' ) {
 | 
|---|
| 190 |                                 offset_begin = p;
 | 
|---|
| 191 |                         } else if ( *p == ')' ) {
 | 
|---|
| 192 |                                 offset_end = p;
 | 
|---|
| 193 |                                 break;
 | 
|---|
| 194 |                         }
 | 
|---|
| 195 |                 }
 | 
|---|
| 196 | 
 | 
|---|
| 197 |                 // if line contains symbol, print it
 | 
|---|
| 198 |                 int frameNo = i - start;
 | 
|---|
| 199 |                 if ( name && offset_begin && offset_end && name < offset_begin ) {
 | 
|---|
| 200 |                         *name++ = '\0';                                                         // delimit strings
 | 
|---|
| 201 |                         *offset_begin++ = '\0';
 | 
|---|
| 202 |                         *offset_end++ = '\0';
 | 
|---|
| 203 | 
 | 
|---|
| 204 |                         __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
 | 
|---|
| 205 |                 } else {                                                                                // otherwise, print the whole line
 | 
|---|
| 206 |                         __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] );
 | 
|---|
| 207 |                 }
 | 
|---|
| 208 |         }
 | 
|---|
| 209 |         free( messages );
 | 
|---|
| 210 | }
 | 
|---|
| 211 | 
 | 
|---|
| 212 | void exit( int status, const char fmt[], ... ) {
 | 
|---|
| 213 |         va_list args;
 | 
|---|
| 214 |         va_start( args, fmt );
 | 
|---|
| 215 |         vfprintf( stderr, fmt, args );
 | 
|---|
| 216 |         va_end( args );
 | 
|---|
| 217 |         __cabi_libc.exit( status );
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | void abort( bool signalAbort, const char fmt[], ... ) {
 | 
|---|
| 221 |         void * kernel_data = kernel_abort();                            // must be done here to lock down kernel
 | 
|---|
| 222 |         int len;
 | 
|---|
| 223 | 
 | 
|---|
| 224 |         signal( SIGABRT, SIG_DFL );                                                     // prevent final "real" abort from recursing to handler
 | 
|---|
| 225 | 
 | 
|---|
| 226 |         len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
 | 
|---|
| 227 |         __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| 228 | 
 | 
|---|
| 229 |         assert( fmt );
 | 
|---|
| 230 |         va_list args;
 | 
|---|
| 231 |         va_start( args, fmt );
 | 
|---|
| 232 | 
 | 
|---|
| 233 |         len = vsnprintf( abort_text, abort_text_size, fmt, args );
 | 
|---|
| 234 |         va_end( args );
 | 
|---|
| 235 |         __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| 236 | 
 | 
|---|
| 237 |         if ( fmt[strlen( fmt ) - 1] != '\n' ) {                         // add optional newline if missing at the end of the format text
 | 
|---|
| 238 |                 __cfaabi_dbg_write( "\n", 1 );
 | 
|---|
| 239 |         } // if
 | 
|---|
| 240 |         kernel_abort_msg( kernel_data, abort_text, abort_text_size );
 | 
|---|
| 241 | 
 | 
|---|
| 242 |         __cfaabi_backtrace( signalAbort ? 4 : 2 );
 | 
|---|
| 243 | 
 | 
|---|
| 244 |         __cabi_libc.abort();                                                            // print stack trace in handler
 | 
|---|
| 245 | }
 | 
|---|
| 246 | 
 | 
|---|
| 247 | void abort( const char fmt[], ... ) {
 | 
|---|
| 248 |         va_list args;
 | 
|---|
| 249 |         va_start( args, fmt );
 | 
|---|
| 250 |         abort( false, fmt, args );
 | 
|---|
| 251 |         va_end( args );
 | 
|---|
| 252 | }
 | 
|---|
| 253 | 
 | 
|---|
| 254 | void sigHandler_segv( __CFA_SIGPARMS__ ) {
 | 
|---|
| 255 |                 if ( sfp->si_addr == 0p ) {
 | 
|---|
| 256 |                         abort( true, "Null pointer (0p) dereference.\n" );
 | 
|---|
| 257 |                 } else {
 | 
|---|
| 258 |                         abort( true, "%s at memory location %p.\n"
 | 
|---|
| 259 |                                    "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",
 | 
|---|
| 260 |                                    (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr );
 | 
|---|
| 261 |                 }
 | 
|---|
| 262 | }
 | 
|---|
| 263 | 
 | 
|---|
| 264 | void sigHandler_ill( __CFA_SIGPARMS__ ) {
 | 
|---|
| 265 |         abort( true, "Executing illegal instruction at location %p.\n"
 | 
|---|
| 266 |                         "Possible cause is stack corruption.\n",
 | 
|---|
| 267 |                         sfp->si_addr );
 | 
|---|
| 268 | }
 | 
|---|
| 269 | 
 | 
|---|
| 270 | void sigHandler_fpe( __CFA_SIGPARMS__ ) {
 | 
|---|
| 271 |         const char * msg;
 | 
|---|
| 272 | 
 | 
|---|
| 273 |         choose( sfp->si_code ) {
 | 
|---|
| 274 |           case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
 | 
|---|
| 275 |           case FPE_FLTOVF: msg = "overflow";
 | 
|---|
| 276 |           case FPE_FLTUND: msg = "underflow";
 | 
|---|
| 277 |           case FPE_FLTRES: msg = "inexact result";
 | 
|---|
| 278 |           case FPE_FLTINV: msg = "invalid operation";
 | 
|---|
| 279 |           default: msg = "unknown";
 | 
|---|
| 280 |         } // choose
 | 
|---|
| 281 |         abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr );
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | void sigHandler_term( __CFA_SIGPARMS__ ) {
 | 
|---|
| 285 |         abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) );
 | 
|---|
| 286 | }
 | 
|---|
| 287 | 
 | 
|---|
| 288 | // Local Variables: //
 | 
|---|
| 289 | // mode: c //
 | 
|---|
| 290 | // tab-width: 4 //
 | 
|---|
| 291 | // End: //
 | 
|---|