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