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_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*);
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_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
54 raw_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
55 raw_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
56 raw_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
57 raw_pthread_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;
88 #define _STHREAD_CONCAT(a, b) a##b
89 #define intercepted_pthcall(name, raw_params, call_params, sim_params) \
90 int _STHREAD_CONCAT(pthread_, name) raw_params \
92 if (_STHREAD_CONCAT(raw_pthread_, name) == NULL) \
94 if (sthread_inside_simgrid) \
95 return _STHREAD_CONCAT(raw_pthread_, name) call_params; \
98 int res = _STHREAD_CONCAT(sthread_, name) sim_params; \
103 intercepted_pthcall(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr));
104 intercepted_pthcall(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type),
105 ((sthread_mutexattr_t*)attr, type));
106 intercepted_pthcall(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type),
107 ((sthread_mutexattr_t*)attr, type));
108 intercepted_pthcall(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness),
109 ((sthread_mutexattr_t*)attr, robustness));
110 intercepted_pthcall(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness),
111 (attr, robustness), ((sthread_mutexattr_t*)attr, robustness));
113 intercepted_pthcall(create, (pthread_t * thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg),
114 (thread, attr, start_routine, arg), ((sthread_t*)thread, attr, start_routine, arg));
115 intercepted_pthcall(join, (pthread_t thread, void** retval), (thread, retval), ((sthread_t)thread, retval));
117 intercepted_pthcall(mutex_init, (pthread_mutex_t * mutex, const pthread_mutexattr_t* attr), (mutex, attr),
118 ((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr));
119 intercepted_pthcall(mutex_lock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
120 intercepted_pthcall(mutex_trylock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
121 intercepted_pthcall(mutex_unlock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
122 intercepted_pthcall(mutex_destroy, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
124 #define intercepted_call(rettype, name, raw_params, call_params, sim_params) \
125 rettype name raw_params \
127 if (_STHREAD_CONCAT(raw_, name) == NULL) \
128 intercepter_init(); \
129 if (sthread_inside_simgrid) \
130 return _STHREAD_CONCAT(raw_, name) call_params; \
133 rettype res = _STHREAD_CONCAT(sthread_, name) sim_params; \
138 intercepted_call(int, sem_init, (sem_t * sem, int pshared, unsigned int value), (sem, pshared, value),
139 ((sthread_sem_t*)sem, pshared, value));
140 intercepted_call(int, sem_destroy, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
141 intercepted_call(int, sem_post, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
142 intercepted_call(int, sem_wait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
143 intercepted_call(int, sem_trywait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
144 intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_timeout), (sem, abs_timeout),
145 ((sthread_sem_t*)sem, abs_timeout));
147 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
148 Other implementations use "void *" instead. */
150 #if !__GLIBC_PREREQ(2, 31)
151 #define TIMEZONE_TYPE struct timezone
154 #ifndef TIMEZONE_TYPE
155 #define TIMEZONE_TYPE void
157 intercepted_call(int, gettimeofday, (struct timeval * tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz), (tv, tz), (tv));
158 intercepted_call(unsigned int, sleep, (unsigned int seconds), (seconds), (seconds));
159 intercepted_call(int, usleep, (useconds_t usec), (usec), (((double)usec) / 1000000.));
162 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
163 *cond = sg_cond_init();
167 int pthread_cond_signal(pthread_cond_t *cond) {
168 sg_cond_notify_one(*cond);
172 int pthread_cond_broadcast(pthread_cond_t *cond) {
173 sg_cond_notify_all(*cond);
177 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
178 sg_cond_wait(*cond, *mutex);
182 int pthread_cond_destroy(pthread_cond_t *cond) {
183 sg_cond_destroy(*cond);
188 /* Trampoline for the real main() */
189 static int (*raw_main)(int, char**, char**);
191 /* Our fake main() that gets called by __libc_start_main() */
192 static int main_hook(int argc, char** argv, char** envp)
194 return sthread_main(argc, argv, envp, raw_main);
197 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
198 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
199 void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
201 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
202 void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
204 /* Save the real main function address */
207 /* Find the real __libc_start_main()... */
208 typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
209 /* ... and call it with our custom main function */
211 /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
212 if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
213 return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
215 return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);