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 : Sun Feb 19 17:09:16 2023 |
---|
13 | // Update Count : 183 |
---|
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 | #include <signal.h> |
---|
21 | extern "C" { |
---|
22 | #include <dlfcn.h> // dlopen, dlsym |
---|
23 | #include <execinfo.h> // backtrace, messages |
---|
24 | } |
---|
25 | |
---|
26 | #include "bits/debug.hfa" |
---|
27 | #include "bits/defs.hfa" |
---|
28 | #include "bits/signal.hfa" // sigHandler_? |
---|
29 | #include "startup.hfa" // STARTUP_PRIORITY_CORE |
---|
30 | #include <assert.h> |
---|
31 | |
---|
32 | //============================================================================================= |
---|
33 | // Interposing helpers |
---|
34 | //============================================================================================= |
---|
35 | |
---|
36 | static 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 | static generic_fptr_t do_interpose_symbol( void * library, const char symbol[], const char version[] ) { |
---|
43 | const char * error; |
---|
44 | |
---|
45 | union { generic_fptr_t fptr; void * ptr; } originalFunc; |
---|
46 | |
---|
47 | originalFunc.ptr = dlsym( library, symbol ); |
---|
48 | error = dlerror(); |
---|
49 | if ( error ) abort( "interpose_symbol : internal error, %s\n", error ); |
---|
50 | |
---|
51 | return originalFunc.fptr; |
---|
52 | } |
---|
53 | |
---|
54 | static generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) { |
---|
55 | static void * library; |
---|
56 | static void * pthread_library; |
---|
57 | |
---|
58 | if ( ! library ) { |
---|
59 | library = dlopen( "libc.so.6", RTLD_LAZY ); |
---|
60 | const char * error = dlerror(); |
---|
61 | if ( error ) { |
---|
62 | abort( "interpose_symbol : failed to open libc, %s\n", error ); |
---|
63 | } // if |
---|
64 | } // if |
---|
65 | if ( ! pthread_library ) { |
---|
66 | pthread_library = dlopen( "libpthread.so", RTLD_LAZY ); |
---|
67 | const char * error = dlerror(); |
---|
68 | if ( error ) { |
---|
69 | abort( "interpose_symbol : failed to open libpthread, %s\n", error ); |
---|
70 | } // if |
---|
71 | } // if |
---|
72 | |
---|
73 | return do_interpose_symbol(library, symbol, version); |
---|
74 | } |
---|
75 | |
---|
76 | #define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver ) |
---|
77 | |
---|
78 | //============================================================================================= |
---|
79 | // Interposition Startup logic |
---|
80 | //============================================================================================= |
---|
81 | |
---|
82 | static void sigHandler_segv( __CFA_SIGPARMS__ ); |
---|
83 | static void sigHandler_ill ( __CFA_SIGPARMS__ ); |
---|
84 | static void sigHandler_fpe ( __CFA_SIGPARMS__ ); |
---|
85 | static void sigHandler_abrt( __CFA_SIGPARMS__ ); |
---|
86 | static void sigHandler_term( __CFA_SIGPARMS__ ); |
---|
87 | |
---|
88 | static struct { |
---|
89 | void (* exit)( int ) __attribute__(( __noreturn__ )); |
---|
90 | void (* abort)( void ) __attribute__(( __noreturn__ )); |
---|
91 | } __cabi_libc; |
---|
92 | |
---|
93 | libcfa_public int cfa_main_returned; |
---|
94 | |
---|
95 | extern "C" { |
---|
96 | void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) __attribute__((weak)); |
---|
97 | void __cfaabi_interpose_startup( void ) { |
---|
98 | const char *version = 0p; |
---|
99 | cfa_main_returned = 0; |
---|
100 | |
---|
101 | preload_libgcc(); |
---|
102 | |
---|
103 | #pragma GCC diagnostic push |
---|
104 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" |
---|
105 | INTERPOSE_LIBC( abort, version ); |
---|
106 | INTERPOSE_LIBC( exit , version ); |
---|
107 | #pragma GCC diagnostic pop |
---|
108 | |
---|
109 | if(__cfathreadabi_interpose_startup) __cfathreadabi_interpose_startup( do_interpose_symbol ); |
---|
110 | |
---|
111 | // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because |
---|
112 | // task stacks might be very small (4K) and the signal delivery corrupts memory to the point that a clean |
---|
113 | // shutdown is impossible. Also, when a stack overflow encounters the non-accessible sentinel page (debug only) |
---|
114 | // and generates a segment fault, the signal cannot be delivered on the sentinel page. Finally, calls to abort |
---|
115 | // print a stack trace that uses substantial stack space. |
---|
116 | |
---|
117 | #define MINSTKSZ SIGSTKSZ * 8 |
---|
118 | static char stack[MINSTKSZ] __attribute__(( aligned (16) )); |
---|
119 | static stack_t ss; |
---|
120 | |
---|
121 | ss.ss_sp = stack; |
---|
122 | ss.ss_size = MINSTKSZ; |
---|
123 | ss.ss_flags = 0; |
---|
124 | if ( sigaltstack( &ss, 0p ) == -1 ) { |
---|
125 | abort( "__cfaabi_interpose_startup : internal error, sigaltstack error(%d) %s.", errno, strerror( errno ) ); |
---|
126 | } // if |
---|
127 | |
---|
128 | // Failure handler |
---|
129 | // internal errors |
---|
130 | __cfaabi_sigaction( SIGSEGV, sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Invalid memory reference (default: Core) |
---|
131 | __cfaabi_sigaction( SIGBUS , sigHandler_segv, SA_SIGINFO | SA_ONSTACK ); // Bus error, bad memory access (default: Core) |
---|
132 | __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO | SA_ONSTACK ); // Illegal Instruction (default: Core) |
---|
133 | __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO | SA_ONSTACK ); // Floating-point exception (default: Core) |
---|
134 | |
---|
135 | // handlers to outside errors |
---|
136 | // reset in-case they insist and send it over and over |
---|
137 | __cfaabi_sigaction( SIGTERM, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Termination signal (default: Term) |
---|
138 | __cfaabi_sigaction( SIGINT , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Interrupt from keyboard (default: Term) |
---|
139 | __cfaabi_sigaction( SIGHUP , sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Hangup detected on controlling terminal or death of controlling process (default: Term) |
---|
140 | __cfaabi_sigaction( SIGQUIT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Quit from keyboard (default: Core) |
---|
141 | __cfaabi_sigaction( SIGABRT, sigHandler_term, SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ); // Abort signal from abort(3) (default: Core) |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | //============================================================================================= |
---|
146 | // Terminating Signals logic |
---|
147 | //============================================================================================= |
---|
148 | |
---|
149 | // Forward declare abort after the __typeof__ call to avoid ambiguities |
---|
150 | libcfa_public void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); |
---|
151 | libcfa_public void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); |
---|
152 | libcfa_public void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); |
---|
153 | libcfa_public void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )); |
---|
154 | |
---|
155 | extern "C" { |
---|
156 | libcfa_public void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) { |
---|
157 | abort( false, "%s", "" ); |
---|
158 | } |
---|
159 | |
---|
160 | libcfa_public void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) { |
---|
161 | va_list argp; |
---|
162 | va_start( argp, fmt ); |
---|
163 | __abort( false, fmt, argp ); |
---|
164 | va_end( argp ); |
---|
165 | } |
---|
166 | |
---|
167 | libcfa_public void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) { |
---|
168 | __cabi_libc.exit( status ); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | // See concurrency/kernel.cfa and concurrency/preemption.cfa for strong definition used in multi-processor mode. |
---|
173 | void __kernel_abort_lock( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {} |
---|
174 | void __kernel_abort_msg( char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {} |
---|
175 | int __kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; } |
---|
176 | |
---|
177 | enum { abort_text_size = 1024 }; |
---|
178 | static char abort_text[ abort_text_size ]; |
---|
179 | |
---|
180 | static void __cfaabi_backtrace( int start ) { |
---|
181 | enum { Frames = 50, }; // maximum number of stack frames |
---|
182 | int last = __kernel_abort_lastframe(); // skip last N stack frames |
---|
183 | |
---|
184 | void * array[Frames]; |
---|
185 | size_t size = backtrace( array, Frames ); |
---|
186 | char ** messages = backtrace_symbols( array, size ); // does not demangle names |
---|
187 | |
---|
188 | *index( messages[0], '(' ) = '\0'; // find executable name |
---|
189 | __cfaabi_bits_print_nolock( STDERR_FILENO, "Stack back trace for: %s\n", messages[0]); |
---|
190 | |
---|
191 | for ( unsigned int i = start; i < size - last && messages != 0p; i += 1 ) { |
---|
192 | char * name = 0p, * offset_begin = 0p, * offset_end = 0p; |
---|
193 | |
---|
194 | for ( char * p = messages[i]; *p; p += 1 ) { // find parantheses and +offset |
---|
195 | //__cfaabi_bits_print_nolock( "X %s\n", p); |
---|
196 | if ( *p == '(' ) { |
---|
197 | name = p; |
---|
198 | } else if ( *p == '+' ) { |
---|
199 | offset_begin = p; |
---|
200 | } else if ( *p == ')' ) { |
---|
201 | offset_end = p; |
---|
202 | break; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | // if line contains symbol, print it |
---|
207 | int frameNo = i - start; |
---|
208 | if ( name && offset_begin && offset_end && name < offset_begin ) { |
---|
209 | *name++ = '\0'; // delimit strings |
---|
210 | *offset_begin++ = '\0'; |
---|
211 | *offset_end++ = '\0'; |
---|
212 | |
---|
213 | __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end); |
---|
214 | } else { // otherwise, print the whole line |
---|
215 | __cfaabi_bits_print_nolock( STDERR_FILENO, "(%i) %s\n", frameNo, messages[i] ); |
---|
216 | } |
---|
217 | } |
---|
218 | free( messages ); |
---|
219 | } |
---|
220 | |
---|
221 | void exit( int status, const char fmt[], ... ) { |
---|
222 | va_list args; |
---|
223 | va_start( args, fmt ); |
---|
224 | vfprintf( stderr, fmt, args ); |
---|
225 | va_end( args ); |
---|
226 | __cabi_libc.exit( status ); |
---|
227 | } |
---|
228 | |
---|
229 | static volatile bool __abort_first = 0; |
---|
230 | |
---|
231 | // Cannot forward va_list. |
---|
232 | void __abort( bool signalAbort, const char fmt[], va_list args ) { |
---|
233 | // Multiple threads can come here from multiple paths |
---|
234 | // To make sure this is safe any concurrent/subsequent call to abort is redirected to libc-abort |
---|
235 | bool first = ! __atomic_test_and_set( &__abort_first, __ATOMIC_SEQ_CST); |
---|
236 | |
---|
237 | // Prevent preemption from kicking-in and messing with the abort |
---|
238 | __kernel_abort_lock(); |
---|
239 | |
---|
240 | // first to abort ? |
---|
241 | if ( !first ) { |
---|
242 | // We aren't the first to abort just let C handle it |
---|
243 | signal( SIGABRT, SIG_DFL ); // restore default in case we came here through the function. |
---|
244 | __cabi_libc.abort(); |
---|
245 | } |
---|
246 | |
---|
247 | int len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid) |
---|
248 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
249 | |
---|
250 | // print the cause of the error |
---|
251 | assert( fmt ); |
---|
252 | len = vsnprintf( abort_text, abort_text_size, fmt, args ); |
---|
253 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
254 | |
---|
255 | // add optional newline if missing at the end of the format text |
---|
256 | if ( fmt[strlen( fmt ) - 1] != '\n' ) { |
---|
257 | __cfaabi_bits_write( STDERR_FILENO, "\n", 1 ); |
---|
258 | } // if |
---|
259 | |
---|
260 | // Give the kernel the chance to add some data in here |
---|
261 | __kernel_abort_msg( abort_text, abort_text_size ); |
---|
262 | |
---|
263 | // print stack trace in handler |
---|
264 | __cfaabi_backtrace( signalAbort ? 4 : 2 ); |
---|
265 | |
---|
266 | // Finally call abort |
---|
267 | __cabi_libc.abort(); |
---|
268 | |
---|
269 | } |
---|
270 | |
---|
271 | void abort( const char fmt[], ... ) { |
---|
272 | va_list args; |
---|
273 | va_start( args, fmt ); |
---|
274 | __abort( false, fmt, args ); |
---|
275 | // CONTROL NEVER REACHES HERE! |
---|
276 | va_end( args ); |
---|
277 | } |
---|
278 | |
---|
279 | void abort( bool signalAbort, const char fmt[], ... ) { |
---|
280 | va_list args; |
---|
281 | va_start( args, fmt ); |
---|
282 | __abort( signalAbort, fmt, args ); |
---|
283 | // CONTROL NEVER REACHES HERE! |
---|
284 | va_end( args ); |
---|
285 | } |
---|
286 | |
---|
287 | void sigHandler_segv( __CFA_SIGPARMS__ ) { |
---|
288 | if ( sfp->si_addr == 0p ) { |
---|
289 | abort( true, "Null pointer (0p) dereference.\n" ); |
---|
290 | } else { |
---|
291 | abort( true, "%s at memory location %p.\n" |
---|
292 | "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", |
---|
293 | (sig == SIGSEGV ? "Segment fault" : "Bus error"), sfp->si_addr ); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | void sigHandler_ill( __CFA_SIGPARMS__ ) { |
---|
298 | abort( true, "Executing illegal instruction at location %p.\n" |
---|
299 | "Possible cause is stack corruption.\n", |
---|
300 | sfp->si_addr ); |
---|
301 | } |
---|
302 | |
---|
303 | void sigHandler_fpe( __CFA_SIGPARMS__ ) { |
---|
304 | const char * msg; |
---|
305 | |
---|
306 | choose( sfp->si_code ) { |
---|
307 | case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero"; |
---|
308 | case FPE_FLTOVF: msg = "overflow"; |
---|
309 | case FPE_FLTUND: msg = "underflow"; |
---|
310 | case FPE_FLTRES: msg = "inexact result"; |
---|
311 | case FPE_FLTINV: msg = "invalid operation"; |
---|
312 | default: msg = "unknown"; |
---|
313 | } // choose |
---|
314 | abort( true, "Computation error %s at location %p.\n", msg, sfp->si_addr ); |
---|
315 | } |
---|
316 | |
---|
317 | void sigHandler_term( __CFA_SIGPARMS__ ) { |
---|
318 | abort( true, "Application interrupted by signal: %s.\n", strsignal( sig ) ); |
---|
319 | } |
---|
320 | |
---|
321 | // Local Variables: // |
---|
322 | // mode: c // |
---|
323 | // tab-width: 4 // |
---|
324 | // End: // |
---|