-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhandler.c
420 lines (383 loc) · 11 KB
/
handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* vim: ts=8:sw=8:tw=79:noet
*
* Copyright (c) 2011-2012, the Locksmith authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define LKSMITH_HANDLER_DOT_C
#include "error.h"
#include "lksmith.h"
#include "util.h"
#include "handler.h"
#include "platform.h"
#include <errno.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
/**
* Handler functions used to redirect pthreads calls to Locksmith.
*/
/**
* A list of mutex types that are compatible with error checking mutexes.
* Note that recursive mutexes are NOT compatible.
*/
static const int g_compatible_with_errcheck[] = {
#ifdef PTHREAD_MUTEX_TIMED_NP
PTHREAD_MUTEX_TIMED_NP,
#endif
#ifdef PTHREAD_MUTEX_ADAPTIVE_NP
PTHREAD_MUTEX_ADAPTIVE_NP,
#endif
#ifdef PTHREAD_MUTEX_FAST_NP
PTHREAD_MUTEX_FAST_NP,
#endif
PTHREAD_MUTEX_NORMAL,
PTHREAD_MUTEX_DEFAULT
};
#define NUM_COMPATIBLE_WITH_ERRCHECK (sizeof(g_compatible_with_errcheck) / \
sizeof(g_compatible_with_errcheck[0]))
static int is_compatible_with_errcheck(int ty)
{
unsigned int i;
for (i = 0; i < NUM_COMPATIBLE_WITH_ERRCHECK; i++) {
if (ty == g_compatible_with_errcheck[i])
return 1;
}
return 0;
}
static int pthread_mutex_init_errcheck(pthread_mutex_t *mutex)
{
int ret;
pthread_mutexattr_t attr;
ret = pthread_mutexattr_init(&attr);
if (ret) {
lksmith_error(ret, "pthread_mutexattr_init failed "
"with error code %d: %s\n", ret, terror(ret));
goto done;
}
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (ret) {
lksmith_error(ret, "pthread_mutexattr_settype failed "
"with error code %d: %s\n", ret, terror(ret));
goto done_free_mutexattr;
}
ret = r_pthread_mutex_init(mutex, &attr);
if (ret) {
lksmith_error(ret, "pthread_mutex_init failed "
"with error code %d: %s\n", ret, terror(ret));
goto done_free_mutexattr;
}
done_free_mutexattr:
pthread_mutexattr_destroy(&attr);
done:
return ret;
}
static int pthread_mutex_real_init(pthread_mutex_t *mutex,
pthread_mutexattr_t *attr, int *recursive)
{
int ret, ty = 0;
*recursive = 0;
if (!attr) {
/* No mutex attributes provided. Initialize this as an error
* checking mutex. */
return pthread_mutex_init_errcheck(mutex);
}
ret = pthread_mutexattr_gettype(attr, &ty);
if (ret == EINVAL) {
lksmith_error(ret, "pthread_mutexattr_gettype failed "
"with error code %d: %s\n", ret, terror(ret));
return ret;
}
if (is_compatible_with_errcheck(ty)) {
/* If the requested mutex type is compatible with the error
* checking type, let's use that type instead, for extra
* safety. */
ret = pthread_mutexattr_settype(attr,
PTHREAD_MUTEX_ERRORCHECK);
if (ret) {
lksmith_error(ret, "pthread_mutexattr_settype failed "
"with error code %d: %s\n", ret, terror(ret));
return ret;
}
} else {
/* If we don't know about the requested mutex type, we assume
* that it's recursive, to be on the safe side.
*/
*recursive = 1;
}
ret = r_pthread_mutex_init(mutex, attr);
if (ret) {
lksmith_error(ret, "pthread_mutex_init failed "
"with error code %d: %s\n", ret, terror(ret));
return ret;
}
return ret;
}
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr)
{
int ret, recursive = 0;
ret = init_tls();
if (ret)
return ret;
/*
* We have to cast away the const here, because the alternative,
* copying the pthread_mutexattr_t, is not feasible.
* pthread_mutexattr_t is an opaque type and no "copy" function is
* provided by pthreads. The set of accessor functions may vary by
* platform, so we can't use those to perform a copy either.
*
* So, we're casting away the const here. This should be safe in
* all cases. C/C++ compilers can't use const as an aide to
* optimization (due to the aliasing problem.)
* We also know that pthread_mutexattr_t is not stored in read-only
* memory, because the only way to initialize it is through
* pthread_mutexattr_init, which modifies it. There is no static
* initializer provided for pthread_mutexattr_t.
*/
ret = pthread_mutex_real_init(mutex, (pthread_mutexattr_t*)attr,
&recursive);
if (ret) {
/* It's nice to log a message when mutex initialization fails.
* It's a very rare scenario and something that a program can
* easily fail to check. */
lksmith_error(ret, "pthread_mutex_init(mutex=%p): "
"failed with error %s (%d)", mutex, terror(ret), ret);
return ret;
}
ret = lksmith_optional_init((const void*)mutex, recursive, 1);
if (ret) {
pthread_mutex_destroy(mutex);
return ret;
}
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
int ret;
ret = lksmith_destroy(mutex);
if ((ret != 0) && (ret != ENOENT)) {
/* We ignore ENOENT here because the mutex may have been
* initialized with PTHREAD_MUTEX_INITIALIZER and then
* destroyed after no interactions with Locksmith.
*
* In order to catch this case, we'd have to peek inside the
* mutex, which we don't want to do for portability reasons.
*/
return ret;
}
ret = r_pthread_mutex_destroy(mutex);
if (ret) {
lksmith_error(ret, "pthread_mutex_destroy(mutex=%p): "
"failed with error %s (%d)", mutex, terror(ret), ret);
}
return ret;
}
int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
int ret = lksmith_prelock(mutex, 1);
if (ret)
return ret;
ret = r_pthread_mutex_trylock(mutex);
lksmith_postlock(mutex, ret);
return ret;
}
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
int ret = lksmith_prelock(mutex, 1);
if (ret)
return ret;
ret = r_pthread_mutex_lock(mutex);
lksmith_postlock(mutex, ret);
return ret;
}
int pthread_mutex_timedlock(pthread_mutex_t *__restrict mutex,
__const struct timespec *__restrict ts)
{
int ret = lksmith_prelock(mutex, 1);
if (ret)
return ret;
ret = r_pthread_mutex_timedlock(mutex, ts);
lksmith_postlock(mutex, ret);
return ret;
}
int pthread_mutex_unlock(pthread_mutex_t *__restrict mutex)
{
int ret = lksmith_preunlock(mutex);
if (ret)
return ret;
ret = r_pthread_mutex_unlock(mutex);
if (ret)
return ret;
lksmith_postunlock(mutex);
return 0;
}
// TODO: pthread_rwlock stuff
int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
{
int ret;
ret = lksmith_optional_init((const void*)lock, 0, 0);
if (ret)
return ret;
ret = r_pthread_spin_init(lock, pshared);
if (ret) {
lksmith_error(ret, "pthread_spin_init(mutex=%p): "
"failed with error %s (%d)", lock, terror(ret), ret);
lksmith_destroy((const void*)lock);
return ret;
}
return 0;
}
int pthread_spin_destroy(pthread_spinlock_t *lock)
{
int ret;
ret = lksmith_destroy((const void*)lock);
if (ret)
return ret;
return r_pthread_spin_destroy(lock);
}
int pthread_spin_lock(pthread_spinlock_t *lock)
{
int ret = lksmith_prelock((const void*)lock, 0);
if (ret)
return ret;
ret = r_pthread_spin_lock(lock);
lksmith_postlock((const void*)lock, ret);
return ret;
}
int pthread_spin_trylock(pthread_spinlock_t *lock)
{
int ret = lksmith_prelock((const void*)lock, 0);
if (ret)
return ret;
ret = r_pthread_spin_trylock(lock);
lksmith_postlock((const void*)lock, ret);
return ret;
}
int pthread_spin_unlock(pthread_spinlock_t *lock)
{
int ret = lksmith_preunlock((const void*)lock);
if (ret)
return ret;
ret = r_pthread_spin_unlock(lock);
if (ret)
return ret;
lksmith_postunlock((const void*)lock);
return 0;
}
int pthread_cond_init(pthread_cond_t *__restrict cond,
const pthread_condattr_t *__restrict attr)
{
int ret = r_pthread_cond_init(cond, attr);
if (ret) {
lksmith_error(ret, "pthread_cond_init(mutex=%p): "
"failed with error %s (%d)", cond, terror(ret), ret);
}
return ret;
}
int pthread_cond_timedwait(pthread_cond_t *__restrict cond,
pthread_mutex_t *__restrict mutex,
const struct timespec *__restrict abstime)
{
struct lksmith_cond *cnd = NULL;
int ret = lksmith_check_locked((const void*)mutex);
if (ret > 0) {
return ret;
} else if (ret == -1) {
lksmith_error(EPERM, "pthread_cond_timedwait(cond=%p, "
"mutex=%p): you called pthread_cond_timedwait on "
"a mutex that you do not currently hold. Please "
"fix this serious error in your program.\n",
cond, mutex);
return EPERM;
}
ret = lksmith_cond_prewait(cond, mutex, &cnd);
if (ret)
return ret;
ret = r_pthread_cond_timedwait(cond, mutex, abstime);
lksmith_cond_postwait(cnd);
return ret;
}
int pthread_cond_wait(pthread_cond_t *__restrict cond,
pthread_mutex_t *__restrict mutex)
{
struct lksmith_cond *cnd = NULL;
int ret = lksmith_check_locked((const void*)mutex);
if (ret > 0) {
return ret;
} else if (ret == -1) {
lksmith_error(EPERM, "pthread_cond_wait(cond=%p, mutex=%p): "
"you called pthread_cond_wait on a mutex that you "
"do not currently hold. Please fix this serious "
"error in your program.\n", cond, mutex);
return EPERM;
}
ret = lksmith_cond_prewait(cond, mutex, &cnd);
if (ret)
return ret;
ret = r_pthread_cond_wait(cond, mutex);
lksmith_cond_postwait(cnd);
return ret;
}
int pthread_cond_destroy(pthread_cond_t *cond)
{
int ret = lksmith_cond_predestroy(cond);
if (ret)
return ret;
ret = r_pthread_cond_destroy(cond);
if (ret) {
lksmith_error(ret, "pthread_cond_destroy(cond=%p): "
"failed with error %s (%d)", cond, terror(ret), ret);
}
return ret;
}
// TODO: support barriers
#define LOAD_FUNC(fn) do { \
r_##fn = get_dlsym_next(#fn); \
if (!r_##fn) { \
return ELIBACC; \
} \
} while (0);
int lksmith_handler_init(void)
{
LOAD_FUNC(pthread_mutex_init);
LOAD_FUNC(pthread_mutex_destroy);
LOAD_FUNC(pthread_mutex_trylock);
LOAD_FUNC(pthread_mutex_lock);
LOAD_FUNC(pthread_mutex_timedlock);
LOAD_FUNC(pthread_mutex_unlock);
LOAD_FUNC(pthread_spin_init);
LOAD_FUNC(pthread_spin_destroy);
LOAD_FUNC(pthread_spin_lock);
LOAD_FUNC(pthread_spin_trylock);
LOAD_FUNC(pthread_spin_unlock);
LOAD_FUNC(pthread_cond_init);
LOAD_FUNC(pthread_cond_wait);
LOAD_FUNC(pthread_cond_timedwait);
LOAD_FUNC(pthread_cond_destroy);
return 0;
}
// TODO: support thread cancellation? ugh...