Daily bump.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_common_interceptors.inc
blob10f321838e82b097194b65a45372c79b0022f4e6
1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Common function interceptors for tools like AddressSanitizer,
9 // ThreadSanitizer, MemorySanitizer, etc.
11 // This file should be included into the tool's interceptor file,
12 // which has to define it's own macros:
13 //   COMMON_INTERCEPTOR_ENTER
14 //   COMMON_INTERCEPTOR_ENTER_NOIGNORE
15 //   COMMON_INTERCEPTOR_READ_RANGE
16 //   COMMON_INTERCEPTOR_WRITE_RANGE
17 //   COMMON_INTERCEPTOR_INITIALIZE_RANGE
18 //   COMMON_INTERCEPTOR_FD_ACQUIRE
19 //   COMMON_INTERCEPTOR_FD_RELEASE
20 //   COMMON_INTERCEPTOR_FD_ACCESS
21 //   COMMON_INTERCEPTOR_SET_THREAD_NAME
22 //   COMMON_INTERCEPTOR_ON_EXIT
23 //   COMMON_INTERCEPTOR_MUTEX_LOCK
24 //   COMMON_INTERCEPTOR_MUTEX_UNLOCK
25 //   COMMON_INTERCEPTOR_MUTEX_REPAIR
26 //   COMMON_INTERCEPTOR_SET_PTHREAD_NAME
27 //   COMMON_INTERCEPTOR_HANDLE_RECVMSG
28 //   COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
29 //===----------------------------------------------------------------------===//
30 #include "interception/interception.h"
31 #include "sanitizer_addrhashmap.h"
32 #include "sanitizer_placement_new.h"
33 #include "sanitizer_platform_interceptors.h"
34 #include "sanitizer_tls_get_addr.h"
36 #include <stdarg.h>
38 #if SANITIZER_WINDOWS && !defined(va_copy)
39 #define va_copy(dst, src) ((dst) = (src))
40 #endif // _WIN32
42 #if SANITIZER_FREEBSD
43 #define pthread_setname_np pthread_set_name_np
44 #endif
46 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
47 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
48 #endif
50 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
51 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
52 #endif
54 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
55 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
56 #endif
58 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
59 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
60 #endif
62 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
63 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
64 #endif
66 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
67 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
68 #endif
70 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
71 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
72 #endif
74 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
75 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
76 #endif
78 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
79 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
80 #endif
82 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
83 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, map) {}
84 #endif
86 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
87 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
88 #endif
90 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
91 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
92   COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
93 #endif
95 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
96 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
97 #endif
99 struct FileMetadata {
100   // For open_memstream().
101   char **addr;
102   SIZE_T *size;
105 struct CommonInterceptorMetadata {
106   enum {
107     CIMT_INVALID = 0,
108     CIMT_FILE
109   } type;
110   union {
111     FileMetadata file;
112   };
115 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
117 static MetadataHashMap *interceptor_metadata_map;
119 #if SI_NOT_WINDOWS
120 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
121                                           const FileMetadata &file) {
122   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
123   CHECK(h.created());
124   h->type = CommonInterceptorMetadata::CIMT_FILE;
125   h->file = file;
128 UNUSED static const FileMetadata *GetInterceptorMetadata(
129     __sanitizer_FILE *addr) {
130   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
131                             /* remove */ false,
132                             /* create */ false);
133   if (h.exists()) {
134     CHECK(!h.created());
135     CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
136     return &h->file;
137   } else {
138     return 0;
139   }
142 UNUSED static void DeleteInterceptorMetadata(void *addr) {
143   MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
144   CHECK(h.exists());
146 #endif  // SI_NOT_WINDOWS
148 #if SANITIZER_INTERCEPT_TEXTDOMAIN
149 INTERCEPTOR(char*, textdomain, const char *domainname) {
150   void *ctx;
151   COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
152   char* domain = REAL(textdomain)(domainname);
153   if (domain) {
154     COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
155   }
156   return domain;
158 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
159 #else
160 #define INIT_TEXTDOMAIN
161 #endif
163 #if SANITIZER_INTERCEPT_STRCMP
164 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
165   return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
168 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
169   void *ctx;
170   COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
171   unsigned char c1, c2;
172   uptr i;
173   for (i = 0;; i++) {
174     c1 = (unsigned char)s1[i];
175     c2 = (unsigned char)s2[i];
176     if (c1 != c2 || c1 == '\0') break;
177   }
178   COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
179   COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
180   return CharCmpX(c1, c2);
183 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
184   if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
185     return internal_strncmp(s1, s2, size);
186   void *ctx;
187   COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
188   unsigned char c1 = 0, c2 = 0;
189   uptr i;
190   for (i = 0; i < size; i++) {
191     c1 = (unsigned char)s1[i];
192     c2 = (unsigned char)s2[i];
193     if (c1 != c2 || c1 == '\0') break;
194   }
195   COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
196   COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
197   return CharCmpX(c1, c2);
200 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
201 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
202 #else
203 #define INIT_STRCMP
204 #define INIT_STRNCMP
205 #endif
207 #if SANITIZER_INTERCEPT_STRCASECMP
208 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
209   int c1_low = ToLower(c1);
210   int c2_low = ToLower(c2);
211   return c1_low - c2_low;
214 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
215   void *ctx;
216   COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
217   unsigned char c1 = 0, c2 = 0;
218   uptr i;
219   for (i = 0;; i++) {
220     c1 = (unsigned char)s1[i];
221     c2 = (unsigned char)s2[i];
222     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
223   }
224   COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
225   COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
226   return CharCaseCmp(c1, c2);
229 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
230   void *ctx;
231   COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
232   unsigned char c1 = 0, c2 = 0;
233   uptr i;
234   for (i = 0; i < n; i++) {
235     c1 = (unsigned char)s1[i];
236     c2 = (unsigned char)s2[i];
237     if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
238   }
239   COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
240   COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
241   return CharCaseCmp(c1, c2);
244 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
245 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
246 #else
247 #define INIT_STRCASECMP
248 #define INIT_STRNCASECMP
249 #endif
251 #if SANITIZER_INTERCEPT_MEMCHR
252 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
253   void *ctx;
254   COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
255   void *res = REAL(memchr)(s, c, n);
256   uptr len = res ? (char*)res - (char*)s + 1 : n;
257   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
258   return res;
261 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
262 #else
263 #define INIT_MEMCHR
264 #endif
266 #if SANITIZER_INTERCEPT_MEMRCHR
267 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
268   void *ctx;
269   COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
270   COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
271   return REAL(memrchr)(s, c, n);
274 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
275 #else
276 #define INIT_MEMRCHR
277 #endif
279 #if SANITIZER_INTERCEPT_FREXP
280 INTERCEPTOR(double, frexp, double x, int *exp) {
281   void *ctx;
282   COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
283   // Assuming frexp() always writes to |exp|.
284   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
285   double res = REAL(frexp)(x, exp);
286   return res;
289 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
290 #else
291 #define INIT_FREXP
292 #endif  // SANITIZER_INTERCEPT_FREXP
294 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
295 INTERCEPTOR(float, frexpf, float x, int *exp) {
296   void *ctx;
297   COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
298   // FIXME: under ASan the call below may write to freed memory and corrupt
299   // its metadata. See
300   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
301   float res = REAL(frexpf)(x, exp);
302   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
303   return res;
306 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
307   void *ctx;
308   COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
309   // FIXME: under ASan the call below may write to freed memory and corrupt
310   // its metadata. See
311   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
312   long double res = REAL(frexpl)(x, exp);
313   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
314   return res;
317 #define INIT_FREXPF_FREXPL           \
318   COMMON_INTERCEPT_FUNCTION(frexpf); \
319   COMMON_INTERCEPT_FUNCTION(frexpl)
320 #else
321 #define INIT_FREXPF_FREXPL
322 #endif  // SANITIZER_INTERCEPT_FREXPF_FREXPL
324 #if SI_NOT_WINDOWS
325 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
326                         SIZE_T iovlen, SIZE_T maxlen) {
327   for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
328     SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
329     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
330     maxlen -= sz;
331   }
334 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
335                        SIZE_T iovlen, SIZE_T maxlen) {
336   COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
337   for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
338     SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
339     COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
340     maxlen -= sz;
341   }
343 #endif
345 #if SANITIZER_INTERCEPT_READ
346 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
347   void *ctx;
348   COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
349   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
350   // FIXME: under ASan the call below may write to freed memory and corrupt
351   // its metadata. See
352   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
353   SSIZE_T res = REAL(read)(fd, ptr, count);
354   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
355   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
356   return res;
358 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
359 #else
360 #define INIT_READ
361 #endif
363 #if SANITIZER_INTERCEPT_PREAD
364 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
365   void *ctx;
366   COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
367   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
368   // FIXME: under ASan the call below may write to freed memory and corrupt
369   // its metadata. See
370   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
371   SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
372   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
373   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
374   return res;
376 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
377 #else
378 #define INIT_PREAD
379 #endif
381 #if SANITIZER_INTERCEPT_PREAD64
382 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
383   void *ctx;
384   COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
385   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
386   // FIXME: under ASan the call below may write to freed memory and corrupt
387   // its metadata. See
388   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
389   SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
390   if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
391   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
392   return res;
394 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
395 #else
396 #define INIT_PREAD64
397 #endif
399 #if SANITIZER_INTERCEPT_READV
400 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
401                         int iovcnt) {
402   void *ctx;
403   COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
404   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
405   SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
406   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
407   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
408   return res;
410 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
411 #else
412 #define INIT_READV
413 #endif
415 #if SANITIZER_INTERCEPT_PREADV
416 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
417             OFF_T offset) {
418   void *ctx;
419   COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
420   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
421   SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
422   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
423   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
424   return res;
426 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
427 #else
428 #define INIT_PREADV
429 #endif
431 #if SANITIZER_INTERCEPT_PREADV64
432 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
433             OFF64_T offset) {
434   void *ctx;
435   COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
436   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
437   SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
438   if (res > 0) write_iovec(ctx, iov, iovcnt, res);
439   if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
440   return res;
442 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
443 #else
444 #define INIT_PREADV64
445 #endif
447 #if SANITIZER_INTERCEPT_WRITE
448 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
449   void *ctx;
450   COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
451   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
452   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
453   SSIZE_T res = REAL(write)(fd, ptr, count);
454   // FIXME: this check should be _before_ the call to REAL(write), not after
455   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
456   return res;
458 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
459 #else
460 #define INIT_WRITE
461 #endif
463 #if SANITIZER_INTERCEPT_PWRITE
464 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
465   void *ctx;
466   COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
467   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
468   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
469   SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
470   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
471   return res;
473 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
474 #else
475 #define INIT_PWRITE
476 #endif
478 #if SANITIZER_INTERCEPT_PWRITE64
479 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
480             OFF64_T offset) {
481   void *ctx;
482   COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
483   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
484   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
485   SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
486   if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
487   return res;
489 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
490 #else
491 #define INIT_PWRITE64
492 #endif
494 #if SANITIZER_INTERCEPT_WRITEV
495 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
496                         int iovcnt) {
497   void *ctx;
498   COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
499   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
500   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
501   SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
502   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
503   return res;
505 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
506 #else
507 #define INIT_WRITEV
508 #endif
510 #if SANITIZER_INTERCEPT_PWRITEV
511 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
512             OFF_T offset) {
513   void *ctx;
514   COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
515   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
516   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
517   SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
518   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
519   return res;
521 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
522 #else
523 #define INIT_PWRITEV
524 #endif
526 #if SANITIZER_INTERCEPT_PWRITEV64
527 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
528             OFF64_T offset) {
529   void *ctx;
530   COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
531   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
532   if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
533   SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
534   if (res > 0) read_iovec(ctx, iov, iovcnt, res);
535   return res;
537 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
538 #else
539 #define INIT_PWRITEV64
540 #endif
542 #if SANITIZER_INTERCEPT_PRCTL
543 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
544             unsigned long arg3,                        // NOLINT
545             unsigned long arg4, unsigned long arg5) {  // NOLINT
546   void *ctx;
547   COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
548   static const int PR_SET_NAME = 15;
549   int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
550   if (option == PR_SET_NAME) {
551     char buff[16];
552     internal_strncpy(buff, (char *)arg2, 15);
553     buff[15] = 0;
554     COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
555   }
556   return res;
558 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
559 #else
560 #define INIT_PRCTL
561 #endif  // SANITIZER_INTERCEPT_PRCTL
563 #if SANITIZER_INTERCEPT_TIME
564 INTERCEPTOR(unsigned long, time, unsigned long *t) {
565   void *ctx;
566   COMMON_INTERCEPTOR_ENTER(ctx, time, t);
567   unsigned long local_t;
568   unsigned long res = REAL(time)(&local_t);
569   if (t && res != (unsigned long)-1) {
570     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
571     *t = local_t;
572   }
573   return res;
575 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
576 #else
577 #define INIT_TIME
578 #endif  // SANITIZER_INTERCEPT_TIME
580 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
581 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
582   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
583   if (tm->tm_zone) {
584     // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
585     // can point to shared memory and tsan would report a data race.
586     COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
587                                         REAL(strlen(tm->tm_zone)) + 1);
588   }
590 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
591   void *ctx;
592   COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
593   __sanitizer_tm *res = REAL(localtime)(timep);
594   if (res) {
595     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
596     unpoison_tm(ctx, res);
597   }
598   return res;
600 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
601   void *ctx;
602   COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
603   __sanitizer_tm *res = REAL(localtime_r)(timep, result);
604   if (res) {
605     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
606     unpoison_tm(ctx, res);
607   }
608   return res;
610 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
611   void *ctx;
612   COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
613   __sanitizer_tm *res = REAL(gmtime)(timep);
614   if (res) {
615     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
616     unpoison_tm(ctx, res);
617   }
618   return res;
620 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
621   void *ctx;
622   COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
623   __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
624   if (res) {
625     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
626     unpoison_tm(ctx, res);
627   }
628   return res;
630 INTERCEPTOR(char *, ctime, unsigned long *timep) {
631   void *ctx;
632   COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
633   // FIXME: under ASan the call below may write to freed memory and corrupt
634   // its metadata. See
635   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
636   char *res = REAL(ctime)(timep);
637   if (res) {
638     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
639     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
640   }
641   return res;
643 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
644   void *ctx;
645   COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
646   // FIXME: under ASan the call below may write to freed memory and corrupt
647   // its metadata. See
648   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
649   char *res = REAL(ctime_r)(timep, result);
650   if (res) {
651     COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
652     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
653   }
654   return res;
656 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
657   void *ctx;
658   COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
659   // FIXME: under ASan the call below may write to freed memory and corrupt
660   // its metadata. See
661   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
662   char *res = REAL(asctime)(tm);
663   if (res) {
664     COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
665     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
666   }
667   return res;
669 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
670   void *ctx;
671   COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
672   // FIXME: under ASan the call below may write to freed memory and corrupt
673   // its metadata. See
674   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
675   char *res = REAL(asctime_r)(tm, result);
676   if (res) {
677     COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
678     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
679   }
680   return res;
682 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
683   void *ctx;
684   COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
685   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
686   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
687   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
688   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
689   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
690   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
691   COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
692   long res = REAL(mktime)(tm);
693   if (res != -1) unpoison_tm(ctx, tm);
694   return res;
696 #define INIT_LOCALTIME_AND_FRIENDS        \
697   COMMON_INTERCEPT_FUNCTION(localtime);   \
698   COMMON_INTERCEPT_FUNCTION(localtime_r); \
699   COMMON_INTERCEPT_FUNCTION(gmtime);      \
700   COMMON_INTERCEPT_FUNCTION(gmtime_r);    \
701   COMMON_INTERCEPT_FUNCTION(ctime);       \
702   COMMON_INTERCEPT_FUNCTION(ctime_r);     \
703   COMMON_INTERCEPT_FUNCTION(asctime);     \
704   COMMON_INTERCEPT_FUNCTION(asctime_r);   \
705   COMMON_INTERCEPT_FUNCTION(mktime);
706 #else
707 #define INIT_LOCALTIME_AND_FRIENDS
708 #endif  // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
710 #if SANITIZER_INTERCEPT_STRPTIME
711 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
712   void *ctx;
713   COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
714   if (format)
715     COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
716   // FIXME: under ASan the call below may write to freed memory and corrupt
717   // its metadata. See
718   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
719   char *res = REAL(strptime)(s, format, tm);
720   if (res) {
721     COMMON_INTERCEPTOR_READ_RANGE(ctx, s, res - s);
722     // Do not call unpoison_tm here, because strptime does not, in fact,
723     // initialize the entire struct tm. For example, tm_zone pointer is left
724     // uninitialized.
725     if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
726   }
727   return res;
729 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
730 #else
731 #define INIT_STRPTIME
732 #endif
734 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
735 #include "sanitizer_common_interceptors_format.inc"
737 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...)                              \
738   {                                                                            \
739     void *ctx;                                                                 \
740     va_list ap;                                                                \
741     va_start(ap, format);                                                      \
742     COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap);                     \
743     int res = WRAP(vname)(__VA_ARGS__, ap);                                    \
744     va_end(ap);                                                                \
745     return res;                                                                \
746   }
748 #endif
750 #if SANITIZER_INTERCEPT_SCANF
752 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...)                    \
753   {                                                                            \
754     void *ctx;                                                                 \
755     COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                         \
756     va_list aq;                                                                \
757     va_copy(aq, ap);                                                           \
758     int res = REAL(vname)(__VA_ARGS__);                                        \
759     if (res > 0)                                                               \
760       scanf_common(ctx, res, allowGnuMalloc, format, aq);                      \
761     va_end(aq);                                                                \
762     return res;                                                                \
763   }
765 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
766 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
768 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
769 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
771 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
772 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
774 #if SANITIZER_INTERCEPT_ISOC99_SCANF
775 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
776 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
778 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
779             va_list ap)
780 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
782 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
783 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
784 #endif  // SANITIZER_INTERCEPT_ISOC99_SCANF
786 INTERCEPTOR(int, scanf, const char *format, ...)
787 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
789 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
790 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
792 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
793 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
795 #if SANITIZER_INTERCEPT_ISOC99_SCANF
796 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
797 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
799 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
800 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
802 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
803 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
804 #endif
806 #endif
808 #if SANITIZER_INTERCEPT_SCANF
809 #define INIT_SCANF                    \
810   COMMON_INTERCEPT_FUNCTION(scanf);   \
811   COMMON_INTERCEPT_FUNCTION(sscanf);  \
812   COMMON_INTERCEPT_FUNCTION(fscanf);  \
813   COMMON_INTERCEPT_FUNCTION(vscanf);  \
814   COMMON_INTERCEPT_FUNCTION(vsscanf); \
815   COMMON_INTERCEPT_FUNCTION(vfscanf);
816 #else
817 #define INIT_SCANF
818 #endif
820 #if SANITIZER_INTERCEPT_ISOC99_SCANF
821 #define INIT_ISOC99_SCANF                      \
822   COMMON_INTERCEPT_FUNCTION(__isoc99_scanf);   \
823   COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf);  \
824   COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf);  \
825   COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf);  \
826   COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
827   COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
828 #else
829 #define INIT_ISOC99_SCANF
830 #endif
832 #if SANITIZER_INTERCEPT_PRINTF
834 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...)                                  \
835   void *ctx;                                                                   \
836   COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__);                           \
837   va_list aq;                                                                  \
838   va_copy(aq, ap);
840 #define VPRINTF_INTERCEPTOR_RETURN()                                           \
841   va_end(aq);
843 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...)                                   \
844   {                                                                            \
845     VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__);                             \
846     if (common_flags()->check_printf)                                          \
847       printf_common(ctx, format, aq);                                          \
848     int res = REAL(vname)(__VA_ARGS__);                                        \
849     VPRINTF_INTERCEPTOR_RETURN();                                              \
850     return res;                                                                \
851   }
853 // FIXME: under ASan the REAL() call below may write to freed memory and
854 // corrupt its metadata. See
855 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
856 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...)                             \
857   {                                                                            \
858     VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__)                         \
859     if (common_flags()->check_printf) {                                        \
860       printf_common(ctx, format, aq);                                          \
861     }                                                                          \
862     int res = REAL(vname)(str, __VA_ARGS__);                                   \
863     if (res >= 0) {                                                            \
864       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1);                       \
865     }                                                                          \
866     VPRINTF_INTERCEPTOR_RETURN();                                              \
867     return res;                                                                \
868   }
870 // FIXME: under ASan the REAL() call below may write to freed memory and
871 // corrupt its metadata. See
872 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
873 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...)                      \
874   {                                                                            \
875     VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__)                   \
876     if (common_flags()->check_printf) {                                        \
877       printf_common(ctx, format, aq);                                          \
878     }                                                                          \
879     int res = REAL(vname)(str, size, __VA_ARGS__);                             \
880     if (res >= 0) {                                                            \
881       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1)));  \
882     }                                                                          \
883     VPRINTF_INTERCEPTOR_RETURN();                                              \
884     return res;                                                                \
885   }
887 // FIXME: under ASan the REAL() call below may write to freed memory and
888 // corrupt its metadata. See
889 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
890 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...)                           \
891   {                                                                            \
892     VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__)                        \
893     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *));                 \
894     if (common_flags()->check_printf) {                                        \
895       printf_common(ctx, format, aq);                                          \
896     }                                                                          \
897     int res = REAL(vname)(strp, __VA_ARGS__);                                  \
898     if (res >= 0) {                                                            \
899       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1);                     \
900     }                                                                          \
901     VPRINTF_INTERCEPTOR_RETURN();                                              \
902     return res;                                                                \
903   }
905 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
906 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
908 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
909             va_list ap)
910 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
912 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
913             va_list ap)
914 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
916 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
917 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
919 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
920 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
922 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
923 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
924 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
926 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
927             const char *format, va_list ap)
928 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
930 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
931             va_list ap)
932 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
934 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
935             va_list ap)
936 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
937                           ap)
939 #endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
941 INTERCEPTOR(int, printf, const char *format, ...)
942 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
944 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
945 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
947 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
948 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
950 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
951 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
953 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
954 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
956 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
957 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
958 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
960 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
961             ...)
962 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
964 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
965 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
967 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
968             const char *format, ...)
969 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
970                         format)
972 #endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF
974 #endif  // SANITIZER_INTERCEPT_PRINTF
976 #if SANITIZER_INTERCEPT_PRINTF
977 #define INIT_PRINTF                     \
978   COMMON_INTERCEPT_FUNCTION(printf);    \
979   COMMON_INTERCEPT_FUNCTION(sprintf);   \
980   COMMON_INTERCEPT_FUNCTION(snprintf);  \
981   COMMON_INTERCEPT_FUNCTION(asprintf);  \
982   COMMON_INTERCEPT_FUNCTION(fprintf);   \
983   COMMON_INTERCEPT_FUNCTION(vprintf);   \
984   COMMON_INTERCEPT_FUNCTION(vsprintf);  \
985   COMMON_INTERCEPT_FUNCTION(vsnprintf); \
986   COMMON_INTERCEPT_FUNCTION(vasprintf); \
987   COMMON_INTERCEPT_FUNCTION(vfprintf);
988 #else
989 #define INIT_PRINTF
990 #endif
992 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
993 #define INIT_ISOC99_PRINTF                       \
994   COMMON_INTERCEPT_FUNCTION(__isoc99_printf);    \
995   COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf);   \
996   COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf);  \
997   COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf);   \
998   COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf);   \
999   COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf);  \
1000   COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1001   COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1002 #else
1003 #define INIT_ISOC99_PRINTF
1004 #endif
1006 #if SANITIZER_INTERCEPT_IOCTL
1007 #include "sanitizer_common_interceptors_ioctl.inc"
1008 INTERCEPTOR(int, ioctl, int d, unsigned request, void *arg) {
1009   void *ctx;
1010   COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1012   CHECK(ioctl_initialized);
1014   // Note: TSan does not use common flags, and they are zero-initialized.
1015   // This effectively disables ioctl handling in TSan.
1016   if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1018   const ioctl_desc *desc = ioctl_lookup(request);
1019   ioctl_desc decoded_desc;
1020   if (!desc) {
1021     VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1022     if (!ioctl_decode(request, &decoded_desc))
1023       Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1024     else
1025       desc = &decoded_desc;
1026   }
1028   if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1029   int res = REAL(ioctl)(d, request, arg);
1030   // FIXME: some ioctls have different return values for success and failure.
1031   if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1032   return res;
1034 #define INIT_IOCTL \
1035   ioctl_init();    \
1036   COMMON_INTERCEPT_FUNCTION(ioctl);
1037 #else
1038 #define INIT_IOCTL
1039 #endif
1041 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1042     SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1043     SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1044 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1045   if (pwd) {
1046     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1047     if (pwd->pw_name)
1048       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1049                                           REAL(strlen)(pwd->pw_name) + 1);
1050     if (pwd->pw_passwd)
1051       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1052                                           REAL(strlen)(pwd->pw_passwd) + 1);
1053 #if !SANITIZER_ANDROID
1054     if (pwd->pw_gecos)
1055       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1056                                           REAL(strlen)(pwd->pw_gecos) + 1);
1057 #endif
1058 #if SANITIZER_MAC
1059     if (pwd->pw_class)
1060       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1061                                           REAL(strlen)(pwd->pw_class) + 1);
1062 #endif
1063     if (pwd->pw_dir)
1064       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1065                                           REAL(strlen)(pwd->pw_dir) + 1);
1066     if (pwd->pw_shell)
1067       COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1068                                           REAL(strlen)(pwd->pw_shell) + 1);
1069   }
1072 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1073   if (grp) {
1074     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1075     if (grp->gr_name)
1076       COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1077                                           REAL(strlen)(grp->gr_name) + 1);
1078     if (grp->gr_passwd)
1079       COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1080                                           REAL(strlen)(grp->gr_passwd) + 1);
1081     char **p = grp->gr_mem;
1082     for (; *p; ++p) {
1083       COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1084     }
1085     COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1086                                         (p - grp->gr_mem + 1) * sizeof(*p));
1087   }
1089 #endif  // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1090         // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1091         // SANITIZER_INTERCEPT_GETPWENT_R ||
1092         // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1094 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1095 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1096   void *ctx;
1097   COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1098   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1099   __sanitizer_passwd *res = REAL(getpwnam)(name);
1100   if (res != 0) unpoison_passwd(ctx, res);
1101   return res;
1103 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1104   void *ctx;
1105   COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1106   __sanitizer_passwd *res = REAL(getpwuid)(uid);
1107   if (res != 0) unpoison_passwd(ctx, res);
1108   return res;
1110 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1111   void *ctx;
1112   COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1113   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1114   __sanitizer_group *res = REAL(getgrnam)(name);
1115   if (res != 0) unpoison_group(ctx, res);
1116   return res;
1118 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1119   void *ctx;
1120   COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1121   __sanitizer_group *res = REAL(getgrgid)(gid);
1122   if (res != 0) unpoison_group(ctx, res);
1123   return res;
1125 #define INIT_GETPWNAM_AND_FRIENDS      \
1126   COMMON_INTERCEPT_FUNCTION(getpwnam); \
1127   COMMON_INTERCEPT_FUNCTION(getpwuid); \
1128   COMMON_INTERCEPT_FUNCTION(getgrnam); \
1129   COMMON_INTERCEPT_FUNCTION(getgrgid);
1130 #else
1131 #define INIT_GETPWNAM_AND_FRIENDS
1132 #endif
1134 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1135 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1136             char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1137   void *ctx;
1138   COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1139   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1140   // FIXME: under ASan the call below may write to freed memory and corrupt
1141   // its metadata. See
1142   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1143   int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1144   if (!res) {
1145     if (result && *result) unpoison_passwd(ctx, *result);
1146     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1147   }
1148   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1149   return res;
1151 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1152             SIZE_T buflen, __sanitizer_passwd **result) {
1153   void *ctx;
1154   COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1155   // FIXME: under ASan the call below may write to freed memory and corrupt
1156   // its metadata. See
1157   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1158   int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1159   if (!res) {
1160     if (result && *result) unpoison_passwd(ctx, *result);
1161     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1162   }
1163   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1164   return res;
1166 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1167             char *buf, SIZE_T buflen, __sanitizer_group **result) {
1168   void *ctx;
1169   COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1170   COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1171   // FIXME: under ASan the call below may write to freed memory and corrupt
1172   // its metadata. See
1173   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1174   int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1175   if (!res) {
1176     if (result && *result) unpoison_group(ctx, *result);
1177     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1178   }
1179   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1180   return res;
1182 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1183             SIZE_T buflen, __sanitizer_group **result) {
1184   void *ctx;
1185   COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1186   // FIXME: under ASan the call below may write to freed memory and corrupt
1187   // its metadata. See
1188   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1189   int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1190   if (!res) {
1191     if (result && *result) unpoison_group(ctx, *result);
1192     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1193   }
1194   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1195   return res;
1197 #define INIT_GETPWNAM_R_AND_FRIENDS      \
1198   COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1199   COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1200   COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1201   COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1202 #else
1203 #define INIT_GETPWNAM_R_AND_FRIENDS
1204 #endif
1206 #if SANITIZER_INTERCEPT_GETPWENT
1207 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1208   void *ctx;
1209   COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1210   __sanitizer_passwd *res = REAL(getpwent)(dummy);
1211   if (res != 0) unpoison_passwd(ctx, res);
1212   return res;
1214 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1215   void *ctx;
1216   COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1217   __sanitizer_group *res = REAL(getgrent)(dummy);
1218   if (res != 0) unpoison_group(ctx, res);;
1219   return res;
1221 #define INIT_GETPWENT                  \
1222   COMMON_INTERCEPT_FUNCTION(getpwent); \
1223   COMMON_INTERCEPT_FUNCTION(getgrent);
1224 #else
1225 #define INIT_GETPWENT
1226 #endif
1228 #if SANITIZER_INTERCEPT_FGETPWENT
1229 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1230   void *ctx;
1231   COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1232   __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1233   if (res != 0) unpoison_passwd(ctx, res);
1234   return res;
1236 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1237   void *ctx;
1238   COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1239   __sanitizer_group *res = REAL(fgetgrent)(fp);
1240   if (res != 0) unpoison_group(ctx, res);
1241   return res;
1243 #define INIT_FGETPWENT                  \
1244   COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1245   COMMON_INTERCEPT_FUNCTION(fgetgrent);
1246 #else
1247 #define INIT_FGETPWENT
1248 #endif
1250 #if SANITIZER_INTERCEPT_GETPWENT_R
1251 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1252             SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1253   void *ctx;
1254   COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1255   // FIXME: under ASan the call below may write to freed memory and corrupt
1256   // its metadata. See
1257   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1258   int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1259   if (!res) {
1260     if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1261     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1262   }
1263   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1264   return res;
1266 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1267             SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1268   void *ctx;
1269   COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1270   // FIXME: under ASan the call below may write to freed memory and corrupt
1271   // its metadata. See
1272   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1273   int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1274   if (!res) {
1275     if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1276     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1277   }
1278   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1279   return res;
1281 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1282             __sanitizer_group **pwbufp) {
1283   void *ctx;
1284   COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1285   // FIXME: under ASan the call below may write to freed memory and corrupt
1286   // its metadata. See
1287   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1288   int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1289   if (!res) {
1290     if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1291     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1292   }
1293   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1294   return res;
1296 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1297             SIZE_T buflen, __sanitizer_group **pwbufp) {
1298   void *ctx;
1299   COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1300   // FIXME: under ASan the call below may write to freed memory and corrupt
1301   // its metadata. See
1302   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1303   int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1304   if (!res) {
1305     if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1306     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1307   }
1308   if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1309   return res;
1311 #define INIT_GETPWENT_R                   \
1312   COMMON_INTERCEPT_FUNCTION(getpwent_r);  \
1313   COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1314   COMMON_INTERCEPT_FUNCTION(getgrent_r);  \
1315   COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1316 #else
1317 #define INIT_GETPWENT_R
1318 #endif
1320 #if SANITIZER_INTERCEPT_SETPWENT
1321 // The only thing these interceptors do is disable any nested interceptors.
1322 // These functions may open nss modules and call uninstrumented functions from
1323 // them, and we don't want things like strlen() to trigger.
1324 INTERCEPTOR(void, setpwent, int dummy) {
1325   void *ctx;
1326   COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1327   REAL(setpwent)(dummy);
1329 INTERCEPTOR(void, endpwent, int dummy) {
1330   void *ctx;
1331   COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1332   REAL(endpwent)(dummy);
1334 INTERCEPTOR(void, setgrent, int dummy) {
1335   void *ctx;
1336   COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1337   REAL(setgrent)(dummy);
1339 INTERCEPTOR(void, endgrent, int dummy) {
1340   void *ctx;
1341   COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1342   REAL(endgrent)(dummy);
1344 #define INIT_SETPWENT                  \
1345   COMMON_INTERCEPT_FUNCTION(setpwent); \
1346   COMMON_INTERCEPT_FUNCTION(endpwent); \
1347   COMMON_INTERCEPT_FUNCTION(setgrent); \
1348   COMMON_INTERCEPT_FUNCTION(endgrent);
1349 #else
1350 #define INIT_SETPWENT
1351 #endif
1353 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1354 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1355   void *ctx;
1356   COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1357   // FIXME: under ASan the call below may write to freed memory and corrupt
1358   // its metadata. See
1359   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1360   int res = REAL(clock_getres)(clk_id, tp);
1361   if (!res && tp) {
1362     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1363   }
1364   return res;
1366 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1367   void *ctx;
1368   COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1369   // FIXME: under ASan the call below may write to freed memory and corrupt
1370   // its metadata. See
1371   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1372   int res = REAL(clock_gettime)(clk_id, tp);
1373   if (!res) {
1374     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1375   }
1376   return res;
1378 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1379   void *ctx;
1380   COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1381   COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1382   return REAL(clock_settime)(clk_id, tp);
1384 #define INIT_CLOCK_GETTIME                  \
1385   COMMON_INTERCEPT_FUNCTION(clock_getres);  \
1386   COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1387   COMMON_INTERCEPT_FUNCTION(clock_settime);
1388 #else
1389 #define INIT_CLOCK_GETTIME
1390 #endif
1392 #if SANITIZER_INTERCEPT_GETITIMER
1393 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1394   void *ctx;
1395   COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1396   // FIXME: under ASan the call below may write to freed memory and corrupt
1397   // its metadata. See
1398   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1399   int res = REAL(getitimer)(which, curr_value);
1400   if (!res && curr_value) {
1401     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1402   }
1403   return res;
1405 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1406   void *ctx;
1407   COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1408   if (new_value)
1409     COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1410   // FIXME: under ASan the call below may write to freed memory and corrupt
1411   // its metadata. See
1412   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1413   int res = REAL(setitimer)(which, new_value, old_value);
1414   if (!res && old_value) {
1415     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1416   }
1417   return res;
1419 #define INIT_GETITIMER                  \
1420   COMMON_INTERCEPT_FUNCTION(getitimer); \
1421   COMMON_INTERCEPT_FUNCTION(setitimer);
1422 #else
1423 #define INIT_GETITIMER
1424 #endif
1426 #if SANITIZER_INTERCEPT_GLOB
1427 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1428   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1429   // +1 for NULL pointer at the end.
1430   if (pglob->gl_pathv)
1431     COMMON_INTERCEPTOR_WRITE_RANGE(
1432         ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1433   for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1434     char *p = pglob->gl_pathv[i];
1435     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1436   }
1439 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1441 static void wrapped_gl_closedir(void *dir) {
1442   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1443   IndirectExternCall(pglob_copy->gl_closedir)(dir);
1446 static void *wrapped_gl_readdir(void *dir) {
1447   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1448   return IndirectExternCall(pglob_copy->gl_readdir)(dir);
1451 static void *wrapped_gl_opendir(const char *s) {
1452   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1453   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1454   return IndirectExternCall(pglob_copy->gl_opendir)(s);
1457 static int wrapped_gl_lstat(const char *s, void *st) {
1458   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1459   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1460   return IndirectExternCall(pglob_copy->gl_lstat)(s, st);
1463 static int wrapped_gl_stat(const char *s, void *st) {
1464   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1465   COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1466   return IndirectExternCall(pglob_copy->gl_stat)(s, st);
1469 INTERCEPTOR(int, glob, const char *pattern, int flags,
1470             int (*errfunc)(const char *epath, int eerrno),
1471             __sanitizer_glob_t *pglob) {
1472   void *ctx;
1473   COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1474   __sanitizer_glob_t glob_copy = {
1475       0,                  0,                   0,
1476       0,                  wrapped_gl_closedir, wrapped_gl_readdir,
1477       wrapped_gl_opendir, wrapped_gl_lstat,    wrapped_gl_stat};
1478   if (flags & glob_altdirfunc) {
1479     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1480     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1481     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1482     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1483     Swap(pglob->gl_stat, glob_copy.gl_stat);
1484     pglob_copy = &glob_copy;
1485   }
1486   int res = REAL(glob)(pattern, flags, errfunc, pglob);
1487   if (flags & glob_altdirfunc) {
1488     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1489     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1490     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1491     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1492     Swap(pglob->gl_stat, glob_copy.gl_stat);
1493   }
1494   pglob_copy = 0;
1495   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1496   return res;
1499 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1500             int (*errfunc)(const char *epath, int eerrno),
1501             __sanitizer_glob_t *pglob) {
1502   void *ctx;
1503   COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1504   __sanitizer_glob_t glob_copy = {
1505       0,                  0,                   0,
1506       0,                  wrapped_gl_closedir, wrapped_gl_readdir,
1507       wrapped_gl_opendir, wrapped_gl_lstat,    wrapped_gl_stat};
1508   if (flags & glob_altdirfunc) {
1509     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1510     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1511     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1512     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1513     Swap(pglob->gl_stat, glob_copy.gl_stat);
1514     pglob_copy = &glob_copy;
1515   }
1516   int res = REAL(glob64)(pattern, flags, errfunc, pglob);
1517   if (flags & glob_altdirfunc) {
1518     Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1519     Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1520     Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1521     Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1522     Swap(pglob->gl_stat, glob_copy.gl_stat);
1523   }
1524   pglob_copy = 0;
1525   if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1526   return res;
1528 #define INIT_GLOB                  \
1529   COMMON_INTERCEPT_FUNCTION(glob); \
1530   COMMON_INTERCEPT_FUNCTION(glob64);
1531 #else  // SANITIZER_INTERCEPT_GLOB
1532 #define INIT_GLOB
1533 #endif  // SANITIZER_INTERCEPT_GLOB
1535 #if SANITIZER_INTERCEPT_WAIT
1536 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
1537 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
1538 // details.
1539 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
1540   void *ctx;
1541   COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
1542   // FIXME: under ASan the call below may write to freed memory and corrupt
1543   // its metadata. See
1544   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1545   int res = REAL(wait)(status);
1546   if (res != -1 && status)
1547     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1548   return res;
1550 // On FreeBSD id_t is always 64-bit wide.
1551 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
1552 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
1553                         int options) {
1554 #else
1555 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
1556                         int options) {
1557 #endif
1558   void *ctx;
1559   COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
1560   // FIXME: under ASan the call below may write to freed memory and corrupt
1561   // its metadata. See
1562   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1563   int res = REAL(waitid)(idtype, id, infop, options);
1564   if (res != -1 && infop)
1565     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
1566   return res;
1568 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
1569   void *ctx;
1570   COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
1571   // FIXME: under ASan the call below may write to freed memory and corrupt
1572   // its metadata. See
1573   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1574   int res = REAL(waitpid)(pid, status, options);
1575   if (res != -1 && status)
1576     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1577   return res;
1579 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
1580   void *ctx;
1581   COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
1582   // FIXME: under ASan the call below may write to freed memory and corrupt
1583   // its metadata. See
1584   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1585   int res = REAL(wait3)(status, options, rusage);
1586   if (res != -1) {
1587     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1588     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1589   }
1590   return res;
1592 #if SANITIZER_ANDROID
1593 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
1594   void *ctx;
1595   COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
1596   // FIXME: under ASan the call below may write to freed memory and corrupt
1597   // its metadata. See
1598   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1599   int res = REAL(__wait4)(pid, status, options, rusage);
1600   if (res != -1) {
1601     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1602     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1603   }
1604   return res;
1606 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
1607 #else
1608 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
1609   void *ctx;
1610   COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
1611   // FIXME: under ASan the call below may write to freed memory and corrupt
1612   // its metadata. See
1613   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1614   int res = REAL(wait4)(pid, status, options, rusage);
1615   if (res != -1) {
1616     if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1617     if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1618   }
1619   return res;
1621 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
1622 #endif  // SANITIZER_ANDROID
1623 #define INIT_WAIT                     \
1624   COMMON_INTERCEPT_FUNCTION(wait);    \
1625   COMMON_INTERCEPT_FUNCTION(waitid);  \
1626   COMMON_INTERCEPT_FUNCTION(waitpid); \
1627   COMMON_INTERCEPT_FUNCTION(wait3);
1628 #else
1629 #define INIT_WAIT
1630 #define INIT_WAIT4
1631 #endif
1633 #if SANITIZER_INTERCEPT_INET
1634 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
1635   void *ctx;
1636   COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
1637   uptr sz = __sanitizer_in_addr_sz(af);
1638   if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
1639   // FIXME: figure out read size based on the address family.
1640   // FIXME: under ASan the call below may write to freed memory and corrupt
1641   // its metadata. See
1642   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1643   char *res = REAL(inet_ntop)(af, src, dst, size);
1644   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1645   return res;
1647 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
1648   void *ctx;
1649   COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
1650   // FIXME: figure out read size based on the address family.
1651   // FIXME: under ASan the call below may write to freed memory and corrupt
1652   // its metadata. See
1653   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1654   int res = REAL(inet_pton)(af, src, dst);
1655   if (res == 1) {
1656     uptr sz = __sanitizer_in_addr_sz(af);
1657     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1658   }
1659   return res;
1661 #define INIT_INET                       \
1662   COMMON_INTERCEPT_FUNCTION(inet_ntop); \
1663   COMMON_INTERCEPT_FUNCTION(inet_pton);
1664 #else
1665 #define INIT_INET
1666 #endif
1668 #if SANITIZER_INTERCEPT_INET
1669 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
1670   void *ctx;
1671   COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
1672   if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
1673   // FIXME: under ASan the call below may write to freed memory and corrupt
1674   // its metadata. See
1675   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1676   int res = REAL(inet_aton)(cp, dst);
1677   if (res != 0) {
1678     uptr sz = __sanitizer_in_addr_sz(af_inet);
1679     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1680   }
1681   return res;
1683 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
1684 #else
1685 #define INIT_INET_ATON
1686 #endif
1688 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
1689 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
1690   void *ctx;
1691   COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
1692   // FIXME: under ASan the call below may write to freed memory and corrupt
1693   // its metadata. See
1694   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1695   int res = REAL(pthread_getschedparam)(thread, policy, param);
1696   if (res == 0) {
1697     if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
1698     if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
1699   }
1700   return res;
1702 #define INIT_PTHREAD_GETSCHEDPARAM \
1703   COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
1704 #else
1705 #define INIT_PTHREAD_GETSCHEDPARAM
1706 #endif
1708 #if SANITIZER_INTERCEPT_GETADDRINFO
1709 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
1710             struct __sanitizer_addrinfo *hints,
1711             struct __sanitizer_addrinfo **out) {
1712   void *ctx;
1713   COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
1714   if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
1715   if (service)
1716     COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
1717   if (hints)
1718     COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
1719   // FIXME: under ASan the call below may write to freed memory and corrupt
1720   // its metadata. See
1721   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1722   int res = REAL(getaddrinfo)(node, service, hints, out);
1723   if (res == 0 && out) {
1724     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
1725     struct __sanitizer_addrinfo *p = *out;
1726     while (p) {
1727       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
1728       if (p->ai_addr)
1729         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
1730       if (p->ai_canonname)
1731         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
1732                                        REAL(strlen)(p->ai_canonname) + 1);
1733       p = p->ai_next;
1734     }
1735   }
1736   return res;
1738 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
1739 #else
1740 #define INIT_GETADDRINFO
1741 #endif
1743 #if SANITIZER_INTERCEPT_GETNAMEINFO
1744 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
1745             unsigned hostlen, char *serv, unsigned servlen, int flags) {
1746   void *ctx;
1747   COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
1748                            serv, servlen, flags);
1749   // FIXME: consider adding READ_RANGE(sockaddr, salen)
1750   // There is padding in in_addr that may make this too noisy
1751   // FIXME: under ASan the call below may write to freed memory and corrupt
1752   // its metadata. See
1753   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1754   int res =
1755       REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
1756   if (res == 0) {
1757     if (host && hostlen)
1758       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
1759     if (serv && servlen)
1760       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
1761   }
1762   return res;
1764 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
1765 #else
1766 #define INIT_GETNAMEINFO
1767 #endif
1769 #if SANITIZER_INTERCEPT_GETSOCKNAME
1770 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
1771   void *ctx;
1772   COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
1773   COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1774   int addrlen_in = *addrlen;
1775   // FIXME: under ASan the call below may write to freed memory and corrupt
1776   // its metadata. See
1777   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1778   int res = REAL(getsockname)(sock_fd, addr, addrlen);
1779   if (res == 0) {
1780     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
1781   }
1782   return res;
1784 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
1785 #else
1786 #define INIT_GETSOCKNAME
1787 #endif
1789 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1790 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
1791   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
1792   if (h->h_name)
1793     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
1794   char **p = h->h_aliases;
1795   while (*p) {
1796     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1797     ++p;
1798   }
1799   COMMON_INTERCEPTOR_WRITE_RANGE(
1800       ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
1801   p = h->h_addr_list;
1802   while (*p) {
1803     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
1804     ++p;
1805   }
1806   COMMON_INTERCEPTOR_WRITE_RANGE(
1807       ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
1809 #endif
1811 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
1812 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
1813   void *ctx;
1814   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
1815   struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
1816   if (res) write_hostent(ctx, res);
1817   return res;
1820 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
1821             int type) {
1822   void *ctx;
1823   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
1824   COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1825   struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
1826   if (res) write_hostent(ctx, res);
1827   return res;
1830 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
1831   void *ctx;
1832   COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
1833   struct __sanitizer_hostent *res = REAL(gethostent)(fake);
1834   if (res) write_hostent(ctx, res);
1835   return res;
1838 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
1839   void *ctx;
1840   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
1841   struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
1842   if (res) write_hostent(ctx, res);
1843   return res;
1845 #define INIT_GETHOSTBYNAME                  \
1846   COMMON_INTERCEPT_FUNCTION(gethostent);    \
1847   COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
1848   COMMON_INTERCEPT_FUNCTION(gethostbyname); \
1849   COMMON_INTERCEPT_FUNCTION(gethostbyname2);
1850 #else
1851 #define INIT_GETHOSTBYNAME
1852 #endif
1854 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1855 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
1856             char *buf, SIZE_T buflen, __sanitizer_hostent **result,
1857             int *h_errnop) {
1858   void *ctx;
1859   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
1860                            h_errnop);
1861   // FIXME: under ASan the call below may write to freed memory and corrupt
1862   // its metadata. See
1863   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1864   int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
1865   if (result) {
1866     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1867     if (res == 0 && *result) write_hostent(ctx, *result);
1868   }
1869   if (h_errnop)
1870     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1871   return res;
1873 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
1874 #else
1875 #define INIT_GETHOSTBYNAME_R
1876 #endif
1878 #if SANITIZER_INTERCEPT_GETHOSTENT_R
1879 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
1880             SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
1881   void *ctx;
1882   COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
1883                            h_errnop);
1884   // FIXME: under ASan the call below may write to freed memory and corrupt
1885   // its metadata. See
1886   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1887   int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
1888   if (result) {
1889     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1890     if (res == 0 && *result) write_hostent(ctx, *result);
1891   }
1892   if (h_errnop)
1893     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1894   return res;
1896 #define INIT_GETHOSTENT_R                  \
1897   COMMON_INTERCEPT_FUNCTION(gethostent_r);
1898 #else
1899 #define INIT_GETHOSTENT_R
1900 #endif
1902 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
1903 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
1904             struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1905             __sanitizer_hostent **result, int *h_errnop) {
1906   void *ctx;
1907   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
1908                            buflen, result, h_errnop);
1909   COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1910   // FIXME: under ASan the call below may write to freed memory and corrupt
1911   // its metadata. See
1912   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1913   int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
1914                                   h_errnop);
1915   if (result) {
1916     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1917     if (res == 0 && *result) write_hostent(ctx, *result);
1918   }
1919   if (h_errnop)
1920     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1921   return res;
1923 #define INIT_GETHOSTBYADDR_R                  \
1924   COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
1925 #else
1926 #define INIT_GETHOSTBYADDR_R
1927 #endif
1929 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
1930 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
1931             struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1932             __sanitizer_hostent **result, int *h_errnop) {
1933   void *ctx;
1934   COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
1935                            result, h_errnop);
1936   // FIXME: under ASan the call below may write to freed memory and corrupt
1937   // its metadata. See
1938   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1939   int res =
1940       REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
1941   if (result) {
1942     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1943     if (res == 0 && *result) write_hostent(ctx, *result);
1944   }
1945   if (h_errnop)
1946     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1947   return res;
1949 #define INIT_GETHOSTBYNAME2_R                  \
1950   COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
1951 #else
1952 #define INIT_GETHOSTBYNAME2_R
1953 #endif
1955 #if SANITIZER_INTERCEPT_GETSOCKOPT
1956 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
1957             int *optlen) {
1958   void *ctx;
1959   COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
1960                            optlen);
1961   if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
1962   // FIXME: under ASan the call below may write to freed memory and corrupt
1963   // its metadata. See
1964   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1965   int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
1966   if (res == 0)
1967     if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
1968   return res;
1970 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
1971 #else
1972 #define INIT_GETSOCKOPT
1973 #endif
1975 #if SANITIZER_INTERCEPT_ACCEPT
1976 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
1977   void *ctx;
1978   COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
1979   unsigned addrlen0 = 0;
1980   if (addrlen) {
1981     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1982     addrlen0 = *addrlen;
1983   }
1984   int fd2 = REAL(accept)(fd, addr, addrlen);
1985   if (fd2 >= 0) {
1986     if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1987     if (addr && addrlen)
1988       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1989   }
1990   return fd2;
1992 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
1993 #else
1994 #define INIT_ACCEPT
1995 #endif
1997 #if SANITIZER_INTERCEPT_ACCEPT4
1998 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
1999   void *ctx;
2000   COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2001   unsigned addrlen0 = 0;
2002   if (addrlen) {
2003     COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2004     addrlen0 = *addrlen;
2005   }
2006   // FIXME: under ASan the call below may write to freed memory and corrupt
2007   // its metadata. See
2008   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2009   int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2010   if (fd2 >= 0) {
2011     if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2012     if (addr && addrlen)
2013       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2014   }
2015   return fd2;
2017 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2018 #else
2019 #define INIT_ACCEPT4
2020 #endif
2022 #if SANITIZER_INTERCEPT_MODF
2023 INTERCEPTOR(double, modf, double x, double *iptr) {
2024   void *ctx;
2025   COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2026   // FIXME: under ASan the call below may write to freed memory and corrupt
2027   // its metadata. See
2028   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2029   double res = REAL(modf)(x, iptr);
2030   if (iptr) {
2031     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2032   }
2033   return res;
2035 INTERCEPTOR(float, modff, float x, float *iptr) {
2036   void *ctx;
2037   COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2038   // FIXME: under ASan the call below may write to freed memory and corrupt
2039   // its metadata. See
2040   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2041   float res = REAL(modff)(x, iptr);
2042   if (iptr) {
2043     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2044   }
2045   return res;
2047 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2048   void *ctx;
2049   COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2050   // FIXME: under ASan the call below may write to freed memory and corrupt
2051   // its metadata. See
2052   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2053   long double res = REAL(modfl)(x, iptr);
2054   if (iptr) {
2055     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2056   }
2057   return res;
2059 #define INIT_MODF                   \
2060   COMMON_INTERCEPT_FUNCTION(modf);  \
2061   COMMON_INTERCEPT_FUNCTION(modff); \
2062   COMMON_INTERCEPT_FUNCTION(modfl);
2063 #else
2064 #define INIT_MODF
2065 #endif
2067 #if SANITIZER_INTERCEPT_RECVMSG
2068 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2069                          SSIZE_T maxlen) {
2070   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2071   if (msg->msg_name && msg->msg_namelen)
2072     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2073   if (msg->msg_iov && msg->msg_iovlen)
2074     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2075                                    sizeof(*msg->msg_iov) * msg->msg_iovlen);
2076   write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2077   if (msg->msg_control && msg->msg_controllen)
2078     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2081 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2082             int flags) {
2083   void *ctx;
2084   COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2085   // FIXME: under ASan the call below may write to freed memory and corrupt
2086   // its metadata. See
2087   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2088   SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2089   if (res >= 0) {
2090     if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2091     if (msg) {
2092       write_msghdr(ctx, msg, res);
2093       COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2094     }
2095   }
2096   return res;
2098 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2099 #else
2100 #define INIT_RECVMSG
2101 #endif
2103 #if SANITIZER_INTERCEPT_GETPEERNAME
2104 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2105   void *ctx;
2106   COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2107   unsigned addr_sz;
2108   if (addrlen) addr_sz = *addrlen;
2109   // FIXME: under ASan the call below may write to freed memory and corrupt
2110   // its metadata. See
2111   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2112   int res = REAL(getpeername)(sockfd, addr, addrlen);
2113   if (!res && addr && addrlen)
2114     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2115   return res;
2117 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2118 #else
2119 #define INIT_GETPEERNAME
2120 #endif
2122 #if SANITIZER_INTERCEPT_SYSINFO
2123 INTERCEPTOR(int, sysinfo, void *info) {
2124   void *ctx;
2125   // FIXME: under ASan the call below may write to freed memory and corrupt
2126   // its metadata. See
2127   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2128   COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2129   int res = REAL(sysinfo)(info);
2130   if (!res && info)
2131     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2132   return res;
2134 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2135 #else
2136 #define INIT_SYSINFO
2137 #endif
2139 #if SANITIZER_INTERCEPT_READDIR
2140 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2141   void *ctx;
2142   COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2143   // FIXME: under ASan the call below may write to freed memory and corrupt
2144   // its metadata. See
2145   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2146   __sanitizer_dirent *res = REAL(readdir)(dirp);
2147   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2148   return res;
2151 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2152             __sanitizer_dirent **result) {
2153   void *ctx;
2154   COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2155   // FIXME: under ASan the call below may write to freed memory and corrupt
2156   // its metadata. See
2157   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2158   int res = REAL(readdir_r)(dirp, entry, result);
2159   if (!res) {
2160     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2161     if (*result)
2162       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2163   }
2164   return res;
2167 #define INIT_READDIR                  \
2168   COMMON_INTERCEPT_FUNCTION(readdir); \
2169   COMMON_INTERCEPT_FUNCTION(readdir_r);
2170 #else
2171 #define INIT_READDIR
2172 #endif
2174 #if SANITIZER_INTERCEPT_READDIR64
2175 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2176   void *ctx;
2177   COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2178   // FIXME: under ASan the call below may write to freed memory and corrupt
2179   // its metadata. See
2180   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2181   __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2182   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2183   return res;
2186 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2187             __sanitizer_dirent64 **result) {
2188   void *ctx;
2189   COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2190   // FIXME: under ASan the call below may write to freed memory and corrupt
2191   // its metadata. See
2192   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2193   int res = REAL(readdir64_r)(dirp, entry, result);
2194   if (!res) {
2195     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2196     if (*result)
2197       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2198   }
2199   return res;
2201 #define INIT_READDIR64                  \
2202   COMMON_INTERCEPT_FUNCTION(readdir64); \
2203   COMMON_INTERCEPT_FUNCTION(readdir64_r);
2204 #else
2205 #define INIT_READDIR64
2206 #endif
2208 #if SANITIZER_INTERCEPT_PTRACE
2209 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2210   void *ctx;
2211   COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2213   if (data) {
2214     if (request == ptrace_setregs)
2215       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2216     else if (request == ptrace_setfpregs)
2217       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2218     else if (request == ptrace_setfpxregs)
2219       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2220     else if (request == ptrace_setsiginfo)
2221       COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2222     else if (request == ptrace_setregset) {
2223       __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
2224       COMMON_INTERCEPTOR_READ_RANGE(ctx, iov->iov_base, iov->iov_len);
2225     }
2226   }
2228   // FIXME: under ASan the call below may write to freed memory and corrupt
2229   // its metadata. See
2230   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2231   uptr res = REAL(ptrace)(request, pid, addr, data);
2233   if (!res && data) {
2234     // Note that PEEK* requests assign different meaning to the return value.
2235     // This function does not handle them (nor does it need to).
2236     if (request == ptrace_getregs)
2237       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2238     else if (request == ptrace_getfpregs)
2239       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2240     else if (request == ptrace_getfpxregs)
2241       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2242     else if (request == ptrace_getsiginfo)
2243       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2244     else if (request == ptrace_geteventmsg)
2245       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2246     else if (request == ptrace_getregset) {
2247       __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
2248       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iov->iov_base, iov->iov_len);
2249     }
2250   }
2251   return res;
2254 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2255 #else
2256 #define INIT_PTRACE
2257 #endif
2259 #if SANITIZER_INTERCEPT_SETLOCALE
2260 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2261   void *ctx;
2262   COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2263   if (locale)
2264     COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2265   char *res = REAL(setlocale)(category, locale);
2266   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2267   return res;
2270 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2271 #else
2272 #define INIT_SETLOCALE
2273 #endif
2275 #if SANITIZER_INTERCEPT_GETCWD
2276 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2277   void *ctx;
2278   COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
2279   // FIXME: under ASan the call below may write to freed memory and corrupt
2280   // its metadata. See
2281   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2282   char *res = REAL(getcwd)(buf, size);
2283   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2284   return res;
2286 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2287 #else
2288 #define INIT_GETCWD
2289 #endif
2291 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2292 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2293   void *ctx;
2294   COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2295   // FIXME: under ASan the call below may write to freed memory and corrupt
2296   // its metadata. See
2297   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2298   char *res = REAL(get_current_dir_name)(fake);
2299   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2300   return res;
2303 #define INIT_GET_CURRENT_DIR_NAME \
2304   COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2305 #else
2306 #define INIT_GET_CURRENT_DIR_NAME
2307 #endif
2309 #if SANITIZER_INTERCEPT_STRTOIMAX
2310 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
2311   void *ctx;
2312   COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
2313   // FIXME: under ASan the call below may write to freed memory and corrupt
2314   // its metadata. See
2315   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2316   INTMAX_T res = REAL(strtoimax)(nptr, endptr, base);
2317   if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2318   return res;
2321 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
2322   void *ctx;
2323   COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
2324   // FIXME: under ASan the call below may write to freed memory and corrupt
2325   // its metadata. See
2326   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2327   INTMAX_T res = REAL(strtoumax)(nptr, endptr, base);
2328   if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2329   return res;
2332 #define INIT_STRTOIMAX                  \
2333   COMMON_INTERCEPT_FUNCTION(strtoimax); \
2334   COMMON_INTERCEPT_FUNCTION(strtoumax);
2335 #else
2336 #define INIT_STRTOIMAX
2337 #endif
2339 #if SANITIZER_INTERCEPT_MBSTOWCS
2340 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
2341   void *ctx;
2342   COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
2343   // FIXME: under ASan the call below may write to freed memory and corrupt
2344   // its metadata. See
2345   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2346   SIZE_T res = REAL(mbstowcs)(dest, src, len);
2347   if (res != (SIZE_T) - 1 && dest) {
2348     SIZE_T write_cnt = res + (res < len);
2349     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2350   }
2351   return res;
2354 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
2355             void *ps) {
2356   void *ctx;
2357   COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
2358   if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2359   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2360   // FIXME: under ASan the call below may write to freed memory and corrupt
2361   // its metadata. See
2362   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2363   SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
2364   if (res != (SIZE_T)(-1) && dest && src) {
2365     // This function, and several others, may or may not write the terminating
2366     // \0 character. They write it iff they clear *src.
2367     SIZE_T write_cnt = res + !*src;
2368     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2369   }
2370   return res;
2373 #define INIT_MBSTOWCS                  \
2374   COMMON_INTERCEPT_FUNCTION(mbstowcs); \
2375   COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
2376 #else
2377 #define INIT_MBSTOWCS
2378 #endif
2380 #if SANITIZER_INTERCEPT_MBSNRTOWCS
2381 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
2382             SIZE_T len, void *ps) {
2383   void *ctx;
2384   COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
2385   if (src) {
2386     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2387     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2388   }
2389   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2390   // FIXME: under ASan the call below may write to freed memory and corrupt
2391   // its metadata. See
2392   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2393   SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
2394   if (res != (SIZE_T)(-1) && dest && src) {
2395     SIZE_T write_cnt = res + !*src;
2396     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2397   }
2398   return res;
2401 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
2402 #else
2403 #define INIT_MBSNRTOWCS
2404 #endif
2406 #if SANITIZER_INTERCEPT_WCSTOMBS
2407 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
2408   void *ctx;
2409   COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
2410   // FIXME: under ASan the call below may write to freed memory and corrupt
2411   // its metadata. See
2412   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2413   SIZE_T res = REAL(wcstombs)(dest, src, len);
2414   if (res != (SIZE_T) - 1 && dest) {
2415     SIZE_T write_cnt = res + (res < len);
2416     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2417   }
2418   return res;
2421 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
2422             void *ps) {
2423   void *ctx;
2424   COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
2425   if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2426   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2427   // FIXME: under ASan the call below may write to freed memory and corrupt
2428   // its metadata. See
2429   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2430   SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
2431   if (res != (SIZE_T) - 1 && dest && src) {
2432     SIZE_T write_cnt = res + !*src;
2433     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2434   }
2435   return res;
2438 #define INIT_WCSTOMBS                  \
2439   COMMON_INTERCEPT_FUNCTION(wcstombs); \
2440   COMMON_INTERCEPT_FUNCTION(wcsrtombs);
2441 #else
2442 #define INIT_WCSTOMBS
2443 #endif
2445 #if SANITIZER_INTERCEPT_WCSNRTOMBS
2446 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
2447             SIZE_T len, void *ps) {
2448   void *ctx;
2449   COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
2450   if (src) {
2451     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2452     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2453   }
2454   if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2455   // FIXME: under ASan the call below may write to freed memory and corrupt
2456   // its metadata. See
2457   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2458   SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
2459   if (res != (SIZE_T) - 1 && dest && src) {
2460     SIZE_T write_cnt = res + !*src;
2461     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2462   }
2463   return res;
2466 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
2467 #else
2468 #define INIT_WCSNRTOMBS
2469 #endif
2471 #if SANITIZER_INTERCEPT_TCGETATTR
2472 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
2473   void *ctx;
2474   COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
2475   // FIXME: under ASan the call below may write to freed memory and corrupt
2476   // its metadata. See
2477   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2478   int res = REAL(tcgetattr)(fd, termios_p);
2479   if (!res && termios_p)
2480     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
2481   return res;
2484 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
2485 #else
2486 #define INIT_TCGETATTR
2487 #endif
2489 #if SANITIZER_INTERCEPT_REALPATH
2490 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
2491   void *ctx;
2492   COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
2493   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2495   // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
2496   // version of a versioned symbol. For realpath(), this gives us something
2497   // (called __old_realpath) that does not handle NULL in the second argument.
2498   // Handle it as part of the interceptor.
2499   char *allocated_path = 0;
2500   if (!resolved_path)
2501     allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
2503   char *res = REAL(realpath)(path, resolved_path);
2504   if (allocated_path && !res) WRAP(free)(allocated_path);
2505   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2506   return res;
2508 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
2509 #else
2510 #define INIT_REALPATH
2511 #endif
2513 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
2514 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
2515   void *ctx;
2516   COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
2517   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2518   char *res = REAL(canonicalize_file_name)(path);
2519   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2520   return res;
2522 #define INIT_CANONICALIZE_FILE_NAME \
2523   COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
2524 #else
2525 #define INIT_CANONICALIZE_FILE_NAME
2526 #endif
2528 #if SANITIZER_INTERCEPT_CONFSTR
2529 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
2530   void *ctx;
2531   COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
2532   // FIXME: under ASan the call below may write to freed memory and corrupt
2533   // its metadata. See
2534   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2535   SIZE_T res = REAL(confstr)(name, buf, len);
2536   if (buf && res)
2537     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
2538   return res;
2540 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
2541 #else
2542 #define INIT_CONFSTR
2543 #endif
2545 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
2546 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
2547   void *ctx;
2548   COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
2549   // FIXME: under ASan the call below may write to freed memory and corrupt
2550   // its metadata. See
2551   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2552   int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
2553   if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
2554   return res;
2556 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
2557 #else
2558 #define INIT_SCHED_GETAFFINITY
2559 #endif
2561 #if SANITIZER_INTERCEPT_STRERROR
2562 INTERCEPTOR(char *, strerror, int errnum) {
2563   void *ctx;
2564   COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
2565   char *res = REAL(strerror)(errnum);
2566   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
2567   return res;
2569 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
2570 #else
2571 #define INIT_STRERROR
2572 #endif
2574 #if SANITIZER_INTERCEPT_STRERROR_R
2575 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
2576   void *ctx;
2577   COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
2578   // FIXME: under ASan the call below may write to freed memory and corrupt
2579   // its metadata. See
2580   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2581   char *res = REAL(strerror_r)(errnum, buf, buflen);
2582   // There are 2 versions of strerror_r:
2583   //  * POSIX version returns 0 on success, negative error code on failure,
2584   //    writes message to buf.
2585   //  * GNU version returns message pointer, which points to either buf or some
2586   //    static storage.
2587   SIZE_T posix_res = (SIZE_T)res;
2588   if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
2589     // POSIX version. Spec is not clear on whether buf is NULL-terminated.
2590     // At least on OSX, buf contents are valid even when the call fails.
2591     SIZE_T sz = internal_strnlen(buf, buflen);
2592     if (sz < buflen) ++sz;
2593     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
2594   } else {
2595     // GNU version.
2596     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2597   }
2598   return res;
2600 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
2601 #else
2602 #define INIT_STRERROR_R
2603 #endif
2605 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
2606 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
2607   void *ctx;
2608   COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
2609   // FIXME: under ASan the call below may write to freed memory and corrupt
2610   // its metadata. See
2611   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2612   int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
2613   // This version always returns a null-terminated string.
2614   if (buf && buflen)
2615     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2616   return res;
2618 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
2619 #else
2620 #define INIT_XPG_STRERROR_R
2621 #endif
2623 #if SANITIZER_INTERCEPT_SCANDIR
2624 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
2625 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
2626                                 const struct __sanitizer_dirent **);
2628 static THREADLOCAL scandir_filter_f scandir_filter;
2629 static THREADLOCAL scandir_compar_f scandir_compar;
2631 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
2632   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2633   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
2634   return IndirectExternCall(scandir_filter)(dir);
2637 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
2638                                   const struct __sanitizer_dirent **b) {
2639   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2640   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
2641   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
2642   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
2643   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
2644   return IndirectExternCall(scandir_compar)(a, b);
2647 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
2648             scandir_filter_f filter, scandir_compar_f compar) {
2649   void *ctx;
2650   COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
2651   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
2652   scandir_filter = filter;
2653   scandir_compar = compar;
2654   // FIXME: under ASan the call below may write to freed memory and corrupt
2655   // its metadata. See
2656   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2657   int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
2658                           compar ? wrapped_scandir_compar : 0);
2659   scandir_filter = 0;
2660   scandir_compar = 0;
2661   if (namelist && res > 0) {
2662     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
2663     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
2664     for (int i = 0; i < res; ++i)
2665       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
2666                                      (*namelist)[i]->d_reclen);
2667   }
2668   return res;
2670 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
2671 #else
2672 #define INIT_SCANDIR
2673 #endif
2675 #if SANITIZER_INTERCEPT_SCANDIR64
2676 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
2677 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
2678                                   const struct __sanitizer_dirent64 **);
2680 static THREADLOCAL scandir64_filter_f scandir64_filter;
2681 static THREADLOCAL scandir64_compar_f scandir64_compar;
2683 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
2684   COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2685   COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
2686   return IndirectExternCall(scandir64_filter)(dir);
2689 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
2690                                     const struct __sanitizer_dirent64 **b) {
2691   COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2692   COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
2693   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
2694   COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
2695   COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
2696   return IndirectExternCall(scandir64_compar)(a, b);
2699 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
2700             scandir64_filter_f filter, scandir64_compar_f compar) {
2701   void *ctx;
2702   COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
2703   if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
2704   scandir64_filter = filter;
2705   scandir64_compar = compar;
2706   // FIXME: under ASan the call below may write to freed memory and corrupt
2707   // its metadata. See
2708   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2709   int res =
2710       REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
2711                       compar ? wrapped_scandir64_compar : 0);
2712   scandir64_filter = 0;
2713   scandir64_compar = 0;
2714   if (namelist && res > 0) {
2715     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
2716     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
2717     for (int i = 0; i < res; ++i)
2718       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
2719                                      (*namelist)[i]->d_reclen);
2720   }
2721   return res;
2723 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
2724 #else
2725 #define INIT_SCANDIR64
2726 #endif
2728 #if SANITIZER_INTERCEPT_GETGROUPS
2729 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
2730   void *ctx;
2731   COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
2732   // FIXME: under ASan the call below may write to freed memory and corrupt
2733   // its metadata. See
2734   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2735   int res = REAL(getgroups)(size, lst);
2736   if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
2737   return res;
2739 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
2740 #else
2741 #define INIT_GETGROUPS
2742 #endif
2744 #if SANITIZER_INTERCEPT_POLL
2745 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
2746                         __sanitizer_nfds_t nfds) {
2747   for (unsigned i = 0; i < nfds; ++i) {
2748     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
2749     COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
2750   }
2753 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
2754                          __sanitizer_nfds_t nfds) {
2755   for (unsigned i = 0; i < nfds; ++i)
2756     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
2757                                    sizeof(fds[i].revents));
2760 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2761             int timeout) {
2762   void *ctx;
2763   COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
2764   if (fds && nfds) read_pollfd(ctx, fds, nfds);
2765   int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
2766   if (fds && nfds) write_pollfd(ctx, fds, nfds);
2767   return res;
2769 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
2770 #else
2771 #define INIT_POLL
2772 #endif
2774 #if SANITIZER_INTERCEPT_PPOLL
2775 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2776             void *timeout_ts, __sanitizer_sigset_t *sigmask) {
2777   void *ctx;
2778   COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
2779   if (fds && nfds) read_pollfd(ctx, fds, nfds);
2780   if (timeout_ts)
2781     COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
2782   // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
2783   int res =
2784       COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
2785   if (fds && nfds) write_pollfd(ctx, fds, nfds);
2786   return res;
2788 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
2789 #else
2790 #define INIT_PPOLL
2791 #endif
2793 #if SANITIZER_INTERCEPT_WORDEXP
2794 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
2795   void *ctx;
2796   COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
2797   if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
2798   // FIXME: under ASan the call below may write to freed memory and corrupt
2799   // its metadata. See
2800   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2801   int res = REAL(wordexp)(s, p, flags);
2802   if (!res && p) {
2803     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2804     if (p->we_wordc)
2805       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
2806                                      sizeof(*p->we_wordv) * p->we_wordc);
2807     for (uptr i = 0; i < p->we_wordc; ++i) {
2808       char *w = p->we_wordv[i];
2809       if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
2810     }
2811   }
2812   return res;
2814 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
2815 #else
2816 #define INIT_WORDEXP
2817 #endif
2819 #if SANITIZER_INTERCEPT_SIGWAIT
2820 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
2821   void *ctx;
2822   COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
2823   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2824   // FIXME: under ASan the call below may write to freed memory and corrupt
2825   // its metadata. See
2826   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2827   int res = REAL(sigwait)(set, sig);
2828   if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
2829   return res;
2831 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
2832 #else
2833 #define INIT_SIGWAIT
2834 #endif
2836 #if SANITIZER_INTERCEPT_SIGWAITINFO
2837 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
2838   void *ctx;
2839   COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
2840   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2841   // FIXME: under ASan the call below may write to freed memory and corrupt
2842   // its metadata. See
2843   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2844   int res = REAL(sigwaitinfo)(set, info);
2845   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2846   return res;
2848 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
2849 #else
2850 #define INIT_SIGWAITINFO
2851 #endif
2853 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
2854 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
2855             void *timeout) {
2856   void *ctx;
2857   COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
2858   if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2859   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2860   // FIXME: under ASan the call below may write to freed memory and corrupt
2861   // its metadata. See
2862   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2863   int res = REAL(sigtimedwait)(set, info, timeout);
2864   if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2865   return res;
2867 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
2868 #else
2869 #define INIT_SIGTIMEDWAIT
2870 #endif
2872 #if SANITIZER_INTERCEPT_SIGSETOPS
2873 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
2874   void *ctx;
2875   COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
2876   // FIXME: under ASan the call below may write to freed memory and corrupt
2877   // its metadata. See
2878   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2879   int res = REAL(sigemptyset)(set);
2880   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2881   return res;
2884 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
2885   void *ctx;
2886   COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
2887   // FIXME: under ASan the call below may write to freed memory and corrupt
2888   // its metadata. See
2889   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2890   int res = REAL(sigfillset)(set);
2891   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2892   return res;
2894 #define INIT_SIGSETOPS                    \
2895   COMMON_INTERCEPT_FUNCTION(sigemptyset); \
2896   COMMON_INTERCEPT_FUNCTION(sigfillset);
2897 #else
2898 #define INIT_SIGSETOPS
2899 #endif
2901 #if SANITIZER_INTERCEPT_SIGPENDING
2902 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
2903   void *ctx;
2904   COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
2905   // FIXME: under ASan the call below may write to freed memory and corrupt
2906   // its metadata. See
2907   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2908   int res = REAL(sigpending)(set);
2909   if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2910   return res;
2912 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
2913 #else
2914 #define INIT_SIGPENDING
2915 #endif
2917 #if SANITIZER_INTERCEPT_SIGPROCMASK
2918 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
2919             __sanitizer_sigset_t *oldset) {
2920   void *ctx;
2921   COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
2922   // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2923   // FIXME: under ASan the call below may write to freed memory and corrupt
2924   // its metadata. See
2925   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2926   int res = REAL(sigprocmask)(how, set, oldset);
2927   if (!res && oldset)
2928     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
2929   return res;
2931 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
2932 #else
2933 #define INIT_SIGPROCMASK
2934 #endif
2936 #if SANITIZER_INTERCEPT_BACKTRACE
2937 INTERCEPTOR(int, backtrace, void **buffer, int size) {
2938   void *ctx;
2939   COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
2940   // FIXME: under ASan the call below may write to freed memory and corrupt
2941   // its metadata. See
2942   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2943   int res = REAL(backtrace)(buffer, size);
2944   if (res && buffer)
2945     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
2946   return res;
2949 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
2950   void *ctx;
2951   COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
2952   if (buffer && size)
2953     COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
2954   // FIXME: under ASan the call below may write to freed memory and corrupt
2955   // its metadata. See
2956   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2957   char **res = REAL(backtrace_symbols)(buffer, size);
2958   if (res && size) {
2959     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
2960     for (int i = 0; i < size; ++i)
2961       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
2962   }
2963   return res;
2965 #define INIT_BACKTRACE                  \
2966   COMMON_INTERCEPT_FUNCTION(backtrace); \
2967   COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
2968 #else
2969 #define INIT_BACKTRACE
2970 #endif
2972 #if SANITIZER_INTERCEPT__EXIT
2973 INTERCEPTOR(void, _exit, int status) {
2974   void *ctx;
2975   COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
2976   int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
2977   if (status == 0) status = status1;
2978   REAL(_exit)(status);
2980 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
2981 #else
2982 #define INIT__EXIT
2983 #endif
2985 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
2986 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
2987   void *ctx;
2988   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
2989   int res = REAL(pthread_mutex_lock)(m);
2990   if (res == errno_EOWNERDEAD)
2991     COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
2992   if (res == 0 || res == errno_EOWNERDEAD)
2993     COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2994   return res;
2997 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
2998   void *ctx;
2999   COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3000   COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3001   return REAL(pthread_mutex_unlock)(m);
3004 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3005 #define INIT_PTHREAD_MUTEX_UNLOCK \
3006   COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3007 #else
3008 #define INIT_PTHREAD_MUTEX_LOCK
3009 #define INIT_PTHREAD_MUTEX_UNLOCK
3010 #endif
3012 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3013 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3014   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3015   if (mnt->mnt_fsname)
3016     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3017                                    REAL(strlen)(mnt->mnt_fsname) + 1);
3018   if (mnt->mnt_dir)
3019     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3020                                    REAL(strlen)(mnt->mnt_dir) + 1);
3021   if (mnt->mnt_type)
3022     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3023                                    REAL(strlen)(mnt->mnt_type) + 1);
3024   if (mnt->mnt_opts)
3025     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3026                                    REAL(strlen)(mnt->mnt_opts) + 1);
3028 #endif
3030 #if SANITIZER_INTERCEPT_GETMNTENT
3031 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3032   void *ctx;
3033   COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3034   __sanitizer_mntent *res = REAL(getmntent)(fp);
3035   if (res) write_mntent(ctx, res);
3036   return res;
3038 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3039 #else
3040 #define INIT_GETMNTENT
3041 #endif
3043 #if SANITIZER_INTERCEPT_GETMNTENT_R
3044 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3045             __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3046   void *ctx;
3047   COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3048   __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3049   if (res) write_mntent(ctx, res);
3050   return res;
3052 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3053 #else
3054 #define INIT_GETMNTENT_R
3055 #endif
3057 #if SANITIZER_INTERCEPT_STATFS
3058 INTERCEPTOR(int, statfs, char *path, void *buf) {
3059   void *ctx;
3060   COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3061   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3062   // FIXME: under ASan the call below may write to freed memory and corrupt
3063   // its metadata. See
3064   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3065   int res = REAL(statfs)(path, buf);
3066   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3067   return res;
3069 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3070   void *ctx;
3071   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3072   // FIXME: under ASan the call below may write to freed memory and corrupt
3073   // its metadata. See
3074   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3075   int res = REAL(fstatfs)(fd, buf);
3076   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3077   return res;
3079 #define INIT_STATFS                  \
3080   COMMON_INTERCEPT_FUNCTION(statfs); \
3081   COMMON_INTERCEPT_FUNCTION(fstatfs);
3082 #else
3083 #define INIT_STATFS
3084 #endif
3086 #if SANITIZER_INTERCEPT_STATFS64
3087 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3088   void *ctx;
3089   COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3090   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3091   // FIXME: under ASan the call below may write to freed memory and corrupt
3092   // its metadata. See
3093   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3094   int res = REAL(statfs64)(path, buf);
3095   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3096   return res;
3098 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3099   void *ctx;
3100   COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3101   // FIXME: under ASan the call below may write to freed memory and corrupt
3102   // its metadata. See
3103   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3104   int res = REAL(fstatfs64)(fd, buf);
3105   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3106   return res;
3108 #define INIT_STATFS64                  \
3109   COMMON_INTERCEPT_FUNCTION(statfs64); \
3110   COMMON_INTERCEPT_FUNCTION(fstatfs64);
3111 #else
3112 #define INIT_STATFS64
3113 #endif
3115 #if SANITIZER_INTERCEPT_STATVFS
3116 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3117   void *ctx;
3118   COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3119   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3120   // FIXME: under ASan the call below may write to freed memory and corrupt
3121   // its metadata. See
3122   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3123   int res = REAL(statvfs)(path, buf);
3124   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3125   return res;
3127 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3128   void *ctx;
3129   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3130   // FIXME: under ASan the call below may write to freed memory and corrupt
3131   // its metadata. See
3132   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3133   int res = REAL(fstatvfs)(fd, buf);
3134   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3135   return res;
3137 #define INIT_STATVFS                  \
3138   COMMON_INTERCEPT_FUNCTION(statvfs); \
3139   COMMON_INTERCEPT_FUNCTION(fstatvfs);
3140 #else
3141 #define INIT_STATVFS
3142 #endif
3144 #if SANITIZER_INTERCEPT_STATVFS64
3145 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3146   void *ctx;
3147   COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3148   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3149   // FIXME: under ASan the call below may write to freed memory and corrupt
3150   // its metadata. See
3151   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3152   int res = REAL(statvfs64)(path, buf);
3153   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3154   return res;
3156 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3157   void *ctx;
3158   COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3159   // FIXME: under ASan the call below may write to freed memory and corrupt
3160   // its metadata. See
3161   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3162   int res = REAL(fstatvfs64)(fd, buf);
3163   if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3164   return res;
3166 #define INIT_STATVFS64                  \
3167   COMMON_INTERCEPT_FUNCTION(statvfs64); \
3168   COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3169 #else
3170 #define INIT_STATVFS64
3171 #endif
3173 #if SANITIZER_INTERCEPT_INITGROUPS
3174 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3175   void *ctx;
3176   COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3177   if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3178   int res = REAL(initgroups)(user, group);
3179   return res;
3181 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3182 #else
3183 #define INIT_INITGROUPS
3184 #endif
3186 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3187 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3188   void *ctx;
3189   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3190   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3191   char *res = REAL(ether_ntoa)(addr);
3192   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3193   return res;
3195 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3196   void *ctx;
3197   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3198   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3199   __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3200   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3201   return res;
3203 #define INIT_ETHER_NTOA_ATON             \
3204   COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3205   COMMON_INTERCEPT_FUNCTION(ether_aton);
3206 #else
3207 #define INIT_ETHER_NTOA_ATON
3208 #endif
3210 #if SANITIZER_INTERCEPT_ETHER_HOST
3211 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3212   void *ctx;
3213   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3214   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3215   // FIXME: under ASan the call below may write to freed memory and corrupt
3216   // its metadata. See
3217   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3218   int res = REAL(ether_ntohost)(hostname, addr);
3219   if (!res && hostname)
3220     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3221   return res;
3223 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3224   void *ctx;
3225   COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3226   if (hostname)
3227     COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3228   // FIXME: under ASan the call below may write to freed memory and corrupt
3229   // its metadata. See
3230   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3231   int res = REAL(ether_hostton)(hostname, addr);
3232   if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3233   return res;
3235 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3236             char *hostname) {
3237   void *ctx;
3238   COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3239   if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3240   // FIXME: under ASan the call below may write to freed memory and corrupt
3241   // its metadata. See
3242   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3243   int res = REAL(ether_line)(line, addr, hostname);
3244   if (!res) {
3245     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3246     if (hostname)
3247       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3248   }
3249   return res;
3251 #define INIT_ETHER_HOST                     \
3252   COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3253   COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3254   COMMON_INTERCEPT_FUNCTION(ether_line);
3255 #else
3256 #define INIT_ETHER_HOST
3257 #endif
3259 #if SANITIZER_INTERCEPT_ETHER_R
3260 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3261   void *ctx;
3262   COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3263   if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3264   // FIXME: under ASan the call below may write to freed memory and corrupt
3265   // its metadata. See
3266   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3267   char *res = REAL(ether_ntoa_r)(addr, buf);
3268   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3269   return res;
3271 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3272             __sanitizer_ether_addr *addr) {
3273   void *ctx;
3274   COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3275   if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3276   // FIXME: under ASan the call below may write to freed memory and corrupt
3277   // its metadata. See
3278   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3279   __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3280   if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3281   return res;
3283 #define INIT_ETHER_R                       \
3284   COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3285   COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3286 #else
3287 #define INIT_ETHER_R
3288 #endif
3290 #if SANITIZER_INTERCEPT_SHMCTL
3291 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3292   void *ctx;
3293   COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3294   // FIXME: under ASan the call below may write to freed memory and corrupt
3295   // its metadata. See
3296   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3297   int res = REAL(shmctl)(shmid, cmd, buf);
3298   if (res >= 0) {
3299     unsigned sz = 0;
3300     if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3301       sz = sizeof(__sanitizer_shmid_ds);
3302     else if (cmd == shmctl_ipc_info)
3303       sz = struct_shminfo_sz;
3304     else if (cmd == shmctl_shm_info)
3305       sz = struct_shm_info_sz;
3306     if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3307   }
3308   return res;
3310 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3311 #else
3312 #define INIT_SHMCTL
3313 #endif
3315 #if SANITIZER_INTERCEPT_RANDOM_R
3316 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
3317   void *ctx;
3318   COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
3319   // FIXME: under ASan the call below may write to freed memory and corrupt
3320   // its metadata. See
3321   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3322   int res = REAL(random_r)(buf, result);
3323   if (!res && result)
3324     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3325   return res;
3327 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3328 #else
3329 #define INIT_RANDOM_R
3330 #endif
3332 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
3333 // its metadata. See
3334 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3335 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET ||              \
3336     SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
3337     SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET ||         \
3338     SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET ||        \
3339     SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET ||          \
3340     SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
3341 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz)            \
3342   INTERCEPTOR(int, fn, void *attr, void *r) {                  \
3343     void *ctx;                                                 \
3344     COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r);                \
3345     int res = REAL(fn)(attr, r);                               \
3346     if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
3347     return res;                                                \
3348   }
3349 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
3350   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
3351 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
3352   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
3353 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
3354   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
3355 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
3356   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
3357 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
3358   INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
3359 #endif
3361 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
3362 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
3363 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
3364 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
3365 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
3366 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
3367 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
3368 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
3369   void *ctx;
3370   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
3371   // FIXME: under ASan the call below may write to freed memory and corrupt
3372   // its metadata. See
3373   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3374   int res = REAL(pthread_attr_getstack)(attr, addr, size);
3375   if (!res) {
3376     if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3377     if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
3378   }
3379   return res;
3382 // We may need to call the real pthread_attr_getstack from the run-time
3383 // in sanitizer_common, but we don't want to include the interception headers
3384 // there. So, just define this function here.
3385 namespace __sanitizer {
3386 extern "C" {
3387 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
3388   return REAL(pthread_attr_getstack)(attr, addr, size);
3390 }  // extern "C"
3391 }  // namespace __sanitizer
3393 #define INIT_PTHREAD_ATTR_GET                             \
3394   COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
3395   COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize);   \
3396   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam);  \
3397   COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
3398   COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope);       \
3399   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize);   \
3400   COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
3401 #else
3402 #define INIT_PTHREAD_ATTR_GET
3403 #endif
3405 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
3406 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
3408 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
3409   COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
3410 #else
3411 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
3412 #endif
3414 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
3415 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
3416             void *cpuset) {
3417   void *ctx;
3418   COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
3419                            cpuset);
3420   // FIXME: under ASan the call below may write to freed memory and corrupt
3421   // its metadata. See
3422   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3423   int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
3424   if (!res && cpusetsize && cpuset)
3425     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
3426   return res;
3429 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
3430   COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
3431 #else
3432 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
3433 #endif
3435 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
3436 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
3437 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
3438   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
3439 #else
3440 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
3441 #endif
3443 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
3444 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
3445 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
3446   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
3447 #else
3448 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
3449 #endif
3451 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
3452 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
3453 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
3454   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
3455 #else
3456 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
3457 #endif
3459 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
3460 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
3461 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
3462   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
3463 #else
3464 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
3465 #endif
3467 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
3468 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
3469 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
3470   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
3471 #else
3472 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
3473 #endif
3475 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
3476 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
3477 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
3478   COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
3479 #else
3480 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
3481 #endif
3483 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
3484 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
3485 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
3486   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
3487 #else
3488 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
3489 #endif
3491 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
3492 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
3493 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
3494   COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
3495 #else
3496 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
3497 #endif
3499 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
3500 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
3501 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
3502   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
3503 #else
3504 #define INIT_PTHREAD_CONDATTR_GETPSHARED
3505 #endif
3507 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
3508 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
3509 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
3510   COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
3511 #else
3512 #define INIT_PTHREAD_CONDATTR_GETCLOCK
3513 #endif
3515 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
3516 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
3517 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
3518   COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
3519 #else
3520 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
3521 #endif
3523 #if SANITIZER_INTERCEPT_TMPNAM
3524 INTERCEPTOR(char *, tmpnam, char *s) {
3525   void *ctx;
3526   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
3527   char *res = REAL(tmpnam)(s);
3528   if (res) {
3529     if (s)
3530       // FIXME: under ASan the call below may write to freed memory and corrupt
3531       // its metadata. See
3532       // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3533       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
3534     else
3535       COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3536   }
3537   return res;
3539 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
3540 #else
3541 #define INIT_TMPNAM
3542 #endif
3544 #if SANITIZER_INTERCEPT_TMPNAM_R
3545 INTERCEPTOR(char *, tmpnam_r, char *s) {
3546   void *ctx;
3547   COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
3548   // FIXME: under ASan the call below may write to freed memory and corrupt
3549   // its metadata. See
3550   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3551   char *res = REAL(tmpnam_r)(s);
3552   if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
3553   return res;
3555 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
3556 #else
3557 #define INIT_TMPNAM_R
3558 #endif
3560 #if SANITIZER_INTERCEPT_TEMPNAM
3561 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
3562   void *ctx;
3563   COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
3564   if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
3565   if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
3566   char *res = REAL(tempnam)(dir, pfx);
3567   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3568   return res;
3570 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
3571 #else
3572 #define INIT_TEMPNAM
3573 #endif
3575 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
3576 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
3577   void *ctx;
3578   COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
3579   COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
3580   return REAL(pthread_setname_np)(thread, name);
3582 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
3583 #else
3584 #define INIT_PTHREAD_SETNAME_NP
3585 #endif
3587 #if SANITIZER_INTERCEPT_SINCOS
3588 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
3589   void *ctx;
3590   COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
3591   // FIXME: under ASan the call below may write to freed memory and corrupt
3592   // its metadata. See
3593   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3594   REAL(sincos)(x, sin, cos);
3595   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
3596   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
3598 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
3599   void *ctx;
3600   COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
3601   // FIXME: under ASan the call below may write to freed memory and corrupt
3602   // its metadata. See
3603   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3604   REAL(sincosf)(x, sin, cos);
3605   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
3606   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
3608 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
3609   void *ctx;
3610   COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
3611   // FIXME: under ASan the call below may write to freed memory and corrupt
3612   // its metadata. See
3613   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3614   REAL(sincosl)(x, sin, cos);
3615   if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
3616   if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
3618 #define INIT_SINCOS                   \
3619   COMMON_INTERCEPT_FUNCTION(sincos);  \
3620   COMMON_INTERCEPT_FUNCTION(sincosf); \
3621   COMMON_INTERCEPT_FUNCTION(sincosl);
3622 #else
3623 #define INIT_SINCOS
3624 #endif
3626 #if SANITIZER_INTERCEPT_REMQUO
3627 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
3628   void *ctx;
3629   COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
3630   // FIXME: under ASan the call below may write to freed memory and corrupt
3631   // its metadata. See
3632   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3633   double res = REAL(remquo)(x, y, quo);
3634   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
3635   return res;
3637 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
3638   void *ctx;
3639   COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
3640   // FIXME: under ASan the call below may write to freed memory and corrupt
3641   // its metadata. See
3642   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3643   float res = REAL(remquof)(x, y, quo);
3644   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
3645   return res;
3647 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
3648   void *ctx;
3649   COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
3650   // FIXME: under ASan the call below may write to freed memory and corrupt
3651   // its metadata. See
3652   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3653   long double res = REAL(remquol)(x, y, quo);
3654   if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
3655   return res;
3657 #define INIT_REMQUO                   \
3658   COMMON_INTERCEPT_FUNCTION(remquo);  \
3659   COMMON_INTERCEPT_FUNCTION(remquof); \
3660   COMMON_INTERCEPT_FUNCTION(remquol);
3661 #else
3662 #define INIT_REMQUO
3663 #endif
3665 #if SANITIZER_INTERCEPT_LGAMMA
3666 extern int signgam;
3667 INTERCEPTOR(double, lgamma, double x) {
3668   void *ctx;
3669   COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
3670   double res = REAL(lgamma)(x);
3671   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3672   return res;
3674 INTERCEPTOR(float, lgammaf, float x) {
3675   void *ctx;
3676   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
3677   float res = REAL(lgammaf)(x);
3678   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3679   return res;
3681 INTERCEPTOR(long double, lgammal, long double x) {
3682   void *ctx;
3683   COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
3684   long double res = REAL(lgammal)(x);
3685   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3686   return res;
3688 #define INIT_LGAMMA                   \
3689   COMMON_INTERCEPT_FUNCTION(lgamma);  \
3690   COMMON_INTERCEPT_FUNCTION(lgammaf); \
3691   COMMON_INTERCEPT_FUNCTION(lgammal);
3692 #else
3693 #define INIT_LGAMMA
3694 #endif
3696 #if SANITIZER_INTERCEPT_LGAMMA_R
3697 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
3698   void *ctx;
3699   COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
3700   // FIXME: under ASan the call below may write to freed memory and corrupt
3701   // its metadata. See
3702   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3703   double res = REAL(lgamma_r)(x, signp);
3704   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3705   return res;
3707 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
3708   void *ctx;
3709   COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
3710   // FIXME: under ASan the call below may write to freed memory and corrupt
3711   // its metadata. See
3712   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3713   float res = REAL(lgammaf_r)(x, signp);
3714   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3715   return res;
3717 #define INIT_LGAMMA_R                   \
3718   COMMON_INTERCEPT_FUNCTION(lgamma_r);  \
3719   COMMON_INTERCEPT_FUNCTION(lgammaf_r);
3720 #else
3721 #define INIT_LGAMMA_R
3722 #endif
3724 #if SANITIZER_INTERCEPT_LGAMMAL_R
3725 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
3726   void *ctx;
3727   COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
3728   // FIXME: under ASan the call below may write to freed memory and corrupt
3729   // its metadata. See
3730   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3731   long double res = REAL(lgammal_r)(x, signp);
3732   if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3733   return res;
3735 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION(lgammal_r);
3736 #else
3737 #define INIT_LGAMMAL_R
3738 #endif
3740 #if SANITIZER_INTERCEPT_DRAND48_R
3741 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
3742   void *ctx;
3743   COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
3744   // FIXME: under ASan the call below may write to freed memory and corrupt
3745   // its metadata. See
3746   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3747   int res = REAL(drand48_r)(buffer, result);
3748   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3749   return res;
3751 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
3752   void *ctx;
3753   COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
3754   // FIXME: under ASan the call below may write to freed memory and corrupt
3755   // its metadata. See
3756   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3757   int res = REAL(lrand48_r)(buffer, result);
3758   if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3759   return res;
3761 #define INIT_DRAND48_R                  \
3762   COMMON_INTERCEPT_FUNCTION(drand48_r); \
3763   COMMON_INTERCEPT_FUNCTION(lrand48_r);
3764 #else
3765 #define INIT_DRAND48_R
3766 #endif
3768 #if SANITIZER_INTERCEPT_RAND_R
3769 INTERCEPTOR(int, rand_r, unsigned *seedp) {
3770   void *ctx;
3771   COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
3772   COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
3773   return REAL(rand_r)(seedp);
3775 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
3776 #else
3777 #define INIT_RAND_R
3778 #endif
3780 #if SANITIZER_INTERCEPT_GETLINE
3781 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
3782   void *ctx;
3783   COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
3784   // FIXME: under ASan the call below may write to freed memory and corrupt
3785   // its metadata. See
3786   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3787   SSIZE_T res = REAL(getline)(lineptr, n, stream);
3788   if (res > 0) {
3789     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
3790     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
3791     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
3792   }
3793   return res;
3795 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
3796             void *stream) {
3797   void *ctx;
3798   COMMON_INTERCEPTOR_ENTER(ctx, __getdelim, lineptr, n, delim, stream);
3799   // FIXME: under ASan the call below may write to freed memory and corrupt
3800   // its metadata. See
3801   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3802   SSIZE_T res = REAL(__getdelim)(lineptr, n, delim, stream);
3803   if (res > 0) {
3804     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
3805     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
3806     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
3807   }
3808   return res;
3810 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
3811             void *stream) {
3812   return __getdelim(lineptr, n, delim, stream);
3814 #define INIT_GETLINE                     \
3815   COMMON_INTERCEPT_FUNCTION(getline);    \
3816   COMMON_INTERCEPT_FUNCTION(__getdelim); \
3817   COMMON_INTERCEPT_FUNCTION(getdelim);
3818 #else
3819 #define INIT_GETLINE
3820 #endif
3822 #if SANITIZER_INTERCEPT_ICONV
3823 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
3824             char **outbuf, SIZE_T *outbytesleft) {
3825   void *ctx;
3826   COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
3827                            outbytesleft);
3828   if (inbytesleft)
3829     COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
3830   if (inbuf && inbytesleft)
3831     COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
3832   if (outbytesleft)
3833     COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
3834   void *outbuf_orig = outbuf ? *outbuf : 0;
3835   // FIXME: under ASan the call below may write to freed memory and corrupt
3836   // its metadata. See
3837   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3838   SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
3839   if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
3840     SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
3841     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
3842   }
3843   return res;
3845 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
3846 #else
3847 #define INIT_ICONV
3848 #endif
3850 #if SANITIZER_INTERCEPT_TIMES
3851 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
3852   void *ctx;
3853   COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
3854   // FIXME: under ASan the call below may write to freed memory and corrupt
3855   // its metadata. See
3856   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3857   __sanitizer_clock_t res = REAL(times)(tms);
3858   if (res != (__sanitizer_clock_t)-1 && tms)
3859     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
3860   return res;
3862 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
3863 #else
3864 #define INIT_TIMES
3865 #endif
3867 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
3868 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
3869 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
3870   void *ctx;
3871   COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
3872   void *res = REAL(__tls_get_addr)(arg);
3873   DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res);
3874   if (dtv) {
3875     // New DTLS block has been allocated.
3876     COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
3877   }
3878   return res;
3880 #else
3881 #define INIT_TLS_GET_ADDR
3882 #endif
3884 #if SANITIZER_INTERCEPT_LISTXATTR
3885 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
3886   void *ctx;
3887   COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
3888   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3889   // FIXME: under ASan the call below may write to freed memory and corrupt
3890   // its metadata. See
3891   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3892   SSIZE_T res = REAL(listxattr)(path, list, size);
3893   // Here and below, size == 0 is a special case where nothing is written to the
3894   // buffer, and res contains the desired buffer size.
3895   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3896   return res;
3898 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
3899   void *ctx;
3900   COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
3901   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3902   // FIXME: under ASan the call below may write to freed memory and corrupt
3903   // its metadata. See
3904   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3905   SSIZE_T res = REAL(llistxattr)(path, list, size);
3906   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3907   return res;
3909 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
3910   void *ctx;
3911   COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
3912   // FIXME: under ASan the call below may write to freed memory and corrupt
3913   // its metadata. See
3914   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3915   SSIZE_T res = REAL(flistxattr)(fd, list, size);
3916   if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3917   return res;
3919 #define INIT_LISTXATTR                   \
3920   COMMON_INTERCEPT_FUNCTION(listxattr);  \
3921   COMMON_INTERCEPT_FUNCTION(llistxattr); \
3922   COMMON_INTERCEPT_FUNCTION(flistxattr);
3923 #else
3924 #define INIT_LISTXATTR
3925 #endif
3927 #if SANITIZER_INTERCEPT_GETXATTR
3928 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
3929             SIZE_T size) {
3930   void *ctx;
3931   COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
3932   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3933   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3934   // FIXME: under ASan the call below may write to freed memory and corrupt
3935   // its metadata. See
3936   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3937   SSIZE_T res = REAL(getxattr)(path, name, value, size);
3938   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
3939   return res;
3941 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
3942             SIZE_T size) {
3943   void *ctx;
3944   COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
3945   if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3946   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3947   // FIXME: under ASan the call below may write to freed memory and corrupt
3948   // its metadata. See
3949   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3950   SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
3951   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
3952   return res;
3954 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
3955             SIZE_T size) {
3956   void *ctx;
3957   COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
3958   if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3959   // FIXME: under ASan the call below may write to freed memory and corrupt
3960   // its metadata. See
3961   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3962   SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
3963   if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
3964   return res;
3966 #define INIT_GETXATTR                   \
3967   COMMON_INTERCEPT_FUNCTION(getxattr);  \
3968   COMMON_INTERCEPT_FUNCTION(lgetxattr); \
3969   COMMON_INTERCEPT_FUNCTION(fgetxattr);
3970 #else
3971 #define INIT_GETXATTR
3972 #endif
3974 #if SANITIZER_INTERCEPT_GETRESID
3975 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
3976   void *ctx;
3977   COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
3978   // FIXME: under ASan the call below may write to freed memory and corrupt
3979   // its metadata. See
3980   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3981   int res = REAL(getresuid)(ruid, euid, suid);
3982   if (res >= 0) {
3983     if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
3984     if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
3985     if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
3986   }
3987   return res;
3989 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
3990   void *ctx;
3991   COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
3992   // FIXME: under ASan the call below may write to freed memory and corrupt
3993   // its metadata. See
3994   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3995   int res = REAL(getresgid)(rgid, egid, sgid);
3996   if (res >= 0) {
3997     if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
3998     if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
3999     if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4000   }
4001   return res;
4003 #define INIT_GETRESID                   \
4004   COMMON_INTERCEPT_FUNCTION(getresuid); \
4005   COMMON_INTERCEPT_FUNCTION(getresgid);
4006 #else
4007 #define INIT_GETRESID
4008 #endif
4010 #if SANITIZER_INTERCEPT_GETIFADDRS
4011 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4012 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4013 // intercept it to poison the memory again.
4014 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4015   void *ctx;
4016   COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4017   // FIXME: under ASan the call below may write to freed memory and corrupt
4018   // its metadata. See
4019   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4020   int res = REAL(getifaddrs)(ifap);
4021   if (res == 0 && ifap) {
4022     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4023     __sanitizer_ifaddrs *p = *ifap;
4024     while (p) {
4025       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4026       if (p->ifa_name)
4027         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4028                                        REAL(strlen)(p->ifa_name) + 1);
4029       if (p->ifa_addr)
4030         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4031       if (p->ifa_netmask)
4032         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4033       // On Linux this is a union, but the other member also points to a
4034       // struct sockaddr, so the following is sufficient.
4035       if (p->ifa_dstaddr)
4036         COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4037       // FIXME(smatveev): Unpoison p->ifa_data as well.
4038       p = p->ifa_next;
4039     }
4040   }
4041   return res;
4043 #define INIT_GETIFADDRS                  \
4044   COMMON_INTERCEPT_FUNCTION(getifaddrs);
4045 #else
4046 #define INIT_GETIFADDRS
4047 #endif
4049 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4050 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4051   void *ctx;
4052   COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4053   // FIXME: under ASan the call below may write to freed memory and corrupt
4054   // its metadata. See
4055   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4056   char *res = REAL(if_indextoname)(ifindex, ifname);
4057   if (res && ifname)
4058     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4059   return res;
4061 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4062   void *ctx;
4063   COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4064   if (ifname)
4065     COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4066   return REAL(if_nametoindex)(ifname);
4068 #define INIT_IF_INDEXTONAME                  \
4069   COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4070   COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4071 #else
4072 #define INIT_IF_INDEXTONAME
4073 #endif
4075 #if SANITIZER_INTERCEPT_CAPGET
4076 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4077   void *ctx;
4078   COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4079   if (hdrp)
4080     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4081   // FIXME: under ASan the call below may write to freed memory and corrupt
4082   // its metadata. See
4083   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4084   int res = REAL(capget)(hdrp, datap);
4085   if (res == 0 && datap)
4086     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4087   // We can also return -1 and write to hdrp->version if the version passed in
4088   // hdrp->version is unsupported. But that's not a trivial condition to check,
4089   // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4090   return res;
4092 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4093   void *ctx;
4094   COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4095   if (hdrp)
4096     COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4097   if (datap)
4098     COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4099   return REAL(capset)(hdrp, datap);
4101 #define INIT_CAPGET                  \
4102   COMMON_INTERCEPT_FUNCTION(capget); \
4103   COMMON_INTERCEPT_FUNCTION(capset);
4104 #else
4105 #define INIT_CAPGET
4106 #endif
4108 #if SANITIZER_INTERCEPT_AEABI_MEM
4109 DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr);
4110 DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr);
4111 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4113 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4114   return WRAP(memmove)(to, from, size);
4116 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4117   return WRAP(memmove)(to, from, size);
4119 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4120   return WRAP(memmove)(to, from, size);
4122 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4123   return WRAP(memcpy)(to, from, size);
4125 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4126   return WRAP(memcpy)(to, from, size);
4128 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4129   return WRAP(memcpy)(to, from, size);
4131 // Note the argument order.
4132 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4133   return WRAP(memset)(block, c, size);
4135 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4136   return WRAP(memset)(block, c, size);
4138 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4139   return WRAP(memset)(block, c, size);
4141 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4142   return WRAP(memset)(block, 0, size);
4144 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4145   return WRAP(memset)(block, 0, size);
4147 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4148   return WRAP(memset)(block, 0, size);
4150 #define INIT_AEABI_MEM                         \
4151   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove);  \
4152   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4153   COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4154   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy);   \
4155   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4);  \
4156   COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8);  \
4157   COMMON_INTERCEPT_FUNCTION(__aeabi_memset);   \
4158   COMMON_INTERCEPT_FUNCTION(__aeabi_memset4);  \
4159   COMMON_INTERCEPT_FUNCTION(__aeabi_memset8);  \
4160   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr);   \
4161   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4);  \
4162   COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4163 #else
4164 #define INIT_AEABI_MEM
4165 #endif  // SANITIZER_INTERCEPT_AEABI_MEM
4167 #if SANITIZER_INTERCEPT___BZERO
4168 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4170 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4171   return WRAP(memset)(block, 0, size);
4173 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4174 #else
4175 #define INIT___BZERO
4176 #endif  // SANITIZER_INTERCEPT___BZERO
4178 #if SANITIZER_INTERCEPT_FTIME
4179 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4180   void *ctx;
4181   COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
4182   // FIXME: under ASan the call below may write to freed memory and corrupt
4183   // its metadata. See
4184   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4185   int res = REAL(ftime)(tp);
4186   if (tp)
4187     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4188   return res;
4190 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4191 #else
4192 #define INIT_FTIME
4193 #endif  // SANITIZER_INTERCEPT_FTIME
4195 #if SANITIZER_INTERCEPT_XDR
4196 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4197             unsigned size, int op) {
4198   void *ctx;
4199   COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
4200   // FIXME: under ASan the call below may write to freed memory and corrupt
4201   // its metadata. See
4202   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4203   REAL(xdrmem_create)(xdrs, addr, size, op);
4204   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4205   if (op == __sanitizer_XDR_ENCODE) {
4206     // It's not obvious how much data individual xdr_ routines write.
4207     // Simply unpoison the entire target buffer in advance.
4208     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
4209   }
4212 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
4213   void *ctx;
4214   COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
4215   // FIXME: under ASan the call below may write to freed memory and corrupt
4216   // its metadata. See
4217   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4218   REAL(xdrstdio_create)(xdrs, file, op);
4219   COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4222 // FIXME: under ASan the call below may write to freed memory and corrupt
4223 // its metadata. See
4224 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4225 #define XDR_INTERCEPTOR(F, T)                             \
4226   INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) {      \
4227     void *ctx;                                            \
4228     COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p);            \
4229     if (p && xdrs->x_op == __sanitizer_XDR_ENCODE)        \
4230       COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));  \
4231     int res = REAL(F)(xdrs, p);                           \
4232     if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
4233       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
4234     return res;                                           \
4235   }
4237 XDR_INTERCEPTOR(xdr_short, short)
4238 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
4239 XDR_INTERCEPTOR(xdr_int, int)
4240 XDR_INTERCEPTOR(xdr_u_int, unsigned)
4241 XDR_INTERCEPTOR(xdr_long, long)
4242 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
4243 XDR_INTERCEPTOR(xdr_hyper, long long)
4244 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
4245 XDR_INTERCEPTOR(xdr_longlong_t, long long)
4246 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
4247 XDR_INTERCEPTOR(xdr_int8_t, u8)
4248 XDR_INTERCEPTOR(xdr_uint8_t, u8)
4249 XDR_INTERCEPTOR(xdr_int16_t, u16)
4250 XDR_INTERCEPTOR(xdr_uint16_t, u16)
4251 XDR_INTERCEPTOR(xdr_int32_t, u32)
4252 XDR_INTERCEPTOR(xdr_uint32_t, u32)
4253 XDR_INTERCEPTOR(xdr_int64_t, u64)
4254 XDR_INTERCEPTOR(xdr_uint64_t, u64)
4255 XDR_INTERCEPTOR(xdr_quad_t, long long)
4256 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
4257 XDR_INTERCEPTOR(xdr_bool, bool)
4258 XDR_INTERCEPTOR(xdr_enum, int)
4259 XDR_INTERCEPTOR(xdr_char, char)
4260 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
4261 XDR_INTERCEPTOR(xdr_float, float)
4262 XDR_INTERCEPTOR(xdr_double, double)
4264 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
4265 // wrapstring, sizeof
4267 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
4268             unsigned maxsize) {
4269   void *ctx;
4270   COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
4271   if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
4272     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
4273     COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
4274     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
4275   }
4276   // FIXME: under ASan the call below may write to freed memory and corrupt
4277   // its metadata. See
4278   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4279   int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
4280   if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
4281     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4282     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
4283     if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
4284   }
4285   return res;
4288 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
4289             unsigned maxsize) {
4290   void *ctx;
4291   COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
4292   if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
4293     COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
4294     COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
4295   }
4296   // FIXME: under ASan the call below may write to freed memory and corrupt
4297   // its metadata. See
4298   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4299   int res = REAL(xdr_string)(xdrs, p, maxsize);
4300   if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
4301     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4302     if (res && *p)
4303       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
4304   }
4305   return res;
4308 #define INIT_XDR                               \
4309   COMMON_INTERCEPT_FUNCTION(xdrmem_create);    \
4310   COMMON_INTERCEPT_FUNCTION(xdrstdio_create);  \
4311   COMMON_INTERCEPT_FUNCTION(xdr_short);        \
4312   COMMON_INTERCEPT_FUNCTION(xdr_u_short);      \
4313   COMMON_INTERCEPT_FUNCTION(xdr_int);          \
4314   COMMON_INTERCEPT_FUNCTION(xdr_u_int);        \
4315   COMMON_INTERCEPT_FUNCTION(xdr_long);         \
4316   COMMON_INTERCEPT_FUNCTION(xdr_u_long);       \
4317   COMMON_INTERCEPT_FUNCTION(xdr_hyper);        \
4318   COMMON_INTERCEPT_FUNCTION(xdr_u_hyper);      \
4319   COMMON_INTERCEPT_FUNCTION(xdr_longlong_t);   \
4320   COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
4321   COMMON_INTERCEPT_FUNCTION(xdr_int8_t);       \
4322   COMMON_INTERCEPT_FUNCTION(xdr_uint8_t);      \
4323   COMMON_INTERCEPT_FUNCTION(xdr_int16_t);      \
4324   COMMON_INTERCEPT_FUNCTION(xdr_uint16_t);     \
4325   COMMON_INTERCEPT_FUNCTION(xdr_int32_t);      \
4326   COMMON_INTERCEPT_FUNCTION(xdr_uint32_t);     \
4327   COMMON_INTERCEPT_FUNCTION(xdr_int64_t);      \
4328   COMMON_INTERCEPT_FUNCTION(xdr_uint64_t);     \
4329   COMMON_INTERCEPT_FUNCTION(xdr_quad_t);       \
4330   COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t);     \
4331   COMMON_INTERCEPT_FUNCTION(xdr_bool);         \
4332   COMMON_INTERCEPT_FUNCTION(xdr_enum);         \
4333   COMMON_INTERCEPT_FUNCTION(xdr_char);         \
4334   COMMON_INTERCEPT_FUNCTION(xdr_u_char);       \
4335   COMMON_INTERCEPT_FUNCTION(xdr_float);        \
4336   COMMON_INTERCEPT_FUNCTION(xdr_double);       \
4337   COMMON_INTERCEPT_FUNCTION(xdr_bytes);        \
4338   COMMON_INTERCEPT_FUNCTION(xdr_string);
4339 #else
4340 #define INIT_XDR
4341 #endif  // SANITIZER_INTERCEPT_XDR
4343 #if SANITIZER_INTERCEPT_TSEARCH
4344 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
4345             int (*compar)(const void *, const void *)) {
4346   void *ctx;
4347   COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
4348   // FIXME: under ASan the call below may write to freed memory and corrupt
4349   // its metadata. See
4350   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4351   void *res = REAL(tsearch)(key, rootp, compar);
4352   if (res && *(void **)res == key)
4353     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
4354   return res;
4356 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
4357 #else
4358 #define INIT_TSEARCH
4359 #endif
4361 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
4362     SANITIZER_INTERCEPT_OPEN_MEMSTREAM
4363 void unpoison_file(__sanitizer_FILE *fp) {
4364 #if SANITIZER_HAS_STRUCT_FILE
4365   COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
4366   if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
4367     COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
4368                                         fp->_IO_read_end - fp->_IO_read_base);
4369 #endif  // SANITIZER_HAS_STRUCT_FILE
4371 #endif
4373 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
4374 // These guys are called when a .c source is built with -O2.
4375 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
4376   void *ctx;
4377   COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
4378   int res = REAL(__uflow)(fp);
4379   unpoison_file(fp);
4380   return res;
4382 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
4383   void *ctx;
4384   COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
4385   int res = REAL(__underflow)(fp);
4386   unpoison_file(fp);
4387   return res;
4389 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
4390   void *ctx;
4391   COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
4392   int res = REAL(__overflow)(fp, ch);
4393   unpoison_file(fp);
4394   return res;
4396 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
4397   void *ctx;
4398   COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
4399   int res = REAL(__wuflow)(fp);
4400   unpoison_file(fp);
4401   return res;
4403 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
4404   void *ctx;
4405   COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
4406   int res = REAL(__wunderflow)(fp);
4407   unpoison_file(fp);
4408   return res;
4410 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
4411   void *ctx;
4412   COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
4413   int res = REAL(__woverflow)(fp, ch);
4414   unpoison_file(fp);
4415   return res;
4417 #define INIT_LIBIO_INTERNALS               \
4418   COMMON_INTERCEPT_FUNCTION(__uflow);      \
4419   COMMON_INTERCEPT_FUNCTION(__underflow);  \
4420   COMMON_INTERCEPT_FUNCTION(__overflow);   \
4421   COMMON_INTERCEPT_FUNCTION(__wuflow);     \
4422   COMMON_INTERCEPT_FUNCTION(__wunderflow); \
4423   COMMON_INTERCEPT_FUNCTION(__woverflow);
4424 #else
4425 #define INIT_LIBIO_INTERNALS
4426 #endif
4428 #if SANITIZER_INTERCEPT_FOPEN
4429 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
4430   void *ctx;
4431   COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
4432   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4433   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4434   __sanitizer_FILE *res = REAL(fopen)(path, mode);
4435   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4436   if (res) unpoison_file(res);
4437   return res;
4439 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
4440   void *ctx;
4441   COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
4442   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4443   __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
4444   if (res) unpoison_file(res);
4445   return res;
4447 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
4448             __sanitizer_FILE *fp) {
4449   void *ctx;
4450   COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
4451   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4452   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4453   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4454   __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
4455   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4456   if (res) unpoison_file(res);
4457   return res;
4459 #define INIT_FOPEN                   \
4460   COMMON_INTERCEPT_FUNCTION(fopen);  \
4461   COMMON_INTERCEPT_FUNCTION(fdopen); \
4462   COMMON_INTERCEPT_FUNCTION(freopen);
4463 #else
4464 #define INIT_FOPEN
4465 #endif
4467 #if SANITIZER_INTERCEPT_FOPEN64
4468 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
4469   void *ctx;
4470   COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
4471   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4472   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4473   __sanitizer_FILE *res = REAL(fopen64)(path, mode);
4474   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4475   if (res) unpoison_file(res);
4476   return res;
4478 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
4479             __sanitizer_FILE *fp) {
4480   void *ctx;
4481   COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
4482   COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4483   COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4484   COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4485   __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
4486   COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4487   if (res) unpoison_file(res);
4488   return res;
4490 #define INIT_FOPEN64                  \
4491   COMMON_INTERCEPT_FUNCTION(fopen64); \
4492   COMMON_INTERCEPT_FUNCTION(freopen64);
4493 #else
4494 #define INIT_FOPEN64
4495 #endif
4497 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
4498 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
4499   void *ctx;
4500   COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
4501   // FIXME: under ASan the call below may write to freed memory and corrupt
4502   // its metadata. See
4503   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4504   __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
4505   if (res) {
4506     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
4507     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
4508     unpoison_file(res);
4509     FileMetadata file = {ptr, sizeloc};
4510     SetInterceptorMetadata(res, file);
4511   }
4512   return res;
4514 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
4515             SIZE_T *sizeloc) {
4516   void *ctx;
4517   COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
4518   __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
4519   if (res) {
4520     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
4521     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
4522     unpoison_file(res);
4523     FileMetadata file = {(char **)ptr, sizeloc};
4524     SetInterceptorMetadata(res, file);
4525   }
4526   return res;
4528 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
4529             const char *mode) {
4530   void *ctx;
4531   COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
4532   // FIXME: under ASan the call below may write to freed memory and corrupt
4533   // its metadata. See
4534   // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4535   __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
4536   if (res) unpoison_file(res);
4537   return res;
4539 #define INIT_OPEN_MEMSTREAM                   \
4540   COMMON_INTERCEPT_FUNCTION(open_memstream);  \
4541   COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
4542   COMMON_INTERCEPT_FUNCTION(fmemopen);
4543 #else
4544 #define INIT_OPEN_MEMSTREAM
4545 #endif
4547 #if SANITIZER_INTERCEPT_OBSTACK
4548 static void initialize_obstack(__sanitizer_obstack *obstack) {
4549   COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
4550   if (obstack->chunk)
4551     COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
4552                                         sizeof(*obstack->chunk));
4555 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
4556             int align, void *(*alloc_fn)(uptr arg, uptr sz),
4557             void (*free_fn)(uptr arg, void *p)) {
4558   void *ctx;
4559   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
4560                            free_fn);
4561   int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
4562   if (res) initialize_obstack(obstack);
4563   return res;
4565 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
4566             int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
4567   void *ctx;
4568   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
4569                            free_fn);
4570   int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
4571   if (res) initialize_obstack(obstack);
4572   return res;
4574 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
4575   void *ctx;
4576   COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
4577   REAL(_obstack_newchunk)(obstack, length);
4578   if (obstack->chunk)
4579     COMMON_INTERCEPTOR_INITIALIZE_RANGE(
4580         obstack->chunk, obstack->next_free - (char *)obstack->chunk);
4582 #define INIT_OBSTACK                           \
4583   COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
4584   COMMON_INTERCEPT_FUNCTION(_obstack_begin);   \
4585   COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
4586 #else
4587 #define INIT_OBSTACK
4588 #endif
4590 #if SANITIZER_INTERCEPT_FFLUSH
4591 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
4592   void *ctx;
4593   COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
4594   int res = REAL(fflush)(fp);
4595   // FIXME: handle fp == NULL
4596   if (fp) {
4597     const FileMetadata *m = GetInterceptorMetadata(fp);
4598     if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
4599   }
4600   return res;
4602 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
4603 #else
4604 #define INIT_FFLUSH
4605 #endif
4607 #if SANITIZER_INTERCEPT_FCLOSE
4608 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
4609   void *ctx;
4610   COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
4611   if (fp) {
4612     COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4613     const FileMetadata *m = GetInterceptorMetadata(fp);
4614     if (m) {
4615       COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
4616       DeleteInterceptorMetadata(fp);
4617     }
4618   }
4619   return REAL(fclose)(fp);
4621 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
4622 #else
4623 #define INIT_FCLOSE
4624 #endif
4626 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
4627 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
4628   void *ctx;
4629   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
4630   void *res = REAL(dlopen)(filename, flag);
4631   COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
4632   return res;
4635 INTERCEPTOR(int, dlclose, void *handle) {
4636   void *ctx;
4637   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
4638   int res = REAL(dlclose)(handle);
4639   COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
4640   return res;
4642 #define INIT_DLOPEN_DLCLOSE          \
4643   COMMON_INTERCEPT_FUNCTION(dlopen); \
4644   COMMON_INTERCEPT_FUNCTION(dlclose);
4645 #else
4646 #define INIT_DLOPEN_DLCLOSE
4647 #endif
4649 #if SANITIZER_INTERCEPT_GETPASS
4650 INTERCEPTOR(char *, getpass, const char *prompt) {
4651   void *ctx;
4652   COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
4653   if (prompt)
4654     COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
4655   char *res = REAL(getpass)(prompt);
4656   if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
4657   return res;
4660 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
4661 #else
4662 #define INIT_GETPASS
4663 #endif
4665 #if SANITIZER_INTERCEPT_TIMERFD
4666 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
4667             void *old_value) {
4668   void *ctx;
4669   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
4670                            old_value);
4671   COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
4672   int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
4673   if (res != -1 && old_value)
4674     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
4675   return res;
4678 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
4679   void *ctx;
4680   COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
4681   int res = REAL(timerfd_gettime)(fd, curr_value);
4682   if (res != -1 && curr_value)
4683     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
4684   return res;
4686 #define INIT_TIMERFD                          \
4687   COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
4688   COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
4689 #else
4690 #define INIT_TIMERFD
4691 #endif
4693 #if SANITIZER_INTERCEPT_MLOCKX
4694 // Linux kernel has a bug that leads to kernel deadlock if a process
4695 // maps TBs of memory and then calls mlock().
4696 static void MlockIsUnsupported() {
4697   static atomic_uint8_t printed;
4698   if (atomic_exchange(&printed, 1, memory_order_relaxed))
4699     return;
4700   VPrintf(1, "INFO: %s ignores mlock/mlockall/munlock/munlockall\n",
4701           SanitizerToolName);
4704 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
4705   MlockIsUnsupported();
4706   return 0;
4709 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
4710   MlockIsUnsupported();
4711   return 0;
4714 INTERCEPTOR(int, mlockall, int flags) {
4715   MlockIsUnsupported();
4716   return 0;
4719 INTERCEPTOR(int, munlockall, void) {
4720   MlockIsUnsupported();
4721   return 0;
4724 #define INIT_MLOCKX                                                            \
4725   COMMON_INTERCEPT_FUNCTION(mlock);                                            \
4726   COMMON_INTERCEPT_FUNCTION(munlock);                                          \
4727   COMMON_INTERCEPT_FUNCTION(mlockall);                                         \
4728   COMMON_INTERCEPT_FUNCTION(munlockall);
4730 #else
4731 #define INIT_MLOCKX
4732 #endif  // SANITIZER_INTERCEPT_MLOCKX
4734 static void InitializeCommonInterceptors() {
4735   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
4736   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
4738   INIT_TEXTDOMAIN;
4739   INIT_STRCMP;
4740   INIT_STRNCMP;
4741   INIT_STRCASECMP;
4742   INIT_STRNCASECMP;
4743   INIT_MEMCHR;
4744   INIT_MEMRCHR;
4745   INIT_READ;
4746   INIT_PREAD;
4747   INIT_PREAD64;
4748   INIT_READV;
4749   INIT_PREADV;
4750   INIT_PREADV64;
4751   INIT_WRITE;
4752   INIT_PWRITE;
4753   INIT_PWRITE64;
4754   INIT_WRITEV;
4755   INIT_PWRITEV;
4756   INIT_PWRITEV64;
4757   INIT_PRCTL;
4758   INIT_LOCALTIME_AND_FRIENDS;
4759   INIT_STRPTIME;
4760   INIT_SCANF;
4761   INIT_ISOC99_SCANF;
4762   INIT_PRINTF;
4763   INIT_ISOC99_PRINTF;
4764   INIT_FREXP;
4765   INIT_FREXPF_FREXPL;
4766   INIT_GETPWNAM_AND_FRIENDS;
4767   INIT_GETPWNAM_R_AND_FRIENDS;
4768   INIT_GETPWENT;
4769   INIT_FGETPWENT;
4770   INIT_GETPWENT_R;
4771   INIT_SETPWENT;
4772   INIT_CLOCK_GETTIME;
4773   INIT_GETITIMER;
4774   INIT_TIME;
4775   INIT_GLOB;
4776   INIT_WAIT;
4777   INIT_WAIT4;
4778   INIT_INET;
4779   INIT_PTHREAD_GETSCHEDPARAM;
4780   INIT_GETADDRINFO;
4781   INIT_GETNAMEINFO;
4782   INIT_GETSOCKNAME;
4783   INIT_GETHOSTBYNAME;
4784   INIT_GETHOSTBYNAME_R;
4785   INIT_GETHOSTBYNAME2_R;
4786   INIT_GETHOSTBYADDR_R;
4787   INIT_GETHOSTENT_R;
4788   INIT_GETSOCKOPT;
4789   INIT_ACCEPT;
4790   INIT_ACCEPT4;
4791   INIT_MODF;
4792   INIT_RECVMSG;
4793   INIT_GETPEERNAME;
4794   INIT_IOCTL;
4795   INIT_INET_ATON;
4796   INIT_SYSINFO;
4797   INIT_READDIR;
4798   INIT_READDIR64;
4799   INIT_PTRACE;
4800   INIT_SETLOCALE;
4801   INIT_GETCWD;
4802   INIT_GET_CURRENT_DIR_NAME;
4803   INIT_STRTOIMAX;
4804   INIT_MBSTOWCS;
4805   INIT_MBSNRTOWCS;
4806   INIT_WCSTOMBS;
4807   INIT_WCSNRTOMBS;
4808   INIT_TCGETATTR;
4809   INIT_REALPATH;
4810   INIT_CANONICALIZE_FILE_NAME;
4811   INIT_CONFSTR;
4812   INIT_SCHED_GETAFFINITY;
4813   INIT_STRERROR;
4814   INIT_STRERROR_R;
4815   INIT_XPG_STRERROR_R;
4816   INIT_SCANDIR;
4817   INIT_SCANDIR64;
4818   INIT_GETGROUPS;
4819   INIT_POLL;
4820   INIT_PPOLL;
4821   INIT_WORDEXP;
4822   INIT_SIGWAIT;
4823   INIT_SIGWAITINFO;
4824   INIT_SIGTIMEDWAIT;
4825   INIT_SIGSETOPS;
4826   INIT_SIGPENDING;
4827   INIT_SIGPROCMASK;
4828   INIT_BACKTRACE;
4829   INIT__EXIT;
4830   INIT_PTHREAD_MUTEX_LOCK;
4831   INIT_PTHREAD_MUTEX_UNLOCK;
4832   INIT_GETMNTENT;
4833   INIT_GETMNTENT_R;
4834   INIT_STATFS;
4835   INIT_STATFS64;
4836   INIT_STATVFS;
4837   INIT_STATVFS64;
4838   INIT_INITGROUPS;
4839   INIT_ETHER_NTOA_ATON;
4840   INIT_ETHER_HOST;
4841   INIT_ETHER_R;
4842   INIT_SHMCTL;
4843   INIT_RANDOM_R;
4844   INIT_PTHREAD_ATTR_GET;
4845   INIT_PTHREAD_ATTR_GETINHERITSCHED;
4846   INIT_PTHREAD_ATTR_GETAFFINITY_NP;
4847   INIT_PTHREAD_MUTEXATTR_GETPSHARED;
4848   INIT_PTHREAD_MUTEXATTR_GETTYPE;
4849   INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
4850   INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
4851   INIT_PTHREAD_MUTEXATTR_GETROBUST;
4852   INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
4853   INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
4854   INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
4855   INIT_PTHREAD_CONDATTR_GETPSHARED;
4856   INIT_PTHREAD_CONDATTR_GETCLOCK;
4857   INIT_PTHREAD_BARRIERATTR_GETPSHARED;
4858   INIT_TMPNAM;
4859   INIT_TMPNAM_R;
4860   INIT_TEMPNAM;
4861   INIT_PTHREAD_SETNAME_NP;
4862   INIT_SINCOS;
4863   INIT_REMQUO;
4864   INIT_LGAMMA;
4865   INIT_LGAMMA_R;
4866   INIT_LGAMMAL_R;
4867   INIT_DRAND48_R;
4868   INIT_RAND_R;
4869   INIT_GETLINE;
4870   INIT_ICONV;
4871   INIT_TIMES;
4872   INIT_TLS_GET_ADDR;
4873   INIT_LISTXATTR;
4874   INIT_GETXATTR;
4875   INIT_GETRESID;
4876   INIT_GETIFADDRS;
4877   INIT_IF_INDEXTONAME;
4878   INIT_CAPGET;
4879   INIT_AEABI_MEM;
4880   INIT___BZERO;
4881   INIT_FTIME;
4882   INIT_XDR;
4883   INIT_TSEARCH;
4884   INIT_LIBIO_INTERNALS;
4885   INIT_FOPEN;
4886   INIT_FOPEN64;
4887   INIT_OPEN_MEMSTREAM;
4888   INIT_OBSTACK;
4889   INIT_FFLUSH;
4890   INIT_FCLOSE;
4891   INIT_DLOPEN_DLCLOSE;
4892   INIT_GETPASS;
4893   INIT_TIMERFD;
4894   INIT_MLOCKX;