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 && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3356 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3358 #define INIT_GETGROUPS
3361 #if SANITIZER_INTERCEPT_POLL
3362 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3363 __sanitizer_nfds_t nfds) {
3364 for (unsigned i = 0; i < nfds; ++i) {
3365 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3366 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3370 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3371 __sanitizer_nfds_t nfds) {
3372 for (unsigned i = 0; i < nfds; ++i)
3373 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3374 sizeof(fds[i].revents));
3377 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3380 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3381 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3382 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3383 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3386 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3391 #if SANITIZER_INTERCEPT_PPOLL
3392 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3393 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3395 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3396 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3398 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3399 // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
3401 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3402 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3405 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3410 #if SANITIZER_INTERCEPT_WORDEXP
3411 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3413 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3414 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3415 // FIXME: under ASan the call below may write to freed memory and corrupt
3416 // its metadata. See
3417 // https://github.com/google/sanitizers/issues/321.
3418 int res = REAL(wordexp)(s, p, flags);
3420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3423 sizeof(*p->we_wordv) * p->we_wordc);
3424 for (uptr i = 0; i < p->we_wordc; ++i) {
3425 char *w = p->we_wordv[i];
3426 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3431 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3433 #define INIT_WORDEXP
3436 #if SANITIZER_INTERCEPT_SIGWAIT
3437 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3439 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3440 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3441 // FIXME: under ASan the call below may write to freed memory and corrupt
3442 // its metadata. See
3443 // https://github.com/google/sanitizers/issues/321.
3444 int res = REAL(sigwait)(set, sig);
3445 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3448 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3450 #define INIT_SIGWAIT
3453 #if SANITIZER_INTERCEPT_SIGWAITINFO
3454 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3456 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3457 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3458 // FIXME: under ASan the call below may write to freed memory and corrupt
3459 // its metadata. See
3460 // https://github.com/google/sanitizers/issues/321.
3461 int res = REAL(sigwaitinfo)(set, info);
3462 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3465 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3467 #define INIT_SIGWAITINFO
3470 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3471 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3474 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3475 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3476 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3477 // FIXME: under ASan the call below may write to freed memory and corrupt
3478 // its metadata. See
3479 // https://github.com/google/sanitizers/issues/321.
3480 int res = REAL(sigtimedwait)(set, info, timeout);
3481 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3484 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3486 #define INIT_SIGTIMEDWAIT
3489 #if SANITIZER_INTERCEPT_SIGSETOPS
3490 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3492 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3493 // FIXME: under ASan the call below may write to freed memory and corrupt
3494 // its metadata. See
3495 // https://github.com/google/sanitizers/issues/321.
3496 int res = REAL(sigemptyset)(set);
3497 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3501 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3503 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
3504 // FIXME: under ASan the call below may write to freed memory and corrupt
3505 // its metadata. See
3506 // https://github.com/google/sanitizers/issues/321.
3507 int res = REAL(sigfillset)(set);
3508 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3511 #define INIT_SIGSETOPS \
3512 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
3513 COMMON_INTERCEPT_FUNCTION(sigfillset);
3515 #define INIT_SIGSETOPS
3518 #if SANITIZER_INTERCEPT_SIGPENDING
3519 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
3521 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
3522 // FIXME: under ASan the call below may write to freed memory and corrupt
3523 // its metadata. See
3524 // https://github.com/google/sanitizers/issues/321.
3525 int res = REAL(sigpending)(set);
3526 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3529 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
3531 #define INIT_SIGPENDING
3534 #if SANITIZER_INTERCEPT_SIGPROCMASK
3535 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
3536 __sanitizer_sigset_t *oldset) {
3538 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
3539 // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
3540 // FIXME: under ASan the call below may write to freed memory and corrupt
3541 // its metadata. See
3542 // https://github.com/google/sanitizers/issues/321.
3543 int res = REAL(sigprocmask)(how, set, oldset);
3545 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
3548 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
3550 #define INIT_SIGPROCMASK
3553 #if SANITIZER_INTERCEPT_BACKTRACE
3554 INTERCEPTOR(int, backtrace, void **buffer, int size) {
3556 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
3557 // FIXME: under ASan the call below may write to freed memory and corrupt
3558 // its metadata. See
3559 // https://github.com/google/sanitizers/issues/321.
3560 int res = REAL(backtrace)(buffer, size);
3562 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
3566 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
3568 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
3570 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
3571 // FIXME: under ASan the call below may write to freed memory and corrupt
3572 // its metadata. See
3573 // https://github.com/google/sanitizers/issues/321.
3574 char **res = REAL(backtrace_symbols)(buffer, size);
3576 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
3577 for (int i = 0; i < size; ++i)
3578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
3582 #define INIT_BACKTRACE \
3583 COMMON_INTERCEPT_FUNCTION(backtrace); \
3584 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
3586 #define INIT_BACKTRACE
3589 #if SANITIZER_INTERCEPT__EXIT
3590 INTERCEPTOR(void, _exit, int status) {
3592 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
3593 COMMON_INTERCEPTOR_USER_CALLBACK_START();
3594 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
3595 COMMON_INTERCEPTOR_USER_CALLBACK_END();
3596 if (status == 0) status = status1;
3597 REAL(_exit)(status);
3599 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
3604 #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
3605 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
3607 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
3608 int res = REAL(pthread_mutex_lock)(m);
3609 if (res == errno_EOWNERDEAD)
3610 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
3611 if (res == 0 || res == errno_EOWNERDEAD)
3612 COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
3613 if (res == errno_EINVAL)
3614 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3618 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
3620 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
3621 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
3622 int res = REAL(pthread_mutex_unlock)(m);
3623 if (res == errno_EINVAL)
3624 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
3628 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
3629 #define INIT_PTHREAD_MUTEX_UNLOCK \
3630 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
3632 #define INIT_PTHREAD_MUTEX_LOCK
3633 #define INIT_PTHREAD_MUTEX_UNLOCK
3636 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
3637 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
3638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
3639 if (mnt->mnt_fsname)
3640 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
3641 REAL(strlen)(mnt->mnt_fsname) + 1);
3643 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
3644 REAL(strlen)(mnt->mnt_dir) + 1);
3646 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
3647 REAL(strlen)(mnt->mnt_type) + 1);
3649 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
3650 REAL(strlen)(mnt->mnt_opts) + 1);
3654 #if SANITIZER_INTERCEPT_GETMNTENT
3655 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
3657 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
3658 __sanitizer_mntent *res = REAL(getmntent)(fp);
3659 if (res) write_mntent(ctx, res);
3662 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
3664 #define INIT_GETMNTENT
3667 #if SANITIZER_INTERCEPT_GETMNTENT_R
3668 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
3669 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
3671 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
3672 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
3673 if (res) write_mntent(ctx, res);
3676 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
3678 #define INIT_GETMNTENT_R
3681 #if SANITIZER_INTERCEPT_STATFS
3682 INTERCEPTOR(int, statfs, char *path, void *buf) {
3684 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
3685 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3686 // FIXME: under ASan the call below may write to freed memory and corrupt
3687 // its metadata. See
3688 // https://github.com/google/sanitizers/issues/321.
3689 int res = REAL(statfs)(path, buf);
3690 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3693 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
3695 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
3696 // FIXME: under ASan the call below may write to freed memory and corrupt
3697 // its metadata. See
3698 // https://github.com/google/sanitizers/issues/321.
3699 int res = REAL(fstatfs)(fd, buf);
3700 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
3703 #define INIT_STATFS \
3704 COMMON_INTERCEPT_FUNCTION(statfs); \
3705 COMMON_INTERCEPT_FUNCTION(fstatfs);
3710 #if SANITIZER_INTERCEPT_STATFS64
3711 INTERCEPTOR(int, statfs64, char *path, void *buf) {
3713 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
3714 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3715 // FIXME: under ASan the call below may write to freed memory and corrupt
3716 // its metadata. See
3717 // https://github.com/google/sanitizers/issues/321.
3718 int res = REAL(statfs64)(path, buf);
3719 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3722 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
3724 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
3725 // FIXME: under ASan the call below may write to freed memory and corrupt
3726 // its metadata. See
3727 // https://github.com/google/sanitizers/issues/321.
3728 int res = REAL(fstatfs64)(fd, buf);
3729 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
3732 #define INIT_STATFS64 \
3733 COMMON_INTERCEPT_FUNCTION(statfs64); \
3734 COMMON_INTERCEPT_FUNCTION(fstatfs64);
3736 #define INIT_STATFS64
3739 #if SANITIZER_INTERCEPT_STATVFS
3740 INTERCEPTOR(int, statvfs, char *path, void *buf) {
3742 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
3743 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3744 // FIXME: under ASan the call below may write to freed memory and corrupt
3745 // its metadata. See
3746 // https://github.com/google/sanitizers/issues/321.
3747 int res = REAL(statvfs)(path, buf);
3748 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3751 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
3753 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
3754 // FIXME: under ASan the call below may write to freed memory and corrupt
3755 // its metadata. See
3756 // https://github.com/google/sanitizers/issues/321.
3757 int res = REAL(fstatvfs)(fd, buf);
3758 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
3761 #define INIT_STATVFS \
3762 COMMON_INTERCEPT_FUNCTION(statvfs); \
3763 COMMON_INTERCEPT_FUNCTION(fstatvfs);
3765 #define INIT_STATVFS
3768 #if SANITIZER_INTERCEPT_STATVFS64
3769 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
3771 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
3772 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3773 // FIXME: under ASan the call below may write to freed memory and corrupt
3774 // its metadata. See
3775 // https://github.com/google/sanitizers/issues/321.
3776 int res = REAL(statvfs64)(path, buf);
3777 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3780 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
3782 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
3783 // FIXME: under ASan the call below may write to freed memory and corrupt
3784 // its metadata. See
3785 // https://github.com/google/sanitizers/issues/321.
3786 int res = REAL(fstatvfs64)(fd, buf);
3787 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
3790 #define INIT_STATVFS64 \
3791 COMMON_INTERCEPT_FUNCTION(statvfs64); \
3792 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
3794 #define INIT_STATVFS64
3797 #if SANITIZER_INTERCEPT_INITGROUPS
3798 INTERCEPTOR(int, initgroups, char *user, u32 group) {
3800 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
3801 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
3802 int res = REAL(initgroups)(user, group);
3805 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
3807 #define INIT_INITGROUPS
3810 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
3811 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
3813 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
3814 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3815 char *res = REAL(ether_ntoa)(addr);
3816 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3819 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
3821 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
3822 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3823 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
3824 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
3827 #define INIT_ETHER_NTOA_ATON \
3828 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
3829 COMMON_INTERCEPT_FUNCTION(ether_aton);
3831 #define INIT_ETHER_NTOA_ATON
3834 #if SANITIZER_INTERCEPT_ETHER_HOST
3835 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
3837 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
3838 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3839 // FIXME: under ASan the call below may write to freed memory and corrupt
3840 // its metadata. See
3841 // https://github.com/google/sanitizers/issues/321.
3842 int res = REAL(ether_ntohost)(hostname, addr);
3843 if (!res && hostname)
3844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3847 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
3849 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
3851 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3852 // FIXME: under ASan the call below may write to freed memory and corrupt
3853 // its metadata. See
3854 // https://github.com/google/sanitizers/issues/321.
3855 int res = REAL(ether_hostton)(hostname, addr);
3856 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3859 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
3862 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
3863 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
3864 // FIXME: under ASan the call below may write to freed memory and corrupt
3865 // its metadata. See
3866 // https://github.com/google/sanitizers/issues/321.
3867 int res = REAL(ether_line)(line, addr, hostname);
3869 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
3871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
3875 #define INIT_ETHER_HOST \
3876 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
3877 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
3878 COMMON_INTERCEPT_FUNCTION(ether_line);
3880 #define INIT_ETHER_HOST
3883 #if SANITIZER_INTERCEPT_ETHER_R
3884 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
3886 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
3887 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
3888 // FIXME: under ASan the call below may write to freed memory and corrupt
3889 // its metadata. See
3890 // https://github.com/google/sanitizers/issues/321.
3891 char *res = REAL(ether_ntoa_r)(addr, buf);
3892 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3895 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
3896 __sanitizer_ether_addr *addr) {
3898 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
3899 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3900 // FIXME: under ASan the call below may write to freed memory and corrupt
3901 // its metadata. See
3902 // https://github.com/google/sanitizers/issues/321.
3903 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
3904 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
3907 #define INIT_ETHER_R \
3908 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
3909 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
3911 #define INIT_ETHER_R
3914 #if SANITIZER_INTERCEPT_SHMCTL
3915 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
3917 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
3918 // FIXME: under ASan the call below may write to freed memory and corrupt
3919 // its metadata. See
3920 // https://github.com/google/sanitizers/issues/321.
3921 int res = REAL(shmctl)(shmid, cmd, buf);
3924 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
3925 sz = sizeof(__sanitizer_shmid_ds);
3926 else if (cmd == shmctl_ipc_info)
3927 sz = struct_shminfo_sz;
3928 else if (cmd == shmctl_shm_info)
3929 sz = struct_shm_info_sz;
3930 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3934 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
3939 #if SANITIZER_INTERCEPT_RANDOM_R
3940 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
3942 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
3943 // FIXME: under ASan the call below may write to freed memory and corrupt
3944 // its metadata. See
3945 // https://github.com/google/sanitizers/issues/321.
3946 int res = REAL(random_r)(buf, result);
3948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3951 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
3953 #define INIT_RANDOM_R
3956 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
3957 // its metadata. See
3958 // https://github.com/google/sanitizers/issues/321.
3959 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
3960 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
3961 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
3962 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
3963 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
3964 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
3965 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
3966 INTERCEPTOR(int, fn, void *attr, void *r) { \
3968 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
3969 int res = REAL(fn)(attr, r); \
3970 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
3973 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
3974 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
3975 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
3976 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
3977 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
3978 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
3979 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
3980 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
3981 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
3982 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
3985 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
3986 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
3987 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
3988 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
3989 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
3990 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
3991 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
3992 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
3994 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
3995 // FIXME: under ASan the call below may write to freed memory and corrupt
3996 // its metadata. See
3997 // https://github.com/google/sanitizers/issues/321.
3998 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4000 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4001 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4006 // We may need to call the real pthread_attr_getstack from the run-time
4007 // in sanitizer_common, but we don't want to include the interception headers
4008 // there. So, just define this function here.
4009 namespace __sanitizer {
4011 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4012 return REAL(pthread_attr_getstack)(attr, addr, size);
4015 } // namespace __sanitizer
4017 #define INIT_PTHREAD_ATTR_GET \
4018 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4019 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4020 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4021 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
4022 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4023 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4024 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4026 #define INIT_PTHREAD_ATTR_GET
4029 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4030 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4032 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4033 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4035 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4038 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4039 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4042 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4044 // FIXME: under ASan the call below may write to freed memory and corrupt
4045 // its metadata. See
4046 // https://github.com/google/sanitizers/issues/321.
4047 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4048 if (!res && cpusetsize && cpuset)
4049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4053 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4054 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4056 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4059 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4060 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4061 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4062 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4064 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4067 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4068 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4069 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4070 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4072 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4075 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4076 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4077 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4078 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4080 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4083 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4084 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4085 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4086 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4088 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4091 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4092 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4093 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4094 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4096 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4099 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4100 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4101 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4102 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4104 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4107 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4108 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4109 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4110 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4112 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4115 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4116 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4117 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4118 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4120 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4123 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4124 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4125 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4126 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4128 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4131 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4132 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4133 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4134 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4136 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4139 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4140 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4141 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4142 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4144 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4147 #if SANITIZER_INTERCEPT_TMPNAM
4148 INTERCEPTOR(char *, tmpnam, char *s) {
4150 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4151 char *res = REAL(tmpnam)(s);
4154 // FIXME: under ASan the call below may write to freed memory and corrupt
4155 // its metadata. See
4156 // https://github.com/google/sanitizers/issues/321.
4157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4159 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4163 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4168 #if SANITIZER_INTERCEPT_TMPNAM_R
4169 INTERCEPTOR(char *, tmpnam_r, char *s) {
4171 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4172 // FIXME: under ASan the call below may write to freed memory and corrupt
4173 // its metadata. See
4174 // https://github.com/google/sanitizers/issues/321.
4175 char *res = REAL(tmpnam_r)(s);
4176 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4179 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4181 #define INIT_TMPNAM_R
4184 #if SANITIZER_INTERCEPT_TTYNAME_R
4185 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4187 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4188 int res = REAL(ttyname_r)(fd, name, namesize);
4190 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4193 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4195 #define INIT_TTYNAME_R
4198 #if SANITIZER_INTERCEPT_TEMPNAM
4199 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4201 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4202 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4203 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4204 char *res = REAL(tempnam)(dir, pfx);
4205 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4208 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4210 #define INIT_TEMPNAM
4213 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
4214 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4216 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4217 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4218 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4219 return REAL(pthread_setname_np)(thread, name);
4221 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4223 #define INIT_PTHREAD_SETNAME_NP
4226 #if SANITIZER_INTERCEPT_SINCOS
4227 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4229 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4230 // FIXME: under ASan the call below may write to freed memory and corrupt
4231 // its metadata. See
4232 // https://github.com/google/sanitizers/issues/321.
4233 REAL(sincos)(x, sin, cos);
4234 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4235 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4237 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4239 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4240 // FIXME: under ASan the call below may write to freed memory and corrupt
4241 // its metadata. See
4242 // https://github.com/google/sanitizers/issues/321.
4243 REAL(sincosf)(x, sin, cos);
4244 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4245 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4247 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4249 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4250 // FIXME: under ASan the call below may write to freed memory and corrupt
4251 // its metadata. See
4252 // https://github.com/google/sanitizers/issues/321.
4253 REAL(sincosl)(x, sin, cos);
4254 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4255 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4257 #define INIT_SINCOS \
4258 COMMON_INTERCEPT_FUNCTION(sincos); \
4259 COMMON_INTERCEPT_FUNCTION(sincosf); \
4260 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4265 #if SANITIZER_INTERCEPT_REMQUO
4266 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4268 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4269 // FIXME: under ASan the call below may write to freed memory and corrupt
4270 // its metadata. See
4271 // https://github.com/google/sanitizers/issues/321.
4272 double res = REAL(remquo)(x, y, quo);
4273 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4276 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4278 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4279 // FIXME: under ASan the call below may write to freed memory and corrupt
4280 // its metadata. See
4281 // https://github.com/google/sanitizers/issues/321.
4282 float res = REAL(remquof)(x, y, quo);
4283 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4286 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4288 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4289 // FIXME: under ASan the call below may write to freed memory and corrupt
4290 // its metadata. See
4291 // https://github.com/google/sanitizers/issues/321.
4292 long double res = REAL(remquol)(x, y, quo);
4293 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4296 #define INIT_REMQUO \
4297 COMMON_INTERCEPT_FUNCTION(remquo); \
4298 COMMON_INTERCEPT_FUNCTION(remquof); \
4299 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4304 #if SANITIZER_INTERCEPT_LGAMMA
4306 INTERCEPTOR(double, lgamma, double x) {
4308 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4309 double res = REAL(lgamma)(x);
4310 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4313 INTERCEPTOR(float, lgammaf, float x) {
4315 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4316 float res = REAL(lgammaf)(x);
4317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4320 INTERCEPTOR(long double, lgammal, long double x) {
4322 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4323 long double res = REAL(lgammal)(x);
4324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4327 #define INIT_LGAMMA \
4328 COMMON_INTERCEPT_FUNCTION(lgamma); \
4329 COMMON_INTERCEPT_FUNCTION(lgammaf); \
4330 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4335 #if SANITIZER_INTERCEPT_LGAMMA_R
4336 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4338 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4339 // FIXME: under ASan the call below may write to freed memory and corrupt
4340 // its metadata. See
4341 // https://github.com/google/sanitizers/issues/321.
4342 double res = REAL(lgamma_r)(x, signp);
4343 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4346 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4348 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4349 // FIXME: under ASan the call below may write to freed memory and corrupt
4350 // its metadata. See
4351 // https://github.com/google/sanitizers/issues/321.
4352 float res = REAL(lgammaf_r)(x, signp);
4353 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4356 #define INIT_LGAMMA_R \
4357 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4358 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4360 #define INIT_LGAMMA_R
4363 #if SANITIZER_INTERCEPT_LGAMMAL_R
4364 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4366 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4367 // FIXME: under ASan the call below may write to freed memory and corrupt
4368 // its metadata. See
4369 // https://github.com/google/sanitizers/issues/321.
4370 long double res = REAL(lgammal_r)(x, signp);
4371 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4374 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4376 #define INIT_LGAMMAL_R
4379 #if SANITIZER_INTERCEPT_DRAND48_R
4380 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4382 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4383 // FIXME: under ASan the call below may write to freed memory and corrupt
4384 // its metadata. See
4385 // https://github.com/google/sanitizers/issues/321.
4386 int res = REAL(drand48_r)(buffer, result);
4387 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4390 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4392 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4393 // FIXME: under ASan the call below may write to freed memory and corrupt
4394 // its metadata. See
4395 // https://github.com/google/sanitizers/issues/321.
4396 int res = REAL(lrand48_r)(buffer, result);
4397 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4400 #define INIT_DRAND48_R \
4401 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4402 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4404 #define INIT_DRAND48_R
4407 #if SANITIZER_INTERCEPT_RAND_R
4408 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4410 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4411 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4412 return REAL(rand_r)(seedp);
4414 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4419 #if SANITIZER_INTERCEPT_GETLINE
4420 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
4422 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
4423 // FIXME: under ASan the call below may write to freed memory and corrupt
4424 // its metadata. See
4425 // https://github.com/google/sanitizers/issues/321.
4426 SSIZE_T res = REAL(getline)(lineptr, n, stream);
4428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
4429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
4430 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
4435 // FIXME: under ASan the call below may write to freed memory and corrupt its
4437 // https://github.com/google/sanitizers/issues/321.
4438 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
4441 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
4442 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
4444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
4445 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
4446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
4451 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
4453 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
4455 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
4456 // with its own body.
4457 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
4459 GETDELIM_INTERCEPTOR_IMPL(getdelim)
4461 #define INIT_GETLINE \
4462 COMMON_INTERCEPT_FUNCTION(getline); \
4463 COMMON_INTERCEPT_FUNCTION(__getdelim); \
4464 COMMON_INTERCEPT_FUNCTION(getdelim);
4466 #define INIT_GETLINE
4469 #if SANITIZER_INTERCEPT_ICONV
4470 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
4471 char **outbuf, SIZE_T *outbytesleft) {
4473 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
4476 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
4477 if (inbuf && inbytesleft)
4478 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
4480 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
4481 void *outbuf_orig = outbuf ? *outbuf : nullptr;
4482 // FIXME: under ASan the call below may write to freed memory and corrupt
4483 // its metadata. See
4484 // https://github.com/google/sanitizers/issues/321.
4485 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
4486 if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
4487 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
4488 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
4492 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
4497 #if SANITIZER_INTERCEPT_TIMES
4498 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
4500 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
4501 // FIXME: under ASan the call below may write to freed memory and corrupt
4502 // its metadata. See
4503 // https://github.com/google/sanitizers/issues/321.
4504 __sanitizer_clock_t res = REAL(times)(tms);
4505 if (res != (__sanitizer_clock_t)-1 && tms)
4506 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
4509 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
4514 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
4516 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
4517 // If you see any crashes around this functions, there are 2 known issues with
4518 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
4519 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
4520 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
4521 // to access thread local variables (it should not happen normally,
4522 // because sanitizers use initial-exec tls model).
4523 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
4525 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
4526 void *res = REAL(__tls_get_addr)(arg);
4527 uptr tls_begin, tls_end;
4528 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4529 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
4531 // New DTLS block has been allocated.
4532 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4537 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
4538 // mostly the same semantics as __tls_get_addr, but its presence enables
4539 // some optimizations in linker (which are safe to ignore here).
4540 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
4541 visibility("default")))
4542 void *__tls_get_addr_opt(void *arg);
4544 #else // SANITIZER_S390
4545 // On s390, we have to intercept two functions here:
4546 // - __tls_get_addr_internal, which is a glibc-internal function that is like
4547 // the usual __tls_get_addr, but returns a TP-relative offset instead of
4548 // a proper pointer. It is used by dlsym for TLS symbols.
4549 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
4550 // descriptor offset as an argument instead of a pointer. GOT address
4551 // is passed in r12, so it's necessary to write it in assembly. This is
4552 // the function used by the compiler.
4553 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr_internal)
4554 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
4556 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
4557 uptr res = REAL(__tls_get_addr_internal)(arg);
4558 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
4559 void *ptr = reinterpret_cast<void *>(res + tp);
4560 uptr tls_begin, tls_end;
4561 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
4562 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
4564 // New DTLS block has been allocated.
4565 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
4569 // We need a protected symbol aliasing the above, so that we can jump
4570 // directly to it from the assembly below.
4571 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
4572 visibility("protected")))
4573 uptr __interceptor___tls_get_addr_internal_protected(void *arg);
4574 // Now carefully intercept __tls_get_offset.
4577 ".global __tls_get_offset\n"
4578 "__tls_get_offset:\n"
4579 // The __intercept_ version has to exist, so that gen_dynamic_list.py
4580 // exports our symbol.
4581 ".global __interceptor___tls_get_offset\n"
4582 "__interceptor___tls_get_offset:\n"
4584 "la %r2, 0(%r2,%r12)\n"
4585 "jg __interceptor___tls_get_addr_internal_protected\n"
4588 "0: la %r2,0(%r2,%r12)\n"
4589 "l %r4,1f-0b(%r3)\n"
4591 "1: .long __interceptor___tls_get_addr_internal_protected - 0b\n"
4593 ".type __tls_get_offset, @function\n"
4594 ".size __tls_get_offset, .-__tls_get_offset\n"
4596 #endif // SANITIZER_S390
4598 #define INIT_TLS_GET_ADDR
4601 #if SANITIZER_INTERCEPT_LISTXATTR
4602 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
4604 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
4605 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4606 // FIXME: under ASan the call below may write to freed memory and corrupt
4607 // its metadata. See
4608 // https://github.com/google/sanitizers/issues/321.
4609 SSIZE_T res = REAL(listxattr)(path, list, size);
4610 // Here and below, size == 0 is a special case where nothing is written to the
4611 // buffer, and res contains the desired buffer size.
4612 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4615 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
4617 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
4618 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4619 // FIXME: under ASan the call below may write to freed memory and corrupt
4620 // its metadata. See
4621 // https://github.com/google/sanitizers/issues/321.
4622 SSIZE_T res = REAL(llistxattr)(path, list, size);
4623 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4626 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
4628 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
4629 // FIXME: under ASan the call below may write to freed memory and corrupt
4630 // its metadata. See
4631 // https://github.com/google/sanitizers/issues/321.
4632 SSIZE_T res = REAL(flistxattr)(fd, list, size);
4633 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
4636 #define INIT_LISTXATTR \
4637 COMMON_INTERCEPT_FUNCTION(listxattr); \
4638 COMMON_INTERCEPT_FUNCTION(llistxattr); \
4639 COMMON_INTERCEPT_FUNCTION(flistxattr);
4641 #define INIT_LISTXATTR
4644 #if SANITIZER_INTERCEPT_GETXATTR
4645 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
4648 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
4649 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4650 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4651 // FIXME: under ASan the call below may write to freed memory and corrupt
4652 // its metadata. See
4653 // https://github.com/google/sanitizers/issues/321.
4654 SSIZE_T res = REAL(getxattr)(path, name, value, size);
4655 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4658 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
4661 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
4662 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4663 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4664 // FIXME: under ASan the call below may write to freed memory and corrupt
4665 // its metadata. See
4666 // https://github.com/google/sanitizers/issues/321.
4667 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
4668 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4671 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
4674 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
4675 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
4676 // FIXME: under ASan the call below may write to freed memory and corrupt
4677 // its metadata. See
4678 // https://github.com/google/sanitizers/issues/321.
4679 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
4680 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
4683 #define INIT_GETXATTR \
4684 COMMON_INTERCEPT_FUNCTION(getxattr); \
4685 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
4686 COMMON_INTERCEPT_FUNCTION(fgetxattr);
4688 #define INIT_GETXATTR
4691 #if SANITIZER_INTERCEPT_GETRESID
4692 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
4694 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
4695 // FIXME: under ASan the call below may write to freed memory and corrupt
4696 // its metadata. See
4697 // https://github.com/google/sanitizers/issues/321.
4698 int res = REAL(getresuid)(ruid, euid, suid);
4700 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
4701 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
4702 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
4706 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
4708 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
4709 // FIXME: under ASan the call below may write to freed memory and corrupt
4710 // its metadata. See
4711 // https://github.com/google/sanitizers/issues/321.
4712 int res = REAL(getresgid)(rgid, egid, sgid);
4714 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
4715 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
4716 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
4720 #define INIT_GETRESID \
4721 COMMON_INTERCEPT_FUNCTION(getresuid); \
4722 COMMON_INTERCEPT_FUNCTION(getresgid);
4724 #define INIT_GETRESID
4727 #if SANITIZER_INTERCEPT_GETIFADDRS
4728 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
4729 // intercept freeifaddrs(). If that ceases to be the case, we might need to
4730 // intercept it to poison the memory again.
4731 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
4733 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
4734 // FIXME: under ASan the call below may write to freed memory and corrupt
4735 // its metadata. See
4736 // https://github.com/google/sanitizers/issues/321.
4737 int res = REAL(getifaddrs)(ifap);
4738 if (res == 0 && ifap) {
4739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
4740 __sanitizer_ifaddrs *p = *ifap;
4742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
4744 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
4745 REAL(strlen)(p->ifa_name) + 1);
4747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
4749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
4750 // On Linux this is a union, but the other member also points to a
4751 // struct sockaddr, so the following is sufficient.
4753 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
4754 // FIXME(smatveev): Unpoison p->ifa_data as well.
4760 #define INIT_GETIFADDRS \
4761 COMMON_INTERCEPT_FUNCTION(getifaddrs);
4763 #define INIT_GETIFADDRS
4766 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
4767 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
4769 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
4770 // FIXME: under ASan the call below may write to freed memory and corrupt
4771 // its metadata. See
4772 // https://github.com/google/sanitizers/issues/321.
4773 char *res = REAL(if_indextoname)(ifindex, ifname);
4775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4778 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
4780 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
4782 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
4783 return REAL(if_nametoindex)(ifname);
4785 #define INIT_IF_INDEXTONAME \
4786 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
4787 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
4789 #define INIT_IF_INDEXTONAME
4792 #if SANITIZER_INTERCEPT_CAPGET
4793 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
4795 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
4797 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4798 // FIXME: under ASan the call below may write to freed memory and corrupt
4799 // its metadata. See
4800 // https://github.com/google/sanitizers/issues/321.
4801 int res = REAL(capget)(hdrp, datap);
4802 if (res == 0 && datap)
4803 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
4804 // We can also return -1 and write to hdrp->version if the version passed in
4805 // hdrp->version is unsupported. But that's not a trivial condition to check,
4806 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
4809 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
4811 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
4813 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
4815 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
4816 return REAL(capset)(hdrp, datap);
4818 #define INIT_CAPGET \
4819 COMMON_INTERCEPT_FUNCTION(capget); \
4820 COMMON_INTERCEPT_FUNCTION(capset);
4825 #if SANITIZER_INTERCEPT_AEABI_MEM
4826 DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr)
4827 DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr)
4828 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr)
4830 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
4831 return WRAP(memmove)(to, from, size);
4833 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
4834 return WRAP(memmove)(to, from, size);
4836 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
4837 return WRAP(memmove)(to, from, size);
4839 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
4840 return WRAP(memcpy)(to, from, size);
4842 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
4843 return WRAP(memcpy)(to, from, size);
4845 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
4846 return WRAP(memcpy)(to, from, size);
4848 // Note the argument order.
4849 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
4850 return WRAP(memset)(block, c, size);
4852 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
4853 return WRAP(memset)(block, c, size);
4855 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
4856 return WRAP(memset)(block, c, size);
4858 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
4859 return WRAP(memset)(block, 0, size);
4861 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
4862 return WRAP(memset)(block, 0, size);
4864 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
4865 return WRAP(memset)(block, 0, size);
4867 #define INIT_AEABI_MEM \
4868 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
4869 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
4870 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
4871 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
4872 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
4873 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
4874 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
4875 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
4876 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
4877 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
4878 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
4879 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
4881 #define INIT_AEABI_MEM
4882 #endif // SANITIZER_INTERCEPT_AEABI_MEM
4884 #if SANITIZER_INTERCEPT___BZERO
4885 DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
4887 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
4888 return WRAP(memset)(block, 0, size);
4890 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
4892 #define INIT___BZERO
4893 #endif // SANITIZER_INTERCEPT___BZERO
4895 #if SANITIZER_INTERCEPT_FTIME
4896 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
4898 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
4899 // FIXME: under ASan the call below may write to freed memory and corrupt
4900 // its metadata. See
4901 // https://github.com/google/sanitizers/issues/321.
4902 int res = REAL(ftime)(tp);
4904 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
4907 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
4910 #endif // SANITIZER_INTERCEPT_FTIME
4912 #if SANITIZER_INTERCEPT_XDR
4913 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
4914 unsigned size, int op) {
4916 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
4917 // FIXME: under ASan the call below may write to freed memory and corrupt
4918 // its metadata. See
4919 // https://github.com/google/sanitizers/issues/321.
4920 REAL(xdrmem_create)(xdrs, addr, size, op);
4921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4922 if (op == __sanitizer_XDR_ENCODE) {
4923 // It's not obvious how much data individual xdr_ routines write.
4924 // Simply unpoison the entire target buffer in advance.
4925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
4929 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
4931 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
4932 // FIXME: under ASan the call below may write to freed memory and corrupt
4933 // its metadata. See
4934 // https://github.com/google/sanitizers/issues/321.
4935 REAL(xdrstdio_create)(xdrs, file, op);
4936 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
4939 // FIXME: under ASan the call below may write to freed memory and corrupt
4940 // its metadata. See
4941 // https://github.com/google/sanitizers/issues/321.
4942 #define XDR_INTERCEPTOR(F, T) \
4943 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
4945 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
4946 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
4947 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
4948 int res = REAL(F)(xdrs, p); \
4949 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
4950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
4954 XDR_INTERCEPTOR(xdr_short, short)
4955 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
4956 XDR_INTERCEPTOR(xdr_int, int)
4957 XDR_INTERCEPTOR(xdr_u_int, unsigned)
4958 XDR_INTERCEPTOR(xdr_long, long)
4959 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
4960 XDR_INTERCEPTOR(xdr_hyper, long long)
4961 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
4962 XDR_INTERCEPTOR(xdr_longlong_t, long long)
4963 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
4964 XDR_INTERCEPTOR(xdr_int8_t, u8)
4965 XDR_INTERCEPTOR(xdr_uint8_t, u8)
4966 XDR_INTERCEPTOR(xdr_int16_t, u16)
4967 XDR_INTERCEPTOR(xdr_uint16_t, u16)
4968 XDR_INTERCEPTOR(xdr_int32_t, u32)
4969 XDR_INTERCEPTOR(xdr_uint32_t, u32)
4970 XDR_INTERCEPTOR(xdr_int64_t, u64)
4971 XDR_INTERCEPTOR(xdr_uint64_t, u64)
4972 XDR_INTERCEPTOR(xdr_quad_t, long long)
4973 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
4974 XDR_INTERCEPTOR(xdr_bool, bool)
4975 XDR_INTERCEPTOR(xdr_enum, int)
4976 XDR_INTERCEPTOR(xdr_char, char)
4977 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
4978 XDR_INTERCEPTOR(xdr_float, float)
4979 XDR_INTERCEPTOR(xdr_double, double)
4981 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
4982 // wrapstring, sizeof
4984 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
4987 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
4988 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
4989 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
4990 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
4991 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
4993 // FIXME: under ASan the call below may write to freed memory and corrupt
4994 // its metadata. See
4995 // https://github.com/google/sanitizers/issues/321.
4996 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
4997 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
4998 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4999 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5000 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5005 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5008 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5009 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5010 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5011 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5013 // FIXME: under ASan the call below may write to freed memory and corrupt
5014 // its metadata. See
5015 // https://github.com/google/sanitizers/issues/321.
5016 int res = REAL(xdr_string)(xdrs, p, maxsize);
5017 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5026 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5027 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5028 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5029 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5030 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5031 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5032 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5033 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5034 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5035 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5036 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5037 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5038 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5039 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5040 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5041 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5042 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5043 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5044 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5045 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5046 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5047 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5048 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5049 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5050 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5051 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5052 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5053 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5054 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5055 COMMON_INTERCEPT_FUNCTION(xdr_string);
5058 #endif // SANITIZER_INTERCEPT_XDR
5060 #if SANITIZER_INTERCEPT_TSEARCH
5061 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5062 int (*compar)(const void *, const void *)) {
5064 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5065 // FIXME: under ASan the call below may write to freed memory and corrupt
5066 // its metadata. See
5067 // https://github.com/google/sanitizers/issues/321.
5068 void *res = REAL(tsearch)(key, rootp, compar);
5069 if (res && *(void **)res == key)
5070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5073 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5075 #define INIT_TSEARCH
5078 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5079 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5080 void unpoison_file(__sanitizer_FILE *fp) {
5081 #if SANITIZER_HAS_STRUCT_FILE
5082 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5083 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5084 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5085 fp->_IO_read_end - fp->_IO_read_base);
5086 #endif // SANITIZER_HAS_STRUCT_FILE
5090 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5091 // These guys are called when a .c source is built with -O2.
5092 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5094 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5095 int res = REAL(__uflow)(fp);
5099 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5101 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5102 int res = REAL(__underflow)(fp);
5106 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5108 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5109 int res = REAL(__overflow)(fp, ch);
5113 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5115 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5116 int res = REAL(__wuflow)(fp);
5120 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5122 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5123 int res = REAL(__wunderflow)(fp);
5127 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5129 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5130 int res = REAL(__woverflow)(fp, ch);
5134 #define INIT_LIBIO_INTERNALS \
5135 COMMON_INTERCEPT_FUNCTION(__uflow); \
5136 COMMON_INTERCEPT_FUNCTION(__underflow); \
5137 COMMON_INTERCEPT_FUNCTION(__overflow); \
5138 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5139 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5140 COMMON_INTERCEPT_FUNCTION(__woverflow);
5142 #define INIT_LIBIO_INTERNALS
5145 #if SANITIZER_INTERCEPT_FOPEN
5146 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5148 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5149 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5150 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5151 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5152 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5153 if (res) unpoison_file(res);
5156 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5158 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5159 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5160 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5161 if (res) unpoison_file(res);
5164 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5165 __sanitizer_FILE *fp) {
5167 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5168 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5169 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5170 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5171 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5172 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5173 if (res) unpoison_file(res);
5176 #define INIT_FOPEN \
5177 COMMON_INTERCEPT_FUNCTION(fopen); \
5178 COMMON_INTERCEPT_FUNCTION(fdopen); \
5179 COMMON_INTERCEPT_FUNCTION(freopen);
5184 #if SANITIZER_INTERCEPT_FOPEN64
5185 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5187 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5188 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5189 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5190 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5191 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5192 if (res) unpoison_file(res);
5195 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5196 __sanitizer_FILE *fp) {
5198 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5199 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5200 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5201 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5202 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5203 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5204 if (res) unpoison_file(res);
5207 #define INIT_FOPEN64 \
5208 COMMON_INTERCEPT_FUNCTION(fopen64); \
5209 COMMON_INTERCEPT_FUNCTION(freopen64);
5211 #define INIT_FOPEN64
5214 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5215 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5217 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5218 // FIXME: under ASan the call below may write to freed memory and corrupt
5219 // its metadata. See
5220 // https://github.com/google/sanitizers/issues/321.
5221 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5224 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5226 FileMetadata file = {ptr, sizeloc};
5227 SetInterceptorMetadata(res, file);
5231 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5234 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5235 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5237 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5238 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5240 FileMetadata file = {(char **)ptr, sizeloc};
5241 SetInterceptorMetadata(res, file);
5245 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5248 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5249 // FIXME: under ASan the call below may write to freed memory and corrupt
5250 // its metadata. See
5251 // https://github.com/google/sanitizers/issues/321.
5252 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5253 if (res) unpoison_file(res);
5256 #define INIT_OPEN_MEMSTREAM \
5257 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5258 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5259 COMMON_INTERCEPT_FUNCTION(fmemopen);
5261 #define INIT_OPEN_MEMSTREAM
5264 #if SANITIZER_INTERCEPT_OBSTACK
5265 static void initialize_obstack(__sanitizer_obstack *obstack) {
5266 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5268 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5269 sizeof(*obstack->chunk));
5272 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5273 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5274 void (*free_fn)(uptr arg, void *p)) {
5276 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5278 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5279 if (res) initialize_obstack(obstack);
5282 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5283 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5285 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5287 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5288 if (res) initialize_obstack(obstack);
5291 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5293 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5294 REAL(_obstack_newchunk)(obstack, length);
5296 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5297 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5299 #define INIT_OBSTACK \
5300 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5301 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5302 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5304 #define INIT_OBSTACK
5307 #if SANITIZER_INTERCEPT_FFLUSH
5308 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5310 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5311 int res = REAL(fflush)(fp);
5312 // FIXME: handle fp == NULL
5314 const FileMetadata *m = GetInterceptorMetadata(fp);
5315 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5319 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5324 #if SANITIZER_INTERCEPT_FCLOSE
5325 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5327 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5328 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5329 const FileMetadata *m = GetInterceptorMetadata(fp);
5330 int res = REAL(fclose)(fp);
5332 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5333 DeleteInterceptorMetadata(fp);
5337 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5342 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5343 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5345 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5346 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5347 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5348 void *res = REAL(dlopen)(filename, flag);
5349 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5353 INTERCEPTOR(int, dlclose, void *handle) {
5355 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5356 int res = REAL(dlclose)(handle);
5357 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5360 #define INIT_DLOPEN_DLCLOSE \
5361 COMMON_INTERCEPT_FUNCTION(dlopen); \
5362 COMMON_INTERCEPT_FUNCTION(dlclose);
5364 #define INIT_DLOPEN_DLCLOSE
5367 #if SANITIZER_INTERCEPT_GETPASS
5368 INTERCEPTOR(char *, getpass, const char *prompt) {
5370 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5372 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5373 char *res = REAL(getpass)(prompt);
5374 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
5378 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
5380 #define INIT_GETPASS
5383 #if SANITIZER_INTERCEPT_TIMERFD
5384 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
5387 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
5389 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
5390 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
5391 if (res != -1 && old_value)
5392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
5396 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
5398 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
5399 int res = REAL(timerfd_gettime)(fd, curr_value);
5400 if (res != -1 && curr_value)
5401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
5404 #define INIT_TIMERFD \
5405 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
5406 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
5408 #define INIT_TIMERFD
5411 #if SANITIZER_INTERCEPT_MLOCKX
5412 // Linux kernel has a bug that leads to kernel deadlock if a process
5413 // maps TBs of memory and then calls mlock().
5414 static void MlockIsUnsupported() {
5415 static atomic_uint8_t printed;
5416 if (atomic_exchange(&printed, 1, memory_order_relaxed))
5418 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
5422 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
5423 MlockIsUnsupported();
5427 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
5428 MlockIsUnsupported();
5432 INTERCEPTOR(int, mlockall, int flags) {
5433 MlockIsUnsupported();
5437 INTERCEPTOR(int, munlockall, void) {
5438 MlockIsUnsupported();
5442 #define INIT_MLOCKX \
5443 COMMON_INTERCEPT_FUNCTION(mlock); \
5444 COMMON_INTERCEPT_FUNCTION(munlock); \
5445 COMMON_INTERCEPT_FUNCTION(mlockall); \
5446 COMMON_INTERCEPT_FUNCTION(munlockall);
5450 #endif // SANITIZER_INTERCEPT_MLOCKX
5452 #if SANITIZER_INTERCEPT_FOPENCOOKIE
5453 struct WrappedCookie {
5455 __sanitizer_cookie_io_functions_t real_io_funcs;
5458 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
5459 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5460 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5461 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
5462 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
5465 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
5466 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5467 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5468 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
5469 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
5472 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
5473 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5474 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
5475 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5476 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
5477 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
5481 static int wrapped_close(void *cookie) {
5482 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
5483 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
5484 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
5485 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
5486 InternalFree(wrapped_cookie);
5490 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
5491 __sanitizer_cookie_io_functions_t io_funcs) {
5493 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
5494 WrappedCookie *wrapped_cookie =
5495 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
5496 wrapped_cookie->real_cookie = cookie;
5497 wrapped_cookie->real_io_funcs = io_funcs;
5498 __sanitizer_FILE *res =
5499 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
5500 wrapped_seek, wrapped_close});
5504 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
5506 #define INIT_FOPENCOOKIE
5507 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
5509 #if SANITIZER_INTERCEPT_SEM
5510 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
5512 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
5513 // Workaround a bug in glibc's "old" semaphore implementation by
5514 // zero-initializing the sem_t contents. This has to be done here because
5515 // interceptors bind to the lowest symbols version by default, hitting the
5516 // buggy code path while the non-sanitized build of the same code works fine.
5517 REAL(memset)(s, 0, sizeof(*s));
5518 int res = REAL(sem_init)(s, pshared, value);
5522 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
5524 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
5525 int res = REAL(sem_destroy)(s);
5529 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
5531 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
5532 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
5534 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5539 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
5541 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
5542 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
5544 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5549 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
5551 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
5552 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
5553 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
5555 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5560 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
5562 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
5563 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
5564 int res = REAL(sem_post)(s);
5568 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
5570 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
5571 int res = REAL(sem_getvalue)(s, sval);
5573 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
5574 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
5579 COMMON_INTERCEPT_FUNCTION(sem_init); \
5580 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
5581 COMMON_INTERCEPT_FUNCTION(sem_wait); \
5582 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
5583 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
5584 COMMON_INTERCEPT_FUNCTION(sem_post); \
5585 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
5588 #endif // SANITIZER_INTERCEPT_SEM
5590 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
5591 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
5593 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
5594 int res = REAL(pthread_setcancelstate)(state, oldstate);
5596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
5600 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
5602 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
5603 int res = REAL(pthread_setcanceltype)(type, oldtype);
5605 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
5608 #define INIT_PTHREAD_SETCANCEL \
5609 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
5610 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
5612 #define INIT_PTHREAD_SETCANCEL
5615 #if SANITIZER_INTERCEPT_MINCORE
5616 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
5618 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
5619 int res = REAL(mincore)(addr, length, vec);
5621 uptr page_size = GetPageSizeCached();
5622 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
5623 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
5627 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
5629 #define INIT_MINCORE
5632 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
5633 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
5634 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5637 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
5638 remote_iov, riovcnt, flags);
5639 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
5642 write_iovec(ctx, local_iov, liovcnt, res);
5646 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
5647 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
5650 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
5651 remote_iov, riovcnt, flags);
5652 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
5655 read_iovec(ctx, local_iov, liovcnt, res);
5658 #define INIT_PROCESS_VM_READV \
5659 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
5660 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
5662 #define INIT_PROCESS_VM_READV
5665 #if SANITIZER_INTERCEPT_CTERMID
5666 INTERCEPTOR(char *, ctermid, char *s) {
5668 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
5669 char *res = REAL(ctermid)(s);
5671 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5675 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
5677 #define INIT_CTERMID
5680 #if SANITIZER_INTERCEPT_CTERMID_R
5681 INTERCEPTOR(char *, ctermid_r, char *s) {
5683 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
5684 char *res = REAL(ctermid_r)(s);
5686 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
5690 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
5692 #define INIT_CTERMID_R
5695 #if SANITIZER_INTERCEPT_RECV_RECVFROM
5696 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
5698 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
5699 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5700 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
5702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5704 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5708 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
5709 void *srcaddr, int *addrlen) {
5711 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
5713 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5715 if (srcaddr) srcaddr_sz = *addrlen;
5716 (void)srcaddr_sz; // prevent "set but not used" warning
5717 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
5719 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
5721 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
5722 Min((SIZE_T)*addrlen, srcaddr_sz));
5726 #define INIT_RECV_RECVFROM \
5727 COMMON_INTERCEPT_FUNCTION(recv); \
5728 COMMON_INTERCEPT_FUNCTION(recvfrom);
5730 #define INIT_RECV_RECVFROM
5733 #if SANITIZER_INTERCEPT_SEND_SENDTO
5734 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
5736 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
5738 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5739 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5741 SSIZE_T res = REAL(send)(fd, buf, len, flags);
5742 if (common_flags()->intercept_send && res > 0)
5743 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5747 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
5748 void *dstaddr, int addrlen) {
5750 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
5752 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5753 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5755 // Can't check dstaddr as it may have uninitialized padding at the end.
5756 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
5757 if (common_flags()->intercept_send && res > 0)
5758 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
5761 #define INIT_SEND_SENDTO \
5762 COMMON_INTERCEPT_FUNCTION(send); \
5763 COMMON_INTERCEPT_FUNCTION(sendto);
5765 #define INIT_SEND_SENDTO
5768 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
5769 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
5771 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
5772 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5773 int res = REAL(eventfd_read)(fd, value);
5775 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
5776 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
5780 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
5782 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
5784 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
5785 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
5787 int res = REAL(eventfd_write)(fd, value);
5790 #define INIT_EVENTFD_READ_WRITE \
5791 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
5792 COMMON_INTERCEPT_FUNCTION(eventfd_write)
5794 #define INIT_EVENTFD_READ_WRITE
5797 #if SANITIZER_INTERCEPT_STAT
5798 INTERCEPTOR(int, stat, const char *path, void *buf) {
5800 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
5801 if (common_flags()->intercept_stat)
5802 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5803 int res = REAL(stat)(path, buf);
5805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5808 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
5813 #if SANITIZER_INTERCEPT___XSTAT
5814 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
5816 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
5817 if (common_flags()->intercept_stat)
5818 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5819 int res = REAL(__xstat)(version, path, buf);
5821 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5824 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
5826 #define INIT___XSTAT
5829 #if SANITIZER_INTERCEPT___XSTAT64
5830 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
5832 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
5833 if (common_flags()->intercept_stat)
5834 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5835 int res = REAL(__xstat64)(version, path, buf);
5837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5840 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
5842 #define INIT___XSTAT64
5845 #if SANITIZER_INTERCEPT___LXSTAT
5846 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
5848 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
5849 if (common_flags()->intercept_stat)
5850 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5851 int res = REAL(__lxstat)(version, path, buf);
5853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
5856 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
5858 #define INIT___LXSTAT
5861 #if SANITIZER_INTERCEPT___LXSTAT64
5862 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
5864 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
5865 if (common_flags()->intercept_stat)
5866 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
5867 int res = REAL(__lxstat64)(version, path, buf);
5869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
5872 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
5874 #define INIT___LXSTAT64
5877 // FIXME: add other *stat interceptor
5879 static void InitializeCommonInterceptors() {
5880 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
5881 interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
5917 INIT_LOCALTIME_AND_FRIENDS;
5926 INIT_GETPWNAM_AND_FRIENDS;
5927 INIT_GETPWNAM_R_AND_FRIENDS;
5939 INIT_PTHREAD_GETSCHEDPARAM;
5944 INIT_GETHOSTBYNAME_R;
5945 INIT_GETHOSTBYNAME2_R;
5946 INIT_GETHOSTBYADDR_R;
5963 INIT_GET_CURRENT_DIR_NAME;
5972 INIT_CANONICALIZE_FILE_NAME;
5974 INIT_SCHED_GETAFFINITY;
5975 INIT_SCHED_GETPARAM;
5978 INIT_XPG_STRERROR_R;
5993 INIT_PTHREAD_MUTEX_LOCK;
5994 INIT_PTHREAD_MUTEX_UNLOCK;
6002 INIT_ETHER_NTOA_ATON;
6007 INIT_PTHREAD_ATTR_GET;
6008 INIT_PTHREAD_ATTR_GETINHERITSCHED;
6009 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
6010 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
6011 INIT_PTHREAD_MUTEXATTR_GETTYPE;
6012 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
6013 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
6014 INIT_PTHREAD_MUTEXATTR_GETROBUST;
6015 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
6016 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
6017 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
6018 INIT_PTHREAD_CONDATTR_GETPSHARED;
6019 INIT_PTHREAD_CONDATTR_GETCLOCK;
6020 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
6025 INIT_PTHREAD_SETNAME_NP;
6041 INIT_IF_INDEXTONAME;
6048 INIT_LIBIO_INTERNALS;
6051 INIT_OPEN_MEMSTREAM;
6055 INIT_DLOPEN_DLCLOSE;
6061 INIT_PTHREAD_SETCANCEL;
6063 INIT_PROCESS_VM_READV;
6069 INIT_EVENTFD_READ_WRITE;
6074 // FIXME: add other *stat interceptors.