source: libcfa/src/interpose.cfa@ 1a3040c

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 1a3040c was 524627e, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

change NULL to 0pt

  • Property mode set to 100644
File size: 8.7 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
[524627e]12// Last Modified On : Sat Nov 30 07:09:42 2019
13// Update Count : 119
[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 ) {
97 const char *version = NULL;
[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
[de94a60]107 // Failure handler
108 __cfaabi_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO );
109 __cfaabi_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO );
110 __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO );
111 __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO );
[4f37255]112 __cfaabi_sigaction( SIGABRT, sigHandler_abrt, SA_SIGINFO | SA_RESETHAND);
[de94a60]113 __cfaabi_sigaction( SIGTERM, sigHandler_term , SA_SIGINFO );
114 __cfaabi_sigaction( SIGINT , sigHandler_term , SA_SIGINFO );
[6bfe5cc]115 }
[9d944b2]116}
117
[dbe9b08]118//=============================================================================================
119// Terminating Signals logic
120//=============================================================================================
121
[3d5f2ef1]122// Forward declare abort after the __typeof__ call to avoid ambiguities
[d7312ac]123void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
124void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
[3d5f2ef1]125
[9d944b2]126extern "C" {
[d7312ac]127 void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
[169d944]128 abort( NULL );
[9d944b2]129 }
130
[d7312ac]131 void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
[3d5f2ef1]132 va_list argp;
133 va_start( argp, fmt );
134 abort( fmt, argp );
135 va_end( argp );
[9d944b2]136 }
137
[d7312ac]138 void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
[169d944]139 __cabi_libc.exit( status );
[3d5f2ef1]140 }
[9d944b2]141}
142
[d7312ac]143void * kernel_abort ( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return NULL; }
144void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
145int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
[9d944b2]146
147enum { abort_text_size = 1024 };
148static char abort_text[ abort_text_size ];
[2b8bc41]149static int abort_lastframe;
[9d944b2]150
[d7312ac]151void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )) {
[169d944]152 va_list args;
153 va_start( args, fmt );
154 vfprintf( stderr, fmt, args );
155 va_end( args );
156 __cabi_libc.exit( status );
157}
158
[d7312ac]159void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
[3d5f2ef1]160 void * kernel_data = kernel_abort(); // must be done here to lock down kernel
161 int len;
[9d944b2]162
[3d5f2ef1]163 abort_lastframe = kernel_abort_lastframe();
164 len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
[1c40091]165 __cfaabi_dbg_write( abort_text, len );
[2b8bc41]166
[3d5f2ef1]167 if ( fmt ) {
168 va_list args;
169 va_start( args, fmt );
[9d944b2]170
[3d5f2ef1]171 len = vsnprintf( abort_text, abort_text_size, fmt, args );
172 va_end( args );
[1c40091]173 __cfaabi_dbg_write( abort_text, len );
[9d944b2]174
[3d5f2ef1]175 if ( fmt[strlen( fmt ) - 1] != '\n' ) { // add optional newline if missing at the end of the format text
[1c40091]176 __cfaabi_dbg_write( "\n", 1 );
[2b8bc41]177 }
[e464759]178 }
[3d5f2ef1]179
180 kernel_abort_msg( kernel_data, abort_text, abort_text_size );
181 __cabi_libc.abort();
[aca65621]182}
[6b0b624]183
[2b8bc41]184static void __cfaabi_backtrace() {
185 enum {
186 Frames = 50, // maximum number of stack frames
187 Start = 8, // skip first N stack frames
188 };
[dbe9b08]189
190 void * array[Frames];
[6bfe5cc]191 size_t size = backtrace( array, Frames );
[dbe9b08]192 char ** messages = backtrace_symbols( array, size );
193
194 // find executable name
195 *index( messages[0], '(' ) = '\0';
[1c40091]196 __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]);
[dbe9b08]197
[524627e]198 for ( int i = Start; i < size - abort_lastframe && messages != 0p; i += 1 ) {
199 char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
[dbe9b08]200
[2b8bc41]201 for ( char * p = messages[i]; *p; ++p ) {
[1c40091]202 //__cfaabi_bits_print_nolock( "X %s\n", p);
[dbe9b08]203 // find parantheses and +offset
204 if ( *p == '(' ) {
205 name = p;
[4f37255]206 } else if ( *p == '+' ) {
[dbe9b08]207 offset_begin = p;
[4f37255]208 } else if ( *p == ')' ) {
[dbe9b08]209 offset_end = p;
210 break;
211 }
212 }
213
214 // if line contains symbol print it
[2b8bc41]215 int frameNo = i - Start;
[dbe9b08]216 if ( name && offset_begin && offset_end && name < offset_begin ) {
217 // delimit strings
218 *name++ = '\0';
219 *offset_begin++ = '\0';
220 *offset_end++ = '\0';
221
[1c40091]222 __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
[4f37255]223 } else { // otherwise, print the whole line
[1c40091]224 __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] );
[dbe9b08]225 }
226 }
227 free( messages );
228}
229
230void sigHandler_segv( __CFA_SIGPARMS__ ) {
[169d944]231 abort( "Addressing invalid memory at location %p\n"
[2b8bc41]232 "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",
233 sfp->si_addr );
234}
235
236void sigHandler_ill( __CFA_SIGPARMS__ ) {
[169d944]237 abort( "Executing illegal instruction at location %p.\n"
[2b8bc41]238 "Possible cause is stack corruption.\n",
239 sfp->si_addr );
240}
241
242void sigHandler_fpe( __CFA_SIGPARMS__ ) {
243 const char * msg;
244
[a424315d]245 choose( sfp->si_code ) {
246 case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
247 case FPE_FLTOVF: msg = "overflow";
248 case FPE_FLTUND: msg = "underflow";
249 case FPE_FLTRES: msg = "inexact result";
250 case FPE_FLTINV: msg = "invalid operation";
[2b8bc41]251 default: msg = "unknown";
[a424315d]252 } // choose
[169d944]253 abort( "Computation error %s at location %p.\n", msg, sfp->si_addr );
[dbe9b08]254}
255
[4f37255]256void sigHandler_abrt( __CFA_SIGPARMS__ ) {
[2b8bc41]257 __cfaabi_backtrace();
[dbe9b08]258
259 // reset default signal handler
[2b8bc41]260 __cfaabi_sigdefault( SIGABRT );
[dbe9b08]261
262 raise( SIGABRT );
263}
264
[94dea96]265void sigHandler_term( __CFA_SIGPARMS__ ) {
266 abort( "Application stopped by %s signal.", sig == SIGINT ? "an interrupt (SIGINT)" : "a terminate (SIGTERM)" );
267}
268
[6b0b624]269// Local Variables: //
270// mode: c //
271// tab-width: 4 //
272// End: //
Note: See TracBrowser for help on using the repository browser.