source: src/libcfa/interpose.c @ a424315d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a424315d was a424315d, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

update error messages

  • Property mode set to 100644
File size: 7.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 : Wed Feb  7 09:05:18 2018
13// Update Count     : 59
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
35typedef void (*generic_fptr_t)(void);
36generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
37        const char * error;
38
39        static void * library;
40        if ( ! library ) {
41                #if defined( RTLD_NEXT )
42                        library = RTLD_NEXT;
43                #else
44                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
45                        library = dlopen( "libc.so.6", RTLD_LAZY );
46                        error = dlerror();
47                        if ( error ) {
48                                abortf( "interpose_symbol : failed to open libc, %s\n", error );
49                        }
50                #endif
51        } // if
52
53        union { generic_fptr_t fptr; void* ptr; } originalFunc;
54
55        #if defined( _GNU_SOURCE )
56                if ( version ) {
57                        originalFunc.ptr = dlvsym( library, symbol, version );
58                } else {
59                        originalFunc.ptr = dlsym( library, symbol );
60                }
61        #else
62                originalFunc.ptr = dlsym( library, symbol );
63        #endif // _GNU_SOURCE
64
65        error = dlerror();
66        if ( error ) abortf( "interpose_symbol : internal error, %s\n", error );
67
68        return originalFunc.fptr;
69}
70
71
72__typeof__( exit ) libc_exit __attribute__(( noreturn ));
73__typeof__( abort ) libc_abort __attribute__(( noreturn ));
74
75forall(dtype T)
76static inline void assign_ptr( 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 INIT_REALRTN( x, ver ) assign_ptr( (void**)&libc_##x, #x, ver)
88
89void sigHandler_segv ( __CFA_SIGPARMS__ );
90void sigHandler_ill  ( __CFA_SIGPARMS__ );
91void sigHandler_fpe  ( __CFA_SIGPARMS__ );
92void sigHandler_abort( __CFA_SIGPARMS__ );
93
94extern "C" {
95        void __cfaabi_interpose_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
96        void __cfaabi_interpose_startup( void ) {
97                const char *version = NULL;
98
99                INIT_REALRTN( abort, version );
100                INIT_REALRTN( exit, version );
101
102                __cfaabi_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // Failure handler
103                __cfaabi_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); // Failure handler
104                __cfaabi_sigaction( SIGILL , sigHandler_ill  , SA_SIGINFO ); // Failure handler
105                __cfaabi_sigaction( SIGFPE , sigHandler_fpe  , SA_SIGINFO ); // Failure handler
106                __cfaabi_sigaction( SIGABRT, sigHandler_abort, SA_SIGINFO ); // Failure handler
107        }
108}
109
110//=============================================================================================
111// Terminating Signals logic
112//=============================================================================================
113
114extern "C" {
115        void abort( void ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
116                abortf( NULL );
117        }
118
119        void exit( int __status ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
120                libc_exit(__status);
121        }
122}
123
124void abort( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
125        va_list argp;
126        va_start( argp, fmt );
127        abortf( fmt, argp );
128        va_end( argp );
129}
130
131void * kernel_abort    ( void ) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
132void   kernel_abort_msg( void * data, char * buffer, int size ) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
133int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return 4; }
134
135enum { abort_text_size = 1024 };
136static char abort_text[ abort_text_size ];
137static int abort_lastframe;
138
139extern "C" {
140        void abortf( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
141                void * kernel_data = kernel_abort();                    // must be done here to lock down kernel
142                int len;
143
144                abort_lastframe = kernel_abort_lastframe();
145                len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
146                __cfaabi_dbg_bits_write( abort_text, len );
147
148                if ( fmt ) {
149                        va_list args;
150                        va_start( args, fmt );
151
152                        len = vsnprintf( abort_text, abort_text_size, fmt, args );
153                        va_end( args );
154                        __cfaabi_dbg_bits_write( abort_text, len );
155
156                        if ( fmt[strlen( fmt ) - 1] != '\n' ) {         // add optional newline if missing at the end of the format text
157                                __cfaabi_dbg_bits_write( "\n", 1 );
158                        }
159                }
160
161                kernel_abort_msg( kernel_data, abort_text, abort_text_size );
162                libc_abort();
163        }
164}
165
166static void __cfaabi_backtrace() {
167        enum {
168                Frames = 50,                                                                    // maximum number of stack frames
169                Start = 8,                                                                              // skip first N stack frames
170        };
171
172        void * array[Frames];
173        size_t size = backtrace( array, Frames );
174        char ** messages = backtrace_symbols( array, size );
175
176        // find executable name
177        *index( messages[0], '(' ) = '\0';
178        __cfaabi_dbg_bits_print_nolock( "Stack back trace for: %s\n", messages[0]);
179
180        for ( int i = Start; i < size - abort_lastframe && messages != NULL; i += 1 ) {
181                char * name = NULL, * offset_begin = NULL, * offset_end = NULL;
182
183                for ( char * p = messages[i]; *p; ++p ) {
184                        //__cfaabi_dbg_bits_print_nolock( "X %s\n", p);
185                        // find parantheses and +offset
186                        if ( *p == '(' ) {
187                                name = p;
188                        }
189                        else if ( *p == '+' ) {
190                                offset_begin = p;
191                        }
192                        else if ( *p == ')' ) {
193                                offset_end = p;
194                                break;
195                        }
196                }
197
198                // if line contains symbol print it
199                int frameNo = i - Start;
200                if ( name && offset_begin && offset_end && name < offset_begin ) {
201                        // delimit strings
202                        *name++ = '\0';
203                        *offset_begin++ = '\0';
204                        *offset_end++ = '\0';
205
206                        __cfaabi_dbg_bits_print_nolock( "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
207                }
208                // otherwise, print the whole line
209                else {
210                        __cfaabi_dbg_bits_print_nolock( "(%i) %s\n", frameNo, messages[i] );
211                }
212        }
213        free( messages );
214}
215
216void sigHandler_segv( __CFA_SIGPARMS__ ) {
217        abortf( "Addressing invalid memory at location %p\n"
218                        "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",
219                        sfp->si_addr );
220}
221
222void sigHandler_ill( __CFA_SIGPARMS__ ) {
223        abortf( "Executing illegal instruction at location %p.\n"
224                        "Possible cause is stack corruption.\n",
225                        sfp->si_addr );
226}
227
228void sigHandler_fpe( __CFA_SIGPARMS__ ) {
229        const char * msg;
230
231        choose( sfp->si_code ) {
232          case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
233          case FPE_FLTOVF: msg = "overflow";
234          case FPE_FLTUND: msg = "underflow";
235          case FPE_FLTRES: msg = "inexact result";
236          case FPE_FLTINV: msg = "invalid operation";
237          default: msg = "unknown";
238        } // choose
239        abortf( "Computation error %s at location %p.\n", msg, sfp->si_addr );
240}
241
242void sigHandler_abort( __CFA_SIGPARMS__ ) {
243        __cfaabi_backtrace();
244
245        // reset default signal handler
246        __cfaabi_sigdefault( SIGABRT );
247
248        raise( SIGABRT );
249}
250
251// Local Variables: //
252// mode: c //
253// tab-width: 4 //
254// End: //
Note: See TracBrowser for help on using the repository browser.