1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common function interceptors for tools like AddressSanitizer,
11 // ThreadSanitizer, MemorySanitizer, etc.
13 // This file should be included into the tool's interceptor file,
14 // which has to define its own macros:
15 // COMMON_INTERCEPTOR_ENTER
16 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
17 // COMMON_INTERCEPTOR_READ_RANGE
18 // COMMON_INTERCEPTOR_WRITE_RANGE
19 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
20 // COMMON_INTERCEPTOR_DIR_ACQUIRE
21 // COMMON_INTERCEPTOR_FD_ACQUIRE
22 // COMMON_INTERCEPTOR_FD_RELEASE
23 // COMMON_INTERCEPTOR_FD_ACCESS
24 // COMMON_INTERCEPTOR_SET_THREAD_NAME
25 // COMMON_INTERCEPTOR_ON_DLOPEN
26 // COMMON_INTERCEPTOR_ON_EXIT
27 // COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
28 // COMMON_INTERCEPTOR_MUTEX_POST_LOCK
29 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
30 // COMMON_INTERCEPTOR_MUTEX_REPAIR
31 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
32 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
33 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
34 // COMMON_INTERCEPTOR_MEMSET_IMPL
35 // COMMON_INTERCEPTOR_MEMMOVE_IMPL
36 // COMMON_INTERCEPTOR_MEMCPY_IMPL
37 // COMMON_INTERCEPTOR_COPY_STRING
38 // COMMON_INTERCEPTOR_STRNDUP_IMPL
39 //===----------------------------------------------------------------------===//
41 #include "interception/interception.h"
42 #include "sanitizer_addrhashmap.h"
43 #include "sanitizer_errno.h"
44 #include "sanitizer_placement_new.h"
45 #include "sanitizer_platform_interceptors.h"
46 #include "sanitizer_tls_get_addr.h"
50 #if SANITIZER_INTERCEPTOR_HOOKS
51 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
53 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
55 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
56 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
58 #endif // SANITIZER_INTERCEPTOR_HOOKS
60 #if SANITIZER_WINDOWS && !defined(va_copy)
61 #define va_copy(dst, src) ((dst) = (src))
65 #define pthread_setname_np pthread_set_name_np
66 #define inet_aton __inet_aton
67 #define inet_pton __inet_pton
68 #define iconv __bsd_iconv
71 // Platform-specific options.
73 namespace __sanitizer {
74 bool PlatformHasDifferentMemcpyAndMemmove();
76 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
77 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
78 #elif SANITIZER_WINDOWS64
79 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
81 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
82 #endif // SANITIZER_MAC
84 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
85 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
88 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
89 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
92 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
93 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
96 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
97 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
100 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
101 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
104 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
105 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
108 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
109 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
112 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
113 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
116 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
117 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
120 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
121 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
124 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
125 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
128 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
129 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
132 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
133 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
136 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
137 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
138 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
141 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
142 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
145 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
146 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
147 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
149 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
150 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
151 CheckNoDeepBind(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 #ifndef COMMON_INTERCEPTOR_COPY_STRING
224 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
227 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
228 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
229 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
230 uptr copy_length = internal_strnlen(s, size); \
231 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
232 if (common_flags()->intercept_strndup) { \
233 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
235 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
236 internal_memcpy(new_mem, s, copy_length); \
237 new_mem[copy_length] = '\0'; \
241 struct FileMetadata {
242 // For open_memstream().
247 struct CommonInterceptorMetadata {
257 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
259 static MetadataHashMap *interceptor_metadata_map;
262 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
263 const FileMetadata &file) {
264 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
266 h->type = CommonInterceptorMetadata::CIMT_FILE;
270 UNUSED static const FileMetadata *GetInterceptorMetadata(
271 __sanitizer_FILE *addr) {
272 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
277 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
284 UNUSED static void DeleteInterceptorMetadata(void *addr) {
285 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
288 #endif // SI_NOT_WINDOWS
290 #if SANITIZER_INTERCEPT_STRLEN
291 INTERCEPTOR(SIZE_T, strlen, const char *s) {
292 // Sometimes strlen is called prior to InitializeCommonInterceptors,
293 // in which case the REAL(strlen) typically used in
294 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
296 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
297 return internal_strlen(s);
299 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
300 SIZE_T result = REAL(strlen)(s);
301 if (common_flags()->intercept_strlen)
302 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
305 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
310 #if SANITIZER_INTERCEPT_STRNLEN
311 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
313 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
314 SIZE_T length = REAL(strnlen)(s, maxlen);
315 if (common_flags()->intercept_strlen)
316 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
319 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
324 #if SANITIZER_INTERCEPT_STRNDUP
325 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
327 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
329 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
332 #endif // SANITIZER_INTERCEPT_STRNDUP
334 #if SANITIZER_INTERCEPT___STRNDUP
335 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
337 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
339 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
341 #define INIT___STRNDUP
342 #endif // SANITIZER_INTERCEPT___STRNDUP
344 #if SANITIZER_INTERCEPT_TEXTDOMAIN
345 INTERCEPTOR(char*, textdomain, const char *domainname) {
347 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
348 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
349 char *domain = REAL(textdomain)(domainname);
351 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
355 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
357 #define INIT_TEXTDOMAIN
360 #if SANITIZER_INTERCEPT_STRCMP
361 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
362 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
365 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
366 const char *s1, const char *s2, int result)
368 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
370 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
371 unsigned char c1, c2;
374 c1 = (unsigned char)s1[i];
375 c2 = (unsigned char)s2[i];
376 if (c1 != c2 || c1 == '\0') break;
378 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
379 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
380 int result = CharCmpX(c1, c2);
381 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
386 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
387 const char *s1, const char *s2, uptr n,
390 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
391 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
392 return internal_strncmp(s1, s2, size);
394 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
395 unsigned char c1 = 0, c2 = 0;
397 for (i = 0; i < size; i++) {
398 c1 = (unsigned char)s1[i];
399 c2 = (unsigned char)s2[i];
400 if (c1 != c2 || c1 == '\0') break;
404 if (common_flags()->strict_string_checks) {
405 for (; i1 < size && s1[i1]; i1++) {}
406 for (; i2 < size && s2[i2]; i2++) {}
408 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
409 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
410 int result = CharCmpX(c1, c2);
411 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
416 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
417 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
423 #if SANITIZER_INTERCEPT_STRCASECMP
424 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
425 int c1_low = ToLower(c1);
426 int c2_low = ToLower(c2);
427 return c1_low - c2_low;
430 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
431 const char *s1, const char *s2, int result)
433 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
435 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
436 unsigned char c1 = 0, c2 = 0;
439 c1 = (unsigned char)s1[i];
440 c2 = (unsigned char)s2[i];
441 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
443 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
444 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
445 int result = CharCaseCmp(c1, c2);
446 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
451 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
452 const char *s1, const char *s2, uptr size,
455 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
457 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
458 unsigned char c1 = 0, c2 = 0;
460 for (i = 0; i < size; i++) {
461 c1 = (unsigned char)s1[i];
462 c2 = (unsigned char)s2[i];
463 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
467 if (common_flags()->strict_string_checks) {
468 for (; i1 < size && s1[i1]; i1++) {}
469 for (; i2 < size && s2[i2]; i2++) {}
471 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
472 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
473 int result = CharCaseCmp(c1, c2);
474 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
475 s1, s2, size, result);
479 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
480 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
482 #define INIT_STRCASECMP
483 #define INIT_STRNCASECMP
486 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
487 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
489 uptr len1 = REAL(strlen)(s1);
490 uptr len2 = REAL(strlen)(s2);
491 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
492 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
496 #if SANITIZER_INTERCEPT_STRSTR
498 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
499 const char *s1, const char *s2, char *result)
501 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
502 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
503 return internal_strstr(s1, s2);
505 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
506 char *r = REAL(strstr)(s1, s2);
507 if (common_flags()->intercept_strstr)
508 StrstrCheck(ctx, r, s1, s2);
509 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
514 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
519 #if SANITIZER_INTERCEPT_STRCASESTR
521 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
522 const char *s1, const char *s2, char *result)
524 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
526 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
527 char *r = REAL(strcasestr)(s1, s2);
528 if (common_flags()->intercept_strstr)
529 StrstrCheck(ctx, r, s1, s2);
530 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
535 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
537 #define INIT_STRCASESTR
540 #if SANITIZER_INTERCEPT_STRTOK
542 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
544 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
545 if (!common_flags()->intercept_strtok) {
546 return REAL(strtok)(str, delimiters);
548 if (common_flags()->strict_string_checks) {
549 // If strict_string_checks is enabled, we check the whole first argument
550 // string on the first call (strtok saves this string in a static buffer
551 // for subsequent calls). We do not need to check strtok's result.
552 // As the delimiters can change, we check them every call.
553 if (str != nullptr) {
554 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
556 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
557 REAL(strlen)(delimiters) + 1);
558 return REAL(strtok)(str, delimiters);
560 // However, when strict_string_checks is disabled we cannot check the
561 // whole string on the first call. Instead, we check the result string
562 // which is guaranteed to be a NULL-terminated substring of the first
563 // argument. We also conservatively check one character of str and the
565 if (str != nullptr) {
566 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
568 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
569 char *result = REAL(strtok)(str, delimiters);
570 if (result != nullptr) {
571 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
572 } else if (str != nullptr) {
573 // No delimiter were found, it's safe to assume that the entire str was
575 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
581 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
586 #if SANITIZER_INTERCEPT_MEMMEM
587 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
588 const void *s1, SIZE_T len1, const void *s2,
589 SIZE_T len2, void *result)
591 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
594 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
595 void *r = REAL(memmem)(s1, len1, s2, len2);
596 if (common_flags()->intercept_memmem) {
597 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
598 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
600 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
601 s1, len1, s2, len2, r);
605 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
608 #endif // SANITIZER_INTERCEPT_MEMMEM
610 #if SANITIZER_INTERCEPT_STRCHR
611 INTERCEPTOR(char*, strchr, const char *s, int c) {
613 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
614 return internal_strchr(s, c);
615 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
616 char *result = REAL(strchr)(s, c);
617 if (common_flags()->intercept_strchr) {
618 // Keep strlen as macro argument, as macro may ignore it.
619 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
620 (result ? result - s : REAL(strlen)(s)) + 1);
624 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
629 #if SANITIZER_INTERCEPT_STRCHRNUL
630 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
632 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
633 char *result = REAL(strchrnul)(s, c);
634 uptr len = result - s + 1;
635 if (common_flags()->intercept_strchr)
636 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
639 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
641 #define INIT_STRCHRNUL
644 #if SANITIZER_INTERCEPT_STRRCHR
645 INTERCEPTOR(char*, strrchr, const char *s, int c) {
647 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
648 return internal_strrchr(s, c);
649 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
650 if (common_flags()->intercept_strchr)
651 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
652 return REAL(strrchr)(s, c);
654 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
659 #if SANITIZER_INTERCEPT_STRSPN
660 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
662 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
663 SIZE_T r = REAL(strspn)(s1, s2);
664 if (common_flags()->intercept_strspn) {
665 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
666 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
671 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
673 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
674 SIZE_T r = REAL(strcspn)(s1, s2);
675 if (common_flags()->intercept_strspn) {
676 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
677 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
682 #define INIT_STRSPN \
683 COMMON_INTERCEPT_FUNCTION(strspn); \
684 COMMON_INTERCEPT_FUNCTION(strcspn);
689 #if SANITIZER_INTERCEPT_STRPBRK
690 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
692 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
693 char *r = REAL(strpbrk)(s1, s2);
694 if (common_flags()->intercept_strpbrk) {
695 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
696 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
697 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
702 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
707 #if SANITIZER_INTERCEPT_MEMSET
708 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
710 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
713 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
718 #if SANITIZER_INTERCEPT_MEMMOVE
719 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
721 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
724 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
729 #if SANITIZER_INTERCEPT_MEMCPY
730 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
731 // On OS X, calling internal_memcpy here will cause memory corruptions,
732 // because memcpy and memmove are actually aliases of the same
733 // implementation. We need to use internal_memmove here.
734 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
735 // due to memcpy being an alias of memmove on OS X.
737 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
738 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
740 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
744 #define INIT_MEMCPY \
746 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
747 COMMON_INTERCEPT_FUNCTION(memcpy); \
749 ASSIGN_REAL(memcpy, memmove); \
751 CHECK(REAL(memcpy)); \
758 #if SANITIZER_INTERCEPT_MEMCMP
760 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
761 const void *s1, const void *s2, uptr n,
764 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
765 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
766 return internal_memcmp(a1, a2, size);
768 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
769 if (common_flags()->intercept_memcmp) {
770 if (common_flags()->strict_memcmp) {
771 // Check the entire regions even if the first bytes of the buffers are
773 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
774 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
775 // Fallthrough to REAL(memcmp) below.
777 unsigned char c1 = 0, c2 = 0;
778 const unsigned char *s1 = (const unsigned char*)a1;
779 const unsigned char *s2 = (const unsigned char*)a2;
781 for (i = 0; i < size; i++) {
786 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
787 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
788 int r = CharCmpX(c1, c2);
789 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
794 int result = REAL(memcmp(a1, a2, size));
795 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
800 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
805 #if SANITIZER_INTERCEPT_MEMCHR
806 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
807 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
808 return internal_memchr(s, c, n);
810 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
811 #if SANITIZER_WINDOWS
814 res = REAL(memchr)(s, c, n);
816 res = internal_memchr(s, c, n);
819 void *res = REAL(memchr)(s, c, n);
821 uptr len = res ? (char *)res - (const char *)s + 1 : n;
822 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
826 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
831 #if SANITIZER_INTERCEPT_MEMRCHR
832 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
834 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
835 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
836 return REAL(memrchr)(s, c, n);
839 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
844 #if SANITIZER_INTERCEPT_FREXP
845 INTERCEPTOR(double, frexp, double x, int *exp) {
847 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
848 // Assuming frexp() always writes to |exp|.
849 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
850 double res = REAL(frexp)(x, exp);
854 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
857 #endif // SANITIZER_INTERCEPT_FREXP
859 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
860 INTERCEPTOR(float, frexpf, float x, int *exp) {
862 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
863 // FIXME: under ASan the call below may write to freed memory and corrupt
865 // https://github.com/google/sanitizers/issues/321.
866 float res = REAL(frexpf)(x, exp);
867 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
871 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
873 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
874 // FIXME: under ASan the call below may write to freed memory and corrupt
876 // https://github.com/google/sanitizers/issues/321.
877 long double res = REAL(frexpl)(x, exp);
878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
882 #define INIT_FREXPF_FREXPL \
883 COMMON_INTERCEPT_FUNCTION(frexpf); \
884 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
886 #define INIT_FREXPF_FREXPL
887 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
890 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
891 SIZE_T iovlen, SIZE_T maxlen) {
892 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
893 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
894 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
899 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
900 SIZE_T iovlen, SIZE_T maxlen) {
901 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
902 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
903 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
904 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
910 #if SANITIZER_INTERCEPT_READ
911 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
913 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
914 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
915 // FIXME: under ASan the call below may write to freed memory and corrupt
917 // https://github.com/google/sanitizers/issues/321.
918 SSIZE_T res = REAL(read)(fd, ptr, count);
919 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
920 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
923 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
928 #if SANITIZER_INTERCEPT_FREAD
929 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
930 // libc file streams can call user-supplied functions, see fopencookie.
932 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
933 // FIXME: under ASan the call below may write to freed memory and corrupt
935 // https://github.com/google/sanitizers/issues/321.
936 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
937 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
940 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
945 #if SANITIZER_INTERCEPT_PREAD
946 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
948 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
949 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
950 // FIXME: under ASan the call below may write to freed memory and corrupt
952 // https://github.com/google/sanitizers/issues/321.
953 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
954 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
955 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
958 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
963 #if SANITIZER_INTERCEPT_PREAD64
964 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
966 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
967 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
968 // FIXME: under ASan the call below may write to freed memory and corrupt
970 // https://github.com/google/sanitizers/issues/321.
971 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
972 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
973 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
976 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
981 #if SANITIZER_INTERCEPT_READV
982 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
985 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
986 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
987 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
988 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
989 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
992 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
997 #if SANITIZER_INTERCEPT_PREADV
998 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1001 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1002 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1003 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1004 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1005 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1008 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1013 #if SANITIZER_INTERCEPT_PREADV64
1014 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1017 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1018 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1019 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1020 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1021 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1024 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1026 #define INIT_PREADV64
1029 #if SANITIZER_INTERCEPT_WRITE
1030 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1032 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1033 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1034 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1035 SSIZE_T res = REAL(write)(fd, ptr, count);
1036 // FIXME: this check should be _before_ the call to REAL(write), not after
1037 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1040 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1045 #if SANITIZER_INTERCEPT_FWRITE
1046 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1047 // libc file streams can call user-supplied functions, see fopencookie.
1049 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1050 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1051 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1054 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1059 #if SANITIZER_INTERCEPT_PWRITE
1060 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1062 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1063 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1064 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1065 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1066 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1069 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1074 #if SANITIZER_INTERCEPT_PWRITE64
1075 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1078 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1079 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1080 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1081 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1082 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1085 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1087 #define INIT_PWRITE64
1090 #if SANITIZER_INTERCEPT_WRITEV
1091 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1094 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1095 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1096 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1097 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1098 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1101 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1106 #if SANITIZER_INTERCEPT_PWRITEV
1107 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1110 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1111 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1112 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1113 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1114 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1117 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1119 #define INIT_PWRITEV
1122 #if SANITIZER_INTERCEPT_PWRITEV64
1123 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1126 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1127 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1128 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1129 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1130 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1133 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1135 #define INIT_PWRITEV64
1138 #if SANITIZER_INTERCEPT_PRCTL
1139 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1140 unsigned long arg3, // NOLINT
1141 unsigned long arg4, unsigned long arg5) { // NOLINT
1143 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1144 static const int PR_SET_NAME = 15;
1145 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1146 if (option == PR_SET_NAME) {
1148 internal_strncpy(buff, (char *)arg2, 15);
1150 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1154 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1157 #endif // SANITIZER_INTERCEPT_PRCTL
1159 #if SANITIZER_INTERCEPT_TIME
1160 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1162 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1163 unsigned long local_t;
1164 unsigned long res = REAL(time)(&local_t);
1165 if (t && res != (unsigned long)-1) {
1166 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1171 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1174 #endif // SANITIZER_INTERCEPT_TIME
1176 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1177 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1180 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1181 // can point to shared memory and tsan would report a data race.
1182 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1183 REAL(strlen(tm->tm_zone)) + 1);
1186 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1188 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1189 __sanitizer_tm *res = REAL(localtime)(timep);
1191 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1192 unpoison_tm(ctx, res);
1196 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1198 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1199 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1201 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1202 unpoison_tm(ctx, res);
1206 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1208 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1209 __sanitizer_tm *res = REAL(gmtime)(timep);
1211 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1212 unpoison_tm(ctx, res);
1216 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1218 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1219 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1221 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1222 unpoison_tm(ctx, res);
1226 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1228 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1229 // FIXME: under ASan the call below may write to freed memory and corrupt
1230 // its metadata. See
1231 // https://github.com/google/sanitizers/issues/321.
1232 char *res = REAL(ctime)(timep);
1234 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1235 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1239 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1241 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1242 // FIXME: under ASan the call below may write to freed memory and corrupt
1243 // its metadata. See
1244 // https://github.com/google/sanitizers/issues/321.
1245 char *res = REAL(ctime_r)(timep, result);
1247 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1252 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1254 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1255 // FIXME: under ASan the call below may write to freed memory and corrupt
1256 // its metadata. See
1257 // https://github.com/google/sanitizers/issues/321.
1258 char *res = REAL(asctime)(tm);
1260 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1261 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1265 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1267 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1268 // FIXME: under ASan the call below may write to freed memory and corrupt
1269 // its metadata. See
1270 // https://github.com/google/sanitizers/issues/321.
1271 char *res = REAL(asctime_r)(tm, result);
1273 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1278 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1280 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1281 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1282 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1283 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1284 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1285 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1286 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1287 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1288 long res = REAL(mktime)(tm);
1289 if (res != -1) unpoison_tm(ctx, tm);
1292 #define INIT_LOCALTIME_AND_FRIENDS \
1293 COMMON_INTERCEPT_FUNCTION(localtime); \
1294 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1295 COMMON_INTERCEPT_FUNCTION(gmtime); \
1296 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1297 COMMON_INTERCEPT_FUNCTION(ctime); \
1298 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1299 COMMON_INTERCEPT_FUNCTION(asctime); \
1300 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1301 COMMON_INTERCEPT_FUNCTION(mktime);
1303 #define INIT_LOCALTIME_AND_FRIENDS
1304 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1306 #if SANITIZER_INTERCEPT_STRPTIME
1307 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1309 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1311 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1312 // FIXME: under ASan the call below may write to freed memory and corrupt
1313 // its metadata. See
1314 // https://github.com/google/sanitizers/issues/321.
1315 char *res = REAL(strptime)(s, format, tm);
1316 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1318 // Do not call unpoison_tm here, because strptime does not, in fact,
1319 // initialize the entire struct tm. For example, tm_zone pointer is left
1321 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1325 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1327 #define INIT_STRPTIME
1330 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1331 #include "sanitizer_common_interceptors_format.inc"
1333 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1337 va_start(ap, format); \
1338 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1339 int res = WRAP(vname)(__VA_ARGS__, ap); \
1346 #if SANITIZER_INTERCEPT_SCANF
1348 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1351 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1354 int res = REAL(vname)(__VA_ARGS__); \
1356 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1361 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1362 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1364 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1365 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1367 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1368 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1370 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1371 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1372 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1374 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1376 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1378 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1379 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1380 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1382 INTERCEPTOR(int, scanf, const char *format, ...)
1383 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1385 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1386 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1388 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1389 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1391 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1392 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1393 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1395 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1396 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1398 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1399 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1404 #if SANITIZER_INTERCEPT_SCANF
1405 #define INIT_SCANF \
1406 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1407 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1408 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1409 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1410 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1411 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1416 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1417 #define INIT_ISOC99_SCANF \
1418 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1419 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1420 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1421 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1422 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1423 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1425 #define INIT_ISOC99_SCANF
1428 #if SANITIZER_INTERCEPT_PRINTF
1430 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1432 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1436 #define VPRINTF_INTERCEPTOR_RETURN() \
1439 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1441 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1442 if (common_flags()->check_printf) \
1443 printf_common(ctx, format, aq); \
1444 int res = REAL(vname)(__VA_ARGS__); \
1445 VPRINTF_INTERCEPTOR_RETURN(); \
1449 // FIXME: under ASan the REAL() call below may write to freed memory and
1450 // corrupt its metadata. See
1451 // https://github.com/google/sanitizers/issues/321.
1452 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1454 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1455 if (common_flags()->check_printf) { \
1456 printf_common(ctx, format, aq); \
1458 int res = REAL(vname)(str, __VA_ARGS__); \
1460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1462 VPRINTF_INTERCEPTOR_RETURN(); \
1466 // FIXME: under ASan the REAL() call below may write to freed memory and
1467 // corrupt its metadata. See
1468 // https://github.com/google/sanitizers/issues/321.
1469 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1471 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1472 if (common_flags()->check_printf) { \
1473 printf_common(ctx, format, aq); \
1475 int res = REAL(vname)(str, size, __VA_ARGS__); \
1477 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1479 VPRINTF_INTERCEPTOR_RETURN(); \
1483 // FIXME: under ASan the REAL() call below may write to freed memory and
1484 // corrupt its metadata. See
1485 // https://github.com/google/sanitizers/issues/321.
1486 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1488 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1490 if (common_flags()->check_printf) { \
1491 printf_common(ctx, format, aq); \
1493 int res = REAL(vname)(strp, __VA_ARGS__); \
1495 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1497 VPRINTF_INTERCEPTOR_RETURN(); \
1501 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1502 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1504 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1506 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1508 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1510 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1512 #if SANITIZER_INTERCEPT_PRINTF_L
1513 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1514 const char *format, va_list ap)
1515 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1517 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1518 const char *format, ...)
1519 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1520 #endif // SANITIZER_INTERCEPT_PRINTF_L
1522 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1523 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1525 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1526 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1528 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1529 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1530 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1532 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1533 const char *format, va_list ap)
1534 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1536 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1538 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1540 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1542 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1545 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1547 INTERCEPTOR(int, printf, const char *format, ...)
1548 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1550 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1551 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1553 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1554 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1556 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1557 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1559 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1560 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1562 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1563 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1564 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1566 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1568 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1570 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1571 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1573 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1574 const char *format, ...)
1575 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1578 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1580 #endif // SANITIZER_INTERCEPT_PRINTF
1582 #if SANITIZER_INTERCEPT_PRINTF
1583 #define INIT_PRINTF \
1584 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1585 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1586 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1587 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1588 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1589 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1590 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1591 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1592 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1593 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1598 #if SANITIZER_INTERCEPT_PRINTF_L
1599 #define INIT_PRINTF_L \
1600 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1601 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1603 #define INIT_PRINTF_L
1606 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1607 #define INIT_ISOC99_PRINTF \
1608 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1609 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1610 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1611 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1612 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1613 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1614 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1615 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1617 #define INIT_ISOC99_PRINTF
1620 #if SANITIZER_INTERCEPT_IOCTL
1621 #include "sanitizer_common_interceptors_ioctl.inc"
1622 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1623 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1624 // can trigger a report and we need to be able to unwind through this
1625 // function. On Mac in debug mode we might not have a frame pointer, because
1626 // ioctl_common_[pre|post] doesn't get inlined here.
1627 ENABLE_FRAME_POINTER;
1631 va_start(ap, request);
1632 void *arg = va_arg(ap, void *);
1634 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1636 CHECK(ioctl_initialized);
1638 // Note: TSan does not use common flags, and they are zero-initialized.
1639 // This effectively disables ioctl handling in TSan.
1640 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1642 // Although request is unsigned long, the rest of the interceptor uses it
1643 // as just "unsigned" to save space, because we know that all values fit in
1644 // "unsigned" - they are compile-time constants.
1646 const ioctl_desc *desc = ioctl_lookup(request);
1647 ioctl_desc decoded_desc;
1649 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1650 if (!ioctl_decode(request, &decoded_desc))
1651 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1653 desc = &decoded_desc;
1656 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1657 int res = REAL(ioctl)(d, request, arg);
1658 // FIXME: some ioctls have different return values for success and failure.
1659 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1662 #define INIT_IOCTL \
1664 COMMON_INTERCEPT_FUNCTION(ioctl);
1669 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1670 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1671 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1672 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1676 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1677 REAL(strlen)(pwd->pw_name) + 1);
1679 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1680 REAL(strlen)(pwd->pw_passwd) + 1);
1681 #if !SANITIZER_ANDROID
1683 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1684 REAL(strlen)(pwd->pw_gecos) + 1);
1688 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1689 REAL(strlen)(pwd->pw_class) + 1);
1692 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1693 REAL(strlen)(pwd->pw_dir) + 1);
1695 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1696 REAL(strlen)(pwd->pw_shell) + 1);
1700 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1704 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1705 REAL(strlen)(grp->gr_name) + 1);
1707 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1708 REAL(strlen)(grp->gr_passwd) + 1);
1709 char **p = grp->gr_mem;
1711 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1713 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1714 (p - grp->gr_mem + 1) * sizeof(*p));
1717 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1718 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1719 // SANITIZER_INTERCEPT_GETPWENT_R ||
1720 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1722 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1723 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1725 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1726 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1727 __sanitizer_passwd *res = REAL(getpwnam)(name);
1728 if (res) unpoison_passwd(ctx, res);
1731 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1733 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1734 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1735 if (res) unpoison_passwd(ctx, res);
1738 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1740 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1741 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1742 __sanitizer_group *res = REAL(getgrnam)(name);
1743 if (res) unpoison_group(ctx, res);
1746 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1748 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1749 __sanitizer_group *res = REAL(getgrgid)(gid);
1750 if (res) unpoison_group(ctx, res);
1753 #define INIT_GETPWNAM_AND_FRIENDS \
1754 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1755 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1756 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1757 COMMON_INTERCEPT_FUNCTION(getgrgid);
1759 #define INIT_GETPWNAM_AND_FRIENDS
1762 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1763 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1764 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1766 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1767 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1768 // FIXME: under ASan the call below may write to freed memory and corrupt
1769 // its metadata. See
1770 // https://github.com/google/sanitizers/issues/321.
1771 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1773 if (result && *result) unpoison_passwd(ctx, *result);
1774 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1776 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1779 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1780 SIZE_T buflen, __sanitizer_passwd **result) {
1782 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1783 // FIXME: under ASan the call below may write to freed memory and corrupt
1784 // its metadata. See
1785 // https://github.com/google/sanitizers/issues/321.
1786 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1788 if (result && *result) unpoison_passwd(ctx, *result);
1789 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1791 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1794 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1795 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1797 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1798 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
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(getgrnam_r)(name, grp, buf, buflen, result);
1804 if (result && *result) unpoison_group(ctx, *result);
1805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1807 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1810 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1811 SIZE_T buflen, __sanitizer_group **result) {
1813 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
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(getgrgid_r)(gid, grp, buf, buflen, result);
1819 if (result && *result) unpoison_group(ctx, *result);
1820 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1822 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1825 #define INIT_GETPWNAM_R_AND_FRIENDS \
1826 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1827 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1828 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1829 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1831 #define INIT_GETPWNAM_R_AND_FRIENDS
1834 #if SANITIZER_INTERCEPT_GETPWENT
1835 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1837 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1838 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1839 if (res) unpoison_passwd(ctx, res);
1842 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1844 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1845 __sanitizer_group *res = REAL(getgrent)(dummy);
1846 if (res) unpoison_group(ctx, res);;
1849 #define INIT_GETPWENT \
1850 COMMON_INTERCEPT_FUNCTION(getpwent); \
1851 COMMON_INTERCEPT_FUNCTION(getgrent);
1853 #define INIT_GETPWENT
1856 #if SANITIZER_INTERCEPT_FGETPWENT
1857 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1859 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1860 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1861 if (res) unpoison_passwd(ctx, res);
1864 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1866 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1867 __sanitizer_group *res = REAL(fgetgrent)(fp);
1868 if (res) unpoison_group(ctx, res);
1871 #define INIT_FGETPWENT \
1872 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1873 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1875 #define INIT_FGETPWENT
1878 #if SANITIZER_INTERCEPT_GETPWENT_R
1879 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1880 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1882 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
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(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1888 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1891 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1894 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1895 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1897 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1898 // FIXME: under ASan the call below may write to freed memory and corrupt
1899 // its metadata. See
1900 // https://github.com/google/sanitizers/issues/321.
1901 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1903 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1906 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1909 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1910 __sanitizer_group **pwbufp) {
1912 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1913 // FIXME: under ASan the call below may write to freed memory and corrupt
1914 // its metadata. See
1915 // https://github.com/google/sanitizers/issues/321.
1916 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1918 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1919 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1921 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1924 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1925 SIZE_T buflen, __sanitizer_group **pwbufp) {
1927 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1928 // FIXME: under ASan the call below may write to freed memory and corrupt
1929 // its metadata. See
1930 // https://github.com/google/sanitizers/issues/321.
1931 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1933 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1934 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1936 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1939 #define INIT_GETPWENT_R \
1940 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
1941 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1942 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
1943 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1945 #define INIT_GETPWENT_R
1948 #if SANITIZER_INTERCEPT_SETPWENT
1949 // The only thing these interceptors do is disable any nested interceptors.
1950 // These functions may open nss modules and call uninstrumented functions from
1951 // them, and we don't want things like strlen() to trigger.
1952 INTERCEPTOR(void, setpwent, int dummy) {
1954 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1955 REAL(setpwent)(dummy);
1957 INTERCEPTOR(void, endpwent, int dummy) {
1959 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1960 REAL(endpwent)(dummy);
1962 INTERCEPTOR(void, setgrent, int dummy) {
1964 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1965 REAL(setgrent)(dummy);
1967 INTERCEPTOR(void, endgrent, int dummy) {
1969 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1970 REAL(endgrent)(dummy);
1972 #define INIT_SETPWENT \
1973 COMMON_INTERCEPT_FUNCTION(setpwent); \
1974 COMMON_INTERCEPT_FUNCTION(endpwent); \
1975 COMMON_INTERCEPT_FUNCTION(setgrent); \
1976 COMMON_INTERCEPT_FUNCTION(endgrent);
1978 #define INIT_SETPWENT
1981 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1982 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1984 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1985 // FIXME: under ASan the call below may write to freed memory and corrupt
1986 // its metadata. See
1987 // https://github.com/google/sanitizers/issues/321.
1988 int res = REAL(clock_getres)(clk_id, tp);
1990 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1994 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1996 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1997 // FIXME: under ASan the call below may write to freed memory and corrupt
1998 // its metadata. See
1999 // https://github.com/google/sanitizers/issues/321.
2000 int res = REAL(clock_gettime)(clk_id, tp);
2002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2006 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2008 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2009 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2010 return REAL(clock_settime)(clk_id, tp);
2012 #define INIT_CLOCK_GETTIME \
2013 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2014 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2015 COMMON_INTERCEPT_FUNCTION(clock_settime);
2017 #define INIT_CLOCK_GETTIME
2020 #if SANITIZER_INTERCEPT_GETITIMER
2021 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2023 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2024 // FIXME: under ASan the call below may write to freed memory and corrupt
2025 // its metadata. See
2026 // https://github.com/google/sanitizers/issues/321.
2027 int res = REAL(getitimer)(which, curr_value);
2028 if (!res && curr_value) {
2029 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2033 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2035 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2037 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
2038 // FIXME: under ASan the call below may write to freed memory and corrupt
2039 // its metadata. See
2040 // https://github.com/google/sanitizers/issues/321.
2041 int res = REAL(setitimer)(which, new_value, old_value);
2042 if (!res && old_value) {
2043 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2047 #define INIT_GETITIMER \
2048 COMMON_INTERCEPT_FUNCTION(getitimer); \
2049 COMMON_INTERCEPT_FUNCTION(setitimer);
2051 #define INIT_GETITIMER
2054 #if SANITIZER_INTERCEPT_GLOB
2055 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2056 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2057 // +1 for NULL pointer at the end.
2058 if (pglob->gl_pathv)
2059 COMMON_INTERCEPTOR_WRITE_RANGE(
2060 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2061 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2062 char *p = pglob->gl_pathv[i];
2063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2067 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2069 static void wrapped_gl_closedir(void *dir) {
2070 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2071 pglob_copy->gl_closedir(dir);
2074 static void *wrapped_gl_readdir(void *dir) {
2075 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2076 return pglob_copy->gl_readdir(dir);
2079 static void *wrapped_gl_opendir(const char *s) {
2080 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2081 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2082 return pglob_copy->gl_opendir(s);
2085 static int wrapped_gl_lstat(const char *s, void *st) {
2086 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2087 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2088 return pglob_copy->gl_lstat(s, st);
2091 static int wrapped_gl_stat(const char *s, void *st) {
2092 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2093 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2094 return pglob_copy->gl_stat(s, st);
2097 static const __sanitizer_glob_t kGlobCopy = {
2099 0, wrapped_gl_closedir, wrapped_gl_readdir,
2100 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2102 INTERCEPTOR(int, glob, const char *pattern, int flags,
2103 int (*errfunc)(const char *epath, int eerrno),
2104 __sanitizer_glob_t *pglob) {
2106 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2107 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2108 __sanitizer_glob_t glob_copy;
2109 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2110 if (flags & glob_altdirfunc) {
2111 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2112 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2113 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2114 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2115 Swap(pglob->gl_stat, glob_copy.gl_stat);
2116 pglob_copy = &glob_copy;
2118 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2119 if (flags & glob_altdirfunc) {
2120 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2121 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2122 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2123 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2124 Swap(pglob->gl_stat, glob_copy.gl_stat);
2127 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2131 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2132 int (*errfunc)(const char *epath, int eerrno),
2133 __sanitizer_glob_t *pglob) {
2135 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2136 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2137 __sanitizer_glob_t glob_copy;
2138 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2139 if (flags & glob_altdirfunc) {
2140 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2141 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2142 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2143 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2144 Swap(pglob->gl_stat, glob_copy.gl_stat);
2145 pglob_copy = &glob_copy;
2147 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2148 if (flags & glob_altdirfunc) {
2149 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2150 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2151 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2152 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2153 Swap(pglob->gl_stat, glob_copy.gl_stat);
2156 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2160 COMMON_INTERCEPT_FUNCTION(glob); \
2161 COMMON_INTERCEPT_FUNCTION(glob64);
2162 #else // SANITIZER_INTERCEPT_GLOB
2164 #endif // SANITIZER_INTERCEPT_GLOB
2166 #if SANITIZER_INTERCEPT_WAIT
2167 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2168 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2170 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2172 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2173 // FIXME: under ASan the call below may write to freed memory and corrupt
2174 // its metadata. See
2175 // https://github.com/google/sanitizers/issues/321.
2176 int res = REAL(wait)(status);
2177 if (res != -1 && status)
2178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2181 // On FreeBSD id_t is always 64-bit wide.
2182 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2183 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2186 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2190 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
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(waitid)(idtype, id, infop, options);
2195 if (res != -1 && infop)
2196 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2199 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2201 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2202 // FIXME: under ASan the call below may write to freed memory and corrupt
2203 // its metadata. See
2204 // https://github.com/google/sanitizers/issues/321.
2205 int res = REAL(waitpid)(pid, status, options);
2206 if (res != -1 && status)
2207 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2210 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2212 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2213 // FIXME: under ASan the call below may write to freed memory and corrupt
2214 // its metadata. See
2215 // https://github.com/google/sanitizers/issues/321.
2216 int res = REAL(wait3)(status, options, rusage);
2218 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2219 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2223 #if SANITIZER_ANDROID
2224 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2226 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2227 // FIXME: under ASan the call below may write to freed memory and corrupt
2228 // its metadata. See
2229 // https://github.com/google/sanitizers/issues/321.
2230 int res = REAL(__wait4)(pid, status, options, rusage);
2232 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2233 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2237 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2239 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2241 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2242 // FIXME: under ASan the call below may write to freed memory and corrupt
2243 // its metadata. See
2244 // https://github.com/google/sanitizers/issues/321.
2245 int res = REAL(wait4)(pid, status, options, rusage);
2247 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2248 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2252 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2253 #endif // SANITIZER_ANDROID
2255 COMMON_INTERCEPT_FUNCTION(wait); \
2256 COMMON_INTERCEPT_FUNCTION(waitid); \
2257 COMMON_INTERCEPT_FUNCTION(waitpid); \
2258 COMMON_INTERCEPT_FUNCTION(wait3);
2264 #if SANITIZER_INTERCEPT_INET
2265 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2267 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2268 uptr sz = __sanitizer_in_addr_sz(af);
2269 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2270 // FIXME: figure out read size based on the address family.
2271 // FIXME: under ASan the call below may write to freed memory and corrupt
2272 // its metadata. See
2273 // https://github.com/google/sanitizers/issues/321.
2274 char *res = REAL(inet_ntop)(af, src, dst, size);
2275 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2278 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2280 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2281 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2282 // FIXME: figure out read size based on the address family.
2283 // FIXME: under ASan the call below may write to freed memory and corrupt
2284 // its metadata. See
2285 // https://github.com/google/sanitizers/issues/321.
2286 int res = REAL(inet_pton)(af, src, dst);
2288 uptr sz = __sanitizer_in_addr_sz(af);
2289 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2294 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2295 COMMON_INTERCEPT_FUNCTION(inet_pton);
2300 #if SANITIZER_INTERCEPT_INET
2301 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2303 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2304 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2305 // FIXME: under ASan the call below may write to freed memory and corrupt
2306 // its metadata. See
2307 // https://github.com/google/sanitizers/issues/321.
2308 int res = REAL(inet_aton)(cp, dst);
2310 uptr sz = __sanitizer_in_addr_sz(af_inet);
2311 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2315 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2317 #define INIT_INET_ATON
2320 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2321 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2323 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2324 // FIXME: under ASan the call below may write to freed memory and corrupt
2325 // its metadata. See
2326 // https://github.com/google/sanitizers/issues/321.
2327 int res = REAL(pthread_getschedparam)(thread, policy, param);
2329 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2330 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2334 #define INIT_PTHREAD_GETSCHEDPARAM \
2335 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2337 #define INIT_PTHREAD_GETSCHEDPARAM
2340 #if SANITIZER_INTERCEPT_GETADDRINFO
2341 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2342 struct __sanitizer_addrinfo *hints,
2343 struct __sanitizer_addrinfo **out) {
2345 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2346 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2348 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2350 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2351 // FIXME: under ASan the call below may write to freed memory and corrupt
2352 // its metadata. See
2353 // https://github.com/google/sanitizers/issues/321.
2354 int res = REAL(getaddrinfo)(node, service, hints, out);
2355 if (res == 0 && out) {
2356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2357 struct __sanitizer_addrinfo *p = *out;
2359 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2361 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2362 if (p->ai_canonname)
2363 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2364 REAL(strlen)(p->ai_canonname) + 1);
2370 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2372 #define INIT_GETADDRINFO
2375 #if SANITIZER_INTERCEPT_GETNAMEINFO
2376 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2377 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2379 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2380 serv, servlen, flags);
2381 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2382 // There is padding in in_addr that may make this too noisy
2383 // FIXME: under ASan the call below may write to freed memory and corrupt
2384 // its metadata. See
2385 // https://github.com/google/sanitizers/issues/321.
2387 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2389 if (host && hostlen)
2390 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2391 if (serv && servlen)
2392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2396 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2398 #define INIT_GETNAMEINFO
2401 #if SANITIZER_INTERCEPT_GETSOCKNAME
2402 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2404 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2405 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2406 int addrlen_in = *addrlen;
2407 // FIXME: under ASan the call below may write to freed memory and corrupt
2408 // its metadata. See
2409 // https://github.com/google/sanitizers/issues/321.
2410 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2412 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2416 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2418 #define INIT_GETSOCKNAME
2421 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2422 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2425 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2426 char **p = h->h_aliases;
2428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2431 COMMON_INTERCEPTOR_WRITE_RANGE(
2432 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2435 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2438 COMMON_INTERCEPTOR_WRITE_RANGE(
2439 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2443 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2444 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2446 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2447 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2448 if (res) write_hostent(ctx, res);
2452 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2455 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2456 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2457 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2458 if (res) write_hostent(ctx, res);
2462 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2464 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2465 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2466 if (res) write_hostent(ctx, res);
2470 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2472 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2473 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2474 if (res) write_hostent(ctx, res);
2477 #define INIT_GETHOSTBYNAME \
2478 COMMON_INTERCEPT_FUNCTION(gethostent); \
2479 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2480 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
2481 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2483 #define INIT_GETHOSTBYNAME
2486 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2487 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2488 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2491 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2493 // FIXME: under ASan the call below may write to freed memory and corrupt
2494 // its metadata. See
2495 // https://github.com/google/sanitizers/issues/321.
2496 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2498 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2499 if (res == 0 && *result) write_hostent(ctx, *result);
2502 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2505 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2507 #define INIT_GETHOSTBYNAME_R
2510 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2511 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2512 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2514 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2516 // FIXME: under ASan the call below may write to freed memory and corrupt
2517 // its metadata. See
2518 // https://github.com/google/sanitizers/issues/321.
2519 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2521 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2522 if (res == 0 && *result) write_hostent(ctx, *result);
2525 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2528 #define INIT_GETHOSTENT_R \
2529 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2531 #define INIT_GETHOSTENT_R
2534 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2535 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2536 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2537 __sanitizer_hostent **result, int *h_errnop) {
2539 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2540 buflen, result, h_errnop);
2541 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2542 // FIXME: under ASan the call below may write to freed memory and corrupt
2543 // its metadata. See
2544 // https://github.com/google/sanitizers/issues/321.
2545 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2548 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2549 if (res == 0 && *result) write_hostent(ctx, *result);
2552 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2555 #define INIT_GETHOSTBYADDR_R \
2556 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2558 #define INIT_GETHOSTBYADDR_R
2561 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2562 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2563 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2564 __sanitizer_hostent **result, int *h_errnop) {
2566 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
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.
2572 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2574 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2575 if (res == 0 && *result) write_hostent(ctx, *result);
2578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2581 #define INIT_GETHOSTBYNAME2_R \
2582 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2584 #define INIT_GETHOSTBYNAME2_R
2587 #if SANITIZER_INTERCEPT_GETSOCKOPT
2588 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2591 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2593 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2594 // FIXME: under ASan the call below may write to freed memory and corrupt
2595 // its metadata. See
2596 // https://github.com/google/sanitizers/issues/321.
2597 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2599 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2602 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2604 #define INIT_GETSOCKOPT
2607 #if SANITIZER_INTERCEPT_ACCEPT
2608 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2610 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2611 unsigned addrlen0 = 0;
2613 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2614 addrlen0 = *addrlen;
2616 int fd2 = REAL(accept)(fd, addr, addrlen);
2618 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2619 if (addr && addrlen)
2620 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2624 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2629 #if SANITIZER_INTERCEPT_ACCEPT4
2630 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2632 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2633 unsigned addrlen0 = 0;
2635 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2636 addrlen0 = *addrlen;
2638 // FIXME: under ASan the call below may write to freed memory and corrupt
2639 // its metadata. See
2640 // https://github.com/google/sanitizers/issues/321.
2641 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2643 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2644 if (addr && addrlen)
2645 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2649 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2651 #define INIT_ACCEPT4
2654 #if SANITIZER_INTERCEPT_MODF
2655 INTERCEPTOR(double, modf, double x, double *iptr) {
2657 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2658 // FIXME: under ASan the call below may write to freed memory and corrupt
2659 // its metadata. See
2660 // https://github.com/google/sanitizers/issues/321.
2661 double res = REAL(modf)(x, iptr);
2663 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2667 INTERCEPTOR(float, modff, float x, float *iptr) {
2669 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2670 // FIXME: under ASan the call below may write to freed memory and corrupt
2671 // its metadata. See
2672 // https://github.com/google/sanitizers/issues/321.
2673 float res = REAL(modff)(x, iptr);
2675 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2679 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2681 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2682 // FIXME: under ASan the call below may write to freed memory and corrupt
2683 // its metadata. See
2684 // https://github.com/google/sanitizers/issues/321.
2685 long double res = REAL(modfl)(x, iptr);
2687 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2692 COMMON_INTERCEPT_FUNCTION(modf); \
2693 COMMON_INTERCEPT_FUNCTION(modff); \
2694 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2699 #if SANITIZER_INTERCEPT_RECVMSG
2700 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2703 if (msg->msg_name && msg->msg_namelen)
2704 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2705 if (msg->msg_iov && msg->msg_iovlen)
2706 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2707 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2708 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2709 if (msg->msg_control && msg->msg_controllen)
2710 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2713 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2716 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2717 // FIXME: under ASan the call below may write to freed memory and corrupt
2718 // its metadata. See
2719 // https://github.com/google/sanitizers/issues/321.
2720 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2722 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2724 write_msghdr(ctx, msg, res);
2725 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2730 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2732 #define INIT_RECVMSG
2735 #if SANITIZER_INTERCEPT_SENDMSG
2736 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2737 const unsigned kCmsgDataOffset =
2738 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2740 char *p = (char *)control;
2741 char *const control_end = p + controllen;
2743 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2744 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2745 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2747 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2749 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2750 sizeof(cmsg->cmsg_level));
2751 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2752 sizeof(cmsg->cmsg_type));
2754 if (cmsg->cmsg_len > kCmsgDataOffset) {
2755 char *data = p + kCmsgDataOffset;
2756 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2757 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2760 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2764 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2767 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
2776 if (msg->msg_name && msg->msg_namelen)
2777 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2778 if (msg->msg_iov && msg->msg_iovlen)
2779 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
2780 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2781 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2782 if (msg->msg_control && msg->msg_controllen)
2783 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
2786 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
2789 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
2791 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
2792 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
2794 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
2795 if (common_flags()->intercept_send && res >= 0 && msg)
2796 read_msghdr(ctx, msg, res);
2799 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
2801 #define INIT_SENDMSG
2804 #if SANITIZER_INTERCEPT_GETPEERNAME
2805 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2807 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2809 if (addrlen) addr_sz = *addrlen;
2810 // FIXME: under ASan the call below may write to freed memory and corrupt
2811 // its metadata. See
2812 // https://github.com/google/sanitizers/issues/321.
2813 int res = REAL(getpeername)(sockfd, addr, addrlen);
2814 if (!res && addr && addrlen)
2815 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2818 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2820 #define INIT_GETPEERNAME
2823 #if SANITIZER_INTERCEPT_SYSINFO
2824 INTERCEPTOR(int, sysinfo, void *info) {
2826 // FIXME: under ASan the call below may write to freed memory and corrupt
2827 // its metadata. See
2828 // https://github.com/google/sanitizers/issues/321.
2829 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2830 int res = REAL(sysinfo)(info);
2832 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2835 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2837 #define INIT_SYSINFO
2840 #if SANITIZER_INTERCEPT_READDIR
2841 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2843 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2844 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2845 __sanitizer_dirent *res = REAL(opendir)(path);
2847 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2851 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2853 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2854 // FIXME: under ASan the call below may write to freed memory and corrupt
2855 // its metadata. See
2856 // https://github.com/google/sanitizers/issues/321.
2857 __sanitizer_dirent *res = REAL(readdir)(dirp);
2858 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2862 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2863 __sanitizer_dirent **result) {
2865 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2866 // FIXME: under ASan the call below may write to freed memory and corrupt
2867 // its metadata. See
2868 // https://github.com/google/sanitizers/issues/321.
2869 int res = REAL(readdir_r)(dirp, entry, result);
2871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2878 #define INIT_READDIR \
2879 COMMON_INTERCEPT_FUNCTION(opendir); \
2880 COMMON_INTERCEPT_FUNCTION(readdir); \
2881 COMMON_INTERCEPT_FUNCTION(readdir_r);
2883 #define INIT_READDIR
2886 #if SANITIZER_INTERCEPT_READDIR64
2887 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2889 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
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 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2894 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2898 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2899 __sanitizer_dirent64 **result) {
2901 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2902 // FIXME: under ASan the call below may write to freed memory and corrupt
2903 // its metadata. See
2904 // https://github.com/google/sanitizers/issues/321.
2905 int res = REAL(readdir64_r)(dirp, entry, result);
2907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2913 #define INIT_READDIR64 \
2914 COMMON_INTERCEPT_FUNCTION(readdir64); \
2915 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2917 #define INIT_READDIR64
2920 #if SANITIZER_INTERCEPT_PTRACE
2921 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2923 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2924 __sanitizer_iovec local_iovec;
2927 if (request == ptrace_setregs)
2928 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2929 else if (request == ptrace_setfpregs)
2930 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2931 else if (request == ptrace_setfpxregs)
2932 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2933 else if (request == ptrace_setvfpregs)
2934 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2935 else if (request == ptrace_setsiginfo)
2936 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2937 // Some kernel might zero the iovec::iov_base in case of invalid
2938 // write access. In this case copy the invalid address for further
2940 else if (request == ptrace_setregset || request == ptrace_getregset) {
2941 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2942 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
2943 local_iovec = *iovec;
2944 if (request == ptrace_setregset)
2945 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
2949 // FIXME: under ASan the call below may write to freed memory and corrupt
2950 // its metadata. See
2951 // https://github.com/google/sanitizers/issues/321.
2952 uptr res = REAL(ptrace)(request, pid, addr, data);
2955 // Note that PEEK* requests assign different meaning to the return value.
2956 // This function does not handle them (nor does it need to).
2957 if (request == ptrace_getregs)
2958 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2959 else if (request == ptrace_getfpregs)
2960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2961 else if (request == ptrace_getfpxregs)
2962 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2963 else if (request == ptrace_getvfpregs)
2964 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2965 else if (request == ptrace_getsiginfo)
2966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2967 else if (request == ptrace_geteventmsg)
2968 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2969 else if (request == ptrace_getregset) {
2970 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2971 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
2972 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
2973 local_iovec.iov_len);
2979 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2984 #if SANITIZER_INTERCEPT_SETLOCALE
2985 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2987 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2989 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2990 char *res = REAL(setlocale)(category, locale);
2991 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2995 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2997 #define INIT_SETLOCALE
3000 #if SANITIZER_INTERCEPT_GETCWD
3001 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3003 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
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 char *res = REAL(getcwd)(buf, size);
3008 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3011 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3016 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3017 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3019 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3020 // FIXME: under ASan the call below may write to freed memory and corrupt
3021 // its metadata. See
3022 // https://github.com/google/sanitizers/issues/321.
3023 char *res = REAL(get_current_dir_name)(fake);
3024 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3028 #define INIT_GET_CURRENT_DIR_NAME \
3029 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3031 #define INIT_GET_CURRENT_DIR_NAME
3034 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3036 if (nptr == *endptr) {
3037 // No digits were found at strtol call, we need to find out the last
3038 // symbol accessed by strtoll on our own.
3039 // We get this symbol by skipping leading blanks and optional +/- sign.
3040 while (IsSpace(*nptr)) nptr++;
3041 if (*nptr == '+' || *nptr == '-') nptr++;
3042 *endptr = const_cast<char *>(nptr);
3044 CHECK(*endptr >= nptr);
3047 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3048 char **endptr, char *real_endptr, int base) {
3050 *endptr = real_endptr;
3051 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3053 // If base has unsupported value, strtol can exit with EINVAL
3054 // without reading any characters. So do additional checks only
3055 // if base is valid.
3056 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3057 if (is_valid_base) {
3058 FixRealStrtolEndptr(nptr, &real_endptr);
3060 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3061 (real_endptr - nptr) + 1 : 0);
3065 #if SANITIZER_INTERCEPT_STRTOIMAX
3066 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3068 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3069 // FIXME: under ASan the call below may write to freed memory and corrupt
3070 // its metadata. See
3071 // https://github.com/google/sanitizers/issues/321.
3073 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3074 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3078 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3080 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3081 // FIXME: under ASan the call below may write to freed memory and corrupt
3082 // its metadata. See
3083 // https://github.com/google/sanitizers/issues/321.
3085 INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3086 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3090 #define INIT_STRTOIMAX \
3091 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3092 COMMON_INTERCEPT_FUNCTION(strtoumax);
3094 #define INIT_STRTOIMAX
3097 #if SANITIZER_INTERCEPT_MBSTOWCS
3098 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3100 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3101 // FIXME: under ASan the call below may write to freed memory and corrupt
3102 // its metadata. See
3103 // https://github.com/google/sanitizers/issues/321.
3104 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3105 if (res != (SIZE_T) - 1 && dest) {
3106 SIZE_T write_cnt = res + (res < len);
3107 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3112 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3115 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3116 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3117 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3118 // FIXME: under ASan the call below may write to freed memory and corrupt
3119 // its metadata. See
3120 // https://github.com/google/sanitizers/issues/321.
3121 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3122 if (res != (SIZE_T)(-1) && dest && src) {
3123 // This function, and several others, may or may not write the terminating
3124 // \0 character. They write it iff they clear *src.
3125 SIZE_T write_cnt = res + !*src;
3126 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3131 #define INIT_MBSTOWCS \
3132 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3133 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3135 #define INIT_MBSTOWCS
3138 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3139 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3140 SIZE_T len, void *ps) {
3142 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3144 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3145 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3147 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3148 // FIXME: under ASan the call below may write to freed memory and corrupt
3149 // its metadata. See
3150 // https://github.com/google/sanitizers/issues/321.
3151 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3152 if (res != (SIZE_T)(-1) && dest && src) {
3153 SIZE_T write_cnt = res + !*src;
3154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3159 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3161 #define INIT_MBSNRTOWCS
3164 #if SANITIZER_INTERCEPT_WCSTOMBS
3165 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3167 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3168 // FIXME: under ASan the call below may write to freed memory and corrupt
3169 // its metadata. See
3170 // https://github.com/google/sanitizers/issues/321.
3171 SIZE_T res = REAL(wcstombs)(dest, src, len);
3172 if (res != (SIZE_T) - 1 && dest) {
3173 SIZE_T write_cnt = res + (res < len);
3174 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3179 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3182 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3183 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3184 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3185 // FIXME: under ASan the call below may write to freed memory and corrupt
3186 // its metadata. See
3187 // https://github.com/google/sanitizers/issues/321.
3188 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3189 if (res != (SIZE_T) - 1 && dest && src) {
3190 SIZE_T write_cnt = res + !*src;
3191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3196 #define INIT_WCSTOMBS \
3197 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3198 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3200 #define INIT_WCSTOMBS
3203 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3204 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3205 SIZE_T len, void *ps) {
3207 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3209 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3210 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3212 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3213 // FIXME: under ASan the call below may write to freed memory and corrupt
3214 // its metadata. See
3215 // https://github.com/google/sanitizers/issues/321.
3216 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3217 if (res != ((SIZE_T)-1) && dest && src) {
3218 SIZE_T write_cnt = res + !*src;
3219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3224 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3226 #define INIT_WCSNRTOMBS
3230 #if SANITIZER_INTERCEPT_WCRTOMB
3231 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3233 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3234 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3235 // FIXME: under ASan the call below may write to freed memory and corrupt
3236 // its metadata. See
3237 // https://github.com/google/sanitizers/issues/321.
3238 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3239 if (res != ((SIZE_T)-1) && dest) {
3240 SIZE_T write_cnt = res;
3241 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3246 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3248 #define INIT_WCRTOMB
3251 #if SANITIZER_INTERCEPT_TCGETATTR
3252 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3254 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3255 // FIXME: under ASan the call below may write to freed memory and corrupt
3256 // its metadata. See
3257 // https://github.com/google/sanitizers/issues/321.
3258 int res = REAL(tcgetattr)(fd, termios_p);
3259 if (!res && termios_p)
3260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3264 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3266 #define INIT_TCGETATTR
3269 #if SANITIZER_INTERCEPT_REALPATH
3270 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3272 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3273 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3275 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3276 // version of a versioned symbol. For realpath(), this gives us something
3277 // (called __old_realpath) that does not handle NULL in the second argument.
3278 // Handle it as part of the interceptor.
3279 char *allocated_path = nullptr;
3281 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3283 char *res = REAL(realpath)(path, resolved_path);
3284 if (allocated_path && !res) WRAP(free)(allocated_path);
3285 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3288 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3290 #define INIT_REALPATH
3293 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3294 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3296 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3297 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3298 char *res = REAL(canonicalize_file_name)(path);
3299 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3302 #define INIT_CANONICALIZE_FILE_NAME \
3303 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3305 #define INIT_CANONICALIZE_FILE_NAME
3308 #if SANITIZER_INTERCEPT_CONFSTR
3309 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3311 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3312 // FIXME: under ASan the call below may write to freed memory and corrupt
3313 // its metadata. See
3314 // https://github.com/google/sanitizers/issues/321.
3315 SIZE_T res = REAL(confstr)(name, buf, len);
3317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3320 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3322 #define INIT_CONFSTR
3325 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3326 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3328 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3329 // FIXME: under ASan the call below may write to freed memory and corrupt
3330 // its metadata. See
3331 // https://github.com/google/sanitizers/issues/321.
3332 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3333 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3336 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3338 #define INIT_SCHED_GETAFFINITY
3341 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3342 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3344 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3345 int res = REAL(sched_getparam)(pid, param);
3346 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3349 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3351 #define INIT_SCHED_GETPARAM
3354 #if SANITIZER_INTERCEPT_STRERROR
3355 INTERCEPTOR(char *, strerror, int errnum) {
3357 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3358 char *res = REAL(strerror)(errnum);
3359 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3362 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3364 #define INIT_STRERROR
3367 #if SANITIZER_INTERCEPT_STRERROR_R
3368 // There are 2 versions of strerror_r:
3369 // * POSIX version returns 0 on success, negative error code on failure,
3370 // writes message to buf.
3371 // * GNU version returns message pointer, which points to either buf or some
3373 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3374 SANITIZER_MAC || SANITIZER_ANDROID
3375 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3376 // At least on OSX, buf contents are valid even when the call fails.
3377 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3379 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3380 // FIXME: under ASan the call below may write to freed memory and corrupt
3381 // its metadata. See
3382 // https://github.com/google/sanitizers/issues/321.
3383 int res = REAL(strerror_r)(errnum, buf, buflen);
3385 SIZE_T sz = internal_strnlen(buf, buflen);
3386 if (sz < buflen) ++sz;
3387 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3392 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3394 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3395 // FIXME: under ASan the call below may write to freed memory and corrupt
3396 // its metadata. See
3397 // https://github.com/google/sanitizers/issues/321.
3398 char *res = REAL(strerror_r)(errnum, buf, buflen);
3400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3402 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3405 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3407 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3409 #define INIT_STRERROR_R
3412 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3413 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3415 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3416 // FIXME: under ASan the call below may write to freed memory and corrupt
3417 // its metadata. See
3418 // https://github.com/google/sanitizers/issues/321.
3419 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3420 // This version always returns a null-terminated string.
3422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3425 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3427 #define INIT_XPG_STRERROR_R
3430 #if SANITIZER_INTERCEPT_SCANDIR
3431 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3432 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3433 const struct __sanitizer_dirent **);
3435 static THREADLOCAL scandir_filter_f scandir_filter;
3436 static THREADLOCAL scandir_compar_f scandir_compar;
3438 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3439 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3440 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3441 return scandir_filter(dir);
3444 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3445 const struct __sanitizer_dirent **b) {
3446 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3447 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3448 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3449 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3450 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3451 return scandir_compar(a, b);
3454 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3455 scandir_filter_f filter, scandir_compar_f compar) {
3457 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3458 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3459 scandir_filter = filter;
3460 scandir_compar = compar;
3461 // FIXME: under ASan the call below may write to freed memory and corrupt
3462 // its metadata. See
3463 // https://github.com/google/sanitizers/issues/321.
3464 int res = REAL(scandir)(dirp, namelist,
3465 filter ? wrapped_scandir_filter : nullptr,
3466 compar ? wrapped_scandir_compar : nullptr);
3467 scandir_filter = nullptr;
3468 scandir_compar = nullptr;
3469 if (namelist && res > 0) {
3470 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3471 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3472 for (int i = 0; i < res; ++i)
3473 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3474 (*namelist)[i]->d_reclen);
3478 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3480 #define INIT_SCANDIR
3483 #if SANITIZER_INTERCEPT_SCANDIR64
3484 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3485 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3486 const struct __sanitizer_dirent64 **);
3488 static THREADLOCAL scandir64_filter_f scandir64_filter;
3489 static THREADLOCAL scandir64_compar_f scandir64_compar;
3491 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3492 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3493 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3494 return scandir64_filter(dir);
3497 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3498 const struct __sanitizer_dirent64 **b) {
3499 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3500 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3501 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3502 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3503 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3504 return scandir64_compar(a, b);
3507 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3508 scandir64_filter_f filter, scandir64_compar_f compar) {
3510 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3511 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3512 scandir64_filter = filter;
3513 scandir64_compar = compar;
3514 // FIXME: under ASan the call below may write to freed memory and corrupt
3515 // its metadata. See
3516 // https://github.com/google/sanitizers/issues/321.
3518 REAL(scandir64)(dirp, namelist,
3519 filter ? wrapped_scandir64_filter : nullptr,
3520 compar ? wrapped_scandir64_compar : nullptr);
3521 scandir64_filter = nullptr;
3522 scandir64_compar = nullptr;
3523 if (namelist && res > 0) {
3524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3525 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3526 for (int i = 0; i < res; ++i)
3527 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3528 (*namelist)[i]->d_reclen);
3532 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3534 #define INIT_SCANDIR64
3537 #if SANITIZER_INTERCEPT_GETGROUPS
3538 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3540 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
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(getgroups)(size, lst);
3545 if (res >= 0 && lst && size > 0)
3546 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3549 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3551 #define INIT_GETGROUPS
3554 #if SANITIZER_INTERCEPT_POLL
3555 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3556 __sanitizer_nfds_t nfds) {
3557 for (unsigned i = 0; i < nfds; ++i) {
3558 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3559 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3563 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3564 __sanitizer_nfds_t nfds) {
3565 for (unsigned i = 0; i < nfds; ++i)
3566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3567 sizeof(fds[i].revents));
3570 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3573 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3574 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3575 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3576 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3579 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3584 #if SANITIZER_INTERCEPT_PPOLL
3585 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3586 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3588 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3589 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3591 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3592 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3594 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3595 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3598 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3603 #if SANITIZER_INTERCEPT_WORDEXP
3604 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3606 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3607 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3608 // FIXME: under ASan the call below may write to freed memory and corrupt
3609 // its metadata. See
3610 // https://github.com/google/sanitizers/issues/321.
3611 int res = REAL(wordexp)(s, p, flags);
3613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3615 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3616 sizeof(*p->we_wordv) * p->we_wordc);
3617 for (uptr i = 0; i < p->we_wordc; ++i) {
3618 char *w = p->we_wordv[i];
3619 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3624 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3626 #define INIT_WORDEXP
3629 #if SANITIZER_INTERCEPT_SIGWAIT
3630 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3632 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3633 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3634 // FIXME: under ASan the call below may write to freed memory and corrupt
3635 // its metadata. See
3636 // https://github.com/google/sanitizers/issues/321.
3637 int res = REAL(sigwait)(set, sig);
3638 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3641 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3643 #define INIT_SIGWAIT
3646 #if SANITIZER_INTERCEPT_SIGWAITINFO
3647 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3649 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3650 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3651 // FIXME: under ASan the call below may write to freed memory and corrupt
3652 // its metadata. See
3653 // https://github.com/google/sanitizers/issues/321.
3654 int res = REAL(sigwaitinfo)(set, info);
3655 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3658 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3660 #define INIT_SIGWAITINFO
3663 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3664 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3667 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3668 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3669 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3670 // FIXME: under ASan the call below may write to freed memory and corrupt
3671 // its metadata. See
3672 // https://github.com/google/sanitizers/issues/321.
3673 int res = REAL(sigtimedwait)(set, info, timeout);
3674 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3677 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3679 #define INIT_SIGTIMEDWAIT
3682 #if SANITIZER_INTERCEPT_SIGSETOPS
3683 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3685 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3686 // FIXME: under ASan the call below may write to freed memory and corrupt
3687 // its metadata. See
3688 // https://github.com/google/sanitizers/issues/321.
3689 int res = REAL(sigemptyset)(set);
3690 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3694 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3696 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3697 // FIXME: under ASan the call below may write to freed memory and corrupt
3698 // its metadata. See
3699 // https://github.com/google/sanitizers/issues/321.
3700 int res = REAL(sigfillset)(set);
3701 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3704 #define INIT_SIGSETOPS \
3705 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3706 COMMON_INTERCEPT_FUNCTION(sigfillset);
3708 #define INIT_SIGSETOPS
3711 #if SANITIZER_INTERCEPT_SIGPENDING
3712 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3714 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3715 // FIXME: under ASan the call below may write to freed memory and corrupt
3716 // its metadata. See
3717 // https://github.com/google/sanitizers/issues/321.
3718 int res = REAL(sigpending)(set);
3719 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3722 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3724 #define INIT_SIGPENDING
3727 #if SANITIZER_INTERCEPT_SIGPROCMASK
3728 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3729 __sanitizer_sigset_t *oldset) {
3731 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3732 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3733 // FIXME: under ASan the call below may write to freed memory and corrupt
3734 // its metadata. See
3735 // https://github.com/google/sanitizers/issues/321.
3736 int res = REAL(sigprocmask)(how, set, oldset);
3738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3741 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3743 #define INIT_SIGPROCMASK
3746 #if SANITIZER_INTERCEPT_BACKTRACE
3747 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3749 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
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(backtrace)(buffer, size);
3755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3759 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3761 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3763 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3764 // FIXME: under ASan the call below may write to freed memory and corrupt
3765 // its metadata. See
3766 // https://github.com/google/sanitizers/issues/321.
3767 char **res = REAL(backtrace_symbols)(buffer, size);
3769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3770 for (int i = 0; i < size; ++i)
3771 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3775 #define INIT_BACKTRACE \
3776 COMMON_INTERCEPT_FUNCTION(backtrace); \
3777 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3779 #define INIT_BACKTRACE
3782 #if SANITIZER_INTERCEPT__EXIT
3783 INTERCEPTOR(void, _exit, int status) {
3785 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3786 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3787 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3788 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3789 if (status == 0) status = status1;
3790 REAL(_exit)(status);
3792 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3797 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3798 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3800 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3801 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
3802 int res = REAL(pthread_mutex_lock)(m);
3803 if (res == errno_EOWNERDEAD)
3804 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3805 if (res == 0 || res == errno_EOWNERDEAD)
3806 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
3807 if (res == errno_EINVAL)
3808 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3812 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3814 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3815 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3816 int res = REAL(pthread_mutex_unlock)(m);
3817 if (res == errno_EINVAL)
3818 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3822 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3823 #define INIT_PTHREAD_MUTEX_UNLOCK \
3824 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3826 #define INIT_PTHREAD_MUTEX_LOCK
3827 #define INIT_PTHREAD_MUTEX_UNLOCK
3830 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3831 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3832 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3833 if (mnt->mnt_fsname)
3834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3835 REAL(strlen)(mnt->mnt_fsname) + 1);
3837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3838 REAL(strlen)(mnt->mnt_dir) + 1);
3840 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3841 REAL(strlen)(mnt->mnt_type) + 1);
3843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3844 REAL(strlen)(mnt->mnt_opts) + 1);
3848 #if SANITIZER_INTERCEPT_GETMNTENT
3849 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3851 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3852 __sanitizer_mntent *res = REAL(getmntent)(fp);
3853 if (res) write_mntent(ctx, res);
3856 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3858 #define INIT_GETMNTENT
3861 #if SANITIZER_INTERCEPT_GETMNTENT_R
3862 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3863 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3865 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3866 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3867 if (res) write_mntent(ctx, res);
3870 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3872 #define INIT_GETMNTENT_R
3875 #if SANITIZER_INTERCEPT_STATFS
3876 INTERCEPTOR(int, statfs, char *path, void *buf) {
3878 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3879 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3880 // FIXME: under ASan the call below may write to freed memory and corrupt
3881 // its metadata. See
3882 // https://github.com/google/sanitizers/issues/321.
3883 int res = REAL(statfs)(path, buf);
3884 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3887 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3889 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3890 // FIXME: under ASan the call below may write to freed memory and corrupt
3891 // its metadata. See
3892 // https://github.com/google/sanitizers/issues/321.
3893 int res = REAL(fstatfs)(fd, buf);
3894 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3897 #define INIT_STATFS \
3898 COMMON_INTERCEPT_FUNCTION(statfs); \
3899 COMMON_INTERCEPT_FUNCTION(fstatfs);
3904 #if SANITIZER_INTERCEPT_STATFS64
3905 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3907 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3908 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3909 // FIXME: under ASan the call below may write to freed memory and corrupt
3910 // its metadata. See
3911 // https://github.com/google/sanitizers/issues/321.
3912 int res = REAL(statfs64)(path, buf);
3913 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3916 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3918 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3919 // FIXME: under ASan the call below may write to freed memory and corrupt
3920 // its metadata. See
3921 // https://github.com/google/sanitizers/issues/321.
3922 int res = REAL(fstatfs64)(fd, buf);
3923 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3926 #define INIT_STATFS64 \
3927 COMMON_INTERCEPT_FUNCTION(statfs64); \
3928 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3930 #define INIT_STATFS64
3933 #if SANITIZER_INTERCEPT_STATVFS
3934 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3936 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3937 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3938 // FIXME: under ASan the call below may write to freed memory and corrupt
3939 // its metadata. See
3940 // https://github.com/google/sanitizers/issues/321.
3941 int res = REAL(statvfs)(path, buf);
3942 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3945 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3947 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3948 // FIXME: under ASan the call below may write to freed memory and corrupt
3949 // its metadata. See
3950 // https://github.com/google/sanitizers/issues/321.
3951 int res = REAL(fstatvfs)(fd, buf);
3952 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3955 #define INIT_STATVFS \
3956 COMMON_INTERCEPT_FUNCTION(statvfs); \
3957 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3959 #define INIT_STATVFS
3962 #if SANITIZER_INTERCEPT_STATVFS64
3963 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3965 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3966 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3967 // FIXME: under ASan the call below may write to freed memory and corrupt
3968 // its metadata. See
3969 // https://github.com/google/sanitizers/issues/321.
3970 int res = REAL(statvfs64)(path, buf);
3971 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3974 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3976 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3977 // FIXME: under ASan the call below may write to freed memory and corrupt
3978 // its metadata. See
3979 // https://github.com/google/sanitizers/issues/321.
3980 int res = REAL(fstatvfs64)(fd, buf);
3981 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3984 #define INIT_STATVFS64 \
3985 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3986 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3988 #define INIT_STATVFS64
3991 #if SANITIZER_INTERCEPT_INITGROUPS
3992 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3994 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3995 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3996 int res = REAL(initgroups)(user, group);
3999 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4001 #define INIT_INITGROUPS
4004 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4005 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4007 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4008 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4009 char *res = REAL(ether_ntoa)(addr);
4010 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4013 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4015 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4016 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4017 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4018 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4021 #define INIT_ETHER_NTOA_ATON \
4022 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4023 COMMON_INTERCEPT_FUNCTION(ether_aton);
4025 #define INIT_ETHER_NTOA_ATON
4028 #if SANITIZER_INTERCEPT_ETHER_HOST
4029 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4031 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4032 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4033 // FIXME: under ASan the call below may write to freed memory and corrupt
4034 // its metadata. See
4035 // https://github.com/google/sanitizers/issues/321.
4036 int res = REAL(ether_ntohost)(hostname, addr);
4037 if (!res && hostname)
4038 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4041 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4043 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4045 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4046 // FIXME: under ASan the call below may write to freed memory and corrupt
4047 // its metadata. See
4048 // https://github.com/google/sanitizers/issues/321.
4049 int res = REAL(ether_hostton)(hostname, addr);
4050 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4053 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4056 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4057 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4058 // FIXME: under ASan the call below may write to freed memory and corrupt
4059 // its metadata. See
4060 // https://github.com/google/sanitizers/issues/321.
4061 int res = REAL(ether_line)(line, addr, hostname);
4063 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4065 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4069 #define INIT_ETHER_HOST \
4070 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4071 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4072 COMMON_INTERCEPT_FUNCTION(ether_line);
4074 #define INIT_ETHER_HOST
4077 #if SANITIZER_INTERCEPT_ETHER_R
4078 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4080 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4081 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4082 // FIXME: under ASan the call below may write to freed memory and corrupt
4083 // its metadata. See
4084 // https://github.com/google/sanitizers/issues/321.
4085 char *res = REAL(ether_ntoa_r)(addr, buf);
4086 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4089 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4090 __sanitizer_ether_addr *addr) {
4092 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4093 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4094 // FIXME: under ASan the call below may write to freed memory and corrupt
4095 // its metadata. See
4096 // https://github.com/google/sanitizers/issues/321.
4097 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4098 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4101 #define INIT_ETHER_R \
4102 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4103 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4105 #define INIT_ETHER_R
4108 #if SANITIZER_INTERCEPT_SHMCTL
4109 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4111 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4112 // FIXME: under ASan the call below may write to freed memory and corrupt
4113 // its metadata. See
4114 // https://github.com/google/sanitizers/issues/321.
4115 int res = REAL(shmctl)(shmid, cmd, buf);
4118 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4119 sz = sizeof(__sanitizer_shmid_ds);
4120 else if (cmd == shmctl_ipc_info)
4121 sz = struct_shminfo_sz;
4122 else if (cmd == shmctl_shm_info)
4123 sz = struct_shm_info_sz;
4124 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4128 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4133 #if SANITIZER_INTERCEPT_RANDOM_R
4134 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4136 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4137 // FIXME: under ASan the call below may write to freed memory and corrupt
4138 // its metadata. See
4139 // https://github.com/google/sanitizers/issues/321.
4140 int res = REAL(random_r)(buf, result);
4142 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4145 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4147 #define INIT_RANDOM_R
4150 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4151 // its metadata. See
4152 // https://github.com/google/sanitizers/issues/321.
4153 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4154 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4155 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4156 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4157 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4158 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4159 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4160 INTERCEPTOR(int, fn, void *attr, void *r) { \
4162 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4163 int res = REAL(fn)(attr, r); \
4164 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4167 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4168 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4169 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4170 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4171 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4172 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4173 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4174 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4175 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4176 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4179 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4180 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4181 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4182 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4183 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4184 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4185 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4186 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4188 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4189 // FIXME: under ASan the call below may write to freed memory and corrupt
4190 // its metadata. See
4191 // https://github.com/google/sanitizers/issues/321.
4192 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4194 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4195 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4200 // We may need to call the real pthread_attr_getstack from the run-time
4201 // in sanitizer_common, but we don't want to include the interception headers
4202 // there. So, just define this function here.
4203 namespace __sanitizer {
4205 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4206 return REAL(pthread_attr_getstack)(attr, addr, size);
4209 } // namespace __sanitizer
4211 #define INIT_PTHREAD_ATTR_GET \
4212 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4213 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4214 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4215 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4216 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4217 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4218 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4220 #define INIT_PTHREAD_ATTR_GET
4223 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4224 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4226 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4227 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4229 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4232 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4233 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4236 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4238 // FIXME: under ASan the call below may write to freed memory and corrupt
4239 // its metadata. See
4240 // https://github.com/google/sanitizers/issues/321.
4241 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4242 if (!res && cpusetsize && cpuset)
4243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4247 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4248 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4250 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4253 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4254 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4255 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4256 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4258 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4261 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4262 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4263 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4264 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4266 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4269 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4270 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4271 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4272 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4274 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4277 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4278 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4279 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4280 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4282 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4285 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4286 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4287 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4288 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4290 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4293 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4294 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4295 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4296 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4298 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4301 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4302 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4303 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4304 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4306 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4309 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4310 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4311 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4312 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4314 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4317 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4318 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4319 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4320 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4322 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4325 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4326 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4327 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4328 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4330 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4333 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4334 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4335 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4336 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4338 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4341 #if SANITIZER_INTERCEPT_TMPNAM
4342 INTERCEPTOR(char *, tmpnam, char *s) {
4344 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4345 char *res = REAL(tmpnam)(s);
4348 // FIXME: under ASan the call below may write to freed memory and corrupt
4349 // its metadata. See
4350 // https://github.com/google/sanitizers/issues/321.
4351 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4353 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4357 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4362 #if SANITIZER_INTERCEPT_TMPNAM_R
4363 INTERCEPTOR(char *, tmpnam_r, char *s) {
4365 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4366 // FIXME: under ASan the call below may write to freed memory and corrupt
4367 // its metadata. See
4368 // https://github.com/google/sanitizers/issues/321.
4369 char *res = REAL(tmpnam_r)(s);
4370 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4373 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4375 #define INIT_TMPNAM_R
4378 #if SANITIZER_INTERCEPT_TTYNAME_R
4379 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4381 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4382 int res = REAL(ttyname_r)(fd, name, namesize);
4384 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4387 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4389 #define INIT_TTYNAME_R
4392 #if SANITIZER_INTERCEPT_TEMPNAM
4393 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4395 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4396 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4397 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4398 char *res = REAL(tempnam)(dir, pfx);
4399 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4402 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4404 #define INIT_TEMPNAM
4407 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4408 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4410 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4411 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4412 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4413 return REAL(pthread_setname_np)(thread, name);
4415 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4417 #define INIT_PTHREAD_SETNAME_NP
4420 #if SANITIZER_INTERCEPT_SINCOS
4421 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4423 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4424 // FIXME: under ASan the call below may write to freed memory and corrupt
4425 // its metadata. See
4426 // https://github.com/google/sanitizers/issues/321.
4427 REAL(sincos)(x, sin, cos);
4428 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4429 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4431 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4433 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4434 // FIXME: under ASan the call below may write to freed memory and corrupt
4435 // its metadata. See
4436 // https://github.com/google/sanitizers/issues/321.
4437 REAL(sincosf)(x, sin, cos);
4438 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4439 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4441 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4443 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4444 // FIXME: under ASan the call below may write to freed memory and corrupt
4445 // its metadata. See
4446 // https://github.com/google/sanitizers/issues/321.
4447 REAL(sincosl)(x, sin, cos);
4448 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4449 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4451 #define INIT_SINCOS \
4452 COMMON_INTERCEPT_FUNCTION(sincos); \
4453 COMMON_INTERCEPT_FUNCTION(sincosf); \
4454 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4459 #if SANITIZER_INTERCEPT_REMQUO
4460 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4462 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4463 // FIXME: under ASan the call below may write to freed memory and corrupt
4464 // its metadata. See
4465 // https://github.com/google/sanitizers/issues/321.
4466 double res = REAL(remquo)(x, y, quo);
4467 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4470 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4472 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4473 // FIXME: under ASan the call below may write to freed memory and corrupt
4474 // its metadata. See
4475 // https://github.com/google/sanitizers/issues/321.
4476 float res = REAL(remquof)(x, y, quo);
4477 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4480 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4482 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4483 // FIXME: under ASan the call below may write to freed memory and corrupt
4484 // its metadata. See
4485 // https://github.com/google/sanitizers/issues/321.
4486 long double res = REAL(remquol)(x, y, quo);
4487 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4490 #define INIT_REMQUO \
4491 COMMON_INTERCEPT_FUNCTION(remquo); \
4492 COMMON_INTERCEPT_FUNCTION(remquof); \
4493 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4498 #if SANITIZER_INTERCEPT_LGAMMA
4500 INTERCEPTOR(double, lgamma, double x) {
4502 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4503 double res = REAL(lgamma)(x);
4504 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4507 INTERCEPTOR(float, lgammaf, float x) {
4509 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4510 float res = REAL(lgammaf)(x);
4511 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4514 INTERCEPTOR(long double, lgammal, long double x) {
4516 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4517 long double res = REAL(lgammal)(x);
4518 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4521 #define INIT_LGAMMA \
4522 COMMON_INTERCEPT_FUNCTION(lgamma); \
4523 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4524 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4529 #if SANITIZER_INTERCEPT_LGAMMA_R
4530 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4532 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4533 // FIXME: under ASan the call below may write to freed memory and corrupt
4534 // its metadata. See
4535 // https://github.com/google/sanitizers/issues/321.
4536 double res = REAL(lgamma_r)(x, signp);
4537 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4540 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4542 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4543 // FIXME: under ASan the call below may write to freed memory and corrupt
4544 // its metadata. See
4545 // https://github.com/google/sanitizers/issues/321.
4546 float res = REAL(lgammaf_r)(x, signp);
4547 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4550 #define INIT_LGAMMA_R \
4551 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4552 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4554 #define INIT_LGAMMA_R
4557 #if SANITIZER_INTERCEPT_LGAMMAL_R
4558 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4560 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4561 // FIXME: under ASan the call below may write to freed memory and corrupt
4562 // its metadata. See
4563 // https://github.com/google/sanitizers/issues/321.
4564 long double res = REAL(lgammal_r)(x, signp);
4565 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4568 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4570 #define INIT_LGAMMAL_R
4573 #if SANITIZER_INTERCEPT_DRAND48_R
4574 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4576 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4577 // FIXME: under ASan the call below may write to freed memory and corrupt
4578 // its metadata. See
4579 // https://github.com/google/sanitizers/issues/321.
4580 int res = REAL(drand48_r)(buffer, result);
4581 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4584 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4586 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4587 // FIXME: under ASan the call below may write to freed memory and corrupt
4588 // its metadata. See
4589 // https://github.com/google/sanitizers/issues/321.
4590 int res = REAL(lrand48_r)(buffer, result);
4591 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4594 #define INIT_DRAND48_R \
4595 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4596 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4598 #define INIT_DRAND48_R
4601 #if SANITIZER_INTERCEPT_RAND_R
4602 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4604 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4605 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4606 return REAL(rand_r)(seedp);
4608 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4613 #if SANITIZER_INTERCEPT_GETLINE
4614 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4616 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4617 // FIXME: under ASan the call below may write to freed memory and corrupt
4618 // its metadata. See
4619 // https://github.com/google/sanitizers/issues/321.
4620 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4623 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4624 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4629 // FIXME: under ASan the call below may write to freed memory and corrupt its
4631 // https://github.com/google/sanitizers/issues/321.
4632 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4635 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4636 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4639 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4640 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4645 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4647 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4649 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4650 // with its own body.
4651 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4653 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4655 #define INIT_GETLINE \
4656 COMMON_INTERCEPT_FUNCTION(getline); \
4657 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4658 COMMON_INTERCEPT_FUNCTION(getdelim);
4660 #define INIT_GETLINE
4663 #if SANITIZER_INTERCEPT_ICONV
4664 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4665 char **outbuf, SIZE_T *outbytesleft) {
4667 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4670 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4671 if (inbuf && inbytesleft)
4672 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4674 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4675 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4676 // FIXME: under ASan the call below may write to freed memory and corrupt
4677 // its metadata. See
4678 // https://github.com/google/sanitizers/issues/321.
4679 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4680 if (outbuf && *outbuf > outbuf_orig) {
4681 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4682 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4686 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4691 #if SANITIZER_INTERCEPT_TIMES
4692 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4694 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4695 // FIXME: under ASan the call below may write to freed memory and corrupt
4696 // its metadata. See
4697 // https://github.com/google/sanitizers/issues/321.
4698 __sanitizer_clock_t res = REAL(times)(tms);
4699 if (res != (__sanitizer_clock_t)-1 && tms)
4700 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4703 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4708 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4710 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4711 // If you see any crashes around this functions, there are 2 known issues with
4712 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4713 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4714 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4715 // to access thread local variables (it should not happen normally,
4716 // because sanitizers use initial-exec tls model).
4717 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4719 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4720 void *res = REAL(__tls_get_addr)(arg);
4721 uptr tls_begin, tls_end;
4722 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4723 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4725 // New DTLS block has been allocated.
4726 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4731 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4732 // mostly the same semantics as __tls_get_addr, but its presence enables
4733 // some optimizations in linker (which are safe to ignore here).
4734 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4735 visibility("default")))
4736 void *__tls_get_addr_opt(void *arg);
4738 #else // SANITIZER_S390
4739 // On s390, we have to intercept two functions here:
4740 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4741 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4742 // a proper pointer. It is used by dlsym for TLS symbols.
4743 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4744 // descriptor offset as an argument instead of a pointer. GOT address
4745 // is passed in r12, so it's necessary to write it in assembly. This is
4746 // the function used by the compiler.
4747 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
4748 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
4749 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
4750 extern "C" uptr __tls_get_offset(void *arg);
4751 extern "C" uptr __interceptor___tls_get_offset(void *arg);
4752 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4754 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4755 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
4756 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4757 void *ptr = reinterpret_cast<void *>(res + tp);
4758 uptr tls_begin, tls_end;
4759 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4760 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4762 // New DTLS block has been allocated.
4763 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4767 // We need a hidden symbol aliasing the above, so that we can jump
4768 // directly to it from the assembly below.
4769 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4770 visibility("hidden")))
4771 uptr __tls_get_addr_hidden(void *arg);
4772 // Now carefully intercept __tls_get_offset.
4775 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4776 // exports our symbol.
4777 ".weak __tls_get_offset\n"
4778 ".type __tls_get_offset, @function\n"
4779 "__tls_get_offset:\n"
4780 ".global __interceptor___tls_get_offset\n"
4781 ".type __interceptor___tls_get_offset, @function\n"
4782 "__interceptor___tls_get_offset:\n"
4784 "la %r2, 0(%r2,%r12)\n"
4785 "jg __tls_get_addr_hidden\n"
4788 "0: la %r2,0(%r2,%r12)\n"
4789 "l %r4,1f-0b(%r3)\n"
4791 "1: .long __tls_get_addr_hidden - 0b\n"
4793 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
4794 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
4795 ".type __tls_get_offset_wrapper, @function\n"
4796 "__tls_get_offset_wrapper:\n"
4803 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
4805 #endif // SANITIZER_S390
4807 #define INIT_TLS_GET_ADDR
4810 #if SANITIZER_INTERCEPT_LISTXATTR
4811 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4813 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4814 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4815 // FIXME: under ASan the call below may write to freed memory and corrupt
4816 // its metadata. See
4817 // https://github.com/google/sanitizers/issues/321.
4818 SSIZE_T res = REAL(listxattr)(path, list, size);
4819 // Here and below, size == 0 is a special case where nothing is written to the
4820 // buffer, and res contains the desired buffer size.
4821 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4824 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4826 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4827 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4828 // FIXME: under ASan the call below may write to freed memory and corrupt
4829 // its metadata. See
4830 // https://github.com/google/sanitizers/issues/321.
4831 SSIZE_T res = REAL(llistxattr)(path, list, size);
4832 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4835 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4837 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4838 // FIXME: under ASan the call below may write to freed memory and corrupt
4839 // its metadata. See
4840 // https://github.com/google/sanitizers/issues/321.
4841 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4842 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4845 #define INIT_LISTXATTR \
4846 COMMON_INTERCEPT_FUNCTION(listxattr); \
4847 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4848 COMMON_INTERCEPT_FUNCTION(flistxattr);
4850 #define INIT_LISTXATTR
4853 #if SANITIZER_INTERCEPT_GETXATTR
4854 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4857 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4858 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4859 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4860 // FIXME: under ASan the call below may write to freed memory and corrupt
4861 // its metadata. See
4862 // https://github.com/google/sanitizers/issues/321.
4863 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4864 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4867 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4870 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4871 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4872 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4873 // FIXME: under ASan the call below may write to freed memory and corrupt
4874 // its metadata. See
4875 // https://github.com/google/sanitizers/issues/321.
4876 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4877 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4880 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4883 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4884 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4885 // FIXME: under ASan the call below may write to freed memory and corrupt
4886 // its metadata. See
4887 // https://github.com/google/sanitizers/issues/321.
4888 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4889 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4892 #define INIT_GETXATTR \
4893 COMMON_INTERCEPT_FUNCTION(getxattr); \
4894 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4895 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4897 #define INIT_GETXATTR
4900 #if SANITIZER_INTERCEPT_GETRESID
4901 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4903 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4904 // FIXME: under ASan the call below may write to freed memory and corrupt
4905 // its metadata. See
4906 // https://github.com/google/sanitizers/issues/321.
4907 int res = REAL(getresuid)(ruid, euid, suid);
4909 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4910 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4911 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4915 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4917 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4918 // FIXME: under ASan the call below may write to freed memory and corrupt
4919 // its metadata. See
4920 // https://github.com/google/sanitizers/issues/321.
4921 int res = REAL(getresgid)(rgid, egid, sgid);
4923 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4924 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4925 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4929 #define INIT_GETRESID \
4930 COMMON_INTERCEPT_FUNCTION(getresuid); \
4931 COMMON_INTERCEPT_FUNCTION(getresgid);
4933 #define INIT_GETRESID
4936 #if SANITIZER_INTERCEPT_GETIFADDRS
4937 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4938 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4939 // intercept it to poison the memory again.
4940 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4942 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4943 // FIXME: under ASan the call below may write to freed memory and corrupt
4944 // its metadata. See
4945 // https://github.com/google/sanitizers/issues/321.
4946 int res = REAL(getifaddrs)(ifap);
4947 if (res == 0 && ifap) {
4948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4949 __sanitizer_ifaddrs *p = *ifap;
4951 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4954 REAL(strlen)(p->ifa_name) + 1);
4956 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4958 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4959 // On Linux this is a union, but the other member also points to a
4960 // struct sockaddr, so the following is sufficient.
4962 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4963 // FIXME(smatveev): Unpoison p->ifa_data as well.
4969 #define INIT_GETIFADDRS \
4970 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4972 #define INIT_GETIFADDRS
4975 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4976 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4978 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4979 // FIXME: under ASan the call below may write to freed memory and corrupt
4980 // its metadata. See
4981 // https://github.com/google/sanitizers/issues/321.
4982 char *res = REAL(if_indextoname)(ifindex, ifname);
4984 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4987 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4989 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4991 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4992 return REAL(if_nametoindex)(ifname);
4994 #define INIT_IF_INDEXTONAME \
4995 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4996 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4998 #define INIT_IF_INDEXTONAME
5001 #if SANITIZER_INTERCEPT_CAPGET
5002 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5004 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5006 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5007 // FIXME: under ASan the call below may write to freed memory and corrupt
5008 // its metadata. See
5009 // https://github.com/google/sanitizers/issues/321.
5010 int res = REAL(capget)(hdrp, datap);
5011 if (res == 0 && datap)
5012 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5013 // We can also return -1 and write to hdrp->version if the version passed in
5014 // hdrp->version is unsupported. But that's not a trivial condition to check,
5015 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5018 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5020 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5022 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5024 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5025 return REAL(capset)(hdrp, datap);
5027 #define INIT_CAPGET \
5028 COMMON_INTERCEPT_FUNCTION(capget); \
5029 COMMON_INTERCEPT_FUNCTION(capset);
5034 #if SANITIZER_INTERCEPT_AEABI_MEM
5035 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5037 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5040 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5042 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5045 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5047 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5050 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5052 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5055 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5057 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5060 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5062 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5065 // Note the argument order.
5066 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5068 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5071 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5073 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5076 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5078 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5081 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5083 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5086 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5088 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5091 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5093 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5096 #define INIT_AEABI_MEM \
5097 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5098 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5099 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5100 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5101 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5102 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5103 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5104 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5105 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5106 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5107 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5108 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5110 #define INIT_AEABI_MEM
5111 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5113 #if SANITIZER_INTERCEPT___BZERO
5114 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5116 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5119 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5121 #define INIT___BZERO
5122 #endif // SANITIZER_INTERCEPT___BZERO
5124 #if SANITIZER_INTERCEPT_FTIME
5125 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5127 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5128 // FIXME: under ASan the call below may write to freed memory and corrupt
5129 // its metadata. See
5130 // https://github.com/google/sanitizers/issues/321.
5131 int res = REAL(ftime)(tp);
5133 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5136 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5139 #endif // SANITIZER_INTERCEPT_FTIME
5141 #if SANITIZER_INTERCEPT_XDR
5142 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5143 unsigned size, int op) {
5145 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5146 // FIXME: under ASan the call below may write to freed memory and corrupt
5147 // its metadata. See
5148 // https://github.com/google/sanitizers/issues/321.
5149 REAL(xdrmem_create)(xdrs, addr, size, op);
5150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5151 if (op == __sanitizer_XDR_ENCODE) {
5152 // It's not obvious how much data individual xdr_ routines write.
5153 // Simply unpoison the entire target buffer in advance.
5154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5158 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5160 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5161 // FIXME: under ASan the call below may write to freed memory and corrupt
5162 // its metadata. See
5163 // https://github.com/google/sanitizers/issues/321.
5164 REAL(xdrstdio_create)(xdrs, file, op);
5165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5168 // FIXME: under ASan the call below may write to freed memory and corrupt
5169 // its metadata. See
5170 // https://github.com/google/sanitizers/issues/321.
5171 #define XDR_INTERCEPTOR(F, T) \
5172 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5174 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5175 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5176 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5177 int res = REAL(F)(xdrs, p); \
5178 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5179 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5183 XDR_INTERCEPTOR(xdr_short, short)
5184 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5185 XDR_INTERCEPTOR(xdr_int, int)
5186 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5187 XDR_INTERCEPTOR(xdr_long, long)
5188 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5189 XDR_INTERCEPTOR(xdr_hyper, long long)
5190 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5191 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5192 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5193 XDR_INTERCEPTOR(xdr_int8_t, u8)
5194 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5195 XDR_INTERCEPTOR(xdr_int16_t, u16)
5196 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5197 XDR_INTERCEPTOR(xdr_int32_t, u32)
5198 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5199 XDR_INTERCEPTOR(xdr_int64_t, u64)
5200 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5201 XDR_INTERCEPTOR(xdr_quad_t, long long)
5202 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5203 XDR_INTERCEPTOR(xdr_bool, bool)
5204 XDR_INTERCEPTOR(xdr_enum, int)
5205 XDR_INTERCEPTOR(xdr_char, char)
5206 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5207 XDR_INTERCEPTOR(xdr_float, float)
5208 XDR_INTERCEPTOR(xdr_double, double)
5210 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5211 // wrapstring, sizeof
5213 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5216 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5217 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5218 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5219 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5220 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5222 // FIXME: under ASan the call below may write to freed memory and corrupt
5223 // its metadata. See
5224 // https://github.com/google/sanitizers/issues/321.
5225 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5226 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5227 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5228 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5229 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5234 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5237 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5238 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5239 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5240 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5242 // FIXME: under ASan the call below may write to freed memory and corrupt
5243 // its metadata. See
5244 // https://github.com/google/sanitizers/issues/321.
5245 int res = REAL(xdr_string)(xdrs, p, maxsize);
5246 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5255 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5256 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5257 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5258 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5259 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5260 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5261 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5262 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5263 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5264 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5265 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5266 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5267 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5268 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5269 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5270 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5271 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5272 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5273 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5274 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5275 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5276 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5277 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5278 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5279 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5280 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5281 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5282 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5283 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5284 COMMON_INTERCEPT_FUNCTION(xdr_string);
5287 #endif // SANITIZER_INTERCEPT_XDR
5289 #if SANITIZER_INTERCEPT_TSEARCH
5290 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5291 int (*compar)(const void *, const void *)) {
5293 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5294 // FIXME: under ASan the call below may write to freed memory and corrupt
5295 // its metadata. See
5296 // https://github.com/google/sanitizers/issues/321.
5297 void *res = REAL(tsearch)(key, rootp, compar);
5298 if (res && *(void **)res == key)
5299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5302 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5304 #define INIT_TSEARCH
5307 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5308 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5309 void unpoison_file(__sanitizer_FILE *fp) {
5310 #if SANITIZER_HAS_STRUCT_FILE
5311 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5312 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5313 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5314 fp->_IO_read_end - fp->_IO_read_base);
5315 #endif // SANITIZER_HAS_STRUCT_FILE
5319 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5320 // These guys are called when a .c source is built with -O2.
5321 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5323 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5324 int res = REAL(__uflow)(fp);
5328 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5330 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5331 int res = REAL(__underflow)(fp);
5335 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5337 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5338 int res = REAL(__overflow)(fp, ch);
5342 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5344 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5345 int res = REAL(__wuflow)(fp);
5349 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5351 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5352 int res = REAL(__wunderflow)(fp);
5356 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5358 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5359 int res = REAL(__woverflow)(fp, ch);
5363 #define INIT_LIBIO_INTERNALS \
5364 COMMON_INTERCEPT_FUNCTION(__uflow); \
5365 COMMON_INTERCEPT_FUNCTION(__underflow); \
5366 COMMON_INTERCEPT_FUNCTION(__overflow); \
5367 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5368 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5369 COMMON_INTERCEPT_FUNCTION(__woverflow);
5371 #define INIT_LIBIO_INTERNALS
5374 #if SANITIZER_INTERCEPT_FOPEN
5375 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5377 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5378 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5379 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5380 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5381 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5382 if (res) unpoison_file(res);
5385 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5387 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5388 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5389 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5390 if (res) unpoison_file(res);
5393 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5394 __sanitizer_FILE *fp) {
5396 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5397 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5398 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5399 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5400 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5401 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5402 if (res) unpoison_file(res);
5405 #define INIT_FOPEN \
5406 COMMON_INTERCEPT_FUNCTION(fopen); \
5407 COMMON_INTERCEPT_FUNCTION(fdopen); \
5408 COMMON_INTERCEPT_FUNCTION(freopen);
5413 #if SANITIZER_INTERCEPT_FOPEN64
5414 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5416 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5417 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5418 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5419 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5420 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5421 if (res) unpoison_file(res);
5424 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5425 __sanitizer_FILE *fp) {
5427 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5428 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5429 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5430 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5431 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5432 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5433 if (res) unpoison_file(res);
5436 #define INIT_FOPEN64 \
5437 COMMON_INTERCEPT_FUNCTION(fopen64); \
5438 COMMON_INTERCEPT_FUNCTION(freopen64);
5440 #define INIT_FOPEN64
5443 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5444 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5446 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5447 // FIXME: under ASan the call below may write to freed memory and corrupt
5448 // its metadata. See
5449 // https://github.com/google/sanitizers/issues/321.
5450 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5452 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5455 FileMetadata file = {ptr, sizeloc};
5456 SetInterceptorMetadata(res, file);
5460 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5463 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5464 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5466 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5469 FileMetadata file = {(char **)ptr, sizeloc};
5470 SetInterceptorMetadata(res, file);
5474 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5477 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5478 // FIXME: under ASan the call below may write to freed memory and corrupt
5479 // its metadata. See
5480 // https://github.com/google/sanitizers/issues/321.
5481 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5482 if (res) unpoison_file(res);
5485 #define INIT_OPEN_MEMSTREAM \
5486 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5487 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5488 COMMON_INTERCEPT_FUNCTION(fmemopen);
5490 #define INIT_OPEN_MEMSTREAM
5493 #if SANITIZER_INTERCEPT_OBSTACK
5494 static void initialize_obstack(__sanitizer_obstack *obstack) {
5495 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5497 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5498 sizeof(*obstack->chunk));
5501 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5502 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5503 void (*free_fn)(uptr arg, void *p)) {
5505 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5507 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5508 if (res) initialize_obstack(obstack);
5511 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5512 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5514 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5516 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5517 if (res) initialize_obstack(obstack);
5520 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5522 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5523 REAL(_obstack_newchunk)(obstack, length);
5525 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5526 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5528 #define INIT_OBSTACK \
5529 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5530 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5531 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5533 #define INIT_OBSTACK
5536 #if SANITIZER_INTERCEPT_FFLUSH
5537 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5539 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5540 int res = REAL(fflush)(fp);
5541 // FIXME: handle fp == NULL
5543 const FileMetadata *m = GetInterceptorMetadata(fp);
5544 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5548 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5553 #if SANITIZER_INTERCEPT_FCLOSE
5554 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5556 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5557 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5558 const FileMetadata *m = GetInterceptorMetadata(fp);
5559 int res = REAL(fclose)(fp);
5561 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5562 DeleteInterceptorMetadata(fp);
5566 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5571 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5572 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5574 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5575 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5576 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5577 void *res = REAL(dlopen)(filename, flag);
5578 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5582 INTERCEPTOR(int, dlclose, void *handle) {
5584 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5585 int res = REAL(dlclose)(handle);
5586 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5589 #define INIT_DLOPEN_DLCLOSE \
5590 COMMON_INTERCEPT_FUNCTION(dlopen); \
5591 COMMON_INTERCEPT_FUNCTION(dlclose);
5593 #define INIT_DLOPEN_DLCLOSE
5596 #if SANITIZER_INTERCEPT_GETPASS
5597 INTERCEPTOR(char *, getpass, const char *prompt) {
5599 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5601 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5602 char *res = REAL(getpass)(prompt);
5603 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5607 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5609 #define INIT_GETPASS
5612 #if SANITIZER_INTERCEPT_TIMERFD
5613 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5616 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5618 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5619 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5620 if (res != -1 && old_value)
5621 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5625 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5627 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5628 int res = REAL(timerfd_gettime)(fd, curr_value);
5629 if (res != -1 && curr_value)
5630 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5633 #define INIT_TIMERFD \
5634 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5635 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5637 #define INIT_TIMERFD
5640 #if SANITIZER_INTERCEPT_MLOCKX
5641 // Linux kernel has a bug that leads to kernel deadlock if a process
5642 // maps TBs of memory and then calls mlock().
5643 static void MlockIsUnsupported() {
5644 static atomic_uint8_t printed;
5645 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5647 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5651 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5652 MlockIsUnsupported();
5656 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5657 MlockIsUnsupported();
5661 INTERCEPTOR(int, mlockall, int flags) {
5662 MlockIsUnsupported();
5666 INTERCEPTOR(int, munlockall, void) {
5667 MlockIsUnsupported();
5671 #define INIT_MLOCKX \
5672 COMMON_INTERCEPT_FUNCTION(mlock); \
5673 COMMON_INTERCEPT_FUNCTION(munlock); \
5674 COMMON_INTERCEPT_FUNCTION(mlockall); \
5675 COMMON_INTERCEPT_FUNCTION(munlockall);
5679 #endif // SANITIZER_INTERCEPT_MLOCKX
5681 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5682 struct WrappedCookie {
5684 __sanitizer_cookie_io_functions_t real_io_funcs;
5687 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5688 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5689 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5690 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5691 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5694 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5695 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5696 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5697 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5698 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5701 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5702 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5703 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5704 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5705 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5706 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5710 static int wrapped_close(void *cookie) {
5711 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5712 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5713 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5714 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5715 InternalFree(wrapped_cookie);
5719 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5720 __sanitizer_cookie_io_functions_t io_funcs) {
5722 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5723 WrappedCookie *wrapped_cookie =
5724 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5725 wrapped_cookie->real_cookie = cookie;
5726 wrapped_cookie->real_io_funcs = io_funcs;
5727 __sanitizer_FILE *res =
5728 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5729 wrapped_seek, wrapped_close});
5733 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5735 #define INIT_FOPENCOOKIE
5736 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5738 #if SANITIZER_INTERCEPT_SEM
5739 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5741 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5742 // Workaround a bug in glibc's "old" semaphore implementation by
5743 // zero-initializing the sem_t contents. This has to be done here because
5744 // interceptors bind to the lowest symbols version by default, hitting the
5745 // buggy code path while the non-sanitized build of the same code works fine.
5746 REAL(memset)(s, 0, sizeof(*s));
5747 int res = REAL(sem_init)(s, pshared, value);
5751 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5753 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5754 int res = REAL(sem_destroy)(s);
5758 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5760 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5761 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5763 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5768 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5770 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5771 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5773 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5778 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5780 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5781 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5782 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5784 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5789 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5791 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5792 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5793 int res = REAL(sem_post)(s);
5797 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5799 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5800 int res = REAL(sem_getvalue)(s, sval);
5802 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5808 COMMON_INTERCEPT_FUNCTION(sem_init); \
5809 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5810 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5811 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5812 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5813 COMMON_INTERCEPT_FUNCTION(sem_post); \
5814 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5817 #endif // SANITIZER_INTERCEPT_SEM
5819 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5820 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5822 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5823 int res = REAL(pthread_setcancelstate)(state, oldstate);
5825 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5829 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5831 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5832 int res = REAL(pthread_setcanceltype)(type, oldtype);
5834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5837 #define INIT_PTHREAD_SETCANCEL \
5838 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5839 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5841 #define INIT_PTHREAD_SETCANCEL
5844 #if SANITIZER_INTERCEPT_MINCORE
5845 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5847 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5848 int res = REAL(mincore)(addr, length, vec);
5850 uptr page_size = GetPageSizeCached();
5851 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5856 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5858 #define INIT_MINCORE
5861 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5862 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5863 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5866 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5867 remote_iov, riovcnt, flags);
5868 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5871 write_iovec(ctx, local_iov, liovcnt, res);
5875 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5876 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5879 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5880 remote_iov, riovcnt, flags);
5881 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5884 read_iovec(ctx, local_iov, liovcnt, res);
5887 #define INIT_PROCESS_VM_READV \
5888 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5889 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5891 #define INIT_PROCESS_VM_READV
5894 #if SANITIZER_INTERCEPT_CTERMID
5895 INTERCEPTOR(char *, ctermid, char *s) {
5897 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5898 char *res = REAL(ctermid)(s);
5900 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5904 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5906 #define INIT_CTERMID
5909 #if SANITIZER_INTERCEPT_CTERMID_R
5910 INTERCEPTOR(char *, ctermid_r, char *s) {
5912 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5913 char *res = REAL(ctermid_r)(s);
5915 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5919 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5921 #define INIT_CTERMID_R
5924 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5925 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5927 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5928 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5929 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5933 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5937 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5938 void *srcaddr, int *addrlen) {
5940 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5942 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5944 if (srcaddr) srcaddr_sz = *addrlen;
5945 (void)srcaddr_sz; // prevent "set but not used" warning
5946 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5950 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5951 Min((SIZE_T)*addrlen, srcaddr_sz));
5955 #define INIT_RECV_RECVFROM \
5956 COMMON_INTERCEPT_FUNCTION(recv); \
5957 COMMON_INTERCEPT_FUNCTION(recvfrom);
5959 #define INIT_RECV_RECVFROM
5962 #if SANITIZER_INTERCEPT_SEND_SENDTO
5963 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5965 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5967 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5968 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5970 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5971 if (common_flags()->intercept_send && res > 0)
5972 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5976 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5977 void *dstaddr, int addrlen) {
5979 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5981 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5982 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5984 // Can't check dstaddr as it may have uninitialized padding at the end.
5985 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5986 if (common_flags()->intercept_send && res > 0)
5987 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5990 #define INIT_SEND_SENDTO \
5991 COMMON_INTERCEPT_FUNCTION(send); \
5992 COMMON_INTERCEPT_FUNCTION(sendto);
5994 #define INIT_SEND_SENDTO
5997 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5998 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6000 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6001 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6002 int res = REAL(eventfd_read)(fd, value);
6004 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6005 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6009 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6011 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6013 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6014 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6016 int res = REAL(eventfd_write)(fd, value);
6019 #define INIT_EVENTFD_READ_WRITE \
6020 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6021 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6023 #define INIT_EVENTFD_READ_WRITE
6026 #if SANITIZER_INTERCEPT_STAT
6027 INTERCEPTOR(int, stat, const char *path, void *buf) {
6029 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6030 if (common_flags()->intercept_stat)
6031 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6032 int res = REAL(stat)(path, buf);
6034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6037 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6042 #if SANITIZER_INTERCEPT___XSTAT
6043 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6045 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6046 if (common_flags()->intercept_stat)
6047 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6048 int res = REAL(__xstat)(version, path, buf);
6050 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6053 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6055 #define INIT___XSTAT
6058 #if SANITIZER_INTERCEPT___XSTAT64
6059 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6061 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6062 if (common_flags()->intercept_stat)
6063 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6064 int res = REAL(__xstat64)(version, path, buf);
6066 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6069 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6071 #define INIT___XSTAT64
6074 #if SANITIZER_INTERCEPT___LXSTAT
6075 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6077 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6078 if (common_flags()->intercept_stat)
6079 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6080 int res = REAL(__lxstat)(version, path, buf);
6082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6085 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6087 #define INIT___LXSTAT
6090 #if SANITIZER_INTERCEPT___LXSTAT64
6091 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6093 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6094 if (common_flags()->intercept_stat)
6095 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6096 int res = REAL(__lxstat64)(version, path, buf);
6098 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6101 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6103 #define INIT___LXSTAT64
6106 // FIXME: add other *stat interceptor
6108 #if SANITIZER_INTERCEPT_UTMP
6109 INTERCEPTOR(void *, getutent, int dummy) {
6111 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6112 void *res = REAL(getutent)(dummy);
6114 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6117 INTERCEPTOR(void *, getutid, void *ut) {
6119 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6120 void *res = REAL(getutid)(ut);
6122 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6125 INTERCEPTOR(void *, getutline, void *ut) {
6127 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6128 void *res = REAL(getutline)(ut);
6130 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6134 COMMON_INTERCEPT_FUNCTION(getutent); \
6135 COMMON_INTERCEPT_FUNCTION(getutid); \
6136 COMMON_INTERCEPT_FUNCTION(getutline);
6141 #if SANITIZER_INTERCEPT_UTMPX
6142 INTERCEPTOR(void *, getutxent, int dummy) {
6144 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6145 void *res = REAL(getutxent)(dummy);
6147 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6150 INTERCEPTOR(void *, getutxid, void *ut) {
6152 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6153 void *res = REAL(getutxid)(ut);
6155 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6158 INTERCEPTOR(void *, getutxline, void *ut) {
6160 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6161 void *res = REAL(getutxline)(ut);
6163 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6166 #define INIT_UTMPX \
6167 COMMON_INTERCEPT_FUNCTION(getutxent); \
6168 COMMON_INTERCEPT_FUNCTION(getutxid); \
6169 COMMON_INTERCEPT_FUNCTION(getutxline);
6174 #if SANITIZER_INTERCEPT_GETLOADAVG
6175 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6177 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6178 int res = REAL(getloadavg)(loadavg, nelem);
6180 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6183 #define INIT_GETLOADAVG \
6184 COMMON_INTERCEPT_FUNCTION(getloadavg);
6186 #define INIT_GETLOADAVG
6189 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6190 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6194 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6198 INTERCEPTOR(int, mprobe, void *ptr) {
6203 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6205 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6206 SIZE_T res = REAL(wcslen)(s);
6207 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6211 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6213 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6214 SIZE_T res = REAL(wcsnlen)(s, n);
6215 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6218 #define INIT_WCSLEN \
6219 COMMON_INTERCEPT_FUNCTION(wcslen); \
6220 COMMON_INTERCEPT_FUNCTION(wcsnlen);
6222 #if SANITIZER_INTERCEPT_WCSCAT
6223 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6225 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6226 SIZE_T src_size = REAL(wcslen)(src);
6227 SIZE_T dst_size = REAL(wcslen)(dst);
6228 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6229 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6231 (src_size + 1) * sizeof(wchar_t));
6232 return REAL(wcscat)(dst, src); // NOLINT
6235 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6237 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6238 SIZE_T src_size = REAL(wcsnlen)(src, n);
6239 SIZE_T dst_size = REAL(wcslen)(dst);
6240 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6241 Min(src_size + 1, n) * sizeof(wchar_t));
6242 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6244 (src_size + 1) * sizeof(wchar_t));
6245 return REAL(wcsncat)(dst, src, n); // NOLINT
6247 #define INIT_WCSCAT \
6248 COMMON_INTERCEPT_FUNCTION(wcscat); \
6249 COMMON_INTERCEPT_FUNCTION(wcsncat);
6254 static void InitializeCommonInterceptors() {
6255 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
6256 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
6297 INIT_LOCALTIME_AND_FRIENDS;
6306 INIT_GETPWNAM_AND_FRIENDS;
6307 INIT_GETPWNAM_R_AND_FRIENDS;
6319 INIT_PTHREAD_GETSCHEDPARAM;
6324 INIT_GETHOSTBYNAME_R;
6325 INIT_GETHOSTBYNAME2_R;
6326 INIT_GETHOSTBYADDR_R;
6343 INIT_GET_CURRENT_DIR_NAME;
6352 INIT_CANONICALIZE_FILE_NAME;
6354 INIT_SCHED_GETAFFINITY;
6355 INIT_SCHED_GETPARAM;
6358 INIT_XPG_STRERROR_R;
6373 INIT_PTHREAD_MUTEX_LOCK;
6374 INIT_PTHREAD_MUTEX_UNLOCK;
6382 INIT_ETHER_NTOA_ATON;
6387 INIT_PTHREAD_ATTR_GET;
6388 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6389 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6390 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6391 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6392 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6393 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6394 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6395 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6396 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6397 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6398 INIT_PTHREAD_CONDATTR_GETPSHARED;
6399 INIT_PTHREAD_CONDATTR_GETCLOCK;
6400 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6405 INIT_PTHREAD_SETNAME_NP;
6421 INIT_IF_INDEXTONAME;
6428 INIT_LIBIO_INTERNALS;
6431 INIT_OPEN_MEMSTREAM;
6435 INIT_DLOPEN_DLCLOSE;
6441 INIT_PTHREAD_SETCANCEL;
6443 INIT_PROCESS_VM_READV;
6449 INIT_EVENTFD_READ_WRITE;
6454 // FIXME: add other *stat interceptors.