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_LOCK
28 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
29 // COMMON_INTERCEPTOR_MUTEX_REPAIR
30 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
31 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
32 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
33 // COMMON_INTERCEPTOR_MEMSET_IMPL
34 // COMMON_INTERCEPTOR_MEMMOVE_IMPL
35 // COMMON_INTERCEPTOR_MEMCPY_IMPL
36 //===----------------------------------------------------------------------===//
38 #include "interception/interception.h"
39 #include "sanitizer_addrhashmap.h"
40 #include "sanitizer_placement_new.h"
41 #include "sanitizer_platform_interceptors.h"
42 #include "sanitizer_tls_get_addr.h"
46 #if SANITIZER_INTERCEPTOR_HOOKS
47 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) \
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
54 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__); \
57 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
58 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
60 #endif // SANITIZER_INTERCEPTOR_HOOKS
62 #if SANITIZER_WINDOWS && !defined(va_copy)
63 #define va_copy(dst, src) ((dst) = (src))
67 #define pthread_setname_np pthread_set_name_np
68 #define inet_aton __inet_aton
69 #define inet_pton __inet_pton
70 #define iconv __bsd_iconv
73 // Platform-specific options.
75 namespace __sanitizer {
76 bool PlatformHasDifferentMemcpyAndMemmove();
78 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
79 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
80 #elif SANITIZER_WINDOWS64
81 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
83 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
84 #endif // SANITIZER_MAC
86 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
87 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
90 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
91 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
94 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
95 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
98 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
99 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
102 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
103 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
106 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
107 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
110 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
111 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
114 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
115 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
118 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
119 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
122 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
123 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
126 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
127 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
130 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
131 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
134 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
135 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
136 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
139 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
140 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
143 #define COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n) \
144 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
145 common_flags()->strict_string_checks ? (len) + 1 : (n) )
147 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
148 COMMON_INTERCEPTOR_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
150 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
151 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) {}
154 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
155 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
158 #ifndef COMMON_INTERCEPTOR_ACQUIRE
159 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
162 #ifndef COMMON_INTERCEPTOR_RELEASE
163 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
166 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
167 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
170 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
171 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
174 #ifdef SANITIZER_NLDBL_VERSION
175 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
176 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
178 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
179 COMMON_INTERCEPT_FUNCTION(fn)
182 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
183 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
185 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
186 return internal_memset(dst, v, size); \
187 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
188 if (common_flags()->intercept_intrin) \
189 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
190 return REAL(memset)(dst, v, size); \
194 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
195 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
197 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
198 return internal_memmove(dst, src, size); \
199 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
200 if (common_flags()->intercept_intrin) { \
201 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
202 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
204 return REAL(memmove)(dst, src, size); \
208 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
209 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
211 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
212 return internal_memmove(dst, src, size); \
214 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
215 if (common_flags()->intercept_intrin) { \
216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
217 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
219 return REAL(memcpy)(dst, src, size); \
223 struct FileMetadata {
224 // For open_memstream().
229 struct CommonInterceptorMetadata {
239 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
241 static MetadataHashMap *interceptor_metadata_map;
244 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
245 const FileMetadata &file) {
246 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
248 h->type = CommonInterceptorMetadata::CIMT_FILE;
252 UNUSED static const FileMetadata *GetInterceptorMetadata(
253 __sanitizer_FILE *addr) {
254 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
259 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
266 UNUSED static void DeleteInterceptorMetadata(void *addr) {
267 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
270 #endif // SI_NOT_WINDOWS
272 #if SANITIZER_INTERCEPT_STRLEN
273 INTERCEPTOR(SIZE_T, strlen, const char *s) {
274 // Sometimes strlen is called prior to InitializeCommonInterceptors,
275 // in which case the REAL(strlen) typically used in
276 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
278 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
279 return internal_strlen(s);
281 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
282 SIZE_T result = REAL(strlen)(s);
283 if (common_flags()->intercept_strlen)
284 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
287 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
292 #if SANITIZER_INTERCEPT_STRNLEN
293 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
295 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
296 SIZE_T length = REAL(strnlen)(s, maxlen);
297 if (common_flags()->intercept_strlen)
298 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
301 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
306 #if SANITIZER_INTERCEPT_TEXTDOMAIN
307 INTERCEPTOR(char*, textdomain, const char *domainname) {
309 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
310 COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
311 char *domain = REAL(textdomain)(domainname);
313 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
317 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
319 #define INIT_TEXTDOMAIN
322 #if SANITIZER_INTERCEPT_STRCMP
323 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
324 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
327 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
328 const char *s1, const char *s2, int result)
330 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
332 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
333 unsigned char c1, c2;
336 c1 = (unsigned char)s1[i];
337 c2 = (unsigned char)s2[i];
338 if (c1 != c2 || c1 == '\0') break;
340 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
341 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
342 int result = CharCmpX(c1, c2);
343 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
348 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
349 const char *s1, const char *s2, uptr n,
352 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
353 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
354 return internal_strncmp(s1, s2, size);
356 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
357 unsigned char c1 = 0, c2 = 0;
359 for (i = 0; i < size; i++) {
360 c1 = (unsigned char)s1[i];
361 c2 = (unsigned char)s2[i];
362 if (c1 != c2 || c1 == '\0') break;
366 if (common_flags()->strict_string_checks) {
367 for (; i1 < size && s1[i1]; i1++) {}
368 for (; i2 < size && s2[i2]; i2++) {}
370 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
371 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
372 int result = CharCmpX(c1, c2);
373 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
378 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
379 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
385 #if SANITIZER_INTERCEPT_STRCASECMP
386 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
387 int c1_low = ToLower(c1);
388 int c2_low = ToLower(c2);
389 return c1_low - c2_low;
392 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
393 const char *s1, const char *s2, int result)
395 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
397 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
398 unsigned char c1 = 0, c2 = 0;
401 c1 = (unsigned char)s1[i];
402 c2 = (unsigned char)s2[i];
403 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
405 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
406 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
407 int result = CharCaseCmp(c1, c2);
408 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
413 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
414 const char *s1, const char *s2, uptr size,
417 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
419 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
420 unsigned char c1 = 0, c2 = 0;
422 for (i = 0; i < size; i++) {
423 c1 = (unsigned char)s1[i];
424 c2 = (unsigned char)s2[i];
425 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
429 if (common_flags()->strict_string_checks) {
430 for (; i1 < size && s1[i1]; i1++) {}
431 for (; i2 < size && s2[i2]; i2++) {}
433 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
434 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
435 int result = CharCaseCmp(c1, c2);
436 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
437 s1, s2, size, result);
441 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
442 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
444 #define INIT_STRCASECMP
445 #define INIT_STRNCASECMP
448 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
449 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
451 uptr len1 = REAL(strlen)(s1);
452 uptr len2 = REAL(strlen)(s2);
453 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s1, len1,
454 r ? r - s1 + len2 : len1 + 1);
455 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
459 #if SANITIZER_INTERCEPT_STRSTR
461 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
462 const char *s1, const char *s2, char *result)
464 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
465 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
466 return internal_strstr(s1, s2);
468 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
469 char *r = REAL(strstr)(s1, s2);
470 if (common_flags()->intercept_strstr)
471 StrstrCheck(ctx, r, s1, s2);
472 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
477 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
482 #if SANITIZER_INTERCEPT_STRCASESTR
484 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
485 const char *s1, const char *s2, char *result)
487 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
489 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
490 char *r = REAL(strcasestr)(s1, s2);
491 if (common_flags()->intercept_strstr)
492 StrstrCheck(ctx, r, s1, s2);
493 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
498 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
500 #define INIT_STRCASESTR
503 #if SANITIZER_INTERCEPT_MEMMEM
504 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
505 const void *s1, SIZE_T len1, const void *s2,
506 SIZE_T len2, void *result)
508 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
511 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
512 void *r = REAL(memmem)(s1, len1, s2, len2);
513 if (common_flags()->intercept_memmem) {
514 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
515 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
517 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
518 s1, len1, s2, len2, r);
522 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
525 #endif // SANITIZER_INTERCEPT_MEMMEM
527 #if SANITIZER_INTERCEPT_STRCHR
528 INTERCEPTOR(char*, strchr, const char *s, int c) {
530 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
531 return internal_strchr(s, c);
532 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
533 char *result = REAL(strchr)(s, c);
534 uptr len = internal_strlen(s);
535 uptr n = result ? result - s + 1 : len + 1;
536 if (common_flags()->intercept_strchr)
537 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n);
540 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
545 #if SANITIZER_INTERCEPT_STRCHRNUL
546 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
548 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
549 char *result = REAL(strchrnul)(s, c);
550 uptr len = result - s + 1;
551 if (common_flags()->intercept_strchr)
552 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
555 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
557 #define INIT_STRCHRNUL
560 #if SANITIZER_INTERCEPT_STRRCHR
561 INTERCEPTOR(char*, strrchr, const char *s, int c) {
563 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
564 return internal_strrchr(s, c);
565 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
566 uptr len = internal_strlen(s);
567 if (common_flags()->intercept_strchr)
568 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, len + 1);
569 return REAL(strrchr)(s, c);
571 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
576 #if SANITIZER_INTERCEPT_STRSPN
577 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
579 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
580 SIZE_T r = REAL(strspn)(s1, s2);
581 if (common_flags()->intercept_strspn) {
582 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
583 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
588 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
590 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
591 SIZE_T r = REAL(strcspn)(s1, s2);
592 if (common_flags()->intercept_strspn) {
593 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
594 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
599 #define INIT_STRSPN \
600 COMMON_INTERCEPT_FUNCTION(strspn); \
601 COMMON_INTERCEPT_FUNCTION(strcspn);
606 #if SANITIZER_INTERCEPT_STRPBRK
607 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
609 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
610 char *r = REAL(strpbrk)(s1, s2);
611 if (common_flags()->intercept_strpbrk) {
612 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
613 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
614 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
619 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
624 #if SANITIZER_INTERCEPT_MEMSET
625 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
627 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
630 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
635 #if SANITIZER_INTERCEPT_MEMMOVE
636 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
638 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
641 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
646 #if SANITIZER_INTERCEPT_MEMCPY
647 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
648 // On OS X, calling internal_memcpy here will cause memory corruptions,
649 // because memcpy and memmove are actually aliases of the same
650 // implementation. We need to use internal_memmove here.
651 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
652 // due to memcpy being an alias of memmove on OS X.
654 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
655 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
657 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
661 #define INIT_MEMCPY \
663 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
664 COMMON_INTERCEPT_FUNCTION(memcpy); \
666 ASSIGN_REAL(memcpy, memmove); \
668 CHECK(REAL(memcpy)); \
675 #if SANITIZER_INTERCEPT_MEMCMP
677 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
678 const void *s1, const void *s2, uptr n,
681 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
682 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
683 return internal_memcmp(a1, a2, size);
685 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
686 if (common_flags()->intercept_memcmp) {
687 if (common_flags()->strict_memcmp) {
688 // Check the entire regions even if the first bytes of the buffers are
690 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
691 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
692 // Fallthrough to REAL(memcmp) below.
694 unsigned char c1 = 0, c2 = 0;
695 const unsigned char *s1 = (const unsigned char*)a1;
696 const unsigned char *s2 = (const unsigned char*)a2;
698 for (i = 0; i < size; i++) {
703 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
704 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
705 int r = CharCmpX(c1, c2);
706 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
711 int result = REAL(memcmp(a1, a2, size));
712 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
717 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
722 #if SANITIZER_INTERCEPT_MEMCHR
723 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
724 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
725 return internal_memchr(s, c, n);
727 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
728 #if SANITIZER_WINDOWS
731 res = REAL(memchr)(s, c, n);
733 res = internal_memchr(s, c, n);
736 void *res = REAL(memchr)(s, c, n);
738 uptr len = res ? (char *)res - (const char *)s + 1 : n;
739 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
743 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
748 #if SANITIZER_INTERCEPT_MEMRCHR
749 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
751 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
752 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
753 return REAL(memrchr)(s, c, n);
756 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
761 #if SANITIZER_INTERCEPT_FREXP
762 INTERCEPTOR(double, frexp, double x, int *exp) {
764 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
765 // Assuming frexp() always writes to |exp|.
766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
767 double res = REAL(frexp)(x, exp);
771 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
774 #endif // SANITIZER_INTERCEPT_FREXP
776 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
777 INTERCEPTOR(float, frexpf, float x, int *exp) {
779 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
780 // FIXME: under ASan the call below may write to freed memory and corrupt
782 // https://github.com/google/sanitizers/issues/321.
783 float res = REAL(frexpf)(x, exp);
784 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
788 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
790 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
791 // FIXME: under ASan the call below may write to freed memory and corrupt
793 // https://github.com/google/sanitizers/issues/321.
794 long double res = REAL(frexpl)(x, exp);
795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
799 #define INIT_FREXPF_FREXPL \
800 COMMON_INTERCEPT_FUNCTION(frexpf); \
801 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
803 #define INIT_FREXPF_FREXPL
804 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
807 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
808 SIZE_T iovlen, SIZE_T maxlen) {
809 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
810 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
816 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
817 SIZE_T iovlen, SIZE_T maxlen) {
818 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
819 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
820 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
821 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
827 #if SANITIZER_INTERCEPT_READ
828 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
830 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
831 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
832 // FIXME: under ASan the call below may write to freed memory and corrupt
834 // https://github.com/google/sanitizers/issues/321.
835 SSIZE_T res = REAL(read)(fd, ptr, count);
836 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
837 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
840 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
845 #if SANITIZER_INTERCEPT_PREAD
846 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
848 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
849 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
850 // FIXME: under ASan the call below may write to freed memory and corrupt
852 // https://github.com/google/sanitizers/issues/321.
853 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
854 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
855 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
858 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
863 #if SANITIZER_INTERCEPT_PREAD64
864 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
866 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
867 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
868 // FIXME: under ASan the call below may write to freed memory and corrupt
870 // https://github.com/google/sanitizers/issues/321.
871 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
872 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
873 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
876 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
881 #if SANITIZER_INTERCEPT_READV
882 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
885 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
886 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
887 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
888 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
889 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
892 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
897 #if SANITIZER_INTERCEPT_PREADV
898 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
901 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
902 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
903 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
904 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
905 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
908 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
913 #if SANITIZER_INTERCEPT_PREADV64
914 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
917 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
918 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
919 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
920 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
921 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
924 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
926 #define INIT_PREADV64
929 #if SANITIZER_INTERCEPT_WRITE
930 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
932 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
933 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
934 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
935 SSIZE_T res = REAL(write)(fd, ptr, count);
936 // FIXME: this check should be _before_ the call to REAL(write), not after
937 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
940 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
945 #if SANITIZER_INTERCEPT_PWRITE
946 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
948 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
949 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
950 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
951 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
952 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
955 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
960 #if SANITIZER_INTERCEPT_PWRITE64
961 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
964 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
965 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
966 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
967 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
968 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
971 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
973 #define INIT_PWRITE64
976 #if SANITIZER_INTERCEPT_WRITEV
977 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
980 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
981 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
982 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
983 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
984 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
987 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
992 #if SANITIZER_INTERCEPT_PWRITEV
993 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
996 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
997 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
998 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
999 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1000 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1003 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1005 #define INIT_PWRITEV
1008 #if SANITIZER_INTERCEPT_PWRITEV64
1009 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1012 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1013 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1014 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1015 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1016 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1019 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1021 #define INIT_PWRITEV64
1024 #if SANITIZER_INTERCEPT_PRCTL
1025 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1026 unsigned long arg3, // NOLINT
1027 unsigned long arg4, unsigned long arg5) { // NOLINT
1029 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1030 static const int PR_SET_NAME = 15;
1031 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1032 if (option == PR_SET_NAME) {
1034 internal_strncpy(buff, (char *)arg2, 15);
1036 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1040 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1043 #endif // SANITIZER_INTERCEPT_PRCTL
1045 #if SANITIZER_INTERCEPT_TIME
1046 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1048 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1049 unsigned long local_t;
1050 unsigned long res = REAL(time)(&local_t);
1051 if (t && res != (unsigned long)-1) {
1052 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1057 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1060 #endif // SANITIZER_INTERCEPT_TIME
1062 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1063 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1066 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1067 // can point to shared memory and tsan would report a data race.
1068 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1069 REAL(strlen(tm->tm_zone)) + 1);
1072 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1074 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1075 __sanitizer_tm *res = REAL(localtime)(timep);
1077 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1078 unpoison_tm(ctx, res);
1082 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1084 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1085 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1087 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1088 unpoison_tm(ctx, res);
1092 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1094 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1095 __sanitizer_tm *res = REAL(gmtime)(timep);
1097 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1098 unpoison_tm(ctx, res);
1102 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1104 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1105 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1107 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1108 unpoison_tm(ctx, res);
1112 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1114 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1115 // FIXME: under ASan the call below may write to freed memory and corrupt
1116 // its metadata. See
1117 // https://github.com/google/sanitizers/issues/321.
1118 char *res = REAL(ctime)(timep);
1120 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1121 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1125 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1127 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1128 // FIXME: under ASan the call below may write to freed memory and corrupt
1129 // its metadata. See
1130 // https://github.com/google/sanitizers/issues/321.
1131 char *res = REAL(ctime_r)(timep, result);
1133 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1134 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1138 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1140 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1141 // FIXME: under ASan the call below may write to freed memory and corrupt
1142 // its metadata. See
1143 // https://github.com/google/sanitizers/issues/321.
1144 char *res = REAL(asctime)(tm);
1146 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1147 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1151 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1153 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1154 // FIXME: under ASan the call below may write to freed memory and corrupt
1155 // its metadata. See
1156 // https://github.com/google/sanitizers/issues/321.
1157 char *res = REAL(asctime_r)(tm, result);
1159 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1164 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1166 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1167 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1168 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1169 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1170 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1171 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1172 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1173 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1174 long res = REAL(mktime)(tm);
1175 if (res != -1) unpoison_tm(ctx, tm);
1178 #define INIT_LOCALTIME_AND_FRIENDS \
1179 COMMON_INTERCEPT_FUNCTION(localtime); \
1180 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1181 COMMON_INTERCEPT_FUNCTION(gmtime); \
1182 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1183 COMMON_INTERCEPT_FUNCTION(ctime); \
1184 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1185 COMMON_INTERCEPT_FUNCTION(asctime); \
1186 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1187 COMMON_INTERCEPT_FUNCTION(mktime);
1189 #define INIT_LOCALTIME_AND_FRIENDS
1190 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1192 #if SANITIZER_INTERCEPT_STRPTIME
1193 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1195 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1197 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1198 // FIXME: under ASan the call below may write to freed memory and corrupt
1199 // its metadata. See
1200 // https://github.com/google/sanitizers/issues/321.
1201 char *res = REAL(strptime)(s, format, tm);
1202 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1204 // Do not call unpoison_tm here, because strptime does not, in fact,
1205 // initialize the entire struct tm. For example, tm_zone pointer is left
1207 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1211 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1213 #define INIT_STRPTIME
1216 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1217 #include "sanitizer_common_interceptors_format.inc"
1219 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1223 va_start(ap, format); \
1224 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1225 int res = WRAP(vname)(__VA_ARGS__, ap); \
1232 #if SANITIZER_INTERCEPT_SCANF
1234 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1237 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1240 int res = REAL(vname)(__VA_ARGS__); \
1242 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1247 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1248 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1250 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1251 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1253 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1254 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1256 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1257 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1258 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1260 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1262 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1264 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1265 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1266 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1268 INTERCEPTOR(int, scanf, const char *format, ...)
1269 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1271 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1272 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1274 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1275 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1277 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1278 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1279 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1281 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1282 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1284 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1285 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1290 #if SANITIZER_INTERCEPT_SCANF
1291 #define INIT_SCANF \
1292 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1293 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1294 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1295 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1296 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1297 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1302 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1303 #define INIT_ISOC99_SCANF \
1304 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1305 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1306 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1307 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1308 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1309 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1311 #define INIT_ISOC99_SCANF
1314 #if SANITIZER_INTERCEPT_PRINTF
1316 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1318 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1322 #define VPRINTF_INTERCEPTOR_RETURN() \
1325 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1327 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1328 if (common_flags()->check_printf) \
1329 printf_common(ctx, format, aq); \
1330 int res = REAL(vname)(__VA_ARGS__); \
1331 VPRINTF_INTERCEPTOR_RETURN(); \
1335 // FIXME: under ASan the REAL() call below may write to freed memory and
1336 // corrupt its metadata. See
1337 // https://github.com/google/sanitizers/issues/321.
1338 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1340 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1341 if (common_flags()->check_printf) { \
1342 printf_common(ctx, format, aq); \
1344 int res = REAL(vname)(str, __VA_ARGS__); \
1346 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1348 VPRINTF_INTERCEPTOR_RETURN(); \
1352 // FIXME: under ASan the REAL() call below may write to freed memory and
1353 // corrupt its metadata. See
1354 // https://github.com/google/sanitizers/issues/321.
1355 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1357 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1358 if (common_flags()->check_printf) { \
1359 printf_common(ctx, format, aq); \
1361 int res = REAL(vname)(str, size, __VA_ARGS__); \
1363 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1365 VPRINTF_INTERCEPTOR_RETURN(); \
1369 // FIXME: under ASan the REAL() call below may write to freed memory and
1370 // corrupt its metadata. See
1371 // https://github.com/google/sanitizers/issues/321.
1372 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1374 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1376 if (common_flags()->check_printf) { \
1377 printf_common(ctx, format, aq); \
1379 int res = REAL(vname)(strp, __VA_ARGS__); \
1381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1383 VPRINTF_INTERCEPTOR_RETURN(); \
1387 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1388 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1390 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1392 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1394 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1396 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1398 #if SANITIZER_INTERCEPT_PRINTF_L
1399 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1400 const char *format, va_list ap)
1401 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1403 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1404 const char *format, ...)
1405 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1406 #endif // SANITIZER_INTERCEPT_PRINTF_L
1408 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1409 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1411 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1412 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1414 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1415 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1416 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1418 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1419 const char *format, va_list ap)
1420 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1422 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1424 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1426 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1428 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1431 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1433 INTERCEPTOR(int, printf, const char *format, ...)
1434 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1436 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1437 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1439 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1440 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1442 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1443 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1445 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1446 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1448 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1449 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1450 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1452 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1454 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1456 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1457 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1459 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1460 const char *format, ...)
1461 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1464 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1466 #endif // SANITIZER_INTERCEPT_PRINTF
1468 #if SANITIZER_INTERCEPT_PRINTF
1469 #define INIT_PRINTF \
1470 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1471 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1472 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1473 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1474 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1475 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1476 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1477 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1478 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1479 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1484 #if SANITIZER_INTERCEPT_PRINTF_L
1485 #define INIT_PRINTF_L \
1486 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1487 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1489 #define INIT_PRINTF_L
1492 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1493 #define INIT_ISOC99_PRINTF \
1494 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1495 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1496 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1497 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1498 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1499 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1500 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1501 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1503 #define INIT_ISOC99_PRINTF
1506 #if SANITIZER_INTERCEPT_IOCTL
1507 #include "sanitizer_common_interceptors_ioctl.inc"
1508 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1509 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1510 // can trigger a report and we need to be able to unwind through this
1511 // function. On Mac in debug mode we might not have a frame pointer, because
1512 // ioctl_common_[pre|post] doesn't get inlined here.
1513 ENABLE_FRAME_POINTER;
1517 va_start(ap, request);
1518 void *arg = va_arg(ap, void *);
1520 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1522 CHECK(ioctl_initialized);
1524 // Note: TSan does not use common flags, and they are zero-initialized.
1525 // This effectively disables ioctl handling in TSan.
1526 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1528 // Although request is unsigned long, the rest of the interceptor uses it
1529 // as just "unsigned" to save space, because we know that all values fit in
1530 // "unsigned" - they are compile-time constants.
1532 const ioctl_desc *desc = ioctl_lookup(request);
1533 ioctl_desc decoded_desc;
1535 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1536 if (!ioctl_decode(request, &decoded_desc))
1537 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1539 desc = &decoded_desc;
1542 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1543 int res = REAL(ioctl)(d, request, arg);
1544 // FIXME: some ioctls have different return values for success and failure.
1545 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1548 #define INIT_IOCTL \
1550 COMMON_INTERCEPT_FUNCTION(ioctl);
1555 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1556 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1557 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1558 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1562 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1563 REAL(strlen)(pwd->pw_name) + 1);
1565 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1566 REAL(strlen)(pwd->pw_passwd) + 1);
1567 #if !SANITIZER_ANDROID
1569 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1570 REAL(strlen)(pwd->pw_gecos) + 1);
1574 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1575 REAL(strlen)(pwd->pw_class) + 1);
1578 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1579 REAL(strlen)(pwd->pw_dir) + 1);
1581 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1582 REAL(strlen)(pwd->pw_shell) + 1);
1586 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1588 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1590 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1591 REAL(strlen)(grp->gr_name) + 1);
1593 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1594 REAL(strlen)(grp->gr_passwd) + 1);
1595 char **p = grp->gr_mem;
1597 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1599 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1600 (p - grp->gr_mem + 1) * sizeof(*p));
1603 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1604 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1605 // SANITIZER_INTERCEPT_GETPWENT_R ||
1606 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1608 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1609 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1611 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1612 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1613 __sanitizer_passwd *res = REAL(getpwnam)(name);
1614 if (res) unpoison_passwd(ctx, res);
1617 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1619 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1620 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1621 if (res) unpoison_passwd(ctx, res);
1624 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1626 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1627 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1628 __sanitizer_group *res = REAL(getgrnam)(name);
1629 if (res) unpoison_group(ctx, res);
1632 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1634 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1635 __sanitizer_group *res = REAL(getgrgid)(gid);
1636 if (res) unpoison_group(ctx, res);
1639 #define INIT_GETPWNAM_AND_FRIENDS \
1640 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1641 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1642 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1643 COMMON_INTERCEPT_FUNCTION(getgrgid);
1645 #define INIT_GETPWNAM_AND_FRIENDS
1648 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1649 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1650 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1652 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1653 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1654 // FIXME: under ASan the call below may write to freed memory and corrupt
1655 // its metadata. See
1656 // https://github.com/google/sanitizers/issues/321.
1657 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1659 if (result && *result) unpoison_passwd(ctx, *result);
1660 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1662 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1665 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1666 SIZE_T buflen, __sanitizer_passwd **result) {
1668 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1669 // FIXME: under ASan the call below may write to freed memory and corrupt
1670 // its metadata. See
1671 // https://github.com/google/sanitizers/issues/321.
1672 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1674 if (result && *result) unpoison_passwd(ctx, *result);
1675 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1677 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1680 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1681 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1683 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1684 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1685 // FIXME: under ASan the call below may write to freed memory and corrupt
1686 // its metadata. See
1687 // https://github.com/google/sanitizers/issues/321.
1688 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1690 if (result && *result) unpoison_group(ctx, *result);
1691 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1693 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1696 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1697 SIZE_T buflen, __sanitizer_group **result) {
1699 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1700 // FIXME: under ASan the call below may write to freed memory and corrupt
1701 // its metadata. See
1702 // https://github.com/google/sanitizers/issues/321.
1703 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1705 if (result && *result) unpoison_group(ctx, *result);
1706 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1708 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1711 #define INIT_GETPWNAM_R_AND_FRIENDS \
1712 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1713 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1714 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1715 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1717 #define INIT_GETPWNAM_R_AND_FRIENDS
1720 #if SANITIZER_INTERCEPT_GETPWENT
1721 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1723 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1724 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1725 if (res) unpoison_passwd(ctx, res);
1728 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1730 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1731 __sanitizer_group *res = REAL(getgrent)(dummy);
1732 if (res) unpoison_group(ctx, res);;
1735 #define INIT_GETPWENT \
1736 COMMON_INTERCEPT_FUNCTION(getpwent); \
1737 COMMON_INTERCEPT_FUNCTION(getgrent);
1739 #define INIT_GETPWENT
1742 #if SANITIZER_INTERCEPT_FGETPWENT
1743 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1745 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1746 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1747 if (res) unpoison_passwd(ctx, res);
1750 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1752 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1753 __sanitizer_group *res = REAL(fgetgrent)(fp);
1754 if (res) unpoison_group(ctx, res);
1757 #define INIT_FGETPWENT \
1758 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1759 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1761 #define INIT_FGETPWENT
1764 #if SANITIZER_INTERCEPT_GETPWENT_R
1765 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1766 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1768 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1769 // FIXME: under ASan the call below may write to freed memory and corrupt
1770 // its metadata. See
1771 // https://github.com/google/sanitizers/issues/321.
1772 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1774 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1777 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1780 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1781 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1783 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1784 // FIXME: under ASan the call below may write to freed memory and corrupt
1785 // its metadata. See
1786 // https://github.com/google/sanitizers/issues/321.
1787 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1789 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1792 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1795 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1796 __sanitizer_group **pwbufp) {
1798 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1799 // FIXME: under ASan the call below may write to freed memory and corrupt
1800 // its metadata. See
1801 // https://github.com/google/sanitizers/issues/321.
1802 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1804 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1807 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1810 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1811 SIZE_T buflen, __sanitizer_group **pwbufp) {
1813 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1814 // FIXME: under ASan the call below may write to freed memory and corrupt
1815 // its metadata. See
1816 // https://github.com/google/sanitizers/issues/321.
1817 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1819 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1820 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1822 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1825 #define INIT_GETPWENT_R \
1826 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
1827 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1828 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
1829 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1831 #define INIT_GETPWENT_R
1834 #if SANITIZER_INTERCEPT_SETPWENT
1835 // The only thing these interceptors do is disable any nested interceptors.
1836 // These functions may open nss modules and call uninstrumented functions from
1837 // them, and we don't want things like strlen() to trigger.
1838 INTERCEPTOR(void, setpwent, int dummy) {
1840 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1841 REAL(setpwent)(dummy);
1843 INTERCEPTOR(void, endpwent, int dummy) {
1845 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1846 REAL(endpwent)(dummy);
1848 INTERCEPTOR(void, setgrent, int dummy) {
1850 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1851 REAL(setgrent)(dummy);
1853 INTERCEPTOR(void, endgrent, int dummy) {
1855 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1856 REAL(endgrent)(dummy);
1858 #define INIT_SETPWENT \
1859 COMMON_INTERCEPT_FUNCTION(setpwent); \
1860 COMMON_INTERCEPT_FUNCTION(endpwent); \
1861 COMMON_INTERCEPT_FUNCTION(setgrent); \
1862 COMMON_INTERCEPT_FUNCTION(endgrent);
1864 #define INIT_SETPWENT
1867 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1868 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1870 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1871 // FIXME: under ASan the call below may write to freed memory and corrupt
1872 // its metadata. See
1873 // https://github.com/google/sanitizers/issues/321.
1874 int res = REAL(clock_getres)(clk_id, tp);
1876 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1880 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1882 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1883 // FIXME: under ASan the call below may write to freed memory and corrupt
1884 // its metadata. See
1885 // https://github.com/google/sanitizers/issues/321.
1886 int res = REAL(clock_gettime)(clk_id, tp);
1888 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1892 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1894 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1895 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1896 return REAL(clock_settime)(clk_id, tp);
1898 #define INIT_CLOCK_GETTIME \
1899 COMMON_INTERCEPT_FUNCTION(clock_getres); \
1900 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1901 COMMON_INTERCEPT_FUNCTION(clock_settime);
1903 #define INIT_CLOCK_GETTIME
1906 #if SANITIZER_INTERCEPT_GETITIMER
1907 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1909 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1910 // FIXME: under ASan the call below may write to freed memory and corrupt
1911 // its metadata. See
1912 // https://github.com/google/sanitizers/issues/321.
1913 int res = REAL(getitimer)(which, curr_value);
1914 if (!res && curr_value) {
1915 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1919 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1921 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1923 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1924 // FIXME: under ASan the call below may write to freed memory and corrupt
1925 // its metadata. See
1926 // https://github.com/google/sanitizers/issues/321.
1927 int res = REAL(setitimer)(which, new_value, old_value);
1928 if (!res && old_value) {
1929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1933 #define INIT_GETITIMER \
1934 COMMON_INTERCEPT_FUNCTION(getitimer); \
1935 COMMON_INTERCEPT_FUNCTION(setitimer);
1937 #define INIT_GETITIMER
1940 #if SANITIZER_INTERCEPT_GLOB
1941 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1942 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1943 // +1 for NULL pointer at the end.
1944 if (pglob->gl_pathv)
1945 COMMON_INTERCEPTOR_WRITE_RANGE(
1946 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1947 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1948 char *p = pglob->gl_pathv[i];
1949 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1953 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1955 static void wrapped_gl_closedir(void *dir) {
1956 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1957 pglob_copy->gl_closedir(dir);
1960 static void *wrapped_gl_readdir(void *dir) {
1961 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1962 return pglob_copy->gl_readdir(dir);
1965 static void *wrapped_gl_opendir(const char *s) {
1966 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1967 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1968 return pglob_copy->gl_opendir(s);
1971 static int wrapped_gl_lstat(const char *s, void *st) {
1972 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1973 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1974 return pglob_copy->gl_lstat(s, st);
1977 static int wrapped_gl_stat(const char *s, void *st) {
1978 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1979 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1980 return pglob_copy->gl_stat(s, st);
1983 static const __sanitizer_glob_t kGlobCopy = {
1985 0, wrapped_gl_closedir, wrapped_gl_readdir,
1986 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1988 INTERCEPTOR(int, glob, const char *pattern, int flags,
1989 int (*errfunc)(const char *epath, int eerrno),
1990 __sanitizer_glob_t *pglob) {
1992 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1993 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1994 __sanitizer_glob_t glob_copy;
1995 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1996 if (flags & glob_altdirfunc) {
1997 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1998 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1999 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2000 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2001 Swap(pglob->gl_stat, glob_copy.gl_stat);
2002 pglob_copy = &glob_copy;
2004 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2005 if (flags & glob_altdirfunc) {
2006 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2007 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2008 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2009 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2010 Swap(pglob->gl_stat, glob_copy.gl_stat);
2013 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2017 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2018 int (*errfunc)(const char *epath, int eerrno),
2019 __sanitizer_glob_t *pglob) {
2021 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2022 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2023 __sanitizer_glob_t glob_copy;
2024 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2025 if (flags & glob_altdirfunc) {
2026 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2027 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2028 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2029 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2030 Swap(pglob->gl_stat, glob_copy.gl_stat);
2031 pglob_copy = &glob_copy;
2033 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2034 if (flags & glob_altdirfunc) {
2035 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2036 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2037 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2038 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2039 Swap(pglob->gl_stat, glob_copy.gl_stat);
2042 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2046 COMMON_INTERCEPT_FUNCTION(glob); \
2047 COMMON_INTERCEPT_FUNCTION(glob64);
2048 #else // SANITIZER_INTERCEPT_GLOB
2050 #endif // SANITIZER_INTERCEPT_GLOB
2052 #if SANITIZER_INTERCEPT_WAIT
2053 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2054 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2056 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2058 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2059 // FIXME: under ASan the call below may write to freed memory and corrupt
2060 // its metadata. See
2061 // https://github.com/google/sanitizers/issues/321.
2062 int res = REAL(wait)(status);
2063 if (res != -1 && status)
2064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2067 // On FreeBSD id_t is always 64-bit wide.
2068 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2069 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2072 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2076 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2077 // FIXME: under ASan the call below may write to freed memory and corrupt
2078 // its metadata. See
2079 // https://github.com/google/sanitizers/issues/321.
2080 int res = REAL(waitid)(idtype, id, infop, options);
2081 if (res != -1 && infop)
2082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2085 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2087 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2088 // FIXME: under ASan the call below may write to freed memory and corrupt
2089 // its metadata. See
2090 // https://github.com/google/sanitizers/issues/321.
2091 int res = REAL(waitpid)(pid, status, options);
2092 if (res != -1 && status)
2093 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2096 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2098 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2099 // FIXME: under ASan the call below may write to freed memory and corrupt
2100 // its metadata. See
2101 // https://github.com/google/sanitizers/issues/321.
2102 int res = REAL(wait3)(status, options, rusage);
2104 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2105 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2109 #if SANITIZER_ANDROID
2110 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2112 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2113 // FIXME: under ASan the call below may write to freed memory and corrupt
2114 // its metadata. See
2115 // https://github.com/google/sanitizers/issues/321.
2116 int res = REAL(__wait4)(pid, status, options, rusage);
2118 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2119 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2123 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2125 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2127 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2128 // FIXME: under ASan the call below may write to freed memory and corrupt
2129 // its metadata. See
2130 // https://github.com/google/sanitizers/issues/321.
2131 int res = REAL(wait4)(pid, status, options, rusage);
2133 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2134 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2138 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2139 #endif // SANITIZER_ANDROID
2141 COMMON_INTERCEPT_FUNCTION(wait); \
2142 COMMON_INTERCEPT_FUNCTION(waitid); \
2143 COMMON_INTERCEPT_FUNCTION(waitpid); \
2144 COMMON_INTERCEPT_FUNCTION(wait3);
2150 #if SANITIZER_INTERCEPT_INET
2151 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2153 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2154 uptr sz = __sanitizer_in_addr_sz(af);
2155 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2156 // FIXME: figure out read size based on the address family.
2157 // FIXME: under ASan the call below may write to freed memory and corrupt
2158 // its metadata. See
2159 // https://github.com/google/sanitizers/issues/321.
2160 char *res = REAL(inet_ntop)(af, src, dst, size);
2161 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2164 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2166 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2167 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2168 // FIXME: figure out read size based on the address family.
2169 // FIXME: under ASan the call below may write to freed memory and corrupt
2170 // its metadata. See
2171 // https://github.com/google/sanitizers/issues/321.
2172 int res = REAL(inet_pton)(af, src, dst);
2174 uptr sz = __sanitizer_in_addr_sz(af);
2175 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2180 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2181 COMMON_INTERCEPT_FUNCTION(inet_pton);
2186 #if SANITIZER_INTERCEPT_INET
2187 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2189 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2190 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2191 // FIXME: under ASan the call below may write to freed memory and corrupt
2192 // its metadata. See
2193 // https://github.com/google/sanitizers/issues/321.
2194 int res = REAL(inet_aton)(cp, dst);
2196 uptr sz = __sanitizer_in_addr_sz(af_inet);
2197 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2201 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2203 #define INIT_INET_ATON
2206 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2207 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2209 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2210 // FIXME: under ASan the call below may write to freed memory and corrupt
2211 // its metadata. See
2212 // https://github.com/google/sanitizers/issues/321.
2213 int res = REAL(pthread_getschedparam)(thread, policy, param);
2215 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2216 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2220 #define INIT_PTHREAD_GETSCHEDPARAM \
2221 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2223 #define INIT_PTHREAD_GETSCHEDPARAM
2226 #if SANITIZER_INTERCEPT_GETADDRINFO
2227 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2228 struct __sanitizer_addrinfo *hints,
2229 struct __sanitizer_addrinfo **out) {
2231 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2232 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2234 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2236 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2237 // FIXME: under ASan the call below may write to freed memory and corrupt
2238 // its metadata. See
2239 // https://github.com/google/sanitizers/issues/321.
2240 int res = REAL(getaddrinfo)(node, service, hints, out);
2241 if (res == 0 && out) {
2242 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2243 struct __sanitizer_addrinfo *p = *out;
2245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2248 if (p->ai_canonname)
2249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2250 REAL(strlen)(p->ai_canonname) + 1);
2256 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2258 #define INIT_GETADDRINFO
2261 #if SANITIZER_INTERCEPT_GETNAMEINFO
2262 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2263 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2265 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2266 serv, servlen, flags);
2267 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2268 // There is padding in in_addr that may make this too noisy
2269 // FIXME: under ASan the call below may write to freed memory and corrupt
2270 // its metadata. See
2271 // https://github.com/google/sanitizers/issues/321.
2273 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2275 if (host && hostlen)
2276 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2277 if (serv && servlen)
2278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2282 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2284 #define INIT_GETNAMEINFO
2287 #if SANITIZER_INTERCEPT_GETSOCKNAME
2288 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2290 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2291 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2292 int addrlen_in = *addrlen;
2293 // FIXME: under ASan the call below may write to freed memory and corrupt
2294 // its metadata. See
2295 // https://github.com/google/sanitizers/issues/321.
2296 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2298 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2302 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2304 #define INIT_GETSOCKNAME
2307 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2308 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2309 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2311 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2312 char **p = h->h_aliases;
2314 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2317 COMMON_INTERCEPTOR_WRITE_RANGE(
2318 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2321 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2324 COMMON_INTERCEPTOR_WRITE_RANGE(
2325 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2329 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2330 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2332 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2333 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2334 if (res) write_hostent(ctx, res);
2338 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2341 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2342 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2343 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2344 if (res) write_hostent(ctx, res);
2348 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2350 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2351 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2352 if (res) write_hostent(ctx, res);
2356 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2358 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2359 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2360 if (res) write_hostent(ctx, res);
2363 #define INIT_GETHOSTBYNAME \
2364 COMMON_INTERCEPT_FUNCTION(gethostent); \
2365 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2366 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
2367 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2369 #define INIT_GETHOSTBYNAME
2372 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2373 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2374 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2377 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2379 // FIXME: under ASan the call below may write to freed memory and corrupt
2380 // its metadata. See
2381 // https://github.com/google/sanitizers/issues/321.
2382 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2384 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2385 if (res == 0 && *result) write_hostent(ctx, *result);
2388 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2391 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2393 #define INIT_GETHOSTBYNAME_R
2396 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2397 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2398 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2400 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2402 // FIXME: under ASan the call below may write to freed memory and corrupt
2403 // its metadata. See
2404 // https://github.com/google/sanitizers/issues/321.
2405 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2408 if (res == 0 && *result) write_hostent(ctx, *result);
2411 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2414 #define INIT_GETHOSTENT_R \
2415 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2417 #define INIT_GETHOSTENT_R
2420 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2421 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2422 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2423 __sanitizer_hostent **result, int *h_errnop) {
2425 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2426 buflen, result, h_errnop);
2427 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2428 // FIXME: under ASan the call below may write to freed memory and corrupt
2429 // its metadata. See
2430 // https://github.com/google/sanitizers/issues/321.
2431 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2434 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2435 if (res == 0 && *result) write_hostent(ctx, *result);
2438 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2441 #define INIT_GETHOSTBYADDR_R \
2442 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2444 #define INIT_GETHOSTBYADDR_R
2447 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2448 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2449 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2450 __sanitizer_hostent **result, int *h_errnop) {
2452 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2454 // FIXME: under ASan the call below may write to freed memory and corrupt
2455 // its metadata. See
2456 // https://github.com/google/sanitizers/issues/321.
2458 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2461 if (res == 0 && *result) write_hostent(ctx, *result);
2464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2467 #define INIT_GETHOSTBYNAME2_R \
2468 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2470 #define INIT_GETHOSTBYNAME2_R
2473 #if SANITIZER_INTERCEPT_GETSOCKOPT
2474 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2477 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2479 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2480 // FIXME: under ASan the call below may write to freed memory and corrupt
2481 // its metadata. See
2482 // https://github.com/google/sanitizers/issues/321.
2483 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2485 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2488 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2490 #define INIT_GETSOCKOPT
2493 #if SANITIZER_INTERCEPT_ACCEPT
2494 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2496 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2497 unsigned addrlen0 = 0;
2499 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2500 addrlen0 = *addrlen;
2502 int fd2 = REAL(accept)(fd, addr, addrlen);
2504 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2505 if (addr && addrlen)
2506 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2510 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2515 #if SANITIZER_INTERCEPT_ACCEPT4
2516 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2518 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2519 unsigned addrlen0 = 0;
2521 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2522 addrlen0 = *addrlen;
2524 // FIXME: under ASan the call below may write to freed memory and corrupt
2525 // its metadata. See
2526 // https://github.com/google/sanitizers/issues/321.
2527 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2529 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2530 if (addr && addrlen)
2531 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2535 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2537 #define INIT_ACCEPT4
2540 #if SANITIZER_INTERCEPT_MODF
2541 INTERCEPTOR(double, modf, double x, double *iptr) {
2543 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2544 // FIXME: under ASan the call below may write to freed memory and corrupt
2545 // its metadata. See
2546 // https://github.com/google/sanitizers/issues/321.
2547 double res = REAL(modf)(x, iptr);
2549 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2553 INTERCEPTOR(float, modff, float x, float *iptr) {
2555 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2556 // FIXME: under ASan the call below may write to freed memory and corrupt
2557 // its metadata. See
2558 // https://github.com/google/sanitizers/issues/321.
2559 float res = REAL(modff)(x, iptr);
2561 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2565 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2567 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2568 // FIXME: under ASan the call below may write to freed memory and corrupt
2569 // its metadata. See
2570 // https://github.com/google/sanitizers/issues/321.
2571 long double res = REAL(modfl)(x, iptr);
2573 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2578 COMMON_INTERCEPT_FUNCTION(modf); \
2579 COMMON_INTERCEPT_FUNCTION(modff); \
2580 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2585 #if SANITIZER_INTERCEPT_RECVMSG
2586 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2588 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2589 if (msg->msg_name && msg->msg_namelen)
2590 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2591 if (msg->msg_iov && msg->msg_iovlen)
2592 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2593 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2594 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2595 if (msg->msg_control && msg->msg_controllen)
2596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2599 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2602 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2603 // FIXME: under ASan the call below may write to freed memory and corrupt
2604 // its metadata. See
2605 // https://github.com/google/sanitizers/issues/321.
2606 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2608 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2610 write_msghdr(ctx, msg, res);
2611 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2616 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2618 #define INIT_RECVMSG
2621 #if SANITIZER_INTERCEPT_SENDMSG
2622 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2623 const unsigned kCmsgDataOffset =
2624 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2626 char *p = (char *)control;
2627 char *const control_end = p + controllen;
2629 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2630 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2631 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2633 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2635 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2636 sizeof(cmsg->cmsg_level));
2637 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2638 sizeof(cmsg->cmsg_type));
2640 if (cmsg->cmsg_len > kCmsgDataOffset) {
2641 char *data = p + kCmsgDataOffset;
2642 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2643 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2646 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2650 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2653 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
2662 if (msg->msg_name && msg->msg_namelen)
2663 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2664 if (msg->msg_iov && msg->msg_iovlen)
2665 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
2666 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2667 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2668 if (msg->msg_control && msg->msg_controllen)
2669 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
2672 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
2675 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
2677 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
2678 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
2680 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
2681 if (common_flags()->intercept_send && res >= 0 && msg)
2682 read_msghdr(ctx, msg, res);
2685 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
2687 #define INIT_SENDMSG
2690 #if SANITIZER_INTERCEPT_GETPEERNAME
2691 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2693 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2695 if (addrlen) addr_sz = *addrlen;
2696 // FIXME: under ASan the call below may write to freed memory and corrupt
2697 // its metadata. See
2698 // https://github.com/google/sanitizers/issues/321.
2699 int res = REAL(getpeername)(sockfd, addr, addrlen);
2700 if (!res && addr && addrlen)
2701 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2704 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2706 #define INIT_GETPEERNAME
2709 #if SANITIZER_INTERCEPT_SYSINFO
2710 INTERCEPTOR(int, sysinfo, void *info) {
2712 // FIXME: under ASan the call below may write to freed memory and corrupt
2713 // its metadata. See
2714 // https://github.com/google/sanitizers/issues/321.
2715 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2716 int res = REAL(sysinfo)(info);
2718 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2721 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2723 #define INIT_SYSINFO
2726 #if SANITIZER_INTERCEPT_READDIR
2727 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2729 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2730 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2731 __sanitizer_dirent *res = REAL(opendir)(path);
2733 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2737 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2739 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2740 // FIXME: under ASan the call below may write to freed memory and corrupt
2741 // its metadata. See
2742 // https://github.com/google/sanitizers/issues/321.
2743 __sanitizer_dirent *res = REAL(readdir)(dirp);
2744 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2748 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2749 __sanitizer_dirent **result) {
2751 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2752 // FIXME: under ASan the call below may write to freed memory and corrupt
2753 // its metadata. See
2754 // https://github.com/google/sanitizers/issues/321.
2755 int res = REAL(readdir_r)(dirp, entry, result);
2757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2764 #define INIT_READDIR \
2765 COMMON_INTERCEPT_FUNCTION(opendir); \
2766 COMMON_INTERCEPT_FUNCTION(readdir); \
2767 COMMON_INTERCEPT_FUNCTION(readdir_r);
2769 #define INIT_READDIR
2772 #if SANITIZER_INTERCEPT_READDIR64
2773 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2775 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2776 // FIXME: under ASan the call below may write to freed memory and corrupt
2777 // its metadata. See
2778 // https://github.com/google/sanitizers/issues/321.
2779 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2780 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2784 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2785 __sanitizer_dirent64 **result) {
2787 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2788 // FIXME: under ASan the call below may write to freed memory and corrupt
2789 // its metadata. See
2790 // https://github.com/google/sanitizers/issues/321.
2791 int res = REAL(readdir64_r)(dirp, entry, result);
2793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2799 #define INIT_READDIR64 \
2800 COMMON_INTERCEPT_FUNCTION(readdir64); \
2801 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2803 #define INIT_READDIR64
2806 #if SANITIZER_INTERCEPT_PTRACE
2807 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2809 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2810 __sanitizer_iovec local_iovec;
2813 if (request == ptrace_setregs)
2814 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2815 else if (request == ptrace_setfpregs)
2816 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2817 else if (request == ptrace_setfpxregs)
2818 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2819 else if (request == ptrace_setvfpregs)
2820 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2821 else if (request == ptrace_setsiginfo)
2822 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2823 // Some kernel might zero the iovec::iov_base in case of invalid
2824 // write access. In this case copy the invalid address for further
2826 else if (request == ptrace_setregset || request == ptrace_getregset) {
2827 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2828 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
2829 local_iovec = *iovec;
2830 if (request == ptrace_setregset)
2831 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
2835 // FIXME: under ASan the call below may write to freed memory and corrupt
2836 // its metadata. See
2837 // https://github.com/google/sanitizers/issues/321.
2838 uptr res = REAL(ptrace)(request, pid, addr, data);
2841 // Note that PEEK* requests assign different meaning to the return value.
2842 // This function does not handle them (nor does it need to).
2843 if (request == ptrace_getregs)
2844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2845 else if (request == ptrace_getfpregs)
2846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2847 else if (request == ptrace_getfpxregs)
2848 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2849 else if (request == ptrace_getvfpregs)
2850 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2851 else if (request == ptrace_getsiginfo)
2852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2853 else if (request == ptrace_geteventmsg)
2854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2855 else if (request == ptrace_getregset) {
2856 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
2858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
2859 local_iovec.iov_len);
2865 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2870 #if SANITIZER_INTERCEPT_SETLOCALE
2871 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2873 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2875 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2876 char *res = REAL(setlocale)(category, locale);
2877 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2881 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2883 #define INIT_SETLOCALE
2886 #if SANITIZER_INTERCEPT_GETCWD
2887 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2889 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
2890 // FIXME: under ASan the call below may write to freed memory and corrupt
2891 // its metadata. See
2892 // https://github.com/google/sanitizers/issues/321.
2893 char *res = REAL(getcwd)(buf, size);
2894 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2897 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2902 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2903 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2905 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2906 // FIXME: under ASan the call below may write to freed memory and corrupt
2907 // its metadata. See
2908 // https://github.com/google/sanitizers/issues/321.
2909 char *res = REAL(get_current_dir_name)(fake);
2910 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2914 #define INIT_GET_CURRENT_DIR_NAME \
2915 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2917 #define INIT_GET_CURRENT_DIR_NAME
2920 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
2922 if (nptr == *endptr) {
2923 // No digits were found at strtol call, we need to find out the last
2924 // symbol accessed by strtoll on our own.
2925 // We get this symbol by skipping leading blanks and optional +/- sign.
2926 while (IsSpace(*nptr)) nptr++;
2927 if (*nptr == '+' || *nptr == '-') nptr++;
2928 *endptr = const_cast<char *>(nptr);
2930 CHECK(*endptr >= nptr);
2933 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
2934 char **endptr, char *real_endptr, int base) {
2936 *endptr = real_endptr;
2937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2939 // If base has unsupported value, strtol can exit with EINVAL
2940 // without reading any characters. So do additional checks only
2941 // if base is valid.
2942 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
2943 if (is_valid_base) {
2944 FixRealStrtolEndptr(nptr, &real_endptr);
2946 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
2947 (real_endptr - nptr) + 1 : 0);
2951 #if SANITIZER_INTERCEPT_STRTOIMAX
2952 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
2954 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
2955 // FIXME: under ASan the call below may write to freed memory and corrupt
2956 // its metadata. See
2957 // https://github.com/google/sanitizers/issues/321.
2959 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
2960 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2964 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
2966 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
2967 // FIXME: under ASan the call below may write to freed memory and corrupt
2968 // its metadata. See
2969 // https://github.com/google/sanitizers/issues/321.
2971 INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
2972 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2976 #define INIT_STRTOIMAX \
2977 COMMON_INTERCEPT_FUNCTION(strtoimax); \
2978 COMMON_INTERCEPT_FUNCTION(strtoumax);
2980 #define INIT_STRTOIMAX
2983 #if SANITIZER_INTERCEPT_MBSTOWCS
2984 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
2986 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
2987 // FIXME: under ASan the call below may write to freed memory and corrupt
2988 // its metadata. See
2989 // https://github.com/google/sanitizers/issues/321.
2990 SIZE_T res = REAL(mbstowcs)(dest, src, len);
2991 if (res != (SIZE_T) - 1 && dest) {
2992 SIZE_T write_cnt = res + (res < len);
2993 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2998 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3001 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3002 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3003 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3004 // FIXME: under ASan the call below may write to freed memory and corrupt
3005 // its metadata. See
3006 // https://github.com/google/sanitizers/issues/321.
3007 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3008 if (res != (SIZE_T)(-1) && dest && src) {
3009 // This function, and several others, may or may not write the terminating
3010 // \0 character. They write it iff they clear *src.
3011 SIZE_T write_cnt = res + !*src;
3012 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3017 #define INIT_MBSTOWCS \
3018 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3019 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3021 #define INIT_MBSTOWCS
3024 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3025 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3026 SIZE_T len, void *ps) {
3028 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3030 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3031 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3033 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3034 // FIXME: under ASan the call below may write to freed memory and corrupt
3035 // its metadata. See
3036 // https://github.com/google/sanitizers/issues/321.
3037 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3038 if (res != (SIZE_T)(-1) && dest && src) {
3039 SIZE_T write_cnt = res + !*src;
3040 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3045 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3047 #define INIT_MBSNRTOWCS
3050 #if SANITIZER_INTERCEPT_WCSTOMBS
3051 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3053 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3054 // FIXME: under ASan the call below may write to freed memory and corrupt
3055 // its metadata. See
3056 // https://github.com/google/sanitizers/issues/321.
3057 SIZE_T res = REAL(wcstombs)(dest, src, len);
3058 if (res != (SIZE_T) - 1 && dest) {
3059 SIZE_T write_cnt = res + (res < len);
3060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3065 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3068 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3069 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3070 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3071 // FIXME: under ASan the call below may write to freed memory and corrupt
3072 // its metadata. See
3073 // https://github.com/google/sanitizers/issues/321.
3074 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3075 if (res != (SIZE_T) - 1 && dest && src) {
3076 SIZE_T write_cnt = res + !*src;
3077 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3082 #define INIT_WCSTOMBS \
3083 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3084 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3086 #define INIT_WCSTOMBS
3089 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3090 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3091 SIZE_T len, void *ps) {
3093 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3095 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3096 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3098 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3099 // FIXME: under ASan the call below may write to freed memory and corrupt
3100 // its metadata. See
3101 // https://github.com/google/sanitizers/issues/321.
3102 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3103 if (res != ((SIZE_T)-1) && dest && src) {
3104 SIZE_T write_cnt = res + !*src;
3105 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3110 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3112 #define INIT_WCSNRTOMBS
3116 #if SANITIZER_INTERCEPT_WCRTOMB
3117 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3119 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3120 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3121 // FIXME: under ASan the call below may write to freed memory and corrupt
3122 // its metadata. See
3123 // https://github.com/google/sanitizers/issues/321.
3124 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3125 if (res != ((SIZE_T)-1) && dest) {
3126 SIZE_T write_cnt = res;
3127 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3132 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3134 #define INIT_WCRTOMB
3137 #if SANITIZER_INTERCEPT_TCGETATTR
3138 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3140 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3141 // FIXME: under ASan the call below may write to freed memory and corrupt
3142 // its metadata. See
3143 // https://github.com/google/sanitizers/issues/321.
3144 int res = REAL(tcgetattr)(fd, termios_p);
3145 if (!res && termios_p)
3146 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3150 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3152 #define INIT_TCGETATTR
3155 #if SANITIZER_INTERCEPT_REALPATH
3156 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3158 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3159 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3161 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3162 // version of a versioned symbol. For realpath(), this gives us something
3163 // (called __old_realpath) that does not handle NULL in the second argument.
3164 // Handle it as part of the interceptor.
3165 char *allocated_path = nullptr;
3167 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3169 char *res = REAL(realpath)(path, resolved_path);
3170 if (allocated_path && !res) WRAP(free)(allocated_path);
3171 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3174 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3176 #define INIT_REALPATH
3179 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3180 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3182 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3183 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3184 char *res = REAL(canonicalize_file_name)(path);
3185 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3188 #define INIT_CANONICALIZE_FILE_NAME \
3189 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3191 #define INIT_CANONICALIZE_FILE_NAME
3194 #if SANITIZER_INTERCEPT_CONFSTR
3195 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3197 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3198 // FIXME: under ASan the call below may write to freed memory and corrupt
3199 // its metadata. See
3200 // https://github.com/google/sanitizers/issues/321.
3201 SIZE_T res = REAL(confstr)(name, buf, len);
3203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3206 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3208 #define INIT_CONFSTR
3211 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3212 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3214 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3215 // FIXME: under ASan the call below may write to freed memory and corrupt
3216 // its metadata. See
3217 // https://github.com/google/sanitizers/issues/321.
3218 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3219 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3222 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3224 #define INIT_SCHED_GETAFFINITY
3227 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3228 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3230 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3231 int res = REAL(sched_getparam)(pid, param);
3232 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3235 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3237 #define INIT_SCHED_GETPARAM
3240 #if SANITIZER_INTERCEPT_STRERROR
3241 INTERCEPTOR(char *, strerror, int errnum) {
3243 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3244 char *res = REAL(strerror)(errnum);
3245 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3248 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3250 #define INIT_STRERROR
3253 #if SANITIZER_INTERCEPT_STRERROR_R
3254 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3256 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3257 // FIXME: under ASan the call below may write to freed memory and corrupt
3258 // its metadata. See
3259 // https://github.com/google/sanitizers/issues/321.
3260 char *res = REAL(strerror_r)(errnum, buf, buflen);
3261 // There are 2 versions of strerror_r:
3262 // * POSIX version returns 0 on success, negative error code on failure,
3263 // writes message to buf.
3264 // * GNU version returns message pointer, which points to either buf or some
3266 SIZE_T posix_res = (SIZE_T)res;
3267 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
3268 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3269 // At least on OSX, buf contents are valid even when the call fails.
3270 SIZE_T sz = internal_strnlen(buf, buflen);
3271 if (sz < buflen) ++sz;
3272 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3275 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3279 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3281 #define INIT_STRERROR_R
3284 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3285 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3287 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
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(__xpg_strerror_r)(errnum, buf, buflen);
3292 // This version always returns a null-terminated string.
3294 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3297 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3299 #define INIT_XPG_STRERROR_R
3302 #if SANITIZER_INTERCEPT_SCANDIR
3303 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3304 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3305 const struct __sanitizer_dirent **);
3307 static THREADLOCAL scandir_filter_f scandir_filter;
3308 static THREADLOCAL scandir_compar_f scandir_compar;
3310 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3311 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3312 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3313 return scandir_filter(dir);
3316 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3317 const struct __sanitizer_dirent **b) {
3318 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3319 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3320 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3321 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3322 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3323 return scandir_compar(a, b);
3326 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3327 scandir_filter_f filter, scandir_compar_f compar) {
3329 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3330 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3331 scandir_filter = filter;
3332 scandir_compar = compar;
3333 // FIXME: under ASan the call below may write to freed memory and corrupt
3334 // its metadata. See
3335 // https://github.com/google/sanitizers/issues/321.
3336 int res = REAL(scandir)(dirp, namelist,
3337 filter ? wrapped_scandir_filter : nullptr,
3338 compar ? wrapped_scandir_compar : nullptr);
3339 scandir_filter = nullptr;
3340 scandir_compar = nullptr;
3341 if (namelist && res > 0) {
3342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3343 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3344 for (int i = 0; i < res; ++i)
3345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3346 (*namelist)[i]->d_reclen);
3350 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3352 #define INIT_SCANDIR
3355 #if SANITIZER_INTERCEPT_SCANDIR64
3356 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3357 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3358 const struct __sanitizer_dirent64 **);
3360 static THREADLOCAL scandir64_filter_f scandir64_filter;
3361 static THREADLOCAL scandir64_compar_f scandir64_compar;
3363 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3364 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3365 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3366 return scandir64_filter(dir);
3369 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3370 const struct __sanitizer_dirent64 **b) {
3371 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3372 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3373 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3374 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3375 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3376 return scandir64_compar(a, b);
3379 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3380 scandir64_filter_f filter, scandir64_compar_f compar) {
3382 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3383 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3384 scandir64_filter = filter;
3385 scandir64_compar = compar;
3386 // FIXME: under ASan the call below may write to freed memory and corrupt
3387 // its metadata. See
3388 // https://github.com/google/sanitizers/issues/321.
3390 REAL(scandir64)(dirp, namelist,
3391 filter ? wrapped_scandir64_filter : nullptr,
3392 compar ? wrapped_scandir64_compar : nullptr);
3393 scandir64_filter = nullptr;
3394 scandir64_compar = nullptr;
3395 if (namelist && res > 0) {
3396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3397 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3398 for (int i = 0; i < res; ++i)
3399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3400 (*namelist)[i]->d_reclen);
3404 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3406 #define INIT_SCANDIR64
3409 #if SANITIZER_INTERCEPT_GETGROUPS
3410 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3412 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3413 // FIXME: under ASan the call below may write to freed memory and corrupt
3414 // its metadata. See
3415 // https://github.com/google/sanitizers/issues/321.
3416 int res = REAL(getgroups)(size, lst);
3417 if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3420 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3422 #define INIT_GETGROUPS
3425 #if SANITIZER_INTERCEPT_POLL
3426 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3427 __sanitizer_nfds_t nfds) {
3428 for (unsigned i = 0; i < nfds; ++i) {
3429 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3430 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3434 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3435 __sanitizer_nfds_t nfds) {
3436 for (unsigned i = 0; i < nfds; ++i)
3437 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3438 sizeof(fds[i].revents));
3441 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3444 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3445 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3446 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3447 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3450 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3455 #if SANITIZER_INTERCEPT_PPOLL
3456 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3457 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3459 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3460 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3462 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3463 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3465 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3466 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3469 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3474 #if SANITIZER_INTERCEPT_WORDEXP
3475 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3477 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3478 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3479 // FIXME: under ASan the call below may write to freed memory and corrupt
3480 // its metadata. See
3481 // https://github.com/google/sanitizers/issues/321.
3482 int res = REAL(wordexp)(s, p, flags);
3484 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3486 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3487 sizeof(*p->we_wordv) * p->we_wordc);
3488 for (uptr i = 0; i < p->we_wordc; ++i) {
3489 char *w = p->we_wordv[i];
3490 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3495 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3497 #define INIT_WORDEXP
3500 #if SANITIZER_INTERCEPT_SIGWAIT
3501 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3503 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3504 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3505 // FIXME: under ASan the call below may write to freed memory and corrupt
3506 // its metadata. See
3507 // https://github.com/google/sanitizers/issues/321.
3508 int res = REAL(sigwait)(set, sig);
3509 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3512 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3514 #define INIT_SIGWAIT
3517 #if SANITIZER_INTERCEPT_SIGWAITINFO
3518 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3520 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3521 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3522 // FIXME: under ASan the call below may write to freed memory and corrupt
3523 // its metadata. See
3524 // https://github.com/google/sanitizers/issues/321.
3525 int res = REAL(sigwaitinfo)(set, info);
3526 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3529 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3531 #define INIT_SIGWAITINFO
3534 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3535 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3538 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3539 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3540 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3541 // FIXME: under ASan the call below may write to freed memory and corrupt
3542 // its metadata. See
3543 // https://github.com/google/sanitizers/issues/321.
3544 int res = REAL(sigtimedwait)(set, info, timeout);
3545 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3548 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3550 #define INIT_SIGTIMEDWAIT
3553 #if SANITIZER_INTERCEPT_SIGSETOPS
3554 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3556 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3557 // FIXME: under ASan the call below may write to freed memory and corrupt
3558 // its metadata. See
3559 // https://github.com/google/sanitizers/issues/321.
3560 int res = REAL(sigemptyset)(set);
3561 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3565 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3567 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3568 // FIXME: under ASan the call below may write to freed memory and corrupt
3569 // its metadata. See
3570 // https://github.com/google/sanitizers/issues/321.
3571 int res = REAL(sigfillset)(set);
3572 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3575 #define INIT_SIGSETOPS \
3576 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3577 COMMON_INTERCEPT_FUNCTION(sigfillset);
3579 #define INIT_SIGSETOPS
3582 #if SANITIZER_INTERCEPT_SIGPENDING
3583 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3585 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3586 // FIXME: under ASan the call below may write to freed memory and corrupt
3587 // its metadata. See
3588 // https://github.com/google/sanitizers/issues/321.
3589 int res = REAL(sigpending)(set);
3590 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3593 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3595 #define INIT_SIGPENDING
3598 #if SANITIZER_INTERCEPT_SIGPROCMASK
3599 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3600 __sanitizer_sigset_t *oldset) {
3602 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3603 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3604 // FIXME: under ASan the call below may write to freed memory and corrupt
3605 // its metadata. See
3606 // https://github.com/google/sanitizers/issues/321.
3607 int res = REAL(sigprocmask)(how, set, oldset);
3609 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3612 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3614 #define INIT_SIGPROCMASK
3617 #if SANITIZER_INTERCEPT_BACKTRACE
3618 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3620 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3621 // FIXME: under ASan the call below may write to freed memory and corrupt
3622 // its metadata. See
3623 // https://github.com/google/sanitizers/issues/321.
3624 int res = REAL(backtrace)(buffer, size);
3626 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3630 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3632 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3634 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3635 // FIXME: under ASan the call below may write to freed memory and corrupt
3636 // its metadata. See
3637 // https://github.com/google/sanitizers/issues/321.
3638 char **res = REAL(backtrace_symbols)(buffer, size);
3640 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3641 for (int i = 0; i < size; ++i)
3642 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3646 #define INIT_BACKTRACE \
3647 COMMON_INTERCEPT_FUNCTION(backtrace); \
3648 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3650 #define INIT_BACKTRACE
3653 #if SANITIZER_INTERCEPT__EXIT
3654 INTERCEPTOR(void, _exit, int status) {
3656 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3657 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3658 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3659 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3660 if (status == 0) status = status1;
3661 REAL(_exit)(status);
3663 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3668 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3669 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3671 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3672 int res = REAL(pthread_mutex_lock)(m);
3673 if (res == errno_EOWNERDEAD)
3674 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3675 if (res == 0 || res == errno_EOWNERDEAD)
3676 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
3677 if (res == errno_EINVAL)
3678 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3682 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3684 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3685 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3686 int res = REAL(pthread_mutex_unlock)(m);
3687 if (res == errno_EINVAL)
3688 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3692 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3693 #define INIT_PTHREAD_MUTEX_UNLOCK \
3694 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3696 #define INIT_PTHREAD_MUTEX_LOCK
3697 #define INIT_PTHREAD_MUTEX_UNLOCK
3700 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3701 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3703 if (mnt->mnt_fsname)
3704 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3705 REAL(strlen)(mnt->mnt_fsname) + 1);
3707 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3708 REAL(strlen)(mnt->mnt_dir) + 1);
3710 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3711 REAL(strlen)(mnt->mnt_type) + 1);
3713 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3714 REAL(strlen)(mnt->mnt_opts) + 1);
3718 #if SANITIZER_INTERCEPT_GETMNTENT
3719 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3721 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3722 __sanitizer_mntent *res = REAL(getmntent)(fp);
3723 if (res) write_mntent(ctx, res);
3726 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3728 #define INIT_GETMNTENT
3731 #if SANITIZER_INTERCEPT_GETMNTENT_R
3732 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3733 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3735 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3736 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3737 if (res) write_mntent(ctx, res);
3740 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3742 #define INIT_GETMNTENT_R
3745 #if SANITIZER_INTERCEPT_STATFS
3746 INTERCEPTOR(int, statfs, char *path, void *buf) {
3748 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3749 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3750 // FIXME: under ASan the call below may write to freed memory and corrupt
3751 // its metadata. See
3752 // https://github.com/google/sanitizers/issues/321.
3753 int res = REAL(statfs)(path, buf);
3754 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3757 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3759 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3760 // FIXME: under ASan the call below may write to freed memory and corrupt
3761 // its metadata. See
3762 // https://github.com/google/sanitizers/issues/321.
3763 int res = REAL(fstatfs)(fd, buf);
3764 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3767 #define INIT_STATFS \
3768 COMMON_INTERCEPT_FUNCTION(statfs); \
3769 COMMON_INTERCEPT_FUNCTION(fstatfs);
3774 #if SANITIZER_INTERCEPT_STATFS64
3775 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3777 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3778 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3779 // FIXME: under ASan the call below may write to freed memory and corrupt
3780 // its metadata. See
3781 // https://github.com/google/sanitizers/issues/321.
3782 int res = REAL(statfs64)(path, buf);
3783 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3786 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3788 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3789 // FIXME: under ASan the call below may write to freed memory and corrupt
3790 // its metadata. See
3791 // https://github.com/google/sanitizers/issues/321.
3792 int res = REAL(fstatfs64)(fd, buf);
3793 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3796 #define INIT_STATFS64 \
3797 COMMON_INTERCEPT_FUNCTION(statfs64); \
3798 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3800 #define INIT_STATFS64
3803 #if SANITIZER_INTERCEPT_STATVFS
3804 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3806 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3807 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3808 // FIXME: under ASan the call below may write to freed memory and corrupt
3809 // its metadata. See
3810 // https://github.com/google/sanitizers/issues/321.
3811 int res = REAL(statvfs)(path, buf);
3812 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3815 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3817 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3818 // FIXME: under ASan the call below may write to freed memory and corrupt
3819 // its metadata. See
3820 // https://github.com/google/sanitizers/issues/321.
3821 int res = REAL(fstatvfs)(fd, buf);
3822 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3825 #define INIT_STATVFS \
3826 COMMON_INTERCEPT_FUNCTION(statvfs); \
3827 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3829 #define INIT_STATVFS
3832 #if SANITIZER_INTERCEPT_STATVFS64
3833 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3835 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3836 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3837 // FIXME: under ASan the call below may write to freed memory and corrupt
3838 // its metadata. See
3839 // https://github.com/google/sanitizers/issues/321.
3840 int res = REAL(statvfs64)(path, buf);
3841 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3844 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3846 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3847 // FIXME: under ASan the call below may write to freed memory and corrupt
3848 // its metadata. See
3849 // https://github.com/google/sanitizers/issues/321.
3850 int res = REAL(fstatvfs64)(fd, buf);
3851 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3854 #define INIT_STATVFS64 \
3855 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3856 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3858 #define INIT_STATVFS64
3861 #if SANITIZER_INTERCEPT_INITGROUPS
3862 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3864 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3865 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3866 int res = REAL(initgroups)(user, group);
3869 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3871 #define INIT_INITGROUPS
3874 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3875 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3877 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3878 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3879 char *res = REAL(ether_ntoa)(addr);
3880 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3883 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3885 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3886 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3887 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3888 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3891 #define INIT_ETHER_NTOA_ATON \
3892 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3893 COMMON_INTERCEPT_FUNCTION(ether_aton);
3895 #define INIT_ETHER_NTOA_ATON
3898 #if SANITIZER_INTERCEPT_ETHER_HOST
3899 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3901 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3902 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3903 // FIXME: under ASan the call below may write to freed memory and corrupt
3904 // its metadata. See
3905 // https://github.com/google/sanitizers/issues/321.
3906 int res = REAL(ether_ntohost)(hostname, addr);
3907 if (!res && hostname)
3908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3911 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3913 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3915 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3916 // FIXME: under ASan the call below may write to freed memory and corrupt
3917 // its metadata. See
3918 // https://github.com/google/sanitizers/issues/321.
3919 int res = REAL(ether_hostton)(hostname, addr);
3920 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3923 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3926 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3927 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3928 // FIXME: under ASan the call below may write to freed memory and corrupt
3929 // its metadata. See
3930 // https://github.com/google/sanitizers/issues/321.
3931 int res = REAL(ether_line)(line, addr, hostname);
3933 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3939 #define INIT_ETHER_HOST \
3940 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3941 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3942 COMMON_INTERCEPT_FUNCTION(ether_line);
3944 #define INIT_ETHER_HOST
3947 #if SANITIZER_INTERCEPT_ETHER_R
3948 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3950 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3951 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3952 // FIXME: under ASan the call below may write to freed memory and corrupt
3953 // its metadata. See
3954 // https://github.com/google/sanitizers/issues/321.
3955 char *res = REAL(ether_ntoa_r)(addr, buf);
3956 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3959 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3960 __sanitizer_ether_addr *addr) {
3962 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3963 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3964 // FIXME: under ASan the call below may write to freed memory and corrupt
3965 // its metadata. See
3966 // https://github.com/google/sanitizers/issues/321.
3967 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3968 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3971 #define INIT_ETHER_R \
3972 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3973 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3975 #define INIT_ETHER_R
3978 #if SANITIZER_INTERCEPT_SHMCTL
3979 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3981 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3982 // FIXME: under ASan the call below may write to freed memory and corrupt
3983 // its metadata. See
3984 // https://github.com/google/sanitizers/issues/321.
3985 int res = REAL(shmctl)(shmid, cmd, buf);
3988 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3989 sz = sizeof(__sanitizer_shmid_ds);
3990 else if (cmd == shmctl_ipc_info)
3991 sz = struct_shminfo_sz;
3992 else if (cmd == shmctl_shm_info)
3993 sz = struct_shm_info_sz;
3994 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3998 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4003 #if SANITIZER_INTERCEPT_RANDOM_R
4004 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4006 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4007 // FIXME: under ASan the call below may write to freed memory and corrupt
4008 // its metadata. See
4009 // https://github.com/google/sanitizers/issues/321.
4010 int res = REAL(random_r)(buf, result);
4012 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4015 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4017 #define INIT_RANDOM_R
4020 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4021 // its metadata. See
4022 // https://github.com/google/sanitizers/issues/321.
4023 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4024 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4025 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4026 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4027 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4028 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4029 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4030 INTERCEPTOR(int, fn, void *attr, void *r) { \
4032 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4033 int res = REAL(fn)(attr, r); \
4034 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4037 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4038 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4039 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4040 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4041 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4042 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4043 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4044 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4045 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4046 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4049 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4050 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4051 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4052 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4053 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4054 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4055 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4056 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4058 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4059 // FIXME: under ASan the call below may write to freed memory and corrupt
4060 // its metadata. See
4061 // https://github.com/google/sanitizers/issues/321.
4062 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4064 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4065 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4070 // We may need to call the real pthread_attr_getstack from the run-time
4071 // in sanitizer_common, but we don't want to include the interception headers
4072 // there. So, just define this function here.
4073 namespace __sanitizer {
4075 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4076 return REAL(pthread_attr_getstack)(attr, addr, size);
4079 } // namespace __sanitizer
4081 #define INIT_PTHREAD_ATTR_GET \
4082 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4083 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4084 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4085 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4086 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4087 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4088 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4090 #define INIT_PTHREAD_ATTR_GET
4093 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4094 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4096 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4097 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4099 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4102 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4103 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4106 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4108 // FIXME: under ASan the call below may write to freed memory and corrupt
4109 // its metadata. See
4110 // https://github.com/google/sanitizers/issues/321.
4111 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4112 if (!res && cpusetsize && cpuset)
4113 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4117 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4118 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4120 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4123 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4124 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4125 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4126 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4128 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4131 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4132 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4133 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4134 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4136 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4139 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4140 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4141 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4142 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4144 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4147 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4148 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4149 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4150 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4152 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4155 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4156 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4157 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4158 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4160 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4163 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4164 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4165 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4166 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4168 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4171 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4172 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4173 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4174 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4176 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4179 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4180 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4181 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4182 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4184 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4187 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4188 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4189 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4190 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4192 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4195 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4196 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4197 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4198 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4200 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4203 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4204 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4205 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4206 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4208 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4211 #if SANITIZER_INTERCEPT_TMPNAM
4212 INTERCEPTOR(char *, tmpnam, char *s) {
4214 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4215 char *res = REAL(tmpnam)(s);
4218 // FIXME: under ASan the call below may write to freed memory and corrupt
4219 // its metadata. See
4220 // https://github.com/google/sanitizers/issues/321.
4221 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4223 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4227 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4232 #if SANITIZER_INTERCEPT_TMPNAM_R
4233 INTERCEPTOR(char *, tmpnam_r, char *s) {
4235 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4236 // FIXME: under ASan the call below may write to freed memory and corrupt
4237 // its metadata. See
4238 // https://github.com/google/sanitizers/issues/321.
4239 char *res = REAL(tmpnam_r)(s);
4240 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4243 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4245 #define INIT_TMPNAM_R
4248 #if SANITIZER_INTERCEPT_TTYNAME_R
4249 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4251 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4252 int res = REAL(ttyname_r)(fd, name, namesize);
4254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4257 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4259 #define INIT_TTYNAME_R
4262 #if SANITIZER_INTERCEPT_TEMPNAM
4263 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4265 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4266 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4267 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4268 char *res = REAL(tempnam)(dir, pfx);
4269 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4272 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4274 #define INIT_TEMPNAM
4277 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4278 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4280 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4281 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4282 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4283 return REAL(pthread_setname_np)(thread, name);
4285 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4287 #define INIT_PTHREAD_SETNAME_NP
4290 #if SANITIZER_INTERCEPT_SINCOS
4291 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4293 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4294 // FIXME: under ASan the call below may write to freed memory and corrupt
4295 // its metadata. See
4296 // https://github.com/google/sanitizers/issues/321.
4297 REAL(sincos)(x, sin, cos);
4298 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4299 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4301 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4303 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
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 REAL(sincosf)(x, sin, cos);
4308 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4309 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4311 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4313 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4314 // FIXME: under ASan the call below may write to freed memory and corrupt
4315 // its metadata. See
4316 // https://github.com/google/sanitizers/issues/321.
4317 REAL(sincosl)(x, sin, cos);
4318 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4319 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4321 #define INIT_SINCOS \
4322 COMMON_INTERCEPT_FUNCTION(sincos); \
4323 COMMON_INTERCEPT_FUNCTION(sincosf); \
4324 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4329 #if SANITIZER_INTERCEPT_REMQUO
4330 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4332 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4333 // FIXME: under ASan the call below may write to freed memory and corrupt
4334 // its metadata. See
4335 // https://github.com/google/sanitizers/issues/321.
4336 double res = REAL(remquo)(x, y, quo);
4337 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4340 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4342 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4343 // FIXME: under ASan the call below may write to freed memory and corrupt
4344 // its metadata. See
4345 // https://github.com/google/sanitizers/issues/321.
4346 float res = REAL(remquof)(x, y, quo);
4347 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4350 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4352 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4353 // FIXME: under ASan the call below may write to freed memory and corrupt
4354 // its metadata. See
4355 // https://github.com/google/sanitizers/issues/321.
4356 long double res = REAL(remquol)(x, y, quo);
4357 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4360 #define INIT_REMQUO \
4361 COMMON_INTERCEPT_FUNCTION(remquo); \
4362 COMMON_INTERCEPT_FUNCTION(remquof); \
4363 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4368 #if SANITIZER_INTERCEPT_LGAMMA
4370 INTERCEPTOR(double, lgamma, double x) {
4372 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4373 double res = REAL(lgamma)(x);
4374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4377 INTERCEPTOR(float, lgammaf, float x) {
4379 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4380 float res = REAL(lgammaf)(x);
4381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4384 INTERCEPTOR(long double, lgammal, long double x) {
4386 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4387 long double res = REAL(lgammal)(x);
4388 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4391 #define INIT_LGAMMA \
4392 COMMON_INTERCEPT_FUNCTION(lgamma); \
4393 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4394 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4399 #if SANITIZER_INTERCEPT_LGAMMA_R
4400 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4402 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4403 // FIXME: under ASan the call below may write to freed memory and corrupt
4404 // its metadata. See
4405 // https://github.com/google/sanitizers/issues/321.
4406 double res = REAL(lgamma_r)(x, signp);
4407 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4410 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4412 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4413 // FIXME: under ASan the call below may write to freed memory and corrupt
4414 // its metadata. See
4415 // https://github.com/google/sanitizers/issues/321.
4416 float res = REAL(lgammaf_r)(x, signp);
4417 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4420 #define INIT_LGAMMA_R \
4421 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4422 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4424 #define INIT_LGAMMA_R
4427 #if SANITIZER_INTERCEPT_LGAMMAL_R
4428 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4430 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4431 // FIXME: under ASan the call below may write to freed memory and corrupt
4432 // its metadata. See
4433 // https://github.com/google/sanitizers/issues/321.
4434 long double res = REAL(lgammal_r)(x, signp);
4435 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4438 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4440 #define INIT_LGAMMAL_R
4443 #if SANITIZER_INTERCEPT_DRAND48_R
4444 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4446 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4447 // FIXME: under ASan the call below may write to freed memory and corrupt
4448 // its metadata. See
4449 // https://github.com/google/sanitizers/issues/321.
4450 int res = REAL(drand48_r)(buffer, result);
4451 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4454 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4456 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4457 // FIXME: under ASan the call below may write to freed memory and corrupt
4458 // its metadata. See
4459 // https://github.com/google/sanitizers/issues/321.
4460 int res = REAL(lrand48_r)(buffer, result);
4461 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4464 #define INIT_DRAND48_R \
4465 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4466 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4468 #define INIT_DRAND48_R
4471 #if SANITIZER_INTERCEPT_RAND_R
4472 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4474 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4475 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4476 return REAL(rand_r)(seedp);
4478 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4483 #if SANITIZER_INTERCEPT_GETLINE
4484 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4486 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4487 // FIXME: under ASan the call below may write to freed memory and corrupt
4488 // its metadata. See
4489 // https://github.com/google/sanitizers/issues/321.
4490 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4492 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4493 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4494 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4499 // FIXME: under ASan the call below may write to freed memory and corrupt its
4501 // https://github.com/google/sanitizers/issues/321.
4502 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4505 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4506 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4508 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4509 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4515 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4517 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4519 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4520 // with its own body.
4521 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4523 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4525 #define INIT_GETLINE \
4526 COMMON_INTERCEPT_FUNCTION(getline); \
4527 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4528 COMMON_INTERCEPT_FUNCTION(getdelim);
4530 #define INIT_GETLINE
4533 #if SANITIZER_INTERCEPT_ICONV
4534 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4535 char **outbuf, SIZE_T *outbytesleft) {
4537 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4540 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4541 if (inbuf && inbytesleft)
4542 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4544 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4545 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4546 // FIXME: under ASan the call below may write to freed memory and corrupt
4547 // its metadata. See
4548 // https://github.com/google/sanitizers/issues/321.
4549 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4550 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
4551 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4552 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4556 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4561 #if SANITIZER_INTERCEPT_TIMES
4562 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4564 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4565 // FIXME: under ASan the call below may write to freed memory and corrupt
4566 // its metadata. See
4567 // https://github.com/google/sanitizers/issues/321.
4568 __sanitizer_clock_t res = REAL(times)(tms);
4569 if (res != (__sanitizer_clock_t)-1 && tms)
4570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4573 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4578 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4580 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4581 // If you see any crashes around this functions, there are 2 known issues with
4582 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4583 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4584 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4585 // to access thread local variables (it should not happen normally,
4586 // because sanitizers use initial-exec tls model).
4587 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4589 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4590 void *res = REAL(__tls_get_addr)(arg);
4591 uptr tls_begin, tls_end;
4592 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4593 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4595 // New DTLS block has been allocated.
4596 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4601 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4602 // mostly the same semantics as __tls_get_addr, but its presence enables
4603 // some optimizations in linker (which are safe to ignore here).
4604 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4605 visibility("default")))
4606 void *__tls_get_addr_opt(void *arg);
4608 #else // SANITIZER_S390
4609 // On s390, we have to intercept two functions here:
4610 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4611 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4612 // a proper pointer. It is used by dlsym for TLS symbols.
4613 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4614 // descriptor offset as an argument instead of a pointer. GOT address
4615 // is passed in r12, so it's necessary to write it in assembly. This is
4616 // the function used by the compiler.
4617 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr_internal)
4618 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4620 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4621 uptr res = REAL(__tls_get_addr_internal)(arg);
4622 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4623 void *ptr = reinterpret_cast<void *>(res + tp);
4624 uptr tls_begin, tls_end;
4625 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4626 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4628 // New DTLS block has been allocated.
4629 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4633 // We need a protected symbol aliasing the above, so that we can jump
4634 // directly to it from the assembly below.
4635 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4636 visibility("protected")))
4637 uptr __interceptor___tls_get_addr_internal_protected(void *arg);
4638 // Now carefully intercept __tls_get_offset.
4641 ".global __tls_get_offset\n"
4642 "__tls_get_offset:\n"
4643 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4644 // exports our symbol.
4645 ".global __interceptor___tls_get_offset\n"
4646 "__interceptor___tls_get_offset:\n"
4648 "la %r2, 0(%r2,%r12)\n"
4649 "jg __interceptor___tls_get_addr_internal_protected\n"
4652 "0: la %r2,0(%r2,%r12)\n"
4653 "l %r4,1f-0b(%r3)\n"
4655 "1: .long __interceptor___tls_get_addr_internal_protected - 0b\n"
4657 ".type __tls_get_offset, @function\n"
4658 ".size __tls_get_offset, .-__tls_get_offset\n"
4660 #endif // SANITIZER_S390
4662 #define INIT_TLS_GET_ADDR
4665 #if SANITIZER_INTERCEPT_LISTXATTR
4666 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4668 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4669 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4670 // FIXME: under ASan the call below may write to freed memory and corrupt
4671 // its metadata. See
4672 // https://github.com/google/sanitizers/issues/321.
4673 SSIZE_T res = REAL(listxattr)(path, list, size);
4674 // Here and below, size == 0 is a special case where nothing is written to the
4675 // buffer, and res contains the desired buffer size.
4676 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4679 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4681 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4682 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4683 // FIXME: under ASan the call below may write to freed memory and corrupt
4684 // its metadata. See
4685 // https://github.com/google/sanitizers/issues/321.
4686 SSIZE_T res = REAL(llistxattr)(path, list, size);
4687 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4690 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4692 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4693 // FIXME: under ASan the call below may write to freed memory and corrupt
4694 // its metadata. See
4695 // https://github.com/google/sanitizers/issues/321.
4696 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4697 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4700 #define INIT_LISTXATTR \
4701 COMMON_INTERCEPT_FUNCTION(listxattr); \
4702 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4703 COMMON_INTERCEPT_FUNCTION(flistxattr);
4705 #define INIT_LISTXATTR
4708 #if SANITIZER_INTERCEPT_GETXATTR
4709 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4712 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4713 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4714 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4715 // FIXME: under ASan the call below may write to freed memory and corrupt
4716 // its metadata. See
4717 // https://github.com/google/sanitizers/issues/321.
4718 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4719 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4722 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4725 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4726 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4727 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4728 // FIXME: under ASan the call below may write to freed memory and corrupt
4729 // its metadata. See
4730 // https://github.com/google/sanitizers/issues/321.
4731 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4732 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4735 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4738 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4739 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4740 // FIXME: under ASan the call below may write to freed memory and corrupt
4741 // its metadata. See
4742 // https://github.com/google/sanitizers/issues/321.
4743 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4744 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4747 #define INIT_GETXATTR \
4748 COMMON_INTERCEPT_FUNCTION(getxattr); \
4749 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4750 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4752 #define INIT_GETXATTR
4755 #if SANITIZER_INTERCEPT_GETRESID
4756 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4758 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4759 // FIXME: under ASan the call below may write to freed memory and corrupt
4760 // its metadata. See
4761 // https://github.com/google/sanitizers/issues/321.
4762 int res = REAL(getresuid)(ruid, euid, suid);
4764 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4765 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4766 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4770 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4772 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4773 // FIXME: under ASan the call below may write to freed memory and corrupt
4774 // its metadata. See
4775 // https://github.com/google/sanitizers/issues/321.
4776 int res = REAL(getresgid)(rgid, egid, sgid);
4778 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4779 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4780 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4784 #define INIT_GETRESID \
4785 COMMON_INTERCEPT_FUNCTION(getresuid); \
4786 COMMON_INTERCEPT_FUNCTION(getresgid);
4788 #define INIT_GETRESID
4791 #if SANITIZER_INTERCEPT_GETIFADDRS
4792 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4793 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4794 // intercept it to poison the memory again.
4795 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4797 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4798 // FIXME: under ASan the call below may write to freed memory and corrupt
4799 // its metadata. See
4800 // https://github.com/google/sanitizers/issues/321.
4801 int res = REAL(getifaddrs)(ifap);
4802 if (res == 0 && ifap) {
4803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4804 __sanitizer_ifaddrs *p = *ifap;
4806 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4809 REAL(strlen)(p->ifa_name) + 1);
4811 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4813 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4814 // On Linux this is a union, but the other member also points to a
4815 // struct sockaddr, so the following is sufficient.
4817 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4818 // FIXME(smatveev): Unpoison p->ifa_data as well.
4824 #define INIT_GETIFADDRS \
4825 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4827 #define INIT_GETIFADDRS
4830 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4831 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4833 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4834 // FIXME: under ASan the call below may write to freed memory and corrupt
4835 // its metadata. See
4836 // https://github.com/google/sanitizers/issues/321.
4837 char *res = REAL(if_indextoname)(ifindex, ifname);
4839 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4842 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4844 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4846 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4847 return REAL(if_nametoindex)(ifname);
4849 #define INIT_IF_INDEXTONAME \
4850 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4851 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4853 #define INIT_IF_INDEXTONAME
4856 #if SANITIZER_INTERCEPT_CAPGET
4857 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4859 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4861 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4862 // FIXME: under ASan the call below may write to freed memory and corrupt
4863 // its metadata. See
4864 // https://github.com/google/sanitizers/issues/321.
4865 int res = REAL(capget)(hdrp, datap);
4866 if (res == 0 && datap)
4867 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4868 // We can also return -1 and write to hdrp->version if the version passed in
4869 // hdrp->version is unsupported. But that's not a trivial condition to check,
4870 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4873 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4875 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4877 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4879 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4880 return REAL(capset)(hdrp, datap);
4882 #define INIT_CAPGET \
4883 COMMON_INTERCEPT_FUNCTION(capget); \
4884 COMMON_INTERCEPT_FUNCTION(capset);
4889 #if SANITIZER_INTERCEPT_AEABI_MEM
4890 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4892 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
4895 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4897 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
4900 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4902 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
4905 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4907 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
4910 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4912 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
4915 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4917 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
4920 // Note the argument order.
4921 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4923 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
4926 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4928 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
4931 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4933 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
4936 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4938 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
4941 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4943 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
4946 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4948 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
4951 #define INIT_AEABI_MEM \
4952 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
4953 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4954 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4955 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
4956 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
4957 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
4958 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
4959 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
4960 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
4961 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
4962 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
4963 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4965 #define INIT_AEABI_MEM
4966 #endif // SANITIZER_INTERCEPT_AEABI_MEM
4968 #if SANITIZER_INTERCEPT___BZERO
4969 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4971 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
4974 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4976 #define INIT___BZERO
4977 #endif // SANITIZER_INTERCEPT___BZERO
4979 #if SANITIZER_INTERCEPT_FTIME
4980 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4982 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
4983 // FIXME: under ASan the call below may write to freed memory and corrupt
4984 // its metadata. See
4985 // https://github.com/google/sanitizers/issues/321.
4986 int res = REAL(ftime)(tp);
4988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4991 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4994 #endif // SANITIZER_INTERCEPT_FTIME
4996 #if SANITIZER_INTERCEPT_XDR
4997 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4998 unsigned size, int op) {
5000 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5001 // FIXME: under ASan the call below may write to freed memory and corrupt
5002 // its metadata. See
5003 // https://github.com/google/sanitizers/issues/321.
5004 REAL(xdrmem_create)(xdrs, addr, size, op);
5005 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5006 if (op == __sanitizer_XDR_ENCODE) {
5007 // It's not obvious how much data individual xdr_ routines write.
5008 // Simply unpoison the entire target buffer in advance.
5009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5013 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5015 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5016 // FIXME: under ASan the call below may write to freed memory and corrupt
5017 // its metadata. See
5018 // https://github.com/google/sanitizers/issues/321.
5019 REAL(xdrstdio_create)(xdrs, file, op);
5020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5023 // FIXME: under ASan the call below may write to freed memory and corrupt
5024 // its metadata. See
5025 // https://github.com/google/sanitizers/issues/321.
5026 #define XDR_INTERCEPTOR(F, T) \
5027 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5029 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5030 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5031 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5032 int res = REAL(F)(xdrs, p); \
5033 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5038 XDR_INTERCEPTOR(xdr_short, short)
5039 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5040 XDR_INTERCEPTOR(xdr_int, int)
5041 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5042 XDR_INTERCEPTOR(xdr_long, long)
5043 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5044 XDR_INTERCEPTOR(xdr_hyper, long long)
5045 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5046 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5047 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5048 XDR_INTERCEPTOR(xdr_int8_t, u8)
5049 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5050 XDR_INTERCEPTOR(xdr_int16_t, u16)
5051 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5052 XDR_INTERCEPTOR(xdr_int32_t, u32)
5053 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5054 XDR_INTERCEPTOR(xdr_int64_t, u64)
5055 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5056 XDR_INTERCEPTOR(xdr_quad_t, long long)
5057 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5058 XDR_INTERCEPTOR(xdr_bool, bool)
5059 XDR_INTERCEPTOR(xdr_enum, int)
5060 XDR_INTERCEPTOR(xdr_char, char)
5061 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5062 XDR_INTERCEPTOR(xdr_float, float)
5063 XDR_INTERCEPTOR(xdr_double, double)
5065 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5066 // wrapstring, sizeof
5068 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5071 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5072 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5073 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5074 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5075 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5077 // FIXME: under ASan the call below may write to freed memory and corrupt
5078 // its metadata. See
5079 // https://github.com/google/sanitizers/issues/321.
5080 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5081 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5084 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5089 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5092 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5093 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5094 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5095 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5097 // FIXME: under ASan the call below may write to freed memory and corrupt
5098 // its metadata. See
5099 // https://github.com/google/sanitizers/issues/321.
5100 int res = REAL(xdr_string)(xdrs, p, maxsize);
5101 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5110 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5111 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5112 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5113 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5114 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5115 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5116 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5117 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5118 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5119 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5120 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5121 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5122 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5123 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5124 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5125 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5126 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5127 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5128 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5129 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5130 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5131 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5132 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5133 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5134 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5135 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5136 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5137 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5138 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5139 COMMON_INTERCEPT_FUNCTION(xdr_string);
5142 #endif // SANITIZER_INTERCEPT_XDR
5144 #if SANITIZER_INTERCEPT_TSEARCH
5145 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5146 int (*compar)(const void *, const void *)) {
5148 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5149 // FIXME: under ASan the call below may write to freed memory and corrupt
5150 // its metadata. See
5151 // https://github.com/google/sanitizers/issues/321.
5152 void *res = REAL(tsearch)(key, rootp, compar);
5153 if (res && *(void **)res == key)
5154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5157 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5159 #define INIT_TSEARCH
5162 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5163 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5164 void unpoison_file(__sanitizer_FILE *fp) {
5165 #if SANITIZER_HAS_STRUCT_FILE
5166 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5167 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5168 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5169 fp->_IO_read_end - fp->_IO_read_base);
5170 #endif // SANITIZER_HAS_STRUCT_FILE
5174 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5175 // These guys are called when a .c source is built with -O2.
5176 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5178 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5179 int res = REAL(__uflow)(fp);
5183 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5185 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5186 int res = REAL(__underflow)(fp);
5190 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5192 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5193 int res = REAL(__overflow)(fp, ch);
5197 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5199 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5200 int res = REAL(__wuflow)(fp);
5204 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5206 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5207 int res = REAL(__wunderflow)(fp);
5211 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5213 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5214 int res = REAL(__woverflow)(fp, ch);
5218 #define INIT_LIBIO_INTERNALS \
5219 COMMON_INTERCEPT_FUNCTION(__uflow); \
5220 COMMON_INTERCEPT_FUNCTION(__underflow); \
5221 COMMON_INTERCEPT_FUNCTION(__overflow); \
5222 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5223 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5224 COMMON_INTERCEPT_FUNCTION(__woverflow);
5226 #define INIT_LIBIO_INTERNALS
5229 #if SANITIZER_INTERCEPT_FOPEN
5230 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5232 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5233 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5234 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5235 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5236 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5237 if (res) unpoison_file(res);
5240 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5242 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5243 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5244 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5245 if (res) unpoison_file(res);
5248 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5249 __sanitizer_FILE *fp) {
5251 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5252 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5253 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5254 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5255 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5256 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5257 if (res) unpoison_file(res);
5260 #define INIT_FOPEN \
5261 COMMON_INTERCEPT_FUNCTION(fopen); \
5262 COMMON_INTERCEPT_FUNCTION(fdopen); \
5263 COMMON_INTERCEPT_FUNCTION(freopen);
5268 #if SANITIZER_INTERCEPT_FOPEN64
5269 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5271 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5272 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5273 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5274 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5275 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5276 if (res) unpoison_file(res);
5279 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5280 __sanitizer_FILE *fp) {
5282 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5283 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5284 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5285 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5286 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5287 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5288 if (res) unpoison_file(res);
5291 #define INIT_FOPEN64 \
5292 COMMON_INTERCEPT_FUNCTION(fopen64); \
5293 COMMON_INTERCEPT_FUNCTION(freopen64);
5295 #define INIT_FOPEN64
5298 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5299 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5301 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5302 // FIXME: under ASan the call below may write to freed memory and corrupt
5303 // its metadata. See
5304 // https://github.com/google/sanitizers/issues/321.
5305 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5307 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5308 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5310 FileMetadata file = {ptr, sizeloc};
5311 SetInterceptorMetadata(res, file);
5315 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5318 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5319 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5321 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5322 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5324 FileMetadata file = {(char **)ptr, sizeloc};
5325 SetInterceptorMetadata(res, file);
5329 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5332 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5333 // FIXME: under ASan the call below may write to freed memory and corrupt
5334 // its metadata. See
5335 // https://github.com/google/sanitizers/issues/321.
5336 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5337 if (res) unpoison_file(res);
5340 #define INIT_OPEN_MEMSTREAM \
5341 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5342 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5343 COMMON_INTERCEPT_FUNCTION(fmemopen);
5345 #define INIT_OPEN_MEMSTREAM
5348 #if SANITIZER_INTERCEPT_OBSTACK
5349 static void initialize_obstack(__sanitizer_obstack *obstack) {
5350 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5352 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5353 sizeof(*obstack->chunk));
5356 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5357 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5358 void (*free_fn)(uptr arg, void *p)) {
5360 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5362 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5363 if (res) initialize_obstack(obstack);
5366 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5367 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5369 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5371 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5372 if (res) initialize_obstack(obstack);
5375 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5377 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5378 REAL(_obstack_newchunk)(obstack, length);
5380 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5381 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5383 #define INIT_OBSTACK \
5384 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5385 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5386 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5388 #define INIT_OBSTACK
5391 #if SANITIZER_INTERCEPT_FFLUSH
5392 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5394 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5395 int res = REAL(fflush)(fp);
5396 // FIXME: handle fp == NULL
5398 const FileMetadata *m = GetInterceptorMetadata(fp);
5399 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5403 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5408 #if SANITIZER_INTERCEPT_FCLOSE
5409 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5411 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5412 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5413 const FileMetadata *m = GetInterceptorMetadata(fp);
5414 int res = REAL(fclose)(fp);
5416 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5417 DeleteInterceptorMetadata(fp);
5421 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5426 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5427 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5429 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5430 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5431 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5432 void *res = REAL(dlopen)(filename, flag);
5433 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5437 INTERCEPTOR(int, dlclose, void *handle) {
5439 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5440 int res = REAL(dlclose)(handle);
5441 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5444 #define INIT_DLOPEN_DLCLOSE \
5445 COMMON_INTERCEPT_FUNCTION(dlopen); \
5446 COMMON_INTERCEPT_FUNCTION(dlclose);
5448 #define INIT_DLOPEN_DLCLOSE
5451 #if SANITIZER_INTERCEPT_GETPASS
5452 INTERCEPTOR(char *, getpass, const char *prompt) {
5454 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5456 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5457 char *res = REAL(getpass)(prompt);
5458 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5462 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5464 #define INIT_GETPASS
5467 #if SANITIZER_INTERCEPT_TIMERFD
5468 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5471 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5473 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5474 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5475 if (res != -1 && old_value)
5476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5480 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5482 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5483 int res = REAL(timerfd_gettime)(fd, curr_value);
5484 if (res != -1 && curr_value)
5485 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5488 #define INIT_TIMERFD \
5489 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5490 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5492 #define INIT_TIMERFD
5495 #if SANITIZER_INTERCEPT_MLOCKX
5496 // Linux kernel has a bug that leads to kernel deadlock if a process
5497 // maps TBs of memory and then calls mlock().
5498 static void MlockIsUnsupported() {
5499 static atomic_uint8_t printed;
5500 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5502 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5506 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5507 MlockIsUnsupported();
5511 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5512 MlockIsUnsupported();
5516 INTERCEPTOR(int, mlockall, int flags) {
5517 MlockIsUnsupported();
5521 INTERCEPTOR(int, munlockall, void) {
5522 MlockIsUnsupported();
5526 #define INIT_MLOCKX \
5527 COMMON_INTERCEPT_FUNCTION(mlock); \
5528 COMMON_INTERCEPT_FUNCTION(munlock); \
5529 COMMON_INTERCEPT_FUNCTION(mlockall); \
5530 COMMON_INTERCEPT_FUNCTION(munlockall);
5534 #endif // SANITIZER_INTERCEPT_MLOCKX
5536 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5537 struct WrappedCookie {
5539 __sanitizer_cookie_io_functions_t real_io_funcs;
5542 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5543 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5544 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5545 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5546 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5549 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5550 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5551 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5552 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5553 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5556 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5557 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5558 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5559 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5560 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5561 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5565 static int wrapped_close(void *cookie) {
5566 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5567 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5568 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5569 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5570 InternalFree(wrapped_cookie);
5574 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5575 __sanitizer_cookie_io_functions_t io_funcs) {
5577 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5578 WrappedCookie *wrapped_cookie =
5579 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5580 wrapped_cookie->real_cookie = cookie;
5581 wrapped_cookie->real_io_funcs = io_funcs;
5582 __sanitizer_FILE *res =
5583 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5584 wrapped_seek, wrapped_close});
5588 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5590 #define INIT_FOPENCOOKIE
5591 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5593 #if SANITIZER_INTERCEPT_SEM
5594 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5596 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5597 // Workaround a bug in glibc's "old" semaphore implementation by
5598 // zero-initializing the sem_t contents. This has to be done here because
5599 // interceptors bind to the lowest symbols version by default, hitting the
5600 // buggy code path while the non-sanitized build of the same code works fine.
5601 REAL(memset)(s, 0, sizeof(*s));
5602 int res = REAL(sem_init)(s, pshared, value);
5606 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5608 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5609 int res = REAL(sem_destroy)(s);
5613 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5615 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5616 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5618 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5623 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5625 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5626 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5628 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5633 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5635 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5636 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5637 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5639 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5644 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5646 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5647 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5648 int res = REAL(sem_post)(s);
5652 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5654 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5655 int res = REAL(sem_getvalue)(s, sval);
5657 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5658 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5663 COMMON_INTERCEPT_FUNCTION(sem_init); \
5664 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5665 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5666 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5667 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5668 COMMON_INTERCEPT_FUNCTION(sem_post); \
5669 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5672 #endif // SANITIZER_INTERCEPT_SEM
5674 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5675 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5677 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5678 int res = REAL(pthread_setcancelstate)(state, oldstate);
5680 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5684 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5686 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5687 int res = REAL(pthread_setcanceltype)(type, oldtype);
5689 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5692 #define INIT_PTHREAD_SETCANCEL \
5693 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5694 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5696 #define INIT_PTHREAD_SETCANCEL
5699 #if SANITIZER_INTERCEPT_MINCORE
5700 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5702 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5703 int res = REAL(mincore)(addr, length, vec);
5705 uptr page_size = GetPageSizeCached();
5706 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5707 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5711 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5713 #define INIT_MINCORE
5716 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5717 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5718 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5721 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5722 remote_iov, riovcnt, flags);
5723 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5726 write_iovec(ctx, local_iov, liovcnt, res);
5730 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5731 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5734 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5735 remote_iov, riovcnt, flags);
5736 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5739 read_iovec(ctx, local_iov, liovcnt, res);
5742 #define INIT_PROCESS_VM_READV \
5743 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5744 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5746 #define INIT_PROCESS_VM_READV
5749 #if SANITIZER_INTERCEPT_CTERMID
5750 INTERCEPTOR(char *, ctermid, char *s) {
5752 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5753 char *res = REAL(ctermid)(s);
5755 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5759 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5761 #define INIT_CTERMID
5764 #if SANITIZER_INTERCEPT_CTERMID_R
5765 INTERCEPTOR(char *, ctermid_r, char *s) {
5767 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5768 char *res = REAL(ctermid_r)(s);
5770 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5774 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5776 #define INIT_CTERMID_R
5779 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5780 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5782 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5783 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5784 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5788 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5792 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5793 void *srcaddr, int *addrlen) {
5795 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5797 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5799 if (srcaddr) srcaddr_sz = *addrlen;
5800 (void)srcaddr_sz; // prevent "set but not used" warning
5801 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5805 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5806 Min((SIZE_T)*addrlen, srcaddr_sz));
5810 #define INIT_RECV_RECVFROM \
5811 COMMON_INTERCEPT_FUNCTION(recv); \
5812 COMMON_INTERCEPT_FUNCTION(recvfrom);
5814 #define INIT_RECV_RECVFROM
5817 #if SANITIZER_INTERCEPT_SEND_SENDTO
5818 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5820 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5822 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5823 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5825 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5826 if (common_flags()->intercept_send && res > 0)
5827 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5831 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5832 void *dstaddr, int addrlen) {
5834 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5836 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5837 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5839 // Can't check dstaddr as it may have uninitialized padding at the end.
5840 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5841 if (common_flags()->intercept_send && res > 0)
5842 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5845 #define INIT_SEND_SENDTO \
5846 COMMON_INTERCEPT_FUNCTION(send); \
5847 COMMON_INTERCEPT_FUNCTION(sendto);
5849 #define INIT_SEND_SENDTO
5852 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5853 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5855 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5856 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5857 int res = REAL(eventfd_read)(fd, value);
5859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
5860 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5864 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
5866 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
5868 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5869 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5871 int res = REAL(eventfd_write)(fd, value);
5874 #define INIT_EVENTFD_READ_WRITE \
5875 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
5876 COMMON_INTERCEPT_FUNCTION(eventfd_write)
5878 #define INIT_EVENTFD_READ_WRITE
5881 #if SANITIZER_INTERCEPT_STAT
5882 INTERCEPTOR(int, stat, const char *path, void *buf) {
5884 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
5885 if (common_flags()->intercept_stat)
5886 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5887 int res = REAL(stat)(path, buf);
5889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5892 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
5897 #if SANITIZER_INTERCEPT___XSTAT
5898 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
5900 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
5901 if (common_flags()->intercept_stat)
5902 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5903 int res = REAL(__xstat)(version, path, buf);
5905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5908 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
5910 #define INIT___XSTAT
5913 #if SANITIZER_INTERCEPT___XSTAT64
5914 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
5916 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
5917 if (common_flags()->intercept_stat)
5918 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5919 int res = REAL(__xstat64)(version, path, buf);
5921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5924 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
5926 #define INIT___XSTAT64
5929 #if SANITIZER_INTERCEPT___LXSTAT
5930 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
5932 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
5933 if (common_flags()->intercept_stat)
5934 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5935 int res = REAL(__lxstat)(version, path, buf);
5937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5940 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
5942 #define INIT___LXSTAT
5945 #if SANITIZER_INTERCEPT___LXSTAT64
5946 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
5948 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
5949 if (common_flags()->intercept_stat)
5950 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5951 int res = REAL(__lxstat64)(version, path, buf);
5953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5956 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
5958 #define INIT___LXSTAT64
5961 // FIXME: add other *stat interceptor
5963 #if SANITIZER_INTERCEPT_UTMP
5964 INTERCEPTOR(void *, getutent, int dummy) {
5966 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
5967 void *res = REAL(getutent)(dummy);
5969 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
5972 INTERCEPTOR(void *, getutid, void *ut) {
5974 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
5975 void *res = REAL(getutid)(ut);
5977 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
5980 INTERCEPTOR(void *, getutline, void *ut) {
5982 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
5983 void *res = REAL(getutline)(ut);
5985 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
5989 COMMON_INTERCEPT_FUNCTION(getutent); \
5990 COMMON_INTERCEPT_FUNCTION(getutid); \
5991 COMMON_INTERCEPT_FUNCTION(getutline);
5996 #if SANITIZER_INTERCEPT_UTMPX
5997 INTERCEPTOR(void *, getutxent, int dummy) {
5999 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6000 void *res = REAL(getutxent)(dummy);
6002 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6005 INTERCEPTOR(void *, getutxid, void *ut) {
6007 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6008 void *res = REAL(getutxid)(ut);
6010 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6013 INTERCEPTOR(void *, getutxline, void *ut) {
6015 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6016 void *res = REAL(getutxline)(ut);
6018 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6021 #define INIT_UTMPX \
6022 COMMON_INTERCEPT_FUNCTION(getutxent); \
6023 COMMON_INTERCEPT_FUNCTION(getutxid); \
6024 COMMON_INTERCEPT_FUNCTION(getutxline);
6029 static void InitializeCommonInterceptors() {
6030 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
6031 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
6067 INIT_LOCALTIME_AND_FRIENDS;
6076 INIT_GETPWNAM_AND_FRIENDS;
6077 INIT_GETPWNAM_R_AND_FRIENDS;
6089 INIT_PTHREAD_GETSCHEDPARAM;
6094 INIT_GETHOSTBYNAME_R;
6095 INIT_GETHOSTBYNAME2_R;
6096 INIT_GETHOSTBYADDR_R;
6113 INIT_GET_CURRENT_DIR_NAME;
6122 INIT_CANONICALIZE_FILE_NAME;
6124 INIT_SCHED_GETAFFINITY;
6125 INIT_SCHED_GETPARAM;
6128 INIT_XPG_STRERROR_R;
6143 INIT_PTHREAD_MUTEX_LOCK;
6144 INIT_PTHREAD_MUTEX_UNLOCK;
6152 INIT_ETHER_NTOA_ATON;
6157 INIT_PTHREAD_ATTR_GET;
6158 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6159 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6160 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6161 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6162 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6163 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6164 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6165 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6166 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6167 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6168 INIT_PTHREAD_CONDATTR_GETPSHARED;
6169 INIT_PTHREAD_CONDATTR_GETCLOCK;
6170 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6175 INIT_PTHREAD_SETNAME_NP;
6191 INIT_IF_INDEXTONAME;
6198 INIT_LIBIO_INTERNALS;
6201 INIT_OPEN_MEMSTREAM;
6205 INIT_DLOPEN_DLCLOSE;
6211 INIT_PTHREAD_SETCANCEL;
6213 INIT_PROCESS_VM_READV;
6219 INIT_EVENTFD_READ_WRITE;
6224 // FIXME: add other *stat interceptors.