blob: 14bdbb53b322267fd92b3a75844194c184b974bb [file] [log] [blame]
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +00001//===-- tsan_fd.cc --------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "tsan_fd.h"
15#include "tsan_rtl.h"
16#include <sanitizer_common/sanitizer_atomic.h>
17
18namespace __tsan {
19
20const int kTableSizeL1 = 1024;
21const int kTableSizeL2 = 1024;
22const int kTableSize = kTableSizeL1 * kTableSizeL2;
23
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000024struct FdSync {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000025 atomic_uint64_t rc;
26};
27
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000028struct FdDesc {
29 FdSync *sync;
30 int creation_tid;
31 u32 creation_stack;
32};
33
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000034struct FdContext {
35 atomic_uintptr_t tab[kTableSizeL1];
36 // Addresses used for synchronization.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000037 FdSync globsync;
38 FdSync filesync;
39 FdSync socksync;
Dmitry Vyukoved513f62012-12-14 20:01:58 +000040 u64 connectsync;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000041};
42
43static FdContext fdctx;
44
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000045static FdSync *allocsync() {
46 FdSync *s = (FdSync*)internal_alloc(MBlockFD, sizeof(FdSync));
47 atomic_store(&s->rc, 1, memory_order_relaxed);
48 return s;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000049}
50
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000051static FdSync *ref(FdSync *s) {
52 if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1)
53 atomic_fetch_add(&s->rc, 1, memory_order_relaxed);
54 return s;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000055}
56
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000057static void unref(ThreadState *thr, uptr pc, FdSync *s) {
58 if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1) {
59 if (atomic_fetch_sub(&s->rc, 1, memory_order_acq_rel) == 1) {
60 CHECK_NE(s, &fdctx.globsync);
61 CHECK_NE(s, &fdctx.filesync);
62 CHECK_NE(s, &fdctx.socksync);
63 SyncVar *v = CTX()->synctab.GetAndRemove(thr, pc, (uptr)s);
64 if (v)
65 DestroyAndFree(v);
66 internal_free(s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000067 }
68 }
69}
70
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000071static FdDesc *fddesc(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000072 CHECK_LT(fd, kTableSize);
73 atomic_uintptr_t *pl1 = &fdctx.tab[fd / kTableSizeL2];
74 uptr l1 = atomic_load(pl1, memory_order_consume);
75 if (l1 == 0) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000076 uptr size = kTableSizeL2 * sizeof(FdDesc);
Alexey Samsonov77330172013-04-19 08:04:46 +000077 // We need this to reside in user memory to properly catch races on it.
78 void *p = user_alloc(thr, pc, size);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000079 internal_memset(p, 0, size);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000080 MemoryResetRange(thr, (uptr)&fddesc, (uptr)p, size);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000081 if (atomic_compare_exchange_strong(pl1, &l1, (uptr)p, memory_order_acq_rel))
82 l1 = (uptr)p;
83 else
Alexey Samsonov77330172013-04-19 08:04:46 +000084 user_free(thr, pc, p);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000085 }
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000086 return &((FdDesc*)l1)[fd % kTableSizeL2]; // NOLINT
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000087}
88
89// pd must be already ref'ed.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000090static void init(ThreadState *thr, uptr pc, int fd, FdSync *s) {
91 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000092 // As a matter of fact, we don't intercept all close calls.
93 // See e.g. libc __res_iclose().
Dmitry Vyukov45d43242012-12-18 12:35:31 +000094 if (d->sync) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000095 unref(thr, pc, d->sync);
Dmitry Vyukov45d43242012-12-18 12:35:31 +000096 d->sync = 0;
97 }
Dmitry Vyukove3178e82012-12-18 12:20:55 +000098 if (flags()->io_sync == 0) {
99 unref(thr, pc, s);
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000100 } else if (flags()->io_sync == 1) {
101 d->sync = s;
102 } else if (flags()->io_sync == 2) {
103 unref(thr, pc, s);
104 d->sync = &fdctx.globsync;
105 }
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000106 d->creation_tid = thr->tid;
107 d->creation_stack = CurrentStackId(thr, pc);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000108 // To catch races between fd usage and open.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000109 MemoryRangeImitateWrite(thr, pc, (uptr)d, 8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000110}
111
112void FdInit() {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000113 atomic_store(&fdctx.globsync.rc, (u64)-1, memory_order_relaxed);
114 atomic_store(&fdctx.filesync.rc, (u64)-1, memory_order_relaxed);
115 atomic_store(&fdctx.socksync.rc, (u64)-1, memory_order_relaxed);
116}
117
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +0000118void FdOnFork(ThreadState *thr, uptr pc) {
119 // On fork() we need to reset all fd's, because the child is going
120 // close all them, and that will cause races between previous read/write
121 // and the close.
122 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
123 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
124 if (tab == 0)
125 break;
126 for (int l2 = 0; l2 < kTableSizeL2; l2++) {
127 FdDesc *d = &tab[l2];
128 MemoryResetRange(thr, pc, (uptr)d, 8);
129 }
130 }
131}
132
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000133bool FdLocation(uptr addr, int *fd, int *tid, u32 *stack) {
134 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
135 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
136 if (tab == 0)
137 break;
138 if (addr >= (uptr)tab && addr < (uptr)(tab + kTableSizeL2)) {
139 int l2 = (addr - (uptr)tab) / sizeof(FdDesc);
140 FdDesc *d = &tab[l2];
141 *fd = l1 * kTableSizeL1 + l2;
142 *tid = d->creation_tid;
143 *stack = d->creation_stack;
144 return true;
145 }
146 }
147 return false;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000148}
149
150void FdAcquire(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000151 FdDesc *d = fddesc(thr, pc, fd);
152 FdSync *s = d->sync;
153 DPrintf("#%d: FdAcquire(%d) -> %p\n", thr->tid, fd, s);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000154 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000155 if (s)
156 Acquire(thr, pc, (uptr)s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000157}
158
159void FdRelease(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000160 FdDesc *d = fddesc(thr, pc, fd);
161 FdSync *s = d->sync;
162 DPrintf("#%d: FdRelease(%d) -> %p\n", thr->tid, fd, s);
163 if (s)
164 Release(thr, pc, (uptr)s);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000165 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000166}
167
Dmitry Vyukova5972582013-01-09 17:31:27 +0000168void FdAccess(ThreadState *thr, uptr pc, int fd) {
169 DPrintf("#%d: FdAccess(%d)\n", thr->tid, fd);
170 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000171 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukova5972582013-01-09 17:31:27 +0000172}
173
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000174void FdClose(ThreadState *thr, uptr pc, int fd) {
175 DPrintf("#%d: FdClose(%d)\n", thr->tid, fd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000176 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000177 // To catch races between fd usage and close.
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000178 MemoryWrite(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000179 // We need to clear it, because if we do not intercept any call out there
180 // that creates fd, we will hit false postives.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000181 MemoryResetRange(thr, pc, (uptr)d, 8);
182 unref(thr, pc, d->sync);
183 d->sync = 0;
184 d->creation_tid = 0;
185 d->creation_stack = 0;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000186}
187
188void FdFileCreate(ThreadState *thr, uptr pc, int fd) {
189 DPrintf("#%d: FdFileCreate(%d)\n", thr->tid, fd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000190 init(thr, pc, fd, &fdctx.filesync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000191}
192
193void FdDup(ThreadState *thr, uptr pc, int oldfd, int newfd) {
194 DPrintf("#%d: FdDup(%d, %d)\n", thr->tid, oldfd, newfd);
195 // Ignore the case when user dups not yet connected socket.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000196 FdDesc *od = fddesc(thr, pc, oldfd);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000197 MemoryRead(thr, pc, (uptr)od, kSizeLog8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000198 FdClose(thr, pc, newfd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000199 init(thr, pc, newfd, ref(od->sync));
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000200}
201
202void FdPipeCreate(ThreadState *thr, uptr pc, int rfd, int wfd) {
203 DPrintf("#%d: FdCreatePipe(%d, %d)\n", thr->tid, rfd, wfd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000204 FdSync *s = allocsync();
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000205 init(thr, pc, rfd, ref(s));
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000206 init(thr, pc, wfd, ref(s));
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000207 unref(thr, pc, s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000208}
209
210void FdEventCreate(ThreadState *thr, uptr pc, int fd) {
211 DPrintf("#%d: FdEventCreate(%d)\n", thr->tid, fd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000212 init(thr, pc, fd, allocsync());
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000213}
214
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000215void FdSignalCreate(ThreadState *thr, uptr pc, int fd) {
216 DPrintf("#%d: FdSignalCreate(%d)\n", thr->tid, fd);
217 init(thr, pc, fd, 0);
218}
219
220void FdInotifyCreate(ThreadState *thr, uptr pc, int fd) {
221 DPrintf("#%d: FdInotifyCreate(%d)\n", thr->tid, fd);
222 init(thr, pc, fd, 0);
223}
224
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000225void FdPollCreate(ThreadState *thr, uptr pc, int fd) {
226 DPrintf("#%d: FdPollCreate(%d)\n", thr->tid, fd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000227 init(thr, pc, fd, allocsync());
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000228}
229
230void FdSocketCreate(ThreadState *thr, uptr pc, int fd) {
231 DPrintf("#%d: FdSocketCreate(%d)\n", thr->tid, fd);
232 // It can be a UDP socket.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000233 init(thr, pc, fd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000234}
235
236void FdSocketAccept(ThreadState *thr, uptr pc, int fd, int newfd) {
237 DPrintf("#%d: FdSocketAccept(%d, %d)\n", thr->tid, fd, newfd);
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000238 // Synchronize connect->accept.
239 Acquire(thr, pc, (uptr)&fdctx.connectsync);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000240 init(thr, pc, newfd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000241}
242
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000243void FdSocketConnecting(ThreadState *thr, uptr pc, int fd) {
244 DPrintf("#%d: FdSocketConnecting(%d)\n", thr->tid, fd);
245 // Synchronize connect->accept.
246 Release(thr, pc, (uptr)&fdctx.connectsync);
247}
248
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000249void FdSocketConnect(ThreadState *thr, uptr pc, int fd) {
250 DPrintf("#%d: FdSocketConnect(%d)\n", thr->tid, fd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000251 init(thr, pc, fd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000252}
253
254uptr File2addr(char *path) {
255 (void)path;
256 static u64 addr;
257 return (uptr)&addr;
258}
259
260uptr Dir2addr(char *path) {
261 (void)path;
262 static u64 addr;
263 return (uptr)&addr;
264}
265
266} // namespace __tsan