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_ON_DLOPEN
25 // COMMON_INTERCEPTOR_ON_EXIT
26 // COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
27 // COMMON_INTERCEPTOR_MUTEX_POST_LOCK
28 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
29 // COMMON_INTERCEPTOR_MUTEX_REPAIR
30 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
31 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
32 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
33 // COMMON_INTERCEPTOR_MEMSET_IMPL
34 // COMMON_INTERCEPTOR_MEMMOVE_IMPL
35 // COMMON_INTERCEPTOR_MEMCPY_IMPL
36 // COMMON_INTERCEPTOR_MMAP_IMPL
37 // COMMON_INTERCEPTOR_COPY_STRING
38 // COMMON_INTERCEPTOR_STRNDUP_IMPL
39 // COMMON_INTERCEPTOR_STRERROR
40 //===----------------------------------------------------------------------===//
42 #include "interception/interception.h"
43 #include "sanitizer_addrhashmap.h"
44 #include "sanitizer_errno.h"
45 #include "sanitizer_placement_new.h"
46 #include "sanitizer_platform_interceptors.h"
47 #include "sanitizer_symbolizer.h"
48 #include "sanitizer_tls_get_addr.h"
52 #if SANITIZER_INTERCEPTOR_HOOKS
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
54 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
55 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
57 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
58 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
60 #endif // SANITIZER_INTERCEPTOR_HOOKS
62 #if SANITIZER_WINDOWS && !defined(va_copy)
63 #define va_copy(dst, src) ((dst) = (src))
67 #define pthread_setname_np pthread_set_name_np
68 #define inet_aton __inet_aton
69 #define inet_pton __inet_pton
70 #define iconv __bsd_iconv
74 #define clock_getres __clock_getres50
75 #define clock_gettime __clock_gettime50
76 #define clock_settime __clock_settime50
77 #define ctime __ctime50
78 #define ctime_r __ctime_r50
79 #define devname __devname50
80 #define fgetpos __fgetpos50
81 #define fsetpos __fsetpos50
82 #define fts_children __fts_children60
83 #define fts_close __fts_close60
84 #define fts_open __fts_open60
85 #define fts_read __fts_read60
86 #define fts_set __fts_set60
87 #define getitimer __getitimer50
88 #define getmntinfo __getmntinfo13
89 #define getpwent __getpwent50
90 #define getpwnam __getpwnam50
91 #define getpwnam_r __getpwnam_r50
92 #define getpwuid __getpwuid50
93 #define getpwuid_r __getpwuid_r50
94 #define getutent __getutent50
95 #define getutxent __getutxent50
96 #define getutxid __getutxid50
97 #define getutxline __getutxline50
98 #define pututxline __pututxline50
100 #define gmtime __gmtime50
101 #define gmtime_r __gmtime_r50
102 #define localtime __locatime50
103 #define localtime_r __localtime_r50
104 #define mktime __mktime50
105 #define lstat __lstat50
106 #define opendir __opendir30
107 #define readdir __readdir30
108 #define readdir_r __readdir_r30
109 #define scandir __scandir30
110 #define setitimer __setitimer50
111 #define setlocale __setlocale50
112 #define shmctl __shmctl50
113 #define sigemptyset __sigemptyset14
114 #define sigfillset __sigfillset14
115 #define sigpending __sigpending14
116 #define sigprocmask __sigprocmask14
117 #define sigtimedwait __sigtimedwait50
118 #define stat __stat50
119 #define 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 // Platform-specific options.
131 namespace __sanitizer {
132 bool PlatformHasDifferentMemcpyAndMemmove();
134 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
135 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
136 #elif SANITIZER_WINDOWS64
137 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
139 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
140 #endif // SANITIZER_MAC
142 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
143 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
146 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
147 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
150 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
151 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
154 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
155 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
158 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
159 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
162 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
163 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
166 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
167 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
170 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
171 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
174 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
175 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
178 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
179 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
182 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
183 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
186 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
187 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
190 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
191 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
194 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
195 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
196 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
199 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
200 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
203 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
204 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
205 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
207 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
208 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
209 CheckNoDeepBind(filename, flag);
212 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
213 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
216 #ifndef COMMON_INTERCEPTOR_ACQUIRE
217 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
220 #ifndef COMMON_INTERCEPTOR_RELEASE
221 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
224 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
225 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
228 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
229 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
232 #ifdef SANITIZER_NLDBL_VERSION
233 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
234 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
236 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
237 COMMON_INTERCEPT_FUNCTION(fn)
240 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
241 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
243 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
244 return internal_memset(dst, v, size); \
245 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
246 if (common_flags()->intercept_intrin) \
247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
248 return REAL(memset)(dst, v, size); \
252 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
253 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
255 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
256 return internal_memmove(dst, src, size); \
257 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
258 if (common_flags()->intercept_intrin) { \
259 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
260 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
262 return REAL(memmove)(dst, src, size); \
266 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
267 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
269 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
270 return internal_memmove(dst, src, size); \
272 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
273 if (common_flags()->intercept_intrin) { \
274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
275 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
277 return REAL(memcpy)(dst, src, size); \
281 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
282 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
284 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
287 #ifndef COMMON_INTERCEPTOR_COPY_STRING
288 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
291 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
292 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
293 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
294 uptr copy_length = internal_strnlen(s, size); \
295 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
296 if (common_flags()->intercept_strndup) { \
297 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
299 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
300 internal_memcpy(new_mem, s, copy_length); \
301 new_mem[copy_length] = '\0'; \
305 #ifndef COMMON_INTERCEPTOR_STRERROR
306 #define COMMON_INTERCEPTOR_STRERROR() {}
309 struct FileMetadata {
310 // For open_memstream().
315 struct CommonInterceptorMetadata {
326 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
328 static MetadataHashMap *interceptor_metadata_map;
330 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
331 const FileMetadata &file) {
332 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
334 h->type = CommonInterceptorMetadata::CIMT_FILE;
338 UNUSED static const FileMetadata *GetInterceptorMetadata(
339 __sanitizer_FILE *addr) {
340 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
343 if (addr && h.exists()) {
345 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
352 UNUSED static void DeleteInterceptorMetadata(void *addr) {
353 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
358 #if SANITIZER_INTERCEPT_STRLEN
359 INTERCEPTOR(SIZE_T, strlen, const char *s) {
360 // Sometimes strlen is called prior to InitializeCommonInterceptors,
361 // in which case the REAL(strlen) typically used in
362 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
364 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
365 return internal_strlen(s);
367 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
368 SIZE_T result = REAL(strlen)(s);
369 if (common_flags()->intercept_strlen)
370 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
373 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
378 #if SANITIZER_INTERCEPT_STRNLEN
379 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
381 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
382 SIZE_T length = REAL(strnlen)(s, maxlen);
383 if (common_flags()->intercept_strlen)
384 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
387 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
392 #if SANITIZER_INTERCEPT_STRNDUP
393 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
395 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
397 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
400 #endif // SANITIZER_INTERCEPT_STRNDUP
402 #if SANITIZER_INTERCEPT___STRNDUP
403 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
405 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
407 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
409 #define INIT___STRNDUP
410 #endif // SANITIZER_INTERCEPT___STRNDUP
412 #if SANITIZER_INTERCEPT_TEXTDOMAIN
413 INTERCEPTOR(char*, textdomain, const char *domainname) {
415 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
416 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
417 char *domain = REAL(textdomain)(domainname);
419 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
423 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
425 #define INIT_TEXTDOMAIN
428 #if SANITIZER_INTERCEPT_STRCMP
429 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
430 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
433 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
434 const char *s1, const char *s2, int result)
436 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
438 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
439 unsigned char c1, c2;
442 c1 = (unsigned char)s1[i];
443 c2 = (unsigned char)s2[i];
444 if (c1 != c2 || c1 == '\0') break;
446 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
447 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
448 int result = CharCmpX(c1, c2);
449 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
454 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
455 const char *s1, const char *s2, uptr n,
458 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
459 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
460 return internal_strncmp(s1, s2, size);
462 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
463 unsigned char c1 = 0, c2 = 0;
465 for (i = 0; i < size; i++) {
466 c1 = (unsigned char)s1[i];
467 c2 = (unsigned char)s2[i];
468 if (c1 != c2 || c1 == '\0') break;
472 if (common_flags()->strict_string_checks) {
473 for (; i1 < size && s1[i1]; i1++) {}
474 for (; i2 < size && s2[i2]; i2++) {}
476 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
477 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
478 int result = CharCmpX(c1, c2);
479 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
484 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
485 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
491 #if SANITIZER_INTERCEPT_STRCASECMP
492 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
493 int c1_low = ToLower(c1);
494 int c2_low = ToLower(c2);
495 return c1_low - c2_low;
498 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
499 const char *s1, const char *s2, int result)
501 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
503 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
504 unsigned char c1 = 0, c2 = 0;
507 c1 = (unsigned char)s1[i];
508 c2 = (unsigned char)s2[i];
509 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
511 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
512 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
513 int result = CharCaseCmp(c1, c2);
514 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
519 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
520 const char *s1, const char *s2, uptr size,
523 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
525 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
526 unsigned char c1 = 0, c2 = 0;
528 for (i = 0; i < size; i++) {
529 c1 = (unsigned char)s1[i];
530 c2 = (unsigned char)s2[i];
531 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
535 if (common_flags()->strict_string_checks) {
536 for (; i1 < size && s1[i1]; i1++) {}
537 for (; i2 < size && s2[i2]; i2++) {}
539 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
540 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
541 int result = CharCaseCmp(c1, c2);
542 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
543 s1, s2, size, result);
547 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
548 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
550 #define INIT_STRCASECMP
551 #define INIT_STRNCASECMP
554 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
555 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
557 uptr len1 = REAL(strlen)(s1);
558 uptr len2 = REAL(strlen)(s2);
559 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
560 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
564 #if SANITIZER_INTERCEPT_STRSTR
566 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
567 const char *s1, const char *s2, char *result)
569 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
570 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
571 return internal_strstr(s1, s2);
573 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
574 char *r = REAL(strstr)(s1, s2);
575 if (common_flags()->intercept_strstr)
576 StrstrCheck(ctx, r, s1, s2);
577 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
582 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
587 #if SANITIZER_INTERCEPT_STRCASESTR
589 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
590 const char *s1, const char *s2, char *result)
592 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
594 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
595 char *r = REAL(strcasestr)(s1, s2);
596 if (common_flags()->intercept_strstr)
597 StrstrCheck(ctx, r, s1, s2);
598 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
603 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
605 #define INIT_STRCASESTR
608 #if SANITIZER_INTERCEPT_STRTOK
610 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
612 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
613 if (!common_flags()->intercept_strtok) {
614 return REAL(strtok)(str, delimiters);
616 if (common_flags()->strict_string_checks) {
617 // If strict_string_checks is enabled, we check the whole first argument
618 // string on the first call (strtok saves this string in a static buffer
619 // for subsequent calls). We do not need to check strtok's result.
620 // As the delimiters can change, we check them every call.
621 if (str != nullptr) {
622 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
624 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
625 REAL(strlen)(delimiters) + 1);
626 return REAL(strtok)(str, delimiters);
628 // However, when strict_string_checks is disabled we cannot check the
629 // whole string on the first call. Instead, we check the result string
630 // which is guaranteed to be a NULL-terminated substring of the first
631 // argument. We also conservatively check one character of str and the
633 if (str != nullptr) {
634 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
636 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
637 char *result = REAL(strtok)(str, delimiters);
638 if (result != nullptr) {
639 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
640 } else if (str != nullptr) {
641 // No delimiter were found, it's safe to assume that the entire str was
643 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
649 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
654 #if SANITIZER_INTERCEPT_MEMMEM
655 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
656 const void *s1, SIZE_T len1, const void *s2,
657 SIZE_T len2, void *result)
659 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
662 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
663 void *r = REAL(memmem)(s1, len1, s2, len2);
664 if (common_flags()->intercept_memmem) {
665 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
666 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
668 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
669 s1, len1, s2, len2, r);
673 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
676 #endif // SANITIZER_INTERCEPT_MEMMEM
678 #if SANITIZER_INTERCEPT_STRCHR
679 INTERCEPTOR(char*, strchr, const char *s, int c) {
681 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
682 return internal_strchr(s, c);
683 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
684 char *result = REAL(strchr)(s, c);
685 if (common_flags()->intercept_strchr) {
686 // Keep strlen as macro argument, as macro may ignore it.
687 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
688 (result ? result - s : REAL(strlen)(s)) + 1);
692 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
697 #if SANITIZER_INTERCEPT_STRCHRNUL
698 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
700 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
701 char *result = REAL(strchrnul)(s, c);
702 uptr len = result - s + 1;
703 if (common_flags()->intercept_strchr)
704 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
707 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
709 #define INIT_STRCHRNUL
712 #if SANITIZER_INTERCEPT_STRRCHR
713 INTERCEPTOR(char*, strrchr, const char *s, int c) {
715 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
716 return internal_strrchr(s, c);
717 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
718 if (common_flags()->intercept_strchr)
719 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
720 return REAL(strrchr)(s, c);
722 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
727 #if SANITIZER_INTERCEPT_STRSPN
728 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
730 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
731 SIZE_T r = REAL(strspn)(s1, s2);
732 if (common_flags()->intercept_strspn) {
733 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
734 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
739 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
741 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
742 SIZE_T r = REAL(strcspn)(s1, s2);
743 if (common_flags()->intercept_strspn) {
744 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
745 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
750 #define INIT_STRSPN \
751 COMMON_INTERCEPT_FUNCTION(strspn); \
752 COMMON_INTERCEPT_FUNCTION(strcspn);
757 #if SANITIZER_INTERCEPT_STRPBRK
758 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
760 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
761 char *r = REAL(strpbrk)(s1, s2);
762 if (common_flags()->intercept_strpbrk) {
763 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
764 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
765 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
770 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
775 #if SANITIZER_INTERCEPT_MEMSET
776 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
778 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
781 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
786 #if SANITIZER_INTERCEPT_MEMMOVE
787 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
789 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
792 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
797 #if SANITIZER_INTERCEPT_MEMCPY
798 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
799 // On OS X, calling internal_memcpy here will cause memory corruptions,
800 // because memcpy and memmove are actually aliases of the same
801 // implementation. We need to use internal_memmove here.
802 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
803 // due to memcpy being an alias of memmove on OS X.
805 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
806 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
808 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
812 #define INIT_MEMCPY \
814 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
815 COMMON_INTERCEPT_FUNCTION(memcpy); \
817 ASSIGN_REAL(memcpy, memmove); \
819 CHECK(REAL(memcpy)); \
826 #if SANITIZER_INTERCEPT_MEMCMP
827 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
828 const void *s1, const void *s2, uptr n,
831 // Common code for `memcmp` and `bcmp`.
832 int MemcmpInterceptorCommon(void *ctx,
833 int (*real_fn)(const void *, const void *, uptr),
834 const void *a1, const void *a2, uptr size) {
835 if (common_flags()->intercept_memcmp) {
836 if (common_flags()->strict_memcmp) {
837 // Check the entire regions even if the first bytes of the buffers are
839 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
840 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
841 // Fallthrough to REAL(memcmp) below.
843 unsigned char c1 = 0, c2 = 0;
844 const unsigned char *s1 = (const unsigned char*)a1;
845 const unsigned char *s2 = (const unsigned char*)a2;
847 for (i = 0; i < size; i++) {
852 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
853 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
854 int r = CharCmpX(c1, c2);
855 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
860 int result = real_fn(a1, a2, size);
861 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
866 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
867 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
868 return internal_memcmp(a1, a2, size);
870 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
871 return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
874 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
879 #if SANITIZER_INTERCEPT_BCMP
880 INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {
881 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
882 return internal_memcmp(a1, a2, size);
884 COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
885 return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
888 #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
893 #if SANITIZER_INTERCEPT_MEMCHR
894 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
895 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
896 return internal_memchr(s, c, n);
898 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
899 #if SANITIZER_WINDOWS
902 res = REAL(memchr)(s, c, n);
904 res = internal_memchr(s, c, n);
907 void *res = REAL(memchr)(s, c, n);
909 uptr len = res ? (char *)res - (const char *)s + 1 : n;
910 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
914 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
919 #if SANITIZER_INTERCEPT_MEMRCHR
920 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
922 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
923 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
924 return REAL(memrchr)(s, c, n);
927 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
932 #if SANITIZER_INTERCEPT_FREXP
933 INTERCEPTOR(double, frexp, double x, int *exp) {
935 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
936 // Assuming frexp() always writes to |exp|.
937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
938 double res = REAL(frexp)(x, exp);
942 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
945 #endif // SANITIZER_INTERCEPT_FREXP
947 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
948 INTERCEPTOR(float, frexpf, float x, int *exp) {
950 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
951 // FIXME: under ASan the call below may write to freed memory and corrupt
953 // https://github.com/google/sanitizers/issues/321.
954 float res = REAL(frexpf)(x, exp);
955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
959 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
961 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
962 // FIXME: under ASan the call below may write to freed memory and corrupt
964 // https://github.com/google/sanitizers/issues/321.
965 long double res = REAL(frexpl)(x, exp);
966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
970 #define INIT_FREXPF_FREXPL \
971 COMMON_INTERCEPT_FUNCTION(frexpf); \
972 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
974 #define INIT_FREXPF_FREXPL
975 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
978 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
979 SIZE_T iovlen, SIZE_T maxlen) {
980 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
981 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
987 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
988 SIZE_T iovlen, SIZE_T maxlen) {
989 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
990 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
991 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
992 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
998 #if SANITIZER_INTERCEPT_READ
999 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
1001 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
1002 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1003 // FIXME: under ASan the call below may write to freed memory and corrupt
1004 // its metadata. See
1005 // https://github.com/google/sanitizers/issues/321.
1006 SSIZE_T res = REAL(read)(fd, ptr, count);
1007 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1008 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1011 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
1016 #if SANITIZER_INTERCEPT_FREAD
1017 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
1018 // libc file streams can call user-supplied functions, see fopencookie.
1020 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
1021 // FIXME: under ASan the call below may write to freed memory and corrupt
1022 // its metadata. See
1023 // https://github.com/google/sanitizers/issues/321.
1024 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
1025 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
1028 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1033 #if SANITIZER_INTERCEPT_PREAD
1034 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1036 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1037 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1038 // FIXME: under ASan the call below may write to freed memory and corrupt
1039 // its metadata. See
1040 // https://github.com/google/sanitizers/issues/321.
1041 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1042 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1043 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1046 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1051 #if SANITIZER_INTERCEPT_PREAD64
1052 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1054 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1055 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1056 // FIXME: under ASan the call below may write to freed memory and corrupt
1057 // its metadata. See
1058 // https://github.com/google/sanitizers/issues/321.
1059 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1060 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1061 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1064 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1066 #define INIT_PREAD64
1069 #if SANITIZER_INTERCEPT_READV
1070 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1073 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1074 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1075 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1076 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1077 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1080 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1085 #if SANITIZER_INTERCEPT_PREADV
1086 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1089 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1090 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1091 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1092 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1093 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1096 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1101 #if SANITIZER_INTERCEPT_PREADV64
1102 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1105 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1106 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1107 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1108 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1109 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1112 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1114 #define INIT_PREADV64
1117 #if SANITIZER_INTERCEPT_WRITE
1118 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1120 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1121 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1122 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1123 SSIZE_T res = REAL(write)(fd, ptr, count);
1124 // FIXME: this check should be _before_ the call to REAL(write), not after
1125 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1128 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1133 #if SANITIZER_INTERCEPT_FWRITE
1134 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1135 // libc file streams can call user-supplied functions, see fopencookie.
1137 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1138 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1139 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1142 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1147 #if SANITIZER_INTERCEPT_PWRITE
1148 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1150 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1151 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1152 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1153 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1154 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1157 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1162 #if SANITIZER_INTERCEPT_PWRITE64
1163 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1166 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1167 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1168 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1169 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1170 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1173 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1175 #define INIT_PWRITE64
1178 #if SANITIZER_INTERCEPT_WRITEV
1179 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1182 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1183 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1184 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1185 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1186 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1189 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1194 #if SANITIZER_INTERCEPT_PWRITEV
1195 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1198 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1199 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1200 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1201 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1202 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1205 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1207 #define INIT_PWRITEV
1210 #if SANITIZER_INTERCEPT_PWRITEV64
1211 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1214 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1215 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1216 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1217 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1218 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1221 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1223 #define INIT_PWRITEV64
1226 #if SANITIZER_INTERCEPT_FGETS
1227 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1228 // libc file streams can call user-supplied functions, see fopencookie.
1230 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1231 // FIXME: under ASan the call below may write to freed memory and corrupt
1232 // its metadata. See
1233 // https://github.com/google/sanitizers/issues/321.
1234 char *res = REAL(fgets)(s, size, file);
1236 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
1239 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1244 #if SANITIZER_INTERCEPT_FPUTS
1245 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1246 // libc file streams can call user-supplied functions, see fopencookie.
1248 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1249 if (!SANITIZER_MAC || s) { // `fputs(NULL, file)` is supported on Darwin.
1250 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1252 return REAL(fputs)(s, file);
1254 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1259 #if SANITIZER_INTERCEPT_PUTS
1260 INTERCEPTOR(int, puts, char *s) {
1261 // libc file streams can call user-supplied functions, see fopencookie.
1263 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1264 if (!SANITIZER_MAC || s) { // `puts(NULL)` is supported on Darwin.
1265 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1267 return REAL(puts)(s);
1269 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1274 #if SANITIZER_INTERCEPT_PRCTL
1275 INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
1276 unsigned long arg4, unsigned long arg5) {
1278 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1279 static const int PR_SET_NAME = 15;
1280 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1281 if (option == PR_SET_NAME) {
1283 internal_strncpy(buff, (char *)arg2, 15);
1285 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1289 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1292 #endif // SANITIZER_INTERCEPT_PRCTL
1294 #if SANITIZER_INTERCEPT_TIME
1295 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1297 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1298 unsigned long local_t;
1299 unsigned long res = REAL(time)(&local_t);
1300 if (t && res != (unsigned long)-1) {
1301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1306 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1309 #endif // SANITIZER_INTERCEPT_TIME
1311 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1312 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1313 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1314 #if !SANITIZER_SOLARIS
1316 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1317 // can point to shared memory and tsan would report a data race.
1318 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1319 REAL(strlen(tm->tm_zone)) + 1);
1323 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1325 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1326 __sanitizer_tm *res = REAL(localtime)(timep);
1328 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1329 unpoison_tm(ctx, res);
1333 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1335 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1336 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1338 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1339 unpoison_tm(ctx, res);
1343 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1345 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1346 __sanitizer_tm *res = REAL(gmtime)(timep);
1348 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1349 unpoison_tm(ctx, res);
1353 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1355 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1356 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1358 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1359 unpoison_tm(ctx, res);
1363 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1365 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1366 // FIXME: under ASan the call below may write to freed memory and corrupt
1367 // its metadata. See
1368 // https://github.com/google/sanitizers/issues/321.
1369 char *res = REAL(ctime)(timep);
1371 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1372 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1376 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1378 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1379 // FIXME: under ASan the call below may write to freed memory and corrupt
1380 // its metadata. See
1381 // https://github.com/google/sanitizers/issues/321.
1382 char *res = REAL(ctime_r)(timep, result);
1384 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1385 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1389 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1391 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1392 // FIXME: under ASan the call below may write to freed memory and corrupt
1393 // its metadata. See
1394 // https://github.com/google/sanitizers/issues/321.
1395 char *res = REAL(asctime)(tm);
1397 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1402 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1404 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1405 // FIXME: under ASan the call below may write to freed memory and corrupt
1406 // its metadata. See
1407 // https://github.com/google/sanitizers/issues/321.
1408 char *res = REAL(asctime_r)(tm, result);
1410 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1411 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1415 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1417 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1418 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1419 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1420 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1421 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1422 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1423 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1424 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1425 long res = REAL(mktime)(tm);
1426 if (res != -1) unpoison_tm(ctx, tm);
1429 #define INIT_LOCALTIME_AND_FRIENDS \
1430 COMMON_INTERCEPT_FUNCTION(localtime); \
1431 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1432 COMMON_INTERCEPT_FUNCTION(gmtime); \
1433 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1434 COMMON_INTERCEPT_FUNCTION(ctime); \
1435 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1436 COMMON_INTERCEPT_FUNCTION(asctime); \
1437 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1438 COMMON_INTERCEPT_FUNCTION(mktime);
1440 #define INIT_LOCALTIME_AND_FRIENDS
1441 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1443 #if SANITIZER_INTERCEPT_STRPTIME
1444 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1446 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1448 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1449 // FIXME: under ASan the call below may write to freed memory and corrupt
1450 // its metadata. See
1451 // https://github.com/google/sanitizers/issues/321.
1452 char *res = REAL(strptime)(s, format, tm);
1453 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1455 // Do not call unpoison_tm here, because strptime does not, in fact,
1456 // initialize the entire struct tm. For example, tm_zone pointer is left
1458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1462 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1464 #define INIT_STRPTIME
1467 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1468 #include "sanitizer_common_interceptors_format.inc"
1470 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1474 va_start(ap, format); \
1475 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1476 int res = WRAP(vname)(__VA_ARGS__, ap); \
1483 #if SANITIZER_INTERCEPT_SCANF
1485 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1488 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1491 int res = REAL(vname)(__VA_ARGS__); \
1493 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1498 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1499 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1501 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1502 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1504 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1505 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1507 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1508 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1509 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1511 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1513 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1515 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1516 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1517 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1519 INTERCEPTOR(int, scanf, const char *format, ...)
1520 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1522 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1523 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1525 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1526 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1528 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1529 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1530 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1532 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1533 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1535 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1536 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1541 #if SANITIZER_INTERCEPT_SCANF
1542 #define INIT_SCANF \
1543 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1544 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1545 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1546 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1547 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1548 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1553 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1554 #define INIT_ISOC99_SCANF \
1555 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1556 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1557 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1558 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1559 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1560 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1562 #define INIT_ISOC99_SCANF
1565 #if SANITIZER_INTERCEPT_PRINTF
1567 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1569 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1573 #define VPRINTF_INTERCEPTOR_RETURN() \
1576 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1578 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1579 if (common_flags()->check_printf) \
1580 printf_common(ctx, format, aq); \
1581 int res = REAL(vname)(__VA_ARGS__); \
1582 VPRINTF_INTERCEPTOR_RETURN(); \
1586 // FIXME: under ASan the REAL() call below may write to freed memory and
1587 // corrupt its metadata. See
1588 // https://github.com/google/sanitizers/issues/321.
1589 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1591 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1592 if (common_flags()->check_printf) { \
1593 printf_common(ctx, format, aq); \
1595 int res = REAL(vname)(str, __VA_ARGS__); \
1597 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1599 VPRINTF_INTERCEPTOR_RETURN(); \
1603 // FIXME: under ASan the REAL() call below may write to freed memory and
1604 // corrupt its metadata. See
1605 // https://github.com/google/sanitizers/issues/321.
1606 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1608 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1609 if (common_flags()->check_printf) { \
1610 printf_common(ctx, format, aq); \
1612 int res = REAL(vname)(str, size, __VA_ARGS__); \
1614 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1616 VPRINTF_INTERCEPTOR_RETURN(); \
1620 // FIXME: under ASan the REAL() call below may write to freed memory and
1621 // corrupt its metadata. See
1622 // https://github.com/google/sanitizers/issues/321.
1623 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1625 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1626 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1627 if (common_flags()->check_printf) { \
1628 printf_common(ctx, format, aq); \
1630 int res = REAL(vname)(strp, __VA_ARGS__); \
1632 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1634 VPRINTF_INTERCEPTOR_RETURN(); \
1638 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1639 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1641 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1643 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1645 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1647 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1649 #if SANITIZER_INTERCEPT___PRINTF_CHK
1650 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1651 SIZE_T size_to, const char *format, va_list ap)
1652 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1655 #if SANITIZER_INTERCEPT_PRINTF_L
1656 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1657 const char *format, va_list ap)
1658 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1660 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1661 const char *format, ...)
1662 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1663 #endif // SANITIZER_INTERCEPT_PRINTF_L
1665 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1666 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1668 #if SANITIZER_INTERCEPT___PRINTF_CHK
1669 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1670 const char *format, va_list ap)
1671 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1674 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1675 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1677 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1678 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1679 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1681 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1682 const char *format, va_list ap)
1683 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1685 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1687 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1689 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1691 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1694 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1696 INTERCEPTOR(int, printf, const char *format, ...)
1697 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1699 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1700 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1702 #if SANITIZER_INTERCEPT___PRINTF_CHK
1703 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1704 const char *format, ...)
1705 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1708 INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
1709 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)
1711 #if SANITIZER_INTERCEPT___PRINTF_CHK
1712 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1713 const char *format, ...)
1714 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
1717 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1718 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1720 #if SANITIZER_INTERCEPT___PRINTF_CHK
1721 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1722 SIZE_T size_to, const char *format, ...)
1723 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
1726 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1727 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1729 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1730 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1731 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1733 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1735 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1737 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1738 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1740 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1741 const char *format, ...)
1742 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1745 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1747 #endif // SANITIZER_INTERCEPT_PRINTF
1749 #if SANITIZER_INTERCEPT_PRINTF
1750 #define INIT_PRINTF \
1751 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1752 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1753 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1754 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1755 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1756 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1757 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1758 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1759 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1760 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1765 #if SANITIZER_INTERCEPT___PRINTF_CHK
1766 #define INIT___PRINTF_CHK \
1767 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1768 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1769 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1770 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1771 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1773 #define INIT___PRINTF_CHK
1776 #if SANITIZER_INTERCEPT_PRINTF_L
1777 #define INIT_PRINTF_L \
1778 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1779 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1781 #define INIT_PRINTF_L
1784 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1785 #define INIT_ISOC99_PRINTF \
1786 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1787 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1788 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1789 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1790 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1791 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1792 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1793 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1795 #define INIT_ISOC99_PRINTF
1798 #if SANITIZER_INTERCEPT_IOCTL
1799 #include "sanitizer_common_interceptors_ioctl.inc"
1800 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1801 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1802 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1803 // can trigger a report and we need to be able to unwind through this
1804 // function. On Mac in debug mode we might not have a frame pointer, because
1805 // ioctl_common_[pre|post] doesn't get inlined here.
1806 ENABLE_FRAME_POINTER;
1810 va_start(ap, request);
1811 void *arg = va_arg(ap, void *);
1813 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1815 CHECK(ioctl_initialized);
1817 // Note: TSan does not use common flags, and they are zero-initialized.
1818 // This effectively disables ioctl handling in TSan.
1819 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1821 // Although request is unsigned long, the rest of the interceptor uses it
1822 // as just "unsigned" to save space, because we know that all values fit in
1823 // "unsigned" - they are compile-time constants.
1825 const ioctl_desc *desc = ioctl_lookup(request);
1826 ioctl_desc decoded_desc;
1828 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1829 if (!ioctl_decode(request, &decoded_desc))
1830 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1832 desc = &decoded_desc;
1835 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1836 int res = REAL(ioctl)(d, request, arg);
1837 // FIXME: some ioctls have different return values for success and failure.
1838 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1841 #define INIT_IOCTL \
1843 COMMON_INTERCEPT_FUNCTION(ioctl);
1849 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1854 REAL(strlen)(pwd->pw_name) + 1);
1856 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1857 REAL(strlen)(pwd->pw_passwd) + 1);
1858 #if !SANITIZER_ANDROID
1860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1861 REAL(strlen)(pwd->pw_gecos) + 1);
1863 #if SANITIZER_MAC || SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
1865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1866 REAL(strlen)(pwd->pw_class) + 1);
1869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1870 REAL(strlen)(pwd->pw_dir) + 1);
1872 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1873 REAL(strlen)(pwd->pw_shell) + 1);
1877 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1879 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1882 REAL(strlen)(grp->gr_name) + 1);
1884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1885 REAL(strlen)(grp->gr_passwd) + 1);
1886 char **p = grp->gr_mem;
1888 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1890 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1891 (p - grp->gr_mem + 1) * sizeof(*p));
1894 #endif // SANITIZER_POSIX
1896 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1897 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1899 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1901 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1902 __sanitizer_passwd *res = REAL(getpwnam)(name);
1903 unpoison_passwd(ctx, res);
1906 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1908 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1909 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1910 unpoison_passwd(ctx, res);
1913 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1915 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1916 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1917 __sanitizer_group *res = REAL(getgrnam)(name);
1918 unpoison_group(ctx, res);
1921 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1923 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1924 __sanitizer_group *res = REAL(getgrgid)(gid);
1925 unpoison_group(ctx, res);
1928 #define INIT_GETPWNAM_AND_FRIENDS \
1929 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1930 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1931 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1932 COMMON_INTERCEPT_FUNCTION(getgrgid);
1934 #define INIT_GETPWNAM_AND_FRIENDS
1937 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1938 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1939 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1941 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1942 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1943 // FIXME: under ASan the call below may write to freed memory and corrupt
1944 // its metadata. See
1945 // https://github.com/google/sanitizers/issues/321.
1946 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1948 unpoison_passwd(ctx, *result);
1949 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1952 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1953 SIZE_T buflen, __sanitizer_passwd **result) {
1955 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1956 // FIXME: under ASan the call below may write to freed memory and corrupt
1957 // its metadata. See
1958 // https://github.com/google/sanitizers/issues/321.
1959 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1961 unpoison_passwd(ctx, *result);
1962 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1965 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1966 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1968 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1969 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1970 // FIXME: under ASan the call below may write to freed memory and corrupt
1971 // its metadata. See
1972 // https://github.com/google/sanitizers/issues/321.
1973 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1975 unpoison_group(ctx, *result);
1976 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1979 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1980 SIZE_T buflen, __sanitizer_group **result) {
1982 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1983 // FIXME: under ASan the call below may write to freed memory and corrupt
1984 // its metadata. See
1985 // https://github.com/google/sanitizers/issues/321.
1986 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1988 unpoison_group(ctx, *result);
1989 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1992 #define INIT_GETPWNAM_R_AND_FRIENDS \
1993 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1994 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1995 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1996 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1998 #define INIT_GETPWNAM_R_AND_FRIENDS
2001 #if SANITIZER_INTERCEPT_GETPWENT
2002 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
2004 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
2005 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2006 unpoison_passwd(ctx, res);
2009 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2011 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2012 __sanitizer_group *res = REAL(getgrent)(dummy);
2013 unpoison_group(ctx, res);
2016 #define INIT_GETPWENT \
2017 COMMON_INTERCEPT_FUNCTION(getpwent); \
2018 COMMON_INTERCEPT_FUNCTION(getgrent);
2020 #define INIT_GETPWENT
2023 #if SANITIZER_INTERCEPT_FGETPWENT
2024 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2026 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2027 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2028 unpoison_passwd(ctx, res);
2031 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2033 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2034 __sanitizer_group *res = REAL(fgetgrent)(fp);
2035 unpoison_group(ctx, res);
2038 #define INIT_FGETPWENT \
2039 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2040 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2042 #define INIT_FGETPWENT
2045 #if SANITIZER_INTERCEPT_GETPWENT_R
2046 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2047 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2049 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2050 // FIXME: under ASan the call below may write to freed memory and corrupt
2051 // its metadata. See
2052 // https://github.com/google/sanitizers/issues/321.
2053 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2055 unpoison_passwd(ctx, *pwbufp);
2056 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2059 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2060 __sanitizer_group **pwbufp) {
2062 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2063 // FIXME: under ASan the call below may write to freed memory and corrupt
2064 // its metadata. See
2065 // https://github.com/google/sanitizers/issues/321.
2066 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2068 unpoison_group(ctx, *pwbufp);
2069 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2072 #define INIT_GETPWENT_R \
2073 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2074 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2076 #define INIT_GETPWENT_R
2079 #if SANITIZER_INTERCEPT_FGETPWENT_R
2080 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2081 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2083 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2084 // FIXME: under ASan the call below may write to freed memory and corrupt
2085 // its metadata. See
2086 // https://github.com/google/sanitizers/issues/321.
2087 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2089 unpoison_passwd(ctx, *pwbufp);
2090 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2093 #define INIT_FGETPWENT_R \
2094 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2096 #define INIT_FGETPWENT_R
2099 #if SANITIZER_INTERCEPT_FGETGRENT_R
2100 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2101 SIZE_T buflen, __sanitizer_group **pwbufp) {
2103 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2104 // FIXME: under ASan the call below may write to freed memory and corrupt
2105 // its metadata. See
2106 // https://github.com/google/sanitizers/issues/321.
2107 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2109 unpoison_group(ctx, *pwbufp);
2110 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2113 #define INIT_FGETGRENT_R \
2114 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2116 #define INIT_FGETGRENT_R
2119 #if SANITIZER_INTERCEPT_SETPWENT
2120 // The only thing these interceptors do is disable any nested interceptors.
2121 // These functions may open nss modules and call uninstrumented functions from
2122 // them, and we don't want things like strlen() to trigger.
2123 INTERCEPTOR(void, setpwent, int dummy) {
2125 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2126 REAL(setpwent)(dummy);
2128 INTERCEPTOR(void, endpwent, int dummy) {
2130 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2131 REAL(endpwent)(dummy);
2133 INTERCEPTOR(void, setgrent, int dummy) {
2135 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2136 REAL(setgrent)(dummy);
2138 INTERCEPTOR(void, endgrent, int dummy) {
2140 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2141 REAL(endgrent)(dummy);
2143 #define INIT_SETPWENT \
2144 COMMON_INTERCEPT_FUNCTION(setpwent); \
2145 COMMON_INTERCEPT_FUNCTION(endpwent); \
2146 COMMON_INTERCEPT_FUNCTION(setgrent); \
2147 COMMON_INTERCEPT_FUNCTION(endgrent);
2149 #define INIT_SETPWENT
2152 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2153 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2155 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2156 // FIXME: under ASan the call below may write to freed memory and corrupt
2157 // its metadata. See
2158 // https://github.com/google/sanitizers/issues/321.
2159 int res = REAL(clock_getres)(clk_id, tp);
2161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2165 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2167 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2168 // FIXME: under ASan the call below may write to freed memory and corrupt
2169 // its metadata. See
2170 // https://github.com/google/sanitizers/issues/321.
2171 int res = REAL(clock_gettime)(clk_id, tp);
2173 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2177 namespace __sanitizer {
2179 int real_clock_gettime(u32 clk_id, void *tp) {
2180 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2181 return internal_clock_gettime(clk_id, tp);
2182 return REAL(clock_gettime)(clk_id, tp);
2185 } // namespace __sanitizer
2186 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2188 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2189 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2190 return REAL(clock_settime)(clk_id, tp);
2192 #define INIT_CLOCK_GETTIME \
2193 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2194 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2195 COMMON_INTERCEPT_FUNCTION(clock_settime);
2197 #define INIT_CLOCK_GETTIME
2200 #if SANITIZER_INTERCEPT_GETITIMER
2201 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2203 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2204 // FIXME: under ASan the call below may write to freed memory and corrupt
2205 // its metadata. See
2206 // https://github.com/google/sanitizers/issues/321.
2207 int res = REAL(getitimer)(which, curr_value);
2208 if (!res && curr_value) {
2209 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2213 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2215 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2217 // itimerval can contain padding that may be legitimately uninitialized
2218 const struct __sanitizer_itimerval *nv =
2219 (const struct __sanitizer_itimerval *)new_value;
2220 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2221 sizeof(__sanitizer_time_t));
2222 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2223 sizeof(__sanitizer_suseconds_t));
2224 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2225 sizeof(__sanitizer_time_t));
2226 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2227 sizeof(__sanitizer_suseconds_t));
2229 // FIXME: under ASan the call below may write to freed memory and corrupt
2230 // its metadata. See
2231 // https://github.com/google/sanitizers/issues/321.
2232 int res = REAL(setitimer)(which, new_value, old_value);
2233 if (!res && old_value) {
2234 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2238 #define INIT_GETITIMER \
2239 COMMON_INTERCEPT_FUNCTION(getitimer); \
2240 COMMON_INTERCEPT_FUNCTION(setitimer);
2242 #define INIT_GETITIMER
2245 #if SANITIZER_INTERCEPT_GLOB
2246 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2248 // +1 for NULL pointer at the end.
2249 if (pglob->gl_pathv)
2250 COMMON_INTERCEPTOR_WRITE_RANGE(
2251 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2252 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2253 char *p = pglob->gl_pathv[i];
2254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2258 #if SANITIZER_SOLARIS
2259 INTERCEPTOR(int, glob, const char *pattern, int flags,
2260 int (*errfunc)(const char *epath, int eerrno),
2261 __sanitizer_glob_t *pglob) {
2263 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2264 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2265 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2266 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2270 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2272 static void wrapped_gl_closedir(void *dir) {
2273 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2274 pglob_copy->gl_closedir(dir);
2277 static void *wrapped_gl_readdir(void *dir) {
2278 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2279 return pglob_copy->gl_readdir(dir);
2282 static void *wrapped_gl_opendir(const char *s) {
2283 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2284 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2285 return pglob_copy->gl_opendir(s);
2288 static int wrapped_gl_lstat(const char *s, void *st) {
2289 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2290 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2291 return pglob_copy->gl_lstat(s, st);
2294 static int wrapped_gl_stat(const char *s, void *st) {
2295 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2296 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2297 return pglob_copy->gl_stat(s, st);
2300 static const __sanitizer_glob_t kGlobCopy = {
2302 0, wrapped_gl_closedir, wrapped_gl_readdir,
2303 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2305 INTERCEPTOR(int, glob, const char *pattern, int flags,
2306 int (*errfunc)(const char *epath, int eerrno),
2307 __sanitizer_glob_t *pglob) {
2309 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2310 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2311 __sanitizer_glob_t glob_copy;
2312 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2313 if (flags & glob_altdirfunc) {
2314 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2315 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2316 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2317 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2318 Swap(pglob->gl_stat, glob_copy.gl_stat);
2319 pglob_copy = &glob_copy;
2321 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2322 if (flags & glob_altdirfunc) {
2323 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2324 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2325 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2326 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2327 Swap(pglob->gl_stat, glob_copy.gl_stat);
2330 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2333 #endif // SANITIZER_SOLARIS
2335 COMMON_INTERCEPT_FUNCTION(glob);
2336 #else // SANITIZER_INTERCEPT_GLOB
2338 #endif // SANITIZER_INTERCEPT_GLOB
2340 #if SANITIZER_INTERCEPT_GLOB64
2341 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2342 int (*errfunc)(const char *epath, int eerrno),
2343 __sanitizer_glob_t *pglob) {
2345 COMMON_INTERCEPTOR_ENTER(ctx, glob64, 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(glob64)(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 #define INIT_GLOB64 \
2370 COMMON_INTERCEPT_FUNCTION(glob64);
2371 #else // SANITIZER_INTERCEPT_GLOB64
2373 #endif // SANITIZER_INTERCEPT_GLOB64
2375 #if SANITIZER_INTERCEPT_WAIT
2376 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2377 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2379 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2381 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2382 // FIXME: under ASan the call below may write to freed memory and corrupt
2383 // its metadata. See
2384 // https://github.com/google/sanitizers/issues/321.
2385 int res = REAL(wait)(status);
2386 if (res != -1 && status)
2387 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2390 // On FreeBSD id_t is always 64-bit wide.
2391 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2392 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2395 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2399 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2400 // FIXME: under ASan the call below may write to freed memory and corrupt
2401 // its metadata. See
2402 // https://github.com/google/sanitizers/issues/321.
2403 int res = REAL(waitid)(idtype, id, infop, options);
2404 if (res != -1 && infop)
2405 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2408 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2410 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2411 // FIXME: under ASan the call below may write to freed memory and corrupt
2412 // its metadata. See
2413 // https://github.com/google/sanitizers/issues/321.
2414 int res = REAL(waitpid)(pid, status, options);
2415 if (res != -1 && status)
2416 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2419 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2421 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2422 // FIXME: under ASan the call below may write to freed memory and corrupt
2423 // its metadata. See
2424 // https://github.com/google/sanitizers/issues/321.
2425 int res = REAL(wait3)(status, options, rusage);
2427 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2428 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2432 #if SANITIZER_ANDROID
2433 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2435 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2436 // FIXME: under ASan the call below may write to freed memory and corrupt
2437 // its metadata. See
2438 // https://github.com/google/sanitizers/issues/321.
2439 int res = REAL(__wait4)(pid, status, options, rusage);
2441 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2442 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2446 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2448 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2450 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2451 // FIXME: under ASan the call below may write to freed memory and corrupt
2452 // its metadata. See
2453 // https://github.com/google/sanitizers/issues/321.
2454 int res = REAL(wait4)(pid, status, options, rusage);
2456 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2457 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2461 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2462 #endif // SANITIZER_ANDROID
2464 COMMON_INTERCEPT_FUNCTION(wait); \
2465 COMMON_INTERCEPT_FUNCTION(waitid); \
2466 COMMON_INTERCEPT_FUNCTION(waitpid); \
2467 COMMON_INTERCEPT_FUNCTION(wait3);
2473 #if SANITIZER_INTERCEPT_INET
2474 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2476 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2477 uptr sz = __sanitizer_in_addr_sz(af);
2478 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2479 // FIXME: figure out read size based on the address family.
2480 // FIXME: under ASan the call below may write to freed memory and corrupt
2481 // its metadata. See
2482 // https://github.com/google/sanitizers/issues/321.
2483 char *res = REAL(inet_ntop)(af, src, dst, size);
2484 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2487 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2489 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2490 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2491 // FIXME: figure out read size based on the address family.
2492 // FIXME: under ASan the call below may write to freed memory and corrupt
2493 // its metadata. See
2494 // https://github.com/google/sanitizers/issues/321.
2495 int res = REAL(inet_pton)(af, src, dst);
2497 uptr sz = __sanitizer_in_addr_sz(af);
2498 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2503 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2504 COMMON_INTERCEPT_FUNCTION(inet_pton);
2509 #if SANITIZER_INTERCEPT_INET
2510 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2512 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2513 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2514 // FIXME: under ASan the call below may write to freed memory and corrupt
2515 // its metadata. See
2516 // https://github.com/google/sanitizers/issues/321.
2517 int res = REAL(inet_aton)(cp, dst);
2519 uptr sz = __sanitizer_in_addr_sz(af_inet);
2520 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2524 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2526 #define INIT_INET_ATON
2529 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2530 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2532 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2533 // FIXME: under ASan the call below may write to freed memory and corrupt
2534 // its metadata. See
2535 // https://github.com/google/sanitizers/issues/321.
2536 int res = REAL(pthread_getschedparam)(thread, policy, param);
2538 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2539 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2543 #define INIT_PTHREAD_GETSCHEDPARAM \
2544 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2546 #define INIT_PTHREAD_GETSCHEDPARAM
2549 #if SANITIZER_INTERCEPT_GETADDRINFO
2550 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2551 struct __sanitizer_addrinfo *hints,
2552 struct __sanitizer_addrinfo **out) {
2554 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2555 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2557 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2559 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2560 // FIXME: under ASan the call below may write to freed memory and corrupt
2561 // its metadata. See
2562 // https://github.com/google/sanitizers/issues/321.
2563 int res = REAL(getaddrinfo)(node, service, hints, out);
2564 if (res == 0 && out) {
2565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2566 struct __sanitizer_addrinfo *p = *out;
2568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2571 if (p->ai_canonname)
2572 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2573 REAL(strlen)(p->ai_canonname) + 1);
2579 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2581 #define INIT_GETADDRINFO
2584 #if SANITIZER_INTERCEPT_GETNAMEINFO
2585 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2586 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2588 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2589 serv, servlen, flags);
2590 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2591 // There is padding in in_addr that may make this too noisy
2592 // FIXME: under ASan the call below may write to freed memory and corrupt
2593 // its metadata. See
2594 // https://github.com/google/sanitizers/issues/321.
2596 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2598 if (host && hostlen)
2599 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2600 if (serv && servlen)
2601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2605 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2607 #define INIT_GETNAMEINFO
2610 #if SANITIZER_INTERCEPT_GETSOCKNAME
2611 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2613 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2614 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2615 int addrlen_in = *addrlen;
2616 // FIXME: under ASan the call below may write to freed memory and corrupt
2617 // its metadata. See
2618 // https://github.com/google/sanitizers/issues/321.
2619 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2621 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2625 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2627 #define INIT_GETSOCKNAME
2630 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2631 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2632 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2635 char **p = h->h_aliases;
2637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2640 COMMON_INTERCEPTOR_WRITE_RANGE(
2641 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2644 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2647 COMMON_INTERCEPTOR_WRITE_RANGE(
2648 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2652 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2653 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2655 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2656 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2657 if (res) write_hostent(ctx, res);
2661 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2664 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2665 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2666 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2667 if (res) write_hostent(ctx, res);
2671 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2673 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2674 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2675 if (res) write_hostent(ctx, res);
2678 #define INIT_GETHOSTBYNAME \
2679 COMMON_INTERCEPT_FUNCTION(gethostent); \
2680 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2681 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2683 #define INIT_GETHOSTBYNAME
2684 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2686 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2687 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2689 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2690 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2691 if (res) write_hostent(ctx, res);
2694 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2696 #define INIT_GETHOSTBYNAME2
2697 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2699 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2700 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2701 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2704 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2706 // FIXME: under ASan the call below may write to freed memory and corrupt
2707 // its metadata. See
2708 // https://github.com/google/sanitizers/issues/321.
2709 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2712 if (res == 0 && *result) write_hostent(ctx, *result);
2715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2718 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2720 #define INIT_GETHOSTBYNAME_R
2723 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2724 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2725 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2727 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2729 // FIXME: under ASan the call below may write to freed memory and corrupt
2730 // its metadata. See
2731 // https://github.com/google/sanitizers/issues/321.
2732 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2734 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2735 if (res == 0 && *result) write_hostent(ctx, *result);
2738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2741 #define INIT_GETHOSTENT_R \
2742 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2744 #define INIT_GETHOSTENT_R
2747 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2748 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2749 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2750 __sanitizer_hostent **result, int *h_errnop) {
2752 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2753 buflen, result, h_errnop);
2754 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2755 // FIXME: under ASan the call below may write to freed memory and corrupt
2756 // its metadata. See
2757 // https://github.com/google/sanitizers/issues/321.
2758 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2761 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2762 if (res == 0 && *result) write_hostent(ctx, *result);
2765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2768 #define INIT_GETHOSTBYADDR_R \
2769 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2771 #define INIT_GETHOSTBYADDR_R
2774 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2775 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2776 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2777 __sanitizer_hostent **result, int *h_errnop) {
2779 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2781 // FIXME: under ASan the call below may write to freed memory and corrupt
2782 // its metadata. See
2783 // https://github.com/google/sanitizers/issues/321.
2785 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2787 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2788 if (res == 0 && *result) write_hostent(ctx, *result);
2791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2794 #define INIT_GETHOSTBYNAME2_R \
2795 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2797 #define INIT_GETHOSTBYNAME2_R
2800 #if SANITIZER_INTERCEPT_GETSOCKOPT
2801 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2804 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2806 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2807 // FIXME: under ASan the call below may write to freed memory and corrupt
2808 // its metadata. See
2809 // https://github.com/google/sanitizers/issues/321.
2810 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2812 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2815 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2817 #define INIT_GETSOCKOPT
2820 #if SANITIZER_INTERCEPT_ACCEPT
2821 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2823 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2824 unsigned addrlen0 = 0;
2826 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2827 addrlen0 = *addrlen;
2829 int fd2 = REAL(accept)(fd, addr, addrlen);
2831 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2832 if (addr && addrlen)
2833 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2837 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2842 #if SANITIZER_INTERCEPT_ACCEPT4
2843 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2845 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2846 unsigned addrlen0 = 0;
2848 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2849 addrlen0 = *addrlen;
2851 // FIXME: under ASan the call below may write to freed memory and corrupt
2852 // its metadata. See
2853 // https://github.com/google/sanitizers/issues/321.
2854 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2856 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2857 if (addr && addrlen)
2858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2862 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2864 #define INIT_ACCEPT4
2867 #if SANITIZER_INTERCEPT_PACCEPT
2868 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2869 __sanitizer_sigset_t *set, int f) {
2871 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2872 unsigned addrlen0 = 0;
2874 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2875 addrlen0 = *addrlen;
2877 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2878 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
2880 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2881 if (addr && addrlen)
2882 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2886 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2888 #define INIT_PACCEPT
2891 #if SANITIZER_INTERCEPT_MODF
2892 INTERCEPTOR(double, modf, double x, double *iptr) {
2894 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2895 // FIXME: under ASan the call below may write to freed memory and corrupt
2896 // its metadata. See
2897 // https://github.com/google/sanitizers/issues/321.
2898 double res = REAL(modf)(x, iptr);
2900 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2904 INTERCEPTOR(float, modff, float x, float *iptr) {
2906 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2907 // FIXME: under ASan the call below may write to freed memory and corrupt
2908 // its metadata. See
2909 // https://github.com/google/sanitizers/issues/321.
2910 float res = REAL(modff)(x, iptr);
2912 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2916 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2918 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2919 // FIXME: under ASan the call below may write to freed memory and corrupt
2920 // its metadata. See
2921 // https://github.com/google/sanitizers/issues/321.
2922 long double res = REAL(modfl)(x, iptr);
2924 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2929 COMMON_INTERCEPT_FUNCTION(modf); \
2930 COMMON_INTERCEPT_FUNCTION(modff); \
2931 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2936 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2937 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2939 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2940 if (msg->msg_name && msg->msg_namelen)
2941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2942 if (msg->msg_iov && msg->msg_iovlen)
2943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2944 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2945 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2946 if (msg->msg_control && msg->msg_controllen)
2947 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2951 #if SANITIZER_INTERCEPT_RECVMSG
2952 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2955 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2956 // FIXME: under ASan the call below may write to freed memory and corrupt
2957 // its metadata. See
2958 // https://github.com/google/sanitizers/issues/321.
2959 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2961 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2963 write_msghdr(ctx, msg, res);
2964 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2969 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2971 #define INIT_RECVMSG
2974 #if SANITIZER_INTERCEPT_RECVMMSG
2975 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2976 unsigned int vlen, int flags, void *timeout) {
2978 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2979 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2980 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2982 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2983 for (int i = 0; i < res; ++i) {
2984 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2985 sizeof(msgvec[i].msg_len));
2986 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2987 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2992 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2994 #define INIT_RECVMMSG
2997 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2998 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2999 const unsigned kCmsgDataOffset =
3000 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
3002 char *p = (char *)control;
3003 char *const control_end = p + controllen;
3005 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3006 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3007 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3009 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3011 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3012 sizeof(cmsg->cmsg_level));
3013 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3014 sizeof(cmsg->cmsg_type));
3016 if (cmsg->cmsg_len > kCmsgDataOffset) {
3017 char *data = p + kCmsgDataOffset;
3018 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3019 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3022 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3026 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3029 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3038 if (msg->msg_name && msg->msg_namelen)
3039 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3040 if (msg->msg_iov && msg->msg_iovlen)
3041 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3042 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3043 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3044 if (msg->msg_control && msg->msg_controllen)
3045 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3049 #if SANITIZER_INTERCEPT_SENDMSG
3050 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3053 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3055 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3056 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3058 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3059 if (common_flags()->intercept_send && res >= 0 && msg)
3060 read_msghdr(ctx, msg, res);
3063 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3065 #define INIT_SENDMSG
3068 #if SANITIZER_INTERCEPT_SENDMMSG
3069 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3070 unsigned vlen, int flags) {
3072 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3074 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3075 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3077 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3078 if (res >= 0 && msgvec) {
3079 for (int i = 0; i < res; ++i) {
3080 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3081 sizeof(msgvec[i].msg_len));
3082 if (common_flags()->intercept_send)
3083 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3088 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3090 #define INIT_SENDMMSG
3093 #if SANITIZER_INTERCEPT_GETPEERNAME
3094 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3096 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3098 if (addrlen) addr_sz = *addrlen;
3099 // FIXME: under ASan the call below may write to freed memory and corrupt
3100 // its metadata. See
3101 // https://github.com/google/sanitizers/issues/321.
3102 int res = REAL(getpeername)(sockfd, addr, addrlen);
3103 if (!res && addr && addrlen)
3104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3107 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3109 #define INIT_GETPEERNAME
3112 #if SANITIZER_INTERCEPT_SYSINFO
3113 INTERCEPTOR(int, sysinfo, void *info) {
3115 // FIXME: under ASan the call below may write to freed memory and corrupt
3116 // its metadata. See
3117 // https://github.com/google/sanitizers/issues/321.
3118 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3119 int res = REAL(sysinfo)(info);
3121 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3124 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3126 #define INIT_SYSINFO
3129 #if SANITIZER_INTERCEPT_READDIR
3130 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3132 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3133 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3134 __sanitizer_dirent *res = REAL(opendir)(path);
3136 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3140 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3142 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3143 // FIXME: under ASan the call below may write to freed memory and corrupt
3144 // its metadata. See
3145 // https://github.com/google/sanitizers/issues/321.
3146 __sanitizer_dirent *res = REAL(readdir)(dirp);
3147 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3151 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3152 __sanitizer_dirent **result) {
3154 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3155 // FIXME: under ASan the call below may write to freed memory and corrupt
3156 // its metadata. See
3157 // https://github.com/google/sanitizers/issues/321.
3158 int res = REAL(readdir_r)(dirp, entry, result);
3160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3167 #define INIT_READDIR \
3168 COMMON_INTERCEPT_FUNCTION(opendir); \
3169 COMMON_INTERCEPT_FUNCTION(readdir); \
3170 COMMON_INTERCEPT_FUNCTION(readdir_r);
3172 #define INIT_READDIR
3175 #if SANITIZER_INTERCEPT_READDIR64
3176 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3178 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3179 // FIXME: under ASan the call below may write to freed memory and corrupt
3180 // its metadata. See
3181 // https://github.com/google/sanitizers/issues/321.
3182 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3183 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3187 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3188 __sanitizer_dirent64 **result) {
3190 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3191 // FIXME: under ASan the call below may write to freed memory and corrupt
3192 // its metadata. See
3193 // https://github.com/google/sanitizers/issues/321.
3194 int res = REAL(readdir64_r)(dirp, entry, result);
3196 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3202 #define INIT_READDIR64 \
3203 COMMON_INTERCEPT_FUNCTION(readdir64); \
3204 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3206 #define INIT_READDIR64
3209 #if SANITIZER_INTERCEPT_PTRACE
3210 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3212 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3213 __sanitizer_iovec local_iovec;
3216 if (request == ptrace_setregs) {
3217 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3218 } else if (request == ptrace_setfpregs) {
3219 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3220 } else if (request == ptrace_setfpxregs) {
3221 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3222 } else if (request == ptrace_setvfpregs) {
3223 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3224 } else if (request == ptrace_setsiginfo) {
3225 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3227 // Some kernel might zero the iovec::iov_base in case of invalid
3228 // write access. In this case copy the invalid address for further
3230 } else if (request == ptrace_setregset || request == ptrace_getregset) {
3231 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3232 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3233 local_iovec = *iovec;
3234 if (request == ptrace_setregset)
3235 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3239 // FIXME: under ASan the call below may write to freed memory and corrupt
3240 // its metadata. See
3241 // https://github.com/google/sanitizers/issues/321.
3242 uptr res = REAL(ptrace)(request, pid, addr, data);
3245 // Note that PEEK* requests assign different meaning to the return value.
3246 // This function does not handle them (nor does it need to).
3247 if (request == ptrace_getregs) {
3248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3249 } else if (request == ptrace_getfpregs) {
3250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3251 } else if (request == ptrace_getfpxregs) {
3252 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3253 } else if (request == ptrace_getvfpregs) {
3254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3255 } else if (request == ptrace_getsiginfo) {
3256 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3257 } else if (request == ptrace_geteventmsg) {
3258 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3259 } else if (request == ptrace_getregset) {
3260 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3261 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3262 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3263 local_iovec.iov_len);
3269 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3274 #if SANITIZER_INTERCEPT_SETLOCALE
3275 static void unpoison_ctype_arrays(void *ctx) {
3276 #if SANITIZER_NETBSD
3277 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3284 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3286 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3288 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3289 char *res = REAL(setlocale)(category, locale);
3291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3292 unpoison_ctype_arrays(ctx);
3297 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3299 #define INIT_SETLOCALE
3302 #if SANITIZER_INTERCEPT_GETCWD
3303 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3305 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3306 // FIXME: under ASan the call below may write to freed memory and corrupt
3307 // its metadata. See
3308 // https://github.com/google/sanitizers/issues/321.
3309 char *res = REAL(getcwd)(buf, size);
3310 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3313 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3318 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3319 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3321 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3322 // FIXME: under ASan the call below may write to freed memory and corrupt
3323 // its metadata. See
3324 // https://github.com/google/sanitizers/issues/321.
3325 char *res = REAL(get_current_dir_name)(fake);
3326 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3330 #define INIT_GET_CURRENT_DIR_NAME \
3331 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3333 #define INIT_GET_CURRENT_DIR_NAME
3336 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3338 if (nptr == *endptr) {
3339 // No digits were found at strtol call, we need to find out the last
3340 // symbol accessed by strtoll on our own.
3341 // We get this symbol by skipping leading blanks and optional +/- sign.
3342 while (IsSpace(*nptr)) nptr++;
3343 if (*nptr == '+' || *nptr == '-') nptr++;
3344 *endptr = const_cast<char *>(nptr);
3346 CHECK(*endptr >= nptr);
3349 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3350 char **endptr, char *real_endptr, int base) {
3352 *endptr = real_endptr;
3353 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3355 // If base has unsupported value, strtol can exit with EINVAL
3356 // without reading any characters. So do additional checks only
3357 // if base is valid.
3358 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3359 if (is_valid_base) {
3360 FixRealStrtolEndptr(nptr, &real_endptr);
3362 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3363 (real_endptr - nptr) + 1 : 0);
3367 #if SANITIZER_INTERCEPT_STRTOIMAX
3368 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3370 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3371 // FIXME: under ASan the call below may write to freed memory and corrupt
3372 // its metadata. See
3373 // https://github.com/google/sanitizers/issues/321.
3375 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3376 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3380 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3382 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3383 // FIXME: under ASan the call below may write to freed memory and corrupt
3384 // its metadata. See
3385 // https://github.com/google/sanitizers/issues/321.
3387 UINTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3388 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3392 #define INIT_STRTOIMAX \
3393 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3394 COMMON_INTERCEPT_FUNCTION(strtoumax);
3396 #define INIT_STRTOIMAX
3399 #if SANITIZER_INTERCEPT_MBSTOWCS
3400 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3402 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3403 // FIXME: under ASan the call below may write to freed memory and corrupt
3404 // its metadata. See
3405 // https://github.com/google/sanitizers/issues/321.
3406 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3407 if (res != (SIZE_T) - 1 && dest) {
3408 SIZE_T write_cnt = res + (res < len);
3409 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3414 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3417 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3418 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3419 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3420 // FIXME: under ASan the call below may write to freed memory and corrupt
3421 // its metadata. See
3422 // https://github.com/google/sanitizers/issues/321.
3423 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3424 if (res != (SIZE_T)(-1) && dest && src) {
3425 // This function, and several others, may or may not write the terminating
3426 // \0 character. They write it iff they clear *src.
3427 SIZE_T write_cnt = res + !*src;
3428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3433 #define INIT_MBSTOWCS \
3434 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3435 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3437 #define INIT_MBSTOWCS
3440 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3441 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3442 SIZE_T len, void *ps) {
3444 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3446 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3447 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3449 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3450 // FIXME: under ASan the call below may write to freed memory and corrupt
3451 // its metadata. See
3452 // https://github.com/google/sanitizers/issues/321.
3453 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3454 if (res != (SIZE_T)(-1) && dest && src) {
3455 SIZE_T write_cnt = res + !*src;
3456 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3461 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3463 #define INIT_MBSNRTOWCS
3466 #if SANITIZER_INTERCEPT_WCSTOMBS
3467 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3469 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3470 // FIXME: under ASan the call below may write to freed memory and corrupt
3471 // its metadata. See
3472 // https://github.com/google/sanitizers/issues/321.
3473 SIZE_T res = REAL(wcstombs)(dest, src, len);
3474 if (res != (SIZE_T) - 1 && dest) {
3475 SIZE_T write_cnt = res + (res < len);
3476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3481 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3484 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3485 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3486 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3487 // FIXME: under ASan the call below may write to freed memory and corrupt
3488 // its metadata. See
3489 // https://github.com/google/sanitizers/issues/321.
3490 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3491 if (res != (SIZE_T) - 1 && dest && src) {
3492 SIZE_T write_cnt = res + !*src;
3493 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3498 #define INIT_WCSTOMBS \
3499 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3500 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3502 #define INIT_WCSTOMBS
3505 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3506 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3507 SIZE_T len, void *ps) {
3509 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3511 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3512 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3514 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3515 // FIXME: under ASan the call below may write to freed memory and corrupt
3516 // its metadata. See
3517 // https://github.com/google/sanitizers/issues/321.
3518 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3519 if (res != ((SIZE_T)-1) && dest && src) {
3520 SIZE_T write_cnt = res + !*src;
3521 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3526 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3528 #define INIT_WCSNRTOMBS
3532 #if SANITIZER_INTERCEPT_WCRTOMB
3533 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3535 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3536 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3539 return REAL(wcrtomb)(dest, src, ps);
3541 char local_dest[32];
3542 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3543 if (res != ((SIZE_T)-1)) {
3544 CHECK_LE(res, sizeof(local_dest));
3545 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3546 REAL(memcpy)(dest, local_dest, res);
3551 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3553 #define INIT_WCRTOMB
3556 #if SANITIZER_INTERCEPT_WCTOMB
3557 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3559 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3561 return REAL(wctomb)(dest, src);
3563 char local_dest[32];
3564 int res = REAL(wctomb)(local_dest, src);
3566 CHECK_LE(res, sizeof(local_dest));
3567 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3568 REAL(memcpy)(dest, local_dest, res);
3573 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3578 #if SANITIZER_INTERCEPT_TCGETATTR
3579 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3581 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3582 // FIXME: under ASan the call below may write to freed memory and corrupt
3583 // its metadata. See
3584 // https://github.com/google/sanitizers/issues/321.
3585 int res = REAL(tcgetattr)(fd, termios_p);
3586 if (!res && termios_p)
3587 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3591 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3593 #define INIT_TCGETATTR
3596 #if SANITIZER_INTERCEPT_REALPATH
3597 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3599 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3600 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3602 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3603 // version of a versioned symbol. For realpath(), this gives us something
3604 // (called __old_realpath) that does not handle NULL in the second argument.
3605 // Handle it as part of the interceptor.
3606 char *allocated_path = nullptr;
3608 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3610 char *res = REAL(realpath)(path, resolved_path);
3611 if (allocated_path && !res) WRAP(free)(allocated_path);
3612 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3615 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3617 #define INIT_REALPATH
3620 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3621 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3623 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3624 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3625 char *res = REAL(canonicalize_file_name)(path);
3626 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3629 #define INIT_CANONICALIZE_FILE_NAME \
3630 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3632 #define INIT_CANONICALIZE_FILE_NAME
3635 #if SANITIZER_INTERCEPT_CONFSTR
3636 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3638 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3639 // FIXME: under ASan the call below may write to freed memory and corrupt
3640 // its metadata. See
3641 // https://github.com/google/sanitizers/issues/321.
3642 SIZE_T res = REAL(confstr)(name, buf, len);
3644 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3647 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3649 #define INIT_CONFSTR
3652 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3653 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3655 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3656 // FIXME: under ASan the call below may write to freed memory and corrupt
3657 // its metadata. See
3658 // https://github.com/google/sanitizers/issues/321.
3659 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3660 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3663 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3665 #define INIT_SCHED_GETAFFINITY
3668 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3669 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3671 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3672 int res = REAL(sched_getparam)(pid, param);
3673 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3676 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3678 #define INIT_SCHED_GETPARAM
3681 #if SANITIZER_INTERCEPT_STRERROR
3682 INTERCEPTOR(char *, strerror, int errnum) {
3684 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3685 COMMON_INTERCEPTOR_STRERROR();
3686 char *res = REAL(strerror)(errnum);
3687 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3690 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3692 #define INIT_STRERROR
3695 #if SANITIZER_INTERCEPT_STRERROR_R
3696 // There are 2 versions of strerror_r:
3697 // * POSIX version returns 0 on success, negative error code on failure,
3698 // writes message to buf.
3699 // * GNU version returns message pointer, which points to either buf or some
3701 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3702 SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3703 SANITIZER_FREEBSD || SANITIZER_OPENBSD
3704 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3705 // At least on OSX, buf contents are valid even when the call fails.
3706 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3708 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3709 // FIXME: under ASan the call below may write to freed memory and corrupt
3710 // its metadata. See
3711 // https://github.com/google/sanitizers/issues/321.
3712 int res = REAL(strerror_r)(errnum, buf, buflen);
3714 SIZE_T sz = internal_strnlen(buf, buflen);
3715 if (sz < buflen) ++sz;
3716 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3721 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3723 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3724 // FIXME: under ASan the call below may write to freed memory and corrupt
3725 // its metadata. See
3726 // https://github.com/google/sanitizers/issues/321.
3727 char *res = REAL(strerror_r)(errnum, buf, buflen);
3729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3731 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3734 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3736 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3738 #define INIT_STRERROR_R
3741 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3742 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3744 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3745 // FIXME: under ASan the call below may write to freed memory and corrupt
3746 // its metadata. See
3747 // https://github.com/google/sanitizers/issues/321.
3748 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3749 // This version always returns a null-terminated string.
3751 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3754 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3756 #define INIT_XPG_STRERROR_R
3759 #if SANITIZER_INTERCEPT_SCANDIR
3760 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3761 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3762 const struct __sanitizer_dirent **);
3764 static THREADLOCAL scandir_filter_f scandir_filter;
3765 static THREADLOCAL scandir_compar_f scandir_compar;
3767 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3768 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3769 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3770 return scandir_filter(dir);
3773 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3774 const struct __sanitizer_dirent **b) {
3775 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3776 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3777 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3778 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3779 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3780 return scandir_compar(a, b);
3783 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3784 scandir_filter_f filter, scandir_compar_f compar) {
3786 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3787 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3788 scandir_filter = filter;
3789 scandir_compar = compar;
3790 // FIXME: under ASan the call below may write to freed memory and corrupt
3791 // its metadata. See
3792 // https://github.com/google/sanitizers/issues/321.
3793 int res = REAL(scandir)(dirp, namelist,
3794 filter ? wrapped_scandir_filter : nullptr,
3795 compar ? wrapped_scandir_compar : nullptr);
3796 scandir_filter = nullptr;
3797 scandir_compar = nullptr;
3798 if (namelist && res > 0) {
3799 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3800 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3801 for (int i = 0; i < res; ++i)
3802 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3803 (*namelist)[i]->d_reclen);
3807 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3809 #define INIT_SCANDIR
3812 #if SANITIZER_INTERCEPT_SCANDIR64
3813 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3814 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3815 const struct __sanitizer_dirent64 **);
3817 static THREADLOCAL scandir64_filter_f scandir64_filter;
3818 static THREADLOCAL scandir64_compar_f scandir64_compar;
3820 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3821 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3822 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3823 return scandir64_filter(dir);
3826 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3827 const struct __sanitizer_dirent64 **b) {
3828 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3829 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3830 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3831 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3832 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3833 return scandir64_compar(a, b);
3836 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3837 scandir64_filter_f filter, scandir64_compar_f compar) {
3839 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3840 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3841 scandir64_filter = filter;
3842 scandir64_compar = compar;
3843 // FIXME: under ASan the call below may write to freed memory and corrupt
3844 // its metadata. See
3845 // https://github.com/google/sanitizers/issues/321.
3847 REAL(scandir64)(dirp, namelist,
3848 filter ? wrapped_scandir64_filter : nullptr,
3849 compar ? wrapped_scandir64_compar : nullptr);
3850 scandir64_filter = nullptr;
3851 scandir64_compar = nullptr;
3852 if (namelist && res > 0) {
3853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3855 for (int i = 0; i < res; ++i)
3856 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3857 (*namelist)[i]->d_reclen);
3861 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3863 #define INIT_SCANDIR64
3866 #if SANITIZER_INTERCEPT_GETGROUPS
3867 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3869 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3870 // FIXME: under ASan the call below may write to freed memory and corrupt
3871 // its metadata. See
3872 // https://github.com/google/sanitizers/issues/321.
3873 int res = REAL(getgroups)(size, lst);
3874 if (res >= 0 && lst && size > 0)
3875 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3878 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3880 #define INIT_GETGROUPS
3883 #if SANITIZER_INTERCEPT_POLL
3884 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3885 __sanitizer_nfds_t nfds) {
3886 for (unsigned i = 0; i < nfds; ++i) {
3887 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3888 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3892 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3893 __sanitizer_nfds_t nfds) {
3894 for (unsigned i = 0; i < nfds; ++i)
3895 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3896 sizeof(fds[i].revents));
3899 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3902 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3903 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3904 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3905 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3908 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3913 #if SANITIZER_INTERCEPT_PPOLL
3914 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3915 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3917 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3918 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3920 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3921 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3923 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3924 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3927 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3932 #if SANITIZER_INTERCEPT_WORDEXP
3933 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3935 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3936 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3937 // FIXME: under ASan the call below may write to freed memory and corrupt
3938 // its metadata. See
3939 // https://github.com/google/sanitizers/issues/321.
3940 int res = REAL(wordexp)(s, p, flags);
3942 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3945 sizeof(*p->we_wordv) * p->we_wordc);
3946 for (uptr i = 0; i < p->we_wordc; ++i) {
3947 char *w = p->we_wordv[i];
3948 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3953 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3955 #define INIT_WORDEXP
3958 #if SANITIZER_INTERCEPT_SIGWAIT
3959 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3961 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3962 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3963 // FIXME: under ASan the call below may write to freed memory and corrupt
3964 // its metadata. See
3965 // https://github.com/google/sanitizers/issues/321.
3966 int res = REAL(sigwait)(set, sig);
3967 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3970 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3972 #define INIT_SIGWAIT
3975 #if SANITIZER_INTERCEPT_SIGWAITINFO
3976 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3978 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3979 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3980 // FIXME: under ASan the call below may write to freed memory and corrupt
3981 // its metadata. See
3982 // https://github.com/google/sanitizers/issues/321.
3983 int res = REAL(sigwaitinfo)(set, info);
3984 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3987 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3989 #define INIT_SIGWAITINFO
3992 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3993 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3996 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3997 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3998 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3999 // FIXME: under ASan the call below may write to freed memory and corrupt
4000 // its metadata. See
4001 // https://github.com/google/sanitizers/issues/321.
4002 int res = REAL(sigtimedwait)(set, info, timeout);
4003 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4006 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4008 #define INIT_SIGTIMEDWAIT
4011 #if SANITIZER_INTERCEPT_SIGSETOPS
4012 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4014 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4015 // FIXME: under ASan the call below may write to freed memory and corrupt
4016 // its metadata. See
4017 // https://github.com/google/sanitizers/issues/321.
4018 int res = REAL(sigemptyset)(set);
4019 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4023 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4025 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4026 // FIXME: under ASan the call below may write to freed memory and corrupt
4027 // its metadata. See
4028 // https://github.com/google/sanitizers/issues/321.
4029 int res = REAL(sigfillset)(set);
4030 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4033 #define INIT_SIGSETOPS \
4034 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4035 COMMON_INTERCEPT_FUNCTION(sigfillset);
4037 #define INIT_SIGSETOPS
4040 #if SANITIZER_INTERCEPT_SIGPENDING
4041 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4043 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4044 // FIXME: under ASan the call below may write to freed memory and corrupt
4045 // its metadata. See
4046 // https://github.com/google/sanitizers/issues/321.
4047 int res = REAL(sigpending)(set);
4048 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4051 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4053 #define INIT_SIGPENDING
4056 #if SANITIZER_INTERCEPT_SIGPROCMASK
4057 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4058 __sanitizer_sigset_t *oldset) {
4060 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4061 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
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.
4065 int res = REAL(sigprocmask)(how, set, oldset);
4067 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4070 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4072 #define INIT_SIGPROCMASK
4075 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4076 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4077 __sanitizer_sigset_t *oldset) {
4079 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4080 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4081 // FIXME: under ASan the call below may write to freed memory and corrupt
4082 // its metadata. See
4083 // https://github.com/google/sanitizers/issues/321.
4084 int res = REAL(pthread_sigmask)(how, set, oldset);
4086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4089 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4091 #define INIT_PTHREAD_SIGMASK
4094 #if SANITIZER_INTERCEPT_BACKTRACE
4095 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4097 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4098 // FIXME: under ASan the call below may write to freed memory and corrupt
4099 // its metadata. See
4100 // https://github.com/google/sanitizers/issues/321.
4101 int res = REAL(backtrace)(buffer, size);
4103 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4107 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4109 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4111 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4112 // FIXME: under ASan the call below may write to freed memory and corrupt
4113 // its metadata. See
4114 // https://github.com/google/sanitizers/issues/321.
4115 char **res = REAL(backtrace_symbols)(buffer, size);
4117 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4118 for (int i = 0; i < size; ++i)
4119 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4123 #define INIT_BACKTRACE \
4124 COMMON_INTERCEPT_FUNCTION(backtrace); \
4125 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4127 #define INIT_BACKTRACE
4130 #if SANITIZER_INTERCEPT__EXIT
4131 INTERCEPTOR(void, _exit, int status) {
4133 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4134 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4135 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4136 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4137 if (status == 0) status = status1;
4138 REAL(_exit)(status);
4140 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4145 #if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4146 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4148 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4149 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4150 int res = REAL(pthread_mutex_lock)(m);
4151 if (res == errno_EOWNERDEAD)
4152 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4153 if (res == 0 || res == errno_EOWNERDEAD)
4154 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4155 if (res == errno_EINVAL)
4156 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4160 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4162 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4163 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4164 int res = REAL(pthread_mutex_unlock)(m);
4165 if (res == errno_EINVAL)
4166 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4170 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4171 #define INIT_PTHREAD_MUTEX_UNLOCK \
4172 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4174 #define INIT_PTHREAD_MUTEX_LOCK
4175 #define INIT_PTHREAD_MUTEX_UNLOCK
4178 #if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4179 INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4180 return WRAP(pthread_mutex_lock)(m);
4183 INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4184 return WRAP(pthread_mutex_unlock)(m);
4187 #define INIT___PTHREAD_MUTEX_LOCK \
4188 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4189 #define INIT___PTHREAD_MUTEX_UNLOCK \
4190 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4192 #define INIT___PTHREAD_MUTEX_LOCK
4193 #define INIT___PTHREAD_MUTEX_UNLOCK
4196 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4197 INTERCEPTOR(int, __libc_mutex_lock, void *m)
4198 ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4200 INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4201 ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4203 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4204 ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4206 #define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4207 #define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4208 #define INIT___LIBC_THR_SETCANCELSTATE \
4209 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4211 #define INIT___LIBC_MUTEX_LOCK
4212 #define INIT___LIBC_MUTEX_UNLOCK
4213 #define INIT___LIBC_THR_SETCANCELSTATE
4216 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4217 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4218 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4219 if (mnt->mnt_fsname)
4220 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4221 REAL(strlen)(mnt->mnt_fsname) + 1);
4223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4224 REAL(strlen)(mnt->mnt_dir) + 1);
4226 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4227 REAL(strlen)(mnt->mnt_type) + 1);
4229 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4230 REAL(strlen)(mnt->mnt_opts) + 1);
4234 #if SANITIZER_INTERCEPT_GETMNTENT
4235 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4237 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4238 __sanitizer_mntent *res = REAL(getmntent)(fp);
4239 if (res) write_mntent(ctx, res);
4242 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4244 #define INIT_GETMNTENT
4247 #if SANITIZER_INTERCEPT_GETMNTENT_R
4248 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4249 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4251 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4252 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4253 if (res) write_mntent(ctx, res);
4256 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4258 #define INIT_GETMNTENT_R
4261 #if SANITIZER_INTERCEPT_STATFS
4262 INTERCEPTOR(int, statfs, char *path, void *buf) {
4264 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4265 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4266 // FIXME: under ASan the call below may write to freed memory and corrupt
4267 // its metadata. See
4268 // https://github.com/google/sanitizers/issues/321.
4269 int res = REAL(statfs)(path, buf);
4270 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4273 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4275 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4276 // FIXME: under ASan the call below may write to freed memory and corrupt
4277 // its metadata. See
4278 // https://github.com/google/sanitizers/issues/321.
4279 int res = REAL(fstatfs)(fd, buf);
4280 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4283 #define INIT_STATFS \
4284 COMMON_INTERCEPT_FUNCTION(statfs); \
4285 COMMON_INTERCEPT_FUNCTION(fstatfs);
4290 #if SANITIZER_INTERCEPT_STATFS64
4291 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4293 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4294 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4295 // FIXME: under ASan the call below may write to freed memory and corrupt
4296 // its metadata. See
4297 // https://github.com/google/sanitizers/issues/321.
4298 int res = REAL(statfs64)(path, buf);
4299 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4302 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4304 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4305 // FIXME: under ASan the call below may write to freed memory and corrupt
4306 // its metadata. See
4307 // https://github.com/google/sanitizers/issues/321.
4308 int res = REAL(fstatfs64)(fd, buf);
4309 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4312 #define INIT_STATFS64 \
4313 COMMON_INTERCEPT_FUNCTION(statfs64); \
4314 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4316 #define INIT_STATFS64
4319 #if SANITIZER_INTERCEPT_STATVFS
4320 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4322 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4323 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4324 // FIXME: under ASan the call below may write to freed memory and corrupt
4325 // its metadata. See
4326 // https://github.com/google/sanitizers/issues/321.
4327 int res = REAL(statvfs)(path, buf);
4328 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4331 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4333 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4334 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4335 // FIXME: under ASan the call below may write to freed memory and corrupt
4336 // its metadata. See
4337 // https://github.com/google/sanitizers/issues/321.
4338 int res = REAL(fstatvfs)(fd, buf);
4340 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4342 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4346 #define INIT_STATVFS \
4347 COMMON_INTERCEPT_FUNCTION(statvfs); \
4348 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4350 #define INIT_STATVFS
4353 #if SANITIZER_INTERCEPT_STATVFS64
4354 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4356 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4357 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4358 // FIXME: under ASan the call below may write to freed memory and corrupt
4359 // its metadata. See
4360 // https://github.com/google/sanitizers/issues/321.
4361 int res = REAL(statvfs64)(path, buf);
4362 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4365 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4367 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4368 // FIXME: under ASan the call below may write to freed memory and corrupt
4369 // its metadata. See
4370 // https://github.com/google/sanitizers/issues/321.
4371 int res = REAL(fstatvfs64)(fd, buf);
4372 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4375 #define INIT_STATVFS64 \
4376 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4377 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4379 #define INIT_STATVFS64
4382 #if SANITIZER_INTERCEPT_INITGROUPS
4383 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4385 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4386 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4387 int res = REAL(initgroups)(user, group);
4390 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4392 #define INIT_INITGROUPS
4395 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4396 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4398 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4399 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4400 char *res = REAL(ether_ntoa)(addr);
4401 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4404 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4406 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4407 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4408 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4409 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4412 #define INIT_ETHER_NTOA_ATON \
4413 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4414 COMMON_INTERCEPT_FUNCTION(ether_aton);
4416 #define INIT_ETHER_NTOA_ATON
4419 #if SANITIZER_INTERCEPT_ETHER_HOST
4420 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4422 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4423 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4424 // FIXME: under ASan the call below may write to freed memory and corrupt
4425 // its metadata. See
4426 // https://github.com/google/sanitizers/issues/321.
4427 int res = REAL(ether_ntohost)(hostname, addr);
4428 if (!res && hostname)
4429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4432 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4434 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4436 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4437 // FIXME: under ASan the call below may write to freed memory and corrupt
4438 // its metadata. See
4439 // https://github.com/google/sanitizers/issues/321.
4440 int res = REAL(ether_hostton)(hostname, addr);
4441 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4444 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4447 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4448 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4449 // FIXME: under ASan the call below may write to freed memory and corrupt
4450 // its metadata. See
4451 // https://github.com/google/sanitizers/issues/321.
4452 int res = REAL(ether_line)(line, addr, hostname);
4454 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4456 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4460 #define INIT_ETHER_HOST \
4461 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4462 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4463 COMMON_INTERCEPT_FUNCTION(ether_line);
4465 #define INIT_ETHER_HOST
4468 #if SANITIZER_INTERCEPT_ETHER_R
4469 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4471 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4472 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
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 char *res = REAL(ether_ntoa_r)(addr, buf);
4477 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4480 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4481 __sanitizer_ether_addr *addr) {
4483 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4484 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4485 // FIXME: under ASan the call below may write to freed memory and corrupt
4486 // its metadata. See
4487 // https://github.com/google/sanitizers/issues/321.
4488 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4489 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4492 #define INIT_ETHER_R \
4493 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4494 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4496 #define INIT_ETHER_R
4499 #if SANITIZER_INTERCEPT_SHMCTL
4500 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4502 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4503 // FIXME: under ASan the call below may write to freed memory and corrupt
4504 // its metadata. See
4505 // https://github.com/google/sanitizers/issues/321.
4506 int res = REAL(shmctl)(shmid, cmd, buf);
4509 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4510 sz = sizeof(__sanitizer_shmid_ds);
4511 else if (cmd == shmctl_ipc_info)
4512 sz = struct_shminfo_sz;
4513 else if (cmd == shmctl_shm_info)
4514 sz = struct_shm_info_sz;
4515 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4519 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4524 #if SANITIZER_INTERCEPT_RANDOM_R
4525 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4527 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4528 // FIXME: under ASan the call below may write to freed memory and corrupt
4529 // its metadata. See
4530 // https://github.com/google/sanitizers/issues/321.
4531 int res = REAL(random_r)(buf, result);
4533 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4536 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4538 #define INIT_RANDOM_R
4541 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4542 // its metadata. See
4543 // https://github.com/google/sanitizers/issues/321.
4544 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4545 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4546 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4547 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4548 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4549 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4550 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4551 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4552 INTERCEPTOR(int, fn, void *attr, void *r) { \
4554 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4555 int res = REAL(fn)(attr, r); \
4556 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4559 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4560 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4561 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4562 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4563 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4564 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4565 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4566 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4567 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4568 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4571 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4572 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4573 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4574 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4575 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4576 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4578 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4579 // FIXME: under ASan the call below may write to freed memory and corrupt
4580 // its metadata. See
4581 // https://github.com/google/sanitizers/issues/321.
4582 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4584 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4585 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4590 // We may need to call the real pthread_attr_getstack from the run-time
4591 // in sanitizer_common, but we don't want to include the interception headers
4592 // there. So, just define this function here.
4593 namespace __sanitizer {
4595 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4596 return REAL(pthread_attr_getstack)(attr, addr, size);
4599 } // namespace __sanitizer
4601 #define INIT_PTHREAD_ATTR_GET \
4602 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4603 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4604 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4605 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4606 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4608 #define INIT_PTHREAD_ATTR_GET
4611 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4612 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4613 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4615 #define INIT_PTHREAD_ATTR_GET_SCHED \
4616 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4617 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4619 #define INIT_PTHREAD_ATTR_GET_SCHED
4622 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4623 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4625 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4626 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4628 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4631 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4632 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4635 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4637 // FIXME: under ASan the call below may write to freed memory and corrupt
4638 // its metadata. See
4639 // https://github.com/google/sanitizers/issues/321.
4640 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4641 if (!res && cpusetsize && cpuset)
4642 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4646 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4647 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4649 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4652 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4653 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4654 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4655 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4657 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4660 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4661 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4662 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4663 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4665 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4668 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4669 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4670 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4671 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4673 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4676 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4677 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4678 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4679 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4681 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4684 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4685 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4686 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4687 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4689 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4692 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4693 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4694 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4695 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4697 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4700 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4701 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4702 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4703 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4705 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4708 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4709 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4710 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4711 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4713 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4716 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4717 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4718 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4719 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4721 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4724 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4725 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4726 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4727 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4729 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4732 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4733 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4734 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4735 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4737 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4740 #if SANITIZER_INTERCEPT_TMPNAM
4741 INTERCEPTOR(char *, tmpnam, char *s) {
4743 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4744 char *res = REAL(tmpnam)(s);
4747 // FIXME: under ASan the call below may write to freed memory and corrupt
4748 // its metadata. See
4749 // https://github.com/google/sanitizers/issues/321.
4750 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4752 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4756 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4761 #if SANITIZER_INTERCEPT_TMPNAM_R
4762 INTERCEPTOR(char *, tmpnam_r, char *s) {
4764 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4765 // FIXME: under ASan the call below may write to freed memory and corrupt
4766 // its metadata. See
4767 // https://github.com/google/sanitizers/issues/321.
4768 char *res = REAL(tmpnam_r)(s);
4769 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4772 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4774 #define INIT_TMPNAM_R
4777 #if SANITIZER_INTERCEPT_TTYNAME
4778 INTERCEPTOR(char *, ttyname, int fd) {
4780 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
4781 char *res = REAL(ttyname)(fd);
4783 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4786 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
4788 #define INIT_TTYNAME
4791 #if SANITIZER_INTERCEPT_TTYNAME_R
4792 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4794 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4795 int res = REAL(ttyname_r)(fd, name, namesize);
4797 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4800 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4802 #define INIT_TTYNAME_R
4805 #if SANITIZER_INTERCEPT_TEMPNAM
4806 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4808 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4809 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4810 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4811 char *res = REAL(tempnam)(dir, pfx);
4812 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4815 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4817 #define INIT_TEMPNAM
4820 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4821 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4823 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4824 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4825 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4826 return REAL(pthread_setname_np)(thread, name);
4828 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4829 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4830 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4832 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4833 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4834 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4835 internal_snprintf(newname, sizeof(newname), name, arg);
4836 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4837 return REAL(pthread_setname_np)(thread, name, arg);
4839 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4841 #define INIT_PTHREAD_SETNAME_NP
4844 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4845 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4847 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4848 int res = REAL(pthread_getname_np)(thread, name, len);
4850 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4853 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4855 #define INIT_PTHREAD_GETNAME_NP
4858 #if SANITIZER_INTERCEPT_SINCOS
4859 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4861 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4862 // FIXME: under ASan the call below may write to freed memory and corrupt
4863 // its metadata. See
4864 // https://github.com/google/sanitizers/issues/321.
4865 REAL(sincos)(x, sin, cos);
4866 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4867 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4869 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4871 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4872 // FIXME: under ASan the call below may write to freed memory and corrupt
4873 // its metadata. See
4874 // https://github.com/google/sanitizers/issues/321.
4875 REAL(sincosf)(x, sin, cos);
4876 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4877 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4879 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4881 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4882 // FIXME: under ASan the call below may write to freed memory and corrupt
4883 // its metadata. See
4884 // https://github.com/google/sanitizers/issues/321.
4885 REAL(sincosl)(x, sin, cos);
4886 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4887 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4889 #define INIT_SINCOS \
4890 COMMON_INTERCEPT_FUNCTION(sincos); \
4891 COMMON_INTERCEPT_FUNCTION(sincosf); \
4892 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4897 #if SANITIZER_INTERCEPT_REMQUO
4898 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4900 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4901 // FIXME: under ASan the call below may write to freed memory and corrupt
4902 // its metadata. See
4903 // https://github.com/google/sanitizers/issues/321.
4904 double res = REAL(remquo)(x, y, quo);
4905 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4908 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4910 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4911 // FIXME: under ASan the call below may write to freed memory and corrupt
4912 // its metadata. See
4913 // https://github.com/google/sanitizers/issues/321.
4914 float res = REAL(remquof)(x, y, quo);
4915 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4918 #define INIT_REMQUO \
4919 COMMON_INTERCEPT_FUNCTION(remquo); \
4920 COMMON_INTERCEPT_FUNCTION(remquof);
4925 #if SANITIZER_INTERCEPT_REMQUOL
4926 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4928 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4929 // FIXME: under ASan the call below may write to freed memory and corrupt
4930 // its metadata. See
4931 // https://github.com/google/sanitizers/issues/321.
4932 long double res = REAL(remquol)(x, y, quo);
4933 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4936 #define INIT_REMQUOL \
4937 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4939 #define INIT_REMQUOL
4942 #if SANITIZER_INTERCEPT_LGAMMA
4944 INTERCEPTOR(double, lgamma, double x) {
4946 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4947 double res = REAL(lgamma)(x);
4948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4951 INTERCEPTOR(float, lgammaf, float x) {
4953 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4954 float res = REAL(lgammaf)(x);
4955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4958 #define INIT_LGAMMA \
4959 COMMON_INTERCEPT_FUNCTION(lgamma); \
4960 COMMON_INTERCEPT_FUNCTION(lgammaf);
4965 #if SANITIZER_INTERCEPT_LGAMMAL
4966 INTERCEPTOR(long double, lgammal, long double x) {
4968 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4969 long double res = REAL(lgammal)(x);
4970 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4973 #define INIT_LGAMMAL \
4974 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4976 #define INIT_LGAMMAL
4979 #if SANITIZER_INTERCEPT_LGAMMA_R
4980 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4982 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
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 double res = REAL(lgamma_r)(x, signp);
4987 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4990 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4992 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4993 // FIXME: under ASan the call below may write to freed memory and corrupt
4994 // its metadata. See
4995 // https://github.com/google/sanitizers/issues/321.
4996 float res = REAL(lgammaf_r)(x, signp);
4997 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5000 #define INIT_LGAMMA_R \
5001 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
5002 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
5004 #define INIT_LGAMMA_R
5007 #if SANITIZER_INTERCEPT_LGAMMAL_R
5008 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5010 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5011 // FIXME: under ASan the call below may write to freed memory and corrupt
5012 // its metadata. See
5013 // https://github.com/google/sanitizers/issues/321.
5014 long double res = REAL(lgammal_r)(x, signp);
5015 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5018 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5020 #define INIT_LGAMMAL_R
5023 #if SANITIZER_INTERCEPT_DRAND48_R
5024 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5026 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5027 // FIXME: under ASan the call below may write to freed memory and corrupt
5028 // its metadata. See
5029 // https://github.com/google/sanitizers/issues/321.
5030 int res = REAL(drand48_r)(buffer, result);
5031 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5034 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5036 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5037 // FIXME: under ASan the call below may write to freed memory and corrupt
5038 // its metadata. See
5039 // https://github.com/google/sanitizers/issues/321.
5040 int res = REAL(lrand48_r)(buffer, result);
5041 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5044 #define INIT_DRAND48_R \
5045 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5046 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5048 #define INIT_DRAND48_R
5051 #if SANITIZER_INTERCEPT_RAND_R
5052 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5054 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5055 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5056 return REAL(rand_r)(seedp);
5058 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5063 #if SANITIZER_INTERCEPT_GETLINE
5064 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5066 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5067 // FIXME: under ASan the call below may write to freed memory and corrupt
5068 // its metadata. See
5069 // https://github.com/google/sanitizers/issues/321.
5070 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5072 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5073 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5079 // FIXME: under ASan the call below may write to freed memory and corrupt its
5081 // https://github.com/google/sanitizers/issues/321.
5082 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5085 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5086 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5088 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5089 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5090 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5095 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5097 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5099 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5100 // with its own body.
5101 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5103 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5105 #define INIT_GETLINE \
5106 COMMON_INTERCEPT_FUNCTION(getline); \
5107 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5108 COMMON_INTERCEPT_FUNCTION(getdelim);
5110 #define INIT_GETLINE
5113 #if SANITIZER_INTERCEPT_ICONV
5114 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5115 char **outbuf, SIZE_T *outbytesleft) {
5117 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5120 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5121 if (inbuf && inbytesleft)
5122 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5124 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5125 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5126 // FIXME: under ASan the call below may write to freed memory and corrupt
5127 // its metadata. See
5128 // https://github.com/google/sanitizers/issues/321.
5129 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5130 if (outbuf && *outbuf > outbuf_orig) {
5131 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5132 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5136 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5141 #if SANITIZER_INTERCEPT_TIMES
5142 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5144 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5145 // FIXME: under ASan the call below may write to freed memory and corrupt
5146 // its metadata. See
5147 // https://github.com/google/sanitizers/issues/321.
5148 __sanitizer_clock_t res = REAL(times)(tms);
5149 if (res != (__sanitizer_clock_t)-1 && tms)
5150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5153 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5158 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5160 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5161 // If you see any crashes around this functions, there are 2 known issues with
5162 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5163 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5164 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5165 // to access thread local variables (it should not happen normally,
5166 // because sanitizers use initial-exec tls model).
5167 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5169 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5170 void *res = REAL(__tls_get_addr)(arg);
5171 uptr tls_begin, tls_end;
5172 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5173 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5175 // New DTLS block has been allocated.
5176 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5181 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5182 // mostly the same semantics as __tls_get_addr, but its presence enables
5183 // some optimizations in linker (which are safe to ignore here).
5184 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5185 visibility("default")))
5186 void *__tls_get_addr_opt(void *arg);
5188 #else // SANITIZER_S390
5189 // On s390, we have to intercept two functions here:
5190 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5191 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5192 // a proper pointer. It is used by dlsym for TLS symbols.
5193 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5194 // descriptor offset as an argument instead of a pointer. GOT address
5195 // is passed in r12, so it's necessary to write it in assembly. This is
5196 // the function used by the compiler.
5197 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5198 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5199 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5200 extern "C" uptr __tls_get_offset(void *arg);
5201 extern "C" uptr __interceptor___tls_get_offset(void *arg);
5202 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5204 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5205 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5206 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5207 void *ptr = reinterpret_cast<void *>(res + tp);
5208 uptr tls_begin, tls_end;
5209 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5210 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5212 // New DTLS block has been allocated.
5213 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5217 // We need a hidden symbol aliasing the above, so that we can jump
5218 // directly to it from the assembly below.
5219 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5220 visibility("hidden")))
5221 uptr __tls_get_addr_hidden(void *arg);
5222 // Now carefully intercept __tls_get_offset.
5225 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5226 // exports our symbol.
5227 ".weak __tls_get_offset\n"
5228 ".type __tls_get_offset, @function\n"
5229 "__tls_get_offset:\n"
5230 ".global __interceptor___tls_get_offset\n"
5231 ".type __interceptor___tls_get_offset, @function\n"
5232 "__interceptor___tls_get_offset:\n"
5234 "la %r2, 0(%r2,%r12)\n"
5235 "jg __tls_get_addr_hidden\n"
5238 "0: la %r2,0(%r2,%r12)\n"
5239 "l %r4,1f-0b(%r3)\n"
5241 "1: .long __tls_get_addr_hidden - 0b\n"
5243 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5244 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5245 ".type __tls_get_offset_wrapper, @function\n"
5246 "__tls_get_offset_wrapper:\n"
5253 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5255 #endif // SANITIZER_S390
5257 #define INIT_TLS_GET_ADDR
5260 #if SANITIZER_INTERCEPT_LISTXATTR
5261 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5263 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5264 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5265 // FIXME: under ASan the call below may write to freed memory and corrupt
5266 // its metadata. See
5267 // https://github.com/google/sanitizers/issues/321.
5268 SSIZE_T res = REAL(listxattr)(path, list, size);
5269 // Here and below, size == 0 is a special case where nothing is written to the
5270 // buffer, and res contains the desired buffer size.
5271 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5274 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5276 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5277 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5278 // FIXME: under ASan the call below may write to freed memory and corrupt
5279 // its metadata. See
5280 // https://github.com/google/sanitizers/issues/321.
5281 SSIZE_T res = REAL(llistxattr)(path, list, size);
5282 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5285 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5287 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5288 // FIXME: under ASan the call below may write to freed memory and corrupt
5289 // its metadata. See
5290 // https://github.com/google/sanitizers/issues/321.
5291 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5292 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5295 #define INIT_LISTXATTR \
5296 COMMON_INTERCEPT_FUNCTION(listxattr); \
5297 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5298 COMMON_INTERCEPT_FUNCTION(flistxattr);
5300 #define INIT_LISTXATTR
5303 #if SANITIZER_INTERCEPT_GETXATTR
5304 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5307 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5308 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5309 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5310 // FIXME: under ASan the call below may write to freed memory and corrupt
5311 // its metadata. See
5312 // https://github.com/google/sanitizers/issues/321.
5313 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5314 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5317 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5320 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5321 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5322 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5323 // FIXME: under ASan the call below may write to freed memory and corrupt
5324 // its metadata. See
5325 // https://github.com/google/sanitizers/issues/321.
5326 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5327 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5330 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5333 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5334 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5335 // FIXME: under ASan the call below may write to freed memory and corrupt
5336 // its metadata. See
5337 // https://github.com/google/sanitizers/issues/321.
5338 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5339 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5342 #define INIT_GETXATTR \
5343 COMMON_INTERCEPT_FUNCTION(getxattr); \
5344 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5345 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5347 #define INIT_GETXATTR
5350 #if SANITIZER_INTERCEPT_GETRESID
5351 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5353 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5354 // FIXME: under ASan the call below may write to freed memory and corrupt
5355 // its metadata. See
5356 // https://github.com/google/sanitizers/issues/321.
5357 int res = REAL(getresuid)(ruid, euid, suid);
5359 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5360 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5361 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5365 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5367 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5368 // FIXME: under ASan the call below may write to freed memory and corrupt
5369 // its metadata. See
5370 // https://github.com/google/sanitizers/issues/321.
5371 int res = REAL(getresgid)(rgid, egid, sgid);
5373 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5374 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5375 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5379 #define INIT_GETRESID \
5380 COMMON_INTERCEPT_FUNCTION(getresuid); \
5381 COMMON_INTERCEPT_FUNCTION(getresgid);
5383 #define INIT_GETRESID
5386 #if SANITIZER_INTERCEPT_GETIFADDRS
5387 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5388 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5389 // intercept it to poison the memory again.
5390 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5392 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5393 // FIXME: under ASan the call below may write to freed memory and corrupt
5394 // its metadata. See
5395 // https://github.com/google/sanitizers/issues/321.
5396 int res = REAL(getifaddrs)(ifap);
5397 if (res == 0 && ifap) {
5398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5399 __sanitizer_ifaddrs *p = *ifap;
5401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5403 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5404 REAL(strlen)(p->ifa_name) + 1);
5406 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5409 // On Linux this is a union, but the other member also points to a
5410 // struct sockaddr, so the following is sufficient.
5412 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5413 // FIXME(smatveev): Unpoison p->ifa_data as well.
5419 #define INIT_GETIFADDRS \
5420 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5422 #define INIT_GETIFADDRS
5425 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5426 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5428 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5429 // FIXME: under ASan the call below may write to freed memory and corrupt
5430 // its metadata. See
5431 // https://github.com/google/sanitizers/issues/321.
5432 char *res = REAL(if_indextoname)(ifindex, ifname);
5434 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5437 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5439 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5441 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5442 return REAL(if_nametoindex)(ifname);
5444 #define INIT_IF_INDEXTONAME \
5445 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5446 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5448 #define INIT_IF_INDEXTONAME
5451 #if SANITIZER_INTERCEPT_CAPGET
5452 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5454 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5456 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5457 // FIXME: under ASan the call below may write to freed memory and corrupt
5458 // its metadata. See
5459 // https://github.com/google/sanitizers/issues/321.
5460 int res = REAL(capget)(hdrp, datap);
5461 if (res == 0 && datap)
5462 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5463 // We can also return -1 and write to hdrp->version if the version passed in
5464 // hdrp->version is unsupported. But that's not a trivial condition to check,
5465 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5468 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5470 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5472 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5474 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5475 return REAL(capset)(hdrp, datap);
5477 #define INIT_CAPGET \
5478 COMMON_INTERCEPT_FUNCTION(capget); \
5479 COMMON_INTERCEPT_FUNCTION(capset);
5484 #if SANITIZER_INTERCEPT_AEABI_MEM
5485 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5487 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5490 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5492 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5495 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5497 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5500 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5502 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5505 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5507 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5510 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5512 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5515 // Note the argument order.
5516 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5518 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5521 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5523 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5526 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5528 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5531 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5533 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5536 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5538 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5541 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5543 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5546 #define INIT_AEABI_MEM \
5547 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5548 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5549 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5550 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5551 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5552 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5553 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5554 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5555 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5556 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5557 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5558 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5560 #define INIT_AEABI_MEM
5561 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5563 #if SANITIZER_INTERCEPT___BZERO
5564 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5566 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5568 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5570 #define INIT___BZERO
5571 #endif // SANITIZER_INTERCEPT___BZERO
5573 #if SANITIZER_INTERCEPT_BZERO
5574 INTERCEPTOR(void *, bzero, void *block, uptr size) {
5576 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5578 #define INIT_BZERO COMMON_INTERCEPT_FUNCTION(bzero);
5581 #endif // SANITIZER_INTERCEPT_BZERO
5583 #if SANITIZER_INTERCEPT_FTIME
5584 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5586 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5587 // FIXME: under ASan the call below may write to freed memory and corrupt
5588 // its metadata. See
5589 // https://github.com/google/sanitizers/issues/321.
5590 int res = REAL(ftime)(tp);
5592 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5595 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5598 #endif // SANITIZER_INTERCEPT_FTIME
5600 #if SANITIZER_INTERCEPT_XDR
5601 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5602 unsigned size, int op) {
5604 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
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 REAL(xdrmem_create)(xdrs, addr, size, op);
5609 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5610 if (op == __sanitizer_XDR_ENCODE) {
5611 // It's not obvious how much data individual xdr_ routines write.
5612 // Simply unpoison the entire target buffer in advance.
5613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5617 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5619 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5620 // FIXME: under ASan the call below may write to freed memory and corrupt
5621 // its metadata. See
5622 // https://github.com/google/sanitizers/issues/321.
5623 REAL(xdrstdio_create)(xdrs, file, op);
5624 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5627 // FIXME: under ASan the call below may write to freed memory and corrupt
5628 // its metadata. See
5629 // https://github.com/google/sanitizers/issues/321.
5630 #define XDR_INTERCEPTOR(F, T) \
5631 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5633 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5634 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5635 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5636 int res = REAL(F)(xdrs, p); \
5637 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5642 XDR_INTERCEPTOR(xdr_short, short)
5643 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5644 XDR_INTERCEPTOR(xdr_int, int)
5645 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5646 XDR_INTERCEPTOR(xdr_long, long)
5647 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5648 XDR_INTERCEPTOR(xdr_hyper, long long)
5649 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5650 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5651 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5652 XDR_INTERCEPTOR(xdr_int8_t, u8)
5653 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5654 XDR_INTERCEPTOR(xdr_int16_t, u16)
5655 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5656 XDR_INTERCEPTOR(xdr_int32_t, u32)
5657 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5658 XDR_INTERCEPTOR(xdr_int64_t, u64)
5659 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5660 XDR_INTERCEPTOR(xdr_quad_t, long long)
5661 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5662 XDR_INTERCEPTOR(xdr_bool, bool)
5663 XDR_INTERCEPTOR(xdr_enum, int)
5664 XDR_INTERCEPTOR(xdr_char, char)
5665 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5666 XDR_INTERCEPTOR(xdr_float, float)
5667 XDR_INTERCEPTOR(xdr_double, double)
5669 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5670 // wrapstring, sizeof
5672 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5675 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5676 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5677 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5678 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5679 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5681 // FIXME: under ASan the call below may write to freed memory and corrupt
5682 // its metadata. See
5683 // https://github.com/google/sanitizers/issues/321.
5684 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5685 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5686 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5687 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5688 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5693 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5696 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5697 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5698 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5699 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5701 // FIXME: under ASan the call below may write to freed memory and corrupt
5702 // its metadata. See
5703 // https://github.com/google/sanitizers/issues/321.
5704 int res = REAL(xdr_string)(xdrs, p, maxsize);
5705 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5706 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5714 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5715 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5716 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5717 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5718 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5719 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5720 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5721 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5722 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5723 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5724 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5725 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5726 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5727 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5728 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5729 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5730 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5731 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5732 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5733 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5734 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5735 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5736 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5737 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5738 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5739 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5740 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5741 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5742 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5743 COMMON_INTERCEPT_FUNCTION(xdr_string);
5746 #endif // SANITIZER_INTERCEPT_XDR
5748 #if SANITIZER_INTERCEPT_TSEARCH
5749 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5750 int (*compar)(const void *, const void *)) {
5752 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5753 // FIXME: under ASan the call below may write to freed memory and corrupt
5754 // its metadata. See
5755 // https://github.com/google/sanitizers/issues/321.
5756 void *res = REAL(tsearch)(key, rootp, compar);
5757 if (res && *(void **)res == key)
5758 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5761 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5763 #define INIT_TSEARCH
5766 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5767 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5768 void unpoison_file(__sanitizer_FILE *fp) {
5769 #if SANITIZER_HAS_STRUCT_FILE
5770 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5771 #if SANITIZER_NETBSD
5772 if (fp->_bf._base && fp->_bf._size > 0)
5773 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
5776 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5777 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5778 fp->_IO_read_end - fp->_IO_read_base);
5780 #endif // SANITIZER_HAS_STRUCT_FILE
5784 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5785 // These guys are called when a .c source is built with -O2.
5786 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5788 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5789 int res = REAL(__uflow)(fp);
5793 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5795 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5796 int res = REAL(__underflow)(fp);
5800 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5802 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5803 int res = REAL(__overflow)(fp, ch);
5807 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5809 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5810 int res = REAL(__wuflow)(fp);
5814 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5816 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5817 int res = REAL(__wunderflow)(fp);
5821 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5823 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5824 int res = REAL(__woverflow)(fp, ch);
5828 #define INIT_LIBIO_INTERNALS \
5829 COMMON_INTERCEPT_FUNCTION(__uflow); \
5830 COMMON_INTERCEPT_FUNCTION(__underflow); \
5831 COMMON_INTERCEPT_FUNCTION(__overflow); \
5832 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5833 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5834 COMMON_INTERCEPT_FUNCTION(__woverflow);
5836 #define INIT_LIBIO_INTERNALS
5839 #if SANITIZER_INTERCEPT_FOPEN
5840 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5842 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5843 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5844 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5845 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5846 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5847 if (res) unpoison_file(res);
5850 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5852 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5853 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5854 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5855 if (res) unpoison_file(res);
5858 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5859 __sanitizer_FILE *fp) {
5861 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5862 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5863 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5864 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5865 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5866 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5867 if (res) unpoison_file(res);
5870 #define INIT_FOPEN \
5871 COMMON_INTERCEPT_FUNCTION(fopen); \
5872 COMMON_INTERCEPT_FUNCTION(fdopen); \
5873 COMMON_INTERCEPT_FUNCTION(freopen);
5878 #if SANITIZER_INTERCEPT_FOPEN64
5879 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5881 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5882 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5883 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5884 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5885 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5886 if (res) unpoison_file(res);
5889 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5890 __sanitizer_FILE *fp) {
5892 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5893 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5894 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5895 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5896 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5897 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5898 if (res) unpoison_file(res);
5901 #define INIT_FOPEN64 \
5902 COMMON_INTERCEPT_FUNCTION(fopen64); \
5903 COMMON_INTERCEPT_FUNCTION(freopen64);
5905 #define INIT_FOPEN64
5908 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5909 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5911 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5912 // FIXME: under ASan the call below may write to freed memory and corrupt
5913 // its metadata. See
5914 // https://github.com/google/sanitizers/issues/321.
5915 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5920 FileMetadata file = {ptr, sizeloc};
5921 SetInterceptorMetadata(res, file);
5925 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5928 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5929 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5934 FileMetadata file = {(char **)ptr, sizeloc};
5935 SetInterceptorMetadata(res, file);
5939 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5942 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5943 // FIXME: under ASan the call below may write to freed memory and corrupt
5944 // its metadata. See
5945 // https://github.com/google/sanitizers/issues/321.
5946 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5947 if (res) unpoison_file(res);
5950 #define INIT_OPEN_MEMSTREAM \
5951 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5952 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5953 COMMON_INTERCEPT_FUNCTION(fmemopen);
5955 #define INIT_OPEN_MEMSTREAM
5958 #if SANITIZER_INTERCEPT_OBSTACK
5959 static void initialize_obstack(__sanitizer_obstack *obstack) {
5960 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5962 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5963 sizeof(*obstack->chunk));
5966 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5967 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5968 void (*free_fn)(uptr arg, void *p)) {
5970 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5972 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5973 if (res) initialize_obstack(obstack);
5976 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5977 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5979 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5981 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5982 if (res) initialize_obstack(obstack);
5985 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5987 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5988 REAL(_obstack_newchunk)(obstack, length);
5990 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5991 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5993 #define INIT_OBSTACK \
5994 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5995 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5996 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5998 #define INIT_OBSTACK
6001 #if SANITIZER_INTERCEPT_FFLUSH
6002 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
6004 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
6005 int res = REAL(fflush)(fp);
6006 // FIXME: handle fp == NULL
6008 const FileMetadata *m = GetInterceptorMetadata(fp);
6009 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6013 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6018 #if SANITIZER_INTERCEPT_FCLOSE
6019 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6021 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6022 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6023 const FileMetadata *m = GetInterceptorMetadata(fp);
6024 int res = REAL(fclose)(fp);
6026 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6027 DeleteInterceptorMetadata(fp);
6031 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6036 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6037 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6039 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6040 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6041 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
6042 void *res = REAL(dlopen)(filename, flag);
6043 Symbolizer::GetOrInit()->InvalidateModuleList();
6044 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6048 INTERCEPTOR(int, dlclose, void *handle) {
6050 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6051 int res = REAL(dlclose)(handle);
6052 Symbolizer::GetOrInit()->InvalidateModuleList();
6053 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6056 #define INIT_DLOPEN_DLCLOSE \
6057 COMMON_INTERCEPT_FUNCTION(dlopen); \
6058 COMMON_INTERCEPT_FUNCTION(dlclose);
6060 #define INIT_DLOPEN_DLCLOSE
6063 #if SANITIZER_INTERCEPT_GETPASS
6064 INTERCEPTOR(char *, getpass, const char *prompt) {
6066 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6068 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
6069 char *res = REAL(getpass)(prompt);
6070 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
6074 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6076 #define INIT_GETPASS
6079 #if SANITIZER_INTERCEPT_TIMERFD
6080 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6083 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6085 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6086 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6087 if (res != -1 && old_value)
6088 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6092 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6094 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6095 int res = REAL(timerfd_gettime)(fd, curr_value);
6096 if (res != -1 && curr_value)
6097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6100 #define INIT_TIMERFD \
6101 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6102 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6104 #define INIT_TIMERFD
6107 #if SANITIZER_INTERCEPT_MLOCKX
6108 // Linux kernel has a bug that leads to kernel deadlock if a process
6109 // maps TBs of memory and then calls mlock().
6110 static void MlockIsUnsupported() {
6111 static atomic_uint8_t printed;
6112 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6114 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6118 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6119 MlockIsUnsupported();
6123 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6124 MlockIsUnsupported();
6128 INTERCEPTOR(int, mlockall, int flags) {
6129 MlockIsUnsupported();
6133 INTERCEPTOR(int, munlockall, void) {
6134 MlockIsUnsupported();
6138 #define INIT_MLOCKX \
6139 COMMON_INTERCEPT_FUNCTION(mlock); \
6140 COMMON_INTERCEPT_FUNCTION(munlock); \
6141 COMMON_INTERCEPT_FUNCTION(mlockall); \
6142 COMMON_INTERCEPT_FUNCTION(munlockall);
6146 #endif // SANITIZER_INTERCEPT_MLOCKX
6148 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6149 struct WrappedCookie {
6151 __sanitizer_cookie_io_functions_t real_io_funcs;
6154 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6155 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6156 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6157 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6158 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6161 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6162 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6163 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6164 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6165 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6168 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6169 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6170 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6171 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6172 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6173 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6177 static int wrapped_close(void *cookie) {
6178 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6179 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6180 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6181 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6182 InternalFree(wrapped_cookie);
6186 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6187 __sanitizer_cookie_io_functions_t io_funcs) {
6189 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6190 WrappedCookie *wrapped_cookie =
6191 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6192 wrapped_cookie->real_cookie = cookie;
6193 wrapped_cookie->real_io_funcs = io_funcs;
6194 __sanitizer_FILE *res =
6195 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6196 wrapped_seek, wrapped_close});
6200 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6202 #define INIT_FOPENCOOKIE
6203 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6205 #if SANITIZER_INTERCEPT_SEM
6206 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6208 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6209 // Workaround a bug in glibc's "old" semaphore implementation by
6210 // zero-initializing the sem_t contents. This has to be done here because
6211 // interceptors bind to the lowest symbols version by default, hitting the
6212 // buggy code path while the non-sanitized build of the same code works fine.
6213 REAL(memset)(s, 0, sizeof(*s));
6214 int res = REAL(sem_init)(s, pshared, value);
6218 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6220 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6221 int res = REAL(sem_destroy)(s);
6225 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6227 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6228 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6230 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6235 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6237 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6238 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6240 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6245 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6247 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6248 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6249 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6251 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6256 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6258 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6259 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6260 int res = REAL(sem_post)(s);
6264 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6266 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6267 int res = REAL(sem_getvalue)(s, sval);
6269 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6270 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6275 COMMON_INTERCEPT_FUNCTION(sem_init); \
6276 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6277 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6278 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6279 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6280 COMMON_INTERCEPT_FUNCTION(sem_post); \
6281 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6284 #endif // SANITIZER_INTERCEPT_SEM
6286 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6287 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6289 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6290 int res = REAL(pthread_setcancelstate)(state, oldstate);
6291 if (res == 0 && oldstate != nullptr)
6292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6296 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6298 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6299 int res = REAL(pthread_setcanceltype)(type, oldtype);
6300 if (res == 0 && oldtype != nullptr)
6301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6304 #define INIT_PTHREAD_SETCANCEL \
6305 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6306 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6308 #define INIT_PTHREAD_SETCANCEL
6311 #if SANITIZER_INTERCEPT_MINCORE
6312 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6314 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6315 int res = REAL(mincore)(addr, length, vec);
6317 uptr page_size = GetPageSizeCached();
6318 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6323 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6325 #define INIT_MINCORE
6328 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6329 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6330 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6333 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6334 remote_iov, riovcnt, flags);
6335 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6338 write_iovec(ctx, local_iov, liovcnt, res);
6342 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6343 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6346 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6347 remote_iov, riovcnt, flags);
6348 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6351 read_iovec(ctx, local_iov, liovcnt, res);
6354 #define INIT_PROCESS_VM_READV \
6355 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6356 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6358 #define INIT_PROCESS_VM_READV
6361 #if SANITIZER_INTERCEPT_CTERMID
6362 INTERCEPTOR(char *, ctermid, char *s) {
6364 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6365 char *res = REAL(ctermid)(s);
6367 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6371 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6373 #define INIT_CTERMID
6376 #if SANITIZER_INTERCEPT_CTERMID_R
6377 INTERCEPTOR(char *, ctermid_r, char *s) {
6379 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6380 char *res = REAL(ctermid_r)(s);
6382 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6386 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6388 #define INIT_CTERMID_R
6391 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6392 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6394 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6395 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6396 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6400 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6404 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6405 void *srcaddr, int *addrlen) {
6407 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6409 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6411 if (srcaddr) srcaddr_sz = *addrlen;
6412 (void)srcaddr_sz; // prevent "set but not used" warning
6413 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6415 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6417 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6418 Min((SIZE_T)*addrlen, srcaddr_sz));
6422 #define INIT_RECV_RECVFROM \
6423 COMMON_INTERCEPT_FUNCTION(recv); \
6424 COMMON_INTERCEPT_FUNCTION(recvfrom);
6426 #define INIT_RECV_RECVFROM
6429 #if SANITIZER_INTERCEPT_SEND_SENDTO
6430 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6432 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6434 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6435 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6437 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6438 if (common_flags()->intercept_send && res > 0)
6439 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6443 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6444 void *dstaddr, int addrlen) {
6446 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6448 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6449 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6451 // Can't check dstaddr as it may have uninitialized padding at the end.
6452 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6453 if (common_flags()->intercept_send && res > 0)
6454 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6457 #define INIT_SEND_SENDTO \
6458 COMMON_INTERCEPT_FUNCTION(send); \
6459 COMMON_INTERCEPT_FUNCTION(sendto);
6461 #define INIT_SEND_SENDTO
6464 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6465 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6467 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6468 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6469 int res = REAL(eventfd_read)(fd, value);
6471 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6472 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6476 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6478 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6480 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6481 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6483 int res = REAL(eventfd_write)(fd, value);
6486 #define INIT_EVENTFD_READ_WRITE \
6487 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6488 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6490 #define INIT_EVENTFD_READ_WRITE
6493 #if SANITIZER_INTERCEPT_STAT
6494 INTERCEPTOR(int, stat, const char *path, void *buf) {
6496 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6497 if (common_flags()->intercept_stat)
6498 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6499 int res = REAL(stat)(path, buf);
6501 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6504 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6509 #if SANITIZER_INTERCEPT_LSTAT
6510 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6512 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6513 if (common_flags()->intercept_stat)
6514 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6515 int res = REAL(lstat)(path, buf);
6517 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6520 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6525 #if SANITIZER_INTERCEPT___XSTAT
6526 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6528 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6529 if (common_flags()->intercept_stat)
6530 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6531 int res = REAL(__xstat)(version, path, buf);
6533 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6536 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6538 #define INIT___XSTAT
6541 #if SANITIZER_INTERCEPT___XSTAT64
6542 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6544 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6545 if (common_flags()->intercept_stat)
6546 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6547 int res = REAL(__xstat64)(version, path, buf);
6549 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6552 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6554 #define INIT___XSTAT64
6557 #if SANITIZER_INTERCEPT___LXSTAT
6558 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6560 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6561 if (common_flags()->intercept_stat)
6562 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6563 int res = REAL(__lxstat)(version, path, buf);
6565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6568 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6570 #define INIT___LXSTAT
6573 #if SANITIZER_INTERCEPT___LXSTAT64
6574 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6576 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6577 if (common_flags()->intercept_stat)
6578 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6579 int res = REAL(__lxstat64)(version, path, buf);
6581 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6584 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6586 #define INIT___LXSTAT64
6589 // FIXME: add other *stat interceptor
6591 #if SANITIZER_INTERCEPT_UTMP
6592 INTERCEPTOR(void *, getutent, int dummy) {
6594 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6595 void *res = REAL(getutent)(dummy);
6597 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6600 INTERCEPTOR(void *, getutid, void *ut) {
6602 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6603 void *res = REAL(getutid)(ut);
6605 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6608 INTERCEPTOR(void *, getutline, void *ut) {
6610 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6611 void *res = REAL(getutline)(ut);
6613 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6617 COMMON_INTERCEPT_FUNCTION(getutent); \
6618 COMMON_INTERCEPT_FUNCTION(getutid); \
6619 COMMON_INTERCEPT_FUNCTION(getutline);
6624 #if SANITIZER_INTERCEPT_UTMPX
6625 INTERCEPTOR(void *, getutxent, int dummy) {
6627 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6628 void *res = REAL(getutxent)(dummy);
6630 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6633 INTERCEPTOR(void *, getutxid, void *ut) {
6635 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6636 void *res = REAL(getutxid)(ut);
6638 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6641 INTERCEPTOR(void *, getutxline, void *ut) {
6643 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6644 void *res = REAL(getutxline)(ut);
6646 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6649 INTERCEPTOR(void *, pututxline, const void *ut) {
6651 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
6653 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
6654 void *res = REAL(pututxline)(ut);
6656 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
6659 #define INIT_UTMPX \
6660 COMMON_INTERCEPT_FUNCTION(getutxent); \
6661 COMMON_INTERCEPT_FUNCTION(getutxid); \
6662 COMMON_INTERCEPT_FUNCTION(getutxline); \
6663 COMMON_INTERCEPT_FUNCTION(pututxline);
6668 #if SANITIZER_INTERCEPT_GETLOADAVG
6669 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6671 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6672 int res = REAL(getloadavg)(loadavg, nelem);
6674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6677 #define INIT_GETLOADAVG \
6678 COMMON_INTERCEPT_FUNCTION(getloadavg);
6680 #define INIT_GETLOADAVG
6683 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6684 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6688 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6692 INTERCEPTOR(int, mprobe, void *ptr) {
6697 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6699 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6700 SIZE_T res = REAL(wcslen)(s);
6701 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6705 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6707 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6708 SIZE_T res = REAL(wcsnlen)(s, n);
6709 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6712 #define INIT_WCSLEN \
6713 COMMON_INTERCEPT_FUNCTION(wcslen); \
6714 COMMON_INTERCEPT_FUNCTION(wcsnlen);
6716 #if SANITIZER_INTERCEPT_WCSCAT
6717 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6719 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6720 SIZE_T src_size = REAL(wcslen)(src);
6721 SIZE_T dst_size = REAL(wcslen)(dst);
6722 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6723 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6724 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6725 (src_size + 1) * sizeof(wchar_t));
6726 return REAL(wcscat)(dst, src);
6729 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6731 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6732 SIZE_T src_size = REAL(wcsnlen)(src, n);
6733 SIZE_T dst_size = REAL(wcslen)(dst);
6734 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6735 Min(src_size + 1, n) * sizeof(wchar_t));
6736 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6737 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6738 (src_size + 1) * sizeof(wchar_t));
6739 return REAL(wcsncat)(dst, src, n);
6741 #define INIT_WCSCAT \
6742 COMMON_INTERCEPT_FUNCTION(wcscat); \
6743 COMMON_INTERCEPT_FUNCTION(wcsncat);
6748 #if SANITIZER_INTERCEPT_WCSDUP
6749 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
6751 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
6752 SIZE_T len = REAL(wcslen)(s);
6753 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
6754 wchar_t *result = REAL(wcsdup)(s);
6756 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
6760 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
6765 #if SANITIZER_INTERCEPT_STRXFRM
6766 static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6768 static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6770 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
6773 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6774 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
6775 sizeof(*src) * (RealStrLen(src) + 1)); \
6776 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
6778 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6782 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6783 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6786 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6788 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6791 #define INIT_STRXFRM \
6792 COMMON_INTERCEPT_FUNCTION(strxfrm); \
6793 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6795 #define INIT_STRXFRM
6798 #if SANITIZER_INTERCEPT___STRXFRM_L
6799 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6801 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6804 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6806 #define INIT___STRXFRM_L
6809 #if SANITIZER_INTERCEPT_WCSXFRM
6810 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6811 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6814 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6816 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6819 #define INIT_WCSXFRM \
6820 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6821 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6823 #define INIT_WCSXFRM
6826 #if SANITIZER_INTERCEPT___WCSXFRM_L
6827 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6829 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6832 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6834 #define INIT___WCSXFRM_L
6837 #if SANITIZER_INTERCEPT_ACCT
6838 INTERCEPTOR(int, acct, const char *file) {
6840 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6842 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6843 return REAL(acct)(file);
6845 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6850 #if SANITIZER_INTERCEPT_USER_FROM_UID
6851 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6854 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6855 user = REAL(user_from_uid)(uid, nouser);
6857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6860 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6862 #define INIT_USER_FROM_UID
6865 #if SANITIZER_INTERCEPT_UID_FROM_USER
6866 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6869 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6871 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6872 res = REAL(uid_from_user)(name, uid);
6874 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6877 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6879 #define INIT_UID_FROM_USER
6882 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
6883 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6886 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6887 group = REAL(group_from_gid)(gid, nogroup);
6889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6892 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6894 #define INIT_GROUP_FROM_GID
6897 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
6898 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6901 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6903 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6904 res = REAL(gid_from_group)(group, gid);
6906 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6909 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6911 #define INIT_GID_FROM_GROUP
6914 #if SANITIZER_INTERCEPT_ACCESS
6915 INTERCEPTOR(int, access, const char *path, int mode) {
6917 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6919 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6920 return REAL(access)(path, mode);
6922 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6927 #if SANITIZER_INTERCEPT_FACCESSAT
6928 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6930 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6932 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6933 return REAL(faccessat)(fd, path, mode, flags);
6935 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6937 #define INIT_FACCESSAT
6940 #if SANITIZER_INTERCEPT_GETGROUPLIST
6941 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6945 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6947 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6949 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6950 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6951 if (!res && groups && ngroups) {
6952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6958 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6960 #define INIT_GETGROUPLIST
6963 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6964 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6965 int maxgrp, int *ngroups) {
6968 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6971 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6972 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6973 if (!res && groups && ngroups) {
6974 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6975 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6980 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6982 #define INIT_GETGROUPMEMBERSHIP
6985 #if SANITIZER_INTERCEPT_READLINK
6986 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6988 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6989 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6990 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6992 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6996 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6998 #define INIT_READLINK
7001 #if SANITIZER_INTERCEPT_READLINKAT
7002 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
7005 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
7006 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7007 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7013 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7015 #define INIT_READLINKAT
7018 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7019 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7020 struct file_handle *handle, int *mount_id, int flags) {
7022 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7024 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
7026 __sanitizer_file_handle *sanitizer_handle =
7027 reinterpret_cast<__sanitizer_file_handle*>(handle);
7028 COMMON_INTERCEPTOR_READ_RANGE(
7029 ctx, &sanitizer_handle->handle_bytes,
7030 sizeof(sanitizer_handle->handle_bytes));
7032 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7034 COMMON_INTERCEPTOR_WRITE_RANGE(
7035 ctx, &sanitizer_handle->handle_bytes,
7036 sizeof(sanitizer_handle->handle_bytes));
7037 COMMON_INTERCEPTOR_WRITE_RANGE(
7038 ctx, &sanitizer_handle->handle_type,
7039 sizeof(sanitizer_handle->handle_type));
7040 COMMON_INTERCEPTOR_WRITE_RANGE(
7041 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7042 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7047 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7049 #define INIT_NAME_TO_HANDLE_AT
7052 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7053 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7056 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7058 __sanitizer_file_handle *sanitizer_handle =
7059 reinterpret_cast<__sanitizer_file_handle*>(handle);
7060 COMMON_INTERCEPTOR_READ_RANGE(
7061 ctx, &sanitizer_handle->handle_bytes,
7062 sizeof(sanitizer_handle->handle_bytes));
7063 COMMON_INTERCEPTOR_READ_RANGE(
7064 ctx, &sanitizer_handle->handle_type,
7065 sizeof(sanitizer_handle->handle_type));
7066 COMMON_INTERCEPTOR_READ_RANGE(
7067 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7069 return REAL(open_by_handle_at)(mount_fd, handle, flags);
7072 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7074 #define INIT_OPEN_BY_HANDLE_AT
7077 #if SANITIZER_INTERCEPT_STRLCPY
7078 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7081 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7083 // Keep strnlen as macro argument, as macro may ignore it.
7084 COMMON_INTERCEPTOR_READ_STRING(
7085 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7087 res = REAL(strlcpy)(dst, src, size);
7088 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
7092 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7095 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7096 // src is checked in the strlcpy() interceptor
7098 len = internal_strnlen(dst, size);
7099 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7101 // Reuse the rest of the code in the strlcpy() interceptor
7102 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7104 #define INIT_STRLCPY \
7105 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7106 COMMON_INTERCEPT_FUNCTION(strlcat);
7108 #define INIT_STRLCPY
7111 #if SANITIZER_INTERCEPT_MMAP
7112 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7115 if (common_flags()->detect_write_exec)
7116 ReportMmapWriteExec(prot);
7117 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7118 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7119 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7120 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7123 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7125 if (common_flags()->detect_write_exec)
7126 ReportMmapWriteExec(prot);
7127 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7128 return (int)internal_mprotect(addr, sz, prot);
7129 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7130 MprotectMallocZones(addr, prot);
7131 return REAL(mprotect)(addr, sz, prot);
7134 COMMON_INTERCEPT_FUNCTION(mmap); \
7135 COMMON_INTERCEPT_FUNCTION(mprotect);
7140 #if SANITIZER_INTERCEPT_MMAP64
7141 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7144 if (common_flags()->detect_write_exec)
7145 ReportMmapWriteExec(prot);
7146 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7147 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7148 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7149 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7151 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7156 #if SANITIZER_INTERCEPT_DEVNAME
7157 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7160 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7161 name = REAL(devname)(dev, type);
7163 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7166 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7168 #define INIT_DEVNAME
7171 #if SANITIZER_INTERCEPT_DEVNAME_R
7172 #if SANITIZER_NETBSD
7173 #define DEVNAME_R_RETTYPE int
7174 #define DEVNAME_R_SUCCESS(x) (!(x))
7176 #define DEVNAME_R_RETTYPE char*
7177 #define DEVNAME_R_SUCCESS(x) (x)
7179 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7182 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7183 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7184 if (DEVNAME_R_SUCCESS(res))
7185 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7188 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7190 #define INIT_DEVNAME_R
7193 #if SANITIZER_INTERCEPT_FGETLN
7194 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7196 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7197 char *str = REAL(fgetln)(stream, len);
7199 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7200 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7204 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7209 #if SANITIZER_INTERCEPT_STRMODE
7210 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7212 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7213 REAL(strmode)(mode, bp);
7215 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7217 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7219 #define INIT_STRMODE
7222 #if SANITIZER_INTERCEPT_TTYENT
7223 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7225 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7226 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7228 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7231 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7233 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7235 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7236 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7238 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7241 INTERCEPTOR(int, setttyentpath, char *path) {
7243 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7245 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7246 return REAL(setttyentpath)(path);
7248 #define INIT_TTYENT \
7249 COMMON_INTERCEPT_FUNCTION(getttyent); \
7250 COMMON_INTERCEPT_FUNCTION(getttynam); \
7251 COMMON_INTERCEPT_FUNCTION(setttyentpath)
7256 #if SANITIZER_INTERCEPT_PROTOENT
7257 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7259 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7260 struct __sanitizer_protoent *p = REAL(getprotoent)();
7262 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7266 SIZE_T pp_size = 1; // One handles the trailing \0
7268 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7269 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7272 pp_size * sizeof(char **));
7277 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7279 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7281 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7282 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7286 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7288 SIZE_T pp_size = 1; // One handles the trailing \0
7290 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7294 pp_size * sizeof(char **));
7299 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7301 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7302 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7306 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7308 SIZE_T pp_size = 1; // One handles the trailing \0
7310 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7311 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7313 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7314 pp_size * sizeof(char **));
7318 #define INIT_PROTOENT \
7319 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7320 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7321 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7323 #define INIT_PROTOENT
7326 #if SANITIZER_INTERCEPT_NETENT
7327 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7329 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7330 struct __sanitizer_netent *n = REAL(getnetent)();
7332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7336 SIZE_T nn_size = 1; // One handles the trailing \0
7338 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7339 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7341 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7342 nn_size * sizeof(char **));
7347 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7349 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7351 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7352 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7358 SIZE_T nn_size = 1; // One handles the trailing \0
7360 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7361 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7363 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7364 nn_size * sizeof(char **));
7369 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7371 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7372 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7376 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7378 SIZE_T nn_size = 1; // One handles the trailing \0
7380 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7383 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7384 nn_size * sizeof(char **));
7388 #define INIT_NETENT \
7389 COMMON_INTERCEPT_FUNCTION(getnetent); \
7390 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7391 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7396 #if SANITIZER_INTERCEPT_GETMNTINFO
7397 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7399 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7400 int cnt = REAL(getmntinfo)(mntbufp, flags);
7401 if (cnt > 0 && mntbufp) {
7402 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7404 #if SANITIZER_NETBSD
7405 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7412 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7414 #define INIT_GETMNTINFO
7417 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7418 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7421 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7423 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7424 REAL(mi_vector_hash)(key, len, seed, hashes);
7426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7428 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7430 #define INIT_MI_VECTOR_HASH
7433 #if SANITIZER_INTERCEPT_SETVBUF
7434 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7437 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7438 int ret = REAL(setvbuf)(stream, buf, mode, size);
7440 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7442 unpoison_file(stream);
7446 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7448 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7449 REAL(setbuf)(stream, buf);
7451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7454 unpoison_file(stream);
7457 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
7459 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
7460 REAL(setbuffer)(stream, buf, mode);
7462 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7465 unpoison_file(stream);
7468 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7470 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7471 REAL(setlinebuf)(stream);
7473 unpoison_file(stream);
7475 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7476 COMMON_INTERCEPT_FUNCTION(setbuf); \
7477 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7478 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7480 #define INIT_SETVBUF
7483 #if SANITIZER_INTERCEPT_GETVFSSTAT
7484 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7486 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7487 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7492 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7494 #define INIT_GETVFSSTAT
7497 #if SANITIZER_INTERCEPT_REGEX
7498 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7500 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7502 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, REAL(strlen)(pattern) + 1);
7503 int res = REAL(regcomp)(preg, pattern, cflags);
7505 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7508 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7509 struct __sanitizer_regmatch *pmatch[], int eflags) {
7511 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7513 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7515 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, REAL(strlen)(string) + 1);
7516 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7518 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7521 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7522 SIZE_T errbuf_size) {
7524 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7526 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7527 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7529 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, REAL(strlen)(errbuf) + 1);
7532 INTERCEPTOR(void, regfree, const void *preg) {
7534 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7536 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7537 REAL(regfree)(preg);
7539 #define INIT_REGEX \
7540 COMMON_INTERCEPT_FUNCTION(regcomp); \
7541 COMMON_INTERCEPT_FUNCTION(regexec); \
7542 COMMON_INTERCEPT_FUNCTION(regerror); \
7543 COMMON_INTERCEPT_FUNCTION(regfree);
7548 #if SANITIZER_INTERCEPT_REGEXSUB
7549 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7550 const struct __sanitizer_regmatch *rm, const char *str) {
7552 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7554 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7555 // The implementation demands and hardcodes 10 elements
7557 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7559 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
7560 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7562 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
7565 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7566 const struct __sanitizer_regmatch *rm, const char *sstr) {
7568 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7570 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7571 // Hardcode 10 elements as this is hardcoded size
7573 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7575 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, REAL(strlen)(sstr) + 1);
7576 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7577 if (res > 0 && buf) {
7578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, REAL(strlen)(*buf) + 1);
7584 #define INIT_REGEXSUB \
7585 COMMON_INTERCEPT_FUNCTION(regnsub); \
7586 COMMON_INTERCEPT_FUNCTION(regasub);
7588 #define INIT_REGEXSUB
7591 #if SANITIZER_INTERCEPT_FTS
7592 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7593 int (*compar)(void **, void **)) {
7595 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
7597 for (char *const *pa = path_argv; ; ++pa) {
7598 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
7601 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
7604 // TODO(kamil): handle compar callback
7605 void *fts = REAL(fts_open)(path_argv, options, compar);
7607 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
7611 INTERCEPTOR(void *, fts_read, void *ftsp) {
7613 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
7615 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7616 void *ftsent = REAL(fts_read)(ftsp);
7618 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7622 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
7624 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
7626 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7627 void *ftsent = REAL(fts_children)(ftsp, options);
7629 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7633 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
7635 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
7637 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7639 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
7640 return REAL(fts_set)(ftsp, f, options);
7643 INTERCEPTOR(int, fts_close, void *ftsp) {
7645 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
7647 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7648 return REAL(fts_close)(ftsp);
7651 COMMON_INTERCEPT_FUNCTION(fts_open); \
7652 COMMON_INTERCEPT_FUNCTION(fts_read); \
7653 COMMON_INTERCEPT_FUNCTION(fts_children); \
7654 COMMON_INTERCEPT_FUNCTION(fts_set); \
7655 COMMON_INTERCEPT_FUNCTION(fts_close);
7660 #if SANITIZER_INTERCEPT_SYSCTL
7661 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
7662 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
7664 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7665 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
7666 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
7669 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
7671 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7673 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7674 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
7677 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7685 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
7686 void *newp, SIZE_T newlen) {
7688 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7689 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
7690 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
7693 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7695 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7697 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7698 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
7701 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7703 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7709 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
7712 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
7714 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7716 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7717 int res = REAL(sysctlnametomib)(sname, name, namelenp);
7720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7722 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7728 #define INIT_SYSCTL \
7729 COMMON_INTERCEPT_FUNCTION(sysctl); \
7730 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
7731 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
7736 #if SANITIZER_INTERCEPT_ASYSCTL
7737 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
7739 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
7741 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
7742 void *res = REAL(asysctl)(name, namelen, len);
7744 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7750 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
7752 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
7754 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7755 void *res = REAL(asysctlbyname)(sname, len);
7757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7758 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7762 #define INIT_ASYSCTL \
7763 COMMON_INTERCEPT_FUNCTION(asysctl); \
7764 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
7766 #define INIT_ASYSCTL
7769 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
7770 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
7771 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
7774 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
7777 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7779 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7781 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
7782 // Skip rnode, it's rarely used and not trivial to sanitize
7783 // It's also used mostly internally
7784 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
7787 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7789 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7792 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
7794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
7799 #define INIT_SYSCTLGETMIBINFO \
7800 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
7802 #define INIT_SYSCTLGETMIBINFO
7805 #if SANITIZER_INTERCEPT_NL_LANGINFO
7806 INTERCEPTOR(char *, nl_langinfo, long item) {
7808 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
7809 char *ret = REAL(nl_langinfo)(item);
7811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7814 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
7816 #define INIT_NL_LANGINFO
7819 #if SANITIZER_INTERCEPT_MODCTL
7820 INTERCEPTOR(int, modctl, int operation, void *argp) {
7823 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
7825 if (operation == modctl_load) {
7827 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
7828 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
7829 if (ml->ml_filename)
7830 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
7831 REAL(strlen)(ml->ml_filename) + 1);
7833 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
7835 ret = REAL(modctl)(operation, argp);
7836 } else if (operation == modctl_unload) {
7838 const char *name = (const char *)argp;
7839 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7841 ret = REAL(modctl)(operation, argp);
7842 } else if (operation == modctl_stat) {
7844 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
7846 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
7847 iov_len = iov->iov_len;
7849 ret = REAL(modctl)(operation, argp);
7851 COMMON_INTERCEPTOR_WRITE_RANGE(
7852 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
7853 } else if (operation == modctl_exists) {
7854 ret = REAL(modctl)(operation, argp);
7856 ret = REAL(modctl)(operation, argp);
7861 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
7866 #if SANITIZER_INTERCEPT_STRTONUM
7867 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
7868 long long maxval, const char **errstr) {
7870 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
7872 // TODO(kamil): Implement strtoll as a common inteceptor
7874 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
7875 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
7877 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
7879 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
7881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1);
7885 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
7887 #define INIT_STRTONUM
7890 #if SANITIZER_INTERCEPT_FPARSELN
7891 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
7892 SIZE_T *lineno, const char delim[3], int flags) {
7894 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
7896 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
7898 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
7899 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
7901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
7909 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
7911 #define INIT_FPARSELN
7914 #if SANITIZER_INTERCEPT_STATVFS1
7915 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
7917 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
7918 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7919 int res = REAL(statvfs1)(path, buf, flags);
7920 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7923 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
7925 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
7926 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7927 int res = REAL(fstatvfs1)(fd, buf, flags);
7929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7931 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
7935 #define INIT_STATVFS1 \
7936 COMMON_INTERCEPT_FUNCTION(statvfs1); \
7937 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
7939 #define INIT_STATVFS1
7942 #if SANITIZER_INTERCEPT_STRTOI
7943 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
7944 INTMAX_T low, INTMAX_T high, int *rstatus) {
7946 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
7948 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
7949 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7951 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7955 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
7956 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
7958 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
7960 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
7961 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7963 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7966 #define INIT_STRTOI \
7967 COMMON_INTERCEPT_FUNCTION(strtoi); \
7968 COMMON_INTERCEPT_FUNCTION(strtou)
7973 #if SANITIZER_INTERCEPT_CAPSICUM
7974 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
7977 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
7979 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7980 __sanitizer_cap_rights_t *ret = \
7981 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
7983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7987 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
7990 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
7992 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7993 __sanitizer_cap_rights_t *ret = \
7994 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
7996 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8000 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
8003 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
8005 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8006 __sanitizer_cap_rights_t *ret = \
8007 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8013 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8016 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8018 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8019 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8022 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8023 __sanitizer_cap_rights_t *rights) {
8024 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8027 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8028 __sanitizer_cap_rights_t *rights) {
8029 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8032 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8033 __sanitizer_cap_rights_t *rights) {
8034 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8037 INTERCEPTOR(bool, cap_rights_is_set,
8038 __sanitizer_cap_rights_t *rights) {
8039 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8042 INTERCEPTOR(int, cap_rights_limit, int fd,
8043 const __sanitizer_cap_rights_t *rights) {
8045 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8047 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8049 return REAL(cap_rights_limit)(fd, rights);
8052 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8054 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8055 int ret = REAL(cap_rights_get)(fd, rights);
8057 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8062 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8064 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8066 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8068 return REAL(cap_rights_is_valid(rights));
8071 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8072 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8074 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8076 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8078 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8080 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8085 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8086 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8088 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8090 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8092 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8094 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8099 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8100 const __sanitizer_cap_rights *little) {
8102 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8104 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8106 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8108 return REAL(cap_rights_contains)(big, little);
8111 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8113 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8115 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8117 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8120 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8122 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8123 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8125 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8129 #define INIT_CAPSICUM \
8130 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8131 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8132 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8133 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8134 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8135 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8136 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8137 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8138 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8139 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8140 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8141 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8143 #define INIT_CAPSICUM
8146 #if SANITIZER_INTERCEPT_SHA1
8147 INTERCEPTOR(void, SHA1Init, void *context) {
8149 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8150 REAL(SHA1Init)(context);
8152 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8154 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8156 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8157 if (data && len > 0)
8158 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8160 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8161 REAL(SHA1Update)(context, data, len);
8163 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8165 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8167 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8169 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8170 REAL(SHA1Final)(digest, context);
8172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8174 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8176 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8178 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8180 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8181 REAL(SHA1Transform)(state, buffer);
8183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8185 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8187 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8189 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8190 char *ret = REAL(SHA1End)(context, buf);
8192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8195 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8197 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8199 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8200 char *ret = REAL(SHA1File)(filename, buf);
8202 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8205 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8208 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8210 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8211 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8213 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8216 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8218 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8220 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8221 char *ret = REAL(SHA1Data)(data, len, buf);
8223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8227 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8228 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8229 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8230 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8231 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8232 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8233 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8234 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8239 #if SANITIZER_INTERCEPT_MD4
8240 INTERCEPTOR(void, MD4Init, void *context) {
8242 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8243 REAL(MD4Init)(context);
8245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8248 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8251 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8252 if (data && len > 0)
8253 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8255 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8256 REAL(MD4Update)(context, data, len);
8258 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8261 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8263 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8265 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8266 REAL(MD4Final)(digest, context);
8268 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8271 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8273 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8275 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8276 char *ret = REAL(MD4End)(context, buf);
8278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8282 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8284 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8286 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8287 char *ret = REAL(MD4File)(filename, buf);
8289 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8293 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8296 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8297 if (data && len > 0)
8298 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8299 char *ret = REAL(MD4Data)(data, len, buf);
8301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8306 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8307 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8308 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8309 COMMON_INTERCEPT_FUNCTION(MD4End); \
8310 COMMON_INTERCEPT_FUNCTION(MD4File); \
8311 COMMON_INTERCEPT_FUNCTION(MD4Data)
8316 #if SANITIZER_INTERCEPT_RMD160
8317 INTERCEPTOR(void, RMD160Init, void *context) {
8319 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8320 REAL(RMD160Init)(context);
8322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8324 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8326 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8327 if (data && len > 0)
8328 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8330 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8331 REAL(RMD160Update)(context, data, len);
8333 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8335 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8337 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8339 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8340 REAL(RMD160Final)(digest, context);
8342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8344 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8346 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8348 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8350 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8351 REAL(RMD160Transform)(state, buffer);
8353 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8355 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8357 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8359 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8360 char *ret = REAL(RMD160End)(context, buf);
8362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8365 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8367 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8369 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8370 char *ret = REAL(RMD160File)(filename, buf);
8372 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8375 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8378 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8380 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8381 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8383 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8386 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8388 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8389 if (data && len > 0)
8390 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8391 char *ret = REAL(RMD160Data)(data, len, buf);
8393 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8396 #define INIT_RMD160 \
8397 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8398 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8399 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8400 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8401 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8402 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8403 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8404 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8409 #if SANITIZER_INTERCEPT_MD5
8410 INTERCEPTOR(void, MD5Init, void *context) {
8412 COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8413 REAL(MD5Init)(context);
8415 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8418 INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8421 COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8422 if (data && len > 0)
8423 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8425 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8426 REAL(MD5Update)(context, data, len);
8428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8431 INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8433 COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8435 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8436 REAL(MD5Final)(digest, context);
8438 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8441 INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8443 COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8445 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8446 char *ret = REAL(MD5End)(context, buf);
8448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8452 INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8454 COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8456 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8457 char *ret = REAL(MD5File)(filename, buf);
8459 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8463 INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8466 COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8467 if (data && len > 0)
8468 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8469 char *ret = REAL(MD5Data)(data, len, buf);
8471 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8476 COMMON_INTERCEPT_FUNCTION(MD5Init); \
8477 COMMON_INTERCEPT_FUNCTION(MD5Update); \
8478 COMMON_INTERCEPT_FUNCTION(MD5Final); \
8479 COMMON_INTERCEPT_FUNCTION(MD5End); \
8480 COMMON_INTERCEPT_FUNCTION(MD5File); \
8481 COMMON_INTERCEPT_FUNCTION(MD5Data)
8486 #if SANITIZER_INTERCEPT_FSEEK
8487 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8489 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8490 return REAL(fseek)(stream, offset, whence);
8492 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8494 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8495 return REAL(fseeko)(stream, offset, whence);
8497 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8499 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8500 return REAL(ftell)(stream);
8502 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8504 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8505 return REAL(ftello)(stream);
8507 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8509 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8510 return REAL(rewind)(stream);
8512 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8514 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8515 int ret = REAL(fgetpos)(stream, pos);
8517 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8520 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8522 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8524 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8525 return REAL(fsetpos)(stream, pos);
8527 #define INIT_FSEEK \
8528 COMMON_INTERCEPT_FUNCTION(fseek); \
8529 COMMON_INTERCEPT_FUNCTION(fseeko); \
8530 COMMON_INTERCEPT_FUNCTION(ftell); \
8531 COMMON_INTERCEPT_FUNCTION(ftello); \
8532 COMMON_INTERCEPT_FUNCTION(rewind); \
8533 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8534 COMMON_INTERCEPT_FUNCTION(fsetpos)
8539 #if SANITIZER_INTERCEPT_MD2
8540 INTERCEPTOR(void, MD2Init, void *context) {
8542 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8543 REAL(MD2Init)(context);
8545 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8548 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8551 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8552 if (data && len > 0)
8553 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8555 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8556 REAL(MD2Update)(context, data, len);
8558 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8561 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8563 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8565 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8566 REAL(MD2Final)(digest, context);
8568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8571 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8573 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8575 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8576 char *ret = REAL(MD2End)(context, buf);
8578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8582 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8584 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8586 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8587 char *ret = REAL(MD2File)(filename, buf);
8589 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8593 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8596 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8597 if (data && len > 0)
8598 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8599 char *ret = REAL(MD2Data)(data, len, buf);
8601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8606 COMMON_INTERCEPT_FUNCTION(MD2Init); \
8607 COMMON_INTERCEPT_FUNCTION(MD2Update); \
8608 COMMON_INTERCEPT_FUNCTION(MD2Final); \
8609 COMMON_INTERCEPT_FUNCTION(MD2End); \
8610 COMMON_INTERCEPT_FUNCTION(MD2File); \
8611 COMMON_INTERCEPT_FUNCTION(MD2Data)
8616 #if SANITIZER_INTERCEPT_SHA2
8617 #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
8618 INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
8620 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
8621 REAL(SHA##LEN##_Init)(context); \
8623 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8625 INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
8626 const u8 *data, SIZE_T len) { \
8628 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
8629 if (data && len > 0) \
8630 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8632 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8633 REAL(SHA##LEN##_Update)(context, data, len); \
8635 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8637 INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
8640 CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
8641 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
8643 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8644 REAL(SHA##LEN##_Final)(digest, context); \
8646 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
8647 sizeof(digest[0]) * \
8648 SHA##LEN##_digest_length); \
8650 INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
8652 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
8654 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8655 char *ret = REAL(SHA##LEN##_End)(context, buf); \
8657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8660 INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
8662 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
8664 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8665 char *ret = REAL(SHA##LEN##_File)(filename, buf); \
8667 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8670 INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
8671 OFF_T offset, OFF_T length) { \
8673 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
8676 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8677 char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
8679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8682 INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
8684 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
8685 if (data && len > 0) \
8686 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8687 char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
8689 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8693 SHA2_INTERCEPTORS(224, u32);
8694 SHA2_INTERCEPTORS(256, u32);
8695 SHA2_INTERCEPTORS(384, u64);
8696 SHA2_INTERCEPTORS(512, u64);
8698 #define INIT_SHA2_INTECEPTORS(LEN) \
8699 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
8700 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
8701 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
8702 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
8703 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
8704 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
8705 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
8708 INIT_SHA2_INTECEPTORS(224); \
8709 INIT_SHA2_INTECEPTORS(256); \
8710 INIT_SHA2_INTECEPTORS(384); \
8711 INIT_SHA2_INTECEPTORS(512)
8712 #undef SHA2_INTERCEPTORS
8717 #if SANITIZER_INTERCEPT_VIS
8718 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
8720 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
8721 char *end = REAL(vis)(dst, c, flag, nextc);
8722 // dst is NULL terminated and end points to the NULL char
8724 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8727 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
8729 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
8730 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
8731 // nvis cannot make sure the dst is NULL terminated
8733 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8736 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
8738 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
8740 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8741 int len = REAL(strvis)(dst, src, flag);
8743 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8746 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
8748 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
8750 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8751 int len = REAL(stravis)(dst, src, flag);
8753 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
8755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
8759 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
8761 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
8763 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8764 int len = REAL(strnvis)(dst, dlen, src, flag);
8765 // The interface will be valid even if there is no space for NULL char
8767 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8770 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
8772 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
8774 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8775 int ret = REAL(strvisx)(dst, src, len, flag);
8777 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8780 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8783 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
8785 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8786 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
8787 if (dst && ret >= 0)
8788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8791 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8792 int flag, int *cerr_ptr) {
8794 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
8796 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8797 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8798 // according to the implementation
8800 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8801 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
8802 if (dst && ret >= 0)
8803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8808 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
8809 const char *extra) {
8811 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
8813 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8814 char *end = REAL(svis)(dst, c, flag, nextc, extra);
8816 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8819 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
8820 const char *extra) {
8822 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
8824 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8825 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
8827 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
8828 Min((SIZE_T)(end - dst + 1), dlen));
8831 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
8832 const char *extra) {
8834 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
8836 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8838 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8839 int len = REAL(strsvis)(dst, src, flag, extra);
8841 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8844 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
8845 const char *extra) {
8847 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
8849 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8851 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8852 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
8853 // The interface will be valid even if there is no space for NULL char
8854 if (dst && len >= 0)
8855 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8858 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
8859 const char *extra) {
8861 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
8863 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8865 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8866 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
8868 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8871 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8872 int flag, const char *extra) {
8874 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
8876 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8878 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8879 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
8880 if (dst && ret >= 0)
8881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8884 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
8885 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
8887 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
8890 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8892 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8893 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8894 // according to the implementation
8896 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8897 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
8898 if (dst && ret >= 0)
8899 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8904 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
8906 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
8908 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
8909 int ret = REAL(unvis)(cp, c, astate, flag);
8910 if (ret == unvis_valid || ret == unvis_validpush) {
8911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
8915 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
8917 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
8919 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8920 int ret = REAL(strunvis)(dst, src);
8922 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8925 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
8927 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
8929 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8930 int ret = REAL(strnunvis)(dst, dlen, src);
8932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8935 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
8937 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
8939 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8940 int ret = REAL(strunvisx)(dst, src, flag);
8942 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8945 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
8948 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
8950 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8951 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
8953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8957 COMMON_INTERCEPT_FUNCTION(vis); \
8958 COMMON_INTERCEPT_FUNCTION(nvis); \
8959 COMMON_INTERCEPT_FUNCTION(strvis); \
8960 COMMON_INTERCEPT_FUNCTION(stravis); \
8961 COMMON_INTERCEPT_FUNCTION(strnvis); \
8962 COMMON_INTERCEPT_FUNCTION(strvisx); \
8963 COMMON_INTERCEPT_FUNCTION(strnvisx); \
8964 COMMON_INTERCEPT_FUNCTION(strenvisx); \
8965 COMMON_INTERCEPT_FUNCTION(svis); \
8966 COMMON_INTERCEPT_FUNCTION(snvis); \
8967 COMMON_INTERCEPT_FUNCTION(strsvis); \
8968 COMMON_INTERCEPT_FUNCTION(strsnvis); \
8969 COMMON_INTERCEPT_FUNCTION(strsvisx); \
8970 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
8971 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
8972 COMMON_INTERCEPT_FUNCTION(unvis); \
8973 COMMON_INTERCEPT_FUNCTION(strunvis); \
8974 COMMON_INTERCEPT_FUNCTION(strnunvis); \
8975 COMMON_INTERCEPT_FUNCTION(strunvisx); \
8976 COMMON_INTERCEPT_FUNCTION(strnunvisx)
8981 #if SANITIZER_INTERCEPT_CDB
8982 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
8984 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
8986 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
8987 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
8989 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8993 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
8994 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
8996 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
8999 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
9000 struct __sanitizer_cdbr *cdbr =
9001 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
9003 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9007 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9009 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9011 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9012 return REAL(cdbr_entries)(cdbr);
9015 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9016 const void **data, SIZE_T *datalen) {
9018 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9020 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9021 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9024 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9026 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9027 if (data && datalen)
9028 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9033 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9034 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9036 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9038 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9040 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9041 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9044 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9046 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9047 if (data && datalen)
9048 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9053 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9055 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9057 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9058 REAL(cdbr_close)(cdbr);
9061 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9063 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9064 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9066 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9070 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9071 SIZE_T keylen, const void *data, SIZE_T datalen) {
9073 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9075 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9076 if (data && datalen)
9077 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9079 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9080 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9086 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9087 SIZE_T datalen, u32 *index) {
9089 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9091 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9092 if (data && datalen)
9093 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9094 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9099 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9104 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9105 SIZE_T keylen, u32 index) {
9107 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9109 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9111 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9112 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9114 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9118 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9119 const char descr[16], u32 (*seedgen)(void)) {
9121 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9122 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9124 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9126 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9128 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9129 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9132 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9134 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9139 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9141 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9143 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9144 REAL(cdbw_close)(cdbw);
9148 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9149 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9150 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9151 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9152 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9153 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9154 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9155 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9156 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9157 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9158 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9159 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9164 #if SANITIZER_INTERCEPT_GETFSENT
9165 INTERCEPTOR(void *, getfsent) {
9167 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9168 void *ret = REAL(getfsent)();
9170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9174 INTERCEPTOR(void *, getfsspec, const char *spec) {
9176 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9178 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, REAL(strlen)(spec) + 1);
9179 void *ret = REAL(getfsspec)(spec);
9181 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9185 INTERCEPTOR(void *, getfsfile, const char *file) {
9187 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9189 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
9190 void *ret = REAL(getfsfile)(file);
9192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9196 #define INIT_GETFSENT \
9197 COMMON_INTERCEPT_FUNCTION(getfsent); \
9198 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9199 COMMON_INTERCEPT_FUNCTION(getfsfile);
9201 #define INIT_GETFSENT
9204 #if SANITIZER_INTERCEPT_ARC4RANDOM
9205 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9207 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9208 REAL(arc4random_buf)(buf, len);
9210 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9213 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9215 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9217 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9218 REAL(arc4random_addrandom)(dat, datlen);
9221 #define INIT_ARC4RANDOM \
9222 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9223 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9225 #define INIT_ARC4RANDOM
9228 #if SANITIZER_INTERCEPT_POPEN
9229 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9231 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9233 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, REAL(strlen)(command) + 1);
9235 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9236 __sanitizer_FILE *res = REAL(popen)(command, type);
9237 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9238 if (res) unpoison_file(res);
9241 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9246 #if SANITIZER_INTERCEPT_POPENVE
9247 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9248 char *const *argv, char *const *envp, const char *type) {
9250 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9252 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
9254 for (char *const *pa = argv; ; ++pa) {
9255 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9258 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9262 for (char *const *pa = envp; ; ++pa) {
9263 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9266 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9270 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9271 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9272 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9273 if (res) unpoison_file(res);
9276 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9278 #define INIT_POPENVE
9281 #if SANITIZER_INTERCEPT_PCLOSE
9282 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9284 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9285 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9286 const FileMetadata *m = GetInterceptorMetadata(fp);
9287 int res = REAL(pclose)(fp);
9289 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9290 DeleteInterceptorMetadata(fp);
9294 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9299 #if SANITIZER_INTERCEPT_FUNOPEN
9300 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9301 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9302 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9303 typedef int (*funopen_closefn)(void *cookie);
9305 struct WrappedFunopenCookie {
9307 funopen_readfn real_read;
9308 funopen_writefn real_write;
9309 funopen_seekfn real_seek;
9310 funopen_closefn real_close;
9313 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9314 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9315 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9316 funopen_readfn real_read = wrapped_cookie->real_read;
9317 return real_read(wrapped_cookie->real_cookie, buf, len);
9320 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9321 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9322 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9323 funopen_writefn real_write = wrapped_cookie->real_write;
9324 return real_write(wrapped_cookie->real_cookie, buf, len);
9327 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9328 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9329 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9330 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9331 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9334 static int wrapped_funopen_close(void *cookie) {
9335 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9336 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9337 funopen_closefn real_close = wrapped_cookie->real_close;
9338 int res = real_close(wrapped_cookie->real_cookie);
9339 InternalFree(wrapped_cookie);
9343 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9344 funopen_writefn writefn, funopen_seekfn seekfn,
9345 funopen_closefn closefn) {
9347 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9350 WrappedFunopenCookie *wrapped_cookie =
9351 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9352 wrapped_cookie->real_cookie = cookie;
9353 wrapped_cookie->real_read = readfn;
9354 wrapped_cookie->real_write = writefn;
9355 wrapped_cookie->real_seek = seekfn;
9356 wrapped_cookie->real_close = closefn;
9358 __sanitizer_FILE *res =
9359 REAL(funopen)(wrapped_cookie,
9360 readfn ? wrapped_funopen_read : nullptr,
9361 writefn ? wrapped_funopen_write : nullptr,
9362 seekfn ? wrapped_funopen_seek : nullptr,
9363 closefn ? wrapped_funopen_close : nullptr);
9368 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9370 #define INIT_FUNOPEN
9373 #if SANITIZER_INTERCEPT_FUNOPEN2
9374 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9375 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9376 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9377 typedef int (*funopen2_flushfn)(void *cookie);
9378 typedef int (*funopen2_closefn)(void *cookie);
9380 struct WrappedFunopen2Cookie {
9382 funopen2_readfn real_read;
9383 funopen2_writefn real_write;
9384 funopen2_seekfn real_seek;
9385 funopen2_flushfn real_flush;
9386 funopen2_closefn real_close;
9389 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9390 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9391 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9392 funopen2_readfn real_read = wrapped_cookie->real_read;
9393 return real_read(wrapped_cookie->real_cookie, buf, len);
9396 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9398 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9399 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9400 funopen2_writefn real_write = wrapped_cookie->real_write;
9401 return real_write(wrapped_cookie->real_cookie, buf, len);
9404 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9405 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9406 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9407 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9408 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9411 static int wrapped_funopen2_flush(void *cookie) {
9412 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9413 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9414 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9415 return real_flush(wrapped_cookie->real_cookie);
9418 static int wrapped_funopen2_close(void *cookie) {
9419 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9420 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9421 funopen2_closefn real_close = wrapped_cookie->real_close;
9422 int res = real_close(wrapped_cookie->real_cookie);
9423 InternalFree(wrapped_cookie);
9427 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9428 funopen2_writefn writefn, funopen2_seekfn seekfn,
9429 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9431 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9434 WrappedFunopen2Cookie *wrapped_cookie =
9435 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9436 wrapped_cookie->real_cookie = cookie;
9437 wrapped_cookie->real_read = readfn;
9438 wrapped_cookie->real_write = writefn;
9439 wrapped_cookie->real_seek = seekfn;
9440 wrapped_cookie->real_flush = flushfn;
9441 wrapped_cookie->real_close = closefn;
9443 __sanitizer_FILE *res =
9444 REAL(funopen2)(wrapped_cookie,
9445 readfn ? wrapped_funopen2_read : nullptr,
9446 writefn ? wrapped_funopen2_write : nullptr,
9447 seekfn ? wrapped_funopen2_seek : nullptr,
9448 flushfn ? wrapped_funopen2_flush : nullptr,
9449 closefn ? wrapped_funopen2_close : nullptr);
9454 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9456 #define INIT_FUNOPEN2
9459 #if SANITIZER_INTERCEPT_FDEVNAME
9460 INTERCEPTOR(char *, fdevname, int fd) {
9462 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9463 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9464 char *name = REAL(fdevname)(fd);
9466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
9468 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9473 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9475 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9476 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9477 char *name = REAL(fdevname_r)(fd, buf, len);
9478 if (name && buf && len > 0) {
9479 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
9481 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9486 #define INIT_FDEVNAME \
9487 COMMON_INTERCEPT_FUNCTION(fdevname); \
9488 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9490 #define INIT_FDEVNAME
9493 #if SANITIZER_INTERCEPT_GETUSERSHELL
9494 INTERCEPTOR(char *, getusershell) {
9496 COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9497 char *res = REAL(getusershell)();
9499 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9503 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9505 #define INIT_GETUSERSHELL
9508 #if SANITIZER_INTERCEPT_SL_INIT
9509 INTERCEPTOR(void *, sl_init) {
9511 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9512 void *res = REAL(sl_init)();
9514 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9518 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9520 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9522 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9524 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9525 int res = REAL(sl_add)(sl, item);
9527 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9531 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9533 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9535 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9537 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9538 char *res = REAL(sl_find)(sl, item);
9540 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9544 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9546 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9548 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9549 REAL(sl_free)(sl, freeall);
9552 #define INIT_SL_INIT \
9553 COMMON_INTERCEPT_FUNCTION(sl_init); \
9554 COMMON_INTERCEPT_FUNCTION(sl_add); \
9555 COMMON_INTERCEPT_FUNCTION(sl_find); \
9556 COMMON_INTERCEPT_FUNCTION(sl_free);
9558 #define INIT_SL_INIT
9561 #if SANITIZER_INTERCEPT_GETRANDOM
9562 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
9564 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
9565 SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
9567 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
9571 #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
9573 #define INIT_GETRANDOM
9576 #if SANITIZER_INTERCEPT_CRYPT
9577 INTERCEPTOR(char *, crypt, char *key, char *salt) {
9579 COMMON_INTERCEPTOR_ENTER(ctx, crypt, key, salt);
9580 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, internal_strlen(key) + 1);
9581 COMMON_INTERCEPTOR_READ_RANGE(ctx, salt, internal_strlen(salt) + 1);
9582 char *res = REAL(crypt)(key, salt);
9584 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
9587 #define INIT_CRYPT COMMON_INTERCEPT_FUNCTION(crypt);
9592 #if SANITIZER_INTERCEPT_CRYPT_R
9593 INTERCEPTOR(char *, crypt_r, char *key, char *salt, void *data) {
9595 COMMON_INTERCEPTOR_ENTER(ctx, crypt_r, key, salt, data);
9596 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, internal_strlen(key) + 1);
9597 COMMON_INTERCEPTOR_READ_RANGE(ctx, salt, internal_strlen(salt) + 1);
9598 char *res = REAL(crypt_r)(key, salt, data);
9599 if (res != nullptr) {
9600 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data,
9601 __sanitizer::struct_crypt_data_sz);
9602 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
9606 #define INIT_CRYPT_R COMMON_INTERCEPT_FUNCTION(crypt_r);
9608 #define INIT_CRYPT_R
9611 #if SANITIZER_INTERCEPT_GETENTROPY
9612 INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
9614 COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
9615 int r = REAL(getentropy)(buf, buflen);
9617 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
9621 #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
9623 #define INIT_GETENTROPY
9626 static void InitializeCommonInterceptors() {
9628 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
9629 interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
9679 INIT_LOCALTIME_AND_FRIENDS;
9688 INIT_GETPWNAM_AND_FRIENDS;
9689 INIT_GETPWNAM_R_AND_FRIENDS;
9704 INIT_PTHREAD_GETSCHEDPARAM;
9709 INIT_GETHOSTBYNAME2;
9710 INIT_GETHOSTBYNAME_R;
9711 INIT_GETHOSTBYNAME2_R;
9712 INIT_GETHOSTBYADDR_R;
9732 INIT_GET_CURRENT_DIR_NAME;
9742 INIT_CANONICALIZE_FILE_NAME;
9744 INIT_SCHED_GETAFFINITY;
9745 INIT_SCHED_GETPARAM;
9748 INIT_XPG_STRERROR_R;
9761 INIT_PTHREAD_SIGMASK;
9764 INIT_PTHREAD_MUTEX_LOCK;
9765 INIT_PTHREAD_MUTEX_UNLOCK;
9766 INIT___PTHREAD_MUTEX_LOCK;
9767 INIT___PTHREAD_MUTEX_UNLOCK;
9768 INIT___LIBC_MUTEX_LOCK;
9769 INIT___LIBC_MUTEX_UNLOCK;
9770 INIT___LIBC_THR_SETCANCELSTATE;
9778 INIT_ETHER_NTOA_ATON;
9783 INIT_PTHREAD_ATTR_GET;
9784 INIT_PTHREAD_ATTR_GET_SCHED;
9785 INIT_PTHREAD_ATTR_GETINHERITSCHED;
9786 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
9787 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
9788 INIT_PTHREAD_MUTEXATTR_GETTYPE;
9789 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
9790 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
9791 INIT_PTHREAD_MUTEXATTR_GETROBUST;
9792 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
9793 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
9794 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
9795 INIT_PTHREAD_CONDATTR_GETPSHARED;
9796 INIT_PTHREAD_CONDATTR_GETCLOCK;
9797 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
9803 INIT_PTHREAD_SETNAME_NP;
9804 INIT_PTHREAD_GETNAME_NP;
9822 INIT_IF_INDEXTONAME;
9830 INIT_LIBIO_INTERNALS;
9833 INIT_OPEN_MEMSTREAM;
9837 INIT_DLOPEN_DLCLOSE;
9843 INIT_PTHREAD_SETCANCEL;
9845 INIT_PROCESS_VM_READV;
9851 INIT_EVENTFD_READ_WRITE;
9857 // FIXME: add other *stat interceptors.
9869 INIT_GROUP_FROM_GID;
9870 INIT_GID_FROM_GROUP;
9874 INIT_GETGROUPMEMBERSHIP;
9877 INIT_NAME_TO_HANDLE_AT;
9878 INIT_OPEN_BY_HANDLE_AT;
9888 INIT_MI_VECTOR_HASH;
9896 INIT_SYSCTLGETMIBINFO;