blob: d84df4a6413c403a1d768908ef9d00820142e25e [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.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080094static void init(ThreadState *thr, uptr pc, int fd, FdSync *s,
95 bool write = true) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +000096 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +000097 // As a matter of fact, we don't intercept all close calls.
98 // See e.g. libc __res_iclose().
Dmitry Vyukov45d43242012-12-18 12:35:31 +000099 if (d->sync) {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000100 unref(thr, pc, d->sync);
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000101 d->sync = 0;
102 }
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000103 if (flags()->io_sync == 0) {
104 unref(thr, pc, s);
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000105 } else if (flags()->io_sync == 1) {
106 d->sync = s;
107 } else if (flags()->io_sync == 2) {
108 unref(thr, pc, s);
109 d->sync = &fdctx.globsync;
110 }
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000111 d->creation_tid = thr->tid;
112 d->creation_stack = CurrentStackId(thr, pc);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800113 if (write) {
114 // To catch races between fd usage and open.
115 MemoryRangeImitateWrite(thr, pc, (uptr)d, 8);
116 } else {
117 // See the dup-related comment in FdClose.
118 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
119 }
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000120}
121
122void FdInit() {
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000123 atomic_store(&fdctx.globsync.rc, (u64)-1, memory_order_relaxed);
124 atomic_store(&fdctx.filesync.rc, (u64)-1, memory_order_relaxed);
125 atomic_store(&fdctx.socksync.rc, (u64)-1, memory_order_relaxed);
126}
127
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +0000128void FdOnFork(ThreadState *thr, uptr pc) {
129 // On fork() we need to reset all fd's, because the child is going
130 // close all them, and that will cause races between previous read/write
131 // and the close.
132 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
133 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
134 if (tab == 0)
135 break;
136 for (int l2 = 0; l2 < kTableSizeL2; l2++) {
137 FdDesc *d = &tab[l2];
138 MemoryResetRange(thr, pc, (uptr)d, 8);
139 }
140 }
141}
142
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000143bool FdLocation(uptr addr, int *fd, int *tid, u32 *stack) {
144 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
145 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
146 if (tab == 0)
147 break;
148 if (addr >= (uptr)tab && addr < (uptr)(tab + kTableSizeL2)) {
149 int l2 = (addr - (uptr)tab) / sizeof(FdDesc);
150 FdDesc *d = &tab[l2];
151 *fd = l1 * kTableSizeL1 + l2;
152 *tid = d->creation_tid;
153 *stack = d->creation_stack;
154 return true;
155 }
156 }
157 return false;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000158}
159
160void FdAcquire(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000161 if (bogusfd(fd))
162 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000163 FdDesc *d = fddesc(thr, pc, fd);
164 FdSync *s = d->sync;
165 DPrintf("#%d: FdAcquire(%d) -> %p\n", thr->tid, fd, s);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000166 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000167 if (s)
168 Acquire(thr, pc, (uptr)s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000169}
170
171void FdRelease(ThreadState *thr, uptr pc, int fd) {
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000172 if (bogusfd(fd))
173 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000174 FdDesc *d = fddesc(thr, pc, fd);
175 FdSync *s = d->sync;
176 DPrintf("#%d: FdRelease(%d) -> %p\n", thr->tid, fd, s);
Dmitry Vyukov48e54202013-06-20 14:32:12 +0000177 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000178 if (s)
179 Release(thr, pc, (uptr)s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000180}
181
Dmitry Vyukova5972582013-01-09 17:31:27 +0000182void FdAccess(ThreadState *thr, uptr pc, int fd) {
183 DPrintf("#%d: FdAccess(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000184 if (bogusfd(fd))
185 return;
Dmitry Vyukova5972582013-01-09 17:31:27 +0000186 FdDesc *d = fddesc(thr, pc, fd);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000187 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
Dmitry Vyukova5972582013-01-09 17:31:27 +0000188}
189
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800190void FdClose(ThreadState *thr, uptr pc, int fd, bool write) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000191 DPrintf("#%d: FdClose(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000192 if (bogusfd(fd))
193 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000194 FdDesc *d = fddesc(thr, pc, fd);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800195 if (write) {
196 // To catch races between fd usage and close.
197 MemoryWrite(thr, pc, (uptr)d, kSizeLog8);
198 } else {
199 // This path is used only by dup2/dup3 calls.
200 // We do read instead of write because there is a number of legitimate
201 // cases where write would lead to false positives:
202 // 1. Some software dups a closed pipe in place of a socket before closing
203 // the socket (to prevent races actually).
204 // 2. Some daemons dup /dev/null in place of stdin/stdout.
205 // On the other hand we have not seen cases when write here catches real
206 // bugs.
207 MemoryRead(thr, pc, (uptr)d, kSizeLog8);
208 }
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000209 // We need to clear it, because if we do not intercept any call out there
210 // that creates fd, we will hit false postives.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000211 MemoryResetRange(thr, pc, (uptr)d, 8);
212 unref(thr, pc, d->sync);
213 d->sync = 0;
214 d->creation_tid = 0;
215 d->creation_stack = 0;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000216}
217
218void FdFileCreate(ThreadState *thr, uptr pc, int fd) {
219 DPrintf("#%d: FdFileCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000220 if (bogusfd(fd))
221 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000222 init(thr, pc, fd, &fdctx.filesync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000223}
224
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800225void FdDup(ThreadState *thr, uptr pc, int oldfd, int newfd, bool write) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000226 DPrintf("#%d: FdDup(%d, %d)\n", thr->tid, oldfd, newfd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000227 if (bogusfd(oldfd) || bogusfd(newfd))
228 return;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000229 // Ignore the case when user dups not yet connected socket.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000230 FdDesc *od = fddesc(thr, pc, oldfd);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000231 MemoryRead(thr, pc, (uptr)od, kSizeLog8);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800232 FdClose(thr, pc, newfd, write);
233 init(thr, pc, newfd, ref(od->sync), write);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000234}
235
236void FdPipeCreate(ThreadState *thr, uptr pc, int rfd, int wfd) {
237 DPrintf("#%d: FdCreatePipe(%d, %d)\n", thr->tid, rfd, wfd);
Stephen Hines6a211c52014-07-21 00:49:56 -0700238 FdSync *s = allocsync(thr, pc);
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000239 init(thr, pc, rfd, ref(s));
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000240 init(thr, pc, wfd, ref(s));
Dmitry Vyukove3178e82012-12-18 12:20:55 +0000241 unref(thr, pc, s);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000242}
243
244void FdEventCreate(ThreadState *thr, uptr pc, int fd) {
245 DPrintf("#%d: FdEventCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000246 if (bogusfd(fd))
247 return;
Stephen Hines6a211c52014-07-21 00:49:56 -0700248 init(thr, pc, fd, allocsync(thr, pc));
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000249}
250
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000251void FdSignalCreate(ThreadState *thr, uptr pc, int fd) {
252 DPrintf("#%d: FdSignalCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000253 if (bogusfd(fd))
254 return;
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000255 init(thr, pc, fd, 0);
256}
257
258void FdInotifyCreate(ThreadState *thr, uptr pc, int fd) {
259 DPrintf("#%d: FdInotifyCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000260 if (bogusfd(fd))
261 return;
Dmitry Vyukov45d43242012-12-18 12:35:31 +0000262 init(thr, pc, fd, 0);
263}
264
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000265void FdPollCreate(ThreadState *thr, uptr pc, int fd) {
266 DPrintf("#%d: FdPollCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000267 if (bogusfd(fd))
268 return;
Stephen Hines6a211c52014-07-21 00:49:56 -0700269 init(thr, pc, fd, allocsync(thr, pc));
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000270}
271
272void FdSocketCreate(ThreadState *thr, uptr pc, int fd) {
273 DPrintf("#%d: FdSocketCreate(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000274 if (bogusfd(fd))
275 return;
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000276 // It can be a UDP socket.
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000277 init(thr, pc, fd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000278}
279
280void FdSocketAccept(ThreadState *thr, uptr pc, int fd, int newfd) {
281 DPrintf("#%d: FdSocketAccept(%d, %d)\n", thr->tid, fd, newfd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000282 if (bogusfd(fd))
283 return;
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000284 // Synchronize connect->accept.
285 Acquire(thr, pc, (uptr)&fdctx.connectsync);
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000286 init(thr, pc, newfd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000287}
288
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000289void FdSocketConnecting(ThreadState *thr, uptr pc, int fd) {
290 DPrintf("#%d: FdSocketConnecting(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000291 if (bogusfd(fd))
292 return;
Dmitry Vyukoved513f62012-12-14 20:01:58 +0000293 // Synchronize connect->accept.
294 Release(thr, pc, (uptr)&fdctx.connectsync);
295}
296
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000297void FdSocketConnect(ThreadState *thr, uptr pc, int fd) {
298 DPrintf("#%d: FdSocketConnect(%d)\n", thr->tid, fd);
Dmitry Vyukov175599b2013-10-25 09:45:44 +0000299 if (bogusfd(fd))
300 return;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000301 init(thr, pc, fd, &fdctx.socksync);
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000302}
303
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700304uptr File2addr(const char *path) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000305 (void)path;
306 static u64 addr;
307 return (uptr)&addr;
308}
309
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700310uptr Dir2addr(const char *path) {
Dmitry Vyukovfb8ca812012-12-12 12:27:00 +0000311 (void)path;
312 static u64 addr;
313 return (uptr)&addr;
314}
315
316} // namespace __tsan