msan_interceptors.cc revision 0e38a67cd2d877e8680d65878c86c9e7e4fa4b1d
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 "sanitizer_common/sanitizer_platform_limits_posix.h"
21#include "sanitizer_common/sanitizer_allocator.h"
22#include "sanitizer_common/sanitizer_atomic.h"
23#include "sanitizer_common/sanitizer_common.h"
24#include "sanitizer_common/sanitizer_stackdepot.h"
25#include "sanitizer_common/sanitizer_libc.h"
26#include "sanitizer_common/sanitizer_linux.h"
27
28#include <stdarg.h>
29// ACHTUNG! No other system header includes in this file.
30// Ideally, we should get rid of stdarg.h as well.
31
32using namespace __msan;
33
34using __sanitizer::memory_order;
35using __sanitizer::atomic_load;
36using __sanitizer::atomic_store;
37using __sanitizer::atomic_uintptr_t;
38
39// True if this is a nested interceptor.
40static THREADLOCAL int in_interceptor_scope;
41
42struct InterceptorScope {
43  InterceptorScope() { ++in_interceptor_scope; }
44  ~InterceptorScope() { --in_interceptor_scope; }
45};
46
47bool IsInInterceptorScope() {
48  return in_interceptor_scope;
49}
50
51#define ENSURE_MSAN_INITED() do { \
52  CHECK(!msan_init_is_running); \
53  if (!msan_inited) { \
54    __msan_init(); \
55  } \
56} while (0)
57
58// Check that [x, x+n) range is unpoisoned.
59#define CHECK_UNPOISONED_0(x, n)                                             \
60  do {                                                                       \
61    sptr offset = __msan_test_shadow(x, n);                                  \
62    if (__msan::IsInSymbolizer()) break;                                     \
63    if (offset >= 0 && __msan::flags()->report_umrs) {                       \
64      GET_CALLER_PC_BP_SP;                                                   \
65      (void) sp;                                                             \
66      Printf("UMR in %s at offset %d inside [%p, +%d) \n", __FUNCTION__,     \
67             offset, x, n);                                                  \
68      __msan::PrintWarningWithOrigin(pc, bp,                                 \
69                                     __msan_get_origin((char *)x + offset)); \
70      if (__msan::flags()->halt_on_error) {                                  \
71        Printf("Exiting\n");                                                 \
72        Die();                                                               \
73      }                                                                      \
74    }                                                                        \
75  } while (0)
76
77// Check that [x, x+n) range is unpoisoned unless we are in a nested
78// interceptor.
79#define CHECK_UNPOISONED(x, n)                             \
80  do {                                                     \
81    if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
82  } while (0);
83
84static void *fast_memset(void *ptr, int c, SIZE_T n);
85static void *fast_memcpy(void *dst, const void *src, SIZE_T n);
86
87INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
88  ENSURE_MSAN_INITED();
89  SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
90  if (res > 0)
91    __msan_unpoison(ptr, res *size);
92  return res;
93}
94
95INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
96            void *file) {
97  ENSURE_MSAN_INITED();
98  SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
99  if (res > 0)
100    __msan_unpoison(ptr, res *size);
101  return res;
102}
103
104INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
105  ENSURE_MSAN_INITED();
106  SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
107  if (res > 0)
108    __msan_unpoison(buf, res);
109  return res;
110}
111
112INTERCEPTOR(void *, memcpy, void *dest, const void *src, SIZE_T n) {
113  return __msan_memcpy(dest, src, n);
114}
115
116INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
117  return (char *)__msan_memcpy(dest, src, n) + n;
118}
119
120INTERCEPTOR(void *, memmove, void *dest, const void *src, SIZE_T n) {
121  return __msan_memmove(dest, src, n);
122}
123
124INTERCEPTOR(void *, memset, void *s, int c, SIZE_T n) {
125  return __msan_memset(s, c, n);
126}
127
128INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
129  return __msan_memmove(dest, src, n);
130}
131
132INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
133  GET_MALLOC_STACK_TRACE;
134  CHECK_EQ(alignment & (alignment - 1), 0);
135  CHECK_NE(memptr, 0);
136  *memptr = MsanReallocate(&stack, 0, size, alignment, false);
137  CHECK_NE(*memptr, 0);
138  __msan_unpoison(memptr, sizeof(*memptr));
139  return 0;
140}
141
142INTERCEPTOR(void, free, void *ptr) {
143  ENSURE_MSAN_INITED();
144  if (ptr == 0) return;
145  MsanDeallocate(ptr);
146}
147
148INTERCEPTOR(SIZE_T, strlen, const char *s) {
149  ENSURE_MSAN_INITED();
150  SIZE_T res = REAL(strlen)(s);
151  CHECK_UNPOISONED(s, res + 1);
152  return res;
153}
154
155INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T n) {
156  ENSURE_MSAN_INITED();
157  SIZE_T res = REAL(strnlen)(s, n);
158  SIZE_T scan_size = (res == n) ? res : res + 1;
159  CHECK_UNPOISONED(s, scan_size);
160  return res;
161}
162
163// FIXME: Add stricter shadow checks in str* interceptors (ex.: strcpy should
164// check the shadow of the terminating \0 byte).
165
166INTERCEPTOR(char *, strcpy, char *dest, const char *src) {  // NOLINT
167  ENSURE_MSAN_INITED();
168  SIZE_T n = REAL(strlen)(src);
169  char *res = REAL(strcpy)(dest, src);  // NOLINT
170  __msan_copy_poison(dest, src, n + 1);
171  return res;
172}
173
174INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) {  // NOLINT
175  ENSURE_MSAN_INITED();
176  SIZE_T copy_size = REAL(strnlen)(src, n);
177  if (copy_size < n)
178    copy_size++;  // trailing \0
179  char *res = REAL(strncpy)(dest, src, n);  // NOLINT
180  __msan_copy_poison(dest, src, copy_size);
181  return res;
182}
183
184INTERCEPTOR(char *, stpcpy, char *dest, const char *src) {  // NOLINT
185  ENSURE_MSAN_INITED();
186  SIZE_T n = REAL(strlen)(src);
187  char *res = REAL(stpcpy)(dest, src);  // NOLINT
188  __msan_copy_poison(dest, src, n + 1);
189  return res;
190}
191
192INTERCEPTOR(char *, strdup, char *src) {
193  ENSURE_MSAN_INITED();
194  SIZE_T n = REAL(strlen)(src);
195  char *res = REAL(strdup)(src);
196  __msan_copy_poison(res, src, n + 1);
197  return res;
198}
199
200INTERCEPTOR(char *, __strdup, char *src) {
201  ENSURE_MSAN_INITED();
202  SIZE_T n = REAL(strlen)(src);
203  char *res = REAL(__strdup)(src);
204  __msan_copy_poison(res, src, n + 1);
205  return res;
206}
207
208INTERCEPTOR(char *, strndup, char *src, SIZE_T n) {
209  ENSURE_MSAN_INITED();
210  SIZE_T copy_size = REAL(strnlen)(src, n);
211  char *res = REAL(strndup)(src, n);
212  __msan_copy_poison(res, src, copy_size);
213  __msan_unpoison(res + copy_size, 1); // \0
214  return res;
215}
216
217INTERCEPTOR(char *, __strndup, char *src, SIZE_T n) {
218  ENSURE_MSAN_INITED();
219  SIZE_T copy_size = REAL(strnlen)(src, n);
220  char *res = REAL(__strndup)(src, n);
221  __msan_copy_poison(res, src, copy_size);
222  __msan_unpoison(res + copy_size, 1); // \0
223  return res;
224}
225
226INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
227  ENSURE_MSAN_INITED();
228  char *res = REAL(gcvt)(number, ndigit, buf);
229  // DynamoRio tool will take care of unpoisoning gcvt result for us.
230  if (!__msan_has_dynamic_component()) {
231    SIZE_T n = REAL(strlen)(buf);
232    __msan_unpoison(buf, n + 1);
233  }
234  return res;
235}
236
237INTERCEPTOR(char *, strcat, char *dest, const char *src) {  // NOLINT
238  ENSURE_MSAN_INITED();
239  SIZE_T src_size = REAL(strlen)(src);
240  SIZE_T dest_size = REAL(strlen)(dest);
241  char *res = REAL(strcat)(dest, src);  // NOLINT
242  __msan_copy_poison(dest + dest_size, src, src_size + 1);
243  return res;
244}
245
246INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) {  // NOLINT
247  ENSURE_MSAN_INITED();
248  SIZE_T dest_size = REAL(strlen)(dest);
249  SIZE_T copy_size = REAL(strlen)(src);
250  if (copy_size < n)
251    copy_size++;  // trailing \0
252  char *res = REAL(strncat)(dest, src, n);  // NOLINT
253  __msan_copy_poison(dest + dest_size, src, copy_size);
254  return res;
255}
256
257INTERCEPTOR(long, strtol, const char *nptr, char **endptr,  // NOLINT
258            int base) {
259  ENSURE_MSAN_INITED();
260  long res = REAL(strtol)(nptr, endptr, base);  // NOLINT
261  if (!__msan_has_dynamic_component()) {
262    __msan_unpoison(endptr, sizeof(*endptr));
263  }
264  return res;
265}
266
267INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr,  // NOLINT
268            int base) {
269  ENSURE_MSAN_INITED();
270  long res = REAL(strtoll)(nptr, endptr, base);  //NOLINT
271  if (!__msan_has_dynamic_component()) {
272    __msan_unpoison(endptr, sizeof(*endptr));
273  }
274  return res;
275}
276
277INTERCEPTOR(unsigned long, strtoul, const char *nptr, char **endptr,  // NOLINT
278            int base) {
279  ENSURE_MSAN_INITED();
280  unsigned long res = REAL(strtoul)(nptr, endptr, base);  // NOLINT
281  if (!__msan_has_dynamic_component()) {
282    __msan_unpoison(endptr, sizeof(*endptr));
283  }
284  return res;
285}
286
287INTERCEPTOR(unsigned long long, strtoull, const char *nptr,  // NOLINT
288            char **endptr, int base) {
289  ENSURE_MSAN_INITED();
290  unsigned long res = REAL(strtoull)(nptr, endptr, base);  // NOLINT
291  if (!__msan_has_dynamic_component()) {
292    __msan_unpoison(endptr, sizeof(*endptr));
293  }
294  return res;
295}
296
297INTERCEPTOR(double, strtod, const char *nptr, char **endptr) {  // NOLINT
298  ENSURE_MSAN_INITED();
299  double res = REAL(strtod)(nptr, endptr);  // NOLINT
300  if (!__msan_has_dynamic_component()) {
301    __msan_unpoison(endptr, sizeof(*endptr));
302  }
303  return res;
304}
305
306INTERCEPTOR(float, strtof, const char *nptr, char **endptr) {  // NOLINT
307  ENSURE_MSAN_INITED();
308  float res = REAL(strtof)(nptr, endptr);  // NOLINT
309  if (!__msan_has_dynamic_component()) {
310    __msan_unpoison(endptr, sizeof(*endptr));
311  }
312  return res;
313}
314
315INTERCEPTOR(long double, strtold, const char *nptr, char **endptr) {  // NOLINT
316  ENSURE_MSAN_INITED();
317  long double res = REAL(strtold)(nptr, endptr);  // NOLINT
318  if (!__msan_has_dynamic_component()) {
319    __msan_unpoison(endptr, sizeof(*endptr));
320  }
321  return res;
322}
323
324INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap) {
325  ENSURE_MSAN_INITED();
326  int res = REAL(vasprintf)(strp, format, ap);
327  if (res >= 0 && !__msan_has_dynamic_component()) {
328    __msan_unpoison(strp, sizeof(*strp));
329    __msan_unpoison(*strp, res + 1);
330  }
331  return res;
332}
333
334INTERCEPTOR(int, asprintf, char **strp, const char *format, ...) {  // NOLINT
335  ENSURE_MSAN_INITED();
336  va_list ap;
337  va_start(ap, format);
338  int res = vasprintf(strp, format, ap);  // NOLINT
339  va_end(ap);
340  return res;
341}
342
343INTERCEPTOR(int, vsnprintf, char *str, uptr size,
344            const char *format, va_list ap) {
345  ENSURE_MSAN_INITED();
346  int res = REAL(vsnprintf)(str, size, format, ap);
347  if (res >= 0 && !__msan_has_dynamic_component()) {
348    __msan_unpoison(str, res + 1);
349  }
350  return res;
351}
352
353INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap) {
354  ENSURE_MSAN_INITED();
355  int res = REAL(vsprintf)(str, format, ap);
356  if (res >= 0 && !__msan_has_dynamic_component()) {
357    __msan_unpoison(str, res + 1);
358  }
359  return res;
360}
361
362INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
363  ENSURE_MSAN_INITED();
364  int res = REAL(vswprintf)(str, size, format, ap);
365  if (res >= 0 && !__msan_has_dynamic_component()) {
366    __msan_unpoison(str, 4 * (res + 1));
367  }
368  return res;
369}
370
371INTERCEPTOR(int, sprintf, char *str, const char *format, ...) {  // NOLINT
372  ENSURE_MSAN_INITED();
373  va_list ap;
374  va_start(ap, format);
375  int res = vsprintf(str, format, ap);  // NOLINT
376  va_end(ap);
377  return res;
378}
379
380INTERCEPTOR(int, snprintf, char *str, uptr size, const char *format, ...) {
381  ENSURE_MSAN_INITED();
382  va_list ap;
383  va_start(ap, format);
384  int res = vsnprintf(str, size, format, ap);
385  va_end(ap);
386  return res;
387}
388
389INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
390  ENSURE_MSAN_INITED();
391  va_list ap;
392  va_start(ap, format);
393  int res = vswprintf(str, size, format, ap);
394  va_end(ap);
395  return res;
396}
397
398// SIZE_T strftime(char *s, SIZE_T max, const char *format,const struct tm *tm);
399INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
400            void *tm) {
401  ENSURE_MSAN_INITED();
402  SIZE_T res = REAL(strftime)(s, max, format, tm);
403  if (res) __msan_unpoison(s, res + 1);
404  return res;
405}
406
407INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
408  ENSURE_MSAN_INITED();
409  int res = REAL(mbtowc)(dest, src, n);
410  if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
411  return res;
412}
413
414INTERCEPTOR(int, mbrtowc, wchar_t *dest, const char *src, SIZE_T n, void *ps) {
415  ENSURE_MSAN_INITED();
416  SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
417  if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
418  return res;
419}
420
421INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
422  ENSURE_MSAN_INITED();
423  SIZE_T res = REAL(wcslen)(s);
424  CHECK_UNPOISONED(s, sizeof(wchar_t) * (res + 1));
425  return res;
426}
427
428// wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
429INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
430  ENSURE_MSAN_INITED();
431  wchar_t *res = REAL(wcschr)(s, wc, ps);
432  return res;
433}
434
435// wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
436INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
437  ENSURE_MSAN_INITED();
438  wchar_t *res = REAL(wcscpy)(dest, src);
439  __msan_copy_poison(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1));
440  return res;
441}
442
443// wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
444INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
445  ENSURE_MSAN_INITED();
446  wchar_t *res = REAL(wmemcpy)(dest, src, n);
447  __msan_copy_poison(dest, src, n * sizeof(wchar_t));
448  return res;
449}
450
451INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
452  ENSURE_MSAN_INITED();
453  wchar_t *res = REAL(wmempcpy)(dest, src, n);
454  __msan_copy_poison(dest, src, n * sizeof(wchar_t));
455  return res;
456}
457
458INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
459  CHECK(MEM_IS_APP(s));
460  ENSURE_MSAN_INITED();
461  wchar_t *res = (wchar_t *)fast_memset(s, c, n * sizeof(wchar_t));
462  __msan_unpoison(s, n * sizeof(wchar_t));
463  return res;
464}
465
466INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
467  ENSURE_MSAN_INITED();
468  wchar_t *res = REAL(wmemmove)(dest, src, n);
469  __msan_move_poison(dest, src, n * sizeof(wchar_t));
470  return res;
471}
472
473INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
474  ENSURE_MSAN_INITED();
475  int res = REAL(wcscmp)(s1, s2);
476  return res;
477}
478
479INTERCEPTOR(double, wcstod, const wchar_t *nptr, wchar_t **endptr) {
480  ENSURE_MSAN_INITED();
481  double res = REAL(wcstod)(nptr, endptr);
482  __msan_unpoison(endptr, sizeof(*endptr));
483  return res;
484}
485
486// #define UNSUPPORTED(name) \
487//   INTERCEPTOR(void, name, void) {                     \
488//     Printf("MSAN: Unsupported %s\n", __FUNCTION__);   \
489//     Die();                                            \
490//   }
491
492// FIXME: intercept the following functions:
493// Note, they only matter when running without a dynamic tool.
494// UNSUPPORTED(wcscoll_l)
495// UNSUPPORTED(wcsnrtombs)
496// UNSUPPORTED(wcstol)
497// UNSUPPORTED(wcstoll)
498// UNSUPPORTED(wcstold)
499// UNSUPPORTED(wcstoul)
500// UNSUPPORTED(wcstoull)
501// UNSUPPORTED(wcsxfrm_l)
502// UNSUPPORTED(wcsdup)
503// UNSUPPORTED(wcsftime)
504// UNSUPPORTED(wcsstr)
505// UNSUPPORTED(wcsrchr)
506// UNSUPPORTED(wctob)
507
508INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
509  ENSURE_MSAN_INITED();
510  int res = REAL(gettimeofday)(tv, tz);
511  if (tv)
512    __msan_unpoison(tv, 16);
513  if (tz)
514    __msan_unpoison(tz, 8);
515  return res;
516}
517
518INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
519  ENSURE_MSAN_INITED();
520  char *res = REAL(fcvt)(x, a, b, c);
521  if (!__msan_has_dynamic_component()) {
522    __msan_unpoison(b, sizeof(*b));
523    __msan_unpoison(c, sizeof(*c));
524  }
525  return res;
526}
527
528INTERCEPTOR(char *, getenv, char *name) {
529  ENSURE_MSAN_INITED();
530  char *res = REAL(getenv)(name);
531  if (!__msan_has_dynamic_component()) {
532    if (res)
533      __msan_unpoison(res, REAL(strlen)(res) + 1);
534  }
535  return res;
536}
537
538extern char **environ;
539
540static void UnpoisonEnviron() {
541  char **envp = environ;
542  for (; *envp; ++envp) {
543    __msan_unpoison(envp, sizeof(*envp));
544    __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
545  }
546  // Trailing NULL pointer.
547  __msan_unpoison(envp, sizeof(*envp));
548}
549
550INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
551  ENSURE_MSAN_INITED();
552  int res = REAL(setenv)(name, value, overwrite);
553  if (!res) UnpoisonEnviron();
554  return res;
555}
556
557INTERCEPTOR(int, putenv, char *string) {
558  ENSURE_MSAN_INITED();
559  int res = REAL(putenv)(string);
560  if (!res) UnpoisonEnviron();
561  return res;
562}
563
564INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
565  ENSURE_MSAN_INITED();
566  int res = REAL(__fxstat)(magic, fd, buf);
567  if (!res)
568    __msan_unpoison(buf, __sanitizer::struct_stat_sz);
569  return res;
570}
571
572INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
573  ENSURE_MSAN_INITED();
574  int res = REAL(__fxstat64)(magic, fd, buf);
575  if (!res)
576    __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
577  return res;
578}
579
580INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) {
581  ENSURE_MSAN_INITED();
582  int res = REAL(__xstat)(magic, path, buf);
583  if (!res)
584    __msan_unpoison(buf, __sanitizer::struct_stat_sz);
585  return res;
586}
587
588INTERCEPTOR(int, __xstat64, int magic, char *path, void *buf) {
589  ENSURE_MSAN_INITED();
590  int res = REAL(__xstat64)(magic, path, buf);
591  if (!res)
592    __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
593  return res;
594}
595
596INTERCEPTOR(int, __lxstat, int magic, char *path, void *buf) {
597  ENSURE_MSAN_INITED();
598  int res = REAL(__lxstat)(magic, path, buf);
599  if (!res)
600    __msan_unpoison(buf, __sanitizer::struct_stat_sz);
601  return res;
602}
603
604INTERCEPTOR(int, __lxstat64, int magic, char *path, void *buf) {
605  ENSURE_MSAN_INITED();
606  int res = REAL(__lxstat64)(magic, path, buf);
607  if (!res)
608    __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
609  return res;
610}
611
612INTERCEPTOR(int, pipe, int pipefd[2]) {
613  if (msan_init_is_running)
614    return REAL(pipe)(pipefd);
615  ENSURE_MSAN_INITED();
616  int res = REAL(pipe)(pipefd);
617  if (!res)
618    __msan_unpoison(pipefd, sizeof(int[2]));
619  return res;
620}
621
622INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
623  ENSURE_MSAN_INITED();
624  int res = REAL(pipe2)(pipefd, flags);
625  if (!res)
626    __msan_unpoison(pipefd, sizeof(int[2]));
627  return res;
628}
629
630INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
631  ENSURE_MSAN_INITED();
632  int res = REAL(socketpair)(domain, type, protocol, sv);
633  if (!res)
634    __msan_unpoison(sv, sizeof(int[2]));
635  return res;
636}
637
638INTERCEPTOR(char *, fgets, char *s, int size, void *stream) {
639  ENSURE_MSAN_INITED();
640  char *res = REAL(fgets)(s, size, stream);
641  if (res)
642    __msan_unpoison(s, REAL(strlen)(s) + 1);
643  return res;
644}
645
646INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
647  ENSURE_MSAN_INITED();
648  char *res = REAL(fgets_unlocked)(s, size, stream);
649  if (res)
650    __msan_unpoison(s, REAL(strlen)(s) + 1);
651  return res;
652}
653
654INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
655  if (msan_init_is_running)
656    return REAL(getrlimit)(resource, rlim);
657  ENSURE_MSAN_INITED();
658  int res = REAL(getrlimit)(resource, rlim);
659  if (!res)
660    __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz);
661  return res;
662}
663
664INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
665  if (msan_init_is_running)
666    return REAL(getrlimit64)(resource, rlim);
667  ENSURE_MSAN_INITED();
668  int res = REAL(getrlimit64)(resource, rlim);
669  if (!res)
670    __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
671  return res;
672}
673
674INTERCEPTOR(int, statfs, const char *s, void *buf) {
675  ENSURE_MSAN_INITED();
676  int res = REAL(statfs)(s, buf);
677  if (!res)
678    __msan_unpoison(buf, __sanitizer::struct_statfs_sz);
679  return res;
680}
681
682INTERCEPTOR(int, fstatfs, int fd, void *buf) {
683  ENSURE_MSAN_INITED();
684  int res = REAL(fstatfs)(fd, buf);
685  if (!res)
686    __msan_unpoison(buf, __sanitizer::struct_statfs_sz);
687  return res;
688}
689
690INTERCEPTOR(int, statfs64, const char *s, void *buf) {
691  ENSURE_MSAN_INITED();
692  int res = REAL(statfs64)(s, buf);
693  if (!res)
694    __msan_unpoison(buf, __sanitizer::struct_statfs64_sz);
695  return res;
696}
697
698INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
699  ENSURE_MSAN_INITED();
700  int res = REAL(fstatfs64)(fd, buf);
701  if (!res)
702    __msan_unpoison(buf, __sanitizer::struct_statfs64_sz);
703  return res;
704}
705
706INTERCEPTOR(int, uname, void *utsname) {
707  ENSURE_MSAN_INITED();
708  int res = REAL(uname)(utsname);
709  if (!res) {
710    __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
711  }
712  return res;
713}
714
715INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
716  ENSURE_MSAN_INITED();
717  int res = REAL(gethostname)(name, len);
718  if (!res) {
719    SIZE_T real_len = REAL(strnlen)(name, len);
720    if (real_len < len)
721      ++real_len;
722    __msan_unpoison(name, real_len);
723  }
724  return res;
725}
726
727INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
728    int timeout) {
729  ENSURE_MSAN_INITED();
730  int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
731  if (res > 0) {
732    __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
733  }
734  return res;
735}
736
737INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
738    int timeout, void *sigmask) {
739  ENSURE_MSAN_INITED();
740  int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
741  if (res > 0) {
742    __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
743  }
744  return res;
745}
746
747INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
748  ENSURE_MSAN_INITED();
749  SSIZE_T res = REAL(recv)(fd, buf, len, flags);
750  if (res > 0)
751    __msan_unpoison(buf, res);
752  return res;
753}
754
755INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
756            void *srcaddr, int *addrlen) {
757  ENSURE_MSAN_INITED();
758  SIZE_T srcaddr_sz;
759  if (srcaddr) srcaddr_sz = *addrlen;
760  SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
761  if (res > 0) {
762    __msan_unpoison(buf, res);
763    if (srcaddr) {
764      SIZE_T sz = *addrlen;
765      __msan_unpoison(srcaddr, (sz < srcaddr_sz) ? sz : srcaddr_sz);
766    }
767  }
768  return res;
769}
770
771INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
772  if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0;
773  GET_MALLOC_STACK_TRACE;
774  if (!msan_inited) {
775    // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
776    const SIZE_T kCallocPoolSize = 1024;
777    static uptr calloc_memory_for_dlsym[kCallocPoolSize];
778    static SIZE_T allocated;
779    SIZE_T size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
780    void *mem = (void*)&calloc_memory_for_dlsym[allocated];
781    allocated += size_in_words;
782    CHECK(allocated < kCallocPoolSize);
783    return mem;
784  }
785  return MsanReallocate(&stack, 0, nmemb * size, sizeof(u64), true);
786}
787
788INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
789  GET_MALLOC_STACK_TRACE;
790  return MsanReallocate(&stack, ptr, size, sizeof(u64), false);
791}
792
793INTERCEPTOR(void *, malloc, SIZE_T size) {
794  GET_MALLOC_STACK_TRACE;
795  return MsanReallocate(&stack, 0, size, sizeof(u64), false);
796}
797
798void __msan_allocated_memory(const void* data, uptr size) {
799  GET_MALLOC_STACK_TRACE;
800  if (flags()->poison_in_malloc)
801    __msan_poison(data, size);
802  if (__msan_get_track_origins()) {
803    u32 stack_id = StackDepotPut(stack.trace, stack.size);
804    CHECK(stack_id);
805    CHECK_EQ((stack_id >> 31), 0);  // Higher bit is occupied by stack origins.
806    __msan_set_origin(data, size, stack_id);
807  }
808}
809
810INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
811            int fd, OFF_T offset) {
812  ENSURE_MSAN_INITED();
813  void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
814  if (res != (void*)-1)
815    __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
816  return res;
817}
818
819INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
820            int fd, OFF64_T offset) {
821  ENSURE_MSAN_INITED();
822  void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
823  if (res != (void*)-1)
824    __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
825  return res;
826}
827
828struct dlinfo {
829  char *dli_fname;
830  void *dli_fbase;
831  char *dli_sname;
832  void *dli_saddr;
833};
834
835INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
836  ENSURE_MSAN_INITED();
837  int res = REAL(dladdr)(addr, info);
838  if (res != 0) {
839    __msan_unpoison(info, sizeof(*info));
840    if (info->dli_fname)
841      __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
842    if (info->dli_sname)
843      __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
844  }
845  return res;
846}
847
848// dlopen() ultimately calls mmap() down inside the loader, which generally
849// doesn't participate in dynamic symbol resolution.  Therefore we won't
850// intercept its calls to mmap, and we have to hook it here.  The loader
851// initializes the module before returning, so without the dynamic component, we
852// won't be able to clear the shadow before the initializers.  Fixing this would
853// require putting our own initializer first to clear the shadow.
854INTERCEPTOR(void *, dlopen, const char *filename, int flag) {
855  ENSURE_MSAN_INITED();
856  EnterLoader();
857  link_map *map = (link_map *)REAL(dlopen)(filename, flag);
858  ExitLoader();
859  if (!__msan_has_dynamic_component() && map) {
860    // If msandr didn't clear the shadow before the initializers ran, we do it
861    // ourselves afterwards.
862    ForEachMappedRegion(map, __msan_unpoison);
863  }
864  return (void *)map;
865}
866
867typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
868                                  void *data);
869struct dl_iterate_phdr_data {
870  dl_iterate_phdr_cb callback;
871  void *data;
872};
873
874static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
875                                   void *data) {
876  if (info) {
877    __msan_unpoison(info, size);
878    if (info->dlpi_name)
879      __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
880  }
881  dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
882  UnpoisonParam(3);
883  return cbdata->callback(info, size, cbdata->data);
884}
885
886INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
887  ENSURE_MSAN_INITED();
888  EnterLoader();
889  dl_iterate_phdr_data cbdata;
890  cbdata.callback = callback;
891  cbdata.data = data;
892  int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
893  ExitLoader();
894  return res;
895}
896
897INTERCEPTOR(int, getrusage, int who, void *usage) {
898  ENSURE_MSAN_INITED();
899  int res = REAL(getrusage)(who, usage);
900  if (res == 0) {
901    __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
902  }
903  return res;
904}
905
906// sigactions_mu guarantees atomicity of sigaction() and signal() calls.
907// Access to sigactions[] is gone with relaxed atomics to avoid data race with
908// the signal handler.
909const int kMaxSignals = 1024;
910static atomic_uintptr_t sigactions[kMaxSignals];
911static StaticSpinMutex sigactions_mu;
912
913static void SignalHandler(int signo) {
914  ScopedThreadLocalStateBackup stlsb;
915  stlsb.Backup();
916  UnpoisonParam(1);
917
918  typedef void (*signal_cb)(int x);
919  signal_cb cb =
920      (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
921  cb(signo);
922  stlsb.Restore();
923}
924
925static void SignalAction(int signo, void *si, void *uc) {
926  ScopedThreadLocalStateBackup stlsb;
927  stlsb.Backup();
928  UnpoisonParam(3);
929  __msan_unpoison(si, sizeof(__sanitizer_sigaction));
930  __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
931
932  typedef void (*sigaction_cb)(int, void *, void *);
933  sigaction_cb cb =
934      (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
935  cb(signo, si, uc);
936  stlsb.Restore();
937}
938
939INTERCEPTOR(int, sigaction, int signo, const __sanitizer_sigaction *act,
940            __sanitizer_sigaction *oldact) {
941  ENSURE_MSAN_INITED();
942  // FIXME: check that *act is unpoisoned.
943  // That requires intercepting all of sigemptyset, sigfillset, etc.
944  int res;
945  if (flags()->wrap_signals) {
946    SpinMutexLock lock(&sigactions_mu);
947    CHECK_LT(signo, kMaxSignals);
948    uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
949    __sanitizer_sigaction new_act;
950    __sanitizer_sigaction *pnew_act = act ? &new_act : 0;
951    if (act) {
952      internal_memcpy(pnew_act, act, sizeof(__sanitizer_sigaction));
953      uptr cb = (uptr)pnew_act->sa_sigaction;
954      uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
955                        ? (uptr)SignalAction
956                        : (uptr)SignalHandler;
957      if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
958        atomic_store(&sigactions[signo], cb, memory_order_relaxed);
959        pnew_act->sa_sigaction = (void (*)(int, void *, void *))new_cb;
960      }
961    }
962    res = REAL(sigaction)(signo, pnew_act, oldact);
963    if (res == 0 && oldact) {
964      uptr cb = (uptr)oldact->sa_sigaction;
965      if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
966        oldact->sa_sigaction = (void (*)(int, void *, void *))old_cb;
967      }
968    }
969  } else {
970    res = REAL(sigaction)(signo, act, oldact);
971  }
972
973  if (res == 0 && oldact) {
974    __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
975  }
976  return res;
977}
978
979INTERCEPTOR(int, signal, int signo, uptr cb) {
980  ENSURE_MSAN_INITED();
981  if (flags()->wrap_signals) {
982    CHECK_LT(signo, kMaxSignals);
983    SpinMutexLock lock(&sigactions_mu);
984    if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
985      atomic_store(&sigactions[signo], cb, memory_order_relaxed);
986      cb = (uptr) SignalHandler;
987    }
988    return REAL(signal)(signo, cb);
989  } else {
990    return REAL(signal)(signo, cb);
991  }
992}
993
994extern "C" int pthread_attr_init(void *attr);
995extern "C" int pthread_attr_destroy(void *attr);
996extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize);
997extern "C" int pthread_attr_getstack(void *attr, uptr *stack, uptr *stacksize);
998
999INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1000            void * param) {
1001  ENSURE_MSAN_INITED(); // for GetTlsSize()
1002  __sanitizer_pthread_attr_t myattr;
1003  if (attr == 0) {
1004    pthread_attr_init(&myattr);
1005    attr = &myattr;
1006  }
1007
1008  AdjustStackSizeLinux(attr, flags()->verbosity);
1009
1010  int res = REAL(pthread_create)(th, attr, callback, param);
1011  if (attr == &myattr)
1012    pthread_attr_destroy(&myattr);
1013  if (!res) {
1014    __msan_unpoison(th, __sanitizer::pthread_t_sz);
1015  }
1016  return res;
1017}
1018
1019INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1020            void (*dtor)(void *value)) {
1021  ENSURE_MSAN_INITED();
1022  int res = REAL(pthread_key_create)(key, dtor);
1023  if (!res && key)
1024    __msan_unpoison(key, sizeof(*key));
1025  return res;
1026}
1027
1028INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1029  ENSURE_MSAN_INITED();
1030  int res = REAL(pthread_join)(th, retval);
1031  if (!res && retval)
1032    __msan_unpoison(retval, sizeof(*retval));
1033  return res;
1034}
1035
1036struct MSanInterceptorContext {
1037  bool in_interceptor_scope;
1038};
1039
1040// A version of CHECK_UNPOISED using a saved scope value. Used in common
1041// interceptors.
1042#define CHECK_UNPOISONED_CTX(ctx, x, n)                         \
1043  do {                                                          \
1044    if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1045      CHECK_UNPOISONED_0(x, n);                                 \
1046  } while (0)
1047
1048#define COMMON_INTERCEPTOR_UNPOISON_PARAM(ctx, count)  \
1049  UnpoisonParam(count)
1050#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
1051  __msan_unpoison(ptr, size)
1052#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
1053  CHECK_UNPOISONED_CTX(ctx, ptr, size)
1054#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)              \
1055  if (msan_init_is_running) return REAL(func)(__VA_ARGS__);   \
1056  MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1057  ctx = (void *)&msan_ctx;                                    \
1058  InterceptorScope interceptor_scope;                         \
1059  ENSURE_MSAN_INITED();
1060#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1061  do {                                         \
1062  } while (false)
1063#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1064  do {                                         \
1065  } while (false)
1066#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1067  do {                                                      \
1068  } while (false)
1069#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
1070  do {                                                \
1071  } while (false)  // FIXME
1072#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
1073#include "sanitizer_common/sanitizer_common_interceptors.inc"
1074
1075#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
1076#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s)
1077#define COMMON_SYSCALL_POST_READ_RANGE(p, s)
1078#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1079#include "sanitizer_common/sanitizer_common_syscalls.inc"
1080
1081// static
1082void *fast_memset(void *ptr, int c, SIZE_T n) {
1083  // hack until we have a really fast internal_memset
1084  if (sizeof(uptr) == 8 &&
1085      (n % 8) == 0 &&
1086      ((uptr)ptr % 8) == 0 &&
1087      (c == 0 || c == -1)) {
1088    // Printf("memset %p %zd %x\n", ptr, n, c);
1089    uptr to_store = c ? -1L : 0L;
1090    uptr *p = (uptr*)ptr;
1091    for (SIZE_T i = 0; i < n / 8; i++)
1092      p[i] = to_store;
1093    return ptr;
1094  }
1095  return internal_memset(ptr, c, n);
1096}
1097
1098// static
1099void *fast_memcpy(void *dst, const void *src, SIZE_T n) {
1100  // Same hack as in fast_memset above.
1101  if (sizeof(uptr) == 8 &&
1102      (n % 8) == 0 &&
1103      ((uptr)dst % 8) == 0 &&
1104      ((uptr)src % 8) == 0) {
1105    uptr *d = (uptr*)dst;
1106    uptr *s = (uptr*)src;
1107    for (SIZE_T i = 0; i < n / 8; i++)
1108      d[i] = s[i];
1109    return dst;
1110  }
1111  return internal_memcpy(dst, src, n);
1112}
1113
1114// These interface functions reside here so that they can use
1115// fast_memset, etc.
1116void __msan_unpoison(const void *a, uptr size) {
1117  if (!MEM_IS_APP(a)) return;
1118  fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size);
1119}
1120
1121void __msan_poison(const void *a, uptr size) {
1122  if (!MEM_IS_APP(a)) return;
1123  fast_memset((void*)MEM_TO_SHADOW((uptr)a),
1124              __msan::flags()->poison_heap_with_zeroes ? 0 : -1, size);
1125}
1126
1127void __msan_poison_stack(void *a, uptr size) {
1128  if (!MEM_IS_APP(a)) return;
1129  fast_memset((void*)MEM_TO_SHADOW((uptr)a),
1130              __msan::flags()->poison_stack_with_zeroes ? 0 : -1, size);
1131}
1132
1133void __msan_clear_and_unpoison(void *a, uptr size) {
1134  fast_memset(a, 0, size);
1135  fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size);
1136}
1137
1138void __msan_copy_origin(void *dst, const void *src, uptr size) {
1139  if (!__msan_get_track_origins()) return;
1140  if (!MEM_IS_APP(dst) || !MEM_IS_APP(src)) return;
1141  uptr d = MEM_TO_ORIGIN(dst);
1142  uptr s = MEM_TO_ORIGIN(src);
1143  uptr beg = d & ~3UL;  // align down.
1144  uptr end = (d + size + 3) & ~3UL;  // align up.
1145  s = s & ~3UL;  // align down.
1146  fast_memcpy((void*)beg, (void*)s, end - beg);
1147}
1148
1149void __msan_copy_poison(void *dst, const void *src, uptr size) {
1150  if (!MEM_IS_APP(dst)) return;
1151  if (!MEM_IS_APP(src)) return;
1152  fast_memcpy((void*)MEM_TO_SHADOW((uptr)dst),
1153              (void*)MEM_TO_SHADOW((uptr)src), size);
1154  __msan_copy_origin(dst, src, size);
1155}
1156
1157void __msan_move_poison(void *dst, const void *src, uptr size) {
1158  if (!MEM_IS_APP(dst)) return;
1159  if (!MEM_IS_APP(src)) return;
1160  internal_memmove((void*)MEM_TO_SHADOW((uptr)dst),
1161         (void*)MEM_TO_SHADOW((uptr)src), size);
1162  __msan_copy_origin(dst, src, size);
1163}
1164
1165void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
1166  ENSURE_MSAN_INITED();
1167  void *res = fast_memcpy(dest, src, n);
1168  __msan_copy_poison(dest, src, n);
1169  return res;
1170}
1171
1172void *__msan_memset(void *s, int c, SIZE_T n) {
1173  ENSURE_MSAN_INITED();
1174  void *res = fast_memset(s, c, n);
1175  __msan_unpoison(s, n);
1176  return res;
1177}
1178
1179void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
1180  ENSURE_MSAN_INITED();
1181  void *res = REAL(memmove)(dest, src, n);
1182  __msan_move_poison(dest, src, n);
1183  return res;
1184}
1185
1186namespace __msan {
1187void InitializeInterceptors() {
1188  static int inited = 0;
1189  CHECK_EQ(inited, 0);
1190  SANITIZER_COMMON_INTERCEPTORS_INIT;
1191
1192  INTERCEPT_FUNCTION(mmap);
1193  INTERCEPT_FUNCTION(mmap64);
1194  INTERCEPT_FUNCTION(posix_memalign);
1195  INTERCEPT_FUNCTION(malloc);
1196  INTERCEPT_FUNCTION(calloc);
1197  INTERCEPT_FUNCTION(realloc);
1198  INTERCEPT_FUNCTION(free);
1199  INTERCEPT_FUNCTION(fread);
1200  INTERCEPT_FUNCTION(fread_unlocked);
1201  INTERCEPT_FUNCTION(readlink);
1202  INTERCEPT_FUNCTION(memcpy);
1203  INTERCEPT_FUNCTION(mempcpy);
1204  INTERCEPT_FUNCTION(memset);
1205  INTERCEPT_FUNCTION(memmove);
1206  INTERCEPT_FUNCTION(bcopy);
1207  INTERCEPT_FUNCTION(wmemset);
1208  INTERCEPT_FUNCTION(wmemcpy);
1209  INTERCEPT_FUNCTION(wmempcpy);
1210  INTERCEPT_FUNCTION(wmemmove);
1211  INTERCEPT_FUNCTION(strcpy);  // NOLINT
1212  INTERCEPT_FUNCTION(stpcpy);  // NOLINT
1213  INTERCEPT_FUNCTION(strdup);
1214  INTERCEPT_FUNCTION(__strdup);
1215  INTERCEPT_FUNCTION(strndup);
1216  INTERCEPT_FUNCTION(__strndup);
1217  INTERCEPT_FUNCTION(strncpy);  // NOLINT
1218  INTERCEPT_FUNCTION(strlen);
1219  INTERCEPT_FUNCTION(strnlen);
1220  INTERCEPT_FUNCTION(gcvt);
1221  INTERCEPT_FUNCTION(strcat);  // NOLINT
1222  INTERCEPT_FUNCTION(strncat);  // NOLINT
1223  INTERCEPT_FUNCTION(strtol);
1224  INTERCEPT_FUNCTION(strtoll);
1225  INTERCEPT_FUNCTION(strtoul);
1226  INTERCEPT_FUNCTION(strtoull);
1227  INTERCEPT_FUNCTION(strtod);
1228  INTERCEPT_FUNCTION(strtof);
1229  INTERCEPT_FUNCTION(strtold);
1230  INTERCEPT_FUNCTION(vasprintf);
1231  INTERCEPT_FUNCTION(asprintf);
1232  INTERCEPT_FUNCTION(vsprintf);
1233  INTERCEPT_FUNCTION(vsnprintf);
1234  INTERCEPT_FUNCTION(vswprintf);
1235  INTERCEPT_FUNCTION(sprintf);  // NOLINT
1236  INTERCEPT_FUNCTION(snprintf);
1237  INTERCEPT_FUNCTION(swprintf);
1238  INTERCEPT_FUNCTION(strftime);
1239  INTERCEPT_FUNCTION(mbtowc);
1240  INTERCEPT_FUNCTION(mbrtowc);
1241  INTERCEPT_FUNCTION(wcslen);
1242  INTERCEPT_FUNCTION(wcschr);
1243  INTERCEPT_FUNCTION(wcscpy);
1244  INTERCEPT_FUNCTION(wcscmp);
1245  INTERCEPT_FUNCTION(wcstod);
1246  INTERCEPT_FUNCTION(getenv);
1247  INTERCEPT_FUNCTION(setenv);
1248  INTERCEPT_FUNCTION(putenv);
1249  INTERCEPT_FUNCTION(gettimeofday);
1250  INTERCEPT_FUNCTION(fcvt);
1251  INTERCEPT_FUNCTION(__fxstat);
1252  INTERCEPT_FUNCTION(__xstat);
1253  INTERCEPT_FUNCTION(__lxstat);
1254  INTERCEPT_FUNCTION(__fxstat64);
1255  INTERCEPT_FUNCTION(__xstat64);
1256  INTERCEPT_FUNCTION(__lxstat64);
1257  INTERCEPT_FUNCTION(pipe);
1258  INTERCEPT_FUNCTION(pipe2);
1259  INTERCEPT_FUNCTION(socketpair);
1260  INTERCEPT_FUNCTION(fgets);
1261  INTERCEPT_FUNCTION(fgets_unlocked);
1262  INTERCEPT_FUNCTION(getrlimit);
1263  INTERCEPT_FUNCTION(getrlimit64);
1264  INTERCEPT_FUNCTION(statfs);
1265  INTERCEPT_FUNCTION(fstatfs);
1266  INTERCEPT_FUNCTION(statfs64);
1267  INTERCEPT_FUNCTION(fstatfs64);
1268  INTERCEPT_FUNCTION(uname);
1269  INTERCEPT_FUNCTION(gethostname);
1270  INTERCEPT_FUNCTION(epoll_wait);
1271  INTERCEPT_FUNCTION(epoll_pwait);
1272  INTERCEPT_FUNCTION(recv);
1273  INTERCEPT_FUNCTION(recvfrom);
1274  INTERCEPT_FUNCTION(dladdr);
1275  INTERCEPT_FUNCTION(dlopen);
1276  INTERCEPT_FUNCTION(dl_iterate_phdr);
1277  INTERCEPT_FUNCTION(getrusage);
1278  INTERCEPT_FUNCTION(sigaction);
1279  INTERCEPT_FUNCTION(signal);
1280  INTERCEPT_FUNCTION(pthread_create);
1281  INTERCEPT_FUNCTION(pthread_key_create);
1282  INTERCEPT_FUNCTION(pthread_join);
1283  inited = 1;
1284}
1285}  // namespace __msan
1286