source: src/libcfa/interpose.c @ 9ef4dcc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9ef4dcc was 9236060, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 3.6 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 Jul 21 22:27:33 2017
13// Update Count     : 1
14//
15
16#include <stdarg.h>
17#include <stddef.h>
18
19extern "C" {
20#include <stdio.h>
21#include <string.h>
22#include <dlfcn.h>
23#include <unistd.h>
24}
25
26#include "libhdr/libdebug.h"
27#include "libhdr/libtools.h"
28#include "startup.h"
29
30void interpose_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
31
32typedef void (*generic_fptr_t)(void);
33generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
34        const char * error;
35
36        static void * library;
37        if ( ! library ) {
38                #if defined( RTLD_NEXT )
39                        library = RTLD_NEXT;
40                #else
41                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
42                        library = dlopen( "libc.so.6", RTLD_LAZY );
43                        error = dlerror();
44                        if ( error ) {
45                                abortf( "interpose_symbol : failed to open libc, %s\n", error );
46                        }
47                #endif
48        } // if
49
50        union { generic_fptr_t fptr; void* ptr; } originalFunc;
51
52        #if defined( _GNU_SOURCE )
53                if ( version ) {
54                        originalFunc.ptr = dlvsym( library, symbol, version );
55                } else {
56                        originalFunc.ptr = dlsym( library, symbol );
57                }
58        #else
59                originalFunc.ptr = dlsym( library, symbol );
60        #endif // _GNU_SOURCE
61
62        error = dlerror();
63        if ( error ) abortf( "interpose_symbol : internal error, %s\n", error );
64
65        return originalFunc.fptr;
66}
67
68
69__typeof__( exit ) libc_exit __attribute__(( noreturn ));
70__typeof__( abort ) libc_abort __attribute__(( noreturn ));
71
72// #define INIT_REALRTN( x, ver ) libc_##x = (__typeof__(libc_##x))interpose_symbol( #x, ver )
73
74forall(dtype T)
75static inline void assign_ptr( T** symbol_ptr, const char * symbol_name, const char * version) {
76        union {
77                generic_fptr_t gp;
78                T* tp;
79        } u;
80
81        u.gp = interpose_symbol( symbol_name, version );
82
83        *symbol_ptr = u.tp;
84}
85
86#define INIT_REALRTN( x, ver ) assign_ptr( (void**)&libc_##x, #x, ver)
87
88void interpose_startup() {
89        const char *version = NULL;
90
91        INIT_REALRTN( abort, version );
92        INIT_REALRTN( exit, version );
93}
94
95extern "C" {
96        void abort (void) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
97                abortf( NULL );
98        }
99
100        void exit (int __status) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
101                libc_exit(__status);
102        }
103}
104
105void abort( const char *fmt, va_list argp ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
106        abortf( fmt, argp );
107}
108
109void * kernel_abort    (void) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
110void   kernel_abort_msg(void * data, char * buffer, int size) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
111
112enum { abort_text_size = 1024 };
113static char abort_text[ abort_text_size ];
114
115extern "C" {
116        void abortf( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
117                void * kernel_data = kernel_abort();
118
119                int len;
120
121                if( fmt ) {
122                        va_list args;
123                        va_start( args, fmt );
124
125                        len = vsnprintf( abort_text, abort_text_size, fmt, args );
126
127                        va_end( args );
128
129                        __lib_debug_write( STDERR_FILENO, abort_text, len );
130                        __lib_debug_write( STDERR_FILENO, "\n", 1 );
131                }
132
133                len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld)\n", (long int)getpid() ); // use UNIX pid (versus getPid)
134                __lib_debug_write( STDERR_FILENO, abort_text, len );
135
136
137                kernel_abort_msg( kernel_data, abort_text, abort_text_size );
138
139                libc_abort();
140        }
141}
142
143// Local Variables: //
144// mode: c //
145// tab-width: 4 //
146// End: //
Note: See TracBrowser for help on using the repository browser.