source: src/libcfa/interpose.c @ ecaeac6e

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 ecaeac6e was 94dea96, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add run-time signal-handlers for SIGTERM and SIGINT

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