tsan_fd.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //===-- tsan_fd.cpp -------------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "tsan_fd.h"
  13. #include <sanitizer_common/sanitizer_atomic.h>
  14. #include "tsan_interceptors.h"
  15. #include "tsan_rtl.h"
  16. namespace __tsan {
  17. const int kTableSizeL1 = 1024;
  18. const int kTableSizeL2 = 1024;
  19. const int kTableSize = kTableSizeL1 * kTableSizeL2;
  20. struct FdSync {
  21. atomic_uint64_t rc;
  22. };
  23. struct FdDesc {
  24. FdSync *sync;
  25. // This is used to establish write -> epoll_wait synchronization
  26. // where epoll_wait receives notification about the write.
  27. atomic_uintptr_t aux_sync; // FdSync*
  28. Tid creation_tid;
  29. StackID creation_stack;
  30. bool closed;
  31. };
  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;
  39. };
  40. static FdContext fdctx;
  41. static bool bogusfd(int fd) {
  42. // Apparently a bogus fd value.
  43. return fd < 0 || fd >= kTableSize;
  44. }
  45. static FdSync *allocsync(ThreadState *thr, uptr pc) {
  46. FdSync *s = (FdSync*)user_alloc_internal(thr, pc, sizeof(FdSync),
  47. kDefaultAlignment, false);
  48. atomic_store(&s->rc, 1, memory_order_relaxed);
  49. return s;
  50. }
  51. static 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;
  55. }
  56. static void unref(ThreadState *thr, uptr pc, FdSync *s) {
  57. if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1) {
  58. if (atomic_fetch_sub(&s->rc, 1, memory_order_acq_rel) == 1) {
  59. CHECK_NE(s, &fdctx.globsync);
  60. CHECK_NE(s, &fdctx.filesync);
  61. CHECK_NE(s, &fdctx.socksync);
  62. user_free(thr, pc, s, false);
  63. }
  64. }
  65. }
  66. static FdDesc *fddesc(ThreadState *thr, uptr pc, int fd) {
  67. CHECK_GE(fd, 0);
  68. CHECK_LT(fd, kTableSize);
  69. atomic_uintptr_t *pl1 = &fdctx.tab[fd / kTableSizeL2];
  70. uptr l1 = atomic_load(pl1, memory_order_consume);
  71. if (l1 == 0) {
  72. uptr size = kTableSizeL2 * sizeof(FdDesc);
  73. // We need this to reside in user memory to properly catch races on it.
  74. void *p = user_alloc_internal(thr, pc, size, kDefaultAlignment, false);
  75. internal_memset(p, 0, size);
  76. MemoryResetRange(thr, (uptr)&fddesc, (uptr)p, size);
  77. if (atomic_compare_exchange_strong(pl1, &l1, (uptr)p, memory_order_acq_rel))
  78. l1 = (uptr)p;
  79. else
  80. user_free(thr, pc, p, false);
  81. }
  82. FdDesc *fds = reinterpret_cast<FdDesc *>(l1);
  83. return &fds[fd % kTableSizeL2];
  84. }
  85. // pd must be already ref'ed.
  86. static void init(ThreadState *thr, uptr pc, int fd, FdSync *s,
  87. bool write = true) {
  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;
  94. }
  95. unref(thr, pc,
  96. reinterpret_cast<FdSync *>(
  97. atomic_load(&d->aux_sync, memory_order_relaxed)));
  98. atomic_store(&d->aux_sync, 0, memory_order_relaxed);
  99. if (flags()->io_sync == 0) {
  100. unref(thr, pc, s);
  101. } else if (flags()->io_sync == 1) {
  102. d->sync = s;
  103. } else if (flags()->io_sync == 2) {
  104. unref(thr, pc, s);
  105. d->sync = &fdctx.globsync;
  106. }
  107. d->creation_tid = thr->tid;
  108. d->creation_stack = CurrentStackId(thr, pc);
  109. d->closed = false;
  110. // This prevents false positives on fd_close_norace3.cpp test.
  111. // The mechanics of the false positive are not completely clear,
  112. // but it happens only if global reset is enabled (flush_memory_ms=1)
  113. // and may be related to lost writes during asynchronous MADV_DONTNEED.
  114. SlotLocker locker(thr);
  115. if (write) {
  116. // To catch races between fd usage and open.
  117. MemoryRangeImitateWrite(thr, pc, (uptr)d, 8);
  118. } else {
  119. // See the dup-related comment in FdClose.
  120. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead | kAccessSlotLocked);
  121. }
  122. }
  123. void FdInit() {
  124. atomic_store(&fdctx.globsync.rc, (u64)-1, memory_order_relaxed);
  125. atomic_store(&fdctx.filesync.rc, (u64)-1, memory_order_relaxed);
  126. atomic_store(&fdctx.socksync.rc, (u64)-1, memory_order_relaxed);
  127. }
  128. void 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. bool FdLocation(uptr addr, int *fd, Tid *tid, StackID *stack, bool *closed) {
  143. for (int l1 = 0; l1 < kTableSizeL1; l1++) {
  144. FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
  145. if (tab == 0)
  146. break;
  147. if (addr >= (uptr)tab && addr < (uptr)(tab + kTableSizeL2)) {
  148. int l2 = (addr - (uptr)tab) / sizeof(FdDesc);
  149. FdDesc *d = &tab[l2];
  150. *fd = l1 * kTableSizeL1 + l2;
  151. *tid = d->creation_tid;
  152. *stack = d->creation_stack;
  153. *closed = d->closed;
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. void FdAcquire(ThreadState *thr, uptr pc, int fd) {
  160. if (bogusfd(fd))
  161. return;
  162. FdDesc *d = fddesc(thr, pc, fd);
  163. FdSync *s = d->sync;
  164. DPrintf("#%d: FdAcquire(%d) -> %p\n", thr->tid, fd, s);
  165. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  166. if (s)
  167. Acquire(thr, pc, (uptr)s);
  168. }
  169. void FdRelease(ThreadState *thr, uptr pc, int fd) {
  170. if (bogusfd(fd))
  171. return;
  172. FdDesc *d = fddesc(thr, pc, fd);
  173. FdSync *s = d->sync;
  174. DPrintf("#%d: FdRelease(%d) -> %p\n", thr->tid, fd, s);
  175. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  176. if (s)
  177. Release(thr, pc, (uptr)s);
  178. if (uptr aux_sync = atomic_load(&d->aux_sync, memory_order_acquire))
  179. Release(thr, pc, aux_sync);
  180. }
  181. void FdAccess(ThreadState *thr, uptr pc, int fd) {
  182. DPrintf("#%d: FdAccess(%d)\n", thr->tid, fd);
  183. if (bogusfd(fd))
  184. return;
  185. FdDesc *d = fddesc(thr, pc, fd);
  186. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  187. }
  188. void FdClose(ThreadState *thr, uptr pc, int fd, bool write) {
  189. DPrintf("#%d: FdClose(%d)\n", thr->tid, fd);
  190. if (bogusfd(fd))
  191. return;
  192. FdDesc *d = fddesc(thr, pc, fd);
  193. {
  194. // Need to lock the slot to make MemoryAccess and MemoryResetRange atomic
  195. // with respect to global reset. See the comment in MemoryRangeFreed.
  196. SlotLocker locker(thr);
  197. if (!MustIgnoreInterceptor(thr)) {
  198. if (write) {
  199. // To catch races between fd usage and close.
  200. MemoryAccess(thr, pc, (uptr)d, 8,
  201. kAccessWrite | kAccessCheckOnly | kAccessSlotLocked);
  202. } else {
  203. // This path is used only by dup2/dup3 calls.
  204. // We do read instead of write because there is a number of legitimate
  205. // cases where write would lead to false positives:
  206. // 1. Some software dups a closed pipe in place of a socket before
  207. // closing
  208. // the socket (to prevent races actually).
  209. // 2. Some daemons dup /dev/null in place of stdin/stdout.
  210. // On the other hand we have not seen cases when write here catches real
  211. // bugs.
  212. MemoryAccess(thr, pc, (uptr)d, 8,
  213. kAccessRead | kAccessCheckOnly | kAccessSlotLocked);
  214. }
  215. }
  216. // We need to clear it, because if we do not intercept any call out there
  217. // that creates fd, we will hit false postives.
  218. MemoryResetRange(thr, pc, (uptr)d, 8);
  219. }
  220. unref(thr, pc, d->sync);
  221. d->sync = 0;
  222. unref(thr, pc,
  223. reinterpret_cast<FdSync *>(
  224. atomic_load(&d->aux_sync, memory_order_relaxed)));
  225. atomic_store(&d->aux_sync, 0, memory_order_relaxed);
  226. d->closed = true;
  227. d->creation_tid = thr->tid;
  228. d->creation_stack = CurrentStackId(thr, pc);
  229. }
  230. void FdFileCreate(ThreadState *thr, uptr pc, int fd) {
  231. DPrintf("#%d: FdFileCreate(%d)\n", thr->tid, fd);
  232. if (bogusfd(fd))
  233. return;
  234. init(thr, pc, fd, &fdctx.filesync);
  235. }
  236. void FdDup(ThreadState *thr, uptr pc, int oldfd, int newfd, bool write) {
  237. DPrintf("#%d: FdDup(%d, %d)\n", thr->tid, oldfd, newfd);
  238. if (bogusfd(oldfd) || bogusfd(newfd))
  239. return;
  240. // Ignore the case when user dups not yet connected socket.
  241. FdDesc *od = fddesc(thr, pc, oldfd);
  242. MemoryAccess(thr, pc, (uptr)od, 8, kAccessRead);
  243. FdClose(thr, pc, newfd, write);
  244. init(thr, pc, newfd, ref(od->sync), write);
  245. }
  246. void FdPipeCreate(ThreadState *thr, uptr pc, int rfd, int wfd) {
  247. DPrintf("#%d: FdCreatePipe(%d, %d)\n", thr->tid, rfd, wfd);
  248. FdSync *s = allocsync(thr, pc);
  249. init(thr, pc, rfd, ref(s));
  250. init(thr, pc, wfd, ref(s));
  251. unref(thr, pc, s);
  252. }
  253. void FdEventCreate(ThreadState *thr, uptr pc, int fd) {
  254. DPrintf("#%d: FdEventCreate(%d)\n", thr->tid, fd);
  255. if (bogusfd(fd))
  256. return;
  257. init(thr, pc, fd, allocsync(thr, pc));
  258. }
  259. void FdSignalCreate(ThreadState *thr, uptr pc, int fd) {
  260. DPrintf("#%d: FdSignalCreate(%d)\n", thr->tid, fd);
  261. if (bogusfd(fd))
  262. return;
  263. init(thr, pc, fd, 0);
  264. }
  265. void FdInotifyCreate(ThreadState *thr, uptr pc, int fd) {
  266. DPrintf("#%d: FdInotifyCreate(%d)\n", thr->tid, fd);
  267. if (bogusfd(fd))
  268. return;
  269. init(thr, pc, fd, 0);
  270. }
  271. void FdPollCreate(ThreadState *thr, uptr pc, int fd) {
  272. DPrintf("#%d: FdPollCreate(%d)\n", thr->tid, fd);
  273. if (bogusfd(fd))
  274. return;
  275. init(thr, pc, fd, allocsync(thr, pc));
  276. }
  277. void FdPollAdd(ThreadState *thr, uptr pc, int epfd, int fd) {
  278. DPrintf("#%d: FdPollAdd(%d, %d)\n", thr->tid, epfd, fd);
  279. if (bogusfd(epfd) || bogusfd(fd))
  280. return;
  281. FdDesc *d = fddesc(thr, pc, fd);
  282. // Associate fd with epoll fd only once.
  283. // While an fd can be associated with multiple epolls at the same time,
  284. // or with different epolls during different phases of lifetime,
  285. // synchronization semantics (and examples) of this are unclear.
  286. // So we don't support this for now.
  287. // If we change the association, it will also create lifetime management
  288. // problem for FdRelease which accesses the aux_sync.
  289. if (atomic_load(&d->aux_sync, memory_order_relaxed))
  290. return;
  291. FdDesc *epd = fddesc(thr, pc, epfd);
  292. FdSync *s = epd->sync;
  293. if (!s)
  294. return;
  295. uptr cmp = 0;
  296. if (atomic_compare_exchange_strong(
  297. &d->aux_sync, &cmp, reinterpret_cast<uptr>(s), memory_order_release))
  298. ref(s);
  299. }
  300. void FdSocketCreate(ThreadState *thr, uptr pc, int fd) {
  301. DPrintf("#%d: FdSocketCreate(%d)\n", thr->tid, fd);
  302. if (bogusfd(fd))
  303. return;
  304. // It can be a UDP socket.
  305. init(thr, pc, fd, &fdctx.socksync);
  306. }
  307. void FdSocketAccept(ThreadState *thr, uptr pc, int fd, int newfd) {
  308. DPrintf("#%d: FdSocketAccept(%d, %d)\n", thr->tid, fd, newfd);
  309. if (bogusfd(fd))
  310. return;
  311. // Synchronize connect->accept.
  312. Acquire(thr, pc, (uptr)&fdctx.connectsync);
  313. init(thr, pc, newfd, &fdctx.socksync);
  314. }
  315. void FdSocketConnecting(ThreadState *thr, uptr pc, int fd) {
  316. DPrintf("#%d: FdSocketConnecting(%d)\n", thr->tid, fd);
  317. if (bogusfd(fd))
  318. return;
  319. // Synchronize connect->accept.
  320. Release(thr, pc, (uptr)&fdctx.connectsync);
  321. }
  322. void FdSocketConnect(ThreadState *thr, uptr pc, int fd) {
  323. DPrintf("#%d: FdSocketConnect(%d)\n", thr->tid, fd);
  324. if (bogusfd(fd))
  325. return;
  326. init(thr, pc, fd, &fdctx.socksync);
  327. }
  328. uptr File2addr(const char *path) {
  329. (void)path;
  330. static u64 addr;
  331. return (uptr)&addr;
  332. }
  333. uptr Dir2addr(const char *path) {
  334. (void)path;
  335. static u64 addr;
  336. return (uptr)&addr;
  337. }
  338. } // namespace __tsan