source: src/libcfa/interpose.c@ 67db067

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 67db067 was 67db067, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

clean up

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