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