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