1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Common function interceptors for tools like AddressSanitizer,
9 // ThreadSanitizer, MemorySanitizer, etc.
11 // This file should be included into the tool's interceptor file,
12 // which has to define it's own macros:
13 // COMMON_INTERCEPTOR_ENTER
14 // COMMON_INTERCEPTOR_READ_RANGE
15 // COMMON_INTERCEPTOR_WRITE_RANGE
16 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
17 // COMMON_INTERCEPTOR_FD_ACQUIRE
18 // COMMON_INTERCEPTOR_FD_RELEASE
19 // COMMON_INTERCEPTOR_FD_ACCESS
20 // COMMON_INTERCEPTOR_SET_THREAD_NAME
21 // COMMON_INTERCEPTOR_ON_EXIT
22 // COMMON_INTERCEPTOR_MUTEX_LOCK
23 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
24 // COMMON_INTERCEPTOR_MUTEX_REPAIR
25 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
26 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
27 //===----------------------------------------------------------------------===//
28 #include "interception/interception.h"
29 #include "sanitizer_platform_interceptors.h"
33 #if SANITIZER_WINDOWS && !defined(va_copy)
34 #define va_copy(dst, src) ((dst) = (src))
37 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
38 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, p, size) {}
41 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
42 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
45 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
46 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
49 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
50 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
53 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
54 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
57 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
58 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
61 #if SANITIZER_INTERCEPT_TEXTDOMAIN
62 INTERCEPTOR(char*, textdomain, const char *domainname) {
64 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
65 char* domain = REAL(textdomain)(domainname);
67 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, domain,
68 REAL(strlen)(domain) + 1);
72 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
74 #define INIT_TEXTDOMAIN
77 #if SANITIZER_INTERCEPT_STRCMP
78 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
79 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
82 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
84 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
88 c1 = (unsigned char)s1[i];
89 c2 = (unsigned char)s2[i];
90 if (c1 != c2 || c1 == '\0') break;
92 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
93 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
94 return CharCmpX(c1, c2);
97 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
99 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
100 unsigned char c1 = 0, c2 = 0;
102 for (i = 0; i < size; i++) {
103 c1 = (unsigned char)s1[i];
104 c2 = (unsigned char)s2[i];
105 if (c1 != c2 || c1 == '\0') break;
107 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
108 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
109 return CharCmpX(c1, c2);
112 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
113 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
119 #if SANITIZER_INTERCEPT_STRCASECMP
120 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
121 int c1_low = ToLower(c1);
122 int c2_low = ToLower(c2);
123 return c1_low - c2_low;
126 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
128 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
129 unsigned char c1 = 0, c2 = 0;
132 c1 = (unsigned char)s1[i];
133 c2 = (unsigned char)s2[i];
134 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
136 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
137 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
138 return CharCaseCmp(c1, c2);
141 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
143 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
144 unsigned char c1 = 0, c2 = 0;
146 for (i = 0; i < n; i++) {
147 c1 = (unsigned char)s1[i];
148 c2 = (unsigned char)s2[i];
149 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
151 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
152 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
153 return CharCaseCmp(c1, c2);
156 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
157 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
159 #define INIT_STRCASECMP
160 #define INIT_STRNCASECMP
163 #if SANITIZER_INTERCEPT_FREXP
164 INTERCEPTOR(double, frexp, double x, int *exp) {
166 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
167 double res = REAL(frexp)(x, exp);
168 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
172 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
175 #endif // SANITIZER_INTERCEPT_FREXP
177 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
178 INTERCEPTOR(float, frexpf, float x, int *exp) {
180 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
181 float res = REAL(frexpf)(x, exp);
182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
186 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
188 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
189 long double res = REAL(frexpl)(x, exp);
190 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
194 #define INIT_FREXPF_FREXPL \
195 COMMON_INTERCEPT_FUNCTION(frexpf); \
196 COMMON_INTERCEPT_FUNCTION(frexpl)
198 #define INIT_FREXPF_FREXPL
199 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
202 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
203 SIZE_T iovlen, SIZE_T maxlen) {
204 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
205 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
211 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
212 SIZE_T iovlen, SIZE_T maxlen) {
213 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
214 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
215 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
216 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
222 #if SANITIZER_INTERCEPT_READ
223 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
225 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
226 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
227 SSIZE_T res = REAL(read)(fd, ptr, count);
228 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
229 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
232 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
237 #if SANITIZER_INTERCEPT_PREAD
238 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
240 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
241 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
242 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
243 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
244 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
247 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
252 #if SANITIZER_INTERCEPT_PREAD64
253 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
255 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
256 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
257 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
258 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
259 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
262 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
267 #if SANITIZER_INTERCEPT_READV
268 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
271 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
272 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
273 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
274 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
275 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
278 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
283 #if SANITIZER_INTERCEPT_PREADV
284 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
287 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
288 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
289 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
290 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
291 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
294 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
299 #if SANITIZER_INTERCEPT_PREADV64
300 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
303 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
304 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
305 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
306 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
307 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
310 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
312 #define INIT_PREADV64
315 #if SANITIZER_INTERCEPT_WRITE
316 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
318 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
319 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
320 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
321 SSIZE_T res = REAL(write)(fd, ptr, count);
322 // FIXME: this check should be _before_ the call to REAL(write), not after
323 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
326 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
331 #if SANITIZER_INTERCEPT_PWRITE
332 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
334 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
335 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
336 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
337 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
338 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
341 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
346 #if SANITIZER_INTERCEPT_PWRITE64
347 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
350 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
351 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
352 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
353 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
354 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
357 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
359 #define INIT_PWRITE64
362 #if SANITIZER_INTERCEPT_WRITEV
363 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
366 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
367 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
368 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
369 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
370 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
373 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
378 #if SANITIZER_INTERCEPT_PWRITEV
379 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
382 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
383 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
384 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
385 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
386 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
389 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
394 #if SANITIZER_INTERCEPT_PWRITEV64
395 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
398 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
399 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
400 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
401 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
402 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
405 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
407 #define INIT_PWRITEV64
410 #if SANITIZER_INTERCEPT_PRCTL
411 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
412 unsigned long arg3, // NOLINT
413 unsigned long arg4, unsigned long arg5) { // NOLINT
415 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
416 static const int PR_SET_NAME = 15;
417 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
418 if (option == PR_SET_NAME) {
420 internal_strncpy(buff, (char *)arg2, 15);
422 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
426 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
429 #endif // SANITIZER_INTERCEPT_PRCTL
431 #if SANITIZER_INTERCEPT_TIME
432 INTERCEPTOR(unsigned long, time, unsigned long *t) {
434 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
435 unsigned long res = REAL(time)(t);
436 if (t && res != (unsigned long)-1) {
437 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
441 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
444 #endif // SANITIZER_INTERCEPT_TIME
446 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
447 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
450 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
451 // can point to shared memory and tsan would report a data race.
452 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, tm->tm_zone,
453 REAL(strlen(tm->tm_zone)) + 1);
456 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
458 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
459 __sanitizer_tm *res = REAL(localtime)(timep);
461 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
462 unpoison_tm(ctx, res);
466 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
468 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
469 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
471 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
472 unpoison_tm(ctx, res);
476 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
478 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
479 __sanitizer_tm *res = REAL(gmtime)(timep);
481 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
482 unpoison_tm(ctx, res);
486 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
488 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
489 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
491 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
492 unpoison_tm(ctx, res);
496 INTERCEPTOR(char *, ctime, unsigned long *timep) {
498 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
499 char *res = REAL(ctime)(timep);
501 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
502 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
506 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
508 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
509 char *res = REAL(ctime_r)(timep, result);
511 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
512 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
516 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
518 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
519 char *res = REAL(asctime)(tm);
521 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
526 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
528 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
529 char *res = REAL(asctime_r)(tm, result);
531 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
536 #define INIT_LOCALTIME_AND_FRIENDS \
537 COMMON_INTERCEPT_FUNCTION(localtime); \
538 COMMON_INTERCEPT_FUNCTION(localtime_r); \
539 COMMON_INTERCEPT_FUNCTION(gmtime); \
540 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
541 COMMON_INTERCEPT_FUNCTION(ctime); \
542 COMMON_INTERCEPT_FUNCTION(ctime_r); \
543 COMMON_INTERCEPT_FUNCTION(asctime); \
544 COMMON_INTERCEPT_FUNCTION(asctime_r);
546 #define INIT_LOCALTIME_AND_FRIENDS
547 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
549 #if SANITIZER_INTERCEPT_STRPTIME
550 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
552 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
554 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
555 char *res = REAL(strptime)(s, format, tm);
557 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, res - s);
558 // Do not call unpoison_tm here, because strptime does not, in fact,
559 // initialize the entire struct tm. For example, tm_zone pointer is left
561 if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
565 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
567 #define INIT_STRPTIME
570 #if SANITIZER_INTERCEPT_SCANF
572 #include "sanitizer_common_interceptors_scanf.inc"
574 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
577 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
580 int res = REAL(vname)(__VA_ARGS__); \
582 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
587 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
588 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
590 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
591 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
593 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
594 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
596 #if SANITIZER_INTERCEPT_ISOC99_SCANF
597 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
598 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
600 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
602 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
604 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
605 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
606 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
608 #define SCANF_INTERCEPTOR_IMPL(name, vname, ...) \
612 va_start(ap, format); \
613 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
614 int res = vname(__VA_ARGS__, ap); \
619 INTERCEPTOR(int, scanf, const char *format, ...)
620 SCANF_INTERCEPTOR_IMPL(scanf, vscanf, format)
622 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
623 SCANF_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
625 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
626 SCANF_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
628 #if SANITIZER_INTERCEPT_ISOC99_SCANF
629 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
630 SCANF_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
632 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
633 SCANF_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
635 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
636 SCANF_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
641 #if SANITIZER_INTERCEPT_SCANF
643 COMMON_INTERCEPT_FUNCTION(scanf); \
644 COMMON_INTERCEPT_FUNCTION(sscanf); \
645 COMMON_INTERCEPT_FUNCTION(fscanf); \
646 COMMON_INTERCEPT_FUNCTION(vscanf); \
647 COMMON_INTERCEPT_FUNCTION(vsscanf); \
648 COMMON_INTERCEPT_FUNCTION(vfscanf);
653 #if SANITIZER_INTERCEPT_ISOC99_SCANF
654 #define INIT_ISOC99_SCANF \
655 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
656 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
657 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
658 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
659 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
660 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
662 #define INIT_ISOC99_SCANF
665 #if SANITIZER_INTERCEPT_IOCTL
666 #include "sanitizer_common_interceptors_ioctl.inc"
667 INTERCEPTOR(int, ioctl, int d, unsigned request, void *arg) {
669 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
671 CHECK(ioctl_initialized);
673 // Note: TSan does not use common flags, and they are zero-initialized.
674 // This effectively disables ioctl handling in TSan.
675 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
677 const ioctl_desc *desc = ioctl_lookup(request);
678 if (!desc) Printf("WARNING: unknown ioctl %x\n", request);
680 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
681 int res = REAL(ioctl)(d, request, arg);
682 // FIXME: some ioctls have different return values for success and failure.
683 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
688 COMMON_INTERCEPT_FUNCTION(ioctl);
693 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
694 INTERCEPTOR(void *, getpwnam, const char *name) {
696 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
697 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
698 void *res = REAL(getpwnam)(name);
699 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz);
702 INTERCEPTOR(void *, getpwuid, u32 uid) {
704 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
705 void *res = REAL(getpwuid)(uid);
706 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz);
709 INTERCEPTOR(void *, getgrnam, const char *name) {
711 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
712 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
713 void *res = REAL(getgrnam)(name);
714 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz);
717 INTERCEPTOR(void *, getgrgid, u32 gid) {
719 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
720 void *res = REAL(getgrgid)(gid);
721 if (res != 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz);
724 #define INIT_GETPWNAM_AND_FRIENDS \
725 COMMON_INTERCEPT_FUNCTION(getpwnam); \
726 COMMON_INTERCEPT_FUNCTION(getpwuid); \
727 COMMON_INTERCEPT_FUNCTION(getgrnam); \
728 COMMON_INTERCEPT_FUNCTION(getgrgid);
730 #define INIT_GETPWNAM_AND_FRIENDS
733 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
734 INTERCEPTOR(int, getpwnam_r, const char *name, void *pwd, char *buf,
735 SIZE_T buflen, void **result) {
737 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
738 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
739 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz);
742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
746 INTERCEPTOR(int, getpwuid_r, u32 uid, void *pwd, char *buf, SIZE_T buflen,
749 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
750 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
752 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz);
753 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
757 INTERCEPTOR(int, getgrnam_r, const char *name, void *grp, char *buf,
758 SIZE_T buflen, void **result) {
760 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
761 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
762 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz);
765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
769 INTERCEPTOR(int, getgrgid_r, u32 gid, void *grp, char *buf, SIZE_T buflen,
772 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
773 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz);
776 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
780 #define INIT_GETPWNAM_R_AND_FRIENDS \
781 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
782 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
783 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
784 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
786 #define INIT_GETPWNAM_R_AND_FRIENDS
789 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
790 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
792 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
793 int res = REAL(clock_getres)(clk_id, tp);
795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
799 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
801 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
802 int res = REAL(clock_gettime)(clk_id, tp);
804 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
808 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
810 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
811 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
812 return REAL(clock_settime)(clk_id, tp);
814 #define INIT_CLOCK_GETTIME \
815 COMMON_INTERCEPT_FUNCTION(clock_getres); \
816 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
817 COMMON_INTERCEPT_FUNCTION(clock_settime);
819 #define INIT_CLOCK_GETTIME
822 #if SANITIZER_INTERCEPT_GETITIMER
823 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
825 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
826 int res = REAL(getitimer)(which, curr_value);
827 if (!res && curr_value) {
828 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
832 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
834 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
836 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
837 int res = REAL(setitimer)(which, new_value, old_value);
838 if (!res && old_value) {
839 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
843 #define INIT_GETITIMER \
844 COMMON_INTERCEPT_FUNCTION(getitimer); \
845 COMMON_INTERCEPT_FUNCTION(setitimer);
847 #define INIT_GETITIMER
850 #if SANITIZER_INTERCEPT_GLOB
851 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
853 // +1 for NULL pointer at the end.
855 COMMON_INTERCEPTOR_WRITE_RANGE(
856 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
857 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
858 char *p = pglob->gl_pathv[i];
859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
863 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
864 static THREADLOCAL void *glob_ctx;
866 static void wrapped_gl_closedir(void *dir) {
867 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
868 pglob_copy->gl_closedir(dir);
871 static void *wrapped_gl_readdir(void *dir) {
872 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
873 return pglob_copy->gl_readdir(dir);
876 static void *wrapped_gl_opendir(const char *s) {
877 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 1);
878 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
879 return pglob_copy->gl_opendir(s);
882 static int wrapped_gl_lstat(const char *s, void *st) {
883 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 2);
884 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
885 return pglob_copy->gl_lstat(s, st);
888 static int wrapped_gl_stat(const char *s, void *st) {
889 COMMON_INTERCEPTOR_UNPOISON_PARAM(glob_ctx, 2);
890 COMMON_INTERCEPTOR_WRITE_RANGE(glob_ctx, s, REAL(strlen)(s) + 1);
891 return pglob_copy->gl_stat(s, st);
894 INTERCEPTOR(int, glob, const char *pattern, int flags,
895 int (*errfunc)(const char *epath, int eerrno),
896 __sanitizer_glob_t *pglob) {
898 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
899 __sanitizer_glob_t glob_copy = {
901 0, wrapped_gl_closedir, wrapped_gl_readdir,
902 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
903 if (flags & glob_altdirfunc) {
904 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
905 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
906 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
907 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
908 Swap(pglob->gl_stat, glob_copy.gl_stat);
909 pglob_copy = &glob_copy;
912 int res = REAL(glob)(pattern, flags, errfunc, pglob);
913 if (flags & glob_altdirfunc) {
914 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
915 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
916 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
917 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
918 Swap(pglob->gl_stat, glob_copy.gl_stat);
922 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
926 INTERCEPTOR(int, glob64, const char *pattern, int flags,
927 int (*errfunc)(const char *epath, int eerrno),
928 __sanitizer_glob_t *pglob) {
930 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
931 __sanitizer_glob_t glob_copy = {
933 0, wrapped_gl_closedir, wrapped_gl_readdir,
934 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
935 if (flags & glob_altdirfunc) {
936 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
937 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
938 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
939 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
940 Swap(pglob->gl_stat, glob_copy.gl_stat);
941 pglob_copy = &glob_copy;
944 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
945 if (flags & glob_altdirfunc) {
946 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
947 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
948 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
949 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
950 Swap(pglob->gl_stat, glob_copy.gl_stat);
954 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
958 COMMON_INTERCEPT_FUNCTION(glob); \
959 COMMON_INTERCEPT_FUNCTION(glob64);
960 #else // SANITIZER_INTERCEPT_GLOB
962 #endif // SANITIZER_INTERCEPT_GLOB
964 #if SANITIZER_INTERCEPT_WAIT
965 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
966 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
968 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
970 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
971 int res = REAL(wait)(status);
972 if (res != -1 && status)
973 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
976 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
979 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
980 int res = REAL(waitid)(idtype, id, infop, options);
981 if (res != -1 && infop)
982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
985 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
987 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
988 int res = REAL(waitpid)(pid, status, options);
989 if (res != -1 && status)
990 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
993 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
995 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
996 int res = REAL(wait3)(status, options, rusage);
998 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
999 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1003 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
1005 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
1006 int res = REAL(wait4)(pid, status, options, rusage);
1008 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
1009 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
1014 COMMON_INTERCEPT_FUNCTION(wait); \
1015 COMMON_INTERCEPT_FUNCTION(waitid); \
1016 COMMON_INTERCEPT_FUNCTION(waitpid); \
1017 COMMON_INTERCEPT_FUNCTION(wait3); \
1018 COMMON_INTERCEPT_FUNCTION(wait4);
1023 #if SANITIZER_INTERCEPT_INET
1024 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
1026 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
1027 uptr sz = __sanitizer_in_addr_sz(af);
1028 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
1029 // FIXME: figure out read size based on the address family.
1030 char *res = REAL(inet_ntop)(af, src, dst, size);
1031 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1034 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
1036 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
1037 // FIXME: figure out read size based on the address family.
1038 int res = REAL(inet_pton)(af, src, dst);
1040 uptr sz = __sanitizer_in_addr_sz(af);
1041 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1046 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
1047 COMMON_INTERCEPT_FUNCTION(inet_pton);
1052 #if SANITIZER_INTERCEPT_INET
1053 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
1055 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
1056 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
1057 int res = REAL(inet_aton)(cp, dst);
1059 uptr sz = __sanitizer_in_addr_sz(af_inet);
1060 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
1064 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
1066 #define INIT_INET_ATON
1069 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
1070 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
1072 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
1073 int res = REAL(pthread_getschedparam)(thread, policy, param);
1075 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
1076 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
1080 #define INIT_PTHREAD_GETSCHEDPARAM \
1081 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
1083 #define INIT_PTHREAD_GETSCHEDPARAM
1086 #if SANITIZER_INTERCEPT_GETADDRINFO
1087 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
1088 struct __sanitizer_addrinfo *hints,
1089 struct __sanitizer_addrinfo **out) {
1091 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
1092 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
1094 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
1096 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
1097 int res = REAL(getaddrinfo)(node, service, hints, out);
1098 if (res == 0 && out) {
1099 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
1100 struct __sanitizer_addrinfo *p = *out;
1102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
1104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
1105 if (p->ai_canonname)
1106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
1107 REAL(strlen)(p->ai_canonname) + 1);
1113 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
1115 #define INIT_GETADDRINFO
1118 #if SANITIZER_INTERCEPT_GETNAMEINFO
1119 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
1120 unsigned hostlen, char *serv, unsigned servlen, int flags) {
1122 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
1123 serv, servlen, flags);
1124 // FIXME: consider adding READ_RANGE(sockaddr, salen)
1125 // There is padding in in_addr that may make this too noisy
1127 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
1129 if (host && hostlen)
1130 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
1131 if (serv && servlen)
1132 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
1136 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
1138 #define INIT_GETNAMEINFO
1141 #if SANITIZER_INTERCEPT_GETSOCKNAME
1142 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
1144 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
1145 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1146 int addrlen_in = *addrlen;
1147 int res = REAL(getsockname)(sock_fd, addr, addrlen);
1149 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
1153 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
1155 #define INIT_GETSOCKNAME
1158 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1159 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
1160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
1162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
1163 char **p = h->h_aliases;
1165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1168 COMMON_INTERCEPTOR_WRITE_RANGE(
1169 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
1172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
1175 COMMON_INTERCEPTOR_WRITE_RANGE(
1176 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
1180 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
1181 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
1183 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
1184 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
1185 if (res) write_hostent(ctx, res);
1189 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
1192 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
1193 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1194 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
1195 if (res) write_hostent(ctx, res);
1199 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
1201 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
1202 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
1203 if (res) write_hostent(ctx, res);
1207 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
1209 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
1210 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
1211 if (res) write_hostent(ctx, res);
1214 #define INIT_GETHOSTBYNAME \
1215 COMMON_INTERCEPT_FUNCTION(gethostent); \
1216 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
1217 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
1218 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
1220 #define INIT_GETHOSTBYNAME
1223 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
1224 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
1225 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
1227 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
1229 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
1232 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1233 if (*result) write_hostent(ctx, *result);
1236 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1241 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
1242 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1243 __sanitizer_hostent **result, int *h_errnop) {
1245 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
1246 buflen, result, h_errnop);
1247 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
1248 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
1252 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1253 if (*result) write_hostent(ctx, *result);
1256 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1261 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
1262 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
1265 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
1267 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
1270 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1271 if (*result) write_hostent(ctx, *result);
1274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1279 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
1280 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
1281 __sanitizer_hostent **result, int *h_errnop) {
1283 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
1286 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
1289 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1290 if (*result) write_hostent(ctx, *result);
1293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
1297 #define INIT_GETHOSTBYNAME_R \
1298 COMMON_INTERCEPT_FUNCTION(gethostent_r); \
1299 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r); \
1300 COMMON_INTERCEPT_FUNCTION(gethostbyname_r); \
1301 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
1303 #define INIT_GETHOSTBYNAME_R
1306 #if SANITIZER_INTERCEPT_GETSOCKOPT
1307 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
1310 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
1312 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
1313 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
1315 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
1318 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
1320 #define INIT_GETSOCKOPT
1323 #if SANITIZER_INTERCEPT_ACCEPT
1324 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
1326 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
1329 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1330 addrlen0 = *addrlen;
1332 int fd2 = REAL(accept)(fd, addr, addrlen);
1334 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1335 if (addr && addrlen)
1336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1340 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
1345 #if SANITIZER_INTERCEPT_ACCEPT4
1346 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
1348 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
1351 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
1352 addrlen0 = *addrlen;
1354 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
1356 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
1357 if (addr && addrlen)
1358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
1362 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
1364 #define INIT_ACCEPT4
1367 #if SANITIZER_INTERCEPT_MODF
1368 INTERCEPTOR(double, modf, double x, double *iptr) {
1370 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
1371 double res = REAL(modf)(x, iptr);
1373 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1377 INTERCEPTOR(float, modff, float x, float *iptr) {
1379 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
1380 float res = REAL(modff)(x, iptr);
1382 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1386 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
1388 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
1389 long double res = REAL(modfl)(x, iptr);
1391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
1396 COMMON_INTERCEPT_FUNCTION(modf); \
1397 COMMON_INTERCEPT_FUNCTION(modff); \
1398 COMMON_INTERCEPT_FUNCTION(modfl);
1403 #if SANITIZER_INTERCEPT_RECVMSG
1404 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
1406 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
1407 if (msg->msg_name && msg->msg_namelen)
1408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
1409 if (msg->msg_iov && msg->msg_iovlen)
1410 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
1411 sizeof(*msg->msg_iov) * msg->msg_iovlen);
1412 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
1413 if (msg->msg_control && msg->msg_controllen)
1414 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
1417 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
1420 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
1421 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
1423 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1425 write_msghdr(ctx, msg, res);
1426 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
1431 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
1433 #define INIT_RECVMSG
1436 #if SANITIZER_INTERCEPT_GETPEERNAME
1437 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
1439 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
1441 if (addrlen) addr_sz = *addrlen;
1442 int res = REAL(getpeername)(sockfd, addr, addrlen);
1443 if (!res && addr && addrlen)
1444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
1447 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
1449 #define INIT_GETPEERNAME
1452 #if SANITIZER_INTERCEPT_SYSINFO
1453 INTERCEPTOR(int, sysinfo, void *info) {
1455 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
1456 int res = REAL(sysinfo)(info);
1458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
1461 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
1463 #define INIT_SYSINFO
1466 #if SANITIZER_INTERCEPT_READDIR
1467 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
1469 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
1470 __sanitizer_dirent *res = REAL(readdir)(dirp);
1471 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
1475 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
1476 __sanitizer_dirent **result) {
1478 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
1479 int res = REAL(readdir_r)(dirp, entry, result);
1481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
1488 #define INIT_READDIR \
1489 COMMON_INTERCEPT_FUNCTION(readdir); \
1490 COMMON_INTERCEPT_FUNCTION(readdir_r);
1492 #define INIT_READDIR
1495 #if SANITIZER_INTERCEPT_READDIR64
1496 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
1498 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
1499 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
1500 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
1504 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
1505 __sanitizer_dirent64 **result) {
1507 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
1508 int res = REAL(readdir64_r)(dirp, entry, result);
1510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1512 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
1516 #define INIT_READDIR64 \
1517 COMMON_INTERCEPT_FUNCTION(readdir64); \
1518 COMMON_INTERCEPT_FUNCTION(readdir64_r);
1520 #define INIT_READDIR64
1523 #if SANITIZER_INTERCEPT_PTRACE
1524 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
1526 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
1529 if (request == ptrace_setregs)
1530 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
1531 else if (request == ptrace_setfpregs)
1532 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
1533 else if (request == ptrace_setfpxregs)
1534 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
1535 else if (request == ptrace_setsiginfo)
1536 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
1537 else if (request == ptrace_setregset) {
1538 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
1539 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov->iov_base, iov->iov_len);
1543 uptr res = REAL(ptrace)(request, pid, addr, data);
1546 // Note that PEEK* requests assing different meaning to the return value.
1547 // This function does not handle them (nor does it need to).
1548 if (request == ptrace_getregs)
1549 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
1550 else if (request == ptrace_getfpregs)
1551 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
1552 else if (request == ptrace_getfpxregs)
1553 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
1554 else if (request == ptrace_getsiginfo)
1555 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
1556 else if (request == ptrace_getregset) {
1557 __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
1558 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iov->iov_base, iov->iov_len);
1564 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
1569 #if SANITIZER_INTERCEPT_SETLOCALE
1570 INTERCEPTOR(char *, setlocale, int category, char *locale) {
1572 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
1574 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
1575 char *res = REAL(setlocale)(category, locale);
1576 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1580 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
1582 #define INIT_SETLOCALE
1585 #if SANITIZER_INTERCEPT_GETCWD
1586 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
1588 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
1589 char *res = REAL(getcwd)(buf, size);
1590 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1593 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
1598 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
1599 INTERCEPTOR(char *, get_current_dir_name, int fake) {
1601 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
1602 char *res = REAL(get_current_dir_name)(fake);
1603 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1607 #define INIT_GET_CURRENT_DIR_NAME \
1608 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
1610 #define INIT_GET_CURRENT_DIR_NAME
1613 #if SANITIZER_INTERCEPT_STRTOIMAX
1614 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
1616 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
1617 INTMAX_T res = REAL(strtoimax)(nptr, endptr, base);
1618 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
1622 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
1624 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
1625 INTMAX_T res = REAL(strtoumax)(nptr, endptr, base);
1626 if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
1630 #define INIT_STRTOIMAX \
1631 COMMON_INTERCEPT_FUNCTION(strtoimax); \
1632 COMMON_INTERCEPT_FUNCTION(strtoumax);
1634 #define INIT_STRTOIMAX
1637 #if SANITIZER_INTERCEPT_MBSTOWCS
1638 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
1640 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
1641 SIZE_T res = REAL(mbstowcs)(dest, src, len);
1642 if (res != (SIZE_T) - 1 && dest) {
1643 SIZE_T write_cnt = res + (res < len);
1644 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1649 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
1652 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
1653 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1654 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1655 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
1656 if (res != (SIZE_T)(-1) && dest && src) {
1657 // This function, and several others, may or may not write the terminating
1658 // \0 character. They write it iff they clear *src.
1659 SIZE_T write_cnt = res + !*src;
1660 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1665 #define INIT_MBSTOWCS \
1666 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
1667 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
1669 #define INIT_MBSTOWCS
1672 #if SANITIZER_INTERCEPT_MBSNRTOWCS
1673 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
1674 SIZE_T len, void *ps) {
1676 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
1678 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1679 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
1681 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1682 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
1683 if (res != (SIZE_T)(-1) && dest && src) {
1684 SIZE_T write_cnt = res + !*src;
1685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
1690 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
1692 #define INIT_MBSNRTOWCS
1695 #if SANITIZER_INTERCEPT_WCSTOMBS
1696 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
1698 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
1699 SIZE_T res = REAL(wcstombs)(dest, src, len);
1700 if (res != (SIZE_T) - 1 && dest) {
1701 SIZE_T write_cnt = res + (res < len);
1702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1707 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
1710 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
1711 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1712 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1713 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
1714 if (res != (SIZE_T) - 1 && dest && src) {
1715 SIZE_T write_cnt = res + !*src;
1716 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1721 #define INIT_WCSTOMBS \
1722 COMMON_INTERCEPT_FUNCTION(wcstombs); \
1723 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
1725 #define INIT_WCSTOMBS
1728 #if SANITIZER_INTERCEPT_WCSNRTOMBS
1729 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
1730 SIZE_T len, void *ps) {
1732 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
1734 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
1735 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
1737 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
1738 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
1739 if (res != (SIZE_T) - 1 && dest && src) {
1740 SIZE_T write_cnt = res + !*src;
1741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
1746 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
1748 #define INIT_WCSNRTOMBS
1751 #if SANITIZER_INTERCEPT_TCGETATTR
1752 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
1754 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
1755 int res = REAL(tcgetattr)(fd, termios_p);
1756 if (!res && termios_p)
1757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
1761 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
1763 #define INIT_TCGETATTR
1766 #if SANITIZER_INTERCEPT_REALPATH
1767 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
1769 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
1770 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
1772 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
1773 // version of a versioned symbol. For realpath(), this gives us something
1774 // (called __old_realpath) that does not handle NULL in the second argument.
1775 // Handle it as part of the interceptor.
1776 char *allocated_path = 0;
1778 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
1780 char *res = REAL(realpath)(path, resolved_path);
1781 if (allocated_path && !res) WRAP(free)(allocated_path);
1782 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1785 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
1787 #define INIT_REALPATH
1790 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
1791 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
1793 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
1794 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
1795 char *res = REAL(canonicalize_file_name)(path);
1796 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1799 #define INIT_CANONICALIZE_FILE_NAME \
1800 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
1802 #define INIT_CANONICALIZE_FILE_NAME
1805 #if SANITIZER_INTERCEPT_CONFSTR
1806 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
1808 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
1809 SIZE_T res = REAL(confstr)(name, buf, len);
1811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
1814 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
1816 #define INIT_CONFSTR
1819 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
1820 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
1822 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
1823 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
1824 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
1827 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
1829 #define INIT_SCHED_GETAFFINITY
1832 #if SANITIZER_INTERCEPT_STRERROR
1833 INTERCEPTOR(char *, strerror, int errnum) {
1835 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
1836 char *res = REAL(strerror)(errnum);
1837 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1840 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
1842 #define INIT_STRERROR
1845 #if SANITIZER_INTERCEPT_STRERROR_R
1846 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
1848 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
1849 char *res = REAL(strerror_r)(errnum, buf, buflen);
1850 // There are 2 versions of strerror_r:
1851 // * POSIX version returns 0 on success, negative error code on failure,
1852 // writes message to buf.
1853 // * GNU version returns message pointer, which points to either buf or some
1855 SIZE_T posix_res = (SIZE_T)res;
1856 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
1857 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
1858 // At least on OSX, buf contents are valid even when the call fails.
1859 SIZE_T sz = internal_strnlen(buf, buflen);
1860 if (sz < buflen) ++sz;
1861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
1864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1868 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
1870 #define INIT_STRERROR_R
1873 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
1874 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
1876 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
1877 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
1878 // This version always returns a null-terminated string.
1880 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
1883 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
1885 #define INIT_XPG_STRERROR_R
1888 #if SANITIZER_INTERCEPT_SCANDIR
1889 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
1890 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
1891 const struct __sanitizer_dirent **);
1893 static THREADLOCAL void *scandir_ctx;
1894 static THREADLOCAL scandir_filter_f scandir_filter;
1895 static THREADLOCAL scandir_compar_f scandir_compar;
1897 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
1898 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 1);
1899 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, dir, dir->d_reclen);
1900 return scandir_filter(dir);
1903 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
1904 const struct __sanitizer_dirent **b) {
1905 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 2);
1906 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, a, sizeof(*a));
1907 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *a, (*a)->d_reclen);
1908 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, b, sizeof(*b));
1909 COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *b, (*b)->d_reclen);
1910 return scandir_compar(a, b);
1913 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
1914 scandir_filter_f filter, scandir_compar_f compar) {
1916 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
1917 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
1918 CHECK_EQ(0, scandir_ctx);
1920 scandir_filter = filter;
1921 scandir_compar = compar;
1922 int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
1923 compar ? wrapped_scandir_compar : 0);
1927 if (namelist && res > 0) {
1928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
1929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
1930 for (int i = 0; i < res; ++i)
1931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
1932 (*namelist)[i]->d_reclen);
1936 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
1938 #define INIT_SCANDIR
1941 #if SANITIZER_INTERCEPT_SCANDIR64
1942 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
1943 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
1944 const struct __sanitizer_dirent64 **);
1946 static THREADLOCAL void *scandir64_ctx;
1947 static THREADLOCAL scandir64_filter_f scandir64_filter;
1948 static THREADLOCAL scandir64_compar_f scandir64_compar;
1950 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
1951 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 1);
1952 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, dir, dir->d_reclen);
1953 return scandir64_filter(dir);
1956 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
1957 const struct __sanitizer_dirent64 **b) {
1958 COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 2);
1959 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, a, sizeof(*a));
1960 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *a, (*a)->d_reclen);
1961 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, b, sizeof(*b));
1962 COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *b, (*b)->d_reclen);
1963 return scandir64_compar(a, b);
1966 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
1967 scandir64_filter_f filter, scandir64_compar_f compar) {
1969 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
1970 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
1971 CHECK_EQ(0, scandir64_ctx);
1972 scandir64_ctx = ctx;
1973 scandir64_filter = filter;
1974 scandir64_compar = compar;
1976 REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
1977 compar ? wrapped_scandir64_compar : 0);
1979 scandir64_filter = 0;
1980 scandir64_compar = 0;
1981 if (namelist && res > 0) {
1982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
1983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
1984 for (int i = 0; i < res; ++i)
1985 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
1986 (*namelist)[i]->d_reclen);
1990 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
1992 #define INIT_SCANDIR64
1995 #if SANITIZER_INTERCEPT_GETGROUPS
1996 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
1998 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
1999 int res = REAL(getgroups)(size, lst);
2000 if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
2003 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
2005 #define INIT_GETGROUPS
2008 #if SANITIZER_INTERCEPT_POLL
2009 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
2010 __sanitizer_nfds_t nfds) {
2011 for (unsigned i = 0; i < nfds; ++i) {
2012 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
2013 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
2017 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
2018 __sanitizer_nfds_t nfds) {
2019 for (unsigned i = 0; i < nfds; ++i)
2020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
2021 sizeof(fds[i].revents));
2024 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2027 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
2028 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2029 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
2030 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2033 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
2038 #if SANITIZER_INTERCEPT_PPOLL
2039 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
2040 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
2042 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
2043 if (fds && nfds) read_pollfd(ctx, fds, nfds);
2045 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
2046 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
2048 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
2049 if (fds && nfds) write_pollfd(ctx, fds, nfds);
2052 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
2057 #if SANITIZER_INTERCEPT_WORDEXP
2058 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
2060 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
2061 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
2062 int res = REAL(wordexp)(s, p, flags);
2064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2066 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
2067 sizeof(*p->we_wordv) * p->we_wordc);
2068 for (uptr i = 0; i < p->we_wordc; ++i) {
2069 char *w = p->we_wordv[i];
2070 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
2075 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
2077 #define INIT_WORDEXP
2080 #if SANITIZER_INTERCEPT_SIGWAIT
2081 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
2083 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
2084 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2085 int res = REAL(sigwait)(set, sig);
2086 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
2089 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
2091 #define INIT_SIGWAIT
2094 #if SANITIZER_INTERCEPT_SIGWAITINFO
2095 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
2097 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
2098 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2099 int res = REAL(sigwaitinfo)(set, info);
2100 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2103 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
2105 #define INIT_SIGWAITINFO
2108 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
2109 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
2112 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
2113 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2114 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2115 int res = REAL(sigtimedwait)(set, info, timeout);
2116 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
2119 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
2121 #define INIT_SIGTIMEDWAIT
2124 #if SANITIZER_INTERCEPT_SIGSETOPS
2125 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
2127 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
2128 int res = REAL(sigemptyset)(set);
2129 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2133 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
2135 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
2136 int res = REAL(sigfillset)(set);
2137 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2140 #define INIT_SIGSETOPS \
2141 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
2142 COMMON_INTERCEPT_FUNCTION(sigfillset);
2144 #define INIT_SIGSETOPS
2147 #if SANITIZER_INTERCEPT_SIGPENDING
2148 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
2150 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
2151 int res = REAL(sigpending)(set);
2152 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
2155 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
2157 #define INIT_SIGPENDING
2160 #if SANITIZER_INTERCEPT_SIGPROCMASK
2161 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
2162 __sanitizer_sigset_t *oldset) {
2164 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
2165 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
2166 int res = REAL(sigprocmask)(how, set, oldset);
2168 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
2171 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
2173 #define INIT_SIGPROCMASK
2176 #if SANITIZER_INTERCEPT_BACKTRACE
2177 INTERCEPTOR(int, backtrace, void **buffer, int size) {
2179 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
2180 int res = REAL(backtrace)(buffer, size);
2182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
2186 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
2188 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
2190 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
2191 char **res = REAL(backtrace_symbols)(buffer, size);
2193 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
2194 for (int i = 0; i < size; ++i)
2195 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
2199 #define INIT_BACKTRACE \
2200 COMMON_INTERCEPT_FUNCTION(backtrace); \
2201 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
2203 #define INIT_BACKTRACE
2206 #if SANITIZER_INTERCEPT__EXIT
2207 INTERCEPTOR(void, _exit, int status) {
2209 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
2210 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
2211 if (status == 0) status = status1;
2212 REAL(_exit)(status);
2214 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
2219 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
2220 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
2222 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
2223 int res = REAL(pthread_mutex_lock)(m);
2224 if (res == errno_EOWNERDEAD)
2225 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
2226 if (res == 0 || res == errno_EOWNERDEAD)
2227 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2231 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
2233 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
2234 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2235 return REAL(pthread_mutex_unlock)(m);
2238 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
2239 #define INIT_PTHREAD_MUTEX_UNLOCK \
2240 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
2242 #define INIT_PTHREAD_MUTEX_LOCK
2243 #define INIT_PTHREAD_MUTEX_UNLOCK
2246 #if SANITIZER_INTERCEPT_PTHREAD_COND
2247 INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) {
2249 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_wait, c, m);
2250 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
2251 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, pthread_cond_t_sz);
2252 int res = REAL(pthread_cond_wait)(c, m);
2253 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
2257 INTERCEPTOR(int, pthread_cond_init, void *c, void *a) {
2259 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_init, c, a);
2260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, c, pthread_cond_t_sz);
2261 return REAL(pthread_cond_init)(c, a);
2264 INTERCEPTOR(int, pthread_cond_signal, void *c) {
2266 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_signal, c);
2267 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, pthread_cond_t_sz);
2268 return REAL(pthread_cond_signal)(c);
2271 INTERCEPTOR(int, pthread_cond_broadcast, void *c) {
2273 COMMON_INTERCEPTOR_ENTER(ctx, pthread_cond_broadcast, c);
2274 COMMON_INTERCEPTOR_READ_RANGE(ctx, c, pthread_cond_t_sz);
2275 return REAL(pthread_cond_broadcast)(c);
2278 #define INIT_PTHREAD_COND_WAIT \
2279 INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2")
2280 #define INIT_PTHREAD_COND_INIT \
2281 INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2")
2282 #define INIT_PTHREAD_COND_SIGNAL \
2283 INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2")
2284 #define INIT_PTHREAD_COND_BROADCAST \
2285 INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2")
2287 #define INIT_PTHREAD_COND_WAIT
2288 #define INIT_PTHREAD_COND_INIT
2289 #define INIT_PTHREAD_COND_SIGNAL
2290 #define INIT_PTHREAD_COND_BROADCAST
2293 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
2294 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
2295 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
2296 if (mnt->mnt_fsname)
2297 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
2298 REAL(strlen)(mnt->mnt_fsname) + 1);
2300 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
2301 REAL(strlen)(mnt->mnt_dir) + 1);
2303 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
2304 REAL(strlen)(mnt->mnt_type) + 1);
2306 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
2307 REAL(strlen)(mnt->mnt_opts) + 1);
2311 #if SANITIZER_INTERCEPT_GETMNTENT
2312 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
2314 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
2315 __sanitizer_mntent *res = REAL(getmntent)(fp);
2316 if (res) write_mntent(ctx, res);
2319 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
2321 #define INIT_GETMNTENT
2324 #if SANITIZER_INTERCEPT_GETMNTENT_R
2325 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
2326 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
2328 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
2329 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
2330 if (res) write_mntent(ctx, res);
2333 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
2335 #define INIT_GETMNTENT_R
2338 #if SANITIZER_INTERCEPT_STATFS
2339 INTERCEPTOR(int, statfs, char *path, void *buf) {
2341 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
2342 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2343 int res = REAL(statfs)(path, buf);
2344 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
2347 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
2349 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
2350 int res = REAL(fstatfs)(fd, buf);
2351 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
2354 #define INIT_STATFS \
2355 COMMON_INTERCEPT_FUNCTION(statfs); \
2356 COMMON_INTERCEPT_FUNCTION(fstatfs);
2361 #if SANITIZER_INTERCEPT_STATFS64
2362 INTERCEPTOR(int, statfs64, char *path, void *buf) {
2364 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
2365 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2366 int res = REAL(statfs64)(path, buf);
2367 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
2370 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
2372 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
2373 int res = REAL(fstatfs64)(fd, buf);
2374 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
2377 #define INIT_STATFS64 \
2378 COMMON_INTERCEPT_FUNCTION(statfs64); \
2379 COMMON_INTERCEPT_FUNCTION(fstatfs64);
2381 #define INIT_STATFS64
2384 #if SANITIZER_INTERCEPT_STATVFS
2385 INTERCEPTOR(int, statvfs, char *path, void *buf) {
2387 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
2388 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2389 int res = REAL(statvfs)(path, buf);
2390 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
2393 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
2395 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
2396 int res = REAL(fstatvfs)(fd, buf);
2397 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
2400 #define INIT_STATVFS \
2401 COMMON_INTERCEPT_FUNCTION(statvfs); \
2402 COMMON_INTERCEPT_FUNCTION(fstatvfs);
2404 #define INIT_STATVFS
2407 #if SANITIZER_INTERCEPT_STATVFS64
2408 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
2410 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
2411 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2412 int res = REAL(statvfs64)(path, buf);
2413 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
2416 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
2418 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
2419 int res = REAL(fstatvfs64)(fd, buf);
2420 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
2423 #define INIT_STATVFS64 \
2424 COMMON_INTERCEPT_FUNCTION(statvfs64); \
2425 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
2427 #define INIT_STATVFS64
2430 #if SANITIZER_INTERCEPT_INITGROUPS
2431 INTERCEPTOR(int, initgroups, char *user, u32 group) {
2433 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
2434 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
2435 int res = REAL(initgroups)(user, group);
2438 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
2440 #define INIT_INITGROUPS
2443 #if SANITIZER_INTERCEPT_ETHER
2444 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
2446 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
2447 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2448 char *res = REAL(ether_ntoa)(addr);
2449 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2452 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
2454 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
2455 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2456 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
2457 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, sizeof(*res));
2460 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
2462 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
2463 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2464 int res = REAL(ether_ntohost)(hostname, addr);
2465 if (!res && hostname)
2466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2469 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
2471 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
2473 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2474 int res = REAL(ether_hostton)(hostname, addr);
2475 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2478 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
2481 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
2482 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
2483 int res = REAL(ether_line)(line, addr, hostname);
2485 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2487 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
2491 #define INIT_ETHER \
2492 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
2493 COMMON_INTERCEPT_FUNCTION(ether_aton); \
2494 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
2495 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
2496 COMMON_INTERCEPT_FUNCTION(ether_line);
2501 #if SANITIZER_INTERCEPT_ETHER_R
2502 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
2504 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
2505 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
2506 char *res = REAL(ether_ntoa_r)(addr, buf);
2507 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2510 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
2511 __sanitizer_ether_addr *addr) {
2513 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
2514 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
2515 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
2516 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
2519 #define INIT_ETHER_R \
2520 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
2521 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
2523 #define INIT_ETHER_R
2526 #if SANITIZER_INTERCEPT_SHMCTL
2527 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
2529 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
2530 int res = REAL(shmctl)(shmid, cmd, buf);
2533 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
2534 sz = sizeof(__sanitizer_shmid_ds);
2535 else if (cmd == shmctl_ipc_info)
2536 sz = struct_shminfo_sz;
2537 else if (cmd == shmctl_shm_info)
2538 sz = struct_shm_info_sz;
2539 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
2543 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
2548 #if SANITIZER_INTERCEPT_RANDOM_R
2549 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
2551 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
2552 int res = REAL(random_r)(buf, result);
2554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2557 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
2559 #define INIT_RANDOM_R
2562 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
2563 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED
2564 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
2565 INTERCEPTOR(int, pthread_attr_get##what, void *attr, void *r) { \
2567 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_get##what, attr, r); \
2568 int res = REAL(pthread_attr_get##what)(attr, r); \
2569 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
2574 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
2575 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
2576 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
2577 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
2578 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
2579 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
2580 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
2581 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
2583 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
2584 int res = REAL(pthread_attr_getstack)(attr, addr, size);
2586 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
2587 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
2592 // We may need to call the real pthread_attr_getstack from the run-time
2593 // in sanitizer_common, but we don't want to include the interception headers
2594 // there. So, just define this function here.
2595 int __sanitizer_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
2596 return REAL(pthread_attr_getstack)(attr, addr, size);
2599 #define INIT_PTHREAD_ATTR_GET \
2600 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
2601 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
2602 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
2603 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
2604 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
2605 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
2606 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
2608 #define INIT_PTHREAD_ATTR_GET
2611 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
2612 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
2614 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
2615 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
2617 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
2620 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
2621 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
2624 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
2626 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
2627 if (!res && cpusetsize && cpuset)
2628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
2632 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
2633 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
2635 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
2638 #if SANITIZER_INTERCEPT_TMPNAM
2639 INTERCEPTOR(char *, tmpnam, char *s) {
2641 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
2642 char *res = REAL(tmpnam)(s);
2645 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
2647 COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2651 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
2656 #if SANITIZER_INTERCEPT_TMPNAM_R
2657 INTERCEPTOR(char *, tmpnam_r, char *s) {
2659 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
2660 char *res = REAL(tmpnam_r)(s);
2661 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
2664 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
2666 #define INIT_TMPNAM_R
2669 #if SANITIZER_INTERCEPT_TEMPNAM
2670 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
2672 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
2673 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
2674 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
2675 char *res = REAL(tempnam)(dir, pfx);
2676 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2679 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
2681 #define INIT_TEMPNAM
2684 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
2685 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
2687 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
2688 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
2689 return REAL(pthread_setname_np)(thread, name);
2691 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
2693 #define INIT_PTHREAD_SETNAME_NP
2696 #if SANITIZER_INTERCEPT_SINCOS
2697 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
2699 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
2700 REAL(sincos)(x, sin, cos);
2701 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2702 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2704 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
2706 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
2707 REAL(sincosf)(x, sin, cos);
2708 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2709 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2711 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
2713 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
2714 REAL(sincosl)(x, sin, cos);
2715 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
2716 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
2718 #define INIT_SINCOS \
2719 COMMON_INTERCEPT_FUNCTION(sincos); \
2720 COMMON_INTERCEPT_FUNCTION(sincosf); \
2721 COMMON_INTERCEPT_FUNCTION(sincosl);
2726 #if SANITIZER_INTERCEPT_REMQUO
2727 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
2729 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
2730 double res = REAL(remquo)(x, y, quo);
2731 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2734 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
2736 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
2737 float res = REAL(remquof)(x, y, quo);
2738 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2741 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
2743 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
2744 long double res = REAL(remquol)(x, y, quo);
2745 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
2748 #define INIT_REMQUO \
2749 COMMON_INTERCEPT_FUNCTION(remquo); \
2750 COMMON_INTERCEPT_FUNCTION(remquof); \
2751 COMMON_INTERCEPT_FUNCTION(remquol);
2756 #if SANITIZER_INTERCEPT_LGAMMA
2758 INTERCEPTOR(double, lgamma, double x) {
2760 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
2761 double res = REAL(lgamma)(x);
2762 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2765 INTERCEPTOR(float, lgammaf, float x) {
2767 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
2768 float res = REAL(lgammaf)(x);
2769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2772 INTERCEPTOR(long double, lgammal, long double x) {
2774 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
2775 long double res = REAL(lgammal)(x);
2776 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
2779 #define INIT_LGAMMA \
2780 COMMON_INTERCEPT_FUNCTION(lgamma); \
2781 COMMON_INTERCEPT_FUNCTION(lgammaf); \
2782 COMMON_INTERCEPT_FUNCTION(lgammal);
2787 #if SANITIZER_INTERCEPT_LGAMMA_R
2788 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
2790 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
2791 double res = REAL(lgamma_r)(x, signp);
2792 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2795 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
2797 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
2798 float res = REAL(lgammaf_r)(x, signp);
2799 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2802 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
2804 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
2805 long double res = REAL(lgammal_r)(x, signp);
2806 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
2809 #define INIT_LGAMMA_R \
2810 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
2811 COMMON_INTERCEPT_FUNCTION(lgammaf_r); \
2812 COMMON_INTERCEPT_FUNCTION(lgammal_r);
2814 #define INIT_LGAMMA_R
2817 #if SANITIZER_INTERCEPT_DRAND48_R
2818 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
2820 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
2821 int res = REAL(drand48_r)(buffer, result);
2822 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2825 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
2827 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
2828 int res = REAL(lrand48_r)(buffer, result);
2829 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2832 #define INIT_DRAND48_R \
2833 COMMON_INTERCEPT_FUNCTION(drand48_r); \
2834 COMMON_INTERCEPT_FUNCTION(lrand48_r);
2836 #define INIT_DRAND48_R
2839 #if SANITIZER_INTERCEPT_GETLINE
2840 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
2842 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
2843 SSIZE_T res = REAL(getline)(lineptr, n, stream);
2845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
2846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
2847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
2851 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
2854 COMMON_INTERCEPTOR_ENTER(ctx, getdelim, lineptr, n, delim, stream);
2855 SSIZE_T res = REAL(getdelim)(lineptr, n, delim, stream);
2857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
2858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
2859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
2863 #define INIT_GETLINE \
2864 COMMON_INTERCEPT_FUNCTION(getline); \
2865 COMMON_INTERCEPT_FUNCTION(getdelim);
2867 #define INIT_GETLINE
2870 #if SANITIZER_INTERCEPT_ICONV
2871 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
2872 char **outbuf, SIZE_T *outbytesleft) {
2874 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
2877 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
2878 if (inbuf && inbytesleft)
2879 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
2881 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
2882 void *outbuf_orig = outbuf ? *outbuf : 0;
2883 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
2884 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
2885 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
2886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
2890 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
2895 #if SANITIZER_INTERCEPT_TIMES
2896 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
2898 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
2899 __sanitizer_clock_t res = REAL(times)(tms);
2900 if (res != (__sanitizer_clock_t)-1 && tms)
2901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
2904 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
2909 #define SANITIZER_COMMON_INTERCEPTORS_INIT \
2928 INIT_LOCALTIME_AND_FRIENDS; \
2931 INIT_ISOC99_SCANF; \
2933 INIT_FREXPF_FREXPL; \
2934 INIT_GETPWNAM_AND_FRIENDS; \
2935 INIT_GETPWNAM_R_AND_FRIENDS; \
2936 INIT_CLOCK_GETTIME; \
2942 INIT_PTHREAD_GETSCHEDPARAM; \
2946 INIT_GETHOSTBYNAME; \
2947 INIT_GETHOSTBYNAME_R; \
2962 INIT_GET_CURRENT_DIR_NAME; \
2970 INIT_CANONICALIZE_FILE_NAME; \
2972 INIT_SCHED_GETAFFINITY; \
2975 INIT_XPG_STRERROR_R; \
2984 INIT_SIGTIMEDWAIT; \
2990 INIT_PTHREAD_MUTEX_LOCK; \
2991 INIT_PTHREAD_MUTEX_UNLOCK; \
2992 INIT_PTHREAD_COND_WAIT; \
2993 INIT_PTHREAD_COND_INIT; \
2994 INIT_PTHREAD_COND_SIGNAL; \
2995 INIT_PTHREAD_COND_BROADCAST; \
3007 INIT_PTHREAD_ATTR_GET; \
3008 INIT_PTHREAD_ATTR_GETINHERITSCHED; \
3009 INIT_PTHREAD_ATTR_GETAFFINITY_NP; \
3013 INIT_PTHREAD_SETNAME_NP; \