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