| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2022 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_thread.c -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Wed Sep 21 11:55:16 2022 | 
|---|
| 11 | // Last Modified By : | 
|---|
| 12 | // Last Modified On : | 
|---|
| 13 | // Update Count     : | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #ifdef __i386__                                                                                 // 32-bit architecture | 
|---|
| 17 | #undef _GNU_SOURCE | 
|---|
| 18 | #endif // __i386__ | 
|---|
| 19 |  | 
|---|
| 20 | #include <signal.h> | 
|---|
| 21 | #include <pthread.h> | 
|---|
| 22 | #include <signal.h> | 
|---|
| 23 | extern "C" { | 
|---|
| 24 | #include <dlfcn.h>                                                                              // dlopen, dlsym | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | #include "bits/defs.hfa" | 
|---|
| 28 |  | 
|---|
| 29 | //============================================================================================= | 
|---|
| 30 | // Interposing helpers | 
|---|
| 31 | //============================================================================================= | 
|---|
| 32 |  | 
|---|
| 33 | typedef void (* generic_fptr_t)(void); | 
|---|
| 34 |  | 
|---|
| 35 | generic_fptr_t interpose_symbol( | 
|---|
| 36 | generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ), | 
|---|
| 37 | const char symbol[], | 
|---|
| 38 | const char version[] | 
|---|
| 39 | ) libcfa_public { | 
|---|
| 40 | void * library; | 
|---|
| 41 |  | 
|---|
| 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( "libpthread.so", RTLD_LAZY ); | 
|---|
| 47 | if ( ! library ) {                                                                      // == nullptr | 
|---|
| 48 | abort( "interpose_symbol : failed to open libpthread, %s\n", dlerror() ); | 
|---|
| 49 | } // if | 
|---|
| 50 | #endif // RTLD_NEXT | 
|---|
| 51 |  | 
|---|
| 52 | return do_interpose_symbol( library, symbol, version ); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | #define INTERPOSE( x, ver ) __cabi_libpthread.x = (typeof(__cabi_libpthread.x))interpose_symbol( do_interpose_symbol, #x, ver ) | 
|---|
| 56 |  | 
|---|
| 57 | //============================================================================================= | 
|---|
| 58 | // Interposition Startup logic | 
|---|
| 59 | //============================================================================================= | 
|---|
| 60 |  | 
|---|
| 61 | static struct { | 
|---|
| 62 | int (*pthread_create)(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); | 
|---|
| 63 | int (*pthread_join)(pthread_t _thread, void **retval); | 
|---|
| 64 | pthread_t (*pthread_self)(void); | 
|---|
| 65 | int (*pthread_attr_init)(pthread_attr_t *attr); | 
|---|
| 66 | int (*pthread_attr_destroy)(pthread_attr_t *attr); | 
|---|
| 67 | int (*pthread_attr_setstack)( pthread_attr_t *attr, void *stackaddr, size_t stacksize ); | 
|---|
| 68 | int (*pthread_attr_getstacksize)( const pthread_attr_t *attr, size_t *stacksize ); | 
|---|
| 69 | int (*pthread_sigmask)(int how, const sigset_t *set, sigset_t *oldset); | 
|---|
| 70 | int (*pthread_sigqueue)(pthread_t _thread, int sig, const union sigval value); | 
|---|
| 71 | int (*pthread_once)(pthread_once_t *once_control, void (*init_routine)(void)); | 
|---|
| 72 | } __cabi_libpthread; | 
|---|
| 73 |  | 
|---|
| 74 | extern "C" { | 
|---|
| 75 | void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) libcfa_public { | 
|---|
| 76 | const char *version = 0p; | 
|---|
| 77 |  | 
|---|
| 78 | #pragma GCC diagnostic push | 
|---|
| 79 | #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" | 
|---|
| 80 | INTERPOSE( pthread_create, version ); | 
|---|
| 81 | INTERPOSE( pthread_join, version ); | 
|---|
| 82 | INTERPOSE( pthread_self, version ); | 
|---|
| 83 | INTERPOSE( pthread_attr_init, version ); | 
|---|
| 84 | INTERPOSE( pthread_attr_destroy, version ); | 
|---|
| 85 | INTERPOSE( pthread_attr_setstack, version ); | 
|---|
| 86 | INTERPOSE( pthread_attr_getstacksize, version ); | 
|---|
| 87 | INTERPOSE( pthread_sigmask, version ); | 
|---|
| 88 | INTERPOSE( pthread_sigqueue, version ); | 
|---|
| 89 | INTERPOSE( pthread_once, version ); | 
|---|
| 90 | #pragma GCC diagnostic pop | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){ | 
|---|
| 94 | return __cabi_libpthread.pthread_create(_thread, attr, start_routine, arg); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | int __cfaabi_pthread_join(pthread_t _thread, void **retval){ | 
|---|
| 98 | return __cabi_libpthread.pthread_join(_thread, retval); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | pthread_t __cfaabi_pthread_self(void){ | 
|---|
| 102 | return __cabi_libpthread.pthread_self(); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | int __cfaabi_pthread_attr_init(pthread_attr_t *attr){ | 
|---|
| 106 | return __cabi_libpthread.pthread_attr_init(attr); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr){ | 
|---|
| 110 | return __cabi_libpthread.pthread_attr_destroy(attr); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){ | 
|---|
| 114 | return __cabi_libpthread.pthread_attr_setstack(attr, stackaddr, stacksize); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | int read_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){ | 
|---|
| 118 | return __cabi_libpthread.pthread_attr_getstacksize(attr, stacksize); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | int __cfaabi_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset){ | 
|---|
| 122 | return __cabi_libpthread.pthread_sigmask(how, set, oldset); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value) { | 
|---|
| 126 | return __cabi_libpthread.pthread_sigqueue(_thread, sig, value); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | int __cfaabi_pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { | 
|---|
| 130 | return __cabi_libpthread.pthread_once(once_control, init_routine); | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|