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