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