Logo AND Algorithmique Numérique Distribuée

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