tsan_interceptors.cc 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  1. //===-- tsan_interceptors.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. // FIXME: move as many interceptors as possible into
  11. // sanitizer_common/sanitizer_common_interceptors.inc
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_atomic.h"
  14. #include "sanitizer_common/sanitizer_libc.h"
  15. #include "sanitizer_common/sanitizer_linux.h"
  16. #include "sanitizer_common/sanitizer_platform_limits_posix.h"
  17. #include "sanitizer_common/sanitizer_placement_new.h"
  18. #include "sanitizer_common/sanitizer_stacktrace.h"
  19. #include "interception/interception.h"
  20. #include "tsan_interface.h"
  21. #include "tsan_platform.h"
  22. #include "tsan_suppressions.h"
  23. #include "tsan_rtl.h"
  24. #include "tsan_mman.h"
  25. #include "tsan_fd.h"
  26. using namespace __tsan; // NOLINT
  27. #if SANITIZER_FREEBSD
  28. #define __errno_location __error
  29. #define __libc_malloc __malloc
  30. #define __libc_realloc __realloc
  31. #define __libc_calloc __calloc
  32. #define __libc_free __free
  33. #define stdout __stdoutp
  34. #define stderr __stderrp
  35. #endif
  36. const int kSigCount = 65;
  37. struct my_siginfo_t {
  38. // The size is determined by looking at sizeof of real siginfo_t on linux.
  39. u64 opaque[128 / sizeof(u64)];
  40. };
  41. struct ucontext_t {
  42. // The size is determined by looking at sizeof of real ucontext_t on linux.
  43. u64 opaque[936 / sizeof(u64) + 1];
  44. };
  45. extern "C" int pthread_attr_init(void *attr);
  46. extern "C" int pthread_attr_destroy(void *attr);
  47. DECLARE_REAL(int, pthread_attr_getdetachstate, void *, void *)
  48. extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize);
  49. extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v));
  50. extern "C" int pthread_setspecific(unsigned key, const void *v);
  51. DECLARE_REAL(int, pthread_mutexattr_gettype, void *, void *)
  52. extern "C" int pthread_yield();
  53. extern "C" int pthread_sigmask(int how, const __sanitizer_sigset_t *set,
  54. __sanitizer_sigset_t *oldset);
  55. // REAL(sigfillset) defined in common interceptors.
  56. DECLARE_REAL(int, sigfillset, __sanitizer_sigset_t *set)
  57. DECLARE_REAL(int, fflush, __sanitizer_FILE *fp)
  58. extern "C" void *pthread_self();
  59. extern "C" void _exit(int status);
  60. extern "C" int *__errno_location();
  61. extern "C" int fileno_unlocked(void *stream);
  62. extern "C" void *__libc_malloc(uptr size);
  63. extern "C" void *__libc_calloc(uptr size, uptr n);
  64. extern "C" void *__libc_realloc(void *ptr, uptr size);
  65. extern "C" void __libc_free(void *ptr);
  66. #if !SANITIZER_FREEBSD
  67. extern "C" int mallopt(int param, int value);
  68. #endif
  69. extern __sanitizer_FILE *stdout, *stderr;
  70. const int PTHREAD_MUTEX_RECURSIVE = 1;
  71. const int PTHREAD_MUTEX_RECURSIVE_NP = 1;
  72. const int EINVAL = 22;
  73. const int EBUSY = 16;
  74. const int EOWNERDEAD = 130;
  75. const int EPOLL_CTL_ADD = 1;
  76. const int SIGILL = 4;
  77. const int SIGABRT = 6;
  78. const int SIGFPE = 8;
  79. const int SIGSEGV = 11;
  80. const int SIGPIPE = 13;
  81. const int SIGTERM = 15;
  82. const int SIGBUS = 7;
  83. const int SIGSYS = 31;
  84. void *const MAP_FAILED = (void*)-1;
  85. const int PTHREAD_BARRIER_SERIAL_THREAD = -1;
  86. const int MAP_FIXED = 0x10;
  87. typedef long long_t; // NOLINT
  88. // From /usr/include/unistd.h
  89. # define F_ULOCK 0 /* Unlock a previously locked region. */
  90. # define F_LOCK 1 /* Lock a region for exclusive use. */
  91. # define F_TLOCK 2 /* Test and lock a region for exclusive use. */
  92. # define F_TEST 3 /* Test a region for other processes locks. */
  93. typedef void (*sighandler_t)(int sig);
  94. #define errno (*__errno_location())
  95. struct sigaction_t {
  96. union {
  97. sighandler_t sa_handler;
  98. void (*sa_sigaction)(int sig, my_siginfo_t *siginfo, void *uctx);
  99. };
  100. #if SANITIZER_FREEBSD
  101. int sa_flags;
  102. __sanitizer_sigset_t sa_mask;
  103. #else
  104. __sanitizer_sigset_t sa_mask;
  105. int sa_flags;
  106. void (*sa_restorer)();
  107. #endif
  108. };
  109. const sighandler_t SIG_DFL = (sighandler_t)0;
  110. const sighandler_t SIG_IGN = (sighandler_t)1;
  111. const sighandler_t SIG_ERR = (sighandler_t)-1;
  112. const int SA_SIGINFO = 4;
  113. const int SIG_SETMASK = 2;
  114. namespace std {
  115. struct nothrow_t {};
  116. } // namespace std
  117. static sigaction_t sigactions[kSigCount];
  118. namespace __tsan {
  119. struct SignalDesc {
  120. bool armed;
  121. bool sigaction;
  122. my_siginfo_t siginfo;
  123. ucontext_t ctx;
  124. };
  125. struct SignalContext {
  126. int int_signal_send;
  127. atomic_uintptr_t in_blocking_func;
  128. atomic_uintptr_t have_pending_signals;
  129. SignalDesc pending_signals[kSigCount];
  130. };
  131. // The object is 64-byte aligned, because we want hot data to be located in
  132. // a single cache line if possible (it's accessed in every interceptor).
  133. static ALIGNED(64) char libignore_placeholder[sizeof(LibIgnore)];
  134. static LibIgnore *libignore() {
  135. return reinterpret_cast<LibIgnore*>(&libignore_placeholder[0]);
  136. }
  137. void InitializeLibIgnore() {
  138. libignore()->Init(*SuppressionContext::Get());
  139. libignore()->OnLibraryLoaded(0);
  140. }
  141. } // namespace __tsan
  142. static SignalContext *SigCtx(ThreadState *thr) {
  143. SignalContext *ctx = (SignalContext*)thr->signal_ctx;
  144. if (ctx == 0 && !thr->is_dead) {
  145. ctx = (SignalContext*)MmapOrDie(sizeof(*ctx), "SignalContext");
  146. MemoryResetRange(thr, (uptr)&SigCtx, (uptr)ctx, sizeof(*ctx));
  147. thr->signal_ctx = ctx;
  148. }
  149. return ctx;
  150. }
  151. static unsigned g_thread_finalize_key;
  152. class ScopedInterceptor {
  153. public:
  154. ScopedInterceptor(ThreadState *thr, const char *fname, uptr pc);
  155. ~ScopedInterceptor();
  156. private:
  157. ThreadState *const thr_;
  158. const uptr pc_;
  159. bool in_ignored_lib_;
  160. };
  161. ScopedInterceptor::ScopedInterceptor(ThreadState *thr, const char *fname,
  162. uptr pc)
  163. : thr_(thr)
  164. , pc_(pc)
  165. , in_ignored_lib_(false) {
  166. if (!thr_->ignore_interceptors) {
  167. Initialize(thr);
  168. FuncEntry(thr, pc);
  169. }
  170. DPrintf("#%d: intercept %s()\n", thr_->tid, fname);
  171. if (!thr_->in_ignored_lib && libignore()->IsIgnored(pc)) {
  172. in_ignored_lib_ = true;
  173. thr_->in_ignored_lib = true;
  174. ThreadIgnoreBegin(thr_, pc_);
  175. }
  176. }
  177. ScopedInterceptor::~ScopedInterceptor() {
  178. if (in_ignored_lib_) {
  179. thr_->in_ignored_lib = false;
  180. ThreadIgnoreEnd(thr_, pc_);
  181. }
  182. if (!thr_->ignore_interceptors) {
  183. ProcessPendingSignals(thr_);
  184. FuncExit(thr_);
  185. CheckNoLocks(thr_);
  186. }
  187. }
  188. #define SCOPED_INTERCEPTOR_RAW(func, ...) \
  189. ThreadState *thr = cur_thread(); \
  190. const uptr caller_pc = GET_CALLER_PC(); \
  191. ScopedInterceptor si(thr, #func, caller_pc); \
  192. const uptr pc = StackTrace::GetCurrentPc(); \
  193. (void)pc; \
  194. /**/
  195. #define SCOPED_TSAN_INTERCEPTOR(func, ...) \
  196. SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \
  197. if (REAL(func) == 0) { \
  198. Report("FATAL: ThreadSanitizer: failed to intercept %s\n", #func); \
  199. Die(); \
  200. } \
  201. if (thr->ignore_interceptors || thr->in_ignored_lib) \
  202. return REAL(func)(__VA_ARGS__); \
  203. /**/
  204. #define TSAN_INTERCEPTOR(ret, func, ...) INTERCEPTOR(ret, func, __VA_ARGS__)
  205. #define TSAN_INTERCEPT(func) INTERCEPT_FUNCTION(func)
  206. #if SANITIZER_FREEBSD
  207. # define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION(func)
  208. #else
  209. # define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION_VER(func, ver)
  210. #endif
  211. #define BLOCK_REAL(name) (BlockingCall(thr), REAL(name))
  212. struct BlockingCall {
  213. explicit BlockingCall(ThreadState *thr)
  214. : thr(thr)
  215. , ctx(SigCtx(thr)) {
  216. for (;;) {
  217. atomic_store(&ctx->in_blocking_func, 1, memory_order_relaxed);
  218. if (atomic_load(&ctx->have_pending_signals, memory_order_relaxed) == 0)
  219. break;
  220. atomic_store(&ctx->in_blocking_func, 0, memory_order_relaxed);
  221. ProcessPendingSignals(thr);
  222. }
  223. // When we are in a "blocking call", we process signals asynchronously
  224. // (right when they arrive). In this context we do not expect to be
  225. // executing any user/runtime code. The known interceptor sequence when
  226. // this is not true is: pthread_join -> munmap(stack). It's fine
  227. // to ignore munmap in this case -- we handle stack shadow separately.
  228. thr->ignore_interceptors++;
  229. }
  230. ~BlockingCall() {
  231. thr->ignore_interceptors--;
  232. atomic_store(&ctx->in_blocking_func, 0, memory_order_relaxed);
  233. }
  234. ThreadState *thr;
  235. SignalContext *ctx;
  236. };
  237. TSAN_INTERCEPTOR(unsigned, sleep, unsigned sec) {
  238. SCOPED_TSAN_INTERCEPTOR(sleep, sec);
  239. unsigned res = BLOCK_REAL(sleep)(sec);
  240. AfterSleep(thr, pc);
  241. return res;
  242. }
  243. TSAN_INTERCEPTOR(int, usleep, long_t usec) {
  244. SCOPED_TSAN_INTERCEPTOR(usleep, usec);
  245. int res = BLOCK_REAL(usleep)(usec);
  246. AfterSleep(thr, pc);
  247. return res;
  248. }
  249. TSAN_INTERCEPTOR(int, nanosleep, void *req, void *rem) {
  250. SCOPED_TSAN_INTERCEPTOR(nanosleep, req, rem);
  251. int res = BLOCK_REAL(nanosleep)(req, rem);
  252. AfterSleep(thr, pc);
  253. return res;
  254. }
  255. // The sole reason tsan wraps atexit callbacks is to establish synchronization
  256. // between callback setup and callback execution.
  257. struct AtExitCtx {
  258. void (*f)();
  259. void *arg;
  260. };
  261. static void at_exit_wrapper(void *arg) {
  262. ThreadState *thr = cur_thread();
  263. uptr pc = 0;
  264. Acquire(thr, pc, (uptr)arg);
  265. AtExitCtx *ctx = (AtExitCtx*)arg;
  266. ((void(*)(void *arg))ctx->f)(ctx->arg);
  267. __libc_free(ctx);
  268. }
  269. static int setup_at_exit_wrapper(ThreadState *thr, uptr pc, void(*f)(),
  270. void *arg, void *dso);
  271. TSAN_INTERCEPTOR(int, atexit, void (*f)()) {
  272. if (cur_thread()->in_symbolizer)
  273. return 0;
  274. // We want to setup the atexit callback even if we are in ignored lib
  275. // or after fork.
  276. SCOPED_INTERCEPTOR_RAW(atexit, f);
  277. return setup_at_exit_wrapper(thr, pc, (void(*)())f, 0, 0);
  278. }
  279. TSAN_INTERCEPTOR(int, __cxa_atexit, void (*f)(void *a), void *arg, void *dso) {
  280. if (cur_thread()->in_symbolizer)
  281. return 0;
  282. SCOPED_TSAN_INTERCEPTOR(__cxa_atexit, f, arg, dso);
  283. return setup_at_exit_wrapper(thr, pc, (void(*)())f, arg, dso);
  284. }
  285. static int setup_at_exit_wrapper(ThreadState *thr, uptr pc, void(*f)(),
  286. void *arg, void *dso) {
  287. AtExitCtx *ctx = (AtExitCtx*)__libc_malloc(sizeof(AtExitCtx));
  288. ctx->f = f;
  289. ctx->arg = arg;
  290. Release(thr, pc, (uptr)ctx);
  291. // Memory allocation in __cxa_atexit will race with free during exit,
  292. // because we do not see synchronization around atexit callback list.
  293. ThreadIgnoreBegin(thr, pc);
  294. int res = REAL(__cxa_atexit)(at_exit_wrapper, ctx, dso);
  295. ThreadIgnoreEnd(thr, pc);
  296. return res;
  297. }
  298. static void on_exit_wrapper(int status, void *arg) {
  299. ThreadState *thr = cur_thread();
  300. uptr pc = 0;
  301. Acquire(thr, pc, (uptr)arg);
  302. AtExitCtx *ctx = (AtExitCtx*)arg;
  303. ((void(*)(int status, void *arg))ctx->f)(status, ctx->arg);
  304. __libc_free(ctx);
  305. }
  306. TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) {
  307. if (cur_thread()->in_symbolizer)
  308. return 0;
  309. SCOPED_TSAN_INTERCEPTOR(on_exit, f, arg);
  310. AtExitCtx *ctx = (AtExitCtx*)__libc_malloc(sizeof(AtExitCtx));
  311. ctx->f = (void(*)())f;
  312. ctx->arg = arg;
  313. Release(thr, pc, (uptr)ctx);
  314. // Memory allocation in __cxa_atexit will race with free during exit,
  315. // because we do not see synchronization around atexit callback list.
  316. ThreadIgnoreBegin(thr, pc);
  317. int res = REAL(on_exit)(on_exit_wrapper, ctx);
  318. ThreadIgnoreEnd(thr, pc);
  319. return res;
  320. }
  321. // Cleanup old bufs.
  322. static void JmpBufGarbageCollect(ThreadState *thr, uptr sp) {
  323. for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) {
  324. JmpBuf *buf = &thr->jmp_bufs[i];
  325. if (buf->sp <= sp) {
  326. uptr sz = thr->jmp_bufs.Size();
  327. thr->jmp_bufs[i] = thr->jmp_bufs[sz - 1];
  328. thr->jmp_bufs.PopBack();
  329. i--;
  330. }
  331. }
  332. }
  333. static void SetJmp(ThreadState *thr, uptr sp, uptr mangled_sp) {
  334. if (thr->shadow_stack_pos == 0) // called from libc guts during bootstrap
  335. return;
  336. // Cleanup old bufs.
  337. JmpBufGarbageCollect(thr, sp);
  338. // Remember the buf.
  339. JmpBuf *buf = thr->jmp_bufs.PushBack();
  340. buf->sp = sp;
  341. buf->mangled_sp = mangled_sp;
  342. buf->shadow_stack_pos = thr->shadow_stack_pos;
  343. SignalContext *sctx = SigCtx(thr);
  344. buf->int_signal_send = sctx ? sctx->int_signal_send : 0;
  345. buf->in_blocking_func = sctx ?
  346. atomic_load(&sctx->in_blocking_func, memory_order_relaxed) :
  347. false;
  348. buf->in_signal_handler = atomic_load(&thr->in_signal_handler,
  349. memory_order_relaxed);
  350. }
  351. static void LongJmp(ThreadState *thr, uptr *env) {
  352. #if SANITIZER_FREEBSD
  353. uptr mangled_sp = env[2];
  354. #else
  355. uptr mangled_sp = env[6];
  356. #endif // SANITIZER_FREEBSD
  357. // Find the saved buf by mangled_sp.
  358. for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) {
  359. JmpBuf *buf = &thr->jmp_bufs[i];
  360. if (buf->mangled_sp == mangled_sp) {
  361. CHECK_GE(thr->shadow_stack_pos, buf->shadow_stack_pos);
  362. // Unwind the stack.
  363. while (thr->shadow_stack_pos > buf->shadow_stack_pos)
  364. FuncExit(thr);
  365. SignalContext *sctx = SigCtx(thr);
  366. if (sctx) {
  367. sctx->int_signal_send = buf->int_signal_send;
  368. atomic_store(&sctx->in_blocking_func, buf->in_blocking_func,
  369. memory_order_relaxed);
  370. }
  371. atomic_store(&thr->in_signal_handler, buf->in_signal_handler,
  372. memory_order_relaxed);
  373. JmpBufGarbageCollect(thr, buf->sp - 1); // do not collect buf->sp
  374. return;
  375. }
  376. }
  377. Printf("ThreadSanitizer: can't find longjmp buf\n");
  378. CHECK(0);
  379. }
  380. // FIXME: put everything below into a common extern "C" block?
  381. extern "C" void __tsan_setjmp(uptr sp, uptr mangled_sp) {
  382. SetJmp(cur_thread(), sp, mangled_sp);
  383. }
  384. // Not called. Merely to satisfy TSAN_INTERCEPT().
  385. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  386. int __interceptor_setjmp(void *env);
  387. extern "C" int __interceptor_setjmp(void *env) {
  388. CHECK(0);
  389. return 0;
  390. }
  391. // FIXME: any reason to have a separate declaration?
  392. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  393. int __interceptor__setjmp(void *env);
  394. extern "C" int __interceptor__setjmp(void *env) {
  395. CHECK(0);
  396. return 0;
  397. }
  398. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  399. int __interceptor_sigsetjmp(void *env);
  400. extern "C" int __interceptor_sigsetjmp(void *env) {
  401. CHECK(0);
  402. return 0;
  403. }
  404. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  405. int __interceptor___sigsetjmp(void *env);
  406. extern "C" int __interceptor___sigsetjmp(void *env) {
  407. CHECK(0);
  408. return 0;
  409. }
  410. extern "C" int setjmp(void *env);
  411. extern "C" int _setjmp(void *env);
  412. extern "C" int sigsetjmp(void *env);
  413. extern "C" int __sigsetjmp(void *env);
  414. DEFINE_REAL(int, setjmp, void *env)
  415. DEFINE_REAL(int, _setjmp, void *env)
  416. DEFINE_REAL(int, sigsetjmp, void *env)
  417. DEFINE_REAL(int, __sigsetjmp, void *env)
  418. TSAN_INTERCEPTOR(void, longjmp, uptr *env, int val) {
  419. {
  420. SCOPED_TSAN_INTERCEPTOR(longjmp, env, val);
  421. }
  422. LongJmp(cur_thread(), env);
  423. REAL(longjmp)(env, val);
  424. }
  425. TSAN_INTERCEPTOR(void, siglongjmp, uptr *env, int val) {
  426. {
  427. SCOPED_TSAN_INTERCEPTOR(siglongjmp, env, val);
  428. }
  429. LongJmp(cur_thread(), env);
  430. REAL(siglongjmp)(env, val);
  431. }
  432. TSAN_INTERCEPTOR(void*, malloc, uptr size) {
  433. if (cur_thread()->in_symbolizer)
  434. return __libc_malloc(size);
  435. void *p = 0;
  436. {
  437. SCOPED_INTERCEPTOR_RAW(malloc, size);
  438. p = user_alloc(thr, pc, size);
  439. }
  440. invoke_malloc_hook(p, size);
  441. return p;
  442. }
  443. TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
  444. SCOPED_TSAN_INTERCEPTOR(__libc_memalign, align, sz);
  445. return user_alloc(thr, pc, sz, align);
  446. }
  447. TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
  448. if (cur_thread()->in_symbolizer)
  449. return __libc_calloc(size, n);
  450. if (__sanitizer::CallocShouldReturnNullDueToOverflow(size, n))
  451. return AllocatorReturnNull();
  452. void *p = 0;
  453. {
  454. SCOPED_INTERCEPTOR_RAW(calloc, size, n);
  455. p = user_alloc(thr, pc, n * size);
  456. if (p)
  457. internal_memset(p, 0, n * size);
  458. }
  459. invoke_malloc_hook(p, n * size);
  460. return p;
  461. }
  462. TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
  463. if (cur_thread()->in_symbolizer)
  464. return __libc_realloc(p, size);
  465. if (p)
  466. invoke_free_hook(p);
  467. {
  468. SCOPED_INTERCEPTOR_RAW(realloc, p, size);
  469. p = user_realloc(thr, pc, p, size);
  470. }
  471. invoke_malloc_hook(p, size);
  472. return p;
  473. }
  474. TSAN_INTERCEPTOR(void, free, void *p) {
  475. if (p == 0)
  476. return;
  477. if (cur_thread()->in_symbolizer)
  478. return __libc_free(p);
  479. invoke_free_hook(p);
  480. SCOPED_INTERCEPTOR_RAW(free, p);
  481. user_free(thr, pc, p);
  482. }
  483. TSAN_INTERCEPTOR(void, cfree, void *p) {
  484. if (p == 0)
  485. return;
  486. if (cur_thread()->in_symbolizer)
  487. return __libc_free(p);
  488. invoke_free_hook(p);
  489. SCOPED_INTERCEPTOR_RAW(cfree, p);
  490. user_free(thr, pc, p);
  491. }
  492. TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) {
  493. SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p);
  494. return user_alloc_usable_size(p);
  495. }
  496. #define OPERATOR_NEW_BODY(mangled_name) \
  497. if (cur_thread()->in_symbolizer) \
  498. return __libc_malloc(size); \
  499. void *p = 0; \
  500. { \
  501. SCOPED_INTERCEPTOR_RAW(mangled_name, size); \
  502. p = user_alloc(thr, pc, size); \
  503. } \
  504. invoke_malloc_hook(p, size); \
  505. return p;
  506. SANITIZER_INTERFACE_ATTRIBUTE
  507. void *operator new(__sanitizer::uptr size);
  508. void *operator new(__sanitizer::uptr size) {
  509. OPERATOR_NEW_BODY(_Znwm);
  510. }
  511. SANITIZER_INTERFACE_ATTRIBUTE
  512. void *operator new[](__sanitizer::uptr size);
  513. void *operator new[](__sanitizer::uptr size) {
  514. OPERATOR_NEW_BODY(_Znam);
  515. }
  516. SANITIZER_INTERFACE_ATTRIBUTE
  517. void *operator new(__sanitizer::uptr size, std::nothrow_t const&);
  518. void *operator new(__sanitizer::uptr size, std::nothrow_t const&) {
  519. OPERATOR_NEW_BODY(_ZnwmRKSt9nothrow_t);
  520. }
  521. SANITIZER_INTERFACE_ATTRIBUTE
  522. void *operator new[](__sanitizer::uptr size, std::nothrow_t const&);
  523. void *operator new[](__sanitizer::uptr size, std::nothrow_t const&) {
  524. OPERATOR_NEW_BODY(_ZnamRKSt9nothrow_t);
  525. }
  526. #define OPERATOR_DELETE_BODY(mangled_name) \
  527. if (ptr == 0) return; \
  528. if (cur_thread()->in_symbolizer) \
  529. return __libc_free(ptr); \
  530. invoke_free_hook(ptr); \
  531. SCOPED_INTERCEPTOR_RAW(mangled_name, ptr); \
  532. user_free(thr, pc, ptr);
  533. SANITIZER_INTERFACE_ATTRIBUTE
  534. void operator delete(void *ptr) throw();
  535. void operator delete(void *ptr) throw() {
  536. OPERATOR_DELETE_BODY(_ZdlPv);
  537. }
  538. SANITIZER_INTERFACE_ATTRIBUTE
  539. void operator delete[](void *ptr) throw();
  540. void operator delete[](void *ptr) throw() {
  541. OPERATOR_DELETE_BODY(_ZdaPv);
  542. }
  543. SANITIZER_INTERFACE_ATTRIBUTE
  544. void operator delete(void *ptr, std::nothrow_t const&);
  545. void operator delete(void *ptr, std::nothrow_t const&) {
  546. OPERATOR_DELETE_BODY(_ZdlPvRKSt9nothrow_t);
  547. }
  548. SANITIZER_INTERFACE_ATTRIBUTE
  549. void operator delete[](void *ptr, std::nothrow_t const&);
  550. void operator delete[](void *ptr, std::nothrow_t const&) {
  551. OPERATOR_DELETE_BODY(_ZdaPvRKSt9nothrow_t);
  552. }
  553. TSAN_INTERCEPTOR(uptr, strlen, const char *s) {
  554. SCOPED_TSAN_INTERCEPTOR(strlen, s);
  555. uptr len = internal_strlen(s);
  556. MemoryAccessRange(thr, pc, (uptr)s, len + 1, false);
  557. return len;
  558. }
  559. TSAN_INTERCEPTOR(void*, memset, void *dst, int v, uptr size) {
  560. SCOPED_TSAN_INTERCEPTOR(memset, dst, v, size);
  561. MemoryAccessRange(thr, pc, (uptr)dst, size, true);
  562. return internal_memset(dst, v, size);
  563. }
  564. TSAN_INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) {
  565. SCOPED_TSAN_INTERCEPTOR(memcpy, dst, src, size);
  566. MemoryAccessRange(thr, pc, (uptr)dst, size, true);
  567. MemoryAccessRange(thr, pc, (uptr)src, size, false);
  568. return internal_memcpy(dst, src, size);
  569. }
  570. TSAN_INTERCEPTOR(int, memcmp, const void *s1, const void *s2, uptr n) {
  571. SCOPED_TSAN_INTERCEPTOR(memcmp, s1, s2, n);
  572. int res = 0;
  573. uptr len = 0;
  574. for (; len < n; len++) {
  575. if ((res = ((unsigned char*)s1)[len] - ((unsigned char*)s2)[len]))
  576. break;
  577. }
  578. MemoryAccessRange(thr, pc, (uptr)s1, len < n ? len + 1 : n, false);
  579. MemoryAccessRange(thr, pc, (uptr)s2, len < n ? len + 1 : n, false);
  580. return res;
  581. }
  582. TSAN_INTERCEPTOR(void*, memmove, void *dst, void *src, uptr n) {
  583. SCOPED_TSAN_INTERCEPTOR(memmove, dst, src, n);
  584. MemoryAccessRange(thr, pc, (uptr)dst, n, true);
  585. MemoryAccessRange(thr, pc, (uptr)src, n, false);
  586. return REAL(memmove)(dst, src, n);
  587. }
  588. TSAN_INTERCEPTOR(char*, strchr, char *s, int c) {
  589. SCOPED_TSAN_INTERCEPTOR(strchr, s, c);
  590. char *res = REAL(strchr)(s, c);
  591. uptr len = res ? (char*)res - (char*)s + 1 : internal_strlen(s) + 1;
  592. MemoryAccessRange(thr, pc, (uptr)s, len, false);
  593. return res;
  594. }
  595. TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) {
  596. SCOPED_TSAN_INTERCEPTOR(strchrnul, s, c);
  597. char *res = REAL(strchrnul)(s, c);
  598. uptr len = (char*)res - (char*)s + 1;
  599. MemoryAccessRange(thr, pc, (uptr)s, len, false);
  600. return res;
  601. }
  602. TSAN_INTERCEPTOR(char*, strrchr, char *s, int c) {
  603. SCOPED_TSAN_INTERCEPTOR(strrchr, s, c);
  604. MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s) + 1, false);
  605. return REAL(strrchr)(s, c);
  606. }
  607. TSAN_INTERCEPTOR(char*, strcpy, char *dst, const char *src) { // NOLINT
  608. SCOPED_TSAN_INTERCEPTOR(strcpy, dst, src); // NOLINT
  609. uptr srclen = internal_strlen(src);
  610. MemoryAccessRange(thr, pc, (uptr)dst, srclen + 1, true);
  611. MemoryAccessRange(thr, pc, (uptr)src, srclen + 1, false);
  612. return REAL(strcpy)(dst, src); // NOLINT
  613. }
  614. TSAN_INTERCEPTOR(char*, strncpy, char *dst, char *src, uptr n) {
  615. SCOPED_TSAN_INTERCEPTOR(strncpy, dst, src, n);
  616. uptr srclen = internal_strnlen(src, n);
  617. MemoryAccessRange(thr, pc, (uptr)dst, n, true);
  618. MemoryAccessRange(thr, pc, (uptr)src, min(srclen + 1, n), false);
  619. return REAL(strncpy)(dst, src, n);
  620. }
  621. TSAN_INTERCEPTOR(const char*, strstr, const char *s1, const char *s2) {
  622. SCOPED_TSAN_INTERCEPTOR(strstr, s1, s2);
  623. const char *res = REAL(strstr)(s1, s2);
  624. uptr len1 = internal_strlen(s1);
  625. uptr len2 = internal_strlen(s2);
  626. MemoryAccessRange(thr, pc, (uptr)s1, len1 + 1, false);
  627. MemoryAccessRange(thr, pc, (uptr)s2, len2 + 1, false);
  628. return res;
  629. }
  630. TSAN_INTERCEPTOR(char*, strdup, const char *str) {
  631. SCOPED_TSAN_INTERCEPTOR(strdup, str);
  632. // strdup will call malloc, so no instrumentation is required here.
  633. return REAL(strdup)(str);
  634. }
  635. static bool fix_mmap_addr(void **addr, long_t sz, int flags) {
  636. if (*addr) {
  637. if (!IsAppMem((uptr)*addr) || !IsAppMem((uptr)*addr + sz - 1)) {
  638. if (flags & MAP_FIXED) {
  639. errno = EINVAL;
  640. return false;
  641. } else {
  642. *addr = 0;
  643. }
  644. }
  645. }
  646. return true;
  647. }
  648. TSAN_INTERCEPTOR(void*, mmap, void *addr, long_t sz, int prot,
  649. int flags, int fd, unsigned off) {
  650. SCOPED_TSAN_INTERCEPTOR(mmap, addr, sz, prot, flags, fd, off);
  651. if (!fix_mmap_addr(&addr, sz, flags))
  652. return MAP_FAILED;
  653. void *res = REAL(mmap)(addr, sz, prot, flags, fd, off);
  654. if (res != MAP_FAILED) {
  655. if (fd > 0)
  656. FdAccess(thr, pc, fd);
  657. MemoryRangeImitateWrite(thr, pc, (uptr)res, sz);
  658. }
  659. return res;
  660. }
  661. #if !SANITIZER_FREEBSD
  662. TSAN_INTERCEPTOR(void*, mmap64, void *addr, long_t sz, int prot,
  663. int flags, int fd, u64 off) {
  664. SCOPED_TSAN_INTERCEPTOR(mmap64, addr, sz, prot, flags, fd, off);
  665. if (!fix_mmap_addr(&addr, sz, flags))
  666. return MAP_FAILED;
  667. void *res = REAL(mmap64)(addr, sz, prot, flags, fd, off);
  668. if (res != MAP_FAILED) {
  669. if (fd > 0)
  670. FdAccess(thr, pc, fd);
  671. MemoryRangeImitateWrite(thr, pc, (uptr)res, sz);
  672. }
  673. return res;
  674. }
  675. #define TSAN_MAYBE_INTERCEPT_MMAP64 TSAN_INTERCEPT(mmap64)
  676. #else
  677. #define TSAN_MAYBE_INTERCEPT_MMAP64
  678. #endif
  679. TSAN_INTERCEPTOR(int, munmap, void *addr, long_t sz) {
  680. SCOPED_TSAN_INTERCEPTOR(munmap, addr, sz);
  681. DontNeedShadowFor((uptr)addr, sz);
  682. int res = REAL(munmap)(addr, sz);
  683. return res;
  684. }
  685. #if !SANITIZER_FREEBSD
  686. TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
  687. SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
  688. return user_alloc(thr, pc, sz, align);
  689. }
  690. #define TSAN_MAYBE_INTERCEPT_MEMALIGN TSAN_INTERCEPT(memalign)
  691. #else
  692. #define TSAN_MAYBE_INTERCEPT_MEMALIGN
  693. #endif
  694. TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) {
  695. SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
  696. return user_alloc(thr, pc, sz, align);
  697. }
  698. TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
  699. SCOPED_INTERCEPTOR_RAW(valloc, sz);
  700. return user_alloc(thr, pc, sz, GetPageSizeCached());
  701. }
  702. #if !SANITIZER_FREEBSD
  703. TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
  704. SCOPED_INTERCEPTOR_RAW(pvalloc, sz);
  705. sz = RoundUp(sz, GetPageSizeCached());
  706. return user_alloc(thr, pc, sz, GetPageSizeCached());
  707. }
  708. #define TSAN_MAYBE_INTERCEPT_PVALLOC TSAN_INTERCEPT(pvalloc)
  709. #else
  710. #define TSAN_MAYBE_INTERCEPT_PVALLOC
  711. #endif
  712. TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {
  713. SCOPED_INTERCEPTOR_RAW(posix_memalign, memptr, align, sz);
  714. *memptr = user_alloc(thr, pc, sz, align);
  715. return 0;
  716. }
  717. // Used in thread-safe function static initialization.
  718. extern "C" int INTERFACE_ATTRIBUTE __cxa_guard_acquire(atomic_uint32_t *g) {
  719. SCOPED_INTERCEPTOR_RAW(__cxa_guard_acquire, g);
  720. for (;;) {
  721. u32 cmp = atomic_load(g, memory_order_acquire);
  722. if (cmp == 0) {
  723. if (atomic_compare_exchange_strong(g, &cmp, 1<<16, memory_order_relaxed))
  724. return 1;
  725. } else if (cmp == 1) {
  726. Acquire(thr, pc, (uptr)g);
  727. return 0;
  728. } else {
  729. internal_sched_yield();
  730. }
  731. }
  732. }
  733. extern "C" void INTERFACE_ATTRIBUTE __cxa_guard_release(atomic_uint32_t *g) {
  734. SCOPED_INTERCEPTOR_RAW(__cxa_guard_release, g);
  735. Release(thr, pc, (uptr)g);
  736. atomic_store(g, 1, memory_order_release);
  737. }
  738. extern "C" void INTERFACE_ATTRIBUTE __cxa_guard_abort(atomic_uint32_t *g) {
  739. SCOPED_INTERCEPTOR_RAW(__cxa_guard_abort, g);
  740. atomic_store(g, 0, memory_order_relaxed);
  741. }
  742. static void thread_finalize(void *v) {
  743. uptr iter = (uptr)v;
  744. if (iter > 1) {
  745. if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
  746. Printf("ThreadSanitizer: failed to set thread key\n");
  747. Die();
  748. }
  749. return;
  750. }
  751. {
  752. ThreadState *thr = cur_thread();
  753. ThreadFinish(thr);
  754. SignalContext *sctx = thr->signal_ctx;
  755. if (sctx) {
  756. thr->signal_ctx = 0;
  757. UnmapOrDie(sctx, sizeof(*sctx));
  758. }
  759. }
  760. }
  761. struct ThreadParam {
  762. void* (*callback)(void *arg);
  763. void *param;
  764. atomic_uintptr_t tid;
  765. };
  766. extern "C" void *__tsan_thread_start_func(void *arg) {
  767. ThreadParam *p = (ThreadParam*)arg;
  768. void* (*callback)(void *arg) = p->callback;
  769. void *param = p->param;
  770. int tid = 0;
  771. {
  772. ThreadState *thr = cur_thread();
  773. // Thread-local state is not initialized yet.
  774. ScopedIgnoreInterceptors ignore;
  775. ThreadIgnoreBegin(thr, 0);
  776. if (pthread_setspecific(g_thread_finalize_key,
  777. (void *)kPthreadDestructorIterations)) {
  778. Printf("ThreadSanitizer: failed to set thread key\n");
  779. Die();
  780. }
  781. ThreadIgnoreEnd(thr, 0);
  782. while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
  783. pthread_yield();
  784. atomic_store(&p->tid, 0, memory_order_release);
  785. ThreadStart(thr, tid, GetTid());
  786. }
  787. void *res = callback(param);
  788. // Prevent the callback from being tail called,
  789. // it mixes up stack traces.
  790. volatile int foo = 42;
  791. foo++;
  792. return res;
  793. }
  794. TSAN_INTERCEPTOR(int, pthread_create,
  795. void *th, void *attr, void *(*callback)(void*), void * param) {
  796. SCOPED_INTERCEPTOR_RAW(pthread_create, th, attr, callback, param);
  797. if (ctx->after_multithreaded_fork) {
  798. if (flags()->die_after_fork) {
  799. Report("ThreadSanitizer: starting new threads after multi-threaded "
  800. "fork is not supported. Dying (set die_after_fork=0 to override)\n");
  801. Die();
  802. } else {
  803. VPrintf(1, "ThreadSanitizer: starting new threads after multi-threaded "
  804. "fork is not supported (pid %d). Continuing because of "
  805. "die_after_fork=0, but you are on your own\n", internal_getpid());
  806. }
  807. }
  808. __sanitizer_pthread_attr_t myattr;
  809. if (attr == 0) {
  810. pthread_attr_init(&myattr);
  811. attr = &myattr;
  812. }
  813. int detached = 0;
  814. REAL(pthread_attr_getdetachstate)(attr, &detached);
  815. AdjustStackSize(attr);
  816. ThreadParam p;
  817. p.callback = callback;
  818. p.param = param;
  819. atomic_store(&p.tid, 0, memory_order_relaxed);
  820. int res = -1;
  821. {
  822. // Otherwise we see false positives in pthread stack manipulation.
  823. ScopedIgnoreInterceptors ignore;
  824. ThreadIgnoreBegin(thr, pc);
  825. res = REAL(pthread_create)(th, attr, __tsan_thread_start_func, &p);
  826. ThreadIgnoreEnd(thr, pc);
  827. }
  828. if (res == 0) {
  829. int tid = ThreadCreate(thr, pc, *(uptr*)th, detached);
  830. CHECK_NE(tid, 0);
  831. atomic_store(&p.tid, tid, memory_order_release);
  832. while (atomic_load(&p.tid, memory_order_acquire) != 0)
  833. pthread_yield();
  834. }
  835. if (attr == &myattr)
  836. pthread_attr_destroy(&myattr);
  837. return res;
  838. }
  839. TSAN_INTERCEPTOR(int, pthread_join, void *th, void **ret) {
  840. SCOPED_INTERCEPTOR_RAW(pthread_join, th, ret);
  841. int tid = ThreadTid(thr, pc, (uptr)th);
  842. ThreadIgnoreBegin(thr, pc);
  843. int res = BLOCK_REAL(pthread_join)(th, ret);
  844. ThreadIgnoreEnd(thr, pc);
  845. if (res == 0) {
  846. ThreadJoin(thr, pc, tid);
  847. }
  848. return res;
  849. }
  850. TSAN_INTERCEPTOR(int, pthread_detach, void *th) {
  851. SCOPED_TSAN_INTERCEPTOR(pthread_detach, th);
  852. int tid = ThreadTid(thr, pc, (uptr)th);
  853. int res = REAL(pthread_detach)(th);
  854. if (res == 0) {
  855. ThreadDetach(thr, pc, tid);
  856. }
  857. return res;
  858. }
  859. // Problem:
  860. // NPTL implementation of pthread_cond has 2 versions (2.2.5 and 2.3.2).
  861. // pthread_cond_t has different size in the different versions.
  862. // If call new REAL functions for old pthread_cond_t, they will corrupt memory
  863. // after pthread_cond_t (old cond is smaller).
  864. // If we call old REAL functions for new pthread_cond_t, we will lose some
  865. // functionality (e.g. old functions do not support waiting against
  866. // CLOCK_REALTIME).
  867. // Proper handling would require to have 2 versions of interceptors as well.
  868. // But this is messy, in particular requires linker scripts when sanitizer
  869. // runtime is linked into a shared library.
  870. // Instead we assume we don't have dynamic libraries built against old
  871. // pthread (2.2.5 is dated by 2002). And provide legacy_pthread_cond flag
  872. // that allows to work with old libraries (but this mode does not support
  873. // some features, e.g. pthread_condattr_getpshared).
  874. static void *init_cond(void *c, bool force = false) {
  875. // sizeof(pthread_cond_t) >= sizeof(uptr) in both versions.
  876. // So we allocate additional memory on the side large enough to hold
  877. // any pthread_cond_t object. Always call new REAL functions, but pass
  878. // the aux object to them.
  879. // Note: the code assumes that PTHREAD_COND_INITIALIZER initializes
  880. // first word of pthread_cond_t to zero.
  881. // It's all relevant only for linux.
  882. if (!common_flags()->legacy_pthread_cond)
  883. return c;
  884. atomic_uintptr_t *p = (atomic_uintptr_t*)c;
  885. uptr cond = atomic_load(p, memory_order_acquire);
  886. if (!force && cond != 0)
  887. return (void*)cond;
  888. void *newcond = WRAP(malloc)(pthread_cond_t_sz);
  889. internal_memset(newcond, 0, pthread_cond_t_sz);
  890. if (atomic_compare_exchange_strong(p, &cond, (uptr)newcond,
  891. memory_order_acq_rel))
  892. return newcond;
  893. WRAP(free)(newcond);
  894. return (void*)cond;
  895. }
  896. struct CondMutexUnlockCtx {
  897. ThreadState *thr;
  898. uptr pc;
  899. void *m;
  900. };
  901. static void cond_mutex_unlock(CondMutexUnlockCtx *arg) {
  902. MutexLock(arg->thr, arg->pc, (uptr)arg->m);
  903. }
  904. INTERCEPTOR(int, pthread_cond_init, void *c, void *a) {
  905. void *cond = init_cond(c, true);
  906. SCOPED_TSAN_INTERCEPTOR(pthread_cond_init, cond, a);
  907. MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), true);
  908. return REAL(pthread_cond_init)(cond, a);
  909. }
  910. INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) {
  911. void *cond = init_cond(c);
  912. SCOPED_TSAN_INTERCEPTOR(pthread_cond_wait, cond, m);
  913. MutexUnlock(thr, pc, (uptr)m);
  914. MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
  915. CondMutexUnlockCtx arg = {thr, pc, m};
  916. // This ensures that we handle mutex lock even in case of pthread_cancel.
  917. // See test/tsan/cond_cancel.cc.
  918. int res = call_pthread_cancel_with_cleanup(
  919. (int(*)(void *c, void *m, void *abstime))REAL(pthread_cond_wait),
  920. cond, m, 0, (void(*)(void *arg))cond_mutex_unlock, &arg);
  921. if (res == errno_EOWNERDEAD)
  922. MutexRepair(thr, pc, (uptr)m);
  923. MutexLock(thr, pc, (uptr)m);
  924. return res;
  925. }
  926. INTERCEPTOR(int, pthread_cond_timedwait, void *c, void *m, void *abstime) {
  927. void *cond = init_cond(c);
  928. SCOPED_TSAN_INTERCEPTOR(pthread_cond_timedwait, cond, m, abstime);
  929. MutexUnlock(thr, pc, (uptr)m);
  930. MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
  931. CondMutexUnlockCtx arg = {thr, pc, m};
  932. // This ensures that we handle mutex lock even in case of pthread_cancel.
  933. // See test/tsan/cond_cancel.cc.
  934. int res = call_pthread_cancel_with_cleanup(
  935. REAL(pthread_cond_timedwait), cond, m, abstime,
  936. (void(*)(void *arg))cond_mutex_unlock, &arg);
  937. if (res == errno_EOWNERDEAD)
  938. MutexRepair(thr, pc, (uptr)m);
  939. MutexLock(thr, pc, (uptr)m);
  940. return res;
  941. }
  942. INTERCEPTOR(int, pthread_cond_signal, void *c) {
  943. void *cond = init_cond(c);
  944. SCOPED_TSAN_INTERCEPTOR(pthread_cond_signal, cond);
  945. MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
  946. return REAL(pthread_cond_signal)(cond);
  947. }
  948. INTERCEPTOR(int, pthread_cond_broadcast, void *c) {
  949. void *cond = init_cond(c);
  950. SCOPED_TSAN_INTERCEPTOR(pthread_cond_broadcast, cond);
  951. MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
  952. return REAL(pthread_cond_broadcast)(cond);
  953. }
  954. INTERCEPTOR(int, pthread_cond_destroy, void *c) {
  955. void *cond = init_cond(c);
  956. SCOPED_TSAN_INTERCEPTOR(pthread_cond_destroy, cond);
  957. MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), true);
  958. int res = REAL(pthread_cond_destroy)(cond);
  959. if (common_flags()->legacy_pthread_cond) {
  960. // Free our aux cond and zero the pointer to not leave dangling pointers.
  961. WRAP(free)(cond);
  962. atomic_store((atomic_uintptr_t*)c, 0, memory_order_relaxed);
  963. }
  964. return res;
  965. }
  966. TSAN_INTERCEPTOR(int, pthread_mutex_init, void *m, void *a) {
  967. SCOPED_TSAN_INTERCEPTOR(pthread_mutex_init, m, a);
  968. int res = REAL(pthread_mutex_init)(m, a);
  969. if (res == 0) {
  970. bool recursive = false;
  971. if (a) {
  972. int type = 0;
  973. if (REAL(pthread_mutexattr_gettype)(a, &type) == 0)
  974. recursive = (type == PTHREAD_MUTEX_RECURSIVE
  975. || type == PTHREAD_MUTEX_RECURSIVE_NP);
  976. }
  977. MutexCreate(thr, pc, (uptr)m, false, recursive, false);
  978. }
  979. return res;
  980. }
  981. TSAN_INTERCEPTOR(int, pthread_mutex_destroy, void *m) {
  982. SCOPED_TSAN_INTERCEPTOR(pthread_mutex_destroy, m);
  983. int res = REAL(pthread_mutex_destroy)(m);
  984. if (res == 0 || res == EBUSY) {
  985. MutexDestroy(thr, pc, (uptr)m);
  986. }
  987. return res;
  988. }
  989. TSAN_INTERCEPTOR(int, pthread_mutex_trylock, void *m) {
  990. SCOPED_TSAN_INTERCEPTOR(pthread_mutex_trylock, m);
  991. int res = REAL(pthread_mutex_trylock)(m);
  992. if (res == EOWNERDEAD)
  993. MutexRepair(thr, pc, (uptr)m);
  994. if (res == 0 || res == EOWNERDEAD)
  995. MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true);
  996. return res;
  997. }
  998. TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) {
  999. SCOPED_TSAN_INTERCEPTOR(pthread_mutex_timedlock, m, abstime);
  1000. int res = REAL(pthread_mutex_timedlock)(m, abstime);
  1001. if (res == 0) {
  1002. MutexLock(thr, pc, (uptr)m);
  1003. }
  1004. return res;
  1005. }
  1006. TSAN_INTERCEPTOR(int, pthread_spin_init, void *m, int pshared) {
  1007. SCOPED_TSAN_INTERCEPTOR(pthread_spin_init, m, pshared);
  1008. int res = REAL(pthread_spin_init)(m, pshared);
  1009. if (res == 0) {
  1010. MutexCreate(thr, pc, (uptr)m, false, false, false);
  1011. }
  1012. return res;
  1013. }
  1014. TSAN_INTERCEPTOR(int, pthread_spin_destroy, void *m) {
  1015. SCOPED_TSAN_INTERCEPTOR(pthread_spin_destroy, m);
  1016. int res = REAL(pthread_spin_destroy)(m);
  1017. if (res == 0) {
  1018. MutexDestroy(thr, pc, (uptr)m);
  1019. }
  1020. return res;
  1021. }
  1022. TSAN_INTERCEPTOR(int, pthread_spin_lock, void *m) {
  1023. SCOPED_TSAN_INTERCEPTOR(pthread_spin_lock, m);
  1024. int res = REAL(pthread_spin_lock)(m);
  1025. if (res == 0) {
  1026. MutexLock(thr, pc, (uptr)m);
  1027. }
  1028. return res;
  1029. }
  1030. TSAN_INTERCEPTOR(int, pthread_spin_trylock, void *m) {
  1031. SCOPED_TSAN_INTERCEPTOR(pthread_spin_trylock, m);
  1032. int res = REAL(pthread_spin_trylock)(m);
  1033. if (res == 0) {
  1034. MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true);
  1035. }
  1036. return res;
  1037. }
  1038. TSAN_INTERCEPTOR(int, pthread_spin_unlock, void *m) {
  1039. SCOPED_TSAN_INTERCEPTOR(pthread_spin_unlock, m);
  1040. MutexUnlock(thr, pc, (uptr)m);
  1041. int res = REAL(pthread_spin_unlock)(m);
  1042. return res;
  1043. }
  1044. TSAN_INTERCEPTOR(int, pthread_rwlock_init, void *m, void *a) {
  1045. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_init, m, a);
  1046. int res = REAL(pthread_rwlock_init)(m, a);
  1047. if (res == 0) {
  1048. MutexCreate(thr, pc, (uptr)m, true, false, false);
  1049. }
  1050. return res;
  1051. }
  1052. TSAN_INTERCEPTOR(int, pthread_rwlock_destroy, void *m) {
  1053. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_destroy, m);
  1054. int res = REAL(pthread_rwlock_destroy)(m);
  1055. if (res == 0) {
  1056. MutexDestroy(thr, pc, (uptr)m);
  1057. }
  1058. return res;
  1059. }
  1060. TSAN_INTERCEPTOR(int, pthread_rwlock_rdlock, void *m) {
  1061. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_rdlock, m);
  1062. int res = REAL(pthread_rwlock_rdlock)(m);
  1063. if (res == 0) {
  1064. MutexReadLock(thr, pc, (uptr)m);
  1065. }
  1066. return res;
  1067. }
  1068. TSAN_INTERCEPTOR(int, pthread_rwlock_tryrdlock, void *m) {
  1069. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_tryrdlock, m);
  1070. int res = REAL(pthread_rwlock_tryrdlock)(m);
  1071. if (res == 0) {
  1072. MutexReadLock(thr, pc, (uptr)m, /*try_lock=*/true);
  1073. }
  1074. return res;
  1075. }
  1076. TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) {
  1077. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedrdlock, m, abstime);
  1078. int res = REAL(pthread_rwlock_timedrdlock)(m, abstime);
  1079. if (res == 0) {
  1080. MutexReadLock(thr, pc, (uptr)m);
  1081. }
  1082. return res;
  1083. }
  1084. TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) {
  1085. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m);
  1086. int res = REAL(pthread_rwlock_wrlock)(m);
  1087. if (res == 0) {
  1088. MutexLock(thr, pc, (uptr)m);
  1089. }
  1090. return res;
  1091. }
  1092. TSAN_INTERCEPTOR(int, pthread_rwlock_trywrlock, void *m) {
  1093. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_trywrlock, m);
  1094. int res = REAL(pthread_rwlock_trywrlock)(m);
  1095. if (res == 0) {
  1096. MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true);
  1097. }
  1098. return res;
  1099. }
  1100. TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) {
  1101. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedwrlock, m, abstime);
  1102. int res = REAL(pthread_rwlock_timedwrlock)(m, abstime);
  1103. if (res == 0) {
  1104. MutexLock(thr, pc, (uptr)m);
  1105. }
  1106. return res;
  1107. }
  1108. TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) {
  1109. SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_unlock, m);
  1110. MutexReadOrWriteUnlock(thr, pc, (uptr)m);
  1111. int res = REAL(pthread_rwlock_unlock)(m);
  1112. return res;
  1113. }
  1114. TSAN_INTERCEPTOR(int, pthread_barrier_init, void *b, void *a, unsigned count) {
  1115. SCOPED_TSAN_INTERCEPTOR(pthread_barrier_init, b, a, count);
  1116. MemoryWrite(thr, pc, (uptr)b, kSizeLog1);
  1117. int res = REAL(pthread_barrier_init)(b, a, count);
  1118. return res;
  1119. }
  1120. TSAN_INTERCEPTOR(int, pthread_barrier_destroy, void *b) {
  1121. SCOPED_TSAN_INTERCEPTOR(pthread_barrier_destroy, b);
  1122. MemoryWrite(thr, pc, (uptr)b, kSizeLog1);
  1123. int res = REAL(pthread_barrier_destroy)(b);
  1124. return res;
  1125. }
  1126. TSAN_INTERCEPTOR(int, pthread_barrier_wait, void *b) {
  1127. SCOPED_TSAN_INTERCEPTOR(pthread_barrier_wait, b);
  1128. Release(thr, pc, (uptr)b);
  1129. MemoryRead(thr, pc, (uptr)b, kSizeLog1);
  1130. int res = REAL(pthread_barrier_wait)(b);
  1131. MemoryRead(thr, pc, (uptr)b, kSizeLog1);
  1132. if (res == 0 || res == PTHREAD_BARRIER_SERIAL_THREAD) {
  1133. Acquire(thr, pc, (uptr)b);
  1134. }
  1135. return res;
  1136. }
  1137. TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) {
  1138. SCOPED_INTERCEPTOR_RAW(pthread_once, o, f);
  1139. if (o == 0 || f == 0)
  1140. return EINVAL;
  1141. atomic_uint32_t *a = static_cast<atomic_uint32_t*>(o);
  1142. u32 v = atomic_load(a, memory_order_acquire);
  1143. if (v == 0 && atomic_compare_exchange_strong(a, &v, 1,
  1144. memory_order_relaxed)) {
  1145. (*f)();
  1146. if (!thr->in_ignored_lib)
  1147. Release(thr, pc, (uptr)o);
  1148. atomic_store(a, 2, memory_order_release);
  1149. } else {
  1150. while (v != 2) {
  1151. pthread_yield();
  1152. v = atomic_load(a, memory_order_acquire);
  1153. }
  1154. if (!thr->in_ignored_lib)
  1155. Acquire(thr, pc, (uptr)o);
  1156. }
  1157. return 0;
  1158. }
  1159. TSAN_INTERCEPTOR(int, sem_init, void *s, int pshared, unsigned value) {
  1160. SCOPED_TSAN_INTERCEPTOR(sem_init, s, pshared, value);
  1161. int res = REAL(sem_init)(s, pshared, value);
  1162. return res;
  1163. }
  1164. TSAN_INTERCEPTOR(int, sem_destroy, void *s) {
  1165. SCOPED_TSAN_INTERCEPTOR(sem_destroy, s);
  1166. int res = REAL(sem_destroy)(s);
  1167. return res;
  1168. }
  1169. TSAN_INTERCEPTOR(int, sem_wait, void *s) {
  1170. SCOPED_TSAN_INTERCEPTOR(sem_wait, s);
  1171. int res = BLOCK_REAL(sem_wait)(s);
  1172. if (res == 0) {
  1173. Acquire(thr, pc, (uptr)s);
  1174. }
  1175. return res;
  1176. }
  1177. TSAN_INTERCEPTOR(int, sem_trywait, void *s) {
  1178. SCOPED_TSAN_INTERCEPTOR(sem_trywait, s);
  1179. int res = BLOCK_REAL(sem_trywait)(s);
  1180. if (res == 0) {
  1181. Acquire(thr, pc, (uptr)s);
  1182. }
  1183. return res;
  1184. }
  1185. TSAN_INTERCEPTOR(int, sem_timedwait, void *s, void *abstime) {
  1186. SCOPED_TSAN_INTERCEPTOR(sem_timedwait, s, abstime);
  1187. int res = BLOCK_REAL(sem_timedwait)(s, abstime);
  1188. if (res == 0) {
  1189. Acquire(thr, pc, (uptr)s);
  1190. }
  1191. return res;
  1192. }
  1193. TSAN_INTERCEPTOR(int, sem_post, void *s) {
  1194. SCOPED_TSAN_INTERCEPTOR(sem_post, s);
  1195. Release(thr, pc, (uptr)s);
  1196. int res = REAL(sem_post)(s);
  1197. return res;
  1198. }
  1199. TSAN_INTERCEPTOR(int, sem_getvalue, void *s, int *sval) {
  1200. SCOPED_TSAN_INTERCEPTOR(sem_getvalue, s, sval);
  1201. int res = REAL(sem_getvalue)(s, sval);
  1202. if (res == 0) {
  1203. Acquire(thr, pc, (uptr)s);
  1204. }
  1205. return res;
  1206. }
  1207. #if !SANITIZER_FREEBSD
  1208. TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
  1209. SCOPED_TSAN_INTERCEPTOR(__xstat, version, path, buf);
  1210. return REAL(__xstat)(version, path, buf);
  1211. }
  1212. #define TSAN_MAYBE_INTERCEPT___XSTAT TSAN_INTERCEPT(__xstat)
  1213. #else
  1214. #define TSAN_MAYBE_INTERCEPT___XSTAT
  1215. #endif
  1216. TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) {
  1217. #if SANITIZER_FREEBSD
  1218. SCOPED_TSAN_INTERCEPTOR(stat, path, buf);
  1219. return REAL(stat)(path, buf);
  1220. #else
  1221. SCOPED_TSAN_INTERCEPTOR(__xstat, 0, path, buf);
  1222. return REAL(__xstat)(0, path, buf);
  1223. #endif
  1224. }
  1225. #if !SANITIZER_FREEBSD
  1226. TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
  1227. SCOPED_TSAN_INTERCEPTOR(__xstat64, version, path, buf);
  1228. return REAL(__xstat64)(version, path, buf);
  1229. }
  1230. #define TSAN_MAYBE_INTERCEPT___XSTAT64 TSAN_INTERCEPT(__xstat64)
  1231. #else
  1232. #define TSAN_MAYBE_INTERCEPT___XSTAT64
  1233. #endif
  1234. #if !SANITIZER_FREEBSD
  1235. TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) {
  1236. SCOPED_TSAN_INTERCEPTOR(__xstat64, 0, path, buf);
  1237. return REAL(__xstat64)(0, path, buf);
  1238. }
  1239. #define TSAN_MAYBE_INTERCEPT_STAT64 TSAN_INTERCEPT(stat64)
  1240. #else
  1241. #define TSAN_MAYBE_INTERCEPT_STAT64
  1242. #endif
  1243. #if !SANITIZER_FREEBSD
  1244. TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
  1245. SCOPED_TSAN_INTERCEPTOR(__lxstat, version, path, buf);
  1246. return REAL(__lxstat)(version, path, buf);
  1247. }
  1248. #define TSAN_MAYBE_INTERCEPT___LXSTAT TSAN_INTERCEPT(__lxstat)
  1249. #else
  1250. #define TSAN_MAYBE_INTERCEPT___LXSTAT
  1251. #endif
  1252. TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) {
  1253. #if SANITIZER_FREEBSD
  1254. SCOPED_TSAN_INTERCEPTOR(lstat, path, buf);
  1255. return REAL(lstat)(path, buf);
  1256. #else
  1257. SCOPED_TSAN_INTERCEPTOR(__lxstat, 0, path, buf);
  1258. return REAL(__lxstat)(0, path, buf);
  1259. #endif
  1260. }
  1261. #if !SANITIZER_FREEBSD
  1262. TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
  1263. SCOPED_TSAN_INTERCEPTOR(__lxstat64, version, path, buf);
  1264. return REAL(__lxstat64)(version, path, buf);
  1265. }
  1266. #define TSAN_MAYBE_INTERCEPT___LXSTAT64 TSAN_INTERCEPT(__lxstat64)
  1267. #else
  1268. #define TSAN_MAYBE_INTERCEPT___LXSTAT64
  1269. #endif
  1270. #if !SANITIZER_FREEBSD
  1271. TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) {
  1272. SCOPED_TSAN_INTERCEPTOR(__lxstat64, 0, path, buf);
  1273. return REAL(__lxstat64)(0, path, buf);
  1274. }
  1275. #define TSAN_MAYBE_INTERCEPT_LSTAT64 TSAN_INTERCEPT(lstat64)
  1276. #else
  1277. #define TSAN_MAYBE_INTERCEPT_LSTAT64
  1278. #endif
  1279. #if !SANITIZER_FREEBSD
  1280. TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) {
  1281. SCOPED_TSAN_INTERCEPTOR(__fxstat, version, fd, buf);
  1282. if (fd > 0)
  1283. FdAccess(thr, pc, fd);
  1284. return REAL(__fxstat)(version, fd, buf);
  1285. }
  1286. #define TSAN_MAYBE_INTERCEPT___FXSTAT TSAN_INTERCEPT(__fxstat)
  1287. #else
  1288. #define TSAN_MAYBE_INTERCEPT___FXSTAT
  1289. #endif
  1290. TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) {
  1291. #if SANITIZER_FREEBSD
  1292. SCOPED_TSAN_INTERCEPTOR(fstat, fd, buf);
  1293. if (fd > 0)
  1294. FdAccess(thr, pc, fd);
  1295. return REAL(fstat)(fd, buf);
  1296. #else
  1297. SCOPED_TSAN_INTERCEPTOR(__fxstat, 0, fd, buf);
  1298. if (fd > 0)
  1299. FdAccess(thr, pc, fd);
  1300. return REAL(__fxstat)(0, fd, buf);
  1301. #endif
  1302. }
  1303. #if !SANITIZER_FREEBSD
  1304. TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) {
  1305. SCOPED_TSAN_INTERCEPTOR(__fxstat64, version, fd, buf);
  1306. if (fd > 0)
  1307. FdAccess(thr, pc, fd);
  1308. return REAL(__fxstat64)(version, fd, buf);
  1309. }
  1310. #define TSAN_MAYBE_INTERCEPT___FXSTAT64 TSAN_INTERCEPT(__fxstat64)
  1311. #else
  1312. #define TSAN_MAYBE_INTERCEPT___FXSTAT64
  1313. #endif
  1314. #if !SANITIZER_FREEBSD
  1315. TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) {
  1316. SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf);
  1317. if (fd > 0)
  1318. FdAccess(thr, pc, fd);
  1319. return REAL(__fxstat64)(0, fd, buf);
  1320. }
  1321. #define TSAN_MAYBE_INTERCEPT_FSTAT64 TSAN_INTERCEPT(fstat64)
  1322. #else
  1323. #define TSAN_MAYBE_INTERCEPT_FSTAT64
  1324. #endif
  1325. TSAN_INTERCEPTOR(int, open, const char *name, int flags, int mode) {
  1326. SCOPED_TSAN_INTERCEPTOR(open, name, flags, mode);
  1327. int fd = REAL(open)(name, flags, mode);
  1328. if (fd >= 0)
  1329. FdFileCreate(thr, pc, fd);
  1330. return fd;
  1331. }
  1332. #if !SANITIZER_FREEBSD
  1333. TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) {
  1334. SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode);
  1335. int fd = REAL(open64)(name, flags, mode);
  1336. if (fd >= 0)
  1337. FdFileCreate(thr, pc, fd);
  1338. return fd;
  1339. }
  1340. #define TSAN_MAYBE_INTERCEPT_OPEN64 TSAN_INTERCEPT(open64)
  1341. #else
  1342. #define TSAN_MAYBE_INTERCEPT_OPEN64
  1343. #endif
  1344. TSAN_INTERCEPTOR(int, creat, const char *name, int mode) {
  1345. SCOPED_TSAN_INTERCEPTOR(creat, name, mode);
  1346. int fd = REAL(creat)(name, mode);
  1347. if (fd >= 0)
  1348. FdFileCreate(thr, pc, fd);
  1349. return fd;
  1350. }
  1351. #if !SANITIZER_FREEBSD
  1352. TSAN_INTERCEPTOR(int, creat64, const char *name, int mode) {
  1353. SCOPED_TSAN_INTERCEPTOR(creat64, name, mode);
  1354. int fd = REAL(creat64)(name, mode);
  1355. if (fd >= 0)
  1356. FdFileCreate(thr, pc, fd);
  1357. return fd;
  1358. }
  1359. #define TSAN_MAYBE_INTERCEPT_CREAT64 TSAN_INTERCEPT(creat64)
  1360. #else
  1361. #define TSAN_MAYBE_INTERCEPT_CREAT64
  1362. #endif
  1363. TSAN_INTERCEPTOR(int, dup, int oldfd) {
  1364. SCOPED_TSAN_INTERCEPTOR(dup, oldfd);
  1365. int newfd = REAL(dup)(oldfd);
  1366. if (oldfd >= 0 && newfd >= 0 && newfd != oldfd)
  1367. FdDup(thr, pc, oldfd, newfd);
  1368. return newfd;
  1369. }
  1370. TSAN_INTERCEPTOR(int, dup2, int oldfd, int newfd) {
  1371. SCOPED_TSAN_INTERCEPTOR(dup2, oldfd, newfd);
  1372. int newfd2 = REAL(dup2)(oldfd, newfd);
  1373. if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd)
  1374. FdDup(thr, pc, oldfd, newfd2);
  1375. return newfd2;
  1376. }
  1377. TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) {
  1378. SCOPED_TSAN_INTERCEPTOR(dup3, oldfd, newfd, flags);
  1379. int newfd2 = REAL(dup3)(oldfd, newfd, flags);
  1380. if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd)
  1381. FdDup(thr, pc, oldfd, newfd2);
  1382. return newfd2;
  1383. }
  1384. #if !SANITIZER_FREEBSD
  1385. TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) {
  1386. SCOPED_TSAN_INTERCEPTOR(eventfd, initval, flags);
  1387. int fd = REAL(eventfd)(initval, flags);
  1388. if (fd >= 0)
  1389. FdEventCreate(thr, pc, fd);
  1390. return fd;
  1391. }
  1392. #define TSAN_MAYBE_INTERCEPT_EVENTFD TSAN_INTERCEPT(eventfd)
  1393. #else
  1394. #define TSAN_MAYBE_INTERCEPT_EVENTFD
  1395. #endif
  1396. #if !SANITIZER_FREEBSD
  1397. TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) {
  1398. SCOPED_TSAN_INTERCEPTOR(signalfd, fd, mask, flags);
  1399. if (fd >= 0)
  1400. FdClose(thr, pc, fd);
  1401. fd = REAL(signalfd)(fd, mask, flags);
  1402. if (fd >= 0)
  1403. FdSignalCreate(thr, pc, fd);
  1404. return fd;
  1405. }
  1406. #define TSAN_MAYBE_INTERCEPT_SIGNALFD TSAN_INTERCEPT(signalfd)
  1407. #else
  1408. #define TSAN_MAYBE_INTERCEPT_SIGNALFD
  1409. #endif
  1410. #if !SANITIZER_FREEBSD
  1411. TSAN_INTERCEPTOR(int, inotify_init, int fake) {
  1412. SCOPED_TSAN_INTERCEPTOR(inotify_init, fake);
  1413. int fd = REAL(inotify_init)(fake);
  1414. if (fd >= 0)
  1415. FdInotifyCreate(thr, pc, fd);
  1416. return fd;
  1417. }
  1418. #define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT TSAN_INTERCEPT(inotify_init)
  1419. #else
  1420. #define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT
  1421. #endif
  1422. #if !SANITIZER_FREEBSD
  1423. TSAN_INTERCEPTOR(int, inotify_init1, int flags) {
  1424. SCOPED_TSAN_INTERCEPTOR(inotify_init1, flags);
  1425. int fd = REAL(inotify_init1)(flags);
  1426. if (fd >= 0)
  1427. FdInotifyCreate(thr, pc, fd);
  1428. return fd;
  1429. }
  1430. #define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 TSAN_INTERCEPT(inotify_init1)
  1431. #else
  1432. #define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
  1433. #endif
  1434. TSAN_INTERCEPTOR(int, socket, int domain, int type, int protocol) {
  1435. SCOPED_TSAN_INTERCEPTOR(socket, domain, type, protocol);
  1436. int fd = REAL(socket)(domain, type, protocol);
  1437. if (fd >= 0)
  1438. FdSocketCreate(thr, pc, fd);
  1439. return fd;
  1440. }
  1441. TSAN_INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int *fd) {
  1442. SCOPED_TSAN_INTERCEPTOR(socketpair, domain, type, protocol, fd);
  1443. int res = REAL(socketpair)(domain, type, protocol, fd);
  1444. if (res == 0 && fd[0] >= 0 && fd[1] >= 0)
  1445. FdPipeCreate(thr, pc, fd[0], fd[1]);
  1446. return res;
  1447. }
  1448. TSAN_INTERCEPTOR(int, connect, int fd, void *addr, unsigned addrlen) {
  1449. SCOPED_TSAN_INTERCEPTOR(connect, fd, addr, addrlen);
  1450. FdSocketConnecting(thr, pc, fd);
  1451. int res = REAL(connect)(fd, addr, addrlen);
  1452. if (res == 0 && fd >= 0)
  1453. FdSocketConnect(thr, pc, fd);
  1454. return res;
  1455. }
  1456. TSAN_INTERCEPTOR(int, bind, int fd, void *addr, unsigned addrlen) {
  1457. SCOPED_TSAN_INTERCEPTOR(bind, fd, addr, addrlen);
  1458. int res = REAL(bind)(fd, addr, addrlen);
  1459. if (fd > 0 && res == 0)
  1460. FdAccess(thr, pc, fd);
  1461. return res;
  1462. }
  1463. TSAN_INTERCEPTOR(int, listen, int fd, int backlog) {
  1464. SCOPED_TSAN_INTERCEPTOR(listen, fd, backlog);
  1465. int res = REAL(listen)(fd, backlog);
  1466. if (fd > 0 && res == 0)
  1467. FdAccess(thr, pc, fd);
  1468. return res;
  1469. }
  1470. #if !SANITIZER_FREEBSD
  1471. TSAN_INTERCEPTOR(int, epoll_create, int size) {
  1472. SCOPED_TSAN_INTERCEPTOR(epoll_create, size);
  1473. int fd = REAL(epoll_create)(size);
  1474. if (fd >= 0)
  1475. FdPollCreate(thr, pc, fd);
  1476. return fd;
  1477. }
  1478. #define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE TSAN_INTERCEPT(epoll_create)
  1479. #else
  1480. #define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE
  1481. #endif
  1482. #if !SANITIZER_FREEBSD
  1483. TSAN_INTERCEPTOR(int, epoll_create1, int flags) {
  1484. SCOPED_TSAN_INTERCEPTOR(epoll_create1, flags);
  1485. int fd = REAL(epoll_create1)(flags);
  1486. if (fd >= 0)
  1487. FdPollCreate(thr, pc, fd);
  1488. return fd;
  1489. }
  1490. #define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE1 TSAN_INTERCEPT(epoll_create1)
  1491. #else
  1492. #define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE1
  1493. #endif
  1494. TSAN_INTERCEPTOR(int, close, int fd) {
  1495. SCOPED_TSAN_INTERCEPTOR(close, fd);
  1496. if (fd >= 0)
  1497. FdClose(thr, pc, fd);
  1498. return REAL(close)(fd);
  1499. }
  1500. #if !SANITIZER_FREEBSD
  1501. TSAN_INTERCEPTOR(int, __close, int fd) {
  1502. SCOPED_TSAN_INTERCEPTOR(__close, fd);
  1503. if (fd >= 0)
  1504. FdClose(thr, pc, fd);
  1505. return REAL(__close)(fd);
  1506. }
  1507. #define TSAN_MAYBE_INTERCEPT___CLOSE TSAN_INTERCEPT(__close)
  1508. #else
  1509. #define TSAN_MAYBE_INTERCEPT___CLOSE
  1510. #endif
  1511. // glibc guts
  1512. #if !SANITIZER_FREEBSD
  1513. TSAN_INTERCEPTOR(void, __res_iclose, void *state, bool free_addr) {
  1514. SCOPED_TSAN_INTERCEPTOR(__res_iclose, state, free_addr);
  1515. int fds[64];
  1516. int cnt = ExtractResolvFDs(state, fds, ARRAY_SIZE(fds));
  1517. for (int i = 0; i < cnt; i++) {
  1518. if (fds[i] > 0)
  1519. FdClose(thr, pc, fds[i]);
  1520. }
  1521. REAL(__res_iclose)(state, free_addr);
  1522. }
  1523. #define TSAN_MAYBE_INTERCEPT___RES_ICLOSE TSAN_INTERCEPT(__res_iclose)
  1524. #else
  1525. #define TSAN_MAYBE_INTERCEPT___RES_ICLOSE
  1526. #endif
  1527. TSAN_INTERCEPTOR(int, pipe, int *pipefd) {
  1528. SCOPED_TSAN_INTERCEPTOR(pipe, pipefd);
  1529. int res = REAL(pipe)(pipefd);
  1530. if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0)
  1531. FdPipeCreate(thr, pc, pipefd[0], pipefd[1]);
  1532. return res;
  1533. }
  1534. TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) {
  1535. SCOPED_TSAN_INTERCEPTOR(pipe2, pipefd, flags);
  1536. int res = REAL(pipe2)(pipefd, flags);
  1537. if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0)
  1538. FdPipeCreate(thr, pc, pipefd[0], pipefd[1]);
  1539. return res;
  1540. }
  1541. TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) {
  1542. SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags);
  1543. if (fd >= 0) {
  1544. FdAccess(thr, pc, fd);
  1545. FdRelease(thr, pc, fd);
  1546. }
  1547. int res = REAL(send)(fd, buf, len, flags);
  1548. return res;
  1549. }
  1550. TSAN_INTERCEPTOR(long_t, sendmsg, int fd, void *msg, int flags) {
  1551. SCOPED_TSAN_INTERCEPTOR(sendmsg, fd, msg, flags);
  1552. if (fd >= 0) {
  1553. FdAccess(thr, pc, fd);
  1554. FdRelease(thr, pc, fd);
  1555. }
  1556. int res = REAL(sendmsg)(fd, msg, flags);
  1557. return res;
  1558. }
  1559. TSAN_INTERCEPTOR(long_t, recv, int fd, void *buf, long_t len, int flags) {
  1560. SCOPED_TSAN_INTERCEPTOR(recv, fd, buf, len, flags);
  1561. if (fd >= 0)
  1562. FdAccess(thr, pc, fd);
  1563. int res = REAL(recv)(fd, buf, len, flags);
  1564. if (res >= 0 && fd >= 0) {
  1565. FdAcquire(thr, pc, fd);
  1566. }
  1567. return res;
  1568. }
  1569. TSAN_INTERCEPTOR(int, unlink, char *path) {
  1570. SCOPED_TSAN_INTERCEPTOR(unlink, path);
  1571. Release(thr, pc, File2addr(path));
  1572. int res = REAL(unlink)(path);
  1573. return res;
  1574. }
  1575. TSAN_INTERCEPTOR(void*, tmpfile, int fake) {
  1576. SCOPED_TSAN_INTERCEPTOR(tmpfile, fake);
  1577. void *res = REAL(tmpfile)(fake);
  1578. if (res) {
  1579. int fd = fileno_unlocked(res);
  1580. if (fd >= 0)
  1581. FdFileCreate(thr, pc, fd);
  1582. }
  1583. return res;
  1584. }
  1585. #if !SANITIZER_FREEBSD
  1586. TSAN_INTERCEPTOR(void*, tmpfile64, int fake) {
  1587. SCOPED_TSAN_INTERCEPTOR(tmpfile64, fake);
  1588. void *res = REAL(tmpfile64)(fake);
  1589. if (res) {
  1590. int fd = fileno_unlocked(res);
  1591. if (fd >= 0)
  1592. FdFileCreate(thr, pc, fd);
  1593. }
  1594. return res;
  1595. }
  1596. #define TSAN_MAYBE_INTERCEPT_TMPFILE64 TSAN_INTERCEPT(tmpfile64)
  1597. #else
  1598. #define TSAN_MAYBE_INTERCEPT_TMPFILE64
  1599. #endif
  1600. TSAN_INTERCEPTOR(uptr, fread, void *ptr, uptr size, uptr nmemb, void *f) {
  1601. // libc file streams can call user-supplied functions, see fopencookie.
  1602. {
  1603. SCOPED_TSAN_INTERCEPTOR(fread, ptr, size, nmemb, f);
  1604. MemoryAccessRange(thr, pc, (uptr)ptr, size * nmemb, true);
  1605. }
  1606. return REAL(fread)(ptr, size, nmemb, f);
  1607. }
  1608. TSAN_INTERCEPTOR(uptr, fwrite, const void *p, uptr size, uptr nmemb, void *f) {
  1609. // libc file streams can call user-supplied functions, see fopencookie.
  1610. {
  1611. SCOPED_TSAN_INTERCEPTOR(fwrite, p, size, nmemb, f);
  1612. MemoryAccessRange(thr, pc, (uptr)p, size * nmemb, false);
  1613. }
  1614. return REAL(fwrite)(p, size, nmemb, f);
  1615. }
  1616. TSAN_INTERCEPTOR(void, abort, int fake) {
  1617. SCOPED_TSAN_INTERCEPTOR(abort, fake);
  1618. REAL(fflush)(0);
  1619. REAL(abort)(fake);
  1620. }
  1621. TSAN_INTERCEPTOR(int, puts, const char *s) {
  1622. SCOPED_TSAN_INTERCEPTOR(puts, s);
  1623. MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s), false);
  1624. return REAL(puts)(s);
  1625. }
  1626. TSAN_INTERCEPTOR(int, rmdir, char *path) {
  1627. SCOPED_TSAN_INTERCEPTOR(rmdir, path);
  1628. Release(thr, pc, Dir2addr(path));
  1629. int res = REAL(rmdir)(path);
  1630. return res;
  1631. }
  1632. TSAN_INTERCEPTOR(void*, opendir, char *path) {
  1633. SCOPED_TSAN_INTERCEPTOR(opendir, path);
  1634. void *res = REAL(opendir)(path);
  1635. if (res != 0)
  1636. Acquire(thr, pc, Dir2addr(path));
  1637. return res;
  1638. }
  1639. #if !SANITIZER_FREEBSD
  1640. TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) {
  1641. SCOPED_TSAN_INTERCEPTOR(epoll_ctl, epfd, op, fd, ev);
  1642. if (epfd >= 0)
  1643. FdAccess(thr, pc, epfd);
  1644. if (epfd >= 0 && fd >= 0)
  1645. FdAccess(thr, pc, fd);
  1646. if (op == EPOLL_CTL_ADD && epfd >= 0)
  1647. FdRelease(thr, pc, epfd);
  1648. int res = REAL(epoll_ctl)(epfd, op, fd, ev);
  1649. return res;
  1650. }
  1651. #define TSAN_MAYBE_INTERCEPT_EPOLL_CTL TSAN_INTERCEPT(epoll_ctl)
  1652. #else
  1653. #define TSAN_MAYBE_INTERCEPT_EPOLL_CTL
  1654. #endif
  1655. #if !SANITIZER_FREEBSD
  1656. TSAN_INTERCEPTOR(int, epoll_wait, int epfd, void *ev, int cnt, int timeout) {
  1657. SCOPED_TSAN_INTERCEPTOR(epoll_wait, epfd, ev, cnt, timeout);
  1658. if (epfd >= 0)
  1659. FdAccess(thr, pc, epfd);
  1660. int res = BLOCK_REAL(epoll_wait)(epfd, ev, cnt, timeout);
  1661. if (res > 0 && epfd >= 0)
  1662. FdAcquire(thr, pc, epfd);
  1663. return res;
  1664. }
  1665. #define TSAN_MAYBE_INTERCEPT_EPOLL_WAIT TSAN_INTERCEPT(epoll_wait)
  1666. #else
  1667. #define TSAN_MAYBE_INTERCEPT_EPOLL_WAIT
  1668. #endif
  1669. namespace __tsan {
  1670. static void CallUserSignalHandler(ThreadState *thr, bool sync, bool acquire,
  1671. bool sigact, int sig, my_siginfo_t *info, void *uctx) {
  1672. if (acquire)
  1673. Acquire(thr, 0, (uptr)&sigactions[sig]);
  1674. // Ensure that the handler does not spoil errno.
  1675. const int saved_errno = errno;
  1676. errno = 99;
  1677. // Need to remember pc before the call, because the handler can reset it.
  1678. uptr pc = sigact ?
  1679. (uptr)sigactions[sig].sa_sigaction :
  1680. (uptr)sigactions[sig].sa_handler;
  1681. pc += 1; // return address is expected, OutputReport() will undo this
  1682. if (sigact)
  1683. sigactions[sig].sa_sigaction(sig, info, uctx);
  1684. else
  1685. sigactions[sig].sa_handler(sig);
  1686. // We do not detect errno spoiling for SIGTERM,
  1687. // because some SIGTERM handlers do spoil errno but reraise SIGTERM,
  1688. // tsan reports false positive in such case.
  1689. // It's difficult to properly detect this situation (reraise),
  1690. // because in async signal processing case (when handler is called directly
  1691. // from rtl_generic_sighandler) we have not yet received the reraised
  1692. // signal; and it looks too fragile to intercept all ways to reraise a signal.
  1693. if (flags()->report_bugs && !sync && sig != SIGTERM && errno != 99) {
  1694. VarSizeStackTrace stack;
  1695. ObtainCurrentStack(thr, pc, &stack);
  1696. ThreadRegistryLock l(ctx->thread_registry);
  1697. ScopedReport rep(ReportTypeErrnoInSignal);
  1698. if (!IsFiredSuppression(ctx, rep, stack)) {
  1699. rep.AddStack(stack, true);
  1700. OutputReport(thr, rep);
  1701. }
  1702. }
  1703. errno = saved_errno;
  1704. }
  1705. void ProcessPendingSignals(ThreadState *thr) {
  1706. SignalContext *sctx = SigCtx(thr);
  1707. if (sctx == 0 ||
  1708. atomic_load(&sctx->have_pending_signals, memory_order_relaxed) == 0)
  1709. return;
  1710. atomic_store(&sctx->have_pending_signals, 0, memory_order_relaxed);
  1711. atomic_fetch_add(&thr->in_signal_handler, 1, memory_order_relaxed);
  1712. // These are too big for stack.
  1713. static THREADLOCAL __sanitizer_sigset_t emptyset, oldset;
  1714. REAL(sigfillset)(&emptyset);
  1715. pthread_sigmask(SIG_SETMASK, &emptyset, &oldset);
  1716. for (int sig = 0; sig < kSigCount; sig++) {
  1717. SignalDesc *signal = &sctx->pending_signals[sig];
  1718. if (signal->armed) {
  1719. signal->armed = false;
  1720. if (sigactions[sig].sa_handler != SIG_DFL
  1721. && sigactions[sig].sa_handler != SIG_IGN) {
  1722. CallUserSignalHandler(thr, false, true, signal->sigaction,
  1723. sig, &signal->siginfo, &signal->ctx);
  1724. }
  1725. }
  1726. }
  1727. pthread_sigmask(SIG_SETMASK, &oldset, 0);
  1728. atomic_fetch_add(&thr->in_signal_handler, -1, memory_order_relaxed);
  1729. }
  1730. } // namespace __tsan
  1731. static bool is_sync_signal(SignalContext *sctx, int sig) {
  1732. return sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
  1733. sig == SIGABRT || sig == SIGFPE || sig == SIGPIPE || sig == SIGSYS ||
  1734. // If we are sending signal to ourselves, we must process it now.
  1735. (sctx && sig == sctx->int_signal_send);
  1736. }
  1737. void ALWAYS_INLINE rtl_generic_sighandler(bool sigact, int sig,
  1738. my_siginfo_t *info, void *ctx) {
  1739. ThreadState *thr = cur_thread();
  1740. SignalContext *sctx = SigCtx(thr);
  1741. if (sig < 0 || sig >= kSigCount) {
  1742. VPrintf(1, "ThreadSanitizer: ignoring signal %d\n", sig);
  1743. return;
  1744. }
  1745. // Don't mess with synchronous signals.
  1746. const bool sync = is_sync_signal(sctx, sig);
  1747. if (sync ||
  1748. // If we are in blocking function, we can safely process it now
  1749. // (but check if we are in a recursive interceptor,
  1750. // i.e. pthread_join()->munmap()).
  1751. (sctx && atomic_load(&sctx->in_blocking_func, memory_order_relaxed))) {
  1752. atomic_fetch_add(&thr->in_signal_handler, 1, memory_order_relaxed);
  1753. if (sctx && atomic_load(&sctx->in_blocking_func, memory_order_relaxed)) {
  1754. // We ignore interceptors in blocking functions,
  1755. // temporary enbled them again while we are calling user function.
  1756. int const i = thr->ignore_interceptors;
  1757. thr->ignore_interceptors = 0;
  1758. atomic_store(&sctx->in_blocking_func, 0, memory_order_relaxed);
  1759. CallUserSignalHandler(thr, sync, true, sigact, sig, info, ctx);
  1760. thr->ignore_interceptors = i;
  1761. atomic_store(&sctx->in_blocking_func, 1, memory_order_relaxed);
  1762. } else {
  1763. // Be very conservative with when we do acquire in this case.
  1764. // It's unsafe to do acquire in async handlers, because ThreadState
  1765. // can be in inconsistent state.
  1766. // SIGSYS looks relatively safe -- it's synchronous and can actually
  1767. // need some global state.
  1768. bool acq = (sig == SIGSYS);
  1769. CallUserSignalHandler(thr, sync, acq, sigact, sig, info, ctx);
  1770. }
  1771. atomic_fetch_add(&thr->in_signal_handler, -1, memory_order_relaxed);
  1772. return;
  1773. }
  1774. if (sctx == 0)
  1775. return;
  1776. SignalDesc *signal = &sctx->pending_signals[sig];
  1777. if (signal->armed == false) {
  1778. signal->armed = true;
  1779. signal->sigaction = sigact;
  1780. if (info)
  1781. internal_memcpy(&signal->siginfo, info, sizeof(*info));
  1782. if (ctx)
  1783. internal_memcpy(&signal->ctx, ctx, sizeof(signal->ctx));
  1784. atomic_store(&sctx->have_pending_signals, 1, memory_order_relaxed);
  1785. }
  1786. }
  1787. static void rtl_sighandler(int sig) {
  1788. rtl_generic_sighandler(false, sig, 0, 0);
  1789. }
  1790. static void rtl_sigaction(int sig, my_siginfo_t *info, void *ctx) {
  1791. rtl_generic_sighandler(true, sig, info, ctx);
  1792. }
  1793. TSAN_INTERCEPTOR(int, sigaction, int sig, sigaction_t *act, sigaction_t *old) {
  1794. SCOPED_TSAN_INTERCEPTOR(sigaction, sig, act, old);
  1795. if (old)
  1796. internal_memcpy(old, &sigactions[sig], sizeof(*old));
  1797. if (act == 0)
  1798. return 0;
  1799. internal_memcpy(&sigactions[sig], act, sizeof(*act));
  1800. sigaction_t newact;
  1801. internal_memcpy(&newact, act, sizeof(newact));
  1802. REAL(sigfillset)(&newact.sa_mask);
  1803. if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL) {
  1804. if (newact.sa_flags & SA_SIGINFO)
  1805. newact.sa_sigaction = rtl_sigaction;
  1806. else
  1807. newact.sa_handler = rtl_sighandler;
  1808. }
  1809. ReleaseStore(thr, pc, (uptr)&sigactions[sig]);
  1810. int res = REAL(sigaction)(sig, &newact, 0);
  1811. return res;
  1812. }
  1813. TSAN_INTERCEPTOR(sighandler_t, signal, int sig, sighandler_t h) {
  1814. sigaction_t act;
  1815. act.sa_handler = h;
  1816. REAL(memset)(&act.sa_mask, -1, sizeof(act.sa_mask));
  1817. act.sa_flags = 0;
  1818. sigaction_t old;
  1819. int res = sigaction(sig, &act, &old);
  1820. if (res)
  1821. return SIG_ERR;
  1822. return old.sa_handler;
  1823. }
  1824. TSAN_INTERCEPTOR(int, sigsuspend, const __sanitizer_sigset_t *mask) {
  1825. SCOPED_TSAN_INTERCEPTOR(sigsuspend, mask);
  1826. return REAL(sigsuspend)(mask);
  1827. }
  1828. TSAN_INTERCEPTOR(int, raise, int sig) {
  1829. SCOPED_TSAN_INTERCEPTOR(raise, sig);
  1830. SignalContext *sctx = SigCtx(thr);
  1831. CHECK_NE(sctx, 0);
  1832. int prev = sctx->int_signal_send;
  1833. sctx->int_signal_send = sig;
  1834. int res = REAL(raise)(sig);
  1835. CHECK_EQ(sctx->int_signal_send, sig);
  1836. sctx->int_signal_send = prev;
  1837. return res;
  1838. }
  1839. TSAN_INTERCEPTOR(int, kill, int pid, int sig) {
  1840. SCOPED_TSAN_INTERCEPTOR(kill, pid, sig);
  1841. SignalContext *sctx = SigCtx(thr);
  1842. CHECK_NE(sctx, 0);
  1843. int prev = sctx->int_signal_send;
  1844. if (pid == (int)internal_getpid()) {
  1845. sctx->int_signal_send = sig;
  1846. }
  1847. int res = REAL(kill)(pid, sig);
  1848. if (pid == (int)internal_getpid()) {
  1849. CHECK_EQ(sctx->int_signal_send, sig);
  1850. sctx->int_signal_send = prev;
  1851. }
  1852. return res;
  1853. }
  1854. TSAN_INTERCEPTOR(int, pthread_kill, void *tid, int sig) {
  1855. SCOPED_TSAN_INTERCEPTOR(pthread_kill, tid, sig);
  1856. SignalContext *sctx = SigCtx(thr);
  1857. CHECK_NE(sctx, 0);
  1858. int prev = sctx->int_signal_send;
  1859. if (tid == pthread_self()) {
  1860. sctx->int_signal_send = sig;
  1861. }
  1862. int res = REAL(pthread_kill)(tid, sig);
  1863. if (tid == pthread_self()) {
  1864. CHECK_EQ(sctx->int_signal_send, sig);
  1865. sctx->int_signal_send = prev;
  1866. }
  1867. return res;
  1868. }
  1869. TSAN_INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
  1870. SCOPED_TSAN_INTERCEPTOR(gettimeofday, tv, tz);
  1871. // It's intercepted merely to process pending signals.
  1872. return REAL(gettimeofday)(tv, tz);
  1873. }
  1874. TSAN_INTERCEPTOR(int, getaddrinfo, void *node, void *service,
  1875. void *hints, void *rv) {
  1876. SCOPED_TSAN_INTERCEPTOR(getaddrinfo, node, service, hints, rv);
  1877. // We miss atomic synchronization in getaddrinfo,
  1878. // and can report false race between malloc and free
  1879. // inside of getaddrinfo. So ignore memory accesses.
  1880. ThreadIgnoreBegin(thr, pc);
  1881. int res = REAL(getaddrinfo)(node, service, hints, rv);
  1882. ThreadIgnoreEnd(thr, pc);
  1883. return res;
  1884. }
  1885. TSAN_INTERCEPTOR(int, fork, int fake) {
  1886. if (cur_thread()->in_symbolizer)
  1887. return REAL(fork)(fake);
  1888. SCOPED_INTERCEPTOR_RAW(fork, fake);
  1889. ForkBefore(thr, pc);
  1890. int pid = REAL(fork)(fake);
  1891. if (pid == 0) {
  1892. // child
  1893. ForkChildAfter(thr, pc);
  1894. FdOnFork(thr, pc);
  1895. } else if (pid > 0) {
  1896. // parent
  1897. ForkParentAfter(thr, pc);
  1898. } else {
  1899. // error
  1900. ForkParentAfter(thr, pc);
  1901. }
  1902. return pid;
  1903. }
  1904. TSAN_INTERCEPTOR(int, vfork, int fake) {
  1905. // Some programs (e.g. openjdk) call close for all file descriptors
  1906. // in the child process. Under tsan it leads to false positives, because
  1907. // address space is shared, so the parent process also thinks that
  1908. // the descriptors are closed (while they are actually not).
  1909. // This leads to false positives due to missed synchronization.
  1910. // Strictly saying this is undefined behavior, because vfork child is not
  1911. // allowed to call any functions other than exec/exit. But this is what
  1912. // openjdk does, so we want to handle it.
  1913. // We could disable interceptors in the child process. But it's not possible
  1914. // to simply intercept and wrap vfork, because vfork child is not allowed
  1915. // to return from the function that calls vfork, and that's exactly what
  1916. // we would do. So this would require some assembly trickery as well.
  1917. // Instead we simply turn vfork into fork.
  1918. return WRAP(fork)(fake);
  1919. }
  1920. static int OnExit(ThreadState *thr) {
  1921. int status = Finalize(thr);
  1922. REAL(fflush)(0);
  1923. return status;
  1924. }
  1925. struct TsanInterceptorContext {
  1926. ThreadState *thr;
  1927. const uptr caller_pc;
  1928. const uptr pc;
  1929. };
  1930. static void HandleRecvmsg(ThreadState *thr, uptr pc,
  1931. __sanitizer_msghdr *msg) {
  1932. int fds[64];
  1933. int cnt = ExtractRecvmsgFDs(msg, fds, ARRAY_SIZE(fds));
  1934. for (int i = 0; i < cnt; i++)
  1935. FdEventCreate(thr, pc, fds[i]);
  1936. }
  1937. #include "sanitizer_common/sanitizer_platform_interceptors.h"
  1938. // Causes interceptor recursion (getaddrinfo() and fopen())
  1939. #undef SANITIZER_INTERCEPT_GETADDRINFO
  1940. // There interceptors do not seem to be strictly necessary for tsan.
  1941. // But we see cases where the interceptors consume 70% of execution time.
  1942. // Memory blocks passed to fgetgrent_r are "written to" by tsan several times.
  1943. // First, there is some recursion (getgrnam_r calls fgetgrent_r), and each
  1944. // function "writes to" the buffer. Then, the same memory is "written to"
  1945. // twice, first as buf and then as pwbufp (both of them refer to the same
  1946. // addresses).
  1947. #undef SANITIZER_INTERCEPT_GETPWENT
  1948. #undef SANITIZER_INTERCEPT_GETPWENT_R
  1949. #undef SANITIZER_INTERCEPT_FGETPWENT
  1950. #undef SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
  1951. #undef SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
  1952. #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
  1953. #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
  1954. MemoryAccessRange(((TsanInterceptorContext *)ctx)->thr, \
  1955. ((TsanInterceptorContext *)ctx)->pc, (uptr)ptr, size, \
  1956. true)
  1957. #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
  1958. MemoryAccessRange(((TsanInterceptorContext *) ctx)->thr, \
  1959. ((TsanInterceptorContext *) ctx)->pc, (uptr) ptr, size, \
  1960. false)
  1961. #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
  1962. SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__); \
  1963. TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \
  1964. ctx = (void *)&_ctx; \
  1965. (void) ctx;
  1966. #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, func, ...) \
  1967. SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \
  1968. TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \
  1969. ctx = (void *)&_ctx; \
  1970. (void) ctx;
  1971. #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) \
  1972. Acquire(thr, pc, File2addr(path)); \
  1973. if (file) { \
  1974. int fd = fileno_unlocked(file); \
  1975. if (fd >= 0) FdFileCreate(thr, pc, fd); \
  1976. }
  1977. #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) \
  1978. if (file) { \
  1979. int fd = fileno_unlocked(file); \
  1980. if (fd >= 0) FdClose(thr, pc, fd); \
  1981. }
  1982. #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res) \
  1983. libignore()->OnLibraryLoaded(filename)
  1984. #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() \
  1985. libignore()->OnLibraryUnloaded()
  1986. #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
  1987. FdAcquire(((TsanInterceptorContext *) ctx)->thr, pc, fd)
  1988. #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
  1989. FdRelease(((TsanInterceptorContext *) ctx)->thr, pc, fd)
  1990. #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) \
  1991. FdAccess(((TsanInterceptorContext *) ctx)->thr, pc, fd)
  1992. #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
  1993. FdSocketAccept(((TsanInterceptorContext *) ctx)->thr, pc, fd, newfd)
  1994. #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
  1995. ThreadSetName(((TsanInterceptorContext *) ctx)->thr, name)
  1996. #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
  1997. __tsan::ctx->thread_registry->SetThreadNameByUserId(thread, name)
  1998. #define COMMON_INTERCEPTOR_BLOCK_REAL(name) BLOCK_REAL(name)
  1999. #define COMMON_INTERCEPTOR_ON_EXIT(ctx) \
  2000. OnExit(((TsanInterceptorContext *) ctx)->thr)
  2001. #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) \
  2002. MutexLock(((TsanInterceptorContext *)ctx)->thr, \
  2003. ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
  2004. #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) \
  2005. MutexUnlock(((TsanInterceptorContext *)ctx)->thr, \
  2006. ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
  2007. #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) \
  2008. MutexRepair(((TsanInterceptorContext *)ctx)->thr, \
  2009. ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
  2010. #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) \
  2011. HandleRecvmsg(((TsanInterceptorContext *)ctx)->thr, \
  2012. ((TsanInterceptorContext *)ctx)->pc, msg)
  2013. #include "sanitizer_common/sanitizer_common_interceptors.inc"
  2014. #define TSAN_SYSCALL() \
  2015. ThreadState *thr = cur_thread(); \
  2016. if (thr->ignore_interceptors) \
  2017. return; \
  2018. ScopedSyscall scoped_syscall(thr) \
  2019. /**/
  2020. struct ScopedSyscall {
  2021. ThreadState *thr;
  2022. explicit ScopedSyscall(ThreadState *thr)
  2023. : thr(thr) {
  2024. Initialize(thr);
  2025. }
  2026. ~ScopedSyscall() {
  2027. ProcessPendingSignals(thr);
  2028. }
  2029. };
  2030. static void syscall_access_range(uptr pc, uptr p, uptr s, bool write) {
  2031. TSAN_SYSCALL();
  2032. MemoryAccessRange(thr, pc, p, s, write);
  2033. }
  2034. static void syscall_acquire(uptr pc, uptr addr) {
  2035. TSAN_SYSCALL();
  2036. Acquire(thr, pc, addr);
  2037. DPrintf("syscall_acquire(%p)\n", addr);
  2038. }
  2039. static void syscall_release(uptr pc, uptr addr) {
  2040. TSAN_SYSCALL();
  2041. DPrintf("syscall_release(%p)\n", addr);
  2042. Release(thr, pc, addr);
  2043. }
  2044. static void syscall_fd_close(uptr pc, int fd) {
  2045. TSAN_SYSCALL();
  2046. FdClose(thr, pc, fd);
  2047. }
  2048. static USED void syscall_fd_acquire(uptr pc, int fd) {
  2049. TSAN_SYSCALL();
  2050. FdAcquire(thr, pc, fd);
  2051. DPrintf("syscall_fd_acquire(%p)\n", fd);
  2052. }
  2053. static USED void syscall_fd_release(uptr pc, int fd) {
  2054. TSAN_SYSCALL();
  2055. DPrintf("syscall_fd_release(%p)\n", fd);
  2056. FdRelease(thr, pc, fd);
  2057. }
  2058. static void syscall_pre_fork(uptr pc) {
  2059. TSAN_SYSCALL();
  2060. ForkBefore(thr, pc);
  2061. }
  2062. static void syscall_post_fork(uptr pc, int pid) {
  2063. TSAN_SYSCALL();
  2064. if (pid == 0) {
  2065. // child
  2066. ForkChildAfter(thr, pc);
  2067. FdOnFork(thr, pc);
  2068. } else if (pid > 0) {
  2069. // parent
  2070. ForkParentAfter(thr, pc);
  2071. } else {
  2072. // error
  2073. ForkParentAfter(thr, pc);
  2074. }
  2075. }
  2076. #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) \
  2077. syscall_access_range(GET_CALLER_PC(), (uptr)(p), (uptr)(s), false)
  2078. #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
  2079. syscall_access_range(GET_CALLER_PC(), (uptr)(p), (uptr)(s), true)
  2080. #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
  2081. do { \
  2082. (void)(p); \
  2083. (void)(s); \
  2084. } while (false)
  2085. #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
  2086. do { \
  2087. (void)(p); \
  2088. (void)(s); \
  2089. } while (false)
  2090. #define COMMON_SYSCALL_ACQUIRE(addr) \
  2091. syscall_acquire(GET_CALLER_PC(), (uptr)(addr))
  2092. #define COMMON_SYSCALL_RELEASE(addr) \
  2093. syscall_release(GET_CALLER_PC(), (uptr)(addr))
  2094. #define COMMON_SYSCALL_FD_CLOSE(fd) syscall_fd_close(GET_CALLER_PC(), fd)
  2095. #define COMMON_SYSCALL_FD_ACQUIRE(fd) syscall_fd_acquire(GET_CALLER_PC(), fd)
  2096. #define COMMON_SYSCALL_FD_RELEASE(fd) syscall_fd_release(GET_CALLER_PC(), fd)
  2097. #define COMMON_SYSCALL_PRE_FORK() \
  2098. syscall_pre_fork(GET_CALLER_PC())
  2099. #define COMMON_SYSCALL_POST_FORK(res) \
  2100. syscall_post_fork(GET_CALLER_PC(), res)
  2101. #include "sanitizer_common/sanitizer_common_syscalls.inc"
  2102. namespace __tsan {
  2103. static void finalize(void *arg) {
  2104. ThreadState *thr = cur_thread();
  2105. int status = Finalize(thr);
  2106. // Make sure the output is not lost.
  2107. // Flushing all the streams here may freeze the process if a child thread is
  2108. // performing file stream operations at the same time.
  2109. REAL(fflush)(stdout);
  2110. REAL(fflush)(stderr);
  2111. if (status)
  2112. REAL(_exit)(status);
  2113. }
  2114. static void unreachable() {
  2115. Report("FATAL: ThreadSanitizer: unreachable called\n");
  2116. Die();
  2117. }
  2118. void InitializeInterceptors() {
  2119. // We need to setup it early, because functions like dlsym() can call it.
  2120. REAL(memset) = internal_memset;
  2121. REAL(memcpy) = internal_memcpy;
  2122. REAL(memcmp) = internal_memcmp;
  2123. // Instruct libc malloc to consume less memory.
  2124. #if !SANITIZER_FREEBSD
  2125. mallopt(1, 0); // M_MXFAST
  2126. mallopt(-3, 32*1024); // M_MMAP_THRESHOLD
  2127. #endif
  2128. InitializeCommonInterceptors();
  2129. // We can not use TSAN_INTERCEPT to get setjmp addr,
  2130. // because it does &setjmp and setjmp is not present in some versions of libc.
  2131. using __interception::GetRealFunctionAddress;
  2132. GetRealFunctionAddress("setjmp", (uptr*)&REAL(setjmp), 0, 0);
  2133. GetRealFunctionAddress("_setjmp", (uptr*)&REAL(_setjmp), 0, 0);
  2134. GetRealFunctionAddress("sigsetjmp", (uptr*)&REAL(sigsetjmp), 0, 0);
  2135. GetRealFunctionAddress("__sigsetjmp", (uptr*)&REAL(__sigsetjmp), 0, 0);
  2136. TSAN_INTERCEPT(longjmp);
  2137. TSAN_INTERCEPT(siglongjmp);
  2138. TSAN_INTERCEPT(malloc);
  2139. TSAN_INTERCEPT(__libc_memalign);
  2140. TSAN_INTERCEPT(calloc);
  2141. TSAN_INTERCEPT(realloc);
  2142. TSAN_INTERCEPT(free);
  2143. TSAN_INTERCEPT(cfree);
  2144. TSAN_INTERCEPT(mmap);
  2145. TSAN_MAYBE_INTERCEPT_MMAP64;
  2146. TSAN_INTERCEPT(munmap);
  2147. TSAN_MAYBE_INTERCEPT_MEMALIGN;
  2148. TSAN_INTERCEPT(valloc);
  2149. TSAN_MAYBE_INTERCEPT_PVALLOC;
  2150. TSAN_INTERCEPT(posix_memalign);
  2151. TSAN_INTERCEPT(strlen);
  2152. TSAN_INTERCEPT(memset);
  2153. TSAN_INTERCEPT(memcpy);
  2154. TSAN_INTERCEPT(memmove);
  2155. TSAN_INTERCEPT(memcmp);
  2156. TSAN_INTERCEPT(strchr);
  2157. TSAN_INTERCEPT(strchrnul);
  2158. TSAN_INTERCEPT(strrchr);
  2159. TSAN_INTERCEPT(strcpy); // NOLINT
  2160. TSAN_INTERCEPT(strncpy);
  2161. TSAN_INTERCEPT(strstr);
  2162. TSAN_INTERCEPT(strdup);
  2163. TSAN_INTERCEPT(pthread_create);
  2164. TSAN_INTERCEPT(pthread_join);
  2165. TSAN_INTERCEPT(pthread_detach);
  2166. TSAN_INTERCEPT_VER(pthread_cond_init, "GLIBC_2.3.2");
  2167. TSAN_INTERCEPT_VER(pthread_cond_signal, "GLIBC_2.3.2");
  2168. TSAN_INTERCEPT_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
  2169. TSAN_INTERCEPT_VER(pthread_cond_wait, "GLIBC_2.3.2");
  2170. TSAN_INTERCEPT_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
  2171. TSAN_INTERCEPT_VER(pthread_cond_destroy, "GLIBC_2.3.2");
  2172. TSAN_INTERCEPT(pthread_mutex_init);
  2173. TSAN_INTERCEPT(pthread_mutex_destroy);
  2174. TSAN_INTERCEPT(pthread_mutex_trylock);
  2175. TSAN_INTERCEPT(pthread_mutex_timedlock);
  2176. TSAN_INTERCEPT(pthread_spin_init);
  2177. TSAN_INTERCEPT(pthread_spin_destroy);
  2178. TSAN_INTERCEPT(pthread_spin_lock);
  2179. TSAN_INTERCEPT(pthread_spin_trylock);
  2180. TSAN_INTERCEPT(pthread_spin_unlock);
  2181. TSAN_INTERCEPT(pthread_rwlock_init);
  2182. TSAN_INTERCEPT(pthread_rwlock_destroy);
  2183. TSAN_INTERCEPT(pthread_rwlock_rdlock);
  2184. TSAN_INTERCEPT(pthread_rwlock_tryrdlock);
  2185. TSAN_INTERCEPT(pthread_rwlock_timedrdlock);
  2186. TSAN_INTERCEPT(pthread_rwlock_wrlock);
  2187. TSAN_INTERCEPT(pthread_rwlock_trywrlock);
  2188. TSAN_INTERCEPT(pthread_rwlock_timedwrlock);
  2189. TSAN_INTERCEPT(pthread_rwlock_unlock);
  2190. TSAN_INTERCEPT(pthread_barrier_init);
  2191. TSAN_INTERCEPT(pthread_barrier_destroy);
  2192. TSAN_INTERCEPT(pthread_barrier_wait);
  2193. TSAN_INTERCEPT(pthread_once);
  2194. TSAN_INTERCEPT(sem_init);
  2195. TSAN_INTERCEPT(sem_destroy);
  2196. TSAN_INTERCEPT(sem_wait);
  2197. TSAN_INTERCEPT(sem_trywait);
  2198. TSAN_INTERCEPT(sem_timedwait);
  2199. TSAN_INTERCEPT(sem_post);
  2200. TSAN_INTERCEPT(sem_getvalue);
  2201. TSAN_INTERCEPT(stat);
  2202. TSAN_MAYBE_INTERCEPT___XSTAT;
  2203. TSAN_MAYBE_INTERCEPT_STAT64;
  2204. TSAN_MAYBE_INTERCEPT___XSTAT64;
  2205. TSAN_INTERCEPT(lstat);
  2206. TSAN_MAYBE_INTERCEPT___LXSTAT;
  2207. TSAN_MAYBE_INTERCEPT_LSTAT64;
  2208. TSAN_MAYBE_INTERCEPT___LXSTAT64;
  2209. TSAN_INTERCEPT(fstat);
  2210. TSAN_MAYBE_INTERCEPT___FXSTAT;
  2211. TSAN_MAYBE_INTERCEPT_FSTAT64;
  2212. TSAN_MAYBE_INTERCEPT___FXSTAT64;
  2213. TSAN_INTERCEPT(open);
  2214. TSAN_MAYBE_INTERCEPT_OPEN64;
  2215. TSAN_INTERCEPT(creat);
  2216. TSAN_MAYBE_INTERCEPT_CREAT64;
  2217. TSAN_INTERCEPT(dup);
  2218. TSAN_INTERCEPT(dup2);
  2219. TSAN_INTERCEPT(dup3);
  2220. TSAN_MAYBE_INTERCEPT_EVENTFD;
  2221. TSAN_MAYBE_INTERCEPT_SIGNALFD;
  2222. TSAN_MAYBE_INTERCEPT_INOTIFY_INIT;
  2223. TSAN_MAYBE_INTERCEPT_INOTIFY_INIT1;
  2224. TSAN_INTERCEPT(socket);
  2225. TSAN_INTERCEPT(socketpair);
  2226. TSAN_INTERCEPT(connect);
  2227. TSAN_INTERCEPT(bind);
  2228. TSAN_INTERCEPT(listen);
  2229. TSAN_MAYBE_INTERCEPT_EPOLL_CREATE;
  2230. TSAN_MAYBE_INTERCEPT_EPOLL_CREATE1;
  2231. TSAN_INTERCEPT(close);
  2232. TSAN_MAYBE_INTERCEPT___CLOSE;
  2233. TSAN_MAYBE_INTERCEPT___RES_ICLOSE;
  2234. TSAN_INTERCEPT(pipe);
  2235. TSAN_INTERCEPT(pipe2);
  2236. TSAN_INTERCEPT(send);
  2237. TSAN_INTERCEPT(sendmsg);
  2238. TSAN_INTERCEPT(recv);
  2239. TSAN_INTERCEPT(unlink);
  2240. TSAN_INTERCEPT(tmpfile);
  2241. TSAN_MAYBE_INTERCEPT_TMPFILE64;
  2242. TSAN_INTERCEPT(fread);
  2243. TSAN_INTERCEPT(fwrite);
  2244. TSAN_INTERCEPT(abort);
  2245. TSAN_INTERCEPT(puts);
  2246. TSAN_INTERCEPT(rmdir);
  2247. TSAN_INTERCEPT(opendir);
  2248. TSAN_MAYBE_INTERCEPT_EPOLL_CTL;
  2249. TSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
  2250. TSAN_INTERCEPT(sigaction);
  2251. TSAN_INTERCEPT(signal);
  2252. TSAN_INTERCEPT(sigsuspend);
  2253. TSAN_INTERCEPT(raise);
  2254. TSAN_INTERCEPT(kill);
  2255. TSAN_INTERCEPT(pthread_kill);
  2256. TSAN_INTERCEPT(sleep);
  2257. TSAN_INTERCEPT(usleep);
  2258. TSAN_INTERCEPT(nanosleep);
  2259. TSAN_INTERCEPT(gettimeofday);
  2260. TSAN_INTERCEPT(getaddrinfo);
  2261. TSAN_INTERCEPT(fork);
  2262. TSAN_INTERCEPT(vfork);
  2263. TSAN_INTERCEPT(on_exit);
  2264. TSAN_INTERCEPT(__cxa_atexit);
  2265. TSAN_INTERCEPT(_exit);
  2266. // Need to setup it, because interceptors check that the function is resolved.
  2267. // But atexit is emitted directly into the module, so can't be resolved.
  2268. REAL(atexit) = (int(*)(void(*)()))unreachable;
  2269. if (REAL(__cxa_atexit)(&finalize, 0, 0)) {
  2270. Printf("ThreadSanitizer: failed to setup atexit callback\n");
  2271. Die();
  2272. }
  2273. if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
  2274. Printf("ThreadSanitizer: failed to create thread key\n");
  2275. Die();
  2276. }
  2277. FdInit();
  2278. }
  2279. void *internal_start_thread(void(*func)(void *arg), void *arg) {
  2280. // Start the thread with signals blocked, otherwise it can steal user signals.
  2281. __sanitizer_sigset_t set, old;
  2282. internal_sigfillset(&set);
  2283. internal_sigprocmask(SIG_SETMASK, &set, &old);
  2284. void *th;
  2285. REAL(pthread_create)(&th, 0, (void*(*)(void *arg))func, arg);
  2286. internal_sigprocmask(SIG_SETMASK, &old, 0);
  2287. return th;
  2288. }
  2289. void internal_join_thread(void *th) {
  2290. REAL(pthread_join)(th, 0);
  2291. }
  2292. } // namespace __tsan