]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_common_interceptors.inc
1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Common function interceptors for tools like AddressSanitizer,
11 // ThreadSanitizer, MemorySanitizer, etc.
12 //
13 // This file should be included into the tool's interceptor file,
14 // which has to define its own macros:
15 //   COMMON_INTERCEPTOR_ENTER
16 //   COMMON_INTERCEPTOR_ENTER_NOIGNORE
17 //   COMMON_INTERCEPTOR_READ_RANGE
18 //   COMMON_INTERCEPTOR_WRITE_RANGE
19 //   COMMON_INTERCEPTOR_INITIALIZE_RANGE
20 //   COMMON_INTERCEPTOR_DIR_ACQUIRE
21 //   COMMON_INTERCEPTOR_FD_ACQUIRE
22 //   COMMON_INTERCEPTOR_FD_RELEASE
23 //   COMMON_INTERCEPTOR_FD_ACCESS
24 //   COMMON_INTERCEPTOR_SET_THREAD_NAME
25 //   COMMON_INTERCEPTOR_ON_DLOPEN
26 //   COMMON_INTERCEPTOR_ON_EXIT
27 //   COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
28 //   COMMON_INTERCEPTOR_MUTEX_POST_LOCK
29 //   COMMON_INTERCEPTOR_MUTEX_UNLOCK
30 //   COMMON_INTERCEPTOR_MUTEX_REPAIR
31 //   COMMON_INTERCEPTOR_SET_PTHREAD_NAME
32 //   COMMON_INTERCEPTOR_HANDLE_RECVMSG
33 //   COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
34 //   COMMON_INTERCEPTOR_MEMSET_IMPL
35 //   COMMON_INTERCEPTOR_MEMMOVE_IMPL
36 //   COMMON_INTERCEPTOR_MEMCPY_IMPL
37 //   COMMON_INTERCEPTOR_MMAP_IMPL
38 //   COMMON_INTERCEPTOR_COPY_STRING
39 //   COMMON_INTERCEPTOR_STRNDUP_IMPL
40 //===----------------------------------------------------------------------===//
41
42 #include "interception/interception.h"
43 #include "sanitizer_addrhashmap.h"
44 #include "sanitizer_errno.h"
45 #include "sanitizer_placement_new.h"
46 #include "sanitizer_platform_interceptors.h"
47 #include "sanitizer_symbolizer.h"
48 #include "sanitizer_tls_get_addr.h"
49
50 #include <stdarg.h>
51
52 #if SANITIZER_INTERCEPTOR_HOOKS
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
54 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
55   SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
56 #else
57 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
58 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
59
60 #endif  // SANITIZER_INTERCEPTOR_HOOKS
61
62 #if SANITIZER_WINDOWS && !defined(va_copy)
63 #define va_copy(dst, src) ((dst) = (src))
64 #endif // _WIN32
65
66 #if SANITIZER_FREEBSD
67 #define pthread_setname_np pthread_set_name_np
68 #define inet_aton __inet_aton
69 #define inet_pton __inet_pton
70 #define iconv __bsd_iconv
71 #endif
72
73 #if SANITIZER_NETBSD
74 #define clock_getres __clock_getres50
75 #define clock_gettime __clock_gettime50
76 #define clock_settime __clock_settime50
77 #define ctime __ctime50
78 #define ctime_r __ctime_r50
79 #define devname __devname50
80 #define getitimer __getitimer50
81 #define getpwent __getpwent50
82 #define getpwnam __getpwnam50
83 #define getpwnam_r __getpwnam_r50
84 #define getpwuid __getpwuid50
85 #define getpwuid_r __getpwuid_r50
86 #define getutent __getutent50
87 #define getutxent __getutxent50
88 #define getutxid __getutxid50
89 #define getutxline __getutxline50
90 #define glob __glob30
91 #define gmtime __gmtime50
92 #define gmtime_r __gmtime_r50
93 #define localtime __locatime50
94 #define localtime_r __localtime_r50
95 #define mktime __mktime50
96 #define lstat __lstat50
97 #define opendir __opendir30
98 #define readdir __readdir30
99 #define readdir_r __readdir_r30
100 #define scandir __scandir30
101 #define setitimer __setitimer50
102 #define setlocale __setlocale50
103 #define shmctl __shmctl50
104 #define sigemptyset __sigemptyset14
105 #define sigfillset __sigfillset14
106 #define sigpending __sigpending14
107 #define sigprocmask __sigprocmask14
108 #define sigtimedwait __sigtimedwait50
109 #define stat __stat50
110 #define time __time50
111 #define times __times13
112 #define wait3 __wait350
113 #define wait4 __wait450
114 extern const unsigned short *_ctype_tab_;
115 extern const short *_toupper_tab_;
116 extern const short *_tolower_tab_;
117 #endif
118
119 // Platform-specific options.
120 #if SANITIZER_MAC
121 namespace __sanitizer {
122 bool PlatformHasDifferentMemcpyAndMemmove();
123 }
124 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
125   (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
126 #elif SANITIZER_WINDOWS64
127 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
128 #else
129 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
130 #endif  // SANITIZER_MAC
131
132 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
133 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
134 #endif
135
136 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
137 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
138 #endif
139
140 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
141 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
142 #endif
143
144 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
145 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
146 #endif
147
148 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
149 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
150 #endif
151
152 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
153 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
154 #endif
155
156 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
157 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
158 #endif
159
160 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
161 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
162 #endif
163
164 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
165 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
166 #endif
167
168 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
169 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
170 #endif
171
172 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
173 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
174 #endif
175
176 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
177 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
178 #endif
179
180 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
181 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
182 #endif
183
184 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
185 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
186   COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
187 #endif
188
189 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
190 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
191 #endif
192
193 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n)                   \
194     COMMON_INTERCEPTOR_READ_RANGE((ctx), (s),                       \
195       common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
196
197 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
198 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
199   CheckNoDeepBind(filename, flag);
200 #endif
201
202 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
203 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
204 #endif
205
206 #ifndef COMMON_INTERCEPTOR_ACQUIRE
207 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
208 #endif
209
210 #ifndef COMMON_INTERCEPTOR_RELEASE
211 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
212 #endif
213
214 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
215 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
216 #endif
217
218 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
219 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
220 #endif
221
222 #ifdef SANITIZER_NLDBL_VERSION
223 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
224     COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
225 #else
226 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
227     COMMON_INTERCEPT_FUNCTION(fn)
228 #endif
229
230 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
231 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
232   {                                                       \
233     if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)        \
234       return internal_memset(dst, v, size);               \
235     COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size);  \
236     if (common_flags()->intercept_intrin)                 \
237       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);     \
238     return REAL(memset)(dst, v, size);                    \
239   }
240 #endif
241
242 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
243 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
244   {                                                          \
245     if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)           \
246       return internal_memmove(dst, src, size);               \
247     COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size);  \
248     if (common_flags()->intercept_intrin) {                  \
249       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);        \
250       COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);         \
251     }                                                        \
252     return REAL(memmove)(dst, src, size);                    \
253   }
254 #endif
255
256 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
257 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
258   {                                                         \
259     if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {        \
260       return internal_memmove(dst, src, size);              \
261     }                                                       \
262     COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size);  \
263     if (common_flags()->intercept_intrin) {                 \
264       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);       \
265       COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);        \
266     }                                                       \
267     return REAL(memcpy)(dst, src, size);                    \
268   }
269 #endif
270
271 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
272 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
273                                      off)                                  \
274   { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
275 #endif
276
277 #ifndef COMMON_INTERCEPTOR_COPY_STRING
278 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
279 #endif
280
281 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
282 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size)                         \
283   COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size);                            \
284   uptr copy_length = internal_strnlen(s, size);                               \
285   char *new_mem = (char *)WRAP(malloc)(copy_length + 1);                      \
286   if (common_flags()->intercept_strndup) {                                    \
287     COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1));       \
288   }                                                                           \
289   COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length);               \
290   internal_memcpy(new_mem, s, copy_length);                                   \
291   new_mem[copy_length] = '\0';                                                \
292   return new_mem;
293 #endif
294
295 struct FileMetadata {
296   // For open_memstream().
297   char **addr;
298   SIZE_T *size;
299 };
300
301 struct CommonInterceptorMetadata {
302   enum {
303     CIMT_INVALID = 0,
304     CIMT_FILE
305   } type;
306   union {
307     FileMetadata file;
308   };
309 };
310
311 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
312
313 static MetadataHashMap *interceptor_metadata_map;
314
315 #if SI_POSIX
316 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
317                                           const FileMetadata &file) {
318   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
319   CHECK(h.created());
320   h->type = CommonInterceptorMetadata::CIMT_FILE;
321   h->file = file;
322 }
323
324 UNUSED static const FileMetadata *GetInterceptorMetadata(
325     __sanitizer_FILE *addr) {
326   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
327                             /* remove */ false,
328                             /* create */ false);
329   if (addr && h.exists()) {
330     CHECK(!h.created());
331     CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
332     return &h->file;
333   } else {
334     return 0;
335   }
336 }
337
338 UNUSED static void DeleteInterceptorMetadata(void *addr) {
339   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
340   CHECK(h.exists());
341 }
342 #endif  // SI_POSIX
343
344 #if SANITIZER_INTERCEPT_STRLEN
345 INTERCEPTOR(SIZE_T, strlen, const char *s) {
346   // Sometimes strlen is called prior to InitializeCommonInterceptors,
347   // in which case the REAL(strlen) typically used in
348   // COMMON_INTERCEPTOR_ENTER will fail.  We use internal_strlen here
349   // to handle that.
350   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
351     return internal_strlen(s);
352   void *ctx;
353   COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
354   SIZE_T result = REAL(strlen)(s);
355   if (common_flags()->intercept_strlen)
356     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
357   return result;
358 }
359 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
360 #else
361 #define INIT_STRLEN
362 #endif
363
364 #if SANITIZER_INTERCEPT_STRNLEN
365 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
366   void *ctx;
367   COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
368   SIZE_T length = REAL(strnlen)(s, maxlen);
369   if (common_flags()->intercept_strlen)
370     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
371   return length;
372 }
373 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
374 #else
375 #define INIT_STRNLEN
376 #endif
377
378 #if SANITIZER_INTERCEPT_STRNDUP
379 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
380   void *ctx;
381   COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
382 }
383 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
384 #else
385 #define INIT_STRNDUP
386 #endif // SANITIZER_INTERCEPT_STRNDUP
387
388 #if SANITIZER_INTERCEPT___STRNDUP
389 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
390   void *ctx;
391   COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
392 }
393 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
394 #else
395 #define INIT___STRNDUP
396 #endif // SANITIZER_INTERCEPT___STRNDUP
397
398 #if SANITIZER_INTERCEPT_TEXTDOMAIN
399 INTERCEPTOR(char*, textdomain, const char *domainname) {
400   void *ctx;
401   COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
402   if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
403   char *domain = REAL(textdomain)(domainname);
404   if (domain) {
405     COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
406   }
407   return domain;
408 }
409 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
410 #else
411 #define INIT_TEXTDOMAIN
412 #endif
413
414 #if SANITIZER_INTERCEPT_STRCMP
415 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
416   return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
417 }
418
419 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
420                               const char *s1, const char *s2, int result)
421
422 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
423   void *ctx;
424   COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
425   unsigned char c1, c2;
426   uptr i;
427   for (i = 0;; i++) {
428     c1 = (unsigned char)s1[i];
429     c2 = (unsigned char)s2[i];
430     if (c1 != c2 || c1 == '\0') break;
431   }
432   COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
433   COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
434   int result = CharCmpX(c1, c2);
435   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
436                              s2, result);
437   return result;
438 }
439
440 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
441                               const char *s1, const char *s2, uptr n,
442                               int result)
443
444 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
445   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
446     return internal_strncmp(s1, s2, size);
447   void *ctx;
448   COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
449   unsigned char c1 = 0, c2 = 0;
450   uptr i;
451   for (i = 0; i < size; i++) {
452     c1 = (unsigned char)s1[i];
453     c2 = (unsigned char)s2[i];
454     if (c1 != c2 || c1 == '\0') break;
455   }
456   uptr i1 = i;
457   uptr i2 = i;
458   if (common_flags()->strict_string_checks) {
459     for (; i1 < size && s1[i1]; i1++) {}
460     for (; i2 < size && s2[i2]; i2++) {}
461   }
462   COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
463   COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
464   int result = CharCmpX(c1, c2);
465   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
466                              s2, size, result);
467   return result;
468 }
469
470 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
471 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
472 #else
473 #define INIT_STRCMP
474 #define INIT_STRNCMP
475 #endif
476
477 #if SANITIZER_INTERCEPT_STRCASECMP
478 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
479   int c1_low = ToLower(c1);
480   int c2_low = ToLower(c2);
481   return c1_low - c2_low;
482 }
483
484 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
485                               const char *s1, const char *s2, int result)
486
487 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
488   void *ctx;
489   COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
490   unsigned char c1 = 0, c2 = 0;
491   uptr i;
492   for (i = 0;; i++) {
493     c1 = (unsigned char)s1[i];
494     c2 = (unsigned char)s2[i];
495     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
496   }
497   COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
498   COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
499   int result = CharCaseCmp(c1, c2);
500   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
501                              s1, s2, result);
502   return result;
503 }
504
505 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
506                               const char *s1, const char *s2, uptr size,
507                               int result)
508
509 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
510   void *ctx;
511   COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
512   unsigned char c1 = 0, c2 = 0;
513   uptr i;
514   for (i = 0; i < size; i++) {
515     c1 = (unsigned char)s1[i];
516     c2 = (unsigned char)s2[i];
517     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
518   }
519   uptr i1 = i;
520   uptr i2 = i;
521   if (common_flags()->strict_string_checks) {
522     for (; i1 < size && s1[i1]; i1++) {}
523     for (; i2 < size && s2[i2]; i2++) {}
524   }
525   COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
526   COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
527   int result = CharCaseCmp(c1, c2);
528   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
529                              s1, s2, size, result);
530   return result;
531 }
532
533 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
534 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
535 #else
536 #define INIT_STRCASECMP
537 #define INIT_STRNCASECMP
538 #endif
539
540 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
541 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
542                                const char *s2) {
543     uptr len1 = REAL(strlen)(s1);
544     uptr len2 = REAL(strlen)(s2);
545     COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
546     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
547 }
548 #endif
549
550 #if SANITIZER_INTERCEPT_STRSTR
551
552 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
553                               const char *s1, const char *s2, char *result)
554
555 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
556   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
557     return internal_strstr(s1, s2);
558   void *ctx;
559   COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
560   char *r = REAL(strstr)(s1, s2);
561   if (common_flags()->intercept_strstr)
562     StrstrCheck(ctx, r, s1, s2);
563   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
564                              s2, r);
565   return r;
566 }
567
568 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
569 #else
570 #define INIT_STRSTR
571 #endif
572
573 #if SANITIZER_INTERCEPT_STRCASESTR
574
575 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
576                               const char *s1, const char *s2, char *result)
577
578 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
579   void *ctx;
580   COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
581   char *r = REAL(strcasestr)(s1, s2);
582   if (common_flags()->intercept_strstr)
583     StrstrCheck(ctx, r, s1, s2);
584   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
585                              s1, s2, r);
586   return r;
587 }
588
589 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
590 #else
591 #define INIT_STRCASESTR
592 #endif
593
594 #if SANITIZER_INTERCEPT_STRTOK
595
596 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
597   void *ctx;
598   COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
599   if (!common_flags()->intercept_strtok) {
600     return REAL(strtok)(str, delimiters);
601   }
602   if (common_flags()->strict_string_checks) {
603     // If strict_string_checks is enabled, we check the whole first argument
604     // string on the first call (strtok saves this string in a static buffer
605     // for subsequent calls). We do not need to check strtok's result.
606     // As the delimiters can change, we check them every call.
607     if (str != nullptr) {
608       COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
609     }
610     COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
611                                   REAL(strlen)(delimiters) + 1);
612     return REAL(strtok)(str, delimiters);
613   } else {
614     // However, when strict_string_checks is disabled we cannot check the
615     // whole string on the first call. Instead, we check the result string
616     // which is guaranteed to be a NULL-terminated substring of the first
617     // argument. We also conservatively check one character of str and the
618     // delimiters.
619     if (str != nullptr) {
620       COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
621     }
622     COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
623     char *result = REAL(strtok)(str, delimiters);
624     if (result != nullptr) {
625       COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
626     } else if (str != nullptr) {
627       // No delimiter were found, it's safe to assume that the entire str was
628       // scanned.
629       COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
630     }
631     return result;
632   }
633 }
634
635 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
636 #else
637 #define INIT_STRTOK
638 #endif
639
640 #if SANITIZER_INTERCEPT_MEMMEM
641 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
642                               const void *s1, SIZE_T len1, const void *s2,
643                               SIZE_T len2, void *result)
644
645 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
646             SIZE_T len2) {
647   void *ctx;
648   COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
649   void *r = REAL(memmem)(s1, len1, s2, len2);
650   if (common_flags()->intercept_memmem) {
651     COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
652     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
653   }
654   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
655                              s1, len1, s2, len2, r);
656   return r;
657 }
658
659 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
660 #else
661 #define INIT_MEMMEM
662 #endif  // SANITIZER_INTERCEPT_MEMMEM
663
664 #if SANITIZER_INTERCEPT_STRCHR
665 INTERCEPTOR(char*, strchr, const char *s, int c) {
666   void *ctx;
667   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
668     return internal_strchr(s, c);
669   COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
670   char *result = REAL(strchr)(s, c);
671   if (common_flags()->intercept_strchr) {
672     // Keep strlen as macro argument, as macro may ignore it.
673     COMMON_INTERCEPTOR_READ_STRING(ctx, s,
674       (result ? result - s : REAL(strlen)(s)) + 1);
675   }
676   return result;
677 }
678 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
679 #else
680 #define INIT_STRCHR
681 #endif
682
683 #if SANITIZER_INTERCEPT_STRCHRNUL
684 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
685   void *ctx;
686   COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
687   char *result = REAL(strchrnul)(s, c);
688   uptr len = result - s + 1;
689   if (common_flags()->intercept_strchr)
690     COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
691   return result;
692 }
693 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
694 #else
695 #define INIT_STRCHRNUL
696 #endif
697
698 #if SANITIZER_INTERCEPT_STRRCHR
699 INTERCEPTOR(char*, strrchr, const char *s, int c) {
700   void *ctx;
701   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
702     return internal_strrchr(s, c);
703   COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
704   if (common_flags()->intercept_strchr)
705     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
706   return REAL(strrchr)(s, c);
707 }
708 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
709 #else
710 #define INIT_STRRCHR
711 #endif
712
713 #if SANITIZER_INTERCEPT_STRSPN
714 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
715   void *ctx;
716   COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
717   SIZE_T r = REAL(strspn)(s1, s2);
718   if (common_flags()->intercept_strspn) {
719     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
720     COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
721   }
722   return r;
723 }
724
725 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
726   void *ctx;
727   COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
728   SIZE_T r = REAL(strcspn)(s1, s2);
729   if (common_flags()->intercept_strspn) {
730     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
731     COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
732   }
733   return r;
734 }
735
736 #define INIT_STRSPN \
737   COMMON_INTERCEPT_FUNCTION(strspn); \
738   COMMON_INTERCEPT_FUNCTION(strcspn);
739 #else
740 #define INIT_STRSPN
741 #endif
742
743 #if SANITIZER_INTERCEPT_STRPBRK
744 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
745   void *ctx;
746   COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
747   char *r = REAL(strpbrk)(s1, s2);
748   if (common_flags()->intercept_strpbrk) {
749     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
750     COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
751         r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
752   }
753   return r;
754 }
755
756 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
757 #else
758 #define INIT_STRPBRK
759 #endif
760
761 #if SANITIZER_INTERCEPT_MEMSET
762 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
763   void *ctx;
764   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
765 }
766
767 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
768 #else
769 #define INIT_MEMSET
770 #endif
771
772 #if SANITIZER_INTERCEPT_MEMMOVE
773 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
774   void *ctx;
775   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
776 }
777
778 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
779 #else
780 #define INIT_MEMMOVE
781 #endif
782
783 #if SANITIZER_INTERCEPT_MEMCPY
784 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
785   // On OS X, calling internal_memcpy here will cause memory corruptions,
786   // because memcpy and memmove are actually aliases of the same
787   // implementation.  We need to use internal_memmove here.
788   // N.B.: If we switch this to internal_ we'll have to use internal_memmove
789   // due to memcpy being an alias of memmove on OS X.
790   void *ctx;
791   if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
792     COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
793   } else {
794     COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
795   }
796 }
797
798 #define INIT_MEMCPY                                  \
799   do {                                               \
800     if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
801       COMMON_INTERCEPT_FUNCTION(memcpy);             \
802     } else {                                         \
803       ASSIGN_REAL(memcpy, memmove);                  \
804     }                                                \
805     CHECK(REAL(memcpy));                             \
806   } while (false)
807
808 #else
809 #define INIT_MEMCPY
810 #endif
811
812 #if SANITIZER_INTERCEPT_MEMCMP
813
814 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
815                               const void *s1, const void *s2, uptr n,
816                               int result)
817
818 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
819   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
820     return internal_memcmp(a1, a2, size);
821   void *ctx;
822   COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
823   if (common_flags()->intercept_memcmp) {
824     if (common_flags()->strict_memcmp) {
825       // Check the entire regions even if the first bytes of the buffers are
826       // different.
827       COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
828       COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
829       // Fallthrough to REAL(memcmp) below.
830     } else {
831       unsigned char c1 = 0, c2 = 0;
832       const unsigned char *s1 = (const unsigned char*)a1;
833       const unsigned char *s2 = (const unsigned char*)a2;
834       uptr i;
835       for (i = 0; i < size; i++) {
836         c1 = s1[i];
837         c2 = s2[i];
838         if (c1 != c2) break;
839       }
840       COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
841       COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
842       int r = CharCmpX(c1, c2);
843       CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
844                                  a1, a2, size, r);
845       return r;
846     }
847   }
848   int result = REAL(memcmp(a1, a2, size));
849   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
850                              a2, size, result);
851   return result;
852 }
853
854 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
855 #else
856 #define INIT_MEMCMP
857 #endif
858
859 #if SANITIZER_INTERCEPT_MEMCHR
860 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
861   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
862     return internal_memchr(s, c, n);
863   void *ctx;
864   COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
865 #if SANITIZER_WINDOWS
866   void *res;
867   if (REAL(memchr)) {
868     res = REAL(memchr)(s, c, n);
869   } else {
870     res = internal_memchr(s, c, n);
871   }
872 #else
873   void *res = REAL(memchr)(s, c, n);
874 #endif
875   uptr len = res ? (char *)res - (const char *)s + 1 : n;
876   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
877   return res;
878 }
879
880 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
881 #else
882 #define INIT_MEMCHR
883 #endif
884
885 #if SANITIZER_INTERCEPT_MEMRCHR
886 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
887   void *ctx;
888   COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
889   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
890   return REAL(memrchr)(s, c, n);
891 }
892
893 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
894 #else
895 #define INIT_MEMRCHR
896 #endif
897
898 #if SANITIZER_INTERCEPT_FREXP
899 INTERCEPTOR(double, frexp, double x, int *exp) {
900   void *ctx;
901   COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
902   // Assuming frexp() always writes to |exp|.
903   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
904   double res = REAL(frexp)(x, exp);
905   return res;
906 }
907
908 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
909 #else
910 #define INIT_FREXP
911 #endif  // SANITIZER_INTERCEPT_FREXP
912
913 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
914 INTERCEPTOR(float, frexpf, float x, int *exp) {
915   void *ctx;
916   COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
917   // FIXME: under ASan the call below may write to freed memory and corrupt
918   // its metadata. See
919   // https://github.com/google/sanitizers/issues/321.
920   float res = REAL(frexpf)(x, exp);
921   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
922   return res;
923 }
924
925 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
926   void *ctx;
927   COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
928   // FIXME: under ASan the call below may write to freed memory and corrupt
929   // its metadata. See
930   // https://github.com/google/sanitizers/issues/321.
931   long double res = REAL(frexpl)(x, exp);
932   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
933   return res;
934 }
935
936 #define INIT_FREXPF_FREXPL           \
937   COMMON_INTERCEPT_FUNCTION(frexpf); \
938   COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
939 #else
940 #define INIT_FREXPF_FREXPL
941 #endif  // SANITIZER_INTERCEPT_FREXPF_FREXPL
942
943 #if SI_POSIX
944 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
945                         SIZE_T iovlen, SIZE_T maxlen) {
946   for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
947     SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
948     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
949     maxlen -= sz;
950   }
951 }
952
953 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
954                        SIZE_T iovlen, SIZE_T maxlen) {
955   COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
956   for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
957     SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
958     COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
959     maxlen -= sz;
960   }
961 }
962 #endif
963
964 #if SANITIZER_INTERCEPT_READ
965 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
966   void *ctx;
967   COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
968   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
969   // FIXME: under ASan the call below may write to freed memory and corrupt
970   // its metadata. See
971   // https://github.com/google/sanitizers/issues/321.
972   SSIZE_T res = REAL(read)(fd, ptr, count);
973   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
974   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
975   return res;
976 }
977 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
978 #else
979 #define INIT_READ
980 #endif
981
982 #if SANITIZER_INTERCEPT_FREAD
983 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
984   // libc file streams can call user-supplied functions, see fopencookie.
985   void *ctx;
986   COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
987   // FIXME: under ASan the call below may write to freed memory and corrupt
988   // its metadata. See
989   // https://github.com/google/sanitizers/issues/321.
990   SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
991   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
992   return res;
993 }
994 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
995 #else
996 #define INIT_FREAD
997 #endif
998
999 #if SANITIZER_INTERCEPT_PREAD
1000 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1001   void *ctx;
1002   COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1003   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1004   // FIXME: under ASan the call below may write to freed memory and corrupt
1005   // its metadata. See
1006   // https://github.com/google/sanitizers/issues/321.
1007   SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1008   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1009   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1010   return res;
1011 }
1012 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1013 #else
1014 #define INIT_PREAD
1015 #endif
1016
1017 #if SANITIZER_INTERCEPT_PREAD64
1018 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1019   void *ctx;
1020   COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1021   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1022   // FIXME: under ASan the call below may write to freed memory and corrupt
1023   // its metadata. See
1024   // https://github.com/google/sanitizers/issues/321.
1025   SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1026   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1027   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1028   return res;
1029 }
1030 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1031 #else
1032 #define INIT_PREAD64
1033 #endif
1034
1035 #if SANITIZER_INTERCEPT_READV
1036 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1037                         int iovcnt) {
1038   void *ctx;
1039   COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1040   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1041   SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1042   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1043   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1044   return res;
1045 }
1046 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1047 #else
1048 #define INIT_READV
1049 #endif
1050
1051 #if SANITIZER_INTERCEPT_PREADV
1052 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1053             OFF_T offset) {
1054   void *ctx;
1055   COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1056   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1057   SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1058   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1059   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1060   return res;
1061 }
1062 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1063 #else
1064 #define INIT_PREADV
1065 #endif
1066
1067 #if SANITIZER_INTERCEPT_PREADV64
1068 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1069             OFF64_T offset) {
1070   void *ctx;
1071   COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1072   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1073   SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1074   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1075   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1076   return res;
1077 }
1078 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1079 #else
1080 #define INIT_PREADV64
1081 #endif
1082
1083 #if SANITIZER_INTERCEPT_WRITE
1084 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1085   void *ctx;
1086   COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1087   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1088   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1089   SSIZE_T res = REAL(write)(fd, ptr, count);
1090   // FIXME: this check should be _before_ the call to REAL(write), not after
1091   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1092   return res;
1093 }
1094 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1095 #else
1096 #define INIT_WRITE
1097 #endif
1098
1099 #if SANITIZER_INTERCEPT_FWRITE
1100 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1101   // libc file streams can call user-supplied functions, see fopencookie.
1102   void *ctx;
1103   COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1104   SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1105   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1106   return res;
1107 }
1108 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1109 #else
1110 #define INIT_FWRITE
1111 #endif
1112
1113 #if SANITIZER_INTERCEPT_PWRITE
1114 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1115   void *ctx;
1116   COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1117   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1118   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1119   SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1120   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1121   return res;
1122 }
1123 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1124 #else
1125 #define INIT_PWRITE
1126 #endif
1127
1128 #if SANITIZER_INTERCEPT_PWRITE64
1129 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1130             OFF64_T offset) {
1131   void *ctx;
1132   COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1133   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1134   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1135   SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1136   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1137   return res;
1138 }
1139 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1140 #else
1141 #define INIT_PWRITE64
1142 #endif
1143
1144 #if SANITIZER_INTERCEPT_WRITEV
1145 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1146                         int iovcnt) {
1147   void *ctx;
1148   COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1149   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1150   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1151   SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1152   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1153   return res;
1154 }
1155 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1156 #else
1157 #define INIT_WRITEV
1158 #endif
1159
1160 #if SANITIZER_INTERCEPT_PWRITEV
1161 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1162             OFF_T offset) {
1163   void *ctx;
1164   COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1165   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1166   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1167   SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1168   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1169   return res;
1170 }
1171 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1172 #else
1173 #define INIT_PWRITEV
1174 #endif
1175
1176 #if SANITIZER_INTERCEPT_PWRITEV64
1177 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1178             OFF64_T offset) {
1179   void *ctx;
1180   COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1181   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1182   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1183   SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1184   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1185   return res;
1186 }
1187 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1188 #else
1189 #define INIT_PWRITEV64
1190 #endif
1191
1192 #if SANITIZER_INTERCEPT_FGETS
1193 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1194   // libc file streams can call user-supplied functions, see fopencookie.
1195   void *ctx;
1196   COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1197   // FIXME: under ASan the call below may write to freed memory and corrupt
1198   // its metadata. See
1199   // https://github.com/google/sanitizers/issues/321.
1200   char *res = REAL(fgets)(s, size, file);
1201   if (res)
1202     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
1203   return res;
1204 }
1205 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1206 #else
1207 #define INIT_FGETS
1208 #endif
1209
1210 #if SANITIZER_INTERCEPT_FPUTS
1211 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1212   // libc file streams can call user-supplied functions, see fopencookie.
1213   void *ctx;
1214   COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1215   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1216   return REAL(fputs)(s, file);
1217 }
1218 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1219 #else
1220 #define INIT_FPUTS
1221 #endif
1222
1223 #if SANITIZER_INTERCEPT_PUTS
1224 INTERCEPTOR(int, puts, char *s) {
1225   // libc file streams can call user-supplied functions, see fopencookie.
1226   void *ctx;
1227   COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1228   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1229   return REAL(puts)(s);
1230 }
1231 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1232 #else
1233 #define INIT_PUTS
1234 #endif
1235
1236 #if SANITIZER_INTERCEPT_PRCTL
1237 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1238             unsigned long arg3,                        // NOLINT
1239             unsigned long arg4, unsigned long arg5) {  // NOLINT
1240   void *ctx;
1241   COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1242   static const int PR_SET_NAME = 15;
1243   int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1244   if (option == PR_SET_NAME) {
1245     char buff[16];
1246     internal_strncpy(buff, (char *)arg2, 15);
1247     buff[15] = 0;
1248     COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1249   }
1250   return res;
1251 }
1252 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1253 #else
1254 #define INIT_PRCTL
1255 #endif  // SANITIZER_INTERCEPT_PRCTL
1256
1257 #if SANITIZER_INTERCEPT_TIME
1258 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1259   void *ctx;
1260   COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1261   unsigned long local_t;
1262   unsigned long res = REAL(time)(&local_t);
1263   if (t && res != (unsigned long)-1) {
1264     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1265     *t = local_t;
1266   }
1267   return res;
1268 }
1269 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1270 #else
1271 #define INIT_TIME
1272 #endif  // SANITIZER_INTERCEPT_TIME
1273
1274 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1275 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1276   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1277 #if !SANITIZER_SOLARIS
1278   if (tm->tm_zone) {
1279     // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1280     // can point to shared memory and tsan would report a data race.
1281     COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1282                                         REAL(strlen(tm->tm_zone)) + 1);
1283   }
1284 #endif
1285 }
1286 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1287   void *ctx;
1288   COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1289   __sanitizer_tm *res = REAL(localtime)(timep);
1290   if (res) {
1291     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1292     unpoison_tm(ctx, res);
1293   }
1294   return res;
1295 }
1296 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1297   void *ctx;
1298   COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1299   __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1300   if (res) {
1301     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1302     unpoison_tm(ctx, res);
1303   }
1304   return res;
1305 }
1306 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1307   void *ctx;
1308   COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1309   __sanitizer_tm *res = REAL(gmtime)(timep);
1310   if (res) {
1311     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1312     unpoison_tm(ctx, res);
1313   }
1314   return res;
1315 }
1316 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1317   void *ctx;
1318   COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1319   __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1320   if (res) {
1321     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1322     unpoison_tm(ctx, res);
1323   }
1324   return res;
1325 }
1326 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1327   void *ctx;
1328   COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1329   // FIXME: under ASan the call below may write to freed memory and corrupt
1330   // its metadata. See
1331   // https://github.com/google/sanitizers/issues/321.
1332   char *res = REAL(ctime)(timep);
1333   if (res) {
1334     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1335     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1336   }
1337   return res;
1338 }
1339 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1340   void *ctx;
1341   COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1342   // FIXME: under ASan the call below may write to freed memory and corrupt
1343   // its metadata. See
1344   // https://github.com/google/sanitizers/issues/321.
1345   char *res = REAL(ctime_r)(timep, result);
1346   if (res) {
1347     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1348     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1349   }
1350   return res;
1351 }
1352 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1353   void *ctx;
1354   COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1355   // FIXME: under ASan the call below may write to freed memory and corrupt
1356   // its metadata. See
1357   // https://github.com/google/sanitizers/issues/321.
1358   char *res = REAL(asctime)(tm);
1359   if (res) {
1360     COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1361     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1362   }
1363   return res;
1364 }
1365 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1366   void *ctx;
1367   COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1368   // FIXME: under ASan the call below may write to freed memory and corrupt
1369   // its metadata. See
1370   // https://github.com/google/sanitizers/issues/321.
1371   char *res = REAL(asctime_r)(tm, result);
1372   if (res) {
1373     COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1374     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1375   }
1376   return res;
1377 }
1378 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1379   void *ctx;
1380   COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1381   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1382   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1383   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1384   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1385   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1386   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1387   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1388   long res = REAL(mktime)(tm);
1389   if (res != -1) unpoison_tm(ctx, tm);
1390   return res;
1391 }
1392 #define INIT_LOCALTIME_AND_FRIENDS        \
1393   COMMON_INTERCEPT_FUNCTION(localtime);   \
1394   COMMON_INTERCEPT_FUNCTION(localtime_r); \
1395   COMMON_INTERCEPT_FUNCTION(gmtime);      \
1396   COMMON_INTERCEPT_FUNCTION(gmtime_r);    \
1397   COMMON_INTERCEPT_FUNCTION(ctime);       \
1398   COMMON_INTERCEPT_FUNCTION(ctime_r);     \
1399   COMMON_INTERCEPT_FUNCTION(asctime);     \
1400   COMMON_INTERCEPT_FUNCTION(asctime_r);   \
1401   COMMON_INTERCEPT_FUNCTION(mktime);
1402 #else
1403 #define INIT_LOCALTIME_AND_FRIENDS
1404 #endif  // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1405
1406 #if SANITIZER_INTERCEPT_STRPTIME
1407 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1408   void *ctx;
1409   COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1410   if (format)
1411     COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1412   // FIXME: under ASan the call below may write to freed memory and corrupt
1413   // its metadata. See
1414   // https://github.com/google/sanitizers/issues/321.
1415   char *res = REAL(strptime)(s, format, tm);
1416   COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1417   if (res && tm) {
1418     // Do not call unpoison_tm here, because strptime does not, in fact,
1419     // initialize the entire struct tm. For example, tm_zone pointer is left
1420     // uninitialized.
1421     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1422   }
1423   return res;
1424 }
1425 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1426 #else
1427 #define INIT_STRPTIME
1428 #endif
1429
1430 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1431 #include "sanitizer_common_interceptors_format.inc"
1432
1433 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...)                              \
1434   {                                                                            \
1435     void *ctx;                                                                 \
1436     va_list ap;                                                                \
1437     va_start(ap, format);                                                      \
1438     COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap);                     \
1439     int res = WRAP(vname)(__VA_ARGS__, ap);                                    \
1440     va_end(ap);                                                                \
1441     return res;                                                                \
1442   }
1443
1444 #endif
1445
1446 #if SANITIZER_INTERCEPT_SCANF
1447
1448 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...)                    \
1449   {                                                                            \
1450     void *ctx;                                                                 \
1451     COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                         \
1452     va_list aq;                                                                \
1453     va_copy(aq, ap);                                                           \
1454     int res = REAL(vname)(__VA_ARGS__);                                        \
1455     if (res > 0)                                                               \
1456       scanf_common(ctx, res, allowGnuMalloc, format, aq);                      \
1457     va_end(aq);                                                                \
1458     return res;                                                                \
1459   }
1460
1461 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1462 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1463
1464 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1465 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1466
1467 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1468 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1469
1470 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1471 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1472 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1473
1474 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1475             va_list ap)
1476 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1477
1478 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1479 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1480 #endif  // SANITIZER_INTERCEPT_ISOC99_SCANF
1481
1482 INTERCEPTOR(int, scanf, const char *format, ...)
1483 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1484
1485 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1486 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1487
1488 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1489 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1490
1491 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1492 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1493 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1494
1495 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1496 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1497
1498 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1499 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1500 #endif
1501
1502 #endif
1503
1504 #if SANITIZER_INTERCEPT_SCANF
1505 #define INIT_SCANF                    \
1506   COMMON_INTERCEPT_FUNCTION_LDBL(scanf);   \
1507   COMMON_INTERCEPT_FUNCTION_LDBL(sscanf);  \
1508   COMMON_INTERCEPT_FUNCTION_LDBL(fscanf);  \
1509   COMMON_INTERCEPT_FUNCTION_LDBL(vscanf);  \
1510   COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1511   COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1512 #else
1513 #define INIT_SCANF
1514 #endif
1515
1516 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1517 #define INIT_ISOC99_SCANF                      \
1518   COMMON_INTERCEPT_FUNCTION(__isoc99_scanf);   \
1519   COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf);  \
1520   COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf);  \
1521   COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf);  \
1522   COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1523   COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1524 #else
1525 #define INIT_ISOC99_SCANF
1526 #endif
1527
1528 #if SANITIZER_INTERCEPT_PRINTF
1529
1530 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...)                                  \
1531   void *ctx;                                                                   \
1532   COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                           \
1533   va_list aq;                                                                  \
1534   va_copy(aq, ap);
1535
1536 #define VPRINTF_INTERCEPTOR_RETURN()                                           \
1537   va_end(aq);
1538
1539 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...)                                   \
1540   {                                                                            \
1541     VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__);                             \
1542     if (common_flags()->check_printf)                                          \
1543       printf_common(ctx, format, aq);                                          \
1544     int res = REAL(vname)(__VA_ARGS__);                                        \
1545     VPRINTF_INTERCEPTOR_RETURN();                                              \
1546     return res;                                                                \
1547   }
1548
1549 // FIXME: under ASan the REAL() call below may write to freed memory and
1550 // corrupt its metadata. See
1551 // https://github.com/google/sanitizers/issues/321.
1552 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...)                             \
1553   {                                                                            \
1554     VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__)                         \
1555     if (common_flags()->check_printf) {                                        \
1556       printf_common(ctx, format, aq);                                          \
1557     }                                                                          \
1558     int res = REAL(vname)(str, __VA_ARGS__);                                   \
1559     if (res >= 0) {                                                            \
1560       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1);                       \
1561     }                                                                          \
1562     VPRINTF_INTERCEPTOR_RETURN();                                              \
1563     return res;                                                                \
1564   }
1565
1566 // FIXME: under ASan the REAL() call below may write to freed memory and
1567 // corrupt its metadata. See
1568 // https://github.com/google/sanitizers/issues/321.
1569 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...)                      \
1570   {                                                                            \
1571     VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__)                   \
1572     if (common_flags()->check_printf) {                                        \
1573       printf_common(ctx, format, aq);                                          \
1574     }                                                                          \
1575     int res = REAL(vname)(str, size, __VA_ARGS__);                             \
1576     if (res >= 0) {                                                            \
1577       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1)));  \
1578     }                                                                          \
1579     VPRINTF_INTERCEPTOR_RETURN();                                              \
1580     return res;                                                                \
1581   }
1582
1583 // FIXME: under ASan the REAL() call below may write to freed memory and
1584 // corrupt its metadata. See
1585 // https://github.com/google/sanitizers/issues/321.
1586 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...)                           \
1587   {                                                                            \
1588     VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__)                        \
1589     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *));                 \
1590     if (common_flags()->check_printf) {                                        \
1591       printf_common(ctx, format, aq);                                          \
1592     }                                                                          \
1593     int res = REAL(vname)(strp, __VA_ARGS__);                                  \
1594     if (res >= 0) {                                                            \
1595       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1);                     \
1596     }                                                                          \
1597     VPRINTF_INTERCEPTOR_RETURN();                                              \
1598     return res;                                                                \
1599   }
1600
1601 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1602 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1603
1604 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1605             va_list ap)
1606 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1607
1608 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1609             va_list ap)
1610 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1611
1612 #if SANITIZER_INTERCEPT___PRINTF_CHK
1613 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1614             SIZE_T size_to, const char *format, va_list ap)
1615 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1616 #endif
1617
1618 #if SANITIZER_INTERCEPT_PRINTF_L
1619 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1620             const char *format, va_list ap)
1621 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1622
1623 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1624             const char *format, ...)
1625 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1626 #endif  // SANITIZER_INTERCEPT_PRINTF_L
1627
1628 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1629 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1630
1631 #if SANITIZER_INTERCEPT___PRINTF_CHK
1632 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1633             const char *format, va_list ap)
1634 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1635 #endif
1636
1637 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1638 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1639
1640 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1641 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1642 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1643
1644 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1645             const char *format, va_list ap)
1646 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1647
1648 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1649             va_list ap)
1650 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1651
1652 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1653             va_list ap)
1654 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1655                           ap)
1656
1657 #endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
1658
1659 INTERCEPTOR(int, printf, const char *format, ...)
1660 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1661
1662 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1663 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1664
1665 #if SANITIZER_INTERCEPT___PRINTF_CHK
1666 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1667             const char *format, ...)
1668 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1669 #endif
1670
1671 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1672 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1673
1674 #if SANITIZER_INTERCEPT___PRINTF_CHK
1675 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1676             const char *format, ...) // NOLINT
1677 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format) // NOLINT
1678 #endif
1679
1680 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1681 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1682
1683 #if SANITIZER_INTERCEPT___PRINTF_CHK
1684 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1685             SIZE_T size_to, const char *format, ...) // NOLINT
1686 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format) // NOLINT
1687 #endif
1688
1689 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1690 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1691
1692 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1693 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1694 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1695
1696 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1697             ...)
1698 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1699
1700 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1701 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1702
1703 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1704             const char *format, ...)
1705 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1706                         format)
1707
1708 #endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
1709
1710 #endif  // SANITIZER_INTERCEPT_PRINTF
1711
1712 #if SANITIZER_INTERCEPT_PRINTF
1713 #define INIT_PRINTF                     \
1714   COMMON_INTERCEPT_FUNCTION_LDBL(printf);    \
1715   COMMON_INTERCEPT_FUNCTION_LDBL(sprintf);   \
1716   COMMON_INTERCEPT_FUNCTION_LDBL(snprintf);  \
1717   COMMON_INTERCEPT_FUNCTION_LDBL(asprintf);  \
1718   COMMON_INTERCEPT_FUNCTION_LDBL(fprintf);   \
1719   COMMON_INTERCEPT_FUNCTION_LDBL(vprintf);   \
1720   COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf);  \
1721   COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1722   COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1723   COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1724 #else
1725 #define INIT_PRINTF
1726 #endif
1727
1728 #if SANITIZER_INTERCEPT___PRINTF_CHK
1729 #define INIT___PRINTF_CHK                     \
1730   COMMON_INTERCEPT_FUNCTION(__sprintf_chk);   \
1731   COMMON_INTERCEPT_FUNCTION(__snprintf_chk);  \
1732   COMMON_INTERCEPT_FUNCTION(__vsprintf_chk);  \
1733   COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1734   COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1735 #else
1736 #define INIT___PRINTF_CHK
1737 #endif
1738
1739 #if SANITIZER_INTERCEPT_PRINTF_L
1740 #define INIT_PRINTF_L                     \
1741   COMMON_INTERCEPT_FUNCTION(snprintf_l);  \
1742   COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1743 #else
1744 #define INIT_PRINTF_L
1745 #endif
1746
1747 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1748 #define INIT_ISOC99_PRINTF                       \
1749   COMMON_INTERCEPT_FUNCTION(__isoc99_printf);    \
1750   COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf);   \
1751   COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf);  \
1752   COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf);   \
1753   COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf);   \
1754   COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf);  \
1755   COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1756   COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1757 #else
1758 #define INIT_ISOC99_PRINTF
1759 #endif
1760
1761 #if SANITIZER_INTERCEPT_IOCTL
1762 #include "sanitizer_common_interceptors_ioctl.inc"
1763 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1764 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1765   // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1766   // can trigger a report and we need to be able to unwind through this
1767   // function.  On Mac in debug mode we might not have a frame pointer, because
1768   // ioctl_common_[pre|post] doesn't get inlined here.
1769   ENABLE_FRAME_POINTER;
1770
1771   void *ctx;
1772   va_list ap;
1773   va_start(ap, request);
1774   void *arg = va_arg(ap, void *);
1775   va_end(ap);
1776   COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1777
1778   CHECK(ioctl_initialized);
1779
1780   // Note: TSan does not use common flags, and they are zero-initialized.
1781   // This effectively disables ioctl handling in TSan.
1782   if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1783
1784   // Although request is unsigned long, the rest of the interceptor uses it
1785   // as just "unsigned" to save space, because we know that all values fit in
1786   // "unsigned" - they are compile-time constants.
1787
1788   const ioctl_desc *desc = ioctl_lookup(request);
1789   ioctl_desc decoded_desc;
1790   if (!desc) {
1791     VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1792     if (!ioctl_decode(request, &decoded_desc))
1793       Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1794     else
1795       desc = &decoded_desc;
1796   }
1797
1798   if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1799   int res = REAL(ioctl)(d, request, arg);
1800   // FIXME: some ioctls have different return values for success and failure.
1801   if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1802   return res;
1803 }
1804 #define INIT_IOCTL \
1805   ioctl_init();    \
1806   COMMON_INTERCEPT_FUNCTION(ioctl);
1807 #else
1808 #define INIT_IOCTL
1809 #endif
1810
1811 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1812     SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1813     SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1814 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1815   if (pwd) {
1816     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1817     if (pwd->pw_name)
1818       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1819                                           REAL(strlen)(pwd->pw_name) + 1);
1820     if (pwd->pw_passwd)
1821       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1822                                           REAL(strlen)(pwd->pw_passwd) + 1);
1823 #if !SANITIZER_ANDROID
1824     if (pwd->pw_gecos)
1825       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1826                                           REAL(strlen)(pwd->pw_gecos) + 1);
1827 #endif
1828 #if SANITIZER_MAC
1829     if (pwd->pw_class)
1830       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1831                                           REAL(strlen)(pwd->pw_class) + 1);
1832 #endif
1833     if (pwd->pw_dir)
1834       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1835                                           REAL(strlen)(pwd->pw_dir) + 1);
1836     if (pwd->pw_shell)
1837       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1838                                           REAL(strlen)(pwd->pw_shell) + 1);
1839   }
1840 }
1841
1842 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1843   if (grp) {
1844     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1845     if (grp->gr_name)
1846       COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1847                                           REAL(strlen)(grp->gr_name) + 1);
1848     if (grp->gr_passwd)
1849       COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1850                                           REAL(strlen)(grp->gr_passwd) + 1);
1851     char **p = grp->gr_mem;
1852     for (; *p; ++p) {
1853       COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1854     }
1855     COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1856                                         (p - grp->gr_mem + 1) * sizeof(*p));
1857   }
1858 }
1859 #endif  // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1860         // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1861         // SANITIZER_INTERCEPT_GETPWENT_R ||
1862         // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1863
1864 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1865 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1866   void *ctx;
1867   COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1868   if (name)
1869     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1870   __sanitizer_passwd *res = REAL(getpwnam)(name);
1871   if (res) unpoison_passwd(ctx, res);
1872   return res;
1873 }
1874 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1875   void *ctx;
1876   COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1877   __sanitizer_passwd *res = REAL(getpwuid)(uid);
1878   if (res) unpoison_passwd(ctx, res);
1879   return res;
1880 }
1881 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1882   void *ctx;
1883   COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1884   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1885   __sanitizer_group *res = REAL(getgrnam)(name);
1886   if (res) unpoison_group(ctx, res);
1887   return res;
1888 }
1889 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1890   void *ctx;
1891   COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1892   __sanitizer_group *res = REAL(getgrgid)(gid);
1893   if (res) unpoison_group(ctx, res);
1894   return res;
1895 }
1896 #define INIT_GETPWNAM_AND_FRIENDS      \
1897   COMMON_INTERCEPT_FUNCTION(getpwnam); \
1898   COMMON_INTERCEPT_FUNCTION(getpwuid); \
1899   COMMON_INTERCEPT_FUNCTION(getgrnam); \
1900   COMMON_INTERCEPT_FUNCTION(getgrgid);
1901 #else
1902 #define INIT_GETPWNAM_AND_FRIENDS
1903 #endif
1904
1905 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1906 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1907             char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1908   void *ctx;
1909   COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1910   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1911   // FIXME: under ASan the call below may write to freed memory and corrupt
1912   // its metadata. See
1913   // https://github.com/google/sanitizers/issues/321.
1914   int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1915   if (!res) {
1916     if (result && *result) unpoison_passwd(ctx, *result);
1917     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1918   }
1919   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1920   return res;
1921 }
1922 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1923             SIZE_T buflen, __sanitizer_passwd **result) {
1924   void *ctx;
1925   COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1926   // FIXME: under ASan the call below may write to freed memory and corrupt
1927   // its metadata. See
1928   // https://github.com/google/sanitizers/issues/321.
1929   int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1930   if (!res) {
1931     if (result && *result) unpoison_passwd(ctx, *result);
1932     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1933   }
1934   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1935   return res;
1936 }
1937 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1938             char *buf, SIZE_T buflen, __sanitizer_group **result) {
1939   void *ctx;
1940   COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1941   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1942   // FIXME: under ASan the call below may write to freed memory and corrupt
1943   // its metadata. See
1944   // https://github.com/google/sanitizers/issues/321.
1945   int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1946   if (!res) {
1947     if (result && *result) unpoison_group(ctx, *result);
1948     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1949   }
1950   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1951   return res;
1952 }
1953 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1954             SIZE_T buflen, __sanitizer_group **result) {
1955   void *ctx;
1956   COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1957   // FIXME: under ASan the call below may write to freed memory and corrupt
1958   // its metadata. See
1959   // https://github.com/google/sanitizers/issues/321.
1960   int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1961   if (!res) {
1962     if (result && *result) unpoison_group(ctx, *result);
1963     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1964   }
1965   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1966   return res;
1967 }
1968 #define INIT_GETPWNAM_R_AND_FRIENDS      \
1969   COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1970   COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1971   COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1972   COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1973 #else
1974 #define INIT_GETPWNAM_R_AND_FRIENDS
1975 #endif
1976
1977 #if SANITIZER_INTERCEPT_GETPWENT
1978 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1979   void *ctx;
1980   COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1981   __sanitizer_passwd *res = REAL(getpwent)(dummy);
1982   if (res) unpoison_passwd(ctx, res);
1983   return res;
1984 }
1985 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1986   void *ctx;
1987   COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1988   __sanitizer_group *res = REAL(getgrent)(dummy);
1989   if (res) unpoison_group(ctx, res);;
1990   return res;
1991 }
1992 #define INIT_GETPWENT                  \
1993   COMMON_INTERCEPT_FUNCTION(getpwent); \
1994   COMMON_INTERCEPT_FUNCTION(getgrent);
1995 #else
1996 #define INIT_GETPWENT
1997 #endif
1998
1999 #if SANITIZER_INTERCEPT_FGETPWENT
2000 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2001   void *ctx;
2002   COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2003   __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2004   if (res) unpoison_passwd(ctx, res);
2005   return res;
2006 }
2007 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2008   void *ctx;
2009   COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2010   __sanitizer_group *res = REAL(fgetgrent)(fp);
2011   if (res) unpoison_group(ctx, res);
2012   return res;
2013 }
2014 #define INIT_FGETPWENT                  \
2015   COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2016   COMMON_INTERCEPT_FUNCTION(fgetgrent);
2017 #else
2018 #define INIT_FGETPWENT
2019 #endif
2020
2021 #if SANITIZER_INTERCEPT_GETPWENT_R
2022 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2023             SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2024   void *ctx;
2025   COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2026   // FIXME: under ASan the call below may write to freed memory and corrupt
2027   // its metadata. See
2028   // https://github.com/google/sanitizers/issues/321.
2029   int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2030   if (!res) {
2031     if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2032     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2033   }
2034   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2035   return res;
2036 }
2037 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2038             SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2039   void *ctx;
2040   COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2041   // FIXME: under ASan the call below may write to freed memory and corrupt
2042   // its metadata. See
2043   // https://github.com/google/sanitizers/issues/321.
2044   int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2045   if (!res) {
2046     if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2047     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2048   }
2049   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2050   return res;
2051 }
2052 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2053             __sanitizer_group **pwbufp) {
2054   void *ctx;
2055   COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2056   // FIXME: under ASan the call below may write to freed memory and corrupt
2057   // its metadata. See
2058   // https://github.com/google/sanitizers/issues/321.
2059   int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2060   if (!res) {
2061     if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2062     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2063   }
2064   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2065   return res;
2066 }
2067 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2068             SIZE_T buflen, __sanitizer_group **pwbufp) {
2069   void *ctx;
2070   COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2071   // FIXME: under ASan the call below may write to freed memory and corrupt
2072   // its metadata. See
2073   // https://github.com/google/sanitizers/issues/321.
2074   int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2075   if (!res) {
2076     if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2077     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2078   }
2079   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2080   return res;
2081 }
2082 #define INIT_GETPWENT_R                   \
2083   COMMON_INTERCEPT_FUNCTION(getpwent_r);  \
2084   COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
2085   COMMON_INTERCEPT_FUNCTION(getgrent_r);  \
2086   COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2087 #else
2088 #define INIT_GETPWENT_R
2089 #endif
2090
2091 #if SANITIZER_INTERCEPT_SETPWENT
2092 // The only thing these interceptors do is disable any nested interceptors.
2093 // These functions may open nss modules and call uninstrumented functions from
2094 // them, and we don't want things like strlen() to trigger.
2095 INTERCEPTOR(void, setpwent, int dummy) {
2096   void *ctx;
2097   COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2098   REAL(setpwent)(dummy);
2099 }
2100 INTERCEPTOR(void, endpwent, int dummy) {
2101   void *ctx;
2102   COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2103   REAL(endpwent)(dummy);
2104 }
2105 INTERCEPTOR(void, setgrent, int dummy) {
2106   void *ctx;
2107   COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2108   REAL(setgrent)(dummy);
2109 }
2110 INTERCEPTOR(void, endgrent, int dummy) {
2111   void *ctx;
2112   COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2113   REAL(endgrent)(dummy);
2114 }
2115 #define INIT_SETPWENT                  \
2116   COMMON_INTERCEPT_FUNCTION(setpwent); \
2117   COMMON_INTERCEPT_FUNCTION(endpwent); \
2118   COMMON_INTERCEPT_FUNCTION(setgrent); \
2119   COMMON_INTERCEPT_FUNCTION(endgrent);
2120 #else
2121 #define INIT_SETPWENT
2122 #endif
2123
2124 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2125 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2126   void *ctx;
2127   COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2128   // FIXME: under ASan the call below may write to freed memory and corrupt
2129   // its metadata. See
2130   // https://github.com/google/sanitizers/issues/321.
2131   int res = REAL(clock_getres)(clk_id, tp);
2132   if (!res && tp) {
2133     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2134   }
2135   return res;
2136 }
2137 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2138   void *ctx;
2139   COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2140   // FIXME: under ASan the call below may write to freed memory and corrupt
2141   // its metadata. See
2142   // https://github.com/google/sanitizers/issues/321.
2143   int res = REAL(clock_gettime)(clk_id, tp);
2144   if (!res) {
2145     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2146   }
2147   return res;
2148 }
2149 namespace __sanitizer {
2150 extern "C" {
2151 int real_clock_gettime(u32 clk_id, void *tp) {
2152   return REAL(clock_gettime)(clk_id, tp);
2153 }
2154 }  // extern "C"
2155 }  // namespace __sanitizer
2156 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2157   void *ctx;
2158   COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2159   COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2160   return REAL(clock_settime)(clk_id, tp);
2161 }
2162 #define INIT_CLOCK_GETTIME                  \
2163   COMMON_INTERCEPT_FUNCTION(clock_getres);  \
2164   COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2165   COMMON_INTERCEPT_FUNCTION(clock_settime);
2166 #else
2167 #define INIT_CLOCK_GETTIME
2168 #endif
2169
2170 #if SANITIZER_INTERCEPT_GETITIMER
2171 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2172   void *ctx;
2173   COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2174   // FIXME: under ASan the call below may write to freed memory and corrupt
2175   // its metadata. See
2176   // https://github.com/google/sanitizers/issues/321.
2177   int res = REAL(getitimer)(which, curr_value);
2178   if (!res && curr_value) {
2179     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2180   }
2181   return res;
2182 }
2183 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2184   void *ctx;
2185   COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2186   if (new_value) {
2187     // itimerval can contain padding that may be legitimately uninitialized
2188     const struct __sanitizer_itimerval *nv =
2189         (const struct __sanitizer_itimerval *)new_value;
2190     COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2191                                   sizeof(__sanitizer_time_t));
2192     COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2193                                   sizeof(__sanitizer_suseconds_t));
2194     COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2195                                   sizeof(__sanitizer_time_t));
2196     COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2197                                   sizeof(__sanitizer_suseconds_t));
2198   }
2199   // FIXME: under ASan the call below may write to freed memory and corrupt
2200   // its metadata. See
2201   // https://github.com/google/sanitizers/issues/321.
2202   int res = REAL(setitimer)(which, new_value, old_value);
2203   if (!res && old_value) {
2204     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2205   }
2206   return res;
2207 }
2208 #define INIT_GETITIMER                  \
2209   COMMON_INTERCEPT_FUNCTION(getitimer); \
2210   COMMON_INTERCEPT_FUNCTION(setitimer);
2211 #else
2212 #define INIT_GETITIMER
2213 #endif
2214
2215 #if SANITIZER_INTERCEPT_GLOB
2216 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2217   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2218   // +1 for NULL pointer at the end.
2219   if (pglob->gl_pathv)
2220     COMMON_INTERCEPTOR_WRITE_RANGE(
2221         ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2222   for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2223     char *p = pglob->gl_pathv[i];
2224     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2225   }
2226 }
2227
2228 #if SANITIZER_SOLARIS
2229 INTERCEPTOR(int, glob, const char *pattern, int flags,
2230             int (*errfunc)(const char *epath, int eerrno),
2231             __sanitizer_glob_t *pglob) {
2232   void *ctx;
2233   COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2234   COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2235   int res = REAL(glob)(pattern, flags, errfunc, pglob);
2236   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2237   return res;
2238 }
2239 #else
2240 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2241
2242 static void wrapped_gl_closedir(void *dir) {
2243   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2244   pglob_copy->gl_closedir(dir);
2245 }
2246
2247 static void *wrapped_gl_readdir(void *dir) {
2248   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2249   return pglob_copy->gl_readdir(dir);
2250 }
2251
2252 static void *wrapped_gl_opendir(const char *s) {
2253   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2254   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2255   return pglob_copy->gl_opendir(s);
2256 }
2257
2258 static int wrapped_gl_lstat(const char *s, void *st) {
2259   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2260   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2261   return pglob_copy->gl_lstat(s, st);
2262 }
2263
2264 static int wrapped_gl_stat(const char *s, void *st) {
2265   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2266   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2267   return pglob_copy->gl_stat(s, st);
2268 }
2269
2270 static const __sanitizer_glob_t kGlobCopy = {
2271       0,                  0,                   0,
2272       0,                  wrapped_gl_closedir, wrapped_gl_readdir,
2273       wrapped_gl_opendir, wrapped_gl_lstat,    wrapped_gl_stat};
2274
2275 INTERCEPTOR(int, glob, const char *pattern, int flags,
2276             int (*errfunc)(const char *epath, int eerrno),
2277             __sanitizer_glob_t *pglob) {
2278   void *ctx;
2279   COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2280   COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2281   __sanitizer_glob_t glob_copy;
2282   internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2283   if (flags & glob_altdirfunc) {
2284     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2285     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2286     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2287     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2288     Swap(pglob->gl_stat, glob_copy.gl_stat);
2289     pglob_copy = &glob_copy;
2290   }
2291   int res = REAL(glob)(pattern, flags, errfunc, pglob);
2292   if (flags & glob_altdirfunc) {
2293     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2294     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2295     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2296     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2297     Swap(pglob->gl_stat, glob_copy.gl_stat);
2298   }
2299   pglob_copy = 0;
2300   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2301   return res;
2302 }
2303 #endif  // SANITIZER_SOLARIS
2304 #define INIT_GLOB                  \
2305   COMMON_INTERCEPT_FUNCTION(glob);
2306 #else  // SANITIZER_INTERCEPT_GLOB
2307 #define INIT_GLOB
2308 #endif  // SANITIZER_INTERCEPT_GLOB
2309
2310 #if SANITIZER_INTERCEPT_GLOB64
2311 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2312             int (*errfunc)(const char *epath, int eerrno),
2313             __sanitizer_glob_t *pglob) {
2314   void *ctx;
2315   COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2316   COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2317   __sanitizer_glob_t glob_copy;
2318   internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2319   if (flags & glob_altdirfunc) {
2320     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2321     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2322     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2323     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2324     Swap(pglob->gl_stat, glob_copy.gl_stat);
2325     pglob_copy = &glob_copy;
2326   }
2327   int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2328   if (flags & glob_altdirfunc) {
2329     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2330     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2331     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2332     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2333     Swap(pglob->gl_stat, glob_copy.gl_stat);
2334   }
2335   pglob_copy = 0;
2336   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2337   return res;
2338 }
2339 #define INIT_GLOB64                \
2340   COMMON_INTERCEPT_FUNCTION(glob64);
2341 #else  // SANITIZER_INTERCEPT_GLOB64
2342 #define INIT_GLOB64
2343 #endif  // SANITIZER_INTERCEPT_GLOB64
2344
2345 #if SANITIZER_INTERCEPT_WAIT
2346 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2347 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2348 // details.
2349 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2350   void *ctx;
2351   COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2352   // FIXME: under ASan the call below may write to freed memory and corrupt
2353   // its metadata. See
2354   // https://github.com/google/sanitizers/issues/321.
2355   int res = REAL(wait)(status);
2356   if (res != -1 && status)
2357     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2358   return res;
2359 }
2360 // On FreeBSD id_t is always 64-bit wide.
2361 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2362 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2363                         int options) {
2364 #else
2365 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2366                         int options) {
2367 #endif
2368   void *ctx;
2369   COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2370   // FIXME: under ASan the call below may write to freed memory and corrupt
2371   // its metadata. See
2372   // https://github.com/google/sanitizers/issues/321.
2373   int res = REAL(waitid)(idtype, id, infop, options);
2374   if (res != -1 && infop)
2375     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2376   return res;
2377 }
2378 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2379   void *ctx;
2380   COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2381   // FIXME: under ASan the call below may write to freed memory and corrupt
2382   // its metadata. See
2383   // https://github.com/google/sanitizers/issues/321.
2384   int res = REAL(waitpid)(pid, status, options);
2385   if (res != -1 && status)
2386     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2387   return res;
2388 }
2389 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2390   void *ctx;
2391   COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2392   // FIXME: under ASan the call below may write to freed memory and corrupt
2393   // its metadata. See
2394   // https://github.com/google/sanitizers/issues/321.
2395   int res = REAL(wait3)(status, options, rusage);
2396   if (res != -1) {
2397     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2398     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2399   }
2400   return res;
2401 }
2402 #if SANITIZER_ANDROID
2403 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2404   void *ctx;
2405   COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2406   // FIXME: under ASan the call below may write to freed memory and corrupt
2407   // its metadata. See
2408   // https://github.com/google/sanitizers/issues/321.
2409   int res = REAL(__wait4)(pid, status, options, rusage);
2410   if (res != -1) {
2411     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2412     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2413   }
2414   return res;
2415 }
2416 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2417 #else
2418 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2419   void *ctx;
2420   COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2421   // FIXME: under ASan the call below may write to freed memory and corrupt
2422   // its metadata. See
2423   // https://github.com/google/sanitizers/issues/321.
2424   int res = REAL(wait4)(pid, status, options, rusage);
2425   if (res != -1) {
2426     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2427     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2428   }
2429   return res;
2430 }
2431 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2432 #endif  // SANITIZER_ANDROID
2433 #define INIT_WAIT                     \
2434   COMMON_INTERCEPT_FUNCTION(wait);    \
2435   COMMON_INTERCEPT_FUNCTION(waitid);  \
2436   COMMON_INTERCEPT_FUNCTION(waitpid); \
2437   COMMON_INTERCEPT_FUNCTION(wait3);
2438 #else
2439 #define INIT_WAIT
2440 #define INIT_WAIT4
2441 #endif
2442
2443 #if SANITIZER_INTERCEPT_INET
2444 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2445   void *ctx;
2446   COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2447   uptr sz = __sanitizer_in_addr_sz(af);
2448   if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2449   // FIXME: figure out read size based on the address family.
2450   // FIXME: under ASan the call below may write to freed memory and corrupt
2451   // its metadata. See
2452   // https://github.com/google/sanitizers/issues/321.
2453   char *res = REAL(inet_ntop)(af, src, dst, size);
2454   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2455   return res;
2456 }
2457 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2458   void *ctx;
2459   COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2460   COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2461   // FIXME: figure out read size based on the address family.
2462   // FIXME: under ASan the call below may write to freed memory and corrupt
2463   // its metadata. See
2464   // https://github.com/google/sanitizers/issues/321.
2465   int res = REAL(inet_pton)(af, src, dst);
2466   if (res == 1) {
2467     uptr sz = __sanitizer_in_addr_sz(af);
2468     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2469   }
2470   return res;
2471 }
2472 #define INIT_INET                       \
2473   COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2474   COMMON_INTERCEPT_FUNCTION(inet_pton);
2475 #else
2476 #define INIT_INET
2477 #endif
2478
2479 #if SANITIZER_INTERCEPT_INET
2480 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2481   void *ctx;
2482   COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2483   if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2484   // FIXME: under ASan the call below may write to freed memory and corrupt
2485   // its metadata. See
2486   // https://github.com/google/sanitizers/issues/321.
2487   int res = REAL(inet_aton)(cp, dst);
2488   if (res != 0) {
2489     uptr sz = __sanitizer_in_addr_sz(af_inet);
2490     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2491   }
2492   return res;
2493 }
2494 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2495 #else
2496 #define INIT_INET_ATON
2497 #endif
2498
2499 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2500 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2501   void *ctx;
2502   COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2503   // FIXME: under ASan the call below may write to freed memory and corrupt
2504   // its metadata. See
2505   // https://github.com/google/sanitizers/issues/321.
2506   int res = REAL(pthread_getschedparam)(thread, policy, param);
2507   if (res == 0) {
2508     if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2509     if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2510   }
2511   return res;
2512 }
2513 #define INIT_PTHREAD_GETSCHEDPARAM \
2514   COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2515 #else
2516 #define INIT_PTHREAD_GETSCHEDPARAM
2517 #endif
2518
2519 #if SANITIZER_INTERCEPT_GETADDRINFO
2520 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2521             struct __sanitizer_addrinfo *hints,
2522             struct __sanitizer_addrinfo **out) {
2523   void *ctx;
2524   COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2525   if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2526   if (service)
2527     COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2528   if (hints)
2529     COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2530   // FIXME: under ASan the call below may write to freed memory and corrupt
2531   // its metadata. See
2532   // https://github.com/google/sanitizers/issues/321.
2533   int res = REAL(getaddrinfo)(node, service, hints, out);
2534   if (res == 0 && out) {
2535     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2536     struct __sanitizer_addrinfo *p = *out;
2537     while (p) {
2538       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2539       if (p->ai_addr)
2540         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2541       if (p->ai_canonname)
2542         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2543                                        REAL(strlen)(p->ai_canonname) + 1);
2544       p = p->ai_next;
2545     }
2546   }
2547   return res;
2548 }
2549 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2550 #else
2551 #define INIT_GETADDRINFO
2552 #endif
2553
2554 #if SANITIZER_INTERCEPT_GETNAMEINFO
2555 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2556             unsigned hostlen, char *serv, unsigned servlen, int flags) {
2557   void *ctx;
2558   COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2559                            serv, servlen, flags);
2560   // FIXME: consider adding READ_RANGE(sockaddr, salen)
2561   // There is padding in in_addr that may make this too noisy
2562   // FIXME: under ASan the call below may write to freed memory and corrupt
2563   // its metadata. See
2564   // https://github.com/google/sanitizers/issues/321.
2565   int res =
2566       REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2567   if (res == 0) {
2568     if (host && hostlen)
2569       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2570     if (serv && servlen)
2571       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2572   }
2573   return res;
2574 }
2575 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2576 #else
2577 #define INIT_GETNAMEINFO
2578 #endif
2579
2580 #if SANITIZER_INTERCEPT_GETSOCKNAME
2581 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2582   void *ctx;
2583   COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2584   COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2585   int addrlen_in = *addrlen;
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 = REAL(getsockname)(sock_fd, addr, addrlen);
2590   if (res == 0) {
2591     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2592   }
2593   return res;
2594 }
2595 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2596 #else
2597 #define INIT_GETSOCKNAME
2598 #endif
2599
2600 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2601 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2602   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2603   if (h->h_name)
2604     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2605   char **p = h->h_aliases;
2606   while (*p) {
2607     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2608     ++p;
2609   }
2610   COMMON_INTERCEPTOR_WRITE_RANGE(
2611       ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2612   p = h->h_addr_list;
2613   while (*p) {
2614     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2615     ++p;
2616   }
2617   COMMON_INTERCEPTOR_WRITE_RANGE(
2618       ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2619 }
2620 #endif
2621
2622 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2623 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2624   void *ctx;
2625   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2626   struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2627   if (res) write_hostent(ctx, res);
2628   return res;
2629 }
2630
2631 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2632             int type) {
2633   void *ctx;
2634   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2635   COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2636   struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2637   if (res) write_hostent(ctx, res);
2638   return res;
2639 }
2640
2641 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2642   void *ctx;
2643   COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2644   struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2645   if (res) write_hostent(ctx, res);
2646   return res;
2647 }
2648 #define INIT_GETHOSTBYNAME                  \
2649   COMMON_INTERCEPT_FUNCTION(gethostent);    \
2650   COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2651   COMMON_INTERCEPT_FUNCTION(gethostbyname);
2652 #else
2653 #define INIT_GETHOSTBYNAME
2654 #endif  // SANITIZER_INTERCEPT_GETHOSTBYNAME
2655
2656 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2657 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2658   void *ctx;
2659   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2660   struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2661   if (res) write_hostent(ctx, res);
2662   return res;
2663 }
2664 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2665 #else
2666 #define INIT_GETHOSTBYNAME2
2667 #endif  // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2668
2669 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2670 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2671             char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2672             int *h_errnop) {
2673   void *ctx;
2674   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2675                            h_errnop);
2676   // FIXME: under ASan the call below may write to freed memory and corrupt
2677   // its metadata. See
2678   // https://github.com/google/sanitizers/issues/321.
2679   int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2680   if (result) {
2681     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2682     if (res == 0 && *result) write_hostent(ctx, *result);
2683   }
2684   if (h_errnop)
2685     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2686   return res;
2687 }
2688 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2689 #else
2690 #define INIT_GETHOSTBYNAME_R
2691 #endif
2692
2693 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2694 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2695             SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2696   void *ctx;
2697   COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2698                            h_errnop);
2699   // FIXME: under ASan the call below may write to freed memory and corrupt
2700   // its metadata. See
2701   // https://github.com/google/sanitizers/issues/321.
2702   int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2703   if (result) {
2704     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2705     if (res == 0 && *result) write_hostent(ctx, *result);
2706   }
2707   if (h_errnop)
2708     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2709   return res;
2710 }
2711 #define INIT_GETHOSTENT_R                  \
2712   COMMON_INTERCEPT_FUNCTION(gethostent_r);
2713 #else
2714 #define INIT_GETHOSTENT_R
2715 #endif
2716
2717 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2718 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2719             struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2720             __sanitizer_hostent **result, int *h_errnop) {
2721   void *ctx;
2722   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2723                            buflen, result, h_errnop);
2724   COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2725   // FIXME: under ASan the call below may write to freed memory and corrupt
2726   // its metadata. See
2727   // https://github.com/google/sanitizers/issues/321.
2728   int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2729                                   h_errnop);
2730   if (result) {
2731     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2732     if (res == 0 && *result) write_hostent(ctx, *result);
2733   }
2734   if (h_errnop)
2735     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2736   return res;
2737 }
2738 #define INIT_GETHOSTBYADDR_R                  \
2739   COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2740 #else
2741 #define INIT_GETHOSTBYADDR_R
2742 #endif
2743
2744 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2745 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2746             struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2747             __sanitizer_hostent **result, int *h_errnop) {
2748   void *ctx;
2749   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2750                            result, h_errnop);
2751   // FIXME: under ASan the call below may write to freed memory and corrupt
2752   // its metadata. See
2753   // https://github.com/google/sanitizers/issues/321.
2754   int res =
2755       REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2756   if (result) {
2757     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2758     if (res == 0 && *result) write_hostent(ctx, *result);
2759   }
2760   if (h_errnop)
2761     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2762   return res;
2763 }
2764 #define INIT_GETHOSTBYNAME2_R                  \
2765   COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2766 #else
2767 #define INIT_GETHOSTBYNAME2_R
2768 #endif
2769
2770 #if SANITIZER_INTERCEPT_GETSOCKOPT
2771 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2772             int *optlen) {
2773   void *ctx;
2774   COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2775                            optlen);
2776   if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2777   // FIXME: under ASan the call below may write to freed memory and corrupt
2778   // its metadata. See
2779   // https://github.com/google/sanitizers/issues/321.
2780   int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2781   if (res == 0)
2782     if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2783   return res;
2784 }
2785 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2786 #else
2787 #define INIT_GETSOCKOPT
2788 #endif
2789
2790 #if SANITIZER_INTERCEPT_ACCEPT
2791 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2792   void *ctx;
2793   COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2794   unsigned addrlen0 = 0;
2795   if (addrlen) {
2796     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2797     addrlen0 = *addrlen;
2798   }
2799   int fd2 = REAL(accept)(fd, addr, addrlen);
2800   if (fd2 >= 0) {
2801     if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2802     if (addr && addrlen)
2803       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2804   }
2805   return fd2;
2806 }
2807 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2808 #else
2809 #define INIT_ACCEPT
2810 #endif
2811
2812 #if SANITIZER_INTERCEPT_ACCEPT4
2813 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2814   void *ctx;
2815   COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2816   unsigned addrlen0 = 0;
2817   if (addrlen) {
2818     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2819     addrlen0 = *addrlen;
2820   }
2821   // FIXME: under ASan the call below may write to freed memory and corrupt
2822   // its metadata. See
2823   // https://github.com/google/sanitizers/issues/321.
2824   int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2825   if (fd2 >= 0) {
2826     if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2827     if (addr && addrlen)
2828       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2829   }
2830   return fd2;
2831 }
2832 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2833 #else
2834 #define INIT_ACCEPT4
2835 #endif
2836
2837 #if SANITIZER_INTERCEPT_PACCEPT
2838 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2839             __sanitizer_sigset_t *set, int f) {
2840   void *ctx;
2841   COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2842   unsigned addrlen0 = 0;
2843   if (addrlen) {
2844     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2845     addrlen0 = *addrlen;
2846   }
2847   if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2848   int fd2 = REAL(paccept)(fd, addr, addrlen, set, 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_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2857 #else
2858 #define INIT_PACCEPT
2859 #endif
2860
2861 #if SANITIZER_INTERCEPT_MODF
2862 INTERCEPTOR(double, modf, double x, double *iptr) {
2863   void *ctx;
2864   COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2865   // FIXME: under ASan the call below may write to freed memory and corrupt
2866   // its metadata. See
2867   // https://github.com/google/sanitizers/issues/321.
2868   double res = REAL(modf)(x, iptr);
2869   if (iptr) {
2870     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2871   }
2872   return res;
2873 }
2874 INTERCEPTOR(float, modff, float x, float *iptr) {
2875   void *ctx;
2876   COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2877   // FIXME: under ASan the call below may write to freed memory and corrupt
2878   // its metadata. See
2879   // https://github.com/google/sanitizers/issues/321.
2880   float res = REAL(modff)(x, iptr);
2881   if (iptr) {
2882     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2883   }
2884   return res;
2885 }
2886 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2887   void *ctx;
2888   COMMON_INTERCEPTOR_ENTER(ctx, modfl, 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   long double res = REAL(modfl)(x, iptr);
2893   if (iptr) {
2894     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2895   }
2896   return res;
2897 }
2898 #define INIT_MODF                   \
2899   COMMON_INTERCEPT_FUNCTION(modf);  \
2900   COMMON_INTERCEPT_FUNCTION(modff); \
2901   COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2902 #else
2903 #define INIT_MODF
2904 #endif
2905
2906 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2907 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2908                          SSIZE_T maxlen) {
2909   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2910   if (msg->msg_name && msg->msg_namelen)
2911     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2912   if (msg->msg_iov && msg->msg_iovlen)
2913     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2914                                    sizeof(*msg->msg_iov) * msg->msg_iovlen);
2915   write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2916   if (msg->msg_control && msg->msg_controllen)
2917     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2918 }
2919 #endif
2920
2921 #if SANITIZER_INTERCEPT_RECVMSG
2922 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2923             int flags) {
2924   void *ctx;
2925   COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2926   // FIXME: under ASan the call below may write to freed memory and corrupt
2927   // its metadata. See
2928   // https://github.com/google/sanitizers/issues/321.
2929   SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2930   if (res >= 0) {
2931     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2932     if (msg) {
2933       write_msghdr(ctx, msg, res);
2934       COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2935     }
2936   }
2937   return res;
2938 }
2939 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2940 #else
2941 #define INIT_RECVMSG
2942 #endif
2943
2944 #if SANITIZER_INTERCEPT_RECVMMSG
2945 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2946             unsigned int vlen, int flags, void *timeout) {
2947   void *ctx;
2948   COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2949   if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2950   int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2951   if (res >= 0) {
2952     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2953     for (int i = 0; i < res; ++i) {
2954       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2955                                      sizeof(msgvec[i].msg_len));
2956       write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2957       COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2958     }
2959   }
2960   return res;
2961 }
2962 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2963 #else
2964 #define INIT_RECVMMSG
2965 #endif
2966
2967 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2968 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2969   const unsigned kCmsgDataOffset =
2970       RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2971
2972   char *p = (char *)control;
2973   char *const control_end = p + controllen;
2974   while (true) {
2975     if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2976     __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2977     COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2978
2979     if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2980
2981     COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2982                                   sizeof(cmsg->cmsg_level));
2983     COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2984                                   sizeof(cmsg->cmsg_type));
2985
2986     if (cmsg->cmsg_len > kCmsgDataOffset) {
2987       char *data = p + kCmsgDataOffset;
2988       unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2989       if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2990     }
2991
2992     p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2993   }
2994 }
2995
2996 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2997                         SSIZE_T maxlen) {
2998 #define R(f) \
2999   COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3000   R(name);
3001   R(namelen);
3002   R(iov);
3003   R(iovlen);
3004   R(control);
3005   R(controllen);
3006   R(flags);
3007 #undef R
3008   if (msg->msg_name && msg->msg_namelen)
3009     COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3010   if (msg->msg_iov && msg->msg_iovlen)
3011     COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3012                                   sizeof(*msg->msg_iov) * msg->msg_iovlen);
3013   read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3014   if (msg->msg_control && msg->msg_controllen)
3015     read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3016 }
3017 #endif
3018
3019 #if SANITIZER_INTERCEPT_SENDMSG
3020 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3021             int flags) {
3022   void *ctx;
3023   COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3024   if (fd >= 0) {
3025     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3026     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3027   }
3028   SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3029   if (common_flags()->intercept_send && res >= 0 && msg)
3030     read_msghdr(ctx, msg, res);
3031   return res;
3032 }
3033 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3034 #else
3035 #define INIT_SENDMSG
3036 #endif
3037
3038 #if SANITIZER_INTERCEPT_SENDMMSG
3039 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3040             unsigned vlen, int flags) {
3041   void *ctx;
3042   COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3043   if (fd >= 0) {
3044     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3045     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3046   }
3047   int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3048   if (res >= 0 && msgvec)
3049     for (int i = 0; i < res; ++i) {
3050       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3051                                      sizeof(msgvec[i].msg_len));
3052       if (common_flags()->intercept_send)
3053         read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3054     }
3055   return res;
3056 }
3057 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3058 #else
3059 #define INIT_SENDMMSG
3060 #endif
3061
3062 #if SANITIZER_INTERCEPT_GETPEERNAME
3063 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3064   void *ctx;
3065   COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3066   unsigned addr_sz;
3067   if (addrlen) addr_sz = *addrlen;
3068   // FIXME: under ASan the call below may write to freed memory and corrupt
3069   // its metadata. See
3070   // https://github.com/google/sanitizers/issues/321.
3071   int res = REAL(getpeername)(sockfd, addr, addrlen);
3072   if (!res && addr && addrlen)
3073     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3074   return res;
3075 }
3076 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3077 #else
3078 #define INIT_GETPEERNAME
3079 #endif
3080
3081 #if SANITIZER_INTERCEPT_SYSINFO
3082 INTERCEPTOR(int, sysinfo, void *info) {
3083   void *ctx;
3084   // FIXME: under ASan the call below may write to freed memory and corrupt
3085   // its metadata. See
3086   // https://github.com/google/sanitizers/issues/321.
3087   COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3088   int res = REAL(sysinfo)(info);
3089   if (!res && info)
3090     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3091   return res;
3092 }
3093 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3094 #else
3095 #define INIT_SYSINFO
3096 #endif
3097
3098 #if SANITIZER_INTERCEPT_READDIR
3099 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3100   void *ctx;
3101   COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3102   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3103   __sanitizer_dirent *res = REAL(opendir)(path);
3104   if (res)
3105     COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3106   return res;
3107 }
3108
3109 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3110   void *ctx;
3111   COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3112   // FIXME: under ASan the call below may write to freed memory and corrupt
3113   // its metadata. See
3114   // https://github.com/google/sanitizers/issues/321.
3115   __sanitizer_dirent *res = REAL(readdir)(dirp);
3116   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3117   return res;
3118 }
3119
3120 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3121             __sanitizer_dirent **result) {
3122   void *ctx;
3123   COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3124   // FIXME: under ASan the call below may write to freed memory and corrupt
3125   // its metadata. See
3126   // https://github.com/google/sanitizers/issues/321.
3127   int res = REAL(readdir_r)(dirp, entry, result);
3128   if (!res) {
3129     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3130     if (*result)
3131       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3132   }
3133   return res;
3134 }
3135
3136 #define INIT_READDIR                  \
3137   COMMON_INTERCEPT_FUNCTION(opendir); \
3138   COMMON_INTERCEPT_FUNCTION(readdir); \
3139   COMMON_INTERCEPT_FUNCTION(readdir_r);
3140 #else
3141 #define INIT_READDIR
3142 #endif
3143
3144 #if SANITIZER_INTERCEPT_READDIR64
3145 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3146   void *ctx;
3147   COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
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   __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3152   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3153   return res;
3154 }
3155
3156 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3157             __sanitizer_dirent64 **result) {
3158   void *ctx;
3159   COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3160   // FIXME: under ASan the call below may write to freed memory and corrupt
3161   // its metadata. See
3162   // https://github.com/google/sanitizers/issues/321.
3163   int res = REAL(readdir64_r)(dirp, entry, result);
3164   if (!res) {
3165     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3166     if (*result)
3167       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3168   }
3169   return res;
3170 }
3171 #define INIT_READDIR64                  \
3172   COMMON_INTERCEPT_FUNCTION(readdir64); \
3173   COMMON_INTERCEPT_FUNCTION(readdir64_r);
3174 #else
3175 #define INIT_READDIR64
3176 #endif
3177
3178 #if SANITIZER_INTERCEPT_PTRACE
3179 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3180   void *ctx;
3181   COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3182   __sanitizer_iovec local_iovec;
3183
3184   if (data) {
3185     if (request == ptrace_setregs)
3186       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3187     else if (request == ptrace_setfpregs)
3188       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3189     else if (request == ptrace_setfpxregs)
3190       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3191     else if (request == ptrace_setvfpregs)
3192       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3193     else if (request == ptrace_setsiginfo)
3194       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3195     // Some kernel might zero the iovec::iov_base in case of invalid
3196     // write access.  In this case copy the invalid address for further
3197     // inspection.
3198     else if (request == ptrace_setregset || request == ptrace_getregset) {
3199       __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3200       COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3201       local_iovec = *iovec;
3202       if (request == ptrace_setregset)
3203         COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3204     }
3205   }
3206
3207   // FIXME: under ASan the call below may write to freed memory and corrupt
3208   // its metadata. See
3209   // https://github.com/google/sanitizers/issues/321.
3210   uptr res = REAL(ptrace)(request, pid, addr, data);
3211
3212   if (!res && data) {
3213     // Note that PEEK* requests assign different meaning to the return value.
3214     // This function does not handle them (nor does it need to).
3215     if (request == ptrace_getregs)
3216       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3217     else if (request == ptrace_getfpregs)
3218       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3219     else if (request == ptrace_getfpxregs)
3220       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3221     else if (request == ptrace_getvfpregs)
3222       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3223     else if (request == ptrace_getsiginfo)
3224       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3225     else if (request == ptrace_geteventmsg)
3226       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3227     else if (request == ptrace_getregset) {
3228       __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3229       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3230       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3231                                      local_iovec.iov_len);
3232     }
3233   }
3234   return res;
3235 }
3236
3237 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3238 #else
3239 #define INIT_PTRACE
3240 #endif
3241
3242 #if SANITIZER_INTERCEPT_SETLOCALE
3243 static void unpoison_ctype_arrays(void *ctx) {
3244 #if SANITIZER_NETBSD
3245   // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3246   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3247   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3248   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3249 #endif
3250 }
3251
3252 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3253   void *ctx;
3254   COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3255   if (locale)
3256     COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3257   char *res = REAL(setlocale)(category, locale);
3258   if (res) {
3259     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3260     unpoison_ctype_arrays(ctx);
3261   }
3262   return res;
3263 }
3264
3265 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3266 #else
3267 #define INIT_SETLOCALE
3268 #endif
3269
3270 #if SANITIZER_INTERCEPT_GETCWD
3271 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3272   void *ctx;
3273   COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3274   // FIXME: under ASan the call below may write to freed memory and corrupt
3275   // its metadata. See
3276   // https://github.com/google/sanitizers/issues/321.
3277   char *res = REAL(getcwd)(buf, size);
3278   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3279   return res;
3280 }
3281 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3282 #else
3283 #define INIT_GETCWD
3284 #endif
3285
3286 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3287 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3288   void *ctx;
3289   COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3290   // FIXME: under ASan the call below may write to freed memory and corrupt
3291   // its metadata. See
3292   // https://github.com/google/sanitizers/issues/321.
3293   char *res = REAL(get_current_dir_name)(fake);
3294   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3295   return res;
3296 }
3297
3298 #define INIT_GET_CURRENT_DIR_NAME \
3299   COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3300 #else
3301 #define INIT_GET_CURRENT_DIR_NAME
3302 #endif
3303
3304 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3305   CHECK(endptr);
3306   if (nptr == *endptr) {
3307     // No digits were found at strtol call, we need to find out the last
3308     // symbol accessed by strtoll on our own.
3309     // We get this symbol by skipping leading blanks and optional +/- sign.
3310     while (IsSpace(*nptr)) nptr++;
3311     if (*nptr == '+' || *nptr == '-') nptr++;
3312     *endptr = const_cast<char *>(nptr);
3313   }
3314   CHECK(*endptr >= nptr);
3315 }
3316
3317 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3318                              char **endptr, char *real_endptr, int base) {
3319   if (endptr) {
3320     *endptr = real_endptr;
3321     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3322   }
3323   // If base has unsupported value, strtol can exit with EINVAL
3324   // without reading any characters. So do additional checks only
3325   // if base is valid.
3326   bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3327   if (is_valid_base) {
3328     FixRealStrtolEndptr(nptr, &real_endptr);
3329   }
3330   COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3331                                  (real_endptr - nptr) + 1 : 0);
3332 }
3333
3334
3335 #if SANITIZER_INTERCEPT_STRTOIMAX
3336 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3337   void *ctx;
3338   COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3339   // FIXME: under ASan the call below may write to freed memory and corrupt
3340   // its metadata. See
3341   // https://github.com/google/sanitizers/issues/321.
3342   char *real_endptr;
3343   INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3344   StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3345   return res;
3346 }
3347
3348 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3349   void *ctx;
3350   COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3351   // FIXME: under ASan the call below may write to freed memory and corrupt
3352   // its metadata. See
3353   // https://github.com/google/sanitizers/issues/321.
3354   char *real_endptr;
3355   INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3356   StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3357   return res;
3358 }
3359
3360 #define INIT_STRTOIMAX                  \
3361   COMMON_INTERCEPT_FUNCTION(strtoimax); \
3362   COMMON_INTERCEPT_FUNCTION(strtoumax);
3363 #else
3364 #define INIT_STRTOIMAX
3365 #endif
3366
3367 #if SANITIZER_INTERCEPT_MBSTOWCS
3368 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3369   void *ctx;
3370   COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3371   // FIXME: under ASan the call below may write to freed memory and corrupt
3372   // its metadata. See
3373   // https://github.com/google/sanitizers/issues/321.
3374   SIZE_T res = REAL(mbstowcs)(dest, src, len);
3375   if (res != (SIZE_T) - 1 && dest) {
3376     SIZE_T write_cnt = res + (res < len);
3377     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3378   }
3379   return res;
3380 }
3381
3382 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3383             void *ps) {
3384   void *ctx;
3385   COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3386   if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3387   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3388   // FIXME: under ASan the call below may write to freed memory and corrupt
3389   // its metadata. See
3390   // https://github.com/google/sanitizers/issues/321.
3391   SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3392   if (res != (SIZE_T)(-1) && dest && src) {
3393     // This function, and several others, may or may not write the terminating
3394     // \0 character. They write it iff they clear *src.
3395     SIZE_T write_cnt = res + !*src;
3396     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3397   }
3398   return res;
3399 }
3400
3401 #define INIT_MBSTOWCS                  \
3402   COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3403   COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3404 #else
3405 #define INIT_MBSTOWCS
3406 #endif
3407
3408 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3409 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3410             SIZE_T len, void *ps) {
3411   void *ctx;
3412   COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3413   if (src) {
3414     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3415     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3416   }
3417   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3418   // FIXME: under ASan the call below may write to freed memory and corrupt
3419   // its metadata. See
3420   // https://github.com/google/sanitizers/issues/321.
3421   SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3422   if (res != (SIZE_T)(-1) && dest && src) {
3423     SIZE_T write_cnt = res + !*src;
3424     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3425   }
3426   return res;
3427 }
3428
3429 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3430 #else
3431 #define INIT_MBSNRTOWCS
3432 #endif
3433
3434 #if SANITIZER_INTERCEPT_WCSTOMBS
3435 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3436   void *ctx;
3437   COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3438   // FIXME: under ASan the call below may write to freed memory and corrupt
3439   // its metadata. See
3440   // https://github.com/google/sanitizers/issues/321.
3441   SIZE_T res = REAL(wcstombs)(dest, src, len);
3442   if (res != (SIZE_T) - 1 && dest) {
3443     SIZE_T write_cnt = res + (res < len);
3444     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3445   }
3446   return res;
3447 }
3448
3449 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3450             void *ps) {
3451   void *ctx;
3452   COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3453   if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3454   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3455   // FIXME: under ASan the call below may write to freed memory and corrupt
3456   // its metadata. See
3457   // https://github.com/google/sanitizers/issues/321.
3458   SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3459   if (res != (SIZE_T) - 1 && dest && src) {
3460     SIZE_T write_cnt = res + !*src;
3461     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3462   }
3463   return res;
3464 }
3465
3466 #define INIT_WCSTOMBS                  \
3467   COMMON_INTERCEPT_FUNCTION(wcstombs); \
3468   COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3469 #else
3470 #define INIT_WCSTOMBS
3471 #endif
3472
3473 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3474 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3475             SIZE_T len, void *ps) {
3476   void *ctx;
3477   COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3478   if (src) {
3479     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3480     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3481   }
3482   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3483   // FIXME: under ASan the call below may write to freed memory and corrupt
3484   // its metadata. See
3485   // https://github.com/google/sanitizers/issues/321.
3486   SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3487   if (res != ((SIZE_T)-1) && dest && src) {
3488     SIZE_T write_cnt = res + !*src;
3489     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3490   }
3491   return res;
3492 }
3493
3494 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3495 #else
3496 #define INIT_WCSNRTOMBS
3497 #endif
3498
3499
3500 #if SANITIZER_INTERCEPT_WCRTOMB
3501 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3502   void *ctx;
3503   COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3504   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3505   // FIXME: under ASan the call below may write to freed memory and corrupt
3506   // its metadata. See
3507   // https://github.com/google/sanitizers/issues/321.
3508   SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3509   if (res != ((SIZE_T)-1) && dest) {
3510     SIZE_T write_cnt = res;
3511     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3512   }
3513   return res;
3514 }
3515
3516 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3517 #else
3518 #define INIT_WCRTOMB
3519 #endif
3520
3521 #if SANITIZER_INTERCEPT_TCGETATTR
3522 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3523   void *ctx;
3524   COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3525   // FIXME: under ASan the call below may write to freed memory and corrupt
3526   // its metadata. See
3527   // https://github.com/google/sanitizers/issues/321.
3528   int res = REAL(tcgetattr)(fd, termios_p);
3529   if (!res && termios_p)
3530     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3531   return res;
3532 }
3533
3534 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3535 #else
3536 #define INIT_TCGETATTR
3537 #endif
3538
3539 #if SANITIZER_INTERCEPT_REALPATH
3540 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3541   void *ctx;
3542   COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3543   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3544
3545   // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3546   // version of a versioned symbol. For realpath(), this gives us something
3547   // (called __old_realpath) that does not handle NULL in the second argument.
3548   // Handle it as part of the interceptor.
3549   char *allocated_path = nullptr;
3550   if (!resolved_path)
3551     allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3552
3553   char *res = REAL(realpath)(path, resolved_path);
3554   if (allocated_path && !res) WRAP(free)(allocated_path);
3555   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3556   return res;
3557 }
3558 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3559 #else
3560 #define INIT_REALPATH
3561 #endif
3562
3563 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3564 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3565   void *ctx;
3566   COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3567   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3568   char *res = REAL(canonicalize_file_name)(path);
3569   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3570   return res;
3571 }
3572 #define INIT_CANONICALIZE_FILE_NAME \
3573   COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3574 #else
3575 #define INIT_CANONICALIZE_FILE_NAME
3576 #endif
3577
3578 #if SANITIZER_INTERCEPT_CONFSTR
3579 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3580   void *ctx;
3581   COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3582   // FIXME: under ASan the call below may write to freed memory and corrupt
3583   // its metadata. See
3584   // https://github.com/google/sanitizers/issues/321.
3585   SIZE_T res = REAL(confstr)(name, buf, len);
3586   if (buf && res)
3587     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3588   return res;
3589 }
3590 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3591 #else
3592 #define INIT_CONFSTR
3593 #endif
3594
3595 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3596 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3597   void *ctx;
3598   COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3599   // FIXME: under ASan the call below may write to freed memory and corrupt
3600   // its metadata. See
3601   // https://github.com/google/sanitizers/issues/321.
3602   int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3603   if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3604   return res;
3605 }
3606 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3607 #else
3608 #define INIT_SCHED_GETAFFINITY
3609 #endif
3610
3611 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3612 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3613   void *ctx;
3614   COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3615   int res = REAL(sched_getparam)(pid, param);
3616   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3617   return res;
3618 }
3619 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3620 #else
3621 #define INIT_SCHED_GETPARAM
3622 #endif
3623
3624 #if SANITIZER_INTERCEPT_STRERROR
3625 INTERCEPTOR(char *, strerror, int errnum) {
3626   void *ctx;
3627   COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3628   char *res = REAL(strerror)(errnum);
3629   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3630   return res;
3631 }
3632 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3633 #else
3634 #define INIT_STRERROR
3635 #endif
3636
3637 #if SANITIZER_INTERCEPT_STRERROR_R
3638 // There are 2 versions of strerror_r:
3639 //  * POSIX version returns 0 on success, negative error code on failure,
3640 //    writes message to buf.
3641 //  * GNU version returns message pointer, which points to either buf or some
3642 //    static storage.
3643 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3644     SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD ||                 \
3645     SANITIZER_FREEBSD || SANITIZER_OPENBSD
3646 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3647 // At least on OSX, buf contents are valid even when the call fails.
3648 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3649   void *ctx;
3650   COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3651   // FIXME: under ASan the call below may write to freed memory and corrupt
3652   // its metadata. See
3653   // https://github.com/google/sanitizers/issues/321.
3654   int res = REAL(strerror_r)(errnum, buf, buflen);
3655
3656   SIZE_T sz = internal_strnlen(buf, buflen);
3657   if (sz < buflen) ++sz;
3658   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3659   return res;
3660 }
3661 #else
3662 // GNU version.
3663 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3664   void *ctx;
3665   COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3666   // FIXME: under ASan the call below may write to freed memory and corrupt
3667   // its metadata. See
3668   // https://github.com/google/sanitizers/issues/321.
3669   char *res = REAL(strerror_r)(errnum, buf, buflen);
3670   if (res == buf)
3671     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3672   else
3673     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3674   return res;
3675 }
3676 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3677        //SANITIZER_MAC
3678 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3679 #else
3680 #define INIT_STRERROR_R
3681 #endif
3682
3683 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3684 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3685   void *ctx;
3686   COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3687   // FIXME: under ASan the call below may write to freed memory and corrupt
3688   // its metadata. See
3689   // https://github.com/google/sanitizers/issues/321.
3690   int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3691   // This version always returns a null-terminated string.
3692   if (buf && buflen)
3693     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3694   return res;
3695 }
3696 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3697 #else
3698 #define INIT_XPG_STRERROR_R
3699 #endif
3700
3701 #if SANITIZER_INTERCEPT_SCANDIR
3702 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3703 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3704                                 const struct __sanitizer_dirent **);
3705
3706 static THREADLOCAL scandir_filter_f scandir_filter;
3707 static THREADLOCAL scandir_compar_f scandir_compar;
3708
3709 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3710   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3711   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3712   return scandir_filter(dir);
3713 }
3714
3715 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3716                                   const struct __sanitizer_dirent **b) {
3717   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3718   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3719   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3720   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3721   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3722   return scandir_compar(a, b);
3723 }
3724
3725 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3726             scandir_filter_f filter, scandir_compar_f compar) {
3727   void *ctx;
3728   COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3729   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3730   scandir_filter = filter;
3731   scandir_compar = compar;
3732   // FIXME: under ASan the call below may write to freed memory and corrupt
3733   // its metadata. See
3734   // https://github.com/google/sanitizers/issues/321.
3735   int res = REAL(scandir)(dirp, namelist,
3736                           filter ? wrapped_scandir_filter : nullptr,
3737                           compar ? wrapped_scandir_compar : nullptr);
3738   scandir_filter = nullptr;
3739   scandir_compar = nullptr;
3740   if (namelist && res > 0) {
3741     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3742     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3743     for (int i = 0; i < res; ++i)
3744       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3745                                      (*namelist)[i]->d_reclen);
3746   }
3747   return res;
3748 }
3749 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3750 #else
3751 #define INIT_SCANDIR
3752 #endif
3753
3754 #if SANITIZER_INTERCEPT_SCANDIR64
3755 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3756 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3757                                   const struct __sanitizer_dirent64 **);
3758
3759 static THREADLOCAL scandir64_filter_f scandir64_filter;
3760 static THREADLOCAL scandir64_compar_f scandir64_compar;
3761
3762 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3763   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3764   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3765   return scandir64_filter(dir);
3766 }
3767
3768 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3769                                     const struct __sanitizer_dirent64 **b) {
3770   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3771   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3772   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3773   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3774   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3775   return scandir64_compar(a, b);
3776 }
3777
3778 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3779             scandir64_filter_f filter, scandir64_compar_f compar) {
3780   void *ctx;
3781   COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3782   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3783   scandir64_filter = filter;
3784   scandir64_compar = compar;
3785   // FIXME: under ASan the call below may write to freed memory and corrupt
3786   // its metadata. See
3787   // https://github.com/google/sanitizers/issues/321.
3788   int res =
3789       REAL(scandir64)(dirp, namelist,
3790                       filter ? wrapped_scandir64_filter : nullptr,
3791                       compar ? wrapped_scandir64_compar : nullptr);
3792   scandir64_filter = nullptr;
3793   scandir64_compar = nullptr;
3794   if (namelist && res > 0) {
3795     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3796     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3797     for (int i = 0; i < res; ++i)
3798       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3799                                      (*namelist)[i]->d_reclen);
3800   }
3801   return res;
3802 }
3803 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3804 #else
3805 #define INIT_SCANDIR64
3806 #endif
3807
3808 #if SANITIZER_INTERCEPT_GETGROUPS
3809 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3810   void *ctx;
3811   COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3812   // FIXME: under ASan the call below may write to freed memory and corrupt
3813   // its metadata. See
3814   // https://github.com/google/sanitizers/issues/321.
3815   int res = REAL(getgroups)(size, lst);
3816   if (res >= 0 && lst && size > 0)
3817     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3818   return res;
3819 }
3820 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3821 #else
3822 #define INIT_GETGROUPS
3823 #endif
3824
3825 #if SANITIZER_INTERCEPT_POLL
3826 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3827                         __sanitizer_nfds_t nfds) {
3828   for (unsigned i = 0; i < nfds; ++i) {
3829     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3830     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3831   }
3832 }
3833
3834 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3835                          __sanitizer_nfds_t nfds) {
3836   for (unsigned i = 0; i < nfds; ++i)
3837     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3838                                    sizeof(fds[i].revents));
3839 }
3840
3841 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3842             int timeout) {
3843   void *ctx;
3844   COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3845   if (fds && nfds) read_pollfd(ctx, fds, nfds);
3846   int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3847   if (fds && nfds) write_pollfd(ctx, fds, nfds);
3848   return res;
3849 }
3850 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3851 #else
3852 #define INIT_POLL
3853 #endif
3854
3855 #if SANITIZER_INTERCEPT_PPOLL
3856 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3857             void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3858   void *ctx;
3859   COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3860   if (fds && nfds) read_pollfd(ctx, fds, nfds);
3861   if (timeout_ts)
3862     COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3863   if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3864   int res =
3865       COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3866   if (fds && nfds) write_pollfd(ctx, fds, nfds);
3867   return res;
3868 }
3869 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3870 #else
3871 #define INIT_PPOLL
3872 #endif
3873
3874 #if SANITIZER_INTERCEPT_WORDEXP
3875 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3876   void *ctx;
3877   COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3878   if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3879   // FIXME: under ASan the call below may write to freed memory and corrupt
3880   // its metadata. See
3881   // https://github.com/google/sanitizers/issues/321.
3882   int res = REAL(wordexp)(s, p, flags);
3883   if (!res && p) {
3884     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3885     if (p->we_wordc)
3886       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3887                                      sizeof(*p->we_wordv) * p->we_wordc);
3888     for (uptr i = 0; i < p->we_wordc; ++i) {
3889       char *w = p->we_wordv[i];
3890       if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3891     }
3892   }
3893   return res;
3894 }
3895 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3896 #else
3897 #define INIT_WORDEXP
3898 #endif
3899
3900 #if SANITIZER_INTERCEPT_SIGWAIT
3901 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3902   void *ctx;
3903   COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3904   if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3905   // FIXME: under ASan the call below may write to freed memory and corrupt
3906   // its metadata. See
3907   // https://github.com/google/sanitizers/issues/321.
3908   int res = REAL(sigwait)(set, sig);
3909   if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3910   return res;
3911 }
3912 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3913 #else
3914 #define INIT_SIGWAIT
3915 #endif
3916
3917 #if SANITIZER_INTERCEPT_SIGWAITINFO
3918 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3919   void *ctx;
3920   COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3921   if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3922   // FIXME: under ASan the call below may write to freed memory and corrupt
3923   // its metadata. See
3924   // https://github.com/google/sanitizers/issues/321.
3925   int res = REAL(sigwaitinfo)(set, info);
3926   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3927   return res;
3928 }
3929 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3930 #else
3931 #define INIT_SIGWAITINFO
3932 #endif
3933
3934 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3935 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3936             void *timeout) {
3937   void *ctx;
3938   COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3939   if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3940   if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3941   // FIXME: under ASan the call below may write to freed memory and corrupt
3942   // its metadata. See
3943   // https://github.com/google/sanitizers/issues/321.
3944   int res = REAL(sigtimedwait)(set, info, timeout);
3945   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3946   return res;
3947 }
3948 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3949 #else
3950 #define INIT_SIGTIMEDWAIT
3951 #endif
3952
3953 #if SANITIZER_INTERCEPT_SIGSETOPS
3954 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3955   void *ctx;
3956   COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3957   // FIXME: under ASan the call below may write to freed memory and corrupt
3958   // its metadata. See
3959   // https://github.com/google/sanitizers/issues/321.
3960   int res = REAL(sigemptyset)(set);
3961   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3962   return res;
3963 }
3964
3965 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3966   void *ctx;
3967   COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3968   // FIXME: under ASan the call below may write to freed memory and corrupt
3969   // its metadata. See
3970   // https://github.com/google/sanitizers/issues/321.
3971   int res = REAL(sigfillset)(set);
3972   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3973   return res;
3974 }
3975 #define INIT_SIGSETOPS                    \
3976   COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3977   COMMON_INTERCEPT_FUNCTION(sigfillset);
3978 #else
3979 #define INIT_SIGSETOPS
3980 #endif
3981
3982 #if SANITIZER_INTERCEPT_SIGPENDING
3983 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3984   void *ctx;
3985   COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3986   // FIXME: under ASan the call below may write to freed memory and corrupt
3987   // its metadata. See
3988   // https://github.com/google/sanitizers/issues/321.
3989   int res = REAL(sigpending)(set);
3990   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3991   return res;
3992 }
3993 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3994 #else
3995 #define INIT_SIGPENDING
3996 #endif
3997
3998 #if SANITIZER_INTERCEPT_SIGPROCMASK
3999 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4000             __sanitizer_sigset_t *oldset) {
4001   void *ctx;
4002   COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4003   if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4004   // FIXME: under ASan the call below may write to freed memory and corrupt
4005   // its metadata. See
4006   // https://github.com/google/sanitizers/issues/321.
4007   int res = REAL(sigprocmask)(how, set, oldset);
4008   if (!res && oldset)
4009     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4010   return res;
4011 }
4012 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4013 #else
4014 #define INIT_SIGPROCMASK
4015 #endif
4016
4017 #if SANITIZER_INTERCEPT_BACKTRACE
4018 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4019   void *ctx;
4020   COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4021   // FIXME: under ASan the call below may write to freed memory and corrupt
4022   // its metadata. See
4023   // https://github.com/google/sanitizers/issues/321.
4024   int res = REAL(backtrace)(buffer, size);
4025   if (res && buffer)
4026     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4027   return res;
4028 }
4029
4030 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4031   void *ctx;
4032   COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4033   if (buffer && size)
4034     COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
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   char **res = REAL(backtrace_symbols)(buffer, size);
4039   if (res && size) {
4040     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4041     for (int i = 0; i < size; ++i)
4042       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4043   }
4044   return res;
4045 }
4046 #define INIT_BACKTRACE                  \
4047   COMMON_INTERCEPT_FUNCTION(backtrace); \
4048   COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4049 #else
4050 #define INIT_BACKTRACE
4051 #endif
4052
4053 #if SANITIZER_INTERCEPT__EXIT
4054 INTERCEPTOR(void, _exit, int status) {
4055   void *ctx;
4056   COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4057   COMMON_INTERCEPTOR_USER_CALLBACK_START();
4058   int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4059   COMMON_INTERCEPTOR_USER_CALLBACK_END();
4060   if (status == 0) status = status1;
4061   REAL(_exit)(status);
4062 }
4063 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4064 #else
4065 #define INIT__EXIT
4066 #endif
4067
4068 #if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4069 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4070   void *ctx;
4071   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4072   COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4073   int res = REAL(pthread_mutex_lock)(m);
4074   if (res == errno_EOWNERDEAD)
4075     COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4076   if (res == 0 || res == errno_EOWNERDEAD)
4077     COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4078   if (res == errno_EINVAL)
4079     COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4080   return res;
4081 }
4082
4083 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4084   void *ctx;
4085   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4086   COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4087   int res = REAL(pthread_mutex_unlock)(m);
4088   if (res == errno_EINVAL)
4089     COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4090   return res;
4091 }
4092
4093 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4094 #define INIT_PTHREAD_MUTEX_UNLOCK \
4095   COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4096 #else
4097 #define INIT_PTHREAD_MUTEX_LOCK
4098 #define INIT_PTHREAD_MUTEX_UNLOCK
4099 #endif
4100
4101 #if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4102 INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4103   return WRAP(pthread_mutex_lock)(m);
4104 }
4105
4106 INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4107   return WRAP(pthread_mutex_unlock)(m);
4108 }
4109
4110 #define INIT___PTHREAD_MUTEX_LOCK \
4111   COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4112 #define INIT___PTHREAD_MUTEX_UNLOCK \
4113   COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4114 #else
4115 #define INIT___PTHREAD_MUTEX_LOCK
4116 #define INIT___PTHREAD_MUTEX_UNLOCK
4117 #endif
4118
4119 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4120 INTERCEPTOR(int, __libc_mutex_lock, void *m)
4121 ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4122
4123 INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4124 ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4125
4126 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4127 ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4128
4129 #define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4130 #define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4131 #define INIT___LIBC_THR_SETCANCELSTATE \
4132   COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4133 #else
4134 #define INIT___LIBC_MUTEX_LOCK
4135 #define INIT___LIBC_MUTEX_UNLOCK
4136 #define INIT___LIBC_THR_SETCANCELSTATE
4137 #endif
4138
4139 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4140 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4141   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4142   if (mnt->mnt_fsname)
4143     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4144                                    REAL(strlen)(mnt->mnt_fsname) + 1);
4145   if (mnt->mnt_dir)
4146     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4147                                    REAL(strlen)(mnt->mnt_dir) + 1);
4148   if (mnt->mnt_type)
4149     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4150                                    REAL(strlen)(mnt->mnt_type) + 1);
4151   if (mnt->mnt_opts)
4152     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4153                                    REAL(strlen)(mnt->mnt_opts) + 1);
4154 }
4155 #endif
4156
4157 #if SANITIZER_INTERCEPT_GETMNTENT
4158 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4159   void *ctx;
4160   COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4161   __sanitizer_mntent *res = REAL(getmntent)(fp);
4162   if (res) write_mntent(ctx, res);
4163   return res;
4164 }
4165 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4166 #else
4167 #define INIT_GETMNTENT
4168 #endif
4169
4170 #if SANITIZER_INTERCEPT_GETMNTENT_R
4171 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4172             __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4173   void *ctx;
4174   COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4175   __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4176   if (res) write_mntent(ctx, res);
4177   return res;
4178 }
4179 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4180 #else
4181 #define INIT_GETMNTENT_R
4182 #endif
4183
4184 #if SANITIZER_INTERCEPT_STATFS
4185 INTERCEPTOR(int, statfs, char *path, void *buf) {
4186   void *ctx;
4187   COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4188   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4189   // FIXME: under ASan the call below may write to freed memory and corrupt
4190   // its metadata. See
4191   // https://github.com/google/sanitizers/issues/321.
4192   int res = REAL(statfs)(path, buf);
4193   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4194   return res;
4195 }
4196 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4197   void *ctx;
4198   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4199   // FIXME: under ASan the call below may write to freed memory and corrupt
4200   // its metadata. See
4201   // https://github.com/google/sanitizers/issues/321.
4202   int res = REAL(fstatfs)(fd, buf);
4203   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4204   return res;
4205 }
4206 #define INIT_STATFS                  \
4207   COMMON_INTERCEPT_FUNCTION(statfs); \
4208   COMMON_INTERCEPT_FUNCTION(fstatfs);
4209 #else
4210 #define INIT_STATFS
4211 #endif
4212
4213 #if SANITIZER_INTERCEPT_STATFS64
4214 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4215   void *ctx;
4216   COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4217   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4218   // FIXME: under ASan the call below may write to freed memory and corrupt
4219   // its metadata. See
4220   // https://github.com/google/sanitizers/issues/321.
4221   int res = REAL(statfs64)(path, buf);
4222   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4223   return res;
4224 }
4225 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4226   void *ctx;
4227   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4228   // FIXME: under ASan the call below may write to freed memory and corrupt
4229   // its metadata. See
4230   // https://github.com/google/sanitizers/issues/321.
4231   int res = REAL(fstatfs64)(fd, buf);
4232   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4233   return res;
4234 }
4235 #define INIT_STATFS64                  \
4236   COMMON_INTERCEPT_FUNCTION(statfs64); \
4237   COMMON_INTERCEPT_FUNCTION(fstatfs64);
4238 #else
4239 #define INIT_STATFS64
4240 #endif
4241
4242 #if SANITIZER_INTERCEPT_STATVFS
4243 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4244   void *ctx;
4245   COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4246   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4247   // FIXME: under ASan the call below may write to freed memory and corrupt
4248   // its metadata. See
4249   // https://github.com/google/sanitizers/issues/321.
4250   int res = REAL(statvfs)(path, buf);
4251   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4252   return res;
4253 }
4254 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4255   void *ctx;
4256   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
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(fstatvfs)(fd, buf);
4261   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4262   return res;
4263 }
4264 #define INIT_STATVFS                  \
4265   COMMON_INTERCEPT_FUNCTION(statvfs); \
4266   COMMON_INTERCEPT_FUNCTION(fstatvfs);
4267 #else
4268 #define INIT_STATVFS
4269 #endif
4270
4271 #if SANITIZER_INTERCEPT_STATVFS64
4272 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4273   void *ctx;
4274   COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4275   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4276   // FIXME: under ASan the call below may write to freed memory and corrupt
4277   // its metadata. See
4278   // https://github.com/google/sanitizers/issues/321.
4279   int res = REAL(statvfs64)(path, buf);
4280   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4281   return res;
4282 }
4283 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4284   void *ctx;
4285   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
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(fstatvfs64)(fd, buf);
4290   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4291   return res;
4292 }
4293 #define INIT_STATVFS64                  \
4294   COMMON_INTERCEPT_FUNCTION(statvfs64); \
4295   COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4296 #else
4297 #define INIT_STATVFS64
4298 #endif
4299
4300 #if SANITIZER_INTERCEPT_INITGROUPS
4301 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4302   void *ctx;
4303   COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4304   if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4305   int res = REAL(initgroups)(user, group);
4306   return res;
4307 }
4308 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4309 #else
4310 #define INIT_INITGROUPS
4311 #endif
4312
4313 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4314 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4315   void *ctx;
4316   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4317   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4318   char *res = REAL(ether_ntoa)(addr);
4319   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4320   return res;
4321 }
4322 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4323   void *ctx;
4324   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4325   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4326   __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4327   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4328   return res;
4329 }
4330 #define INIT_ETHER_NTOA_ATON             \
4331   COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4332   COMMON_INTERCEPT_FUNCTION(ether_aton);
4333 #else
4334 #define INIT_ETHER_NTOA_ATON
4335 #endif
4336
4337 #if SANITIZER_INTERCEPT_ETHER_HOST
4338 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4339   void *ctx;
4340   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4341   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4342   // FIXME: under ASan the call below may write to freed memory and corrupt
4343   // its metadata. See
4344   // https://github.com/google/sanitizers/issues/321.
4345   int res = REAL(ether_ntohost)(hostname, addr);
4346   if (!res && hostname)
4347     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4348   return res;
4349 }
4350 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4351   void *ctx;
4352   COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4353   if (hostname)
4354     COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4355   // FIXME: under ASan the call below may write to freed memory and corrupt
4356   // its metadata. See
4357   // https://github.com/google/sanitizers/issues/321.
4358   int res = REAL(ether_hostton)(hostname, addr);
4359   if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4360   return res;
4361 }
4362 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4363             char *hostname) {
4364   void *ctx;
4365   COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4366   if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4367   // FIXME: under ASan the call below may write to freed memory and corrupt
4368   // its metadata. See
4369   // https://github.com/google/sanitizers/issues/321.
4370   int res = REAL(ether_line)(line, addr, hostname);
4371   if (!res) {
4372     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4373     if (hostname)
4374       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4375   }
4376   return res;
4377 }
4378 #define INIT_ETHER_HOST                     \
4379   COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4380   COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4381   COMMON_INTERCEPT_FUNCTION(ether_line);
4382 #else
4383 #define INIT_ETHER_HOST
4384 #endif
4385
4386 #if SANITIZER_INTERCEPT_ETHER_R
4387 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4388   void *ctx;
4389   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4390   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4391   // FIXME: under ASan the call below may write to freed memory and corrupt
4392   // its metadata. See
4393   // https://github.com/google/sanitizers/issues/321.
4394   char *res = REAL(ether_ntoa_r)(addr, buf);
4395   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4396   return res;
4397 }
4398 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4399             __sanitizer_ether_addr *addr) {
4400   void *ctx;
4401   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4402   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4403   // FIXME: under ASan the call below may write to freed memory and corrupt
4404   // its metadata. See
4405   // https://github.com/google/sanitizers/issues/321.
4406   __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4407   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4408   return res;
4409 }
4410 #define INIT_ETHER_R                       \
4411   COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4412   COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4413 #else
4414 #define INIT_ETHER_R
4415 #endif
4416
4417 #if SANITIZER_INTERCEPT_SHMCTL
4418 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4419   void *ctx;
4420   COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4421   // FIXME: under ASan the call below may write to freed memory and corrupt
4422   // its metadata. See
4423   // https://github.com/google/sanitizers/issues/321.
4424   int res = REAL(shmctl)(shmid, cmd, buf);
4425   if (res >= 0) {
4426     unsigned sz = 0;
4427     if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4428       sz = sizeof(__sanitizer_shmid_ds);
4429     else if (cmd == shmctl_ipc_info)
4430       sz = struct_shminfo_sz;
4431     else if (cmd == shmctl_shm_info)
4432       sz = struct_shm_info_sz;
4433     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4434   }
4435   return res;
4436 }
4437 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4438 #else
4439 #define INIT_SHMCTL
4440 #endif
4441
4442 #if SANITIZER_INTERCEPT_RANDOM_R
4443 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4444   void *ctx;
4445   COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4446   // FIXME: under ASan the call below may write to freed memory and corrupt
4447   // its metadata. See
4448   // https://github.com/google/sanitizers/issues/321.
4449   int res = REAL(random_r)(buf, result);
4450   if (!res && result)
4451     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4452   return res;
4453 }
4454 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4455 #else
4456 #define INIT_RANDOM_R
4457 #endif
4458
4459 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4460 // its metadata. See
4461 // https://github.com/google/sanitizers/issues/321.
4462 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET ||              \
4463     SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED ||        \
4464     SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4465     SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET ||         \
4466     SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET ||        \
4467     SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET ||          \
4468     SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4469 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz)            \
4470   INTERCEPTOR(int, fn, void *attr, void *r) {                  \
4471     void *ctx;                                                 \
4472     COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r);                \
4473     int res = REAL(fn)(attr, r);                               \
4474     if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4475     return res;                                                \
4476   }
4477 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4478   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4479 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4480   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4481 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4482   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4483 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4484   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4485 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4486   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4487 #endif
4488
4489 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4490 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4491 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4492 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4493 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4494 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4495   void *ctx;
4496   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4497   // FIXME: under ASan the call below may write to freed memory and corrupt
4498   // its metadata. See
4499   // https://github.com/google/sanitizers/issues/321.
4500   int res = REAL(pthread_attr_getstack)(attr, addr, size);
4501   if (!res) {
4502     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4503     if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4504   }
4505   return res;
4506 }
4507
4508 // We may need to call the real pthread_attr_getstack from the run-time
4509 // in sanitizer_common, but we don't want to include the interception headers
4510 // there. So, just define this function here.
4511 namespace __sanitizer {
4512 extern "C" {
4513 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4514   return REAL(pthread_attr_getstack)(attr, addr, size);
4515 }
4516 }  // extern "C"
4517 }  // namespace __sanitizer
4518
4519 #define INIT_PTHREAD_ATTR_GET                             \
4520   COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4521   COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize);   \
4522   COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope);       \
4523   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize);   \
4524   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4525 #else
4526 #define INIT_PTHREAD_ATTR_GET
4527 #endif
4528
4529 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4530 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4531 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4532
4533 #define INIT_PTHREAD_ATTR_GET_SCHED                      \
4534   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4535   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4536 #else
4537 #define INIT_PTHREAD_ATTR_GET_SCHED
4538 #endif
4539
4540 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4541 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4542
4543 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4544   COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4545 #else
4546 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4547 #endif
4548
4549 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4550 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4551             void *cpuset) {
4552   void *ctx;
4553   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4554                            cpuset);
4555   // FIXME: under ASan the call below may write to freed memory and corrupt
4556   // its metadata. See
4557   // https://github.com/google/sanitizers/issues/321.
4558   int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4559   if (!res && cpusetsize && cpuset)
4560     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4561   return res;
4562 }
4563
4564 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4565   COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4566 #else
4567 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4568 #endif
4569
4570 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4571 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4572 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4573   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4574 #else
4575 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4576 #endif
4577
4578 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4579 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4580 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4581   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4582 #else
4583 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4584 #endif
4585
4586 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4587 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4588 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4589   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4590 #else
4591 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4592 #endif
4593
4594 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4595 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4596 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4597   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4598 #else
4599 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4600 #endif
4601
4602 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4603 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4604 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4605   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4606 #else
4607 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4608 #endif
4609
4610 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4611 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4612 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4613   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4614 #else
4615 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4616 #endif
4617
4618 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4619 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4620 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4621   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4622 #else
4623 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4624 #endif
4625
4626 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4627 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4628 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4629   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4630 #else
4631 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4632 #endif
4633
4634 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4635 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4636 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4637   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4638 #else
4639 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4640 #endif
4641
4642 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4643 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4644 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4645   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4646 #else
4647 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4648 #endif
4649
4650 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4651 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4652 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4653   COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4654 #else
4655 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4656 #endif
4657
4658 #if SANITIZER_INTERCEPT_TMPNAM
4659 INTERCEPTOR(char *, tmpnam, char *s) {
4660   void *ctx;
4661   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4662   char *res = REAL(tmpnam)(s);
4663   if (res) {
4664     if (s)
4665       // FIXME: under ASan the call below may write to freed memory and corrupt
4666       // its metadata. See
4667       // https://github.com/google/sanitizers/issues/321.
4668       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4669     else
4670       COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4671   }
4672   return res;
4673 }
4674 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4675 #else
4676 #define INIT_TMPNAM
4677 #endif
4678
4679 #if SANITIZER_INTERCEPT_TMPNAM_R
4680 INTERCEPTOR(char *, tmpnam_r, char *s) {
4681   void *ctx;
4682   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4683   // FIXME: under ASan the call below may write to freed memory and corrupt
4684   // its metadata. See
4685   // https://github.com/google/sanitizers/issues/321.
4686   char *res = REAL(tmpnam_r)(s);
4687   if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4688   return res;
4689 }
4690 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4691 #else
4692 #define INIT_TMPNAM_R
4693 #endif
4694
4695 #if SANITIZER_INTERCEPT_TTYNAME_R
4696 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4697   void *ctx;
4698   COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4699   int res = REAL(ttyname_r)(fd, name, namesize);
4700   if (res == 0)
4701     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4702   return res;
4703 }
4704 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4705 #else
4706 #define INIT_TTYNAME_R
4707 #endif
4708
4709 #if SANITIZER_INTERCEPT_TEMPNAM
4710 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4711   void *ctx;
4712   COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4713   if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4714   if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4715   char *res = REAL(tempnam)(dir, pfx);
4716   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4717   return res;
4718 }
4719 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4720 #else
4721 #define INIT_TEMPNAM
4722 #endif
4723
4724 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4725 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4726   void *ctx;
4727   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4728   COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4729   COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4730   return REAL(pthread_setname_np)(thread, name);
4731 }
4732 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4733 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4734 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4735   void *ctx;
4736   char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4737   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4738   COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4739   internal_snprintf(newname, sizeof(newname), name, arg);
4740   COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4741   return REAL(pthread_setname_np)(thread, name, arg);
4742 }
4743 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4744 #else
4745 #define INIT_PTHREAD_SETNAME_NP
4746 #endif
4747
4748 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4749 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4750   void *ctx;
4751   COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4752   int res = REAL(pthread_getname_np)(thread, name, len);
4753   if (!res)
4754     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4755   return res;
4756 }
4757 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4758 #else
4759 #define INIT_PTHREAD_GETNAME_NP
4760 #endif
4761
4762 #if SANITIZER_INTERCEPT_SINCOS
4763 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4764   void *ctx;
4765   COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4766   // FIXME: under ASan the call below may write to freed memory and corrupt
4767   // its metadata. See
4768   // https://github.com/google/sanitizers/issues/321.
4769   REAL(sincos)(x, sin, cos);
4770   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4771   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4772 }
4773 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4774   void *ctx;
4775   COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4776   // FIXME: under ASan the call below may write to freed memory and corrupt
4777   // its metadata. See
4778   // https://github.com/google/sanitizers/issues/321.
4779   REAL(sincosf)(x, sin, cos);
4780   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4781   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4782 }
4783 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4784   void *ctx;
4785   COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4786   // FIXME: under ASan the call below may write to freed memory and corrupt
4787   // its metadata. See
4788   // https://github.com/google/sanitizers/issues/321.
4789   REAL(sincosl)(x, sin, cos);
4790   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4791   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4792 }
4793 #define INIT_SINCOS                   \
4794   COMMON_INTERCEPT_FUNCTION(sincos);  \
4795   COMMON_INTERCEPT_FUNCTION(sincosf); \
4796   COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4797 #else
4798 #define INIT_SINCOS
4799 #endif
4800
4801 #if SANITIZER_INTERCEPT_REMQUO
4802 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4803   void *ctx;
4804   COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4805   // FIXME: under ASan the call below may write to freed memory and corrupt
4806   // its metadata. See
4807   // https://github.com/google/sanitizers/issues/321.
4808   double res = REAL(remquo)(x, y, quo);
4809   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4810   return res;
4811 }
4812 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4813   void *ctx;
4814   COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4815   // FIXME: under ASan the call below may write to freed memory and corrupt
4816   // its metadata. See
4817   // https://github.com/google/sanitizers/issues/321.
4818   float res = REAL(remquof)(x, y, quo);
4819   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4820   return res;
4821 }
4822 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4823   void *ctx;
4824   COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4825   // FIXME: under ASan the call below may write to freed memory and corrupt
4826   // its metadata. See
4827   // https://github.com/google/sanitizers/issues/321.
4828   long double res = REAL(remquol)(x, y, quo);
4829   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4830   return res;
4831 }
4832 #define INIT_REMQUO                   \
4833   COMMON_INTERCEPT_FUNCTION(remquo);  \
4834   COMMON_INTERCEPT_FUNCTION(remquof); \
4835   COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4836 #else
4837 #define INIT_REMQUO
4838 #endif
4839
4840 #if SANITIZER_INTERCEPT_LGAMMA
4841 extern int signgam;
4842 INTERCEPTOR(double, lgamma, double x) {
4843   void *ctx;
4844   COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4845   double res = REAL(lgamma)(x);
4846   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4847   return res;
4848 }
4849 INTERCEPTOR(float, lgammaf, float x) {
4850   void *ctx;
4851   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4852   float res = REAL(lgammaf)(x);
4853   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4854   return res;
4855 }
4856 INTERCEPTOR(long double, lgammal, long double x) {
4857   void *ctx;
4858   COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4859   long double res = REAL(lgammal)(x);
4860   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4861   return res;
4862 }
4863 #define INIT_LGAMMA                   \
4864   COMMON_INTERCEPT_FUNCTION(lgamma);  \
4865   COMMON_INTERCEPT_FUNCTION(lgammaf); \
4866   COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4867 #else
4868 #define INIT_LGAMMA
4869 #endif
4870
4871 #if SANITIZER_INTERCEPT_LGAMMA_R
4872 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4873   void *ctx;
4874   COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4875   // FIXME: under ASan the call below may write to freed memory and corrupt
4876   // its metadata. See
4877   // https://github.com/google/sanitizers/issues/321.
4878   double res = REAL(lgamma_r)(x, signp);
4879   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4880   return res;
4881 }
4882 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4883   void *ctx;
4884   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4885   // FIXME: under ASan the call below may write to freed memory and corrupt
4886   // its metadata. See
4887   // https://github.com/google/sanitizers/issues/321.
4888   float res = REAL(lgammaf_r)(x, signp);
4889   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4890   return res;
4891 }
4892 #define INIT_LGAMMA_R                   \
4893   COMMON_INTERCEPT_FUNCTION(lgamma_r);  \
4894   COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4895 #else
4896 #define INIT_LGAMMA_R
4897 #endif
4898
4899 #if SANITIZER_INTERCEPT_LGAMMAL_R
4900 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4901   void *ctx;
4902   COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4903   // FIXME: under ASan the call below may write to freed memory and corrupt
4904   // its metadata. See
4905   // https://github.com/google/sanitizers/issues/321.
4906   long double res = REAL(lgammal_r)(x, signp);
4907   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4908   return res;
4909 }
4910 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4911 #else
4912 #define INIT_LGAMMAL_R
4913 #endif
4914
4915 #if SANITIZER_INTERCEPT_DRAND48_R
4916 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4917   void *ctx;
4918   COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4919   // FIXME: under ASan the call below may write to freed memory and corrupt
4920   // its metadata. See
4921   // https://github.com/google/sanitizers/issues/321.
4922   int res = REAL(drand48_r)(buffer, result);
4923   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4924   return res;
4925 }
4926 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4927   void *ctx;
4928   COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4929   // FIXME: under ASan the call below may write to freed memory and corrupt
4930   // its metadata. See
4931   // https://github.com/google/sanitizers/issues/321.
4932   int res = REAL(lrand48_r)(buffer, result);
4933   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4934   return res;
4935 }
4936 #define INIT_DRAND48_R                  \
4937   COMMON_INTERCEPT_FUNCTION(drand48_r); \
4938   COMMON_INTERCEPT_FUNCTION(lrand48_r);
4939 #else
4940 #define INIT_DRAND48_R
4941 #endif
4942
4943 #if SANITIZER_INTERCEPT_RAND_R
4944 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4945   void *ctx;
4946   COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4947   COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4948   return REAL(rand_r)(seedp);
4949 }
4950 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4951 #else
4952 #define INIT_RAND_R
4953 #endif
4954
4955 #if SANITIZER_INTERCEPT_GETLINE
4956 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4957   void *ctx;
4958   COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4959   // FIXME: under ASan the call below may write to freed memory and corrupt
4960   // its metadata. See
4961   // https://github.com/google/sanitizers/issues/321.
4962   SSIZE_T res = REAL(getline)(lineptr, n, stream);
4963   if (res > 0) {
4964     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4965     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4966     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4967   }
4968   return res;
4969 }
4970
4971 // FIXME: under ASan the call below may write to freed memory and corrupt its
4972 // metadata. See
4973 // https://github.com/google/sanitizers/issues/321.
4974 #define GETDELIM_INTERCEPTOR_IMPL(vname)                                       \
4975   {                                                                            \
4976     void *ctx;                                                                 \
4977     COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream);           \
4978     SSIZE_T res = REAL(vname)(lineptr, n, delim, stream);                      \
4979     if (res > 0) {                                                             \
4980       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));          \
4981       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));                      \
4982       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);                  \
4983     }                                                                          \
4984     return res;                                                                \
4985   }
4986
4987 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4988             void *stream)
4989 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4990
4991 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4992 // with its own body.
4993 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4994             void *stream)
4995 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4996
4997 #define INIT_GETLINE                     \
4998   COMMON_INTERCEPT_FUNCTION(getline);    \
4999   COMMON_INTERCEPT_FUNCTION(__getdelim); \
5000   COMMON_INTERCEPT_FUNCTION(getdelim);
5001 #else
5002 #define INIT_GETLINE
5003 #endif
5004
5005 #if SANITIZER_INTERCEPT_ICONV
5006 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5007             char **outbuf, SIZE_T *outbytesleft) {
5008   void *ctx;
5009   COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5010                            outbytesleft);
5011   if (inbytesleft)
5012     COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5013   if (inbuf && inbytesleft)
5014     COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5015   if (outbytesleft)
5016     COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5017   void *outbuf_orig = outbuf ? *outbuf : nullptr;
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   SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5022   if (outbuf && *outbuf > outbuf_orig) {
5023     SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5024     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5025   }
5026   return res;
5027 }
5028 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5029 #else
5030 #define INIT_ICONV
5031 #endif
5032
5033 #if SANITIZER_INTERCEPT_TIMES
5034 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5035   void *ctx;
5036   COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5037   // FIXME: under ASan the call below may write to freed memory and corrupt
5038   // its metadata. See
5039   // https://github.com/google/sanitizers/issues/321.
5040   __sanitizer_clock_t res = REAL(times)(tms);
5041   if (res != (__sanitizer_clock_t)-1 && tms)
5042     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5043   return res;
5044 }
5045 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5046 #else
5047 #define INIT_TIMES
5048 #endif
5049
5050 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5051 #if !SANITIZER_S390
5052 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5053 // If you see any crashes around this functions, there are 2 known issues with
5054 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5055 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5056 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5057 // to access thread local variables (it should not happen normally,
5058 // because sanitizers use initial-exec tls model).
5059 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5060   void *ctx;
5061   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5062   void *res = REAL(__tls_get_addr)(arg);
5063   uptr tls_begin, tls_end;
5064   COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5065   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5066   if (dtv) {
5067     // New DTLS block has been allocated.
5068     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5069   }
5070   return res;
5071 }
5072 #if SANITIZER_PPC
5073 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5074 // mostly the same semantics as __tls_get_addr, but its presence enables
5075 // some optimizations in linker (which are safe to ignore here).
5076 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5077                           visibility("default")))
5078 void *__tls_get_addr_opt(void *arg);
5079 #endif
5080 #else // SANITIZER_S390
5081 // On s390, we have to intercept two functions here:
5082 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5083 //   the usual __tls_get_addr, but returns a TP-relative offset instead of
5084 //   a proper pointer.  It is used by dlsym for TLS symbols.
5085 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5086 //   descriptor offset as an argument instead of a pointer.  GOT address
5087 //   is passed in r12, so it's necessary to write it in assembly.  This is
5088 //   the function used by the compiler.
5089 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5090 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5091 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5092 extern "C" uptr __tls_get_offset(void *arg);
5093 extern "C" uptr __interceptor___tls_get_offset(void *arg);
5094 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5095   void *ctx;
5096   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5097   uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5098   uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5099   void *ptr = reinterpret_cast<void *>(res + tp);
5100   uptr tls_begin, tls_end;
5101   COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5102   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5103   if (dtv) {
5104     // New DTLS block has been allocated.
5105     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5106   }
5107   return res;
5108 }
5109 // We need a hidden symbol aliasing the above, so that we can jump
5110 // directly to it from the assembly below.
5111 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5112                           visibility("hidden")))
5113 uptr __tls_get_addr_hidden(void *arg);
5114 // Now carefully intercept __tls_get_offset.
5115 asm(
5116   ".text\n"
5117 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5118 // exports our symbol.
5119   ".weak __tls_get_offset\n"
5120   ".type __tls_get_offset, @function\n"
5121   "__tls_get_offset:\n"
5122   ".global __interceptor___tls_get_offset\n"
5123   ".type __interceptor___tls_get_offset, @function\n"
5124   "__interceptor___tls_get_offset:\n"
5125 #ifdef __s390x__
5126   "la %r2, 0(%r2,%r12)\n"
5127   "jg __tls_get_addr_hidden\n"
5128 #else
5129   "basr %r3,0\n"
5130   "0: la %r2,0(%r2,%r12)\n"
5131   "l %r4,1f-0b(%r3)\n"
5132   "b 0(%r4,%r3)\n"
5133   "1: .long __tls_get_addr_hidden - 0b\n"
5134 #endif
5135   ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5136 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5137   ".type __tls_get_offset_wrapper, @function\n"
5138   "__tls_get_offset_wrapper:\n"
5139 #ifdef __s390x__
5140   "sgr %r2,%r12\n"
5141 #else
5142   "sr %r2,%r12\n"
5143 #endif
5144   "br %r3\n"
5145   ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5146 );
5147 #endif // SANITIZER_S390
5148 #else
5149 #define INIT_TLS_GET_ADDR
5150 #endif
5151
5152 #if SANITIZER_INTERCEPT_LISTXATTR
5153 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5154   void *ctx;
5155   COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5156   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5157   // FIXME: under ASan the call below may write to freed memory and corrupt
5158   // its metadata. See
5159   // https://github.com/google/sanitizers/issues/321.
5160   SSIZE_T res = REAL(listxattr)(path, list, size);
5161   // Here and below, size == 0 is a special case where nothing is written to the
5162   // buffer, and res contains the desired buffer size.
5163   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5164   return res;
5165 }
5166 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5167   void *ctx;
5168   COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5169   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5170   // FIXME: under ASan the call below may write to freed memory and corrupt
5171   // its metadata. See
5172   // https://github.com/google/sanitizers/issues/321.
5173   SSIZE_T res = REAL(llistxattr)(path, list, size);
5174   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5175   return res;
5176 }
5177 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5178   void *ctx;
5179   COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5180   // FIXME: under ASan the call below may write to freed memory and corrupt
5181   // its metadata. See
5182   // https://github.com/google/sanitizers/issues/321.
5183   SSIZE_T res = REAL(flistxattr)(fd, list, size);
5184   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5185   return res;
5186 }
5187 #define INIT_LISTXATTR                   \
5188   COMMON_INTERCEPT_FUNCTION(listxattr);  \
5189   COMMON_INTERCEPT_FUNCTION(llistxattr); \
5190   COMMON_INTERCEPT_FUNCTION(flistxattr);
5191 #else
5192 #define INIT_LISTXATTR
5193 #endif
5194
5195 #if SANITIZER_INTERCEPT_GETXATTR
5196 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5197             SIZE_T size) {
5198   void *ctx;
5199   COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5200   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5201   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5202   // FIXME: under ASan the call below may write to freed memory and corrupt
5203   // its metadata. See
5204   // https://github.com/google/sanitizers/issues/321.
5205   SSIZE_T res = REAL(getxattr)(path, name, value, size);
5206   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5207   return res;
5208 }
5209 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5210             SIZE_T size) {
5211   void *ctx;
5212   COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5213   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5214   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5215   // FIXME: under ASan the call below may write to freed memory and corrupt
5216   // its metadata. See
5217   // https://github.com/google/sanitizers/issues/321.
5218   SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5219   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5220   return res;
5221 }
5222 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5223             SIZE_T size) {
5224   void *ctx;
5225   COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5226   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5227   // FIXME: under ASan the call below may write to freed memory and corrupt
5228   // its metadata. See
5229   // https://github.com/google/sanitizers/issues/321.
5230   SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5231   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5232   return res;
5233 }
5234 #define INIT_GETXATTR                   \
5235   COMMON_INTERCEPT_FUNCTION(getxattr);  \
5236   COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5237   COMMON_INTERCEPT_FUNCTION(fgetxattr);
5238 #else
5239 #define INIT_GETXATTR
5240 #endif
5241
5242 #if SANITIZER_INTERCEPT_GETRESID
5243 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5244   void *ctx;
5245   COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5246   // FIXME: under ASan the call below may write to freed memory and corrupt
5247   // its metadata. See
5248   // https://github.com/google/sanitizers/issues/321.
5249   int res = REAL(getresuid)(ruid, euid, suid);
5250   if (res >= 0) {
5251     if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5252     if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5253     if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5254   }
5255   return res;
5256 }
5257 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5258   void *ctx;
5259   COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5260   // FIXME: under ASan the call below may write to freed memory and corrupt
5261   // its metadata. See
5262   // https://github.com/google/sanitizers/issues/321.
5263   int res = REAL(getresgid)(rgid, egid, sgid);
5264   if (res >= 0) {
5265     if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5266     if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5267     if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5268   }
5269   return res;
5270 }
5271 #define INIT_GETRESID                   \
5272   COMMON_INTERCEPT_FUNCTION(getresuid); \
5273   COMMON_INTERCEPT_FUNCTION(getresgid);
5274 #else
5275 #define INIT_GETRESID
5276 #endif
5277
5278 #if SANITIZER_INTERCEPT_GETIFADDRS
5279 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5280 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5281 // intercept it to poison the memory again.
5282 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5283   void *ctx;
5284   COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5285   // FIXME: under ASan the call below may write to freed memory and corrupt
5286   // its metadata. See
5287   // https://github.com/google/sanitizers/issues/321.
5288   int res = REAL(getifaddrs)(ifap);
5289   if (res == 0 && ifap) {
5290     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5291     __sanitizer_ifaddrs *p = *ifap;
5292     while (p) {
5293       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5294       if (p->ifa_name)
5295         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5296                                        REAL(strlen)(p->ifa_name) + 1);
5297       if (p->ifa_addr)
5298         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5299       if (p->ifa_netmask)
5300         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5301       // On Linux this is a union, but the other member also points to a
5302       // struct sockaddr, so the following is sufficient.
5303       if (p->ifa_dstaddr)
5304         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5305       // FIXME(smatveev): Unpoison p->ifa_data as well.
5306       p = p->ifa_next;
5307     }
5308   }
5309   return res;
5310 }
5311 #define INIT_GETIFADDRS                  \
5312   COMMON_INTERCEPT_FUNCTION(getifaddrs);
5313 #else
5314 #define INIT_GETIFADDRS
5315 #endif
5316
5317 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5318 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5319   void *ctx;
5320   COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5321   // FIXME: under ASan the call below may write to freed memory and corrupt
5322   // its metadata. See
5323   // https://github.com/google/sanitizers/issues/321.
5324   char *res = REAL(if_indextoname)(ifindex, ifname);
5325   if (res && ifname)
5326     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5327   return res;
5328 }
5329 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5330   void *ctx;
5331   COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5332   if (ifname)
5333     COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5334   return REAL(if_nametoindex)(ifname);
5335 }
5336 #define INIT_IF_INDEXTONAME                  \
5337   COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5338   COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5339 #else
5340 #define INIT_IF_INDEXTONAME
5341 #endif
5342
5343 #if SANITIZER_INTERCEPT_CAPGET
5344 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5345   void *ctx;
5346   COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5347   if (hdrp)
5348     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5349   // FIXME: under ASan the call below may write to freed memory and corrupt
5350   // its metadata. See
5351   // https://github.com/google/sanitizers/issues/321.
5352   int res = REAL(capget)(hdrp, datap);
5353   if (res == 0 && datap)
5354     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5355   // We can also return -1 and write to hdrp->version if the version passed in
5356   // hdrp->version is unsupported. But that's not a trivial condition to check,
5357   // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5358   return res;
5359 }
5360 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5361   void *ctx;
5362   COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5363   if (hdrp)
5364     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5365   if (datap)
5366     COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5367   return REAL(capset)(hdrp, datap);
5368 }
5369 #define INIT_CAPGET                  \
5370   COMMON_INTERCEPT_FUNCTION(capget); \
5371   COMMON_INTERCEPT_FUNCTION(capset);
5372 #else
5373 #define INIT_CAPGET
5374 #endif
5375
5376 #if SANITIZER_INTERCEPT_AEABI_MEM
5377 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5378   void *ctx;
5379   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5380 }
5381
5382 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5383   void *ctx;
5384   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5385 }
5386
5387 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5388   void *ctx;
5389   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5390 }
5391
5392 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5393   void *ctx;
5394   COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5395 }
5396
5397 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5398   void *ctx;
5399   COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5400 }
5401
5402 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5403   void *ctx;
5404   COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5405 }
5406
5407 // Note the argument order.
5408 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5409   void *ctx;
5410   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5411 }
5412
5413 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5414   void *ctx;
5415   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5416 }
5417
5418 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5419   void *ctx;
5420   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5421 }
5422
5423 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5424   void *ctx;
5425   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5426 }
5427
5428 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5429   void *ctx;
5430   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5431 }
5432
5433 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5434   void *ctx;
5435   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5436 }
5437
5438 #define INIT_AEABI_MEM                         \
5439   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove);  \
5440   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5441   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5442   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy);   \
5443   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4);  \
5444   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8);  \
5445   COMMON_INTERCEPT_FUNCTION(__aeabi_memset);   \
5446   COMMON_INTERCEPT_FUNCTION(__aeabi_memset4);  \
5447   COMMON_INTERCEPT_FUNCTION(__aeabi_memset8);  \
5448   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr);   \
5449   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4);  \
5450   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5451 #else
5452 #define INIT_AEABI_MEM
5453 #endif  // SANITIZER_INTERCEPT_AEABI_MEM
5454
5455 #if SANITIZER_INTERCEPT___BZERO
5456 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5457   void *ctx;
5458   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5459 }
5460
5461 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5462 #else
5463 #define INIT___BZERO
5464 #endif  // SANITIZER_INTERCEPT___BZERO
5465
5466 #if SANITIZER_INTERCEPT_FTIME
5467 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5468   void *ctx;
5469   COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5470   // FIXME: under ASan the call below may write to freed memory and corrupt
5471   // its metadata. See
5472   // https://github.com/google/sanitizers/issues/321.
5473   int res = REAL(ftime)(tp);
5474   if (tp)
5475     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5476   return res;
5477 }
5478 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5479 #else
5480 #define INIT_FTIME
5481 #endif  // SANITIZER_INTERCEPT_FTIME
5482
5483 #if SANITIZER_INTERCEPT_XDR
5484 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5485             unsigned size, int op) {
5486   void *ctx;
5487   COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5488   // FIXME: under ASan the call below may write to freed memory and corrupt
5489   // its metadata. See
5490   // https://github.com/google/sanitizers/issues/321.
5491   REAL(xdrmem_create)(xdrs, addr, size, op);
5492   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5493   if (op == __sanitizer_XDR_ENCODE) {
5494     // It's not obvious how much data individual xdr_ routines write.
5495     // Simply unpoison the entire target buffer in advance.
5496     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5497   }
5498 }
5499
5500 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5501   void *ctx;
5502   COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5503   // FIXME: under ASan the call below may write to freed memory and corrupt
5504   // its metadata. See
5505   // https://github.com/google/sanitizers/issues/321.
5506   REAL(xdrstdio_create)(xdrs, file, op);
5507   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5508 }
5509
5510 // FIXME: under ASan the call below may write to freed memory and corrupt
5511 // its metadata. See
5512 // https://github.com/google/sanitizers/issues/321.
5513 #define XDR_INTERCEPTOR(F, T)                             \
5514   INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) {      \
5515     void *ctx;                                            \
5516     COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p);            \
5517     if (p && xdrs->x_op == __sanitizer_XDR_ENCODE)        \
5518       COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));  \
5519     int res = REAL(F)(xdrs, p);                           \
5520     if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5521       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5522     return res;                                           \
5523   }
5524
5525 XDR_INTERCEPTOR(xdr_short, short)
5526 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5527 XDR_INTERCEPTOR(xdr_int, int)
5528 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5529 XDR_INTERCEPTOR(xdr_long, long)
5530 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5531 XDR_INTERCEPTOR(xdr_hyper, long long)
5532 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5533 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5534 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5535 XDR_INTERCEPTOR(xdr_int8_t, u8)
5536 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5537 XDR_INTERCEPTOR(xdr_int16_t, u16)
5538 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5539 XDR_INTERCEPTOR(xdr_int32_t, u32)
5540 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5541 XDR_INTERCEPTOR(xdr_int64_t, u64)
5542 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5543 XDR_INTERCEPTOR(xdr_quad_t, long long)
5544 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5545 XDR_INTERCEPTOR(xdr_bool, bool)
5546 XDR_INTERCEPTOR(xdr_enum, int)
5547 XDR_INTERCEPTOR(xdr_char, char)
5548 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5549 XDR_INTERCEPTOR(xdr_float, float)
5550 XDR_INTERCEPTOR(xdr_double, double)
5551
5552 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5553 // wrapstring, sizeof
5554
5555 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5556             unsigned maxsize) {
5557   void *ctx;
5558   COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5559   if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5560     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5561     COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5562     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5563   }
5564   // FIXME: under ASan the call below may write to freed memory and corrupt
5565   // its metadata. See
5566   // https://github.com/google/sanitizers/issues/321.
5567   int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5568   if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5569     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5570     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5571     if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5572   }
5573   return res;
5574 }
5575
5576 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5577             unsigned maxsize) {
5578   void *ctx;
5579   COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5580   if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5581     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5582     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5583   }
5584   // FIXME: under ASan the call below may write to freed memory and corrupt
5585   // its metadata. See
5586   // https://github.com/google/sanitizers/issues/321.
5587   int res = REAL(xdr_string)(xdrs, p, maxsize);
5588   if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5589     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5590     if (res && *p)
5591       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5592   }
5593   return res;
5594 }
5595
5596 #define INIT_XDR                               \
5597   COMMON_INTERCEPT_FUNCTION(xdrmem_create);    \
5598   COMMON_INTERCEPT_FUNCTION(xdrstdio_create);  \
5599   COMMON_INTERCEPT_FUNCTION(xdr_short);        \
5600   COMMON_INTERCEPT_FUNCTION(xdr_u_short);      \
5601   COMMON_INTERCEPT_FUNCTION(xdr_int);          \
5602   COMMON_INTERCEPT_FUNCTION(xdr_u_int);        \
5603   COMMON_INTERCEPT_FUNCTION(xdr_long);         \
5604   COMMON_INTERCEPT_FUNCTION(xdr_u_long);       \
5605   COMMON_INTERCEPT_FUNCTION(xdr_hyper);        \
5606   COMMON_INTERCEPT_FUNCTION(xdr_u_hyper);      \
5607   COMMON_INTERCEPT_FUNCTION(xdr_longlong_t);   \
5608   COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5609   COMMON_INTERCEPT_FUNCTION(xdr_int8_t);       \
5610   COMMON_INTERCEPT_FUNCTION(xdr_uint8_t);      \
5611   COMMON_INTERCEPT_FUNCTION(xdr_int16_t);      \
5612   COMMON_INTERCEPT_FUNCTION(xdr_uint16_t);     \
5613   COMMON_INTERCEPT_FUNCTION(xdr_int32_t);      \
5614   COMMON_INTERCEPT_FUNCTION(xdr_uint32_t);     \
5615   COMMON_INTERCEPT_FUNCTION(xdr_int64_t);      \
5616   COMMON_INTERCEPT_FUNCTION(xdr_uint64_t);     \
5617   COMMON_INTERCEPT_FUNCTION(xdr_quad_t);       \
5618   COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t);     \
5619   COMMON_INTERCEPT_FUNCTION(xdr_bool);         \
5620   COMMON_INTERCEPT_FUNCTION(xdr_enum);         \
5621   COMMON_INTERCEPT_FUNCTION(xdr_char);         \
5622   COMMON_INTERCEPT_FUNCTION(xdr_u_char);       \
5623   COMMON_INTERCEPT_FUNCTION(xdr_float);        \
5624   COMMON_INTERCEPT_FUNCTION(xdr_double);       \
5625   COMMON_INTERCEPT_FUNCTION(xdr_bytes);        \
5626   COMMON_INTERCEPT_FUNCTION(xdr_string);
5627 #else
5628 #define INIT_XDR
5629 #endif  // SANITIZER_INTERCEPT_XDR
5630
5631 #if SANITIZER_INTERCEPT_TSEARCH
5632 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5633             int (*compar)(const void *, const void *)) {
5634   void *ctx;
5635   COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5636   // FIXME: under ASan the call below may write to freed memory and corrupt
5637   // its metadata. See
5638   // https://github.com/google/sanitizers/issues/321.
5639   void *res = REAL(tsearch)(key, rootp, compar);
5640   if (res && *(void **)res == key)
5641     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5642   return res;
5643 }
5644 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5645 #else
5646 #define INIT_TSEARCH
5647 #endif
5648
5649 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5650     SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5651 void unpoison_file(__sanitizer_FILE *fp) {
5652 #if SANITIZER_HAS_STRUCT_FILE
5653   COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5654   if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5655     COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5656                                         fp->_IO_read_end - fp->_IO_read_base);
5657 #endif  // SANITIZER_HAS_STRUCT_FILE
5658 }
5659 #endif
5660
5661 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5662 // These guys are called when a .c source is built with -O2.
5663 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5664   void *ctx;
5665   COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5666   int res = REAL(__uflow)(fp);
5667   unpoison_file(fp);
5668   return res;
5669 }
5670 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5671   void *ctx;
5672   COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5673   int res = REAL(__underflow)(fp);
5674   unpoison_file(fp);
5675   return res;
5676 }
5677 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5678   void *ctx;
5679   COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5680   int res = REAL(__overflow)(fp, ch);
5681   unpoison_file(fp);
5682   return res;
5683 }
5684 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5685   void *ctx;
5686   COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5687   int res = REAL(__wuflow)(fp);
5688   unpoison_file(fp);
5689   return res;
5690 }
5691 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5692   void *ctx;
5693   COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5694   int res = REAL(__wunderflow)(fp);
5695   unpoison_file(fp);
5696   return res;
5697 }
5698 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5699   void *ctx;
5700   COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5701   int res = REAL(__woverflow)(fp, ch);
5702   unpoison_file(fp);
5703   return res;
5704 }
5705 #define INIT_LIBIO_INTERNALS               \
5706   COMMON_INTERCEPT_FUNCTION(__uflow);      \
5707   COMMON_INTERCEPT_FUNCTION(__underflow);  \
5708   COMMON_INTERCEPT_FUNCTION(__overflow);   \
5709   COMMON_INTERCEPT_FUNCTION(__wuflow);     \
5710   COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5711   COMMON_INTERCEPT_FUNCTION(__woverflow);
5712 #else
5713 #define INIT_LIBIO_INTERNALS
5714 #endif
5715
5716 #if SANITIZER_INTERCEPT_FOPEN
5717 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5718   void *ctx;
5719   COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5720   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5721   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5722   __sanitizer_FILE *res = REAL(fopen)(path, mode);
5723   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5724   if (res) unpoison_file(res);
5725   return res;
5726 }
5727 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5728   void *ctx;
5729   COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5730   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5731   __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5732   if (res) unpoison_file(res);
5733   return res;
5734 }
5735 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5736             __sanitizer_FILE *fp) {
5737   void *ctx;
5738   COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5739   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5740   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5741   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5742   __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5743   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5744   if (res) unpoison_file(res);
5745   return res;
5746 }
5747 #define INIT_FOPEN                   \
5748   COMMON_INTERCEPT_FUNCTION(fopen);  \
5749   COMMON_INTERCEPT_FUNCTION(fdopen); \
5750   COMMON_INTERCEPT_FUNCTION(freopen);
5751 #else
5752 #define INIT_FOPEN
5753 #endif
5754
5755 #if SANITIZER_INTERCEPT_FOPEN64
5756 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5757   void *ctx;
5758   COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5759   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5760   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5761   __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5762   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5763   if (res) unpoison_file(res);
5764   return res;
5765 }
5766 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5767             __sanitizer_FILE *fp) {
5768   void *ctx;
5769   COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5770   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5771   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5772   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5773   __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5774   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5775   if (res) unpoison_file(res);
5776   return res;
5777 }
5778 #define INIT_FOPEN64                  \
5779   COMMON_INTERCEPT_FUNCTION(fopen64); \
5780   COMMON_INTERCEPT_FUNCTION(freopen64);
5781 #else
5782 #define INIT_FOPEN64
5783 #endif
5784
5785 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5786 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5787   void *ctx;
5788   COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5789   // FIXME: under ASan the call below may write to freed memory and corrupt
5790   // its metadata. See
5791   // https://github.com/google/sanitizers/issues/321.
5792   __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5793   if (res) {
5794     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5795     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5796     unpoison_file(res);
5797     FileMetadata file = {ptr, sizeloc};
5798     SetInterceptorMetadata(res, file);
5799   }
5800   return res;
5801 }
5802 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5803             SIZE_T *sizeloc) {
5804   void *ctx;
5805   COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5806   __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5807   if (res) {
5808     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5809     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5810     unpoison_file(res);
5811     FileMetadata file = {(char **)ptr, sizeloc};
5812     SetInterceptorMetadata(res, file);
5813   }
5814   return res;
5815 }
5816 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5817             const char *mode) {
5818   void *ctx;
5819   COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5820   // FIXME: under ASan the call below may write to freed memory and corrupt
5821   // its metadata. See
5822   // https://github.com/google/sanitizers/issues/321.
5823   __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5824   if (res) unpoison_file(res);
5825   return res;
5826 }
5827 #define INIT_OPEN_MEMSTREAM                   \
5828   COMMON_INTERCEPT_FUNCTION(open_memstream);  \
5829   COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5830   COMMON_INTERCEPT_FUNCTION(fmemopen);
5831 #else
5832 #define INIT_OPEN_MEMSTREAM
5833 #endif
5834
5835 #if SANITIZER_INTERCEPT_OBSTACK
5836 static void initialize_obstack(__sanitizer_obstack *obstack) {
5837   COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5838   if (obstack->chunk)
5839     COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5840                                         sizeof(*obstack->chunk));
5841 }
5842
5843 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5844             int align, void *(*alloc_fn)(uptr arg, uptr sz),
5845             void (*free_fn)(uptr arg, void *p)) {
5846   void *ctx;
5847   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5848                            free_fn);
5849   int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5850   if (res) initialize_obstack(obstack);
5851   return res;
5852 }
5853 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5854             int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5855   void *ctx;
5856   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5857                            free_fn);
5858   int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5859   if (res) initialize_obstack(obstack);
5860   return res;
5861 }
5862 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5863   void *ctx;
5864   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5865   REAL(_obstack_newchunk)(obstack, length);
5866   if (obstack->chunk)
5867     COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5868         obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5869 }
5870 #define INIT_OBSTACK                           \
5871   COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5872   COMMON_INTERCEPT_FUNCTION(_obstack_begin);   \
5873   COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5874 #else
5875 #define INIT_OBSTACK
5876 #endif
5877
5878 #if SANITIZER_INTERCEPT_FFLUSH
5879 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5880   void *ctx;
5881   COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5882   int res = REAL(fflush)(fp);
5883   // FIXME: handle fp == NULL
5884   if (fp) {
5885     const FileMetadata *m = GetInterceptorMetadata(fp);
5886     if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5887   }
5888   return res;
5889 }
5890 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5891 #else
5892 #define INIT_FFLUSH
5893 #endif
5894
5895 #if SANITIZER_INTERCEPT_FCLOSE
5896 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5897   void *ctx;
5898   COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5899   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5900   const FileMetadata *m = GetInterceptorMetadata(fp);
5901   int res = REAL(fclose)(fp);
5902   if (m) {
5903     COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5904     DeleteInterceptorMetadata(fp);
5905   }
5906   return res;
5907 }
5908 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5909 #else
5910 #define INIT_FCLOSE
5911 #endif
5912
5913 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5914 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5915   void *ctx;
5916   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5917   if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5918   COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5919   void *res = REAL(dlopen)(filename, flag);
5920   Symbolizer::GetOrInit()->InvalidateModuleList();
5921   COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5922   return res;
5923 }
5924
5925 INTERCEPTOR(int, dlclose, void *handle) {
5926   void *ctx;
5927   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5928   int res = REAL(dlclose)(handle);
5929   Symbolizer::GetOrInit()->InvalidateModuleList();
5930   COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5931   return res;
5932 }
5933 #define INIT_DLOPEN_DLCLOSE          \
5934   COMMON_INTERCEPT_FUNCTION(dlopen); \
5935   COMMON_INTERCEPT_FUNCTION(dlclose);
5936 #else
5937 #define INIT_DLOPEN_DLCLOSE
5938 #endif
5939
5940 #if SANITIZER_INTERCEPT_GETPASS
5941 INTERCEPTOR(char *, getpass, const char *prompt) {
5942   void *ctx;
5943   COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5944   if (prompt)
5945     COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5946   char *res = REAL(getpass)(prompt);
5947   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5948   return res;
5949 }
5950
5951 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5952 #else
5953 #define INIT_GETPASS
5954 #endif
5955
5956 #if SANITIZER_INTERCEPT_TIMERFD
5957 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5958             void *old_value) {
5959   void *ctx;
5960   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5961                            old_value);
5962   COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5963   int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5964   if (res != -1 && old_value)
5965     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5966   return res;
5967 }
5968
5969 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5970   void *ctx;
5971   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5972   int res = REAL(timerfd_gettime)(fd, curr_value);
5973   if (res != -1 && curr_value)
5974     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5975   return res;
5976 }
5977 #define INIT_TIMERFD                          \
5978   COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5979   COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5980 #else
5981 #define INIT_TIMERFD
5982 #endif
5983
5984 #if SANITIZER_INTERCEPT_MLOCKX
5985 // Linux kernel has a bug that leads to kernel deadlock if a process
5986 // maps TBs of memory and then calls mlock().
5987 static void MlockIsUnsupported() {
5988   static atomic_uint8_t printed;
5989   if (atomic_exchange(&printed, 1, memory_order_relaxed))
5990     return;
5991   VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5992           SanitizerToolName);
5993 }
5994
5995 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5996   MlockIsUnsupported();
5997   return 0;
5998 }
5999
6000 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6001   MlockIsUnsupported();
6002   return 0;
6003 }
6004
6005 INTERCEPTOR(int, mlockall, int flags) {
6006   MlockIsUnsupported();
6007   return 0;
6008 }
6009
6010 INTERCEPTOR(int, munlockall, void) {
6011   MlockIsUnsupported();
6012   return 0;
6013 }
6014
6015 #define INIT_MLOCKX                                                            \
6016   COMMON_INTERCEPT_FUNCTION(mlock);                                            \
6017   COMMON_INTERCEPT_FUNCTION(munlock);                                          \
6018   COMMON_INTERCEPT_FUNCTION(mlockall);                                         \
6019   COMMON_INTERCEPT_FUNCTION(munlockall);
6020
6021 #else
6022 #define INIT_MLOCKX
6023 #endif  // SANITIZER_INTERCEPT_MLOCKX
6024
6025 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6026 struct WrappedCookie {
6027   void *real_cookie;
6028   __sanitizer_cookie_io_functions_t real_io_funcs;
6029 };
6030
6031 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6032   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6033   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6034   __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6035   return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6036 }
6037
6038 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6039   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6040   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6041   __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6042   return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6043 }
6044
6045 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6046   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6047   COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6048   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6049   __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6050   return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6051                    : -1;
6052 }
6053
6054 static int wrapped_close(void *cookie) {
6055   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6056   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6057   __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6058   int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6059   InternalFree(wrapped_cookie);
6060   return res;
6061 }
6062
6063 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6064             __sanitizer_cookie_io_functions_t io_funcs) {
6065   void *ctx;
6066   COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6067   WrappedCookie *wrapped_cookie =
6068       (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6069   wrapped_cookie->real_cookie = cookie;
6070   wrapped_cookie->real_io_funcs = io_funcs;
6071   __sanitizer_FILE *res =
6072       REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6073                                                wrapped_seek, wrapped_close});
6074   return res;
6075 }
6076
6077 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6078 #else
6079 #define INIT_FOPENCOOKIE
6080 #endif  // SANITIZER_INTERCEPT_FOPENCOOKIE
6081
6082 #if SANITIZER_INTERCEPT_SEM
6083 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6084   void *ctx;
6085   COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6086   // Workaround a bug in glibc's "old" semaphore implementation by
6087   // zero-initializing the sem_t contents. This has to be done here because
6088   // interceptors bind to the lowest symbols version by default, hitting the
6089   // buggy code path while the non-sanitized build of the same code works fine.
6090   REAL(memset)(s, 0, sizeof(*s));
6091   int res = REAL(sem_init)(s, pshared, value);
6092   return res;
6093 }
6094
6095 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6096   void *ctx;
6097   COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6098   int res = REAL(sem_destroy)(s);
6099   return res;
6100 }
6101
6102 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6103   void *ctx;
6104   COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6105   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6106   if (res == 0) {
6107     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6108   }
6109   return res;
6110 }
6111
6112 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6113   void *ctx;
6114   COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6115   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6116   if (res == 0) {
6117     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6118   }
6119   return res;
6120 }
6121
6122 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6123   void *ctx;
6124   COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6125   COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6126   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6127   if (res == 0) {
6128     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6129   }
6130   return res;
6131 }
6132
6133 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6134   void *ctx;
6135   COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6136   COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6137   int res = REAL(sem_post)(s);
6138   return res;
6139 }
6140
6141 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6142   void *ctx;
6143   COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6144   int res = REAL(sem_getvalue)(s, sval);
6145   if (res == 0) {
6146     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6147     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6148   }
6149   return res;
6150 }
6151 #define INIT_SEM                                                               \
6152   COMMON_INTERCEPT_FUNCTION(sem_init);                                         \
6153   COMMON_INTERCEPT_FUNCTION(sem_destroy);                                      \
6154   COMMON_INTERCEPT_FUNCTION(sem_wait);                                         \
6155   COMMON_INTERCEPT_FUNCTION(sem_trywait);                                      \
6156   COMMON_INTERCEPT_FUNCTION(sem_timedwait);                                    \
6157   COMMON_INTERCEPT_FUNCTION(sem_post);                                         \
6158   COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6159 #else
6160 #define INIT_SEM
6161 #endif // SANITIZER_INTERCEPT_SEM
6162
6163 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6164 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6165   void *ctx;
6166   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6167   int res = REAL(pthread_setcancelstate)(state, oldstate);
6168   if (res == 0 && oldstate != nullptr)
6169     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6170   return res;
6171 }
6172
6173 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6174   void *ctx;
6175   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6176   int res = REAL(pthread_setcanceltype)(type, oldtype);
6177   if (res == 0 && oldtype != nullptr)
6178     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6179   return res;
6180 }
6181 #define INIT_PTHREAD_SETCANCEL                                                 \
6182   COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate);                           \
6183   COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6184 #else
6185 #define INIT_PTHREAD_SETCANCEL
6186 #endif
6187
6188 #if SANITIZER_INTERCEPT_MINCORE
6189 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6190   void *ctx;
6191   COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6192   int res = REAL(mincore)(addr, length, vec);
6193   if (res == 0) {
6194     uptr page_size = GetPageSizeCached();
6195     uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6196     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6197   }
6198   return res;
6199 }
6200 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6201 #else
6202 #define INIT_MINCORE
6203 #endif
6204
6205 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6206 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6207             uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6208             uptr flags) {
6209   void *ctx;
6210   COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6211                            remote_iov, riovcnt, flags);
6212   SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6213                                        riovcnt, flags);
6214   if (res > 0)
6215     write_iovec(ctx, local_iov, liovcnt, res);
6216   return res;
6217 }
6218
6219 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6220             uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6221             uptr flags) {
6222   void *ctx;
6223   COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6224                            remote_iov, riovcnt, flags);
6225   SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6226                                         riovcnt, flags);
6227   if (res > 0)
6228     read_iovec(ctx, local_iov, liovcnt, res);
6229   return res;
6230 }
6231 #define INIT_PROCESS_VM_READV                                                  \
6232   COMMON_INTERCEPT_FUNCTION(process_vm_readv);                                 \
6233   COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6234 #else
6235 #define INIT_PROCESS_VM_READV
6236 #endif
6237
6238 #if SANITIZER_INTERCEPT_CTERMID
6239 INTERCEPTOR(char *, ctermid, char *s) {
6240   void *ctx;
6241   COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6242   char *res = REAL(ctermid)(s);
6243   if (res) {
6244     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6245   }
6246   return res;
6247 }
6248 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6249 #else
6250 #define INIT_CTERMID
6251 #endif
6252
6253 #if SANITIZER_INTERCEPT_CTERMID_R
6254 INTERCEPTOR(char *, ctermid_r, char *s) {
6255   void *ctx;
6256   COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6257   char *res = REAL(ctermid_r)(s);
6258   if (res) {
6259     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6260   }
6261   return res;
6262 }
6263 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6264 #else
6265 #define INIT_CTERMID_R
6266 #endif
6267
6268 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6269 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6270   void *ctx;
6271   COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6272   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6273   SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6274   if (res > 0) {
6275     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6276   }
6277   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6278   return res;
6279 }
6280
6281 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6282             void *srcaddr, int *addrlen) {
6283   void *ctx;
6284   COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6285                            addrlen);
6286   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6287   SIZE_T srcaddr_sz;
6288   if (srcaddr) srcaddr_sz = *addrlen;
6289   (void)srcaddr_sz;  // prevent "set but not used" warning
6290   SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6291   if (res > 0) {
6292     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6293     if (srcaddr)
6294       COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6295                                           Min((SIZE_T)*addrlen, srcaddr_sz));
6296   }
6297   return res;
6298 }
6299 #define INIT_RECV_RECVFROM          \
6300   COMMON_INTERCEPT_FUNCTION(recv);  \
6301   COMMON_INTERCEPT_FUNCTION(recvfrom);
6302 #else
6303 #define INIT_RECV_RECVFROM
6304 #endif
6305
6306 #if SANITIZER_INTERCEPT_SEND_SENDTO
6307 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6308   void *ctx;
6309   COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6310   if (fd >= 0) {
6311     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6312     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6313   }
6314   SSIZE_T res = REAL(send)(fd, buf, len, flags);
6315   if (common_flags()->intercept_send && res > 0)
6316     COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6317   return res;
6318 }
6319
6320 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6321             void *dstaddr, int addrlen) {
6322   void *ctx;
6323   COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6324   if (fd >= 0) {
6325     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6326     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6327   }
6328   // Can't check dstaddr as it may have uninitialized padding at the end.
6329   SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6330   if (common_flags()->intercept_send && res > 0)
6331     COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6332   return res;
6333 }
6334 #define INIT_SEND_SENDTO           \
6335   COMMON_INTERCEPT_FUNCTION(send); \
6336   COMMON_INTERCEPT_FUNCTION(sendto);
6337 #else
6338 #define INIT_SEND_SENDTO
6339 #endif
6340
6341 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6342 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6343   void *ctx;
6344   COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6345   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6346   int res = REAL(eventfd_read)(fd, value);
6347   if (res == 0) {
6348     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6349     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6350   }
6351   return res;
6352 }
6353 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6354   void *ctx;
6355   COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6356   if (fd >= 0) {
6357     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6358     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6359   }
6360   int res = REAL(eventfd_write)(fd, value);
6361   return res;
6362 }
6363 #define INIT_EVENTFD_READ_WRITE            \
6364   COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6365   COMMON_INTERCEPT_FUNCTION(eventfd_write)
6366 #else
6367 #define INIT_EVENTFD_READ_WRITE
6368 #endif
6369
6370 #if SANITIZER_INTERCEPT_STAT
6371 INTERCEPTOR(int, stat, const char *path, void *buf) {
6372   void *ctx;
6373   COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6374   if (common_flags()->intercept_stat)
6375     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6376   int res = REAL(stat)(path, buf);
6377   if (!res)
6378     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6379   return res;
6380 }
6381 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6382 #else
6383 #define INIT_STAT
6384 #endif
6385
6386 #if SANITIZER_INTERCEPT_LSTAT
6387 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6388   void *ctx;
6389   COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6390   if (common_flags()->intercept_stat)
6391     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6392   int res = REAL(lstat)(path, buf);
6393   if (!res)
6394     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6395   return res;
6396 }
6397 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6398 #else
6399 #define INIT_LSTAT
6400 #endif
6401
6402 #if SANITIZER_INTERCEPT___XSTAT
6403 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6404   void *ctx;
6405   COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6406   if (common_flags()->intercept_stat)
6407     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6408   int res = REAL(__xstat)(version, path, buf);
6409   if (!res)
6410     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6411   return res;
6412 }
6413 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6414 #else
6415 #define INIT___XSTAT
6416 #endif
6417
6418 #if SANITIZER_INTERCEPT___XSTAT64
6419 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6420   void *ctx;
6421   COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6422   if (common_flags()->intercept_stat)
6423     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6424   int res = REAL(__xstat64)(version, path, buf);
6425   if (!res)
6426     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6427   return res;
6428 }
6429 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6430 #else
6431 #define INIT___XSTAT64
6432 #endif
6433
6434 #if SANITIZER_INTERCEPT___LXSTAT
6435 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6436   void *ctx;
6437   COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6438   if (common_flags()->intercept_stat)
6439     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6440   int res = REAL(__lxstat)(version, path, buf);
6441   if (!res)
6442     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6443   return res;
6444 }
6445 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6446 #else
6447 #define INIT___LXSTAT
6448 #endif
6449
6450 #if SANITIZER_INTERCEPT___LXSTAT64
6451 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6452   void *ctx;
6453   COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6454   if (common_flags()->intercept_stat)
6455     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6456   int res = REAL(__lxstat64)(version, path, buf);
6457   if (!res)
6458     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6459   return res;
6460 }
6461 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6462 #else
6463 #define INIT___LXSTAT64
6464 #endif
6465
6466 // FIXME: add other *stat interceptor
6467
6468 #if SANITIZER_INTERCEPT_UTMP
6469 INTERCEPTOR(void *, getutent, int dummy) {
6470   void *ctx;
6471   COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6472   void *res = REAL(getutent)(dummy);
6473   if (res)
6474     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6475   return res;
6476 }
6477 INTERCEPTOR(void *, getutid, void *ut) {
6478   void *ctx;
6479   COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6480   void *res = REAL(getutid)(ut);
6481   if (res)
6482     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6483   return res;
6484 }
6485 INTERCEPTOR(void *, getutline, void *ut) {
6486   void *ctx;
6487   COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6488   void *res = REAL(getutline)(ut);
6489   if (res)
6490     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6491   return res;
6492 }
6493 #define INIT_UTMP                      \
6494   COMMON_INTERCEPT_FUNCTION(getutent); \
6495   COMMON_INTERCEPT_FUNCTION(getutid);  \
6496   COMMON_INTERCEPT_FUNCTION(getutline);
6497 #else
6498 #define INIT_UTMP
6499 #endif
6500
6501 #if SANITIZER_INTERCEPT_UTMPX
6502 INTERCEPTOR(void *, getutxent, int dummy) {
6503   void *ctx;
6504   COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6505   void *res = REAL(getutxent)(dummy);
6506   if (res)
6507     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6508   return res;
6509 }
6510 INTERCEPTOR(void *, getutxid, void *ut) {
6511   void *ctx;
6512   COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6513   void *res = REAL(getutxid)(ut);
6514   if (res)
6515     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6516   return res;
6517 }
6518 INTERCEPTOR(void *, getutxline, void *ut) {
6519   void *ctx;
6520   COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6521   void *res = REAL(getutxline)(ut);
6522   if (res)
6523     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6524   return res;
6525 }
6526 #define INIT_UTMPX                      \
6527   COMMON_INTERCEPT_FUNCTION(getutxent); \
6528   COMMON_INTERCEPT_FUNCTION(getutxid);  \
6529   COMMON_INTERCEPT_FUNCTION(getutxline);
6530 #else
6531 #define INIT_UTMPX
6532 #endif
6533
6534 #if SANITIZER_INTERCEPT_GETLOADAVG
6535 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6536   void *ctx;
6537   COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6538   int res = REAL(getloadavg)(loadavg, nelem);
6539   if (res > 0)
6540     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6541   return res;
6542 }
6543 #define INIT_GETLOADAVG                      \
6544   COMMON_INTERCEPT_FUNCTION(getloadavg);
6545 #else
6546 #define INIT_GETLOADAVG
6547 #endif
6548
6549 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6550 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6551   return 0;
6552 }
6553
6554 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6555   return 0;
6556 }
6557
6558 INTERCEPTOR(int, mprobe, void *ptr) {
6559   return 0;
6560 }
6561 #endif
6562
6563 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6564   void *ctx;
6565   COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6566   SIZE_T res = REAL(wcslen)(s);
6567   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6568   return res;
6569 }
6570
6571 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6572   void *ctx;
6573   COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6574   SIZE_T res = REAL(wcsnlen)(s, n);
6575   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6576   return res;
6577 }
6578 #define INIT_WCSLEN                  \
6579   COMMON_INTERCEPT_FUNCTION(wcslen); \
6580   COMMON_INTERCEPT_FUNCTION(wcsnlen);
6581
6582 #if SANITIZER_INTERCEPT_WCSCAT
6583 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6584   void *ctx;
6585   COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6586   SIZE_T src_size = REAL(wcslen)(src);
6587   SIZE_T dst_size = REAL(wcslen)(dst);
6588   COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6589   COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6590   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6591                                  (src_size + 1) * sizeof(wchar_t));
6592   return REAL(wcscat)(dst, src);  // NOLINT
6593 }
6594
6595 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6596   void *ctx;
6597   COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6598   SIZE_T src_size = REAL(wcsnlen)(src, n);
6599   SIZE_T dst_size = REAL(wcslen)(dst);
6600   COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6601                                 Min(src_size + 1, n) * sizeof(wchar_t));
6602   COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6603   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6604                                  (src_size + 1) * sizeof(wchar_t));
6605   return REAL(wcsncat)(dst, src, n);  // NOLINT
6606 }
6607 #define INIT_WCSCAT                  \
6608   COMMON_INTERCEPT_FUNCTION(wcscat); \
6609   COMMON_INTERCEPT_FUNCTION(wcsncat);
6610 #else
6611 #define INIT_WCSCAT
6612 #endif
6613
6614 #if SANITIZER_INTERCEPT_STRXFRM
6615 static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6616
6617 static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6618
6619 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...)             \
6620   {                                                                        \
6621     void *ctx;                                                             \
6622     COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6623     COMMON_INTERCEPTOR_READ_RANGE(ctx, src,                                \
6624                                   sizeof(*src) * (RealStrLen(src) + 1));   \
6625     SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__);             \
6626     if (res < len)                                                         \
6627       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6628     return res;                                                            \
6629   }
6630
6631 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6632   STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6633 }
6634
6635 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6636             void *locale) {
6637   STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6638 }
6639
6640 #define INIT_STRXFRM                  \
6641   COMMON_INTERCEPT_FUNCTION(strxfrm); \
6642   COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6643 #else
6644 #define INIT_STRXFRM
6645 #endif
6646
6647 #if SANITIZER_INTERCEPT___STRXFRM_L
6648 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6649             void *locale) {
6650   STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6651 }
6652
6653 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6654 #else
6655 #define INIT___STRXFRM_L
6656 #endif
6657
6658 #if SANITIZER_INTERCEPT_WCSXFRM
6659 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6660   STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6661 }
6662
6663 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6664             void *locale) {
6665   STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6666 }
6667
6668 #define INIT_WCSXFRM                  \
6669   COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6670   COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6671 #else
6672 #define INIT_WCSXFRM
6673 #endif
6674
6675 #if SANITIZER_INTERCEPT___WCSXFRM_L
6676 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6677             void *locale) {
6678   STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6679 }
6680
6681 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6682 #else
6683 #define INIT___WCSXFRM_L
6684 #endif
6685
6686 #if SANITIZER_INTERCEPT_ACCT
6687 INTERCEPTOR(int, acct, const char *file) {
6688   void *ctx;
6689   COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6690   if (file)
6691     COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6692   return REAL(acct)(file);
6693 }
6694 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6695 #else
6696 #define INIT_ACCT
6697 #endif
6698
6699 #if SANITIZER_INTERCEPT_USER_FROM_UID
6700 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6701   void *ctx;
6702   const char *user;
6703   COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6704   user = REAL(user_from_uid)(uid, nouser);
6705   if (user)
6706     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6707   return user;
6708 }
6709 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6710 #else
6711 #define INIT_USER_FROM_UID
6712 #endif
6713
6714 #if SANITIZER_INTERCEPT_UID_FROM_USER
6715 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6716   void *ctx;
6717   int res;
6718   COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6719   if (name)
6720     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6721   res = REAL(uid_from_user)(name, uid);
6722   if (uid)
6723     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6724   return res;
6725 }
6726 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6727 #else
6728 #define INIT_UID_FROM_USER
6729 #endif
6730
6731 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
6732 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6733   void *ctx;
6734   const char *group;
6735   COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6736   group = REAL(group_from_gid)(gid, nogroup);
6737   if (group)
6738     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6739   return group;
6740 }
6741 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6742 #else
6743 #define INIT_GROUP_FROM_GID
6744 #endif
6745
6746 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
6747 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6748   void *ctx;
6749   int res;
6750   COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6751   if (group)
6752     COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6753   res = REAL(gid_from_group)(group, gid);
6754   if (gid)
6755     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6756   return res;
6757 }
6758 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6759 #else
6760 #define INIT_GID_FROM_GROUP
6761 #endif
6762
6763 #if SANITIZER_INTERCEPT_ACCESS
6764 INTERCEPTOR(int, access, const char *path, int mode) {
6765   void *ctx;
6766   COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6767   if (path)
6768     COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6769   return REAL(access)(path, mode);
6770 }
6771 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6772 #else
6773 #define INIT_ACCESS
6774 #endif
6775
6776 #if SANITIZER_INTERCEPT_FACCESSAT
6777 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6778   void *ctx;
6779   COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6780   if (path)
6781     COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6782   return REAL(faccessat)(fd, path, mode, flags);
6783 }
6784 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6785 #else
6786 #define INIT_FACCESSAT
6787 #endif
6788
6789 #if SANITIZER_INTERCEPT_GETGROUPLIST
6790 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6791             int *ngroups) {
6792   void *ctx;
6793   int res;
6794   COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6795   if (name)
6796     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6797   if (ngroups)
6798     COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6799   res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6800   if (!res && groups && ngroups) {
6801     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6802     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6803   }
6804   return res;
6805 }
6806
6807 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6808 #else
6809 #define INIT_GETGROUPLIST
6810 #endif
6811
6812 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6813 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6814             int maxgrp, int *ngroups) {
6815   void *ctx;
6816   int res;
6817   COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6818                            maxgrp, ngroups);
6819   if (name)
6820     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6821   res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6822   if (!res && groups && ngroups) {
6823     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6824     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6825   }
6826   return res;
6827 }
6828
6829 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6830 #else
6831 #define INIT_GETGROUPMEMBERSHIP
6832 #endif
6833
6834 #if SANITIZER_INTERCEPT_READLINK
6835 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6836   void* ctx;
6837   COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6838   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6839   SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6840   if (res > 0)
6841     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6842   return res;
6843 }
6844
6845 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6846 #else
6847 #define INIT_READLINK
6848 #endif
6849
6850 #if SANITIZER_INTERCEPT_READLINKAT
6851 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
6852             SIZE_T bufsiz) {
6853   void* ctx;
6854   COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
6855   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6856   SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
6857   if (res > 0)
6858     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6859   return res;
6860 }
6861
6862 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
6863 #else
6864 #define INIT_READLINKAT
6865 #endif
6866
6867 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
6868 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
6869             struct file_handle *handle, int *mount_id, int flags) {
6870   void* ctx;
6871   COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
6872                            mount_id, flags);
6873   COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
6874
6875   __sanitizer_file_handle *sanitizer_handle =
6876       reinterpret_cast<__sanitizer_file_handle*>(handle);
6877   COMMON_INTERCEPTOR_READ_RANGE(
6878       ctx, &sanitizer_handle->handle_bytes,
6879       sizeof(sanitizer_handle->handle_bytes));
6880
6881   int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
6882   if (!res) {
6883     COMMON_INTERCEPTOR_WRITE_RANGE(
6884         ctx, &sanitizer_handle->handle_bytes,
6885         sizeof(sanitizer_handle->handle_bytes));
6886     COMMON_INTERCEPTOR_WRITE_RANGE(
6887         ctx, &sanitizer_handle->handle_type,
6888         sizeof(sanitizer_handle->handle_type));
6889     COMMON_INTERCEPTOR_WRITE_RANGE(
6890         ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6891     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
6892   }
6893   return res;
6894 }
6895
6896 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
6897 #else
6898 #define INIT_NAME_TO_HANDLE_AT
6899 #endif
6900
6901 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
6902 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
6903             int flags) {
6904   void* ctx;
6905   COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
6906
6907   __sanitizer_file_handle *sanitizer_handle =
6908       reinterpret_cast<__sanitizer_file_handle*>(handle);
6909   COMMON_INTERCEPTOR_READ_RANGE(
6910       ctx, &sanitizer_handle->handle_bytes,
6911       sizeof(sanitizer_handle->handle_bytes));
6912   COMMON_INTERCEPTOR_READ_RANGE(
6913       ctx, &sanitizer_handle->handle_type,
6914       sizeof(sanitizer_handle->handle_type));
6915   COMMON_INTERCEPTOR_READ_RANGE(
6916       ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6917
6918   return REAL(open_by_handle_at)(mount_fd, handle, flags);
6919 }
6920
6921 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
6922 #else
6923 #define INIT_OPEN_BY_HANDLE_AT
6924 #endif
6925
6926 #if SANITIZER_INTERCEPT_STRLCPY
6927 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
6928   void *ctx;
6929   SIZE_T res;
6930   COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
6931   if (src) {
6932     // Keep strnlen as macro argument, as macro may ignore it.
6933     COMMON_INTERCEPTOR_READ_STRING(
6934         ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
6935   }
6936   res = REAL(strlcpy)(dst, src, size);
6937   COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
6938   return res;
6939 }
6940
6941 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
6942   void *ctx;
6943   SIZE_T len = 0;
6944   COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
6945   // src is checked in the strlcpy() interceptor
6946   if (dst) {
6947     len = internal_strnlen(dst, size);
6948     COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
6949   }
6950   // Reuse the rest of the code in the strlcpy() interceptor
6951   return WRAP(strlcpy)(dst + len, src, size - len) + len;
6952 }
6953 #define INIT_STRLCPY \
6954   COMMON_INTERCEPT_FUNCTION(strlcpy); \
6955   COMMON_INTERCEPT_FUNCTION(strlcat);
6956 #else
6957 #define INIT_STRLCPY
6958 #endif
6959
6960 #if SANITIZER_INTERCEPT_MMAP
6961 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
6962             OFF_T off) {
6963   void *ctx;
6964   if (common_flags()->detect_write_exec)
6965     ReportMmapWriteExec(prot);
6966   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
6967     return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
6968   COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
6969   COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
6970 }
6971
6972 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
6973   void *ctx;
6974   if (common_flags()->detect_write_exec)
6975     ReportMmapWriteExec(prot);
6976   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
6977     return (int)internal_mprotect(addr, sz, prot);
6978   COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
6979   MprotectMallocZones(addr, prot);
6980   return REAL(mprotect)(addr, sz, prot);
6981 }
6982 #define INIT_MMAP                                                              \
6983   COMMON_INTERCEPT_FUNCTION(mmap);                                             \
6984   COMMON_INTERCEPT_FUNCTION(mprotect);
6985 #else
6986 #define INIT_MMAP
6987 #endif
6988
6989 #if SANITIZER_INTERCEPT_MMAP64
6990 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
6991             OFF64_T off) {
6992   void *ctx;
6993   if (common_flags()->detect_write_exec)
6994     ReportMmapWriteExec(prot);
6995   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
6996     return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
6997   COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
6998   COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
6999 }
7000 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7001 #else
7002 #define INIT_MMAP64
7003 #endif
7004
7005 #if SANITIZER_INTERCEPT_DEVNAME
7006 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7007   void *ctx;
7008   char *name;
7009   COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7010   name = REAL(devname)(dev, type);
7011   if (name)
7012     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7013   return name;
7014 }
7015 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7016 #else
7017 #define INIT_DEVNAME
7018 #endif
7019
7020 #if SANITIZER_INTERCEPT_DEVNAME_R
7021 INTERCEPTOR(int, devname_r, u64 dev, u32 type, char *path, uptr len) {
7022   void *ctx;
7023   int res;
7024   COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7025   res = REAL(devname_r)(dev, type, path, len);
7026   if (!res)
7027     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7028   return res;
7029 }
7030 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7031 #else
7032 #define INIT_DEVNAME_R
7033 #endif
7034
7035 #if SANITIZER_INTERCEPT_FGETLN
7036 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7037   void *ctx;
7038   COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7039   char *str = REAL(fgetln)(stream, len);
7040   if (str && len) {
7041     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7042     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7043   }
7044   return str;
7045 }
7046 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7047 #else
7048 #define INIT_FGETLN
7049 #endif
7050
7051 #if SANITIZER_INTERCEPT_STRMODE
7052 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7053   void *ctx;
7054   COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7055   REAL(strmode)(mode, bp);
7056   if (bp)
7057     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7058 }
7059 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7060 #else
7061 #define INIT_STRMODE
7062 #endif
7063
7064 #if SANITIZER_INTERCEPT_TTYENT
7065 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7066   void *ctx;
7067   COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7068   struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7069   if (ttyent)
7070     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7071   return ttyent;
7072 }
7073 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7074   void *ctx;
7075   COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7076   if (name)
7077     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7078   struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7079   if (ttyent)
7080     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7081   return ttyent;
7082 }
7083 INTERCEPTOR(int, setttyentpath, char *path) {
7084   void *ctx;
7085   COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7086   if (path)
7087     COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7088   return REAL(setttyentpath)(path);
7089 }
7090 #define INIT_TTYENT \
7091   COMMON_INTERCEPT_FUNCTION(getttyent); \
7092   COMMON_INTERCEPT_FUNCTION(getttynam); \
7093   COMMON_INTERCEPT_FUNCTION(setttyentpath)
7094 #else
7095 #define INIT_TTYENT
7096 #endif
7097
7098 #if SANITIZER_INTERCEPT_PROTOENT
7099 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7100   void *ctx;
7101   COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7102   struct __sanitizer_protoent *p = REAL(getprotoent)();
7103   if (p) {
7104     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7105
7106     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7107
7108     SIZE_T pp_size = 1; // One handles the trailing \0
7109
7110     for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7111        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7112
7113     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7114                                    pp_size * sizeof(char **));
7115   }
7116   return p;
7117 }
7118
7119 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7120   void *ctx;
7121   COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7122   if (name)
7123     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7124   struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7125   if (p) {
7126     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7127
7128     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7129
7130     SIZE_T pp_size = 1; // One handles the trailing \0
7131
7132     for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7133        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7134
7135     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7136                                    pp_size * sizeof(char **));
7137   }
7138   return p;
7139 }
7140
7141 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7142   void *ctx;
7143   COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7144   struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7145   if (p) {
7146     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7147
7148     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7149
7150     SIZE_T pp_size = 1; // One handles the trailing \0
7151
7152     for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7153        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7154
7155     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7156                                    pp_size * sizeof(char **));
7157   }
7158   return p;
7159 }
7160 #define INIT_PROTOENT \
7161   COMMON_INTERCEPT_FUNCTION(getprotoent); \
7162   COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7163   COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7164 #else
7165 #define INIT_PROTOENT
7166 #endif
7167
7168 #if SANITIZER_INTERCEPT_NETENT
7169 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7170   void *ctx;
7171   COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7172   struct __sanitizer_netent *n = REAL(getnetent)();
7173   if (n) {
7174     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7175
7176     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7177
7178     SIZE_T nn_size = 1; // One handles the trailing \0
7179
7180     for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7181       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7182
7183     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7184                                    nn_size * sizeof(char **));
7185   }
7186   return n;
7187 }
7188
7189 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7190   void *ctx;
7191   COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7192   if (name)
7193     COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7194   struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7195   if (n) {
7196     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7197
7198     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7199
7200     SIZE_T nn_size = 1; // One handles the trailing \0
7201
7202     for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7203       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7204
7205     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7206                                    nn_size * sizeof(char **));
7207   }
7208   return n;
7209 }
7210
7211 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7212   void *ctx;
7213   COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7214   struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7215   if (n) {
7216     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7217
7218     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7219
7220     SIZE_T nn_size = 1; // One handles the trailing \0
7221
7222     for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7223       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7224
7225     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7226                                    nn_size * sizeof(char **));
7227   }
7228   return n;
7229 }
7230 #define INIT_NETENT \
7231   COMMON_INTERCEPT_FUNCTION(getnetent); \
7232   COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7233   COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7234 #else
7235 #define INIT_NETENT
7236 #endif
7237
7238 static void InitializeCommonInterceptors() {
7239   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
7240   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
7241
7242   INIT_MMAP;
7243   INIT_MMAP64;
7244   INIT_TEXTDOMAIN;
7245   INIT_STRLEN;
7246   INIT_STRNLEN;
7247   INIT_STRNDUP;
7248   INIT___STRNDUP;
7249   INIT_STRCMP;
7250   INIT_STRNCMP;
7251   INIT_STRCASECMP;
7252   INIT_STRNCASECMP;
7253   INIT_STRSTR;
7254   INIT_STRCASESTR;
7255   INIT_STRCHR;
7256   INIT_STRCHRNUL;
7257   INIT_STRRCHR;
7258   INIT_STRSPN;
7259   INIT_STRTOK;
7260   INIT_STRPBRK;
7261   INIT_STRXFRM;
7262   INIT___STRXFRM_L;
7263   INIT_MEMSET;
7264   INIT_MEMMOVE;
7265   INIT_MEMCPY;
7266   INIT_MEMCHR;
7267   INIT_MEMCMP;
7268   INIT_MEMRCHR;
7269   INIT_MEMMEM;
7270   INIT_READ;
7271   INIT_FREAD;
7272   INIT_PREAD;
7273   INIT_PREAD64;
7274   INIT_READV;
7275   INIT_PREADV;
7276   INIT_PREADV64;
7277   INIT_WRITE;
7278   INIT_FWRITE;
7279   INIT_PWRITE;
7280   INIT_PWRITE64;
7281   INIT_WRITEV;
7282   INIT_PWRITEV;
7283   INIT_PWRITEV64;
7284   INIT_FGETS;
7285   INIT_FPUTS;
7286   INIT_PUTS;
7287   INIT_PRCTL;
7288   INIT_LOCALTIME_AND_FRIENDS;
7289   INIT_STRPTIME;
7290   INIT_SCANF;
7291   INIT_ISOC99_SCANF;
7292   INIT_PRINTF;
7293   INIT_PRINTF_L;
7294   INIT_ISOC99_PRINTF;
7295   INIT_FREXP;
7296   INIT_FREXPF_FREXPL;
7297   INIT_GETPWNAM_AND_FRIENDS;
7298   INIT_GETPWNAM_R_AND_FRIENDS;
7299   INIT_GETPWENT;
7300   INIT_FGETPWENT;
7301   INIT_GETPWENT_R;
7302   INIT_SETPWENT;
7303   INIT_CLOCK_GETTIME;
7304   INIT_GETITIMER;
7305   INIT_TIME;
7306   INIT_GLOB;
7307   INIT_GLOB64;
7308   INIT_WAIT;
7309   INIT_WAIT4;
7310   INIT_INET;
7311   INIT_PTHREAD_GETSCHEDPARAM;
7312   INIT_GETADDRINFO;
7313   INIT_GETNAMEINFO;
7314   INIT_GETSOCKNAME;
7315   INIT_GETHOSTBYNAME;
7316   INIT_GETHOSTBYNAME2;
7317   INIT_GETHOSTBYNAME_R;
7318   INIT_GETHOSTBYNAME2_R;
7319   INIT_GETHOSTBYADDR_R;
7320   INIT_GETHOSTENT_R;
7321   INIT_GETSOCKOPT;
7322   INIT_ACCEPT;
7323   INIT_ACCEPT4;
7324   INIT_PACCEPT;
7325   INIT_MODF;
7326   INIT_RECVMSG;
7327   INIT_SENDMSG;
7328   INIT_RECVMMSG;
7329   INIT_SENDMMSG;
7330   INIT_GETPEERNAME;
7331   INIT_IOCTL;
7332   INIT_INET_ATON;
7333   INIT_SYSINFO;
7334   INIT_READDIR;
7335   INIT_READDIR64;
7336   INIT_PTRACE;
7337   INIT_SETLOCALE;
7338   INIT_GETCWD;
7339   INIT_GET_CURRENT_DIR_NAME;
7340   INIT_STRTOIMAX;
7341   INIT_MBSTOWCS;
7342   INIT_MBSNRTOWCS;
7343   INIT_WCSTOMBS;
7344   INIT_WCSNRTOMBS;
7345   INIT_WCRTOMB;
7346   INIT_TCGETATTR;
7347   INIT_REALPATH;
7348   INIT_CANONICALIZE_FILE_NAME;
7349   INIT_CONFSTR;
7350   INIT_SCHED_GETAFFINITY;
7351   INIT_SCHED_GETPARAM;
7352   INIT_STRERROR;
7353   INIT_STRERROR_R;
7354   INIT_XPG_STRERROR_R;
7355   INIT_SCANDIR;
7356   INIT_SCANDIR64;
7357   INIT_GETGROUPS;
7358   INIT_POLL;
7359   INIT_PPOLL;
7360   INIT_WORDEXP;
7361   INIT_SIGWAIT;
7362   INIT_SIGWAITINFO;
7363   INIT_SIGTIMEDWAIT;
7364   INIT_SIGSETOPS;
7365   INIT_SIGPENDING;
7366   INIT_SIGPROCMASK;
7367   INIT_BACKTRACE;
7368   INIT__EXIT;
7369   INIT_PTHREAD_MUTEX_LOCK;
7370   INIT_PTHREAD_MUTEX_UNLOCK;
7371   INIT___PTHREAD_MUTEX_LOCK;
7372   INIT___PTHREAD_MUTEX_UNLOCK;
7373   INIT___LIBC_MUTEX_LOCK;
7374   INIT___LIBC_MUTEX_UNLOCK;
7375   INIT___LIBC_THR_SETCANCELSTATE;
7376   INIT_GETMNTENT;
7377   INIT_GETMNTENT_R;
7378   INIT_STATFS;
7379   INIT_STATFS64;
7380   INIT_STATVFS;
7381   INIT_STATVFS64;
7382   INIT_INITGROUPS;
7383   INIT_ETHER_NTOA_ATON;
7384   INIT_ETHER_HOST;
7385   INIT_ETHER_R;
7386   INIT_SHMCTL;
7387   INIT_RANDOM_R;
7388   INIT_PTHREAD_ATTR_GET;
7389   INIT_PTHREAD_ATTR_GET_SCHED;
7390   INIT_PTHREAD_ATTR_GETINHERITSCHED;
7391   INIT_PTHREAD_ATTR_GETAFFINITY_NP;
7392   INIT_PTHREAD_MUTEXATTR_GETPSHARED;
7393   INIT_PTHREAD_MUTEXATTR_GETTYPE;
7394   INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
7395   INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
7396   INIT_PTHREAD_MUTEXATTR_GETROBUST;
7397   INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
7398   INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
7399   INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
7400   INIT_PTHREAD_CONDATTR_GETPSHARED;
7401   INIT_PTHREAD_CONDATTR_GETCLOCK;
7402   INIT_PTHREAD_BARRIERATTR_GETPSHARED;
7403   INIT_TMPNAM;
7404   INIT_TMPNAM_R;
7405   INIT_TTYNAME_R;
7406   INIT_TEMPNAM;
7407   INIT_PTHREAD_SETNAME_NP;
7408   INIT_PTHREAD_GETNAME_NP;
7409   INIT_SINCOS;
7410   INIT_REMQUO;
7411   INIT_LGAMMA;
7412   INIT_LGAMMA_R;
7413   INIT_LGAMMAL_R;
7414   INIT_DRAND48_R;
7415   INIT_RAND_R;
7416   INIT_GETLINE;
7417   INIT_ICONV;
7418   INIT_TIMES;
7419   INIT_TLS_GET_ADDR;
7420   INIT_LISTXATTR;
7421   INIT_GETXATTR;
7422   INIT_GETRESID;
7423   INIT_GETIFADDRS;
7424   INIT_IF_INDEXTONAME;
7425   INIT_CAPGET;
7426   INIT_AEABI_MEM;
7427   INIT___BZERO;
7428   INIT_FTIME;
7429   INIT_XDR;
7430   INIT_TSEARCH;
7431   INIT_LIBIO_INTERNALS;
7432   INIT_FOPEN;
7433   INIT_FOPEN64;
7434   INIT_OPEN_MEMSTREAM;
7435   INIT_OBSTACK;
7436   INIT_FFLUSH;
7437   INIT_FCLOSE;
7438   INIT_DLOPEN_DLCLOSE;
7439   INIT_GETPASS;
7440   INIT_TIMERFD;
7441   INIT_MLOCKX;
7442   INIT_FOPENCOOKIE;
7443   INIT_SEM;
7444   INIT_PTHREAD_SETCANCEL;
7445   INIT_MINCORE;
7446   INIT_PROCESS_VM_READV;
7447   INIT_CTERMID;
7448   INIT_CTERMID_R;
7449   INIT_RECV_RECVFROM;
7450   INIT_SEND_SENDTO;
7451   INIT_STAT;
7452   INIT_EVENTFD_READ_WRITE;
7453   INIT_LSTAT;
7454   INIT___XSTAT;
7455   INIT___XSTAT64;
7456   INIT___LXSTAT;
7457   INIT___LXSTAT64;
7458   // FIXME: add other *stat interceptors.
7459   INIT_UTMP;
7460   INIT_UTMPX;
7461   INIT_GETLOADAVG;
7462   INIT_WCSLEN;
7463   INIT_WCSCAT;
7464   INIT_WCSXFRM;
7465   INIT___WCSXFRM_L;
7466   INIT_ACCT;
7467   INIT_USER_FROM_UID;
7468   INIT_UID_FROM_USER;
7469   INIT_GROUP_FROM_GID;
7470   INIT_GID_FROM_GROUP;
7471   INIT_ACCESS;
7472   INIT_FACCESSAT;
7473   INIT_GETGROUPLIST;
7474   INIT_GETGROUPMEMBERSHIP;
7475   INIT_READLINK;
7476   INIT_READLINKAT;
7477   INIT_NAME_TO_HANDLE_AT;
7478   INIT_OPEN_BY_HANDLE_AT;
7479   INIT_STRLCPY;
7480   INIT_DEVNAME;
7481   INIT_DEVNAME_R;
7482   INIT_FGETLN;
7483   INIT_STRMODE;
7484   INIT_TTYENT;
7485   INIT_PROTOENT;
7486   INIT_NETENT;
7487
7488   INIT___PRINTF_CHK;
7489 }