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_symbolizer.h"
45 #include "sanitizer_tls_get_addr.h"
47 #if SANITIZER_INTERCEPTOR_HOOKS
48 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
49 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
50 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
55 #endif // SANITIZER_INTERCEPTOR_HOOKS
57 #if SANITIZER_WINDOWS && !defined(va_copy)
58 #define va_copy(dst, src) ((dst) = (src))
62 #define pthread_setname_np pthread_set_name_np
63 #define inet_aton __inet_aton
64 #define inet_pton __inet_pton
65 #define iconv __bsd_iconv
69 #define clock_getres __clock_getres50
70 #define clock_gettime __clock_gettime50
71 #define clock_settime __clock_settime50
72 #define ctime __ctime50
73 #define ctime_r __ctime_r50
74 #define devname __devname50
75 #define fgetpos __fgetpos50
76 #define fsetpos __fsetpos50
77 #define fstatvfs __fstatvfs90
78 #define fstatvfs1 __fstatvfs190
79 #define fts_children __fts_children60
80 #define fts_close __fts_close60
81 #define fts_open __fts_open60
82 #define fts_read __fts_read60
83 #define fts_set __fts_set60
84 #define getitimer __getitimer50
85 #define getmntinfo __getmntinfo90
86 #define getpwent __getpwent50
87 #define getpwnam __getpwnam50
88 #define getpwnam_r __getpwnam_r50
89 #define getpwuid __getpwuid50
90 #define getpwuid_r __getpwuid_r50
91 #define getutent __getutent50
92 #define getutxent __getutxent50
93 #define getutxid __getutxid50
94 #define getutxline __getutxline50
95 #define getvfsstat __getvfsstat90
96 #define pututxline __pututxline50
98 #define gmtime __gmtime50
99 #define gmtime_r __gmtime_r50
100 #define localtime __locatime50
101 #define localtime_r __localtime_r50
102 #define mktime __mktime50
103 #define lstat __lstat50
104 #define opendir __opendir30
105 #define readdir __readdir30
106 #define readdir_r __readdir_r30
107 #define scandir __scandir30
108 #define setitimer __setitimer50
109 #define setlocale __setlocale50
110 #define shmctl __shmctl50
111 #define sigaltstack __sigaltstack14
112 #define sigemptyset __sigemptyset14
113 #define sigfillset __sigfillset14
114 #define sigpending __sigpending14
115 #define sigprocmask __sigprocmask14
116 #define sigtimedwait __sigtimedwait50
117 #define stat __stat50
118 #define statvfs __statvfs90
119 #define statvfs1 __statvfs190
120 #define time __time50
121 #define times __times13
122 #define unvis __unvis50
123 #define wait3 __wait350
124 #define wait4 __wait450
125 extern const unsigned short *_ctype_tab_;
126 extern const short *_toupper_tab_;
127 extern const short *_tolower_tab_;
130 #if SANITIZER_MUSL && \
131 (defined(__i386__) || defined(__arm__) || SANITIZER_MIPS32 || SANITIZER_PPC32)
132 // musl 1.2.0 on existing 32-bit architectures uses new symbol names for the
133 // time-related functions that take 64-bit time_t values. See
134 // https://musl.libc.org/time64.html
135 #define adjtime __adjtime64
136 #define adjtimex __adjtimex_time64
137 #define aio_suspend __aio_suspend_time64
138 #define clock_adjtime __clock_adjtime64
139 #define clock_getres __clock_getres_time64
140 #define clock_gettime __clock_gettime64
141 #define clock_nanosleep __clock_nanosleep_time64
142 #define clock_settime __clock_settime64
143 #define cnd_timedwait __cnd_timedwait_time64
144 #define ctime __ctime64
145 #define ctime_r __ctime64_r
146 #define difftime __difftime64
147 #define dlsym __dlsym_time64
148 #define fstatat __fstatat_time64
149 #define fstat __fstat_time64
150 #define ftime __ftime64
151 #define futimens __futimens_time64
152 #define futimesat __futimesat_time64
153 #define futimes __futimes_time64
154 #define getitimer __getitimer_time64
155 #define getrusage __getrusage_time64
156 #define gettimeofday __gettimeofday_time64
157 #define gmtime __gmtime64
158 #define gmtime_r __gmtime64_r
159 #define localtime __localtime64
160 #define localtime_r __localtime64_r
161 #define lstat __lstat_time64
162 #define lutimes __lutimes_time64
163 #define mktime __mktime64
164 #define mq_timedreceive __mq_timedreceive_time64
165 #define mq_timedsend __mq_timedsend_time64
166 #define mtx_timedlock __mtx_timedlock_time64
167 #define nanosleep __nanosleep_time64
168 #define ppoll __ppoll_time64
169 #define pselect __pselect_time64
170 #define pthread_cond_timedwait __pthread_cond_timedwait_time64
171 #define pthread_mutex_timedlock __pthread_mutex_timedlock_time64
172 #define pthread_rwlock_timedrdlock __pthread_rwlock_timedrdlock_time64
173 #define pthread_rwlock_timedwrlock __pthread_rwlock_timedwrlock_time64
174 #define pthread_timedjoin_np __pthread_timedjoin_np_time64
175 #define recvmmsg __recvmmsg_time64
176 #define sched_rr_get_interval __sched_rr_get_interval_time64
177 #define select __select_time64
178 #define semtimedop __semtimedop_time64
179 #define sem_timedwait __sem_timedwait_time64
180 #define setitimer __setitimer_time64
181 #define settimeofday __settimeofday_time64
182 #define sigtimedwait __sigtimedwait_time64
183 #define stat __stat_time64
184 #define stime __stime64
185 #define thrd_sleep __thrd_sleep_time64
186 #define timegm __timegm_time64
187 #define timerfd_gettime __timerfd_gettime64
188 #define timerfd_settime __timerfd_settime64
189 #define timer_gettime __timer_gettime64
190 #define timer_settime __timer_settime64
191 #define timespec_get __timespec_get_time64
192 #define time __time64
193 #define utimensat __utimensat_time64
194 #define utimes __utimes_time64
195 #define utime __utime64
196 #define wait3 __wait3_time64
197 #define wait4 __wait4_time64
200 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
201 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
204 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
205 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
208 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
209 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
212 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
213 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
216 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
217 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
220 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
221 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
224 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
225 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
228 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
229 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
232 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
233 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
234 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
237 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
238 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
241 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
242 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
243 common_flags()->strict_string_checks ? (internal_strlen(s)) + 1 : (n) )
245 #ifndef COMMON_INTERCEPTOR_DLOPEN
246 #define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
247 ({ CheckNoDeepBind(filename, flag); REAL(dlopen)(filename, flag); })
250 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
251 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
254 #ifndef COMMON_INTERCEPTOR_ACQUIRE
255 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
258 #ifndef COMMON_INTERCEPTOR_RELEASE
259 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
262 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
263 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
266 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
267 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
270 #ifdef SANITIZER_NLDBL_VERSION
271 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
272 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
274 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
275 COMMON_INTERCEPT_FUNCTION(fn)
279 // If we could not find the versioned symbol, fall back to an unversioned
280 // lookup. This is needed to work around a GLibc bug that causes dlsym
281 // with RTLD_NEXT to return the oldest versioned symbol.
282 // See https://sourceware.org/bugzilla/show_bug.cgi?id=14932.
283 // For certain symbols (e.g. regexec) we have to perform a versioned lookup,
284 // but that versioned symbol will only exist for architectures where the
285 // oldest Glibc version pre-dates support for that architecture.
286 // For example, regexec@GLIBC_2.3.4 exists on x86_64, but not RISC-V.
287 // See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98920.
288 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
289 COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(fn, ver)
291 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
292 COMMON_INTERCEPT_FUNCTION(fn)
295 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
296 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
298 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
301 #ifndef COMMON_INTERCEPTOR_MUNMAP_IMPL
302 #define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz) \
303 { return REAL(munmap)(addr, sz); }
306 #ifndef COMMON_INTERCEPTOR_COPY_STRING
307 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
310 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
311 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
312 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
313 uptr copy_length = internal_strnlen(s, size); \
314 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
315 if (common_flags()->intercept_strndup) { \
316 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
319 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
320 internal_memcpy(new_mem, s, copy_length); \
321 new_mem[copy_length] = '\0'; \
326 #ifndef COMMON_INTERCEPTOR_STRERROR
327 #define COMMON_INTERCEPTOR_STRERROR() {}
330 struct FileMetadata {
331 // For open_memstream().
336 struct CommonInterceptorMetadata {
347 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
349 static MetadataHashMap *interceptor_metadata_map;
351 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
352 const FileMetadata &file) {
353 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
355 h->type = CommonInterceptorMetadata::CIMT_FILE;
359 UNUSED static const FileMetadata *GetInterceptorMetadata(
360 __sanitizer_FILE *addr) {
361 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
364 if (addr && h.exists()) {
366 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
373 UNUSED static void DeleteInterceptorMetadata(void *addr) {
374 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
379 #if SANITIZER_INTERCEPT_STRLEN
380 INTERCEPTOR(SIZE_T, strlen, const char *s) {
381 // Sometimes strlen is called prior to InitializeCommonInterceptors,
382 // in which case the REAL(strlen) typically used in
383 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
385 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
386 return internal_strlen(s);
388 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
389 SIZE_T result = REAL(strlen)(s);
390 if (common_flags()->intercept_strlen)
391 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
394 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
399 #if SANITIZER_INTERCEPT_STRNLEN
400 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
402 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
403 SIZE_T length = REAL(strnlen)(s, maxlen);
404 if (common_flags()->intercept_strlen)
405 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
408 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
413 #if SANITIZER_INTERCEPT_STRNDUP
414 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
416 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
418 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
421 #endif // SANITIZER_INTERCEPT_STRNDUP
423 #if SANITIZER_INTERCEPT___STRNDUP
424 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
426 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
428 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
430 #define INIT___STRNDUP
431 #endif // SANITIZER_INTERCEPT___STRNDUP
433 #if SANITIZER_INTERCEPT_TEXTDOMAIN
434 INTERCEPTOR(char*, textdomain, const char *domainname) {
436 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
437 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
438 char *domain = REAL(textdomain)(domainname);
440 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, internal_strlen(domain) + 1);
444 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
446 #define INIT_TEXTDOMAIN
449 #if SANITIZER_INTERCEPT_STRCMP || SANITIZER_INTERCEPT_MEMCMP
450 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
451 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
455 #if SANITIZER_INTERCEPT_STRCMP
456 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
457 const char *s1, const char *s2, int result)
459 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
461 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
462 unsigned char c1, c2;
465 c1 = (unsigned char)s1[i];
466 c2 = (unsigned char)s2[i];
467 if (c1 != c2 || c1 == '\0') break;
469 if (common_flags()->intercept_strcmp) {
470 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
471 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
473 int result = CharCmpX(c1, c2);
474 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
479 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
480 const char *s1, const char *s2, uptr n,
483 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
484 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
485 return internal_strncmp(s1, s2, size);
487 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
488 unsigned char c1 = 0, c2 = 0;
490 for (i = 0; i < size; i++) {
491 c1 = (unsigned char)s1[i];
492 c2 = (unsigned char)s2[i];
493 if (c1 != c2 || c1 == '\0') break;
497 if (common_flags()->strict_string_checks) {
498 for (; i1 < size && s1[i1]; i1++) {}
499 for (; i2 < size && s2[i2]; i2++) {}
501 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
502 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
503 int result = CharCmpX(c1, c2);
504 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
509 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
510 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
516 #if SANITIZER_INTERCEPT_STRCASECMP
517 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
518 int c1_low = ToLower(c1);
519 int c2_low = ToLower(c2);
520 return c1_low - c2_low;
523 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
524 const char *s1, const char *s2, int result)
526 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
528 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
529 unsigned char c1 = 0, c2 = 0;
532 c1 = (unsigned char)s1[i];
533 c2 = (unsigned char)s2[i];
534 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
536 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
537 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
538 int result = CharCaseCmp(c1, c2);
539 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
544 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
545 const char *s1, const char *s2, uptr size,
548 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
550 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
551 unsigned char c1 = 0, c2 = 0;
553 for (i = 0; i < size; i++) {
554 c1 = (unsigned char)s1[i];
555 c2 = (unsigned char)s2[i];
556 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
560 if (common_flags()->strict_string_checks) {
561 for (; i1 < size && s1[i1]; i1++) {}
562 for (; i2 < size && s2[i2]; i2++) {}
564 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
565 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
566 int result = CharCaseCmp(c1, c2);
567 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
568 s1, s2, size, result);
572 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
573 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
575 #define INIT_STRCASECMP
576 #define INIT_STRNCASECMP
579 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
580 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
582 uptr len1 = internal_strlen(s1);
583 uptr len2 = internal_strlen(s2);
584 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
585 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
589 #if SANITIZER_INTERCEPT_STRSTR
591 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
592 const char *s1, const char *s2, char *result)
594 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
595 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
596 return internal_strstr(s1, s2);
598 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
599 char *r = REAL(strstr)(s1, s2);
600 if (common_flags()->intercept_strstr)
601 StrstrCheck(ctx, r, s1, s2);
602 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
607 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
612 #if SANITIZER_INTERCEPT_STRCASESTR
614 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
615 const char *s1, const char *s2, char *result)
617 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
619 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
620 char *r = REAL(strcasestr)(s1, s2);
621 if (common_flags()->intercept_strstr)
622 StrstrCheck(ctx, r, s1, s2);
623 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
628 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
630 #define INIT_STRCASESTR
633 #if SANITIZER_INTERCEPT_STRTOK
635 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
637 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
638 if (!common_flags()->intercept_strtok) {
639 return REAL(strtok)(str, delimiters);
641 if (common_flags()->strict_string_checks) {
642 // If strict_string_checks is enabled, we check the whole first argument
643 // string on the first call (strtok saves this string in a static buffer
644 // for subsequent calls). We do not need to check strtok's result.
645 // As the delimiters can change, we check them every call.
646 if (str != nullptr) {
647 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
649 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
650 internal_strlen(delimiters) + 1);
651 return REAL(strtok)(str, delimiters);
653 // However, when strict_string_checks is disabled we cannot check the
654 // whole string on the first call. Instead, we check the result string
655 // which is guaranteed to be a NULL-terminated substring of the first
656 // argument. We also conservatively check one character of str and the
658 if (str != nullptr) {
659 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
661 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
662 char *result = REAL(strtok)(str, delimiters);
663 if (result != nullptr) {
664 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, internal_strlen(result) + 1);
665 } else if (str != nullptr) {
666 // No delimiter were found, it's safe to assume that the entire str was
668 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
674 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
679 #if SANITIZER_INTERCEPT_MEMMEM
680 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
681 const void *s1, SIZE_T len1, const void *s2,
682 SIZE_T len2, void *result)
684 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
687 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
688 void *r = REAL(memmem)(s1, len1, s2, len2);
689 if (common_flags()->intercept_memmem) {
690 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
691 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
693 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
694 s1, len1, s2, len2, r);
698 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
701 #endif // SANITIZER_INTERCEPT_MEMMEM
703 #if SANITIZER_INTERCEPT_STRCHR
704 INTERCEPTOR(char*, strchr, const char *s, int c) {
706 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
707 return internal_strchr(s, c);
708 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
709 char *result = REAL(strchr)(s, c);
710 if (common_flags()->intercept_strchr) {
711 // Keep strlen as macro argument, as macro may ignore it.
712 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
713 (result ? result - s : internal_strlen(s)) + 1);
717 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
722 #if SANITIZER_INTERCEPT_STRCHRNUL
723 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
725 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
726 char *result = REAL(strchrnul)(s, c);
727 uptr len = result - s + 1;
728 if (common_flags()->intercept_strchr)
729 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
732 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
734 #define INIT_STRCHRNUL
737 #if SANITIZER_INTERCEPT_STRRCHR
738 INTERCEPTOR(char*, strrchr, const char *s, int c) {
740 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
741 return internal_strrchr(s, c);
742 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
743 if (common_flags()->intercept_strchr)
744 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
745 return REAL(strrchr)(s, c);
747 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
752 #if SANITIZER_INTERCEPT_STRSPN
753 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
755 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
756 SIZE_T r = REAL(strspn)(s1, s2);
757 if (common_flags()->intercept_strspn) {
758 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
759 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
764 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
766 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
767 SIZE_T r = REAL(strcspn)(s1, s2);
768 if (common_flags()->intercept_strspn) {
769 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
770 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
775 #define INIT_STRSPN \
776 COMMON_INTERCEPT_FUNCTION(strspn); \
777 COMMON_INTERCEPT_FUNCTION(strcspn);
782 #if SANITIZER_INTERCEPT_STRPBRK
783 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
785 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
786 char *r = REAL(strpbrk)(s1, s2);
787 if (common_flags()->intercept_strpbrk) {
788 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
789 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
790 r ? r - s1 + 1 : internal_strlen(s1) + 1);
795 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
800 #if SANITIZER_INTERCEPT_MEMCMP
801 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
802 const void *s1, const void *s2, uptr n,
805 // Common code for `memcmp` and `bcmp`.
806 int MemcmpInterceptorCommon(void *ctx,
807 int (*real_fn)(const void *, const void *, uptr),
808 const void *a1, const void *a2, uptr size) {
809 if (common_flags()->intercept_memcmp) {
810 if (common_flags()->strict_memcmp) {
811 // Check the entire regions even if the first bytes of the buffers are
813 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
814 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
815 // Fallthrough to REAL(memcmp) below.
817 unsigned char c1 = 0, c2 = 0;
818 const unsigned char *s1 = (const unsigned char*)a1;
819 const unsigned char *s2 = (const unsigned char*)a2;
821 for (i = 0; i < size; i++) {
826 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
827 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
828 int r = CharCmpX(c1, c2);
829 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
834 int result = real_fn(a1, a2, size);
835 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
840 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
841 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
842 return internal_memcmp(a1, a2, size);
844 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
845 return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
848 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
853 #if SANITIZER_INTERCEPT_BCMP
854 INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {
855 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
856 return internal_memcmp(a1, a2, size);
858 COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
859 return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
862 #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
867 #if SANITIZER_INTERCEPT_MEMCHR
868 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
869 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
870 return internal_memchr(s, c, n);
872 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
873 #if SANITIZER_WINDOWS
876 res = REAL(memchr)(s, c, n);
878 res = internal_memchr(s, c, n);
881 void *res = REAL(memchr)(s, c, n);
883 uptr len = res ? (char *)res - (const char *)s + 1 : n;
884 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
888 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
893 #if SANITIZER_INTERCEPT_MEMRCHR
894 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
896 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
897 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
898 return REAL(memrchr)(s, c, n);
901 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
906 #if SANITIZER_INTERCEPT_FREXP
907 INTERCEPTOR(double, frexp, double x, int *exp) {
909 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
910 // Assuming frexp() always writes to |exp|.
911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
912 double res = REAL(frexp)(x, exp);
913 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
917 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
920 #endif // SANITIZER_INTERCEPT_FREXP
922 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
923 INTERCEPTOR(float, frexpf, float x, int *exp) {
925 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
926 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
927 float res = REAL(frexpf)(x, exp);
928 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
932 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
934 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
936 long double res = REAL(frexpl)(x, exp);
937 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
941 #define INIT_FREXPF_FREXPL \
942 COMMON_INTERCEPT_FUNCTION(frexpf); \
943 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
945 #define INIT_FREXPF_FREXPL
946 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
949 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
950 SIZE_T iovlen, SIZE_T maxlen) {
951 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
952 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
958 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
959 SIZE_T iovlen, SIZE_T maxlen) {
960 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
961 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
962 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
963 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
969 #if SANITIZER_INTERCEPT_READ
970 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
972 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
973 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
974 // FIXME: under ASan the call below may write to freed memory and corrupt
976 // https://github.com/google/sanitizers/issues/321.
977 SSIZE_T res = REAL(read)(fd, ptr, count);
978 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
979 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
982 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
987 #if SANITIZER_INTERCEPT_FREAD
988 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
989 // libc file streams can call user-supplied functions, see fopencookie.
991 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
992 // FIXME: under ASan the call below may write to freed memory and corrupt
994 // https://github.com/google/sanitizers/issues/321.
995 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
996 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
999 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1004 #if SANITIZER_INTERCEPT_PREAD
1005 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1007 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1008 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1009 // FIXME: under ASan the call below may write to freed memory and corrupt
1010 // its metadata. See
1011 // https://github.com/google/sanitizers/issues/321.
1012 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1013 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1014 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1017 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1022 #if SANITIZER_INTERCEPT_PREAD64
1023 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1025 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1026 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1027 // FIXME: under ASan the call below may write to freed memory and corrupt
1028 // its metadata. See
1029 // https://github.com/google/sanitizers/issues/321.
1030 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1031 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1032 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1035 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1037 #define INIT_PREAD64
1040 #if SANITIZER_INTERCEPT_READV
1041 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1044 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1045 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1046 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1047 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1048 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1051 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1056 #if SANITIZER_INTERCEPT_PREADV
1057 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1060 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1061 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1062 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1063 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1064 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1067 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1072 #if SANITIZER_INTERCEPT_PREADV64
1073 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1076 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1077 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1078 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1079 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1080 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1083 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1085 #define INIT_PREADV64
1088 #if SANITIZER_INTERCEPT_WRITE
1089 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1091 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1092 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1093 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1094 SSIZE_T res = REAL(write)(fd, ptr, count);
1095 // FIXME: this check should be _before_ the call to REAL(write), not after
1096 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1099 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1104 #if SANITIZER_INTERCEPT_FWRITE
1105 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1106 // libc file streams can call user-supplied functions, see fopencookie.
1108 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1109 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1110 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1113 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1118 #if SANITIZER_INTERCEPT_PWRITE
1119 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1121 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1122 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1123 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1124 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1125 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1128 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1133 #if SANITIZER_INTERCEPT_PWRITE64
1134 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1137 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1138 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1139 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1140 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1141 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1144 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1146 #define INIT_PWRITE64
1149 #if SANITIZER_INTERCEPT_WRITEV
1150 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1153 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1154 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1155 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1156 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1157 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1160 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1165 #if SANITIZER_INTERCEPT_PWRITEV
1166 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1169 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1170 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1171 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1172 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1173 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1176 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1178 #define INIT_PWRITEV
1181 #if SANITIZER_INTERCEPT_PWRITEV64
1182 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1185 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1186 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1187 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1188 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1189 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1192 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1194 #define INIT_PWRITEV64
1197 #if SANITIZER_INTERCEPT_FGETS
1198 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1199 // libc file streams can call user-supplied functions, see fopencookie.
1201 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1202 // FIXME: under ASan the call below may write to freed memory and corrupt
1203 // its metadata. See
1204 // https://github.com/google/sanitizers/issues/321.
1205 char *res = REAL(fgets)(s, size, file);
1207 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
1210 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1215 #if SANITIZER_INTERCEPT_FPUTS
1216 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1217 // libc file streams can call user-supplied functions, see fopencookie.
1219 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1220 if (!SANITIZER_APPLE || s) { // `fputs(NULL, file)` is supported on Darwin.
1221 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1223 return REAL(fputs)(s, file);
1225 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1230 #if SANITIZER_INTERCEPT_PUTS
1231 INTERCEPTOR(int, puts, char *s) {
1232 // libc file streams can call user-supplied functions, see fopencookie.
1234 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1235 if (!SANITIZER_APPLE || s) { // `puts(NULL)` is supported on Darwin.
1236 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1238 return REAL(puts)(s);
1240 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1245 #if SANITIZER_INTERCEPT_PRCTL
1246 INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
1247 unsigned long arg4, unsigned long arg5) {
1249 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1250 static const int PR_SET_NAME = 15;
1251 static const int PR_SET_VMA = 0x53564d41;
1252 static const int PR_SCHED_CORE = 62;
1253 static const int PR_SCHED_CORE_GET = 0;
1254 if (option == PR_SET_VMA && arg2 == 0UL) {
1255 char *name = (char *)arg5;
1256 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1258 int res = REAL(prctl)(option, arg2, arg3, arg4, arg5);
1259 if (option == PR_SET_NAME) {
1261 internal_strncpy(buff, (char *)arg2, 15);
1263 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1264 } else if (res != -1 && option == PR_SCHED_CORE && arg2 == PR_SCHED_CORE_GET) {
1265 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64*)(arg5), sizeof(u64));
1269 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1272 #endif // SANITIZER_INTERCEPT_PRCTL
1274 #if SANITIZER_INTERCEPT_TIME
1275 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1277 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1278 unsigned long local_t;
1279 unsigned long res = REAL(time)(&local_t);
1280 if (t && res != (unsigned long)-1) {
1281 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1286 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1289 #endif // SANITIZER_INTERCEPT_TIME
1291 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1292 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1294 #if !SANITIZER_SOLARIS
1296 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1297 // can point to shared memory and tsan would report a data race.
1298 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1299 internal_strlen(tm->tm_zone) + 1);
1303 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1305 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1306 __sanitizer_tm *res = REAL(localtime)(timep);
1308 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1309 unpoison_tm(ctx, res);
1313 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1315 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1316 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1318 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1319 unpoison_tm(ctx, res);
1323 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1325 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1326 __sanitizer_tm *res = REAL(gmtime)(timep);
1328 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1329 unpoison_tm(ctx, res);
1333 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1335 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1336 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1338 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1339 unpoison_tm(ctx, res);
1343 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1345 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1346 // FIXME: under ASan the call below may write to freed memory and corrupt
1347 // its metadata. See
1348 // https://github.com/google/sanitizers/issues/321.
1349 char *res = REAL(ctime)(timep);
1351 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1352 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1356 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1358 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1359 // FIXME: under ASan the call below may write to freed memory and corrupt
1360 // its metadata. See
1361 // https://github.com/google/sanitizers/issues/321.
1362 char *res = REAL(ctime_r)(timep, result);
1364 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1365 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1369 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1371 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1372 // FIXME: under ASan the call below may write to freed memory and corrupt
1373 // its metadata. See
1374 // https://github.com/google/sanitizers/issues/321.
1375 char *res = REAL(asctime)(tm);
1377 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1378 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1382 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1384 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1385 // FIXME: under ASan the call below may write to freed memory and corrupt
1386 // its metadata. See
1387 // https://github.com/google/sanitizers/issues/321.
1388 char *res = REAL(asctime_r)(tm, result);
1390 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1395 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1397 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1398 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1399 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1400 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1401 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1402 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1403 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1404 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1405 long res = REAL(mktime)(tm);
1406 if (res != -1) unpoison_tm(ctx, tm);
1409 #define INIT_LOCALTIME_AND_FRIENDS \
1410 COMMON_INTERCEPT_FUNCTION(localtime); \
1411 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1412 COMMON_INTERCEPT_FUNCTION(gmtime); \
1413 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1414 COMMON_INTERCEPT_FUNCTION(ctime); \
1415 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1416 COMMON_INTERCEPT_FUNCTION(asctime); \
1417 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1418 COMMON_INTERCEPT_FUNCTION(mktime);
1420 #define INIT_LOCALTIME_AND_FRIENDS
1421 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1423 #if SANITIZER_INTERCEPT_STRPTIME
1424 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1426 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1428 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
1429 // FIXME: under ASan the call below may write to freed memory and corrupt
1430 // its metadata. See
1431 // https://github.com/google/sanitizers/issues/321.
1432 char *res = REAL(strptime)(s, format, tm);
1433 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1435 // Do not call unpoison_tm here, because strptime does not, in fact,
1436 // initialize the entire struct tm. For example, tm_zone pointer is left
1438 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1442 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1444 #define INIT_STRPTIME
1447 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1448 #include "sanitizer_common_interceptors_format.inc"
1450 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1454 va_start(ap, format); \
1455 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1456 int res = WRAP(vname)(__VA_ARGS__, ap); \
1463 #if SANITIZER_INTERCEPT_SCANF
1465 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1468 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1471 int res = REAL(vname)(__VA_ARGS__); \
1473 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1478 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1479 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1481 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1482 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1484 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1485 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1487 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1488 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1489 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1491 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1493 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1495 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1496 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1498 INTERCEPTOR(int, __isoc23_vscanf, const char *format, va_list ap)
1499 VSCANF_INTERCEPTOR_IMPL(__isoc23_vscanf, false, format, ap)
1501 INTERCEPTOR(int, __isoc23_vsscanf, const char *str, const char *format,
1503 VSCANF_INTERCEPTOR_IMPL(__isoc23_vsscanf, false, str, format, ap)
1505 INTERCEPTOR(int, __isoc23_vfscanf, void *stream, const char *format, va_list ap)
1506 VSCANF_INTERCEPTOR_IMPL(__isoc23_vfscanf, false, stream, format, ap)
1507 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1509 INTERCEPTOR(int, scanf, const char *format, ...)
1510 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1512 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1513 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1515 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1516 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1518 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1519 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1520 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1522 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1523 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1525 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1526 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1528 INTERCEPTOR(int, __isoc23_scanf, const char *format, ...)
1529 FORMAT_INTERCEPTOR_IMPL(__isoc23_scanf, __isoc23_vscanf, format)
1531 INTERCEPTOR(int, __isoc23_fscanf, void *stream, const char *format, ...)
1532 FORMAT_INTERCEPTOR_IMPL(__isoc23_fscanf, __isoc23_vfscanf, stream, format)
1534 INTERCEPTOR(int, __isoc23_sscanf, const char *str, const char *format, ...)
1535 FORMAT_INTERCEPTOR_IMPL(__isoc23_sscanf, __isoc23_vsscanf, str, format)
1540 #if SANITIZER_INTERCEPT_SCANF
1541 #define INIT_SCANF \
1542 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1543 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1544 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1545 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1546 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1547 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1552 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1553 #define INIT_ISOC99_SCANF \
1554 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1555 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1556 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1557 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1558 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1559 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf); \
1560 COMMON_INTERCEPT_FUNCTION(__isoc23_scanf); \
1561 COMMON_INTERCEPT_FUNCTION(__isoc23_sscanf); \
1562 COMMON_INTERCEPT_FUNCTION(__isoc23_fscanf); \
1563 COMMON_INTERCEPT_FUNCTION(__isoc23_vscanf); \
1564 COMMON_INTERCEPT_FUNCTION(__isoc23_vsscanf); \
1565 COMMON_INTERCEPT_FUNCTION(__isoc23_vfscanf);
1567 #define INIT_ISOC99_SCANF
1570 #if SANITIZER_INTERCEPT_PRINTF
1572 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1574 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1578 #define VPRINTF_INTERCEPTOR_RETURN() \
1581 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1583 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1584 if (common_flags()->check_printf) \
1585 printf_common(ctx, format, aq); \
1586 int res = REAL(vname)(__VA_ARGS__); \
1587 VPRINTF_INTERCEPTOR_RETURN(); \
1591 // FIXME: under ASan the REAL() call below may write to freed memory and
1592 // corrupt its metadata. See
1593 // https://github.com/google/sanitizers/issues/321.
1594 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1596 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1597 if (common_flags()->check_printf) { \
1598 printf_common(ctx, format, aq); \
1600 int res = REAL(vname)(str, __VA_ARGS__); \
1602 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1604 VPRINTF_INTERCEPTOR_RETURN(); \
1608 // FIXME: under ASan the REAL() call below may write to freed memory and
1609 // corrupt its metadata. See
1610 // https://github.com/google/sanitizers/issues/321.
1611 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1613 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1614 if (common_flags()->check_printf) { \
1615 printf_common(ctx, format, aq); \
1617 int res = REAL(vname)(str, size, __VA_ARGS__); \
1619 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1621 VPRINTF_INTERCEPTOR_RETURN(); \
1625 // FIXME: under ASan the REAL() call below may write to freed memory and
1626 // corrupt its metadata. See
1627 // https://github.com/google/sanitizers/issues/321.
1628 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1630 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1631 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1632 if (common_flags()->check_printf) { \
1633 printf_common(ctx, format, aq); \
1635 int res = REAL(vname)(strp, __VA_ARGS__); \
1637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1639 VPRINTF_INTERCEPTOR_RETURN(); \
1643 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1644 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1646 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1648 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1650 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1652 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1654 #if SANITIZER_INTERCEPT___PRINTF_CHK
1655 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1656 SIZE_T size_to, const char *format, va_list ap)
1657 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1660 #if SANITIZER_INTERCEPT_PRINTF_L
1661 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1662 const char *format, va_list ap)
1663 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1665 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1666 const char *format, ...)
1667 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1668 #endif // SANITIZER_INTERCEPT_PRINTF_L
1670 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1671 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1673 #if SANITIZER_INTERCEPT___PRINTF_CHK
1674 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1675 const char *format, va_list ap)
1676 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1679 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1680 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1682 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1683 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1684 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1686 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1687 const char *format, va_list ap)
1688 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1690 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1692 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1694 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1696 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1699 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1701 INTERCEPTOR(int, printf, const char *format, ...)
1702 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1704 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1705 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1707 #if SANITIZER_INTERCEPT___PRINTF_CHK
1708 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1709 const char *format, ...)
1710 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1713 INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
1714 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)
1716 #if SANITIZER_INTERCEPT___PRINTF_CHK
1717 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1718 const char *format, ...)
1719 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
1722 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1723 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1725 #if SANITIZER_INTERCEPT___PRINTF_CHK
1726 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1727 SIZE_T size_to, const char *format, ...)
1728 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
1731 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1732 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1734 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1735 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1736 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1738 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1740 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1742 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1743 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1745 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1746 const char *format, ...)
1747 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1750 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1752 #endif // SANITIZER_INTERCEPT_PRINTF
1754 #if SANITIZER_INTERCEPT_PRINTF
1755 #define INIT_PRINTF \
1756 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1757 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1758 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1759 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1760 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1761 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1762 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1763 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1764 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1765 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1770 #if SANITIZER_INTERCEPT___PRINTF_CHK
1771 #define INIT___PRINTF_CHK \
1772 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1773 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1774 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1775 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1776 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1778 #define INIT___PRINTF_CHK
1781 #if SANITIZER_INTERCEPT_PRINTF_L
1782 #define INIT_PRINTF_L \
1783 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1784 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1786 #define INIT_PRINTF_L
1789 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1790 #define INIT_ISOC99_PRINTF \
1791 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1792 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1793 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1794 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1795 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1796 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1797 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1798 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1800 #define INIT_ISOC99_PRINTF
1803 #if SANITIZER_INTERCEPT_IOCTL
1804 #include "sanitizer_common_interceptors_ioctl.inc"
1805 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1806 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1807 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1808 // can trigger a report and we need to be able to unwind through this
1809 // function. On Mac in debug mode we might not have a frame pointer, because
1810 // ioctl_common_[pre|post] doesn't get inlined here.
1811 ENABLE_FRAME_POINTER;
1815 va_start(ap, request);
1816 void *arg = va_arg(ap, void *);
1818 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1820 CHECK(ioctl_initialized);
1822 // Note: TSan does not use common flags, and they are zero-initialized.
1823 // This effectively disables ioctl handling in TSan.
1824 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1826 // Although request is unsigned long, the rest of the interceptor uses it
1827 // as just "unsigned" to save space, because we know that all values fit in
1828 // "unsigned" - they are compile-time constants.
1830 const ioctl_desc *desc = ioctl_lookup(request);
1831 ioctl_desc decoded_desc;
1833 VPrintf(2, "Decoding unknown ioctl 0x%lx\n", request);
1834 if (!ioctl_decode(request, &decoded_desc))
1835 Printf("WARNING: failed decoding unknown ioctl 0x%lx\n", request);
1837 desc = &decoded_desc;
1840 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1841 int res = REAL(ioctl)(d, request, arg);
1842 // FIXME: some ioctls have different return values for success and failure.
1843 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1846 #define INIT_IOCTL \
1848 COMMON_INTERCEPT_FUNCTION(ioctl);
1854 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1856 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1859 internal_strlen(pwd->pw_name) + 1);
1861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1862 internal_strlen(pwd->pw_passwd) + 1);
1863 #if !SANITIZER_ANDROID
1865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1866 internal_strlen(pwd->pw_gecos) + 1);
1868 #if SANITIZER_APPLE || SANITIZER_FREEBSD || SANITIZER_NETBSD
1870 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1871 internal_strlen(pwd->pw_class) + 1);
1874 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1875 internal_strlen(pwd->pw_dir) + 1);
1877 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1878 internal_strlen(pwd->pw_shell) + 1);
1882 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1887 internal_strlen(grp->gr_name) + 1);
1889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1890 internal_strlen(grp->gr_passwd) + 1);
1891 char **p = grp->gr_mem;
1893 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
1895 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1896 (p - grp->gr_mem + 1) * sizeof(*p));
1899 #endif // SANITIZER_POSIX
1901 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1902 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1904 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1906 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1907 __sanitizer_passwd *res = REAL(getpwnam)(name);
1908 unpoison_passwd(ctx, res);
1911 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1913 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1914 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1915 unpoison_passwd(ctx, res);
1918 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1920 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1921 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1922 __sanitizer_group *res = REAL(getgrnam)(name);
1923 unpoison_group(ctx, res);
1926 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1928 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1929 __sanitizer_group *res = REAL(getgrgid)(gid);
1930 unpoison_group(ctx, res);
1933 #define INIT_GETPWNAM_AND_FRIENDS \
1934 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1935 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1936 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1937 COMMON_INTERCEPT_FUNCTION(getgrgid);
1939 #define INIT_GETPWNAM_AND_FRIENDS
1942 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1943 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1944 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1946 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1947 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1948 // FIXME: under ASan the call below may write to freed memory and corrupt
1949 // its metadata. See
1950 // https://github.com/google/sanitizers/issues/321.
1951 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1953 unpoison_passwd(ctx, *result);
1954 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1957 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1958 SIZE_T buflen, __sanitizer_passwd **result) {
1960 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1961 // FIXME: under ASan the call below may write to freed memory and corrupt
1962 // its metadata. See
1963 // https://github.com/google/sanitizers/issues/321.
1964 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1966 unpoison_passwd(ctx, *result);
1967 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1970 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1971 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1973 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1974 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1975 // FIXME: under ASan the call below may write to freed memory and corrupt
1976 // its metadata. See
1977 // https://github.com/google/sanitizers/issues/321.
1978 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1980 unpoison_group(ctx, *result);
1981 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1984 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1985 SIZE_T buflen, __sanitizer_group **result) {
1987 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1988 // FIXME: under ASan the call below may write to freed memory and corrupt
1989 // its metadata. See
1990 // https://github.com/google/sanitizers/issues/321.
1991 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1993 unpoison_group(ctx, *result);
1994 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1997 #define INIT_GETPWNAM_R_AND_FRIENDS \
1998 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1999 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
2000 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
2001 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
2003 #define INIT_GETPWNAM_R_AND_FRIENDS
2006 #if SANITIZER_INTERCEPT_GETPWENT
2007 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
2009 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
2010 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2011 unpoison_passwd(ctx, res);
2014 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2016 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2017 __sanitizer_group *res = REAL(getgrent)(dummy);
2018 unpoison_group(ctx, res);
2021 #define INIT_GETPWENT \
2022 COMMON_INTERCEPT_FUNCTION(getpwent); \
2023 COMMON_INTERCEPT_FUNCTION(getgrent);
2025 #define INIT_GETPWENT
2028 #if SANITIZER_INTERCEPT_FGETPWENT
2029 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2031 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2032 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2033 unpoison_passwd(ctx, res);
2036 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2038 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2039 __sanitizer_group *res = REAL(fgetgrent)(fp);
2040 unpoison_group(ctx, res);
2043 #define INIT_FGETPWENT \
2044 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2045 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2047 #define INIT_FGETPWENT
2050 #if SANITIZER_INTERCEPT_GETPWENT_R
2051 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2052 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2054 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2055 // FIXME: under ASan the call below may write to freed memory and corrupt
2056 // its metadata. See
2057 // https://github.com/google/sanitizers/issues/321.
2058 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2060 unpoison_passwd(ctx, *pwbufp);
2061 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2064 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2065 __sanitizer_group **pwbufp) {
2067 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2068 // FIXME: under ASan the call below may write to freed memory and corrupt
2069 // its metadata. See
2070 // https://github.com/google/sanitizers/issues/321.
2071 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2073 unpoison_group(ctx, *pwbufp);
2074 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2077 #define INIT_GETPWENT_R \
2078 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2079 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2081 #define INIT_GETPWENT_R
2084 #if SANITIZER_INTERCEPT_FGETPWENT_R
2085 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2086 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2088 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2089 // FIXME: under ASan the call below may write to freed memory and corrupt
2090 // its metadata. See
2091 // https://github.com/google/sanitizers/issues/321.
2092 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2094 unpoison_passwd(ctx, *pwbufp);
2095 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2098 #define INIT_FGETPWENT_R \
2099 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2101 #define INIT_FGETPWENT_R
2104 #if SANITIZER_INTERCEPT_FGETGRENT_R
2105 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2106 SIZE_T buflen, __sanitizer_group **pwbufp) {
2108 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2109 // FIXME: under ASan the call below may write to freed memory and corrupt
2110 // its metadata. See
2111 // https://github.com/google/sanitizers/issues/321.
2112 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2114 unpoison_group(ctx, *pwbufp);
2115 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2118 #define INIT_FGETGRENT_R \
2119 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2121 #define INIT_FGETGRENT_R
2124 #if SANITIZER_INTERCEPT_SETPWENT
2125 // The only thing these interceptors do is disable any nested interceptors.
2126 // These functions may open nss modules and call uninstrumented functions from
2127 // them, and we don't want things like strlen() to trigger.
2128 INTERCEPTOR(void, setpwent, int dummy) {
2130 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2131 REAL(setpwent)(dummy);
2133 INTERCEPTOR(void, endpwent, int dummy) {
2135 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2136 REAL(endpwent)(dummy);
2138 INTERCEPTOR(void, setgrent, int dummy) {
2140 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2141 REAL(setgrent)(dummy);
2143 INTERCEPTOR(void, endgrent, int dummy) {
2145 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2146 REAL(endgrent)(dummy);
2148 #define INIT_SETPWENT \
2149 COMMON_INTERCEPT_FUNCTION(setpwent); \
2150 COMMON_INTERCEPT_FUNCTION(endpwent); \
2151 COMMON_INTERCEPT_FUNCTION(setgrent); \
2152 COMMON_INTERCEPT_FUNCTION(endgrent);
2154 #define INIT_SETPWENT
2157 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2158 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2160 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2161 // FIXME: under ASan the call below may write to freed memory and corrupt
2162 // its metadata. See
2163 // https://github.com/google/sanitizers/issues/321.
2164 int res = REAL(clock_getres)(clk_id, tp);
2166 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2170 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2172 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2173 // FIXME: under ASan the call below may write to freed memory and corrupt
2174 // its metadata. See
2175 // https://github.com/google/sanitizers/issues/321.
2176 int res = REAL(clock_gettime)(clk_id, tp);
2178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2183 namespace __sanitizer {
2185 int real_clock_gettime(u32 clk_id, void *tp) {
2186 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2187 return internal_clock_gettime(clk_id, tp);
2188 return REAL(clock_gettime)(clk_id, tp);
2191 } // namespace __sanitizer
2193 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2195 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2196 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2197 return REAL(clock_settime)(clk_id, tp);
2199 #define INIT_CLOCK_GETTIME \
2200 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2201 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2202 COMMON_INTERCEPT_FUNCTION(clock_settime);
2204 #define INIT_CLOCK_GETTIME
2207 #if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
2208 INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
2209 __sanitizer_clockid_t *clockid) {
2211 COMMON_INTERCEPTOR_ENTER(ctx, clock_getcpuclockid, pid, clockid);
2212 int res = REAL(clock_getcpuclockid)(pid, clockid);
2213 if (!res && clockid) {
2214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2219 INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
2220 __sanitizer_clockid_t *clockid) {
2222 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
2223 int res = REAL(pthread_getcpuclockid)(thread, clockid);
2224 if (!res && clockid) {
2225 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2230 #define INIT_CLOCK_GETCPUCLOCKID \
2231 COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid); \
2232 COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
2234 #define INIT_CLOCK_GETCPUCLOCKID
2237 #if SANITIZER_INTERCEPT_GETITIMER
2238 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2240 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2241 // FIXME: under ASan the call below may write to freed memory and corrupt
2242 // its metadata. See
2243 // https://github.com/google/sanitizers/issues/321.
2244 int res = REAL(getitimer)(which, curr_value);
2245 if (!res && curr_value) {
2246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2250 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2252 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2254 // itimerval can contain padding that may be legitimately uninitialized
2255 const struct __sanitizer_itimerval *nv =
2256 (const struct __sanitizer_itimerval *)new_value;
2257 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2258 sizeof(__sanitizer_time_t));
2259 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2260 sizeof(__sanitizer_suseconds_t));
2261 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2262 sizeof(__sanitizer_time_t));
2263 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2264 sizeof(__sanitizer_suseconds_t));
2266 // FIXME: under ASan the call below may write to freed memory and corrupt
2267 // its metadata. See
2268 // https://github.com/google/sanitizers/issues/321.
2269 int res = REAL(setitimer)(which, new_value, old_value);
2270 if (!res && old_value) {
2271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2275 #define INIT_GETITIMER \
2276 COMMON_INTERCEPT_FUNCTION(getitimer); \
2277 COMMON_INTERCEPT_FUNCTION(setitimer);
2279 #define INIT_GETITIMER
2282 #if SANITIZER_INTERCEPT_GLOB
2283 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2285 // +1 for NULL pointer at the end.
2286 if (pglob->gl_pathv)
2287 COMMON_INTERCEPTOR_WRITE_RANGE(
2288 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2289 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2290 char *p = pglob->gl_pathv[i];
2291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, internal_strlen(p) + 1);
2295 #if SANITIZER_SOLARIS
2296 INTERCEPTOR(int, glob, const char *pattern, int flags,
2297 int (*errfunc)(const char *epath, int eerrno),
2298 __sanitizer_glob_t *pglob) {
2300 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2301 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2302 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2303 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2307 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2309 static void wrapped_gl_closedir(void *dir) {
2310 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2311 pglob_copy->gl_closedir(dir);
2314 static void *wrapped_gl_readdir(void *dir) {
2315 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2316 return pglob_copy->gl_readdir(dir);
2319 static void *wrapped_gl_opendir(const char *s) {
2320 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2321 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2322 return pglob_copy->gl_opendir(s);
2325 static int wrapped_gl_lstat(const char *s, void *st) {
2326 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2327 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2328 return pglob_copy->gl_lstat(s, st);
2331 static int wrapped_gl_stat(const char *s, void *st) {
2332 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2333 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2334 return pglob_copy->gl_stat(s, st);
2337 static const __sanitizer_glob_t kGlobCopy = {
2339 0, wrapped_gl_closedir, wrapped_gl_readdir,
2340 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2342 INTERCEPTOR(int, glob, const char *pattern, int flags,
2343 int (*errfunc)(const char *epath, int eerrno),
2344 __sanitizer_glob_t *pglob) {
2346 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2347 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2348 __sanitizer_glob_t glob_copy;
2349 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2350 if (flags & glob_altdirfunc) {
2351 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2352 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2353 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2354 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2355 Swap(pglob->gl_stat, glob_copy.gl_stat);
2356 pglob_copy = &glob_copy;
2358 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2359 if (flags & glob_altdirfunc) {
2360 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2361 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2362 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2363 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2364 Swap(pglob->gl_stat, glob_copy.gl_stat);
2367 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2370 #endif // SANITIZER_SOLARIS
2372 COMMON_INTERCEPT_FUNCTION(glob);
2373 #else // SANITIZER_INTERCEPT_GLOB
2375 #endif // SANITIZER_INTERCEPT_GLOB
2377 #if SANITIZER_INTERCEPT_GLOB64
2378 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2379 int (*errfunc)(const char *epath, int eerrno),
2380 __sanitizer_glob_t *pglob) {
2382 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2383 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2384 __sanitizer_glob_t glob_copy;
2385 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2386 if (flags & glob_altdirfunc) {
2387 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2388 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2389 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2390 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2391 Swap(pglob->gl_stat, glob_copy.gl_stat);
2392 pglob_copy = &glob_copy;
2394 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2395 if (flags & glob_altdirfunc) {
2396 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2397 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2398 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2399 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2400 Swap(pglob->gl_stat, glob_copy.gl_stat);
2403 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2406 #define INIT_GLOB64 \
2407 COMMON_INTERCEPT_FUNCTION(glob64);
2408 #else // SANITIZER_INTERCEPT_GLOB64
2410 #endif // SANITIZER_INTERCEPT_GLOB64
2412 #if SANITIZER_INTERCEPT___B64_TO
2413 INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
2414 char *target, SIZE_T targsize) {
2416 COMMON_INTERCEPTOR_ENTER(ctx, __b64_ntop, src, srclength, target, targsize);
2417 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclength);
2418 int res = REAL(__b64_ntop)(src, srclength, target, targsize);
2420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res + 1);
2423 INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
2425 COMMON_INTERCEPTOR_ENTER(ctx, __b64_pton, src, target, targsize);
2426 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
2427 int res = REAL(__b64_pton)(src, target, targsize);
2429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res);
2432 #define INIT___B64_TO \
2433 COMMON_INTERCEPT_FUNCTION(__b64_ntop); \
2434 COMMON_INTERCEPT_FUNCTION(__b64_pton);
2435 #else // SANITIZER_INTERCEPT___B64_TO
2436 #define INIT___B64_TO
2437 #endif // SANITIZER_INTERCEPT___B64_TO
2439 #if SANITIZER_INTERCEPT_DN_COMP_EXPAND
2440 # if __GLIBC_PREREQ(2, 34)
2441 // Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
2442 # define DN_COMP_INTERCEPTOR_NAME dn_comp
2443 # define DN_EXPAND_INTERCEPTOR_NAME dn_expand
2445 # define DN_COMP_INTERCEPTOR_NAME __dn_comp
2446 # define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
2448 INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
2449 unsigned char *comp_dn, int length, unsigned char **dnptrs,
2450 unsigned char **lastdnptr) {
2452 COMMON_INTERCEPTOR_ENTER(ctx, DN_COMP_INTERCEPTOR_NAME, exp_dn, comp_dn,
2453 length, dnptrs, lastdnptr);
2454 int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
2457 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, comp_dn, res);
2458 if (dnptrs && lastdnptr) {
2459 unsigned char **p = dnptrs;
2460 for (; p != lastdnptr && *p; ++p)
2464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dnptrs, (p - dnptrs) * sizeof(*p));
2469 INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
2470 unsigned char const *end, unsigned char const *src, char *dest,
2473 COMMON_INTERCEPTOR_ENTER(ctx, DN_EXPAND_INTERCEPTOR_NAME, base, end, src,
2475 // TODO: add read check if __dn_comp intercept added
2476 int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
2478 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, internal_strlen(dest) + 1);
2481 # define INIT_DN_COMP_EXPAND \
2482 COMMON_INTERCEPT_FUNCTION(DN_COMP_INTERCEPTOR_NAME); \
2483 COMMON_INTERCEPT_FUNCTION(DN_EXPAND_INTERCEPTOR_NAME);
2484 #else // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2485 # define INIT_DN_COMP_EXPAND
2486 #endif // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2488 #if SANITIZER_INTERCEPT_POSIX_SPAWN
2490 template <class RealSpawnPtr>
2491 static int PosixSpawnImpl(void *ctx, RealSpawnPtr *real_posix_spawn, pid_t *pid,
2492 const char *file_or_path, const void *file_actions,
2493 const void *attrp, char *const argv[],
2494 char *const envp[]) {
2495 COMMON_INTERCEPTOR_READ_RANGE(ctx, file_or_path,
2496 internal_strlen(file_or_path) + 1);
2498 for (char *const *s = argv; ; ++s) {
2499 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2501 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2505 for (char *const *s = envp; ; ++s) {
2506 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2508 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2512 real_posix_spawn(pid, file_or_path, file_actions, attrp, argv, envp);
2514 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pid, sizeof(*pid));
2517 INTERCEPTOR(int, posix_spawn, pid_t *pid, const char *path,
2518 const void *file_actions, const void *attrp, char *const argv[],
2519 char *const envp[]) {
2521 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawn, pid, path, file_actions, attrp,
2523 return PosixSpawnImpl(ctx, REAL(posix_spawn), pid, path, file_actions, attrp,
2526 INTERCEPTOR(int, posix_spawnp, pid_t *pid, const char *file,
2527 const void *file_actions, const void *attrp, char *const argv[],
2528 char *const envp[]) {
2530 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawnp, pid, file, file_actions, attrp,
2532 return PosixSpawnImpl(ctx, REAL(posix_spawnp), pid, file, file_actions, attrp,
2535 # define INIT_POSIX_SPAWN \
2536 COMMON_INTERCEPT_FUNCTION(posix_spawn); \
2537 COMMON_INTERCEPT_FUNCTION(posix_spawnp);
2538 #else // SANITIZER_INTERCEPT_POSIX_SPAWN
2539 # define INIT_POSIX_SPAWN
2540 #endif // SANITIZER_INTERCEPT_POSIX_SPAWN
2542 #if SANITIZER_INTERCEPT_WAIT
2543 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2544 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2546 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2548 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2549 // FIXME: under ASan the call below may write to freed memory and corrupt
2550 // its metadata. See
2551 // https://github.com/google/sanitizers/issues/321.
2552 int res = REAL(wait)(status);
2553 if (res != -1 && status)
2554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2557 // On FreeBSD id_t is always 64-bit wide.
2558 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2559 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2562 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2566 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2567 // FIXME: under ASan the call below may write to freed memory and corrupt
2568 // its metadata. See
2569 // https://github.com/google/sanitizers/issues/321.
2570 int res = REAL(waitid)(idtype, id, infop, options);
2571 if (res != -1 && infop)
2572 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2575 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2577 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2578 // FIXME: under ASan the call below may write to freed memory and corrupt
2579 // its metadata. See
2580 // https://github.com/google/sanitizers/issues/321.
2581 int res = REAL(waitpid)(pid, status, options);
2582 if (res != -1 && status)
2583 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2586 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2588 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2589 // FIXME: under ASan the call below may write to freed memory and corrupt
2590 // its metadata. See
2591 // https://github.com/google/sanitizers/issues/321.
2592 int res = REAL(wait3)(status, options, rusage);
2594 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2595 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2599 #if SANITIZER_ANDROID
2600 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2602 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2603 // FIXME: under ASan the call below may write to freed memory and corrupt
2604 // its metadata. See
2605 // https://github.com/google/sanitizers/issues/321.
2606 int res = REAL(__wait4)(pid, status, options, rusage);
2608 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2609 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2613 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2615 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2617 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2618 // FIXME: under ASan the call below may write to freed memory and corrupt
2619 // its metadata. See
2620 // https://github.com/google/sanitizers/issues/321.
2621 int res = REAL(wait4)(pid, status, options, rusage);
2623 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2624 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2628 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2629 #endif // SANITIZER_ANDROID
2631 COMMON_INTERCEPT_FUNCTION(wait); \
2632 COMMON_INTERCEPT_FUNCTION(waitid); \
2633 COMMON_INTERCEPT_FUNCTION(waitpid); \
2634 COMMON_INTERCEPT_FUNCTION(wait3);
2640 #if SANITIZER_INTERCEPT_INET
2641 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2643 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2644 uptr sz = __sanitizer_in_addr_sz(af);
2645 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2646 // FIXME: figure out read size based on the address family.
2647 // FIXME: under ASan the call below may write to freed memory and corrupt
2648 // its metadata. See
2649 // https://github.com/google/sanitizers/issues/321.
2650 char *res = REAL(inet_ntop)(af, src, dst, size);
2651 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
2654 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2656 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2657 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2658 // FIXME: figure out read size based on the address family.
2659 // FIXME: under ASan the call below may write to freed memory and corrupt
2660 // its metadata. See
2661 // https://github.com/google/sanitizers/issues/321.
2662 int res = REAL(inet_pton)(af, src, dst);
2664 uptr sz = __sanitizer_in_addr_sz(af);
2665 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2670 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2671 COMMON_INTERCEPT_FUNCTION(inet_pton);
2676 #if SANITIZER_INTERCEPT_INET
2677 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2679 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2680 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, internal_strlen(cp) + 1);
2681 // FIXME: under ASan the call below may write to freed memory and corrupt
2682 // its metadata. See
2683 // https://github.com/google/sanitizers/issues/321.
2684 int res = REAL(inet_aton)(cp, dst);
2686 uptr sz = __sanitizer_in_addr_sz(af_inet);
2687 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2691 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2693 #define INIT_INET_ATON
2696 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2697 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2699 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2700 // FIXME: under ASan the call below may write to freed memory and corrupt
2701 // its metadata. See
2702 // https://github.com/google/sanitizers/issues/321.
2703 int res = REAL(pthread_getschedparam)(thread, policy, param);
2705 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2706 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2710 #define INIT_PTHREAD_GETSCHEDPARAM \
2711 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2713 #define INIT_PTHREAD_GETSCHEDPARAM
2716 #if SANITIZER_INTERCEPT_GETADDRINFO
2717 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2718 struct __sanitizer_addrinfo *hints,
2719 struct __sanitizer_addrinfo **out) {
2721 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2722 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, internal_strlen(node) + 1);
2724 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, internal_strlen(service) + 1);
2726 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2727 // FIXME: under ASan the call below may write to freed memory and corrupt
2728 // its metadata. See
2729 // https://github.com/google/sanitizers/issues/321.
2730 int res = REAL(getaddrinfo)(node, service, hints, out);
2731 if (res == 0 && out) {
2732 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2733 struct __sanitizer_addrinfo *p = *out;
2735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2737 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2738 if (p->ai_canonname)
2739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2740 internal_strlen(p->ai_canonname) + 1);
2746 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2748 #define INIT_GETADDRINFO
2751 #if SANITIZER_INTERCEPT_GETNAMEINFO
2752 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2753 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2755 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2756 serv, servlen, flags);
2757 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2758 // There is padding in in_addr that may make this too noisy
2759 // FIXME: under ASan the call below may write to freed memory and corrupt
2760 // its metadata. See
2761 // https://github.com/google/sanitizers/issues/321.
2763 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2765 if (host && hostlen)
2766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, internal_strlen(host) + 1);
2767 if (serv && servlen)
2768 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, internal_strlen(serv) + 1);
2772 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2774 #define INIT_GETNAMEINFO
2777 #if SANITIZER_INTERCEPT_GETSOCKNAME
2778 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {
2780 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2783 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2786 // FIXME: under ASan the call below may write to freed memory and corrupt
2787 // its metadata. See
2788 // https://github.com/google/sanitizers/issues/321.
2789 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2790 if (!res && addr && addrlen) {
2791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2795 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2797 #define INIT_GETSOCKNAME
2800 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2801 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2802 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2804 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, internal_strlen(h->h_name) + 1);
2805 char **p = h->h_aliases;
2807 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
2810 COMMON_INTERCEPTOR_WRITE_RANGE(
2811 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2814 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2817 COMMON_INTERCEPTOR_WRITE_RANGE(
2818 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2822 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2823 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2825 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2826 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2827 if (res) write_hostent(ctx, res);
2831 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2834 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2835 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2836 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2837 if (res) write_hostent(ctx, res);
2841 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2843 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2844 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2845 if (res) write_hostent(ctx, res);
2848 #define INIT_GETHOSTBYNAME \
2849 COMMON_INTERCEPT_FUNCTION(gethostent); \
2850 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2851 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2853 #define INIT_GETHOSTBYNAME
2854 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2856 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2857 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2859 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2860 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2861 if (res) write_hostent(ctx, res);
2864 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2866 #define INIT_GETHOSTBYNAME2
2867 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2869 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2870 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2871 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2874 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2876 // FIXME: under ASan the call below may write to freed memory and corrupt
2877 // its metadata. See
2878 // https://github.com/google/sanitizers/issues/321.
2879 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2882 if (res == 0 && *result) write_hostent(ctx, *result);
2885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2888 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2890 #define INIT_GETHOSTBYNAME_R
2893 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2894 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2895 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2897 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2899 // FIXME: under ASan the call below may write to freed memory and corrupt
2900 // its metadata. See
2901 // https://github.com/google/sanitizers/issues/321.
2902 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2905 if (res == 0 && *result) write_hostent(ctx, *result);
2908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2911 #define INIT_GETHOSTENT_R \
2912 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2914 #define INIT_GETHOSTENT_R
2917 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2918 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2919 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2920 __sanitizer_hostent **result, int *h_errnop) {
2922 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2923 buflen, result, h_errnop);
2924 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2925 // FIXME: under ASan the call below may write to freed memory and corrupt
2926 // its metadata. See
2927 // https://github.com/google/sanitizers/issues/321.
2928 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2932 if (res == 0 && *result) write_hostent(ctx, *result);
2935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2938 #define INIT_GETHOSTBYADDR_R \
2939 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2941 #define INIT_GETHOSTBYADDR_R
2944 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2945 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2946 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2947 __sanitizer_hostent **result, int *h_errnop) {
2949 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2951 // FIXME: under ASan the call below may write to freed memory and corrupt
2952 // its metadata. See
2953 // https://github.com/google/sanitizers/issues/321.
2955 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2957 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2958 if (res == 0 && *result) write_hostent(ctx, *result);
2961 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2964 #define INIT_GETHOSTBYNAME2_R \
2965 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2967 #define INIT_GETHOSTBYNAME2_R
2970 #if SANITIZER_INTERCEPT_GETSOCKOPT
2971 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2974 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2976 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2977 // FIXME: under ASan the call below may write to freed memory and corrupt
2978 // its metadata. See
2979 // https://github.com/google/sanitizers/issues/321.
2980 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2982 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2985 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2987 #define INIT_GETSOCKOPT
2990 #if SANITIZER_INTERCEPT_ACCEPT
2991 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2993 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2994 unsigned addrlen0 = 0;
2996 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2997 addrlen0 = *addrlen;
2999 int fd2 = REAL(accept)(fd, addr, addrlen);
3001 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3002 if (addr && addrlen)
3003 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3007 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
3012 #if SANITIZER_INTERCEPT_ACCEPT4
3013 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
3015 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
3016 unsigned addrlen0 = 0;
3018 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3019 addrlen0 = *addrlen;
3021 // FIXME: under ASan the call below may write to freed memory and corrupt
3022 // its metadata. See
3023 // https://github.com/google/sanitizers/issues/321.
3024 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
3026 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3027 if (addr && addrlen)
3028 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3032 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
3034 #define INIT_ACCEPT4
3037 #if SANITIZER_INTERCEPT_PACCEPT
3038 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
3039 __sanitizer_sigset_t *set, int f) {
3041 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
3042 unsigned addrlen0 = 0;
3044 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3045 addrlen0 = *addrlen;
3047 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3048 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
3050 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3051 if (addr && addrlen)
3052 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3056 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
3058 #define INIT_PACCEPT
3061 #if SANITIZER_INTERCEPT_MODF
3062 INTERCEPTOR(double, modf, double x, double *iptr) {
3064 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
3065 // FIXME: under ASan the call below may write to freed memory and corrupt
3066 // its metadata. See
3067 // https://github.com/google/sanitizers/issues/321.
3068 double res = REAL(modf)(x, iptr);
3070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3074 INTERCEPTOR(float, modff, float x, float *iptr) {
3076 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
3077 // FIXME: under ASan the call below may write to freed memory and corrupt
3078 // its metadata. See
3079 // https://github.com/google/sanitizers/issues/321.
3080 float res = REAL(modff)(x, iptr);
3082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3086 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
3088 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
3089 // FIXME: under ASan the call below may write to freed memory and corrupt
3090 // its metadata. See
3091 // https://github.com/google/sanitizers/issues/321.
3092 long double res = REAL(modfl)(x, iptr);
3094 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3099 COMMON_INTERCEPT_FUNCTION(modf); \
3100 COMMON_INTERCEPT_FUNCTION(modff); \
3101 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
3106 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
3107 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3109 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
3110 if (msg->msg_name && msg->msg_namelen)
3111 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3112 if (msg->msg_iov && msg->msg_iovlen)
3113 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
3114 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3115 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3116 if (msg->msg_control && msg->msg_controllen)
3117 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
3121 #if SANITIZER_INTERCEPT_RECVMSG
3122 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
3125 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
3126 // FIXME: under ASan the call below may write to freed memory and corrupt
3127 // its metadata. See
3128 // https://github.com/google/sanitizers/issues/321.
3129 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
3131 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3133 write_msghdr(ctx, msg, res);
3134 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
3139 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
3141 #define INIT_RECVMSG
3144 #if SANITIZER_INTERCEPT_RECVMMSG
3145 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3146 unsigned int vlen, int flags, void *timeout) {
3148 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
3149 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3150 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
3152 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3153 for (int i = 0; i < res; ++i) {
3154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3155 sizeof(msgvec[i].msg_len));
3156 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3157 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
3162 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
3164 #define INIT_RECVMMSG
3167 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
3168 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
3169 const unsigned kCmsgDataOffset =
3170 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
3172 char *p = (char *)control;
3173 char *const control_end = p + controllen;
3175 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3176 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3177 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3179 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3181 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3182 sizeof(cmsg->cmsg_level));
3183 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3184 sizeof(cmsg->cmsg_type));
3186 if (cmsg->cmsg_len > kCmsgDataOffset) {
3187 char *data = p + kCmsgDataOffset;
3188 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3189 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3192 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3196 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3199 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3208 if (msg->msg_name && msg->msg_namelen)
3209 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3210 if (msg->msg_iov && msg->msg_iovlen)
3211 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3212 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3213 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3214 if (msg->msg_control && msg->msg_controllen)
3215 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3219 #if SANITIZER_INTERCEPT_SENDMSG
3220 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3223 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3225 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3226 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3228 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3229 if (common_flags()->intercept_send && res >= 0 && msg)
3230 read_msghdr(ctx, msg, res);
3233 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3235 #define INIT_SENDMSG
3238 #if SANITIZER_INTERCEPT_SENDMMSG
3239 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3240 unsigned vlen, int flags) {
3242 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3244 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3245 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3247 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3248 if (res >= 0 && msgvec) {
3249 for (int i = 0; i < res; ++i) {
3250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3251 sizeof(msgvec[i].msg_len));
3252 if (common_flags()->intercept_send)
3253 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3258 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3260 #define INIT_SENDMMSG
3263 #if SANITIZER_INTERCEPT_SYSMSG
3264 INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
3267 COMMON_INTERCEPTOR_ENTER(ctx, msgsnd, msqid, msgp, msgsz, msgflg);
3269 COMMON_INTERCEPTOR_READ_RANGE(ctx, msgp, sizeof(long) + msgsz);
3270 int res = REAL(msgsnd)(msqid, msgp, msgsz, msgflg);
3274 INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
3275 long msgtyp, int msgflg) {
3277 COMMON_INTERCEPTOR_ENTER(ctx, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
3278 SSIZE_T len = REAL(msgrcv)(msqid, msgp, msgsz, msgtyp, msgflg);
3280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msgp, sizeof(long) + len);
3284 #define INIT_SYSMSG \
3285 COMMON_INTERCEPT_FUNCTION(msgsnd); \
3286 COMMON_INTERCEPT_FUNCTION(msgrcv);
3291 #if SANITIZER_INTERCEPT_GETPEERNAME
3292 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3294 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3297 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3300 // FIXME: under ASan the call below may write to freed memory and corrupt
3301 // its metadata. See
3302 // https://github.com/google/sanitizers/issues/321.
3303 int res = REAL(getpeername)(sockfd, addr, addrlen);
3304 if (!res && addr && addrlen) {
3305 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3309 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3311 #define INIT_GETPEERNAME
3314 #if SANITIZER_INTERCEPT_SYSINFO
3315 INTERCEPTOR(int, sysinfo, void *info) {
3317 // FIXME: under ASan the call below may write to freed memory and corrupt
3318 // its metadata. See
3319 // https://github.com/google/sanitizers/issues/321.
3320 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3321 int res = REAL(sysinfo)(info);
3323 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3326 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3328 #define INIT_SYSINFO
3331 #if SANITIZER_INTERCEPT_READDIR
3332 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3334 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3335 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3336 __sanitizer_dirent *res = REAL(opendir)(path);
3338 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3342 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3344 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3345 // FIXME: under ASan the call below may write to freed memory and corrupt
3346 // its metadata. See
3347 // https://github.com/google/sanitizers/issues/321.
3348 __sanitizer_dirent *res = REAL(readdir)(dirp);
3350 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3354 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3355 __sanitizer_dirent **result) {
3357 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3358 // FIXME: under ASan the call below may write to freed memory and corrupt
3359 // its metadata. See
3360 // https://github.com/google/sanitizers/issues/321.
3361 int res = REAL(readdir_r)(dirp, entry, result);
3363 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3365 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3370 #define INIT_READDIR \
3371 COMMON_INTERCEPT_FUNCTION(opendir); \
3372 COMMON_INTERCEPT_FUNCTION(readdir); \
3373 COMMON_INTERCEPT_FUNCTION(readdir_r);
3375 #define INIT_READDIR
3378 #if SANITIZER_INTERCEPT_READDIR64
3379 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3381 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3382 // FIXME: under ASan the call below may write to freed memory and corrupt
3383 // its metadata. See
3384 // https://github.com/google/sanitizers/issues/321.
3385 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3387 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3391 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3392 __sanitizer_dirent64 **result) {
3394 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3395 // FIXME: under ASan the call below may write to freed memory and corrupt
3396 // its metadata. See
3397 // https://github.com/google/sanitizers/issues/321.
3398 int res = REAL(readdir64_r)(dirp, entry, result);
3400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3402 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3406 #define INIT_READDIR64 \
3407 COMMON_INTERCEPT_FUNCTION(readdir64); \
3408 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3410 #define INIT_READDIR64
3413 #if SANITIZER_INTERCEPT_PTRACE
3414 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3416 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3417 __sanitizer_iovec local_iovec;
3420 if (request == ptrace_setregs) {
3421 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3422 } else if (request == ptrace_setfpregs) {
3423 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3424 } else if (request == ptrace_setfpxregs) {
3425 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3426 } else if (request == ptrace_setvfpregs) {
3427 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3428 } else if (request == ptrace_setsiginfo) {
3429 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3431 // Some kernel might zero the iovec::iov_base in case of invalid
3432 // write access. In this case copy the invalid address for further
3434 } else if (request == ptrace_setregset || request == ptrace_getregset) {
3435 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3436 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3437 local_iovec = *iovec;
3438 if (request == ptrace_setregset)
3439 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3443 // FIXME: under ASan the call below may write to freed memory and corrupt
3444 // its metadata. See
3445 // https://github.com/google/sanitizers/issues/321.
3446 uptr res = REAL(ptrace)(request, pid, addr, data);
3449 // Note that PEEK* requests assign different meaning to the return value.
3450 // This function does not handle them (nor does it need to).
3451 if (request == ptrace_getregs) {
3452 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3453 } else if (request == ptrace_getfpregs) {
3454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3455 } else if (request == ptrace_getfpxregs) {
3456 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3457 } else if (request == ptrace_getvfpregs) {
3458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3459 } else if (request == ptrace_getsiginfo) {
3460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3461 } else if (request == ptrace_geteventmsg) {
3462 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3463 } else if (request == ptrace_getregset) {
3464 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3465 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3467 local_iovec.iov_len);
3473 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3478 #if SANITIZER_INTERCEPT_SETLOCALE
3479 static void unpoison_ctype_arrays(void *ctx) {
3480 #if SANITIZER_NETBSD
3481 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3482 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3484 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3488 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3490 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3492 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, internal_strlen(locale) + 1);
3493 char *res = REAL(setlocale)(category, locale);
3495 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3496 unpoison_ctype_arrays(ctx);
3501 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3503 #define INIT_SETLOCALE
3506 #if SANITIZER_INTERCEPT_GETCWD
3507 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3509 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3510 // FIXME: under ASan the call below may write to freed memory and corrupt
3511 // its metadata. See
3512 // https://github.com/google/sanitizers/issues/321.
3513 char *res = REAL(getcwd)(buf, size);
3514 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3517 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3522 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3523 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3525 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3526 // FIXME: under ASan the call below may write to freed memory and corrupt
3527 // its metadata. See
3528 // https://github.com/google/sanitizers/issues/321.
3529 char *res = REAL(get_current_dir_name)(fake);
3530 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3534 #define INIT_GET_CURRENT_DIR_NAME \
3535 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3537 #define INIT_GET_CURRENT_DIR_NAME
3540 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3542 if (nptr == *endptr) {
3543 // No digits were found at strtol call, we need to find out the last
3544 // symbol accessed by strtoll on our own.
3545 // We get this symbol by skipping leading blanks and optional +/- sign.
3546 while (IsSpace(*nptr)) nptr++;
3547 if (*nptr == '+' || *nptr == '-') nptr++;
3548 *endptr = const_cast<char *>(nptr);
3550 CHECK(*endptr >= nptr);
3553 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3554 char **endptr, char *real_endptr, int base) {
3556 *endptr = real_endptr;
3557 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3559 // If base has unsupported value, strtol can exit with EINVAL
3560 // without reading any characters. So do additional checks only
3561 // if base is valid.
3562 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3563 if (is_valid_base) {
3564 FixRealStrtolEndptr(nptr, &real_endptr);
3566 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3567 (real_endptr - nptr) + 1 : 0);
3570 #if SANITIZER_INTERCEPT_STRTOIMAX
3571 template <typename Fn>
3572 static ALWAYS_INLINE auto StrtoimaxImpl(void *ctx, Fn real, const char *nptr,
3573 char **endptr, int base)
3574 -> decltype(real(nullptr, nullptr, 0)) {
3576 auto res = real(nptr, &real_endptr, base);
3577 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3581 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3583 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3584 return StrtoimaxImpl(ctx, REAL(strtoimax), nptr, endptr, base);
3586 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3588 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3589 return StrtoimaxImpl(ctx, REAL(strtoumax), nptr, endptr, base);
3592 #define INIT_STRTOIMAX \
3593 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3594 COMMON_INTERCEPT_FUNCTION(strtoumax);
3596 #define INIT_STRTOIMAX
3599 #if SANITIZER_INTERCEPT_STRTOIMAX && SANITIZER_GLIBC
3600 INTERCEPTOR(INTMAX_T, __isoc23_strtoimax, const char *nptr, char **endptr, int base) {
3602 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoimax, nptr, endptr, base);
3603 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoimax), nptr, endptr, base);
3605 INTERCEPTOR(UINTMAX_T, __isoc23_strtoumax, const char *nptr, char **endptr, int base) {
3607 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoumax, nptr, endptr, base);
3608 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoumax), nptr, endptr, base);
3611 # define INIT_STRTOIMAX_C23 \
3612 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoimax); \
3613 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoumax);
3615 # define INIT_STRTOIMAX_C23
3618 #if SANITIZER_INTERCEPT_MBSTOWCS
3619 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3621 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3622 // FIXME: under ASan the call below may write to freed memory and corrupt
3623 // its metadata. See
3624 // https://github.com/google/sanitizers/issues/321.
3625 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3626 if (res != (SIZE_T) - 1 && dest) {
3627 SIZE_T write_cnt = res + (res < len);
3628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3633 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3636 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3637 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3638 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3639 // FIXME: under ASan the call below may write to freed memory and corrupt
3640 // its metadata. See
3641 // https://github.com/google/sanitizers/issues/321.
3642 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3643 if (res != (SIZE_T)(-1) && dest && src) {
3644 // This function, and several others, may or may not write the terminating
3645 // \0 character. They write it iff they clear *src.
3646 SIZE_T write_cnt = res + !*src;
3647 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3652 #define INIT_MBSTOWCS \
3653 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3654 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3656 #define INIT_MBSTOWCS
3659 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3660 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3661 SIZE_T len, void *ps) {
3663 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3665 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3666 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3668 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3669 // FIXME: under ASan the call below may write to freed memory and corrupt
3670 // its metadata. See
3671 // https://github.com/google/sanitizers/issues/321.
3672 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3673 if (res != (SIZE_T)(-1) && dest && src) {
3674 SIZE_T write_cnt = res + !*src;
3675 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3680 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3682 #define INIT_MBSNRTOWCS
3685 #if SANITIZER_INTERCEPT_WCSTOMBS
3686 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3688 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3689 // FIXME: under ASan the call below may write to freed memory and corrupt
3690 // its metadata. See
3691 // https://github.com/google/sanitizers/issues/321.
3692 SIZE_T res = REAL(wcstombs)(dest, src, len);
3693 if (res != (SIZE_T) - 1 && dest) {
3694 SIZE_T write_cnt = res + (res < len);
3695 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3700 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3703 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3704 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3705 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3706 // FIXME: under ASan the call below may write to freed memory and corrupt
3707 // its metadata. See
3708 // https://github.com/google/sanitizers/issues/321.
3709 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3710 if (res != (SIZE_T) - 1 && dest && src) {
3711 SIZE_T write_cnt = res + !*src;
3712 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3717 #define INIT_WCSTOMBS \
3718 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3719 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3721 #define INIT_WCSTOMBS
3724 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3725 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3726 SIZE_T len, void *ps) {
3728 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3730 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3731 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3733 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3734 // FIXME: under ASan the call below may write to freed memory and corrupt
3735 // its metadata. See
3736 // https://github.com/google/sanitizers/issues/321.
3737 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3738 if (res != ((SIZE_T)-1) && dest && src) {
3739 SIZE_T write_cnt = res + !*src;
3740 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3745 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3747 #define INIT_WCSNRTOMBS
3751 #if SANITIZER_INTERCEPT_WCRTOMB
3752 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3754 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3755 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3758 return REAL(wcrtomb)(dest, src, ps);
3760 char local_dest[32];
3761 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3762 if (res != ((SIZE_T)-1)) {
3763 CHECK_LE(res, sizeof(local_dest));
3764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3765 REAL(memcpy)(dest, local_dest, res);
3770 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3772 #define INIT_WCRTOMB
3775 #if SANITIZER_INTERCEPT_WCTOMB
3776 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3778 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3780 return REAL(wctomb)(dest, src);
3782 char local_dest[32];
3783 int res = REAL(wctomb)(local_dest, src);
3785 CHECK_LE(res, sizeof(local_dest));
3786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3787 REAL(memcpy)(dest, local_dest, res);
3792 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3797 #if SANITIZER_INTERCEPT_TCGETATTR
3798 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3800 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3801 // FIXME: under ASan the call below may write to freed memory and corrupt
3802 // its metadata. See
3803 // https://github.com/google/sanitizers/issues/321.
3804 int res = REAL(tcgetattr)(fd, termios_p);
3805 if (!res && termios_p)
3806 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3810 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3812 #define INIT_TCGETATTR
3815 #if SANITIZER_INTERCEPT_REALPATH
3816 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3818 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3819 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3821 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3822 // version of a versioned symbol. For realpath(), this gives us something
3823 // (called __old_realpath) that does not handle NULL in the second argument.
3824 // Handle it as part of the interceptor.
3825 char *allocated_path = nullptr;
3827 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3829 char *res = REAL(realpath)(path, resolved_path);
3830 if (allocated_path && !res)
3831 WRAP(free)(allocated_path);
3832 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3835 # define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3837 #define INIT_REALPATH
3840 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3841 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3843 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3844 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3845 char *res = REAL(canonicalize_file_name)(path);
3846 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3849 #define INIT_CANONICALIZE_FILE_NAME \
3850 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3852 #define INIT_CANONICALIZE_FILE_NAME
3855 #if SANITIZER_INTERCEPT_CONFSTR
3856 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3858 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3859 // FIXME: under ASan the call below may write to freed memory and corrupt
3860 // its metadata. See
3861 // https://github.com/google/sanitizers/issues/321.
3862 SIZE_T res = REAL(confstr)(name, buf, len);
3864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3867 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3869 #define INIT_CONFSTR
3872 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3873 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3875 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3876 // FIXME: under ASan the call below may write to freed memory and corrupt
3877 // its metadata. See
3878 // https://github.com/google/sanitizers/issues/321.
3879 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3880 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3883 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3885 #define INIT_SCHED_GETAFFINITY
3888 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3889 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3891 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3892 int res = REAL(sched_getparam)(pid, param);
3893 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3896 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3898 #define INIT_SCHED_GETPARAM
3901 #if SANITIZER_INTERCEPT_STRERROR
3902 INTERCEPTOR(char *, strerror, int errnum) {
3904 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3905 COMMON_INTERCEPTOR_STRERROR();
3906 char *res = REAL(strerror)(errnum);
3907 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3910 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3912 #define INIT_STRERROR
3915 #if SANITIZER_INTERCEPT_STRERROR_R
3916 // There are 2 versions of strerror_r:
3917 // * POSIX version returns 0 on success, negative error code on failure,
3918 // writes message to buf.
3919 // * GNU version returns message pointer, which points to either buf or some
3921 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3922 SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3924 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3925 // At least on OSX, buf contents are valid even when the call fails.
3926 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3928 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3929 // FIXME: under ASan the call below may write to freed memory and corrupt
3930 // its metadata. See
3931 // https://github.com/google/sanitizers/issues/321.
3932 int res = REAL(strerror_r)(errnum, buf, buflen);
3934 SIZE_T sz = internal_strnlen(buf, buflen);
3935 if (sz < buflen) ++sz;
3936 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3941 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3943 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3944 // FIXME: under ASan the call below may write to freed memory and corrupt
3945 // its metadata. See
3946 // https://github.com/google/sanitizers/issues/321.
3947 char *res = REAL(strerror_r)(errnum, buf, buflen);
3949 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3951 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3954 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3956 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3958 #define INIT_STRERROR_R
3961 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3962 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3964 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3965 // FIXME: under ASan the call below may write to freed memory and corrupt
3966 // its metadata. See
3967 // https://github.com/google/sanitizers/issues/321.
3968 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3969 // This version always returns a null-terminated string.
3971 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
3974 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3976 #define INIT_XPG_STRERROR_R
3979 #if SANITIZER_INTERCEPT_SCANDIR
3980 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3981 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3982 const struct __sanitizer_dirent **);
3984 static THREADLOCAL scandir_filter_f scandir_filter;
3985 static THREADLOCAL scandir_compar_f scandir_compar;
3987 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3988 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3989 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
3990 return scandir_filter(dir);
3993 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3994 const struct __sanitizer_dirent **b) {
3995 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3996 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3997 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
3998 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3999 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4000 return scandir_compar(a, b);
4003 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
4004 scandir_filter_f filter, scandir_compar_f compar) {
4006 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
4007 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4008 scandir_filter = filter;
4009 scandir_compar = compar;
4010 // FIXME: under ASan the call below may write to freed memory and corrupt
4011 // its metadata. See
4012 // https://github.com/google/sanitizers/issues/321.
4013 int res = REAL(scandir)(dirp, namelist,
4014 filter ? wrapped_scandir_filter : nullptr,
4015 compar ? wrapped_scandir_compar : nullptr);
4016 scandir_filter = nullptr;
4017 scandir_compar = nullptr;
4018 if (namelist && res > 0) {
4019 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4021 for (int i = 0; i < res; ++i)
4022 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4023 __sanitizer_dirsiz((*namelist)[i]));
4027 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
4029 #define INIT_SCANDIR
4032 #if SANITIZER_INTERCEPT_SCANDIR64
4033 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
4034 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
4035 const struct __sanitizer_dirent64 **);
4037 static THREADLOCAL scandir64_filter_f scandir64_filter;
4038 static THREADLOCAL scandir64_compar_f scandir64_compar;
4040 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
4041 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4042 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4043 return scandir64_filter(dir);
4046 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
4047 const struct __sanitizer_dirent64 **b) {
4048 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4049 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4050 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4051 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4052 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4053 return scandir64_compar(a, b);
4056 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
4057 scandir64_filter_f filter, scandir64_compar_f compar) {
4059 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
4060 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4061 scandir64_filter = filter;
4062 scandir64_compar = compar;
4063 // FIXME: under ASan the call below may write to freed memory and corrupt
4064 // its metadata. See
4065 // https://github.com/google/sanitizers/issues/321.
4067 REAL(scandir64)(dirp, namelist,
4068 filter ? wrapped_scandir64_filter : nullptr,
4069 compar ? wrapped_scandir64_compar : nullptr);
4070 scandir64_filter = nullptr;
4071 scandir64_compar = nullptr;
4072 if (namelist && res > 0) {
4073 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4075 for (int i = 0; i < res; ++i)
4076 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4077 __sanitizer_dirsiz((*namelist)[i]));
4081 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
4083 #define INIT_SCANDIR64
4086 #if SANITIZER_INTERCEPT_GETGROUPS
4087 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
4089 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
4090 // FIXME: under ASan the call below may write to freed memory and corrupt
4091 // its metadata. See
4092 // https://github.com/google/sanitizers/issues/321.
4093 int res = REAL(getgroups)(size, lst);
4094 if (res >= 0 && lst && size > 0)
4095 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
4098 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
4100 #define INIT_GETGROUPS
4103 #if SANITIZER_INTERCEPT_POLL
4104 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
4105 __sanitizer_nfds_t nfds) {
4106 for (unsigned i = 0; i < nfds; ++i) {
4107 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
4108 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
4112 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
4113 __sanitizer_nfds_t nfds) {
4114 for (unsigned i = 0; i < nfds; ++i)
4115 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
4116 sizeof(fds[i].revents));
4119 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4122 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
4123 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4124 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
4125 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4128 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
4133 #if SANITIZER_INTERCEPT_PPOLL
4134 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4135 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
4137 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
4138 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4140 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
4141 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
4143 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
4144 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4147 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
4152 #if SANITIZER_INTERCEPT_WORDEXP
4153 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
4155 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
4156 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
4157 // FIXME: under ASan the call below may write to freed memory and corrupt
4158 // its metadata. See
4159 // https://github.com/google/sanitizers/issues/321.
4160 int res = REAL(wordexp)(s, p, flags);
4162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4164 ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc;
4165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
4166 sizeof(*p->we_wordv) * (we_wordc + 1));
4167 for (uptr i = 0; i < we_wordc; ++i) {
4168 char *w = p->we_wordv[i];
4169 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);
4174 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
4176 #define INIT_WORDEXP
4179 #if SANITIZER_INTERCEPT_SIGWAIT
4180 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
4182 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
4183 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4184 // FIXME: under ASan the call below may write to freed memory and corrupt
4185 // its metadata. See
4186 // https://github.com/google/sanitizers/issues/321.
4187 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwait)(set, sig);
4188 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
4191 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
4193 #define INIT_SIGWAIT
4196 #if SANITIZER_INTERCEPT_SIGWAITINFO
4197 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
4199 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
4200 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4201 // FIXME: under ASan the call below may write to freed memory and corrupt
4202 // its metadata. See
4203 // https://github.com/google/sanitizers/issues/321.
4204 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwaitinfo)(set, info);
4205 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4208 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
4210 #define INIT_SIGWAITINFO
4213 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
4214 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
4217 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
4218 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
4219 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4220 // FIXME: under ASan the call below may write to freed memory and corrupt
4221 // its metadata. See
4222 // https://github.com/google/sanitizers/issues/321.
4223 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigtimedwait)(set, info, timeout);
4224 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4227 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4229 #define INIT_SIGTIMEDWAIT
4232 #if SANITIZER_INTERCEPT_SIGSETOPS
4233 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4235 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4236 // FIXME: under ASan the call below may write to freed memory and corrupt
4237 // its metadata. See
4238 // https://github.com/google/sanitizers/issues/321.
4239 int res = REAL(sigemptyset)(set);
4240 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4244 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4246 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4247 // FIXME: under ASan the call below may write to freed memory and corrupt
4248 // its metadata. See
4249 // https://github.com/google/sanitizers/issues/321.
4250 int res = REAL(sigfillset)(set);
4251 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4254 #define INIT_SIGSETOPS \
4255 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4256 COMMON_INTERCEPT_FUNCTION(sigfillset);
4258 #define INIT_SIGSETOPS
4261 #if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
4262 INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
4263 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4265 COMMON_INTERCEPTOR_ENTER(ctx, sigandset, dst, src1, src2);
4267 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4269 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4270 int res = REAL(sigandset)(dst, src1, src2);
4272 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4276 INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
4277 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4279 COMMON_INTERCEPTOR_ENTER(ctx, sigorset, dst, src1, src2);
4281 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4283 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4284 int res = REAL(sigorset)(dst, src1, src2);
4286 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4289 #define INIT_SIGSET_LOGICOPS \
4290 COMMON_INTERCEPT_FUNCTION(sigandset); \
4291 COMMON_INTERCEPT_FUNCTION(sigorset);
4293 #define INIT_SIGSET_LOGICOPS
4296 #if SANITIZER_INTERCEPT_SIGPENDING
4297 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4299 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4300 // FIXME: under ASan the call below may write to freed memory and corrupt
4301 // its metadata. See
4302 // https://github.com/google/sanitizers/issues/321.
4303 int res = REAL(sigpending)(set);
4304 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4307 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4309 #define INIT_SIGPENDING
4312 #if SANITIZER_INTERCEPT_SIGPROCMASK
4313 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4314 __sanitizer_sigset_t *oldset) {
4316 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4317 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4318 // FIXME: under ASan the call below may write to freed memory and corrupt
4319 // its metadata. See
4320 // https://github.com/google/sanitizers/issues/321.
4321 int res = REAL(sigprocmask)(how, set, oldset);
4323 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4326 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4328 #define INIT_SIGPROCMASK
4331 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4332 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4333 __sanitizer_sigset_t *oldset) {
4335 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4336 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4337 // FIXME: under ASan the call below may write to freed memory and corrupt
4338 // its metadata. See
4339 // https://github.com/google/sanitizers/issues/321.
4340 int res = REAL(pthread_sigmask)(how, set, oldset);
4342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4345 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4347 #define INIT_PTHREAD_SIGMASK
4350 #if SANITIZER_INTERCEPT_BACKTRACE
4351 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4353 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4354 // 'buffer' might be freed memory, hence it is unsafe to directly call
4355 // REAL(backtrace)(buffer, size). Instead, we use our own known-good
4357 void **scratch = (void**)InternalAlloc(sizeof(void*) * size);
4358 int res = REAL(backtrace)(scratch, size);
4359 if (res && buffer) {
4360 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4361 internal_memcpy(buffer, scratch, res * sizeof(*buffer));
4363 InternalFree(scratch);
4367 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4369 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4371 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4372 // The COMMON_INTERCEPTOR_READ_RANGE above ensures that 'buffer' is
4373 // valid for reading.
4374 char **res = REAL(backtrace_symbols)(buffer, size);
4376 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4377 for (int i = 0; i < size; ++i)
4378 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], internal_strlen(res[i]) + 1);
4382 #define INIT_BACKTRACE \
4383 COMMON_INTERCEPT_FUNCTION(backtrace); \
4384 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4386 #define INIT_BACKTRACE
4389 #if SANITIZER_INTERCEPT__EXIT
4390 INTERCEPTOR(void, _exit, int status) {
4392 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4393 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4394 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4395 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4396 if (status == 0) status = status1;
4397 REAL(_exit)(status);
4399 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4404 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4405 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4406 ALIAS(WRAP(pthread_setcancelstate));
4408 #define INIT___LIBC_THR_SETCANCELSTATE \
4409 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4411 #define INIT___LIBC_THR_SETCANCELSTATE
4414 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4415 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4416 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4417 if (mnt->mnt_fsname)
4418 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4419 internal_strlen(mnt->mnt_fsname) + 1);
4421 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4422 internal_strlen(mnt->mnt_dir) + 1);
4424 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4425 internal_strlen(mnt->mnt_type) + 1);
4427 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4428 internal_strlen(mnt->mnt_opts) + 1);
4432 #if SANITIZER_INTERCEPT_GETMNTENT
4433 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4435 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4436 __sanitizer_mntent *res = REAL(getmntent)(fp);
4437 if (res) write_mntent(ctx, res);
4440 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4442 #define INIT_GETMNTENT
4445 #if SANITIZER_INTERCEPT_GETMNTENT_R
4446 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4447 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4449 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4450 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4451 if (res) write_mntent(ctx, res);
4454 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4456 #define INIT_GETMNTENT_R
4459 #if SANITIZER_INTERCEPT_STATFS
4460 INTERCEPTOR(int, statfs, char *path, void *buf) {
4462 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4463 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4464 // FIXME: under ASan the call below may write to freed memory and corrupt
4465 // its metadata. See
4466 // https://github.com/google/sanitizers/issues/321.
4467 int res = REAL(statfs)(path, buf);
4468 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4471 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4473 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4474 // FIXME: under ASan the call below may write to freed memory and corrupt
4475 // its metadata. See
4476 // https://github.com/google/sanitizers/issues/321.
4477 int res = REAL(fstatfs)(fd, buf);
4478 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4481 #define INIT_STATFS \
4482 COMMON_INTERCEPT_FUNCTION(statfs); \
4483 COMMON_INTERCEPT_FUNCTION(fstatfs);
4488 #if SANITIZER_INTERCEPT_STATFS64
4489 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4491 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4492 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4493 // FIXME: under ASan the call below may write to freed memory and corrupt
4494 // its metadata. See
4495 // https://github.com/google/sanitizers/issues/321.
4496 int res = REAL(statfs64)(path, buf);
4497 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4500 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4502 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4503 // FIXME: under ASan the call below may write to freed memory and corrupt
4504 // its metadata. See
4505 // https://github.com/google/sanitizers/issues/321.
4506 int res = REAL(fstatfs64)(fd, buf);
4507 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4510 #define INIT_STATFS64 \
4511 COMMON_INTERCEPT_FUNCTION(statfs64); \
4512 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4514 #define INIT_STATFS64
4517 #if SANITIZER_INTERCEPT_STATVFS
4518 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4520 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4521 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4522 // FIXME: under ASan the call below may write to freed memory and corrupt
4523 // its metadata. See
4524 // https://github.com/google/sanitizers/issues/321.
4525 int res = REAL(statvfs)(path, buf);
4526 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4529 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4531 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4532 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4533 // FIXME: under ASan the call below may write to freed memory and corrupt
4534 // its metadata. See
4535 // https://github.com/google/sanitizers/issues/321.
4536 int res = REAL(fstatvfs)(fd, buf);
4538 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4540 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4544 #define INIT_STATVFS \
4545 COMMON_INTERCEPT_FUNCTION(statvfs); \
4546 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4548 #define INIT_STATVFS
4551 #if SANITIZER_INTERCEPT_STATVFS64
4552 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4554 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4555 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4556 // FIXME: under ASan the call below may write to freed memory and corrupt
4557 // its metadata. See
4558 // https://github.com/google/sanitizers/issues/321.
4559 int res = REAL(statvfs64)(path, buf);
4560 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4563 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4565 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4566 // FIXME: under ASan the call below may write to freed memory and corrupt
4567 // its metadata. See
4568 // https://github.com/google/sanitizers/issues/321.
4569 int res = REAL(fstatvfs64)(fd, buf);
4570 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4573 #define INIT_STATVFS64 \
4574 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4575 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4577 #define INIT_STATVFS64
4580 #if SANITIZER_INTERCEPT_INITGROUPS
4581 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4583 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4584 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, internal_strlen(user) + 1);
4585 int res = REAL(initgroups)(user, group);
4588 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4590 #define INIT_INITGROUPS
4593 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4594 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4596 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4597 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4598 char *res = REAL(ether_ntoa)(addr);
4599 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4602 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4604 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4605 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4606 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4607 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4610 #define INIT_ETHER_NTOA_ATON \
4611 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4612 COMMON_INTERCEPT_FUNCTION(ether_aton);
4614 #define INIT_ETHER_NTOA_ATON
4617 #if SANITIZER_INTERCEPT_ETHER_HOST
4618 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4620 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4621 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4622 // FIXME: under ASan the call below may write to freed memory and corrupt
4623 // its metadata. See
4624 // https://github.com/google/sanitizers/issues/321.
4625 int res = REAL(ether_ntohost)(hostname, addr);
4626 if (!res && hostname)
4627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4630 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4632 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4634 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4635 // FIXME: under ASan the call below may write to freed memory and corrupt
4636 // its metadata. See
4637 // https://github.com/google/sanitizers/issues/321.
4638 int res = REAL(ether_hostton)(hostname, addr);
4639 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4642 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4645 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4646 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, internal_strlen(line) + 1);
4647 // FIXME: under ASan the call below may write to freed memory and corrupt
4648 // its metadata. See
4649 // https://github.com/google/sanitizers/issues/321.
4650 int res = REAL(ether_line)(line, addr, hostname);
4652 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4654 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4658 #define INIT_ETHER_HOST \
4659 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4660 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4661 COMMON_INTERCEPT_FUNCTION(ether_line);
4663 #define INIT_ETHER_HOST
4666 #if SANITIZER_INTERCEPT_ETHER_R
4667 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4669 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4670 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4671 // FIXME: under ASan the call below may write to freed memory and corrupt
4672 // its metadata. See
4673 // https://github.com/google/sanitizers/issues/321.
4674 char *res = REAL(ether_ntoa_r)(addr, buf);
4675 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4678 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4679 __sanitizer_ether_addr *addr) {
4681 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4682 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4683 // FIXME: under ASan the call below may write to freed memory and corrupt
4684 // its metadata. See
4685 // https://github.com/google/sanitizers/issues/321.
4686 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4687 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4690 #define INIT_ETHER_R \
4691 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4692 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4694 #define INIT_ETHER_R
4697 #if SANITIZER_INTERCEPT_SHMCTL
4698 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4700 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4701 // FIXME: under ASan the call below may write to freed memory and corrupt
4702 // its metadata. See
4703 // https://github.com/google/sanitizers/issues/321.
4704 int res = REAL(shmctl)(shmid, cmd, buf);
4707 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4708 sz = sizeof(__sanitizer_shmid_ds);
4709 else if (cmd == shmctl_ipc_info)
4710 sz = struct_shminfo_sz;
4711 else if (cmd == shmctl_shm_info)
4712 sz = struct_shm_info_sz;
4713 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4717 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4722 #if SANITIZER_INTERCEPT_RANDOM_R
4723 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4725 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4726 // FIXME: under ASan the call below may write to freed memory and corrupt
4727 // its metadata. See
4728 // https://github.com/google/sanitizers/issues/321.
4729 int res = REAL(random_r)(buf, result);
4731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4734 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4736 #define INIT_RANDOM_R
4739 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4740 // its metadata. See
4741 // https://github.com/google/sanitizers/issues/321.
4742 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4743 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4744 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4745 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4746 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4747 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4748 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4749 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4750 INTERCEPTOR(int, fn, void *attr, void *r) { \
4752 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4753 int res = REAL(fn)(attr, r); \
4754 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4757 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4758 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4759 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4760 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4761 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4762 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4763 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4764 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4765 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4766 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4769 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4770 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4771 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4772 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4773 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4774 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4776 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4777 // FIXME: under ASan the call below may write to freed memory and corrupt
4778 // its metadata. See
4779 // https://github.com/google/sanitizers/issues/321.
4780 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4782 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4783 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4788 // We may need to call the real pthread_attr_getstack from the run-time
4789 // in sanitizer_common, but we don't want to include the interception headers
4790 // there. So, just define this function here.
4791 namespace __sanitizer {
4793 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4794 return REAL(pthread_attr_getstack)(attr, addr, size);
4797 } // namespace __sanitizer
4799 #define INIT_PTHREAD_ATTR_GET \
4800 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4801 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4802 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4803 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4804 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4806 #define INIT_PTHREAD_ATTR_GET
4809 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4810 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4811 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4813 #define INIT_PTHREAD_ATTR_GET_SCHED \
4814 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4815 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4817 #define INIT_PTHREAD_ATTR_GET_SCHED
4820 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4821 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4823 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4824 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4826 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4829 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4830 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4833 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4835 // FIXME: under ASan the call below may write to freed memory and corrupt
4836 // its metadata. See
4837 // https://github.com/google/sanitizers/issues/321.
4838 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4839 if (!res && cpusetsize && cpuset)
4840 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4844 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4845 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4847 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4850 #if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
4851 INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
4854 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
4856 // FIXME: under ASan the call below may write to freed memory and corrupt
4857 // its metadata. See
4858 // https://github.com/google/sanitizers/issues/321.
4859 int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
4860 if (!res && cpusetsize && cpuset)
4861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4865 #define INIT_PTHREAD_GETAFFINITY_NP \
4866 COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
4868 #define INIT_PTHREAD_GETAFFINITY_NP
4871 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4872 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4873 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4874 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4876 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4879 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4880 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4881 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4882 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4884 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4887 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4888 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4889 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4890 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4892 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4895 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4896 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4897 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4898 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4900 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4903 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4904 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4905 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4906 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4908 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4911 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4912 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4913 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4914 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4916 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4919 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4920 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4921 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4922 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4924 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4927 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4928 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4929 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4930 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4932 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4935 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4936 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4937 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4938 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4940 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4943 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4944 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4945 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4946 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4948 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4951 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4952 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4953 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4954 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4956 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4959 #if SANITIZER_INTERCEPT_TMPNAM
4960 INTERCEPTOR(char *, tmpnam, char *s) {
4962 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4963 char *res = REAL(tmpnam)(s);
4966 // FIXME: under ASan the call below may write to freed memory and corrupt
4967 // its metadata. See
4968 // https://github.com/google/sanitizers/issues/321.
4969 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
4971 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4975 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4980 #if SANITIZER_INTERCEPT_TMPNAM_R
4981 INTERCEPTOR(char *, tmpnam_r, char *s) {
4983 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4984 // FIXME: under ASan the call below may write to freed memory and corrupt
4985 // its metadata. See
4986 // https://github.com/google/sanitizers/issues/321.
4987 char *res = REAL(tmpnam_r)(s);
4988 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
4991 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4993 #define INIT_TMPNAM_R
4996 #if SANITIZER_INTERCEPT_PTSNAME
4997 INTERCEPTOR(char *, ptsname, int fd) {
4999 COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
5000 char *res = REAL(ptsname)(fd);
5002 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5005 #define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
5007 #define INIT_PTSNAME
5010 #if SANITIZER_INTERCEPT_PTSNAME_R
5011 INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
5013 COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
5014 int res = REAL(ptsname_r)(fd, name, namesize);
5016 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5019 #define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
5021 #define INIT_PTSNAME_R
5024 #if SANITIZER_INTERCEPT_TTYNAME
5025 INTERCEPTOR(char *, ttyname, int fd) {
5027 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
5028 char *res = REAL(ttyname)(fd);
5030 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5033 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
5035 #define INIT_TTYNAME
5038 #if SANITIZER_INTERCEPT_TTYNAME_R
5039 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
5041 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
5042 int res = REAL(ttyname_r)(fd, name, namesize);
5044 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5047 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
5049 #define INIT_TTYNAME_R
5052 #if SANITIZER_INTERCEPT_TEMPNAM
5053 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
5055 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
5056 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, internal_strlen(dir) + 1);
5057 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, internal_strlen(pfx) + 1);
5058 char *res = REAL(tempnam)(dir, pfx);
5059 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5062 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
5064 #define INIT_TEMPNAM
5067 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
5068 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
5070 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
5071 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5072 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
5073 return REAL(pthread_setname_np)(thread, name);
5075 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5076 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
5077 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
5079 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
5080 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
5081 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5082 internal_snprintf(newname, sizeof(newname), name, arg);
5083 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
5084 return REAL(pthread_setname_np)(thread, name, arg);
5086 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5088 #define INIT_PTHREAD_SETNAME_NP
5091 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
5092 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
5094 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
5095 int res = REAL(pthread_getname_np)(thread, name, len);
5097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
5100 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
5102 #define INIT_PTHREAD_GETNAME_NP
5105 #if SANITIZER_INTERCEPT_SINCOS
5106 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
5108 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
5109 // FIXME: under ASan the call below may write to freed memory and corrupt
5110 // its metadata. See
5111 // https://github.com/google/sanitizers/issues/321.
5112 REAL(sincos)(x, sin, cos);
5113 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5114 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5116 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
5118 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
5119 // FIXME: under ASan the call below may write to freed memory and corrupt
5120 // its metadata. See
5121 // https://github.com/google/sanitizers/issues/321.
5122 REAL(sincosf)(x, sin, cos);
5123 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5124 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5126 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
5128 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
5129 // FIXME: under ASan the call below may write to freed memory and corrupt
5130 // its metadata. See
5131 // https://github.com/google/sanitizers/issues/321.
5132 REAL(sincosl)(x, sin, cos);
5133 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5134 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5136 #define INIT_SINCOS \
5137 COMMON_INTERCEPT_FUNCTION(sincos); \
5138 COMMON_INTERCEPT_FUNCTION(sincosf); \
5139 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
5144 #if SANITIZER_INTERCEPT_REMQUO
5145 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
5147 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
5148 // FIXME: under ASan the call below may write to freed memory and corrupt
5149 // its metadata. See
5150 // https://github.com/google/sanitizers/issues/321.
5151 double res = REAL(remquo)(x, y, quo);
5152 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5155 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
5157 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
5158 // FIXME: under ASan the call below may write to freed memory and corrupt
5159 // its metadata. See
5160 // https://github.com/google/sanitizers/issues/321.
5161 float res = REAL(remquof)(x, y, quo);
5162 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5165 #define INIT_REMQUO \
5166 COMMON_INTERCEPT_FUNCTION(remquo); \
5167 COMMON_INTERCEPT_FUNCTION(remquof);
5172 #if SANITIZER_INTERCEPT_REMQUOL
5173 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
5175 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
5176 // FIXME: under ASan the call below may write to freed memory and corrupt
5177 // its metadata. See
5178 // https://github.com/google/sanitizers/issues/321.
5179 long double res = REAL(remquol)(x, y, quo);
5180 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5183 #define INIT_REMQUOL \
5184 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
5186 #define INIT_REMQUOL
5189 #if SANITIZER_INTERCEPT_LGAMMA
5191 INTERCEPTOR(double, lgamma, double x) {
5193 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
5194 double res = REAL(lgamma)(x);
5195 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5198 INTERCEPTOR(float, lgammaf, float x) {
5200 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
5201 float res = REAL(lgammaf)(x);
5202 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5205 #define INIT_LGAMMA \
5206 COMMON_INTERCEPT_FUNCTION(lgamma); \
5207 COMMON_INTERCEPT_FUNCTION(lgammaf);
5212 #if SANITIZER_INTERCEPT_LGAMMAL
5213 INTERCEPTOR(long double, lgammal, long double x) {
5215 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
5216 long double res = REAL(lgammal)(x);
5217 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5220 #define INIT_LGAMMAL \
5221 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
5223 #define INIT_LGAMMAL
5226 #if SANITIZER_INTERCEPT_LGAMMA_R
5227 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
5229 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
5230 // FIXME: under ASan the call below may write to freed memory and corrupt
5231 // its metadata. See
5232 // https://github.com/google/sanitizers/issues/321.
5233 double res = REAL(lgamma_r)(x, signp);
5234 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5237 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
5239 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
5240 // FIXME: under ASan the call below may write to freed memory and corrupt
5241 // its metadata. See
5242 // https://github.com/google/sanitizers/issues/321.
5243 float res = REAL(lgammaf_r)(x, signp);
5244 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5247 #define INIT_LGAMMA_R \
5248 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
5249 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
5251 #define INIT_LGAMMA_R
5254 #if SANITIZER_INTERCEPT_LGAMMAL_R
5255 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5257 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
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 long double res = REAL(lgammal_r)(x, signp);
5262 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5265 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5267 #define INIT_LGAMMAL_R
5270 #if SANITIZER_INTERCEPT_DRAND48_R
5271 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5273 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5274 // FIXME: under ASan the call below may write to freed memory and corrupt
5275 // its metadata. See
5276 // https://github.com/google/sanitizers/issues/321.
5277 int res = REAL(drand48_r)(buffer, result);
5278 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5281 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5283 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5284 // FIXME: under ASan the call below may write to freed memory and corrupt
5285 // its metadata. See
5286 // https://github.com/google/sanitizers/issues/321.
5287 int res = REAL(lrand48_r)(buffer, result);
5288 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5291 #define INIT_DRAND48_R \
5292 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5293 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5295 #define INIT_DRAND48_R
5298 #if SANITIZER_INTERCEPT_RAND_R
5299 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5301 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5302 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5303 return REAL(rand_r)(seedp);
5305 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5310 #if SANITIZER_INTERCEPT_GETLINE
5311 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5313 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5314 // FIXME: under ASan the call below may write to freed memory and corrupt
5315 // its metadata. See
5316 // https://github.com/google/sanitizers/issues/321.
5317 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5320 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5321 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5326 // FIXME: under ASan the call below may write to freed memory and corrupt its
5328 // https://github.com/google/sanitizers/issues/321.
5329 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5332 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5333 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5342 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5344 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5346 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5347 // with its own body.
5348 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5350 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5352 #define INIT_GETLINE \
5353 COMMON_INTERCEPT_FUNCTION(getline); \
5354 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5355 COMMON_INTERCEPT_FUNCTION(getdelim);
5357 #define INIT_GETLINE
5360 #if SANITIZER_INTERCEPT_ICONV
5361 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5362 char **outbuf, SIZE_T *outbytesleft) {
5364 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5367 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5368 if (inbuf && inbytesleft)
5369 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5371 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5372 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5373 // FIXME: under ASan the call below may write to freed memory and corrupt
5374 // its metadata. See
5375 // https://github.com/google/sanitizers/issues/321.
5376 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5377 if (outbuf && *outbuf > outbuf_orig) {
5378 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5379 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5383 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5388 #if SANITIZER_INTERCEPT_TIMES
5389 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5391 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5392 // FIXME: under ASan the call below may write to freed memory and corrupt
5393 // its metadata. See
5394 // https://github.com/google/sanitizers/issues/321.
5395 __sanitizer_clock_t res = REAL(times)(tms);
5396 if (res != (__sanitizer_clock_t)-1 && tms)
5397 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5400 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5405 #if SANITIZER_S390 && \
5406 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5407 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5408 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5411 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5413 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5414 // If you see any crashes around this functions, there are 2 known issues with
5415 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5416 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5417 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5418 // to access thread local variables (it should not happen normally,
5419 // because sanitizers use initial-exec tls model).
5420 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5422 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5423 void *res = REAL(__tls_get_addr)(arg);
5424 uptr tls_begin, tls_end;
5425 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5426 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5428 // New DTLS block has been allocated.
5429 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5434 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5435 // mostly the same semantics as __tls_get_addr, but its presence enables
5436 // some optimizations in linker (which are safe to ignore here).
5437 INTERCEPTOR(void *, __tls_get_addr_opt, void *arg) ALIAS(WRAP(__tls_get_addr));
5439 #else // SANITIZER_S390
5440 // On s390, we have to intercept two functions here:
5441 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5442 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5443 // a proper pointer. It is used by dlsym for TLS symbols.
5444 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5445 // descriptor offset as an argument instead of a pointer. GOT address
5446 // is passed in r12, so it's necessary to write it in assembly. This is
5447 // the function used by the compiler.
5448 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5449 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5451 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5452 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5453 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5454 void *ptr = reinterpret_cast<void *>(res + tp);
5455 uptr tls_begin, tls_end;
5456 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5457 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5459 // New DTLS block has been allocated.
5460 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5464 #endif // SANITIZER_S390
5466 #define INIT_TLS_GET_ADDR
5469 #if SANITIZER_S390 && \
5470 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5471 // We need a hidden symbol aliasing the above, so that we can jump
5472 // directly to it from the assembly below.
5473 extern "C" __attribute__((visibility("hidden"))) uptr __tls_get_addr_hidden(
5474 void *arg) ALIAS(WRAP(__tls_get_addr_internal));
5475 extern "C" uptr __tls_get_offset(void *arg);
5476 extern "C" uptr TRAMPOLINE(__tls_get_offset)(void *arg);
5477 extern "C" uptr WRAP(__tls_get_offset)(void *arg);
5478 // Now carefully intercept __tls_get_offset.
5481 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5482 // exports our symbol.
5483 ".weak __tls_get_offset\n"
5484 ".set __tls_get_offset, __interceptor___tls_get_offset\n"
5485 ".global __interceptor___tls_get_offset\n"
5486 ".type __interceptor___tls_get_offset, @function\n"
5487 "__interceptor___tls_get_offset:\n"
5489 "la %r2, 0(%r2,%r12)\n"
5490 "jg __tls_get_addr_hidden\n"
5493 "0: la %r2,0(%r2,%r12)\n"
5494 "l %r4,1f-0b(%r3)\n"
5496 "1: .long __tls_get_addr_hidden - 0b\n"
5498 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5499 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5500 ".type __tls_get_offset_wrapper, @function\n"
5501 "__tls_get_offset_wrapper:\n"
5508 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5512 #if SANITIZER_INTERCEPT_LISTXATTR
5513 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5515 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5516 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5517 // FIXME: under ASan the call below may write to freed memory and corrupt
5518 // its metadata. See
5519 // https://github.com/google/sanitizers/issues/321.
5520 SSIZE_T res = REAL(listxattr)(path, list, size);
5521 // Here and below, size == 0 is a special case where nothing is written to the
5522 // buffer, and res contains the desired buffer size.
5523 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5526 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5528 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5529 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5530 // FIXME: under ASan the call below may write to freed memory and corrupt
5531 // its metadata. See
5532 // https://github.com/google/sanitizers/issues/321.
5533 SSIZE_T res = REAL(llistxattr)(path, list, size);
5534 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5537 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5539 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5540 // FIXME: under ASan the call below may write to freed memory and corrupt
5541 // its metadata. See
5542 // https://github.com/google/sanitizers/issues/321.
5543 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5544 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5547 #define INIT_LISTXATTR \
5548 COMMON_INTERCEPT_FUNCTION(listxattr); \
5549 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5550 COMMON_INTERCEPT_FUNCTION(flistxattr);
5552 #define INIT_LISTXATTR
5555 #if SANITIZER_INTERCEPT_GETXATTR
5556 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5559 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5560 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5561 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5562 // FIXME: under ASan the call below may write to freed memory and corrupt
5563 // its metadata. See
5564 // https://github.com/google/sanitizers/issues/321.
5565 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5566 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5569 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5572 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5573 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5574 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5575 // FIXME: under ASan the call below may write to freed memory and corrupt
5576 // its metadata. See
5577 // https://github.com/google/sanitizers/issues/321.
5578 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5579 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5582 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5585 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5586 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5587 // FIXME: under ASan the call below may write to freed memory and corrupt
5588 // its metadata. See
5589 // https://github.com/google/sanitizers/issues/321.
5590 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5591 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5594 #define INIT_GETXATTR \
5595 COMMON_INTERCEPT_FUNCTION(getxattr); \
5596 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5597 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5599 #define INIT_GETXATTR
5602 #if SANITIZER_INTERCEPT_GETRESID
5603 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5605 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5606 // FIXME: under ASan the call below may write to freed memory and corrupt
5607 // its metadata. See
5608 // https://github.com/google/sanitizers/issues/321.
5609 int res = REAL(getresuid)(ruid, euid, suid);
5611 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5612 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5613 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5617 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5619 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5620 // FIXME: under ASan the call below may write to freed memory and corrupt
5621 // its metadata. See
5622 // https://github.com/google/sanitizers/issues/321.
5623 int res = REAL(getresgid)(rgid, egid, sgid);
5625 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5626 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5627 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5631 #define INIT_GETRESID \
5632 COMMON_INTERCEPT_FUNCTION(getresuid); \
5633 COMMON_INTERCEPT_FUNCTION(getresgid);
5635 #define INIT_GETRESID
5638 #if SANITIZER_INTERCEPT_GETIFADDRS
5639 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5640 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5641 // intercept it to poison the memory again.
5642 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5644 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5645 // FIXME: under ASan the call below may write to freed memory and corrupt
5646 // its metadata. See
5647 // https://github.com/google/sanitizers/issues/321.
5648 int res = REAL(getifaddrs)(ifap);
5649 if (res == 0 && ifap) {
5650 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5651 __sanitizer_ifaddrs *p = *ifap;
5653 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5655 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5656 internal_strlen(p->ifa_name) + 1);
5658 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5660 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5661 // On Linux this is a union, but the other member also points to a
5662 // struct sockaddr, so the following is sufficient.
5664 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5665 // FIXME(smatveev): Unpoison p->ifa_data as well.
5671 #define INIT_GETIFADDRS \
5672 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5674 #define INIT_GETIFADDRS
5677 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5678 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5680 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5681 // FIXME: under ASan the call below may write to freed memory and corrupt
5682 // its metadata. See
5683 // https://github.com/google/sanitizers/issues/321.
5684 char *res = REAL(if_indextoname)(ifindex, ifname);
5686 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5689 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5691 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5693 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5694 return REAL(if_nametoindex)(ifname);
5696 #define INIT_IF_INDEXTONAME \
5697 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5698 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5700 #define INIT_IF_INDEXTONAME
5703 #if SANITIZER_INTERCEPT_CAPGET
5704 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5706 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5708 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5709 // FIXME: under ASan the call below may write to freed memory and corrupt
5710 // its metadata. See
5711 // https://github.com/google/sanitizers/issues/321.
5712 int res = REAL(capget)(hdrp, datap);
5713 if (res == 0 && datap) {
5714 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, datasz);
5717 // We can also return -1 and write to hdrp->version if the version passed in
5718 // hdrp->version is unsupported. But that's not a trivial condition to check,
5719 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5722 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5724 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5726 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5728 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5729 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, datasz);
5731 return REAL(capset)(hdrp, datap);
5733 #define INIT_CAPGET \
5734 COMMON_INTERCEPT_FUNCTION(capget); \
5735 COMMON_INTERCEPT_FUNCTION(capset);
5740 #if SANITIZER_INTERCEPT_FTIME
5741 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5743 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5744 // FIXME: under ASan the call below may write to freed memory and corrupt
5745 // its metadata. See
5746 // https://github.com/google/sanitizers/issues/321.
5747 int res = REAL(ftime)(tp);
5749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5752 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5755 #endif // SANITIZER_INTERCEPT_FTIME
5757 #if SANITIZER_INTERCEPT_XDR
5758 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5759 unsigned size, int op) {
5761 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5762 // FIXME: under ASan the call below may write to freed memory and corrupt
5763 // its metadata. See
5764 // https://github.com/google/sanitizers/issues/321.
5765 REAL(xdrmem_create)(xdrs, addr, size, op);
5766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5767 if (op == __sanitizer_XDR_ENCODE) {
5768 // It's not obvious how much data individual xdr_ routines write.
5769 // Simply unpoison the entire target buffer in advance.
5770 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5774 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5776 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5777 // FIXME: under ASan the call below may write to freed memory and corrupt
5778 // its metadata. See
5779 // https://github.com/google/sanitizers/issues/321.
5780 REAL(xdrstdio_create)(xdrs, file, op);
5781 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
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 #define XDR_INTERCEPTOR(F, T) \
5788 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5790 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5791 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5792 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5793 int res = REAL(F)(xdrs, p); \
5794 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5799 XDR_INTERCEPTOR(xdr_short, short)
5800 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5801 XDR_INTERCEPTOR(xdr_int, int)
5802 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5803 XDR_INTERCEPTOR(xdr_long, long)
5804 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5805 XDR_INTERCEPTOR(xdr_hyper, long long)
5806 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5807 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5808 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5809 XDR_INTERCEPTOR(xdr_int8_t, u8)
5810 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5811 XDR_INTERCEPTOR(xdr_int16_t, u16)
5812 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5813 XDR_INTERCEPTOR(xdr_int32_t, u32)
5814 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5815 XDR_INTERCEPTOR(xdr_int64_t, u64)
5816 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5817 XDR_INTERCEPTOR(xdr_quad_t, long long)
5818 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5819 XDR_INTERCEPTOR(xdr_bool, bool)
5820 XDR_INTERCEPTOR(xdr_enum, int)
5821 XDR_INTERCEPTOR(xdr_char, char)
5822 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5823 XDR_INTERCEPTOR(xdr_float, float)
5824 XDR_INTERCEPTOR(xdr_double, double)
5826 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5827 // wrapstring, sizeof
5829 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5832 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5833 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5834 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5835 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5836 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5838 // FIXME: under ASan the call below may write to freed memory and corrupt
5839 // its metadata. See
5840 // https://github.com/google/sanitizers/issues/321.
5841 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5842 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5845 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5850 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5853 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5854 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5855 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5856 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, internal_strlen(*p) + 1);
5858 // FIXME: under ASan the call below may write to freed memory and corrupt
5859 // its metadata. See
5860 // https://github.com/google/sanitizers/issues/321.
5861 int res = REAL(xdr_string)(xdrs, p, maxsize);
5862 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5863 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
5871 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5872 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5873 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5874 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5875 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5876 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5877 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5878 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5879 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5880 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5881 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5882 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5883 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5884 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5885 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5886 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5887 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5888 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5889 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5890 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5891 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5892 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5893 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5894 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5895 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5896 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5897 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5898 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5899 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5900 COMMON_INTERCEPT_FUNCTION(xdr_string);
5903 #endif // SANITIZER_INTERCEPT_XDR
5905 #if SANITIZER_INTERCEPT_XDRREC
5906 typedef int (*xdrrec_cb)(char*, char*, int);
5907 struct XdrRecWrapper {
5911 typedef AddrHashMap<XdrRecWrapper *, 11> XdrRecWrapMap;
5912 static XdrRecWrapMap *xdrrec_wrap_map;
5914 static int xdrrec_wr_wrap(char *handle, char *buf, int count) {
5915 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5916 COMMON_INTERCEPTOR_INITIALIZE_RANGE(buf, count);
5917 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
5918 return wrap->wr(wrap->handle, buf, count);
5921 static int xdrrec_rd_wrap(char *handle, char *buf, int count) {
5922 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5923 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
5924 return wrap->rd(wrap->handle, buf, count);
5927 // This doesn't apply to the solaris version as it has a different function
5929 INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
5930 unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
5931 int (*wr)(char*, char*, int)) {
5933 COMMON_INTERCEPTOR_ENTER(ctx, xdrrec_create, xdr, sndsize, rcvsize,
5935 COMMON_INTERCEPTOR_READ_RANGE(ctx, &xdr->x_op, sizeof xdr->x_op);
5937 // We can't allocate a wrapper on the stack, as the handle is used outside
5938 // this stack frame. So we put it on the heap, and keep track of it with
5939 // the HashMap (keyed by x_private). When we later need to xdr_destroy,
5940 // we can index the map, free the wrapper, and then clean the map entry.
5941 XdrRecWrapper *wrap_data =
5942 (XdrRecWrapper *)InternalAlloc(sizeof(XdrRecWrapper));
5943 wrap_data->handle = handle;
5947 wr = xdrrec_wr_wrap;
5949 rd = xdrrec_rd_wrap;
5950 handle = (char *)wrap_data;
5952 REAL(xdrrec_create)(xdr, sndsize, rcvsize, handle, rd, wr);
5953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdr, sizeof *xdr);
5955 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, false, true);
5959 // We have to intercept this to be able to free wrapper memory;
5960 // otherwise it's not necessary.
5961 INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {
5963 COMMON_INTERCEPTOR_ENTER(ctx, xdr_destroy, xdr);
5965 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, true);
5966 InternalFree(*wrap);
5967 REAL(xdr_destroy)(xdr);
5969 #define INIT_XDRREC_LINUX \
5970 static u64 xdrrec_wrap_mem[sizeof(XdrRecWrapMap) / sizeof(u64) + 1]; \
5971 xdrrec_wrap_map = new ((void *)&xdrrec_wrap_mem) XdrRecWrapMap(); \
5972 COMMON_INTERCEPT_FUNCTION(xdrrec_create); \
5973 COMMON_INTERCEPT_FUNCTION(xdr_destroy);
5975 #define INIT_XDRREC_LINUX
5978 #if SANITIZER_INTERCEPT_TSEARCH
5979 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5980 int (*compar)(const void *, const void *)) {
5982 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5983 // FIXME: under ASan the call below may write to freed memory and corrupt
5984 // its metadata. See
5985 // https://github.com/google/sanitizers/issues/321.
5986 void *res = REAL(tsearch)(key, rootp, compar);
5987 if (res && *(void **)res == key)
5988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5991 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5993 #define INIT_TSEARCH
5996 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5997 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5998 void unpoison_file(__sanitizer_FILE *fp) {
5999 #if SANITIZER_HAS_STRUCT_FILE
6000 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
6001 #if SANITIZER_NETBSD
6002 if (fp->_bf._base && fp->_bf._size > 0)
6003 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
6006 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
6007 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
6008 fp->_IO_read_end - fp->_IO_read_base);
6009 if (fp->_IO_write_base && fp->_IO_write_base < fp->_IO_write_end)
6010 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_write_base,
6011 fp->_IO_write_end - fp->_IO_write_base);
6013 #endif // SANITIZER_HAS_STRUCT_FILE
6017 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
6018 // These guys are called when a .c source is built with -O2.
6019 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
6021 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
6022 int res = REAL(__uflow)(fp);
6026 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
6028 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
6029 int res = REAL(__underflow)(fp);
6033 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
6035 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
6036 int res = REAL(__overflow)(fp, ch);
6040 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
6042 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
6043 int res = REAL(__wuflow)(fp);
6047 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
6049 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
6050 int res = REAL(__wunderflow)(fp);
6054 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
6056 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
6057 int res = REAL(__woverflow)(fp, ch);
6061 #define INIT_LIBIO_INTERNALS \
6062 COMMON_INTERCEPT_FUNCTION(__uflow); \
6063 COMMON_INTERCEPT_FUNCTION(__underflow); \
6064 COMMON_INTERCEPT_FUNCTION(__overflow); \
6065 COMMON_INTERCEPT_FUNCTION(__wuflow); \
6066 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
6067 COMMON_INTERCEPT_FUNCTION(__woverflow);
6069 #define INIT_LIBIO_INTERNALS
6072 #if SANITIZER_INTERCEPT_FOPEN
6073 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
6075 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
6076 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6077 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6078 __sanitizer_FILE *res = REAL(fopen)(path, mode);
6079 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6080 if (res) unpoison_file(res);
6083 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
6085 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
6086 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6087 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
6088 if (res) unpoison_file(res);
6091 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
6092 __sanitizer_FILE *fp) {
6094 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
6095 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6096 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6097 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6098 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
6099 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6100 if (res) unpoison_file(res);
6103 #define INIT_FOPEN \
6104 COMMON_INTERCEPT_FUNCTION(fopen); \
6105 COMMON_INTERCEPT_FUNCTION(fdopen); \
6106 COMMON_INTERCEPT_FUNCTION(freopen);
6111 #if SANITIZER_INTERCEPT_FLOPEN
6112 INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
6115 va_start(ap, flags);
6116 u16 mode = static_cast<u16>(va_arg(ap, u32));
6118 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6120 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6122 return REAL(flopen)(path, flags, mode);
6125 INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
6128 va_start(ap, flags);
6129 u16 mode = static_cast<u16>(va_arg(ap, u32));
6131 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6133 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6135 return REAL(flopenat)(dirfd, path, flags, mode);
6138 #define INIT_FLOPEN \
6139 COMMON_INTERCEPT_FUNCTION(flopen); \
6140 COMMON_INTERCEPT_FUNCTION(flopenat);
6145 #if SANITIZER_INTERCEPT_FOPEN64
6146 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
6148 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
6149 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6150 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6151 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
6152 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6153 if (res) unpoison_file(res);
6156 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
6157 __sanitizer_FILE *fp) {
6159 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
6160 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6161 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6162 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6163 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
6164 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6165 if (res) unpoison_file(res);
6168 #define INIT_FOPEN64 \
6169 COMMON_INTERCEPT_FUNCTION(fopen64); \
6170 COMMON_INTERCEPT_FUNCTION(freopen64);
6172 #define INIT_FOPEN64
6175 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6176 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
6178 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
6179 // FIXME: under ASan the call below may write to freed memory and corrupt
6180 // its metadata. See
6181 // https://github.com/google/sanitizers/issues/321.
6182 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
6184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6185 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6187 FileMetadata file = {ptr, sizeloc};
6188 SetInterceptorMetadata(res, file);
6192 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
6195 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
6196 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
6198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6199 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6201 FileMetadata file = {(char **)ptr, sizeloc};
6202 SetInterceptorMetadata(res, file);
6206 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
6209 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
6210 // FIXME: under ASan the call below may write to freed memory and corrupt
6211 // its metadata. See
6212 // https://github.com/google/sanitizers/issues/321.
6213 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
6214 if (res) unpoison_file(res);
6217 #define INIT_OPEN_MEMSTREAM \
6218 COMMON_INTERCEPT_FUNCTION(open_memstream); \
6219 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
6220 COMMON_INTERCEPT_FUNCTION(fmemopen);
6222 #define INIT_OPEN_MEMSTREAM
6225 #if SANITIZER_INTERCEPT_OBSTACK
6226 static void initialize_obstack(__sanitizer_obstack *obstack) {
6227 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
6229 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
6230 sizeof(*obstack->chunk));
6233 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
6234 int align, void *(*alloc_fn)(uptr arg, uptr sz),
6235 void (*free_fn)(uptr arg, void *p)) {
6237 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
6239 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
6240 if (res) initialize_obstack(obstack);
6243 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
6244 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
6246 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
6248 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
6249 if (res) initialize_obstack(obstack);
6252 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
6254 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
6255 REAL(_obstack_newchunk)(obstack, length);
6257 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
6258 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
6260 #define INIT_OBSTACK \
6261 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
6262 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
6263 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
6265 #define INIT_OBSTACK
6268 #if SANITIZER_INTERCEPT_FFLUSH
6269 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
6271 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
6274 int res = REAL(fflush)(fp);
6275 // FIXME: handle fp == NULL
6277 const FileMetadata *m = GetInterceptorMetadata(fp);
6278 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6282 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6287 #if SANITIZER_INTERCEPT_FCLOSE
6288 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6290 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6291 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6292 const FileMetadata *m = GetInterceptorMetadata(fp);
6295 int res = REAL(fclose)(fp);
6297 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6298 DeleteInterceptorMetadata(fp);
6302 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6307 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6308 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6310 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6313 COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6315 # if !SANITIZER_DYNAMIC
6316 // We care about a very specific use-case: dladdr on
6317 // statically-linked ASan may return <main program>
6318 // instead of the library.
6319 // We therefore only take effect if the sanitizer is statically
6320 // linked, and we don't bother canonicalizing paths because
6321 // dladdr should return the same address both times (we assume
6322 // the user did not canonicalize the result from dladdr).
6323 if (common_flags()->test_only_replace_dlopen_main_program) {
6324 VPrintf(1, "dlopen interceptor: filename: %s\n", filename);
6326 const char *SelfFName = DladdrSelfFName();
6327 VPrintf(1, "dlopen interceptor: DladdrSelfFName: %p %s\n",
6328 (void *)SelfFName, SelfFName);
6330 if (internal_strcmp(SelfFName, filename) == 0) {
6331 // It's possible they copied the string from dladdr, so
6332 // we do a string comparison rather than pointer comparison.
6333 VPrintf(1, "dlopen interceptor: replacing %s because it matches %s\n",
6334 filename, SelfFName);
6335 filename = (char *)0; // RTLD_DEFAULT
6338 # endif // !SANITIZER_DYNAMIC
6341 void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
6342 Symbolizer::GetOrInit()->InvalidateModuleList();
6343 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6347 INTERCEPTOR(int, dlclose, void *handle) {
6349 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6350 int res = REAL(dlclose)(handle);
6351 Symbolizer::GetOrInit()->InvalidateModuleList();
6352 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6355 #define INIT_DLOPEN_DLCLOSE \
6356 COMMON_INTERCEPT_FUNCTION(dlopen); \
6357 COMMON_INTERCEPT_FUNCTION(dlclose);
6359 #define INIT_DLOPEN_DLCLOSE
6362 #if SANITIZER_INTERCEPT_GETPASS
6363 INTERCEPTOR(char *, getpass, const char *prompt) {
6365 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6367 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
6368 char *res = REAL(getpass)(prompt);
6369 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
6373 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6375 #define INIT_GETPASS
6378 #if SANITIZER_INTERCEPT_TIMERFD
6379 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6382 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6384 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6385 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6386 if (res != -1 && old_value)
6387 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6391 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6393 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6394 int res = REAL(timerfd_gettime)(fd, curr_value);
6395 if (res != -1 && curr_value)
6396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6399 #define INIT_TIMERFD \
6400 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6401 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6403 #define INIT_TIMERFD
6406 #if SANITIZER_INTERCEPT_MLOCKX
6407 // Linux kernel has a bug that leads to kernel deadlock if a process
6408 // maps TBs of memory and then calls mlock().
6409 static void MlockIsUnsupported() {
6410 static atomic_uint8_t printed;
6411 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6413 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6417 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6418 MlockIsUnsupported();
6422 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6423 MlockIsUnsupported();
6427 INTERCEPTOR(int, mlockall, int flags) {
6428 MlockIsUnsupported();
6432 INTERCEPTOR(int, munlockall, void) {
6433 MlockIsUnsupported();
6437 #define INIT_MLOCKX \
6438 COMMON_INTERCEPT_FUNCTION(mlock); \
6439 COMMON_INTERCEPT_FUNCTION(munlock); \
6440 COMMON_INTERCEPT_FUNCTION(mlockall); \
6441 COMMON_INTERCEPT_FUNCTION(munlockall);
6445 #endif // SANITIZER_INTERCEPT_MLOCKX
6447 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6448 struct WrappedCookie {
6450 __sanitizer_cookie_io_functions_t real_io_funcs;
6453 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6454 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6455 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6456 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6457 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6460 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6461 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6462 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6463 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6464 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6467 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6468 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6469 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6470 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6471 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6472 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6476 static int wrapped_close(void *cookie) {
6477 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6478 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6479 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6480 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6481 InternalFree(wrapped_cookie);
6485 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6486 __sanitizer_cookie_io_functions_t io_funcs) {
6488 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6489 WrappedCookie *wrapped_cookie =
6490 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6491 wrapped_cookie->real_cookie = cookie;
6492 wrapped_cookie->real_io_funcs = io_funcs;
6493 __sanitizer_FILE *res =
6494 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6495 wrapped_seek, wrapped_close});
6499 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6501 #define INIT_FOPENCOOKIE
6502 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6504 #if SANITIZER_INTERCEPT_SEM
6505 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6507 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6508 // Workaround a bug in glibc's "old" semaphore implementation by
6509 // zero-initializing the sem_t contents. This has to be done here because
6510 // interceptors bind to the lowest version before glibc 2.36, hitting the
6511 // buggy code path while the non-sanitized build of the same code works fine.
6512 REAL(memset)(s, 0, sizeof(*s));
6513 int res = REAL(sem_init)(s, pshared, value);
6517 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6519 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6520 int res = REAL(sem_destroy)(s);
6524 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6526 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6527 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6529 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6534 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6536 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6537 int res = REAL(sem_trywait)(s);
6539 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6544 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6546 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6547 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6548 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6550 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6555 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6557 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6558 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6559 int res = REAL(sem_post)(s);
6563 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6565 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6566 int res = REAL(sem_getvalue)(s, sval);
6568 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6574 INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
6577 va_start(ap, oflag);
6578 u32 mode = va_arg(ap, u32);
6579 u32 value = va_arg(ap, u32);
6580 COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
6581 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6582 __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
6584 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
6589 INTERCEPTOR(int, sem_unlink, const char *name) {
6591 COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
6592 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6593 return REAL(sem_unlink)(name);
6597 COMMON_INTERCEPT_FUNCTION(sem_init); \
6598 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6599 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6600 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6601 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6602 COMMON_INTERCEPT_FUNCTION(sem_post); \
6603 COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
6604 COMMON_INTERCEPT_FUNCTION(sem_open); \
6605 COMMON_INTERCEPT_FUNCTION(sem_unlink);
6608 #endif // SANITIZER_INTERCEPT_SEM
6610 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6611 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6613 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6614 int res = REAL(pthread_setcancelstate)(state, oldstate);
6615 if (res == 0 && oldstate != nullptr)
6616 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6620 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6622 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6623 int res = REAL(pthread_setcanceltype)(type, oldtype);
6624 if (res == 0 && oldtype != nullptr)
6625 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6628 #define INIT_PTHREAD_SETCANCEL \
6629 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6630 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6632 #define INIT_PTHREAD_SETCANCEL
6635 #if SANITIZER_INTERCEPT_MINCORE
6636 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6638 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6639 int res = REAL(mincore)(addr, length, vec);
6641 uptr page_size = GetPageSizeCached();
6642 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6643 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6647 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6649 #define INIT_MINCORE
6652 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6653 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6654 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6657 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6658 remote_iov, riovcnt, flags);
6659 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6662 write_iovec(ctx, local_iov, liovcnt, res);
6666 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6667 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6670 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6671 remote_iov, riovcnt, flags);
6672 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6675 read_iovec(ctx, local_iov, liovcnt, res);
6678 #define INIT_PROCESS_VM_READV \
6679 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6680 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6682 #define INIT_PROCESS_VM_READV
6685 #if SANITIZER_INTERCEPT_CTERMID
6686 INTERCEPTOR(char *, ctermid, char *s) {
6688 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6689 char *res = REAL(ctermid)(s);
6691 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6695 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6697 #define INIT_CTERMID
6700 #if SANITIZER_INTERCEPT_CTERMID_R
6701 INTERCEPTOR(char *, ctermid_r, char *s) {
6703 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6704 char *res = REAL(ctermid_r)(s);
6706 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6710 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6712 #define INIT_CTERMID_R
6715 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6716 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6718 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6719 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6720 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6722 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6724 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6728 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6729 void *srcaddr, int *addrlen) {
6731 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6733 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6735 if (srcaddr) srcaddr_sz = *addrlen;
6736 (void)srcaddr_sz; // prevent "set but not used" warning
6737 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6740 if (res >= 0 && srcaddr)
6741 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6742 Min((SIZE_T)*addrlen, srcaddr_sz));
6745 #define INIT_RECV_RECVFROM \
6746 COMMON_INTERCEPT_FUNCTION(recv); \
6747 COMMON_INTERCEPT_FUNCTION(recvfrom);
6749 #define INIT_RECV_RECVFROM
6752 #if SANITIZER_INTERCEPT_SEND_SENDTO
6753 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6755 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6757 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6758 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6760 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6761 if (common_flags()->intercept_send && res > 0)
6762 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6766 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6767 void *dstaddr, int addrlen) {
6769 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6771 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6772 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6774 // Can't check dstaddr as it may have uninitialized padding at the end.
6775 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6776 if (common_flags()->intercept_send && res > 0)
6777 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6780 #define INIT_SEND_SENDTO \
6781 COMMON_INTERCEPT_FUNCTION(send); \
6782 COMMON_INTERCEPT_FUNCTION(sendto);
6784 #define INIT_SEND_SENDTO
6787 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6788 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6790 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6791 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6792 int res = REAL(eventfd_read)(fd, value);
6794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6795 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6799 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6801 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6803 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6804 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6806 int res = REAL(eventfd_write)(fd, value);
6809 #define INIT_EVENTFD_READ_WRITE \
6810 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6811 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6813 #define INIT_EVENTFD_READ_WRITE
6816 #if SANITIZER_INTERCEPT_STAT
6817 INTERCEPTOR(int, stat, const char *path, void *buf) {
6819 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6820 if (common_flags()->intercept_stat)
6821 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6822 int res = REAL(stat)(path, buf);
6824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6827 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6832 #if SANITIZER_INTERCEPT_STAT64
6833 INTERCEPTOR(int, stat64, const char *path, void *buf) {
6835 COMMON_INTERCEPTOR_ENTER(ctx, stat64, path, buf);
6836 if (common_flags()->intercept_stat)
6837 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6838 int res = REAL(stat64)(path, buf);
6840 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6843 #define INIT_STAT64 COMMON_INTERCEPT_FUNCTION(stat64)
6849 #if SANITIZER_INTERCEPT_LSTAT
6850 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6852 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6853 if (common_flags()->intercept_stat)
6854 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6855 int res = REAL(lstat)(path, buf);
6857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6860 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6865 #if SANITIZER_INTERCEPT_STAT64
6866 INTERCEPTOR(int, lstat64, const char *path, void *buf) {
6868 COMMON_INTERCEPTOR_ENTER(ctx, lstat64, path, buf);
6869 if (common_flags()->intercept_stat)
6870 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6871 int res = REAL(lstat64)(path, buf);
6873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6876 #define INIT_LSTAT64 COMMON_INTERCEPT_FUNCTION(lstat64)
6878 #define INIT_LSTAT64
6881 #if SANITIZER_INTERCEPT___XSTAT
6882 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6884 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6885 if (common_flags()->intercept_stat)
6886 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6887 int res = REAL(__xstat)(version, path, buf);
6889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6892 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6894 #define INIT___XSTAT
6897 #if SANITIZER_INTERCEPT___XSTAT64
6898 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6900 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6901 if (common_flags()->intercept_stat)
6902 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6903 int res = REAL(__xstat64)(version, path, buf);
6905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6908 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6910 #define INIT___XSTAT64
6913 #if SANITIZER_INTERCEPT___LXSTAT
6914 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6916 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6917 if (common_flags()->intercept_stat)
6918 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6919 int res = REAL(__lxstat)(version, path, buf);
6921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6924 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6926 #define INIT___LXSTAT
6929 #if SANITIZER_INTERCEPT___LXSTAT64
6930 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6932 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6933 if (common_flags()->intercept_stat)
6934 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6935 int res = REAL(__lxstat64)(version, path, buf);
6937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6940 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6942 #define INIT___LXSTAT64
6945 // FIXME: add other *stat interceptor
6947 #if SANITIZER_INTERCEPT_UTMP
6948 INTERCEPTOR(void *, getutent, int dummy) {
6950 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6951 void *res = REAL(getutent)(dummy);
6953 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6956 INTERCEPTOR(void *, getutid, void *ut) {
6958 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6959 void *res = REAL(getutid)(ut);
6961 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6964 INTERCEPTOR(void *, getutline, void *ut) {
6966 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6967 void *res = REAL(getutline)(ut);
6969 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6973 COMMON_INTERCEPT_FUNCTION(getutent); \
6974 COMMON_INTERCEPT_FUNCTION(getutid); \
6975 COMMON_INTERCEPT_FUNCTION(getutline);
6980 #if SANITIZER_INTERCEPT_UTMPX
6981 INTERCEPTOR(void *, getutxent, int dummy) {
6983 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6984 void *res = REAL(getutxent)(dummy);
6986 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6989 INTERCEPTOR(void *, getutxid, void *ut) {
6991 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6992 void *res = REAL(getutxid)(ut);
6994 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6997 INTERCEPTOR(void *, getutxline, void *ut) {
6999 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
7000 void *res = REAL(getutxline)(ut);
7002 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7005 INTERCEPTOR(void *, pututxline, const void *ut) {
7007 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
7009 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
7010 void *res = REAL(pututxline)(ut);
7012 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
7015 #define INIT_UTMPX \
7016 COMMON_INTERCEPT_FUNCTION(getutxent); \
7017 COMMON_INTERCEPT_FUNCTION(getutxid); \
7018 COMMON_INTERCEPT_FUNCTION(getutxline); \
7019 COMMON_INTERCEPT_FUNCTION(pututxline);
7024 #if SANITIZER_INTERCEPT_GETLOADAVG
7025 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
7027 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
7028 int res = REAL(getloadavg)(loadavg, nelem);
7030 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
7033 #define INIT_GETLOADAVG \
7034 COMMON_INTERCEPT_FUNCTION(getloadavg);
7036 #define INIT_GETLOADAVG
7039 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
7040 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
7044 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
7048 INTERCEPTOR(int, mprobe, void *ptr) {
7053 #if SANITIZER_INTERCEPT_WCSLEN
7054 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
7056 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
7057 SIZE_T res = REAL(wcslen)(s);
7058 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
7062 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
7064 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
7065 SIZE_T res = REAL(wcsnlen)(s, n);
7066 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
7069 #define INIT_WCSLEN \
7070 COMMON_INTERCEPT_FUNCTION(wcslen); \
7071 COMMON_INTERCEPT_FUNCTION(wcsnlen);
7076 #if SANITIZER_INTERCEPT_WCSCAT
7077 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
7079 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
7080 SIZE_T src_size = internal_wcslen(src);
7081 SIZE_T dst_size = internal_wcslen(dst);
7082 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
7083 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7084 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7085 (src_size + 1) * sizeof(wchar_t));
7086 return REAL(wcscat)(dst, src);
7089 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
7091 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
7092 SIZE_T src_size = internal_wcsnlen(src, n);
7093 SIZE_T dst_size = internal_wcslen(dst);
7094 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
7095 Min(src_size + 1, n) * sizeof(wchar_t));
7096 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7098 (src_size + 1) * sizeof(wchar_t));
7099 return REAL(wcsncat)(dst, src, n);
7101 #define INIT_WCSCAT \
7102 COMMON_INTERCEPT_FUNCTION(wcscat); \
7103 COMMON_INTERCEPT_FUNCTION(wcsncat);
7108 #if SANITIZER_INTERCEPT_WCSDUP
7109 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
7111 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
7112 SIZE_T len = internal_wcslen(s);
7113 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
7114 wchar_t *result = REAL(wcsdup)(s);
7116 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
7120 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
7125 #if SANITIZER_INTERCEPT_STRXFRM
7126 static SIZE_T RealStrLen(const char *str) { return internal_strlen(str); }
7128 static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(str); }
7130 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
7133 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
7134 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
7135 sizeof(*src) * (RealStrLen(src) + 1)); \
7136 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
7138 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
7142 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
7143 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
7146 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
7148 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
7151 #define INIT_STRXFRM \
7152 COMMON_INTERCEPT_FUNCTION(strxfrm); \
7153 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
7155 #define INIT_STRXFRM
7158 #if SANITIZER_INTERCEPT___STRXFRM_L
7159 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
7161 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
7164 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
7166 #define INIT___STRXFRM_L
7169 #if SANITIZER_INTERCEPT_WCSXFRM
7170 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
7171 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
7174 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7176 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
7179 #define INIT_WCSXFRM \
7180 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
7181 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
7183 #define INIT_WCSXFRM
7186 #if SANITIZER_INTERCEPT___WCSXFRM_L
7187 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7189 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
7192 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
7194 #define INIT___WCSXFRM_L
7197 #if SANITIZER_INTERCEPT_ACCT
7198 INTERCEPTOR(int, acct, const char *file) {
7200 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
7202 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
7203 return REAL(acct)(file);
7205 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
7210 #if SANITIZER_INTERCEPT_USER_FROM_UID
7211 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
7214 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
7215 user = REAL(user_from_uid)(uid, nouser);
7217 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
7220 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
7222 #define INIT_USER_FROM_UID
7225 #if SANITIZER_INTERCEPT_UID_FROM_USER
7226 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
7229 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
7231 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7232 res = REAL(uid_from_user)(name, uid);
7234 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
7237 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
7239 #define INIT_UID_FROM_USER
7242 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
7243 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
7246 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
7247 group = REAL(group_from_gid)(gid, nogroup);
7249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
7252 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
7254 #define INIT_GROUP_FROM_GID
7257 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
7258 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
7261 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
7263 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
7264 res = REAL(gid_from_group)(group, gid);
7266 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
7269 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
7271 #define INIT_GID_FROM_GROUP
7274 #if SANITIZER_INTERCEPT_ACCESS
7275 INTERCEPTOR(int, access, const char *path, int mode) {
7277 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
7279 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7280 return REAL(access)(path, mode);
7282 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
7287 #if SANITIZER_INTERCEPT_FACCESSAT
7288 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
7290 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
7292 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7293 return REAL(faccessat)(fd, path, mode, flags);
7295 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
7297 #define INIT_FACCESSAT
7300 #if SANITIZER_INTERCEPT_GETGROUPLIST
7301 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
7305 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
7307 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7309 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
7310 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
7311 if (!res && groups && ngroups) {
7312 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7313 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7318 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
7320 #define INIT_GETGROUPLIST
7323 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
7324 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
7325 int maxgrp, int *ngroups) {
7328 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
7331 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7332 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
7333 if (!res && groups && ngroups) {
7334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7340 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
7342 #define INIT_GETGROUPMEMBERSHIP
7345 #if SANITIZER_INTERCEPT_READLINK
7346 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
7348 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
7349 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7350 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
7352 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7356 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
7358 #define INIT_READLINK
7361 #if SANITIZER_INTERCEPT_READLINKAT
7362 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
7365 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
7366 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7367 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7369 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7373 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7375 #define INIT_READLINKAT
7378 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7379 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7380 struct file_handle *handle, int *mount_id, int flags) {
7382 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7384 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
7386 __sanitizer_file_handle *sanitizer_handle =
7387 reinterpret_cast<__sanitizer_file_handle*>(handle);
7388 COMMON_INTERCEPTOR_READ_RANGE(
7389 ctx, &sanitizer_handle->handle_bytes,
7390 sizeof(sanitizer_handle->handle_bytes));
7392 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7394 COMMON_INTERCEPTOR_WRITE_RANGE(
7395 ctx, &sanitizer_handle->handle_bytes,
7396 sizeof(sanitizer_handle->handle_bytes));
7397 COMMON_INTERCEPTOR_WRITE_RANGE(
7398 ctx, &sanitizer_handle->handle_type,
7399 sizeof(sanitizer_handle->handle_type));
7400 COMMON_INTERCEPTOR_WRITE_RANGE(
7401 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7402 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7407 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7409 #define INIT_NAME_TO_HANDLE_AT
7412 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7413 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7416 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7418 __sanitizer_file_handle *sanitizer_handle =
7419 reinterpret_cast<__sanitizer_file_handle*>(handle);
7420 COMMON_INTERCEPTOR_READ_RANGE(
7421 ctx, &sanitizer_handle->handle_bytes,
7422 sizeof(sanitizer_handle->handle_bytes));
7423 COMMON_INTERCEPTOR_READ_RANGE(
7424 ctx, &sanitizer_handle->handle_type,
7425 sizeof(sanitizer_handle->handle_type));
7426 COMMON_INTERCEPTOR_READ_RANGE(
7427 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7429 return REAL(open_by_handle_at)(mount_fd, handle, flags);
7432 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7434 #define INIT_OPEN_BY_HANDLE_AT
7437 #if SANITIZER_INTERCEPT_STRLCPY
7438 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7441 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7443 // Keep strnlen as macro argument, as macro may ignore it.
7444 COMMON_INTERCEPTOR_READ_STRING(
7445 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7447 res = REAL(strlcpy)(dst, src, size);
7448 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
7452 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7455 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7456 // src is checked in the strlcpy() interceptor
7458 len = internal_strnlen(dst, size);
7459 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7461 // Reuse the rest of the code in the strlcpy() interceptor
7462 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7464 #define INIT_STRLCPY \
7465 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7466 COMMON_INTERCEPT_FUNCTION(strlcat);
7468 #define INIT_STRLCPY
7471 #if SANITIZER_INTERCEPT_MMAP
7472 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7475 if (common_flags()->detect_write_exec)
7476 ReportMmapWriteExec(prot, flags);
7477 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7478 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7479 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7480 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7483 INTERCEPTOR(int, munmap, void *addr, SIZE_T sz) {
7485 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7486 return (int)internal_munmap(addr, sz);
7487 COMMON_INTERCEPTOR_ENTER(ctx, munmap, addr, sz);
7488 COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz);
7491 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7493 if (common_flags()->detect_write_exec)
7494 ReportMmapWriteExec(prot, 0);
7495 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7496 return (int)internal_mprotect(addr, sz, prot);
7497 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7498 MprotectMallocZones(addr, prot);
7499 return REAL(mprotect)(addr, sz, prot);
7502 COMMON_INTERCEPT_FUNCTION(mmap); \
7503 COMMON_INTERCEPT_FUNCTION(munmap); \
7504 COMMON_INTERCEPT_FUNCTION(mprotect);
7509 #if SANITIZER_INTERCEPT_MMAP64
7510 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7513 if (common_flags()->detect_write_exec)
7514 ReportMmapWriteExec(prot, flags);
7515 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7516 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7517 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7518 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7520 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7525 #if SANITIZER_INTERCEPT_DEVNAME
7526 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7529 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7530 name = REAL(devname)(dev, type);
7532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
7535 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7537 #define INIT_DEVNAME
7540 #if SANITIZER_INTERCEPT_DEVNAME_R
7541 #if SANITIZER_NETBSD
7542 #define DEVNAME_R_RETTYPE int
7543 #define DEVNAME_R_SUCCESS(x) (!(x))
7545 #define DEVNAME_R_RETTYPE char*
7546 #define DEVNAME_R_SUCCESS(x) (x)
7548 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7551 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7552 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7553 if (DEVNAME_R_SUCCESS(res))
7554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
7557 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7559 #define INIT_DEVNAME_R
7562 #if SANITIZER_INTERCEPT_FGETLN
7563 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7565 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7566 char *str = REAL(fgetln)(stream, len);
7568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7573 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7578 #if SANITIZER_INTERCEPT_STRMODE
7579 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7581 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7582 REAL(strmode)(mode, bp);
7584 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
7586 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7588 #define INIT_STRMODE
7591 #if SANITIZER_INTERCEPT_TTYENT
7592 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7594 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7595 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7597 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7600 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7602 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7604 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7605 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7607 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7610 #define INIT_TTYENT \
7611 COMMON_INTERCEPT_FUNCTION(getttyent); \
7612 COMMON_INTERCEPT_FUNCTION(getttynam);
7617 #if SANITIZER_INTERCEPT_TTYENTPATH
7618 INTERCEPTOR(int, setttyentpath, char *path) {
7620 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7622 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7623 return REAL(setttyentpath)(path);
7625 #define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
7627 #define INIT_TTYENTPATH
7630 #if SANITIZER_INTERCEPT_PROTOENT
7631 static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
7632 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
7636 SIZE_T pp_size = 1; // One handles the trailing \0
7638 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7639 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
7641 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, pp_size * sizeof(char *));
7644 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7646 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7647 struct __sanitizer_protoent *p = REAL(getprotoent)();
7649 write_protoent(ctx, p);
7653 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7655 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7657 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7658 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7660 write_protoent(ctx, p);
7664 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7666 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7667 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7669 write_protoent(ctx, p);
7672 #define INIT_PROTOENT \
7673 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7674 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7675 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7677 #define INIT_PROTOENT
7680 #if SANITIZER_INTERCEPT_PROTOENT_R
7681 INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
7682 char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
7684 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
7686 int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
7688 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7689 if (!res && *result)
7690 write_protoent(ctx, *result);
7694 INTERCEPTOR(int, getprotobyname_r, const char *name,
7695 struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
7696 struct __sanitizer_protoent **result) {
7698 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
7701 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7702 int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
7704 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7705 if (!res && *result)
7706 write_protoent(ctx, *result);
7710 INTERCEPTOR(int, getprotobynumber_r, int num,
7711 struct __sanitizer_protoent *result_buf, char *buf,
7712 SIZE_T buflen, struct __sanitizer_protoent **result) {
7714 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
7716 int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
7718 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7719 if (!res && *result)
7720 write_protoent(ctx, *result);
7724 #define INIT_PROTOENT_R \
7725 COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
7726 COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
7727 COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
7729 #define INIT_PROTOENT_R
7732 #if SANITIZER_INTERCEPT_NETENT
7733 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7735 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7736 struct __sanitizer_netent *n = REAL(getnetent)();
7738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7740 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7742 SIZE_T nn_size = 1; // One handles the trailing \0
7744 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7752 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7754 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7756 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7757 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7761 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7763 SIZE_T nn_size = 1; // One handles the trailing \0
7765 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7768 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7773 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7775 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7776 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7778 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7782 SIZE_T nn_size = 1; // One handles the trailing \0
7784 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7785 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7787 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7791 #define INIT_NETENT \
7792 COMMON_INTERCEPT_FUNCTION(getnetent); \
7793 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7794 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7799 #if SANITIZER_INTERCEPT_GETMNTINFO
7800 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7802 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7803 int cnt = REAL(getmntinfo)(mntbufp, flags);
7804 if (cnt > 0 && mntbufp) {
7805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7807 #if SANITIZER_NETBSD
7808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7810 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7815 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7817 #define INIT_GETMNTINFO
7820 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7821 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7824 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7826 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7827 REAL(mi_vector_hash)(key, len, seed, hashes);
7829 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7831 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7833 #define INIT_MI_VECTOR_HASH
7836 #if SANITIZER_INTERCEPT_SETVBUF
7837 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7840 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7841 int ret = REAL(setvbuf)(stream, buf, mode, size);
7843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7845 unpoison_file(stream);
7849 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7851 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7852 REAL(setbuf)(stream, buf);
7854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7857 unpoison_file(stream);
7860 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
7862 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
7863 REAL(setbuffer)(stream, buf, size);
7865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7868 unpoison_file(stream);
7871 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7873 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7874 REAL(setlinebuf)(stream);
7876 unpoison_file(stream);
7878 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7879 COMMON_INTERCEPT_FUNCTION(setbuf); \
7880 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7881 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7883 #define INIT_SETVBUF
7886 #if SANITIZER_INTERCEPT_GETVFSSTAT
7887 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7889 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7890 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7892 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7895 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7897 #define INIT_GETVFSSTAT
7900 #if SANITIZER_INTERCEPT_REGEX
7901 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7903 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7905 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
7906 int res = REAL(regcomp)(preg, pattern, cflags);
7908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7911 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7912 struct __sanitizer_regmatch *pmatch[], int eflags) {
7914 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7916 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7918 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
7919 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7924 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7925 SIZE_T errbuf_size) {
7927 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7929 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7930 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
7935 INTERCEPTOR(void, regfree, const void *preg) {
7937 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7939 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7940 REAL(regfree)(preg);
7942 #define INIT_REGEX \
7943 COMMON_INTERCEPT_FUNCTION(regcomp); \
7944 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
7945 COMMON_INTERCEPT_FUNCTION(regerror); \
7946 COMMON_INTERCEPT_FUNCTION(regfree);
7951 #if SANITIZER_INTERCEPT_REGEXSUB
7952 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7953 const struct __sanitizer_regmatch *rm, const char *str) {
7955 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7957 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
7958 // The implementation demands and hardcodes 10 elements
7960 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7962 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
7963 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7965 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
7968 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7969 const struct __sanitizer_regmatch *rm, const char *sstr) {
7971 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7973 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
7974 // Hardcode 10 elements as this is hardcoded size
7976 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7978 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
7979 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7980 if (res > 0 && buf) {
7981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
7987 #define INIT_REGEXSUB \
7988 COMMON_INTERCEPT_FUNCTION(regnsub); \
7989 COMMON_INTERCEPT_FUNCTION(regasub);
7991 #define INIT_REGEXSUB
7994 #if SANITIZER_INTERCEPT_FTS
7995 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7996 int (*compar)(void **, void **)) {
7998 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
8000 for (char *const *pa = path_argv; ; ++pa) {
8001 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
8004 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
8007 // TODO(kamil): handle compar callback
8008 void *fts = REAL(fts_open)(path_argv, options, compar);
8010 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
8014 INTERCEPTOR(void *, fts_read, void *ftsp) {
8016 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
8018 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8019 void *ftsent = REAL(fts_read)(ftsp);
8021 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8025 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
8027 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
8029 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8030 void *ftsent = REAL(fts_children)(ftsp, options);
8032 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8036 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
8038 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
8040 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8042 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
8043 return REAL(fts_set)(ftsp, f, options);
8046 INTERCEPTOR(int, fts_close, void *ftsp) {
8048 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
8050 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8051 return REAL(fts_close)(ftsp);
8054 COMMON_INTERCEPT_FUNCTION(fts_open); \
8055 COMMON_INTERCEPT_FUNCTION(fts_read); \
8056 COMMON_INTERCEPT_FUNCTION(fts_children); \
8057 COMMON_INTERCEPT_FUNCTION(fts_set); \
8058 COMMON_INTERCEPT_FUNCTION(fts_close);
8063 #if SANITIZER_INTERCEPT_SYSCTL
8064 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
8065 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
8067 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8068 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
8069 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
8072 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
8074 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8076 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8077 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
8080 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8088 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
8089 void *newp, SIZE_T newlen) {
8091 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8092 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
8093 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
8096 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8098 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8100 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8101 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
8104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8112 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
8115 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
8117 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8119 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8120 int res = REAL(sysctlnametomib)(sname, name, namelenp);
8123 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8125 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8131 #define INIT_SYSCTL \
8132 COMMON_INTERCEPT_FUNCTION(sysctl); \
8133 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
8134 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
8139 #if SANITIZER_INTERCEPT_ASYSCTL
8140 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
8142 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
8144 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
8145 void *res = REAL(asysctl)(name, namelen, len);
8147 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8148 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8153 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
8155 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
8157 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8158 void *res = REAL(asysctlbyname)(sname, len);
8160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8165 #define INIT_ASYSCTL \
8166 COMMON_INTERCEPT_FUNCTION(asysctl); \
8167 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
8169 #define INIT_ASYSCTL
8172 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
8173 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
8174 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
8177 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
8180 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8182 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8184 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
8185 // Skip rnode, it's rarely used and not trivial to sanitize
8186 // It's also used mostly internally
8187 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
8190 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8195 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
8197 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
8202 #define INIT_SYSCTLGETMIBINFO \
8203 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
8205 #define INIT_SYSCTLGETMIBINFO
8208 #if SANITIZER_INTERCEPT_NL_LANGINFO
8209 INTERCEPTOR(char *, nl_langinfo, long item) {
8211 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
8212 char *ret = REAL(nl_langinfo)(item);
8214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8217 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
8219 #define INIT_NL_LANGINFO
8222 #if SANITIZER_INTERCEPT_MODCTL
8223 INTERCEPTOR(int, modctl, int operation, void *argp) {
8226 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
8228 if (operation == modctl_load) {
8230 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
8231 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
8232 if (ml->ml_filename)
8233 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
8234 internal_strlen(ml->ml_filename) + 1);
8236 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
8238 ret = REAL(modctl)(operation, argp);
8239 } else if (operation == modctl_unload) {
8241 const char *name = (const char *)argp;
8242 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
8244 ret = REAL(modctl)(operation, argp);
8245 } else if (operation == modctl_stat) {
8247 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
8249 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
8250 iov_len = iov->iov_len;
8252 ret = REAL(modctl)(operation, argp);
8254 COMMON_INTERCEPTOR_WRITE_RANGE(
8255 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
8256 } else if (operation == modctl_exists) {
8257 ret = REAL(modctl)(operation, argp);
8259 ret = REAL(modctl)(operation, argp);
8264 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
8269 #if SANITIZER_INTERCEPT_STRTONUM
8270 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
8271 long long maxval, const char **errstr) {
8273 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
8275 // TODO(kamil): Implement strtoll as a common inteceptor
8277 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
8278 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
8280 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
8282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
8284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
8288 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
8290 #define INIT_STRTONUM
8293 #if SANITIZER_INTERCEPT_FPARSELN
8294 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
8295 SIZE_T *lineno, const char delim[3], int flags) {
8297 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
8299 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
8301 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
8302 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
8304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8306 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8308 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
8312 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
8314 #define INIT_FPARSELN
8317 #if SANITIZER_INTERCEPT_STATVFS1
8318 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
8320 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
8321 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
8322 int res = REAL(statvfs1)(path, buf, flags);
8323 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8326 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
8328 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
8329 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
8330 int res = REAL(fstatvfs1)(fd, buf, flags);
8332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8334 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
8338 #define INIT_STATVFS1 \
8339 COMMON_INTERCEPT_FUNCTION(statvfs1); \
8340 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
8342 #define INIT_STATVFS1
8345 #if SANITIZER_INTERCEPT_STRTOI
8346 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
8347 INTMAX_T low, INTMAX_T high, int *rstatus) {
8349 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
8351 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
8352 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8358 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
8359 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
8361 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
8363 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
8364 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8366 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8369 #define INIT_STRTOI \
8370 COMMON_INTERCEPT_FUNCTION(strtoi); \
8371 COMMON_INTERCEPT_FUNCTION(strtou)
8376 #if SANITIZER_INTERCEPT_CAPSICUM
8377 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
8380 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
8382 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8383 __sanitizer_cap_rights_t *ret = \
8384 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
8386 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8390 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
8393 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
8395 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8396 __sanitizer_cap_rights_t *ret = \
8397 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
8399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8403 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
8406 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
8408 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8409 __sanitizer_cap_rights_t *ret = \
8410 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8412 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8416 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8419 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8421 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8422 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8425 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8426 __sanitizer_cap_rights_t *rights) {
8427 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8430 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8431 __sanitizer_cap_rights_t *rights) {
8432 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8435 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8436 __sanitizer_cap_rights_t *rights) {
8437 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8440 INTERCEPTOR(bool, cap_rights_is_set,
8441 __sanitizer_cap_rights_t *rights) {
8442 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8445 INTERCEPTOR(int, cap_rights_limit, int fd,
8446 const __sanitizer_cap_rights_t *rights) {
8448 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8450 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8452 return REAL(cap_rights_limit)(fd, rights);
8455 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8457 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8458 int ret = REAL(cap_rights_get)(fd, rights);
8460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8465 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8467 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8469 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8471 return REAL(cap_rights_is_valid(rights));
8474 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8475 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8477 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8479 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8481 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8488 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8489 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8491 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8493 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8495 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8497 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8502 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8503 const __sanitizer_cap_rights *little) {
8505 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8507 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8509 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8511 return REAL(cap_rights_contains)(big, little);
8514 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8516 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8518 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8520 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8523 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8525 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8526 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8532 #define INIT_CAPSICUM \
8533 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8534 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8535 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8536 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8537 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8538 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8539 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8540 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8541 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8542 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8543 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8544 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8546 #define INIT_CAPSICUM
8549 #if SANITIZER_INTERCEPT_SHA1
8550 INTERCEPTOR(void, SHA1Init, void *context) {
8552 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8553 REAL(SHA1Init)(context);
8555 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8557 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8559 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8560 if (data && len > 0)
8561 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8563 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8564 REAL(SHA1Update)(context, data, len);
8566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8568 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8570 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8572 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8573 REAL(SHA1Final)(digest, context);
8575 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8577 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8579 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8581 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8583 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8584 REAL(SHA1Transform)(state, buffer);
8586 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8588 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8590 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8592 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8593 char *ret = REAL(SHA1End)(context, buf);
8595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8598 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8600 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8602 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8603 char *ret = REAL(SHA1File)(filename, buf);
8605 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8608 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8611 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8613 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8614 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8616 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8619 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8621 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8623 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8624 char *ret = REAL(SHA1Data)(data, len, buf);
8626 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8630 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8631 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8632 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8633 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8634 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8635 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8636 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8637 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8642 #if SANITIZER_INTERCEPT_MD4
8643 INTERCEPTOR(void, MD4Init, void *context) {
8645 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8646 REAL(MD4Init)(context);
8648 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8651 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8654 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8655 if (data && len > 0)
8656 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8658 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8659 REAL(MD4Update)(context, data, len);
8661 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8664 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8666 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8668 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8669 REAL(MD4Final)(digest, context);
8671 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8674 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8676 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8678 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8679 char *ret = REAL(MD4End)(context, buf);
8681 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8685 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8687 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8689 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8690 char *ret = REAL(MD4File)(filename, buf);
8692 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8696 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8699 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8700 if (data && len > 0)
8701 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8702 char *ret = REAL(MD4Data)(data, len, buf);
8704 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8709 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8710 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8711 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8712 COMMON_INTERCEPT_FUNCTION(MD4End); \
8713 COMMON_INTERCEPT_FUNCTION(MD4File); \
8714 COMMON_INTERCEPT_FUNCTION(MD4Data)
8719 #if SANITIZER_INTERCEPT_RMD160
8720 INTERCEPTOR(void, RMD160Init, void *context) {
8722 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8723 REAL(RMD160Init)(context);
8725 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8727 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8729 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8730 if (data && len > 0)
8731 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8733 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8734 REAL(RMD160Update)(context, data, len);
8736 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8738 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8740 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8742 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8743 REAL(RMD160Final)(digest, context);
8745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8747 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8749 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8751 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8753 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8754 REAL(RMD160Transform)(state, buffer);
8756 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8758 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8760 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8762 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8763 char *ret = REAL(RMD160End)(context, buf);
8765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8768 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8770 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8772 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8773 char *ret = REAL(RMD160File)(filename, buf);
8775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8778 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8781 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8783 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8784 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8789 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8791 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8792 if (data && len > 0)
8793 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8794 char *ret = REAL(RMD160Data)(data, len, buf);
8796 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8799 #define INIT_RMD160 \
8800 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8801 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8802 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8803 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8804 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8805 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8806 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8807 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8812 #if SANITIZER_INTERCEPT_MD5
8813 INTERCEPTOR(void, MD5Init, void *context) {
8815 COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8816 REAL(MD5Init)(context);
8818 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8821 INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8824 COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8825 if (data && len > 0)
8826 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8828 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8829 REAL(MD5Update)(context, data, len);
8831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8834 INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8836 COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8838 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8839 REAL(MD5Final)(digest, context);
8841 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8844 INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8846 COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8848 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8849 char *ret = REAL(MD5End)(context, buf);
8851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8855 INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8857 COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8859 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8860 char *ret = REAL(MD5File)(filename, buf);
8862 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8866 INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8869 COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8870 if (data && len > 0)
8871 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8872 char *ret = REAL(MD5Data)(data, len, buf);
8874 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8879 COMMON_INTERCEPT_FUNCTION(MD5Init); \
8880 COMMON_INTERCEPT_FUNCTION(MD5Update); \
8881 COMMON_INTERCEPT_FUNCTION(MD5Final); \
8882 COMMON_INTERCEPT_FUNCTION(MD5End); \
8883 COMMON_INTERCEPT_FUNCTION(MD5File); \
8884 COMMON_INTERCEPT_FUNCTION(MD5Data)
8889 #if SANITIZER_INTERCEPT_FSEEK
8890 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8892 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8893 return REAL(fseek)(stream, offset, whence);
8895 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8897 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8898 return REAL(fseeko)(stream, offset, whence);
8900 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8902 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8903 return REAL(ftell)(stream);
8905 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8907 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8908 return REAL(ftello)(stream);
8910 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8912 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8913 return REAL(rewind)(stream);
8915 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8917 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8918 int ret = REAL(fgetpos)(stream, pos);
8920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8923 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8925 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8927 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8928 return REAL(fsetpos)(stream, pos);
8930 #define INIT_FSEEK \
8931 COMMON_INTERCEPT_FUNCTION(fseek); \
8932 COMMON_INTERCEPT_FUNCTION(fseeko); \
8933 COMMON_INTERCEPT_FUNCTION(ftell); \
8934 COMMON_INTERCEPT_FUNCTION(ftello); \
8935 COMMON_INTERCEPT_FUNCTION(rewind); \
8936 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8937 COMMON_INTERCEPT_FUNCTION(fsetpos)
8942 #if SANITIZER_INTERCEPT_MD2
8943 INTERCEPTOR(void, MD2Init, void *context) {
8945 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8946 REAL(MD2Init)(context);
8948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8951 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8954 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8955 if (data && len > 0)
8956 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8958 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8959 REAL(MD2Update)(context, data, len);
8961 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8964 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8966 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8968 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8969 REAL(MD2Final)(digest, context);
8971 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8974 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8976 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8978 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8979 char *ret = REAL(MD2End)(context, buf);
8981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8985 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8987 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8989 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8990 char *ret = REAL(MD2File)(filename, buf);
8992 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8996 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8999 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
9000 if (data && len > 0)
9001 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
9002 char *ret = REAL(MD2Data)(data, len, buf);
9004 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9009 COMMON_INTERCEPT_FUNCTION(MD2Init); \
9010 COMMON_INTERCEPT_FUNCTION(MD2Update); \
9011 COMMON_INTERCEPT_FUNCTION(MD2Final); \
9012 COMMON_INTERCEPT_FUNCTION(MD2End); \
9013 COMMON_INTERCEPT_FUNCTION(MD2File); \
9014 COMMON_INTERCEPT_FUNCTION(MD2Data)
9019 #if SANITIZER_INTERCEPT_SHA2
9020 #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
9021 INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
9023 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
9024 REAL(SHA##LEN##_Init)(context); \
9026 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9028 INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
9029 const u8 *data, SIZE_T len) { \
9031 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
9032 if (data && len > 0) \
9033 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
9035 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9036 REAL(SHA##LEN##_Update)(context, data, len); \
9038 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9040 INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
9043 CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
9044 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
9046 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9047 REAL(SHA##LEN##_Final)(digest, context); \
9049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
9050 sizeof(digest[0]) * \
9051 SHA##LEN##_digest_length); \
9053 INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
9055 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
9057 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9058 char *ret = REAL(SHA##LEN##_End)(context, buf); \
9060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9063 INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
9065 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
9067 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
9068 char *ret = REAL(SHA##LEN##_File)(filename, buf); \
9070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9073 INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
9074 OFF_T offset, OFF_T length) { \
9076 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
9079 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
9080 char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
9082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9085 INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
9087 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
9088 if (data && len > 0) \
9089 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
9090 char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
9092 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9096 SHA2_INTERCEPTORS(224, u32)
9097 SHA2_INTERCEPTORS(256, u32)
9098 SHA2_INTERCEPTORS(384, u64)
9099 SHA2_INTERCEPTORS(512, u64)
9101 #define INIT_SHA2_INTECEPTORS(LEN) \
9102 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
9103 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
9104 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
9105 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
9106 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
9107 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
9108 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
9111 INIT_SHA2_INTECEPTORS(224); \
9112 INIT_SHA2_INTECEPTORS(256); \
9113 INIT_SHA2_INTECEPTORS(384); \
9114 INIT_SHA2_INTECEPTORS(512)
9115 #undef SHA2_INTERCEPTORS
9120 #if SANITIZER_INTERCEPT_VIS
9121 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
9123 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
9124 char *end = REAL(vis)(dst, c, flag, nextc);
9125 // dst is NULL terminated and end points to the NULL char
9127 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9130 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
9132 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
9133 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
9134 // nvis cannot make sure the dst is NULL terminated
9136 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9139 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
9141 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
9143 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9144 int len = REAL(strvis)(dst, src, flag);
9146 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9149 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
9151 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
9153 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9154 int len = REAL(stravis)(dst, src, flag);
9156 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
9158 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
9162 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
9164 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
9166 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9167 int len = REAL(strnvis)(dst, dlen, src, flag);
9168 // The interface will be valid even if there is no space for NULL char
9170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9173 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
9175 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
9177 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9178 int ret = REAL(strvisx)(dst, src, len, flag);
9180 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9183 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9186 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
9188 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9189 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
9190 if (dst && ret >= 0)
9191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9194 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9195 int flag, int *cerr_ptr) {
9197 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
9199 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9200 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9201 // according to the implementation
9203 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9204 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
9205 if (dst && ret >= 0)
9206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9208 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9211 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
9212 const char *extra) {
9214 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
9216 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9217 char *end = REAL(svis)(dst, c, flag, nextc, extra);
9219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9222 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
9223 const char *extra) {
9225 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
9227 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9228 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
9230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
9231 Min((SIZE_T)(end - dst + 1), dlen));
9234 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
9235 const char *extra) {
9237 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
9239 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9241 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9242 int len = REAL(strsvis)(dst, src, flag, extra);
9244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9247 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
9248 const char *extra) {
9250 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
9252 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9254 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9255 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
9256 // The interface will be valid even if there is no space for NULL char
9257 if (dst && len >= 0)
9258 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9261 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
9262 const char *extra) {
9264 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
9266 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9268 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9269 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
9271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9274 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9275 int flag, const char *extra) {
9277 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
9279 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9281 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9282 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
9283 if (dst && ret >= 0)
9284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9287 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
9288 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
9290 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
9293 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9295 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9296 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9297 // according to the implementation
9299 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9300 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
9301 if (dst && ret >= 0)
9302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9307 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
9309 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
9311 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
9312 int ret = REAL(unvis)(cp, c, astate, flag);
9313 if (ret == unvis_valid || ret == unvis_validpush) {
9314 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
9318 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
9320 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
9322 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9323 int ret = REAL(strunvis)(dst, src);
9325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9328 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
9330 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
9332 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9333 int ret = REAL(strnunvis)(dst, dlen, src);
9335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9338 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
9340 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
9342 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9343 int ret = REAL(strunvisx)(dst, src, flag);
9345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9348 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
9351 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
9353 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9354 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
9356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9360 COMMON_INTERCEPT_FUNCTION(vis); \
9361 COMMON_INTERCEPT_FUNCTION(nvis); \
9362 COMMON_INTERCEPT_FUNCTION(strvis); \
9363 COMMON_INTERCEPT_FUNCTION(stravis); \
9364 COMMON_INTERCEPT_FUNCTION(strnvis); \
9365 COMMON_INTERCEPT_FUNCTION(strvisx); \
9366 COMMON_INTERCEPT_FUNCTION(strnvisx); \
9367 COMMON_INTERCEPT_FUNCTION(strenvisx); \
9368 COMMON_INTERCEPT_FUNCTION(svis); \
9369 COMMON_INTERCEPT_FUNCTION(snvis); \
9370 COMMON_INTERCEPT_FUNCTION(strsvis); \
9371 COMMON_INTERCEPT_FUNCTION(strsnvis); \
9372 COMMON_INTERCEPT_FUNCTION(strsvisx); \
9373 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
9374 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
9375 COMMON_INTERCEPT_FUNCTION(unvis); \
9376 COMMON_INTERCEPT_FUNCTION(strunvis); \
9377 COMMON_INTERCEPT_FUNCTION(strnunvis); \
9378 COMMON_INTERCEPT_FUNCTION(strunvisx); \
9379 COMMON_INTERCEPT_FUNCTION(strnunvisx)
9384 #if SANITIZER_INTERCEPT_CDB
9385 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
9387 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
9389 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9390 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
9392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9396 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
9397 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
9399 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
9402 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
9403 struct __sanitizer_cdbr *cdbr =
9404 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
9406 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9410 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9412 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9414 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9415 return REAL(cdbr_entries)(cdbr);
9418 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9419 const void **data, SIZE_T *datalen) {
9421 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9423 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9424 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9427 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9430 if (data && datalen)
9431 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9436 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9437 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9439 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9441 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9443 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9444 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9447 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9449 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9450 if (data && datalen)
9451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9456 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9458 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9460 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9461 REAL(cdbr_close)(cdbr);
9464 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9466 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9467 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9469 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9473 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9474 SIZE_T keylen, const void *data, SIZE_T datalen) {
9476 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9478 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9479 if (data && datalen)
9480 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9482 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9483 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9485 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9489 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9490 SIZE_T datalen, u32 *index) {
9492 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9494 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9495 if (data && datalen)
9496 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9497 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9500 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9502 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9507 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9508 SIZE_T keylen, u32 index) {
9510 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9512 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9514 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9515 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9517 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9521 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9522 const char descr[16], u32 (*seedgen)(void)) {
9524 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9525 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9527 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9529 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9531 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9532 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9535 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9537 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9542 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9544 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9546 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9547 REAL(cdbw_close)(cdbw);
9551 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9552 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9553 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9554 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9555 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9556 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9557 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9558 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9559 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9560 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9561 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9562 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9567 #if SANITIZER_INTERCEPT_GETFSENT
9568 INTERCEPTOR(void *, getfsent) {
9570 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9571 void *ret = REAL(getfsent)();
9573 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9577 INTERCEPTOR(void *, getfsspec, const char *spec) {
9579 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9581 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
9582 void *ret = REAL(getfsspec)(spec);
9584 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9588 INTERCEPTOR(void *, getfsfile, const char *file) {
9590 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9592 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
9593 void *ret = REAL(getfsfile)(file);
9595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9599 #define INIT_GETFSENT \
9600 COMMON_INTERCEPT_FUNCTION(getfsent); \
9601 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9602 COMMON_INTERCEPT_FUNCTION(getfsfile);
9604 #define INIT_GETFSENT
9607 #if SANITIZER_INTERCEPT_ARC4RANDOM
9608 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9610 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9611 REAL(arc4random_buf)(buf, len);
9613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9616 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9618 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9620 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9621 REAL(arc4random_addrandom)(dat, datlen);
9624 #define INIT_ARC4RANDOM \
9625 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9626 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9628 #define INIT_ARC4RANDOM
9631 #if SANITIZER_INTERCEPT_POPEN
9632 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9634 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9636 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
9638 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9639 __sanitizer_FILE *res = REAL(popen)(command, type);
9640 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9641 if (res) unpoison_file(res);
9644 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9649 #if SANITIZER_INTERCEPT_POPENVE
9650 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9651 char *const *argv, char *const *envp, const char *type) {
9653 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9655 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9657 for (char *const *pa = argv; ; ++pa) {
9658 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9661 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9665 for (char *const *pa = envp; ; ++pa) {
9666 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9669 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9673 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9674 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9675 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9676 if (res) unpoison_file(res);
9679 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9681 #define INIT_POPENVE
9684 #if SANITIZER_INTERCEPT_PCLOSE
9685 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9687 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9688 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9689 const FileMetadata *m = GetInterceptorMetadata(fp);
9690 int res = REAL(pclose)(fp);
9692 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9693 DeleteInterceptorMetadata(fp);
9697 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9702 #if SANITIZER_INTERCEPT_FUNOPEN
9703 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9704 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9705 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9706 typedef int (*funopen_closefn)(void *cookie);
9708 struct WrappedFunopenCookie {
9710 funopen_readfn real_read;
9711 funopen_writefn real_write;
9712 funopen_seekfn real_seek;
9713 funopen_closefn real_close;
9716 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9717 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9718 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9719 funopen_readfn real_read = wrapped_cookie->real_read;
9720 return real_read(wrapped_cookie->real_cookie, buf, len);
9723 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9724 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9725 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9726 funopen_writefn real_write = wrapped_cookie->real_write;
9727 return real_write(wrapped_cookie->real_cookie, buf, len);
9730 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9731 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9732 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9733 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9734 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9737 static int wrapped_funopen_close(void *cookie) {
9738 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9739 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9740 funopen_closefn real_close = wrapped_cookie->real_close;
9741 int res = real_close(wrapped_cookie->real_cookie);
9742 InternalFree(wrapped_cookie);
9746 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9747 funopen_writefn writefn, funopen_seekfn seekfn,
9748 funopen_closefn closefn) {
9750 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9753 WrappedFunopenCookie *wrapped_cookie =
9754 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9755 wrapped_cookie->real_cookie = cookie;
9756 wrapped_cookie->real_read = readfn;
9757 wrapped_cookie->real_write = writefn;
9758 wrapped_cookie->real_seek = seekfn;
9759 wrapped_cookie->real_close = closefn;
9761 __sanitizer_FILE *res =
9762 REAL(funopen)(wrapped_cookie,
9763 readfn ? wrapped_funopen_read : nullptr,
9764 writefn ? wrapped_funopen_write : nullptr,
9765 seekfn ? wrapped_funopen_seek : nullptr,
9766 closefn ? wrapped_funopen_close : nullptr);
9771 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9773 #define INIT_FUNOPEN
9776 #if SANITIZER_INTERCEPT_FUNOPEN2
9777 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9778 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9779 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9780 typedef int (*funopen2_flushfn)(void *cookie);
9781 typedef int (*funopen2_closefn)(void *cookie);
9783 struct WrappedFunopen2Cookie {
9785 funopen2_readfn real_read;
9786 funopen2_writefn real_write;
9787 funopen2_seekfn real_seek;
9788 funopen2_flushfn real_flush;
9789 funopen2_closefn real_close;
9792 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9793 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9794 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9795 funopen2_readfn real_read = wrapped_cookie->real_read;
9796 return real_read(wrapped_cookie->real_cookie, buf, len);
9799 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9801 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9802 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9803 funopen2_writefn real_write = wrapped_cookie->real_write;
9804 return real_write(wrapped_cookie->real_cookie, buf, len);
9807 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9808 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9809 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9810 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9811 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9814 static int wrapped_funopen2_flush(void *cookie) {
9815 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9816 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9817 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9818 return real_flush(wrapped_cookie->real_cookie);
9821 static int wrapped_funopen2_close(void *cookie) {
9822 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9823 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9824 funopen2_closefn real_close = wrapped_cookie->real_close;
9825 int res = real_close(wrapped_cookie->real_cookie);
9826 InternalFree(wrapped_cookie);
9830 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9831 funopen2_writefn writefn, funopen2_seekfn seekfn,
9832 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9834 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9837 WrappedFunopen2Cookie *wrapped_cookie =
9838 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9839 wrapped_cookie->real_cookie = cookie;
9840 wrapped_cookie->real_read = readfn;
9841 wrapped_cookie->real_write = writefn;
9842 wrapped_cookie->real_seek = seekfn;
9843 wrapped_cookie->real_flush = flushfn;
9844 wrapped_cookie->real_close = closefn;
9846 __sanitizer_FILE *res =
9847 REAL(funopen2)(wrapped_cookie,
9848 readfn ? wrapped_funopen2_read : nullptr,
9849 writefn ? wrapped_funopen2_write : nullptr,
9850 seekfn ? wrapped_funopen2_seek : nullptr,
9851 flushfn ? wrapped_funopen2_flush : nullptr,
9852 closefn ? wrapped_funopen2_close : nullptr);
9857 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9859 #define INIT_FUNOPEN2
9862 #if SANITIZER_INTERCEPT_FDEVNAME
9863 INTERCEPTOR(char *, fdevname, int fd) {
9865 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9866 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9867 char *name = REAL(fdevname)(fd);
9869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
9871 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9876 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9878 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9879 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9880 char *name = REAL(fdevname_r)(fd, buf, len);
9881 if (name && buf && len > 0) {
9882 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
9884 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9889 #define INIT_FDEVNAME \
9890 COMMON_INTERCEPT_FUNCTION(fdevname); \
9891 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9893 #define INIT_FDEVNAME
9896 #if SANITIZER_INTERCEPT_GETUSERSHELL
9897 INTERCEPTOR(char *, getusershell) {
9899 COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9900 char *res = REAL(getusershell)();
9902 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9906 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9908 #define INIT_GETUSERSHELL
9911 #if SANITIZER_INTERCEPT_SL_INIT
9912 INTERCEPTOR(void *, sl_init) {
9914 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9915 void *res = REAL(sl_init)();
9917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9921 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9923 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9925 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9927 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9928 int res = REAL(sl_add)(sl, item);
9930 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9934 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9936 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9938 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9940 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9941 char *res = REAL(sl_find)(sl, item);
9943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9947 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9949 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9951 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9952 REAL(sl_free)(sl, freeall);
9955 #define INIT_SL_INIT \
9956 COMMON_INTERCEPT_FUNCTION(sl_init); \
9957 COMMON_INTERCEPT_FUNCTION(sl_add); \
9958 COMMON_INTERCEPT_FUNCTION(sl_find); \
9959 COMMON_INTERCEPT_FUNCTION(sl_free);
9961 #define INIT_SL_INIT
9964 #if SANITIZER_INTERCEPT_GETRANDOM
9965 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
9967 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
9968 SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
9970 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
9974 #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
9976 #define INIT_GETRANDOM
9979 #if SANITIZER_INTERCEPT_GETENTROPY
9980 INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
9982 COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
9983 int r = REAL(getentropy)(buf, buflen);
9985 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
9989 #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
9991 #define INIT_GETENTROPY
9994 #if SANITIZER_INTERCEPT_QSORT_R
9995 typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
9996 struct qsort_r_compar_params {
9998 qsort_r_compar_f compar;
10001 static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
10002 qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
10003 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
10004 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
10005 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
10006 return params->compar(a, b, params->arg);
10009 INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
10010 qsort_r_compar_f compar, void *arg) {
10012 COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
10013 // Run the comparator over all array elements to detect any memory issues.
10015 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
10016 void *p = (void *)((char *)base + i * size);
10017 void *q = (void *)((char *)base + (i + 1) * size);
10018 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
10022 qsort_r_compar_params params = {size, compar, arg};
10023 REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, ¶ms);
10024 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
10026 # define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
10028 # define INIT_QSORT_R
10031 #if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
10032 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10033 qsort_r_compar_f compar) {
10035 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10036 WRAP(qsort_r)(base, nmemb, size, compar, nullptr);
10038 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10039 #elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
10040 // Glibc qsort uses a temporary buffer allocated either on stack or on heap.
10041 // Poisoned memory from there may get copied into the comparator arguments,
10042 // where it needs to be dealt with. But even that is not enough - the results of
10043 // the sort may be copied into the input/output array based on the results of
10044 // the comparator calls, but directly from the temp memory, bypassing the
10045 // unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
10046 // unpoisoning the entire array after the sort is done.
10048 // We can not check that the entire array is initialized at the beginning. IMHO,
10049 // it's fine for parts of the sorted objects to contain uninitialized memory,
10050 // ex. as padding in structs.
10051 typedef int (*qsort_compar_f)(const void *, const void *);
10052 static THREADLOCAL qsort_compar_f qsort_compar;
10053 static THREADLOCAL SIZE_T qsort_size;
10054 static int wrapped_qsort_compar(const void *a, const void *b) {
10055 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10056 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
10057 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
10058 return qsort_compar(a, b);
10061 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10062 qsort_compar_f compar) {
10064 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10065 // Run the comparator over all array elements to detect any memory issues.
10067 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
10068 void *p = (void *)((char *)base + i * size);
10069 void *q = (void *)((char *)base + (i + 1) * size);
10070 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10074 qsort_compar_f old_compar = qsort_compar;
10075 SIZE_T old_size = qsort_size;
10076 // Handle qsort() implementations that recurse using an
10077 // interposable function call:
10078 bool already_wrapped = compar == wrapped_qsort_compar;
10079 if (already_wrapped) {
10080 // This case should only happen if the qsort() implementation calls itself
10081 // using a preemptible function call (e.g. the FreeBSD libc version).
10082 // Check that the size and comparator arguments are as expected.
10083 CHECK_NE(compar, qsort_compar);
10084 CHECK_EQ(qsort_size, size);
10086 qsort_compar = compar;
10089 REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
10090 if (!already_wrapped) {
10091 qsort_compar = old_compar;
10092 qsort_size = old_size;
10094 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
10096 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10098 # define INIT_QSORT
10101 #if SANITIZER_INTERCEPT_BSEARCH
10102 typedef int (*bsearch_compar_f)(const void *, const void *);
10103 struct bsearch_compar_params {
10105 bsearch_compar_f compar;
10108 static int wrapped_bsearch_compar(const void *key, const void *b) {
10109 const bsearch_compar_params *params = (const bsearch_compar_params *)key;
10110 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10111 return params->compar(params->key, b);
10114 INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
10115 SIZE_T size, bsearch_compar_f compar) {
10117 COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
10118 bsearch_compar_params params = {key, compar};
10119 return REAL(bsearch)(¶ms, base, nmemb, size, wrapped_bsearch_compar);
10121 # define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
10123 # define INIT_BSEARCH
10126 #if SANITIZER_INTERCEPT_SIGALTSTACK
10127 INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
10129 COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
10130 int r = REAL(sigaltstack)(ss, oss);
10131 if (r == 0 && oss != nullptr) {
10132 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
10136 #define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
10138 #define INIT_SIGALTSTACK
10141 #if SANITIZER_INTERCEPT_PROCCTL
10142 INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
10144 COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
10145 static const int PROC_REAP_ACQUIRE = 2;
10146 static const int PROC_REAP_RELEASE = 3;
10147 static const int PROC_REAP_STATUS = 4;
10148 static const int PROC_REAP_GETPIDS = 5;
10149 static const int PROC_REAP_KILL = 6;
10150 if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
10151 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
10153 // reap_acquire/reap_release bears no arguments.
10154 if (cmd > PROC_REAP_RELEASE) {
10155 unsigned int reapsz;
10157 case PROC_REAP_STATUS:
10158 reapsz = struct_procctl_reaper_status_sz;
10160 case PROC_REAP_GETPIDS:
10161 reapsz = struct_procctl_reaper_pids_sz;
10163 case PROC_REAP_KILL:
10164 reapsz = struct_procctl_reaper_kill_sz;
10167 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
10170 return REAL(procctl)(idtype, id, cmd, data);
10172 #define INIT_PROCCTL COMMON_INTERCEPT_FUNCTION(procctl)
10174 #define INIT_PROCCTL
10177 #if SANITIZER_INTERCEPT_UNAME
10178 INTERCEPTOR(int, uname, struct utsname *utsname) {
10179 #if SANITIZER_LINUX
10180 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
10181 return internal_uname(utsname);
10184 COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
10185 int res = REAL(uname)(utsname);
10187 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10188 __sanitizer::struct_utsname_sz);
10191 #define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
10196 #if SANITIZER_INTERCEPT___XUNAME
10197 // FreeBSD's <sys/utsname.h> define uname() as
10198 // static __inline int uname(struct utsname *name) {
10199 // return __xuname(SYS_NMLN, (void*)name);
10201 INTERCEPTOR(int, __xuname, int size, void *utsname) {
10203 COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
10204 int res = REAL(__xuname)(size, utsname);
10206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10207 __sanitizer::struct_utsname_sz);
10210 #define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
10212 #define INIT___XUNAME
10215 #if SANITIZER_INTERCEPT_HEXDUMP
10216 INTERCEPTOR(void, hexdump, const void *ptr, int length, const char *header, int flags) {
10218 COMMON_INTERCEPTOR_ENTER(ctx, hexdump, ptr, length, header, flags);
10219 COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, length);
10220 COMMON_INTERCEPTOR_READ_RANGE(ctx, header, internal_strlen(header) + 1);
10221 REAL(hexdump)(ptr, length, header, flags);
10224 #define INIT_HEXDUMP COMMON_INTERCEPT_FUNCTION(hexdump);
10226 #define INIT_HEXDUMP
10229 #if SANITIZER_INTERCEPT_ARGP_PARSE
10230 INTERCEPTOR(int, argp_parse, const struct argp *argp, int argc, char **argv,
10231 unsigned flags, int *arg_index, void *input) {
10233 COMMON_INTERCEPTOR_ENTER(ctx, argp_parse, argp, argc, argv, flags, arg_index,
10235 for (int i = 0; i < argc; i++)
10236 COMMON_INTERCEPTOR_READ_RANGE(ctx, argv[i], internal_strlen(argv[i]) + 1);
10237 int res = REAL(argp_parse)(argp, argc, argv, flags, arg_index, input);
10238 if (!res && arg_index)
10239 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg_index, sizeof(int));
10243 #define INIT_ARGP_PARSE COMMON_INTERCEPT_FUNCTION(argp_parse);
10245 #define INIT_ARGP_PARSE
10248 #if SANITIZER_INTERCEPT_CPUSET_GETAFFINITY
10249 INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T cpusetsize, __sanitizer_cpuset_t *mask) {
10251 COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getaffinity, level, which, id, cpusetsize, mask);
10252 int res = REAL(cpuset_getaffinity)(level, which, id, cpusetsize, mask);
10253 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
10256 #define INIT_CPUSET_GETAFFINITY COMMON_INTERCEPT_FUNCTION(cpuset_getaffinity);
10258 #define INIT_CPUSET_GETAFFINITY
10261 #include "sanitizer_common_interceptors_netbsd_compat.inc"
10263 namespace __sanitizer {
10264 void InitializeMemintrinsicInterceptors();
10265 } // namespace __sanitizer
10267 static void InitializeCommonInterceptors() {
10269 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
10270 interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
10273 __sanitizer::InitializeMemintrinsicInterceptors();
10319 INIT_LOCALTIME_AND_FRIENDS;
10325 INIT_ISOC99_PRINTF;
10327 INIT_FREXPF_FREXPL;
10328 INIT_GETPWNAM_AND_FRIENDS;
10329 INIT_GETPWNAM_R_AND_FRIENDS;
10336 INIT_CLOCK_GETTIME;
10337 INIT_CLOCK_GETCPUCLOCKID;
10343 INIT_DN_COMP_EXPAND;
10348 INIT_PTHREAD_GETSCHEDPARAM;
10352 INIT_GETHOSTBYNAME;
10353 INIT_GETHOSTBYNAME2;
10354 INIT_GETHOSTBYNAME_R;
10355 INIT_GETHOSTBYNAME2_R;
10356 INIT_GETHOSTBYADDR_R;
10377 INIT_GET_CURRENT_DIR_NAME;
10379 INIT_STRTOIMAX_C23;
10388 INIT_CANONICALIZE_FILE_NAME;
10390 INIT_SCHED_GETAFFINITY;
10391 INIT_SCHED_GETPARAM;
10394 INIT_XPG_STRERROR_R;
10405 INIT_SIGSET_LOGICOPS;
10408 INIT_PTHREAD_SIGMASK;
10411 INIT___LIBC_THR_SETCANCELSTATE;
10419 INIT_ETHER_NTOA_ATON;
10424 INIT_PTHREAD_ATTR_GET;
10425 INIT_PTHREAD_ATTR_GET_SCHED;
10426 INIT_PTHREAD_ATTR_GETINHERITSCHED;
10427 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
10428 INIT_PTHREAD_GETAFFINITY_NP;
10429 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
10430 INIT_PTHREAD_MUTEXATTR_GETTYPE;
10431 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
10432 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
10433 INIT_PTHREAD_MUTEXATTR_GETROBUST;
10434 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
10435 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
10436 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
10437 INIT_PTHREAD_CONDATTR_GETPSHARED;
10438 INIT_PTHREAD_CONDATTR_GETCLOCK;
10439 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
10447 INIT_PTHREAD_SETNAME_NP;
10448 INIT_PTHREAD_GETNAME_NP;
10466 INIT_IF_INDEXTONAME;
10472 INIT_LIBIO_INTERNALS;
10476 INIT_OPEN_MEMSTREAM;
10480 INIT_DLOPEN_DLCLOSE;
10486 INIT_PTHREAD_SETCANCEL;
10488 INIT_PROCESS_VM_READV;
10491 INIT_RECV_RECVFROM;
10495 INIT_EVENTFD_READ_WRITE;
10502 // FIXME: add other *stat interceptors.
10512 INIT_USER_FROM_UID;
10513 INIT_UID_FROM_USER;
10514 INIT_GROUP_FROM_GID;
10515 INIT_GID_FROM_GROUP;
10519 INIT_GETGROUPMEMBERSHIP;
10522 INIT_NAME_TO_HANDLE_AT;
10523 INIT_OPEN_BY_HANDLE_AT;
10534 INIT_MI_VECTOR_HASH;
10542 INIT_SYSCTLGETMIBINFO;
10580 INIT_CPUSET_GETAFFINITY;