source: src/libcfa/interpose.c@ cfc3e0f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since cfc3e0f was 7867eb9, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update includes

  • Property mode set to 100644
File size: 8.5 KB
Line 
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
22extern "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
36typedef void (* generic_fptr_t)(void);
37generic_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
78void sigHandler_segv ( __CFA_SIGPARMS__ );
79void sigHandler_ill ( __CFA_SIGPARMS__ );
80void sigHandler_fpe ( __CFA_SIGPARMS__ );
81void sigHandler_abort( __CFA_SIGPARMS__ );
82void sigHandler_term ( __CFA_SIGPARMS__ );
83
84struct {
85 void (* exit)( int ) __attribute__(( __noreturn__ ));
86 void (* abort)( void ) __attribute__(( __noreturn__ ));
87} __cabi_libc;
88
89extern "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 __cfaabi_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // Failure handler
101 __cfaabi_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); // Failure handler
102 __cfaabi_sigaction( SIGILL , sigHandler_ill , SA_SIGINFO ); // Failure handler
103 __cfaabi_sigaction( SIGFPE , sigHandler_fpe , SA_SIGINFO ); // Failure handler
104 __cfaabi_sigaction( SIGABRT, sigHandler_abort, SA_SIGINFO ); // Failure handler
105 __cfaabi_sigaction( SIGTERM, sigHandler_term , SA_SIGINFO ); // Failure handler
106 __cfaabi_sigaction( SIGINT , sigHandler_term , SA_SIGINFO ); // Failure handler
107 }
108}
109
110//=============================================================================================
111// Terminating Signals logic
112//=============================================================================================
113
114// Forward declare abort after the __typeof__ call to avoid ambiguities
115void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
116void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
117
118extern "C" {
119 void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
120 abort( NULL );
121 }
122
123 void __cabi_abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
124 va_list argp;
125 va_start( argp, fmt );
126 abort( fmt, argp );
127 va_end( argp );
128 }
129
130 void exit( int status ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
131 __cabi_libc.exit( status );
132 }
133}
134
135void * kernel_abort ( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return NULL; }
136void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
137int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
138
139enum { abort_text_size = 1024 };
140static char abort_text[ abort_text_size ];
141static int abort_lastframe;
142
143void exit( int status, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )) {
144 va_list args;
145 va_start( args, fmt );
146 vfprintf( stderr, fmt, args );
147 va_end( args );
148 __cabi_libc.exit( status );
149}
150
151void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )) {
152 void * kernel_data = kernel_abort(); // must be done here to lock down kernel
153 int len;
154
155 abort_lastframe = kernel_abort_lastframe();
156 len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
157 __cfaabi_dbg_bits_write( abort_text, len );
158
159 if ( fmt ) {
160 va_list args;
161 va_start( args, fmt );
162
163 len = vsnprintf( abort_text, abort_text_size, fmt, args );
164 va_end( args );
165 __cfaabi_dbg_bits_write( abort_text, len );
166
167 if ( fmt[strlen( fmt ) - 1] != '\n' ) { // add optional newline if missing at the end of the format text
168 __cfaabi_dbg_bits_write( "\n", 1 );
169 }
170 }
171
172 kernel_abort_msg( kernel_data, abort_text, abort_text_size );
173 __cabi_libc.abort();
174}
175
176static void __cfaabi_backtrace() {
177 enum {
178 Frames = 50, // maximum number of stack frames
179 Start = 8, // skip first N stack frames
180 };
181
182 void * array[Frames];
183 size_t size = backtrace( array, Frames );
184 char ** messages = backtrace_symbols( array, size );
185
186 // find executable name
187 *index( messages[0], '(' ) = '\0';
188 __cfaabi_dbg_bits_print_nolock( "Stack back trace for: %s\n", messages[0]);
189
190 for ( int i = Start; i < size - abort_lastframe && messages != NULL; i += 1 ) {
191 char * name = NULL, * offset_begin = NULL, * offset_end = NULL;
192
193 for ( char * p = messages[i]; *p; ++p ) {
194 //__cfaabi_dbg_bits_print_nolock( "X %s\n", p);
195 // find parantheses and +offset
196 if ( *p == '(' ) {
197 name = p;
198 }
199 else if ( *p == '+' ) {
200 offset_begin = p;
201 }
202 else if ( *p == ')' ) {
203 offset_end = p;
204 break;
205 }
206 }
207
208 // if line contains symbol print it
209 int frameNo = i - Start;
210 if ( name && offset_begin && offset_end && name < offset_begin ) {
211 // delimit strings
212 *name++ = '\0';
213 *offset_begin++ = '\0';
214 *offset_end++ = '\0';
215
216 __cfaabi_dbg_bits_print_nolock( "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
217 }
218 // otherwise, print the whole line
219 else {
220 __cfaabi_dbg_bits_print_nolock( "(%i) %s\n", frameNo, messages[i] );
221 }
222 }
223 free( messages );
224}
225
226void sigHandler_segv( __CFA_SIGPARMS__ ) {
227 abort( "Addressing invalid memory at location %p\n"
228 "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",
229 sfp->si_addr );
230}
231
232void sigHandler_ill( __CFA_SIGPARMS__ ) {
233 abort( "Executing illegal instruction at location %p.\n"
234 "Possible cause is stack corruption.\n",
235 sfp->si_addr );
236}
237
238void sigHandler_fpe( __CFA_SIGPARMS__ ) {
239 const char * msg;
240
241 choose( sfp->si_code ) {
242 case FPE_INTDIV, FPE_FLTDIV: msg = "divide by zero";
243 case FPE_FLTOVF: msg = "overflow";
244 case FPE_FLTUND: msg = "underflow";
245 case FPE_FLTRES: msg = "inexact result";
246 case FPE_FLTINV: msg = "invalid operation";
247 default: msg = "unknown";
248 } // choose
249 abort( "Computation error %s at location %p.\n", msg, sfp->si_addr );
250}
251
252void sigHandler_abort( __CFA_SIGPARMS__ ) {
253 __cfaabi_backtrace();
254
255 // reset default signal handler
256 __cfaabi_sigdefault( SIGABRT );
257
258 raise( SIGABRT );
259}
260
261void sigHandler_term( __CFA_SIGPARMS__ ) {
262 abort( "Application stopped by %s signal.", sig == SIGINT ? "an interrupt (SIGINT)" : "a terminate (SIGTERM)" );
263}
264
265// Local Variables: //
266// mode: c //
267// tab-width: 4 //
268// End: //
Note: See TracBrowser for help on using the repository browser.