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_placement_new.h"
44 #include "sanitizer_platform_interceptors.h"
45 #include "sanitizer_tls_get_addr.h"
49 #if SANITIZER_INTERCEPTOR_HOOKS
50 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
51 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
52 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
54 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
55 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
57 #endif // SANITIZER_INTERCEPTOR_HOOKS
59 #if SANITIZER_WINDOWS && !defined(va_copy)
60 #define va_copy(dst, src) ((dst) = (src))
64 #define pthread_setname_np pthread_set_name_np
65 #define inet_aton __inet_aton
66 #define inet_pton __inet_pton
67 #define iconv __bsd_iconv
70 // Platform-specific options.
72 namespace __sanitizer {
73 bool PlatformHasDifferentMemcpyAndMemmove();
75 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
76 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
77 #elif SANITIZER_WINDOWS64
78 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
80 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
81 #endif // SANITIZER_MAC
83 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
84 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
87 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
88 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
91 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
92 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
95 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
96 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
99 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
100 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
103 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
104 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
107 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
108 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
111 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
112 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
115 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
116 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
119 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
120 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
123 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
124 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
127 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
128 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
131 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
132 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
135 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
136 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
137 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
140 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
141 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
144 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
145 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
146 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
148 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
149 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
150 CheckNoDeepBind(filename, flag);
153 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
154 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
157 #ifndef COMMON_INTERCEPTOR_ACQUIRE
158 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
161 #ifndef COMMON_INTERCEPTOR_RELEASE
162 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
165 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
166 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
169 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
170 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
173 #ifdef SANITIZER_NLDBL_VERSION
174 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
175 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
177 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
178 COMMON_INTERCEPT_FUNCTION(fn)
181 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
182 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
184 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
185 return internal_memset(dst, v, size); \
186 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
187 if (common_flags()->intercept_intrin) \
188 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
189 return REAL(memset)(dst, v, size); \
193 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
194 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
196 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
197 return internal_memmove(dst, src, size); \
198 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
199 if (common_flags()->intercept_intrin) { \
200 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
201 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
203 return REAL(memmove)(dst, src, size); \
207 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
208 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
210 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
211 return internal_memmove(dst, src, size); \
213 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
214 if (common_flags()->intercept_intrin) { \
215 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
216 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
218 return REAL(memcpy)(dst, src, size); \
222 #ifndef COMMON_INTERCEPTOR_COPY_STRING
223 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
226 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
227 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
228 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
229 uptr from_length = internal_strnlen(s, size); \
230 uptr copy_length = Min(size, from_length); \
231 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
232 if (common_flags()->intercept_strndup) { \
233 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, 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);
3399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3402 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3404 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3406 #define INIT_STRERROR_R
3409 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3410 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3412 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3413 // FIXME: under ASan the call below may write to freed memory and corrupt
3414 // its metadata. See
3415 // https://github.com/google/sanitizers/issues/321.
3416 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3417 // This version always returns a null-terminated string.
3419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3422 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3424 #define INIT_XPG_STRERROR_R
3427 #if SANITIZER_INTERCEPT_SCANDIR
3428 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3429 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3430 const struct __sanitizer_dirent **);
3432 static THREADLOCAL scandir_filter_f scandir_filter;
3433 static THREADLOCAL scandir_compar_f scandir_compar;
3435 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3436 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3437 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3438 return scandir_filter(dir);
3441 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3442 const struct __sanitizer_dirent **b) {
3443 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3444 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3445 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3446 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3447 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3448 return scandir_compar(a, b);
3451 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3452 scandir_filter_f filter, scandir_compar_f compar) {
3454 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3455 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3456 scandir_filter = filter;
3457 scandir_compar = compar;
3458 // FIXME: under ASan the call below may write to freed memory and corrupt
3459 // its metadata. See
3460 // https://github.com/google/sanitizers/issues/321.
3461 int res = REAL(scandir)(dirp, namelist,
3462 filter ? wrapped_scandir_filter : nullptr,
3463 compar ? wrapped_scandir_compar : nullptr);
3464 scandir_filter = nullptr;
3465 scandir_compar = nullptr;
3466 if (namelist && res > 0) {
3467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3468 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3469 for (int i = 0; i < res; ++i)
3470 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3471 (*namelist)[i]->d_reclen);
3475 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3477 #define INIT_SCANDIR
3480 #if SANITIZER_INTERCEPT_SCANDIR64
3481 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3482 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3483 const struct __sanitizer_dirent64 **);
3485 static THREADLOCAL scandir64_filter_f scandir64_filter;
3486 static THREADLOCAL scandir64_compar_f scandir64_compar;
3488 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3489 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3490 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3491 return scandir64_filter(dir);
3494 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3495 const struct __sanitizer_dirent64 **b) {
3496 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3497 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3498 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3499 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3500 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3501 return scandir64_compar(a, b);
3504 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3505 scandir64_filter_f filter, scandir64_compar_f compar) {
3507 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3508 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3509 scandir64_filter = filter;
3510 scandir64_compar = compar;
3511 // FIXME: under ASan the call below may write to freed memory and corrupt
3512 // its metadata. See
3513 // https://github.com/google/sanitizers/issues/321.
3515 REAL(scandir64)(dirp, namelist,
3516 filter ? wrapped_scandir64_filter : nullptr,
3517 compar ? wrapped_scandir64_compar : nullptr);
3518 scandir64_filter = nullptr;
3519 scandir64_compar = nullptr;
3520 if (namelist && res > 0) {
3521 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3523 for (int i = 0; i < res; ++i)
3524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3525 (*namelist)[i]->d_reclen);
3529 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3531 #define INIT_SCANDIR64
3534 #if SANITIZER_INTERCEPT_GETGROUPS
3535 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3537 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3538 // FIXME: under ASan the call below may write to freed memory and corrupt
3539 // its metadata. See
3540 // https://github.com/google/sanitizers/issues/321.
3541 int res = REAL(getgroups)(size, lst);
3542 if (res >= 0 && lst && size > 0)
3543 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3546 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3548 #define INIT_GETGROUPS
3551 #if SANITIZER_INTERCEPT_POLL
3552 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3553 __sanitizer_nfds_t nfds) {
3554 for (unsigned i = 0; i < nfds; ++i) {
3555 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3556 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3560 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3561 __sanitizer_nfds_t nfds) {
3562 for (unsigned i = 0; i < nfds; ++i)
3563 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3564 sizeof(fds[i].revents));
3567 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3570 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3571 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3572 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3573 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3576 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3581 #if SANITIZER_INTERCEPT_PPOLL
3582 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3583 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3585 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3586 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3588 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3589 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3591 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3592 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3595 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3600 #if SANITIZER_INTERCEPT_WORDEXP
3601 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3603 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3604 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3605 // FIXME: under ASan the call below may write to freed memory and corrupt
3606 // its metadata. See
3607 // https://github.com/google/sanitizers/issues/321.
3608 int res = REAL(wordexp)(s, p, flags);
3610 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3612 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3613 sizeof(*p->we_wordv) * p->we_wordc);
3614 for (uptr i = 0; i < p->we_wordc; ++i) {
3615 char *w = p->we_wordv[i];
3616 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3621 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3623 #define INIT_WORDEXP
3626 #if SANITIZER_INTERCEPT_SIGWAIT
3627 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3629 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3630 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3631 // FIXME: under ASan the call below may write to freed memory and corrupt
3632 // its metadata. See
3633 // https://github.com/google/sanitizers/issues/321.
3634 int res = REAL(sigwait)(set, sig);
3635 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3638 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3640 #define INIT_SIGWAIT
3643 #if SANITIZER_INTERCEPT_SIGWAITINFO
3644 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3646 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3647 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3648 // FIXME: under ASan the call below may write to freed memory and corrupt
3649 // its metadata. See
3650 // https://github.com/google/sanitizers/issues/321.
3651 int res = REAL(sigwaitinfo)(set, info);
3652 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3655 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3657 #define INIT_SIGWAITINFO
3660 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3661 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3664 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3665 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3666 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3667 // FIXME: under ASan the call below may write to freed memory and corrupt
3668 // its metadata. See
3669 // https://github.com/google/sanitizers/issues/321.
3670 int res = REAL(sigtimedwait)(set, info, timeout);
3671 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3674 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3676 #define INIT_SIGTIMEDWAIT
3679 #if SANITIZER_INTERCEPT_SIGSETOPS
3680 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3682 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3683 // FIXME: under ASan the call below may write to freed memory and corrupt
3684 // its metadata. See
3685 // https://github.com/google/sanitizers/issues/321.
3686 int res = REAL(sigemptyset)(set);
3687 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3691 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3693 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3694 // FIXME: under ASan the call below may write to freed memory and corrupt
3695 // its metadata. See
3696 // https://github.com/google/sanitizers/issues/321.
3697 int res = REAL(sigfillset)(set);
3698 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3701 #define INIT_SIGSETOPS \
3702 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3703 COMMON_INTERCEPT_FUNCTION(sigfillset);
3705 #define INIT_SIGSETOPS
3708 #if SANITIZER_INTERCEPT_SIGPENDING
3709 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3711 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3712 // FIXME: under ASan the call below may write to freed memory and corrupt
3713 // its metadata. See
3714 // https://github.com/google/sanitizers/issues/321.
3715 int res = REAL(sigpending)(set);
3716 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3719 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3721 #define INIT_SIGPENDING
3724 #if SANITIZER_INTERCEPT_SIGPROCMASK
3725 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3726 __sanitizer_sigset_t *oldset) {
3728 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3729 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3730 // FIXME: under ASan the call below may write to freed memory and corrupt
3731 // its metadata. See
3732 // https://github.com/google/sanitizers/issues/321.
3733 int res = REAL(sigprocmask)(how, set, oldset);
3735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3738 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3740 #define INIT_SIGPROCMASK
3743 #if SANITIZER_INTERCEPT_BACKTRACE
3744 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3746 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3747 // FIXME: under ASan the call below may write to freed memory and corrupt
3748 // its metadata. See
3749 // https://github.com/google/sanitizers/issues/321.
3750 int res = REAL(backtrace)(buffer, size);
3752 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3756 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3758 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3760 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3761 // FIXME: under ASan the call below may write to freed memory and corrupt
3762 // its metadata. See
3763 // https://github.com/google/sanitizers/issues/321.
3764 char **res = REAL(backtrace_symbols)(buffer, size);
3766 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3767 for (int i = 0; i < size; ++i)
3768 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3772 #define INIT_BACKTRACE \
3773 COMMON_INTERCEPT_FUNCTION(backtrace); \
3774 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3776 #define INIT_BACKTRACE
3779 #if SANITIZER_INTERCEPT__EXIT
3780 INTERCEPTOR(void, _exit, int status) {
3782 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3783 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3784 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3785 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3786 if (status == 0) status = status1;
3787 REAL(_exit)(status);
3789 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3794 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3795 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3797 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3798 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
3799 int res = REAL(pthread_mutex_lock)(m);
3800 if (res == errno_EOWNERDEAD)
3801 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3802 if (res == 0 || res == errno_EOWNERDEAD)
3803 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
3804 if (res == errno_EINVAL)
3805 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3809 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3811 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3812 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3813 int res = REAL(pthread_mutex_unlock)(m);
3814 if (res == errno_EINVAL)
3815 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3819 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3820 #define INIT_PTHREAD_MUTEX_UNLOCK \
3821 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3823 #define INIT_PTHREAD_MUTEX_LOCK
3824 #define INIT_PTHREAD_MUTEX_UNLOCK
3827 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3828 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3829 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3830 if (mnt->mnt_fsname)
3831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3832 REAL(strlen)(mnt->mnt_fsname) + 1);
3834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3835 REAL(strlen)(mnt->mnt_dir) + 1);
3837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3838 REAL(strlen)(mnt->mnt_type) + 1);
3840 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3841 REAL(strlen)(mnt->mnt_opts) + 1);
3845 #if SANITIZER_INTERCEPT_GETMNTENT
3846 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3848 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3849 __sanitizer_mntent *res = REAL(getmntent)(fp);
3850 if (res) write_mntent(ctx, res);
3853 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3855 #define INIT_GETMNTENT
3858 #if SANITIZER_INTERCEPT_GETMNTENT_R
3859 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3860 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3862 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3863 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3864 if (res) write_mntent(ctx, res);
3867 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3869 #define INIT_GETMNTENT_R
3872 #if SANITIZER_INTERCEPT_STATFS
3873 INTERCEPTOR(int, statfs, char *path, void *buf) {
3875 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3876 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3877 // FIXME: under ASan the call below may write to freed memory and corrupt
3878 // its metadata. See
3879 // https://github.com/google/sanitizers/issues/321.
3880 int res = REAL(statfs)(path, buf);
3881 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3884 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3886 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3887 // FIXME: under ASan the call below may write to freed memory and corrupt
3888 // its metadata. See
3889 // https://github.com/google/sanitizers/issues/321.
3890 int res = REAL(fstatfs)(fd, buf);
3891 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3894 #define INIT_STATFS \
3895 COMMON_INTERCEPT_FUNCTION(statfs); \
3896 COMMON_INTERCEPT_FUNCTION(fstatfs);
3901 #if SANITIZER_INTERCEPT_STATFS64
3902 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3904 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3905 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3906 // FIXME: under ASan the call below may write to freed memory and corrupt
3907 // its metadata. See
3908 // https://github.com/google/sanitizers/issues/321.
3909 int res = REAL(statfs64)(path, buf);
3910 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3913 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3915 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3916 // FIXME: under ASan the call below may write to freed memory and corrupt
3917 // its metadata. See
3918 // https://github.com/google/sanitizers/issues/321.
3919 int res = REAL(fstatfs64)(fd, buf);
3920 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3923 #define INIT_STATFS64 \
3924 COMMON_INTERCEPT_FUNCTION(statfs64); \
3925 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3927 #define INIT_STATFS64
3930 #if SANITIZER_INTERCEPT_STATVFS
3931 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3933 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3934 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3935 // FIXME: under ASan the call below may write to freed memory and corrupt
3936 // its metadata. See
3937 // https://github.com/google/sanitizers/issues/321.
3938 int res = REAL(statvfs)(path, buf);
3939 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3942 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3944 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3945 // FIXME: under ASan the call below may write to freed memory and corrupt
3946 // its metadata. See
3947 // https://github.com/google/sanitizers/issues/321.
3948 int res = REAL(fstatvfs)(fd, buf);
3949 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3952 #define INIT_STATVFS \
3953 COMMON_INTERCEPT_FUNCTION(statvfs); \
3954 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3956 #define INIT_STATVFS
3959 #if SANITIZER_INTERCEPT_STATVFS64
3960 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3962 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3963 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3964 // FIXME: under ASan the call below may write to freed memory and corrupt
3965 // its metadata. See
3966 // https://github.com/google/sanitizers/issues/321.
3967 int res = REAL(statvfs64)(path, buf);
3968 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3971 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3973 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3974 // FIXME: under ASan the call below may write to freed memory and corrupt
3975 // its metadata. See
3976 // https://github.com/google/sanitizers/issues/321.
3977 int res = REAL(fstatvfs64)(fd, buf);
3978 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3981 #define INIT_STATVFS64 \
3982 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3983 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3985 #define INIT_STATVFS64
3988 #if SANITIZER_INTERCEPT_INITGROUPS
3989 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3991 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3992 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3993 int res = REAL(initgroups)(user, group);
3996 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3998 #define INIT_INITGROUPS
4001 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4002 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4004 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4005 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4006 char *res = REAL(ether_ntoa)(addr);
4007 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4010 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4012 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4013 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4014 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4015 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4018 #define INIT_ETHER_NTOA_ATON \
4019 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4020 COMMON_INTERCEPT_FUNCTION(ether_aton);
4022 #define INIT_ETHER_NTOA_ATON
4025 #if SANITIZER_INTERCEPT_ETHER_HOST
4026 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4028 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4029 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4030 // FIXME: under ASan the call below may write to freed memory and corrupt
4031 // its metadata. See
4032 // https://github.com/google/sanitizers/issues/321.
4033 int res = REAL(ether_ntohost)(hostname, addr);
4034 if (!res && hostname)
4035 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4038 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4040 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4042 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4043 // FIXME: under ASan the call below may write to freed memory and corrupt
4044 // its metadata. See
4045 // https://github.com/google/sanitizers/issues/321.
4046 int res = REAL(ether_hostton)(hostname, addr);
4047 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4050 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4053 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4054 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4055 // FIXME: under ASan the call below may write to freed memory and corrupt
4056 // its metadata. See
4057 // https://github.com/google/sanitizers/issues/321.
4058 int res = REAL(ether_line)(line, addr, hostname);
4060 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4062 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4066 #define INIT_ETHER_HOST \
4067 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4068 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4069 COMMON_INTERCEPT_FUNCTION(ether_line);
4071 #define INIT_ETHER_HOST
4074 #if SANITIZER_INTERCEPT_ETHER_R
4075 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4077 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4078 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4079 // FIXME: under ASan the call below may write to freed memory and corrupt
4080 // its metadata. See
4081 // https://github.com/google/sanitizers/issues/321.
4082 char *res = REAL(ether_ntoa_r)(addr, buf);
4083 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4086 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4087 __sanitizer_ether_addr *addr) {
4089 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4090 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4091 // FIXME: under ASan the call below may write to freed memory and corrupt
4092 // its metadata. See
4093 // https://github.com/google/sanitizers/issues/321.
4094 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4095 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4098 #define INIT_ETHER_R \
4099 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4100 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4102 #define INIT_ETHER_R
4105 #if SANITIZER_INTERCEPT_SHMCTL
4106 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4108 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4109 // FIXME: under ASan the call below may write to freed memory and corrupt
4110 // its metadata. See
4111 // https://github.com/google/sanitizers/issues/321.
4112 int res = REAL(shmctl)(shmid, cmd, buf);
4115 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4116 sz = sizeof(__sanitizer_shmid_ds);
4117 else if (cmd == shmctl_ipc_info)
4118 sz = struct_shminfo_sz;
4119 else if (cmd == shmctl_shm_info)
4120 sz = struct_shm_info_sz;
4121 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4125 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4130 #if SANITIZER_INTERCEPT_RANDOM_R
4131 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4133 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4134 // FIXME: under ASan the call below may write to freed memory and corrupt
4135 // its metadata. See
4136 // https://github.com/google/sanitizers/issues/321.
4137 int res = REAL(random_r)(buf, result);
4139 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4142 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4144 #define INIT_RANDOM_R
4147 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4148 // its metadata. See
4149 // https://github.com/google/sanitizers/issues/321.
4150 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4151 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4152 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4153 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4154 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4155 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4156 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4157 INTERCEPTOR(int, fn, void *attr, void *r) { \
4159 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4160 int res = REAL(fn)(attr, r); \
4161 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4164 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4165 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4166 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4167 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4168 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4169 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4170 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4171 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4172 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4173 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4176 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4177 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4178 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4179 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4180 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4181 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4182 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4183 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4185 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4186 // FIXME: under ASan the call below may write to freed memory and corrupt
4187 // its metadata. See
4188 // https://github.com/google/sanitizers/issues/321.
4189 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4191 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4192 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4197 // We may need to call the real pthread_attr_getstack from the run-time
4198 // in sanitizer_common, but we don't want to include the interception headers
4199 // there. So, just define this function here.
4200 namespace __sanitizer {
4202 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4203 return REAL(pthread_attr_getstack)(attr, addr, size);
4206 } // namespace __sanitizer
4208 #define INIT_PTHREAD_ATTR_GET \
4209 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4210 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4211 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4212 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4213 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4214 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4215 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4217 #define INIT_PTHREAD_ATTR_GET
4220 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4221 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4223 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4224 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4226 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4229 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4230 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4233 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4235 // FIXME: under ASan the call below may write to freed memory and corrupt
4236 // its metadata. See
4237 // https://github.com/google/sanitizers/issues/321.
4238 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4239 if (!res && cpusetsize && cpuset)
4240 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4244 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4245 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4247 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4250 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4251 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4252 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4253 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4255 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4258 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4259 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4260 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4261 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4263 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4266 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4267 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4268 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4269 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4271 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4274 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4275 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4276 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4277 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4279 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4282 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4283 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4284 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4285 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4287 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4290 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4291 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4292 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4293 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4295 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4298 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4299 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4300 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4301 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4303 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4306 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4307 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4308 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4309 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4311 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4314 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4315 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4316 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4317 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4319 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4322 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4323 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4324 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4325 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4327 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4330 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4331 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4332 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4333 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4335 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4338 #if SANITIZER_INTERCEPT_TMPNAM
4339 INTERCEPTOR(char *, tmpnam, char *s) {
4341 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4342 char *res = REAL(tmpnam)(s);
4345 // FIXME: under ASan the call below may write to freed memory and corrupt
4346 // its metadata. See
4347 // https://github.com/google/sanitizers/issues/321.
4348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4350 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4354 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4359 #if SANITIZER_INTERCEPT_TMPNAM_R
4360 INTERCEPTOR(char *, tmpnam_r, char *s) {
4362 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4363 // FIXME: under ASan the call below may write to freed memory and corrupt
4364 // its metadata. See
4365 // https://github.com/google/sanitizers/issues/321.
4366 char *res = REAL(tmpnam_r)(s);
4367 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4370 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4372 #define INIT_TMPNAM_R
4375 #if SANITIZER_INTERCEPT_TTYNAME_R
4376 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4378 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4379 int res = REAL(ttyname_r)(fd, name, namesize);
4381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4384 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4386 #define INIT_TTYNAME_R
4389 #if SANITIZER_INTERCEPT_TEMPNAM
4390 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4392 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4393 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4394 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4395 char *res = REAL(tempnam)(dir, pfx);
4396 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4399 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4401 #define INIT_TEMPNAM
4404 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4405 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4407 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4408 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4409 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4410 return REAL(pthread_setname_np)(thread, name);
4412 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4414 #define INIT_PTHREAD_SETNAME_NP
4417 #if SANITIZER_INTERCEPT_SINCOS
4418 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4420 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4421 // FIXME: under ASan the call below may write to freed memory and corrupt
4422 // its metadata. See
4423 // https://github.com/google/sanitizers/issues/321.
4424 REAL(sincos)(x, sin, cos);
4425 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4426 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4428 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4430 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4431 // FIXME: under ASan the call below may write to freed memory and corrupt
4432 // its metadata. See
4433 // https://github.com/google/sanitizers/issues/321.
4434 REAL(sincosf)(x, sin, cos);
4435 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4436 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4438 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4440 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4441 // FIXME: under ASan the call below may write to freed memory and corrupt
4442 // its metadata. See
4443 // https://github.com/google/sanitizers/issues/321.
4444 REAL(sincosl)(x, sin, cos);
4445 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4446 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4448 #define INIT_SINCOS \
4449 COMMON_INTERCEPT_FUNCTION(sincos); \
4450 COMMON_INTERCEPT_FUNCTION(sincosf); \
4451 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4456 #if SANITIZER_INTERCEPT_REMQUO
4457 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4459 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4460 // FIXME: under ASan the call below may write to freed memory and corrupt
4461 // its metadata. See
4462 // https://github.com/google/sanitizers/issues/321.
4463 double res = REAL(remquo)(x, y, quo);
4464 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4467 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4469 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4470 // FIXME: under ASan the call below may write to freed memory and corrupt
4471 // its metadata. See
4472 // https://github.com/google/sanitizers/issues/321.
4473 float res = REAL(remquof)(x, y, quo);
4474 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4477 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4479 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4480 // FIXME: under ASan the call below may write to freed memory and corrupt
4481 // its metadata. See
4482 // https://github.com/google/sanitizers/issues/321.
4483 long double res = REAL(remquol)(x, y, quo);
4484 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4487 #define INIT_REMQUO \
4488 COMMON_INTERCEPT_FUNCTION(remquo); \
4489 COMMON_INTERCEPT_FUNCTION(remquof); \
4490 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4495 #if SANITIZER_INTERCEPT_LGAMMA
4497 INTERCEPTOR(double, lgamma, double x) {
4499 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4500 double res = REAL(lgamma)(x);
4501 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4504 INTERCEPTOR(float, lgammaf, float x) {
4506 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4507 float res = REAL(lgammaf)(x);
4508 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4511 INTERCEPTOR(long double, lgammal, long double x) {
4513 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4514 long double res = REAL(lgammal)(x);
4515 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4518 #define INIT_LGAMMA \
4519 COMMON_INTERCEPT_FUNCTION(lgamma); \
4520 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4521 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4526 #if SANITIZER_INTERCEPT_LGAMMA_R
4527 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4529 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4530 // FIXME: under ASan the call below may write to freed memory and corrupt
4531 // its metadata. See
4532 // https://github.com/google/sanitizers/issues/321.
4533 double res = REAL(lgamma_r)(x, signp);
4534 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4537 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4539 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4540 // FIXME: under ASan the call below may write to freed memory and corrupt
4541 // its metadata. See
4542 // https://github.com/google/sanitizers/issues/321.
4543 float res = REAL(lgammaf_r)(x, signp);
4544 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4547 #define INIT_LGAMMA_R \
4548 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4549 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4551 #define INIT_LGAMMA_R
4554 #if SANITIZER_INTERCEPT_LGAMMAL_R
4555 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4557 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4558 // FIXME: under ASan the call below may write to freed memory and corrupt
4559 // its metadata. See
4560 // https://github.com/google/sanitizers/issues/321.
4561 long double res = REAL(lgammal_r)(x, signp);
4562 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4565 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4567 #define INIT_LGAMMAL_R
4570 #if SANITIZER_INTERCEPT_DRAND48_R
4571 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4573 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4574 // FIXME: under ASan the call below may write to freed memory and corrupt
4575 // its metadata. See
4576 // https://github.com/google/sanitizers/issues/321.
4577 int res = REAL(drand48_r)(buffer, result);
4578 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4581 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4583 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4584 // FIXME: under ASan the call below may write to freed memory and corrupt
4585 // its metadata. See
4586 // https://github.com/google/sanitizers/issues/321.
4587 int res = REAL(lrand48_r)(buffer, result);
4588 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4591 #define INIT_DRAND48_R \
4592 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4593 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4595 #define INIT_DRAND48_R
4598 #if SANITIZER_INTERCEPT_RAND_R
4599 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4601 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4602 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4603 return REAL(rand_r)(seedp);
4605 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4610 #if SANITIZER_INTERCEPT_GETLINE
4611 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4613 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4614 // FIXME: under ASan the call below may write to freed memory and corrupt
4615 // its metadata. See
4616 // https://github.com/google/sanitizers/issues/321.
4617 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4619 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4620 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4621 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4626 // FIXME: under ASan the call below may write to freed memory and corrupt its
4628 // https://github.com/google/sanitizers/issues/321.
4629 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4632 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4633 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4635 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4636 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4642 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4644 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4646 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4647 // with its own body.
4648 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4650 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4652 #define INIT_GETLINE \
4653 COMMON_INTERCEPT_FUNCTION(getline); \
4654 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4655 COMMON_INTERCEPT_FUNCTION(getdelim);
4657 #define INIT_GETLINE
4660 #if SANITIZER_INTERCEPT_ICONV
4661 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4662 char **outbuf, SIZE_T *outbytesleft) {
4664 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4667 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4668 if (inbuf && inbytesleft)
4669 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4671 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4672 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4673 // FIXME: under ASan the call below may write to freed memory and corrupt
4674 // its metadata. See
4675 // https://github.com/google/sanitizers/issues/321.
4676 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4677 if (outbuf && *outbuf > outbuf_orig) {
4678 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4683 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4688 #if SANITIZER_INTERCEPT_TIMES
4689 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4691 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4692 // FIXME: under ASan the call below may write to freed memory and corrupt
4693 // its metadata. See
4694 // https://github.com/google/sanitizers/issues/321.
4695 __sanitizer_clock_t res = REAL(times)(tms);
4696 if (res != (__sanitizer_clock_t)-1 && tms)
4697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4700 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4705 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4707 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4708 // If you see any crashes around this functions, there are 2 known issues with
4709 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4710 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4711 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4712 // to access thread local variables (it should not happen normally,
4713 // because sanitizers use initial-exec tls model).
4714 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4716 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4717 void *res = REAL(__tls_get_addr)(arg);
4718 uptr tls_begin, tls_end;
4719 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4720 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4722 // New DTLS block has been allocated.
4723 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4728 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4729 // mostly the same semantics as __tls_get_addr, but its presence enables
4730 // some optimizations in linker (which are safe to ignore here).
4731 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4732 visibility("default")))
4733 void *__tls_get_addr_opt(void *arg);
4735 #else // SANITIZER_S390
4736 // On s390, we have to intercept two functions here:
4737 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4738 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4739 // a proper pointer. It is used by dlsym for TLS symbols.
4740 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4741 // descriptor offset as an argument instead of a pointer. GOT address
4742 // is passed in r12, so it's necessary to write it in assembly. This is
4743 // the function used by the compiler.
4744 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
4745 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
4746 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
4747 extern "C" uptr __tls_get_offset(void *arg);
4748 extern "C" uptr __interceptor___tls_get_offset(void *arg);
4749 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4751 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4752 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
4753 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4754 void *ptr = reinterpret_cast<void *>(res + tp);
4755 uptr tls_begin, tls_end;
4756 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4757 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4759 // New DTLS block has been allocated.
4760 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4764 // We need a hidden symbol aliasing the above, so that we can jump
4765 // directly to it from the assembly below.
4766 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4767 visibility("hidden")))
4768 uptr __tls_get_addr_hidden(void *arg);
4769 // Now carefully intercept __tls_get_offset.
4772 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4773 // exports our symbol.
4774 ".weak __tls_get_offset\n"
4775 ".type __tls_get_offset, @function\n"
4776 "__tls_get_offset:\n"
4777 ".global __interceptor___tls_get_offset\n"
4778 ".type __interceptor___tls_get_offset, @function\n"
4779 "__interceptor___tls_get_offset:\n"
4781 "la %r2, 0(%r2,%r12)\n"
4782 "jg __tls_get_addr_hidden\n"
4785 "0: la %r2,0(%r2,%r12)\n"
4786 "l %r4,1f-0b(%r3)\n"
4788 "1: .long __tls_get_addr_hidden - 0b\n"
4790 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
4791 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
4792 ".type __tls_get_offset_wrapper, @function\n"
4793 "__tls_get_offset_wrapper:\n"
4800 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
4802 #endif // SANITIZER_S390
4804 #define INIT_TLS_GET_ADDR
4807 #if SANITIZER_INTERCEPT_LISTXATTR
4808 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4810 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4811 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4812 // FIXME: under ASan the call below may write to freed memory and corrupt
4813 // its metadata. See
4814 // https://github.com/google/sanitizers/issues/321.
4815 SSIZE_T res = REAL(listxattr)(path, list, size);
4816 // Here and below, size == 0 is a special case where nothing is written to the
4817 // buffer, and res contains the desired buffer size.
4818 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4821 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4823 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4824 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4825 // FIXME: under ASan the call below may write to freed memory and corrupt
4826 // its metadata. See
4827 // https://github.com/google/sanitizers/issues/321.
4828 SSIZE_T res = REAL(llistxattr)(path, list, size);
4829 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4832 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4834 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4835 // FIXME: under ASan the call below may write to freed memory and corrupt
4836 // its metadata. See
4837 // https://github.com/google/sanitizers/issues/321.
4838 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4839 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4842 #define INIT_LISTXATTR \
4843 COMMON_INTERCEPT_FUNCTION(listxattr); \
4844 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4845 COMMON_INTERCEPT_FUNCTION(flistxattr);
4847 #define INIT_LISTXATTR
4850 #if SANITIZER_INTERCEPT_GETXATTR
4851 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4854 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4855 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4856 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4857 // FIXME: under ASan the call below may write to freed memory and corrupt
4858 // its metadata. See
4859 // https://github.com/google/sanitizers/issues/321.
4860 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4861 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4864 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4867 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4868 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4869 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4870 // FIXME: under ASan the call below may write to freed memory and corrupt
4871 // its metadata. See
4872 // https://github.com/google/sanitizers/issues/321.
4873 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4874 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4877 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4880 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4881 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4882 // FIXME: under ASan the call below may write to freed memory and corrupt
4883 // its metadata. See
4884 // https://github.com/google/sanitizers/issues/321.
4885 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4886 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4889 #define INIT_GETXATTR \
4890 COMMON_INTERCEPT_FUNCTION(getxattr); \
4891 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4892 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4894 #define INIT_GETXATTR
4897 #if SANITIZER_INTERCEPT_GETRESID
4898 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4900 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4901 // FIXME: under ASan the call below may write to freed memory and corrupt
4902 // its metadata. See
4903 // https://github.com/google/sanitizers/issues/321.
4904 int res = REAL(getresuid)(ruid, euid, suid);
4906 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4907 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4908 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4912 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4914 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4915 // FIXME: under ASan the call below may write to freed memory and corrupt
4916 // its metadata. See
4917 // https://github.com/google/sanitizers/issues/321.
4918 int res = REAL(getresgid)(rgid, egid, sgid);
4920 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4921 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4922 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4926 #define INIT_GETRESID \
4927 COMMON_INTERCEPT_FUNCTION(getresuid); \
4928 COMMON_INTERCEPT_FUNCTION(getresgid);
4930 #define INIT_GETRESID
4933 #if SANITIZER_INTERCEPT_GETIFADDRS
4934 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4935 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4936 // intercept it to poison the memory again.
4937 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4939 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4940 // FIXME: under ASan the call below may write to freed memory and corrupt
4941 // its metadata. See
4942 // https://github.com/google/sanitizers/issues/321.
4943 int res = REAL(getifaddrs)(ifap);
4944 if (res == 0 && ifap) {
4945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4946 __sanitizer_ifaddrs *p = *ifap;
4948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4951 REAL(strlen)(p->ifa_name) + 1);
4953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4956 // On Linux this is a union, but the other member also points to a
4957 // struct sockaddr, so the following is sufficient.
4959 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4960 // FIXME(smatveev): Unpoison p->ifa_data as well.
4966 #define INIT_GETIFADDRS \
4967 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4969 #define INIT_GETIFADDRS
4972 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4973 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4975 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4976 // FIXME: under ASan the call below may write to freed memory and corrupt
4977 // its metadata. See
4978 // https://github.com/google/sanitizers/issues/321.
4979 char *res = REAL(if_indextoname)(ifindex, ifname);
4981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4984 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4986 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4988 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4989 return REAL(if_nametoindex)(ifname);
4991 #define INIT_IF_INDEXTONAME \
4992 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4993 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4995 #define INIT_IF_INDEXTONAME
4998 #if SANITIZER_INTERCEPT_CAPGET
4999 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5001 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5003 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5004 // FIXME: under ASan the call below may write to freed memory and corrupt
5005 // its metadata. See
5006 // https://github.com/google/sanitizers/issues/321.
5007 int res = REAL(capget)(hdrp, datap);
5008 if (res == 0 && datap)
5009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5010 // We can also return -1 and write to hdrp->version if the version passed in
5011 // hdrp->version is unsupported. But that's not a trivial condition to check,
5012 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5015 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5017 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5019 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5021 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5022 return REAL(capset)(hdrp, datap);
5024 #define INIT_CAPGET \
5025 COMMON_INTERCEPT_FUNCTION(capget); \
5026 COMMON_INTERCEPT_FUNCTION(capset);
5031 #if SANITIZER_INTERCEPT_AEABI_MEM
5032 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5034 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5037 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5039 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5042 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5044 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5047 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5049 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5052 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5054 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5057 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5059 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5062 // Note the argument order.
5063 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5065 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5068 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5070 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5073 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5075 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5078 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5080 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5083 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5085 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5088 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5090 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5093 #define INIT_AEABI_MEM \
5094 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5095 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5096 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5097 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5098 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5099 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5100 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5101 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5102 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5103 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5104 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5105 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5107 #define INIT_AEABI_MEM
5108 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5110 #if SANITIZER_INTERCEPT___BZERO
5111 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5113 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5116 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5118 #define INIT___BZERO
5119 #endif // SANITIZER_INTERCEPT___BZERO
5121 #if SANITIZER_INTERCEPT_FTIME
5122 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5124 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5125 // FIXME: under ASan the call below may write to freed memory and corrupt
5126 // its metadata. See
5127 // https://github.com/google/sanitizers/issues/321.
5128 int res = REAL(ftime)(tp);
5130 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5133 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5136 #endif // SANITIZER_INTERCEPT_FTIME
5138 #if SANITIZER_INTERCEPT_XDR
5139 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5140 unsigned size, int op) {
5142 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5143 // FIXME: under ASan the call below may write to freed memory and corrupt
5144 // its metadata. See
5145 // https://github.com/google/sanitizers/issues/321.
5146 REAL(xdrmem_create)(xdrs, addr, size, op);
5147 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5148 if (op == __sanitizer_XDR_ENCODE) {
5149 // It's not obvious how much data individual xdr_ routines write.
5150 // Simply unpoison the entire target buffer in advance.
5151 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5155 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5157 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5158 // FIXME: under ASan the call below may write to freed memory and corrupt
5159 // its metadata. See
5160 // https://github.com/google/sanitizers/issues/321.
5161 REAL(xdrstdio_create)(xdrs, file, op);
5162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5165 // FIXME: under ASan the call below may write to freed memory and corrupt
5166 // its metadata. See
5167 // https://github.com/google/sanitizers/issues/321.
5168 #define XDR_INTERCEPTOR(F, T) \
5169 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5171 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5172 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5173 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5174 int res = REAL(F)(xdrs, p); \
5175 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5180 XDR_INTERCEPTOR(xdr_short, short)
5181 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5182 XDR_INTERCEPTOR(xdr_int, int)
5183 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5184 XDR_INTERCEPTOR(xdr_long, long)
5185 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5186 XDR_INTERCEPTOR(xdr_hyper, long long)
5187 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5188 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5189 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5190 XDR_INTERCEPTOR(xdr_int8_t, u8)
5191 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5192 XDR_INTERCEPTOR(xdr_int16_t, u16)
5193 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5194 XDR_INTERCEPTOR(xdr_int32_t, u32)
5195 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5196 XDR_INTERCEPTOR(xdr_int64_t, u64)
5197 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5198 XDR_INTERCEPTOR(xdr_quad_t, long long)
5199 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5200 XDR_INTERCEPTOR(xdr_bool, bool)
5201 XDR_INTERCEPTOR(xdr_enum, int)
5202 XDR_INTERCEPTOR(xdr_char, char)
5203 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5204 XDR_INTERCEPTOR(xdr_float, float)
5205 XDR_INTERCEPTOR(xdr_double, double)
5207 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5208 // wrapstring, sizeof
5210 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5213 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5214 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5215 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5216 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5217 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5219 // FIXME: under ASan the call below may write to freed memory and corrupt
5220 // its metadata. See
5221 // https://github.com/google/sanitizers/issues/321.
5222 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5223 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5225 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5226 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5231 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5234 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5235 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5236 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5237 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5239 // FIXME: under ASan the call below may write to freed memory and corrupt
5240 // its metadata. See
5241 // https://github.com/google/sanitizers/issues/321.
5242 int res = REAL(xdr_string)(xdrs, p, maxsize);
5243 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5252 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5253 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5254 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5255 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5256 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5257 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5258 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5259 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5260 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5261 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5262 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5263 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5264 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5265 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5266 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5267 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5268 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5269 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5270 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5271 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5272 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5273 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5274 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5275 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5276 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5277 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5278 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5279 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5280 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5281 COMMON_INTERCEPT_FUNCTION(xdr_string);
5284 #endif // SANITIZER_INTERCEPT_XDR
5286 #if SANITIZER_INTERCEPT_TSEARCH
5287 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5288 int (*compar)(const void *, const void *)) {
5290 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5291 // FIXME: under ASan the call below may write to freed memory and corrupt
5292 // its metadata. See
5293 // https://github.com/google/sanitizers/issues/321.
5294 void *res = REAL(tsearch)(key, rootp, compar);
5295 if (res && *(void **)res == key)
5296 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5299 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5301 #define INIT_TSEARCH
5304 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5305 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5306 void unpoison_file(__sanitizer_FILE *fp) {
5307 #if SANITIZER_HAS_STRUCT_FILE
5308 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5309 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5310 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5311 fp->_IO_read_end - fp->_IO_read_base);
5312 #endif // SANITIZER_HAS_STRUCT_FILE
5316 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5317 // These guys are called when a .c source is built with -O2.
5318 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5320 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5321 int res = REAL(__uflow)(fp);
5325 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5327 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5328 int res = REAL(__underflow)(fp);
5332 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5334 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5335 int res = REAL(__overflow)(fp, ch);
5339 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5341 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5342 int res = REAL(__wuflow)(fp);
5346 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5348 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5349 int res = REAL(__wunderflow)(fp);
5353 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5355 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5356 int res = REAL(__woverflow)(fp, ch);
5360 #define INIT_LIBIO_INTERNALS \
5361 COMMON_INTERCEPT_FUNCTION(__uflow); \
5362 COMMON_INTERCEPT_FUNCTION(__underflow); \
5363 COMMON_INTERCEPT_FUNCTION(__overflow); \
5364 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5365 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5366 COMMON_INTERCEPT_FUNCTION(__woverflow);
5368 #define INIT_LIBIO_INTERNALS
5371 #if SANITIZER_INTERCEPT_FOPEN
5372 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5374 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5375 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5376 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5377 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5378 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5379 if (res) unpoison_file(res);
5382 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5384 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5385 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5386 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5387 if (res) unpoison_file(res);
5390 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5391 __sanitizer_FILE *fp) {
5393 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5394 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5395 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5396 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5397 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5398 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5399 if (res) unpoison_file(res);
5402 #define INIT_FOPEN \
5403 COMMON_INTERCEPT_FUNCTION(fopen); \
5404 COMMON_INTERCEPT_FUNCTION(fdopen); \
5405 COMMON_INTERCEPT_FUNCTION(freopen);
5410 #if SANITIZER_INTERCEPT_FOPEN64
5411 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5413 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5414 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5415 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5416 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5417 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5418 if (res) unpoison_file(res);
5421 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5422 __sanitizer_FILE *fp) {
5424 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5425 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5426 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5427 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5428 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5429 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5430 if (res) unpoison_file(res);
5433 #define INIT_FOPEN64 \
5434 COMMON_INTERCEPT_FUNCTION(fopen64); \
5435 COMMON_INTERCEPT_FUNCTION(freopen64);
5437 #define INIT_FOPEN64
5440 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5441 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5443 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5444 // FIXME: under ASan the call below may write to freed memory and corrupt
5445 // its metadata. See
5446 // https://github.com/google/sanitizers/issues/321.
5447 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5449 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5450 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5452 FileMetadata file = {ptr, sizeloc};
5453 SetInterceptorMetadata(res, file);
5457 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5460 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5461 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5463 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5466 FileMetadata file = {(char **)ptr, sizeloc};
5467 SetInterceptorMetadata(res, file);
5471 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5474 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5475 // FIXME: under ASan the call below may write to freed memory and corrupt
5476 // its metadata. See
5477 // https://github.com/google/sanitizers/issues/321.
5478 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5479 if (res) unpoison_file(res);
5482 #define INIT_OPEN_MEMSTREAM \
5483 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5484 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5485 COMMON_INTERCEPT_FUNCTION(fmemopen);
5487 #define INIT_OPEN_MEMSTREAM
5490 #if SANITIZER_INTERCEPT_OBSTACK
5491 static void initialize_obstack(__sanitizer_obstack *obstack) {
5492 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5494 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5495 sizeof(*obstack->chunk));
5498 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5499 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5500 void (*free_fn)(uptr arg, void *p)) {
5502 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5504 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5505 if (res) initialize_obstack(obstack);
5508 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5509 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5511 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5513 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5514 if (res) initialize_obstack(obstack);
5517 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5519 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5520 REAL(_obstack_newchunk)(obstack, length);
5522 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5523 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5525 #define INIT_OBSTACK \
5526 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5527 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5528 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5530 #define INIT_OBSTACK
5533 #if SANITIZER_INTERCEPT_FFLUSH
5534 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5536 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5537 int res = REAL(fflush)(fp);
5538 // FIXME: handle fp == NULL
5540 const FileMetadata *m = GetInterceptorMetadata(fp);
5541 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5545 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5550 #if SANITIZER_INTERCEPT_FCLOSE
5551 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5553 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5554 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5555 const FileMetadata *m = GetInterceptorMetadata(fp);
5556 int res = REAL(fclose)(fp);
5558 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5559 DeleteInterceptorMetadata(fp);
5563 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5568 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5569 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5571 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5572 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5573 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5574 void *res = REAL(dlopen)(filename, flag);
5575 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5579 INTERCEPTOR(int, dlclose, void *handle) {
5581 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5582 int res = REAL(dlclose)(handle);
5583 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5586 #define INIT_DLOPEN_DLCLOSE \
5587 COMMON_INTERCEPT_FUNCTION(dlopen); \
5588 COMMON_INTERCEPT_FUNCTION(dlclose);
5590 #define INIT_DLOPEN_DLCLOSE
5593 #if SANITIZER_INTERCEPT_GETPASS
5594 INTERCEPTOR(char *, getpass, const char *prompt) {
5596 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5598 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5599 char *res = REAL(getpass)(prompt);
5600 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5604 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5606 #define INIT_GETPASS
5609 #if SANITIZER_INTERCEPT_TIMERFD
5610 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5613 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5615 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5616 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5617 if (res != -1 && old_value)
5618 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5622 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5624 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5625 int res = REAL(timerfd_gettime)(fd, curr_value);
5626 if (res != -1 && curr_value)
5627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5630 #define INIT_TIMERFD \
5631 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5632 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5634 #define INIT_TIMERFD
5637 #if SANITIZER_INTERCEPT_MLOCKX
5638 // Linux kernel has a bug that leads to kernel deadlock if a process
5639 // maps TBs of memory and then calls mlock().
5640 static void MlockIsUnsupported() {
5641 static atomic_uint8_t printed;
5642 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5644 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5648 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5649 MlockIsUnsupported();
5653 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5654 MlockIsUnsupported();
5658 INTERCEPTOR(int, mlockall, int flags) {
5659 MlockIsUnsupported();
5663 INTERCEPTOR(int, munlockall, void) {
5664 MlockIsUnsupported();
5668 #define INIT_MLOCKX \
5669 COMMON_INTERCEPT_FUNCTION(mlock); \
5670 COMMON_INTERCEPT_FUNCTION(munlock); \
5671 COMMON_INTERCEPT_FUNCTION(mlockall); \
5672 COMMON_INTERCEPT_FUNCTION(munlockall);
5676 #endif // SANITIZER_INTERCEPT_MLOCKX
5678 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5679 struct WrappedCookie {
5681 __sanitizer_cookie_io_functions_t real_io_funcs;
5684 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5685 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5686 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5687 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5688 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5691 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5692 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5693 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5694 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5695 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5698 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5699 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5700 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5701 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5702 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5703 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5707 static int wrapped_close(void *cookie) {
5708 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5709 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5710 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5711 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5712 InternalFree(wrapped_cookie);
5716 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5717 __sanitizer_cookie_io_functions_t io_funcs) {
5719 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5720 WrappedCookie *wrapped_cookie =
5721 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5722 wrapped_cookie->real_cookie = cookie;
5723 wrapped_cookie->real_io_funcs = io_funcs;
5724 __sanitizer_FILE *res =
5725 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5726 wrapped_seek, wrapped_close});
5730 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5732 #define INIT_FOPENCOOKIE
5733 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5735 #if SANITIZER_INTERCEPT_SEM
5736 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5738 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5739 // Workaround a bug in glibc's "old" semaphore implementation by
5740 // zero-initializing the sem_t contents. This has to be done here because
5741 // interceptors bind to the lowest symbols version by default, hitting the
5742 // buggy code path while the non-sanitized build of the same code works fine.
5743 REAL(memset)(s, 0, sizeof(*s));
5744 int res = REAL(sem_init)(s, pshared, value);
5748 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5750 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5751 int res = REAL(sem_destroy)(s);
5755 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5757 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5758 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5760 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5765 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5767 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5768 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5770 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5775 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5777 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5778 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5779 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5781 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5786 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5788 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5789 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5790 int res = REAL(sem_post)(s);
5794 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5796 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5797 int res = REAL(sem_getvalue)(s, sval);
5799 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5800 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5805 COMMON_INTERCEPT_FUNCTION(sem_init); \
5806 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5807 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5808 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5809 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5810 COMMON_INTERCEPT_FUNCTION(sem_post); \
5811 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5814 #endif // SANITIZER_INTERCEPT_SEM
5816 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5817 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5819 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5820 int res = REAL(pthread_setcancelstate)(state, oldstate);
5822 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5826 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5828 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5829 int res = REAL(pthread_setcanceltype)(type, oldtype);
5831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5834 #define INIT_PTHREAD_SETCANCEL \
5835 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5836 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5838 #define INIT_PTHREAD_SETCANCEL
5841 #if SANITIZER_INTERCEPT_MINCORE
5842 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5844 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5845 int res = REAL(mincore)(addr, length, vec);
5847 uptr page_size = GetPageSizeCached();
5848 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5849 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5853 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5855 #define INIT_MINCORE
5858 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5859 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5860 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5863 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5864 remote_iov, riovcnt, flags);
5865 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5868 write_iovec(ctx, local_iov, liovcnt, res);
5872 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5873 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5876 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5877 remote_iov, riovcnt, flags);
5878 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5881 read_iovec(ctx, local_iov, liovcnt, res);
5884 #define INIT_PROCESS_VM_READV \
5885 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5886 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5888 #define INIT_PROCESS_VM_READV
5891 #if SANITIZER_INTERCEPT_CTERMID
5892 INTERCEPTOR(char *, ctermid, char *s) {
5894 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5895 char *res = REAL(ctermid)(s);
5897 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5901 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5903 #define INIT_CTERMID
5906 #if SANITIZER_INTERCEPT_CTERMID_R
5907 INTERCEPTOR(char *, ctermid_r, char *s) {
5909 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5910 char *res = REAL(ctermid_r)(s);
5912 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5916 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5918 #define INIT_CTERMID_R
5921 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5922 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5924 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5925 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5926 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5930 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5934 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5935 void *srcaddr, int *addrlen) {
5937 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5939 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5941 if (srcaddr) srcaddr_sz = *addrlen;
5942 (void)srcaddr_sz; // prevent "set but not used" warning
5943 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5947 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5948 Min((SIZE_T)*addrlen, srcaddr_sz));
5952 #define INIT_RECV_RECVFROM \
5953 COMMON_INTERCEPT_FUNCTION(recv); \
5954 COMMON_INTERCEPT_FUNCTION(recvfrom);
5956 #define INIT_RECV_RECVFROM
5959 #if SANITIZER_INTERCEPT_SEND_SENDTO
5960 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5962 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5964 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5965 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5967 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5968 if (common_flags()->intercept_send && res > 0)
5969 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5973 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5974 void *dstaddr, int addrlen) {
5976 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5978 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5979 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5981 // Can't check dstaddr as it may have uninitialized padding at the end.
5982 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5983 if (common_flags()->intercept_send && res > 0)
5984 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5987 #define INIT_SEND_SENDTO \
5988 COMMON_INTERCEPT_FUNCTION(send); \
5989 COMMON_INTERCEPT_FUNCTION(sendto);
5991 #define INIT_SEND_SENDTO
5994 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5995 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5997 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5998 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5999 int res = REAL(eventfd_read)(fd, value);
6001 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6002 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6006 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6008 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6010 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6011 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6013 int res = REAL(eventfd_write)(fd, value);
6016 #define INIT_EVENTFD_READ_WRITE \
6017 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6018 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6020 #define INIT_EVENTFD_READ_WRITE
6023 #if SANITIZER_INTERCEPT_STAT
6024 INTERCEPTOR(int, stat, const char *path, void *buf) {
6026 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6027 if (common_flags()->intercept_stat)
6028 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6029 int res = REAL(stat)(path, buf);
6031 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6034 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6039 #if SANITIZER_INTERCEPT___XSTAT
6040 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6042 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6043 if (common_flags()->intercept_stat)
6044 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6045 int res = REAL(__xstat)(version, path, buf);
6047 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6050 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6052 #define INIT___XSTAT
6055 #if SANITIZER_INTERCEPT___XSTAT64
6056 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6058 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6059 if (common_flags()->intercept_stat)
6060 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6061 int res = REAL(__xstat64)(version, path, buf);
6063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6066 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6068 #define INIT___XSTAT64
6071 #if SANITIZER_INTERCEPT___LXSTAT
6072 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6074 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6075 if (common_flags()->intercept_stat)
6076 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6077 int res = REAL(__lxstat)(version, path, buf);
6079 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6082 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6084 #define INIT___LXSTAT
6087 #if SANITIZER_INTERCEPT___LXSTAT64
6088 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6090 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6091 if (common_flags()->intercept_stat)
6092 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6093 int res = REAL(__lxstat64)(version, path, buf);
6095 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6098 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6100 #define INIT___LXSTAT64
6103 // FIXME: add other *stat interceptor
6105 #if SANITIZER_INTERCEPT_UTMP
6106 INTERCEPTOR(void *, getutent, int dummy) {
6108 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6109 void *res = REAL(getutent)(dummy);
6111 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6114 INTERCEPTOR(void *, getutid, void *ut) {
6116 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6117 void *res = REAL(getutid)(ut);
6119 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6122 INTERCEPTOR(void *, getutline, void *ut) {
6124 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6125 void *res = REAL(getutline)(ut);
6127 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6131 COMMON_INTERCEPT_FUNCTION(getutent); \
6132 COMMON_INTERCEPT_FUNCTION(getutid); \
6133 COMMON_INTERCEPT_FUNCTION(getutline);
6138 #if SANITIZER_INTERCEPT_UTMPX
6139 INTERCEPTOR(void *, getutxent, int dummy) {
6141 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6142 void *res = REAL(getutxent)(dummy);
6144 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6147 INTERCEPTOR(void *, getutxid, void *ut) {
6149 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6150 void *res = REAL(getutxid)(ut);
6152 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6155 INTERCEPTOR(void *, getutxline, void *ut) {
6157 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6158 void *res = REAL(getutxline)(ut);
6160 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6163 #define INIT_UTMPX \
6164 COMMON_INTERCEPT_FUNCTION(getutxent); \
6165 COMMON_INTERCEPT_FUNCTION(getutxid); \
6166 COMMON_INTERCEPT_FUNCTION(getutxline);
6171 #if SANITIZER_INTERCEPT_GETLOADAVG
6172 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6174 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6175 int res = REAL(getloadavg)(loadavg, nelem);
6177 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6180 #define INIT_GETLOADAVG \
6181 COMMON_INTERCEPT_FUNCTION(getloadavg);
6183 #define INIT_GETLOADAVG
6186 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6187 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6191 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6195 INTERCEPTOR(int, mprobe, void *ptr) {
6200 static void InitializeCommonInterceptors() {
6201 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
6202 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
6243 INIT_LOCALTIME_AND_FRIENDS;
6252 INIT_GETPWNAM_AND_FRIENDS;
6253 INIT_GETPWNAM_R_AND_FRIENDS;
6265 INIT_PTHREAD_GETSCHEDPARAM;
6270 INIT_GETHOSTBYNAME_R;
6271 INIT_GETHOSTBYNAME2_R;
6272 INIT_GETHOSTBYADDR_R;
6289 INIT_GET_CURRENT_DIR_NAME;
6298 INIT_CANONICALIZE_FILE_NAME;
6300 INIT_SCHED_GETAFFINITY;
6301 INIT_SCHED_GETPARAM;
6304 INIT_XPG_STRERROR_R;
6319 INIT_PTHREAD_MUTEX_LOCK;
6320 INIT_PTHREAD_MUTEX_UNLOCK;
6328 INIT_ETHER_NTOA_ATON;
6333 INIT_PTHREAD_ATTR_GET;
6334 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6335 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6336 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6337 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6338 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6339 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6340 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6341 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6342 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6343 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6344 INIT_PTHREAD_CONDATTR_GETPSHARED;
6345 INIT_PTHREAD_CONDATTR_GETCLOCK;
6346 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6351 INIT_PTHREAD_SETNAME_NP;
6367 INIT_IF_INDEXTONAME;
6374 INIT_LIBIO_INTERNALS;
6377 INIT_OPEN_MEMSTREAM;
6381 INIT_DLOPEN_DLCLOSE;
6387 INIT_PTHREAD_SETCANCEL;
6389 INIT_PROCESS_VM_READV;
6395 INIT_EVENTFD_READ_WRITE;
6400 // FIXME: add other *stat interceptors.