tsan_fd.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. Tid creation_tid;
  26. StackID creation_stack;
  27. };
  28. struct FdContext {
  29. atomic_uintptr_t tab[kTableSizeL1];
  30. // Addresses used for synchronization.
  31. FdSync globsync;
  32. FdSync filesync;
  33. FdSync socksync;
  34. u64 connectsync;
  35. };
  36. static FdContext fdctx;
  37. static bool bogusfd(int fd) {
  38. // Apparently a bogus fd value.
  39. return fd < 0 || fd >= kTableSize;
  40. }
  41. static FdSync *allocsync(ThreadState *thr, uptr pc) {
  42. FdSync *s = (FdSync*)user_alloc_internal(thr, pc, sizeof(FdSync),
  43. kDefaultAlignment, false);
  44. atomic_store(&s->rc, 1, memory_order_relaxed);
  45. return s;
  46. }
  47. static FdSync *ref(FdSync *s) {
  48. if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1)
  49. atomic_fetch_add(&s->rc, 1, memory_order_relaxed);
  50. return s;
  51. }
  52. static void unref(ThreadState *thr, uptr pc, FdSync *s) {
  53. if (s && atomic_load(&s->rc, memory_order_relaxed) != (u64)-1) {
  54. if (atomic_fetch_sub(&s->rc, 1, memory_order_acq_rel) == 1) {
  55. CHECK_NE(s, &fdctx.globsync);
  56. CHECK_NE(s, &fdctx.filesync);
  57. CHECK_NE(s, &fdctx.socksync);
  58. user_free(thr, pc, s, false);
  59. }
  60. }
  61. }
  62. static FdDesc *fddesc(ThreadState *thr, uptr pc, int fd) {
  63. CHECK_GE(fd, 0);
  64. CHECK_LT(fd, kTableSize);
  65. atomic_uintptr_t *pl1 = &fdctx.tab[fd / kTableSizeL2];
  66. uptr l1 = atomic_load(pl1, memory_order_consume);
  67. if (l1 == 0) {
  68. uptr size = kTableSizeL2 * sizeof(FdDesc);
  69. // We need this to reside in user memory to properly catch races on it.
  70. void *p = user_alloc_internal(thr, pc, size, kDefaultAlignment, false);
  71. internal_memset(p, 0, size);
  72. MemoryResetRange(thr, (uptr)&fddesc, (uptr)p, size);
  73. if (atomic_compare_exchange_strong(pl1, &l1, (uptr)p, memory_order_acq_rel))
  74. l1 = (uptr)p;
  75. else
  76. user_free(thr, pc, p, false);
  77. }
  78. FdDesc *fds = reinterpret_cast<FdDesc *>(l1);
  79. return &fds[fd % kTableSizeL2];
  80. }
  81. // pd must be already ref'ed.
  82. static void init(ThreadState *thr, uptr pc, int fd, FdSync *s,
  83. bool write = true) {
  84. FdDesc *d = fddesc(thr, pc, fd);
  85. // As a matter of fact, we don't intercept all close calls.
  86. // See e.g. libc __res_iclose().
  87. if (d->sync) {
  88. unref(thr, pc, d->sync);
  89. d->sync = 0;
  90. }
  91. if (flags()->io_sync == 0) {
  92. unref(thr, pc, s);
  93. } else if (flags()->io_sync == 1) {
  94. d->sync = s;
  95. } else if (flags()->io_sync == 2) {
  96. unref(thr, pc, s);
  97. d->sync = &fdctx.globsync;
  98. }
  99. d->creation_tid = thr->tid;
  100. d->creation_stack = CurrentStackId(thr, pc);
  101. if (write) {
  102. // To catch races between fd usage and open.
  103. MemoryRangeImitateWrite(thr, pc, (uptr)d, 8);
  104. } else {
  105. // See the dup-related comment in FdClose.
  106. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  107. }
  108. }
  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);
  113. }
  114. void FdOnFork(ThreadState *thr, uptr pc) {
  115. // On fork() we need to reset all fd's, because the child is going
  116. // close all them, and that will cause races between previous read/write
  117. // and the close.
  118. for (int l1 = 0; l1 < kTableSizeL1; l1++) {
  119. FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
  120. if (tab == 0)
  121. break;
  122. for (int l2 = 0; l2 < kTableSizeL2; l2++) {
  123. FdDesc *d = &tab[l2];
  124. MemoryResetRange(thr, pc, (uptr)d, 8);
  125. }
  126. }
  127. }
  128. bool FdLocation(uptr addr, int *fd, Tid *tid, StackID *stack) {
  129. for (int l1 = 0; l1 < kTableSizeL1; l1++) {
  130. FdDesc *tab = (FdDesc*)atomic_load(&fdctx.tab[l1], memory_order_relaxed);
  131. if (tab == 0)
  132. break;
  133. if (addr >= (uptr)tab && addr < (uptr)(tab + kTableSizeL2)) {
  134. int l2 = (addr - (uptr)tab) / sizeof(FdDesc);
  135. FdDesc *d = &tab[l2];
  136. *fd = l1 * kTableSizeL1 + l2;
  137. *tid = d->creation_tid;
  138. *stack = d->creation_stack;
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. void FdAcquire(ThreadState *thr, uptr pc, int fd) {
  145. if (bogusfd(fd))
  146. return;
  147. FdDesc *d = fddesc(thr, pc, fd);
  148. FdSync *s = d->sync;
  149. DPrintf("#%d: FdAcquire(%d) -> %p\n", thr->tid, fd, s);
  150. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  151. if (s)
  152. Acquire(thr, pc, (uptr)s);
  153. }
  154. void FdRelease(ThreadState *thr, uptr pc, int fd) {
  155. if (bogusfd(fd))
  156. return;
  157. FdDesc *d = fddesc(thr, pc, fd);
  158. FdSync *s = d->sync;
  159. DPrintf("#%d: FdRelease(%d) -> %p\n", thr->tid, fd, s);
  160. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  161. if (s)
  162. Release(thr, pc, (uptr)s);
  163. }
  164. void FdAccess(ThreadState *thr, uptr pc, int fd) {
  165. DPrintf("#%d: FdAccess(%d)\n", thr->tid, fd);
  166. if (bogusfd(fd))
  167. return;
  168. FdDesc *d = fddesc(thr, pc, fd);
  169. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  170. }
  171. void FdClose(ThreadState *thr, uptr pc, int fd, bool write) {
  172. DPrintf("#%d: FdClose(%d)\n", thr->tid, fd);
  173. if (bogusfd(fd))
  174. return;
  175. FdDesc *d = fddesc(thr, pc, fd);
  176. if (!MustIgnoreInterceptor(thr)) {
  177. if (write) {
  178. // To catch races between fd usage and close.
  179. MemoryAccess(thr, pc, (uptr)d, 8, kAccessWrite);
  180. } else {
  181. // This path is used only by dup2/dup3 calls.
  182. // We do read instead of write because there is a number of legitimate
  183. // cases where write would lead to false positives:
  184. // 1. Some software dups a closed pipe in place of a socket before closing
  185. // the socket (to prevent races actually).
  186. // 2. Some daemons dup /dev/null in place of stdin/stdout.
  187. // On the other hand we have not seen cases when write here catches real
  188. // bugs.
  189. MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead);
  190. }
  191. }
  192. // We need to clear it, because if we do not intercept any call out there
  193. // that creates fd, we will hit false postives.
  194. MemoryResetRange(thr, pc, (uptr)d, 8);
  195. unref(thr, pc, d->sync);
  196. d->sync = 0;
  197. d->creation_tid = kInvalidTid;
  198. d->creation_stack = kInvalidStackID;
  199. }
  200. void FdFileCreate(ThreadState *thr, uptr pc, int fd) {
  201. DPrintf("#%d: FdFileCreate(%d)\n", thr->tid, fd);
  202. if (bogusfd(fd))
  203. return;
  204. init(thr, pc, fd, &fdctx.filesync);
  205. }
  206. void FdDup(ThreadState *thr, uptr pc, int oldfd, int newfd, bool write) {
  207. DPrintf("#%d: FdDup(%d, %d)\n", thr->tid, oldfd, newfd);
  208. if (bogusfd(oldfd) || bogusfd(newfd))
  209. return;
  210. // Ignore the case when user dups not yet connected socket.
  211. FdDesc *od = fddesc(thr, pc, oldfd);
  212. MemoryAccess(thr, pc, (uptr)od, 8, kAccessRead);
  213. FdClose(thr, pc, newfd, write);
  214. init(thr, pc, newfd, ref(od->sync), write);
  215. }
  216. void FdPipeCreate(ThreadState *thr, uptr pc, int rfd, int wfd) {
  217. DPrintf("#%d: FdCreatePipe(%d, %d)\n", thr->tid, rfd, wfd);
  218. FdSync *s = allocsync(thr, pc);
  219. init(thr, pc, rfd, ref(s));
  220. init(thr, pc, wfd, ref(s));
  221. unref(thr, pc, s);
  222. }
  223. void FdEventCreate(ThreadState *thr, uptr pc, int fd) {
  224. DPrintf("#%d: FdEventCreate(%d)\n", thr->tid, fd);
  225. if (bogusfd(fd))
  226. return;
  227. init(thr, pc, fd, allocsync(thr, pc));
  228. }
  229. void FdSignalCreate(ThreadState *thr, uptr pc, int fd) {
  230. DPrintf("#%d: FdSignalCreate(%d)\n", thr->tid, fd);
  231. if (bogusfd(fd))
  232. return;
  233. init(thr, pc, fd, 0);
  234. }
  235. void FdInotifyCreate(ThreadState *thr, uptr pc, int fd) {
  236. DPrintf("#%d: FdInotifyCreate(%d)\n", thr->tid, fd);
  237. if (bogusfd(fd))
  238. return;
  239. init(thr, pc, fd, 0);
  240. }
  241. void FdPollCreate(ThreadState *thr, uptr pc, int fd) {
  242. DPrintf("#%d: FdPollCreate(%d)\n", thr->tid, fd);
  243. if (bogusfd(fd))
  244. return;
  245. init(thr, pc, fd, allocsync(thr, pc));
  246. }
  247. void FdSocketCreate(ThreadState *thr, uptr pc, int fd) {
  248. DPrintf("#%d: FdSocketCreate(%d)\n", thr->tid, fd);
  249. if (bogusfd(fd))
  250. return;
  251. // It can be a UDP socket.
  252. init(thr, pc, fd, &fdctx.socksync);
  253. }
  254. void FdSocketAccept(ThreadState *thr, uptr pc, int fd, int newfd) {
  255. DPrintf("#%d: FdSocketAccept(%d, %d)\n", thr->tid, fd, newfd);
  256. if (bogusfd(fd))
  257. return;
  258. // Synchronize connect->accept.
  259. Acquire(thr, pc, (uptr)&fdctx.connectsync);
  260. init(thr, pc, newfd, &fdctx.socksync);
  261. }
  262. void FdSocketConnecting(ThreadState *thr, uptr pc, int fd) {
  263. DPrintf("#%d: FdSocketConnecting(%d)\n", thr->tid, fd);
  264. if (bogusfd(fd))
  265. return;
  266. // Synchronize connect->accept.
  267. Release(thr, pc, (uptr)&fdctx.connectsync);
  268. }
  269. void FdSocketConnect(ThreadState *thr, uptr pc, int fd) {
  270. DPrintf("#%d: FdSocketConnect(%d)\n", thr->tid, fd);
  271. if (bogusfd(fd))
  272. return;
  273. init(thr, pc, fd, &fdctx.socksync);
  274. }
  275. uptr File2addr(const char *path) {
  276. (void)path;
  277. static u64 addr;
  278. return (uptr)&addr;
  279. }
  280. uptr Dir2addr(const char *path) {
  281. (void)path;
  282. static u64 addr;
  283. return (uptr)&addr;
  284. }
  285. } // namespace __tsan