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