source: src/libcfa/interpose.c@ cfc3e0f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since cfc3e0f was 7867eb9, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update includes

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