msan_interceptors.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  1. //===-- msan_interceptors.cpp ---------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of MemorySanitizer.
  10. //
  11. // Interceptors for standard library functions.
  12. //
  13. // FIXME: move as many interceptors as possible into
  14. // sanitizer_common/sanitizer_common_interceptors.h
  15. //===----------------------------------------------------------------------===//
  16. #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS
  17. #include "interception/interception.h"
  18. #include "msan.h"
  19. #include "msan_chained_origin_depot.h"
  20. #include "msan_dl.h"
  21. #include "msan_origin.h"
  22. #include "msan_poisoning.h"
  23. #include "msan_report.h"
  24. #include "msan_thread.h"
  25. #include "sanitizer_common/sanitizer_allocator.h"
  26. #include "sanitizer_common/sanitizer_allocator_dlsym.h"
  27. #include "sanitizer_common/sanitizer_allocator_interface.h"
  28. #include "sanitizer_common/sanitizer_atomic.h"
  29. #include "sanitizer_common/sanitizer_common.h"
  30. #include "sanitizer_common/sanitizer_errno.h"
  31. #include "sanitizer_common/sanitizer_errno_codes.h"
  32. #include "sanitizer_common/sanitizer_glibc_version.h"
  33. #include "sanitizer_common/sanitizer_libc.h"
  34. #include "sanitizer_common/sanitizer_linux.h"
  35. #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
  36. #include "sanitizer_common/sanitizer_platform_limits_posix.h"
  37. #include "sanitizer_common/sanitizer_stackdepot.h"
  38. #include "sanitizer_common/sanitizer_tls_get_addr.h"
  39. #include "sanitizer_common/sanitizer_vector.h"
  40. #if SANITIZER_NETBSD
  41. #define fstat __fstat50
  42. #define gettimeofday __gettimeofday50
  43. #define getrusage __getrusage50
  44. #define tzset __tzset50
  45. #endif
  46. #include <stdarg.h>
  47. // ACHTUNG! No other system header includes in this file.
  48. // Ideally, we should get rid of stdarg.h as well.
  49. using namespace __msan;
  50. using __sanitizer::memory_order;
  51. using __sanitizer::atomic_load;
  52. using __sanitizer::atomic_store;
  53. using __sanitizer::atomic_uintptr_t;
  54. DECLARE_REAL(SIZE_T, strlen, const char *s)
  55. DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
  56. DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n)
  57. DECLARE_REAL(void *, memset, void *dest, int c, uptr n)
  58. // True if this is a nested interceptor.
  59. static THREADLOCAL int in_interceptor_scope;
  60. void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; }
  61. void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; }
  62. struct InterceptorScope {
  63. InterceptorScope() { ++in_interceptor_scope; }
  64. ~InterceptorScope() { --in_interceptor_scope; }
  65. };
  66. bool IsInInterceptorScope() {
  67. return in_interceptor_scope;
  68. }
  69. struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
  70. static bool UseImpl() { return !msan_inited; }
  71. };
  72. #define ENSURE_MSAN_INITED() do { \
  73. CHECK(!msan_init_is_running); \
  74. if (!msan_inited) { \
  75. __msan_init(); \
  76. } \
  77. } while (0)
  78. // Check that [x, x+n) range is unpoisoned.
  79. #define CHECK_UNPOISONED_0(x, n) \
  80. do { \
  81. sptr __offset = __msan_test_shadow(x, n); \
  82. if (__msan::IsInSymbolizerOrUnwider()) \
  83. break; \
  84. if (__offset >= 0 && __msan::flags()->report_umrs) { \
  85. GET_CALLER_PC_BP; \
  86. ReportUMRInsideAddressRange(__func__, x, n, __offset); \
  87. __msan::PrintWarningWithOrigin( \
  88. pc, bp, __msan_get_origin((const char *)x + __offset)); \
  89. if (__msan::flags()->halt_on_error) { \
  90. Printf("Exiting\n"); \
  91. Die(); \
  92. } \
  93. } \
  94. } while (0)
  95. // Check that [x, x+n) range is unpoisoned unless we are in a nested
  96. // interceptor.
  97. #define CHECK_UNPOISONED(x, n) \
  98. do { \
  99. if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
  100. } while (0)
  101. #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \
  102. CHECK_UNPOISONED((x), \
  103. common_flags()->strict_string_checks ? (len) + 1 : (n) )
  104. #define CHECK_UNPOISONED_STRING(x, n) \
  105. CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n))
  106. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  107. INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
  108. void *file) {
  109. ENSURE_MSAN_INITED();
  110. SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
  111. if (res > 0)
  112. __msan_unpoison(ptr, res *size);
  113. return res;
  114. }
  115. #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked)
  116. #else
  117. #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED
  118. #endif
  119. #if !SANITIZER_NETBSD
  120. INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
  121. return (char *)__msan_memcpy(dest, src, n) + n;
  122. }
  123. #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy)
  124. #else
  125. #define MSAN_MAYBE_INTERCEPT_MEMPCPY
  126. #endif
  127. INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) {
  128. ENSURE_MSAN_INITED();
  129. void *res = REAL(memccpy)(dest, src, c, n);
  130. CHECK(!res || (res >= dest && res <= (char *)dest + n));
  131. SIZE_T sz = res ? (char *)res - (char *)dest : n;
  132. CHECK_UNPOISONED(src, sz);
  133. __msan_unpoison(dest, sz);
  134. return res;
  135. }
  136. INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
  137. return __msan_memmove(dest, src, n);
  138. }
  139. INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
  140. GET_MALLOC_STACK_TRACE;
  141. CHECK_NE(memptr, 0);
  142. int res = msan_posix_memalign(memptr, alignment, size, &stack);
  143. if (!res)
  144. __msan_unpoison(memptr, sizeof(*memptr));
  145. return res;
  146. }
  147. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  148. INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
  149. GET_MALLOC_STACK_TRACE;
  150. return msan_memalign(alignment, size, &stack);
  151. }
  152. #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
  153. #else
  154. #define MSAN_MAYBE_INTERCEPT_MEMALIGN
  155. #endif
  156. INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
  157. GET_MALLOC_STACK_TRACE;
  158. return msan_aligned_alloc(alignment, size, &stack);
  159. }
  160. #if !SANITIZER_NETBSD
  161. INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
  162. GET_MALLOC_STACK_TRACE;
  163. void *ptr = msan_memalign(alignment, size, &stack);
  164. if (ptr)
  165. DTLS_on_libc_memalign(ptr, size);
  166. return ptr;
  167. }
  168. #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
  169. #else
  170. #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
  171. #endif
  172. INTERCEPTOR(void *, valloc, SIZE_T size) {
  173. GET_MALLOC_STACK_TRACE;
  174. return msan_valloc(size, &stack);
  175. }
  176. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  177. INTERCEPTOR(void *, pvalloc, SIZE_T size) {
  178. GET_MALLOC_STACK_TRACE;
  179. return msan_pvalloc(size, &stack);
  180. }
  181. #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
  182. #else
  183. #define MSAN_MAYBE_INTERCEPT_PVALLOC
  184. #endif
  185. INTERCEPTOR(void, free, void *ptr) {
  186. if (UNLIKELY(!ptr))
  187. return;
  188. if (DlsymAlloc::PointerIsMine(ptr))
  189. return DlsymAlloc::Free(ptr);
  190. GET_MALLOC_STACK_TRACE;
  191. MsanDeallocate(&stack, ptr);
  192. }
  193. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  194. INTERCEPTOR(void, cfree, void *ptr) {
  195. if (UNLIKELY(!ptr))
  196. return;
  197. if (DlsymAlloc::PointerIsMine(ptr))
  198. return DlsymAlloc::Free(ptr);
  199. GET_MALLOC_STACK_TRACE;
  200. MsanDeallocate(&stack, ptr);
  201. }
  202. # define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
  203. #else
  204. #define MSAN_MAYBE_INTERCEPT_CFREE
  205. #endif
  206. #if !SANITIZER_NETBSD
  207. INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
  208. return __sanitizer_get_allocated_size(ptr);
  209. }
  210. #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
  211. INTERCEPT_FUNCTION(malloc_usable_size)
  212. #else
  213. #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
  214. #endif
  215. #if (!SANITIZER_FREEBSD && !SANITIZER_NETBSD) || __GLIBC_PREREQ(2, 33)
  216. template <class T>
  217. static NOINLINE void clear_mallinfo(T *sret) {
  218. ENSURE_MSAN_INITED();
  219. internal_memset(sret, 0, sizeof(*sret));
  220. __msan_unpoison(sret, sizeof(*sret));
  221. }
  222. #endif
  223. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  224. // Interceptors use NRVO and assume that sret will be pre-allocated in
  225. // caller frame.
  226. INTERCEPTOR(__sanitizer_struct_mallinfo, mallinfo) {
  227. __sanitizer_struct_mallinfo sret;
  228. clear_mallinfo(&sret);
  229. return sret;
  230. }
  231. # define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
  232. #else
  233. # define MSAN_MAYBE_INTERCEPT_MALLINFO
  234. #endif
  235. #if __GLIBC_PREREQ(2, 33)
  236. INTERCEPTOR(__sanitizer_struct_mallinfo2, mallinfo2) {
  237. __sanitizer_struct_mallinfo2 sret;
  238. clear_mallinfo(&sret);
  239. return sret;
  240. }
  241. # define MSAN_MAYBE_INTERCEPT_MALLINFO2 INTERCEPT_FUNCTION(mallinfo2)
  242. #else
  243. # define MSAN_MAYBE_INTERCEPT_MALLINFO2
  244. #endif
  245. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  246. INTERCEPTOR(int, mallopt, int cmd, int value) {
  247. return 0;
  248. }
  249. #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
  250. #else
  251. #define MSAN_MAYBE_INTERCEPT_MALLOPT
  252. #endif
  253. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  254. INTERCEPTOR(void, malloc_stats, void) {
  255. // FIXME: implement, but don't call REAL(malloc_stats)!
  256. }
  257. #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats)
  258. #else
  259. #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS
  260. #endif
  261. INTERCEPTOR(char *, strcpy, char *dest, const char *src) {
  262. ENSURE_MSAN_INITED();
  263. GET_STORE_STACK_TRACE;
  264. SIZE_T n = internal_strlen(src);
  265. CHECK_UNPOISONED_STRING(src + n, 0);
  266. char *res = REAL(strcpy)(dest, src);
  267. CopyShadowAndOrigin(dest, src, n + 1, &stack);
  268. return res;
  269. }
  270. INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) {
  271. ENSURE_MSAN_INITED();
  272. GET_STORE_STACK_TRACE;
  273. SIZE_T copy_size = internal_strnlen(src, n);
  274. if (copy_size < n)
  275. copy_size++; // trailing \0
  276. char *res = REAL(strncpy)(dest, src, n);
  277. CopyShadowAndOrigin(dest, src, copy_size, &stack);
  278. __msan_unpoison(dest + copy_size, n - copy_size);
  279. return res;
  280. }
  281. #if !SANITIZER_NETBSD
  282. INTERCEPTOR(char *, stpcpy, char *dest, const char *src) {
  283. ENSURE_MSAN_INITED();
  284. GET_STORE_STACK_TRACE;
  285. SIZE_T n = internal_strlen(src);
  286. CHECK_UNPOISONED_STRING(src + n, 0);
  287. char *res = REAL(stpcpy)(dest, src);
  288. CopyShadowAndOrigin(dest, src, n + 1, &stack);
  289. return res;
  290. }
  291. INTERCEPTOR(char *, stpncpy, char *dest, const char *src, SIZE_T n) {
  292. ENSURE_MSAN_INITED();
  293. GET_STORE_STACK_TRACE;
  294. SIZE_T copy_size = Min(n, internal_strnlen(src, n) + 1);
  295. char *res = REAL(stpncpy)(dest, src, n);
  296. CopyShadowAndOrigin(dest, src, copy_size, &stack);
  297. __msan_unpoison(dest + copy_size, n - copy_size);
  298. return res;
  299. }
  300. # define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy)
  301. # define MSAN_MAYBE_INTERCEPT_STPNCPY INTERCEPT_FUNCTION(stpncpy)
  302. #else
  303. #define MSAN_MAYBE_INTERCEPT_STPCPY
  304. # define MSAN_MAYBE_INTERCEPT_STPNCPY
  305. #endif
  306. INTERCEPTOR(char *, strdup, char *src) {
  307. ENSURE_MSAN_INITED();
  308. GET_STORE_STACK_TRACE;
  309. // On FreeBSD strdup() leverages strlen().
  310. InterceptorScope interceptor_scope;
  311. SIZE_T n = internal_strlen(src);
  312. CHECK_UNPOISONED_STRING(src + n, 0);
  313. char *res = REAL(strdup)(src);
  314. CopyShadowAndOrigin(res, src, n + 1, &stack);
  315. return res;
  316. }
  317. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  318. INTERCEPTOR(char *, __strdup, char *src) {
  319. ENSURE_MSAN_INITED();
  320. GET_STORE_STACK_TRACE;
  321. SIZE_T n = internal_strlen(src);
  322. CHECK_UNPOISONED_STRING(src + n, 0);
  323. char *res = REAL(__strdup)(src);
  324. CopyShadowAndOrigin(res, src, n + 1, &stack);
  325. return res;
  326. }
  327. #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup)
  328. #else
  329. #define MSAN_MAYBE_INTERCEPT___STRDUP
  330. #endif
  331. #if !SANITIZER_NETBSD
  332. INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
  333. ENSURE_MSAN_INITED();
  334. char *res = REAL(gcvt)(number, ndigit, buf);
  335. SIZE_T n = internal_strlen(buf);
  336. __msan_unpoison(buf, n + 1);
  337. return res;
  338. }
  339. #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt)
  340. #else
  341. #define MSAN_MAYBE_INTERCEPT_GCVT
  342. #endif
  343. INTERCEPTOR(char *, strcat, char *dest, const char *src) {
  344. ENSURE_MSAN_INITED();
  345. GET_STORE_STACK_TRACE;
  346. SIZE_T src_size = internal_strlen(src);
  347. SIZE_T dest_size = internal_strlen(dest);
  348. CHECK_UNPOISONED_STRING(src + src_size, 0);
  349. CHECK_UNPOISONED_STRING(dest + dest_size, 0);
  350. char *res = REAL(strcat)(dest, src);
  351. CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack);
  352. return res;
  353. }
  354. INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) {
  355. ENSURE_MSAN_INITED();
  356. GET_STORE_STACK_TRACE;
  357. SIZE_T dest_size = internal_strlen(dest);
  358. SIZE_T copy_size = internal_strnlen(src, n);
  359. CHECK_UNPOISONED_STRING(dest + dest_size, 0);
  360. char *res = REAL(strncat)(dest, src, n);
  361. CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack);
  362. __msan_unpoison(dest + dest_size + copy_size, 1); // \0
  363. return res;
  364. }
  365. // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to
  366. // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO.
  367. #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \
  368. ENSURE_MSAN_INITED(); \
  369. ret_type res = REAL(func)(__VA_ARGS__); \
  370. __msan_unpoison(endptr, sizeof(*endptr)); \
  371. return res;
  372. // On s390x, long double return values are passed via implicit reference,
  373. // which needs to be unpoisoned. We make the implicit pointer explicit.
  374. #define INTERCEPTOR_STRTO_SRET_BODY(func, sret, ...) \
  375. ENSURE_MSAN_INITED(); \
  376. REAL(func)(sret, __VA_ARGS__); \
  377. __msan_unpoison(sret, sizeof(*sret)); \
  378. __msan_unpoison(endptr, sizeof(*endptr));
  379. #define INTERCEPTOR_STRTO(ret_type, func, char_type) \
  380. INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \
  381. INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \
  382. }
  383. #define INTERCEPTOR_STRTO_SRET(ret_type, func, char_type) \
  384. INTERCEPTOR(void, func, ret_type *sret, const char_type *nptr, \
  385. char_type **endptr) { \
  386. INTERCEPTOR_STRTO_SRET_BODY(func, sret, nptr, endptr); \
  387. }
  388. #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
  389. INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
  390. int base) { \
  391. INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \
  392. }
  393. #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \
  394. INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
  395. void *loc) { \
  396. INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \
  397. }
  398. #define INTERCEPTOR_STRTO_SRET_LOC(ret_type, func, char_type) \
  399. INTERCEPTOR(void, func, ret_type *sret, const char_type *nptr, \
  400. char_type **endptr, void *loc) { \
  401. INTERCEPTOR_STRTO_SRET_BODY(func, sret, nptr, endptr, loc); \
  402. }
  403. #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \
  404. INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
  405. int base, void *loc) { \
  406. INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \
  407. }
  408. #if SANITIZER_NETBSD
  409. #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
  410. INTERCEPTOR_STRTO(ret_type, func, char_type) \
  411. INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type)
  412. #define INTERCEPTORS_STRTO_SRET(ret_type, func, char_type) \
  413. INTERCEPTOR_STRTO_SRET(ret_type, func, char_type) \
  414. INTERCEPTOR_STRTO_SRET_LOC(ret_type, func##_l, char_type)
  415. #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
  416. INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
  417. INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type)
  418. #else
  419. #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
  420. INTERCEPTOR_STRTO(ret_type, func, char_type) \
  421. INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \
  422. INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \
  423. INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type)
  424. #define INTERCEPTORS_STRTO_SRET(ret_type, func, char_type) \
  425. INTERCEPTOR_STRTO_SRET(ret_type, func, char_type) \
  426. INTERCEPTOR_STRTO_SRET_LOC(ret_type, func##_l, char_type) \
  427. INTERCEPTOR_STRTO_SRET_LOC(ret_type, __##func##_l, char_type) \
  428. INTERCEPTOR_STRTO_SRET_LOC(ret_type, __##func##_internal, char_type)
  429. #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
  430. INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
  431. INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \
  432. INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \
  433. INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type)
  434. #endif
  435. INTERCEPTORS_STRTO(double, strtod, char)
  436. INTERCEPTORS_STRTO(float, strtof, char)
  437. #ifdef __s390x__
  438. INTERCEPTORS_STRTO_SRET(long double, strtold, char)
  439. #else
  440. INTERCEPTORS_STRTO(long double, strtold, char)
  441. #endif
  442. INTERCEPTORS_STRTO_BASE(long, strtol, char)
  443. INTERCEPTORS_STRTO_BASE(long long, strtoll, char)
  444. INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char)
  445. INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char)
  446. INTERCEPTORS_STRTO_BASE(u64, strtouq, char)
  447. INTERCEPTORS_STRTO(double, wcstod, wchar_t)
  448. INTERCEPTORS_STRTO(float, wcstof, wchar_t)
  449. #ifdef __s390x__
  450. INTERCEPTORS_STRTO_SRET(long double, wcstold, wchar_t)
  451. #else
  452. INTERCEPTORS_STRTO(long double, wcstold, wchar_t)
  453. #endif
  454. INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t)
  455. INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t)
  456. INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t)
  457. INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t)
  458. #if SANITIZER_GLIBC
  459. INTERCEPTORS_STRTO(double, __isoc23_strtod, char)
  460. INTERCEPTORS_STRTO(float, __isoc23_strtof, char)
  461. #ifdef __s390x__
  462. INTERCEPTORS_STRTO_SRET(long double, __isoc23_strtold, char)
  463. #else
  464. INTERCEPTORS_STRTO(long double, __isoc23_strtold, char)
  465. #endif
  466. INTERCEPTORS_STRTO_BASE(long, __isoc23_strtol, char)
  467. INTERCEPTORS_STRTO_BASE(long long, __isoc23_strtoll, char)
  468. INTERCEPTORS_STRTO_BASE(unsigned long, __isoc23_strtoul, char)
  469. INTERCEPTORS_STRTO_BASE(unsigned long long, __isoc23_strtoull, char)
  470. INTERCEPTORS_STRTO_BASE(u64, __isoc23_strtouq, char)
  471. INTERCEPTORS_STRTO(double, __isoc23_wcstod, wchar_t)
  472. INTERCEPTORS_STRTO(float, __isoc23_wcstof, wchar_t)
  473. #ifdef __s390x__
  474. INTERCEPTORS_STRTO_SRET(long double, __isoc23_wcstold, wchar_t)
  475. #else
  476. INTERCEPTORS_STRTO(long double, __isoc23_wcstold, wchar_t)
  477. #endif
  478. INTERCEPTORS_STRTO_BASE(long, __isoc23_wcstol, wchar_t)
  479. INTERCEPTORS_STRTO_BASE(long long, __isoc23_wcstoll, wchar_t)
  480. INTERCEPTORS_STRTO_BASE(unsigned long, __isoc23_wcstoul, wchar_t)
  481. INTERCEPTORS_STRTO_BASE(unsigned long long, __isoc23_wcstoull, wchar_t)
  482. #endif
  483. #if SANITIZER_NETBSD
  484. #define INTERCEPT_STRTO(func) \
  485. INTERCEPT_FUNCTION(func); \
  486. INTERCEPT_FUNCTION(func##_l);
  487. #else
  488. #define INTERCEPT_STRTO(func) \
  489. INTERCEPT_FUNCTION(func); \
  490. INTERCEPT_FUNCTION(func##_l); \
  491. INTERCEPT_FUNCTION(__##func##_l); \
  492. INTERCEPT_FUNCTION(__##func##_internal);
  493. #define INTERCEPT_STRTO_VER(func, ver) \
  494. INTERCEPT_FUNCTION_VER(func, ver); \
  495. INTERCEPT_FUNCTION_VER(func##_l, ver); \
  496. INTERCEPT_FUNCTION_VER(__##func##_l, ver); \
  497. INTERCEPT_FUNCTION_VER(__##func##_internal, ver);
  498. #endif
  499. // FIXME: support *wprintf in common format interceptors.
  500. INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
  501. ENSURE_MSAN_INITED();
  502. int res = REAL(vswprintf)(str, size, format, ap);
  503. if (res >= 0) {
  504. __msan_unpoison(str, 4 * (res + 1));
  505. }
  506. return res;
  507. }
  508. INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
  509. ENSURE_MSAN_INITED();
  510. va_list ap;
  511. va_start(ap, format);
  512. int res = vswprintf(str, size, format, ap);
  513. va_end(ap);
  514. return res;
  515. }
  516. #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \
  517. ENSURE_MSAN_INITED(); \
  518. InterceptorScope interceptor_scope; \
  519. ret_type res = REAL(func)(s, __VA_ARGS__); \
  520. if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \
  521. return res;
  522. INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
  523. __sanitizer_tm *tm) {
  524. INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm);
  525. }
  526. INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format,
  527. __sanitizer_tm *tm, void *loc) {
  528. INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc);
  529. }
  530. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  531. INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format,
  532. __sanitizer_tm *tm, void *loc) {
  533. INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm,
  534. loc);
  535. }
  536. #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l)
  537. #else
  538. #define MSAN_MAYBE_INTERCEPT___STRFTIME_L
  539. #endif
  540. INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format,
  541. __sanitizer_tm *tm) {
  542. INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm);
  543. }
  544. INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
  545. __sanitizer_tm *tm, void *loc) {
  546. INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm,
  547. loc);
  548. }
  549. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  550. INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
  551. __sanitizer_tm *tm, void *loc) {
  552. INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm,
  553. loc);
  554. }
  555. #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l)
  556. #else
  557. #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L
  558. #endif
  559. INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
  560. ENSURE_MSAN_INITED();
  561. int res = REAL(mbtowc)(dest, src, n);
  562. if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
  563. return res;
  564. }
  565. INTERCEPTOR(SIZE_T, mbrtowc, wchar_t *dest, const char *src, SIZE_T n,
  566. void *ps) {
  567. ENSURE_MSAN_INITED();
  568. SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
  569. if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
  570. return res;
  571. }
  572. // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
  573. INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
  574. ENSURE_MSAN_INITED();
  575. GET_STORE_STACK_TRACE;
  576. wchar_t *res = REAL(wmemcpy)(dest, src, n);
  577. CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
  578. return res;
  579. }
  580. #if !SANITIZER_NETBSD
  581. INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
  582. ENSURE_MSAN_INITED();
  583. GET_STORE_STACK_TRACE;
  584. wchar_t *res = REAL(wmempcpy)(dest, src, n);
  585. CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
  586. return res;
  587. }
  588. #define MSAN_MAYBE_INTERCEPT_WMEMPCPY INTERCEPT_FUNCTION(wmempcpy)
  589. #else
  590. #define MSAN_MAYBE_INTERCEPT_WMEMPCPY
  591. #endif
  592. INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
  593. CHECK(MEM_IS_APP(s));
  594. ENSURE_MSAN_INITED();
  595. wchar_t *res = REAL(wmemset)(s, c, n);
  596. __msan_unpoison(s, n * sizeof(wchar_t));
  597. return res;
  598. }
  599. INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
  600. ENSURE_MSAN_INITED();
  601. GET_STORE_STACK_TRACE;
  602. wchar_t *res = REAL(wmemmove)(dest, src, n);
  603. MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
  604. return res;
  605. }
  606. INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
  607. ENSURE_MSAN_INITED();
  608. int res = REAL(wcscmp)(s1, s2);
  609. return res;
  610. }
  611. INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
  612. ENSURE_MSAN_INITED();
  613. int res = REAL(gettimeofday)(tv, tz);
  614. if (tv)
  615. __msan_unpoison(tv, 16);
  616. if (tz)
  617. __msan_unpoison(tz, 8);
  618. return res;
  619. }
  620. #if !SANITIZER_NETBSD
  621. INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
  622. ENSURE_MSAN_INITED();
  623. char *res = REAL(fcvt)(x, a, b, c);
  624. __msan_unpoison(b, sizeof(*b));
  625. __msan_unpoison(c, sizeof(*c));
  626. if (res)
  627. __msan_unpoison(res, internal_strlen(res) + 1);
  628. return res;
  629. }
  630. #define MSAN_MAYBE_INTERCEPT_FCVT INTERCEPT_FUNCTION(fcvt)
  631. #else
  632. #define MSAN_MAYBE_INTERCEPT_FCVT
  633. #endif
  634. INTERCEPTOR(char *, getenv, char *name) {
  635. if (msan_init_is_running)
  636. return REAL(getenv)(name);
  637. ENSURE_MSAN_INITED();
  638. char *res = REAL(getenv)(name);
  639. if (res)
  640. __msan_unpoison(res, internal_strlen(res) + 1);
  641. return res;
  642. }
  643. extern char **environ;
  644. static void UnpoisonEnviron() {
  645. char **envp = environ;
  646. for (; *envp; ++envp) {
  647. __msan_unpoison(envp, sizeof(*envp));
  648. __msan_unpoison(*envp, internal_strlen(*envp) + 1);
  649. }
  650. // Trailing NULL pointer.
  651. __msan_unpoison(envp, sizeof(*envp));
  652. }
  653. INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
  654. ENSURE_MSAN_INITED();
  655. CHECK_UNPOISONED_STRING(name, 0);
  656. int res = REAL(setenv)(name, value, overwrite);
  657. if (!res) UnpoisonEnviron();
  658. return res;
  659. }
  660. INTERCEPTOR(int, putenv, char *string) {
  661. ENSURE_MSAN_INITED();
  662. int res = REAL(putenv)(string);
  663. if (!res) UnpoisonEnviron();
  664. return res;
  665. }
  666. #define SANITIZER_STAT_LINUX (SANITIZER_LINUX && __GLIBC_PREREQ(2, 33))
  667. #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX
  668. INTERCEPTOR(int, fstat, int fd, void *buf) {
  669. ENSURE_MSAN_INITED();
  670. int res = REAL(fstat)(fd, buf);
  671. if (!res)
  672. __msan_unpoison(buf, __sanitizer::struct_stat_sz);
  673. return res;
  674. }
  675. # define MSAN_MAYBE_INTERCEPT_FSTAT MSAN_INTERCEPT_FUNC(fstat)
  676. #else
  677. #define MSAN_MAYBE_INTERCEPT_FSTAT
  678. #endif
  679. #if SANITIZER_STAT_LINUX
  680. INTERCEPTOR(int, fstat64, int fd, void *buf) {
  681. ENSURE_MSAN_INITED();
  682. int res = REAL(fstat64)(fd, buf);
  683. if (!res)
  684. __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
  685. return res;
  686. }
  687. # define MSAN_MAYBE_INTERCEPT_FSTAT64 MSAN_INTERCEPT_FUNC(fstat64)
  688. #else
  689. # define MSAN_MAYBE_INTERCEPT_FSTAT64
  690. #endif
  691. #if SANITIZER_GLIBC
  692. INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
  693. ENSURE_MSAN_INITED();
  694. int res = REAL(__fxstat)(magic, fd, buf);
  695. if (!res)
  696. __msan_unpoison(buf, __sanitizer::struct_stat_sz);
  697. return res;
  698. }
  699. # define MSAN_MAYBE_INTERCEPT___FXSTAT MSAN_INTERCEPT_FUNC(__fxstat)
  700. #else
  701. #define MSAN_MAYBE_INTERCEPT___FXSTAT
  702. #endif
  703. #if SANITIZER_GLIBC
  704. INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
  705. ENSURE_MSAN_INITED();
  706. int res = REAL(__fxstat64)(magic, fd, buf);
  707. if (!res)
  708. __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
  709. return res;
  710. }
  711. # define MSAN_MAYBE_INTERCEPT___FXSTAT64 MSAN_INTERCEPT_FUNC(__fxstat64)
  712. #else
  713. # define MSAN_MAYBE_INTERCEPT___FXSTAT64
  714. #endif
  715. #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX
  716. INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
  717. ENSURE_MSAN_INITED();
  718. int res = REAL(fstatat)(fd, pathname, buf, flags);
  719. if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
  720. return res;
  721. }
  722. # define MSAN_MAYBE_INTERCEPT_FSTATAT MSAN_INTERCEPT_FUNC(fstatat)
  723. #else
  724. # define MSAN_MAYBE_INTERCEPT_FSTATAT
  725. #endif
  726. #if SANITIZER_STAT_LINUX
  727. INTERCEPTOR(int, fstatat64, int fd, char *pathname, void *buf, int flags) {
  728. ENSURE_MSAN_INITED();
  729. int res = REAL(fstatat64)(fd, pathname, buf, flags);
  730. if (!res)
  731. __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
  732. return res;
  733. }
  734. # define MSAN_MAYBE_INTERCEPT_FSTATAT64 MSAN_INTERCEPT_FUNC(fstatat64)
  735. #else
  736. # define MSAN_MAYBE_INTERCEPT_FSTATAT64
  737. #endif
  738. #if SANITIZER_GLIBC
  739. INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
  740. int flags) {
  741. ENSURE_MSAN_INITED();
  742. int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
  743. if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
  744. return res;
  745. }
  746. # define MSAN_MAYBE_INTERCEPT___FXSTATAT MSAN_INTERCEPT_FUNC(__fxstatat)
  747. #else
  748. # define MSAN_MAYBE_INTERCEPT___FXSTATAT
  749. #endif
  750. #if SANITIZER_GLIBC
  751. INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
  752. int flags) {
  753. ENSURE_MSAN_INITED();
  754. int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
  755. if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
  756. return res;
  757. }
  758. # define MSAN_MAYBE_INTERCEPT___FXSTATAT64 MSAN_INTERCEPT_FUNC(__fxstatat64)
  759. #else
  760. # define MSAN_MAYBE_INTERCEPT___FXSTATAT64
  761. #endif
  762. INTERCEPTOR(int, pipe, int pipefd[2]) {
  763. if (msan_init_is_running)
  764. return REAL(pipe)(pipefd);
  765. ENSURE_MSAN_INITED();
  766. int res = REAL(pipe)(pipefd);
  767. if (!res)
  768. __msan_unpoison(pipefd, sizeof(int[2]));
  769. return res;
  770. }
  771. INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
  772. ENSURE_MSAN_INITED();
  773. int res = REAL(pipe2)(pipefd, flags);
  774. if (!res)
  775. __msan_unpoison(pipefd, sizeof(int[2]));
  776. return res;
  777. }
  778. INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
  779. ENSURE_MSAN_INITED();
  780. int res = REAL(socketpair)(domain, type, protocol, sv);
  781. if (!res)
  782. __msan_unpoison(sv, sizeof(int[2]));
  783. return res;
  784. }
  785. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  786. INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
  787. ENSURE_MSAN_INITED();
  788. char *res = REAL(fgets_unlocked)(s, size, stream);
  789. if (res)
  790. __msan_unpoison(s, internal_strlen(s) + 1);
  791. return res;
  792. }
  793. #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked)
  794. #else
  795. #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED
  796. #endif
  797. #define INTERCEPTOR_GETRLIMIT_BODY(func, resource, rlim) \
  798. if (msan_init_is_running) \
  799. return REAL(getrlimit)(resource, rlim); \
  800. ENSURE_MSAN_INITED(); \
  801. int res = REAL(func)(resource, rlim); \
  802. if (!res) \
  803. __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz); \
  804. return res
  805. INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
  806. INTERCEPTOR_GETRLIMIT_BODY(getrlimit, resource, rlim);
  807. }
  808. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  809. INTERCEPTOR(int, __getrlimit, int resource, void *rlim) {
  810. INTERCEPTOR_GETRLIMIT_BODY(__getrlimit, resource, rlim);
  811. }
  812. INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
  813. if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim);
  814. ENSURE_MSAN_INITED();
  815. int res = REAL(getrlimit64)(resource, rlim);
  816. if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
  817. return res;
  818. }
  819. INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit,
  820. void *old_rlimit) {
  821. if (msan_init_is_running)
  822. return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
  823. ENSURE_MSAN_INITED();
  824. CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz);
  825. int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
  826. if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz);
  827. return res;
  828. }
  829. INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit,
  830. void *old_rlimit) {
  831. if (msan_init_is_running)
  832. return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
  833. ENSURE_MSAN_INITED();
  834. CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz);
  835. int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
  836. if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz);
  837. return res;
  838. }
  839. #define MSAN_MAYBE_INTERCEPT___GETRLIMIT INTERCEPT_FUNCTION(__getrlimit)
  840. #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64)
  841. #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit)
  842. #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64)
  843. #else
  844. #define MSAN_MAYBE_INTERCEPT___GETRLIMIT
  845. #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64
  846. #define MSAN_MAYBE_INTERCEPT_PRLIMIT
  847. #define MSAN_MAYBE_INTERCEPT_PRLIMIT64
  848. #endif
  849. INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
  850. ENSURE_MSAN_INITED();
  851. int res = REAL(gethostname)(name, len);
  852. if (!res || (res == -1 && errno == errno_ENAMETOOLONG)) {
  853. SIZE_T real_len = internal_strnlen(name, len);
  854. if (real_len < len)
  855. ++real_len;
  856. __msan_unpoison(name, real_len);
  857. }
  858. return res;
  859. }
  860. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  861. INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
  862. int timeout) {
  863. ENSURE_MSAN_INITED();
  864. int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
  865. if (res > 0) {
  866. __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
  867. }
  868. return res;
  869. }
  870. #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
  871. #else
  872. #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT
  873. #endif
  874. #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  875. INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
  876. int timeout, void *sigmask) {
  877. ENSURE_MSAN_INITED();
  878. int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
  879. if (res > 0) {
  880. __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
  881. }
  882. return res;
  883. }
  884. #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
  885. #else
  886. #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
  887. #endif
  888. INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
  889. GET_MALLOC_STACK_TRACE;
  890. if (DlsymAlloc::Use())
  891. return DlsymAlloc::Callocate(nmemb, size);
  892. return msan_calloc(nmemb, size, &stack);
  893. }
  894. INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
  895. if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
  896. return DlsymAlloc::Realloc(ptr, size);
  897. GET_MALLOC_STACK_TRACE;
  898. return msan_realloc(ptr, size, &stack);
  899. }
  900. INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) {
  901. GET_MALLOC_STACK_TRACE;
  902. return msan_reallocarray(ptr, nmemb, size, &stack);
  903. }
  904. INTERCEPTOR(void *, malloc, SIZE_T size) {
  905. if (DlsymAlloc::Use())
  906. return DlsymAlloc::Allocate(size);
  907. GET_MALLOC_STACK_TRACE;
  908. return msan_malloc(size, &stack);
  909. }
  910. void __msan_allocated_memory(const void *data, uptr size) {
  911. if (flags()->poison_in_malloc) {
  912. GET_MALLOC_STACK_TRACE;
  913. stack.tag = STACK_TRACE_TAG_POISON;
  914. PoisonMemory(data, size, &stack);
  915. }
  916. }
  917. void __msan_copy_shadow(void *dest, const void *src, uptr n) {
  918. GET_STORE_STACK_TRACE;
  919. MoveShadowAndOrigin(dest, src, n, &stack);
  920. }
  921. void __sanitizer_dtor_callback(const void *data, uptr size) {
  922. if (flags()->poison_in_dtor) {
  923. GET_MALLOC_STACK_TRACE;
  924. stack.tag = STACK_TRACE_TAG_POISON;
  925. PoisonMemory(data, size, &stack);
  926. }
  927. }
  928. void __sanitizer_dtor_callback_fields(const void *data, uptr size) {
  929. if (flags()->poison_in_dtor) {
  930. GET_MALLOC_STACK_TRACE;
  931. stack.tag = STACK_TRACE_TAG_FIELDS;
  932. PoisonMemory(data, size, &stack);
  933. }
  934. }
  935. void __sanitizer_dtor_callback_vptr(const void *data) {
  936. if (flags()->poison_in_dtor) {
  937. GET_MALLOC_STACK_TRACE;
  938. stack.tag = STACK_TRACE_TAG_VPTR;
  939. PoisonMemory(data, sizeof(void *), &stack);
  940. }
  941. }
  942. template <class Mmap>
  943. static void *mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length,
  944. int prot, int flags, int fd, OFF64_T offset) {
  945. SIZE_T rounded_length = RoundUpTo(length, GetPageSize());
  946. void *end_addr = (char *)addr + (rounded_length - 1);
  947. if (addr && (!MEM_IS_APP(addr) || !MEM_IS_APP(end_addr))) {
  948. if (flags & map_fixed) {
  949. errno = errno_EINVAL;
  950. return (void *)-1;
  951. } else {
  952. addr = nullptr;
  953. }
  954. }
  955. void *res = real_mmap(addr, length, prot, flags, fd, offset);
  956. if (res != (void *)-1) {
  957. void *end_res = (char *)res + (rounded_length - 1);
  958. if (MEM_IS_APP(res) && MEM_IS_APP(end_res)) {
  959. __msan_unpoison(res, rounded_length);
  960. } else {
  961. // Application has attempted to map more memory than is supported by
  962. // MSAN. Act as if we ran out of memory.
  963. internal_munmap(res, length);
  964. errno = errno_ENOMEM;
  965. return (void *)-1;
  966. }
  967. }
  968. return res;
  969. }
  970. INTERCEPTOR(int, getrusage, int who, void *usage) {
  971. ENSURE_MSAN_INITED();
  972. int res = REAL(getrusage)(who, usage);
  973. if (res == 0) {
  974. __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
  975. }
  976. return res;
  977. }
  978. class SignalHandlerScope {
  979. public:
  980. SignalHandlerScope() {
  981. if (MsanThread *t = GetCurrentThread())
  982. t->EnterSignalHandler();
  983. }
  984. ~SignalHandlerScope() {
  985. if (MsanThread *t = GetCurrentThread())
  986. t->LeaveSignalHandler();
  987. }
  988. };
  989. // sigactions_mu guarantees atomicity of sigaction() and signal() calls.
  990. // Access to sigactions[] is gone with relaxed atomics to avoid data race with
  991. // the signal handler.
  992. const int kMaxSignals = 1024;
  993. static atomic_uintptr_t sigactions[kMaxSignals];
  994. static StaticSpinMutex sigactions_mu;
  995. static void SignalHandler(int signo) {
  996. SignalHandlerScope signal_handler_scope;
  997. ScopedThreadLocalStateBackup stlsb;
  998. UnpoisonParam(1);
  999. typedef void (*signal_cb)(int x);
  1000. signal_cb cb =
  1001. (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
  1002. cb(signo);
  1003. }
  1004. static void SignalAction(int signo, void *si, void *uc) {
  1005. SignalHandlerScope signal_handler_scope;
  1006. ScopedThreadLocalStateBackup stlsb;
  1007. UnpoisonParam(3);
  1008. __msan_unpoison(si, sizeof(__sanitizer_sigaction));
  1009. __msan_unpoison(uc, ucontext_t_sz(uc));
  1010. typedef void (*sigaction_cb)(int, void *, void *);
  1011. sigaction_cb cb =
  1012. (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
  1013. cb(signo, si, uc);
  1014. CHECK_UNPOISONED(uc, ucontext_t_sz(uc));
  1015. }
  1016. static void read_sigaction(const __sanitizer_sigaction *act) {
  1017. CHECK_UNPOISONED(&act->sa_flags, sizeof(act->sa_flags));
  1018. if (act->sa_flags & __sanitizer::sa_siginfo)
  1019. CHECK_UNPOISONED(&act->sigaction, sizeof(act->sigaction));
  1020. else
  1021. CHECK_UNPOISONED(&act->handler, sizeof(act->handler));
  1022. CHECK_UNPOISONED(&act->sa_mask, sizeof(act->sa_mask));
  1023. }
  1024. extern "C" int pthread_attr_init(void *attr);
  1025. extern "C" int pthread_attr_destroy(void *attr);
  1026. static void *MsanThreadStartFunc(void *arg) {
  1027. MsanThread *t = (MsanThread *)arg;
  1028. SetCurrentThread(t);
  1029. t->Init();
  1030. SetSigProcMask(&t->starting_sigset_, nullptr);
  1031. return t->ThreadStart();
  1032. }
  1033. INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
  1034. void * param) {
  1035. ENSURE_MSAN_INITED(); // for GetTlsSize()
  1036. __sanitizer_pthread_attr_t myattr;
  1037. if (!attr) {
  1038. pthread_attr_init(&myattr);
  1039. attr = &myattr;
  1040. }
  1041. AdjustStackSize(attr);
  1042. MsanThread *t = MsanThread::Create(callback, param);
  1043. ScopedBlockSignals block(&t->starting_sigset_);
  1044. int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t);
  1045. if (attr == &myattr)
  1046. pthread_attr_destroy(&myattr);
  1047. if (!res) {
  1048. __msan_unpoison(th, __sanitizer::pthread_t_sz);
  1049. }
  1050. return res;
  1051. }
  1052. INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
  1053. void (*dtor)(void *value)) {
  1054. if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor);
  1055. ENSURE_MSAN_INITED();
  1056. int res = REAL(pthread_key_create)(key, dtor);
  1057. if (!res && key)
  1058. __msan_unpoison(key, sizeof(*key));
  1059. return res;
  1060. }
  1061. #if SANITIZER_NETBSD
  1062. INTERCEPTOR(int, __libc_thr_keycreate, __sanitizer_pthread_key_t *m,
  1063. void (*dtor)(void *value))
  1064. ALIAS(WRAP(pthread_key_create));
  1065. #endif
  1066. INTERCEPTOR(int, pthread_join, void *thread, void **retval) {
  1067. ENSURE_MSAN_INITED();
  1068. int res = REAL(pthread_join)(thread, retval);
  1069. if (!res && retval)
  1070. __msan_unpoison(retval, sizeof(*retval));
  1071. return res;
  1072. }
  1073. #if SANITIZER_GLIBC
  1074. INTERCEPTOR(int, pthread_tryjoin_np, void *thread, void **retval) {
  1075. ENSURE_MSAN_INITED();
  1076. int res = REAL(pthread_tryjoin_np)(thread, retval);
  1077. if (!res && retval)
  1078. __msan_unpoison(retval, sizeof(*retval));
  1079. return res;
  1080. }
  1081. INTERCEPTOR(int, pthread_timedjoin_np, void *thread, void **retval,
  1082. const struct timespec *abstime) {
  1083. int res = REAL(pthread_timedjoin_np)(thread, retval, abstime);
  1084. if (!res && retval)
  1085. __msan_unpoison(retval, sizeof(*retval));
  1086. return res;
  1087. }
  1088. #endif
  1089. DEFINE_REAL_PTHREAD_FUNCTIONS
  1090. extern char *tzname[2];
  1091. INTERCEPTOR(void, tzset, int fake) {
  1092. ENSURE_MSAN_INITED();
  1093. InterceptorScope interceptor_scope;
  1094. REAL(tzset)(fake);
  1095. if (tzname[0])
  1096. __msan_unpoison(tzname[0], internal_strlen(tzname[0]) + 1);
  1097. if (tzname[1])
  1098. __msan_unpoison(tzname[1], internal_strlen(tzname[1]) + 1);
  1099. return;
  1100. }
  1101. struct MSanAtExitRecord {
  1102. void (*func)(void *arg);
  1103. void *arg;
  1104. };
  1105. struct InterceptorContext {
  1106. Mutex atexit_mu;
  1107. Vector<struct MSanAtExitRecord *> AtExitStack;
  1108. InterceptorContext()
  1109. : AtExitStack() {
  1110. }
  1111. };
  1112. static ALIGNED(64) char interceptor_placeholder[sizeof(InterceptorContext)];
  1113. InterceptorContext *interceptor_ctx() {
  1114. return reinterpret_cast<InterceptorContext*>(&interceptor_placeholder[0]);
  1115. }
  1116. void MSanAtExitWrapper() {
  1117. MSanAtExitRecord *r;
  1118. {
  1119. Lock l(&interceptor_ctx()->atexit_mu);
  1120. uptr element = interceptor_ctx()->AtExitStack.Size() - 1;
  1121. r = interceptor_ctx()->AtExitStack[element];
  1122. interceptor_ctx()->AtExitStack.PopBack();
  1123. }
  1124. UnpoisonParam(1);
  1125. ((void(*)())r->func)();
  1126. InternalFree(r);
  1127. }
  1128. void MSanCxaAtExitWrapper(void *arg) {
  1129. UnpoisonParam(1);
  1130. MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
  1131. // libc before 2.27 had race which caused occasional double handler execution
  1132. // https://sourceware.org/ml/libc-alpha/2017-08/msg01204.html
  1133. if (!r->func)
  1134. return;
  1135. r->func(r->arg);
  1136. r->func = nullptr;
  1137. }
  1138. static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso);
  1139. // Unpoison argument shadow for C++ module destructors.
  1140. INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
  1141. void *dso_handle) {
  1142. if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
  1143. return setup_at_exit_wrapper((void(*)())func, arg, dso_handle);
  1144. }
  1145. // Unpoison argument shadow for C++ module destructors.
  1146. INTERCEPTOR(int, atexit, void (*func)()) {
  1147. // Avoid calling real atexit as it is unreachable on at least on Linux.
  1148. if (msan_init_is_running)
  1149. return REAL(__cxa_atexit)((void (*)(void *a))func, 0, 0);
  1150. return setup_at_exit_wrapper((void(*)())func, 0, 0);
  1151. }
  1152. static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso) {
  1153. ENSURE_MSAN_INITED();
  1154. MSanAtExitRecord *r =
  1155. (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
  1156. r->func = (void(*)(void *a))f;
  1157. r->arg = arg;
  1158. int res;
  1159. if (!dso) {
  1160. // NetBSD does not preserve the 2nd argument if dso is equal to 0
  1161. // Store ctx in a local stack-like structure
  1162. Lock l(&interceptor_ctx()->atexit_mu);
  1163. res = REAL(__cxa_atexit)((void (*)(void *a))MSanAtExitWrapper, 0, 0);
  1164. if (!res) {
  1165. interceptor_ctx()->AtExitStack.PushBack(r);
  1166. }
  1167. } else {
  1168. res = REAL(__cxa_atexit)(MSanCxaAtExitWrapper, r, dso);
  1169. }
  1170. return res;
  1171. }
  1172. // NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly
  1173. // with MSan.
  1174. #if SANITIZER_LINUX
  1175. INTERCEPTOR(int, openpty, int *aparent, int *aworker, char *name,
  1176. const void *termp, const void *winp) {
  1177. ENSURE_MSAN_INITED();
  1178. InterceptorScope interceptor_scope;
  1179. int res = REAL(openpty)(aparent, aworker, name, termp, winp);
  1180. if (!res) {
  1181. __msan_unpoison(aparent, sizeof(*aparent));
  1182. __msan_unpoison(aworker, sizeof(*aworker));
  1183. }
  1184. return res;
  1185. }
  1186. #define MSAN_MAYBE_INTERCEPT_OPENPTY INTERCEPT_FUNCTION(openpty)
  1187. #else
  1188. #define MSAN_MAYBE_INTERCEPT_OPENPTY
  1189. #endif
  1190. // NetBSD ships with forkpty(3) in -lutil, that needs to be prebuilt explicitly
  1191. // with MSan.
  1192. #if SANITIZER_LINUX
  1193. INTERCEPTOR(int, forkpty, int *aparent, char *name, const void *termp,
  1194. const void *winp) {
  1195. ENSURE_MSAN_INITED();
  1196. InterceptorScope interceptor_scope;
  1197. int res = REAL(forkpty)(aparent, name, termp, winp);
  1198. if (res != -1)
  1199. __msan_unpoison(aparent, sizeof(*aparent));
  1200. return res;
  1201. }
  1202. #define MSAN_MAYBE_INTERCEPT_FORKPTY INTERCEPT_FUNCTION(forkpty)
  1203. #else
  1204. #define MSAN_MAYBE_INTERCEPT_FORKPTY
  1205. #endif
  1206. struct MSanInterceptorContext {
  1207. bool in_interceptor_scope;
  1208. };
  1209. namespace __msan {
  1210. int OnExit() {
  1211. // FIXME: ask frontend whether we need to return failure.
  1212. return 0;
  1213. }
  1214. } // namespace __msan
  1215. // A version of CHECK_UNPOISONED using a saved scope value. Used in common
  1216. // interceptors.
  1217. #define CHECK_UNPOISONED_CTX(ctx, x, n) \
  1218. do { \
  1219. if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
  1220. CHECK_UNPOISONED_0(x, n); \
  1221. } while (0)
  1222. #define MSAN_INTERCEPT_FUNC(name) \
  1223. do { \
  1224. if (!INTERCEPT_FUNCTION(name)) \
  1225. VReport(1, "MemorySanitizer: failed to intercept '%s'\n", #name); \
  1226. } while (0)
  1227. #define MSAN_INTERCEPT_FUNC_VER(name, ver) \
  1228. do { \
  1229. if (!INTERCEPT_FUNCTION_VER(name, ver)) \
  1230. VReport(1, "MemorySanitizer: failed to intercept '%s@@%s'\n", #name, \
  1231. ver); \
  1232. } while (0)
  1233. #define MSAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \
  1234. do { \
  1235. if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \
  1236. VReport(1, "MemorySanitizer: failed to intercept '%s@@%s' or '%s'\n", \
  1237. #name, ver, #name); \
  1238. } while (0)
  1239. #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name)
  1240. #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
  1241. MSAN_INTERCEPT_FUNC_VER(name, ver)
  1242. #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \
  1243. MSAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver)
  1244. #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \
  1245. UnpoisonParam(count)
  1246. #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
  1247. __msan_unpoison(ptr, size)
  1248. #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
  1249. CHECK_UNPOISONED_CTX(ctx, ptr, size)
  1250. #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
  1251. __msan_unpoison(ptr, size)
  1252. #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
  1253. if (msan_init_is_running) \
  1254. return REAL(func)(__VA_ARGS__); \
  1255. ENSURE_MSAN_INITED(); \
  1256. MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
  1257. ctx = (void *)&msan_ctx; \
  1258. (void)ctx; \
  1259. InterceptorScope interceptor_scope; \
  1260. __msan_unpoison(__errno_location(), sizeof(int));
  1261. #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
  1262. do { \
  1263. } while (false)
  1264. #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
  1265. do { \
  1266. } while (false)
  1267. #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
  1268. do { \
  1269. } while (false)
  1270. #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
  1271. do { \
  1272. } while (false)
  1273. #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
  1274. do { \
  1275. } while (false) // FIXME
  1276. #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
  1277. do { \
  1278. } while (false) // FIXME
  1279. #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
  1280. #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
  1281. #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
  1282. do { \
  1283. link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \
  1284. if (filename && map) \
  1285. ForEachMappedRegion(map, __msan_unpoison); \
  1286. } while (false)
  1287. #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!msan_inited)
  1288. #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
  1289. if (MsanThread *t = GetCurrentThread()) { \
  1290. *begin = t->tls_begin(); \
  1291. *end = t->tls_end(); \
  1292. } else { \
  1293. *begin = *end = 0; \
  1294. }
  1295. #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
  1296. { \
  1297. (void)ctx; \
  1298. return __msan_memset(block, c, size); \
  1299. }
  1300. #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
  1301. { \
  1302. (void)ctx; \
  1303. return __msan_memmove(to, from, size); \
  1304. }
  1305. #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
  1306. { \
  1307. (void)ctx; \
  1308. return __msan_memcpy(to, from, size); \
  1309. }
  1310. #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
  1311. do { \
  1312. GET_STORE_STACK_TRACE; \
  1313. CopyShadowAndOrigin(to, from, size, &stack); \
  1314. __msan_unpoison(to + size, 1); \
  1315. } while (false)
  1316. #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, fd, \
  1317. offset) \
  1318. do { \
  1319. return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \
  1320. } while (false)
  1321. #include "sanitizer_common/sanitizer_platform_interceptors.h"
  1322. #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
  1323. #include "sanitizer_common/sanitizer_common_interceptors.inc"
  1324. static uptr signal_impl(int signo, uptr cb);
  1325. static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
  1326. __sanitizer_sigaction *oldact);
  1327. #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \
  1328. { return sigaction_impl(signo, act, oldact); }
  1329. #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \
  1330. { \
  1331. handler = signal_impl(signo, handler); \
  1332. InterceptorScope interceptor_scope; \
  1333. return REAL(func)(signo, handler); \
  1334. }
  1335. #define SIGNAL_INTERCEPTOR_ENTER() ENSURE_MSAN_INITED()
  1336. #include "sanitizer_common/sanitizer_signal_interceptors.inc"
  1337. static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
  1338. __sanitizer_sigaction *oldact) {
  1339. ENSURE_MSAN_INITED();
  1340. if (signo <= 0 || signo >= kMaxSignals) {
  1341. errno = errno_EINVAL;
  1342. return -1;
  1343. }
  1344. if (act) read_sigaction(act);
  1345. int res;
  1346. if (flags()->wrap_signals) {
  1347. SpinMutexLock lock(&sigactions_mu);
  1348. uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
  1349. __sanitizer_sigaction new_act;
  1350. __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr;
  1351. if (act) {
  1352. REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction));
  1353. uptr cb = (uptr)pnew_act->sigaction;
  1354. uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
  1355. ? (uptr)SignalAction
  1356. : (uptr)SignalHandler;
  1357. if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
  1358. atomic_store(&sigactions[signo], cb, memory_order_relaxed);
  1359. pnew_act->sigaction = (decltype(pnew_act->sigaction))new_cb;
  1360. }
  1361. }
  1362. res = REAL(SIGACTION_SYMNAME)(signo, pnew_act, oldact);
  1363. if (res == 0 && oldact) {
  1364. uptr cb = (uptr)oldact->sigaction;
  1365. if (cb == (uptr)SignalAction || cb == (uptr)SignalHandler) {
  1366. oldact->sigaction = (decltype(oldact->sigaction))old_cb;
  1367. }
  1368. }
  1369. } else {
  1370. res = REAL(SIGACTION_SYMNAME)(signo, act, oldact);
  1371. }
  1372. if (res == 0 && oldact) {
  1373. __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
  1374. }
  1375. return res;
  1376. }
  1377. static uptr signal_impl(int signo, uptr cb) {
  1378. ENSURE_MSAN_INITED();
  1379. if (signo <= 0 || signo >= kMaxSignals) {
  1380. errno = errno_EINVAL;
  1381. return -1;
  1382. }
  1383. if (flags()->wrap_signals) {
  1384. SpinMutexLock lock(&sigactions_mu);
  1385. if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
  1386. atomic_store(&sigactions[signo], cb, memory_order_relaxed);
  1387. cb = (uptr)&SignalHandler;
  1388. }
  1389. }
  1390. return cb;
  1391. }
  1392. #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
  1393. #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
  1394. do { \
  1395. } while (false)
  1396. #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
  1397. do { \
  1398. } while (false)
  1399. #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
  1400. #include "sanitizer_common/sanitizer_common_syscalls.inc"
  1401. #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
  1402. INTERCEPTOR(const char *, strsignal, int sig) {
  1403. void *ctx;
  1404. COMMON_INTERCEPTOR_ENTER(ctx, strsignal, sig);
  1405. const char *res = REAL(strsignal)(sig);
  1406. if (res)
  1407. __msan_unpoison(res, internal_strlen(res) + 1);
  1408. return res;
  1409. }
  1410. INTERCEPTOR(int, dladdr, void *addr, void *info) {
  1411. void *ctx;
  1412. COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info);
  1413. int res = REAL(dladdr)(addr, info);
  1414. if (res != 0)
  1415. UnpoisonDllAddrInfo(info);
  1416. return res;
  1417. }
  1418. #if SANITIZER_GLIBC
  1419. INTERCEPTOR(int, dladdr1, void *addr, void *info, void **extra_info,
  1420. int flags) {
  1421. void *ctx;
  1422. COMMON_INTERCEPTOR_ENTER(ctx, dladdr1, addr, info, extra_info, flags);
  1423. int res = REAL(dladdr1)(addr, info, extra_info, flags);
  1424. if (res != 0) {
  1425. UnpoisonDllAddrInfo(info);
  1426. UnpoisonDllAddr1ExtraInfo(extra_info, flags);
  1427. }
  1428. return res;
  1429. }
  1430. # define MSAN_MAYBE_INTERCEPT_DLADDR1 MSAN_INTERCEPT_FUNC(dladdr1)
  1431. #else
  1432. #define MSAN_MAYBE_INTERCEPT_DLADDR1
  1433. #endif
  1434. INTERCEPTOR(char *, dlerror, int fake) {
  1435. void *ctx;
  1436. COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake);
  1437. char *res = REAL(dlerror)(fake);
  1438. if (res)
  1439. __msan_unpoison(res, internal_strlen(res) + 1);
  1440. return res;
  1441. }
  1442. typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
  1443. void *data);
  1444. struct dl_iterate_phdr_data {
  1445. dl_iterate_phdr_cb callback;
  1446. void *data;
  1447. };
  1448. static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
  1449. void *data) {
  1450. if (info) {
  1451. __msan_unpoison(info, size);
  1452. if (info->dlpi_phdr && info->dlpi_phnum)
  1453. __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum);
  1454. if (info->dlpi_name)
  1455. __msan_unpoison(info->dlpi_name, internal_strlen(info->dlpi_name) + 1);
  1456. }
  1457. dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
  1458. UnpoisonParam(3);
  1459. return cbdata->callback(info, size, cbdata->data);
  1460. }
  1461. INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) {
  1462. ENSURE_MSAN_INITED();
  1463. void *p = REAL(shmat)(shmid, shmaddr, shmflg);
  1464. if (p != (void *)-1) {
  1465. __sanitizer_shmid_ds ds;
  1466. int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds);
  1467. if (!res) {
  1468. __msan_unpoison(p, ds.shm_segsz);
  1469. }
  1470. }
  1471. return p;
  1472. }
  1473. INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
  1474. void *ctx;
  1475. COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data);
  1476. dl_iterate_phdr_data cbdata;
  1477. cbdata.callback = callback;
  1478. cbdata.data = data;
  1479. int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
  1480. return res;
  1481. }
  1482. // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
  1483. INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
  1484. ENSURE_MSAN_INITED();
  1485. wchar_t *res = REAL(wcschr)(s, wc, ps);
  1486. return res;
  1487. }
  1488. // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
  1489. INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
  1490. ENSURE_MSAN_INITED();
  1491. GET_STORE_STACK_TRACE;
  1492. wchar_t *res = REAL(wcscpy)(dest, src);
  1493. CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (internal_wcslen(src) + 1),
  1494. &stack);
  1495. return res;
  1496. }
  1497. INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
  1498. ENSURE_MSAN_INITED();
  1499. GET_STORE_STACK_TRACE;
  1500. SIZE_T copy_size = internal_wcsnlen(src, n);
  1501. if (copy_size < n) copy_size++; // trailing \0
  1502. wchar_t *res = REAL(wcsncpy)(dest, src, n);
  1503. CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack);
  1504. __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t));
  1505. return res;
  1506. }
  1507. // These interface functions reside here so that they can use
  1508. // REAL(memset), etc.
  1509. void __msan_unpoison(const void *a, uptr size) {
  1510. if (!MEM_IS_APP(a)) return;
  1511. SetShadow(a, size, 0);
  1512. }
  1513. void __msan_poison(const void *a, uptr size) {
  1514. if (!MEM_IS_APP(a)) return;
  1515. SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1);
  1516. }
  1517. void __msan_poison_stack(void *a, uptr size) {
  1518. if (!MEM_IS_APP(a)) return;
  1519. SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1);
  1520. }
  1521. void __msan_unpoison_param(uptr n) { UnpoisonParam(n); }
  1522. void __msan_clear_and_unpoison(void *a, uptr size) {
  1523. REAL(memset)(a, 0, size);
  1524. SetShadow(a, size, 0);
  1525. }
  1526. void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
  1527. if (!msan_inited) return internal_memcpy(dest, src, n);
  1528. if (msan_init_is_running || __msan::IsInSymbolizerOrUnwider())
  1529. return REAL(memcpy)(dest, src, n);
  1530. ENSURE_MSAN_INITED();
  1531. GET_STORE_STACK_TRACE;
  1532. void *res = REAL(memcpy)(dest, src, n);
  1533. CopyShadowAndOrigin(dest, src, n, &stack);
  1534. return res;
  1535. }
  1536. void *__msan_memset(void *s, int c, SIZE_T n) {
  1537. if (!msan_inited) return internal_memset(s, c, n);
  1538. if (msan_init_is_running) return REAL(memset)(s, c, n);
  1539. ENSURE_MSAN_INITED();
  1540. void *res = REAL(memset)(s, c, n);
  1541. __msan_unpoison(s, n);
  1542. return res;
  1543. }
  1544. void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
  1545. if (!msan_inited) return internal_memmove(dest, src, n);
  1546. if (msan_init_is_running) return REAL(memmove)(dest, src, n);
  1547. ENSURE_MSAN_INITED();
  1548. GET_STORE_STACK_TRACE;
  1549. void *res = REAL(memmove)(dest, src, n);
  1550. MoveShadowAndOrigin(dest, src, n, &stack);
  1551. return res;
  1552. }
  1553. void __msan_unpoison_string(const char* s) {
  1554. if (!MEM_IS_APP(s)) return;
  1555. __msan_unpoison(s, internal_strlen(s) + 1);
  1556. }
  1557. namespace __msan {
  1558. void InitializeInterceptors() {
  1559. static int inited = 0;
  1560. CHECK_EQ(inited, 0);
  1561. new(interceptor_ctx()) InterceptorContext();
  1562. InitializeCommonInterceptors();
  1563. InitializeSignalInterceptors();
  1564. INTERCEPT_FUNCTION(posix_memalign);
  1565. MSAN_MAYBE_INTERCEPT_MEMALIGN;
  1566. MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
  1567. INTERCEPT_FUNCTION(valloc);
  1568. MSAN_MAYBE_INTERCEPT_PVALLOC;
  1569. INTERCEPT_FUNCTION(malloc);
  1570. INTERCEPT_FUNCTION(calloc);
  1571. INTERCEPT_FUNCTION(realloc);
  1572. INTERCEPT_FUNCTION(reallocarray);
  1573. INTERCEPT_FUNCTION(free);
  1574. MSAN_MAYBE_INTERCEPT_CFREE;
  1575. MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
  1576. MSAN_MAYBE_INTERCEPT_MALLINFO;
  1577. MSAN_MAYBE_INTERCEPT_MALLINFO2;
  1578. MSAN_MAYBE_INTERCEPT_MALLOPT;
  1579. MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
  1580. INTERCEPT_FUNCTION(fread);
  1581. MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED;
  1582. INTERCEPT_FUNCTION(memccpy);
  1583. MSAN_MAYBE_INTERCEPT_MEMPCPY;
  1584. INTERCEPT_FUNCTION(bcopy);
  1585. INTERCEPT_FUNCTION(wmemset);
  1586. INTERCEPT_FUNCTION(wmemcpy);
  1587. MSAN_MAYBE_INTERCEPT_WMEMPCPY;
  1588. INTERCEPT_FUNCTION(wmemmove);
  1589. INTERCEPT_FUNCTION(strcpy);
  1590. MSAN_MAYBE_INTERCEPT_STPCPY;
  1591. MSAN_MAYBE_INTERCEPT_STPNCPY;
  1592. INTERCEPT_FUNCTION(strdup);
  1593. MSAN_MAYBE_INTERCEPT___STRDUP;
  1594. INTERCEPT_FUNCTION(strncpy);
  1595. MSAN_MAYBE_INTERCEPT_GCVT;
  1596. INTERCEPT_FUNCTION(strcat);
  1597. INTERCEPT_FUNCTION(strncat);
  1598. INTERCEPT_STRTO(strtod);
  1599. INTERCEPT_STRTO(strtof);
  1600. #ifdef SANITIZER_NLDBL_VERSION
  1601. INTERCEPT_STRTO_VER(strtold, SANITIZER_NLDBL_VERSION);
  1602. #else
  1603. INTERCEPT_STRTO(strtold);
  1604. #endif
  1605. INTERCEPT_STRTO(strtol);
  1606. INTERCEPT_STRTO(strtoul);
  1607. INTERCEPT_STRTO(strtoll);
  1608. INTERCEPT_STRTO(strtoull);
  1609. INTERCEPT_STRTO(strtouq);
  1610. INTERCEPT_STRTO(wcstod);
  1611. INTERCEPT_STRTO(wcstof);
  1612. #ifdef SANITIZER_NLDBL_VERSION
  1613. INTERCEPT_STRTO_VER(wcstold, SANITIZER_NLDBL_VERSION);
  1614. #else
  1615. INTERCEPT_STRTO(wcstold);
  1616. #endif
  1617. INTERCEPT_STRTO(wcstol);
  1618. INTERCEPT_STRTO(wcstoul);
  1619. INTERCEPT_STRTO(wcstoll);
  1620. INTERCEPT_STRTO(wcstoull);
  1621. #if SANITIZER_GLIBC
  1622. INTERCEPT_STRTO(__isoc23_strtod);
  1623. INTERCEPT_STRTO(__isoc23_strtof);
  1624. INTERCEPT_STRTO(__isoc23_strtold);
  1625. INTERCEPT_STRTO(__isoc23_strtol);
  1626. INTERCEPT_STRTO(__isoc23_strtoul);
  1627. INTERCEPT_STRTO(__isoc23_strtoll);
  1628. INTERCEPT_STRTO(__isoc23_strtoull);
  1629. INTERCEPT_STRTO(__isoc23_strtouq);
  1630. INTERCEPT_STRTO(__isoc23_wcstod);
  1631. INTERCEPT_STRTO(__isoc23_wcstof);
  1632. INTERCEPT_STRTO(__isoc23_wcstold);
  1633. INTERCEPT_STRTO(__isoc23_wcstol);
  1634. INTERCEPT_STRTO(__isoc23_wcstoul);
  1635. INTERCEPT_STRTO(__isoc23_wcstoll);
  1636. INTERCEPT_STRTO(__isoc23_wcstoull);
  1637. #endif
  1638. #ifdef SANITIZER_NLDBL_VERSION
  1639. INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION);
  1640. INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION);
  1641. #else
  1642. INTERCEPT_FUNCTION(vswprintf);
  1643. INTERCEPT_FUNCTION(swprintf);
  1644. #endif
  1645. INTERCEPT_FUNCTION(strftime);
  1646. INTERCEPT_FUNCTION(strftime_l);
  1647. MSAN_MAYBE_INTERCEPT___STRFTIME_L;
  1648. INTERCEPT_FUNCTION(wcsftime);
  1649. INTERCEPT_FUNCTION(wcsftime_l);
  1650. MSAN_MAYBE_INTERCEPT___WCSFTIME_L;
  1651. INTERCEPT_FUNCTION(mbtowc);
  1652. INTERCEPT_FUNCTION(mbrtowc);
  1653. INTERCEPT_FUNCTION(wcslen);
  1654. INTERCEPT_FUNCTION(wcsnlen);
  1655. INTERCEPT_FUNCTION(wcschr);
  1656. INTERCEPT_FUNCTION(wcscpy);
  1657. INTERCEPT_FUNCTION(wcsncpy);
  1658. INTERCEPT_FUNCTION(wcscmp);
  1659. INTERCEPT_FUNCTION(getenv);
  1660. INTERCEPT_FUNCTION(setenv);
  1661. INTERCEPT_FUNCTION(putenv);
  1662. INTERCEPT_FUNCTION(gettimeofday);
  1663. MSAN_MAYBE_INTERCEPT_FCVT;
  1664. MSAN_MAYBE_INTERCEPT_FSTAT;
  1665. MSAN_MAYBE_INTERCEPT_FSTAT64;
  1666. MSAN_MAYBE_INTERCEPT___FXSTAT;
  1667. MSAN_MAYBE_INTERCEPT_FSTATAT;
  1668. MSAN_MAYBE_INTERCEPT_FSTATAT64;
  1669. MSAN_MAYBE_INTERCEPT___FXSTATAT;
  1670. MSAN_MAYBE_INTERCEPT___FXSTAT64;
  1671. MSAN_MAYBE_INTERCEPT___FXSTATAT64;
  1672. INTERCEPT_FUNCTION(pipe);
  1673. INTERCEPT_FUNCTION(pipe2);
  1674. INTERCEPT_FUNCTION(socketpair);
  1675. MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED;
  1676. INTERCEPT_FUNCTION(getrlimit);
  1677. MSAN_MAYBE_INTERCEPT___GETRLIMIT;
  1678. MSAN_MAYBE_INTERCEPT_GETRLIMIT64;
  1679. MSAN_MAYBE_INTERCEPT_PRLIMIT;
  1680. MSAN_MAYBE_INTERCEPT_PRLIMIT64;
  1681. INTERCEPT_FUNCTION(gethostname);
  1682. MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
  1683. MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
  1684. INTERCEPT_FUNCTION(strsignal);
  1685. INTERCEPT_FUNCTION(dladdr);
  1686. MSAN_MAYBE_INTERCEPT_DLADDR1;
  1687. INTERCEPT_FUNCTION(dlerror);
  1688. INTERCEPT_FUNCTION(dl_iterate_phdr);
  1689. INTERCEPT_FUNCTION(getrusage);
  1690. #if defined(__mips__)
  1691. INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2");
  1692. #else
  1693. INTERCEPT_FUNCTION(pthread_create);
  1694. #endif
  1695. INTERCEPT_FUNCTION(pthread_join);
  1696. INTERCEPT_FUNCTION(pthread_key_create);
  1697. #if SANITIZER_GLIBC
  1698. INTERCEPT_FUNCTION(pthread_tryjoin_np);
  1699. INTERCEPT_FUNCTION(pthread_timedjoin_np);
  1700. #endif
  1701. #if SANITIZER_NETBSD
  1702. INTERCEPT_FUNCTION(__libc_thr_keycreate);
  1703. #endif
  1704. INTERCEPT_FUNCTION(pthread_join);
  1705. INTERCEPT_FUNCTION(tzset);
  1706. INTERCEPT_FUNCTION(atexit);
  1707. INTERCEPT_FUNCTION(__cxa_atexit);
  1708. INTERCEPT_FUNCTION(shmat);
  1709. MSAN_MAYBE_INTERCEPT_OPENPTY;
  1710. MSAN_MAYBE_INTERCEPT_FORKPTY;
  1711. inited = 1;
  1712. }
  1713. } // namespace __msan