]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Merge ^/head r318560 through r318657.
[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_placement_new.h"
44 #include "sanitizer_platform_interceptors.h"
45 #include "sanitizer_tls_get_addr.h"
46
47 #include <stdarg.h>
48
49 #if SANITIZER_INTERCEPTOR_HOOKS
50 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
51 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
52   SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
53 #else
54 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
55 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
56
57 #endif  // SANITIZER_INTERCEPTOR_HOOKS
58
59 #if SANITIZER_WINDOWS && !defined(va_copy)
60 #define va_copy(dst, src) ((dst) = (src))
61 #endif // _WIN32
62
63 #if SANITIZER_FREEBSD
64 #define pthread_setname_np pthread_set_name_np
65 #define inet_aton __inet_aton
66 #define inet_pton __inet_pton
67 #define iconv __bsd_iconv
68 #endif
69
70 // Platform-specific options.
71 #if SANITIZER_MAC
72 namespace __sanitizer {
73 bool PlatformHasDifferentMemcpyAndMemmove();
74 }
75 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
76   (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
77 #elif SANITIZER_WINDOWS64
78 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
79 #else
80 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
81 #endif  // SANITIZER_MAC
82
83 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
84 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
85 #endif
86
87 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
88 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
89 #endif
90
91 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
92 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
93 #endif
94
95 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
96 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
97 #endif
98
99 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
100 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
101 #endif
102
103 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
104 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
105 #endif
106
107 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
108 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
109 #endif
110
111 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
112 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
113 #endif
114
115 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
116 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
117 #endif
118
119 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
120 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
121 #endif
122
123 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
124 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
125 #endif
126
127 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
128 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
129 #endif
130
131 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
132 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
133 #endif
134
135 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
136 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
137   COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
138 #endif
139
140 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
141 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
142 #endif
143
144 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n)                   \
145     COMMON_INTERCEPTOR_READ_RANGE((ctx), (s),                       \
146       common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
147
148 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
149 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
150   CheckNoDeepBind(filename, flag);
151 #endif
152
153 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
154 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
155 #endif
156
157 #ifndef COMMON_INTERCEPTOR_ACQUIRE
158 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
159 #endif
160
161 #ifndef COMMON_INTERCEPTOR_RELEASE
162 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
163 #endif
164
165 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
166 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
167 #endif
168
169 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
170 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
171 #endif
172
173 #ifdef SANITIZER_NLDBL_VERSION
174 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
175     COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
176 #else
177 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn)                          \
178     COMMON_INTERCEPT_FUNCTION(fn)
179 #endif
180
181 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
182 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
183   {                                                       \
184     if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)        \
185       return internal_memset(dst, v, size);               \
186     COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size);  \
187     if (common_flags()->intercept_intrin)                 \
188       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);     \
189     return REAL(memset)(dst, v, size);                    \
190   }
191 #endif
192
193 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
194 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
195   {                                                          \
196     if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)           \
197       return internal_memmove(dst, src, size);               \
198     COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size);  \
199     if (common_flags()->intercept_intrin) {                  \
200       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);        \
201       COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);         \
202     }                                                        \
203     return REAL(memmove)(dst, src, size);                    \
204   }
205 #endif
206
207 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
208 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
209   {                                                         \
210     if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {        \
211       return internal_memmove(dst, src, size);              \
212     }                                                       \
213     COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size);  \
214     if (common_flags()->intercept_intrin) {                 \
215       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);       \
216       COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);        \
217     }                                                       \
218     return REAL(memcpy)(dst, src, size);                    \
219   }
220 #endif
221
222 #ifndef COMMON_INTERCEPTOR_COPY_STRING
223 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
224 #endif
225
226 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
227 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size)                          \
228   COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size);                             \
229   uptr from_length = internal_strnlen(s, size);                                \
230   uptr copy_length = Min(size, from_length);                                   \
231   char *new_mem = (char *)WRAP(malloc)(copy_length + 1);                       \
232   if (common_flags()->intercept_strndup) {                                     \
233     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, 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   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3400   return res;
3401 }
3402 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3403        //SANITIZER_MAC
3404 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3405 #else
3406 #define INIT_STRERROR_R
3407 #endif
3408
3409 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3410 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3411   void *ctx;
3412   COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3413   // FIXME: under ASan the call below may write to freed memory and corrupt
3414   // its metadata. See
3415   // https://github.com/google/sanitizers/issues/321.
3416   int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3417   // This version always returns a null-terminated string.
3418   if (buf && buflen)
3419     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3420   return res;
3421 }
3422 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3423 #else
3424 #define INIT_XPG_STRERROR_R
3425 #endif
3426
3427 #if SANITIZER_INTERCEPT_SCANDIR
3428 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3429 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3430                                 const struct __sanitizer_dirent **);
3431
3432 static THREADLOCAL scandir_filter_f scandir_filter;
3433 static THREADLOCAL scandir_compar_f scandir_compar;
3434
3435 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3436   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3437   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3438   return scandir_filter(dir);
3439 }
3440
3441 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3442                                   const struct __sanitizer_dirent **b) {
3443   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3444   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3445   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3446   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3447   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3448   return scandir_compar(a, b);
3449 }
3450
3451 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3452             scandir_filter_f filter, scandir_compar_f compar) {
3453   void *ctx;
3454   COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3455   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3456   scandir_filter = filter;
3457   scandir_compar = compar;
3458   // FIXME: under ASan the call below may write to freed memory and corrupt
3459   // its metadata. See
3460   // https://github.com/google/sanitizers/issues/321.
3461   int res = REAL(scandir)(dirp, namelist,
3462                           filter ? wrapped_scandir_filter : nullptr,
3463                           compar ? wrapped_scandir_compar : nullptr);
3464   scandir_filter = nullptr;
3465   scandir_compar = nullptr;
3466   if (namelist && res > 0) {
3467     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3468     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3469     for (int i = 0; i < res; ++i)
3470       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3471                                      (*namelist)[i]->d_reclen);
3472   }
3473   return res;
3474 }
3475 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3476 #else
3477 #define INIT_SCANDIR
3478 #endif
3479
3480 #if SANITIZER_INTERCEPT_SCANDIR64
3481 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3482 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3483                                   const struct __sanitizer_dirent64 **);
3484
3485 static THREADLOCAL scandir64_filter_f scandir64_filter;
3486 static THREADLOCAL scandir64_compar_f scandir64_compar;
3487
3488 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3489   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3490   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3491   return scandir64_filter(dir);
3492 }
3493
3494 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3495                                     const struct __sanitizer_dirent64 **b) {
3496   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3497   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3498   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3499   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3500   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3501   return scandir64_compar(a, b);
3502 }
3503
3504 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3505             scandir64_filter_f filter, scandir64_compar_f compar) {
3506   void *ctx;
3507   COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3508   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3509   scandir64_filter = filter;
3510   scandir64_compar = compar;
3511   // FIXME: under ASan the call below may write to freed memory and corrupt
3512   // its metadata. See
3513   // https://github.com/google/sanitizers/issues/321.
3514   int res =
3515       REAL(scandir64)(dirp, namelist,
3516                       filter ? wrapped_scandir64_filter : nullptr,
3517                       compar ? wrapped_scandir64_compar : nullptr);
3518   scandir64_filter = nullptr;
3519   scandir64_compar = nullptr;
3520   if (namelist && res > 0) {
3521     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3522     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3523     for (int i = 0; i < res; ++i)
3524       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3525                                      (*namelist)[i]->d_reclen);
3526   }
3527   return res;
3528 }
3529 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3530 #else
3531 #define INIT_SCANDIR64
3532 #endif
3533
3534 #if SANITIZER_INTERCEPT_GETGROUPS
3535 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3536   void *ctx;
3537   COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3538   // FIXME: under ASan the call below may write to freed memory and corrupt
3539   // its metadata. See
3540   // https://github.com/google/sanitizers/issues/321.
3541   int res = REAL(getgroups)(size, lst);
3542   if (res >= 0 && lst && size > 0)
3543     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3544   return res;
3545 }
3546 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3547 #else
3548 #define INIT_GETGROUPS
3549 #endif
3550
3551 #if SANITIZER_INTERCEPT_POLL
3552 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3553                         __sanitizer_nfds_t nfds) {
3554   for (unsigned i = 0; i < nfds; ++i) {
3555     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3556     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3557   }
3558 }
3559
3560 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3561                          __sanitizer_nfds_t nfds) {
3562   for (unsigned i = 0; i < nfds; ++i)
3563     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3564                                    sizeof(fds[i].revents));
3565 }
3566
3567 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3568             int timeout) {
3569   void *ctx;
3570   COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3571   if (fds && nfds) read_pollfd(ctx, fds, nfds);
3572   int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3573   if (fds && nfds) write_pollfd(ctx, fds, nfds);
3574   return res;
3575 }
3576 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3577 #else
3578 #define INIT_POLL
3579 #endif
3580
3581 #if SANITIZER_INTERCEPT_PPOLL
3582 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3583             void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3584   void *ctx;
3585   COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3586   if (fds && nfds) read_pollfd(ctx, fds, nfds);
3587   if (timeout_ts)
3588     COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3589   // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3590   int res =
3591       COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3592   if (fds && nfds) write_pollfd(ctx, fds, nfds);
3593   return res;
3594 }
3595 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3596 #else
3597 #define INIT_PPOLL
3598 #endif
3599
3600 #if SANITIZER_INTERCEPT_WORDEXP
3601 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3602   void *ctx;
3603   COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3604   if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3605   // FIXME: under ASan the call below may write to freed memory and corrupt
3606   // its metadata. See
3607   // https://github.com/google/sanitizers/issues/321.
3608   int res = REAL(wordexp)(s, p, flags);
3609   if (!res && p) {
3610     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3611     if (p->we_wordc)
3612       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3613                                      sizeof(*p->we_wordv) * p->we_wordc);
3614     for (uptr i = 0; i < p->we_wordc; ++i) {
3615       char *w = p->we_wordv[i];
3616       if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3617     }
3618   }
3619   return res;
3620 }
3621 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3622 #else
3623 #define INIT_WORDEXP
3624 #endif
3625
3626 #if SANITIZER_INTERCEPT_SIGWAIT
3627 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3628   void *ctx;
3629   COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3630   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3631   // FIXME: under ASan the call below may write to freed memory and corrupt
3632   // its metadata. See
3633   // https://github.com/google/sanitizers/issues/321.
3634   int res = REAL(sigwait)(set, sig);
3635   if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3636   return res;
3637 }
3638 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3639 #else
3640 #define INIT_SIGWAIT
3641 #endif
3642
3643 #if SANITIZER_INTERCEPT_SIGWAITINFO
3644 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3645   void *ctx;
3646   COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3647   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3648   // FIXME: under ASan the call below may write to freed memory and corrupt
3649   // its metadata. See
3650   // https://github.com/google/sanitizers/issues/321.
3651   int res = REAL(sigwaitinfo)(set, info);
3652   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3653   return res;
3654 }
3655 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3656 #else
3657 #define INIT_SIGWAITINFO
3658 #endif
3659
3660 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3661 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3662             void *timeout) {
3663   void *ctx;
3664   COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3665   if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3666   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3667   // FIXME: under ASan the call below may write to freed memory and corrupt
3668   // its metadata. See
3669   // https://github.com/google/sanitizers/issues/321.
3670   int res = REAL(sigtimedwait)(set, info, timeout);
3671   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3672   return res;
3673 }
3674 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3675 #else
3676 #define INIT_SIGTIMEDWAIT
3677 #endif
3678
3679 #if SANITIZER_INTERCEPT_SIGSETOPS
3680 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3681   void *ctx;
3682   COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3683   // FIXME: under ASan the call below may write to freed memory and corrupt
3684   // its metadata. See
3685   // https://github.com/google/sanitizers/issues/321.
3686   int res = REAL(sigemptyset)(set);
3687   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3688   return res;
3689 }
3690
3691 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3692   void *ctx;
3693   COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3694   // FIXME: under ASan the call below may write to freed memory and corrupt
3695   // its metadata. See
3696   // https://github.com/google/sanitizers/issues/321.
3697   int res = REAL(sigfillset)(set);
3698   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3699   return res;
3700 }
3701 #define INIT_SIGSETOPS                    \
3702   COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3703   COMMON_INTERCEPT_FUNCTION(sigfillset);
3704 #else
3705 #define INIT_SIGSETOPS
3706 #endif
3707
3708 #if SANITIZER_INTERCEPT_SIGPENDING
3709 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3710   void *ctx;
3711   COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3712   // FIXME: under ASan the call below may write to freed memory and corrupt
3713   // its metadata. See
3714   // https://github.com/google/sanitizers/issues/321.
3715   int res = REAL(sigpending)(set);
3716   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3717   return res;
3718 }
3719 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3720 #else
3721 #define INIT_SIGPENDING
3722 #endif
3723
3724 #if SANITIZER_INTERCEPT_SIGPROCMASK
3725 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3726             __sanitizer_sigset_t *oldset) {
3727   void *ctx;
3728   COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3729   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3730   // FIXME: under ASan the call below may write to freed memory and corrupt
3731   // its metadata. See
3732   // https://github.com/google/sanitizers/issues/321.
3733   int res = REAL(sigprocmask)(how, set, oldset);
3734   if (!res && oldset)
3735     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3736   return res;
3737 }
3738 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3739 #else
3740 #define INIT_SIGPROCMASK
3741 #endif
3742
3743 #if SANITIZER_INTERCEPT_BACKTRACE
3744 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3745   void *ctx;
3746   COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3747   // FIXME: under ASan the call below may write to freed memory and corrupt
3748   // its metadata. See
3749   // https://github.com/google/sanitizers/issues/321.
3750   int res = REAL(backtrace)(buffer, size);
3751   if (res && buffer)
3752     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3753   return res;
3754 }
3755
3756 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3757   void *ctx;
3758   COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3759   if (buffer && size)
3760     COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3761   // FIXME: under ASan the call below may write to freed memory and corrupt
3762   // its metadata. See
3763   // https://github.com/google/sanitizers/issues/321.
3764   char **res = REAL(backtrace_symbols)(buffer, size);
3765   if (res && size) {
3766     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3767     for (int i = 0; i < size; ++i)
3768       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3769   }
3770   return res;
3771 }
3772 #define INIT_BACKTRACE                  \
3773   COMMON_INTERCEPT_FUNCTION(backtrace); \
3774   COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3775 #else
3776 #define INIT_BACKTRACE
3777 #endif
3778
3779 #if SANITIZER_INTERCEPT__EXIT
3780 INTERCEPTOR(void, _exit, int status) {
3781   void *ctx;
3782   COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3783   COMMON_INTERCEPTOR_USER_CALLBACK_START();
3784   int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3785   COMMON_INTERCEPTOR_USER_CALLBACK_END();
3786   if (status == 0) status = status1;
3787   REAL(_exit)(status);
3788 }
3789 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3790 #else
3791 #define INIT__EXIT
3792 #endif
3793
3794 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3795 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3796   void *ctx;
3797   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3798   COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
3799   int res = REAL(pthread_mutex_lock)(m);
3800   if (res == errno_EOWNERDEAD)
3801     COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3802   if (res == 0 || res == errno_EOWNERDEAD)
3803     COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
3804   if (res == errno_EINVAL)
3805     COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3806   return res;
3807 }
3808
3809 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3810   void *ctx;
3811   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3812   COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3813   int res = REAL(pthread_mutex_unlock)(m);
3814   if (res == errno_EINVAL)
3815     COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3816   return res;
3817 }
3818
3819 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3820 #define INIT_PTHREAD_MUTEX_UNLOCK \
3821   COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3822 #else
3823 #define INIT_PTHREAD_MUTEX_LOCK
3824 #define INIT_PTHREAD_MUTEX_UNLOCK
3825 #endif
3826
3827 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3828 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3829   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3830   if (mnt->mnt_fsname)
3831     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3832                                    REAL(strlen)(mnt->mnt_fsname) + 1);
3833   if (mnt->mnt_dir)
3834     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3835                                    REAL(strlen)(mnt->mnt_dir) + 1);
3836   if (mnt->mnt_type)
3837     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3838                                    REAL(strlen)(mnt->mnt_type) + 1);
3839   if (mnt->mnt_opts)
3840     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3841                                    REAL(strlen)(mnt->mnt_opts) + 1);
3842 }
3843 #endif
3844
3845 #if SANITIZER_INTERCEPT_GETMNTENT
3846 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3847   void *ctx;
3848   COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3849   __sanitizer_mntent *res = REAL(getmntent)(fp);
3850   if (res) write_mntent(ctx, res);
3851   return res;
3852 }
3853 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3854 #else
3855 #define INIT_GETMNTENT
3856 #endif
3857
3858 #if SANITIZER_INTERCEPT_GETMNTENT_R
3859 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3860             __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3861   void *ctx;
3862   COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3863   __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3864   if (res) write_mntent(ctx, res);
3865   return res;
3866 }
3867 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3868 #else
3869 #define INIT_GETMNTENT_R
3870 #endif
3871
3872 #if SANITIZER_INTERCEPT_STATFS
3873 INTERCEPTOR(int, statfs, char *path, void *buf) {
3874   void *ctx;
3875   COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3876   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3877   // FIXME: under ASan the call below may write to freed memory and corrupt
3878   // its metadata. See
3879   // https://github.com/google/sanitizers/issues/321.
3880   int res = REAL(statfs)(path, buf);
3881   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3882   return res;
3883 }
3884 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3885   void *ctx;
3886   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3887   // FIXME: under ASan the call below may write to freed memory and corrupt
3888   // its metadata. See
3889   // https://github.com/google/sanitizers/issues/321.
3890   int res = REAL(fstatfs)(fd, buf);
3891   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3892   return res;
3893 }
3894 #define INIT_STATFS                  \
3895   COMMON_INTERCEPT_FUNCTION(statfs); \
3896   COMMON_INTERCEPT_FUNCTION(fstatfs);
3897 #else
3898 #define INIT_STATFS
3899 #endif
3900
3901 #if SANITIZER_INTERCEPT_STATFS64
3902 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3903   void *ctx;
3904   COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3905   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3906   // FIXME: under ASan the call below may write to freed memory and corrupt
3907   // its metadata. See
3908   // https://github.com/google/sanitizers/issues/321.
3909   int res = REAL(statfs64)(path, buf);
3910   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3911   return res;
3912 }
3913 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3914   void *ctx;
3915   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3916   // FIXME: under ASan the call below may write to freed memory and corrupt
3917   // its metadata. See
3918   // https://github.com/google/sanitizers/issues/321.
3919   int res = REAL(fstatfs64)(fd, buf);
3920   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3921   return res;
3922 }
3923 #define INIT_STATFS64                  \
3924   COMMON_INTERCEPT_FUNCTION(statfs64); \
3925   COMMON_INTERCEPT_FUNCTION(fstatfs64);
3926 #else
3927 #define INIT_STATFS64
3928 #endif
3929
3930 #if SANITIZER_INTERCEPT_STATVFS
3931 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3932   void *ctx;
3933   COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3934   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3935   // FIXME: under ASan the call below may write to freed memory and corrupt
3936   // its metadata. See
3937   // https://github.com/google/sanitizers/issues/321.
3938   int res = REAL(statvfs)(path, buf);
3939   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3940   return res;
3941 }
3942 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3943   void *ctx;
3944   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3945   // FIXME: under ASan the call below may write to freed memory and corrupt
3946   // its metadata. See
3947   // https://github.com/google/sanitizers/issues/321.
3948   int res = REAL(fstatvfs)(fd, buf);
3949   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3950   return res;
3951 }
3952 #define INIT_STATVFS                  \
3953   COMMON_INTERCEPT_FUNCTION(statvfs); \
3954   COMMON_INTERCEPT_FUNCTION(fstatvfs);
3955 #else
3956 #define INIT_STATVFS
3957 #endif
3958
3959 #if SANITIZER_INTERCEPT_STATVFS64
3960 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3961   void *ctx;
3962   COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3963   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3964   // FIXME: under ASan the call below may write to freed memory and corrupt
3965   // its metadata. See
3966   // https://github.com/google/sanitizers/issues/321.
3967   int res = REAL(statvfs64)(path, buf);
3968   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3969   return res;
3970 }
3971 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3972   void *ctx;
3973   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3974   // FIXME: under ASan the call below may write to freed memory and corrupt
3975   // its metadata. See
3976   // https://github.com/google/sanitizers/issues/321.
3977   int res = REAL(fstatvfs64)(fd, buf);
3978   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3979   return res;
3980 }
3981 #define INIT_STATVFS64                  \
3982   COMMON_INTERCEPT_FUNCTION(statvfs64); \
3983   COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3984 #else
3985 #define INIT_STATVFS64
3986 #endif
3987
3988 #if SANITIZER_INTERCEPT_INITGROUPS
3989 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3990   void *ctx;
3991   COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3992   if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3993   int res = REAL(initgroups)(user, group);
3994   return res;
3995 }
3996 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3997 #else
3998 #define INIT_INITGROUPS
3999 #endif
4000
4001 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4002 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4003   void *ctx;
4004   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4005   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4006   char *res = REAL(ether_ntoa)(addr);
4007   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4008   return res;
4009 }
4010 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4011   void *ctx;
4012   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4013   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4014   __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4015   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4016   return res;
4017 }
4018 #define INIT_ETHER_NTOA_ATON             \
4019   COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4020   COMMON_INTERCEPT_FUNCTION(ether_aton);
4021 #else
4022 #define INIT_ETHER_NTOA_ATON
4023 #endif
4024
4025 #if SANITIZER_INTERCEPT_ETHER_HOST
4026 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4027   void *ctx;
4028   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4029   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4030   // FIXME: under ASan the call below may write to freed memory and corrupt
4031   // its metadata. See
4032   // https://github.com/google/sanitizers/issues/321.
4033   int res = REAL(ether_ntohost)(hostname, addr);
4034   if (!res && hostname)
4035     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4036   return res;
4037 }
4038 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4039   void *ctx;
4040   COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4041   if (hostname)
4042     COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4043   // FIXME: under ASan the call below may write to freed memory and corrupt
4044   // its metadata. See
4045   // https://github.com/google/sanitizers/issues/321.
4046   int res = REAL(ether_hostton)(hostname, addr);
4047   if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4048   return res;
4049 }
4050 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4051             char *hostname) {
4052   void *ctx;
4053   COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4054   if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4055   // FIXME: under ASan the call below may write to freed memory and corrupt
4056   // its metadata. See
4057   // https://github.com/google/sanitizers/issues/321.
4058   int res = REAL(ether_line)(line, addr, hostname);
4059   if (!res) {
4060     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4061     if (hostname)
4062       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4063   }
4064   return res;
4065 }
4066 #define INIT_ETHER_HOST                     \
4067   COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4068   COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4069   COMMON_INTERCEPT_FUNCTION(ether_line);
4070 #else
4071 #define INIT_ETHER_HOST
4072 #endif
4073
4074 #if SANITIZER_INTERCEPT_ETHER_R
4075 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4076   void *ctx;
4077   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4078   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4079   // FIXME: under ASan the call below may write to freed memory and corrupt
4080   // its metadata. See
4081   // https://github.com/google/sanitizers/issues/321.
4082   char *res = REAL(ether_ntoa_r)(addr, buf);
4083   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4084   return res;
4085 }
4086 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4087             __sanitizer_ether_addr *addr) {
4088   void *ctx;
4089   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4090   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4091   // FIXME: under ASan the call below may write to freed memory and corrupt
4092   // its metadata. See
4093   // https://github.com/google/sanitizers/issues/321.
4094   __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4095   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4096   return res;
4097 }
4098 #define INIT_ETHER_R                       \
4099   COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4100   COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4101 #else
4102 #define INIT_ETHER_R
4103 #endif
4104
4105 #if SANITIZER_INTERCEPT_SHMCTL
4106 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4107   void *ctx;
4108   COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4109   // FIXME: under ASan the call below may write to freed memory and corrupt
4110   // its metadata. See
4111   // https://github.com/google/sanitizers/issues/321.
4112   int res = REAL(shmctl)(shmid, cmd, buf);
4113   if (res >= 0) {
4114     unsigned sz = 0;
4115     if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4116       sz = sizeof(__sanitizer_shmid_ds);
4117     else if (cmd == shmctl_ipc_info)
4118       sz = struct_shminfo_sz;
4119     else if (cmd == shmctl_shm_info)
4120       sz = struct_shm_info_sz;
4121     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4122   }
4123   return res;
4124 }
4125 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4126 #else
4127 #define INIT_SHMCTL
4128 #endif
4129
4130 #if SANITIZER_INTERCEPT_RANDOM_R
4131 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4132   void *ctx;
4133   COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4134   // FIXME: under ASan the call below may write to freed memory and corrupt
4135   // its metadata. See
4136   // https://github.com/google/sanitizers/issues/321.
4137   int res = REAL(random_r)(buf, result);
4138   if (!res && result)
4139     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4140   return res;
4141 }
4142 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4143 #else
4144 #define INIT_RANDOM_R
4145 #endif
4146
4147 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4148 // its metadata. See
4149 // https://github.com/google/sanitizers/issues/321.
4150 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET ||              \
4151     SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4152     SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET ||         \
4153     SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET ||        \
4154     SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET ||          \
4155     SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4156 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz)            \
4157   INTERCEPTOR(int, fn, void *attr, void *r) {                  \
4158     void *ctx;                                                 \
4159     COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r);                \
4160     int res = REAL(fn)(attr, r);                               \
4161     if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4162     return res;                                                \
4163   }
4164 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4165   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4166 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4167   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4168 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4169   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4170 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4171   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4172 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4173   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4174 #endif
4175
4176 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4177 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4178 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4179 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4180 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4181 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4182 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4183 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4184   void *ctx;
4185   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4186   // FIXME: under ASan the call below may write to freed memory and corrupt
4187   // its metadata. See
4188   // https://github.com/google/sanitizers/issues/321.
4189   int res = REAL(pthread_attr_getstack)(attr, addr, size);
4190   if (!res) {
4191     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4192     if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4193   }
4194   return res;
4195 }
4196
4197 // We may need to call the real pthread_attr_getstack from the run-time
4198 // in sanitizer_common, but we don't want to include the interception headers
4199 // there. So, just define this function here.
4200 namespace __sanitizer {
4201 extern "C" {
4202 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4203   return REAL(pthread_attr_getstack)(attr, addr, size);
4204 }
4205 }  // extern "C"
4206 }  // namespace __sanitizer
4207
4208 #define INIT_PTHREAD_ATTR_GET                             \
4209   COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4210   COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize);   \
4211   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam);  \
4212   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4213   COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope);       \
4214   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize);   \
4215   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4216 #else
4217 #define INIT_PTHREAD_ATTR_GET
4218 #endif
4219
4220 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4221 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4222
4223 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4224   COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4225 #else
4226 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4227 #endif
4228
4229 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4230 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4231             void *cpuset) {
4232   void *ctx;
4233   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4234                            cpuset);
4235   // FIXME: under ASan the call below may write to freed memory and corrupt
4236   // its metadata. See
4237   // https://github.com/google/sanitizers/issues/321.
4238   int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4239   if (!res && cpusetsize && cpuset)
4240     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4241   return res;
4242 }
4243
4244 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4245   COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4246 #else
4247 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4248 #endif
4249
4250 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4251 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4252 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4253   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4254 #else
4255 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4256 #endif
4257
4258 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4259 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4260 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4261   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4262 #else
4263 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4264 #endif
4265
4266 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4267 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4268 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4269   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4270 #else
4271 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4272 #endif
4273
4274 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4275 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4276 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4277   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4278 #else
4279 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4280 #endif
4281
4282 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4283 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4284 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4285   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4286 #else
4287 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4288 #endif
4289
4290 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4291 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4292 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4293   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4294 #else
4295 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4296 #endif
4297
4298 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4299 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4300 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4301   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4302 #else
4303 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4304 #endif
4305
4306 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4307 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4308 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4309   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4310 #else
4311 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4312 #endif
4313
4314 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4315 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4316 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4317   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4318 #else
4319 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4320 #endif
4321
4322 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4323 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4324 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4325   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4326 #else
4327 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4328 #endif
4329
4330 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4331 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4332 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4333   COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4334 #else
4335 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4336 #endif
4337
4338 #if SANITIZER_INTERCEPT_TMPNAM
4339 INTERCEPTOR(char *, tmpnam, char *s) {
4340   void *ctx;
4341   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4342   char *res = REAL(tmpnam)(s);
4343   if (res) {
4344     if (s)
4345       // FIXME: under ASan the call below may write to freed memory and corrupt
4346       // its metadata. See
4347       // https://github.com/google/sanitizers/issues/321.
4348       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4349     else
4350       COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4351   }
4352   return res;
4353 }
4354 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4355 #else
4356 #define INIT_TMPNAM
4357 #endif
4358
4359 #if SANITIZER_INTERCEPT_TMPNAM_R
4360 INTERCEPTOR(char *, tmpnam_r, char *s) {
4361   void *ctx;
4362   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4363   // FIXME: under ASan the call below may write to freed memory and corrupt
4364   // its metadata. See
4365   // https://github.com/google/sanitizers/issues/321.
4366   char *res = REAL(tmpnam_r)(s);
4367   if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4368   return res;
4369 }
4370 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4371 #else
4372 #define INIT_TMPNAM_R
4373 #endif
4374
4375 #if SANITIZER_INTERCEPT_TTYNAME_R
4376 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4377   void *ctx;
4378   COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4379   int res = REAL(ttyname_r)(fd, name, namesize);
4380   if (res == 0)
4381     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4382   return res;
4383 }
4384 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4385 #else
4386 #define INIT_TTYNAME_R
4387 #endif
4388
4389 #if SANITIZER_INTERCEPT_TEMPNAM
4390 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4391   void *ctx;
4392   COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4393   if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4394   if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4395   char *res = REAL(tempnam)(dir, pfx);
4396   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4397   return res;
4398 }
4399 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4400 #else
4401 #define INIT_TEMPNAM
4402 #endif
4403
4404 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4405 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4406   void *ctx;
4407   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4408   COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4409   COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4410   return REAL(pthread_setname_np)(thread, name);
4411 }
4412 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4413 #else
4414 #define INIT_PTHREAD_SETNAME_NP
4415 #endif
4416
4417 #if SANITIZER_INTERCEPT_SINCOS
4418 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4419   void *ctx;
4420   COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4421   // FIXME: under ASan the call below may write to freed memory and corrupt
4422   // its metadata. See
4423   // https://github.com/google/sanitizers/issues/321.
4424   REAL(sincos)(x, sin, cos);
4425   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4426   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4427 }
4428 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4429   void *ctx;
4430   COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4431   // FIXME: under ASan the call below may write to freed memory and corrupt
4432   // its metadata. See
4433   // https://github.com/google/sanitizers/issues/321.
4434   REAL(sincosf)(x, sin, cos);
4435   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4436   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4437 }
4438 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4439   void *ctx;
4440   COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4441   // FIXME: under ASan the call below may write to freed memory and corrupt
4442   // its metadata. See
4443   // https://github.com/google/sanitizers/issues/321.
4444   REAL(sincosl)(x, sin, cos);
4445   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4446   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4447 }
4448 #define INIT_SINCOS                   \
4449   COMMON_INTERCEPT_FUNCTION(sincos);  \
4450   COMMON_INTERCEPT_FUNCTION(sincosf); \
4451   COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4452 #else
4453 #define INIT_SINCOS
4454 #endif
4455
4456 #if SANITIZER_INTERCEPT_REMQUO
4457 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4458   void *ctx;
4459   COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4460   // FIXME: under ASan the call below may write to freed memory and corrupt
4461   // its metadata. See
4462   // https://github.com/google/sanitizers/issues/321.
4463   double res = REAL(remquo)(x, y, quo);
4464   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4465   return res;
4466 }
4467 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4468   void *ctx;
4469   COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4470   // FIXME: under ASan the call below may write to freed memory and corrupt
4471   // its metadata. See
4472   // https://github.com/google/sanitizers/issues/321.
4473   float res = REAL(remquof)(x, y, quo);
4474   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4475   return res;
4476 }
4477 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4478   void *ctx;
4479   COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4480   // FIXME: under ASan the call below may write to freed memory and corrupt
4481   // its metadata. See
4482   // https://github.com/google/sanitizers/issues/321.
4483   long double res = REAL(remquol)(x, y, quo);
4484   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4485   return res;
4486 }
4487 #define INIT_REMQUO                   \
4488   COMMON_INTERCEPT_FUNCTION(remquo);  \
4489   COMMON_INTERCEPT_FUNCTION(remquof); \
4490   COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4491 #else
4492 #define INIT_REMQUO
4493 #endif
4494
4495 #if SANITIZER_INTERCEPT_LGAMMA
4496 extern int signgam;
4497 INTERCEPTOR(double, lgamma, double x) {
4498   void *ctx;
4499   COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4500   double res = REAL(lgamma)(x);
4501   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4502   return res;
4503 }
4504 INTERCEPTOR(float, lgammaf, float x) {
4505   void *ctx;
4506   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4507   float res = REAL(lgammaf)(x);
4508   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4509   return res;
4510 }
4511 INTERCEPTOR(long double, lgammal, long double x) {
4512   void *ctx;
4513   COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4514   long double res = REAL(lgammal)(x);
4515   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4516   return res;
4517 }
4518 #define INIT_LGAMMA                   \
4519   COMMON_INTERCEPT_FUNCTION(lgamma);  \
4520   COMMON_INTERCEPT_FUNCTION(lgammaf); \
4521   COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4522 #else
4523 #define INIT_LGAMMA
4524 #endif
4525
4526 #if SANITIZER_INTERCEPT_LGAMMA_R
4527 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4528   void *ctx;
4529   COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4530   // FIXME: under ASan the call below may write to freed memory and corrupt
4531   // its metadata. See
4532   // https://github.com/google/sanitizers/issues/321.
4533   double res = REAL(lgamma_r)(x, signp);
4534   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4535   return res;
4536 }
4537 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4538   void *ctx;
4539   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4540   // FIXME: under ASan the call below may write to freed memory and corrupt
4541   // its metadata. See
4542   // https://github.com/google/sanitizers/issues/321.
4543   float res = REAL(lgammaf_r)(x, signp);
4544   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4545   return res;
4546 }
4547 #define INIT_LGAMMA_R                   \
4548   COMMON_INTERCEPT_FUNCTION(lgamma_r);  \
4549   COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4550 #else
4551 #define INIT_LGAMMA_R
4552 #endif
4553
4554 #if SANITIZER_INTERCEPT_LGAMMAL_R
4555 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4556   void *ctx;
4557   COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4558   // FIXME: under ASan the call below may write to freed memory and corrupt
4559   // its metadata. See
4560   // https://github.com/google/sanitizers/issues/321.
4561   long double res = REAL(lgammal_r)(x, signp);
4562   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4563   return res;
4564 }
4565 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4566 #else
4567 #define INIT_LGAMMAL_R
4568 #endif
4569
4570 #if SANITIZER_INTERCEPT_DRAND48_R
4571 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4572   void *ctx;
4573   COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4574   // FIXME: under ASan the call below may write to freed memory and corrupt
4575   // its metadata. See
4576   // https://github.com/google/sanitizers/issues/321.
4577   int res = REAL(drand48_r)(buffer, result);
4578   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4579   return res;
4580 }
4581 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4582   void *ctx;
4583   COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4584   // FIXME: under ASan the call below may write to freed memory and corrupt
4585   // its metadata. See
4586   // https://github.com/google/sanitizers/issues/321.
4587   int res = REAL(lrand48_r)(buffer, result);
4588   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4589   return res;
4590 }
4591 #define INIT_DRAND48_R                  \
4592   COMMON_INTERCEPT_FUNCTION(drand48_r); \
4593   COMMON_INTERCEPT_FUNCTION(lrand48_r);
4594 #else
4595 #define INIT_DRAND48_R
4596 #endif
4597
4598 #if SANITIZER_INTERCEPT_RAND_R
4599 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4600   void *ctx;
4601   COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4602   COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4603   return REAL(rand_r)(seedp);
4604 }
4605 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4606 #else
4607 #define INIT_RAND_R
4608 #endif
4609
4610 #if SANITIZER_INTERCEPT_GETLINE
4611 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4612   void *ctx;
4613   COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4614   // FIXME: under ASan the call below may write to freed memory and corrupt
4615   // its metadata. See
4616   // https://github.com/google/sanitizers/issues/321.
4617   SSIZE_T res = REAL(getline)(lineptr, n, stream);
4618   if (res > 0) {
4619     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4620     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4621     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4622   }
4623   return res;
4624 }
4625
4626 // FIXME: under ASan the call below may write to freed memory and corrupt its
4627 // metadata. See
4628 // https://github.com/google/sanitizers/issues/321.
4629 #define GETDELIM_INTERCEPTOR_IMPL(vname)                                       \
4630   {                                                                            \
4631     void *ctx;                                                                 \
4632     COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream);           \
4633     SSIZE_T res = REAL(vname)(lineptr, n, delim, stream);                      \
4634     if (res > 0) {                                                             \
4635       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));          \
4636       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));                      \
4637       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);                  \
4638     }                                                                          \
4639     return res;                                                                \
4640   }
4641
4642 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4643             void *stream)
4644 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4645
4646 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4647 // with its own body.
4648 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4649             void *stream)
4650 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4651
4652 #define INIT_GETLINE                     \
4653   COMMON_INTERCEPT_FUNCTION(getline);    \
4654   COMMON_INTERCEPT_FUNCTION(__getdelim); \
4655   COMMON_INTERCEPT_FUNCTION(getdelim);
4656 #else
4657 #define INIT_GETLINE
4658 #endif
4659
4660 #if SANITIZER_INTERCEPT_ICONV
4661 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4662             char **outbuf, SIZE_T *outbytesleft) {
4663   void *ctx;
4664   COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4665                            outbytesleft);
4666   if (inbytesleft)
4667     COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4668   if (inbuf && inbytesleft)
4669     COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4670   if (outbytesleft)
4671     COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4672   void *outbuf_orig = outbuf ? *outbuf : nullptr;
4673   // FIXME: under ASan the call below may write to freed memory and corrupt
4674   // its metadata. See
4675   // https://github.com/google/sanitizers/issues/321.
4676   SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4677   if (outbuf && *outbuf > outbuf_orig) {
4678     SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4679     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4680   }
4681   return res;
4682 }
4683 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4684 #else
4685 #define INIT_ICONV
4686 #endif
4687
4688 #if SANITIZER_INTERCEPT_TIMES
4689 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4690   void *ctx;
4691   COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4692   // FIXME: under ASan the call below may write to freed memory and corrupt
4693   // its metadata. See
4694   // https://github.com/google/sanitizers/issues/321.
4695   __sanitizer_clock_t res = REAL(times)(tms);
4696   if (res != (__sanitizer_clock_t)-1 && tms)
4697     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4698   return res;
4699 }
4700 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4701 #else
4702 #define INIT_TIMES
4703 #endif
4704
4705 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4706 #if !SANITIZER_S390
4707 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4708 // If you see any crashes around this functions, there are 2 known issues with
4709 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4710 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4711 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4712 // to access thread local variables (it should not happen normally,
4713 // because sanitizers use initial-exec tls model).
4714 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4715   void *ctx;
4716   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4717   void *res = REAL(__tls_get_addr)(arg);
4718   uptr tls_begin, tls_end;
4719   COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4720   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4721   if (dtv) {
4722     // New DTLS block has been allocated.
4723     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4724   }
4725   return res;
4726 }
4727 #if SANITIZER_PPC
4728 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4729 // mostly the same semantics as __tls_get_addr, but its presence enables
4730 // some optimizations in linker (which are safe to ignore here).
4731 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4732                           visibility("default")))
4733 void *__tls_get_addr_opt(void *arg);
4734 #endif
4735 #else // SANITIZER_S390
4736 // On s390, we have to intercept two functions here:
4737 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4738 //   the usual __tls_get_addr, but returns a TP-relative offset instead of
4739 //   a proper pointer.  It is used by dlsym for TLS symbols.
4740 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4741 //   descriptor offset as an argument instead of a pointer.  GOT address
4742 //   is passed in r12, so it's necessary to write it in assembly.  This is
4743 //   the function used by the compiler.
4744 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
4745 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
4746 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
4747 extern "C" uptr __tls_get_offset(void *arg);
4748 extern "C" uptr __interceptor___tls_get_offset(void *arg);
4749 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4750   void *ctx;
4751   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4752   uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
4753   uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4754   void *ptr = reinterpret_cast<void *>(res + tp);
4755   uptr tls_begin, tls_end;
4756   COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4757   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4758   if (dtv) {
4759     // New DTLS block has been allocated.
4760     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4761   }
4762   return res;
4763 }
4764 // We need a hidden symbol aliasing the above, so that we can jump
4765 // directly to it from the assembly below.
4766 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4767                           visibility("hidden")))
4768 uptr __tls_get_addr_hidden(void *arg);
4769 // Now carefully intercept __tls_get_offset.
4770 asm(
4771   ".text\n"
4772 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4773 // exports our symbol.
4774   ".weak __tls_get_offset\n"
4775   ".type __tls_get_offset, @function\n"
4776   "__tls_get_offset:\n"
4777   ".global __interceptor___tls_get_offset\n"
4778   ".type __interceptor___tls_get_offset, @function\n"
4779   "__interceptor___tls_get_offset:\n"
4780 #ifdef __s390x__
4781   "la %r2, 0(%r2,%r12)\n"
4782   "jg __tls_get_addr_hidden\n"
4783 #else
4784   "basr %r3,0\n"
4785   "0: la %r2,0(%r2,%r12)\n"
4786   "l %r4,1f-0b(%r3)\n"
4787   "b 0(%r4,%r3)\n"
4788   "1: .long __tls_get_addr_hidden - 0b\n"
4789 #endif
4790   ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
4791 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
4792   ".type __tls_get_offset_wrapper, @function\n"
4793   "__tls_get_offset_wrapper:\n"
4794 #ifdef __s390x__
4795   "sgr %r2,%r12\n"
4796 #else
4797   "sr %r2,%r12\n"
4798 #endif
4799   "br %r3\n"
4800   ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
4801 );
4802 #endif // SANITIZER_S390
4803 #else
4804 #define INIT_TLS_GET_ADDR
4805 #endif
4806
4807 #if SANITIZER_INTERCEPT_LISTXATTR
4808 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4809   void *ctx;
4810   COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4811   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4812   // FIXME: under ASan the call below may write to freed memory and corrupt
4813   // its metadata. See
4814   // https://github.com/google/sanitizers/issues/321.
4815   SSIZE_T res = REAL(listxattr)(path, list, size);
4816   // Here and below, size == 0 is a special case where nothing is written to the
4817   // buffer, and res contains the desired buffer size.
4818   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4819   return res;
4820 }
4821 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4822   void *ctx;
4823   COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4824   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4825   // FIXME: under ASan the call below may write to freed memory and corrupt
4826   // its metadata. See
4827   // https://github.com/google/sanitizers/issues/321.
4828   SSIZE_T res = REAL(llistxattr)(path, list, size);
4829   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4830   return res;
4831 }
4832 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4833   void *ctx;
4834   COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4835   // FIXME: under ASan the call below may write to freed memory and corrupt
4836   // its metadata. See
4837   // https://github.com/google/sanitizers/issues/321.
4838   SSIZE_T res = REAL(flistxattr)(fd, list, size);
4839   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4840   return res;
4841 }
4842 #define INIT_LISTXATTR                   \
4843   COMMON_INTERCEPT_FUNCTION(listxattr);  \
4844   COMMON_INTERCEPT_FUNCTION(llistxattr); \
4845   COMMON_INTERCEPT_FUNCTION(flistxattr);
4846 #else
4847 #define INIT_LISTXATTR
4848 #endif
4849
4850 #if SANITIZER_INTERCEPT_GETXATTR
4851 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4852             SIZE_T size) {
4853   void *ctx;
4854   COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4855   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4856   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4857   // FIXME: under ASan the call below may write to freed memory and corrupt
4858   // its metadata. See
4859   // https://github.com/google/sanitizers/issues/321.
4860   SSIZE_T res = REAL(getxattr)(path, name, value, size);
4861   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4862   return res;
4863 }
4864 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4865             SIZE_T size) {
4866   void *ctx;
4867   COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4868   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4869   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4870   // FIXME: under ASan the call below may write to freed memory and corrupt
4871   // its metadata. See
4872   // https://github.com/google/sanitizers/issues/321.
4873   SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4874   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4875   return res;
4876 }
4877 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4878             SIZE_T size) {
4879   void *ctx;
4880   COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4881   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4882   // FIXME: under ASan the call below may write to freed memory and corrupt
4883   // its metadata. See
4884   // https://github.com/google/sanitizers/issues/321.
4885   SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4886   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4887   return res;
4888 }
4889 #define INIT_GETXATTR                   \
4890   COMMON_INTERCEPT_FUNCTION(getxattr);  \
4891   COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4892   COMMON_INTERCEPT_FUNCTION(fgetxattr);
4893 #else
4894 #define INIT_GETXATTR
4895 #endif
4896
4897 #if SANITIZER_INTERCEPT_GETRESID
4898 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4899   void *ctx;
4900   COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4901   // FIXME: under ASan the call below may write to freed memory and corrupt
4902   // its metadata. See
4903   // https://github.com/google/sanitizers/issues/321.
4904   int res = REAL(getresuid)(ruid, euid, suid);
4905   if (res >= 0) {
4906     if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4907     if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4908     if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4909   }
4910   return res;
4911 }
4912 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4913   void *ctx;
4914   COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4915   // FIXME: under ASan the call below may write to freed memory and corrupt
4916   // its metadata. See
4917   // https://github.com/google/sanitizers/issues/321.
4918   int res = REAL(getresgid)(rgid, egid, sgid);
4919   if (res >= 0) {
4920     if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4921     if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4922     if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4923   }
4924   return res;
4925 }
4926 #define INIT_GETRESID                   \
4927   COMMON_INTERCEPT_FUNCTION(getresuid); \
4928   COMMON_INTERCEPT_FUNCTION(getresgid);
4929 #else
4930 #define INIT_GETRESID
4931 #endif
4932
4933 #if SANITIZER_INTERCEPT_GETIFADDRS
4934 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4935 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4936 // intercept it to poison the memory again.
4937 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4938   void *ctx;
4939   COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4940   // FIXME: under ASan the call below may write to freed memory and corrupt
4941   // its metadata. See
4942   // https://github.com/google/sanitizers/issues/321.
4943   int res = REAL(getifaddrs)(ifap);
4944   if (res == 0 && ifap) {
4945     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4946     __sanitizer_ifaddrs *p = *ifap;
4947     while (p) {
4948       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4949       if (p->ifa_name)
4950         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4951                                        REAL(strlen)(p->ifa_name) + 1);
4952       if (p->ifa_addr)
4953         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4954       if (p->ifa_netmask)
4955         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4956       // On Linux this is a union, but the other member also points to a
4957       // struct sockaddr, so the following is sufficient.
4958       if (p->ifa_dstaddr)
4959         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4960       // FIXME(smatveev): Unpoison p->ifa_data as well.
4961       p = p->ifa_next;
4962     }
4963   }
4964   return res;
4965 }
4966 #define INIT_GETIFADDRS                  \
4967   COMMON_INTERCEPT_FUNCTION(getifaddrs);
4968 #else
4969 #define INIT_GETIFADDRS
4970 #endif
4971
4972 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4973 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4974   void *ctx;
4975   COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4976   // FIXME: under ASan the call below may write to freed memory and corrupt
4977   // its metadata. See
4978   // https://github.com/google/sanitizers/issues/321.
4979   char *res = REAL(if_indextoname)(ifindex, ifname);
4980   if (res && ifname)
4981     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4982   return res;
4983 }
4984 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4985   void *ctx;
4986   COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4987   if (ifname)
4988     COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4989   return REAL(if_nametoindex)(ifname);
4990 }
4991 #define INIT_IF_INDEXTONAME                  \
4992   COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4993   COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4994 #else
4995 #define INIT_IF_INDEXTONAME
4996 #endif
4997
4998 #if SANITIZER_INTERCEPT_CAPGET
4999 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5000   void *ctx;
5001   COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5002   if (hdrp)
5003     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5004   // FIXME: under ASan the call below may write to freed memory and corrupt
5005   // its metadata. See
5006   // https://github.com/google/sanitizers/issues/321.
5007   int res = REAL(capget)(hdrp, datap);
5008   if (res == 0 && datap)
5009     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5010   // We can also return -1 and write to hdrp->version if the version passed in
5011   // hdrp->version is unsupported. But that's not a trivial condition to check,
5012   // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5013   return res;
5014 }
5015 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5016   void *ctx;
5017   COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5018   if (hdrp)
5019     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5020   if (datap)
5021     COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5022   return REAL(capset)(hdrp, datap);
5023 }
5024 #define INIT_CAPGET                  \
5025   COMMON_INTERCEPT_FUNCTION(capget); \
5026   COMMON_INTERCEPT_FUNCTION(capset);
5027 #else
5028 #define INIT_CAPGET
5029 #endif
5030
5031 #if SANITIZER_INTERCEPT_AEABI_MEM
5032 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5033   void *ctx;
5034   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5035 }
5036
5037 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5038   void *ctx;
5039   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5040 }
5041
5042 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5043   void *ctx;
5044   COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5045 }
5046
5047 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5048   void *ctx;
5049   COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5050 }
5051
5052 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5053   void *ctx;
5054   COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5055 }
5056
5057 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5058   void *ctx;
5059   COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5060 }
5061
5062 // Note the argument order.
5063 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5064   void *ctx;
5065   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5066 }
5067
5068 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5069   void *ctx;
5070   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5071 }
5072
5073 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5074   void *ctx;
5075   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5076 }
5077
5078 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5079   void *ctx;
5080   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5081 }
5082
5083 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5084   void *ctx;
5085   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5086 }
5087
5088 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5089   void *ctx;
5090   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5091 }
5092
5093 #define INIT_AEABI_MEM                         \
5094   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove);  \
5095   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5096   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5097   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy);   \
5098   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4);  \
5099   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8);  \
5100   COMMON_INTERCEPT_FUNCTION(__aeabi_memset);   \
5101   COMMON_INTERCEPT_FUNCTION(__aeabi_memset4);  \
5102   COMMON_INTERCEPT_FUNCTION(__aeabi_memset8);  \
5103   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr);   \
5104   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4);  \
5105   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5106 #else
5107 #define INIT_AEABI_MEM
5108 #endif  // SANITIZER_INTERCEPT_AEABI_MEM
5109
5110 #if SANITIZER_INTERCEPT___BZERO
5111 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5112   void *ctx;
5113   COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5114 }
5115
5116 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5117 #else
5118 #define INIT___BZERO
5119 #endif  // SANITIZER_INTERCEPT___BZERO
5120
5121 #if SANITIZER_INTERCEPT_FTIME
5122 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5123   void *ctx;
5124   COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5125   // FIXME: under ASan the call below may write to freed memory and corrupt
5126   // its metadata. See
5127   // https://github.com/google/sanitizers/issues/321.
5128   int res = REAL(ftime)(tp);
5129   if (tp)
5130     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5131   return res;
5132 }
5133 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5134 #else
5135 #define INIT_FTIME
5136 #endif  // SANITIZER_INTERCEPT_FTIME
5137
5138 #if SANITIZER_INTERCEPT_XDR
5139 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5140             unsigned size, int op) {
5141   void *ctx;
5142   COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5143   // FIXME: under ASan the call below may write to freed memory and corrupt
5144   // its metadata. See
5145   // https://github.com/google/sanitizers/issues/321.
5146   REAL(xdrmem_create)(xdrs, addr, size, op);
5147   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5148   if (op == __sanitizer_XDR_ENCODE) {
5149     // It's not obvious how much data individual xdr_ routines write.
5150     // Simply unpoison the entire target buffer in advance.
5151     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5152   }
5153 }
5154
5155 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5156   void *ctx;
5157   COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5158   // FIXME: under ASan the call below may write to freed memory and corrupt
5159   // its metadata. See
5160   // https://github.com/google/sanitizers/issues/321.
5161   REAL(xdrstdio_create)(xdrs, file, op);
5162   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5163 }
5164
5165 // FIXME: under ASan the call below may write to freed memory and corrupt
5166 // its metadata. See
5167 // https://github.com/google/sanitizers/issues/321.
5168 #define XDR_INTERCEPTOR(F, T)                             \
5169   INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) {      \
5170     void *ctx;                                            \
5171     COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p);            \
5172     if (p && xdrs->x_op == __sanitizer_XDR_ENCODE)        \
5173       COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));  \
5174     int res = REAL(F)(xdrs, p);                           \
5175     if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5176       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5177     return res;                                           \
5178   }
5179
5180 XDR_INTERCEPTOR(xdr_short, short)
5181 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5182 XDR_INTERCEPTOR(xdr_int, int)
5183 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5184 XDR_INTERCEPTOR(xdr_long, long)
5185 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5186 XDR_INTERCEPTOR(xdr_hyper, long long)
5187 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5188 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5189 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5190 XDR_INTERCEPTOR(xdr_int8_t, u8)
5191 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5192 XDR_INTERCEPTOR(xdr_int16_t, u16)
5193 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5194 XDR_INTERCEPTOR(xdr_int32_t, u32)
5195 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5196 XDR_INTERCEPTOR(xdr_int64_t, u64)
5197 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5198 XDR_INTERCEPTOR(xdr_quad_t, long long)
5199 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5200 XDR_INTERCEPTOR(xdr_bool, bool)
5201 XDR_INTERCEPTOR(xdr_enum, int)
5202 XDR_INTERCEPTOR(xdr_char, char)
5203 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5204 XDR_INTERCEPTOR(xdr_float, float)
5205 XDR_INTERCEPTOR(xdr_double, double)
5206
5207 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5208 // wrapstring, sizeof
5209
5210 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5211             unsigned maxsize) {
5212   void *ctx;
5213   COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5214   if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5215     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5216     COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5217     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5218   }
5219   // FIXME: under ASan the call below may write to freed memory and corrupt
5220   // its metadata. See
5221   // https://github.com/google/sanitizers/issues/321.
5222   int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5223   if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5224     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5225     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5226     if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5227   }
5228   return res;
5229 }
5230
5231 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5232             unsigned maxsize) {
5233   void *ctx;
5234   COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5235   if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5236     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5237     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5238   }
5239   // FIXME: under ASan the call below may write to freed memory and corrupt
5240   // its metadata. See
5241   // https://github.com/google/sanitizers/issues/321.
5242   int res = REAL(xdr_string)(xdrs, p, maxsize);
5243   if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5244     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5245     if (res && *p)
5246       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5247   }
5248   return res;
5249 }
5250
5251 #define INIT_XDR                               \
5252   COMMON_INTERCEPT_FUNCTION(xdrmem_create);    \
5253   COMMON_INTERCEPT_FUNCTION(xdrstdio_create);  \
5254   COMMON_INTERCEPT_FUNCTION(xdr_short);        \
5255   COMMON_INTERCEPT_FUNCTION(xdr_u_short);      \
5256   COMMON_INTERCEPT_FUNCTION(xdr_int);          \
5257   COMMON_INTERCEPT_FUNCTION(xdr_u_int);        \
5258   COMMON_INTERCEPT_FUNCTION(xdr_long);         \
5259   COMMON_INTERCEPT_FUNCTION(xdr_u_long);       \
5260   COMMON_INTERCEPT_FUNCTION(xdr_hyper);        \
5261   COMMON_INTERCEPT_FUNCTION(xdr_u_hyper);      \
5262   COMMON_INTERCEPT_FUNCTION(xdr_longlong_t);   \
5263   COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5264   COMMON_INTERCEPT_FUNCTION(xdr_int8_t);       \
5265   COMMON_INTERCEPT_FUNCTION(xdr_uint8_t);      \
5266   COMMON_INTERCEPT_FUNCTION(xdr_int16_t);      \
5267   COMMON_INTERCEPT_FUNCTION(xdr_uint16_t);     \
5268   COMMON_INTERCEPT_FUNCTION(xdr_int32_t);      \
5269   COMMON_INTERCEPT_FUNCTION(xdr_uint32_t);     \
5270   COMMON_INTERCEPT_FUNCTION(xdr_int64_t);      \
5271   COMMON_INTERCEPT_FUNCTION(xdr_uint64_t);     \
5272   COMMON_INTERCEPT_FUNCTION(xdr_quad_t);       \
5273   COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t);     \
5274   COMMON_INTERCEPT_FUNCTION(xdr_bool);         \
5275   COMMON_INTERCEPT_FUNCTION(xdr_enum);         \
5276   COMMON_INTERCEPT_FUNCTION(xdr_char);         \
5277   COMMON_INTERCEPT_FUNCTION(xdr_u_char);       \
5278   COMMON_INTERCEPT_FUNCTION(xdr_float);        \
5279   COMMON_INTERCEPT_FUNCTION(xdr_double);       \
5280   COMMON_INTERCEPT_FUNCTION(xdr_bytes);        \
5281   COMMON_INTERCEPT_FUNCTION(xdr_string);
5282 #else
5283 #define INIT_XDR
5284 #endif  // SANITIZER_INTERCEPT_XDR
5285
5286 #if SANITIZER_INTERCEPT_TSEARCH
5287 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5288             int (*compar)(const void *, const void *)) {
5289   void *ctx;
5290   COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5291   // FIXME: under ASan the call below may write to freed memory and corrupt
5292   // its metadata. See
5293   // https://github.com/google/sanitizers/issues/321.
5294   void *res = REAL(tsearch)(key, rootp, compar);
5295   if (res && *(void **)res == key)
5296     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5297   return res;
5298 }
5299 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5300 #else
5301 #define INIT_TSEARCH
5302 #endif
5303
5304 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5305     SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5306 void unpoison_file(__sanitizer_FILE *fp) {
5307 #if SANITIZER_HAS_STRUCT_FILE
5308   COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5309   if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5310     COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5311                                         fp->_IO_read_end - fp->_IO_read_base);
5312 #endif  // SANITIZER_HAS_STRUCT_FILE
5313 }
5314 #endif
5315
5316 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5317 // These guys are called when a .c source is built with -O2.
5318 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5319   void *ctx;
5320   COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5321   int res = REAL(__uflow)(fp);
5322   unpoison_file(fp);
5323   return res;
5324 }
5325 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5326   void *ctx;
5327   COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5328   int res = REAL(__underflow)(fp);
5329   unpoison_file(fp);
5330   return res;
5331 }
5332 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5333   void *ctx;
5334   COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5335   int res = REAL(__overflow)(fp, ch);
5336   unpoison_file(fp);
5337   return res;
5338 }
5339 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5340   void *ctx;
5341   COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5342   int res = REAL(__wuflow)(fp);
5343   unpoison_file(fp);
5344   return res;
5345 }
5346 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5347   void *ctx;
5348   COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5349   int res = REAL(__wunderflow)(fp);
5350   unpoison_file(fp);
5351   return res;
5352 }
5353 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5354   void *ctx;
5355   COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5356   int res = REAL(__woverflow)(fp, ch);
5357   unpoison_file(fp);
5358   return res;
5359 }
5360 #define INIT_LIBIO_INTERNALS               \
5361   COMMON_INTERCEPT_FUNCTION(__uflow);      \
5362   COMMON_INTERCEPT_FUNCTION(__underflow);  \
5363   COMMON_INTERCEPT_FUNCTION(__overflow);   \
5364   COMMON_INTERCEPT_FUNCTION(__wuflow);     \
5365   COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5366   COMMON_INTERCEPT_FUNCTION(__woverflow);
5367 #else
5368 #define INIT_LIBIO_INTERNALS
5369 #endif
5370
5371 #if SANITIZER_INTERCEPT_FOPEN
5372 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5373   void *ctx;
5374   COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5375   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5376   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5377   __sanitizer_FILE *res = REAL(fopen)(path, mode);
5378   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5379   if (res) unpoison_file(res);
5380   return res;
5381 }
5382 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5383   void *ctx;
5384   COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5385   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5386   __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5387   if (res) unpoison_file(res);
5388   return res;
5389 }
5390 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5391             __sanitizer_FILE *fp) {
5392   void *ctx;
5393   COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5394   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5395   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5396   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5397   __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5398   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5399   if (res) unpoison_file(res);
5400   return res;
5401 }
5402 #define INIT_FOPEN                   \
5403   COMMON_INTERCEPT_FUNCTION(fopen);  \
5404   COMMON_INTERCEPT_FUNCTION(fdopen); \
5405   COMMON_INTERCEPT_FUNCTION(freopen);
5406 #else
5407 #define INIT_FOPEN
5408 #endif
5409
5410 #if SANITIZER_INTERCEPT_FOPEN64
5411 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5412   void *ctx;
5413   COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5414   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5415   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5416   __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5417   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5418   if (res) unpoison_file(res);
5419   return res;
5420 }
5421 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5422             __sanitizer_FILE *fp) {
5423   void *ctx;
5424   COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5425   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5426   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5427   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5428   __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5429   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5430   if (res) unpoison_file(res);
5431   return res;
5432 }
5433 #define INIT_FOPEN64                  \
5434   COMMON_INTERCEPT_FUNCTION(fopen64); \
5435   COMMON_INTERCEPT_FUNCTION(freopen64);
5436 #else
5437 #define INIT_FOPEN64
5438 #endif
5439
5440 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5441 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5442   void *ctx;
5443   COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5444   // FIXME: under ASan the call below may write to freed memory and corrupt
5445   // its metadata. See
5446   // https://github.com/google/sanitizers/issues/321.
5447   __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5448   if (res) {
5449     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5450     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5451     unpoison_file(res);
5452     FileMetadata file = {ptr, sizeloc};
5453     SetInterceptorMetadata(res, file);
5454   }
5455   return res;
5456 }
5457 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5458             SIZE_T *sizeloc) {
5459   void *ctx;
5460   COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5461   __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5462   if (res) {
5463     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5464     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5465     unpoison_file(res);
5466     FileMetadata file = {(char **)ptr, sizeloc};
5467     SetInterceptorMetadata(res, file);
5468   }
5469   return res;
5470 }
5471 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5472             const char *mode) {
5473   void *ctx;
5474   COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5475   // FIXME: under ASan the call below may write to freed memory and corrupt
5476   // its metadata. See
5477   // https://github.com/google/sanitizers/issues/321.
5478   __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5479   if (res) unpoison_file(res);
5480   return res;
5481 }
5482 #define INIT_OPEN_MEMSTREAM                   \
5483   COMMON_INTERCEPT_FUNCTION(open_memstream);  \
5484   COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5485   COMMON_INTERCEPT_FUNCTION(fmemopen);
5486 #else
5487 #define INIT_OPEN_MEMSTREAM
5488 #endif
5489
5490 #if SANITIZER_INTERCEPT_OBSTACK
5491 static void initialize_obstack(__sanitizer_obstack *obstack) {
5492   COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5493   if (obstack->chunk)
5494     COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5495                                         sizeof(*obstack->chunk));
5496 }
5497
5498 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5499             int align, void *(*alloc_fn)(uptr arg, uptr sz),
5500             void (*free_fn)(uptr arg, void *p)) {
5501   void *ctx;
5502   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5503                            free_fn);
5504   int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5505   if (res) initialize_obstack(obstack);
5506   return res;
5507 }
5508 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5509             int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5510   void *ctx;
5511   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5512                            free_fn);
5513   int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5514   if (res) initialize_obstack(obstack);
5515   return res;
5516 }
5517 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5518   void *ctx;
5519   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5520   REAL(_obstack_newchunk)(obstack, length);
5521   if (obstack->chunk)
5522     COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5523         obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5524 }
5525 #define INIT_OBSTACK                           \
5526   COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5527   COMMON_INTERCEPT_FUNCTION(_obstack_begin);   \
5528   COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5529 #else
5530 #define INIT_OBSTACK
5531 #endif
5532
5533 #if SANITIZER_INTERCEPT_FFLUSH
5534 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5535   void *ctx;
5536   COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5537   int res = REAL(fflush)(fp);
5538   // FIXME: handle fp == NULL
5539   if (fp) {
5540     const FileMetadata *m = GetInterceptorMetadata(fp);
5541     if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5542   }
5543   return res;
5544 }
5545 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5546 #else
5547 #define INIT_FFLUSH
5548 #endif
5549
5550 #if SANITIZER_INTERCEPT_FCLOSE
5551 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5552   void *ctx;
5553   COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5554   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5555   const FileMetadata *m = GetInterceptorMetadata(fp);
5556   int res = REAL(fclose)(fp);
5557   if (m) {
5558     COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5559     DeleteInterceptorMetadata(fp);
5560   }
5561   return res;
5562 }
5563 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5564 #else
5565 #define INIT_FCLOSE
5566 #endif
5567
5568 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5569 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5570   void *ctx;
5571   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5572   if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5573   COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5574   void *res = REAL(dlopen)(filename, flag);
5575   COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5576   return res;
5577 }
5578
5579 INTERCEPTOR(int, dlclose, void *handle) {
5580   void *ctx;
5581   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5582   int res = REAL(dlclose)(handle);
5583   COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5584   return res;
5585 }
5586 #define INIT_DLOPEN_DLCLOSE          \
5587   COMMON_INTERCEPT_FUNCTION(dlopen); \
5588   COMMON_INTERCEPT_FUNCTION(dlclose);
5589 #else
5590 #define INIT_DLOPEN_DLCLOSE
5591 #endif
5592
5593 #if SANITIZER_INTERCEPT_GETPASS
5594 INTERCEPTOR(char *, getpass, const char *prompt) {
5595   void *ctx;
5596   COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5597   if (prompt)
5598     COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5599   char *res = REAL(getpass)(prompt);
5600   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5601   return res;
5602 }
5603
5604 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5605 #else
5606 #define INIT_GETPASS
5607 #endif
5608
5609 #if SANITIZER_INTERCEPT_TIMERFD
5610 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5611             void *old_value) {
5612   void *ctx;
5613   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5614                            old_value);
5615   COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5616   int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5617   if (res != -1 && old_value)
5618     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5619   return res;
5620 }
5621
5622 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5623   void *ctx;
5624   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5625   int res = REAL(timerfd_gettime)(fd, curr_value);
5626   if (res != -1 && curr_value)
5627     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5628   return res;
5629 }
5630 #define INIT_TIMERFD                          \
5631   COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5632   COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5633 #else
5634 #define INIT_TIMERFD
5635 #endif
5636
5637 #if SANITIZER_INTERCEPT_MLOCKX
5638 // Linux kernel has a bug that leads to kernel deadlock if a process
5639 // maps TBs of memory and then calls mlock().
5640 static void MlockIsUnsupported() {
5641   static atomic_uint8_t printed;
5642   if (atomic_exchange(&printed, 1, memory_order_relaxed))
5643     return;
5644   VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5645           SanitizerToolName);
5646 }
5647
5648 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5649   MlockIsUnsupported();
5650   return 0;
5651 }
5652
5653 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5654   MlockIsUnsupported();
5655   return 0;
5656 }
5657
5658 INTERCEPTOR(int, mlockall, int flags) {
5659   MlockIsUnsupported();
5660   return 0;
5661 }
5662
5663 INTERCEPTOR(int, munlockall, void) {
5664   MlockIsUnsupported();
5665   return 0;
5666 }
5667
5668 #define INIT_MLOCKX                                                            \
5669   COMMON_INTERCEPT_FUNCTION(mlock);                                            \
5670   COMMON_INTERCEPT_FUNCTION(munlock);                                          \
5671   COMMON_INTERCEPT_FUNCTION(mlockall);                                         \
5672   COMMON_INTERCEPT_FUNCTION(munlockall);
5673
5674 #else
5675 #define INIT_MLOCKX
5676 #endif  // SANITIZER_INTERCEPT_MLOCKX
5677
5678 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5679 struct WrappedCookie {
5680   void *real_cookie;
5681   __sanitizer_cookie_io_functions_t real_io_funcs;
5682 };
5683
5684 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5685   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5686   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5687   __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5688   return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5689 }
5690
5691 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5692   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5693   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5694   __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5695   return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5696 }
5697
5698 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5699   COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5700   COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5701   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5702   __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5703   return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5704                    : -1;
5705 }
5706
5707 static int wrapped_close(void *cookie) {
5708   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5709   WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5710   __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5711   int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5712   InternalFree(wrapped_cookie);
5713   return res;
5714 }
5715
5716 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5717             __sanitizer_cookie_io_functions_t io_funcs) {
5718   void *ctx;
5719   COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5720   WrappedCookie *wrapped_cookie =
5721       (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5722   wrapped_cookie->real_cookie = cookie;
5723   wrapped_cookie->real_io_funcs = io_funcs;
5724   __sanitizer_FILE *res =
5725       REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5726                                                wrapped_seek, wrapped_close});
5727   return res;
5728 }
5729
5730 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5731 #else
5732 #define INIT_FOPENCOOKIE
5733 #endif  // SANITIZER_INTERCEPT_FOPENCOOKIE
5734
5735 #if SANITIZER_INTERCEPT_SEM
5736 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5737   void *ctx;
5738   COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5739   // Workaround a bug in glibc's "old" semaphore implementation by
5740   // zero-initializing the sem_t contents. This has to be done here because
5741   // interceptors bind to the lowest symbols version by default, hitting the
5742   // buggy code path while the non-sanitized build of the same code works fine.
5743   REAL(memset)(s, 0, sizeof(*s));
5744   int res = REAL(sem_init)(s, pshared, value);
5745   return res;
5746 }
5747
5748 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5749   void *ctx;
5750   COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5751   int res = REAL(sem_destroy)(s);
5752   return res;
5753 }
5754
5755 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5756   void *ctx;
5757   COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5758   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5759   if (res == 0) {
5760     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5761   }
5762   return res;
5763 }
5764
5765 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5766   void *ctx;
5767   COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5768   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5769   if (res == 0) {
5770     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5771   }
5772   return res;
5773 }
5774
5775 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5776   void *ctx;
5777   COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5778   COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5779   int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5780   if (res == 0) {
5781     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5782   }
5783   return res;
5784 }
5785
5786 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5787   void *ctx;
5788   COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5789   COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5790   int res = REAL(sem_post)(s);
5791   return res;
5792 }
5793
5794 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5795   void *ctx;
5796   COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5797   int res = REAL(sem_getvalue)(s, sval);
5798   if (res == 0) {
5799     COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5800     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5801   }
5802   return res;
5803 }
5804 #define INIT_SEM                                                               \
5805   COMMON_INTERCEPT_FUNCTION(sem_init);                                         \
5806   COMMON_INTERCEPT_FUNCTION(sem_destroy);                                      \
5807   COMMON_INTERCEPT_FUNCTION(sem_wait);                                         \
5808   COMMON_INTERCEPT_FUNCTION(sem_trywait);                                      \
5809   COMMON_INTERCEPT_FUNCTION(sem_timedwait);                                    \
5810   COMMON_INTERCEPT_FUNCTION(sem_post);                                         \
5811   COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5812 #else
5813 #define INIT_SEM
5814 #endif // SANITIZER_INTERCEPT_SEM
5815
5816 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5817 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5818   void *ctx;
5819   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5820   int res = REAL(pthread_setcancelstate)(state, oldstate);
5821   if (res == 0)
5822     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5823   return res;
5824 }
5825
5826 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5827   void *ctx;
5828   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5829   int res = REAL(pthread_setcanceltype)(type, oldtype);
5830   if (res == 0)
5831     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5832   return res;
5833 }
5834 #define INIT_PTHREAD_SETCANCEL                                                 \
5835   COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate);                           \
5836   COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5837 #else
5838 #define INIT_PTHREAD_SETCANCEL
5839 #endif
5840
5841 #if SANITIZER_INTERCEPT_MINCORE
5842 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5843   void *ctx;
5844   COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5845   int res = REAL(mincore)(addr, length, vec);
5846   if (res == 0) {
5847     uptr page_size = GetPageSizeCached();
5848     uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5849     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5850   }
5851   return res;
5852 }
5853 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5854 #else
5855 #define INIT_MINCORE
5856 #endif
5857
5858 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5859 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5860             uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5861             uptr flags) {
5862   void *ctx;
5863   COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5864                            remote_iov, riovcnt, flags);
5865   SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5866                                        riovcnt, flags);
5867   if (res > 0)
5868     write_iovec(ctx, local_iov, liovcnt, res);
5869   return res;
5870 }
5871
5872 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5873             uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5874             uptr flags) {
5875   void *ctx;
5876   COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5877                            remote_iov, riovcnt, flags);
5878   SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5879                                         riovcnt, flags);
5880   if (res > 0)
5881     read_iovec(ctx, local_iov, liovcnt, res);
5882   return res;
5883 }
5884 #define INIT_PROCESS_VM_READV                                                  \
5885   COMMON_INTERCEPT_FUNCTION(process_vm_readv);                                 \
5886   COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5887 #else
5888 #define INIT_PROCESS_VM_READV
5889 #endif
5890
5891 #if SANITIZER_INTERCEPT_CTERMID
5892 INTERCEPTOR(char *, ctermid, char *s) {
5893   void *ctx;
5894   COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5895   char *res = REAL(ctermid)(s);
5896   if (res) {
5897     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5898   }
5899   return res;
5900 }
5901 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5902 #else
5903 #define INIT_CTERMID
5904 #endif
5905
5906 #if SANITIZER_INTERCEPT_CTERMID_R
5907 INTERCEPTOR(char *, ctermid_r, char *s) {
5908   void *ctx;
5909   COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5910   char *res = REAL(ctermid_r)(s);
5911   if (res) {
5912     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5913   }
5914   return res;
5915 }
5916 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5917 #else
5918 #define INIT_CTERMID_R
5919 #endif
5920
5921 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5922 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5923   void *ctx;
5924   COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5925   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5926   SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5927   if (res > 0) {
5928     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5929   }
5930   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5931   return res;
5932 }
5933
5934 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5935             void *srcaddr, int *addrlen) {
5936   void *ctx;
5937   COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5938                            addrlen);
5939   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5940   SIZE_T srcaddr_sz;
5941   if (srcaddr) srcaddr_sz = *addrlen;
5942   (void)srcaddr_sz;  // prevent "set but not used" warning
5943   SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5944   if (res > 0) {
5945     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5946     if (srcaddr)
5947       COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5948                                           Min((SIZE_T)*addrlen, srcaddr_sz));
5949   }
5950   return res;
5951 }
5952 #define INIT_RECV_RECVFROM          \
5953   COMMON_INTERCEPT_FUNCTION(recv);  \
5954   COMMON_INTERCEPT_FUNCTION(recvfrom);
5955 #else
5956 #define INIT_RECV_RECVFROM
5957 #endif
5958
5959 #if SANITIZER_INTERCEPT_SEND_SENDTO
5960 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5961   void *ctx;
5962   COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5963   if (fd >= 0) {
5964     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5965     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5966   }
5967   SSIZE_T res = REAL(send)(fd, buf, len, flags);
5968   if (common_flags()->intercept_send && res > 0)
5969     COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5970   return res;
5971 }
5972
5973 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5974             void *dstaddr, int addrlen) {
5975   void *ctx;
5976   COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5977   if (fd >= 0) {
5978     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5979     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5980   }
5981   // Can't check dstaddr as it may have uninitialized padding at the end.
5982   SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5983   if (common_flags()->intercept_send && res > 0)
5984     COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5985   return res;
5986 }
5987 #define INIT_SEND_SENDTO           \
5988   COMMON_INTERCEPT_FUNCTION(send); \
5989   COMMON_INTERCEPT_FUNCTION(sendto);
5990 #else
5991 #define INIT_SEND_SENDTO
5992 #endif
5993
5994 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5995 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5996   void *ctx;
5997   COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5998   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5999   int res = REAL(eventfd_read)(fd, value);
6000   if (res == 0) {
6001     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6002     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6003   }
6004   return res;
6005 }
6006 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6007   void *ctx;
6008   COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6009   if (fd >= 0) {
6010     COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6011     COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6012   }
6013   int res = REAL(eventfd_write)(fd, value);
6014   return res;
6015 }
6016 #define INIT_EVENTFD_READ_WRITE            \
6017   COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6018   COMMON_INTERCEPT_FUNCTION(eventfd_write)
6019 #else
6020 #define INIT_EVENTFD_READ_WRITE
6021 #endif
6022
6023 #if SANITIZER_INTERCEPT_STAT
6024 INTERCEPTOR(int, stat, const char *path, void *buf) {
6025   void *ctx;
6026   COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6027   if (common_flags()->intercept_stat)
6028     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6029   int res = REAL(stat)(path, buf);
6030   if (!res)
6031     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6032   return res;
6033 }
6034 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6035 #else
6036 #define INIT_STAT
6037 #endif
6038
6039 #if SANITIZER_INTERCEPT___XSTAT
6040 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6041   void *ctx;
6042   COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6043   if (common_flags()->intercept_stat)
6044     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6045   int res = REAL(__xstat)(version, path, buf);
6046   if (!res)
6047     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6048   return res;
6049 }
6050 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6051 #else
6052 #define INIT___XSTAT
6053 #endif
6054
6055 #if SANITIZER_INTERCEPT___XSTAT64
6056 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6057   void *ctx;
6058   COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6059   if (common_flags()->intercept_stat)
6060     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6061   int res = REAL(__xstat64)(version, path, buf);
6062   if (!res)
6063     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6064   return res;
6065 }
6066 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6067 #else
6068 #define INIT___XSTAT64
6069 #endif
6070
6071 #if SANITIZER_INTERCEPT___LXSTAT
6072 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6073   void *ctx;
6074   COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6075   if (common_flags()->intercept_stat)
6076     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6077   int res = REAL(__lxstat)(version, path, buf);
6078   if (!res)
6079     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6080   return res;
6081 }
6082 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6083 #else
6084 #define INIT___LXSTAT
6085 #endif
6086
6087 #if SANITIZER_INTERCEPT___LXSTAT64
6088 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6089   void *ctx;
6090   COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6091   if (common_flags()->intercept_stat)
6092     COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6093   int res = REAL(__lxstat64)(version, path, buf);
6094   if (!res)
6095     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6096   return res;
6097 }
6098 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6099 #else
6100 #define INIT___LXSTAT64
6101 #endif
6102
6103 // FIXME: add other *stat interceptor
6104
6105 #if SANITIZER_INTERCEPT_UTMP
6106 INTERCEPTOR(void *, getutent, int dummy) {
6107   void *ctx;
6108   COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6109   void *res = REAL(getutent)(dummy);
6110   if (res)
6111     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6112   return res;
6113 }
6114 INTERCEPTOR(void *, getutid, void *ut) {
6115   void *ctx;
6116   COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6117   void *res = REAL(getutid)(ut);
6118   if (res)
6119     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6120   return res;
6121 }
6122 INTERCEPTOR(void *, getutline, void *ut) {
6123   void *ctx;
6124   COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6125   void *res = REAL(getutline)(ut);
6126   if (res)
6127     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6128   return res;
6129 }
6130 #define INIT_UTMP                      \
6131   COMMON_INTERCEPT_FUNCTION(getutent); \
6132   COMMON_INTERCEPT_FUNCTION(getutid);  \
6133   COMMON_INTERCEPT_FUNCTION(getutline);
6134 #else
6135 #define INIT_UTMP
6136 #endif
6137
6138 #if SANITIZER_INTERCEPT_UTMPX
6139 INTERCEPTOR(void *, getutxent, int dummy) {
6140   void *ctx;
6141   COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6142   void *res = REAL(getutxent)(dummy);
6143   if (res)
6144     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6145   return res;
6146 }
6147 INTERCEPTOR(void *, getutxid, void *ut) {
6148   void *ctx;
6149   COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6150   void *res = REAL(getutxid)(ut);
6151   if (res)
6152     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6153   return res;
6154 }
6155 INTERCEPTOR(void *, getutxline, void *ut) {
6156   void *ctx;
6157   COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6158   void *res = REAL(getutxline)(ut);
6159   if (res)
6160     COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6161   return res;
6162 }
6163 #define INIT_UTMPX                      \
6164   COMMON_INTERCEPT_FUNCTION(getutxent); \
6165   COMMON_INTERCEPT_FUNCTION(getutxid);  \
6166   COMMON_INTERCEPT_FUNCTION(getutxline);
6167 #else
6168 #define INIT_UTMPX
6169 #endif
6170
6171 #if SANITIZER_INTERCEPT_GETLOADAVG
6172 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6173   void *ctx;
6174   COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6175   int res = REAL(getloadavg)(loadavg, nelem);
6176   if (res > 0)
6177     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6178   return res;
6179 }
6180 #define INIT_GETLOADAVG                      \
6181   COMMON_INTERCEPT_FUNCTION(getloadavg);
6182 #else
6183 #define INIT_GETLOADAVG
6184 #endif
6185
6186 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6187 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6188   return 0;
6189 }
6190
6191 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6192   return 0;
6193 }
6194
6195 INTERCEPTOR(int, mprobe, void *ptr) {
6196   return 0;
6197 }
6198 #endif
6199
6200 static void InitializeCommonInterceptors() {
6201   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
6202   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
6203
6204   INIT_TEXTDOMAIN;
6205   INIT_STRLEN;
6206   INIT_STRNLEN;
6207   INIT_STRNDUP;
6208   INIT___STRNDUP;
6209   INIT_STRCMP;
6210   INIT_STRNCMP;
6211   INIT_STRCASECMP;
6212   INIT_STRNCASECMP;
6213   INIT_STRSTR;
6214   INIT_STRCASESTR;
6215   INIT_STRCHR;
6216   INIT_STRCHRNUL;
6217   INIT_STRRCHR;
6218   INIT_STRSPN;
6219   INIT_STRTOK;
6220   INIT_STRPBRK;
6221   INIT_MEMSET;
6222   INIT_MEMMOVE;
6223   INIT_MEMCPY;
6224   INIT_MEMCHR;
6225   INIT_MEMCMP;
6226   INIT_MEMRCHR;
6227   INIT_MEMMEM;
6228   INIT_READ;
6229   INIT_FREAD;
6230   INIT_PREAD;
6231   INIT_PREAD64;
6232   INIT_READV;
6233   INIT_PREADV;
6234   INIT_PREADV64;
6235   INIT_WRITE;
6236   INIT_FWRITE;
6237   INIT_PWRITE;
6238   INIT_PWRITE64;
6239   INIT_WRITEV;
6240   INIT_PWRITEV;
6241   INIT_PWRITEV64;
6242   INIT_PRCTL;
6243   INIT_LOCALTIME_AND_FRIENDS;
6244   INIT_STRPTIME;
6245   INIT_SCANF;
6246   INIT_ISOC99_SCANF;
6247   INIT_PRINTF;
6248   INIT_PRINTF_L;
6249   INIT_ISOC99_PRINTF;
6250   INIT_FREXP;
6251   INIT_FREXPF_FREXPL;
6252   INIT_GETPWNAM_AND_FRIENDS;
6253   INIT_GETPWNAM_R_AND_FRIENDS;
6254   INIT_GETPWENT;
6255   INIT_FGETPWENT;
6256   INIT_GETPWENT_R;
6257   INIT_SETPWENT;
6258   INIT_CLOCK_GETTIME;
6259   INIT_GETITIMER;
6260   INIT_TIME;
6261   INIT_GLOB;
6262   INIT_WAIT;
6263   INIT_WAIT4;
6264   INIT_INET;
6265   INIT_PTHREAD_GETSCHEDPARAM;
6266   INIT_GETADDRINFO;
6267   INIT_GETNAMEINFO;
6268   INIT_GETSOCKNAME;
6269   INIT_GETHOSTBYNAME;
6270   INIT_GETHOSTBYNAME_R;
6271   INIT_GETHOSTBYNAME2_R;
6272   INIT_GETHOSTBYADDR_R;
6273   INIT_GETHOSTENT_R;
6274   INIT_GETSOCKOPT;
6275   INIT_ACCEPT;
6276   INIT_ACCEPT4;
6277   INIT_MODF;
6278   INIT_RECVMSG;
6279   INIT_SENDMSG;
6280   INIT_GETPEERNAME;
6281   INIT_IOCTL;
6282   INIT_INET_ATON;
6283   INIT_SYSINFO;
6284   INIT_READDIR;
6285   INIT_READDIR64;
6286   INIT_PTRACE;
6287   INIT_SETLOCALE;
6288   INIT_GETCWD;
6289   INIT_GET_CURRENT_DIR_NAME;
6290   INIT_STRTOIMAX;
6291   INIT_MBSTOWCS;
6292   INIT_MBSNRTOWCS;
6293   INIT_WCSTOMBS;
6294   INIT_WCSNRTOMBS;
6295   INIT_WCRTOMB;
6296   INIT_TCGETATTR;
6297   INIT_REALPATH;
6298   INIT_CANONICALIZE_FILE_NAME;
6299   INIT_CONFSTR;
6300   INIT_SCHED_GETAFFINITY;
6301   INIT_SCHED_GETPARAM;
6302   INIT_STRERROR;
6303   INIT_STRERROR_R;
6304   INIT_XPG_STRERROR_R;
6305   INIT_SCANDIR;
6306   INIT_SCANDIR64;
6307   INIT_GETGROUPS;
6308   INIT_POLL;
6309   INIT_PPOLL;
6310   INIT_WORDEXP;
6311   INIT_SIGWAIT;
6312   INIT_SIGWAITINFO;
6313   INIT_SIGTIMEDWAIT;
6314   INIT_SIGSETOPS;
6315   INIT_SIGPENDING;
6316   INIT_SIGPROCMASK;
6317   INIT_BACKTRACE;
6318   INIT__EXIT;
6319   INIT_PTHREAD_MUTEX_LOCK;
6320   INIT_PTHREAD_MUTEX_UNLOCK;
6321   INIT_GETMNTENT;
6322   INIT_GETMNTENT_R;
6323   INIT_STATFS;
6324   INIT_STATFS64;
6325   INIT_STATVFS;
6326   INIT_STATVFS64;
6327   INIT_INITGROUPS;
6328   INIT_ETHER_NTOA_ATON;
6329   INIT_ETHER_HOST;
6330   INIT_ETHER_R;
6331   INIT_SHMCTL;
6332   INIT_RANDOM_R;
6333   INIT_PTHREAD_ATTR_GET;
6334   INIT_PTHREAD_ATTR_GETINHERITSCHED;
6335   INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6336   INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6337   INIT_PTHREAD_MUTEXATTR_GETTYPE;
6338   INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6339   INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6340   INIT_PTHREAD_MUTEXATTR_GETROBUST;
6341   INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6342   INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6343   INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6344   INIT_PTHREAD_CONDATTR_GETPSHARED;
6345   INIT_PTHREAD_CONDATTR_GETCLOCK;
6346   INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6347   INIT_TMPNAM;
6348   INIT_TMPNAM_R;
6349   INIT_TTYNAME_R;
6350   INIT_TEMPNAM;
6351   INIT_PTHREAD_SETNAME_NP;
6352   INIT_SINCOS;
6353   INIT_REMQUO;
6354   INIT_LGAMMA;
6355   INIT_LGAMMA_R;
6356   INIT_LGAMMAL_R;
6357   INIT_DRAND48_R;
6358   INIT_RAND_R;
6359   INIT_GETLINE;
6360   INIT_ICONV;
6361   INIT_TIMES;
6362   INIT_TLS_GET_ADDR;
6363   INIT_LISTXATTR;
6364   INIT_GETXATTR;
6365   INIT_GETRESID;
6366   INIT_GETIFADDRS;
6367   INIT_IF_INDEXTONAME;
6368   INIT_CAPGET;
6369   INIT_AEABI_MEM;
6370   INIT___BZERO;
6371   INIT_FTIME;
6372   INIT_XDR;
6373   INIT_TSEARCH;
6374   INIT_LIBIO_INTERNALS;
6375   INIT_FOPEN;
6376   INIT_FOPEN64;
6377   INIT_OPEN_MEMSTREAM;
6378   INIT_OBSTACK;
6379   INIT_FFLUSH;
6380   INIT_FCLOSE;
6381   INIT_DLOPEN_DLCLOSE;
6382   INIT_GETPASS;
6383   INIT_TIMERFD;
6384   INIT_MLOCKX;
6385   INIT_FOPENCOOKIE;
6386   INIT_SEM;
6387   INIT_PTHREAD_SETCANCEL;
6388   INIT_MINCORE;
6389   INIT_PROCESS_VM_READV;
6390   INIT_CTERMID;
6391   INIT_CTERMID_R;
6392   INIT_RECV_RECVFROM;
6393   INIT_SEND_SENDTO;
6394   INIT_STAT;
6395   INIT_EVENTFD_READ_WRITE;
6396   INIT___XSTAT;
6397   INIT___XSTAT64;
6398   INIT___LXSTAT;
6399   INIT___LXSTAT64;
6400   // FIXME: add other *stat interceptors.
6401   INIT_UTMP;
6402   INIT_UTMPX;
6403   INIT_GETLOADAVG;
6404 }