source: libcfa/src/interpose_thread.cfa @ 95dab9e

ADTast-experimentalpthread-emulation
Last change on this file since 95dab9e was 95dab9e, checked in by Thierry Delisle <tdelisle@…>, 23 months ago

Changed real_pthread symbols (now cfaabi_pthread) to be protected in libcfathread

  • Property mode set to 100644
File size: 4.5 KB
Line 
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#include <stdarg.h>                                                                             // va_start, va_end
17#include <stdio.h>
18#include <string.h>                                                                             // strlen
19extern "C" {
20#include <dlfcn.h>                                                                              // dlopen, dlsym
21#include <execinfo.h>                                                                   // backtrace, messages
22}
23
24#include "bits/debug.hfa"
25#include "bits/defs.hfa"
26#include <assert.h>
27
28//=============================================================================================
29// Interposing helpers
30//=============================================================================================
31
32typedef void (* generic_fptr_t)(void);
33
34generic_fptr_t interpose_symbol(
35        generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ),
36        const char symbol[],
37        const char version[]
38) libcfa_public {
39        const char * error;
40
41        static void * library;
42        if ( ! library ) {
43                #if defined( RTLD_NEXT )
44                        library = RTLD_NEXT;
45                #else
46                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
47                        library = dlopen( "libpthread.so", RTLD_LAZY );
48                        error = dlerror();
49                        if ( error ) {
50                                abort( "interpose_symbol : failed to open libpthread, %s\n", error );
51                        }
52                #endif
53        } // if
54
55        return do_interpose_symbol(library, symbol, version);
56}
57
58#define INTERPOSE( x, ver ) __cabi_libpthread.x = (typeof(__cabi_libpthread.x))interpose_symbol( do_interpose_symbol, #x, ver )
59
60//=============================================================================================
61// Interposition Startup logic
62//=============================================================================================
63
64static struct {
65        int (*pthread_create)(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
66        int (*pthread_join)(pthread_t _thread, void **retval);
67        pthread_t (*pthread_self)(void);
68        int (*pthread_attr_init)(pthread_attr_t *attr);
69        int (*pthread_attr_destroy)(pthread_attr_t *attr);
70        int (*pthread_attr_setstack)( pthread_attr_t *attr, void *stackaddr, size_t stacksize );
71        int (*pthread_attr_getstacksize)( const pthread_attr_t *attr, size_t *stacksize );
72        int (*pthread_sigmask)(int how, const sigset_t *set, sigset_t *oldset);
73        int (*pthread_sigqueue)(pthread_t _thread, int sig, const union sigval value);
74} __cabi_libpthread;
75
76extern "C" {
77        void __cfathreadabi_interpose_startup( generic_fptr_t (*do_interpose_symbol)( void * library, const char symbol[], const char version[] ) ) libcfa_public {
78                const char *version = 0p;
79
80#pragma GCC diagnostic push
81#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
82                INTERPOSE( pthread_create , version );
83                INTERPOSE( pthread_join , version );
84                INTERPOSE( pthread_self , version );
85                INTERPOSE( pthread_attr_init , version );
86                INTERPOSE( pthread_attr_destroy , version );
87                INTERPOSE( pthread_attr_setstack , version );
88                INTERPOSE( pthread_attr_getstacksize , version );
89                INTERPOSE( pthread_sigmask , version );
90                INTERPOSE( pthread_sigqueue , version );
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        }
105        int __cfaabi_pthread_attr_init(pthread_attr_t *attr){
106                return __cabi_libpthread.pthread_attr_init(attr);
107        }
108        int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr){
109                return __cabi_libpthread.pthread_attr_destroy(attr);
110        }
111        int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){
112                return __cabi_libpthread.pthread_attr_setstack(attr, stackaddr, stacksize);
113        }
114        int read_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){
115                return __cabi_libpthread.pthread_attr_getstacksize(attr, stacksize);
116        }
117        int __cfaabi_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset){
118                return __cabi_libpthread.pthread_sigmask(how, set, oldset);
119        }
120        int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value){
121                return __cabi_libpthread.pthread_sigqueue(_thread, sig, value);
122        }
123}
Note: See TracBrowser for help on using the repository browser.