Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement pthread_cond in sthread -- too bad it's TODO in MC
[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_destroy)(pthread_cond_t*);
46
47 static unsigned int (*raw_sleep)(unsigned int);
48 static int (*raw_usleep)(useconds_t);
49 static int (*raw_gettimeofday)(struct timeval*, void*);
50
51 static sem_t* (*raw_sem_open)(const char*, int);
52 static int (*raw_sem_init)(sem_t*, int, unsigned int);
53 static int (*raw_sem_wait)(sem_t*);
54 static int (*raw_sem_post)(sem_t*);
55 static int (*raw_sem_destroy)(sem_t*);
56 static int (*raw_sem_trywait)(sem_t*);
57 static int (*raw_sem_timedwait)(sem_t*, const struct timespec*);
58
59 static void intercepter_init()
60 {
61   raw_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
62   raw_pthread_join   = dlsym(RTLD_NEXT, "pthread_join");
63   raw_pthread_mutex_init    = dlsym(RTLD_NEXT, "pthread_mutex_init");
64   raw_pthread_mutex_lock    = dlsym(RTLD_NEXT, "pthread_mutex_lock");
65   raw_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
66   raw_pthread_mutex_unlock  = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
67   raw_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
68
69   raw_pthread_mutexattr_init      = dlsym(RTLD_NEXT, "pthread_mutexattr_init");
70   raw_pthread_mutexattr_settype   = dlsym(RTLD_NEXT, "pthread_mutexattr_settype");
71   raw_pthread_mutexattr_gettype   = dlsym(RTLD_NEXT, "pthread_mutexattr_gettype");
72   raw_pthread_mutexattr_getrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_getrobust");
73   raw_pthread_mutexattr_setrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_setrobust");
74
75   raw_pthread_barrier_init = dlsym(RTLD_NEXT, "raw_pthread_barrier_init");
76   raw_pthread_barrier_wait = dlsym(RTLD_NEXT, "raw_pthread_barrier_wait");
77   raw_pthread_barrier_destroy = dlsym(RTLD_NEXT, "raw_pthread_barrier_destroy");
78
79   raw_pthread_cond_init      = dlsym(RTLD_NEXT, "raw_pthread_cond_init");
80   raw_pthread_cond_signal    = dlsym(RTLD_NEXT, "raw_pthread_cond_signal");
81   raw_pthread_cond_broadcast = dlsym(RTLD_NEXT, "raw_pthread_cond_broadcast");
82   raw_pthread_cond_wait      = dlsym(RTLD_NEXT, "raw_pthread_cond_wait");
83   raw_pthread_cond_destroy   = dlsym(RTLD_NEXT, "raw_pthread_cond_destroy");
84
85   raw_sleep        = dlsym(RTLD_NEXT, "sleep");
86   raw_usleep       = dlsym(RTLD_NEXT, "usleep");
87   raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
88
89   raw_sem_open = dlsym(RTLD_NEXT, "sem_open");
90   raw_sem_init = dlsym(RTLD_NEXT, "sem_init");
91   raw_sem_wait = dlsym(RTLD_NEXT, "sem_wait");
92   raw_sem_post = dlsym(RTLD_NEXT, "sem_post");
93   raw_sem_destroy   = dlsym(RTLD_NEXT, "sem_destroy");
94   raw_sem_trywait   = dlsym(RTLD_NEXT, "sem_trywait");
95   raw_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
96 }
97
98 static int sthread_inside_simgrid = 1;
99 void sthread_enable(void)
100 { // Start intercepting all pthread calls
101   sthread_inside_simgrid = 0;
102 }
103 void sthread_disable(void)
104 { // Stop intercepting all pthread calls
105   sthread_inside_simgrid = 1;
106 }
107
108 #define _STHREAD_CONCAT(a, b) a##b
109 #define intercepted_pthcall(name, raw_params, call_params, sim_params)                                                 \
110   int _STHREAD_CONCAT(pthread_, name) raw_params                                                                       \
111   {                                                                                                                    \
112     if (_STHREAD_CONCAT(raw_pthread_, name) == NULL)                                                                   \
113       intercepter_init();                                                                                              \
114     if (sthread_inside_simgrid)                                                                                        \
115       return _STHREAD_CONCAT(raw_pthread_, name) call_params;                                                          \
116                                                                                                                        \
117     sthread_disable();                                                                                                 \
118     int res = _STHREAD_CONCAT(sthread_, name) sim_params;                                                              \
119     sthread_enable();                                                                                                  \
120     return res;                                                                                                        \
121   }
122
123 intercepted_pthcall(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr));
124 intercepted_pthcall(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type),
125                     ((sthread_mutexattr_t*)attr, type));
126 intercepted_pthcall(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type),
127                     ((sthread_mutexattr_t*)attr, type));
128 intercepted_pthcall(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness),
129                     ((sthread_mutexattr_t*)attr, robustness));
130 intercepted_pthcall(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness),
131                     (attr, robustness), ((sthread_mutexattr_t*)attr, robustness));
132
133 intercepted_pthcall(create, (pthread_t * thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg),
134                     (thread, attr, start_routine, arg), ((sthread_t*)thread, attr, start_routine, arg));
135 intercepted_pthcall(join, (pthread_t thread, void** retval), (thread, retval), ((sthread_t)thread, retval));
136
137 intercepted_pthcall(mutex_init, (pthread_mutex_t * mutex, const pthread_mutexattr_t* attr), (mutex, attr),
138                     ((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr));
139 intercepted_pthcall(mutex_lock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
140 intercepted_pthcall(mutex_trylock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
141 intercepted_pthcall(mutex_unlock, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
142 intercepted_pthcall(mutex_destroy, (pthread_mutex_t * mutex), (mutex), ((sthread_mutex_t*)mutex));
143
144 intercepted_pthcall(barrier_init, (pthread_barrier_t * barrier, const pthread_barrierattr_t* attr, unsigned count),
145                     (barrier, attr, count), ((sthread_barrier_t*)barrier, (const sthread_barrierattr_t*)attr, count));
146 intercepted_pthcall(barrier_wait, (pthread_barrier_t* barrier),( barrier),((sthread_barrier_t*) barrier));
147 intercepted_pthcall(barrier_destroy, (pthread_barrier_t* barrier),( barrier),((sthread_barrier_t*) barrier));
148
149 intercepted_pthcall(cond_init, (pthread_cond_t * cond, const pthread_condattr_t* attr), (cond, attr),
150                     ((sthread_cond_t*)cond, (sthread_condattr_t*)attr));
151 intercepted_pthcall(cond_signal, (pthread_cond_t * cond), (cond), ((sthread_cond_t*)cond));
152 intercepted_pthcall(cond_broadcast, (pthread_cond_t * cond), (cond), ((sthread_cond_t*)cond));
153 intercepted_pthcall(cond_wait, (pthread_cond_t * cond, pthread_mutex_t* mutex), (cond, mutex),
154                     ((sthread_cond_t*)cond, (sthread_mutex_t*)mutex));
155 intercepted_pthcall(cond_destroy, (pthread_cond_t * cond), (cond), ((sthread_cond_t*)cond));
156
157 #define intercepted_call(rettype, name, raw_params, call_params, sim_params)                                           \
158   rettype name raw_params                                                                                              \
159   {                                                                                                                    \
160     if (_STHREAD_CONCAT(raw_, name) == NULL)                                                                           \
161       intercepter_init();                                                                                              \
162     if (sthread_inside_simgrid)                                                                                        \
163       return _STHREAD_CONCAT(raw_, name) call_params;                                                                  \
164                                                                                                                        \
165     sthread_disable();                                                                                                 \
166     rettype res = _STHREAD_CONCAT(sthread_, name) sim_params;                                                          \
167     sthread_enable();                                                                                                  \
168     return res;                                                                                                        \
169   }
170
171 intercepted_call(int, sem_init, (sem_t * sem, int pshared, unsigned int value), (sem, pshared, value),
172                  ((sthread_sem_t*)sem, pshared, value));
173 intercepted_call(int, sem_destroy, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
174 intercepted_call(int, sem_post, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
175 intercepted_call(int, sem_wait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
176 intercepted_call(int, sem_trywait, (sem_t * sem), (sem), ((sthread_sem_t*)sem));
177 intercepted_call(int, sem_timedwait, (sem_t * sem, const struct timespec* abs_timeout), (sem, abs_timeout),
178                  ((sthread_sem_t*)sem, abs_timeout));
179
180 /* Glibc < 2.31 uses type "struct timezone *" for the second parameter of gettimeofday.
181    Other implementations use "void *" instead. */
182 #ifdef __GLIBC__
183 #if !__GLIBC_PREREQ(2, 31)
184 #define TIMEZONE_TYPE struct timezone
185 #endif
186 #endif
187 #ifndef TIMEZONE_TYPE
188 #define TIMEZONE_TYPE void
189 #endif
190 intercepted_call(int, gettimeofday, (struct timeval * tv, XBT_ATTRIB_UNUSED TIMEZONE_TYPE* tz), (tv, tz), (tv));
191 intercepted_call(unsigned int, sleep, (unsigned int seconds), (seconds), (seconds));
192 intercepted_call(int, usleep, (useconds_t usec), (usec), (((double)usec) / 1000000.));
193
194 /* Trampoline for the real main() */
195 static int (*raw_main)(int, char**, char**);
196
197 /* Our fake main() that gets called by __libc_start_main() */
198 static int main_hook(int argc, char** argv, char** envp)
199 {
200   return sthread_main(argc, argv, envp, raw_main);
201 }
202
203 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
204 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
205                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
206
207 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
208                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
209 {
210   /* Save the real main function address */
211   raw_main = main;
212
213   /* Find the real __libc_start_main()... */
214   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
215   /* ... and call it with our custom main function */
216 #if HAVE_VALGRIND_H
217   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
218   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
219     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
220 #endif
221   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
222 }