source: libcfa/src/interpose.cfa@ 3381ed7

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 3381ed7 was 8a13c47, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

restructure errors invoking signal handlers and handled by abort

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[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
[8a13c47]12// Last Modified On : Thu Jan 30 17:47:32 2020
13// Update Count : 156
[9d944b2]14//
15
[7867eb9]16#include <stdarg.h> // va_start, va_end
17#include <string.h> // strlen
18#include <unistd.h> // _exit, getpid
[dbe9b08]19#define __USE_GNU
[58b6d1b]20#include <signal.h>
[dbe9b08]21#undef __USE_GNU
[67db067]22extern "C" {
[7867eb9]23#include <dlfcn.h> // dlopen, dlsym
24#include <execinfo.h> // backtrace, messages
[9d944b2]25}
26
[73abe95]27#include "bits/debug.hfa"
28#include "bits/defs.hfa"
29#include "bits/signal.hfa" // sigHandler_?
30#include "startup.hfa" // STARTUP_PRIORITY_CORE
[9d944b2]31
[3d5f2ef1]32//=============================================================================================
33// Interposing helpers
34//=============================================================================================
35
[ad64520]36void 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
[67db067]41typedef void (* generic_fptr_t)(void);
42generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
[9d944b2]43 const char * error;
44
45 static void * library;
46 if ( ! library ) {
47 #if defined( RTLD_NEXT )
48 library = RTLD_NEXT;
49 #else
50 // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
51 library = dlopen( "libc.so.6", RTLD_LAZY );
52 error = dlerror();
53 if ( error ) {
[169d944]54 abort( "interpose_symbol : failed to open libc, %s\n", error );
[9d944b2]55 }
56 #endif
57 } // if
58
[67db067]59 union { generic_fptr_t fptr; void * ptr; } originalFunc;
[aca65621]60
[9d944b2]61 #if defined( _GNU_SOURCE )
62 if ( version ) {
63 originalFunc.ptr = dlvsym( library, symbol, version );
64 } else {
65 originalFunc.ptr = dlsym( library, symbol );
66 }
67 #else
68 originalFunc.ptr = dlsym( library, symbol );
69 #endif // _GNU_SOURCE
[aca65621]70
[9d944b2]71 error = dlerror();
[169d944]72 if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
[9d944b2]73
74 return originalFunc.fptr;
75}
76
[67db067]77#define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver )
[3d5f2ef1]78
79//=============================================================================================
[67db067]80// Interposition Startup logic
[3d5f2ef1]81//=============================================================================================
[9d944b2]82
[4f37255]83void sigHandler_segv( __CFA_SIGPARMS__ );
84void sigHandler_ill ( __CFA_SIGPARMS__ );
85void sigHandler_fpe ( __CFA_SIGPARMS__ );
86void sigHandler_abrt( __CFA_SIGPARMS__ );
87void sigHandler_term( __CFA_SIGPARMS__ );
[dbe9b08]88
[3d5f2ef1]89struct {
[d7312ac]90 void (* exit)( int ) __attribute__(( __noreturn__ ));
91 void (* abort)( void ) __attribute__(( __noreturn__ ));
[3d5f2ef1]92} __cabi_libc;
93
[6bfe5cc]94extern "C" {
95 void __cfaabi_interpose_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
96 void __cfaabi_interpose_startup( void ) {
[8a13c47]97 const char *version = 0p;
[9d944b2]98
[ad64520]99 preload_libgcc();
100
[d7312ac]101#pragma GCC diagnostic push
102#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
[3d5f2ef1]103 INTERPOSE_LIBC( abort, version );
104 INTERPOSE_LIBC( exit , version );
[d7312ac]105#pragma GCC diagnostic pop
[dbe9b08]106
[8a13c47]107 // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because
108 // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean
109 // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only)
110 // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort
111 // print a stack trace that uses substantial stack space.
112
113 #define MINSTKSZ SIGSTKSZ * 8
114 static char stack[MINSTKSZ] __attribute__(( aligned (16) ));
115 static stack_t ss;
116
117 ss.ss_sp = stack;
118 ss.ss_size = MINSTKSZ;
119 ss.ss_flags = 0;
120 if ( sigaltstack( &ss, 0p ) == -1 ) {
121 abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) );
122 } // if
123
[de94a60]124 // Failure handler
[8a13c47]125 __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK );
126 __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK );
127 __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK );
128 __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK );
129 __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // one shot handler, return to default
130 __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND );
131 __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND );
132 __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // terminal hangup
[6bfe5cc]133 }
[9d944b2]134}
135
[dbe9b08]136//=============================================================================================
137// Terminating Signals logic
138//=============================================================================================
139
[3d5f2ef1]140// Forward declare abort after the __typeof__ call to avoid ambiguities
[d7312ac]141void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
142void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
[8a13c47]143void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
[3d5f2ef1]144
[9d944b2]145extern "C" {
[d7312ac]146 void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
[8a13c47]147 abort( false, NULL ); // FIX ME: 0p does not work
[9d944b2]148 }
149
[d7312ac]150 void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
[3d5f2ef1]151 va_list argp;
152 va_start( argp, fmt );
[8a13c47]153 abort( false, fmt, argp );
[3d5f2ef1]154 va_end( argp );
[9d944b2]155 }
156
[d7312ac]157 void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
[169d944]158 __cabi_libc.exit( status );
[3d5f2ef1]159 }
[9d944b2]160}
161
[8a13c47]162void * kernel_abort( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 0p; }
163void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
164// See concurrency/kernel.cfa for strong definition used in multi-processor mode.
[d7312ac]165int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
[9d944b2]166
167enum { abort_text_size = 1024 };
168static char abort_text[ abort_text_size ];
169
[8a13c47]170static void __cfaabi_backtrace( int start ) {
[2b8bc41]171 enum {
172 Frames = 50, // maximum number of stack frames
173 };
[8a13c47]174 int last = kernel_abort_lastframe(); // skip last N stack frames
[dbe9b08]175
176 void * array[Frames];
[6bfe5cc]177 size_t size = backtrace( array, Frames );
[dbe9b08]178 char ** messages = backtrace_symbols( array, size );
179
[8a13c47]180 *index( messages[0], '(' ) = '\0'; // find executable name
[1c40091]181 __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]);
[dbe9b08]182
[8a13c47]183 for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) {
[524627e]184 char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
[dbe9b08]185
[8a13c47]186 for ( char * p = messages[i]; *p; ++p ) { // find parantheses and +offset
[1c40091]187 //__cfaabi_bits_print_nolock( "X %s\n", p);
[dbe9b08]188 if ( *p == '(' ) {
189 name = p;
[4f37255]190 } else if ( *p == '+' ) {
[dbe9b08]191 offset_begin = p;
[4f37255]192 } else if ( *p == ')' ) {
[dbe9b08]193 offset_end = p;
194 break;
195 }
196 }
197
[8a13c47]198 // if line contains symbol, print it
199 int frameNo = i - start;
[dbe9b08]200 if ( name && offset_begin && offset_end && name < offset_begin ) {
[8a13c47]201 *name++ = '\0'; // delimit strings
[dbe9b08]202 *offset_begin++ = '\0';
203 *offset_end++ = '\0';
204
[1c40091]205 __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
[4f37255]206 } else { // otherwise, print the whole line
[1c40091]207 __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] );
[dbe9b08]208 }
209 }
210 free( messages );
211}
212
[8a13c47]213void exit( int status, const char fmt[], ... ) {
214 va_list args;
215 va_start( args, fmt );
216 vfprintf( stderr, fmt, args );
217 va_end( args );
218 __cabi_libc.exit( status );
219}
220
221void abort( bool signalAbort, const char fmt[], ... ) {
222 void * kernel_data = kernel_abort(); // must be done here to lock down kernel
223 int len;
224
225 signal( SIGABRT, SIG_DFL ); // prevent final "real" abort from recursing to handler
226
227 len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
228 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
229
230 if ( fmt ) {
231 va_list args;
232 va_start( args, fmt );
233
234 len = vsnprintf( abort_text, abort_text_size, fmt, args );
235 va_end( args );
236 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
237
238 if ( fmt[strlen( fmt ) - 1] != '\n' ) { // add optional newline if missing at the end of the format text
239 __cfaabi_dbg_write( "\n", 1 );
240 }
241 }
242
243 kernel_abort_msg( kernel_data, abort_text, abort_text_size );
244 __cfaabi_backtrace( signalAbort ? 4 : 3 );
245
246 __cabi_libc.abort(); // print stack trace in handler
247}
248
249void abort( const char fmt[], ... ) {
250 va_list args;
251 va_start( args, fmt );
252 abort( false, fmt, args );
253 va_end( args );
254}
255
[dbe9b08]256void sigHandler_segv( __CFA_SIGPARMS__ ) {
[8a13c47]257 if ( sfp->si_addr == 0p ) {
258 abort( true, "Null pointer (0p) dereference.\n" );
[1469a8a]259 } else {
[8a13c47]260 abort( true, "%s at memory location %p.\n"
[1469a8a]261 "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",
262 (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr );
263 }
[2b8bc41]264}
265
266void sigHandler_ill( __CFA_SIGPARMS__ ) {
[8a13c47]267 abort( true, "Executing illegal instruction at location %p.\n"
[2b8bc41]268 "Possible cause is stack corruption.\n",
269 sfp->si_addr );
270}
271
272void sigHandler_fpe( __CFA_SIGPARMS__ ) {
273 const char * msg;
274
[a424315d]275 choose( sfp->si_code ) {
276 case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
277 case FPE_FLTOVF: msg = "overflow";
278 case FPE_FLTUND: msg = "underflow";
279 case FPE_FLTRES: msg = "inexact result";
280 case FPE_FLTINV: msg = "invalid operation";
[2b8bc41]281 default: msg = "unknown";
[a424315d]282 } // choose
[8a13c47]283 abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr );
[dbe9b08]284}
285
[94dea96]286void sigHandler_term( __CFA_SIGPARMS__ ) {
[8a13c47]287 abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) );
[94dea96]288}
289
[6b0b624]290// Local Variables: //
291// mode: c //
292// tab-width: 4 //
293// End: //
Note: See TracBrowser for help on using the repository browser.