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 //===----------------------------------------------------------------------===//
39 #include "interception/interception.h"
40 #include "sanitizer_addrhashmap.h"
41 #include "sanitizer_placement_new.h"
42 #include "sanitizer_platform_interceptors.h"
43 #include "sanitizer_tls_get_addr.h"
47 #if SANITIZER_INTERCEPTOR_HOOKS
48 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
49 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
50 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
55 #endif // SANITIZER_INTERCEPTOR_HOOKS
57 #if SANITIZER_WINDOWS && !defined(va_copy)
58 #define va_copy(dst, src) ((dst) = (src))
62 #define pthread_setname_np pthread_set_name_np
63 #define inet_aton __inet_aton
64 #define inet_pton __inet_pton
65 #define iconv __bsd_iconv
68 // Platform-specific options.
70 namespace __sanitizer {
71 bool PlatformHasDifferentMemcpyAndMemmove();
73 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
74 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
75 #elif SANITIZER_WINDOWS64
76 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
78 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
79 #endif // SANITIZER_MAC
81 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
82 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
85 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
86 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
89 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
90 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
93 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
94 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
97 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
98 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
101 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
102 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
105 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
106 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
109 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
110 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
113 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
114 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
117 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
118 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
121 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
122 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
125 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
126 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
129 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
130 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
133 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
134 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
135 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
138 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
139 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
142 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
143 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
144 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
146 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
147 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
148 CheckNoDeepBind(filename, flag);
151 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
152 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
155 #ifndef COMMON_INTERCEPTOR_ACQUIRE
156 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
159 #ifndef COMMON_INTERCEPTOR_RELEASE
160 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
163 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
164 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
167 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
168 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
171 #ifdef SANITIZER_NLDBL_VERSION
172 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
173 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
175 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
176 COMMON_INTERCEPT_FUNCTION(fn)
179 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
180 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
182 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
183 return internal_memset(dst, v, size); \
184 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
185 if (common_flags()->intercept_intrin) \
186 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
187 return REAL(memset)(dst, v, size); \
191 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
192 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
194 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
195 return internal_memmove(dst, src, size); \
196 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
197 if (common_flags()->intercept_intrin) { \
198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
199 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
201 return REAL(memmove)(dst, src, size); \
205 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
206 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
208 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
209 return internal_memmove(dst, src, size); \
211 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
212 if (common_flags()->intercept_intrin) { \
213 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
214 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
216 return REAL(memcpy)(dst, src, size); \
220 struct FileMetadata {
221 // For open_memstream().
226 struct CommonInterceptorMetadata {
236 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
238 static MetadataHashMap *interceptor_metadata_map;
241 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
242 const FileMetadata &file) {
243 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
245 h->type = CommonInterceptorMetadata::CIMT_FILE;
249 UNUSED static const FileMetadata *GetInterceptorMetadata(
250 __sanitizer_FILE *addr) {
251 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
256 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
263 UNUSED static void DeleteInterceptorMetadata(void *addr) {
264 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
267 #endif // SI_NOT_WINDOWS
269 #if SANITIZER_INTERCEPT_STRLEN
270 INTERCEPTOR(SIZE_T, strlen, const char *s) {
271 // Sometimes strlen is called prior to InitializeCommonInterceptors,
272 // in which case the REAL(strlen) typically used in
273 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
275 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
276 return internal_strlen(s);
278 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
279 SIZE_T result = REAL(strlen)(s);
280 if (common_flags()->intercept_strlen)
281 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
284 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
289 #if SANITIZER_INTERCEPT_STRNLEN
290 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
292 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
293 SIZE_T length = REAL(strnlen)(s, maxlen);
294 if (common_flags()->intercept_strlen)
295 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
298 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
303 #if SANITIZER_INTERCEPT_TEXTDOMAIN
304 INTERCEPTOR(char*, textdomain, const char *domainname) {
306 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
307 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
308 char *domain = REAL(textdomain)(domainname);
310 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
314 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
316 #define INIT_TEXTDOMAIN
319 #if SANITIZER_INTERCEPT_STRCMP
320 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
321 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
324 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
325 const char *s1, const char *s2, int result)
327 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
329 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
330 unsigned char c1, c2;
333 c1 = (unsigned char)s1[i];
334 c2 = (unsigned char)s2[i];
335 if (c1 != c2 || c1 == '\0') break;
337 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
338 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
339 int result = CharCmpX(c1, c2);
340 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
345 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
346 const char *s1, const char *s2, uptr n,
349 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
350 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
351 return internal_strncmp(s1, s2, size);
353 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
354 unsigned char c1 = 0, c2 = 0;
356 for (i = 0; i < size; i++) {
357 c1 = (unsigned char)s1[i];
358 c2 = (unsigned char)s2[i];
359 if (c1 != c2 || c1 == '\0') break;
363 if (common_flags()->strict_string_checks) {
364 for (; i1 < size && s1[i1]; i1++) {}
365 for (; i2 < size && s2[i2]; i2++) {}
367 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
368 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
369 int result = CharCmpX(c1, c2);
370 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
375 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
376 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
382 #if SANITIZER_INTERCEPT_STRCASECMP
383 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
384 int c1_low = ToLower(c1);
385 int c2_low = ToLower(c2);
386 return c1_low - c2_low;
389 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
390 const char *s1, const char *s2, int result)
392 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
394 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
395 unsigned char c1 = 0, c2 = 0;
398 c1 = (unsigned char)s1[i];
399 c2 = (unsigned char)s2[i];
400 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
402 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
403 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
404 int result = CharCaseCmp(c1, c2);
405 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
410 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
411 const char *s1, const char *s2, uptr size,
414 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
416 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
417 unsigned char c1 = 0, c2 = 0;
419 for (i = 0; i < size; i++) {
420 c1 = (unsigned char)s1[i];
421 c2 = (unsigned char)s2[i];
422 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
426 if (common_flags()->strict_string_checks) {
427 for (; i1 < size && s1[i1]; i1++) {}
428 for (; i2 < size && s2[i2]; i2++) {}
430 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
431 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
432 int result = CharCaseCmp(c1, c2);
433 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
434 s1, s2, size, result);
438 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
439 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
441 #define INIT_STRCASECMP
442 #define INIT_STRNCASECMP
445 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
446 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
448 uptr len1 = REAL(strlen)(s1);
449 uptr len2 = REAL(strlen)(s2);
450 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
451 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
455 #if SANITIZER_INTERCEPT_STRSTR
457 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
458 const char *s1, const char *s2, char *result)
460 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
461 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
462 return internal_strstr(s1, s2);
464 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
465 char *r = REAL(strstr)(s1, s2);
466 if (common_flags()->intercept_strstr)
467 StrstrCheck(ctx, r, s1, s2);
468 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
473 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
478 #if SANITIZER_INTERCEPT_STRCASESTR
480 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
481 const char *s1, const char *s2, char *result)
483 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
485 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
486 char *r = REAL(strcasestr)(s1, s2);
487 if (common_flags()->intercept_strstr)
488 StrstrCheck(ctx, r, s1, s2);
489 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
494 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
496 #define INIT_STRCASESTR
499 #if SANITIZER_INTERCEPT_STRTOK
501 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
503 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
504 if (!common_flags()->intercept_strtok) {
505 return REAL(strtok)(str, delimiters);
507 if (common_flags()->strict_string_checks) {
508 // If strict_string_checks is enabled, we check the whole first argument
509 // string on the first call (strtok saves this string in a static buffer
510 // for subsequent calls). We do not need to check strtok's result.
511 // As the delimiters can change, we check them every call.
512 if (str != nullptr) {
513 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
515 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
516 REAL(strlen)(delimiters) + 1);
517 return REAL(strtok)(str, delimiters);
519 // However, when strict_string_checks is disabled we cannot check the
520 // whole string on the first call. Instead, we check the result string
521 // which is guaranteed to be a NULL-terminated substring of the first
522 // argument. We also conservatively check one character of str and the
524 if (str != nullptr) {
525 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
527 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
528 char *result = REAL(strtok)(str, delimiters);
529 if (result != nullptr) {
530 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
531 } else if (str != nullptr) {
532 // No delimiter were found, it's safe to assume that the entire str was
534 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
540 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
545 #if SANITIZER_INTERCEPT_MEMMEM
546 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
547 const void *s1, SIZE_T len1, const void *s2,
548 SIZE_T len2, void *result)
550 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
553 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
554 void *r = REAL(memmem)(s1, len1, s2, len2);
555 if (common_flags()->intercept_memmem) {
556 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
557 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
559 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
560 s1, len1, s2, len2, r);
564 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
567 #endif // SANITIZER_INTERCEPT_MEMMEM
569 #if SANITIZER_INTERCEPT_STRCHR
570 INTERCEPTOR(char*, strchr, const char *s, int c) {
572 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
573 return internal_strchr(s, c);
574 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
575 char *result = REAL(strchr)(s, c);
576 if (common_flags()->intercept_strchr) {
577 // Keep strlen as macro argument, as macro may ignore it.
578 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
579 (result ? result - s : REAL(strlen)(s)) + 1);
583 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
588 #if SANITIZER_INTERCEPT_STRCHRNUL
589 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
591 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
592 char *result = REAL(strchrnul)(s, c);
593 uptr len = result - s + 1;
594 if (common_flags()->intercept_strchr)
595 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
598 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
600 #define INIT_STRCHRNUL
603 #if SANITIZER_INTERCEPT_STRRCHR
604 INTERCEPTOR(char*, strrchr, const char *s, int c) {
606 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
607 return internal_strrchr(s, c);
608 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
609 if (common_flags()->intercept_strchr)
610 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
611 return REAL(strrchr)(s, c);
613 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
618 #if SANITIZER_INTERCEPT_STRSPN
619 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
621 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
622 SIZE_T r = REAL(strspn)(s1, s2);
623 if (common_flags()->intercept_strspn) {
624 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
625 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
630 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
632 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
633 SIZE_T r = REAL(strcspn)(s1, s2);
634 if (common_flags()->intercept_strspn) {
635 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
636 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
641 #define INIT_STRSPN \
642 COMMON_INTERCEPT_FUNCTION(strspn); \
643 COMMON_INTERCEPT_FUNCTION(strcspn);
648 #if SANITIZER_INTERCEPT_STRPBRK
649 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
651 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
652 char *r = REAL(strpbrk)(s1, s2);
653 if (common_flags()->intercept_strpbrk) {
654 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
655 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
656 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
661 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
666 #if SANITIZER_INTERCEPT_MEMSET
667 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
669 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
672 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
677 #if SANITIZER_INTERCEPT_MEMMOVE
678 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
680 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
683 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
688 #if SANITIZER_INTERCEPT_MEMCPY
689 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
690 // On OS X, calling internal_memcpy here will cause memory corruptions,
691 // because memcpy and memmove are actually aliases of the same
692 // implementation. We need to use internal_memmove here.
693 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
694 // due to memcpy being an alias of memmove on OS X.
696 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
697 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
699 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
703 #define INIT_MEMCPY \
705 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
706 COMMON_INTERCEPT_FUNCTION(memcpy); \
708 ASSIGN_REAL(memcpy, memmove); \
710 CHECK(REAL(memcpy)); \
717 #if SANITIZER_INTERCEPT_MEMCMP
719 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
720 const void *s1, const void *s2, uptr n,
723 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
724 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
725 return internal_memcmp(a1, a2, size);
727 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
728 if (common_flags()->intercept_memcmp) {
729 if (common_flags()->strict_memcmp) {
730 // Check the entire regions even if the first bytes of the buffers are
732 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
733 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
734 // Fallthrough to REAL(memcmp) below.
736 unsigned char c1 = 0, c2 = 0;
737 const unsigned char *s1 = (const unsigned char*)a1;
738 const unsigned char *s2 = (const unsigned char*)a2;
740 for (i = 0; i < size; i++) {
745 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
746 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
747 int r = CharCmpX(c1, c2);
748 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
753 int result = REAL(memcmp(a1, a2, size));
754 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
759 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
764 #if SANITIZER_INTERCEPT_MEMCHR
765 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
766 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
767 return internal_memchr(s, c, n);
769 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
770 #if SANITIZER_WINDOWS
773 res = REAL(memchr)(s, c, n);
775 res = internal_memchr(s, c, n);
778 void *res = REAL(memchr)(s, c, n);
780 uptr len = res ? (char *)res - (const char *)s + 1 : n;
781 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
785 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
790 #if SANITIZER_INTERCEPT_MEMRCHR
791 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
793 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
794 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
795 return REAL(memrchr)(s, c, n);
798 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
803 #if SANITIZER_INTERCEPT_FREXP
804 INTERCEPTOR(double, frexp, double x, int *exp) {
806 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
807 // Assuming frexp() always writes to |exp|.
808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
809 double res = REAL(frexp)(x, exp);
813 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
816 #endif // SANITIZER_INTERCEPT_FREXP
818 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
819 INTERCEPTOR(float, frexpf, float x, int *exp) {
821 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
822 // FIXME: under ASan the call below may write to freed memory and corrupt
824 // https://github.com/google/sanitizers/issues/321.
825 float res = REAL(frexpf)(x, exp);
826 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
830 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
832 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
833 // FIXME: under ASan the call below may write to freed memory and corrupt
835 // https://github.com/google/sanitizers/issues/321.
836 long double res = REAL(frexpl)(x, exp);
837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
841 #define INIT_FREXPF_FREXPL \
842 COMMON_INTERCEPT_FUNCTION(frexpf); \
843 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
845 #define INIT_FREXPF_FREXPL
846 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
849 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
850 SIZE_T iovlen, SIZE_T maxlen) {
851 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
852 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
858 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
859 SIZE_T iovlen, SIZE_T maxlen) {
860 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
861 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
862 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
863 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
869 #if SANITIZER_INTERCEPT_READ
870 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
872 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
873 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
874 // FIXME: under ASan the call below may write to freed memory and corrupt
876 // https://github.com/google/sanitizers/issues/321.
877 SSIZE_T res = REAL(read)(fd, ptr, count);
878 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
879 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
882 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
887 #if SANITIZER_INTERCEPT_FREAD
888 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
889 // libc file streams can call user-supplied functions, see fopencookie.
891 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
892 // FIXME: under ASan the call below may write to freed memory and corrupt
894 // https://github.com/google/sanitizers/issues/321.
895 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
896 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
899 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
904 #if SANITIZER_INTERCEPT_PREAD
905 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
907 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
908 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
909 // FIXME: under ASan the call below may write to freed memory and corrupt
911 // https://github.com/google/sanitizers/issues/321.
912 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
913 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
914 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
917 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
922 #if SANITIZER_INTERCEPT_PREAD64
923 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
925 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
926 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
927 // FIXME: under ASan the call below may write to freed memory and corrupt
929 // https://github.com/google/sanitizers/issues/321.
930 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
931 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
932 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
935 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
940 #if SANITIZER_INTERCEPT_READV
941 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
944 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
945 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
946 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
947 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
948 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
951 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
956 #if SANITIZER_INTERCEPT_PREADV
957 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
960 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
961 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
962 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
963 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
964 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
967 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
972 #if SANITIZER_INTERCEPT_PREADV64
973 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
976 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
977 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
978 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
979 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
980 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
983 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
985 #define INIT_PREADV64
988 #if SANITIZER_INTERCEPT_WRITE
989 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
991 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
992 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
993 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
994 SSIZE_T res = REAL(write)(fd, ptr, count);
995 // FIXME: this check should be _before_ the call to REAL(write), not after
996 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
999 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1004 #if SANITIZER_INTERCEPT_FWRITE
1005 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1006 // libc file streams can call user-supplied functions, see fopencookie.
1008 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1009 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1010 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1013 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1018 #if SANITIZER_INTERCEPT_PWRITE
1019 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1021 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1022 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1023 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1024 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1025 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1028 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1033 #if SANITIZER_INTERCEPT_PWRITE64
1034 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1037 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1038 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1039 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1040 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1041 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1044 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1046 #define INIT_PWRITE64
1049 #if SANITIZER_INTERCEPT_WRITEV
1050 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1053 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1054 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1055 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1056 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1057 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1060 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1065 #if SANITIZER_INTERCEPT_PWRITEV
1066 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1069 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1070 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1071 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1072 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1073 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1076 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1078 #define INIT_PWRITEV
1081 #if SANITIZER_INTERCEPT_PWRITEV64
1082 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1085 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1086 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1087 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1088 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1089 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1092 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1094 #define INIT_PWRITEV64
1097 #if SANITIZER_INTERCEPT_PRCTL
1098 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1099 unsigned long arg3, // NOLINT
1100 unsigned long arg4, unsigned long arg5) { // NOLINT
1102 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1103 static const int PR_SET_NAME = 15;
1104 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1105 if (option == PR_SET_NAME) {
1107 internal_strncpy(buff, (char *)arg2, 15);
1109 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1113 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1116 #endif // SANITIZER_INTERCEPT_PRCTL
1118 #if SANITIZER_INTERCEPT_TIME
1119 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1121 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1122 unsigned long local_t;
1123 unsigned long res = REAL(time)(&local_t);
1124 if (t && res != (unsigned long)-1) {
1125 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1130 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1133 #endif // SANITIZER_INTERCEPT_TIME
1135 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1136 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1137 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1139 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1140 // can point to shared memory and tsan would report a data race.
1141 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1142 REAL(strlen(tm->tm_zone)) + 1);
1145 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1147 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1148 __sanitizer_tm *res = REAL(localtime)(timep);
1150 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1151 unpoison_tm(ctx, res);
1155 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1157 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1158 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1160 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1161 unpoison_tm(ctx, res);
1165 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1167 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1168 __sanitizer_tm *res = REAL(gmtime)(timep);
1170 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1171 unpoison_tm(ctx, res);
1175 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1177 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1178 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1180 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1181 unpoison_tm(ctx, res);
1185 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1187 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1188 // FIXME: under ASan the call below may write to freed memory and corrupt
1189 // its metadata. See
1190 // https://github.com/google/sanitizers/issues/321.
1191 char *res = REAL(ctime)(timep);
1193 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1198 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1200 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1201 // FIXME: under ASan the call below may write to freed memory and corrupt
1202 // its metadata. See
1203 // https://github.com/google/sanitizers/issues/321.
1204 char *res = REAL(ctime_r)(timep, result);
1206 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1207 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1211 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1213 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1214 // FIXME: under ASan the call below may write to freed memory and corrupt
1215 // its metadata. See
1216 // https://github.com/google/sanitizers/issues/321.
1217 char *res = REAL(asctime)(tm);
1219 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1220 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1224 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1226 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1227 // FIXME: under ASan the call below may write to freed memory and corrupt
1228 // its metadata. See
1229 // https://github.com/google/sanitizers/issues/321.
1230 char *res = REAL(asctime_r)(tm, result);
1232 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1233 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1237 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1239 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1240 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1241 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1242 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1243 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1244 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1245 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1246 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1247 long res = REAL(mktime)(tm);
1248 if (res != -1) unpoison_tm(ctx, tm);
1251 #define INIT_LOCALTIME_AND_FRIENDS \
1252 COMMON_INTERCEPT_FUNCTION(localtime); \
1253 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1254 COMMON_INTERCEPT_FUNCTION(gmtime); \
1255 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1256 COMMON_INTERCEPT_FUNCTION(ctime); \
1257 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1258 COMMON_INTERCEPT_FUNCTION(asctime); \
1259 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1260 COMMON_INTERCEPT_FUNCTION(mktime);
1262 #define INIT_LOCALTIME_AND_FRIENDS
1263 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1265 #if SANITIZER_INTERCEPT_STRPTIME
1266 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1268 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1270 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1271 // FIXME: under ASan the call below may write to freed memory and corrupt
1272 // its metadata. See
1273 // https://github.com/google/sanitizers/issues/321.
1274 char *res = REAL(strptime)(s, format, tm);
1275 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1277 // Do not call unpoison_tm here, because strptime does not, in fact,
1278 // initialize the entire struct tm. For example, tm_zone pointer is left
1280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1284 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1286 #define INIT_STRPTIME
1289 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1290 #include "sanitizer_common_interceptors_format.inc"
1292 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1296 va_start(ap, format); \
1297 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1298 int res = WRAP(vname)(__VA_ARGS__, ap); \
1305 #if SANITIZER_INTERCEPT_SCANF
1307 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1310 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1313 int res = REAL(vname)(__VA_ARGS__); \
1315 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1320 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1321 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1323 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1324 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1326 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1327 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1329 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1330 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1331 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1333 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1335 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1337 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1338 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1339 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1341 INTERCEPTOR(int, scanf, const char *format, ...)
1342 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1344 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1345 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1347 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1348 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1350 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1351 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1352 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1354 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1355 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1357 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1358 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1363 #if SANITIZER_INTERCEPT_SCANF
1364 #define INIT_SCANF \
1365 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1366 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1367 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1368 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1369 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1370 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1375 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1376 #define INIT_ISOC99_SCANF \
1377 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1378 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1379 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1380 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1381 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1382 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1384 #define INIT_ISOC99_SCANF
1387 #if SANITIZER_INTERCEPT_PRINTF
1389 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1391 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1395 #define VPRINTF_INTERCEPTOR_RETURN() \
1398 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1400 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1401 if (common_flags()->check_printf) \
1402 printf_common(ctx, format, aq); \
1403 int res = REAL(vname)(__VA_ARGS__); \
1404 VPRINTF_INTERCEPTOR_RETURN(); \
1408 // FIXME: under ASan the REAL() call below may write to freed memory and
1409 // corrupt its metadata. See
1410 // https://github.com/google/sanitizers/issues/321.
1411 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1413 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1414 if (common_flags()->check_printf) { \
1415 printf_common(ctx, format, aq); \
1417 int res = REAL(vname)(str, __VA_ARGS__); \
1419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1421 VPRINTF_INTERCEPTOR_RETURN(); \
1425 // FIXME: under ASan the REAL() call below may write to freed memory and
1426 // corrupt its metadata. See
1427 // https://github.com/google/sanitizers/issues/321.
1428 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1430 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1431 if (common_flags()->check_printf) { \
1432 printf_common(ctx, format, aq); \
1434 int res = REAL(vname)(str, size, __VA_ARGS__); \
1436 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1438 VPRINTF_INTERCEPTOR_RETURN(); \
1442 // FIXME: under ASan the REAL() call below may write to freed memory and
1443 // corrupt its metadata. See
1444 // https://github.com/google/sanitizers/issues/321.
1445 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1447 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1449 if (common_flags()->check_printf) { \
1450 printf_common(ctx, format, aq); \
1452 int res = REAL(vname)(strp, __VA_ARGS__); \
1454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1456 VPRINTF_INTERCEPTOR_RETURN(); \
1460 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1461 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1463 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1465 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1467 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1469 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1471 #if SANITIZER_INTERCEPT_PRINTF_L
1472 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1473 const char *format, va_list ap)
1474 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1476 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1477 const char *format, ...)
1478 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1479 #endif // SANITIZER_INTERCEPT_PRINTF_L
1481 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1482 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1484 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1485 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1487 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1488 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1489 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1491 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1492 const char *format, va_list ap)
1493 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1495 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1497 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1499 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1501 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1504 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1506 INTERCEPTOR(int, printf, const char *format, ...)
1507 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1509 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1510 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1512 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1513 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1515 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1516 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1518 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1519 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1521 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1522 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1523 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1525 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1527 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1529 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1530 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1532 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1533 const char *format, ...)
1534 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1537 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1539 #endif // SANITIZER_INTERCEPT_PRINTF
1541 #if SANITIZER_INTERCEPT_PRINTF
1542 #define INIT_PRINTF \
1543 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1544 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1545 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1546 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1547 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1548 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1549 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1550 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1551 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1552 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1557 #if SANITIZER_INTERCEPT_PRINTF_L
1558 #define INIT_PRINTF_L \
1559 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1560 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1562 #define INIT_PRINTF_L
1565 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1566 #define INIT_ISOC99_PRINTF \
1567 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1568 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1569 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1570 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1571 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1572 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1573 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1574 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1576 #define INIT_ISOC99_PRINTF
1579 #if SANITIZER_INTERCEPT_IOCTL
1580 #include "sanitizer_common_interceptors_ioctl.inc"
1581 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1582 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1583 // can trigger a report and we need to be able to unwind through this
1584 // function. On Mac in debug mode we might not have a frame pointer, because
1585 // ioctl_common_[pre|post] doesn't get inlined here.
1586 ENABLE_FRAME_POINTER;
1590 va_start(ap, request);
1591 void *arg = va_arg(ap, void *);
1593 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1595 CHECK(ioctl_initialized);
1597 // Note: TSan does not use common flags, and they are zero-initialized.
1598 // This effectively disables ioctl handling in TSan.
1599 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1601 // Although request is unsigned long, the rest of the interceptor uses it
1602 // as just "unsigned" to save space, because we know that all values fit in
1603 // "unsigned" - they are compile-time constants.
1605 const ioctl_desc *desc = ioctl_lookup(request);
1606 ioctl_desc decoded_desc;
1608 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1609 if (!ioctl_decode(request, &decoded_desc))
1610 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1612 desc = &decoded_desc;
1615 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1616 int res = REAL(ioctl)(d, request, arg);
1617 // FIXME: some ioctls have different return values for success and failure.
1618 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1621 #define INIT_IOCTL \
1623 COMMON_INTERCEPT_FUNCTION(ioctl);
1628 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1629 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1630 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1631 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1633 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1635 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1636 REAL(strlen)(pwd->pw_name) + 1);
1638 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1639 REAL(strlen)(pwd->pw_passwd) + 1);
1640 #if !SANITIZER_ANDROID
1642 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1643 REAL(strlen)(pwd->pw_gecos) + 1);
1647 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1648 REAL(strlen)(pwd->pw_class) + 1);
1651 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1652 REAL(strlen)(pwd->pw_dir) + 1);
1654 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1655 REAL(strlen)(pwd->pw_shell) + 1);
1659 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1661 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1663 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1664 REAL(strlen)(grp->gr_name) + 1);
1666 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1667 REAL(strlen)(grp->gr_passwd) + 1);
1668 char **p = grp->gr_mem;
1670 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1672 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1673 (p - grp->gr_mem + 1) * sizeof(*p));
1676 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1677 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1678 // SANITIZER_INTERCEPT_GETPWENT_R ||
1679 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1681 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1682 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1684 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1685 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1686 __sanitizer_passwd *res = REAL(getpwnam)(name);
1687 if (res) unpoison_passwd(ctx, res);
1690 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1692 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1693 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1694 if (res) unpoison_passwd(ctx, res);
1697 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1699 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1700 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1701 __sanitizer_group *res = REAL(getgrnam)(name);
1702 if (res) unpoison_group(ctx, res);
1705 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1707 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1708 __sanitizer_group *res = REAL(getgrgid)(gid);
1709 if (res) unpoison_group(ctx, res);
1712 #define INIT_GETPWNAM_AND_FRIENDS \
1713 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1714 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1715 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1716 COMMON_INTERCEPT_FUNCTION(getgrgid);
1718 #define INIT_GETPWNAM_AND_FRIENDS
1721 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1722 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1723 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1725 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1726 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1727 // FIXME: under ASan the call below may write to freed memory and corrupt
1728 // its metadata. See
1729 // https://github.com/google/sanitizers/issues/321.
1730 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1732 if (result && *result) unpoison_passwd(ctx, *result);
1733 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1735 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1738 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1739 SIZE_T buflen, __sanitizer_passwd **result) {
1741 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1742 // FIXME: under ASan the call below may write to freed memory and corrupt
1743 // its metadata. See
1744 // https://github.com/google/sanitizers/issues/321.
1745 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1747 if (result && *result) unpoison_passwd(ctx, *result);
1748 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1750 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1753 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1754 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1756 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1757 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1758 // FIXME: under ASan the call below may write to freed memory and corrupt
1759 // its metadata. See
1760 // https://github.com/google/sanitizers/issues/321.
1761 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1763 if (result && *result) unpoison_group(ctx, *result);
1764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1766 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1769 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1770 SIZE_T buflen, __sanitizer_group **result) {
1772 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1773 // FIXME: under ASan the call below may write to freed memory and corrupt
1774 // its metadata. See
1775 // https://github.com/google/sanitizers/issues/321.
1776 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1778 if (result && *result) unpoison_group(ctx, *result);
1779 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1781 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1784 #define INIT_GETPWNAM_R_AND_FRIENDS \
1785 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1786 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1787 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1788 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1790 #define INIT_GETPWNAM_R_AND_FRIENDS
1793 #if SANITIZER_INTERCEPT_GETPWENT
1794 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1796 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1797 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1798 if (res) unpoison_passwd(ctx, res);
1801 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1803 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1804 __sanitizer_group *res = REAL(getgrent)(dummy);
1805 if (res) unpoison_group(ctx, res);;
1808 #define INIT_GETPWENT \
1809 COMMON_INTERCEPT_FUNCTION(getpwent); \
1810 COMMON_INTERCEPT_FUNCTION(getgrent);
1812 #define INIT_GETPWENT
1815 #if SANITIZER_INTERCEPT_FGETPWENT
1816 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1818 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1819 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1820 if (res) unpoison_passwd(ctx, res);
1823 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1825 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1826 __sanitizer_group *res = REAL(fgetgrent)(fp);
1827 if (res) unpoison_group(ctx, res);
1830 #define INIT_FGETPWENT \
1831 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1832 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1834 #define INIT_FGETPWENT
1837 #if SANITIZER_INTERCEPT_GETPWENT_R
1838 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1839 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1841 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1842 // FIXME: under ASan the call below may write to freed memory and corrupt
1843 // its metadata. See
1844 // https://github.com/google/sanitizers/issues/321.
1845 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1847 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1848 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1850 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1853 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1854 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1856 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1857 // FIXME: under ASan the call below may write to freed memory and corrupt
1858 // its metadata. See
1859 // https://github.com/google/sanitizers/issues/321.
1860 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1862 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1863 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1865 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1868 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1869 __sanitizer_group **pwbufp) {
1871 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1872 // FIXME: under ASan the call below may write to freed memory and corrupt
1873 // its metadata. See
1874 // https://github.com/google/sanitizers/issues/321.
1875 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1877 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1880 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1883 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1884 SIZE_T buflen, __sanitizer_group **pwbufp) {
1886 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1887 // FIXME: under ASan the call below may write to freed memory and corrupt
1888 // its metadata. See
1889 // https://github.com/google/sanitizers/issues/321.
1890 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1892 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1893 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1895 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1898 #define INIT_GETPWENT_R \
1899 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
1900 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1901 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
1902 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1904 #define INIT_GETPWENT_R
1907 #if SANITIZER_INTERCEPT_SETPWENT
1908 // The only thing these interceptors do is disable any nested interceptors.
1909 // These functions may open nss modules and call uninstrumented functions from
1910 // them, and we don't want things like strlen() to trigger.
1911 INTERCEPTOR(void, setpwent, int dummy) {
1913 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1914 REAL(setpwent)(dummy);
1916 INTERCEPTOR(void, endpwent, int dummy) {
1918 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1919 REAL(endpwent)(dummy);
1921 INTERCEPTOR(void, setgrent, int dummy) {
1923 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1924 REAL(setgrent)(dummy);
1926 INTERCEPTOR(void, endgrent, int dummy) {
1928 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1929 REAL(endgrent)(dummy);
1931 #define INIT_SETPWENT \
1932 COMMON_INTERCEPT_FUNCTION(setpwent); \
1933 COMMON_INTERCEPT_FUNCTION(endpwent); \
1934 COMMON_INTERCEPT_FUNCTION(setgrent); \
1935 COMMON_INTERCEPT_FUNCTION(endgrent);
1937 #define INIT_SETPWENT
1940 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1941 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1943 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1944 // FIXME: under ASan the call below may write to freed memory and corrupt
1945 // its metadata. See
1946 // https://github.com/google/sanitizers/issues/321.
1947 int res = REAL(clock_getres)(clk_id, tp);
1949 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1953 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1955 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1956 // FIXME: under ASan the call below may write to freed memory and corrupt
1957 // its metadata. See
1958 // https://github.com/google/sanitizers/issues/321.
1959 int res = REAL(clock_gettime)(clk_id, tp);
1961 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1965 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1967 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1968 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1969 return REAL(clock_settime)(clk_id, tp);
1971 #define INIT_CLOCK_GETTIME \
1972 COMMON_INTERCEPT_FUNCTION(clock_getres); \
1973 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1974 COMMON_INTERCEPT_FUNCTION(clock_settime);
1976 #define INIT_CLOCK_GETTIME
1979 #if SANITIZER_INTERCEPT_GETITIMER
1980 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1982 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1983 // FIXME: under ASan the call below may write to freed memory and corrupt
1984 // its metadata. See
1985 // https://github.com/google/sanitizers/issues/321.
1986 int res = REAL(getitimer)(which, curr_value);
1987 if (!res && curr_value) {
1988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1992 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1994 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1996 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1997 // FIXME: under ASan the call below may write to freed memory and corrupt
1998 // its metadata. See
1999 // https://github.com/google/sanitizers/issues/321.
2000 int res = REAL(setitimer)(which, new_value, old_value);
2001 if (!res && old_value) {
2002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2006 #define INIT_GETITIMER \
2007 COMMON_INTERCEPT_FUNCTION(getitimer); \
2008 COMMON_INTERCEPT_FUNCTION(setitimer);
2010 #define INIT_GETITIMER
2013 #if SANITIZER_INTERCEPT_GLOB
2014 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2015 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2016 // +1 for NULL pointer at the end.
2017 if (pglob->gl_pathv)
2018 COMMON_INTERCEPTOR_WRITE_RANGE(
2019 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2020 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2021 char *p = pglob->gl_pathv[i];
2022 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2026 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2028 static void wrapped_gl_closedir(void *dir) {
2029 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2030 pglob_copy->gl_closedir(dir);
2033 static void *wrapped_gl_readdir(void *dir) {
2034 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2035 return pglob_copy->gl_readdir(dir);
2038 static void *wrapped_gl_opendir(const char *s) {
2039 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2040 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2041 return pglob_copy->gl_opendir(s);
2044 static int wrapped_gl_lstat(const char *s, void *st) {
2045 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2046 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2047 return pglob_copy->gl_lstat(s, st);
2050 static int wrapped_gl_stat(const char *s, void *st) {
2051 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2052 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2053 return pglob_copy->gl_stat(s, st);
2056 static const __sanitizer_glob_t kGlobCopy = {
2058 0, wrapped_gl_closedir, wrapped_gl_readdir,
2059 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2061 INTERCEPTOR(int, glob, const char *pattern, int flags,
2062 int (*errfunc)(const char *epath, int eerrno),
2063 __sanitizer_glob_t *pglob) {
2065 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2066 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2067 __sanitizer_glob_t glob_copy;
2068 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2069 if (flags & glob_altdirfunc) {
2070 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2071 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2072 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2073 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2074 Swap(pglob->gl_stat, glob_copy.gl_stat);
2075 pglob_copy = &glob_copy;
2077 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2078 if (flags & glob_altdirfunc) {
2079 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2080 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2081 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2082 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2083 Swap(pglob->gl_stat, glob_copy.gl_stat);
2086 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2090 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2091 int (*errfunc)(const char *epath, int eerrno),
2092 __sanitizer_glob_t *pglob) {
2094 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2095 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2096 __sanitizer_glob_t glob_copy;
2097 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2098 if (flags & glob_altdirfunc) {
2099 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2100 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2101 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2102 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2103 Swap(pglob->gl_stat, glob_copy.gl_stat);
2104 pglob_copy = &glob_copy;
2106 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2107 if (flags & glob_altdirfunc) {
2108 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2109 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2110 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2111 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2112 Swap(pglob->gl_stat, glob_copy.gl_stat);
2115 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2119 COMMON_INTERCEPT_FUNCTION(glob); \
2120 COMMON_INTERCEPT_FUNCTION(glob64);
2121 #else // SANITIZER_INTERCEPT_GLOB
2123 #endif // SANITIZER_INTERCEPT_GLOB
2125 #if SANITIZER_INTERCEPT_WAIT
2126 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2127 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2129 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2131 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2132 // FIXME: under ASan the call below may write to freed memory and corrupt
2133 // its metadata. See
2134 // https://github.com/google/sanitizers/issues/321.
2135 int res = REAL(wait)(status);
2136 if (res != -1 && status)
2137 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2140 // On FreeBSD id_t is always 64-bit wide.
2141 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2142 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2145 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2149 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2150 // FIXME: under ASan the call below may write to freed memory and corrupt
2151 // its metadata. See
2152 // https://github.com/google/sanitizers/issues/321.
2153 int res = REAL(waitid)(idtype, id, infop, options);
2154 if (res != -1 && infop)
2155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2158 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2160 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2161 // FIXME: under ASan the call below may write to freed memory and corrupt
2162 // its metadata. See
2163 // https://github.com/google/sanitizers/issues/321.
2164 int res = REAL(waitpid)(pid, status, options);
2165 if (res != -1 && status)
2166 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2169 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2171 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2172 // FIXME: under ASan the call below may write to freed memory and corrupt
2173 // its metadata. See
2174 // https://github.com/google/sanitizers/issues/321.
2175 int res = REAL(wait3)(status, options, rusage);
2177 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2178 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2182 #if SANITIZER_ANDROID
2183 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2185 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2186 // FIXME: under ASan the call below may write to freed memory and corrupt
2187 // its metadata. See
2188 // https://github.com/google/sanitizers/issues/321.
2189 int res = REAL(__wait4)(pid, status, options, rusage);
2191 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2192 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2196 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2198 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2200 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2201 // FIXME: under ASan the call below may write to freed memory and corrupt
2202 // its metadata. See
2203 // https://github.com/google/sanitizers/issues/321.
2204 int res = REAL(wait4)(pid, status, options, rusage);
2206 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2207 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2211 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2212 #endif // SANITIZER_ANDROID
2214 COMMON_INTERCEPT_FUNCTION(wait); \
2215 COMMON_INTERCEPT_FUNCTION(waitid); \
2216 COMMON_INTERCEPT_FUNCTION(waitpid); \
2217 COMMON_INTERCEPT_FUNCTION(wait3);
2223 #if SANITIZER_INTERCEPT_INET
2224 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2226 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2227 uptr sz = __sanitizer_in_addr_sz(af);
2228 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2229 // FIXME: figure out read size based on the address family.
2230 // FIXME: under ASan the call below may write to freed memory and corrupt
2231 // its metadata. See
2232 // https://github.com/google/sanitizers/issues/321.
2233 char *res = REAL(inet_ntop)(af, src, dst, size);
2234 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2237 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2239 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2240 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2241 // FIXME: figure out read size based on the address family.
2242 // FIXME: under ASan the call below may write to freed memory and corrupt
2243 // its metadata. See
2244 // https://github.com/google/sanitizers/issues/321.
2245 int res = REAL(inet_pton)(af, src, dst);
2247 uptr sz = __sanitizer_in_addr_sz(af);
2248 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2253 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2254 COMMON_INTERCEPT_FUNCTION(inet_pton);
2259 #if SANITIZER_INTERCEPT_INET
2260 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2262 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2263 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2264 // FIXME: under ASan the call below may write to freed memory and corrupt
2265 // its metadata. See
2266 // https://github.com/google/sanitizers/issues/321.
2267 int res = REAL(inet_aton)(cp, dst);
2269 uptr sz = __sanitizer_in_addr_sz(af_inet);
2270 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2274 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2276 #define INIT_INET_ATON
2279 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2280 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2282 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2283 // FIXME: under ASan the call below may write to freed memory and corrupt
2284 // its metadata. See
2285 // https://github.com/google/sanitizers/issues/321.
2286 int res = REAL(pthread_getschedparam)(thread, policy, param);
2288 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2289 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2293 #define INIT_PTHREAD_GETSCHEDPARAM \
2294 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2296 #define INIT_PTHREAD_GETSCHEDPARAM
2299 #if SANITIZER_INTERCEPT_GETADDRINFO
2300 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2301 struct __sanitizer_addrinfo *hints,
2302 struct __sanitizer_addrinfo **out) {
2304 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2305 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2307 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2309 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2310 // FIXME: under ASan the call below may write to freed memory and corrupt
2311 // its metadata. See
2312 // https://github.com/google/sanitizers/issues/321.
2313 int res = REAL(getaddrinfo)(node, service, hints, out);
2314 if (res == 0 && out) {
2315 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2316 struct __sanitizer_addrinfo *p = *out;
2318 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2320 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2321 if (p->ai_canonname)
2322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2323 REAL(strlen)(p->ai_canonname) + 1);
2329 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2331 #define INIT_GETADDRINFO
2334 #if SANITIZER_INTERCEPT_GETNAMEINFO
2335 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2336 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2338 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2339 serv, servlen, flags);
2340 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2341 // There is padding in in_addr that may make this too noisy
2342 // FIXME: under ASan the call below may write to freed memory and corrupt
2343 // its metadata. See
2344 // https://github.com/google/sanitizers/issues/321.
2346 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2348 if (host && hostlen)
2349 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2350 if (serv && servlen)
2351 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2355 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2357 #define INIT_GETNAMEINFO
2360 #if SANITIZER_INTERCEPT_GETSOCKNAME
2361 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2363 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2364 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2365 int addrlen_in = *addrlen;
2366 // FIXME: under ASan the call below may write to freed memory and corrupt
2367 // its metadata. See
2368 // https://github.com/google/sanitizers/issues/321.
2369 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2371 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2375 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2377 #define INIT_GETSOCKNAME
2380 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2381 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2382 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2384 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2385 char **p = h->h_aliases;
2387 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2390 COMMON_INTERCEPTOR_WRITE_RANGE(
2391 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2394 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2397 COMMON_INTERCEPTOR_WRITE_RANGE(
2398 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2402 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2403 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2405 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2406 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2407 if (res) write_hostent(ctx, res);
2411 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2414 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2415 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2416 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2417 if (res) write_hostent(ctx, res);
2421 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2423 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2424 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2425 if (res) write_hostent(ctx, res);
2429 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2431 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2432 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2433 if (res) write_hostent(ctx, res);
2436 #define INIT_GETHOSTBYNAME \
2437 COMMON_INTERCEPT_FUNCTION(gethostent); \
2438 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2439 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
2440 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2442 #define INIT_GETHOSTBYNAME
2445 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2446 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2447 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2450 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2452 // FIXME: under ASan the call below may write to freed memory and corrupt
2453 // its metadata. See
2454 // https://github.com/google/sanitizers/issues/321.
2455 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2457 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2458 if (res == 0 && *result) write_hostent(ctx, *result);
2461 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2464 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2466 #define INIT_GETHOSTBYNAME_R
2469 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2470 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2471 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2473 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2475 // FIXME: under ASan the call below may write to freed memory and corrupt
2476 // its metadata. See
2477 // https://github.com/google/sanitizers/issues/321.
2478 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2480 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2481 if (res == 0 && *result) write_hostent(ctx, *result);
2484 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2487 #define INIT_GETHOSTENT_R \
2488 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2490 #define INIT_GETHOSTENT_R
2493 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2494 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2495 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2496 __sanitizer_hostent **result, int *h_errnop) {
2498 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2499 buflen, result, h_errnop);
2500 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2501 // FIXME: under ASan the call below may write to freed memory and corrupt
2502 // its metadata. See
2503 // https://github.com/google/sanitizers/issues/321.
2504 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2507 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2508 if (res == 0 && *result) write_hostent(ctx, *result);
2511 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2514 #define INIT_GETHOSTBYADDR_R \
2515 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2517 #define INIT_GETHOSTBYADDR_R
2520 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2521 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2522 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2523 __sanitizer_hostent **result, int *h_errnop) {
2525 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2527 // FIXME: under ASan the call below may write to freed memory and corrupt
2528 // its metadata. See
2529 // https://github.com/google/sanitizers/issues/321.
2531 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2533 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2534 if (res == 0 && *result) write_hostent(ctx, *result);
2537 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2540 #define INIT_GETHOSTBYNAME2_R \
2541 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2543 #define INIT_GETHOSTBYNAME2_R
2546 #if SANITIZER_INTERCEPT_GETSOCKOPT
2547 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2550 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2552 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2553 // FIXME: under ASan the call below may write to freed memory and corrupt
2554 // its metadata. See
2555 // https://github.com/google/sanitizers/issues/321.
2556 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2558 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2561 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2563 #define INIT_GETSOCKOPT
2566 #if SANITIZER_INTERCEPT_ACCEPT
2567 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2569 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2570 unsigned addrlen0 = 0;
2572 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2573 addrlen0 = *addrlen;
2575 int fd2 = REAL(accept)(fd, addr, addrlen);
2577 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2578 if (addr && addrlen)
2579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2583 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2588 #if SANITIZER_INTERCEPT_ACCEPT4
2589 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2591 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2592 unsigned addrlen0 = 0;
2594 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2595 addrlen0 = *addrlen;
2597 // FIXME: under ASan the call below may write to freed memory and corrupt
2598 // its metadata. See
2599 // https://github.com/google/sanitizers/issues/321.
2600 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2602 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2603 if (addr && addrlen)
2604 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2608 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2610 #define INIT_ACCEPT4
2613 #if SANITIZER_INTERCEPT_MODF
2614 INTERCEPTOR(double, modf, double x, double *iptr) {
2616 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2617 // FIXME: under ASan the call below may write to freed memory and corrupt
2618 // its metadata. See
2619 // https://github.com/google/sanitizers/issues/321.
2620 double res = REAL(modf)(x, iptr);
2622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2626 INTERCEPTOR(float, modff, float x, float *iptr) {
2628 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2629 // FIXME: under ASan the call below may write to freed memory and corrupt
2630 // its metadata. See
2631 // https://github.com/google/sanitizers/issues/321.
2632 float res = REAL(modff)(x, iptr);
2634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2638 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2640 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2641 // FIXME: under ASan the call below may write to freed memory and corrupt
2642 // its metadata. See
2643 // https://github.com/google/sanitizers/issues/321.
2644 long double res = REAL(modfl)(x, iptr);
2646 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2651 COMMON_INTERCEPT_FUNCTION(modf); \
2652 COMMON_INTERCEPT_FUNCTION(modff); \
2653 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2658 #if SANITIZER_INTERCEPT_RECVMSG
2659 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2661 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2662 if (msg->msg_name && msg->msg_namelen)
2663 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2664 if (msg->msg_iov && msg->msg_iovlen)
2665 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2666 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2667 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2668 if (msg->msg_control && msg->msg_controllen)
2669 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2672 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2675 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
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 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2681 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2683 write_msghdr(ctx, msg, res);
2684 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2689 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2691 #define INIT_RECVMSG
2694 #if SANITIZER_INTERCEPT_SENDMSG
2695 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2696 const unsigned kCmsgDataOffset =
2697 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2699 char *p = (char *)control;
2700 char *const control_end = p + controllen;
2702 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2703 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2704 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2706 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2708 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2709 sizeof(cmsg->cmsg_level));
2710 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2711 sizeof(cmsg->cmsg_type));
2713 if (cmsg->cmsg_len > kCmsgDataOffset) {
2714 char *data = p + kCmsgDataOffset;
2715 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2716 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2719 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2723 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2726 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
2735 if (msg->msg_name && msg->msg_namelen)
2736 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2737 if (msg->msg_iov && msg->msg_iovlen)
2738 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
2739 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2740 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2741 if (msg->msg_control && msg->msg_controllen)
2742 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
2745 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
2748 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
2750 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
2751 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
2753 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
2754 if (common_flags()->intercept_send && res >= 0 && msg)
2755 read_msghdr(ctx, msg, res);
2758 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
2760 #define INIT_SENDMSG
2763 #if SANITIZER_INTERCEPT_GETPEERNAME
2764 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2766 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2768 if (addrlen) addr_sz = *addrlen;
2769 // FIXME: under ASan the call below may write to freed memory and corrupt
2770 // its metadata. See
2771 // https://github.com/google/sanitizers/issues/321.
2772 int res = REAL(getpeername)(sockfd, addr, addrlen);
2773 if (!res && addr && addrlen)
2774 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2777 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2779 #define INIT_GETPEERNAME
2782 #if SANITIZER_INTERCEPT_SYSINFO
2783 INTERCEPTOR(int, sysinfo, void *info) {
2785 // FIXME: under ASan the call below may write to freed memory and corrupt
2786 // its metadata. See
2787 // https://github.com/google/sanitizers/issues/321.
2788 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2789 int res = REAL(sysinfo)(info);
2791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2794 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2796 #define INIT_SYSINFO
2799 #if SANITIZER_INTERCEPT_READDIR
2800 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2802 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2803 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2804 __sanitizer_dirent *res = REAL(opendir)(path);
2806 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2810 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2812 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2813 // FIXME: under ASan the call below may write to freed memory and corrupt
2814 // its metadata. See
2815 // https://github.com/google/sanitizers/issues/321.
2816 __sanitizer_dirent *res = REAL(readdir)(dirp);
2817 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2821 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2822 __sanitizer_dirent **result) {
2824 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2825 // FIXME: under ASan the call below may write to freed memory and corrupt
2826 // its metadata. See
2827 // https://github.com/google/sanitizers/issues/321.
2828 int res = REAL(readdir_r)(dirp, entry, result);
2830 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2832 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2837 #define INIT_READDIR \
2838 COMMON_INTERCEPT_FUNCTION(opendir); \
2839 COMMON_INTERCEPT_FUNCTION(readdir); \
2840 COMMON_INTERCEPT_FUNCTION(readdir_r);
2842 #define INIT_READDIR
2845 #if SANITIZER_INTERCEPT_READDIR64
2846 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2848 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2849 // FIXME: under ASan the call below may write to freed memory and corrupt
2850 // its metadata. See
2851 // https://github.com/google/sanitizers/issues/321.
2852 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2853 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2857 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2858 __sanitizer_dirent64 **result) {
2860 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2861 // FIXME: under ASan the call below may write to freed memory and corrupt
2862 // its metadata. See
2863 // https://github.com/google/sanitizers/issues/321.
2864 int res = REAL(readdir64_r)(dirp, entry, result);
2866 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2868 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2872 #define INIT_READDIR64 \
2873 COMMON_INTERCEPT_FUNCTION(readdir64); \
2874 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2876 #define INIT_READDIR64
2879 #if SANITIZER_INTERCEPT_PTRACE
2880 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2882 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2883 __sanitizer_iovec local_iovec;
2886 if (request == ptrace_setregs)
2887 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2888 else if (request == ptrace_setfpregs)
2889 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2890 else if (request == ptrace_setfpxregs)
2891 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2892 else if (request == ptrace_setvfpregs)
2893 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2894 else if (request == ptrace_setsiginfo)
2895 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2896 // Some kernel might zero the iovec::iov_base in case of invalid
2897 // write access. In this case copy the invalid address for further
2899 else if (request == ptrace_setregset || request == ptrace_getregset) {
2900 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2901 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
2902 local_iovec = *iovec;
2903 if (request == ptrace_setregset)
2904 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
2908 // FIXME: under ASan the call below may write to freed memory and corrupt
2909 // its metadata. See
2910 // https://github.com/google/sanitizers/issues/321.
2911 uptr res = REAL(ptrace)(request, pid, addr, data);
2914 // Note that PEEK* requests assign different meaning to the return value.
2915 // This function does not handle them (nor does it need to).
2916 if (request == ptrace_getregs)
2917 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2918 else if (request == ptrace_getfpregs)
2919 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2920 else if (request == ptrace_getfpxregs)
2921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2922 else if (request == ptrace_getvfpregs)
2923 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2924 else if (request == ptrace_getsiginfo)
2925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2926 else if (request == ptrace_geteventmsg)
2927 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2928 else if (request == ptrace_getregset) {
2929 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2930 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
2931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
2932 local_iovec.iov_len);
2938 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2943 #if SANITIZER_INTERCEPT_SETLOCALE
2944 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2946 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2948 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2949 char *res = REAL(setlocale)(category, locale);
2950 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2954 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2956 #define INIT_SETLOCALE
2959 #if SANITIZER_INTERCEPT_GETCWD
2960 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2962 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
2963 // FIXME: under ASan the call below may write to freed memory and corrupt
2964 // its metadata. See
2965 // https://github.com/google/sanitizers/issues/321.
2966 char *res = REAL(getcwd)(buf, size);
2967 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2970 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2975 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2976 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2978 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2979 // FIXME: under ASan the call below may write to freed memory and corrupt
2980 // its metadata. See
2981 // https://github.com/google/sanitizers/issues/321.
2982 char *res = REAL(get_current_dir_name)(fake);
2983 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2987 #define INIT_GET_CURRENT_DIR_NAME \
2988 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2990 #define INIT_GET_CURRENT_DIR_NAME
2993 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
2995 if (nptr == *endptr) {
2996 // No digits were found at strtol call, we need to find out the last
2997 // symbol accessed by strtoll on our own.
2998 // We get this symbol by skipping leading blanks and optional +/- sign.
2999 while (IsSpace(*nptr)) nptr++;
3000 if (*nptr == '+' || *nptr == '-') nptr++;
3001 *endptr = const_cast<char *>(nptr);
3003 CHECK(*endptr >= nptr);
3006 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3007 char **endptr, char *real_endptr, int base) {
3009 *endptr = real_endptr;
3010 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3012 // If base has unsupported value, strtol can exit with EINVAL
3013 // without reading any characters. So do additional checks only
3014 // if base is valid.
3015 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3016 if (is_valid_base) {
3017 FixRealStrtolEndptr(nptr, &real_endptr);
3019 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3020 (real_endptr - nptr) + 1 : 0);
3024 #if SANITIZER_INTERCEPT_STRTOIMAX
3025 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3027 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3028 // FIXME: under ASan the call below may write to freed memory and corrupt
3029 // its metadata. See
3030 // https://github.com/google/sanitizers/issues/321.
3032 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3033 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3037 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3039 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3040 // FIXME: under ASan the call below may write to freed memory and corrupt
3041 // its metadata. See
3042 // https://github.com/google/sanitizers/issues/321.
3044 INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3045 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3049 #define INIT_STRTOIMAX \
3050 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3051 COMMON_INTERCEPT_FUNCTION(strtoumax);
3053 #define INIT_STRTOIMAX
3056 #if SANITIZER_INTERCEPT_MBSTOWCS
3057 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3059 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3060 // FIXME: under ASan the call below may write to freed memory and corrupt
3061 // its metadata. See
3062 // https://github.com/google/sanitizers/issues/321.
3063 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3064 if (res != (SIZE_T) - 1 && dest) {
3065 SIZE_T write_cnt = res + (res < len);
3066 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3071 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3074 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3075 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3076 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3077 // FIXME: under ASan the call below may write to freed memory and corrupt
3078 // its metadata. See
3079 // https://github.com/google/sanitizers/issues/321.
3080 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3081 if (res != (SIZE_T)(-1) && dest && src) {
3082 // This function, and several others, may or may not write the terminating
3083 // \0 character. They write it iff they clear *src.
3084 SIZE_T write_cnt = res + !*src;
3085 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3090 #define INIT_MBSTOWCS \
3091 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3092 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3094 #define INIT_MBSTOWCS
3097 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3098 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3099 SIZE_T len, void *ps) {
3101 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3103 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3104 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3106 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3107 // FIXME: under ASan the call below may write to freed memory and corrupt
3108 // its metadata. See
3109 // https://github.com/google/sanitizers/issues/321.
3110 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3111 if (res != (SIZE_T)(-1) && dest && src) {
3112 SIZE_T write_cnt = res + !*src;
3113 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3118 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3120 #define INIT_MBSNRTOWCS
3123 #if SANITIZER_INTERCEPT_WCSTOMBS
3124 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3126 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3127 // FIXME: under ASan the call below may write to freed memory and corrupt
3128 // its metadata. See
3129 // https://github.com/google/sanitizers/issues/321.
3130 SIZE_T res = REAL(wcstombs)(dest, src, len);
3131 if (res != (SIZE_T) - 1 && dest) {
3132 SIZE_T write_cnt = res + (res < len);
3133 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3138 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3141 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3142 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3143 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3144 // FIXME: under ASan the call below may write to freed memory and corrupt
3145 // its metadata. See
3146 // https://github.com/google/sanitizers/issues/321.
3147 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3148 if (res != (SIZE_T) - 1 && dest && src) {
3149 SIZE_T write_cnt = res + !*src;
3150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3155 #define INIT_WCSTOMBS \
3156 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3157 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3159 #define INIT_WCSTOMBS
3162 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3163 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3164 SIZE_T len, void *ps) {
3166 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3168 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3169 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3171 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3172 // FIXME: under ASan the call below may write to freed memory and corrupt
3173 // its metadata. See
3174 // https://github.com/google/sanitizers/issues/321.
3175 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3176 if (res != ((SIZE_T)-1) && dest && src) {
3177 SIZE_T write_cnt = res + !*src;
3178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3183 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3185 #define INIT_WCSNRTOMBS
3189 #if SANITIZER_INTERCEPT_WCRTOMB
3190 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3192 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3193 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3194 // FIXME: under ASan the call below may write to freed memory and corrupt
3195 // its metadata. See
3196 // https://github.com/google/sanitizers/issues/321.
3197 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3198 if (res != ((SIZE_T)-1) && dest) {
3199 SIZE_T write_cnt = res;
3200 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3205 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3207 #define INIT_WCRTOMB
3210 #if SANITIZER_INTERCEPT_TCGETATTR
3211 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3213 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3214 // FIXME: under ASan the call below may write to freed memory and corrupt
3215 // its metadata. See
3216 // https://github.com/google/sanitizers/issues/321.
3217 int res = REAL(tcgetattr)(fd, termios_p);
3218 if (!res && termios_p)
3219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3223 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3225 #define INIT_TCGETATTR
3228 #if SANITIZER_INTERCEPT_REALPATH
3229 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3231 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3232 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3234 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3235 // version of a versioned symbol. For realpath(), this gives us something
3236 // (called __old_realpath) that does not handle NULL in the second argument.
3237 // Handle it as part of the interceptor.
3238 char *allocated_path = nullptr;
3240 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3242 char *res = REAL(realpath)(path, resolved_path);
3243 if (allocated_path && !res) WRAP(free)(allocated_path);
3244 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3247 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3249 #define INIT_REALPATH
3252 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3253 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3255 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3256 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3257 char *res = REAL(canonicalize_file_name)(path);
3258 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3261 #define INIT_CANONICALIZE_FILE_NAME \
3262 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3264 #define INIT_CANONICALIZE_FILE_NAME
3267 #if SANITIZER_INTERCEPT_CONFSTR
3268 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3270 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3271 // FIXME: under ASan the call below may write to freed memory and corrupt
3272 // its metadata. See
3273 // https://github.com/google/sanitizers/issues/321.
3274 SIZE_T res = REAL(confstr)(name, buf, len);
3276 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3279 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3281 #define INIT_CONFSTR
3284 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3285 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3287 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3288 // FIXME: under ASan the call below may write to freed memory and corrupt
3289 // its metadata. See
3290 // https://github.com/google/sanitizers/issues/321.
3291 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3292 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3295 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3297 #define INIT_SCHED_GETAFFINITY
3300 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3301 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3303 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3304 int res = REAL(sched_getparam)(pid, param);
3305 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3308 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3310 #define INIT_SCHED_GETPARAM
3313 #if SANITIZER_INTERCEPT_STRERROR
3314 INTERCEPTOR(char *, strerror, int errnum) {
3316 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3317 char *res = REAL(strerror)(errnum);
3318 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3321 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3323 #define INIT_STRERROR
3326 #if SANITIZER_INTERCEPT_STRERROR_R
3327 // There are 2 versions of strerror_r:
3328 // * POSIX version returns 0 on success, negative error code on failure,
3329 // writes message to buf.
3330 // * GNU version returns message pointer, which points to either buf or some
3332 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3333 SANITIZER_MAC || SANITIZER_ANDROID
3334 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3335 // At least on OSX, buf contents are valid even when the call fails.
3336 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3338 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
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.
3342 int res = REAL(strerror_r)(errnum, buf, buflen);
3344 SIZE_T sz = internal_strnlen(buf, buflen);
3345 if (sz < buflen) ++sz;
3346 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3351 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3353 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3354 // FIXME: under ASan the call below may write to freed memory and corrupt
3355 // its metadata. See
3356 // https://github.com/google/sanitizers/issues/321.
3357 char *res = REAL(strerror_r)(errnum, buf, buflen);
3358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3361 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3363 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3365 #define INIT_STRERROR_R
3368 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3369 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3371 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3372 // FIXME: under ASan the call below may write to freed memory and corrupt
3373 // its metadata. See
3374 // https://github.com/google/sanitizers/issues/321.
3375 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3376 // This version always returns a null-terminated string.
3378 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3381 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3383 #define INIT_XPG_STRERROR_R
3386 #if SANITIZER_INTERCEPT_SCANDIR
3387 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3388 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3389 const struct __sanitizer_dirent **);
3391 static THREADLOCAL scandir_filter_f scandir_filter;
3392 static THREADLOCAL scandir_compar_f scandir_compar;
3394 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3395 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3396 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3397 return scandir_filter(dir);
3400 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3401 const struct __sanitizer_dirent **b) {
3402 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3403 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3404 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3405 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3406 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3407 return scandir_compar(a, b);
3410 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3411 scandir_filter_f filter, scandir_compar_f compar) {
3413 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3414 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3415 scandir_filter = filter;
3416 scandir_compar = compar;
3417 // FIXME: under ASan the call below may write to freed memory and corrupt
3418 // its metadata. See
3419 // https://github.com/google/sanitizers/issues/321.
3420 int res = REAL(scandir)(dirp, namelist,
3421 filter ? wrapped_scandir_filter : nullptr,
3422 compar ? wrapped_scandir_compar : nullptr);
3423 scandir_filter = nullptr;
3424 scandir_compar = nullptr;
3425 if (namelist && res > 0) {
3426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3427 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3428 for (int i = 0; i < res; ++i)
3429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3430 (*namelist)[i]->d_reclen);
3434 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3436 #define INIT_SCANDIR
3439 #if SANITIZER_INTERCEPT_SCANDIR64
3440 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3441 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3442 const struct __sanitizer_dirent64 **);
3444 static THREADLOCAL scandir64_filter_f scandir64_filter;
3445 static THREADLOCAL scandir64_compar_f scandir64_compar;
3447 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3448 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3449 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3450 return scandir64_filter(dir);
3453 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3454 const struct __sanitizer_dirent64 **b) {
3455 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3456 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3457 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3458 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3459 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3460 return scandir64_compar(a, b);
3463 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3464 scandir64_filter_f filter, scandir64_compar_f compar) {
3466 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3467 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3468 scandir64_filter = filter;
3469 scandir64_compar = compar;
3470 // FIXME: under ASan the call below may write to freed memory and corrupt
3471 // its metadata. See
3472 // https://github.com/google/sanitizers/issues/321.
3474 REAL(scandir64)(dirp, namelist,
3475 filter ? wrapped_scandir64_filter : nullptr,
3476 compar ? wrapped_scandir64_compar : nullptr);
3477 scandir64_filter = nullptr;
3478 scandir64_compar = nullptr;
3479 if (namelist && res > 0) {
3480 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3482 for (int i = 0; i < res; ++i)
3483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3484 (*namelist)[i]->d_reclen);
3488 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3490 #define INIT_SCANDIR64
3493 #if SANITIZER_INTERCEPT_GETGROUPS
3494 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3496 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3497 // FIXME: under ASan the call below may write to freed memory and corrupt
3498 // its metadata. See
3499 // https://github.com/google/sanitizers/issues/321.
3500 int res = REAL(getgroups)(size, lst);
3501 if (res >= 0 && lst && size > 0)
3502 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3505 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3507 #define INIT_GETGROUPS
3510 #if SANITIZER_INTERCEPT_POLL
3511 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3512 __sanitizer_nfds_t nfds) {
3513 for (unsigned i = 0; i < nfds; ++i) {
3514 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3515 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3519 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3520 __sanitizer_nfds_t nfds) {
3521 for (unsigned i = 0; i < nfds; ++i)
3522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3523 sizeof(fds[i].revents));
3526 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3529 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3530 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3531 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3532 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3535 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3540 #if SANITIZER_INTERCEPT_PPOLL
3541 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3542 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3544 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3545 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3547 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3548 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3550 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3551 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3554 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3559 #if SANITIZER_INTERCEPT_WORDEXP
3560 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3562 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3563 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3564 // FIXME: under ASan the call below may write to freed memory and corrupt
3565 // its metadata. See
3566 // https://github.com/google/sanitizers/issues/321.
3567 int res = REAL(wordexp)(s, p, flags);
3569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3571 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3572 sizeof(*p->we_wordv) * p->we_wordc);
3573 for (uptr i = 0; i < p->we_wordc; ++i) {
3574 char *w = p->we_wordv[i];
3575 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3580 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3582 #define INIT_WORDEXP
3585 #if SANITIZER_INTERCEPT_SIGWAIT
3586 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3588 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3589 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3590 // FIXME: under ASan the call below may write to freed memory and corrupt
3591 // its metadata. See
3592 // https://github.com/google/sanitizers/issues/321.
3593 int res = REAL(sigwait)(set, sig);
3594 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3597 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3599 #define INIT_SIGWAIT
3602 #if SANITIZER_INTERCEPT_SIGWAITINFO
3603 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3605 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3606 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3607 // FIXME: under ASan the call below may write to freed memory and corrupt
3608 // its metadata. See
3609 // https://github.com/google/sanitizers/issues/321.
3610 int res = REAL(sigwaitinfo)(set, info);
3611 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3614 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3616 #define INIT_SIGWAITINFO
3619 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3620 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3623 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3624 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3625 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3626 // FIXME: under ASan the call below may write to freed memory and corrupt
3627 // its metadata. See
3628 // https://github.com/google/sanitizers/issues/321.
3629 int res = REAL(sigtimedwait)(set, info, timeout);
3630 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3633 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3635 #define INIT_SIGTIMEDWAIT
3638 #if SANITIZER_INTERCEPT_SIGSETOPS
3639 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3641 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3642 // FIXME: under ASan the call below may write to freed memory and corrupt
3643 // its metadata. See
3644 // https://github.com/google/sanitizers/issues/321.
3645 int res = REAL(sigemptyset)(set);
3646 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3650 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3652 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3653 // FIXME: under ASan the call below may write to freed memory and corrupt
3654 // its metadata. See
3655 // https://github.com/google/sanitizers/issues/321.
3656 int res = REAL(sigfillset)(set);
3657 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3660 #define INIT_SIGSETOPS \
3661 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3662 COMMON_INTERCEPT_FUNCTION(sigfillset);
3664 #define INIT_SIGSETOPS
3667 #if SANITIZER_INTERCEPT_SIGPENDING
3668 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3670 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3671 // FIXME: under ASan the call below may write to freed memory and corrupt
3672 // its metadata. See
3673 // https://github.com/google/sanitizers/issues/321.
3674 int res = REAL(sigpending)(set);
3675 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3678 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3680 #define INIT_SIGPENDING
3683 #if SANITIZER_INTERCEPT_SIGPROCMASK
3684 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3685 __sanitizer_sigset_t *oldset) {
3687 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3688 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3689 // FIXME: under ASan the call below may write to freed memory and corrupt
3690 // its metadata. See
3691 // https://github.com/google/sanitizers/issues/321.
3692 int res = REAL(sigprocmask)(how, set, oldset);
3694 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3697 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3699 #define INIT_SIGPROCMASK
3702 #if SANITIZER_INTERCEPT_BACKTRACE
3703 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3705 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3706 // FIXME: under ASan the call below may write to freed memory and corrupt
3707 // its metadata. See
3708 // https://github.com/google/sanitizers/issues/321.
3709 int res = REAL(backtrace)(buffer, size);
3711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3715 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3717 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3719 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3720 // FIXME: under ASan the call below may write to freed memory and corrupt
3721 // its metadata. See
3722 // https://github.com/google/sanitizers/issues/321.
3723 char **res = REAL(backtrace_symbols)(buffer, size);
3725 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3726 for (int i = 0; i < size; ++i)
3727 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3731 #define INIT_BACKTRACE \
3732 COMMON_INTERCEPT_FUNCTION(backtrace); \
3733 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3735 #define INIT_BACKTRACE
3738 #if SANITIZER_INTERCEPT__EXIT
3739 INTERCEPTOR(void, _exit, int status) {
3741 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3742 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3743 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3744 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3745 if (status == 0) status = status1;
3746 REAL(_exit)(status);
3748 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3753 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3754 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3756 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3757 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
3758 int res = REAL(pthread_mutex_lock)(m);
3759 if (res == errno_EOWNERDEAD)
3760 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3761 if (res == 0 || res == errno_EOWNERDEAD)
3762 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
3763 if (res == errno_EINVAL)
3764 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3768 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3770 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3771 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3772 int res = REAL(pthread_mutex_unlock)(m);
3773 if (res == errno_EINVAL)
3774 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3778 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3779 #define INIT_PTHREAD_MUTEX_UNLOCK \
3780 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3782 #define INIT_PTHREAD_MUTEX_LOCK
3783 #define INIT_PTHREAD_MUTEX_UNLOCK
3786 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3787 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3789 if (mnt->mnt_fsname)
3790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3791 REAL(strlen)(mnt->mnt_fsname) + 1);
3793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3794 REAL(strlen)(mnt->mnt_dir) + 1);
3796 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3797 REAL(strlen)(mnt->mnt_type) + 1);
3799 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3800 REAL(strlen)(mnt->mnt_opts) + 1);
3804 #if SANITIZER_INTERCEPT_GETMNTENT
3805 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3807 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3808 __sanitizer_mntent *res = REAL(getmntent)(fp);
3809 if (res) write_mntent(ctx, res);
3812 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3814 #define INIT_GETMNTENT
3817 #if SANITIZER_INTERCEPT_GETMNTENT_R
3818 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3819 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3821 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3822 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3823 if (res) write_mntent(ctx, res);
3826 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3828 #define INIT_GETMNTENT_R
3831 #if SANITIZER_INTERCEPT_STATFS
3832 INTERCEPTOR(int, statfs, char *path, void *buf) {
3834 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3835 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3836 // FIXME: under ASan the call below may write to freed memory and corrupt
3837 // its metadata. See
3838 // https://github.com/google/sanitizers/issues/321.
3839 int res = REAL(statfs)(path, buf);
3840 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3843 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3845 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3846 // FIXME: under ASan the call below may write to freed memory and corrupt
3847 // its metadata. See
3848 // https://github.com/google/sanitizers/issues/321.
3849 int res = REAL(fstatfs)(fd, buf);
3850 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3853 #define INIT_STATFS \
3854 COMMON_INTERCEPT_FUNCTION(statfs); \
3855 COMMON_INTERCEPT_FUNCTION(fstatfs);
3860 #if SANITIZER_INTERCEPT_STATFS64
3861 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3863 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3864 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3865 // FIXME: under ASan the call below may write to freed memory and corrupt
3866 // its metadata. See
3867 // https://github.com/google/sanitizers/issues/321.
3868 int res = REAL(statfs64)(path, buf);
3869 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3872 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3874 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3875 // FIXME: under ASan the call below may write to freed memory and corrupt
3876 // its metadata. See
3877 // https://github.com/google/sanitizers/issues/321.
3878 int res = REAL(fstatfs64)(fd, buf);
3879 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3882 #define INIT_STATFS64 \
3883 COMMON_INTERCEPT_FUNCTION(statfs64); \
3884 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3886 #define INIT_STATFS64
3889 #if SANITIZER_INTERCEPT_STATVFS
3890 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3892 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3893 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3894 // FIXME: under ASan the call below may write to freed memory and corrupt
3895 // its metadata. See
3896 // https://github.com/google/sanitizers/issues/321.
3897 int res = REAL(statvfs)(path, buf);
3898 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3901 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3903 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3904 // FIXME: under ASan the call below may write to freed memory and corrupt
3905 // its metadata. See
3906 // https://github.com/google/sanitizers/issues/321.
3907 int res = REAL(fstatvfs)(fd, buf);
3908 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3911 #define INIT_STATVFS \
3912 COMMON_INTERCEPT_FUNCTION(statvfs); \
3913 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3915 #define INIT_STATVFS
3918 #if SANITIZER_INTERCEPT_STATVFS64
3919 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3921 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3922 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3923 // FIXME: under ASan the call below may write to freed memory and corrupt
3924 // its metadata. See
3925 // https://github.com/google/sanitizers/issues/321.
3926 int res = REAL(statvfs64)(path, buf);
3927 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3930 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3932 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3933 // FIXME: under ASan the call below may write to freed memory and corrupt
3934 // its metadata. See
3935 // https://github.com/google/sanitizers/issues/321.
3936 int res = REAL(fstatvfs64)(fd, buf);
3937 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3940 #define INIT_STATVFS64 \
3941 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3942 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3944 #define INIT_STATVFS64
3947 #if SANITIZER_INTERCEPT_INITGROUPS
3948 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3950 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3951 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3952 int res = REAL(initgroups)(user, group);
3955 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3957 #define INIT_INITGROUPS
3960 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3961 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3963 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3964 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3965 char *res = REAL(ether_ntoa)(addr);
3966 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3969 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3971 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3972 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3973 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3974 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3977 #define INIT_ETHER_NTOA_ATON \
3978 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3979 COMMON_INTERCEPT_FUNCTION(ether_aton);
3981 #define INIT_ETHER_NTOA_ATON
3984 #if SANITIZER_INTERCEPT_ETHER_HOST
3985 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3987 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3988 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3989 // FIXME: under ASan the call below may write to freed memory and corrupt
3990 // its metadata. See
3991 // https://github.com/google/sanitizers/issues/321.
3992 int res = REAL(ether_ntohost)(hostname, addr);
3993 if (!res && hostname)
3994 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3997 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3999 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4001 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4002 // FIXME: under ASan the call below may write to freed memory and corrupt
4003 // its metadata. See
4004 // https://github.com/google/sanitizers/issues/321.
4005 int res = REAL(ether_hostton)(hostname, addr);
4006 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4009 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4012 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4013 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4014 // FIXME: under ASan the call below may write to freed memory and corrupt
4015 // its metadata. See
4016 // https://github.com/google/sanitizers/issues/321.
4017 int res = REAL(ether_line)(line, addr, hostname);
4019 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4021 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4025 #define INIT_ETHER_HOST \
4026 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4027 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4028 COMMON_INTERCEPT_FUNCTION(ether_line);
4030 #define INIT_ETHER_HOST
4033 #if SANITIZER_INTERCEPT_ETHER_R
4034 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4036 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4037 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4038 // FIXME: under ASan the call below may write to freed memory and corrupt
4039 // its metadata. See
4040 // https://github.com/google/sanitizers/issues/321.
4041 char *res = REAL(ether_ntoa_r)(addr, buf);
4042 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4045 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4046 __sanitizer_ether_addr *addr) {
4048 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4049 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4050 // FIXME: under ASan the call below may write to freed memory and corrupt
4051 // its metadata. See
4052 // https://github.com/google/sanitizers/issues/321.
4053 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4054 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4057 #define INIT_ETHER_R \
4058 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4059 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4061 #define INIT_ETHER_R
4064 #if SANITIZER_INTERCEPT_SHMCTL
4065 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4067 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4068 // FIXME: under ASan the call below may write to freed memory and corrupt
4069 // its metadata. See
4070 // https://github.com/google/sanitizers/issues/321.
4071 int res = REAL(shmctl)(shmid, cmd, buf);
4074 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4075 sz = sizeof(__sanitizer_shmid_ds);
4076 else if (cmd == shmctl_ipc_info)
4077 sz = struct_shminfo_sz;
4078 else if (cmd == shmctl_shm_info)
4079 sz = struct_shm_info_sz;
4080 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4084 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4089 #if SANITIZER_INTERCEPT_RANDOM_R
4090 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4092 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4093 // FIXME: under ASan the call below may write to freed memory and corrupt
4094 // its metadata. See
4095 // https://github.com/google/sanitizers/issues/321.
4096 int res = REAL(random_r)(buf, result);
4098 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4101 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4103 #define INIT_RANDOM_R
4106 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4107 // its metadata. See
4108 // https://github.com/google/sanitizers/issues/321.
4109 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4110 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4111 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4112 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4113 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4114 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4115 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4116 INTERCEPTOR(int, fn, void *attr, void *r) { \
4118 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4119 int res = REAL(fn)(attr, r); \
4120 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4123 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4124 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4125 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4126 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4127 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4128 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4129 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4130 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4131 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4132 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4135 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4136 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4137 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4138 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4139 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4140 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4141 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4142 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4144 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4145 // FIXME: under ASan the call below may write to freed memory and corrupt
4146 // its metadata. See
4147 // https://github.com/google/sanitizers/issues/321.
4148 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4150 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4151 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4156 // We may need to call the real pthread_attr_getstack from the run-time
4157 // in sanitizer_common, but we don't want to include the interception headers
4158 // there. So, just define this function here.
4159 namespace __sanitizer {
4161 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4162 return REAL(pthread_attr_getstack)(attr, addr, size);
4165 } // namespace __sanitizer
4167 #define INIT_PTHREAD_ATTR_GET \
4168 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4169 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4170 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4171 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4172 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4173 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4174 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4176 #define INIT_PTHREAD_ATTR_GET
4179 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4180 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4182 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4183 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4185 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4188 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4189 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4192 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4194 // FIXME: under ASan the call below may write to freed memory and corrupt
4195 // its metadata. See
4196 // https://github.com/google/sanitizers/issues/321.
4197 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4198 if (!res && cpusetsize && cpuset)
4199 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4203 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4204 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4206 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4209 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4210 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4211 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4212 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4214 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4217 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4218 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4219 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4220 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4222 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4225 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4226 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4227 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4228 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4230 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4233 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4234 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4235 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4236 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4238 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4241 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4242 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4243 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4244 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4246 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4249 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4250 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4251 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4252 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4254 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4257 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4258 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4259 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4260 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4262 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4265 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4266 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4267 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4268 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4270 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4273 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4274 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4275 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4276 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4278 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4281 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4282 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4283 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4284 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4286 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4289 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4290 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4291 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4292 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4294 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4297 #if SANITIZER_INTERCEPT_TMPNAM
4298 INTERCEPTOR(char *, tmpnam, char *s) {
4300 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4301 char *res = REAL(tmpnam)(s);
4304 // FIXME: under ASan the call below may write to freed memory and corrupt
4305 // its metadata. See
4306 // https://github.com/google/sanitizers/issues/321.
4307 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4309 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4313 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4318 #if SANITIZER_INTERCEPT_TMPNAM_R
4319 INTERCEPTOR(char *, tmpnam_r, char *s) {
4321 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4322 // FIXME: under ASan the call below may write to freed memory and corrupt
4323 // its metadata. See
4324 // https://github.com/google/sanitizers/issues/321.
4325 char *res = REAL(tmpnam_r)(s);
4326 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4329 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4331 #define INIT_TMPNAM_R
4334 #if SANITIZER_INTERCEPT_TTYNAME_R
4335 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4337 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4338 int res = REAL(ttyname_r)(fd, name, namesize);
4340 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4343 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4345 #define INIT_TTYNAME_R
4348 #if SANITIZER_INTERCEPT_TEMPNAM
4349 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4351 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4352 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4353 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4354 char *res = REAL(tempnam)(dir, pfx);
4355 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4358 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4360 #define INIT_TEMPNAM
4363 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4364 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4366 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4367 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4368 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4369 return REAL(pthread_setname_np)(thread, name);
4371 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4373 #define INIT_PTHREAD_SETNAME_NP
4376 #if SANITIZER_INTERCEPT_SINCOS
4377 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4379 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4380 // FIXME: under ASan the call below may write to freed memory and corrupt
4381 // its metadata. See
4382 // https://github.com/google/sanitizers/issues/321.
4383 REAL(sincos)(x, sin, cos);
4384 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4385 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4387 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4389 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4390 // FIXME: under ASan the call below may write to freed memory and corrupt
4391 // its metadata. See
4392 // https://github.com/google/sanitizers/issues/321.
4393 REAL(sincosf)(x, sin, cos);
4394 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4395 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4397 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4399 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4400 // FIXME: under ASan the call below may write to freed memory and corrupt
4401 // its metadata. See
4402 // https://github.com/google/sanitizers/issues/321.
4403 REAL(sincosl)(x, sin, cos);
4404 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4405 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4407 #define INIT_SINCOS \
4408 COMMON_INTERCEPT_FUNCTION(sincos); \
4409 COMMON_INTERCEPT_FUNCTION(sincosf); \
4410 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4415 #if SANITIZER_INTERCEPT_REMQUO
4416 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4418 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4419 // FIXME: under ASan the call below may write to freed memory and corrupt
4420 // its metadata. See
4421 // https://github.com/google/sanitizers/issues/321.
4422 double res = REAL(remquo)(x, y, quo);
4423 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4426 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4428 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4429 // FIXME: under ASan the call below may write to freed memory and corrupt
4430 // its metadata. See
4431 // https://github.com/google/sanitizers/issues/321.
4432 float res = REAL(remquof)(x, y, quo);
4433 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4436 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4438 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4439 // FIXME: under ASan the call below may write to freed memory and corrupt
4440 // its metadata. See
4441 // https://github.com/google/sanitizers/issues/321.
4442 long double res = REAL(remquol)(x, y, quo);
4443 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4446 #define INIT_REMQUO \
4447 COMMON_INTERCEPT_FUNCTION(remquo); \
4448 COMMON_INTERCEPT_FUNCTION(remquof); \
4449 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4454 #if SANITIZER_INTERCEPT_LGAMMA
4456 INTERCEPTOR(double, lgamma, double x) {
4458 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4459 double res = REAL(lgamma)(x);
4460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4463 INTERCEPTOR(float, lgammaf, float x) {
4465 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4466 float res = REAL(lgammaf)(x);
4467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4470 INTERCEPTOR(long double, lgammal, long double x) {
4472 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4473 long double res = REAL(lgammal)(x);
4474 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4477 #define INIT_LGAMMA \
4478 COMMON_INTERCEPT_FUNCTION(lgamma); \
4479 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4480 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4485 #if SANITIZER_INTERCEPT_LGAMMA_R
4486 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4488 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4489 // FIXME: under ASan the call below may write to freed memory and corrupt
4490 // its metadata. See
4491 // https://github.com/google/sanitizers/issues/321.
4492 double res = REAL(lgamma_r)(x, signp);
4493 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4496 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4498 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4499 // FIXME: under ASan the call below may write to freed memory and corrupt
4500 // its metadata. See
4501 // https://github.com/google/sanitizers/issues/321.
4502 float res = REAL(lgammaf_r)(x, signp);
4503 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4506 #define INIT_LGAMMA_R \
4507 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4508 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4510 #define INIT_LGAMMA_R
4513 #if SANITIZER_INTERCEPT_LGAMMAL_R
4514 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4516 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4517 // FIXME: under ASan the call below may write to freed memory and corrupt
4518 // its metadata. See
4519 // https://github.com/google/sanitizers/issues/321.
4520 long double res = REAL(lgammal_r)(x, signp);
4521 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4524 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4526 #define INIT_LGAMMAL_R
4529 #if SANITIZER_INTERCEPT_DRAND48_R
4530 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4532 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4533 // FIXME: under ASan the call below may write to freed memory and corrupt
4534 // its metadata. See
4535 // https://github.com/google/sanitizers/issues/321.
4536 int res = REAL(drand48_r)(buffer, result);
4537 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4540 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4542 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4543 // FIXME: under ASan the call below may write to freed memory and corrupt
4544 // its metadata. See
4545 // https://github.com/google/sanitizers/issues/321.
4546 int res = REAL(lrand48_r)(buffer, result);
4547 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4550 #define INIT_DRAND48_R \
4551 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4552 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4554 #define INIT_DRAND48_R
4557 #if SANITIZER_INTERCEPT_RAND_R
4558 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4560 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4561 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4562 return REAL(rand_r)(seedp);
4564 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4569 #if SANITIZER_INTERCEPT_GETLINE
4570 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4572 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4573 // FIXME: under ASan the call below may write to freed memory and corrupt
4574 // its metadata. See
4575 // https://github.com/google/sanitizers/issues/321.
4576 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4580 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4585 // FIXME: under ASan the call below may write to freed memory and corrupt its
4587 // https://github.com/google/sanitizers/issues/321.
4588 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4591 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4592 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4594 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4601 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4603 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4605 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4606 // with its own body.
4607 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4609 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4611 #define INIT_GETLINE \
4612 COMMON_INTERCEPT_FUNCTION(getline); \
4613 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4614 COMMON_INTERCEPT_FUNCTION(getdelim);
4616 #define INIT_GETLINE
4619 #if SANITIZER_INTERCEPT_ICONV
4620 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4621 char **outbuf, SIZE_T *outbytesleft) {
4623 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4626 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4627 if (inbuf && inbytesleft)
4628 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4630 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4631 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4632 // FIXME: under ASan the call below may write to freed memory and corrupt
4633 // its metadata. See
4634 // https://github.com/google/sanitizers/issues/321.
4635 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4636 if (outbuf && *outbuf > outbuf_orig) {
4637 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4642 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4647 #if SANITIZER_INTERCEPT_TIMES
4648 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4650 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4651 // FIXME: under ASan the call below may write to freed memory and corrupt
4652 // its metadata. See
4653 // https://github.com/google/sanitizers/issues/321.
4654 __sanitizer_clock_t res = REAL(times)(tms);
4655 if (res != (__sanitizer_clock_t)-1 && tms)
4656 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4659 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4664 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4666 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4667 // If you see any crashes around this functions, there are 2 known issues with
4668 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4669 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4670 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4671 // to access thread local variables (it should not happen normally,
4672 // because sanitizers use initial-exec tls model).
4673 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4675 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4676 void *res = REAL(__tls_get_addr)(arg);
4677 uptr tls_begin, tls_end;
4678 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4679 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4681 // New DTLS block has been allocated.
4682 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4687 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4688 // mostly the same semantics as __tls_get_addr, but its presence enables
4689 // some optimizations in linker (which are safe to ignore here).
4690 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4691 visibility("default")))
4692 void *__tls_get_addr_opt(void *arg);
4694 #else // SANITIZER_S390
4695 // On s390, we have to intercept two functions here:
4696 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4697 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4698 // a proper pointer. It is used by dlsym for TLS symbols.
4699 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4700 // descriptor offset as an argument instead of a pointer. GOT address
4701 // is passed in r12, so it's necessary to write it in assembly. This is
4702 // the function used by the compiler.
4703 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
4704 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
4705 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
4706 extern "C" uptr __tls_get_offset(void *arg);
4707 extern "C" uptr __interceptor___tls_get_offset(void *arg);
4708 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4710 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4711 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
4712 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4713 void *ptr = reinterpret_cast<void *>(res + tp);
4714 uptr tls_begin, tls_end;
4715 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4716 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4718 // New DTLS block has been allocated.
4719 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4723 // We need a hidden symbol aliasing the above, so that we can jump
4724 // directly to it from the assembly below.
4725 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4726 visibility("hidden")))
4727 uptr __tls_get_addr_hidden(void *arg);
4728 // Now carefully intercept __tls_get_offset.
4731 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4732 // exports our symbol.
4733 ".weak __tls_get_offset\n"
4734 ".type __tls_get_offset, @function\n"
4735 "__tls_get_offset:\n"
4736 ".global __interceptor___tls_get_offset\n"
4737 ".type __interceptor___tls_get_offset, @function\n"
4738 "__interceptor___tls_get_offset:\n"
4740 "la %r2, 0(%r2,%r12)\n"
4741 "jg __tls_get_addr_hidden\n"
4744 "0: la %r2,0(%r2,%r12)\n"
4745 "l %r4,1f-0b(%r3)\n"
4747 "1: .long __tls_get_addr_hidden - 0b\n"
4749 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
4750 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
4751 ".type __tls_get_offset_wrapper, @function\n"
4752 "__tls_get_offset_wrapper:\n"
4759 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
4761 #endif // SANITIZER_S390
4763 #define INIT_TLS_GET_ADDR
4766 #if SANITIZER_INTERCEPT_LISTXATTR
4767 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4769 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4770 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4771 // FIXME: under ASan the call below may write to freed memory and corrupt
4772 // its metadata. See
4773 // https://github.com/google/sanitizers/issues/321.
4774 SSIZE_T res = REAL(listxattr)(path, list, size);
4775 // Here and below, size == 0 is a special case where nothing is written to the
4776 // buffer, and res contains the desired buffer size.
4777 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4780 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4782 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4783 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4784 // FIXME: under ASan the call below may write to freed memory and corrupt
4785 // its metadata. See
4786 // https://github.com/google/sanitizers/issues/321.
4787 SSIZE_T res = REAL(llistxattr)(path, list, size);
4788 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4791 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4793 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4794 // FIXME: under ASan the call below may write to freed memory and corrupt
4795 // its metadata. See
4796 // https://github.com/google/sanitizers/issues/321.
4797 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4798 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4801 #define INIT_LISTXATTR \
4802 COMMON_INTERCEPT_FUNCTION(listxattr); \
4803 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4804 COMMON_INTERCEPT_FUNCTION(flistxattr);
4806 #define INIT_LISTXATTR
4809 #if SANITIZER_INTERCEPT_GETXATTR
4810 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4813 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4814 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4815 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4816 // FIXME: under ASan the call below may write to freed memory and corrupt
4817 // its metadata. See
4818 // https://github.com/google/sanitizers/issues/321.
4819 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4820 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4823 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4826 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4827 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4828 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4829 // FIXME: under ASan the call below may write to freed memory and corrupt
4830 // its metadata. See
4831 // https://github.com/google/sanitizers/issues/321.
4832 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4833 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4836 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4839 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4840 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4841 // FIXME: under ASan the call below may write to freed memory and corrupt
4842 // its metadata. See
4843 // https://github.com/google/sanitizers/issues/321.
4844 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4845 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4848 #define INIT_GETXATTR \
4849 COMMON_INTERCEPT_FUNCTION(getxattr); \
4850 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4851 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4853 #define INIT_GETXATTR
4856 #if SANITIZER_INTERCEPT_GETRESID
4857 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4859 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4860 // FIXME: under ASan the call below may write to freed memory and corrupt
4861 // its metadata. See
4862 // https://github.com/google/sanitizers/issues/321.
4863 int res = REAL(getresuid)(ruid, euid, suid);
4865 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4866 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4867 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4871 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4873 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4874 // FIXME: under ASan the call below may write to freed memory and corrupt
4875 // its metadata. See
4876 // https://github.com/google/sanitizers/issues/321.
4877 int res = REAL(getresgid)(rgid, egid, sgid);
4879 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4880 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4881 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4885 #define INIT_GETRESID \
4886 COMMON_INTERCEPT_FUNCTION(getresuid); \
4887 COMMON_INTERCEPT_FUNCTION(getresgid);
4889 #define INIT_GETRESID
4892 #if SANITIZER_INTERCEPT_GETIFADDRS
4893 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4894 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4895 // intercept it to poison the memory again.
4896 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4898 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4899 // FIXME: under ASan the call below may write to freed memory and corrupt
4900 // its metadata. See
4901 // https://github.com/google/sanitizers/issues/321.
4902 int res = REAL(getifaddrs)(ifap);
4903 if (res == 0 && ifap) {
4904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4905 __sanitizer_ifaddrs *p = *ifap;
4907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4910 REAL(strlen)(p->ifa_name) + 1);
4912 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4914 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4915 // On Linux this is a union, but the other member also points to a
4916 // struct sockaddr, so the following is sufficient.
4918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4919 // FIXME(smatveev): Unpoison p->ifa_data as well.
4925 #define INIT_GETIFADDRS \
4926 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4928 #define INIT_GETIFADDRS
4931 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4932 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4934 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4935 // FIXME: under ASan the call below may write to freed memory and corrupt
4936 // its metadata. See
4937 // https://github.com/google/sanitizers/issues/321.
4938 char *res = REAL(if_indextoname)(ifindex, ifname);
4940 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4943 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4945 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4947 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4948 return REAL(if_nametoindex)(ifname);
4950 #define INIT_IF_INDEXTONAME \
4951 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4952 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4954 #define INIT_IF_INDEXTONAME
4957 #if SANITIZER_INTERCEPT_CAPGET
4958 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4960 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4962 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4963 // FIXME: under ASan the call below may write to freed memory and corrupt
4964 // its metadata. See
4965 // https://github.com/google/sanitizers/issues/321.
4966 int res = REAL(capget)(hdrp, datap);
4967 if (res == 0 && datap)
4968 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4969 // We can also return -1 and write to hdrp->version if the version passed in
4970 // hdrp->version is unsupported. But that's not a trivial condition to check,
4971 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4974 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4976 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4978 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4980 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4981 return REAL(capset)(hdrp, datap);
4983 #define INIT_CAPGET \
4984 COMMON_INTERCEPT_FUNCTION(capget); \
4985 COMMON_INTERCEPT_FUNCTION(capset);
4990 #if SANITIZER_INTERCEPT_AEABI_MEM
4991 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4993 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
4996 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4998 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5001 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5003 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5006 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5008 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5011 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5013 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5016 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5018 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5021 // Note the argument order.
5022 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5024 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5027 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5029 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5032 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5034 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5037 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5039 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5042 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5044 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5047 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5049 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5052 #define INIT_AEABI_MEM \
5053 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5054 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5055 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5056 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5057 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5058 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5059 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5060 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5061 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5062 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5063 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5064 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5066 #define INIT_AEABI_MEM
5067 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5069 #if SANITIZER_INTERCEPT___BZERO
5070 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5072 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5075 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5077 #define INIT___BZERO
5078 #endif // SANITIZER_INTERCEPT___BZERO
5080 #if SANITIZER_INTERCEPT_FTIME
5081 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5083 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5084 // FIXME: under ASan the call below may write to freed memory and corrupt
5085 // its metadata. See
5086 // https://github.com/google/sanitizers/issues/321.
5087 int res = REAL(ftime)(tp);
5089 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5092 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5095 #endif // SANITIZER_INTERCEPT_FTIME
5097 #if SANITIZER_INTERCEPT_XDR
5098 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5099 unsigned size, int op) {
5101 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5102 // FIXME: under ASan the call below may write to freed memory and corrupt
5103 // its metadata. See
5104 // https://github.com/google/sanitizers/issues/321.
5105 REAL(xdrmem_create)(xdrs, addr, size, op);
5106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5107 if (op == __sanitizer_XDR_ENCODE) {
5108 // It's not obvious how much data individual xdr_ routines write.
5109 // Simply unpoison the entire target buffer in advance.
5110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5114 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5116 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5117 // FIXME: under ASan the call below may write to freed memory and corrupt
5118 // its metadata. See
5119 // https://github.com/google/sanitizers/issues/321.
5120 REAL(xdrstdio_create)(xdrs, file, op);
5121 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5124 // FIXME: under ASan the call below may write to freed memory and corrupt
5125 // its metadata. See
5126 // https://github.com/google/sanitizers/issues/321.
5127 #define XDR_INTERCEPTOR(F, T) \
5128 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5130 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5131 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5132 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5133 int res = REAL(F)(xdrs, p); \
5134 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5135 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5139 XDR_INTERCEPTOR(xdr_short, short)
5140 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5141 XDR_INTERCEPTOR(xdr_int, int)
5142 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5143 XDR_INTERCEPTOR(xdr_long, long)
5144 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5145 XDR_INTERCEPTOR(xdr_hyper, long long)
5146 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5147 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5148 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5149 XDR_INTERCEPTOR(xdr_int8_t, u8)
5150 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5151 XDR_INTERCEPTOR(xdr_int16_t, u16)
5152 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5153 XDR_INTERCEPTOR(xdr_int32_t, u32)
5154 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5155 XDR_INTERCEPTOR(xdr_int64_t, u64)
5156 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5157 XDR_INTERCEPTOR(xdr_quad_t, long long)
5158 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5159 XDR_INTERCEPTOR(xdr_bool, bool)
5160 XDR_INTERCEPTOR(xdr_enum, int)
5161 XDR_INTERCEPTOR(xdr_char, char)
5162 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5163 XDR_INTERCEPTOR(xdr_float, float)
5164 XDR_INTERCEPTOR(xdr_double, double)
5166 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5167 // wrapstring, sizeof
5169 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5172 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5173 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5174 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5175 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5176 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5178 // FIXME: under ASan the call below may write to freed memory and corrupt
5179 // its metadata. See
5180 // https://github.com/google/sanitizers/issues/321.
5181 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5182 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5185 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5190 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5193 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5194 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5195 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5196 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5198 // FIXME: under ASan the call below may write to freed memory and corrupt
5199 // its metadata. See
5200 // https://github.com/google/sanitizers/issues/321.
5201 int res = REAL(xdr_string)(xdrs, p, maxsize);
5202 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5205 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5211 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5212 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5213 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5214 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5215 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5216 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5217 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5218 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5219 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5220 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5221 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5222 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5223 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5224 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5225 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5226 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5227 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5228 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5229 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5230 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5231 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5232 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5233 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5234 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5235 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5236 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5237 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5238 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5239 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5240 COMMON_INTERCEPT_FUNCTION(xdr_string);
5243 #endif // SANITIZER_INTERCEPT_XDR
5245 #if SANITIZER_INTERCEPT_TSEARCH
5246 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5247 int (*compar)(const void *, const void *)) {
5249 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5250 // FIXME: under ASan the call below may write to freed memory and corrupt
5251 // its metadata. See
5252 // https://github.com/google/sanitizers/issues/321.
5253 void *res = REAL(tsearch)(key, rootp, compar);
5254 if (res && *(void **)res == key)
5255 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5258 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5260 #define INIT_TSEARCH
5263 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5264 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5265 void unpoison_file(__sanitizer_FILE *fp) {
5266 #if SANITIZER_HAS_STRUCT_FILE
5267 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5268 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5269 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5270 fp->_IO_read_end - fp->_IO_read_base);
5271 #endif // SANITIZER_HAS_STRUCT_FILE
5275 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5276 // These guys are called when a .c source is built with -O2.
5277 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5279 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5280 int res = REAL(__uflow)(fp);
5284 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5286 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5287 int res = REAL(__underflow)(fp);
5291 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5293 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5294 int res = REAL(__overflow)(fp, ch);
5298 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5300 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5301 int res = REAL(__wuflow)(fp);
5305 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5307 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5308 int res = REAL(__wunderflow)(fp);
5312 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5314 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5315 int res = REAL(__woverflow)(fp, ch);
5319 #define INIT_LIBIO_INTERNALS \
5320 COMMON_INTERCEPT_FUNCTION(__uflow); \
5321 COMMON_INTERCEPT_FUNCTION(__underflow); \
5322 COMMON_INTERCEPT_FUNCTION(__overflow); \
5323 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5324 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5325 COMMON_INTERCEPT_FUNCTION(__woverflow);
5327 #define INIT_LIBIO_INTERNALS
5330 #if SANITIZER_INTERCEPT_FOPEN
5331 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5333 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5334 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5335 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5336 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5337 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5338 if (res) unpoison_file(res);
5341 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5343 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5344 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5345 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5346 if (res) unpoison_file(res);
5349 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5350 __sanitizer_FILE *fp) {
5352 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5353 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5354 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5355 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5356 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5357 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5358 if (res) unpoison_file(res);
5361 #define INIT_FOPEN \
5362 COMMON_INTERCEPT_FUNCTION(fopen); \
5363 COMMON_INTERCEPT_FUNCTION(fdopen); \
5364 COMMON_INTERCEPT_FUNCTION(freopen);
5369 #if SANITIZER_INTERCEPT_FOPEN64
5370 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5372 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5373 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5374 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5375 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5376 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5377 if (res) unpoison_file(res);
5380 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5381 __sanitizer_FILE *fp) {
5383 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5384 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5385 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5386 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5387 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5388 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5389 if (res) unpoison_file(res);
5392 #define INIT_FOPEN64 \
5393 COMMON_INTERCEPT_FUNCTION(fopen64); \
5394 COMMON_INTERCEPT_FUNCTION(freopen64);
5396 #define INIT_FOPEN64
5399 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5400 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5402 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5403 // FIXME: under ASan the call below may write to freed memory and corrupt
5404 // its metadata. See
5405 // https://github.com/google/sanitizers/issues/321.
5406 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5409 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5411 FileMetadata file = {ptr, sizeloc};
5412 SetInterceptorMetadata(res, file);
5416 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5419 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5420 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5425 FileMetadata file = {(char **)ptr, sizeloc};
5426 SetInterceptorMetadata(res, file);
5430 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5433 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5434 // FIXME: under ASan the call below may write to freed memory and corrupt
5435 // its metadata. See
5436 // https://github.com/google/sanitizers/issues/321.
5437 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5438 if (res) unpoison_file(res);
5441 #define INIT_OPEN_MEMSTREAM \
5442 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5443 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5444 COMMON_INTERCEPT_FUNCTION(fmemopen);
5446 #define INIT_OPEN_MEMSTREAM
5449 #if SANITIZER_INTERCEPT_OBSTACK
5450 static void initialize_obstack(__sanitizer_obstack *obstack) {
5451 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5453 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5454 sizeof(*obstack->chunk));
5457 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5458 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5459 void (*free_fn)(uptr arg, void *p)) {
5461 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5463 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5464 if (res) initialize_obstack(obstack);
5467 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5468 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5470 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5472 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5473 if (res) initialize_obstack(obstack);
5476 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5478 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5479 REAL(_obstack_newchunk)(obstack, length);
5481 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5482 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5484 #define INIT_OBSTACK \
5485 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5486 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5487 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5489 #define INIT_OBSTACK
5492 #if SANITIZER_INTERCEPT_FFLUSH
5493 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5495 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5496 int res = REAL(fflush)(fp);
5497 // FIXME: handle fp == NULL
5499 const FileMetadata *m = GetInterceptorMetadata(fp);
5500 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5504 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5509 #if SANITIZER_INTERCEPT_FCLOSE
5510 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5512 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5513 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5514 const FileMetadata *m = GetInterceptorMetadata(fp);
5515 int res = REAL(fclose)(fp);
5517 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5518 DeleteInterceptorMetadata(fp);
5522 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5527 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5528 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5530 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5531 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5532 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5533 void *res = REAL(dlopen)(filename, flag);
5534 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5538 INTERCEPTOR(int, dlclose, void *handle) {
5540 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5541 int res = REAL(dlclose)(handle);
5542 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5545 #define INIT_DLOPEN_DLCLOSE \
5546 COMMON_INTERCEPT_FUNCTION(dlopen); \
5547 COMMON_INTERCEPT_FUNCTION(dlclose);
5549 #define INIT_DLOPEN_DLCLOSE
5552 #if SANITIZER_INTERCEPT_GETPASS
5553 INTERCEPTOR(char *, getpass, const char *prompt) {
5555 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5557 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5558 char *res = REAL(getpass)(prompt);
5559 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5563 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5565 #define INIT_GETPASS
5568 #if SANITIZER_INTERCEPT_TIMERFD
5569 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5572 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5574 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5575 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5576 if (res != -1 && old_value)
5577 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5581 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5583 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5584 int res = REAL(timerfd_gettime)(fd, curr_value);
5585 if (res != -1 && curr_value)
5586 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5589 #define INIT_TIMERFD \
5590 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5591 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5593 #define INIT_TIMERFD
5596 #if SANITIZER_INTERCEPT_MLOCKX
5597 // Linux kernel has a bug that leads to kernel deadlock if a process
5598 // maps TBs of memory and then calls mlock().
5599 static void MlockIsUnsupported() {
5600 static atomic_uint8_t printed;
5601 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5603 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5607 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5608 MlockIsUnsupported();
5612 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5613 MlockIsUnsupported();
5617 INTERCEPTOR(int, mlockall, int flags) {
5618 MlockIsUnsupported();
5622 INTERCEPTOR(int, munlockall, void) {
5623 MlockIsUnsupported();
5627 #define INIT_MLOCKX \
5628 COMMON_INTERCEPT_FUNCTION(mlock); \
5629 COMMON_INTERCEPT_FUNCTION(munlock); \
5630 COMMON_INTERCEPT_FUNCTION(mlockall); \
5631 COMMON_INTERCEPT_FUNCTION(munlockall);
5635 #endif // SANITIZER_INTERCEPT_MLOCKX
5637 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5638 struct WrappedCookie {
5640 __sanitizer_cookie_io_functions_t real_io_funcs;
5643 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5644 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5645 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5646 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5647 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5650 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5651 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5652 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5653 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5654 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5657 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5658 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5659 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5660 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5661 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5662 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5666 static int wrapped_close(void *cookie) {
5667 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5668 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5669 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5670 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5671 InternalFree(wrapped_cookie);
5675 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5676 __sanitizer_cookie_io_functions_t io_funcs) {
5678 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5679 WrappedCookie *wrapped_cookie =
5680 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5681 wrapped_cookie->real_cookie = cookie;
5682 wrapped_cookie->real_io_funcs = io_funcs;
5683 __sanitizer_FILE *res =
5684 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5685 wrapped_seek, wrapped_close});
5689 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5691 #define INIT_FOPENCOOKIE
5692 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5694 #if SANITIZER_INTERCEPT_SEM
5695 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5697 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5698 // Workaround a bug in glibc's "old" semaphore implementation by
5699 // zero-initializing the sem_t contents. This has to be done here because
5700 // interceptors bind to the lowest symbols version by default, hitting the
5701 // buggy code path while the non-sanitized build of the same code works fine.
5702 REAL(memset)(s, 0, sizeof(*s));
5703 int res = REAL(sem_init)(s, pshared, value);
5707 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5709 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5710 int res = REAL(sem_destroy)(s);
5714 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5716 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5717 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5719 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5724 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5726 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5727 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5729 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5734 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5736 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5737 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5738 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5740 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5745 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5747 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5748 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5749 int res = REAL(sem_post)(s);
5753 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5755 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5756 int res = REAL(sem_getvalue)(s, sval);
5758 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5764 COMMON_INTERCEPT_FUNCTION(sem_init); \
5765 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5766 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5767 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5768 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5769 COMMON_INTERCEPT_FUNCTION(sem_post); \
5770 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5773 #endif // SANITIZER_INTERCEPT_SEM
5775 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5776 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5778 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5779 int res = REAL(pthread_setcancelstate)(state, oldstate);
5781 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5785 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5787 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5788 int res = REAL(pthread_setcanceltype)(type, oldtype);
5790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5793 #define INIT_PTHREAD_SETCANCEL \
5794 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5795 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5797 #define INIT_PTHREAD_SETCANCEL
5800 #if SANITIZER_INTERCEPT_MINCORE
5801 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5803 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5804 int res = REAL(mincore)(addr, length, vec);
5806 uptr page_size = GetPageSizeCached();
5807 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5812 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5814 #define INIT_MINCORE
5817 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5818 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5819 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5822 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5823 remote_iov, riovcnt, flags);
5824 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5827 write_iovec(ctx, local_iov, liovcnt, res);
5831 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5832 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5835 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5836 remote_iov, riovcnt, flags);
5837 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5840 read_iovec(ctx, local_iov, liovcnt, res);
5843 #define INIT_PROCESS_VM_READV \
5844 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5845 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5847 #define INIT_PROCESS_VM_READV
5850 #if SANITIZER_INTERCEPT_CTERMID
5851 INTERCEPTOR(char *, ctermid, char *s) {
5853 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5854 char *res = REAL(ctermid)(s);
5856 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5860 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5862 #define INIT_CTERMID
5865 #if SANITIZER_INTERCEPT_CTERMID_R
5866 INTERCEPTOR(char *, ctermid_r, char *s) {
5868 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5869 char *res = REAL(ctermid_r)(s);
5871 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5875 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5877 #define INIT_CTERMID_R
5880 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5881 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5883 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5884 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5885 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5887 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5889 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5893 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5894 void *srcaddr, int *addrlen) {
5896 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5898 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5900 if (srcaddr) srcaddr_sz = *addrlen;
5901 (void)srcaddr_sz; // prevent "set but not used" warning
5902 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5906 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5907 Min((SIZE_T)*addrlen, srcaddr_sz));
5911 #define INIT_RECV_RECVFROM \
5912 COMMON_INTERCEPT_FUNCTION(recv); \
5913 COMMON_INTERCEPT_FUNCTION(recvfrom);
5915 #define INIT_RECV_RECVFROM
5918 #if SANITIZER_INTERCEPT_SEND_SENDTO
5919 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5921 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5923 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5924 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5926 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5927 if (common_flags()->intercept_send && res > 0)
5928 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5932 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5933 void *dstaddr, int addrlen) {
5935 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5937 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5938 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5940 // Can't check dstaddr as it may have uninitialized padding at the end.
5941 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5942 if (common_flags()->intercept_send && res > 0)
5943 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5946 #define INIT_SEND_SENDTO \
5947 COMMON_INTERCEPT_FUNCTION(send); \
5948 COMMON_INTERCEPT_FUNCTION(sendto);
5950 #define INIT_SEND_SENDTO
5953 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5954 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5956 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5957 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5958 int res = REAL(eventfd_read)(fd, value);
5960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
5961 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5965 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
5967 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
5969 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5970 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5972 int res = REAL(eventfd_write)(fd, value);
5975 #define INIT_EVENTFD_READ_WRITE \
5976 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
5977 COMMON_INTERCEPT_FUNCTION(eventfd_write)
5979 #define INIT_EVENTFD_READ_WRITE
5982 #if SANITIZER_INTERCEPT_STAT
5983 INTERCEPTOR(int, stat, const char *path, void *buf) {
5985 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
5986 if (common_flags()->intercept_stat)
5987 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5988 int res = REAL(stat)(path, buf);
5990 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5993 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
5998 #if SANITIZER_INTERCEPT___XSTAT
5999 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6001 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6002 if (common_flags()->intercept_stat)
6003 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6004 int res = REAL(__xstat)(version, path, buf);
6006 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6009 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6011 #define INIT___XSTAT
6014 #if SANITIZER_INTERCEPT___XSTAT64
6015 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6017 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6018 if (common_flags()->intercept_stat)
6019 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6020 int res = REAL(__xstat64)(version, path, buf);
6022 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6025 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6027 #define INIT___XSTAT64
6030 #if SANITIZER_INTERCEPT___LXSTAT
6031 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6033 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6034 if (common_flags()->intercept_stat)
6035 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6036 int res = REAL(__lxstat)(version, path, buf);
6038 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6041 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6043 #define INIT___LXSTAT
6046 #if SANITIZER_INTERCEPT___LXSTAT64
6047 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6049 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6050 if (common_flags()->intercept_stat)
6051 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6052 int res = REAL(__lxstat64)(version, path, buf);
6054 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6057 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6059 #define INIT___LXSTAT64
6062 // FIXME: add other *stat interceptor
6064 #if SANITIZER_INTERCEPT_UTMP
6065 INTERCEPTOR(void *, getutent, int dummy) {
6067 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6068 void *res = REAL(getutent)(dummy);
6070 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6073 INTERCEPTOR(void *, getutid, void *ut) {
6075 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6076 void *res = REAL(getutid)(ut);
6078 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6081 INTERCEPTOR(void *, getutline, void *ut) {
6083 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6084 void *res = REAL(getutline)(ut);
6086 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6090 COMMON_INTERCEPT_FUNCTION(getutent); \
6091 COMMON_INTERCEPT_FUNCTION(getutid); \
6092 COMMON_INTERCEPT_FUNCTION(getutline);
6097 #if SANITIZER_INTERCEPT_UTMPX
6098 INTERCEPTOR(void *, getutxent, int dummy) {
6100 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6101 void *res = REAL(getutxent)(dummy);
6103 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6106 INTERCEPTOR(void *, getutxid, void *ut) {
6108 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6109 void *res = REAL(getutxid)(ut);
6111 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6114 INTERCEPTOR(void *, getutxline, void *ut) {
6116 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6117 void *res = REAL(getutxline)(ut);
6119 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6122 #define INIT_UTMPX \
6123 COMMON_INTERCEPT_FUNCTION(getutxent); \
6124 COMMON_INTERCEPT_FUNCTION(getutxid); \
6125 COMMON_INTERCEPT_FUNCTION(getutxline);
6130 #if SANITIZER_INTERCEPT_GETLOADAVG
6131 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6133 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6134 int res = REAL(getloadavg)(loadavg, nelem);
6136 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6139 #define INIT_GETLOADAVG \
6140 COMMON_INTERCEPT_FUNCTION(getloadavg);
6142 #define INIT_GETLOADAVG
6145 static void InitializeCommonInterceptors() {
6146 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
6147 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
6186 INIT_LOCALTIME_AND_FRIENDS;
6195 INIT_GETPWNAM_AND_FRIENDS;
6196 INIT_GETPWNAM_R_AND_FRIENDS;
6208 INIT_PTHREAD_GETSCHEDPARAM;
6213 INIT_GETHOSTBYNAME_R;
6214 INIT_GETHOSTBYNAME2_R;
6215 INIT_GETHOSTBYADDR_R;
6232 INIT_GET_CURRENT_DIR_NAME;
6241 INIT_CANONICALIZE_FILE_NAME;
6243 INIT_SCHED_GETAFFINITY;
6244 INIT_SCHED_GETPARAM;
6247 INIT_XPG_STRERROR_R;
6262 INIT_PTHREAD_MUTEX_LOCK;
6263 INIT_PTHREAD_MUTEX_UNLOCK;
6271 INIT_ETHER_NTOA_ATON;
6276 INIT_PTHREAD_ATTR_GET;
6277 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6278 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6279 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6280 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6281 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6282 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6283 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6284 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6285 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6286 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6287 INIT_PTHREAD_CONDATTR_GETPSHARED;
6288 INIT_PTHREAD_CONDATTR_GETCLOCK;
6289 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6294 INIT_PTHREAD_SETNAME_NP;
6310 INIT_IF_INDEXTONAME;
6317 INIT_LIBIO_INTERNALS;
6320 INIT_OPEN_MEMSTREAM;
6324 INIT_DLOPEN_DLCLOSE;
6330 INIT_PTHREAD_SETCANCEL;
6332 INIT_PROCESS_VM_READV;
6338 INIT_EVENTFD_READ_WRITE;
6343 // FIXME: add other *stat interceptors.