source: libcfa/src/interpose.cfa @ 9cd5bd2

pthread-emulation
Last change on this file since 9cd5bd2 was 9cd5bd2, checked in by Thierry Delisle <tdelisle@…>, 4 months ago

Added an assembly to prevent null-checks from being optimized out.
Attempted to reduce the scope of needed headers.
Cleaned tabbing.

  • Property mode set to 100644
File size: 12.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 : Fri Mar 13 17:35:37 2020
13// Update Count     : 178
14//
15
16#include <stdarg.h>                                                                             // va_start, va_end
17#include <stdio.h>
18#include <string.h>                                                                             // strlen
19#include <unistd.h>                                                                             // _exit, getpid
20#define __USE_GNU
21#include <signal.h>
22#undef __USE_GNU
23extern "C" {
24#include <dlfcn.h>                                                                              // dlopen, dlsym
25#include <execinfo.h>                                                                   // backtrace, messages
26}
27
28#include "bits/debug.hfa"
29#include "bits/defs.hfa"
30#include "bits/signal.hfa"                                                              // sigHandler_?
31#include "startup.hfa"                                                                  // STARTUP_PRIORITY_CORE
32#include <assert.h>
33
34//=============================================================================================
35// Interposing helpers
36//=============================================================================================
37
38static void preload_libgcc(void) {
39        dlopen( "libgcc_s.so.1", RTLD_NOW );
40        if ( const char * error = dlerror() ) abort( "interpose_symbol : internal error pre-loading libgcc, %s\n", error );
41}
42
43typedef void (* generic_fptr_t)(void);
44static generic_fptr_t do_interpose_symbol( void * library, const char symbol[], const char version[] ) {
45        const char * error;
46
47        union { generic_fptr_t fptr; void * ptr; } originalFunc;
48
49        #if defined( _GNU_SOURCE )
50                if ( version ) {
51                        originalFunc.ptr = dlvsym( library, symbol, version );
52                } else {
53                        originalFunc.ptr = dlsym( library, symbol );
54                }
55        #else
56                originalFunc.ptr = dlsym( library, symbol );
57        #endif // _GNU_SOURCE
58
59        error = dlerror();
60        if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
61
62        return originalFunc.fptr;
63}
64
65static generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
66        const char * error;
67
68        static void * library;
69        static void * pthread_library;
70        if ( ! library ) {
71                #if defined( RTLD_NEXT )
72                        library = RTLD_NEXT;
73                #else
74                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
75                        library = dlopen( "libc.so.6", RTLD_LAZY );
76                        error = dlerror();
77                        if ( error ) {
78                                abort( "interpose_symbol : failed to open libc, %s\n", error );
79                        }
80                #endif
81        } // if
82        if ( ! pthread_library ) {
83                #if defined( RTLD_NEXT )
84                        pthread_library = RTLD_NEXT;
85                #else
86                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
87                        pthread_library = dlopen( "libpthread.so", RTLD_LAZY );
88                        error = dlerror();
89                        if ( error ) {
90                                abort( "interpose_symbol : failed to open libpthread, %s\n", error );
91                        }
92                #endif
93        } // if
94
95        return do_interpose_symbol(library, symbol, version);
96}
97
98#define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver )
99
100//=============================================================================================
101// Interposition Startup logic
102//=============================================================================================
103
104static void sigHandler_segv( __CFA_SIGPARMS__ );
105static void sigHandler_ill ( __CFA_SIGPARMS__ );
106static void sigHandler_fpe ( __CFA_SIGPARMS__ );
107static void sigHandler_abrt( __CFA_SIGPARMS__ );
108static void sigHandler_term( __CFA_SIGPARMS__ );
109
110static struct {
111        void (* exit)( int ) __attribute__(( __noreturn__ ));
112        void (* abort)( void ) __attribute__(( __noreturn__ ));
113} __cabi_libc;
114
115libcfa_public int cfa_main_returned;
116
117extern "C" {
118        void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) __attribute__((weak));
119        void __cfaabi_interpose_startup( void ) {
120                const char *version = 0p;
121                cfa_main_returned = 0;
122
123                preload_libgcc();
124
125#pragma GCC diagnostic push
126#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
127                INTERPOSE_LIBC( abort, version );
128                INTERPOSE_LIBC( exit , version );
129#pragma GCC diagnostic pop
130
131                if(__cfathreadabi_interpose_startup) __cfathreadabi_interpose_startup( do_interpose_symbol );
132
133                // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because
134                // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean
135                // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only)
136                // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort
137                // print a stack trace that uses substantial stack space.
138
139                #define MINSTKSZ SIGSTKSZ * 8
140                static char stack[MINSTKSZ] __attribute__(( aligned (16) ));
141                static stack_t ss;
142
143                ss.ss_sp = stack;
144                ss.ss_size = MINSTKSZ;
145                ss.ss_flags = 0;
146                if ( sigaltstack( &ss, 0p ) == -1 ) {
147                        abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) );
148                } // if
149
150                // Failure handler
151                 // internal errors
152                __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Invalid memory reference (default: Core)
153                __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Bus error, bad memory access (default: Core)
154                __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK ); // Illegal Instruction (default: Core)
155                __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK ); // Floating-point exception (default: Core)
156
157                // handlers to outside errors
158                // reset in-case they insist and send it over and over
159                __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Termination signal (default: Term)
160                __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Interrupt from keyboard (default: Term)
161                __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Hangup detected on controlling terminal or death of controlling process (default: Term)
162                __cfaabi_sigaction( SIGQUIT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Quit from keyboard (default: Core)
163                __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Abort signal from abort(3) (default: Core)
164        }
165}
166
167//=============================================================================================
168// Terminating Signals logic
169//=============================================================================================
170
171// Forward declare abort after the __typeof__ call to avoid ambiguities
172libcfa_public void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
173libcfa_public void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
174libcfa_public void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
175libcfa_public void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ ));
176
177extern "C" {
178        libcfa_public void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
179                abort( false, "%s", "" );
180        }
181
182        libcfa_public void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
183                va_list argp;
184                va_start( argp, fmt );
185                __abort( false, fmt, argp );
186                va_end( argp );
187        }
188
189        libcfa_public void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
190                __cabi_libc.exit( status );
191        }
192}
193
194// See concurrency/kernel.cfa and concurrency/preemption.cfa for strong definition used in multi-processor mode.
195void __kernel_abort_lock( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
196void __kernel_abort_msg( char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
197int __kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
198
199enum { abort_text_size = 1024 };
200static char abort_text[ abort_text_size ];
201
202static void __cfaabi_backtrace( int start ) {
203        enum { Frames = 50, };                                                          // maximum number of stack frames
204        int last = __kernel_abort_lastframe();                          // skip last N stack frames
205
206        void * array[Frames];
207        size_t size = backtrace( array, Frames );
208        char ** messages = backtrace_symbols( array, size ); // does not demangle names
209
210        *index( messages[0], '(' ) = '\0';                                      // find executable name
211        __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]);
212
213        for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) {
214                char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
215
216                for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset
217                        //__cfaabi_bits_print_nolock( "X %s\n", p);
218                        if ( *p == '(' ) {
219                                name = p;
220                        } else if ( *p == '+' ) {
221                                offset_begin = p;
222                        } else if ( *p == ')' ) {
223                                offset_end = p;
224                                break;
225                        }
226                }
227
228                // if line contains symbol, print it
229                int frameNo = i - start;
230                if ( name && offset_begin && offset_end && name < offset_begin ) {
231                        *name++ = '\0';                                                         // delimit strings
232                        *offset_begin++ = '\0';
233                        *offset_end++ = '\0';
234
235                        __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
236                } else {                                                                                // otherwise, print the whole line
237                        __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] );
238                }
239        }
240        free( messages );
241}
242
243void exit( int status, const char fmt[], ... ) {
244        va_list args;
245        va_start( args, fmt );
246        vfprintf( stderr, fmt, args );
247        va_end( args );
248        __cabi_libc.exit( status );
249}
250
251static volatile bool __abort_first = 0;
252
253// Cannot forward va_list.
254void __abort( bool signalAbort, const char fmt[], va_list args ) {
255        // Multiple threads can come here from multiple paths
256        // To make sure this is safe any concurrent/subsequent call to abort is redirected to libc-abort
257        bool first = ! __atomic_test_and_set( &__abort_first, __ATOMIC_SEQ_CST);
258
259        // Prevent preemption from kicking-in and messing with the abort
260        __kernel_abort_lock();
261
262        // first to abort ?
263        if ( !first ) {
264                // We aren't the first to abort just let C handle it
265                signal( SIGABRT, SIG_DFL );     // restore default in case we came here through the function.
266                __cabi_libc.abort();
267        }
268
269        int len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
270        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
271
272        // print the cause of the error
273        assert( fmt );
274        len = vsnprintf( abort_text, abort_text_size, fmt, args );
275        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
276
277        // add optional newline if missing at the end of the format text
278        if ( fmt[strlen( fmt ) - 1] != '\n' ) {
279                __cfaabi_bits_write( STDERR_FILENO, "\n", 1 );
280        } // if
281
282        // Give the kernel the chance to add some data in here
283        __kernel_abort_msg( abort_text, abort_text_size );
284
285        // print stack trace in handler
286        __cfaabi_backtrace( signalAbort ? 4 : 2 );
287
288        // Finally call abort
289        __cabi_libc.abort();
290
291}
292
293void abort( const char fmt[], ... ) {
294        va_list args;
295        va_start( args, fmt );
296        __abort( false, fmt, args );
297    // CONTROL NEVER REACHES HERE!
298        va_end( args );
299}
300
301void abort( bool signalAbort, const char fmt[], ... ) {
302    va_list args;
303    va_start( args, fmt );
304    __abort( signalAbort, fmt, args );
305    // CONTROL NEVER REACHES HERE!
306    va_end( args );
307}
308
309void sigHandler_segv( __CFA_SIGPARMS__ ) {
310                if ( sfp->si_addr == 0p ) {
311                        abort( true, "Null pointer (0p) dereference.\n" );
312                } else {
313                        abort( true, "%s at memory location %p.\n"
314                                   "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",
315                                   (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr );
316                }
317}
318
319void sigHandler_ill( __CFA_SIGPARMS__ ) {
320        abort( true, "Executing illegal instruction at location %p.\n"
321                        "Possible cause is stack corruption.\n",
322                        sfp->si_addr );
323}
324
325void sigHandler_fpe( __CFA_SIGPARMS__ ) {
326        const char * msg;
327
328        choose( sfp->si_code ) {
329          case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
330          case FPE_FLTOVF: msg = "overflow";
331          case FPE_FLTUND: msg = "underflow";
332          case FPE_FLTRES: msg = "inexact result";
333          case FPE_FLTINV: msg = "invalid operation";
334          default: msg = "unknown";
335        } // choose
336        abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr );
337}
338
339void sigHandler_term( __CFA_SIGPARMS__ ) {
340        abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) );
341}
342
343// Local Variables: //
344// mode: c //
345// tab-width: 4 //
346// End: //
Note: See TracBrowser for help on using the repository browser.