1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
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"
38 #if SANITIZER_WINDOWS && !defined(va_copy)
39 #define va_copy(dst, src) ((dst) = (src))
43 #define pthread_setname_np pthread_set_name_np
46 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
47 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
50 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
51 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
54 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
55 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
58 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
59 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
62 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
63 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
66 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
67 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
70 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
71 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
74 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
75 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
78 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
79 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
82 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
83 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, map) {}
86 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
87 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
90 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
91 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
92 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
95 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
96 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
100 // For open_memstream().
105 struct CommonInterceptorMetadata {
115 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
117 static MetadataHashMap *interceptor_metadata_map;
120 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
121 const FileMetadata &file) {
122 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
124 h->type = CommonInterceptorMetadata::CIMT_FILE;
128 UNUSED static const FileMetadata *GetInterceptorMetadata(
129 __sanitizer_FILE *addr) {
130 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
135 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
142 UNUSED static void DeleteInterceptorMetadata(void *addr) {
143 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
146 #endif // SI_NOT_WINDOWS
148 #if SANITIZER_INTERCEPT_TEXTDOMAIN
149 INTERCEPTOR(char*, textdomain, const char *domainname) {
151 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
152 char* domain = REAL(textdomain)(domainname);
154 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
158 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
160 #define INIT_TEXTDOMAIN
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) {
170 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
171 unsigned char c1, c2;
174 c1 = (unsigned char)s1[i];
175 c2 = (unsigned char)s2[i];
176 if (c1 != c2 || c1 == '\0') break;
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);
187 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
188 unsigned char c1 = 0, c2 = 0;
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;
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)
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) {
216 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
217 unsigned char c1 = 0, c2 = 0;
220 c1 = (unsigned char)s1[i];
221 c2 = (unsigned char)s2[i];
222 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
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) {
231 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
232 unsigned char c1 = 0, c2 = 0;
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;
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)
247 #define INIT_STRCASECMP
248 #define INIT_STRNCASECMP
251 #if SANITIZER_INTERCEPT_MEMCHR
252 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
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);
261 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
266 #if SANITIZER_INTERCEPT_MEMRCHR
267 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
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)
279 #if SANITIZER_INTERCEPT_FREXP
280 INTERCEPTOR(double, frexp, double x, int *exp) {
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);
289 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
292 #endif // SANITIZER_INTERCEPT_FREXP
294 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
295 INTERCEPTOR(float, frexpf, float x, int *exp) {
297 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
298 // FIXME: under ASan the call below may write to freed memory and corrupt
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));
306 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
308 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
309 // FIXME: under ASan the call below may write to freed memory and corrupt
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));
317 #define INIT_FREXPF_FREXPL \
318 COMMON_INTERCEPT_FUNCTION(frexpf); \
319 COMMON_INTERCEPT_FUNCTION(frexpl)
321 #define INIT_FREXPF_FREXPL
322 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
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);
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);
345 #if SANITIZER_INTERCEPT_READ
346 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
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
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);
358 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
363 #if SANITIZER_INTERCEPT_PREAD
364 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
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
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);
376 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
381 #if SANITIZER_INTERCEPT_PREAD64
382 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
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
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);
394 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
399 #if SANITIZER_INTERCEPT_READV
400 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
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);
410 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
415 #if SANITIZER_INTERCEPT_PREADV
416 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
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);
426 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
431 #if SANITIZER_INTERCEPT_PREADV64
432 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
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);
442 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
444 #define INIT_PREADV64
447 #if SANITIZER_INTERCEPT_WRITE
448 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
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);
458 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
463 #if SANITIZER_INTERCEPT_PWRITE
464 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
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);
473 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
478 #if SANITIZER_INTERCEPT_PWRITE64
479 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
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);
489 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
491 #define INIT_PWRITE64
494 #if SANITIZER_INTERCEPT_WRITEV
495 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
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);
505 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
510 #if SANITIZER_INTERCEPT_PWRITEV
511 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
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);
521 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
526 #if SANITIZER_INTERCEPT_PWRITEV64
527 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
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);
537 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
539 #define INIT_PWRITEV64
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
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) {
552 internal_strncpy(buff, (char *)arg2, 15);
554 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
558 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
561 #endif // SANITIZER_INTERCEPT_PRCTL
563 #if SANITIZER_INTERCEPT_TIME
564 INTERCEPTOR(unsigned long, time, unsigned long *t) {
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));
575 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(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));
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);
590 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
592 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
593 __sanitizer_tm *res = REAL(localtime)(timep);
595 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
596 unpoison_tm(ctx, res);
600 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
602 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
603 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
605 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
606 unpoison_tm(ctx, res);
610 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
612 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
613 __sanitizer_tm *res = REAL(gmtime)(timep);
615 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
616 unpoison_tm(ctx, res);
620 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
622 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
623 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
625 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
626 unpoison_tm(ctx, res);
630 INTERCEPTOR(char *, ctime, unsigned long *timep) {
632 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
633 // FIXME: under ASan the call below may write to freed memory and corrupt
635 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
636 char *res = REAL(ctime)(timep);
638 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
639 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
643 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
645 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
646 // FIXME: under ASan the call below may write to freed memory and corrupt
648 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
649 char *res = REAL(ctime_r)(timep, result);
651 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
652 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
656 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
658 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
659 // FIXME: under ASan the call below may write to freed memory and corrupt
661 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
662 char *res = REAL(asctime)(tm);
664 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
665 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
669 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
671 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
672 // FIXME: under ASan the call below may write to freed memory and corrupt
674 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
675 char *res = REAL(asctime_r)(tm, result);
677 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
678 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
682 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
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);
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);
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) {
713 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
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
718 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
719 char *res = REAL(strptime)(s, format, tm);
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
725 if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
729 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
731 #define INIT_STRPTIME
734 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
735 #include "sanitizer_common_interceptors_format.inc"
737 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
741 va_start(ap, format); \
742 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
743 int res = WRAP(vname)(__VA_ARGS__, ap); \
750 #if SANITIZER_INTERCEPT_SCANF
752 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
755 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
758 int res = REAL(vname)(__VA_ARGS__); \
760 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
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,
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)
808 #if SANITIZER_INTERCEPT_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);
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);
829 #define INIT_ISOC99_SCANF
832 #if SANITIZER_INTERCEPT_PRINTF
834 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
836 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
840 #define VPRINTF_INTERCEPTOR_RETURN() \
843 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
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(); \
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, ...) \
858 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
859 if (common_flags()->check_printf) { \
860 printf_common(ctx, format, aq); \
862 int res = REAL(vname)(str, __VA_ARGS__); \
864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
866 VPRINTF_INTERCEPTOR_RETURN(); \
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, ...) \
875 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
876 if (common_flags()->check_printf) { \
877 printf_common(ctx, format, aq); \
879 int res = REAL(vname)(str, size, __VA_ARGS__); \
881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
883 VPRINTF_INTERCEPTOR_RETURN(); \
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, ...) \
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); \
897 int res = REAL(vname)(strp, __VA_ARGS__); \
899 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
901 VPRINTF_INTERCEPTOR_RETURN(); \
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,
910 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
912 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
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,
932 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
934 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
936 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
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,
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,
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);
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);
1003 #define INIT_ISOC99_PRINTF
1006 #if SANITIZER_INTERCEPT_IOCTL
1007 #include "sanitizer_common_interceptors_ioctl.inc"
1008 INTERCEPTOR(int, ioctl, int d, unsigned request, void *arg) {
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;
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);
1025 desc = &decoded_desc;
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);
1034 #define INIT_IOCTL \
1036 COMMON_INTERCEPT_FUNCTION(ioctl);
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) {
1046 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1048 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1049 REAL(strlen)(pwd->pw_name) + 1);
1051 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1052 REAL(strlen)(pwd->pw_passwd) + 1);
1053 #if !SANITIZER_ANDROID
1055 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1056 REAL(strlen)(pwd->pw_gecos) + 1);
1060 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1061 REAL(strlen)(pwd->pw_class) + 1);
1064 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1065 REAL(strlen)(pwd->pw_dir) + 1);
1067 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1068 REAL(strlen)(pwd->pw_shell) + 1);
1072 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1076 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1077 REAL(strlen)(grp->gr_name) + 1);
1079 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1080 REAL(strlen)(grp->gr_passwd) + 1);
1081 char **p = grp->gr_mem;
1083 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1085 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1086 (p - grp->gr_mem + 1) * sizeof(*p));
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) {
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);
1103 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1105 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1106 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1107 if (res != 0) unpoison_passwd(ctx, res);
1110 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
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);
1118 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1120 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1121 __sanitizer_group *res = REAL(getgrgid)(gid);
1122 if (res != 0) unpoison_group(ctx, 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);
1131 #define INIT_GETPWNAM_AND_FRIENDS
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) {
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);
1145 if (result && *result) unpoison_passwd(ctx, *result);
1146 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1148 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1151 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1152 SIZE_T buflen, __sanitizer_passwd **result) {
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);
1160 if (result && *result) unpoison_passwd(ctx, *result);
1161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1163 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1166 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1167 char *buf, SIZE_T buflen, __sanitizer_group **result) {
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);
1176 if (result && *result) unpoison_group(ctx, *result);
1177 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1179 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1182 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1183 SIZE_T buflen, __sanitizer_group **result) {
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);
1191 if (result && *result) unpoison_group(ctx, *result);
1192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1194 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
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);
1203 #define INIT_GETPWNAM_R_AND_FRIENDS
1206 #if SANITIZER_INTERCEPT_GETPWENT
1207 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1209 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1210 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1211 if (res != 0) unpoison_passwd(ctx, res);
1214 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1216 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1217 __sanitizer_group *res = REAL(getgrent)(dummy);
1218 if (res != 0) unpoison_group(ctx, res);;
1221 #define INIT_GETPWENT \
1222 COMMON_INTERCEPT_FUNCTION(getpwent); \
1223 COMMON_INTERCEPT_FUNCTION(getgrent);
1225 #define INIT_GETPWENT
1228 #if SANITIZER_INTERCEPT_FGETPWENT
1229 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1231 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1232 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1233 if (res != 0) unpoison_passwd(ctx, res);
1236 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1238 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1239 __sanitizer_group *res = REAL(fgetgrent)(fp);
1240 if (res != 0) unpoison_group(ctx, res);
1243 #define INIT_FGETPWENT \
1244 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1245 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1247 #define INIT_FGETPWENT
1250 #if SANITIZER_INTERCEPT_GETPWENT_R
1251 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1252 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
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);
1260 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1261 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1263 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1266 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1267 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
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);
1275 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1276 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1278 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1281 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1282 __sanitizer_group **pwbufp) {
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);
1290 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1293 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1296 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1297 SIZE_T buflen, __sanitizer_group **pwbufp) {
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);
1305 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1306 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1308 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
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);
1317 #define INIT_GETPWENT_R
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) {
1326 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1327 REAL(setpwent)(dummy);
1329 INTERCEPTOR(void, endpwent, int dummy) {
1331 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1332 REAL(endpwent)(dummy);
1334 INTERCEPTOR(void, setgrent, int dummy) {
1336 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1337 REAL(setgrent)(dummy);
1339 INTERCEPTOR(void, endgrent, int dummy) {
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);
1350 #define INIT_SETPWENT
1353 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1354 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
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);
1362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1366 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
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);
1374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1378 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
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);
1389 #define INIT_CLOCK_GETTIME
1392 #if SANITIZER_INTERCEPT_GETITIMER
1393 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
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);
1405 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1407 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_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);
1419 #define INIT_GETITIMER \
1420 COMMON_INTERCEPT_FUNCTION(getitimer); \
1421 COMMON_INTERCEPT_FUNCTION(setitimer);
1423 #define INIT_GETITIMER
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);
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) {
1473 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1474 __sanitizer_glob_t glob_copy = {
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;
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);
1495 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1499 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1500 int (*errfunc)(const char *epath, int eerrno),
1501 __sanitizer_glob_t *pglob) {
1503 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1504 __sanitizer_glob_t glob_copy = {
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;
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);
1525 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1529 COMMON_INTERCEPT_FUNCTION(glob); \
1530 COMMON_INTERCEPT_FUNCTION(glob64);
1531 #else // SANITIZER_INTERCEPT_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
1539 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
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));
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,
1555 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
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);
1568 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
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));
1579 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
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);
1587 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1588 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1592 #if SANITIZER_ANDROID
1593 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
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);
1601 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1602 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1606 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
1608 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
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);
1616 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1617 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1621 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
1622 #endif // SANITIZER_ANDROID
1624 COMMON_INTERCEPT_FUNCTION(wait); \
1625 COMMON_INTERCEPT_FUNCTION(waitid); \
1626 COMMON_INTERCEPT_FUNCTION(waitpid); \
1627 COMMON_INTERCEPT_FUNCTION(wait3);
1633 #if SANITIZER_INTERCEPT_INET
1634 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
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);
1647 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
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);
1656 uptr sz = __sanitizer_in_addr_sz(af);
1657 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1662 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
1663 COMMON_INTERCEPT_FUNCTION(inet_pton);
1668 #if SANITIZER_INTERCEPT_INET
1669 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
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);
1678 uptr sz = __sanitizer_in_addr_sz(af_inet);
1679 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1683 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
1685 #define INIT_INET_ATON
1688 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
1689 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
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);
1697 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
1698 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
1702 #define INIT_PTHREAD_GETSCHEDPARAM \
1703 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
1705 #define INIT_PTHREAD_GETSCHEDPARAM
1708 #if SANITIZER_INTERCEPT_GETADDRINFO
1709 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
1710 struct __sanitizer_addrinfo *hints,
1711 struct __sanitizer_addrinfo **out) {
1713 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
1714 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
1716 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
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;
1727 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
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);
1738 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
1740 #define INIT_GETADDRINFO
1743 #if SANITIZER_INTERCEPT_GETNAMEINFO
1744 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
1745 unsigned hostlen, char *serv, unsigned servlen, int flags) {
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.
1755 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
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);
1764 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
1766 #define INIT_GETNAMEINFO
1769 #if SANITIZER_INTERCEPT_GETSOCKNAME
1770 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
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);
1780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
1784 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
1786 #define INIT_GETSOCKNAME
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));
1793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
1794 char **p = h->h_aliases;
1796 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1799 COMMON_INTERCEPTOR_WRITE_RANGE(
1800 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
1803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
1806 COMMON_INTERCEPTOR_WRITE_RANGE(
1807 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
1811 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
1812 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
1814 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
1815 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
1816 if (res) write_hostent(ctx, res);
1820 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
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);
1830 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
1832 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
1833 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
1834 if (res) write_hostent(ctx, res);
1838 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
1840 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
1841 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
1842 if (res) write_hostent(ctx, 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);
1851 #define INIT_GETHOSTBYNAME
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,
1859 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
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);
1866 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1867 if (res == 0 && *result) write_hostent(ctx, *result);
1870 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1873 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
1875 #define INIT_GETHOSTBYNAME_R
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) {
1882 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
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);
1889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1890 if (res == 0 && *result) write_hostent(ctx, *result);
1893 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1896 #define INIT_GETHOSTENT_R \
1897 COMMON_INTERCEPT_FUNCTION(gethostent_r);
1899 #define INIT_GETHOSTENT_R
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) {
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,
1916 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1917 if (res == 0 && *result) write_hostent(ctx, *result);
1920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1923 #define INIT_GETHOSTBYADDR_R \
1924 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
1926 #define INIT_GETHOSTBYADDR_R
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) {
1934 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
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.
1940 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
1942 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1943 if (res == 0 && *result) write_hostent(ctx, *result);
1946 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1949 #define INIT_GETHOSTBYNAME2_R \
1950 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
1952 #define INIT_GETHOSTBYNAME2_R
1955 #if SANITIZER_INTERCEPT_GETSOCKOPT
1956 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
1959 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
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);
1967 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
1970 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
1972 #define INIT_GETSOCKOPT
1975 #if SANITIZER_INTERCEPT_ACCEPT
1976 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
1978 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
1979 unsigned addrlen0 = 0;
1981 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1982 addrlen0 = *addrlen;
1984 int fd2 = REAL(accept)(fd, addr, addrlen);
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));
1992 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
1997 #if SANITIZER_INTERCEPT_ACCEPT4
1998 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2000 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2001 unsigned addrlen0 = 0;
2003 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2004 addrlen0 = *addrlen;
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);
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));
2017 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2019 #define INIT_ACCEPT4
2022 #if SANITIZER_INTERCEPT_MODF
2023 INTERCEPTOR(double, modf, double x, double *iptr) {
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);
2031 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2035 INTERCEPTOR(float, modff, float x, float *iptr) {
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);
2043 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2047 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
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);
2055 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2060 COMMON_INTERCEPT_FUNCTION(modf); \
2061 COMMON_INTERCEPT_FUNCTION(modff); \
2062 COMMON_INTERCEPT_FUNCTION(modfl);
2067 #if SANITIZER_INTERCEPT_RECVMSG
2068 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
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,
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);
2090 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2092 write_msghdr(ctx, msg, res);
2093 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2098 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2100 #define INIT_RECVMSG
2103 #if SANITIZER_INTERCEPT_GETPEERNAME
2104 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2106 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
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));
2117 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2119 #define INIT_GETPEERNAME
2122 #if SANITIZER_INTERCEPT_SYSINFO
2123 INTERCEPTOR(int, sysinfo, void *info) {
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);
2131 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2134 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2136 #define INIT_SYSINFO
2139 #if SANITIZER_INTERCEPT_READDIR
2140 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
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);
2151 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2152 __sanitizer_dirent **result) {
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);
2160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2167 #define INIT_READDIR \
2168 COMMON_INTERCEPT_FUNCTION(readdir); \
2169 COMMON_INTERCEPT_FUNCTION(readdir_r);
2171 #define INIT_READDIR
2174 #if SANITIZER_INTERCEPT_READDIR64
2175 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
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);
2186 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2187 __sanitizer_dirent64 **result) {
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);
2195 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2197 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2201 #define INIT_READDIR64 \
2202 COMMON_INTERCEPT_FUNCTION(readdir64); \
2203 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2205 #define INIT_READDIR64
2208 #if SANITIZER_INTERCEPT_PTRACE
2209 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2211 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, 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);
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);
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);
2254 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2259 #if SANITIZER_INTERCEPT_SETLOCALE
2260 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2262 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, 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);
2270 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2272 #define INIT_SETLOCALE
2275 #if SANITIZER_INTERCEPT_GETCWD
2276 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
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);
2286 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2291 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2292 INTERCEPTOR(char *, get_current_dir_name, int fake) {
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);
2303 #define INIT_GET_CURRENT_DIR_NAME \
2304 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2306 #define INIT_GET_CURRENT_DIR_NAME
2309 #if SANITIZER_INTERCEPT_STRTOIMAX
2310 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
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));
2321 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
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));
2332 #define INIT_STRTOIMAX \
2333 COMMON_INTERCEPT_FUNCTION(strtoimax); \
2334 COMMON_INTERCEPT_FUNCTION(strtoumax);
2336 #define INIT_STRTOIMAX
2339 #if SANITIZER_INTERCEPT_MBSTOWCS
2340 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
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));
2354 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
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));
2373 #define INIT_MBSTOWCS \
2374 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
2375 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
2377 #define INIT_MBSTOWCS
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) {
2384 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
2386 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2387 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
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));
2401 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
2403 #define INIT_MBSNRTOWCS
2406 #if SANITIZER_INTERCEPT_WCSTOMBS
2407 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
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);
2421 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
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);
2438 #define INIT_WCSTOMBS \
2439 COMMON_INTERCEPT_FUNCTION(wcstombs); \
2440 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
2442 #define INIT_WCSTOMBS
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) {
2449 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
2451 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2452 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
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);
2466 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
2468 #define INIT_WCSNRTOMBS
2471 #if SANITIZER_INTERCEPT_TCGETATTR
2472 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
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);
2484 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
2486 #define INIT_TCGETATTR
2489 #if SANITIZER_INTERCEPT_REALPATH
2490 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
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;
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);
2508 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
2510 #define INIT_REALPATH
2513 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
2514 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
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);
2522 #define INIT_CANONICALIZE_FILE_NAME \
2523 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
2525 #define INIT_CANONICALIZE_FILE_NAME
2528 #if SANITIZER_INTERCEPT_CONFSTR
2529 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
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);
2537 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
2540 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
2542 #define INIT_CONFSTR
2545 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
2546 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
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);
2556 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
2558 #define INIT_SCHED_GETAFFINITY
2561 #if SANITIZER_INTERCEPT_STRERROR
2562 INTERCEPTOR(char *, strerror, int errnum) {
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);
2569 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
2571 #define INIT_STRERROR
2574 #if SANITIZER_INTERCEPT_STRERROR_R
2575 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
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
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);
2596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2600 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
2602 #define INIT_STRERROR_R
2605 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
2606 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
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.
2615 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2618 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
2620 #define INIT_XPG_STRERROR_R
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) {
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);
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);
2670 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
2672 #define INIT_SCANDIR
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) {
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.
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);
2723 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
2725 #define INIT_SCANDIR64
2728 #if SANITIZER_INTERCEPT_GETGROUPS
2729 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
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));
2739 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
2741 #define INIT_GETGROUPS
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));
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,
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);
2769 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
2774 #if SANITIZER_INTERCEPT_PPOLL
2775 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2776 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
2778 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
2779 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2781 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
2782 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
2784 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
2785 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2788 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
2793 #if SANITIZER_INTERCEPT_WORDEXP
2794 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
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);
2803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
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);
2814 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
2816 #define INIT_WORDEXP
2819 #if SANITIZER_INTERCEPT_SIGWAIT
2820 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
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));
2831 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
2833 #define INIT_SIGWAIT
2836 #if SANITIZER_INTERCEPT_SIGWAITINFO
2837 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
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);
2848 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
2850 #define INIT_SIGWAITINFO
2853 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
2854 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
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);
2867 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
2869 #define INIT_SIGTIMEDWAIT
2872 #if SANITIZER_INTERCEPT_SIGSETOPS
2873 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
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));
2884 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
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));
2894 #define INIT_SIGSETOPS \
2895 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
2896 COMMON_INTERCEPT_FUNCTION(sigfillset);
2898 #define INIT_SIGSETOPS
2901 #if SANITIZER_INTERCEPT_SIGPENDING
2902 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
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));
2912 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
2914 #define INIT_SIGPENDING
2917 #if SANITIZER_INTERCEPT_SIGPROCMASK
2918 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
2919 __sanitizer_sigset_t *oldset) {
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);
2928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
2931 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
2933 #define INIT_SIGPROCMASK
2936 #if SANITIZER_INTERCEPT_BACKTRACE
2937 INTERCEPTOR(int, backtrace, void **buffer, int size) {
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);
2945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
2949 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
2951 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, 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);
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);
2965 #define INIT_BACKTRACE \
2966 COMMON_INTERCEPT_FUNCTION(backtrace); \
2967 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
2969 #define INIT_BACKTRACE
2972 #if SANITIZER_INTERCEPT__EXIT
2973 INTERCEPTOR(void, _exit, int status) {
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);
2985 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
2986 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
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);
2997 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
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)
3008 #define INIT_PTHREAD_MUTEX_LOCK
3009 #define INIT_PTHREAD_MUTEX_UNLOCK
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);
3019 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3020 REAL(strlen)(mnt->mnt_dir) + 1);
3022 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3023 REAL(strlen)(mnt->mnt_type) + 1);
3025 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3026 REAL(strlen)(mnt->mnt_opts) + 1);
3030 #if SANITIZER_INTERCEPT_GETMNTENT
3031 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3033 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3034 __sanitizer_mntent *res = REAL(getmntent)(fp);
3035 if (res) write_mntent(ctx, res);
3038 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3040 #define INIT_GETMNTENT
3043 #if SANITIZER_INTERCEPT_GETMNTENT_R
3044 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3045 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
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);
3052 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3054 #define INIT_GETMNTENT_R
3057 #if SANITIZER_INTERCEPT_STATFS
3058 INTERCEPTOR(int, statfs, char *path, void *buf) {
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);
3069 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
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);
3079 #define INIT_STATFS \
3080 COMMON_INTERCEPT_FUNCTION(statfs); \
3081 COMMON_INTERCEPT_FUNCTION(fstatfs);
3086 #if SANITIZER_INTERCEPT_STATFS64
3087 INTERCEPTOR(int, statfs64, char *path, void *buf) {
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);
3098 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
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);
3108 #define INIT_STATFS64 \
3109 COMMON_INTERCEPT_FUNCTION(statfs64); \
3110 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3112 #define INIT_STATFS64
3115 #if SANITIZER_INTERCEPT_STATVFS
3116 INTERCEPTOR(int, statvfs, char *path, void *buf) {
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);
3127 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
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);
3137 #define INIT_STATVFS \
3138 COMMON_INTERCEPT_FUNCTION(statvfs); \
3139 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3141 #define INIT_STATVFS
3144 #if SANITIZER_INTERCEPT_STATVFS64
3145 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
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);
3156 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
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);
3166 #define INIT_STATVFS64 \
3167 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3168 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3170 #define INIT_STATVFS64
3173 #if SANITIZER_INTERCEPT_INITGROUPS
3174 INTERCEPTOR(int, initgroups, char *user, u32 group) {
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);
3181 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3183 #define INIT_INITGROUPS
3186 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3187 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
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);
3195 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
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));
3203 #define INIT_ETHER_NTOA_ATON \
3204 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3205 COMMON_INTERCEPT_FUNCTION(ether_aton);
3207 #define INIT_ETHER_NTOA_ATON
3210 #if SANITIZER_INTERCEPT_ETHER_HOST
3211 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
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);
3223 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3225 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
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));
3235 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
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);
3245 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3251 #define INIT_ETHER_HOST \
3252 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3253 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3254 COMMON_INTERCEPT_FUNCTION(ether_line);
3256 #define INIT_ETHER_HOST
3259 #if SANITIZER_INTERCEPT_ETHER_R
3260 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
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);
3271 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3272 __sanitizer_ether_addr *addr) {
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));
3283 #define INIT_ETHER_R \
3284 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3285 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3287 #define INIT_ETHER_R
3290 #if SANITIZER_INTERCEPT_SHMCTL
3291 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
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);
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);
3310 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3315 #if SANITIZER_INTERCEPT_RANDOM_R
3316 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
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);
3324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3327 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3329 #define INIT_RANDOM_R
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) { \
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); \
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)
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) {
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);
3376 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3377 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
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 {
3387 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
3388 return REAL(pthread_attr_getstack)(attr, addr, size);
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);
3402 #define INIT_PTHREAD_ATTR_GET
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);
3411 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
3414 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
3415 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
3418 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
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);
3429 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
3430 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
3432 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
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);
3440 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
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);
3448 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
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);
3456 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
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);
3464 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
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);
3472 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
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);
3480 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
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);
3488 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
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);
3496 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
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);
3504 #define INIT_PTHREAD_CONDATTR_GETPSHARED
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);
3512 #define INIT_PTHREAD_CONDATTR_GETCLOCK
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);
3520 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
3523 #if SANITIZER_INTERCEPT_TMPNAM
3524 INTERCEPTOR(char *, tmpnam, char *s) {
3526 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
3527 char *res = REAL(tmpnam)(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);
3535 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3539 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
3544 #if SANITIZER_INTERCEPT_TMPNAM_R
3545 INTERCEPTOR(char *, tmpnam_r, char *s) {
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);
3555 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
3557 #define INIT_TMPNAM_R
3560 #if SANITIZER_INTERCEPT_TEMPNAM
3561 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
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);
3570 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
3572 #define INIT_TEMPNAM
3575 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
3576 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
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);
3584 #define INIT_PTHREAD_SETNAME_NP
3587 #if SANITIZER_INTERCEPT_SINCOS
3588 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
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) {
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) {
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);
3626 #if SANITIZER_INTERCEPT_REMQUO
3627 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
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));
3637 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
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));
3647 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
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));
3657 #define INIT_REMQUO \
3658 COMMON_INTERCEPT_FUNCTION(remquo); \
3659 COMMON_INTERCEPT_FUNCTION(remquof); \
3660 COMMON_INTERCEPT_FUNCTION(remquol);
3665 #if SANITIZER_INTERCEPT_LGAMMA
3667 INTERCEPTOR(double, lgamma, double x) {
3669 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
3670 double res = REAL(lgamma)(x);
3671 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3674 INTERCEPTOR(float, lgammaf, float x) {
3676 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
3677 float res = REAL(lgammaf)(x);
3678 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3681 INTERCEPTOR(long double, lgammal, long double x) {
3683 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
3684 long double res = REAL(lgammal)(x);
3685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3688 #define INIT_LGAMMA \
3689 COMMON_INTERCEPT_FUNCTION(lgamma); \
3690 COMMON_INTERCEPT_FUNCTION(lgammaf); \
3691 COMMON_INTERCEPT_FUNCTION(lgammal);
3696 #if SANITIZER_INTERCEPT_LGAMMA_R
3697 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
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));
3707 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
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));
3717 #define INIT_LGAMMA_R \
3718 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
3719 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
3721 #define INIT_LGAMMA_R
3724 #if SANITIZER_INTERCEPT_LGAMMAL_R
3725 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
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));
3735 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION(lgammal_r);
3737 #define INIT_LGAMMAL_R
3740 #if SANITIZER_INTERCEPT_DRAND48_R
3741 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
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));
3751 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
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));
3761 #define INIT_DRAND48_R \
3762 COMMON_INTERCEPT_FUNCTION(drand48_r); \
3763 COMMON_INTERCEPT_FUNCTION(lrand48_r);
3765 #define INIT_DRAND48_R
3768 #if SANITIZER_INTERCEPT_RAND_R
3769 INTERCEPTOR(int, rand_r, unsigned *seedp) {
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);
3780 #if SANITIZER_INTERCEPT_GETLINE
3781 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
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);
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);
3795 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
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);
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);
3810 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
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);
3819 #define INIT_GETLINE
3822 #if SANITIZER_INTERCEPT_ICONV
3823 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
3824 char **outbuf, SIZE_T *outbytesleft) {
3826 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
3829 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
3830 if (inbuf && inbytesleft)
3831 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
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);
3845 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
3850 #if SANITIZER_INTERCEPT_TIMES
3851 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
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);
3862 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
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) {
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);
3875 // New DTLS block has been allocated.
3876 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
3881 #define INIT_TLS_GET_ADDR
3884 #if SANITIZER_INTERCEPT_LISTXATTR
3885 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
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);
3898 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
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);
3909 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
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);
3919 #define INIT_LISTXATTR \
3920 COMMON_INTERCEPT_FUNCTION(listxattr); \
3921 COMMON_INTERCEPT_FUNCTION(llistxattr); \
3922 COMMON_INTERCEPT_FUNCTION(flistxattr);
3924 #define INIT_LISTXATTR
3927 #if SANITIZER_INTERCEPT_GETXATTR
3928 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
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);
3941 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
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);
3954 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
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);
3966 #define INIT_GETXATTR \
3967 COMMON_INTERCEPT_FUNCTION(getxattr); \
3968 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
3969 COMMON_INTERCEPT_FUNCTION(fgetxattr);
3971 #define INIT_GETXATTR
3974 #if SANITIZER_INTERCEPT_GETRESID
3975 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
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);
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);
3989 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
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);
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);
4003 #define INIT_GETRESID \
4004 COMMON_INTERCEPT_FUNCTION(getresuid); \
4005 COMMON_INTERCEPT_FUNCTION(getresgid);
4007 #define INIT_GETRESID
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) {
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;
4025 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4027 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4028 REAL(strlen)(p->ifa_name) + 1);
4030 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
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.
4036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4037 // FIXME(smatveev): Unpoison p->ifa_data as well.
4043 #define INIT_GETIFADDRS \
4044 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4046 #define INIT_GETIFADDRS
4049 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4050 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
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);
4058 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4061 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4063 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, 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);
4072 #define INIT_IF_INDEXTONAME
4075 #if SANITIZER_INTERCEPT_CAPGET
4076 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4078 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
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.
4092 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4094 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4096 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
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);
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);
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);
4175 #define INIT___BZERO
4176 #endif // SANITIZER_INTERCEPT___BZERO
4178 #if SANITIZER_INTERCEPT_FTIME
4179 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
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);
4187 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4190 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(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) {
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);
4212 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
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) { \
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)); \
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,
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);
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);
4288 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
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);
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));
4303 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
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);
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 *)) {
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 *));
4356 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
4358 #define INIT_TSEARCH
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
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) {
4377 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
4378 int res = REAL(__uflow)(fp);
4382 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
4384 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
4385 int res = REAL(__underflow)(fp);
4389 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
4391 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
4392 int res = REAL(__overflow)(fp, ch);
4396 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
4398 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
4399 int res = REAL(__wuflow)(fp);
4403 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
4405 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
4406 int res = REAL(__wunderflow)(fp);
4410 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
4412 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
4413 int res = REAL(__woverflow)(fp, ch);
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);
4425 #define INIT_LIBIO_INTERNALS
4428 #if SANITIZER_INTERCEPT_FOPEN
4429 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
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);
4439 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
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);
4447 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
4448 __sanitizer_FILE *fp) {
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);
4459 #define INIT_FOPEN \
4460 COMMON_INTERCEPT_FUNCTION(fopen); \
4461 COMMON_INTERCEPT_FUNCTION(fdopen); \
4462 COMMON_INTERCEPT_FUNCTION(freopen);
4467 #if SANITIZER_INTERCEPT_FOPEN64
4468 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
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);
4478 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
4479 __sanitizer_FILE *fp) {
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);
4490 #define INIT_FOPEN64 \
4491 COMMON_INTERCEPT_FUNCTION(fopen64); \
4492 COMMON_INTERCEPT_FUNCTION(freopen64);
4494 #define INIT_FOPEN64
4497 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
4498 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
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);
4506 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
4507 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
4509 FileMetadata file = {ptr, sizeloc};
4510 SetInterceptorMetadata(res, file);
4514 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
4517 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
4518 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
4520 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
4521 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
4523 FileMetadata file = {(char **)ptr, sizeloc};
4524 SetInterceptorMetadata(res, file);
4528 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
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);
4539 #define INIT_OPEN_MEMSTREAM \
4540 COMMON_INTERCEPT_FUNCTION(open_memstream); \
4541 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
4542 COMMON_INTERCEPT_FUNCTION(fmemopen);
4544 #define INIT_OPEN_MEMSTREAM
4547 #if SANITIZER_INTERCEPT_OBSTACK
4548 static void initialize_obstack(__sanitizer_obstack *obstack) {
4549 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
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)) {
4559 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
4561 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
4562 if (res) initialize_obstack(obstack);
4565 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
4566 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
4568 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
4570 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
4571 if (res) initialize_obstack(obstack);
4574 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
4576 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
4577 REAL(_obstack_newchunk)(obstack, length);
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);
4587 #define INIT_OBSTACK
4590 #if SANITIZER_INTERCEPT_FFLUSH
4591 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
4593 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
4594 int res = REAL(fflush)(fp);
4595 // FIXME: handle fp == NULL
4597 const FileMetadata *m = GetInterceptorMetadata(fp);
4598 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
4602 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
4607 #if SANITIZER_INTERCEPT_FCLOSE
4608 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
4610 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
4612 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4613 const FileMetadata *m = GetInterceptorMetadata(fp);
4615 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
4616 DeleteInterceptorMetadata(fp);
4619 return REAL(fclose)(fp);
4621 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
4626 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
4627 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
4629 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
4630 void *res = REAL(dlopen)(filename, flag);
4631 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
4635 INTERCEPTOR(int, dlclose, void *handle) {
4637 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
4638 int res = REAL(dlclose)(handle);
4639 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
4642 #define INIT_DLOPEN_DLCLOSE \
4643 COMMON_INTERCEPT_FUNCTION(dlopen); \
4644 COMMON_INTERCEPT_FUNCTION(dlclose);
4646 #define INIT_DLOPEN_DLCLOSE
4649 #if SANITIZER_INTERCEPT_GETPASS
4650 INTERCEPTOR(char *, getpass, const char *prompt) {
4652 COMMON_INTERCEPTOR_ENTER(ctx, getpass, 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);
4660 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
4662 #define INIT_GETPASS
4665 #if SANITIZER_INTERCEPT_TIMERFD
4666 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
4669 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_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);
4678 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
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);
4686 #define INIT_TIMERFD \
4687 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
4688 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
4690 #define INIT_TIMERFD
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))
4700 VPrintf(1, "INFO: %s ignores mlock/mlockall/munlock/munlockall\n",
4704 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
4705 MlockIsUnsupported();
4709 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
4710 MlockIsUnsupported();
4714 INTERCEPTOR(int, mlockall, int flags) {
4715 MlockIsUnsupported();
4719 INTERCEPTOR(int, munlockall, void) {
4720 MlockIsUnsupported();
4724 #define INIT_MLOCKX \
4725 COMMON_INTERCEPT_FUNCTION(mlock); \
4726 COMMON_INTERCEPT_FUNCTION(munlock); \
4727 COMMON_INTERCEPT_FUNCTION(mlockall); \
4728 COMMON_INTERCEPT_FUNCTION(munlockall);
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();
4758 INIT_LOCALTIME_AND_FRIENDS;
4766 INIT_GETPWNAM_AND_FRIENDS;
4767 INIT_GETPWNAM_R_AND_FRIENDS;
4779 INIT_PTHREAD_GETSCHEDPARAM;
4784 INIT_GETHOSTBYNAME_R;
4785 INIT_GETHOSTBYNAME2_R;
4786 INIT_GETHOSTBYADDR_R;
4802 INIT_GET_CURRENT_DIR_NAME;
4810 INIT_CANONICALIZE_FILE_NAME;
4812 INIT_SCHED_GETAFFINITY;
4815 INIT_XPG_STRERROR_R;
4830 INIT_PTHREAD_MUTEX_LOCK;
4831 INIT_PTHREAD_MUTEX_UNLOCK;
4839 INIT_ETHER_NTOA_ATON;
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;
4861 INIT_PTHREAD_SETNAME_NP;
4877 INIT_IF_INDEXTONAME;
4884 INIT_LIBIO_INTERNALS;
4887 INIT_OPEN_MEMSTREAM;
4891 INIT_DLOPEN_DLCLOSE;