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