Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*);
26 static int (*raw_mutex_lock)(pthread_mutex_t*);
27 static int (*raw_mutex_trylock)(pthread_mutex_t*);
28 static int (*raw_mutex_unlock)(pthread_mutex_t*);
29 static int (*raw_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 unsigned int (*raw_sleep)(unsigned int);
38 static int (*raw_usleep)(useconds_t);
39 static int (*raw_gettimeofday)(struct timeval*, void*);
40
41 static sem_t* (*raw_sem_open)(const char*, int);
42 static int (*raw_sem_init)(sem_t*, int, unsigned int);
43 static int (*raw_sem_wait)(sem_t*);
44 static int (*raw_sem_post)(sem_t*);
45 static int (*raw_sem_destroy)(sem_t*);
46 static int (*raw_sem_trywait)(sem_t*);
47 static int (*raw_sem_timedwait)(sem_t*, const struct timespec*);
48
49 static void intercepter_init()
50 {
51   raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
52   raw_pthread_join   = dlsym(RTLD_NEXT, "pthread_join");
53   raw_mutex_init     = dlsym(RTLD_NEXT, "pthread_mutex_init");
54   raw_mutex_lock     = dlsym(RTLD_NEXT, "pthread_mutex_lock");
55   raw_mutex_trylock  = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
56   raw_mutex_unlock   = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
57   raw_mutex_destroy  = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
58
59   raw_pthread_mutexattr_init      = dlsym(RTLD_NEXT, "pthread_mutexattr_init");
60   raw_pthread_mutexattr_settype   = dlsym(RTLD_NEXT, "pthread_mutexattr_settype");
61   raw_pthread_mutexattr_gettype   = dlsym(RTLD_NEXT, "pthread_mutexattr_gettype");
62   raw_pthread_mutexattr_getrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_getrobust");
63   raw_pthread_mutexattr_setrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_setrobust");
64
65   raw_sleep        = dlsym(RTLD_NEXT, "sleep");
66   raw_usleep       = dlsym(RTLD_NEXT, "usleep");
67   raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
68
69   raw_sem_open = dlsym(RTLD_NEXT, "sem_open");
70   raw_sem_init = dlsym(RTLD_NEXT, "sem_init");
71   raw_sem_wait = dlsym(RTLD_NEXT, "sem_wait");
72   raw_sem_post = dlsym(RTLD_NEXT, "sem_post");
73   raw_sem_destroy   = dlsym(RTLD_NEXT, "sem_destroy");
74   raw_sem_trywait   = dlsym(RTLD_NEXT, "sem_trywait");
75   raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
76 }
77
78 static int sthread_inside_simgrid = 1;
79 void sthread_enable(void)
80 { // Start intercepting all pthread calls
81   sthread_inside_simgrid = 0;
82 }
83 void sthread_disable(void)
84 { // Stop intercepting all pthread calls
85   sthread_inside_simgrid = 1;
86 }
87 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
88 {
89   if (raw_pthread_create == NULL)
90     intercepter_init();
91
92   if (sthread_inside_simgrid)
93     return raw_pthread_create(thread, attr, start_routine, arg);
94
95   sthread_disable();
96   int res = sthread_create((sthread_t*)thread, attr, start_routine, arg);
97   sthread_enable();
98   return res;
99 }
100
101 #define _STHREAD_CONCAT(a, b) a##b
102 #define intercepted_call(name, raw_params, call_params, sim_params)                                                    \
103   int _STHREAD_CONCAT(pthread_, name) raw_params                                                                       \
104   {                                                                                                                    \
105     if (_STHREAD_CONCAT(raw_pthread_, name) == NULL)                                                                   \
106       intercepter_init();                                                                                              \
107     if (sthread_inside_simgrid)                                                                                        \
108       return _STHREAD_CONCAT(raw_pthread_, name) call_params;                                                          \
109                                                                                                                        \
110     sthread_disable();                                                                                                 \
111     int res = _STHREAD_CONCAT(sthread_, name) sim_params;                                                              \
112     sthread_enable();                                                                                                  \
113     return res;                                                                                                        \
114   }
115
116 intercepted_call(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr));
117 intercepted_call(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type),
118                  ((sthread_mutexattr_t*)attr, type));
119 intercepted_call(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type),
120                  ((sthread_mutexattr_t*)attr, type));
121 intercepted_call(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness),
122                  ((sthread_mutexattr_t*)attr, robustness));
123 intercepted_call(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness),
124                  (attr, robustness), ((sthread_mutexattr_t*)attr, robustness));
125
126 int pthread_join(pthread_t thread, void** retval)
127 {
128   if (raw_pthread_join == NULL)
129     intercepter_init();
130   if (sthread_inside_simgrid)
131     return raw_pthread_join(thread, retval);
132
133   sthread_disable();
134   int res = sthread_join((sthread_t)thread, retval);
135   sthread_enable();
136   return res;
137 }
138
139 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
140 {
141   if (raw_mutex_init == NULL)
142     intercepter_init();
143
144   if (sthread_inside_simgrid)
145     return raw_mutex_init(mutex, attr);
146
147   sthread_disable();
148   int res = sthread_mutex_init((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr);
149   sthread_enable();
150   return res;
151 }
152
153 int pthread_mutex_lock(pthread_mutex_t* mutex)
154 {
155   if (raw_mutex_lock == NULL)
156     intercepter_init();
157
158   if (sthread_inside_simgrid)
159     return raw_mutex_lock(mutex);
160
161   sthread_disable();
162   int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
163   sthread_enable();
164   return res;
165 }
166
167 int pthread_mutex_trylock(pthread_mutex_t* mutex)
168 {
169   if (raw_mutex_trylock == NULL)
170     intercepter_init();
171
172   if (sthread_inside_simgrid)
173     return raw_mutex_trylock(mutex);
174
175   sthread_disable();
176   int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
177   sthread_enable();
178   return res;
179 }
180
181 int pthread_mutex_unlock(pthread_mutex_t* mutex)
182 {
183   if (raw_mutex_unlock == NULL)
184     intercepter_init();
185
186   if (sthread_inside_simgrid)
187     return raw_mutex_unlock(mutex);
188
189   sthread_disable();
190   int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
191   sthread_enable();
192   return res;
193 }
194 int pthread_mutex_destroy(pthread_mutex_t* mutex)
195 {
196   if (raw_mutex_destroy == NULL)
197     intercepter_init();
198
199   if (sthread_inside_simgrid)
200     return raw_mutex_destroy(mutex);
201
202   sthread_disable();
203   int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
204   sthread_enable();
205   return res;
206 }
207 int sem_init(sem_t* sem, int pshared, unsigned int value)
208 {
209   if (raw_sem_init == NULL)
210     intercepter_init();
211
212   if (sthread_inside_simgrid)
213     return raw_sem_init(sem, pshared, value);
214
215   sthread_disable();
216   int res = sthread_sem_init((sthread_sem_t*)sem, pshared, value);
217   sthread_enable();
218   return res;
219 }
220 int sem_destroy(sem_t* sem)
221 {
222   if (raw_sem_destroy == NULL)
223     intercepter_init();
224
225   if (sthread_inside_simgrid)
226     return raw_sem_destroy(sem);
227
228   sthread_disable();
229   int res = sthread_sem_destroy((sthread_sem_t*)sem);
230   sthread_enable();
231   return res;
232 }
233 int sem_post(sem_t* sem)
234 {
235   if (raw_sem_post == NULL)
236     intercepter_init();
237
238   if (sthread_inside_simgrid)
239     return raw_sem_post(sem);
240
241   sthread_disable();
242   int res = sthread_sem_post((sthread_sem_t*)sem);
243   sthread_enable();
244   return res;
245 }
246 int sem_wait(sem_t* sem)
247 {
248   if (raw_sem_wait == NULL)
249     intercepter_init();
250
251   if (sthread_inside_simgrid)
252     return raw_sem_wait(sem);
253
254   sthread_disable();
255   int res = sthread_sem_wait((sthread_sem_t*)sem);
256   sthread_enable();
257   return res;
258 }
259 int sem_trywait(sem_t* sem)
260 {
261   if (raw_sem_trywait == NULL)
262     intercepter_init();
263
264   if (sthread_inside_simgrid)
265     return raw_sem_trywait(sem);
266
267   sthread_disable();
268   int res = sthread_sem_trywait((sthread_sem_t*)sem);
269   sthread_enable();
270   return res;
271 }
272 int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout)
273 {
274   if (raw_sem_timedwait == NULL)
275     intercepter_init();
276
277   if (sthread_inside_simgrid)
278     return raw_sem_timedwait(sem, abs_timeout);
279
280   sthread_disable();
281   int res = sthread_sem_timedwait((sthread_sem_t*)sem, abs_timeout);
282   sthread_enable();
283   return res;
284 }
285
286 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
287    Other implementations use "void *" instead. */
288 #ifdef __GLIBC__
289 #if !__GLIBC_PREREQ(2, 31)
290 #define TIMEZONE_TYPE struct timezone
291 #endif
292 #endif
293 #ifndef TIMEZONE_TYPE
294 #define TIMEZONE_TYPE void
295 #endif
296
297 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
298 {
299   if (raw_gettimeofday == NULL)
300     intercepter_init();
301
302   if (sthread_inside_simgrid)
303     return raw_gettimeofday(tv, tz);
304
305   sthread_disable();
306   int res = sthread_gettimeofday(tv);
307   sthread_enable();
308   return res;
309 }
310
311 unsigned int sleep(unsigned int seconds)
312 {
313   if (raw_sleep == NULL)
314     intercepter_init();
315
316   if (sthread_inside_simgrid)
317     return raw_sleep(seconds);
318
319   sthread_disable();
320   sthread_sleep(seconds);
321   sthread_enable();
322   return 0;
323 }
324
325 int usleep(useconds_t usec)
326 {
327   if (raw_usleep == NULL)
328     intercepter_init();
329
330   if (sthread_inside_simgrid)
331     return raw_usleep(usec);
332
333   sthread_disable();
334   sthread_sleep(((double)usec) / 1000000.);
335   sthread_enable();
336   return 0;
337 }
338
339 #if 0
340 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
341     *cond = sg_cond_init();
342     return 0;
343 }
344
345 int pthread_cond_signal(pthread_cond_t *cond) {
346         sg_cond_notify_one(*cond);
347     return 0;
348 }
349
350 int pthread_cond_broadcast(pthread_cond_t *cond) {
351         sg_cond_notify_all(*cond);
352     return 0;
353 }
354
355 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
356         sg_cond_wait(*cond, *mutex);
357     return 0;
358 }
359
360 int pthread_cond_destroy(pthread_cond_t *cond) {
361         sg_cond_destroy(*cond);
362     return 0;
363 }
364 #endif
365
366 /* Trampoline for the real main() */
367 static int (*raw_main)(int, char**, char**);
368
369 /* Our fake main() that gets called by __libc_start_main() */
370 static int main_hook(int argc, char** argv, char** envp)
371 {
372   return sthread_main(argc, argv, envp, raw_main);
373 }
374
375 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
376 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
377                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
378
379 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
380                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
381 {
382   /* Save the real main function address */
383   raw_main = main;
384
385   /* Find the real __libc_start_main()... */
386   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
387   /* ... and call it with our custom main function */
388 #if HAVE_VALGRIND_H
389   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
390   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
391     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
392 #endif
393   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
394 }