blob: d18502f005403b4c095e5c627bc08800c0e012e0 [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 Vyukov175599b2013-10-25 09:45:44 +000045static bool bogusfd(int fd) {
46 // Apparently a bogus fd value.
Stephen Hines2d1fdb22014-05-28 23:58:16 -070047 return fd < 0 || fd >= kTableSize;
Dmitry Vyukov175599b2013-10-25 09:45:44 +000048}
49
Stephen Hines6a211c52014-07-21 00:49:56 -070050static FdSync *allocsync(ThreadState *thr, uptr pc) {
Stephen Hines6d186232014-11-26 17:56:19 -080051 FdSync *s = (FdSync*)user_alloc(thr, pc, sizeof(FdSync), kDefaultAlignment,
52 false);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000053 atomic_store(&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 FdSync *ref(FdSync *s) {
58 if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1)
59 atomic_fetch_add(&s->rc, 1, memory_order_relaxed);
60 return s;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000061}
62
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000063static void unref(ThreadState *thr, uptr pc, FdSync *s) {
64 if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1) {
65 if (atomic_fetch_sub(&s->rc, 1, memory_order_acq_rel) == 1) {
66 CHECK_NE(s, &fdctx.globsync);
67 CHECK_NE(s, &fdctx.filesync);
68 CHECK_NE(s, &fdctx.socksync);
Stephen Hines6d186232014-11-26 17:56:19 -080069 user_free(thr, pc, s, false);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000070 }
71 }
72}
73
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000074static FdDesc *fddesc(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukov175599b2013-10-25 09:45:44 +000075 CHECK_GE(fd, 0);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000076 CHECK_LT(fd, kTableSize);
77 atomic_uintptr_t *pl1 = &fdctx.tab[fd / kTableSizeL2];
78 uptr l1 = atomic_load(pl1, memory_order_consume);
79 if (l1 == 0) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000080 uptr size = kTableSizeL2 * sizeof(FdDesc);
Alexey Samsonov77330172013-04-19 08:04:46 +000081 // We need this to reside in user memory to properly catch races on it.
Stephen Hines6d186232014-11-26 17:56:19 -080082 void *p = user_alloc(thr, pc, size, kDefaultAlignment, false);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000083 internal_memset(p, 0, size);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000084 MemoryResetRange(thr, (uptr)&fddesc, (uptr)p, size);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000085 if (atomic_compare_exchange_strong(pl1, &l1, (uptr)p, memory_order_acq_rel))
86 l1 = (uptr)p;
87 else
Stephen Hines6d186232014-11-26 17:56:19 -080088 user_free(thr, pc, p, false);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000089 }
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000090 return &((FdDesc*)l1)[fd % kTableSizeL2]; // NOLINT
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000091}
92
93// pd must be already ref'ed.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000094static void init(ThreadState *thr, uptr pc, int fd, FdSync *s) {
95 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000096 // As a matter of fact, we don't intercept all close calls.
97 // See e.g. libc __res_iclose().
Dmitry Vyukov45d43242012-12-18 12:35:31 +000098 if (d->sync) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000099 unref(thr, pc, d->sync);
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000100 d->sync = 0;
101 }
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000102 if (flags()->io_sync == 0) {
103 unref(thr, pc, s);
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000104 } else if (flags()->io_sync == 1) {
105 d->sync = s;
106 } else if (flags()->io_sync == 2) {
107 unref(thr, pc, s);
108 d->sync = &fdctx.globsync;
109 }
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000110 d->creation_tid = thr->tid;
111 d->creation_stack = CurrentStackId(thr, pc);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000112 // To catch races between fd usage and open.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000113 MemoryRangeImitateWrite(thr, pc, (uptr)d, 8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000114}
115
116void FdInit() {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000117 atomic_store(&fdctx.globsync.rc, (u64)-1, memory_order_relaxed);
118 atomic_store(&fdctx.filesync.rc, (u64)-1, memory_order_relaxed);
119 atomic_store(&fdctx.socksync.rc, (u64)-1, memory_order_relaxed);
120}
121
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +0000122void FdOnFork(ThreadState *thr, uptr pc) {
123 // On fork() we need to reset all fd's, because the child is going
124 // close all them, and that will cause races between previous read/write
125 // and the close.
126 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
127 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
128 if (tab == 0)
129 break;
130 for (int l2 = 0; l2 < kTableSizeL2; l2++) {
131 FdDesc *d = &tab[l2];
132 MemoryResetRange(thr, pc, (uptr)d, 8);
133 }
134 }
135}
136
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000137bool FdLocation(uptr addr, int *fd, int *tid, u32 *stack) {
138 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
139 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
140 if (tab == 0)
141 break;
142 if (addr >= (uptr)tab && addr < (uptr)(tab + kTableSizeL2)) {
143 int l2 = (addr - (uptr)tab) / sizeof(FdDesc);
144 FdDesc *d = &tab[l2];
145 *fd = l1 * kTableSizeL1 + l2;
146 *tid = d->creation_tid;
147 *stack = d->creation_stack;
148 return true;
149 }
150 }
151 return false;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000152}
153
154void FdAcquire(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000155 if (bogusfd(fd))
156 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000157 FdDesc *d = fddesc(thr, pc, fd);
158 FdSync *s = d->sync;
159 DPrintf("#%d: FdAcquire(%d) -> %p\n", thr->tid, fd, s);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000160 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000161 if (s)
162 Acquire(thr, pc, (uptr)s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000163}
164
165void FdRelease(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000166 if (bogusfd(fd))
167 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000168 FdDesc *d = fddesc(thr, pc, fd);
169 FdSync *s = d->sync;
170 DPrintf("#%d: FdRelease(%d) -> %p\n", thr->tid, fd, s);
Dmitry Vyukov48e54202013-06-20 14:32:12 +0000171 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000172 if (s)
173 Release(thr, pc, (uptr)s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000174}
175
Dmitry Vyukova5972582013-01-09 17:31:27 +0000176void FdAccess(ThreadState *thr, uptr pc, int fd) {
177 DPrintf("#%d: FdAccess(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000178 if (bogusfd(fd))
179 return;
Dmitry Vyukova5972582013-01-09 17:31:27 +0000180 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000181 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukova5972582013-01-09 17:31:27 +0000182}
183
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000184void FdClose(ThreadState *thr, uptr pc, int fd) {
185 DPrintf("#%d: FdClose(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000186 if (bogusfd(fd))
187 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000188 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000189 // To catch races between fd usage and close.
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000190 MemoryWrite(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000191 // We need to clear it, because if we do not intercept any call out there
192 // that creates fd, we will hit false postives.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000193 MemoryResetRange(thr, pc, (uptr)d, 8);
194 unref(thr, pc, d->sync);
195 d->sync = 0;
196 d->creation_tid = 0;
197 d->creation_stack = 0;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000198}
199
200void FdFileCreate(ThreadState *thr, uptr pc, int fd) {
201 DPrintf("#%d: FdFileCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000202 if (bogusfd(fd))
203 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000204 init(thr, pc, fd, &fdctx.filesync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000205}
206
207void FdDup(ThreadState *thr, uptr pc, int oldfd, int newfd) {
208 DPrintf("#%d: FdDup(%d, %d)\n", thr->tid, oldfd, newfd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000209 if (bogusfd(oldfd) || bogusfd(newfd))
210 return;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000211 // Ignore the case when user dups not yet connected socket.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000212 FdDesc *od = fddesc(thr, pc, oldfd);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000213 MemoryRead(thr, pc, (uptr)od, kSizeLog8);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000214 FdClose(thr, pc, newfd);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000215 init(thr, pc, newfd, ref(od->sync));
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000216}
217
218void FdPipeCreate(ThreadState *thr, uptr pc, int rfd, int wfd) {
219 DPrintf("#%d: FdCreatePipe(%d, %d)\n", thr->tid, rfd, wfd);
Stephen Hines6a211c52014-07-21 00:49:56 -0700220 FdSync *s = allocsync(thr, pc);
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000221 init(thr, pc, rfd, ref(s));
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000222 init(thr, pc, wfd, ref(s));
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000223 unref(thr, pc, s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000224}
225
226void FdEventCreate(ThreadState *thr, uptr pc, int fd) {
227 DPrintf("#%d: FdEventCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000228 if (bogusfd(fd))
229 return;
Stephen Hines6a211c52014-07-21 00:49:56 -0700230 init(thr, pc, fd, allocsync(thr, pc));
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000231}
232
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000233void FdSignalCreate(ThreadState *thr, uptr pc, int fd) {
234 DPrintf("#%d: FdSignalCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000235 if (bogusfd(fd))
236 return;
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000237 init(thr, pc, fd, 0);
238}
239
240void FdInotifyCreate(ThreadState *thr, uptr pc, int fd) {
241 DPrintf("#%d: FdInotifyCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000242 if (bogusfd(fd))
243 return;
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000244 init(thr, pc, fd, 0);
245}
246
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000247void FdPollCreate(ThreadState *thr, uptr pc, int fd) {
248 DPrintf("#%d: FdPollCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000249 if (bogusfd(fd))
250 return;
Stephen Hines6a211c52014-07-21 00:49:56 -0700251 init(thr, pc, fd, allocsync(thr, pc));
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000252}
253
254void FdSocketCreate(ThreadState *thr, uptr pc, int fd) {
255 DPrintf("#%d: FdSocketCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000256 if (bogusfd(fd))
257 return;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000258 // It can be a UDP socket.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000259 init(thr, pc, fd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000260}
261
262void FdSocketAccept(ThreadState *thr, uptr pc, int fd, int newfd) {
263 DPrintf("#%d: FdSocketAccept(%d, %d)\n", thr->tid, fd, newfd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000264 if (bogusfd(fd))
265 return;
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000266 // Synchronize connect->accept.
267 Acquire(thr, pc, (uptr)&fdctx.connectsync);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000268 init(thr, pc, newfd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000269}
270
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000271void FdSocketConnecting(ThreadState *thr, uptr pc, int fd) {
272 DPrintf("#%d: FdSocketConnecting(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000273 if (bogusfd(fd))
274 return;
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000275 // Synchronize connect->accept.
276 Release(thr, pc, (uptr)&fdctx.connectsync);
277}
278
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000279void FdSocketConnect(ThreadState *thr, uptr pc, int fd) {
280 DPrintf("#%d: FdSocketConnect(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000281 if (bogusfd(fd))
282 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000283 init(thr, pc, fd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000284}
285
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700286uptr File2addr(const char *path) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000287 (void)path;
288 static u64 addr;
289 return (uptr)&addr;
290}
291
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700292uptr Dir2addr(const char *path) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000293 (void)path;
294 static u64 addr;
295 return (uptr)&addr;
296}
297
298} // namespace __tsan