source: libcfa/src/interpose_thread.cfa@ 416b443

Last change on this file since 416b443 was e10714a, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

update interpose code, and document 32-bit interpose bug in glibc

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[95dab9e]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
[e10714a]16// BUG in 32-bit gcc with interpose: fixed in >= gcc-9.5, gcc-10.4, gcc-12.2
[089a0d7]17#ifdef __i386__ // 32-bit architecture
18#undef _GNU_SOURCE
19#endif // __i386__
20
[9cd5bd2]21#include <signal.h>
22#include <pthread.h>
[089a0d7]23#include <signal.h>
[95dab9e]24extern "C" {
25#include <dlfcn.h> // dlopen, dlsym
26}
27
28#include "bits/defs.hfa"
29
30//=============================================================================================
31// Interposing helpers
32//=============================================================================================
33
34typedef void (* generic_fptr_t)(void);
35
[e10714a]36generic_fptr_t libcfa_public interpose_symbol(
[95dab9e]37 generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ),
38 const char symbol[],
39 const char version[]
[e10714a]40) {
[8bb46d2]41 void * library;
[089a0d7]42
[8bb46d2]43 #if defined( RTLD_NEXT )
44 library = RTLD_NEXT;
45 #else
46 // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
[089a0d7]47 library = dlopen( "libpthread.so", RTLD_LAZY );
48 if ( ! library ) { // == nullptr
49 abort( "interpose_symbol : failed to open libpthread, %s\n", dlerror() );
[95dab9e]50 } // if
[8bb46d2]51 #endif // RTLD_NEXT
[95dab9e]52
[089a0d7]53 return do_interpose_symbol( library, symbol, version );
[95dab9e]54}
55
56#define INTERPOSE( x, ver ) __cabi_libpthread.x = (typeof(__cabi_libpthread.x))interpose_symbol( do_interpose_symbol, #x, ver )
57
58//=============================================================================================
59// Interposition Startup logic
60//=============================================================================================
61
62static struct {
63 int (*pthread_create)(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
64 int (*pthread_join)(pthread_t _thread, void **retval);
65 pthread_t (*pthread_self)(void);
66 int (*pthread_attr_init)(pthread_attr_t *attr);
67 int (*pthread_attr_destroy)(pthread_attr_t *attr);
68 int (*pthread_attr_setstack)( pthread_attr_t *attr, void *stackaddr, size_t stacksize );
69 int (*pthread_attr_getstacksize)( const pthread_attr_t *attr, size_t *stacksize );
70 int (*pthread_sigmask)(int how, const sigset_t *set, sigset_t *oldset);
71 int (*pthread_sigqueue)(pthread_t _thread, int sig, const union sigval value);
[7f81ef4]72 int (*pthread_once)(pthread_once_t *once_control, void (*init_routine)(void));
[95dab9e]73} __cabi_libpthread;
74
75extern "C" {
76 void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) libcfa_public {
77 const char *version = 0p;
78
79#pragma GCC diagnostic push
80#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
[089a0d7]81 INTERPOSE( pthread_create, version );
82 INTERPOSE( pthread_join, version );
83 INTERPOSE( pthread_self, version );
84 INTERPOSE( pthread_attr_init, version );
85 INTERPOSE( pthread_attr_destroy, version );
86 INTERPOSE( pthread_attr_setstack, version );
87 INTERPOSE( pthread_attr_getstacksize, version );
88 INTERPOSE( pthread_sigmask, version );
89 INTERPOSE( pthread_sigqueue, version );
90 INTERPOSE( pthread_once, version );
[95dab9e]91#pragma GCC diagnostic pop
92 }
93
94 int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){
95 return __cabi_libpthread.pthread_create(_thread, attr, start_routine, arg);
96 }
97
98 int __cfaabi_pthread_join(pthread_t _thread, void **retval){
99 return __cabi_libpthread.pthread_join(_thread, retval);
100 }
101
102 pthread_t __cfaabi_pthread_self(void){
103 return __cabi_libpthread.pthread_self();
104 }
[7f81ef4]105
[95dab9e]106 int __cfaabi_pthread_attr_init(pthread_attr_t *attr){
107 return __cabi_libpthread.pthread_attr_init(attr);
108 }
[7f81ef4]109
[95dab9e]110 int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr){
111 return __cabi_libpthread.pthread_attr_destroy(attr);
112 }
[7f81ef4]113
[95dab9e]114 int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){
115 return __cabi_libpthread.pthread_attr_setstack(attr, stackaddr, stacksize);
116 }
[7f81ef4]117
[95dab9e]118 int read_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){
119 return __cabi_libpthread.pthread_attr_getstacksize(attr, stacksize);
120 }
[7f81ef4]121
[95dab9e]122 int __cfaabi_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset){
123 return __cabi_libpthread.pthread_sigmask(how, set, oldset);
124 }
[7f81ef4]125
126 int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value) {
[95dab9e]127 return __cabi_libpthread.pthread_sigqueue(_thread, sig, value);
128 }
[7f81ef4]129
130 int __cfaabi_pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) {
131 return __cabi_libpthread.pthread_once(once_control, init_routine);
132 }
[fbdfcd8]133}
Note: See TracBrowser for help on using the repository browser.