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