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