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