[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
|
---|
[2b8bc41] | 12 | // Last Modified On : Tue Feb 6 17:57:56 2018
|
---|
| 13 | // Update Count : 49
|
---|
[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 |
|
---|
| 35 | typedef void (*generic_fptr_t)(void);
|
---|
| 36 | generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
|
---|
| 37 | const char * error;
|
---|
| 38 |
|
---|
| 39 | static void * library;
|
---|
| 40 | if ( ! library ) {
|
---|
| 41 | #if defined( RTLD_NEXT )
|
---|
| 42 | library = RTLD_NEXT;
|
---|
| 43 | #else
|
---|
| 44 | // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
|
---|
| 45 | library = dlopen( "libc.so.6", RTLD_LAZY );
|
---|
| 46 | error = dlerror();
|
---|
| 47 | if ( error ) {
|
---|
| 48 | abortf( "interpose_symbol : failed to open libc, %s\n", error );
|
---|
| 49 | }
|
---|
| 50 | #endif
|
---|
| 51 | } // if
|
---|
| 52 |
|
---|
| 53 | union { generic_fptr_t fptr; void* ptr; } originalFunc;
|
---|
[aca65621] | 54 |
|
---|
[9d944b2] | 55 | #if defined( _GNU_SOURCE )
|
---|
| 56 | if ( version ) {
|
---|
| 57 | originalFunc.ptr = dlvsym( library, symbol, version );
|
---|
| 58 | } else {
|
---|
| 59 | originalFunc.ptr = dlsym( library, symbol );
|
---|
| 60 | }
|
---|
| 61 | #else
|
---|
| 62 | originalFunc.ptr = dlsym( library, symbol );
|
---|
| 63 | #endif // _GNU_SOURCE
|
---|
[aca65621] | 64 |
|
---|
[9d944b2] | 65 | error = dlerror();
|
---|
[aca65621] | 66 | if ( error ) abortf( "interpose_symbol : internal error, %s\n", error );
|
---|
[9d944b2] | 67 |
|
---|
| 68 | return originalFunc.fptr;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | __typeof__( exit ) libc_exit __attribute__(( noreturn ));
|
---|
| 73 | __typeof__( abort ) libc_abort __attribute__(( noreturn ));
|
---|
| 74 |
|
---|
| 75 | forall(dtype T)
|
---|
| 76 | static inline void assign_ptr( 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 |
|
---|
| 87 | #define INIT_REALRTN( x, ver ) assign_ptr( (void**)&libc_##x, #x, ver)
|
---|
| 88 |
|
---|
[dbe9b08] | 89 | void sigHandler_segv ( __CFA_SIGPARMS__ );
|
---|
[2b8bc41] | 90 | void sigHandler_ill ( __CFA_SIGPARMS__ );
|
---|
| 91 | void sigHandler_fpe ( __CFA_SIGPARMS__ );
|
---|
[dbe9b08] | 92 | void sigHandler_abort( __CFA_SIGPARMS__ );
|
---|
| 93 |
|
---|
[6bfe5cc] | 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;
|
---|
[9d944b2] | 98 |
|
---|
[6bfe5cc] | 99 | INIT_REALRTN( abort, version );
|
---|
| 100 | INIT_REALRTN( exit, version );
|
---|
[dbe9b08] | 101 |
|
---|
[2b8bc41] | 102 | __cfaabi_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // Failure handler
|
---|
| 103 | __cfaabi_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); // Failure handler
|
---|
| 104 | __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO ); // Failure handler
|
---|
| 105 | __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO ); // Failure handler
|
---|
| 106 | __cfaabi_sigaction( SIGABRT, sigHandler_abort, SA_SIGINFO ); // Failure handler
|
---|
[6bfe5cc] | 107 | }
|
---|
[9d944b2] | 108 | }
|
---|
| 109 |
|
---|
[dbe9b08] | 110 | //=============================================================================================
|
---|
| 111 | // Terminating Signals logic
|
---|
| 112 | //=============================================================================================
|
---|
| 113 |
|
---|
[9d944b2] | 114 | extern "C" {
|
---|
[6bfe5cc] | 115 | void abort( void ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
---|
[9d944b2] | 116 | abortf( NULL );
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[6bfe5cc] | 119 | void exit( int __status ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
---|
[9d944b2] | 120 | libc_exit(__status);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[2b8bc41] | 124 | void abort( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
---|
| 125 | va_list argp;
|
---|
| 126 | va_start( argp, fmt );
|
---|
[9d944b2] | 127 | abortf( fmt, argp );
|
---|
[2b8bc41] | 128 | va_end( argp );
|
---|
[9d944b2] | 129 | }
|
---|
| 130 |
|
---|
[6bfe5cc] | 131 | void * kernel_abort ( void ) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
|
---|
| 132 | void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
|
---|
[2b8bc41] | 133 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return 4; }
|
---|
[9d944b2] | 134 |
|
---|
| 135 | enum { abort_text_size = 1024 };
|
---|
| 136 | static char abort_text[ abort_text_size ];
|
---|
[2b8bc41] | 137 | static int abort_lastframe;
|
---|
[9d944b2] | 138 |
|
---|
[e464759] | 139 | extern "C" {
|
---|
| 140 | void abortf( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
---|
[2b8bc41] | 141 | void * kernel_data = kernel_abort(); // must be done here to lock down kernel
|
---|
[e464759] | 142 | int len;
|
---|
[9d944b2] | 143 |
|
---|
[2b8bc41] | 144 | abort_lastframe = kernel_abort_lastframe();
|
---|
| 145 | len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
|
---|
| 146 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
| 147 |
|
---|
[6bfe5cc] | 148 | if ( fmt ) {
|
---|
[e464759] | 149 | va_list args;
|
---|
| 150 | va_start( args, fmt );
|
---|
[9d944b2] | 151 |
|
---|
[e464759] | 152 | len = vsnprintf( abort_text, abort_text_size, fmt, args );
|
---|
| 153 | va_end( args );
|
---|
[36982fc] | 154 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
[9d944b2] | 155 |
|
---|
[2b8bc41] | 156 | if ( fmt[strlen( fmt ) - 1] != '\n' ) { // add optional newline if missing at the end of the format text
|
---|
| 157 | __cfaabi_dbg_bits_write( "\n", 1 );
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
[9d944b2] | 160 |
|
---|
[e464759] | 161 | kernel_abort_msg( kernel_data, abort_text, abort_text_size );
|
---|
| 162 | libc_abort();
|
---|
| 163 | }
|
---|
[aca65621] | 164 | }
|
---|
[6b0b624] | 165 |
|
---|
[2b8bc41] | 166 | static void __cfaabi_backtrace() {
|
---|
| 167 | enum {
|
---|
| 168 | Frames = 50, // maximum number of stack frames
|
---|
| 169 | Start = 8, // skip first N stack frames
|
---|
| 170 | };
|
---|
[dbe9b08] | 171 |
|
---|
| 172 | void * array[Frames];
|
---|
[6bfe5cc] | 173 | size_t size = backtrace( array, Frames );
|
---|
[dbe9b08] | 174 | char ** messages = backtrace_symbols( array, size );
|
---|
| 175 |
|
---|
| 176 | // find executable name
|
---|
| 177 | *index( messages[0], '(' ) = '\0';
|
---|
| 178 | __cfaabi_dbg_bits_print_nolock( "Stack back trace for: %s\n", messages[0]);
|
---|
| 179 |
|
---|
[2b8bc41] | 180 | for ( int i = Start; i < size - abort_lastframe && messages != NULL; i += 1 ) {
|
---|
| 181 | char * name = NULL, * offset_begin = NULL, * offset_end = NULL;
|
---|
[dbe9b08] | 182 |
|
---|
[2b8bc41] | 183 | for ( char * p = messages[i]; *p; ++p ) {
|
---|
[6bfe5cc] | 184 | //__cfaabi_dbg_bits_print_nolock( "X %s\n", p);
|
---|
[dbe9b08] | 185 | // find parantheses and +offset
|
---|
| 186 | if ( *p == '(' ) {
|
---|
| 187 | name = p;
|
---|
| 188 | }
|
---|
| 189 | else if ( *p == '+' ) {
|
---|
| 190 | offset_begin = p;
|
---|
| 191 | }
|
---|
| 192 | else if ( *p == ')' ) {
|
---|
| 193 | offset_end = p;
|
---|
| 194 | break;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | // if line contains symbol print it
|
---|
[2b8bc41] | 199 | int frameNo = i - Start;
|
---|
[dbe9b08] | 200 | if ( name && offset_begin && offset_end && name < offset_begin ) {
|
---|
| 201 | // delimit strings
|
---|
| 202 | *name++ = '\0';
|
---|
| 203 | *offset_begin++ = '\0';
|
---|
| 204 | *offset_end++ = '\0';
|
---|
| 205 |
|
---|
| 206 | __cfaabi_dbg_bits_print_nolock( "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
|
---|
| 207 | }
|
---|
| 208 | // otherwise, print the whole line
|
---|
| 209 | else {
|
---|
| 210 | __cfaabi_dbg_bits_print_nolock( "(%i) %s\n", frameNo, messages[i] );
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | free( messages );
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | void sigHandler_segv( __CFA_SIGPARMS__ ) {
|
---|
[2b8bc41] | 217 | abortf( "Attempt to address location %p\n"
|
---|
| 218 | "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",
|
---|
| 219 | sfp->si_addr );
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | void sigHandler_ill( __CFA_SIGPARMS__ ) {
|
---|
| 223 | abortf( "Attempt to execute code at location %p.\n"
|
---|
| 224 | "Possible cause is stack corruption.\n",
|
---|
| 225 | sfp->si_addr );
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | void sigHandler_fpe( __CFA_SIGPARMS__ ) {
|
---|
| 229 | const char * msg;
|
---|
| 230 |
|
---|
| 231 | switch ( sfp->si_code ) {
|
---|
| 232 | case FPE_INTDIV:
|
---|
| 233 | case FPE_FLTDIV: msg = "divide by zero"; break;
|
---|
| 234 | case FPE_FLTOVF: msg = "overflow"; break;
|
---|
| 235 | case FPE_FLTUND: msg = "underflow"; break;
|
---|
| 236 | case FPE_FLTRES: msg = "inexact result"; break;
|
---|
| 237 | case FPE_FLTINV: msg = "invalid operation"; break;
|
---|
| 238 | default: msg = "unknown";
|
---|
| 239 | } // switch
|
---|
| 240 | abortf( "Floating point error.\n"
|
---|
| 241 | "Cause is %s.\n", msg );
|
---|
[dbe9b08] | 242 | }
|
---|
| 243 |
|
---|
| 244 | void sigHandler_abort( __CFA_SIGPARMS__ ) {
|
---|
[2b8bc41] | 245 | __cfaabi_backtrace();
|
---|
[dbe9b08] | 246 |
|
---|
| 247 | // reset default signal handler
|
---|
[2b8bc41] | 248 | __cfaabi_sigdefault( SIGABRT );
|
---|
[dbe9b08] | 249 |
|
---|
| 250 | raise( SIGABRT );
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[6b0b624] | 253 | // Local Variables: //
|
---|
| 254 | // mode: c //
|
---|
| 255 | // tab-width: 4 //
|
---|
| 256 | // End: //
|
---|