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 //===----------------------------------------------------------------------===//
36 #include "interception/interception.h"
37 #include "sanitizer_addrhashmap.h"
38 #include "sanitizer_errno.h"
39 #include "sanitizer_placement_new.h"
40 #include "sanitizer_platform_interceptors.h"
41 #include "sanitizer_symbolizer.h"
42 #include "sanitizer_tls_get_addr.h"
46 #if SANITIZER_INTERCEPTOR_HOOKS
47 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
48 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
49 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
51 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
52 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
54 #endif // SANITIZER_INTERCEPTOR_HOOKS
56 #if SANITIZER_WINDOWS && !defined(va_copy)
57 #define va_copy(dst, src) ((dst) = (src))
61 #define pthread_setname_np pthread_set_name_np
62 #define inet_aton __inet_aton
63 #define inet_pton __inet_pton
64 #define iconv __bsd_iconv
68 #define clock_getres __clock_getres50
69 #define clock_gettime __clock_gettime50
70 #define clock_settime __clock_settime50
71 #define ctime __ctime50
72 #define ctime_r __ctime_r50
73 #define devname __devname50
74 #define fgetpos __fgetpos50
75 #define fsetpos __fsetpos50
76 #define fstatvfs __fstatvfs90
77 #define fstatvfs1 __fstatvfs190
78 #define fts_children __fts_children60
79 #define fts_close __fts_close60
80 #define fts_open __fts_open60
81 #define fts_read __fts_read60
82 #define fts_set __fts_set60
83 #define getitimer __getitimer50
84 #define getmntinfo __getmntinfo90
85 #define getpwent __getpwent50
86 #define getpwnam __getpwnam50
87 #define getpwnam_r __getpwnam_r50
88 #define getpwuid __getpwuid50
89 #define getpwuid_r __getpwuid_r50
90 #define getutent __getutent50
91 #define getutxent __getutxent50
92 #define getutxid __getutxid50
93 #define getutxline __getutxline50
94 #define getvfsstat __getvfsstat90
95 #define pututxline __pututxline50
97 #define gmtime __gmtime50
98 #define gmtime_r __gmtime_r50
99 #define localtime __locatime50
100 #define localtime_r __localtime_r50
101 #define mktime __mktime50
102 #define lstat __lstat50
103 #define opendir __opendir30
104 #define readdir __readdir30
105 #define readdir_r __readdir_r30
106 #define scandir __scandir30
107 #define setitimer __setitimer50
108 #define setlocale __setlocale50
109 #define shmctl __shmctl50
110 #define sigaltstack __sigaltstack14
111 #define sigemptyset __sigemptyset14
112 #define sigfillset __sigfillset14
113 #define sigpending __sigpending14
114 #define sigprocmask __sigprocmask14
115 #define sigtimedwait __sigtimedwait50
116 #define stat __stat50
117 #define statvfs __statvfs90
118 #define statvfs1 __statvfs190
119 #define time __time50
120 #define times __times13
121 #define unvis __unvis50
122 #define wait3 __wait350
123 #define wait4 __wait450
124 extern const unsigned short *_ctype_tab_;
125 extern const short *_toupper_tab_;
126 extern const short *_tolower_tab_;
129 #if SANITIZER_MUSL && \
130 (defined(__i386__) || defined(__arm__) || SANITIZER_MIPS32 || SANITIZER_PPC32)
131 // musl 1.2.0 on existing 32-bit architectures uses new symbol names for the
132 // time-related functions that take 64-bit time_t values. See
133 // https://musl.libc.org/time64.html
134 #define adjtime __adjtime64
135 #define adjtimex __adjtimex_time64
136 #define aio_suspend __aio_suspend_time64
137 #define clock_adjtime __clock_adjtime64
138 #define clock_getres __clock_getres_time64
139 #define clock_gettime __clock_gettime64
140 #define clock_nanosleep __clock_nanosleep_time64
141 #define clock_settime __clock_settime64
142 #define cnd_timedwait __cnd_timedwait_time64
143 #define ctime __ctime64
144 #define ctime_r __ctime64_r
145 #define difftime __difftime64
146 #define dlsym __dlsym_time64
147 #define fstatat __fstatat_time64
148 #define fstat __fstat_time64
149 #define ftime __ftime64
150 #define futimens __futimens_time64
151 #define futimesat __futimesat_time64
152 #define futimes __futimes_time64
153 #define getitimer __getitimer_time64
154 #define getrusage __getrusage_time64
155 #define gettimeofday __gettimeofday_time64
156 #define gmtime __gmtime64
157 #define gmtime_r __gmtime64_r
158 #define localtime __localtime64
159 #define localtime_r __localtime64_r
160 #define lstat __lstat_time64
161 #define lutimes __lutimes_time64
162 #define mktime __mktime64
163 #define mq_timedreceive __mq_timedreceive_time64
164 #define mq_timedsend __mq_timedsend_time64
165 #define mtx_timedlock __mtx_timedlock_time64
166 #define nanosleep __nanosleep_time64
167 #define ppoll __ppoll_time64
168 #define pselect __pselect_time64
169 #define pthread_cond_timedwait __pthread_cond_timedwait_time64
170 #define pthread_mutex_timedlock __pthread_mutex_timedlock_time64
171 #define pthread_rwlock_timedrdlock __pthread_rwlock_timedrdlock_time64
172 #define pthread_rwlock_timedwrlock __pthread_rwlock_timedwrlock_time64
173 #define pthread_timedjoin_np __pthread_timedjoin_np_time64
174 #define recvmmsg __recvmmsg_time64
175 #define sched_rr_get_interval __sched_rr_get_interval_time64
176 #define select __select_time64
177 #define semtimedop __semtimedop_time64
178 #define sem_timedwait __sem_timedwait_time64
179 #define setitimer __setitimer_time64
180 #define settimeofday __settimeofday_time64
181 #define sigtimedwait __sigtimedwait_time64
182 #define stat __stat_time64
183 #define stime __stime64
184 #define thrd_sleep __thrd_sleep_time64
185 #define timegm __timegm_time64
186 #define timerfd_gettime __timerfd_gettime64
187 #define timerfd_settime __timerfd_settime64
188 #define timer_gettime __timer_gettime64
189 #define timer_settime __timer_settime64
190 #define timespec_get __timespec_get_time64
191 #define time __time64
192 #define utimensat __utimensat_time64
193 #define utimes __utimes_time64
194 #define utime __utime64
195 #define wait3 __wait3_time64
196 #define wait4 __wait4_time64
199 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
200 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
203 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
204 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
207 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
208 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
211 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
212 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
215 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
216 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
219 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
220 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
223 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
224 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
227 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
228 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
231 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
232 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
233 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
236 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
237 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
240 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
241 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
242 common_flags()->strict_string_checks ? (internal_strlen(s)) + 1 : (n) )
244 #ifndef COMMON_INTERCEPTOR_DLOPEN
245 #define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
246 ({ CheckNoDeepBind(filename, flag); REAL(dlopen)(filename, flag); })
249 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
250 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
253 #ifndef COMMON_INTERCEPTOR_ACQUIRE
254 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
257 #ifndef COMMON_INTERCEPTOR_RELEASE
258 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
261 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
262 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
265 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
266 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
269 #ifdef SANITIZER_NLDBL_VERSION
270 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
271 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
273 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
274 COMMON_INTERCEPT_FUNCTION(fn)
278 // If we could not find the versioned symbol, fall back to an unversioned
279 // lookup. This is needed to work around a GLibc bug that causes dlsym
280 // with RTLD_NEXT to return the oldest versioned symbol.
281 // See https://sourceware.org/bugzilla/show_bug.cgi?id=14932.
282 // For certain symbols (e.g. regexec) we have to perform a versioned lookup,
283 // but that versioned symbol will only exist for architectures where the
284 // oldest Glibc version pre-dates support for that architecture.
285 // For example, regexec@GLIBC_2.3.4 exists on x86_64, but not RISC-V.
286 // See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98920.
287 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
288 COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(fn, ver)
290 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
291 COMMON_INTERCEPT_FUNCTION(fn)
294 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
295 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
297 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
300 #ifndef COMMON_INTERCEPTOR_MUNMAP_IMPL
301 #define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz) \
302 { return REAL(munmap)(addr, sz); }
305 #ifndef COMMON_INTERCEPTOR_COPY_STRING
306 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
309 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
310 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
311 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
312 uptr copy_length = internal_strnlen(s, size); \
313 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
314 if (common_flags()->intercept_strndup) { \
315 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
318 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
319 internal_memcpy(new_mem, s, copy_length); \
320 new_mem[copy_length] = '\0'; \
325 #ifndef COMMON_INTERCEPTOR_STRERROR
326 #define COMMON_INTERCEPTOR_STRERROR() {}
329 struct FileMetadata {
330 // For open_memstream().
335 struct CommonInterceptorMetadata {
346 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
348 static MetadataHashMap *interceptor_metadata_map;
350 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
351 const FileMetadata &file) {
352 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
354 h->type = CommonInterceptorMetadata::CIMT_FILE;
358 UNUSED static const FileMetadata *GetInterceptorMetadata(
359 __sanitizer_FILE *addr) {
360 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
363 if (addr && h.exists()) {
365 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
372 UNUSED static void DeleteInterceptorMetadata(void *addr) {
373 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
378 #if SANITIZER_INTERCEPT_STRLEN
379 INTERCEPTOR(SIZE_T, strlen, const char *s) {
380 // Sometimes strlen is called prior to InitializeCommonInterceptors,
381 // in which case the REAL(strlen) typically used in
382 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
384 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
385 return internal_strlen(s);
387 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
388 SIZE_T result = REAL(strlen)(s);
389 if (common_flags()->intercept_strlen)
390 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
393 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
398 #if SANITIZER_INTERCEPT_STRNLEN
399 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
401 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
402 SIZE_T length = REAL(strnlen)(s, maxlen);
403 if (common_flags()->intercept_strlen)
404 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
407 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
412 #if SANITIZER_INTERCEPT_STRNDUP
413 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
415 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
417 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
420 #endif // SANITIZER_INTERCEPT_STRNDUP
422 #if SANITIZER_INTERCEPT___STRNDUP
423 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
425 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
427 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
429 #define INIT___STRNDUP
430 #endif // SANITIZER_INTERCEPT___STRNDUP
432 #if SANITIZER_INTERCEPT_TEXTDOMAIN
433 INTERCEPTOR(char*, textdomain, const char *domainname) {
435 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
436 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
437 char *domain = REAL(textdomain)(domainname);
439 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, internal_strlen(domain) + 1);
443 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
445 #define INIT_TEXTDOMAIN
448 #if SANITIZER_INTERCEPT_STRCMP || SANITIZER_INTERCEPT_MEMCMP
449 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
450 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
454 #if SANITIZER_INTERCEPT_STRCMP
455 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
456 const char *s1, const char *s2, int result)
458 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
460 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
461 unsigned char c1, c2;
464 c1 = (unsigned char)s1[i];
465 c2 = (unsigned char)s2[i];
466 if (c1 != c2 || c1 == '\0') break;
468 if (common_flags()->intercept_strcmp) {
469 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
470 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
472 int result = CharCmpX(c1, c2);
473 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
478 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
479 const char *s1, const char *s2, uptr n,
482 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
483 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
484 return internal_strncmp(s1, s2, size);
486 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
487 unsigned char c1 = 0, c2 = 0;
489 for (i = 0; i < size; i++) {
490 c1 = (unsigned char)s1[i];
491 c2 = (unsigned char)s2[i];
492 if (c1 != c2 || c1 == '\0') break;
496 if (common_flags()->strict_string_checks) {
497 for (; i1 < size && s1[i1]; i1++) {}
498 for (; i2 < size && s2[i2]; i2++) {}
500 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
501 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
502 int result = CharCmpX(c1, c2);
503 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
508 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
509 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
515 #if SANITIZER_INTERCEPT_STRCASECMP
516 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
517 int c1_low = ToLower(c1);
518 int c2_low = ToLower(c2);
519 return c1_low - c2_low;
522 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
523 const char *s1, const char *s2, int result)
525 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
527 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
528 unsigned char c1 = 0, c2 = 0;
531 c1 = (unsigned char)s1[i];
532 c2 = (unsigned char)s2[i];
533 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
535 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
536 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
537 int result = CharCaseCmp(c1, c2);
538 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
543 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
544 const char *s1, const char *s2, uptr size,
547 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
549 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
550 unsigned char c1 = 0, c2 = 0;
552 for (i = 0; i < size; i++) {
553 c1 = (unsigned char)s1[i];
554 c2 = (unsigned char)s2[i];
555 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
559 if (common_flags()->strict_string_checks) {
560 for (; i1 < size && s1[i1]; i1++) {}
561 for (; i2 < size && s2[i2]; i2++) {}
563 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
564 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
565 int result = CharCaseCmp(c1, c2);
566 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
567 s1, s2, size, result);
571 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
572 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
574 #define INIT_STRCASECMP
575 #define INIT_STRNCASECMP
578 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
579 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
581 uptr len1 = internal_strlen(s1);
582 uptr len2 = internal_strlen(s2);
583 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
584 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
588 #if SANITIZER_INTERCEPT_STRSTR
590 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
591 const char *s1, const char *s2, char *result)
593 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
594 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
595 return internal_strstr(s1, s2);
597 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
598 char *r = REAL(strstr)(s1, s2);
599 if (common_flags()->intercept_strstr)
600 StrstrCheck(ctx, r, s1, s2);
601 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
606 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
611 #if SANITIZER_INTERCEPT_STRCASESTR
613 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
614 const char *s1, const char *s2, char *result)
616 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
618 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
619 char *r = REAL(strcasestr)(s1, s2);
620 if (common_flags()->intercept_strstr)
621 StrstrCheck(ctx, r, s1, s2);
622 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
627 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
629 #define INIT_STRCASESTR
632 #if SANITIZER_INTERCEPT_STRTOK
634 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
636 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
637 if (!common_flags()->intercept_strtok) {
638 return REAL(strtok)(str, delimiters);
640 if (common_flags()->strict_string_checks) {
641 // If strict_string_checks is enabled, we check the whole first argument
642 // string on the first call (strtok saves this string in a static buffer
643 // for subsequent calls). We do not need to check strtok's result.
644 // As the delimiters can change, we check them every call.
645 if (str != nullptr) {
646 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
648 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
649 internal_strlen(delimiters) + 1);
650 return REAL(strtok)(str, delimiters);
652 // However, when strict_string_checks is disabled we cannot check the
653 // whole string on the first call. Instead, we check the result string
654 // which is guaranteed to be a NULL-terminated substring of the first
655 // argument. We also conservatively check one character of str and the
657 if (str != nullptr) {
658 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
660 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
661 char *result = REAL(strtok)(str, delimiters);
662 if (result != nullptr) {
663 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, internal_strlen(result) + 1);
664 } else if (str != nullptr) {
665 // No delimiter were found, it's safe to assume that the entire str was
667 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
673 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
678 #if SANITIZER_INTERCEPT_MEMMEM
679 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
680 const void *s1, SIZE_T len1, const void *s2,
681 SIZE_T len2, void *result)
683 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
686 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
687 void *r = REAL(memmem)(s1, len1, s2, len2);
688 if (common_flags()->intercept_memmem) {
689 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
690 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
692 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
693 s1, len1, s2, len2, r);
697 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
700 #endif // SANITIZER_INTERCEPT_MEMMEM
702 #if SANITIZER_INTERCEPT_STRCHR
703 INTERCEPTOR(char*, strchr, const char *s, int c) {
705 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
706 return internal_strchr(s, c);
707 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
708 char *result = REAL(strchr)(s, c);
709 if (common_flags()->intercept_strchr) {
710 // Keep strlen as macro argument, as macro may ignore it.
711 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
712 (result ? result - s : internal_strlen(s)) + 1);
716 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
721 #if SANITIZER_INTERCEPT_STRCHRNUL
722 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
724 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
725 char *result = REAL(strchrnul)(s, c);
726 uptr len = result - s + 1;
727 if (common_flags()->intercept_strchr)
728 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
731 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
733 #define INIT_STRCHRNUL
736 #if SANITIZER_INTERCEPT_STRRCHR
737 INTERCEPTOR(char*, strrchr, const char *s, int c) {
739 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
740 return internal_strrchr(s, c);
741 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
742 if (common_flags()->intercept_strchr)
743 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
744 return REAL(strrchr)(s, c);
746 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
751 #if SANITIZER_INTERCEPT_STRSPN
752 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
754 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
755 SIZE_T r = REAL(strspn)(s1, s2);
756 if (common_flags()->intercept_strspn) {
757 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
758 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
763 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
765 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
766 SIZE_T r = REAL(strcspn)(s1, s2);
767 if (common_flags()->intercept_strspn) {
768 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
769 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
774 #define INIT_STRSPN \
775 COMMON_INTERCEPT_FUNCTION(strspn); \
776 COMMON_INTERCEPT_FUNCTION(strcspn);
781 #if SANITIZER_INTERCEPT_STRPBRK
782 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
784 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
785 char *r = REAL(strpbrk)(s1, s2);
786 if (common_flags()->intercept_strpbrk) {
787 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
788 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
789 r ? r - s1 + 1 : internal_strlen(s1) + 1);
794 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
799 #if SANITIZER_INTERCEPT_MEMCMP
800 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
801 const void *s1, const void *s2, uptr n,
804 // Common code for `memcmp` and `bcmp`.
805 int MemcmpInterceptorCommon(void *ctx,
806 int (*real_fn)(const void *, const void *, uptr),
807 const void *a1, const void *a2, uptr size) {
808 if (common_flags()->intercept_memcmp) {
809 if (common_flags()->strict_memcmp) {
810 // Check the entire regions even if the first bytes of the buffers are
812 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
813 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
814 // Fallthrough to REAL(memcmp) below.
816 unsigned char c1 = 0, c2 = 0;
817 const unsigned char *s1 = (const unsigned char*)a1;
818 const unsigned char *s2 = (const unsigned char*)a2;
820 for (i = 0; i < size; i++) {
825 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
826 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
827 int r = CharCmpX(c1, c2);
828 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
833 int result = real_fn(a1, a2, size);
834 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
839 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
840 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
841 return internal_memcmp(a1, a2, size);
843 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
844 return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
847 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
852 #if SANITIZER_INTERCEPT_BCMP
853 INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {
854 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
855 return internal_memcmp(a1, a2, size);
857 COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
858 return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
861 #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
866 #if SANITIZER_INTERCEPT_MEMCHR
867 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
868 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
869 return internal_memchr(s, c, n);
871 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
872 #if SANITIZER_WINDOWS
875 res = REAL(memchr)(s, c, n);
877 res = internal_memchr(s, c, n);
880 void *res = REAL(memchr)(s, c, n);
882 uptr len = res ? (char *)res - (const char *)s + 1 : n;
883 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
887 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
892 #if SANITIZER_INTERCEPT_MEMRCHR
893 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
895 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
896 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
897 return REAL(memrchr)(s, c, n);
900 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
905 #if SANITIZER_INTERCEPT_FREXP
906 INTERCEPTOR(double, frexp, double x, int *exp) {
908 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
909 // Assuming frexp() always writes to |exp|.
910 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
911 double res = REAL(frexp)(x, exp);
912 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
916 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
919 #endif // SANITIZER_INTERCEPT_FREXP
921 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
922 INTERCEPTOR(float, frexpf, float x, int *exp) {
924 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
926 float res = REAL(frexpf)(x, exp);
927 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
931 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
933 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
934 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
935 long double res = REAL(frexpl)(x, exp);
936 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
940 #define INIT_FREXPF_FREXPL \
941 COMMON_INTERCEPT_FUNCTION(frexpf); \
942 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
944 #define INIT_FREXPF_FREXPL
945 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
948 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
949 SIZE_T iovlen, SIZE_T maxlen) {
950 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
951 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
957 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
958 SIZE_T iovlen, SIZE_T maxlen) {
959 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
960 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
961 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
962 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
968 #if SANITIZER_INTERCEPT_READ
969 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
971 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
972 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
973 // FIXME: under ASan the call below may write to freed memory and corrupt
975 // https://github.com/google/sanitizers/issues/321.
976 SSIZE_T res = REAL(read)(fd, ptr, count);
977 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
978 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
981 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
986 #if SANITIZER_INTERCEPT_FREAD
987 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
988 // libc file streams can call user-supplied functions, see fopencookie.
990 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
991 // FIXME: under ASan the call below may write to freed memory and corrupt
993 // https://github.com/google/sanitizers/issues/321.
994 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
995 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
998 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1003 #if SANITIZER_INTERCEPT_PREAD
1004 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1006 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1007 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1008 // FIXME: under ASan the call below may write to freed memory and corrupt
1009 // its metadata. See
1010 // https://github.com/google/sanitizers/issues/321.
1011 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1012 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1013 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1016 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1021 #if SANITIZER_INTERCEPT_PREAD64
1022 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1024 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1025 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1026 // FIXME: under ASan the call below may write to freed memory and corrupt
1027 // its metadata. See
1028 // https://github.com/google/sanitizers/issues/321.
1029 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1030 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1031 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1034 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1036 #define INIT_PREAD64
1039 #if SANITIZER_INTERCEPT_READV
1040 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1043 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1044 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1045 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1046 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1047 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1050 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1055 #if SANITIZER_INTERCEPT_PREADV
1056 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1059 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1060 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1061 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1062 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1063 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1066 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1071 #if SANITIZER_INTERCEPT_PREADV64
1072 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1075 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1076 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1077 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1078 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1079 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1082 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1084 #define INIT_PREADV64
1087 #if SANITIZER_INTERCEPT_WRITE
1088 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1090 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1091 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1092 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1093 SSIZE_T res = REAL(write)(fd, ptr, count);
1094 // FIXME: this check should be _before_ the call to REAL(write), not after
1095 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1098 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1103 #if SANITIZER_INTERCEPT_FWRITE
1104 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1105 // libc file streams can call user-supplied functions, see fopencookie.
1107 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1108 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1109 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1112 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1117 #if SANITIZER_INTERCEPT_PWRITE
1118 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1120 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1121 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1122 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1123 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1124 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1127 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1132 #if SANITIZER_INTERCEPT_PWRITE64
1133 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1136 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1137 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1138 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1139 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1140 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1143 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1145 #define INIT_PWRITE64
1148 #if SANITIZER_INTERCEPT_WRITEV
1149 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1152 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1153 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1154 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1155 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1156 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1159 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1164 #if SANITIZER_INTERCEPT_PWRITEV
1165 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1168 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1169 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1170 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1171 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1172 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1175 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1177 #define INIT_PWRITEV
1180 #if SANITIZER_INTERCEPT_PWRITEV64
1181 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1184 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1185 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1186 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1187 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1188 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1191 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1193 #define INIT_PWRITEV64
1196 #if SANITIZER_INTERCEPT_FGETS
1197 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1198 // libc file streams can call user-supplied functions, see fopencookie.
1200 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1201 // FIXME: under ASan the call below may write to freed memory and corrupt
1202 // its metadata. See
1203 // https://github.com/google/sanitizers/issues/321.
1204 char *res = REAL(fgets)(s, size, file);
1206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
1209 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1214 #if SANITIZER_INTERCEPT_FPUTS
1215 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1216 // libc file streams can call user-supplied functions, see fopencookie.
1218 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1219 if (!SANITIZER_APPLE || s) { // `fputs(NULL, file)` is supported on Darwin.
1220 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1222 return REAL(fputs)(s, file);
1224 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1229 #if SANITIZER_INTERCEPT_PUTS
1230 INTERCEPTOR(int, puts, char *s) {
1231 // libc file streams can call user-supplied functions, see fopencookie.
1233 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1234 if (!SANITIZER_APPLE || s) { // `puts(NULL)` is supported on Darwin.
1235 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1237 return REAL(puts)(s);
1239 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1244 #if SANITIZER_INTERCEPT_PRCTL
1245 INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
1246 unsigned long arg4, unsigned long arg5) {
1248 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1249 static const int PR_SET_NAME = 15;
1250 static const int PR_SET_VMA = 0x53564d41;
1251 static const int PR_SCHED_CORE = 62;
1252 static const int PR_SCHED_CORE_GET = 0;
1253 if (option == PR_SET_VMA && arg2 == 0UL) {
1254 char *name = (char *)arg5;
1255 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1257 int res = REAL(prctl)(option, arg2, arg3, arg4, arg5);
1258 if (option == PR_SET_NAME) {
1260 internal_strncpy(buff, (char *)arg2, 15);
1262 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1263 } else if (res != -1 && option == PR_SCHED_CORE && arg2 == PR_SCHED_CORE_GET) {
1264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64*)(arg5), sizeof(u64));
1268 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1271 #endif // SANITIZER_INTERCEPT_PRCTL
1273 #if SANITIZER_INTERCEPT_TIME
1274 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1276 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1277 unsigned long local_t;
1278 unsigned long res = REAL(time)(&local_t);
1279 if (t && res != (unsigned long)-1) {
1280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1285 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1288 #endif // SANITIZER_INTERCEPT_TIME
1290 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1291 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1293 #if !SANITIZER_SOLARIS
1295 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1296 // can point to shared memory and tsan would report a data race.
1297 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1298 internal_strlen(tm->tm_zone) + 1);
1302 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1304 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1305 __sanitizer_tm *res = REAL(localtime)(timep);
1307 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1308 unpoison_tm(ctx, res);
1312 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1314 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1315 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1317 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1318 unpoison_tm(ctx, res);
1322 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1324 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1325 __sanitizer_tm *res = REAL(gmtime)(timep);
1327 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1328 unpoison_tm(ctx, res);
1332 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1334 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1335 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1337 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1338 unpoison_tm(ctx, res);
1342 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1344 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1345 // FIXME: under ASan the call below may write to freed memory and corrupt
1346 // its metadata. See
1347 // https://github.com/google/sanitizers/issues/321.
1348 char *res = REAL(ctime)(timep);
1350 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1351 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1355 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1357 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1358 // FIXME: under ASan the call below may write to freed memory and corrupt
1359 // its metadata. See
1360 // https://github.com/google/sanitizers/issues/321.
1361 char *res = REAL(ctime_r)(timep, result);
1363 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1364 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1368 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1370 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1371 // FIXME: under ASan the call below may write to freed memory and corrupt
1372 // its metadata. See
1373 // https://github.com/google/sanitizers/issues/321.
1374 char *res = REAL(asctime)(tm);
1376 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1377 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1381 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1383 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1384 // FIXME: under ASan the call below may write to freed memory and corrupt
1385 // its metadata. See
1386 // https://github.com/google/sanitizers/issues/321.
1387 char *res = REAL(asctime_r)(tm, result);
1389 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1390 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1394 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1396 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1397 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1398 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1399 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1400 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1401 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1402 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1403 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1404 long res = REAL(mktime)(tm);
1405 if (res != -1) unpoison_tm(ctx, tm);
1408 #define INIT_LOCALTIME_AND_FRIENDS \
1409 COMMON_INTERCEPT_FUNCTION(localtime); \
1410 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1411 COMMON_INTERCEPT_FUNCTION(gmtime); \
1412 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1413 COMMON_INTERCEPT_FUNCTION(ctime); \
1414 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1415 COMMON_INTERCEPT_FUNCTION(asctime); \
1416 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1417 COMMON_INTERCEPT_FUNCTION(mktime);
1419 #define INIT_LOCALTIME_AND_FRIENDS
1420 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1422 #if SANITIZER_INTERCEPT_STRPTIME
1423 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1425 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1427 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
1428 // FIXME: under ASan the call below may write to freed memory and corrupt
1429 // its metadata. See
1430 // https://github.com/google/sanitizers/issues/321.
1431 char *res = REAL(strptime)(s, format, tm);
1432 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1434 // Do not call unpoison_tm here, because strptime does not, in fact,
1435 // initialize the entire struct tm. For example, tm_zone pointer is left
1437 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1441 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1443 #define INIT_STRPTIME
1446 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1447 #include "sanitizer_common_interceptors_format.inc"
1449 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1453 va_start(ap, format); \
1454 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1455 int res = WRAP(vname)(__VA_ARGS__, ap); \
1462 #if SANITIZER_INTERCEPT_SCANF
1464 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1467 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1470 int res = REAL(vname)(__VA_ARGS__); \
1472 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1477 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1478 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1480 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1481 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1483 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1484 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1486 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1487 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1488 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1490 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1492 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1494 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1495 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1497 INTERCEPTOR(int, __isoc23_vscanf, const char *format, va_list ap)
1498 VSCANF_INTERCEPTOR_IMPL(__isoc23_vscanf, false, format, ap)
1500 INTERCEPTOR(int, __isoc23_vsscanf, const char *str, const char *format,
1502 VSCANF_INTERCEPTOR_IMPL(__isoc23_vsscanf, false, str, format, ap)
1504 INTERCEPTOR(int, __isoc23_vfscanf, void *stream, const char *format, va_list ap)
1505 VSCANF_INTERCEPTOR_IMPL(__isoc23_vfscanf, false, stream, format, ap)
1506 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1508 INTERCEPTOR(int, scanf, const char *format, ...)
1509 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1511 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1512 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1514 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1515 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1517 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1518 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1519 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1521 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1522 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1524 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1525 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1527 INTERCEPTOR(int, __isoc23_scanf, const char *format, ...)
1528 FORMAT_INTERCEPTOR_IMPL(__isoc23_scanf, __isoc23_vscanf, format)
1530 INTERCEPTOR(int, __isoc23_fscanf, void *stream, const char *format, ...)
1531 FORMAT_INTERCEPTOR_IMPL(__isoc23_fscanf, __isoc23_vfscanf, stream, format)
1533 INTERCEPTOR(int, __isoc23_sscanf, const char *str, const char *format, ...)
1534 FORMAT_INTERCEPTOR_IMPL(__isoc23_sscanf, __isoc23_vsscanf, str, format)
1539 #if SANITIZER_INTERCEPT_SCANF
1540 #define INIT_SCANF \
1541 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1542 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1543 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1544 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1545 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1546 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1551 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1552 #define INIT_ISOC99_SCANF \
1553 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1554 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1555 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1556 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1557 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1558 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf); \
1559 COMMON_INTERCEPT_FUNCTION(__isoc23_scanf); \
1560 COMMON_INTERCEPT_FUNCTION(__isoc23_sscanf); \
1561 COMMON_INTERCEPT_FUNCTION(__isoc23_fscanf); \
1562 COMMON_INTERCEPT_FUNCTION(__isoc23_vscanf); \
1563 COMMON_INTERCEPT_FUNCTION(__isoc23_vsscanf); \
1564 COMMON_INTERCEPT_FUNCTION(__isoc23_vfscanf);
1566 #define INIT_ISOC99_SCANF
1569 #if SANITIZER_INTERCEPT_PRINTF
1571 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1573 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1577 #define VPRINTF_INTERCEPTOR_RETURN() \
1580 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1582 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1583 if (common_flags()->check_printf) \
1584 printf_common(ctx, format, aq); \
1585 int res = REAL(vname)(__VA_ARGS__); \
1586 VPRINTF_INTERCEPTOR_RETURN(); \
1590 // FIXME: under ASan the REAL() call below may write to freed memory and
1591 // corrupt its metadata. See
1592 // https://github.com/google/sanitizers/issues/321.
1593 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1595 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1596 if (common_flags()->check_printf) { \
1597 printf_common(ctx, format, aq); \
1599 int res = REAL(vname)(str, __VA_ARGS__); \
1601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1603 VPRINTF_INTERCEPTOR_RETURN(); \
1607 // FIXME: under ASan the REAL() call below may write to freed memory and
1608 // corrupt its metadata. See
1609 // https://github.com/google/sanitizers/issues/321.
1610 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1612 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1613 if (common_flags()->check_printf) { \
1614 printf_common(ctx, format, aq); \
1616 int res = REAL(vname)(str, size, __VA_ARGS__); \
1618 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1620 VPRINTF_INTERCEPTOR_RETURN(); \
1624 // FIXME: under ASan the REAL() call below may write to freed memory and
1625 // corrupt its metadata. See
1626 // https://github.com/google/sanitizers/issues/321.
1627 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1629 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1630 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1631 if (common_flags()->check_printf) { \
1632 printf_common(ctx, format, aq); \
1634 int res = REAL(vname)(strp, __VA_ARGS__); \
1636 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1638 VPRINTF_INTERCEPTOR_RETURN(); \
1642 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1643 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1645 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1647 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1649 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1651 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1653 #if SANITIZER_INTERCEPT___PRINTF_CHK
1654 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1655 SIZE_T size_to, const char *format, va_list ap)
1656 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1659 #if SANITIZER_INTERCEPT_PRINTF_L
1660 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1661 const char *format, va_list ap)
1662 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1664 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1665 const char *format, ...)
1666 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1667 #endif // SANITIZER_INTERCEPT_PRINTF_L
1669 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1670 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1672 #if SANITIZER_INTERCEPT___PRINTF_CHK
1673 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1674 const char *format, va_list ap)
1675 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1678 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1679 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1681 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1682 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1683 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1685 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1686 const char *format, va_list ap)
1687 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1689 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1691 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1693 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1695 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1698 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1700 INTERCEPTOR(int, printf, const char *format, ...)
1701 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1703 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1704 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1706 #if SANITIZER_INTERCEPT___PRINTF_CHK
1707 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1708 const char *format, ...)
1709 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1712 INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
1713 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)
1715 #if SANITIZER_INTERCEPT___PRINTF_CHK
1716 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1717 const char *format, ...)
1718 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
1721 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1722 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1724 #if SANITIZER_INTERCEPT___PRINTF_CHK
1725 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1726 SIZE_T size_to, const char *format, ...)
1727 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
1730 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1731 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1733 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1734 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1735 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1737 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1739 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1741 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1742 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1744 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1745 const char *format, ...)
1746 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1749 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1751 #endif // SANITIZER_INTERCEPT_PRINTF
1753 #if SANITIZER_INTERCEPT_PRINTF
1754 #define INIT_PRINTF \
1755 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1756 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1757 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1758 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1759 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1760 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1761 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1762 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1763 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1764 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1769 #if SANITIZER_INTERCEPT___PRINTF_CHK
1770 #define INIT___PRINTF_CHK \
1771 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1772 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1773 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1774 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1775 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1777 #define INIT___PRINTF_CHK
1780 #if SANITIZER_INTERCEPT_PRINTF_L
1781 #define INIT_PRINTF_L \
1782 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1783 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1785 #define INIT_PRINTF_L
1788 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1789 #define INIT_ISOC99_PRINTF \
1790 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1791 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1792 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1793 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1794 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1795 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1796 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1797 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1799 #define INIT_ISOC99_PRINTF
1802 #if SANITIZER_INTERCEPT_IOCTL
1803 #include "sanitizer_common_interceptors_ioctl.inc"
1804 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1805 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1806 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1807 // can trigger a report and we need to be able to unwind through this
1808 // function. On Mac in debug mode we might not have a frame pointer, because
1809 // ioctl_common_[pre|post] doesn't get inlined here.
1810 ENABLE_FRAME_POINTER;
1814 va_start(ap, request);
1815 void *arg = va_arg(ap, void *);
1817 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1819 CHECK(ioctl_initialized);
1821 // Note: TSan does not use common flags, and they are zero-initialized.
1822 // This effectively disables ioctl handling in TSan.
1823 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1825 // Although request is unsigned long, the rest of the interceptor uses it
1826 // as just "unsigned" to save space, because we know that all values fit in
1827 // "unsigned" - they are compile-time constants.
1829 const ioctl_desc *desc = ioctl_lookup(request);
1830 ioctl_desc decoded_desc;
1832 VPrintf(2, "Decoding unknown ioctl 0x%lx\n", request);
1833 if (!ioctl_decode(request, &decoded_desc))
1834 Printf("WARNING: failed decoding unknown ioctl 0x%lx\n", request);
1836 desc = &decoded_desc;
1839 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1840 int res = REAL(ioctl)(d, request, arg);
1841 // FIXME: some ioctls have different return values for success and failure.
1842 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1845 #define INIT_IOCTL \
1847 COMMON_INTERCEPT_FUNCTION(ioctl);
1853 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1855 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1858 internal_strlen(pwd->pw_name) + 1);
1860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1861 internal_strlen(pwd->pw_passwd) + 1);
1862 #if !SANITIZER_ANDROID
1864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1865 internal_strlen(pwd->pw_gecos) + 1);
1867 #if SANITIZER_APPLE || SANITIZER_FREEBSD || SANITIZER_NETBSD
1869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1870 internal_strlen(pwd->pw_class) + 1);
1873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1874 internal_strlen(pwd->pw_dir) + 1);
1876 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1877 internal_strlen(pwd->pw_shell) + 1);
1881 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1883 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1886 internal_strlen(grp->gr_name) + 1);
1888 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1889 internal_strlen(grp->gr_passwd) + 1);
1890 char **p = grp->gr_mem;
1892 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
1894 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1895 (p - grp->gr_mem + 1) * sizeof(*p));
1898 #endif // SANITIZER_POSIX
1900 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1901 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1903 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1905 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1906 __sanitizer_passwd *res = REAL(getpwnam)(name);
1907 unpoison_passwd(ctx, res);
1910 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1912 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1913 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1914 unpoison_passwd(ctx, res);
1917 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1919 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1920 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1921 __sanitizer_group *res = REAL(getgrnam)(name);
1922 unpoison_group(ctx, res);
1925 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1927 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1928 __sanitizer_group *res = REAL(getgrgid)(gid);
1929 unpoison_group(ctx, res);
1932 #define INIT_GETPWNAM_AND_FRIENDS \
1933 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1934 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1935 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1936 COMMON_INTERCEPT_FUNCTION(getgrgid);
1938 #define INIT_GETPWNAM_AND_FRIENDS
1941 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1942 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1943 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1945 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1946 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1947 // FIXME: under ASan the call below may write to freed memory and corrupt
1948 // its metadata. See
1949 // https://github.com/google/sanitizers/issues/321.
1950 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1952 unpoison_passwd(ctx, *result);
1953 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1956 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1957 SIZE_T buflen, __sanitizer_passwd **result) {
1959 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1960 // FIXME: under ASan the call below may write to freed memory and corrupt
1961 // its metadata. See
1962 // https://github.com/google/sanitizers/issues/321.
1963 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1965 unpoison_passwd(ctx, *result);
1966 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1969 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1970 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1972 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1973 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1974 // FIXME: under ASan the call below may write to freed memory and corrupt
1975 // its metadata. See
1976 // https://github.com/google/sanitizers/issues/321.
1977 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1979 unpoison_group(ctx, *result);
1980 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1983 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1984 SIZE_T buflen, __sanitizer_group **result) {
1986 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1987 // FIXME: under ASan the call below may write to freed memory and corrupt
1988 // its metadata. See
1989 // https://github.com/google/sanitizers/issues/321.
1990 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1992 unpoison_group(ctx, *result);
1993 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1996 #define INIT_GETPWNAM_R_AND_FRIENDS \
1997 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1998 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1999 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
2000 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
2002 #define INIT_GETPWNAM_R_AND_FRIENDS
2005 #if SANITIZER_INTERCEPT_GETPWENT
2006 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
2008 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
2009 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2010 unpoison_passwd(ctx, res);
2013 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2015 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2016 __sanitizer_group *res = REAL(getgrent)(dummy);
2017 unpoison_group(ctx, res);
2020 #define INIT_GETPWENT \
2021 COMMON_INTERCEPT_FUNCTION(getpwent); \
2022 COMMON_INTERCEPT_FUNCTION(getgrent);
2024 #define INIT_GETPWENT
2027 #if SANITIZER_INTERCEPT_FGETPWENT
2028 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2030 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2031 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2032 unpoison_passwd(ctx, res);
2035 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2037 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2038 __sanitizer_group *res = REAL(fgetgrent)(fp);
2039 unpoison_group(ctx, res);
2042 #define INIT_FGETPWENT \
2043 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2044 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2046 #define INIT_FGETPWENT
2049 #if SANITIZER_INTERCEPT_GETPWENT_R
2050 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2051 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2053 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2054 // FIXME: under ASan the call below may write to freed memory and corrupt
2055 // its metadata. See
2056 // https://github.com/google/sanitizers/issues/321.
2057 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2059 unpoison_passwd(ctx, *pwbufp);
2060 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2063 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2064 __sanitizer_group **pwbufp) {
2066 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2067 // FIXME: under ASan the call below may write to freed memory and corrupt
2068 // its metadata. See
2069 // https://github.com/google/sanitizers/issues/321.
2070 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2072 unpoison_group(ctx, *pwbufp);
2073 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2076 #define INIT_GETPWENT_R \
2077 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2078 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2080 #define INIT_GETPWENT_R
2083 #if SANITIZER_INTERCEPT_FGETPWENT_R
2084 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2085 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2087 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2088 // FIXME: under ASan the call below may write to freed memory and corrupt
2089 // its metadata. See
2090 // https://github.com/google/sanitizers/issues/321.
2091 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2093 unpoison_passwd(ctx, *pwbufp);
2094 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2097 #define INIT_FGETPWENT_R \
2098 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2100 #define INIT_FGETPWENT_R
2103 #if SANITIZER_INTERCEPT_FGETGRENT_R
2104 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2105 SIZE_T buflen, __sanitizer_group **pwbufp) {
2107 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2108 // FIXME: under ASan the call below may write to freed memory and corrupt
2109 // its metadata. See
2110 // https://github.com/google/sanitizers/issues/321.
2111 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2113 unpoison_group(ctx, *pwbufp);
2114 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2117 #define INIT_FGETGRENT_R \
2118 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2120 #define INIT_FGETGRENT_R
2123 #if SANITIZER_INTERCEPT_SETPWENT
2124 // The only thing these interceptors do is disable any nested interceptors.
2125 // These functions may open nss modules and call uninstrumented functions from
2126 // them, and we don't want things like strlen() to trigger.
2127 INTERCEPTOR(void, setpwent, int dummy) {
2129 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2130 REAL(setpwent)(dummy);
2132 INTERCEPTOR(void, endpwent, int dummy) {
2134 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2135 REAL(endpwent)(dummy);
2137 INTERCEPTOR(void, setgrent, int dummy) {
2139 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2140 REAL(setgrent)(dummy);
2142 INTERCEPTOR(void, endgrent, int dummy) {
2144 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2145 REAL(endgrent)(dummy);
2147 #define INIT_SETPWENT \
2148 COMMON_INTERCEPT_FUNCTION(setpwent); \
2149 COMMON_INTERCEPT_FUNCTION(endpwent); \
2150 COMMON_INTERCEPT_FUNCTION(setgrent); \
2151 COMMON_INTERCEPT_FUNCTION(endgrent);
2153 #define INIT_SETPWENT
2156 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2157 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2159 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2160 // FIXME: under ASan the call below may write to freed memory and corrupt
2161 // its metadata. See
2162 // https://github.com/google/sanitizers/issues/321.
2163 int res = REAL(clock_getres)(clk_id, tp);
2165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2169 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2171 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2172 // FIXME: under ASan the call below may write to freed memory and corrupt
2173 // its metadata. See
2174 // https://github.com/google/sanitizers/issues/321.
2175 int res = REAL(clock_gettime)(clk_id, tp);
2177 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2182 namespace __sanitizer {
2184 int real_clock_gettime(u32 clk_id, void *tp) {
2185 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2186 return internal_clock_gettime(clk_id, tp);
2187 return REAL(clock_gettime)(clk_id, tp);
2190 } // namespace __sanitizer
2192 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2194 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2195 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2196 return REAL(clock_settime)(clk_id, tp);
2198 #define INIT_CLOCK_GETTIME \
2199 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2200 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2201 COMMON_INTERCEPT_FUNCTION(clock_settime);
2203 #define INIT_CLOCK_GETTIME
2206 #if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
2207 INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
2208 __sanitizer_clockid_t *clockid) {
2210 COMMON_INTERCEPTOR_ENTER(ctx, clock_getcpuclockid, pid, clockid);
2211 int res = REAL(clock_getcpuclockid)(pid, clockid);
2212 if (!res && clockid) {
2213 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2218 INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
2219 __sanitizer_clockid_t *clockid) {
2221 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
2222 int res = REAL(pthread_getcpuclockid)(thread, clockid);
2223 if (!res && clockid) {
2224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2229 #define INIT_CLOCK_GETCPUCLOCKID \
2230 COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid); \
2231 COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
2233 #define INIT_CLOCK_GETCPUCLOCKID
2236 #if SANITIZER_INTERCEPT_GETITIMER
2237 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2239 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2240 // FIXME: under ASan the call below may write to freed memory and corrupt
2241 // its metadata. See
2242 // https://github.com/google/sanitizers/issues/321.
2243 int res = REAL(getitimer)(which, curr_value);
2244 if (!res && curr_value) {
2245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2249 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2251 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2253 // itimerval can contain padding that may be legitimately uninitialized
2254 const struct __sanitizer_itimerval *nv =
2255 (const struct __sanitizer_itimerval *)new_value;
2256 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2257 sizeof(__sanitizer_time_t));
2258 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2259 sizeof(__sanitizer_suseconds_t));
2260 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2261 sizeof(__sanitizer_time_t));
2262 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2263 sizeof(__sanitizer_suseconds_t));
2265 // FIXME: under ASan the call below may write to freed memory and corrupt
2266 // its metadata. See
2267 // https://github.com/google/sanitizers/issues/321.
2268 int res = REAL(setitimer)(which, new_value, old_value);
2269 if (!res && old_value) {
2270 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2274 #define INIT_GETITIMER \
2275 COMMON_INTERCEPT_FUNCTION(getitimer); \
2276 COMMON_INTERCEPT_FUNCTION(setitimer);
2278 #define INIT_GETITIMER
2281 #if SANITIZER_INTERCEPT_GLOB
2282 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2283 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2284 // +1 for NULL pointer at the end.
2285 if (pglob->gl_pathv)
2286 COMMON_INTERCEPTOR_WRITE_RANGE(
2287 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2288 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2289 char *p = pglob->gl_pathv[i];
2290 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, internal_strlen(p) + 1);
2294 #if SANITIZER_SOLARIS
2295 INTERCEPTOR(int, glob, const char *pattern, int flags,
2296 int (*errfunc)(const char *epath, int eerrno),
2297 __sanitizer_glob_t *pglob) {
2299 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2300 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2301 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2302 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2306 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2308 static void wrapped_gl_closedir(void *dir) {
2309 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2310 pglob_copy->gl_closedir(dir);
2313 static void *wrapped_gl_readdir(void *dir) {
2314 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2315 return pglob_copy->gl_readdir(dir);
2318 static void *wrapped_gl_opendir(const char *s) {
2319 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2320 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2321 return pglob_copy->gl_opendir(s);
2324 static int wrapped_gl_lstat(const char *s, void *st) {
2325 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2326 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2327 return pglob_copy->gl_lstat(s, st);
2330 static int wrapped_gl_stat(const char *s, void *st) {
2331 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2332 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2333 return pglob_copy->gl_stat(s, st);
2336 static const __sanitizer_glob_t kGlobCopy = {
2338 0, wrapped_gl_closedir, wrapped_gl_readdir,
2339 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2341 INTERCEPTOR(int, glob, const char *pattern, int flags,
2342 int (*errfunc)(const char *epath, int eerrno),
2343 __sanitizer_glob_t *pglob) {
2345 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2346 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2347 __sanitizer_glob_t glob_copy;
2348 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2349 if (flags & glob_altdirfunc) {
2350 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2351 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2352 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2353 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2354 Swap(pglob->gl_stat, glob_copy.gl_stat);
2355 pglob_copy = &glob_copy;
2357 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2358 if (flags & glob_altdirfunc) {
2359 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2360 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2361 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2362 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2363 Swap(pglob->gl_stat, glob_copy.gl_stat);
2366 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2369 #endif // SANITIZER_SOLARIS
2371 COMMON_INTERCEPT_FUNCTION(glob);
2372 #else // SANITIZER_INTERCEPT_GLOB
2374 #endif // SANITIZER_INTERCEPT_GLOB
2376 #if SANITIZER_INTERCEPT_GLOB64
2377 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2378 int (*errfunc)(const char *epath, int eerrno),
2379 __sanitizer_glob_t *pglob) {
2381 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2382 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2383 __sanitizer_glob_t glob_copy;
2384 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2385 if (flags & glob_altdirfunc) {
2386 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2387 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2388 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2389 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2390 Swap(pglob->gl_stat, glob_copy.gl_stat);
2391 pglob_copy = &glob_copy;
2393 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2394 if (flags & glob_altdirfunc) {
2395 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2396 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2397 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2398 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2399 Swap(pglob->gl_stat, glob_copy.gl_stat);
2402 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2405 #define INIT_GLOB64 \
2406 COMMON_INTERCEPT_FUNCTION(glob64);
2407 #else // SANITIZER_INTERCEPT_GLOB64
2409 #endif // SANITIZER_INTERCEPT_GLOB64
2411 #if SANITIZER_INTERCEPT___B64_TO
2412 INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
2413 char *target, SIZE_T targsize) {
2415 COMMON_INTERCEPTOR_ENTER(ctx, __b64_ntop, src, srclength, target, targsize);
2416 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclength);
2417 int res = REAL(__b64_ntop)(src, srclength, target, targsize);
2419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res + 1);
2422 INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
2424 COMMON_INTERCEPTOR_ENTER(ctx, __b64_pton, src, target, targsize);
2425 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
2426 int res = REAL(__b64_pton)(src, target, targsize);
2428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res);
2431 #define INIT___B64_TO \
2432 COMMON_INTERCEPT_FUNCTION(__b64_ntop); \
2433 COMMON_INTERCEPT_FUNCTION(__b64_pton);
2434 #else // SANITIZER_INTERCEPT___B64_TO
2435 #define INIT___B64_TO
2436 #endif // SANITIZER_INTERCEPT___B64_TO
2438 #if SANITIZER_INTERCEPT_DN_COMP_EXPAND
2439 # if __GLIBC_PREREQ(2, 34)
2440 // Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
2441 # define DN_COMP_INTERCEPTOR_NAME dn_comp
2442 # define DN_EXPAND_INTERCEPTOR_NAME dn_expand
2444 # define DN_COMP_INTERCEPTOR_NAME __dn_comp
2445 # define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
2447 INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
2448 unsigned char *comp_dn, int length, unsigned char **dnptrs,
2449 unsigned char **lastdnptr) {
2451 COMMON_INTERCEPTOR_ENTER(ctx, DN_COMP_INTERCEPTOR_NAME, exp_dn, comp_dn,
2452 length, dnptrs, lastdnptr);
2453 int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
2456 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, comp_dn, res);
2457 if (dnptrs && lastdnptr) {
2458 unsigned char **p = dnptrs;
2459 for (; p != lastdnptr && *p; ++p)
2463 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dnptrs, (p - dnptrs) * sizeof(*p));
2468 INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
2469 unsigned char const *end, unsigned char const *src, char *dest,
2472 COMMON_INTERCEPTOR_ENTER(ctx, DN_EXPAND_INTERCEPTOR_NAME, base, end, src,
2474 // TODO: add read check if __dn_comp intercept added
2475 int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
2477 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, internal_strlen(dest) + 1);
2480 # define INIT_DN_COMP_EXPAND \
2481 COMMON_INTERCEPT_FUNCTION(DN_COMP_INTERCEPTOR_NAME); \
2482 COMMON_INTERCEPT_FUNCTION(DN_EXPAND_INTERCEPTOR_NAME);
2483 #else // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2484 # define INIT_DN_COMP_EXPAND
2485 #endif // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2487 #if SANITIZER_INTERCEPT_POSIX_SPAWN
2489 template <class RealSpawnPtr>
2490 static int PosixSpawnImpl(void *ctx, RealSpawnPtr *real_posix_spawn, pid_t *pid,
2491 const char *file_or_path, const void *file_actions,
2492 const void *attrp, char *const argv[],
2493 char *const envp[]) {
2494 COMMON_INTERCEPTOR_READ_RANGE(ctx, file_or_path,
2495 internal_strlen(file_or_path) + 1);
2497 for (char *const *s = argv; ; ++s) {
2498 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2500 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2504 for (char *const *s = envp; ; ++s) {
2505 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2507 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2511 real_posix_spawn(pid, file_or_path, file_actions, attrp, argv, envp);
2513 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pid, sizeof(*pid));
2516 INTERCEPTOR(int, posix_spawn, pid_t *pid, const char *path,
2517 const void *file_actions, const void *attrp, char *const argv[],
2518 char *const envp[]) {
2520 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawn, pid, path, file_actions, attrp,
2522 return PosixSpawnImpl(ctx, REAL(posix_spawn), pid, path, file_actions, attrp,
2525 INTERCEPTOR(int, posix_spawnp, pid_t *pid, const char *file,
2526 const void *file_actions, const void *attrp, char *const argv[],
2527 char *const envp[]) {
2529 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawnp, pid, file, file_actions, attrp,
2531 return PosixSpawnImpl(ctx, REAL(posix_spawnp), pid, file, file_actions, attrp,
2534 # define INIT_POSIX_SPAWN \
2535 COMMON_INTERCEPT_FUNCTION(posix_spawn); \
2536 COMMON_INTERCEPT_FUNCTION(posix_spawnp);
2537 #else // SANITIZER_INTERCEPT_POSIX_SPAWN
2538 # define INIT_POSIX_SPAWN
2539 #endif // SANITIZER_INTERCEPT_POSIX_SPAWN
2541 #if SANITIZER_INTERCEPT_WAIT
2542 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2543 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2545 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2547 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2548 // FIXME: under ASan the call below may write to freed memory and corrupt
2549 // its metadata. See
2550 // https://github.com/google/sanitizers/issues/321.
2551 int res = REAL(wait)(status);
2552 if (res != -1 && status)
2553 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2556 // On FreeBSD id_t is always 64-bit wide.
2557 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2558 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2561 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2565 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2566 // FIXME: under ASan the call below may write to freed memory and corrupt
2567 // its metadata. See
2568 // https://github.com/google/sanitizers/issues/321.
2569 int res = REAL(waitid)(idtype, id, infop, options);
2570 if (res != -1 && infop)
2571 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2574 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2576 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2577 // FIXME: under ASan the call below may write to freed memory and corrupt
2578 // its metadata. See
2579 // https://github.com/google/sanitizers/issues/321.
2580 int res = REAL(waitpid)(pid, status, options);
2581 if (res != -1 && status)
2582 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2585 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2587 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2588 // FIXME: under ASan the call below may write to freed memory and corrupt
2589 // its metadata. See
2590 // https://github.com/google/sanitizers/issues/321.
2591 int res = REAL(wait3)(status, options, rusage);
2593 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2594 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2598 #if SANITIZER_ANDROID
2599 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2601 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2602 // FIXME: under ASan the call below may write to freed memory and corrupt
2603 // its metadata. See
2604 // https://github.com/google/sanitizers/issues/321.
2605 int res = REAL(__wait4)(pid, status, options, rusage);
2607 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2608 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2612 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2614 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2616 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2617 // FIXME: under ASan the call below may write to freed memory and corrupt
2618 // its metadata. See
2619 // https://github.com/google/sanitizers/issues/321.
2620 int res = REAL(wait4)(pid, status, options, rusage);
2622 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2623 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2627 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2628 #endif // SANITIZER_ANDROID
2630 COMMON_INTERCEPT_FUNCTION(wait); \
2631 COMMON_INTERCEPT_FUNCTION(waitid); \
2632 COMMON_INTERCEPT_FUNCTION(waitpid); \
2633 COMMON_INTERCEPT_FUNCTION(wait3);
2639 #if SANITIZER_INTERCEPT_INET
2640 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2642 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2643 uptr sz = __sanitizer_in_addr_sz(af);
2644 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2645 // FIXME: figure out read size based on the address family.
2646 // FIXME: under ASan the call below may write to freed memory and corrupt
2647 // its metadata. See
2648 // https://github.com/google/sanitizers/issues/321.
2649 char *res = REAL(inet_ntop)(af, src, dst, size);
2650 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
2653 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2655 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2656 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2657 // FIXME: figure out read size based on the address family.
2658 // FIXME: under ASan the call below may write to freed memory and corrupt
2659 // its metadata. See
2660 // https://github.com/google/sanitizers/issues/321.
2661 int res = REAL(inet_pton)(af, src, dst);
2663 uptr sz = __sanitizer_in_addr_sz(af);
2664 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2669 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2670 COMMON_INTERCEPT_FUNCTION(inet_pton);
2675 #if SANITIZER_INTERCEPT_INET
2676 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2678 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2679 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, internal_strlen(cp) + 1);
2680 // FIXME: under ASan the call below may write to freed memory and corrupt
2681 // its metadata. See
2682 // https://github.com/google/sanitizers/issues/321.
2683 int res = REAL(inet_aton)(cp, dst);
2685 uptr sz = __sanitizer_in_addr_sz(af_inet);
2686 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2690 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2692 #define INIT_INET_ATON
2695 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2696 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2698 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2699 // FIXME: under ASan the call below may write to freed memory and corrupt
2700 // its metadata. See
2701 // https://github.com/google/sanitizers/issues/321.
2702 int res = REAL(pthread_getschedparam)(thread, policy, param);
2704 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2705 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2709 #define INIT_PTHREAD_GETSCHEDPARAM \
2710 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2712 #define INIT_PTHREAD_GETSCHEDPARAM
2715 #if SANITIZER_INTERCEPT_GETADDRINFO
2716 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2717 struct __sanitizer_addrinfo *hints,
2718 struct __sanitizer_addrinfo **out) {
2720 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2721 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, internal_strlen(node) + 1);
2723 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, internal_strlen(service) + 1);
2725 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2726 // FIXME: under ASan the call below may write to freed memory and corrupt
2727 // its metadata. See
2728 // https://github.com/google/sanitizers/issues/321.
2729 int res = REAL(getaddrinfo)(node, service, hints, out);
2730 if (res == 0 && out) {
2731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2732 struct __sanitizer_addrinfo *p = *out;
2734 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2736 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2737 if (p->ai_canonname)
2738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2739 internal_strlen(p->ai_canonname) + 1);
2745 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2747 #define INIT_GETADDRINFO
2750 #if SANITIZER_INTERCEPT_GETNAMEINFO
2751 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2752 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2754 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2755 serv, servlen, flags);
2756 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2757 // There is padding in in_addr that may make this too noisy
2758 // FIXME: under ASan the call below may write to freed memory and corrupt
2759 // its metadata. See
2760 // https://github.com/google/sanitizers/issues/321.
2762 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2764 if (host && hostlen)
2765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, internal_strlen(host) + 1);
2766 if (serv && servlen)
2767 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, internal_strlen(serv) + 1);
2771 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2773 #define INIT_GETNAMEINFO
2776 #if SANITIZER_INTERCEPT_GETSOCKNAME
2777 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {
2779 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2782 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2785 // FIXME: under ASan the call below may write to freed memory and corrupt
2786 // its metadata. See
2787 // https://github.com/google/sanitizers/issues/321.
2788 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2789 if (!res && addr && addrlen) {
2790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2794 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2796 #define INIT_GETSOCKNAME
2799 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2800 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2801 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, internal_strlen(h->h_name) + 1);
2804 char **p = h->h_aliases;
2806 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
2809 COMMON_INTERCEPTOR_WRITE_RANGE(
2810 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2813 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2816 COMMON_INTERCEPTOR_WRITE_RANGE(
2817 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2821 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2822 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2824 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2825 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2826 if (res) write_hostent(ctx, res);
2830 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2833 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2834 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2835 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2836 if (res) write_hostent(ctx, res);
2840 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2842 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2843 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2844 if (res) write_hostent(ctx, res);
2847 #define INIT_GETHOSTBYNAME \
2848 COMMON_INTERCEPT_FUNCTION(gethostent); \
2849 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2850 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2852 #define INIT_GETHOSTBYNAME
2853 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2855 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2856 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2858 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2859 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2860 if (res) write_hostent(ctx, res);
2863 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2865 #define INIT_GETHOSTBYNAME2
2866 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2868 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2869 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2870 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2873 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2875 // FIXME: under ASan the call below may write to freed memory and corrupt
2876 // its metadata. See
2877 // https://github.com/google/sanitizers/issues/321.
2878 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2880 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2881 if (res == 0 && *result) write_hostent(ctx, *result);
2884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2887 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2889 #define INIT_GETHOSTBYNAME_R
2892 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2893 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2894 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2896 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2898 // FIXME: under ASan the call below may write to freed memory and corrupt
2899 // its metadata. See
2900 // https://github.com/google/sanitizers/issues/321.
2901 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2904 if (res == 0 && *result) write_hostent(ctx, *result);
2907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2910 #define INIT_GETHOSTENT_R \
2911 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2913 #define INIT_GETHOSTENT_R
2916 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2917 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2918 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2919 __sanitizer_hostent **result, int *h_errnop) {
2921 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2922 buflen, result, h_errnop);
2923 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2924 // FIXME: under ASan the call below may write to freed memory and corrupt
2925 // its metadata. See
2926 // https://github.com/google/sanitizers/issues/321.
2927 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2930 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2931 if (res == 0 && *result) write_hostent(ctx, *result);
2934 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2937 #define INIT_GETHOSTBYADDR_R \
2938 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2940 #define INIT_GETHOSTBYADDR_R
2943 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2944 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2945 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2946 __sanitizer_hostent **result, int *h_errnop) {
2948 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2950 // FIXME: under ASan the call below may write to freed memory and corrupt
2951 // its metadata. See
2952 // https://github.com/google/sanitizers/issues/321.
2954 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2956 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2957 if (res == 0 && *result) write_hostent(ctx, *result);
2960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2963 #define INIT_GETHOSTBYNAME2_R \
2964 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2966 #define INIT_GETHOSTBYNAME2_R
2969 #if SANITIZER_INTERCEPT_GETSOCKOPT
2970 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2973 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2975 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2976 // FIXME: under ASan the call below may write to freed memory and corrupt
2977 // its metadata. See
2978 // https://github.com/google/sanitizers/issues/321.
2979 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2981 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2984 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2986 #define INIT_GETSOCKOPT
2989 #if SANITIZER_INTERCEPT_ACCEPT
2990 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2992 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2993 unsigned addrlen0 = 0;
2995 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2996 addrlen0 = *addrlen;
2998 int fd2 = REAL(accept)(fd, addr, addrlen);
3000 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3001 if (addr && addrlen)
3002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3006 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
3011 #if SANITIZER_INTERCEPT_ACCEPT4
3012 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
3014 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
3015 unsigned addrlen0 = 0;
3017 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3018 addrlen0 = *addrlen;
3020 // FIXME: under ASan the call below may write to freed memory and corrupt
3021 // its metadata. See
3022 // https://github.com/google/sanitizers/issues/321.
3023 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
3025 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3026 if (addr && addrlen)
3027 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3031 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
3033 #define INIT_ACCEPT4
3036 #if SANITIZER_INTERCEPT_PACCEPT
3037 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
3038 __sanitizer_sigset_t *set, int f) {
3040 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
3041 unsigned addrlen0 = 0;
3043 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3044 addrlen0 = *addrlen;
3046 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3047 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
3049 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3050 if (addr && addrlen)
3051 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3055 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
3057 #define INIT_PACCEPT
3060 #if SANITIZER_INTERCEPT_MODF
3061 INTERCEPTOR(double, modf, double x, double *iptr) {
3063 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
3064 // FIXME: under ASan the call below may write to freed memory and corrupt
3065 // its metadata. See
3066 // https://github.com/google/sanitizers/issues/321.
3067 double res = REAL(modf)(x, iptr);
3069 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3073 INTERCEPTOR(float, modff, float x, float *iptr) {
3075 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
3076 // FIXME: under ASan the call below may write to freed memory and corrupt
3077 // its metadata. See
3078 // https://github.com/google/sanitizers/issues/321.
3079 float res = REAL(modff)(x, iptr);
3081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3085 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
3087 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
3088 // FIXME: under ASan the call below may write to freed memory and corrupt
3089 // its metadata. See
3090 // https://github.com/google/sanitizers/issues/321.
3091 long double res = REAL(modfl)(x, iptr);
3093 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3098 COMMON_INTERCEPT_FUNCTION(modf); \
3099 COMMON_INTERCEPT_FUNCTION(modff); \
3100 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
3105 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
3106 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3108 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
3109 if (msg->msg_name && msg->msg_namelen)
3110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3111 if (msg->msg_iov && msg->msg_iovlen)
3112 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
3113 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3114 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3115 if (msg->msg_control && msg->msg_controllen)
3116 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
3120 #if SANITIZER_INTERCEPT_RECVMSG
3121 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
3124 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
3125 // FIXME: under ASan the call below may write to freed memory and corrupt
3126 // its metadata. See
3127 // https://github.com/google/sanitizers/issues/321.
3128 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
3130 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3132 write_msghdr(ctx, msg, res);
3133 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
3138 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
3140 #define INIT_RECVMSG
3143 #if SANITIZER_INTERCEPT_RECVMMSG
3144 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3145 unsigned int vlen, int flags, void *timeout) {
3147 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
3148 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3149 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
3151 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3152 for (int i = 0; i < res; ++i) {
3153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3154 sizeof(msgvec[i].msg_len));
3155 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3156 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
3161 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
3163 #define INIT_RECVMMSG
3166 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
3167 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
3168 const unsigned kCmsgDataOffset =
3169 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
3171 char *p = (char *)control;
3172 char *const control_end = p + controllen;
3174 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3175 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3176 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3178 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3180 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3181 sizeof(cmsg->cmsg_level));
3182 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3183 sizeof(cmsg->cmsg_type));
3185 if (cmsg->cmsg_len > kCmsgDataOffset) {
3186 char *data = p + kCmsgDataOffset;
3187 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3188 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3191 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3195 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3198 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3207 if (msg->msg_name && msg->msg_namelen)
3208 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3209 if (msg->msg_iov && msg->msg_iovlen)
3210 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3211 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3212 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3213 if (msg->msg_control && msg->msg_controllen)
3214 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3218 #if SANITIZER_INTERCEPT_SENDMSG
3219 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3222 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3224 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3225 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3227 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3228 if (common_flags()->intercept_send && res >= 0 && msg)
3229 read_msghdr(ctx, msg, res);
3232 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3234 #define INIT_SENDMSG
3237 #if SANITIZER_INTERCEPT_SENDMMSG
3238 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3239 unsigned vlen, int flags) {
3241 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3243 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3244 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3246 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3247 if (res >= 0 && msgvec) {
3248 for (int i = 0; i < res; ++i) {
3249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3250 sizeof(msgvec[i].msg_len));
3251 if (common_flags()->intercept_send)
3252 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3257 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3259 #define INIT_SENDMMSG
3262 #if SANITIZER_INTERCEPT_SYSMSG
3263 INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
3266 COMMON_INTERCEPTOR_ENTER(ctx, msgsnd, msqid, msgp, msgsz, msgflg);
3268 COMMON_INTERCEPTOR_READ_RANGE(ctx, msgp, sizeof(long) + msgsz);
3269 int res = REAL(msgsnd)(msqid, msgp, msgsz, msgflg);
3273 INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
3274 long msgtyp, int msgflg) {
3276 COMMON_INTERCEPTOR_ENTER(ctx, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
3277 SSIZE_T len = REAL(msgrcv)(msqid, msgp, msgsz, msgtyp, msgflg);
3279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msgp, sizeof(long) + len);
3283 #define INIT_SYSMSG \
3284 COMMON_INTERCEPT_FUNCTION(msgsnd); \
3285 COMMON_INTERCEPT_FUNCTION(msgrcv);
3290 #if SANITIZER_INTERCEPT_GETPEERNAME
3291 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3293 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3296 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3299 // FIXME: under ASan the call below may write to freed memory and corrupt
3300 // its metadata. See
3301 // https://github.com/google/sanitizers/issues/321.
3302 int res = REAL(getpeername)(sockfd, addr, addrlen);
3303 if (!res && addr && addrlen) {
3304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3308 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3310 #define INIT_GETPEERNAME
3313 #if SANITIZER_INTERCEPT_SYSINFO
3314 INTERCEPTOR(int, sysinfo, void *info) {
3316 // FIXME: under ASan the call below may write to freed memory and corrupt
3317 // its metadata. See
3318 // https://github.com/google/sanitizers/issues/321.
3319 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3320 int res = REAL(sysinfo)(info);
3322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3325 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3327 #define INIT_SYSINFO
3330 #if SANITIZER_INTERCEPT_READDIR
3331 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3333 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3334 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3335 __sanitizer_dirent *res = REAL(opendir)(path);
3337 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3341 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3343 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3344 // FIXME: under ASan the call below may write to freed memory and corrupt
3345 // its metadata. See
3346 // https://github.com/google/sanitizers/issues/321.
3347 __sanitizer_dirent *res = REAL(readdir)(dirp);
3349 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3353 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3354 __sanitizer_dirent **result) {
3356 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3357 // FIXME: under ASan the call below may write to freed memory and corrupt
3358 // its metadata. See
3359 // https://github.com/google/sanitizers/issues/321.
3360 int res = REAL(readdir_r)(dirp, entry, result);
3362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3364 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3369 #define INIT_READDIR \
3370 COMMON_INTERCEPT_FUNCTION(opendir); \
3371 COMMON_INTERCEPT_FUNCTION(readdir); \
3372 COMMON_INTERCEPT_FUNCTION(readdir_r);
3374 #define INIT_READDIR
3377 #if SANITIZER_INTERCEPT_READDIR64
3378 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3380 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3381 // FIXME: under ASan the call below may write to freed memory and corrupt
3382 // its metadata. See
3383 // https://github.com/google/sanitizers/issues/321.
3384 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3386 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3390 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3391 __sanitizer_dirent64 **result) {
3393 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3394 // FIXME: under ASan the call below may write to freed memory and corrupt
3395 // its metadata. See
3396 // https://github.com/google/sanitizers/issues/321.
3397 int res = REAL(readdir64_r)(dirp, entry, result);
3399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3405 #define INIT_READDIR64 \
3406 COMMON_INTERCEPT_FUNCTION(readdir64); \
3407 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3409 #define INIT_READDIR64
3412 #if SANITIZER_INTERCEPT_PTRACE
3413 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3415 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3416 __sanitizer_iovec local_iovec;
3419 if (request == ptrace_setregs) {
3420 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3421 } else if (request == ptrace_setfpregs) {
3422 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3423 } else if (request == ptrace_setfpxregs) {
3424 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3425 } else if (request == ptrace_setvfpregs) {
3426 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3427 } else if (request == ptrace_setsiginfo) {
3428 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3430 // Some kernel might zero the iovec::iov_base in case of invalid
3431 // write access. In this case copy the invalid address for further
3433 } else if (request == ptrace_setregset || request == ptrace_getregset) {
3434 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3435 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3436 local_iovec = *iovec;
3437 if (request == ptrace_setregset)
3438 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3442 // FIXME: under ASan the call below may write to freed memory and corrupt
3443 // its metadata. See
3444 // https://github.com/google/sanitizers/issues/321.
3445 uptr res = REAL(ptrace)(request, pid, addr, data);
3448 // Note that PEEK* requests assign different meaning to the return value.
3449 // This function does not handle them (nor does it need to).
3450 if (request == ptrace_getregs) {
3451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3452 } else if (request == ptrace_getfpregs) {
3453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3454 } else if (request == ptrace_getfpxregs) {
3455 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3456 } else if (request == ptrace_getvfpregs) {
3457 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3458 } else if (request == ptrace_getsiginfo) {
3459 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3460 } else if (request == ptrace_geteventmsg) {
3461 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3462 } else if (request == ptrace_getregset) {
3463 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3465 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3466 local_iovec.iov_len);
3472 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3477 #if SANITIZER_INTERCEPT_SETLOCALE
3478 static void unpoison_ctype_arrays(void *ctx) {
3479 #if SANITIZER_NETBSD
3480 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3482 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3487 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3489 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3491 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, internal_strlen(locale) + 1);
3492 char *res = REAL(setlocale)(category, locale);
3494 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3495 unpoison_ctype_arrays(ctx);
3500 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3502 #define INIT_SETLOCALE
3505 #if SANITIZER_INTERCEPT_GETCWD
3506 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3508 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3509 // FIXME: under ASan the call below may write to freed memory and corrupt
3510 // its metadata. See
3511 // https://github.com/google/sanitizers/issues/321.
3512 char *res = REAL(getcwd)(buf, size);
3513 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3516 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3521 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3522 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3524 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3525 // FIXME: under ASan the call below may write to freed memory and corrupt
3526 // its metadata. See
3527 // https://github.com/google/sanitizers/issues/321.
3528 char *res = REAL(get_current_dir_name)(fake);
3529 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3533 #define INIT_GET_CURRENT_DIR_NAME \
3534 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3536 #define INIT_GET_CURRENT_DIR_NAME
3539 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3541 if (nptr == *endptr) {
3542 // No digits were found at strtol call, we need to find out the last
3543 // symbol accessed by strtoll on our own.
3544 // We get this symbol by skipping leading blanks and optional +/- sign.
3545 while (IsSpace(*nptr)) nptr++;
3546 if (*nptr == '+' || *nptr == '-') nptr++;
3547 *endptr = const_cast<char *>(nptr);
3549 CHECK(*endptr >= nptr);
3552 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3553 char **endptr, char *real_endptr, int base) {
3555 *endptr = real_endptr;
3556 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3558 // If base has unsupported value, strtol can exit with EINVAL
3559 // without reading any characters. So do additional checks only
3560 // if base is valid.
3561 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3562 if (is_valid_base) {
3563 FixRealStrtolEndptr(nptr, &real_endptr);
3565 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3566 (real_endptr - nptr) + 1 : 0);
3569 #if SANITIZER_INTERCEPT_STRTOIMAX
3570 template <typename Fn>
3571 static ALWAYS_INLINE auto StrtoimaxImpl(void *ctx, Fn real, const char *nptr,
3572 char **endptr, int base)
3573 -> decltype(real(nullptr, nullptr, 0)) {
3575 auto res = real(nptr, &real_endptr, base);
3576 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3580 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3582 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3583 return StrtoimaxImpl(ctx, REAL(strtoimax), nptr, endptr, base);
3585 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3587 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3588 return StrtoimaxImpl(ctx, REAL(strtoumax), nptr, endptr, base);
3591 #define INIT_STRTOIMAX \
3592 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3593 COMMON_INTERCEPT_FUNCTION(strtoumax);
3595 #define INIT_STRTOIMAX
3598 #if SANITIZER_INTERCEPT_STRTOIMAX && SANITIZER_GLIBC
3599 INTERCEPTOR(INTMAX_T, __isoc23_strtoimax, const char *nptr, char **endptr, int base) {
3601 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoimax, nptr, endptr, base);
3602 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoimax), nptr, endptr, base);
3604 INTERCEPTOR(UINTMAX_T, __isoc23_strtoumax, const char *nptr, char **endptr, int base) {
3606 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoumax, nptr, endptr, base);
3607 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoumax), nptr, endptr, base);
3610 # define INIT_STRTOIMAX_C23 \
3611 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoimax); \
3612 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoumax);
3614 # define INIT_STRTOIMAX_C23
3617 #if SANITIZER_INTERCEPT_MBSTOWCS
3618 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3620 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3621 // FIXME: under ASan the call below may write to freed memory and corrupt
3622 // its metadata. See
3623 // https://github.com/google/sanitizers/issues/321.
3624 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3625 if (res != (SIZE_T) - 1 && dest) {
3626 SIZE_T write_cnt = res + (res < len);
3627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3632 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3635 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3636 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3637 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3638 // FIXME: under ASan the call below may write to freed memory and corrupt
3639 // its metadata. See
3640 // https://github.com/google/sanitizers/issues/321.
3641 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3642 if (res != (SIZE_T)(-1) && dest && src) {
3643 // This function, and several others, may or may not write the terminating
3644 // \0 character. They write it iff they clear *src.
3645 SIZE_T write_cnt = res + !*src;
3646 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3651 #define INIT_MBSTOWCS \
3652 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3653 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3655 #define INIT_MBSTOWCS
3658 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3659 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3660 SIZE_T len, void *ps) {
3662 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3664 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3665 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3667 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3668 // FIXME: under ASan the call below may write to freed memory and corrupt
3669 // its metadata. See
3670 // https://github.com/google/sanitizers/issues/321.
3671 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3672 if (res != (SIZE_T)(-1) && dest && src) {
3673 SIZE_T write_cnt = res + !*src;
3674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3679 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3681 #define INIT_MBSNRTOWCS
3684 #if SANITIZER_INTERCEPT_WCSTOMBS
3685 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3687 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3688 // FIXME: under ASan the call below may write to freed memory and corrupt
3689 // its metadata. See
3690 // https://github.com/google/sanitizers/issues/321.
3691 SIZE_T res = REAL(wcstombs)(dest, src, len);
3692 if (res != (SIZE_T) - 1 && dest) {
3693 SIZE_T write_cnt = res + (res < len);
3694 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3699 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3702 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3703 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3704 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3705 // FIXME: under ASan the call below may write to freed memory and corrupt
3706 // its metadata. See
3707 // https://github.com/google/sanitizers/issues/321.
3708 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3709 if (res != (SIZE_T) - 1 && dest && src) {
3710 SIZE_T write_cnt = res + !*src;
3711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3716 #define INIT_WCSTOMBS \
3717 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3718 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3720 #define INIT_WCSTOMBS
3723 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3724 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3725 SIZE_T len, void *ps) {
3727 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3729 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3730 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3732 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3733 // FIXME: under ASan the call below may write to freed memory and corrupt
3734 // its metadata. See
3735 // https://github.com/google/sanitizers/issues/321.
3736 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3737 if (res != ((SIZE_T)-1) && dest && src) {
3738 SIZE_T write_cnt = res + !*src;
3739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3744 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3746 #define INIT_WCSNRTOMBS
3750 #if SANITIZER_INTERCEPT_WCRTOMB
3751 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3753 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3754 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3757 return REAL(wcrtomb)(dest, src, ps);
3759 char local_dest[32];
3760 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3761 if (res != ((SIZE_T)-1)) {
3762 CHECK_LE(res, sizeof(local_dest));
3763 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3764 REAL(memcpy)(dest, local_dest, res);
3769 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3771 #define INIT_WCRTOMB
3774 #if SANITIZER_INTERCEPT_WCTOMB
3775 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3777 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3779 return REAL(wctomb)(dest, src);
3781 char local_dest[32];
3782 int res = REAL(wctomb)(local_dest, src);
3784 CHECK_LE(res, sizeof(local_dest));
3785 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3786 REAL(memcpy)(dest, local_dest, res);
3791 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3796 #if SANITIZER_INTERCEPT_TCGETATTR
3797 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3799 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3800 // FIXME: under ASan the call below may write to freed memory and corrupt
3801 // its metadata. See
3802 // https://github.com/google/sanitizers/issues/321.
3803 int res = REAL(tcgetattr)(fd, termios_p);
3804 if (!res && termios_p)
3805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3809 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3811 #define INIT_TCGETATTR
3814 #if SANITIZER_INTERCEPT_REALPATH
3815 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3817 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3818 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3820 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3821 // version of a versioned symbol. For realpath(), this gives us something
3822 // (called __old_realpath) that does not handle NULL in the second argument.
3823 // Handle it as part of the interceptor.
3824 char *allocated_path = nullptr;
3826 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3828 char *res = REAL(realpath)(path, resolved_path);
3829 if (allocated_path && !res)
3830 WRAP(free)(allocated_path);
3831 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3834 # define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3836 #define INIT_REALPATH
3839 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3840 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3842 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3843 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3844 char *res = REAL(canonicalize_file_name)(path);
3845 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3848 #define INIT_CANONICALIZE_FILE_NAME \
3849 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3851 #define INIT_CANONICALIZE_FILE_NAME
3854 #if SANITIZER_INTERCEPT_CONFSTR
3855 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3857 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3858 // FIXME: under ASan the call below may write to freed memory and corrupt
3859 // its metadata. See
3860 // https://github.com/google/sanitizers/issues/321.
3861 SIZE_T res = REAL(confstr)(name, buf, len);
3863 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3866 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3868 #define INIT_CONFSTR
3871 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3872 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3874 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3875 // FIXME: under ASan the call below may write to freed memory and corrupt
3876 // its metadata. See
3877 // https://github.com/google/sanitizers/issues/321.
3878 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3879 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3882 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3884 #define INIT_SCHED_GETAFFINITY
3887 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3888 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3890 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3891 int res = REAL(sched_getparam)(pid, param);
3892 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3895 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3897 #define INIT_SCHED_GETPARAM
3900 #if SANITIZER_INTERCEPT_STRERROR
3901 INTERCEPTOR(char *, strerror, int errnum) {
3903 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3904 COMMON_INTERCEPTOR_STRERROR();
3905 char *res = REAL(strerror)(errnum);
3906 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3909 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3911 #define INIT_STRERROR
3914 #if SANITIZER_INTERCEPT_STRERROR_R
3915 // There are 2 versions of strerror_r:
3916 // * POSIX version returns 0 on success, negative error code on failure,
3917 // writes message to buf.
3918 // * GNU version returns message pointer, which points to either buf or some
3920 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3921 SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3923 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3924 // At least on OSX, buf contents are valid even when the call fails.
3925 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3927 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3928 // FIXME: under ASan the call below may write to freed memory and corrupt
3929 // its metadata. See
3930 // https://github.com/google/sanitizers/issues/321.
3931 int res = REAL(strerror_r)(errnum, buf, buflen);
3933 SIZE_T sz = internal_strnlen(buf, buflen);
3934 if (sz < buflen) ++sz;
3935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3940 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3942 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3943 // FIXME: under ASan the call below may write to freed memory and corrupt
3944 // its metadata. See
3945 // https://github.com/google/sanitizers/issues/321.
3946 char *res = REAL(strerror_r)(errnum, buf, buflen);
3948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3950 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3953 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3955 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3957 #define INIT_STRERROR_R
3960 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3961 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3963 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3964 // FIXME: under ASan the call below may write to freed memory and corrupt
3965 // its metadata. See
3966 // https://github.com/google/sanitizers/issues/321.
3967 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3968 // This version always returns a null-terminated string.
3970 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
3973 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3975 #define INIT_XPG_STRERROR_R
3978 #if SANITIZER_INTERCEPT_SCANDIR
3979 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3980 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3981 const struct __sanitizer_dirent **);
3983 static THREADLOCAL scandir_filter_f scandir_filter;
3984 static THREADLOCAL scandir_compar_f scandir_compar;
3986 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3987 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3988 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
3989 return scandir_filter(dir);
3992 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3993 const struct __sanitizer_dirent **b) {
3994 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3995 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3996 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
3997 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3998 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
3999 return scandir_compar(a, b);
4002 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
4003 scandir_filter_f filter, scandir_compar_f compar) {
4005 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
4006 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4007 scandir_filter = filter;
4008 scandir_compar = compar;
4009 // FIXME: under ASan the call below may write to freed memory and corrupt
4010 // its metadata. See
4011 // https://github.com/google/sanitizers/issues/321.
4012 int res = REAL(scandir)(dirp, namelist,
4013 filter ? wrapped_scandir_filter : nullptr,
4014 compar ? wrapped_scandir_compar : nullptr);
4015 scandir_filter = nullptr;
4016 scandir_compar = nullptr;
4017 if (namelist && res > 0) {
4018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4019 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4020 for (int i = 0; i < res; ++i)
4021 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4022 __sanitizer_dirsiz((*namelist)[i]));
4026 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
4028 #define INIT_SCANDIR
4031 #if SANITIZER_INTERCEPT_SCANDIR64
4032 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
4033 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
4034 const struct __sanitizer_dirent64 **);
4036 static THREADLOCAL scandir64_filter_f scandir64_filter;
4037 static THREADLOCAL scandir64_compar_f scandir64_compar;
4039 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
4040 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4041 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4042 return scandir64_filter(dir);
4045 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
4046 const struct __sanitizer_dirent64 **b) {
4047 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4048 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4049 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4050 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4051 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4052 return scandir64_compar(a, b);
4055 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
4056 scandir64_filter_f filter, scandir64_compar_f compar) {
4058 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
4059 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4060 scandir64_filter = filter;
4061 scandir64_compar = compar;
4062 // FIXME: under ASan the call below may write to freed memory and corrupt
4063 // its metadata. See
4064 // https://github.com/google/sanitizers/issues/321.
4066 REAL(scandir64)(dirp, namelist,
4067 filter ? wrapped_scandir64_filter : nullptr,
4068 compar ? wrapped_scandir64_compar : nullptr);
4069 scandir64_filter = nullptr;
4070 scandir64_compar = nullptr;
4071 if (namelist && res > 0) {
4072 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4073 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4074 for (int i = 0; i < res; ++i)
4075 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4076 __sanitizer_dirsiz((*namelist)[i]));
4080 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
4082 #define INIT_SCANDIR64
4085 #if SANITIZER_INTERCEPT_GETGROUPS
4086 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
4088 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
4089 // FIXME: under ASan the call below may write to freed memory and corrupt
4090 // its metadata. See
4091 // https://github.com/google/sanitizers/issues/321.
4092 int res = REAL(getgroups)(size, lst);
4093 if (res >= 0 && lst && size > 0)
4094 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
4097 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
4099 #define INIT_GETGROUPS
4102 #if SANITIZER_INTERCEPT_POLL
4103 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
4104 __sanitizer_nfds_t nfds) {
4105 for (unsigned i = 0; i < nfds; ++i) {
4106 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
4107 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
4111 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
4112 __sanitizer_nfds_t nfds) {
4113 for (unsigned i = 0; i < nfds; ++i)
4114 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
4115 sizeof(fds[i].revents));
4118 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4121 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
4122 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4123 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
4124 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4127 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
4132 #if SANITIZER_INTERCEPT_PPOLL
4133 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4134 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
4136 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
4137 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4139 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
4140 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
4142 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
4143 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4146 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
4151 #if SANITIZER_INTERCEPT_WORDEXP
4152 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
4154 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
4155 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
4156 // FIXME: under ASan the call below may write to freed memory and corrupt
4157 // its metadata. See
4158 // https://github.com/google/sanitizers/issues/321.
4159 int res = REAL(wordexp)(s, p, flags);
4161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4163 ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc;
4164 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
4165 sizeof(*p->we_wordv) * (we_wordc + 1));
4166 for (uptr i = 0; i < we_wordc; ++i) {
4167 char *w = p->we_wordv[i];
4168 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);
4173 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
4175 #define INIT_WORDEXP
4178 #if SANITIZER_INTERCEPT_SIGWAIT
4179 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
4181 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
4182 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4183 // FIXME: under ASan the call below may write to freed memory and corrupt
4184 // its metadata. See
4185 // https://github.com/google/sanitizers/issues/321.
4186 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwait)(set, sig);
4187 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
4190 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
4192 #define INIT_SIGWAIT
4195 #if SANITIZER_INTERCEPT_SIGWAITINFO
4196 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
4198 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
4199 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4200 // FIXME: under ASan the call below may write to freed memory and corrupt
4201 // its metadata. See
4202 // https://github.com/google/sanitizers/issues/321.
4203 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwaitinfo)(set, info);
4204 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4207 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
4209 #define INIT_SIGWAITINFO
4212 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
4213 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
4216 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
4217 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
4218 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4219 // FIXME: under ASan the call below may write to freed memory and corrupt
4220 // its metadata. See
4221 // https://github.com/google/sanitizers/issues/321.
4222 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigtimedwait)(set, info, timeout);
4223 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4226 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4228 #define INIT_SIGTIMEDWAIT
4231 #if SANITIZER_INTERCEPT_SIGSETOPS
4232 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4234 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4235 // FIXME: under ASan the call below may write to freed memory and corrupt
4236 // its metadata. See
4237 // https://github.com/google/sanitizers/issues/321.
4238 int res = REAL(sigemptyset)(set);
4239 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4243 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4245 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4246 // FIXME: under ASan the call below may write to freed memory and corrupt
4247 // its metadata. See
4248 // https://github.com/google/sanitizers/issues/321.
4249 int res = REAL(sigfillset)(set);
4250 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4253 #define INIT_SIGSETOPS \
4254 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4255 COMMON_INTERCEPT_FUNCTION(sigfillset);
4257 #define INIT_SIGSETOPS
4260 #if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
4261 INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
4262 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4264 COMMON_INTERCEPTOR_ENTER(ctx, sigandset, dst, src1, src2);
4266 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4268 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4269 int res = REAL(sigandset)(dst, src1, src2);
4271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4275 INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
4276 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4278 COMMON_INTERCEPTOR_ENTER(ctx, sigorset, dst, src1, src2);
4280 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4282 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4283 int res = REAL(sigorset)(dst, src1, src2);
4285 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4288 #define INIT_SIGSET_LOGICOPS \
4289 COMMON_INTERCEPT_FUNCTION(sigandset); \
4290 COMMON_INTERCEPT_FUNCTION(sigorset);
4292 #define INIT_SIGSET_LOGICOPS
4295 #if SANITIZER_INTERCEPT_SIGPENDING
4296 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4298 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4299 // FIXME: under ASan the call below may write to freed memory and corrupt
4300 // its metadata. See
4301 // https://github.com/google/sanitizers/issues/321.
4302 int res = REAL(sigpending)(set);
4303 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4306 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4308 #define INIT_SIGPENDING
4311 #if SANITIZER_INTERCEPT_SIGPROCMASK
4312 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4313 __sanitizer_sigset_t *oldset) {
4315 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4316 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4317 // FIXME: under ASan the call below may write to freed memory and corrupt
4318 // its metadata. See
4319 // https://github.com/google/sanitizers/issues/321.
4320 int res = REAL(sigprocmask)(how, set, oldset);
4322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4325 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4327 #define INIT_SIGPROCMASK
4330 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4331 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4332 __sanitizer_sigset_t *oldset) {
4334 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4335 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4336 // FIXME: under ASan the call below may write to freed memory and corrupt
4337 // its metadata. See
4338 // https://github.com/google/sanitizers/issues/321.
4339 int res = REAL(pthread_sigmask)(how, set, oldset);
4341 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4344 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4346 #define INIT_PTHREAD_SIGMASK
4349 #if SANITIZER_INTERCEPT_BACKTRACE
4350 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4352 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4353 // 'buffer' might be freed memory, hence it is unsafe to directly call
4354 // REAL(backtrace)(buffer, size). Instead, we use our own known-good
4356 void **scratch = (void**)InternalAlloc(sizeof(void*) * size);
4357 int res = REAL(backtrace)(scratch, size);
4358 if (res && buffer) {
4359 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4360 internal_memcpy(buffer, scratch, res * sizeof(*buffer));
4362 InternalFree(scratch);
4366 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4368 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4370 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4371 // The COMMON_INTERCEPTOR_READ_RANGE above ensures that 'buffer' is
4372 // valid for reading.
4373 char **res = REAL(backtrace_symbols)(buffer, size);
4375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4376 for (int i = 0; i < size; ++i)
4377 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], internal_strlen(res[i]) + 1);
4381 #define INIT_BACKTRACE \
4382 COMMON_INTERCEPT_FUNCTION(backtrace); \
4383 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4385 #define INIT_BACKTRACE
4388 #if SANITIZER_INTERCEPT__EXIT
4389 INTERCEPTOR(void, _exit, int status) {
4391 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4392 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4393 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4394 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4395 if (status == 0) status = status1;
4396 REAL(_exit)(status);
4398 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4403 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4404 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4405 ALIAS(WRAP(pthread_setcancelstate));
4407 #define INIT___LIBC_THR_SETCANCELSTATE \
4408 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4410 #define INIT___LIBC_THR_SETCANCELSTATE
4413 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4414 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4415 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4416 if (mnt->mnt_fsname)
4417 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4418 internal_strlen(mnt->mnt_fsname) + 1);
4420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4421 internal_strlen(mnt->mnt_dir) + 1);
4423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4424 internal_strlen(mnt->mnt_type) + 1);
4426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4427 internal_strlen(mnt->mnt_opts) + 1);
4431 #if SANITIZER_INTERCEPT_GETMNTENT
4432 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4434 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4435 __sanitizer_mntent *res = REAL(getmntent)(fp);
4436 if (res) write_mntent(ctx, res);
4439 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4441 #define INIT_GETMNTENT
4444 #if SANITIZER_INTERCEPT_GETMNTENT_R
4445 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4446 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4448 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4449 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4450 if (res) write_mntent(ctx, res);
4453 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4455 #define INIT_GETMNTENT_R
4458 #if SANITIZER_INTERCEPT_STATFS
4459 INTERCEPTOR(int, statfs, char *path, void *buf) {
4461 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4462 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4463 // FIXME: under ASan the call below may write to freed memory and corrupt
4464 // its metadata. See
4465 // https://github.com/google/sanitizers/issues/321.
4466 int res = REAL(statfs)(path, buf);
4467 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4470 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4472 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4473 // FIXME: under ASan the call below may write to freed memory and corrupt
4474 // its metadata. See
4475 // https://github.com/google/sanitizers/issues/321.
4476 int res = REAL(fstatfs)(fd, buf);
4477 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4480 #define INIT_STATFS \
4481 COMMON_INTERCEPT_FUNCTION(statfs); \
4482 COMMON_INTERCEPT_FUNCTION(fstatfs);
4487 #if SANITIZER_INTERCEPT_STATFS64
4488 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4490 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4491 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4492 // FIXME: under ASan the call below may write to freed memory and corrupt
4493 // its metadata. See
4494 // https://github.com/google/sanitizers/issues/321.
4495 int res = REAL(statfs64)(path, buf);
4496 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4499 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4501 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4502 // FIXME: under ASan the call below may write to freed memory and corrupt
4503 // its metadata. See
4504 // https://github.com/google/sanitizers/issues/321.
4505 int res = REAL(fstatfs64)(fd, buf);
4506 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4509 #define INIT_STATFS64 \
4510 COMMON_INTERCEPT_FUNCTION(statfs64); \
4511 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4513 #define INIT_STATFS64
4516 #if SANITIZER_INTERCEPT_STATVFS
4517 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4519 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4520 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4521 // FIXME: under ASan the call below may write to freed memory and corrupt
4522 // its metadata. See
4523 // https://github.com/google/sanitizers/issues/321.
4524 int res = REAL(statvfs)(path, buf);
4525 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4528 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4530 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4531 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4532 // FIXME: under ASan the call below may write to freed memory and corrupt
4533 // its metadata. See
4534 // https://github.com/google/sanitizers/issues/321.
4535 int res = REAL(fstatvfs)(fd, buf);
4537 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4539 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4543 #define INIT_STATVFS \
4544 COMMON_INTERCEPT_FUNCTION(statvfs); \
4545 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4547 #define INIT_STATVFS
4550 #if SANITIZER_INTERCEPT_STATVFS64
4551 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4553 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4554 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4555 // FIXME: under ASan the call below may write to freed memory and corrupt
4556 // its metadata. See
4557 // https://github.com/google/sanitizers/issues/321.
4558 int res = REAL(statvfs64)(path, buf);
4559 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4562 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4564 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4565 // FIXME: under ASan the call below may write to freed memory and corrupt
4566 // its metadata. See
4567 // https://github.com/google/sanitizers/issues/321.
4568 int res = REAL(fstatvfs64)(fd, buf);
4569 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4572 #define INIT_STATVFS64 \
4573 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4574 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4576 #define INIT_STATVFS64
4579 #if SANITIZER_INTERCEPT_INITGROUPS
4580 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4582 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4583 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, internal_strlen(user) + 1);
4584 int res = REAL(initgroups)(user, group);
4587 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4589 #define INIT_INITGROUPS
4592 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4593 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4595 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4596 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4597 char *res = REAL(ether_ntoa)(addr);
4598 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4601 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4603 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4604 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4605 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4606 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4609 #define INIT_ETHER_NTOA_ATON \
4610 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4611 COMMON_INTERCEPT_FUNCTION(ether_aton);
4613 #define INIT_ETHER_NTOA_ATON
4616 #if SANITIZER_INTERCEPT_ETHER_HOST
4617 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4619 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4620 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4621 // FIXME: under ASan the call below may write to freed memory and corrupt
4622 // its metadata. See
4623 // https://github.com/google/sanitizers/issues/321.
4624 int res = REAL(ether_ntohost)(hostname, addr);
4625 if (!res && hostname)
4626 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4629 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4631 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4633 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4634 // FIXME: under ASan the call below may write to freed memory and corrupt
4635 // its metadata. See
4636 // https://github.com/google/sanitizers/issues/321.
4637 int res = REAL(ether_hostton)(hostname, addr);
4638 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4641 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4644 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4645 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, internal_strlen(line) + 1);
4646 // FIXME: under ASan the call below may write to freed memory and corrupt
4647 // its metadata. See
4648 // https://github.com/google/sanitizers/issues/321.
4649 int res = REAL(ether_line)(line, addr, hostname);
4651 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4653 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4657 #define INIT_ETHER_HOST \
4658 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4659 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4660 COMMON_INTERCEPT_FUNCTION(ether_line);
4662 #define INIT_ETHER_HOST
4665 #if SANITIZER_INTERCEPT_ETHER_R
4666 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4668 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4669 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4670 // FIXME: under ASan the call below may write to freed memory and corrupt
4671 // its metadata. See
4672 // https://github.com/google/sanitizers/issues/321.
4673 char *res = REAL(ether_ntoa_r)(addr, buf);
4674 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4677 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4678 __sanitizer_ether_addr *addr) {
4680 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4681 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4682 // FIXME: under ASan the call below may write to freed memory and corrupt
4683 // its metadata. See
4684 // https://github.com/google/sanitizers/issues/321.
4685 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4686 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4689 #define INIT_ETHER_R \
4690 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4691 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4693 #define INIT_ETHER_R
4696 #if SANITIZER_INTERCEPT_SHMCTL
4697 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4699 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4700 // FIXME: under ASan the call below may write to freed memory and corrupt
4701 // its metadata. See
4702 // https://github.com/google/sanitizers/issues/321.
4703 int res = REAL(shmctl)(shmid, cmd, buf);
4706 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4707 sz = sizeof(__sanitizer_shmid_ds);
4708 else if (cmd == shmctl_ipc_info)
4709 sz = struct_shminfo_sz;
4710 else if (cmd == shmctl_shm_info)
4711 sz = struct_shm_info_sz;
4712 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4716 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4721 #if SANITIZER_INTERCEPT_RANDOM_R
4722 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4724 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4725 // FIXME: under ASan the call below may write to freed memory and corrupt
4726 // its metadata. See
4727 // https://github.com/google/sanitizers/issues/321.
4728 int res = REAL(random_r)(buf, result);
4730 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4733 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4735 #define INIT_RANDOM_R
4738 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4739 // its metadata. See
4740 // https://github.com/google/sanitizers/issues/321.
4741 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4742 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4743 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4744 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4745 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4746 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4747 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4748 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4749 INTERCEPTOR(int, fn, void *attr, void *r) { \
4751 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4752 int res = REAL(fn)(attr, r); \
4753 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4756 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4757 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4758 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4759 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4760 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4761 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4762 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4763 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4764 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4765 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4768 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4769 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4770 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4771 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4772 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4773 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4775 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4776 // FIXME: under ASan the call below may write to freed memory and corrupt
4777 // its metadata. See
4778 // https://github.com/google/sanitizers/issues/321.
4779 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4781 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4782 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4787 // We may need to call the real pthread_attr_getstack from the run-time
4788 // in sanitizer_common, but we don't want to include the interception headers
4789 // there. So, just define this function here.
4790 namespace __sanitizer {
4792 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4793 return REAL(pthread_attr_getstack)(attr, addr, size);
4796 } // namespace __sanitizer
4798 #define INIT_PTHREAD_ATTR_GET \
4799 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4800 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4801 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4802 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4803 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4805 #define INIT_PTHREAD_ATTR_GET
4808 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4809 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4810 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4812 #define INIT_PTHREAD_ATTR_GET_SCHED \
4813 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4814 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4816 #define INIT_PTHREAD_ATTR_GET_SCHED
4819 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4820 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4822 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4823 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4825 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4828 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4829 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4832 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4834 // FIXME: under ASan the call below may write to freed memory and corrupt
4835 // its metadata. See
4836 // https://github.com/google/sanitizers/issues/321.
4837 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4838 if (!res && cpusetsize && cpuset)
4839 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4843 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4844 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4846 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4849 #if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
4850 INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
4853 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
4855 // FIXME: under ASan the call below may write to freed memory and corrupt
4856 // its metadata. See
4857 // https://github.com/google/sanitizers/issues/321.
4858 int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
4859 if (!res && cpusetsize && cpuset)
4860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4864 #define INIT_PTHREAD_GETAFFINITY_NP \
4865 COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
4867 #define INIT_PTHREAD_GETAFFINITY_NP
4870 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4871 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4872 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4873 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4875 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4878 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4879 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4880 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4881 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4883 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4886 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4887 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4888 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4889 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4891 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4894 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4895 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4896 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4897 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4899 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4902 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4903 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4904 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4905 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4907 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4910 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4911 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4912 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4913 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4915 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4918 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4919 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4920 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4921 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4923 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4926 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4927 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4928 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4929 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4931 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4934 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4935 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4936 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4937 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4939 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4942 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4943 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4944 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4945 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4947 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4950 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4951 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4952 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4953 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4955 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4958 #if SANITIZER_INTERCEPT_TMPNAM
4959 INTERCEPTOR(char *, tmpnam, char *s) {
4961 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4962 char *res = REAL(tmpnam)(s);
4965 // FIXME: under ASan the call below may write to freed memory and corrupt
4966 // its metadata. See
4967 // https://github.com/google/sanitizers/issues/321.
4968 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
4970 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4974 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4979 #if SANITIZER_INTERCEPT_TMPNAM_R
4980 INTERCEPTOR(char *, tmpnam_r, char *s) {
4982 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4983 // FIXME: under ASan the call below may write to freed memory and corrupt
4984 // its metadata. See
4985 // https://github.com/google/sanitizers/issues/321.
4986 char *res = REAL(tmpnam_r)(s);
4987 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
4990 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4992 #define INIT_TMPNAM_R
4995 #if SANITIZER_INTERCEPT_PTSNAME
4996 INTERCEPTOR(char *, ptsname, int fd) {
4998 COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
4999 char *res = REAL(ptsname)(fd);
5001 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5004 #define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
5006 #define INIT_PTSNAME
5009 #if SANITIZER_INTERCEPT_PTSNAME_R
5010 INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
5012 COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
5013 int res = REAL(ptsname_r)(fd, name, namesize);
5015 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5018 #define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
5020 #define INIT_PTSNAME_R
5023 #if SANITIZER_INTERCEPT_TTYNAME
5024 INTERCEPTOR(char *, ttyname, int fd) {
5026 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
5027 char *res = REAL(ttyname)(fd);
5029 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5032 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
5034 #define INIT_TTYNAME
5037 #if SANITIZER_INTERCEPT_TTYNAME_R
5038 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
5040 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
5041 int res = REAL(ttyname_r)(fd, name, namesize);
5043 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5046 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
5048 #define INIT_TTYNAME_R
5051 #if SANITIZER_INTERCEPT_TEMPNAM
5052 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
5054 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
5055 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, internal_strlen(dir) + 1);
5056 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, internal_strlen(pfx) + 1);
5057 char *res = REAL(tempnam)(dir, pfx);
5058 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5061 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
5063 #define INIT_TEMPNAM
5066 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
5067 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
5069 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
5070 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5071 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
5072 return REAL(pthread_setname_np)(thread, name);
5074 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5075 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
5076 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
5078 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
5079 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
5080 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5081 internal_snprintf(newname, sizeof(newname), name, arg);
5082 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
5083 return REAL(pthread_setname_np)(thread, name, arg);
5085 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5087 #define INIT_PTHREAD_SETNAME_NP
5090 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
5091 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
5093 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
5094 int res = REAL(pthread_getname_np)(thread, name, len);
5096 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
5099 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
5101 #define INIT_PTHREAD_GETNAME_NP
5104 #if SANITIZER_INTERCEPT_SINCOS
5105 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
5107 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
5108 // FIXME: under ASan the call below may write to freed memory and corrupt
5109 // its metadata. See
5110 // https://github.com/google/sanitizers/issues/321.
5111 REAL(sincos)(x, sin, cos);
5112 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5113 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5115 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
5117 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
5118 // FIXME: under ASan the call below may write to freed memory and corrupt
5119 // its metadata. See
5120 // https://github.com/google/sanitizers/issues/321.
5121 REAL(sincosf)(x, sin, cos);
5122 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5123 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5125 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
5127 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
5128 // FIXME: under ASan the call below may write to freed memory and corrupt
5129 // its metadata. See
5130 // https://github.com/google/sanitizers/issues/321.
5131 REAL(sincosl)(x, sin, cos);
5132 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5133 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5135 #define INIT_SINCOS \
5136 COMMON_INTERCEPT_FUNCTION(sincos); \
5137 COMMON_INTERCEPT_FUNCTION(sincosf); \
5138 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
5143 #if SANITIZER_INTERCEPT_REMQUO
5144 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
5146 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
5147 // FIXME: under ASan the call below may write to freed memory and corrupt
5148 // its metadata. See
5149 // https://github.com/google/sanitizers/issues/321.
5150 double res = REAL(remquo)(x, y, quo);
5151 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5154 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
5156 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
5157 // FIXME: under ASan the call below may write to freed memory and corrupt
5158 // its metadata. See
5159 // https://github.com/google/sanitizers/issues/321.
5160 float res = REAL(remquof)(x, y, quo);
5161 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5164 #define INIT_REMQUO \
5165 COMMON_INTERCEPT_FUNCTION(remquo); \
5166 COMMON_INTERCEPT_FUNCTION(remquof);
5171 #if SANITIZER_INTERCEPT_REMQUOL
5172 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
5174 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
5175 // FIXME: under ASan the call below may write to freed memory and corrupt
5176 // its metadata. See
5177 // https://github.com/google/sanitizers/issues/321.
5178 long double res = REAL(remquol)(x, y, quo);
5179 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5182 #define INIT_REMQUOL \
5183 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
5185 #define INIT_REMQUOL
5188 #if SANITIZER_INTERCEPT_LGAMMA
5190 INTERCEPTOR(double, lgamma, double x) {
5192 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
5193 double res = REAL(lgamma)(x);
5194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5197 INTERCEPTOR(float, lgammaf, float x) {
5199 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
5200 float res = REAL(lgammaf)(x);
5201 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5204 #define INIT_LGAMMA \
5205 COMMON_INTERCEPT_FUNCTION(lgamma); \
5206 COMMON_INTERCEPT_FUNCTION(lgammaf);
5211 #if SANITIZER_INTERCEPT_LGAMMAL
5212 INTERCEPTOR(long double, lgammal, long double x) {
5214 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
5215 long double res = REAL(lgammal)(x);
5216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5219 #define INIT_LGAMMAL \
5220 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
5222 #define INIT_LGAMMAL
5225 #if SANITIZER_INTERCEPT_LGAMMA_R
5226 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
5228 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
5229 // FIXME: under ASan the call below may write to freed memory and corrupt
5230 // its metadata. See
5231 // https://github.com/google/sanitizers/issues/321.
5232 double res = REAL(lgamma_r)(x, signp);
5233 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5236 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
5238 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
5239 // FIXME: under ASan the call below may write to freed memory and corrupt
5240 // its metadata. See
5241 // https://github.com/google/sanitizers/issues/321.
5242 float res = REAL(lgammaf_r)(x, signp);
5243 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5246 #define INIT_LGAMMA_R \
5247 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
5248 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
5250 #define INIT_LGAMMA_R
5253 #if SANITIZER_INTERCEPT_LGAMMAL_R
5254 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5256 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5257 // FIXME: under ASan the call below may write to freed memory and corrupt
5258 // its metadata. See
5259 // https://github.com/google/sanitizers/issues/321.
5260 long double res = REAL(lgammal_r)(x, signp);
5261 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5264 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5266 #define INIT_LGAMMAL_R
5269 #if SANITIZER_INTERCEPT_DRAND48_R
5270 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5272 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5273 // FIXME: under ASan the call below may write to freed memory and corrupt
5274 // its metadata. See
5275 // https://github.com/google/sanitizers/issues/321.
5276 int res = REAL(drand48_r)(buffer, result);
5277 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5280 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5282 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5283 // FIXME: under ASan the call below may write to freed memory and corrupt
5284 // its metadata. See
5285 // https://github.com/google/sanitizers/issues/321.
5286 int res = REAL(lrand48_r)(buffer, result);
5287 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5290 #define INIT_DRAND48_R \
5291 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5292 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5294 #define INIT_DRAND48_R
5297 #if SANITIZER_INTERCEPT_RAND_R
5298 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5300 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5301 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5302 return REAL(rand_r)(seedp);
5304 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5309 #if SANITIZER_INTERCEPT_GETLINE
5310 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5312 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5313 // FIXME: under ASan the call below may write to freed memory and corrupt
5314 // its metadata. See
5315 // https://github.com/google/sanitizers/issues/321.
5316 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5318 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5320 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5325 // FIXME: under ASan the call below may write to freed memory and corrupt its
5327 // https://github.com/google/sanitizers/issues/321.
5328 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5331 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5332 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5341 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5343 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5345 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5346 // with its own body.
5347 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5349 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5351 #define INIT_GETLINE \
5352 COMMON_INTERCEPT_FUNCTION(getline); \
5353 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5354 COMMON_INTERCEPT_FUNCTION(getdelim);
5356 #define INIT_GETLINE
5359 #if SANITIZER_INTERCEPT_ICONV
5360 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5361 char **outbuf, SIZE_T *outbytesleft) {
5363 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5366 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5367 if (inbuf && inbytesleft)
5368 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5370 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5371 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5372 // FIXME: under ASan the call below may write to freed memory and corrupt
5373 // its metadata. See
5374 // https://github.com/google/sanitizers/issues/321.
5375 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5376 if (outbuf && *outbuf > outbuf_orig) {
5377 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5378 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5382 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5387 #if SANITIZER_INTERCEPT_TIMES
5388 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5390 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5391 // FIXME: under ASan the call below may write to freed memory and corrupt
5392 // its metadata. See
5393 // https://github.com/google/sanitizers/issues/321.
5394 __sanitizer_clock_t res = REAL(times)(tms);
5395 if (res != (__sanitizer_clock_t)-1 && tms)
5396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5399 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5404 #if SANITIZER_S390 && \
5405 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5406 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5407 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5410 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5412 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5413 // If you see any crashes around this functions, there are 2 known issues with
5414 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5415 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5416 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5417 // to access thread local variables (it should not happen normally,
5418 // because sanitizers use initial-exec tls model).
5419 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5421 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5422 void *res = REAL(__tls_get_addr)(arg);
5423 uptr tls_begin, tls_end;
5424 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5425 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5427 // New DTLS block has been allocated.
5428 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5433 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5434 // mostly the same semantics as __tls_get_addr, but its presence enables
5435 // some optimizations in linker (which are safe to ignore here).
5436 INTERCEPTOR(void *, __tls_get_addr_opt, void *arg) ALIAS(WRAP(__tls_get_addr));
5438 #else // SANITIZER_S390
5439 // On s390, we have to intercept two functions here:
5440 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5441 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5442 // a proper pointer. It is used by dlsym for TLS symbols.
5443 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5444 // descriptor offset as an argument instead of a pointer. GOT address
5445 // is passed in r12, so it's necessary to write it in assembly. This is
5446 // the function used by the compiler.
5447 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5448 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5450 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5451 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5452 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5453 void *ptr = reinterpret_cast<void *>(res + tp);
5454 uptr tls_begin, tls_end;
5455 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5456 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5458 // New DTLS block has been allocated.
5459 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5463 #endif // SANITIZER_S390
5465 #define INIT_TLS_GET_ADDR
5468 #if SANITIZER_S390 && \
5469 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5470 // We need a hidden symbol aliasing the above, so that we can jump
5471 // directly to it from the assembly below.
5472 extern "C" __attribute__((visibility("hidden"))) uptr __tls_get_addr_hidden(
5473 void *arg) ALIAS(WRAP(__tls_get_addr_internal));
5474 extern "C" uptr __tls_get_offset(void *arg);
5475 extern "C" uptr TRAMPOLINE(__tls_get_offset)(void *arg);
5476 extern "C" uptr WRAP(__tls_get_offset)(void *arg);
5477 // Now carefully intercept __tls_get_offset.
5480 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5481 // exports our symbol.
5482 ".weak __tls_get_offset\n"
5483 ".set __tls_get_offset, __interceptor___tls_get_offset\n"
5484 ".global __interceptor___tls_get_offset\n"
5485 ".type __interceptor___tls_get_offset, @function\n"
5486 "__interceptor___tls_get_offset:\n"
5488 "la %r2, 0(%r2,%r12)\n"
5489 "jg __tls_get_addr_hidden\n"
5492 "0: la %r2,0(%r2,%r12)\n"
5493 "l %r4,1f-0b(%r3)\n"
5495 "1: .long __tls_get_addr_hidden - 0b\n"
5497 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5498 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5499 ".type __tls_get_offset_wrapper, @function\n"
5500 "__tls_get_offset_wrapper:\n"
5507 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5511 #if SANITIZER_INTERCEPT_LISTXATTR
5512 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5514 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5515 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5516 // FIXME: under ASan the call below may write to freed memory and corrupt
5517 // its metadata. See
5518 // https://github.com/google/sanitizers/issues/321.
5519 SSIZE_T res = REAL(listxattr)(path, list, size);
5520 // Here and below, size == 0 is a special case where nothing is written to the
5521 // buffer, and res contains the desired buffer size.
5522 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5525 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5527 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5528 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5529 // FIXME: under ASan the call below may write to freed memory and corrupt
5530 // its metadata. See
5531 // https://github.com/google/sanitizers/issues/321.
5532 SSIZE_T res = REAL(llistxattr)(path, list, size);
5533 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5536 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5538 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5539 // FIXME: under ASan the call below may write to freed memory and corrupt
5540 // its metadata. See
5541 // https://github.com/google/sanitizers/issues/321.
5542 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5543 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5546 #define INIT_LISTXATTR \
5547 COMMON_INTERCEPT_FUNCTION(listxattr); \
5548 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5549 COMMON_INTERCEPT_FUNCTION(flistxattr);
5551 #define INIT_LISTXATTR
5554 #if SANITIZER_INTERCEPT_GETXATTR
5555 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5558 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5559 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5560 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5561 // FIXME: under ASan the call below may write to freed memory and corrupt
5562 // its metadata. See
5563 // https://github.com/google/sanitizers/issues/321.
5564 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5565 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5568 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5571 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5572 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5573 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5574 // FIXME: under ASan the call below may write to freed memory and corrupt
5575 // its metadata. See
5576 // https://github.com/google/sanitizers/issues/321.
5577 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5578 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5581 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5584 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5585 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5586 // FIXME: under ASan the call below may write to freed memory and corrupt
5587 // its metadata. See
5588 // https://github.com/google/sanitizers/issues/321.
5589 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5590 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5593 #define INIT_GETXATTR \
5594 COMMON_INTERCEPT_FUNCTION(getxattr); \
5595 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5596 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5598 #define INIT_GETXATTR
5601 #if SANITIZER_INTERCEPT_GETRESID
5602 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5604 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5605 // FIXME: under ASan the call below may write to freed memory and corrupt
5606 // its metadata. See
5607 // https://github.com/google/sanitizers/issues/321.
5608 int res = REAL(getresuid)(ruid, euid, suid);
5610 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5611 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5612 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5616 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5618 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5619 // FIXME: under ASan the call below may write to freed memory and corrupt
5620 // its metadata. See
5621 // https://github.com/google/sanitizers/issues/321.
5622 int res = REAL(getresgid)(rgid, egid, sgid);
5624 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5625 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5626 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5630 #define INIT_GETRESID \
5631 COMMON_INTERCEPT_FUNCTION(getresuid); \
5632 COMMON_INTERCEPT_FUNCTION(getresgid);
5634 #define INIT_GETRESID
5637 #if SANITIZER_INTERCEPT_GETIFADDRS
5638 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5639 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5640 // intercept it to poison the memory again.
5641 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5643 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5644 // FIXME: under ASan the call below may write to freed memory and corrupt
5645 // its metadata. See
5646 // https://github.com/google/sanitizers/issues/321.
5647 int res = REAL(getifaddrs)(ifap);
5648 if (res == 0 && ifap) {
5649 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5650 __sanitizer_ifaddrs *p = *ifap;
5652 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5654 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5655 internal_strlen(p->ifa_name) + 1);
5657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5659 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5660 // On Linux this is a union, but the other member also points to a
5661 // struct sockaddr, so the following is sufficient.
5663 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5664 // FIXME(smatveev): Unpoison p->ifa_data as well.
5670 #define INIT_GETIFADDRS \
5671 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5673 #define INIT_GETIFADDRS
5676 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5677 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5679 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5680 // FIXME: under ASan the call below may write to freed memory and corrupt
5681 // its metadata. See
5682 // https://github.com/google/sanitizers/issues/321.
5683 char *res = REAL(if_indextoname)(ifindex, ifname);
5685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5688 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5690 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5692 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5693 return REAL(if_nametoindex)(ifname);
5695 #define INIT_IF_INDEXTONAME \
5696 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5697 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5699 #define INIT_IF_INDEXTONAME
5702 #if SANITIZER_INTERCEPT_CAPGET
5703 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5705 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5707 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5708 // FIXME: under ASan the call below may write to freed memory and corrupt
5709 // its metadata. See
5710 // https://github.com/google/sanitizers/issues/321.
5711 int res = REAL(capget)(hdrp, datap);
5712 if (res == 0 && datap) {
5713 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5714 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, datasz);
5716 // We can also return -1 and write to hdrp->version if the version passed in
5717 // hdrp->version is unsupported. But that's not a trivial condition to check,
5718 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5721 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5723 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5725 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5727 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5728 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, datasz);
5730 return REAL(capset)(hdrp, datap);
5732 #define INIT_CAPGET \
5733 COMMON_INTERCEPT_FUNCTION(capget); \
5734 COMMON_INTERCEPT_FUNCTION(capset);
5739 #if SANITIZER_INTERCEPT_FTIME
5740 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5742 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5743 // FIXME: under ASan the call below may write to freed memory and corrupt
5744 // its metadata. See
5745 // https://github.com/google/sanitizers/issues/321.
5746 int res = REAL(ftime)(tp);
5748 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5751 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5754 #endif // SANITIZER_INTERCEPT_FTIME
5756 #if SANITIZER_INTERCEPT_XDR
5757 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5758 unsigned size, int op) {
5760 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5761 // FIXME: under ASan the call below may write to freed memory and corrupt
5762 // its metadata. See
5763 // https://github.com/google/sanitizers/issues/321.
5764 REAL(xdrmem_create)(xdrs, addr, size, op);
5765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5766 if (op == __sanitizer_XDR_ENCODE) {
5767 // It's not obvious how much data individual xdr_ routines write.
5768 // Simply unpoison the entire target buffer in advance.
5769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5773 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5775 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5776 // FIXME: under ASan the call below may write to freed memory and corrupt
5777 // its metadata. See
5778 // https://github.com/google/sanitizers/issues/321.
5779 REAL(xdrstdio_create)(xdrs, file, op);
5780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5783 // FIXME: under ASan the call below may write to freed memory and corrupt
5784 // its metadata. See
5785 // https://github.com/google/sanitizers/issues/321.
5786 #define XDR_INTERCEPTOR(F, T) \
5787 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5789 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5790 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5791 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5792 int res = REAL(F)(xdrs, p); \
5793 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5798 XDR_INTERCEPTOR(xdr_short, short)
5799 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5800 XDR_INTERCEPTOR(xdr_int, int)
5801 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5802 XDR_INTERCEPTOR(xdr_long, long)
5803 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5804 XDR_INTERCEPTOR(xdr_hyper, long long)
5805 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5806 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5807 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5808 XDR_INTERCEPTOR(xdr_int8_t, u8)
5809 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5810 XDR_INTERCEPTOR(xdr_int16_t, u16)
5811 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5812 XDR_INTERCEPTOR(xdr_int32_t, u32)
5813 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5814 XDR_INTERCEPTOR(xdr_int64_t, u64)
5815 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5816 XDR_INTERCEPTOR(xdr_quad_t, long long)
5817 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5818 XDR_INTERCEPTOR(xdr_bool, bool)
5819 XDR_INTERCEPTOR(xdr_enum, int)
5820 XDR_INTERCEPTOR(xdr_char, char)
5821 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5822 XDR_INTERCEPTOR(xdr_float, float)
5823 XDR_INTERCEPTOR(xdr_double, double)
5825 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5826 // wrapstring, sizeof
5828 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5831 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5832 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5833 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5834 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5835 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5837 // FIXME: under ASan the call below may write to freed memory and corrupt
5838 // its metadata. See
5839 // https://github.com/google/sanitizers/issues/321.
5840 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5841 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5842 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5844 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5849 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5852 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5853 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5854 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5855 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, internal_strlen(*p) + 1);
5857 // FIXME: under ASan the call below may write to freed memory and corrupt
5858 // its metadata. See
5859 // https://github.com/google/sanitizers/issues/321.
5860 int res = REAL(xdr_string)(xdrs, p, maxsize);
5861 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5862 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
5870 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5871 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5872 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5873 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5874 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5875 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5876 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5877 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5878 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5879 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5880 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5881 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5882 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5883 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5884 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5885 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5886 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5887 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5888 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5889 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5890 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5891 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5892 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5893 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5894 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5895 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5896 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5897 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5898 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5899 COMMON_INTERCEPT_FUNCTION(xdr_string);
5902 #endif // SANITIZER_INTERCEPT_XDR
5904 #if SANITIZER_INTERCEPT_XDRREC
5905 typedef int (*xdrrec_cb)(char*, char*, int);
5906 struct XdrRecWrapper {
5910 typedef AddrHashMap<XdrRecWrapper *, 11> XdrRecWrapMap;
5911 static XdrRecWrapMap *xdrrec_wrap_map;
5913 static int xdrrec_wr_wrap(char *handle, char *buf, int count) {
5914 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5915 COMMON_INTERCEPTOR_INITIALIZE_RANGE(buf, count);
5916 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
5917 return wrap->wr(wrap->handle, buf, count);
5920 static int xdrrec_rd_wrap(char *handle, char *buf, int count) {
5921 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5922 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
5923 return wrap->rd(wrap->handle, buf, count);
5926 // This doesn't apply to the solaris version as it has a different function
5928 INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
5929 unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
5930 int (*wr)(char*, char*, int)) {
5932 COMMON_INTERCEPTOR_ENTER(ctx, xdrrec_create, xdr, sndsize, rcvsize,
5934 COMMON_INTERCEPTOR_READ_RANGE(ctx, &xdr->x_op, sizeof xdr->x_op);
5936 // We can't allocate a wrapper on the stack, as the handle is used outside
5937 // this stack frame. So we put it on the heap, and keep track of it with
5938 // the HashMap (keyed by x_private). When we later need to xdr_destroy,
5939 // we can index the map, free the wrapper, and then clean the map entry.
5940 XdrRecWrapper *wrap_data =
5941 (XdrRecWrapper *)InternalAlloc(sizeof(XdrRecWrapper));
5942 wrap_data->handle = handle;
5946 wr = xdrrec_wr_wrap;
5948 rd = xdrrec_rd_wrap;
5949 handle = (char *)wrap_data;
5951 REAL(xdrrec_create)(xdr, sndsize, rcvsize, handle, rd, wr);
5952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdr, sizeof *xdr);
5954 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, false, true);
5958 // We have to intercept this to be able to free wrapper memory;
5959 // otherwise it's not necessary.
5960 INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {
5962 COMMON_INTERCEPTOR_ENTER(ctx, xdr_destroy, xdr);
5964 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, true);
5965 InternalFree(*wrap);
5966 REAL(xdr_destroy)(xdr);
5968 #define INIT_XDRREC_LINUX \
5969 static u64 xdrrec_wrap_mem[sizeof(XdrRecWrapMap) / sizeof(u64) + 1]; \
5970 xdrrec_wrap_map = new ((void *)&xdrrec_wrap_mem) XdrRecWrapMap(); \
5971 COMMON_INTERCEPT_FUNCTION(xdrrec_create); \
5972 COMMON_INTERCEPT_FUNCTION(xdr_destroy);
5974 #define INIT_XDRREC_LINUX
5977 #if SANITIZER_INTERCEPT_TSEARCH
5978 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5979 int (*compar)(const void *, const void *)) {
5981 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5982 // FIXME: under ASan the call below may write to freed memory and corrupt
5983 // its metadata. See
5984 // https://github.com/google/sanitizers/issues/321.
5985 void *res = REAL(tsearch)(key, rootp, compar);
5986 if (res && *(void **)res == key)
5987 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5990 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5992 #define INIT_TSEARCH
5995 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5996 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5997 void unpoison_file(__sanitizer_FILE *fp) {
5998 #if SANITIZER_HAS_STRUCT_FILE
5999 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
6000 #if SANITIZER_NETBSD
6001 if (fp->_bf._base && fp->_bf._size > 0)
6002 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
6005 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
6006 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
6007 fp->_IO_read_end - fp->_IO_read_base);
6008 if (fp->_IO_write_base && fp->_IO_write_base < fp->_IO_write_end)
6009 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_write_base,
6010 fp->_IO_write_end - fp->_IO_write_base);
6012 #endif // SANITIZER_HAS_STRUCT_FILE
6016 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
6017 // These guys are called when a .c source is built with -O2.
6018 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
6020 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
6021 int res = REAL(__uflow)(fp);
6025 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
6027 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
6028 int res = REAL(__underflow)(fp);
6032 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
6034 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
6035 int res = REAL(__overflow)(fp, ch);
6039 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
6041 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
6042 int res = REAL(__wuflow)(fp);
6046 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
6048 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
6049 int res = REAL(__wunderflow)(fp);
6053 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
6055 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
6056 int res = REAL(__woverflow)(fp, ch);
6060 #define INIT_LIBIO_INTERNALS \
6061 COMMON_INTERCEPT_FUNCTION(__uflow); \
6062 COMMON_INTERCEPT_FUNCTION(__underflow); \
6063 COMMON_INTERCEPT_FUNCTION(__overflow); \
6064 COMMON_INTERCEPT_FUNCTION(__wuflow); \
6065 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
6066 COMMON_INTERCEPT_FUNCTION(__woverflow);
6068 #define INIT_LIBIO_INTERNALS
6071 #if SANITIZER_INTERCEPT_FOPEN
6072 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
6074 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
6075 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6076 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6077 __sanitizer_FILE *res = REAL(fopen)(path, mode);
6078 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6079 if (res) unpoison_file(res);
6082 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
6084 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
6085 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6086 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
6087 if (res) unpoison_file(res);
6090 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
6091 __sanitizer_FILE *fp) {
6093 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
6094 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6095 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6096 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6097 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
6098 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6099 if (res) unpoison_file(res);
6102 #define INIT_FOPEN \
6103 COMMON_INTERCEPT_FUNCTION(fopen); \
6104 COMMON_INTERCEPT_FUNCTION(fdopen); \
6105 COMMON_INTERCEPT_FUNCTION(freopen);
6110 #if SANITIZER_INTERCEPT_FLOPEN
6111 INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
6114 va_start(ap, flags);
6115 u16 mode = static_cast<u16>(va_arg(ap, u32));
6117 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6119 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6121 return REAL(flopen)(path, flags, mode);
6124 INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
6127 va_start(ap, flags);
6128 u16 mode = static_cast<u16>(va_arg(ap, u32));
6130 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6132 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6134 return REAL(flopenat)(dirfd, path, flags, mode);
6137 #define INIT_FLOPEN \
6138 COMMON_INTERCEPT_FUNCTION(flopen); \
6139 COMMON_INTERCEPT_FUNCTION(flopenat);
6144 #if SANITIZER_INTERCEPT_FOPEN64
6145 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
6147 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
6148 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6149 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6150 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
6151 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6152 if (res) unpoison_file(res);
6155 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
6156 __sanitizer_FILE *fp) {
6158 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
6159 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6160 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6161 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6162 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
6163 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6164 if (res) unpoison_file(res);
6167 #define INIT_FOPEN64 \
6168 COMMON_INTERCEPT_FUNCTION(fopen64); \
6169 COMMON_INTERCEPT_FUNCTION(freopen64);
6171 #define INIT_FOPEN64
6174 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6175 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
6177 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
6178 // FIXME: under ASan the call below may write to freed memory and corrupt
6179 // its metadata. See
6180 // https://github.com/google/sanitizers/issues/321.
6181 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
6183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6186 FileMetadata file = {ptr, sizeloc};
6187 SetInterceptorMetadata(res, file);
6191 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
6194 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
6195 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
6197 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6200 FileMetadata file = {(char **)ptr, sizeloc};
6201 SetInterceptorMetadata(res, file);
6205 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
6208 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
6209 // FIXME: under ASan the call below may write to freed memory and corrupt
6210 // its metadata. See
6211 // https://github.com/google/sanitizers/issues/321.
6212 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
6213 if (res) unpoison_file(res);
6216 #define INIT_OPEN_MEMSTREAM \
6217 COMMON_INTERCEPT_FUNCTION(open_memstream); \
6218 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
6219 COMMON_INTERCEPT_FUNCTION(fmemopen);
6221 #define INIT_OPEN_MEMSTREAM
6224 #if SANITIZER_INTERCEPT_OBSTACK
6225 static void initialize_obstack(__sanitizer_obstack *obstack) {
6226 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
6228 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
6229 sizeof(*obstack->chunk));
6232 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
6233 int align, void *(*alloc_fn)(uptr arg, uptr sz),
6234 void (*free_fn)(uptr arg, void *p)) {
6236 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
6238 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
6239 if (res) initialize_obstack(obstack);
6242 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
6243 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
6245 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
6247 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
6248 if (res) initialize_obstack(obstack);
6251 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
6253 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
6254 REAL(_obstack_newchunk)(obstack, length);
6256 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
6257 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
6259 #define INIT_OBSTACK \
6260 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
6261 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
6262 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
6264 #define INIT_OBSTACK
6267 #if SANITIZER_INTERCEPT_FFLUSH
6268 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
6270 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
6273 int res = REAL(fflush)(fp);
6274 // FIXME: handle fp == NULL
6276 const FileMetadata *m = GetInterceptorMetadata(fp);
6277 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6281 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6286 #if SANITIZER_INTERCEPT_FCLOSE
6287 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6289 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6290 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6291 const FileMetadata *m = GetInterceptorMetadata(fp);
6294 int res = REAL(fclose)(fp);
6296 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6297 DeleteInterceptorMetadata(fp);
6301 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6306 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6307 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6309 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6310 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6311 void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
6312 Symbolizer::GetOrInit()->InvalidateModuleList();
6313 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6317 INTERCEPTOR(int, dlclose, void *handle) {
6319 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6320 int res = REAL(dlclose)(handle);
6321 Symbolizer::GetOrInit()->InvalidateModuleList();
6322 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6325 #define INIT_DLOPEN_DLCLOSE \
6326 COMMON_INTERCEPT_FUNCTION(dlopen); \
6327 COMMON_INTERCEPT_FUNCTION(dlclose);
6329 #define INIT_DLOPEN_DLCLOSE
6332 #if SANITIZER_INTERCEPT_GETPASS
6333 INTERCEPTOR(char *, getpass, const char *prompt) {
6335 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6337 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
6338 char *res = REAL(getpass)(prompt);
6339 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
6343 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6345 #define INIT_GETPASS
6348 #if SANITIZER_INTERCEPT_TIMERFD
6349 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6352 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6354 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6355 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6356 if (res != -1 && old_value)
6357 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6361 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6363 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6364 int res = REAL(timerfd_gettime)(fd, curr_value);
6365 if (res != -1 && curr_value)
6366 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6369 #define INIT_TIMERFD \
6370 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6371 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6373 #define INIT_TIMERFD
6376 #if SANITIZER_INTERCEPT_MLOCKX
6377 // Linux kernel has a bug that leads to kernel deadlock if a process
6378 // maps TBs of memory and then calls mlock().
6379 static void MlockIsUnsupported() {
6380 static atomic_uint8_t printed;
6381 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6383 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6387 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6388 MlockIsUnsupported();
6392 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6393 MlockIsUnsupported();
6397 INTERCEPTOR(int, mlockall, int flags) {
6398 MlockIsUnsupported();
6402 INTERCEPTOR(int, munlockall, void) {
6403 MlockIsUnsupported();
6407 #define INIT_MLOCKX \
6408 COMMON_INTERCEPT_FUNCTION(mlock); \
6409 COMMON_INTERCEPT_FUNCTION(munlock); \
6410 COMMON_INTERCEPT_FUNCTION(mlockall); \
6411 COMMON_INTERCEPT_FUNCTION(munlockall);
6415 #endif // SANITIZER_INTERCEPT_MLOCKX
6417 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6418 struct WrappedCookie {
6420 __sanitizer_cookie_io_functions_t real_io_funcs;
6423 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6424 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6425 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6426 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6427 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6430 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6431 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6432 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6433 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6434 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6437 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6438 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6439 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6440 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6441 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6442 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6446 static int wrapped_close(void *cookie) {
6447 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6448 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6449 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6450 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6451 InternalFree(wrapped_cookie);
6455 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6456 __sanitizer_cookie_io_functions_t io_funcs) {
6458 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6459 WrappedCookie *wrapped_cookie =
6460 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6461 wrapped_cookie->real_cookie = cookie;
6462 wrapped_cookie->real_io_funcs = io_funcs;
6463 __sanitizer_FILE *res =
6464 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6465 wrapped_seek, wrapped_close});
6469 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6471 #define INIT_FOPENCOOKIE
6472 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6474 #if SANITIZER_INTERCEPT_SEM
6475 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6477 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6478 // Workaround a bug in glibc's "old" semaphore implementation by
6479 // zero-initializing the sem_t contents. This has to be done here because
6480 // interceptors bind to the lowest version before glibc 2.36, hitting the
6481 // buggy code path while the non-sanitized build of the same code works fine.
6482 REAL(memset)(s, 0, sizeof(*s));
6483 int res = REAL(sem_init)(s, pshared, value);
6487 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6489 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6490 int res = REAL(sem_destroy)(s);
6494 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6496 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6497 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6499 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6504 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6506 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6507 int res = REAL(sem_trywait)(s);
6509 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6514 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6516 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6517 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6518 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6520 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6525 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6527 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6528 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6529 int res = REAL(sem_post)(s);
6533 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6535 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6536 int res = REAL(sem_getvalue)(s, sval);
6538 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6539 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6544 INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
6547 va_start(ap, oflag);
6548 u32 mode = va_arg(ap, u32);
6549 u32 value = va_arg(ap, u32);
6550 COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
6551 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6552 __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
6554 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
6559 INTERCEPTOR(int, sem_unlink, const char *name) {
6561 COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
6562 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6563 return REAL(sem_unlink)(name);
6567 COMMON_INTERCEPT_FUNCTION(sem_init); \
6568 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6569 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6570 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6571 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6572 COMMON_INTERCEPT_FUNCTION(sem_post); \
6573 COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
6574 COMMON_INTERCEPT_FUNCTION(sem_open); \
6575 COMMON_INTERCEPT_FUNCTION(sem_unlink);
6578 #endif // SANITIZER_INTERCEPT_SEM
6580 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6581 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6583 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6584 int res = REAL(pthread_setcancelstate)(state, oldstate);
6585 if (res == 0 && oldstate != nullptr)
6586 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6590 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6592 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6593 int res = REAL(pthread_setcanceltype)(type, oldtype);
6594 if (res == 0 && oldtype != nullptr)
6595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6598 #define INIT_PTHREAD_SETCANCEL \
6599 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6600 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6602 #define INIT_PTHREAD_SETCANCEL
6605 #if SANITIZER_INTERCEPT_MINCORE
6606 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6608 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6609 int res = REAL(mincore)(addr, length, vec);
6611 uptr page_size = GetPageSizeCached();
6612 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6617 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6619 #define INIT_MINCORE
6622 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6623 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6624 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6627 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6628 remote_iov, riovcnt, flags);
6629 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6632 write_iovec(ctx, local_iov, liovcnt, res);
6636 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6637 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6640 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6641 remote_iov, riovcnt, flags);
6642 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6645 read_iovec(ctx, local_iov, liovcnt, res);
6648 #define INIT_PROCESS_VM_READV \
6649 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6650 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6652 #define INIT_PROCESS_VM_READV
6655 #if SANITIZER_INTERCEPT_CTERMID
6656 INTERCEPTOR(char *, ctermid, char *s) {
6658 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6659 char *res = REAL(ctermid)(s);
6661 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6665 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6667 #define INIT_CTERMID
6670 #if SANITIZER_INTERCEPT_CTERMID_R
6671 INTERCEPTOR(char *, ctermid_r, char *s) {
6673 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6674 char *res = REAL(ctermid_r)(s);
6676 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6680 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6682 #define INIT_CTERMID_R
6685 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6686 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6688 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6689 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6690 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6692 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6694 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6698 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6699 void *srcaddr, int *addrlen) {
6701 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6703 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6705 if (srcaddr) srcaddr_sz = *addrlen;
6706 (void)srcaddr_sz; // prevent "set but not used" warning
6707 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6709 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6710 if (res >= 0 && srcaddr)
6711 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6712 Min((SIZE_T)*addrlen, srcaddr_sz));
6715 #define INIT_RECV_RECVFROM \
6716 COMMON_INTERCEPT_FUNCTION(recv); \
6717 COMMON_INTERCEPT_FUNCTION(recvfrom);
6719 #define INIT_RECV_RECVFROM
6722 #if SANITIZER_INTERCEPT_SEND_SENDTO
6723 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6725 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6727 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6728 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6730 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6731 if (common_flags()->intercept_send && res > 0)
6732 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6736 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6737 void *dstaddr, int addrlen) {
6739 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6741 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6742 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6744 // Can't check dstaddr as it may have uninitialized padding at the end.
6745 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6746 if (common_flags()->intercept_send && res > 0)
6747 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6750 #define INIT_SEND_SENDTO \
6751 COMMON_INTERCEPT_FUNCTION(send); \
6752 COMMON_INTERCEPT_FUNCTION(sendto);
6754 #define INIT_SEND_SENDTO
6757 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6758 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6760 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6761 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6762 int res = REAL(eventfd_read)(fd, value);
6764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6765 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6769 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6771 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6773 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6774 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6776 int res = REAL(eventfd_write)(fd, value);
6779 #define INIT_EVENTFD_READ_WRITE \
6780 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6781 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6783 #define INIT_EVENTFD_READ_WRITE
6786 #if SANITIZER_INTERCEPT_STAT
6787 INTERCEPTOR(int, stat, const char *path, void *buf) {
6789 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6790 if (common_flags()->intercept_stat)
6791 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6792 int res = REAL(stat)(path, buf);
6794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6797 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6802 #if SANITIZER_INTERCEPT_STAT64
6803 INTERCEPTOR(int, stat64, const char *path, void *buf) {
6805 COMMON_INTERCEPTOR_ENTER(ctx, stat64, path, buf);
6806 if (common_flags()->intercept_stat)
6807 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6808 int res = REAL(stat64)(path, buf);
6810 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6813 #define INIT_STAT64 COMMON_INTERCEPT_FUNCTION(stat64)
6819 #if SANITIZER_INTERCEPT_LSTAT
6820 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6822 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6823 if (common_flags()->intercept_stat)
6824 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6825 int res = REAL(lstat)(path, buf);
6827 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6830 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6835 #if SANITIZER_INTERCEPT_STAT64
6836 INTERCEPTOR(int, lstat64, const char *path, void *buf) {
6838 COMMON_INTERCEPTOR_ENTER(ctx, lstat64, path, buf);
6839 if (common_flags()->intercept_stat)
6840 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6841 int res = REAL(lstat64)(path, buf);
6843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6846 #define INIT_LSTAT64 COMMON_INTERCEPT_FUNCTION(lstat64)
6848 #define INIT_LSTAT64
6851 #if SANITIZER_INTERCEPT___XSTAT
6852 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6854 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6855 if (common_flags()->intercept_stat)
6856 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6857 int res = REAL(__xstat)(version, path, buf);
6859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6862 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6864 #define INIT___XSTAT
6867 #if SANITIZER_INTERCEPT___XSTAT64
6868 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6870 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6871 if (common_flags()->intercept_stat)
6872 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6873 int res = REAL(__xstat64)(version, path, buf);
6875 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6878 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6880 #define INIT___XSTAT64
6883 #if SANITIZER_INTERCEPT___LXSTAT
6884 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6886 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6887 if (common_flags()->intercept_stat)
6888 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6889 int res = REAL(__lxstat)(version, path, buf);
6891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6894 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6896 #define INIT___LXSTAT
6899 #if SANITIZER_INTERCEPT___LXSTAT64
6900 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6902 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6903 if (common_flags()->intercept_stat)
6904 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6905 int res = REAL(__lxstat64)(version, path, buf);
6907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6910 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6912 #define INIT___LXSTAT64
6915 // FIXME: add other *stat interceptor
6917 #if SANITIZER_INTERCEPT_UTMP
6918 INTERCEPTOR(void *, getutent, int dummy) {
6920 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6921 void *res = REAL(getutent)(dummy);
6923 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6926 INTERCEPTOR(void *, getutid, void *ut) {
6928 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6929 void *res = REAL(getutid)(ut);
6931 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6934 INTERCEPTOR(void *, getutline, void *ut) {
6936 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6937 void *res = REAL(getutline)(ut);
6939 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6943 COMMON_INTERCEPT_FUNCTION(getutent); \
6944 COMMON_INTERCEPT_FUNCTION(getutid); \
6945 COMMON_INTERCEPT_FUNCTION(getutline);
6950 #if SANITIZER_INTERCEPT_UTMPX
6951 INTERCEPTOR(void *, getutxent, int dummy) {
6953 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6954 void *res = REAL(getutxent)(dummy);
6956 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6959 INTERCEPTOR(void *, getutxid, void *ut) {
6961 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6962 void *res = REAL(getutxid)(ut);
6964 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6967 INTERCEPTOR(void *, getutxline, void *ut) {
6969 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6970 void *res = REAL(getutxline)(ut);
6972 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6975 INTERCEPTOR(void *, pututxline, const void *ut) {
6977 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
6979 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
6980 void *res = REAL(pututxline)(ut);
6982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
6985 #define INIT_UTMPX \
6986 COMMON_INTERCEPT_FUNCTION(getutxent); \
6987 COMMON_INTERCEPT_FUNCTION(getutxid); \
6988 COMMON_INTERCEPT_FUNCTION(getutxline); \
6989 COMMON_INTERCEPT_FUNCTION(pututxline);
6994 #if SANITIZER_INTERCEPT_GETLOADAVG
6995 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6997 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6998 int res = REAL(getloadavg)(loadavg, nelem);
7000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
7003 #define INIT_GETLOADAVG \
7004 COMMON_INTERCEPT_FUNCTION(getloadavg);
7006 #define INIT_GETLOADAVG
7009 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
7010 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
7014 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
7018 INTERCEPTOR(int, mprobe, void *ptr) {
7023 #if SANITIZER_INTERCEPT_WCSLEN
7024 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
7026 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
7027 SIZE_T res = REAL(wcslen)(s);
7028 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
7032 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
7034 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
7035 SIZE_T res = REAL(wcsnlen)(s, n);
7036 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
7039 #define INIT_WCSLEN \
7040 COMMON_INTERCEPT_FUNCTION(wcslen); \
7041 COMMON_INTERCEPT_FUNCTION(wcsnlen);
7046 #if SANITIZER_INTERCEPT_WCSCAT
7047 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
7049 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
7050 SIZE_T src_size = internal_wcslen(src);
7051 SIZE_T dst_size = internal_wcslen(dst);
7052 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
7053 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7054 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7055 (src_size + 1) * sizeof(wchar_t));
7056 return REAL(wcscat)(dst, src);
7059 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
7061 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
7062 SIZE_T src_size = internal_wcsnlen(src, n);
7063 SIZE_T dst_size = internal_wcslen(dst);
7064 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
7065 Min(src_size + 1, n) * sizeof(wchar_t));
7066 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7067 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7068 (src_size + 1) * sizeof(wchar_t));
7069 return REAL(wcsncat)(dst, src, n);
7071 #define INIT_WCSCAT \
7072 COMMON_INTERCEPT_FUNCTION(wcscat); \
7073 COMMON_INTERCEPT_FUNCTION(wcsncat);
7078 #if SANITIZER_INTERCEPT_WCSDUP
7079 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
7081 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
7082 SIZE_T len = internal_wcslen(s);
7083 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
7084 wchar_t *result = REAL(wcsdup)(s);
7086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
7090 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
7095 #if SANITIZER_INTERCEPT_STRXFRM
7096 static SIZE_T RealStrLen(const char *str) { return internal_strlen(str); }
7098 static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(str); }
7100 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
7103 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
7104 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
7105 sizeof(*src) * (RealStrLen(src) + 1)); \
7106 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
7108 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
7112 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
7113 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
7116 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
7118 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
7121 #define INIT_STRXFRM \
7122 COMMON_INTERCEPT_FUNCTION(strxfrm); \
7123 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
7125 #define INIT_STRXFRM
7128 #if SANITIZER_INTERCEPT___STRXFRM_L
7129 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
7131 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
7134 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
7136 #define INIT___STRXFRM_L
7139 #if SANITIZER_INTERCEPT_WCSXFRM
7140 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
7141 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
7144 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7146 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
7149 #define INIT_WCSXFRM \
7150 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
7151 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
7153 #define INIT_WCSXFRM
7156 #if SANITIZER_INTERCEPT___WCSXFRM_L
7157 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7159 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
7162 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
7164 #define INIT___WCSXFRM_L
7167 #if SANITIZER_INTERCEPT_ACCT
7168 INTERCEPTOR(int, acct, const char *file) {
7170 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
7172 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
7173 return REAL(acct)(file);
7175 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
7180 #if SANITIZER_INTERCEPT_USER_FROM_UID
7181 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
7184 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
7185 user = REAL(user_from_uid)(uid, nouser);
7187 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
7190 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
7192 #define INIT_USER_FROM_UID
7195 #if SANITIZER_INTERCEPT_UID_FROM_USER
7196 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
7199 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
7201 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7202 res = REAL(uid_from_user)(name, uid);
7204 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
7207 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
7209 #define INIT_UID_FROM_USER
7212 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
7213 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
7216 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
7217 group = REAL(group_from_gid)(gid, nogroup);
7219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
7222 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
7224 #define INIT_GROUP_FROM_GID
7227 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
7228 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
7231 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
7233 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
7234 res = REAL(gid_from_group)(group, gid);
7236 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
7239 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
7241 #define INIT_GID_FROM_GROUP
7244 #if SANITIZER_INTERCEPT_ACCESS
7245 INTERCEPTOR(int, access, const char *path, int mode) {
7247 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
7249 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7250 return REAL(access)(path, mode);
7252 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
7257 #if SANITIZER_INTERCEPT_FACCESSAT
7258 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
7260 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
7262 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7263 return REAL(faccessat)(fd, path, mode, flags);
7265 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
7267 #define INIT_FACCESSAT
7270 #if SANITIZER_INTERCEPT_GETGROUPLIST
7271 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
7275 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
7277 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7279 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
7280 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
7281 if (!res && groups && ngroups) {
7282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7283 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7288 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
7290 #define INIT_GETGROUPLIST
7293 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
7294 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
7295 int maxgrp, int *ngroups) {
7298 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
7301 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7302 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
7303 if (!res && groups && ngroups) {
7304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7305 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7310 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
7312 #define INIT_GETGROUPMEMBERSHIP
7315 #if SANITIZER_INTERCEPT_READLINK
7316 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
7318 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
7319 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7320 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
7322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7326 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
7328 #define INIT_READLINK
7331 #if SANITIZER_INTERCEPT_READLINKAT
7332 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
7335 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
7336 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7337 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7339 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7343 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7345 #define INIT_READLINKAT
7348 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7349 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7350 struct file_handle *handle, int *mount_id, int flags) {
7352 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7354 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
7356 __sanitizer_file_handle *sanitizer_handle =
7357 reinterpret_cast<__sanitizer_file_handle*>(handle);
7358 COMMON_INTERCEPTOR_READ_RANGE(
7359 ctx, &sanitizer_handle->handle_bytes,
7360 sizeof(sanitizer_handle->handle_bytes));
7362 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7364 COMMON_INTERCEPTOR_WRITE_RANGE(
7365 ctx, &sanitizer_handle->handle_bytes,
7366 sizeof(sanitizer_handle->handle_bytes));
7367 COMMON_INTERCEPTOR_WRITE_RANGE(
7368 ctx, &sanitizer_handle->handle_type,
7369 sizeof(sanitizer_handle->handle_type));
7370 COMMON_INTERCEPTOR_WRITE_RANGE(
7371 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7372 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7377 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7379 #define INIT_NAME_TO_HANDLE_AT
7382 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7383 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7386 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7388 __sanitizer_file_handle *sanitizer_handle =
7389 reinterpret_cast<__sanitizer_file_handle*>(handle);
7390 COMMON_INTERCEPTOR_READ_RANGE(
7391 ctx, &sanitizer_handle->handle_bytes,
7392 sizeof(sanitizer_handle->handle_bytes));
7393 COMMON_INTERCEPTOR_READ_RANGE(
7394 ctx, &sanitizer_handle->handle_type,
7395 sizeof(sanitizer_handle->handle_type));
7396 COMMON_INTERCEPTOR_READ_RANGE(
7397 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7399 return REAL(open_by_handle_at)(mount_fd, handle, flags);
7402 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7404 #define INIT_OPEN_BY_HANDLE_AT
7407 #if SANITIZER_INTERCEPT_STRLCPY
7408 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7411 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7413 // Keep strnlen as macro argument, as macro may ignore it.
7414 COMMON_INTERCEPTOR_READ_STRING(
7415 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7417 res = REAL(strlcpy)(dst, src, size);
7418 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
7422 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7425 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7426 // src is checked in the strlcpy() interceptor
7428 len = internal_strnlen(dst, size);
7429 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7431 // Reuse the rest of the code in the strlcpy() interceptor
7432 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7434 #define INIT_STRLCPY \
7435 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7436 COMMON_INTERCEPT_FUNCTION(strlcat);
7438 #define INIT_STRLCPY
7441 #if SANITIZER_INTERCEPT_MMAP
7442 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7445 if (common_flags()->detect_write_exec)
7446 ReportMmapWriteExec(prot, flags);
7447 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7448 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7449 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7450 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7453 INTERCEPTOR(int, munmap, void *addr, SIZE_T sz) {
7455 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7456 return (int)internal_munmap(addr, sz);
7457 COMMON_INTERCEPTOR_ENTER(ctx, munmap, addr, sz);
7458 COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz);
7461 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7463 if (common_flags()->detect_write_exec)
7464 ReportMmapWriteExec(prot, 0);
7465 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7466 return (int)internal_mprotect(addr, sz, prot);
7467 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7468 MprotectMallocZones(addr, prot);
7469 return REAL(mprotect)(addr, sz, prot);
7472 COMMON_INTERCEPT_FUNCTION(mmap); \
7473 COMMON_INTERCEPT_FUNCTION(munmap); \
7474 COMMON_INTERCEPT_FUNCTION(mprotect);
7479 #if SANITIZER_INTERCEPT_MMAP64
7480 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7483 if (common_flags()->detect_write_exec)
7484 ReportMmapWriteExec(prot, flags);
7485 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7486 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7487 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7488 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7490 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7495 #if SANITIZER_INTERCEPT_DEVNAME
7496 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7499 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7500 name = REAL(devname)(dev, type);
7502 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
7505 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7507 #define INIT_DEVNAME
7510 #if SANITIZER_INTERCEPT_DEVNAME_R
7511 #if SANITIZER_NETBSD
7512 #define DEVNAME_R_RETTYPE int
7513 #define DEVNAME_R_SUCCESS(x) (!(x))
7515 #define DEVNAME_R_RETTYPE char*
7516 #define DEVNAME_R_SUCCESS(x) (x)
7518 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7521 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7522 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7523 if (DEVNAME_R_SUCCESS(res))
7524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
7527 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7529 #define INIT_DEVNAME_R
7532 #if SANITIZER_INTERCEPT_FGETLN
7533 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7535 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7536 char *str = REAL(fgetln)(stream, len);
7538 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7539 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7543 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7548 #if SANITIZER_INTERCEPT_STRMODE
7549 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7551 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7552 REAL(strmode)(mode, bp);
7554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
7556 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7558 #define INIT_STRMODE
7561 #if SANITIZER_INTERCEPT_TTYENT
7562 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7564 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7565 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7567 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7570 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7572 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7574 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7575 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7577 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7580 #define INIT_TTYENT \
7581 COMMON_INTERCEPT_FUNCTION(getttyent); \
7582 COMMON_INTERCEPT_FUNCTION(getttynam);
7587 #if SANITIZER_INTERCEPT_TTYENTPATH
7588 INTERCEPTOR(int, setttyentpath, char *path) {
7590 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7592 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7593 return REAL(setttyentpath)(path);
7595 #define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
7597 #define INIT_TTYENTPATH
7600 #if SANITIZER_INTERCEPT_PROTOENT
7601 static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
7602 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7604 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
7606 SIZE_T pp_size = 1; // One handles the trailing \0
7608 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7609 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
7611 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, pp_size * sizeof(char *));
7614 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7616 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7617 struct __sanitizer_protoent *p = REAL(getprotoent)();
7619 write_protoent(ctx, p);
7623 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7625 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7627 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7628 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7630 write_protoent(ctx, p);
7634 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7636 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7637 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7639 write_protoent(ctx, p);
7642 #define INIT_PROTOENT \
7643 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7644 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7645 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7647 #define INIT_PROTOENT
7650 #if SANITIZER_INTERCEPT_PROTOENT_R
7651 INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
7652 char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
7654 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
7656 int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
7658 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7659 if (!res && *result)
7660 write_protoent(ctx, *result);
7664 INTERCEPTOR(int, getprotobyname_r, const char *name,
7665 struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
7666 struct __sanitizer_protoent **result) {
7668 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
7671 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7672 int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
7674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7675 if (!res && *result)
7676 write_protoent(ctx, *result);
7680 INTERCEPTOR(int, getprotobynumber_r, int num,
7681 struct __sanitizer_protoent *result_buf, char *buf,
7682 SIZE_T buflen, struct __sanitizer_protoent **result) {
7684 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
7686 int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
7688 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7689 if (!res && *result)
7690 write_protoent(ctx, *result);
7694 #define INIT_PROTOENT_R \
7695 COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
7696 COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
7697 COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
7699 #define INIT_PROTOENT_R
7702 #if SANITIZER_INTERCEPT_NETENT
7703 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7705 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7706 struct __sanitizer_netent *n = REAL(getnetent)();
7708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7710 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7712 SIZE_T nn_size = 1; // One handles the trailing \0
7714 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7717 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7722 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7724 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7726 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7727 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7733 SIZE_T nn_size = 1; // One handles the trailing \0
7735 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7736 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7743 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7745 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7746 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7748 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7750 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7752 SIZE_T nn_size = 1; // One handles the trailing \0
7754 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7761 #define INIT_NETENT \
7762 COMMON_INTERCEPT_FUNCTION(getnetent); \
7763 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7764 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7769 #if SANITIZER_INTERCEPT_GETMNTINFO
7770 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7772 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7773 int cnt = REAL(getmntinfo)(mntbufp, flags);
7774 if (cnt > 0 && mntbufp) {
7775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7777 #if SANITIZER_NETBSD
7778 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7785 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7787 #define INIT_GETMNTINFO
7790 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7791 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7794 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7796 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7797 REAL(mi_vector_hash)(key, len, seed, hashes);
7799 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7801 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7803 #define INIT_MI_VECTOR_HASH
7806 #if SANITIZER_INTERCEPT_SETVBUF
7807 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7810 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7811 int ret = REAL(setvbuf)(stream, buf, mode, size);
7813 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7815 unpoison_file(stream);
7819 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7821 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7822 REAL(setbuf)(stream, buf);
7824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7827 unpoison_file(stream);
7830 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
7832 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
7833 REAL(setbuffer)(stream, buf, size);
7835 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7838 unpoison_file(stream);
7841 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7843 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7844 REAL(setlinebuf)(stream);
7846 unpoison_file(stream);
7848 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7849 COMMON_INTERCEPT_FUNCTION(setbuf); \
7850 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7851 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7853 #define INIT_SETVBUF
7856 #if SANITIZER_INTERCEPT_GETVFSSTAT
7857 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7859 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7860 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7862 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7865 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7867 #define INIT_GETVFSSTAT
7870 #if SANITIZER_INTERCEPT_REGEX
7871 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7873 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7875 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
7876 int res = REAL(regcomp)(preg, pattern, cflags);
7878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7881 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7882 struct __sanitizer_regmatch *pmatch[], int eflags) {
7884 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7886 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7888 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
7889 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7894 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7895 SIZE_T errbuf_size) {
7897 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7899 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7900 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7902 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
7905 INTERCEPTOR(void, regfree, const void *preg) {
7907 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7909 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7910 REAL(regfree)(preg);
7912 #define INIT_REGEX \
7913 COMMON_INTERCEPT_FUNCTION(regcomp); \
7914 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
7915 COMMON_INTERCEPT_FUNCTION(regerror); \
7916 COMMON_INTERCEPT_FUNCTION(regfree);
7921 #if SANITIZER_INTERCEPT_REGEXSUB
7922 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7923 const struct __sanitizer_regmatch *rm, const char *str) {
7925 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7927 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
7928 // The implementation demands and hardcodes 10 elements
7930 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7932 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
7933 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
7938 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7939 const struct __sanitizer_regmatch *rm, const char *sstr) {
7941 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7943 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
7944 // Hardcode 10 elements as this is hardcoded size
7946 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7948 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
7949 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7950 if (res > 0 && buf) {
7951 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
7957 #define INIT_REGEXSUB \
7958 COMMON_INTERCEPT_FUNCTION(regnsub); \
7959 COMMON_INTERCEPT_FUNCTION(regasub);
7961 #define INIT_REGEXSUB
7964 #if SANITIZER_INTERCEPT_FTS
7965 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7966 int (*compar)(void **, void **)) {
7968 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
7970 for (char *const *pa = path_argv; ; ++pa) {
7971 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
7974 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
7977 // TODO(kamil): handle compar callback
7978 void *fts = REAL(fts_open)(path_argv, options, compar);
7980 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
7984 INTERCEPTOR(void *, fts_read, void *ftsp) {
7986 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
7988 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7989 void *ftsent = REAL(fts_read)(ftsp);
7991 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7995 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
7997 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
7999 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8000 void *ftsent = REAL(fts_children)(ftsp, options);
8002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8006 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
8008 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
8010 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8012 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
8013 return REAL(fts_set)(ftsp, f, options);
8016 INTERCEPTOR(int, fts_close, void *ftsp) {
8018 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
8020 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8021 return REAL(fts_close)(ftsp);
8024 COMMON_INTERCEPT_FUNCTION(fts_open); \
8025 COMMON_INTERCEPT_FUNCTION(fts_read); \
8026 COMMON_INTERCEPT_FUNCTION(fts_children); \
8027 COMMON_INTERCEPT_FUNCTION(fts_set); \
8028 COMMON_INTERCEPT_FUNCTION(fts_close);
8033 #if SANITIZER_INTERCEPT_SYSCTL
8034 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
8035 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
8037 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8038 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
8039 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
8042 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
8044 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8046 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8047 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
8050 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8052 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8058 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
8059 void *newp, SIZE_T newlen) {
8061 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8062 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
8063 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
8066 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8068 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8070 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8071 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
8074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8076 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8082 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
8085 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
8087 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8089 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8090 int res = REAL(sysctlnametomib)(sname, name, namelenp);
8093 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8095 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8101 #define INIT_SYSCTL \
8102 COMMON_INTERCEPT_FUNCTION(sysctl); \
8103 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
8104 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
8109 #if SANITIZER_INTERCEPT_ASYSCTL
8110 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
8112 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
8114 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
8115 void *res = REAL(asysctl)(name, namelen, len);
8117 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8118 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8123 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
8125 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
8127 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8128 void *res = REAL(asysctlbyname)(sname, len);
8130 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8131 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8135 #define INIT_ASYSCTL \
8136 COMMON_INTERCEPT_FUNCTION(asysctl); \
8137 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
8139 #define INIT_ASYSCTL
8142 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
8143 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
8144 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
8147 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
8150 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8152 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8154 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
8155 // Skip rnode, it's rarely used and not trivial to sanitize
8156 // It's also used mostly internally
8157 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
8160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
8167 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
8172 #define INIT_SYSCTLGETMIBINFO \
8173 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
8175 #define INIT_SYSCTLGETMIBINFO
8178 #if SANITIZER_INTERCEPT_NL_LANGINFO
8179 INTERCEPTOR(char *, nl_langinfo, long item) {
8181 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
8182 char *ret = REAL(nl_langinfo)(item);
8184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8187 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
8189 #define INIT_NL_LANGINFO
8192 #if SANITIZER_INTERCEPT_MODCTL
8193 INTERCEPTOR(int, modctl, int operation, void *argp) {
8196 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
8198 if (operation == modctl_load) {
8200 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
8201 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
8202 if (ml->ml_filename)
8203 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
8204 internal_strlen(ml->ml_filename) + 1);
8206 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
8208 ret = REAL(modctl)(operation, argp);
8209 } else if (operation == modctl_unload) {
8211 const char *name = (const char *)argp;
8212 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
8214 ret = REAL(modctl)(operation, argp);
8215 } else if (operation == modctl_stat) {
8217 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
8219 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
8220 iov_len = iov->iov_len;
8222 ret = REAL(modctl)(operation, argp);
8224 COMMON_INTERCEPTOR_WRITE_RANGE(
8225 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
8226 } else if (operation == modctl_exists) {
8227 ret = REAL(modctl)(operation, argp);
8229 ret = REAL(modctl)(operation, argp);
8234 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
8239 #if SANITIZER_INTERCEPT_STRTONUM
8240 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
8241 long long maxval, const char **errstr) {
8243 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
8245 // TODO(kamil): Implement strtoll as a common inteceptor
8247 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
8248 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
8250 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
8252 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
8254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
8258 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
8260 #define INIT_STRTONUM
8263 #if SANITIZER_INTERCEPT_FPARSELN
8264 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
8265 SIZE_T *lineno, const char delim[3], int flags) {
8267 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
8269 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
8271 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
8272 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
8274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8276 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
8282 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
8284 #define INIT_FPARSELN
8287 #if SANITIZER_INTERCEPT_STATVFS1
8288 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
8290 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
8291 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
8292 int res = REAL(statvfs1)(path, buf, flags);
8293 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8296 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
8298 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
8299 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
8300 int res = REAL(fstatvfs1)(fd, buf, flags);
8302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8304 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
8308 #define INIT_STATVFS1 \
8309 COMMON_INTERCEPT_FUNCTION(statvfs1); \
8310 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
8312 #define INIT_STATVFS1
8315 #if SANITIZER_INTERCEPT_STRTOI
8316 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
8317 INTMAX_T low, INTMAX_T high, int *rstatus) {
8319 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
8321 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
8322 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8328 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
8329 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
8331 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
8333 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
8334 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8339 #define INIT_STRTOI \
8340 COMMON_INTERCEPT_FUNCTION(strtoi); \
8341 COMMON_INTERCEPT_FUNCTION(strtou)
8346 #if SANITIZER_INTERCEPT_CAPSICUM
8347 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
8350 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
8352 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8353 __sanitizer_cap_rights_t *ret = \
8354 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
8356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8360 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
8363 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
8365 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8366 __sanitizer_cap_rights_t *ret = \
8367 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
8369 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8373 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
8376 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
8378 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8379 __sanitizer_cap_rights_t *ret = \
8380 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8382 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8386 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8389 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8391 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8392 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8395 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8396 __sanitizer_cap_rights_t *rights) {
8397 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8400 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8401 __sanitizer_cap_rights_t *rights) {
8402 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8405 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8406 __sanitizer_cap_rights_t *rights) {
8407 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8410 INTERCEPTOR(bool, cap_rights_is_set,
8411 __sanitizer_cap_rights_t *rights) {
8412 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8415 INTERCEPTOR(int, cap_rights_limit, int fd,
8416 const __sanitizer_cap_rights_t *rights) {
8418 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8420 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8422 return REAL(cap_rights_limit)(fd, rights);
8425 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8427 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8428 int ret = REAL(cap_rights_get)(fd, rights);
8430 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8435 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8437 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8439 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8441 return REAL(cap_rights_is_valid(rights));
8444 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8445 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8447 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8449 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8451 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8458 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8459 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8461 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8463 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8465 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8472 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8473 const __sanitizer_cap_rights *little) {
8475 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8477 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8479 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8481 return REAL(cap_rights_contains)(big, little);
8484 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8486 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8488 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8490 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8493 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8495 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8496 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8498 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8502 #define INIT_CAPSICUM \
8503 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8504 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8505 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8506 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8507 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8508 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8509 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8510 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8511 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8512 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8513 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8514 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8516 #define INIT_CAPSICUM
8519 #if SANITIZER_INTERCEPT_SHA1
8520 INTERCEPTOR(void, SHA1Init, void *context) {
8522 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8523 REAL(SHA1Init)(context);
8525 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8527 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8529 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8530 if (data && len > 0)
8531 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8533 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8534 REAL(SHA1Update)(context, data, len);
8536 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8538 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8540 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8542 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8543 REAL(SHA1Final)(digest, context);
8545 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8547 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8549 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8551 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8553 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8554 REAL(SHA1Transform)(state, buffer);
8556 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8558 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8560 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8562 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8563 char *ret = REAL(SHA1End)(context, buf);
8565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8568 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8570 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8572 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8573 char *ret = REAL(SHA1File)(filename, buf);
8575 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8578 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8581 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8583 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8584 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8586 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8589 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8591 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8593 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8594 char *ret = REAL(SHA1Data)(data, len, buf);
8596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8600 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8601 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8602 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8603 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8604 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8605 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8606 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8607 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8612 #if SANITIZER_INTERCEPT_MD4
8613 INTERCEPTOR(void, MD4Init, void *context) {
8615 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8616 REAL(MD4Init)(context);
8618 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8621 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8624 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8625 if (data && len > 0)
8626 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8628 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8629 REAL(MD4Update)(context, data, len);
8631 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8634 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8636 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8638 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8639 REAL(MD4Final)(digest, context);
8641 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8644 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8646 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8648 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8649 char *ret = REAL(MD4End)(context, buf);
8651 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8655 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8657 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8659 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8660 char *ret = REAL(MD4File)(filename, buf);
8662 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8666 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8669 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8670 if (data && len > 0)
8671 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8672 char *ret = REAL(MD4Data)(data, len, buf);
8674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8679 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8680 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8681 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8682 COMMON_INTERCEPT_FUNCTION(MD4End); \
8683 COMMON_INTERCEPT_FUNCTION(MD4File); \
8684 COMMON_INTERCEPT_FUNCTION(MD4Data)
8689 #if SANITIZER_INTERCEPT_RMD160
8690 INTERCEPTOR(void, RMD160Init, void *context) {
8692 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8693 REAL(RMD160Init)(context);
8695 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8697 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8699 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8700 if (data && len > 0)
8701 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8703 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8704 REAL(RMD160Update)(context, data, len);
8706 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8708 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8710 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8712 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8713 REAL(RMD160Final)(digest, context);
8715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8717 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8719 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8721 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8723 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8724 REAL(RMD160Transform)(state, buffer);
8726 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8728 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8730 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8732 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8733 char *ret = REAL(RMD160End)(context, buf);
8735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8738 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8740 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8742 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8743 char *ret = REAL(RMD160File)(filename, buf);
8745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8748 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8751 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8753 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8754 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8756 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8759 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8761 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8762 if (data && len > 0)
8763 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8764 char *ret = REAL(RMD160Data)(data, len, buf);
8766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8769 #define INIT_RMD160 \
8770 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8771 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8772 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8773 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8774 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8775 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8776 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8777 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8782 #if SANITIZER_INTERCEPT_MD5
8783 INTERCEPTOR(void, MD5Init, void *context) {
8785 COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8786 REAL(MD5Init)(context);
8788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8791 INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8794 COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8795 if (data && len > 0)
8796 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8798 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8799 REAL(MD5Update)(context, data, len);
8801 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8804 INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8806 COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8808 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8809 REAL(MD5Final)(digest, context);
8811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8814 INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8816 COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8818 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8819 char *ret = REAL(MD5End)(context, buf);
8821 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8825 INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8827 COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8829 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8830 char *ret = REAL(MD5File)(filename, buf);
8832 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8836 INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8839 COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8840 if (data && len > 0)
8841 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8842 char *ret = REAL(MD5Data)(data, len, buf);
8844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8849 COMMON_INTERCEPT_FUNCTION(MD5Init); \
8850 COMMON_INTERCEPT_FUNCTION(MD5Update); \
8851 COMMON_INTERCEPT_FUNCTION(MD5Final); \
8852 COMMON_INTERCEPT_FUNCTION(MD5End); \
8853 COMMON_INTERCEPT_FUNCTION(MD5File); \
8854 COMMON_INTERCEPT_FUNCTION(MD5Data)
8859 #if SANITIZER_INTERCEPT_FSEEK
8860 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8862 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8863 return REAL(fseek)(stream, offset, whence);
8865 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8867 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8868 return REAL(fseeko)(stream, offset, whence);
8870 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8872 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8873 return REAL(ftell)(stream);
8875 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8877 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8878 return REAL(ftello)(stream);
8880 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8882 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8883 return REAL(rewind)(stream);
8885 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8887 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8888 int ret = REAL(fgetpos)(stream, pos);
8890 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8893 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8895 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8897 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8898 return REAL(fsetpos)(stream, pos);
8900 #define INIT_FSEEK \
8901 COMMON_INTERCEPT_FUNCTION(fseek); \
8902 COMMON_INTERCEPT_FUNCTION(fseeko); \
8903 COMMON_INTERCEPT_FUNCTION(ftell); \
8904 COMMON_INTERCEPT_FUNCTION(ftello); \
8905 COMMON_INTERCEPT_FUNCTION(rewind); \
8906 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8907 COMMON_INTERCEPT_FUNCTION(fsetpos)
8912 #if SANITIZER_INTERCEPT_MD2
8913 INTERCEPTOR(void, MD2Init, void *context) {
8915 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8916 REAL(MD2Init)(context);
8918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8921 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8924 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8925 if (data && len > 0)
8926 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8928 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8929 REAL(MD2Update)(context, data, len);
8931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8934 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8936 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8938 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8939 REAL(MD2Final)(digest, context);
8941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8944 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8946 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8948 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8949 char *ret = REAL(MD2End)(context, buf);
8951 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8955 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8957 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8959 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8960 char *ret = REAL(MD2File)(filename, buf);
8962 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8966 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8969 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8970 if (data && len > 0)
8971 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8972 char *ret = REAL(MD2Data)(data, len, buf);
8974 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8979 COMMON_INTERCEPT_FUNCTION(MD2Init); \
8980 COMMON_INTERCEPT_FUNCTION(MD2Update); \
8981 COMMON_INTERCEPT_FUNCTION(MD2Final); \
8982 COMMON_INTERCEPT_FUNCTION(MD2End); \
8983 COMMON_INTERCEPT_FUNCTION(MD2File); \
8984 COMMON_INTERCEPT_FUNCTION(MD2Data)
8989 #if SANITIZER_INTERCEPT_SHA2
8990 #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
8991 INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
8993 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
8994 REAL(SHA##LEN##_Init)(context); \
8996 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8998 INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
8999 const u8 *data, SIZE_T len) { \
9001 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
9002 if (data && len > 0) \
9003 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
9005 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9006 REAL(SHA##LEN##_Update)(context, data, len); \
9008 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9010 INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
9013 CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
9014 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
9016 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9017 REAL(SHA##LEN##_Final)(digest, context); \
9019 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
9020 sizeof(digest[0]) * \
9021 SHA##LEN##_digest_length); \
9023 INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
9025 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
9027 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
9028 char *ret = REAL(SHA##LEN##_End)(context, buf); \
9030 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9033 INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
9035 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
9037 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
9038 char *ret = REAL(SHA##LEN##_File)(filename, buf); \
9040 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9043 INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
9044 OFF_T offset, OFF_T length) { \
9046 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
9049 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
9050 char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
9052 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9055 INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
9057 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
9058 if (data && len > 0) \
9059 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
9060 char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
9062 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
9066 SHA2_INTERCEPTORS(224, u32)
9067 SHA2_INTERCEPTORS(256, u32)
9068 SHA2_INTERCEPTORS(384, u64)
9069 SHA2_INTERCEPTORS(512, u64)
9071 #define INIT_SHA2_INTECEPTORS(LEN) \
9072 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
9073 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
9074 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
9075 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
9076 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
9077 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
9078 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
9081 INIT_SHA2_INTECEPTORS(224); \
9082 INIT_SHA2_INTECEPTORS(256); \
9083 INIT_SHA2_INTECEPTORS(384); \
9084 INIT_SHA2_INTECEPTORS(512)
9085 #undef SHA2_INTERCEPTORS
9090 #if SANITIZER_INTERCEPT_VIS
9091 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
9093 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
9094 char *end = REAL(vis)(dst, c, flag, nextc);
9095 // dst is NULL terminated and end points to the NULL char
9097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9100 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
9102 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
9103 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
9104 // nvis cannot make sure the dst is NULL terminated
9106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9109 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
9111 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
9113 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9114 int len = REAL(strvis)(dst, src, flag);
9116 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9119 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
9121 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
9123 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9124 int len = REAL(stravis)(dst, src, flag);
9126 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
9128 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
9132 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
9134 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
9136 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9137 int len = REAL(strnvis)(dst, dlen, src, flag);
9138 // The interface will be valid even if there is no space for NULL char
9140 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9143 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
9145 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
9147 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9148 int ret = REAL(strvisx)(dst, src, len, flag);
9150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9153 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9156 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
9158 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9159 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
9160 if (dst && ret >= 0)
9161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9164 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9165 int flag, int *cerr_ptr) {
9167 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
9169 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9170 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9171 // according to the implementation
9173 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9174 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
9175 if (dst && ret >= 0)
9176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9181 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
9182 const char *extra) {
9184 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
9186 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9187 char *end = REAL(svis)(dst, c, flag, nextc, extra);
9189 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9192 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
9193 const char *extra) {
9195 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
9197 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9198 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
9200 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
9201 Min((SIZE_T)(end - dst + 1), dlen));
9204 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
9205 const char *extra) {
9207 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
9209 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9211 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9212 int len = REAL(strsvis)(dst, src, flag, extra);
9214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9217 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
9218 const char *extra) {
9220 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
9222 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9224 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9225 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
9226 // The interface will be valid even if there is no space for NULL char
9227 if (dst && len >= 0)
9228 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9231 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
9232 const char *extra) {
9234 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
9236 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9238 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9239 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
9241 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9244 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9245 int flag, const char *extra) {
9247 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
9249 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9251 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9252 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
9253 if (dst && ret >= 0)
9254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9257 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
9258 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
9260 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
9263 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9265 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9266 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9267 // according to the implementation
9269 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9270 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
9271 if (dst && ret >= 0)
9272 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9277 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
9279 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
9281 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
9282 int ret = REAL(unvis)(cp, c, astate, flag);
9283 if (ret == unvis_valid || ret == unvis_validpush) {
9284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
9288 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
9290 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
9292 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9293 int ret = REAL(strunvis)(dst, src);
9295 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9298 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
9300 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
9302 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9303 int ret = REAL(strnunvis)(dst, dlen, src);
9305 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9308 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
9310 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
9312 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9313 int ret = REAL(strunvisx)(dst, src, flag);
9315 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9318 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
9321 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
9323 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9324 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
9326 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9330 COMMON_INTERCEPT_FUNCTION(vis); \
9331 COMMON_INTERCEPT_FUNCTION(nvis); \
9332 COMMON_INTERCEPT_FUNCTION(strvis); \
9333 COMMON_INTERCEPT_FUNCTION(stravis); \
9334 COMMON_INTERCEPT_FUNCTION(strnvis); \
9335 COMMON_INTERCEPT_FUNCTION(strvisx); \
9336 COMMON_INTERCEPT_FUNCTION(strnvisx); \
9337 COMMON_INTERCEPT_FUNCTION(strenvisx); \
9338 COMMON_INTERCEPT_FUNCTION(svis); \
9339 COMMON_INTERCEPT_FUNCTION(snvis); \
9340 COMMON_INTERCEPT_FUNCTION(strsvis); \
9341 COMMON_INTERCEPT_FUNCTION(strsnvis); \
9342 COMMON_INTERCEPT_FUNCTION(strsvisx); \
9343 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
9344 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
9345 COMMON_INTERCEPT_FUNCTION(unvis); \
9346 COMMON_INTERCEPT_FUNCTION(strunvis); \
9347 COMMON_INTERCEPT_FUNCTION(strnunvis); \
9348 COMMON_INTERCEPT_FUNCTION(strunvisx); \
9349 COMMON_INTERCEPT_FUNCTION(strnunvisx)
9354 #if SANITIZER_INTERCEPT_CDB
9355 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
9357 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
9359 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9360 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
9362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9366 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
9367 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
9369 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
9372 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
9373 struct __sanitizer_cdbr *cdbr =
9374 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
9376 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9380 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9382 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9384 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9385 return REAL(cdbr_entries)(cdbr);
9388 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9389 const void **data, SIZE_T *datalen) {
9391 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9393 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9394 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9397 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9400 if (data && datalen)
9401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9406 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9407 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9409 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9411 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9413 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9414 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9417 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9420 if (data && datalen)
9421 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9426 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9428 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9430 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9431 REAL(cdbr_close)(cdbr);
9434 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9436 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9437 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9439 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9443 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9444 SIZE_T keylen, const void *data, SIZE_T datalen) {
9446 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9448 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9449 if (data && datalen)
9450 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9452 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9453 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9455 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9459 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9460 SIZE_T datalen, u32 *index) {
9462 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9464 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9465 if (data && datalen)
9466 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9467 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9470 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9472 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9477 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9478 SIZE_T keylen, u32 index) {
9480 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9482 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9484 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9485 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9487 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9491 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9492 const char descr[16], u32 (*seedgen)(void)) {
9494 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9495 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9497 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9499 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9501 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9502 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9505 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9507 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9512 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9514 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9516 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9517 REAL(cdbw_close)(cdbw);
9521 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9522 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9523 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9524 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9525 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9526 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9527 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9528 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9529 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9530 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9531 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9532 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9537 #if SANITIZER_INTERCEPT_GETFSENT
9538 INTERCEPTOR(void *, getfsent) {
9540 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9541 void *ret = REAL(getfsent)();
9543 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9547 INTERCEPTOR(void *, getfsspec, const char *spec) {
9549 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9551 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
9552 void *ret = REAL(getfsspec)(spec);
9554 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9558 INTERCEPTOR(void *, getfsfile, const char *file) {
9560 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9562 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
9563 void *ret = REAL(getfsfile)(file);
9565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9569 #define INIT_GETFSENT \
9570 COMMON_INTERCEPT_FUNCTION(getfsent); \
9571 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9572 COMMON_INTERCEPT_FUNCTION(getfsfile);
9574 #define INIT_GETFSENT
9577 #if SANITIZER_INTERCEPT_ARC4RANDOM
9578 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9580 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9581 REAL(arc4random_buf)(buf, len);
9583 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9586 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9588 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9590 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9591 REAL(arc4random_addrandom)(dat, datlen);
9594 #define INIT_ARC4RANDOM \
9595 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9596 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9598 #define INIT_ARC4RANDOM
9601 #if SANITIZER_INTERCEPT_POPEN
9602 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9604 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9606 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
9608 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9609 __sanitizer_FILE *res = REAL(popen)(command, type);
9610 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9611 if (res) unpoison_file(res);
9614 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9619 #if SANITIZER_INTERCEPT_POPENVE
9620 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9621 char *const *argv, char *const *envp, const char *type) {
9623 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9625 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9627 for (char *const *pa = argv; ; ++pa) {
9628 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9631 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9635 for (char *const *pa = envp; ; ++pa) {
9636 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9639 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9643 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9644 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9645 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9646 if (res) unpoison_file(res);
9649 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9651 #define INIT_POPENVE
9654 #if SANITIZER_INTERCEPT_PCLOSE
9655 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9657 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9658 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9659 const FileMetadata *m = GetInterceptorMetadata(fp);
9660 int res = REAL(pclose)(fp);
9662 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9663 DeleteInterceptorMetadata(fp);
9667 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9672 #if SANITIZER_INTERCEPT_FUNOPEN
9673 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9674 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9675 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9676 typedef int (*funopen_closefn)(void *cookie);
9678 struct WrappedFunopenCookie {
9680 funopen_readfn real_read;
9681 funopen_writefn real_write;
9682 funopen_seekfn real_seek;
9683 funopen_closefn real_close;
9686 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9687 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9688 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9689 funopen_readfn real_read = wrapped_cookie->real_read;
9690 return real_read(wrapped_cookie->real_cookie, buf, len);
9693 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9694 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9695 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9696 funopen_writefn real_write = wrapped_cookie->real_write;
9697 return real_write(wrapped_cookie->real_cookie, buf, len);
9700 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9701 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9702 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9703 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9704 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9707 static int wrapped_funopen_close(void *cookie) {
9708 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9709 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9710 funopen_closefn real_close = wrapped_cookie->real_close;
9711 int res = real_close(wrapped_cookie->real_cookie);
9712 InternalFree(wrapped_cookie);
9716 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9717 funopen_writefn writefn, funopen_seekfn seekfn,
9718 funopen_closefn closefn) {
9720 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9723 WrappedFunopenCookie *wrapped_cookie =
9724 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9725 wrapped_cookie->real_cookie = cookie;
9726 wrapped_cookie->real_read = readfn;
9727 wrapped_cookie->real_write = writefn;
9728 wrapped_cookie->real_seek = seekfn;
9729 wrapped_cookie->real_close = closefn;
9731 __sanitizer_FILE *res =
9732 REAL(funopen)(wrapped_cookie,
9733 readfn ? wrapped_funopen_read : nullptr,
9734 writefn ? wrapped_funopen_write : nullptr,
9735 seekfn ? wrapped_funopen_seek : nullptr,
9736 closefn ? wrapped_funopen_close : nullptr);
9741 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9743 #define INIT_FUNOPEN
9746 #if SANITIZER_INTERCEPT_FUNOPEN2
9747 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9748 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9749 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9750 typedef int (*funopen2_flushfn)(void *cookie);
9751 typedef int (*funopen2_closefn)(void *cookie);
9753 struct WrappedFunopen2Cookie {
9755 funopen2_readfn real_read;
9756 funopen2_writefn real_write;
9757 funopen2_seekfn real_seek;
9758 funopen2_flushfn real_flush;
9759 funopen2_closefn real_close;
9762 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9763 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9764 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9765 funopen2_readfn real_read = wrapped_cookie->real_read;
9766 return real_read(wrapped_cookie->real_cookie, buf, len);
9769 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9771 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9772 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9773 funopen2_writefn real_write = wrapped_cookie->real_write;
9774 return real_write(wrapped_cookie->real_cookie, buf, len);
9777 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9778 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9779 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9780 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9781 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9784 static int wrapped_funopen2_flush(void *cookie) {
9785 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9786 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9787 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9788 return real_flush(wrapped_cookie->real_cookie);
9791 static int wrapped_funopen2_close(void *cookie) {
9792 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9793 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9794 funopen2_closefn real_close = wrapped_cookie->real_close;
9795 int res = real_close(wrapped_cookie->real_cookie);
9796 InternalFree(wrapped_cookie);
9800 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9801 funopen2_writefn writefn, funopen2_seekfn seekfn,
9802 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9804 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9807 WrappedFunopen2Cookie *wrapped_cookie =
9808 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9809 wrapped_cookie->real_cookie = cookie;
9810 wrapped_cookie->real_read = readfn;
9811 wrapped_cookie->real_write = writefn;
9812 wrapped_cookie->real_seek = seekfn;
9813 wrapped_cookie->real_flush = flushfn;
9814 wrapped_cookie->real_close = closefn;
9816 __sanitizer_FILE *res =
9817 REAL(funopen2)(wrapped_cookie,
9818 readfn ? wrapped_funopen2_read : nullptr,
9819 writefn ? wrapped_funopen2_write : nullptr,
9820 seekfn ? wrapped_funopen2_seek : nullptr,
9821 flushfn ? wrapped_funopen2_flush : nullptr,
9822 closefn ? wrapped_funopen2_close : nullptr);
9827 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9829 #define INIT_FUNOPEN2
9832 #if SANITIZER_INTERCEPT_FDEVNAME
9833 INTERCEPTOR(char *, fdevname, int fd) {
9835 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9836 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9837 char *name = REAL(fdevname)(fd);
9839 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
9841 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9846 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9848 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9849 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9850 char *name = REAL(fdevname_r)(fd, buf, len);
9851 if (name && buf && len > 0) {
9852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
9854 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9859 #define INIT_FDEVNAME \
9860 COMMON_INTERCEPT_FUNCTION(fdevname); \
9861 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9863 #define INIT_FDEVNAME
9866 #if SANITIZER_INTERCEPT_GETUSERSHELL
9867 INTERCEPTOR(char *, getusershell) {
9869 COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9870 char *res = REAL(getusershell)();
9872 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9876 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9878 #define INIT_GETUSERSHELL
9881 #if SANITIZER_INTERCEPT_SL_INIT
9882 INTERCEPTOR(void *, sl_init) {
9884 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9885 void *res = REAL(sl_init)();
9887 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9891 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9893 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9895 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9897 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9898 int res = REAL(sl_add)(sl, item);
9900 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9904 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9906 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9908 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9910 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9911 char *res = REAL(sl_find)(sl, item);
9913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9917 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9919 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9921 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9922 REAL(sl_free)(sl, freeall);
9925 #define INIT_SL_INIT \
9926 COMMON_INTERCEPT_FUNCTION(sl_init); \
9927 COMMON_INTERCEPT_FUNCTION(sl_add); \
9928 COMMON_INTERCEPT_FUNCTION(sl_find); \
9929 COMMON_INTERCEPT_FUNCTION(sl_free);
9931 #define INIT_SL_INIT
9934 #if SANITIZER_INTERCEPT_GETRANDOM
9935 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
9937 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
9938 SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
9940 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
9944 #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
9946 #define INIT_GETRANDOM
9949 #if SANITIZER_INTERCEPT_GETENTROPY
9950 INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
9952 COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
9953 int r = REAL(getentropy)(buf, buflen);
9955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
9959 #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
9961 #define INIT_GETENTROPY
9964 #if SANITIZER_INTERCEPT_QSORT_R
9965 typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
9966 struct qsort_r_compar_params {
9968 qsort_r_compar_f compar;
9971 static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
9972 qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
9973 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9974 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
9975 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
9976 return params->compar(a, b, params->arg);
9979 INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
9980 qsort_r_compar_f compar, void *arg) {
9982 COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
9983 // Run the comparator over all array elements to detect any memory issues.
9985 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
9986 void *p = (void *)((char *)base + i * size);
9987 void *q = (void *)((char *)base + (i + 1) * size);
9988 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9992 qsort_r_compar_params params = {size, compar, arg};
9993 REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, ¶ms);
9994 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
9996 # define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
9998 # define INIT_QSORT_R
10001 #if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
10002 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10003 qsort_r_compar_f compar) {
10005 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10006 WRAP(qsort_r)(base, nmemb, size, compar, nullptr);
10008 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10009 #elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
10010 // Glibc qsort uses a temporary buffer allocated either on stack or on heap.
10011 // Poisoned memory from there may get copied into the comparator arguments,
10012 // where it needs to be dealt with. But even that is not enough - the results of
10013 // the sort may be copied into the input/output array based on the results of
10014 // the comparator calls, but directly from the temp memory, bypassing the
10015 // unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
10016 // unpoisoning the entire array after the sort is done.
10018 // We can not check that the entire array is initialized at the beginning. IMHO,
10019 // it's fine for parts of the sorted objects to contain uninitialized memory,
10020 // ex. as padding in structs.
10021 typedef int (*qsort_compar_f)(const void *, const void *);
10022 static THREADLOCAL qsort_compar_f qsort_compar;
10023 static THREADLOCAL SIZE_T qsort_size;
10024 static int wrapped_qsort_compar(const void *a, const void *b) {
10025 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10026 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
10027 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
10028 return qsort_compar(a, b);
10031 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10032 qsort_compar_f compar) {
10034 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10035 // Run the comparator over all array elements to detect any memory issues.
10037 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
10038 void *p = (void *)((char *)base + i * size);
10039 void *q = (void *)((char *)base + (i + 1) * size);
10040 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10044 qsort_compar_f old_compar = qsort_compar;
10045 SIZE_T old_size = qsort_size;
10046 // Handle qsort() implementations that recurse using an
10047 // interposable function call:
10048 bool already_wrapped = compar == wrapped_qsort_compar;
10049 if (already_wrapped) {
10050 // This case should only happen if the qsort() implementation calls itself
10051 // using a preemptible function call (e.g. the FreeBSD libc version).
10052 // Check that the size and comparator arguments are as expected.
10053 CHECK_NE(compar, qsort_compar);
10054 CHECK_EQ(qsort_size, size);
10056 qsort_compar = compar;
10059 REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
10060 if (!already_wrapped) {
10061 qsort_compar = old_compar;
10062 qsort_size = old_size;
10064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
10066 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10068 # define INIT_QSORT
10071 #if SANITIZER_INTERCEPT_BSEARCH
10072 typedef int (*bsearch_compar_f)(const void *, const void *);
10073 struct bsearch_compar_params {
10075 bsearch_compar_f compar;
10078 static int wrapped_bsearch_compar(const void *key, const void *b) {
10079 const bsearch_compar_params *params = (const bsearch_compar_params *)key;
10080 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10081 return params->compar(params->key, b);
10084 INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
10085 SIZE_T size, bsearch_compar_f compar) {
10087 COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
10088 bsearch_compar_params params = {key, compar};
10089 return REAL(bsearch)(¶ms, base, nmemb, size, wrapped_bsearch_compar);
10091 # define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
10093 # define INIT_BSEARCH
10096 #if SANITIZER_INTERCEPT_SIGALTSTACK
10097 INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
10099 COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
10100 int r = REAL(sigaltstack)(ss, oss);
10101 if (r == 0 && oss != nullptr) {
10102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
10106 #define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
10108 #define INIT_SIGALTSTACK
10111 #if SANITIZER_INTERCEPT_PROCCTL
10112 INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
10114 COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
10115 static const int PROC_REAP_ACQUIRE = 2;
10116 static const int PROC_REAP_RELEASE = 3;
10117 static const int PROC_REAP_STATUS = 4;
10118 static const int PROC_REAP_GETPIDS = 5;
10119 static const int PROC_REAP_KILL = 6;
10120 if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
10121 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
10123 // reap_acquire/reap_release bears no arguments.
10124 if (cmd > PROC_REAP_RELEASE) {
10125 unsigned int reapsz;
10127 case PROC_REAP_STATUS:
10128 reapsz = struct_procctl_reaper_status_sz;
10130 case PROC_REAP_GETPIDS:
10131 reapsz = struct_procctl_reaper_pids_sz;
10133 case PROC_REAP_KILL:
10134 reapsz = struct_procctl_reaper_kill_sz;
10137 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
10140 return REAL(procctl)(idtype, id, cmd, data);
10142 #define INIT_PROCCTL COMMON_INTERCEPT_FUNCTION(procctl)
10144 #define INIT_PROCCTL
10147 #if SANITIZER_INTERCEPT_UNAME
10148 INTERCEPTOR(int, uname, struct utsname *utsname) {
10149 #if SANITIZER_LINUX
10150 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
10151 return internal_uname(utsname);
10154 COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
10155 int res = REAL(uname)(utsname);
10157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10158 __sanitizer::struct_utsname_sz);
10161 #define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
10166 #if SANITIZER_INTERCEPT___XUNAME
10167 // FreeBSD's <sys/utsname.h> define uname() as
10168 // static __inline int uname(struct utsname *name) {
10169 // return __xuname(SYS_NMLN, (void*)name);
10171 INTERCEPTOR(int, __xuname, int size, void *utsname) {
10173 COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
10174 int res = REAL(__xuname)(size, utsname);
10176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10177 __sanitizer::struct_utsname_sz);
10180 #define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
10182 #define INIT___XUNAME
10185 #if SANITIZER_INTERCEPT_HEXDUMP
10186 INTERCEPTOR(void, hexdump, const void *ptr, int length, const char *header, int flags) {
10188 COMMON_INTERCEPTOR_ENTER(ctx, hexdump, ptr, length, header, flags);
10189 COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, length);
10190 COMMON_INTERCEPTOR_READ_RANGE(ctx, header, internal_strlen(header) + 1);
10191 REAL(hexdump)(ptr, length, header, flags);
10194 #define INIT_HEXDUMP COMMON_INTERCEPT_FUNCTION(hexdump);
10196 #define INIT_HEXDUMP
10199 #if SANITIZER_INTERCEPT_ARGP_PARSE
10200 INTERCEPTOR(int, argp_parse, const struct argp *argp, int argc, char **argv,
10201 unsigned flags, int *arg_index, void *input) {
10203 COMMON_INTERCEPTOR_ENTER(ctx, argp_parse, argp, argc, argv, flags, arg_index,
10205 for (int i = 0; i < argc; i++)
10206 COMMON_INTERCEPTOR_READ_RANGE(ctx, argv[i], internal_strlen(argv[i]) + 1);
10207 int res = REAL(argp_parse)(argp, argc, argv, flags, arg_index, input);
10208 if (!res && arg_index)
10209 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg_index, sizeof(int));
10213 #define INIT_ARGP_PARSE COMMON_INTERCEPT_FUNCTION(argp_parse);
10215 #define INIT_ARGP_PARSE
10218 #if SANITIZER_INTERCEPT_CPUSET_GETAFFINITY
10219 INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T cpusetsize, __sanitizer_cpuset_t *mask) {
10221 COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getaffinity, level, which, id, cpusetsize, mask);
10222 int res = REAL(cpuset_getaffinity)(level, which, id, cpusetsize, mask);
10223 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
10226 #define INIT_CPUSET_GETAFFINITY COMMON_INTERCEPT_FUNCTION(cpuset_getaffinity);
10228 #define INIT_CPUSET_GETAFFINITY
10231 #include "sanitizer_common_interceptors_netbsd_compat.inc"
10233 namespace __sanitizer {
10234 void InitializeMemintrinsicInterceptors();
10235 } // namespace __sanitizer
10237 static void InitializeCommonInterceptors() {
10239 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
10240 interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
10243 __sanitizer::InitializeMemintrinsicInterceptors();
10289 INIT_LOCALTIME_AND_FRIENDS;
10295 INIT_ISOC99_PRINTF;
10297 INIT_FREXPF_FREXPL;
10298 INIT_GETPWNAM_AND_FRIENDS;
10299 INIT_GETPWNAM_R_AND_FRIENDS;
10306 INIT_CLOCK_GETTIME;
10307 INIT_CLOCK_GETCPUCLOCKID;
10313 INIT_DN_COMP_EXPAND;
10318 INIT_PTHREAD_GETSCHEDPARAM;
10322 INIT_GETHOSTBYNAME;
10323 INIT_GETHOSTBYNAME2;
10324 INIT_GETHOSTBYNAME_R;
10325 INIT_GETHOSTBYNAME2_R;
10326 INIT_GETHOSTBYADDR_R;
10347 INIT_GET_CURRENT_DIR_NAME;
10349 INIT_STRTOIMAX_C23;
10358 INIT_CANONICALIZE_FILE_NAME;
10360 INIT_SCHED_GETAFFINITY;
10361 INIT_SCHED_GETPARAM;
10364 INIT_XPG_STRERROR_R;
10375 INIT_SIGSET_LOGICOPS;
10378 INIT_PTHREAD_SIGMASK;
10381 INIT___LIBC_THR_SETCANCELSTATE;
10389 INIT_ETHER_NTOA_ATON;
10394 INIT_PTHREAD_ATTR_GET;
10395 INIT_PTHREAD_ATTR_GET_SCHED;
10396 INIT_PTHREAD_ATTR_GETINHERITSCHED;
10397 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
10398 INIT_PTHREAD_GETAFFINITY_NP;
10399 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
10400 INIT_PTHREAD_MUTEXATTR_GETTYPE;
10401 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
10402 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
10403 INIT_PTHREAD_MUTEXATTR_GETROBUST;
10404 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
10405 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
10406 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
10407 INIT_PTHREAD_CONDATTR_GETPSHARED;
10408 INIT_PTHREAD_CONDATTR_GETCLOCK;
10409 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
10417 INIT_PTHREAD_SETNAME_NP;
10418 INIT_PTHREAD_GETNAME_NP;
10436 INIT_IF_INDEXTONAME;
10442 INIT_LIBIO_INTERNALS;
10446 INIT_OPEN_MEMSTREAM;
10450 INIT_DLOPEN_DLCLOSE;
10456 INIT_PTHREAD_SETCANCEL;
10458 INIT_PROCESS_VM_READV;
10461 INIT_RECV_RECVFROM;
10465 INIT_EVENTFD_READ_WRITE;
10472 // FIXME: add other *stat interceptors.
10482 INIT_USER_FROM_UID;
10483 INIT_UID_FROM_USER;
10484 INIT_GROUP_FROM_GID;
10485 INIT_GID_FROM_GROUP;
10489 INIT_GETGROUPMEMBERSHIP;
10492 INIT_NAME_TO_HANDLE_AT;
10493 INIT_OPEN_BY_HANDLE_AT;
10504 INIT_MI_VECTOR_HASH;
10512 INIT_SYSCTLGETMIBINFO;
10550 INIT_CPUSET_GETAFFINITY;