1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common function interceptors for tools like AddressSanitizer,
11 // ThreadSanitizer, MemorySanitizer, etc.
13 // This file should be included into the tool's interceptor file,
14 // which has to define it's own macros:
15 // COMMON_INTERCEPTOR_ENTER
16 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
17 // COMMON_INTERCEPTOR_READ_RANGE
18 // COMMON_INTERCEPTOR_WRITE_RANGE
19 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
20 // COMMON_INTERCEPTOR_DIR_ACQUIRE
21 // COMMON_INTERCEPTOR_FD_ACQUIRE
22 // COMMON_INTERCEPTOR_FD_RELEASE
23 // COMMON_INTERCEPTOR_FD_ACCESS
24 // COMMON_INTERCEPTOR_SET_THREAD_NAME
25 // COMMON_INTERCEPTOR_ON_EXIT
26 // COMMON_INTERCEPTOR_MUTEX_LOCK
27 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
28 // COMMON_INTERCEPTOR_MUTEX_REPAIR
29 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
30 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
31 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
32 //===----------------------------------------------------------------------===//
33 #include "interception/interception.h"
34 #include "sanitizer_addrhashmap.h"
35 #include "sanitizer_placement_new.h"
36 #include "sanitizer_platform_interceptors.h"
37 #include "sanitizer_tls_get_addr.h"
41 #if SANITIZER_WINDOWS && !defined(va_copy)
42 #define va_copy(dst, src) ((dst) = (src))
46 #define pthread_setname_np pthread_set_name_np
47 #define inet_aton __inet_aton
48 #define inet_pton __inet_pton
51 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
52 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
55 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
56 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
59 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
60 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
63 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
64 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
67 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
68 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
71 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
72 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
75 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
76 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
79 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
80 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
83 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
84 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
87 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
88 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
91 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
92 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
95 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
96 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
97 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
100 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
101 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
104 struct FileMetadata {
105 // For open_memstream().
110 struct CommonInterceptorMetadata {
120 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
122 static MetadataHashMap *interceptor_metadata_map;
125 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
126 const FileMetadata &file) {
127 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
129 h->type = CommonInterceptorMetadata::CIMT_FILE;
133 UNUSED static const FileMetadata *GetInterceptorMetadata(
134 __sanitizer_FILE *addr) {
135 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
140 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
147 UNUSED static void DeleteInterceptorMetadata(void *addr) {
148 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
151 #endif // SI_NOT_WINDOWS
153 #if SANITIZER_INTERCEPT_TEXTDOMAIN
154 INTERCEPTOR(char*, textdomain, const char *domainname) {
156 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
157 char* domain = REAL(textdomain)(domainname);
159 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
163 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
165 #define INIT_TEXTDOMAIN
168 #if SANITIZER_INTERCEPT_STRCMP
169 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
170 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
173 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
175 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
176 unsigned char c1, c2;
179 c1 = (unsigned char)s1[i];
180 c2 = (unsigned char)s2[i];
181 if (c1 != c2 || c1 == '\0') break;
183 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
184 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
185 return CharCmpX(c1, c2);
188 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
189 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
190 return internal_strncmp(s1, s2, size);
192 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
193 unsigned char c1 = 0, c2 = 0;
195 for (i = 0; i < size; i++) {
196 c1 = (unsigned char)s1[i];
197 c2 = (unsigned char)s2[i];
198 if (c1 != c2 || c1 == '\0') break;
200 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
201 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
202 return CharCmpX(c1, c2);
205 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
206 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
212 #if SANITIZER_INTERCEPT_STRCASECMP
213 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
214 int c1_low = ToLower(c1);
215 int c2_low = ToLower(c2);
216 return c1_low - c2_low;
219 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
221 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
222 unsigned char c1 = 0, c2 = 0;
225 c1 = (unsigned char)s1[i];
226 c2 = (unsigned char)s2[i];
227 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
229 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
230 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
231 return CharCaseCmp(c1, c2);
234 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
236 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
237 unsigned char c1 = 0, c2 = 0;
239 for (i = 0; i < n; i++) {
240 c1 = (unsigned char)s1[i];
241 c2 = (unsigned char)s2[i];
242 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
244 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
245 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
246 return CharCaseCmp(c1, c2);
249 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
250 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
252 #define INIT_STRCASECMP
253 #define INIT_STRNCASECMP
256 #if SANITIZER_INTERCEPT_MEMCHR
257 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
259 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
260 void *res = REAL(memchr)(s, c, n);
261 uptr len = res ? (char *)res - (const char *)s + 1 : n;
262 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
266 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
271 #if SANITIZER_INTERCEPT_MEMRCHR
272 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
274 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
275 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
276 return REAL(memrchr)(s, c, n);
279 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
284 #if SANITIZER_INTERCEPT_FREXP
285 INTERCEPTOR(double, frexp, double x, int *exp) {
287 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
288 // Assuming frexp() always writes to |exp|.
289 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
290 double res = REAL(frexp)(x, exp);
294 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
297 #endif // SANITIZER_INTERCEPT_FREXP
299 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
300 INTERCEPTOR(float, frexpf, float x, int *exp) {
302 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
303 // FIXME: under ASan the call below may write to freed memory and corrupt
305 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
306 float res = REAL(frexpf)(x, exp);
307 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
311 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
313 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
314 // FIXME: under ASan the call below may write to freed memory and corrupt
316 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
317 long double res = REAL(frexpl)(x, exp);
318 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
322 #define INIT_FREXPF_FREXPL \
323 COMMON_INTERCEPT_FUNCTION(frexpf); \
324 COMMON_INTERCEPT_FUNCTION(frexpl)
326 #define INIT_FREXPF_FREXPL
327 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
330 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
331 SIZE_T iovlen, SIZE_T maxlen) {
332 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
333 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
339 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
340 SIZE_T iovlen, SIZE_T maxlen) {
341 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
342 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
343 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
344 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
350 #if SANITIZER_INTERCEPT_READ
351 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
353 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
354 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
355 // FIXME: under ASan the call below may write to freed memory and corrupt
357 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
358 SSIZE_T res = REAL(read)(fd, ptr, count);
359 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
360 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
363 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
368 #if SANITIZER_INTERCEPT_PREAD
369 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
371 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
372 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
373 // FIXME: under ASan the call below may write to freed memory and corrupt
375 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
376 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
377 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
378 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
381 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
386 #if SANITIZER_INTERCEPT_PREAD64
387 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
389 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
390 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
391 // FIXME: under ASan the call below may write to freed memory and corrupt
393 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
394 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
395 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
396 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
399 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
404 #if SANITIZER_INTERCEPT_READV
405 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
408 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
409 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
410 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
411 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
412 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
415 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
420 #if SANITIZER_INTERCEPT_PREADV
421 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
424 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
425 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
426 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
427 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
428 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
431 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
436 #if SANITIZER_INTERCEPT_PREADV64
437 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
440 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
441 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
442 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
443 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
444 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
447 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
449 #define INIT_PREADV64
452 #if SANITIZER_INTERCEPT_WRITE
453 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
455 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
456 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
457 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
458 SSIZE_T res = REAL(write)(fd, ptr, count);
459 // FIXME: this check should be _before_ the call to REAL(write), not after
460 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
463 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
468 #if SANITIZER_INTERCEPT_PWRITE
469 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
471 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
472 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
473 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
474 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
475 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
478 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
483 #if SANITIZER_INTERCEPT_PWRITE64
484 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
487 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
488 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
489 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
490 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
491 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
494 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
496 #define INIT_PWRITE64
499 #if SANITIZER_INTERCEPT_WRITEV
500 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
503 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
504 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
505 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
506 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
507 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
510 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
515 #if SANITIZER_INTERCEPT_PWRITEV
516 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
519 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
520 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
521 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
522 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
523 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
526 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
531 #if SANITIZER_INTERCEPT_PWRITEV64
532 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
535 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
536 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
537 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
538 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
539 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
542 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
544 #define INIT_PWRITEV64
547 #if SANITIZER_INTERCEPT_PRCTL
548 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
549 unsigned long arg3, // NOLINT
550 unsigned long arg4, unsigned long arg5) { // NOLINT
552 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
553 static const int PR_SET_NAME = 15;
554 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
555 if (option == PR_SET_NAME) {
557 internal_strncpy(buff, (char *)arg2, 15);
559 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
563 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
566 #endif // SANITIZER_INTERCEPT_PRCTL
568 #if SANITIZER_INTERCEPT_TIME
569 INTERCEPTOR(unsigned long, time, unsigned long *t) {
571 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
572 unsigned long local_t;
573 unsigned long res = REAL(time)(&local_t);
574 if (t && res != (unsigned long)-1) {
575 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
580 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
583 #endif // SANITIZER_INTERCEPT_TIME
585 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
586 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
587 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
589 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
590 // can point to shared memory and tsan would report a data race.
591 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
592 REAL(strlen(tm->tm_zone)) + 1);
595 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
597 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
598 __sanitizer_tm *res = REAL(localtime)(timep);
600 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
601 unpoison_tm(ctx, res);
605 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
607 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
608 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
610 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
611 unpoison_tm(ctx, res);
615 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
617 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
618 __sanitizer_tm *res = REAL(gmtime)(timep);
620 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
621 unpoison_tm(ctx, res);
625 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
627 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
628 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
630 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
631 unpoison_tm(ctx, res);
635 INTERCEPTOR(char *, ctime, unsigned long *timep) {
637 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
638 // FIXME: under ASan the call below may write to freed memory and corrupt
640 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
641 char *res = REAL(ctime)(timep);
643 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
644 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
648 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
650 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
651 // FIXME: under ASan the call below may write to freed memory and corrupt
653 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
654 char *res = REAL(ctime_r)(timep, result);
656 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
661 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
663 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
664 // FIXME: under ASan the call below may write to freed memory and corrupt
666 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
667 char *res = REAL(asctime)(tm);
669 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
670 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
674 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
676 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
677 // FIXME: under ASan the call below may write to freed memory and corrupt
679 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
680 char *res = REAL(asctime_r)(tm, result);
682 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
683 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
687 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
689 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
690 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
691 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
692 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
693 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
694 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
695 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
696 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
697 long res = REAL(mktime)(tm);
698 if (res != -1) unpoison_tm(ctx, tm);
701 #define INIT_LOCALTIME_AND_FRIENDS \
702 COMMON_INTERCEPT_FUNCTION(localtime); \
703 COMMON_INTERCEPT_FUNCTION(localtime_r); \
704 COMMON_INTERCEPT_FUNCTION(gmtime); \
705 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
706 COMMON_INTERCEPT_FUNCTION(ctime); \
707 COMMON_INTERCEPT_FUNCTION(ctime_r); \
708 COMMON_INTERCEPT_FUNCTION(asctime); \
709 COMMON_INTERCEPT_FUNCTION(asctime_r); \
710 COMMON_INTERCEPT_FUNCTION(mktime);
712 #define INIT_LOCALTIME_AND_FRIENDS
713 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
715 #if SANITIZER_INTERCEPT_STRPTIME
716 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
718 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
720 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
721 // FIXME: under ASan the call below may write to freed memory and corrupt
723 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
724 char *res = REAL(strptime)(s, format, tm);
726 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, res - s);
727 // Do not call unpoison_tm here, because strptime does not, in fact,
728 // initialize the entire struct tm. For example, tm_zone pointer is left
730 if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
734 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
736 #define INIT_STRPTIME
739 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
740 #include "sanitizer_common_interceptors_format.inc"
742 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
746 va_start(ap, format); \
747 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
748 int res = WRAP(vname)(__VA_ARGS__, ap); \
755 #if SANITIZER_INTERCEPT_SCANF
757 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
760 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
763 int res = REAL(vname)(__VA_ARGS__); \
765 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
770 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
771 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
773 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
774 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
776 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
777 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
779 #if SANITIZER_INTERCEPT_ISOC99_SCANF
780 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
781 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
783 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
785 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
787 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
788 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
789 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
791 INTERCEPTOR(int, scanf, const char *format, ...)
792 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
794 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
795 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
797 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
798 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
800 #if SANITIZER_INTERCEPT_ISOC99_SCANF
801 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
802 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
804 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
805 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
807 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
808 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
813 #if SANITIZER_INTERCEPT_SCANF
815 COMMON_INTERCEPT_FUNCTION(scanf); \
816 COMMON_INTERCEPT_FUNCTION(sscanf); \
817 COMMON_INTERCEPT_FUNCTION(fscanf); \
818 COMMON_INTERCEPT_FUNCTION(vscanf); \
819 COMMON_INTERCEPT_FUNCTION(vsscanf); \
820 COMMON_INTERCEPT_FUNCTION(vfscanf);
825 #if SANITIZER_INTERCEPT_ISOC99_SCANF
826 #define INIT_ISOC99_SCANF \
827 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
828 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
829 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
830 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
831 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
832 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
834 #define INIT_ISOC99_SCANF
837 #if SANITIZER_INTERCEPT_PRINTF
839 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
841 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
845 #define VPRINTF_INTERCEPTOR_RETURN() \
848 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
850 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
851 if (common_flags()->check_printf) \
852 printf_common(ctx, format, aq); \
853 int res = REAL(vname)(__VA_ARGS__); \
854 VPRINTF_INTERCEPTOR_RETURN(); \
858 // FIXME: under ASan the REAL() call below may write to freed memory and
859 // corrupt its metadata. See
860 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
861 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
863 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
864 if (common_flags()->check_printf) { \
865 printf_common(ctx, format, aq); \
867 int res = REAL(vname)(str, __VA_ARGS__); \
869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
871 VPRINTF_INTERCEPTOR_RETURN(); \
875 // FIXME: under ASan the REAL() call below may write to freed memory and
876 // corrupt its metadata. See
877 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
878 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
880 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
881 if (common_flags()->check_printf) { \
882 printf_common(ctx, format, aq); \
884 int res = REAL(vname)(str, size, __VA_ARGS__); \
886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
888 VPRINTF_INTERCEPTOR_RETURN(); \
892 // FIXME: under ASan the REAL() call below may write to freed memory and
893 // corrupt its metadata. See
894 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
895 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
897 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
898 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
899 if (common_flags()->check_printf) { \
900 printf_common(ctx, format, aq); \
902 int res = REAL(vname)(strp, __VA_ARGS__); \
904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
906 VPRINTF_INTERCEPTOR_RETURN(); \
910 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
911 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
913 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
915 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
917 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
919 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
921 #if SANITIZER_INTERCEPT_PRINTF_L
922 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
923 const char *format, va_list ap)
924 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
926 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
927 const char *format, ...)
928 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
929 #endif // SANITIZER_INTERCEPT_PRINTF_L
931 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
932 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
934 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
935 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
937 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
938 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
939 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
941 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
942 const char *format, va_list ap)
943 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
945 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
947 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
949 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
951 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
954 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
956 INTERCEPTOR(int, printf, const char *format, ...)
957 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
959 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
960 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
962 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
963 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
965 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
966 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
968 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
969 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
971 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
972 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
973 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
975 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
977 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
979 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
980 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
982 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
983 const char *format, ...)
984 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
987 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
989 #endif // SANITIZER_INTERCEPT_PRINTF
991 #if SANITIZER_INTERCEPT_PRINTF
992 #define INIT_PRINTF \
993 COMMON_INTERCEPT_FUNCTION(printf); \
994 COMMON_INTERCEPT_FUNCTION(sprintf); \
995 COMMON_INTERCEPT_FUNCTION(snprintf); \
996 COMMON_INTERCEPT_FUNCTION(asprintf); \
997 COMMON_INTERCEPT_FUNCTION(fprintf); \
998 COMMON_INTERCEPT_FUNCTION(vprintf); \
999 COMMON_INTERCEPT_FUNCTION(vsprintf); \
1000 COMMON_INTERCEPT_FUNCTION(vsnprintf); \
1001 COMMON_INTERCEPT_FUNCTION(vasprintf); \
1002 COMMON_INTERCEPT_FUNCTION(vfprintf);
1007 #if SANITIZER_INTERCEPT_PRINTF_L
1008 #define INIT_PRINTF_L \
1009 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1010 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1012 #define INIT_PRINTF_L
1015 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1016 #define INIT_ISOC99_PRINTF \
1017 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1018 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1019 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1020 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1021 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1022 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1023 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1024 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1026 #define INIT_ISOC99_PRINTF
1029 #if SANITIZER_INTERCEPT_IOCTL
1030 #include "sanitizer_common_interceptors_ioctl.inc"
1031 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1034 va_start(ap, request);
1035 void *arg = va_arg(ap, void *);
1037 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1039 CHECK(ioctl_initialized);
1041 // Note: TSan does not use common flags, and they are zero-initialized.
1042 // This effectively disables ioctl handling in TSan.
1043 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1045 // Although request is unsigned long, the rest of the interceptor uses it
1046 // as just "unsigned" to save space, because we know that all values fit in
1047 // "unsigned" - they are compile-time constants.
1049 const ioctl_desc *desc = ioctl_lookup(request);
1050 ioctl_desc decoded_desc;
1052 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1053 if (!ioctl_decode(request, &decoded_desc))
1054 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1056 desc = &decoded_desc;
1059 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1060 int res = REAL(ioctl)(d, request, arg);
1061 // FIXME: some ioctls have different return values for success and failure.
1062 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1065 #define INIT_IOCTL \
1067 COMMON_INTERCEPT_FUNCTION(ioctl);
1072 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1073 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1074 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1075 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1077 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1079 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1080 REAL(strlen)(pwd->pw_name) + 1);
1082 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1083 REAL(strlen)(pwd->pw_passwd) + 1);
1084 #if !SANITIZER_ANDROID
1086 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1087 REAL(strlen)(pwd->pw_gecos) + 1);
1091 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1092 REAL(strlen)(pwd->pw_class) + 1);
1095 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1096 REAL(strlen)(pwd->pw_dir) + 1);
1098 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1099 REAL(strlen)(pwd->pw_shell) + 1);
1103 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1105 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1107 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1108 REAL(strlen)(grp->gr_name) + 1);
1110 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1111 REAL(strlen)(grp->gr_passwd) + 1);
1112 char **p = grp->gr_mem;
1114 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1116 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1117 (p - grp->gr_mem + 1) * sizeof(*p));
1120 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1121 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1122 // SANITIZER_INTERCEPT_GETPWENT_R ||
1123 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1125 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1126 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1128 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1129 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1130 __sanitizer_passwd *res = REAL(getpwnam)(name);
1131 if (res != 0) unpoison_passwd(ctx, res);
1134 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1136 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1137 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1138 if (res != 0) unpoison_passwd(ctx, res);
1141 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1143 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1144 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1145 __sanitizer_group *res = REAL(getgrnam)(name);
1146 if (res != 0) unpoison_group(ctx, res);
1149 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1151 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1152 __sanitizer_group *res = REAL(getgrgid)(gid);
1153 if (res != 0) unpoison_group(ctx, res);
1156 #define INIT_GETPWNAM_AND_FRIENDS \
1157 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1158 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1159 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1160 COMMON_INTERCEPT_FUNCTION(getgrgid);
1162 #define INIT_GETPWNAM_AND_FRIENDS
1165 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1166 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1167 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1169 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, 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(getpwnam_r)(name, pwd, buf, buflen, result);
1176 if (result && *result) unpoison_passwd(ctx, *result);
1177 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1179 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1182 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1183 SIZE_T buflen, __sanitizer_passwd **result) {
1185 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, 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(getpwuid_r)(uid, pwd, buf, buflen, result);
1191 if (result && *result) unpoison_passwd(ctx, *result);
1192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1194 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1197 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1198 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1200 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1201 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1202 // FIXME: under ASan the call below may write to freed memory and corrupt
1203 // its metadata. See
1204 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1205 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1207 if (result && *result) unpoison_group(ctx, *result);
1208 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1210 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1213 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1214 SIZE_T buflen, __sanitizer_group **result) {
1216 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1217 // FIXME: under ASan the call below may write to freed memory and corrupt
1218 // its metadata. See
1219 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1220 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1222 if (result && *result) unpoison_group(ctx, *result);
1223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1225 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1228 #define INIT_GETPWNAM_R_AND_FRIENDS \
1229 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1230 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1231 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1232 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1234 #define INIT_GETPWNAM_R_AND_FRIENDS
1237 #if SANITIZER_INTERCEPT_GETPWENT
1238 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1240 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1241 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1242 if (res != 0) unpoison_passwd(ctx, res);
1245 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1247 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1248 __sanitizer_group *res = REAL(getgrent)(dummy);
1249 if (res != 0) unpoison_group(ctx, res);;
1252 #define INIT_GETPWENT \
1253 COMMON_INTERCEPT_FUNCTION(getpwent); \
1254 COMMON_INTERCEPT_FUNCTION(getgrent);
1256 #define INIT_GETPWENT
1259 #if SANITIZER_INTERCEPT_FGETPWENT
1260 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1262 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1263 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1264 if (res != 0) unpoison_passwd(ctx, res);
1267 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1269 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1270 __sanitizer_group *res = REAL(fgetgrent)(fp);
1271 if (res != 0) unpoison_group(ctx, res);
1274 #define INIT_FGETPWENT \
1275 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1276 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1278 #define INIT_FGETPWENT
1281 #if SANITIZER_INTERCEPT_GETPWENT_R
1282 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1283 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1285 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1286 // FIXME: under ASan the call below may write to freed memory and corrupt
1287 // its metadata. See
1288 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1289 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1291 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1294 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1297 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1298 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1300 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1301 // FIXME: under ASan the call below may write to freed memory and corrupt
1302 // its metadata. See
1303 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1304 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1306 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1307 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1309 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1312 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1313 __sanitizer_group **pwbufp) {
1315 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1316 // FIXME: under ASan the call below may write to freed memory and corrupt
1317 // its metadata. See
1318 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1319 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1321 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1324 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1327 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1328 SIZE_T buflen, __sanitizer_group **pwbufp) {
1330 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1331 // FIXME: under ASan the call below may write to freed memory and corrupt
1332 // its metadata. See
1333 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1334 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1336 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1339 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1342 #define INIT_GETPWENT_R \
1343 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
1344 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1345 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
1346 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1348 #define INIT_GETPWENT_R
1351 #if SANITIZER_INTERCEPT_SETPWENT
1352 // The only thing these interceptors do is disable any nested interceptors.
1353 // These functions may open nss modules and call uninstrumented functions from
1354 // them, and we don't want things like strlen() to trigger.
1355 INTERCEPTOR(void, setpwent, int dummy) {
1357 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1358 REAL(setpwent)(dummy);
1360 INTERCEPTOR(void, endpwent, int dummy) {
1362 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1363 REAL(endpwent)(dummy);
1365 INTERCEPTOR(void, setgrent, int dummy) {
1367 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1368 REAL(setgrent)(dummy);
1370 INTERCEPTOR(void, endgrent, int dummy) {
1372 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1373 REAL(endgrent)(dummy);
1375 #define INIT_SETPWENT \
1376 COMMON_INTERCEPT_FUNCTION(setpwent); \
1377 COMMON_INTERCEPT_FUNCTION(endpwent); \
1378 COMMON_INTERCEPT_FUNCTION(setgrent); \
1379 COMMON_INTERCEPT_FUNCTION(endgrent);
1381 #define INIT_SETPWENT
1384 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1385 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1387 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1388 // FIXME: under ASan the call below may write to freed memory and corrupt
1389 // its metadata. See
1390 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1391 int res = REAL(clock_getres)(clk_id, tp);
1393 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1397 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1399 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1400 // FIXME: under ASan the call below may write to freed memory and corrupt
1401 // its metadata. See
1402 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1403 int res = REAL(clock_gettime)(clk_id, tp);
1405 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1409 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1411 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1412 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1413 return REAL(clock_settime)(clk_id, tp);
1415 #define INIT_CLOCK_GETTIME \
1416 COMMON_INTERCEPT_FUNCTION(clock_getres); \
1417 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1418 COMMON_INTERCEPT_FUNCTION(clock_settime);
1420 #define INIT_CLOCK_GETTIME
1423 #if SANITIZER_INTERCEPT_GETITIMER
1424 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1426 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1427 // FIXME: under ASan the call below may write to freed memory and corrupt
1428 // its metadata. See
1429 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1430 int res = REAL(getitimer)(which, curr_value);
1431 if (!res && curr_value) {
1432 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1436 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1438 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1440 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1441 // FIXME: under ASan the call below may write to freed memory and corrupt
1442 // its metadata. See
1443 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1444 int res = REAL(setitimer)(which, new_value, old_value);
1445 if (!res && old_value) {
1446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1450 #define INIT_GETITIMER \
1451 COMMON_INTERCEPT_FUNCTION(getitimer); \
1452 COMMON_INTERCEPT_FUNCTION(setitimer);
1454 #define INIT_GETITIMER
1457 #if SANITIZER_INTERCEPT_GLOB
1458 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1459 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1460 // +1 for NULL pointer at the end.
1461 if (pglob->gl_pathv)
1462 COMMON_INTERCEPTOR_WRITE_RANGE(
1463 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1464 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1465 char *p = pglob->gl_pathv[i];
1466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1470 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1472 static void wrapped_gl_closedir(void *dir) {
1473 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1474 pglob_copy->gl_closedir(dir);
1477 static void *wrapped_gl_readdir(void *dir) {
1478 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1479 return pglob_copy->gl_readdir(dir);
1482 static void *wrapped_gl_opendir(const char *s) {
1483 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1484 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1485 return pglob_copy->gl_opendir(s);
1488 static int wrapped_gl_lstat(const char *s, void *st) {
1489 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1490 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1491 return pglob_copy->gl_lstat(s, st);
1494 static int wrapped_gl_stat(const char *s, void *st) {
1495 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1496 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1497 return pglob_copy->gl_stat(s, st);
1500 INTERCEPTOR(int, glob, const char *pattern, int flags,
1501 int (*errfunc)(const char *epath, int eerrno),
1502 __sanitizer_glob_t *pglob) {
1504 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1505 __sanitizer_glob_t glob_copy = {
1507 0, wrapped_gl_closedir, wrapped_gl_readdir,
1508 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1509 if (flags & glob_altdirfunc) {
1510 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1511 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1512 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1513 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1514 Swap(pglob->gl_stat, glob_copy.gl_stat);
1515 pglob_copy = &glob_copy;
1517 int res = REAL(glob)(pattern, flags, errfunc, pglob);
1518 if (flags & glob_altdirfunc) {
1519 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1520 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1521 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1522 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1523 Swap(pglob->gl_stat, glob_copy.gl_stat);
1526 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1530 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1531 int (*errfunc)(const char *epath, int eerrno),
1532 __sanitizer_glob_t *pglob) {
1534 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1535 __sanitizer_glob_t glob_copy = {
1537 0, wrapped_gl_closedir, wrapped_gl_readdir,
1538 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1539 if (flags & glob_altdirfunc) {
1540 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1541 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1542 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1543 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1544 Swap(pglob->gl_stat, glob_copy.gl_stat);
1545 pglob_copy = &glob_copy;
1547 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
1548 if (flags & glob_altdirfunc) {
1549 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1550 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1551 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1552 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1553 Swap(pglob->gl_stat, glob_copy.gl_stat);
1556 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1560 COMMON_INTERCEPT_FUNCTION(glob); \
1561 COMMON_INTERCEPT_FUNCTION(glob64);
1562 #else // SANITIZER_INTERCEPT_GLOB
1564 #endif // SANITIZER_INTERCEPT_GLOB
1566 #if SANITIZER_INTERCEPT_WAIT
1567 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
1568 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
1570 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
1572 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
1573 // FIXME: under ASan the call below may write to freed memory and corrupt
1574 // its metadata. See
1575 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1576 int res = REAL(wait)(status);
1577 if (res != -1 && status)
1578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1581 // On FreeBSD id_t is always 64-bit wide.
1582 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
1583 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
1586 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
1590 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
1591 // FIXME: under ASan the call below may write to freed memory and corrupt
1592 // its metadata. See
1593 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1594 int res = REAL(waitid)(idtype, id, infop, options);
1595 if (res != -1 && infop)
1596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
1599 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
1601 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
1602 // FIXME: under ASan the call below may write to freed memory and corrupt
1603 // its metadata. See
1604 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1605 int res = REAL(waitpid)(pid, status, options);
1606 if (res != -1 && status)
1607 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1610 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
1612 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
1613 // FIXME: under ASan the call below may write to freed memory and corrupt
1614 // its metadata. See
1615 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1616 int res = REAL(wait3)(status, options, rusage);
1618 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1619 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1623 #if SANITIZER_ANDROID
1624 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
1626 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
1627 // FIXME: under ASan the call below may write to freed memory and corrupt
1628 // its metadata. See
1629 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1630 int res = REAL(__wait4)(pid, status, options, rusage);
1632 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1633 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1637 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
1639 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
1641 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
1642 // FIXME: under ASan the call below may write to freed memory and corrupt
1643 // its metadata. See
1644 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1645 int res = REAL(wait4)(pid, status, options, rusage);
1647 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1648 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1652 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
1653 #endif // SANITIZER_ANDROID
1655 COMMON_INTERCEPT_FUNCTION(wait); \
1656 COMMON_INTERCEPT_FUNCTION(waitid); \
1657 COMMON_INTERCEPT_FUNCTION(waitpid); \
1658 COMMON_INTERCEPT_FUNCTION(wait3);
1664 #if SANITIZER_INTERCEPT_INET
1665 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
1667 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
1668 uptr sz = __sanitizer_in_addr_sz(af);
1669 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
1670 // FIXME: figure out read size based on the address family.
1671 // FIXME: under ASan the call below may write to freed memory and corrupt
1672 // its metadata. See
1673 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1674 char *res = REAL(inet_ntop)(af, src, dst, size);
1675 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1678 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
1680 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
1681 // FIXME: figure out read size based on the address family.
1682 // FIXME: under ASan the call below may write to freed memory and corrupt
1683 // its metadata. See
1684 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1685 int res = REAL(inet_pton)(af, src, dst);
1687 uptr sz = __sanitizer_in_addr_sz(af);
1688 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1693 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
1694 COMMON_INTERCEPT_FUNCTION(inet_pton);
1699 #if SANITIZER_INTERCEPT_INET
1700 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
1702 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
1703 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
1704 // FIXME: under ASan the call below may write to freed memory and corrupt
1705 // its metadata. See
1706 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1707 int res = REAL(inet_aton)(cp, dst);
1709 uptr sz = __sanitizer_in_addr_sz(af_inet);
1710 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1714 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
1716 #define INIT_INET_ATON
1719 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
1720 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
1722 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
1723 // FIXME: under ASan the call below may write to freed memory and corrupt
1724 // its metadata. See
1725 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1726 int res = REAL(pthread_getschedparam)(thread, policy, param);
1728 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
1729 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
1733 #define INIT_PTHREAD_GETSCHEDPARAM \
1734 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
1736 #define INIT_PTHREAD_GETSCHEDPARAM
1739 #if SANITIZER_INTERCEPT_GETADDRINFO
1740 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
1741 struct __sanitizer_addrinfo *hints,
1742 struct __sanitizer_addrinfo **out) {
1744 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
1745 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
1747 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
1749 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
1750 // FIXME: under ASan the call below may write to freed memory and corrupt
1751 // its metadata. See
1752 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1753 int res = REAL(getaddrinfo)(node, service, hints, out);
1754 if (res == 0 && out) {
1755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
1756 struct __sanitizer_addrinfo *p = *out;
1758 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
1760 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
1761 if (p->ai_canonname)
1762 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
1763 REAL(strlen)(p->ai_canonname) + 1);
1769 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
1771 #define INIT_GETADDRINFO
1774 #if SANITIZER_INTERCEPT_GETNAMEINFO
1775 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
1776 unsigned hostlen, char *serv, unsigned servlen, int flags) {
1778 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
1779 serv, servlen, flags);
1780 // FIXME: consider adding READ_RANGE(sockaddr, salen)
1781 // There is padding in in_addr that may make this too noisy
1782 // FIXME: under ASan the call below may write to freed memory and corrupt
1783 // its metadata. See
1784 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1786 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
1788 if (host && hostlen)
1789 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
1790 if (serv && servlen)
1791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
1795 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
1797 #define INIT_GETNAMEINFO
1800 #if SANITIZER_INTERCEPT_GETSOCKNAME
1801 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
1803 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
1804 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1805 int addrlen_in = *addrlen;
1806 // FIXME: under ASan the call below may write to freed memory and corrupt
1807 // its metadata. See
1808 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1809 int res = REAL(getsockname)(sock_fd, addr, addrlen);
1811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
1815 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
1817 #define INIT_GETSOCKNAME
1820 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1821 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
1822 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
1824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
1825 char **p = h->h_aliases;
1827 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1830 COMMON_INTERCEPTOR_WRITE_RANGE(
1831 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
1834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
1837 COMMON_INTERCEPTOR_WRITE_RANGE(
1838 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
1842 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
1843 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
1845 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
1846 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
1847 if (res) write_hostent(ctx, res);
1851 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
1854 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
1855 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1856 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
1857 if (res) write_hostent(ctx, res);
1861 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
1863 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
1864 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
1865 if (res) write_hostent(ctx, res);
1869 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
1871 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
1872 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
1873 if (res) write_hostent(ctx, res);
1876 #define INIT_GETHOSTBYNAME \
1877 COMMON_INTERCEPT_FUNCTION(gethostent); \
1878 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
1879 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
1880 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
1882 #define INIT_GETHOSTBYNAME
1885 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1886 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
1887 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
1890 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
1892 // FIXME: under ASan the call below may write to freed memory and corrupt
1893 // its metadata. See
1894 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1895 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
1897 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1898 if (res == 0 && *result) write_hostent(ctx, *result);
1901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1904 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
1906 #define INIT_GETHOSTBYNAME_R
1909 #if SANITIZER_INTERCEPT_GETHOSTENT_R
1910 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
1911 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
1913 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
1915 // FIXME: under ASan the call below may write to freed memory and corrupt
1916 // its metadata. See
1917 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1918 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
1920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1921 if (res == 0 && *result) write_hostent(ctx, *result);
1924 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1927 #define INIT_GETHOSTENT_R \
1928 COMMON_INTERCEPT_FUNCTION(gethostent_r);
1930 #define INIT_GETHOSTENT_R
1933 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
1934 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
1935 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1936 __sanitizer_hostent **result, int *h_errnop) {
1938 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
1939 buflen, result, h_errnop);
1940 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1941 // FIXME: under ASan the call below may write to freed memory and corrupt
1942 // its metadata. See
1943 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1944 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
1947 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1948 if (res == 0 && *result) write_hostent(ctx, *result);
1951 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1954 #define INIT_GETHOSTBYADDR_R \
1955 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
1957 #define INIT_GETHOSTBYADDR_R
1960 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
1961 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
1962 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1963 __sanitizer_hostent **result, int *h_errnop) {
1965 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
1967 // FIXME: under ASan the call below may write to freed memory and corrupt
1968 // its metadata. See
1969 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1971 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
1973 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1974 if (res == 0 && *result) write_hostent(ctx, *result);
1977 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1980 #define INIT_GETHOSTBYNAME2_R \
1981 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
1983 #define INIT_GETHOSTBYNAME2_R
1986 #if SANITIZER_INTERCEPT_GETSOCKOPT
1987 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
1990 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
1992 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
1993 // FIXME: under ASan the call below may write to freed memory and corrupt
1994 // its metadata. See
1995 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
1996 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
1998 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2001 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2003 #define INIT_GETSOCKOPT
2006 #if SANITIZER_INTERCEPT_ACCEPT
2007 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2009 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2010 unsigned addrlen0 = 0;
2012 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2013 addrlen0 = *addrlen;
2015 int fd2 = REAL(accept)(fd, addr, addrlen);
2017 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2018 if (addr && addrlen)
2019 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2023 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2028 #if SANITIZER_INTERCEPT_ACCEPT4
2029 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2031 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2032 unsigned addrlen0 = 0;
2034 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2035 addrlen0 = *addrlen;
2037 // FIXME: under ASan the call below may write to freed memory and corrupt
2038 // its metadata. See
2039 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2040 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2042 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2043 if (addr && addrlen)
2044 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2048 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2050 #define INIT_ACCEPT4
2053 #if SANITIZER_INTERCEPT_MODF
2054 INTERCEPTOR(double, modf, double x, double *iptr) {
2056 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2057 // FIXME: under ASan the call below may write to freed memory and corrupt
2058 // its metadata. See
2059 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2060 double res = REAL(modf)(x, iptr);
2062 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2066 INTERCEPTOR(float, modff, float x, float *iptr) {
2068 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2069 // FIXME: under ASan the call below may write to freed memory and corrupt
2070 // its metadata. See
2071 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2072 float res = REAL(modff)(x, iptr);
2074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2078 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2080 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2081 // FIXME: under ASan the call below may write to freed memory and corrupt
2082 // its metadata. See
2083 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2084 long double res = REAL(modfl)(x, iptr);
2086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2091 COMMON_INTERCEPT_FUNCTION(modf); \
2092 COMMON_INTERCEPT_FUNCTION(modff); \
2093 COMMON_INTERCEPT_FUNCTION(modfl);
2098 #if SANITIZER_INTERCEPT_RECVMSG
2099 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2101 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2102 if (msg->msg_name && msg->msg_namelen)
2103 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2104 if (msg->msg_iov && msg->msg_iovlen)
2105 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2106 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2107 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2108 if (msg->msg_control && msg->msg_controllen)
2109 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2112 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2115 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2116 // FIXME: under ASan the call below may write to freed memory and corrupt
2117 // its metadata. See
2118 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2119 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2121 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2123 write_msghdr(ctx, msg, res);
2124 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2129 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2131 #define INIT_RECVMSG
2134 #if SANITIZER_INTERCEPT_GETPEERNAME
2135 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2137 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2139 if (addrlen) addr_sz = *addrlen;
2140 // FIXME: under ASan the call below may write to freed memory and corrupt
2141 // its metadata. See
2142 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2143 int res = REAL(getpeername)(sockfd, addr, addrlen);
2144 if (!res && addr && addrlen)
2145 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2148 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2150 #define INIT_GETPEERNAME
2153 #if SANITIZER_INTERCEPT_SYSINFO
2154 INTERCEPTOR(int, sysinfo, void *info) {
2156 // FIXME: under ASan the call below may write to freed memory and corrupt
2157 // its metadata. See
2158 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2159 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2160 int res = REAL(sysinfo)(info);
2162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2165 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2167 #define INIT_SYSINFO
2170 #if SANITIZER_INTERCEPT_READDIR
2171 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2173 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2174 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2175 __sanitizer_dirent *res = REAL(opendir)(path);
2177 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2181 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2183 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2184 // FIXME: under ASan the call below may write to freed memory and corrupt
2185 // its metadata. See
2186 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2187 __sanitizer_dirent *res = REAL(readdir)(dirp);
2188 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2192 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2193 __sanitizer_dirent **result) {
2195 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2196 // FIXME: under ASan the call below may write to freed memory and corrupt
2197 // its metadata. See
2198 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2199 int res = REAL(readdir_r)(dirp, entry, result);
2201 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2208 #define INIT_READDIR \
2209 COMMON_INTERCEPT_FUNCTION(opendir); \
2210 COMMON_INTERCEPT_FUNCTION(readdir); \
2211 COMMON_INTERCEPT_FUNCTION(readdir_r);
2213 #define INIT_READDIR
2216 #if SANITIZER_INTERCEPT_READDIR64
2217 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2219 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2220 // FIXME: under ASan the call below may write to freed memory and corrupt
2221 // its metadata. See
2222 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2223 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2224 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2228 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2229 __sanitizer_dirent64 **result) {
2231 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2232 // FIXME: under ASan the call below may write to freed memory and corrupt
2233 // its metadata. See
2234 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2235 int res = REAL(readdir64_r)(dirp, entry, result);
2237 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2239 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2243 #define INIT_READDIR64 \
2244 COMMON_INTERCEPT_FUNCTION(readdir64); \
2245 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2247 #define INIT_READDIR64
2250 #if SANITIZER_INTERCEPT_PTRACE
2251 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2253 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2256 if (request == ptrace_setregs)
2257 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2258 else if (request == ptrace_setfpregs)
2259 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2260 else if (request == ptrace_setfpxregs)
2261 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2262 else if (request == ptrace_setsiginfo)
2263 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2264 else if (request == ptrace_setregset) {
2265 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
2266 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov->iov_base, iov->iov_len);
2270 // FIXME: under ASan the call below may write to freed memory and corrupt
2271 // its metadata. See
2272 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2273 uptr res = REAL(ptrace)(request, pid, addr, data);
2276 // Note that PEEK* requests assign different meaning to the return value.
2277 // This function does not handle them (nor does it need to).
2278 if (request == ptrace_getregs)
2279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2280 else if (request == ptrace_getfpregs)
2281 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2282 else if (request == ptrace_getfpxregs)
2283 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2284 else if (request == ptrace_getsiginfo)
2285 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2286 else if (request == ptrace_geteventmsg)
2287 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2288 else if (request == ptrace_getregset) {
2289 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
2290 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iov->iov_base, iov->iov_len);
2296 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2301 #if SANITIZER_INTERCEPT_SETLOCALE
2302 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2304 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2306 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2307 char *res = REAL(setlocale)(category, locale);
2308 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2312 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2314 #define INIT_SETLOCALE
2317 #if SANITIZER_INTERCEPT_GETCWD
2318 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2320 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
2321 // FIXME: under ASan the call below may write to freed memory and corrupt
2322 // its metadata. See
2323 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2324 char *res = REAL(getcwd)(buf, size);
2325 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2328 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2333 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2334 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2336 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2337 // FIXME: under ASan the call below may write to freed memory and corrupt
2338 // its metadata. See
2339 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2340 char *res = REAL(get_current_dir_name)(fake);
2341 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2345 #define INIT_GET_CURRENT_DIR_NAME \
2346 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2348 #define INIT_GET_CURRENT_DIR_NAME
2351 #if SANITIZER_INTERCEPT_STRTOIMAX
2352 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
2354 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
2355 // FIXME: under ASan the call below may write to freed memory and corrupt
2356 // its metadata. See
2357 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2358 INTMAX_T res = REAL(strtoimax)(nptr, endptr, base);
2359 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2363 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
2365 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
2366 // FIXME: under ASan the call below may write to freed memory and corrupt
2367 // its metadata. See
2368 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2369 INTMAX_T res = REAL(strtoumax)(nptr, endptr, base);
2370 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2374 #define INIT_STRTOIMAX \
2375 COMMON_INTERCEPT_FUNCTION(strtoimax); \
2376 COMMON_INTERCEPT_FUNCTION(strtoumax);
2378 #define INIT_STRTOIMAX
2381 #if SANITIZER_INTERCEPT_MBSTOWCS
2382 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
2384 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
2385 // FIXME: under ASan the call below may write to freed memory and corrupt
2386 // its metadata. See
2387 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2388 SIZE_T res = REAL(mbstowcs)(dest, src, len);
2389 if (res != (SIZE_T) - 1 && dest) {
2390 SIZE_T write_cnt = res + (res < len);
2391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2396 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
2399 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
2400 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2401 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2402 // FIXME: under ASan the call below may write to freed memory and corrupt
2403 // its metadata. See
2404 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2405 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
2406 if (res != (SIZE_T)(-1) && dest && src) {
2407 // This function, and several others, may or may not write the terminating
2408 // \0 character. They write it iff they clear *src.
2409 SIZE_T write_cnt = res + !*src;
2410 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2415 #define INIT_MBSTOWCS \
2416 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
2417 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
2419 #define INIT_MBSTOWCS
2422 #if SANITIZER_INTERCEPT_MBSNRTOWCS
2423 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
2424 SIZE_T len, void *ps) {
2426 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
2428 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2429 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2431 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2432 // FIXME: under ASan the call below may write to freed memory and corrupt
2433 // its metadata. See
2434 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2435 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
2436 if (res != (SIZE_T)(-1) && dest && src) {
2437 SIZE_T write_cnt = res + !*src;
2438 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2443 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
2445 #define INIT_MBSNRTOWCS
2448 #if SANITIZER_INTERCEPT_WCSTOMBS
2449 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
2451 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
2452 // FIXME: under ASan the call below may write to freed memory and corrupt
2453 // its metadata. See
2454 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2455 SIZE_T res = REAL(wcstombs)(dest, src, len);
2456 if (res != (SIZE_T) - 1 && dest) {
2457 SIZE_T write_cnt = res + (res < len);
2458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2463 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
2466 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
2467 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2468 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2469 // FIXME: under ASan the call below may write to freed memory and corrupt
2470 // its metadata. See
2471 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2472 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
2473 if (res != (SIZE_T) - 1 && dest && src) {
2474 SIZE_T write_cnt = res + !*src;
2475 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2480 #define INIT_WCSTOMBS \
2481 COMMON_INTERCEPT_FUNCTION(wcstombs); \
2482 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
2484 #define INIT_WCSTOMBS
2487 #if SANITIZER_INTERCEPT_WCSNRTOMBS
2488 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
2489 SIZE_T len, void *ps) {
2491 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
2493 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2494 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2496 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2497 // FIXME: under ASan the call below may write to freed memory and corrupt
2498 // its metadata. See
2499 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2500 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
2501 if (res != (SIZE_T) - 1 && dest && src) {
2502 SIZE_T write_cnt = res + !*src;
2503 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
2508 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
2510 #define INIT_WCSNRTOMBS
2513 #if SANITIZER_INTERCEPT_TCGETATTR
2514 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
2516 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
2517 // FIXME: under ASan the call below may write to freed memory and corrupt
2518 // its metadata. See
2519 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2520 int res = REAL(tcgetattr)(fd, termios_p);
2521 if (!res && termios_p)
2522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
2526 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
2528 #define INIT_TCGETATTR
2531 #if SANITIZER_INTERCEPT_REALPATH
2532 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
2534 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
2535 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2537 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
2538 // version of a versioned symbol. For realpath(), this gives us something
2539 // (called __old_realpath) that does not handle NULL in the second argument.
2540 // Handle it as part of the interceptor.
2541 char *allocated_path = 0;
2543 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
2545 char *res = REAL(realpath)(path, resolved_path);
2546 if (allocated_path && !res) WRAP(free)(allocated_path);
2547 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2550 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
2552 #define INIT_REALPATH
2555 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
2556 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
2558 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
2559 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2560 char *res = REAL(canonicalize_file_name)(path);
2561 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2564 #define INIT_CANONICALIZE_FILE_NAME \
2565 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
2567 #define INIT_CANONICALIZE_FILE_NAME
2570 #if SANITIZER_INTERCEPT_CONFSTR
2571 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
2573 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
2574 // FIXME: under ASan the call below may write to freed memory and corrupt
2575 // its metadata. See
2576 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2577 SIZE_T res = REAL(confstr)(name, buf, len);
2579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
2582 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
2584 #define INIT_CONFSTR
2587 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
2588 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
2590 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
2591 // FIXME: under ASan the call below may write to freed memory and corrupt
2592 // its metadata. See
2593 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2594 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
2595 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
2598 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
2600 #define INIT_SCHED_GETAFFINITY
2603 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
2604 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
2606 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
2607 int res = REAL(sched_getparam)(pid, param);
2608 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
2611 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
2613 #define INIT_SCHED_GETPARAM
2616 #if SANITIZER_INTERCEPT_STRERROR
2617 INTERCEPTOR(char *, strerror, int errnum) {
2619 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
2620 char *res = REAL(strerror)(errnum);
2621 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
2624 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
2626 #define INIT_STRERROR
2629 #if SANITIZER_INTERCEPT_STRERROR_R
2630 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
2632 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
2633 // FIXME: under ASan the call below may write to freed memory and corrupt
2634 // its metadata. See
2635 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2636 char *res = REAL(strerror_r)(errnum, buf, buflen);
2637 // There are 2 versions of strerror_r:
2638 // * POSIX version returns 0 on success, negative error code on failure,
2639 // writes message to buf.
2640 // * GNU version returns message pointer, which points to either buf or some
2642 SIZE_T posix_res = (SIZE_T)res;
2643 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
2644 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
2645 // At least on OSX, buf contents are valid even when the call fails.
2646 SIZE_T sz = internal_strnlen(buf, buflen);
2647 if (sz < buflen) ++sz;
2648 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
2651 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2655 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
2657 #define INIT_STRERROR_R
2660 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
2661 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
2663 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
2664 // FIXME: under ASan the call below may write to freed memory and corrupt
2665 // its metadata. See
2666 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2667 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
2668 // This version always returns a null-terminated string.
2670 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2673 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
2675 #define INIT_XPG_STRERROR_R
2678 #if SANITIZER_INTERCEPT_SCANDIR
2679 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
2680 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
2681 const struct __sanitizer_dirent **);
2683 static THREADLOCAL scandir_filter_f scandir_filter;
2684 static THREADLOCAL scandir_compar_f scandir_compar;
2686 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
2687 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2688 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
2689 return scandir_filter(dir);
2692 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
2693 const struct __sanitizer_dirent **b) {
2694 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2695 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
2696 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
2697 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
2698 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
2699 return scandir_compar(a, b);
2702 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
2703 scandir_filter_f filter, scandir_compar_f compar) {
2705 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
2706 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
2707 scandir_filter = filter;
2708 scandir_compar = compar;
2709 // FIXME: under ASan the call below may write to freed memory and corrupt
2710 // its metadata. See
2711 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2712 int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
2713 compar ? wrapped_scandir_compar : 0);
2716 if (namelist && res > 0) {
2717 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
2718 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
2719 for (int i = 0; i < res; ++i)
2720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
2721 (*namelist)[i]->d_reclen);
2725 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
2727 #define INIT_SCANDIR
2730 #if SANITIZER_INTERCEPT_SCANDIR64
2731 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
2732 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
2733 const struct __sanitizer_dirent64 **);
2735 static THREADLOCAL scandir64_filter_f scandir64_filter;
2736 static THREADLOCAL scandir64_compar_f scandir64_compar;
2738 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
2739 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2740 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
2741 return scandir64_filter(dir);
2744 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
2745 const struct __sanitizer_dirent64 **b) {
2746 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2747 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
2748 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
2749 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
2750 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
2751 return scandir64_compar(a, b);
2754 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
2755 scandir64_filter_f filter, scandir64_compar_f compar) {
2757 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
2758 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
2759 scandir64_filter = filter;
2760 scandir64_compar = compar;
2761 // FIXME: under ASan the call below may write to freed memory and corrupt
2762 // its metadata. See
2763 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2765 REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
2766 compar ? wrapped_scandir64_compar : 0);
2767 scandir64_filter = 0;
2768 scandir64_compar = 0;
2769 if (namelist && res > 0) {
2770 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
2771 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
2772 for (int i = 0; i < res; ++i)
2773 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
2774 (*namelist)[i]->d_reclen);
2778 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
2780 #define INIT_SCANDIR64
2783 #if SANITIZER_INTERCEPT_GETGROUPS
2784 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
2786 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
2787 // FIXME: under ASan the call below may write to freed memory and corrupt
2788 // its metadata. See
2789 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2790 int res = REAL(getgroups)(size, lst);
2791 if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
2794 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
2796 #define INIT_GETGROUPS
2799 #if SANITIZER_INTERCEPT_POLL
2800 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
2801 __sanitizer_nfds_t nfds) {
2802 for (unsigned i = 0; i < nfds; ++i) {
2803 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
2804 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
2808 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
2809 __sanitizer_nfds_t nfds) {
2810 for (unsigned i = 0; i < nfds; ++i)
2811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
2812 sizeof(fds[i].revents));
2815 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2818 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
2819 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2820 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
2821 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2824 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
2829 #if SANITIZER_INTERCEPT_PPOLL
2830 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2831 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
2833 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
2834 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2836 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
2837 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
2839 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
2840 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2843 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
2848 #if SANITIZER_INTERCEPT_WORDEXP
2849 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
2851 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
2852 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
2853 // FIXME: under ASan the call below may write to freed memory and corrupt
2854 // its metadata. See
2855 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2856 int res = REAL(wordexp)(s, p, flags);
2858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
2861 sizeof(*p->we_wordv) * p->we_wordc);
2862 for (uptr i = 0; i < p->we_wordc; ++i) {
2863 char *w = p->we_wordv[i];
2864 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
2869 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
2871 #define INIT_WORDEXP
2874 #if SANITIZER_INTERCEPT_SIGWAIT
2875 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
2877 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
2878 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2879 // FIXME: under ASan the call below may write to freed memory and corrupt
2880 // its metadata. See
2881 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2882 int res = REAL(sigwait)(set, sig);
2883 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
2886 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
2888 #define INIT_SIGWAIT
2891 #if SANITIZER_INTERCEPT_SIGWAITINFO
2892 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
2894 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
2895 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2896 // FIXME: under ASan the call below may write to freed memory and corrupt
2897 // its metadata. See
2898 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2899 int res = REAL(sigwaitinfo)(set, info);
2900 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2903 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
2905 #define INIT_SIGWAITINFO
2908 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
2909 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
2912 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
2913 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2914 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2915 // FIXME: under ASan the call below may write to freed memory and corrupt
2916 // its metadata. See
2917 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2918 int res = REAL(sigtimedwait)(set, info, timeout);
2919 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2922 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
2924 #define INIT_SIGTIMEDWAIT
2927 #if SANITIZER_INTERCEPT_SIGSETOPS
2928 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
2930 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
2931 // FIXME: under ASan the call below may write to freed memory and corrupt
2932 // its metadata. See
2933 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2934 int res = REAL(sigemptyset)(set);
2935 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2939 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
2941 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
2942 // FIXME: under ASan the call below may write to freed memory and corrupt
2943 // its metadata. See
2944 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2945 int res = REAL(sigfillset)(set);
2946 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2949 #define INIT_SIGSETOPS \
2950 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
2951 COMMON_INTERCEPT_FUNCTION(sigfillset);
2953 #define INIT_SIGSETOPS
2956 #if SANITIZER_INTERCEPT_SIGPENDING
2957 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
2959 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
2960 // FIXME: under ASan the call below may write to freed memory and corrupt
2961 // its metadata. See
2962 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2963 int res = REAL(sigpending)(set);
2964 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2967 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
2969 #define INIT_SIGPENDING
2972 #if SANITIZER_INTERCEPT_SIGPROCMASK
2973 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
2974 __sanitizer_sigset_t *oldset) {
2976 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
2977 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2978 // FIXME: under ASan the call below may write to freed memory and corrupt
2979 // its metadata. See
2980 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2981 int res = REAL(sigprocmask)(how, set, oldset);
2983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
2986 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
2988 #define INIT_SIGPROCMASK
2991 #if SANITIZER_INTERCEPT_BACKTRACE
2992 INTERCEPTOR(int, backtrace, void **buffer, int size) {
2994 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
2995 // FIXME: under ASan the call below may write to freed memory and corrupt
2996 // its metadata. See
2997 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
2998 int res = REAL(backtrace)(buffer, size);
3000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3004 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3006 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3008 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3009 // FIXME: under ASan the call below may write to freed memory and corrupt
3010 // its metadata. See
3011 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3012 char **res = REAL(backtrace_symbols)(buffer, size);
3014 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3015 for (int i = 0; i < size; ++i)
3016 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3020 #define INIT_BACKTRACE \
3021 COMMON_INTERCEPT_FUNCTION(backtrace); \
3022 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3024 #define INIT_BACKTRACE
3027 #if SANITIZER_INTERCEPT__EXIT
3028 INTERCEPTOR(void, _exit, int status) {
3030 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3031 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3032 if (status == 0) status = status1;
3033 REAL(_exit)(status);
3035 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3040 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3041 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3043 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3044 int res = REAL(pthread_mutex_lock)(m);
3045 if (res == errno_EOWNERDEAD)
3046 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3047 if (res == 0 || res == errno_EOWNERDEAD)
3048 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
3052 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3054 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3055 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3056 return REAL(pthread_mutex_unlock)(m);
3059 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3060 #define INIT_PTHREAD_MUTEX_UNLOCK \
3061 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3063 #define INIT_PTHREAD_MUTEX_LOCK
3064 #define INIT_PTHREAD_MUTEX_UNLOCK
3067 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3068 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3069 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3070 if (mnt->mnt_fsname)
3071 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3072 REAL(strlen)(mnt->mnt_fsname) + 1);
3074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3075 REAL(strlen)(mnt->mnt_dir) + 1);
3077 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3078 REAL(strlen)(mnt->mnt_type) + 1);
3080 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3081 REAL(strlen)(mnt->mnt_opts) + 1);
3085 #if SANITIZER_INTERCEPT_GETMNTENT
3086 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3088 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3089 __sanitizer_mntent *res = REAL(getmntent)(fp);
3090 if (res) write_mntent(ctx, res);
3093 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3095 #define INIT_GETMNTENT
3098 #if SANITIZER_INTERCEPT_GETMNTENT_R
3099 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3100 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3102 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3103 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3104 if (res) write_mntent(ctx, res);
3107 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3109 #define INIT_GETMNTENT_R
3112 #if SANITIZER_INTERCEPT_STATFS
3113 INTERCEPTOR(int, statfs, char *path, void *buf) {
3115 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3116 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3117 // FIXME: under ASan the call below may write to freed memory and corrupt
3118 // its metadata. See
3119 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3120 int res = REAL(statfs)(path, buf);
3121 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3124 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3126 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3127 // FIXME: under ASan the call below may write to freed memory and corrupt
3128 // its metadata. See
3129 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3130 int res = REAL(fstatfs)(fd, buf);
3131 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3134 #define INIT_STATFS \
3135 COMMON_INTERCEPT_FUNCTION(statfs); \
3136 COMMON_INTERCEPT_FUNCTION(fstatfs);
3141 #if SANITIZER_INTERCEPT_STATFS64
3142 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3144 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3145 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3146 // FIXME: under ASan the call below may write to freed memory and corrupt
3147 // its metadata. See
3148 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3149 int res = REAL(statfs64)(path, buf);
3150 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3153 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3155 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3156 // FIXME: under ASan the call below may write to freed memory and corrupt
3157 // its metadata. See
3158 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3159 int res = REAL(fstatfs64)(fd, buf);
3160 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3163 #define INIT_STATFS64 \
3164 COMMON_INTERCEPT_FUNCTION(statfs64); \
3165 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3167 #define INIT_STATFS64
3170 #if SANITIZER_INTERCEPT_STATVFS
3171 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3173 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3174 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3175 // FIXME: under ASan the call below may write to freed memory and corrupt
3176 // its metadata. See
3177 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3178 int res = REAL(statvfs)(path, buf);
3179 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3182 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3184 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3185 // FIXME: under ASan the call below may write to freed memory and corrupt
3186 // its metadata. See
3187 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3188 int res = REAL(fstatvfs)(fd, buf);
3189 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3192 #define INIT_STATVFS \
3193 COMMON_INTERCEPT_FUNCTION(statvfs); \
3194 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3196 #define INIT_STATVFS
3199 #if SANITIZER_INTERCEPT_STATVFS64
3200 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3202 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3203 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3204 // FIXME: under ASan the call below may write to freed memory and corrupt
3205 // its metadata. See
3206 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3207 int res = REAL(statvfs64)(path, buf);
3208 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3211 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3213 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3214 // FIXME: under ASan the call below may write to freed memory and corrupt
3215 // its metadata. See
3216 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3217 int res = REAL(fstatvfs64)(fd, buf);
3218 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3221 #define INIT_STATVFS64 \
3222 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3223 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3225 #define INIT_STATVFS64
3228 #if SANITIZER_INTERCEPT_INITGROUPS
3229 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3231 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3232 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3233 int res = REAL(initgroups)(user, group);
3236 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3238 #define INIT_INITGROUPS
3241 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3242 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3244 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3245 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3246 char *res = REAL(ether_ntoa)(addr);
3247 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3250 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3252 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3253 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3254 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3255 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3258 #define INIT_ETHER_NTOA_ATON \
3259 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3260 COMMON_INTERCEPT_FUNCTION(ether_aton);
3262 #define INIT_ETHER_NTOA_ATON
3265 #if SANITIZER_INTERCEPT_ETHER_HOST
3266 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3268 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3269 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3270 // FIXME: under ASan the call below may write to freed memory and corrupt
3271 // its metadata. See
3272 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3273 int res = REAL(ether_ntohost)(hostname, addr);
3274 if (!res && hostname)
3275 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3278 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3280 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3282 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3283 // FIXME: under ASan the call below may write to freed memory and corrupt
3284 // its metadata. See
3285 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3286 int res = REAL(ether_hostton)(hostname, addr);
3287 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3290 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3293 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3294 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3295 // FIXME: under ASan the call below may write to freed memory and corrupt
3296 // its metadata. See
3297 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3298 int res = REAL(ether_line)(line, addr, hostname);
3300 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3306 #define INIT_ETHER_HOST \
3307 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3308 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3309 COMMON_INTERCEPT_FUNCTION(ether_line);
3311 #define INIT_ETHER_HOST
3314 #if SANITIZER_INTERCEPT_ETHER_R
3315 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3317 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3318 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
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 char *res = REAL(ether_ntoa_r)(addr, buf);
3323 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3326 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3327 __sanitizer_ether_addr *addr) {
3329 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3330 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3331 // FIXME: under ASan the call below may write to freed memory and corrupt
3332 // its metadata. See
3333 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3334 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3335 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3338 #define INIT_ETHER_R \
3339 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3340 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3342 #define INIT_ETHER_R
3345 #if SANITIZER_INTERCEPT_SHMCTL
3346 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3348 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3349 // FIXME: under ASan the call below may write to freed memory and corrupt
3350 // its metadata. See
3351 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3352 int res = REAL(shmctl)(shmid, cmd, buf);
3355 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3356 sz = sizeof(__sanitizer_shmid_ds);
3357 else if (cmd == shmctl_ipc_info)
3358 sz = struct_shminfo_sz;
3359 else if (cmd == shmctl_shm_info)
3360 sz = struct_shm_info_sz;
3361 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3365 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3370 #if SANITIZER_INTERCEPT_RANDOM_R
3371 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
3373 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
3374 // FIXME: under ASan the call below may write to freed memory and corrupt
3375 // its metadata. See
3376 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3377 int res = REAL(random_r)(buf, result);
3379 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3382 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3384 #define INIT_RANDOM_R
3387 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
3388 // its metadata. See
3389 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3390 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
3391 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
3392 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
3393 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
3394 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
3395 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
3396 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
3397 INTERCEPTOR(int, fn, void *attr, void *r) { \
3399 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
3400 int res = REAL(fn)(attr, r); \
3401 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
3404 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
3405 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
3406 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
3407 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
3408 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
3409 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
3410 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
3411 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
3412 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
3413 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
3416 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
3417 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
3418 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
3419 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
3420 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
3421 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
3422 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
3423 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
3425 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
3426 // FIXME: under ASan the call below may write to freed memory and corrupt
3427 // its metadata. See
3428 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3429 int res = REAL(pthread_attr_getstack)(attr, addr, size);
3431 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3432 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
3437 // We may need to call the real pthread_attr_getstack from the run-time
3438 // in sanitizer_common, but we don't want to include the interception headers
3439 // there. So, just define this function here.
3440 namespace __sanitizer {
3442 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
3443 return REAL(pthread_attr_getstack)(attr, addr, size);
3446 } // namespace __sanitizer
3448 #define INIT_PTHREAD_ATTR_GET \
3449 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
3450 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
3451 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
3452 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
3453 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
3454 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
3455 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
3457 #define INIT_PTHREAD_ATTR_GET
3460 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
3461 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
3463 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
3464 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
3466 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
3469 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
3470 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
3473 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
3475 // FIXME: under ASan the call below may write to freed memory and corrupt
3476 // its metadata. See
3477 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3478 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
3479 if (!res && cpusetsize && cpuset)
3480 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
3484 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
3485 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
3487 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
3490 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
3491 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
3492 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
3493 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
3495 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
3498 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
3499 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
3500 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
3501 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
3503 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
3506 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
3507 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
3508 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
3509 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
3511 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
3514 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
3515 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
3516 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
3517 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
3519 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
3522 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
3523 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
3524 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
3525 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
3527 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
3530 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
3531 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
3532 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
3533 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
3535 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
3538 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
3539 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
3540 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
3541 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
3543 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
3546 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
3547 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
3548 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
3549 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
3551 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
3554 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
3555 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
3556 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
3557 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
3559 #define INIT_PTHREAD_CONDATTR_GETPSHARED
3562 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
3563 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
3564 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
3565 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
3567 #define INIT_PTHREAD_CONDATTR_GETCLOCK
3570 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
3571 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
3572 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
3573 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
3575 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
3578 #if SANITIZER_INTERCEPT_TMPNAM
3579 INTERCEPTOR(char *, tmpnam, char *s) {
3581 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
3582 char *res = REAL(tmpnam)(s);
3585 // FIXME: under ASan the call below may write to freed memory and corrupt
3586 // its metadata. See
3587 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3588 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
3590 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3594 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
3599 #if SANITIZER_INTERCEPT_TMPNAM_R
3600 INTERCEPTOR(char *, tmpnam_r, char *s) {
3602 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
3603 // FIXME: under ASan the call below may write to freed memory and corrupt
3604 // its metadata. See
3605 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3606 char *res = REAL(tmpnam_r)(s);
3607 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
3610 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
3612 #define INIT_TMPNAM_R
3615 #if SANITIZER_INTERCEPT_TEMPNAM
3616 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
3618 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
3619 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
3620 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
3621 char *res = REAL(tempnam)(dir, pfx);
3622 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3625 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
3627 #define INIT_TEMPNAM
3630 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
3631 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
3633 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
3634 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
3635 return REAL(pthread_setname_np)(thread, name);
3637 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
3639 #define INIT_PTHREAD_SETNAME_NP
3642 #if SANITIZER_INTERCEPT_SINCOS
3643 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
3645 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
3646 // FIXME: under ASan the call below may write to freed memory and corrupt
3647 // its metadata. See
3648 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3649 REAL(sincos)(x, sin, cos);
3650 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
3651 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
3653 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
3655 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
3656 // FIXME: under ASan the call below may write to freed memory and corrupt
3657 // its metadata. See
3658 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3659 REAL(sincosf)(x, sin, cos);
3660 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
3661 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
3663 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
3665 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
3666 // FIXME: under ASan the call below may write to freed memory and corrupt
3667 // its metadata. See
3668 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3669 REAL(sincosl)(x, sin, cos);
3670 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
3671 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
3673 #define INIT_SINCOS \
3674 COMMON_INTERCEPT_FUNCTION(sincos); \
3675 COMMON_INTERCEPT_FUNCTION(sincosf); \
3676 COMMON_INTERCEPT_FUNCTION(sincosl);
3681 #if SANITIZER_INTERCEPT_REMQUO
3682 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
3684 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
3685 // FIXME: under ASan the call below may write to freed memory and corrupt
3686 // its metadata. See
3687 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3688 double res = REAL(remquo)(x, y, quo);
3689 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
3692 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
3694 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
3695 // FIXME: under ASan the call below may write to freed memory and corrupt
3696 // its metadata. See
3697 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3698 float res = REAL(remquof)(x, y, quo);
3699 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
3702 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
3704 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
3705 // FIXME: under ASan the call below may write to freed memory and corrupt
3706 // its metadata. See
3707 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3708 long double res = REAL(remquol)(x, y, quo);
3709 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
3712 #define INIT_REMQUO \
3713 COMMON_INTERCEPT_FUNCTION(remquo); \
3714 COMMON_INTERCEPT_FUNCTION(remquof); \
3715 COMMON_INTERCEPT_FUNCTION(remquol);
3720 #if SANITIZER_INTERCEPT_LGAMMA
3722 INTERCEPTOR(double, lgamma, double x) {
3724 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
3725 double res = REAL(lgamma)(x);
3726 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3729 INTERCEPTOR(float, lgammaf, float x) {
3731 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
3732 float res = REAL(lgammaf)(x);
3733 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3736 INTERCEPTOR(long double, lgammal, long double x) {
3738 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
3739 long double res = REAL(lgammal)(x);
3740 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
3743 #define INIT_LGAMMA \
3744 COMMON_INTERCEPT_FUNCTION(lgamma); \
3745 COMMON_INTERCEPT_FUNCTION(lgammaf); \
3746 COMMON_INTERCEPT_FUNCTION(lgammal);
3751 #if SANITIZER_INTERCEPT_LGAMMA_R
3752 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
3754 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
3755 // FIXME: under ASan the call below may write to freed memory and corrupt
3756 // its metadata. See
3757 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3758 double res = REAL(lgamma_r)(x, signp);
3759 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3762 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
3764 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
3765 // FIXME: under ASan the call below may write to freed memory and corrupt
3766 // its metadata. See
3767 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3768 float res = REAL(lgammaf_r)(x, signp);
3769 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3772 #define INIT_LGAMMA_R \
3773 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
3774 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
3776 #define INIT_LGAMMA_R
3779 #if SANITIZER_INTERCEPT_LGAMMAL_R
3780 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
3782 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
3783 // FIXME: under ASan the call below may write to freed memory and corrupt
3784 // its metadata. See
3785 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3786 long double res = REAL(lgammal_r)(x, signp);
3787 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3790 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION(lgammal_r);
3792 #define INIT_LGAMMAL_R
3795 #if SANITIZER_INTERCEPT_DRAND48_R
3796 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
3798 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
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 int res = REAL(drand48_r)(buffer, result);
3803 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3806 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
3808 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
3809 // FIXME: under ASan the call below may write to freed memory and corrupt
3810 // its metadata. See
3811 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3812 int res = REAL(lrand48_r)(buffer, result);
3813 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3816 #define INIT_DRAND48_R \
3817 COMMON_INTERCEPT_FUNCTION(drand48_r); \
3818 COMMON_INTERCEPT_FUNCTION(lrand48_r);
3820 #define INIT_DRAND48_R
3823 #if SANITIZER_INTERCEPT_RAND_R
3824 INTERCEPTOR(int, rand_r, unsigned *seedp) {
3826 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
3827 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
3828 return REAL(rand_r)(seedp);
3830 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
3835 #if SANITIZER_INTERCEPT_GETLINE
3836 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
3838 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
3839 // FIXME: under ASan the call below may write to freed memory and corrupt
3840 // its metadata. See
3841 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3842 SSIZE_T res = REAL(getline)(lineptr, n, stream);
3844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
3845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
3846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
3850 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
3853 COMMON_INTERCEPTOR_ENTER(ctx, __getdelim, lineptr, n, delim, stream);
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 SSIZE_T res = REAL(__getdelim)(lineptr, n, delim, stream);
3859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
3860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
3861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
3865 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
3867 return __getdelim(lineptr, n, delim, stream);
3869 #define INIT_GETLINE \
3870 COMMON_INTERCEPT_FUNCTION(getline); \
3871 COMMON_INTERCEPT_FUNCTION(__getdelim); \
3872 COMMON_INTERCEPT_FUNCTION(getdelim);
3874 #define INIT_GETLINE
3877 #if SANITIZER_INTERCEPT_ICONV
3878 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
3879 char **outbuf, SIZE_T *outbytesleft) {
3881 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
3884 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
3885 if (inbuf && inbytesleft)
3886 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
3888 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
3889 void *outbuf_orig = outbuf ? *outbuf : 0;
3890 // FIXME: under ASan the call below may write to freed memory and corrupt
3891 // its metadata. See
3892 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3893 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
3894 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
3895 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
3896 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
3900 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
3905 #if SANITIZER_INTERCEPT_TIMES
3906 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
3908 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
3909 // FIXME: under ASan the call below may write to freed memory and corrupt
3910 // its metadata. See
3911 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3912 __sanitizer_clock_t res = REAL(times)(tms);
3913 if (res != (__sanitizer_clock_t)-1 && tms)
3914 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
3917 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
3922 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
3923 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
3924 // If you see any crashes around this functions, there are 2 known issues with
3925 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
3926 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
3927 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
3928 // to access thread local variables (it should not happen normally,
3929 // because sanitizers use initial-exec tls model).
3930 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
3932 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
3933 void *res = REAL(__tls_get_addr)(arg);
3934 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res);
3936 // New DTLS block has been allocated.
3937 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
3942 #define INIT_TLS_GET_ADDR
3945 #if SANITIZER_INTERCEPT_LISTXATTR
3946 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
3948 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
3949 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3950 // FIXME: under ASan the call below may write to freed memory and corrupt
3951 // its metadata. See
3952 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3953 SSIZE_T res = REAL(listxattr)(path, list, size);
3954 // Here and below, size == 0 is a special case where nothing is written to the
3955 // buffer, and res contains the desired buffer size.
3956 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3959 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
3961 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
3962 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3963 // FIXME: under ASan the call below may write to freed memory and corrupt
3964 // its metadata. See
3965 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3966 SSIZE_T res = REAL(llistxattr)(path, list, size);
3967 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3970 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
3972 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
3973 // FIXME: under ASan the call below may write to freed memory and corrupt
3974 // its metadata. See
3975 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3976 SSIZE_T res = REAL(flistxattr)(fd, list, size);
3977 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3980 #define INIT_LISTXATTR \
3981 COMMON_INTERCEPT_FUNCTION(listxattr); \
3982 COMMON_INTERCEPT_FUNCTION(llistxattr); \
3983 COMMON_INTERCEPT_FUNCTION(flistxattr);
3985 #define INIT_LISTXATTR
3988 #if SANITIZER_INTERCEPT_GETXATTR
3989 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
3992 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
3993 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3994 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3995 // FIXME: under ASan the call below may write to freed memory and corrupt
3996 // its metadata. See
3997 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
3998 SSIZE_T res = REAL(getxattr)(path, name, value, size);
3999 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4002 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4005 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4006 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4007 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4008 // FIXME: under ASan the call below may write to freed memory and corrupt
4009 // its metadata. See
4010 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4011 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4012 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4015 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4018 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4019 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4020 // FIXME: under ASan the call below may write to freed memory and corrupt
4021 // its metadata. See
4022 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4023 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4024 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4027 #define INIT_GETXATTR \
4028 COMMON_INTERCEPT_FUNCTION(getxattr); \
4029 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4030 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4032 #define INIT_GETXATTR
4035 #if SANITIZER_INTERCEPT_GETRESID
4036 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4038 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4039 // FIXME: under ASan the call below may write to freed memory and corrupt
4040 // its metadata. See
4041 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4042 int res = REAL(getresuid)(ruid, euid, suid);
4044 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4045 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4046 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4050 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4052 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
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 int res = REAL(getresgid)(rgid, egid, sgid);
4058 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4059 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4060 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4064 #define INIT_GETRESID \
4065 COMMON_INTERCEPT_FUNCTION(getresuid); \
4066 COMMON_INTERCEPT_FUNCTION(getresgid);
4068 #define INIT_GETRESID
4071 #if SANITIZER_INTERCEPT_GETIFADDRS
4072 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4073 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4074 // intercept it to poison the memory again.
4075 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4077 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4078 // FIXME: under ASan the call below may write to freed memory and corrupt
4079 // its metadata. See
4080 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4081 int res = REAL(getifaddrs)(ifap);
4082 if (res == 0 && ifap) {
4083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4084 __sanitizer_ifaddrs *p = *ifap;
4086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4088 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4089 REAL(strlen)(p->ifa_name) + 1);
4091 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4093 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4094 // On Linux this is a union, but the other member also points to a
4095 // struct sockaddr, so the following is sufficient.
4097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4098 // FIXME(smatveev): Unpoison p->ifa_data as well.
4104 #define INIT_GETIFADDRS \
4105 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4107 #define INIT_GETIFADDRS
4110 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4111 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4113 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4114 // FIXME: under ASan the call below may write to freed memory and corrupt
4115 // its metadata. See
4116 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4117 char *res = REAL(if_indextoname)(ifindex, ifname);
4119 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4122 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4124 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4126 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4127 return REAL(if_nametoindex)(ifname);
4129 #define INIT_IF_INDEXTONAME \
4130 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4131 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4133 #define INIT_IF_INDEXTONAME
4136 #if SANITIZER_INTERCEPT_CAPGET
4137 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4139 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4141 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4142 // FIXME: under ASan the call below may write to freed memory and corrupt
4143 // its metadata. See
4144 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4145 int res = REAL(capget)(hdrp, datap);
4146 if (res == 0 && datap)
4147 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4148 // We can also return -1 and write to hdrp->version if the version passed in
4149 // hdrp->version is unsupported. But that's not a trivial condition to check,
4150 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4153 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4155 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4157 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4159 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4160 return REAL(capset)(hdrp, datap);
4162 #define INIT_CAPGET \
4163 COMMON_INTERCEPT_FUNCTION(capget); \
4164 COMMON_INTERCEPT_FUNCTION(capset);
4169 #if SANITIZER_INTERCEPT_AEABI_MEM
4170 DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr);
4171 DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr);
4172 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4174 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4175 return WRAP(memmove)(to, from, size);
4177 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4178 return WRAP(memmove)(to, from, size);
4180 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4181 return WRAP(memmove)(to, from, size);
4183 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4184 return WRAP(memcpy)(to, from, size);
4186 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4187 return WRAP(memcpy)(to, from, size);
4189 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4190 return WRAP(memcpy)(to, from, size);
4192 // Note the argument order.
4193 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4194 return WRAP(memset)(block, c, size);
4196 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4197 return WRAP(memset)(block, c, size);
4199 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4200 return WRAP(memset)(block, c, size);
4202 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4203 return WRAP(memset)(block, 0, size);
4205 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4206 return WRAP(memset)(block, 0, size);
4208 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4209 return WRAP(memset)(block, 0, size);
4211 #define INIT_AEABI_MEM \
4212 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
4213 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4214 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4215 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
4216 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
4217 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
4218 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
4219 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
4220 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
4221 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
4222 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
4223 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4225 #define INIT_AEABI_MEM
4226 #endif // SANITIZER_INTERCEPT_AEABI_MEM
4228 #if SANITIZER_INTERCEPT___BZERO
4229 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4231 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4232 return WRAP(memset)(block, 0, size);
4234 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4236 #define INIT___BZERO
4237 #endif // SANITIZER_INTERCEPT___BZERO
4239 #if SANITIZER_INTERCEPT_FTIME
4240 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4242 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
4243 // FIXME: under ASan the call below may write to freed memory and corrupt
4244 // its metadata. See
4245 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4246 int res = REAL(ftime)(tp);
4248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4251 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4254 #endif // SANITIZER_INTERCEPT_FTIME
4256 #if SANITIZER_INTERCEPT_XDR
4257 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4258 unsigned size, int op) {
4260 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
4261 // FIXME: under ASan the call below may write to freed memory and corrupt
4262 // its metadata. See
4263 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4264 REAL(xdrmem_create)(xdrs, addr, size, op);
4265 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4266 if (op == __sanitizer_XDR_ENCODE) {
4267 // It's not obvious how much data individual xdr_ routines write.
4268 // Simply unpoison the entire target buffer in advance.
4269 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
4273 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
4275 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
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 REAL(xdrstdio_create)(xdrs, file, op);
4280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4283 // FIXME: under ASan the call below may write to freed memory and corrupt
4284 // its metadata. See
4285 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4286 #define XDR_INTERCEPTOR(F, T) \
4287 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
4289 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
4290 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
4291 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
4292 int res = REAL(F)(xdrs, p); \
4293 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
4294 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
4298 XDR_INTERCEPTOR(xdr_short, short)
4299 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
4300 XDR_INTERCEPTOR(xdr_int, int)
4301 XDR_INTERCEPTOR(xdr_u_int, unsigned)
4302 XDR_INTERCEPTOR(xdr_long, long)
4303 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
4304 XDR_INTERCEPTOR(xdr_hyper, long long)
4305 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
4306 XDR_INTERCEPTOR(xdr_longlong_t, long long)
4307 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
4308 XDR_INTERCEPTOR(xdr_int8_t, u8)
4309 XDR_INTERCEPTOR(xdr_uint8_t, u8)
4310 XDR_INTERCEPTOR(xdr_int16_t, u16)
4311 XDR_INTERCEPTOR(xdr_uint16_t, u16)
4312 XDR_INTERCEPTOR(xdr_int32_t, u32)
4313 XDR_INTERCEPTOR(xdr_uint32_t, u32)
4314 XDR_INTERCEPTOR(xdr_int64_t, u64)
4315 XDR_INTERCEPTOR(xdr_uint64_t, u64)
4316 XDR_INTERCEPTOR(xdr_quad_t, long long)
4317 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
4318 XDR_INTERCEPTOR(xdr_bool, bool)
4319 XDR_INTERCEPTOR(xdr_enum, int)
4320 XDR_INTERCEPTOR(xdr_char, char)
4321 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
4322 XDR_INTERCEPTOR(xdr_float, float)
4323 XDR_INTERCEPTOR(xdr_double, double)
4325 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
4326 // wrapstring, sizeof
4328 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
4331 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
4332 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
4333 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
4334 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
4335 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
4337 // FIXME: under ASan the call below may write to freed memory and corrupt
4338 // its metadata. See
4339 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4340 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
4341 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
4342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4343 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
4344 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
4349 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
4352 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
4353 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
4354 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
4355 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
4357 // FIXME: under ASan the call below may write to freed memory and corrupt
4358 // its metadata. See
4359 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4360 int res = REAL(xdr_string)(xdrs, p, maxsize);
4361 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
4362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4364 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
4370 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
4371 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
4372 COMMON_INTERCEPT_FUNCTION(xdr_short); \
4373 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
4374 COMMON_INTERCEPT_FUNCTION(xdr_int); \
4375 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
4376 COMMON_INTERCEPT_FUNCTION(xdr_long); \
4377 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
4378 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
4379 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
4380 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
4381 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
4382 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
4383 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
4384 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
4385 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
4386 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
4387 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
4388 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
4389 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
4390 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
4391 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
4392 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
4393 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
4394 COMMON_INTERCEPT_FUNCTION(xdr_char); \
4395 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
4396 COMMON_INTERCEPT_FUNCTION(xdr_float); \
4397 COMMON_INTERCEPT_FUNCTION(xdr_double); \
4398 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
4399 COMMON_INTERCEPT_FUNCTION(xdr_string);
4402 #endif // SANITIZER_INTERCEPT_XDR
4404 #if SANITIZER_INTERCEPT_TSEARCH
4405 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
4406 int (*compar)(const void *, const void *)) {
4408 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
4409 // FIXME: under ASan the call below may write to freed memory and corrupt
4410 // its metadata. See
4411 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4412 void *res = REAL(tsearch)(key, rootp, compar);
4413 if (res && *(void **)res == key)
4414 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
4417 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
4419 #define INIT_TSEARCH
4422 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
4423 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
4424 void unpoison_file(__sanitizer_FILE *fp) {
4425 #if SANITIZER_HAS_STRUCT_FILE
4426 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
4427 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
4428 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
4429 fp->_IO_read_end - fp->_IO_read_base);
4430 #endif // SANITIZER_HAS_STRUCT_FILE
4434 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
4435 // These guys are called when a .c source is built with -O2.
4436 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
4438 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
4439 int res = REAL(__uflow)(fp);
4443 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
4445 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
4446 int res = REAL(__underflow)(fp);
4450 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
4452 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
4453 int res = REAL(__overflow)(fp, ch);
4457 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
4459 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
4460 int res = REAL(__wuflow)(fp);
4464 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
4466 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
4467 int res = REAL(__wunderflow)(fp);
4471 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
4473 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
4474 int res = REAL(__woverflow)(fp, ch);
4478 #define INIT_LIBIO_INTERNALS \
4479 COMMON_INTERCEPT_FUNCTION(__uflow); \
4480 COMMON_INTERCEPT_FUNCTION(__underflow); \
4481 COMMON_INTERCEPT_FUNCTION(__overflow); \
4482 COMMON_INTERCEPT_FUNCTION(__wuflow); \
4483 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
4484 COMMON_INTERCEPT_FUNCTION(__woverflow);
4486 #define INIT_LIBIO_INTERNALS
4489 #if SANITIZER_INTERCEPT_FOPEN
4490 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
4492 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
4493 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4494 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4495 __sanitizer_FILE *res = REAL(fopen)(path, mode);
4496 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4497 if (res) unpoison_file(res);
4500 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
4502 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
4503 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4504 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
4505 if (res) unpoison_file(res);
4508 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
4509 __sanitizer_FILE *fp) {
4511 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
4512 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4513 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4514 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4515 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
4516 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4517 if (res) unpoison_file(res);
4520 #define INIT_FOPEN \
4521 COMMON_INTERCEPT_FUNCTION(fopen); \
4522 COMMON_INTERCEPT_FUNCTION(fdopen); \
4523 COMMON_INTERCEPT_FUNCTION(freopen);
4528 #if SANITIZER_INTERCEPT_FOPEN64
4529 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
4531 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
4532 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4533 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4534 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
4535 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4536 if (res) unpoison_file(res);
4539 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
4540 __sanitizer_FILE *fp) {
4542 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
4543 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4544 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
4545 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4546 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
4547 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
4548 if (res) unpoison_file(res);
4551 #define INIT_FOPEN64 \
4552 COMMON_INTERCEPT_FUNCTION(fopen64); \
4553 COMMON_INTERCEPT_FUNCTION(freopen64);
4555 #define INIT_FOPEN64
4558 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
4559 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
4561 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
4562 // FIXME: under ASan the call below may write to freed memory and corrupt
4563 // its metadata. See
4564 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4565 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
4567 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
4568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
4570 FileMetadata file = {ptr, sizeloc};
4571 SetInterceptorMetadata(res, file);
4575 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
4578 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
4579 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
4581 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
4582 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
4584 FileMetadata file = {(char **)ptr, sizeloc};
4585 SetInterceptorMetadata(res, file);
4589 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
4592 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
4593 // FIXME: under ASan the call below may write to freed memory and corrupt
4594 // its metadata. See
4595 // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
4596 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
4597 if (res) unpoison_file(res);
4600 #define INIT_OPEN_MEMSTREAM \
4601 COMMON_INTERCEPT_FUNCTION(open_memstream); \
4602 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
4603 COMMON_INTERCEPT_FUNCTION(fmemopen);
4605 #define INIT_OPEN_MEMSTREAM
4608 #if SANITIZER_INTERCEPT_OBSTACK
4609 static void initialize_obstack(__sanitizer_obstack *obstack) {
4610 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
4612 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
4613 sizeof(*obstack->chunk));
4616 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
4617 int align, void *(*alloc_fn)(uptr arg, uptr sz),
4618 void (*free_fn)(uptr arg, void *p)) {
4620 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
4622 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
4623 if (res) initialize_obstack(obstack);
4626 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
4627 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
4629 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
4631 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
4632 if (res) initialize_obstack(obstack);
4635 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
4637 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
4638 REAL(_obstack_newchunk)(obstack, length);
4640 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
4641 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
4643 #define INIT_OBSTACK \
4644 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
4645 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
4646 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
4648 #define INIT_OBSTACK
4651 #if SANITIZER_INTERCEPT_FFLUSH
4652 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
4654 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
4655 int res = REAL(fflush)(fp);
4656 // FIXME: handle fp == NULL
4658 const FileMetadata *m = GetInterceptorMetadata(fp);
4659 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
4663 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
4668 #if SANITIZER_INTERCEPT_FCLOSE
4669 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
4671 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
4673 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
4674 const FileMetadata *m = GetInterceptorMetadata(fp);
4676 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
4677 DeleteInterceptorMetadata(fp);
4680 return REAL(fclose)(fp);
4682 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
4687 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
4688 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
4690 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
4691 void *res = REAL(dlopen)(filename, flag);
4692 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
4696 INTERCEPTOR(int, dlclose, void *handle) {
4698 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
4699 int res = REAL(dlclose)(handle);
4700 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
4703 #define INIT_DLOPEN_DLCLOSE \
4704 COMMON_INTERCEPT_FUNCTION(dlopen); \
4705 COMMON_INTERCEPT_FUNCTION(dlclose);
4707 #define INIT_DLOPEN_DLCLOSE
4710 #if SANITIZER_INTERCEPT_GETPASS
4711 INTERCEPTOR(char *, getpass, const char *prompt) {
4713 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
4715 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
4716 char *res = REAL(getpass)(prompt);
4717 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
4721 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
4723 #define INIT_GETPASS
4726 #if SANITIZER_INTERCEPT_TIMERFD
4727 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
4730 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
4732 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
4733 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
4734 if (res != -1 && old_value)
4735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
4739 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
4741 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
4742 int res = REAL(timerfd_gettime)(fd, curr_value);
4743 if (res != -1 && curr_value)
4744 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
4747 #define INIT_TIMERFD \
4748 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
4749 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
4751 #define INIT_TIMERFD
4754 #if SANITIZER_INTERCEPT_MLOCKX
4755 // Linux kernel has a bug that leads to kernel deadlock if a process
4756 // maps TBs of memory and then calls mlock().
4757 static void MlockIsUnsupported() {
4758 static atomic_uint8_t printed;
4759 if (atomic_exchange(&printed, 1, memory_order_relaxed))
4761 VPrintf(1, "INFO: %s ignores mlock/mlockall/munlock/munlockall\n",
4765 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
4766 MlockIsUnsupported();
4770 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
4771 MlockIsUnsupported();
4775 INTERCEPTOR(int, mlockall, int flags) {
4776 MlockIsUnsupported();
4780 INTERCEPTOR(int, munlockall, void) {
4781 MlockIsUnsupported();
4785 #define INIT_MLOCKX \
4786 COMMON_INTERCEPT_FUNCTION(mlock); \
4787 COMMON_INTERCEPT_FUNCTION(munlock); \
4788 COMMON_INTERCEPT_FUNCTION(mlockall); \
4789 COMMON_INTERCEPT_FUNCTION(munlockall);
4793 #endif // SANITIZER_INTERCEPT_MLOCKX
4795 static void InitializeCommonInterceptors() {
4796 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
4797 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
4819 INIT_LOCALTIME_AND_FRIENDS;
4828 INIT_GETPWNAM_AND_FRIENDS;
4829 INIT_GETPWNAM_R_AND_FRIENDS;
4841 INIT_PTHREAD_GETSCHEDPARAM;
4846 INIT_GETHOSTBYNAME_R;
4847 INIT_GETHOSTBYNAME2_R;
4848 INIT_GETHOSTBYADDR_R;
4864 INIT_GET_CURRENT_DIR_NAME;
4872 INIT_CANONICALIZE_FILE_NAME;
4874 INIT_SCHED_GETAFFINITY;
4875 INIT_SCHED_GETPARAM;
4878 INIT_XPG_STRERROR_R;
4893 INIT_PTHREAD_MUTEX_LOCK;
4894 INIT_PTHREAD_MUTEX_UNLOCK;
4902 INIT_ETHER_NTOA_ATON;
4907 INIT_PTHREAD_ATTR_GET;
4908 INIT_PTHREAD_ATTR_GETINHERITSCHED;
4909 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
4910 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
4911 INIT_PTHREAD_MUTEXATTR_GETTYPE;
4912 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
4913 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
4914 INIT_PTHREAD_MUTEXATTR_GETROBUST;
4915 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
4916 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
4917 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
4918 INIT_PTHREAD_CONDATTR_GETPSHARED;
4919 INIT_PTHREAD_CONDATTR_GETCLOCK;
4920 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
4924 INIT_PTHREAD_SETNAME_NP;
4940 INIT_IF_INDEXTONAME;
4947 INIT_LIBIO_INTERNALS;
4950 INIT_OPEN_MEMSTREAM;
4954 INIT_DLOPEN_DLCLOSE;