1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved. */
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. */
6 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
9 #include "src/sthread/sthread.h"
10 #include "src/internal_config.h"
13 #include <semaphore.h>
19 #include <valgrind/valgrind.h>
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*);
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);
37 static unsigned int (*raw_sleep)(unsigned int);
38 static int (*raw_usleep)(useconds_t);
39 static int (*raw_gettimeofday)(struct timeval*, void*);
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*);
49 static void intercepter_init()
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");
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");
65 raw_sleep = dlsym(RTLD_NEXT, "sleep");
66 raw_usleep = dlsym(RTLD_NEXT, "usleep");
67 raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
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");
78 static int sthread_inside_simgrid = 1;
79 void sthread_enable(void)
80 { // Start intercepting all pthread calls
81 sthread_inside_simgrid = 0;
83 void sthread_disable(void)
84 { // Stop intercepting all pthread calls
85 sthread_inside_simgrid = 1;
87 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
89 if (raw_pthread_create == NULL)
92 if (sthread_inside_simgrid)
93 return raw_pthread_create(thread, attr, start_routine, arg);
96 int res = sthread_create((sthread_t*)thread, attr, start_routine, arg);
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 \
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; \
111 int res = _STHREAD_CONCAT(sthread_, name) sim_params; \
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));
126 int pthread_join(pthread_t thread, void** retval)
128 if (raw_pthread_join == NULL)
130 if (sthread_inside_simgrid)
131 return raw_pthread_join(thread, retval);
134 int res = sthread_join((sthread_t)thread, retval);
139 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
141 if (raw_mutex_init == NULL)
144 if (sthread_inside_simgrid)
145 return raw_mutex_init(mutex, attr);
148 int res = sthread_mutex_init((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr);
153 int pthread_mutex_lock(pthread_mutex_t* mutex)
155 if (raw_mutex_lock == NULL)
158 if (sthread_inside_simgrid)
159 return raw_mutex_lock(mutex);
162 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
167 int pthread_mutex_trylock(pthread_mutex_t* mutex)
169 if (raw_mutex_trylock == NULL)
172 if (sthread_inside_simgrid)
173 return raw_mutex_trylock(mutex);
176 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
181 int pthread_mutex_unlock(pthread_mutex_t* mutex)
183 if (raw_mutex_unlock == NULL)
186 if (sthread_inside_simgrid)
187 return raw_mutex_unlock(mutex);
190 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
194 int pthread_mutex_destroy(pthread_mutex_t* mutex)
196 if (raw_mutex_destroy == NULL)
199 if (sthread_inside_simgrid)
200 return raw_mutex_destroy(mutex);
203 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
207 int sem_init(sem_t* sem, int pshared, unsigned int value)
209 if (raw_sem_init == NULL)
212 if (sthread_inside_simgrid)
213 return raw_sem_init(sem, pshared, value);
216 int res = sthread_sem_init((sthread_sem_t*)sem, pshared, value);
220 int sem_destroy(sem_t* sem)
222 if (raw_sem_destroy == NULL)
225 if (sthread_inside_simgrid)
226 return raw_sem_destroy(sem);
229 int res = sthread_sem_destroy((sthread_sem_t*)sem);
233 int sem_post(sem_t* sem)
235 if (raw_sem_post == NULL)
238 if (sthread_inside_simgrid)
239 return raw_sem_post(sem);
242 int res = sthread_sem_post((sthread_sem_t*)sem);
246 int sem_wait(sem_t* sem)
248 if (raw_sem_wait == NULL)
251 if (sthread_inside_simgrid)
252 return raw_sem_wait(sem);
255 int res = sthread_sem_wait((sthread_sem_t*)sem);
259 int sem_trywait(sem_t* sem)
261 if (raw_sem_trywait == NULL)
264 if (sthread_inside_simgrid)
265 return raw_sem_trywait(sem);
268 int res = sthread_sem_trywait((sthread_sem_t*)sem);
272 int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout)
274 if (raw_sem_timedwait == NULL)
277 if (sthread_inside_simgrid)
278 return raw_sem_timedwait(sem, abs_timeout);
281 int res = sthread_sem_timedwait((sthread_sem_t*)sem, abs_timeout);
286 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
287 Other implementations use "void *" instead. */
289 #if !__GLIBC_PREREQ(2, 31)
290 #define TIMEZONE_TYPE struct timezone
293 #ifndef TIMEZONE_TYPE
294 #define TIMEZONE_TYPE void
297 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
299 if (raw_gettimeofday == NULL)
302 if (sthread_inside_simgrid)
303 return raw_gettimeofday(tv, tz);
306 int res = sthread_gettimeofday(tv);
311 unsigned int sleep(unsigned int seconds)
313 if (raw_sleep == NULL)
316 if (sthread_inside_simgrid)
317 return raw_sleep(seconds);
320 sthread_sleep(seconds);
325 int usleep(useconds_t usec)
327 if (raw_usleep == NULL)
330 if (sthread_inside_simgrid)
331 return raw_usleep(usec);
334 sthread_sleep(((double)usec) / 1000000.);
340 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
341 *cond = sg_cond_init();
345 int pthread_cond_signal(pthread_cond_t *cond) {
346 sg_cond_notify_one(*cond);
350 int pthread_cond_broadcast(pthread_cond_t *cond) {
351 sg_cond_notify_all(*cond);
355 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
356 sg_cond_wait(*cond, *mutex);
360 int pthread_cond_destroy(pthread_cond_t *cond) {
361 sg_cond_destroy(*cond);
366 /* Trampoline for the real main() */
367 static int (*raw_main)(int, char**, char**);
369 /* Our fake main() that gets called by __libc_start_main() */
370 static int main_hook(int argc, char** argv, char** envp)
372 return sthread_main(argc, argv, envp, raw_main);
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);
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)
382 /* Save the real main function address */
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 */
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);
393 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);