1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Common function interceptors for tools like AddressSanitizer,
9 // ThreadSanitizer, MemorySanitizer, etc.
11 // This file should be included into the tool's interceptor file,
12 // which has to define its own macros:
13 // COMMON_INTERCEPTOR_ENTER
14 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
15 // COMMON_INTERCEPTOR_READ_RANGE
16 // COMMON_INTERCEPTOR_WRITE_RANGE
17 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
18 // COMMON_INTERCEPTOR_DIR_ACQUIRE
19 // COMMON_INTERCEPTOR_FD_ACQUIRE
20 // COMMON_INTERCEPTOR_FD_RELEASE
21 // COMMON_INTERCEPTOR_FD_ACCESS
22 // COMMON_INTERCEPTOR_SET_THREAD_NAME
23 // COMMON_INTERCEPTOR_ON_DLOPEN
24 // COMMON_INTERCEPTOR_ON_EXIT
25 // COMMON_INTERCEPTOR_MUTEX_LOCK
26 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
27 // COMMON_INTERCEPTOR_MUTEX_REPAIR
28 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
29 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
30 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
31 //===----------------------------------------------------------------------===//
33 #include "interception/interception.h"
34 #include "sanitizer_addrhashmap.h"
35 #include "sanitizer_placement_new.h"
36 #include "sanitizer_platform_interceptors.h"
37 #include "sanitizer_tls_get_addr.h"
41 #if SANITIZER_INTERCEPTOR_HOOKS
42 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) \
47 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
49 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__); \
52 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
55 #endif // SANITIZER_INTERCEPTOR_HOOKS
57 #if SANITIZER_WINDOWS && !defined(va_copy)
58 #define va_copy(dst, src) ((dst) = (src))
62 #define pthread_setname_np pthread_set_name_np
63 #define inet_aton __inet_aton
64 #define inet_pton __inet_pton
65 #define iconv __bsd_iconv
68 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
69 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
72 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
73 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
76 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
77 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
80 #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
81 #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
84 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
85 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
88 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
89 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
92 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
93 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
96 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
97 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
100 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
101 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
104 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
105 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
108 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
109 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
112 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
113 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
116 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
117 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
118 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
121 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
122 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
125 #define COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n) \
126 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
127 common_flags()->strict_string_checks ? (len) + 1 : (n) )
129 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
130 COMMON_INTERCEPTOR_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n))
132 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
133 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) {}
136 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
137 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
140 #ifndef COMMON_INTERCEPTOR_ACQUIRE
141 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
144 #ifndef COMMON_INTERCEPTOR_RELEASE
145 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
148 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
149 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
152 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
153 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
156 #ifdef SANITIZER_NLDBL_VERSION
157 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
158 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
160 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
161 COMMON_INTERCEPT_FUNCTION(fn)
164 struct FileMetadata {
165 // For open_memstream().
170 struct CommonInterceptorMetadata {
180 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
182 static MetadataHashMap *interceptor_metadata_map;
185 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
186 const FileMetadata &file) {
187 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
189 h->type = CommonInterceptorMetadata::CIMT_FILE;
193 UNUSED static const FileMetadata *GetInterceptorMetadata(
194 __sanitizer_FILE *addr) {
195 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
200 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
207 UNUSED static void DeleteInterceptorMetadata(void *addr) {
208 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
211 #endif // SI_NOT_WINDOWS
213 #if SANITIZER_INTERCEPT_STRLEN
214 INTERCEPTOR(SIZE_T, strlen, const char *s) {
215 // Sometimes strlen is called prior to InitializeCommonInterceptors,
216 // in which case the REAL(strlen) typically used in
217 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
219 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
220 return internal_strlen(s);
222 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
223 SIZE_T result = REAL(strlen)(s);
224 if (common_flags()->intercept_strlen)
225 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
228 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
233 #if SANITIZER_INTERCEPT_STRNLEN
234 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
236 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
237 SIZE_T length = REAL(strnlen)(s, maxlen);
238 if (common_flags()->intercept_strlen)
239 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
242 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
247 #if SANITIZER_INTERCEPT_TEXTDOMAIN
248 INTERCEPTOR(char*, textdomain, const char *domainname) {
250 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
251 COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
252 char *domain = REAL(textdomain)(domainname);
254 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
258 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
260 #define INIT_TEXTDOMAIN
263 #if SANITIZER_INTERCEPT_STRCMP
264 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
265 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
268 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
269 const char *s1, const char *s2, int result)
271 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
273 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
274 unsigned char c1, c2;
277 c1 = (unsigned char)s1[i];
278 c2 = (unsigned char)s2[i];
279 if (c1 != c2 || c1 == '\0') break;
281 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
282 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
283 int result = CharCmpX(c1, c2);
284 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
289 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
290 const char *s1, const char *s2, uptr n,
293 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
294 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
295 return internal_strncmp(s1, s2, size);
297 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
298 unsigned char c1 = 0, c2 = 0;
300 for (i = 0; i < size; i++) {
301 c1 = (unsigned char)s1[i];
302 c2 = (unsigned char)s2[i];
303 if (c1 != c2 || c1 == '\0') break;
305 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, Min(i + 1, size));
306 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, Min(i + 1, size));
307 int result = CharCmpX(c1, c2);
308 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
313 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
314 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
320 #if SANITIZER_INTERCEPT_STRCASECMP
321 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
322 int c1_low = ToLower(c1);
323 int c2_low = ToLower(c2);
324 return c1_low - c2_low;
327 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
328 const char *s1, const char *s2, int result)
330 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
332 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
333 unsigned char c1 = 0, c2 = 0;
336 c1 = (unsigned char)s1[i];
337 c2 = (unsigned char)s2[i];
338 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
340 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
341 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
342 int result = CharCaseCmp(c1, c2);
343 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
348 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
349 const char *s1, const char *s2, uptr n,
352 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
354 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
355 unsigned char c1 = 0, c2 = 0;
357 for (i = 0; i < n; i++) {
358 c1 = (unsigned char)s1[i];
359 c2 = (unsigned char)s2[i];
360 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
362 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
363 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
364 int result = CharCaseCmp(c1, c2);
365 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
370 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
371 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
373 #define INIT_STRCASECMP
374 #define INIT_STRNCASECMP
377 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
378 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
380 uptr len1 = REAL(strlen)(s1);
381 uptr len2 = REAL(strlen)(s2);
382 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s1, len1,
383 r ? r - s1 + len2 : len1 + 1);
384 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
388 #if SANITIZER_INTERCEPT_STRSTR
390 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
391 const char *s1, const char *s2, char *result)
393 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
394 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
395 return internal_strstr(s1, s2);
397 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
398 char *r = REAL(strstr)(s1, s2);
399 if (common_flags()->intercept_strstr)
400 StrstrCheck(ctx, r, s1, s2);
401 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
406 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
411 #if SANITIZER_INTERCEPT_STRCASESTR
413 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
414 const char *s1, const char *s2, char *result)
416 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
418 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
419 char *r = REAL(strcasestr)(s1, s2);
420 if (common_flags()->intercept_strstr)
421 StrstrCheck(ctx, r, s1, s2);
422 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
427 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
429 #define INIT_STRCASESTR
432 #if SANITIZER_INTERCEPT_MEMMEM
433 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
434 const void *s1, SIZE_T len1, const void *s2,
435 SIZE_T len2, void *result)
437 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
440 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
441 void *r = REAL(memmem)(s1, len1, s2, len2);
442 if (common_flags()->intercept_memmem) {
443 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
444 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
446 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
447 s1, len1, s2, len2, r);
451 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
454 #endif // SANITIZER_INTERCEPT_MEMMEM
456 #if SANITIZER_INTERCEPT_STRCHR
457 INTERCEPTOR(char*, strchr, const char *s, int c) {
459 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
460 return internal_strchr(s, c);
461 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
462 char *result = REAL(strchr)(s, c);
463 uptr len = internal_strlen(s);
464 uptr n = result ? result - s + 1 : len + 1;
465 if (common_flags()->intercept_strchr)
466 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, n);
469 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
474 #if SANITIZER_INTERCEPT_STRCHRNUL
475 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
477 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
478 char *result = REAL(strchrnul)(s, c);
479 uptr len = result - s + 1;
480 if (common_flags()->intercept_strchr)
481 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
484 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
486 #define INIT_STRCHRNUL
489 #if SANITIZER_INTERCEPT_STRRCHR
490 INTERCEPTOR(char*, strrchr, const char *s, int c) {
492 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
493 return internal_strrchr(s, c);
494 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
495 uptr len = internal_strlen(s);
496 if (common_flags()->intercept_strchr)
497 COMMON_INTERCEPTOR_READ_STRING_OF_LEN(ctx, s, len, len + 1);
498 return REAL(strrchr)(s, c);
500 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
505 #if SANITIZER_INTERCEPT_STRSPN
506 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
508 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
509 SIZE_T r = REAL(strspn)(s1, s2);
510 if (common_flags()->intercept_strspn) {
511 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
512 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
517 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
519 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
520 SIZE_T r = REAL(strcspn)(s1, s2);
521 if (common_flags()->intercept_strspn) {
522 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
523 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
528 #define INIT_STRSPN \
529 COMMON_INTERCEPT_FUNCTION(strspn); \
530 COMMON_INTERCEPT_FUNCTION(strcspn);
535 #if SANITIZER_INTERCEPT_STRPBRK
536 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
538 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
539 char *r = REAL(strpbrk)(s1, s2);
540 if (common_flags()->intercept_strpbrk) {
541 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
542 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
543 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
548 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
553 #if SANITIZER_INTERCEPT_MEMSET
554 INTERCEPTOR(void*, memset, void *dst, int v, uptr size) {
555 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
556 return internal_memset(dst, v, size);
558 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size);
559 if (common_flags()->intercept_intrin)
560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
561 return REAL(memset)(dst, v, size);
564 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
569 #if SANITIZER_INTERCEPT_MEMMOVE
570 INTERCEPTOR(void*, memmove, void *dst, const void *src, uptr size) {
571 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
572 return internal_memmove(dst, src, size);
574 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size);
575 if (common_flags()->intercept_intrin) {
576 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
577 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);
579 return REAL(memmove)(dst, src, size);
582 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
587 #if SANITIZER_INTERCEPT_MEMCPY
588 INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) {
589 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {
590 // On OS X, calling internal_memcpy here will cause memory corruptions,
591 // because memcpy and memmove are actually aliases of the same
592 // implementation. We need to use internal_memmove here.
593 return internal_memmove(dst, src, size);
596 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size);
597 if (common_flags()->intercept_intrin) {
598 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size);
599 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size);
601 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
602 // due to memcpy being an alias of memmove on OS X.
603 return REAL(memcpy)(dst, src, size);
606 #define INIT_MEMCPY COMMON_INTERCEPT_FUNCTION(memcpy)
611 #if SANITIZER_INTERCEPT_MEMCMP
613 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
614 const void *s1, const void *s2, uptr n,
617 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
618 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
619 return internal_memcmp(a1, a2, size);
621 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
622 if (common_flags()->intercept_memcmp) {
623 if (common_flags()->strict_memcmp) {
624 // Check the entire regions even if the first bytes of the buffers are
626 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
627 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
628 // Fallthrough to REAL(memcmp) below.
630 unsigned char c1 = 0, c2 = 0;
631 const unsigned char *s1 = (const unsigned char*)a1;
632 const unsigned char *s2 = (const unsigned char*)a2;
634 for (i = 0; i < size; i++) {
639 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
640 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
641 int r = CharCmpX(c1, c2);
642 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
647 int result = REAL(memcmp(a1, a2, size));
648 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
653 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
658 #if SANITIZER_INTERCEPT_MEMCHR
659 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
660 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
661 return internal_memchr(s, c, n);
663 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
664 #if SANITIZER_WINDOWS
667 res = REAL(memchr)(s, c, n);
669 res = internal_memchr(s, c, n);
672 void *res = REAL(memchr)(s, c, n);
674 uptr len = res ? (char *)res - (const char *)s + 1 : n;
675 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
679 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
684 #if SANITIZER_INTERCEPT_MEMRCHR
685 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
687 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
688 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
689 return REAL(memrchr)(s, c, n);
692 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
697 #if SANITIZER_INTERCEPT_FREXP
698 INTERCEPTOR(double, frexp, double x, int *exp) {
700 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
701 // Assuming frexp() always writes to |exp|.
702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
703 double res = REAL(frexp)(x, exp);
707 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
710 #endif // SANITIZER_INTERCEPT_FREXP
712 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
713 INTERCEPTOR(float, frexpf, float x, int *exp) {
715 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
716 // FIXME: under ASan the call below may write to freed memory and corrupt
718 // https://github.com/google/sanitizers/issues/321.
719 float res = REAL(frexpf)(x, exp);
720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
724 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
726 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
727 // FIXME: under ASan the call below may write to freed memory and corrupt
729 // https://github.com/google/sanitizers/issues/321.
730 long double res = REAL(frexpl)(x, exp);
731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
735 #define INIT_FREXPF_FREXPL \
736 COMMON_INTERCEPT_FUNCTION(frexpf); \
737 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
739 #define INIT_FREXPF_FREXPL
740 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
743 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
744 SIZE_T iovlen, SIZE_T maxlen) {
745 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
746 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
752 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
753 SIZE_T iovlen, SIZE_T maxlen) {
754 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
755 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
756 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
757 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
763 #if SANITIZER_INTERCEPT_READ
764 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
766 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
767 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
768 // FIXME: under ASan the call below may write to freed memory and corrupt
770 // https://github.com/google/sanitizers/issues/321.
771 SSIZE_T res = REAL(read)(fd, ptr, count);
772 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
773 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
776 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
781 #if SANITIZER_INTERCEPT_PREAD
782 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
784 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
785 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
786 // FIXME: under ASan the call below may write to freed memory and corrupt
788 // https://github.com/google/sanitizers/issues/321.
789 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
790 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
791 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
794 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
799 #if SANITIZER_INTERCEPT_PREAD64
800 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
802 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
803 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
804 // FIXME: under ASan the call below may write to freed memory and corrupt
806 // https://github.com/google/sanitizers/issues/321.
807 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
808 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
809 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
812 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
817 #if SANITIZER_INTERCEPT_READV
818 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
821 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
822 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
823 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
824 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
825 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
828 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
833 #if SANITIZER_INTERCEPT_PREADV
834 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
837 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
838 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
839 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
840 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
841 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
844 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
849 #if SANITIZER_INTERCEPT_PREADV64
850 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
853 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
854 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
855 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
856 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
857 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
860 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
862 #define INIT_PREADV64
865 #if SANITIZER_INTERCEPT_WRITE
866 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
868 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
869 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
870 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
871 SSIZE_T res = REAL(write)(fd, ptr, count);
872 // FIXME: this check should be _before_ the call to REAL(write), not after
873 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
876 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
881 #if SANITIZER_INTERCEPT_PWRITE
882 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
884 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
885 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
886 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
887 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
888 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
891 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
896 #if SANITIZER_INTERCEPT_PWRITE64
897 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
900 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
901 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
902 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
903 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
904 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
907 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
909 #define INIT_PWRITE64
912 #if SANITIZER_INTERCEPT_WRITEV
913 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
916 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
917 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
918 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
919 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
920 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
923 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
928 #if SANITIZER_INTERCEPT_PWRITEV
929 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
932 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
933 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
934 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
935 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
936 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
939 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
944 #if SANITIZER_INTERCEPT_PWRITEV64
945 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
948 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
949 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
950 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
951 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
952 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
955 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
957 #define INIT_PWRITEV64
960 #if SANITIZER_INTERCEPT_PRCTL
961 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
962 unsigned long arg3, // NOLINT
963 unsigned long arg4, unsigned long arg5) { // NOLINT
965 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
966 static const int PR_SET_NAME = 15;
967 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
968 if (option == PR_SET_NAME) {
970 internal_strncpy(buff, (char *)arg2, 15);
972 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
976 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
979 #endif // SANITIZER_INTERCEPT_PRCTL
981 #if SANITIZER_INTERCEPT_TIME
982 INTERCEPTOR(unsigned long, time, unsigned long *t) {
984 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
985 unsigned long local_t;
986 unsigned long res = REAL(time)(&local_t);
987 if (t && res != (unsigned long)-1) {
988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
993 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
996 #endif // SANITIZER_INTERCEPT_TIME
998 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
999 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1002 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1003 // can point to shared memory and tsan would report a data race.
1004 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1005 REAL(strlen(tm->tm_zone)) + 1);
1008 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1010 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1011 __sanitizer_tm *res = REAL(localtime)(timep);
1013 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1014 unpoison_tm(ctx, res);
1018 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1020 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1021 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1023 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1024 unpoison_tm(ctx, res);
1028 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1030 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1031 __sanitizer_tm *res = REAL(gmtime)(timep);
1033 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1034 unpoison_tm(ctx, res);
1038 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1040 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1041 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1043 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1044 unpoison_tm(ctx, res);
1048 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1050 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1051 // FIXME: under ASan the call below may write to freed memory and corrupt
1052 // its metadata. See
1053 // https://github.com/google/sanitizers/issues/321.
1054 char *res = REAL(ctime)(timep);
1056 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1057 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1061 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1063 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1064 // FIXME: under ASan the call below may write to freed memory and corrupt
1065 // its metadata. See
1066 // https://github.com/google/sanitizers/issues/321.
1067 char *res = REAL(ctime_r)(timep, result);
1069 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1074 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1076 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1077 // FIXME: under ASan the call below may write to freed memory and corrupt
1078 // its metadata. See
1079 // https://github.com/google/sanitizers/issues/321.
1080 char *res = REAL(asctime)(tm);
1082 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1087 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1089 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1090 // FIXME: under ASan the call below may write to freed memory and corrupt
1091 // its metadata. See
1092 // https://github.com/google/sanitizers/issues/321.
1093 char *res = REAL(asctime_r)(tm, result);
1095 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1096 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1100 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1102 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1103 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1104 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1105 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1106 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1107 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1108 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1109 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1110 long res = REAL(mktime)(tm);
1111 if (res != -1) unpoison_tm(ctx, tm);
1114 #define INIT_LOCALTIME_AND_FRIENDS \
1115 COMMON_INTERCEPT_FUNCTION(localtime); \
1116 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1117 COMMON_INTERCEPT_FUNCTION(gmtime); \
1118 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1119 COMMON_INTERCEPT_FUNCTION(ctime); \
1120 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1121 COMMON_INTERCEPT_FUNCTION(asctime); \
1122 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1123 COMMON_INTERCEPT_FUNCTION(mktime);
1125 #define INIT_LOCALTIME_AND_FRIENDS
1126 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1128 #if SANITIZER_INTERCEPT_STRPTIME
1129 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1131 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1133 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1134 // FIXME: under ASan the call below may write to freed memory and corrupt
1135 // its metadata. See
1136 // https://github.com/google/sanitizers/issues/321.
1137 char *res = REAL(strptime)(s, format, tm);
1138 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1140 // Do not call unpoison_tm here, because strptime does not, in fact,
1141 // initialize the entire struct tm. For example, tm_zone pointer is left
1143 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1147 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1149 #define INIT_STRPTIME
1152 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1153 #include "sanitizer_common_interceptors_format.inc"
1155 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1159 va_start(ap, format); \
1160 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1161 int res = WRAP(vname)(__VA_ARGS__, ap); \
1168 #if SANITIZER_INTERCEPT_SCANF
1170 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1173 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1176 int res = REAL(vname)(__VA_ARGS__); \
1178 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1183 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1184 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1186 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1187 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1189 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1190 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1192 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1193 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1194 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1196 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1198 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1200 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1201 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1202 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1204 INTERCEPTOR(int, scanf, const char *format, ...)
1205 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1207 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1208 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1210 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1211 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1213 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1214 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1215 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1217 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1218 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1220 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1221 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1226 #if SANITIZER_INTERCEPT_SCANF
1227 #define INIT_SCANF \
1228 COMMON_INTERCEPT_FUNCTION(scanf); \
1229 COMMON_INTERCEPT_FUNCTION(sscanf); \
1230 COMMON_INTERCEPT_FUNCTION(fscanf); \
1231 COMMON_INTERCEPT_FUNCTION(vscanf); \
1232 COMMON_INTERCEPT_FUNCTION(vsscanf); \
1233 COMMON_INTERCEPT_FUNCTION(vfscanf);
1238 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1239 #define INIT_ISOC99_SCANF \
1240 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1241 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1242 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1243 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1244 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1245 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1247 #define INIT_ISOC99_SCANF
1250 #if SANITIZER_INTERCEPT_PRINTF
1252 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1254 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1258 #define VPRINTF_INTERCEPTOR_RETURN() \
1261 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1263 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1264 if (common_flags()->check_printf) \
1265 printf_common(ctx, format, aq); \
1266 int res = REAL(vname)(__VA_ARGS__); \
1267 VPRINTF_INTERCEPTOR_RETURN(); \
1271 // FIXME: under ASan the REAL() call below may write to freed memory and
1272 // corrupt its metadata. See
1273 // https://github.com/google/sanitizers/issues/321.
1274 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1276 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1277 if (common_flags()->check_printf) { \
1278 printf_common(ctx, format, aq); \
1280 int res = REAL(vname)(str, __VA_ARGS__); \
1282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1284 VPRINTF_INTERCEPTOR_RETURN(); \
1288 // FIXME: under ASan the REAL() call below may write to freed memory and
1289 // corrupt its metadata. See
1290 // https://github.com/google/sanitizers/issues/321.
1291 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1293 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1294 if (common_flags()->check_printf) { \
1295 printf_common(ctx, format, aq); \
1297 int res = REAL(vname)(str, size, __VA_ARGS__); \
1299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1301 VPRINTF_INTERCEPTOR_RETURN(); \
1305 // FIXME: under ASan the REAL() call below may write to freed memory and
1306 // corrupt its metadata. See
1307 // https://github.com/google/sanitizers/issues/321.
1308 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1310 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1311 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1312 if (common_flags()->check_printf) { \
1313 printf_common(ctx, format, aq); \
1315 int res = REAL(vname)(strp, __VA_ARGS__); \
1317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1319 VPRINTF_INTERCEPTOR_RETURN(); \
1323 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1324 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1326 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1328 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1330 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1332 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1334 #if SANITIZER_INTERCEPT_PRINTF_L
1335 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1336 const char *format, va_list ap)
1337 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1339 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1340 const char *format, ...)
1341 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1342 #endif // SANITIZER_INTERCEPT_PRINTF_L
1344 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1345 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1347 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1348 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1350 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1351 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1352 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1354 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1355 const char *format, va_list ap)
1356 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1358 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1360 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1362 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1364 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1367 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1369 INTERCEPTOR(int, printf, const char *format, ...)
1370 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1372 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1373 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1375 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1376 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1378 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1379 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1381 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1382 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1384 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1385 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1386 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1388 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1390 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1392 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1393 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1395 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1396 const char *format, ...)
1397 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1400 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1402 #endif // SANITIZER_INTERCEPT_PRINTF
1404 #if SANITIZER_INTERCEPT_PRINTF
1405 #define INIT_PRINTF \
1406 COMMON_INTERCEPT_FUNCTION(printf); \
1407 COMMON_INTERCEPT_FUNCTION(sprintf); \
1408 COMMON_INTERCEPT_FUNCTION(snprintf); \
1409 COMMON_INTERCEPT_FUNCTION(asprintf); \
1410 COMMON_INTERCEPT_FUNCTION(fprintf); \
1411 COMMON_INTERCEPT_FUNCTION(vprintf); \
1412 COMMON_INTERCEPT_FUNCTION(vsprintf); \
1413 COMMON_INTERCEPT_FUNCTION(vsnprintf); \
1414 COMMON_INTERCEPT_FUNCTION(vasprintf); \
1415 COMMON_INTERCEPT_FUNCTION(vfprintf);
1420 #if SANITIZER_INTERCEPT_PRINTF_L
1421 #define INIT_PRINTF_L \
1422 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1423 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1425 #define INIT_PRINTF_L
1428 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1429 #define INIT_ISOC99_PRINTF \
1430 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1431 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1432 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1433 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1434 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1435 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1436 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1437 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1439 #define INIT_ISOC99_PRINTF
1442 #if SANITIZER_INTERCEPT_IOCTL
1443 #include "sanitizer_common_interceptors_ioctl.inc"
1444 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1445 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1446 // can trigger a report and we need to be able to unwind through this
1447 // function. On Mac in debug mode we might not have a frame pointer, because
1448 // ioctl_common_[pre|post] doesn't get inlined here.
1449 ENABLE_FRAME_POINTER;
1453 va_start(ap, request);
1454 void *arg = va_arg(ap, void *);
1456 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1458 CHECK(ioctl_initialized);
1460 // Note: TSan does not use common flags, and they are zero-initialized.
1461 // This effectively disables ioctl handling in TSan.
1462 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1464 // Although request is unsigned long, the rest of the interceptor uses it
1465 // as just "unsigned" to save space, because we know that all values fit in
1466 // "unsigned" - they are compile-time constants.
1468 const ioctl_desc *desc = ioctl_lookup(request);
1469 ioctl_desc decoded_desc;
1471 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1472 if (!ioctl_decode(request, &decoded_desc))
1473 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1475 desc = &decoded_desc;
1478 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1479 int res = REAL(ioctl)(d, request, arg);
1480 // FIXME: some ioctls have different return values for success and failure.
1481 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1484 #define INIT_IOCTL \
1486 COMMON_INTERCEPT_FUNCTION(ioctl);
1491 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1492 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1493 SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1494 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1496 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1498 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1499 REAL(strlen)(pwd->pw_name) + 1);
1501 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1502 REAL(strlen)(pwd->pw_passwd) + 1);
1503 #if !SANITIZER_ANDROID
1505 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1506 REAL(strlen)(pwd->pw_gecos) + 1);
1510 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1511 REAL(strlen)(pwd->pw_class) + 1);
1514 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1515 REAL(strlen)(pwd->pw_dir) + 1);
1517 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1518 REAL(strlen)(pwd->pw_shell) + 1);
1522 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1526 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1527 REAL(strlen)(grp->gr_name) + 1);
1529 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1530 REAL(strlen)(grp->gr_passwd) + 1);
1531 char **p = grp->gr_mem;
1533 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1535 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1536 (p - grp->gr_mem + 1) * sizeof(*p));
1539 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1540 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1541 // SANITIZER_INTERCEPT_GETPWENT_R ||
1542 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1544 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1545 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1547 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1548 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1549 __sanitizer_passwd *res = REAL(getpwnam)(name);
1550 if (res) unpoison_passwd(ctx, res);
1553 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1555 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1556 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1557 if (res) unpoison_passwd(ctx, res);
1560 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1562 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1563 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1564 __sanitizer_group *res = REAL(getgrnam)(name);
1565 if (res) unpoison_group(ctx, res);
1568 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1570 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1571 __sanitizer_group *res = REAL(getgrgid)(gid);
1572 if (res) unpoison_group(ctx, res);
1575 #define INIT_GETPWNAM_AND_FRIENDS \
1576 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1577 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1578 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1579 COMMON_INTERCEPT_FUNCTION(getgrgid);
1581 #define INIT_GETPWNAM_AND_FRIENDS
1584 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1585 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1586 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1588 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1589 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1590 // FIXME: under ASan the call below may write to freed memory and corrupt
1591 // its metadata. See
1592 // https://github.com/google/sanitizers/issues/321.
1593 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1595 if (result && *result) unpoison_passwd(ctx, *result);
1596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1598 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1601 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1602 SIZE_T buflen, __sanitizer_passwd **result) {
1604 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1605 // FIXME: under ASan the call below may write to freed memory and corrupt
1606 // its metadata. See
1607 // https://github.com/google/sanitizers/issues/321.
1608 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1610 if (result && *result) unpoison_passwd(ctx, *result);
1611 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1613 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1616 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1617 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1619 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1620 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1621 // FIXME: under ASan the call below may write to freed memory and corrupt
1622 // its metadata. See
1623 // https://github.com/google/sanitizers/issues/321.
1624 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1626 if (result && *result) unpoison_group(ctx, *result);
1627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1629 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1632 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1633 SIZE_T buflen, __sanitizer_group **result) {
1635 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1636 // FIXME: under ASan the call below may write to freed memory and corrupt
1637 // its metadata. See
1638 // https://github.com/google/sanitizers/issues/321.
1639 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1641 if (result && *result) unpoison_group(ctx, *result);
1642 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1644 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1647 #define INIT_GETPWNAM_R_AND_FRIENDS \
1648 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1649 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1650 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1651 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1653 #define INIT_GETPWNAM_R_AND_FRIENDS
1656 #if SANITIZER_INTERCEPT_GETPWENT
1657 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1659 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1660 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1661 if (res) unpoison_passwd(ctx, res);
1664 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
1666 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
1667 __sanitizer_group *res = REAL(getgrent)(dummy);
1668 if (res) unpoison_group(ctx, res);;
1671 #define INIT_GETPWENT \
1672 COMMON_INTERCEPT_FUNCTION(getpwent); \
1673 COMMON_INTERCEPT_FUNCTION(getgrent);
1675 #define INIT_GETPWENT
1678 #if SANITIZER_INTERCEPT_FGETPWENT
1679 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
1681 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
1682 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
1683 if (res) unpoison_passwd(ctx, res);
1686 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
1688 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
1689 __sanitizer_group *res = REAL(fgetgrent)(fp);
1690 if (res) unpoison_group(ctx, res);
1693 #define INIT_FGETPWENT \
1694 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
1695 COMMON_INTERCEPT_FUNCTION(fgetgrent);
1697 #define INIT_FGETPWENT
1700 #if SANITIZER_INTERCEPT_GETPWENT_R
1701 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
1702 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1704 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
1705 // FIXME: under ASan the call below may write to freed memory and corrupt
1706 // its metadata. See
1707 // https://github.com/google/sanitizers/issues/321.
1708 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
1710 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1713 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1716 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
1717 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
1719 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
1720 // FIXME: under ASan the call below may write to freed memory and corrupt
1721 // its metadata. See
1722 // https://github.com/google/sanitizers/issues/321.
1723 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
1725 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
1726 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1728 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1731 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
1732 __sanitizer_group **pwbufp) {
1734 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
1735 // FIXME: under ASan the call below may write to freed memory and corrupt
1736 // its metadata. See
1737 // https://github.com/google/sanitizers/issues/321.
1738 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
1740 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1743 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1746 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
1747 SIZE_T buflen, __sanitizer_group **pwbufp) {
1749 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
1750 // FIXME: under ASan the call below may write to freed memory and corrupt
1751 // its metadata. See
1752 // https://github.com/google/sanitizers/issues/321.
1753 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
1755 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
1756 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1758 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
1761 #define INIT_GETPWENT_R \
1762 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
1763 COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
1764 COMMON_INTERCEPT_FUNCTION(getgrent_r); \
1765 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
1767 #define INIT_GETPWENT_R
1770 #if SANITIZER_INTERCEPT_SETPWENT
1771 // The only thing these interceptors do is disable any nested interceptors.
1772 // These functions may open nss modules and call uninstrumented functions from
1773 // them, and we don't want things like strlen() to trigger.
1774 INTERCEPTOR(void, setpwent, int dummy) {
1776 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
1777 REAL(setpwent)(dummy);
1779 INTERCEPTOR(void, endpwent, int dummy) {
1781 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
1782 REAL(endpwent)(dummy);
1784 INTERCEPTOR(void, setgrent, int dummy) {
1786 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
1787 REAL(setgrent)(dummy);
1789 INTERCEPTOR(void, endgrent, int dummy) {
1791 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
1792 REAL(endgrent)(dummy);
1794 #define INIT_SETPWENT \
1795 COMMON_INTERCEPT_FUNCTION(setpwent); \
1796 COMMON_INTERCEPT_FUNCTION(endpwent); \
1797 COMMON_INTERCEPT_FUNCTION(setgrent); \
1798 COMMON_INTERCEPT_FUNCTION(endgrent);
1800 #define INIT_SETPWENT
1803 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
1804 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
1806 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
1807 // FIXME: under ASan the call below may write to freed memory and corrupt
1808 // its metadata. See
1809 // https://github.com/google/sanitizers/issues/321.
1810 int res = REAL(clock_getres)(clk_id, tp);
1812 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1816 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
1818 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
1819 // FIXME: under ASan the call below may write to freed memory and corrupt
1820 // its metadata. See
1821 // https://github.com/google/sanitizers/issues/321.
1822 int res = REAL(clock_gettime)(clk_id, tp);
1824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
1828 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
1830 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
1831 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
1832 return REAL(clock_settime)(clk_id, tp);
1834 #define INIT_CLOCK_GETTIME \
1835 COMMON_INTERCEPT_FUNCTION(clock_getres); \
1836 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
1837 COMMON_INTERCEPT_FUNCTION(clock_settime);
1839 #define INIT_CLOCK_GETTIME
1842 #if SANITIZER_INTERCEPT_GETITIMER
1843 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
1845 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
1846 // FIXME: under ASan the call below may write to freed memory and corrupt
1847 // its metadata. See
1848 // https://github.com/google/sanitizers/issues/321.
1849 int res = REAL(getitimer)(which, curr_value);
1850 if (!res && curr_value) {
1851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
1855 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
1857 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
1859 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
1860 // FIXME: under ASan the call below may write to freed memory and corrupt
1861 // its metadata. See
1862 // https://github.com/google/sanitizers/issues/321.
1863 int res = REAL(setitimer)(which, new_value, old_value);
1864 if (!res && old_value) {
1865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
1869 #define INIT_GETITIMER \
1870 COMMON_INTERCEPT_FUNCTION(getitimer); \
1871 COMMON_INTERCEPT_FUNCTION(setitimer);
1873 #define INIT_GETITIMER
1876 #if SANITIZER_INTERCEPT_GLOB
1877 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
1878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
1879 // +1 for NULL pointer at the end.
1880 if (pglob->gl_pathv)
1881 COMMON_INTERCEPTOR_WRITE_RANGE(
1882 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
1883 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
1884 char *p = pglob->gl_pathv[i];
1885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
1889 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
1891 static void wrapped_gl_closedir(void *dir) {
1892 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1893 pglob_copy->gl_closedir(dir);
1896 static void *wrapped_gl_readdir(void *dir) {
1897 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1898 return pglob_copy->gl_readdir(dir);
1901 static void *wrapped_gl_opendir(const char *s) {
1902 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
1903 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1904 return pglob_copy->gl_opendir(s);
1907 static int wrapped_gl_lstat(const char *s, void *st) {
1908 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1909 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1910 return pglob_copy->gl_lstat(s, st);
1913 static int wrapped_gl_stat(const char *s, void *st) {
1914 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
1915 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
1916 return pglob_copy->gl_stat(s, st);
1919 static const __sanitizer_glob_t kGlobCopy = {
1921 0, wrapped_gl_closedir, wrapped_gl_readdir,
1922 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
1924 INTERCEPTOR(int, glob, const char *pattern, int flags,
1925 int (*errfunc)(const char *epath, int eerrno),
1926 __sanitizer_glob_t *pglob) {
1928 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
1929 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1930 __sanitizer_glob_t glob_copy;
1931 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1932 if (flags & glob_altdirfunc) {
1933 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1934 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1935 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1936 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1937 Swap(pglob->gl_stat, glob_copy.gl_stat);
1938 pglob_copy = &glob_copy;
1940 int res = REAL(glob)(pattern, flags, errfunc, pglob);
1941 if (flags & glob_altdirfunc) {
1942 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1943 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1944 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1945 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1946 Swap(pglob->gl_stat, glob_copy.gl_stat);
1949 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1953 INTERCEPTOR(int, glob64, const char *pattern, int flags,
1954 int (*errfunc)(const char *epath, int eerrno),
1955 __sanitizer_glob_t *pglob) {
1957 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
1958 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
1959 __sanitizer_glob_t glob_copy;
1960 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
1961 if (flags & glob_altdirfunc) {
1962 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1963 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1964 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1965 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1966 Swap(pglob->gl_stat, glob_copy.gl_stat);
1967 pglob_copy = &glob_copy;
1969 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
1970 if (flags & glob_altdirfunc) {
1971 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
1972 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
1973 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
1974 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
1975 Swap(pglob->gl_stat, glob_copy.gl_stat);
1978 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
1982 COMMON_INTERCEPT_FUNCTION(glob); \
1983 COMMON_INTERCEPT_FUNCTION(glob64);
1984 #else // SANITIZER_INTERCEPT_GLOB
1986 #endif // SANITIZER_INTERCEPT_GLOB
1988 #if SANITIZER_INTERCEPT_WAIT
1989 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
1990 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
1992 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
1994 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
1995 // FIXME: under ASan the call below may write to freed memory and corrupt
1996 // its metadata. See
1997 // https://github.com/google/sanitizers/issues/321.
1998 int res = REAL(wait)(status);
1999 if (res != -1 && status)
2000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2003 // On FreeBSD id_t is always 64-bit wide.
2004 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2005 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2008 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2012 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2013 // FIXME: under ASan the call below may write to freed memory and corrupt
2014 // its metadata. See
2015 // https://github.com/google/sanitizers/issues/321.
2016 int res = REAL(waitid)(idtype, id, infop, options);
2017 if (res != -1 && infop)
2018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2021 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2023 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
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(waitpid)(pid, status, options);
2028 if (res != -1 && status)
2029 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2032 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2034 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2035 // FIXME: under ASan the call below may write to freed memory and corrupt
2036 // its metadata. See
2037 // https://github.com/google/sanitizers/issues/321.
2038 int res = REAL(wait3)(status, options, rusage);
2040 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2041 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2045 #if SANITIZER_ANDROID
2046 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2048 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2049 // FIXME: under ASan the call below may write to freed memory and corrupt
2050 // its metadata. See
2051 // https://github.com/google/sanitizers/issues/321.
2052 int res = REAL(__wait4)(pid, status, options, rusage);
2054 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2055 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2059 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2061 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2063 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2064 // FIXME: under ASan the call below may write to freed memory and corrupt
2065 // its metadata. See
2066 // https://github.com/google/sanitizers/issues/321.
2067 int res = REAL(wait4)(pid, status, options, rusage);
2069 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2070 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2074 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2075 #endif // SANITIZER_ANDROID
2077 COMMON_INTERCEPT_FUNCTION(wait); \
2078 COMMON_INTERCEPT_FUNCTION(waitid); \
2079 COMMON_INTERCEPT_FUNCTION(waitpid); \
2080 COMMON_INTERCEPT_FUNCTION(wait3);
2086 #if SANITIZER_INTERCEPT_INET
2087 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2089 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2090 uptr sz = __sanitizer_in_addr_sz(af);
2091 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2092 // FIXME: figure out read size based on the address family.
2093 // FIXME: under ASan the call below may write to freed memory and corrupt
2094 // its metadata. See
2095 // https://github.com/google/sanitizers/issues/321.
2096 char *res = REAL(inet_ntop)(af, src, dst, size);
2097 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2100 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2102 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2103 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2104 // FIXME: figure out read size based on the address family.
2105 // FIXME: under ASan the call below may write to freed memory and corrupt
2106 // its metadata. See
2107 // https://github.com/google/sanitizers/issues/321.
2108 int res = REAL(inet_pton)(af, src, dst);
2110 uptr sz = __sanitizer_in_addr_sz(af);
2111 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2116 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2117 COMMON_INTERCEPT_FUNCTION(inet_pton);
2122 #if SANITIZER_INTERCEPT_INET
2123 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2125 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2126 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2127 // FIXME: under ASan the call below may write to freed memory and corrupt
2128 // its metadata. See
2129 // https://github.com/google/sanitizers/issues/321.
2130 int res = REAL(inet_aton)(cp, dst);
2132 uptr sz = __sanitizer_in_addr_sz(af_inet);
2133 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2137 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2139 #define INIT_INET_ATON
2142 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2143 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2145 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2146 // FIXME: under ASan the call below may write to freed memory and corrupt
2147 // its metadata. See
2148 // https://github.com/google/sanitizers/issues/321.
2149 int res = REAL(pthread_getschedparam)(thread, policy, param);
2151 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2152 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2156 #define INIT_PTHREAD_GETSCHEDPARAM \
2157 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2159 #define INIT_PTHREAD_GETSCHEDPARAM
2162 #if SANITIZER_INTERCEPT_GETADDRINFO
2163 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2164 struct __sanitizer_addrinfo *hints,
2165 struct __sanitizer_addrinfo **out) {
2167 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2168 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2170 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2172 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
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(getaddrinfo)(node, service, hints, out);
2177 if (res == 0 && out) {
2178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2179 struct __sanitizer_addrinfo *p = *out;
2181 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2184 if (p->ai_canonname)
2185 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2186 REAL(strlen)(p->ai_canonname) + 1);
2192 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2194 #define INIT_GETADDRINFO
2197 #if SANITIZER_INTERCEPT_GETNAMEINFO
2198 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2199 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2201 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2202 serv, servlen, flags);
2203 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2204 // There is padding in in_addr that may make this too noisy
2205 // FIXME: under ASan the call below may write to freed memory and corrupt
2206 // its metadata. See
2207 // https://github.com/google/sanitizers/issues/321.
2209 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2211 if (host && hostlen)
2212 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2213 if (serv && servlen)
2214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2218 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2220 #define INIT_GETNAMEINFO
2223 #if SANITIZER_INTERCEPT_GETSOCKNAME
2224 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2226 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2227 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2228 int addrlen_in = *addrlen;
2229 // FIXME: under ASan the call below may write to freed memory and corrupt
2230 // its metadata. See
2231 // https://github.com/google/sanitizers/issues/321.
2232 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2234 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2238 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2240 #define INIT_GETSOCKNAME
2243 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2244 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2248 char **p = h->h_aliases;
2250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2253 COMMON_INTERCEPTOR_WRITE_RANGE(
2254 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2257 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2260 COMMON_INTERCEPTOR_WRITE_RANGE(
2261 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2265 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2266 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2268 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2269 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2270 if (res) write_hostent(ctx, res);
2274 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2277 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2278 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2279 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2280 if (res) write_hostent(ctx, res);
2284 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2286 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2287 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2288 if (res) write_hostent(ctx, res);
2292 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2294 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2295 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2296 if (res) write_hostent(ctx, res);
2299 #define INIT_GETHOSTBYNAME \
2300 COMMON_INTERCEPT_FUNCTION(gethostent); \
2301 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2302 COMMON_INTERCEPT_FUNCTION(gethostbyname); \
2303 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2305 #define INIT_GETHOSTBYNAME
2308 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2309 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2310 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2313 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2315 // FIXME: under ASan the call below may write to freed memory and corrupt
2316 // its metadata. See
2317 // https://github.com/google/sanitizers/issues/321.
2318 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2320 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2321 if (res == 0 && *result) write_hostent(ctx, *result);
2324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2327 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2329 #define INIT_GETHOSTBYNAME_R
2332 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2333 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2334 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2336 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2338 // FIXME: under ASan the call below may write to freed memory and corrupt
2339 // its metadata. See
2340 // https://github.com/google/sanitizers/issues/321.
2341 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2343 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2344 if (res == 0 && *result) write_hostent(ctx, *result);
2347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2350 #define INIT_GETHOSTENT_R \
2351 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2353 #define INIT_GETHOSTENT_R
2356 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2357 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2358 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2359 __sanitizer_hostent **result, int *h_errnop) {
2361 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2362 buflen, result, h_errnop);
2363 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2364 // FIXME: under ASan the call below may write to freed memory and corrupt
2365 // its metadata. See
2366 // https://github.com/google/sanitizers/issues/321.
2367 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2370 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2371 if (res == 0 && *result) write_hostent(ctx, *result);
2374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2377 #define INIT_GETHOSTBYADDR_R \
2378 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2380 #define INIT_GETHOSTBYADDR_R
2383 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2384 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2385 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2386 __sanitizer_hostent **result, int *h_errnop) {
2388 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2390 // FIXME: under ASan the call below may write to freed memory and corrupt
2391 // its metadata. See
2392 // https://github.com/google/sanitizers/issues/321.
2394 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2397 if (res == 0 && *result) write_hostent(ctx, *result);
2400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2403 #define INIT_GETHOSTBYNAME2_R \
2404 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2406 #define INIT_GETHOSTBYNAME2_R
2409 #if SANITIZER_INTERCEPT_GETSOCKOPT
2410 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2413 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2415 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2416 // FIXME: under ASan the call below may write to freed memory and corrupt
2417 // its metadata. See
2418 // https://github.com/google/sanitizers/issues/321.
2419 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2421 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2424 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2426 #define INIT_GETSOCKOPT
2429 #if SANITIZER_INTERCEPT_ACCEPT
2430 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2432 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2433 unsigned addrlen0 = 0;
2435 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2436 addrlen0 = *addrlen;
2438 int fd2 = REAL(accept)(fd, addr, addrlen);
2440 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2441 if (addr && addrlen)
2442 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2446 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2451 #if SANITIZER_INTERCEPT_ACCEPT4
2452 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2454 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2455 unsigned addrlen0 = 0;
2457 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2458 addrlen0 = *addrlen;
2460 // FIXME: under ASan the call below may write to freed memory and corrupt
2461 // its metadata. See
2462 // https://github.com/google/sanitizers/issues/321.
2463 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2465 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2466 if (addr && addrlen)
2467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2471 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2473 #define INIT_ACCEPT4
2476 #if SANITIZER_INTERCEPT_MODF
2477 INTERCEPTOR(double, modf, double x, double *iptr) {
2479 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2480 // FIXME: under ASan the call below may write to freed memory and corrupt
2481 // its metadata. See
2482 // https://github.com/google/sanitizers/issues/321.
2483 double res = REAL(modf)(x, iptr);
2485 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2489 INTERCEPTOR(float, modff, float x, float *iptr) {
2491 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2492 // FIXME: under ASan the call below may write to freed memory and corrupt
2493 // its metadata. See
2494 // https://github.com/google/sanitizers/issues/321.
2495 float res = REAL(modff)(x, iptr);
2497 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2501 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2503 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2504 // FIXME: under ASan the call below may write to freed memory and corrupt
2505 // its metadata. See
2506 // https://github.com/google/sanitizers/issues/321.
2507 long double res = REAL(modfl)(x, iptr);
2509 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2514 COMMON_INTERCEPT_FUNCTION(modf); \
2515 COMMON_INTERCEPT_FUNCTION(modff); \
2516 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2521 #if SANITIZER_INTERCEPT_RECVMSG
2522 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2525 if (msg->msg_name && msg->msg_namelen)
2526 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2527 if (msg->msg_iov && msg->msg_iovlen)
2528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2529 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2530 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2531 if (msg->msg_control && msg->msg_controllen)
2532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2535 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2538 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2539 // FIXME: under ASan the call below may write to freed memory and corrupt
2540 // its metadata. See
2541 // https://github.com/google/sanitizers/issues/321.
2542 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2544 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2546 write_msghdr(ctx, msg, res);
2547 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2552 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2554 #define INIT_RECVMSG
2557 #if SANITIZER_INTERCEPT_SENDMSG
2558 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2559 const unsigned kCmsgDataOffset =
2560 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2562 char *p = (char *)control;
2563 char *const control_end = p + controllen;
2565 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
2566 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
2567 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
2569 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
2571 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
2572 sizeof(cmsg->cmsg_level));
2573 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
2574 sizeof(cmsg->cmsg_type));
2576 if (cmsg->cmsg_len > kCmsgDataOffset) {
2577 char *data = p + kCmsgDataOffset;
2578 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
2579 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
2582 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
2586 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2589 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
2598 if (msg->msg_name && msg->msg_namelen)
2599 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2600 if (msg->msg_iov && msg->msg_iovlen)
2601 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
2602 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2603 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2604 if (msg->msg_control && msg->msg_controllen)
2605 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
2608 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
2611 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
2613 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
2614 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
2616 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
2617 if (common_flags()->intercept_send && res >= 0 && msg)
2618 read_msghdr(ctx, msg, res);
2621 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
2623 #define INIT_SENDMSG
2626 #if SANITIZER_INTERCEPT_GETPEERNAME
2627 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
2629 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
2631 if (addrlen) addr_sz = *addrlen;
2632 // FIXME: under ASan the call below may write to freed memory and corrupt
2633 // its metadata. See
2634 // https://github.com/google/sanitizers/issues/321.
2635 int res = REAL(getpeername)(sockfd, addr, addrlen);
2636 if (!res && addr && addrlen)
2637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2640 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
2642 #define INIT_GETPEERNAME
2645 #if SANITIZER_INTERCEPT_SYSINFO
2646 INTERCEPTOR(int, sysinfo, void *info) {
2648 // FIXME: under ASan the call below may write to freed memory and corrupt
2649 // its metadata. See
2650 // https://github.com/google/sanitizers/issues/321.
2651 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
2652 int res = REAL(sysinfo)(info);
2654 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
2657 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
2659 #define INIT_SYSINFO
2662 #if SANITIZER_INTERCEPT_READDIR
2663 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
2665 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
2666 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
2667 __sanitizer_dirent *res = REAL(opendir)(path);
2669 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
2673 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
2675 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
2676 // FIXME: under ASan the call below may write to freed memory and corrupt
2677 // its metadata. See
2678 // https://github.com/google/sanitizers/issues/321.
2679 __sanitizer_dirent *res = REAL(readdir)(dirp);
2680 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2684 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
2685 __sanitizer_dirent **result) {
2687 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
2688 // FIXME: under ASan the call below may write to freed memory and corrupt
2689 // its metadata. See
2690 // https://github.com/google/sanitizers/issues/321.
2691 int res = REAL(readdir_r)(dirp, entry, result);
2693 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2695 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2700 #define INIT_READDIR \
2701 COMMON_INTERCEPT_FUNCTION(opendir); \
2702 COMMON_INTERCEPT_FUNCTION(readdir); \
2703 COMMON_INTERCEPT_FUNCTION(readdir_r);
2705 #define INIT_READDIR
2708 #if SANITIZER_INTERCEPT_READDIR64
2709 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
2711 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
2712 // FIXME: under ASan the call below may write to freed memory and corrupt
2713 // its metadata. See
2714 // https://github.com/google/sanitizers/issues/321.
2715 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
2716 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
2720 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
2721 __sanitizer_dirent64 **result) {
2723 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
2724 // FIXME: under ASan the call below may write to freed memory and corrupt
2725 // its metadata. See
2726 // https://github.com/google/sanitizers/issues/321.
2727 int res = REAL(readdir64_r)(dirp, entry, result);
2729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
2735 #define INIT_READDIR64 \
2736 COMMON_INTERCEPT_FUNCTION(readdir64); \
2737 COMMON_INTERCEPT_FUNCTION(readdir64_r);
2739 #define INIT_READDIR64
2742 #if SANITIZER_INTERCEPT_PTRACE
2743 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
2745 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
2746 __sanitizer_iovec local_iovec;
2749 if (request == ptrace_setregs)
2750 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
2751 else if (request == ptrace_setfpregs)
2752 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2753 else if (request == ptrace_setfpxregs)
2754 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2755 else if (request == ptrace_setvfpregs)
2756 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2757 else if (request == ptrace_setsiginfo)
2758 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
2759 // Some kernel might zero the iovec::iov_base in case of invalid
2760 // write access. In this case copy the invalid address for further
2762 else if (request == ptrace_setregset || request == ptrace_getregset) {
2763 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2764 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
2765 local_iovec = *iovec;
2766 if (request == ptrace_setregset)
2767 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
2771 // FIXME: under ASan the call below may write to freed memory and corrupt
2772 // its metadata. See
2773 // https://github.com/google/sanitizers/issues/321.
2774 uptr res = REAL(ptrace)(request, pid, addr, data);
2777 // Note that PEEK* requests assign different meaning to the return value.
2778 // This function does not handle them (nor does it need to).
2779 if (request == ptrace_getregs)
2780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
2781 else if (request == ptrace_getfpregs)
2782 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
2783 else if (request == ptrace_getfpxregs)
2784 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
2785 else if (request == ptrace_getvfpregs)
2786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
2787 else if (request == ptrace_getsiginfo)
2788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
2789 else if (request == ptrace_geteventmsg)
2790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
2791 else if (request == ptrace_getregset) {
2792 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
2793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
2794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
2795 local_iovec.iov_len);
2801 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
2806 #if SANITIZER_INTERCEPT_SETLOCALE
2807 INTERCEPTOR(char *, setlocale, int category, char *locale) {
2809 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
2811 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
2812 char *res = REAL(setlocale)(category, locale);
2813 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2817 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
2819 #define INIT_SETLOCALE
2822 #if SANITIZER_INTERCEPT_GETCWD
2823 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
2825 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
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 char *res = REAL(getcwd)(buf, size);
2830 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2833 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
2838 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
2839 INTERCEPTOR(char *, get_current_dir_name, int fake) {
2841 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
2842 // FIXME: under ASan the call below may write to freed memory and corrupt
2843 // its metadata. See
2844 // https://github.com/google/sanitizers/issues/321.
2845 char *res = REAL(get_current_dir_name)(fake);
2846 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2850 #define INIT_GET_CURRENT_DIR_NAME \
2851 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
2853 #define INIT_GET_CURRENT_DIR_NAME
2856 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
2858 if (nptr == *endptr) {
2859 // No digits were found at strtol call, we need to find out the last
2860 // symbol accessed by strtoll on our own.
2861 // We get this symbol by skipping leading blanks and optional +/- sign.
2862 while (IsSpace(*nptr)) nptr++;
2863 if (*nptr == '+' || *nptr == '-') nptr++;
2864 *endptr = const_cast<char *>(nptr);
2866 CHECK(*endptr >= nptr);
2869 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
2870 char **endptr, char *real_endptr, int base) {
2872 *endptr = real_endptr;
2873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
2875 // If base has unsupported value, strtol can exit with EINVAL
2876 // without reading any characters. So do additional checks only
2877 // if base is valid.
2878 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
2879 if (is_valid_base) {
2880 FixRealStrtolEndptr(nptr, &real_endptr);
2882 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
2883 (real_endptr - nptr) + 1 : 0);
2887 #if SANITIZER_INTERCEPT_STRTOIMAX
2888 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
2890 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
2891 // FIXME: under ASan the call below may write to freed memory and corrupt
2892 // its metadata. See
2893 // https://github.com/google/sanitizers/issues/321.
2895 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
2896 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2900 INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
2902 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
2903 // FIXME: under ASan the call below may write to freed memory and corrupt
2904 // its metadata. See
2905 // https://github.com/google/sanitizers/issues/321.
2907 INTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
2908 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
2912 #define INIT_STRTOIMAX \
2913 COMMON_INTERCEPT_FUNCTION(strtoimax); \
2914 COMMON_INTERCEPT_FUNCTION(strtoumax);
2916 #define INIT_STRTOIMAX
2919 #if SANITIZER_INTERCEPT_MBSTOWCS
2920 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
2922 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
2923 // FIXME: under ASan the call below may write to freed memory and corrupt
2924 // its metadata. See
2925 // https://github.com/google/sanitizers/issues/321.
2926 SIZE_T res = REAL(mbstowcs)(dest, src, len);
2927 if (res != (SIZE_T) - 1 && dest) {
2928 SIZE_T write_cnt = res + (res < len);
2929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2934 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
2937 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
2938 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2939 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2940 // FIXME: under ASan the call below may write to freed memory and corrupt
2941 // its metadata. See
2942 // https://github.com/google/sanitizers/issues/321.
2943 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
2944 if (res != (SIZE_T)(-1) && dest && src) {
2945 // This function, and several others, may or may not write the terminating
2946 // \0 character. They write it iff they clear *src.
2947 SIZE_T write_cnt = res + !*src;
2948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2953 #define INIT_MBSTOWCS \
2954 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
2955 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
2957 #define INIT_MBSTOWCS
2960 #if SANITIZER_INTERCEPT_MBSNRTOWCS
2961 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
2962 SIZE_T len, void *ps) {
2964 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
2966 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
2967 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
2969 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
2970 // FIXME: under ASan the call below may write to freed memory and corrupt
2971 // its metadata. See
2972 // https://github.com/google/sanitizers/issues/321.
2973 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
2974 if (res != (SIZE_T)(-1) && dest && src) {
2975 SIZE_T write_cnt = res + !*src;
2976 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
2981 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
2983 #define INIT_MBSNRTOWCS
2986 #if SANITIZER_INTERCEPT_WCSTOMBS
2987 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
2989 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
2990 // FIXME: under ASan the call below may write to freed memory and corrupt
2991 // its metadata. See
2992 // https://github.com/google/sanitizers/issues/321.
2993 SIZE_T res = REAL(wcstombs)(dest, src, len);
2994 if (res != (SIZE_T) - 1 && dest) {
2995 SIZE_T write_cnt = res + (res < len);
2996 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3001 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3004 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3005 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3006 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3007 // FIXME: under ASan the call below may write to freed memory and corrupt
3008 // its metadata. See
3009 // https://github.com/google/sanitizers/issues/321.
3010 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3011 if (res != (SIZE_T) - 1 && dest && src) {
3012 SIZE_T write_cnt = res + !*src;
3013 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3018 #define INIT_WCSTOMBS \
3019 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3020 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3022 #define INIT_WCSTOMBS
3025 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3026 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3027 SIZE_T len, void *ps) {
3029 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3031 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3032 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3034 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3035 // FIXME: under ASan the call below may write to freed memory and corrupt
3036 // its metadata. See
3037 // https://github.com/google/sanitizers/issues/321.
3038 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3039 if (res != ((SIZE_T)-1) && dest && src) {
3040 SIZE_T write_cnt = res + !*src;
3041 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3046 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3048 #define INIT_WCSNRTOMBS
3052 #if SANITIZER_INTERCEPT_WCRTOMB
3053 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3055 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3056 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3057 // FIXME: under ASan the call below may write to freed memory and corrupt
3058 // its metadata. See
3059 // https://github.com/google/sanitizers/issues/321.
3060 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3061 if (res != ((SIZE_T)-1) && dest) {
3062 SIZE_T write_cnt = res;
3063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3068 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3070 #define INIT_WCRTOMB
3073 #if SANITIZER_INTERCEPT_TCGETATTR
3074 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3076 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3077 // FIXME: under ASan the call below may write to freed memory and corrupt
3078 // its metadata. See
3079 // https://github.com/google/sanitizers/issues/321.
3080 int res = REAL(tcgetattr)(fd, termios_p);
3081 if (!res && termios_p)
3082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3086 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3088 #define INIT_TCGETATTR
3091 #if SANITIZER_INTERCEPT_REALPATH
3092 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3094 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3095 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3097 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3098 // version of a versioned symbol. For realpath(), this gives us something
3099 // (called __old_realpath) that does not handle NULL in the second argument.
3100 // Handle it as part of the interceptor.
3101 char *allocated_path = nullptr;
3103 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3105 char *res = REAL(realpath)(path, resolved_path);
3106 if (allocated_path && !res) WRAP(free)(allocated_path);
3107 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3110 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3112 #define INIT_REALPATH
3115 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3116 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3118 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3119 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3120 char *res = REAL(canonicalize_file_name)(path);
3121 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3124 #define INIT_CANONICALIZE_FILE_NAME \
3125 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3127 #define INIT_CANONICALIZE_FILE_NAME
3130 #if SANITIZER_INTERCEPT_CONFSTR
3131 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3133 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3134 // FIXME: under ASan the call below may write to freed memory and corrupt
3135 // its metadata. See
3136 // https://github.com/google/sanitizers/issues/321.
3137 SIZE_T res = REAL(confstr)(name, buf, len);
3139 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3142 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3144 #define INIT_CONFSTR
3147 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3148 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3150 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3151 // FIXME: under ASan the call below may write to freed memory and corrupt
3152 // its metadata. See
3153 // https://github.com/google/sanitizers/issues/321.
3154 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3155 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3158 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3160 #define INIT_SCHED_GETAFFINITY
3163 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3164 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3166 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3167 int res = REAL(sched_getparam)(pid, param);
3168 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3171 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3173 #define INIT_SCHED_GETPARAM
3176 #if SANITIZER_INTERCEPT_STRERROR
3177 INTERCEPTOR(char *, strerror, int errnum) {
3179 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3180 char *res = REAL(strerror)(errnum);
3181 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3184 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3186 #define INIT_STRERROR
3189 #if SANITIZER_INTERCEPT_STRERROR_R
3190 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3192 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3193 // FIXME: under ASan the call below may write to freed memory and corrupt
3194 // its metadata. See
3195 // https://github.com/google/sanitizers/issues/321.
3196 char *res = REAL(strerror_r)(errnum, buf, buflen);
3197 // There are 2 versions of strerror_r:
3198 // * POSIX version returns 0 on success, negative error code on failure,
3199 // writes message to buf.
3200 // * GNU version returns message pointer, which points to either buf or some
3202 SIZE_T posix_res = (SIZE_T)res;
3203 if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
3204 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3205 // At least on OSX, buf contents are valid even when the call fails.
3206 SIZE_T sz = internal_strnlen(buf, buflen);
3207 if (sz < buflen) ++sz;
3208 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3215 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3217 #define INIT_STRERROR_R
3220 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3221 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3223 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3224 // FIXME: under ASan the call below may write to freed memory and corrupt
3225 // its metadata. See
3226 // https://github.com/google/sanitizers/issues/321.
3227 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3228 // This version always returns a null-terminated string.
3230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3233 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3235 #define INIT_XPG_STRERROR_R
3238 #if SANITIZER_INTERCEPT_SCANDIR
3239 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3240 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3241 const struct __sanitizer_dirent **);
3243 static THREADLOCAL scandir_filter_f scandir_filter;
3244 static THREADLOCAL scandir_compar_f scandir_compar;
3246 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3247 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3248 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3249 return scandir_filter(dir);
3252 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3253 const struct __sanitizer_dirent **b) {
3254 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3255 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3256 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3257 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3258 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3259 return scandir_compar(a, b);
3262 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3263 scandir_filter_f filter, scandir_compar_f compar) {
3265 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3266 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3267 scandir_filter = filter;
3268 scandir_compar = compar;
3269 // FIXME: under ASan the call below may write to freed memory and corrupt
3270 // its metadata. See
3271 // https://github.com/google/sanitizers/issues/321.
3272 int res = REAL(scandir)(dirp, namelist,
3273 filter ? wrapped_scandir_filter : nullptr,
3274 compar ? wrapped_scandir_compar : nullptr);
3275 scandir_filter = nullptr;
3276 scandir_compar = nullptr;
3277 if (namelist && res > 0) {
3278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3280 for (int i = 0; i < res; ++i)
3281 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3282 (*namelist)[i]->d_reclen);
3286 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3288 #define INIT_SCANDIR
3291 #if SANITIZER_INTERCEPT_SCANDIR64
3292 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3293 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3294 const struct __sanitizer_dirent64 **);
3296 static THREADLOCAL scandir64_filter_f scandir64_filter;
3297 static THREADLOCAL scandir64_compar_f scandir64_compar;
3299 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3300 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3301 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3302 return scandir64_filter(dir);
3305 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3306 const struct __sanitizer_dirent64 **b) {
3307 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3308 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3309 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3310 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3311 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3312 return scandir64_compar(a, b);
3315 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3316 scandir64_filter_f filter, scandir64_compar_f compar) {
3318 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3319 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3320 scandir64_filter = filter;
3321 scandir64_compar = compar;
3322 // FIXME: under ASan the call below may write to freed memory and corrupt
3323 // its metadata. See
3324 // https://github.com/google/sanitizers/issues/321.
3326 REAL(scandir64)(dirp, namelist,
3327 filter ? wrapped_scandir64_filter : nullptr,
3328 compar ? wrapped_scandir64_compar : nullptr);
3329 scandir64_filter = nullptr;
3330 scandir64_compar = nullptr;
3331 if (namelist && res > 0) {
3332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3333 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3334 for (int i = 0; i < res; ++i)
3335 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3336 (*namelist)[i]->d_reclen);
3340 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3342 #define INIT_SCANDIR64
3345 #if SANITIZER_INTERCEPT_GETGROUPS
3346 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3348 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3349 // FIXME: under ASan the call below may write to freed memory and corrupt
3350 // its metadata. See
3351 // https://github.com/google/sanitizers/issues/321.
3352 int res = REAL(getgroups)(size, lst);
3353 if (res >= 0 && lst && size > 0)
3354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3357 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3359 #define INIT_GETGROUPS
3362 #if SANITIZER_INTERCEPT_POLL
3363 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3364 __sanitizer_nfds_t nfds) {
3365 for (unsigned i = 0; i < nfds; ++i) {
3366 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3367 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3371 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3372 __sanitizer_nfds_t nfds) {
3373 for (unsigned i = 0; i < nfds; ++i)
3374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3375 sizeof(fds[i].revents));
3378 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3381 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3382 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3383 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3384 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3387 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3392 #if SANITIZER_INTERCEPT_PPOLL
3393 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3394 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3396 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3397 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3399 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3400 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3402 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3403 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3406 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3411 #if SANITIZER_INTERCEPT_WORDEXP
3412 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3414 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3415 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3416 // FIXME: under ASan the call below may write to freed memory and corrupt
3417 // its metadata. See
3418 // https://github.com/google/sanitizers/issues/321.
3419 int res = REAL(wordexp)(s, p, flags);
3421 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3424 sizeof(*p->we_wordv) * p->we_wordc);
3425 for (uptr i = 0; i < p->we_wordc; ++i) {
3426 char *w = p->we_wordv[i];
3427 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3432 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3434 #define INIT_WORDEXP
3437 #if SANITIZER_INTERCEPT_SIGWAIT
3438 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3440 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3441 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3442 // FIXME: under ASan the call below may write to freed memory and corrupt
3443 // its metadata. See
3444 // https://github.com/google/sanitizers/issues/321.
3445 int res = REAL(sigwait)(set, sig);
3446 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3449 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3451 #define INIT_SIGWAIT
3454 #if SANITIZER_INTERCEPT_SIGWAITINFO
3455 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3457 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3458 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3459 // FIXME: under ASan the call below may write to freed memory and corrupt
3460 // its metadata. See
3461 // https://github.com/google/sanitizers/issues/321.
3462 int res = REAL(sigwaitinfo)(set, info);
3463 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3466 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3468 #define INIT_SIGWAITINFO
3471 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3472 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3475 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3476 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3477 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3478 // FIXME: under ASan the call below may write to freed memory and corrupt
3479 // its metadata. See
3480 // https://github.com/google/sanitizers/issues/321.
3481 int res = REAL(sigtimedwait)(set, info, timeout);
3482 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3485 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3487 #define INIT_SIGTIMEDWAIT
3490 #if SANITIZER_INTERCEPT_SIGSETOPS
3491 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3493 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3494 // FIXME: under ASan the call below may write to freed memory and corrupt
3495 // its metadata. See
3496 // https://github.com/google/sanitizers/issues/321.
3497 int res = REAL(sigemptyset)(set);
3498 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3502 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3504 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3505 // FIXME: under ASan the call below may write to freed memory and corrupt
3506 // its metadata. See
3507 // https://github.com/google/sanitizers/issues/321.
3508 int res = REAL(sigfillset)(set);
3509 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3512 #define INIT_SIGSETOPS \
3513 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3514 COMMON_INTERCEPT_FUNCTION(sigfillset);
3516 #define INIT_SIGSETOPS
3519 #if SANITIZER_INTERCEPT_SIGPENDING
3520 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3522 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3523 // FIXME: under ASan the call below may write to freed memory and corrupt
3524 // its metadata. See
3525 // https://github.com/google/sanitizers/issues/321.
3526 int res = REAL(sigpending)(set);
3527 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3530 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3532 #define INIT_SIGPENDING
3535 #if SANITIZER_INTERCEPT_SIGPROCMASK
3536 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3537 __sanitizer_sigset_t *oldset) {
3539 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3540 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3541 // FIXME: under ASan the call below may write to freed memory and corrupt
3542 // its metadata. See
3543 // https://github.com/google/sanitizers/issues/321.
3544 int res = REAL(sigprocmask)(how, set, oldset);
3546 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3549 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3551 #define INIT_SIGPROCMASK
3554 #if SANITIZER_INTERCEPT_BACKTRACE
3555 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3557 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3558 // FIXME: under ASan the call below may write to freed memory and corrupt
3559 // its metadata. See
3560 // https://github.com/google/sanitizers/issues/321.
3561 int res = REAL(backtrace)(buffer, size);
3563 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3567 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3569 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3571 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3572 // FIXME: under ASan the call below may write to freed memory and corrupt
3573 // its metadata. See
3574 // https://github.com/google/sanitizers/issues/321.
3575 char **res = REAL(backtrace_symbols)(buffer, size);
3577 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3578 for (int i = 0; i < size; ++i)
3579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3583 #define INIT_BACKTRACE \
3584 COMMON_INTERCEPT_FUNCTION(backtrace); \
3585 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3587 #define INIT_BACKTRACE
3590 #if SANITIZER_INTERCEPT__EXIT
3591 INTERCEPTOR(void, _exit, int status) {
3593 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3594 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3595 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3596 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3597 if (status == 0) status = status1;
3598 REAL(_exit)(status);
3600 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3605 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3606 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3608 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3609 int res = REAL(pthread_mutex_lock)(m);
3610 if (res == errno_EOWNERDEAD)
3611 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3612 if (res == 0 || res == errno_EOWNERDEAD)
3613 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
3614 if (res == errno_EINVAL)
3615 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3619 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3621 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3622 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3623 int res = REAL(pthread_mutex_unlock)(m);
3624 if (res == errno_EINVAL)
3625 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3629 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3630 #define INIT_PTHREAD_MUTEX_UNLOCK \
3631 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3633 #define INIT_PTHREAD_MUTEX_LOCK
3634 #define INIT_PTHREAD_MUTEX_UNLOCK
3637 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3638 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3639 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3640 if (mnt->mnt_fsname)
3641 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3642 REAL(strlen)(mnt->mnt_fsname) + 1);
3644 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3645 REAL(strlen)(mnt->mnt_dir) + 1);
3647 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3648 REAL(strlen)(mnt->mnt_type) + 1);
3650 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3651 REAL(strlen)(mnt->mnt_opts) + 1);
3655 #if SANITIZER_INTERCEPT_GETMNTENT
3656 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3658 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3659 __sanitizer_mntent *res = REAL(getmntent)(fp);
3660 if (res) write_mntent(ctx, res);
3663 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3665 #define INIT_GETMNTENT
3668 #if SANITIZER_INTERCEPT_GETMNTENT_R
3669 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3670 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3672 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3673 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3674 if (res) write_mntent(ctx, res);
3677 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3679 #define INIT_GETMNTENT_R
3682 #if SANITIZER_INTERCEPT_STATFS
3683 INTERCEPTOR(int, statfs, char *path, void *buf) {
3685 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3686 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3687 // FIXME: under ASan the call below may write to freed memory and corrupt
3688 // its metadata. See
3689 // https://github.com/google/sanitizers/issues/321.
3690 int res = REAL(statfs)(path, buf);
3691 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3694 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3696 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3697 // FIXME: under ASan the call below may write to freed memory and corrupt
3698 // its metadata. See
3699 // https://github.com/google/sanitizers/issues/321.
3700 int res = REAL(fstatfs)(fd, buf);
3701 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3704 #define INIT_STATFS \
3705 COMMON_INTERCEPT_FUNCTION(statfs); \
3706 COMMON_INTERCEPT_FUNCTION(fstatfs);
3711 #if SANITIZER_INTERCEPT_STATFS64
3712 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3714 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3715 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3716 // FIXME: under ASan the call below may write to freed memory and corrupt
3717 // its metadata. See
3718 // https://github.com/google/sanitizers/issues/321.
3719 int res = REAL(statfs64)(path, buf);
3720 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3723 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3725 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3726 // FIXME: under ASan the call below may write to freed memory and corrupt
3727 // its metadata. See
3728 // https://github.com/google/sanitizers/issues/321.
3729 int res = REAL(fstatfs64)(fd, buf);
3730 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3733 #define INIT_STATFS64 \
3734 COMMON_INTERCEPT_FUNCTION(statfs64); \
3735 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3737 #define INIT_STATFS64
3740 #if SANITIZER_INTERCEPT_STATVFS
3741 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3743 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3744 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3745 // FIXME: under ASan the call below may write to freed memory and corrupt
3746 // its metadata. See
3747 // https://github.com/google/sanitizers/issues/321.
3748 int res = REAL(statvfs)(path, buf);
3749 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3752 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3754 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3755 // FIXME: under ASan the call below may write to freed memory and corrupt
3756 // its metadata. See
3757 // https://github.com/google/sanitizers/issues/321.
3758 int res = REAL(fstatvfs)(fd, buf);
3759 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3762 #define INIT_STATVFS \
3763 COMMON_INTERCEPT_FUNCTION(statvfs); \
3764 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3766 #define INIT_STATVFS
3769 #if SANITIZER_INTERCEPT_STATVFS64
3770 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3772 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3773 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3774 // FIXME: under ASan the call below may write to freed memory and corrupt
3775 // its metadata. See
3776 // https://github.com/google/sanitizers/issues/321.
3777 int res = REAL(statvfs64)(path, buf);
3778 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3781 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3783 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3784 // FIXME: under ASan the call below may write to freed memory and corrupt
3785 // its metadata. See
3786 // https://github.com/google/sanitizers/issues/321.
3787 int res = REAL(fstatvfs64)(fd, buf);
3788 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3791 #define INIT_STATVFS64 \
3792 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3793 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3795 #define INIT_STATVFS64
3798 #if SANITIZER_INTERCEPT_INITGROUPS
3799 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3801 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3802 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3803 int res = REAL(initgroups)(user, group);
3806 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3808 #define INIT_INITGROUPS
3811 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3812 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3814 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3815 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3816 char *res = REAL(ether_ntoa)(addr);
3817 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3820 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3822 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3823 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3824 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3825 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3828 #define INIT_ETHER_NTOA_ATON \
3829 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3830 COMMON_INTERCEPT_FUNCTION(ether_aton);
3832 #define INIT_ETHER_NTOA_ATON
3835 #if SANITIZER_INTERCEPT_ETHER_HOST
3836 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3838 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3839 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3840 // FIXME: under ASan the call below may write to freed memory and corrupt
3841 // its metadata. See
3842 // https://github.com/google/sanitizers/issues/321.
3843 int res = REAL(ether_ntohost)(hostname, addr);
3844 if (!res && hostname)
3845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3848 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3850 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3852 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3853 // FIXME: under ASan the call below may write to freed memory and corrupt
3854 // its metadata. See
3855 // https://github.com/google/sanitizers/issues/321.
3856 int res = REAL(ether_hostton)(hostname, addr);
3857 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3860 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3863 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3864 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3865 // FIXME: under ASan the call below may write to freed memory and corrupt
3866 // its metadata. See
3867 // https://github.com/google/sanitizers/issues/321.
3868 int res = REAL(ether_line)(line, addr, hostname);
3870 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3872 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3876 #define INIT_ETHER_HOST \
3877 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3878 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3879 COMMON_INTERCEPT_FUNCTION(ether_line);
3881 #define INIT_ETHER_HOST
3884 #if SANITIZER_INTERCEPT_ETHER_R
3885 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3887 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3888 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3889 // FIXME: under ASan the call below may write to freed memory and corrupt
3890 // its metadata. See
3891 // https://github.com/google/sanitizers/issues/321.
3892 char *res = REAL(ether_ntoa_r)(addr, buf);
3893 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3896 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3897 __sanitizer_ether_addr *addr) {
3899 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3900 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3901 // FIXME: under ASan the call below may write to freed memory and corrupt
3902 // its metadata. See
3903 // https://github.com/google/sanitizers/issues/321.
3904 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3905 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3908 #define INIT_ETHER_R \
3909 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3910 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3912 #define INIT_ETHER_R
3915 #if SANITIZER_INTERCEPT_SHMCTL
3916 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3918 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3919 // FIXME: under ASan the call below may write to freed memory and corrupt
3920 // its metadata. See
3921 // https://github.com/google/sanitizers/issues/321.
3922 int res = REAL(shmctl)(shmid, cmd, buf);
3925 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3926 sz = sizeof(__sanitizer_shmid_ds);
3927 else if (cmd == shmctl_ipc_info)
3928 sz = struct_shminfo_sz;
3929 else if (cmd == shmctl_shm_info)
3930 sz = struct_shm_info_sz;
3931 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3935 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3940 #if SANITIZER_INTERCEPT_RANDOM_R
3941 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
3943 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
3944 // FIXME: under ASan the call below may write to freed memory and corrupt
3945 // its metadata. See
3946 // https://github.com/google/sanitizers/issues/321.
3947 int res = REAL(random_r)(buf, result);
3949 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3952 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3954 #define INIT_RANDOM_R
3957 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
3958 // its metadata. See
3959 // https://github.com/google/sanitizers/issues/321.
3960 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
3961 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
3962 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
3963 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
3964 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
3965 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
3966 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
3967 INTERCEPTOR(int, fn, void *attr, void *r) { \
3969 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
3970 int res = REAL(fn)(attr, r); \
3971 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
3974 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
3975 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
3976 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
3977 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
3978 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
3979 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
3980 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
3981 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
3982 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
3983 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
3986 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
3987 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
3988 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
3989 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
3990 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
3991 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
3992 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
3993 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
3995 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
3996 // FIXME: under ASan the call below may write to freed memory and corrupt
3997 // its metadata. See
3998 // https://github.com/google/sanitizers/issues/321.
3999 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4001 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4002 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4007 // We may need to call the real pthread_attr_getstack from the run-time
4008 // in sanitizer_common, but we don't want to include the interception headers
4009 // there. So, just define this function here.
4010 namespace __sanitizer {
4012 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4013 return REAL(pthread_attr_getstack)(attr, addr, size);
4016 } // namespace __sanitizer
4018 #define INIT_PTHREAD_ATTR_GET \
4019 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4020 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4021 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4022 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4023 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4024 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4025 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4027 #define INIT_PTHREAD_ATTR_GET
4030 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4031 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4033 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4034 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4036 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4039 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4040 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4043 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4045 // FIXME: under ASan the call below may write to freed memory and corrupt
4046 // its metadata. See
4047 // https://github.com/google/sanitizers/issues/321.
4048 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4049 if (!res && cpusetsize && cpuset)
4050 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4054 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4055 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4057 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4060 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4061 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4062 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4063 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4065 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4068 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4069 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4070 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4071 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4073 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4076 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4077 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4078 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4079 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4081 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4084 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4085 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4086 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4087 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4089 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4092 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4093 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4094 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4095 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4097 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4100 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4101 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4102 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4103 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4105 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4108 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4109 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4110 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4111 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4113 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4116 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4117 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4118 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4119 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4121 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4124 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4125 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4126 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4127 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4129 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4132 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4133 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4134 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4135 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4137 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4140 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4141 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4142 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4143 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4145 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4148 #if SANITIZER_INTERCEPT_TMPNAM
4149 INTERCEPTOR(char *, tmpnam, char *s) {
4151 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4152 char *res = REAL(tmpnam)(s);
4155 // FIXME: under ASan the call below may write to freed memory and corrupt
4156 // its metadata. See
4157 // https://github.com/google/sanitizers/issues/321.
4158 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4160 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4164 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4169 #if SANITIZER_INTERCEPT_TMPNAM_R
4170 INTERCEPTOR(char *, tmpnam_r, char *s) {
4172 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4173 // FIXME: under ASan the call below may write to freed memory and corrupt
4174 // its metadata. See
4175 // https://github.com/google/sanitizers/issues/321.
4176 char *res = REAL(tmpnam_r)(s);
4177 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4180 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4182 #define INIT_TMPNAM_R
4185 #if SANITIZER_INTERCEPT_TTYNAME_R
4186 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4188 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4189 int res = REAL(ttyname_r)(fd, name, namesize);
4191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4194 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4196 #define INIT_TTYNAME_R
4199 #if SANITIZER_INTERCEPT_TEMPNAM
4200 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4202 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4203 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4204 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4205 char *res = REAL(tempnam)(dir, pfx);
4206 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4209 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4211 #define INIT_TEMPNAM
4214 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4215 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4217 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4218 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4219 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4220 return REAL(pthread_setname_np)(thread, name);
4222 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4224 #define INIT_PTHREAD_SETNAME_NP
4227 #if SANITIZER_INTERCEPT_SINCOS
4228 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4230 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4231 // FIXME: under ASan the call below may write to freed memory and corrupt
4232 // its metadata. See
4233 // https://github.com/google/sanitizers/issues/321.
4234 REAL(sincos)(x, sin, cos);
4235 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4236 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4238 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4240 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4241 // FIXME: under ASan the call below may write to freed memory and corrupt
4242 // its metadata. See
4243 // https://github.com/google/sanitizers/issues/321.
4244 REAL(sincosf)(x, sin, cos);
4245 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4246 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4248 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4250 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4251 // FIXME: under ASan the call below may write to freed memory and corrupt
4252 // its metadata. See
4253 // https://github.com/google/sanitizers/issues/321.
4254 REAL(sincosl)(x, sin, cos);
4255 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4256 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4258 #define INIT_SINCOS \
4259 COMMON_INTERCEPT_FUNCTION(sincos); \
4260 COMMON_INTERCEPT_FUNCTION(sincosf); \
4261 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4266 #if SANITIZER_INTERCEPT_REMQUO
4267 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4269 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4270 // FIXME: under ASan the call below may write to freed memory and corrupt
4271 // its metadata. See
4272 // https://github.com/google/sanitizers/issues/321.
4273 double res = REAL(remquo)(x, y, quo);
4274 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4277 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4279 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4280 // FIXME: under ASan the call below may write to freed memory and corrupt
4281 // its metadata. See
4282 // https://github.com/google/sanitizers/issues/321.
4283 float res = REAL(remquof)(x, y, quo);
4284 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4287 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4289 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4290 // FIXME: under ASan the call below may write to freed memory and corrupt
4291 // its metadata. See
4292 // https://github.com/google/sanitizers/issues/321.
4293 long double res = REAL(remquol)(x, y, quo);
4294 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4297 #define INIT_REMQUO \
4298 COMMON_INTERCEPT_FUNCTION(remquo); \
4299 COMMON_INTERCEPT_FUNCTION(remquof); \
4300 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4305 #if SANITIZER_INTERCEPT_LGAMMA
4307 INTERCEPTOR(double, lgamma, double x) {
4309 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4310 double res = REAL(lgamma)(x);
4311 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4314 INTERCEPTOR(float, lgammaf, float x) {
4316 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4317 float res = REAL(lgammaf)(x);
4318 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4321 INTERCEPTOR(long double, lgammal, long double x) {
4323 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4324 long double res = REAL(lgammal)(x);
4325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4328 #define INIT_LGAMMA \
4329 COMMON_INTERCEPT_FUNCTION(lgamma); \
4330 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4331 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4336 #if SANITIZER_INTERCEPT_LGAMMA_R
4337 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4339 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4340 // FIXME: under ASan the call below may write to freed memory and corrupt
4341 // its metadata. See
4342 // https://github.com/google/sanitizers/issues/321.
4343 double res = REAL(lgamma_r)(x, signp);
4344 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4347 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4349 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4350 // FIXME: under ASan the call below may write to freed memory and corrupt
4351 // its metadata. See
4352 // https://github.com/google/sanitizers/issues/321.
4353 float res = REAL(lgammaf_r)(x, signp);
4354 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4357 #define INIT_LGAMMA_R \
4358 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4359 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4361 #define INIT_LGAMMA_R
4364 #if SANITIZER_INTERCEPT_LGAMMAL_R
4365 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4367 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4368 // FIXME: under ASan the call below may write to freed memory and corrupt
4369 // its metadata. See
4370 // https://github.com/google/sanitizers/issues/321.
4371 long double res = REAL(lgammal_r)(x, signp);
4372 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4375 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4377 #define INIT_LGAMMAL_R
4380 #if SANITIZER_INTERCEPT_DRAND48_R
4381 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4383 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4384 // FIXME: under ASan the call below may write to freed memory and corrupt
4385 // its metadata. See
4386 // https://github.com/google/sanitizers/issues/321.
4387 int res = REAL(drand48_r)(buffer, result);
4388 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4391 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4393 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4394 // FIXME: under ASan the call below may write to freed memory and corrupt
4395 // its metadata. See
4396 // https://github.com/google/sanitizers/issues/321.
4397 int res = REAL(lrand48_r)(buffer, result);
4398 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4401 #define INIT_DRAND48_R \
4402 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4403 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4405 #define INIT_DRAND48_R
4408 #if SANITIZER_INTERCEPT_RAND_R
4409 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4411 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4412 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4413 return REAL(rand_r)(seedp);
4415 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4420 #if SANITIZER_INTERCEPT_GETLINE
4421 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4423 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4424 // FIXME: under ASan the call below may write to freed memory and corrupt
4425 // its metadata. See
4426 // https://github.com/google/sanitizers/issues/321.
4427 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4430 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4431 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4436 // FIXME: under ASan the call below may write to freed memory and corrupt its
4438 // https://github.com/google/sanitizers/issues/321.
4439 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4442 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4443 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4445 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4447 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4452 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4454 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4456 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4457 // with its own body.
4458 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4460 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4462 #define INIT_GETLINE \
4463 COMMON_INTERCEPT_FUNCTION(getline); \
4464 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4465 COMMON_INTERCEPT_FUNCTION(getdelim);
4467 #define INIT_GETLINE
4470 #if SANITIZER_INTERCEPT_ICONV
4471 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4472 char **outbuf, SIZE_T *outbytesleft) {
4474 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4477 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4478 if (inbuf && inbytesleft)
4479 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4481 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4482 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4483 // FIXME: under ASan the call below may write to freed memory and corrupt
4484 // its metadata. See
4485 // https://github.com/google/sanitizers/issues/321.
4486 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4487 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
4488 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4493 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4498 #if SANITIZER_INTERCEPT_TIMES
4499 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4501 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4502 // FIXME: under ASan the call below may write to freed memory and corrupt
4503 // its metadata. See
4504 // https://github.com/google/sanitizers/issues/321.
4505 __sanitizer_clock_t res = REAL(times)(tms);
4506 if (res != (__sanitizer_clock_t)-1 && tms)
4507 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4510 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4515 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4517 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4518 // If you see any crashes around this functions, there are 2 known issues with
4519 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4520 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4521 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4522 // to access thread local variables (it should not happen normally,
4523 // because sanitizers use initial-exec tls model).
4524 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4526 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4527 void *res = REAL(__tls_get_addr)(arg);
4528 uptr tls_begin, tls_end;
4529 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4530 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4532 // New DTLS block has been allocated.
4533 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4538 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4539 // mostly the same semantics as __tls_get_addr, but its presence enables
4540 // some optimizations in linker (which are safe to ignore here).
4541 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4542 visibility("default")))
4543 void *__tls_get_addr_opt(void *arg);
4545 #else // SANITIZER_S390
4546 // On s390, we have to intercept two functions here:
4547 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4548 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4549 // a proper pointer. It is used by dlsym for TLS symbols.
4550 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4551 // descriptor offset as an argument instead of a pointer. GOT address
4552 // is passed in r12, so it's necessary to write it in assembly. This is
4553 // the function used by the compiler.
4554 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
4555 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
4556 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
4557 extern "C" uptr __tls_get_offset(void *arg);
4558 extern "C" uptr __interceptor___tls_get_offset(void *arg);
4559 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4561 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4562 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
4563 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4564 void *ptr = reinterpret_cast<void *>(res + tp);
4565 uptr tls_begin, tls_end;
4566 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4567 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4569 // New DTLS block has been allocated.
4570 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4574 // We need a hidden symbol aliasing the above, so that we can jump
4575 // directly to it from the assembly below.
4576 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4577 visibility("hidden")))
4578 uptr __tls_get_addr_hidden(void *arg);
4579 // Now carefully intercept __tls_get_offset.
4582 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4583 // exports our symbol.
4584 ".weak __tls_get_offset\n"
4585 ".type __tls_get_offset, @function\n"
4586 "__tls_get_offset:\n"
4587 ".global __interceptor___tls_get_offset\n"
4588 ".type __interceptor___tls_get_offset, @function\n"
4589 "__interceptor___tls_get_offset:\n"
4591 "la %r2, 0(%r2,%r12)\n"
4592 "jg __tls_get_addr_hidden\n"
4595 "0: la %r2,0(%r2,%r12)\n"
4596 "l %r4,1f-0b(%r3)\n"
4598 "1: .long __tls_get_addr_hidden - 0b\n"
4600 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
4601 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
4602 ".type __tls_get_offset_wrapper, @function\n"
4603 "__tls_get_offset_wrapper:\n"
4610 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
4612 #endif // SANITIZER_S390
4614 #define INIT_TLS_GET_ADDR
4617 #if SANITIZER_INTERCEPT_LISTXATTR
4618 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4620 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4621 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4622 // FIXME: under ASan the call below may write to freed memory and corrupt
4623 // its metadata. See
4624 // https://github.com/google/sanitizers/issues/321.
4625 SSIZE_T res = REAL(listxattr)(path, list, size);
4626 // Here and below, size == 0 is a special case where nothing is written to the
4627 // buffer, and res contains the desired buffer size.
4628 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4631 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4633 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4634 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4635 // FIXME: under ASan the call below may write to freed memory and corrupt
4636 // its metadata. See
4637 // https://github.com/google/sanitizers/issues/321.
4638 SSIZE_T res = REAL(llistxattr)(path, list, size);
4639 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4642 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4644 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4645 // FIXME: under ASan the call below may write to freed memory and corrupt
4646 // its metadata. See
4647 // https://github.com/google/sanitizers/issues/321.
4648 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4649 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4652 #define INIT_LISTXATTR \
4653 COMMON_INTERCEPT_FUNCTION(listxattr); \
4654 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4655 COMMON_INTERCEPT_FUNCTION(flistxattr);
4657 #define INIT_LISTXATTR
4660 #if SANITIZER_INTERCEPT_GETXATTR
4661 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4664 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4665 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4666 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4667 // FIXME: under ASan the call below may write to freed memory and corrupt
4668 // its metadata. See
4669 // https://github.com/google/sanitizers/issues/321.
4670 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4671 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4674 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4677 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4678 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4679 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4680 // FIXME: under ASan the call below may write to freed memory and corrupt
4681 // its metadata. See
4682 // https://github.com/google/sanitizers/issues/321.
4683 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4684 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4687 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4690 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4691 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
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 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4696 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4699 #define INIT_GETXATTR \
4700 COMMON_INTERCEPT_FUNCTION(getxattr); \
4701 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4702 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4704 #define INIT_GETXATTR
4707 #if SANITIZER_INTERCEPT_GETRESID
4708 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4710 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4711 // FIXME: under ASan the call below may write to freed memory and corrupt
4712 // its metadata. See
4713 // https://github.com/google/sanitizers/issues/321.
4714 int res = REAL(getresuid)(ruid, euid, suid);
4716 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4717 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4718 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4722 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4724 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4725 // FIXME: under ASan the call below may write to freed memory and corrupt
4726 // its metadata. See
4727 // https://github.com/google/sanitizers/issues/321.
4728 int res = REAL(getresgid)(rgid, egid, sgid);
4730 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4731 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4732 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4736 #define INIT_GETRESID \
4737 COMMON_INTERCEPT_FUNCTION(getresuid); \
4738 COMMON_INTERCEPT_FUNCTION(getresgid);
4740 #define INIT_GETRESID
4743 #if SANITIZER_INTERCEPT_GETIFADDRS
4744 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4745 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4746 // intercept it to poison the memory again.
4747 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4749 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4750 // FIXME: under ASan the call below may write to freed memory and corrupt
4751 // its metadata. See
4752 // https://github.com/google/sanitizers/issues/321.
4753 int res = REAL(getifaddrs)(ifap);
4754 if (res == 0 && ifap) {
4755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4756 __sanitizer_ifaddrs *p = *ifap;
4758 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4760 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4761 REAL(strlen)(p->ifa_name) + 1);
4763 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4765 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4766 // On Linux this is a union, but the other member also points to a
4767 // struct sockaddr, so the following is sufficient.
4769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4770 // FIXME(smatveev): Unpoison p->ifa_data as well.
4776 #define INIT_GETIFADDRS \
4777 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4779 #define INIT_GETIFADDRS
4782 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4783 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4785 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4786 // FIXME: under ASan the call below may write to freed memory and corrupt
4787 // its metadata. See
4788 // https://github.com/google/sanitizers/issues/321.
4789 char *res = REAL(if_indextoname)(ifindex, ifname);
4791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4794 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4796 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4798 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4799 return REAL(if_nametoindex)(ifname);
4801 #define INIT_IF_INDEXTONAME \
4802 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4803 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4805 #define INIT_IF_INDEXTONAME
4808 #if SANITIZER_INTERCEPT_CAPGET
4809 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4811 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4813 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4814 // FIXME: under ASan the call below may write to freed memory and corrupt
4815 // its metadata. See
4816 // https://github.com/google/sanitizers/issues/321.
4817 int res = REAL(capget)(hdrp, datap);
4818 if (res == 0 && datap)
4819 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4820 // We can also return -1 and write to hdrp->version if the version passed in
4821 // hdrp->version is unsupported. But that's not a trivial condition to check,
4822 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4825 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4827 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4829 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4831 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4832 return REAL(capset)(hdrp, datap);
4834 #define INIT_CAPGET \
4835 COMMON_INTERCEPT_FUNCTION(capget); \
4836 COMMON_INTERCEPT_FUNCTION(capset);
4841 #if SANITIZER_INTERCEPT_AEABI_MEM
4842 DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr)
4843 DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr)
4844 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr)
4846 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4847 return WRAP(memmove)(to, from, size);
4849 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4850 return WRAP(memmove)(to, from, size);
4852 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4853 return WRAP(memmove)(to, from, size);
4855 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4856 return WRAP(memcpy)(to, from, size);
4858 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4859 return WRAP(memcpy)(to, from, size);
4861 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4862 return WRAP(memcpy)(to, from, size);
4864 // Note the argument order.
4865 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4866 return WRAP(memset)(block, c, size);
4868 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4869 return WRAP(memset)(block, c, size);
4871 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4872 return WRAP(memset)(block, c, size);
4874 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4875 return WRAP(memset)(block, 0, size);
4877 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4878 return WRAP(memset)(block, 0, size);
4880 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4881 return WRAP(memset)(block, 0, size);
4883 #define INIT_AEABI_MEM \
4884 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
4885 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4886 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4887 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
4888 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
4889 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
4890 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
4891 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
4892 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
4893 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
4894 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
4895 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4897 #define INIT_AEABI_MEM
4898 #endif // SANITIZER_INTERCEPT_AEABI_MEM
4900 #if SANITIZER_INTERCEPT___BZERO
4901 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4903 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4904 return WRAP(memset)(block, 0, size);
4906 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4908 #define INIT___BZERO
4909 #endif // SANITIZER_INTERCEPT___BZERO
4911 #if SANITIZER_INTERCEPT_FTIME
4912 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4914 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
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(ftime)(tp);
4920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4923 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4926 #endif // SANITIZER_INTERCEPT_FTIME
4928 #if SANITIZER_INTERCEPT_XDR
4929 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4930 unsigned size, int op) {
4932 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
4933 // FIXME: under ASan the call below may write to freed memory and corrupt
4934 // its metadata. See
4935 // https://github.com/google/sanitizers/issues/321.
4936 REAL(xdrmem_create)(xdrs, addr, size, op);
4937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4938 if (op == __sanitizer_XDR_ENCODE) {
4939 // It's not obvious how much data individual xdr_ routines write.
4940 // Simply unpoison the entire target buffer in advance.
4941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
4945 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
4947 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
4948 // FIXME: under ASan the call below may write to freed memory and corrupt
4949 // its metadata. See
4950 // https://github.com/google/sanitizers/issues/321.
4951 REAL(xdrstdio_create)(xdrs, file, op);
4952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4955 // FIXME: under ASan the call below may write to freed memory and corrupt
4956 // its metadata. See
4957 // https://github.com/google/sanitizers/issues/321.
4958 #define XDR_INTERCEPTOR(F, T) \
4959 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
4961 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
4962 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
4963 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
4964 int res = REAL(F)(xdrs, p); \
4965 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
4966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
4970 XDR_INTERCEPTOR(xdr_short, short)
4971 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
4972 XDR_INTERCEPTOR(xdr_int, int)
4973 XDR_INTERCEPTOR(xdr_u_int, unsigned)
4974 XDR_INTERCEPTOR(xdr_long, long)
4975 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
4976 XDR_INTERCEPTOR(xdr_hyper, long long)
4977 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
4978 XDR_INTERCEPTOR(xdr_longlong_t, long long)
4979 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
4980 XDR_INTERCEPTOR(xdr_int8_t, u8)
4981 XDR_INTERCEPTOR(xdr_uint8_t, u8)
4982 XDR_INTERCEPTOR(xdr_int16_t, u16)
4983 XDR_INTERCEPTOR(xdr_uint16_t, u16)
4984 XDR_INTERCEPTOR(xdr_int32_t, u32)
4985 XDR_INTERCEPTOR(xdr_uint32_t, u32)
4986 XDR_INTERCEPTOR(xdr_int64_t, u64)
4987 XDR_INTERCEPTOR(xdr_uint64_t, u64)
4988 XDR_INTERCEPTOR(xdr_quad_t, long long)
4989 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
4990 XDR_INTERCEPTOR(xdr_bool, bool)
4991 XDR_INTERCEPTOR(xdr_enum, int)
4992 XDR_INTERCEPTOR(xdr_char, char)
4993 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
4994 XDR_INTERCEPTOR(xdr_float, float)
4995 XDR_INTERCEPTOR(xdr_double, double)
4997 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
4998 // wrapstring, sizeof
5000 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5003 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5004 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5005 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5006 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5007 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5009 // FIXME: under ASan the call below may write to freed memory and corrupt
5010 // its metadata. See
5011 // https://github.com/google/sanitizers/issues/321.
5012 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5013 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5014 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5015 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5016 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5021 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5024 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5025 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5026 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5027 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5029 // FIXME: under ASan the call below may write to freed memory and corrupt
5030 // its metadata. See
5031 // https://github.com/google/sanitizers/issues/321.
5032 int res = REAL(xdr_string)(xdrs, p, maxsize);
5033 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5042 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5043 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5044 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5045 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5046 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5047 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5048 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5049 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5050 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5051 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5052 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5053 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5054 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5055 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5056 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5057 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5058 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5059 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5060 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5061 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5062 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5063 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5064 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5065 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5066 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5067 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5068 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5069 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5070 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5071 COMMON_INTERCEPT_FUNCTION(xdr_string);
5074 #endif // SANITIZER_INTERCEPT_XDR
5076 #if SANITIZER_INTERCEPT_TSEARCH
5077 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5078 int (*compar)(const void *, const void *)) {
5080 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5081 // FIXME: under ASan the call below may write to freed memory and corrupt
5082 // its metadata. See
5083 // https://github.com/google/sanitizers/issues/321.
5084 void *res = REAL(tsearch)(key, rootp, compar);
5085 if (res && *(void **)res == key)
5086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5089 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5091 #define INIT_TSEARCH
5094 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5095 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5096 void unpoison_file(__sanitizer_FILE *fp) {
5097 #if SANITIZER_HAS_STRUCT_FILE
5098 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5099 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5100 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5101 fp->_IO_read_end - fp->_IO_read_base);
5102 #endif // SANITIZER_HAS_STRUCT_FILE
5106 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5107 // These guys are called when a .c source is built with -O2.
5108 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5110 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5111 int res = REAL(__uflow)(fp);
5115 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5117 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5118 int res = REAL(__underflow)(fp);
5122 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5124 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5125 int res = REAL(__overflow)(fp, ch);
5129 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5131 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5132 int res = REAL(__wuflow)(fp);
5136 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5138 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5139 int res = REAL(__wunderflow)(fp);
5143 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5145 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5146 int res = REAL(__woverflow)(fp, ch);
5150 #define INIT_LIBIO_INTERNALS \
5151 COMMON_INTERCEPT_FUNCTION(__uflow); \
5152 COMMON_INTERCEPT_FUNCTION(__underflow); \
5153 COMMON_INTERCEPT_FUNCTION(__overflow); \
5154 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5155 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5156 COMMON_INTERCEPT_FUNCTION(__woverflow);
5158 #define INIT_LIBIO_INTERNALS
5161 #if SANITIZER_INTERCEPT_FOPEN
5162 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5164 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5165 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5166 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5167 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5168 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5169 if (res) unpoison_file(res);
5172 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5174 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5175 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5176 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5177 if (res) unpoison_file(res);
5180 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5181 __sanitizer_FILE *fp) {
5183 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5184 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5185 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5186 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5187 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5188 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5189 if (res) unpoison_file(res);
5192 #define INIT_FOPEN \
5193 COMMON_INTERCEPT_FUNCTION(fopen); \
5194 COMMON_INTERCEPT_FUNCTION(fdopen); \
5195 COMMON_INTERCEPT_FUNCTION(freopen);
5200 #if SANITIZER_INTERCEPT_FOPEN64
5201 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5203 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5204 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5205 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5206 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5207 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5208 if (res) unpoison_file(res);
5211 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5212 __sanitizer_FILE *fp) {
5214 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5215 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5216 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5217 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5218 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5219 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5220 if (res) unpoison_file(res);
5223 #define INIT_FOPEN64 \
5224 COMMON_INTERCEPT_FUNCTION(fopen64); \
5225 COMMON_INTERCEPT_FUNCTION(freopen64);
5227 #define INIT_FOPEN64
5230 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5231 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5233 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5234 // FIXME: under ASan the call below may write to freed memory and corrupt
5235 // its metadata. See
5236 // https://github.com/google/sanitizers/issues/321.
5237 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5239 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5240 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5242 FileMetadata file = {ptr, sizeloc};
5243 SetInterceptorMetadata(res, file);
5247 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5250 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5251 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5253 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5256 FileMetadata file = {(char **)ptr, sizeloc};
5257 SetInterceptorMetadata(res, file);
5261 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5264 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5265 // FIXME: under ASan the call below may write to freed memory and corrupt
5266 // its metadata. See
5267 // https://github.com/google/sanitizers/issues/321.
5268 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5269 if (res) unpoison_file(res);
5272 #define INIT_OPEN_MEMSTREAM \
5273 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5274 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5275 COMMON_INTERCEPT_FUNCTION(fmemopen);
5277 #define INIT_OPEN_MEMSTREAM
5280 #if SANITIZER_INTERCEPT_OBSTACK
5281 static void initialize_obstack(__sanitizer_obstack *obstack) {
5282 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5284 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5285 sizeof(*obstack->chunk));
5288 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5289 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5290 void (*free_fn)(uptr arg, void *p)) {
5292 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5294 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5295 if (res) initialize_obstack(obstack);
5298 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5299 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5301 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5303 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5304 if (res) initialize_obstack(obstack);
5307 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5309 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5310 REAL(_obstack_newchunk)(obstack, length);
5312 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5313 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5315 #define INIT_OBSTACK \
5316 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5317 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5318 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5320 #define INIT_OBSTACK
5323 #if SANITIZER_INTERCEPT_FFLUSH
5324 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5326 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5327 int res = REAL(fflush)(fp);
5328 // FIXME: handle fp == NULL
5330 const FileMetadata *m = GetInterceptorMetadata(fp);
5331 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5335 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5340 #if SANITIZER_INTERCEPT_FCLOSE
5341 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5343 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5344 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5345 const FileMetadata *m = GetInterceptorMetadata(fp);
5346 int res = REAL(fclose)(fp);
5348 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5349 DeleteInterceptorMetadata(fp);
5353 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5358 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5359 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5361 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5362 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5363 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5364 void *res = REAL(dlopen)(filename, flag);
5365 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5369 INTERCEPTOR(int, dlclose, void *handle) {
5371 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5372 int res = REAL(dlclose)(handle);
5373 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5376 #define INIT_DLOPEN_DLCLOSE \
5377 COMMON_INTERCEPT_FUNCTION(dlopen); \
5378 COMMON_INTERCEPT_FUNCTION(dlclose);
5380 #define INIT_DLOPEN_DLCLOSE
5383 #if SANITIZER_INTERCEPT_GETPASS
5384 INTERCEPTOR(char *, getpass, const char *prompt) {
5386 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5388 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5389 char *res = REAL(getpass)(prompt);
5390 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5394 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5396 #define INIT_GETPASS
5399 #if SANITIZER_INTERCEPT_TIMERFD
5400 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5403 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5405 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5406 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5407 if (res != -1 && old_value)
5408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5412 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5414 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5415 int res = REAL(timerfd_gettime)(fd, curr_value);
5416 if (res != -1 && curr_value)
5417 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5420 #define INIT_TIMERFD \
5421 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5422 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5424 #define INIT_TIMERFD
5427 #if SANITIZER_INTERCEPT_MLOCKX
5428 // Linux kernel has a bug that leads to kernel deadlock if a process
5429 // maps TBs of memory and then calls mlock().
5430 static void MlockIsUnsupported() {
5431 static atomic_uint8_t printed;
5432 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5434 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5438 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5439 MlockIsUnsupported();
5443 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5444 MlockIsUnsupported();
5448 INTERCEPTOR(int, mlockall, int flags) {
5449 MlockIsUnsupported();
5453 INTERCEPTOR(int, munlockall, void) {
5454 MlockIsUnsupported();
5458 #define INIT_MLOCKX \
5459 COMMON_INTERCEPT_FUNCTION(mlock); \
5460 COMMON_INTERCEPT_FUNCTION(munlock); \
5461 COMMON_INTERCEPT_FUNCTION(mlockall); \
5462 COMMON_INTERCEPT_FUNCTION(munlockall);
5466 #endif // SANITIZER_INTERCEPT_MLOCKX
5468 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5469 struct WrappedCookie {
5471 __sanitizer_cookie_io_functions_t real_io_funcs;
5474 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5475 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5476 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5477 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5478 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5481 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5482 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5483 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5484 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5485 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5488 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5489 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5490 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5491 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5492 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5493 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5497 static int wrapped_close(void *cookie) {
5498 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5499 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5500 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5501 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5502 InternalFree(wrapped_cookie);
5506 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5507 __sanitizer_cookie_io_functions_t io_funcs) {
5509 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5510 WrappedCookie *wrapped_cookie =
5511 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5512 wrapped_cookie->real_cookie = cookie;
5513 wrapped_cookie->real_io_funcs = io_funcs;
5514 __sanitizer_FILE *res =
5515 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5516 wrapped_seek, wrapped_close});
5520 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5522 #define INIT_FOPENCOOKIE
5523 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5525 #if SANITIZER_INTERCEPT_SEM
5526 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5528 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5529 // Workaround a bug in glibc's "old" semaphore implementation by
5530 // zero-initializing the sem_t contents. This has to be done here because
5531 // interceptors bind to the lowest symbols version by default, hitting the
5532 // buggy code path while the non-sanitized build of the same code works fine.
5533 REAL(memset)(s, 0, sizeof(*s));
5534 int res = REAL(sem_init)(s, pshared, value);
5538 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5540 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5541 int res = REAL(sem_destroy)(s);
5545 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5547 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5548 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5550 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5555 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5557 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5558 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5560 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5565 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5567 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5568 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5569 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5571 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5576 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5578 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5579 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5580 int res = REAL(sem_post)(s);
5584 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5586 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5587 int res = REAL(sem_getvalue)(s, sval);
5589 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5590 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5595 COMMON_INTERCEPT_FUNCTION(sem_init); \
5596 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5597 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5598 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5599 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5600 COMMON_INTERCEPT_FUNCTION(sem_post); \
5601 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5604 #endif // SANITIZER_INTERCEPT_SEM
5606 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5607 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5609 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5610 int res = REAL(pthread_setcancelstate)(state, oldstate);
5612 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5616 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5618 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5619 int res = REAL(pthread_setcanceltype)(type, oldtype);
5621 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5624 #define INIT_PTHREAD_SETCANCEL \
5625 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5626 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5628 #define INIT_PTHREAD_SETCANCEL
5631 #if SANITIZER_INTERCEPT_MINCORE
5632 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5634 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5635 int res = REAL(mincore)(addr, length, vec);
5637 uptr page_size = GetPageSizeCached();
5638 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5639 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5643 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5645 #define INIT_MINCORE
5648 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5649 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5650 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5653 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5654 remote_iov, riovcnt, flags);
5655 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5658 write_iovec(ctx, local_iov, liovcnt, res);
5662 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5663 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5666 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5667 remote_iov, riovcnt, flags);
5668 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5671 read_iovec(ctx, local_iov, liovcnt, res);
5674 #define INIT_PROCESS_VM_READV \
5675 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5676 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5678 #define INIT_PROCESS_VM_READV
5681 #if SANITIZER_INTERCEPT_CTERMID
5682 INTERCEPTOR(char *, ctermid, char *s) {
5684 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5685 char *res = REAL(ctermid)(s);
5687 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5691 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5693 #define INIT_CTERMID
5696 #if SANITIZER_INTERCEPT_CTERMID_R
5697 INTERCEPTOR(char *, ctermid_r, char *s) {
5699 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5700 char *res = REAL(ctermid_r)(s);
5702 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5706 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5708 #define INIT_CTERMID_R
5711 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5712 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5714 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5715 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5716 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5718 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5720 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5724 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5725 void *srcaddr, int *addrlen) {
5727 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5729 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5731 if (srcaddr) srcaddr_sz = *addrlen;
5732 (void)srcaddr_sz; // prevent "set but not used" warning
5733 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5737 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5738 Min((SIZE_T)*addrlen, srcaddr_sz));
5742 #define INIT_RECV_RECVFROM \
5743 COMMON_INTERCEPT_FUNCTION(recv); \
5744 COMMON_INTERCEPT_FUNCTION(recvfrom);
5746 #define INIT_RECV_RECVFROM
5749 #if SANITIZER_INTERCEPT_SEND_SENDTO
5750 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5752 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5754 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5755 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5757 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5758 if (common_flags()->intercept_send && res > 0)
5759 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5763 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5764 void *dstaddr, int addrlen) {
5766 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5768 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5769 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5771 // Can't check dstaddr as it may have uninitialized padding at the end.
5772 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5773 if (common_flags()->intercept_send && res > 0)
5774 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5777 #define INIT_SEND_SENDTO \
5778 COMMON_INTERCEPT_FUNCTION(send); \
5779 COMMON_INTERCEPT_FUNCTION(sendto);
5781 #define INIT_SEND_SENDTO
5784 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5785 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5787 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5788 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5789 int res = REAL(eventfd_read)(fd, value);
5791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
5792 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5796 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
5798 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
5800 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5801 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5803 int res = REAL(eventfd_write)(fd, value);
5806 #define INIT_EVENTFD_READ_WRITE \
5807 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
5808 COMMON_INTERCEPT_FUNCTION(eventfd_write)
5810 #define INIT_EVENTFD_READ_WRITE
5813 #if SANITIZER_INTERCEPT_STAT
5814 INTERCEPTOR(int, stat, const char *path, void *buf) {
5816 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
5817 if (common_flags()->intercept_stat)
5818 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5819 int res = REAL(stat)(path, buf);
5821 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5824 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
5829 #if SANITIZER_INTERCEPT___XSTAT
5830 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
5832 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
5833 if (common_flags()->intercept_stat)
5834 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5835 int res = REAL(__xstat)(version, path, buf);
5837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5840 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
5842 #define INIT___XSTAT
5845 #if SANITIZER_INTERCEPT___XSTAT64
5846 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
5848 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
5849 if (common_flags()->intercept_stat)
5850 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5851 int res = REAL(__xstat64)(version, path, buf);
5853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5856 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
5858 #define INIT___XSTAT64
5861 #if SANITIZER_INTERCEPT___LXSTAT
5862 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
5864 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
5865 if (common_flags()->intercept_stat)
5866 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5867 int res = REAL(__lxstat)(version, path, buf);
5869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5872 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
5874 #define INIT___LXSTAT
5877 #if SANITIZER_INTERCEPT___LXSTAT64
5878 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
5880 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
5881 if (common_flags()->intercept_stat)
5882 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5883 int res = REAL(__lxstat64)(version, path, buf);
5885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5888 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
5890 #define INIT___LXSTAT64
5893 // FIXME: add other *stat interceptor
5895 static void InitializeCommonInterceptors() {
5896 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
5897 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
5933 INIT_LOCALTIME_AND_FRIENDS;
5942 INIT_GETPWNAM_AND_FRIENDS;
5943 INIT_GETPWNAM_R_AND_FRIENDS;
5955 INIT_PTHREAD_GETSCHEDPARAM;
5960 INIT_GETHOSTBYNAME_R;
5961 INIT_GETHOSTBYNAME2_R;
5962 INIT_GETHOSTBYADDR_R;
5979 INIT_GET_CURRENT_DIR_NAME;
5988 INIT_CANONICALIZE_FILE_NAME;
5990 INIT_SCHED_GETAFFINITY;
5991 INIT_SCHED_GETPARAM;
5994 INIT_XPG_STRERROR_R;
6009 INIT_PTHREAD_MUTEX_LOCK;
6010 INIT_PTHREAD_MUTEX_UNLOCK;
6018 INIT_ETHER_NTOA_ATON;
6023 INIT_PTHREAD_ATTR_GET;
6024 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6025 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6026 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6027 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6028 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6029 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6030 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6031 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6032 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6033 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6034 INIT_PTHREAD_CONDATTR_GETPSHARED;
6035 INIT_PTHREAD_CONDATTR_GETCLOCK;
6036 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6041 INIT_PTHREAD_SETNAME_NP;
6057 INIT_IF_INDEXTONAME;
6064 INIT_LIBIO_INTERNALS;
6067 INIT_OPEN_MEMSTREAM;
6071 INIT_DLOPEN_DLCLOSE;
6077 INIT_PTHREAD_SETCANCEL;
6079 INIT_PROCESS_VM_READV;
6085 INIT_EVENTFD_READ_WRITE;
6090 // FIXME: add other *stat interceptors.