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