source: src/libcfa/interpose.c @ e8db295

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

some more work on kernel doubly linked lists and fixed segfault in abort message

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