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