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