PR rtl-optimization/55547
[official-gcc.git] / libsanitizer / tsan / tsan_fd.cc
blob9aca9c51b38f0dcab31b070424272cdfb6af4af6
1 //===-- tsan_fd.cc --------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
12 #include "tsan_fd.h"
13 #include "tsan_rtl.h"
14 #include <sanitizer_common/sanitizer_atomic.h>
16 namespace __tsan {
18 const int kTableSizeL1 = 1024;
19 const int kTableSizeL2 = 1024;
20 const int kTableSize = kTableSizeL1 * kTableSizeL2;
22 struct FdSync {
23 atomic_uint64_t rc;
26 struct FdDesc {
27 FdSync *sync;
28 int creation_tid;
29 u32 creation_stack;
32 struct FdContext {
33 atomic_uintptr_t tab[kTableSizeL1];
34 // Addresses used for synchronization.
35 FdSync globsync;
36 FdSync filesync;
37 FdSync socksync;
38 u64 connectsync;
41 static FdContext fdctx;
43 static FdSync *allocsync() {
44 FdSync *s = (FdSync*)internal_alloc(MBlockFD, sizeof(FdSync));
45 atomic_store(&s->rc, 1, memory_order_relaxed);
46 return s;
49 static FdSync *ref(FdSync *s) {
50 if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1)
51 atomic_fetch_add(&s->rc, 1, memory_order_relaxed);
52 return s;
55 static void unref(ThreadState *thr, uptr pc, FdSync *s) {
56 if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1) {
57 if (atomic_fetch_sub(&s->rc, 1, memory_order_acq_rel) == 1) {
58 CHECK_NE(s, &fdctx.globsync);
59 CHECK_NE(s, &fdctx.filesync);
60 CHECK_NE(s, &fdctx.socksync);
61 SyncVar *v = CTX()->synctab.GetAndRemove(thr, pc, (uptr)s);
62 if (v)
63 DestroyAndFree(v);
64 internal_free(s);
69 static FdDesc *fddesc(ThreadState *thr, uptr pc, int fd) {
70 CHECK_LT(fd, kTableSize);
71 atomic_uintptr_t *pl1 = &fdctx.tab[fd / kTableSizeL2];
72 uptr l1 = atomic_load(pl1, memory_order_consume);
73 if (l1 == 0) {
74 uptr size = kTableSizeL2 * sizeof(FdDesc);
75 void *p = internal_alloc(MBlockFD, size);
76 internal_memset(p, 0, size);
77 MemoryResetRange(thr, (uptr)&fddesc, (uptr)p, size);
78 if (atomic_compare_exchange_strong(pl1, &l1, (uptr)p, memory_order_acq_rel))
79 l1 = (uptr)p;
80 else
81 internal_free(p);
83 return &((FdDesc*)l1)[fd % kTableSizeL2]; // NOLINT
86 // pd must be already ref'ed.
87 static void init(ThreadState *thr, uptr pc, int fd, FdSync *s) {
88 FdDesc *d = fddesc(thr, pc, fd);
89 // As a matter of fact, we don't intercept all close calls.
90 // See e.g. libc __res_iclose().
91 if (d->sync) {
92 unref(thr, pc, d->sync);
93 d->sync = 0;
95 if (flags()->io_sync == 0) {
96 unref(thr, pc, s);
97 } else if (flags()->io_sync == 1) {
98 d->sync = s;
99 } else if (flags()->io_sync == 2) {
100 unref(thr, pc, s);
101 d->sync = &fdctx.globsync;
103 d->creation_tid = thr->tid;
104 d->creation_stack = CurrentStackId(thr, pc);
105 // To catch races between fd usage and open.
106 MemoryRangeImitateWrite(thr, pc, (uptr)d, 8);
109 void FdInit() {
110 atomic_store(&fdctx.globsync.rc, (u64)-1, memory_order_relaxed);
111 atomic_store(&fdctx.filesync.rc, (u64)-1, memory_order_relaxed);
112 atomic_store(&fdctx.socksync.rc, (u64)-1, memory_order_relaxed);
115 void FdOnFork(ThreadState *thr, uptr pc) {
116 // On fork() we need to reset all fd's, because the child is going
117 // close all them, and that will cause races between previous read/write
118 // and the close.
119 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
120 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
121 if (tab == 0)
122 break;
123 for (int l2 = 0; l2 < kTableSizeL2; l2++) {
124 FdDesc *d = &tab[l2];
125 MemoryResetRange(thr, pc, (uptr)d, 8);
130 bool FdLocation(uptr addr, int *fd, int *tid, u32 *stack) {
131 for (int l1 = 0; l1 < kTableSizeL1; l1++) {
132 FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
133 if (tab == 0)
134 break;
135 if (addr >= (uptr)tab && addr < (uptr)(tab + kTableSizeL2)) {
136 int l2 = (addr - (uptr)tab) / sizeof(FdDesc);
137 FdDesc *d = &tab[l2];
138 *fd = l1 * kTableSizeL1 + l2;
139 *tid = d->creation_tid;
140 *stack = d->creation_stack;
141 return true;
144 return false;
147 void FdAcquire(ThreadState *thr, uptr pc, int fd) {
148 FdDesc *d = fddesc(thr, pc, fd);
149 FdSync *s = d->sync;
150 DPrintf("#%d: FdAcquire(%d) -> %p\n", thr->tid, fd, s);
151 MemoryRead8Byte(thr, pc, (uptr)d);
152 if (s)
153 Acquire(thr, pc, (uptr)s);
156 void FdRelease(ThreadState *thr, uptr pc, int fd) {
157 FdDesc *d = fddesc(thr, pc, fd);
158 FdSync *s = d->sync;
159 DPrintf("#%d: FdRelease(%d) -> %p\n", thr->tid, fd, s);
160 if (s)
161 Release(thr, pc, (uptr)s);
162 MemoryRead8Byte(thr, pc, (uptr)d);
165 void FdClose(ThreadState *thr, uptr pc, int fd) {
166 DPrintf("#%d: FdClose(%d)\n", thr->tid, fd);
167 FdDesc *d = fddesc(thr, pc, fd);
168 // To catch races between fd usage and close.
169 MemoryWrite8Byte(thr, pc, (uptr)d);
170 // We need to clear it, because if we do not intercept any call out there
171 // that creates fd, we will hit false postives.
172 MemoryResetRange(thr, pc, (uptr)d, 8);
173 unref(thr, pc, d->sync);
174 d->sync = 0;
175 d->creation_tid = 0;
176 d->creation_stack = 0;
179 void FdFileCreate(ThreadState *thr, uptr pc, int fd) {
180 DPrintf("#%d: FdFileCreate(%d)\n", thr->tid, fd);
181 init(thr, pc, fd, &fdctx.filesync);
184 void FdDup(ThreadState *thr, uptr pc, int oldfd, int newfd) {
185 DPrintf("#%d: FdDup(%d, %d)\n", thr->tid, oldfd, newfd);
186 // Ignore the case when user dups not yet connected socket.
187 FdDesc *od = fddesc(thr, pc, oldfd);
188 MemoryRead8Byte(thr, pc, (uptr)od);
189 FdClose(thr, pc, newfd);
190 init(thr, pc, newfd, ref(od->sync));
193 void FdPipeCreate(ThreadState *thr, uptr pc, int rfd, int wfd) {
194 DPrintf("#%d: FdCreatePipe(%d, %d)\n", thr->tid, rfd, wfd);
195 FdSync *s = allocsync();
196 init(thr, pc, rfd, ref(s));
197 init(thr, pc, wfd, ref(s));
198 unref(thr, pc, s);
201 void FdEventCreate(ThreadState *thr, uptr pc, int fd) {
202 DPrintf("#%d: FdEventCreate(%d)\n", thr->tid, fd);
203 init(thr, pc, fd, allocsync());
206 void FdSignalCreate(ThreadState *thr, uptr pc, int fd) {
207 DPrintf("#%d: FdSignalCreate(%d)\n", thr->tid, fd);
208 init(thr, pc, fd, 0);
211 void FdInotifyCreate(ThreadState *thr, uptr pc, int fd) {
212 DPrintf("#%d: FdInotifyCreate(%d)\n", thr->tid, fd);
213 init(thr, pc, fd, 0);
216 void FdPollCreate(ThreadState *thr, uptr pc, int fd) {
217 DPrintf("#%d: FdPollCreate(%d)\n", thr->tid, fd);
218 init(thr, pc, fd, allocsync());
221 void FdSocketCreate(ThreadState *thr, uptr pc, int fd) {
222 DPrintf("#%d: FdSocketCreate(%d)\n", thr->tid, fd);
223 // It can be a UDP socket.
224 init(thr, pc, fd, &fdctx.socksync);
227 void FdSocketAccept(ThreadState *thr, uptr pc, int fd, int newfd) {
228 DPrintf("#%d: FdSocketAccept(%d, %d)\n", thr->tid, fd, newfd);
229 // Synchronize connect->accept.
230 Acquire(thr, pc, (uptr)&fdctx.connectsync);
231 init(thr, pc, newfd, &fdctx.socksync);
234 void FdSocketConnecting(ThreadState *thr, uptr pc, int fd) {
235 DPrintf("#%d: FdSocketConnecting(%d)\n", thr->tid, fd);
236 // Synchronize connect->accept.
237 Release(thr, pc, (uptr)&fdctx.connectsync);
240 void FdSocketConnect(ThreadState *thr, uptr pc, int fd) {
241 DPrintf("#%d: FdSocketConnect(%d)\n", thr->tid, fd);
242 init(thr, pc, fd, &fdctx.socksync);
245 uptr File2addr(char *path) {
246 (void)path;
247 static u64 addr;
248 return (uptr)&addr;
251 uptr Dir2addr(char *path) {
252 (void)path;
253 static u64 addr;
254 return (uptr)&addr;
257 } // namespace __tsan