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