| 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 : Thu Mar  2 13:56:26 2023 | 
|---|
| 13 | // Update Count     : 191 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <stdarg.h>                                                                             // va_start, va_end | 
|---|
| 17 | #include <stdio.h> | 
|---|
| 18 | #include <string.h>                                                                             // strlen | 
|---|
| 19 | #include <unistd.h>                                                                             // _exit, getpid | 
|---|
| 20 | #include <signal.h> | 
|---|
| 21 | extern "C" { | 
|---|
| 22 | #include <dlfcn.h>                                                                              // dlopen, dlsym | 
|---|
| 23 | #include <execinfo.h>                                                                   // backtrace, messages | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | #include "bits/debug.hfa" | 
|---|
| 27 | #include "bits/defs.hfa" | 
|---|
| 28 | #include "bits/signal.hfa"                                                              // sigHandler_? | 
|---|
| 29 | #include "startup.hfa"                                                                  // STARTUP_PRIORITY_CORE | 
|---|
| 30 | #include <assert.h> | 
|---|
| 31 |  | 
|---|
| 32 | //============================================================================================= | 
|---|
| 33 | // Interposing helpers | 
|---|
| 34 | //============================================================================================= | 
|---|
| 35 |  | 
|---|
| 36 | static 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 | static generic_fptr_t do_interpose_symbol( void * library, const char symbol[], const char version[] ) { | 
|---|
| 43 | const char * error; | 
|---|
| 44 |  | 
|---|
| 45 | union { generic_fptr_t fptr; void * ptr; } originalFunc; | 
|---|
| 46 |  | 
|---|
| 47 | originalFunc.ptr = dlsym( library, symbol ); | 
|---|
| 48 | error = dlerror(); | 
|---|
| 49 | if ( error ) abort( "interpose_symbol : internal error, %s\n", error ); | 
|---|
| 50 |  | 
|---|
| 51 | return originalFunc.fptr; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | static generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) { | 
|---|
| 55 | void * library; | 
|---|
| 56 | #if defined( RTLD_NEXT ) | 
|---|
| 57 | library = RTLD_NEXT; | 
|---|
| 58 | #else | 
|---|
| 59 | library = dlopen( "libc.so.6", RTLD_LAZY ); | 
|---|
| 60 | if ( ! library ) { | 
|---|
| 61 | const char * error = dlerror(); | 
|---|
| 62 | if ( error ) { | 
|---|
| 63 | abort( "interpose_symbol : failed to open libc, %s\n", error ); | 
|---|
| 64 | } // if | 
|---|
| 65 | } // if | 
|---|
| 66 | #endif | 
|---|
| 67 |  | 
|---|
| 68 | return do_interpose_symbol(library, symbol, version); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | #define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver ) | 
|---|
| 72 |  | 
|---|
| 73 | //============================================================================================= | 
|---|
| 74 | // Interposition Startup logic | 
|---|
| 75 | //============================================================================================= | 
|---|
| 76 |  | 
|---|
| 77 | static void sigHandler_segv( __CFA_SIGPARMS__ ); | 
|---|
| 78 | static void sigHandler_ill ( __CFA_SIGPARMS__ ); | 
|---|
| 79 | static void sigHandler_fpe ( __CFA_SIGPARMS__ ); | 
|---|
| 80 | static void sigHandler_abrt( __CFA_SIGPARMS__ ); | 
|---|
| 81 | static void sigHandler_term( __CFA_SIGPARMS__ ); | 
|---|
| 82 |  | 
|---|
| 83 | static struct { | 
|---|
| 84 | void (* exit)( int ) __attribute__(( __noreturn__ )); | 
|---|
| 85 | void (* abort)( void ) __attribute__(( __noreturn__ )); | 
|---|
| 86 | } __cabi_libc; | 
|---|
| 87 |  | 
|---|
| 88 | libcfa_public int cfa_main_returned; | 
|---|
| 89 |  | 
|---|
| 90 | extern "C" { | 
|---|
| 91 | void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) __attribute__((weak)); | 
|---|
| 92 | void __cfaabi_interpose_startup( void ) { | 
|---|
| 93 | const char *version = 0p; | 
|---|
| 94 | cfa_main_returned = 0; | 
|---|
| 95 |  | 
|---|
| 96 | preload_libgcc(); | 
|---|
| 97 |  | 
|---|
| 98 | #pragma GCC diagnostic push | 
|---|
| 99 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" | 
|---|
| 100 | INTERPOSE_LIBC( abort, version ); | 
|---|
| 101 | INTERPOSE_LIBC( exit , version ); | 
|---|
| 102 | #pragma GCC diagnostic pop | 
|---|
| 103 |  | 
|---|
| 104 | if(__cfathreadabi_interpose_startup) __cfathreadabi_interpose_startup( do_interpose_symbol ); | 
|---|
| 105 |  | 
|---|
| 106 | // SKULLDUGGERY: In Ubuntu 22.04, someone augmented signal.h to allow SIGSTKSZ to be "sysconf(_SC_SIGSTKSZ)" in | 
|---|
| 107 | // sigstksz.h, as well as 8192 in sigstack.h. HOWEVER, they forgot to provide a mechanism to tell signal.h to | 
|---|
| 108 | // use sigstack.h rather than sigstksz.h. (I'm not happy.) By undefining _GNU_SOURCE before signal.h and | 
|---|
| 109 | // redefining it afterwards, you can get 8192, but then nothing works correctly inside of signal.h without | 
|---|
| 110 | // _GNU_SOURCE defined.  So what is needed is a way to get signal.h to use sigstack.h WITH _GNU_SOURCE defined. | 
|---|
| 111 | // Basically something is wrong with features.h and its use in signal.h. | 
|---|
| 112 |  | 
|---|
| 113 | #undef SIGSTKSZ | 
|---|
| 114 | #define SIGSTKSZ 8192 | 
|---|
| 115 |  | 
|---|
| 116 | // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because | 
|---|
| 117 | // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean | 
|---|
| 118 | // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only) | 
|---|
| 119 | // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort | 
|---|
| 120 | // print a stack trace that uses substantial stack space. | 
|---|
| 121 |  | 
|---|
| 122 | #define MINSTKSZ SIGSTKSZ * 8 | 
|---|
| 123 | static char stack[MINSTKSZ] __attribute__(( aligned (16) )); | 
|---|
| 124 | static stack_t ss; | 
|---|
| 125 |  | 
|---|
| 126 | ss.ss_sp = stack; | 
|---|
| 127 | ss.ss_size = MINSTKSZ; | 
|---|
| 128 | ss.ss_flags = 0; | 
|---|
| 129 | if ( sigaltstack( &ss, 0p ) == -1 ) { | 
|---|
| 130 | abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) ); | 
|---|
| 131 | } // if | 
|---|
| 132 |  | 
|---|
| 133 | // Failure handler | 
|---|
| 134 | // internal errors | 
|---|
| 135 | __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Invalid memory reference (default: Core) | 
|---|
| 136 | __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Bus error, bad memory access (default: Core) | 
|---|
| 137 | __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK ); // Illegal Instruction (default: Core) | 
|---|
| 138 | __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK ); // Floating-point exception (default: Core) | 
|---|
| 139 |  | 
|---|
| 140 | // handlers to outside errors | 
|---|
| 141 | // reset in-case they insist and send it over and over | 
|---|
| 142 | __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Termination signal (default: Term) | 
|---|
| 143 | __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Interrupt from keyboard (default: Term) | 
|---|
| 144 | __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Hangup detected on controlling terminal or death of controlling process (default: Term) | 
|---|
| 145 | __cfaabi_sigaction( SIGQUIT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Quit from keyboard (default: Core) | 
|---|
| 146 | __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Abort signal from abort(3) (default: Core) | 
|---|
| 147 | } | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | //============================================================================================= | 
|---|
| 151 | // Terminating Signals logic | 
|---|
| 152 | //============================================================================================= | 
|---|
| 153 |  | 
|---|
| 154 | // Forward declare abort after the __typeof__ call to avoid ambiguities | 
|---|
| 155 | libcfa_public void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 156 | libcfa_public void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 157 | libcfa_public void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 158 | libcfa_public void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )); | 
|---|
| 159 |  | 
|---|
| 160 | extern "C" { | 
|---|
| 161 | libcfa_public void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) { | 
|---|
| 162 | abort( false, "%s", "" ); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | libcfa_public void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) { | 
|---|
| 166 | va_list argp; | 
|---|
| 167 | va_start( argp, fmt ); | 
|---|
| 168 | __abort( false, fmt, argp ); | 
|---|
| 169 | va_end( argp ); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | libcfa_public void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) { | 
|---|
| 173 | __cabi_libc.exit( status ); | 
|---|
| 174 | } | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | // See concurrency/kernel.cfa and concurrency/preemption.cfa for strong definition used in multi-processor mode. | 
|---|
| 178 | void __kernel_abort_lock( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {} | 
|---|
| 179 | void __kernel_abort_msg( char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {} | 
|---|
| 180 | int __kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; } | 
|---|
| 181 |  | 
|---|
| 182 | enum { abort_text_size = 1024 }; | 
|---|
| 183 | static char abort_text[ abort_text_size ]; | 
|---|
| 184 |  | 
|---|
| 185 | static void __cfaabi_backtrace( int start ) { | 
|---|
| 186 | enum { Frames = 50, };                                                          // maximum number of stack frames | 
|---|
| 187 | int last = __kernel_abort_lastframe();                          // skip last N stack frames | 
|---|
| 188 |  | 
|---|
| 189 | void * array[Frames]; | 
|---|
| 190 | size_t size = backtrace( array, Frames ); | 
|---|
| 191 | char ** messages = backtrace_symbols( array, size ); // does not demangle names | 
|---|
| 192 |  | 
|---|
| 193 | *index( messages[0], '(' ) = '\0';                                      // find executable name | 
|---|
| 194 | __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]); | 
|---|
| 195 |  | 
|---|
| 196 | for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) { | 
|---|
| 197 | char * name = 0p, * offset_begin = 0p, * offset_end = 0p; | 
|---|
| 198 |  | 
|---|
| 199 | for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset | 
|---|
| 200 | //__cfaabi_bits_print_nolock( "X %s\n", p); | 
|---|
| 201 | if ( *p == '(' ) { | 
|---|
| 202 | name = p; | 
|---|
| 203 | } else if ( *p == '+' ) { | 
|---|
| 204 | offset_begin = p; | 
|---|
| 205 | } else if ( *p == ')' ) { | 
|---|
| 206 | offset_end = p; | 
|---|
| 207 | break; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | // if line contains symbol, print it | 
|---|
| 212 | int frameNo = i - start; | 
|---|
| 213 | if ( name && offset_begin && offset_end && name < offset_begin ) { | 
|---|
| 214 | *name++ = '\0';                                                         // delimit strings | 
|---|
| 215 | *offset_begin++ = '\0'; | 
|---|
| 216 | *offset_end++ = '\0'; | 
|---|
| 217 |  | 
|---|
| 218 | __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end); | 
|---|
| 219 | } else {                                                                                // otherwise, print the whole line | 
|---|
| 220 | __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] ); | 
|---|
| 221 | } | 
|---|
| 222 | } | 
|---|
| 223 | free( messages ); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | void exit( int status, const char fmt[], ... ) { | 
|---|
| 227 | va_list args; | 
|---|
| 228 | va_start( args, fmt ); | 
|---|
| 229 | vfprintf( stderr, fmt, args ); | 
|---|
| 230 | va_end( args ); | 
|---|
| 231 | __cabi_libc.exit( status ); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | static volatile bool __abort_first = 0; | 
|---|
| 235 |  | 
|---|
| 236 | // Cannot forward va_list. | 
|---|
| 237 | void __abort( bool signalAbort, const char fmt[], va_list args ) { | 
|---|
| 238 | // Multiple threads can come here from multiple paths | 
|---|
| 239 | // To make sure this is safe any concurrent/subsequent call to abort is redirected to libc-abort | 
|---|
| 240 | bool first = ! __atomic_test_and_set( &__abort_first, __ATOMIC_SEQ_CST); | 
|---|
| 241 |  | 
|---|
| 242 | // Prevent preemption from kicking-in and messing with the abort | 
|---|
| 243 | __kernel_abort_lock(); | 
|---|
| 244 |  | 
|---|
| 245 | // first to abort ? | 
|---|
| 246 | if ( !first ) { | 
|---|
| 247 | // We aren't the first to abort just let C handle it | 
|---|
| 248 | signal( SIGABRT, SIG_DFL );     // restore default in case we came here through the function. | 
|---|
| 249 | __cabi_libc.abort(); | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | int len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid) | 
|---|
| 253 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); | 
|---|
| 254 |  | 
|---|
| 255 | // print the cause of the error | 
|---|
| 256 | assert( fmt ); | 
|---|
| 257 | len = vsnprintf( abort_text, abort_text_size, fmt, args ); | 
|---|
| 258 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); | 
|---|
| 259 |  | 
|---|
| 260 | // add optional newline if missing at the end of the format text | 
|---|
| 261 | if ( fmt[strlen( fmt ) - 1] != '\n' ) { | 
|---|
| 262 | __cfaabi_bits_write( STDERR_FILENO, "\n", 1 ); | 
|---|
| 263 | } // if | 
|---|
| 264 |  | 
|---|
| 265 | // Give the kernel the chance to add some data in here | 
|---|
| 266 | __kernel_abort_msg( abort_text, abort_text_size ); | 
|---|
| 267 |  | 
|---|
| 268 | // print stack trace in handler | 
|---|
| 269 | __cfaabi_backtrace( signalAbort ? 4 : 2 ); | 
|---|
| 270 |  | 
|---|
| 271 | // Finally call abort | 
|---|
| 272 | __cabi_libc.abort(); | 
|---|
| 273 |  | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | void abort( const char fmt[], ... ) { | 
|---|
| 277 | va_list args; | 
|---|
| 278 | va_start( args, fmt ); | 
|---|
| 279 | __abort( false, fmt, args ); | 
|---|
| 280 | // CONTROL NEVER REACHES HERE! | 
|---|
| 281 | va_end( args ); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | void abort( bool signalAbort, const char fmt[], ... ) { | 
|---|
| 285 | va_list args; | 
|---|
| 286 | va_start( args, fmt ); | 
|---|
| 287 | __abort( signalAbort, fmt, args ); | 
|---|
| 288 | // CONTROL NEVER REACHES HERE! | 
|---|
| 289 | va_end( args ); | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | void sigHandler_segv( __CFA_SIGPARMS__ ) { | 
|---|
| 293 | if ( sfp->si_addr == 0p ) { | 
|---|
| 294 | abort( true, "Null pointer (0p) dereference.\n" ); | 
|---|
| 295 | } else { | 
|---|
| 296 | abort( true, "%s at memory location %p.\n" | 
|---|
| 297 | "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", | 
|---|
| 298 | (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr ); | 
|---|
| 299 | } | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | void sigHandler_ill( __CFA_SIGPARMS__ ) { | 
|---|
| 303 | abort( true, "Executing illegal instruction at location %p.\n" | 
|---|
| 304 | "Possible cause is stack corruption.\n", | 
|---|
| 305 | sfp->si_addr ); | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | void sigHandler_fpe( __CFA_SIGPARMS__ ) { | 
|---|
| 309 | const char * msg; | 
|---|
| 310 |  | 
|---|
| 311 | choose( sfp->si_code ) { | 
|---|
| 312 | case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero"; | 
|---|
| 313 | case FPE_FLTOVF: msg = "overflow"; | 
|---|
| 314 | case FPE_FLTUND: msg = "underflow"; | 
|---|
| 315 | case FPE_FLTRES: msg = "inexact result"; | 
|---|
| 316 | case FPE_FLTINV: msg = "invalid operation"; | 
|---|
| 317 | default: msg = "unknown"; | 
|---|
| 318 | } // choose | 
|---|
| 319 | abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr ); | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | void sigHandler_term( __CFA_SIGPARMS__ ) { | 
|---|
| 323 | abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) ); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | // Local Variables: // | 
|---|
| 327 | // mode: c // | 
|---|
| 328 | // tab-width: 4 // | 
|---|
| 329 | // End: // | 
|---|