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