1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common function interceptors for tools like AddressSanitizer,
11 // ThreadSanitizer, MemorySanitizer, etc.
13 // This file should be included into the tool's interceptor file,
14 // which has to define its own macros:
15 // COMMON_INTERCEPTOR_ENTER
16 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
17 // COMMON_INTERCEPTOR_READ_RANGE
18 // COMMON_INTERCEPTOR_WRITE_RANGE
19 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
20 // COMMON_INTERCEPTOR_DIR_ACQUIRE
21 // COMMON_INTERCEPTOR_FD_ACQUIRE
22 // COMMON_INTERCEPTOR_FD_RELEASE
23 // COMMON_INTERCEPTOR_FD_ACCESS
24 // COMMON_INTERCEPTOR_SET_THREAD_NAME
25 // COMMON_INTERCEPTOR_ON_DLOPEN
26 // COMMON_INTERCEPTOR_ON_EXIT
27 // COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
28 // COMMON_INTERCEPTOR_MUTEX_POST_LOCK
29 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
30 // COMMON_INTERCEPTOR_MUTEX_REPAIR
31 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
32 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
33 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
34 // COMMON_INTERCEPTOR_MEMSET_IMPL
35 // COMMON_INTERCEPTOR_MEMMOVE_IMPL
36 // COMMON_INTERCEPTOR_MEMCPY_IMPL
37 // COMMON_INTERCEPTOR_MMAP_IMPL
38 // COMMON_INTERCEPTOR_COPY_STRING
39 // COMMON_INTERCEPTOR_STRNDUP_IMPL
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 getitimer __getitimer50
81 #define getpwent __getpwent50
82 #define getpwnam __getpwnam50
83 #define getpwnam_r __getpwnam_r50
84 #define getpwuid __getpwuid50
85 #define getpwuid_r __getpwuid_r50
86 #define getutent __getutent50
87 #define getutxent __getutxent50
88 #define getutxid __getutxid50
89 #define getutxline __getutxline50
91 #define gmtime __gmtime50
92 #define gmtime_r __gmtime_r50
93 #define localtime __locatime50
94 #define localtime_r __localtime_r50
95 #define mktime __mktime50
96 #define lstat __lstat50
97 #define opendir __opendir30
98 #define readdir __readdir30
99 #define readdir_r __readdir_r30
100 #define scandir __scandir30
101 #define setitimer __setitimer50
102 #define setlocale __setlocale50
103 #define shmctl __shmctl50
104 #define sigemptyset __sigemptyset14
105 #define sigfillset __sigfillset14
106 #define sigpending __sigpending14
107 #define sigprocmask __sigprocmask14
108 #define sigtimedwait __sigtimedwait50
109 #define stat __stat50
110 #define time __time50
111 #define times __times13
112 #define wait3 __wait350
113 #define wait4 __wait450
114 extern const unsigned short *_ctype_tab_;
115 extern const short *_toupper_tab_;
116 extern const short *_tolower_tab_;
119 // Platform-specific options.
121 namespace __sanitizer {
122 bool PlatformHasDifferentMemcpyAndMemmove();
124 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
125 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
126 #elif SANITIZER_WINDOWS64
127 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
129 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
130 #endif // SANITIZER_MAC
132 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
133 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
136 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
137 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
140 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
141 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
144 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
145 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
148 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
149 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
152 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
153 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
156 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
157 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
160 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
161 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
164 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
165 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
168 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
169 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
172 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
173 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
176 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
177 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
180 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
181 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
184 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
185 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
186 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
189 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
190 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
193 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
194 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
195 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
197 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
198 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
199 CheckNoDeepBind(filename, flag);
202 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
203 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
206 #ifndef COMMON_INTERCEPTOR_ACQUIRE
207 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
210 #ifndef COMMON_INTERCEPTOR_RELEASE
211 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
214 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
215 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
218 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
219 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
222 #ifdef SANITIZER_NLDBL_VERSION
223 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
224 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
226 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
227 COMMON_INTERCEPT_FUNCTION(fn)
230 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
231 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
233 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
234 return internal_memset(dst, v, size); \
235 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
236 if (common_flags()->intercept_intrin) \
237 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
238 return REAL(memset)(dst, v, size); \
242 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
243 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
245 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
246 return internal_memmove(dst, src, size); \
247 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
248 if (common_flags()->intercept_intrin) { \
249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
250 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
252 return REAL(memmove)(dst, src, size); \
256 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
257 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
259 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
260 return internal_memmove(dst, src, size); \
262 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
263 if (common_flags()->intercept_intrin) { \
264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
265 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
267 return REAL(memcpy)(dst, src, size); \
271 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
272 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
274 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
277 #ifndef COMMON_INTERCEPTOR_COPY_STRING
278 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
281 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
282 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
283 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
284 uptr copy_length = internal_strnlen(s, size); \
285 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
286 if (common_flags()->intercept_strndup) { \
287 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
289 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
290 internal_memcpy(new_mem, s, copy_length); \
291 new_mem[copy_length] = '\0'; \
295 struct FileMetadata {
296 // For open_memstream().
301 struct CommonInterceptorMetadata {
311 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
313 static MetadataHashMap *interceptor_metadata_map;
316 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
317 const FileMetadata &file) {
318 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
320 h->type = CommonInterceptorMetadata::CIMT_FILE;
324 UNUSED static const FileMetadata *GetInterceptorMetadata(
325 __sanitizer_FILE *addr) {
326 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
329 if (addr && h.exists()) {
331 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
338 UNUSED static void DeleteInterceptorMetadata(void *addr) {
339 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
344 #if SANITIZER_INTERCEPT_STRLEN
345 INTERCEPTOR(SIZE_T, strlen, const char *s) {
346 // Sometimes strlen is called prior to InitializeCommonInterceptors,
347 // in which case the REAL(strlen) typically used in
348 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
350 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
351 return internal_strlen(s);
353 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
354 SIZE_T result = REAL(strlen)(s);
355 if (common_flags()->intercept_strlen)
356 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
359 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
364 #if SANITIZER_INTERCEPT_STRNLEN
365 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
367 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
368 SIZE_T length = REAL(strnlen)(s, maxlen);
369 if (common_flags()->intercept_strlen)
370 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
373 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
378 #if SANITIZER_INTERCEPT_STRNDUP
379 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
381 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
383 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
386 #endif // SANITIZER_INTERCEPT_STRNDUP
388 #if SANITIZER_INTERCEPT___STRNDUP
389 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
391 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
393 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
395 #define INIT___STRNDUP
396 #endif // SANITIZER_INTERCEPT___STRNDUP
398 #if SANITIZER_INTERCEPT_TEXTDOMAIN
399 INTERCEPTOR(char*, textdomain, const char *domainname) {
401 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
402 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
403 char *domain = REAL(textdomain)(domainname);
405 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
409 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
411 #define INIT_TEXTDOMAIN
414 #if SANITIZER_INTERCEPT_STRCMP
415 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
416 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
419 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
420 const char *s1, const char *s2, int result)
422 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
424 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
425 unsigned char c1, c2;
428 c1 = (unsigned char)s1[i];
429 c2 = (unsigned char)s2[i];
430 if (c1 != c2 || c1 == '\0') break;
432 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
433 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
434 int result = CharCmpX(c1, c2);
435 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
440 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
441 const char *s1, const char *s2, uptr n,
444 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
445 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
446 return internal_strncmp(s1, s2, size);
448 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
449 unsigned char c1 = 0, c2 = 0;
451 for (i = 0; i < size; i++) {
452 c1 = (unsigned char)s1[i];
453 c2 = (unsigned char)s2[i];
454 if (c1 != c2 || c1 == '\0') break;
458 if (common_flags()->strict_string_checks) {
459 for (; i1 < size && s1[i1]; i1++) {}
460 for (; i2 < size && s2[i2]; i2++) {}
462 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
463 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
464 int result = CharCmpX(c1, c2);
465 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
470 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
471 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
477 #if SANITIZER_INTERCEPT_STRCASECMP
478 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
479 int c1_low = ToLower(c1);
480 int c2_low = ToLower(c2);
481 return c1_low - c2_low;
484 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
485 const char *s1, const char *s2, int result)
487 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
489 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
490 unsigned char c1 = 0, c2 = 0;
493 c1 = (unsigned char)s1[i];
494 c2 = (unsigned char)s2[i];
495 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
497 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
498 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
499 int result = CharCaseCmp(c1, c2);
500 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
505 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
506 const char *s1, const char *s2, uptr size,
509 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
511 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
512 unsigned char c1 = 0, c2 = 0;
514 for (i = 0; i < size; i++) {
515 c1 = (unsigned char)s1[i];
516 c2 = (unsigned char)s2[i];
517 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
521 if (common_flags()->strict_string_checks) {
522 for (; i1 < size && s1[i1]; i1++) {}
523 for (; i2 < size && s2[i2]; i2++) {}
525 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
526 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
527 int result = CharCaseCmp(c1, c2);
528 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
529 s1, s2, size, result);
533 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
534 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
536 #define INIT_STRCASECMP
537 #define INIT_STRNCASECMP
540 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
541 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
543 uptr len1 = REAL(strlen)(s1);
544 uptr len2 = REAL(strlen)(s2);
545 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
546 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
550 #if SANITIZER_INTERCEPT_STRSTR
552 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
553 const char *s1, const char *s2, char *result)
555 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
556 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
557 return internal_strstr(s1, s2);
559 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
560 char *r = REAL(strstr)(s1, s2);
561 if (common_flags()->intercept_strstr)
562 StrstrCheck(ctx, r, s1, s2);
563 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
568 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
573 #if SANITIZER_INTERCEPT_STRCASESTR
575 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
576 const char *s1, const char *s2, char *result)
578 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
580 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
581 char *r = REAL(strcasestr)(s1, s2);
582 if (common_flags()->intercept_strstr)
583 StrstrCheck(ctx, r, s1, s2);
584 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
589 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
591 #define INIT_STRCASESTR
594 #if SANITIZER_INTERCEPT_STRTOK
596 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
598 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
599 if (!common_flags()->intercept_strtok) {
600 return REAL(strtok)(str, delimiters);
602 if (common_flags()->strict_string_checks) {
603 // If strict_string_checks is enabled, we check the whole first argument
604 // string on the first call (strtok saves this string in a static buffer
605 // for subsequent calls). We do not need to check strtok's result.
606 // As the delimiters can change, we check them every call.
607 if (str != nullptr) {
608 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
610 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
611 REAL(strlen)(delimiters) + 1);
612 return REAL(strtok)(str, delimiters);
614 // However, when strict_string_checks is disabled we cannot check the
615 // whole string on the first call. Instead, we check the result string
616 // which is guaranteed to be a NULL-terminated substring of the first
617 // argument. We also conservatively check one character of str and the
619 if (str != nullptr) {
620 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
622 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
623 char *result = REAL(strtok)(str, delimiters);
624 if (result != nullptr) {
625 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
626 } else if (str != nullptr) {
627 // No delimiter were found, it's safe to assume that the entire str was
629 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
635 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
640 #if SANITIZER_INTERCEPT_MEMMEM
641 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
642 const void *s1, SIZE_T len1, const void *s2,
643 SIZE_T len2, void *result)
645 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
648 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
649 void *r = REAL(memmem)(s1, len1, s2, len2);
650 if (common_flags()->intercept_memmem) {
651 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
652 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
654 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
655 s1, len1, s2, len2, r);
659 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
662 #endif // SANITIZER_INTERCEPT_MEMMEM
664 #if SANITIZER_INTERCEPT_STRCHR
665 INTERCEPTOR(char*, strchr, const char *s, int c) {
667 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
668 return internal_strchr(s, c);
669 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
670 char *result = REAL(strchr)(s, c);
671 if (common_flags()->intercept_strchr) {
672 // Keep strlen as macro argument, as macro may ignore it.
673 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
674 (result ? result - s : REAL(strlen)(s)) + 1);
678 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
683 #if SANITIZER_INTERCEPT_STRCHRNUL
684 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
686 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
687 char *result = REAL(strchrnul)(s, c);
688 uptr len = result - s + 1;
689 if (common_flags()->intercept_strchr)
690 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
693 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
695 #define INIT_STRCHRNUL
698 #if SANITIZER_INTERCEPT_STRRCHR
699 INTERCEPTOR(char*, strrchr, const char *s, int c) {
701 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
702 return internal_strrchr(s, c);
703 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
704 if (common_flags()->intercept_strchr)
705 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
706 return REAL(strrchr)(s, c);
708 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
713 #if SANITIZER_INTERCEPT_STRSPN
714 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
716 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
717 SIZE_T r = REAL(strspn)(s1, s2);
718 if (common_flags()->intercept_strspn) {
719 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
720 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
725 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
727 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
728 SIZE_T r = REAL(strcspn)(s1, s2);
729 if (common_flags()->intercept_strspn) {
730 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
731 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
736 #define INIT_STRSPN \
737 COMMON_INTERCEPT_FUNCTION(strspn); \
738 COMMON_INTERCEPT_FUNCTION(strcspn);
743 #if SANITIZER_INTERCEPT_STRPBRK
744 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
746 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
747 char *r = REAL(strpbrk)(s1, s2);
748 if (common_flags()->intercept_strpbrk) {
749 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
750 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
751 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
756 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
761 #if SANITIZER_INTERCEPT_MEMSET
762 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
764 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
767 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
772 #if SANITIZER_INTERCEPT_MEMMOVE
773 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
775 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
778 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
783 #if SANITIZER_INTERCEPT_MEMCPY
784 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
785 // On OS X, calling internal_memcpy here will cause memory corruptions,
786 // because memcpy and memmove are actually aliases of the same
787 // implementation. We need to use internal_memmove here.
788 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
789 // due to memcpy being an alias of memmove on OS X.
791 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
792 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
794 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
798 #define INIT_MEMCPY \
800 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
801 COMMON_INTERCEPT_FUNCTION(memcpy); \
803 ASSIGN_REAL(memcpy, memmove); \
805 CHECK(REAL(memcpy)); \
812 #if SANITIZER_INTERCEPT_MEMCMP
814 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
815 const void *s1, const void *s2, uptr n,
818 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
819 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
820 return internal_memcmp(a1, a2, size);
822 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
823 if (common_flags()->intercept_memcmp) {
824 if (common_flags()->strict_memcmp) {
825 // Check the entire regions even if the first bytes of the buffers are
827 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
828 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
829 // Fallthrough to REAL(memcmp) below.
831 unsigned char c1 = 0, c2 = 0;
832 const unsigned char *s1 = (const unsigned char*)a1;
833 const unsigned char *s2 = (const unsigned char*)a2;
835 for (i = 0; i < size; i++) {
840 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
841 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
842 int r = CharCmpX(c1, c2);
843 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
848 int result = REAL(memcmp(a1, a2, size));
849 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
854 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
859 #if SANITIZER_INTERCEPT_MEMCHR
860 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
861 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
862 return internal_memchr(s, c, n);
864 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
865 #if SANITIZER_WINDOWS
868 res = REAL(memchr)(s, c, n);
870 res = internal_memchr(s, c, n);
873 void *res = REAL(memchr)(s, c, n);
875 uptr len = res ? (char *)res - (const char *)s + 1 : n;
876 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
880 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
885 #if SANITIZER_INTERCEPT_MEMRCHR
886 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
888 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
889 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
890 return REAL(memrchr)(s, c, n);
893 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
898 #if SANITIZER_INTERCEPT_FREXP
899 INTERCEPTOR(double, frexp, double x, int *exp) {
901 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
902 // Assuming frexp() always writes to |exp|.
903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
904 double res = REAL(frexp)(x, exp);
908 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
911 #endif // SANITIZER_INTERCEPT_FREXP
913 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
914 INTERCEPTOR(float, frexpf, float x, int *exp) {
916 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
917 // FIXME: under ASan the call below may write to freed memory and corrupt
919 // https://github.com/google/sanitizers/issues/321.
920 float res = REAL(frexpf)(x, exp);
921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
925 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
927 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
928 // FIXME: under ASan the call below may write to freed memory and corrupt
930 // https://github.com/google/sanitizers/issues/321.
931 long double res = REAL(frexpl)(x, exp);
932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
936 #define INIT_FREXPF_FREXPL \
937 COMMON_INTERCEPT_FUNCTION(frexpf); \
938 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
940 #define INIT_FREXPF_FREXPL
941 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
944 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
945 SIZE_T iovlen, SIZE_T maxlen) {
946 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
947 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
953 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
954 SIZE_T iovlen, SIZE_T maxlen) {
955 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
956 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
957 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
958 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
964 #if SANITIZER_INTERCEPT_READ
965 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
967 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
968 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
969 // FIXME: under ASan the call below may write to freed memory and corrupt
971 // https://github.com/google/sanitizers/issues/321.
972 SSIZE_T res = REAL(read)(fd, ptr, count);
973 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
974 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
977 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
982 #if SANITIZER_INTERCEPT_FREAD
983 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
984 // libc file streams can call user-supplied functions, see fopencookie.
986 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
987 // FIXME: under ASan the call below may write to freed memory and corrupt
989 // https://github.com/google/sanitizers/issues/321.
990 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
991 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
994 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
999 #if SANITIZER_INTERCEPT_PREAD
1000 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1002 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1003 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1004 // FIXME: under ASan the call below may write to freed memory and corrupt
1005 // its metadata. See
1006 // https://github.com/google/sanitizers/issues/321.
1007 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1008 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1009 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1012 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1017 #if SANITIZER_INTERCEPT_PREAD64
1018 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1020 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1021 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1022 // FIXME: under ASan the call below may write to freed memory and corrupt
1023 // its metadata. See
1024 // https://github.com/google/sanitizers/issues/321.
1025 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1026 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1027 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1030 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1032 #define INIT_PREAD64
1035 #if SANITIZER_INTERCEPT_READV
1036 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1039 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1040 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1041 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1042 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1043 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1046 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1051 #if SANITIZER_INTERCEPT_PREADV
1052 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1055 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1056 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1057 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1058 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1059 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1062 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1067 #if SANITIZER_INTERCEPT_PREADV64
1068 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1071 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1072 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1073 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1074 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1075 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1078 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1080 #define INIT_PREADV64
1083 #if SANITIZER_INTERCEPT_WRITE
1084 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1086 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1087 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1088 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1089 SSIZE_T res = REAL(write)(fd, ptr, count);
1090 // FIXME: this check should be _before_ the call to REAL(write), not after
1091 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1094 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1099 #if SANITIZER_INTERCEPT_FWRITE
1100 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1101 // libc file streams can call user-supplied functions, see fopencookie.
1103 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1104 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1105 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1108 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1113 #if SANITIZER_INTERCEPT_PWRITE
1114 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1116 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1117 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1118 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1119 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1120 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1123 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1128 #if SANITIZER_INTERCEPT_PWRITE64
1129 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1132 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1133 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1134 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1135 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1136 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1139 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1141 #define INIT_PWRITE64
1144 #if SANITIZER_INTERCEPT_WRITEV
1145 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1148 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1149 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1150 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1151 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1152 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1155 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1160 #if SANITIZER_INTERCEPT_PWRITEV
1161 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1164 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1165 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1166 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1167 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1168 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1171 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1173 #define INIT_PWRITEV
1176 #if SANITIZER_INTERCEPT_PWRITEV64
1177 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1180 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1181 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1182 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1183 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1184 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1187 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1189 #define INIT_PWRITEV64
1192 #if SANITIZER_INTERCEPT_FGETS
1193 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1194 // libc file streams can call user-supplied functions, see fopencookie.
1196 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1197 // FIXME: under ASan the call below may write to freed memory and corrupt
1198 // its metadata. See
1199 // https://github.com/google/sanitizers/issues/321.
1200 char *res = REAL(fgets)(s, size, file);
1202 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
1205 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1210 #if SANITIZER_INTERCEPT_FPUTS
1211 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1212 // libc file streams can call user-supplied functions, see fopencookie.
1214 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1215 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1216 return REAL(fputs)(s, file);
1218 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1223 #if SANITIZER_INTERCEPT_PUTS
1224 INTERCEPTOR(int, puts, char *s) {
1225 // libc file streams can call user-supplied functions, see fopencookie.
1227 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1228 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1229 return REAL(puts)(s);
1231 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1236 #if SANITIZER_INTERCEPT_PRCTL
1237 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1238 unsigned long arg3, // NOLINT
1239 unsigned long arg4, unsigned long arg5) { // NOLINT
1241 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1242 static const int PR_SET_NAME = 15;
1243 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1244 if (option == PR_SET_NAME) {
1246 internal_strncpy(buff, (char *)arg2, 15);
1248 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1252 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1255 #endif // SANITIZER_INTERCEPT_PRCTL
1257 #if SANITIZER_INTERCEPT_TIME
1258 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1260 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1261 unsigned long local_t;
1262 unsigned long res = REAL(time)(&local_t);
1263 if (t && res != (unsigned long)-1) {
1264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1269 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1272 #endif // SANITIZER_INTERCEPT_TIME
1274 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1275 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1276 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1277 #if !SANITIZER_SOLARIS
1279 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1280 // can point to shared memory and tsan would report a data race.
1281 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1282 REAL(strlen(tm->tm_zone)) + 1);
1286 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1288 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1289 __sanitizer_tm *res = REAL(localtime)(timep);
1291 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1292 unpoison_tm(ctx, res);
1296 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1298 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1299 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1301 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1302 unpoison_tm(ctx, res);
1306 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1308 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1309 __sanitizer_tm *res = REAL(gmtime)(timep);
1311 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1312 unpoison_tm(ctx, res);
1316 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1318 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1319 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1321 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1322 unpoison_tm(ctx, res);
1326 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1328 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1329 // FIXME: under ASan the call below may write to freed memory and corrupt
1330 // its metadata. See
1331 // https://github.com/google/sanitizers/issues/321.
1332 char *res = REAL(ctime)(timep);
1334 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1339 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1341 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1342 // FIXME: under ASan the call below may write to freed memory and corrupt
1343 // its metadata. See
1344 // https://github.com/google/sanitizers/issues/321.
1345 char *res = REAL(ctime_r)(timep, result);
1347 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1352 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1354 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1355 // FIXME: under ASan the call below may write to freed memory and corrupt
1356 // its metadata. See
1357 // https://github.com/google/sanitizers/issues/321.
1358 char *res = REAL(asctime)(tm);
1360 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1361 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1365 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1367 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1368 // FIXME: under ASan the call below may write to freed memory and corrupt
1369 // its metadata. See
1370 // https://github.com/google/sanitizers/issues/321.
1371 char *res = REAL(asctime_r)(tm, result);
1373 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1378 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1380 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1381 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1382 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1383 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1384 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1385 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1386 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1387 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1388 long res = REAL(mktime)(tm);
1389 if (res != -1) unpoison_tm(ctx, tm);
1392 #define INIT_LOCALTIME_AND_FRIENDS \
1393 COMMON_INTERCEPT_FUNCTION(localtime); \
1394 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1395 COMMON_INTERCEPT_FUNCTION(gmtime); \
1396 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1397 COMMON_INTERCEPT_FUNCTION(ctime); \
1398 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1399 COMMON_INTERCEPT_FUNCTION(asctime); \
1400 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1401 COMMON_INTERCEPT_FUNCTION(mktime);
1403 #define INIT_LOCALTIME_AND_FRIENDS
1404 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1406 #if SANITIZER_INTERCEPT_STRPTIME
1407 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1409 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1411 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1412 // FIXME: under ASan the call below may write to freed memory and corrupt
1413 // its metadata. See
1414 // https://github.com/google/sanitizers/issues/321.
1415 char *res = REAL(strptime)(s, format, tm);
1416 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1418 // Do not call unpoison_tm here, because strptime does not, in fact,
1419 // initialize the entire struct tm. For example, tm_zone pointer is left
1421 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1425 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1427 #define INIT_STRPTIME
1430 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1431 #include "sanitizer_common_interceptors_format.inc"
1433 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1437 va_start(ap, format); \
1438 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1439 int res = WRAP(vname)(__VA_ARGS__, ap); \
1446 #if SANITIZER_INTERCEPT_SCANF
1448 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1451 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1454 int res = REAL(vname)(__VA_ARGS__); \
1456 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1461 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1462 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1464 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1465 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1467 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1468 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1470 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1471 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1472 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1474 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1476 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1478 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1479 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1480 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1482 INTERCEPTOR(int, scanf, const char *format, ...)
1483 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1485 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1486 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1488 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1489 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1491 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1492 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1493 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1495 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1496 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1498 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1499 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1504 #if SANITIZER_INTERCEPT_SCANF
1505 #define INIT_SCANF \
1506 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1507 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1508 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1509 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1510 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1511 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1516 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1517 #define INIT_ISOC99_SCANF \
1518 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1519 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1520 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1521 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1522 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1523 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1525 #define INIT_ISOC99_SCANF
1528 #if SANITIZER_INTERCEPT_PRINTF
1530 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1532 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1536 #define VPRINTF_INTERCEPTOR_RETURN() \
1539 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1541 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1542 if (common_flags()->check_printf) \
1543 printf_common(ctx, format, aq); \
1544 int res = REAL(vname)(__VA_ARGS__); \
1545 VPRINTF_INTERCEPTOR_RETURN(); \
1549 // FIXME: under ASan the REAL() call below may write to freed memory and
1550 // corrupt its metadata. See
1551 // https://github.com/google/sanitizers/issues/321.
1552 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1554 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1555 if (common_flags()->check_printf) { \
1556 printf_common(ctx, format, aq); \
1558 int res = REAL(vname)(str, __VA_ARGS__); \
1560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1562 VPRINTF_INTERCEPTOR_RETURN(); \
1566 // FIXME: under ASan the REAL() call below may write to freed memory and
1567 // corrupt its metadata. See
1568 // https://github.com/google/sanitizers/issues/321.
1569 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1571 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1572 if (common_flags()->check_printf) { \
1573 printf_common(ctx, format, aq); \
1575 int res = REAL(vname)(str, size, __VA_ARGS__); \
1577 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1579 VPRINTF_INTERCEPTOR_RETURN(); \
1583 // FIXME: under ASan the REAL() call below may write to freed memory and
1584 // corrupt its metadata. See
1585 // https://github.com/google/sanitizers/issues/321.
1586 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1588 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1589 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1590 if (common_flags()->check_printf) { \
1591 printf_common(ctx, format, aq); \
1593 int res = REAL(vname)(strp, __VA_ARGS__); \
1595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1597 VPRINTF_INTERCEPTOR_RETURN(); \
1601 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1602 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1604 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1606 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1608 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1610 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1612 #if SANITIZER_INTERCEPT___PRINTF_CHK
1613 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1614 SIZE_T size_to, const char *format, va_list ap)
1615 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1618 #if SANITIZER_INTERCEPT_PRINTF_L
1619 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1620 const char *format, va_list ap)
1621 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1623 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1624 const char *format, ...)
1625 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1626 #endif // SANITIZER_INTERCEPT_PRINTF_L
1628 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1629 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1631 #if SANITIZER_INTERCEPT___PRINTF_CHK
1632 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1633 const char *format, va_list ap)
1634 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1637 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1638 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1640 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1641 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1642 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1644 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1645 const char *format, va_list ap)
1646 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1648 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1650 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1652 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1654 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1657 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1659 INTERCEPTOR(int, printf, const char *format, ...)
1660 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1662 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1663 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1665 #if SANITIZER_INTERCEPT___PRINTF_CHK
1666 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1667 const char *format, ...)
1668 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1671 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1672 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1674 #if SANITIZER_INTERCEPT___PRINTF_CHK
1675 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1676 const char *format, ...) // NOLINT
1677 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format) // NOLINT
1680 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1681 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1683 #if SANITIZER_INTERCEPT___PRINTF_CHK
1684 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1685 SIZE_T size_to, const char *format, ...) // NOLINT
1686 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format) // NOLINT
1689 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1690 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1692 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1693 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1694 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1696 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1698 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1700 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1701 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1703 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1704 const char *format, ...)
1705 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1708 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1710 #endif // SANITIZER_INTERCEPT_PRINTF
1712 #if SANITIZER_INTERCEPT_PRINTF
1713 #define INIT_PRINTF \
1714 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1715 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1716 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1717 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1718 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1719 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1720 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1721 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1722 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1723 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1728 #if SANITIZER_INTERCEPT___PRINTF_CHK
1729 #define INIT___PRINTF_CHK \
1730 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1731 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1732 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1733 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1734 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1736 #define INIT___PRINTF_CHK
1739 #if SANITIZER_INTERCEPT_PRINTF_L
1740 #define INIT_PRINTF_L \
1741 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1742 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1744 #define INIT_PRINTF_L
1747 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1748 #define INIT_ISOC99_PRINTF \
1749 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1750 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1751 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1752 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1753 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1754 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1755 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1756 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1758 #define INIT_ISOC99_PRINTF
1761 #if SANITIZER_INTERCEPT_IOCTL
1762 #include "sanitizer_common_interceptors_ioctl.inc"
1763 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1764 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1765 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1766 // can trigger a report and we need to be able to unwind through this
1767 // function. On Mac in debug mode we might not have a frame pointer, because
1768 // ioctl_common_[pre|post] doesn't get inlined here.
1769 ENABLE_FRAME_POINTER;
1773 va_start(ap, request);
1774 void *arg = va_arg(ap, void *);
1776 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1778 CHECK(ioctl_initialized);
1780 // Note: TSan does not use common flags, and they are zero-initialized.
1781 // This effectively disables ioctl handling in TSan.
1782 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1784 // Although request is unsigned long, the rest of the interceptor uses it
1785 // as just "unsigned" to save space, because we know that all values fit in
1786 // "unsigned" - they are compile-time constants.
1788 const ioctl_desc *desc = ioctl_lookup(request);
1789 ioctl_desc decoded_desc;
1791 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1792 if (!ioctl_decode(request, &decoded_desc))
1793 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1795 desc = &decoded_desc;
1798 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1799 int res = REAL(ioctl)(d, request, arg);
1800 // FIXME: some ioctls have different return values for success and failure.
1801 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1804 #define INIT_IOCTL \
1806 COMMON_INTERCEPT_FUNCTION(ioctl);
1811 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1812 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1813 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1814 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1816 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1818 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1819 REAL(strlen)(pwd->pw_name) + 1);
1821 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1822 REAL(strlen)(pwd->pw_passwd) + 1);
1823 #if !SANITIZER_ANDROID
1825 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1826 REAL(strlen)(pwd->pw_gecos) + 1);
1830 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1831 REAL(strlen)(pwd->pw_class) + 1);
1834 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1835 REAL(strlen)(pwd->pw_dir) + 1);
1837 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1838 REAL(strlen)(pwd->pw_shell) + 1);
1842 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1846 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1847 REAL(strlen)(grp->gr_name) + 1);
1849 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1850 REAL(strlen)(grp->gr_passwd) + 1);
1851 char **p = grp->gr_mem;
1853 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1855 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1856 (p - grp->gr_mem + 1) * sizeof(*p));
1859 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1860 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1861 // SANITIZER_INTERCEPT_GETPWENT_R ||
1862 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1864 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1865 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1867 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1869 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1870 __sanitizer_passwd *res = REAL(getpwnam)(name);
1871 if (res) unpoison_passwd(ctx, res);
1874 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1876 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1877 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1878 if (res) unpoison_passwd(ctx, res);
1881 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1883 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1884 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1885 __sanitizer_group *res = REAL(getgrnam)(name);
1886 if (res) unpoison_group(ctx, res);
1889 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1891 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1892 __sanitizer_group *res = REAL(getgrgid)(gid);
1893 if (res) unpoison_group(ctx, res);
1896 #define INIT_GETPWNAM_AND_FRIENDS \
1897 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1898 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1899 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1900 COMMON_INTERCEPT_FUNCTION(getgrgid);
1902 #define INIT_GETPWNAM_AND_FRIENDS
1905 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1906 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1907 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1909 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1910 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1911 // FIXME: under ASan the call below may write to freed memory and corrupt
1912 // its metadata. See
1913 // https://github.com/google/sanitizers/issues/321.
1914 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1916 if (result && *result) unpoison_passwd(ctx, *result);
1917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1919 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1922 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1923 SIZE_T buflen, __sanitizer_passwd **result) {
1925 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1926 // FIXME: under ASan the call below may write to freed memory and corrupt
1927 // its metadata. See
1928 // https://github.com/google/sanitizers/issues/321.
1929 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1931 if (result && *result) unpoison_passwd(ctx, *result);
1932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1934 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1937 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1938 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1940 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1941 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1942 // FIXME: under ASan the call below may write to freed memory and corrupt
1943 // its metadata. See
1944 // https://github.com/google/sanitizers/issues/321.
1945 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1947 if (result && *result) unpoison_group(ctx, *result);
1948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1950 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1953 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1954 SIZE_T buflen, __sanitizer_group **result) {
1956 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1957 // FIXME: under ASan the call below may write to freed memory and corrupt
1958 // its metadata. See
1959 // https://github.com/google/sanitizers/issues/321.
1960 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1962 if (result && *result) unpoison_group(ctx, *result);
1963 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1965 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1968 #define INIT_GETPWNAM_R_AND_FRIENDS \
1969 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1970 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1971 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1972 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1974 #define INIT_GETPWNAM_R_AND_FRIENDS
1977 #if SANITIZER_INTERCEPT_GETPWENT
1978 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1980 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1981 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1982 if (res) unpoison_passwd(ctx, res);
1985 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1987 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1988 __sanitizer_group *res = REAL(getgrent)(dummy);
1989 if (res) unpoison_group(ctx, res);;
1992 #define INIT_GETPWENT \
1993 COMMON_INTERCEPT_FUNCTION(getpwent); \
1994 COMMON_INTERCEPT_FUNCTION(getgrent);
1996 #define INIT_GETPWENT
1999 #if SANITIZER_INTERCEPT_FGETPWENT
2000 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2002 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2003 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2004 if (res) unpoison_passwd(ctx, res);
2007 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2009 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2010 __sanitizer_group *res = REAL(fgetgrent)(fp);
2011 if (res) unpoison_group(ctx, res);
2014 #define INIT_FGETPWENT \
2015 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2016 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2018 #define INIT_FGETPWENT
2021 #if SANITIZER_INTERCEPT_GETPWENT_R
2022 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2023 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2025 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2026 // FIXME: under ASan the call below may write to freed memory and corrupt
2027 // its metadata. See
2028 // https://github.com/google/sanitizers/issues/321.
2029 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2031 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2032 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2034 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2037 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2038 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2040 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2041 // FIXME: under ASan the call below may write to freed memory and corrupt
2042 // its metadata. See
2043 // https://github.com/google/sanitizers/issues/321.
2044 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2046 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2047 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2049 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2052 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2053 __sanitizer_group **pwbufp) {
2055 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2056 // FIXME: under ASan the call below may write to freed memory and corrupt
2057 // its metadata. See
2058 // https://github.com/google/sanitizers/issues/321.
2059 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2061 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2062 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2064 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2067 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2068 SIZE_T buflen, __sanitizer_group **pwbufp) {
2070 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2071 // FIXME: under ASan the call below may write to freed memory and corrupt
2072 // its metadata. See
2073 // https://github.com/google/sanitizers/issues/321.
2074 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2076 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2077 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2079 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2082 #define INIT_GETPWENT_R \
2083 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2084 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
2085 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
2086 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2088 #define INIT_GETPWENT_R
2091 #if SANITIZER_INTERCEPT_SETPWENT
2092 // The only thing these interceptors do is disable any nested interceptors.
2093 // These functions may open nss modules and call uninstrumented functions from
2094 // them, and we don't want things like strlen() to trigger.
2095 INTERCEPTOR(void, setpwent, int dummy) {
2097 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2098 REAL(setpwent)(dummy);
2100 INTERCEPTOR(void, endpwent, int dummy) {
2102 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2103 REAL(endpwent)(dummy);
2105 INTERCEPTOR(void, setgrent, int dummy) {
2107 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2108 REAL(setgrent)(dummy);
2110 INTERCEPTOR(void, endgrent, int dummy) {
2112 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2113 REAL(endgrent)(dummy);
2115 #define INIT_SETPWENT \
2116 COMMON_INTERCEPT_FUNCTION(setpwent); \
2117 COMMON_INTERCEPT_FUNCTION(endpwent); \
2118 COMMON_INTERCEPT_FUNCTION(setgrent); \
2119 COMMON_INTERCEPT_FUNCTION(endgrent);
2121 #define INIT_SETPWENT
2124 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2125 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2127 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2128 // FIXME: under ASan the call below may write to freed memory and corrupt
2129 // its metadata. See
2130 // https://github.com/google/sanitizers/issues/321.
2131 int res = REAL(clock_getres)(clk_id, tp);
2133 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2137 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2139 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2140 // FIXME: under ASan the call below may write to freed memory and corrupt
2141 // its metadata. See
2142 // https://github.com/google/sanitizers/issues/321.
2143 int res = REAL(clock_gettime)(clk_id, tp);
2145 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2149 namespace __sanitizer {
2151 int real_clock_gettime(u32 clk_id, void *tp) {
2152 return REAL(clock_gettime)(clk_id, tp);
2155 } // namespace __sanitizer
2156 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2158 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2159 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2160 return REAL(clock_settime)(clk_id, tp);
2162 #define INIT_CLOCK_GETTIME \
2163 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2164 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2165 COMMON_INTERCEPT_FUNCTION(clock_settime);
2167 #define INIT_CLOCK_GETTIME
2170 #if SANITIZER_INTERCEPT_GETITIMER
2171 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2173 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2174 // FIXME: under ASan the call below may write to freed memory and corrupt
2175 // its metadata. See
2176 // https://github.com/google/sanitizers/issues/321.
2177 int res = REAL(getitimer)(which, curr_value);
2178 if (!res && curr_value) {
2179 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2183 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2185 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2187 // itimerval can contain padding that may be legitimately uninitialized
2188 const struct __sanitizer_itimerval *nv =
2189 (const struct __sanitizer_itimerval *)new_value;
2190 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2191 sizeof(__sanitizer_time_t));
2192 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2193 sizeof(__sanitizer_suseconds_t));
2194 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2195 sizeof(__sanitizer_time_t));
2196 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2197 sizeof(__sanitizer_suseconds_t));
2199 // FIXME: under ASan the call below may write to freed memory and corrupt
2200 // its metadata. See
2201 // https://github.com/google/sanitizers/issues/321.
2202 int res = REAL(setitimer)(which, new_value, old_value);
2203 if (!res && old_value) {
2204 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2208 #define INIT_GETITIMER \
2209 COMMON_INTERCEPT_FUNCTION(getitimer); \
2210 COMMON_INTERCEPT_FUNCTION(setitimer);
2212 #define INIT_GETITIMER
2215 #if SANITIZER_INTERCEPT_GLOB
2216 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2217 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2218 // +1 for NULL pointer at the end.
2219 if (pglob->gl_pathv)
2220 COMMON_INTERCEPTOR_WRITE_RANGE(
2221 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2222 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2223 char *p = pglob->gl_pathv[i];
2224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2228 #if SANITIZER_SOLARIS
2229 INTERCEPTOR(int, glob, const char *pattern, int flags,
2230 int (*errfunc)(const char *epath, int eerrno),
2231 __sanitizer_glob_t *pglob) {
2233 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2234 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2235 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2236 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2240 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2242 static void wrapped_gl_closedir(void *dir) {
2243 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2244 pglob_copy->gl_closedir(dir);
2247 static void *wrapped_gl_readdir(void *dir) {
2248 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2249 return pglob_copy->gl_readdir(dir);
2252 static void *wrapped_gl_opendir(const char *s) {
2253 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2254 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2255 return pglob_copy->gl_opendir(s);
2258 static int wrapped_gl_lstat(const char *s, void *st) {
2259 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2260 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2261 return pglob_copy->gl_lstat(s, st);
2264 static int wrapped_gl_stat(const char *s, void *st) {
2265 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2266 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2267 return pglob_copy->gl_stat(s, st);
2270 static const __sanitizer_glob_t kGlobCopy = {
2272 0, wrapped_gl_closedir, wrapped_gl_readdir,
2273 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2275 INTERCEPTOR(int, glob, const char *pattern, int flags,
2276 int (*errfunc)(const char *epath, int eerrno),
2277 __sanitizer_glob_t *pglob) {
2279 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2280 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2281 __sanitizer_glob_t glob_copy;
2282 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2283 if (flags & glob_altdirfunc) {
2284 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2285 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2286 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2287 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2288 Swap(pglob->gl_stat, glob_copy.gl_stat);
2289 pglob_copy = &glob_copy;
2291 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2292 if (flags & glob_altdirfunc) {
2293 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2294 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2295 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2296 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2297 Swap(pglob->gl_stat, glob_copy.gl_stat);
2300 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2303 #endif // SANITIZER_SOLARIS
2305 COMMON_INTERCEPT_FUNCTION(glob);
2306 #else // SANITIZER_INTERCEPT_GLOB
2308 #endif // SANITIZER_INTERCEPT_GLOB
2310 #if SANITIZER_INTERCEPT_GLOB64
2311 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2312 int (*errfunc)(const char *epath, int eerrno),
2313 __sanitizer_glob_t *pglob) {
2315 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2316 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2317 __sanitizer_glob_t glob_copy;
2318 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2319 if (flags & glob_altdirfunc) {
2320 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2321 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2322 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2323 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2324 Swap(pglob->gl_stat, glob_copy.gl_stat);
2325 pglob_copy = &glob_copy;
2327 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2328 if (flags & glob_altdirfunc) {
2329 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2330 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2331 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2332 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2333 Swap(pglob->gl_stat, glob_copy.gl_stat);
2336 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2339 #define INIT_GLOB64 \
2340 COMMON_INTERCEPT_FUNCTION(glob64);
2341 #else // SANITIZER_INTERCEPT_GLOB64
2343 #endif // SANITIZER_INTERCEPT_GLOB64
2345 #if SANITIZER_INTERCEPT_WAIT
2346 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2347 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2349 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2351 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2352 // FIXME: under ASan the call below may write to freed memory and corrupt
2353 // its metadata. See
2354 // https://github.com/google/sanitizers/issues/321.
2355 int res = REAL(wait)(status);
2356 if (res != -1 && status)
2357 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2360 // On FreeBSD id_t is always 64-bit wide.
2361 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2362 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2365 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2369 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2370 // FIXME: under ASan the call below may write to freed memory and corrupt
2371 // its metadata. See
2372 // https://github.com/google/sanitizers/issues/321.
2373 int res = REAL(waitid)(idtype, id, infop, options);
2374 if (res != -1 && infop)
2375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2378 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2380 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2381 // FIXME: under ASan the call below may write to freed memory and corrupt
2382 // its metadata. See
2383 // https://github.com/google/sanitizers/issues/321.
2384 int res = REAL(waitpid)(pid, status, options);
2385 if (res != -1 && status)
2386 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2389 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2391 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2392 // FIXME: under ASan the call below may write to freed memory and corrupt
2393 // its metadata. See
2394 // https://github.com/google/sanitizers/issues/321.
2395 int res = REAL(wait3)(status, options, rusage);
2397 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2398 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2402 #if SANITIZER_ANDROID
2403 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2405 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2406 // FIXME: under ASan the call below may write to freed memory and corrupt
2407 // its metadata. See
2408 // https://github.com/google/sanitizers/issues/321.
2409 int res = REAL(__wait4)(pid, status, options, rusage);
2411 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2412 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2416 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2418 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2420 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2421 // FIXME: under ASan the call below may write to freed memory and corrupt
2422 // its metadata. See
2423 // https://github.com/google/sanitizers/issues/321.
2424 int res = REAL(wait4)(pid, status, options, rusage);
2426 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2427 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2431 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2432 #endif // SANITIZER_ANDROID
2434 COMMON_INTERCEPT_FUNCTION(wait); \
2435 COMMON_INTERCEPT_FUNCTION(waitid); \
2436 COMMON_INTERCEPT_FUNCTION(waitpid); \
2437 COMMON_INTERCEPT_FUNCTION(wait3);
2443 #if SANITIZER_INTERCEPT_INET
2444 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2446 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2447 uptr sz = __sanitizer_in_addr_sz(af);
2448 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2449 // FIXME: figure out read size based on the address family.
2450 // FIXME: under ASan the call below may write to freed memory and corrupt
2451 // its metadata. See
2452 // https://github.com/google/sanitizers/issues/321.
2453 char *res = REAL(inet_ntop)(af, src, dst, size);
2454 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2457 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2459 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2460 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2461 // FIXME: figure out read size based on the address family.
2462 // FIXME: under ASan the call below may write to freed memory and corrupt
2463 // its metadata. See
2464 // https://github.com/google/sanitizers/issues/321.
2465 int res = REAL(inet_pton)(af, src, dst);
2467 uptr sz = __sanitizer_in_addr_sz(af);
2468 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2473 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2474 COMMON_INTERCEPT_FUNCTION(inet_pton);
2479 #if SANITIZER_INTERCEPT_INET
2480 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2482 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2483 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2484 // FIXME: under ASan the call below may write to freed memory and corrupt
2485 // its metadata. See
2486 // https://github.com/google/sanitizers/issues/321.
2487 int res = REAL(inet_aton)(cp, dst);
2489 uptr sz = __sanitizer_in_addr_sz(af_inet);
2490 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2494 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2496 #define INIT_INET_ATON
2499 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2500 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2502 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2503 // FIXME: under ASan the call below may write to freed memory and corrupt
2504 // its metadata. See
2505 // https://github.com/google/sanitizers/issues/321.
2506 int res = REAL(pthread_getschedparam)(thread, policy, param);
2508 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2509 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2513 #define INIT_PTHREAD_GETSCHEDPARAM \
2514 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2516 #define INIT_PTHREAD_GETSCHEDPARAM
2519 #if SANITIZER_INTERCEPT_GETADDRINFO
2520 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2521 struct __sanitizer_addrinfo *hints,
2522 struct __sanitizer_addrinfo **out) {
2524 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2525 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2527 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2529 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2530 // FIXME: under ASan the call below may write to freed memory and corrupt
2531 // its metadata. See
2532 // https://github.com/google/sanitizers/issues/321.
2533 int res = REAL(getaddrinfo)(node, service, hints, out);
2534 if (res == 0 && out) {
2535 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2536 struct __sanitizer_addrinfo *p = *out;
2538 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2540 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2541 if (p->ai_canonname)
2542 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2543 REAL(strlen)(p->ai_canonname) + 1);
2549 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2551 #define INIT_GETADDRINFO
2554 #if SANITIZER_INTERCEPT_GETNAMEINFO
2555 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2556 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2558 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2559 serv, servlen, flags);
2560 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2561 // There is padding in in_addr that may make this too noisy
2562 // FIXME: under ASan the call below may write to freed memory and corrupt
2563 // its metadata. See
2564 // https://github.com/google/sanitizers/issues/321.
2566 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2568 if (host && hostlen)
2569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2570 if (serv && servlen)
2571 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2575 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2577 #define INIT_GETNAMEINFO
2580 #if SANITIZER_INTERCEPT_GETSOCKNAME
2581 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2583 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2584 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2585 int addrlen_in = *addrlen;
2586 // FIXME: under ASan the call below may write to freed memory and corrupt
2587 // its metadata. See
2588 // https://github.com/google/sanitizers/issues/321.
2589 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2591 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2595 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2597 #define INIT_GETSOCKNAME
2600 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2601 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2602 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2604 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2605 char **p = h->h_aliases;
2607 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2610 COMMON_INTERCEPTOR_WRITE_RANGE(
2611 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2614 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2617 COMMON_INTERCEPTOR_WRITE_RANGE(
2618 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2622 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2623 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2625 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2626 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2627 if (res) write_hostent(ctx, res);
2631 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2634 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2635 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2636 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2637 if (res) write_hostent(ctx, res);
2641 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2643 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2644 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2645 if (res) write_hostent(ctx, res);
2648 #define INIT_GETHOSTBYNAME \
2649 COMMON_INTERCEPT_FUNCTION(gethostent); \
2650 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2651 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2653 #define INIT_GETHOSTBYNAME
2654 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2656 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2657 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2659 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2660 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2661 if (res) write_hostent(ctx, res);
2664 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2666 #define INIT_GETHOSTBYNAME2
2667 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2669 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2670 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2671 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2674 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2676 // FIXME: under ASan the call below may write to freed memory and corrupt
2677 // its metadata. See
2678 // https://github.com/google/sanitizers/issues/321.
2679 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2681 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2682 if (res == 0 && *result) write_hostent(ctx, *result);
2685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2688 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2690 #define INIT_GETHOSTBYNAME_R
2693 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2694 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2695 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2697 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2699 // FIXME: under ASan the call below may write to freed memory and corrupt
2700 // its metadata. See
2701 // https://github.com/google/sanitizers/issues/321.
2702 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2704 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2705 if (res == 0 && *result) write_hostent(ctx, *result);
2708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2711 #define INIT_GETHOSTENT_R \
2712 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2714 #define INIT_GETHOSTENT_R
2717 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2718 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2719 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2720 __sanitizer_hostent **result, int *h_errnop) {
2722 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2723 buflen, result, h_errnop);
2724 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2725 // FIXME: under ASan the call below may write to freed memory and corrupt
2726 // its metadata. See
2727 // https://github.com/google/sanitizers/issues/321.
2728 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2732 if (res == 0 && *result) write_hostent(ctx, *result);
2735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2738 #define INIT_GETHOSTBYADDR_R \
2739 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2741 #define INIT_GETHOSTBYADDR_R
2744 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2745 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2746 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2747 __sanitizer_hostent **result, int *h_errnop) {
2749 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2751 // FIXME: under ASan the call below may write to freed memory and corrupt
2752 // its metadata. See
2753 // https://github.com/google/sanitizers/issues/321.
2755 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2758 if (res == 0 && *result) write_hostent(ctx, *result);
2761 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2764 #define INIT_GETHOSTBYNAME2_R \
2765 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2767 #define INIT_GETHOSTBYNAME2_R
2770 #if SANITIZER_INTERCEPT_GETSOCKOPT
2771 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2774 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2776 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2777 // FIXME: under ASan the call below may write to freed memory and corrupt
2778 // its metadata. See
2779 // https://github.com/google/sanitizers/issues/321.
2780 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2782 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2785 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2787 #define INIT_GETSOCKOPT
2790 #if SANITIZER_INTERCEPT_ACCEPT
2791 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2793 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2794 unsigned addrlen0 = 0;
2796 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2797 addrlen0 = *addrlen;
2799 int fd2 = REAL(accept)(fd, addr, addrlen);
2801 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2802 if (addr && addrlen)
2803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2807 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2812 #if SANITIZER_INTERCEPT_ACCEPT4
2813 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2815 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2816 unsigned addrlen0 = 0;
2818 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2819 addrlen0 = *addrlen;
2821 // FIXME: under ASan the call below may write to freed memory and corrupt
2822 // its metadata. See
2823 // https://github.com/google/sanitizers/issues/321.
2824 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2826 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2827 if (addr && addrlen)
2828 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2832 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2834 #define INIT_ACCEPT4
2837 #if SANITIZER_INTERCEPT_PACCEPT
2838 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2839 __sanitizer_sigset_t *set, int f) {
2841 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2842 unsigned addrlen0 = 0;
2844 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2845 addrlen0 = *addrlen;
2847 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2848 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
2850 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2851 if (addr && addrlen)
2852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2856 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2858 #define INIT_PACCEPT
2861 #if SANITIZER_INTERCEPT_MODF
2862 INTERCEPTOR(double, modf, double x, double *iptr) {
2864 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2865 // FIXME: under ASan the call below may write to freed memory and corrupt
2866 // its metadata. See
2867 // https://github.com/google/sanitizers/issues/321.
2868 double res = REAL(modf)(x, iptr);
2870 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2874 INTERCEPTOR(float, modff, float x, float *iptr) {
2876 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2877 // FIXME: under ASan the call below may write to freed memory and corrupt
2878 // its metadata. See
2879 // https://github.com/google/sanitizers/issues/321.
2880 float res = REAL(modff)(x, iptr);
2882 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2886 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2888 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2889 // FIXME: under ASan the call below may write to freed memory and corrupt
2890 // its metadata. See
2891 // https://github.com/google/sanitizers/issues/321.
2892 long double res = REAL(modfl)(x, iptr);
2894 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2899 COMMON_INTERCEPT_FUNCTION(modf); \
2900 COMMON_INTERCEPT_FUNCTION(modff); \
2901 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2906 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2907 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2910 if (msg->msg_name && msg->msg_namelen)
2911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2912 if (msg->msg_iov && msg->msg_iovlen)
2913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2914 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2915 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2916 if (msg->msg_control && msg->msg_controllen)
2917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2921 #if SANITIZER_INTERCEPT_RECVMSG
2922 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2925 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2926 // FIXME: under ASan the call below may write to freed memory and corrupt
2927 // its metadata. See
2928 // https://github.com/google/sanitizers/issues/321.
2929 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2931 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2933 write_msghdr(ctx, msg, res);
2934 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2939 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2941 #define INIT_RECVMSG
2944 #if SANITIZER_INTERCEPT_RECVMMSG
2945 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2946 unsigned int vlen, int flags, void *timeout) {
2948 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2949 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2950 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2952 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2953 for (int i = 0; i < res; ++i) {
2954 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2955 sizeof(msgvec[i].msg_len));
2956 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2957 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2962 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2964 #define INIT_RECVMMSG
2967 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2968 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2969 const unsigned kCmsgDataOffset =
2970 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2972 char *p = (char *)control;
2973 char *const control_end = p + controllen;
2975 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2976 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2977 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2979 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2981 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2982 sizeof(cmsg->cmsg_level));
2983 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2984 sizeof(cmsg->cmsg_type));
2986 if (cmsg->cmsg_len > kCmsgDataOffset) {
2987 char *data = p + kCmsgDataOffset;
2988 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2989 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2992 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2996 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2999 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3008 if (msg->msg_name && msg->msg_namelen)
3009 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3010 if (msg->msg_iov && msg->msg_iovlen)
3011 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3012 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3013 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3014 if (msg->msg_control && msg->msg_controllen)
3015 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3019 #if SANITIZER_INTERCEPT_SENDMSG
3020 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3023 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3025 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3026 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3028 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3029 if (common_flags()->intercept_send && res >= 0 && msg)
3030 read_msghdr(ctx, msg, res);
3033 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3035 #define INIT_SENDMSG
3038 #if SANITIZER_INTERCEPT_SENDMMSG
3039 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3040 unsigned vlen, int flags) {
3042 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3044 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3045 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3047 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3048 if (res >= 0 && msgvec)
3049 for (int i = 0; i < res; ++i) {
3050 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3051 sizeof(msgvec[i].msg_len));
3052 if (common_flags()->intercept_send)
3053 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3057 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3059 #define INIT_SENDMMSG
3062 #if SANITIZER_INTERCEPT_GETPEERNAME
3063 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3065 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3067 if (addrlen) addr_sz = *addrlen;
3068 // FIXME: under ASan the call below may write to freed memory and corrupt
3069 // its metadata. See
3070 // https://github.com/google/sanitizers/issues/321.
3071 int res = REAL(getpeername)(sockfd, addr, addrlen);
3072 if (!res && addr && addrlen)
3073 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3076 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3078 #define INIT_GETPEERNAME
3081 #if SANITIZER_INTERCEPT_SYSINFO
3082 INTERCEPTOR(int, sysinfo, void *info) {
3084 // FIXME: under ASan the call below may write to freed memory and corrupt
3085 // its metadata. See
3086 // https://github.com/google/sanitizers/issues/321.
3087 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3088 int res = REAL(sysinfo)(info);
3090 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3093 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3095 #define INIT_SYSINFO
3098 #if SANITIZER_INTERCEPT_READDIR
3099 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3101 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3102 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3103 __sanitizer_dirent *res = REAL(opendir)(path);
3105 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3109 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3111 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3112 // FIXME: under ASan the call below may write to freed memory and corrupt
3113 // its metadata. See
3114 // https://github.com/google/sanitizers/issues/321.
3115 __sanitizer_dirent *res = REAL(readdir)(dirp);
3116 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3120 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3121 __sanitizer_dirent **result) {
3123 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3124 // FIXME: under ASan the call below may write to freed memory and corrupt
3125 // its metadata. See
3126 // https://github.com/google/sanitizers/issues/321.
3127 int res = REAL(readdir_r)(dirp, entry, result);
3129 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3131 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3136 #define INIT_READDIR \
3137 COMMON_INTERCEPT_FUNCTION(opendir); \
3138 COMMON_INTERCEPT_FUNCTION(readdir); \
3139 COMMON_INTERCEPT_FUNCTION(readdir_r);
3141 #define INIT_READDIR
3144 #if SANITIZER_INTERCEPT_READDIR64
3145 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3147 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3148 // FIXME: under ASan the call below may write to freed memory and corrupt
3149 // its metadata. See
3150 // https://github.com/google/sanitizers/issues/321.
3151 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3152 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3156 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3157 __sanitizer_dirent64 **result) {
3159 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3160 // FIXME: under ASan the call below may write to freed memory and corrupt
3161 // its metadata. See
3162 // https://github.com/google/sanitizers/issues/321.
3163 int res = REAL(readdir64_r)(dirp, entry, result);
3165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3167 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3171 #define INIT_READDIR64 \
3172 COMMON_INTERCEPT_FUNCTION(readdir64); \
3173 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3175 #define INIT_READDIR64
3178 #if SANITIZER_INTERCEPT_PTRACE
3179 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3181 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3182 __sanitizer_iovec local_iovec;
3185 if (request == ptrace_setregs)
3186 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3187 else if (request == ptrace_setfpregs)
3188 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3189 else if (request == ptrace_setfpxregs)
3190 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3191 else if (request == ptrace_setvfpregs)
3192 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3193 else if (request == ptrace_setsiginfo)
3194 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3195 // Some kernel might zero the iovec::iov_base in case of invalid
3196 // write access. In this case copy the invalid address for further
3198 else if (request == ptrace_setregset || request == ptrace_getregset) {
3199 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3200 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3201 local_iovec = *iovec;
3202 if (request == ptrace_setregset)
3203 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3207 // FIXME: under ASan the call below may write to freed memory and corrupt
3208 // its metadata. See
3209 // https://github.com/google/sanitizers/issues/321.
3210 uptr res = REAL(ptrace)(request, pid, addr, data);
3213 // Note that PEEK* requests assign different meaning to the return value.
3214 // This function does not handle them (nor does it need to).
3215 if (request == ptrace_getregs)
3216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3217 else if (request == ptrace_getfpregs)
3218 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3219 else if (request == ptrace_getfpxregs)
3220 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3221 else if (request == ptrace_getvfpregs)
3222 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3223 else if (request == ptrace_getsiginfo)
3224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3225 else if (request == ptrace_geteventmsg)
3226 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3227 else if (request == ptrace_getregset) {
3228 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3229 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3231 local_iovec.iov_len);
3237 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3242 #if SANITIZER_INTERCEPT_SETLOCALE
3243 static void unpoison_ctype_arrays(void *ctx) {
3244 #if SANITIZER_NETBSD
3245 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3252 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3254 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3256 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3257 char *res = REAL(setlocale)(category, locale);
3259 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3260 unpoison_ctype_arrays(ctx);
3265 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3267 #define INIT_SETLOCALE
3270 #if SANITIZER_INTERCEPT_GETCWD
3271 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3273 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3274 // FIXME: under ASan the call below may write to freed memory and corrupt
3275 // its metadata. See
3276 // https://github.com/google/sanitizers/issues/321.
3277 char *res = REAL(getcwd)(buf, size);
3278 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3281 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3286 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3287 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3289 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3290 // FIXME: under ASan the call below may write to freed memory and corrupt
3291 // its metadata. See
3292 // https://github.com/google/sanitizers/issues/321.
3293 char *res = REAL(get_current_dir_name)(fake);
3294 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3298 #define INIT_GET_CURRENT_DIR_NAME \
3299 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3301 #define INIT_GET_CURRENT_DIR_NAME
3304 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3306 if (nptr == *endptr) {
3307 // No digits were found at strtol call, we need to find out the last
3308 // symbol accessed by strtoll on our own.
3309 // We get this symbol by skipping leading blanks and optional +/- sign.
3310 while (IsSpace(*nptr)) nptr++;
3311 if (*nptr == '+' || *nptr == '-') nptr++;
3312 *endptr = const_cast<char *>(nptr);
3314 CHECK(*endptr >= nptr);
3317 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3318 char **endptr, char *real_endptr, int base) {
3320 *endptr = real_endptr;
3321 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3323 // If base has unsupported value, strtol can exit with EINVAL
3324 // without reading any characters. So do additional checks only
3325 // if base is valid.
3326 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3327 if (is_valid_base) {
3328 FixRealStrtolEndptr(nptr, &real_endptr);
3330 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3331 (real_endptr - nptr) + 1 : 0);
3335 #if SANITIZER_INTERCEPT_STRTOIMAX
3336 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3338 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3339 // FIXME: under ASan the call below may write to freed memory and corrupt
3340 // its metadata. See
3341 // https://github.com/google/sanitizers/issues/321.
3343 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3344 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3348 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3350 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3351 // FIXME: under ASan the call below may write to freed memory and corrupt
3352 // its metadata. See
3353 // https://github.com/google/sanitizers/issues/321.
3355 INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3356 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3360 #define INIT_STRTOIMAX \
3361 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3362 COMMON_INTERCEPT_FUNCTION(strtoumax);
3364 #define INIT_STRTOIMAX
3367 #if SANITIZER_INTERCEPT_MBSTOWCS
3368 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3370 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
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.
3374 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3375 if (res != (SIZE_T) - 1 && dest) {
3376 SIZE_T write_cnt = res + (res < len);
3377 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3382 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3385 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3386 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3387 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3388 // FIXME: under ASan the call below may write to freed memory and corrupt
3389 // its metadata. See
3390 // https://github.com/google/sanitizers/issues/321.
3391 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3392 if (res != (SIZE_T)(-1) && dest && src) {
3393 // This function, and several others, may or may not write the terminating
3394 // \0 character. They write it iff they clear *src.
3395 SIZE_T write_cnt = res + !*src;
3396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3401 #define INIT_MBSTOWCS \
3402 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3403 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3405 #define INIT_MBSTOWCS
3408 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3409 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3410 SIZE_T len, void *ps) {
3412 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3414 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3415 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3417 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3418 // FIXME: under ASan the call below may write to freed memory and corrupt
3419 // its metadata. See
3420 // https://github.com/google/sanitizers/issues/321.
3421 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3422 if (res != (SIZE_T)(-1) && dest && src) {
3423 SIZE_T write_cnt = res + !*src;
3424 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3429 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3431 #define INIT_MBSNRTOWCS
3434 #if SANITIZER_INTERCEPT_WCSTOMBS
3435 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3437 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3438 // FIXME: under ASan the call below may write to freed memory and corrupt
3439 // its metadata. See
3440 // https://github.com/google/sanitizers/issues/321.
3441 SIZE_T res = REAL(wcstombs)(dest, src, len);
3442 if (res != (SIZE_T) - 1 && dest) {
3443 SIZE_T write_cnt = res + (res < len);
3444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3449 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3452 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3453 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3454 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3455 // FIXME: under ASan the call below may write to freed memory and corrupt
3456 // its metadata. See
3457 // https://github.com/google/sanitizers/issues/321.
3458 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3459 if (res != (SIZE_T) - 1 && dest && src) {
3460 SIZE_T write_cnt = res + !*src;
3461 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3466 #define INIT_WCSTOMBS \
3467 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3468 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3470 #define INIT_WCSTOMBS
3473 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3474 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3475 SIZE_T len, void *ps) {
3477 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3479 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3480 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3482 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3483 // FIXME: under ASan the call below may write to freed memory and corrupt
3484 // its metadata. See
3485 // https://github.com/google/sanitizers/issues/321.
3486 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3487 if (res != ((SIZE_T)-1) && dest && src) {
3488 SIZE_T write_cnt = res + !*src;
3489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3494 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3496 #define INIT_WCSNRTOMBS
3500 #if SANITIZER_INTERCEPT_WCRTOMB
3501 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3503 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3504 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3505 // FIXME: under ASan the call below may write to freed memory and corrupt
3506 // its metadata. See
3507 // https://github.com/google/sanitizers/issues/321.
3508 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3509 if (res != ((SIZE_T)-1) && dest) {
3510 SIZE_T write_cnt = res;
3511 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3516 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3518 #define INIT_WCRTOMB
3521 #if SANITIZER_INTERCEPT_TCGETATTR
3522 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3524 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3525 // FIXME: under ASan the call below may write to freed memory and corrupt
3526 // its metadata. See
3527 // https://github.com/google/sanitizers/issues/321.
3528 int res = REAL(tcgetattr)(fd, termios_p);
3529 if (!res && termios_p)
3530 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3534 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3536 #define INIT_TCGETATTR
3539 #if SANITIZER_INTERCEPT_REALPATH
3540 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3542 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3543 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3545 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3546 // version of a versioned symbol. For realpath(), this gives us something
3547 // (called __old_realpath) that does not handle NULL in the second argument.
3548 // Handle it as part of the interceptor.
3549 char *allocated_path = nullptr;
3551 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3553 char *res = REAL(realpath)(path, resolved_path);
3554 if (allocated_path && !res) WRAP(free)(allocated_path);
3555 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3558 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3560 #define INIT_REALPATH
3563 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3564 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3566 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3567 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3568 char *res = REAL(canonicalize_file_name)(path);
3569 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3572 #define INIT_CANONICALIZE_FILE_NAME \
3573 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3575 #define INIT_CANONICALIZE_FILE_NAME
3578 #if SANITIZER_INTERCEPT_CONFSTR
3579 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3581 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
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 SIZE_T res = REAL(confstr)(name, buf, len);
3587 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3590 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3592 #define INIT_CONFSTR
3595 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3596 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3598 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3599 // FIXME: under ASan the call below may write to freed memory and corrupt
3600 // its metadata. See
3601 // https://github.com/google/sanitizers/issues/321.
3602 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3603 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3606 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3608 #define INIT_SCHED_GETAFFINITY
3611 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3612 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3614 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3615 int res = REAL(sched_getparam)(pid, param);
3616 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3619 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3621 #define INIT_SCHED_GETPARAM
3624 #if SANITIZER_INTERCEPT_STRERROR
3625 INTERCEPTOR(char *, strerror, int errnum) {
3627 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3628 char *res = REAL(strerror)(errnum);
3629 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3632 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3634 #define INIT_STRERROR
3637 #if SANITIZER_INTERCEPT_STRERROR_R
3638 // There are 2 versions of strerror_r:
3639 // * POSIX version returns 0 on success, negative error code on failure,
3640 // writes message to buf.
3641 // * GNU version returns message pointer, which points to either buf or some
3643 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3644 SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3645 SANITIZER_FREEBSD || SANITIZER_OPENBSD
3646 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3647 // At least on OSX, buf contents are valid even when the call fails.
3648 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3650 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3651 // FIXME: under ASan the call below may write to freed memory and corrupt
3652 // its metadata. See
3653 // https://github.com/google/sanitizers/issues/321.
3654 int res = REAL(strerror_r)(errnum, buf, buflen);
3656 SIZE_T sz = internal_strnlen(buf, buflen);
3657 if (sz < buflen) ++sz;
3658 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3663 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3665 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3666 // FIXME: under ASan the call below may write to freed memory and corrupt
3667 // its metadata. See
3668 // https://github.com/google/sanitizers/issues/321.
3669 char *res = REAL(strerror_r)(errnum, buf, buflen);
3671 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3673 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3676 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3678 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3680 #define INIT_STRERROR_R
3683 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3684 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3686 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3687 // FIXME: under ASan the call below may write to freed memory and corrupt
3688 // its metadata. See
3689 // https://github.com/google/sanitizers/issues/321.
3690 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3691 // This version always returns a null-terminated string.
3693 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3696 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3698 #define INIT_XPG_STRERROR_R
3701 #if SANITIZER_INTERCEPT_SCANDIR
3702 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3703 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3704 const struct __sanitizer_dirent **);
3706 static THREADLOCAL scandir_filter_f scandir_filter;
3707 static THREADLOCAL scandir_compar_f scandir_compar;
3709 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3710 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3711 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3712 return scandir_filter(dir);
3715 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3716 const struct __sanitizer_dirent **b) {
3717 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3718 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3719 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3720 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3721 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3722 return scandir_compar(a, b);
3725 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3726 scandir_filter_f filter, scandir_compar_f compar) {
3728 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3729 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3730 scandir_filter = filter;
3731 scandir_compar = compar;
3732 // FIXME: under ASan the call below may write to freed memory and corrupt
3733 // its metadata. See
3734 // https://github.com/google/sanitizers/issues/321.
3735 int res = REAL(scandir)(dirp, namelist,
3736 filter ? wrapped_scandir_filter : nullptr,
3737 compar ? wrapped_scandir_compar : nullptr);
3738 scandir_filter = nullptr;
3739 scandir_compar = nullptr;
3740 if (namelist && res > 0) {
3741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3743 for (int i = 0; i < res; ++i)
3744 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3745 (*namelist)[i]->d_reclen);
3749 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3751 #define INIT_SCANDIR
3754 #if SANITIZER_INTERCEPT_SCANDIR64
3755 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3756 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3757 const struct __sanitizer_dirent64 **);
3759 static THREADLOCAL scandir64_filter_f scandir64_filter;
3760 static THREADLOCAL scandir64_compar_f scandir64_compar;
3762 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3763 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3764 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3765 return scandir64_filter(dir);
3768 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3769 const struct __sanitizer_dirent64 **b) {
3770 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3771 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3772 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3773 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3774 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3775 return scandir64_compar(a, b);
3778 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3779 scandir64_filter_f filter, scandir64_compar_f compar) {
3781 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3782 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3783 scandir64_filter = filter;
3784 scandir64_compar = compar;
3785 // FIXME: under ASan the call below may write to freed memory and corrupt
3786 // its metadata. See
3787 // https://github.com/google/sanitizers/issues/321.
3789 REAL(scandir64)(dirp, namelist,
3790 filter ? wrapped_scandir64_filter : nullptr,
3791 compar ? wrapped_scandir64_compar : nullptr);
3792 scandir64_filter = nullptr;
3793 scandir64_compar = nullptr;
3794 if (namelist && res > 0) {
3795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3796 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3797 for (int i = 0; i < res; ++i)
3798 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3799 (*namelist)[i]->d_reclen);
3803 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3805 #define INIT_SCANDIR64
3808 #if SANITIZER_INTERCEPT_GETGROUPS
3809 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3811 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3812 // FIXME: under ASan the call below may write to freed memory and corrupt
3813 // its metadata. See
3814 // https://github.com/google/sanitizers/issues/321.
3815 int res = REAL(getgroups)(size, lst);
3816 if (res >= 0 && lst && size > 0)
3817 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3820 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3822 #define INIT_GETGROUPS
3825 #if SANITIZER_INTERCEPT_POLL
3826 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3827 __sanitizer_nfds_t nfds) {
3828 for (unsigned i = 0; i < nfds; ++i) {
3829 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3830 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3834 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3835 __sanitizer_nfds_t nfds) {
3836 for (unsigned i = 0; i < nfds; ++i)
3837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3838 sizeof(fds[i].revents));
3841 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3844 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3845 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3846 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3847 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3850 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3855 #if SANITIZER_INTERCEPT_PPOLL
3856 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3857 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3859 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3860 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3862 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3863 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3865 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3866 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3869 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3874 #if SANITIZER_INTERCEPT_WORDEXP
3875 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3877 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3878 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3879 // FIXME: under ASan the call below may write to freed memory and corrupt
3880 // its metadata. See
3881 // https://github.com/google/sanitizers/issues/321.
3882 int res = REAL(wordexp)(s, p, flags);
3884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3887 sizeof(*p->we_wordv) * p->we_wordc);
3888 for (uptr i = 0; i < p->we_wordc; ++i) {
3889 char *w = p->we_wordv[i];
3890 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3895 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3897 #define INIT_WORDEXP
3900 #if SANITIZER_INTERCEPT_SIGWAIT
3901 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3903 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3904 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3905 // FIXME: under ASan the call below may write to freed memory and corrupt
3906 // its metadata. See
3907 // https://github.com/google/sanitizers/issues/321.
3908 int res = REAL(sigwait)(set, sig);
3909 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3912 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3914 #define INIT_SIGWAIT
3917 #if SANITIZER_INTERCEPT_SIGWAITINFO
3918 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3920 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3921 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3922 // FIXME: under ASan the call below may write to freed memory and corrupt
3923 // its metadata. See
3924 // https://github.com/google/sanitizers/issues/321.
3925 int res = REAL(sigwaitinfo)(set, info);
3926 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3929 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3931 #define INIT_SIGWAITINFO
3934 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3935 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3938 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3939 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3940 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3941 // FIXME: under ASan the call below may write to freed memory and corrupt
3942 // its metadata. See
3943 // https://github.com/google/sanitizers/issues/321.
3944 int res = REAL(sigtimedwait)(set, info, timeout);
3945 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3948 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3950 #define INIT_SIGTIMEDWAIT
3953 #if SANITIZER_INTERCEPT_SIGSETOPS
3954 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3956 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3957 // FIXME: under ASan the call below may write to freed memory and corrupt
3958 // its metadata. See
3959 // https://github.com/google/sanitizers/issues/321.
3960 int res = REAL(sigemptyset)(set);
3961 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3965 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3967 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3968 // FIXME: under ASan the call below may write to freed memory and corrupt
3969 // its metadata. See
3970 // https://github.com/google/sanitizers/issues/321.
3971 int res = REAL(sigfillset)(set);
3972 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3975 #define INIT_SIGSETOPS \
3976 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3977 COMMON_INTERCEPT_FUNCTION(sigfillset);
3979 #define INIT_SIGSETOPS
3982 #if SANITIZER_INTERCEPT_SIGPENDING
3983 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3985 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3986 // FIXME: under ASan the call below may write to freed memory and corrupt
3987 // its metadata. See
3988 // https://github.com/google/sanitizers/issues/321.
3989 int res = REAL(sigpending)(set);
3990 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3993 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3995 #define INIT_SIGPENDING
3998 #if SANITIZER_INTERCEPT_SIGPROCMASK
3999 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4000 __sanitizer_sigset_t *oldset) {
4002 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4003 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4004 // FIXME: under ASan the call below may write to freed memory and corrupt
4005 // its metadata. See
4006 // https://github.com/google/sanitizers/issues/321.
4007 int res = REAL(sigprocmask)(how, set, oldset);
4009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4012 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4014 #define INIT_SIGPROCMASK
4017 #if SANITIZER_INTERCEPT_BACKTRACE
4018 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4020 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4021 // FIXME: under ASan the call below may write to freed memory and corrupt
4022 // its metadata. See
4023 // https://github.com/google/sanitizers/issues/321.
4024 int res = REAL(backtrace)(buffer, size);
4026 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4030 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4032 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4034 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4035 // FIXME: under ASan the call below may write to freed memory and corrupt
4036 // its metadata. See
4037 // https://github.com/google/sanitizers/issues/321.
4038 char **res = REAL(backtrace_symbols)(buffer, size);
4040 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4041 for (int i = 0; i < size; ++i)
4042 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4046 #define INIT_BACKTRACE \
4047 COMMON_INTERCEPT_FUNCTION(backtrace); \
4048 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4050 #define INIT_BACKTRACE
4053 #if SANITIZER_INTERCEPT__EXIT
4054 INTERCEPTOR(void, _exit, int status) {
4056 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4057 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4058 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4059 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4060 if (status == 0) status = status1;
4061 REAL(_exit)(status);
4063 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4068 #if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4069 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4071 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4072 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4073 int res = REAL(pthread_mutex_lock)(m);
4074 if (res == errno_EOWNERDEAD)
4075 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4076 if (res == 0 || res == errno_EOWNERDEAD)
4077 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4078 if (res == errno_EINVAL)
4079 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4083 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4085 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4086 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4087 int res = REAL(pthread_mutex_unlock)(m);
4088 if (res == errno_EINVAL)
4089 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4093 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4094 #define INIT_PTHREAD_MUTEX_UNLOCK \
4095 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4097 #define INIT_PTHREAD_MUTEX_LOCK
4098 #define INIT_PTHREAD_MUTEX_UNLOCK
4101 #if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4102 INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4103 return WRAP(pthread_mutex_lock)(m);
4106 INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4107 return WRAP(pthread_mutex_unlock)(m);
4110 #define INIT___PTHREAD_MUTEX_LOCK \
4111 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4112 #define INIT___PTHREAD_MUTEX_UNLOCK \
4113 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4115 #define INIT___PTHREAD_MUTEX_LOCK
4116 #define INIT___PTHREAD_MUTEX_UNLOCK
4119 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4120 INTERCEPTOR(int, __libc_mutex_lock, void *m)
4121 ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4123 INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4124 ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4126 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4127 ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4129 #define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4130 #define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4131 #define INIT___LIBC_THR_SETCANCELSTATE \
4132 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4134 #define INIT___LIBC_MUTEX_LOCK
4135 #define INIT___LIBC_MUTEX_UNLOCK
4136 #define INIT___LIBC_THR_SETCANCELSTATE
4139 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4140 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4141 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4142 if (mnt->mnt_fsname)
4143 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4144 REAL(strlen)(mnt->mnt_fsname) + 1);
4146 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4147 REAL(strlen)(mnt->mnt_dir) + 1);
4149 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4150 REAL(strlen)(mnt->mnt_type) + 1);
4152 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4153 REAL(strlen)(mnt->mnt_opts) + 1);
4157 #if SANITIZER_INTERCEPT_GETMNTENT
4158 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4160 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4161 __sanitizer_mntent *res = REAL(getmntent)(fp);
4162 if (res) write_mntent(ctx, res);
4165 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4167 #define INIT_GETMNTENT
4170 #if SANITIZER_INTERCEPT_GETMNTENT_R
4171 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4172 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4174 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4175 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4176 if (res) write_mntent(ctx, res);
4179 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4181 #define INIT_GETMNTENT_R
4184 #if SANITIZER_INTERCEPT_STATFS
4185 INTERCEPTOR(int, statfs, char *path, void *buf) {
4187 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4188 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4189 // FIXME: under ASan the call below may write to freed memory and corrupt
4190 // its metadata. See
4191 // https://github.com/google/sanitizers/issues/321.
4192 int res = REAL(statfs)(path, buf);
4193 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4196 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4198 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4199 // FIXME: under ASan the call below may write to freed memory and corrupt
4200 // its metadata. See
4201 // https://github.com/google/sanitizers/issues/321.
4202 int res = REAL(fstatfs)(fd, buf);
4203 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4206 #define INIT_STATFS \
4207 COMMON_INTERCEPT_FUNCTION(statfs); \
4208 COMMON_INTERCEPT_FUNCTION(fstatfs);
4213 #if SANITIZER_INTERCEPT_STATFS64
4214 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4216 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4217 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4218 // FIXME: under ASan the call below may write to freed memory and corrupt
4219 // its metadata. See
4220 // https://github.com/google/sanitizers/issues/321.
4221 int res = REAL(statfs64)(path, buf);
4222 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4225 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4227 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4228 // FIXME: under ASan the call below may write to freed memory and corrupt
4229 // its metadata. See
4230 // https://github.com/google/sanitizers/issues/321.
4231 int res = REAL(fstatfs64)(fd, buf);
4232 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4235 #define INIT_STATFS64 \
4236 COMMON_INTERCEPT_FUNCTION(statfs64); \
4237 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4239 #define INIT_STATFS64
4242 #if SANITIZER_INTERCEPT_STATVFS
4243 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4245 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4246 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4247 // FIXME: under ASan the call below may write to freed memory and corrupt
4248 // its metadata. See
4249 // https://github.com/google/sanitizers/issues/321.
4250 int res = REAL(statvfs)(path, buf);
4251 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4254 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4256 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4257 // FIXME: under ASan the call below may write to freed memory and corrupt
4258 // its metadata. See
4259 // https://github.com/google/sanitizers/issues/321.
4260 int res = REAL(fstatvfs)(fd, buf);
4261 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4264 #define INIT_STATVFS \
4265 COMMON_INTERCEPT_FUNCTION(statvfs); \
4266 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4268 #define INIT_STATVFS
4271 #if SANITIZER_INTERCEPT_STATVFS64
4272 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4274 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4275 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
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(statvfs64)(path, buf);
4280 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4283 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4285 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4286 // FIXME: under ASan the call below may write to freed memory and corrupt
4287 // its metadata. See
4288 // https://github.com/google/sanitizers/issues/321.
4289 int res = REAL(fstatvfs64)(fd, buf);
4290 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4293 #define INIT_STATVFS64 \
4294 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4295 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4297 #define INIT_STATVFS64
4300 #if SANITIZER_INTERCEPT_INITGROUPS
4301 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4303 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4304 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4305 int res = REAL(initgroups)(user, group);
4308 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4310 #define INIT_INITGROUPS
4313 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4314 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4316 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4317 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4318 char *res = REAL(ether_ntoa)(addr);
4319 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4322 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4324 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4325 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4326 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4327 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4330 #define INIT_ETHER_NTOA_ATON \
4331 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4332 COMMON_INTERCEPT_FUNCTION(ether_aton);
4334 #define INIT_ETHER_NTOA_ATON
4337 #if SANITIZER_INTERCEPT_ETHER_HOST
4338 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4340 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4341 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4342 // FIXME: under ASan the call below may write to freed memory and corrupt
4343 // its metadata. See
4344 // https://github.com/google/sanitizers/issues/321.
4345 int res = REAL(ether_ntohost)(hostname, addr);
4346 if (!res && hostname)
4347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4350 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4352 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4354 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4355 // FIXME: under ASan the call below may write to freed memory and corrupt
4356 // its metadata. See
4357 // https://github.com/google/sanitizers/issues/321.
4358 int res = REAL(ether_hostton)(hostname, addr);
4359 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4362 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4365 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4366 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4367 // FIXME: under ASan the call below may write to freed memory and corrupt
4368 // its metadata. See
4369 // https://github.com/google/sanitizers/issues/321.
4370 int res = REAL(ether_line)(line, addr, hostname);
4372 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4378 #define INIT_ETHER_HOST \
4379 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4380 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4381 COMMON_INTERCEPT_FUNCTION(ether_line);
4383 #define INIT_ETHER_HOST
4386 #if SANITIZER_INTERCEPT_ETHER_R
4387 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4389 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4390 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4391 // FIXME: under ASan the call below may write to freed memory and corrupt
4392 // its metadata. See
4393 // https://github.com/google/sanitizers/issues/321.
4394 char *res = REAL(ether_ntoa_r)(addr, buf);
4395 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4398 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4399 __sanitizer_ether_addr *addr) {
4401 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4402 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4403 // FIXME: under ASan the call below may write to freed memory and corrupt
4404 // its metadata. See
4405 // https://github.com/google/sanitizers/issues/321.
4406 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4407 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4410 #define INIT_ETHER_R \
4411 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4412 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4414 #define INIT_ETHER_R
4417 #if SANITIZER_INTERCEPT_SHMCTL
4418 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4420 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4421 // FIXME: under ASan the call below may write to freed memory and corrupt
4422 // its metadata. See
4423 // https://github.com/google/sanitizers/issues/321.
4424 int res = REAL(shmctl)(shmid, cmd, buf);
4427 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4428 sz = sizeof(__sanitizer_shmid_ds);
4429 else if (cmd == shmctl_ipc_info)
4430 sz = struct_shminfo_sz;
4431 else if (cmd == shmctl_shm_info)
4432 sz = struct_shm_info_sz;
4433 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4437 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4442 #if SANITIZER_INTERCEPT_RANDOM_R
4443 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4445 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4446 // FIXME: under ASan the call below may write to freed memory and corrupt
4447 // its metadata. See
4448 // https://github.com/google/sanitizers/issues/321.
4449 int res = REAL(random_r)(buf, result);
4451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4454 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4456 #define INIT_RANDOM_R
4459 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4460 // its metadata. See
4461 // https://github.com/google/sanitizers/issues/321.
4462 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4463 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4464 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4465 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4466 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4467 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4468 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4469 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4470 INTERCEPTOR(int, fn, void *attr, void *r) { \
4472 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4473 int res = REAL(fn)(attr, r); \
4474 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4477 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4478 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4479 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4480 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4481 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4482 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4483 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4484 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4485 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4486 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4489 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4490 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4491 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4492 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4493 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4494 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4496 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4497 // FIXME: under ASan the call below may write to freed memory and corrupt
4498 // its metadata. See
4499 // https://github.com/google/sanitizers/issues/321.
4500 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4502 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4503 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4508 // We may need to call the real pthread_attr_getstack from the run-time
4509 // in sanitizer_common, but we don't want to include the interception headers
4510 // there. So, just define this function here.
4511 namespace __sanitizer {
4513 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4514 return REAL(pthread_attr_getstack)(attr, addr, size);
4517 } // namespace __sanitizer
4519 #define INIT_PTHREAD_ATTR_GET \
4520 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4521 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4522 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4523 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4524 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4526 #define INIT_PTHREAD_ATTR_GET
4529 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4530 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4531 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4533 #define INIT_PTHREAD_ATTR_GET_SCHED \
4534 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4535 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4537 #define INIT_PTHREAD_ATTR_GET_SCHED
4540 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4541 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4543 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4544 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4546 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4549 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4550 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4553 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4555 // FIXME: under ASan the call below may write to freed memory and corrupt
4556 // its metadata. See
4557 // https://github.com/google/sanitizers/issues/321.
4558 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4559 if (!res && cpusetsize && cpuset)
4560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4564 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4565 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4567 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4570 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4571 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4572 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4573 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4575 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4578 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4579 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4580 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4581 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4583 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4586 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4587 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4588 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4589 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4591 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4594 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4595 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4596 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4597 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4599 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4602 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4603 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4604 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4605 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4607 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4610 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4611 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4612 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4613 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4615 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4618 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4619 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4620 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4621 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4623 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4626 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4627 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4628 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4629 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4631 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4634 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4635 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4636 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4637 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4639 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4642 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4643 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4644 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4645 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4647 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4650 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4651 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4652 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4653 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4655 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4658 #if SANITIZER_INTERCEPT_TMPNAM
4659 INTERCEPTOR(char *, tmpnam, char *s) {
4661 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4662 char *res = REAL(tmpnam)(s);
4665 // FIXME: under ASan the call below may write to freed memory and corrupt
4666 // its metadata. See
4667 // https://github.com/google/sanitizers/issues/321.
4668 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4670 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4674 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4679 #if SANITIZER_INTERCEPT_TMPNAM_R
4680 INTERCEPTOR(char *, tmpnam_r, char *s) {
4682 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4683 // FIXME: under ASan the call below may write to freed memory and corrupt
4684 // its metadata. See
4685 // https://github.com/google/sanitizers/issues/321.
4686 char *res = REAL(tmpnam_r)(s);
4687 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4690 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4692 #define INIT_TMPNAM_R
4695 #if SANITIZER_INTERCEPT_TTYNAME_R
4696 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4698 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4699 int res = REAL(ttyname_r)(fd, name, namesize);
4701 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4704 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4706 #define INIT_TTYNAME_R
4709 #if SANITIZER_INTERCEPT_TEMPNAM
4710 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4712 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4713 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4714 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4715 char *res = REAL(tempnam)(dir, pfx);
4716 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4719 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4721 #define INIT_TEMPNAM
4724 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4725 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4727 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4728 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4729 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4730 return REAL(pthread_setname_np)(thread, name);
4732 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4733 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4734 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4736 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4737 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4738 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4739 internal_snprintf(newname, sizeof(newname), name, arg);
4740 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4741 return REAL(pthread_setname_np)(thread, name, arg);
4743 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4745 #define INIT_PTHREAD_SETNAME_NP
4748 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4749 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4751 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4752 int res = REAL(pthread_getname_np)(thread, name, len);
4754 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4757 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4759 #define INIT_PTHREAD_GETNAME_NP
4762 #if SANITIZER_INTERCEPT_SINCOS
4763 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4765 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4766 // FIXME: under ASan the call below may write to freed memory and corrupt
4767 // its metadata. See
4768 // https://github.com/google/sanitizers/issues/321.
4769 REAL(sincos)(x, sin, cos);
4770 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4771 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4773 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4775 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4776 // FIXME: under ASan the call below may write to freed memory and corrupt
4777 // its metadata. See
4778 // https://github.com/google/sanitizers/issues/321.
4779 REAL(sincosf)(x, sin, cos);
4780 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4781 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4783 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4785 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4786 // FIXME: under ASan the call below may write to freed memory and corrupt
4787 // its metadata. See
4788 // https://github.com/google/sanitizers/issues/321.
4789 REAL(sincosl)(x, sin, cos);
4790 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4791 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4793 #define INIT_SINCOS \
4794 COMMON_INTERCEPT_FUNCTION(sincos); \
4795 COMMON_INTERCEPT_FUNCTION(sincosf); \
4796 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4801 #if SANITIZER_INTERCEPT_REMQUO
4802 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4804 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4805 // FIXME: under ASan the call below may write to freed memory and corrupt
4806 // its metadata. See
4807 // https://github.com/google/sanitizers/issues/321.
4808 double res = REAL(remquo)(x, y, quo);
4809 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4812 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4814 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4815 // FIXME: under ASan the call below may write to freed memory and corrupt
4816 // its metadata. See
4817 // https://github.com/google/sanitizers/issues/321.
4818 float res = REAL(remquof)(x, y, quo);
4819 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4822 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4824 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4825 // FIXME: under ASan the call below may write to freed memory and corrupt
4826 // its metadata. See
4827 // https://github.com/google/sanitizers/issues/321.
4828 long double res = REAL(remquol)(x, y, quo);
4829 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4832 #define INIT_REMQUO \
4833 COMMON_INTERCEPT_FUNCTION(remquo); \
4834 COMMON_INTERCEPT_FUNCTION(remquof); \
4835 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4840 #if SANITIZER_INTERCEPT_LGAMMA
4842 INTERCEPTOR(double, lgamma, double x) {
4844 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4845 double res = REAL(lgamma)(x);
4846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4849 INTERCEPTOR(float, lgammaf, float x) {
4851 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4852 float res = REAL(lgammaf)(x);
4853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4856 INTERCEPTOR(long double, lgammal, long double x) {
4858 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4859 long double res = REAL(lgammal)(x);
4860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4863 #define INIT_LGAMMA \
4864 COMMON_INTERCEPT_FUNCTION(lgamma); \
4865 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4866 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4871 #if SANITIZER_INTERCEPT_LGAMMA_R
4872 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4874 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4875 // FIXME: under ASan the call below may write to freed memory and corrupt
4876 // its metadata. See
4877 // https://github.com/google/sanitizers/issues/321.
4878 double res = REAL(lgamma_r)(x, signp);
4879 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4882 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4884 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4885 // FIXME: under ASan the call below may write to freed memory and corrupt
4886 // its metadata. See
4887 // https://github.com/google/sanitizers/issues/321.
4888 float res = REAL(lgammaf_r)(x, signp);
4889 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4892 #define INIT_LGAMMA_R \
4893 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4894 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4896 #define INIT_LGAMMA_R
4899 #if SANITIZER_INTERCEPT_LGAMMAL_R
4900 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4902 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4903 // FIXME: under ASan the call below may write to freed memory and corrupt
4904 // its metadata. See
4905 // https://github.com/google/sanitizers/issues/321.
4906 long double res = REAL(lgammal_r)(x, signp);
4907 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4910 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4912 #define INIT_LGAMMAL_R
4915 #if SANITIZER_INTERCEPT_DRAND48_R
4916 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4918 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4919 // FIXME: under ASan the call below may write to freed memory and corrupt
4920 // its metadata. See
4921 // https://github.com/google/sanitizers/issues/321.
4922 int res = REAL(drand48_r)(buffer, result);
4923 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4926 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4928 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
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 int res = REAL(lrand48_r)(buffer, result);
4933 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4936 #define INIT_DRAND48_R \
4937 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4938 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4940 #define INIT_DRAND48_R
4943 #if SANITIZER_INTERCEPT_RAND_R
4944 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4946 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4947 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4948 return REAL(rand_r)(seedp);
4950 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4955 #if SANITIZER_INTERCEPT_GETLINE
4956 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4958 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4959 // FIXME: under ASan the call below may write to freed memory and corrupt
4960 // its metadata. See
4961 // https://github.com/google/sanitizers/issues/321.
4962 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4964 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4965 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4971 // FIXME: under ASan the call below may write to freed memory and corrupt its
4973 // https://github.com/google/sanitizers/issues/321.
4974 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4977 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4978 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4980 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4982 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4987 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4989 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4991 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4992 // with its own body.
4993 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4995 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4997 #define INIT_GETLINE \
4998 COMMON_INTERCEPT_FUNCTION(getline); \
4999 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5000 COMMON_INTERCEPT_FUNCTION(getdelim);
5002 #define INIT_GETLINE
5005 #if SANITIZER_INTERCEPT_ICONV
5006 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5007 char **outbuf, SIZE_T *outbytesleft) {
5009 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5012 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5013 if (inbuf && inbytesleft)
5014 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5016 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5017 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5018 // FIXME: under ASan the call below may write to freed memory and corrupt
5019 // its metadata. See
5020 // https://github.com/google/sanitizers/issues/321.
5021 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5022 if (outbuf && *outbuf > outbuf_orig) {
5023 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5024 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5028 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5033 #if SANITIZER_INTERCEPT_TIMES
5034 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5036 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
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 __sanitizer_clock_t res = REAL(times)(tms);
5041 if (res != (__sanitizer_clock_t)-1 && tms)
5042 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5045 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5050 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5052 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5053 // If you see any crashes around this functions, there are 2 known issues with
5054 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5055 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5056 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5057 // to access thread local variables (it should not happen normally,
5058 // because sanitizers use initial-exec tls model).
5059 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5061 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5062 void *res = REAL(__tls_get_addr)(arg);
5063 uptr tls_begin, tls_end;
5064 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5065 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5067 // New DTLS block has been allocated.
5068 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5073 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5074 // mostly the same semantics as __tls_get_addr, but its presence enables
5075 // some optimizations in linker (which are safe to ignore here).
5076 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5077 visibility("default")))
5078 void *__tls_get_addr_opt(void *arg);
5080 #else // SANITIZER_S390
5081 // On s390, we have to intercept two functions here:
5082 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5083 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5084 // a proper pointer. It is used by dlsym for TLS symbols.
5085 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5086 // descriptor offset as an argument instead of a pointer. GOT address
5087 // is passed in r12, so it's necessary to write it in assembly. This is
5088 // the function used by the compiler.
5089 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5090 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5091 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5092 extern "C" uptr __tls_get_offset(void *arg);
5093 extern "C" uptr __interceptor___tls_get_offset(void *arg);
5094 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5096 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5097 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5098 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5099 void *ptr = reinterpret_cast<void *>(res + tp);
5100 uptr tls_begin, tls_end;
5101 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5102 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5104 // New DTLS block has been allocated.
5105 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5109 // We need a hidden symbol aliasing the above, so that we can jump
5110 // directly to it from the assembly below.
5111 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5112 visibility("hidden")))
5113 uptr __tls_get_addr_hidden(void *arg);
5114 // Now carefully intercept __tls_get_offset.
5117 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5118 // exports our symbol.
5119 ".weak __tls_get_offset\n"
5120 ".type __tls_get_offset, @function\n"
5121 "__tls_get_offset:\n"
5122 ".global __interceptor___tls_get_offset\n"
5123 ".type __interceptor___tls_get_offset, @function\n"
5124 "__interceptor___tls_get_offset:\n"
5126 "la %r2, 0(%r2,%r12)\n"
5127 "jg __tls_get_addr_hidden\n"
5130 "0: la %r2,0(%r2,%r12)\n"
5131 "l %r4,1f-0b(%r3)\n"
5133 "1: .long __tls_get_addr_hidden - 0b\n"
5135 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5136 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5137 ".type __tls_get_offset_wrapper, @function\n"
5138 "__tls_get_offset_wrapper:\n"
5145 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5147 #endif // SANITIZER_S390
5149 #define INIT_TLS_GET_ADDR
5152 #if SANITIZER_INTERCEPT_LISTXATTR
5153 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5155 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5156 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5157 // FIXME: under ASan the call below may write to freed memory and corrupt
5158 // its metadata. See
5159 // https://github.com/google/sanitizers/issues/321.
5160 SSIZE_T res = REAL(listxattr)(path, list, size);
5161 // Here and below, size == 0 is a special case where nothing is written to the
5162 // buffer, and res contains the desired buffer size.
5163 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5166 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5168 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5169 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5170 // FIXME: under ASan the call below may write to freed memory and corrupt
5171 // its metadata. See
5172 // https://github.com/google/sanitizers/issues/321.
5173 SSIZE_T res = REAL(llistxattr)(path, list, size);
5174 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5177 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5179 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5180 // FIXME: under ASan the call below may write to freed memory and corrupt
5181 // its metadata. See
5182 // https://github.com/google/sanitizers/issues/321.
5183 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5184 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5187 #define INIT_LISTXATTR \
5188 COMMON_INTERCEPT_FUNCTION(listxattr); \
5189 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5190 COMMON_INTERCEPT_FUNCTION(flistxattr);
5192 #define INIT_LISTXATTR
5195 #if SANITIZER_INTERCEPT_GETXATTR
5196 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5199 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5200 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5201 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5202 // FIXME: under ASan the call below may write to freed memory and corrupt
5203 // its metadata. See
5204 // https://github.com/google/sanitizers/issues/321.
5205 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5206 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5209 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5212 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5213 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5214 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5215 // FIXME: under ASan the call below may write to freed memory and corrupt
5216 // its metadata. See
5217 // https://github.com/google/sanitizers/issues/321.
5218 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5219 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5222 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5225 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5226 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5227 // FIXME: under ASan the call below may write to freed memory and corrupt
5228 // its metadata. See
5229 // https://github.com/google/sanitizers/issues/321.
5230 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5231 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5234 #define INIT_GETXATTR \
5235 COMMON_INTERCEPT_FUNCTION(getxattr); \
5236 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5237 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5239 #define INIT_GETXATTR
5242 #if SANITIZER_INTERCEPT_GETRESID
5243 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5245 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5246 // FIXME: under ASan the call below may write to freed memory and corrupt
5247 // its metadata. See
5248 // https://github.com/google/sanitizers/issues/321.
5249 int res = REAL(getresuid)(ruid, euid, suid);
5251 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5252 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5253 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5257 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5259 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5260 // FIXME: under ASan the call below may write to freed memory and corrupt
5261 // its metadata. See
5262 // https://github.com/google/sanitizers/issues/321.
5263 int res = REAL(getresgid)(rgid, egid, sgid);
5265 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5266 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5267 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5271 #define INIT_GETRESID \
5272 COMMON_INTERCEPT_FUNCTION(getresuid); \
5273 COMMON_INTERCEPT_FUNCTION(getresgid);
5275 #define INIT_GETRESID
5278 #if SANITIZER_INTERCEPT_GETIFADDRS
5279 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5280 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5281 // intercept it to poison the memory again.
5282 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5284 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5285 // FIXME: under ASan the call below may write to freed memory and corrupt
5286 // its metadata. See
5287 // https://github.com/google/sanitizers/issues/321.
5288 int res = REAL(getifaddrs)(ifap);
5289 if (res == 0 && ifap) {
5290 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5291 __sanitizer_ifaddrs *p = *ifap;
5293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5295 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5296 REAL(strlen)(p->ifa_name) + 1);
5298 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5300 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5301 // On Linux this is a union, but the other member also points to a
5302 // struct sockaddr, so the following is sufficient.
5304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5305 // FIXME(smatveev): Unpoison p->ifa_data as well.
5311 #define INIT_GETIFADDRS \
5312 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5314 #define INIT_GETIFADDRS
5317 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5318 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5320 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5321 // FIXME: under ASan the call below may write to freed memory and corrupt
5322 // its metadata. See
5323 // https://github.com/google/sanitizers/issues/321.
5324 char *res = REAL(if_indextoname)(ifindex, ifname);
5326 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5329 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5331 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5333 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5334 return REAL(if_nametoindex)(ifname);
5336 #define INIT_IF_INDEXTONAME \
5337 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5338 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5340 #define INIT_IF_INDEXTONAME
5343 #if SANITIZER_INTERCEPT_CAPGET
5344 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5346 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5348 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5349 // FIXME: under ASan the call below may write to freed memory and corrupt
5350 // its metadata. See
5351 // https://github.com/google/sanitizers/issues/321.
5352 int res = REAL(capget)(hdrp, datap);
5353 if (res == 0 && datap)
5354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5355 // We can also return -1 and write to hdrp->version if the version passed in
5356 // hdrp->version is unsupported. But that's not a trivial condition to check,
5357 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5360 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5362 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5364 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5366 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5367 return REAL(capset)(hdrp, datap);
5369 #define INIT_CAPGET \
5370 COMMON_INTERCEPT_FUNCTION(capget); \
5371 COMMON_INTERCEPT_FUNCTION(capset);
5376 #if SANITIZER_INTERCEPT_AEABI_MEM
5377 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5379 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5382 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5384 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5387 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5389 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5392 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5394 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5397 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5399 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5402 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5404 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5407 // Note the argument order.
5408 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5410 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5413 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5415 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5418 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5420 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5423 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5425 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5428 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5430 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5433 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5435 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5438 #define INIT_AEABI_MEM \
5439 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5440 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5441 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5442 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5443 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5444 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5445 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5446 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5447 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5448 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5449 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5450 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5452 #define INIT_AEABI_MEM
5453 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5455 #if SANITIZER_INTERCEPT___BZERO
5456 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5458 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5461 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5463 #define INIT___BZERO
5464 #endif // SANITIZER_INTERCEPT___BZERO
5466 #if SANITIZER_INTERCEPT_FTIME
5467 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5469 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5470 // FIXME: under ASan the call below may write to freed memory and corrupt
5471 // its metadata. See
5472 // https://github.com/google/sanitizers/issues/321.
5473 int res = REAL(ftime)(tp);
5475 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5478 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5481 #endif // SANITIZER_INTERCEPT_FTIME
5483 #if SANITIZER_INTERCEPT_XDR
5484 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5485 unsigned size, int op) {
5487 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5488 // FIXME: under ASan the call below may write to freed memory and corrupt
5489 // its metadata. See
5490 // https://github.com/google/sanitizers/issues/321.
5491 REAL(xdrmem_create)(xdrs, addr, size, op);
5492 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5493 if (op == __sanitizer_XDR_ENCODE) {
5494 // It's not obvious how much data individual xdr_ routines write.
5495 // Simply unpoison the entire target buffer in advance.
5496 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5500 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5502 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5503 // FIXME: under ASan the call below may write to freed memory and corrupt
5504 // its metadata. See
5505 // https://github.com/google/sanitizers/issues/321.
5506 REAL(xdrstdio_create)(xdrs, file, op);
5507 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5510 // FIXME: under ASan the call below may write to freed memory and corrupt
5511 // its metadata. See
5512 // https://github.com/google/sanitizers/issues/321.
5513 #define XDR_INTERCEPTOR(F, T) \
5514 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5516 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5517 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5518 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5519 int res = REAL(F)(xdrs, p); \
5520 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5521 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5525 XDR_INTERCEPTOR(xdr_short, short)
5526 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5527 XDR_INTERCEPTOR(xdr_int, int)
5528 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5529 XDR_INTERCEPTOR(xdr_long, long)
5530 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5531 XDR_INTERCEPTOR(xdr_hyper, long long)
5532 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5533 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5534 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5535 XDR_INTERCEPTOR(xdr_int8_t, u8)
5536 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5537 XDR_INTERCEPTOR(xdr_int16_t, u16)
5538 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5539 XDR_INTERCEPTOR(xdr_int32_t, u32)
5540 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5541 XDR_INTERCEPTOR(xdr_int64_t, u64)
5542 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5543 XDR_INTERCEPTOR(xdr_quad_t, long long)
5544 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5545 XDR_INTERCEPTOR(xdr_bool, bool)
5546 XDR_INTERCEPTOR(xdr_enum, int)
5547 XDR_INTERCEPTOR(xdr_char, char)
5548 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5549 XDR_INTERCEPTOR(xdr_float, float)
5550 XDR_INTERCEPTOR(xdr_double, double)
5552 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5553 // wrapstring, sizeof
5555 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5558 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5559 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5560 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5561 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5562 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5564 // FIXME: under ASan the call below may write to freed memory and corrupt
5565 // its metadata. See
5566 // https://github.com/google/sanitizers/issues/321.
5567 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5568 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5571 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5576 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5579 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5580 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5581 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5582 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5584 // FIXME: under ASan the call below may write to freed memory and corrupt
5585 // its metadata. See
5586 // https://github.com/google/sanitizers/issues/321.
5587 int res = REAL(xdr_string)(xdrs, p, maxsize);
5588 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5589 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5591 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5597 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5598 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5599 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5600 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5601 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5602 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5603 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5604 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5605 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5606 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5607 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5608 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5609 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5610 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5611 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5612 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5613 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5614 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5615 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5616 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5617 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5618 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5619 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5620 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5621 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5622 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5623 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5624 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5625 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5626 COMMON_INTERCEPT_FUNCTION(xdr_string);
5629 #endif // SANITIZER_INTERCEPT_XDR
5631 #if SANITIZER_INTERCEPT_TSEARCH
5632 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5633 int (*compar)(const void *, const void *)) {
5635 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5636 // FIXME: under ASan the call below may write to freed memory and corrupt
5637 // its metadata. See
5638 // https://github.com/google/sanitizers/issues/321.
5639 void *res = REAL(tsearch)(key, rootp, compar);
5640 if (res && *(void **)res == key)
5641 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5644 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5646 #define INIT_TSEARCH
5649 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5650 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5651 void unpoison_file(__sanitizer_FILE *fp) {
5652 #if SANITIZER_HAS_STRUCT_FILE
5653 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5654 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5655 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5656 fp->_IO_read_end - fp->_IO_read_base);
5657 #endif // SANITIZER_HAS_STRUCT_FILE
5661 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5662 // These guys are called when a .c source is built with -O2.
5663 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5665 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5666 int res = REAL(__uflow)(fp);
5670 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5672 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5673 int res = REAL(__underflow)(fp);
5677 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5679 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5680 int res = REAL(__overflow)(fp, ch);
5684 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5686 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5687 int res = REAL(__wuflow)(fp);
5691 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5693 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5694 int res = REAL(__wunderflow)(fp);
5698 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5700 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5701 int res = REAL(__woverflow)(fp, ch);
5705 #define INIT_LIBIO_INTERNALS \
5706 COMMON_INTERCEPT_FUNCTION(__uflow); \
5707 COMMON_INTERCEPT_FUNCTION(__underflow); \
5708 COMMON_INTERCEPT_FUNCTION(__overflow); \
5709 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5710 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5711 COMMON_INTERCEPT_FUNCTION(__woverflow);
5713 #define INIT_LIBIO_INTERNALS
5716 #if SANITIZER_INTERCEPT_FOPEN
5717 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5719 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5720 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5721 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5722 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5723 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5724 if (res) unpoison_file(res);
5727 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5729 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5730 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5731 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5732 if (res) unpoison_file(res);
5735 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5736 __sanitizer_FILE *fp) {
5738 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5739 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5740 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5741 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5742 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5743 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5744 if (res) unpoison_file(res);
5747 #define INIT_FOPEN \
5748 COMMON_INTERCEPT_FUNCTION(fopen); \
5749 COMMON_INTERCEPT_FUNCTION(fdopen); \
5750 COMMON_INTERCEPT_FUNCTION(freopen);
5755 #if SANITIZER_INTERCEPT_FOPEN64
5756 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5758 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5759 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5760 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5761 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5762 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5763 if (res) unpoison_file(res);
5766 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5767 __sanitizer_FILE *fp) {
5769 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5770 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5771 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5772 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5773 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5774 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5775 if (res) unpoison_file(res);
5778 #define INIT_FOPEN64 \
5779 COMMON_INTERCEPT_FUNCTION(fopen64); \
5780 COMMON_INTERCEPT_FUNCTION(freopen64);
5782 #define INIT_FOPEN64
5785 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5786 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5788 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5789 // FIXME: under ASan the call below may write to freed memory and corrupt
5790 // its metadata. See
5791 // https://github.com/google/sanitizers/issues/321.
5792 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5797 FileMetadata file = {ptr, sizeloc};
5798 SetInterceptorMetadata(res, file);
5802 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5805 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5806 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5809 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5811 FileMetadata file = {(char **)ptr, sizeloc};
5812 SetInterceptorMetadata(res, file);
5816 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5819 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5820 // FIXME: under ASan the call below may write to freed memory and corrupt
5821 // its metadata. See
5822 // https://github.com/google/sanitizers/issues/321.
5823 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5824 if (res) unpoison_file(res);
5827 #define INIT_OPEN_MEMSTREAM \
5828 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5829 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5830 COMMON_INTERCEPT_FUNCTION(fmemopen);
5832 #define INIT_OPEN_MEMSTREAM
5835 #if SANITIZER_INTERCEPT_OBSTACK
5836 static void initialize_obstack(__sanitizer_obstack *obstack) {
5837 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5839 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5840 sizeof(*obstack->chunk));
5843 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5844 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5845 void (*free_fn)(uptr arg, void *p)) {
5847 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5849 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5850 if (res) initialize_obstack(obstack);
5853 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5854 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5856 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5858 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5859 if (res) initialize_obstack(obstack);
5862 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5864 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5865 REAL(_obstack_newchunk)(obstack, length);
5867 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5868 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5870 #define INIT_OBSTACK \
5871 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5872 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5873 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5875 #define INIT_OBSTACK
5878 #if SANITIZER_INTERCEPT_FFLUSH
5879 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5881 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5882 int res = REAL(fflush)(fp);
5883 // FIXME: handle fp == NULL
5885 const FileMetadata *m = GetInterceptorMetadata(fp);
5886 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5890 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5895 #if SANITIZER_INTERCEPT_FCLOSE
5896 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5898 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5899 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5900 const FileMetadata *m = GetInterceptorMetadata(fp);
5901 int res = REAL(fclose)(fp);
5903 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5904 DeleteInterceptorMetadata(fp);
5908 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5913 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5914 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5916 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5917 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5918 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5919 void *res = REAL(dlopen)(filename, flag);
5920 Symbolizer::GetOrInit()->InvalidateModuleList();
5921 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5925 INTERCEPTOR(int, dlclose, void *handle) {
5927 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5928 int res = REAL(dlclose)(handle);
5929 Symbolizer::GetOrInit()->InvalidateModuleList();
5930 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5933 #define INIT_DLOPEN_DLCLOSE \
5934 COMMON_INTERCEPT_FUNCTION(dlopen); \
5935 COMMON_INTERCEPT_FUNCTION(dlclose);
5937 #define INIT_DLOPEN_DLCLOSE
5940 #if SANITIZER_INTERCEPT_GETPASS
5941 INTERCEPTOR(char *, getpass, const char *prompt) {
5943 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5945 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5946 char *res = REAL(getpass)(prompt);
5947 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5951 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5953 #define INIT_GETPASS
5956 #if SANITIZER_INTERCEPT_TIMERFD
5957 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5960 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5962 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5963 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5964 if (res != -1 && old_value)
5965 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5969 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5971 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5972 int res = REAL(timerfd_gettime)(fd, curr_value);
5973 if (res != -1 && curr_value)
5974 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5977 #define INIT_TIMERFD \
5978 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5979 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5981 #define INIT_TIMERFD
5984 #if SANITIZER_INTERCEPT_MLOCKX
5985 // Linux kernel has a bug that leads to kernel deadlock if a process
5986 // maps TBs of memory and then calls mlock().
5987 static void MlockIsUnsupported() {
5988 static atomic_uint8_t printed;
5989 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5991 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5995 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5996 MlockIsUnsupported();
6000 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6001 MlockIsUnsupported();
6005 INTERCEPTOR(int, mlockall, int flags) {
6006 MlockIsUnsupported();
6010 INTERCEPTOR(int, munlockall, void) {
6011 MlockIsUnsupported();
6015 #define INIT_MLOCKX \
6016 COMMON_INTERCEPT_FUNCTION(mlock); \
6017 COMMON_INTERCEPT_FUNCTION(munlock); \
6018 COMMON_INTERCEPT_FUNCTION(mlockall); \
6019 COMMON_INTERCEPT_FUNCTION(munlockall);
6023 #endif // SANITIZER_INTERCEPT_MLOCKX
6025 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6026 struct WrappedCookie {
6028 __sanitizer_cookie_io_functions_t real_io_funcs;
6031 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6032 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6033 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6034 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6035 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6038 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6039 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6040 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6041 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6042 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6045 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6046 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6047 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6048 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6049 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6050 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6054 static int wrapped_close(void *cookie) {
6055 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6056 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6057 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6058 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6059 InternalFree(wrapped_cookie);
6063 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6064 __sanitizer_cookie_io_functions_t io_funcs) {
6066 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6067 WrappedCookie *wrapped_cookie =
6068 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6069 wrapped_cookie->real_cookie = cookie;
6070 wrapped_cookie->real_io_funcs = io_funcs;
6071 __sanitizer_FILE *res =
6072 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6073 wrapped_seek, wrapped_close});
6077 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6079 #define INIT_FOPENCOOKIE
6080 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6082 #if SANITIZER_INTERCEPT_SEM
6083 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6085 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6086 // Workaround a bug in glibc's "old" semaphore implementation by
6087 // zero-initializing the sem_t contents. This has to be done here because
6088 // interceptors bind to the lowest symbols version by default, hitting the
6089 // buggy code path while the non-sanitized build of the same code works fine.
6090 REAL(memset)(s, 0, sizeof(*s));
6091 int res = REAL(sem_init)(s, pshared, value);
6095 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6097 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6098 int res = REAL(sem_destroy)(s);
6102 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6104 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6105 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6107 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6112 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6114 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6115 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6117 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6122 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6124 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6125 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6126 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6128 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6133 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6135 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6136 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6137 int res = REAL(sem_post)(s);
6141 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6143 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6144 int res = REAL(sem_getvalue)(s, sval);
6146 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6147 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6152 COMMON_INTERCEPT_FUNCTION(sem_init); \
6153 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6154 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6155 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6156 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6157 COMMON_INTERCEPT_FUNCTION(sem_post); \
6158 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6161 #endif // SANITIZER_INTERCEPT_SEM
6163 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6164 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6166 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6167 int res = REAL(pthread_setcancelstate)(state, oldstate);
6168 if (res == 0 && oldstate != nullptr)
6169 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6173 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6175 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6176 int res = REAL(pthread_setcanceltype)(type, oldtype);
6177 if (res == 0 && oldtype != nullptr)
6178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6181 #define INIT_PTHREAD_SETCANCEL \
6182 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6183 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6185 #define INIT_PTHREAD_SETCANCEL
6188 #if SANITIZER_INTERCEPT_MINCORE
6189 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6191 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6192 int res = REAL(mincore)(addr, length, vec);
6194 uptr page_size = GetPageSizeCached();
6195 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6196 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6200 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6202 #define INIT_MINCORE
6205 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6206 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6207 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6210 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6211 remote_iov, riovcnt, flags);
6212 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6215 write_iovec(ctx, local_iov, liovcnt, res);
6219 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6220 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6223 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6224 remote_iov, riovcnt, flags);
6225 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6228 read_iovec(ctx, local_iov, liovcnt, res);
6231 #define INIT_PROCESS_VM_READV \
6232 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6233 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6235 #define INIT_PROCESS_VM_READV
6238 #if SANITIZER_INTERCEPT_CTERMID
6239 INTERCEPTOR(char *, ctermid, char *s) {
6241 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6242 char *res = REAL(ctermid)(s);
6244 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6248 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6250 #define INIT_CTERMID
6253 #if SANITIZER_INTERCEPT_CTERMID_R
6254 INTERCEPTOR(char *, ctermid_r, char *s) {
6256 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6257 char *res = REAL(ctermid_r)(s);
6259 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6263 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6265 #define INIT_CTERMID_R
6268 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6269 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6271 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6272 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6273 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6275 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6277 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6281 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6282 void *srcaddr, int *addrlen) {
6284 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6286 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6288 if (srcaddr) srcaddr_sz = *addrlen;
6289 (void)srcaddr_sz; // prevent "set but not used" warning
6290 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6294 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6295 Min((SIZE_T)*addrlen, srcaddr_sz));
6299 #define INIT_RECV_RECVFROM \
6300 COMMON_INTERCEPT_FUNCTION(recv); \
6301 COMMON_INTERCEPT_FUNCTION(recvfrom);
6303 #define INIT_RECV_RECVFROM
6306 #if SANITIZER_INTERCEPT_SEND_SENDTO
6307 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6309 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6311 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6312 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6314 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6315 if (common_flags()->intercept_send && res > 0)
6316 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6320 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6321 void *dstaddr, int addrlen) {
6323 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6325 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6326 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6328 // Can't check dstaddr as it may have uninitialized padding at the end.
6329 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6330 if (common_flags()->intercept_send && res > 0)
6331 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6334 #define INIT_SEND_SENDTO \
6335 COMMON_INTERCEPT_FUNCTION(send); \
6336 COMMON_INTERCEPT_FUNCTION(sendto);
6338 #define INIT_SEND_SENDTO
6341 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6342 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6344 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6345 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6346 int res = REAL(eventfd_read)(fd, value);
6348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6349 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6353 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6355 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6357 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6358 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6360 int res = REAL(eventfd_write)(fd, value);
6363 #define INIT_EVENTFD_READ_WRITE \
6364 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6365 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6367 #define INIT_EVENTFD_READ_WRITE
6370 #if SANITIZER_INTERCEPT_STAT
6371 INTERCEPTOR(int, stat, const char *path, void *buf) {
6373 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6374 if (common_flags()->intercept_stat)
6375 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6376 int res = REAL(stat)(path, buf);
6378 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6381 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6386 #if SANITIZER_INTERCEPT_LSTAT
6387 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6389 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6390 if (common_flags()->intercept_stat)
6391 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6392 int res = REAL(lstat)(path, buf);
6394 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6397 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6402 #if SANITIZER_INTERCEPT___XSTAT
6403 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6405 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6406 if (common_flags()->intercept_stat)
6407 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6408 int res = REAL(__xstat)(version, path, buf);
6410 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6413 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6415 #define INIT___XSTAT
6418 #if SANITIZER_INTERCEPT___XSTAT64
6419 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6421 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6422 if (common_flags()->intercept_stat)
6423 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6424 int res = REAL(__xstat64)(version, path, buf);
6426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6429 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6431 #define INIT___XSTAT64
6434 #if SANITIZER_INTERCEPT___LXSTAT
6435 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6437 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6438 if (common_flags()->intercept_stat)
6439 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6440 int res = REAL(__lxstat)(version, path, buf);
6442 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6445 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6447 #define INIT___LXSTAT
6450 #if SANITIZER_INTERCEPT___LXSTAT64
6451 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6453 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6454 if (common_flags()->intercept_stat)
6455 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6456 int res = REAL(__lxstat64)(version, path, buf);
6458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6461 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6463 #define INIT___LXSTAT64
6466 // FIXME: add other *stat interceptor
6468 #if SANITIZER_INTERCEPT_UTMP
6469 INTERCEPTOR(void *, getutent, int dummy) {
6471 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6472 void *res = REAL(getutent)(dummy);
6474 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6477 INTERCEPTOR(void *, getutid, void *ut) {
6479 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6480 void *res = REAL(getutid)(ut);
6482 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6485 INTERCEPTOR(void *, getutline, void *ut) {
6487 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6488 void *res = REAL(getutline)(ut);
6490 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6494 COMMON_INTERCEPT_FUNCTION(getutent); \
6495 COMMON_INTERCEPT_FUNCTION(getutid); \
6496 COMMON_INTERCEPT_FUNCTION(getutline);
6501 #if SANITIZER_INTERCEPT_UTMPX
6502 INTERCEPTOR(void *, getutxent, int dummy) {
6504 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6505 void *res = REAL(getutxent)(dummy);
6507 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6510 INTERCEPTOR(void *, getutxid, void *ut) {
6512 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6513 void *res = REAL(getutxid)(ut);
6515 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6518 INTERCEPTOR(void *, getutxline, void *ut) {
6520 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6521 void *res = REAL(getutxline)(ut);
6523 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6526 #define INIT_UTMPX \
6527 COMMON_INTERCEPT_FUNCTION(getutxent); \
6528 COMMON_INTERCEPT_FUNCTION(getutxid); \
6529 COMMON_INTERCEPT_FUNCTION(getutxline);
6534 #if SANITIZER_INTERCEPT_GETLOADAVG
6535 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6537 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6538 int res = REAL(getloadavg)(loadavg, nelem);
6540 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6543 #define INIT_GETLOADAVG \
6544 COMMON_INTERCEPT_FUNCTION(getloadavg);
6546 #define INIT_GETLOADAVG
6549 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6550 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6554 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6558 INTERCEPTOR(int, mprobe, void *ptr) {
6563 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6565 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6566 SIZE_T res = REAL(wcslen)(s);
6567 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6571 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6573 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6574 SIZE_T res = REAL(wcsnlen)(s, n);
6575 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6578 #define INIT_WCSLEN \
6579 COMMON_INTERCEPT_FUNCTION(wcslen); \
6580 COMMON_INTERCEPT_FUNCTION(wcsnlen);
6582 #if SANITIZER_INTERCEPT_WCSCAT
6583 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6585 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6586 SIZE_T src_size = REAL(wcslen)(src);
6587 SIZE_T dst_size = REAL(wcslen)(dst);
6588 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6589 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6590 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6591 (src_size + 1) * sizeof(wchar_t));
6592 return REAL(wcscat)(dst, src); // NOLINT
6595 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6597 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6598 SIZE_T src_size = REAL(wcsnlen)(src, n);
6599 SIZE_T dst_size = REAL(wcslen)(dst);
6600 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6601 Min(src_size + 1, n) * sizeof(wchar_t));
6602 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6603 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6604 (src_size + 1) * sizeof(wchar_t));
6605 return REAL(wcsncat)(dst, src, n); // NOLINT
6607 #define INIT_WCSCAT \
6608 COMMON_INTERCEPT_FUNCTION(wcscat); \
6609 COMMON_INTERCEPT_FUNCTION(wcsncat);
6614 #if SANITIZER_INTERCEPT_STRXFRM
6615 static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6617 static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6619 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
6622 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6623 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
6624 sizeof(*src) * (RealStrLen(src) + 1)); \
6625 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
6627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6631 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6632 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6635 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6637 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6640 #define INIT_STRXFRM \
6641 COMMON_INTERCEPT_FUNCTION(strxfrm); \
6642 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6644 #define INIT_STRXFRM
6647 #if SANITIZER_INTERCEPT___STRXFRM_L
6648 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6650 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6653 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6655 #define INIT___STRXFRM_L
6658 #if SANITIZER_INTERCEPT_WCSXFRM
6659 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6660 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6663 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6665 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6668 #define INIT_WCSXFRM \
6669 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6670 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6672 #define INIT_WCSXFRM
6675 #if SANITIZER_INTERCEPT___WCSXFRM_L
6676 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6678 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6681 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6683 #define INIT___WCSXFRM_L
6686 #if SANITIZER_INTERCEPT_ACCT
6687 INTERCEPTOR(int, acct, const char *file) {
6689 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6691 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6692 return REAL(acct)(file);
6694 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6699 #if SANITIZER_INTERCEPT_USER_FROM_UID
6700 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6703 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6704 user = REAL(user_from_uid)(uid, nouser);
6706 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6709 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6711 #define INIT_USER_FROM_UID
6714 #if SANITIZER_INTERCEPT_UID_FROM_USER
6715 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6718 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6720 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6721 res = REAL(uid_from_user)(name, uid);
6723 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6726 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6728 #define INIT_UID_FROM_USER
6731 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
6732 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6735 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6736 group = REAL(group_from_gid)(gid, nogroup);
6738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6741 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6743 #define INIT_GROUP_FROM_GID
6746 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
6747 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6750 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6752 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6753 res = REAL(gid_from_group)(group, gid);
6755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6758 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6760 #define INIT_GID_FROM_GROUP
6763 #if SANITIZER_INTERCEPT_ACCESS
6764 INTERCEPTOR(int, access, const char *path, int mode) {
6766 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6768 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6769 return REAL(access)(path, mode);
6771 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6776 #if SANITIZER_INTERCEPT_FACCESSAT
6777 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6779 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6781 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6782 return REAL(faccessat)(fd, path, mode, flags);
6784 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6786 #define INIT_FACCESSAT
6789 #if SANITIZER_INTERCEPT_GETGROUPLIST
6790 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6794 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6796 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6798 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6799 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6800 if (!res && groups && ngroups) {
6801 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6802 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6807 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6809 #define INIT_GETGROUPLIST
6812 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6813 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6814 int maxgrp, int *ngroups) {
6817 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6820 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6821 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6822 if (!res && groups && ngroups) {
6823 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6829 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6831 #define INIT_GETGROUPMEMBERSHIP
6834 #if SANITIZER_INTERCEPT_READLINK
6835 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6837 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6838 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6839 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6841 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6845 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6847 #define INIT_READLINK
6850 #if SANITIZER_INTERCEPT_READLINKAT
6851 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
6854 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
6855 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6856 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
6858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6862 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
6864 #define INIT_READLINKAT
6867 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
6868 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
6869 struct file_handle *handle, int *mount_id, int flags) {
6871 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
6873 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
6875 __sanitizer_file_handle *sanitizer_handle =
6876 reinterpret_cast<__sanitizer_file_handle*>(handle);
6877 COMMON_INTERCEPTOR_READ_RANGE(
6878 ctx, &sanitizer_handle->handle_bytes,
6879 sizeof(sanitizer_handle->handle_bytes));
6881 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
6883 COMMON_INTERCEPTOR_WRITE_RANGE(
6884 ctx, &sanitizer_handle->handle_bytes,
6885 sizeof(sanitizer_handle->handle_bytes));
6886 COMMON_INTERCEPTOR_WRITE_RANGE(
6887 ctx, &sanitizer_handle->handle_type,
6888 sizeof(sanitizer_handle->handle_type));
6889 COMMON_INTERCEPTOR_WRITE_RANGE(
6890 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
6896 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
6898 #define INIT_NAME_TO_HANDLE_AT
6901 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
6902 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
6905 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
6907 __sanitizer_file_handle *sanitizer_handle =
6908 reinterpret_cast<__sanitizer_file_handle*>(handle);
6909 COMMON_INTERCEPTOR_READ_RANGE(
6910 ctx, &sanitizer_handle->handle_bytes,
6911 sizeof(sanitizer_handle->handle_bytes));
6912 COMMON_INTERCEPTOR_READ_RANGE(
6913 ctx, &sanitizer_handle->handle_type,
6914 sizeof(sanitizer_handle->handle_type));
6915 COMMON_INTERCEPTOR_READ_RANGE(
6916 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6918 return REAL(open_by_handle_at)(mount_fd, handle, flags);
6921 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
6923 #define INIT_OPEN_BY_HANDLE_AT
6926 #if SANITIZER_INTERCEPT_STRLCPY
6927 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
6930 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
6932 // Keep strnlen as macro argument, as macro may ignore it.
6933 COMMON_INTERCEPTOR_READ_STRING(
6934 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
6936 res = REAL(strlcpy)(dst, src, size);
6937 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
6941 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
6944 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
6945 // src is checked in the strlcpy() interceptor
6947 len = internal_strnlen(dst, size);
6948 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
6950 // Reuse the rest of the code in the strlcpy() interceptor
6951 return WRAP(strlcpy)(dst + len, src, size - len) + len;
6953 #define INIT_STRLCPY \
6954 COMMON_INTERCEPT_FUNCTION(strlcpy); \
6955 COMMON_INTERCEPT_FUNCTION(strlcat);
6957 #define INIT_STRLCPY
6960 #if SANITIZER_INTERCEPT_MMAP
6961 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
6964 if (common_flags()->detect_write_exec)
6965 ReportMmapWriteExec(prot);
6966 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
6967 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
6968 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
6969 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
6972 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
6974 if (common_flags()->detect_write_exec)
6975 ReportMmapWriteExec(prot);
6976 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
6977 return (int)internal_mprotect(addr, sz, prot);
6978 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
6979 MprotectMallocZones(addr, prot);
6980 return REAL(mprotect)(addr, sz, prot);
6983 COMMON_INTERCEPT_FUNCTION(mmap); \
6984 COMMON_INTERCEPT_FUNCTION(mprotect);
6989 #if SANITIZER_INTERCEPT_MMAP64
6990 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
6993 if (common_flags()->detect_write_exec)
6994 ReportMmapWriteExec(prot);
6995 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
6996 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
6997 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
6998 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7000 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7005 #if SANITIZER_INTERCEPT_DEVNAME
7006 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7009 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7010 name = REAL(devname)(dev, type);
7012 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7015 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7017 #define INIT_DEVNAME
7020 #if SANITIZER_INTERCEPT_DEVNAME_R
7021 INTERCEPTOR(int, devname_r, u64 dev, u32 type, char *path, uptr len) {
7024 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7025 res = REAL(devname_r)(dev, type, path, len);
7027 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7030 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7032 #define INIT_DEVNAME_R
7035 #if SANITIZER_INTERCEPT_FGETLN
7036 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7038 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7039 char *str = REAL(fgetln)(stream, len);
7041 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7042 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7046 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7051 #if SANITIZER_INTERCEPT_STRMODE
7052 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7054 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7055 REAL(strmode)(mode, bp);
7057 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7059 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7061 #define INIT_STRMODE
7064 #if SANITIZER_INTERCEPT_TTYENT
7065 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7067 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7068 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7073 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7075 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7077 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7078 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7080 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7083 INTERCEPTOR(int, setttyentpath, char *path) {
7085 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7087 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7088 return REAL(setttyentpath)(path);
7090 #define INIT_TTYENT \
7091 COMMON_INTERCEPT_FUNCTION(getttyent); \
7092 COMMON_INTERCEPT_FUNCTION(getttynam); \
7093 COMMON_INTERCEPT_FUNCTION(setttyentpath)
7098 #if SANITIZER_INTERCEPT_PROTOENT
7099 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7101 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7102 struct __sanitizer_protoent *p = REAL(getprotoent)();
7104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7108 SIZE_T pp_size = 1; // One handles the trailing \0
7110 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7111 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7113 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7114 pp_size * sizeof(char **));
7119 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7121 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7123 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7124 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7126 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7128 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7130 SIZE_T pp_size = 1; // One handles the trailing \0
7132 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7133 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7135 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7136 pp_size * sizeof(char **));
7141 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7143 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7144 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7146 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7148 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7150 SIZE_T pp_size = 1; // One handles the trailing \0
7152 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7156 pp_size * sizeof(char **));
7160 #define INIT_PROTOENT \
7161 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7162 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7163 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7165 #define INIT_PROTOENT
7168 #if SANITIZER_INTERCEPT_NETENT
7169 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7171 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7172 struct __sanitizer_netent *n = REAL(getnetent)();
7174 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7178 SIZE_T nn_size = 1; // One handles the trailing \0
7180 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7181 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7184 nn_size * sizeof(char **));
7189 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7191 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7193 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7194 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7196 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7200 SIZE_T nn_size = 1; // One handles the trailing \0
7202 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7205 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7206 nn_size * sizeof(char **));
7211 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7213 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7214 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7218 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7220 SIZE_T nn_size = 1; // One handles the trailing \0
7222 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7225 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7226 nn_size * sizeof(char **));
7230 #define INIT_NETENT \
7231 COMMON_INTERCEPT_FUNCTION(getnetent); \
7232 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7233 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7238 static void InitializeCommonInterceptors() {
7239 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
7240 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
7288 INIT_LOCALTIME_AND_FRIENDS;
7297 INIT_GETPWNAM_AND_FRIENDS;
7298 INIT_GETPWNAM_R_AND_FRIENDS;
7311 INIT_PTHREAD_GETSCHEDPARAM;
7316 INIT_GETHOSTBYNAME2;
7317 INIT_GETHOSTBYNAME_R;
7318 INIT_GETHOSTBYNAME2_R;
7319 INIT_GETHOSTBYADDR_R;
7339 INIT_GET_CURRENT_DIR_NAME;
7348 INIT_CANONICALIZE_FILE_NAME;
7350 INIT_SCHED_GETAFFINITY;
7351 INIT_SCHED_GETPARAM;
7354 INIT_XPG_STRERROR_R;
7369 INIT_PTHREAD_MUTEX_LOCK;
7370 INIT_PTHREAD_MUTEX_UNLOCK;
7371 INIT___PTHREAD_MUTEX_LOCK;
7372 INIT___PTHREAD_MUTEX_UNLOCK;
7373 INIT___LIBC_MUTEX_LOCK;
7374 INIT___LIBC_MUTEX_UNLOCK;
7375 INIT___LIBC_THR_SETCANCELSTATE;
7383 INIT_ETHER_NTOA_ATON;
7388 INIT_PTHREAD_ATTR_GET;
7389 INIT_PTHREAD_ATTR_GET_SCHED;
7390 INIT_PTHREAD_ATTR_GETINHERITSCHED;
7391 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
7392 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
7393 INIT_PTHREAD_MUTEXATTR_GETTYPE;
7394 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
7395 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
7396 INIT_PTHREAD_MUTEXATTR_GETROBUST;
7397 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
7398 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
7399 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
7400 INIT_PTHREAD_CONDATTR_GETPSHARED;
7401 INIT_PTHREAD_CONDATTR_GETCLOCK;
7402 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
7407 INIT_PTHREAD_SETNAME_NP;
7408 INIT_PTHREAD_GETNAME_NP;
7424 INIT_IF_INDEXTONAME;
7431 INIT_LIBIO_INTERNALS;
7434 INIT_OPEN_MEMSTREAM;
7438 INIT_DLOPEN_DLCLOSE;
7444 INIT_PTHREAD_SETCANCEL;
7446 INIT_PROCESS_VM_READV;
7452 INIT_EVENTFD_READ_WRITE;
7458 // FIXME: add other *stat interceptors.
7469 INIT_GROUP_FROM_GID;
7470 INIT_GID_FROM_GROUP;
7474 INIT_GETGROUPMEMBERSHIP;
7477 INIT_NAME_TO_HANDLE_AT;
7478 INIT_OPEN_BY_HANDLE_AT;