source: src/libcfa/interpose.c@ 2183e12

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 resolv-new with_gc
Last change on this file since 2183e12 was 9d944b2, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Implemented interposing for abort and exit, implemented safer debug output

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[9d944b2]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
20extern "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 "startup.h"
29
30void abortf( const char *fmt, ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__));
31
32void interpose_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
33
34typedef void (*generic_fptr_t)(void);
35generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
36 const char * error;
37
38 static void * library;
39 if ( ! library ) {
40 #if defined( RTLD_NEXT )
41 library = RTLD_NEXT;
42 #else
43 // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
44 library = dlopen( "libc.so.6", RTLD_LAZY );
45 error = dlerror();
46 if ( error ) {
47 abortf( "interpose_symbol : failed to open libc, %s\n", error );
48 }
49 #endif
50 } // if
51
52 union { generic_fptr_t fptr; void* ptr; } originalFunc;
53
54 #if defined( _GNU_SOURCE )
55 if ( version ) {
56 originalFunc.ptr = dlvsym( library, symbol, version );
57 } else {
58 originalFunc.ptr = dlsym( library, symbol );
59 }
60 #else
61 originalFunc.ptr = dlsym( library, symbol );
62 #endif // _GNU_SOURCE
63
64 error = dlerror();
65 if ( error ) abortf( "interpose_symbol : internal error, %s\n", error );
66
67 return originalFunc.fptr;
68}
69
70
71__typeof__( exit ) libc_exit __attribute__(( noreturn ));
72__typeof__( abort ) libc_abort __attribute__(( noreturn ));
73
74// #define INIT_REALRTN( x, ver ) libc_##x = (__typeof__(libc_##x))interpose_symbol( #x, ver )
75
76forall(dtype T)
77static inline void assign_ptr( T** symbol_ptr, const char * symbol_name, const char * version) {
78 union {
79 generic_fptr_t gp;
80 T* tp;
81 } u;
82
83 u.gp = interpose_symbol( symbol_name, version );
84
85 *symbol_ptr = u.tp;
86}
87
88#define INIT_REALRTN( x, ver ) assign_ptr( (void**)&libc_##x, #x, ver)
89
90void interpose_startup() {
91 const char *version = NULL;
92
93 INIT_REALRTN( abort, version );
94 INIT_REALRTN( exit, version );
95}
96
97extern "C" {
98 void abort (void) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
99 abortf( NULL );
100 }
101
102 void exit (int __status) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
103 libc_exit(__status);
104 }
105}
106
107void abort( const char *fmt, va_list argp ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
108 abortf( fmt, argp );
109}
110
111void * kernel_abort (void) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
112void kernel_abort_msg(void * data, char * buffer, int size) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
113
114enum { abort_text_size = 1024 };
115static char abort_text[ abort_text_size ];
116
117void 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}
Note: See TracBrowser for help on using the repository browser.