1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Common function interceptors for tools like AddressSanitizer,
10 // ThreadSanitizer, MemorySanitizer, etc.
12 // This file should be included into the tool's interceptor file,
13 // which has to define its own macros:
14 // COMMON_INTERCEPTOR_ENTER
15 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
16 // COMMON_INTERCEPTOR_READ_RANGE
17 // COMMON_INTERCEPTOR_WRITE_RANGE
18 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
19 // COMMON_INTERCEPTOR_DIR_ACQUIRE
20 // COMMON_INTERCEPTOR_FD_ACQUIRE
21 // COMMON_INTERCEPTOR_FD_RELEASE
22 // COMMON_INTERCEPTOR_FD_ACCESS
23 // COMMON_INTERCEPTOR_SET_THREAD_NAME
24 // COMMON_INTERCEPTOR_DLOPEN
25 // COMMON_INTERCEPTOR_ON_EXIT
26 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
27 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
28 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
29 // COMMON_INTERCEPTOR_MMAP_IMPL
30 // COMMON_INTERCEPTOR_MUNMAP_IMPL
31 // COMMON_INTERCEPTOR_COPY_STRING
32 // COMMON_INTERCEPTOR_STRNDUP_IMPL
33 // COMMON_INTERCEPTOR_STRERROR
34 //===----------------------------------------------------------------------===//
38 #include "interception/interception.h"
39 #include "sanitizer_addrhashmap.h"
40 #include "sanitizer_dl.h"
41 #include "sanitizer_errno.h"
42 #include "sanitizer_placement_new.h"
43 #include "sanitizer_platform_interceptors.h"
44 #include "sanitizer_platform_limits_posix.h"
45 #include "sanitizer_symbolizer.h"
46 #include "sanitizer_tls_get_addr.h"
48 #if SANITIZER_INTERCEPTOR_HOOKS
49 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
50 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
51 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
53 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
54 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
56 #endif // SANITIZER_INTERCEPTOR_HOOKS
58 #if SANITIZER_WINDOWS && !defined(va_copy)
59 #define va_copy(dst, src) ((dst) = (src))
63 #define pthread_setname_np pthread_set_name_np
64 #define inet_aton __inet_aton
65 #define inet_pton __inet_pton
66 #define iconv __bsd_iconv
70 #define clock_getres __clock_getres50
71 #define clock_gettime __clock_gettime50
72 #define clock_settime __clock_settime50
73 #define ctime __ctime50
74 #define ctime_r __ctime_r50
75 #define devname __devname50
76 #define fgetpos __fgetpos50
77 #define fsetpos __fsetpos50
78 #define fstatvfs __fstatvfs90
79 #define fstatvfs1 __fstatvfs190
80 #define fts_children __fts_children60
81 #define fts_close __fts_close60
82 #define fts_open __fts_open60
83 #define fts_read __fts_read60
84 #define fts_set __fts_set60
85 #define getitimer __getitimer50
86 #define getmntinfo __getmntinfo90
87 #define getpwent __getpwent50
88 #define getpwnam __getpwnam50
89 #define getpwnam_r __getpwnam_r50
90 #define getpwuid __getpwuid50
91 #define getpwuid_r __getpwuid_r50
92 #define getutent __getutent50
93 #define getutxent __getutxent50
94 #define getutxid __getutxid50
95 #define getutxline __getutxline50
96 #define getvfsstat __getvfsstat90
97 #define pututxline __pututxline50
99 #define gmtime __gmtime50
100 #define gmtime_r __gmtime_r50
101 #define localtime __locatime50
102 #define localtime_r __localtime_r50
103 #define mktime __mktime50
104 #define lstat __lstat50
105 #define opendir __opendir30
106 #define readdir __readdir30
107 #define readdir_r __readdir_r30
108 #define scandir __scandir30
109 #define setitimer __setitimer50
110 #define setlocale __setlocale50
111 #define shmctl __shmctl50
112 #define sigaltstack __sigaltstack14
113 #define sigemptyset __sigemptyset14
114 #define sigfillset __sigfillset14
115 #define sigpending __sigpending14
116 #define sigprocmask __sigprocmask14
117 #define sigtimedwait __sigtimedwait50
118 #define stat __stat50
119 #define statvfs __statvfs90
120 #define statvfs1 __statvfs190
121 #define time __time50
122 #define times __times13
123 #define unvis __unvis50
124 #define wait3 __wait350
125 #define wait4 __wait450
126 extern const unsigned short *_ctype_tab_;
127 extern const short *_toupper_tab_;
128 extern const short *_tolower_tab_;
131 #if SANITIZER_LINUX && SANITIZER_SPARC32
132 // On 32-bit Linux/sparc64, double and long double are identical and glibc
133 // uses a __nldbl_ (no long double) prefix for various stdio functions.
134 # define __isoc23_fscanf __nldbl___isoc23_fscanf
135 # define __isoc23_scanf __nldbl___isoc23_scanf
136 # define __isoc23_sscanf __nldbl___isoc23_sscanf
137 # define __isoc23_vfscanf __nldbl___isoc23_vfscanf
138 # define __isoc23_vscanf __nldbl___isoc23_vscanf
139 # define __isoc23_vsscanf __nldbl___isoc23_vsscanf
140 # define __isoc99_fscanf __nldbl___isoc99_fscanf
141 # define __isoc99_scanf __nldbl___isoc99_scanf
142 # define __isoc99_sscanf __nldbl___isoc99_sscanf
143 # define __isoc99_vfscanf __nldbl___isoc99_vfscanf
144 # define __isoc99_vscanf __nldbl___isoc99_vscanf
145 # define __isoc99_vsscanf __nldbl___isoc99_vsscanf
146 # define asprintf __nldbl_asprintf
147 # define fprintf __nldbl_fprintf
148 # define fscanf __nldbl_fscanf
149 # define printf __nldbl_printf
150 # define scanf __nldbl_scanf
151 # define snprintf __nldbl_snprintf
152 # define sprintf __nldbl_sprintf
153 # define sscanf __nldbl_sscanf
154 # define vasprintf __nldbl_vasprintf
155 # define vfprintf __nldbl_vfprintf
156 # define vfscanf __nldbl_vfscanf
157 # define vprintf __nldbl_vprintf
158 # define vscanf __nldbl_vscanf
159 # define vsnprintf __nldbl_vsnprintf
160 # define vsprintf __nldbl_vsprintf
161 # define vsscanf __nldbl_vsscanf
164 #if SANITIZER_MUSL && \
165 (defined(__i386__) || defined(__arm__) || SANITIZER_MIPS32 || SANITIZER_PPC32)
166 // musl 1.2.0 on existing 32-bit architectures uses new symbol names for the
167 // time-related functions that take 64-bit time_t values. See
168 // https://musl.libc.org/time64.html
169 #define adjtime __adjtime64
170 #define adjtimex __adjtimex_time64
171 #define aio_suspend __aio_suspend_time64
172 #define clock_adjtime __clock_adjtime64
173 #define clock_getres __clock_getres_time64
174 #define clock_gettime __clock_gettime64
175 #define clock_nanosleep __clock_nanosleep_time64
176 #define clock_settime __clock_settime64
177 #define cnd_timedwait __cnd_timedwait_time64
178 #define ctime __ctime64
179 #define ctime_r __ctime64_r
180 #define difftime __difftime64
181 #define dlsym __dlsym_time64
182 #define fstatat __fstatat_time64
183 #define fstat __fstat_time64
184 #define ftime __ftime64
185 #define futimens __futimens_time64
186 #define futimesat __futimesat_time64
187 #define futimes __futimes_time64
188 #define getitimer __getitimer_time64
189 #define getrusage __getrusage_time64
190 #define gettimeofday __gettimeofday_time64
191 #define gmtime __gmtime64
192 #define gmtime_r __gmtime64_r
193 #define localtime __localtime64
194 #define localtime_r __localtime64_r
195 #define lstat __lstat_time64
196 #define lutimes __lutimes_time64
197 #define mktime __mktime64
198 #define mq_timedreceive __mq_timedreceive_time64
199 #define mq_timedsend __mq_timedsend_time64
200 #define mtx_timedlock __mtx_timedlock_time64
201 #define nanosleep __nanosleep_time64
202 #define ppoll __ppoll_time64
203 #define pselect __pselect_time64
204 #define pthread_cond_timedwait __pthread_cond_timedwait_time64
205 #define pthread_mutex_timedlock __pthread_mutex_timedlock_time64
206 #define pthread_rwlock_timedrdlock __pthread_rwlock_timedrdlock_time64
207 #define pthread_rwlock_timedwrlock __pthread_rwlock_timedwrlock_time64
208 #define pthread_timedjoin_np __pthread_timedjoin_np_time64
209 #define recvmmsg __recvmmsg_time64
210 #define sched_rr_get_interval __sched_rr_get_interval_time64
211 #define select __select_time64
212 #define semtimedop __semtimedop_time64
213 #define sem_timedwait __sem_timedwait_time64
214 #define setitimer __setitimer_time64
215 #define settimeofday __settimeofday_time64
216 #define sigtimedwait __sigtimedwait_time64
217 #define stat __stat_time64
218 #define stime __stime64
219 #define thrd_sleep __thrd_sleep_time64
220 #define timegm __timegm_time64
221 #define timerfd_gettime __timerfd_gettime64
222 #define timerfd_settime __timerfd_settime64
223 #define timer_gettime __timer_gettime64
224 #define timer_settime __timer_settime64
225 #define timespec_get __timespec_get_time64
226 #define time __time64
227 #define utimensat __utimensat_time64
228 #define utimes __utimes_time64
229 #define utime __utime64
230 #define wait3 __wait3_time64
231 #define wait4 __wait4_time64
234 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
235 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
238 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
239 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
242 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
243 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
246 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
247 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
250 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
251 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
254 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
255 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
258 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
259 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
262 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
263 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
266 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
267 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
268 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
271 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
272 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
275 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
276 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
277 common_flags()->strict_string_checks ? (internal_strlen(s)) + 1 : (n) )
279 #ifndef COMMON_INTERCEPTOR_DLOPEN
280 #define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
281 ({ CheckNoDeepBind(filename, flag); REAL(dlopen)(filename, flag); })
284 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
285 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
288 #ifndef COMMON_INTERCEPTOR_ACQUIRE
289 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
292 #ifndef COMMON_INTERCEPTOR_RELEASE
293 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
296 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
297 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
300 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
301 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
304 #ifdef SANITIZER_NLDBL_VERSION
305 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
306 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
308 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
309 COMMON_INTERCEPT_FUNCTION(fn)
313 // If we could not find the versioned symbol, fall back to an unversioned
314 // lookup. This is needed to work around a GLibc bug that causes dlsym
315 // with RTLD_NEXT to return the oldest versioned symbol.
316 // See https://sourceware.org/bugzilla/show_bug.cgi?id=14932.
317 // For certain symbols (e.g. regexec) we have to perform a versioned lookup,
318 // but that versioned symbol will only exist for architectures where the
319 // oldest Glibc version pre-dates support for that architecture.
320 // For example, regexec@GLIBC_2.3.4 exists on x86_64, but not RISC-V.
321 // See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98920.
322 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
323 COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(fn, ver)
325 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
326 COMMON_INTERCEPT_FUNCTION(fn)
329 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
330 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
332 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
335 #ifndef COMMON_INTERCEPTOR_MUNMAP_IMPL
336 #define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz) \
337 { return REAL(munmap)(addr, sz); }
340 #ifndef COMMON_INTERCEPTOR_COPY_STRING
341 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
344 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
345 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
346 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
347 uptr copy_length = internal_strnlen(s, size); \
348 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
349 if (common_flags()->intercept_strndup) { \
350 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
353 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
354 internal_memcpy(new_mem, s, copy_length); \
355 new_mem[copy_length] = '\0'; \
360 #ifndef COMMON_INTERCEPTOR_STRERROR
361 #define COMMON_INTERCEPTOR_STRERROR() {}
364 struct FileMetadata {
365 // For open_memstream().
370 struct CommonInterceptorMetadata {
381 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
383 static MetadataHashMap *interceptor_metadata_map;
385 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
386 const FileMetadata &file) {
387 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
389 h->type = CommonInterceptorMetadata::CIMT_FILE;
393 UNUSED static const FileMetadata *GetInterceptorMetadata(
394 __sanitizer_FILE *addr) {
395 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
398 if (addr && h.exists()) {
400 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
407 UNUSED static void DeleteInterceptorMetadata(void *addr) {
408 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
413 #if SANITIZER_INTERCEPT_STRLEN
414 INTERCEPTOR(SIZE_T, strlen, const char *s) {
415 // Sometimes strlen is called prior to InitializeCommonInterceptors,
416 // in which case the REAL(strlen) typically used in
417 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
419 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
420 return internal_strlen(s);
422 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
423 SIZE_T result = REAL(strlen)(s);
424 if (common_flags()->intercept_strlen)
425 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
428 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
433 #if SANITIZER_INTERCEPT_STRNLEN
434 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
436 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
437 SIZE_T length = REAL(strnlen)(s, maxlen);
438 if (common_flags()->intercept_strlen)
439 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
442 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
447 #if SANITIZER_INTERCEPT_STRNDUP
448 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
450 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
452 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
455 #endif // SANITIZER_INTERCEPT_STRNDUP
457 #if SANITIZER_INTERCEPT___STRNDUP
458 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
460 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
462 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
464 #define INIT___STRNDUP
465 #endif // SANITIZER_INTERCEPT___STRNDUP
467 #if SANITIZER_INTERCEPT_TEXTDOMAIN
468 INTERCEPTOR(char*, textdomain, const char *domainname) {
470 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
471 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
472 char *domain = REAL(textdomain)(domainname);
474 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, internal_strlen(domain) + 1);
478 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
480 #define INIT_TEXTDOMAIN
483 #if SANITIZER_INTERCEPT_STRCMP || SANITIZER_INTERCEPT_MEMCMP
484 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
485 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
489 #if SANITIZER_INTERCEPT_STRCMP
490 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
491 const char *s1, const char *s2, int result)
493 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
495 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
496 unsigned char c1, c2;
499 c1 = (unsigned char)s1[i];
500 c2 = (unsigned char)s2[i];
501 if (c1 != c2 || c1 == '\0') break;
503 if (common_flags()->intercept_strcmp) {
504 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
505 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
507 int result = CharCmpX(c1, c2);
508 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
513 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
514 const char *s1, const char *s2, uptr n,
517 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
518 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
519 return internal_strncmp(s1, s2, size);
521 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
522 unsigned char c1 = 0, c2 = 0;
524 for (i = 0; i < size; i++) {
525 c1 = (unsigned char)s1[i];
526 c2 = (unsigned char)s2[i];
527 if (c1 != c2 || c1 == '\0') break;
531 if (common_flags()->strict_string_checks) {
532 for (; i1 < size && s1[i1]; i1++) {}
533 for (; i2 < size && s2[i2]; i2++) {}
535 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
536 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
537 int result = CharCmpX(c1, c2);
538 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
543 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
544 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
550 #if SANITIZER_INTERCEPT_STRCASECMP
551 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
552 int c1_low = ToLower(c1);
553 int c2_low = ToLower(c2);
554 return c1_low - c2_low;
557 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
558 const char *s1, const char *s2, int result)
560 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
562 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
563 unsigned char c1 = 0, c2 = 0;
566 c1 = (unsigned char)s1[i];
567 c2 = (unsigned char)s2[i];
568 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
570 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
571 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
572 int result = CharCaseCmp(c1, c2);
573 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
578 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
579 const char *s1, const char *s2, uptr size,
582 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
584 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
585 unsigned char c1 = 0, c2 = 0;
587 for (i = 0; i < size; i++) {
588 c1 = (unsigned char)s1[i];
589 c2 = (unsigned char)s2[i];
590 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
594 if (common_flags()->strict_string_checks) {
595 for (; i1 < size && s1[i1]; i1++) {}
596 for (; i2 < size && s2[i2]; i2++) {}
598 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
599 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
600 int result = CharCaseCmp(c1, c2);
601 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
602 s1, s2, size, result);
606 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
607 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
609 #define INIT_STRCASECMP
610 #define INIT_STRNCASECMP
613 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
614 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
616 uptr len1 = internal_strlen(s1);
617 uptr len2 = internal_strlen(s2);
618 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
619 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
623 #if SANITIZER_INTERCEPT_STRSTR
625 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
626 const char *s1, const char *s2, char *result)
628 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
629 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
630 return internal_strstr(s1, s2);
632 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
633 char *r = REAL(strstr)(s1, s2);
634 if (common_flags()->intercept_strstr)
635 StrstrCheck(ctx, r, s1, s2);
636 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
641 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
646 #if SANITIZER_INTERCEPT_STRCASESTR
648 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
649 const char *s1, const char *s2, char *result)
651 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
653 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
654 char *r = REAL(strcasestr)(s1, s2);
655 if (common_flags()->intercept_strstr)
656 StrstrCheck(ctx, r, s1, s2);
657 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
662 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
664 #define INIT_STRCASESTR
667 #if SANITIZER_INTERCEPT_STRTOK
669 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
671 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
672 if (!common_flags()->intercept_strtok) {
673 return REAL(strtok)(str, delimiters);
675 if (common_flags()->strict_string_checks) {
676 // If strict_string_checks is enabled, we check the whole first argument
677 // string on the first call (strtok saves this string in a static buffer
678 // for subsequent calls). We do not need to check strtok's result.
679 // As the delimiters can change, we check them every call.
680 if (str != nullptr) {
681 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
683 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
684 internal_strlen(delimiters) + 1);
685 return REAL(strtok)(str, delimiters);
687 // However, when strict_string_checks is disabled we cannot check the
688 // whole string on the first call. Instead, we check the result string
689 // which is guaranteed to be a NULL-terminated substring of the first
690 // argument. We also conservatively check one character of str and the
692 if (str != nullptr) {
693 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
695 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
696 char *result = REAL(strtok)(str, delimiters);
697 if (result != nullptr) {
698 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, internal_strlen(result) + 1);
699 } else if (str != nullptr) {
700 // No delimiter were found, it's safe to assume that the entire str was
702 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
708 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
713 #if SANITIZER_INTERCEPT_MEMMEM
714 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
715 const void *s1, SIZE_T len1, const void *s2,
716 SIZE_T len2, void *result)
718 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
721 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
722 void *r = REAL(memmem)(s1, len1, s2, len2);
723 if (common_flags()->intercept_memmem) {
724 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
725 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
727 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
728 s1, len1, s2, len2, r);
732 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
735 #endif // SANITIZER_INTERCEPT_MEMMEM
737 #if SANITIZER_INTERCEPT_STRCHR
738 INTERCEPTOR(char*, strchr, const char *s, int c) {
740 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
741 return internal_strchr(s, c);
742 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
743 char *result = REAL(strchr)(s, c);
744 if (common_flags()->intercept_strchr) {
745 // Keep strlen as macro argument, as macro may ignore it.
746 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
747 (result ? result - s : internal_strlen(s)) + 1);
751 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
756 #if SANITIZER_INTERCEPT_STRCHRNUL
757 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
759 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
760 char *result = REAL(strchrnul)(s, c);
761 uptr len = result - s + 1;
762 if (common_flags()->intercept_strchr)
763 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
766 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
768 #define INIT_STRCHRNUL
771 #if SANITIZER_INTERCEPT_STRRCHR
772 INTERCEPTOR(char*, strrchr, const char *s, int c) {
774 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
775 return internal_strrchr(s, c);
776 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
777 if (common_flags()->intercept_strchr)
778 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
779 return REAL(strrchr)(s, c);
781 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
786 #if SANITIZER_INTERCEPT_STRSPN
787 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
789 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
790 SIZE_T r = REAL(strspn)(s1, s2);
791 if (common_flags()->intercept_strspn) {
792 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
793 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
798 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
800 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
801 SIZE_T r = REAL(strcspn)(s1, s2);
802 if (common_flags()->intercept_strspn) {
803 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
804 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
809 #define INIT_STRSPN \
810 COMMON_INTERCEPT_FUNCTION(strspn); \
811 COMMON_INTERCEPT_FUNCTION(strcspn);
816 #if SANITIZER_INTERCEPT_STRPBRK
817 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
819 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
820 char *r = REAL(strpbrk)(s1, s2);
821 if (common_flags()->intercept_strpbrk) {
822 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
823 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
824 r ? r - s1 + 1 : internal_strlen(s1) + 1);
829 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
834 #if SANITIZER_INTERCEPT_MEMCMP
835 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
836 const void *s1, const void *s2, uptr n,
839 // Common code for `memcmp` and `bcmp`.
840 int MemcmpInterceptorCommon(void *ctx,
841 int (*real_fn)(const void *, const void *, uptr),
842 const void *a1, const void *a2, uptr size) {
843 if (common_flags()->intercept_memcmp) {
844 if (common_flags()->strict_memcmp) {
845 // Check the entire regions even if the first bytes of the buffers are
847 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
848 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
849 // Fallthrough to REAL(memcmp) below.
851 unsigned char c1 = 0, c2 = 0;
852 const unsigned char *s1 = (const unsigned char*)a1;
853 const unsigned char *s2 = (const unsigned char*)a2;
855 for (i = 0; i < size; i++) {
860 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
861 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
862 int r = CharCmpX(c1, c2);
863 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
868 int result = real_fn(a1, a2, size);
869 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
874 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
875 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
876 return internal_memcmp(a1, a2, size);
878 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
879 return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
882 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
887 #if SANITIZER_INTERCEPT_BCMP
888 INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {
889 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
890 return internal_memcmp(a1, a2, size);
892 COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
893 return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
896 #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
901 #if SANITIZER_INTERCEPT_MEMCHR
902 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
903 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
904 return internal_memchr(s, c, n);
906 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
907 #if SANITIZER_WINDOWS
910 res = REAL(memchr)(s, c, n);
912 res = internal_memchr(s, c, n);
915 void *res = REAL(memchr)(s, c, n);
917 uptr len = res ? (char *)res - (const char *)s + 1 : n;
918 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
922 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
927 #if SANITIZER_INTERCEPT_MEMRCHR
928 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
930 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
931 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
932 return REAL(memrchr)(s, c, n);
935 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
940 #if SANITIZER_INTERCEPT_FREXP
941 INTERCEPTOR(double, frexp, double x, int *exp) {
943 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
944 // Assuming frexp() always writes to |exp|.
945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
946 double res = REAL(frexp)(x, exp);
947 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
951 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
954 #endif // SANITIZER_INTERCEPT_FREXP
956 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
957 INTERCEPTOR(float, frexpf, float x, int *exp) {
959 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
961 float res = REAL(frexpf)(x, exp);
962 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
966 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
968 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
969 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
970 long double res = REAL(frexpl)(x, exp);
971 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
975 #define INIT_FREXPF_FREXPL \
976 COMMON_INTERCEPT_FUNCTION(frexpf); \
977 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
979 #define INIT_FREXPF_FREXPL
980 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
983 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
984 SIZE_T iovlen, SIZE_T maxlen) {
985 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
986 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
987 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
992 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
993 SIZE_T iovlen, SIZE_T maxlen) {
994 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
995 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
996 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
997 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
1003 #if SANITIZER_INTERCEPT_READ
1004 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
1006 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
1007 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1008 // FIXME: under ASan the call below may write to freed memory and corrupt
1009 // its metadata. See
1010 // https://github.com/google/sanitizers/issues/321.
1011 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(read)(fd, ptr, count);
1012 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1013 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1016 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
1021 #if SANITIZER_INTERCEPT_FREAD
1022 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
1023 // libc file streams can call user-supplied functions, see fopencookie.
1025 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
1026 // FIXME: under ASan the call below may write to freed memory and corrupt
1027 // its metadata. See
1028 // https://github.com/google/sanitizers/issues/321.
1029 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
1030 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
1033 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1038 #if SANITIZER_INTERCEPT_PREAD
1039 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1041 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1042 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1043 // FIXME: under ASan the call below may write to freed memory and corrupt
1044 // its metadata. See
1045 // https://github.com/google/sanitizers/issues/321.
1046 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pread)(fd, ptr, count, offset);
1047 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1048 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1051 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1056 #if SANITIZER_INTERCEPT_PREAD64
1057 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1059 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1060 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1061 // FIXME: under ASan the call below may write to freed memory and corrupt
1062 // its metadata. See
1063 // https://github.com/google/sanitizers/issues/321.
1064 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pread64)(fd, ptr, count, offset);
1065 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1066 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1069 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1071 #define INIT_PREAD64
1074 #if SANITIZER_INTERCEPT_READV
1075 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1078 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1079 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1080 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(readv)(fd, iov, iovcnt);
1081 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1082 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1085 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1090 #if SANITIZER_INTERCEPT_PREADV
1091 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1094 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1095 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1096 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(preadv)(fd, iov, iovcnt, offset);
1097 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1098 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1101 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1106 #if SANITIZER_INTERCEPT_PREADV64
1107 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1110 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1111 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1113 COMMON_INTERCEPTOR_BLOCK_REAL(preadv64)(fd, iov, iovcnt, offset);
1114 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1115 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1118 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1120 #define INIT_PREADV64
1123 #if SANITIZER_INTERCEPT_WRITE
1124 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1126 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1127 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1128 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1129 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(write)(fd, ptr, count);
1130 // FIXME: this check should be _before_ the call to
1131 // COMMON_INTERCEPTOR_BLOCK_REAL(write), not after
1132 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1135 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1140 #if SANITIZER_INTERCEPT_FWRITE
1141 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1142 // libc file streams can call user-supplied functions, see fopencookie.
1144 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1145 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1146 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1149 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1154 #if SANITIZER_INTERCEPT_PWRITE
1155 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1157 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1158 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1159 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1160 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pwrite)(fd, ptr, count, offset);
1161 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1164 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1169 #if SANITIZER_INTERCEPT_PWRITE64
1170 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1173 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1174 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1175 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1176 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pwrite64)(fd, ptr, count, offset);
1177 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1180 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1182 #define INIT_PWRITE64
1185 #if SANITIZER_INTERCEPT_WRITEV
1186 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1189 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1190 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1191 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1192 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(writev)(fd, iov, iovcnt);
1193 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1196 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1201 #if SANITIZER_INTERCEPT_PWRITEV
1202 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1205 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1206 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1207 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1208 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pwritev)(fd, iov, iovcnt, offset);
1209 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1212 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1214 #define INIT_PWRITEV
1217 #if SANITIZER_INTERCEPT_PWRITEV64
1218 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1221 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1222 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1223 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1225 COMMON_INTERCEPTOR_BLOCK_REAL(pwritev64)(fd, iov, iovcnt, offset);
1226 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1229 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1231 #define INIT_PWRITEV64
1234 #if SANITIZER_INTERCEPT_FGETS
1235 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1236 // libc file streams can call user-supplied functions, see fopencookie.
1238 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1239 // FIXME: under ASan the call below may write to freed memory and corrupt
1240 // its metadata. See
1241 // https://github.com/google/sanitizers/issues/321.
1242 char *res = REAL(fgets)(s, size, file);
1244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
1247 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1252 #if SANITIZER_INTERCEPT_FPUTS
1253 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1254 // libc file streams can call user-supplied functions, see fopencookie.
1256 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1257 if (!SANITIZER_APPLE || s) { // `fputs(NULL, file)` is supported on Darwin.
1258 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1260 return REAL(fputs)(s, file);
1262 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1267 #if SANITIZER_INTERCEPT_PUTS
1268 INTERCEPTOR(int, puts, char *s) {
1269 // libc file streams can call user-supplied functions, see fopencookie.
1271 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1272 if (!SANITIZER_APPLE || s) { // `puts(NULL)` is supported on Darwin.
1273 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1275 return REAL(puts)(s);
1277 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1282 #if SANITIZER_INTERCEPT_PRCTL
1283 INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
1284 unsigned long arg4, unsigned long arg5) {
1286 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1287 static const int PR_SET_NAME = 15;
1288 static const int PR_GET_NAME = 16;
1289 static const int PR_SET_VMA = 0x53564d41;
1290 static const int PR_SCHED_CORE = 62;
1291 static const int PR_SCHED_CORE_GET = 0;
1292 static const int PR_GET_PDEATHSIG = 2;
1294 # if !SANITIZER_ANDROID
1295 static const int PR_SET_SECCOMP = 22;
1296 static const int SECCOMP_MODE_FILTER = 2;
1298 if (option == PR_SET_VMA && arg2 == 0UL) {
1299 char *name = (char *)arg5;
1300 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1302 int res = REAL(prctl)(option, arg2, arg3, arg4, arg5);
1303 if (option == PR_SET_NAME) {
1305 internal_strncpy(buff, (char *)arg2, 15);
1307 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1308 } else if (res == 0 && option == PR_GET_NAME) {
1309 char *name = (char *)arg2;
1310 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
1311 } else if (res != -1 && option == PR_SCHED_CORE &&
1312 arg2 == PR_SCHED_CORE_GET) {
1313 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64 *)(arg5), sizeof(u64));
1314 } else if (res != -1 && option == PR_GET_PDEATHSIG) {
1315 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64 *)(arg2), sizeof(int));
1316 # if !SANITIZER_ANDROID
1317 } else if (res != -1 && option == PR_SET_SECCOMP &&
1318 arg2 == SECCOMP_MODE_FILTER) {
1319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64 *)(arg3), struct_sock_fprog_sz);
1324 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1327 #endif // SANITIZER_INTERCEPT_PRCTL
1329 #if SANITIZER_INTERCEPT_TIME
1330 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1332 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1333 unsigned long local_t;
1334 unsigned long res = REAL(time)(&local_t);
1335 if (t && res != (unsigned long)-1) {
1336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1341 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1344 #endif // SANITIZER_INTERCEPT_TIME
1346 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1347 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1349 #if !SANITIZER_SOLARIS
1351 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1352 // can point to shared memory and tsan would report a data race.
1353 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1354 internal_strlen(tm->tm_zone) + 1);
1358 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1360 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1361 __sanitizer_tm *res = REAL(localtime)(timep);
1363 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1364 unpoison_tm(ctx, res);
1368 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1370 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1371 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1373 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1374 unpoison_tm(ctx, res);
1378 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1380 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1381 __sanitizer_tm *res = REAL(gmtime)(timep);
1383 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1384 unpoison_tm(ctx, res);
1388 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1390 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1391 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1393 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1394 unpoison_tm(ctx, res);
1398 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1400 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1401 // FIXME: under ASan the call below may write to freed memory and corrupt
1402 // its metadata. See
1403 // https://github.com/google/sanitizers/issues/321.
1404 char *res = REAL(ctime)(timep);
1406 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1411 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1413 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1414 // FIXME: under ASan the call below may write to freed memory and corrupt
1415 // its metadata. See
1416 // https://github.com/google/sanitizers/issues/321.
1417 char *res = REAL(ctime_r)(timep, result);
1419 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1424 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1426 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1427 // FIXME: under ASan the call below may write to freed memory and corrupt
1428 // its metadata. See
1429 // https://github.com/google/sanitizers/issues/321.
1430 char *res = REAL(asctime)(tm);
1432 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1433 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1437 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1439 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1440 // FIXME: under ASan the call below may write to freed memory and corrupt
1441 // its metadata. See
1442 // https://github.com/google/sanitizers/issues/321.
1443 char *res = REAL(asctime_r)(tm, result);
1445 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1450 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1452 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1453 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1454 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1455 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1456 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1457 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1458 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1459 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1460 long res = REAL(mktime)(tm);
1461 if (res != -1) unpoison_tm(ctx, tm);
1464 #define INIT_LOCALTIME_AND_FRIENDS \
1465 COMMON_INTERCEPT_FUNCTION(localtime); \
1466 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1467 COMMON_INTERCEPT_FUNCTION(gmtime); \
1468 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1469 COMMON_INTERCEPT_FUNCTION(ctime); \
1470 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1471 COMMON_INTERCEPT_FUNCTION(asctime); \
1472 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1473 COMMON_INTERCEPT_FUNCTION(mktime);
1475 #define INIT_LOCALTIME_AND_FRIENDS
1476 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1478 #if SANITIZER_INTERCEPT_STRPTIME
1479 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1481 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1483 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
1484 // FIXME: under ASan the call below may write to freed memory and corrupt
1485 // its metadata. See
1486 // https://github.com/google/sanitizers/issues/321.
1487 char *res = REAL(strptime)(s, format, tm);
1488 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1490 // Do not call unpoison_tm here, because strptime does not, in fact,
1491 // initialize the entire struct tm. For example, tm_zone pointer is left
1493 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1497 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1499 #define INIT_STRPTIME
1502 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1503 #include "sanitizer_common_interceptors_format.inc"
1505 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1509 va_start(ap, format); \
1510 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1511 int res = WRAP(vname)(__VA_ARGS__, ap); \
1518 #if SANITIZER_INTERCEPT_SCANF
1520 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1523 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1526 int res = REAL(vname)(__VA_ARGS__); \
1528 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1533 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1534 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1536 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1537 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1539 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1540 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1542 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1543 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1544 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1546 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1548 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1550 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1551 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1553 INTERCEPTOR(int, __isoc23_vscanf, const char *format, va_list ap)
1554 VSCANF_INTERCEPTOR_IMPL(__isoc23_vscanf, false, format, ap)
1556 INTERCEPTOR(int, __isoc23_vsscanf, const char *str, const char *format,
1558 VSCANF_INTERCEPTOR_IMPL(__isoc23_vsscanf, false, str, format, ap)
1560 INTERCEPTOR(int, __isoc23_vfscanf, void *stream, const char *format, va_list ap)
1561 VSCANF_INTERCEPTOR_IMPL(__isoc23_vfscanf, false, stream, format, ap)
1562 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1564 INTERCEPTOR(int, scanf, const char *format, ...)
1565 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1567 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1568 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1570 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1571 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1573 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1574 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1575 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1577 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1578 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1580 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1581 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1583 INTERCEPTOR(int, __isoc23_scanf, const char *format, ...)
1584 FORMAT_INTERCEPTOR_IMPL(__isoc23_scanf, __isoc23_vscanf, format)
1586 INTERCEPTOR(int, __isoc23_fscanf, void *stream, const char *format, ...)
1587 FORMAT_INTERCEPTOR_IMPL(__isoc23_fscanf, __isoc23_vfscanf, stream, format)
1589 INTERCEPTOR(int, __isoc23_sscanf, const char *str, const char *format, ...)
1590 FORMAT_INTERCEPTOR_IMPL(__isoc23_sscanf, __isoc23_vsscanf, str, format)
1595 #if SANITIZER_INTERCEPT_SCANF
1596 #define INIT_SCANF \
1597 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1598 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1599 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1600 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1601 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1602 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1607 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1608 #define INIT_ISOC99_SCANF \
1609 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1610 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1611 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1612 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1613 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1614 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf); \
1615 COMMON_INTERCEPT_FUNCTION(__isoc23_scanf); \
1616 COMMON_INTERCEPT_FUNCTION(__isoc23_sscanf); \
1617 COMMON_INTERCEPT_FUNCTION(__isoc23_fscanf); \
1618 COMMON_INTERCEPT_FUNCTION(__isoc23_vscanf); \
1619 COMMON_INTERCEPT_FUNCTION(__isoc23_vsscanf); \
1620 COMMON_INTERCEPT_FUNCTION(__isoc23_vfscanf);
1622 #define INIT_ISOC99_SCANF
1625 #if SANITIZER_INTERCEPT_PRINTF
1627 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1629 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1633 #define VPRINTF_INTERCEPTOR_RETURN() \
1636 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1638 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1639 if (common_flags()->check_printf) \
1640 printf_common(ctx, format, aq); \
1641 int res = REAL(vname)(__VA_ARGS__); \
1642 VPRINTF_INTERCEPTOR_RETURN(); \
1646 // FIXME: under ASan the REAL() call below may write to freed memory and
1647 // corrupt its metadata. See
1648 // https://github.com/google/sanitizers/issues/321.
1649 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1651 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1652 if (common_flags()->check_printf) { \
1653 printf_common(ctx, format, aq); \
1655 int res = REAL(vname)(str, __VA_ARGS__); \
1657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1659 VPRINTF_INTERCEPTOR_RETURN(); \
1663 // FIXME: under ASan the REAL() call below may write to freed memory and
1664 // corrupt its metadata. See
1665 // https://github.com/google/sanitizers/issues/321.
1666 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1668 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1669 if (common_flags()->check_printf) { \
1670 printf_common(ctx, format, aq); \
1672 int res = REAL(vname)(str, size, __VA_ARGS__); \
1674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1676 VPRINTF_INTERCEPTOR_RETURN(); \
1680 // FIXME: under ASan the REAL() call below may write to freed memory and
1681 // corrupt its metadata. See
1682 // https://github.com/google/sanitizers/issues/321.
1683 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1685 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1686 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1687 if (common_flags()->check_printf) { \
1688 printf_common(ctx, format, aq); \
1690 int res = REAL(vname)(strp, __VA_ARGS__); \
1692 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1694 VPRINTF_INTERCEPTOR_RETURN(); \
1698 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1699 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1701 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1703 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1705 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1707 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1709 #if SANITIZER_INTERCEPT___PRINTF_CHK
1710 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1711 SIZE_T size_to, const char *format, va_list ap)
1712 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1715 #if SANITIZER_INTERCEPT_PRINTF_L
1716 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1717 const char *format, va_list ap)
1718 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1720 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1721 const char *format, ...)
1722 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1723 #endif // SANITIZER_INTERCEPT_PRINTF_L
1725 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1726 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1728 #if SANITIZER_INTERCEPT___PRINTF_CHK
1729 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1730 const char *format, va_list ap)
1731 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1734 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1735 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1737 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1738 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1739 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1741 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1742 const char *format, va_list ap)
1743 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1745 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1747 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1749 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1751 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1754 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1756 INTERCEPTOR(int, printf, const char *format, ...)
1757 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1759 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1760 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1762 #if SANITIZER_INTERCEPT___PRINTF_CHK
1763 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1764 const char *format, ...)
1765 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1768 INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
1769 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)
1771 #if SANITIZER_INTERCEPT___PRINTF_CHK
1772 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1773 const char *format, ...)
1774 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
1777 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1778 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1780 #if SANITIZER_INTERCEPT___PRINTF_CHK
1781 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1782 SIZE_T size_to, const char *format, ...)
1783 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
1786 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1787 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1789 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1790 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1791 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1793 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1795 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1797 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1798 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1800 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1801 const char *format, ...)
1802 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1805 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1807 #endif // SANITIZER_INTERCEPT_PRINTF
1809 #if SANITIZER_INTERCEPT_PRINTF
1810 #define INIT_PRINTF \
1811 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1812 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1813 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1814 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1815 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1816 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1817 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1818 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1819 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1820 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1825 #if SANITIZER_INTERCEPT___PRINTF_CHK
1826 #define INIT___PRINTF_CHK \
1827 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1828 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1829 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1830 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1831 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1833 #define INIT___PRINTF_CHK
1836 #if SANITIZER_INTERCEPT_PRINTF_L
1837 #define INIT_PRINTF_L \
1838 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1839 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1841 #define INIT_PRINTF_L
1844 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1845 #define INIT_ISOC99_PRINTF \
1846 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1847 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1848 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1849 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1850 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1851 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1852 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1853 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1855 #define INIT_ISOC99_PRINTF
1858 #if SANITIZER_INTERCEPT_IOCTL
1859 #include "sanitizer_common_interceptors_ioctl.inc"
1860 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1861 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1862 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1863 // can trigger a report and we need to be able to unwind through this
1864 // function. On Mac in debug mode we might not have a frame pointer, because
1865 // ioctl_common_[pre|post] doesn't get inlined here.
1866 ENABLE_FRAME_POINTER;
1870 va_start(ap, request);
1871 void *arg = va_arg(ap, void *);
1873 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1875 CHECK(ioctl_initialized);
1877 // Note: TSan does not use common flags, and they are zero-initialized.
1878 // This effectively disables ioctl handling in TSan.
1879 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1881 // Although request is unsigned long, the rest of the interceptor uses it
1882 // as just "unsigned" to save space, because we know that all values fit in
1883 // "unsigned" - they are compile-time constants.
1885 const ioctl_desc *desc = ioctl_lookup(request);
1886 ioctl_desc decoded_desc;
1888 VPrintf(2, "Decoding unknown ioctl 0x%lx\n", request);
1889 if (!ioctl_decode(request, &decoded_desc))
1890 Printf("WARNING: failed decoding unknown ioctl 0x%lx\n", request);
1892 desc = &decoded_desc;
1895 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1896 int res = REAL(ioctl)(d, request, arg);
1897 // FIXME: some ioctls have different return values for success and failure.
1898 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1901 #define INIT_IOCTL \
1903 COMMON_INTERCEPT_FUNCTION(ioctl);
1909 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1914 internal_strlen(pwd->pw_name) + 1);
1916 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1917 internal_strlen(pwd->pw_passwd) + 1);
1918 #if !SANITIZER_ANDROID
1920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1921 internal_strlen(pwd->pw_gecos) + 1);
1923 #if SANITIZER_APPLE || SANITIZER_FREEBSD || SANITIZER_NETBSD
1925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1926 internal_strlen(pwd->pw_class) + 1);
1929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1930 internal_strlen(pwd->pw_dir) + 1);
1932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1933 internal_strlen(pwd->pw_shell) + 1);
1937 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1939 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1942 internal_strlen(grp->gr_name) + 1);
1944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1945 internal_strlen(grp->gr_passwd) + 1);
1946 char **p = grp->gr_mem;
1948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
1950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1951 (p - grp->gr_mem + 1) * sizeof(*p));
1954 #endif // SANITIZER_POSIX
1956 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1957 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1959 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1961 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1962 __sanitizer_passwd *res = REAL(getpwnam)(name);
1963 unpoison_passwd(ctx, res);
1966 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1968 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1969 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1970 unpoison_passwd(ctx, res);
1973 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1975 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1976 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1977 __sanitizer_group *res = REAL(getgrnam)(name);
1978 unpoison_group(ctx, res);
1981 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1983 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1984 __sanitizer_group *res = REAL(getgrgid)(gid);
1985 unpoison_group(ctx, res);
1988 #define INIT_GETPWNAM_AND_FRIENDS \
1989 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1990 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1991 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1992 COMMON_INTERCEPT_FUNCTION(getgrgid);
1994 #define INIT_GETPWNAM_AND_FRIENDS
1997 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1998 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1999 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
2001 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
2002 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
2003 // FIXME: under ASan the call below may write to freed memory and corrupt
2004 // its metadata. See
2005 // https://github.com/google/sanitizers/issues/321.
2006 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
2008 unpoison_passwd(ctx, *result);
2009 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2012 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
2013 SIZE_T buflen, __sanitizer_passwd **result) {
2015 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
2016 // FIXME: under ASan the call below may write to freed memory and corrupt
2017 // its metadata. See
2018 // https://github.com/google/sanitizers/issues/321.
2019 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
2021 unpoison_passwd(ctx, *result);
2022 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2025 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
2026 char *buf, SIZE_T buflen, __sanitizer_group **result) {
2028 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
2029 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
2030 // FIXME: under ASan the call below may write to freed memory and corrupt
2031 // its metadata. See
2032 // https://github.com/google/sanitizers/issues/321.
2033 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
2035 unpoison_group(ctx, *result);
2036 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2039 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
2040 SIZE_T buflen, __sanitizer_group **result) {
2042 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
2043 // FIXME: under ASan the call below may write to freed memory and corrupt
2044 // its metadata. See
2045 // https://github.com/google/sanitizers/issues/321.
2046 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
2048 unpoison_group(ctx, *result);
2049 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2052 #define INIT_GETPWNAM_R_AND_FRIENDS \
2053 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
2054 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
2055 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
2056 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
2058 #define INIT_GETPWNAM_R_AND_FRIENDS
2061 #if SANITIZER_INTERCEPT_GETPWENT
2062 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
2064 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
2065 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2066 unpoison_passwd(ctx, res);
2069 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2071 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2072 __sanitizer_group *res = REAL(getgrent)(dummy);
2073 unpoison_group(ctx, res);
2076 #define INIT_GETPWENT \
2077 COMMON_INTERCEPT_FUNCTION(getpwent); \
2078 COMMON_INTERCEPT_FUNCTION(getgrent);
2080 #define INIT_GETPWENT
2083 #if SANITIZER_INTERCEPT_FGETPWENT
2084 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2086 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2087 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2088 unpoison_passwd(ctx, res);
2091 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2093 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2094 __sanitizer_group *res = REAL(fgetgrent)(fp);
2095 unpoison_group(ctx, res);
2098 #define INIT_FGETPWENT \
2099 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2100 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2102 #define INIT_FGETPWENT
2105 #if SANITIZER_INTERCEPT_GETPWENT_R
2106 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2107 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2109 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2110 // FIXME: under ASan the call below may write to freed memory and corrupt
2111 // its metadata. See
2112 // https://github.com/google/sanitizers/issues/321.
2113 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2115 unpoison_passwd(ctx, *pwbufp);
2116 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2119 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2120 __sanitizer_group **pwbufp) {
2122 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2123 // FIXME: under ASan the call below may write to freed memory and corrupt
2124 // its metadata. See
2125 // https://github.com/google/sanitizers/issues/321.
2126 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2128 unpoison_group(ctx, *pwbufp);
2129 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2132 #define INIT_GETPWENT_R \
2133 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2134 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2136 #define INIT_GETPWENT_R
2139 #if SANITIZER_INTERCEPT_FGETPWENT_R
2140 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2141 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2143 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2144 // FIXME: under ASan the call below may write to freed memory and corrupt
2145 // its metadata. See
2146 // https://github.com/google/sanitizers/issues/321.
2147 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2149 unpoison_passwd(ctx, *pwbufp);
2150 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2153 #define INIT_FGETPWENT_R \
2154 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2156 #define INIT_FGETPWENT_R
2159 #if SANITIZER_INTERCEPT_FGETGRENT_R
2160 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2161 SIZE_T buflen, __sanitizer_group **pwbufp) {
2163 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2164 // FIXME: under ASan the call below may write to freed memory and corrupt
2165 // its metadata. See
2166 // https://github.com/google/sanitizers/issues/321.
2167 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2169 unpoison_group(ctx, *pwbufp);
2170 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2173 #define INIT_FGETGRENT_R \
2174 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2176 #define INIT_FGETGRENT_R
2179 #if SANITIZER_INTERCEPT_SETPWENT
2180 // The only thing these interceptors do is disable any nested interceptors.
2181 // These functions may open nss modules and call uninstrumented functions from
2182 // them, and we don't want things like strlen() to trigger.
2183 INTERCEPTOR(void, setpwent, int dummy) {
2185 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2186 REAL(setpwent)(dummy);
2188 INTERCEPTOR(void, endpwent, int dummy) {
2190 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2191 REAL(endpwent)(dummy);
2193 INTERCEPTOR(void, setgrent, int dummy) {
2195 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2196 REAL(setgrent)(dummy);
2198 INTERCEPTOR(void, endgrent, int dummy) {
2200 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2201 REAL(endgrent)(dummy);
2203 #define INIT_SETPWENT \
2204 COMMON_INTERCEPT_FUNCTION(setpwent); \
2205 COMMON_INTERCEPT_FUNCTION(endpwent); \
2206 COMMON_INTERCEPT_FUNCTION(setgrent); \
2207 COMMON_INTERCEPT_FUNCTION(endgrent);
2209 #define INIT_SETPWENT
2212 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2213 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2215 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2216 // FIXME: under ASan the call below may write to freed memory and corrupt
2217 // its metadata. See
2218 // https://github.com/google/sanitizers/issues/321.
2219 int res = REAL(clock_getres)(clk_id, tp);
2221 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2225 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2227 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2228 // FIXME: under ASan the call below may write to freed memory and corrupt
2229 // its metadata. See
2230 // https://github.com/google/sanitizers/issues/321.
2231 int res = REAL(clock_gettime)(clk_id, tp);
2233 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2238 namespace __sanitizer {
2240 int real_clock_gettime(u32 clk_id, void *tp) {
2241 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2242 return internal_clock_gettime(clk_id, tp);
2243 return REAL(clock_gettime)(clk_id, tp);
2246 } // namespace __sanitizer
2248 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2250 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2251 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2252 return REAL(clock_settime)(clk_id, tp);
2254 #define INIT_CLOCK_GETTIME \
2255 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2256 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2257 COMMON_INTERCEPT_FUNCTION(clock_settime);
2259 #define INIT_CLOCK_GETTIME
2262 #if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
2263 INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
2264 __sanitizer_clockid_t *clockid) {
2266 COMMON_INTERCEPTOR_ENTER(ctx, clock_getcpuclockid, pid, clockid);
2267 int res = REAL(clock_getcpuclockid)(pid, clockid);
2268 if (!res && clockid) {
2269 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2274 INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
2275 __sanitizer_clockid_t *clockid) {
2277 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
2278 int res = REAL(pthread_getcpuclockid)(thread, clockid);
2279 if (!res && clockid) {
2280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2285 #define INIT_CLOCK_GETCPUCLOCKID \
2286 COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid); \
2287 COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
2289 #define INIT_CLOCK_GETCPUCLOCKID
2292 #if SANITIZER_INTERCEPT_TIMER_CREATE
2293 INTERCEPTOR(int, timer_create, __sanitizer_clockid_t clockid, void *sevp,
2294 __sanitizer_timer_t *timer) {
2296 COMMON_INTERCEPTOR_ENTER(ctx, timer_create, clockid, sevp, timer);
2297 int res = REAL(timer_create)(clockid, sevp, timer);
2298 if (!res && timer) {
2299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, timer, sizeof *timer);
2304 INTERCEPTOR(int, timer_delete, __sanitizer_timer_t timer) {
2306 COMMON_INTERCEPTOR_ENTER(ctx, timer_delete, timer);
2307 int res = REAL(timer_delete)(timer);
2311 INTERCEPTOR(int, timer_gettime, __sanitizer_timer_t timer,
2312 struct __sanitizer_itimerspec *curr_value) {
2314 COMMON_INTERCEPTOR_ENTER(ctx, timer_gettime, timer, curr_value);
2315 int res = REAL(timer_gettime)(timer, curr_value);
2316 if (!res && curr_value) {
2317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, sizeof *curr_value);
2322 INTERCEPTOR(int, timer_settime, __sanitizer_timer_t timer, int flags,
2323 const struct __sanitizer_itimerspec *new_value,
2324 struct __sanitizer_itimerspec *old_value) {
2326 COMMON_INTERCEPTOR_ENTER(ctx, timer_settime, timer, flags, new_value,
2328 int res = REAL(timer_settime)(timer, flags, new_value, old_value);
2331 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, sizeof *new_value);
2333 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, sizeof *old_value);
2338 # define INIT_TIMER_CREATE \
2339 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(timer_create, "GLIBC_2.3.3"); \
2340 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(timer_delete, "GLIBC_2.3.3"); \
2341 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(timer_gettime, "GLIBC_2.3.3"); \
2342 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(timer_settime, "GLIBC_2.3.3");
2344 # define INIT_TIMER_CREATE
2347 #if SANITIZER_INTERCEPT_GETITIMER
2348 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2350 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2351 // FIXME: under ASan the call below may write to freed memory and corrupt
2352 // its metadata. See
2353 // https://github.com/google/sanitizers/issues/321.
2354 int res = REAL(getitimer)(which, curr_value);
2355 if (!res && curr_value) {
2356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2360 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2362 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2364 // itimerval can contain padding that may be legitimately uninitialized
2365 const struct __sanitizer_itimerval *nv =
2366 (const struct __sanitizer_itimerval *)new_value;
2367 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2368 sizeof(__sanitizer_time_t));
2369 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2370 sizeof(__sanitizer_suseconds_t));
2371 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2372 sizeof(__sanitizer_time_t));
2373 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2374 sizeof(__sanitizer_suseconds_t));
2376 // FIXME: under ASan the call below may write to freed memory and corrupt
2377 // its metadata. See
2378 // https://github.com/google/sanitizers/issues/321.
2379 int res = REAL(setitimer)(which, new_value, old_value);
2380 if (!res && old_value) {
2381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2385 #define INIT_GETITIMER \
2386 COMMON_INTERCEPT_FUNCTION(getitimer); \
2387 COMMON_INTERCEPT_FUNCTION(setitimer);
2389 #define INIT_GETITIMER
2392 #if SANITIZER_INTERCEPT_TIMESPEC_GET
2393 INTERCEPTOR(int, timespec_get, struct __sanitizer_timespec *ts, int base) {
2395 COMMON_INTERCEPTOR_ENTER(ctx, timespec_get, ts, base);
2396 // We don't yet know if ts is addressable, so we use our own scratch buffer
2397 struct __sanitizer_timespec ts_local;
2398 int res = REAL(timespec_get)(&ts_local, base);
2400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ts,
2401 sizeof(struct __sanitizer_timespec));
2402 internal_memcpy(ts, &ts_local, sizeof(struct __sanitizer_timespec));
2406 # define INIT_TIMESPEC_GET COMMON_INTERCEPT_FUNCTION(timespec_get);
2408 # define INIT_TIMESPEC_GET
2411 #if SANITIZER_INTERCEPT_GLOB
2412 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2413 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2414 // +1 for NULL pointer at the end.
2415 if (pglob->gl_pathv)
2416 COMMON_INTERCEPTOR_WRITE_RANGE(
2417 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2418 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2419 char *p = pglob->gl_pathv[i];
2420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, internal_strlen(p) + 1);
2424 #if SANITIZER_SOLARIS
2425 INTERCEPTOR(int, glob, const char *pattern, int flags,
2426 int (*errfunc)(const char *epath, int eerrno),
2427 __sanitizer_glob_t *pglob) {
2429 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2430 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2431 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2432 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2436 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2438 static void wrapped_gl_closedir(void *dir) {
2439 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2440 pglob_copy->gl_closedir(dir);
2443 static void *wrapped_gl_readdir(void *dir) {
2444 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2445 return pglob_copy->gl_readdir(dir);
2448 static void *wrapped_gl_opendir(const char *s) {
2449 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2450 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2451 return pglob_copy->gl_opendir(s);
2454 static int wrapped_gl_lstat(const char *s, void *st) {
2455 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2456 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2457 return pglob_copy->gl_lstat(s, st);
2460 static int wrapped_gl_stat(const char *s, void *st) {
2461 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2462 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2463 return pglob_copy->gl_stat(s, st);
2466 static const __sanitizer_glob_t kGlobCopy = {
2468 0, wrapped_gl_closedir, wrapped_gl_readdir,
2469 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2471 INTERCEPTOR(int, glob, const char *pattern, int flags,
2472 int (*errfunc)(const char *epath, int eerrno),
2473 __sanitizer_glob_t *pglob) {
2475 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2476 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2477 __sanitizer_glob_t glob_copy;
2478 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2479 if (flags & glob_altdirfunc) {
2480 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2481 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2482 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2483 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2484 Swap(pglob->gl_stat, glob_copy.gl_stat);
2485 pglob_copy = &glob_copy;
2487 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2488 if (flags & glob_altdirfunc) {
2489 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2490 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2491 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2492 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2493 Swap(pglob->gl_stat, glob_copy.gl_stat);
2496 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2499 #endif // SANITIZER_SOLARIS
2501 COMMON_INTERCEPT_FUNCTION(glob);
2502 #else // SANITIZER_INTERCEPT_GLOB
2504 #endif // SANITIZER_INTERCEPT_GLOB
2506 #if SANITIZER_INTERCEPT_GLOB64
2507 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2508 int (*errfunc)(const char *epath, int eerrno),
2509 __sanitizer_glob_t *pglob) {
2511 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2512 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2513 __sanitizer_glob_t glob_copy;
2514 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2515 if (flags & glob_altdirfunc) {
2516 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2517 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2518 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2519 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2520 Swap(pglob->gl_stat, glob_copy.gl_stat);
2521 pglob_copy = &glob_copy;
2523 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2524 if (flags & glob_altdirfunc) {
2525 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2526 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2527 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2528 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2529 Swap(pglob->gl_stat, glob_copy.gl_stat);
2532 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2535 #define INIT_GLOB64 \
2536 COMMON_INTERCEPT_FUNCTION(glob64);
2537 #else // SANITIZER_INTERCEPT_GLOB64
2539 #endif // SANITIZER_INTERCEPT_GLOB64
2541 #if SANITIZER_INTERCEPT___B64_TO
2542 INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
2543 char *target, SIZE_T targsize) {
2545 COMMON_INTERCEPTOR_ENTER(ctx, __b64_ntop, src, srclength, target, targsize);
2546 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclength);
2547 int res = REAL(__b64_ntop)(src, srclength, target, targsize);
2549 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res + 1);
2552 INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
2554 COMMON_INTERCEPTOR_ENTER(ctx, __b64_pton, src, target, targsize);
2555 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
2556 int res = REAL(__b64_pton)(src, target, targsize);
2558 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res);
2561 #define INIT___B64_TO \
2562 COMMON_INTERCEPT_FUNCTION(__b64_ntop); \
2563 COMMON_INTERCEPT_FUNCTION(__b64_pton);
2564 #else // SANITIZER_INTERCEPT___B64_TO
2565 #define INIT___B64_TO
2566 #endif // SANITIZER_INTERCEPT___B64_TO
2568 #if SANITIZER_INTERCEPT_DN_COMP_EXPAND
2569 # if __GLIBC_PREREQ(2, 34)
2570 // Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
2571 # define DN_COMP_INTERCEPTOR_NAME dn_comp
2572 # define DN_EXPAND_INTERCEPTOR_NAME dn_expand
2574 # define DN_COMP_INTERCEPTOR_NAME __dn_comp
2575 # define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
2577 INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
2578 unsigned char *comp_dn, int length, unsigned char **dnptrs,
2579 unsigned char **lastdnptr) {
2581 COMMON_INTERCEPTOR_ENTER(ctx, DN_COMP_INTERCEPTOR_NAME, exp_dn, comp_dn,
2582 length, dnptrs, lastdnptr);
2583 int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
2586 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, comp_dn, res);
2587 if (dnptrs && lastdnptr) {
2588 unsigned char **p = dnptrs;
2589 for (; p != lastdnptr && *p; ++p)
2593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dnptrs, (p - dnptrs) * sizeof(*p));
2598 INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
2599 unsigned char const *end, unsigned char const *src, char *dest,
2602 COMMON_INTERCEPTOR_ENTER(ctx, DN_EXPAND_INTERCEPTOR_NAME, base, end, src,
2604 // TODO: add read check if __dn_comp intercept added
2605 int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
2607 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, internal_strlen(dest) + 1);
2610 # define INIT_DN_COMP_EXPAND \
2611 COMMON_INTERCEPT_FUNCTION(DN_COMP_INTERCEPTOR_NAME); \
2612 COMMON_INTERCEPT_FUNCTION(DN_EXPAND_INTERCEPTOR_NAME);
2613 #else // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2614 # define INIT_DN_COMP_EXPAND
2615 #endif // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2617 #if SANITIZER_INTERCEPT_POSIX_SPAWN
2619 template <class RealSpawnPtr>
2620 static int PosixSpawnImpl(void *ctx, RealSpawnPtr *real_posix_spawn, pid_t *pid,
2621 const char *file_or_path, const void *file_actions,
2622 const void *attrp, char *const argv[],
2623 char *const envp[]) {
2624 COMMON_INTERCEPTOR_READ_RANGE(ctx, file_or_path,
2625 internal_strlen(file_or_path) + 1);
2627 for (char *const *s = argv; ; ++s) {
2628 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2630 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2634 for (char *const *s = envp; ; ++s) {
2635 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2637 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2641 real_posix_spawn(pid, file_or_path, file_actions, attrp, argv, envp);
2643 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pid, sizeof(*pid));
2646 INTERCEPTOR(int, posix_spawn, pid_t *pid, const char *path,
2647 const void *file_actions, const void *attrp, char *const argv[],
2648 char *const envp[]) {
2650 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawn, pid, path, file_actions, attrp,
2652 return PosixSpawnImpl(ctx, REAL(posix_spawn), pid, path, file_actions, attrp,
2655 INTERCEPTOR(int, posix_spawnp, pid_t *pid, const char *file,
2656 const void *file_actions, const void *attrp, char *const argv[],
2657 char *const envp[]) {
2659 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawnp, pid, file, file_actions, attrp,
2661 return PosixSpawnImpl(ctx, REAL(posix_spawnp), pid, file, file_actions, attrp,
2664 # define INIT_POSIX_SPAWN \
2665 COMMON_INTERCEPT_FUNCTION(posix_spawn); \
2666 COMMON_INTERCEPT_FUNCTION(posix_spawnp);
2667 #else // SANITIZER_INTERCEPT_POSIX_SPAWN
2668 # define INIT_POSIX_SPAWN
2669 #endif // SANITIZER_INTERCEPT_POSIX_SPAWN
2671 #if SANITIZER_INTERCEPT_WAIT
2672 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2673 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2675 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2677 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2678 // FIXME: under ASan the call below may write to freed memory and corrupt
2679 // its metadata. See
2680 // https://github.com/google/sanitizers/issues/321.
2681 int res = COMMON_INTERCEPTOR_BLOCK_REAL(wait)(status);
2682 if (res != -1 && status)
2683 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2686 // On FreeBSD id_t is always 64-bit wide.
2687 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2688 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2691 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2695 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2696 // FIXME: under ASan the call below may write to freed memory and corrupt
2697 // its metadata. See
2698 // https://github.com/google/sanitizers/issues/321.
2699 int res = COMMON_INTERCEPTOR_BLOCK_REAL(waitid)(idtype, id, infop, options);
2700 if (res != -1 && infop)
2701 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2704 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2706 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2707 // FIXME: under ASan the call below may write to freed memory and corrupt
2708 // its metadata. See
2709 // https://github.com/google/sanitizers/issues/321.
2710 int res = COMMON_INTERCEPTOR_BLOCK_REAL(waitpid)(pid, status, options);
2711 if (res != -1 && status)
2712 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2715 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2717 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2718 // FIXME: under ASan the call below may write to freed memory and corrupt
2719 // its metadata. See
2720 // https://github.com/google/sanitizers/issues/321.
2721 int res = COMMON_INTERCEPTOR_BLOCK_REAL(wait3)(status, options, rusage);
2723 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2724 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2728 #if SANITIZER_ANDROID
2729 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2731 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2732 // FIXME: under ASan the call below may write to freed memory and corrupt
2733 // its metadata. See
2734 // https://github.com/google/sanitizers/issues/321.
2736 COMMON_INTERCEPTOR_BLOCK_REAL(__wait4)(pid, status, options, rusage);
2738 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2739 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2743 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2745 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2747 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2748 // FIXME: under ASan the call below may write to freed memory and corrupt
2749 // its metadata. See
2750 // https://github.com/google/sanitizers/issues/321.
2751 int res = COMMON_INTERCEPTOR_BLOCK_REAL(wait4)(pid, status, options, rusage);
2753 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2754 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2758 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2759 #endif // SANITIZER_ANDROID
2761 COMMON_INTERCEPT_FUNCTION(wait); \
2762 COMMON_INTERCEPT_FUNCTION(waitid); \
2763 COMMON_INTERCEPT_FUNCTION(waitpid); \
2764 COMMON_INTERCEPT_FUNCTION(wait3);
2770 #if SANITIZER_INTERCEPT_INET
2771 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2773 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2774 uptr sz = __sanitizer_in_addr_sz(af);
2775 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2776 // FIXME: figure out read size based on the address family.
2777 // FIXME: under ASan the call below may write to freed memory and corrupt
2778 // its metadata. See
2779 // https://github.com/google/sanitizers/issues/321.
2780 char *res = REAL(inet_ntop)(af, src, dst, size);
2781 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
2784 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2786 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2787 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2788 // FIXME: figure out read size based on the address family.
2789 // FIXME: under ASan the call below may write to freed memory and corrupt
2790 // its metadata. See
2791 // https://github.com/google/sanitizers/issues/321.
2792 int res = REAL(inet_pton)(af, src, dst);
2794 uptr sz = __sanitizer_in_addr_sz(af);
2795 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2800 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2801 COMMON_INTERCEPT_FUNCTION(inet_pton);
2806 #if SANITIZER_INTERCEPT_INET
2807 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2809 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2810 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, internal_strlen(cp) + 1);
2811 // FIXME: under ASan the call below may write to freed memory and corrupt
2812 // its metadata. See
2813 // https://github.com/google/sanitizers/issues/321.
2814 int res = REAL(inet_aton)(cp, dst);
2816 uptr sz = __sanitizer_in_addr_sz(af_inet);
2817 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2821 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2823 #define INIT_INET_ATON
2826 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2827 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2829 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2830 // FIXME: under ASan the call below may write to freed memory and corrupt
2831 // its metadata. See
2832 // https://github.com/google/sanitizers/issues/321.
2833 int res = REAL(pthread_getschedparam)(thread, policy, param);
2835 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2836 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2840 #define INIT_PTHREAD_GETSCHEDPARAM \
2841 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2843 #define INIT_PTHREAD_GETSCHEDPARAM
2846 #if SANITIZER_INTERCEPT_GETADDRINFO
2847 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2848 struct __sanitizer_addrinfo *hints,
2849 struct __sanitizer_addrinfo **out) {
2851 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2852 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, internal_strlen(node) + 1);
2854 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, internal_strlen(service) + 1);
2856 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2857 // FIXME: under ASan the call below may write to freed memory and corrupt
2858 // its metadata. See
2859 // https://github.com/google/sanitizers/issues/321.
2860 int res = REAL(getaddrinfo)(node, service, hints, out);
2861 if (res == 0 && out) {
2862 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2863 struct __sanitizer_addrinfo *p = *out;
2865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2867 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2868 if (p->ai_canonname)
2869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2870 internal_strlen(p->ai_canonname) + 1);
2876 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2878 #define INIT_GETADDRINFO
2881 #if SANITIZER_INTERCEPT_GETNAMEINFO
2882 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2883 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2885 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2886 serv, servlen, flags);
2887 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2888 // There is padding in in_addr that may make this too noisy
2889 // FIXME: under ASan the call below may write to freed memory and corrupt
2890 // its metadata. See
2891 // https://github.com/google/sanitizers/issues/321.
2893 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2895 if (host && hostlen)
2896 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, internal_strlen(host) + 1);
2897 if (serv && servlen)
2898 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, internal_strlen(serv) + 1);
2902 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2904 #define INIT_GETNAMEINFO
2907 #if SANITIZER_INTERCEPT_GETSOCKNAME
2908 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {
2910 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2913 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2916 // FIXME: under ASan the call below may write to freed memory and corrupt
2917 // its metadata. See
2918 // https://github.com/google/sanitizers/issues/321.
2919 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2920 if (!res && addr && addrlen) {
2921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2925 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2927 #define INIT_GETSOCKNAME
2930 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2931 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2934 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, internal_strlen(h->h_name) + 1);
2935 char **p = h->h_aliases;
2937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
2940 COMMON_INTERCEPTOR_WRITE_RANGE(
2941 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2947 COMMON_INTERCEPTOR_WRITE_RANGE(
2948 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2952 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2953 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2955 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2956 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2957 if (res) write_hostent(ctx, res);
2961 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2964 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2965 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2966 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2967 if (res) write_hostent(ctx, res);
2971 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2973 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2974 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2975 if (res) write_hostent(ctx, res);
2978 #define INIT_GETHOSTBYNAME \
2979 COMMON_INTERCEPT_FUNCTION(gethostent); \
2980 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2981 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2983 #define INIT_GETHOSTBYNAME
2984 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2986 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2987 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2989 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2990 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2991 if (res) write_hostent(ctx, res);
2994 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2996 #define INIT_GETHOSTBYNAME2
2997 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2999 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
3000 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
3001 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
3004 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
3006 // FIXME: under ASan the call below may write to freed memory and corrupt
3007 // its metadata. See
3008 // https://github.com/google/sanitizers/issues/321.
3009 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
3011 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3012 if (res == 0 && *result) write_hostent(ctx, *result);
3015 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3018 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
3020 #define INIT_GETHOSTBYNAME_R
3023 #if SANITIZER_INTERCEPT_GETHOSTENT_R
3024 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
3025 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
3027 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
3029 // FIXME: under ASan the call below may write to freed memory and corrupt
3030 // its metadata. See
3031 // https://github.com/google/sanitizers/issues/321.
3032 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
3034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3035 if (res == 0 && *result) write_hostent(ctx, *result);
3038 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3041 #define INIT_GETHOSTENT_R \
3042 COMMON_INTERCEPT_FUNCTION(gethostent_r);
3044 #define INIT_GETHOSTENT_R
3047 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
3048 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
3049 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
3050 __sanitizer_hostent **result, int *h_errnop) {
3052 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
3053 buflen, result, h_errnop);
3054 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
3055 // FIXME: under ASan the call below may write to freed memory and corrupt
3056 // its metadata. See
3057 // https://github.com/google/sanitizers/issues/321.
3058 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
3061 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3062 if (res == 0 && *result) write_hostent(ctx, *result);
3065 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3068 #define INIT_GETHOSTBYADDR_R \
3069 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
3071 #define INIT_GETHOSTBYADDR_R
3074 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
3075 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
3076 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
3077 __sanitizer_hostent **result, int *h_errnop) {
3079 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
3081 // FIXME: under ASan the call below may write to freed memory and corrupt
3082 // its metadata. See
3083 // https://github.com/google/sanitizers/issues/321.
3085 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
3087 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3088 if (res == 0 && *result) write_hostent(ctx, *result);
3091 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3094 #define INIT_GETHOSTBYNAME2_R \
3095 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
3097 #define INIT_GETHOSTBYNAME2_R
3100 #if SANITIZER_INTERCEPT_GETSOCKOPT
3101 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
3104 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
3106 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
3107 // FIXME: under ASan the call below may write to freed memory and corrupt
3108 // its metadata. See
3109 // https://github.com/google/sanitizers/issues/321.
3110 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
3112 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
3115 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
3117 #define INIT_GETSOCKOPT
3120 #if SANITIZER_INTERCEPT_ACCEPT
3121 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
3123 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
3124 unsigned addrlen0 = 0;
3126 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3127 addrlen0 = *addrlen;
3129 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(accept)(fd, addr, addrlen);
3131 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3132 if (addr && addrlen)
3133 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3137 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
3142 #if SANITIZER_INTERCEPT_ACCEPT4
3143 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
3145 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
3146 unsigned addrlen0 = 0;
3148 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3149 addrlen0 = *addrlen;
3151 // FIXME: under ASan the call below may write to freed memory and corrupt
3152 // its metadata. See
3153 // https://github.com/google/sanitizers/issues/321.
3154 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(accept4)(fd, addr, addrlen, f);
3156 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3157 if (addr && addrlen)
3158 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3162 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
3164 #define INIT_ACCEPT4
3167 #if SANITIZER_INTERCEPT_PACCEPT
3168 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
3169 __sanitizer_sigset_t *set, int f) {
3171 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
3172 unsigned addrlen0 = 0;
3174 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3175 addrlen0 = *addrlen;
3177 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3178 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(paccept)(fd, addr, addrlen, set, f);
3180 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3181 if (addr && addrlen)
3182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3186 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
3188 #define INIT_PACCEPT
3191 #if SANITIZER_INTERCEPT_MODF
3192 INTERCEPTOR(double, modf, double x, double *iptr) {
3194 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
3195 // FIXME: under ASan the call below may write to freed memory and corrupt
3196 // its metadata. See
3197 // https://github.com/google/sanitizers/issues/321.
3198 double res = REAL(modf)(x, iptr);
3200 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3204 INTERCEPTOR(float, modff, float x, float *iptr) {
3206 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
3207 // FIXME: under ASan the call below may write to freed memory and corrupt
3208 // its metadata. See
3209 // https://github.com/google/sanitizers/issues/321.
3210 float res = REAL(modff)(x, iptr);
3212 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3216 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
3218 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
3219 // FIXME: under ASan the call below may write to freed memory and corrupt
3220 // its metadata. See
3221 // https://github.com/google/sanitizers/issues/321.
3222 long double res = REAL(modfl)(x, iptr);
3224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3229 COMMON_INTERCEPT_FUNCTION(modf); \
3230 COMMON_INTERCEPT_FUNCTION(modff); \
3231 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
3236 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
3237 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3239 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
3240 if (msg->msg_name && msg->msg_namelen)
3241 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3242 if (msg->msg_iov && msg->msg_iovlen)
3243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
3244 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3245 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3246 if (msg->msg_control && msg->msg_controllen)
3247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
3251 #if SANITIZER_INTERCEPT_RECVMSG
3252 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
3255 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
3256 // FIXME: under ASan the call below may write to freed memory and corrupt
3257 // its metadata. See
3258 // https://github.com/google/sanitizers/issues/321.
3259 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recvmsg)(fd, msg, flags);
3261 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3263 write_msghdr(ctx, msg, res);
3264 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
3269 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
3271 #define INIT_RECVMSG
3274 #if SANITIZER_INTERCEPT_RECVMMSG
3275 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3276 unsigned int vlen, int flags, void *timeout) {
3278 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
3279 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3281 COMMON_INTERCEPTOR_BLOCK_REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
3283 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3284 for (int i = 0; i < res; ++i) {
3285 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3286 sizeof(msgvec[i].msg_len));
3287 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3288 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
3293 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
3295 #define INIT_RECVMMSG
3298 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
3299 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
3300 const unsigned kCmsgDataOffset =
3301 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
3303 char *p = (char *)control;
3304 char *const control_end = p + controllen;
3306 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3307 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3308 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3310 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3312 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3313 sizeof(cmsg->cmsg_level));
3314 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3315 sizeof(cmsg->cmsg_type));
3317 if (cmsg->cmsg_len > kCmsgDataOffset) {
3318 char *data = p + kCmsgDataOffset;
3319 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3320 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3323 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3327 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3330 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3339 if (msg->msg_name && msg->msg_namelen)
3340 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3341 if (msg->msg_iov && msg->msg_iovlen)
3342 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3343 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3344 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3345 if (msg->msg_control && msg->msg_controllen)
3346 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3350 #if SANITIZER_INTERCEPT_SENDMSG
3351 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3354 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3356 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3357 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3359 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(sendmsg)(fd, msg, flags);
3360 if (common_flags()->intercept_send && res >= 0 && msg)
3361 read_msghdr(ctx, msg, res);
3364 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3366 #define INIT_SENDMSG
3369 #if SANITIZER_INTERCEPT_SENDMMSG
3370 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3371 unsigned vlen, int flags) {
3373 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3375 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3376 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3378 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sendmmsg)(fd, msgvec, vlen, flags);
3379 if (res >= 0 && msgvec) {
3380 for (int i = 0; i < res; ++i) {
3381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3382 sizeof(msgvec[i].msg_len));
3383 if (common_flags()->intercept_send)
3384 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3389 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3391 #define INIT_SENDMMSG
3394 #if SANITIZER_INTERCEPT_SYSMSG
3395 INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
3398 COMMON_INTERCEPTOR_ENTER(ctx, msgsnd, msqid, msgp, msgsz, msgflg);
3400 COMMON_INTERCEPTOR_READ_RANGE(ctx, msgp, sizeof(long) + msgsz);
3401 int res = COMMON_INTERCEPTOR_BLOCK_REAL(msgsnd)(msqid, msgp, msgsz, msgflg);
3405 INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
3406 long msgtyp, int msgflg) {
3408 COMMON_INTERCEPTOR_ENTER(ctx, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
3410 COMMON_INTERCEPTOR_BLOCK_REAL(msgrcv)(msqid, msgp, msgsz, msgtyp, msgflg);
3412 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msgp, sizeof(long) + len);
3416 #define INIT_SYSMSG \
3417 COMMON_INTERCEPT_FUNCTION(msgsnd); \
3418 COMMON_INTERCEPT_FUNCTION(msgrcv);
3423 #if SANITIZER_INTERCEPT_GETPEERNAME
3424 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3426 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3429 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3432 // FIXME: under ASan the call below may write to freed memory and corrupt
3433 // its metadata. See
3434 // https://github.com/google/sanitizers/issues/321.
3435 int res = REAL(getpeername)(sockfd, addr, addrlen);
3436 if (!res && addr && addrlen) {
3437 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3441 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3443 #define INIT_GETPEERNAME
3446 #if SANITIZER_INTERCEPT_SYSINFO
3447 INTERCEPTOR(int, sysinfo, void *info) {
3449 // FIXME: under ASan the call below may write to freed memory and corrupt
3450 // its metadata. See
3451 // https://github.com/google/sanitizers/issues/321.
3452 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3453 int res = REAL(sysinfo)(info);
3455 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3458 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3460 #define INIT_SYSINFO
3463 #if SANITIZER_INTERCEPT_READDIR
3464 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3466 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3467 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3468 __sanitizer_dirent *res = REAL(opendir)(path);
3470 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3474 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3476 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3477 // FIXME: under ASan the call below may write to freed memory and corrupt
3478 // its metadata. See
3479 // https://github.com/google/sanitizers/issues/321.
3480 __sanitizer_dirent *res = REAL(readdir)(dirp);
3482 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3486 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3487 __sanitizer_dirent **result) {
3489 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3490 // FIXME: under ASan the call below may write to freed memory and corrupt
3491 // its metadata. See
3492 // https://github.com/google/sanitizers/issues/321.
3493 int res = REAL(readdir_r)(dirp, entry, result);
3495 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3497 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3502 #define INIT_READDIR \
3503 COMMON_INTERCEPT_FUNCTION(opendir); \
3504 COMMON_INTERCEPT_FUNCTION(readdir); \
3505 COMMON_INTERCEPT_FUNCTION(readdir_r);
3507 #define INIT_READDIR
3510 #if SANITIZER_INTERCEPT_READDIR64
3511 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3513 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3514 // FIXME: under ASan the call below may write to freed memory and corrupt
3515 // its metadata. See
3516 // https://github.com/google/sanitizers/issues/321.
3517 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3519 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3523 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3524 __sanitizer_dirent64 **result) {
3526 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3527 // FIXME: under ASan the call below may write to freed memory and corrupt
3528 // its metadata. See
3529 // https://github.com/google/sanitizers/issues/321.
3530 int res = REAL(readdir64_r)(dirp, entry, result);
3532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3534 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3538 #define INIT_READDIR64 \
3539 COMMON_INTERCEPT_FUNCTION(readdir64); \
3540 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3542 #define INIT_READDIR64
3545 #if SANITIZER_INTERCEPT_PTRACE
3546 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3548 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3549 __sanitizer_iovec local_iovec;
3551 void *data_arg = ptrace_data_arg(request, addr, data);
3553 if (request == ptrace_setregs) {
3554 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg, struct_user_regs_struct_sz);
3555 } else if (request == ptrace_setfpregs) {
3556 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3557 struct_user_fpregs_struct_sz);
3558 } else if (request == ptrace_setfpxregs) {
3559 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3560 struct_user_fpxregs_struct_sz);
3561 } else if (request == ptrace_setvfpregs) {
3562 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3563 struct_user_vfpregs_struct_sz);
3564 } else if (request == ptrace_setsiginfo) {
3565 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg, siginfo_t_sz);
3567 // Some kernel might zero the iovec::iov_base in case of invalid
3568 // write access. In this case copy the invalid address for further
3570 } else if (request == ptrace_setregset || request == ptrace_getregset) {
3571 __sanitizer_iovec *iovec = (__sanitizer_iovec *)data_arg;
3572 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3573 local_iovec = *iovec;
3574 if (request == ptrace_setregset)
3575 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3579 // FIXME: under ASan the call below may write to freed memory and corrupt
3580 // its metadata. See
3581 // https://github.com/google/sanitizers/issues/321.
3582 uptr res = REAL(ptrace)(request, pid, addr, data);
3584 if (!res && data_arg) {
3585 // Note that PEEK* requests assign different meaning to the return value.
3586 // This function does not handle them (nor does it need to).
3587 if (request == ptrace_getregs) {
3588 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, struct_user_regs_struct_sz);
3589 } else if (request == ptrace_getfpregs) {
3590 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3591 struct_user_fpregs_struct_sz);
3592 } else if (request == ptrace_getfpxregs) {
3593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3594 struct_user_fpxregs_struct_sz);
3595 } else if (request == ptrace_getvfpregs) {
3596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3597 struct_user_vfpregs_struct_sz);
3598 } else if (request == ptrace_getsiginfo) {
3599 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, siginfo_t_sz);
3600 } else if (request == ptrace_geteventmsg) {
3601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, sizeof(unsigned long));
3602 } else if (request == ptrace_getregset) {
3603 __sanitizer_iovec *iovec = (__sanitizer_iovec *)data_arg;
3604 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3605 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3606 local_iovec.iov_len);
3612 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3617 #if SANITIZER_INTERCEPT_SETLOCALE
3618 static void unpoison_ctype_arrays(void *ctx) {
3619 #if SANITIZER_NETBSD
3620 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3621 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3623 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3627 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3629 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3631 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, internal_strlen(locale) + 1);
3632 char *res = REAL(setlocale)(category, locale);
3634 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3635 unpoison_ctype_arrays(ctx);
3640 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3642 #define INIT_SETLOCALE
3645 #if SANITIZER_INTERCEPT_GETCWD
3646 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3648 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3649 // FIXME: under ASan the call below may write to freed memory and corrupt
3650 // its metadata. See
3651 // https://github.com/google/sanitizers/issues/321.
3652 char *res = REAL(getcwd)(buf, size);
3653 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3656 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3661 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3662 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3664 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3665 // FIXME: under ASan the call below may write to freed memory and corrupt
3666 // its metadata. See
3667 // https://github.com/google/sanitizers/issues/321.
3668 char *res = REAL(get_current_dir_name)(fake);
3669 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3673 #define INIT_GET_CURRENT_DIR_NAME \
3674 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3676 #define INIT_GET_CURRENT_DIR_NAME
3679 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3681 if (nptr == *endptr) {
3682 // No digits were found at strtol call, we need to find out the last
3683 // symbol accessed by strtoll on our own.
3684 // We get this symbol by skipping leading blanks and optional +/- sign.
3685 while (IsSpace(*nptr)) nptr++;
3686 if (*nptr == '+' || *nptr == '-') nptr++;
3687 *endptr = const_cast<char *>(nptr);
3689 CHECK(*endptr >= nptr);
3692 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3693 char **endptr, char *real_endptr, int base) {
3695 *endptr = real_endptr;
3696 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3698 // If base has unsupported value, strtol can exit with EINVAL
3699 // without reading any characters. So do additional checks only
3700 // if base is valid.
3701 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3702 if (is_valid_base) {
3703 FixRealStrtolEndptr(nptr, &real_endptr);
3705 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3706 (real_endptr - nptr) + 1 : 0);
3709 #if SANITIZER_INTERCEPT_STRTOIMAX
3710 template <typename Fn>
3711 static ALWAYS_INLINE auto StrtoimaxImpl(void *ctx, Fn real, const char *nptr,
3712 char **endptr, int base)
3713 -> decltype(real(nullptr, nullptr, 0)) {
3715 auto res = real(nptr, &real_endptr, base);
3716 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3720 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3722 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3723 return StrtoimaxImpl(ctx, REAL(strtoimax), nptr, endptr, base);
3725 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3727 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3728 return StrtoimaxImpl(ctx, REAL(strtoumax), nptr, endptr, base);
3731 #define INIT_STRTOIMAX \
3732 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3733 COMMON_INTERCEPT_FUNCTION(strtoumax);
3735 #define INIT_STRTOIMAX
3738 #if SANITIZER_INTERCEPT_STRTOIMAX && SANITIZER_GLIBC
3739 INTERCEPTOR(INTMAX_T, __isoc23_strtoimax, const char *nptr, char **endptr, int base) {
3741 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoimax, nptr, endptr, base);
3742 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoimax), nptr, endptr, base);
3744 INTERCEPTOR(UINTMAX_T, __isoc23_strtoumax, const char *nptr, char **endptr, int base) {
3746 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoumax, nptr, endptr, base);
3747 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoumax), nptr, endptr, base);
3750 # define INIT_STRTOIMAX_C23 \
3751 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoimax); \
3752 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoumax);
3754 # define INIT_STRTOIMAX_C23
3757 #if SANITIZER_INTERCEPT_MBSTOWCS
3758 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3760 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3761 // FIXME: under ASan the call below may write to freed memory and corrupt
3762 // its metadata. See
3763 // https://github.com/google/sanitizers/issues/321.
3764 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3765 if (res != (SIZE_T) - 1 && dest) {
3766 SIZE_T write_cnt = res + (res < len);
3767 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3772 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3775 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3776 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3777 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3778 // FIXME: under ASan the call below may write to freed memory and corrupt
3779 // its metadata. See
3780 // https://github.com/google/sanitizers/issues/321.
3781 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3782 if (res != (SIZE_T)(-1) && dest && src) {
3783 // This function, and several others, may or may not write the terminating
3784 // \0 character. They write it iff they clear *src.
3785 SIZE_T write_cnt = res + !*src;
3786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3791 #define INIT_MBSTOWCS \
3792 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3793 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3795 #define INIT_MBSTOWCS
3798 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3799 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3800 SIZE_T len, void *ps) {
3802 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3804 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3805 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3807 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3808 // FIXME: under ASan the call below may write to freed memory and corrupt
3809 // its metadata. See
3810 // https://github.com/google/sanitizers/issues/321.
3811 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3812 if (res != (SIZE_T)(-1) && dest && src) {
3813 SIZE_T write_cnt = res + !*src;
3814 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3819 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3821 #define INIT_MBSNRTOWCS
3824 #if SANITIZER_INTERCEPT_WCSTOMBS
3825 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3827 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3828 // FIXME: under ASan the call below may write to freed memory and corrupt
3829 // its metadata. See
3830 // https://github.com/google/sanitizers/issues/321.
3831 SIZE_T res = REAL(wcstombs)(dest, src, len);
3832 if (res != (SIZE_T) - 1 && dest) {
3833 SIZE_T write_cnt = res + (res < len);
3834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3839 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3842 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3843 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3844 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3845 // FIXME: under ASan the call below may write to freed memory and corrupt
3846 // its metadata. See
3847 // https://github.com/google/sanitizers/issues/321.
3848 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3849 if (res != (SIZE_T) - 1 && dest && src) {
3850 SIZE_T write_cnt = res + !*src;
3851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3856 #define INIT_WCSTOMBS \
3857 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3858 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3860 #define INIT_WCSTOMBS
3863 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3864 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3865 SIZE_T len, void *ps) {
3867 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3869 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3870 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3872 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3873 // FIXME: under ASan the call below may write to freed memory and corrupt
3874 // its metadata. See
3875 // https://github.com/google/sanitizers/issues/321.
3876 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3877 if (res != ((SIZE_T)-1) && dest && src) {
3878 SIZE_T write_cnt = res + !*src;
3879 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3884 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3886 #define INIT_WCSNRTOMBS
3890 #if SANITIZER_INTERCEPT_WCRTOMB
3891 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3893 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3894 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3897 return REAL(wcrtomb)(dest, src, ps);
3899 char local_dest[32];
3900 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3901 if (res != ((SIZE_T)-1)) {
3902 CHECK_LE(res, sizeof(local_dest));
3903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3904 REAL(memcpy)(dest, local_dest, res);
3909 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3911 #define INIT_WCRTOMB
3914 #if SANITIZER_INTERCEPT_WCTOMB
3915 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3917 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3919 return REAL(wctomb)(dest, src);
3921 char local_dest[32];
3922 int res = REAL(wctomb)(local_dest, src);
3924 CHECK_LE(res, sizeof(local_dest));
3925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3926 REAL(memcpy)(dest, local_dest, res);
3931 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3936 #if SANITIZER_INTERCEPT_TCGETATTR
3937 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3939 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3940 // FIXME: under ASan the call below may write to freed memory and corrupt
3941 // its metadata. See
3942 // https://github.com/google/sanitizers/issues/321.
3943 int res = REAL(tcgetattr)(fd, termios_p);
3944 if (!res && termios_p)
3945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3949 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3951 #define INIT_TCGETATTR
3954 #if SANITIZER_INTERCEPT_REALPATH
3955 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3957 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3958 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3960 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3961 // version of a versioned symbol. For realpath(), this gives us something
3962 // (called __old_realpath) that does not handle NULL in the second argument.
3963 // Handle it as part of the interceptor.
3964 char *allocated_path = nullptr;
3966 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3968 char *res = REAL(realpath)(path, resolved_path);
3969 if (allocated_path && !res)
3970 WRAP(free)(allocated_path);
3971 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3974 # define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3976 #define INIT_REALPATH
3979 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3980 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3982 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3983 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3984 char *res = REAL(canonicalize_file_name)(path);
3985 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3988 #define INIT_CANONICALIZE_FILE_NAME \
3989 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3991 #define INIT_CANONICALIZE_FILE_NAME
3994 #if SANITIZER_INTERCEPT_CONFSTR
3995 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3997 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3998 // FIXME: under ASan the call below may write to freed memory and corrupt
3999 // its metadata. See
4000 // https://github.com/google/sanitizers/issues/321.
4001 SIZE_T res = REAL(confstr)(name, buf, len);
4003 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
4006 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
4008 #define INIT_CONFSTR
4011 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
4012 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
4014 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
4015 // FIXME: under ASan the call below may write to freed memory and corrupt
4016 // its metadata. See
4017 // https://github.com/google/sanitizers/issues/321.
4018 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
4019 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
4022 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
4024 #define INIT_SCHED_GETAFFINITY
4027 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
4028 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
4030 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
4031 int res = REAL(sched_getparam)(pid, param);
4032 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
4035 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
4037 #define INIT_SCHED_GETPARAM
4040 #if SANITIZER_INTERCEPT_STRERROR
4041 INTERCEPTOR(char *, strerror, int errnum) {
4043 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
4044 COMMON_INTERCEPTOR_STRERROR();
4045 char *res = REAL(strerror)(errnum);
4046 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4049 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
4051 #define INIT_STRERROR
4054 #if SANITIZER_INTERCEPT_STRERROR_R
4055 // There are 2 versions of strerror_r:
4056 // * POSIX version returns 0 on success, negative error code on failure,
4057 // writes message to buf.
4058 // * GNU version returns message pointer, which points to either buf or some
4060 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
4061 SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD || \
4063 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
4064 // At least on OSX, buf contents are valid even when the call fails.
4065 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
4067 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
4068 // FIXME: under ASan the call below may write to freed memory and corrupt
4069 // its metadata. See
4070 // https://github.com/google/sanitizers/issues/321.
4071 int res = REAL(strerror_r)(errnum, buf, buflen);
4073 SIZE_T sz = internal_strnlen(buf, buflen);
4074 if (sz < buflen) ++sz;
4075 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4080 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
4082 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
4083 // FIXME: under ASan the call below may write to freed memory and corrupt
4084 // its metadata. See
4085 // https://github.com/google/sanitizers/issues/321.
4086 char *res = REAL(strerror_r)(errnum, buf, buflen);
4088 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4090 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4093 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
4095 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
4097 #define INIT_STRERROR_R
4100 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
4101 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
4103 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
4104 // FIXME: under ASan the call below may write to freed memory and corrupt
4105 // its metadata. See
4106 // https://github.com/google/sanitizers/issues/321.
4107 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
4108 // This version always returns a null-terminated string.
4110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
4113 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
4115 #define INIT_XPG_STRERROR_R
4118 #if SANITIZER_INTERCEPT_SCANDIR
4119 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
4120 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
4121 const struct __sanitizer_dirent **);
4123 static THREADLOCAL scandir_filter_f scandir_filter;
4124 static THREADLOCAL scandir_compar_f scandir_compar;
4126 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
4127 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4128 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4129 return scandir_filter(dir);
4132 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
4133 const struct __sanitizer_dirent **b) {
4134 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4135 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4136 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4137 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4138 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4139 return scandir_compar(a, b);
4142 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
4143 scandir_filter_f filter, scandir_compar_f compar) {
4145 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
4146 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4147 scandir_filter = filter;
4148 scandir_compar = compar;
4149 // FIXME: under ASan the call below may write to freed memory and corrupt
4150 // its metadata. See
4151 // https://github.com/google/sanitizers/issues/321.
4152 int res = REAL(scandir)(dirp, namelist,
4153 filter ? wrapped_scandir_filter : nullptr,
4154 compar ? wrapped_scandir_compar : nullptr);
4155 scandir_filter = nullptr;
4156 scandir_compar = nullptr;
4157 if (namelist && res > 0) {
4158 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4159 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4160 for (int i = 0; i < res; ++i)
4161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4162 __sanitizer_dirsiz((*namelist)[i]));
4166 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
4168 #define INIT_SCANDIR
4171 #if SANITIZER_INTERCEPT_SCANDIR64
4172 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
4173 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
4174 const struct __sanitizer_dirent64 **);
4176 static THREADLOCAL scandir64_filter_f scandir64_filter;
4177 static THREADLOCAL scandir64_compar_f scandir64_compar;
4179 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
4180 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4181 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4182 return scandir64_filter(dir);
4185 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
4186 const struct __sanitizer_dirent64 **b) {
4187 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4188 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4189 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4190 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4191 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4192 return scandir64_compar(a, b);
4195 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
4196 scandir64_filter_f filter, scandir64_compar_f compar) {
4198 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
4199 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4200 scandir64_filter = filter;
4201 scandir64_compar = compar;
4202 // FIXME: under ASan the call below may write to freed memory and corrupt
4203 // its metadata. See
4204 // https://github.com/google/sanitizers/issues/321.
4206 REAL(scandir64)(dirp, namelist,
4207 filter ? wrapped_scandir64_filter : nullptr,
4208 compar ? wrapped_scandir64_compar : nullptr);
4209 scandir64_filter = nullptr;
4210 scandir64_compar = nullptr;
4211 if (namelist && res > 0) {
4212 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4213 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4214 for (int i = 0; i < res; ++i)
4215 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4216 __sanitizer_dirsiz((*namelist)[i]));
4220 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
4222 #define INIT_SCANDIR64
4225 #if SANITIZER_INTERCEPT_GETGROUPS
4226 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
4228 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
4229 // FIXME: under ASan the call below may write to freed memory and corrupt
4230 // its metadata. See
4231 // https://github.com/google/sanitizers/issues/321.
4232 int res = REAL(getgroups)(size, lst);
4233 if (res >= 0 && lst && size > 0)
4234 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
4237 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
4239 #define INIT_GETGROUPS
4242 #if SANITIZER_INTERCEPT_POLL
4243 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
4244 __sanitizer_nfds_t nfds) {
4245 for (unsigned i = 0; i < nfds; ++i) {
4246 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
4247 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
4251 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
4252 __sanitizer_nfds_t nfds) {
4253 for (unsigned i = 0; i < nfds; ++i)
4254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
4255 sizeof(fds[i].revents));
4258 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4261 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
4262 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4263 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
4264 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4267 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
4272 #if SANITIZER_INTERCEPT_PPOLL
4273 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4274 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
4276 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
4277 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4279 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
4280 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
4282 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
4283 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4286 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
4291 #if SANITIZER_INTERCEPT_WORDEXP
4292 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
4294 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
4295 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
4296 // FIXME: under ASan the call below may write to freed memory and corrupt
4297 // its metadata. See
4298 // https://github.com/google/sanitizers/issues/321.
4299 int res = REAL(wordexp)(s, p, flags);
4301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4303 ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc;
4304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
4305 sizeof(*p->we_wordv) * (we_wordc + 1));
4306 for (uptr i = 0; i < we_wordc; ++i) {
4307 char *w = p->we_wordv[i];
4308 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);
4313 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
4315 #define INIT_WORDEXP
4318 #if SANITIZER_INTERCEPT_SIGWAIT
4319 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
4321 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
4322 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4323 // FIXME: under ASan the call below may write to freed memory and corrupt
4324 // its metadata. See
4325 // https://github.com/google/sanitizers/issues/321.
4326 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwait)(set, sig);
4327 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
4330 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
4332 #define INIT_SIGWAIT
4335 #if SANITIZER_INTERCEPT_SIGWAITINFO
4336 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
4338 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
4339 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4340 // FIXME: under ASan the call below may write to freed memory and corrupt
4341 // its metadata. See
4342 // https://github.com/google/sanitizers/issues/321.
4343 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwaitinfo)(set, info);
4344 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4347 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
4349 #define INIT_SIGWAITINFO
4352 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
4353 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
4356 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
4357 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
4358 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4359 // FIXME: under ASan the call below may write to freed memory and corrupt
4360 // its metadata. See
4361 // https://github.com/google/sanitizers/issues/321.
4362 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigtimedwait)(set, info, timeout);
4363 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4366 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4368 #define INIT_SIGTIMEDWAIT
4371 #if SANITIZER_INTERCEPT_SIGSETOPS
4372 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4374 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4375 // FIXME: under ASan the call below may write to freed memory and corrupt
4376 // its metadata. See
4377 // https://github.com/google/sanitizers/issues/321.
4378 int res = REAL(sigemptyset)(set);
4379 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4383 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4385 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4386 // FIXME: under ASan the call below may write to freed memory and corrupt
4387 // its metadata. See
4388 // https://github.com/google/sanitizers/issues/321.
4389 int res = REAL(sigfillset)(set);
4390 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4393 #define INIT_SIGSETOPS \
4394 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4395 COMMON_INTERCEPT_FUNCTION(sigfillset);
4397 #define INIT_SIGSETOPS
4400 #if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
4401 INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
4402 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4404 COMMON_INTERCEPTOR_ENTER(ctx, sigandset, dst, src1, src2);
4406 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4408 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4409 int res = REAL(sigandset)(dst, src1, src2);
4411 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4415 INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
4416 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4418 COMMON_INTERCEPTOR_ENTER(ctx, sigorset, dst, src1, src2);
4420 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4422 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4423 int res = REAL(sigorset)(dst, src1, src2);
4425 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4428 #define INIT_SIGSET_LOGICOPS \
4429 COMMON_INTERCEPT_FUNCTION(sigandset); \
4430 COMMON_INTERCEPT_FUNCTION(sigorset);
4432 #define INIT_SIGSET_LOGICOPS
4435 #if SANITIZER_INTERCEPT_SIGPENDING
4436 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4438 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4439 // FIXME: under ASan the call below may write to freed memory and corrupt
4440 // its metadata. See
4441 // https://github.com/google/sanitizers/issues/321.
4442 int res = REAL(sigpending)(set);
4443 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4446 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4448 #define INIT_SIGPENDING
4451 #if SANITIZER_INTERCEPT_SIGPROCMASK
4452 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4453 __sanitizer_sigset_t *oldset) {
4455 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4456 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4457 // FIXME: under ASan the call below may write to freed memory and corrupt
4458 // its metadata. See
4459 // https://github.com/google/sanitizers/issues/321.
4460 int res = REAL(sigprocmask)(how, set, oldset);
4462 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4465 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4467 #define INIT_SIGPROCMASK
4470 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4471 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4472 __sanitizer_sigset_t *oldset) {
4474 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4475 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4476 // FIXME: under ASan the call below may write to freed memory and corrupt
4477 // its metadata. See
4478 // https://github.com/google/sanitizers/issues/321.
4479 int res = REAL(pthread_sigmask)(how, set, oldset);
4481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4484 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4486 #define INIT_PTHREAD_SIGMASK
4489 #if SANITIZER_INTERCEPT_BACKTRACE
4490 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4492 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4493 // 'buffer' might be freed memory, hence it is unsafe to directly call
4494 // REAL(backtrace)(buffer, size). Instead, we use our own known-good
4496 void **scratch = (void**)InternalAlloc(sizeof(void*) * size);
4497 int res = REAL(backtrace)(scratch, size);
4498 if (res && buffer) {
4499 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4500 internal_memcpy(buffer, scratch, res * sizeof(*buffer));
4502 InternalFree(scratch);
4506 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4508 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4510 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4511 // The COMMON_INTERCEPTOR_READ_RANGE above ensures that 'buffer' is
4512 // valid for reading.
4513 char **res = REAL(backtrace_symbols)(buffer, size);
4515 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4516 for (int i = 0; i < size; ++i)
4517 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], internal_strlen(res[i]) + 1);
4521 #define INIT_BACKTRACE \
4522 COMMON_INTERCEPT_FUNCTION(backtrace); \
4523 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4525 #define INIT_BACKTRACE
4528 #if SANITIZER_INTERCEPT__EXIT
4529 INTERCEPTOR(void, _exit, int status) {
4531 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4532 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4533 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4534 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4535 if (status == 0) status = status1;
4536 REAL(_exit)(status);
4538 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4543 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4544 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4545 ALIAS(WRAP(pthread_setcancelstate));
4547 #define INIT___LIBC_THR_SETCANCELSTATE \
4548 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4550 #define INIT___LIBC_THR_SETCANCELSTATE
4553 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4554 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4555 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4556 if (mnt->mnt_fsname)
4557 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4558 internal_strlen(mnt->mnt_fsname) + 1);
4560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4561 internal_strlen(mnt->mnt_dir) + 1);
4563 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4564 internal_strlen(mnt->mnt_type) + 1);
4566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4567 internal_strlen(mnt->mnt_opts) + 1);
4571 #if SANITIZER_INTERCEPT_GETMNTENT
4572 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4574 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4575 __sanitizer_mntent *res = REAL(getmntent)(fp);
4576 if (res) write_mntent(ctx, res);
4579 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4581 #define INIT_GETMNTENT
4584 #if SANITIZER_INTERCEPT_GETMNTENT_R
4585 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4586 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4588 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4589 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4590 if (res) write_mntent(ctx, res);
4593 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4595 #define INIT_GETMNTENT_R
4598 #if SANITIZER_INTERCEPT_STATFS
4599 INTERCEPTOR(int, statfs, char *path, void *buf) {
4601 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4602 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4603 // FIXME: under ASan the call below may write to freed memory and corrupt
4604 // its metadata. See
4605 // https://github.com/google/sanitizers/issues/321.
4606 int res = REAL(statfs)(path, buf);
4607 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4610 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4612 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4613 // FIXME: under ASan the call below may write to freed memory and corrupt
4614 // its metadata. See
4615 // https://github.com/google/sanitizers/issues/321.
4616 int res = REAL(fstatfs)(fd, buf);
4617 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4620 #define INIT_STATFS \
4621 COMMON_INTERCEPT_FUNCTION(statfs); \
4622 COMMON_INTERCEPT_FUNCTION(fstatfs);
4627 #if SANITIZER_INTERCEPT_STATFS64
4628 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4630 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4631 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4632 // FIXME: under ASan the call below may write to freed memory and corrupt
4633 // its metadata. See
4634 // https://github.com/google/sanitizers/issues/321.
4635 int res = REAL(statfs64)(path, buf);
4636 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4639 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4641 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4642 // FIXME: under ASan the call below may write to freed memory and corrupt
4643 // its metadata. See
4644 // https://github.com/google/sanitizers/issues/321.
4645 int res = REAL(fstatfs64)(fd, buf);
4646 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4649 #define INIT_STATFS64 \
4650 COMMON_INTERCEPT_FUNCTION(statfs64); \
4651 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4653 #define INIT_STATFS64
4656 #if SANITIZER_INTERCEPT_STATVFS
4657 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4659 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4660 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4661 // FIXME: under ASan the call below may write to freed memory and corrupt
4662 // its metadata. See
4663 // https://github.com/google/sanitizers/issues/321.
4664 int res = REAL(statvfs)(path, buf);
4665 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4668 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4670 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4671 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4672 // FIXME: under ASan the call below may write to freed memory and corrupt
4673 // its metadata. See
4674 // https://github.com/google/sanitizers/issues/321.
4675 int res = REAL(fstatvfs)(fd, buf);
4677 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4679 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4683 #define INIT_STATVFS \
4684 COMMON_INTERCEPT_FUNCTION(statvfs); \
4685 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4687 #define INIT_STATVFS
4690 #if SANITIZER_INTERCEPT_STATVFS64
4691 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4693 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4694 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4695 // FIXME: under ASan the call below may write to freed memory and corrupt
4696 // its metadata. See
4697 // https://github.com/google/sanitizers/issues/321.
4698 int res = REAL(statvfs64)(path, buf);
4699 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4702 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4704 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4705 // FIXME: under ASan the call below may write to freed memory and corrupt
4706 // its metadata. See
4707 // https://github.com/google/sanitizers/issues/321.
4708 int res = REAL(fstatvfs64)(fd, buf);
4709 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4712 #define INIT_STATVFS64 \
4713 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4714 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4716 #define INIT_STATVFS64
4719 #if SANITIZER_INTERCEPT_INITGROUPS
4720 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4722 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4723 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, internal_strlen(user) + 1);
4724 int res = REAL(initgroups)(user, group);
4727 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4729 #define INIT_INITGROUPS
4732 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4733 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4735 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4736 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4737 char *res = REAL(ether_ntoa)(addr);
4738 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4741 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4743 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4744 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4745 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4746 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4749 #define INIT_ETHER_NTOA_ATON \
4750 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4751 COMMON_INTERCEPT_FUNCTION(ether_aton);
4753 #define INIT_ETHER_NTOA_ATON
4756 #if SANITIZER_INTERCEPT_ETHER_HOST
4757 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4759 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4760 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4761 // FIXME: under ASan the call below may write to freed memory and corrupt
4762 // its metadata. See
4763 // https://github.com/google/sanitizers/issues/321.
4764 int res = REAL(ether_ntohost)(hostname, addr);
4765 if (!res && hostname)
4766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4769 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4771 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4773 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4774 // FIXME: under ASan the call below may write to freed memory and corrupt
4775 // its metadata. See
4776 // https://github.com/google/sanitizers/issues/321.
4777 int res = REAL(ether_hostton)(hostname, addr);
4778 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4781 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4784 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4785 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, internal_strlen(line) + 1);
4786 // FIXME: under ASan the call below may write to freed memory and corrupt
4787 // its metadata. See
4788 // https://github.com/google/sanitizers/issues/321.
4789 int res = REAL(ether_line)(line, addr, hostname);
4791 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4797 #define INIT_ETHER_HOST \
4798 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4799 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4800 COMMON_INTERCEPT_FUNCTION(ether_line);
4802 #define INIT_ETHER_HOST
4805 #if SANITIZER_INTERCEPT_ETHER_R
4806 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4808 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4809 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4810 // FIXME: under ASan the call below may write to freed memory and corrupt
4811 // its metadata. See
4812 // https://github.com/google/sanitizers/issues/321.
4813 char *res = REAL(ether_ntoa_r)(addr, buf);
4814 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4817 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4818 __sanitizer_ether_addr *addr) {
4820 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4821 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4822 // FIXME: under ASan the call below may write to freed memory and corrupt
4823 // its metadata. See
4824 // https://github.com/google/sanitizers/issues/321.
4825 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4826 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4829 #define INIT_ETHER_R \
4830 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4831 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4833 #define INIT_ETHER_R
4836 #if SANITIZER_INTERCEPT_SHMCTL
4837 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4839 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4840 // FIXME: under ASan the call below may write to freed memory and corrupt
4841 // its metadata. See
4842 // https://github.com/google/sanitizers/issues/321.
4843 int res = REAL(shmctl)(shmid, cmd, buf);
4846 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4847 sz = sizeof(__sanitizer_shmid_ds);
4848 else if (cmd == shmctl_ipc_info)
4849 sz = struct_shminfo_sz;
4850 else if (cmd == shmctl_shm_info)
4851 sz = struct_shm_info_sz;
4852 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4856 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4861 #if SANITIZER_INTERCEPT_RANDOM_R
4862 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4864 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4865 // FIXME: under ASan the call below may write to freed memory and corrupt
4866 // its metadata. See
4867 // https://github.com/google/sanitizers/issues/321.
4868 int res = REAL(random_r)(buf, result);
4870 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4873 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4875 #define INIT_RANDOM_R
4878 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4879 // its metadata. See
4880 // https://github.com/google/sanitizers/issues/321.
4881 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4882 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4883 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4884 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4885 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4886 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4887 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4888 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4889 INTERCEPTOR(int, fn, void *attr, void *r) { \
4891 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4892 int res = REAL(fn)(attr, r); \
4893 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4896 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4897 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4898 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4899 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4900 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4901 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4902 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4903 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4904 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4905 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4908 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4909 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4910 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4911 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4912 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4913 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4915 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4916 // FIXME: under ASan the call below may write to freed memory and corrupt
4917 // its metadata. See
4918 // https://github.com/google/sanitizers/issues/321.
4919 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4921 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4922 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4927 // We may need to call the real pthread_attr_getstack from the run-time
4928 // in sanitizer_common, but we don't want to include the interception headers
4929 // there. So, just define this function here.
4930 namespace __sanitizer {
4932 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4933 return REAL(pthread_attr_getstack)(attr, addr, size);
4936 } // namespace __sanitizer
4938 #define INIT_PTHREAD_ATTR_GET \
4939 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4940 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4941 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4942 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4943 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4945 #define INIT_PTHREAD_ATTR_GET
4948 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4949 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4950 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4952 #define INIT_PTHREAD_ATTR_GET_SCHED \
4953 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4954 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4956 #define INIT_PTHREAD_ATTR_GET_SCHED
4959 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4960 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4962 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4963 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4965 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4968 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4969 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4972 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4974 // FIXME: under ASan the call below may write to freed memory and corrupt
4975 // its metadata. See
4976 // https://github.com/google/sanitizers/issues/321.
4977 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4978 if (!res && cpusetsize && cpuset)
4979 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4983 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4984 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4986 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4989 #if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
4990 INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
4993 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
4995 // FIXME: under ASan the call below may write to freed memory and corrupt
4996 // its metadata. See
4997 // https://github.com/google/sanitizers/issues/321.
4998 int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
4999 if (!res && cpusetsize && cpuset)
5000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
5004 #define INIT_PTHREAD_GETAFFINITY_NP \
5005 COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
5007 #define INIT_PTHREAD_GETAFFINITY_NP
5010 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
5011 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
5012 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
5013 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
5015 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
5018 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
5019 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
5020 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
5021 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
5023 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
5026 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
5027 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
5028 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
5029 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
5031 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
5034 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
5035 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
5036 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
5037 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
5039 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
5042 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
5043 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
5044 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
5045 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
5047 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
5050 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
5051 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
5052 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
5053 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
5055 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
5058 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
5059 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
5060 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
5061 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
5063 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
5066 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
5067 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
5068 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
5069 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
5071 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
5074 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
5075 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
5076 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
5077 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
5079 #define INIT_PTHREAD_CONDATTR_GETPSHARED
5082 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
5083 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
5084 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
5085 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
5087 #define INIT_PTHREAD_CONDATTR_GETCLOCK
5090 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
5091 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
5092 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
5093 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
5095 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
5098 #if SANITIZER_INTERCEPT_TMPNAM
5099 INTERCEPTOR(char *, tmpnam, char *s) {
5101 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
5102 char *res = REAL(tmpnam)(s);
5105 // FIXME: under ASan the call below may write to freed memory and corrupt
5106 // its metadata. See
5107 // https://github.com/google/sanitizers/issues/321.
5108 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
5110 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5114 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
5119 #if SANITIZER_INTERCEPT_TMPNAM_R
5120 INTERCEPTOR(char *, tmpnam_r, char *s) {
5122 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
5123 // FIXME: under ASan the call below may write to freed memory and corrupt
5124 // its metadata. See
5125 // https://github.com/google/sanitizers/issues/321.
5126 char *res = REAL(tmpnam_r)(s);
5127 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
5130 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
5132 #define INIT_TMPNAM_R
5135 #if SANITIZER_INTERCEPT_PTSNAME
5136 INTERCEPTOR(char *, ptsname, int fd) {
5138 COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
5139 char *res = REAL(ptsname)(fd);
5141 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5144 #define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
5146 #define INIT_PTSNAME
5149 #if SANITIZER_INTERCEPT_PTSNAME_R
5150 INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
5152 COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
5153 int res = REAL(ptsname_r)(fd, name, namesize);
5155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5158 #define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
5160 #define INIT_PTSNAME_R
5163 #if SANITIZER_INTERCEPT_TTYNAME
5164 INTERCEPTOR(char *, ttyname, int fd) {
5166 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
5167 char *res = REAL(ttyname)(fd);
5169 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5172 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
5174 #define INIT_TTYNAME
5177 #if SANITIZER_INTERCEPT_TTYNAME_R
5178 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
5180 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
5181 int res = REAL(ttyname_r)(fd, name, namesize);
5183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5186 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
5188 #define INIT_TTYNAME_R
5191 #if SANITIZER_INTERCEPT_TEMPNAM
5192 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
5194 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
5195 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, internal_strlen(dir) + 1);
5196 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, internal_strlen(pfx) + 1);
5197 char *res = REAL(tempnam)(dir, pfx);
5198 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5201 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
5203 #define INIT_TEMPNAM
5206 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
5207 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
5209 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
5210 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5211 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
5212 return REAL(pthread_setname_np)(thread, name);
5214 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5215 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
5216 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
5218 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
5219 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
5220 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5221 internal_snprintf(newname, sizeof(newname), name, arg);
5222 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
5223 return REAL(pthread_setname_np)(thread, name, arg);
5225 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5227 #define INIT_PTHREAD_SETNAME_NP
5230 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
5231 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
5233 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
5234 int res = REAL(pthread_getname_np)(thread, name, len);
5236 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
5239 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
5241 #define INIT_PTHREAD_GETNAME_NP
5244 #if SANITIZER_INTERCEPT_SINCOS
5245 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
5247 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
5248 // FIXME: under ASan the call below may write to freed memory and corrupt
5249 // its metadata. See
5250 // https://github.com/google/sanitizers/issues/321.
5251 REAL(sincos)(x, sin, cos);
5252 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5253 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5255 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
5257 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
5258 // FIXME: under ASan the call below may write to freed memory and corrupt
5259 // its metadata. See
5260 // https://github.com/google/sanitizers/issues/321.
5261 REAL(sincosf)(x, sin, cos);
5262 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5263 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5265 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
5267 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
5268 // FIXME: under ASan the call below may write to freed memory and corrupt
5269 // its metadata. See
5270 // https://github.com/google/sanitizers/issues/321.
5271 REAL(sincosl)(x, sin, cos);
5272 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5273 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5275 #define INIT_SINCOS \
5276 COMMON_INTERCEPT_FUNCTION(sincos); \
5277 COMMON_INTERCEPT_FUNCTION(sincosf); \
5278 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
5283 #if SANITIZER_INTERCEPT_REMQUO
5284 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
5286 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
5287 // FIXME: under ASan the call below may write to freed memory and corrupt
5288 // its metadata. See
5289 // https://github.com/google/sanitizers/issues/321.
5290 double res = REAL(remquo)(x, y, quo);
5291 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5294 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
5296 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
5297 // FIXME: under ASan the call below may write to freed memory and corrupt
5298 // its metadata. See
5299 // https://github.com/google/sanitizers/issues/321.
5300 float res = REAL(remquof)(x, y, quo);
5301 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5304 #define INIT_REMQUO \
5305 COMMON_INTERCEPT_FUNCTION(remquo); \
5306 COMMON_INTERCEPT_FUNCTION(remquof);
5311 #if SANITIZER_INTERCEPT_REMQUOL
5312 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
5314 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
5315 // FIXME: under ASan the call below may write to freed memory and corrupt
5316 // its metadata. See
5317 // https://github.com/google/sanitizers/issues/321.
5318 long double res = REAL(remquol)(x, y, quo);
5319 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5322 #define INIT_REMQUOL \
5323 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
5325 #define INIT_REMQUOL
5328 #if SANITIZER_INTERCEPT_LGAMMA
5330 INTERCEPTOR(double, lgamma, double x) {
5332 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
5333 double res = REAL(lgamma)(x);
5334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5337 INTERCEPTOR(float, lgammaf, float x) {
5339 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
5340 float res = REAL(lgammaf)(x);
5341 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5344 #define INIT_LGAMMA \
5345 COMMON_INTERCEPT_FUNCTION(lgamma); \
5346 COMMON_INTERCEPT_FUNCTION(lgammaf);
5351 #if SANITIZER_INTERCEPT_LGAMMAL
5352 INTERCEPTOR(long double, lgammal, long double x) {
5354 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
5355 long double res = REAL(lgammal)(x);
5356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5359 #define INIT_LGAMMAL \
5360 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
5362 #define INIT_LGAMMAL
5365 #if SANITIZER_INTERCEPT_LGAMMA_R
5366 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
5368 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
5369 // FIXME: under ASan the call below may write to freed memory and corrupt
5370 // its metadata. See
5371 // https://github.com/google/sanitizers/issues/321.
5372 double res = REAL(lgamma_r)(x, signp);
5373 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5376 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
5378 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
5379 // FIXME: under ASan the call below may write to freed memory and corrupt
5380 // its metadata. See
5381 // https://github.com/google/sanitizers/issues/321.
5382 float res = REAL(lgammaf_r)(x, signp);
5383 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5386 #define INIT_LGAMMA_R \
5387 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
5388 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
5390 #define INIT_LGAMMA_R
5393 #if SANITIZER_INTERCEPT_LGAMMAL_R
5394 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5396 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5397 // FIXME: under ASan the call below may write to freed memory and corrupt
5398 // its metadata. See
5399 // https://github.com/google/sanitizers/issues/321.
5400 long double res = REAL(lgammal_r)(x, signp);
5401 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5404 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5406 #define INIT_LGAMMAL_R
5409 #if SANITIZER_INTERCEPT_DRAND48_R
5410 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5412 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5413 // FIXME: under ASan the call below may write to freed memory and corrupt
5414 // its metadata. See
5415 // https://github.com/google/sanitizers/issues/321.
5416 int res = REAL(drand48_r)(buffer, result);
5417 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5420 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5422 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5423 // FIXME: under ASan the call below may write to freed memory and corrupt
5424 // its metadata. See
5425 // https://github.com/google/sanitizers/issues/321.
5426 int res = REAL(lrand48_r)(buffer, result);
5427 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5430 #define INIT_DRAND48_R \
5431 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5432 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5434 #define INIT_DRAND48_R
5437 #if SANITIZER_INTERCEPT_RAND_R
5438 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5440 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5441 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5442 return REAL(rand_r)(seedp);
5444 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5449 #if SANITIZER_INTERCEPT_GETLINE
5450 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5452 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5453 // FIXME: under ASan the call below may write to freed memory and corrupt
5454 // its metadata. See
5455 // https://github.com/google/sanitizers/issues/321.
5456 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5459 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5465 // FIXME: under ASan the call below may write to freed memory and corrupt its
5467 // https://github.com/google/sanitizers/issues/321.
5468 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5471 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5472 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5474 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5475 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5481 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5483 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5485 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5486 // with its own body.
5487 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5489 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5491 #define INIT_GETLINE \
5492 COMMON_INTERCEPT_FUNCTION(getline); \
5493 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5494 COMMON_INTERCEPT_FUNCTION(getdelim);
5496 #define INIT_GETLINE
5499 #if SANITIZER_INTERCEPT_ICONV
5500 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5501 char **outbuf, SIZE_T *outbytesleft) {
5503 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5506 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5507 if (inbuf && inbytesleft)
5508 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5510 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5511 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5512 // FIXME: under ASan the call below may write to freed memory and corrupt
5513 // its metadata. See
5514 // https://github.com/google/sanitizers/issues/321.
5515 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5516 if (outbuf && *outbuf > outbuf_orig) {
5517 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5518 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5522 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5527 #if SANITIZER_INTERCEPT_TIMES
5528 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5530 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5531 // FIXME: under ASan the call below may write to freed memory and corrupt
5532 // its metadata. See
5533 // https://github.com/google/sanitizers/issues/321.
5534 __sanitizer_clock_t res = REAL(times)(tms);
5535 if (res != (__sanitizer_clock_t)-1 && tms)
5536 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5539 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5544 #if SANITIZER_S390 && \
5545 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5546 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5547 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5550 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5552 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5553 // If you see any crashes around this functions, there are 2 known issues with
5554 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5555 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5556 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5557 // to access thread local variables (it should not happen normally,
5558 // because sanitizers use initial-exec tls model).
5559 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5561 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5562 void *res = REAL(__tls_get_addr)(arg);
5563 uptr tls_begin, tls_end;
5564 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5565 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5567 // New DTLS block has been allocated.
5568 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5573 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5574 // mostly the same semantics as __tls_get_addr, but its presence enables
5575 // some optimizations in linker (which are safe to ignore here).
5576 INTERCEPTOR(void *, __tls_get_addr_opt, void *arg) ALIAS(WRAP(__tls_get_addr));
5578 #else // SANITIZER_S390
5579 // On s390, we have to intercept two functions here:
5580 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5581 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5582 // a proper pointer. It is used by dlsym for TLS symbols.
5583 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5584 // descriptor offset as an argument instead of a pointer. GOT address
5585 // is passed in r12, so it's necessary to write it in assembly. This is
5586 // the function used by the compiler.
5587 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5588 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5590 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5591 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5592 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5593 void *ptr = reinterpret_cast<void *>(res + tp);
5594 uptr tls_begin, tls_end;
5595 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5596 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5598 // New DTLS block has been allocated.
5599 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5603 #endif // SANITIZER_S390
5605 #define INIT_TLS_GET_ADDR
5608 #if SANITIZER_S390 && \
5609 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5610 // We need a hidden symbol aliasing the above, so that we can jump
5611 // directly to it from the assembly below.
5612 extern "C" __attribute__((visibility("hidden"))) uptr __tls_get_addr_hidden(
5613 void *arg) ALIAS(WRAP(__tls_get_addr_internal));
5614 extern "C" uptr __tls_get_offset(void *arg);
5615 extern "C" uptr TRAMPOLINE(__tls_get_offset)(void *arg);
5616 extern "C" uptr WRAP(__tls_get_offset)(void *arg);
5617 // Now carefully intercept __tls_get_offset.
5620 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5621 // exports our symbol.
5622 ".weak __tls_get_offset\n"
5623 ".set __tls_get_offset, __interceptor___tls_get_offset\n"
5624 ".global __interceptor___tls_get_offset\n"
5625 ".type __interceptor___tls_get_offset, @function\n"
5626 "__interceptor___tls_get_offset:\n"
5628 "la %r2, 0(%r2,%r12)\n"
5629 "jg __tls_get_addr_hidden\n"
5632 "0: la %r2,0(%r2,%r12)\n"
5633 "l %r4,1f-0b(%r3)\n"
5635 "1: .long __tls_get_addr_hidden - 0b\n"
5637 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5638 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5639 ".type __tls_get_offset_wrapper, @function\n"
5640 "__tls_get_offset_wrapper:\n"
5647 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5651 #if SANITIZER_INTERCEPT_LISTXATTR
5652 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5654 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5655 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5656 // FIXME: under ASan the call below may write to freed memory and corrupt
5657 // its metadata. See
5658 // https://github.com/google/sanitizers/issues/321.
5659 SSIZE_T res = REAL(listxattr)(path, list, size);
5660 // Here and below, size == 0 is a special case where nothing is written to the
5661 // buffer, and res contains the desired buffer size.
5662 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5665 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5667 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5668 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5669 // FIXME: under ASan the call below may write to freed memory and corrupt
5670 // its metadata. See
5671 // https://github.com/google/sanitizers/issues/321.
5672 SSIZE_T res = REAL(llistxattr)(path, list, size);
5673 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5676 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5678 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5679 // FIXME: under ASan the call below may write to freed memory and corrupt
5680 // its metadata. See
5681 // https://github.com/google/sanitizers/issues/321.
5682 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5683 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5686 #define INIT_LISTXATTR \
5687 COMMON_INTERCEPT_FUNCTION(listxattr); \
5688 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5689 COMMON_INTERCEPT_FUNCTION(flistxattr);
5691 #define INIT_LISTXATTR
5694 #if SANITIZER_INTERCEPT_GETXATTR
5695 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5698 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5699 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5700 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5701 // FIXME: under ASan the call below may write to freed memory and corrupt
5702 // its metadata. See
5703 // https://github.com/google/sanitizers/issues/321.
5704 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5705 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5708 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5711 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5712 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5713 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5714 // FIXME: under ASan the call below may write to freed memory and corrupt
5715 // its metadata. See
5716 // https://github.com/google/sanitizers/issues/321.
5717 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5718 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5721 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5724 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5725 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5726 // FIXME: under ASan the call below may write to freed memory and corrupt
5727 // its metadata. See
5728 // https://github.com/google/sanitizers/issues/321.
5729 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5730 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5733 #define INIT_GETXATTR \
5734 COMMON_INTERCEPT_FUNCTION(getxattr); \
5735 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5736 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5738 #define INIT_GETXATTR
5741 #if SANITIZER_INTERCEPT_GETRESID
5742 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5744 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5745 // FIXME: under ASan the call below may write to freed memory and corrupt
5746 // its metadata. See
5747 // https://github.com/google/sanitizers/issues/321.
5748 int res = REAL(getresuid)(ruid, euid, suid);
5750 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5751 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5752 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5756 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5758 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5759 // FIXME: under ASan the call below may write to freed memory and corrupt
5760 // its metadata. See
5761 // https://github.com/google/sanitizers/issues/321.
5762 int res = REAL(getresgid)(rgid, egid, sgid);
5764 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5765 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5766 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5770 #define INIT_GETRESID \
5771 COMMON_INTERCEPT_FUNCTION(getresuid); \
5772 COMMON_INTERCEPT_FUNCTION(getresgid);
5774 #define INIT_GETRESID
5777 #if SANITIZER_INTERCEPT_GETIFADDRS
5778 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5779 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5780 // intercept it to poison the memory again.
5781 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5783 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5784 // FIXME: under ASan the call below may write to freed memory and corrupt
5785 // its metadata. See
5786 // https://github.com/google/sanitizers/issues/321.
5787 int res = REAL(getifaddrs)(ifap);
5788 if (res == 0 && ifap) {
5789 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5790 __sanitizer_ifaddrs *p = *ifap;
5792 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5795 internal_strlen(p->ifa_name) + 1);
5797 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5799 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5800 // On Linux this is a union, but the other member also points to a
5801 // struct sockaddr, so the following is sufficient.
5803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5804 // FIXME(smatveev): Unpoison p->ifa_data as well.
5810 #define INIT_GETIFADDRS \
5811 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5813 #define INIT_GETIFADDRS
5816 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5817 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5819 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5820 // FIXME: under ASan the call below may write to freed memory and corrupt
5821 // its metadata. See
5822 // https://github.com/google/sanitizers/issues/321.
5823 char *res = REAL(if_indextoname)(ifindex, ifname);
5825 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5828 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5830 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5832 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5833 return REAL(if_nametoindex)(ifname);
5835 #define INIT_IF_INDEXTONAME \
5836 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5837 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5839 #define INIT_IF_INDEXTONAME
5842 #if SANITIZER_INTERCEPT_CAPGET
5843 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5845 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5847 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5848 // FIXME: under ASan the call below may write to freed memory and corrupt
5849 // its metadata. See
5850 // https://github.com/google/sanitizers/issues/321.
5851 int res = REAL(capget)(hdrp, datap);
5852 if (res == 0 && datap) {
5853 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, datasz);
5856 // We can also return -1 and write to hdrp->version if the version passed in
5857 // hdrp->version is unsupported. But that's not a trivial condition to check,
5858 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5861 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5863 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5865 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5867 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5868 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, datasz);
5870 return REAL(capset)(hdrp, datap);
5872 #define INIT_CAPGET \
5873 COMMON_INTERCEPT_FUNCTION(capget); \
5874 COMMON_INTERCEPT_FUNCTION(capset);
5879 #if SANITIZER_INTERCEPT_FTIME
5880 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5882 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5883 // FIXME: under ASan the call below may write to freed memory and corrupt
5884 // its metadata. See
5885 // https://github.com/google/sanitizers/issues/321.
5886 int res = REAL(ftime)(tp);
5888 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5891 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5894 #endif // SANITIZER_INTERCEPT_FTIME
5896 #if SANITIZER_INTERCEPT_XDR
5897 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5898 unsigned size, int op) {
5900 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5901 // FIXME: under ASan the call below may write to freed memory and corrupt
5902 // its metadata. See
5903 // https://github.com/google/sanitizers/issues/321.
5904 REAL(xdrmem_create)(xdrs, addr, size, op);
5905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5906 if (op == __sanitizer_XDR_ENCODE) {
5907 // It's not obvious how much data individual xdr_ routines write.
5908 // Simply unpoison the entire target buffer in advance.
5909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5913 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5915 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5916 // FIXME: under ASan the call below may write to freed memory and corrupt
5917 // its metadata. See
5918 // https://github.com/google/sanitizers/issues/321.
5919 REAL(xdrstdio_create)(xdrs, file, op);
5920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5923 // FIXME: under ASan the call below may write to freed memory and corrupt
5924 // its metadata. See
5925 // https://github.com/google/sanitizers/issues/321.
5926 #define XDR_INTERCEPTOR(F, T) \
5927 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5929 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5930 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5931 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5932 int res = REAL(F)(xdrs, p); \
5933 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5934 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5938 XDR_INTERCEPTOR(xdr_short, short)
5939 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5940 XDR_INTERCEPTOR(xdr_int, int)
5941 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5942 XDR_INTERCEPTOR(xdr_long, long)
5943 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5944 XDR_INTERCEPTOR(xdr_hyper, long long)
5945 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5946 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5947 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5948 XDR_INTERCEPTOR(xdr_int8_t, u8)
5949 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5950 XDR_INTERCEPTOR(xdr_int16_t, u16)
5951 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5952 XDR_INTERCEPTOR(xdr_int32_t, u32)
5953 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5954 XDR_INTERCEPTOR(xdr_int64_t, u64)
5955 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5956 XDR_INTERCEPTOR(xdr_quad_t, long long)
5957 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5958 XDR_INTERCEPTOR(xdr_bool, bool)
5959 XDR_INTERCEPTOR(xdr_enum, int)
5960 XDR_INTERCEPTOR(xdr_char, char)
5961 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5962 XDR_INTERCEPTOR(xdr_float, float)
5963 XDR_INTERCEPTOR(xdr_double, double)
5965 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5966 // wrapstring, sizeof
5968 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5971 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5972 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5973 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5974 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5975 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5977 // FIXME: under ASan the call below may write to freed memory and corrupt
5978 // its metadata. See
5979 // https://github.com/google/sanitizers/issues/321.
5980 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5981 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5984 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5989 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5992 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5993 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5994 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5995 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, internal_strlen(*p) + 1);
5997 // FIXME: under ASan the call below may write to freed memory and corrupt
5998 // its metadata. See
5999 // https://github.com/google/sanitizers/issues/321.
6000 int res = REAL(xdr_string)(xdrs, p, maxsize);
6001 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
6002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
6004 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
6010 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
6011 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
6012 COMMON_INTERCEPT_FUNCTION(xdr_short); \
6013 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
6014 COMMON_INTERCEPT_FUNCTION(xdr_int); \
6015 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
6016 COMMON_INTERCEPT_FUNCTION(xdr_long); \
6017 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
6018 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
6019 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
6020 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
6021 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
6022 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
6023 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
6024 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
6025 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
6026 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
6027 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
6028 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
6029 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
6030 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
6031 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
6032 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
6033 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
6034 COMMON_INTERCEPT_FUNCTION(xdr_char); \
6035 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
6036 COMMON_INTERCEPT_FUNCTION(xdr_float); \
6037 COMMON_INTERCEPT_FUNCTION(xdr_double); \
6038 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
6039 COMMON_INTERCEPT_FUNCTION(xdr_string);
6042 #endif // SANITIZER_INTERCEPT_XDR
6044 #if SANITIZER_INTERCEPT_XDRREC
6045 typedef int (*xdrrec_cb)(char*, char*, int);
6046 struct XdrRecWrapper {
6050 typedef AddrHashMap<XdrRecWrapper *, 11> XdrRecWrapMap;
6051 static XdrRecWrapMap *xdrrec_wrap_map;
6053 static int xdrrec_wr_wrap(char *handle, char *buf, int count) {
6054 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6055 COMMON_INTERCEPTOR_INITIALIZE_RANGE(buf, count);
6056 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
6057 return wrap->wr(wrap->handle, buf, count);
6060 static int xdrrec_rd_wrap(char *handle, char *buf, int count) {
6061 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6062 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
6063 return wrap->rd(wrap->handle, buf, count);
6066 // This doesn't apply to the solaris version as it has a different function
6068 INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
6069 unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
6070 int (*wr)(char*, char*, int)) {
6072 COMMON_INTERCEPTOR_ENTER(ctx, xdrrec_create, xdr, sndsize, rcvsize,
6074 COMMON_INTERCEPTOR_READ_RANGE(ctx, &xdr->x_op, sizeof xdr->x_op);
6076 // We can't allocate a wrapper on the stack, as the handle is used outside
6077 // this stack frame. So we put it on the heap, and keep track of it with
6078 // the HashMap (keyed by x_private). When we later need to xdr_destroy,
6079 // we can index the map, free the wrapper, and then clean the map entry.
6080 XdrRecWrapper *wrap_data =
6081 (XdrRecWrapper *)InternalAlloc(sizeof(XdrRecWrapper));
6082 wrap_data->handle = handle;
6086 wr = xdrrec_wr_wrap;
6088 rd = xdrrec_rd_wrap;
6089 handle = (char *)wrap_data;
6091 REAL(xdrrec_create)(xdr, sndsize, rcvsize, handle, rd, wr);
6092 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdr, sizeof *xdr);
6094 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, false, true);
6098 // We have to intercept this to be able to free wrapper memory;
6099 // otherwise it's not necessary.
6100 INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {
6102 COMMON_INTERCEPTOR_ENTER(ctx, xdr_destroy, xdr);
6104 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, true);
6105 InternalFree(*wrap);
6106 REAL(xdr_destroy)(xdr);
6108 #define INIT_XDRREC_LINUX \
6109 static u64 xdrrec_wrap_mem[sizeof(XdrRecWrapMap) / sizeof(u64) + 1]; \
6110 xdrrec_wrap_map = new ((void *)&xdrrec_wrap_mem) XdrRecWrapMap(); \
6111 COMMON_INTERCEPT_FUNCTION(xdrrec_create); \
6112 COMMON_INTERCEPT_FUNCTION(xdr_destroy);
6114 #define INIT_XDRREC_LINUX
6117 #if SANITIZER_INTERCEPT_TSEARCH
6118 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
6119 int (*compar)(const void *, const void *)) {
6121 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
6122 // FIXME: under ASan the call below may write to freed memory and corrupt
6123 // its metadata. See
6124 // https://github.com/google/sanitizers/issues/321.
6125 void *res = REAL(tsearch)(key, rootp, compar);
6126 if (res && *(void **)res == key)
6127 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
6130 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
6132 #define INIT_TSEARCH
6135 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
6136 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6137 void unpoison_file(__sanitizer_FILE *fp) {
6138 #if SANITIZER_HAS_STRUCT_FILE
6139 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
6140 #if SANITIZER_NETBSD
6141 if (fp->_bf._base && fp->_bf._size > 0)
6142 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
6145 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
6146 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
6147 fp->_IO_read_end - fp->_IO_read_base);
6148 if (fp->_IO_write_base && fp->_IO_write_base < fp->_IO_write_end)
6149 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_write_base,
6150 fp->_IO_write_end - fp->_IO_write_base);
6152 #endif // SANITIZER_HAS_STRUCT_FILE
6156 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
6157 // These guys are called when a .c source is built with -O2.
6158 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
6160 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
6161 int res = REAL(__uflow)(fp);
6165 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
6167 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
6168 int res = REAL(__underflow)(fp);
6172 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
6174 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
6175 int res = REAL(__overflow)(fp, ch);
6179 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
6181 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
6182 int res = REAL(__wuflow)(fp);
6186 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
6188 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
6189 int res = REAL(__wunderflow)(fp);
6193 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
6195 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
6196 int res = REAL(__woverflow)(fp, ch);
6200 #define INIT_LIBIO_INTERNALS \
6201 COMMON_INTERCEPT_FUNCTION(__uflow); \
6202 COMMON_INTERCEPT_FUNCTION(__underflow); \
6203 COMMON_INTERCEPT_FUNCTION(__overflow); \
6204 COMMON_INTERCEPT_FUNCTION(__wuflow); \
6205 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
6206 COMMON_INTERCEPT_FUNCTION(__woverflow);
6208 #define INIT_LIBIO_INTERNALS
6211 #if SANITIZER_INTERCEPT_FOPEN
6212 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
6214 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
6215 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6216 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6217 __sanitizer_FILE *res = REAL(fopen)(path, mode);
6218 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6219 if (res) unpoison_file(res);
6222 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
6224 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
6225 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6226 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
6227 if (res) unpoison_file(res);
6230 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
6231 __sanitizer_FILE *fp) {
6233 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
6234 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6235 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6236 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6237 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
6238 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6239 if (res) unpoison_file(res);
6242 #define INIT_FOPEN \
6243 COMMON_INTERCEPT_FUNCTION(fopen); \
6244 COMMON_INTERCEPT_FUNCTION(fdopen); \
6245 COMMON_INTERCEPT_FUNCTION(freopen);
6250 #if SANITIZER_INTERCEPT_FLOPEN
6251 INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
6254 va_start(ap, flags);
6255 u16 mode = static_cast<u16>(va_arg(ap, u32));
6257 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6259 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6261 return COMMON_INTERCEPTOR_BLOCK_REAL(flopen)(path, flags, mode);
6264 INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
6267 va_start(ap, flags);
6268 u16 mode = static_cast<u16>(va_arg(ap, u32));
6270 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6272 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6274 return COMMON_INTERCEPTOR_BLOCK_REAL(flopenat)(dirfd, path, flags, mode);
6277 #define INIT_FLOPEN \
6278 COMMON_INTERCEPT_FUNCTION(flopen); \
6279 COMMON_INTERCEPT_FUNCTION(flopenat);
6284 #if SANITIZER_INTERCEPT_FOPEN64
6285 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
6287 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
6288 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6289 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6290 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
6291 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6292 if (res) unpoison_file(res);
6295 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
6296 __sanitizer_FILE *fp) {
6298 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
6299 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6300 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6301 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6302 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
6303 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6304 if (res) unpoison_file(res);
6307 #define INIT_FOPEN64 \
6308 COMMON_INTERCEPT_FUNCTION(fopen64); \
6309 COMMON_INTERCEPT_FUNCTION(freopen64);
6311 #define INIT_FOPEN64
6314 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6315 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
6317 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
6318 // FIXME: under ASan the call below may write to freed memory and corrupt
6319 // its metadata. See
6320 // https://github.com/google/sanitizers/issues/321.
6321 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
6323 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6326 FileMetadata file = {ptr, sizeloc};
6327 SetInterceptorMetadata(res, file);
6331 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
6334 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
6335 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
6337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6338 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6340 FileMetadata file = {(char **)ptr, sizeloc};
6341 SetInterceptorMetadata(res, file);
6345 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
6348 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
6349 // FIXME: under ASan the call below may write to freed memory and corrupt
6350 // its metadata. See
6351 // https://github.com/google/sanitizers/issues/321.
6352 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
6353 if (res) unpoison_file(res);
6356 #define INIT_OPEN_MEMSTREAM \
6357 COMMON_INTERCEPT_FUNCTION(open_memstream); \
6358 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
6359 COMMON_INTERCEPT_FUNCTION(fmemopen);
6361 #define INIT_OPEN_MEMSTREAM
6364 #if SANITIZER_INTERCEPT_OBSTACK
6365 static void initialize_obstack(__sanitizer_obstack *obstack) {
6366 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
6368 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
6369 sizeof(*obstack->chunk));
6372 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
6373 int align, void *(*alloc_fn)(uptr arg, uptr sz),
6374 void (*free_fn)(uptr arg, void *p)) {
6376 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
6378 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
6379 if (res) initialize_obstack(obstack);
6382 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
6383 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
6385 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
6387 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
6388 if (res) initialize_obstack(obstack);
6391 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
6393 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
6394 REAL(_obstack_newchunk)(obstack, length);
6396 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
6397 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
6399 #define INIT_OBSTACK \
6400 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
6401 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
6402 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
6404 #define INIT_OBSTACK
6407 #if SANITIZER_INTERCEPT_FFLUSH
6408 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
6410 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
6413 int res = REAL(fflush)(fp);
6414 // FIXME: handle fp == NULL
6416 const FileMetadata *m = GetInterceptorMetadata(fp);
6417 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6421 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6426 #if SANITIZER_INTERCEPT_FCLOSE
6427 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6429 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6430 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6431 const FileMetadata *m = GetInterceptorMetadata(fp);
6434 int res = REAL(fclose)(fp);
6436 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6437 DeleteInterceptorMetadata(fp);
6441 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6446 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6447 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6449 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6452 COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6454 # if !SANITIZER_DYNAMIC
6455 // We care about a very specific use-case: dladdr on
6456 // statically-linked ASan may return <main program>
6457 // instead of the library.
6458 // We therefore only take effect if the sanitizer is statically
6459 // linked, and we don't bother canonicalizing paths because
6460 // dladdr should return the same address both times (we assume
6461 // the user did not canonicalize the result from dladdr).
6462 if (common_flags()->test_only_replace_dlopen_main_program) {
6463 VPrintf(1, "dlopen interceptor: filename: %s\n", filename);
6465 const char *SelfFName = DladdrSelfFName();
6466 VPrintf(1, "dlopen interceptor: DladdrSelfFName: %p %s\n",
6467 (const void *)SelfFName, SelfFName);
6469 if (SelfFName && internal_strcmp(SelfFName, filename) == 0) {
6470 // It's possible they copied the string from dladdr, so
6471 // we do a string comparison rather than pointer comparison.
6472 VPrintf(1, "dlopen interceptor: replacing %s because it matches %s\n",
6473 filename, SelfFName);
6474 filename = (char *)0; // RTLD_DEFAULT
6477 # endif // !SANITIZER_DYNAMIC
6480 void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
6481 Symbolizer::GetOrInit()->InvalidateModuleList();
6482 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6486 INTERCEPTOR(int, dlclose, void *handle) {
6488 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6489 int res = REAL(dlclose)(handle);
6490 Symbolizer::GetOrInit()->InvalidateModuleList();
6491 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6494 #define INIT_DLOPEN_DLCLOSE \
6495 COMMON_INTERCEPT_FUNCTION(dlopen); \
6496 COMMON_INTERCEPT_FUNCTION(dlclose);
6498 #define INIT_DLOPEN_DLCLOSE
6501 #if SANITIZER_INTERCEPT_GETPASS
6502 INTERCEPTOR(char *, getpass, const char *prompt) {
6504 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6506 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
6507 char *res = REAL(getpass)(prompt);
6508 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
6512 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6514 #define INIT_GETPASS
6517 #if SANITIZER_INTERCEPT_TIMERFD
6518 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6521 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6523 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6524 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6525 if (res != -1 && old_value)
6526 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6530 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6532 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6533 int res = REAL(timerfd_gettime)(fd, curr_value);
6534 if (res != -1 && curr_value)
6535 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6538 #define INIT_TIMERFD \
6539 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6540 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6542 #define INIT_TIMERFD
6545 #if SANITIZER_INTERCEPT_MLOCKX
6546 // Linux kernel has a bug that leads to kernel deadlock if a process
6547 // maps TBs of memory and then calls mlock().
6548 static void MlockIsUnsupported() {
6549 static atomic_uint8_t printed;
6550 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6552 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6556 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6557 MlockIsUnsupported();
6561 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6562 MlockIsUnsupported();
6566 INTERCEPTOR(int, mlockall, int flags) {
6567 MlockIsUnsupported();
6571 INTERCEPTOR(int, munlockall, void) {
6572 MlockIsUnsupported();
6576 #define INIT_MLOCKX \
6577 COMMON_INTERCEPT_FUNCTION(mlock); \
6578 COMMON_INTERCEPT_FUNCTION(munlock); \
6579 COMMON_INTERCEPT_FUNCTION(mlockall); \
6580 COMMON_INTERCEPT_FUNCTION(munlockall);
6584 #endif // SANITIZER_INTERCEPT_MLOCKX
6586 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6587 struct WrappedCookie {
6589 __sanitizer_cookie_io_functions_t real_io_funcs;
6592 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6593 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6594 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6595 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6596 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6599 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6600 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6601 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6602 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6603 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6606 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6607 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6608 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6609 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6610 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6611 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6615 static int wrapped_close(void *cookie) {
6616 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6617 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6618 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6619 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6620 InternalFree(wrapped_cookie);
6624 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6625 __sanitizer_cookie_io_functions_t io_funcs) {
6627 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6628 WrappedCookie *wrapped_cookie =
6629 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6630 wrapped_cookie->real_cookie = cookie;
6631 wrapped_cookie->real_io_funcs = io_funcs;
6632 __sanitizer_FILE *res =
6633 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6634 wrapped_seek, wrapped_close});
6638 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6640 #define INIT_FOPENCOOKIE
6641 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6643 #if SANITIZER_INTERCEPT_SEM
6644 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6646 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6647 // Workaround a bug in glibc's "old" semaphore implementation by
6648 // zero-initializing the sem_t contents. This has to be done here because
6649 // interceptors bind to the lowest version before glibc 2.36, hitting the
6650 // buggy code path while the non-sanitized build of the same code works fine.
6651 REAL(memset)(s, 0, sizeof(*s));
6652 int res = REAL(sem_init)(s, pshared, value);
6656 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6658 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6659 int res = REAL(sem_destroy)(s);
6663 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6665 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6666 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6668 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6673 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6675 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6676 int res = REAL(sem_trywait)(s);
6678 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6683 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6685 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6686 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6687 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6689 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6694 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6696 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6697 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6698 int res = REAL(sem_post)(s);
6702 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6704 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6705 int res = REAL(sem_getvalue)(s, sval);
6707 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6713 INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
6716 va_start(ap, oflag);
6717 u32 mode = va_arg(ap, u32);
6718 u32 value = va_arg(ap, u32);
6719 COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
6720 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6721 __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
6723 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
6728 INTERCEPTOR(int, sem_unlink, const char *name) {
6730 COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
6731 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6732 return REAL(sem_unlink)(name);
6736 COMMON_INTERCEPT_FUNCTION(sem_init); \
6737 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6738 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6739 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6740 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6741 COMMON_INTERCEPT_FUNCTION(sem_post); \
6742 COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
6743 COMMON_INTERCEPT_FUNCTION(sem_open); \
6744 COMMON_INTERCEPT_FUNCTION(sem_unlink);
6747 #endif // SANITIZER_INTERCEPT_SEM
6749 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6750 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6752 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6753 int res = REAL(pthread_setcancelstate)(state, oldstate);
6754 if (res == 0 && oldstate != nullptr)
6755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6759 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6761 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6762 int res = REAL(pthread_setcanceltype)(type, oldtype);
6763 if (res == 0 && oldtype != nullptr)
6764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6767 #define INIT_PTHREAD_SETCANCEL \
6768 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6769 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6771 #define INIT_PTHREAD_SETCANCEL
6774 #if SANITIZER_INTERCEPT_MINCORE
6775 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6777 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6778 int res = REAL(mincore)(addr, length, vec);
6780 uptr page_size = GetPageSizeCached();
6781 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6782 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6786 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6788 #define INIT_MINCORE
6791 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6792 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6793 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6796 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6797 remote_iov, riovcnt, flags);
6798 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6801 write_iovec(ctx, local_iov, liovcnt, res);
6805 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6806 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6809 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6810 remote_iov, riovcnt, flags);
6811 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6814 read_iovec(ctx, local_iov, liovcnt, res);
6817 #define INIT_PROCESS_VM_READV \
6818 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6819 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6821 #define INIT_PROCESS_VM_READV
6824 #if SANITIZER_INTERCEPT_CTERMID
6825 INTERCEPTOR(char *, ctermid, char *s) {
6827 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6828 char *res = REAL(ctermid)(s);
6830 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6834 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6836 #define INIT_CTERMID
6839 #if SANITIZER_INTERCEPT_CTERMID_R
6840 INTERCEPTOR(char *, ctermid_r, char *s) {
6842 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6843 char *res = REAL(ctermid_r)(s);
6845 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6849 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6851 #define INIT_CTERMID_R
6854 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6855 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6857 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6858 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6859 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recv)(fd, buf, len, flags);
6861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6863 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6867 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6868 void *srcaddr, int *addrlen) {
6870 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6872 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6874 if (srcaddr) srcaddr_sz = *addrlen;
6875 (void)srcaddr_sz; // prevent "set but not used" warning
6876 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recvfrom)(fd, buf, len, flags,
6879 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6880 if (res >= 0 && srcaddr)
6881 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6882 Min((SIZE_T)*addrlen, srcaddr_sz));
6885 #define INIT_RECV_RECVFROM \
6886 COMMON_INTERCEPT_FUNCTION(recv); \
6887 COMMON_INTERCEPT_FUNCTION(recvfrom);
6889 #define INIT_RECV_RECVFROM
6892 #if SANITIZER_INTERCEPT_SEND_SENDTO
6893 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6895 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6897 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6898 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6900 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(send)(fd, buf, len, flags);
6901 if (common_flags()->intercept_send && res > 0)
6902 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6906 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6907 void *dstaddr, int addrlen) {
6909 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6911 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6912 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6914 // Can't check dstaddr as it may have uninitialized padding at the end.
6915 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(sendto)(fd, buf, len, flags,
6917 if (common_flags()->intercept_send && res > 0)
6918 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6921 #define INIT_SEND_SENDTO \
6922 COMMON_INTERCEPT_FUNCTION(send); \
6923 COMMON_INTERCEPT_FUNCTION(sendto);
6925 #define INIT_SEND_SENDTO
6928 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6929 INTERCEPTOR(int, eventfd_read, int fd, __sanitizer_eventfd_t *value) {
6931 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6932 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6933 int res = COMMON_INTERCEPTOR_BLOCK_REAL(eventfd_read)(fd, value);
6935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6936 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6940 INTERCEPTOR(int, eventfd_write, int fd, __sanitizer_eventfd_t value) {
6942 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6944 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6945 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6947 int res = COMMON_INTERCEPTOR_BLOCK_REAL(eventfd_write)(fd, value);
6950 #define INIT_EVENTFD_READ_WRITE \
6951 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6952 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6954 #define INIT_EVENTFD_READ_WRITE
6957 #if SANITIZER_INTERCEPT_STAT
6958 INTERCEPTOR(int, stat, const char *path, void *buf) {
6960 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6961 if (common_flags()->intercept_stat)
6962 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6963 int res = REAL(stat)(path, buf);
6965 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6968 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6973 #if SANITIZER_INTERCEPT_STAT64
6974 INTERCEPTOR(int, stat64, const char *path, void *buf) {
6976 COMMON_INTERCEPTOR_ENTER(ctx, stat64, path, buf);
6977 if (common_flags()->intercept_stat)
6978 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6979 int res = REAL(stat64)(path, buf);
6981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6984 #define INIT_STAT64 COMMON_INTERCEPT_FUNCTION(stat64)
6990 #if SANITIZER_INTERCEPT_LSTAT
6991 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6993 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6994 if (common_flags()->intercept_stat)
6995 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6996 int res = REAL(lstat)(path, buf);
6998 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7001 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
7006 #if SANITIZER_INTERCEPT_STAT64
7007 INTERCEPTOR(int, lstat64, const char *path, void *buf) {
7009 COMMON_INTERCEPTOR_ENTER(ctx, lstat64, path, buf);
7010 if (common_flags()->intercept_stat)
7011 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7012 int res = REAL(lstat64)(path, buf);
7014 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7017 #define INIT_LSTAT64 COMMON_INTERCEPT_FUNCTION(lstat64)
7019 #define INIT_LSTAT64
7022 #if SANITIZER_INTERCEPT___XSTAT
7023 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
7025 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
7026 if (common_flags()->intercept_stat)
7027 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7028 int res = REAL(__xstat)(version, path, buf);
7030 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7033 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
7035 #define INIT___XSTAT
7038 #if SANITIZER_INTERCEPT___XSTAT64
7039 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
7041 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
7042 if (common_flags()->intercept_stat)
7043 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7044 int res = REAL(__xstat64)(version, path, buf);
7046 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7049 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
7051 #define INIT___XSTAT64
7054 #if SANITIZER_INTERCEPT___LXSTAT
7055 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
7057 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
7058 if (common_flags()->intercept_stat)
7059 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7060 int res = REAL(__lxstat)(version, path, buf);
7062 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7065 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
7067 #define INIT___LXSTAT
7070 #if SANITIZER_INTERCEPT___LXSTAT64
7071 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
7073 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
7074 if (common_flags()->intercept_stat)
7075 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7076 int res = REAL(__lxstat64)(version, path, buf);
7078 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7081 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
7083 #define INIT___LXSTAT64
7086 // FIXME: add other *stat interceptor
7088 #if SANITIZER_INTERCEPT_UTMP
7089 INTERCEPTOR(void *, getutent, int dummy) {
7091 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
7092 void *res = REAL(getutent)(dummy);
7094 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7097 INTERCEPTOR(void *, getutid, void *ut) {
7099 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
7100 void *res = REAL(getutid)(ut);
7102 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7105 INTERCEPTOR(void *, getutline, void *ut) {
7107 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
7108 void *res = REAL(getutline)(ut);
7110 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7114 COMMON_INTERCEPT_FUNCTION(getutent); \
7115 COMMON_INTERCEPT_FUNCTION(getutid); \
7116 COMMON_INTERCEPT_FUNCTION(getutline);
7121 #if SANITIZER_INTERCEPT_UTMPX
7122 INTERCEPTOR(void *, getutxent, int dummy) {
7124 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
7125 void *res = REAL(getutxent)(dummy);
7127 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7130 INTERCEPTOR(void *, getutxid, void *ut) {
7132 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
7133 void *res = REAL(getutxid)(ut);
7135 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7138 INTERCEPTOR(void *, getutxline, void *ut) {
7140 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
7141 void *res = REAL(getutxline)(ut);
7143 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7146 INTERCEPTOR(void *, pututxline, const void *ut) {
7148 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
7150 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
7151 void *res = REAL(pututxline)(ut);
7153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
7156 #define INIT_UTMPX \
7157 COMMON_INTERCEPT_FUNCTION(getutxent); \
7158 COMMON_INTERCEPT_FUNCTION(getutxid); \
7159 COMMON_INTERCEPT_FUNCTION(getutxline); \
7160 COMMON_INTERCEPT_FUNCTION(pututxline);
7165 #if SANITIZER_INTERCEPT_GETLOADAVG
7166 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
7168 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
7169 int res = REAL(getloadavg)(loadavg, nelem);
7171 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
7174 #define INIT_GETLOADAVG \
7175 COMMON_INTERCEPT_FUNCTION(getloadavg);
7177 #define INIT_GETLOADAVG
7180 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
7181 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
7185 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
7189 INTERCEPTOR(int, mprobe, void *ptr) {
7194 #if SANITIZER_INTERCEPT_WCSLEN
7195 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
7197 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
7198 SIZE_T res = REAL(wcslen)(s);
7199 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
7203 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
7205 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
7206 SIZE_T res = REAL(wcsnlen)(s, n);
7207 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
7210 #define INIT_WCSLEN \
7211 COMMON_INTERCEPT_FUNCTION(wcslen); \
7212 COMMON_INTERCEPT_FUNCTION(wcsnlen);
7217 #if SANITIZER_INTERCEPT_WCSCAT
7218 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
7220 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
7221 SIZE_T src_size = internal_wcslen(src);
7222 SIZE_T dst_size = internal_wcslen(dst);
7223 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
7224 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7225 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7226 (src_size + 1) * sizeof(wchar_t));
7227 return REAL(wcscat)(dst, src);
7230 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
7232 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
7233 SIZE_T src_size = internal_wcsnlen(src, n);
7234 SIZE_T dst_size = internal_wcslen(dst);
7235 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
7236 Min(src_size + 1, n) * sizeof(wchar_t));
7237 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7238 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7239 (src_size + 1) * sizeof(wchar_t));
7240 return REAL(wcsncat)(dst, src, n);
7242 #define INIT_WCSCAT \
7243 COMMON_INTERCEPT_FUNCTION(wcscat); \
7244 COMMON_INTERCEPT_FUNCTION(wcsncat);
7249 #if SANITIZER_INTERCEPT_WCSDUP
7250 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
7252 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
7253 SIZE_T len = internal_wcslen(s);
7254 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
7255 wchar_t *result = REAL(wcsdup)(s);
7257 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
7261 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
7266 #if SANITIZER_INTERCEPT_STRXFRM
7267 static SIZE_T RealStrLen(const char *str) { return internal_strlen(str); }
7269 static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(str); }
7271 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
7274 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
7275 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
7276 sizeof(*src) * (RealStrLen(src) + 1)); \
7277 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
7279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
7283 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
7284 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
7287 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
7289 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
7292 #define INIT_STRXFRM \
7293 COMMON_INTERCEPT_FUNCTION(strxfrm); \
7294 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
7296 #define INIT_STRXFRM
7299 #if SANITIZER_INTERCEPT___STRXFRM_L
7300 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
7302 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
7305 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
7307 #define INIT___STRXFRM_L
7310 #if SANITIZER_INTERCEPT_WCSXFRM
7311 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
7312 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
7315 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7317 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
7320 #define INIT_WCSXFRM \
7321 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
7322 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
7324 #define INIT_WCSXFRM
7327 #if SANITIZER_INTERCEPT___WCSXFRM_L
7328 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7330 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
7333 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
7335 #define INIT___WCSXFRM_L
7338 #if SANITIZER_INTERCEPT_ACCT
7339 INTERCEPTOR(int, acct, const char *file) {
7341 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
7343 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
7344 return REAL(acct)(file);
7346 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
7351 #if SANITIZER_INTERCEPT_USER_FROM_UID
7352 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
7355 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
7356 user = REAL(user_from_uid)(uid, nouser);
7358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
7361 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
7363 #define INIT_USER_FROM_UID
7366 #if SANITIZER_INTERCEPT_UID_FROM_USER
7367 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
7370 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
7372 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7373 res = REAL(uid_from_user)(name, uid);
7375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
7378 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
7380 #define INIT_UID_FROM_USER
7383 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
7384 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
7387 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
7388 group = REAL(group_from_gid)(gid, nogroup);
7390 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
7393 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
7395 #define INIT_GROUP_FROM_GID
7398 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
7399 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
7402 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
7404 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
7405 res = REAL(gid_from_group)(group, gid);
7407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
7410 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
7412 #define INIT_GID_FROM_GROUP
7415 #if SANITIZER_INTERCEPT_ACCESS
7416 INTERCEPTOR(int, access, const char *path, int mode) {
7418 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
7420 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7421 return REAL(access)(path, mode);
7423 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
7428 #if SANITIZER_INTERCEPT_FACCESSAT
7429 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
7431 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
7433 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7434 return REAL(faccessat)(fd, path, mode, flags);
7436 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
7438 #define INIT_FACCESSAT
7441 #if SANITIZER_INTERCEPT_GETGROUPLIST
7442 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
7446 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
7448 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7450 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
7451 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
7452 if (!res && groups && ngroups) {
7453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7459 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
7461 #define INIT_GETGROUPLIST
7464 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
7465 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
7466 int maxgrp, int *ngroups) {
7469 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
7472 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7473 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
7474 if (!res && groups && ngroups) {
7475 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7481 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
7483 #define INIT_GETGROUPMEMBERSHIP
7486 #if SANITIZER_INTERCEPT_READLINK
7487 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
7489 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
7490 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7491 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
7493 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7497 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
7499 #define INIT_READLINK
7502 #if SANITIZER_INTERCEPT_READLINKAT
7503 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
7506 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
7507 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7508 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7514 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7516 #define INIT_READLINKAT
7519 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7520 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7521 struct file_handle *handle, int *mount_id, int flags) {
7523 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7525 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
7527 __sanitizer_file_handle *sanitizer_handle =
7528 reinterpret_cast<__sanitizer_file_handle*>(handle);
7529 COMMON_INTERCEPTOR_READ_RANGE(
7530 ctx, &sanitizer_handle->handle_bytes,
7531 sizeof(sanitizer_handle->handle_bytes));
7533 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7535 COMMON_INTERCEPTOR_WRITE_RANGE(
7536 ctx, &sanitizer_handle->handle_bytes,
7537 sizeof(sanitizer_handle->handle_bytes));
7538 COMMON_INTERCEPTOR_WRITE_RANGE(
7539 ctx, &sanitizer_handle->handle_type,
7540 sizeof(sanitizer_handle->handle_type));
7541 COMMON_INTERCEPTOR_WRITE_RANGE(
7542 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7543 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7548 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7550 #define INIT_NAME_TO_HANDLE_AT
7553 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7554 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7557 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7559 __sanitizer_file_handle *sanitizer_handle =
7560 reinterpret_cast<__sanitizer_file_handle*>(handle);
7561 COMMON_INTERCEPTOR_READ_RANGE(
7562 ctx, &sanitizer_handle->handle_bytes,
7563 sizeof(sanitizer_handle->handle_bytes));
7564 COMMON_INTERCEPTOR_READ_RANGE(
7565 ctx, &sanitizer_handle->handle_type,
7566 sizeof(sanitizer_handle->handle_type));
7567 COMMON_INTERCEPTOR_READ_RANGE(
7568 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7570 return COMMON_INTERCEPTOR_BLOCK_REAL(open_by_handle_at)(mount_fd, handle,
7574 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7576 #define INIT_OPEN_BY_HANDLE_AT
7579 #if SANITIZER_INTERCEPT_STRLCPY
7580 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7583 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7585 // Keep strnlen as macro argument, as macro may ignore it.
7586 COMMON_INTERCEPTOR_READ_STRING(
7587 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7589 res = REAL(strlcpy)(dst, src, size);
7590 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
7594 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7597 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7598 // src is checked in the strlcpy() interceptor
7600 len = internal_strnlen(dst, size);
7601 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7603 // Reuse the rest of the code in the strlcpy() interceptor
7604 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7606 #define INIT_STRLCPY \
7607 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7608 COMMON_INTERCEPT_FUNCTION(strlcat);
7610 #define INIT_STRLCPY
7613 #if SANITIZER_INTERCEPT_MMAP
7614 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7617 if (common_flags()->detect_write_exec)
7618 ReportMmapWriteExec(prot, flags);
7619 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7620 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7621 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7622 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7625 INTERCEPTOR(int, munmap, void *addr, SIZE_T sz) {
7627 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7628 return (int)internal_munmap(addr, sz);
7629 COMMON_INTERCEPTOR_ENTER(ctx, munmap, addr, sz);
7630 COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz);
7633 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7635 if (common_flags()->detect_write_exec)
7636 ReportMmapWriteExec(prot, 0);
7637 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7638 return (int)internal_mprotect(addr, sz, prot);
7639 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7640 MprotectMallocZones(addr, prot);
7641 return REAL(mprotect)(addr, sz, prot);
7644 COMMON_INTERCEPT_FUNCTION(mmap); \
7645 COMMON_INTERCEPT_FUNCTION(munmap); \
7646 COMMON_INTERCEPT_FUNCTION(mprotect);
7651 #if SANITIZER_INTERCEPT_MMAP64
7652 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7655 if (common_flags()->detect_write_exec)
7656 ReportMmapWriteExec(prot, flags);
7657 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7658 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7659 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7660 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7662 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7667 #if SANITIZER_INTERCEPT_DEVNAME
7668 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7671 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7672 name = REAL(devname)(dev, type);
7674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
7677 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7679 #define INIT_DEVNAME
7682 #if SANITIZER_INTERCEPT_DEVNAME_R
7683 #if SANITIZER_NETBSD
7684 #define DEVNAME_R_RETTYPE int
7685 #define DEVNAME_R_SUCCESS(x) (!(x))
7687 #define DEVNAME_R_RETTYPE char*
7688 #define DEVNAME_R_SUCCESS(x) (x)
7690 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7693 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7694 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7695 if (DEVNAME_R_SUCCESS(res))
7696 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
7699 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7701 #define INIT_DEVNAME_R
7704 #if SANITIZER_INTERCEPT_FGETLN
7705 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7707 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7708 char *str = REAL(fgetln)(stream, len);
7710 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7715 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7720 #if SANITIZER_INTERCEPT_STRMODE
7721 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7723 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7724 REAL(strmode)(mode, bp);
7726 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
7728 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7730 #define INIT_STRMODE
7733 #if SANITIZER_INTERCEPT_TTYENT
7734 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7736 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7737 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7742 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7744 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7746 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7747 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7752 #define INIT_TTYENT \
7753 COMMON_INTERCEPT_FUNCTION(getttyent); \
7754 COMMON_INTERCEPT_FUNCTION(getttynam);
7759 #if SANITIZER_INTERCEPT_TTYENTPATH
7760 INTERCEPTOR(int, setttyentpath, char *path) {
7762 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7764 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7765 return REAL(setttyentpath)(path);
7767 #define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
7769 #define INIT_TTYENTPATH
7772 #if SANITIZER_INTERCEPT_PROTOENT
7773 static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
7774 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7776 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
7778 SIZE_T pp_size = 1; // One handles the trailing \0
7780 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7781 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
7783 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, pp_size * sizeof(char *));
7786 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent,) {
7788 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent,);
7789 struct __sanitizer_protoent *p = REAL(getprotoent)();
7791 write_protoent(ctx, p);
7795 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7797 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7799 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7800 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7802 write_protoent(ctx, p);
7806 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7808 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7809 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7811 write_protoent(ctx, p);
7814 #define INIT_PROTOENT \
7815 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7816 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7817 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7819 #define INIT_PROTOENT
7822 #if SANITIZER_INTERCEPT_PROTOENT_R
7823 INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
7824 char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
7826 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
7828 int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
7830 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7831 if (!res && *result)
7832 write_protoent(ctx, *result);
7836 INTERCEPTOR(int, getprotobyname_r, const char *name,
7837 struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
7838 struct __sanitizer_protoent **result) {
7840 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
7843 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7844 int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
7846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7847 if (!res && *result)
7848 write_protoent(ctx, *result);
7852 INTERCEPTOR(int, getprotobynumber_r, int num,
7853 struct __sanitizer_protoent *result_buf, char *buf,
7854 SIZE_T buflen, struct __sanitizer_protoent **result) {
7856 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
7858 int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
7860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7861 if (!res && *result)
7862 write_protoent(ctx, *result);
7866 #define INIT_PROTOENT_R \
7867 COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
7868 COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
7869 COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
7871 #define INIT_PROTOENT_R
7874 #if SANITIZER_INTERCEPT_NETENT
7875 INTERCEPTOR(struct __sanitizer_netent *, getnetent,) {
7877 COMMON_INTERCEPTOR_ENTER(ctx, getnetent,);
7878 struct __sanitizer_netent *n = REAL(getnetent)();
7880 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7882 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7884 SIZE_T nn_size = 1; // One handles the trailing \0
7886 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7887 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7894 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7896 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7898 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7899 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7905 SIZE_T nn_size = 1; // One handles the trailing \0
7907 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7910 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7915 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7917 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7918 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7922 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7924 SIZE_T nn_size = 1; // One handles the trailing \0
7926 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7927 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7933 #define INIT_NETENT \
7934 COMMON_INTERCEPT_FUNCTION(getnetent); \
7935 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7936 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7941 #if SANITIZER_INTERCEPT_GETMNTINFO
7942 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7944 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7945 int cnt = REAL(getmntinfo)(mntbufp, flags);
7946 if (cnt > 0 && mntbufp) {
7947 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7949 #if SANITIZER_NETBSD
7950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7957 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7959 #define INIT_GETMNTINFO
7962 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7963 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7966 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7968 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7969 REAL(mi_vector_hash)(key, len, seed, hashes);
7971 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7973 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7975 #define INIT_MI_VECTOR_HASH
7978 #if SANITIZER_INTERCEPT_SETVBUF
7979 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7982 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7983 int ret = REAL(setvbuf)(stream, buf, mode, size);
7985 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7987 unpoison_file(stream);
7991 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7993 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7994 REAL(setbuf)(stream, buf);
7996 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7999 unpoison_file(stream);
8002 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
8004 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
8005 REAL(setbuffer)(stream, buf, size);
8007 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
8010 unpoison_file(stream);
8013 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
8015 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
8016 REAL(setlinebuf)(stream);
8018 unpoison_file(stream);
8020 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
8021 COMMON_INTERCEPT_FUNCTION(setbuf); \
8022 COMMON_INTERCEPT_FUNCTION(setbuffer); \
8023 COMMON_INTERCEPT_FUNCTION(setlinebuf)
8025 #define INIT_SETVBUF
8028 #if SANITIZER_INTERCEPT_GETVFSSTAT
8029 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
8031 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
8032 int ret = REAL(getvfsstat)(buf, bufsize, flags);
8034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
8037 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
8039 #define INIT_GETVFSSTAT
8042 #if SANITIZER_INTERCEPT_REGEX
8043 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
8045 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
8047 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
8048 int res = REAL(regcomp)(preg, pattern, cflags);
8050 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
8053 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
8054 struct __sanitizer_regmatch *pmatch[], int eflags) {
8056 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
8058 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8060 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
8061 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
8063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
8066 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
8067 SIZE_T errbuf_size) {
8069 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
8071 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8072 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
8074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
8077 INTERCEPTOR(void, regfree, const void *preg) {
8079 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
8081 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8082 REAL(regfree)(preg);
8084 #define INIT_REGEX \
8085 COMMON_INTERCEPT_FUNCTION(regcomp); \
8086 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
8087 COMMON_INTERCEPT_FUNCTION(regerror); \
8088 COMMON_INTERCEPT_FUNCTION(regfree);
8093 #if SANITIZER_INTERCEPT_REGEXSUB
8094 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
8095 const struct __sanitizer_regmatch *rm, const char *str) {
8097 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
8099 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
8100 // The implementation demands and hardcodes 10 elements
8102 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
8104 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
8105 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
8107 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
8110 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
8111 const struct __sanitizer_regmatch *rm, const char *sstr) {
8113 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
8115 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
8116 // Hardcode 10 elements as this is hardcoded size
8118 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
8120 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
8121 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
8122 if (res > 0 && buf) {
8123 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
8124 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
8129 #define INIT_REGEXSUB \
8130 COMMON_INTERCEPT_FUNCTION(regnsub); \
8131 COMMON_INTERCEPT_FUNCTION(regasub);
8133 #define INIT_REGEXSUB
8136 #if SANITIZER_INTERCEPT_FTS
8137 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
8138 int (*compar)(void **, void **)) {
8140 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
8142 for (char *const *pa = path_argv; ; ++pa) {
8143 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
8146 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
8149 // TODO(kamil): handle compar callback
8150 void *fts = REAL(fts_open)(path_argv, options, compar);
8152 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
8156 INTERCEPTOR(void *, fts_read, void *ftsp) {
8158 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
8160 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8161 void *ftsent = REAL(fts_read)(ftsp);
8163 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8167 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
8169 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
8171 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8172 void *ftsent = REAL(fts_children)(ftsp, options);
8174 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8178 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
8180 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
8182 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8184 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
8185 return REAL(fts_set)(ftsp, f, options);
8188 INTERCEPTOR(int, fts_close, void *ftsp) {
8190 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
8192 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8193 return REAL(fts_close)(ftsp);
8196 COMMON_INTERCEPT_FUNCTION(fts_open); \
8197 COMMON_INTERCEPT_FUNCTION(fts_read); \
8198 COMMON_INTERCEPT_FUNCTION(fts_children); \
8199 COMMON_INTERCEPT_FUNCTION(fts_set); \
8200 COMMON_INTERCEPT_FUNCTION(fts_close);
8205 #if SANITIZER_INTERCEPT_SYSCTL
8206 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
8207 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
8209 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8210 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
8211 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
8214 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
8216 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8218 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8219 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
8222 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8230 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
8231 void *newp, SIZE_T newlen) {
8233 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8234 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
8235 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
8238 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8240 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8242 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8243 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
8246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8254 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
8257 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
8259 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8261 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8262 int res = REAL(sysctlnametomib)(sname, name, namelenp);
8265 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8267 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8273 #define INIT_SYSCTL \
8274 COMMON_INTERCEPT_FUNCTION(sysctl); \
8275 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
8276 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
8281 #if SANITIZER_INTERCEPT_ASYSCTL
8282 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
8284 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
8286 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
8287 void *res = REAL(asysctl)(name, namelen, len);
8289 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8290 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8295 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
8297 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
8299 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8300 void *res = REAL(asysctlbyname)(sname, len);
8302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8303 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8307 #define INIT_ASYSCTL \
8308 COMMON_INTERCEPT_FUNCTION(asysctl); \
8309 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
8311 #define INIT_ASYSCTL
8314 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
8315 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
8316 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
8319 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
8322 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8324 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8326 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
8327 // Skip rnode, it's rarely used and not trivial to sanitize
8328 // It's also used mostly internally
8329 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
8332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
8339 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
8344 #define INIT_SYSCTLGETMIBINFO \
8345 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
8347 #define INIT_SYSCTLGETMIBINFO
8350 #if SANITIZER_INTERCEPT_NL_LANGINFO
8351 INTERCEPTOR(char *, nl_langinfo, long item) {
8353 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
8354 char *ret = REAL(nl_langinfo)(item);
8356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8359 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
8361 #define INIT_NL_LANGINFO
8364 #if SANITIZER_INTERCEPT_MODCTL
8365 INTERCEPTOR(int, modctl, int operation, void *argp) {
8368 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
8370 if (operation == modctl_load) {
8372 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
8373 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
8374 if (ml->ml_filename)
8375 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
8376 internal_strlen(ml->ml_filename) + 1);
8378 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
8380 ret = REAL(modctl)(operation, argp);
8381 } else if (operation == modctl_unload) {
8383 const char *name = (const char *)argp;
8384 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
8386 ret = REAL(modctl)(operation, argp);
8387 } else if (operation == modctl_stat) {
8389 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
8391 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
8392 iov_len = iov->iov_len;
8394 ret = REAL(modctl)(operation, argp);
8396 COMMON_INTERCEPTOR_WRITE_RANGE(
8397 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
8398 } else if (operation == modctl_exists) {
8399 ret = REAL(modctl)(operation, argp);
8401 ret = REAL(modctl)(operation, argp);
8406 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
8411 #if SANITIZER_INTERCEPT_STRTONUM
8412 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
8413 long long maxval, const char **errstr) {
8415 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
8417 // TODO(kamil): Implement strtoll as a common inteceptor
8419 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
8420 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
8422 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
8424 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
8426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
8430 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
8432 #define INIT_STRTONUM
8435 #if SANITIZER_INTERCEPT_FPARSELN
8436 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
8437 SIZE_T *lineno, const char delim[3], int flags) {
8439 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
8441 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
8443 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
8444 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
8446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8450 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
8454 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
8456 #define INIT_FPARSELN
8459 #if SANITIZER_INTERCEPT_STATVFS1
8460 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
8462 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
8463 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
8464 int res = REAL(statvfs1)(path, buf, flags);
8465 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8468 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
8470 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
8471 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
8472 int res = REAL(fstatvfs1)(fd, buf, flags);
8474 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8476 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
8480 #define INIT_STATVFS1 \
8481 COMMON_INTERCEPT_FUNCTION(statvfs1); \
8482 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
8484 #define INIT_STATVFS1
8487 #if SANITIZER_INTERCEPT_STRTOI
8488 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
8489 INTMAX_T low, INTMAX_T high, int *rstatus) {
8491 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
8493 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
8494 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8496 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8500 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
8501 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
8503 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
8505 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
8506 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8508 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8511 #define INIT_STRTOI \
8512 COMMON_INTERCEPT_FUNCTION(strtoi); \
8513 COMMON_INTERCEPT_FUNCTION(strtou)
8518 #if SANITIZER_INTERCEPT_CAPSICUM
8519 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
8522 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
8524 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8525 __sanitizer_cap_rights_t *ret = \
8526 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
8528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8532 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
8535 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
8537 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8538 __sanitizer_cap_rights_t *ret = \
8539 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
8541 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8545 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
8548 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
8550 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8551 __sanitizer_cap_rights_t *ret = \
8552 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8558 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8561 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8563 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8564 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8567 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8568 __sanitizer_cap_rights_t *rights) {
8569 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8572 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8573 __sanitizer_cap_rights_t *rights) {
8574 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8577 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8578 __sanitizer_cap_rights_t *rights) {
8579 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8582 INTERCEPTOR(bool, cap_rights_is_set,
8583 __sanitizer_cap_rights_t *rights) {
8584 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8587 INTERCEPTOR(int, cap_rights_limit, int fd,
8588 const __sanitizer_cap_rights_t *rights) {
8590 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8592 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8594 return REAL(cap_rights_limit)(fd, rights);
8597 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8599 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8600 int ret = REAL(cap_rights_get)(fd, rights);
8602 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8607 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8609 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8611 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8613 return REAL(cap_rights_is_valid(rights));
8616 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8617 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8619 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8621 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8623 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8625 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8630 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8631 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8633 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8635 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8637 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8639 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8644 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8645 const __sanitizer_cap_rights *little) {
8647 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8649 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8651 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8653 return REAL(cap_rights_contains)(big, little);
8656 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8658 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8660 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8662 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8665 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8667 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8668 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8670 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8674 #define INIT_CAPSICUM \
8675 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8676 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8677 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8678 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8679 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8680 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8681 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8682 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8683 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8684 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8685 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8686 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8688 #define INIT_CAPSICUM
8691 #if SANITIZER_INTERCEPT_SHA1
8692 INTERCEPTOR(void, SHA1Init, void *context) {
8694 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8695 REAL(SHA1Init)(context);
8697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8699 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8701 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8702 if (data && len > 0)
8703 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8705 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8706 REAL(SHA1Update)(context, data, len);
8708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8710 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8712 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8714 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8715 REAL(SHA1Final)(digest, context);
8717 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8719 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8721 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8723 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8725 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8726 REAL(SHA1Transform)(state, buffer);
8728 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8730 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8732 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8734 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8735 char *ret = REAL(SHA1End)(context, buf);
8737 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8740 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8742 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8744 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8745 char *ret = REAL(SHA1File)(filename, buf);
8747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8750 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8753 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8755 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8756 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8758 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8761 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8763 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8765 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8766 char *ret = REAL(SHA1Data)(data, len, buf);
8768 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8772 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8773 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8774 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8775 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8776 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8777 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8778 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8779 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8784 #if SANITIZER_INTERCEPT_MD4
8785 INTERCEPTOR(void, MD4Init, void *context) {
8787 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8788 REAL(MD4Init)(context);
8790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8793 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8796 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8797 if (data && len > 0)
8798 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8800 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8801 REAL(MD4Update)(context, data, len);
8803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8806 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8808 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8810 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8811 REAL(MD4Final)(digest, context);
8813 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8816 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8818 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8820 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8821 char *ret = REAL(MD4End)(context, buf);
8823 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8827 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8829 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8831 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8832 char *ret = REAL(MD4File)(filename, buf);
8834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8838 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8841 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8842 if (data && len > 0)
8843 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8844 char *ret = REAL(MD4Data)(data, len, buf);
8846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8851 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8852 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8853 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8854 COMMON_INTERCEPT_FUNCTION(MD4End); \
8855 COMMON_INTERCEPT_FUNCTION(MD4File); \
8856 COMMON_INTERCEPT_FUNCTION(MD4Data)
8861 #if SANITIZER_INTERCEPT_RMD160
8862 INTERCEPTOR(void, RMD160Init, void *context) {
8864 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8865 REAL(RMD160Init)(context);
8867 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8869 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8871 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8872 if (data && len > 0)
8873 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8875 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8876 REAL(RMD160Update)(context, data, len);
8878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8880 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8882 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8884 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8885 REAL(RMD160Final)(digest, context);
8887 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8889 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8891 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8893 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8895 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8896 REAL(RMD160Transform)(state, buffer);
8898 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8900 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8902 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8904 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8905 char *ret = REAL(RMD160End)(context, buf);
8907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8910 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8912 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8914 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8915 char *ret = REAL(RMD160File)(filename, buf);
8917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8920 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8923 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8925 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8926 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8931 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8933 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8934 if (data && len > 0)
8935 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8936 char *ret = REAL(RMD160Data)(data, len, buf);
8938 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8941 #define INIT_RMD160 \
8942 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8943 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8944 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8945 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8946 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8947 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8948 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8949 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8954 #if SANITIZER_INTERCEPT_FSEEK
8955 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8957 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8958 return REAL(fseek)(stream, offset, whence);
8960 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8962 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8963 return REAL(fseeko)(stream, offset, whence);
8965 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8967 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8968 return REAL(ftell)(stream);
8970 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8972 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8973 return REAL(ftello)(stream);
8975 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8977 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8978 return REAL(rewind)(stream);
8980 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8982 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8983 int ret = REAL(fgetpos)(stream, pos);
8985 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8988 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8990 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8992 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8993 return REAL(fsetpos)(stream, pos);
8995 #define INIT_FSEEK \
8996 COMMON_INTERCEPT_FUNCTION(fseek); \
8997 COMMON_INTERCEPT_FUNCTION(fseeko); \
8998 COMMON_INTERCEPT_FUNCTION(ftell); \
8999 COMMON_INTERCEPT_FUNCTION(ftello); \
9000 COMMON_INTERCEPT_FUNCTION(rewind); \
9001 COMMON_INTERCEPT_FUNCTION(fgetpos); \
9002 COMMON_INTERCEPT_FUNCTION(fsetpos)
9007 #if SANITIZER_INTERCEPT_MD2
9008 INTERCEPTOR(void, MD2Init, void *context) {
9010 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
9011 REAL(MD2Init)(context);
9013 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
9016 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
9019 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
9020 if (data && len > 0)
9021 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
9023 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
9024 REAL(MD2Update)(context, data, len);
9026 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
9029 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
9031 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
9033 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
9034 REAL(MD2Final)(digest, context);
9036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
9039 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
9041 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
9043 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
9044 char *ret = REAL(MD2End)(context, buf);
9046 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9050 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
9052 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
9054 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
9055 char *ret = REAL(MD2File)(filename, buf);
9057 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9061 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
9064 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
9065 if (data && len > 0)
9066 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
9067 char *ret = REAL(MD2Data)(data, len, buf);
9069 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9074 COMMON_INTERCEPT_FUNCTION(MD2Init); \
9075 COMMON_INTERCEPT_FUNCTION(MD2Update); \
9076 COMMON_INTERCEPT_FUNCTION(MD2Final); \
9077 COMMON_INTERCEPT_FUNCTION(MD2End); \
9078 COMMON_INTERCEPT_FUNCTION(MD2File); \
9079 COMMON_INTERCEPT_FUNCTION(MD2Data)
9084 #if SANITIZER_INTERCEPT_VIS
9085 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
9087 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
9088 char *end = REAL(vis)(dst, c, flag, nextc);
9089 // dst is NULL terminated and end points to the NULL char
9091 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9094 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
9096 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
9097 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
9098 // nvis cannot make sure the dst is NULL terminated
9100 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9103 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
9105 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
9107 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9108 int len = REAL(strvis)(dst, src, flag);
9110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9113 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
9115 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
9117 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9118 int len = REAL(stravis)(dst, src, flag);
9120 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
9122 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
9126 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
9128 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
9130 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9131 int len = REAL(strnvis)(dst, dlen, src, flag);
9132 // The interface will be valid even if there is no space for NULL char
9134 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9137 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
9139 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
9141 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9142 int ret = REAL(strvisx)(dst, src, len, flag);
9144 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9147 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9150 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
9152 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9153 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
9154 if (dst && ret >= 0)
9155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9158 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9159 int flag, int *cerr_ptr) {
9161 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
9163 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9164 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9165 // according to the implementation
9167 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9168 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
9169 if (dst && ret >= 0)
9170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9175 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
9176 const char *extra) {
9178 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
9180 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9181 char *end = REAL(svis)(dst, c, flag, nextc, extra);
9183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9186 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
9187 const char *extra) {
9189 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
9191 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9192 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
9194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
9195 Min((SIZE_T)(end - dst + 1), dlen));
9198 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
9199 const char *extra) {
9201 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
9203 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9205 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9206 int len = REAL(strsvis)(dst, src, flag, extra);
9208 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9211 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
9212 const char *extra) {
9214 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
9216 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9218 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9219 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
9220 // The interface will be valid even if there is no space for NULL char
9221 if (dst && len >= 0)
9222 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9225 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
9226 const char *extra) {
9228 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
9230 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9232 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9233 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
9235 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9238 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9239 int flag, const char *extra) {
9241 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
9243 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9245 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9246 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
9247 if (dst && ret >= 0)
9248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9251 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
9252 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
9254 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
9257 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9259 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9260 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9261 // according to the implementation
9263 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9264 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
9265 if (dst && ret >= 0)
9266 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9268 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9271 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
9273 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
9275 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
9276 int ret = REAL(unvis)(cp, c, astate, flag);
9277 if (ret == unvis_valid || ret == unvis_validpush) {
9278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
9282 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
9284 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
9286 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9287 int ret = REAL(strunvis)(dst, src);
9289 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9292 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
9294 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
9296 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9297 int ret = REAL(strnunvis)(dst, dlen, src);
9299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9302 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
9304 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
9306 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9307 int ret = REAL(strunvisx)(dst, src, flag);
9309 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9312 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
9315 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
9317 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9318 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
9320 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9324 COMMON_INTERCEPT_FUNCTION(vis); \
9325 COMMON_INTERCEPT_FUNCTION(nvis); \
9326 COMMON_INTERCEPT_FUNCTION(strvis); \
9327 COMMON_INTERCEPT_FUNCTION(stravis); \
9328 COMMON_INTERCEPT_FUNCTION(strnvis); \
9329 COMMON_INTERCEPT_FUNCTION(strvisx); \
9330 COMMON_INTERCEPT_FUNCTION(strnvisx); \
9331 COMMON_INTERCEPT_FUNCTION(strenvisx); \
9332 COMMON_INTERCEPT_FUNCTION(svis); \
9333 COMMON_INTERCEPT_FUNCTION(snvis); \
9334 COMMON_INTERCEPT_FUNCTION(strsvis); \
9335 COMMON_INTERCEPT_FUNCTION(strsnvis); \
9336 COMMON_INTERCEPT_FUNCTION(strsvisx); \
9337 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
9338 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
9339 COMMON_INTERCEPT_FUNCTION(unvis); \
9340 COMMON_INTERCEPT_FUNCTION(strunvis); \
9341 COMMON_INTERCEPT_FUNCTION(strnunvis); \
9342 COMMON_INTERCEPT_FUNCTION(strunvisx); \
9343 COMMON_INTERCEPT_FUNCTION(strnunvisx)
9348 #if SANITIZER_INTERCEPT_CDB
9349 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
9351 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
9353 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9354 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
9356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9360 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
9361 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
9363 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
9366 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
9367 struct __sanitizer_cdbr *cdbr =
9368 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
9370 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9374 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9376 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9378 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9379 return REAL(cdbr_entries)(cdbr);
9382 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9383 const void **data, SIZE_T *datalen) {
9385 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9387 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9388 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9393 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9394 if (data && datalen)
9395 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9400 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9401 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9403 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9405 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9407 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9408 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9411 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9413 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9414 if (data && datalen)
9415 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9420 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9422 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9424 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9425 REAL(cdbr_close)(cdbr);
9428 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9430 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9431 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9433 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9437 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9438 SIZE_T keylen, const void *data, SIZE_T datalen) {
9440 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9442 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9443 if (data && datalen)
9444 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9446 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9447 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9449 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9453 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9454 SIZE_T datalen, u32 *index) {
9456 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9458 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9459 if (data && datalen)
9460 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9461 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9471 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9472 SIZE_T keylen, u32 index) {
9474 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9476 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9478 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9479 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9485 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9486 const char descr[16], u32 (*seedgen)(void)) {
9488 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9489 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9491 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9493 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9495 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9496 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9499 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9501 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9506 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9508 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9510 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9511 REAL(cdbw_close)(cdbw);
9515 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9516 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9517 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9518 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9519 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9520 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9521 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9522 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9523 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9524 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9525 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9526 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9531 #if SANITIZER_INTERCEPT_GETFSENT
9532 INTERCEPTOR(void *, getfsent) {
9534 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9535 void *ret = REAL(getfsent)();
9537 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9541 INTERCEPTOR(void *, getfsspec, const char *spec) {
9543 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9545 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
9546 void *ret = REAL(getfsspec)(spec);
9548 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9552 INTERCEPTOR(void *, getfsfile, const char *file) {
9554 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9556 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
9557 void *ret = REAL(getfsfile)(file);
9559 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9563 #define INIT_GETFSENT \
9564 COMMON_INTERCEPT_FUNCTION(getfsent); \
9565 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9566 COMMON_INTERCEPT_FUNCTION(getfsfile);
9568 #define INIT_GETFSENT
9571 #if SANITIZER_INTERCEPT_ARC4RANDOM
9572 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9574 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9575 REAL(arc4random_buf)(buf, len);
9577 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9580 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9582 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9584 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9585 REAL(arc4random_addrandom)(dat, datlen);
9588 #define INIT_ARC4RANDOM \
9589 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9590 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9592 #define INIT_ARC4RANDOM
9595 #if SANITIZER_INTERCEPT_POPEN
9596 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9598 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9600 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
9602 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9603 __sanitizer_FILE *res = REAL(popen)(command, type);
9604 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9605 if (res) unpoison_file(res);
9608 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9613 #if SANITIZER_INTERCEPT_POPENVE
9614 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9615 char *const *argv, char *const *envp, const char *type) {
9617 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9619 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9621 for (char *const *pa = argv; ; ++pa) {
9622 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9625 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9629 for (char *const *pa = envp; ; ++pa) {
9630 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9633 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9637 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9638 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9639 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9640 if (res) unpoison_file(res);
9643 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9645 #define INIT_POPENVE
9648 #if SANITIZER_INTERCEPT_PCLOSE
9649 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9651 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9652 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9653 const FileMetadata *m = GetInterceptorMetadata(fp);
9654 int res = REAL(pclose)(fp);
9656 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9657 DeleteInterceptorMetadata(fp);
9661 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9666 #if SANITIZER_INTERCEPT_FUNOPEN
9667 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9668 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9669 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9670 typedef int (*funopen_closefn)(void *cookie);
9672 struct WrappedFunopenCookie {
9674 funopen_readfn real_read;
9675 funopen_writefn real_write;
9676 funopen_seekfn real_seek;
9677 funopen_closefn real_close;
9680 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9681 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9682 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9683 funopen_readfn real_read = wrapped_cookie->real_read;
9684 return real_read(wrapped_cookie->real_cookie, buf, len);
9687 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9688 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9689 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9690 funopen_writefn real_write = wrapped_cookie->real_write;
9691 return real_write(wrapped_cookie->real_cookie, buf, len);
9694 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9695 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9696 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9697 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9698 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9701 static int wrapped_funopen_close(void *cookie) {
9702 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9703 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9704 funopen_closefn real_close = wrapped_cookie->real_close;
9705 int res = real_close(wrapped_cookie->real_cookie);
9706 InternalFree(wrapped_cookie);
9710 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9711 funopen_writefn writefn, funopen_seekfn seekfn,
9712 funopen_closefn closefn) {
9714 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9717 WrappedFunopenCookie *wrapped_cookie =
9718 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9719 wrapped_cookie->real_cookie = cookie;
9720 wrapped_cookie->real_read = readfn;
9721 wrapped_cookie->real_write = writefn;
9722 wrapped_cookie->real_seek = seekfn;
9723 wrapped_cookie->real_close = closefn;
9725 __sanitizer_FILE *res =
9726 REAL(funopen)(wrapped_cookie,
9727 readfn ? wrapped_funopen_read : nullptr,
9728 writefn ? wrapped_funopen_write : nullptr,
9729 seekfn ? wrapped_funopen_seek : nullptr,
9730 closefn ? wrapped_funopen_close : nullptr);
9735 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9737 #define INIT_FUNOPEN
9740 #if SANITIZER_INTERCEPT_FUNOPEN2
9741 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9742 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9743 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9744 typedef int (*funopen2_flushfn)(void *cookie);
9745 typedef int (*funopen2_closefn)(void *cookie);
9747 struct WrappedFunopen2Cookie {
9749 funopen2_readfn real_read;
9750 funopen2_writefn real_write;
9751 funopen2_seekfn real_seek;
9752 funopen2_flushfn real_flush;
9753 funopen2_closefn real_close;
9756 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9757 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9758 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9759 funopen2_readfn real_read = wrapped_cookie->real_read;
9760 return real_read(wrapped_cookie->real_cookie, buf, len);
9763 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9765 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9766 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9767 funopen2_writefn real_write = wrapped_cookie->real_write;
9768 return real_write(wrapped_cookie->real_cookie, buf, len);
9771 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9772 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9773 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9774 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9775 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9778 static int wrapped_funopen2_flush(void *cookie) {
9779 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9780 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9781 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9782 return real_flush(wrapped_cookie->real_cookie);
9785 static int wrapped_funopen2_close(void *cookie) {
9786 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9787 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9788 funopen2_closefn real_close = wrapped_cookie->real_close;
9789 int res = real_close(wrapped_cookie->real_cookie);
9790 InternalFree(wrapped_cookie);
9794 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9795 funopen2_writefn writefn, funopen2_seekfn seekfn,
9796 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9798 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9801 WrappedFunopen2Cookie *wrapped_cookie =
9802 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9803 wrapped_cookie->real_cookie = cookie;
9804 wrapped_cookie->real_read = readfn;
9805 wrapped_cookie->real_write = writefn;
9806 wrapped_cookie->real_seek = seekfn;
9807 wrapped_cookie->real_flush = flushfn;
9808 wrapped_cookie->real_close = closefn;
9810 __sanitizer_FILE *res =
9811 REAL(funopen2)(wrapped_cookie,
9812 readfn ? wrapped_funopen2_read : nullptr,
9813 writefn ? wrapped_funopen2_write : nullptr,
9814 seekfn ? wrapped_funopen2_seek : nullptr,
9815 flushfn ? wrapped_funopen2_flush : nullptr,
9816 closefn ? wrapped_funopen2_close : nullptr);
9821 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9823 #define INIT_FUNOPEN2
9826 #if SANITIZER_INTERCEPT_FDEVNAME
9827 INTERCEPTOR(char *, fdevname, int fd) {
9829 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9830 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9831 char *name = REAL(fdevname)(fd);
9833 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
9835 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9840 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9842 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9843 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9844 char *name = REAL(fdevname_r)(fd, buf, len);
9845 if (name && buf && len > 0) {
9846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
9848 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9853 #define INIT_FDEVNAME \
9854 COMMON_INTERCEPT_FUNCTION(fdevname); \
9855 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9857 #define INIT_FDEVNAME
9860 #if SANITIZER_INTERCEPT_GETUSERSHELL
9861 INTERCEPTOR(char *, getusershell,) {
9863 COMMON_INTERCEPTOR_ENTER(ctx, getusershell,);
9864 char *res = REAL(getusershell)();
9866 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9870 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9872 #define INIT_GETUSERSHELL
9875 #if SANITIZER_INTERCEPT_SL_INIT
9876 INTERCEPTOR(void *, sl_init) {
9878 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9879 void *res = REAL(sl_init)();
9881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9885 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9887 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9889 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9891 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9892 int res = REAL(sl_add)(sl, item);
9894 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9898 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9900 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9902 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9904 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9905 char *res = REAL(sl_find)(sl, item);
9907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9911 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9913 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9915 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9916 REAL(sl_free)(sl, freeall);
9919 #define INIT_SL_INIT \
9920 COMMON_INTERCEPT_FUNCTION(sl_init); \
9921 COMMON_INTERCEPT_FUNCTION(sl_add); \
9922 COMMON_INTERCEPT_FUNCTION(sl_find); \
9923 COMMON_INTERCEPT_FUNCTION(sl_free);
9925 #define INIT_SL_INIT
9928 #if SANITIZER_INTERCEPT_GETRANDOM
9929 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
9931 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
9932 // If GRND_NONBLOCK is set in the flags, it is non blocking.
9933 static const int grnd_nonblock = 1;
9935 if ((flags & grnd_nonblock))
9936 n = REAL(getrandom)(buf, buflen, flags);
9938 n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
9940 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
9944 #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
9946 #define INIT_GETRANDOM
9949 #if SANITIZER_INTERCEPT_GETENTROPY
9950 INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
9952 COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
9953 int r = REAL(getentropy)(buf, buflen);
9955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
9959 #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
9961 #define INIT_GETENTROPY
9964 #if SANITIZER_INTERCEPT_QSORT_R
9965 typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
9966 struct qsort_r_compar_params {
9968 qsort_r_compar_f compar;
9971 static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
9972 qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
9973 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9974 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
9975 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
9976 return params->compar(a, b, params->arg);
9979 INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
9980 qsort_r_compar_f compar, void *arg) {
9982 COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
9983 // Run the comparator over all array elements to detect any memory issues.
9985 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
9986 void *p = (void *)((char *)base + i * size);
9987 void *q = (void *)((char *)base + (i + 1) * size);
9988 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9992 qsort_r_compar_params params = {size, compar, arg};
9993 REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, ¶ms);
9994 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
9996 # define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
9998 # define INIT_QSORT_R
10001 #if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
10002 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10003 qsort_r_compar_f compar) {
10005 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10006 WRAP(qsort_r)(base, nmemb, size, compar, nullptr);
10008 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10009 #elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
10010 // Glibc qsort uses a temporary buffer allocated either on stack or on heap.
10011 // Poisoned memory from there may get copied into the comparator arguments,
10012 // where it needs to be dealt with. But even that is not enough - the results of
10013 // the sort may be copied into the input/output array based on the results of
10014 // the comparator calls, but directly from the temp memory, bypassing the
10015 // unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
10016 // unpoisoning the entire array after the sort is done.
10018 // We can not check that the entire array is initialized at the beginning. IMHO,
10019 // it's fine for parts of the sorted objects to contain uninitialized memory,
10020 // ex. as padding in structs.
10021 typedef int (*qsort_compar_f)(const void *, const void *);
10022 static THREADLOCAL qsort_compar_f qsort_compar;
10023 static THREADLOCAL SIZE_T qsort_size;
10024 static int wrapped_qsort_compar(const void *a, const void *b) {
10025 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10026 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
10027 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
10028 return qsort_compar(a, b);
10031 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10032 qsort_compar_f compar) {
10034 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10035 // Run the comparator over all array elements to detect any memory issues.
10037 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
10038 void *p = (void *)((char *)base + i * size);
10039 void *q = (void *)((char *)base + (i + 1) * size);
10040 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10044 qsort_compar_f old_compar = qsort_compar;
10045 SIZE_T old_size = qsort_size;
10046 // Handle qsort() implementations that recurse using an
10047 // interposable function call:
10048 bool already_wrapped = compar == wrapped_qsort_compar;
10049 if (already_wrapped) {
10050 // This case should only happen if the qsort() implementation calls itself
10051 // using a preemptible function call (e.g. the FreeBSD libc version).
10052 // Check that the size and comparator arguments are as expected.
10053 CHECK_NE(compar, qsort_compar);
10054 CHECK_EQ(qsort_size, size);
10056 qsort_compar = compar;
10059 REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
10060 if (!already_wrapped) {
10061 qsort_compar = old_compar;
10062 qsort_size = old_size;
10064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
10066 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10068 # define INIT_QSORT
10071 #if SANITIZER_INTERCEPT_BSEARCH
10072 typedef int (*bsearch_compar_f)(const void *, const void *);
10073 struct bsearch_compar_params {
10075 bsearch_compar_f compar;
10078 static int wrapped_bsearch_compar(const void *key, const void *b) {
10079 const bsearch_compar_params *params = (const bsearch_compar_params *)key;
10080 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10081 return params->compar(params->key, b);
10084 INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
10085 SIZE_T size, bsearch_compar_f compar) {
10087 COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
10088 bsearch_compar_params params = {key, compar};
10089 return REAL(bsearch)(¶ms, base, nmemb, size, wrapped_bsearch_compar);
10091 # define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
10093 # define INIT_BSEARCH
10096 #if SANITIZER_INTERCEPT_SIGALTSTACK
10097 INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
10099 COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
10100 int r = REAL(sigaltstack)(ss, oss);
10101 if (r == 0 && oss != nullptr) {
10102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
10106 #define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
10108 #define INIT_SIGALTSTACK
10111 #if SANITIZER_INTERCEPT_PROCCTL
10112 INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
10114 COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
10115 static const int PROC_REAP_ACQUIRE = 2;
10116 static const int PROC_REAP_RELEASE = 3;
10117 static const int PROC_REAP_STATUS = 4;
10118 static const int PROC_REAP_GETPIDS = 5;
10119 static const int PROC_REAP_KILL = 6;
10120 if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
10121 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
10123 // reap_acquire/reap_release bears no arguments.
10124 if (cmd > PROC_REAP_RELEASE) {
10125 unsigned int reapsz;
10127 case PROC_REAP_STATUS:
10128 reapsz = struct_procctl_reaper_status_sz;
10130 case PROC_REAP_GETPIDS:
10131 reapsz = struct_procctl_reaper_pids_sz;
10133 case PROC_REAP_KILL:
10134 reapsz = struct_procctl_reaper_kill_sz;
10137 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
10140 return REAL(procctl)(idtype, id, cmd, data);
10142 #define INIT_PROCCTL COMMON_INTERCEPT_FUNCTION(procctl)
10144 #define INIT_PROCCTL
10147 #if SANITIZER_INTERCEPT_UNAME
10148 INTERCEPTOR(int, uname, struct utsname *utsname) {
10149 #if SANITIZER_LINUX
10150 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
10151 return internal_uname(utsname);
10154 COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
10155 int res = REAL(uname)(utsname);
10157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10158 __sanitizer::struct_utsname_sz);
10161 #define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
10166 #if SANITIZER_INTERCEPT___XUNAME
10167 // FreeBSD's <sys/utsname.h> define uname() as
10168 // static __inline int uname(struct utsname *name) {
10169 // return __xuname(SYS_NMLN, (void*)name);
10171 INTERCEPTOR(int, __xuname, int size, void *utsname) {
10173 COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
10174 int res = REAL(__xuname)(size, utsname);
10176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10177 __sanitizer::struct_utsname_sz);
10180 #define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
10182 #define INIT___XUNAME
10185 #if SANITIZER_INTERCEPT_ARGP_PARSE
10186 INTERCEPTOR(int, argp_parse, const struct argp *argp, int argc, char **argv,
10187 unsigned flags, int *arg_index, void *input) {
10189 COMMON_INTERCEPTOR_ENTER(ctx, argp_parse, argp, argc, argv, flags, arg_index,
10191 for (int i = 0; i < argc; i++)
10192 COMMON_INTERCEPTOR_READ_RANGE(ctx, argv[i], internal_strlen(argv[i]) + 1);
10193 int res = REAL(argp_parse)(argp, argc, argv, flags, arg_index, input);
10194 if (!res && arg_index)
10195 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg_index, sizeof(int));
10199 #define INIT_ARGP_PARSE COMMON_INTERCEPT_FUNCTION(argp_parse);
10201 #define INIT_ARGP_PARSE
10204 #if SANITIZER_INTERCEPT_CPUSET_GETAFFINITY
10205 INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T cpusetsize, __sanitizer_cpuset_t *mask) {
10207 COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getaffinity, level, which, id, cpusetsize, mask);
10208 int res = REAL(cpuset_getaffinity)(level, which, id, cpusetsize, mask);
10209 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
10212 #define INIT_CPUSET_GETAFFINITY COMMON_INTERCEPT_FUNCTION(cpuset_getaffinity);
10214 #define INIT_CPUSET_GETAFFINITY
10217 #if SANITIZER_INTERCEPT_PREADV2
10218 INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
10219 OFF_T offset, int flags) {
10221 COMMON_INTERCEPTOR_ENTER(ctx, preadv2, fd, iov, iovcnt, offset, flags);
10222 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10223 SSIZE_T res = REAL(preadv2)(fd, iov, iovcnt, offset, flags);
10224 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
10225 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10228 #define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
10230 #define INIT_PREADV2
10233 #if SANITIZER_INTERCEPT_PWRITEV2
10234 INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
10235 OFF_T offset, int flags) {
10237 COMMON_INTERCEPTOR_ENTER(ctx, pwritev2, fd, iov, iovcnt, offset, flags);
10238 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10239 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
10240 SSIZE_T res = REAL(pwritev2)(fd, iov, iovcnt, offset, flags);
10241 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
10244 #define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
10246 #define INIT_PWRITEV2
10249 #if SANITIZER_INTERCEPT_FREADLINK
10250 INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) {
10252 COMMON_INTERCEPTOR_ENTER(ctx, freadlink, fd, buf, bufsiz);
10253 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10254 SSIZE_T res = REAL(freadlink)(fd, buf, bufsiz);
10256 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
10257 if (res >= 0 && fd > 0)
10258 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10261 # define INIT_FREADLINK COMMON_INTERCEPT_FUNCTION(freadlink)
10263 # define INIT_FREADLINK
10266 #include "sanitizer_common_interceptors_netbsd_compat.inc"
10268 namespace __sanitizer {
10269 void InitializeMemintrinsicInterceptors();
10270 } // namespace __sanitizer
10272 static void InitializeCommonInterceptors() {
10274 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
10275 interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
10278 __sanitizer::InitializeMemintrinsicInterceptors();
10324 INIT_LOCALTIME_AND_FRIENDS;
10330 INIT_ISOC99_PRINTF;
10332 INIT_FREXPF_FREXPL;
10333 INIT_GETPWNAM_AND_FRIENDS;
10334 INIT_GETPWNAM_R_AND_FRIENDS;
10341 INIT_CLOCK_GETTIME;
10342 INIT_CLOCK_GETCPUCLOCKID;
10350 INIT_DN_COMP_EXPAND;
10355 INIT_PTHREAD_GETSCHEDPARAM;
10359 INIT_GETHOSTBYNAME;
10360 INIT_GETHOSTBYNAME2;
10361 INIT_GETHOSTBYNAME_R;
10362 INIT_GETHOSTBYNAME2_R;
10363 INIT_GETHOSTBYADDR_R;
10384 INIT_GET_CURRENT_DIR_NAME;
10386 INIT_STRTOIMAX_C23;
10395 INIT_CANONICALIZE_FILE_NAME;
10397 INIT_SCHED_GETAFFINITY;
10398 INIT_SCHED_GETPARAM;
10401 INIT_XPG_STRERROR_R;
10412 INIT_SIGSET_LOGICOPS;
10415 INIT_PTHREAD_SIGMASK;
10418 INIT___LIBC_THR_SETCANCELSTATE;
10426 INIT_ETHER_NTOA_ATON;
10431 INIT_PTHREAD_ATTR_GET;
10432 INIT_PTHREAD_ATTR_GET_SCHED;
10433 INIT_PTHREAD_ATTR_GETINHERITSCHED;
10434 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
10435 INIT_PTHREAD_GETAFFINITY_NP;
10436 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
10437 INIT_PTHREAD_MUTEXATTR_GETTYPE;
10438 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
10439 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
10440 INIT_PTHREAD_MUTEXATTR_GETROBUST;
10441 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
10442 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
10443 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
10444 INIT_PTHREAD_CONDATTR_GETPSHARED;
10445 INIT_PTHREAD_CONDATTR_GETCLOCK;
10446 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
10454 INIT_PTHREAD_SETNAME_NP;
10455 INIT_PTHREAD_GETNAME_NP;
10473 INIT_IF_INDEXTONAME;
10479 INIT_LIBIO_INTERNALS;
10483 INIT_OPEN_MEMSTREAM;
10487 INIT_DLOPEN_DLCLOSE;
10493 INIT_PTHREAD_SETCANCEL;
10495 INIT_PROCESS_VM_READV;
10498 INIT_RECV_RECVFROM;
10502 INIT_EVENTFD_READ_WRITE;
10509 // FIXME: add other *stat interceptors.
10519 INIT_USER_FROM_UID;
10520 INIT_UID_FROM_USER;
10521 INIT_GROUP_FROM_GID;
10522 INIT_GID_FROM_GROUP;
10526 INIT_GETGROUPMEMBERSHIP;
10529 INIT_NAME_TO_HANDLE_AT;
10530 INIT_OPEN_BY_HANDLE_AT;
10541 INIT_MI_VECTOR_HASH;
10549 INIT_SYSCTLGETMIBINFO;
10584 INIT_CPUSET_GETAFFINITY;