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