1//===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
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// Common function interceptors for tools like AddressSanitizer,
11// ThreadSanitizer, MemorySanitizer, etc.
12//
13// This file should be included into the tool's interceptor file,
14// which has to define its own macros:
15//   COMMON_INTERCEPTOR_ENTER
16//   COMMON_INTERCEPTOR_ENTER_NOIGNORE
17//   COMMON_INTERCEPTOR_READ_RANGE
18//   COMMON_INTERCEPTOR_WRITE_RANGE
19//   COMMON_INTERCEPTOR_INITIALIZE_RANGE
20//   COMMON_INTERCEPTOR_DIR_ACQUIRE
21//   COMMON_INTERCEPTOR_FD_ACQUIRE
22//   COMMON_INTERCEPTOR_FD_RELEASE
23//   COMMON_INTERCEPTOR_FD_ACCESS
24//   COMMON_INTERCEPTOR_SET_THREAD_NAME
25//   COMMON_INTERCEPTOR_ON_DLOPEN
26//   COMMON_INTERCEPTOR_ON_EXIT
27//   COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
28//   COMMON_INTERCEPTOR_MUTEX_POST_LOCK
29//   COMMON_INTERCEPTOR_MUTEX_UNLOCK
30//   COMMON_INTERCEPTOR_MUTEX_REPAIR
31//   COMMON_INTERCEPTOR_SET_PTHREAD_NAME
32//   COMMON_INTERCEPTOR_HANDLE_RECVMSG
33//   COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
34//   COMMON_INTERCEPTOR_MEMSET_IMPL
35//   COMMON_INTERCEPTOR_MEMMOVE_IMPL
36//   COMMON_INTERCEPTOR_MEMCPY_IMPL
37//   COMMON_INTERCEPTOR_MMAP_IMPL
38//   COMMON_INTERCEPTOR_COPY_STRING
39//   COMMON_INTERCEPTOR_STRNDUP_IMPL
40//===----------------------------------------------------------------------===//
41
42#include "interception/interception.h"
43#include "sanitizer_addrhashmap.h"
44#include "sanitizer_errno.h"
45#include "sanitizer_placement_new.h"
46#include "sanitizer_platform_interceptors.h"
47#include "sanitizer_symbolizer.h"
48#include "sanitizer_tls_get_addr.h"
49
50#include <stdarg.h>
51
52#if SANITIZER_INTERCEPTOR_HOOKS
53#define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
54#define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
55  SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
56#else
57#define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
58#define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
59
60#endif  // SANITIZER_INTERCEPTOR_HOOKS
61
62#if SANITIZER_WINDOWS && !defined(va_copy)
63#define va_copy(dst, src) ((dst) = (src))
64#endif // _WIN32
65
66#if SANITIZER_FREEBSD
67#define pthread_setname_np pthread_set_name_np
68#define inet_aton __inet_aton
69#define inet_pton __inet_pton
70#define iconv __bsd_iconv
71#endif
72
73#if SANITIZER_NETBSD
74#define clock_getres __clock_getres50
75#define clock_gettime __clock_gettime50
76#define clock_settime __clock_settime50
77#define ctime __ctime50
78#define ctime_r __ctime_r50
79#define devname __devname50
80#define fgetpos __fgetpos50
81#define fsetpos __fsetpos50
82#define fts_children __fts_children60
83#define fts_close __fts_close60
84#define fts_open __fts_open60
85#define fts_read __fts_read60
86#define fts_set __fts_set60
87#define getitimer __getitimer50
88#define getmntinfo __getmntinfo13
89#define getpwent __getpwent50
90#define getpwnam __getpwnam50
91#define getpwnam_r __getpwnam_r50
92#define getpwuid __getpwuid50
93#define getpwuid_r __getpwuid_r50
94#define getutent __getutent50
95#define getutxent __getutxent50
96#define getutxid __getutxid50
97#define getutxline __getutxline50
98#define pututxline __pututxline50
99#define glob __glob30
100#define gmtime __gmtime50
101#define gmtime_r __gmtime_r50
102#define localtime __locatime50
103#define localtime_r __localtime_r50
104#define mktime __mktime50
105#define lstat __lstat50
106#define opendir __opendir30
107#define readdir __readdir30
108#define readdir_r __readdir_r30
109#define scandir __scandir30
110#define setitimer __setitimer50
111#define setlocale __setlocale50
112#define shmctl __shmctl50
113#define sigemptyset __sigemptyset14
114#define sigfillset __sigfillset14
115#define sigpending __sigpending14
116#define sigprocmask __sigprocmask14
117#define sigtimedwait __sigtimedwait50
118#define stat __stat50
119#define time __time50
120#define times __times13
121#define unvis __unvis50
122#define wait3 __wait350
123#define wait4 __wait450
124extern const unsigned short *_ctype_tab_;
125extern const short *_toupper_tab_;
126extern const short *_tolower_tab_;
127#endif
128
129// Platform-specific options.
130#if SANITIZER_MAC
131namespace __sanitizer {
132bool PlatformHasDifferentMemcpyAndMemmove();
133}
134#define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
135  (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
136#elif SANITIZER_WINDOWS64
137#define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
138#else
139#define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
140#endif  // SANITIZER_MAC
141
142#ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
143#define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
144#endif
145
146#ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
147#define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
148#endif
149
150#ifndef COMMON_INTERCEPTOR_FD_ACCESS
151#define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
152#endif
153
154#ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
155#define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
156#endif
157
158#ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
159#define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
160#endif
161
162#ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
163#define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
164#endif
165
166#ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
167#define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
168#endif
169
170#ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
171#define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
172#endif
173
174#ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
175#define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
176#endif
177
178#ifndef COMMON_INTERCEPTOR_FILE_OPEN
179#define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
180#endif
181
182#ifndef COMMON_INTERCEPTOR_FILE_CLOSE
183#define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
184#endif
185
186#ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
187#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
188#endif
189
190#ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
191#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
192#endif
193
194#ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
195#define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
196  COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
197#endif
198
199#ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
200#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
201#endif
202
203#define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n)                   \
204    COMMON_INTERCEPTOR_READ_RANGE((ctx), (s),                       \
205      common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
206
207#ifndef COMMON_INTERCEPTOR_ON_DLOPEN
208#define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
209  CheckNoDeepBind(filename, flag);
210#endif
211
212#ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
213#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
214#endif
215
216#ifndef COMMON_INTERCEPTOR_ACQUIRE
217#define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
218#endif
219
220#ifndef COMMON_INTERCEPTOR_RELEASE
221#define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
222#endif
223
224#ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
225#define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
226#endif
227
228#ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
229#define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
230#endif
231
232#ifdef SANITIZER_NLDBL_VERSION
233#define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
234    COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
235#else
236#define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
237    COMMON_INTERCEPT_FUNCTION(fn)
238#endif
239
240#ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
241#define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
242  {                                                       \
243    if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)        \
244      return internal_memset(dst, v, size);               \
245    COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size);  \
246    if (common_flags()->intercept_intrin)                 \
247      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);     \
248    return REAL(memset)(dst, v, size);                    \
249  }
250#endif
251
252#ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
253#define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
254  {                                                          \
255    if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)           \
256      return internal_memmove(dst, src, size);               \
257    COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size);  \
258    if (common_flags()->intercept_intrin) {                  \
259      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);        \
260      COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);         \
261    }                                                        \
262    return REAL(memmove)(dst, src, size);                    \
263  }
264#endif
265
266#ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
267#define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
268  {                                                         \
269    if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {        \
270      return internal_memmove(dst, src, size);              \
271    }                                                       \
272    COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size);  \
273    if (common_flags()->intercept_intrin) {                 \
274      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);       \
275      COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);        \
276    }                                                       \
277    return REAL(memcpy)(dst, src, size);                    \
278  }
279#endif
280
281#ifndef COMMON_INTERCEPTOR_MMAP_IMPL
282#define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
283                                     off)                                  \
284  { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
285#endif
286
287#ifndef COMMON_INTERCEPTOR_COPY_STRING
288#define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
289#endif
290
291#ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
292#define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size)                         \
293  COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size);                            \
294  uptr copy_length = internal_strnlen(s, size);                               \
295  char *new_mem = (char *)WRAP(malloc)(copy_length + 1);                      \
296  if (common_flags()->intercept_strndup) {                                    \
297    COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1));       \
298  }                                                                           \
299  COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length);               \
300  internal_memcpy(new_mem, s, copy_length);                                   \
301  new_mem[copy_length] = '\0';                                                \
302  return new_mem;
303#endif
304
305struct FileMetadata {
306  // For open_memstream().
307  char **addr;
308  SIZE_T *size;
309};
310
311struct CommonInterceptorMetadata {
312  enum {
313    CIMT_INVALID = 0,
314    CIMT_FILE
315  } type;
316  union {
317    FileMetadata file;
318  };
319};
320
321typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
322
323static MetadataHashMap *interceptor_metadata_map;
324
325#if SI_POSIX
326UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
327                                          const FileMetadata &file) {
328  MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
329  CHECK(h.created());
330  h->type = CommonInterceptorMetadata::CIMT_FILE;
331  h->file = file;
332}
333
334UNUSED static const FileMetadata *GetInterceptorMetadata(
335    __sanitizer_FILE *addr) {
336  MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
337                            /* remove */ false,
338                            /* create */ false);
339  if (addr && h.exists()) {
340    CHECK(!h.created());
341    CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
342    return &h->file;
343  } else {
344    return 0;
345  }
346}
347
348UNUSED static void DeleteInterceptorMetadata(void *addr) {
349  MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
350  CHECK(h.exists());
351}
352#endif  // SI_POSIX
353
354#if SANITIZER_INTERCEPT_STRLEN
355INTERCEPTOR(SIZE_T, strlen, const char *s) {
356  // Sometimes strlen is called prior to InitializeCommonInterceptors,
357  // in which case the REAL(strlen) typically used in
358  // COMMON_INTERCEPTOR_ENTER will fail.  We use internal_strlen here
359  // to handle that.
360  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
361    return internal_strlen(s);
362  void *ctx;
363  COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
364  SIZE_T result = REAL(strlen)(s);
365  if (common_flags()->intercept_strlen)
366    COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
367  return result;
368}
369#define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
370#else
371#define INIT_STRLEN
372#endif
373
374#if SANITIZER_INTERCEPT_STRNLEN
375INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
376  void *ctx;
377  COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
378  SIZE_T length = REAL(strnlen)(s, maxlen);
379  if (common_flags()->intercept_strlen)
380    COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
381  return length;
382}
383#define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
384#else
385#define INIT_STRNLEN
386#endif
387
388#if SANITIZER_INTERCEPT_STRNDUP
389INTERCEPTOR(char*, strndup, const char *s, uptr size) {
390  void *ctx;
391  COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
392}
393#define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
394#else
395#define INIT_STRNDUP
396#endif // SANITIZER_INTERCEPT_STRNDUP
397
398#if SANITIZER_INTERCEPT___STRNDUP
399INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
400  void *ctx;
401  COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
402}
403#define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
404#else
405#define INIT___STRNDUP
406#endif // SANITIZER_INTERCEPT___STRNDUP
407
408#if SANITIZER_INTERCEPT_TEXTDOMAIN
409INTERCEPTOR(char*, textdomain, const char *domainname) {
410  void *ctx;
411  COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
412  if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
413  char *domain = REAL(textdomain)(domainname);
414  if (domain) {
415    COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
416  }
417  return domain;
418}
419#define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
420#else
421#define INIT_TEXTDOMAIN
422#endif
423
424#if SANITIZER_INTERCEPT_STRCMP
425static inline int CharCmpX(unsigned char c1, unsigned char c2) {
426  return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
427}
428
429DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
430                              const char *s1, const char *s2, int result)
431
432INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
433  void *ctx;
434  COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
435  unsigned char c1, c2;
436  uptr i;
437  for (i = 0;; i++) {
438    c1 = (unsigned char)s1[i];
439    c2 = (unsigned char)s2[i];
440    if (c1 != c2 || c1 == '\0') break;
441  }
442  COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
443  COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
444  int result = CharCmpX(c1, c2);
445  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
446                             s2, result);
447  return result;
448}
449
450DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
451                              const char *s1, const char *s2, uptr n,
452                              int result)
453
454INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
455  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
456    return internal_strncmp(s1, s2, size);
457  void *ctx;
458  COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
459  unsigned char c1 = 0, c2 = 0;
460  uptr i;
461  for (i = 0; i < size; i++) {
462    c1 = (unsigned char)s1[i];
463    c2 = (unsigned char)s2[i];
464    if (c1 != c2 || c1 == '\0') break;
465  }
466  uptr i1 = i;
467  uptr i2 = i;
468  if (common_flags()->strict_string_checks) {
469    for (; i1 < size && s1[i1]; i1++) {}
470    for (; i2 < size && s2[i2]; i2++) {}
471  }
472  COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
473  COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
474  int result = CharCmpX(c1, c2);
475  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
476                             s2, size, result);
477  return result;
478}
479
480#define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
481#define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
482#else
483#define INIT_STRCMP
484#define INIT_STRNCMP
485#endif
486
487#if SANITIZER_INTERCEPT_STRCASECMP
488static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
489  int c1_low = ToLower(c1);
490  int c2_low = ToLower(c2);
491  return c1_low - c2_low;
492}
493
494DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
495                              const char *s1, const char *s2, int result)
496
497INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
498  void *ctx;
499  COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
500  unsigned char c1 = 0, c2 = 0;
501  uptr i;
502  for (i = 0;; i++) {
503    c1 = (unsigned char)s1[i];
504    c2 = (unsigned char)s2[i];
505    if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
506  }
507  COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
508  COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
509  int result = CharCaseCmp(c1, c2);
510  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
511                             s1, s2, result);
512  return result;
513}
514
515DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
516                              const char *s1, const char *s2, uptr size,
517                              int result)
518
519INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
520  void *ctx;
521  COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
522  unsigned char c1 = 0, c2 = 0;
523  uptr i;
524  for (i = 0; i < size; i++) {
525    c1 = (unsigned char)s1[i];
526    c2 = (unsigned char)s2[i];
527    if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
528  }
529  uptr i1 = i;
530  uptr i2 = i;
531  if (common_flags()->strict_string_checks) {
532    for (; i1 < size && s1[i1]; i1++) {}
533    for (; i2 < size && s2[i2]; i2++) {}
534  }
535  COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
536  COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
537  int result = CharCaseCmp(c1, c2);
538  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
539                             s1, s2, size, result);
540  return result;
541}
542
543#define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
544#define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
545#else
546#define INIT_STRCASECMP
547#define INIT_STRNCASECMP
548#endif
549
550#if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
551static inline void StrstrCheck(void *ctx, char *r, const char *s1,
552                               const char *s2) {
553    uptr len1 = REAL(strlen)(s1);
554    uptr len2 = REAL(strlen)(s2);
555    COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
556    COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
557}
558#endif
559
560#if SANITIZER_INTERCEPT_STRSTR
561
562DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
563                              const char *s1, const char *s2, char *result)
564
565INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
566  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
567    return internal_strstr(s1, s2);
568  void *ctx;
569  COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
570  char *r = REAL(strstr)(s1, s2);
571  if (common_flags()->intercept_strstr)
572    StrstrCheck(ctx, r, s1, s2);
573  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
574                             s2, r);
575  return r;
576}
577
578#define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
579#else
580#define INIT_STRSTR
581#endif
582
583#if SANITIZER_INTERCEPT_STRCASESTR
584
585DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
586                              const char *s1, const char *s2, char *result)
587
588INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
589  void *ctx;
590  COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
591  char *r = REAL(strcasestr)(s1, s2);
592  if (common_flags()->intercept_strstr)
593    StrstrCheck(ctx, r, s1, s2);
594  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
595                             s1, s2, r);
596  return r;
597}
598
599#define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
600#else
601#define INIT_STRCASESTR
602#endif
603
604#if SANITIZER_INTERCEPT_STRTOK
605
606INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
607  void *ctx;
608  COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
609  if (!common_flags()->intercept_strtok) {
610    return REAL(strtok)(str, delimiters);
611  }
612  if (common_flags()->strict_string_checks) {
613    // If strict_string_checks is enabled, we check the whole first argument
614    // string on the first call (strtok saves this string in a static buffer
615    // for subsequent calls). We do not need to check strtok's result.
616    // As the delimiters can change, we check them every call.
617    if (str != nullptr) {
618      COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
619    }
620    COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
621                                  REAL(strlen)(delimiters) + 1);
622    return REAL(strtok)(str, delimiters);
623  } else {
624    // However, when strict_string_checks is disabled we cannot check the
625    // whole string on the first call. Instead, we check the result string
626    // which is guaranteed to be a NULL-terminated substring of the first
627    // argument. We also conservatively check one character of str and the
628    // delimiters.
629    if (str != nullptr) {
630      COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
631    }
632    COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
633    char *result = REAL(strtok)(str, delimiters);
634    if (result != nullptr) {
635      COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
636    } else if (str != nullptr) {
637      // No delimiter were found, it's safe to assume that the entire str was
638      // scanned.
639      COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
640    }
641    return result;
642  }
643}
644
645#define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
646#else
647#define INIT_STRTOK
648#endif
649
650#if SANITIZER_INTERCEPT_MEMMEM
651DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
652                              const void *s1, SIZE_T len1, const void *s2,
653                              SIZE_T len2, void *result)
654
655INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
656            SIZE_T len2) {
657  void *ctx;
658  COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
659  void *r = REAL(memmem)(s1, len1, s2, len2);
660  if (common_flags()->intercept_memmem) {
661    COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
662    COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
663  }
664  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
665                             s1, len1, s2, len2, r);
666  return r;
667}
668
669#define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
670#else
671#define INIT_MEMMEM
672#endif  // SANITIZER_INTERCEPT_MEMMEM
673
674#if SANITIZER_INTERCEPT_STRCHR
675INTERCEPTOR(char*, strchr, const char *s, int c) {
676  void *ctx;
677  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
678    return internal_strchr(s, c);
679  COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
680  char *result = REAL(strchr)(s, c);
681  if (common_flags()->intercept_strchr) {
682    // Keep strlen as macro argument, as macro may ignore it.
683    COMMON_INTERCEPTOR_READ_STRING(ctx, s,
684      (result ? result - s : REAL(strlen)(s)) + 1);
685  }
686  return result;
687}
688#define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
689#else
690#define INIT_STRCHR
691#endif
692
693#if SANITIZER_INTERCEPT_STRCHRNUL
694INTERCEPTOR(char*, strchrnul, const char *s, int c) {
695  void *ctx;
696  COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
697  char *result = REAL(strchrnul)(s, c);
698  uptr len = result - s + 1;
699  if (common_flags()->intercept_strchr)
700    COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
701  return result;
702}
703#define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
704#else
705#define INIT_STRCHRNUL
706#endif
707
708#if SANITIZER_INTERCEPT_STRRCHR
709INTERCEPTOR(char*, strrchr, const char *s, int c) {
710  void *ctx;
711  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
712    return internal_strrchr(s, c);
713  COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
714  if (common_flags()->intercept_strchr)
715    COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
716  return REAL(strrchr)(s, c);
717}
718#define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
719#else
720#define INIT_STRRCHR
721#endif
722
723#if SANITIZER_INTERCEPT_STRSPN
724INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
725  void *ctx;
726  COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
727  SIZE_T r = REAL(strspn)(s1, s2);
728  if (common_flags()->intercept_strspn) {
729    COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
730    COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
731  }
732  return r;
733}
734
735INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
736  void *ctx;
737  COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
738  SIZE_T r = REAL(strcspn)(s1, s2);
739  if (common_flags()->intercept_strspn) {
740    COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
741    COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
742  }
743  return r;
744}
745
746#define INIT_STRSPN \
747  COMMON_INTERCEPT_FUNCTION(strspn); \
748  COMMON_INTERCEPT_FUNCTION(strcspn);
749#else
750#define INIT_STRSPN
751#endif
752
753#if SANITIZER_INTERCEPT_STRPBRK
754INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
755  void *ctx;
756  COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
757  char *r = REAL(strpbrk)(s1, s2);
758  if (common_flags()->intercept_strpbrk) {
759    COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
760    COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
761        r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
762  }
763  return r;
764}
765
766#define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
767#else
768#define INIT_STRPBRK
769#endif
770
771#if SANITIZER_INTERCEPT_MEMSET
772INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
773  void *ctx;
774  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
775}
776
777#define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
778#else
779#define INIT_MEMSET
780#endif
781
782#if SANITIZER_INTERCEPT_MEMMOVE
783INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
784  void *ctx;
785  COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
786}
787
788#define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
789#else
790#define INIT_MEMMOVE
791#endif
792
793#if SANITIZER_INTERCEPT_MEMCPY
794INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
795  // On OS X, calling internal_memcpy here will cause memory corruptions,
796  // because memcpy and memmove are actually aliases of the same
797  // implementation.  We need to use internal_memmove here.
798  // N.B.: If we switch this to internal_ we'll have to use internal_memmove
799  // due to memcpy being an alias of memmove on OS X.
800  void *ctx;
801  if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
802    COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
803  } else {
804    COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
805  }
806}
807
808#define INIT_MEMCPY                                  \
809  do {                                               \
810    if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
811      COMMON_INTERCEPT_FUNCTION(memcpy);             \
812    } else {                                         \
813      ASSIGN_REAL(memcpy, memmove);                  \
814    }                                                \
815    CHECK(REAL(memcpy));                             \
816  } while (false)
817
818#else
819#define INIT_MEMCPY
820#endif
821
822#if SANITIZER_INTERCEPT_MEMCMP
823
824DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
825                              const void *s1, const void *s2, uptr n,
826                              int result)
827
828INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
829  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
830    return internal_memcmp(a1, a2, size);
831  void *ctx;
832  COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
833  if (common_flags()->intercept_memcmp) {
834    if (common_flags()->strict_memcmp) {
835      // Check the entire regions even if the first bytes of the buffers are
836      // different.
837      COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
838      COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
839      // Fallthrough to REAL(memcmp) below.
840    } else {
841      unsigned char c1 = 0, c2 = 0;
842      const unsigned char *s1 = (const unsigned char*)a1;
843      const unsigned char *s2 = (const unsigned char*)a2;
844      uptr i;
845      for (i = 0; i < size; i++) {
846        c1 = s1[i];
847        c2 = s2[i];
848        if (c1 != c2) break;
849      }
850      COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
851      COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
852      int r = CharCmpX(c1, c2);
853      CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
854                                 a1, a2, size, r);
855      return r;
856    }
857  }
858  int result = REAL(memcmp(a1, a2, size));
859  CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
860                             a2, size, result);
861  return result;
862}
863
864#define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
865#else
866#define INIT_MEMCMP
867#endif
868
869#if SANITIZER_INTERCEPT_MEMCHR
870INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
871  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
872    return internal_memchr(s, c, n);
873  void *ctx;
874  COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
875#if SANITIZER_WINDOWS
876  void *res;
877  if (REAL(memchr)) {
878    res = REAL(memchr)(s, c, n);
879  } else {
880    res = internal_memchr(s, c, n);
881  }
882#else
883  void *res = REAL(memchr)(s, c, n);
884#endif
885  uptr len = res ? (char *)res - (const char *)s + 1 : n;
886  COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
887  return res;
888}
889
890#define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
891#else
892#define INIT_MEMCHR
893#endif
894
895#if SANITIZER_INTERCEPT_MEMRCHR
896INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
897  void *ctx;
898  COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
899  COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
900  return REAL(memrchr)(s, c, n);
901}
902
903#define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
904#else
905#define INIT_MEMRCHR
906#endif
907
908#if SANITIZER_INTERCEPT_FREXP
909INTERCEPTOR(double, frexp, double x, int *exp) {
910  void *ctx;
911  COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
912  // Assuming frexp() always writes to |exp|.
913  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
914  double res = REAL(frexp)(x, exp);
915  return res;
916}
917
918#define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
919#else
920#define INIT_FREXP
921#endif  // SANITIZER_INTERCEPT_FREXP
922
923#if SANITIZER_INTERCEPT_FREXPF_FREXPL
924INTERCEPTOR(float, frexpf, float x, int *exp) {
925  void *ctx;
926  COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
927  // FIXME: under ASan the call below may write to freed memory and corrupt
928  // its metadata. See
929  // https://github.com/google/sanitizers/issues/321.
930  float res = REAL(frexpf)(x, exp);
931  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
932  return res;
933}
934
935INTERCEPTOR(long double, frexpl, long double x, int *exp) {
936  void *ctx;
937  COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
938  // FIXME: under ASan the call below may write to freed memory and corrupt
939  // its metadata. See
940  // https://github.com/google/sanitizers/issues/321.
941  long double res = REAL(frexpl)(x, exp);
942  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
943  return res;
944}
945
946#define INIT_FREXPF_FREXPL           \
947  COMMON_INTERCEPT_FUNCTION(frexpf); \
948  COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
949#else
950#define INIT_FREXPF_FREXPL
951#endif  // SANITIZER_INTERCEPT_FREXPF_FREXPL
952
953#if SI_POSIX
954static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
955                        SIZE_T iovlen, SIZE_T maxlen) {
956  for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
957    SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
958    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
959    maxlen -= sz;
960  }
961}
962
963static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
964                       SIZE_T iovlen, SIZE_T maxlen) {
965  COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
966  for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
967    SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
968    COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
969    maxlen -= sz;
970  }
971}
972#endif
973
974#if SANITIZER_INTERCEPT_READ
975INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
976  void *ctx;
977  COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
978  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
979  // FIXME: under ASan the call below may write to freed memory and corrupt
980  // its metadata. See
981  // https://github.com/google/sanitizers/issues/321.
982  SSIZE_T res = REAL(read)(fd, ptr, count);
983  if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
984  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
985  return res;
986}
987#define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
988#else
989#define INIT_READ
990#endif
991
992#if SANITIZER_INTERCEPT_FREAD
993INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
994  // libc file streams can call user-supplied functions, see fopencookie.
995  void *ctx;
996  COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
997  // FIXME: under ASan the call below may write to freed memory and corrupt
998  // its metadata. See
999  // https://github.com/google/sanitizers/issues/321.
1000  SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
1001  if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
1002  return res;
1003}
1004#define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1005#else
1006#define INIT_FREAD
1007#endif
1008
1009#if SANITIZER_INTERCEPT_PREAD
1010INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1011  void *ctx;
1012  COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1013  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1014  // FIXME: under ASan the call below may write to freed memory and corrupt
1015  // its metadata. See
1016  // https://github.com/google/sanitizers/issues/321.
1017  SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1018  if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1019  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1020  return res;
1021}
1022#define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1023#else
1024#define INIT_PREAD
1025#endif
1026
1027#if SANITIZER_INTERCEPT_PREAD64
1028INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1029  void *ctx;
1030  COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1031  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1032  // FIXME: under ASan the call below may write to freed memory and corrupt
1033  // its metadata. See
1034  // https://github.com/google/sanitizers/issues/321.
1035  SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1036  if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1037  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1038  return res;
1039}
1040#define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1041#else
1042#define INIT_PREAD64
1043#endif
1044
1045#if SANITIZER_INTERCEPT_READV
1046INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1047                        int iovcnt) {
1048  void *ctx;
1049  COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1050  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1051  SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1052  if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1053  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1054  return res;
1055}
1056#define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1057#else
1058#define INIT_READV
1059#endif
1060
1061#if SANITIZER_INTERCEPT_PREADV
1062INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1063            OFF_T offset) {
1064  void *ctx;
1065  COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1066  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1067  SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1068  if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1069  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1070  return res;
1071}
1072#define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1073#else
1074#define INIT_PREADV
1075#endif
1076
1077#if SANITIZER_INTERCEPT_PREADV64
1078INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1079            OFF64_T offset) {
1080  void *ctx;
1081  COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1082  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1083  SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1084  if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1085  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1086  return res;
1087}
1088#define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1089#else
1090#define INIT_PREADV64
1091#endif
1092
1093#if SANITIZER_INTERCEPT_WRITE
1094INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1095  void *ctx;
1096  COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1097  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1098  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1099  SSIZE_T res = REAL(write)(fd, ptr, count);
1100  // FIXME: this check should be _before_ the call to REAL(write), not after
1101  if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1102  return res;
1103}
1104#define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1105#else
1106#define INIT_WRITE
1107#endif
1108
1109#if SANITIZER_INTERCEPT_FWRITE
1110INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1111  // libc file streams can call user-supplied functions, see fopencookie.
1112  void *ctx;
1113  COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1114  SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1115  if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1116  return res;
1117}
1118#define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1119#else
1120#define INIT_FWRITE
1121#endif
1122
1123#if SANITIZER_INTERCEPT_PWRITE
1124INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1125  void *ctx;
1126  COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1127  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1128  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1129  SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1130  if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1131  return res;
1132}
1133#define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1134#else
1135#define INIT_PWRITE
1136#endif
1137
1138#if SANITIZER_INTERCEPT_PWRITE64
1139INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1140            OFF64_T offset) {
1141  void *ctx;
1142  COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1143  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1144  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1145  SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1146  if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1147  return res;
1148}
1149#define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1150#else
1151#define INIT_PWRITE64
1152#endif
1153
1154#if SANITIZER_INTERCEPT_WRITEV
1155INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1156                        int iovcnt) {
1157  void *ctx;
1158  COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1159  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1160  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1161  SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1162  if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1163  return res;
1164}
1165#define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1166#else
1167#define INIT_WRITEV
1168#endif
1169
1170#if SANITIZER_INTERCEPT_PWRITEV
1171INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1172            OFF_T offset) {
1173  void *ctx;
1174  COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1175  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1176  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1177  SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1178  if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1179  return res;
1180}
1181#define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1182#else
1183#define INIT_PWRITEV
1184#endif
1185
1186#if SANITIZER_INTERCEPT_PWRITEV64
1187INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1188            OFF64_T offset) {
1189  void *ctx;
1190  COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1191  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1192  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1193  SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1194  if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1195  return res;
1196}
1197#define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1198#else
1199#define INIT_PWRITEV64
1200#endif
1201
1202#if SANITIZER_INTERCEPT_FGETS
1203INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1204  // libc file streams can call user-supplied functions, see fopencookie.
1205  void *ctx;
1206  COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1207  // FIXME: under ASan the call below may write to freed memory and corrupt
1208  // its metadata. See
1209  // https://github.com/google/sanitizers/issues/321.
1210  char *res = REAL(fgets)(s, size, file);
1211  if (res)
1212    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
1213  return res;
1214}
1215#define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1216#else
1217#define INIT_FGETS
1218#endif
1219
1220#if SANITIZER_INTERCEPT_FPUTS
1221INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1222  // libc file streams can call user-supplied functions, see fopencookie.
1223  void *ctx;
1224  COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1225  COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1226  return REAL(fputs)(s, file);
1227}
1228#define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1229#else
1230#define INIT_FPUTS
1231#endif
1232
1233#if SANITIZER_INTERCEPT_PUTS
1234INTERCEPTOR(int, puts, char *s) {
1235  // libc file streams can call user-supplied functions, see fopencookie.
1236  void *ctx;
1237  COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1238  COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1239  return REAL(puts)(s);
1240}
1241#define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1242#else
1243#define INIT_PUTS
1244#endif
1245
1246#if SANITIZER_INTERCEPT_PRCTL
1247INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1248            unsigned long arg3,                        // NOLINT
1249            unsigned long arg4, unsigned long arg5) {  // NOLINT
1250  void *ctx;
1251  COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1252  static const int PR_SET_NAME = 15;
1253  int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1254  if (option == PR_SET_NAME) {
1255    char buff[16];
1256    internal_strncpy(buff, (char *)arg2, 15);
1257    buff[15] = 0;
1258    COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1259  }
1260  return res;
1261}
1262#define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1263#else
1264#define INIT_PRCTL
1265#endif  // SANITIZER_INTERCEPT_PRCTL
1266
1267#if SANITIZER_INTERCEPT_TIME
1268INTERCEPTOR(unsigned long, time, unsigned long *t) {
1269  void *ctx;
1270  COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1271  unsigned long local_t;
1272  unsigned long res = REAL(time)(&local_t);
1273  if (t && res != (unsigned long)-1) {
1274    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1275    *t = local_t;
1276  }
1277  return res;
1278}
1279#define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1280#else
1281#define INIT_TIME
1282#endif  // SANITIZER_INTERCEPT_TIME
1283
1284#if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1285static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1286  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1287#if !SANITIZER_SOLARIS
1288  if (tm->tm_zone) {
1289    // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1290    // can point to shared memory and tsan would report a data race.
1291    COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1292                                        REAL(strlen(tm->tm_zone)) + 1);
1293  }
1294#endif
1295}
1296INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1297  void *ctx;
1298  COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1299  __sanitizer_tm *res = REAL(localtime)(timep);
1300  if (res) {
1301    COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1302    unpoison_tm(ctx, res);
1303  }
1304  return res;
1305}
1306INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1307  void *ctx;
1308  COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1309  __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1310  if (res) {
1311    COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1312    unpoison_tm(ctx, res);
1313  }
1314  return res;
1315}
1316INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1317  void *ctx;
1318  COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1319  __sanitizer_tm *res = REAL(gmtime)(timep);
1320  if (res) {
1321    COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1322    unpoison_tm(ctx, res);
1323  }
1324  return res;
1325}
1326INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1327  void *ctx;
1328  COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1329  __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1330  if (res) {
1331    COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1332    unpoison_tm(ctx, res);
1333  }
1334  return res;
1335}
1336INTERCEPTOR(char *, ctime, unsigned long *timep) {
1337  void *ctx;
1338  COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1339  // FIXME: under ASan the call below may write to freed memory and corrupt
1340  // its metadata. See
1341  // https://github.com/google/sanitizers/issues/321.
1342  char *res = REAL(ctime)(timep);
1343  if (res) {
1344    COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1345    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1346  }
1347  return res;
1348}
1349INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1350  void *ctx;
1351  COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1352  // FIXME: under ASan the call below may write to freed memory and corrupt
1353  // its metadata. See
1354  // https://github.com/google/sanitizers/issues/321.
1355  char *res = REAL(ctime_r)(timep, result);
1356  if (res) {
1357    COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1358    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1359  }
1360  return res;
1361}
1362INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1363  void *ctx;
1364  COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1365  // FIXME: under ASan the call below may write to freed memory and corrupt
1366  // its metadata. See
1367  // https://github.com/google/sanitizers/issues/321.
1368  char *res = REAL(asctime)(tm);
1369  if (res) {
1370    COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1371    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1372  }
1373  return res;
1374}
1375INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1376  void *ctx;
1377  COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1378  // FIXME: under ASan the call below may write to freed memory and corrupt
1379  // its metadata. See
1380  // https://github.com/google/sanitizers/issues/321.
1381  char *res = REAL(asctime_r)(tm, result);
1382  if (res) {
1383    COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1384    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1385  }
1386  return res;
1387}
1388INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1389  void *ctx;
1390  COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1391  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1392  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1393  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1394  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1395  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1396  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1397  COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1398  long res = REAL(mktime)(tm);
1399  if (res != -1) unpoison_tm(ctx, tm);
1400  return res;
1401}
1402#define INIT_LOCALTIME_AND_FRIENDS        \
1403  COMMON_INTERCEPT_FUNCTION(localtime);   \
1404  COMMON_INTERCEPT_FUNCTION(localtime_r); \
1405  COMMON_INTERCEPT_FUNCTION(gmtime);      \
1406  COMMON_INTERCEPT_FUNCTION(gmtime_r);    \
1407  COMMON_INTERCEPT_FUNCTION(ctime);       \
1408  COMMON_INTERCEPT_FUNCTION(ctime_r);     \
1409  COMMON_INTERCEPT_FUNCTION(asctime);     \
1410  COMMON_INTERCEPT_FUNCTION(asctime_r);   \
1411  COMMON_INTERCEPT_FUNCTION(mktime);
1412#else
1413#define INIT_LOCALTIME_AND_FRIENDS
1414#endif  // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1415
1416#if SANITIZER_INTERCEPT_STRPTIME
1417INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1418  void *ctx;
1419  COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1420  if (format)
1421    COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1422  // FIXME: under ASan the call below may write to freed memory and corrupt
1423  // its metadata. See
1424  // https://github.com/google/sanitizers/issues/321.
1425  char *res = REAL(strptime)(s, format, tm);
1426  COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1427  if (res && tm) {
1428    // Do not call unpoison_tm here, because strptime does not, in fact,
1429    // initialize the entire struct tm. For example, tm_zone pointer is left
1430    // uninitialized.
1431    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1432  }
1433  return res;
1434}
1435#define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1436#else
1437#define INIT_STRPTIME
1438#endif
1439
1440#if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1441#include "sanitizer_common_interceptors_format.inc"
1442
1443#define FORMAT_INTERCEPTOR_IMPL(name, vname, ...)                              \
1444  {                                                                            \
1445    void *ctx;                                                                 \
1446    va_list ap;                                                                \
1447    va_start(ap, format);                                                      \
1448    COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap);                     \
1449    int res = WRAP(vname)(__VA_ARGS__, ap);                                    \
1450    va_end(ap);                                                                \
1451    return res;                                                                \
1452  }
1453
1454#endif
1455
1456#if SANITIZER_INTERCEPT_SCANF
1457
1458#define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...)                    \
1459  {                                                                            \
1460    void *ctx;                                                                 \
1461    COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                         \
1462    va_list aq;                                                                \
1463    va_copy(aq, ap);                                                           \
1464    int res = REAL(vname)(__VA_ARGS__);                                        \
1465    if (res > 0)                                                               \
1466      scanf_common(ctx, res, allowGnuMalloc, format, aq);                      \
1467    va_end(aq);                                                                \
1468    return res;                                                                \
1469  }
1470
1471INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1472VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1473
1474INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1475VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1476
1477INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1478VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1479
1480#if SANITIZER_INTERCEPT_ISOC99_SCANF
1481INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1482VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1483
1484INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1485            va_list ap)
1486VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1487
1488INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1489VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1490#endif  // SANITIZER_INTERCEPT_ISOC99_SCANF
1491
1492INTERCEPTOR(int, scanf, const char *format, ...)
1493FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1494
1495INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1496FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1497
1498INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1499FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1500
1501#if SANITIZER_INTERCEPT_ISOC99_SCANF
1502INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1503FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1504
1505INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1506FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1507
1508INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1509FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1510#endif
1511
1512#endif
1513
1514#if SANITIZER_INTERCEPT_SCANF
1515#define INIT_SCANF                    \
1516  COMMON_INTERCEPT_FUNCTION_LDBL(scanf);   \
1517  COMMON_INTERCEPT_FUNCTION_LDBL(sscanf);  \
1518  COMMON_INTERCEPT_FUNCTION_LDBL(fscanf);  \
1519  COMMON_INTERCEPT_FUNCTION_LDBL(vscanf);  \
1520  COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1521  COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1522#else
1523#define INIT_SCANF
1524#endif
1525
1526#if SANITIZER_INTERCEPT_ISOC99_SCANF
1527#define INIT_ISOC99_SCANF                      \
1528  COMMON_INTERCEPT_FUNCTION(__isoc99_scanf);   \
1529  COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf);  \
1530  COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf);  \
1531  COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf);  \
1532  COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1533  COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1534#else
1535#define INIT_ISOC99_SCANF
1536#endif
1537
1538#if SANITIZER_INTERCEPT_PRINTF
1539
1540#define VPRINTF_INTERCEPTOR_ENTER(vname, ...)                                  \
1541  void *ctx;                                                                   \
1542  COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                           \
1543  va_list aq;                                                                  \
1544  va_copy(aq, ap);
1545
1546#define VPRINTF_INTERCEPTOR_RETURN()                                           \
1547  va_end(aq);
1548
1549#define VPRINTF_INTERCEPTOR_IMPL(vname, ...)                                   \
1550  {                                                                            \
1551    VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__);                             \
1552    if (common_flags()->check_printf)                                          \
1553      printf_common(ctx, format, aq);                                          \
1554    int res = REAL(vname)(__VA_ARGS__);                                        \
1555    VPRINTF_INTERCEPTOR_RETURN();                                              \
1556    return res;                                                                \
1557  }
1558
1559// FIXME: under ASan the REAL() call below may write to freed memory and
1560// corrupt its metadata. See
1561// https://github.com/google/sanitizers/issues/321.
1562#define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...)                             \
1563  {                                                                            \
1564    VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__)                         \
1565    if (common_flags()->check_printf) {                                        \
1566      printf_common(ctx, format, aq);                                          \
1567    }                                                                          \
1568    int res = REAL(vname)(str, __VA_ARGS__);                                   \
1569    if (res >= 0) {                                                            \
1570      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1);                       \
1571    }                                                                          \
1572    VPRINTF_INTERCEPTOR_RETURN();                                              \
1573    return res;                                                                \
1574  }
1575
1576// FIXME: under ASan the REAL() call below may write to freed memory and
1577// corrupt its metadata. See
1578// https://github.com/google/sanitizers/issues/321.
1579#define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...)                      \
1580  {                                                                            \
1581    VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__)                   \
1582    if (common_flags()->check_printf) {                                        \
1583      printf_common(ctx, format, aq);                                          \
1584    }                                                                          \
1585    int res = REAL(vname)(str, size, __VA_ARGS__);                             \
1586    if (res >= 0) {                                                            \
1587      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1)));  \
1588    }                                                                          \
1589    VPRINTF_INTERCEPTOR_RETURN();                                              \
1590    return res;                                                                \
1591  }
1592
1593// FIXME: under ASan the REAL() call below may write to freed memory and
1594// corrupt its metadata. See
1595// https://github.com/google/sanitizers/issues/321.
1596#define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...)                           \
1597  {                                                                            \
1598    VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__)                        \
1599    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *));                 \
1600    if (common_flags()->check_printf) {                                        \
1601      printf_common(ctx, format, aq);                                          \
1602    }                                                                          \
1603    int res = REAL(vname)(strp, __VA_ARGS__);                                  \
1604    if (res >= 0) {                                                            \
1605      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1);                     \
1606    }                                                                          \
1607    VPRINTF_INTERCEPTOR_RETURN();                                              \
1608    return res;                                                                \
1609  }
1610
1611INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1612VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1613
1614INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1615            va_list ap)
1616VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1617
1618INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1619            va_list ap)
1620VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1621
1622#if SANITIZER_INTERCEPT___PRINTF_CHK
1623INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1624            SIZE_T size_to, const char *format, va_list ap)
1625VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1626#endif
1627
1628#if SANITIZER_INTERCEPT_PRINTF_L
1629INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1630            const char *format, va_list ap)
1631VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1632
1633INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1634            const char *format, ...)
1635FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1636#endif  // SANITIZER_INTERCEPT_PRINTF_L
1637
1638INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1639VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1640
1641#if SANITIZER_INTERCEPT___PRINTF_CHK
1642INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1643            const char *format, va_list ap)
1644VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1645#endif
1646
1647INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1648VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1649
1650#if SANITIZER_INTERCEPT_ISOC99_PRINTF
1651INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1652VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1653
1654INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1655            const char *format, va_list ap)
1656VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1657
1658INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1659            va_list ap)
1660VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1661
1662INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1663            va_list ap)
1664VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1665                          ap)
1666
1667#endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
1668
1669INTERCEPTOR(int, printf, const char *format, ...)
1670FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1671
1672INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1673FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1674
1675#if SANITIZER_INTERCEPT___PRINTF_CHK
1676INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1677            const char *format, ...)
1678FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1679#endif
1680
1681INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1682FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1683
1684#if SANITIZER_INTERCEPT___PRINTF_CHK
1685INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1686            const char *format, ...) // NOLINT
1687FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format) // NOLINT
1688#endif
1689
1690INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1691FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1692
1693#if SANITIZER_INTERCEPT___PRINTF_CHK
1694INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1695            SIZE_T size_to, const char *format, ...) // NOLINT
1696FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format) // NOLINT
1697#endif
1698
1699INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1700FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1701
1702#if SANITIZER_INTERCEPT_ISOC99_PRINTF
1703INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1704FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1705
1706INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1707            ...)
1708FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1709
1710INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1711FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1712
1713INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1714            const char *format, ...)
1715FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1716                        format)
1717
1718#endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
1719
1720#endif  // SANITIZER_INTERCEPT_PRINTF
1721
1722#if SANITIZER_INTERCEPT_PRINTF
1723#define INIT_PRINTF                     \
1724  COMMON_INTERCEPT_FUNCTION_LDBL(printf);    \
1725  COMMON_INTERCEPT_FUNCTION_LDBL(sprintf);   \
1726  COMMON_INTERCEPT_FUNCTION_LDBL(snprintf);  \
1727  COMMON_INTERCEPT_FUNCTION_LDBL(asprintf);  \
1728  COMMON_INTERCEPT_FUNCTION_LDBL(fprintf);   \
1729  COMMON_INTERCEPT_FUNCTION_LDBL(vprintf);   \
1730  COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf);  \
1731  COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1732  COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1733  COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1734#else
1735#define INIT_PRINTF
1736#endif
1737
1738#if SANITIZER_INTERCEPT___PRINTF_CHK
1739#define INIT___PRINTF_CHK                     \
1740  COMMON_INTERCEPT_FUNCTION(__sprintf_chk);   \
1741  COMMON_INTERCEPT_FUNCTION(__snprintf_chk);  \
1742  COMMON_INTERCEPT_FUNCTION(__vsprintf_chk);  \
1743  COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1744  COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1745#else
1746#define INIT___PRINTF_CHK
1747#endif
1748
1749#if SANITIZER_INTERCEPT_PRINTF_L
1750#define INIT_PRINTF_L                     \
1751  COMMON_INTERCEPT_FUNCTION(snprintf_l);  \
1752  COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1753#else
1754#define INIT_PRINTF_L
1755#endif
1756
1757#if SANITIZER_INTERCEPT_ISOC99_PRINTF
1758#define INIT_ISOC99_PRINTF                       \
1759  COMMON_INTERCEPT_FUNCTION(__isoc99_printf);    \
1760  COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf);   \
1761  COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf);  \
1762  COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf);   \
1763  COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf);   \
1764  COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf);  \
1765  COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1766  COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1767#else
1768#define INIT_ISOC99_PRINTF
1769#endif
1770
1771#if SANITIZER_INTERCEPT_IOCTL
1772#include "sanitizer_common_interceptors_ioctl.inc"
1773#include "sanitizer_interceptors_ioctl_netbsd.inc"
1774INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1775  // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1776  // can trigger a report and we need to be able to unwind through this
1777  // function.  On Mac in debug mode we might not have a frame pointer, because
1778  // ioctl_common_[pre|post] doesn't get inlined here.
1779  ENABLE_FRAME_POINTER;
1780
1781  void *ctx;
1782  va_list ap;
1783  va_start(ap, request);
1784  void *arg = va_arg(ap, void *);
1785  va_end(ap);
1786  COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1787
1788  CHECK(ioctl_initialized);
1789
1790  // Note: TSan does not use common flags, and they are zero-initialized.
1791  // This effectively disables ioctl handling in TSan.
1792  if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1793
1794  // Although request is unsigned long, the rest of the interceptor uses it
1795  // as just "unsigned" to save space, because we know that all values fit in
1796  // "unsigned" - they are compile-time constants.
1797
1798  const ioctl_desc *desc = ioctl_lookup(request);
1799  ioctl_desc decoded_desc;
1800  if (!desc) {
1801    VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1802    if (!ioctl_decode(request, &decoded_desc))
1803      Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1804    else
1805      desc = &decoded_desc;
1806  }
1807
1808  if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1809  int res = REAL(ioctl)(d, request, arg);
1810  // FIXME: some ioctls have different return values for success and failure.
1811  if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1812  return res;
1813}
1814#define INIT_IOCTL \
1815  ioctl_init();    \
1816  COMMON_INTERCEPT_FUNCTION(ioctl);
1817#else
1818#define INIT_IOCTL
1819#endif
1820
1821#if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1822    SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1823    SANITIZER_INTERCEPT_GETPWENT_R || \
1824    SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS || \
1825    SANITIZER_INTERCEPT_FGETPWENT_R || \
1826    SANITIZER_INTERCEPT_FGETGRENT_R
1827static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1828  if (pwd) {
1829    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1830    if (pwd->pw_name)
1831      COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1832                                          REAL(strlen)(pwd->pw_name) + 1);
1833    if (pwd->pw_passwd)
1834      COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1835                                          REAL(strlen)(pwd->pw_passwd) + 1);
1836#if !SANITIZER_ANDROID
1837    if (pwd->pw_gecos)
1838      COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1839                                          REAL(strlen)(pwd->pw_gecos) + 1);
1840#endif
1841#if SANITIZER_MAC
1842    if (pwd->pw_class)
1843      COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1844                                          REAL(strlen)(pwd->pw_class) + 1);
1845#endif
1846    if (pwd->pw_dir)
1847      COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1848                                          REAL(strlen)(pwd->pw_dir) + 1);
1849    if (pwd->pw_shell)
1850      COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1851                                          REAL(strlen)(pwd->pw_shell) + 1);
1852  }
1853}
1854
1855static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1856  if (grp) {
1857    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1858    if (grp->gr_name)
1859      COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1860                                          REAL(strlen)(grp->gr_name) + 1);
1861    if (grp->gr_passwd)
1862      COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1863                                          REAL(strlen)(grp->gr_passwd) + 1);
1864    char **p = grp->gr_mem;
1865    for (; *p; ++p) {
1866      COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1867    }
1868    COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1869                                        (p - grp->gr_mem + 1) * sizeof(*p));
1870  }
1871}
1872#endif  // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1873        // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1874        // SANITIZER_INTERCEPT_GETPWENT_R ||
1875        // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1876
1877#if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1878INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1879  void *ctx;
1880  COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1881  if (name)
1882    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1883  __sanitizer_passwd *res = REAL(getpwnam)(name);
1884  if (res) unpoison_passwd(ctx, res);
1885  return res;
1886}
1887INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1888  void *ctx;
1889  COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1890  __sanitizer_passwd *res = REAL(getpwuid)(uid);
1891  if (res) unpoison_passwd(ctx, res);
1892  return res;
1893}
1894INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1895  void *ctx;
1896  COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1897  COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1898  __sanitizer_group *res = REAL(getgrnam)(name);
1899  if (res) unpoison_group(ctx, res);
1900  return res;
1901}
1902INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1903  void *ctx;
1904  COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1905  __sanitizer_group *res = REAL(getgrgid)(gid);
1906  if (res) unpoison_group(ctx, res);
1907  return res;
1908}
1909#define INIT_GETPWNAM_AND_FRIENDS      \
1910  COMMON_INTERCEPT_FUNCTION(getpwnam); \
1911  COMMON_INTERCEPT_FUNCTION(getpwuid); \
1912  COMMON_INTERCEPT_FUNCTION(getgrnam); \
1913  COMMON_INTERCEPT_FUNCTION(getgrgid);
1914#else
1915#define INIT_GETPWNAM_AND_FRIENDS
1916#endif
1917
1918#if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1919INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1920            char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1921  void *ctx;
1922  COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1923  COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1924  // FIXME: under ASan the call below may write to freed memory and corrupt
1925  // its metadata. See
1926  // https://github.com/google/sanitizers/issues/321.
1927  int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1928  if (!res) {
1929    if (result && *result) unpoison_passwd(ctx, *result);
1930    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1931  }
1932  if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1933  return res;
1934}
1935INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1936            SIZE_T buflen, __sanitizer_passwd **result) {
1937  void *ctx;
1938  COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1939  // FIXME: under ASan the call below may write to freed memory and corrupt
1940  // its metadata. See
1941  // https://github.com/google/sanitizers/issues/321.
1942  int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1943  if (!res) {
1944    if (result && *result) unpoison_passwd(ctx, *result);
1945    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1946  }
1947  if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1948  return res;
1949}
1950INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1951            char *buf, SIZE_T buflen, __sanitizer_group **result) {
1952  void *ctx;
1953  COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1954  COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1955  // FIXME: under ASan the call below may write to freed memory and corrupt
1956  // its metadata. See
1957  // https://github.com/google/sanitizers/issues/321.
1958  int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1959  if (!res) {
1960    if (result && *result) unpoison_group(ctx, *result);
1961    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1962  }
1963  if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1964  return res;
1965}
1966INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1967            SIZE_T buflen, __sanitizer_group **result) {
1968  void *ctx;
1969  COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1970  // FIXME: under ASan the call below may write to freed memory and corrupt
1971  // its metadata. See
1972  // https://github.com/google/sanitizers/issues/321.
1973  int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1974  if (!res) {
1975    if (result && *result) unpoison_group(ctx, *result);
1976    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1977  }
1978  if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1979  return res;
1980}
1981#define INIT_GETPWNAM_R_AND_FRIENDS      \
1982  COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1983  COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1984  COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1985  COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1986#else
1987#define INIT_GETPWNAM_R_AND_FRIENDS
1988#endif
1989
1990#if SANITIZER_INTERCEPT_GETPWENT
1991INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1992  void *ctx;
1993  COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1994  __sanitizer_passwd *res = REAL(getpwent)(dummy);
1995  if (res) unpoison_passwd(ctx, res);
1996  return res;
1997}
1998INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1999  void *ctx;
2000  COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2001  __sanitizer_group *res = REAL(getgrent)(dummy);
2002  if (res) unpoison_group(ctx, res);;
2003  return res;
2004}
2005#define INIT_GETPWENT                  \
2006  COMMON_INTERCEPT_FUNCTION(getpwent); \
2007  COMMON_INTERCEPT_FUNCTION(getgrent);
2008#else
2009#define INIT_GETPWENT
2010#endif
2011
2012#if SANITIZER_INTERCEPT_FGETPWENT
2013INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2014  void *ctx;
2015  COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2016  __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2017  if (res) unpoison_passwd(ctx, res);
2018  return res;
2019}
2020INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2021  void *ctx;
2022  COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2023  __sanitizer_group *res = REAL(fgetgrent)(fp);
2024  if (res) unpoison_group(ctx, res);
2025  return res;
2026}
2027#define INIT_FGETPWENT                  \
2028  COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2029  COMMON_INTERCEPT_FUNCTION(fgetgrent);
2030#else
2031#define INIT_FGETPWENT
2032#endif
2033
2034#if SANITIZER_INTERCEPT_GETPWENT_R
2035INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2036            SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2037  void *ctx;
2038  COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2039  // FIXME: under ASan the call below may write to freed memory and corrupt
2040  // its metadata. See
2041  // https://github.com/google/sanitizers/issues/321.
2042  int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2043  if (!res) {
2044    if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2045    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2046  }
2047  if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2048  return res;
2049}
2050INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2051            __sanitizer_group **pwbufp) {
2052  void *ctx;
2053  COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2054  // FIXME: under ASan the call below may write to freed memory and corrupt
2055  // its metadata. See
2056  // https://github.com/google/sanitizers/issues/321.
2057  int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2058  if (!res) {
2059    if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2060    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2061  }
2062  if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2063  return res;
2064}
2065#define INIT_GETPWENT_R                   \
2066  COMMON_INTERCEPT_FUNCTION(getpwent_r);  \
2067  COMMON_INTERCEPT_FUNCTION(getgrent_r);
2068#else
2069#define INIT_GETPWENT_R
2070#endif
2071
2072#if SANITIZER_INTERCEPT_FGETPWENT_R
2073INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2074            SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2075  void *ctx;
2076  COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2077  // FIXME: under ASan the call below may write to freed memory and corrupt
2078  // its metadata. See
2079  // https://github.com/google/sanitizers/issues/321.
2080  int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2081  if (!res) {
2082    if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2083    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2084  }
2085  if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2086  return res;
2087}
2088#define INIT_FGETPWENT_R                  \
2089  COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2090#else
2091#define INIT_FGETPWENT_R
2092#endif
2093
2094#if SANITIZER_INTERCEPT_FGETGRENT_R
2095INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2096            SIZE_T buflen, __sanitizer_group **pwbufp) {
2097  void *ctx;
2098  COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2099  // FIXME: under ASan the call below may write to freed memory and corrupt
2100  // its metadata. See
2101  // https://github.com/google/sanitizers/issues/321.
2102  int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2103  if (!res) {
2104    if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2105    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2106  }
2107  if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2108  return res;
2109}
2110#define INIT_FGETGRENT_R                  \
2111  COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2112#else
2113#define INIT_FGETGRENT_R
2114#endif
2115
2116#if SANITIZER_INTERCEPT_SETPWENT
2117// The only thing these interceptors do is disable any nested interceptors.
2118// These functions may open nss modules and call uninstrumented functions from
2119// them, and we don't want things like strlen() to trigger.
2120INTERCEPTOR(void, setpwent, int dummy) {
2121  void *ctx;
2122  COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2123  REAL(setpwent)(dummy);
2124}
2125INTERCEPTOR(void, endpwent, int dummy) {
2126  void *ctx;
2127  COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2128  REAL(endpwent)(dummy);
2129}
2130INTERCEPTOR(void, setgrent, int dummy) {
2131  void *ctx;
2132  COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2133  REAL(setgrent)(dummy);
2134}
2135INTERCEPTOR(void, endgrent, int dummy) {
2136  void *ctx;
2137  COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2138  REAL(endgrent)(dummy);
2139}
2140#define INIT_SETPWENT                  \
2141  COMMON_INTERCEPT_FUNCTION(setpwent); \
2142  COMMON_INTERCEPT_FUNCTION(endpwent); \
2143  COMMON_INTERCEPT_FUNCTION(setgrent); \
2144  COMMON_INTERCEPT_FUNCTION(endgrent);
2145#else
2146#define INIT_SETPWENT
2147#endif
2148
2149#if SANITIZER_INTERCEPT_CLOCK_GETTIME
2150INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2151  void *ctx;
2152  COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2153  // FIXME: under ASan the call below may write to freed memory and corrupt
2154  // its metadata. See
2155  // https://github.com/google/sanitizers/issues/321.
2156  int res = REAL(clock_getres)(clk_id, tp);
2157  if (!res && tp) {
2158    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2159  }
2160  return res;
2161}
2162INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2163  void *ctx;
2164  COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2165  // FIXME: under ASan the call below may write to freed memory and corrupt
2166  // its metadata. See
2167  // https://github.com/google/sanitizers/issues/321.
2168  int res = REAL(clock_gettime)(clk_id, tp);
2169  if (!res) {
2170    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2171  }
2172  return res;
2173}
2174namespace __sanitizer {
2175extern "C" {
2176int real_clock_gettime(u32 clk_id, void *tp) {
2177  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2178    return internal_clock_gettime(clk_id, tp);
2179  return REAL(clock_gettime)(clk_id, tp);
2180}
2181}  // extern "C"
2182}  // namespace __sanitizer
2183INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2184  void *ctx;
2185  COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2186  COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2187  return REAL(clock_settime)(clk_id, tp);
2188}
2189#define INIT_CLOCK_GETTIME                  \
2190  COMMON_INTERCEPT_FUNCTION(clock_getres);  \
2191  COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2192  COMMON_INTERCEPT_FUNCTION(clock_settime);
2193#else
2194#define INIT_CLOCK_GETTIME
2195#endif
2196
2197#if SANITIZER_INTERCEPT_GETITIMER
2198INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2199  void *ctx;
2200  COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2201  // FIXME: under ASan the call below may write to freed memory and corrupt
2202  // its metadata. See
2203  // https://github.com/google/sanitizers/issues/321.
2204  int res = REAL(getitimer)(which, curr_value);
2205  if (!res && curr_value) {
2206    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2207  }
2208  return res;
2209}
2210INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2211  void *ctx;
2212  COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2213  if (new_value) {
2214    // itimerval can contain padding that may be legitimately uninitialized
2215    const struct __sanitizer_itimerval *nv =
2216        (const struct __sanitizer_itimerval *)new_value;
2217    COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2218                                  sizeof(__sanitizer_time_t));
2219    COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2220                                  sizeof(__sanitizer_suseconds_t));
2221    COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2222                                  sizeof(__sanitizer_time_t));
2223    COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2224                                  sizeof(__sanitizer_suseconds_t));
2225  }
2226  // FIXME: under ASan the call below may write to freed memory and corrupt
2227  // its metadata. See
2228  // https://github.com/google/sanitizers/issues/321.
2229  int res = REAL(setitimer)(which, new_value, old_value);
2230  if (!res && old_value) {
2231    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2232  }
2233  return res;
2234}
2235#define INIT_GETITIMER                  \
2236  COMMON_INTERCEPT_FUNCTION(getitimer); \
2237  COMMON_INTERCEPT_FUNCTION(setitimer);
2238#else
2239#define INIT_GETITIMER
2240#endif
2241
2242#if SANITIZER_INTERCEPT_GLOB
2243static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2244  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2245  // +1 for NULL pointer at the end.
2246  if (pglob->gl_pathv)
2247    COMMON_INTERCEPTOR_WRITE_RANGE(
2248        ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2249  for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2250    char *p = pglob->gl_pathv[i];
2251    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2252  }
2253}
2254
2255#if SANITIZER_SOLARIS
2256INTERCEPTOR(int, glob, const char *pattern, int flags,
2257            int (*errfunc)(const char *epath, int eerrno),
2258            __sanitizer_glob_t *pglob) {
2259  void *ctx;
2260  COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2261  COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2262  int res = REAL(glob)(pattern, flags, errfunc, pglob);
2263  if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2264  return res;
2265}
2266#else
2267static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2268
2269static void wrapped_gl_closedir(void *dir) {
2270  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2271  pglob_copy->gl_closedir(dir);
2272}
2273
2274static void *wrapped_gl_readdir(void *dir) {
2275  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2276  return pglob_copy->gl_readdir(dir);
2277}
2278
2279static void *wrapped_gl_opendir(const char *s) {
2280  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2281  COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2282  return pglob_copy->gl_opendir(s);
2283}
2284
2285static int wrapped_gl_lstat(const char *s, void *st) {
2286  COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2287  COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2288  return pglob_copy->gl_lstat(s, st);
2289}
2290
2291static int wrapped_gl_stat(const char *s, void *st) {
2292  COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2293  COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2294  return pglob_copy->gl_stat(s, st);
2295}
2296
2297static const __sanitizer_glob_t kGlobCopy = {
2298      0,                  0,                   0,
2299      0,                  wrapped_gl_closedir, wrapped_gl_readdir,
2300      wrapped_gl_opendir, wrapped_gl_lstat,    wrapped_gl_stat};
2301
2302INTERCEPTOR(int, glob, const char *pattern, int flags,
2303            int (*errfunc)(const char *epath, int eerrno),
2304            __sanitizer_glob_t *pglob) {
2305  void *ctx;
2306  COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2307  COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2308  __sanitizer_glob_t glob_copy;
2309  internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2310  if (flags & glob_altdirfunc) {
2311    Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2312    Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2313    Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2314    Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2315    Swap(pglob->gl_stat, glob_copy.gl_stat);
2316    pglob_copy = &glob_copy;
2317  }
2318  int res = REAL(glob)(pattern, flags, errfunc, pglob);
2319  if (flags & glob_altdirfunc) {
2320    Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2321    Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2322    Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2323    Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2324    Swap(pglob->gl_stat, glob_copy.gl_stat);
2325  }
2326  pglob_copy = 0;
2327  if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2328  return res;
2329}
2330#endif  // SANITIZER_SOLARIS
2331#define INIT_GLOB                  \
2332  COMMON_INTERCEPT_FUNCTION(glob);
2333#else  // SANITIZER_INTERCEPT_GLOB
2334#define INIT_GLOB
2335#endif  // SANITIZER_INTERCEPT_GLOB
2336
2337#if SANITIZER_INTERCEPT_GLOB64
2338INTERCEPTOR(int, glob64, const char *pattern, int flags,
2339            int (*errfunc)(const char *epath, int eerrno),
2340            __sanitizer_glob_t *pglob) {
2341  void *ctx;
2342  COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2343  COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2344  __sanitizer_glob_t glob_copy;
2345  internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2346  if (flags & glob_altdirfunc) {
2347    Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2348    Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2349    Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2350    Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2351    Swap(pglob->gl_stat, glob_copy.gl_stat);
2352    pglob_copy = &glob_copy;
2353  }
2354  int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2355  if (flags & glob_altdirfunc) {
2356    Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2357    Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2358    Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2359    Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2360    Swap(pglob->gl_stat, glob_copy.gl_stat);
2361  }
2362  pglob_copy = 0;
2363  if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2364  return res;
2365}
2366#define INIT_GLOB64                \
2367  COMMON_INTERCEPT_FUNCTION(glob64);
2368#else  // SANITIZER_INTERCEPT_GLOB64
2369#define INIT_GLOB64
2370#endif  // SANITIZER_INTERCEPT_GLOB64
2371
2372#if SANITIZER_INTERCEPT_WAIT
2373// According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2374// suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2375// details.
2376INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2377  void *ctx;
2378  COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2379  // FIXME: under ASan the call below may write to freed memory and corrupt
2380  // its metadata. See
2381  // https://github.com/google/sanitizers/issues/321.
2382  int res = REAL(wait)(status);
2383  if (res != -1 && status)
2384    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2385  return res;
2386}
2387// On FreeBSD id_t is always 64-bit wide.
2388#if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2389INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2390                        int options) {
2391#else
2392INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2393                        int options) {
2394#endif
2395  void *ctx;
2396  COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2397  // FIXME: under ASan the call below may write to freed memory and corrupt
2398  // its metadata. See
2399  // https://github.com/google/sanitizers/issues/321.
2400  int res = REAL(waitid)(idtype, id, infop, options);
2401  if (res != -1 && infop)
2402    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2403  return res;
2404}
2405INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2406  void *ctx;
2407  COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2408  // FIXME: under ASan the call below may write to freed memory and corrupt
2409  // its metadata. See
2410  // https://github.com/google/sanitizers/issues/321.
2411  int res = REAL(waitpid)(pid, status, options);
2412  if (res != -1 && status)
2413    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2414  return res;
2415}
2416INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2417  void *ctx;
2418  COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2419  // FIXME: under ASan the call below may write to freed memory and corrupt
2420  // its metadata. See
2421  // https://github.com/google/sanitizers/issues/321.
2422  int res = REAL(wait3)(status, options, rusage);
2423  if (res != -1) {
2424    if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2425    if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2426  }
2427  return res;
2428}
2429#if SANITIZER_ANDROID
2430INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2431  void *ctx;
2432  COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2433  // FIXME: under ASan the call below may write to freed memory and corrupt
2434  // its metadata. See
2435  // https://github.com/google/sanitizers/issues/321.
2436  int res = REAL(__wait4)(pid, status, options, rusage);
2437  if (res != -1) {
2438    if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2439    if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2440  }
2441  return res;
2442}
2443#define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2444#else
2445INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2446  void *ctx;
2447  COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2448  // FIXME: under ASan the call below may write to freed memory and corrupt
2449  // its metadata. See
2450  // https://github.com/google/sanitizers/issues/321.
2451  int res = REAL(wait4)(pid, status, options, rusage);
2452  if (res != -1) {
2453    if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2454    if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2455  }
2456  return res;
2457}
2458#define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2459#endif  // SANITIZER_ANDROID
2460#define INIT_WAIT                     \
2461  COMMON_INTERCEPT_FUNCTION(wait);    \
2462  COMMON_INTERCEPT_FUNCTION(waitid);  \
2463  COMMON_INTERCEPT_FUNCTION(waitpid); \
2464  COMMON_INTERCEPT_FUNCTION(wait3);
2465#else
2466#define INIT_WAIT
2467#define INIT_WAIT4
2468#endif
2469
2470#if SANITIZER_INTERCEPT_INET
2471INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2472  void *ctx;
2473  COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2474  uptr sz = __sanitizer_in_addr_sz(af);
2475  if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2476  // FIXME: figure out read size based on the address family.
2477  // FIXME: under ASan the call below may write to freed memory and corrupt
2478  // its metadata. See
2479  // https://github.com/google/sanitizers/issues/321.
2480  char *res = REAL(inet_ntop)(af, src, dst, size);
2481  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2482  return res;
2483}
2484INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2485  void *ctx;
2486  COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2487  COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2488  // FIXME: figure out read size based on the address family.
2489  // FIXME: under ASan the call below may write to freed memory and corrupt
2490  // its metadata. See
2491  // https://github.com/google/sanitizers/issues/321.
2492  int res = REAL(inet_pton)(af, src, dst);
2493  if (res == 1) {
2494    uptr sz = __sanitizer_in_addr_sz(af);
2495    if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2496  }
2497  return res;
2498}
2499#define INIT_INET                       \
2500  COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2501  COMMON_INTERCEPT_FUNCTION(inet_pton);
2502#else
2503#define INIT_INET
2504#endif
2505
2506#if SANITIZER_INTERCEPT_INET
2507INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2508  void *ctx;
2509  COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2510  if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2511  // FIXME: under ASan the call below may write to freed memory and corrupt
2512  // its metadata. See
2513  // https://github.com/google/sanitizers/issues/321.
2514  int res = REAL(inet_aton)(cp, dst);
2515  if (res != 0) {
2516    uptr sz = __sanitizer_in_addr_sz(af_inet);
2517    if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2518  }
2519  return res;
2520}
2521#define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2522#else
2523#define INIT_INET_ATON
2524#endif
2525
2526#if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2527INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2528  void *ctx;
2529  COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2530  // FIXME: under ASan the call below may write to freed memory and corrupt
2531  // its metadata. See
2532  // https://github.com/google/sanitizers/issues/321.
2533  int res = REAL(pthread_getschedparam)(thread, policy, param);
2534  if (res == 0) {
2535    if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2536    if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2537  }
2538  return res;
2539}
2540#define INIT_PTHREAD_GETSCHEDPARAM \
2541  COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2542#else
2543#define INIT_PTHREAD_GETSCHEDPARAM
2544#endif
2545
2546#if SANITIZER_INTERCEPT_GETADDRINFO
2547INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2548            struct __sanitizer_addrinfo *hints,
2549            struct __sanitizer_addrinfo **out) {
2550  void *ctx;
2551  COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2552  if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2553  if (service)
2554    COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2555  if (hints)
2556    COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2557  // FIXME: under ASan the call below may write to freed memory and corrupt
2558  // its metadata. See
2559  // https://github.com/google/sanitizers/issues/321.
2560  int res = REAL(getaddrinfo)(node, service, hints, out);
2561  if (res == 0 && out) {
2562    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2563    struct __sanitizer_addrinfo *p = *out;
2564    while (p) {
2565      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2566      if (p->ai_addr)
2567        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2568      if (p->ai_canonname)
2569        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2570                                       REAL(strlen)(p->ai_canonname) + 1);
2571      p = p->ai_next;
2572    }
2573  }
2574  return res;
2575}
2576#define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2577#else
2578#define INIT_GETADDRINFO
2579#endif
2580
2581#if SANITIZER_INTERCEPT_GETNAMEINFO
2582INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2583            unsigned hostlen, char *serv, unsigned servlen, int flags) {
2584  void *ctx;
2585  COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2586                           serv, servlen, flags);
2587  // FIXME: consider adding READ_RANGE(sockaddr, salen)
2588  // There is padding in in_addr that may make this too noisy
2589  // FIXME: under ASan the call below may write to freed memory and corrupt
2590  // its metadata. See
2591  // https://github.com/google/sanitizers/issues/321.
2592  int res =
2593      REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2594  if (res == 0) {
2595    if (host && hostlen)
2596      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2597    if (serv && servlen)
2598      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2599  }
2600  return res;
2601}
2602#define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2603#else
2604#define INIT_GETNAMEINFO
2605#endif
2606
2607#if SANITIZER_INTERCEPT_GETSOCKNAME
2608INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2609  void *ctx;
2610  COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2611  COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2612  int addrlen_in = *addrlen;
2613  // FIXME: under ASan the call below may write to freed memory and corrupt
2614  // its metadata. See
2615  // https://github.com/google/sanitizers/issues/321.
2616  int res = REAL(getsockname)(sock_fd, addr, addrlen);
2617  if (res == 0) {
2618    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2619  }
2620  return res;
2621}
2622#define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2623#else
2624#define INIT_GETSOCKNAME
2625#endif
2626
2627#if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2628static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2629  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2630  if (h->h_name)
2631    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2632  char **p = h->h_aliases;
2633  while (*p) {
2634    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2635    ++p;
2636  }
2637  COMMON_INTERCEPTOR_WRITE_RANGE(
2638      ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2639  p = h->h_addr_list;
2640  while (*p) {
2641    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2642    ++p;
2643  }
2644  COMMON_INTERCEPTOR_WRITE_RANGE(
2645      ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2646}
2647#endif
2648
2649#if SANITIZER_INTERCEPT_GETHOSTBYNAME
2650INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2651  void *ctx;
2652  COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2653  struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2654  if (res) write_hostent(ctx, res);
2655  return res;
2656}
2657
2658INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2659            int type) {
2660  void *ctx;
2661  COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2662  COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2663  struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2664  if (res) write_hostent(ctx, res);
2665  return res;
2666}
2667
2668INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2669  void *ctx;
2670  COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2671  struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2672  if (res) write_hostent(ctx, res);
2673  return res;
2674}
2675#define INIT_GETHOSTBYNAME                  \
2676  COMMON_INTERCEPT_FUNCTION(gethostent);    \
2677  COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2678  COMMON_INTERCEPT_FUNCTION(gethostbyname);
2679#else
2680#define INIT_GETHOSTBYNAME
2681#endif  // SANITIZER_INTERCEPT_GETHOSTBYNAME
2682
2683#if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2684INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2685  void *ctx;
2686  COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2687  struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2688  if (res) write_hostent(ctx, res);
2689  return res;
2690}
2691#define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2692#else
2693#define INIT_GETHOSTBYNAME2
2694#endif  // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2695
2696#if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2697INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2698            char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2699            int *h_errnop) {
2700  void *ctx;
2701  COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2702                           h_errnop);
2703  // FIXME: under ASan the call below may write to freed memory and corrupt
2704  // its metadata. See
2705  // https://github.com/google/sanitizers/issues/321.
2706  int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2707  if (result) {
2708    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2709    if (res == 0 && *result) write_hostent(ctx, *result);
2710  }
2711  if (h_errnop)
2712    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2713  return res;
2714}
2715#define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2716#else
2717#define INIT_GETHOSTBYNAME_R
2718#endif
2719
2720#if SANITIZER_INTERCEPT_GETHOSTENT_R
2721INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2722            SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2723  void *ctx;
2724  COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2725                           h_errnop);
2726  // FIXME: under ASan the call below may write to freed memory and corrupt
2727  // its metadata. See
2728  // https://github.com/google/sanitizers/issues/321.
2729  int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2730  if (result) {
2731    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2732    if (res == 0 && *result) write_hostent(ctx, *result);
2733  }
2734  if (h_errnop)
2735    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2736  return res;
2737}
2738#define INIT_GETHOSTENT_R                  \
2739  COMMON_INTERCEPT_FUNCTION(gethostent_r);
2740#else
2741#define INIT_GETHOSTENT_R
2742#endif
2743
2744#if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2745INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2746            struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2747            __sanitizer_hostent **result, int *h_errnop) {
2748  void *ctx;
2749  COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2750                           buflen, result, h_errnop);
2751  COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2752  // FIXME: under ASan the call below may write to freed memory and corrupt
2753  // its metadata. See
2754  // https://github.com/google/sanitizers/issues/321.
2755  int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2756                                  h_errnop);
2757  if (result) {
2758    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2759    if (res == 0 && *result) write_hostent(ctx, *result);
2760  }
2761  if (h_errnop)
2762    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2763  return res;
2764}
2765#define INIT_GETHOSTBYADDR_R                  \
2766  COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2767#else
2768#define INIT_GETHOSTBYADDR_R
2769#endif
2770
2771#if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2772INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2773            struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2774            __sanitizer_hostent **result, int *h_errnop) {
2775  void *ctx;
2776  COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2777                           result, h_errnop);
2778  // FIXME: under ASan the call below may write to freed memory and corrupt
2779  // its metadata. See
2780  // https://github.com/google/sanitizers/issues/321.
2781  int res =
2782      REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2783  if (result) {
2784    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2785    if (res == 0 && *result) write_hostent(ctx, *result);
2786  }
2787  if (h_errnop)
2788    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2789  return res;
2790}
2791#define INIT_GETHOSTBYNAME2_R                  \
2792  COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2793#else
2794#define INIT_GETHOSTBYNAME2_R
2795#endif
2796
2797#if SANITIZER_INTERCEPT_GETSOCKOPT
2798INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2799            int *optlen) {
2800  void *ctx;
2801  COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2802                           optlen);
2803  if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2804  // FIXME: under ASan the call below may write to freed memory and corrupt
2805  // its metadata. See
2806  // https://github.com/google/sanitizers/issues/321.
2807  int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2808  if (res == 0)
2809    if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2810  return res;
2811}
2812#define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2813#else
2814#define INIT_GETSOCKOPT
2815#endif
2816
2817#if SANITIZER_INTERCEPT_ACCEPT
2818INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2819  void *ctx;
2820  COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2821  unsigned addrlen0 = 0;
2822  if (addrlen) {
2823    COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2824    addrlen0 = *addrlen;
2825  }
2826  int fd2 = REAL(accept)(fd, addr, addrlen);
2827  if (fd2 >= 0) {
2828    if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2829    if (addr && addrlen)
2830      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2831  }
2832  return fd2;
2833}
2834#define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2835#else
2836#define INIT_ACCEPT
2837#endif
2838
2839#if SANITIZER_INTERCEPT_ACCEPT4
2840INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2841  void *ctx;
2842  COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2843  unsigned addrlen0 = 0;
2844  if (addrlen) {
2845    COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2846    addrlen0 = *addrlen;
2847  }
2848  // FIXME: under ASan the call below may write to freed memory and corrupt
2849  // its metadata. See
2850  // https://github.com/google/sanitizers/issues/321.
2851  int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2852  if (fd2 >= 0) {
2853    if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2854    if (addr && addrlen)
2855      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2856  }
2857  return fd2;
2858}
2859#define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2860#else
2861#define INIT_ACCEPT4
2862#endif
2863
2864#if SANITIZER_INTERCEPT_PACCEPT
2865INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2866            __sanitizer_sigset_t *set, int f) {
2867  void *ctx;
2868  COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2869  unsigned addrlen0 = 0;
2870  if (addrlen) {
2871    COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2872    addrlen0 = *addrlen;
2873  }
2874  if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2875  int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
2876  if (fd2 >= 0) {
2877    if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2878    if (addr && addrlen)
2879      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2880  }
2881  return fd2;
2882}
2883#define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2884#else
2885#define INIT_PACCEPT
2886#endif
2887
2888#if SANITIZER_INTERCEPT_MODF
2889INTERCEPTOR(double, modf, double x, double *iptr) {
2890  void *ctx;
2891  COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2892  // FIXME: under ASan the call below may write to freed memory and corrupt
2893  // its metadata. See
2894  // https://github.com/google/sanitizers/issues/321.
2895  double res = REAL(modf)(x, iptr);
2896  if (iptr) {
2897    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2898  }
2899  return res;
2900}
2901INTERCEPTOR(float, modff, float x, float *iptr) {
2902  void *ctx;
2903  COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2904  // FIXME: under ASan the call below may write to freed memory and corrupt
2905  // its metadata. See
2906  // https://github.com/google/sanitizers/issues/321.
2907  float res = REAL(modff)(x, iptr);
2908  if (iptr) {
2909    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2910  }
2911  return res;
2912}
2913INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2914  void *ctx;
2915  COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2916  // FIXME: under ASan the call below may write to freed memory and corrupt
2917  // its metadata. See
2918  // https://github.com/google/sanitizers/issues/321.
2919  long double res = REAL(modfl)(x, iptr);
2920  if (iptr) {
2921    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2922  }
2923  return res;
2924}
2925#define INIT_MODF                   \
2926  COMMON_INTERCEPT_FUNCTION(modf);  \
2927  COMMON_INTERCEPT_FUNCTION(modff); \
2928  COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2929#else
2930#define INIT_MODF
2931#endif
2932
2933#if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2934static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2935                         SSIZE_T maxlen) {
2936  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2937  if (msg->msg_name && msg->msg_namelen)
2938    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2939  if (msg->msg_iov && msg->msg_iovlen)
2940    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2941                                   sizeof(*msg->msg_iov) * msg->msg_iovlen);
2942  write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2943  if (msg->msg_control && msg->msg_controllen)
2944    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2945}
2946#endif
2947
2948#if SANITIZER_INTERCEPT_RECVMSG
2949INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2950            int flags) {
2951  void *ctx;
2952  COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2953  // FIXME: under ASan the call below may write to freed memory and corrupt
2954  // its metadata. See
2955  // https://github.com/google/sanitizers/issues/321.
2956  SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2957  if (res >= 0) {
2958    if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2959    if (msg) {
2960      write_msghdr(ctx, msg, res);
2961      COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2962    }
2963  }
2964  return res;
2965}
2966#define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2967#else
2968#define INIT_RECVMSG
2969#endif
2970
2971#if SANITIZER_INTERCEPT_RECVMMSG
2972INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2973            unsigned int vlen, int flags, void *timeout) {
2974  void *ctx;
2975  COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2976  if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2977  int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2978  if (res >= 0) {
2979    if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2980    for (int i = 0; i < res; ++i) {
2981      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2982                                     sizeof(msgvec[i].msg_len));
2983      write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2984      COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2985    }
2986  }
2987  return res;
2988}
2989#define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2990#else
2991#define INIT_RECVMMSG
2992#endif
2993
2994#if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2995static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2996  const unsigned kCmsgDataOffset =
2997      RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2998
2999  char *p = (char *)control;
3000  char *const control_end = p + controllen;
3001  while (true) {
3002    if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3003    __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3004    COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3005
3006    if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3007
3008    COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3009                                  sizeof(cmsg->cmsg_level));
3010    COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3011                                  sizeof(cmsg->cmsg_type));
3012
3013    if (cmsg->cmsg_len > kCmsgDataOffset) {
3014      char *data = p + kCmsgDataOffset;
3015      unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3016      if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3017    }
3018
3019    p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3020  }
3021}
3022
3023static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3024                        SSIZE_T maxlen) {
3025#define R(f) \
3026  COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3027  R(name);
3028  R(namelen);
3029  R(iov);
3030  R(iovlen);
3031  R(control);
3032  R(controllen);
3033  R(flags);
3034#undef R
3035  if (msg->msg_name && msg->msg_namelen)
3036    COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3037  if (msg->msg_iov && msg->msg_iovlen)
3038    COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3039                                  sizeof(*msg->msg_iov) * msg->msg_iovlen);
3040  read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3041  if (msg->msg_control && msg->msg_controllen)
3042    read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3043}
3044#endif
3045
3046#if SANITIZER_INTERCEPT_SENDMSG
3047INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3048            int flags) {
3049  void *ctx;
3050  COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3051  if (fd >= 0) {
3052    COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3053    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3054  }
3055  SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3056  if (common_flags()->intercept_send && res >= 0 && msg)
3057    read_msghdr(ctx, msg, res);
3058  return res;
3059}
3060#define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3061#else
3062#define INIT_SENDMSG
3063#endif
3064
3065#if SANITIZER_INTERCEPT_SENDMMSG
3066INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3067            unsigned vlen, int flags) {
3068  void *ctx;
3069  COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3070  if (fd >= 0) {
3071    COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3072    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3073  }
3074  int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3075  if (res >= 0 && msgvec)
3076    for (int i = 0; i < res; ++i) {
3077      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3078                                     sizeof(msgvec[i].msg_len));
3079      if (common_flags()->intercept_send)
3080        read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3081    }
3082  return res;
3083}
3084#define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3085#else
3086#define INIT_SENDMMSG
3087#endif
3088
3089#if SANITIZER_INTERCEPT_GETPEERNAME
3090INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3091  void *ctx;
3092  COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3093  unsigned addr_sz;
3094  if (addrlen) addr_sz = *addrlen;
3095  // FIXME: under ASan the call below may write to freed memory and corrupt
3096  // its metadata. See
3097  // https://github.com/google/sanitizers/issues/321.
3098  int res = REAL(getpeername)(sockfd, addr, addrlen);
3099  if (!res && addr && addrlen)
3100    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3101  return res;
3102}
3103#define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3104#else
3105#define INIT_GETPEERNAME
3106#endif
3107
3108#if SANITIZER_INTERCEPT_SYSINFO
3109INTERCEPTOR(int, sysinfo, void *info) {
3110  void *ctx;
3111  // FIXME: under ASan the call below may write to freed memory and corrupt
3112  // its metadata. See
3113  // https://github.com/google/sanitizers/issues/321.
3114  COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3115  int res = REAL(sysinfo)(info);
3116  if (!res && info)
3117    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3118  return res;
3119}
3120#define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3121#else
3122#define INIT_SYSINFO
3123#endif
3124
3125#if SANITIZER_INTERCEPT_READDIR
3126INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3127  void *ctx;
3128  COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3129  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3130  __sanitizer_dirent *res = REAL(opendir)(path);
3131  if (res)
3132    COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3133  return res;
3134}
3135
3136INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3137  void *ctx;
3138  COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3139  // FIXME: under ASan the call below may write to freed memory and corrupt
3140  // its metadata. See
3141  // https://github.com/google/sanitizers/issues/321.
3142  __sanitizer_dirent *res = REAL(readdir)(dirp);
3143  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3144  return res;
3145}
3146
3147INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3148            __sanitizer_dirent **result) {
3149  void *ctx;
3150  COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3151  // FIXME: under ASan the call below may write to freed memory and corrupt
3152  // its metadata. See
3153  // https://github.com/google/sanitizers/issues/321.
3154  int res = REAL(readdir_r)(dirp, entry, result);
3155  if (!res) {
3156    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3157    if (*result)
3158      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3159  }
3160  return res;
3161}
3162
3163#define INIT_READDIR                  \
3164  COMMON_INTERCEPT_FUNCTION(opendir); \
3165  COMMON_INTERCEPT_FUNCTION(readdir); \
3166  COMMON_INTERCEPT_FUNCTION(readdir_r);
3167#else
3168#define INIT_READDIR
3169#endif
3170
3171#if SANITIZER_INTERCEPT_READDIR64
3172INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3173  void *ctx;
3174  COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3175  // FIXME: under ASan the call below may write to freed memory and corrupt
3176  // its metadata. See
3177  // https://github.com/google/sanitizers/issues/321.
3178  __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3179  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3180  return res;
3181}
3182
3183INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3184            __sanitizer_dirent64 **result) {
3185  void *ctx;
3186  COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3187  // FIXME: under ASan the call below may write to freed memory and corrupt
3188  // its metadata. See
3189  // https://github.com/google/sanitizers/issues/321.
3190  int res = REAL(readdir64_r)(dirp, entry, result);
3191  if (!res) {
3192    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3193    if (*result)
3194      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3195  }
3196  return res;
3197}
3198#define INIT_READDIR64                  \
3199  COMMON_INTERCEPT_FUNCTION(readdir64); \
3200  COMMON_INTERCEPT_FUNCTION(readdir64_r);
3201#else
3202#define INIT_READDIR64
3203#endif
3204
3205#if SANITIZER_INTERCEPT_PTRACE
3206INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3207  void *ctx;
3208  COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3209  __sanitizer_iovec local_iovec;
3210
3211  if (data) {
3212    if (request == ptrace_setregs)
3213      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3214    else if (request == ptrace_setfpregs)
3215      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3216    else if (request == ptrace_setfpxregs)
3217      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3218    else if (request == ptrace_setvfpregs)
3219      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3220    else if (request == ptrace_setsiginfo)
3221      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3222    // Some kernel might zero the iovec::iov_base in case of invalid
3223    // write access.  In this case copy the invalid address for further
3224    // inspection.
3225    else if (request == ptrace_setregset || request == ptrace_getregset) {
3226      __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3227      COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3228      local_iovec = *iovec;
3229      if (request == ptrace_setregset)
3230        COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3231    }
3232  }
3233
3234  // FIXME: under ASan the call below may write to freed memory and corrupt
3235  // its metadata. See
3236  // https://github.com/google/sanitizers/issues/321.
3237  uptr res = REAL(ptrace)(request, pid, addr, data);
3238
3239  if (!res && data) {
3240    // Note that PEEK* requests assign different meaning to the return value.
3241    // This function does not handle them (nor does it need to).
3242    if (request == ptrace_getregs)
3243      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3244    else if (request == ptrace_getfpregs)
3245      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3246    else if (request == ptrace_getfpxregs)
3247      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3248    else if (request == ptrace_getvfpregs)
3249      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3250    else if (request == ptrace_getsiginfo)
3251      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3252    else if (request == ptrace_geteventmsg)
3253      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3254    else if (request == ptrace_getregset) {
3255      __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3256      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3257      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3258                                     local_iovec.iov_len);
3259    }
3260  }
3261  return res;
3262}
3263
3264#define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3265#else
3266#define INIT_PTRACE
3267#endif
3268
3269#if SANITIZER_INTERCEPT_SETLOCALE
3270static void unpoison_ctype_arrays(void *ctx) {
3271#if SANITIZER_NETBSD
3272  // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3273  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3274  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3275  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3276#endif
3277}
3278
3279INTERCEPTOR(char *, setlocale, int category, char *locale) {
3280  void *ctx;
3281  COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3282  if (locale)
3283    COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3284  char *res = REAL(setlocale)(category, locale);
3285  if (res) {
3286    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3287    unpoison_ctype_arrays(ctx);
3288  }
3289  return res;
3290}
3291
3292#define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3293#else
3294#define INIT_SETLOCALE
3295#endif
3296
3297#if SANITIZER_INTERCEPT_GETCWD
3298INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3299  void *ctx;
3300  COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3301  // FIXME: under ASan the call below may write to freed memory and corrupt
3302  // its metadata. See
3303  // https://github.com/google/sanitizers/issues/321.
3304  char *res = REAL(getcwd)(buf, size);
3305  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3306  return res;
3307}
3308#define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3309#else
3310#define INIT_GETCWD
3311#endif
3312
3313#if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3314INTERCEPTOR(char *, get_current_dir_name, int fake) {
3315  void *ctx;
3316  COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3317  // FIXME: under ASan the call below may write to freed memory and corrupt
3318  // its metadata. See
3319  // https://github.com/google/sanitizers/issues/321.
3320  char *res = REAL(get_current_dir_name)(fake);
3321  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3322  return res;
3323}
3324
3325#define INIT_GET_CURRENT_DIR_NAME \
3326  COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3327#else
3328#define INIT_GET_CURRENT_DIR_NAME
3329#endif
3330
3331UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3332  CHECK(endptr);
3333  if (nptr == *endptr) {
3334    // No digits were found at strtol call, we need to find out the last
3335    // symbol accessed by strtoll on our own.
3336    // We get this symbol by skipping leading blanks and optional +/- sign.
3337    while (IsSpace(*nptr)) nptr++;
3338    if (*nptr == '+' || *nptr == '-') nptr++;
3339    *endptr = const_cast<char *>(nptr);
3340  }
3341  CHECK(*endptr >= nptr);
3342}
3343
3344UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3345                             char **endptr, char *real_endptr, int base) {
3346  if (endptr) {
3347    *endptr = real_endptr;
3348    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3349  }
3350  // If base has unsupported value, strtol can exit with EINVAL
3351  // without reading any characters. So do additional checks only
3352  // if base is valid.
3353  bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3354  if (is_valid_base) {
3355    FixRealStrtolEndptr(nptr, &real_endptr);
3356  }
3357  COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3358                                 (real_endptr - nptr) + 1 : 0);
3359}
3360
3361
3362#if SANITIZER_INTERCEPT_STRTOIMAX
3363INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3364  void *ctx;
3365  COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3366  // FIXME: under ASan the call below may write to freed memory and corrupt
3367  // its metadata. See
3368  // https://github.com/google/sanitizers/issues/321.
3369  char *real_endptr;
3370  INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3371  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3372  return res;
3373}
3374
3375INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3376  void *ctx;
3377  COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3378  // FIXME: under ASan the call below may write to freed memory and corrupt
3379  // its metadata. See
3380  // https://github.com/google/sanitizers/issues/321.
3381  char *real_endptr;
3382  UINTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3383  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3384  return res;
3385}
3386
3387#define INIT_STRTOIMAX                  \
3388  COMMON_INTERCEPT_FUNCTION(strtoimax); \
3389  COMMON_INTERCEPT_FUNCTION(strtoumax);
3390#else
3391#define INIT_STRTOIMAX
3392#endif
3393
3394#if SANITIZER_INTERCEPT_MBSTOWCS
3395INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3396  void *ctx;
3397  COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3398  // FIXME: under ASan the call below may write to freed memory and corrupt
3399  // its metadata. See
3400  // https://github.com/google/sanitizers/issues/321.
3401  SIZE_T res = REAL(mbstowcs)(dest, src, len);
3402  if (res != (SIZE_T) - 1 && dest) {
3403    SIZE_T write_cnt = res + (res < len);
3404    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3405  }
3406  return res;
3407}
3408
3409INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3410            void *ps) {
3411  void *ctx;
3412  COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3413  if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3414  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3415  // FIXME: under ASan the call below may write to freed memory and corrupt
3416  // its metadata. See
3417  // https://github.com/google/sanitizers/issues/321.
3418  SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3419  if (res != (SIZE_T)(-1) && dest && src) {
3420    // This function, and several others, may or may not write the terminating
3421    // \0 character. They write it iff they clear *src.
3422    SIZE_T write_cnt = res + !*src;
3423    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3424  }
3425  return res;
3426}
3427
3428#define INIT_MBSTOWCS                  \
3429  COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3430  COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3431#else
3432#define INIT_MBSTOWCS
3433#endif
3434
3435#if SANITIZER_INTERCEPT_MBSNRTOWCS
3436INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3437            SIZE_T len, void *ps) {
3438  void *ctx;
3439  COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3440  if (src) {
3441    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3442    if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3443  }
3444  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3445  // FIXME: under ASan the call below may write to freed memory and corrupt
3446  // its metadata. See
3447  // https://github.com/google/sanitizers/issues/321.
3448  SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3449  if (res != (SIZE_T)(-1) && dest && src) {
3450    SIZE_T write_cnt = res + !*src;
3451    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3452  }
3453  return res;
3454}
3455
3456#define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3457#else
3458#define INIT_MBSNRTOWCS
3459#endif
3460
3461#if SANITIZER_INTERCEPT_WCSTOMBS
3462INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3463  void *ctx;
3464  COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3465  // FIXME: under ASan the call below may write to freed memory and corrupt
3466  // its metadata. See
3467  // https://github.com/google/sanitizers/issues/321.
3468  SIZE_T res = REAL(wcstombs)(dest, src, len);
3469  if (res != (SIZE_T) - 1 && dest) {
3470    SIZE_T write_cnt = res + (res < len);
3471    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3472  }
3473  return res;
3474}
3475
3476INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3477            void *ps) {
3478  void *ctx;
3479  COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3480  if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3481  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3482  // FIXME: under ASan the call below may write to freed memory and corrupt
3483  // its metadata. See
3484  // https://github.com/google/sanitizers/issues/321.
3485  SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3486  if (res != (SIZE_T) - 1 && dest && src) {
3487    SIZE_T write_cnt = res + !*src;
3488    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3489  }
3490  return res;
3491}
3492
3493#define INIT_WCSTOMBS                  \
3494  COMMON_INTERCEPT_FUNCTION(wcstombs); \
3495  COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3496#else
3497#define INIT_WCSTOMBS
3498#endif
3499
3500#if SANITIZER_INTERCEPT_WCSNRTOMBS
3501INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3502            SIZE_T len, void *ps) {
3503  void *ctx;
3504  COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3505  if (src) {
3506    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3507    if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3508  }
3509  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3510  // FIXME: under ASan the call below may write to freed memory and corrupt
3511  // its metadata. See
3512  // https://github.com/google/sanitizers/issues/321.
3513  SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3514  if (res != ((SIZE_T)-1) && dest && src) {
3515    SIZE_T write_cnt = res + !*src;
3516    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3517  }
3518  return res;
3519}
3520
3521#define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3522#else
3523#define INIT_WCSNRTOMBS
3524#endif
3525
3526
3527#if SANITIZER_INTERCEPT_WCRTOMB
3528INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3529  void *ctx;
3530  COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3531  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3532  // FIXME: under ASan the call below may write to freed memory and corrupt
3533  // its metadata. See
3534  // https://github.com/google/sanitizers/issues/321.
3535  SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3536  if (res != ((SIZE_T)-1) && dest) {
3537    SIZE_T write_cnt = res;
3538    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3539  }
3540  return res;
3541}
3542
3543#define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3544#else
3545#define INIT_WCRTOMB
3546#endif
3547
3548#if SANITIZER_INTERCEPT_TCGETATTR
3549INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3550  void *ctx;
3551  COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3552  // FIXME: under ASan the call below may write to freed memory and corrupt
3553  // its metadata. See
3554  // https://github.com/google/sanitizers/issues/321.
3555  int res = REAL(tcgetattr)(fd, termios_p);
3556  if (!res && termios_p)
3557    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3558  return res;
3559}
3560
3561#define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3562#else
3563#define INIT_TCGETATTR
3564#endif
3565
3566#if SANITIZER_INTERCEPT_REALPATH
3567INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3568  void *ctx;
3569  COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3570  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3571
3572  // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3573  // version of a versioned symbol. For realpath(), this gives us something
3574  // (called __old_realpath) that does not handle NULL in the second argument.
3575  // Handle it as part of the interceptor.
3576  char *allocated_path = nullptr;
3577  if (!resolved_path)
3578    allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3579
3580  char *res = REAL(realpath)(path, resolved_path);
3581  if (allocated_path && !res) WRAP(free)(allocated_path);
3582  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3583  return res;
3584}
3585#define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3586#else
3587#define INIT_REALPATH
3588#endif
3589
3590#if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3591INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3592  void *ctx;
3593  COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3594  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3595  char *res = REAL(canonicalize_file_name)(path);
3596  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3597  return res;
3598}
3599#define INIT_CANONICALIZE_FILE_NAME \
3600  COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3601#else
3602#define INIT_CANONICALIZE_FILE_NAME
3603#endif
3604
3605#if SANITIZER_INTERCEPT_CONFSTR
3606INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3607  void *ctx;
3608  COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3609  // FIXME: under ASan the call below may write to freed memory and corrupt
3610  // its metadata. See
3611  // https://github.com/google/sanitizers/issues/321.
3612  SIZE_T res = REAL(confstr)(name, buf, len);
3613  if (buf && res)
3614    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3615  return res;
3616}
3617#define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3618#else
3619#define INIT_CONFSTR
3620#endif
3621
3622#if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3623INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3624  void *ctx;
3625  COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3626  // FIXME: under ASan the call below may write to freed memory and corrupt
3627  // its metadata. See
3628  // https://github.com/google/sanitizers/issues/321.
3629  int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3630  if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3631  return res;
3632}
3633#define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3634#else
3635#define INIT_SCHED_GETAFFINITY
3636#endif
3637
3638#if SANITIZER_INTERCEPT_SCHED_GETPARAM
3639INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3640  void *ctx;
3641  COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3642  int res = REAL(sched_getparam)(pid, param);
3643  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3644  return res;
3645}
3646#define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3647#else
3648#define INIT_SCHED_GETPARAM
3649#endif
3650
3651#if SANITIZER_INTERCEPT_STRERROR
3652INTERCEPTOR(char *, strerror, int errnum) {
3653  void *ctx;
3654  COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3655  char *res = REAL(strerror)(errnum);
3656  if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3657  return res;
3658}
3659#define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3660#else
3661#define INIT_STRERROR
3662#endif
3663
3664#if SANITIZER_INTERCEPT_STRERROR_R
3665// There are 2 versions of strerror_r:
3666//  * POSIX version returns 0 on success, negative error code on failure,
3667//    writes message to buf.
3668//  * GNU version returns message pointer, which points to either buf or some
3669//    static storage.
3670#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3671    SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD ||                 \
3672    SANITIZER_FREEBSD || SANITIZER_OPENBSD
3673// POSIX version. Spec is not clear on whether buf is NULL-terminated.
3674// At least on OSX, buf contents are valid even when the call fails.
3675INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3676  void *ctx;
3677  COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3678  // FIXME: under ASan the call below may write to freed memory and corrupt
3679  // its metadata. See
3680  // https://github.com/google/sanitizers/issues/321.
3681  int res = REAL(strerror_r)(errnum, buf, buflen);
3682
3683  SIZE_T sz = internal_strnlen(buf, buflen);
3684  if (sz < buflen) ++sz;
3685  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3686  return res;
3687}
3688#else
3689// GNU version.
3690INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3691  void *ctx;
3692  COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3693  // FIXME: under ASan the call below may write to freed memory and corrupt
3694  // its metadata. See
3695  // https://github.com/google/sanitizers/issues/321.
3696  char *res = REAL(strerror_r)(errnum, buf, buflen);
3697  if (res == buf)
3698    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3699  else
3700    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3701  return res;
3702}
3703#endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3704       //SANITIZER_MAC
3705#define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3706#else
3707#define INIT_STRERROR_R
3708#endif
3709
3710#if SANITIZER_INTERCEPT_XPG_STRERROR_R
3711INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3712  void *ctx;
3713  COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3714  // FIXME: under ASan the call below may write to freed memory and corrupt
3715  // its metadata. See
3716  // https://github.com/google/sanitizers/issues/321.
3717  int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3718  // This version always returns a null-terminated string.
3719  if (buf && buflen)
3720    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3721  return res;
3722}
3723#define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3724#else
3725#define INIT_XPG_STRERROR_R
3726#endif
3727
3728#if SANITIZER_INTERCEPT_SCANDIR
3729typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3730typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3731                                const struct __sanitizer_dirent **);
3732
3733static THREADLOCAL scandir_filter_f scandir_filter;
3734static THREADLOCAL scandir_compar_f scandir_compar;
3735
3736static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3737  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3738  COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3739  return scandir_filter(dir);
3740}
3741
3742static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3743                                  const struct __sanitizer_dirent **b) {
3744  COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3745  COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3746  COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3747  COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3748  COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3749  return scandir_compar(a, b);
3750}
3751
3752INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3753            scandir_filter_f filter, scandir_compar_f compar) {
3754  void *ctx;
3755  COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3756  if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3757  scandir_filter = filter;
3758  scandir_compar = compar;
3759  // FIXME: under ASan the call below may write to freed memory and corrupt
3760  // its metadata. See
3761  // https://github.com/google/sanitizers/issues/321.
3762  int res = REAL(scandir)(dirp, namelist,
3763                          filter ? wrapped_scandir_filter : nullptr,
3764                          compar ? wrapped_scandir_compar : nullptr);
3765  scandir_filter = nullptr;
3766  scandir_compar = nullptr;
3767  if (namelist && res > 0) {
3768    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3769    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3770    for (int i = 0; i < res; ++i)
3771      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3772                                     (*namelist)[i]->d_reclen);
3773  }
3774  return res;
3775}
3776#define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3777#else
3778#define INIT_SCANDIR
3779#endif
3780
3781#if SANITIZER_INTERCEPT_SCANDIR64
3782typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3783typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3784                                  const struct __sanitizer_dirent64 **);
3785
3786static THREADLOCAL scandir64_filter_f scandir64_filter;
3787static THREADLOCAL scandir64_compar_f scandir64_compar;
3788
3789static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3790  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3791  COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3792  return scandir64_filter(dir);
3793}
3794
3795static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3796                                    const struct __sanitizer_dirent64 **b) {
3797  COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3798  COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3799  COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3800  COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3801  COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3802  return scandir64_compar(a, b);
3803}
3804
3805INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3806            scandir64_filter_f filter, scandir64_compar_f compar) {
3807  void *ctx;
3808  COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3809  if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3810  scandir64_filter = filter;
3811  scandir64_compar = compar;
3812  // FIXME: under ASan the call below may write to freed memory and corrupt
3813  // its metadata. See
3814  // https://github.com/google/sanitizers/issues/321.
3815  int res =
3816      REAL(scandir64)(dirp, namelist,
3817                      filter ? wrapped_scandir64_filter : nullptr,
3818                      compar ? wrapped_scandir64_compar : nullptr);
3819  scandir64_filter = nullptr;
3820  scandir64_compar = nullptr;
3821  if (namelist && res > 0) {
3822    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3823    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3824    for (int i = 0; i < res; ++i)
3825      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3826                                     (*namelist)[i]->d_reclen);
3827  }
3828  return res;
3829}
3830#define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3831#else
3832#define INIT_SCANDIR64
3833#endif
3834
3835#if SANITIZER_INTERCEPT_GETGROUPS
3836INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3837  void *ctx;
3838  COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3839  // FIXME: under ASan the call below may write to freed memory and corrupt
3840  // its metadata. See
3841  // https://github.com/google/sanitizers/issues/321.
3842  int res = REAL(getgroups)(size, lst);
3843  if (res >= 0 && lst && size > 0)
3844    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3845  return res;
3846}
3847#define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3848#else
3849#define INIT_GETGROUPS
3850#endif
3851
3852#if SANITIZER_INTERCEPT_POLL
3853static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3854                        __sanitizer_nfds_t nfds) {
3855  for (unsigned i = 0; i < nfds; ++i) {
3856    COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3857    COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3858  }
3859}
3860
3861static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3862                         __sanitizer_nfds_t nfds) {
3863  for (unsigned i = 0; i < nfds; ++i)
3864    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3865                                   sizeof(fds[i].revents));
3866}
3867
3868INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3869            int timeout) {
3870  void *ctx;
3871  COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3872  if (fds && nfds) read_pollfd(ctx, fds, nfds);
3873  int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3874  if (fds && nfds) write_pollfd(ctx, fds, nfds);
3875  return res;
3876}
3877#define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3878#else
3879#define INIT_POLL
3880#endif
3881
3882#if SANITIZER_INTERCEPT_PPOLL
3883INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3884            void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3885  void *ctx;
3886  COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3887  if (fds && nfds) read_pollfd(ctx, fds, nfds);
3888  if (timeout_ts)
3889    COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3890  if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3891  int res =
3892      COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3893  if (fds && nfds) write_pollfd(ctx, fds, nfds);
3894  return res;
3895}
3896#define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3897#else
3898#define INIT_PPOLL
3899#endif
3900
3901#if SANITIZER_INTERCEPT_WORDEXP
3902INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3903  void *ctx;
3904  COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3905  if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3906  // FIXME: under ASan the call below may write to freed memory and corrupt
3907  // its metadata. See
3908  // https://github.com/google/sanitizers/issues/321.
3909  int res = REAL(wordexp)(s, p, flags);
3910  if (!res && p) {
3911    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3912    if (p->we_wordc)
3913      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3914                                     sizeof(*p->we_wordv) * p->we_wordc);
3915    for (uptr i = 0; i < p->we_wordc; ++i) {
3916      char *w = p->we_wordv[i];
3917      if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3918    }
3919  }
3920  return res;
3921}
3922#define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3923#else
3924#define INIT_WORDEXP
3925#endif
3926
3927#if SANITIZER_INTERCEPT_SIGWAIT
3928INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3929  void *ctx;
3930  COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3931  if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3932  // FIXME: under ASan the call below may write to freed memory and corrupt
3933  // its metadata. See
3934  // https://github.com/google/sanitizers/issues/321.
3935  int res = REAL(sigwait)(set, sig);
3936  if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3937  return res;
3938}
3939#define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3940#else
3941#define INIT_SIGWAIT
3942#endif
3943
3944#if SANITIZER_INTERCEPT_SIGWAITINFO
3945INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3946  void *ctx;
3947  COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3948  if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3949  // FIXME: under ASan the call below may write to freed memory and corrupt
3950  // its metadata. See
3951  // https://github.com/google/sanitizers/issues/321.
3952  int res = REAL(sigwaitinfo)(set, info);
3953  if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3954  return res;
3955}
3956#define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3957#else
3958#define INIT_SIGWAITINFO
3959#endif
3960
3961#if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3962INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3963            void *timeout) {
3964  void *ctx;
3965  COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3966  if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3967  if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3968  // FIXME: under ASan the call below may write to freed memory and corrupt
3969  // its metadata. See
3970  // https://github.com/google/sanitizers/issues/321.
3971  int res = REAL(sigtimedwait)(set, info, timeout);
3972  if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3973  return res;
3974}
3975#define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3976#else
3977#define INIT_SIGTIMEDWAIT
3978#endif
3979
3980#if SANITIZER_INTERCEPT_SIGSETOPS
3981INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3982  void *ctx;
3983  COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3984  // FIXME: under ASan the call below may write to freed memory and corrupt
3985  // its metadata. See
3986  // https://github.com/google/sanitizers/issues/321.
3987  int res = REAL(sigemptyset)(set);
3988  if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3989  return res;
3990}
3991
3992INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3993  void *ctx;
3994  COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3995  // FIXME: under ASan the call below may write to freed memory and corrupt
3996  // its metadata. See
3997  // https://github.com/google/sanitizers/issues/321.
3998  int res = REAL(sigfillset)(set);
3999  if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4000  return res;
4001}
4002#define INIT_SIGSETOPS                    \
4003  COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4004  COMMON_INTERCEPT_FUNCTION(sigfillset);
4005#else
4006#define INIT_SIGSETOPS
4007#endif
4008
4009#if SANITIZER_INTERCEPT_SIGPENDING
4010INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4011  void *ctx;
4012  COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4013  // FIXME: under ASan the call below may write to freed memory and corrupt
4014  // its metadata. See
4015  // https://github.com/google/sanitizers/issues/321.
4016  int res = REAL(sigpending)(set);
4017  if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4018  return res;
4019}
4020#define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4021#else
4022#define INIT_SIGPENDING
4023#endif
4024
4025#if SANITIZER_INTERCEPT_SIGPROCMASK
4026INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4027            __sanitizer_sigset_t *oldset) {
4028  void *ctx;
4029  COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4030  if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4031  // FIXME: under ASan the call below may write to freed memory and corrupt
4032  // its metadata. See
4033  // https://github.com/google/sanitizers/issues/321.
4034  int res = REAL(sigprocmask)(how, set, oldset);
4035  if (!res && oldset)
4036    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4037  return res;
4038}
4039#define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4040#else
4041#define INIT_SIGPROCMASK
4042#endif
4043
4044#if SANITIZER_INTERCEPT_BACKTRACE
4045INTERCEPTOR(int, backtrace, void **buffer, int size) {
4046  void *ctx;
4047  COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4048  // FIXME: under ASan the call below may write to freed memory and corrupt
4049  // its metadata. See
4050  // https://github.com/google/sanitizers/issues/321.
4051  int res = REAL(backtrace)(buffer, size);
4052  if (res && buffer)
4053    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4054  return res;
4055}
4056
4057INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4058  void *ctx;
4059  COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4060  if (buffer && size)
4061    COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4062  // FIXME: under ASan the call below may write to freed memory and corrupt
4063  // its metadata. See
4064  // https://github.com/google/sanitizers/issues/321.
4065  char **res = REAL(backtrace_symbols)(buffer, size);
4066  if (res && size) {
4067    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4068    for (int i = 0; i < size; ++i)
4069      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4070  }
4071  return res;
4072}
4073#define INIT_BACKTRACE                  \
4074  COMMON_INTERCEPT_FUNCTION(backtrace); \
4075  COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4076#else
4077#define INIT_BACKTRACE
4078#endif
4079
4080#if SANITIZER_INTERCEPT__EXIT
4081INTERCEPTOR(void, _exit, int status) {
4082  void *ctx;
4083  COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4084  COMMON_INTERCEPTOR_USER_CALLBACK_START();
4085  int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4086  COMMON_INTERCEPTOR_USER_CALLBACK_END();
4087  if (status == 0) status = status1;
4088  REAL(_exit)(status);
4089}
4090#define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4091#else
4092#define INIT__EXIT
4093#endif
4094
4095#if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4096INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4097  void *ctx;
4098  COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4099  COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4100  int res = REAL(pthread_mutex_lock)(m);
4101  if (res == errno_EOWNERDEAD)
4102    COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4103  if (res == 0 || res == errno_EOWNERDEAD)
4104    COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4105  if (res == errno_EINVAL)
4106    COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4107  return res;
4108}
4109
4110INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4111  void *ctx;
4112  COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4113  COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4114  int res = REAL(pthread_mutex_unlock)(m);
4115  if (res == errno_EINVAL)
4116    COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4117  return res;
4118}
4119
4120#define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4121#define INIT_PTHREAD_MUTEX_UNLOCK \
4122  COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4123#else
4124#define INIT_PTHREAD_MUTEX_LOCK
4125#define INIT_PTHREAD_MUTEX_UNLOCK
4126#endif
4127
4128#if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4129INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4130  return WRAP(pthread_mutex_lock)(m);
4131}
4132
4133INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4134  return WRAP(pthread_mutex_unlock)(m);
4135}
4136
4137#define INIT___PTHREAD_MUTEX_LOCK \
4138  COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4139#define INIT___PTHREAD_MUTEX_UNLOCK \
4140  COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4141#else
4142#define INIT___PTHREAD_MUTEX_LOCK
4143#define INIT___PTHREAD_MUTEX_UNLOCK
4144#endif
4145
4146#if SANITIZER_INTERCEPT___LIBC_MUTEX
4147INTERCEPTOR(int, __libc_mutex_lock, void *m)
4148ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4149
4150INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4151ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4152
4153INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4154ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4155
4156#define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4157#define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4158#define INIT___LIBC_THR_SETCANCELSTATE \
4159  COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4160#else
4161#define INIT___LIBC_MUTEX_LOCK
4162#define INIT___LIBC_MUTEX_UNLOCK
4163#define INIT___LIBC_THR_SETCANCELSTATE
4164#endif
4165
4166#if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4167static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4168  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4169  if (mnt->mnt_fsname)
4170    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4171                                   REAL(strlen)(mnt->mnt_fsname) + 1);
4172  if (mnt->mnt_dir)
4173    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4174                                   REAL(strlen)(mnt->mnt_dir) + 1);
4175  if (mnt->mnt_type)
4176    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4177                                   REAL(strlen)(mnt->mnt_type) + 1);
4178  if (mnt->mnt_opts)
4179    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4180                                   REAL(strlen)(mnt->mnt_opts) + 1);
4181}
4182#endif
4183
4184#if SANITIZER_INTERCEPT_GETMNTENT
4185INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4186  void *ctx;
4187  COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4188  __sanitizer_mntent *res = REAL(getmntent)(fp);
4189  if (res) write_mntent(ctx, res);
4190  return res;
4191}
4192#define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4193#else
4194#define INIT_GETMNTENT
4195#endif
4196
4197#if SANITIZER_INTERCEPT_GETMNTENT_R
4198INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4199            __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4200  void *ctx;
4201  COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4202  __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4203  if (res) write_mntent(ctx, res);
4204  return res;
4205}
4206#define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4207#else
4208#define INIT_GETMNTENT_R
4209#endif
4210
4211#if SANITIZER_INTERCEPT_STATFS
4212INTERCEPTOR(int, statfs, char *path, void *buf) {
4213  void *ctx;
4214  COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4215  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4216  // FIXME: under ASan the call below may write to freed memory and corrupt
4217  // its metadata. See
4218  // https://github.com/google/sanitizers/issues/321.
4219  int res = REAL(statfs)(path, buf);
4220  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4221  return res;
4222}
4223INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4224  void *ctx;
4225  COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4226  // FIXME: under ASan the call below may write to freed memory and corrupt
4227  // its metadata. See
4228  // https://github.com/google/sanitizers/issues/321.
4229  int res = REAL(fstatfs)(fd, buf);
4230  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4231  return res;
4232}
4233#define INIT_STATFS                  \
4234  COMMON_INTERCEPT_FUNCTION(statfs); \
4235  COMMON_INTERCEPT_FUNCTION(fstatfs);
4236#else
4237#define INIT_STATFS
4238#endif
4239
4240#if SANITIZER_INTERCEPT_STATFS64
4241INTERCEPTOR(int, statfs64, char *path, void *buf) {
4242  void *ctx;
4243  COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4244  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4245  // FIXME: under ASan the call below may write to freed memory and corrupt
4246  // its metadata. See
4247  // https://github.com/google/sanitizers/issues/321.
4248  int res = REAL(statfs64)(path, buf);
4249  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4250  return res;
4251}
4252INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4253  void *ctx;
4254  COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4255  // FIXME: under ASan the call below may write to freed memory and corrupt
4256  // its metadata. See
4257  // https://github.com/google/sanitizers/issues/321.
4258  int res = REAL(fstatfs64)(fd, buf);
4259  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4260  return res;
4261}
4262#define INIT_STATFS64                  \
4263  COMMON_INTERCEPT_FUNCTION(statfs64); \
4264  COMMON_INTERCEPT_FUNCTION(fstatfs64);
4265#else
4266#define INIT_STATFS64
4267#endif
4268
4269#if SANITIZER_INTERCEPT_STATVFS
4270INTERCEPTOR(int, statvfs, char *path, void *buf) {
4271  void *ctx;
4272  COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4273  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4274  // FIXME: under ASan the call below may write to freed memory and corrupt
4275  // its metadata. See
4276  // https://github.com/google/sanitizers/issues/321.
4277  int res = REAL(statvfs)(path, buf);
4278  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4279  return res;
4280}
4281INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4282  void *ctx;
4283  COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4284  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4285  // FIXME: under ASan the call below may write to freed memory and corrupt
4286  // its metadata. See
4287  // https://github.com/google/sanitizers/issues/321.
4288  int res = REAL(fstatvfs)(fd, buf);
4289  if (!res) {
4290    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4291    if (fd >= 0)
4292      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4293  }
4294  return res;
4295}
4296#define INIT_STATVFS                  \
4297  COMMON_INTERCEPT_FUNCTION(statvfs); \
4298  COMMON_INTERCEPT_FUNCTION(fstatvfs);
4299#else
4300#define INIT_STATVFS
4301#endif
4302
4303#if SANITIZER_INTERCEPT_STATVFS64
4304INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4305  void *ctx;
4306  COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4307  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4308  // FIXME: under ASan the call below may write to freed memory and corrupt
4309  // its metadata. See
4310  // https://github.com/google/sanitizers/issues/321.
4311  int res = REAL(statvfs64)(path, buf);
4312  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4313  return res;
4314}
4315INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4316  void *ctx;
4317  COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4318  // FIXME: under ASan the call below may write to freed memory and corrupt
4319  // its metadata. See
4320  // https://github.com/google/sanitizers/issues/321.
4321  int res = REAL(fstatvfs64)(fd, buf);
4322  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4323  return res;
4324}
4325#define INIT_STATVFS64                  \
4326  COMMON_INTERCEPT_FUNCTION(statvfs64); \
4327  COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4328#else
4329#define INIT_STATVFS64
4330#endif
4331
4332#if SANITIZER_INTERCEPT_INITGROUPS
4333INTERCEPTOR(int, initgroups, char *user, u32 group) {
4334  void *ctx;
4335  COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4336  if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4337  int res = REAL(initgroups)(user, group);
4338  return res;
4339}
4340#define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4341#else
4342#define INIT_INITGROUPS
4343#endif
4344
4345#if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4346INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4347  void *ctx;
4348  COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4349  if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4350  char *res = REAL(ether_ntoa)(addr);
4351  if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4352  return res;
4353}
4354INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4355  void *ctx;
4356  COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4357  if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4358  __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4359  if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4360  return res;
4361}
4362#define INIT_ETHER_NTOA_ATON             \
4363  COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4364  COMMON_INTERCEPT_FUNCTION(ether_aton);
4365#else
4366#define INIT_ETHER_NTOA_ATON
4367#endif
4368
4369#if SANITIZER_INTERCEPT_ETHER_HOST
4370INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4371  void *ctx;
4372  COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4373  if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4374  // FIXME: under ASan the call below may write to freed memory and corrupt
4375  // its metadata. See
4376  // https://github.com/google/sanitizers/issues/321.
4377  int res = REAL(ether_ntohost)(hostname, addr);
4378  if (!res && hostname)
4379    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4380  return res;
4381}
4382INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4383  void *ctx;
4384  COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4385  if (hostname)
4386    COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4387  // FIXME: under ASan the call below may write to freed memory and corrupt
4388  // its metadata. See
4389  // https://github.com/google/sanitizers/issues/321.
4390  int res = REAL(ether_hostton)(hostname, addr);
4391  if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4392  return res;
4393}
4394INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4395            char *hostname) {
4396  void *ctx;
4397  COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4398  if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4399  // FIXME: under ASan the call below may write to freed memory and corrupt
4400  // its metadata. See
4401  // https://github.com/google/sanitizers/issues/321.
4402  int res = REAL(ether_line)(line, addr, hostname);
4403  if (!res) {
4404    if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4405    if (hostname)
4406      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4407  }
4408  return res;
4409}
4410#define INIT_ETHER_HOST                     \
4411  COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4412  COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4413  COMMON_INTERCEPT_FUNCTION(ether_line);
4414#else
4415#define INIT_ETHER_HOST
4416#endif
4417
4418#if SANITIZER_INTERCEPT_ETHER_R
4419INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4420  void *ctx;
4421  COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4422  if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4423  // FIXME: under ASan the call below may write to freed memory and corrupt
4424  // its metadata. See
4425  // https://github.com/google/sanitizers/issues/321.
4426  char *res = REAL(ether_ntoa_r)(addr, buf);
4427  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4428  return res;
4429}
4430INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4431            __sanitizer_ether_addr *addr) {
4432  void *ctx;
4433  COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4434  if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4435  // FIXME: under ASan the call below may write to freed memory and corrupt
4436  // its metadata. See
4437  // https://github.com/google/sanitizers/issues/321.
4438  __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4439  if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4440  return res;
4441}
4442#define INIT_ETHER_R                       \
4443  COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4444  COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4445#else
4446#define INIT_ETHER_R
4447#endif
4448
4449#if SANITIZER_INTERCEPT_SHMCTL
4450INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4451  void *ctx;
4452  COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4453  // FIXME: under ASan the call below may write to freed memory and corrupt
4454  // its metadata. See
4455  // https://github.com/google/sanitizers/issues/321.
4456  int res = REAL(shmctl)(shmid, cmd, buf);
4457  if (res >= 0) {
4458    unsigned sz = 0;
4459    if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4460      sz = sizeof(__sanitizer_shmid_ds);
4461    else if (cmd == shmctl_ipc_info)
4462      sz = struct_shminfo_sz;
4463    else if (cmd == shmctl_shm_info)
4464      sz = struct_shm_info_sz;
4465    if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4466  }
4467  return res;
4468}
4469#define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4470#else
4471#define INIT_SHMCTL
4472#endif
4473
4474#if SANITIZER_INTERCEPT_RANDOM_R
4475INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4476  void *ctx;
4477  COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4478  // FIXME: under ASan the call below may write to freed memory and corrupt
4479  // its metadata. See
4480  // https://github.com/google/sanitizers/issues/321.
4481  int res = REAL(random_r)(buf, result);
4482  if (!res && result)
4483    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4484  return res;
4485}
4486#define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4487#else
4488#define INIT_RANDOM_R
4489#endif
4490
4491// FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4492// its metadata. See
4493// https://github.com/google/sanitizers/issues/321.
4494#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET ||              \
4495    SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED ||        \
4496    SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4497    SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET ||         \
4498    SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET ||        \
4499    SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET ||          \
4500    SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4501#define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz)            \
4502  INTERCEPTOR(int, fn, void *attr, void *r) {                  \
4503    void *ctx;                                                 \
4504    COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r);                \
4505    int res = REAL(fn)(attr, r);                               \
4506    if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4507    return res;                                                \
4508  }
4509#define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4510  INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4511#define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4512  INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4513#define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4514  INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4515#define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4516  INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4517#define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4518  INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4519#endif
4520
4521#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4522INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4523INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4524INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4525INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4526INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4527  void *ctx;
4528  COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4529  // FIXME: under ASan the call below may write to freed memory and corrupt
4530  // its metadata. See
4531  // https://github.com/google/sanitizers/issues/321.
4532  int res = REAL(pthread_attr_getstack)(attr, addr, size);
4533  if (!res) {
4534    if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4535    if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4536  }
4537  return res;
4538}
4539
4540// We may need to call the real pthread_attr_getstack from the run-time
4541// in sanitizer_common, but we don't want to include the interception headers
4542// there. So, just define this function here.
4543namespace __sanitizer {
4544extern "C" {
4545int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4546  return REAL(pthread_attr_getstack)(attr, addr, size);
4547}
4548}  // extern "C"
4549}  // namespace __sanitizer
4550
4551#define INIT_PTHREAD_ATTR_GET                             \
4552  COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4553  COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize);   \
4554  COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope);       \
4555  COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize);   \
4556  COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4557#else
4558#define INIT_PTHREAD_ATTR_GET
4559#endif
4560
4561#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4562INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4563INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4564
4565#define INIT_PTHREAD_ATTR_GET_SCHED                      \
4566  COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4567  COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4568#else
4569#define INIT_PTHREAD_ATTR_GET_SCHED
4570#endif
4571
4572#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4573INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4574
4575#define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4576  COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4577#else
4578#define INIT_PTHREAD_ATTR_GETINHERITSCHED
4579#endif
4580
4581#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4582INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4583            void *cpuset) {
4584  void *ctx;
4585  COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4586                           cpuset);
4587  // FIXME: under ASan the call below may write to freed memory and corrupt
4588  // its metadata. See
4589  // https://github.com/google/sanitizers/issues/321.
4590  int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4591  if (!res && cpusetsize && cpuset)
4592    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4593  return res;
4594}
4595
4596#define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4597  COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4598#else
4599#define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4600#endif
4601
4602#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4603INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4604#define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4605  COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4606#else
4607#define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4608#endif
4609
4610#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4611INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4612#define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4613  COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4614#else
4615#define INIT_PTHREAD_MUTEXATTR_GETTYPE
4616#endif
4617
4618#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4619INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4620#define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4621  COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4622#else
4623#define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4624#endif
4625
4626#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4627INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4628#define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4629  COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4630#else
4631#define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4632#endif
4633
4634#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4635INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4636#define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4637  COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4638#else
4639#define INIT_PTHREAD_MUTEXATTR_GETROBUST
4640#endif
4641
4642#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4643INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4644#define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4645  COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4646#else
4647#define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4648#endif
4649
4650#if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4651INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4652#define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4653  COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4654#else
4655#define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4656#endif
4657
4658#if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4659INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4660#define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4661  COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4662#else
4663#define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4664#endif
4665
4666#if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4667INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4668#define INIT_PTHREAD_CONDATTR_GETPSHARED \
4669  COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4670#else
4671#define INIT_PTHREAD_CONDATTR_GETPSHARED
4672#endif
4673
4674#if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4675INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4676#define INIT_PTHREAD_CONDATTR_GETCLOCK \
4677  COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4678#else
4679#define INIT_PTHREAD_CONDATTR_GETCLOCK
4680#endif
4681
4682#if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4683INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4684#define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4685  COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4686#else
4687#define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4688#endif
4689
4690#if SANITIZER_INTERCEPT_TMPNAM
4691INTERCEPTOR(char *, tmpnam, char *s) {
4692  void *ctx;
4693  COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4694  char *res = REAL(tmpnam)(s);
4695  if (res) {
4696    if (s)
4697      // FIXME: under ASan the call below may write to freed memory and corrupt
4698      // its metadata. See
4699      // https://github.com/google/sanitizers/issues/321.
4700      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4701    else
4702      COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4703  }
4704  return res;
4705}
4706#define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4707#else
4708#define INIT_TMPNAM
4709#endif
4710
4711#if SANITIZER_INTERCEPT_TMPNAM_R
4712INTERCEPTOR(char *, tmpnam_r, char *s) {
4713  void *ctx;
4714  COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4715  // FIXME: under ASan the call below may write to freed memory and corrupt
4716  // its metadata. See
4717  // https://github.com/google/sanitizers/issues/321.
4718  char *res = REAL(tmpnam_r)(s);
4719  if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4720  return res;
4721}
4722#define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4723#else
4724#define INIT_TMPNAM_R
4725#endif
4726
4727#if SANITIZER_INTERCEPT_TTYNAME_R
4728INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4729  void *ctx;
4730  COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4731  int res = REAL(ttyname_r)(fd, name, namesize);
4732  if (res == 0)
4733    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4734  return res;
4735}
4736#define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4737#else
4738#define INIT_TTYNAME_R
4739#endif
4740
4741#if SANITIZER_INTERCEPT_TEMPNAM
4742INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4743  void *ctx;
4744  COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4745  if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4746  if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4747  char *res = REAL(tempnam)(dir, pfx);
4748  if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4749  return res;
4750}
4751#define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4752#else
4753#define INIT_TEMPNAM
4754#endif
4755
4756#if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4757INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4758  void *ctx;
4759  COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4760  COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4761  COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4762  return REAL(pthread_setname_np)(thread, name);
4763}
4764#define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4765#elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4766INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4767  void *ctx;
4768  char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4769  COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4770  COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4771  internal_snprintf(newname, sizeof(newname), name, arg);
4772  COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4773  return REAL(pthread_setname_np)(thread, name, arg);
4774}
4775#define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4776#else
4777#define INIT_PTHREAD_SETNAME_NP
4778#endif
4779
4780#if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4781INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4782  void *ctx;
4783  COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4784  int res = REAL(pthread_getname_np)(thread, name, len);
4785  if (!res)
4786    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4787  return res;
4788}
4789#define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4790#else
4791#define INIT_PTHREAD_GETNAME_NP
4792#endif
4793
4794#if SANITIZER_INTERCEPT_SINCOS
4795INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4796  void *ctx;
4797  COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4798  // FIXME: under ASan the call below may write to freed memory and corrupt
4799  // its metadata. See
4800  // https://github.com/google/sanitizers/issues/321.
4801  REAL(sincos)(x, sin, cos);
4802  if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4803  if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4804}
4805INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4806  void *ctx;
4807  COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4808  // FIXME: under ASan the call below may write to freed memory and corrupt
4809  // its metadata. See
4810  // https://github.com/google/sanitizers/issues/321.
4811  REAL(sincosf)(x, sin, cos);
4812  if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4813  if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4814}
4815INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4816  void *ctx;
4817  COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4818  // FIXME: under ASan the call below may write to freed memory and corrupt
4819  // its metadata. See
4820  // https://github.com/google/sanitizers/issues/321.
4821  REAL(sincosl)(x, sin, cos);
4822  if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4823  if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4824}
4825#define INIT_SINCOS                   \
4826  COMMON_INTERCEPT_FUNCTION(sincos);  \
4827  COMMON_INTERCEPT_FUNCTION(sincosf); \
4828  COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4829#else
4830#define INIT_SINCOS
4831#endif
4832
4833#if SANITIZER_INTERCEPT_REMQUO
4834INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4835  void *ctx;
4836  COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4837  // FIXME: under ASan the call below may write to freed memory and corrupt
4838  // its metadata. See
4839  // https://github.com/google/sanitizers/issues/321.
4840  double res = REAL(remquo)(x, y, quo);
4841  if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4842  return res;
4843}
4844INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4845  void *ctx;
4846  COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4847  // FIXME: under ASan the call below may write to freed memory and corrupt
4848  // its metadata. See
4849  // https://github.com/google/sanitizers/issues/321.
4850  float res = REAL(remquof)(x, y, quo);
4851  if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4852  return res;
4853}
4854#define INIT_REMQUO                   \
4855  COMMON_INTERCEPT_FUNCTION(remquo);  \
4856  COMMON_INTERCEPT_FUNCTION(remquof);
4857#else
4858#define INIT_REMQUO
4859#endif
4860
4861#if SANITIZER_INTERCEPT_REMQUOL
4862INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4863  void *ctx;
4864  COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4865  // FIXME: under ASan the call below may write to freed memory and corrupt
4866  // its metadata. See
4867  // https://github.com/google/sanitizers/issues/321.
4868  long double res = REAL(remquol)(x, y, quo);
4869  if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4870  return res;
4871}
4872#define INIT_REMQUOL                  \
4873  COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4874#else
4875#define INIT_REMQUOL
4876#endif
4877
4878#if SANITIZER_INTERCEPT_LGAMMA
4879extern int signgam;
4880INTERCEPTOR(double, lgamma, double x) {
4881  void *ctx;
4882  COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4883  double res = REAL(lgamma)(x);
4884  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4885  return res;
4886}
4887INTERCEPTOR(float, lgammaf, float x) {
4888  void *ctx;
4889  COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4890  float res = REAL(lgammaf)(x);
4891  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4892  return res;
4893}
4894#define INIT_LGAMMA                   \
4895  COMMON_INTERCEPT_FUNCTION(lgamma);  \
4896  COMMON_INTERCEPT_FUNCTION(lgammaf);
4897#else
4898#define INIT_LGAMMA
4899#endif
4900
4901#if SANITIZER_INTERCEPT_LGAMMAL
4902INTERCEPTOR(long double, lgammal, long double x) {
4903  void *ctx;
4904  COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4905  long double res = REAL(lgammal)(x);
4906  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4907  return res;
4908}
4909#define INIT_LGAMMAL                  \
4910  COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4911#else
4912#define INIT_LGAMMAL
4913#endif
4914
4915#if SANITIZER_INTERCEPT_LGAMMA_R
4916INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4917  void *ctx;
4918  COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4919  // FIXME: under ASan the call below may write to freed memory and corrupt
4920  // its metadata. See
4921  // https://github.com/google/sanitizers/issues/321.
4922  double res = REAL(lgamma_r)(x, signp);
4923  if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4924  return res;
4925}
4926INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4927  void *ctx;
4928  COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4929  // FIXME: under ASan the call below may write to freed memory and corrupt
4930  // its metadata. See
4931  // https://github.com/google/sanitizers/issues/321.
4932  float res = REAL(lgammaf_r)(x, signp);
4933  if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4934  return res;
4935}
4936#define INIT_LGAMMA_R                   \
4937  COMMON_INTERCEPT_FUNCTION(lgamma_r);  \
4938  COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4939#else
4940#define INIT_LGAMMA_R
4941#endif
4942
4943#if SANITIZER_INTERCEPT_LGAMMAL_R
4944INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4945  void *ctx;
4946  COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4947  // FIXME: under ASan the call below may write to freed memory and corrupt
4948  // its metadata. See
4949  // https://github.com/google/sanitizers/issues/321.
4950  long double res = REAL(lgammal_r)(x, signp);
4951  if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4952  return res;
4953}
4954#define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4955#else
4956#define INIT_LGAMMAL_R
4957#endif
4958
4959#if SANITIZER_INTERCEPT_DRAND48_R
4960INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4961  void *ctx;
4962  COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4963  // FIXME: under ASan the call below may write to freed memory and corrupt
4964  // its metadata. See
4965  // https://github.com/google/sanitizers/issues/321.
4966  int res = REAL(drand48_r)(buffer, result);
4967  if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4968  return res;
4969}
4970INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4971  void *ctx;
4972  COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4973  // FIXME: under ASan the call below may write to freed memory and corrupt
4974  // its metadata. See
4975  // https://github.com/google/sanitizers/issues/321.
4976  int res = REAL(lrand48_r)(buffer, result);
4977  if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4978  return res;
4979}
4980#define INIT_DRAND48_R                  \
4981  COMMON_INTERCEPT_FUNCTION(drand48_r); \
4982  COMMON_INTERCEPT_FUNCTION(lrand48_r);
4983#else
4984#define INIT_DRAND48_R
4985#endif
4986
4987#if SANITIZER_INTERCEPT_RAND_R
4988INTERCEPTOR(int, rand_r, unsigned *seedp) {
4989  void *ctx;
4990  COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4991  COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4992  return REAL(rand_r)(seedp);
4993}
4994#define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4995#else
4996#define INIT_RAND_R
4997#endif
4998
4999#if SANITIZER_INTERCEPT_GETLINE
5000INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5001  void *ctx;
5002  COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5003  // FIXME: under ASan the call below may write to freed memory and corrupt
5004  // its metadata. See
5005  // https://github.com/google/sanitizers/issues/321.
5006  SSIZE_T res = REAL(getline)(lineptr, n, stream);
5007  if (res > 0) {
5008    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5009    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5010    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5011  }
5012  return res;
5013}
5014
5015// FIXME: under ASan the call below may write to freed memory and corrupt its
5016// metadata. See
5017// https://github.com/google/sanitizers/issues/321.
5018#define GETDELIM_INTERCEPTOR_IMPL(vname)                                       \
5019  {                                                                            \
5020    void *ctx;                                                                 \
5021    COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream);           \
5022    SSIZE_T res = REAL(vname)(lineptr, n, delim, stream);                      \
5023    if (res > 0) {                                                             \
5024      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));          \
5025      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));                      \
5026      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);                  \
5027    }                                                                          \
5028    return res;                                                                \
5029  }
5030
5031INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5032            void *stream)
5033GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5034
5035// There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5036// with its own body.
5037INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5038            void *stream)
5039GETDELIM_INTERCEPTOR_IMPL(getdelim)
5040
5041#define INIT_GETLINE                     \
5042  COMMON_INTERCEPT_FUNCTION(getline);    \
5043  COMMON_INTERCEPT_FUNCTION(__getdelim); \
5044  COMMON_INTERCEPT_FUNCTION(getdelim);
5045#else
5046#define INIT_GETLINE
5047#endif
5048
5049#if SANITIZER_INTERCEPT_ICONV
5050INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5051            char **outbuf, SIZE_T *outbytesleft) {
5052  void *ctx;
5053  COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5054                           outbytesleft);
5055  if (inbytesleft)
5056    COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5057  if (inbuf && inbytesleft)
5058    COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5059  if (outbytesleft)
5060    COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5061  void *outbuf_orig = outbuf ? *outbuf : nullptr;
5062  // FIXME: under ASan the call below may write to freed memory and corrupt
5063  // its metadata. See
5064  // https://github.com/google/sanitizers/issues/321.
5065  SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5066  if (outbuf && *outbuf > outbuf_orig) {
5067    SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5068    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5069  }
5070  return res;
5071}
5072#define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5073#else
5074#define INIT_ICONV
5075#endif
5076
5077#if SANITIZER_INTERCEPT_TIMES
5078INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5079  void *ctx;
5080  COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5081  // FIXME: under ASan the call below may write to freed memory and corrupt
5082  // its metadata. See
5083  // https://github.com/google/sanitizers/issues/321.
5084  __sanitizer_clock_t res = REAL(times)(tms);
5085  if (res != (__sanitizer_clock_t)-1 && tms)
5086    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5087  return res;
5088}
5089#define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5090#else
5091#define INIT_TIMES
5092#endif
5093
5094#if SANITIZER_INTERCEPT_TLS_GET_ADDR
5095#if !SANITIZER_S390
5096#define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5097// If you see any crashes around this functions, there are 2 known issues with
5098// it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5099// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5100// 2. It can be called recursively if sanitizer code uses __tls_get_addr
5101// to access thread local variables (it should not happen normally,
5102// because sanitizers use initial-exec tls model).
5103INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5104  void *ctx;
5105  COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5106  void *res = REAL(__tls_get_addr)(arg);
5107  uptr tls_begin, tls_end;
5108  COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5109  DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5110  if (dtv) {
5111    // New DTLS block has been allocated.
5112    COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5113  }
5114  return res;
5115}
5116#if SANITIZER_PPC
5117// On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5118// mostly the same semantics as __tls_get_addr, but its presence enables
5119// some optimizations in linker (which are safe to ignore here).
5120extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5121                          visibility("default")))
5122void *__tls_get_addr_opt(void *arg);
5123#endif
5124#else // SANITIZER_S390
5125// On s390, we have to intercept two functions here:
5126// - __tls_get_addr_internal, which is a glibc-internal function that is like
5127//   the usual __tls_get_addr, but returns a TP-relative offset instead of
5128//   a proper pointer.  It is used by dlsym for TLS symbols.
5129// - __tls_get_offset, which is like the above, but also takes a GOT-relative
5130//   descriptor offset as an argument instead of a pointer.  GOT address
5131//   is passed in r12, so it's necessary to write it in assembly.  This is
5132//   the function used by the compiler.
5133extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5134#define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5135DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5136extern "C" uptr __tls_get_offset(void *arg);
5137extern "C" uptr __interceptor___tls_get_offset(void *arg);
5138INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5139  void *ctx;
5140  COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5141  uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5142  uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5143  void *ptr = reinterpret_cast<void *>(res + tp);
5144  uptr tls_begin, tls_end;
5145  COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5146  DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5147  if (dtv) {
5148    // New DTLS block has been allocated.
5149    COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5150  }
5151  return res;
5152}
5153// We need a hidden symbol aliasing the above, so that we can jump
5154// directly to it from the assembly below.
5155extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5156                          visibility("hidden")))
5157uptr __tls_get_addr_hidden(void *arg);
5158// Now carefully intercept __tls_get_offset.
5159asm(
5160  ".text\n"
5161// The __intercept_ version has to exist, so that gen_dynamic_list.py
5162// exports our symbol.
5163  ".weak __tls_get_offset\n"
5164  ".type __tls_get_offset, @function\n"
5165  "__tls_get_offset:\n"
5166  ".global __interceptor___tls_get_offset\n"
5167  ".type __interceptor___tls_get_offset, @function\n"
5168  "__interceptor___tls_get_offset:\n"
5169#ifdef __s390x__
5170  "la %r2, 0(%r2,%r12)\n"
5171  "jg __tls_get_addr_hidden\n"
5172#else
5173  "basr %r3,0\n"
5174  "0: la %r2,0(%r2,%r12)\n"
5175  "l %r4,1f-0b(%r3)\n"
5176  "b 0(%r4,%r3)\n"
5177  "1: .long __tls_get_addr_hidden - 0b\n"
5178#endif
5179  ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5180// Assembly wrapper to call REAL(__tls_get_offset)(arg)
5181  ".type __tls_get_offset_wrapper, @function\n"
5182  "__tls_get_offset_wrapper:\n"
5183#ifdef __s390x__
5184  "sgr %r2,%r12\n"
5185#else
5186  "sr %r2,%r12\n"
5187#endif
5188  "br %r3\n"
5189  ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5190);
5191#endif // SANITIZER_S390
5192#else
5193#define INIT_TLS_GET_ADDR
5194#endif
5195
5196#if SANITIZER_INTERCEPT_LISTXATTR
5197INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5198  void *ctx;
5199  COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5200  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5201  // FIXME: under ASan the call below may write to freed memory and corrupt
5202  // its metadata. See
5203  // https://github.com/google/sanitizers/issues/321.
5204  SSIZE_T res = REAL(listxattr)(path, list, size);
5205  // Here and below, size == 0 is a special case where nothing is written to the
5206  // buffer, and res contains the desired buffer size.
5207  if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5208  return res;
5209}
5210INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5211  void *ctx;
5212  COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5213  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5214  // FIXME: under ASan the call below may write to freed memory and corrupt
5215  // its metadata. See
5216  // https://github.com/google/sanitizers/issues/321.
5217  SSIZE_T res = REAL(llistxattr)(path, list, size);
5218  if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5219  return res;
5220}
5221INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5222  void *ctx;
5223  COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5224  // FIXME: under ASan the call below may write to freed memory and corrupt
5225  // its metadata. See
5226  // https://github.com/google/sanitizers/issues/321.
5227  SSIZE_T res = REAL(flistxattr)(fd, list, size);
5228  if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5229  return res;
5230}
5231#define INIT_LISTXATTR                   \
5232  COMMON_INTERCEPT_FUNCTION(listxattr);  \
5233  COMMON_INTERCEPT_FUNCTION(llistxattr); \
5234  COMMON_INTERCEPT_FUNCTION(flistxattr);
5235#else
5236#define INIT_LISTXATTR
5237#endif
5238
5239#if SANITIZER_INTERCEPT_GETXATTR
5240INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5241            SIZE_T size) {
5242  void *ctx;
5243  COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5244  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5245  if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5246  // FIXME: under ASan the call below may write to freed memory and corrupt
5247  // its metadata. See
5248  // https://github.com/google/sanitizers/issues/321.
5249  SSIZE_T res = REAL(getxattr)(path, name, value, size);
5250  if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5251  return res;
5252}
5253INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5254            SIZE_T size) {
5255  void *ctx;
5256  COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5257  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5258  if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5259  // FIXME: under ASan the call below may write to freed memory and corrupt
5260  // its metadata. See
5261  // https://github.com/google/sanitizers/issues/321.
5262  SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5263  if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5264  return res;
5265}
5266INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5267            SIZE_T size) {
5268  void *ctx;
5269  COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5270  if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5271  // FIXME: under ASan the call below may write to freed memory and corrupt
5272  // its metadata. See
5273  // https://github.com/google/sanitizers/issues/321.
5274  SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5275  if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5276  return res;
5277}
5278#define INIT_GETXATTR                   \
5279  COMMON_INTERCEPT_FUNCTION(getxattr);  \
5280  COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5281  COMMON_INTERCEPT_FUNCTION(fgetxattr);
5282#else
5283#define INIT_GETXATTR
5284#endif
5285
5286#if SANITIZER_INTERCEPT_GETRESID
5287INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5288  void *ctx;
5289  COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5290  // FIXME: under ASan the call below may write to freed memory and corrupt
5291  // its metadata. See
5292  // https://github.com/google/sanitizers/issues/321.
5293  int res = REAL(getresuid)(ruid, euid, suid);
5294  if (res >= 0) {
5295    if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5296    if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5297    if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5298  }
5299  return res;
5300}
5301INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5302  void *ctx;
5303  COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5304  // FIXME: under ASan the call below may write to freed memory and corrupt
5305  // its metadata. See
5306  // https://github.com/google/sanitizers/issues/321.
5307  int res = REAL(getresgid)(rgid, egid, sgid);
5308  if (res >= 0) {
5309    if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5310    if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5311    if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5312  }
5313  return res;
5314}
5315#define INIT_GETRESID                   \
5316  COMMON_INTERCEPT_FUNCTION(getresuid); \
5317  COMMON_INTERCEPT_FUNCTION(getresgid);
5318#else
5319#define INIT_GETRESID
5320#endif
5321
5322#if SANITIZER_INTERCEPT_GETIFADDRS
5323// As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5324// intercept freeifaddrs(). If that ceases to be the case, we might need to
5325// intercept it to poison the memory again.
5326INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5327  void *ctx;
5328  COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5329  // FIXME: under ASan the call below may write to freed memory and corrupt
5330  // its metadata. See
5331  // https://github.com/google/sanitizers/issues/321.
5332  int res = REAL(getifaddrs)(ifap);
5333  if (res == 0 && ifap) {
5334    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5335    __sanitizer_ifaddrs *p = *ifap;
5336    while (p) {
5337      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5338      if (p->ifa_name)
5339        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5340                                       REAL(strlen)(p->ifa_name) + 1);
5341      if (p->ifa_addr)
5342        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5343      if (p->ifa_netmask)
5344        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5345      // On Linux this is a union, but the other member also points to a
5346      // struct sockaddr, so the following is sufficient.
5347      if (p->ifa_dstaddr)
5348        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5349      // FIXME(smatveev): Unpoison p->ifa_data as well.
5350      p = p->ifa_next;
5351    }
5352  }
5353  return res;
5354}
5355#define INIT_GETIFADDRS                  \
5356  COMMON_INTERCEPT_FUNCTION(getifaddrs);
5357#else
5358#define INIT_GETIFADDRS
5359#endif
5360
5361#if SANITIZER_INTERCEPT_IF_INDEXTONAME
5362INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5363  void *ctx;
5364  COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5365  // FIXME: under ASan the call below may write to freed memory and corrupt
5366  // its metadata. See
5367  // https://github.com/google/sanitizers/issues/321.
5368  char *res = REAL(if_indextoname)(ifindex, ifname);
5369  if (res && ifname)
5370    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5371  return res;
5372}
5373INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5374  void *ctx;
5375  COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5376  if (ifname)
5377    COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5378  return REAL(if_nametoindex)(ifname);
5379}
5380#define INIT_IF_INDEXTONAME                  \
5381  COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5382  COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5383#else
5384#define INIT_IF_INDEXTONAME
5385#endif
5386
5387#if SANITIZER_INTERCEPT_CAPGET
5388INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5389  void *ctx;
5390  COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5391  if (hdrp)
5392    COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5393  // FIXME: under ASan the call below may write to freed memory and corrupt
5394  // its metadata. See
5395  // https://github.com/google/sanitizers/issues/321.
5396  int res = REAL(capget)(hdrp, datap);
5397  if (res == 0 && datap)
5398    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5399  // We can also return -1 and write to hdrp->version if the version passed in
5400  // hdrp->version is unsupported. But that's not a trivial condition to check,
5401  // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5402  return res;
5403}
5404INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5405  void *ctx;
5406  COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5407  if (hdrp)
5408    COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5409  if (datap)
5410    COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5411  return REAL(capset)(hdrp, datap);
5412}
5413#define INIT_CAPGET                  \
5414  COMMON_INTERCEPT_FUNCTION(capget); \
5415  COMMON_INTERCEPT_FUNCTION(capset);
5416#else
5417#define INIT_CAPGET
5418#endif
5419
5420#if SANITIZER_INTERCEPT_AEABI_MEM
5421INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5422  void *ctx;
5423  COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5424}
5425
5426INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5427  void *ctx;
5428  COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5429}
5430
5431INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5432  void *ctx;
5433  COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5434}
5435
5436INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5437  void *ctx;
5438  COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5439}
5440
5441INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5442  void *ctx;
5443  COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5444}
5445
5446INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5447  void *ctx;
5448  COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5449}
5450
5451// Note the argument order.
5452INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5453  void *ctx;
5454  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5455}
5456
5457INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5458  void *ctx;
5459  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5460}
5461
5462INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5463  void *ctx;
5464  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5465}
5466
5467INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5468  void *ctx;
5469  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5470}
5471
5472INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5473  void *ctx;
5474  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5475}
5476
5477INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5478  void *ctx;
5479  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5480}
5481
5482#define INIT_AEABI_MEM                         \
5483  COMMON_INTERCEPT_FUNCTION(__aeabi_memmove);  \
5484  COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5485  COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5486  COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy);   \
5487  COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4);  \
5488  COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8);  \
5489  COMMON_INTERCEPT_FUNCTION(__aeabi_memset);   \
5490  COMMON_INTERCEPT_FUNCTION(__aeabi_memset4);  \
5491  COMMON_INTERCEPT_FUNCTION(__aeabi_memset8);  \
5492  COMMON_INTERCEPT_FUNCTION(__aeabi_memclr);   \
5493  COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4);  \
5494  COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5495#else
5496#define INIT_AEABI_MEM
5497#endif  // SANITIZER_INTERCEPT_AEABI_MEM
5498
5499#if SANITIZER_INTERCEPT___BZERO
5500INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5501  void *ctx;
5502  COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5503}
5504
5505#define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5506#else
5507#define INIT___BZERO
5508#endif  // SANITIZER_INTERCEPT___BZERO
5509
5510#if SANITIZER_INTERCEPT_FTIME
5511INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5512  void *ctx;
5513  COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5514  // FIXME: under ASan the call below may write to freed memory and corrupt
5515  // its metadata. See
5516  // https://github.com/google/sanitizers/issues/321.
5517  int res = REAL(ftime)(tp);
5518  if (tp)
5519    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5520  return res;
5521}
5522#define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5523#else
5524#define INIT_FTIME
5525#endif  // SANITIZER_INTERCEPT_FTIME
5526
5527#if SANITIZER_INTERCEPT_XDR
5528INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5529            unsigned size, int op) {
5530  void *ctx;
5531  COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5532  // FIXME: under ASan the call below may write to freed memory and corrupt
5533  // its metadata. See
5534  // https://github.com/google/sanitizers/issues/321.
5535  REAL(xdrmem_create)(xdrs, addr, size, op);
5536  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5537  if (op == __sanitizer_XDR_ENCODE) {
5538    // It's not obvious how much data individual xdr_ routines write.
5539    // Simply unpoison the entire target buffer in advance.
5540    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5541  }
5542}
5543
5544INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5545  void *ctx;
5546  COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5547  // FIXME: under ASan the call below may write to freed memory and corrupt
5548  // its metadata. See
5549  // https://github.com/google/sanitizers/issues/321.
5550  REAL(xdrstdio_create)(xdrs, file, op);
5551  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5552}
5553
5554// FIXME: under ASan the call below may write to freed memory and corrupt
5555// its metadata. See
5556// https://github.com/google/sanitizers/issues/321.
5557#define XDR_INTERCEPTOR(F, T)                             \
5558  INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) {      \
5559    void *ctx;                                            \
5560    COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p);            \
5561    if (p && xdrs->x_op == __sanitizer_XDR_ENCODE)        \
5562      COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));  \
5563    int res = REAL(F)(xdrs, p);                           \
5564    if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5565      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5566    return res;                                           \
5567  }
5568
5569XDR_INTERCEPTOR(xdr_short, short)
5570XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5571XDR_INTERCEPTOR(xdr_int, int)
5572XDR_INTERCEPTOR(xdr_u_int, unsigned)
5573XDR_INTERCEPTOR(xdr_long, long)
5574XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5575XDR_INTERCEPTOR(xdr_hyper, long long)
5576XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5577XDR_INTERCEPTOR(xdr_longlong_t, long long)
5578XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5579XDR_INTERCEPTOR(xdr_int8_t, u8)
5580XDR_INTERCEPTOR(xdr_uint8_t, u8)
5581XDR_INTERCEPTOR(xdr_int16_t, u16)
5582XDR_INTERCEPTOR(xdr_uint16_t, u16)
5583XDR_INTERCEPTOR(xdr_int32_t, u32)
5584XDR_INTERCEPTOR(xdr_uint32_t, u32)
5585XDR_INTERCEPTOR(xdr_int64_t, u64)
5586XDR_INTERCEPTOR(xdr_uint64_t, u64)
5587XDR_INTERCEPTOR(xdr_quad_t, long long)
5588XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5589XDR_INTERCEPTOR(xdr_bool, bool)
5590XDR_INTERCEPTOR(xdr_enum, int)
5591XDR_INTERCEPTOR(xdr_char, char)
5592XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5593XDR_INTERCEPTOR(xdr_float, float)
5594XDR_INTERCEPTOR(xdr_double, double)
5595
5596// FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5597// wrapstring, sizeof
5598
5599INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5600            unsigned maxsize) {
5601  void *ctx;
5602  COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5603  if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5604    COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5605    COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5606    COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5607  }
5608  // FIXME: under ASan the call below may write to freed memory and corrupt
5609  // its metadata. See
5610  // https://github.com/google/sanitizers/issues/321.
5611  int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5612  if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5613    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5614    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5615    if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5616  }
5617  return res;
5618}
5619
5620INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5621            unsigned maxsize) {
5622  void *ctx;
5623  COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5624  if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5625    COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5626    COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5627  }
5628  // FIXME: under ASan the call below may write to freed memory and corrupt
5629  // its metadata. See
5630  // https://github.com/google/sanitizers/issues/321.
5631  int res = REAL(xdr_string)(xdrs, p, maxsize);
5632  if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5633    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5634    if (res && *p)
5635      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5636  }
5637  return res;
5638}
5639
5640#define INIT_XDR                               \
5641  COMMON_INTERCEPT_FUNCTION(xdrmem_create);    \
5642  COMMON_INTERCEPT_FUNCTION(xdrstdio_create);  \
5643  COMMON_INTERCEPT_FUNCTION(xdr_short);        \
5644  COMMON_INTERCEPT_FUNCTION(xdr_u_short);      \
5645  COMMON_INTERCEPT_FUNCTION(xdr_int);          \
5646  COMMON_INTERCEPT_FUNCTION(xdr_u_int);        \
5647  COMMON_INTERCEPT_FUNCTION(xdr_long);         \
5648  COMMON_INTERCEPT_FUNCTION(xdr_u_long);       \
5649  COMMON_INTERCEPT_FUNCTION(xdr_hyper);        \
5650  COMMON_INTERCEPT_FUNCTION(xdr_u_hyper);      \
5651  COMMON_INTERCEPT_FUNCTION(xdr_longlong_t);   \
5652  COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5653  COMMON_INTERCEPT_FUNCTION(xdr_int8_t);       \
5654  COMMON_INTERCEPT_FUNCTION(xdr_uint8_t);      \
5655  COMMON_INTERCEPT_FUNCTION(xdr_int16_t);      \
5656  COMMON_INTERCEPT_FUNCTION(xdr_uint16_t);     \
5657  COMMON_INTERCEPT_FUNCTION(xdr_int32_t);      \
5658  COMMON_INTERCEPT_FUNCTION(xdr_uint32_t);     \
5659  COMMON_INTERCEPT_FUNCTION(xdr_int64_t);      \
5660  COMMON_INTERCEPT_FUNCTION(xdr_uint64_t);     \
5661  COMMON_INTERCEPT_FUNCTION(xdr_quad_t);       \
5662  COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t);     \
5663  COMMON_INTERCEPT_FUNCTION(xdr_bool);         \
5664  COMMON_INTERCEPT_FUNCTION(xdr_enum);         \
5665  COMMON_INTERCEPT_FUNCTION(xdr_char);         \
5666  COMMON_INTERCEPT_FUNCTION(xdr_u_char);       \
5667  COMMON_INTERCEPT_FUNCTION(xdr_float);        \
5668  COMMON_INTERCEPT_FUNCTION(xdr_double);       \
5669  COMMON_INTERCEPT_FUNCTION(xdr_bytes);        \
5670  COMMON_INTERCEPT_FUNCTION(xdr_string);
5671#else
5672#define INIT_XDR
5673#endif  // SANITIZER_INTERCEPT_XDR
5674
5675#if SANITIZER_INTERCEPT_TSEARCH
5676INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5677            int (*compar)(const void *, const void *)) {
5678  void *ctx;
5679  COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5680  // FIXME: under ASan the call below may write to freed memory and corrupt
5681  // its metadata. See
5682  // https://github.com/google/sanitizers/issues/321.
5683  void *res = REAL(tsearch)(key, rootp, compar);
5684  if (res && *(void **)res == key)
5685    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5686  return res;
5687}
5688#define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5689#else
5690#define INIT_TSEARCH
5691#endif
5692
5693#if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5694    SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5695void unpoison_file(__sanitizer_FILE *fp) {
5696#if SANITIZER_HAS_STRUCT_FILE
5697  COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5698#if SANITIZER_NETBSD
5699  if (fp->_bf._base && fp->_bf._size > 0)
5700    COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
5701                                        fp->_bf._size);
5702#else
5703  if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5704    COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5705                                        fp->_IO_read_end - fp->_IO_read_base);
5706#endif
5707#endif  // SANITIZER_HAS_STRUCT_FILE
5708}
5709#endif
5710
5711#if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5712// These guys are called when a .c source is built with -O2.
5713INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5714  void *ctx;
5715  COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5716  int res = REAL(__uflow)(fp);
5717  unpoison_file(fp);
5718  return res;
5719}
5720INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5721  void *ctx;
5722  COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5723  int res = REAL(__underflow)(fp);
5724  unpoison_file(fp);
5725  return res;
5726}
5727INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5728  void *ctx;
5729  COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5730  int res = REAL(__overflow)(fp, ch);
5731  unpoison_file(fp);
5732  return res;
5733}
5734INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5735  void *ctx;
5736  COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5737  int res = REAL(__wuflow)(fp);
5738  unpoison_file(fp);
5739  return res;
5740}
5741INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5742  void *ctx;
5743  COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5744  int res = REAL(__wunderflow)(fp);
5745  unpoison_file(fp);
5746  return res;
5747}
5748INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5749  void *ctx;
5750  COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5751  int res = REAL(__woverflow)(fp, ch);
5752  unpoison_file(fp);
5753  return res;
5754}
5755#define INIT_LIBIO_INTERNALS               \
5756  COMMON_INTERCEPT_FUNCTION(__uflow);      \
5757  COMMON_INTERCEPT_FUNCTION(__underflow);  \
5758  COMMON_INTERCEPT_FUNCTION(__overflow);   \
5759  COMMON_INTERCEPT_FUNCTION(__wuflow);     \
5760  COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5761  COMMON_INTERCEPT_FUNCTION(__woverflow);
5762#else
5763#define INIT_LIBIO_INTERNALS
5764#endif
5765
5766#if SANITIZER_INTERCEPT_FOPEN
5767INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5768  void *ctx;
5769  COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5770  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5771  COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5772  __sanitizer_FILE *res = REAL(fopen)(path, mode);
5773  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5774  if (res) unpoison_file(res);
5775  return res;
5776}
5777INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5778  void *ctx;
5779  COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5780  COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5781  __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5782  if (res) unpoison_file(res);
5783  return res;
5784}
5785INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5786            __sanitizer_FILE *fp) {
5787  void *ctx;
5788  COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5789  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5790  COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5791  COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5792  __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5793  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5794  if (res) unpoison_file(res);
5795  return res;
5796}
5797#define INIT_FOPEN                   \
5798  COMMON_INTERCEPT_FUNCTION(fopen);  \
5799  COMMON_INTERCEPT_FUNCTION(fdopen); \
5800  COMMON_INTERCEPT_FUNCTION(freopen);
5801#else
5802#define INIT_FOPEN
5803#endif
5804
5805#if SANITIZER_INTERCEPT_FOPEN64
5806INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5807  void *ctx;
5808  COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5809  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5810  COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5811  __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5812  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5813  if (res) unpoison_file(res);
5814  return res;
5815}
5816INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5817            __sanitizer_FILE *fp) {
5818  void *ctx;
5819  COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5820  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5821  COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5822  COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5823  __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5824  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5825  if (res) unpoison_file(res);
5826  return res;
5827}
5828#define INIT_FOPEN64                  \
5829  COMMON_INTERCEPT_FUNCTION(fopen64); \
5830  COMMON_INTERCEPT_FUNCTION(freopen64);
5831#else
5832#define INIT_FOPEN64
5833#endif
5834
5835#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5836INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5837  void *ctx;
5838  COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5839  // FIXME: under ASan the call below may write to freed memory and corrupt
5840  // its metadata. See
5841  // https://github.com/google/sanitizers/issues/321.
5842  __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5843  if (res) {
5844    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5845    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5846    unpoison_file(res);
5847    FileMetadata file = {ptr, sizeloc};
5848    SetInterceptorMetadata(res, file);
5849  }
5850  return res;
5851}
5852INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5853            SIZE_T *sizeloc) {
5854  void *ctx;
5855  COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5856  __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5857  if (res) {
5858    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5859    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5860    unpoison_file(res);
5861    FileMetadata file = {(char **)ptr, sizeloc};
5862    SetInterceptorMetadata(res, file);
5863  }
5864  return res;
5865}
5866INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5867            const char *mode) {
5868  void *ctx;
5869  COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5870  // FIXME: under ASan the call below may write to freed memory and corrupt
5871  // its metadata. See
5872  // https://github.com/google/sanitizers/issues/321.
5873  __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5874  if (res) unpoison_file(res);
5875  return res;
5876}
5877#define INIT_OPEN_MEMSTREAM                   \
5878  COMMON_INTERCEPT_FUNCTION(open_memstream);  \
5879  COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5880  COMMON_INTERCEPT_FUNCTION(fmemopen);
5881#else
5882#define INIT_OPEN_MEMSTREAM
5883#endif
5884
5885#if SANITIZER_INTERCEPT_OBSTACK
5886static void initialize_obstack(__sanitizer_obstack *obstack) {
5887  COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5888  if (obstack->chunk)
5889    COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5890                                        sizeof(*obstack->chunk));
5891}
5892
5893INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5894            int align, void *(*alloc_fn)(uptr arg, uptr sz),
5895            void (*free_fn)(uptr arg, void *p)) {
5896  void *ctx;
5897  COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5898                           free_fn);
5899  int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5900  if (res) initialize_obstack(obstack);
5901  return res;
5902}
5903INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5904            int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5905  void *ctx;
5906  COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5907                           free_fn);
5908  int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5909  if (res) initialize_obstack(obstack);
5910  return res;
5911}
5912INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5913  void *ctx;
5914  COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5915  REAL(_obstack_newchunk)(obstack, length);
5916  if (obstack->chunk)
5917    COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5918        obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5919}
5920#define INIT_OBSTACK                           \
5921  COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5922  COMMON_INTERCEPT_FUNCTION(_obstack_begin);   \
5923  COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5924#else
5925#define INIT_OBSTACK
5926#endif
5927
5928#if SANITIZER_INTERCEPT_FFLUSH
5929INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5930  void *ctx;
5931  COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5932  int res = REAL(fflush)(fp);
5933  // FIXME: handle fp == NULL
5934  if (fp) {
5935    const FileMetadata *m = GetInterceptorMetadata(fp);
5936    if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5937  }
5938  return res;
5939}
5940#define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5941#else
5942#define INIT_FFLUSH
5943#endif
5944
5945#if SANITIZER_INTERCEPT_FCLOSE
5946INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5947  void *ctx;
5948  COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5949  COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5950  const FileMetadata *m = GetInterceptorMetadata(fp);
5951  int res = REAL(fclose)(fp);
5952  if (m) {
5953    COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5954    DeleteInterceptorMetadata(fp);
5955  }
5956  return res;
5957}
5958#define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5959#else
5960#define INIT_FCLOSE
5961#endif
5962
5963#if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5964INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5965  void *ctx;
5966  COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5967  if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5968  COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5969  void *res = REAL(dlopen)(filename, flag);
5970  Symbolizer::GetOrInit()->InvalidateModuleList();
5971  COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5972  return res;
5973}
5974
5975INTERCEPTOR(int, dlclose, void *handle) {
5976  void *ctx;
5977  COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5978  int res = REAL(dlclose)(handle);
5979  Symbolizer::GetOrInit()->InvalidateModuleList();
5980  COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5981  return res;
5982}
5983#define INIT_DLOPEN_DLCLOSE          \
5984  COMMON_INTERCEPT_FUNCTION(dlopen); \
5985  COMMON_INTERCEPT_FUNCTION(dlclose);
5986#else
5987#define INIT_DLOPEN_DLCLOSE
5988#endif
5989
5990#if SANITIZER_INTERCEPT_GETPASS
5991INTERCEPTOR(char *, getpass, const char *prompt) {
5992  void *ctx;
5993  COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5994  if (prompt)
5995    COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5996  char *res = REAL(getpass)(prompt);
5997  if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5998  return res;
5999}
6000
6001#define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6002#else
6003#define INIT_GETPASS
6004#endif
6005
6006#if SANITIZER_INTERCEPT_TIMERFD
6007INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6008            void *old_value) {
6009  void *ctx;
6010  COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6011                           old_value);
6012  COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6013  int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6014  if (res != -1 && old_value)
6015    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6016  return res;
6017}
6018
6019INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6020  void *ctx;
6021  COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6022  int res = REAL(timerfd_gettime)(fd, curr_value);
6023  if (res != -1 && curr_value)
6024    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6025  return res;
6026}
6027#define INIT_TIMERFD                          \
6028  COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6029  COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6030#else
6031#define INIT_TIMERFD
6032#endif
6033
6034#if SANITIZER_INTERCEPT_MLOCKX
6035// Linux kernel has a bug that leads to kernel deadlock if a process
6036// maps TBs of memory and then calls mlock().
6037static void MlockIsUnsupported() {
6038  static atomic_uint8_t printed;
6039  if (atomic_exchange(&printed, 1, memory_order_relaxed))
6040    return;
6041  VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6042          SanitizerToolName);
6043}
6044
6045INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6046  MlockIsUnsupported();
6047  return 0;
6048}
6049
6050INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6051  MlockIsUnsupported();
6052  return 0;
6053}
6054
6055INTERCEPTOR(int, mlockall, int flags) {
6056  MlockIsUnsupported();
6057  return 0;
6058}
6059
6060INTERCEPTOR(int, munlockall, void) {
6061  MlockIsUnsupported();
6062  return 0;
6063}
6064
6065#define INIT_MLOCKX                                                            \
6066  COMMON_INTERCEPT_FUNCTION(mlock);                                            \
6067  COMMON_INTERCEPT_FUNCTION(munlock);                                          \
6068  COMMON_INTERCEPT_FUNCTION(mlockall);                                         \
6069  COMMON_INTERCEPT_FUNCTION(munlockall);
6070
6071#else
6072#define INIT_MLOCKX
6073#endif  // SANITIZER_INTERCEPT_MLOCKX
6074
6075#if SANITIZER_INTERCEPT_FOPENCOOKIE
6076struct WrappedCookie {
6077  void *real_cookie;
6078  __sanitizer_cookie_io_functions_t real_io_funcs;
6079};
6080
6081static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6082  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6083  WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6084  __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6085  return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6086}
6087
6088static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6089  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6090  WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6091  __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6092  return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6093}
6094
6095static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6096  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6097  COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6098  WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6099  __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6100  return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6101                   : -1;
6102}
6103
6104static int wrapped_close(void *cookie) {
6105  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6106  WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6107  __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6108  int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6109  InternalFree(wrapped_cookie);
6110  return res;
6111}
6112
6113INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6114            __sanitizer_cookie_io_functions_t io_funcs) {
6115  void *ctx;
6116  COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6117  WrappedCookie *wrapped_cookie =
6118      (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6119  wrapped_cookie->real_cookie = cookie;
6120  wrapped_cookie->real_io_funcs = io_funcs;
6121  __sanitizer_FILE *res =
6122      REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6123                                               wrapped_seek, wrapped_close});
6124  return res;
6125}
6126
6127#define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6128#else
6129#define INIT_FOPENCOOKIE
6130#endif  // SANITIZER_INTERCEPT_FOPENCOOKIE
6131
6132#if SANITIZER_INTERCEPT_SEM
6133INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6134  void *ctx;
6135  COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6136  // Workaround a bug in glibc's "old" semaphore implementation by
6137  // zero-initializing the sem_t contents. This has to be done here because
6138  // interceptors bind to the lowest symbols version by default, hitting the
6139  // buggy code path while the non-sanitized build of the same code works fine.
6140  REAL(memset)(s, 0, sizeof(*s));
6141  int res = REAL(sem_init)(s, pshared, value);
6142  return res;
6143}
6144
6145INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6146  void *ctx;
6147  COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6148  int res = REAL(sem_destroy)(s);
6149  return res;
6150}
6151
6152INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6153  void *ctx;
6154  COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6155  int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6156  if (res == 0) {
6157    COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6158  }
6159  return res;
6160}
6161
6162INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6163  void *ctx;
6164  COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6165  int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6166  if (res == 0) {
6167    COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6168  }
6169  return res;
6170}
6171
6172INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6173  void *ctx;
6174  COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6175  COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6176  int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6177  if (res == 0) {
6178    COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6179  }
6180  return res;
6181}
6182
6183INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6184  void *ctx;
6185  COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6186  COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6187  int res = REAL(sem_post)(s);
6188  return res;
6189}
6190
6191INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6192  void *ctx;
6193  COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6194  int res = REAL(sem_getvalue)(s, sval);
6195  if (res == 0) {
6196    COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6197    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6198  }
6199  return res;
6200}
6201#define INIT_SEM                                                               \
6202  COMMON_INTERCEPT_FUNCTION(sem_init);                                         \
6203  COMMON_INTERCEPT_FUNCTION(sem_destroy);                                      \
6204  COMMON_INTERCEPT_FUNCTION(sem_wait);                                         \
6205  COMMON_INTERCEPT_FUNCTION(sem_trywait);                                      \
6206  COMMON_INTERCEPT_FUNCTION(sem_timedwait);                                    \
6207  COMMON_INTERCEPT_FUNCTION(sem_post);                                         \
6208  COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6209#else
6210#define INIT_SEM
6211#endif // SANITIZER_INTERCEPT_SEM
6212
6213#if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6214INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6215  void *ctx;
6216  COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6217  int res = REAL(pthread_setcancelstate)(state, oldstate);
6218  if (res == 0 && oldstate != nullptr)
6219    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6220  return res;
6221}
6222
6223INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6224  void *ctx;
6225  COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6226  int res = REAL(pthread_setcanceltype)(type, oldtype);
6227  if (res == 0 && oldtype != nullptr)
6228    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6229  return res;
6230}
6231#define INIT_PTHREAD_SETCANCEL                                                 \
6232  COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate);                           \
6233  COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6234#else
6235#define INIT_PTHREAD_SETCANCEL
6236#endif
6237
6238#if SANITIZER_INTERCEPT_MINCORE
6239INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6240  void *ctx;
6241  COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6242  int res = REAL(mincore)(addr, length, vec);
6243  if (res == 0) {
6244    uptr page_size = GetPageSizeCached();
6245    uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6246    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6247  }
6248  return res;
6249}
6250#define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6251#else
6252#define INIT_MINCORE
6253#endif
6254
6255#if SANITIZER_INTERCEPT_PROCESS_VM_READV
6256INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6257            uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6258            uptr flags) {
6259  void *ctx;
6260  COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6261                           remote_iov, riovcnt, flags);
6262  SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6263                                       riovcnt, flags);
6264  if (res > 0)
6265    write_iovec(ctx, local_iov, liovcnt, res);
6266  return res;
6267}
6268
6269INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6270            uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6271            uptr flags) {
6272  void *ctx;
6273  COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6274                           remote_iov, riovcnt, flags);
6275  SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6276                                        riovcnt, flags);
6277  if (res > 0)
6278    read_iovec(ctx, local_iov, liovcnt, res);
6279  return res;
6280}
6281#define INIT_PROCESS_VM_READV                                                  \
6282  COMMON_INTERCEPT_FUNCTION(process_vm_readv);                                 \
6283  COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6284#else
6285#define INIT_PROCESS_VM_READV
6286#endif
6287
6288#if SANITIZER_INTERCEPT_CTERMID
6289INTERCEPTOR(char *, ctermid, char *s) {
6290  void *ctx;
6291  COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6292  char *res = REAL(ctermid)(s);
6293  if (res) {
6294    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6295  }
6296  return res;
6297}
6298#define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6299#else
6300#define INIT_CTERMID
6301#endif
6302
6303#if SANITIZER_INTERCEPT_CTERMID_R
6304INTERCEPTOR(char *, ctermid_r, char *s) {
6305  void *ctx;
6306  COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6307  char *res = REAL(ctermid_r)(s);
6308  if (res) {
6309    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6310  }
6311  return res;
6312}
6313#define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6314#else
6315#define INIT_CTERMID_R
6316#endif
6317
6318#if SANITIZER_INTERCEPT_RECV_RECVFROM
6319INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6320  void *ctx;
6321  COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6322  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6323  SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6324  if (res > 0) {
6325    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6326  }
6327  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6328  return res;
6329}
6330
6331INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6332            void *srcaddr, int *addrlen) {
6333  void *ctx;
6334  COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6335                           addrlen);
6336  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6337  SIZE_T srcaddr_sz;
6338  if (srcaddr) srcaddr_sz = *addrlen;
6339  (void)srcaddr_sz;  // prevent "set but not used" warning
6340  SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6341  if (res > 0) {
6342    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6343    if (srcaddr)
6344      COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6345                                          Min((SIZE_T)*addrlen, srcaddr_sz));
6346  }
6347  return res;
6348}
6349#define INIT_RECV_RECVFROM          \
6350  COMMON_INTERCEPT_FUNCTION(recv);  \
6351  COMMON_INTERCEPT_FUNCTION(recvfrom);
6352#else
6353#define INIT_RECV_RECVFROM
6354#endif
6355
6356#if SANITIZER_INTERCEPT_SEND_SENDTO
6357INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6358  void *ctx;
6359  COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6360  if (fd >= 0) {
6361    COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6362    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6363  }
6364  SSIZE_T res = REAL(send)(fd, buf, len, flags);
6365  if (common_flags()->intercept_send && res > 0)
6366    COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6367  return res;
6368}
6369
6370INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6371            void *dstaddr, int addrlen) {
6372  void *ctx;
6373  COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6374  if (fd >= 0) {
6375    COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6376    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6377  }
6378  // Can't check dstaddr as it may have uninitialized padding at the end.
6379  SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6380  if (common_flags()->intercept_send && res > 0)
6381    COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6382  return res;
6383}
6384#define INIT_SEND_SENDTO           \
6385  COMMON_INTERCEPT_FUNCTION(send); \
6386  COMMON_INTERCEPT_FUNCTION(sendto);
6387#else
6388#define INIT_SEND_SENDTO
6389#endif
6390
6391#if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6392INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6393  void *ctx;
6394  COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6395  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6396  int res = REAL(eventfd_read)(fd, value);
6397  if (res == 0) {
6398    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6399    if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6400  }
6401  return res;
6402}
6403INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6404  void *ctx;
6405  COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6406  if (fd >= 0) {
6407    COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6408    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6409  }
6410  int res = REAL(eventfd_write)(fd, value);
6411  return res;
6412}
6413#define INIT_EVENTFD_READ_WRITE            \
6414  COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6415  COMMON_INTERCEPT_FUNCTION(eventfd_write)
6416#else
6417#define INIT_EVENTFD_READ_WRITE
6418#endif
6419
6420#if SANITIZER_INTERCEPT_STAT
6421INTERCEPTOR(int, stat, const char *path, void *buf) {
6422  void *ctx;
6423  COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6424  if (common_flags()->intercept_stat)
6425    COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6426  int res = REAL(stat)(path, buf);
6427  if (!res)
6428    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6429  return res;
6430}
6431#define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6432#else
6433#define INIT_STAT
6434#endif
6435
6436#if SANITIZER_INTERCEPT_LSTAT
6437INTERCEPTOR(int, lstat, const char *path, void *buf) {
6438  void *ctx;
6439  COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6440  if (common_flags()->intercept_stat)
6441    COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6442  int res = REAL(lstat)(path, buf);
6443  if (!res)
6444    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6445  return res;
6446}
6447#define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6448#else
6449#define INIT_LSTAT
6450#endif
6451
6452#if SANITIZER_INTERCEPT___XSTAT
6453INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6454  void *ctx;
6455  COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6456  if (common_flags()->intercept_stat)
6457    COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6458  int res = REAL(__xstat)(version, path, buf);
6459  if (!res)
6460    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6461  return res;
6462}
6463#define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6464#else
6465#define INIT___XSTAT
6466#endif
6467
6468#if SANITIZER_INTERCEPT___XSTAT64
6469INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6470  void *ctx;
6471  COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6472  if (common_flags()->intercept_stat)
6473    COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6474  int res = REAL(__xstat64)(version, path, buf);
6475  if (!res)
6476    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6477  return res;
6478}
6479#define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6480#else
6481#define INIT___XSTAT64
6482#endif
6483
6484#if SANITIZER_INTERCEPT___LXSTAT
6485INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6486  void *ctx;
6487  COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6488  if (common_flags()->intercept_stat)
6489    COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6490  int res = REAL(__lxstat)(version, path, buf);
6491  if (!res)
6492    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6493  return res;
6494}
6495#define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6496#else
6497#define INIT___LXSTAT
6498#endif
6499
6500#if SANITIZER_INTERCEPT___LXSTAT64
6501INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6502  void *ctx;
6503  COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6504  if (common_flags()->intercept_stat)
6505    COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6506  int res = REAL(__lxstat64)(version, path, buf);
6507  if (!res)
6508    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6509  return res;
6510}
6511#define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6512#else
6513#define INIT___LXSTAT64
6514#endif
6515
6516// FIXME: add other *stat interceptor
6517
6518#if SANITIZER_INTERCEPT_UTMP
6519INTERCEPTOR(void *, getutent, int dummy) {
6520  void *ctx;
6521  COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6522  void *res = REAL(getutent)(dummy);
6523  if (res)
6524    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6525  return res;
6526}
6527INTERCEPTOR(void *, getutid, void *ut) {
6528  void *ctx;
6529  COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6530  void *res = REAL(getutid)(ut);
6531  if (res)
6532    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6533  return res;
6534}
6535INTERCEPTOR(void *, getutline, void *ut) {
6536  void *ctx;
6537  COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6538  void *res = REAL(getutline)(ut);
6539  if (res)
6540    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6541  return res;
6542}
6543#define INIT_UTMP                      \
6544  COMMON_INTERCEPT_FUNCTION(getutent); \
6545  COMMON_INTERCEPT_FUNCTION(getutid);  \
6546  COMMON_INTERCEPT_FUNCTION(getutline);
6547#else
6548#define INIT_UTMP
6549#endif
6550
6551#if SANITIZER_INTERCEPT_UTMPX
6552INTERCEPTOR(void *, getutxent, int dummy) {
6553  void *ctx;
6554  COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6555  void *res = REAL(getutxent)(dummy);
6556  if (res)
6557    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6558  return res;
6559}
6560INTERCEPTOR(void *, getutxid, void *ut) {
6561  void *ctx;
6562  COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6563  void *res = REAL(getutxid)(ut);
6564  if (res)
6565    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6566  return res;
6567}
6568INTERCEPTOR(void *, getutxline, void *ut) {
6569  void *ctx;
6570  COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6571  void *res = REAL(getutxline)(ut);
6572  if (res)
6573    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6574  return res;
6575}
6576INTERCEPTOR(void *, pututxline, const void *ut) {
6577  void *ctx;
6578  COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
6579  if (ut)
6580    COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
6581  void *res = REAL(pututxline)(ut);
6582  if (res)
6583    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
6584  return res;
6585}
6586#define INIT_UTMPX                      \
6587  COMMON_INTERCEPT_FUNCTION(getutxent); \
6588  COMMON_INTERCEPT_FUNCTION(getutxid);  \
6589  COMMON_INTERCEPT_FUNCTION(getutxline); \
6590  COMMON_INTERCEPT_FUNCTION(pututxline);
6591#else
6592#define INIT_UTMPX
6593#endif
6594
6595#if SANITIZER_INTERCEPT_GETLOADAVG
6596INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6597  void *ctx;
6598  COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6599  int res = REAL(getloadavg)(loadavg, nelem);
6600  if (res > 0)
6601    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6602  return res;
6603}
6604#define INIT_GETLOADAVG                      \
6605  COMMON_INTERCEPT_FUNCTION(getloadavg);
6606#else
6607#define INIT_GETLOADAVG
6608#endif
6609
6610#if SANITIZER_INTERCEPT_MCHECK_MPROBE
6611INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6612  return 0;
6613}
6614
6615INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6616  return 0;
6617}
6618
6619INTERCEPTOR(int, mprobe, void *ptr) {
6620  return 0;
6621}
6622#endif
6623
6624INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6625  void *ctx;
6626  COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6627  SIZE_T res = REAL(wcslen)(s);
6628  COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6629  return res;
6630}
6631
6632INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6633  void *ctx;
6634  COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6635  SIZE_T res = REAL(wcsnlen)(s, n);
6636  COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6637  return res;
6638}
6639#define INIT_WCSLEN                  \
6640  COMMON_INTERCEPT_FUNCTION(wcslen); \
6641  COMMON_INTERCEPT_FUNCTION(wcsnlen);
6642
6643#if SANITIZER_INTERCEPT_WCSCAT
6644INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6645  void *ctx;
6646  COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6647  SIZE_T src_size = REAL(wcslen)(src);
6648  SIZE_T dst_size = REAL(wcslen)(dst);
6649  COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6650  COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6651  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6652                                 (src_size + 1) * sizeof(wchar_t));
6653  return REAL(wcscat)(dst, src);  // NOLINT
6654}
6655
6656INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6657  void *ctx;
6658  COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6659  SIZE_T src_size = REAL(wcsnlen)(src, n);
6660  SIZE_T dst_size = REAL(wcslen)(dst);
6661  COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6662                                Min(src_size + 1, n) * sizeof(wchar_t));
6663  COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6664  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6665                                 (src_size + 1) * sizeof(wchar_t));
6666  return REAL(wcsncat)(dst, src, n);  // NOLINT
6667}
6668#define INIT_WCSCAT                  \
6669  COMMON_INTERCEPT_FUNCTION(wcscat); \
6670  COMMON_INTERCEPT_FUNCTION(wcsncat);
6671#else
6672#define INIT_WCSCAT
6673#endif
6674
6675#if SANITIZER_INTERCEPT_STRXFRM
6676static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6677
6678static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6679
6680#define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...)             \
6681  {                                                                        \
6682    void *ctx;                                                             \
6683    COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6684    COMMON_INTERCEPTOR_READ_RANGE(ctx, src,                                \
6685                                  sizeof(*src) * (RealStrLen(src) + 1));   \
6686    SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__);             \
6687    if (res < len)                                                         \
6688      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6689    return res;                                                            \
6690  }
6691
6692INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6693  STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6694}
6695
6696INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6697            void *locale) {
6698  STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6699}
6700
6701#define INIT_STRXFRM                  \
6702  COMMON_INTERCEPT_FUNCTION(strxfrm); \
6703  COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6704#else
6705#define INIT_STRXFRM
6706#endif
6707
6708#if SANITIZER_INTERCEPT___STRXFRM_L
6709INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6710            void *locale) {
6711  STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6712}
6713
6714#define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6715#else
6716#define INIT___STRXFRM_L
6717#endif
6718
6719#if SANITIZER_INTERCEPT_WCSXFRM
6720INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6721  STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6722}
6723
6724INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6725            void *locale) {
6726  STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6727}
6728
6729#define INIT_WCSXFRM                  \
6730  COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6731  COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6732#else
6733#define INIT_WCSXFRM
6734#endif
6735
6736#if SANITIZER_INTERCEPT___WCSXFRM_L
6737INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6738            void *locale) {
6739  STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6740}
6741
6742#define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6743#else
6744#define INIT___WCSXFRM_L
6745#endif
6746
6747#if SANITIZER_INTERCEPT_ACCT
6748INTERCEPTOR(int, acct, const char *file) {
6749  void *ctx;
6750  COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6751  if (file)
6752    COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6753  return REAL(acct)(file);
6754}
6755#define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6756#else
6757#define INIT_ACCT
6758#endif
6759
6760#if SANITIZER_INTERCEPT_USER_FROM_UID
6761INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6762  void *ctx;
6763  const char *user;
6764  COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6765  user = REAL(user_from_uid)(uid, nouser);
6766  if (user)
6767    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6768  return user;
6769}
6770#define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6771#else
6772#define INIT_USER_FROM_UID
6773#endif
6774
6775#if SANITIZER_INTERCEPT_UID_FROM_USER
6776INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6777  void *ctx;
6778  int res;
6779  COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6780  if (name)
6781    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6782  res = REAL(uid_from_user)(name, uid);
6783  if (uid)
6784    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6785  return res;
6786}
6787#define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6788#else
6789#define INIT_UID_FROM_USER
6790#endif
6791
6792#if SANITIZER_INTERCEPT_GROUP_FROM_GID
6793INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6794  void *ctx;
6795  const char *group;
6796  COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6797  group = REAL(group_from_gid)(gid, nogroup);
6798  if (group)
6799    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6800  return group;
6801}
6802#define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6803#else
6804#define INIT_GROUP_FROM_GID
6805#endif
6806
6807#if SANITIZER_INTERCEPT_GID_FROM_GROUP
6808INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6809  void *ctx;
6810  int res;
6811  COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6812  if (group)
6813    COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6814  res = REAL(gid_from_group)(group, gid);
6815  if (gid)
6816    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6817  return res;
6818}
6819#define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6820#else
6821#define INIT_GID_FROM_GROUP
6822#endif
6823
6824#if SANITIZER_INTERCEPT_ACCESS
6825INTERCEPTOR(int, access, const char *path, int mode) {
6826  void *ctx;
6827  COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6828  if (path)
6829    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6830  return REAL(access)(path, mode);
6831}
6832#define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6833#else
6834#define INIT_ACCESS
6835#endif
6836
6837#if SANITIZER_INTERCEPT_FACCESSAT
6838INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6839  void *ctx;
6840  COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6841  if (path)
6842    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6843  return REAL(faccessat)(fd, path, mode, flags);
6844}
6845#define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6846#else
6847#define INIT_FACCESSAT
6848#endif
6849
6850#if SANITIZER_INTERCEPT_GETGROUPLIST
6851INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6852            int *ngroups) {
6853  void *ctx;
6854  int res;
6855  COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6856  if (name)
6857    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6858  if (ngroups)
6859    COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6860  res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6861  if (!res && groups && ngroups) {
6862    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6863    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6864  }
6865  return res;
6866}
6867
6868#define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6869#else
6870#define INIT_GETGROUPLIST
6871#endif
6872
6873#if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6874INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6875            int maxgrp, int *ngroups) {
6876  void *ctx;
6877  int res;
6878  COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6879                           maxgrp, ngroups);
6880  if (name)
6881    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6882  res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6883  if (!res && groups && ngroups) {
6884    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6885    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6886  }
6887  return res;
6888}
6889
6890#define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6891#else
6892#define INIT_GETGROUPMEMBERSHIP
6893#endif
6894
6895#if SANITIZER_INTERCEPT_READLINK
6896INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6897  void* ctx;
6898  COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6899  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6900  SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6901  if (res > 0)
6902    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6903  return res;
6904}
6905
6906#define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6907#else
6908#define INIT_READLINK
6909#endif
6910
6911#if SANITIZER_INTERCEPT_READLINKAT
6912INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
6913            SIZE_T bufsiz) {
6914  void* ctx;
6915  COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
6916  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6917  SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
6918  if (res > 0)
6919    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6920  return res;
6921}
6922
6923#define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
6924#else
6925#define INIT_READLINKAT
6926#endif
6927
6928#if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
6929INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
6930            struct file_handle *handle, int *mount_id, int flags) {
6931  void* ctx;
6932  COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
6933                           mount_id, flags);
6934  COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
6935
6936  __sanitizer_file_handle *sanitizer_handle =
6937      reinterpret_cast<__sanitizer_file_handle*>(handle);
6938  COMMON_INTERCEPTOR_READ_RANGE(
6939      ctx, &sanitizer_handle->handle_bytes,
6940      sizeof(sanitizer_handle->handle_bytes));
6941
6942  int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
6943  if (!res) {
6944    COMMON_INTERCEPTOR_WRITE_RANGE(
6945        ctx, &sanitizer_handle->handle_bytes,
6946        sizeof(sanitizer_handle->handle_bytes));
6947    COMMON_INTERCEPTOR_WRITE_RANGE(
6948        ctx, &sanitizer_handle->handle_type,
6949        sizeof(sanitizer_handle->handle_type));
6950    COMMON_INTERCEPTOR_WRITE_RANGE(
6951        ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6952    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
6953  }
6954  return res;
6955}
6956
6957#define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
6958#else
6959#define INIT_NAME_TO_HANDLE_AT
6960#endif
6961
6962#if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
6963INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
6964            int flags) {
6965  void* ctx;
6966  COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
6967
6968  __sanitizer_file_handle *sanitizer_handle =
6969      reinterpret_cast<__sanitizer_file_handle*>(handle);
6970  COMMON_INTERCEPTOR_READ_RANGE(
6971      ctx, &sanitizer_handle->handle_bytes,
6972      sizeof(sanitizer_handle->handle_bytes));
6973  COMMON_INTERCEPTOR_READ_RANGE(
6974      ctx, &sanitizer_handle->handle_type,
6975      sizeof(sanitizer_handle->handle_type));
6976  COMMON_INTERCEPTOR_READ_RANGE(
6977      ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6978
6979  return REAL(open_by_handle_at)(mount_fd, handle, flags);
6980}
6981
6982#define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
6983#else
6984#define INIT_OPEN_BY_HANDLE_AT
6985#endif
6986
6987#if SANITIZER_INTERCEPT_STRLCPY
6988INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
6989  void *ctx;
6990  SIZE_T res;
6991  COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
6992  if (src) {
6993    // Keep strnlen as macro argument, as macro may ignore it.
6994    COMMON_INTERCEPTOR_READ_STRING(
6995        ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
6996  }
6997  res = REAL(strlcpy)(dst, src, size);
6998  COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
6999  return res;
7000}
7001
7002INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7003  void *ctx;
7004  SIZE_T len = 0;
7005  COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7006  // src is checked in the strlcpy() interceptor
7007  if (dst) {
7008    len = internal_strnlen(dst, size);
7009    COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7010  }
7011  // Reuse the rest of the code in the strlcpy() interceptor
7012  return WRAP(strlcpy)(dst + len, src, size - len) + len;
7013}
7014#define INIT_STRLCPY \
7015  COMMON_INTERCEPT_FUNCTION(strlcpy); \
7016  COMMON_INTERCEPT_FUNCTION(strlcat);
7017#else
7018#define INIT_STRLCPY
7019#endif
7020
7021#if SANITIZER_INTERCEPT_MMAP
7022INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7023            OFF_T off) {
7024  void *ctx;
7025  if (common_flags()->detect_write_exec)
7026    ReportMmapWriteExec(prot);
7027  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7028    return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7029  COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7030  COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7031}
7032
7033INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7034  void *ctx;
7035  if (common_flags()->detect_write_exec)
7036    ReportMmapWriteExec(prot);
7037  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7038    return (int)internal_mprotect(addr, sz, prot);
7039  COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7040  MprotectMallocZones(addr, prot);
7041  return REAL(mprotect)(addr, sz, prot);
7042}
7043#define INIT_MMAP                                                              \
7044  COMMON_INTERCEPT_FUNCTION(mmap);                                             \
7045  COMMON_INTERCEPT_FUNCTION(mprotect);
7046#else
7047#define INIT_MMAP
7048#endif
7049
7050#if SANITIZER_INTERCEPT_MMAP64
7051INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7052            OFF64_T off) {
7053  void *ctx;
7054  if (common_flags()->detect_write_exec)
7055    ReportMmapWriteExec(prot);
7056  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7057    return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7058  COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7059  COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7060}
7061#define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7062#else
7063#define INIT_MMAP64
7064#endif
7065
7066#if SANITIZER_INTERCEPT_DEVNAME
7067INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7068  void *ctx;
7069  char *name;
7070  COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7071  name = REAL(devname)(dev, type);
7072  if (name)
7073    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7074  return name;
7075}
7076#define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7077#else
7078#define INIT_DEVNAME
7079#endif
7080
7081#if SANITIZER_INTERCEPT_DEVNAME_R
7082#if SANITIZER_NETBSD
7083#define DEVNAME_R_RETTYPE int
7084#define DEVNAME_R_SUCCESS(x) (!(x))
7085#else
7086#define DEVNAME_R_RETTYPE char*
7087#define DEVNAME_R_SUCCESS(x) (x)
7088#endif
7089INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7090            uptr len) {
7091  void *ctx;
7092  COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7093  DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7094  if (DEVNAME_R_SUCCESS(res))
7095    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7096  return res;
7097}
7098#define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7099#else
7100#define INIT_DEVNAME_R
7101#endif
7102
7103#if SANITIZER_INTERCEPT_FGETLN
7104INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7105  void *ctx;
7106  COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7107  char *str = REAL(fgetln)(stream, len);
7108  if (str && len) {
7109    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7110    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7111  }
7112  return str;
7113}
7114#define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7115#else
7116#define INIT_FGETLN
7117#endif
7118
7119#if SANITIZER_INTERCEPT_STRMODE
7120INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7121  void *ctx;
7122  COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7123  REAL(strmode)(mode, bp);
7124  if (bp)
7125    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7126}
7127#define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7128#else
7129#define INIT_STRMODE
7130#endif
7131
7132#if SANITIZER_INTERCEPT_TTYENT
7133INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7134  void *ctx;
7135  COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7136  struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7137  if (ttyent)
7138    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7139  return ttyent;
7140}
7141INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7142  void *ctx;
7143  COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7144  if (name)
7145    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7146  struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7147  if (ttyent)
7148    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7149  return ttyent;
7150}
7151INTERCEPTOR(int, setttyentpath, char *path) {
7152  void *ctx;
7153  COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7154  if (path)
7155    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7156  return REAL(setttyentpath)(path);
7157}
7158#define INIT_TTYENT \
7159  COMMON_INTERCEPT_FUNCTION(getttyent); \
7160  COMMON_INTERCEPT_FUNCTION(getttynam); \
7161  COMMON_INTERCEPT_FUNCTION(setttyentpath)
7162#else
7163#define INIT_TTYENT
7164#endif
7165
7166#if SANITIZER_INTERCEPT_PROTOENT
7167INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7168  void *ctx;
7169  COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7170  struct __sanitizer_protoent *p = REAL(getprotoent)();
7171  if (p) {
7172    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7173
7174    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7175
7176    SIZE_T pp_size = 1; // One handles the trailing \0
7177
7178    for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7179       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7180
7181    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7182                                   pp_size * sizeof(char **));
7183  }
7184  return p;
7185}
7186
7187INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7188  void *ctx;
7189  COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7190  if (name)
7191    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7192  struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7193  if (p) {
7194    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7195
7196    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7197
7198    SIZE_T pp_size = 1; // One handles the trailing \0
7199
7200    for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7201       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7202
7203    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7204                                   pp_size * sizeof(char **));
7205  }
7206  return p;
7207}
7208
7209INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7210  void *ctx;
7211  COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7212  struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7213  if (p) {
7214    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7215
7216    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7217
7218    SIZE_T pp_size = 1; // One handles the trailing \0
7219
7220    for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7221       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7222
7223    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7224                                   pp_size * sizeof(char **));
7225  }
7226  return p;
7227}
7228#define INIT_PROTOENT \
7229  COMMON_INTERCEPT_FUNCTION(getprotoent); \
7230  COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7231  COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7232#else
7233#define INIT_PROTOENT
7234#endif
7235
7236#if SANITIZER_INTERCEPT_NETENT
7237INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7238  void *ctx;
7239  COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7240  struct __sanitizer_netent *n = REAL(getnetent)();
7241  if (n) {
7242    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7243
7244    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7245
7246    SIZE_T nn_size = 1; // One handles the trailing \0
7247
7248    for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7249      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7250
7251    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7252                                   nn_size * sizeof(char **));
7253  }
7254  return n;
7255}
7256
7257INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7258  void *ctx;
7259  COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7260  if (name)
7261    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7262  struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7263  if (n) {
7264    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7265
7266    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7267
7268    SIZE_T nn_size = 1; // One handles the trailing \0
7269
7270    for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7271      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7272
7273    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7274                                   nn_size * sizeof(char **));
7275  }
7276  return n;
7277}
7278
7279INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7280  void *ctx;
7281  COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7282  struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7283  if (n) {
7284    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7285
7286    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7287
7288    SIZE_T nn_size = 1; // One handles the trailing \0
7289
7290    for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7291      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7292
7293    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7294                                   nn_size * sizeof(char **));
7295  }
7296  return n;
7297}
7298#define INIT_NETENT \
7299  COMMON_INTERCEPT_FUNCTION(getnetent); \
7300  COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7301  COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7302#else
7303#define INIT_NETENT
7304#endif
7305
7306#if SANITIZER_INTERCEPT_GETMNTINFO
7307INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7308  void *ctx;
7309  COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7310  int cnt = REAL(getmntinfo)(mntbufp, flags);
7311  if (cnt > 0 && mntbufp) {
7312    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7313    if (*mntbufp)
7314#if SANITIZER_NETBSD
7315      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7316#else
7317      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7318#endif
7319  }
7320  return cnt;
7321}
7322#define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7323#else
7324#define INIT_GETMNTINFO
7325#endif
7326
7327#if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7328INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7329            u32 hashes[3]) {
7330  void *ctx;
7331  COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7332  if (key)
7333    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7334  REAL(mi_vector_hash)(key, len, seed, hashes);
7335  if (hashes)
7336    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7337}
7338#define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7339#else
7340#define INIT_MI_VECTOR_HASH
7341#endif
7342
7343#if SANITIZER_INTERCEPT_SETVBUF
7344INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7345  SIZE_T size) {
7346  void *ctx;
7347  COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7348  int ret = REAL(setvbuf)(stream, buf, mode, size);
7349  if (buf)
7350    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7351  if (stream)
7352      unpoison_file(stream);
7353  return ret;
7354}
7355
7356INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7357  void *ctx;
7358  COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7359  REAL(setbuf)(stream, buf);
7360  if (buf) {
7361    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7362  }
7363  if (stream)
7364      unpoison_file(stream);
7365}
7366
7367INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
7368  void *ctx;
7369  COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
7370  REAL(setbuffer)(stream, buf, mode);
7371  if (buf) {
7372    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7373  }
7374  if (stream)
7375    unpoison_file(stream);
7376}
7377
7378INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7379  void *ctx;
7380  COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7381  REAL(setlinebuf)(stream);
7382  if (stream)
7383    unpoison_file(stream);
7384}
7385#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7386    COMMON_INTERCEPT_FUNCTION(setbuf); \
7387    COMMON_INTERCEPT_FUNCTION(setbuffer); \
7388    COMMON_INTERCEPT_FUNCTION(setlinebuf)
7389#else
7390#define INIT_SETVBUF
7391#endif
7392
7393#if SANITIZER_INTERCEPT_GETVFSSTAT
7394INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7395  void *ctx;
7396  COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7397  int ret = REAL(getvfsstat)(buf, bufsize, flags);
7398  if (buf && ret > 0)
7399    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7400  return ret;
7401}
7402#define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7403#else
7404#define INIT_GETVFSSTAT
7405#endif
7406
7407#if SANITIZER_INTERCEPT_REGEX
7408INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7409  void *ctx;
7410  COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7411  if (pattern)
7412    COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, REAL(strlen)(pattern) + 1);
7413  int res = REAL(regcomp)(preg, pattern, cflags);
7414  if (!res)
7415    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7416  return res;
7417}
7418INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7419            struct __sanitizer_regmatch *pmatch[], int eflags) {
7420  void *ctx;
7421  COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7422  if (preg)
7423    COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7424  if (string)
7425    COMMON_INTERCEPTOR_READ_RANGE(ctx, string, REAL(strlen)(string) + 1);
7426  int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7427  if (!res && pmatch)
7428    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7429  return res;
7430}
7431INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7432            SIZE_T errbuf_size) {
7433  void *ctx;
7434  COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7435  if (preg)
7436    COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7437  SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7438  if (errbuf)
7439    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, REAL(strlen)(errbuf) + 1);
7440  return res;
7441}
7442INTERCEPTOR(void, regfree, const void *preg) {
7443  void *ctx;
7444  COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7445  if (preg)
7446    COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7447  REAL(regfree)(preg);
7448}
7449#define INIT_REGEX                                                             \
7450  COMMON_INTERCEPT_FUNCTION(regcomp);                                          \
7451  COMMON_INTERCEPT_FUNCTION(regexec);                                          \
7452  COMMON_INTERCEPT_FUNCTION(regerror);                                         \
7453  COMMON_INTERCEPT_FUNCTION(regfree);
7454#else
7455#define INIT_REGEX
7456#endif
7457
7458#if SANITIZER_INTERCEPT_REGEXSUB
7459INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7460            const struct __sanitizer_regmatch *rm, const char *str) {
7461  void *ctx;
7462  COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7463  if (sub)
7464    COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7465  // The implementation demands and hardcodes 10 elements
7466  if (rm)
7467    COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7468  if (str)
7469    COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
7470  SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7471  if (res > 0 && buf)
7472    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
7473  return res;
7474}
7475INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7476            const struct __sanitizer_regmatch *rm, const char *sstr) {
7477  void *ctx;
7478  COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7479  if (sub)
7480    COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7481  // Hardcode 10 elements as this is hardcoded size
7482  if (rm)
7483    COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7484  if (sstr)
7485    COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, REAL(strlen)(sstr) + 1);
7486  SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7487  if (res > 0 && buf) {
7488    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7489    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, REAL(strlen)(*buf) + 1);
7490  }
7491  return res;
7492}
7493
7494#define INIT_REGEXSUB                                                          \
7495  COMMON_INTERCEPT_FUNCTION(regnsub);                                          \
7496  COMMON_INTERCEPT_FUNCTION(regasub);
7497#else
7498#define INIT_REGEXSUB
7499#endif
7500
7501#if SANITIZER_INTERCEPT_FTS
7502INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7503            int (*compar)(void **, void **)) {
7504  void *ctx;
7505  COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
7506  if (path_argv) {
7507    for (char *const *pa = path_argv; ; ++pa) {
7508      COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
7509      if (!*pa)
7510        break;
7511      COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
7512    }
7513  }
7514  // TODO(kamil): handle compar callback
7515  void *fts = REAL(fts_open)(path_argv, options, compar);
7516  if (fts)
7517    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
7518  return fts;
7519}
7520
7521INTERCEPTOR(void *, fts_read, void *ftsp) {
7522  void *ctx;
7523  COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
7524  if (ftsp)
7525    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7526  void *ftsent = REAL(fts_read)(ftsp);
7527  if (ftsent)
7528    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7529  return ftsent;
7530}
7531
7532INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
7533  void *ctx;
7534  COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
7535  if (ftsp)
7536    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7537  void *ftsent = REAL(fts_children)(ftsp, options);
7538  if (ftsent)
7539    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7540  return ftsent;
7541}
7542
7543INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
7544  void *ctx;
7545  COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
7546  if (ftsp)
7547    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7548  if (f)
7549    COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
7550  return REAL(fts_set)(ftsp, f, options);
7551}
7552
7553INTERCEPTOR(int, fts_close, void *ftsp) {
7554  void *ctx;
7555  COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
7556  if (ftsp)
7557    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7558  return REAL(fts_close)(ftsp);
7559}
7560#define INIT_FTS                                                               \
7561  COMMON_INTERCEPT_FUNCTION(fts_open);                                         \
7562  COMMON_INTERCEPT_FUNCTION(fts_read);                                         \
7563  COMMON_INTERCEPT_FUNCTION(fts_children);                                     \
7564  COMMON_INTERCEPT_FUNCTION(fts_set);                                          \
7565  COMMON_INTERCEPT_FUNCTION(fts_close);
7566#else
7567#define INIT_FTS
7568#endif
7569
7570#if SANITIZER_INTERCEPT_SYSCTL
7571INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
7572            SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
7573  void *ctx;
7574  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7575    return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
7576  COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
7577                           newlen);
7578  if (name)
7579    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
7580  if (oldlenp)
7581    COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7582  if (newp && newlen)
7583    COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7584  int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
7585  if (!res) {
7586    if (oldlenp) {
7587      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7588      if (oldp)
7589        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7590    }
7591  }
7592  return res;
7593}
7594
7595INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
7596            void *newp, SIZE_T newlen) {
7597  void *ctx;
7598  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7599    return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
7600  COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
7601                           newlen);
7602  if (sname)
7603    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7604  if (oldlenp)
7605    COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7606  if (newp && newlen)
7607    COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7608  int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
7609  if (!res) {
7610    if (oldlenp) {
7611      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7612      if (oldp)
7613        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7614    }
7615  }
7616  return res;
7617}
7618
7619INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
7620            SIZE_T *namelenp) {
7621  void *ctx;
7622  COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
7623  if (sname)
7624    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7625  if (namelenp)
7626    COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7627  int res = REAL(sysctlnametomib)(sname, name, namelenp);
7628  if (!res) {
7629    if (namelenp) {
7630      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7631      if (name)
7632        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7633    }
7634  }
7635  return res;
7636}
7637
7638#define INIT_SYSCTL                        \
7639  COMMON_INTERCEPT_FUNCTION(sysctl);       \
7640  COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
7641  COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
7642#else
7643#define INIT_SYSCTL
7644#endif
7645
7646#if SANITIZER_INTERCEPT_ASYSCTL
7647INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
7648  void *ctx;
7649  COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
7650  if (name)
7651    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
7652  void *res = REAL(asysctl)(name, namelen, len);
7653  if (res && len) {
7654    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7655    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7656  }
7657  return res;
7658}
7659
7660INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
7661  void *ctx;
7662  COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
7663  if (sname)
7664    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7665  void *res = REAL(asysctlbyname)(sname, len);
7666  if (res && len) {
7667    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7668    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7669  }
7670  return res;
7671}
7672#define INIT_ASYSCTL                           \
7673  COMMON_INTERCEPT_FUNCTION(asysctl);          \
7674  COMMON_INTERCEPT_FUNCTION(asysctlbyname);
7675#else
7676#define INIT_ASYSCTL
7677#endif
7678
7679#if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
7680INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
7681            unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
7682            int v) {
7683  void *ctx;
7684  COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
7685                           csz, rnode, v);
7686  if (sname)
7687    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7688  if (namelenp)
7689    COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7690  if (csz)
7691    COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
7692  // Skip rnode, it's rarely used and not trivial to sanitize
7693  // It's also used mostly internally
7694  int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
7695  if (!res) {
7696    if (namelenp) {
7697      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7698      if (name)
7699        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7700    }
7701    if (csz) {
7702      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
7703      if (cname)
7704        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
7705    }
7706  }
7707  return res;
7708}
7709#define INIT_SYSCTLGETMIBINFO                  \
7710  COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
7711#else
7712#define INIT_SYSCTLGETMIBINFO
7713#endif
7714
7715#if SANITIZER_INTERCEPT_NL_LANGINFO
7716INTERCEPTOR(char *, nl_langinfo, long item) {
7717  void *ctx;
7718  COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
7719  char *ret = REAL(nl_langinfo)(item);
7720  if (ret)
7721    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7722  return ret;
7723}
7724#define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
7725#else
7726#define INIT_NL_LANGINFO
7727#endif
7728
7729#if SANITIZER_INTERCEPT_MODCTL
7730INTERCEPTOR(int, modctl, int operation, void *argp) {
7731  void *ctx;
7732  int ret;
7733  COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
7734
7735  if (operation == modctl_load) {
7736    if (argp) {
7737      __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
7738      COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
7739      if (ml->ml_filename)
7740        COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
7741                                      REAL(strlen)(ml->ml_filename) + 1);
7742      if (ml->ml_props)
7743        COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
7744    }
7745    ret = REAL(modctl)(operation, argp);
7746  } else if (operation == modctl_unload) {
7747    if (argp) {
7748      const char *name = (const char *)argp;
7749      COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7750    }
7751    ret = REAL(modctl)(operation, argp);
7752  } else if (operation == modctl_stat) {
7753    uptr iov_len;
7754    struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
7755    if (iov) {
7756      COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
7757      iov_len = iov->iov_len;
7758    }
7759    ret = REAL(modctl)(operation, argp);
7760    if (iov)
7761      COMMON_INTERCEPTOR_WRITE_RANGE(
7762          ctx, iov->iov_base, Min(iov_len,  iov->iov_len));
7763  } else if (operation == modctl_exists)
7764    ret = REAL(modctl)(operation, argp);
7765  else
7766    ret = REAL(modctl)(operation, argp);
7767
7768  return ret;
7769}
7770#define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
7771#else
7772#define INIT_MODCTL
7773#endif
7774
7775#if SANITIZER_INTERCEPT_STRTONUM
7776INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
7777            long long maxval, const char **errstr) {
7778  void *ctx;
7779  COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
7780
7781  // TODO(kamil): Implement strtoll as a common inteceptor
7782  char *real_endptr;
7783  long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
7784  StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
7785
7786  ret = REAL(strtonum)(nptr, minval, maxval, errstr);
7787  if (errstr) {
7788    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
7789     if (*errstr)
7790      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1);
7791  }
7792  return ret;
7793}
7794#define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
7795#else
7796#define INIT_STRTONUM
7797#endif
7798
7799#if SANITIZER_INTERCEPT_FPARSELN
7800INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
7801            SIZE_T *lineno, const char delim[3], int flags) {
7802  void *ctx;
7803  COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
7804  if (lineno)
7805    COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
7806  if (delim)
7807    COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
7808  char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
7809  if (ret) {
7810    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7811    if (len)
7812      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7813    if (lineno)
7814      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
7815  }
7816  return ret;
7817}
7818#define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
7819#else
7820#define INIT_FPARSELN
7821#endif
7822
7823#if SANITIZER_INTERCEPT_STATVFS1
7824INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
7825  void *ctx;
7826  COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
7827  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7828  int res = REAL(statvfs1)(path, buf, flags);
7829  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7830  return res;
7831}
7832INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
7833  void *ctx;
7834  COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
7835  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7836  int res = REAL(fstatvfs1)(fd, buf, flags);
7837  if (!res) {
7838    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7839    if (fd >= 0)
7840      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
7841  }
7842  return res;
7843}
7844#define INIT_STATVFS1                  \
7845  COMMON_INTERCEPT_FUNCTION(statvfs1);  \
7846  COMMON_INTERCEPT_FUNCTION(fstatvfs1);
7847#else
7848#define INIT_STATVFS1
7849#endif
7850
7851#if SANITIZER_INTERCEPT_STRTOI
7852INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
7853            INTMAX_T low, INTMAX_T high, int *rstatus) {
7854  void *ctx;
7855  COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
7856  char *real_endptr;
7857  INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
7858  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7859  if (rstatus)
7860    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7861  return ret;
7862}
7863
7864INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
7865            UINTMAX_T low, UINTMAX_T high, int *rstatus) {
7866  void *ctx;
7867  COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
7868  char *real_endptr;
7869  UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
7870  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7871  if (rstatus)
7872    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7873  return ret;
7874}
7875#define INIT_STRTOI                                                            \
7876  COMMON_INTERCEPT_FUNCTION(strtoi);                                           \
7877  COMMON_INTERCEPT_FUNCTION(strtou)
7878#else
7879#define INIT_STRTOI
7880#endif
7881
7882#if SANITIZER_INTERCEPT_CAPSICUM
7883#define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...)          \
7884  {                                                                        \
7885    void *ctx;                                                             \
7886    COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
7887    if (rights)                                                            \
7888      COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));         \
7889    __sanitizer_cap_rights_t *ret =                                        \
7890        REAL(cap_rights_init)(rights, ##__VA_ARGS__);                      \
7891    if (ret)                                                               \
7892      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));              \
7893    return ret;                                                            \
7894  }
7895
7896#define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...)           \
7897  {                                                                       \
7898    void *ctx;                                                            \
7899    COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
7900    if (rights)                                                           \
7901      COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));        \
7902    __sanitizer_cap_rights_t *ret =                                       \
7903        REAL(cap_rights_set)(rights, ##__VA_ARGS__);                      \
7904    if (ret)                                                              \
7905      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));             \
7906    return ret;                                                           \
7907  }
7908
7909#define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...)         \
7910  {                                                                         \
7911    void *ctx;                                                              \
7912    COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
7913    if (rights)                                                             \
7914      COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));          \
7915    __sanitizer_cap_rights_t *ret =                                         \
7916        REAL(cap_rights_clear)(rights, ##__VA_ARGS__);                      \
7917    if (ret)                                                                \
7918      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));               \
7919    return ret;                                                             \
7920  }
7921
7922#define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...)        \
7923  {                                                                          \
7924    void *ctx;                                                               \
7925    COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
7926    if (rights)                                                              \
7927      COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));           \
7928    return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__);                   \
7929  }
7930
7931INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
7932            __sanitizer_cap_rights_t *rights) {
7933  CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
7934}
7935
7936INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
7937            __sanitizer_cap_rights_t *rights) {
7938  CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
7939}
7940
7941INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
7942            __sanitizer_cap_rights_t *rights) {
7943  CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
7944}
7945
7946INTERCEPTOR(bool, cap_rights_is_set,
7947            __sanitizer_cap_rights_t *rights) {
7948  CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
7949}
7950
7951INTERCEPTOR(int, cap_rights_limit, int fd,
7952            const __sanitizer_cap_rights_t *rights) {
7953  void *ctx;
7954  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
7955  if (rights)
7956    COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
7957
7958  return REAL(cap_rights_limit)(fd, rights);
7959}
7960
7961INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
7962  void *ctx;
7963  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
7964  int ret = REAL(cap_rights_get)(fd, rights);
7965  if (!ret && rights)
7966    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
7967
7968  return ret;
7969}
7970
7971INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
7972  void *ctx;
7973  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
7974  if (rights)
7975    COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
7976
7977  return REAL(cap_rights_is_valid(rights));
7978}
7979
7980INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
7981  __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
7982  void *ctx;
7983  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
7984  if (src)
7985    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
7986
7987  __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
7988  if (dst)
7989    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
7990
7991  return ret;
7992}
7993
7994INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
7995  __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
7996  void *ctx;
7997  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
7998  if (src)
7999    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8000
8001  __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8002  if (dst)
8003    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8004
8005  return ret;
8006}
8007
8008INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8009  const __sanitizer_cap_rights *little) {
8010  void *ctx;
8011  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8012  if (little)
8013    COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8014  if (big)
8015    COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8016
8017  return REAL(cap_rights_contains)(big, little);
8018}
8019
8020INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8021  void *ctx;
8022  COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8023  if (cmds)
8024    COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8025
8026  return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8027}
8028
8029INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8030  void *ctx;
8031  COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8032  int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8033  if (!ret && cmds)
8034    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8035
8036  return ret;
8037}
8038#define INIT_CAPSICUM                          \
8039  COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8040  COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8041  COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8042  COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8043  COMMON_INTERCEPT_FUNCTION(cap_rights_get);   \
8044  COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8045  COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8046  COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8047  COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8048  COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8049  COMMON_INTERCEPT_FUNCTION(cap_ioctls_get);   \
8050  COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8051#else
8052#define INIT_CAPSICUM
8053#endif
8054
8055#if SANITIZER_INTERCEPT_SHA1
8056INTERCEPTOR(void, SHA1Init, void *context) {
8057  void *ctx;
8058  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8059  REAL(SHA1Init)(context);
8060  if (context)
8061    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8062}
8063INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8064  void *ctx;
8065  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8066  if (data && len > 0)
8067    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8068  if (context)
8069    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8070  REAL(SHA1Update)(context, data, len);
8071  if (context)
8072    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8073}
8074INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8075  void *ctx;
8076  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8077  if (context)
8078    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8079  REAL(SHA1Final)(digest, context);
8080  if (digest)
8081    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8082}
8083INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8084  void *ctx;
8085  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8086  if (state)
8087    COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8088  if (buffer)
8089    COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8090  REAL(SHA1Transform)(state, buffer);
8091  if (state)
8092    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8093}
8094INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8095  void *ctx;
8096  COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8097  if (context)
8098    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8099  char *ret = REAL(SHA1End)(context, buf);
8100  if (ret)
8101    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8102  return ret;
8103}
8104INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8105  void *ctx;
8106  COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8107  if (filename)
8108    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8109  char *ret = REAL(SHA1File)(filename, buf);
8110  if (ret)
8111    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8112  return ret;
8113}
8114INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8115  OFF_T length) {
8116  void *ctx;
8117  COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8118  if (filename)
8119    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8120  char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8121  if (ret)
8122    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8123  return ret;
8124}
8125INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8126  void *ctx;
8127  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8128  if (data)
8129    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8130  char *ret = REAL(SHA1Data)(data, len, buf);
8131  if (ret)
8132    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8133  return ret;
8134}
8135#define INIT_SHA1                                                              \
8136  COMMON_INTERCEPT_FUNCTION(SHA1Init);                                         \
8137  COMMON_INTERCEPT_FUNCTION(SHA1Update);                                       \
8138  COMMON_INTERCEPT_FUNCTION(SHA1Final);                                        \
8139  COMMON_INTERCEPT_FUNCTION(SHA1Transform);                                    \
8140  COMMON_INTERCEPT_FUNCTION(SHA1End);                                          \
8141  COMMON_INTERCEPT_FUNCTION(SHA1File);                                         \
8142  COMMON_INTERCEPT_FUNCTION(SHA1FileChunk);                                    \
8143  COMMON_INTERCEPT_FUNCTION(SHA1Data)
8144#else
8145#define INIT_SHA1
8146#endif
8147
8148#if SANITIZER_INTERCEPT_MD4
8149INTERCEPTOR(void, MD4Init, void *context) {
8150  void *ctx;
8151  COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8152  REAL(MD4Init)(context);
8153  if (context)
8154    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8155}
8156
8157INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8158            unsigned int len) {
8159  void *ctx;
8160  COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8161  if (data && len > 0)
8162    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8163  if (context)
8164    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8165  REAL(MD4Update)(context, data, len);
8166  if (context)
8167    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8168}
8169
8170INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8171  void *ctx;
8172  COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8173  if (context)
8174    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8175  REAL(MD4Final)(digest, context);
8176  if (digest)
8177    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8178}
8179
8180INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8181  void *ctx;
8182  COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8183  if (context)
8184    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8185  char *ret = REAL(MD4End)(context, buf);
8186  if (ret)
8187    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8188  return ret;
8189}
8190
8191INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8192  void *ctx;
8193  COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8194  if (filename)
8195    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8196  char *ret = REAL(MD4File)(filename, buf);
8197  if (ret)
8198    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8199  return ret;
8200}
8201
8202INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8203            char *buf) {
8204  void *ctx;
8205  COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8206  if (data && len > 0)
8207    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8208  char *ret = REAL(MD4Data)(data, len, buf);
8209  if (ret)
8210    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8211  return ret;
8212}
8213
8214#define INIT_MD4                                                               \
8215  COMMON_INTERCEPT_FUNCTION(MD4Init);                                          \
8216  COMMON_INTERCEPT_FUNCTION(MD4Update);                                        \
8217  COMMON_INTERCEPT_FUNCTION(MD4Final);                                         \
8218  COMMON_INTERCEPT_FUNCTION(MD4End);                                           \
8219  COMMON_INTERCEPT_FUNCTION(MD4File);                                          \
8220  COMMON_INTERCEPT_FUNCTION(MD4Data)
8221#else
8222#define INIT_MD4
8223#endif
8224
8225#if SANITIZER_INTERCEPT_RMD160
8226INTERCEPTOR(void, RMD160Init, void *context) {
8227  void *ctx;
8228  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8229  REAL(RMD160Init)(context);
8230  if (context)
8231    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8232}
8233INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8234  void *ctx;
8235  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8236  if (data && len > 0)
8237    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8238  if (context)
8239    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8240  REAL(RMD160Update)(context, data, len);
8241  if (context)
8242    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8243}
8244INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8245  void *ctx;
8246  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8247  if (context)
8248    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8249  REAL(RMD160Final)(digest, context);
8250  if (digest)
8251    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8252}
8253INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8254  void *ctx;
8255  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8256  if (state)
8257    COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8258  if (buffer)
8259    COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8260  REAL(RMD160Transform)(state, buffer);
8261  if (state)
8262    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8263}
8264INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8265  void *ctx;
8266  COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8267  if (context)
8268    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8269  char *ret = REAL(RMD160End)(context, buf);
8270  if (ret)
8271    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8272  return ret;
8273}
8274INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8275  void *ctx;
8276  COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8277  if (filename)
8278    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8279  char *ret = REAL(RMD160File)(filename, buf);
8280  if (ret)
8281    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8282  return ret;
8283}
8284INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8285  OFF_T length) {
8286  void *ctx;
8287  COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8288  if (filename)
8289    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8290  char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8291  if (ret)
8292    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8293  return ret;
8294}
8295INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8296  void *ctx;
8297  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8298  if (data && len > 0)
8299    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8300  char *ret = REAL(RMD160Data)(data, len, buf);
8301  if (ret)
8302    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8303  return ret;
8304}
8305#define INIT_RMD160                                                            \
8306  COMMON_INTERCEPT_FUNCTION(RMD160Init);                                       \
8307  COMMON_INTERCEPT_FUNCTION(RMD160Update);                                     \
8308  COMMON_INTERCEPT_FUNCTION(RMD160Final);                                      \
8309  COMMON_INTERCEPT_FUNCTION(RMD160Transform);                                  \
8310  COMMON_INTERCEPT_FUNCTION(RMD160End);                                        \
8311  COMMON_INTERCEPT_FUNCTION(RMD160File);                                       \
8312  COMMON_INTERCEPT_FUNCTION(RMD160FileChunk);                                  \
8313  COMMON_INTERCEPT_FUNCTION(RMD160Data)
8314#else
8315#define INIT_RMD160
8316#endif
8317
8318#if SANITIZER_INTERCEPT_MD5
8319INTERCEPTOR(void, MD5Init, void *context) {
8320  void *ctx;
8321  COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8322  REAL(MD5Init)(context);
8323  if (context)
8324    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8325}
8326
8327INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8328            unsigned int len) {
8329  void *ctx;
8330  COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8331  if (data && len > 0)
8332    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8333  if (context)
8334    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8335  REAL(MD5Update)(context, data, len);
8336  if (context)
8337    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8338}
8339
8340INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8341  void *ctx;
8342  COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8343  if (context)
8344    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8345  REAL(MD5Final)(digest, context);
8346  if (digest)
8347    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8348}
8349
8350INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8351  void *ctx;
8352  COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8353  if (context)
8354    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8355  char *ret = REAL(MD5End)(context, buf);
8356  if (ret)
8357    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8358  return ret;
8359}
8360
8361INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8362  void *ctx;
8363  COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8364  if (filename)
8365    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8366  char *ret = REAL(MD5File)(filename, buf);
8367  if (ret)
8368    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8369  return ret;
8370}
8371
8372INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8373            char *buf) {
8374  void *ctx;
8375  COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8376  if (data && len > 0)
8377    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8378  char *ret = REAL(MD5Data)(data, len, buf);
8379  if (ret)
8380    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8381  return ret;
8382}
8383
8384#define INIT_MD5                                                               \
8385  COMMON_INTERCEPT_FUNCTION(MD5Init);                                          \
8386  COMMON_INTERCEPT_FUNCTION(MD5Update);                                        \
8387  COMMON_INTERCEPT_FUNCTION(MD5Final);                                         \
8388  COMMON_INTERCEPT_FUNCTION(MD5End);                                           \
8389  COMMON_INTERCEPT_FUNCTION(MD5File);                                          \
8390  COMMON_INTERCEPT_FUNCTION(MD5Data)
8391#else
8392#define INIT_MD5
8393#endif
8394
8395#if SANITIZER_INTERCEPT_FSEEK
8396INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8397  void *ctx;
8398  COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8399  return REAL(fseek)(stream, offset, whence);
8400}
8401INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8402  void *ctx;
8403  COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8404  return REAL(fseeko)(stream, offset, whence);
8405}
8406INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8407  void *ctx;
8408  COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8409  return REAL(ftell)(stream);
8410}
8411INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8412  void *ctx;
8413  COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8414  return REAL(ftello)(stream);
8415}
8416INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8417  void *ctx;
8418  COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8419  return REAL(rewind)(stream);
8420}
8421INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8422  void *ctx;
8423  COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8424  int ret = REAL(fgetpos)(stream, pos);
8425  if (pos && !ret)
8426    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8427  return ret;
8428}
8429INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8430  void *ctx;
8431  COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8432  if (pos)
8433    COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8434  return REAL(fsetpos)(stream, pos);
8435}
8436#define INIT_FSEEK \
8437  COMMON_INTERCEPT_FUNCTION(fseek); \
8438  COMMON_INTERCEPT_FUNCTION(fseeko); \
8439  COMMON_INTERCEPT_FUNCTION(ftell); \
8440  COMMON_INTERCEPT_FUNCTION(ftello); \
8441  COMMON_INTERCEPT_FUNCTION(rewind); \
8442  COMMON_INTERCEPT_FUNCTION(fgetpos); \
8443  COMMON_INTERCEPT_FUNCTION(fsetpos)
8444#else
8445#define INIT_FSEEK
8446#endif
8447
8448#if SANITIZER_INTERCEPT_MD2
8449INTERCEPTOR(void, MD2Init, void *context) {
8450  void *ctx;
8451  COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8452  REAL(MD2Init)(context);
8453  if (context)
8454    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8455}
8456
8457INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8458            unsigned int len) {
8459  void *ctx;
8460  COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8461  if (data && len > 0)
8462    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8463  if (context)
8464    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8465  REAL(MD2Update)(context, data, len);
8466  if (context)
8467    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8468}
8469
8470INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8471  void *ctx;
8472  COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8473  if (context)
8474    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8475  REAL(MD2Final)(digest, context);
8476  if (digest)
8477    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8478}
8479
8480INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8481  void *ctx;
8482  COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8483  if (context)
8484    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8485  char *ret = REAL(MD2End)(context, buf);
8486  if (ret)
8487    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8488  return ret;
8489}
8490
8491INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8492  void *ctx;
8493  COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8494  if (filename)
8495    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8496  char *ret = REAL(MD2File)(filename, buf);
8497  if (ret)
8498    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8499  return ret;
8500}
8501
8502INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8503            char *buf) {
8504  void *ctx;
8505  COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8506  if (data && len > 0)
8507    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8508  char *ret = REAL(MD2Data)(data, len, buf);
8509  if (ret)
8510    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8511  return ret;
8512}
8513
8514#define INIT_MD2                                                               \
8515  COMMON_INTERCEPT_FUNCTION(MD2Init);                                          \
8516  COMMON_INTERCEPT_FUNCTION(MD2Update);                                        \
8517  COMMON_INTERCEPT_FUNCTION(MD2Final);                                         \
8518  COMMON_INTERCEPT_FUNCTION(MD2End);                                           \
8519  COMMON_INTERCEPT_FUNCTION(MD2File);                                          \
8520  COMMON_INTERCEPT_FUNCTION(MD2Data)
8521#else
8522#define INIT_MD2
8523#endif
8524
8525#if SANITIZER_INTERCEPT_SHA2
8526#define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
8527  INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
8528    void *ctx; \
8529    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
8530    REAL(SHA##LEN##_Init)(context); \
8531    if (context) \
8532      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8533  } \
8534  INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
8535              const u8 *data, SIZE_T len) { \
8536    void *ctx; \
8537    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
8538    if (data && len > 0) \
8539      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8540    if (context) \
8541      COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8542    REAL(SHA##LEN##_Update)(context, data, len); \
8543    if (context) \
8544      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8545  } \
8546  INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
8547  void *context) { \
8548    void *ctx; \
8549    CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
8550    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
8551    if (context) \
8552      COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8553    REAL(SHA##LEN##_Final)(digest, context); \
8554    if (digest) \
8555      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
8556                                     sizeof(digest[0]) * \
8557  SHA##LEN##_digest_length); \
8558  } \
8559  INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
8560    void *ctx; \
8561    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
8562    if (context) \
8563      COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8564    char *ret = REAL(SHA##LEN##_End)(context, buf); \
8565    if (ret) \
8566      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8567    return ret; \
8568  } \
8569  INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
8570    void *ctx; \
8571    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
8572    if (filename) \
8573      COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8574    char *ret = REAL(SHA##LEN##_File)(filename, buf); \
8575    if (ret) \
8576      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8577    return ret; \
8578  } \
8579  INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
8580              OFF_T offset, OFF_T length) { \
8581    void *ctx; \
8582    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
8583  length); \
8584    if (filename) \
8585      COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8586    char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
8587    if (ret) \
8588      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8589    return ret; \
8590  } \
8591  INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
8592    void *ctx; \
8593    COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
8594    if (data && len > 0) \
8595      COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8596    char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
8597    if (ret) \
8598      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8599    return ret; \
8600  }
8601
8602SHA2_INTERCEPTORS(224, u32);
8603SHA2_INTERCEPTORS(256, u32);
8604SHA2_INTERCEPTORS(384, u64);
8605SHA2_INTERCEPTORS(512, u64);
8606
8607#define INIT_SHA2_INTECEPTORS(LEN) \
8608  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
8609  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
8610  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
8611  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
8612  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
8613  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
8614  COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
8615
8616#define INIT_SHA2 \
8617  INIT_SHA2_INTECEPTORS(224); \
8618  INIT_SHA2_INTECEPTORS(256); \
8619  INIT_SHA2_INTECEPTORS(384); \
8620  INIT_SHA2_INTECEPTORS(512)
8621#undef SHA2_INTERCEPTORS
8622#else
8623#define INIT_SHA2
8624#endif
8625
8626#if SANITIZER_INTERCEPT_VIS
8627INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
8628  void *ctx;
8629  COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
8630  char *end = REAL(vis)(dst, c, flag, nextc);
8631  // dst is NULL terminated and end points to the NULL char
8632  if (dst && end)
8633    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8634  return end;
8635}
8636INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
8637  void *ctx;
8638  COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
8639  char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
8640  // nvis cannot make sure the dst is NULL terminated
8641  if (dst && end)
8642    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8643  return end;
8644}
8645INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
8646  void *ctx;
8647  COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
8648  if (src)
8649    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8650  int len = REAL(strvis)(dst, src, flag);
8651  if (dst)
8652    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8653  return len;
8654}
8655INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
8656  void *ctx;
8657  COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
8658  if (src)
8659    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8660  int len = REAL(stravis)(dst, src, flag);
8661  if (dst) {
8662    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
8663    if (*dst)
8664      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
8665  }
8666  return len;
8667}
8668INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
8669  void *ctx;
8670  COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
8671  if (src)
8672    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8673  int len = REAL(strnvis)(dst, dlen, src, flag);
8674  // The interface will be valid even if there is no space for NULL char
8675  if (dst && len > 0)
8676    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8677  return len;
8678}
8679INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
8680  void *ctx;
8681  COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
8682  if (src)
8683    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8684  int ret = REAL(strvisx)(dst, src, len, flag);
8685  if (dst)
8686    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8687  return ret;
8688}
8689INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8690            int flag) {
8691  void *ctx;
8692  COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
8693  if (src)
8694    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8695  int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
8696  if (dst && ret >= 0)
8697    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8698  return ret;
8699}
8700INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8701            int flag, int *cerr_ptr) {
8702  void *ctx;
8703  COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
8704  if (src)
8705    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8706  // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8707  // according to the implementation
8708  if (cerr_ptr)
8709    COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8710  int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
8711  if (dst && ret >= 0)
8712    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8713  if (cerr_ptr)
8714    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8715  return ret;
8716}
8717INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
8718            const char *extra) {
8719  void *ctx;
8720  COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
8721  if (extra)
8722    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8723  char *end = REAL(svis)(dst, c, flag, nextc, extra);
8724  if (dst && end)
8725    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8726  return end;
8727}
8728INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
8729            const char *extra) {
8730  void *ctx;
8731  COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
8732  if (extra)
8733    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8734  char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
8735  if (dst && end)
8736    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
8737                                   Min((SIZE_T)(end - dst + 1), dlen));
8738  return end;
8739}
8740INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
8741            const char *extra) {
8742  void *ctx;
8743  COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
8744  if (src)
8745    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8746  if (extra)
8747    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8748  int len = REAL(strsvis)(dst, src, flag, extra);
8749  if (dst)
8750    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8751  return len;
8752}
8753INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
8754            const char *extra) {
8755  void *ctx;
8756  COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
8757  if (src)
8758    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8759  if (extra)
8760    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8761  int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
8762  // The interface will be valid even if there is no space for NULL char
8763  if (dst && len >= 0)
8764    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8765  return len;
8766}
8767INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
8768            const char *extra) {
8769  void *ctx;
8770  COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
8771  if (src)
8772    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8773  if (extra)
8774    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8775  int ret = REAL(strsvisx)(dst, src, len, flag, extra);
8776  if (dst)
8777    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8778  return ret;
8779}
8780INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8781            int flag, const char *extra) {
8782  void *ctx;
8783  COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
8784  if (src)
8785    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8786  if (extra)
8787    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8788  int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
8789  if (dst && ret >= 0)
8790    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8791  return ret;
8792}
8793INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
8794            SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
8795  void *ctx;
8796  COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
8797                           cerr_ptr);
8798  if (src)
8799    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8800  if (extra)
8801    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8802  // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8803  // according to the implementation
8804  if (cerr_ptr)
8805    COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8806  int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
8807  if (dst && ret >= 0)
8808    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8809  if (cerr_ptr)
8810    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8811  return ret;
8812}
8813INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
8814  void *ctx;
8815  COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
8816  if (astate)
8817    COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
8818  int ret = REAL(unvis)(cp, c, astate, flag);
8819  if (ret == unvis_valid || ret == unvis_validpush) {
8820    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
8821  }
8822  return ret;
8823}
8824INTERCEPTOR(int, strunvis, char *dst, const char *src) {
8825  void *ctx;
8826  COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
8827  if (src)
8828    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8829  int ret = REAL(strunvis)(dst, src);
8830  if (ret != -1)
8831    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8832  return ret;
8833}
8834INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
8835  void *ctx;
8836  COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
8837  if (src)
8838    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8839  int ret = REAL(strnunvis)(dst, dlen, src);
8840  if (ret != -1)
8841    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8842  return ret;
8843}
8844INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
8845  void *ctx;
8846  COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
8847  if (src)
8848    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8849  int ret = REAL(strunvisx)(dst, src, flag);
8850  if (ret != -1)
8851    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8852  return ret;
8853}
8854INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
8855            int flag) {
8856  void *ctx;
8857  COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
8858  if (src)
8859    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8860  int ret = REAL(strnunvisx)(dst, dlen, src, flag);
8861  if (ret != -1)
8862    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8863  return ret;
8864}
8865#define INIT_VIS                                                               \
8866  COMMON_INTERCEPT_FUNCTION(vis);                                              \
8867  COMMON_INTERCEPT_FUNCTION(nvis);                                             \
8868  COMMON_INTERCEPT_FUNCTION(strvis);                                           \
8869  COMMON_INTERCEPT_FUNCTION(stravis);                                          \
8870  COMMON_INTERCEPT_FUNCTION(strnvis);                                          \
8871  COMMON_INTERCEPT_FUNCTION(strvisx);                                          \
8872  COMMON_INTERCEPT_FUNCTION(strnvisx);                                         \
8873  COMMON_INTERCEPT_FUNCTION(strenvisx);                                        \
8874  COMMON_INTERCEPT_FUNCTION(svis);                                             \
8875  COMMON_INTERCEPT_FUNCTION(snvis);                                            \
8876  COMMON_INTERCEPT_FUNCTION(strsvis);                                          \
8877  COMMON_INTERCEPT_FUNCTION(strsnvis);                                         \
8878  COMMON_INTERCEPT_FUNCTION(strsvisx);                                         \
8879  COMMON_INTERCEPT_FUNCTION(strsnvisx);                                        \
8880  COMMON_INTERCEPT_FUNCTION(strsenvisx);                                       \
8881  COMMON_INTERCEPT_FUNCTION(unvis);                                            \
8882  COMMON_INTERCEPT_FUNCTION(strunvis);                                         \
8883  COMMON_INTERCEPT_FUNCTION(strnunvis);                                        \
8884  COMMON_INTERCEPT_FUNCTION(strunvisx);                                        \
8885  COMMON_INTERCEPT_FUNCTION(strnunvisx)
8886#else
8887#define INIT_VIS
8888#endif
8889
8890#if SANITIZER_INTERCEPT_CDB
8891INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
8892  void *ctx;
8893  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
8894  if (path)
8895    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
8896  struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
8897  if (cdbr)
8898    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8899  return cdbr;
8900}
8901
8902INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
8903  int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
8904  void *ctx;
8905  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
8906    cookie);
8907  if (base && size)
8908    COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
8909  struct __sanitizer_cdbr *cdbr =
8910    REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
8911  if (cdbr)
8912    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8913  return cdbr;
8914}
8915
8916INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
8917  void *ctx;
8918  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
8919  if (cdbr)
8920    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8921  return REAL(cdbr_entries)(cdbr);
8922}
8923
8924INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
8925            const void **data, SIZE_T *datalen) {
8926  void *ctx;
8927  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
8928  if (cdbr)
8929    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8930  int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
8931  if (!ret) {
8932    if (data)
8933      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
8934    if (datalen)
8935      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
8936    if (data && datalen)
8937      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
8938  }
8939  return ret;
8940}
8941
8942INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
8943            SIZE_T keylen, const void **data, SIZE_T *datalen) {
8944  void *ctx;
8945  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
8946  if (cdbr)
8947    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8948  if (key)
8949    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
8950  int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
8951  if (!ret) {
8952    if (data)
8953      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
8954    if (datalen)
8955      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
8956    if (data && datalen)
8957      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
8958  }
8959  return ret;
8960}
8961
8962INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
8963  void *ctx;
8964  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
8965  if (cdbr)
8966    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8967  REAL(cdbr_close)(cdbr);
8968}
8969
8970INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
8971  void *ctx;
8972  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
8973  struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
8974  if (ret)
8975    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
8976  return ret;
8977}
8978
8979INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
8980  SIZE_T keylen, const void *data, SIZE_T datalen) {
8981  void *ctx;
8982  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
8983  if (cdbw)
8984    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
8985  if (data && datalen)
8986    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
8987  if (key && keylen)
8988    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
8989  int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
8990  if (!ret && cdbw)
8991    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
8992  return ret;
8993}
8994
8995INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
8996  SIZE_T datalen, u32 *index) {
8997  void *ctx;
8998  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
8999  if (cdbw)
9000    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9001  if (data && datalen)
9002    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9003  int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9004  if (!ret) {
9005    if (index)
9006      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9007    if (cdbw)
9008      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9009  }
9010  return ret;
9011}
9012
9013INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9014  SIZE_T keylen, u32 index) {
9015  void *ctx;
9016  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9017  if (cdbw)
9018    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9019  if (key && keylen)
9020    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9021  int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9022  if (!ret && cdbw)
9023    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9024  return ret;
9025}
9026
9027INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9028  const char descr[16], u32 (*seedgen)(void)) {
9029  void *ctx;
9030  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9031  COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9032  if (cdbw)
9033    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9034  if (descr)
9035    COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9036  if (seedgen)
9037    COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9038  int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9039  if (!ret) {
9040    if (cdbw)
9041      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9042    if (output >= 0)
9043      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9044  }
9045  return ret;
9046}
9047
9048INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9049  void *ctx;
9050  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9051  if (cdbw)
9052    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9053  REAL(cdbw_close)(cdbw);
9054}
9055
9056#define INIT_CDB \
9057  COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9058  COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9059  COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9060  COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9061  COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9062  COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9063  COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9064  COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9065  COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9066  COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9067  COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9068  COMMON_INTERCEPT_FUNCTION(cdbw_close)
9069#else
9070#define INIT_CDB
9071#endif
9072
9073#if SANITIZER_INTERCEPT_GETFSENT
9074INTERCEPTOR(void *, getfsent) {
9075  void *ctx;
9076  COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9077  void *ret = REAL(getfsent)();
9078  if (ret)
9079    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9080  return ret;
9081}
9082
9083INTERCEPTOR(void *, getfsspec, const char *spec) {
9084  void *ctx;
9085  COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9086  if (spec)
9087    COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, REAL(strlen)(spec) + 1);
9088  void *ret = REAL(getfsspec)(spec);
9089  if (ret)
9090    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9091  return ret;
9092}
9093
9094INTERCEPTOR(void *, getfsfile, const char *file) {
9095  void *ctx;
9096  COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9097  if (file)
9098    COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
9099  void *ret = REAL(getfsfile)(file);
9100  if (ret)
9101    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9102  return ret;
9103}
9104
9105#define INIT_GETFSENT \
9106  COMMON_INTERCEPT_FUNCTION(getfsent); \
9107  COMMON_INTERCEPT_FUNCTION(getfsspec); \
9108  COMMON_INTERCEPT_FUNCTION(getfsfile);
9109#else
9110#define INIT_GETFSENT
9111#endif
9112
9113#if SANITIZER_INTERCEPT_ARC4RANDOM
9114INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9115  void *ctx;
9116  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9117  REAL(arc4random_buf)(buf, len);
9118  if (buf && len)
9119    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9120}
9121
9122INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9123  void *ctx;
9124  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9125  if (dat && datlen)
9126    COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9127  REAL(arc4random_addrandom)(dat, datlen);
9128}
9129
9130#define INIT_ARC4RANDOM \
9131  COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9132  COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9133#else
9134#define INIT_ARC4RANDOM
9135#endif
9136
9137#if SANITIZER_INTERCEPT_POPEN
9138INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9139  void *ctx;
9140  COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9141  if (command)
9142    COMMON_INTERCEPTOR_READ_RANGE(ctx, command, REAL(strlen)(command) + 1);
9143  if (type)
9144    COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9145  __sanitizer_FILE *res = REAL(popen)(command, type);
9146  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9147  if (res) unpoison_file(res);
9148  return res;
9149}
9150#define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9151#else
9152#define INIT_POPEN
9153#endif
9154
9155#if SANITIZER_INTERCEPT_POPENVE
9156INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9157            char *const *argv, char *const *envp, const char *type) {
9158  void *ctx;
9159  COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9160  if (path)
9161    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
9162  if (argv) {
9163    for (char *const *pa = argv; ; ++pa) {
9164      COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9165      if (!*pa)
9166        break;
9167      COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9168    }
9169  }
9170  if (envp) {
9171    for (char *const *pa = envp; ; ++pa) {
9172      COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9173      if (!*pa)
9174        break;
9175      COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9176    }
9177  }
9178  if (type)
9179    COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9180  __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9181  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9182  if (res) unpoison_file(res);
9183  return res;
9184}
9185#define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9186#else
9187#define INIT_POPENVE
9188#endif
9189
9190#if SANITIZER_INTERCEPT_PCLOSE
9191INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9192  void *ctx;
9193  COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9194  COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9195  const FileMetadata *m = GetInterceptorMetadata(fp);
9196  int res = REAL(pclose)(fp);
9197  if (m) {
9198    COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9199    DeleteInterceptorMetadata(fp);
9200  }
9201  return res;
9202}
9203#define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9204#else
9205#define INIT_PCLOSE
9206#endif
9207
9208#if SANITIZER_INTERCEPT_FUNOPEN
9209typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9210typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9211typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9212typedef int (*funopen_closefn)(void *cookie);
9213
9214struct WrappedFunopenCookie {
9215  void *real_cookie;
9216  funopen_readfn real_read;
9217  funopen_writefn real_write;
9218  funopen_seekfn real_seek;
9219  funopen_closefn real_close;
9220};
9221
9222static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9223  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9224  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9225  funopen_readfn real_read = wrapped_cookie->real_read;
9226  return real_read(wrapped_cookie->real_cookie, buf, len);
9227}
9228
9229static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9230  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9231  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9232  funopen_writefn real_write = wrapped_cookie->real_write;
9233  return real_write(wrapped_cookie->real_cookie, buf, len);
9234}
9235
9236static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9237  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9238  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9239  funopen_seekfn real_seek = wrapped_cookie->real_seek;
9240  return real_seek(wrapped_cookie->real_cookie, offset, whence);
9241}
9242
9243static int wrapped_funopen_close(void *cookie) {
9244  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9245  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9246  funopen_closefn real_close = wrapped_cookie->real_close;
9247  int res = real_close(wrapped_cookie->real_cookie);
9248  InternalFree(wrapped_cookie);
9249  return res;
9250}
9251
9252INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9253            funopen_writefn writefn, funopen_seekfn seekfn,
9254            funopen_closefn closefn) {
9255  void *ctx;
9256  COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9257                           closefn);
9258
9259  WrappedFunopenCookie *wrapped_cookie =
9260      (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9261  wrapped_cookie->real_cookie = cookie;
9262  wrapped_cookie->real_read = readfn;
9263  wrapped_cookie->real_write = writefn;
9264  wrapped_cookie->real_seek = seekfn;
9265  wrapped_cookie->real_close = closefn;
9266
9267  __sanitizer_FILE *res =
9268      REAL(funopen)(wrapped_cookie,
9269                    readfn  ? wrapped_funopen_read  : nullptr,
9270                    writefn ? wrapped_funopen_write : nullptr,
9271                    seekfn  ? wrapped_funopen_seek  : nullptr,
9272                    closefn ? wrapped_funopen_close : nullptr);
9273  if (res)
9274    unpoison_file(res);
9275  return res;
9276}
9277#define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9278#else
9279#define INIT_FUNOPEN
9280#endif
9281
9282#if SANITIZER_INTERCEPT_FUNOPEN2
9283typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9284typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9285typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9286typedef int (*funopen2_flushfn)(void *cookie);
9287typedef int (*funopen2_closefn)(void *cookie);
9288
9289struct WrappedFunopen2Cookie {
9290  void *real_cookie;
9291  funopen2_readfn real_read;
9292  funopen2_writefn real_write;
9293  funopen2_seekfn real_seek;
9294  funopen2_flushfn real_flush;
9295  funopen2_closefn real_close;
9296};
9297
9298static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9299  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9300  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9301  funopen2_readfn real_read = wrapped_cookie->real_read;
9302  return real_read(wrapped_cookie->real_cookie, buf, len);
9303}
9304
9305static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9306                                      SIZE_T len) {
9307  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9308  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9309  funopen2_writefn real_write = wrapped_cookie->real_write;
9310  return real_write(wrapped_cookie->real_cookie, buf, len);
9311}
9312
9313static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9314  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9315  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9316  funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9317  return real_seek(wrapped_cookie->real_cookie, offset, whence);
9318}
9319
9320static int wrapped_funopen2_flush(void *cookie) {
9321  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9322  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9323  funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9324  return real_flush(wrapped_cookie->real_cookie);
9325}
9326
9327static int wrapped_funopen2_close(void *cookie) {
9328  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9329  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9330  funopen2_closefn real_close = wrapped_cookie->real_close;
9331  int res = real_close(wrapped_cookie->real_cookie);
9332  InternalFree(wrapped_cookie);
9333  return res;
9334}
9335
9336INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9337            funopen2_writefn writefn, funopen2_seekfn seekfn,
9338            funopen2_flushfn flushfn, funopen2_closefn closefn) {
9339  void *ctx;
9340  COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9341                           flushfn, closefn);
9342
9343  WrappedFunopen2Cookie *wrapped_cookie =
9344      (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9345  wrapped_cookie->real_cookie = cookie;
9346  wrapped_cookie->real_read = readfn;
9347  wrapped_cookie->real_write = writefn;
9348  wrapped_cookie->real_seek = seekfn;
9349  wrapped_cookie->real_flush = flushfn;
9350  wrapped_cookie->real_close = closefn;
9351
9352  __sanitizer_FILE *res =
9353      REAL(funopen2)(wrapped_cookie,
9354                     readfn  ? wrapped_funopen2_read  : nullptr,
9355                     writefn ? wrapped_funopen2_write : nullptr,
9356                     seekfn  ? wrapped_funopen2_seek  : nullptr,
9357                     flushfn ? wrapped_funopen2_flush : nullptr,
9358                     closefn ? wrapped_funopen2_close : nullptr);
9359  if (res)
9360    unpoison_file(res);
9361  return res;
9362}
9363#define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9364#else
9365#define INIT_FUNOPEN2
9366#endif
9367
9368#if SANITIZER_INTERCEPT_FDEVNAME
9369INTERCEPTOR(char *, fdevname,  int fd) {
9370  void *ctx;
9371  COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9372  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9373  char *name = REAL(fdevname)(fd);
9374  if (name) {
9375    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
9376    if (fd > 0)
9377      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9378  }
9379  return name;
9380}
9381
9382INTERCEPTOR(char *, fdevname_r,  int fd, char *buf, SIZE_T len) {
9383  void *ctx;
9384  COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9385  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9386  char *name = REAL(fdevname_r)(fd, buf, len);
9387  if (name && buf && len > 0) {
9388    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
9389    if (fd > 0)
9390      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9391  }
9392  return name;
9393}
9394
9395#define INIT_FDEVNAME \
9396  COMMON_INTERCEPT_FUNCTION(fdevname); \
9397  COMMON_INTERCEPT_FUNCTION(fdevname_r);
9398#else
9399#define INIT_FDEVNAME
9400#endif
9401
9402#if SANITIZER_INTERCEPT_GETUSERSHELL
9403INTERCEPTOR(char *, getusershell) {
9404  void *ctx;
9405  COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9406  char *res = REAL(getusershell)();
9407  if (res)
9408    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9409  return res;
9410}
9411
9412#define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9413#else
9414#define INIT_GETUSERSHELL
9415#endif
9416
9417#if SANITIZER_INTERCEPT_SL_INIT
9418INTERCEPTOR(void *, sl_init) {
9419  void *ctx;
9420  COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9421  void *res = REAL(sl_init)();
9422  if (res)
9423    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9424  return res;
9425}
9426
9427INTERCEPTOR(int, sl_add, void *sl, char *item) {
9428  void *ctx;
9429  COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9430  if (sl)
9431    COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9432  if (item)
9433    COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9434  int res = REAL(sl_add)(sl, item);
9435  if (!res)
9436    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9437  return res;
9438}
9439
9440INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9441  void *ctx;
9442  COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9443  if (sl)
9444    COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9445  if (item)
9446    COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9447  char *res = REAL(sl_find)(sl, item);
9448  if (res)
9449    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9450  return res;
9451}
9452
9453INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9454  void *ctx;
9455  COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9456  if (sl)
9457    COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9458  REAL(sl_free)(sl, freeall);
9459}
9460
9461#define INIT_SL_INIT                  \
9462  COMMON_INTERCEPT_FUNCTION(sl_init); \
9463  COMMON_INTERCEPT_FUNCTION(sl_add);  \
9464  COMMON_INTERCEPT_FUNCTION(sl_find); \
9465  COMMON_INTERCEPT_FUNCTION(sl_free);
9466#else
9467#define INIT_SL_INIT
9468#endif
9469
9470static void InitializeCommonInterceptors() {
9471  static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
9472  interceptor_metadata_map =
9473      new ((void *)&metadata_mem) MetadataHashMap();  // NOLINT
9474
9475  INIT_MMAP;
9476  INIT_MMAP64;
9477  INIT_TEXTDOMAIN;
9478  INIT_STRLEN;
9479  INIT_STRNLEN;
9480  INIT_STRNDUP;
9481  INIT___STRNDUP;
9482  INIT_STRCMP;
9483  INIT_STRNCMP;
9484  INIT_STRCASECMP;
9485  INIT_STRNCASECMP;
9486  INIT_STRSTR;
9487  INIT_STRCASESTR;
9488  INIT_STRCHR;
9489  INIT_STRCHRNUL;
9490  INIT_STRRCHR;
9491  INIT_STRSPN;
9492  INIT_STRTOK;
9493  INIT_STRPBRK;
9494  INIT_STRXFRM;
9495  INIT___STRXFRM_L;
9496  INIT_MEMSET;
9497  INIT_MEMMOVE;
9498  INIT_MEMCPY;
9499  INIT_MEMCHR;
9500  INIT_MEMCMP;
9501  INIT_MEMRCHR;
9502  INIT_MEMMEM;
9503  INIT_READ;
9504  INIT_FREAD;
9505  INIT_PREAD;
9506  INIT_PREAD64;
9507  INIT_READV;
9508  INIT_PREADV;
9509  INIT_PREADV64;
9510  INIT_WRITE;
9511  INIT_FWRITE;
9512  INIT_PWRITE;
9513  INIT_PWRITE64;
9514  INIT_WRITEV;
9515  INIT_PWRITEV;
9516  INIT_PWRITEV64;
9517  INIT_FGETS;
9518  INIT_FPUTS;
9519  INIT_PUTS;
9520  INIT_PRCTL;
9521  INIT_LOCALTIME_AND_FRIENDS;
9522  INIT_STRPTIME;
9523  INIT_SCANF;
9524  INIT_ISOC99_SCANF;
9525  INIT_PRINTF;
9526  INIT_PRINTF_L;
9527  INIT_ISOC99_PRINTF;
9528  INIT_FREXP;
9529  INIT_FREXPF_FREXPL;
9530  INIT_GETPWNAM_AND_FRIENDS;
9531  INIT_GETPWNAM_R_AND_FRIENDS;
9532  INIT_GETPWENT;
9533  INIT_FGETPWENT;
9534  INIT_GETPWENT_R;
9535  INIT_FGETPWENT_R;
9536  INIT_FGETGRENT_R;
9537  INIT_SETPWENT;
9538  INIT_CLOCK_GETTIME;
9539  INIT_GETITIMER;
9540  INIT_TIME;
9541  INIT_GLOB;
9542  INIT_GLOB64;
9543  INIT_WAIT;
9544  INIT_WAIT4;
9545  INIT_INET;
9546  INIT_PTHREAD_GETSCHEDPARAM;
9547  INIT_GETADDRINFO;
9548  INIT_GETNAMEINFO;
9549  INIT_GETSOCKNAME;
9550  INIT_GETHOSTBYNAME;
9551  INIT_GETHOSTBYNAME2;
9552  INIT_GETHOSTBYNAME_R;
9553  INIT_GETHOSTBYNAME2_R;
9554  INIT_GETHOSTBYADDR_R;
9555  INIT_GETHOSTENT_R;
9556  INIT_GETSOCKOPT;
9557  INIT_ACCEPT;
9558  INIT_ACCEPT4;
9559  INIT_PACCEPT;
9560  INIT_MODF;
9561  INIT_RECVMSG;
9562  INIT_SENDMSG;
9563  INIT_RECVMMSG;
9564  INIT_SENDMMSG;
9565  INIT_GETPEERNAME;
9566  INIT_IOCTL;
9567  INIT_INET_ATON;
9568  INIT_SYSINFO;
9569  INIT_READDIR;
9570  INIT_READDIR64;
9571  INIT_PTRACE;
9572  INIT_SETLOCALE;
9573  INIT_GETCWD;
9574  INIT_GET_CURRENT_DIR_NAME;
9575  INIT_STRTOIMAX;
9576  INIT_MBSTOWCS;
9577  INIT_MBSNRTOWCS;
9578  INIT_WCSTOMBS;
9579  INIT_WCSNRTOMBS;
9580  INIT_WCRTOMB;
9581  INIT_TCGETATTR;
9582  INIT_REALPATH;
9583  INIT_CANONICALIZE_FILE_NAME;
9584  INIT_CONFSTR;
9585  INIT_SCHED_GETAFFINITY;
9586  INIT_SCHED_GETPARAM;
9587  INIT_STRERROR;
9588  INIT_STRERROR_R;
9589  INIT_XPG_STRERROR_R;
9590  INIT_SCANDIR;
9591  INIT_SCANDIR64;
9592  INIT_GETGROUPS;
9593  INIT_POLL;
9594  INIT_PPOLL;
9595  INIT_WORDEXP;
9596  INIT_SIGWAIT;
9597  INIT_SIGWAITINFO;
9598  INIT_SIGTIMEDWAIT;
9599  INIT_SIGSETOPS;
9600  INIT_SIGPENDING;
9601  INIT_SIGPROCMASK;
9602  INIT_BACKTRACE;
9603  INIT__EXIT;
9604  INIT_PTHREAD_MUTEX_LOCK;
9605  INIT_PTHREAD_MUTEX_UNLOCK;
9606  INIT___PTHREAD_MUTEX_LOCK;
9607  INIT___PTHREAD_MUTEX_UNLOCK;
9608  INIT___LIBC_MUTEX_LOCK;
9609  INIT___LIBC_MUTEX_UNLOCK;
9610  INIT___LIBC_THR_SETCANCELSTATE;
9611  INIT_GETMNTENT;
9612  INIT_GETMNTENT_R;
9613  INIT_STATFS;
9614  INIT_STATFS64;
9615  INIT_STATVFS;
9616  INIT_STATVFS64;
9617  INIT_INITGROUPS;
9618  INIT_ETHER_NTOA_ATON;
9619  INIT_ETHER_HOST;
9620  INIT_ETHER_R;
9621  INIT_SHMCTL;
9622  INIT_RANDOM_R;
9623  INIT_PTHREAD_ATTR_GET;
9624  INIT_PTHREAD_ATTR_GET_SCHED;
9625  INIT_PTHREAD_ATTR_GETINHERITSCHED;
9626  INIT_PTHREAD_ATTR_GETAFFINITY_NP;
9627  INIT_PTHREAD_MUTEXATTR_GETPSHARED;
9628  INIT_PTHREAD_MUTEXATTR_GETTYPE;
9629  INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
9630  INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
9631  INIT_PTHREAD_MUTEXATTR_GETROBUST;
9632  INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
9633  INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
9634  INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
9635  INIT_PTHREAD_CONDATTR_GETPSHARED;
9636  INIT_PTHREAD_CONDATTR_GETCLOCK;
9637  INIT_PTHREAD_BARRIERATTR_GETPSHARED;
9638  INIT_TMPNAM;
9639  INIT_TMPNAM_R;
9640  INIT_TTYNAME_R;
9641  INIT_TEMPNAM;
9642  INIT_PTHREAD_SETNAME_NP;
9643  INIT_PTHREAD_GETNAME_NP;
9644  INIT_SINCOS;
9645  INIT_REMQUO;
9646  INIT_REMQUOL;
9647  INIT_LGAMMA;
9648  INIT_LGAMMAL;
9649  INIT_LGAMMA_R;
9650  INIT_LGAMMAL_R;
9651  INIT_DRAND48_R;
9652  INIT_RAND_R;
9653  INIT_GETLINE;
9654  INIT_ICONV;
9655  INIT_TIMES;
9656  INIT_TLS_GET_ADDR;
9657  INIT_LISTXATTR;
9658  INIT_GETXATTR;
9659  INIT_GETRESID;
9660  INIT_GETIFADDRS;
9661  INIT_IF_INDEXTONAME;
9662  INIT_CAPGET;
9663  INIT_AEABI_MEM;
9664  INIT___BZERO;
9665  INIT_FTIME;
9666  INIT_XDR;
9667  INIT_TSEARCH;
9668  INIT_LIBIO_INTERNALS;
9669  INIT_FOPEN;
9670  INIT_FOPEN64;
9671  INIT_OPEN_MEMSTREAM;
9672  INIT_OBSTACK;
9673  INIT_FFLUSH;
9674  INIT_FCLOSE;
9675  INIT_DLOPEN_DLCLOSE;
9676  INIT_GETPASS;
9677  INIT_TIMERFD;
9678  INIT_MLOCKX;
9679  INIT_FOPENCOOKIE;
9680  INIT_SEM;
9681  INIT_PTHREAD_SETCANCEL;
9682  INIT_MINCORE;
9683  INIT_PROCESS_VM_READV;
9684  INIT_CTERMID;
9685  INIT_CTERMID_R;
9686  INIT_RECV_RECVFROM;
9687  INIT_SEND_SENDTO;
9688  INIT_STAT;
9689  INIT_EVENTFD_READ_WRITE;
9690  INIT_LSTAT;
9691  INIT___XSTAT;
9692  INIT___XSTAT64;
9693  INIT___LXSTAT;
9694  INIT___LXSTAT64;
9695  // FIXME: add other *stat interceptors.
9696  INIT_UTMP;
9697  INIT_UTMPX;
9698  INIT_GETLOADAVG;
9699  INIT_WCSLEN;
9700  INIT_WCSCAT;
9701  INIT_WCSXFRM;
9702  INIT___WCSXFRM_L;
9703  INIT_ACCT;
9704  INIT_USER_FROM_UID;
9705  INIT_UID_FROM_USER;
9706  INIT_GROUP_FROM_GID;
9707  INIT_GID_FROM_GROUP;
9708  INIT_ACCESS;
9709  INIT_FACCESSAT;
9710  INIT_GETGROUPLIST;
9711  INIT_GETGROUPMEMBERSHIP;
9712  INIT_READLINK;
9713  INIT_READLINKAT;
9714  INIT_NAME_TO_HANDLE_AT;
9715  INIT_OPEN_BY_HANDLE_AT;
9716  INIT_STRLCPY;
9717  INIT_DEVNAME;
9718  INIT_DEVNAME_R;
9719  INIT_FGETLN;
9720  INIT_STRMODE;
9721  INIT_TTYENT;
9722  INIT_PROTOENT;
9723  INIT_NETENT;
9724  INIT_GETMNTINFO;
9725  INIT_MI_VECTOR_HASH;
9726  INIT_SETVBUF;
9727  INIT_GETVFSSTAT;
9728  INIT_REGEX;
9729  INIT_REGEXSUB;
9730  INIT_FTS;
9731  INIT_SYSCTL;
9732  INIT_ASYSCTL;
9733  INIT_SYSCTLGETMIBINFO;
9734  INIT_NL_LANGINFO;
9735  INIT_MODCTL;
9736  INIT_STRTONUM;
9737  INIT_FPARSELN;
9738  INIT_STATVFS1;
9739  INIT_STRTOI;
9740  INIT_CAPSICUM;
9741  INIT_SHA1;
9742  INIT_MD4;
9743  INIT_RMD160;
9744  INIT_MD5;
9745  INIT_FSEEK;
9746  INIT_MD2;
9747  INIT_SHA2;
9748  INIT_VIS;
9749  INIT_CDB;
9750  INIT_GETFSENT;
9751  INIT_ARC4RANDOM;
9752  INIT_POPEN;
9753  INIT_POPENVE;
9754  INIT_PCLOSE;
9755  INIT_FUNOPEN;
9756  INIT_FUNOPEN2;
9757  INIT_FDEVNAME;
9758  INIT_GETUSERSHELL;
9759  INIT_SL_INIT;
9760
9761  INIT___PRINTF_CHK;
9762}
9763