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 unsigned int (*raw_sleep)(unsigned int);
32 static int (*raw_usleep)(useconds_t);
33 static int (*raw_gettimeofday)(struct timeval*, void*);
35 static sem_t* (*raw_sem_open)(const char*, int);
36 static int (*raw_sem_init)(sem_t*, int, unsigned int);
37 static int (*raw_sem_wait)(sem_t*);
38 static int (*raw_sem_post)(sem_t*);
39 static int (*raw_sem_destroy)(sem_t*);
40 static int (*raw_sem_trywait)(sem_t*);
41 static int (*raw_sem_timedwait)(sem_t*, const struct timespec*);
43 static void intercepter_init()
45 raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
46 raw_pthread_join = dlsym(RTLD_NEXT, "pthread_join");
47 raw_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
48 raw_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
49 raw_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
50 raw_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
51 raw_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
53 raw_sleep = dlsym(RTLD_NEXT, "sleep");
54 raw_usleep = dlsym(RTLD_NEXT, "usleep");
55 raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
57 raw_sem_open = dlsym(RTLD_NEXT, "sem_open");
58 raw_sem_init = dlsym(RTLD_NEXT, "sem_init");
59 raw_sem_wait = dlsym(RTLD_NEXT, "sem_wait");
60 raw_sem_post = dlsym(RTLD_NEXT, "sem_post");
61 raw_sem_destroy = dlsym(RTLD_NEXT, "sem_destroy");
62 raw_sem_trywait = dlsym(RTLD_NEXT, "sem_trywait");
63 raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
66 static int sthread_inside_simgrid = 1;
67 void sthread_enable(void)
68 { // Start intercepting all pthread calls
69 sthread_inside_simgrid = 0;
71 void sthread_disable(void)
72 { // Stop intercepting all pthread calls
73 sthread_inside_simgrid = 1;
75 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
77 if (raw_pthread_create == NULL)
80 if (sthread_inside_simgrid)
81 return raw_pthread_create(thread, attr, start_routine, arg);
84 int res = sthread_create((sthread_t*)thread, attr, start_routine, arg);
88 int pthread_join(pthread_t thread, void** retval)
90 if (raw_pthread_join == NULL)
92 if (sthread_inside_simgrid)
93 return raw_pthread_join(thread, retval);
96 int res = sthread_join((sthread_t)thread, retval);
101 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
103 if (raw_mutex_init == NULL)
106 if (sthread_inside_simgrid)
107 return raw_mutex_init(mutex, attr);
110 int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
115 int pthread_mutex_lock(pthread_mutex_t* mutex)
117 if (raw_mutex_lock == NULL)
120 if (sthread_inside_simgrid)
121 return raw_mutex_lock(mutex);
124 int res = sthread_mutex_lock((sthread_mutex_t*)mutex);
129 int pthread_mutex_trylock(pthread_mutex_t* mutex)
131 if (raw_mutex_trylock == NULL)
134 if (sthread_inside_simgrid)
135 return raw_mutex_trylock(mutex);
138 int res = sthread_mutex_trylock((sthread_mutex_t*)mutex);
143 int pthread_mutex_unlock(pthread_mutex_t* mutex)
145 if (raw_mutex_unlock == NULL)
148 if (sthread_inside_simgrid)
149 return raw_mutex_unlock(mutex);
152 int res = sthread_mutex_unlock((sthread_mutex_t*)mutex);
156 int pthread_mutex_destroy(pthread_mutex_t* mutex)
158 if (raw_mutex_destroy == NULL)
161 if (sthread_inside_simgrid)
162 return raw_mutex_destroy(mutex);
165 int res = sthread_mutex_destroy((sthread_mutex_t*)mutex);
169 int sem_init(sem_t* sem, int pshared, unsigned int value)
171 if (raw_sem_init == NULL)
174 if (sthread_inside_simgrid)
175 return raw_sem_init(sem, pshared, value);
178 int res = sthread_sem_init((sthread_sem_t*)sem, pshared, value);
182 int sem_destroy(sem_t* sem)
184 if (raw_sem_destroy == NULL)
187 if (sthread_inside_simgrid)
188 return raw_sem_destroy(sem);
191 int res = sthread_sem_destroy((sthread_sem_t*)sem);
195 int sem_post(sem_t* sem)
197 if (raw_sem_post == NULL)
200 if (sthread_inside_simgrid)
201 return raw_sem_post(sem);
204 int res = sthread_sem_post((sthread_sem_t*)sem);
208 int sem_wait(sem_t* sem)
210 if (raw_sem_wait == NULL)
213 if (sthread_inside_simgrid)
214 return raw_sem_wait(sem);
217 int res = sthread_sem_wait((sthread_sem_t*)sem);
221 int sem_trywait(sem_t* sem)
223 if (raw_sem_trywait == NULL)
226 if (sthread_inside_simgrid)
227 return raw_sem_trywait(sem);
230 int res = sthread_sem_trywait((sthread_sem_t*)sem);
234 int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout)
236 if (raw_sem_timedwait == NULL)
239 if (sthread_inside_simgrid)
240 return raw_sem_timedwait(sem, abs_timeout);
243 int res = sthread_sem_timedwait((sthread_sem_t*)sem, abs_timeout);
248 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
249 Other implementations use "void *" instead. */
251 #if !__GLIBC_PREREQ(2, 31)
252 #define TIMEZONE_TYPE struct timezone
255 #ifndef TIMEZONE_TYPE
256 #define TIMEZONE_TYPE void
259 int gettimeofday(struct timeval* tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz)
261 if (raw_gettimeofday == NULL)
264 if (sthread_inside_simgrid)
265 return raw_gettimeofday(tv, tz);
268 int res = sthread_gettimeofday(tv);
273 unsigned int sleep(unsigned int seconds)
275 if (raw_sleep == NULL)
278 if (sthread_inside_simgrid)
279 return raw_sleep(seconds);
282 sthread_sleep(seconds);
287 int usleep(useconds_t usec)
289 if (raw_usleep == NULL)
292 if (sthread_inside_simgrid)
293 return raw_usleep(usec);
296 sthread_sleep(((double)usec) / 1000000.);
302 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
303 *cond = sg_cond_init();
307 int pthread_cond_signal(pthread_cond_t *cond) {
308 sg_cond_notify_one(*cond);
312 int pthread_cond_broadcast(pthread_cond_t *cond) {
313 sg_cond_notify_all(*cond);
317 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
318 sg_cond_wait(*cond, *mutex);
322 int pthread_cond_destroy(pthread_cond_t *cond) {
323 sg_cond_destroy(*cond);
328 /* Trampoline for the real main() */
329 static int (*raw_main)(int, char**, char**);
331 /* Our fake main() that gets called by __libc_start_main() */
332 static int main_hook(int argc, char** argv, char** envp)
334 return sthread_main(argc, argv, envp, raw_main);
337 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
338 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
339 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
341 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
342 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
344 /* Save the real main function address */
347 /* Find the real __libc_start_main()... */
348 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
349 /* ... and call it with our custom main function */
351 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
352 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
353 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
355 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);