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_READ_RANGE
17 // COMMON_INTERCEPTOR_WRITE_RANGE
18 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
19 // COMMON_INTERCEPTOR_FD_ACQUIRE
20 // COMMON_INTERCEPTOR_FD_RELEASE
21 // COMMON_INTERCEPTOR_FD_ACCESS
22 // COMMON_INTERCEPTOR_SET_THREAD_NAME
23 // COMMON_INTERCEPTOR_ON_EXIT
24 // COMMON_INTERCEPTOR_MUTEX_LOCK
25 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
26 // COMMON_INTERCEPTOR_MUTEX_REPAIR
27 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
28 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
29 //===----------------------------------------------------------------------===//
30 #include "interception/interception.h"
31 #include "sanitizer_platform_interceptors.h"
32 #include "sanitizer_tls_get_addr.h"
36 #if SANITIZER_WINDOWS && !defined(va_copy)
37 #define va_copy(dst, src) ((dst) = (src))
40 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
41 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, p, size) {}
44 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
45 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
48 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
49 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
52 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
53 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
56 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
57 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
60 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
61 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
64 #if SANITIZER_INTERCEPT_TEXTDOMAIN
65 INTERCEPTOR(char*, textdomain, const char *domainname) {
67 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
68 char* domain = REAL(textdomain)(domainname);
70 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, domain,
71 REAL(strlen)(domain) + 1);
75 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
77 #define INIT_TEXTDOMAIN
80 #if SANITIZER_INTERCEPT_STRCMP
81 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
82 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
85 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
87 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
91 c1 = (unsigned char)s1[i];
92 c2 = (unsigned char)s2[i];
93 if (c1 != c2 || c1 == '\0') break;
95 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
96 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
97 return CharCmpX(c1, c2);
100 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
102 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
103 unsigned char c1 = 0, c2 = 0;
105 for (i = 0; i < size; i++) {
106 c1 = (unsigned char)s1[i];
107 c2 = (unsigned char)s2[i];
108 if (c1 != c2 || c1 == '\0') break;
110 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
111 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
112 return CharCmpX(c1, c2);
115 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
116 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
122 #if SANITIZER_INTERCEPT_STRCASECMP
123 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
124 int c1_low = ToLower(c1);
125 int c2_low = ToLower(c2);
126 return c1_low - c2_low;
129 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
131 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
132 unsigned char c1 = 0, c2 = 0;
135 c1 = (unsigned char)s1[i];
136 c2 = (unsigned char)s2[i];
137 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
139 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
140 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
141 return CharCaseCmp(c1, c2);
144 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
146 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
147 unsigned char c1 = 0, c2 = 0;
149 for (i = 0; i < n; i++) {
150 c1 = (unsigned char)s1[i];
151 c2 = (unsigned char)s2[i];
152 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
154 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
155 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
156 return CharCaseCmp(c1, c2);
159 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
160 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
162 #define INIT_STRCASECMP
163 #define INIT_STRNCASECMP
166 #if SANITIZER_INTERCEPT_FREXP
167 INTERCEPTOR(double, frexp, double x, int *exp) {
169 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
170 double res = REAL(frexp)(x, exp);
171 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
175 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
178 #endif // SANITIZER_INTERCEPT_FREXP
180 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
181 INTERCEPTOR(float, frexpf, float x, int *exp) {
183 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
184 float res = REAL(frexpf)(x, exp);
185 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
189 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
191 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
192 long double res = REAL(frexpl)(x, exp);
193 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
197 #define INIT_FREXPF_FREXPL \
198 COMMON_INTERCEPT_FUNCTION(frexpf); \
199 COMMON_INTERCEPT_FUNCTION(frexpl)
201 #define INIT_FREXPF_FREXPL
202 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
205 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
206 SIZE_T iovlen, SIZE_T maxlen) {
207 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
208 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
209 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
214 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
215 SIZE_T iovlen, SIZE_T maxlen) {
216 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
217 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
218 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
219 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
225 #if SANITIZER_INTERCEPT_READ
226 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
228 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
229 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
230 SSIZE_T res = REAL(read)(fd, ptr, count);
231 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
232 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
235 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
240 #if SANITIZER_INTERCEPT_PREAD
241 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
243 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
244 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
245 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
246 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
247 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
250 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
255 #if SANITIZER_INTERCEPT_PREAD64
256 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
258 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
259 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
260 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
261 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
262 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
265 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
270 #if SANITIZER_INTERCEPT_READV
271 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
274 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
275 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
276 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
277 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
278 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
281 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
286 #if SANITIZER_INTERCEPT_PREADV
287 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
290 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
291 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
292 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
293 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
294 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
297 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
302 #if SANITIZER_INTERCEPT_PREADV64
303 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
306 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
307 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
308 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
309 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
310 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
313 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
315 #define INIT_PREADV64
318 #if SANITIZER_INTERCEPT_WRITE
319 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
321 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
322 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
323 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
324 SSIZE_T res = REAL(write)(fd, ptr, count);
325 // FIXME: this check should be _before_ the call to REAL(write), not after
326 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
329 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
334 #if SANITIZER_INTERCEPT_PWRITE
335 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
337 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
338 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
339 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
340 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
341 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
344 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
349 #if SANITIZER_INTERCEPT_PWRITE64
350 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
353 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
354 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
355 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
356 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
357 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
360 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
362 #define INIT_PWRITE64
365 #if SANITIZER_INTERCEPT_WRITEV
366 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
369 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
370 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
371 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
372 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
373 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
376 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
381 #if SANITIZER_INTERCEPT_PWRITEV
382 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
385 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
386 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
387 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
388 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
389 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
392 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
397 #if SANITIZER_INTERCEPT_PWRITEV64
398 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
401 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
402 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
403 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
404 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
405 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
408 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
410 #define INIT_PWRITEV64
413 #if SANITIZER_INTERCEPT_PRCTL
414 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
415 unsigned long arg3, // NOLINT
416 unsigned long arg4, unsigned long arg5) { // NOLINT
418 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
419 static const int PR_SET_NAME = 15;
420 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
421 if (option == PR_SET_NAME) {
423 internal_strncpy(buff, (char *)arg2, 15);
425 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
429 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
432 #endif // SANITIZER_INTERCEPT_PRCTL
434 #if SANITIZER_INTERCEPT_TIME
435 INTERCEPTOR(unsigned long, time, unsigned long *t) {
437 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
438 unsigned long res = REAL(time)(t);
439 if (t && res != (unsigned long)-1) {
440 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
444 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
447 #endif // SANITIZER_INTERCEPT_TIME
449 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
450 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
453 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
454 // can point to shared memory and tsan would report a data race.
455 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, tm->tm_zone,
456 REAL(strlen(tm->tm_zone)) + 1);
459 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
461 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
462 __sanitizer_tm *res = REAL(localtime)(timep);
464 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
465 unpoison_tm(ctx, res);
469 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
471 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
472 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
474 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
475 unpoison_tm(ctx, res);
479 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
481 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
482 __sanitizer_tm *res = REAL(gmtime)(timep);
484 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
485 unpoison_tm(ctx, res);
489 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
491 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
492 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
494 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
495 unpoison_tm(ctx, res);
499 INTERCEPTOR(char *, ctime, unsigned long *timep) {
501 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
502 char *res = REAL(ctime)(timep);
504 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
505 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
509 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
511 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
512 char *res = REAL(ctime_r)(timep, result);
514 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
515 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
519 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
521 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
522 char *res = REAL(asctime)(tm);
524 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
525 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
529 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
531 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
532 char *res = REAL(asctime_r)(tm, result);
534 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
535 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
539 #define INIT_LOCALTIME_AND_FRIENDS \
540 COMMON_INTERCEPT_FUNCTION(localtime); \
541 COMMON_INTERCEPT_FUNCTION(localtime_r); \
542 COMMON_INTERCEPT_FUNCTION(gmtime); \
543 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
544 COMMON_INTERCEPT_FUNCTION(ctime); \
545 COMMON_INTERCEPT_FUNCTION(ctime_r); \
546 COMMON_INTERCEPT_FUNCTION(asctime); \
547 COMMON_INTERCEPT_FUNCTION(asctime_r);
549 #define INIT_LOCALTIME_AND_FRIENDS
550 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
552 #if SANITIZER_INTERCEPT_STRPTIME
553 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
555 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
557 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
558 char *res = REAL(strptime)(s, format, tm);
560 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, res - s);
561 // Do not call unpoison_tm here, because strptime does not, in fact,
562 // initialize the entire struct tm. For example, tm_zone pointer is left
564 if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
568 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
570 #define INIT_STRPTIME
573 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
574 #include "sanitizer_common_interceptors_format.inc"
576 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
580 va_start(ap, format); \
581 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
582 int res = WRAP(vname)(__VA_ARGS__, ap); \
589 #if SANITIZER_INTERCEPT_SCANF
591 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
594 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
597 int res = REAL(vname)(__VA_ARGS__); \
599 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
604 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
605 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
607 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
608 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
610 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
611 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
613 #if SANITIZER_INTERCEPT_ISOC99_SCANF
614 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
615 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
617 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
619 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
621 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
622 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
623 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
625 INTERCEPTOR(int, scanf, const char *format, ...)
626 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
628 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
629 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
631 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
632 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
634 #if SANITIZER_INTERCEPT_ISOC99_SCANF
635 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
636 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
638 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
639 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
641 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
642 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
647 #if SANITIZER_INTERCEPT_SCANF
649 COMMON_INTERCEPT_FUNCTION(scanf); \
650 COMMON_INTERCEPT_FUNCTION(sscanf); \
651 COMMON_INTERCEPT_FUNCTION(fscanf); \
652 COMMON_INTERCEPT_FUNCTION(vscanf); \
653 COMMON_INTERCEPT_FUNCTION(vsscanf); \
654 COMMON_INTERCEPT_FUNCTION(vfscanf);
659 #if SANITIZER_INTERCEPT_ISOC99_SCANF
660 #define INIT_ISOC99_SCANF \
661 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
662 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
663 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
664 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
665 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
666 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
668 #define INIT_ISOC99_SCANF
671 #if SANITIZER_INTERCEPT_PRINTF
673 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
675 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
679 #define VPRINTF_INTERCEPTOR_RETURN() \
682 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
684 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
685 if (common_flags()->check_printf) \
686 printf_common(ctx, format, aq); \
687 int res = REAL(vname)(__VA_ARGS__); \
688 VPRINTF_INTERCEPTOR_RETURN(); \
692 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
694 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
695 if (common_flags()->check_printf) { \
696 printf_common(ctx, format, aq); \
698 int res = REAL(vname)(str, __VA_ARGS__); \
699 if (res >= 0 && common_flags()->check_printf) { \
700 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
702 VPRINTF_INTERCEPTOR_RETURN(); \
706 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
708 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
709 if (common_flags()->check_printf) { \
710 printf_common(ctx, format, aq); \
712 int res = REAL(vname)(str, size, __VA_ARGS__); \
713 if (res >= 0 && common_flags()->check_printf) { \
714 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
716 VPRINTF_INTERCEPTOR_RETURN(); \
720 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
722 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
723 if (common_flags()->check_printf) { \
724 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
725 printf_common(ctx, format, aq); \
727 int res = REAL(vname)(strp, __VA_ARGS__); \
728 if (res >= 0 && common_flags()->check_printf) { \
729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
731 VPRINTF_INTERCEPTOR_RETURN(); \
735 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
736 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
738 INTERCEPTOR(int, vfprintf, void *stream, const char *format, va_list ap)
739 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
741 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
743 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
745 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
746 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
748 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
749 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
751 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
752 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
753 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
755 INTERCEPTOR(int, __isoc99_vfprintf, void *stream, const char *format,
757 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
759 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
761 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
763 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
765 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
768 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
770 INTERCEPTOR(int, printf, const char *format, ...)
771 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
773 INTERCEPTOR(int, fprintf, void *stream, const char *format, ...)
774 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
776 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
777 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
779 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
780 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
782 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
783 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
785 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
786 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
787 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
789 INTERCEPTOR(int, __isoc99_fprintf, void *stream, const char *format, ...)
790 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
792 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
793 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
795 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
796 const char *format, ...)
797 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
800 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
802 #endif // SANITIZER_INTERCEPT_PRINTF
804 #if SANITIZER_INTERCEPT_PRINTF
805 #define INIT_PRINTF \
806 COMMON_INTERCEPT_FUNCTION(printf); \
807 COMMON_INTERCEPT_FUNCTION(sprintf); \
808 COMMON_INTERCEPT_FUNCTION(snprintf); \
809 COMMON_INTERCEPT_FUNCTION(asprintf); \
810 COMMON_INTERCEPT_FUNCTION(fprintf); \
811 COMMON_INTERCEPT_FUNCTION(vprintf); \
812 COMMON_INTERCEPT_FUNCTION(vsprintf); \
813 COMMON_INTERCEPT_FUNCTION(vsnprintf); \
814 COMMON_INTERCEPT_FUNCTION(vasprintf); \
815 COMMON_INTERCEPT_FUNCTION(vfprintf);
820 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
821 #define INIT_ISOC99_PRINTF \
822 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
823 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
824 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
825 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
826 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
827 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
828 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
829 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
831 #define INIT_ISOC99_PRINTF
834 #if SANITIZER_INTERCEPT_IOCTL
835 #include "sanitizer_common_interceptors_ioctl.inc"
836 INTERCEPTOR(int, ioctl, int d, unsigned request, void *arg) {
838 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
840 CHECK(ioctl_initialized);
842 // Note: TSan does not use common flags, and they are zero-initialized.
843 // This effectively disables ioctl handling in TSan.
844 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
846 const ioctl_desc *desc = ioctl_lookup(request);
847 ioctl_desc decoded_desc;
849 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
850 if (!ioctl_decode(request, &decoded_desc))
851 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
853 desc = &decoded_desc;
856 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
857 int res = REAL(ioctl)(d, request, arg);
858 // FIXME: some ioctls have different return values for success and failure.
859 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
864 COMMON_INTERCEPT_FUNCTION(ioctl);
869 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
870 INTERCEPTOR(void *, getpwnam, const char *name) {
872 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
873 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
874 void *res = REAL(getpwnam)(name);
875 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz);
878 INTERCEPTOR(void *, getpwuid, u32 uid) {
880 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
881 void *res = REAL(getpwuid)(uid);
882 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz);
885 INTERCEPTOR(void *, getgrnam, const char *name) {
887 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
888 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
889 void *res = REAL(getgrnam)(name);
890 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz);
893 INTERCEPTOR(void *, getgrgid, u32 gid) {
895 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
896 void *res = REAL(getgrgid)(gid);
897 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz);
900 #define INIT_GETPWNAM_AND_FRIENDS \
901 COMMON_INTERCEPT_FUNCTION(getpwnam); \
902 COMMON_INTERCEPT_FUNCTION(getpwuid); \
903 COMMON_INTERCEPT_FUNCTION(getgrnam); \
904 COMMON_INTERCEPT_FUNCTION(getgrgid);
906 #define INIT_GETPWNAM_AND_FRIENDS
909 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
910 INTERCEPTOR(int, getpwnam_r, const char *name, void *pwd, char *buf,
911 SIZE_T buflen, void **result) {
913 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
914 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
915 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz);
918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
920 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
923 INTERCEPTOR(int, getpwuid_r, u32 uid, void *pwd, char *buf, SIZE_T buflen,
926 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
927 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz);
930 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
932 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
935 INTERCEPTOR(int, getgrnam_r, const char *name, void *grp, char *buf,
936 SIZE_T buflen, void **result) {
938 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
939 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
940 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
942 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz);
943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
945 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
948 INTERCEPTOR(int, getgrgid_r, u32 gid, void *grp, char *buf, SIZE_T buflen,
951 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
952 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
954 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz);
955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
957 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
960 #define INIT_GETPWNAM_R_AND_FRIENDS \
961 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
962 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
963 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
964 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
966 #define INIT_GETPWNAM_R_AND_FRIENDS
969 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
970 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
972 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
973 int res = REAL(clock_getres)(clk_id, tp);
975 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
979 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
981 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
982 int res = REAL(clock_gettime)(clk_id, tp);
984 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
988 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
990 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
991 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
992 return REAL(clock_settime)(clk_id, tp);
994 #define INIT_CLOCK_GETTIME \
995 COMMON_INTERCEPT_FUNCTION(clock_getres); \
996 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
997 COMMON_INTERCEPT_FUNCTION(clock_settime);
999 #define INIT_CLOCK_GETTIME
1002 #if SANITIZER_INTERCEPT_GETITIMER
1003 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1005 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1006 int res = REAL(getitimer)(which, curr_value);
1007 if (!res && curr_value) {
1008 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1012 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1014 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1016 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1017 int res = REAL(setitimer)(which, new_value, old_value);
1018 if (!res && old_value) {
1019 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1023 #define INIT_GETITIMER \
1024 COMMON_INTERCEPT_FUNCTION(getitimer); \
1025 COMMON_INTERCEPT_FUNCTION(setitimer);
1027 #define INIT_GETITIMER
1030 #if SANITIZER_INTERCEPT_GLOB
1031 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1032 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1033 // +1 for NULL pointer at the end.
1034 if (pglob->gl_pathv)
1035 COMMON_INTERCEPTOR_WRITE_RANGE(
1036 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1037 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1038 char *p = pglob->gl_pathv[i];
1039 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1043 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1044 static THREADLOCAL void *glob_ctx;
1046 static void wrapped_gl_closedir(void *dir) {
1047 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
1048 IndirectExternCall(pglob_copy->gl_closedir)(dir);
1051 static void *wrapped_gl_readdir(void *dir) {
1052 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
1053 return IndirectExternCall(pglob_copy->gl_readdir)(dir);
1056 static void *wrapped_gl_opendir(const char *s) {
1057 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
1058 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
1059 return IndirectExternCall(pglob_copy->gl_opendir)(s);
1062 static int wrapped_gl_lstat(const char *s, void *st) {
1063 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 2);
1064 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
1065 return IndirectExternCall(pglob_copy->gl_lstat)(s, st);
1068 static int wrapped_gl_stat(const char *s, void *st) {
1069 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 2);
1070 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
1071 return IndirectExternCall(pglob_copy->gl_stat)(s, st);
1074 INTERCEPTOR(int, glob, const char *pattern, int flags,
1075 int (*errfunc)(const char *epath, int eerrno),
1076 __sanitizer_glob_t *pglob) {
1078 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1079 __sanitizer_glob_t glob_copy = {
1081 0, wrapped_gl_closedir, wrapped_gl_readdir,
1082 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1083 if (flags & glob_altdirfunc) {
1084 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1085 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1086 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1087 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1088 Swap(pglob->gl_stat, glob_copy.gl_stat);
1089 pglob_copy = &glob_copy;
1092 int res = REAL(glob)(pattern, flags, errfunc, pglob);
1093 if (flags & glob_altdirfunc) {
1094 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1095 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1096 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1097 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1098 Swap(pglob->gl_stat, glob_copy.gl_stat);
1102 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1106 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1107 int (*errfunc)(const char *epath, int eerrno),
1108 __sanitizer_glob_t *pglob) {
1110 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1111 __sanitizer_glob_t glob_copy = {
1113 0, wrapped_gl_closedir, wrapped_gl_readdir,
1114 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1115 if (flags & glob_altdirfunc) {
1116 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1117 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1118 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1119 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1120 Swap(pglob->gl_stat, glob_copy.gl_stat);
1121 pglob_copy = &glob_copy;
1124 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
1125 if (flags & glob_altdirfunc) {
1126 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1127 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1128 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1129 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1130 Swap(pglob->gl_stat, glob_copy.gl_stat);
1134 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1138 COMMON_INTERCEPT_FUNCTION(glob); \
1139 COMMON_INTERCEPT_FUNCTION(glob64);
1140 #else // SANITIZER_INTERCEPT_GLOB
1142 #endif // SANITIZER_INTERCEPT_GLOB
1144 #if SANITIZER_INTERCEPT_WAIT
1145 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
1146 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
1148 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
1150 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
1151 int res = REAL(wait)(status);
1152 if (res != -1 && status)
1153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1156 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
1159 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
1160 int res = REAL(waitid)(idtype, id, infop, options);
1161 if (res != -1 && infop)
1162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
1165 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
1167 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
1168 int res = REAL(waitpid)(pid, status, options);
1169 if (res != -1 && status)
1170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1173 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
1175 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
1176 int res = REAL(wait3)(status, options, rusage);
1178 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1179 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1183 #if SANITIZER_ANDROID
1184 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
1186 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
1187 int res = REAL(__wait4)(pid, status, options, rusage);
1189 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1190 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1194 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
1196 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
1198 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
1199 int res = REAL(wait4)(pid, status, options, rusage);
1201 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1202 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1206 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
1207 #endif // SANITIZER_ANDROID
1209 COMMON_INTERCEPT_FUNCTION(wait); \
1210 COMMON_INTERCEPT_FUNCTION(waitid); \
1211 COMMON_INTERCEPT_FUNCTION(waitpid); \
1212 COMMON_INTERCEPT_FUNCTION(wait3);
1218 #if SANITIZER_INTERCEPT_INET
1219 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
1221 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
1222 uptr sz = __sanitizer_in_addr_sz(af);
1223 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
1224 // FIXME: figure out read size based on the address family.
1225 char *res = REAL(inet_ntop)(af, src, dst, size);
1226 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1229 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
1231 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
1232 // FIXME: figure out read size based on the address family.
1233 int res = REAL(inet_pton)(af, src, dst);
1235 uptr sz = __sanitizer_in_addr_sz(af);
1236 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1241 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
1242 COMMON_INTERCEPT_FUNCTION(inet_pton);
1247 #if SANITIZER_INTERCEPT_INET
1248 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
1250 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
1251 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
1252 int res = REAL(inet_aton)(cp, dst);
1254 uptr sz = __sanitizer_in_addr_sz(af_inet);
1255 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1259 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
1261 #define INIT_INET_ATON
1264 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
1265 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
1267 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
1268 int res = REAL(pthread_getschedparam)(thread, policy, param);
1270 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
1271 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
1275 #define INIT_PTHREAD_GETSCHEDPARAM \
1276 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
1278 #define INIT_PTHREAD_GETSCHEDPARAM
1281 #if SANITIZER_INTERCEPT_GETADDRINFO
1282 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
1283 struct __sanitizer_addrinfo *hints,
1284 struct __sanitizer_addrinfo **out) {
1286 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
1287 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
1289 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
1291 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
1292 int res = REAL(getaddrinfo)(node, service, hints, out);
1293 if (res == 0 && out) {
1294 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
1295 struct __sanitizer_addrinfo *p = *out;
1297 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
1299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
1300 if (p->ai_canonname)
1301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
1302 REAL(strlen)(p->ai_canonname) + 1);
1308 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
1310 #define INIT_GETADDRINFO
1313 #if SANITIZER_INTERCEPT_GETNAMEINFO
1314 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
1315 unsigned hostlen, char *serv, unsigned servlen, int flags) {
1317 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
1318 serv, servlen, flags);
1319 // FIXME: consider adding READ_RANGE(sockaddr, salen)
1320 // There is padding in in_addr that may make this too noisy
1322 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
1324 if (host && hostlen)
1325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
1326 if (serv && servlen)
1327 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
1331 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
1333 #define INIT_GETNAMEINFO
1336 #if SANITIZER_INTERCEPT_GETSOCKNAME
1337 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
1339 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
1340 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1341 int addrlen_in = *addrlen;
1342 int res = REAL(getsockname)(sock_fd, addr, addrlen);
1344 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
1348 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
1350 #define INIT_GETSOCKNAME
1353 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1354 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
1355 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
1357 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
1358 char **p = h->h_aliases;
1360 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1363 COMMON_INTERCEPTOR_WRITE_RANGE(
1364 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
1367 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
1370 COMMON_INTERCEPTOR_WRITE_RANGE(
1371 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
1375 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
1376 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
1378 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
1379 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
1380 if (res) write_hostent(ctx, res);
1384 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
1387 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
1388 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1389 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
1390 if (res) write_hostent(ctx, res);
1394 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
1396 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
1397 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
1398 if (res) write_hostent(ctx, res);
1402 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
1404 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
1405 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
1406 if (res) write_hostent(ctx, res);
1409 #define INIT_GETHOSTBYNAME \
1410 COMMON_INTERCEPT_FUNCTION(gethostent); \
1411 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
1412 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
1413 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
1415 #define INIT_GETHOSTBYNAME
1418 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1419 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
1420 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
1422 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
1424 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
1426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1427 if (res == 0 && *result) write_hostent(ctx, *result);
1430 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1434 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
1435 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1436 __sanitizer_hostent **result, int *h_errnop) {
1438 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
1439 buflen, result, h_errnop);
1440 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1441 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
1444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1445 if (res == 0 && *result) write_hostent(ctx, *result);
1448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1452 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
1453 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
1456 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
1458 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
1460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1461 if (res == 0 && *result) write_hostent(ctx, *result);
1464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1468 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
1469 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1470 __sanitizer_hostent **result, int *h_errnop) {
1472 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
1475 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
1477 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1478 if (res == 0 && *result) write_hostent(ctx, *result);
1481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1484 #define INIT_GETHOSTBYNAME_R \
1485 COMMON_INTERCEPT_FUNCTION(gethostent_r); \
1486 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r); \
1487 COMMON_INTERCEPT_FUNCTION(gethostbyname_r); \
1488 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
1490 #define INIT_GETHOSTBYNAME_R
1493 #if SANITIZER_INTERCEPT_GETSOCKOPT
1494 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
1497 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
1499 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
1500 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
1502 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
1505 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
1507 #define INIT_GETSOCKOPT
1510 #if SANITIZER_INTERCEPT_ACCEPT
1511 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
1513 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
1516 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1517 addrlen0 = *addrlen;
1519 int fd2 = REAL(accept)(fd, addr, addrlen);
1521 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1522 if (addr && addrlen)
1523 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1527 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
1532 #if SANITIZER_INTERCEPT_ACCEPT4
1533 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
1535 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
1538 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1539 addrlen0 = *addrlen;
1541 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
1543 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1544 if (addr && addrlen)
1545 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1549 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
1551 #define INIT_ACCEPT4
1554 #if SANITIZER_INTERCEPT_MODF
1555 INTERCEPTOR(double, modf, double x, double *iptr) {
1557 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
1558 double res = REAL(modf)(x, iptr);
1560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1564 INTERCEPTOR(float, modff, float x, float *iptr) {
1566 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
1567 float res = REAL(modff)(x, iptr);
1569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1573 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
1575 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
1576 long double res = REAL(modfl)(x, iptr);
1578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1583 COMMON_INTERCEPT_FUNCTION(modf); \
1584 COMMON_INTERCEPT_FUNCTION(modff); \
1585 COMMON_INTERCEPT_FUNCTION(modfl);
1590 #if SANITIZER_INTERCEPT_RECVMSG
1591 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
1593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
1594 if (msg->msg_name && msg->msg_namelen)
1595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
1596 if (msg->msg_iov && msg->msg_iovlen)
1597 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
1598 sizeof(*msg->msg_iov) * msg->msg_iovlen);
1599 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
1600 if (msg->msg_control && msg->msg_controllen)
1601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
1604 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
1607 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
1608 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
1610 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1612 write_msghdr(ctx, msg, res);
1613 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
1618 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
1620 #define INIT_RECVMSG
1623 #if SANITIZER_INTERCEPT_GETPEERNAME
1624 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
1626 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
1628 if (addrlen) addr_sz = *addrlen;
1629 int res = REAL(getpeername)(sockfd, addr, addrlen);
1630 if (!res && addr && addrlen)
1631 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
1634 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
1636 #define INIT_GETPEERNAME
1639 #if SANITIZER_INTERCEPT_SYSINFO
1640 INTERCEPTOR(int, sysinfo, void *info) {
1642 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
1643 int res = REAL(sysinfo)(info);
1645 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
1648 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
1650 #define INIT_SYSINFO
1653 #if SANITIZER_INTERCEPT_READDIR
1654 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
1656 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
1657 __sanitizer_dirent *res = REAL(readdir)(dirp);
1658 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
1662 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
1663 __sanitizer_dirent **result) {
1665 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
1666 int res = REAL(readdir_r)(dirp, entry, result);
1668 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1670 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
1675 #define INIT_READDIR \
1676 COMMON_INTERCEPT_FUNCTION(readdir); \
1677 COMMON_INTERCEPT_FUNCTION(readdir_r);
1679 #define INIT_READDIR
1682 #if SANITIZER_INTERCEPT_READDIR64
1683 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
1685 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
1686 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
1687 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
1691 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
1692 __sanitizer_dirent64 **result) {
1694 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
1695 int res = REAL(readdir64_r)(dirp, entry, result);
1697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1699 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
1703 #define INIT_READDIR64 \
1704 COMMON_INTERCEPT_FUNCTION(readdir64); \
1705 COMMON_INTERCEPT_FUNCTION(readdir64_r);
1707 #define INIT_READDIR64
1710 #if SANITIZER_INTERCEPT_PTRACE
1711 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
1713 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
1716 if (request == ptrace_setregs)
1717 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
1718 else if (request == ptrace_setfpregs)
1719 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
1720 else if (request == ptrace_setfpxregs)
1721 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
1722 else if (request == ptrace_setsiginfo)
1723 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
1724 else if (request == ptrace_setregset) {
1725 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
1726 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov->iov_base, iov->iov_len);
1730 uptr res = REAL(ptrace)(request, pid, addr, data);
1733 // Note that PEEK* requests assing different meaning to the return value.
1734 // This function does not handle them (nor does it need to).
1735 if (request == ptrace_getregs)
1736 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
1737 else if (request == ptrace_getfpregs)
1738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
1739 else if (request == ptrace_getfpxregs)
1740 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
1741 else if (request == ptrace_getsiginfo)
1742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
1743 else if (request == ptrace_getregset) {
1744 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
1745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iov->iov_base, iov->iov_len);
1751 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
1756 #if SANITIZER_INTERCEPT_SETLOCALE
1757 INTERCEPTOR(char *, setlocale, int category, char *locale) {
1759 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
1761 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
1762 char *res = REAL(setlocale)(category, locale);
1763 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1767 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
1769 #define INIT_SETLOCALE
1772 #if SANITIZER_INTERCEPT_GETCWD
1773 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
1775 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
1776 char *res = REAL(getcwd)(buf, size);
1777 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1780 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
1785 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
1786 INTERCEPTOR(char *, get_current_dir_name, int fake) {
1788 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
1789 char *res = REAL(get_current_dir_name)(fake);
1790 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1794 #define INIT_GET_CURRENT_DIR_NAME \
1795 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
1797 #define INIT_GET_CURRENT_DIR_NAME
1800 #if SANITIZER_INTERCEPT_STRTOIMAX
1801 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
1803 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
1804 INTMAX_T res = REAL(strtoimax)(nptr, endptr, base);
1805 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
1809 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
1811 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
1812 INTMAX_T res = REAL(strtoumax)(nptr, endptr, base);
1813 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
1817 #define INIT_STRTOIMAX \
1818 COMMON_INTERCEPT_FUNCTION(strtoimax); \
1819 COMMON_INTERCEPT_FUNCTION(strtoumax);
1821 #define INIT_STRTOIMAX
1824 #if SANITIZER_INTERCEPT_MBSTOWCS
1825 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
1827 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
1828 SIZE_T res = REAL(mbstowcs)(dest, src, len);
1829 if (res != (SIZE_T) - 1 && dest) {
1830 SIZE_T write_cnt = res + (res < len);
1831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1836 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
1839 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
1840 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1841 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1842 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
1843 if (res != (SIZE_T)(-1) && dest && src) {
1844 // This function, and several others, may or may not write the terminating
1845 // \0 character. They write it iff they clear *src.
1846 SIZE_T write_cnt = res + !*src;
1847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1852 #define INIT_MBSTOWCS \
1853 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
1854 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
1856 #define INIT_MBSTOWCS
1859 #if SANITIZER_INTERCEPT_MBSNRTOWCS
1860 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
1861 SIZE_T len, void *ps) {
1863 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
1865 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1866 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
1868 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1869 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
1870 if (res != (SIZE_T)(-1) && dest && src) {
1871 SIZE_T write_cnt = res + !*src;
1872 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1877 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
1879 #define INIT_MBSNRTOWCS
1882 #if SANITIZER_INTERCEPT_WCSTOMBS
1883 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
1885 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
1886 SIZE_T res = REAL(wcstombs)(dest, src, len);
1887 if (res != (SIZE_T) - 1 && dest) {
1888 SIZE_T write_cnt = res + (res < len);
1889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1894 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
1897 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
1898 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1899 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1900 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
1901 if (res != (SIZE_T) - 1 && dest && src) {
1902 SIZE_T write_cnt = res + !*src;
1903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1908 #define INIT_WCSTOMBS \
1909 COMMON_INTERCEPT_FUNCTION(wcstombs); \
1910 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
1912 #define INIT_WCSTOMBS
1915 #if SANITIZER_INTERCEPT_WCSNRTOMBS
1916 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
1917 SIZE_T len, void *ps) {
1919 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
1921 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1922 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
1924 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1925 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
1926 if (res != (SIZE_T) - 1 && dest && src) {
1927 SIZE_T write_cnt = res + !*src;
1928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1933 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
1935 #define INIT_WCSNRTOMBS
1938 #if SANITIZER_INTERCEPT_TCGETATTR
1939 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
1941 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
1942 int res = REAL(tcgetattr)(fd, termios_p);
1943 if (!res && termios_p)
1944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
1948 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
1950 #define INIT_TCGETATTR
1953 #if SANITIZER_INTERCEPT_REALPATH
1954 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
1956 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
1957 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
1959 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
1960 // version of a versioned symbol. For realpath(), this gives us something
1961 // (called __old_realpath) that does not handle NULL in the second argument.
1962 // Handle it as part of the interceptor.
1963 char *allocated_path = 0;
1965 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
1967 char *res = REAL(realpath)(path, resolved_path);
1968 if (allocated_path && !res) WRAP(free)(allocated_path);
1969 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1972 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
1974 #define INIT_REALPATH
1977 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
1978 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
1980 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
1981 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
1982 char *res = REAL(canonicalize_file_name)(path);
1983 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1986 #define INIT_CANONICALIZE_FILE_NAME \
1987 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
1989 #define INIT_CANONICALIZE_FILE_NAME
1992 #if SANITIZER_INTERCEPT_CONFSTR
1993 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
1995 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
1996 SIZE_T res = REAL(confstr)(name, buf, len);
1998 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
2001 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
2003 #define INIT_CONFSTR
2006 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
2007 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
2009 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
2010 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
2011 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
2014 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
2016 #define INIT_SCHED_GETAFFINITY
2019 #if SANITIZER_INTERCEPT_STRERROR
2020 INTERCEPTOR(char *, strerror, int errnum) {
2022 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
2023 char *res = REAL(strerror)(errnum);
2024 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2027 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
2029 #define INIT_STRERROR
2032 #if SANITIZER_INTERCEPT_STRERROR_R
2033 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
2035 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
2036 char *res = REAL(strerror_r)(errnum, buf, buflen);
2037 // There are 2 versions of strerror_r:
2038 // * POSIX version returns 0 on success, negative error code on failure,
2039 // writes message to buf.
2040 // * GNU version returns message pointer, which points to either buf or some
2042 SIZE_T posix_res = (SIZE_T)res;
2043 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
2044 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
2045 // At least on OSX, buf contents are valid even when the call fails.
2046 SIZE_T sz = internal_strnlen(buf, buflen);
2047 if (sz < buflen) ++sz;
2048 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
2051 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2055 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
2057 #define INIT_STRERROR_R
2060 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
2061 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
2063 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
2064 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
2065 // This version always returns a null-terminated string.
2067 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2070 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
2072 #define INIT_XPG_STRERROR_R
2075 #if SANITIZER_INTERCEPT_SCANDIR
2076 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
2077 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
2078 const struct __sanitizer_dirent **);
2080 static THREADLOCAL void *scandir_ctx;
2081 static THREADLOCAL scandir_filter_f scandir_filter;
2082 static THREADLOCAL scandir_compar_f scandir_compar;
2084 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
2085 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 1);
2086 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, dir, dir->d_reclen);
2087 return IndirectExternCall(scandir_filter)(dir);
2090 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
2091 const struct __sanitizer_dirent **b) {
2092 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 2);
2093 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, a, sizeof(*a));
2094 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *a, (*a)->d_reclen);
2095 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, b, sizeof(*b));
2096 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *b, (*b)->d_reclen);
2097 return IndirectExternCall(scandir_compar)(a, b);
2100 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
2101 scandir_filter_f filter, scandir_compar_f compar) {
2103 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
2104 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
2105 CHECK_EQ(0, scandir_ctx);
2107 scandir_filter = filter;
2108 scandir_compar = compar;
2109 int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
2110 compar ? wrapped_scandir_compar : 0);
2114 if (namelist && res > 0) {
2115 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
2116 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
2117 for (int i = 0; i < res; ++i)
2118 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
2119 (*namelist)[i]->d_reclen);
2123 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
2125 #define INIT_SCANDIR
2128 #if SANITIZER_INTERCEPT_SCANDIR64
2129 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
2130 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
2131 const struct __sanitizer_dirent64 **);
2133 static THREADLOCAL void *scandir64_ctx;
2134 static THREADLOCAL scandir64_filter_f scandir64_filter;
2135 static THREADLOCAL scandir64_compar_f scandir64_compar;
2137 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
2138 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 1);
2139 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, dir, dir->d_reclen);
2140 return IndirectExternCall(scandir64_filter)(dir);
2143 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
2144 const struct __sanitizer_dirent64 **b) {
2145 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 2);
2146 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, a, sizeof(*a));
2147 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *a, (*a)->d_reclen);
2148 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, b, sizeof(*b));
2149 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *b, (*b)->d_reclen);
2150 return IndirectExternCall(scandir64_compar)(a, b);
2153 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
2154 scandir64_filter_f filter, scandir64_compar_f compar) {
2156 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
2157 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
2158 CHECK_EQ(0, scandir64_ctx);
2159 scandir64_ctx = ctx;
2160 scandir64_filter = filter;
2161 scandir64_compar = compar;
2163 REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
2164 compar ? wrapped_scandir64_compar : 0);
2166 scandir64_filter = 0;
2167 scandir64_compar = 0;
2168 if (namelist && res > 0) {
2169 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
2170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
2171 for (int i = 0; i < res; ++i)
2172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
2173 (*namelist)[i]->d_reclen);
2177 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
2179 #define INIT_SCANDIR64
2182 #if SANITIZER_INTERCEPT_GETGROUPS
2183 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
2185 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
2186 int res = REAL(getgroups)(size, lst);
2187 if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
2190 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
2192 #define INIT_GETGROUPS
2195 #if SANITIZER_INTERCEPT_POLL
2196 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
2197 __sanitizer_nfds_t nfds) {
2198 for (unsigned i = 0; i < nfds; ++i) {
2199 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
2200 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
2204 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
2205 __sanitizer_nfds_t nfds) {
2206 for (unsigned i = 0; i < nfds; ++i)
2207 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
2208 sizeof(fds[i].revents));
2211 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2214 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
2215 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2216 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
2217 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2220 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
2225 #if SANITIZER_INTERCEPT_PPOLL
2226 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2227 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
2229 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
2230 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2232 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
2233 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
2235 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
2236 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2239 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
2244 #if SANITIZER_INTERCEPT_WORDEXP
2245 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
2247 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
2248 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
2249 int res = REAL(wordexp)(s, p, flags);
2251 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2253 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
2254 sizeof(*p->we_wordv) * p->we_wordc);
2255 for (uptr i = 0; i < p->we_wordc; ++i) {
2256 char *w = p->we_wordv[i];
2257 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
2262 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
2264 #define INIT_WORDEXP
2267 #if SANITIZER_INTERCEPT_SIGWAIT
2268 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
2270 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
2271 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2272 int res = REAL(sigwait)(set, sig);
2273 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
2276 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
2278 #define INIT_SIGWAIT
2281 #if SANITIZER_INTERCEPT_SIGWAITINFO
2282 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
2284 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
2285 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2286 int res = REAL(sigwaitinfo)(set, info);
2287 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2290 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
2292 #define INIT_SIGWAITINFO
2295 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
2296 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
2299 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
2300 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2301 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2302 int res = REAL(sigtimedwait)(set, info, timeout);
2303 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2306 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
2308 #define INIT_SIGTIMEDWAIT
2311 #if SANITIZER_INTERCEPT_SIGSETOPS
2312 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
2314 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
2315 int res = REAL(sigemptyset)(set);
2316 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2320 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
2322 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
2323 int res = REAL(sigfillset)(set);
2324 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2327 #define INIT_SIGSETOPS \
2328 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
2329 COMMON_INTERCEPT_FUNCTION(sigfillset);
2331 #define INIT_SIGSETOPS
2334 #if SANITIZER_INTERCEPT_SIGPENDING
2335 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
2337 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
2338 int res = REAL(sigpending)(set);
2339 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2342 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
2344 #define INIT_SIGPENDING
2347 #if SANITIZER_INTERCEPT_SIGPROCMASK
2348 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
2349 __sanitizer_sigset_t *oldset) {
2351 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
2352 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2353 int res = REAL(sigprocmask)(how, set, oldset);
2355 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
2358 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
2360 #define INIT_SIGPROCMASK
2363 #if SANITIZER_INTERCEPT_BACKTRACE
2364 INTERCEPTOR(int, backtrace, void **buffer, int size) {
2366 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
2367 int res = REAL(backtrace)(buffer, size);
2369 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
2373 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
2375 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
2377 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
2378 char **res = REAL(backtrace_symbols)(buffer, size);
2380 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
2381 for (int i = 0; i < size; ++i)
2382 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
2386 #define INIT_BACKTRACE \
2387 COMMON_INTERCEPT_FUNCTION(backtrace); \
2388 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
2390 #define INIT_BACKTRACE
2393 #if SANITIZER_INTERCEPT__EXIT
2394 INTERCEPTOR(void, _exit, int status) {
2396 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
2397 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
2398 if (status == 0) status = status1;
2399 REAL(_exit)(status);
2401 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
2406 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
2407 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
2409 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
2410 int res = REAL(pthread_mutex_lock)(m);
2411 if (res == errno_EOWNERDEAD)
2412 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
2413 if (res == 0 || res == errno_EOWNERDEAD)
2414 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2418 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
2420 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
2421 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2422 return REAL(pthread_mutex_unlock)(m);
2425 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
2426 #define INIT_PTHREAD_MUTEX_UNLOCK \
2427 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
2429 #define INIT_PTHREAD_MUTEX_LOCK
2430 #define INIT_PTHREAD_MUTEX_UNLOCK
2433 #if SANITIZER_INTERCEPT_PTHREAD_COND
2434 INTERCEPTOR(int, pthread_cond_init, void *c, void *a) {
2436 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_init, c, a);
2437 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, c, 1);
2438 return REAL(pthread_cond_init)(c, a);
2441 INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) {
2443 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_wait, c, m);
2444 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2445 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, 1);
2446 int res = REAL(pthread_cond_wait)(c, m);
2447 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2451 INTERCEPTOR(int, pthread_cond_timedwait, void *c, void *m, void *abstime) {
2453 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_timedwait, c, m, abstime);
2454 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2455 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, 1);
2456 int res = REAL(pthread_cond_timedwait)(c, m, abstime);
2457 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2461 INTERCEPTOR(int, pthread_cond_signal, void *c) {
2463 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_signal, c);
2464 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, 1);
2465 return REAL(pthread_cond_signal)(c);
2468 INTERCEPTOR(int, pthread_cond_broadcast, void *c) {
2470 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_broadcast, c);
2471 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, 1);
2472 return REAL(pthread_cond_broadcast)(c);
2475 INTERCEPTOR(int, pthread_cond_destroy, void *c) {
2477 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_destroy, c);
2478 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, c, 1);
2479 return REAL(pthread_cond_destroy)(c);
2482 #define INIT_PTHREAD_COND \
2483 INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2"); \
2484 INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2"); \
2485 INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2"); \
2486 INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2"); \
2487 INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2"); \
2488 INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2")
2490 #define INIT_PTHREAD_COND
2493 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
2494 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
2495 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
2496 if (mnt->mnt_fsname)
2497 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
2498 REAL(strlen)(mnt->mnt_fsname) + 1);
2500 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
2501 REAL(strlen)(mnt->mnt_dir) + 1);
2503 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
2504 REAL(strlen)(mnt->mnt_type) + 1);
2506 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
2507 REAL(strlen)(mnt->mnt_opts) + 1);
2511 #if SANITIZER_INTERCEPT_GETMNTENT
2512 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
2514 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
2515 __sanitizer_mntent *res = REAL(getmntent)(fp);
2516 if (res) write_mntent(ctx, res);
2519 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
2521 #define INIT_GETMNTENT
2524 #if SANITIZER_INTERCEPT_GETMNTENT_R
2525 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
2526 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
2528 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
2529 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
2530 if (res) write_mntent(ctx, res);
2533 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
2535 #define INIT_GETMNTENT_R
2538 #if SANITIZER_INTERCEPT_STATFS
2539 INTERCEPTOR(int, statfs, char *path, void *buf) {
2541 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
2542 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2543 int res = REAL(statfs)(path, buf);
2544 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
2547 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
2549 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
2550 int res = REAL(fstatfs)(fd, buf);
2551 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
2554 #define INIT_STATFS \
2555 COMMON_INTERCEPT_FUNCTION(statfs); \
2556 COMMON_INTERCEPT_FUNCTION(fstatfs);
2561 #if SANITIZER_INTERCEPT_STATFS64
2562 INTERCEPTOR(int, statfs64, char *path, void *buf) {
2564 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
2565 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2566 int res = REAL(statfs64)(path, buf);
2567 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
2570 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
2572 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
2573 int res = REAL(fstatfs64)(fd, buf);
2574 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
2577 #define INIT_STATFS64 \
2578 COMMON_INTERCEPT_FUNCTION(statfs64); \
2579 COMMON_INTERCEPT_FUNCTION(fstatfs64);
2581 #define INIT_STATFS64
2584 #if SANITIZER_INTERCEPT_STATVFS
2585 INTERCEPTOR(int, statvfs, char *path, void *buf) {
2587 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
2588 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2589 int res = REAL(statvfs)(path, buf);
2590 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
2593 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
2595 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
2596 int res = REAL(fstatvfs)(fd, buf);
2597 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
2600 #define INIT_STATVFS \
2601 COMMON_INTERCEPT_FUNCTION(statvfs); \
2602 COMMON_INTERCEPT_FUNCTION(fstatvfs);
2604 #define INIT_STATVFS
2607 #if SANITIZER_INTERCEPT_STATVFS64
2608 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
2610 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
2611 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2612 int res = REAL(statvfs64)(path, buf);
2613 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
2616 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
2618 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
2619 int res = REAL(fstatvfs64)(fd, buf);
2620 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
2623 #define INIT_STATVFS64 \
2624 COMMON_INTERCEPT_FUNCTION(statvfs64); \
2625 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
2627 #define INIT_STATVFS64
2630 #if SANITIZER_INTERCEPT_INITGROUPS
2631 INTERCEPTOR(int, initgroups, char *user, u32 group) {
2633 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
2634 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
2635 int res = REAL(initgroups)(user, group);
2638 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
2640 #define INIT_INITGROUPS
2643 #if SANITIZER_INTERCEPT_ETHER
2644 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
2646 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
2647 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2648 char *res = REAL(ether_ntoa)(addr);
2649 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2652 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
2654 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
2655 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2656 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
2657 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, sizeof(*res));
2660 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
2662 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
2663 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2664 int res = REAL(ether_ntohost)(hostname, addr);
2665 if (!res && hostname)
2666 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2669 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
2671 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
2673 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2674 int res = REAL(ether_hostton)(hostname, addr);
2675 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2678 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
2681 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
2682 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
2683 int res = REAL(ether_line)(line, addr, hostname);
2685 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2687 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2691 #define INIT_ETHER \
2692 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
2693 COMMON_INTERCEPT_FUNCTION(ether_aton); \
2694 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
2695 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
2696 COMMON_INTERCEPT_FUNCTION(ether_line);
2701 #if SANITIZER_INTERCEPT_ETHER_R
2702 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
2704 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
2705 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2706 char *res = REAL(ether_ntoa_r)(addr, buf);
2707 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2710 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
2711 __sanitizer_ether_addr *addr) {
2713 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
2714 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2715 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
2716 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
2719 #define INIT_ETHER_R \
2720 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
2721 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
2723 #define INIT_ETHER_R
2726 #if SANITIZER_INTERCEPT_SHMCTL
2727 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
2729 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
2730 int res = REAL(shmctl)(shmid, cmd, buf);
2733 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
2734 sz = sizeof(__sanitizer_shmid_ds);
2735 else if (cmd == shmctl_ipc_info)
2736 sz = struct_shminfo_sz;
2737 else if (cmd == shmctl_shm_info)
2738 sz = struct_shm_info_sz;
2739 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
2743 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
2748 #if SANITIZER_INTERCEPT_RANDOM_R
2749 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
2751 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
2752 int res = REAL(random_r)(buf, result);
2754 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2757 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
2759 #define INIT_RANDOM_R
2762 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
2763 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED
2764 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
2765 INTERCEPTOR(int, pthread_attr_get##what, void *attr, void *r) { \
2767 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_get##what, attr, r); \
2768 int res = REAL(pthread_attr_get##what)(attr, r); \
2769 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
2774 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
2775 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
2776 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
2777 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
2778 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
2779 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
2780 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
2781 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
2783 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
2784 int res = REAL(pthread_attr_getstack)(attr, addr, size);
2786 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2787 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
2792 // We may need to call the real pthread_attr_getstack from the run-time
2793 // in sanitizer_common, but we don't want to include the interception headers
2794 // there. So, just define this function here.
2795 namespace __sanitizer {
2796 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
2797 return REAL(pthread_attr_getstack)(attr, addr, size);
2799 } // namespace __sanitizer
2801 #define INIT_PTHREAD_ATTR_GET \
2802 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
2803 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
2804 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
2805 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
2806 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
2807 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
2808 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
2810 #define INIT_PTHREAD_ATTR_GET
2813 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
2814 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
2816 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
2817 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
2819 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
2822 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
2823 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
2826 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
2828 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
2829 if (!res && cpusetsize && cpuset)
2830 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
2834 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
2835 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
2837 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
2840 #if SANITIZER_INTERCEPT_TMPNAM
2841 INTERCEPTOR(char *, tmpnam, char *s) {
2843 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
2844 char *res = REAL(tmpnam)(s);
2847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
2849 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2853 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
2858 #if SANITIZER_INTERCEPT_TMPNAM_R
2859 INTERCEPTOR(char *, tmpnam_r, char *s) {
2861 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
2862 char *res = REAL(tmpnam_r)(s);
2863 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
2866 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
2868 #define INIT_TMPNAM_R
2871 #if SANITIZER_INTERCEPT_TEMPNAM
2872 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
2874 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
2875 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
2876 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
2877 char *res = REAL(tempnam)(dir, pfx);
2878 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2881 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
2883 #define INIT_TEMPNAM
2886 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
2887 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
2889 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
2890 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
2891 return REAL(pthread_setname_np)(thread, name);
2893 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
2895 #define INIT_PTHREAD_SETNAME_NP
2898 #if SANITIZER_INTERCEPT_SINCOS
2899 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
2901 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
2902 REAL(sincos)(x, sin, cos);
2903 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2904 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2906 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
2908 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
2909 REAL(sincosf)(x, sin, cos);
2910 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2911 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2913 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
2915 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
2916 REAL(sincosl)(x, sin, cos);
2917 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2918 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2920 #define INIT_SINCOS \
2921 COMMON_INTERCEPT_FUNCTION(sincos); \
2922 COMMON_INTERCEPT_FUNCTION(sincosf); \
2923 COMMON_INTERCEPT_FUNCTION(sincosl);
2928 #if SANITIZER_INTERCEPT_REMQUO
2929 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
2931 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
2932 double res = REAL(remquo)(x, y, quo);
2933 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2936 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
2938 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
2939 float res = REAL(remquof)(x, y, quo);
2940 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2943 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
2945 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
2946 long double res = REAL(remquol)(x, y, quo);
2947 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2950 #define INIT_REMQUO \
2951 COMMON_INTERCEPT_FUNCTION(remquo); \
2952 COMMON_INTERCEPT_FUNCTION(remquof); \
2953 COMMON_INTERCEPT_FUNCTION(remquol);
2958 #if SANITIZER_INTERCEPT_LGAMMA
2960 INTERCEPTOR(double, lgamma, double x) {
2962 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
2963 double res = REAL(lgamma)(x);
2964 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2967 INTERCEPTOR(float, lgammaf, float x) {
2969 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
2970 float res = REAL(lgammaf)(x);
2971 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2974 INTERCEPTOR(long double, lgammal, long double x) {
2976 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
2977 long double res = REAL(lgammal)(x);
2978 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2981 #define INIT_LGAMMA \
2982 COMMON_INTERCEPT_FUNCTION(lgamma); \
2983 COMMON_INTERCEPT_FUNCTION(lgammaf); \
2984 COMMON_INTERCEPT_FUNCTION(lgammal);
2989 #if SANITIZER_INTERCEPT_LGAMMA_R
2990 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
2992 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
2993 double res = REAL(lgamma_r)(x, signp);
2994 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2997 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
2999 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
3000 float res = REAL(lgammaf_r)(x, signp);
3001 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3004 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
3006 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
3007 long double res = REAL(lgammal_r)(x, signp);
3008 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
3011 #define INIT_LGAMMA_R \
3012 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
3013 COMMON_INTERCEPT_FUNCTION(lgammaf_r); \
3014 COMMON_INTERCEPT_FUNCTION(lgammal_r);
3016 #define INIT_LGAMMA_R
3019 #if SANITIZER_INTERCEPT_DRAND48_R
3020 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
3022 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
3023 int res = REAL(drand48_r)(buffer, result);
3024 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3027 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
3029 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
3030 int res = REAL(lrand48_r)(buffer, result);
3031 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3034 #define INIT_DRAND48_R \
3035 COMMON_INTERCEPT_FUNCTION(drand48_r); \
3036 COMMON_INTERCEPT_FUNCTION(lrand48_r);
3038 #define INIT_DRAND48_R
3041 #if SANITIZER_INTERCEPT_GETLINE
3042 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
3044 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
3045 SSIZE_T res = REAL(getline)(lineptr, n, stream);
3047 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
3048 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
3049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
3053 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
3056 COMMON_INTERCEPTOR_ENTER(ctx, getdelim, lineptr, n, delim, stream);
3057 SSIZE_T res = REAL(getdelim)(lineptr, n, delim, stream);
3059 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
3060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
3061 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
3065 #define INIT_GETLINE \
3066 COMMON_INTERCEPT_FUNCTION(getline); \
3067 COMMON_INTERCEPT_FUNCTION(getdelim);
3069 #define INIT_GETLINE
3072 #if SANITIZER_INTERCEPT_ICONV
3073 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
3074 char **outbuf, SIZE_T *outbytesleft) {
3076 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
3079 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
3080 if (inbuf && inbytesleft)
3081 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
3083 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
3084 void *outbuf_orig = outbuf ? *outbuf : 0;
3085 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
3086 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
3087 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
3088 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
3092 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
3097 #if SANITIZER_INTERCEPT_TIMES
3098 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
3100 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
3101 __sanitizer_clock_t res = REAL(times)(tms);
3102 if (res != (__sanitizer_clock_t)-1 && tms)
3103 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
3106 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
3111 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
3112 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
3113 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
3115 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
3116 void *res = REAL(__tls_get_addr)(arg);
3117 DTLS_on_tls_get_addr(arg, res);
3121 #define INIT_TLS_GET_ADDR
3124 #if SANITIZER_INTERCEPT_LISTXATTR
3125 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
3127 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
3128 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3129 SSIZE_T res = REAL(listxattr)(path, list, size);
3130 // Here and below, size == 0 is a special case where nothing is written to the
3131 // buffer, and res contains the desired buffer size.
3132 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3135 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
3137 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
3138 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3139 SSIZE_T res = REAL(llistxattr)(path, list, size);
3140 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3143 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
3145 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
3146 SSIZE_T res = REAL(flistxattr)(fd, list, size);
3147 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
3150 #define INIT_LISTXATTR \
3151 COMMON_INTERCEPT_FUNCTION(listxattr); \
3152 COMMON_INTERCEPT_FUNCTION(llistxattr); \
3153 COMMON_INTERCEPT_FUNCTION(flistxattr);
3155 #define INIT_LISTXATTR
3158 #if SANITIZER_INTERCEPT_GETXATTR
3159 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
3162 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
3163 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3164 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3165 SSIZE_T res = REAL(getxattr)(path, name, value, size);
3166 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
3169 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
3172 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
3173 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3174 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3175 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
3176 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
3179 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
3182 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
3183 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
3184 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
3185 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
3188 #define INIT_GETXATTR \
3189 COMMON_INTERCEPT_FUNCTION(getxattr); \
3190 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
3191 COMMON_INTERCEPT_FUNCTION(fgetxattr);
3193 #define INIT_GETXATTR
3196 #if SANITIZER_INTERCEPT_GETRESID
3197 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
3199 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
3200 int res = REAL(getresuid)(ruid, euid, suid);
3202 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
3203 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
3204 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
3208 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
3210 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
3211 int res = REAL(getresgid)(rgid, egid, sgid);
3213 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
3214 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
3215 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
3219 #define INIT_GETRESID \
3220 COMMON_INTERCEPT_FUNCTION(getresuid); \
3221 COMMON_INTERCEPT_FUNCTION(getresgid);
3223 #define INIT_GETRESID
3226 #if SANITIZER_INTERCEPT_GETIFADDRS
3227 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
3228 // intercept freeifaddrs(). If that ceases to be the case, we might need to
3229 // intercept it to poison the memory again.
3230 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
3232 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
3233 int res = REAL(getifaddrs)(ifap);
3234 if (res == 0 && ifap) {
3235 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
3236 __sanitizer_ifaddrs *p = *ifap;
3238 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
3240 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
3241 REAL(strlen)(p->ifa_name) + 1);
3243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
3245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
3246 // On Linux this is a union, but the other member also points to a
3247 // struct sockaddr, so the following is sufficient.
3249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
3250 // FIXME(smatveev): Unpoison p->ifa_data as well.
3256 #define INIT_GETIFADDRS \
3257 COMMON_INTERCEPT_FUNCTION(getifaddrs);
3259 #define INIT_GETIFADDRS
3262 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
3263 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
3265 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
3266 char *res = REAL(if_indextoname)(ifindex, ifname);
3268 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
3271 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
3273 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
3275 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
3276 return REAL(if_nametoindex)(ifname);
3278 #define INIT_IF_INDEXTONAME \
3279 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
3280 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
3282 #define INIT_IF_INDEXTONAME
3285 #if SANITIZER_INTERCEPT_CAPGET
3286 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
3288 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
3290 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
3291 int res = REAL(capget)(hdrp, datap);
3292 if (res == 0 && datap)
3293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
3294 // We can also return -1 and write to hdrp->version if the version passed in
3295 // hdrp->version is unsupported. But that's not a trivial condition to check,
3296 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
3299 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
3301 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
3303 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
3305 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
3306 return REAL(capset)(hdrp, datap);
3308 #define INIT_CAPGET \
3309 COMMON_INTERCEPT_FUNCTION(capget); \
3310 COMMON_INTERCEPT_FUNCTION(capset);
3315 #define SANITIZER_COMMON_INTERCEPTORS_INIT \
3334 INIT_LOCALTIME_AND_FRIENDS; \
3337 INIT_ISOC99_SCANF; \
3339 INIT_ISOC99_PRINTF; \
3341 INIT_FREXPF_FREXPL; \
3342 INIT_GETPWNAM_AND_FRIENDS; \
3343 INIT_GETPWNAM_R_AND_FRIENDS; \
3344 INIT_CLOCK_GETTIME; \
3351 INIT_PTHREAD_GETSCHEDPARAM; \
3355 INIT_GETHOSTBYNAME; \
3356 INIT_GETHOSTBYNAME_R; \
3371 INIT_GET_CURRENT_DIR_NAME; \
3379 INIT_CANONICALIZE_FILE_NAME; \
3381 INIT_SCHED_GETAFFINITY; \
3384 INIT_XPG_STRERROR_R; \
3393 INIT_SIGTIMEDWAIT; \
3399 INIT_PTHREAD_MUTEX_LOCK; \
3400 INIT_PTHREAD_MUTEX_UNLOCK; \
3401 INIT_PTHREAD_COND; \
3413 INIT_PTHREAD_ATTR_GET; \
3414 INIT_PTHREAD_ATTR_GETINHERITSCHED; \
3415 INIT_PTHREAD_ATTR_GETAFFINITY_NP; \
3419 INIT_PTHREAD_SETNAME_NP; \
3428 INIT_TLS_GET_ADDR; \
3433 INIT_IF_INDEXTONAME; \