dd_interceptors.cpp
9.28 KB
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
//===-- dd_interceptors.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "dd_rtl.h"
#include "interception/interception.h"
#include "sanitizer_common/sanitizer_procmaps.h"
#include <pthread.h>
#include <stdlib.h>
using namespace __dsan;
__attribute__((tls_model("initial-exec")))
static __thread Thread *thr;
__attribute__((tls_model("initial-exec")))
static __thread volatile int initing;
static bool inited;
static uptr g_data_start;
static uptr g_data_end;
static bool InitThread() {
if (initing)
return false;
if (thr != 0)
return true;
initing = true;
if (!inited) {
inited = true;
Initialize();
}
thr = (Thread*)InternalAlloc(sizeof(*thr));
internal_memset(thr, 0, sizeof(*thr));
ThreadInit(thr);
initing = false;
return true;
}
INTERCEPTOR(int, pthread_mutex_destroy, pthread_mutex_t *m) {
InitThread();
MutexDestroy(thr, (uptr)m);
return REAL(pthread_mutex_destroy)(m);
}
INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *m) {
InitThread();
MutexBeforeLock(thr, (uptr)m, true);
int res = REAL(pthread_mutex_lock)(m);
MutexAfterLock(thr, (uptr)m, true, false);
return res;
}
INTERCEPTOR(int, pthread_mutex_trylock, pthread_mutex_t *m) {
InitThread();
int res = REAL(pthread_mutex_trylock)(m);
if (res == 0)
MutexAfterLock(thr, (uptr)m, true, true);
return res;
}
INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *m) {
InitThread();
MutexBeforeUnlock(thr, (uptr)m, true);
return REAL(pthread_mutex_unlock)(m);
}
INTERCEPTOR(int, pthread_spin_destroy, pthread_spinlock_t *m) {
InitThread();
int res = REAL(pthread_spin_destroy)(m);
MutexDestroy(thr, (uptr)m);
return res;
}
INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *m) {
InitThread();
MutexBeforeLock(thr, (uptr)m, true);
int res = REAL(pthread_spin_lock)(m);
MutexAfterLock(thr, (uptr)m, true, false);
return res;
}
INTERCEPTOR(int, pthread_spin_trylock, pthread_spinlock_t *m) {
InitThread();
int res = REAL(pthread_spin_trylock)(m);
if (res == 0)
MutexAfterLock(thr, (uptr)m, true, true);
return res;
}
INTERCEPTOR(int, pthread_spin_unlock, pthread_spinlock_t *m) {
InitThread();
MutexBeforeUnlock(thr, (uptr)m, true);
return REAL(pthread_spin_unlock)(m);
}
INTERCEPTOR(int, pthread_rwlock_destroy, pthread_rwlock_t *m) {
InitThread();
MutexDestroy(thr, (uptr)m);
return REAL(pthread_rwlock_destroy)(m);
}
INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *m) {
InitThread();
MutexBeforeLock(thr, (uptr)m, false);
int res = REAL(pthread_rwlock_rdlock)(m);
MutexAfterLock(thr, (uptr)m, false, false);
return res;
}
INTERCEPTOR(int, pthread_rwlock_tryrdlock, pthread_rwlock_t *m) {
InitThread();
int res = REAL(pthread_rwlock_tryrdlock)(m);
if (res == 0)
MutexAfterLock(thr, (uptr)m, false, true);
return res;
}
INTERCEPTOR(int, pthread_rwlock_timedrdlock, pthread_rwlock_t *m,
const timespec *abstime) {
InitThread();
int res = REAL(pthread_rwlock_timedrdlock)(m, abstime);
if (res == 0)
MutexAfterLock(thr, (uptr)m, false, true);
return res;
}
INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *m) {
InitThread();
MutexBeforeLock(thr, (uptr)m, true);
int res = REAL(pthread_rwlock_wrlock)(m);
MutexAfterLock(thr, (uptr)m, true, false);
return res;
}
INTERCEPTOR(int, pthread_rwlock_trywrlock, pthread_rwlock_t *m) {
InitThread();
int res = REAL(pthread_rwlock_trywrlock)(m);
if (res == 0)
MutexAfterLock(thr, (uptr)m, true, true);
return res;
}
INTERCEPTOR(int, pthread_rwlock_timedwrlock, pthread_rwlock_t *m,
const timespec *abstime) {
InitThread();
int res = REAL(pthread_rwlock_timedwrlock)(m, abstime);
if (res == 0)
MutexAfterLock(thr, (uptr)m, true, true);
return res;
}
INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *m) {
InitThread();
MutexBeforeUnlock(thr, (uptr)m, true); // note: not necessary write unlock
return REAL(pthread_rwlock_unlock)(m);
}
static pthread_cond_t *init_cond(pthread_cond_t *c, bool force = false) {
atomic_uintptr_t *p = (atomic_uintptr_t*)c;
uptr cond = atomic_load(p, memory_order_acquire);
if (!force && cond != 0)
return (pthread_cond_t*)cond;
void *newcond = malloc(sizeof(pthread_cond_t));
internal_memset(newcond, 0, sizeof(pthread_cond_t));
if (atomic_compare_exchange_strong(p, &cond, (uptr)newcond,
memory_order_acq_rel))
return (pthread_cond_t*)newcond;
free(newcond);
return (pthread_cond_t*)cond;
}
INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *c,
const pthread_condattr_t *a) {
InitThread();
pthread_cond_t *cond = init_cond(c, true);
return REAL(pthread_cond_init)(cond, a);
}
INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *c, pthread_mutex_t *m) {
InitThread();
pthread_cond_t *cond = init_cond(c);
MutexBeforeUnlock(thr, (uptr)m, true);
MutexBeforeLock(thr, (uptr)m, true);
int res = REAL(pthread_cond_wait)(cond, m);
MutexAfterLock(thr, (uptr)m, true, false);
return res;
}
INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *c, pthread_mutex_t *m,
const timespec *abstime) {
InitThread();
pthread_cond_t *cond = init_cond(c);
MutexBeforeUnlock(thr, (uptr)m, true);
MutexBeforeLock(thr, (uptr)m, true);
int res = REAL(pthread_cond_timedwait)(cond, m, abstime);
MutexAfterLock(thr, (uptr)m, true, false);
return res;
}
INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *c) {
InitThread();
pthread_cond_t *cond = init_cond(c);
return REAL(pthread_cond_signal)(cond);
}
INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *c) {
InitThread();
pthread_cond_t *cond = init_cond(c);
return REAL(pthread_cond_broadcast)(cond);
}
INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *c) {
InitThread();
pthread_cond_t *cond = init_cond(c);
int res = REAL(pthread_cond_destroy)(cond);
free(cond);
atomic_store((atomic_uintptr_t*)c, 0, memory_order_relaxed);
return res;
}
// for symbolizer
INTERCEPTOR(char*, realpath, const char *path, char *resolved_path) {
InitThread();
return REAL(realpath)(path, resolved_path);
}
INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
InitThread();
return REAL(read)(fd, ptr, count);
}
INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
InitThread();
return REAL(pread)(fd, ptr, count, offset);
}
extern "C" {
void __dsan_before_mutex_lock(uptr m, int writelock) {
if (!InitThread())
return;
MutexBeforeLock(thr, m, writelock);
}
void __dsan_after_mutex_lock(uptr m, int writelock, int trylock) {
if (!InitThread())
return;
MutexAfterLock(thr, m, writelock, trylock);
}
void __dsan_before_mutex_unlock(uptr m, int writelock) {
if (!InitThread())
return;
MutexBeforeUnlock(thr, m, writelock);
}
void __dsan_mutex_destroy(uptr m) {
if (!InitThread())
return;
// if (m >= g_data_start && m < g_data_end)
// return;
MutexDestroy(thr, m);
}
} // extern "C"
namespace __dsan {
static void InitDataSeg() {
MemoryMappingLayout proc_maps(true);
char name[128];
MemoryMappedSegment segment(name, ARRAY_SIZE(name));
bool prev_is_data = false;
while (proc_maps.Next(&segment)) {
bool is_data = segment.offset != 0 && segment.filename[0] != 0;
// BSS may get merged with [heap] in /proc/self/maps. This is not very
// reliable.
bool is_bss = segment.offset == 0 &&
(segment.filename[0] == 0 ||
internal_strcmp(segment.filename, "[heap]") == 0) &&
prev_is_data;
if (g_data_start == 0 && is_data) g_data_start = segment.start;
if (is_bss) g_data_end = segment.end;
prev_is_data = is_data;
}
VPrintf(1, "guessed data_start=%p data_end=%p\n", g_data_start, g_data_end);
CHECK_LT(g_data_start, g_data_end);
CHECK_GE((uptr)&g_data_start, g_data_start);
CHECK_LT((uptr)&g_data_start, g_data_end);
}
void InitializeInterceptors() {
INTERCEPT_FUNCTION(pthread_mutex_destroy);
INTERCEPT_FUNCTION(pthread_mutex_lock);
INTERCEPT_FUNCTION(pthread_mutex_trylock);
INTERCEPT_FUNCTION(pthread_mutex_unlock);
INTERCEPT_FUNCTION(pthread_spin_destroy);
INTERCEPT_FUNCTION(pthread_spin_lock);
INTERCEPT_FUNCTION(pthread_spin_trylock);
INTERCEPT_FUNCTION(pthread_spin_unlock);
INTERCEPT_FUNCTION(pthread_rwlock_destroy);
INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
INTERCEPT_FUNCTION(pthread_rwlock_tryrdlock);
INTERCEPT_FUNCTION(pthread_rwlock_timedrdlock);
INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
INTERCEPT_FUNCTION(pthread_rwlock_trywrlock);
INTERCEPT_FUNCTION(pthread_rwlock_timedwrlock);
INTERCEPT_FUNCTION(pthread_rwlock_unlock);
INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2");
INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2");
INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2");
INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2");
// for symbolizer
INTERCEPT_FUNCTION(realpath);
INTERCEPT_FUNCTION(read);
INTERCEPT_FUNCTION(pread);
InitDataSeg();
}
} // namespace __dsan