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