source: libcfa/src/interpose.cfa @ a7d696f

ADTast-experimentalpthread-emulation
Last change on this file since a7d696f was a7d696f, checked in by z277zhu <z277zhu@…>, 2 years ago

added pthread symbol interpose

Signed-off-by: z277zhu <z277zhu@…>

  • Property mode set to 100644
File size: 16.1 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 interpose_symbol( const char symbol[], const char version[] ) {
45        const char * error;
46
47        static void * library;
48        static void * pthread_library;
49        if ( ! library ) {
50                #if defined( RTLD_NEXT )
51                        library = RTLD_NEXT;
52                #else
53                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
54                        library = dlopen( "libc.so.6", RTLD_LAZY );
55                        error = dlerror();
56                        if ( error ) {
57                                abort( "interpose_symbol : failed to open libc, %s\n", error );
58                        }
59                #endif
60        } // if
61        if ( ! pthread_library ) {
62                #if defined( RTLD_NEXT )
63                        pthread_library = RTLD_NEXT;
64                #else
65                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
66                        pthread_library = dlopen( "libpthread.so", RTLD_LAZY );
67                        error = dlerror();
68                        if ( error ) {
69                                abort( "interpose_symbol : failed to open libpthread, %s\n", error );
70                        }
71                #endif
72        } // if
73
74        union { generic_fptr_t fptr; void * ptr; } originalFunc;
75
76        #if defined( _GNU_SOURCE )
77                if ( version ) {
78                        originalFunc.ptr = dlvsym( library, symbol, version );
79                } else {
80                        originalFunc.ptr = dlsym( library, symbol );
81                }
82        #else
83                originalFunc.ptr = dlsym( library, symbol );
84        #endif // _GNU_SOURCE
85
86        error = dlerror();
87        if ( error ) {
88                originalFunc.ptr = dlsym( pthread_library, symbol );
89                error = dlerror();
90                if (error){
91                        abort( "interpose_symbol : internal error, %s\n", error );
92                }       // if
93        }       // if
94
95        return originalFunc.fptr;
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        typeof(pthread_create) pthread_create;
114        typeof(pthread_join) pthread_join;
115        typeof(pthread_self) pthread_self;
116        typeof(pthread_cond_init) pthread_cond_init;
117        typeof(pthread_cond_destroy) pthread_cond_destroy;
118        typeof(pthread_cond_signal) pthread_cond_signal;
119        typeof(pthread_cond_broadcast) pthread_cond_broadcast;
120        typeof(pthread_cond_wait) pthread_cond_wait;
121        typeof(pthread_mutex_init) pthread_mutex_init;
122        typeof(pthread_mutex_lock) pthread_mutex_lock;
123        typeof(pthread_mutex_trylock) pthread_mutex_trylock;
124        typeof(pthread_mutex_unlock) pthread_mutex_unlock;
125        typeof(pthread_mutex_destroy) pthread_mutex_destroy;
126        typeof(pthread_attr_init ) pthread_attr_init;
127        typeof(pthread_attr_setstack ) pthread_attr_setstack;
128        typeof(pthread_attr_destroy) pthread_attr_destroy;
129        typeof(pthread_attr_getstacksize) pthread_attr_getstacksize;
130} __cabi_libc;
131
132libcfa_public int cfa_main_returned;
133
134extern "C" {
135        void __cfaabi_interpose_startup( void ) {
136                const char *version = 0p;
137                cfa_main_returned = 0;
138
139                preload_libgcc();
140
141#pragma GCC diagnostic push
142#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
143                INTERPOSE_LIBC( abort, version );
144                INTERPOSE_LIBC( exit , version );
145                INTERPOSE_LIBC( pthread_create , version );
146                INTERPOSE_LIBC( pthread_join , version );
147                INTERPOSE_LIBC( pthread_self , version );
148                INTERPOSE_LIBC( pthread_mutex_init , version );
149                INTERPOSE_LIBC( pthread_mutex_lock , version );
150                INTERPOSE_LIBC( pthread_mutex_unlock , version );
151                INTERPOSE_LIBC( pthread_mutex_destroy , version );
152                INTERPOSE_LIBC( pthread_cond_init , version );
153                INTERPOSE_LIBC( pthread_cond_destroy , version );
154                INTERPOSE_LIBC( pthread_cond_signal , version );
155                INTERPOSE_LIBC( pthread_cond_broadcast , version );
156                INTERPOSE_LIBC( pthread_cond_wait , version );
157                INTERPOSE_LIBC( pthread_attr_init , version );
158                INTERPOSE_LIBC( pthread_attr_destroy , version );
159                INTERPOSE_LIBC( pthread_attr_setstack , version );
160                INTERPOSE_LIBC( pthread_attr_getstacksize , version );
161#pragma GCC diagnostic pop
162
163                // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because
164                // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean
165                // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only)
166                // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort
167                // print a stack trace that uses substantial stack space.
168
169                #define MINSTKSZ SIGSTKSZ * 8
170                static char stack[MINSTKSZ] __attribute__(( aligned (16) ));
171                static stack_t ss;
172
173                ss.ss_sp = stack;
174                ss.ss_size = MINSTKSZ;
175                ss.ss_flags = 0;
176                if ( sigaltstack( &ss, 0p ) == -1 ) {
177                        abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) );
178                } // if
179
180                // Failure handler
181                 // internal errors
182                __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Invalid memory reference (default: Core)
183                __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Bus error, bad memory access (default: Core)
184                __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK ); // Illegal Instruction (default: Core)
185                __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK ); // Floating-point exception (default: Core)
186
187                // handlers to outside errors
188                // reset in-case they insist and send it over and over
189                __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Termination signal (default: Term)
190                __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Interrupt from keyboard (default: Term)
191                __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Hangup detected on controlling terminal or death of controlling process (default: Term)
192                __cfaabi_sigaction( SIGQUIT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Quit from keyboard (default: Core)
193                __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Abort signal from abort(3) (default: Core)
194        }
195}
196
197//=============================================================================================
198// Terminating Signals logic
199//=============================================================================================
200
201// Forward declare abort after the __typeof__ call to avoid ambiguities
202libcfa_public void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
203libcfa_public void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
204libcfa_public void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
205libcfa_public void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ ));
206
207extern "C" {
208        libcfa_public void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
209                abort( false, "%s", "" );
210        }
211
212        libcfa_public void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
213                va_list argp;
214                va_start( argp, fmt );
215                __abort( false, fmt, argp );
216                va_end( argp );
217        }
218
219        libcfa_public void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
220                __cabi_libc.exit( status );
221        }
222
223        libcfa_public int real_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){
224                return __cabi_libc.pthread_create(_thread, attr, start_routine, arg);
225        }
226
227        libcfa_public int real_pthread_join(pthread_t _thread, void **retval){
228                return __cabi_libc.pthread_join(_thread, retval);
229        }
230
231        libcfa_public pthread_t real_pthread_self(void){
232                return __cabi_libc.pthread_self();
233        }
234        /* mutex Default attr is PTHREAD_MUTEX_RECURSIVE*/
235        libcfa_public int real_pthread_mutex_init(pthread_mutex_t *_mutex, const pthread_mutexattr_t *attr){
236                return __cabi_libc.pthread_mutex_init(_mutex, attr);
237        }
238        libcfa_public int real_pthread_mutex_destroy(pthread_mutex_t *_mutex){
239                return __cabi_libc.pthread_mutex_destroy(_mutex);
240        }
241        libcfa_public int real_pthread_mutex_lock(pthread_mutex_t *_mutex){
242                return __cabi_libc.pthread_mutex_lock(_mutex);
243        }
244        libcfa_public int real_pthread_mutex_unlock(pthread_mutex_t *_mutex){
245                return  __cabi_libc.pthread_mutex_unlock(_mutex);
246        }
247        libcfa_public int real_pthread_mutex_trylock(pthread_mutex_t *_mutex){
248                return __cabi_libc.pthread_mutex_trylock(_mutex);
249        }
250        libcfa_public int real_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr){
251                return __cabi_libc.pthread_cond_init(cond, attr);
252        }
253        libcfa_public int real_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *_mutex){
254                return __cabi_libc.pthread_cond_wait(cond, _mutex);
255        }
256        libcfa_public int real_pthread_cond_signal(pthread_cond_t *cond){
257                return __cabi_libc.pthread_cond_signal(cond);
258        }
259        libcfa_public int real_pthread_cond_broadcast(pthread_cond_t *cond){
260                return __cabi_libc.pthread_cond_broadcast(cond);
261        }
262        libcfa_public int real_pthread_cond_destroy(pthread_cond_t *cond){
263                return __cabi_libc.pthread_cond_destroy(cond);
264        }
265        libcfa_public int real_pthread_attr_init(pthread_attr_t *attr){
266                return __cabi_libc.pthread_attr_init(attr);
267        }
268        libcfa_public int real_pthread_attr_destroy(pthread_attr_t *attr){
269                return __cabi_libc.pthread_attr_destroy(attr);
270        }
271        libcfa_public int real_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){
272                return __cabi_libc.pthread_attr_setstack(attr, stackaddr, stacksize);
273        }
274        libcfa_public int pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){
275                return __cabi_libc.pthread_attr_getstacksize(attr, stacksize);
276        }
277
278}
279
280// See concurrency/kernel.cfa and concurrency/preemption.cfa for strong definition used in multi-processor mode.
281void __kernel_abort_lock( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
282void __kernel_abort_msg( char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
283int __kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
284
285enum { abort_text_size = 1024 };
286static char abort_text[ abort_text_size ];
287
288static void __cfaabi_backtrace( int start ) {
289        enum { Frames = 50, };                                                          // maximum number of stack frames
290        int last = __kernel_abort_lastframe();                          // skip last N stack frames
291
292        void * array[Frames];
293        size_t size = backtrace( array, Frames );
294        char ** messages = backtrace_symbols( array, size ); // does not demangle names
295
296        *index( messages[0], '(' ) = '\0';                                      // find executable name
297        __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]);
298
299        for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) {
300                char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
301
302                for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset
303                        //__cfaabi_bits_print_nolock( "X %s\n", p);
304                        if ( *p == '(' ) {
305                                name = p;
306                        } else if ( *p == '+' ) {
307                                offset_begin = p;
308                        } else if ( *p == ')' ) {
309                                offset_end = p;
310                                break;
311                        }
312                }
313
314                // if line contains symbol, print it
315                int frameNo = i - start;
316                if ( name && offset_begin && offset_end && name < offset_begin ) {
317                        *name++ = '\0';                                                         // delimit strings
318                        *offset_begin++ = '\0';
319                        *offset_end++ = '\0';
320
321                        __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
322                } else {                                                                                // otherwise, print the whole line
323                        __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] );
324                }
325        }
326        free( messages );
327}
328
329void exit( int status, const char fmt[], ... ) {
330        va_list args;
331        va_start( args, fmt );
332        vfprintf( stderr, fmt, args );
333        va_end( args );
334        __cabi_libc.exit( status );
335}
336
337static volatile bool __abort_first = 0;
338
339// Cannot forward va_list.
340void __abort( bool signalAbort, const char fmt[], va_list args ) {
341        // Multiple threads can come here from multiple paths
342        // To make sure this is safe any concurrent/subsequent call to abort is redirected to libc-abort
343        bool first = ! __atomic_test_and_set( &__abort_first, __ATOMIC_SEQ_CST);
344
345        // Prevent preemption from kicking-in and messing with the abort
346        __kernel_abort_lock();
347
348        // first to abort ?
349        if ( !first ) {
350                // We aren't the first to abort just let C handle it
351                signal( SIGABRT, SIG_DFL );     // restore default in case we came here through the function.
352                __cabi_libc.abort();
353        }
354
355        int len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
356        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
357
358        // print the cause of the error
359        assert( fmt );
360        len = vsnprintf( abort_text, abort_text_size, fmt, args );
361        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
362
363        // add optional newline if missing at the end of the format text
364        if ( fmt[strlen( fmt ) - 1] != '\n' ) {
365                __cfaabi_bits_write( STDERR_FILENO, "\n", 1 );
366        } // if
367
368        // Give the kernel the chance to add some data in here
369        __kernel_abort_msg( abort_text, abort_text_size );
370
371        // print stack trace in handler
372        __cfaabi_backtrace( signalAbort ? 4 : 2 );
373
374        // Finally call abort
375        __cabi_libc.abort();
376
377}
378
379void abort( const char fmt[], ... ) {
380        va_list args;
381        va_start( args, fmt );
382        __abort( false, fmt, args );
383    // CONTROL NEVER REACHES HERE!
384        va_end( args );
385}
386
387void abort( bool signalAbort, const char fmt[], ... ) {
388    va_list args;
389    va_start( args, fmt );
390    __abort( signalAbort, fmt, args );
391    // CONTROL NEVER REACHES HERE!
392    va_end( args );
393}
394
395void sigHandler_segv( __CFA_SIGPARMS__ ) {
396                if ( sfp->si_addr == 0p ) {
397                        abort( true, "Null pointer (0p) dereference.\n" );
398                } else {
399                        abort( true, "%s at memory location %p.\n"
400                                   "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",
401                                   (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr );
402                }
403}
404
405void sigHandler_ill( __CFA_SIGPARMS__ ) {
406        abort( true, "Executing illegal instruction at location %p.\n"
407                        "Possible cause is stack corruption.\n",
408                        sfp->si_addr );
409}
410
411void sigHandler_fpe( __CFA_SIGPARMS__ ) {
412        const char * msg;
413
414        choose( sfp->si_code ) {
415          case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
416          case FPE_FLTOVF: msg = "overflow";
417          case FPE_FLTUND: msg = "underflow";
418          case FPE_FLTRES: msg = "inexact result";
419          case FPE_FLTINV: msg = "invalid operation";
420          default: msg = "unknown";
421        } // choose
422        abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr );
423}
424
425void sigHandler_term( __CFA_SIGPARMS__ ) {
426        abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) );
427}
428
429// Local Variables: //
430// mode: c //
431// tab-width: 4 //
432// End: //
Note: See TracBrowser for help on using the repository browser.