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