source: src/libcfa/interpose.c@ c02f761

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 c02f761 was 67db067, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

clean up

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