Fix date
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_common_interceptors.inc
blob195014022a045774fa631e07a7795431377ec7f9
1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Common function interceptors for tools like AddressSanitizer,
9 // ThreadSanitizer, MemorySanitizer, etc.
11 // This file should be included into the tool's interceptor file,
12 // which has to define its own macros:
13 //   COMMON_INTERCEPTOR_ENTER
14 //   COMMON_INTERCEPTOR_ENTER_NOIGNORE
15 //   COMMON_INTERCEPTOR_READ_RANGE
16 //   COMMON_INTERCEPTOR_WRITE_RANGE
17 //   COMMON_INTERCEPTOR_INITIALIZE_RANGE
18 //   COMMON_INTERCEPTOR_DIR_ACQUIRE
19 //   COMMON_INTERCEPTOR_FD_ACQUIRE
20 //   COMMON_INTERCEPTOR_FD_RELEASE
21 //   COMMON_INTERCEPTOR_FD_ACCESS
22 //   COMMON_INTERCEPTOR_SET_THREAD_NAME
23 //   COMMON_INTERCEPTOR_ON_DLOPEN
24 //   COMMON_INTERCEPTOR_ON_EXIT
25 //   COMMON_INTERCEPTOR_MUTEX_LOCK
26 //   COMMON_INTERCEPTOR_MUTEX_UNLOCK
27 //   COMMON_INTERCEPTOR_MUTEX_REPAIR
28 //   COMMON_INTERCEPTOR_SET_PTHREAD_NAME
29 //   COMMON_INTERCEPTOR_HANDLE_RECVMSG
30 //   COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
31 //===----------------------------------------------------------------------===//
33 #include "interception/interception.h"
34 #include "sanitizer_addrhashmap.h"
35 #include "sanitizer_placement_new.h"
36 #include "sanitizer_platform_interceptors.h"
37 #include "sanitizer_tls_get_addr.h"
39 #include <stdarg.h>
41 #if SANITIZER_INTERCEPTOR_HOOKS
42 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)                                     \
43   do {                                                                         \
44     if (f)                                                                     \
45       f(__VA_ARGS__);                                                          \
46   } while (false);
47 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)                                  \
48   extern "C" {                                                                 \
49   SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__);  \
50   } // extern "C"
51 #else
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
55 #endif  // SANITIZER_INTERCEPTOR_HOOKS
57 #if SANITIZER_WINDOWS && !defined(va_copy)
58 #define va_copy(dst, src) ((dst) = (src))
59 #endif // _WIN32
61 #if SANITIZER_FREEBSD
62 #define pthread_setname_np pthread_set_name_np
63 #define inet_aton __inet_aton
64 #define inet_pton __inet_pton
65 #define iconv __bsd_iconv
66 #endif
68 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
69 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
70 #endif
72 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
73 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
74 #endif
76 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
77 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
78 #endif
80 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
81 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
82 #endif
84 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
85 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
86 #endif
88 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
89 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
90 #endif
92 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
93 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
94 #endif
96 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
97 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
98 #endif
100 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
101 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
102 #endif
104 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
105 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
106 #endif
108 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
109 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
110 #endif
112 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
113 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
114 #endif
116 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
117 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
118   COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
119 #endif
121 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
122 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
123 #endif
125 #define COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n)       \
126     COMMON_INTERCEPTOR_READ_RANGE((ctx), (s),                       \
127       common_flags()->strict_string_checks ? (len) + 1 : (n) )
129 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n)                   \
130     COMMON_INTERCEPTOR_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
132 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
133 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) {}
134 #endif
136 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
137 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
138 #endif
140 #ifndef COMMON_INTERCEPTOR_ACQUIRE
141 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
142 #endif
144 #ifndef COMMON_INTERCEPTOR_RELEASE
145 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
146 #endif
148 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
149 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
150 #endif
152 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
153 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
154 #endif
156 #ifdef SANITIZER_NLDBL_VERSION
157 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
158     COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
159 #else
160 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
161     COMMON_INTERCEPT_FUNCTION(fn)
162 #endif
164 struct FileMetadata {
165   // For open_memstream().
166   char **addr;
167   SIZE_T *size;
170 struct CommonInterceptorMetadata {
171   enum {
172     CIMT_INVALID = 0,
173     CIMT_FILE
174   } type;
175   union {
176     FileMetadata file;
177   };
180 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
182 static MetadataHashMap *interceptor_metadata_map;
184 #if SI_NOT_WINDOWS
185 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
186                                           const FileMetadata &file) {
187   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
188   CHECK(h.created());
189   h->type = CommonInterceptorMetadata::CIMT_FILE;
190   h->file = file;
193 UNUSED static const FileMetadata *GetInterceptorMetadata(
194     __sanitizer_FILE *addr) {
195   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
196                             /* remove */ false,
197                             /* create */ false);
198   if (h.exists()) {
199     CHECK(!h.created());
200     CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
201     return &h->file;
202   } else {
203     return 0;
204   }
207 UNUSED static void DeleteInterceptorMetadata(void *addr) {
208   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
209   CHECK(h.exists());
211 #endif  // SI_NOT_WINDOWS
213 #if SANITIZER_INTERCEPT_STRLEN
214 INTERCEPTOR(SIZE_T, strlen, const char *s) {
215   // Sometimes strlen is called prior to InitializeCommonInterceptors,
216   // in which case the REAL(strlen) typically used in
217   // COMMON_INTERCEPTOR_ENTER will fail.  We use internal_strlen here
218   // to handle that.
219   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
220     return internal_strlen(s);
221   void *ctx;
222   COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
223   SIZE_T result = REAL(strlen)(s);
224   if (common_flags()->intercept_strlen)
225     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
226   return result;
228 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
229 #else
230 #define INIT_STRLEN
231 #endif
233 #if SANITIZER_INTERCEPT_STRNLEN
234 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
235   void *ctx;
236   COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
237   SIZE_T length = REAL(strnlen)(s, maxlen);
238   if (common_flags()->intercept_strlen)
239     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
240   return length;
242 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
243 #else
244 #define INIT_STRNLEN
245 #endif
247 #if SANITIZER_INTERCEPT_TEXTDOMAIN
248 INTERCEPTOR(char*, textdomain, const char *domainname) {
249   void *ctx;
250   COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
251   COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
252   char *domain = REAL(textdomain)(domainname);
253   if (domain) {
254     COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
255   }
256   return domain;
258 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
259 #else
260 #define INIT_TEXTDOMAIN
261 #endif
263 #if SANITIZER_INTERCEPT_STRCMP
264 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
265   return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
268 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
269                               const char *s1, const char *s2, int result)
271 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
272   void *ctx;
273   COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
274   unsigned char c1, c2;
275   uptr i;
276   for (i = 0;; i++) {
277     c1 = (unsigned char)s1[i];
278     c2 = (unsigned char)s2[i];
279     if (c1 != c2 || c1 == '\0') break;
280   }
281   COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
282   COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
283   int result = CharCmpX(c1, c2);
284   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
285                              s2, result);
286   return result;
289 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
290                               const char *s1, const char *s2, uptr n,
291                               int result)
293 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
294   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
295     return internal_strncmp(s1, s2, size);
296   void *ctx;
297   COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
298   unsigned char c1 = 0, c2 = 0;
299   uptr i;
300   for (i = 0; i < size; i++) {
301     c1 = (unsigned char)s1[i];
302     c2 = (unsigned char)s2[i];
303     if (c1 != c2 || c1 == '\0') break;
304   }
305   COMMON_INTERCEPTOR_READ_STRING(ctx, s1, Min(i + 1, size));
306   COMMON_INTERCEPTOR_READ_STRING(ctx, s2, Min(i + 1, size));
307   int result = CharCmpX(c1, c2);
308   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
309                              s2, size, result);
310   return result;
313 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
314 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
315 #else
316 #define INIT_STRCMP
317 #define INIT_STRNCMP
318 #endif
320 #if SANITIZER_INTERCEPT_STRCASECMP
321 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
322   int c1_low = ToLower(c1);
323   int c2_low = ToLower(c2);
324   return c1_low - c2_low;
327 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
328                               const char *s1, const char *s2, int result)
330 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
331   void *ctx;
332   COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
333   unsigned char c1 = 0, c2 = 0;
334   uptr i;
335   for (i = 0;; i++) {
336     c1 = (unsigned char)s1[i];
337     c2 = (unsigned char)s2[i];
338     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
339   }
340   COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
341   COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
342   int result = CharCaseCmp(c1, c2);
343   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
344                              s1, s2, result);
345   return result;
348 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
349                               const char *s1, const char *s2, uptr n,
350                               int result)
352 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
353   void *ctx;
354   COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
355   unsigned char c1 = 0, c2 = 0;
356   uptr i;
357   for (i = 0; i < n; i++) {
358     c1 = (unsigned char)s1[i];
359     c2 = (unsigned char)s2[i];
360     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
361   }
362   COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
363   COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
364   int result = CharCaseCmp(c1, c2);
365   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
366                              s1, s2, n, result);
367   return result;
370 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
371 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
372 #else
373 #define INIT_STRCASECMP
374 #define INIT_STRNCASECMP
375 #endif
377 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
378 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
379                                const char *s2) {
380     uptr len1 = REAL(strlen)(s1);
381     uptr len2 = REAL(strlen)(s2);
382     COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s1, len1,
383                                           r ? r - s1 + len2 : len1 + 1);
384     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
386 #endif
388 #if SANITIZER_INTERCEPT_STRSTR
390 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
391                               const char *s1, const char *s2, char *result)
393 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
394   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
395     return internal_strstr(s1, s2);
396   void *ctx;
397   COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
398   char *r = REAL(strstr)(s1, s2);
399   if (common_flags()->intercept_strstr)
400     StrstrCheck(ctx, r, s1, s2);
401   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
402                              s2, r);
403   return r;
406 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
407 #else
408 #define INIT_STRSTR
409 #endif
411 #if SANITIZER_INTERCEPT_STRCASESTR
413 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
414                               const char *s1, const char *s2, char *result)
416 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
417   void *ctx;
418   COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
419   char *r = REAL(strcasestr)(s1, s2);
420   if (common_flags()->intercept_strstr)
421     StrstrCheck(ctx, r, s1, s2);
422   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
423                              s1, s2, r);
424   return r;
427 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
428 #else
429 #define INIT_STRCASESTR
430 #endif
432 #if SANITIZER_INTERCEPT_MEMMEM
433 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
434                               const void *s1, SIZE_T len1, const void *s2,
435                               SIZE_T len2, void *result)
437 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
438             SIZE_T len2) {
439   void *ctx;
440   COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
441   void *r = REAL(memmem)(s1, len1, s2, len2);
442   if (common_flags()->intercept_memmem) {
443     COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
444     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
445   }
446   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
447                              s1, len1, s2, len2, r);
448   return r;
451 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
452 #else
453 #define INIT_MEMMEM
454 #endif  // SANITIZER_INTERCEPT_MEMMEM
456 #if SANITIZER_INTERCEPT_STRCHR
457 INTERCEPTOR(char*, strchr, const char *s, int c) {
458   void *ctx;
459   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
460     return internal_strchr(s, c);
461   COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
462   char *result = REAL(strchr)(s, c);
463   uptr len = internal_strlen(s);
464   uptr n = result ? result - s + 1 : len + 1;
465   if (common_flags()->intercept_strchr)
466     COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n);
467   return result;
469 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
470 #else
471 #define INIT_STRCHR
472 #endif
474 #if SANITIZER_INTERCEPT_STRCHRNUL
475 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
476   void *ctx;
477   COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
478   char *result = REAL(strchrnul)(s, c);
479   uptr len = result - s + 1;
480   if (common_flags()->intercept_strchr)
481     COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
482   return result;
484 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
485 #else
486 #define INIT_STRCHRNUL
487 #endif
489 #if SANITIZER_INTERCEPT_STRRCHR
490 INTERCEPTOR(char*, strrchr, const char *s, int c) {
491   void *ctx;
492   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
493     return internal_strrchr(s, c);
494   COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
495   uptr len = internal_strlen(s);
496   if (common_flags()->intercept_strchr)
497     COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, len + 1);
498   return REAL(strrchr)(s, c);
500 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
501 #else
502 #define INIT_STRRCHR
503 #endif
505 #if SANITIZER_INTERCEPT_STRSPN
506 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
507   void *ctx;
508   COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
509   SIZE_T r = REAL(strspn)(s1, s2);
510   if (common_flags()->intercept_strspn) {
511     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
512     COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
513   }
514   return r;
517 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
518   void *ctx;
519   COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
520   SIZE_T r = REAL(strcspn)(s1, s2);
521   if (common_flags()->intercept_strspn) {
522     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
523     COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
524   }
525   return r;
528 #define INIT_STRSPN \
529   COMMON_INTERCEPT_FUNCTION(strspn); \
530   COMMON_INTERCEPT_FUNCTION(strcspn);
531 #else
532 #define INIT_STRSPN
533 #endif
535 #if SANITIZER_INTERCEPT_STRPBRK
536 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
537   void *ctx;
538   COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
539   char *r = REAL(strpbrk)(s1, s2);
540   if (common_flags()->intercept_strpbrk) {
541     COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
542     COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
543         r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
544   }
545   return r;
548 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
549 #else
550 #define INIT_STRPBRK
551 #endif
553 #if SANITIZER_INTERCEPT_MEMSET
554 INTERCEPTOR(void*, memset, void *dst, int v, uptr size) {
555   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
556     return internal_memset(dst, v, size);
557   void *ctx;
558   COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size);
559   if (common_flags()->intercept_intrin)
560     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
561   return REAL(memset)(dst, v, size);
564 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
565 #else
566 #define INIT_MEMSET
567 #endif
569 #if SANITIZER_INTERCEPT_MEMMOVE
570 INTERCEPTOR(void*, memmove, void *dst, const void *src, uptr size) {
571   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
572     return internal_memmove(dst, src, size);
573   void *ctx;
574   COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size);
575   if (common_flags()->intercept_intrin) {
576     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
577     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);
578   }
579   return REAL(memmove)(dst, src, size);
582 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
583 #else
584 #define INIT_MEMMOVE
585 #endif
587 #if SANITIZER_INTERCEPT_MEMCPY
588 INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) {
589   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {
590     // On OS X, calling internal_memcpy here will cause memory corruptions,
591     // because memcpy and memmove are actually aliases of the same
592     // implementation.  We need to use internal_memmove here.
593     return internal_memmove(dst, src, size);
594   }
595   void *ctx;
596   COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size);
597   if (common_flags()->intercept_intrin) {
598     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
599     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);
600   }
601   // N.B.: If we switch this to internal_ we'll have to use internal_memmove
602   // due to memcpy being an alias of memmove on OS X.
603   return REAL(memcpy)(dst, src, size);
606 #define INIT_MEMCPY COMMON_INTERCEPT_FUNCTION(memcpy)
607 #else
608 #define INIT_MEMCPY
609 #endif
611 #if SANITIZER_INTERCEPT_MEMCMP
613 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
614                               const void *s1, const void *s2, uptr n,
615                               int result)
617 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
618   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
619     return internal_memcmp(a1, a2, size);
620   void *ctx;
621   COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
622   if (common_flags()->intercept_memcmp) {
623     if (common_flags()->strict_memcmp) {
624       // Check the entire regions even if the first bytes of the buffers are
625       // different.
626       COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
627       COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
628       // Fallthrough to REAL(memcmp) below.
629     } else {
630       unsigned char c1 = 0, c2 = 0;
631       const unsigned char *s1 = (const unsigned char*)a1;
632       const unsigned char *s2 = (const unsigned char*)a2;
633       uptr i;
634       for (i = 0; i < size; i++) {
635         c1 = s1[i];
636         c2 = s2[i];
637         if (c1 != c2) break;
638       }
639       COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
640       COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
641       int r = CharCmpX(c1, c2);
642       CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
643                                  a1, a2, size, r);
644       return r;
645     }
646   }
647   int result = REAL(memcmp(a1, a2, size));
648   CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
649                              a2, size, result);
650   return result;
653 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
654 #else
655 #define INIT_MEMCMP
656 #endif
658 #if SANITIZER_INTERCEPT_MEMCHR
659 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
660   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
661     return internal_memchr(s, c, n);
662   void *ctx;
663   COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
664 #if SANITIZER_WINDOWS
665   void *res;
666   if (REAL(memchr)) {
667     res = REAL(memchr)(s, c, n);
668   } else {
669     res = internal_memchr(s, c, n);
670   }
671 #else
672   void *res = REAL(memchr)(s, c, n);
673 #endif
674   uptr len = res ? (char *)res - (const char *)s + 1 : n;
675   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
676   return res;
679 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
680 #else
681 #define INIT_MEMCHR
682 #endif
684 #if SANITIZER_INTERCEPT_MEMRCHR
685 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
686   void *ctx;
687   COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
688   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
689   return REAL(memrchr)(s, c, n);
692 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
693 #else
694 #define INIT_MEMRCHR
695 #endif
697 #if SANITIZER_INTERCEPT_FREXP
698 INTERCEPTOR(double, frexp, double x, int *exp) {
699   void *ctx;
700   COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
701   // Assuming frexp() always writes to |exp|.
702   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
703   double res = REAL(frexp)(x, exp);
704   return res;
707 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
708 #else
709 #define INIT_FREXP
710 #endif  // SANITIZER_INTERCEPT_FREXP
712 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
713 INTERCEPTOR(float, frexpf, float x, int *exp) {
714   void *ctx;
715   COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
716   // FIXME: under ASan the call below may write to freed memory and corrupt
717   // its metadata. See
718   // https://github.com/google/sanitizers/issues/321.
719   float res = REAL(frexpf)(x, exp);
720   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
721   return res;
724 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
725   void *ctx;
726   COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
727   // FIXME: under ASan the call below may write to freed memory and corrupt
728   // its metadata. See
729   // https://github.com/google/sanitizers/issues/321.
730   long double res = REAL(frexpl)(x, exp);
731   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
732   return res;
735 #define INIT_FREXPF_FREXPL           \
736   COMMON_INTERCEPT_FUNCTION(frexpf); \
737   COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
738 #else
739 #define INIT_FREXPF_FREXPL
740 #endif  // SANITIZER_INTERCEPT_FREXPF_FREXPL
742 #if SI_NOT_WINDOWS
743 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
744                         SIZE_T iovlen, SIZE_T maxlen) {
745   for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
746     SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
747     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
748     maxlen -= sz;
749   }
752 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
753                        SIZE_T iovlen, SIZE_T maxlen) {
754   COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
755   for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
756     SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
757     COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
758     maxlen -= sz;
759   }
761 #endif
763 #if SANITIZER_INTERCEPT_READ
764 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
765   void *ctx;
766   COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
767   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
768   // FIXME: under ASan the call below may write to freed memory and corrupt
769   // its metadata. See
770   // https://github.com/google/sanitizers/issues/321.
771   SSIZE_T res = REAL(read)(fd, ptr, count);
772   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
773   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
774   return res;
776 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
777 #else
778 #define INIT_READ
779 #endif
781 #if SANITIZER_INTERCEPT_PREAD
782 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
783   void *ctx;
784   COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
785   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
786   // FIXME: under ASan the call below may write to freed memory and corrupt
787   // its metadata. See
788   // https://github.com/google/sanitizers/issues/321.
789   SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
790   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
791   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
792   return res;
794 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
795 #else
796 #define INIT_PREAD
797 #endif
799 #if SANITIZER_INTERCEPT_PREAD64
800 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
801   void *ctx;
802   COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
803   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
804   // FIXME: under ASan the call below may write to freed memory and corrupt
805   // its metadata. See
806   // https://github.com/google/sanitizers/issues/321.
807   SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
808   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
809   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
810   return res;
812 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
813 #else
814 #define INIT_PREAD64
815 #endif
817 #if SANITIZER_INTERCEPT_READV
818 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
819                         int iovcnt) {
820   void *ctx;
821   COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
822   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
823   SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
824   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
825   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
826   return res;
828 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
829 #else
830 #define INIT_READV
831 #endif
833 #if SANITIZER_INTERCEPT_PREADV
834 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
835             OFF_T offset) {
836   void *ctx;
837   COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
838   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
839   SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
840   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
841   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
842   return res;
844 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
845 #else
846 #define INIT_PREADV
847 #endif
849 #if SANITIZER_INTERCEPT_PREADV64
850 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
851             OFF64_T offset) {
852   void *ctx;
853   COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
854   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
855   SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
856   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
857   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
858   return res;
860 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
861 #else
862 #define INIT_PREADV64
863 #endif
865 #if SANITIZER_INTERCEPT_WRITE
866 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
867   void *ctx;
868   COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
869   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
870   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
871   SSIZE_T res = REAL(write)(fd, ptr, count);
872   // FIXME: this check should be _before_ the call to REAL(write), not after
873   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
874   return res;
876 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
877 #else
878 #define INIT_WRITE
879 #endif
881 #if SANITIZER_INTERCEPT_PWRITE
882 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
883   void *ctx;
884   COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
885   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
886   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
887   SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
888   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
889   return res;
891 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
892 #else
893 #define INIT_PWRITE
894 #endif
896 #if SANITIZER_INTERCEPT_PWRITE64
897 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
898             OFF64_T offset) {
899   void *ctx;
900   COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
901   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
902   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
903   SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
904   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
905   return res;
907 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
908 #else
909 #define INIT_PWRITE64
910 #endif
912 #if SANITIZER_INTERCEPT_WRITEV
913 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
914                         int iovcnt) {
915   void *ctx;
916   COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
917   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
918   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
919   SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
920   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
921   return res;
923 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
924 #else
925 #define INIT_WRITEV
926 #endif
928 #if SANITIZER_INTERCEPT_PWRITEV
929 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
930             OFF_T offset) {
931   void *ctx;
932   COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
933   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
934   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
935   SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
936   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
937   return res;
939 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
940 #else
941 #define INIT_PWRITEV
942 #endif
944 #if SANITIZER_INTERCEPT_PWRITEV64
945 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
946             OFF64_T offset) {
947   void *ctx;
948   COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
949   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
950   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
951   SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
952   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
953   return res;
955 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
956 #else
957 #define INIT_PWRITEV64
958 #endif
960 #if SANITIZER_INTERCEPT_PRCTL
961 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
962             unsigned long arg3,                        // NOLINT
963             unsigned long arg4, unsigned long arg5) {  // NOLINT
964   void *ctx;
965   COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
966   static const int PR_SET_NAME = 15;
967   int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
968   if (option == PR_SET_NAME) {
969     char buff[16];
970     internal_strncpy(buff, (char *)arg2, 15);
971     buff[15] = 0;
972     COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
973   }
974   return res;
976 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
977 #else
978 #define INIT_PRCTL
979 #endif  // SANITIZER_INTERCEPT_PRCTL
981 #if SANITIZER_INTERCEPT_TIME
982 INTERCEPTOR(unsigned long, time, unsigned long *t) {
983   void *ctx;
984   COMMON_INTERCEPTOR_ENTER(ctx, time, t);
985   unsigned long local_t;
986   unsigned long res = REAL(time)(&local_t);
987   if (t && res != (unsigned long)-1) {
988     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
989     *t = local_t;
990   }
991   return res;
993 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
994 #else
995 #define INIT_TIME
996 #endif  // SANITIZER_INTERCEPT_TIME
998 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
999 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1000   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1001   if (tm->tm_zone) {
1002     // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1003     // can point to shared memory and tsan would report a data race.
1004     COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1005                                         REAL(strlen(tm->tm_zone)) + 1);
1006   }
1008 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1009   void *ctx;
1010   COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1011   __sanitizer_tm *res = REAL(localtime)(timep);
1012   if (res) {
1013     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1014     unpoison_tm(ctx, res);
1015   }
1016   return res;
1018 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1019   void *ctx;
1020   COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1021   __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1022   if (res) {
1023     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1024     unpoison_tm(ctx, res);
1025   }
1026   return res;
1028 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1029   void *ctx;
1030   COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1031   __sanitizer_tm *res = REAL(gmtime)(timep);
1032   if (res) {
1033     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1034     unpoison_tm(ctx, res);
1035   }
1036   return res;
1038 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1039   void *ctx;
1040   COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1041   __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1042   if (res) {
1043     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1044     unpoison_tm(ctx, res);
1045   }
1046   return res;
1048 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1049   void *ctx;
1050   COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1051   // FIXME: under ASan the call below may write to freed memory and corrupt
1052   // its metadata. See
1053   // https://github.com/google/sanitizers/issues/321.
1054   char *res = REAL(ctime)(timep);
1055   if (res) {
1056     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1057     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1058   }
1059   return res;
1061 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1062   void *ctx;
1063   COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1064   // FIXME: under ASan the call below may write to freed memory and corrupt
1065   // its metadata. See
1066   // https://github.com/google/sanitizers/issues/321.
1067   char *res = REAL(ctime_r)(timep, result);
1068   if (res) {
1069     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1070     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1071   }
1072   return res;
1074 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1075   void *ctx;
1076   COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1077   // FIXME: under ASan the call below may write to freed memory and corrupt
1078   // its metadata. See
1079   // https://github.com/google/sanitizers/issues/321.
1080   char *res = REAL(asctime)(tm);
1081   if (res) {
1082     COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1083     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1084   }
1085   return res;
1087 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1088   void *ctx;
1089   COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1090   // FIXME: under ASan the call below may write to freed memory and corrupt
1091   // its metadata. See
1092   // https://github.com/google/sanitizers/issues/321.
1093   char *res = REAL(asctime_r)(tm, result);
1094   if (res) {
1095     COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1096     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1097   }
1098   return res;
1100 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1101   void *ctx;
1102   COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1103   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1104   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1105   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1106   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1107   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1108   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1109   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1110   long res = REAL(mktime)(tm);
1111   if (res != -1) unpoison_tm(ctx, tm);
1112   return res;
1114 #define INIT_LOCALTIME_AND_FRIENDS        \
1115   COMMON_INTERCEPT_FUNCTION(localtime);   \
1116   COMMON_INTERCEPT_FUNCTION(localtime_r); \
1117   COMMON_INTERCEPT_FUNCTION(gmtime);      \
1118   COMMON_INTERCEPT_FUNCTION(gmtime_r);    \
1119   COMMON_INTERCEPT_FUNCTION(ctime);       \
1120   COMMON_INTERCEPT_FUNCTION(ctime_r);     \
1121   COMMON_INTERCEPT_FUNCTION(asctime);     \
1122   COMMON_INTERCEPT_FUNCTION(asctime_r);   \
1123   COMMON_INTERCEPT_FUNCTION(mktime);
1124 #else
1125 #define INIT_LOCALTIME_AND_FRIENDS
1126 #endif  // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1128 #if SANITIZER_INTERCEPT_STRPTIME
1129 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1130   void *ctx;
1131   COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1132   if (format)
1133     COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1134   // FIXME: under ASan the call below may write to freed memory and corrupt
1135   // its metadata. See
1136   // https://github.com/google/sanitizers/issues/321.
1137   char *res = REAL(strptime)(s, format, tm);
1138   COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1139   if (res && tm) {
1140     // Do not call unpoison_tm here, because strptime does not, in fact,
1141     // initialize the entire struct tm. For example, tm_zone pointer is left
1142     // uninitialized.
1143     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1144   }
1145   return res;
1147 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1148 #else
1149 #define INIT_STRPTIME
1150 #endif
1152 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1153 #include "sanitizer_common_interceptors_format.inc"
1155 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...)                              \
1156   {                                                                            \
1157     void *ctx;                                                                 \
1158     va_list ap;                                                                \
1159     va_start(ap, format);                                                      \
1160     COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap);                     \
1161     int res = WRAP(vname)(__VA_ARGS__, ap);                                    \
1162     va_end(ap);                                                                \
1163     return res;                                                                \
1164   }
1166 #endif
1168 #if SANITIZER_INTERCEPT_SCANF
1170 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...)                    \
1171   {                                                                            \
1172     void *ctx;                                                                 \
1173     COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                         \
1174     va_list aq;                                                                \
1175     va_copy(aq, ap);                                                           \
1176     int res = REAL(vname)(__VA_ARGS__);                                        \
1177     if (res > 0)                                                               \
1178       scanf_common(ctx, res, allowGnuMalloc, format, aq);                      \
1179     va_end(aq);                                                                \
1180     return res;                                                                \
1181   }
1183 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1184 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1186 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1187 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1189 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1190 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1192 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1193 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1194 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1196 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1197             va_list ap)
1198 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1200 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1201 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1202 #endif  // SANITIZER_INTERCEPT_ISOC99_SCANF
1204 INTERCEPTOR(int, scanf, const char *format, ...)
1205 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1207 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1208 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1210 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1211 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1213 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1214 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1215 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1217 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1218 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1220 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1221 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1222 #endif
1224 #endif
1226 #if SANITIZER_INTERCEPT_SCANF
1227 #define INIT_SCANF                    \
1228   COMMON_INTERCEPT_FUNCTION(scanf);   \
1229   COMMON_INTERCEPT_FUNCTION(sscanf);  \
1230   COMMON_INTERCEPT_FUNCTION(fscanf);  \
1231   COMMON_INTERCEPT_FUNCTION(vscanf);  \
1232   COMMON_INTERCEPT_FUNCTION(vsscanf); \
1233   COMMON_INTERCEPT_FUNCTION(vfscanf);
1234 #else
1235 #define INIT_SCANF
1236 #endif
1238 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1239 #define INIT_ISOC99_SCANF                      \
1240   COMMON_INTERCEPT_FUNCTION(__isoc99_scanf);   \
1241   COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf);  \
1242   COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf);  \
1243   COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf);  \
1244   COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1245   COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1246 #else
1247 #define INIT_ISOC99_SCANF
1248 #endif
1250 #if SANITIZER_INTERCEPT_PRINTF
1252 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...)                                  \
1253   void *ctx;                                                                   \
1254   COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                           \
1255   va_list aq;                                                                  \
1256   va_copy(aq, ap);
1258 #define VPRINTF_INTERCEPTOR_RETURN()                                           \
1259   va_end(aq);
1261 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...)                                   \
1262   {                                                                            \
1263     VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__);                             \
1264     if (common_flags()->check_printf)                                          \
1265       printf_common(ctx, format, aq);                                          \
1266     int res = REAL(vname)(__VA_ARGS__);                                        \
1267     VPRINTF_INTERCEPTOR_RETURN();                                              \
1268     return res;                                                                \
1269   }
1271 // FIXME: under ASan the REAL() call below may write to freed memory and
1272 // corrupt its metadata. See
1273 // https://github.com/google/sanitizers/issues/321.
1274 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...)                             \
1275   {                                                                            \
1276     VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__)                         \
1277     if (common_flags()->check_printf) {                                        \
1278       printf_common(ctx, format, aq);                                          \
1279     }                                                                          \
1280     int res = REAL(vname)(str, __VA_ARGS__);                                   \
1281     if (res >= 0) {                                                            \
1282       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1);                       \
1283     }                                                                          \
1284     VPRINTF_INTERCEPTOR_RETURN();                                              \
1285     return res;                                                                \
1286   }
1288 // FIXME: under ASan the REAL() call below may write to freed memory and
1289 // corrupt its metadata. See
1290 // https://github.com/google/sanitizers/issues/321.
1291 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...)                      \
1292   {                                                                            \
1293     VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__)                   \
1294     if (common_flags()->check_printf) {                                        \
1295       printf_common(ctx, format, aq);                                          \
1296     }                                                                          \
1297     int res = REAL(vname)(str, size, __VA_ARGS__);                             \
1298     if (res >= 0) {                                                            \
1299       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1)));  \
1300     }                                                                          \
1301     VPRINTF_INTERCEPTOR_RETURN();                                              \
1302     return res;                                                                \
1303   }
1305 // FIXME: under ASan the REAL() call below may write to freed memory and
1306 // corrupt its metadata. See
1307 // https://github.com/google/sanitizers/issues/321.
1308 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...)                           \
1309   {                                                                            \
1310     VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__)                        \
1311     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *));                 \
1312     if (common_flags()->check_printf) {                                        \
1313       printf_common(ctx, format, aq);                                          \
1314     }                                                                          \
1315     int res = REAL(vname)(strp, __VA_ARGS__);                                  \
1316     if (res >= 0) {                                                            \
1317       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1);                     \
1318     }                                                                          \
1319     VPRINTF_INTERCEPTOR_RETURN();                                              \
1320     return res;                                                                \
1321   }
1323 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1324 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1326 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1327             va_list ap)
1328 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1330 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1331             va_list ap)
1332 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1334 #if SANITIZER_INTERCEPT_PRINTF_L
1335 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1336             const char *format, va_list ap)
1337 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1339 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1340             const char *format, ...)
1341 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1342 #endif  // SANITIZER_INTERCEPT_PRINTF_L
1344 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1345 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1347 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1348 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1350 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1351 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1352 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1354 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1355             const char *format, va_list ap)
1356 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1358 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1359             va_list ap)
1360 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1362 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1363             va_list ap)
1364 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1365                           ap)
1367 #endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
1369 INTERCEPTOR(int, printf, const char *format, ...)
1370 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1372 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1373 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1375 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1376 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1378 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1379 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1381 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1382 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1384 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1385 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1386 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1388 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1389             ...)
1390 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1392 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1393 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1395 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1396             const char *format, ...)
1397 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1398                         format)
1400 #endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
1402 #endif  // SANITIZER_INTERCEPT_PRINTF
1404 #if SANITIZER_INTERCEPT_PRINTF
1405 #define INIT_PRINTF                     \
1406   COMMON_INTERCEPT_FUNCTION(printf);    \
1407   COMMON_INTERCEPT_FUNCTION(sprintf);   \
1408   COMMON_INTERCEPT_FUNCTION(snprintf);  \
1409   COMMON_INTERCEPT_FUNCTION(asprintf);  \
1410   COMMON_INTERCEPT_FUNCTION(fprintf);   \
1411   COMMON_INTERCEPT_FUNCTION(vprintf);   \
1412   COMMON_INTERCEPT_FUNCTION(vsprintf);  \
1413   COMMON_INTERCEPT_FUNCTION(vsnprintf); \
1414   COMMON_INTERCEPT_FUNCTION(vasprintf); \
1415   COMMON_INTERCEPT_FUNCTION(vfprintf);
1416 #else
1417 #define INIT_PRINTF
1418 #endif
1420 #if SANITIZER_INTERCEPT_PRINTF_L
1421 #define INIT_PRINTF_L                     \
1422   COMMON_INTERCEPT_FUNCTION(snprintf_l);  \
1423   COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1424 #else
1425 #define INIT_PRINTF_L
1426 #endif
1428 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1429 #define INIT_ISOC99_PRINTF                       \
1430   COMMON_INTERCEPT_FUNCTION(__isoc99_printf);    \
1431   COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf);   \
1432   COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf);  \
1433   COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf);   \
1434   COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf);   \
1435   COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf);  \
1436   COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1437   COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1438 #else
1439 #define INIT_ISOC99_PRINTF
1440 #endif
1442 #if SANITIZER_INTERCEPT_IOCTL
1443 #include "sanitizer_common_interceptors_ioctl.inc"
1444 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1445   // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1446   // can trigger a report and we need to be able to unwind through this
1447   // function.  On Mac in debug mode we might not have a frame pointer, because
1448   // ioctl_common_[pre|post] doesn't get inlined here.
1449   ENABLE_FRAME_POINTER;
1451   void *ctx;
1452   va_list ap;
1453   va_start(ap, request);
1454   void *arg = va_arg(ap, void *);
1455   va_end(ap);
1456   COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1458   CHECK(ioctl_initialized);
1460   // Note: TSan does not use common flags, and they are zero-initialized.
1461   // This effectively disables ioctl handling in TSan.
1462   if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1464   // Although request is unsigned long, the rest of the interceptor uses it
1465   // as just "unsigned" to save space, because we know that all values fit in
1466   // "unsigned" - they are compile-time constants.
1468   const ioctl_desc *desc = ioctl_lookup(request);
1469   ioctl_desc decoded_desc;
1470   if (!desc) {
1471     VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1472     if (!ioctl_decode(request, &decoded_desc))
1473       Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1474     else
1475       desc = &decoded_desc;
1476   }
1478   if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1479   int res = REAL(ioctl)(d, request, arg);
1480   // FIXME: some ioctls have different return values for success and failure.
1481   if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1482   return res;
1484 #define INIT_IOCTL \
1485   ioctl_init();    \
1486   COMMON_INTERCEPT_FUNCTION(ioctl);
1487 #else
1488 #define INIT_IOCTL
1489 #endif
1491 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1492     SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1493     SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1494 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1495   if (pwd) {
1496     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1497     if (pwd->pw_name)
1498       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1499                                           REAL(strlen)(pwd->pw_name) + 1);
1500     if (pwd->pw_passwd)
1501       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1502                                           REAL(strlen)(pwd->pw_passwd) + 1);
1503 #if !SANITIZER_ANDROID
1504     if (pwd->pw_gecos)
1505       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1506                                           REAL(strlen)(pwd->pw_gecos) + 1);
1507 #endif
1508 #if SANITIZER_MAC
1509     if (pwd->pw_class)
1510       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1511                                           REAL(strlen)(pwd->pw_class) + 1);
1512 #endif
1513     if (pwd->pw_dir)
1514       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1515                                           REAL(strlen)(pwd->pw_dir) + 1);
1516     if (pwd->pw_shell)
1517       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1518                                           REAL(strlen)(pwd->pw_shell) + 1);
1519   }
1522 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1523   if (grp) {
1524     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1525     if (grp->gr_name)
1526       COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1527                                           REAL(strlen)(grp->gr_name) + 1);
1528     if (grp->gr_passwd)
1529       COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1530                                           REAL(strlen)(grp->gr_passwd) + 1);
1531     char **p = grp->gr_mem;
1532     for (; *p; ++p) {
1533       COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1534     }
1535     COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1536                                         (p - grp->gr_mem + 1) * sizeof(*p));
1537   }
1539 #endif  // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1540         // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1541         // SANITIZER_INTERCEPT_GETPWENT_R ||
1542         // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1544 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1545 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1546   void *ctx;
1547   COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1548   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1549   __sanitizer_passwd *res = REAL(getpwnam)(name);
1550   if (res) unpoison_passwd(ctx, res);
1551   return res;
1553 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1554   void *ctx;
1555   COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1556   __sanitizer_passwd *res = REAL(getpwuid)(uid);
1557   if (res) unpoison_passwd(ctx, res);
1558   return res;
1560 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1561   void *ctx;
1562   COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1563   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1564   __sanitizer_group *res = REAL(getgrnam)(name);
1565   if (res) unpoison_group(ctx, res);
1566   return res;
1568 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1569   void *ctx;
1570   COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1571   __sanitizer_group *res = REAL(getgrgid)(gid);
1572   if (res) unpoison_group(ctx, res);
1573   return res;
1575 #define INIT_GETPWNAM_AND_FRIENDS      \
1576   COMMON_INTERCEPT_FUNCTION(getpwnam); \
1577   COMMON_INTERCEPT_FUNCTION(getpwuid); \
1578   COMMON_INTERCEPT_FUNCTION(getgrnam); \
1579   COMMON_INTERCEPT_FUNCTION(getgrgid);
1580 #else
1581 #define INIT_GETPWNAM_AND_FRIENDS
1582 #endif
1584 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1585 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1586             char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1587   void *ctx;
1588   COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1589   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1590   // FIXME: under ASan the call below may write to freed memory and corrupt
1591   // its metadata. See
1592   // https://github.com/google/sanitizers/issues/321.
1593   int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1594   if (!res) {
1595     if (result && *result) unpoison_passwd(ctx, *result);
1596     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1597   }
1598   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1599   return res;
1601 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1602             SIZE_T buflen, __sanitizer_passwd **result) {
1603   void *ctx;
1604   COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1605   // FIXME: under ASan the call below may write to freed memory and corrupt
1606   // its metadata. See
1607   // https://github.com/google/sanitizers/issues/321.
1608   int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1609   if (!res) {
1610     if (result && *result) unpoison_passwd(ctx, *result);
1611     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1612   }
1613   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1614   return res;
1616 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1617             char *buf, SIZE_T buflen, __sanitizer_group **result) {
1618   void *ctx;
1619   COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1620   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1621   // FIXME: under ASan the call below may write to freed memory and corrupt
1622   // its metadata. See
1623   // https://github.com/google/sanitizers/issues/321.
1624   int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1625   if (!res) {
1626     if (result && *result) unpoison_group(ctx, *result);
1627     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1628   }
1629   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1630   return res;
1632 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1633             SIZE_T buflen, __sanitizer_group **result) {
1634   void *ctx;
1635   COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1636   // FIXME: under ASan the call below may write to freed memory and corrupt
1637   // its metadata. See
1638   // https://github.com/google/sanitizers/issues/321.
1639   int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1640   if (!res) {
1641     if (result && *result) unpoison_group(ctx, *result);
1642     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1643   }
1644   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1645   return res;
1647 #define INIT_GETPWNAM_R_AND_FRIENDS      \
1648   COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1649   COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1650   COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1651   COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1652 #else
1653 #define INIT_GETPWNAM_R_AND_FRIENDS
1654 #endif
1656 #if SANITIZER_INTERCEPT_GETPWENT
1657 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1658   void *ctx;
1659   COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1660   __sanitizer_passwd *res = REAL(getpwent)(dummy);
1661   if (res) unpoison_passwd(ctx, res);
1662   return res;
1664 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1665   void *ctx;
1666   COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1667   __sanitizer_group *res = REAL(getgrent)(dummy);
1668   if (res) unpoison_group(ctx, res);;
1669   return res;
1671 #define INIT_GETPWENT                  \
1672   COMMON_INTERCEPT_FUNCTION(getpwent); \
1673   COMMON_INTERCEPT_FUNCTION(getgrent);
1674 #else
1675 #define INIT_GETPWENT
1676 #endif
1678 #if SANITIZER_INTERCEPT_FGETPWENT
1679 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1680   void *ctx;
1681   COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1682   __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1683   if (res) unpoison_passwd(ctx, res);
1684   return res;
1686 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1687   void *ctx;
1688   COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1689   __sanitizer_group *res = REAL(fgetgrent)(fp);
1690   if (res) unpoison_group(ctx, res);
1691   return res;
1693 #define INIT_FGETPWENT                  \
1694   COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1695   COMMON_INTERCEPT_FUNCTION(fgetgrent);
1696 #else
1697 #define INIT_FGETPWENT
1698 #endif
1700 #if SANITIZER_INTERCEPT_GETPWENT_R
1701 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1702             SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1703   void *ctx;
1704   COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1705   // FIXME: under ASan the call below may write to freed memory and corrupt
1706   // its metadata. See
1707   // https://github.com/google/sanitizers/issues/321.
1708   int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1709   if (!res) {
1710     if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1711     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1712   }
1713   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1714   return res;
1716 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1717             SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1718   void *ctx;
1719   COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1720   // FIXME: under ASan the call below may write to freed memory and corrupt
1721   // its metadata. See
1722   // https://github.com/google/sanitizers/issues/321.
1723   int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1724   if (!res) {
1725     if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1726     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1727   }
1728   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1729   return res;
1731 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1732             __sanitizer_group **pwbufp) {
1733   void *ctx;
1734   COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1735   // FIXME: under ASan the call below may write to freed memory and corrupt
1736   // its metadata. See
1737   // https://github.com/google/sanitizers/issues/321.
1738   int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1739   if (!res) {
1740     if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1741     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1742   }
1743   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1744   return res;
1746 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1747             SIZE_T buflen, __sanitizer_group **pwbufp) {
1748   void *ctx;
1749   COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1750   // FIXME: under ASan the call below may write to freed memory and corrupt
1751   // its metadata. See
1752   // https://github.com/google/sanitizers/issues/321.
1753   int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1754   if (!res) {
1755     if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1756     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1757   }
1758   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1759   return res;
1761 #define INIT_GETPWENT_R                   \
1762   COMMON_INTERCEPT_FUNCTION(getpwent_r);  \
1763   COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1764   COMMON_INTERCEPT_FUNCTION(getgrent_r);  \
1765   COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1766 #else
1767 #define INIT_GETPWENT_R
1768 #endif
1770 #if SANITIZER_INTERCEPT_SETPWENT
1771 // The only thing these interceptors do is disable any nested interceptors.
1772 // These functions may open nss modules and call uninstrumented functions from
1773 // them, and we don't want things like strlen() to trigger.
1774 INTERCEPTOR(void, setpwent, int dummy) {
1775   void *ctx;
1776   COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1777   REAL(setpwent)(dummy);
1779 INTERCEPTOR(void, endpwent, int dummy) {
1780   void *ctx;
1781   COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1782   REAL(endpwent)(dummy);
1784 INTERCEPTOR(void, setgrent, int dummy) {
1785   void *ctx;
1786   COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1787   REAL(setgrent)(dummy);
1789 INTERCEPTOR(void, endgrent, int dummy) {
1790   void *ctx;
1791   COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1792   REAL(endgrent)(dummy);
1794 #define INIT_SETPWENT                  \
1795   COMMON_INTERCEPT_FUNCTION(setpwent); \
1796   COMMON_INTERCEPT_FUNCTION(endpwent); \
1797   COMMON_INTERCEPT_FUNCTION(setgrent); \
1798   COMMON_INTERCEPT_FUNCTION(endgrent);
1799 #else
1800 #define INIT_SETPWENT
1801 #endif
1803 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1804 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1805   void *ctx;
1806   COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1807   // FIXME: under ASan the call below may write to freed memory and corrupt
1808   // its metadata. See
1809   // https://github.com/google/sanitizers/issues/321.
1810   int res = REAL(clock_getres)(clk_id, tp);
1811   if (!res && tp) {
1812     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1813   }
1814   return res;
1816 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1817   void *ctx;
1818   COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1819   // FIXME: under ASan the call below may write to freed memory and corrupt
1820   // its metadata. See
1821   // https://github.com/google/sanitizers/issues/321.
1822   int res = REAL(clock_gettime)(clk_id, tp);
1823   if (!res) {
1824     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1825   }
1826   return res;
1828 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1829   void *ctx;
1830   COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1831   COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1832   return REAL(clock_settime)(clk_id, tp);
1834 #define INIT_CLOCK_GETTIME                  \
1835   COMMON_INTERCEPT_FUNCTION(clock_getres);  \
1836   COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1837   COMMON_INTERCEPT_FUNCTION(clock_settime);
1838 #else
1839 #define INIT_CLOCK_GETTIME
1840 #endif
1842 #if SANITIZER_INTERCEPT_GETITIMER
1843 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1844   void *ctx;
1845   COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1846   // FIXME: under ASan the call below may write to freed memory and corrupt
1847   // its metadata. See
1848   // https://github.com/google/sanitizers/issues/321.
1849   int res = REAL(getitimer)(which, curr_value);
1850   if (!res && curr_value) {
1851     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1852   }
1853   return res;
1855 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1856   void *ctx;
1857   COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1858   if (new_value)
1859     COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1860   // FIXME: under ASan the call below may write to freed memory and corrupt
1861   // its metadata. See
1862   // https://github.com/google/sanitizers/issues/321.
1863   int res = REAL(setitimer)(which, new_value, old_value);
1864   if (!res && old_value) {
1865     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1866   }
1867   return res;
1869 #define INIT_GETITIMER                  \
1870   COMMON_INTERCEPT_FUNCTION(getitimer); \
1871   COMMON_INTERCEPT_FUNCTION(setitimer);
1872 #else
1873 #define INIT_GETITIMER
1874 #endif
1876 #if SANITIZER_INTERCEPT_GLOB
1877 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1878   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1879   // +1 for NULL pointer at the end.
1880   if (pglob->gl_pathv)
1881     COMMON_INTERCEPTOR_WRITE_RANGE(
1882         ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1883   for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1884     char *p = pglob->gl_pathv[i];
1885     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1886   }
1889 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1891 static void wrapped_gl_closedir(void *dir) {
1892   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1893   pglob_copy->gl_closedir(dir);
1896 static void *wrapped_gl_readdir(void *dir) {
1897   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1898   return pglob_copy->gl_readdir(dir);
1901 static void *wrapped_gl_opendir(const char *s) {
1902   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1903   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1904   return pglob_copy->gl_opendir(s);
1907 static int wrapped_gl_lstat(const char *s, void *st) {
1908   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1909   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1910   return pglob_copy->gl_lstat(s, st);
1913 static int wrapped_gl_stat(const char *s, void *st) {
1914   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1915   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1916   return pglob_copy->gl_stat(s, st);
1919 static const __sanitizer_glob_t kGlobCopy = {
1920       0,                  0,                   0,
1921       0,                  wrapped_gl_closedir, wrapped_gl_readdir,
1922       wrapped_gl_opendir, wrapped_gl_lstat,    wrapped_gl_stat};
1924 INTERCEPTOR(int, glob, const char *pattern, int flags,
1925             int (*errfunc)(const char *epath, int eerrno),
1926             __sanitizer_glob_t *pglob) {
1927   void *ctx;
1928   COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1929   COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1930   __sanitizer_glob_t glob_copy;
1931   internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1932   if (flags & glob_altdirfunc) {
1933     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1934     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1935     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1936     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1937     Swap(pglob->gl_stat, glob_copy.gl_stat);
1938     pglob_copy = &glob_copy;
1939   }
1940   int res = REAL(glob)(pattern, flags, errfunc, pglob);
1941   if (flags & glob_altdirfunc) {
1942     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1943     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1944     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1945     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1946     Swap(pglob->gl_stat, glob_copy.gl_stat);
1947   }
1948   pglob_copy = 0;
1949   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1950   return res;
1953 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1954             int (*errfunc)(const char *epath, int eerrno),
1955             __sanitizer_glob_t *pglob) {
1956   void *ctx;
1957   COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1958   COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1959   __sanitizer_glob_t glob_copy;
1960   internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1961   if (flags & glob_altdirfunc) {
1962     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1963     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1964     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1965     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1966     Swap(pglob->gl_stat, glob_copy.gl_stat);
1967     pglob_copy = &glob_copy;
1968   }
1969   int res = REAL(glob64)(pattern, flags, errfunc, pglob);
1970   if (flags & glob_altdirfunc) {
1971     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1972     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1973     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1974     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1975     Swap(pglob->gl_stat, glob_copy.gl_stat);
1976   }
1977   pglob_copy = 0;
1978   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1979   return res;
1981 #define INIT_GLOB                  \
1982   COMMON_INTERCEPT_FUNCTION(glob); \
1983   COMMON_INTERCEPT_FUNCTION(glob64);
1984 #else  // SANITIZER_INTERCEPT_GLOB
1985 #define INIT_GLOB
1986 #endif  // SANITIZER_INTERCEPT_GLOB
1988 #if SANITIZER_INTERCEPT_WAIT
1989 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
1990 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
1991 // details.
1992 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
1993   void *ctx;
1994   COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
1995   // FIXME: under ASan the call below may write to freed memory and corrupt
1996   // its metadata. See
1997   // https://github.com/google/sanitizers/issues/321.
1998   int res = REAL(wait)(status);
1999   if (res != -1 && status)
2000     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2001   return res;
2003 // On FreeBSD id_t is always 64-bit wide.
2004 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2005 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2006                         int options) {
2007 #else
2008 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2009                         int options) {
2010 #endif
2011   void *ctx;
2012   COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2013   // FIXME: under ASan the call below may write to freed memory and corrupt
2014   // its metadata. See
2015   // https://github.com/google/sanitizers/issues/321.
2016   int res = REAL(waitid)(idtype, id, infop, options);
2017   if (res != -1 && infop)
2018     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2019   return res;
2021 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2022   void *ctx;
2023   COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2024   // FIXME: under ASan the call below may write to freed memory and corrupt
2025   // its metadata. See
2026   // https://github.com/google/sanitizers/issues/321.
2027   int res = REAL(waitpid)(pid, status, options);
2028   if (res != -1 && status)
2029     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2030   return res;
2032 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2033   void *ctx;
2034   COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2035   // FIXME: under ASan the call below may write to freed memory and corrupt
2036   // its metadata. See
2037   // https://github.com/google/sanitizers/issues/321.
2038   int res = REAL(wait3)(status, options, rusage);
2039   if (res != -1) {
2040     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2041     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2042   }
2043   return res;
2045 #if SANITIZER_ANDROID
2046 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2047   void *ctx;
2048   COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2049   // FIXME: under ASan the call below may write to freed memory and corrupt
2050   // its metadata. See
2051   // https://github.com/google/sanitizers/issues/321.
2052   int res = REAL(__wait4)(pid, status, options, rusage);
2053   if (res != -1) {
2054     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2055     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2056   }
2057   return res;
2059 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2060 #else
2061 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2062   void *ctx;
2063   COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2064   // FIXME: under ASan the call below may write to freed memory and corrupt
2065   // its metadata. See
2066   // https://github.com/google/sanitizers/issues/321.
2067   int res = REAL(wait4)(pid, status, options, rusage);
2068   if (res != -1) {
2069     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2070     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2071   }
2072   return res;
2074 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2075 #endif  // SANITIZER_ANDROID
2076 #define INIT_WAIT                     \
2077   COMMON_INTERCEPT_FUNCTION(wait);    \
2078   COMMON_INTERCEPT_FUNCTION(waitid);  \
2079   COMMON_INTERCEPT_FUNCTION(waitpid); \
2080   COMMON_INTERCEPT_FUNCTION(wait3);
2081 #else
2082 #define INIT_WAIT
2083 #define INIT_WAIT4
2084 #endif
2086 #if SANITIZER_INTERCEPT_INET
2087 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2088   void *ctx;
2089   COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2090   uptr sz = __sanitizer_in_addr_sz(af);
2091   if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2092   // FIXME: figure out read size based on the address family.
2093   // FIXME: under ASan the call below may write to freed memory and corrupt
2094   // its metadata. See
2095   // https://github.com/google/sanitizers/issues/321.
2096   char *res = REAL(inet_ntop)(af, src, dst, size);
2097   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2098   return res;
2100 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2101   void *ctx;
2102   COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2103   COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2104   // FIXME: figure out read size based on the address family.
2105   // FIXME: under ASan the call below may write to freed memory and corrupt
2106   // its metadata. See
2107   // https://github.com/google/sanitizers/issues/321.
2108   int res = REAL(inet_pton)(af, src, dst);
2109   if (res == 1) {
2110     uptr sz = __sanitizer_in_addr_sz(af);
2111     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2112   }
2113   return res;
2115 #define INIT_INET                       \
2116   COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2117   COMMON_INTERCEPT_FUNCTION(inet_pton);
2118 #else
2119 #define INIT_INET
2120 #endif
2122 #if SANITIZER_INTERCEPT_INET
2123 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2124   void *ctx;
2125   COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2126   if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2127   // FIXME: under ASan the call below may write to freed memory and corrupt
2128   // its metadata. See
2129   // https://github.com/google/sanitizers/issues/321.
2130   int res = REAL(inet_aton)(cp, dst);
2131   if (res != 0) {
2132     uptr sz = __sanitizer_in_addr_sz(af_inet);
2133     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2134   }
2135   return res;
2137 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2138 #else
2139 #define INIT_INET_ATON
2140 #endif
2142 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2143 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2144   void *ctx;
2145   COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2146   // FIXME: under ASan the call below may write to freed memory and corrupt
2147   // its metadata. See
2148   // https://github.com/google/sanitizers/issues/321.
2149   int res = REAL(pthread_getschedparam)(thread, policy, param);
2150   if (res == 0) {
2151     if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2152     if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2153   }
2154   return res;
2156 #define INIT_PTHREAD_GETSCHEDPARAM \
2157   COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2158 #else
2159 #define INIT_PTHREAD_GETSCHEDPARAM
2160 #endif
2162 #if SANITIZER_INTERCEPT_GETADDRINFO
2163 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2164             struct __sanitizer_addrinfo *hints,
2165             struct __sanitizer_addrinfo **out) {
2166   void *ctx;
2167   COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2168   if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2169   if (service)
2170     COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2171   if (hints)
2172     COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2173   // FIXME: under ASan the call below may write to freed memory and corrupt
2174   // its metadata. See
2175   // https://github.com/google/sanitizers/issues/321.
2176   int res = REAL(getaddrinfo)(node, service, hints, out);
2177   if (res == 0 && out) {
2178     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2179     struct __sanitizer_addrinfo *p = *out;
2180     while (p) {
2181       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2182       if (p->ai_addr)
2183         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2184       if (p->ai_canonname)
2185         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2186                                        REAL(strlen)(p->ai_canonname) + 1);
2187       p = p->ai_next;
2188     }
2189   }
2190   return res;
2192 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2193 #else
2194 #define INIT_GETADDRINFO
2195 #endif
2197 #if SANITIZER_INTERCEPT_GETNAMEINFO
2198 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2199             unsigned hostlen, char *serv, unsigned servlen, int flags) {
2200   void *ctx;
2201   COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2202                            serv, servlen, flags);
2203   // FIXME: consider adding READ_RANGE(sockaddr, salen)
2204   // There is padding in in_addr that may make this too noisy
2205   // FIXME: under ASan the call below may write to freed memory and corrupt
2206   // its metadata. See
2207   // https://github.com/google/sanitizers/issues/321.
2208   int res =
2209       REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2210   if (res == 0) {
2211     if (host && hostlen)
2212       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2213     if (serv && servlen)
2214       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2215   }
2216   return res;
2218 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2219 #else
2220 #define INIT_GETNAMEINFO
2221 #endif
2223 #if SANITIZER_INTERCEPT_GETSOCKNAME
2224 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2225   void *ctx;
2226   COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2227   COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2228   int addrlen_in = *addrlen;
2229   // FIXME: under ASan the call below may write to freed memory and corrupt
2230   // its metadata. See
2231   // https://github.com/google/sanitizers/issues/321.
2232   int res = REAL(getsockname)(sock_fd, addr, addrlen);
2233   if (res == 0) {
2234     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2235   }
2236   return res;
2238 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2239 #else
2240 #define INIT_GETSOCKNAME
2241 #endif
2243 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2244 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2245   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2246   if (h->h_name)
2247     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2248   char **p = h->h_aliases;
2249   while (*p) {
2250     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2251     ++p;
2252   }
2253   COMMON_INTERCEPTOR_WRITE_RANGE(
2254       ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2255   p = h->h_addr_list;
2256   while (*p) {
2257     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2258     ++p;
2259   }
2260   COMMON_INTERCEPTOR_WRITE_RANGE(
2261       ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2263 #endif
2265 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2266 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2267   void *ctx;
2268   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2269   struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2270   if (res) write_hostent(ctx, res);
2271   return res;
2274 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2275             int type) {
2276   void *ctx;
2277   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2278   COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2279   struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2280   if (res) write_hostent(ctx, res);
2281   return res;
2284 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2285   void *ctx;
2286   COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2287   struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2288   if (res) write_hostent(ctx, res);
2289   return res;
2292 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2293   void *ctx;
2294   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2295   struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2296   if (res) write_hostent(ctx, res);
2297   return res;
2299 #define INIT_GETHOSTBYNAME                  \
2300   COMMON_INTERCEPT_FUNCTION(gethostent);    \
2301   COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2302   COMMON_INTERCEPT_FUNCTION(gethostbyname); \
2303   COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2304 #else
2305 #define INIT_GETHOSTBYNAME
2306 #endif
2308 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2309 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2310             char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2311             int *h_errnop) {
2312   void *ctx;
2313   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2314                            h_errnop);
2315   // FIXME: under ASan the call below may write to freed memory and corrupt
2316   // its metadata. See
2317   // https://github.com/google/sanitizers/issues/321.
2318   int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2319   if (result) {
2320     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2321     if (res == 0 && *result) write_hostent(ctx, *result);
2322   }
2323   if (h_errnop)
2324     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2325   return res;
2327 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2328 #else
2329 #define INIT_GETHOSTBYNAME_R
2330 #endif
2332 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2333 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2334             SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2335   void *ctx;
2336   COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2337                            h_errnop);
2338   // FIXME: under ASan the call below may write to freed memory and corrupt
2339   // its metadata. See
2340   // https://github.com/google/sanitizers/issues/321.
2341   int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2342   if (result) {
2343     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2344     if (res == 0 && *result) write_hostent(ctx, *result);
2345   }
2346   if (h_errnop)
2347     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2348   return res;
2350 #define INIT_GETHOSTENT_R                  \
2351   COMMON_INTERCEPT_FUNCTION(gethostent_r);
2352 #else
2353 #define INIT_GETHOSTENT_R
2354 #endif
2356 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2357 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2358             struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2359             __sanitizer_hostent **result, int *h_errnop) {
2360   void *ctx;
2361   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2362                            buflen, result, h_errnop);
2363   COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2364   // FIXME: under ASan the call below may write to freed memory and corrupt
2365   // its metadata. See
2366   // https://github.com/google/sanitizers/issues/321.
2367   int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2368                                   h_errnop);
2369   if (result) {
2370     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2371     if (res == 0 && *result) write_hostent(ctx, *result);
2372   }
2373   if (h_errnop)
2374     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2375   return res;
2377 #define INIT_GETHOSTBYADDR_R                  \
2378   COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2379 #else
2380 #define INIT_GETHOSTBYADDR_R
2381 #endif
2383 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2384 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2385             struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2386             __sanitizer_hostent **result, int *h_errnop) {
2387   void *ctx;
2388   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2389                            result, h_errnop);
2390   // FIXME: under ASan the call below may write to freed memory and corrupt
2391   // its metadata. See
2392   // https://github.com/google/sanitizers/issues/321.
2393   int res =
2394       REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2395   if (result) {
2396     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2397     if (res == 0 && *result) write_hostent(ctx, *result);
2398   }
2399   if (h_errnop)
2400     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2401   return res;
2403 #define INIT_GETHOSTBYNAME2_R                  \
2404   COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2405 #else
2406 #define INIT_GETHOSTBYNAME2_R
2407 #endif
2409 #if SANITIZER_INTERCEPT_GETSOCKOPT
2410 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2411             int *optlen) {
2412   void *ctx;
2413   COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2414                            optlen);
2415   if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2416   // FIXME: under ASan the call below may write to freed memory and corrupt
2417   // its metadata. See
2418   // https://github.com/google/sanitizers/issues/321.
2419   int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2420   if (res == 0)
2421     if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2422   return res;
2424 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2425 #else
2426 #define INIT_GETSOCKOPT
2427 #endif
2429 #if SANITIZER_INTERCEPT_ACCEPT
2430 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2431   void *ctx;
2432   COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2433   unsigned addrlen0 = 0;
2434   if (addrlen) {
2435     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2436     addrlen0 = *addrlen;
2437   }
2438   int fd2 = REAL(accept)(fd, addr, addrlen);
2439   if (fd2 >= 0) {
2440     if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2441     if (addr && addrlen)
2442       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2443   }
2444   return fd2;
2446 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2447 #else
2448 #define INIT_ACCEPT
2449 #endif
2451 #if SANITIZER_INTERCEPT_ACCEPT4
2452 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2453   void *ctx;
2454   COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2455   unsigned addrlen0 = 0;
2456   if (addrlen) {
2457     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2458     addrlen0 = *addrlen;
2459   }
2460   // FIXME: under ASan the call below may write to freed memory and corrupt
2461   // its metadata. See
2462   // https://github.com/google/sanitizers/issues/321.
2463   int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2464   if (fd2 >= 0) {
2465     if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2466     if (addr && addrlen)
2467       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2468   }
2469   return fd2;
2471 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2472 #else
2473 #define INIT_ACCEPT4
2474 #endif
2476 #if SANITIZER_INTERCEPT_MODF
2477 INTERCEPTOR(double, modf, double x, double *iptr) {
2478   void *ctx;
2479   COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2480   // FIXME: under ASan the call below may write to freed memory and corrupt
2481   // its metadata. See
2482   // https://github.com/google/sanitizers/issues/321.
2483   double res = REAL(modf)(x, iptr);
2484   if (iptr) {
2485     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2486   }
2487   return res;
2489 INTERCEPTOR(float, modff, float x, float *iptr) {
2490   void *ctx;
2491   COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2492   // FIXME: under ASan the call below may write to freed memory and corrupt
2493   // its metadata. See
2494   // https://github.com/google/sanitizers/issues/321.
2495   float res = REAL(modff)(x, iptr);
2496   if (iptr) {
2497     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2498   }
2499   return res;
2501 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2502   void *ctx;
2503   COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2504   // FIXME: under ASan the call below may write to freed memory and corrupt
2505   // its metadata. See
2506   // https://github.com/google/sanitizers/issues/321.
2507   long double res = REAL(modfl)(x, iptr);
2508   if (iptr) {
2509     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2510   }
2511   return res;
2513 #define INIT_MODF                   \
2514   COMMON_INTERCEPT_FUNCTION(modf);  \
2515   COMMON_INTERCEPT_FUNCTION(modff); \
2516   COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2517 #else
2518 #define INIT_MODF
2519 #endif
2521 #if SANITIZER_INTERCEPT_RECVMSG
2522 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2523                          SSIZE_T maxlen) {
2524   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2525   if (msg->msg_name && msg->msg_namelen)
2526     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2527   if (msg->msg_iov && msg->msg_iovlen)
2528     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2529                                    sizeof(*msg->msg_iov) * msg->msg_iovlen);
2530   write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2531   if (msg->msg_control && msg->msg_controllen)
2532     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2535 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2536             int flags) {
2537   void *ctx;
2538   COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2539   // FIXME: under ASan the call below may write to freed memory and corrupt
2540   // its metadata. See
2541   // https://github.com/google/sanitizers/issues/321.
2542   SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2543   if (res >= 0) {
2544     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2545     if (msg) {
2546       write_msghdr(ctx, msg, res);
2547       COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2548     }
2549   }
2550   return res;
2552 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2553 #else
2554 #define INIT_RECVMSG
2555 #endif
2557 #if SANITIZER_INTERCEPT_SENDMSG
2558 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2559   const unsigned kCmsgDataOffset =
2560       RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2562   char *p = (char *)control;
2563   char *const control_end = p + controllen;
2564   while (true) {
2565     if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2566     __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2567     COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2569     if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2571     COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2572                                   sizeof(cmsg->cmsg_level));
2573     COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2574                                   sizeof(cmsg->cmsg_type));
2576     if (cmsg->cmsg_len > kCmsgDataOffset) {
2577       char *data = p + kCmsgDataOffset;
2578       unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2579       if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2580     }
2582     p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2583   }
2586 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2587                         SSIZE_T maxlen) {
2588 #define R(f) \
2589   COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
2590   R(name);
2591   R(namelen);
2592   R(iov);
2593   R(iovlen);
2594   R(control);
2595   R(controllen);
2596   R(flags);
2597 #undef R
2598   if (msg->msg_name && msg->msg_namelen)
2599     COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2600   if (msg->msg_iov && msg->msg_iovlen)
2601     COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
2602                                   sizeof(*msg->msg_iov) * msg->msg_iovlen);
2603   read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2604   if (msg->msg_control && msg->msg_controllen)
2605     read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
2608 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
2609             int flags) {
2610   void *ctx;
2611   COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
2612   if (fd >= 0) {
2613     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
2614     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
2615   }
2616   SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
2617   if (common_flags()->intercept_send && res >= 0 && msg)
2618     read_msghdr(ctx, msg, res);
2619   return res;
2621 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
2622 #else
2623 #define INIT_SENDMSG
2624 #endif
2626 #if SANITIZER_INTERCEPT_GETPEERNAME
2627 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2628   void *ctx;
2629   COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2630   unsigned addr_sz;
2631   if (addrlen) addr_sz = *addrlen;
2632   // FIXME: under ASan the call below may write to freed memory and corrupt
2633   // its metadata. See
2634   // https://github.com/google/sanitizers/issues/321.
2635   int res = REAL(getpeername)(sockfd, addr, addrlen);
2636   if (!res && addr && addrlen)
2637     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2638   return res;
2640 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2641 #else
2642 #define INIT_GETPEERNAME
2643 #endif
2645 #if SANITIZER_INTERCEPT_SYSINFO
2646 INTERCEPTOR(int, sysinfo, void *info) {
2647   void *ctx;
2648   // FIXME: under ASan the call below may write to freed memory and corrupt
2649   // its metadata. See
2650   // https://github.com/google/sanitizers/issues/321.
2651   COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2652   int res = REAL(sysinfo)(info);
2653   if (!res && info)
2654     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2655   return res;
2657 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2658 #else
2659 #define INIT_SYSINFO
2660 #endif
2662 #if SANITIZER_INTERCEPT_READDIR
2663 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2664   void *ctx;
2665   COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2666   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2667   __sanitizer_dirent *res = REAL(opendir)(path);
2668   if (res)
2669     COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2670   return res;
2673 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2674   void *ctx;
2675   COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
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   __sanitizer_dirent *res = REAL(readdir)(dirp);
2680   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2681   return res;
2684 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2685             __sanitizer_dirent **result) {
2686   void *ctx;
2687   COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2688   // FIXME: under ASan the call below may write to freed memory and corrupt
2689   // its metadata. See
2690   // https://github.com/google/sanitizers/issues/321.
2691   int res = REAL(readdir_r)(dirp, entry, result);
2692   if (!res) {
2693     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2694     if (*result)
2695       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2696   }
2697   return res;
2700 #define INIT_READDIR                  \
2701   COMMON_INTERCEPT_FUNCTION(opendir); \
2702   COMMON_INTERCEPT_FUNCTION(readdir); \
2703   COMMON_INTERCEPT_FUNCTION(readdir_r);
2704 #else
2705 #define INIT_READDIR
2706 #endif
2708 #if SANITIZER_INTERCEPT_READDIR64
2709 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2710   void *ctx;
2711   COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2712   // FIXME: under ASan the call below may write to freed memory and corrupt
2713   // its metadata. See
2714   // https://github.com/google/sanitizers/issues/321.
2715   __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2716   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2717   return res;
2720 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2721             __sanitizer_dirent64 **result) {
2722   void *ctx;
2723   COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2724   // FIXME: under ASan the call below may write to freed memory and corrupt
2725   // its metadata. See
2726   // https://github.com/google/sanitizers/issues/321.
2727   int res = REAL(readdir64_r)(dirp, entry, result);
2728   if (!res) {
2729     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2730     if (*result)
2731       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2732   }
2733   return res;
2735 #define INIT_READDIR64                  \
2736   COMMON_INTERCEPT_FUNCTION(readdir64); \
2737   COMMON_INTERCEPT_FUNCTION(readdir64_r);
2738 #else
2739 #define INIT_READDIR64
2740 #endif
2742 #if SANITIZER_INTERCEPT_PTRACE
2743 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2744   void *ctx;
2745   COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2746   __sanitizer_iovec local_iovec;
2748   if (data) {
2749     if (request == ptrace_setregs)
2750       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2751     else if (request == ptrace_setfpregs)
2752       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2753     else if (request == ptrace_setfpxregs)
2754       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2755     else if (request == ptrace_setvfpregs)
2756       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2757     else if (request == ptrace_setsiginfo)
2758       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2759     // Some kernel might zero the iovec::iov_base in case of invalid
2760     // write access.  In this case copy the invalid address for further
2761     // inspection.
2762     else if (request == ptrace_setregset || request == ptrace_getregset) {
2763       __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2764       COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
2765       local_iovec = *iovec;
2766       if (request == ptrace_setregset)
2767         COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
2768     }
2769   }
2771   // FIXME: under ASan the call below may write to freed memory and corrupt
2772   // its metadata. See
2773   // https://github.com/google/sanitizers/issues/321.
2774   uptr res = REAL(ptrace)(request, pid, addr, data);
2776   if (!res && data) {
2777     // Note that PEEK* requests assign different meaning to the return value.
2778     // This function does not handle them (nor does it need to).
2779     if (request == ptrace_getregs)
2780       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2781     else if (request == ptrace_getfpregs)
2782       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2783     else if (request == ptrace_getfpxregs)
2784       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2785     else if (request == ptrace_getvfpregs)
2786       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2787     else if (request == ptrace_getsiginfo)
2788       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2789     else if (request == ptrace_geteventmsg)
2790       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2791     else if (request == ptrace_getregset) {
2792       __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2793       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
2794       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
2795                                      local_iovec.iov_len);
2796     }
2797   }
2798   return res;
2801 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2802 #else
2803 #define INIT_PTRACE
2804 #endif
2806 #if SANITIZER_INTERCEPT_SETLOCALE
2807 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2808   void *ctx;
2809   COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2810   if (locale)
2811     COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2812   char *res = REAL(setlocale)(category, locale);
2813   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2814   return res;
2817 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2818 #else
2819 #define INIT_SETLOCALE
2820 #endif
2822 #if SANITIZER_INTERCEPT_GETCWD
2823 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2824   void *ctx;
2825   COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
2826   // FIXME: under ASan the call below may write to freed memory and corrupt
2827   // its metadata. See
2828   // https://github.com/google/sanitizers/issues/321.
2829   char *res = REAL(getcwd)(buf, size);
2830   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2831   return res;
2833 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2834 #else
2835 #define INIT_GETCWD
2836 #endif
2838 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2839 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2840   void *ctx;
2841   COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2842   // FIXME: under ASan the call below may write to freed memory and corrupt
2843   // its metadata. See
2844   // https://github.com/google/sanitizers/issues/321.
2845   char *res = REAL(get_current_dir_name)(fake);
2846   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2847   return res;
2850 #define INIT_GET_CURRENT_DIR_NAME \
2851   COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2852 #else
2853 #define INIT_GET_CURRENT_DIR_NAME
2854 #endif
2856 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
2857   CHECK(endptr);
2858   if (nptr == *endptr) {
2859     // No digits were found at strtol call, we need to find out the last
2860     // symbol accessed by strtoll on our own.
2861     // We get this symbol by skipping leading blanks and optional +/- sign.
2862     while (IsSpace(*nptr)) nptr++;
2863     if (*nptr == '+' || *nptr == '-') nptr++;
2864     *endptr = const_cast<char *>(nptr);
2865   }
2866   CHECK(*endptr >= nptr);
2869 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
2870                              char **endptr, char *real_endptr, int base) {
2871   if (endptr) {
2872     *endptr = real_endptr;
2873     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2874   }
2875   // If base has unsupported value, strtol can exit with EINVAL
2876   // without reading any characters. So do additional checks only
2877   // if base is valid.
2878   bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
2879   if (is_valid_base) {
2880     FixRealStrtolEndptr(nptr, &real_endptr);
2881   }
2882   COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
2883                                  (real_endptr - nptr) + 1 : 0);
2887 #if SANITIZER_INTERCEPT_STRTOIMAX
2888 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
2889   void *ctx;
2890   COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
2891   // FIXME: under ASan the call below may write to freed memory and corrupt
2892   // its metadata. See
2893   // https://github.com/google/sanitizers/issues/321.
2894   char *real_endptr;
2895   INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
2896   StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2897   return res;
2900 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
2901   void *ctx;
2902   COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
2903   // FIXME: under ASan the call below may write to freed memory and corrupt
2904   // its metadata. See
2905   // https://github.com/google/sanitizers/issues/321.
2906   char *real_endptr;
2907   INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
2908   StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2909   return res;
2912 #define INIT_STRTOIMAX                  \
2913   COMMON_INTERCEPT_FUNCTION(strtoimax); \
2914   COMMON_INTERCEPT_FUNCTION(strtoumax);
2915 #else
2916 #define INIT_STRTOIMAX
2917 #endif
2919 #if SANITIZER_INTERCEPT_MBSTOWCS
2920 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
2921   void *ctx;
2922   COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
2923   // FIXME: under ASan the call below may write to freed memory and corrupt
2924   // its metadata. See
2925   // https://github.com/google/sanitizers/issues/321.
2926   SIZE_T res = REAL(mbstowcs)(dest, src, len);
2927   if (res != (SIZE_T) - 1 && dest) {
2928     SIZE_T write_cnt = res + (res < len);
2929     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2930   }
2931   return res;
2934 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
2935             void *ps) {
2936   void *ctx;
2937   COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
2938   if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2939   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2940   // FIXME: under ASan the call below may write to freed memory and corrupt
2941   // its metadata. See
2942   // https://github.com/google/sanitizers/issues/321.
2943   SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
2944   if (res != (SIZE_T)(-1) && dest && src) {
2945     // This function, and several others, may or may not write the terminating
2946     // \0 character. They write it iff they clear *src.
2947     SIZE_T write_cnt = res + !*src;
2948     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2949   }
2950   return res;
2953 #define INIT_MBSTOWCS                  \
2954   COMMON_INTERCEPT_FUNCTION(mbstowcs); \
2955   COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
2956 #else
2957 #define INIT_MBSTOWCS
2958 #endif
2960 #if SANITIZER_INTERCEPT_MBSNRTOWCS
2961 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
2962             SIZE_T len, void *ps) {
2963   void *ctx;
2964   COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
2965   if (src) {
2966     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2967     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2968   }
2969   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2970   // FIXME: under ASan the call below may write to freed memory and corrupt
2971   // its metadata. See
2972   // https://github.com/google/sanitizers/issues/321.
2973   SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
2974   if (res != (SIZE_T)(-1) && dest && src) {
2975     SIZE_T write_cnt = res + !*src;
2976     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2977   }
2978   return res;
2981 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
2982 #else
2983 #define INIT_MBSNRTOWCS
2984 #endif
2986 #if SANITIZER_INTERCEPT_WCSTOMBS
2987 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
2988   void *ctx;
2989   COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
2990   // FIXME: under ASan the call below may write to freed memory and corrupt
2991   // its metadata. See
2992   // https://github.com/google/sanitizers/issues/321.
2993   SIZE_T res = REAL(wcstombs)(dest, src, len);
2994   if (res != (SIZE_T) - 1 && dest) {
2995     SIZE_T write_cnt = res + (res < len);
2996     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2997   }
2998   return res;
3001 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3002             void *ps) {
3003   void *ctx;
3004   COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3005   if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3006   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3007   // FIXME: under ASan the call below may write to freed memory and corrupt
3008   // its metadata. See
3009   // https://github.com/google/sanitizers/issues/321.
3010   SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3011   if (res != (SIZE_T) - 1 && dest && src) {
3012     SIZE_T write_cnt = res + !*src;
3013     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3014   }
3015   return res;
3018 #define INIT_WCSTOMBS                  \
3019   COMMON_INTERCEPT_FUNCTION(wcstombs); \
3020   COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3021 #else
3022 #define INIT_WCSTOMBS
3023 #endif
3025 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3026 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3027             SIZE_T len, void *ps) {
3028   void *ctx;
3029   COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3030   if (src) {
3031     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3032     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3033   }
3034   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3035   // FIXME: under ASan the call below may write to freed memory and corrupt
3036   // its metadata. See
3037   // https://github.com/google/sanitizers/issues/321.
3038   SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3039   if (res != ((SIZE_T)-1) && dest && src) {
3040     SIZE_T write_cnt = res + !*src;
3041     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3042   }
3043   return res;
3046 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3047 #else
3048 #define INIT_WCSNRTOMBS
3049 #endif
3052 #if SANITIZER_INTERCEPT_WCRTOMB
3053 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3054   void *ctx;
3055   COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3056   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3057   // FIXME: under ASan the call below may write to freed memory and corrupt
3058   // its metadata. See
3059   // https://github.com/google/sanitizers/issues/321.
3060   SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3061   if (res != ((SIZE_T)-1) && dest) {
3062     SIZE_T write_cnt = res;
3063     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3064   }
3065   return res;
3068 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3069 #else
3070 #define INIT_WCRTOMB
3071 #endif
3073 #if SANITIZER_INTERCEPT_TCGETATTR
3074 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3075   void *ctx;
3076   COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3077   // FIXME: under ASan the call below may write to freed memory and corrupt
3078   // its metadata. See
3079   // https://github.com/google/sanitizers/issues/321.
3080   int res = REAL(tcgetattr)(fd, termios_p);
3081   if (!res && termios_p)
3082     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3083   return res;
3086 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3087 #else
3088 #define INIT_TCGETATTR
3089 #endif
3091 #if SANITIZER_INTERCEPT_REALPATH
3092 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3093   void *ctx;
3094   COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3095   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3097   // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3098   // version of a versioned symbol. For realpath(), this gives us something
3099   // (called __old_realpath) that does not handle NULL in the second argument.
3100   // Handle it as part of the interceptor.
3101   char *allocated_path = nullptr;
3102   if (!resolved_path)
3103     allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3105   char *res = REAL(realpath)(path, resolved_path);
3106   if (allocated_path && !res) WRAP(free)(allocated_path);
3107   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3108   return res;
3110 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3111 #else
3112 #define INIT_REALPATH
3113 #endif
3115 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3116 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3117   void *ctx;
3118   COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3119   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3120   char *res = REAL(canonicalize_file_name)(path);
3121   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3122   return res;
3124 #define INIT_CANONICALIZE_FILE_NAME \
3125   COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3126 #else
3127 #define INIT_CANONICALIZE_FILE_NAME
3128 #endif
3130 #if SANITIZER_INTERCEPT_CONFSTR
3131 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3132   void *ctx;
3133   COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3134   // FIXME: under ASan the call below may write to freed memory and corrupt
3135   // its metadata. See
3136   // https://github.com/google/sanitizers/issues/321.
3137   SIZE_T res = REAL(confstr)(name, buf, len);
3138   if (buf && res)
3139     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3140   return res;
3142 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3143 #else
3144 #define INIT_CONFSTR
3145 #endif
3147 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3148 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3149   void *ctx;
3150   COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3151   // FIXME: under ASan the call below may write to freed memory and corrupt
3152   // its metadata. See
3153   // https://github.com/google/sanitizers/issues/321.
3154   int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3155   if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3156   return res;
3158 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3159 #else
3160 #define INIT_SCHED_GETAFFINITY
3161 #endif
3163 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3164 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3165   void *ctx;
3166   COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3167   int res = REAL(sched_getparam)(pid, param);
3168   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3169   return res;
3171 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3172 #else
3173 #define INIT_SCHED_GETPARAM
3174 #endif
3176 #if SANITIZER_INTERCEPT_STRERROR
3177 INTERCEPTOR(char *, strerror, int errnum) {
3178   void *ctx;
3179   COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3180   char *res = REAL(strerror)(errnum);
3181   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3182   return res;
3184 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3185 #else
3186 #define INIT_STRERROR
3187 #endif
3189 #if SANITIZER_INTERCEPT_STRERROR_R
3190 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3191   void *ctx;
3192   COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3193   // FIXME: under ASan the call below may write to freed memory and corrupt
3194   // its metadata. See
3195   // https://github.com/google/sanitizers/issues/321.
3196   char *res = REAL(strerror_r)(errnum, buf, buflen);
3197   // There are 2 versions of strerror_r:
3198   //  * POSIX version returns 0 on success, negative error code on failure,
3199   //    writes message to buf.
3200   //  * GNU version returns message pointer, which points to either buf or some
3201   //    static storage.
3202   SIZE_T posix_res = (SIZE_T)res;
3203   if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
3204     // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3205     // At least on OSX, buf contents are valid even when the call fails.
3206     SIZE_T sz = internal_strnlen(buf, buflen);
3207     if (sz < buflen) ++sz;
3208     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3209   } else {
3210     // GNU version.
3211     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3212   }
3213   return res;
3215 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3216 #else
3217 #define INIT_STRERROR_R
3218 #endif
3220 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3221 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3222   void *ctx;
3223   COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3224   // FIXME: under ASan the call below may write to freed memory and corrupt
3225   // its metadata. See
3226   // https://github.com/google/sanitizers/issues/321.
3227   int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3228   // This version always returns a null-terminated string.
3229   if (buf && buflen)
3230     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3231   return res;
3233 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3234 #else
3235 #define INIT_XPG_STRERROR_R
3236 #endif
3238 #if SANITIZER_INTERCEPT_SCANDIR
3239 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3240 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3241                                 const struct __sanitizer_dirent **);
3243 static THREADLOCAL scandir_filter_f scandir_filter;
3244 static THREADLOCAL scandir_compar_f scandir_compar;
3246 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3247   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3248   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3249   return scandir_filter(dir);
3252 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3253                                   const struct __sanitizer_dirent **b) {
3254   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3255   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3256   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3257   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3258   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3259   return scandir_compar(a, b);
3262 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3263             scandir_filter_f filter, scandir_compar_f compar) {
3264   void *ctx;
3265   COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3266   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3267   scandir_filter = filter;
3268   scandir_compar = compar;
3269   // FIXME: under ASan the call below may write to freed memory and corrupt
3270   // its metadata. See
3271   // https://github.com/google/sanitizers/issues/321.
3272   int res = REAL(scandir)(dirp, namelist,
3273                           filter ? wrapped_scandir_filter : nullptr,
3274                           compar ? wrapped_scandir_compar : nullptr);
3275   scandir_filter = nullptr;
3276   scandir_compar = nullptr;
3277   if (namelist && res > 0) {
3278     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3279     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3280     for (int i = 0; i < res; ++i)
3281       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3282                                      (*namelist)[i]->d_reclen);
3283   }
3284   return res;
3286 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3287 #else
3288 #define INIT_SCANDIR
3289 #endif
3291 #if SANITIZER_INTERCEPT_SCANDIR64
3292 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3293 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3294                                   const struct __sanitizer_dirent64 **);
3296 static THREADLOCAL scandir64_filter_f scandir64_filter;
3297 static THREADLOCAL scandir64_compar_f scandir64_compar;
3299 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3300   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3301   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3302   return scandir64_filter(dir);
3305 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3306                                     const struct __sanitizer_dirent64 **b) {
3307   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3308   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3309   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3310   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3311   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3312   return scandir64_compar(a, b);
3315 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3316             scandir64_filter_f filter, scandir64_compar_f compar) {
3317   void *ctx;
3318   COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3319   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3320   scandir64_filter = filter;
3321   scandir64_compar = compar;
3322   // FIXME: under ASan the call below may write to freed memory and corrupt
3323   // its metadata. See
3324   // https://github.com/google/sanitizers/issues/321.
3325   int res =
3326       REAL(scandir64)(dirp, namelist,
3327                       filter ? wrapped_scandir64_filter : nullptr,
3328                       compar ? wrapped_scandir64_compar : nullptr);
3329   scandir64_filter = nullptr;
3330   scandir64_compar = nullptr;
3331   if (namelist && res > 0) {
3332     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3333     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3334     for (int i = 0; i < res; ++i)
3335       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3336                                      (*namelist)[i]->d_reclen);
3337   }
3338   return res;
3340 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3341 #else
3342 #define INIT_SCANDIR64
3343 #endif
3345 #if SANITIZER_INTERCEPT_GETGROUPS
3346 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3347   void *ctx;
3348   COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3349   // FIXME: under ASan the call below may write to freed memory and corrupt
3350   // its metadata. See
3351   // https://github.com/google/sanitizers/issues/321.
3352   int res = REAL(getgroups)(size, lst);
3353   if (res >= 0 && lst && size > 0)
3354     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3355   return res;
3357 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3358 #else
3359 #define INIT_GETGROUPS
3360 #endif
3362 #if SANITIZER_INTERCEPT_POLL
3363 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3364                         __sanitizer_nfds_t nfds) {
3365   for (unsigned i = 0; i < nfds; ++i) {
3366     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3367     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3368   }
3371 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3372                          __sanitizer_nfds_t nfds) {
3373   for (unsigned i = 0; i < nfds; ++i)
3374     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3375                                    sizeof(fds[i].revents));
3378 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3379             int timeout) {
3380   void *ctx;
3381   COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3382   if (fds && nfds) read_pollfd(ctx, fds, nfds);
3383   int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3384   if (fds && nfds) write_pollfd(ctx, fds, nfds);
3385   return res;
3387 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3388 #else
3389 #define INIT_POLL
3390 #endif
3392 #if SANITIZER_INTERCEPT_PPOLL
3393 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3394             void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3395   void *ctx;
3396   COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3397   if (fds && nfds) read_pollfd(ctx, fds, nfds);
3398   if (timeout_ts)
3399     COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3400   // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3401   int res =
3402       COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3403   if (fds && nfds) write_pollfd(ctx, fds, nfds);
3404   return res;
3406 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3407 #else
3408 #define INIT_PPOLL
3409 #endif
3411 #if SANITIZER_INTERCEPT_WORDEXP
3412 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3413   void *ctx;
3414   COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3415   if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3416   // FIXME: under ASan the call below may write to freed memory and corrupt
3417   // its metadata. See
3418   // https://github.com/google/sanitizers/issues/321.
3419   int res = REAL(wordexp)(s, p, flags);
3420   if (!res && p) {
3421     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3422     if (p->we_wordc)
3423       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3424                                      sizeof(*p->we_wordv) * p->we_wordc);
3425     for (uptr i = 0; i < p->we_wordc; ++i) {
3426       char *w = p->we_wordv[i];
3427       if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3428     }
3429   }
3430   return res;
3432 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3433 #else
3434 #define INIT_WORDEXP
3435 #endif
3437 #if SANITIZER_INTERCEPT_SIGWAIT
3438 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3439   void *ctx;
3440   COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3441   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3442   // FIXME: under ASan the call below may write to freed memory and corrupt
3443   // its metadata. See
3444   // https://github.com/google/sanitizers/issues/321.
3445   int res = REAL(sigwait)(set, sig);
3446   if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3447   return res;
3449 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3450 #else
3451 #define INIT_SIGWAIT
3452 #endif
3454 #if SANITIZER_INTERCEPT_SIGWAITINFO
3455 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3456   void *ctx;
3457   COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3458   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3459   // FIXME: under ASan the call below may write to freed memory and corrupt
3460   // its metadata. See
3461   // https://github.com/google/sanitizers/issues/321.
3462   int res = REAL(sigwaitinfo)(set, info);
3463   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3464   return res;
3466 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3467 #else
3468 #define INIT_SIGWAITINFO
3469 #endif
3471 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3472 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3473             void *timeout) {
3474   void *ctx;
3475   COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3476   if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3477   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3478   // FIXME: under ASan the call below may write to freed memory and corrupt
3479   // its metadata. See
3480   // https://github.com/google/sanitizers/issues/321.
3481   int res = REAL(sigtimedwait)(set, info, timeout);
3482   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3483   return res;
3485 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3486 #else
3487 #define INIT_SIGTIMEDWAIT
3488 #endif
3490 #if SANITIZER_INTERCEPT_SIGSETOPS
3491 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3492   void *ctx;
3493   COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3494   // FIXME: under ASan the call below may write to freed memory and corrupt
3495   // its metadata. See
3496   // https://github.com/google/sanitizers/issues/321.
3497   int res = REAL(sigemptyset)(set);
3498   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3499   return res;
3502 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3503   void *ctx;
3504   COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
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   int res = REAL(sigfillset)(set);
3509   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3510   return res;
3512 #define INIT_SIGSETOPS                    \
3513   COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3514   COMMON_INTERCEPT_FUNCTION(sigfillset);
3515 #else
3516 #define INIT_SIGSETOPS
3517 #endif
3519 #if SANITIZER_INTERCEPT_SIGPENDING
3520 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3521   void *ctx;
3522   COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3523   // FIXME: under ASan the call below may write to freed memory and corrupt
3524   // its metadata. See
3525   // https://github.com/google/sanitizers/issues/321.
3526   int res = REAL(sigpending)(set);
3527   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3528   return res;
3530 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3531 #else
3532 #define INIT_SIGPENDING
3533 #endif
3535 #if SANITIZER_INTERCEPT_SIGPROCMASK
3536 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3537             __sanitizer_sigset_t *oldset) {
3538   void *ctx;
3539   COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3540   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3541   // FIXME: under ASan the call below may write to freed memory and corrupt
3542   // its metadata. See
3543   // https://github.com/google/sanitizers/issues/321.
3544   int res = REAL(sigprocmask)(how, set, oldset);
3545   if (!res && oldset)
3546     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3547   return res;
3549 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3550 #else
3551 #define INIT_SIGPROCMASK
3552 #endif
3554 #if SANITIZER_INTERCEPT_BACKTRACE
3555 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3556   void *ctx;
3557   COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3558   // FIXME: under ASan the call below may write to freed memory and corrupt
3559   // its metadata. See
3560   // https://github.com/google/sanitizers/issues/321.
3561   int res = REAL(backtrace)(buffer, size);
3562   if (res && buffer)
3563     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3564   return res;
3567 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3568   void *ctx;
3569   COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3570   if (buffer && size)
3571     COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3572   // FIXME: under ASan the call below may write to freed memory and corrupt
3573   // its metadata. See
3574   // https://github.com/google/sanitizers/issues/321.
3575   char **res = REAL(backtrace_symbols)(buffer, size);
3576   if (res && size) {
3577     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3578     for (int i = 0; i < size; ++i)
3579       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3580   }
3581   return res;
3583 #define INIT_BACKTRACE                  \
3584   COMMON_INTERCEPT_FUNCTION(backtrace); \
3585   COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3586 #else
3587 #define INIT_BACKTRACE
3588 #endif
3590 #if SANITIZER_INTERCEPT__EXIT
3591 INTERCEPTOR(void, _exit, int status) {
3592   void *ctx;
3593   COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3594   COMMON_INTERCEPTOR_USER_CALLBACK_START();
3595   int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3596   COMMON_INTERCEPTOR_USER_CALLBACK_END();
3597   if (status == 0) status = status1;
3598   REAL(_exit)(status);
3600 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3601 #else
3602 #define INIT__EXIT
3603 #endif
3605 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3606 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3607   void *ctx;
3608   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3609   int res = REAL(pthread_mutex_lock)(m);
3610   if (res == errno_EOWNERDEAD)
3611     COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3612   if (res == 0 || res == errno_EOWNERDEAD)
3613     COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
3614   if (res == errno_EINVAL)
3615     COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3616   return res;
3619 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3620   void *ctx;
3621   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3622   COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3623   int res = REAL(pthread_mutex_unlock)(m);
3624   if (res == errno_EINVAL)
3625     COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3626   return res;
3629 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3630 #define INIT_PTHREAD_MUTEX_UNLOCK \
3631   COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3632 #else
3633 #define INIT_PTHREAD_MUTEX_LOCK
3634 #define INIT_PTHREAD_MUTEX_UNLOCK
3635 #endif
3637 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3638 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3639   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3640   if (mnt->mnt_fsname)
3641     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3642                                    REAL(strlen)(mnt->mnt_fsname) + 1);
3643   if (mnt->mnt_dir)
3644     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3645                                    REAL(strlen)(mnt->mnt_dir) + 1);
3646   if (mnt->mnt_type)
3647     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3648                                    REAL(strlen)(mnt->mnt_type) + 1);
3649   if (mnt->mnt_opts)
3650     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3651                                    REAL(strlen)(mnt->mnt_opts) + 1);
3653 #endif
3655 #if SANITIZER_INTERCEPT_GETMNTENT
3656 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3657   void *ctx;
3658   COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3659   __sanitizer_mntent *res = REAL(getmntent)(fp);
3660   if (res) write_mntent(ctx, res);
3661   return res;
3663 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3664 #else
3665 #define INIT_GETMNTENT
3666 #endif
3668 #if SANITIZER_INTERCEPT_GETMNTENT_R
3669 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3670             __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3671   void *ctx;
3672   COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3673   __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3674   if (res) write_mntent(ctx, res);
3675   return res;
3677 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3678 #else
3679 #define INIT_GETMNTENT_R
3680 #endif
3682 #if SANITIZER_INTERCEPT_STATFS
3683 INTERCEPTOR(int, statfs, char *path, void *buf) {
3684   void *ctx;
3685   COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3686   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
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(statfs)(path, buf);
3691   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3692   return res;
3694 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3695   void *ctx;
3696   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3697   // FIXME: under ASan the call below may write to freed memory and corrupt
3698   // its metadata. See
3699   // https://github.com/google/sanitizers/issues/321.
3700   int res = REAL(fstatfs)(fd, buf);
3701   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3702   return res;
3704 #define INIT_STATFS                  \
3705   COMMON_INTERCEPT_FUNCTION(statfs); \
3706   COMMON_INTERCEPT_FUNCTION(fstatfs);
3707 #else
3708 #define INIT_STATFS
3709 #endif
3711 #if SANITIZER_INTERCEPT_STATFS64
3712 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3713   void *ctx;
3714   COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3715   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3716   // FIXME: under ASan the call below may write to freed memory and corrupt
3717   // its metadata. See
3718   // https://github.com/google/sanitizers/issues/321.
3719   int res = REAL(statfs64)(path, buf);
3720   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3721   return res;
3723 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3724   void *ctx;
3725   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3726   // FIXME: under ASan the call below may write to freed memory and corrupt
3727   // its metadata. See
3728   // https://github.com/google/sanitizers/issues/321.
3729   int res = REAL(fstatfs64)(fd, buf);
3730   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3731   return res;
3733 #define INIT_STATFS64                  \
3734   COMMON_INTERCEPT_FUNCTION(statfs64); \
3735   COMMON_INTERCEPT_FUNCTION(fstatfs64);
3736 #else
3737 #define INIT_STATFS64
3738 #endif
3740 #if SANITIZER_INTERCEPT_STATVFS
3741 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3742   void *ctx;
3743   COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3744   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3745   // FIXME: under ASan the call below may write to freed memory and corrupt
3746   // its metadata. See
3747   // https://github.com/google/sanitizers/issues/321.
3748   int res = REAL(statvfs)(path, buf);
3749   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3750   return res;
3752 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3753   void *ctx;
3754   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3755   // FIXME: under ASan the call below may write to freed memory and corrupt
3756   // its metadata. See
3757   // https://github.com/google/sanitizers/issues/321.
3758   int res = REAL(fstatvfs)(fd, buf);
3759   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3760   return res;
3762 #define INIT_STATVFS                  \
3763   COMMON_INTERCEPT_FUNCTION(statvfs); \
3764   COMMON_INTERCEPT_FUNCTION(fstatvfs);
3765 #else
3766 #define INIT_STATVFS
3767 #endif
3769 #if SANITIZER_INTERCEPT_STATVFS64
3770 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3771   void *ctx;
3772   COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3773   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3774   // FIXME: under ASan the call below may write to freed memory and corrupt
3775   // its metadata. See
3776   // https://github.com/google/sanitizers/issues/321.
3777   int res = REAL(statvfs64)(path, buf);
3778   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3779   return res;
3781 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3782   void *ctx;
3783   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3784   // FIXME: under ASan the call below may write to freed memory and corrupt
3785   // its metadata. See
3786   // https://github.com/google/sanitizers/issues/321.
3787   int res = REAL(fstatvfs64)(fd, buf);
3788   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3789   return res;
3791 #define INIT_STATVFS64                  \
3792   COMMON_INTERCEPT_FUNCTION(statvfs64); \
3793   COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3794 #else
3795 #define INIT_STATVFS64
3796 #endif
3798 #if SANITIZER_INTERCEPT_INITGROUPS
3799 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3800   void *ctx;
3801   COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3802   if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3803   int res = REAL(initgroups)(user, group);
3804   return res;
3806 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3807 #else
3808 #define INIT_INITGROUPS
3809 #endif
3811 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3812 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3813   void *ctx;
3814   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3815   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3816   char *res = REAL(ether_ntoa)(addr);
3817   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3818   return res;
3820 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3821   void *ctx;
3822   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3823   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3824   __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3825   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3826   return res;
3828 #define INIT_ETHER_NTOA_ATON             \
3829   COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3830   COMMON_INTERCEPT_FUNCTION(ether_aton);
3831 #else
3832 #define INIT_ETHER_NTOA_ATON
3833 #endif
3835 #if SANITIZER_INTERCEPT_ETHER_HOST
3836 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3837   void *ctx;
3838   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3839   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3840   // FIXME: under ASan the call below may write to freed memory and corrupt
3841   // its metadata. See
3842   // https://github.com/google/sanitizers/issues/321.
3843   int res = REAL(ether_ntohost)(hostname, addr);
3844   if (!res && hostname)
3845     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3846   return res;
3848 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3849   void *ctx;
3850   COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3851   if (hostname)
3852     COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3853   // FIXME: under ASan the call below may write to freed memory and corrupt
3854   // its metadata. See
3855   // https://github.com/google/sanitizers/issues/321.
3856   int res = REAL(ether_hostton)(hostname, addr);
3857   if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3858   return res;
3860 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3861             char *hostname) {
3862   void *ctx;
3863   COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3864   if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3865   // FIXME: under ASan the call below may write to freed memory and corrupt
3866   // its metadata. See
3867   // https://github.com/google/sanitizers/issues/321.
3868   int res = REAL(ether_line)(line, addr, hostname);
3869   if (!res) {
3870     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3871     if (hostname)
3872       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3873   }
3874   return res;
3876 #define INIT_ETHER_HOST                     \
3877   COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3878   COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3879   COMMON_INTERCEPT_FUNCTION(ether_line);
3880 #else
3881 #define INIT_ETHER_HOST
3882 #endif
3884 #if SANITIZER_INTERCEPT_ETHER_R
3885 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3886   void *ctx;
3887   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3888   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3889   // FIXME: under ASan the call below may write to freed memory and corrupt
3890   // its metadata. See
3891   // https://github.com/google/sanitizers/issues/321.
3892   char *res = REAL(ether_ntoa_r)(addr, buf);
3893   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3894   return res;
3896 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3897             __sanitizer_ether_addr *addr) {
3898   void *ctx;
3899   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3900   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3901   // FIXME: under ASan the call below may write to freed memory and corrupt
3902   // its metadata. See
3903   // https://github.com/google/sanitizers/issues/321.
3904   __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3905   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3906   return res;
3908 #define INIT_ETHER_R                       \
3909   COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3910   COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3911 #else
3912 #define INIT_ETHER_R
3913 #endif
3915 #if SANITIZER_INTERCEPT_SHMCTL
3916 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3917   void *ctx;
3918   COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3919   // FIXME: under ASan the call below may write to freed memory and corrupt
3920   // its metadata. See
3921   // https://github.com/google/sanitizers/issues/321.
3922   int res = REAL(shmctl)(shmid, cmd, buf);
3923   if (res >= 0) {
3924     unsigned sz = 0;
3925     if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3926       sz = sizeof(__sanitizer_shmid_ds);
3927     else if (cmd == shmctl_ipc_info)
3928       sz = struct_shminfo_sz;
3929     else if (cmd == shmctl_shm_info)
3930       sz = struct_shm_info_sz;
3931     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3932   }
3933   return res;
3935 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3936 #else
3937 #define INIT_SHMCTL
3938 #endif
3940 #if SANITIZER_INTERCEPT_RANDOM_R
3941 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
3942   void *ctx;
3943   COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
3944   // FIXME: under ASan the call below may write to freed memory and corrupt
3945   // its metadata. See
3946   // https://github.com/google/sanitizers/issues/321.
3947   int res = REAL(random_r)(buf, result);
3948   if (!res && result)
3949     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3950   return res;
3952 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3953 #else
3954 #define INIT_RANDOM_R
3955 #endif
3957 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
3958 // its metadata. See
3959 // https://github.com/google/sanitizers/issues/321.
3960 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET ||              \
3961     SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
3962     SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET ||         \
3963     SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET ||        \
3964     SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET ||          \
3965     SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
3966 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz)            \
3967   INTERCEPTOR(int, fn, void *attr, void *r) {                  \
3968     void *ctx;                                                 \
3969     COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r);                \
3970     int res = REAL(fn)(attr, r);                               \
3971     if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
3972     return res;                                                \
3973   }
3974 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
3975   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
3976 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
3977   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
3978 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
3979   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
3980 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
3981   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
3982 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
3983   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
3984 #endif
3986 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
3987 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
3988 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
3989 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
3990 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
3991 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
3992 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
3993 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
3994   void *ctx;
3995   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
3996   // FIXME: under ASan the call below may write to freed memory and corrupt
3997   // its metadata. See
3998   // https://github.com/google/sanitizers/issues/321.
3999   int res = REAL(pthread_attr_getstack)(attr, addr, size);
4000   if (!res) {
4001     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4002     if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4003   }
4004   return res;
4007 // We may need to call the real pthread_attr_getstack from the run-time
4008 // in sanitizer_common, but we don't want to include the interception headers
4009 // there. So, just define this function here.
4010 namespace __sanitizer {
4011 extern "C" {
4012 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4013   return REAL(pthread_attr_getstack)(attr, addr, size);
4015 }  // extern "C"
4016 }  // namespace __sanitizer
4018 #define INIT_PTHREAD_ATTR_GET                             \
4019   COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4020   COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize);   \
4021   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam);  \
4022   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4023   COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope);       \
4024   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize);   \
4025   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4026 #else
4027 #define INIT_PTHREAD_ATTR_GET
4028 #endif
4030 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4031 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4033 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4034   COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4035 #else
4036 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4037 #endif
4039 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4040 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4041             void *cpuset) {
4042   void *ctx;
4043   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4044                            cpuset);
4045   // FIXME: under ASan the call below may write to freed memory and corrupt
4046   // its metadata. See
4047   // https://github.com/google/sanitizers/issues/321.
4048   int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4049   if (!res && cpusetsize && cpuset)
4050     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4051   return res;
4054 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4055   COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4056 #else
4057 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4058 #endif
4060 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4061 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4062 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4063   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4064 #else
4065 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4066 #endif
4068 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4069 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4070 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4071   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4072 #else
4073 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4074 #endif
4076 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4077 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4078 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4079   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4080 #else
4081 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4082 #endif
4084 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4085 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4086 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4087   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4088 #else
4089 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4090 #endif
4092 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4093 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4094 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4095   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4096 #else
4097 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4098 #endif
4100 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4101 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4102 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4103   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4104 #else
4105 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4106 #endif
4108 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4109 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4110 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4111   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4112 #else
4113 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4114 #endif
4116 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4117 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4118 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4119   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4120 #else
4121 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4122 #endif
4124 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4125 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4126 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4127   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4128 #else
4129 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4130 #endif
4132 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4133 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4134 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4135   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4136 #else
4137 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4138 #endif
4140 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4141 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4142 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4143   COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4144 #else
4145 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4146 #endif
4148 #if SANITIZER_INTERCEPT_TMPNAM
4149 INTERCEPTOR(char *, tmpnam, char *s) {
4150   void *ctx;
4151   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4152   char *res = REAL(tmpnam)(s);
4153   if (res) {
4154     if (s)
4155       // FIXME: under ASan the call below may write to freed memory and corrupt
4156       // its metadata. See
4157       // https://github.com/google/sanitizers/issues/321.
4158       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4159     else
4160       COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4161   }
4162   return res;
4164 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4165 #else
4166 #define INIT_TMPNAM
4167 #endif
4169 #if SANITIZER_INTERCEPT_TMPNAM_R
4170 INTERCEPTOR(char *, tmpnam_r, char *s) {
4171   void *ctx;
4172   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4173   // FIXME: under ASan the call below may write to freed memory and corrupt
4174   // its metadata. See
4175   // https://github.com/google/sanitizers/issues/321.
4176   char *res = REAL(tmpnam_r)(s);
4177   if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4178   return res;
4180 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4181 #else
4182 #define INIT_TMPNAM_R
4183 #endif
4185 #if SANITIZER_INTERCEPT_TTYNAME_R
4186 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4187   void *ctx;
4188   COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4189   int res = REAL(ttyname_r)(fd, name, namesize);
4190   if (res == 0)
4191     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4192   return res;
4194 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4195 #else
4196 #define INIT_TTYNAME_R
4197 #endif
4199 #if SANITIZER_INTERCEPT_TEMPNAM
4200 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4201   void *ctx;
4202   COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4203   if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4204   if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4205   char *res = REAL(tempnam)(dir, pfx);
4206   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4207   return res;
4209 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4210 #else
4211 #define INIT_TEMPNAM
4212 #endif
4214 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4215 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4216   void *ctx;
4217   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4218   COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4219   COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4220   return REAL(pthread_setname_np)(thread, name);
4222 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4223 #else
4224 #define INIT_PTHREAD_SETNAME_NP
4225 #endif
4227 #if SANITIZER_INTERCEPT_SINCOS
4228 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4229   void *ctx;
4230   COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4231   // FIXME: under ASan the call below may write to freed memory and corrupt
4232   // its metadata. See
4233   // https://github.com/google/sanitizers/issues/321.
4234   REAL(sincos)(x, sin, cos);
4235   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4236   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4238 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4239   void *ctx;
4240   COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4241   // FIXME: under ASan the call below may write to freed memory and corrupt
4242   // its metadata. See
4243   // https://github.com/google/sanitizers/issues/321.
4244   REAL(sincosf)(x, sin, cos);
4245   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4246   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4248 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4249   void *ctx;
4250   COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4251   // FIXME: under ASan the call below may write to freed memory and corrupt
4252   // its metadata. See
4253   // https://github.com/google/sanitizers/issues/321.
4254   REAL(sincosl)(x, sin, cos);
4255   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4256   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4258 #define INIT_SINCOS                   \
4259   COMMON_INTERCEPT_FUNCTION(sincos);  \
4260   COMMON_INTERCEPT_FUNCTION(sincosf); \
4261   COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4262 #else
4263 #define INIT_SINCOS
4264 #endif
4266 #if SANITIZER_INTERCEPT_REMQUO
4267 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4268   void *ctx;
4269   COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4270   // FIXME: under ASan the call below may write to freed memory and corrupt
4271   // its metadata. See
4272   // https://github.com/google/sanitizers/issues/321.
4273   double res = REAL(remquo)(x, y, quo);
4274   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4275   return res;
4277 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4278   void *ctx;
4279   COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4280   // FIXME: under ASan the call below may write to freed memory and corrupt
4281   // its metadata. See
4282   // https://github.com/google/sanitizers/issues/321.
4283   float res = REAL(remquof)(x, y, quo);
4284   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4285   return res;
4287 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4288   void *ctx;
4289   COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4290   // FIXME: under ASan the call below may write to freed memory and corrupt
4291   // its metadata. See
4292   // https://github.com/google/sanitizers/issues/321.
4293   long double res = REAL(remquol)(x, y, quo);
4294   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4295   return res;
4297 #define INIT_REMQUO                   \
4298   COMMON_INTERCEPT_FUNCTION(remquo);  \
4299   COMMON_INTERCEPT_FUNCTION(remquof); \
4300   COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4301 #else
4302 #define INIT_REMQUO
4303 #endif
4305 #if SANITIZER_INTERCEPT_LGAMMA
4306 extern int signgam;
4307 INTERCEPTOR(double, lgamma, double x) {
4308   void *ctx;
4309   COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4310   double res = REAL(lgamma)(x);
4311   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4312   return res;
4314 INTERCEPTOR(float, lgammaf, float x) {
4315   void *ctx;
4316   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4317   float res = REAL(lgammaf)(x);
4318   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4319   return res;
4321 INTERCEPTOR(long double, lgammal, long double x) {
4322   void *ctx;
4323   COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4324   long double res = REAL(lgammal)(x);
4325   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4326   return res;
4328 #define INIT_LGAMMA                   \
4329   COMMON_INTERCEPT_FUNCTION(lgamma);  \
4330   COMMON_INTERCEPT_FUNCTION(lgammaf); \
4331   COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4332 #else
4333 #define INIT_LGAMMA
4334 #endif
4336 #if SANITIZER_INTERCEPT_LGAMMA_R
4337 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4338   void *ctx;
4339   COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4340   // FIXME: under ASan the call below may write to freed memory and corrupt
4341   // its metadata. See
4342   // https://github.com/google/sanitizers/issues/321.
4343   double res = REAL(lgamma_r)(x, signp);
4344   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4345   return res;
4347 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4348   void *ctx;
4349   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4350   // FIXME: under ASan the call below may write to freed memory and corrupt
4351   // its metadata. See
4352   // https://github.com/google/sanitizers/issues/321.
4353   float res = REAL(lgammaf_r)(x, signp);
4354   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4355   return res;
4357 #define INIT_LGAMMA_R                   \
4358   COMMON_INTERCEPT_FUNCTION(lgamma_r);  \
4359   COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4360 #else
4361 #define INIT_LGAMMA_R
4362 #endif
4364 #if SANITIZER_INTERCEPT_LGAMMAL_R
4365 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4366   void *ctx;
4367   COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4368   // FIXME: under ASan the call below may write to freed memory and corrupt
4369   // its metadata. See
4370   // https://github.com/google/sanitizers/issues/321.
4371   long double res = REAL(lgammal_r)(x, signp);
4372   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4373   return res;
4375 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4376 #else
4377 #define INIT_LGAMMAL_R
4378 #endif
4380 #if SANITIZER_INTERCEPT_DRAND48_R
4381 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4382   void *ctx;
4383   COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4384   // FIXME: under ASan the call below may write to freed memory and corrupt
4385   // its metadata. See
4386   // https://github.com/google/sanitizers/issues/321.
4387   int res = REAL(drand48_r)(buffer, result);
4388   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4389   return res;
4391 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4392   void *ctx;
4393   COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4394   // FIXME: under ASan the call below may write to freed memory and corrupt
4395   // its metadata. See
4396   // https://github.com/google/sanitizers/issues/321.
4397   int res = REAL(lrand48_r)(buffer, result);
4398   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4399   return res;
4401 #define INIT_DRAND48_R                  \
4402   COMMON_INTERCEPT_FUNCTION(drand48_r); \
4403   COMMON_INTERCEPT_FUNCTION(lrand48_r);
4404 #else
4405 #define INIT_DRAND48_R
4406 #endif
4408 #if SANITIZER_INTERCEPT_RAND_R
4409 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4410   void *ctx;
4411   COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4412   COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4413   return REAL(rand_r)(seedp);
4415 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4416 #else
4417 #define INIT_RAND_R
4418 #endif
4420 #if SANITIZER_INTERCEPT_GETLINE
4421 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4422   void *ctx;
4423   COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4424   // FIXME: under ASan the call below may write to freed memory and corrupt
4425   // its metadata. See
4426   // https://github.com/google/sanitizers/issues/321.
4427   SSIZE_T res = REAL(getline)(lineptr, n, stream);
4428   if (res > 0) {
4429     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4430     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4431     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4432   }
4433   return res;
4436 // FIXME: under ASan the call below may write to freed memory and corrupt its
4437 // metadata. See
4438 // https://github.com/google/sanitizers/issues/321.
4439 #define GETDELIM_INTERCEPTOR_IMPL(vname)                                       \
4440   {                                                                            \
4441     void *ctx;                                                                 \
4442     COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream);           \
4443     SSIZE_T res = REAL(vname)(lineptr, n, delim, stream);                      \
4444     if (res > 0) {                                                             \
4445       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));          \
4446       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));                      \
4447       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);                  \
4448     }                                                                          \
4449     return res;                                                                \
4450   }
4452 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4453             void *stream)
4454 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4456 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4457 // with its own body.
4458 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4459             void *stream)
4460 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4462 #define INIT_GETLINE                     \
4463   COMMON_INTERCEPT_FUNCTION(getline);    \
4464   COMMON_INTERCEPT_FUNCTION(__getdelim); \
4465   COMMON_INTERCEPT_FUNCTION(getdelim);
4466 #else
4467 #define INIT_GETLINE
4468 #endif
4470 #if SANITIZER_INTERCEPT_ICONV
4471 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4472             char **outbuf, SIZE_T *outbytesleft) {
4473   void *ctx;
4474   COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4475                            outbytesleft);
4476   if (inbytesleft)
4477     COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4478   if (inbuf && inbytesleft)
4479     COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4480   if (outbytesleft)
4481     COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4482   void *outbuf_orig = outbuf ? *outbuf : nullptr;
4483   // FIXME: under ASan the call below may write to freed memory and corrupt
4484   // its metadata. See
4485   // https://github.com/google/sanitizers/issues/321.
4486   SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4487   if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
4488     SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4489     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4490   }
4491   return res;
4493 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4494 #else
4495 #define INIT_ICONV
4496 #endif
4498 #if SANITIZER_INTERCEPT_TIMES
4499 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4500   void *ctx;
4501   COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4502   // FIXME: under ASan the call below may write to freed memory and corrupt
4503   // its metadata. See
4504   // https://github.com/google/sanitizers/issues/321.
4505   __sanitizer_clock_t res = REAL(times)(tms);
4506   if (res != (__sanitizer_clock_t)-1 && tms)
4507     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4508   return res;
4510 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4511 #else
4512 #define INIT_TIMES
4513 #endif
4515 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4516 #if !SANITIZER_S390
4517 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4518 // If you see any crashes around this functions, there are 2 known issues with
4519 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4520 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4521 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4522 // to access thread local variables (it should not happen normally,
4523 // because sanitizers use initial-exec tls model).
4524 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4525   void *ctx;
4526   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4527   void *res = REAL(__tls_get_addr)(arg);
4528   uptr tls_begin, tls_end;
4529   COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4530   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4531   if (dtv) {
4532     // New DTLS block has been allocated.
4533     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4534   }
4535   return res;
4537 #if SANITIZER_PPC
4538 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4539 // mostly the same semantics as __tls_get_addr, but its presence enables
4540 // some optimizations in linker (which are safe to ignore here).
4541 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4542                           visibility("default")))
4543 void *__tls_get_addr_opt(void *arg);
4544 #endif
4545 #else // SANITIZER_S390
4546 // On s390, we have to intercept two functions here:
4547 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4548 //   the usual __tls_get_addr, but returns a TP-relative offset instead of
4549 //   a proper pointer.  It is used by dlsym for TLS symbols.
4550 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4551 //   descriptor offset as an argument instead of a pointer.  GOT address
4552 //   is passed in r12, so it's necessary to write it in assembly.  This is
4553 //   the function used by the compiler.
4554 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
4555 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
4556 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
4557 extern "C" uptr __tls_get_offset(void *arg);
4558 extern "C" uptr __interceptor___tls_get_offset(void *arg);
4559 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4560   void *ctx;
4561   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4562   uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
4563   uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4564   void *ptr = reinterpret_cast<void *>(res + tp);
4565   uptr tls_begin, tls_end;
4566   COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4567   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4568   if (dtv) {
4569     // New DTLS block has been allocated.
4570     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4571   }
4572   return res;
4574 // We need a hidden symbol aliasing the above, so that we can jump
4575 // directly to it from the assembly below.
4576 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4577                           visibility("hidden")))
4578 uptr __tls_get_addr_hidden(void *arg);
4579 // Now carefully intercept __tls_get_offset.
4580 asm(
4581   ".text\n"
4582 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4583 // exports our symbol.
4584   ".weak __tls_get_offset\n"
4585   ".type __tls_get_offset, @function\n"
4586   "__tls_get_offset:\n"
4587   ".global __interceptor___tls_get_offset\n"
4588   ".type __interceptor___tls_get_offset, @function\n"
4589   "__interceptor___tls_get_offset:\n"
4590 #ifdef __s390x__
4591   "la %r2, 0(%r2,%r12)\n"
4592   "jg __tls_get_addr_hidden\n"
4593 #else
4594   "basr %r3,0\n"
4595   "0: la %r2,0(%r2,%r12)\n"
4596   "l %r4,1f-0b(%r3)\n"
4597   "b 0(%r4,%r3)\n"
4598   "1: .long __tls_get_addr_hidden - 0b\n"
4599 #endif
4600   ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
4601 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
4602   ".type __tls_get_offset_wrapper, @function\n"
4603   "__tls_get_offset_wrapper:\n"
4604 #ifdef __s390x__
4605   "sgr %r2,%r12\n"
4606 #else
4607   "sr %r2,%r12\n"
4608 #endif
4609   "br %r3\n"
4610   ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
4612 #endif // SANITIZER_S390
4613 #else
4614 #define INIT_TLS_GET_ADDR
4615 #endif
4617 #if SANITIZER_INTERCEPT_LISTXATTR
4618 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4619   void *ctx;
4620   COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4621   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4622   // FIXME: under ASan the call below may write to freed memory and corrupt
4623   // its metadata. See
4624   // https://github.com/google/sanitizers/issues/321.
4625   SSIZE_T res = REAL(listxattr)(path, list, size);
4626   // Here and below, size == 0 is a special case where nothing is written to the
4627   // buffer, and res contains the desired buffer size.
4628   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4629   return res;
4631 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4632   void *ctx;
4633   COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4634   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4635   // FIXME: under ASan the call below may write to freed memory and corrupt
4636   // its metadata. See
4637   // https://github.com/google/sanitizers/issues/321.
4638   SSIZE_T res = REAL(llistxattr)(path, list, size);
4639   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4640   return res;
4642 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4643   void *ctx;
4644   COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4645   // FIXME: under ASan the call below may write to freed memory and corrupt
4646   // its metadata. See
4647   // https://github.com/google/sanitizers/issues/321.
4648   SSIZE_T res = REAL(flistxattr)(fd, list, size);
4649   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4650   return res;
4652 #define INIT_LISTXATTR                   \
4653   COMMON_INTERCEPT_FUNCTION(listxattr);  \
4654   COMMON_INTERCEPT_FUNCTION(llistxattr); \
4655   COMMON_INTERCEPT_FUNCTION(flistxattr);
4656 #else
4657 #define INIT_LISTXATTR
4658 #endif
4660 #if SANITIZER_INTERCEPT_GETXATTR
4661 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4662             SIZE_T size) {
4663   void *ctx;
4664   COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4665   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4666   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4667   // FIXME: under ASan the call below may write to freed memory and corrupt
4668   // its metadata. See
4669   // https://github.com/google/sanitizers/issues/321.
4670   SSIZE_T res = REAL(getxattr)(path, name, value, size);
4671   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4672   return res;
4674 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4675             SIZE_T size) {
4676   void *ctx;
4677   COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4678   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4679   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4680   // FIXME: under ASan the call below may write to freed memory and corrupt
4681   // its metadata. See
4682   // https://github.com/google/sanitizers/issues/321.
4683   SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4684   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4685   return res;
4687 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4688             SIZE_T size) {
4689   void *ctx;
4690   COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4691   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4692   // FIXME: under ASan the call below may write to freed memory and corrupt
4693   // its metadata. See
4694   // https://github.com/google/sanitizers/issues/321.
4695   SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4696   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4697   return res;
4699 #define INIT_GETXATTR                   \
4700   COMMON_INTERCEPT_FUNCTION(getxattr);  \
4701   COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4702   COMMON_INTERCEPT_FUNCTION(fgetxattr);
4703 #else
4704 #define INIT_GETXATTR
4705 #endif
4707 #if SANITIZER_INTERCEPT_GETRESID
4708 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4709   void *ctx;
4710   COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4711   // FIXME: under ASan the call below may write to freed memory and corrupt
4712   // its metadata. See
4713   // https://github.com/google/sanitizers/issues/321.
4714   int res = REAL(getresuid)(ruid, euid, suid);
4715   if (res >= 0) {
4716     if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4717     if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4718     if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4719   }
4720   return res;
4722 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4723   void *ctx;
4724   COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4725   // FIXME: under ASan the call below may write to freed memory and corrupt
4726   // its metadata. See
4727   // https://github.com/google/sanitizers/issues/321.
4728   int res = REAL(getresgid)(rgid, egid, sgid);
4729   if (res >= 0) {
4730     if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4731     if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4732     if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4733   }
4734   return res;
4736 #define INIT_GETRESID                   \
4737   COMMON_INTERCEPT_FUNCTION(getresuid); \
4738   COMMON_INTERCEPT_FUNCTION(getresgid);
4739 #else
4740 #define INIT_GETRESID
4741 #endif
4743 #if SANITIZER_INTERCEPT_GETIFADDRS
4744 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4745 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4746 // intercept it to poison the memory again.
4747 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4748   void *ctx;
4749   COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4750   // FIXME: under ASan the call below may write to freed memory and corrupt
4751   // its metadata. See
4752   // https://github.com/google/sanitizers/issues/321.
4753   int res = REAL(getifaddrs)(ifap);
4754   if (res == 0 && ifap) {
4755     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4756     __sanitizer_ifaddrs *p = *ifap;
4757     while (p) {
4758       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4759       if (p->ifa_name)
4760         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4761                                        REAL(strlen)(p->ifa_name) + 1);
4762       if (p->ifa_addr)
4763         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4764       if (p->ifa_netmask)
4765         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4766       // On Linux this is a union, but the other member also points to a
4767       // struct sockaddr, so the following is sufficient.
4768       if (p->ifa_dstaddr)
4769         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4770       // FIXME(smatveev): Unpoison p->ifa_data as well.
4771       p = p->ifa_next;
4772     }
4773   }
4774   return res;
4776 #define INIT_GETIFADDRS                  \
4777   COMMON_INTERCEPT_FUNCTION(getifaddrs);
4778 #else
4779 #define INIT_GETIFADDRS
4780 #endif
4782 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4783 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4784   void *ctx;
4785   COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
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   char *res = REAL(if_indextoname)(ifindex, ifname);
4790   if (res && ifname)
4791     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4792   return res;
4794 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4795   void *ctx;
4796   COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4797   if (ifname)
4798     COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4799   return REAL(if_nametoindex)(ifname);
4801 #define INIT_IF_INDEXTONAME                  \
4802   COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4803   COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4804 #else
4805 #define INIT_IF_INDEXTONAME
4806 #endif
4808 #if SANITIZER_INTERCEPT_CAPGET
4809 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4810   void *ctx;
4811   COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4812   if (hdrp)
4813     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4814   // FIXME: under ASan the call below may write to freed memory and corrupt
4815   // its metadata. See
4816   // https://github.com/google/sanitizers/issues/321.
4817   int res = REAL(capget)(hdrp, datap);
4818   if (res == 0 && datap)
4819     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4820   // We can also return -1 and write to hdrp->version if the version passed in
4821   // hdrp->version is unsupported. But that's not a trivial condition to check,
4822   // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4823   return res;
4825 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4826   void *ctx;
4827   COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4828   if (hdrp)
4829     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4830   if (datap)
4831     COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4832   return REAL(capset)(hdrp, datap);
4834 #define INIT_CAPGET                  \
4835   COMMON_INTERCEPT_FUNCTION(capget); \
4836   COMMON_INTERCEPT_FUNCTION(capset);
4837 #else
4838 #define INIT_CAPGET
4839 #endif
4841 #if SANITIZER_INTERCEPT_AEABI_MEM
4842 DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr)
4843 DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr)
4844 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr)
4846 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4847   return WRAP(memmove)(to, from, size);
4849 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4850   return WRAP(memmove)(to, from, size);
4852 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4853   return WRAP(memmove)(to, from, size);
4855 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4856   return WRAP(memcpy)(to, from, size);
4858 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4859   return WRAP(memcpy)(to, from, size);
4861 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4862   return WRAP(memcpy)(to, from, size);
4864 // Note the argument order.
4865 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4866   return WRAP(memset)(block, c, size);
4868 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4869   return WRAP(memset)(block, c, size);
4871 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4872   return WRAP(memset)(block, c, size);
4874 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4875   return WRAP(memset)(block, 0, size);
4877 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4878   return WRAP(memset)(block, 0, size);
4880 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4881   return WRAP(memset)(block, 0, size);
4883 #define INIT_AEABI_MEM                         \
4884   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove);  \
4885   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4886   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4887   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy);   \
4888   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4);  \
4889   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8);  \
4890   COMMON_INTERCEPT_FUNCTION(__aeabi_memset);   \
4891   COMMON_INTERCEPT_FUNCTION(__aeabi_memset4);  \
4892   COMMON_INTERCEPT_FUNCTION(__aeabi_memset8);  \
4893   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr);   \
4894   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4);  \
4895   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4896 #else
4897 #define INIT_AEABI_MEM
4898 #endif  // SANITIZER_INTERCEPT_AEABI_MEM
4900 #if SANITIZER_INTERCEPT___BZERO
4901 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4903 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4904   return WRAP(memset)(block, 0, size);
4906 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4907 #else
4908 #define INIT___BZERO
4909 #endif  // SANITIZER_INTERCEPT___BZERO
4911 #if SANITIZER_INTERCEPT_FTIME
4912 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4913   void *ctx;
4914   COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
4915   // FIXME: under ASan the call below may write to freed memory and corrupt
4916   // its metadata. See
4917   // https://github.com/google/sanitizers/issues/321.
4918   int res = REAL(ftime)(tp);
4919   if (tp)
4920     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4921   return res;
4923 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4924 #else
4925 #define INIT_FTIME
4926 #endif  // SANITIZER_INTERCEPT_FTIME
4928 #if SANITIZER_INTERCEPT_XDR
4929 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4930             unsigned size, int op) {
4931   void *ctx;
4932   COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
4933   // FIXME: under ASan the call below may write to freed memory and corrupt
4934   // its metadata. See
4935   // https://github.com/google/sanitizers/issues/321.
4936   REAL(xdrmem_create)(xdrs, addr, size, op);
4937   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4938   if (op == __sanitizer_XDR_ENCODE) {
4939     // It's not obvious how much data individual xdr_ routines write.
4940     // Simply unpoison the entire target buffer in advance.
4941     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
4942   }
4945 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
4946   void *ctx;
4947   COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
4948   // FIXME: under ASan the call below may write to freed memory and corrupt
4949   // its metadata. See
4950   // https://github.com/google/sanitizers/issues/321.
4951   REAL(xdrstdio_create)(xdrs, file, op);
4952   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4955 // FIXME: under ASan the call below may write to freed memory and corrupt
4956 // its metadata. See
4957 // https://github.com/google/sanitizers/issues/321.
4958 #define XDR_INTERCEPTOR(F, T)                             \
4959   INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) {      \
4960     void *ctx;                                            \
4961     COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p);            \
4962     if (p && xdrs->x_op == __sanitizer_XDR_ENCODE)        \
4963       COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));  \
4964     int res = REAL(F)(xdrs, p);                           \
4965     if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
4966       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
4967     return res;                                           \
4968   }
4970 XDR_INTERCEPTOR(xdr_short, short)
4971 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
4972 XDR_INTERCEPTOR(xdr_int, int)
4973 XDR_INTERCEPTOR(xdr_u_int, unsigned)
4974 XDR_INTERCEPTOR(xdr_long, long)
4975 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
4976 XDR_INTERCEPTOR(xdr_hyper, long long)
4977 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
4978 XDR_INTERCEPTOR(xdr_longlong_t, long long)
4979 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
4980 XDR_INTERCEPTOR(xdr_int8_t, u8)
4981 XDR_INTERCEPTOR(xdr_uint8_t, u8)
4982 XDR_INTERCEPTOR(xdr_int16_t, u16)
4983 XDR_INTERCEPTOR(xdr_uint16_t, u16)
4984 XDR_INTERCEPTOR(xdr_int32_t, u32)
4985 XDR_INTERCEPTOR(xdr_uint32_t, u32)
4986 XDR_INTERCEPTOR(xdr_int64_t, u64)
4987 XDR_INTERCEPTOR(xdr_uint64_t, u64)
4988 XDR_INTERCEPTOR(xdr_quad_t, long long)
4989 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
4990 XDR_INTERCEPTOR(xdr_bool, bool)
4991 XDR_INTERCEPTOR(xdr_enum, int)
4992 XDR_INTERCEPTOR(xdr_char, char)
4993 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
4994 XDR_INTERCEPTOR(xdr_float, float)
4995 XDR_INTERCEPTOR(xdr_double, double)
4997 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
4998 // wrapstring, sizeof
5000 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5001             unsigned maxsize) {
5002   void *ctx;
5003   COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5004   if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5005     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5006     COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5007     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5008   }
5009   // FIXME: under ASan the call below may write to freed memory and corrupt
5010   // its metadata. See
5011   // https://github.com/google/sanitizers/issues/321.
5012   int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5013   if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5014     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5015     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5016     if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5017   }
5018   return res;
5021 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5022             unsigned maxsize) {
5023   void *ctx;
5024   COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5025   if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5026     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5027     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5028   }
5029   // FIXME: under ASan the call below may write to freed memory and corrupt
5030   // its metadata. See
5031   // https://github.com/google/sanitizers/issues/321.
5032   int res = REAL(xdr_string)(xdrs, p, maxsize);
5033   if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5034     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5035     if (res && *p)
5036       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5037   }
5038   return res;
5041 #define INIT_XDR                               \
5042   COMMON_INTERCEPT_FUNCTION(xdrmem_create);    \
5043   COMMON_INTERCEPT_FUNCTION(xdrstdio_create);  \
5044   COMMON_INTERCEPT_FUNCTION(xdr_short);        \
5045   COMMON_INTERCEPT_FUNCTION(xdr_u_short);      \
5046   COMMON_INTERCEPT_FUNCTION(xdr_int);          \
5047   COMMON_INTERCEPT_FUNCTION(xdr_u_int);        \
5048   COMMON_INTERCEPT_FUNCTION(xdr_long);         \
5049   COMMON_INTERCEPT_FUNCTION(xdr_u_long);       \
5050   COMMON_INTERCEPT_FUNCTION(xdr_hyper);        \
5051   COMMON_INTERCEPT_FUNCTION(xdr_u_hyper);      \
5052   COMMON_INTERCEPT_FUNCTION(xdr_longlong_t);   \
5053   COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5054   COMMON_INTERCEPT_FUNCTION(xdr_int8_t);       \
5055   COMMON_INTERCEPT_FUNCTION(xdr_uint8_t);      \
5056   COMMON_INTERCEPT_FUNCTION(xdr_int16_t);      \
5057   COMMON_INTERCEPT_FUNCTION(xdr_uint16_t);     \
5058   COMMON_INTERCEPT_FUNCTION(xdr_int32_t);      \
5059   COMMON_INTERCEPT_FUNCTION(xdr_uint32_t);     \
5060   COMMON_INTERCEPT_FUNCTION(xdr_int64_t);      \
5061   COMMON_INTERCEPT_FUNCTION(xdr_uint64_t);     \
5062   COMMON_INTERCEPT_FUNCTION(xdr_quad_t);       \
5063   COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t);     \
5064   COMMON_INTERCEPT_FUNCTION(xdr_bool);         \
5065   COMMON_INTERCEPT_FUNCTION(xdr_enum);         \
5066   COMMON_INTERCEPT_FUNCTION(xdr_char);         \
5067   COMMON_INTERCEPT_FUNCTION(xdr_u_char);       \
5068   COMMON_INTERCEPT_FUNCTION(xdr_float);        \
5069   COMMON_INTERCEPT_FUNCTION(xdr_double);       \
5070   COMMON_INTERCEPT_FUNCTION(xdr_bytes);        \
5071   COMMON_INTERCEPT_FUNCTION(xdr_string);
5072 #else
5073 #define INIT_XDR
5074 #endif  // SANITIZER_INTERCEPT_XDR
5076 #if SANITIZER_INTERCEPT_TSEARCH
5077 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5078             int (*compar)(const void *, const void *)) {
5079   void *ctx;
5080   COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5081   // FIXME: under ASan the call below may write to freed memory and corrupt
5082   // its metadata. See
5083   // https://github.com/google/sanitizers/issues/321.
5084   void *res = REAL(tsearch)(key, rootp, compar);
5085   if (res && *(void **)res == key)
5086     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5087   return res;
5089 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5090 #else
5091 #define INIT_TSEARCH
5092 #endif
5094 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5095     SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5096 void unpoison_file(__sanitizer_FILE *fp) {
5097 #if SANITIZER_HAS_STRUCT_FILE
5098   COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5099   if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5100     COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5101                                         fp->_IO_read_end - fp->_IO_read_base);
5102 #endif  // SANITIZER_HAS_STRUCT_FILE
5104 #endif
5106 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5107 // These guys are called when a .c source is built with -O2.
5108 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5109   void *ctx;
5110   COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5111   int res = REAL(__uflow)(fp);
5112   unpoison_file(fp);
5113   return res;
5115 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5116   void *ctx;
5117   COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5118   int res = REAL(__underflow)(fp);
5119   unpoison_file(fp);
5120   return res;
5122 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5123   void *ctx;
5124   COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5125   int res = REAL(__overflow)(fp, ch);
5126   unpoison_file(fp);
5127   return res;
5129 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5130   void *ctx;
5131   COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5132   int res = REAL(__wuflow)(fp);
5133   unpoison_file(fp);
5134   return res;
5136 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5137   void *ctx;
5138   COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5139   int res = REAL(__wunderflow)(fp);
5140   unpoison_file(fp);
5141   return res;
5143 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5144   void *ctx;
5145   COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5146   int res = REAL(__woverflow)(fp, ch);
5147   unpoison_file(fp);
5148   return res;
5150 #define INIT_LIBIO_INTERNALS               \
5151   COMMON_INTERCEPT_FUNCTION(__uflow);      \
5152   COMMON_INTERCEPT_FUNCTION(__underflow);  \
5153   COMMON_INTERCEPT_FUNCTION(__overflow);   \
5154   COMMON_INTERCEPT_FUNCTION(__wuflow);     \
5155   COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5156   COMMON_INTERCEPT_FUNCTION(__woverflow);
5157 #else
5158 #define INIT_LIBIO_INTERNALS
5159 #endif
5161 #if SANITIZER_INTERCEPT_FOPEN
5162 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5163   void *ctx;
5164   COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5165   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5166   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5167   __sanitizer_FILE *res = REAL(fopen)(path, mode);
5168   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5169   if (res) unpoison_file(res);
5170   return res;
5172 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5173   void *ctx;
5174   COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5175   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5176   __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5177   if (res) unpoison_file(res);
5178   return res;
5180 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5181             __sanitizer_FILE *fp) {
5182   void *ctx;
5183   COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5184   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5185   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5186   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5187   __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5188   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5189   if (res) unpoison_file(res);
5190   return res;
5192 #define INIT_FOPEN                   \
5193   COMMON_INTERCEPT_FUNCTION(fopen);  \
5194   COMMON_INTERCEPT_FUNCTION(fdopen); \
5195   COMMON_INTERCEPT_FUNCTION(freopen);
5196 #else
5197 #define INIT_FOPEN
5198 #endif
5200 #if SANITIZER_INTERCEPT_FOPEN64
5201 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5202   void *ctx;
5203   COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5204   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5205   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5206   __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5207   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5208   if (res) unpoison_file(res);
5209   return res;
5211 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5212             __sanitizer_FILE *fp) {
5213   void *ctx;
5214   COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5215   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5216   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5217   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5218   __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5219   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5220   if (res) unpoison_file(res);
5221   return res;
5223 #define INIT_FOPEN64                  \
5224   COMMON_INTERCEPT_FUNCTION(fopen64); \
5225   COMMON_INTERCEPT_FUNCTION(freopen64);
5226 #else
5227 #define INIT_FOPEN64
5228 #endif
5230 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5231 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5232   void *ctx;
5233   COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5234   // FIXME: under ASan the call below may write to freed memory and corrupt
5235   // its metadata. See
5236   // https://github.com/google/sanitizers/issues/321.
5237   __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5238   if (res) {
5239     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5240     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5241     unpoison_file(res);
5242     FileMetadata file = {ptr, sizeloc};
5243     SetInterceptorMetadata(res, file);
5244   }
5245   return res;
5247 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5248             SIZE_T *sizeloc) {
5249   void *ctx;
5250   COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5251   __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5252   if (res) {
5253     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5254     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5255     unpoison_file(res);
5256     FileMetadata file = {(char **)ptr, sizeloc};
5257     SetInterceptorMetadata(res, file);
5258   }
5259   return res;
5261 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5262             const char *mode) {
5263   void *ctx;
5264   COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5265   // FIXME: under ASan the call below may write to freed memory and corrupt
5266   // its metadata. See
5267   // https://github.com/google/sanitizers/issues/321.
5268   __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5269   if (res) unpoison_file(res);
5270   return res;
5272 #define INIT_OPEN_MEMSTREAM                   \
5273   COMMON_INTERCEPT_FUNCTION(open_memstream);  \
5274   COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5275   COMMON_INTERCEPT_FUNCTION(fmemopen);
5276 #else
5277 #define INIT_OPEN_MEMSTREAM
5278 #endif
5280 #if SANITIZER_INTERCEPT_OBSTACK
5281 static void initialize_obstack(__sanitizer_obstack *obstack) {
5282   COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5283   if (obstack->chunk)
5284     COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5285                                         sizeof(*obstack->chunk));
5288 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5289             int align, void *(*alloc_fn)(uptr arg, uptr sz),
5290             void (*free_fn)(uptr arg, void *p)) {
5291   void *ctx;
5292   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5293                            free_fn);
5294   int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5295   if (res) initialize_obstack(obstack);
5296   return res;
5298 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5299             int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5300   void *ctx;
5301   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5302                            free_fn);
5303   int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5304   if (res) initialize_obstack(obstack);
5305   return res;
5307 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5308   void *ctx;
5309   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5310   REAL(_obstack_newchunk)(obstack, length);
5311   if (obstack->chunk)
5312     COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5313         obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5315 #define INIT_OBSTACK                           \
5316   COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5317   COMMON_INTERCEPT_FUNCTION(_obstack_begin);   \
5318   COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5319 #else
5320 #define INIT_OBSTACK
5321 #endif
5323 #if SANITIZER_INTERCEPT_FFLUSH
5324 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5325   void *ctx;
5326   COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5327   int res = REAL(fflush)(fp);
5328   // FIXME: handle fp == NULL
5329   if (fp) {
5330     const FileMetadata *m = GetInterceptorMetadata(fp);
5331     if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5332   }
5333   return res;
5335 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5336 #else
5337 #define INIT_FFLUSH
5338 #endif
5340 #if SANITIZER_INTERCEPT_FCLOSE
5341 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5342   void *ctx;
5343   COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5344   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5345   const FileMetadata *m = GetInterceptorMetadata(fp);
5346   int res = REAL(fclose)(fp);
5347   if (m) {
5348     COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5349     DeleteInterceptorMetadata(fp);
5350   }
5351   return res;
5353 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5354 #else
5355 #define INIT_FCLOSE
5356 #endif
5358 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5359 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5360   void *ctx;
5361   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5362   if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5363   COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5364   void *res = REAL(dlopen)(filename, flag);
5365   COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5366   return res;
5369 INTERCEPTOR(int, dlclose, void *handle) {
5370   void *ctx;
5371   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5372   int res = REAL(dlclose)(handle);
5373   COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5374   return res;
5376 #define INIT_DLOPEN_DLCLOSE          \
5377   COMMON_INTERCEPT_FUNCTION(dlopen); \
5378   COMMON_INTERCEPT_FUNCTION(dlclose);
5379 #else
5380 #define INIT_DLOPEN_DLCLOSE
5381 #endif
5383 #if SANITIZER_INTERCEPT_GETPASS
5384 INTERCEPTOR(char *, getpass, const char *prompt) {
5385   void *ctx;
5386   COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5387   if (prompt)
5388     COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5389   char *res = REAL(getpass)(prompt);
5390   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5391   return res;
5394 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5395 #else
5396 #define INIT_GETPASS
5397 #endif
5399 #if SANITIZER_INTERCEPT_TIMERFD
5400 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5401             void *old_value) {
5402   void *ctx;
5403   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5404                            old_value);
5405   COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5406   int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5407   if (res != -1 && old_value)
5408     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5409   return res;
5412 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5413   void *ctx;
5414   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5415   int res = REAL(timerfd_gettime)(fd, curr_value);
5416   if (res != -1 && curr_value)
5417     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5418   return res;
5420 #define INIT_TIMERFD                          \
5421   COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5422   COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5423 #else
5424 #define INIT_TIMERFD
5425 #endif
5427 #if SANITIZER_INTERCEPT_MLOCKX
5428 // Linux kernel has a bug that leads to kernel deadlock if a process
5429 // maps TBs of memory and then calls mlock().
5430 static void MlockIsUnsupported() {
5431   static atomic_uint8_t printed;
5432   if (atomic_exchange(&printed, 1, memory_order_relaxed))
5433     return;
5434   VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5435           SanitizerToolName);
5438 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5439   MlockIsUnsupported();
5440   return 0;
5443 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5444   MlockIsUnsupported();
5445   return 0;
5448 INTERCEPTOR(int, mlockall, int flags) {
5449   MlockIsUnsupported();
5450   return 0;
5453 INTERCEPTOR(int, munlockall, void) {
5454   MlockIsUnsupported();
5455   return 0;
5458 #define INIT_MLOCKX                                                            \
5459   COMMON_INTERCEPT_FUNCTION(mlock);                                            \
5460   COMMON_INTERCEPT_FUNCTION(munlock);                                          \
5461   COMMON_INTERCEPT_FUNCTION(mlockall);                                         \
5462   COMMON_INTERCEPT_FUNCTION(munlockall);
5464 #else
5465 #define INIT_MLOCKX
5466 #endif  // SANITIZER_INTERCEPT_MLOCKX
5468 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5469 struct WrappedCookie {
5470   void *real_cookie;
5471   __sanitizer_cookie_io_functions_t real_io_funcs;
5474 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5475   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5476   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5477   __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5478   return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5481 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5482   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5483   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5484   __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5485   return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5488 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5489   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5490   COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5491   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5492   __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5493   return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5494                    : -1;
5497 static int wrapped_close(void *cookie) {
5498   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5499   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5500   __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5501   int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5502   InternalFree(wrapped_cookie);
5503   return res;
5506 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5507             __sanitizer_cookie_io_functions_t io_funcs) {
5508   void *ctx;
5509   COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5510   WrappedCookie *wrapped_cookie =
5511       (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5512   wrapped_cookie->real_cookie = cookie;
5513   wrapped_cookie->real_io_funcs = io_funcs;
5514   __sanitizer_FILE *res =
5515       REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5516                                                wrapped_seek, wrapped_close});
5517   return res;
5520 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5521 #else
5522 #define INIT_FOPENCOOKIE
5523 #endif  // SANITIZER_INTERCEPT_FOPENCOOKIE
5525 #if SANITIZER_INTERCEPT_SEM
5526 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5527   void *ctx;
5528   COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5529   // Workaround a bug in glibc's "old" semaphore implementation by
5530   // zero-initializing the sem_t contents. This has to be done here because
5531   // interceptors bind to the lowest symbols version by default, hitting the
5532   // buggy code path while the non-sanitized build of the same code works fine.
5533   REAL(memset)(s, 0, sizeof(*s));
5534   int res = REAL(sem_init)(s, pshared, value);
5535   return res;
5538 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5539   void *ctx;
5540   COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5541   int res = REAL(sem_destroy)(s);
5542   return res;
5545 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5546   void *ctx;
5547   COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5548   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5549   if (res == 0) {
5550     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5551   }
5552   return res;
5555 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5556   void *ctx;
5557   COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5558   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5559   if (res == 0) {
5560     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5561   }
5562   return res;
5565 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5566   void *ctx;
5567   COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5568   COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5569   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5570   if (res == 0) {
5571     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5572   }
5573   return res;
5576 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5577   void *ctx;
5578   COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5579   COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5580   int res = REAL(sem_post)(s);
5581   return res;
5584 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5585   void *ctx;
5586   COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5587   int res = REAL(sem_getvalue)(s, sval);
5588   if (res == 0) {
5589     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5590     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5591   }
5592   return res;
5594 #define INIT_SEM                                                               \
5595   COMMON_INTERCEPT_FUNCTION(sem_init);                                         \
5596   COMMON_INTERCEPT_FUNCTION(sem_destroy);                                      \
5597   COMMON_INTERCEPT_FUNCTION(sem_wait);                                         \
5598   COMMON_INTERCEPT_FUNCTION(sem_trywait);                                      \
5599   COMMON_INTERCEPT_FUNCTION(sem_timedwait);                                    \
5600   COMMON_INTERCEPT_FUNCTION(sem_post);                                         \
5601   COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5602 #else
5603 #define INIT_SEM
5604 #endif // SANITIZER_INTERCEPT_SEM
5606 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5607 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5608   void *ctx;
5609   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5610   int res = REAL(pthread_setcancelstate)(state, oldstate);
5611   if (res == 0)
5612     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5613   return res;
5616 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5617   void *ctx;
5618   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5619   int res = REAL(pthread_setcanceltype)(type, oldtype);
5620   if (res == 0)
5621     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5622   return res;
5624 #define INIT_PTHREAD_SETCANCEL                                                 \
5625   COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate);                           \
5626   COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5627 #else
5628 #define INIT_PTHREAD_SETCANCEL
5629 #endif
5631 #if SANITIZER_INTERCEPT_MINCORE
5632 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5633   void *ctx;
5634   COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5635   int res = REAL(mincore)(addr, length, vec);
5636   if (res == 0) {
5637     uptr page_size = GetPageSizeCached();
5638     uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5639     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5640   }
5641   return res;
5643 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5644 #else
5645 #define INIT_MINCORE
5646 #endif
5648 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5649 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5650             uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5651             uptr flags) {
5652   void *ctx;
5653   COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5654                            remote_iov, riovcnt, flags);
5655   SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5656                                        riovcnt, flags);
5657   if (res > 0)
5658     write_iovec(ctx, local_iov, liovcnt, res);
5659   return res;
5662 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5663             uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5664             uptr flags) {
5665   void *ctx;
5666   COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5667                            remote_iov, riovcnt, flags);
5668   SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5669                                         riovcnt, flags);
5670   if (res > 0)
5671     read_iovec(ctx, local_iov, liovcnt, res);
5672   return res;
5674 #define INIT_PROCESS_VM_READV                                                  \
5675   COMMON_INTERCEPT_FUNCTION(process_vm_readv);                                 \
5676   COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5677 #else
5678 #define INIT_PROCESS_VM_READV
5679 #endif
5681 #if SANITIZER_INTERCEPT_CTERMID
5682 INTERCEPTOR(char *, ctermid, char *s) {
5683   void *ctx;
5684   COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5685   char *res = REAL(ctermid)(s);
5686   if (res) {
5687     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5688   }
5689   return res;
5691 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5692 #else
5693 #define INIT_CTERMID
5694 #endif
5696 #if SANITIZER_INTERCEPT_CTERMID_R
5697 INTERCEPTOR(char *, ctermid_r, char *s) {
5698   void *ctx;
5699   COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5700   char *res = REAL(ctermid_r)(s);
5701   if (res) {
5702     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5703   }
5704   return res;
5706 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5707 #else
5708 #define INIT_CTERMID_R
5709 #endif
5711 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5712 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5713   void *ctx;
5714   COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5715   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5716   SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5717   if (res > 0) {
5718     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5719   }
5720   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5721   return res;
5724 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5725             void *srcaddr, int *addrlen) {
5726   void *ctx;
5727   COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5728                            addrlen);
5729   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5730   SIZE_T srcaddr_sz;
5731   if (srcaddr) srcaddr_sz = *addrlen;
5732   (void)srcaddr_sz;  // prevent "set but not used" warning
5733   SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5734   if (res > 0) {
5735     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5736     if (srcaddr)
5737       COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5738                                           Min((SIZE_T)*addrlen, srcaddr_sz));
5739   }
5740   return res;
5742 #define INIT_RECV_RECVFROM          \
5743   COMMON_INTERCEPT_FUNCTION(recv);  \
5744   COMMON_INTERCEPT_FUNCTION(recvfrom);
5745 #else
5746 #define INIT_RECV_RECVFROM
5747 #endif
5749 #if SANITIZER_INTERCEPT_SEND_SENDTO
5750 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5751   void *ctx;
5752   COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5753   if (fd >= 0) {
5754     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5755     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5756   }
5757   SSIZE_T res = REAL(send)(fd, buf, len, flags);
5758   if (common_flags()->intercept_send && res > 0)
5759     COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5760   return res;
5763 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5764             void *dstaddr, int addrlen) {
5765   void *ctx;
5766   COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5767   if (fd >= 0) {
5768     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5769     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5770   }
5771   // Can't check dstaddr as it may have uninitialized padding at the end.
5772   SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5773   if (common_flags()->intercept_send && res > 0)
5774     COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5775   return res;
5777 #define INIT_SEND_SENDTO           \
5778   COMMON_INTERCEPT_FUNCTION(send); \
5779   COMMON_INTERCEPT_FUNCTION(sendto);
5780 #else
5781 #define INIT_SEND_SENDTO
5782 #endif
5784 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5785 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5786   void *ctx;
5787   COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5788   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5789   int res = REAL(eventfd_read)(fd, value);
5790   if (res == 0) {
5791     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
5792     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5793   }
5794   return res;
5796 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
5797   void *ctx;
5798   COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
5799   if (fd >= 0) {
5800     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5801     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5802   }
5803   int res = REAL(eventfd_write)(fd, value);
5804   return res;
5806 #define INIT_EVENTFD_READ_WRITE            \
5807   COMMON_INTERCEPT_FUNCTION(eventfd_read); \
5808   COMMON_INTERCEPT_FUNCTION(eventfd_write)
5809 #else
5810 #define INIT_EVENTFD_READ_WRITE
5811 #endif
5813 #if SANITIZER_INTERCEPT_STAT
5814 INTERCEPTOR(int, stat, const char *path, void *buf) {
5815   void *ctx;
5816   COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
5817   if (common_flags()->intercept_stat)
5818     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5819   int res = REAL(stat)(path, buf);
5820   if (!res)
5821     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5822   return res;
5824 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
5825 #else
5826 #define INIT_STAT
5827 #endif
5829 #if SANITIZER_INTERCEPT___XSTAT
5830 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
5831   void *ctx;
5832   COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
5833   if (common_flags()->intercept_stat)
5834     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5835   int res = REAL(__xstat)(version, path, buf);
5836   if (!res)
5837     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5838   return res;
5840 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
5841 #else
5842 #define INIT___XSTAT
5843 #endif
5845 #if SANITIZER_INTERCEPT___XSTAT64
5846 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
5847   void *ctx;
5848   COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
5849   if (common_flags()->intercept_stat)
5850     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5851   int res = REAL(__xstat64)(version, path, buf);
5852   if (!res)
5853     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5854   return res;
5856 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
5857 #else
5858 #define INIT___XSTAT64
5859 #endif
5861 #if SANITIZER_INTERCEPT___LXSTAT
5862 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
5863   void *ctx;
5864   COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
5865   if (common_flags()->intercept_stat)
5866     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5867   int res = REAL(__lxstat)(version, path, buf);
5868   if (!res)
5869     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5870   return res;
5872 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
5873 #else
5874 #define INIT___LXSTAT
5875 #endif
5877 #if SANITIZER_INTERCEPT___LXSTAT64
5878 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
5879   void *ctx;
5880   COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
5881   if (common_flags()->intercept_stat)
5882     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5883   int res = REAL(__lxstat64)(version, path, buf);
5884   if (!res)
5885     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5886   return res;
5888 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
5889 #else
5890 #define INIT___LXSTAT64
5891 #endif
5893 // FIXME: add other *stat interceptor
5895 static void InitializeCommonInterceptors() {
5896   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
5897   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
5899   INIT_TEXTDOMAIN;
5900   INIT_STRLEN;
5901   INIT_STRNLEN;
5902   INIT_STRCMP;
5903   INIT_STRNCMP;
5904   INIT_STRCASECMP;
5905   INIT_STRNCASECMP;
5906   INIT_STRSTR;
5907   INIT_STRCASESTR;
5908   INIT_STRCHR;
5909   INIT_STRCHRNUL;
5910   INIT_STRRCHR;
5911   INIT_STRSPN;
5912   INIT_STRPBRK;
5913   INIT_MEMSET;
5914   INIT_MEMMOVE;
5915   INIT_MEMCPY;
5916   INIT_MEMCHR;
5917   INIT_MEMCMP;
5918   INIT_MEMRCHR;
5919   INIT_MEMMEM;
5920   INIT_READ;
5921   INIT_PREAD;
5922   INIT_PREAD64;
5923   INIT_READV;
5924   INIT_PREADV;
5925   INIT_PREADV64;
5926   INIT_WRITE;
5927   INIT_PWRITE;
5928   INIT_PWRITE64;
5929   INIT_WRITEV;
5930   INIT_PWRITEV;
5931   INIT_PWRITEV64;
5932   INIT_PRCTL;
5933   INIT_LOCALTIME_AND_FRIENDS;
5934   INIT_STRPTIME;
5935   INIT_SCANF;
5936   INIT_ISOC99_SCANF;
5937   INIT_PRINTF;
5938   INIT_PRINTF_L;
5939   INIT_ISOC99_PRINTF;
5940   INIT_FREXP;
5941   INIT_FREXPF_FREXPL;
5942   INIT_GETPWNAM_AND_FRIENDS;
5943   INIT_GETPWNAM_R_AND_FRIENDS;
5944   INIT_GETPWENT;
5945   INIT_FGETPWENT;
5946   INIT_GETPWENT_R;
5947   INIT_SETPWENT;
5948   INIT_CLOCK_GETTIME;
5949   INIT_GETITIMER;
5950   INIT_TIME;
5951   INIT_GLOB;
5952   INIT_WAIT;
5953   INIT_WAIT4;
5954   INIT_INET;
5955   INIT_PTHREAD_GETSCHEDPARAM;
5956   INIT_GETADDRINFO;
5957   INIT_GETNAMEINFO;
5958   INIT_GETSOCKNAME;
5959   INIT_GETHOSTBYNAME;
5960   INIT_GETHOSTBYNAME_R;
5961   INIT_GETHOSTBYNAME2_R;
5962   INIT_GETHOSTBYADDR_R;
5963   INIT_GETHOSTENT_R;
5964   INIT_GETSOCKOPT;
5965   INIT_ACCEPT;
5966   INIT_ACCEPT4;
5967   INIT_MODF;
5968   INIT_RECVMSG;
5969   INIT_SENDMSG;
5970   INIT_GETPEERNAME;
5971   INIT_IOCTL;
5972   INIT_INET_ATON;
5973   INIT_SYSINFO;
5974   INIT_READDIR;
5975   INIT_READDIR64;
5976   INIT_PTRACE;
5977   INIT_SETLOCALE;
5978   INIT_GETCWD;
5979   INIT_GET_CURRENT_DIR_NAME;
5980   INIT_STRTOIMAX;
5981   INIT_MBSTOWCS;
5982   INIT_MBSNRTOWCS;
5983   INIT_WCSTOMBS;
5984   INIT_WCSNRTOMBS;
5985   INIT_WCRTOMB;
5986   INIT_TCGETATTR;
5987   INIT_REALPATH;
5988   INIT_CANONICALIZE_FILE_NAME;
5989   INIT_CONFSTR;
5990   INIT_SCHED_GETAFFINITY;
5991   INIT_SCHED_GETPARAM;
5992   INIT_STRERROR;
5993   INIT_STRERROR_R;
5994   INIT_XPG_STRERROR_R;
5995   INIT_SCANDIR;
5996   INIT_SCANDIR64;
5997   INIT_GETGROUPS;
5998   INIT_POLL;
5999   INIT_PPOLL;
6000   INIT_WORDEXP;
6001   INIT_SIGWAIT;
6002   INIT_SIGWAITINFO;
6003   INIT_SIGTIMEDWAIT;
6004   INIT_SIGSETOPS;
6005   INIT_SIGPENDING;
6006   INIT_SIGPROCMASK;
6007   INIT_BACKTRACE;
6008   INIT__EXIT;
6009   INIT_PTHREAD_MUTEX_LOCK;
6010   INIT_PTHREAD_MUTEX_UNLOCK;
6011   INIT_GETMNTENT;
6012   INIT_GETMNTENT_R;
6013   INIT_STATFS;
6014   INIT_STATFS64;
6015   INIT_STATVFS;
6016   INIT_STATVFS64;
6017   INIT_INITGROUPS;
6018   INIT_ETHER_NTOA_ATON;
6019   INIT_ETHER_HOST;
6020   INIT_ETHER_R;
6021   INIT_SHMCTL;
6022   INIT_RANDOM_R;
6023   INIT_PTHREAD_ATTR_GET;
6024   INIT_PTHREAD_ATTR_GETINHERITSCHED;
6025   INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6026   INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6027   INIT_PTHREAD_MUTEXATTR_GETTYPE;
6028   INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6029   INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6030   INIT_PTHREAD_MUTEXATTR_GETROBUST;
6031   INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6032   INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6033   INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6034   INIT_PTHREAD_CONDATTR_GETPSHARED;
6035   INIT_PTHREAD_CONDATTR_GETCLOCK;
6036   INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6037   INIT_TMPNAM;
6038   INIT_TMPNAM_R;
6039   INIT_TTYNAME_R;
6040   INIT_TEMPNAM;
6041   INIT_PTHREAD_SETNAME_NP;
6042   INIT_SINCOS;
6043   INIT_REMQUO;
6044   INIT_LGAMMA;
6045   INIT_LGAMMA_R;
6046   INIT_LGAMMAL_R;
6047   INIT_DRAND48_R;
6048   INIT_RAND_R;
6049   INIT_GETLINE;
6050   INIT_ICONV;
6051   INIT_TIMES;
6052   INIT_TLS_GET_ADDR;
6053   INIT_LISTXATTR;
6054   INIT_GETXATTR;
6055   INIT_GETRESID;
6056   INIT_GETIFADDRS;
6057   INIT_IF_INDEXTONAME;
6058   INIT_CAPGET;
6059   INIT_AEABI_MEM;
6060   INIT___BZERO;
6061   INIT_FTIME;
6062   INIT_XDR;
6063   INIT_TSEARCH;
6064   INIT_LIBIO_INTERNALS;
6065   INIT_FOPEN;
6066   INIT_FOPEN64;
6067   INIT_OPEN_MEMSTREAM;
6068   INIT_OBSTACK;
6069   INIT_FFLUSH;
6070   INIT_FCLOSE;
6071   INIT_DLOPEN_DLCLOSE;
6072   INIT_GETPASS;
6073   INIT_TIMERFD;
6074   INIT_MLOCKX;
6075   INIT_FOPENCOOKIE;
6076   INIT_SEM;
6077   INIT_PTHREAD_SETCANCEL;
6078   INIT_MINCORE;
6079   INIT_PROCESS_VM_READV;
6080   INIT_CTERMID;
6081   INIT_CTERMID_R;
6082   INIT_RECV_RECVFROM;
6083   INIT_SEND_SENDTO;
6084   INIT_STAT;
6085   INIT_EVENTFD_READ_WRITE;
6086   INIT___XSTAT;
6087   INIT___XSTAT64;
6088   INIT___LXSTAT;
6089   INIT___LXSTAT64;
6090   // FIXME: add other *stat interceptors.