| 1 | //                              -*- Mode: CFA -*-
 | 
|---|
| 2 | //
 | 
|---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
 | 
|---|
| 4 | //
 | 
|---|
| 5 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 6 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 7 | //
 | 
|---|
| 8 | // interpose.c --
 | 
|---|
| 9 | //
 | 
|---|
| 10 | // Author           : Thierry Delisle
 | 
|---|
| 11 | // Created On       : Wed Mar 29 16:10:31 2017
 | 
|---|
| 12 | // Last Modified By :
 | 
|---|
| 13 | // Last Modified On :
 | 
|---|
| 14 | // Update Count     : 0
 | 
|---|
| 15 | //
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <stdarg.h>
 | 
|---|
| 18 | #include <stddef.h>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | extern "C" {
 | 
|---|
| 21 | #include <stdio.h>
 | 
|---|
| 22 | #include <string.h>
 | 
|---|
| 23 | #include <dlfcn.h>
 | 
|---|
| 24 | #include <unistd.h>
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "libhdr/libdebug.h"
 | 
|---|
| 28 | #include "libhdr/libtools.h"
 | 
|---|
| 29 | #include "startup.h"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | void interpose_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
 | 
|---|
| 32 | 
 | 
|---|
| 33 | typedef void (*generic_fptr_t)(void);
 | 
|---|
| 34 | generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
 | 
|---|
| 35 |         const char * error;
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         static void * library;
 | 
|---|
| 38 |         if ( ! library ) {
 | 
|---|
| 39 |                 #if defined( RTLD_NEXT )
 | 
|---|
| 40 |                         library = RTLD_NEXT;
 | 
|---|
| 41 |                 #else
 | 
|---|
| 42 |                         // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
 | 
|---|
| 43 |                         library = dlopen( "libc.so.6", RTLD_LAZY );
 | 
|---|
| 44 |                         error = dlerror();
 | 
|---|
| 45 |                         if ( error ) {
 | 
|---|
| 46 |                                 abortf( "interpose_symbol : failed to open libc, %s\n", error );
 | 
|---|
| 47 |                         }
 | 
|---|
| 48 |                 #endif
 | 
|---|
| 49 |         } // if
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         union { generic_fptr_t fptr; void* ptr; } originalFunc;
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         #if defined( _GNU_SOURCE )
 | 
|---|
| 54 |                 if ( version ) {
 | 
|---|
| 55 |                         originalFunc.ptr = dlvsym( library, symbol, version );
 | 
|---|
| 56 |                 } else {
 | 
|---|
| 57 |                         originalFunc.ptr = dlsym( library, symbol );
 | 
|---|
| 58 |                 }
 | 
|---|
| 59 |         #else
 | 
|---|
| 60 |                 originalFunc.ptr = dlsym( library, symbol );
 | 
|---|
| 61 |         #endif // _GNU_SOURCE
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         error = dlerror();
 | 
|---|
| 64 |         if ( error ) abortf( "interpose_symbol : internal error, %s\n", error );
 | 
|---|
| 65 | 
 | 
|---|
| 66 |         return originalFunc.fptr;
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | 
 | 
|---|
| 70 | __typeof__( exit ) libc_exit __attribute__(( noreturn ));
 | 
|---|
| 71 | __typeof__( abort ) libc_abort __attribute__(( noreturn ));
 | 
|---|
| 72 | 
 | 
|---|
| 73 | // #define INIT_REALRTN( x, ver ) libc_##x = (__typeof__(libc_##x))interpose_symbol( #x, ver )
 | 
|---|
| 74 | 
 | 
|---|
| 75 | forall(dtype T)
 | 
|---|
| 76 | static inline void assign_ptr( T** symbol_ptr, const char * symbol_name, const char * version) {
 | 
|---|
| 77 |         union {
 | 
|---|
| 78 |                 generic_fptr_t gp;
 | 
|---|
| 79 |                 T* tp;
 | 
|---|
| 80 |         } u;
 | 
|---|
| 81 | 
 | 
|---|
| 82 |         u.gp = interpose_symbol( symbol_name, version );
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         *symbol_ptr = u.tp;
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | #define INIT_REALRTN( x, ver ) assign_ptr( (void**)&libc_##x, #x, ver)
 | 
|---|
| 88 | 
 | 
|---|
| 89 | void interpose_startup() {
 | 
|---|
| 90 |         const char *version = NULL;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         INIT_REALRTN( abort, version );
 | 
|---|
| 93 |         INIT_REALRTN( exit, version );
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | extern "C" {
 | 
|---|
| 97 |         void abort (void) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
 | 
|---|
| 98 |                 abortf( NULL );
 | 
|---|
| 99 |         }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         void exit (int __status) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
 | 
|---|
| 102 |                 libc_exit(__status);
 | 
|---|
| 103 |         }
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | void abort( const char *fmt, va_list argp ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
 | 
|---|
| 107 |         abortf( fmt, argp );
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | void * kernel_abort    (void) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
 | 
|---|
| 111 | void   kernel_abort_msg(void * data, char * buffer, int size) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
 | 
|---|
| 112 | 
 | 
|---|
| 113 | enum { abort_text_size = 1024 };
 | 
|---|
| 114 | static char abort_text[ abort_text_size ];
 | 
|---|
| 115 | 
 | 
|---|
| 116 | extern "C" {
 | 
|---|
| 117 |         void abortf( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
 | 
|---|
| 118 |                 void * kernel_data = kernel_abort();
 | 
|---|
| 119 | 
 | 
|---|
| 120 |                 int len;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |                 if( fmt ) {
 | 
|---|
| 123 |                         va_list args;
 | 
|---|
| 124 |                         va_start( args, fmt );
 | 
|---|
| 125 | 
 | 
|---|
| 126 |                         len = vsnprintf( abort_text, abort_text_size, fmt, args );
 | 
|---|
| 127 | 
 | 
|---|
| 128 |                         va_end( args );
 | 
|---|
| 129 | 
 | 
|---|
| 130 |                         __lib_debug_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| 131 |                         __lib_debug_write( STDERR_FILENO, "\n", 1 );
 | 
|---|
| 132 |                 }
 | 
|---|
| 133 | 
 | 
|---|
| 134 |                 len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld)\n", (long int)getpid() ); // use UNIX pid (versus getPid)
 | 
|---|
| 135 |                 __lib_debug_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| 136 | 
 | 
|---|
| 137 | 
 | 
|---|
| 138 |                 kernel_abort_msg( kernel_data, abort_text, abort_text_size );
 | 
|---|
| 139 | 
 | 
|---|
| 140 |                 libc_abort();
 | 
|---|
| 141 |         }
 | 
|---|
| 142 | }
 | 
|---|