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 Feb 6 17:57:56 2018
|
---|
13 | // Update Count : 49
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <stdarg.h>
|
---|
17 | #include <stddef.h>
|
---|
18 |
|
---|
19 | extern "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 | typedef void (*generic_fptr_t)(void);
|
---|
36 | generic_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 |
|
---|
75 | forall(dtype T)
|
---|
76 | static 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 |
|
---|
89 | void sigHandler_segv ( __CFA_SIGPARMS__ );
|
---|
90 | void sigHandler_ill ( __CFA_SIGPARMS__ );
|
---|
91 | void sigHandler_fpe ( __CFA_SIGPARMS__ );
|
---|
92 | void sigHandler_abort( __CFA_SIGPARMS__ );
|
---|
93 |
|
---|
94 | extern "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 |
|
---|
114 | extern "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 |
|
---|
124 | void 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 |
|
---|
131 | void * kernel_abort ( void ) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
|
---|
132 | void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
|
---|
133 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return 4; }
|
---|
134 |
|
---|
135 | enum { abort_text_size = 1024 };
|
---|
136 | static char abort_text[ abort_text_size ];
|
---|
137 | static int abort_lastframe;
|
---|
138 |
|
---|
139 | extern "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 |
|
---|
166 | static 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 |
|
---|
216 | void sigHandler_segv( __CFA_SIGPARMS__ ) {
|
---|
217 | abortf( "Attempt to address 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 |
|
---|
222 | void sigHandler_ill( __CFA_SIGPARMS__ ) {
|
---|
223 | abortf( "Attempt to execute code at location %p.\n"
|
---|
224 | "Possible cause is stack corruption.\n",
|
---|
225 | sfp->si_addr );
|
---|
226 | }
|
---|
227 |
|
---|
228 | void sigHandler_fpe( __CFA_SIGPARMS__ ) {
|
---|
229 | const char * msg;
|
---|
230 |
|
---|
231 | switch ( sfp->si_code ) {
|
---|
232 | case FPE_INTDIV:
|
---|
233 | case FPE_FLTDIV: msg = "divide by zero"; break;
|
---|
234 | case FPE_FLTOVF: msg = "overflow"; break;
|
---|
235 | case FPE_FLTUND: msg = "underflow"; break;
|
---|
236 | case FPE_FLTRES: msg = "inexact result"; break;
|
---|
237 | case FPE_FLTINV: msg = "invalid operation"; break;
|
---|
238 | default: msg = "unknown";
|
---|
239 | } // switch
|
---|
240 | abortf( "Floating point error.\n"
|
---|
241 | "Cause is %s.\n", msg );
|
---|
242 | }
|
---|
243 |
|
---|
244 | void sigHandler_abort( __CFA_SIGPARMS__ ) {
|
---|
245 | __cfaabi_backtrace();
|
---|
246 |
|
---|
247 | // reset default signal handler
|
---|
248 | __cfaabi_sigdefault( SIGABRT );
|
---|
249 |
|
---|
250 | raise( SIGABRT );
|
---|
251 | }
|
---|
252 |
|
---|
253 | // Local Variables: //
|
---|
254 | // mode: c //
|
---|
255 | // tab-width: 4 //
|
---|
256 | // End: //
|
---|