source: libcfa/src/interpose.cfa @ ba9baad

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since ba9baad was ba9baad, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Simplified some of the handling by converting libcfa sources from .c to .cfa

  • 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
[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
[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
[dbe9b08]83void sigHandler_segv ( __CFA_SIGPARMS__ );
[2b8bc41]84void sigHandler_ill  ( __CFA_SIGPARMS__ );
85void sigHandler_fpe  ( __CFA_SIGPARMS__ );
[dbe9b08]86void sigHandler_abort( __CFA_SIGPARMS__ );
[94dea96]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 );
112                __cfaabi_sigaction( SIGABRT, sigHandler_abort, SA_SIGINFO | SA_RESETHAND);
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)
165        __cfaabi_dbg_bits_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 );
173                __cfaabi_dbg_bits_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
176                        __cfaabi_dbg_bits_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';
196        __cfaabi_dbg_bits_print_nolock( "Stack back trace for: %s\n", messages[0]);
197
[2b8bc41]198        for ( int i = Start; i < size - abort_lastframe && messages != NULL; i += 1 ) {
199                char * name = NULL, * offset_begin = NULL, * offset_end = NULL;
[dbe9b08]200
[2b8bc41]201                for ( char * p = messages[i]; *p; ++p ) {
[6bfe5cc]202                        //__cfaabi_dbg_bits_print_nolock( "X %s\n", p);
[dbe9b08]203                        // find parantheses and +offset
204                        if ( *p == '(' ) {
205                                name = p;
206                        }
207                        else if ( *p == '+' ) {
208                                offset_begin = p;
209                        }
210                        else if ( *p == ')' ) {
211                                offset_end = p;
212                                break;
213                        }
214                }
215
216                // if line contains symbol print it
[2b8bc41]217                int frameNo = i - Start;
[dbe9b08]218                if ( name && offset_begin && offset_end && name < offset_begin ) {
219                        // delimit strings
220                        *name++ = '\0';
221                        *offset_begin++ = '\0';
222                        *offset_end++ = '\0';
223
224                        __cfaabi_dbg_bits_print_nolock( "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
225                }
226                // otherwise, print the whole line
227                else {
228                        __cfaabi_dbg_bits_print_nolock( "(%i) %s\n", frameNo, messages[i] );
229                }
230        }
231        free( messages );
232}
233
234void sigHandler_segv( __CFA_SIGPARMS__ ) {
[169d944]235        abort( "Addressing invalid memory at location %p\n"
[2b8bc41]236                        "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",
237                        sfp->si_addr );
238}
239
240void sigHandler_ill( __CFA_SIGPARMS__ ) {
[169d944]241        abort( "Executing illegal instruction at location %p.\n"
[2b8bc41]242                        "Possible cause is stack corruption.\n",
243                        sfp->si_addr );
244}
245
246void sigHandler_fpe( __CFA_SIGPARMS__ ) {
247        const char * msg;
248
[a424315d]249        choose( sfp->si_code ) {
250          case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
251          case FPE_FLTOVF: msg = "overflow";
252          case FPE_FLTUND: msg = "underflow";
253          case FPE_FLTRES: msg = "inexact result";
254          case FPE_FLTINV: msg = "invalid operation";
[2b8bc41]255          default: msg = "unknown";
[a424315d]256        } // choose
[169d944]257        abort( "Computation error %s at location %p.\n", msg, sfp->si_addr );
[dbe9b08]258}
259
260void sigHandler_abort( __CFA_SIGPARMS__ ) {
[2b8bc41]261        __cfaabi_backtrace();
[dbe9b08]262
263        // reset default signal handler
[2b8bc41]264        __cfaabi_sigdefault( SIGABRT );
[dbe9b08]265
266        raise( SIGABRT );
267}
268
[94dea96]269void sigHandler_term( __CFA_SIGPARMS__ ) {
270        abort( "Application stopped by %s signal.", sig == SIGINT ? "an interrupt (SIGINT)" : "a terminate (SIGTERM)" );
271}
272
[6b0b624]273// Local Variables: //
274// mode: c //
275// tab-width: 4 //
276// End: //
Note: See TracBrowser for help on using the repository browser.