| 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 : Sat May  5 11:37:35 2018
 | 
|---|
| 13 | // Update Count     : 111
 | 
|---|
| 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.h"
 | 
|---|
| 28 | #include "bits/defs.h"
 | 
|---|
| 29 | #include "bits/signal.h"                                                                // sigHandler_?
 | 
|---|
| 30 | #include "startup.h"                                                                    // STARTUP_PRIORITY_CORE
 | 
|---|
| 31 | 
 | 
|---|
| 32 | //=============================================================================================
 | 
|---|
| 33 | // Interposing helpers
 | 
|---|
| 34 | //=============================================================================================
 | 
|---|
| 35 | 
 | 
|---|
| 36 | void preload_libgcc(void) {
 | 
|---|
| 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 | 
 | 
|---|
| 41 | typedef void (* generic_fptr_t)(void);
 | 
|---|
| 42 | generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
 | 
|---|
| 43 |         const char * error;
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         static void * library;
 | 
|---|
| 46 |         if ( ! library ) {
 | 
|---|
| 47 |                 #if defined( RTLD_NEXT )
 | 
|---|
| 48 |                         library = RTLD_NEXT;
 | 
|---|
| 49 |                 #else
 | 
|---|
| 50 |                         // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
 | 
|---|
| 51 |                         library = dlopen( "libc.so.6", RTLD_LAZY );
 | 
|---|
| 52 |                         error = dlerror();
 | 
|---|
| 53 |                         if ( error ) {
 | 
|---|
| 54 |                                 abort( "interpose_symbol : failed to open libc, %s\n", error );
 | 
|---|
| 55 |                         }
 | 
|---|
| 56 |                 #endif
 | 
|---|
| 57 |         } // if
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         union { generic_fptr_t fptr; void * ptr; } originalFunc;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         #if defined( _GNU_SOURCE )
 | 
|---|
| 62 |                 if ( version ) {
 | 
|---|
| 63 |                         originalFunc.ptr = dlvsym( library, symbol, version );
 | 
|---|
| 64 |                 } else {
 | 
|---|
| 65 |                         originalFunc.ptr = dlsym( library, symbol );
 | 
|---|
| 66 |                 }
 | 
|---|
| 67 |         #else
 | 
|---|
| 68 |                 originalFunc.ptr = dlsym( library, symbol );
 | 
|---|
| 69 |         #endif // _GNU_SOURCE
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         error = dlerror();
 | 
|---|
| 72 |         if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
 | 
|---|
| 73 | 
 | 
|---|
| 74 |         return originalFunc.fptr;
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | #define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver )
 | 
|---|
| 78 | 
 | 
|---|
| 79 | //=============================================================================================
 | 
|---|
| 80 | // Interposition Startup logic
 | 
|---|
| 81 | //=============================================================================================
 | 
|---|
| 82 | 
 | 
|---|
| 83 | void sigHandler_segv ( __CFA_SIGPARMS__ );
 | 
|---|
| 84 | void sigHandler_ill  ( __CFA_SIGPARMS__ );
 | 
|---|
| 85 | void sigHandler_fpe  ( __CFA_SIGPARMS__ );
 | 
|---|
| 86 | void sigHandler_abort( __CFA_SIGPARMS__ );
 | 
|---|
| 87 | void sigHandler_term ( __CFA_SIGPARMS__ );
 | 
|---|
| 88 | 
 | 
|---|
| 89 | struct {
 | 
|---|
| 90 |         void (* exit)( int ) __attribute__(( __noreturn__ ));
 | 
|---|
| 91 |         void (* abort)( void ) __attribute__(( __noreturn__ ));
 | 
|---|
| 92 | } __cabi_libc;
 | 
|---|
| 93 | 
 | 
|---|
| 94 | extern "C" {
 | 
|---|
| 95 |         void __cfaabi_interpose_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
 | 
|---|
| 96 |         void __cfaabi_interpose_startup( void ) {
 | 
|---|
| 97 |                 const char *version = NULL;
 | 
|---|
| 98 | 
 | 
|---|
| 99 |                 preload_libgcc();
 | 
|---|
| 100 | 
 | 
|---|
| 101 | #pragma GCC diagnostic push
 | 
|---|
| 102 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
 | 
|---|
| 103 |                 INTERPOSE_LIBC( abort, version );
 | 
|---|
| 104 |                 INTERPOSE_LIBC( exit , version );
 | 
|---|
| 105 | #pragma GCC diagnostic pop
 | 
|---|
| 106 | 
 | 
|---|
| 107 |                 // Failure handler
 | 
|---|
| 108 |                 __cfaabi_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO );
 | 
|---|
| 109 |                 __cfaabi_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO );
 | 
|---|
| 110 |                 __cfaabi_sigaction( SIGILL , sigHandler_ill  , SA_SIGINFO );
 | 
|---|
| 111 |                 __cfaabi_sigaction( SIGFPE , sigHandler_fpe  , SA_SIGINFO );
 | 
|---|
| 112 |                 __cfaabi_sigaction( SIGABRT, sigHandler_abort, SA_SIGINFO | SA_RESETHAND);
 | 
|---|
| 113 |                 __cfaabi_sigaction( SIGTERM, sigHandler_term , SA_SIGINFO );
 | 
|---|
| 114 |                 __cfaabi_sigaction( SIGINT , sigHandler_term , SA_SIGINFO );
 | 
|---|
| 115 |         }
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | //=============================================================================================
 | 
|---|
| 119 | // Terminating Signals logic
 | 
|---|
| 120 | //=============================================================================================
 | 
|---|
| 121 | 
 | 
|---|
| 122 | // Forward declare abort after the __typeof__ call to avoid ambiguities
 | 
|---|
| 123 | void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
 | 
|---|
| 124 | void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
 | 
|---|
| 125 | 
 | 
|---|
| 126 | extern "C" {
 | 
|---|
| 127 |         void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 128 |                 abort( NULL );
 | 
|---|
| 129 |         }
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 132 |                 va_list argp;
 | 
|---|
| 133 |                 va_start( argp, fmt );
 | 
|---|
| 134 |                 abort( fmt, argp );
 | 
|---|
| 135 |                 va_end( argp );
 | 
|---|
| 136 |         }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 139 |                 __cabi_libc.exit( status );
 | 
|---|
| 140 |         }
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | void * kernel_abort    ( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return NULL; }
 | 
|---|
| 144 | void   kernel_abort_msg( void * data, char * buffer, int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
 | 
|---|
| 145 | int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | enum { abort_text_size = 1024 };
 | 
|---|
| 148 | static char abort_text[ abort_text_size ];
 | 
|---|
| 149 | static int abort_lastframe;
 | 
|---|
| 150 | 
 | 
|---|
| 151 | void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 152 |     va_list args;
 | 
|---|
| 153 |     va_start( args, fmt );
 | 
|---|
| 154 |     vfprintf( stderr, fmt, args );
 | 
|---|
| 155 |     va_end( args );
 | 
|---|
| 156 |         __cabi_libc.exit( status );
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
 | 
|---|
| 160 |         void * kernel_data = kernel_abort();                    // must be done here to lock down kernel
 | 
|---|
| 161 |         int len;
 | 
|---|
| 162 | 
 | 
|---|
| 163 |         abort_lastframe = kernel_abort_lastframe();
 | 
|---|
| 164 |         len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
 | 
|---|
| 165 |         __cfaabi_dbg_bits_write( abort_text, len );
 | 
|---|
| 166 | 
 | 
|---|
| 167 |         if ( fmt ) {
 | 
|---|
| 168 |                 va_list args;
 | 
|---|
| 169 |                 va_start( args, fmt );
 | 
|---|
| 170 | 
 | 
|---|
| 171 |                 len = vsnprintf( abort_text, abort_text_size, fmt, args );
 | 
|---|
| 172 |                 va_end( args );
 | 
|---|
| 173 |                 __cfaabi_dbg_bits_write( abort_text, len );
 | 
|---|
| 174 | 
 | 
|---|
| 175 |                 if ( fmt[strlen( fmt ) - 1] != '\n' ) {         // add optional newline if missing at the end of the format text
 | 
|---|
| 176 |                         __cfaabi_dbg_bits_write( "\n", 1 );
 | 
|---|
| 177 |                 }
 | 
|---|
| 178 |         }
 | 
|---|
| 179 | 
 | 
|---|
| 180 |         kernel_abort_msg( kernel_data, abort_text, abort_text_size );
 | 
|---|
| 181 |         __cabi_libc.abort();
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | static void __cfaabi_backtrace() {
 | 
|---|
| 185 |         enum {
 | 
|---|
| 186 |                 Frames = 50,                                                                    // maximum number of stack frames
 | 
|---|
| 187 |                 Start = 8,                                                                              // skip first N stack frames
 | 
|---|
| 188 |         };
 | 
|---|
| 189 | 
 | 
|---|
| 190 |         void * array[Frames];
 | 
|---|
| 191 |         size_t size = backtrace( array, Frames );
 | 
|---|
| 192 |         char ** messages = backtrace_symbols( array, size );
 | 
|---|
| 193 | 
 | 
|---|
| 194 |         // find executable name
 | 
|---|
| 195 |         *index( messages[0], '(' ) = '\0';
 | 
|---|
| 196 |         __cfaabi_dbg_bits_print_nolock( "Stack back trace for: %s\n", messages[0]);
 | 
|---|
| 197 | 
 | 
|---|
| 198 |         for ( int i = Start; i < size - abort_lastframe && messages != NULL; i += 1 ) {
 | 
|---|
| 199 |                 char * name = NULL, * offset_begin = NULL, * offset_end = NULL;
 | 
|---|
| 200 | 
 | 
|---|
| 201 |                 for ( char * p = messages[i]; *p; ++p ) {
 | 
|---|
| 202 |                         //__cfaabi_dbg_bits_print_nolock( "X %s\n", p);
 | 
|---|
| 203 |                         // find parantheses and +offset
 | 
|---|
| 204 |                         if ( *p == '(' ) {
 | 
|---|
| 205 |                                 name = p;
 | 
|---|
| 206 |                         }
 | 
|---|
| 207 |                         else if ( *p == '+' ) {
 | 
|---|
| 208 |                                 offset_begin = p;
 | 
|---|
| 209 |                         }
 | 
|---|
| 210 |                         else if ( *p == ')' ) {
 | 
|---|
| 211 |                                 offset_end = p;
 | 
|---|
| 212 |                                 break;
 | 
|---|
| 213 |                         }
 | 
|---|
| 214 |                 }
 | 
|---|
| 215 | 
 | 
|---|
| 216 |                 // if line contains symbol print it
 | 
|---|
| 217 |                 int frameNo = i - Start;
 | 
|---|
| 218 |                 if ( name && offset_begin && offset_end && name < offset_begin ) {
 | 
|---|
| 219 |                         // delimit strings
 | 
|---|
| 220 |                         *name++ = '\0';
 | 
|---|
| 221 |                         *offset_begin++ = '\0';
 | 
|---|
| 222 |                         *offset_end++ = '\0';
 | 
|---|
| 223 | 
 | 
|---|
| 224 |                         __cfaabi_dbg_bits_print_nolock( "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
 | 
|---|
| 225 |                 }
 | 
|---|
| 226 |                 // otherwise, print the whole line
 | 
|---|
| 227 |                 else {
 | 
|---|
| 228 |                         __cfaabi_dbg_bits_print_nolock( "(%i) %s\n", frameNo, messages[i] );
 | 
|---|
| 229 |                 }
 | 
|---|
| 230 |         }
 | 
|---|
| 231 |         free( messages );
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | void sigHandler_segv( __CFA_SIGPARMS__ ) {
 | 
|---|
| 235 |         abort( "Addressing invalid memory at location %p\n"
 | 
|---|
| 236 |                         "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",
 | 
|---|
| 237 |                         sfp->si_addr );
 | 
|---|
| 238 | }
 | 
|---|
| 239 | 
 | 
|---|
| 240 | void sigHandler_ill( __CFA_SIGPARMS__ ) {
 | 
|---|
| 241 |         abort( "Executing illegal instruction at location %p.\n"
 | 
|---|
| 242 |                         "Possible cause is stack corruption.\n",
 | 
|---|
| 243 |                         sfp->si_addr );
 | 
|---|
| 244 | }
 | 
|---|
| 245 | 
 | 
|---|
| 246 | void sigHandler_fpe( __CFA_SIGPARMS__ ) {
 | 
|---|
| 247 |         const char * msg;
 | 
|---|
| 248 | 
 | 
|---|
| 249 |         choose( sfp->si_code ) {
 | 
|---|
| 250 |           case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
 | 
|---|
| 251 |           case FPE_FLTOVF: msg = "overflow";
 | 
|---|
| 252 |           case FPE_FLTUND: msg = "underflow";
 | 
|---|
| 253 |           case FPE_FLTRES: msg = "inexact result";
 | 
|---|
| 254 |           case FPE_FLTINV: msg = "invalid operation";
 | 
|---|
| 255 |           default: msg = "unknown";
 | 
|---|
| 256 |         } // choose
 | 
|---|
| 257 |         abort( "Computation error %s at location %p.\n", msg, sfp->si_addr );
 | 
|---|
| 258 | }
 | 
|---|
| 259 | 
 | 
|---|
| 260 | void sigHandler_abort( __CFA_SIGPARMS__ ) {
 | 
|---|
| 261 |         __cfaabi_backtrace();
 | 
|---|
| 262 | 
 | 
|---|
| 263 |         // reset default signal handler
 | 
|---|
| 264 |         __cfaabi_sigdefault( SIGABRT );
 | 
|---|
| 265 | 
 | 
|---|
| 266 |         raise( SIGABRT );
 | 
|---|
| 267 | }
 | 
|---|
| 268 | 
 | 
|---|
| 269 | void sigHandler_term( __CFA_SIGPARMS__ ) {
 | 
|---|
| 270 |         abort( "Application stopped by %s signal.", sig == SIGINT ? "an interrupt (SIGINT)" : "a terminate (SIGTERM)" );
 | 
|---|
| 271 | }
 | 
|---|
| 272 | 
 | 
|---|
| 273 | // Local Variables: //
 | 
|---|
| 274 | // mode: c //
 | 
|---|
| 275 | // tab-width: 4 //
 | 
|---|
| 276 | // End: //
 | 
|---|