1 //===-- dd_interceptors.cc ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "interception/interception.h"
11 #include "sanitizer_common/sanitizer_procmaps.h"
15 using namespace __dsan;
17 __attribute__((tls_model("initial-exec")))
18 static __thread Thread *thr;
19 __attribute__((tls_model("initial-exec")))
20 static __thread volatile int initing;
22 static uptr g_data_start;
23 static uptr g_data_end;
25 static bool InitThread() {
35 thr = (Thread*)InternalAlloc(sizeof(*thr));
36 internal_memset(thr, 0, sizeof(*thr));
42 INTERCEPTOR(int, pthread_mutex_destroy, pthread_mutex_t *m) {
44 MutexDestroy(thr, (uptr)m);
45 return REAL(pthread_mutex_destroy)(m);
48 INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *m) {
50 MutexBeforeLock(thr, (uptr)m, true);
51 int res = REAL(pthread_mutex_lock)(m);
52 MutexAfterLock(thr, (uptr)m, true, false);
56 INTERCEPTOR(int, pthread_mutex_trylock, pthread_mutex_t *m) {
58 int res = REAL(pthread_mutex_trylock)(m);
60 MutexAfterLock(thr, (uptr)m, true, true);
64 INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *m) {
66 MutexBeforeUnlock(thr, (uptr)m, true);
67 return REAL(pthread_mutex_unlock)(m);
70 INTERCEPTOR(int, pthread_spin_destroy, pthread_spinlock_t *m) {
72 int res = REAL(pthread_spin_destroy)(m);
73 MutexDestroy(thr, (uptr)m);
77 INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *m) {
79 MutexBeforeLock(thr, (uptr)m, true);
80 int res = REAL(pthread_spin_lock)(m);
81 MutexAfterLock(thr, (uptr)m, true, false);
85 INTERCEPTOR(int, pthread_spin_trylock, pthread_spinlock_t *m) {
87 int res = REAL(pthread_spin_trylock)(m);
89 MutexAfterLock(thr, (uptr)m, true, true);
93 INTERCEPTOR(int, pthread_spin_unlock, pthread_spinlock_t *m) {
95 MutexBeforeUnlock(thr, (uptr)m, true);
96 return REAL(pthread_spin_unlock)(m);
99 INTERCEPTOR(int, pthread_rwlock_destroy, pthread_rwlock_t *m) {
101 MutexDestroy(thr, (uptr)m);
102 return REAL(pthread_rwlock_destroy)(m);
105 INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *m) {
107 MutexBeforeLock(thr, (uptr)m, false);
108 int res = REAL(pthread_rwlock_rdlock)(m);
109 MutexAfterLock(thr, (uptr)m, false, false);
113 INTERCEPTOR(int, pthread_rwlock_tryrdlock, pthread_rwlock_t *m) {
115 int res = REAL(pthread_rwlock_tryrdlock)(m);
117 MutexAfterLock(thr, (uptr)m, false, true);
121 INTERCEPTOR(int, pthread_rwlock_timedrdlock, pthread_rwlock_t *m,
122 const timespec *abstime) {
124 int res = REAL(pthread_rwlock_timedrdlock)(m, abstime);
126 MutexAfterLock(thr, (uptr)m, false, true);
130 INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *m) {
132 MutexBeforeLock(thr, (uptr)m, true);
133 int res = REAL(pthread_rwlock_wrlock)(m);
134 MutexAfterLock(thr, (uptr)m, true, false);
138 INTERCEPTOR(int, pthread_rwlock_trywrlock, pthread_rwlock_t *m) {
140 int res = REAL(pthread_rwlock_trywrlock)(m);
142 MutexAfterLock(thr, (uptr)m, true, true);
146 INTERCEPTOR(int, pthread_rwlock_timedwrlock, pthread_rwlock_t *m,
147 const timespec *abstime) {
149 int res = REAL(pthread_rwlock_timedwrlock)(m, abstime);
151 MutexAfterLock(thr, (uptr)m, true, true);
155 INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *m) {
157 MutexBeforeUnlock(thr, (uptr)m, true); // note: not necessary write unlock
158 return REAL(pthread_rwlock_unlock)(m);
161 static pthread_cond_t *init_cond(pthread_cond_t *c, bool force = false) {
162 atomic_uintptr_t *p = (atomic_uintptr_t*)c;
163 uptr cond = atomic_load(p, memory_order_acquire);
164 if (!force && cond != 0)
165 return (pthread_cond_t*)cond;
166 void *newcond = malloc(sizeof(pthread_cond_t));
167 internal_memset(newcond, 0, sizeof(pthread_cond_t));
168 if (atomic_compare_exchange_strong(p, &cond, (uptr)newcond,
169 memory_order_acq_rel))
170 return (pthread_cond_t*)newcond;
172 return (pthread_cond_t*)cond;
175 INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *c,
176 const pthread_condattr_t *a) {
178 pthread_cond_t *cond = init_cond(c, true);
179 return REAL(pthread_cond_init)(cond, a);
182 INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *c, pthread_mutex_t *m) {
184 pthread_cond_t *cond = init_cond(c);
185 MutexBeforeUnlock(thr, (uptr)m, true);
186 MutexBeforeLock(thr, (uptr)m, true);
187 int res = REAL(pthread_cond_wait)(cond, m);
188 MutexAfterLock(thr, (uptr)m, true, false);
192 INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *c, pthread_mutex_t *m,
193 const timespec *abstime) {
195 pthread_cond_t *cond = init_cond(c);
196 MutexBeforeUnlock(thr, (uptr)m, true);
197 MutexBeforeLock(thr, (uptr)m, true);
198 int res = REAL(pthread_cond_timedwait)(cond, m, abstime);
199 MutexAfterLock(thr, (uptr)m, true, false);
203 INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *c) {
205 pthread_cond_t *cond = init_cond(c);
206 return REAL(pthread_cond_signal)(cond);
209 INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *c) {
211 pthread_cond_t *cond = init_cond(c);
212 return REAL(pthread_cond_broadcast)(cond);
215 INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *c) {
217 pthread_cond_t *cond = init_cond(c);
218 int res = REAL(pthread_cond_destroy)(cond);
220 atomic_store((atomic_uintptr_t*)c, 0, memory_order_relaxed);
225 INTERCEPTOR(char*, realpath, const char *path, char *resolved_path) {
227 return REAL(realpath)(path, resolved_path);
230 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
232 return REAL(read)(fd, ptr, count);
235 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
237 return REAL(pread)(fd, ptr, count, offset);
241 void __dsan_before_mutex_lock(uptr m, int writelock) {
244 MutexBeforeLock(thr, m, writelock);
247 void __dsan_after_mutex_lock(uptr m, int writelock, int trylock) {
250 MutexAfterLock(thr, m, writelock, trylock);
253 void __dsan_before_mutex_unlock(uptr m, int writelock) {
256 MutexBeforeUnlock(thr, m, writelock);
259 void __dsan_mutex_destroy(uptr m) {
262 // if (m >= g_data_start && m < g_data_end)
264 MutexDestroy(thr, m);
270 static void InitDataSeg() {
271 MemoryMappingLayout proc_maps(true);
273 MemoryMappedSegment segment(name, ARRAY_SIZE(name));
274 bool prev_is_data = false;
275 while (proc_maps.Next(&segment)) {
276 bool is_data = segment.offset != 0 && segment.filename[0] != 0;
277 // BSS may get merged with [heap] in /proc/self/maps. This is not very
279 bool is_bss = segment.offset == 0 &&
280 (segment.filename[0] == 0 ||
281 internal_strcmp(segment.filename, "[heap]") == 0) &&
283 if (g_data_start == 0 && is_data) g_data_start = segment.start;
284 if (is_bss) g_data_end = segment.end;
285 prev_is_data = is_data;
287 VPrintf(1, "guessed data_start=%p data_end=%p\n", g_data_start, g_data_end);
288 CHECK_LT(g_data_start, g_data_end);
289 CHECK_GE((uptr)&g_data_start, g_data_start);
290 CHECK_LT((uptr)&g_data_start, g_data_end);
293 void InitializeInterceptors() {
294 INTERCEPT_FUNCTION(pthread_mutex_destroy);
295 INTERCEPT_FUNCTION(pthread_mutex_lock);
296 INTERCEPT_FUNCTION(pthread_mutex_trylock);
297 INTERCEPT_FUNCTION(pthread_mutex_unlock);
299 INTERCEPT_FUNCTION(pthread_spin_destroy);
300 INTERCEPT_FUNCTION(pthread_spin_lock);
301 INTERCEPT_FUNCTION(pthread_spin_trylock);
302 INTERCEPT_FUNCTION(pthread_spin_unlock);
304 INTERCEPT_FUNCTION(pthread_rwlock_destroy);
305 INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
306 INTERCEPT_FUNCTION(pthread_rwlock_tryrdlock);
307 INTERCEPT_FUNCTION(pthread_rwlock_timedrdlock);
308 INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
309 INTERCEPT_FUNCTION(pthread_rwlock_trywrlock);
310 INTERCEPT_FUNCTION(pthread_rwlock_timedwrlock);
311 INTERCEPT_FUNCTION(pthread_rwlock_unlock);
313 INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2");
314 INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2");
315 INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
316 INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2");
317 INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
318 INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2");
321 INTERCEPT_FUNCTION(realpath);
322 INTERCEPT_FUNCTION(read);
323 INTERCEPT_FUNCTION(pread);
328 } // namespace __dsan