Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'battery-get-name' into 'master'
[simgrid.git] / src / sthread / sthread.c
1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
7
8 #define _GNU_SOURCE
9 #include "src/sthread/sthread.h"
10 #include "src/internal_config.h"
11 #include <dlfcn.h>
12 #include <pthread.h>
13 #include <semaphore.h>
14 #include <stdio.h>
15 #include <unistd.h>
16
17 #if HAVE_VALGRIND_H
18 #include <stdlib.h>
19 #include <valgrind/valgrind.h>
20 #endif
21
22 /* We don't want to intercept pthread within SimGrid. Instead we should provide the real implem to SimGrid */
23 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
24 static int (*raw_pthread_join)(pthread_t, void**);
25 static int (*raw_pthread_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*);
26 static int (*raw_pthread_mutex_lock)(pthread_mutex_t*);
27 static int (*raw_pthread_mutex_trylock)(pthread_mutex_t*);
28 static int (*raw_pthread_mutex_unlock)(pthread_mutex_t*);
29 static int (*raw_pthread_mutex_destroy)(pthread_mutex_t*);
30
31 static int (*raw_pthread_mutexattr_init)(pthread_mutexattr_t*);
32 static int (*raw_pthread_mutexattr_settype)(pthread_mutexattr_t*, int);
33 static int (*raw_pthread_mutexattr_gettype)(const pthread_mutexattr_t* restrict, int* restrict);
34 static int (*raw_pthread_mutexattr_getrobust)(const pthread_mutexattr_t*, int*);
35 static int (*raw_pthread_mutexattr_setrobust)(pthread_mutexattr_t*, int);
36
37 static int (*raw_pthread_barrier_init)(pthread_barrier_t*, const pthread_barrierattr_t*, unsigned int count);
38 static int (*raw_pthread_barrier_wait)(pthread_barrier_t*);
39 static int (*raw_pthread_barrier_destroy)(pthread_barrier_t*);
40
41 static int (*raw_pthread_cond_init)(pthread_cond_t*, const pthread_condattr_t*);
42 static int (*raw_pthread_cond_signal)(pthread_cond_t*);
43 static int (*raw_pthread_cond_broadcast)(pthread_cond_t*);
44 static int (*raw_pthread_cond_wait)(pthread_cond_t*, pthread_mutex_t*);
45 static int (*raw_pthread_cond_timedwait)(pthread_cond_t*, pthread_mutex_t*, const struct timespec* abstime);
46 static int (*raw_pthread_cond_destroy)(pthread_cond_t*);
47
48 static unsigned int (*raw_sleep)(unsigned int);
49 static int (*raw_usleep)(useconds_t);
50 static int (*raw_gettimeofday)(struct timeval*, void*);
51
52 static sem_t* (*raw_sem_open)(const char*, int);
53 static int (*raw_sem_init)(sem_t*, int, unsigned int);
54 static int (*raw_sem_wait)(sem_t*);
55 static int (*raw_sem_post)(sem_t*);
56 static int (*raw_sem_destroy)(sem_t*);
57 static int (*raw_sem_trywait)(sem_t*);
58 static int (*raw_sem_timedwait)(sem_t*, const struct timespec*);
59
60 static void intercepter_init()
61 {
62   raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
63   raw_pthread_join   = dlsym(RTLD_NEXT, "pthread_join");
64   raw_pthread_mutex_init    = dlsym(RTLD_NEXT, "pthread_mutex_init");
65   raw_pthread_mutex_lock    = dlsym(RTLD_NEXT, "pthread_mutex_lock");
66   raw_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
67   raw_pthread_mutex_unlock  = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
68   raw_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
69
70   raw_pthread_mutexattr_init      = dlsym(RTLD_NEXT, "pthread_mutexattr_init");
71   raw_pthread_mutexattr_settype   = dlsym(RTLD_NEXT, "pthread_mutexattr_settype");
72   raw_pthread_mutexattr_gettype   = dlsym(RTLD_NEXT, "pthread_mutexattr_gettype");
73   raw_pthread_mutexattr_getrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_getrobust");
74   raw_pthread_mutexattr_setrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_setrobust");
75
76   raw_pthread_barrier_init = dlsym(RTLD_NEXT, "raw_pthread_barrier_init");
77   raw_pthread_barrier_wait = dlsym(RTLD_NEXT, "raw_pthread_barrier_wait");
78   raw_pthread_barrier_destroy = dlsym(RTLD_NEXT, "raw_pthread_barrier_destroy");
79
80   raw_pthread_cond_init      = dlsym(RTLD_NEXT, "raw_pthread_cond_init");
81   raw_pthread_cond_signal    = dlsym(RTLD_NEXT, "raw_pthread_cond_signal");
82   raw_pthread_cond_broadcast = dlsym(RTLD_NEXT, "raw_pthread_cond_broadcast");
83   raw_pthread_cond_wait      = dlsym(RTLD_NEXT, "raw_pthread_cond_wait");
84   raw_pthread_cond_timedwait = dlsym(RTLD_NEXT, "raw_pthread_cond_timedwait");
85   raw_pthread_cond_destroy   = dlsym(RTLD_NEXT, "raw_pthread_cond_destroy");
86
87   raw_sleep        = dlsym(RTLD_NEXT, "sleep");
88   raw_usleep       = dlsym(RTLD_NEXT, "usleep");
89   raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
90
91   raw_sem_open = dlsym(RTLD_NEXT, "sem_open");
92   raw_sem_init = dlsym(RTLD_NEXT, "sem_init");
93   raw_sem_wait = dlsym(RTLD_NEXT, "sem_wait");
94   raw_sem_post = dlsym(RTLD_NEXT, "sem_post");
95   raw_sem_destroy   = dlsym(RTLD_NEXT, "sem_destroy");
96   raw_sem_trywait   = dlsym(RTLD_NEXT, "sem_trywait");
97   raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
98 }
99
100 static int sthread_inside_simgrid = 1;
101 void sthread_enable(void)
102 { // Start intercepting all pthread calls
103   sthread_inside_simgrid = 0;
104 }
105 void sthread_disable(void)
106 { // Stop intercepting all pthread calls
107   sthread_inside_simgrid = 1;
108 }
109
110 #define _STHREAD_CONCAT(a, b) a##b
111 #define intercepted_pthcall(name, raw_params, call_params, sim_params)                                                 \
112   int _STHREAD_CONCAT(pthread_, name) raw_params                                                                       \
113   {                                                                                                                    \
114     if (_STHREAD_CONCAT(raw_pthread_, name) == NULL)                                                                   \
115       intercepter_init();                                                                                              \
116     if (sthread_inside_simgrid)                                                                                        \
117       return _STHREAD_CONCAT(raw_pthread_, name) call_params;                                                          \
118                                                                                                                        \
119     sthread_disable();                                                                                                 \
120     int res = _STHREAD_CONCAT(sthread_, name) sim_params;                                                              \
121     sthread_enable();                                                                                                  \
122     return res;                                                                                                        \
123   }
124
125 intercepted_pthcall(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr));
126 intercepted_pthcall(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type),
127                     ((sthread_mutexattr_t*)attr, type));
128 intercepted_pthcall(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type),
129                     ((sthread_mutexattr_t*)attr, type));
130 intercepted_pthcall(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness),
131                     ((sthread_mutexattr_t*)attr, robustness));
132 intercepted_pthcall(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness),
133                     (attr, robustness), ((sthread_mutexattr_t*)attr, robustness));
134
135 intercepted_pthcall(create, (pthread_t * thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg),
136                     (thread, attr, start_routine, arg), ((sthread_t*)thread, attr, start_routine, arg));
137 intercepted_pthcall(join, (pthread_t thread, void** retval), (thread, retval), ((sthread_t)thread, retval));
138
139 intercepted_pthcall(mutex_init, (pthread_mutex_t * mutex, const pthread_mutexattr_t* attr), (mutex, attr),
140                     ((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr));
141 intercepted_pthcall(mutex_lock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
142 intercepted_pthcall(mutex_trylock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
143 intercepted_pthcall(mutex_unlock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
144 intercepted_pthcall(mutex_destroy, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
145
146 intercepted_pthcall(barrier_init, (pthread_barrier_t * barrier, const pthread_barrierattr_t* attr, unsigned count),
147                     (barrier, attr, count), ((sthread_barrier_t*)barrier, (const sthread_barrierattr_t*)attr, count));
148 intercepted_pthcall(barrier_wait, (pthread_barrier_t* barrier),( barrier),((sthread_barrier_t*) barrier));
149 intercepted_pthcall(barrier_destroy, (pthread_barrier_t* barrier),( barrier),((sthread_barrier_t*) barrier));
150
151 intercepted_pthcall(cond_init, (pthread_cond_t * cond, const pthread_condattr_t* attr), (cond, attr),
152                     ((sthread_cond_t*)cond, (sthread_condattr_t*)attr));
153 intercepted_pthcall(cond_signal, (pthread_cond_t * cond), (cond), ((sthread_cond_t*)cond));
154 intercepted_pthcall(cond_broadcast, (pthread_cond_t * cond), (cond), ((sthread_cond_t*)cond));
155 intercepted_pthcall(cond_wait, (pthread_cond_t * cond, pthread_mutex_t* mutex), (cond, mutex),
156                     ((sthread_cond_t*)cond, (sthread_mutex_t*)mutex));
157 intercepted_pthcall(cond_timedwait, (pthread_cond_t * cond, pthread_mutex_t* mutex, const struct timespec* abstime),
158                     (cond, mutex, abstime), ((sthread_cond_t*)cond, (sthread_mutex_t*)mutex, abstime));
159 intercepted_pthcall(cond_destroy, (pthread_cond_t * cond), (cond), ((sthread_cond_t*)cond));
160
161 #define intercepted_call(rettype, name, raw_params, call_params, sim_params)                                           \
162   rettype name raw_params                                                                                              \
163   {                                                                                                                    \
164     if (_STHREAD_CONCAT(raw_, name) == NULL)                                                                           \
165       intercepter_init();                                                                                              \
166     if (sthread_inside_simgrid)                                                                                        \
167       return _STHREAD_CONCAT(raw_, name) call_params;                                                                  \
168                                                                                                                        \
169     sthread_disable();                                                                                                 \
170     rettype res = _STHREAD_CONCAT(sthread_, name) sim_params;                                                          \
171     sthread_enable();                                                                                                  \
172     return res;                                                                                                        \
173   }
174
175 intercepted_call(int, sem_init, (sem_t * sem, int pshared, unsigned int value), (sem, pshared, value),
176                  ((sthread_sem_t*)sem, pshared, value));
177 intercepted_call(int, sem_destroy, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
178 intercepted_call(int, sem_post, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
179 intercepted_call(int, sem_wait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
180 intercepted_call(int, sem_trywait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
181 intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_timeout), (sem, abs_timeout),
182                  ((sthread_sem_t*)sem, abs_timeout));
183
184 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
185    Other implementations use "void *" instead. */
186 #ifdef __GLIBC__
187 #if !__GLIBC_PREREQ(2, 31)
188 #define TIMEZONE_TYPE struct timezone
189 #endif
190 #endif
191 #ifndef TIMEZONE_TYPE
192 #define TIMEZONE_TYPE void
193 #endif
194 intercepted_call(int, gettimeofday, (struct timeval * tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz), (tv, tz), (tv));
195 intercepted_call(unsigned int, sleep, (unsigned int seconds), (seconds), (seconds));
196 intercepted_call(int, usleep, (useconds_t usec), (usec), (((double)usec) / 1000000.));
197
198 /* Trampoline for the real main() */
199 static int (*raw_main)(int, char**, char**);
200
201 /* Our fake main() that gets called by __libc_start_main() */
202 static int main_hook(int argc, char** argv, char** envp)
203 {
204   return sthread_main(argc, argv, envp, raw_main);
205 }
206
207 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
208 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
209                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
210
211 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
212                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
213 {
214   /* Save the real main function address */
215   raw_main = main;
216
217   /* Find the real __libc_start_main()... */
218   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
219   /* ... and call it with our custom main function */
220 #if HAVE_VALGRIND_H
221   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
222   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
223     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
224 #endif
225   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
226 }