1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common function interceptors for tools like AddressSanitizer,
11 // ThreadSanitizer, MemorySanitizer, etc.
13 // This file should be included into the tool's interceptor file,
14 // which has to define its own macros:
15 // COMMON_INTERCEPTOR_ENTER
16 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
17 // COMMON_INTERCEPTOR_READ_RANGE
18 // COMMON_INTERCEPTOR_WRITE_RANGE
19 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
20 // COMMON_INTERCEPTOR_DIR_ACQUIRE
21 // COMMON_INTERCEPTOR_FD_ACQUIRE
22 // COMMON_INTERCEPTOR_FD_RELEASE
23 // COMMON_INTERCEPTOR_FD_ACCESS
24 // COMMON_INTERCEPTOR_SET_THREAD_NAME
25 // COMMON_INTERCEPTOR_ON_DLOPEN
26 // COMMON_INTERCEPTOR_ON_EXIT
27 // COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
28 // COMMON_INTERCEPTOR_MUTEX_POST_LOCK
29 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
30 // COMMON_INTERCEPTOR_MUTEX_REPAIR
31 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
32 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
33 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
34 // COMMON_INTERCEPTOR_MEMSET_IMPL
35 // COMMON_INTERCEPTOR_MEMMOVE_IMPL
36 // COMMON_INTERCEPTOR_MEMCPY_IMPL
37 // COMMON_INTERCEPTOR_MMAP_IMPL
38 // COMMON_INTERCEPTOR_COPY_STRING
39 // COMMON_INTERCEPTOR_STRNDUP_IMPL
40 //===----------------------------------------------------------------------===//
42 #include "interception/interception.h"
43 #include "sanitizer_addrhashmap.h"
44 #include "sanitizer_errno.h"
45 #include "sanitizer_placement_new.h"
46 #include "sanitizer_platform_interceptors.h"
47 #include "sanitizer_symbolizer.h"
48 #include "sanitizer_tls_get_addr.h"
52 #if SANITIZER_INTERCEPTOR_HOOKS
53 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
54 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
55 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
57 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
58 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
60 #endif // SANITIZER_INTERCEPTOR_HOOKS
62 #if SANITIZER_WINDOWS && !defined(va_copy)
63 #define va_copy(dst, src) ((dst) = (src))
67 #define pthread_setname_np pthread_set_name_np
68 #define inet_aton __inet_aton
69 #define inet_pton __inet_pton
70 #define iconv __bsd_iconv
74 #define clock_getres __clock_getres50
75 #define clock_gettime __clock_gettime50
76 #define clock_settime __clock_settime50
77 #define ctime __ctime50
78 #define ctime_r __ctime_r50
79 #define devname __devname50
80 #define fgetpos __fgetpos50
81 #define fsetpos __fsetpos50
82 #define fts_children __fts_children60
83 #define fts_close __fts_close60
84 #define fts_open __fts_open60
85 #define fts_read __fts_read60
86 #define fts_set __fts_set60
87 #define getitimer __getitimer50
88 #define getmntinfo __getmntinfo13
89 #define getpwent __getpwent50
90 #define getpwnam __getpwnam50
91 #define getpwnam_r __getpwnam_r50
92 #define getpwuid __getpwuid50
93 #define getpwuid_r __getpwuid_r50
94 #define getutent __getutent50
95 #define getutxent __getutxent50
96 #define getutxid __getutxid50
97 #define getutxline __getutxline50
98 #define pututxline __pututxline50
100 #define gmtime __gmtime50
101 #define gmtime_r __gmtime_r50
102 #define localtime __locatime50
103 #define localtime_r __localtime_r50
104 #define mktime __mktime50
105 #define lstat __lstat50
106 #define opendir __opendir30
107 #define readdir __readdir30
108 #define readdir_r __readdir_r30
109 #define scandir __scandir30
110 #define setitimer __setitimer50
111 #define setlocale __setlocale50
112 #define shmctl __shmctl50
113 #define sigemptyset __sigemptyset14
114 #define sigfillset __sigfillset14
115 #define sigpending __sigpending14
116 #define sigprocmask __sigprocmask14
117 #define sigtimedwait __sigtimedwait50
118 #define stat __stat50
119 #define time __time50
120 #define times __times13
121 #define unvis __unvis50
122 #define wait3 __wait350
123 #define wait4 __wait450
124 extern const unsigned short *_ctype_tab_;
125 extern const short *_toupper_tab_;
126 extern const short *_tolower_tab_;
129 // Platform-specific options.
131 namespace __sanitizer {
132 bool PlatformHasDifferentMemcpyAndMemmove();
134 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
135 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
136 #elif SANITIZER_WINDOWS64
137 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
139 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
140 #endif // SANITIZER_MAC
142 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
143 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
146 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
147 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
150 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
151 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
154 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
155 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
158 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
159 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
162 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
163 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
166 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
167 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
170 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
171 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
174 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
175 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
178 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
179 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
182 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
183 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
186 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
187 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
190 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
191 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
194 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
195 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
196 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
199 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
200 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
203 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
204 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
205 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
207 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
208 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
209 CheckNoDeepBind(filename, flag);
212 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
213 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
216 #ifndef COMMON_INTERCEPTOR_ACQUIRE
217 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
220 #ifndef COMMON_INTERCEPTOR_RELEASE
221 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
224 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
225 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
228 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
229 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
232 #ifdef SANITIZER_NLDBL_VERSION
233 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
234 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
236 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
237 COMMON_INTERCEPT_FUNCTION(fn)
240 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
241 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
243 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
244 return internal_memset(dst, v, size); \
245 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
246 if (common_flags()->intercept_intrin) \
247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
248 return REAL(memset)(dst, v, size); \
252 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
253 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
255 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
256 return internal_memmove(dst, src, size); \
257 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
258 if (common_flags()->intercept_intrin) { \
259 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
260 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
262 return REAL(memmove)(dst, src, size); \
266 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
267 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
269 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
270 return internal_memmove(dst, src, size); \
272 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
273 if (common_flags()->intercept_intrin) { \
274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
275 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
277 return REAL(memcpy)(dst, src, size); \
281 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
282 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
284 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
287 #ifndef COMMON_INTERCEPTOR_COPY_STRING
288 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
291 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
292 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
293 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
294 uptr copy_length = internal_strnlen(s, size); \
295 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
296 if (common_flags()->intercept_strndup) { \
297 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
299 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
300 internal_memcpy(new_mem, s, copy_length); \
301 new_mem[copy_length] = '\0'; \
305 struct FileMetadata {
306 // For open_memstream().
311 struct CommonInterceptorMetadata {
321 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
323 static MetadataHashMap *interceptor_metadata_map;
326 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
327 const FileMetadata &file) {
328 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
330 h->type = CommonInterceptorMetadata::CIMT_FILE;
334 UNUSED static const FileMetadata *GetInterceptorMetadata(
335 __sanitizer_FILE *addr) {
336 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
339 if (addr && h.exists()) {
341 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
348 UNUSED static void DeleteInterceptorMetadata(void *addr) {
349 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
354 #if SANITIZER_INTERCEPT_STRLEN
355 INTERCEPTOR(SIZE_T, strlen, const char *s) {
356 // Sometimes strlen is called prior to InitializeCommonInterceptors,
357 // in which case the REAL(strlen) typically used in
358 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
360 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
361 return internal_strlen(s);
363 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
364 SIZE_T result = REAL(strlen)(s);
365 if (common_flags()->intercept_strlen)
366 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
369 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
374 #if SANITIZER_INTERCEPT_STRNLEN
375 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
377 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
378 SIZE_T length = REAL(strnlen)(s, maxlen);
379 if (common_flags()->intercept_strlen)
380 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
383 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
388 #if SANITIZER_INTERCEPT_STRNDUP
389 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
391 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
393 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
396 #endif // SANITIZER_INTERCEPT_STRNDUP
398 #if SANITIZER_INTERCEPT___STRNDUP
399 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
401 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
403 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
405 #define INIT___STRNDUP
406 #endif // SANITIZER_INTERCEPT___STRNDUP
408 #if SANITIZER_INTERCEPT_TEXTDOMAIN
409 INTERCEPTOR(char*, textdomain, const char *domainname) {
411 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
412 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
413 char *domain = REAL(textdomain)(domainname);
415 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
419 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
421 #define INIT_TEXTDOMAIN
424 #if SANITIZER_INTERCEPT_STRCMP
425 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
426 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
429 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
430 const char *s1, const char *s2, int result)
432 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
434 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
435 unsigned char c1, c2;
438 c1 = (unsigned char)s1[i];
439 c2 = (unsigned char)s2[i];
440 if (c1 != c2 || c1 == '\0') break;
442 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
443 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
444 int result = CharCmpX(c1, c2);
445 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
450 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
451 const char *s1, const char *s2, uptr n,
454 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
455 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
456 return internal_strncmp(s1, s2, size);
458 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
459 unsigned char c1 = 0, c2 = 0;
461 for (i = 0; i < size; i++) {
462 c1 = (unsigned char)s1[i];
463 c2 = (unsigned char)s2[i];
464 if (c1 != c2 || c1 == '\0') break;
468 if (common_flags()->strict_string_checks) {
469 for (; i1 < size && s1[i1]; i1++) {}
470 for (; i2 < size && s2[i2]; i2++) {}
472 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
473 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
474 int result = CharCmpX(c1, c2);
475 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
480 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
481 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
487 #if SANITIZER_INTERCEPT_STRCASECMP
488 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
489 int c1_low = ToLower(c1);
490 int c2_low = ToLower(c2);
491 return c1_low - c2_low;
494 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
495 const char *s1, const char *s2, int result)
497 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
499 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
500 unsigned char c1 = 0, c2 = 0;
503 c1 = (unsigned char)s1[i];
504 c2 = (unsigned char)s2[i];
505 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
507 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
508 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
509 int result = CharCaseCmp(c1, c2);
510 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
515 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
516 const char *s1, const char *s2, uptr size,
519 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
521 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
522 unsigned char c1 = 0, c2 = 0;
524 for (i = 0; i < size; i++) {
525 c1 = (unsigned char)s1[i];
526 c2 = (unsigned char)s2[i];
527 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
531 if (common_flags()->strict_string_checks) {
532 for (; i1 < size && s1[i1]; i1++) {}
533 for (; i2 < size && s2[i2]; i2++) {}
535 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
536 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
537 int result = CharCaseCmp(c1, c2);
538 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
539 s1, s2, size, result);
543 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
544 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
546 #define INIT_STRCASECMP
547 #define INIT_STRNCASECMP
550 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
551 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
553 uptr len1 = REAL(strlen)(s1);
554 uptr len2 = REAL(strlen)(s2);
555 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
556 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
560 #if SANITIZER_INTERCEPT_STRSTR
562 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
563 const char *s1, const char *s2, char *result)
565 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
566 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
567 return internal_strstr(s1, s2);
569 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
570 char *r = REAL(strstr)(s1, s2);
571 if (common_flags()->intercept_strstr)
572 StrstrCheck(ctx, r, s1, s2);
573 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
578 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
583 #if SANITIZER_INTERCEPT_STRCASESTR
585 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
586 const char *s1, const char *s2, char *result)
588 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
590 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
591 char *r = REAL(strcasestr)(s1, s2);
592 if (common_flags()->intercept_strstr)
593 StrstrCheck(ctx, r, s1, s2);
594 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
599 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
601 #define INIT_STRCASESTR
604 #if SANITIZER_INTERCEPT_STRTOK
606 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
608 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
609 if (!common_flags()->intercept_strtok) {
610 return REAL(strtok)(str, delimiters);
612 if (common_flags()->strict_string_checks) {
613 // If strict_string_checks is enabled, we check the whole first argument
614 // string on the first call (strtok saves this string in a static buffer
615 // for subsequent calls). We do not need to check strtok's result.
616 // As the delimiters can change, we check them every call.
617 if (str != nullptr) {
618 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
620 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
621 REAL(strlen)(delimiters) + 1);
622 return REAL(strtok)(str, delimiters);
624 // However, when strict_string_checks is disabled we cannot check the
625 // whole string on the first call. Instead, we check the result string
626 // which is guaranteed to be a NULL-terminated substring of the first
627 // argument. We also conservatively check one character of str and the
629 if (str != nullptr) {
630 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
632 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
633 char *result = REAL(strtok)(str, delimiters);
634 if (result != nullptr) {
635 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
636 } else if (str != nullptr) {
637 // No delimiter were found, it's safe to assume that the entire str was
639 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
645 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
650 #if SANITIZER_INTERCEPT_MEMMEM
651 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
652 const void *s1, SIZE_T len1, const void *s2,
653 SIZE_T len2, void *result)
655 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
658 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
659 void *r = REAL(memmem)(s1, len1, s2, len2);
660 if (common_flags()->intercept_memmem) {
661 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
662 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
664 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
665 s1, len1, s2, len2, r);
669 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
672 #endif // SANITIZER_INTERCEPT_MEMMEM
674 #if SANITIZER_INTERCEPT_STRCHR
675 INTERCEPTOR(char*, strchr, const char *s, int c) {
677 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
678 return internal_strchr(s, c);
679 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
680 char *result = REAL(strchr)(s, c);
681 if (common_flags()->intercept_strchr) {
682 // Keep strlen as macro argument, as macro may ignore it.
683 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
684 (result ? result - s : REAL(strlen)(s)) + 1);
688 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
693 #if SANITIZER_INTERCEPT_STRCHRNUL
694 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
696 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
697 char *result = REAL(strchrnul)(s, c);
698 uptr len = result - s + 1;
699 if (common_flags()->intercept_strchr)
700 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
703 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
705 #define INIT_STRCHRNUL
708 #if SANITIZER_INTERCEPT_STRRCHR
709 INTERCEPTOR(char*, strrchr, const char *s, int c) {
711 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
712 return internal_strrchr(s, c);
713 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
714 if (common_flags()->intercept_strchr)
715 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
716 return REAL(strrchr)(s, c);
718 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
723 #if SANITIZER_INTERCEPT_STRSPN
724 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
726 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
727 SIZE_T r = REAL(strspn)(s1, s2);
728 if (common_flags()->intercept_strspn) {
729 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
730 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
735 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
737 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
738 SIZE_T r = REAL(strcspn)(s1, s2);
739 if (common_flags()->intercept_strspn) {
740 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
741 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
746 #define INIT_STRSPN \
747 COMMON_INTERCEPT_FUNCTION(strspn); \
748 COMMON_INTERCEPT_FUNCTION(strcspn);
753 #if SANITIZER_INTERCEPT_STRPBRK
754 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
756 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
757 char *r = REAL(strpbrk)(s1, s2);
758 if (common_flags()->intercept_strpbrk) {
759 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
760 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
761 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
766 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
771 #if SANITIZER_INTERCEPT_MEMSET
772 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
774 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
777 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
782 #if SANITIZER_INTERCEPT_MEMMOVE
783 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
785 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
788 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
793 #if SANITIZER_INTERCEPT_MEMCPY
794 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
795 // On OS X, calling internal_memcpy here will cause memory corruptions,
796 // because memcpy and memmove are actually aliases of the same
797 // implementation. We need to use internal_memmove here.
798 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
799 // due to memcpy being an alias of memmove on OS X.
801 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
802 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
804 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
808 #define INIT_MEMCPY \
810 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
811 COMMON_INTERCEPT_FUNCTION(memcpy); \
813 ASSIGN_REAL(memcpy, memmove); \
815 CHECK(REAL(memcpy)); \
822 #if SANITIZER_INTERCEPT_MEMCMP
824 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
825 const void *s1, const void *s2, uptr n,
828 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
829 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
830 return internal_memcmp(a1, a2, size);
832 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
833 if (common_flags()->intercept_memcmp) {
834 if (common_flags()->strict_memcmp) {
835 // Check the entire regions even if the first bytes of the buffers are
837 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
838 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
839 // Fallthrough to REAL(memcmp) below.
841 unsigned char c1 = 0, c2 = 0;
842 const unsigned char *s1 = (const unsigned char*)a1;
843 const unsigned char *s2 = (const unsigned char*)a2;
845 for (i = 0; i < size; i++) {
850 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
851 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
852 int r = CharCmpX(c1, c2);
853 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
858 int result = REAL(memcmp(a1, a2, size));
859 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
864 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
869 #if SANITIZER_INTERCEPT_MEMCHR
870 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
871 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
872 return internal_memchr(s, c, n);
874 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
875 #if SANITIZER_WINDOWS
878 res = REAL(memchr)(s, c, n);
880 res = internal_memchr(s, c, n);
883 void *res = REAL(memchr)(s, c, n);
885 uptr len = res ? (char *)res - (const char *)s + 1 : n;
886 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
890 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
895 #if SANITIZER_INTERCEPT_MEMRCHR
896 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
898 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
899 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
900 return REAL(memrchr)(s, c, n);
903 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
908 #if SANITIZER_INTERCEPT_FREXP
909 INTERCEPTOR(double, frexp, double x, int *exp) {
911 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
912 // Assuming frexp() always writes to |exp|.
913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
914 double res = REAL(frexp)(x, exp);
918 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
921 #endif // SANITIZER_INTERCEPT_FREXP
923 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
924 INTERCEPTOR(float, frexpf, float x, int *exp) {
926 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
927 // FIXME: under ASan the call below may write to freed memory and corrupt
929 // https://github.com/google/sanitizers/issues/321.
930 float res = REAL(frexpf)(x, exp);
931 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
935 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
937 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
938 // FIXME: under ASan the call below may write to freed memory and corrupt
940 // https://github.com/google/sanitizers/issues/321.
941 long double res = REAL(frexpl)(x, exp);
942 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
946 #define INIT_FREXPF_FREXPL \
947 COMMON_INTERCEPT_FUNCTION(frexpf); \
948 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
950 #define INIT_FREXPF_FREXPL
951 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
954 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
955 SIZE_T iovlen, SIZE_T maxlen) {
956 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
957 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
958 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
963 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
964 SIZE_T iovlen, SIZE_T maxlen) {
965 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
966 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
967 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
968 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
974 #if SANITIZER_INTERCEPT_READ
975 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
977 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
978 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
979 // FIXME: under ASan the call below may write to freed memory and corrupt
981 // https://github.com/google/sanitizers/issues/321.
982 SSIZE_T res = REAL(read)(fd, ptr, count);
983 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
984 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
987 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
992 #if SANITIZER_INTERCEPT_FREAD
993 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
994 // libc file streams can call user-supplied functions, see fopencookie.
996 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
997 // FIXME: under ASan the call below may write to freed memory and corrupt
999 // https://github.com/google/sanitizers/issues/321.
1000 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
1001 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
1004 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1009 #if SANITIZER_INTERCEPT_PREAD
1010 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1012 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1013 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1014 // FIXME: under ASan the call below may write to freed memory and corrupt
1015 // its metadata. See
1016 // https://github.com/google/sanitizers/issues/321.
1017 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1018 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1019 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1022 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1027 #if SANITIZER_INTERCEPT_PREAD64
1028 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1030 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1031 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1032 // FIXME: under ASan the call below may write to freed memory and corrupt
1033 // its metadata. See
1034 // https://github.com/google/sanitizers/issues/321.
1035 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1036 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1037 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1040 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1042 #define INIT_PREAD64
1045 #if SANITIZER_INTERCEPT_READV
1046 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1049 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1050 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1051 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1052 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1053 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1056 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1061 #if SANITIZER_INTERCEPT_PREADV
1062 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1065 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1066 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1067 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1068 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1069 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1072 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1077 #if SANITIZER_INTERCEPT_PREADV64
1078 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1081 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1082 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1083 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1084 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1085 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1088 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1090 #define INIT_PREADV64
1093 #if SANITIZER_INTERCEPT_WRITE
1094 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1096 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1097 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1098 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1099 SSIZE_T res = REAL(write)(fd, ptr, count);
1100 // FIXME: this check should be _before_ the call to REAL(write), not after
1101 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1104 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1109 #if SANITIZER_INTERCEPT_FWRITE
1110 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1111 // libc file streams can call user-supplied functions, see fopencookie.
1113 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1114 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1115 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1118 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1123 #if SANITIZER_INTERCEPT_PWRITE
1124 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1126 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1127 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1128 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1129 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1130 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1133 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1138 #if SANITIZER_INTERCEPT_PWRITE64
1139 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1142 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1143 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1144 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1145 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1146 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1149 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1151 #define INIT_PWRITE64
1154 #if SANITIZER_INTERCEPT_WRITEV
1155 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1158 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1159 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1160 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1161 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1162 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1165 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1170 #if SANITIZER_INTERCEPT_PWRITEV
1171 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1174 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1175 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1176 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1177 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1178 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1181 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1183 #define INIT_PWRITEV
1186 #if SANITIZER_INTERCEPT_PWRITEV64
1187 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1190 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1191 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1192 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1193 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1194 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1197 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1199 #define INIT_PWRITEV64
1202 #if SANITIZER_INTERCEPT_FGETS
1203 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1204 // libc file streams can call user-supplied functions, see fopencookie.
1206 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1207 // FIXME: under ASan the call below may write to freed memory and corrupt
1208 // its metadata. See
1209 // https://github.com/google/sanitizers/issues/321.
1210 char *res = REAL(fgets)(s, size, file);
1212 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
1215 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1220 #if SANITIZER_INTERCEPT_FPUTS
1221 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1222 // libc file streams can call user-supplied functions, see fopencookie.
1224 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1225 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1226 return REAL(fputs)(s, file);
1228 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1233 #if SANITIZER_INTERCEPT_PUTS
1234 INTERCEPTOR(int, puts, char *s) {
1235 // libc file streams can call user-supplied functions, see fopencookie.
1237 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1238 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1239 return REAL(puts)(s);
1241 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1246 #if SANITIZER_INTERCEPT_PRCTL
1247 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1248 unsigned long arg3, // NOLINT
1249 unsigned long arg4, unsigned long arg5) { // NOLINT
1251 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1252 static const int PR_SET_NAME = 15;
1253 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1254 if (option == PR_SET_NAME) {
1256 internal_strncpy(buff, (char *)arg2, 15);
1258 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1262 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1265 #endif // SANITIZER_INTERCEPT_PRCTL
1267 #if SANITIZER_INTERCEPT_TIME
1268 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1270 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1271 unsigned long local_t;
1272 unsigned long res = REAL(time)(&local_t);
1273 if (t && res != (unsigned long)-1) {
1274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1279 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1282 #endif // SANITIZER_INTERCEPT_TIME
1284 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1285 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1286 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1287 #if !SANITIZER_SOLARIS
1289 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1290 // can point to shared memory and tsan would report a data race.
1291 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1292 REAL(strlen(tm->tm_zone)) + 1);
1296 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1298 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1299 __sanitizer_tm *res = REAL(localtime)(timep);
1301 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1302 unpoison_tm(ctx, res);
1306 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1308 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1309 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1311 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1312 unpoison_tm(ctx, res);
1316 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1318 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1319 __sanitizer_tm *res = REAL(gmtime)(timep);
1321 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1322 unpoison_tm(ctx, res);
1326 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1328 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1329 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1331 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1332 unpoison_tm(ctx, res);
1336 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1338 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1339 // FIXME: under ASan the call below may write to freed memory and corrupt
1340 // its metadata. See
1341 // https://github.com/google/sanitizers/issues/321.
1342 char *res = REAL(ctime)(timep);
1344 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1349 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1351 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1352 // FIXME: under ASan the call below may write to freed memory and corrupt
1353 // its metadata. See
1354 // https://github.com/google/sanitizers/issues/321.
1355 char *res = REAL(ctime_r)(timep, result);
1357 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1362 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1364 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1365 // FIXME: under ASan the call below may write to freed memory and corrupt
1366 // its metadata. See
1367 // https://github.com/google/sanitizers/issues/321.
1368 char *res = REAL(asctime)(tm);
1370 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1371 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1375 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1377 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1378 // FIXME: under ASan the call below may write to freed memory and corrupt
1379 // its metadata. See
1380 // https://github.com/google/sanitizers/issues/321.
1381 char *res = REAL(asctime_r)(tm, result);
1383 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1384 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1388 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1390 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1391 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1392 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1393 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1394 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1395 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1396 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1397 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1398 long res = REAL(mktime)(tm);
1399 if (res != -1) unpoison_tm(ctx, tm);
1402 #define INIT_LOCALTIME_AND_FRIENDS \
1403 COMMON_INTERCEPT_FUNCTION(localtime); \
1404 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1405 COMMON_INTERCEPT_FUNCTION(gmtime); \
1406 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1407 COMMON_INTERCEPT_FUNCTION(ctime); \
1408 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1409 COMMON_INTERCEPT_FUNCTION(asctime); \
1410 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1411 COMMON_INTERCEPT_FUNCTION(mktime);
1413 #define INIT_LOCALTIME_AND_FRIENDS
1414 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1416 #if SANITIZER_INTERCEPT_STRPTIME
1417 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1419 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1421 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1422 // FIXME: under ASan the call below may write to freed memory and corrupt
1423 // its metadata. See
1424 // https://github.com/google/sanitizers/issues/321.
1425 char *res = REAL(strptime)(s, format, tm);
1426 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1428 // Do not call unpoison_tm here, because strptime does not, in fact,
1429 // initialize the entire struct tm. For example, tm_zone pointer is left
1431 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1435 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1437 #define INIT_STRPTIME
1440 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1441 #include "sanitizer_common_interceptors_format.inc"
1443 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1447 va_start(ap, format); \
1448 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1449 int res = WRAP(vname)(__VA_ARGS__, ap); \
1456 #if SANITIZER_INTERCEPT_SCANF
1458 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1461 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1464 int res = REAL(vname)(__VA_ARGS__); \
1466 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1471 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1472 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1474 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1475 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1477 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1478 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1480 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1481 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1482 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1484 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1486 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1488 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1489 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1490 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1492 INTERCEPTOR(int, scanf, const char *format, ...)
1493 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1495 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1496 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1498 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1499 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1501 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1502 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1503 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1505 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1506 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1508 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1509 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1514 #if SANITIZER_INTERCEPT_SCANF
1515 #define INIT_SCANF \
1516 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1517 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1518 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1519 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1520 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1521 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1526 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1527 #define INIT_ISOC99_SCANF \
1528 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1529 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1530 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1531 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1532 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1533 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1535 #define INIT_ISOC99_SCANF
1538 #if SANITIZER_INTERCEPT_PRINTF
1540 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1542 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1546 #define VPRINTF_INTERCEPTOR_RETURN() \
1549 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1551 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1552 if (common_flags()->check_printf) \
1553 printf_common(ctx, format, aq); \
1554 int res = REAL(vname)(__VA_ARGS__); \
1555 VPRINTF_INTERCEPTOR_RETURN(); \
1559 // FIXME: under ASan the REAL() call below may write to freed memory and
1560 // corrupt its metadata. See
1561 // https://github.com/google/sanitizers/issues/321.
1562 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1564 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1565 if (common_flags()->check_printf) { \
1566 printf_common(ctx, format, aq); \
1568 int res = REAL(vname)(str, __VA_ARGS__); \
1570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1572 VPRINTF_INTERCEPTOR_RETURN(); \
1576 // FIXME: under ASan the REAL() call below may write to freed memory and
1577 // corrupt its metadata. See
1578 // https://github.com/google/sanitizers/issues/321.
1579 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1581 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1582 if (common_flags()->check_printf) { \
1583 printf_common(ctx, format, aq); \
1585 int res = REAL(vname)(str, size, __VA_ARGS__); \
1587 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1589 VPRINTF_INTERCEPTOR_RETURN(); \
1593 // FIXME: under ASan the REAL() call below may write to freed memory and
1594 // corrupt its metadata. See
1595 // https://github.com/google/sanitizers/issues/321.
1596 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1598 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1599 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1600 if (common_flags()->check_printf) { \
1601 printf_common(ctx, format, aq); \
1603 int res = REAL(vname)(strp, __VA_ARGS__); \
1605 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1607 VPRINTF_INTERCEPTOR_RETURN(); \
1611 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1612 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1614 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1616 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1618 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1620 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1622 #if SANITIZER_INTERCEPT___PRINTF_CHK
1623 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1624 SIZE_T size_to, const char *format, va_list ap)
1625 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1628 #if SANITIZER_INTERCEPT_PRINTF_L
1629 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1630 const char *format, va_list ap)
1631 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1633 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1634 const char *format, ...)
1635 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1636 #endif // SANITIZER_INTERCEPT_PRINTF_L
1638 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1639 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1641 #if SANITIZER_INTERCEPT___PRINTF_CHK
1642 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1643 const char *format, va_list ap)
1644 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1647 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1648 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1650 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1651 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1652 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1654 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1655 const char *format, va_list ap)
1656 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1658 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1660 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1662 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1664 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1667 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1669 INTERCEPTOR(int, printf, const char *format, ...)
1670 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1672 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1673 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1675 #if SANITIZER_INTERCEPT___PRINTF_CHK
1676 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1677 const char *format, ...)
1678 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1681 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1682 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1684 #if SANITIZER_INTERCEPT___PRINTF_CHK
1685 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1686 const char *format, ...) // NOLINT
1687 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format) // NOLINT
1690 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1691 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1693 #if SANITIZER_INTERCEPT___PRINTF_CHK
1694 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1695 SIZE_T size_to, const char *format, ...) // NOLINT
1696 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format) // NOLINT
1699 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1700 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1702 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1703 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1704 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1706 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1708 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1710 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1711 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1713 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1714 const char *format, ...)
1715 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1718 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1720 #endif // SANITIZER_INTERCEPT_PRINTF
1722 #if SANITIZER_INTERCEPT_PRINTF
1723 #define INIT_PRINTF \
1724 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1725 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1726 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1727 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1728 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1729 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1730 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1731 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1732 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1733 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1738 #if SANITIZER_INTERCEPT___PRINTF_CHK
1739 #define INIT___PRINTF_CHK \
1740 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1741 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1742 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1743 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1744 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1746 #define INIT___PRINTF_CHK
1749 #if SANITIZER_INTERCEPT_PRINTF_L
1750 #define INIT_PRINTF_L \
1751 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1752 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1754 #define INIT_PRINTF_L
1757 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1758 #define INIT_ISOC99_PRINTF \
1759 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1760 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1761 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1762 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1763 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1764 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1765 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1766 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1768 #define INIT_ISOC99_PRINTF
1771 #if SANITIZER_INTERCEPT_IOCTL
1772 #include "sanitizer_common_interceptors_ioctl.inc"
1773 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1774 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1775 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1776 // can trigger a report and we need to be able to unwind through this
1777 // function. On Mac in debug mode we might not have a frame pointer, because
1778 // ioctl_common_[pre|post] doesn't get inlined here.
1779 ENABLE_FRAME_POINTER;
1783 va_start(ap, request);
1784 void *arg = va_arg(ap, void *);
1786 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1788 CHECK(ioctl_initialized);
1790 // Note: TSan does not use common flags, and they are zero-initialized.
1791 // This effectively disables ioctl handling in TSan.
1792 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1794 // Although request is unsigned long, the rest of the interceptor uses it
1795 // as just "unsigned" to save space, because we know that all values fit in
1796 // "unsigned" - they are compile-time constants.
1798 const ioctl_desc *desc = ioctl_lookup(request);
1799 ioctl_desc decoded_desc;
1801 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1802 if (!ioctl_decode(request, &decoded_desc))
1803 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1805 desc = &decoded_desc;
1808 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1809 int res = REAL(ioctl)(d, request, arg);
1810 // FIXME: some ioctls have different return values for success and failure.
1811 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1814 #define INIT_IOCTL \
1816 COMMON_INTERCEPT_FUNCTION(ioctl);
1821 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
1822 SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
1823 SANITIZER_INTERCEPT_GETPWENT_R || \
1824 SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS || \
1825 SANITIZER_INTERCEPT_FGETPWENT_R || \
1826 SANITIZER_INTERCEPT_FGETGRENT_R
1827 static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1829 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1831 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
1832 REAL(strlen)(pwd->pw_name) + 1);
1834 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
1835 REAL(strlen)(pwd->pw_passwd) + 1);
1836 #if !SANITIZER_ANDROID
1838 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
1839 REAL(strlen)(pwd->pw_gecos) + 1);
1843 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
1844 REAL(strlen)(pwd->pw_class) + 1);
1847 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
1848 REAL(strlen)(pwd->pw_dir) + 1);
1850 COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
1851 REAL(strlen)(pwd->pw_shell) + 1);
1855 static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1859 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
1860 REAL(strlen)(grp->gr_name) + 1);
1862 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
1863 REAL(strlen)(grp->gr_passwd) + 1);
1864 char **p = grp->gr_mem;
1866 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
1868 COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
1869 (p - grp->gr_mem + 1) * sizeof(*p));
1872 #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
1873 // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
1874 // SANITIZER_INTERCEPT_GETPWENT_R ||
1875 // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1877 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1878 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1880 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1882 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1883 __sanitizer_passwd *res = REAL(getpwnam)(name);
1884 if (res) unpoison_passwd(ctx, res);
1887 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1889 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1890 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1891 if (res) unpoison_passwd(ctx, res);
1894 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1896 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1897 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1898 __sanitizer_group *res = REAL(getgrnam)(name);
1899 if (res) unpoison_group(ctx, res);
1902 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1904 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1905 __sanitizer_group *res = REAL(getgrgid)(gid);
1906 if (res) unpoison_group(ctx, res);
1909 #define INIT_GETPWNAM_AND_FRIENDS \
1910 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1911 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1912 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1913 COMMON_INTERCEPT_FUNCTION(getgrgid);
1915 #define INIT_GETPWNAM_AND_FRIENDS
1918 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1919 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1920 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1922 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1923 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1924 // FIXME: under ASan the call below may write to freed memory and corrupt
1925 // its metadata. See
1926 // https://github.com/google/sanitizers/issues/321.
1927 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1929 if (result && *result) unpoison_passwd(ctx, *result);
1930 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1932 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1935 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1936 SIZE_T buflen, __sanitizer_passwd **result) {
1938 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1939 // FIXME: under ASan the call below may write to freed memory and corrupt
1940 // its metadata. See
1941 // https://github.com/google/sanitizers/issues/321.
1942 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1944 if (result && *result) unpoison_passwd(ctx, *result);
1945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1947 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1950 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1951 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1953 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1954 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1955 // FIXME: under ASan the call below may write to freed memory and corrupt
1956 // its metadata. See
1957 // https://github.com/google/sanitizers/issues/321.
1958 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1960 if (result && *result) unpoison_group(ctx, *result);
1961 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1963 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1966 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1967 SIZE_T buflen, __sanitizer_group **result) {
1969 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1970 // FIXME: under ASan the call below may write to freed memory and corrupt
1971 // its metadata. See
1972 // https://github.com/google/sanitizers/issues/321.
1973 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1975 if (result && *result) unpoison_group(ctx, *result);
1976 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
1978 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1981 #define INIT_GETPWNAM_R_AND_FRIENDS \
1982 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1983 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1984 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1985 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1987 #define INIT_GETPWNAM_R_AND_FRIENDS
1990 #if SANITIZER_INTERCEPT_GETPWENT
1991 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1993 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1994 __sanitizer_passwd *res = REAL(getpwent)(dummy);
1995 if (res) unpoison_passwd(ctx, res);
1998 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2000 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2001 __sanitizer_group *res = REAL(getgrent)(dummy);
2002 if (res) unpoison_group(ctx, res);;
2005 #define INIT_GETPWENT \
2006 COMMON_INTERCEPT_FUNCTION(getpwent); \
2007 COMMON_INTERCEPT_FUNCTION(getgrent);
2009 #define INIT_GETPWENT
2012 #if SANITIZER_INTERCEPT_FGETPWENT
2013 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2015 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2016 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2017 if (res) unpoison_passwd(ctx, res);
2020 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2022 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2023 __sanitizer_group *res = REAL(fgetgrent)(fp);
2024 if (res) unpoison_group(ctx, res);
2027 #define INIT_FGETPWENT \
2028 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2029 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2031 #define INIT_FGETPWENT
2034 #if SANITIZER_INTERCEPT_GETPWENT_R
2035 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2036 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2038 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2039 // FIXME: under ASan the call below may write to freed memory and corrupt
2040 // its metadata. See
2041 // https://github.com/google/sanitizers/issues/321.
2042 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2044 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2045 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2047 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2050 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2051 __sanitizer_group **pwbufp) {
2053 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2054 // FIXME: under ASan the call below may write to freed memory and corrupt
2055 // its metadata. See
2056 // https://github.com/google/sanitizers/issues/321.
2057 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2059 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2062 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2065 #define INIT_GETPWENT_R \
2066 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2067 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2069 #define INIT_GETPWENT_R
2072 #if SANITIZER_INTERCEPT_FGETPWENT_R
2073 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2074 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2076 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2077 // FIXME: under ASan the call below may write to freed memory and corrupt
2078 // its metadata. See
2079 // https://github.com/google/sanitizers/issues/321.
2080 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2082 if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
2083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2085 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2088 #define INIT_FGETPWENT_R \
2089 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2091 #define INIT_FGETPWENT_R
2094 #if SANITIZER_INTERCEPT_FGETGRENT_R
2095 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2096 SIZE_T buflen, __sanitizer_group **pwbufp) {
2098 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2099 // FIXME: under ASan the call below may write to freed memory and corrupt
2100 // its metadata. See
2101 // https://github.com/google/sanitizers/issues/321.
2102 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2104 if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
2105 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
2107 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2110 #define INIT_FGETGRENT_R \
2111 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2113 #define INIT_FGETGRENT_R
2116 #if SANITIZER_INTERCEPT_SETPWENT
2117 // The only thing these interceptors do is disable any nested interceptors.
2118 // These functions may open nss modules and call uninstrumented functions from
2119 // them, and we don't want things like strlen() to trigger.
2120 INTERCEPTOR(void, setpwent, int dummy) {
2122 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2123 REAL(setpwent)(dummy);
2125 INTERCEPTOR(void, endpwent, int dummy) {
2127 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2128 REAL(endpwent)(dummy);
2130 INTERCEPTOR(void, setgrent, int dummy) {
2132 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2133 REAL(setgrent)(dummy);
2135 INTERCEPTOR(void, endgrent, int dummy) {
2137 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2138 REAL(endgrent)(dummy);
2140 #define INIT_SETPWENT \
2141 COMMON_INTERCEPT_FUNCTION(setpwent); \
2142 COMMON_INTERCEPT_FUNCTION(endpwent); \
2143 COMMON_INTERCEPT_FUNCTION(setgrent); \
2144 COMMON_INTERCEPT_FUNCTION(endgrent);
2146 #define INIT_SETPWENT
2149 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2150 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2152 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2153 // FIXME: under ASan the call below may write to freed memory and corrupt
2154 // its metadata. See
2155 // https://github.com/google/sanitizers/issues/321.
2156 int res = REAL(clock_getres)(clk_id, tp);
2158 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2162 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2164 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2165 // FIXME: under ASan the call below may write to freed memory and corrupt
2166 // its metadata. See
2167 // https://github.com/google/sanitizers/issues/321.
2168 int res = REAL(clock_gettime)(clk_id, tp);
2170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2174 namespace __sanitizer {
2176 int real_clock_gettime(u32 clk_id, void *tp) {
2177 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2178 return internal_clock_gettime(clk_id, tp);
2179 return REAL(clock_gettime)(clk_id, tp);
2182 } // namespace __sanitizer
2183 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2185 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2186 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2187 return REAL(clock_settime)(clk_id, tp);
2189 #define INIT_CLOCK_GETTIME \
2190 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2191 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2192 COMMON_INTERCEPT_FUNCTION(clock_settime);
2194 #define INIT_CLOCK_GETTIME
2197 #if SANITIZER_INTERCEPT_GETITIMER
2198 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2200 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2201 // FIXME: under ASan the call below may write to freed memory and corrupt
2202 // its metadata. See
2203 // https://github.com/google/sanitizers/issues/321.
2204 int res = REAL(getitimer)(which, curr_value);
2205 if (!res && curr_value) {
2206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2210 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2212 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2214 // itimerval can contain padding that may be legitimately uninitialized
2215 const struct __sanitizer_itimerval *nv =
2216 (const struct __sanitizer_itimerval *)new_value;
2217 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2218 sizeof(__sanitizer_time_t));
2219 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2220 sizeof(__sanitizer_suseconds_t));
2221 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2222 sizeof(__sanitizer_time_t));
2223 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2224 sizeof(__sanitizer_suseconds_t));
2226 // FIXME: under ASan the call below may write to freed memory and corrupt
2227 // its metadata. See
2228 // https://github.com/google/sanitizers/issues/321.
2229 int res = REAL(setitimer)(which, new_value, old_value);
2230 if (!res && old_value) {
2231 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2235 #define INIT_GETITIMER \
2236 COMMON_INTERCEPT_FUNCTION(getitimer); \
2237 COMMON_INTERCEPT_FUNCTION(setitimer);
2239 #define INIT_GETITIMER
2242 #if SANITIZER_INTERCEPT_GLOB
2243 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2245 // +1 for NULL pointer at the end.
2246 if (pglob->gl_pathv)
2247 COMMON_INTERCEPTOR_WRITE_RANGE(
2248 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2249 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2250 char *p = pglob->gl_pathv[i];
2251 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2255 #if SANITIZER_SOLARIS
2256 INTERCEPTOR(int, glob, const char *pattern, int flags,
2257 int (*errfunc)(const char *epath, int eerrno),
2258 __sanitizer_glob_t *pglob) {
2260 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2261 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2262 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2263 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2267 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2269 static void wrapped_gl_closedir(void *dir) {
2270 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2271 pglob_copy->gl_closedir(dir);
2274 static void *wrapped_gl_readdir(void *dir) {
2275 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2276 return pglob_copy->gl_readdir(dir);
2279 static void *wrapped_gl_opendir(const char *s) {
2280 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2281 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2282 return pglob_copy->gl_opendir(s);
2285 static int wrapped_gl_lstat(const char *s, void *st) {
2286 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2287 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2288 return pglob_copy->gl_lstat(s, st);
2291 static int wrapped_gl_stat(const char *s, void *st) {
2292 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2293 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2294 return pglob_copy->gl_stat(s, st);
2297 static const __sanitizer_glob_t kGlobCopy = {
2299 0, wrapped_gl_closedir, wrapped_gl_readdir,
2300 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2302 INTERCEPTOR(int, glob, const char *pattern, int flags,
2303 int (*errfunc)(const char *epath, int eerrno),
2304 __sanitizer_glob_t *pglob) {
2306 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2307 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2308 __sanitizer_glob_t glob_copy;
2309 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2310 if (flags & glob_altdirfunc) {
2311 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2312 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2313 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2314 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2315 Swap(pglob->gl_stat, glob_copy.gl_stat);
2316 pglob_copy = &glob_copy;
2318 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2319 if (flags & glob_altdirfunc) {
2320 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2321 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2322 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2323 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2324 Swap(pglob->gl_stat, glob_copy.gl_stat);
2327 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2330 #endif // SANITIZER_SOLARIS
2332 COMMON_INTERCEPT_FUNCTION(glob);
2333 #else // SANITIZER_INTERCEPT_GLOB
2335 #endif // SANITIZER_INTERCEPT_GLOB
2337 #if SANITIZER_INTERCEPT_GLOB64
2338 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2339 int (*errfunc)(const char *epath, int eerrno),
2340 __sanitizer_glob_t *pglob) {
2342 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2343 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2344 __sanitizer_glob_t glob_copy;
2345 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2346 if (flags & glob_altdirfunc) {
2347 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2348 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2349 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2350 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2351 Swap(pglob->gl_stat, glob_copy.gl_stat);
2352 pglob_copy = &glob_copy;
2354 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2355 if (flags & glob_altdirfunc) {
2356 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2357 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2358 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2359 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2360 Swap(pglob->gl_stat, glob_copy.gl_stat);
2363 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2366 #define INIT_GLOB64 \
2367 COMMON_INTERCEPT_FUNCTION(glob64);
2368 #else // SANITIZER_INTERCEPT_GLOB64
2370 #endif // SANITIZER_INTERCEPT_GLOB64
2372 #if SANITIZER_INTERCEPT_WAIT
2373 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2374 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2376 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2378 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2379 // FIXME: under ASan the call below may write to freed memory and corrupt
2380 // its metadata. See
2381 // https://github.com/google/sanitizers/issues/321.
2382 int res = REAL(wait)(status);
2383 if (res != -1 && status)
2384 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2387 // On FreeBSD id_t is always 64-bit wide.
2388 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2389 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2392 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2396 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2397 // FIXME: under ASan the call below may write to freed memory and corrupt
2398 // its metadata. See
2399 // https://github.com/google/sanitizers/issues/321.
2400 int res = REAL(waitid)(idtype, id, infop, options);
2401 if (res != -1 && infop)
2402 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2405 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2407 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2408 // FIXME: under ASan the call below may write to freed memory and corrupt
2409 // its metadata. See
2410 // https://github.com/google/sanitizers/issues/321.
2411 int res = REAL(waitpid)(pid, status, options);
2412 if (res != -1 && status)
2413 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2416 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2418 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2419 // FIXME: under ASan the call below may write to freed memory and corrupt
2420 // its metadata. See
2421 // https://github.com/google/sanitizers/issues/321.
2422 int res = REAL(wait3)(status, options, rusage);
2424 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2425 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2429 #if SANITIZER_ANDROID
2430 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2432 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2433 // FIXME: under ASan the call below may write to freed memory and corrupt
2434 // its metadata. See
2435 // https://github.com/google/sanitizers/issues/321.
2436 int res = REAL(__wait4)(pid, status, options, rusage);
2438 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2439 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2443 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2445 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2447 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2448 // FIXME: under ASan the call below may write to freed memory and corrupt
2449 // its metadata. See
2450 // https://github.com/google/sanitizers/issues/321.
2451 int res = REAL(wait4)(pid, status, options, rusage);
2453 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2454 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2458 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2459 #endif // SANITIZER_ANDROID
2461 COMMON_INTERCEPT_FUNCTION(wait); \
2462 COMMON_INTERCEPT_FUNCTION(waitid); \
2463 COMMON_INTERCEPT_FUNCTION(waitpid); \
2464 COMMON_INTERCEPT_FUNCTION(wait3);
2470 #if SANITIZER_INTERCEPT_INET
2471 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2473 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2474 uptr sz = __sanitizer_in_addr_sz(af);
2475 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2476 // FIXME: figure out read size based on the address family.
2477 // FIXME: under ASan the call below may write to freed memory and corrupt
2478 // its metadata. See
2479 // https://github.com/google/sanitizers/issues/321.
2480 char *res = REAL(inet_ntop)(af, src, dst, size);
2481 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2484 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2486 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2487 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2488 // FIXME: figure out read size based on the address family.
2489 // FIXME: under ASan the call below may write to freed memory and corrupt
2490 // its metadata. See
2491 // https://github.com/google/sanitizers/issues/321.
2492 int res = REAL(inet_pton)(af, src, dst);
2494 uptr sz = __sanitizer_in_addr_sz(af);
2495 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2500 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2501 COMMON_INTERCEPT_FUNCTION(inet_pton);
2506 #if SANITIZER_INTERCEPT_INET
2507 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2509 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2510 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2511 // FIXME: under ASan the call below may write to freed memory and corrupt
2512 // its metadata. See
2513 // https://github.com/google/sanitizers/issues/321.
2514 int res = REAL(inet_aton)(cp, dst);
2516 uptr sz = __sanitizer_in_addr_sz(af_inet);
2517 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2521 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2523 #define INIT_INET_ATON
2526 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2527 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2529 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2530 // FIXME: under ASan the call below may write to freed memory and corrupt
2531 // its metadata. See
2532 // https://github.com/google/sanitizers/issues/321.
2533 int res = REAL(pthread_getschedparam)(thread, policy, param);
2535 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2536 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2540 #define INIT_PTHREAD_GETSCHEDPARAM \
2541 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2543 #define INIT_PTHREAD_GETSCHEDPARAM
2546 #if SANITIZER_INTERCEPT_GETADDRINFO
2547 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2548 struct __sanitizer_addrinfo *hints,
2549 struct __sanitizer_addrinfo **out) {
2551 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2552 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2554 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2556 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2557 // FIXME: under ASan the call below may write to freed memory and corrupt
2558 // its metadata. See
2559 // https://github.com/google/sanitizers/issues/321.
2560 int res = REAL(getaddrinfo)(node, service, hints, out);
2561 if (res == 0 && out) {
2562 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2563 struct __sanitizer_addrinfo *p = *out;
2565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2567 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2568 if (p->ai_canonname)
2569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2570 REAL(strlen)(p->ai_canonname) + 1);
2576 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2578 #define INIT_GETADDRINFO
2581 #if SANITIZER_INTERCEPT_GETNAMEINFO
2582 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2583 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2585 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2586 serv, servlen, flags);
2587 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2588 // There is padding in in_addr that may make this too noisy
2589 // FIXME: under ASan the call below may write to freed memory and corrupt
2590 // its metadata. See
2591 // https://github.com/google/sanitizers/issues/321.
2593 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2595 if (host && hostlen)
2596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2597 if (serv && servlen)
2598 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2602 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2604 #define INIT_GETNAMEINFO
2607 #if SANITIZER_INTERCEPT_GETSOCKNAME
2608 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2610 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2611 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2612 int addrlen_in = *addrlen;
2613 // FIXME: under ASan the call below may write to freed memory and corrupt
2614 // its metadata. See
2615 // https://github.com/google/sanitizers/issues/321.
2616 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2618 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2622 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2624 #define INIT_GETSOCKNAME
2627 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2628 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2629 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2631 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2632 char **p = h->h_aliases;
2634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2637 COMMON_INTERCEPTOR_WRITE_RANGE(
2638 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2641 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2644 COMMON_INTERCEPTOR_WRITE_RANGE(
2645 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2649 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2650 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2652 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2653 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2654 if (res) write_hostent(ctx, res);
2658 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2661 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2662 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2663 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2664 if (res) write_hostent(ctx, res);
2668 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2670 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2671 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2672 if (res) write_hostent(ctx, res);
2675 #define INIT_GETHOSTBYNAME \
2676 COMMON_INTERCEPT_FUNCTION(gethostent); \
2677 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2678 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2680 #define INIT_GETHOSTBYNAME
2681 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2683 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2684 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2686 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2687 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2688 if (res) write_hostent(ctx, res);
2691 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2693 #define INIT_GETHOSTBYNAME2
2694 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2696 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2697 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2698 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2701 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2703 // FIXME: under ASan the call below may write to freed memory and corrupt
2704 // its metadata. See
2705 // https://github.com/google/sanitizers/issues/321.
2706 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2709 if (res == 0 && *result) write_hostent(ctx, *result);
2712 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2715 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2717 #define INIT_GETHOSTBYNAME_R
2720 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2721 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2722 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2724 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2726 // FIXME: under ASan the call below may write to freed memory and corrupt
2727 // its metadata. See
2728 // https://github.com/google/sanitizers/issues/321.
2729 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2731 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2732 if (res == 0 && *result) write_hostent(ctx, *result);
2735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2738 #define INIT_GETHOSTENT_R \
2739 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2741 #define INIT_GETHOSTENT_R
2744 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2745 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2746 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2747 __sanitizer_hostent **result, int *h_errnop) {
2749 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2750 buflen, result, h_errnop);
2751 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2752 // FIXME: under ASan the call below may write to freed memory and corrupt
2753 // its metadata. See
2754 // https://github.com/google/sanitizers/issues/321.
2755 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2758 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2759 if (res == 0 && *result) write_hostent(ctx, *result);
2762 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2765 #define INIT_GETHOSTBYADDR_R \
2766 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2768 #define INIT_GETHOSTBYADDR_R
2771 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2772 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2773 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2774 __sanitizer_hostent **result, int *h_errnop) {
2776 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2778 // FIXME: under ASan the call below may write to freed memory and corrupt
2779 // its metadata. See
2780 // https://github.com/google/sanitizers/issues/321.
2782 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2784 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2785 if (res == 0 && *result) write_hostent(ctx, *result);
2788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2791 #define INIT_GETHOSTBYNAME2_R \
2792 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2794 #define INIT_GETHOSTBYNAME2_R
2797 #if SANITIZER_INTERCEPT_GETSOCKOPT
2798 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2801 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2803 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2804 // FIXME: under ASan the call below may write to freed memory and corrupt
2805 // its metadata. See
2806 // https://github.com/google/sanitizers/issues/321.
2807 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2809 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2812 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2814 #define INIT_GETSOCKOPT
2817 #if SANITIZER_INTERCEPT_ACCEPT
2818 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2820 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2821 unsigned addrlen0 = 0;
2823 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2824 addrlen0 = *addrlen;
2826 int fd2 = REAL(accept)(fd, addr, addrlen);
2828 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2829 if (addr && addrlen)
2830 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2834 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2839 #if SANITIZER_INTERCEPT_ACCEPT4
2840 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2842 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2843 unsigned addrlen0 = 0;
2845 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2846 addrlen0 = *addrlen;
2848 // FIXME: under ASan the call below may write to freed memory and corrupt
2849 // its metadata. See
2850 // https://github.com/google/sanitizers/issues/321.
2851 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2853 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2854 if (addr && addrlen)
2855 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2859 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2861 #define INIT_ACCEPT4
2864 #if SANITIZER_INTERCEPT_PACCEPT
2865 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2866 __sanitizer_sigset_t *set, int f) {
2868 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2869 unsigned addrlen0 = 0;
2871 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2872 addrlen0 = *addrlen;
2874 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2875 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
2877 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2878 if (addr && addrlen)
2879 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2883 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2885 #define INIT_PACCEPT
2888 #if SANITIZER_INTERCEPT_MODF
2889 INTERCEPTOR(double, modf, double x, double *iptr) {
2891 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2892 // FIXME: under ASan the call below may write to freed memory and corrupt
2893 // its metadata. See
2894 // https://github.com/google/sanitizers/issues/321.
2895 double res = REAL(modf)(x, iptr);
2897 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2901 INTERCEPTOR(float, modff, float x, float *iptr) {
2903 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2904 // FIXME: under ASan the call below may write to freed memory and corrupt
2905 // its metadata. See
2906 // https://github.com/google/sanitizers/issues/321.
2907 float res = REAL(modff)(x, iptr);
2909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2913 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2915 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2916 // FIXME: under ASan the call below may write to freed memory and corrupt
2917 // its metadata. See
2918 // https://github.com/google/sanitizers/issues/321.
2919 long double res = REAL(modfl)(x, iptr);
2921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2926 COMMON_INTERCEPT_FUNCTION(modf); \
2927 COMMON_INTERCEPT_FUNCTION(modff); \
2928 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2933 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2934 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2936 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2937 if (msg->msg_name && msg->msg_namelen)
2938 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2939 if (msg->msg_iov && msg->msg_iovlen)
2940 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2941 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2942 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2943 if (msg->msg_control && msg->msg_controllen)
2944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2948 #if SANITIZER_INTERCEPT_RECVMSG
2949 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2952 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2953 // FIXME: under ASan the call below may write to freed memory and corrupt
2954 // its metadata. See
2955 // https://github.com/google/sanitizers/issues/321.
2956 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2958 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2960 write_msghdr(ctx, msg, res);
2961 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2966 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2968 #define INIT_RECVMSG
2971 #if SANITIZER_INTERCEPT_RECVMMSG
2972 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2973 unsigned int vlen, int flags, void *timeout) {
2975 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2976 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2977 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2979 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2980 for (int i = 0; i < res; ++i) {
2981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2982 sizeof(msgvec[i].msg_len));
2983 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2984 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2989 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2991 #define INIT_RECVMMSG
2994 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2995 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2996 const unsigned kCmsgDataOffset =
2997 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2999 char *p = (char *)control;
3000 char *const control_end = p + controllen;
3002 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3003 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3004 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3006 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3008 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3009 sizeof(cmsg->cmsg_level));
3010 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3011 sizeof(cmsg->cmsg_type));
3013 if (cmsg->cmsg_len > kCmsgDataOffset) {
3014 char *data = p + kCmsgDataOffset;
3015 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3016 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3019 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3023 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3026 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3035 if (msg->msg_name && msg->msg_namelen)
3036 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3037 if (msg->msg_iov && msg->msg_iovlen)
3038 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3039 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3040 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3041 if (msg->msg_control && msg->msg_controllen)
3042 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3046 #if SANITIZER_INTERCEPT_SENDMSG
3047 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3050 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3052 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3053 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3055 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3056 if (common_flags()->intercept_send && res >= 0 && msg)
3057 read_msghdr(ctx, msg, res);
3060 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3062 #define INIT_SENDMSG
3065 #if SANITIZER_INTERCEPT_SENDMMSG
3066 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3067 unsigned vlen, int flags) {
3069 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3071 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3072 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3074 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3075 if (res >= 0 && msgvec)
3076 for (int i = 0; i < res; ++i) {
3077 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3078 sizeof(msgvec[i].msg_len));
3079 if (common_flags()->intercept_send)
3080 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3084 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3086 #define INIT_SENDMMSG
3089 #if SANITIZER_INTERCEPT_GETPEERNAME
3090 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3092 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3094 if (addrlen) addr_sz = *addrlen;
3095 // FIXME: under ASan the call below may write to freed memory and corrupt
3096 // its metadata. See
3097 // https://github.com/google/sanitizers/issues/321.
3098 int res = REAL(getpeername)(sockfd, addr, addrlen);
3099 if (!res && addr && addrlen)
3100 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3103 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3105 #define INIT_GETPEERNAME
3108 #if SANITIZER_INTERCEPT_SYSINFO
3109 INTERCEPTOR(int, sysinfo, void *info) {
3111 // FIXME: under ASan the call below may write to freed memory and corrupt
3112 // its metadata. See
3113 // https://github.com/google/sanitizers/issues/321.
3114 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3115 int res = REAL(sysinfo)(info);
3117 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3120 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3122 #define INIT_SYSINFO
3125 #if SANITIZER_INTERCEPT_READDIR
3126 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3128 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3129 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3130 __sanitizer_dirent *res = REAL(opendir)(path);
3132 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3136 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3138 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3139 // FIXME: under ASan the call below may write to freed memory and corrupt
3140 // its metadata. See
3141 // https://github.com/google/sanitizers/issues/321.
3142 __sanitizer_dirent *res = REAL(readdir)(dirp);
3143 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3147 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3148 __sanitizer_dirent **result) {
3150 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
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(readdir_r)(dirp, entry, result);
3156 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3158 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3163 #define INIT_READDIR \
3164 COMMON_INTERCEPT_FUNCTION(opendir); \
3165 COMMON_INTERCEPT_FUNCTION(readdir); \
3166 COMMON_INTERCEPT_FUNCTION(readdir_r);
3168 #define INIT_READDIR
3171 #if SANITIZER_INTERCEPT_READDIR64
3172 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3174 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3175 // FIXME: under ASan the call below may write to freed memory and corrupt
3176 // its metadata. See
3177 // https://github.com/google/sanitizers/issues/321.
3178 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3179 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3183 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3184 __sanitizer_dirent64 **result) {
3186 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3187 // FIXME: under ASan the call below may write to freed memory and corrupt
3188 // its metadata. See
3189 // https://github.com/google/sanitizers/issues/321.
3190 int res = REAL(readdir64_r)(dirp, entry, result);
3192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3198 #define INIT_READDIR64 \
3199 COMMON_INTERCEPT_FUNCTION(readdir64); \
3200 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3202 #define INIT_READDIR64
3205 #if SANITIZER_INTERCEPT_PTRACE
3206 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3208 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3209 __sanitizer_iovec local_iovec;
3212 if (request == ptrace_setregs)
3213 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3214 else if (request == ptrace_setfpregs)
3215 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3216 else if (request == ptrace_setfpxregs)
3217 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3218 else if (request == ptrace_setvfpregs)
3219 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3220 else if (request == ptrace_setsiginfo)
3221 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3222 // Some kernel might zero the iovec::iov_base in case of invalid
3223 // write access. In this case copy the invalid address for further
3225 else if (request == ptrace_setregset || request == ptrace_getregset) {
3226 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3227 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3228 local_iovec = *iovec;
3229 if (request == ptrace_setregset)
3230 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3234 // FIXME: under ASan the call below may write to freed memory and corrupt
3235 // its metadata. See
3236 // https://github.com/google/sanitizers/issues/321.
3237 uptr res = REAL(ptrace)(request, pid, addr, data);
3240 // Note that PEEK* requests assign different meaning to the return value.
3241 // This function does not handle them (nor does it need to).
3242 if (request == ptrace_getregs)
3243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3244 else if (request == ptrace_getfpregs)
3245 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3246 else if (request == ptrace_getfpxregs)
3247 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3248 else if (request == ptrace_getvfpregs)
3249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3250 else if (request == ptrace_getsiginfo)
3251 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3252 else if (request == ptrace_geteventmsg)
3253 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3254 else if (request == ptrace_getregset) {
3255 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3256 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3257 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3258 local_iovec.iov_len);
3264 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3269 #if SANITIZER_INTERCEPT_SETLOCALE
3270 static void unpoison_ctype_arrays(void *ctx) {
3271 #if SANITIZER_NETBSD
3272 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3273 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3275 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3279 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3281 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3283 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3284 char *res = REAL(setlocale)(category, locale);
3286 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3287 unpoison_ctype_arrays(ctx);
3292 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3294 #define INIT_SETLOCALE
3297 #if SANITIZER_INTERCEPT_GETCWD
3298 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3300 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3301 // FIXME: under ASan the call below may write to freed memory and corrupt
3302 // its metadata. See
3303 // https://github.com/google/sanitizers/issues/321.
3304 char *res = REAL(getcwd)(buf, size);
3305 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3308 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3313 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3314 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3316 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3317 // FIXME: under ASan the call below may write to freed memory and corrupt
3318 // its metadata. See
3319 // https://github.com/google/sanitizers/issues/321.
3320 char *res = REAL(get_current_dir_name)(fake);
3321 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3325 #define INIT_GET_CURRENT_DIR_NAME \
3326 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3328 #define INIT_GET_CURRENT_DIR_NAME
3331 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3333 if (nptr == *endptr) {
3334 // No digits were found at strtol call, we need to find out the last
3335 // symbol accessed by strtoll on our own.
3336 // We get this symbol by skipping leading blanks and optional +/- sign.
3337 while (IsSpace(*nptr)) nptr++;
3338 if (*nptr == '+' || *nptr == '-') nptr++;
3339 *endptr = const_cast<char *>(nptr);
3341 CHECK(*endptr >= nptr);
3344 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3345 char **endptr, char *real_endptr, int base) {
3347 *endptr = real_endptr;
3348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3350 // If base has unsupported value, strtol can exit with EINVAL
3351 // without reading any characters. So do additional checks only
3352 // if base is valid.
3353 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3354 if (is_valid_base) {
3355 FixRealStrtolEndptr(nptr, &real_endptr);
3357 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3358 (real_endptr - nptr) + 1 : 0);
3362 #if SANITIZER_INTERCEPT_STRTOIMAX
3363 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3365 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3366 // FIXME: under ASan the call below may write to freed memory and corrupt
3367 // its metadata. See
3368 // https://github.com/google/sanitizers/issues/321.
3370 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3371 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3375 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3377 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3378 // FIXME: under ASan the call below may write to freed memory and corrupt
3379 // its metadata. See
3380 // https://github.com/google/sanitizers/issues/321.
3382 UINTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3383 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3387 #define INIT_STRTOIMAX \
3388 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3389 COMMON_INTERCEPT_FUNCTION(strtoumax);
3391 #define INIT_STRTOIMAX
3394 #if SANITIZER_INTERCEPT_MBSTOWCS
3395 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3397 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3398 // FIXME: under ASan the call below may write to freed memory and corrupt
3399 // its metadata. See
3400 // https://github.com/google/sanitizers/issues/321.
3401 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3402 if (res != (SIZE_T) - 1 && dest) {
3403 SIZE_T write_cnt = res + (res < len);
3404 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3409 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3412 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3413 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3414 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
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 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3419 if (res != (SIZE_T)(-1) && dest && src) {
3420 // This function, and several others, may or may not write the terminating
3421 // \0 character. They write it iff they clear *src.
3422 SIZE_T write_cnt = res + !*src;
3423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3428 #define INIT_MBSTOWCS \
3429 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3430 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3432 #define INIT_MBSTOWCS
3435 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3436 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3437 SIZE_T len, void *ps) {
3439 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3441 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3442 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3444 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3445 // FIXME: under ASan the call below may write to freed memory and corrupt
3446 // its metadata. See
3447 // https://github.com/google/sanitizers/issues/321.
3448 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3449 if (res != (SIZE_T)(-1) && dest && src) {
3450 SIZE_T write_cnt = res + !*src;
3451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3456 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3458 #define INIT_MBSNRTOWCS
3461 #if SANITIZER_INTERCEPT_WCSTOMBS
3462 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3464 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3465 // FIXME: under ASan the call below may write to freed memory and corrupt
3466 // its metadata. See
3467 // https://github.com/google/sanitizers/issues/321.
3468 SIZE_T res = REAL(wcstombs)(dest, src, len);
3469 if (res != (SIZE_T) - 1 && dest) {
3470 SIZE_T write_cnt = res + (res < len);
3471 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3476 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3479 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3480 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3481 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3482 // FIXME: under ASan the call below may write to freed memory and corrupt
3483 // its metadata. See
3484 // https://github.com/google/sanitizers/issues/321.
3485 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3486 if (res != (SIZE_T) - 1 && dest && src) {
3487 SIZE_T write_cnt = res + !*src;
3488 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3493 #define INIT_WCSTOMBS \
3494 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3495 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3497 #define INIT_WCSTOMBS
3500 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3501 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3502 SIZE_T len, void *ps) {
3504 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3506 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3507 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3509 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3510 // FIXME: under ASan the call below may write to freed memory and corrupt
3511 // its metadata. See
3512 // https://github.com/google/sanitizers/issues/321.
3513 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3514 if (res != ((SIZE_T)-1) && dest && src) {
3515 SIZE_T write_cnt = res + !*src;
3516 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3521 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3523 #define INIT_WCSNRTOMBS
3527 #if SANITIZER_INTERCEPT_WCRTOMB
3528 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3530 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3531 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3532 // FIXME: under ASan the call below may write to freed memory and corrupt
3533 // its metadata. See
3534 // https://github.com/google/sanitizers/issues/321.
3535 SIZE_T res = REAL(wcrtomb)(dest, src, ps);
3536 if (res != ((SIZE_T)-1) && dest) {
3537 SIZE_T write_cnt = res;
3538 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3543 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3545 #define INIT_WCRTOMB
3548 #if SANITIZER_INTERCEPT_TCGETATTR
3549 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3551 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3552 // FIXME: under ASan the call below may write to freed memory and corrupt
3553 // its metadata. See
3554 // https://github.com/google/sanitizers/issues/321.
3555 int res = REAL(tcgetattr)(fd, termios_p);
3556 if (!res && termios_p)
3557 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3561 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3563 #define INIT_TCGETATTR
3566 #if SANITIZER_INTERCEPT_REALPATH
3567 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3569 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3570 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3572 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3573 // version of a versioned symbol. For realpath(), this gives us something
3574 // (called __old_realpath) that does not handle NULL in the second argument.
3575 // Handle it as part of the interceptor.
3576 char *allocated_path = nullptr;
3578 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3580 char *res = REAL(realpath)(path, resolved_path);
3581 if (allocated_path && !res) WRAP(free)(allocated_path);
3582 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3585 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3587 #define INIT_REALPATH
3590 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3591 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3593 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3594 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3595 char *res = REAL(canonicalize_file_name)(path);
3596 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3599 #define INIT_CANONICALIZE_FILE_NAME \
3600 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3602 #define INIT_CANONICALIZE_FILE_NAME
3605 #if SANITIZER_INTERCEPT_CONFSTR
3606 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3608 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3609 // FIXME: under ASan the call below may write to freed memory and corrupt
3610 // its metadata. See
3611 // https://github.com/google/sanitizers/issues/321.
3612 SIZE_T res = REAL(confstr)(name, buf, len);
3614 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3617 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3619 #define INIT_CONFSTR
3622 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3623 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3625 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3626 // FIXME: under ASan the call below may write to freed memory and corrupt
3627 // its metadata. See
3628 // https://github.com/google/sanitizers/issues/321.
3629 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3630 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3633 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3635 #define INIT_SCHED_GETAFFINITY
3638 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3639 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3641 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3642 int res = REAL(sched_getparam)(pid, param);
3643 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3646 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3648 #define INIT_SCHED_GETPARAM
3651 #if SANITIZER_INTERCEPT_STRERROR
3652 INTERCEPTOR(char *, strerror, int errnum) {
3654 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3655 char *res = REAL(strerror)(errnum);
3656 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3659 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3661 #define INIT_STRERROR
3664 #if SANITIZER_INTERCEPT_STRERROR_R
3665 // There are 2 versions of strerror_r:
3666 // * POSIX version returns 0 on success, negative error code on failure,
3667 // writes message to buf.
3668 // * GNU version returns message pointer, which points to either buf or some
3670 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3671 SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3672 SANITIZER_FREEBSD || SANITIZER_OPENBSD
3673 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3674 // At least on OSX, buf contents are valid even when the call fails.
3675 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3677 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3678 // FIXME: under ASan the call below may write to freed memory and corrupt
3679 // its metadata. See
3680 // https://github.com/google/sanitizers/issues/321.
3681 int res = REAL(strerror_r)(errnum, buf, buflen);
3683 SIZE_T sz = internal_strnlen(buf, buflen);
3684 if (sz < buflen) ++sz;
3685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3690 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3692 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3693 // FIXME: under ASan the call below may write to freed memory and corrupt
3694 // its metadata. See
3695 // https://github.com/google/sanitizers/issues/321.
3696 char *res = REAL(strerror_r)(errnum, buf, buflen);
3698 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3700 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3703 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3705 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3707 #define INIT_STRERROR_R
3710 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3711 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3713 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3714 // FIXME: under ASan the call below may write to freed memory and corrupt
3715 // its metadata. See
3716 // https://github.com/google/sanitizers/issues/321.
3717 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3718 // This version always returns a null-terminated string.
3720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3723 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3725 #define INIT_XPG_STRERROR_R
3728 #if SANITIZER_INTERCEPT_SCANDIR
3729 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3730 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3731 const struct __sanitizer_dirent **);
3733 static THREADLOCAL scandir_filter_f scandir_filter;
3734 static THREADLOCAL scandir_compar_f scandir_compar;
3736 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3737 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3738 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3739 return scandir_filter(dir);
3742 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3743 const struct __sanitizer_dirent **b) {
3744 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3745 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3746 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3747 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3748 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3749 return scandir_compar(a, b);
3752 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3753 scandir_filter_f filter, scandir_compar_f compar) {
3755 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3756 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3757 scandir_filter = filter;
3758 scandir_compar = compar;
3759 // FIXME: under ASan the call below may write to freed memory and corrupt
3760 // its metadata. See
3761 // https://github.com/google/sanitizers/issues/321.
3762 int res = REAL(scandir)(dirp, namelist,
3763 filter ? wrapped_scandir_filter : nullptr,
3764 compar ? wrapped_scandir_compar : nullptr);
3765 scandir_filter = nullptr;
3766 scandir_compar = nullptr;
3767 if (namelist && res > 0) {
3768 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3770 for (int i = 0; i < res; ++i)
3771 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3772 (*namelist)[i]->d_reclen);
3776 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3778 #define INIT_SCANDIR
3781 #if SANITIZER_INTERCEPT_SCANDIR64
3782 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3783 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3784 const struct __sanitizer_dirent64 **);
3786 static THREADLOCAL scandir64_filter_f scandir64_filter;
3787 static THREADLOCAL scandir64_compar_f scandir64_compar;
3789 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3790 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3791 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3792 return scandir64_filter(dir);
3795 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3796 const struct __sanitizer_dirent64 **b) {
3797 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3798 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3799 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3800 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3801 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3802 return scandir64_compar(a, b);
3805 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3806 scandir64_filter_f filter, scandir64_compar_f compar) {
3808 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3809 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3810 scandir64_filter = filter;
3811 scandir64_compar = compar;
3812 // FIXME: under ASan the call below may write to freed memory and corrupt
3813 // its metadata. See
3814 // https://github.com/google/sanitizers/issues/321.
3816 REAL(scandir64)(dirp, namelist,
3817 filter ? wrapped_scandir64_filter : nullptr,
3818 compar ? wrapped_scandir64_compar : nullptr);
3819 scandir64_filter = nullptr;
3820 scandir64_compar = nullptr;
3821 if (namelist && res > 0) {
3822 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3823 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3824 for (int i = 0; i < res; ++i)
3825 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3826 (*namelist)[i]->d_reclen);
3830 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3832 #define INIT_SCANDIR64
3835 #if SANITIZER_INTERCEPT_GETGROUPS
3836 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3838 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
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(getgroups)(size, lst);
3843 if (res >= 0 && lst && size > 0)
3844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3847 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3849 #define INIT_GETGROUPS
3852 #if SANITIZER_INTERCEPT_POLL
3853 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3854 __sanitizer_nfds_t nfds) {
3855 for (unsigned i = 0; i < nfds; ++i) {
3856 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3857 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3861 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3862 __sanitizer_nfds_t nfds) {
3863 for (unsigned i = 0; i < nfds; ++i)
3864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3865 sizeof(fds[i].revents));
3868 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3871 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3872 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3873 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3874 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3877 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3882 #if SANITIZER_INTERCEPT_PPOLL
3883 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3884 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3886 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3887 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3889 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3890 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3892 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3893 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3896 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3901 #if SANITIZER_INTERCEPT_WORDEXP
3902 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3904 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3905 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3906 // FIXME: under ASan the call below may write to freed memory and corrupt
3907 // its metadata. See
3908 // https://github.com/google/sanitizers/issues/321.
3909 int res = REAL(wordexp)(s, p, flags);
3911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3914 sizeof(*p->we_wordv) * p->we_wordc);
3915 for (uptr i = 0; i < p->we_wordc; ++i) {
3916 char *w = p->we_wordv[i];
3917 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3922 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3924 #define INIT_WORDEXP
3927 #if SANITIZER_INTERCEPT_SIGWAIT
3928 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3930 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3931 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3932 // FIXME: under ASan the call below may write to freed memory and corrupt
3933 // its metadata. See
3934 // https://github.com/google/sanitizers/issues/321.
3935 int res = REAL(sigwait)(set, sig);
3936 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3939 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3941 #define INIT_SIGWAIT
3944 #if SANITIZER_INTERCEPT_SIGWAITINFO
3945 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3947 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3948 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3949 // FIXME: under ASan the call below may write to freed memory and corrupt
3950 // its metadata. See
3951 // https://github.com/google/sanitizers/issues/321.
3952 int res = REAL(sigwaitinfo)(set, info);
3953 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3956 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3958 #define INIT_SIGWAITINFO
3961 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3962 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3965 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3966 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3967 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3968 // FIXME: under ASan the call below may write to freed memory and corrupt
3969 // its metadata. See
3970 // https://github.com/google/sanitizers/issues/321.
3971 int res = REAL(sigtimedwait)(set, info, timeout);
3972 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3975 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3977 #define INIT_SIGTIMEDWAIT
3980 #if SANITIZER_INTERCEPT_SIGSETOPS
3981 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
3983 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
3984 // FIXME: under ASan the call below may write to freed memory and corrupt
3985 // its metadata. See
3986 // https://github.com/google/sanitizers/issues/321.
3987 int res = REAL(sigemptyset)(set);
3988 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
3992 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
3994 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
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(sigfillset)(set);
3999 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4002 #define INIT_SIGSETOPS \
4003 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4004 COMMON_INTERCEPT_FUNCTION(sigfillset);
4006 #define INIT_SIGSETOPS
4009 #if SANITIZER_INTERCEPT_SIGPENDING
4010 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4012 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4013 // FIXME: under ASan the call below may write to freed memory and corrupt
4014 // its metadata. See
4015 // https://github.com/google/sanitizers/issues/321.
4016 int res = REAL(sigpending)(set);
4017 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4020 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4022 #define INIT_SIGPENDING
4025 #if SANITIZER_INTERCEPT_SIGPROCMASK
4026 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4027 __sanitizer_sigset_t *oldset) {
4029 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4030 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4031 // FIXME: under ASan the call below may write to freed memory and corrupt
4032 // its metadata. See
4033 // https://github.com/google/sanitizers/issues/321.
4034 int res = REAL(sigprocmask)(how, set, oldset);
4036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4039 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4041 #define INIT_SIGPROCMASK
4044 #if SANITIZER_INTERCEPT_BACKTRACE
4045 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4047 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4048 // FIXME: under ASan the call below may write to freed memory and corrupt
4049 // its metadata. See
4050 // https://github.com/google/sanitizers/issues/321.
4051 int res = REAL(backtrace)(buffer, size);
4053 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4057 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4059 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4061 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4062 // FIXME: under ASan the call below may write to freed memory and corrupt
4063 // its metadata. See
4064 // https://github.com/google/sanitizers/issues/321.
4065 char **res = REAL(backtrace_symbols)(buffer, size);
4067 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4068 for (int i = 0; i < size; ++i)
4069 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4073 #define INIT_BACKTRACE \
4074 COMMON_INTERCEPT_FUNCTION(backtrace); \
4075 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4077 #define INIT_BACKTRACE
4080 #if SANITIZER_INTERCEPT__EXIT
4081 INTERCEPTOR(void, _exit, int status) {
4083 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4084 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4085 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4086 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4087 if (status == 0) status = status1;
4088 REAL(_exit)(status);
4090 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4095 #if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4096 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4098 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4099 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4100 int res = REAL(pthread_mutex_lock)(m);
4101 if (res == errno_EOWNERDEAD)
4102 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4103 if (res == 0 || res == errno_EOWNERDEAD)
4104 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4105 if (res == errno_EINVAL)
4106 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4110 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4112 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4113 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4114 int res = REAL(pthread_mutex_unlock)(m);
4115 if (res == errno_EINVAL)
4116 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4120 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4121 #define INIT_PTHREAD_MUTEX_UNLOCK \
4122 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4124 #define INIT_PTHREAD_MUTEX_LOCK
4125 #define INIT_PTHREAD_MUTEX_UNLOCK
4128 #if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4129 INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4130 return WRAP(pthread_mutex_lock)(m);
4133 INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4134 return WRAP(pthread_mutex_unlock)(m);
4137 #define INIT___PTHREAD_MUTEX_LOCK \
4138 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4139 #define INIT___PTHREAD_MUTEX_UNLOCK \
4140 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4142 #define INIT___PTHREAD_MUTEX_LOCK
4143 #define INIT___PTHREAD_MUTEX_UNLOCK
4146 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4147 INTERCEPTOR(int, __libc_mutex_lock, void *m)
4148 ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4150 INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4151 ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4153 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4154 ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4156 #define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4157 #define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4158 #define INIT___LIBC_THR_SETCANCELSTATE \
4159 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4161 #define INIT___LIBC_MUTEX_LOCK
4162 #define INIT___LIBC_MUTEX_UNLOCK
4163 #define INIT___LIBC_THR_SETCANCELSTATE
4166 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4167 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4168 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4169 if (mnt->mnt_fsname)
4170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4171 REAL(strlen)(mnt->mnt_fsname) + 1);
4173 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4174 REAL(strlen)(mnt->mnt_dir) + 1);
4176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4177 REAL(strlen)(mnt->mnt_type) + 1);
4179 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4180 REAL(strlen)(mnt->mnt_opts) + 1);
4184 #if SANITIZER_INTERCEPT_GETMNTENT
4185 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4187 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4188 __sanitizer_mntent *res = REAL(getmntent)(fp);
4189 if (res) write_mntent(ctx, res);
4192 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4194 #define INIT_GETMNTENT
4197 #if SANITIZER_INTERCEPT_GETMNTENT_R
4198 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4199 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4201 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4202 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4203 if (res) write_mntent(ctx, res);
4206 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4208 #define INIT_GETMNTENT_R
4211 #if SANITIZER_INTERCEPT_STATFS
4212 INTERCEPTOR(int, statfs, char *path, void *buf) {
4214 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4215 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4216 // FIXME: under ASan the call below may write to freed memory and corrupt
4217 // its metadata. See
4218 // https://github.com/google/sanitizers/issues/321.
4219 int res = REAL(statfs)(path, buf);
4220 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4223 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4225 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4226 // FIXME: under ASan the call below may write to freed memory and corrupt
4227 // its metadata. See
4228 // https://github.com/google/sanitizers/issues/321.
4229 int res = REAL(fstatfs)(fd, buf);
4230 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4233 #define INIT_STATFS \
4234 COMMON_INTERCEPT_FUNCTION(statfs); \
4235 COMMON_INTERCEPT_FUNCTION(fstatfs);
4240 #if SANITIZER_INTERCEPT_STATFS64
4241 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4243 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4244 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4245 // FIXME: under ASan the call below may write to freed memory and corrupt
4246 // its metadata. See
4247 // https://github.com/google/sanitizers/issues/321.
4248 int res = REAL(statfs64)(path, buf);
4249 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4252 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4254 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4255 // FIXME: under ASan the call below may write to freed memory and corrupt
4256 // its metadata. See
4257 // https://github.com/google/sanitizers/issues/321.
4258 int res = REAL(fstatfs64)(fd, buf);
4259 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4262 #define INIT_STATFS64 \
4263 COMMON_INTERCEPT_FUNCTION(statfs64); \
4264 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4266 #define INIT_STATFS64
4269 #if SANITIZER_INTERCEPT_STATVFS
4270 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4272 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4273 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4274 // FIXME: under ASan the call below may write to freed memory and corrupt
4275 // its metadata. See
4276 // https://github.com/google/sanitizers/issues/321.
4277 int res = REAL(statvfs)(path, buf);
4278 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4281 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4283 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4284 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4285 // FIXME: under ASan the call below may write to freed memory and corrupt
4286 // its metadata. See
4287 // https://github.com/google/sanitizers/issues/321.
4288 int res = REAL(fstatvfs)(fd, buf);
4290 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4292 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4296 #define INIT_STATVFS \
4297 COMMON_INTERCEPT_FUNCTION(statvfs); \
4298 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4300 #define INIT_STATVFS
4303 #if SANITIZER_INTERCEPT_STATVFS64
4304 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4306 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4307 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4308 // FIXME: under ASan the call below may write to freed memory and corrupt
4309 // its metadata. See
4310 // https://github.com/google/sanitizers/issues/321.
4311 int res = REAL(statvfs64)(path, buf);
4312 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4315 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4317 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4318 // FIXME: under ASan the call below may write to freed memory and corrupt
4319 // its metadata. See
4320 // https://github.com/google/sanitizers/issues/321.
4321 int res = REAL(fstatvfs64)(fd, buf);
4322 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4325 #define INIT_STATVFS64 \
4326 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4327 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4329 #define INIT_STATVFS64
4332 #if SANITIZER_INTERCEPT_INITGROUPS
4333 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4335 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4336 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4337 int res = REAL(initgroups)(user, group);
4340 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4342 #define INIT_INITGROUPS
4345 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4346 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4348 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4349 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4350 char *res = REAL(ether_ntoa)(addr);
4351 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4354 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4356 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4357 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4358 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4359 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4362 #define INIT_ETHER_NTOA_ATON \
4363 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4364 COMMON_INTERCEPT_FUNCTION(ether_aton);
4366 #define INIT_ETHER_NTOA_ATON
4369 #if SANITIZER_INTERCEPT_ETHER_HOST
4370 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4372 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4373 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4374 // FIXME: under ASan the call below may write to freed memory and corrupt
4375 // its metadata. See
4376 // https://github.com/google/sanitizers/issues/321.
4377 int res = REAL(ether_ntohost)(hostname, addr);
4378 if (!res && hostname)
4379 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4382 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4384 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4386 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4387 // FIXME: under ASan the call below may write to freed memory and corrupt
4388 // its metadata. See
4389 // https://github.com/google/sanitizers/issues/321.
4390 int res = REAL(ether_hostton)(hostname, addr);
4391 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4394 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4397 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4398 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4399 // FIXME: under ASan the call below may write to freed memory and corrupt
4400 // its metadata. See
4401 // https://github.com/google/sanitizers/issues/321.
4402 int res = REAL(ether_line)(line, addr, hostname);
4404 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4406 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4410 #define INIT_ETHER_HOST \
4411 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4412 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4413 COMMON_INTERCEPT_FUNCTION(ether_line);
4415 #define INIT_ETHER_HOST
4418 #if SANITIZER_INTERCEPT_ETHER_R
4419 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4421 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4422 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
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 char *res = REAL(ether_ntoa_r)(addr, buf);
4427 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4430 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4431 __sanitizer_ether_addr *addr) {
4433 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4434 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4435 // FIXME: under ASan the call below may write to freed memory and corrupt
4436 // its metadata. See
4437 // https://github.com/google/sanitizers/issues/321.
4438 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4439 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4442 #define INIT_ETHER_R \
4443 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4444 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4446 #define INIT_ETHER_R
4449 #if SANITIZER_INTERCEPT_SHMCTL
4450 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4452 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4453 // FIXME: under ASan the call below may write to freed memory and corrupt
4454 // its metadata. See
4455 // https://github.com/google/sanitizers/issues/321.
4456 int res = REAL(shmctl)(shmid, cmd, buf);
4459 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4460 sz = sizeof(__sanitizer_shmid_ds);
4461 else if (cmd == shmctl_ipc_info)
4462 sz = struct_shminfo_sz;
4463 else if (cmd == shmctl_shm_info)
4464 sz = struct_shm_info_sz;
4465 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4469 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4474 #if SANITIZER_INTERCEPT_RANDOM_R
4475 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4477 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4478 // FIXME: under ASan the call below may write to freed memory and corrupt
4479 // its metadata. See
4480 // https://github.com/google/sanitizers/issues/321.
4481 int res = REAL(random_r)(buf, result);
4483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4486 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4488 #define INIT_RANDOM_R
4491 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4492 // its metadata. See
4493 // https://github.com/google/sanitizers/issues/321.
4494 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4495 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4496 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4497 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4498 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4499 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4500 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4501 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4502 INTERCEPTOR(int, fn, void *attr, void *r) { \
4504 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4505 int res = REAL(fn)(attr, r); \
4506 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4509 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4510 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4511 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4512 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4513 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4514 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4515 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4516 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4517 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4518 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4521 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4522 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4523 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4524 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4525 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4526 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4528 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4529 // FIXME: under ASan the call below may write to freed memory and corrupt
4530 // its metadata. See
4531 // https://github.com/google/sanitizers/issues/321.
4532 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4534 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4535 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4540 // We may need to call the real pthread_attr_getstack from the run-time
4541 // in sanitizer_common, but we don't want to include the interception headers
4542 // there. So, just define this function here.
4543 namespace __sanitizer {
4545 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4546 return REAL(pthread_attr_getstack)(attr, addr, size);
4549 } // namespace __sanitizer
4551 #define INIT_PTHREAD_ATTR_GET \
4552 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4553 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4554 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4555 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4556 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4558 #define INIT_PTHREAD_ATTR_GET
4561 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4562 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4563 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4565 #define INIT_PTHREAD_ATTR_GET_SCHED \
4566 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4567 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4569 #define INIT_PTHREAD_ATTR_GET_SCHED
4572 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4573 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4575 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4576 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4578 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4581 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4582 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4585 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4587 // FIXME: under ASan the call below may write to freed memory and corrupt
4588 // its metadata. See
4589 // https://github.com/google/sanitizers/issues/321.
4590 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4591 if (!res && cpusetsize && cpuset)
4592 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4596 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4597 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4599 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4602 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4603 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4604 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4605 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4607 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4610 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4611 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4612 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4613 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4615 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4618 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4619 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4620 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4621 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4623 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4626 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4627 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4628 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4629 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4631 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4634 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4635 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4636 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4637 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4639 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4642 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4643 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4644 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4645 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4647 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4650 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4651 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4652 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4653 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4655 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4658 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4659 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4660 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4661 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4663 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4666 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4667 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4668 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4669 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4671 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4674 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4675 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4676 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4677 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4679 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4682 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4683 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4684 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4685 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4687 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4690 #if SANITIZER_INTERCEPT_TMPNAM
4691 INTERCEPTOR(char *, tmpnam, char *s) {
4693 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4694 char *res = REAL(tmpnam)(s);
4697 // FIXME: under ASan the call below may write to freed memory and corrupt
4698 // its metadata. See
4699 // https://github.com/google/sanitizers/issues/321.
4700 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4702 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4706 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4711 #if SANITIZER_INTERCEPT_TMPNAM_R
4712 INTERCEPTOR(char *, tmpnam_r, char *s) {
4714 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4715 // FIXME: under ASan the call below may write to freed memory and corrupt
4716 // its metadata. See
4717 // https://github.com/google/sanitizers/issues/321.
4718 char *res = REAL(tmpnam_r)(s);
4719 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4722 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4724 #define INIT_TMPNAM_R
4727 #if SANITIZER_INTERCEPT_TTYNAME_R
4728 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4730 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4731 int res = REAL(ttyname_r)(fd, name, namesize);
4733 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4736 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4738 #define INIT_TTYNAME_R
4741 #if SANITIZER_INTERCEPT_TEMPNAM
4742 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4744 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4745 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4746 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4747 char *res = REAL(tempnam)(dir, pfx);
4748 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4751 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4753 #define INIT_TEMPNAM
4756 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4757 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4759 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4760 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4761 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4762 return REAL(pthread_setname_np)(thread, name);
4764 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4765 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4766 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4768 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4769 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4770 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4771 internal_snprintf(newname, sizeof(newname), name, arg);
4772 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4773 return REAL(pthread_setname_np)(thread, name, arg);
4775 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4777 #define INIT_PTHREAD_SETNAME_NP
4780 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4781 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4783 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4784 int res = REAL(pthread_getname_np)(thread, name, len);
4786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4789 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4791 #define INIT_PTHREAD_GETNAME_NP
4794 #if SANITIZER_INTERCEPT_SINCOS
4795 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4797 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
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 REAL(sincos)(x, sin, cos);
4802 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4803 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4805 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4807 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4808 // FIXME: under ASan the call below may write to freed memory and corrupt
4809 // its metadata. See
4810 // https://github.com/google/sanitizers/issues/321.
4811 REAL(sincosf)(x, sin, cos);
4812 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4813 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4815 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4817 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4818 // FIXME: under ASan the call below may write to freed memory and corrupt
4819 // its metadata. See
4820 // https://github.com/google/sanitizers/issues/321.
4821 REAL(sincosl)(x, sin, cos);
4822 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4823 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4825 #define INIT_SINCOS \
4826 COMMON_INTERCEPT_FUNCTION(sincos); \
4827 COMMON_INTERCEPT_FUNCTION(sincosf); \
4828 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4833 #if SANITIZER_INTERCEPT_REMQUO
4834 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4836 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4837 // FIXME: under ASan the call below may write to freed memory and corrupt
4838 // its metadata. See
4839 // https://github.com/google/sanitizers/issues/321.
4840 double res = REAL(remquo)(x, y, quo);
4841 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4844 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4846 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4847 // FIXME: under ASan the call below may write to freed memory and corrupt
4848 // its metadata. See
4849 // https://github.com/google/sanitizers/issues/321.
4850 float res = REAL(remquof)(x, y, quo);
4851 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4854 #define INIT_REMQUO \
4855 COMMON_INTERCEPT_FUNCTION(remquo); \
4856 COMMON_INTERCEPT_FUNCTION(remquof);
4861 #if SANITIZER_INTERCEPT_REMQUOL
4862 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4864 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4865 // FIXME: under ASan the call below may write to freed memory and corrupt
4866 // its metadata. See
4867 // https://github.com/google/sanitizers/issues/321.
4868 long double res = REAL(remquol)(x, y, quo);
4869 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4872 #define INIT_REMQUOL \
4873 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4875 #define INIT_REMQUOL
4878 #if SANITIZER_INTERCEPT_LGAMMA
4880 INTERCEPTOR(double, lgamma, double x) {
4882 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4883 double res = REAL(lgamma)(x);
4884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4887 INTERCEPTOR(float, lgammaf, float x) {
4889 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4890 float res = REAL(lgammaf)(x);
4891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4894 #define INIT_LGAMMA \
4895 COMMON_INTERCEPT_FUNCTION(lgamma); \
4896 COMMON_INTERCEPT_FUNCTION(lgammaf);
4901 #if SANITIZER_INTERCEPT_LGAMMAL
4902 INTERCEPTOR(long double, lgammal, long double x) {
4904 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4905 long double res = REAL(lgammal)(x);
4906 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4909 #define INIT_LGAMMAL \
4910 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4912 #define INIT_LGAMMAL
4915 #if SANITIZER_INTERCEPT_LGAMMA_R
4916 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4918 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4919 // FIXME: under ASan the call below may write to freed memory and corrupt
4920 // its metadata. See
4921 // https://github.com/google/sanitizers/issues/321.
4922 double res = REAL(lgamma_r)(x, signp);
4923 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4926 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4928 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4929 // FIXME: under ASan the call below may write to freed memory and corrupt
4930 // its metadata. See
4931 // https://github.com/google/sanitizers/issues/321.
4932 float res = REAL(lgammaf_r)(x, signp);
4933 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4936 #define INIT_LGAMMA_R \
4937 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4938 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4940 #define INIT_LGAMMA_R
4943 #if SANITIZER_INTERCEPT_LGAMMAL_R
4944 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
4946 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
4947 // FIXME: under ASan the call below may write to freed memory and corrupt
4948 // its metadata. See
4949 // https://github.com/google/sanitizers/issues/321.
4950 long double res = REAL(lgammal_r)(x, signp);
4951 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4954 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
4956 #define INIT_LGAMMAL_R
4959 #if SANITIZER_INTERCEPT_DRAND48_R
4960 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
4962 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
4963 // FIXME: under ASan the call below may write to freed memory and corrupt
4964 // its metadata. See
4965 // https://github.com/google/sanitizers/issues/321.
4966 int res = REAL(drand48_r)(buffer, result);
4967 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4970 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
4972 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
4973 // FIXME: under ASan the call below may write to freed memory and corrupt
4974 // its metadata. See
4975 // https://github.com/google/sanitizers/issues/321.
4976 int res = REAL(lrand48_r)(buffer, result);
4977 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4980 #define INIT_DRAND48_R \
4981 COMMON_INTERCEPT_FUNCTION(drand48_r); \
4982 COMMON_INTERCEPT_FUNCTION(lrand48_r);
4984 #define INIT_DRAND48_R
4987 #if SANITIZER_INTERCEPT_RAND_R
4988 INTERCEPTOR(int, rand_r, unsigned *seedp) {
4990 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
4991 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
4992 return REAL(rand_r)(seedp);
4994 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
4999 #if SANITIZER_INTERCEPT_GETLINE
5000 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5002 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5003 // FIXME: under ASan the call below may write to freed memory and corrupt
5004 // its metadata. See
5005 // https://github.com/google/sanitizers/issues/321.
5006 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5008 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5010 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5015 // FIXME: under ASan the call below may write to freed memory and corrupt its
5017 // https://github.com/google/sanitizers/issues/321.
5018 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5021 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5022 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5024 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5025 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5026 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5031 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5033 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5035 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5036 // with its own body.
5037 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5039 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5041 #define INIT_GETLINE \
5042 COMMON_INTERCEPT_FUNCTION(getline); \
5043 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5044 COMMON_INTERCEPT_FUNCTION(getdelim);
5046 #define INIT_GETLINE
5049 #if SANITIZER_INTERCEPT_ICONV
5050 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5051 char **outbuf, SIZE_T *outbytesleft) {
5053 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5056 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5057 if (inbuf && inbytesleft)
5058 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5060 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5061 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5062 // FIXME: under ASan the call below may write to freed memory and corrupt
5063 // its metadata. See
5064 // https://github.com/google/sanitizers/issues/321.
5065 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5066 if (outbuf && *outbuf > outbuf_orig) {
5067 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5068 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5072 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5077 #if SANITIZER_INTERCEPT_TIMES
5078 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5080 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5081 // FIXME: under ASan the call below may write to freed memory and corrupt
5082 // its metadata. See
5083 // https://github.com/google/sanitizers/issues/321.
5084 __sanitizer_clock_t res = REAL(times)(tms);
5085 if (res != (__sanitizer_clock_t)-1 && tms)
5086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5089 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5094 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5096 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5097 // If you see any crashes around this functions, there are 2 known issues with
5098 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5099 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5100 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5101 // to access thread local variables (it should not happen normally,
5102 // because sanitizers use initial-exec tls model).
5103 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5105 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5106 void *res = REAL(__tls_get_addr)(arg);
5107 uptr tls_begin, tls_end;
5108 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5109 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5111 // New DTLS block has been allocated.
5112 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5117 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5118 // mostly the same semantics as __tls_get_addr, but its presence enables
5119 // some optimizations in linker (which are safe to ignore here).
5120 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5121 visibility("default")))
5122 void *__tls_get_addr_opt(void *arg);
5124 #else // SANITIZER_S390
5125 // On s390, we have to intercept two functions here:
5126 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5127 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5128 // a proper pointer. It is used by dlsym for TLS symbols.
5129 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5130 // descriptor offset as an argument instead of a pointer. GOT address
5131 // is passed in r12, so it's necessary to write it in assembly. This is
5132 // the function used by the compiler.
5133 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5134 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5135 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5136 extern "C" uptr __tls_get_offset(void *arg);
5137 extern "C" uptr __interceptor___tls_get_offset(void *arg);
5138 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5140 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5141 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5142 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5143 void *ptr = reinterpret_cast<void *>(res + tp);
5144 uptr tls_begin, tls_end;
5145 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5146 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5148 // New DTLS block has been allocated.
5149 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5153 // We need a hidden symbol aliasing the above, so that we can jump
5154 // directly to it from the assembly below.
5155 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5156 visibility("hidden")))
5157 uptr __tls_get_addr_hidden(void *arg);
5158 // Now carefully intercept __tls_get_offset.
5161 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5162 // exports our symbol.
5163 ".weak __tls_get_offset\n"
5164 ".type __tls_get_offset, @function\n"
5165 "__tls_get_offset:\n"
5166 ".global __interceptor___tls_get_offset\n"
5167 ".type __interceptor___tls_get_offset, @function\n"
5168 "__interceptor___tls_get_offset:\n"
5170 "la %r2, 0(%r2,%r12)\n"
5171 "jg __tls_get_addr_hidden\n"
5174 "0: la %r2,0(%r2,%r12)\n"
5175 "l %r4,1f-0b(%r3)\n"
5177 "1: .long __tls_get_addr_hidden - 0b\n"
5179 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5180 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5181 ".type __tls_get_offset_wrapper, @function\n"
5182 "__tls_get_offset_wrapper:\n"
5189 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5191 #endif // SANITIZER_S390
5193 #define INIT_TLS_GET_ADDR
5196 #if SANITIZER_INTERCEPT_LISTXATTR
5197 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5199 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5200 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5201 // FIXME: under ASan the call below may write to freed memory and corrupt
5202 // its metadata. See
5203 // https://github.com/google/sanitizers/issues/321.
5204 SSIZE_T res = REAL(listxattr)(path, list, size);
5205 // Here and below, size == 0 is a special case where nothing is written to the
5206 // buffer, and res contains the desired buffer size.
5207 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5210 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5212 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5213 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5214 // FIXME: under ASan the call below may write to freed memory and corrupt
5215 // its metadata. See
5216 // https://github.com/google/sanitizers/issues/321.
5217 SSIZE_T res = REAL(llistxattr)(path, list, size);
5218 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5221 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5223 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5224 // FIXME: under ASan the call below may write to freed memory and corrupt
5225 // its metadata. See
5226 // https://github.com/google/sanitizers/issues/321.
5227 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5228 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5231 #define INIT_LISTXATTR \
5232 COMMON_INTERCEPT_FUNCTION(listxattr); \
5233 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5234 COMMON_INTERCEPT_FUNCTION(flistxattr);
5236 #define INIT_LISTXATTR
5239 #if SANITIZER_INTERCEPT_GETXATTR
5240 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5243 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5244 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5245 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5246 // FIXME: under ASan the call below may write to freed memory and corrupt
5247 // its metadata. See
5248 // https://github.com/google/sanitizers/issues/321.
5249 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5250 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5253 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5256 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5257 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5258 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5259 // FIXME: under ASan the call below may write to freed memory and corrupt
5260 // its metadata. See
5261 // https://github.com/google/sanitizers/issues/321.
5262 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5263 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5266 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5269 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5270 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5271 // FIXME: under ASan the call below may write to freed memory and corrupt
5272 // its metadata. See
5273 // https://github.com/google/sanitizers/issues/321.
5274 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5275 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5278 #define INIT_GETXATTR \
5279 COMMON_INTERCEPT_FUNCTION(getxattr); \
5280 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5281 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5283 #define INIT_GETXATTR
5286 #if SANITIZER_INTERCEPT_GETRESID
5287 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5289 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5290 // FIXME: under ASan the call below may write to freed memory and corrupt
5291 // its metadata. See
5292 // https://github.com/google/sanitizers/issues/321.
5293 int res = REAL(getresuid)(ruid, euid, suid);
5295 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5296 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5297 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5301 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5303 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5304 // FIXME: under ASan the call below may write to freed memory and corrupt
5305 // its metadata. See
5306 // https://github.com/google/sanitizers/issues/321.
5307 int res = REAL(getresgid)(rgid, egid, sgid);
5309 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5310 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5311 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5315 #define INIT_GETRESID \
5316 COMMON_INTERCEPT_FUNCTION(getresuid); \
5317 COMMON_INTERCEPT_FUNCTION(getresgid);
5319 #define INIT_GETRESID
5322 #if SANITIZER_INTERCEPT_GETIFADDRS
5323 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5324 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5325 // intercept it to poison the memory again.
5326 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5328 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5329 // FIXME: under ASan the call below may write to freed memory and corrupt
5330 // its metadata. See
5331 // https://github.com/google/sanitizers/issues/321.
5332 int res = REAL(getifaddrs)(ifap);
5333 if (res == 0 && ifap) {
5334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5335 __sanitizer_ifaddrs *p = *ifap;
5337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5339 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5340 REAL(strlen)(p->ifa_name) + 1);
5342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5344 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5345 // On Linux this is a union, but the other member also points to a
5346 // struct sockaddr, so the following is sufficient.
5348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5349 // FIXME(smatveev): Unpoison p->ifa_data as well.
5355 #define INIT_GETIFADDRS \
5356 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5358 #define INIT_GETIFADDRS
5361 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5362 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5364 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5365 // FIXME: under ASan the call below may write to freed memory and corrupt
5366 // its metadata. See
5367 // https://github.com/google/sanitizers/issues/321.
5368 char *res = REAL(if_indextoname)(ifindex, ifname);
5370 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5373 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5375 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5377 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5378 return REAL(if_nametoindex)(ifname);
5380 #define INIT_IF_INDEXTONAME \
5381 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5382 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5384 #define INIT_IF_INDEXTONAME
5387 #if SANITIZER_INTERCEPT_CAPGET
5388 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5390 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5392 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5393 // FIXME: under ASan the call below may write to freed memory and corrupt
5394 // its metadata. See
5395 // https://github.com/google/sanitizers/issues/321.
5396 int res = REAL(capget)(hdrp, datap);
5397 if (res == 0 && datap)
5398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5399 // We can also return -1 and write to hdrp->version if the version passed in
5400 // hdrp->version is unsupported. But that's not a trivial condition to check,
5401 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5404 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5406 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5408 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5410 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5411 return REAL(capset)(hdrp, datap);
5413 #define INIT_CAPGET \
5414 COMMON_INTERCEPT_FUNCTION(capget); \
5415 COMMON_INTERCEPT_FUNCTION(capset);
5420 #if SANITIZER_INTERCEPT_AEABI_MEM
5421 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5423 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5426 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5428 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5431 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5433 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5436 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5438 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5441 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5443 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5446 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5448 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5451 // Note the argument order.
5452 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5454 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5457 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5459 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5462 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5464 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5467 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5469 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5472 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5474 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5477 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5479 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5482 #define INIT_AEABI_MEM \
5483 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5484 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5485 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5486 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5487 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5488 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5489 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5490 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5491 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5492 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5493 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5494 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5496 #define INIT_AEABI_MEM
5497 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5499 #if SANITIZER_INTERCEPT___BZERO
5500 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5502 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5505 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5507 #define INIT___BZERO
5508 #endif // SANITIZER_INTERCEPT___BZERO
5510 #if SANITIZER_INTERCEPT_FTIME
5511 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5513 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5514 // FIXME: under ASan the call below may write to freed memory and corrupt
5515 // its metadata. See
5516 // https://github.com/google/sanitizers/issues/321.
5517 int res = REAL(ftime)(tp);
5519 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5522 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5525 #endif // SANITIZER_INTERCEPT_FTIME
5527 #if SANITIZER_INTERCEPT_XDR
5528 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5529 unsigned size, int op) {
5531 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5532 // FIXME: under ASan the call below may write to freed memory and corrupt
5533 // its metadata. See
5534 // https://github.com/google/sanitizers/issues/321.
5535 REAL(xdrmem_create)(xdrs, addr, size, op);
5536 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5537 if (op == __sanitizer_XDR_ENCODE) {
5538 // It's not obvious how much data individual xdr_ routines write.
5539 // Simply unpoison the entire target buffer in advance.
5540 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5544 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5546 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5547 // FIXME: under ASan the call below may write to freed memory and corrupt
5548 // its metadata. See
5549 // https://github.com/google/sanitizers/issues/321.
5550 REAL(xdrstdio_create)(xdrs, file, op);
5551 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5554 // FIXME: under ASan the call below may write to freed memory and corrupt
5555 // its metadata. See
5556 // https://github.com/google/sanitizers/issues/321.
5557 #define XDR_INTERCEPTOR(F, T) \
5558 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5560 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5561 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5562 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5563 int res = REAL(F)(xdrs, p); \
5564 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5569 XDR_INTERCEPTOR(xdr_short, short)
5570 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5571 XDR_INTERCEPTOR(xdr_int, int)
5572 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5573 XDR_INTERCEPTOR(xdr_long, long)
5574 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5575 XDR_INTERCEPTOR(xdr_hyper, long long)
5576 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5577 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5578 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5579 XDR_INTERCEPTOR(xdr_int8_t, u8)
5580 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5581 XDR_INTERCEPTOR(xdr_int16_t, u16)
5582 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5583 XDR_INTERCEPTOR(xdr_int32_t, u32)
5584 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5585 XDR_INTERCEPTOR(xdr_int64_t, u64)
5586 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5587 XDR_INTERCEPTOR(xdr_quad_t, long long)
5588 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5589 XDR_INTERCEPTOR(xdr_bool, bool)
5590 XDR_INTERCEPTOR(xdr_enum, int)
5591 XDR_INTERCEPTOR(xdr_char, char)
5592 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5593 XDR_INTERCEPTOR(xdr_float, float)
5594 XDR_INTERCEPTOR(xdr_double, double)
5596 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5597 // wrapstring, sizeof
5599 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5602 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5603 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5604 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5605 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5606 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5608 // FIXME: under ASan the call below may write to freed memory and corrupt
5609 // its metadata. See
5610 // https://github.com/google/sanitizers/issues/321.
5611 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5612 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5614 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5615 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5620 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5623 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5624 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5625 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5626 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5628 // FIXME: under ASan the call below may write to freed memory and corrupt
5629 // its metadata. See
5630 // https://github.com/google/sanitizers/issues/321.
5631 int res = REAL(xdr_string)(xdrs, p, maxsize);
5632 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5633 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5635 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5641 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5642 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5643 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5644 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5645 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5646 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5647 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5648 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5649 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5650 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5651 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5652 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5653 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5654 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5655 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5656 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5657 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5658 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5659 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5660 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5661 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5662 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5663 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5664 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5665 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5666 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5667 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5668 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5669 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5670 COMMON_INTERCEPT_FUNCTION(xdr_string);
5673 #endif // SANITIZER_INTERCEPT_XDR
5675 #if SANITIZER_INTERCEPT_TSEARCH
5676 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5677 int (*compar)(const void *, const void *)) {
5679 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5680 // FIXME: under ASan the call below may write to freed memory and corrupt
5681 // its metadata. See
5682 // https://github.com/google/sanitizers/issues/321.
5683 void *res = REAL(tsearch)(key, rootp, compar);
5684 if (res && *(void **)res == key)
5685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5688 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5690 #define INIT_TSEARCH
5693 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5694 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5695 void unpoison_file(__sanitizer_FILE *fp) {
5696 #if SANITIZER_HAS_STRUCT_FILE
5697 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5698 #if SANITIZER_NETBSD
5699 if (fp->_bf._base && fp->_bf._size > 0)
5700 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
5703 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5704 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5705 fp->_IO_read_end - fp->_IO_read_base);
5707 #endif // SANITIZER_HAS_STRUCT_FILE
5711 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5712 // These guys are called when a .c source is built with -O2.
5713 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5715 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5716 int res = REAL(__uflow)(fp);
5720 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5722 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5723 int res = REAL(__underflow)(fp);
5727 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5729 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5730 int res = REAL(__overflow)(fp, ch);
5734 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5736 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5737 int res = REAL(__wuflow)(fp);
5741 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5743 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5744 int res = REAL(__wunderflow)(fp);
5748 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5750 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5751 int res = REAL(__woverflow)(fp, ch);
5755 #define INIT_LIBIO_INTERNALS \
5756 COMMON_INTERCEPT_FUNCTION(__uflow); \
5757 COMMON_INTERCEPT_FUNCTION(__underflow); \
5758 COMMON_INTERCEPT_FUNCTION(__overflow); \
5759 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5760 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5761 COMMON_INTERCEPT_FUNCTION(__woverflow);
5763 #define INIT_LIBIO_INTERNALS
5766 #if SANITIZER_INTERCEPT_FOPEN
5767 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5769 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5770 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5771 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5772 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5773 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5774 if (res) unpoison_file(res);
5777 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5779 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5780 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5781 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5782 if (res) unpoison_file(res);
5785 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5786 __sanitizer_FILE *fp) {
5788 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5789 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5790 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5791 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5792 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5793 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5794 if (res) unpoison_file(res);
5797 #define INIT_FOPEN \
5798 COMMON_INTERCEPT_FUNCTION(fopen); \
5799 COMMON_INTERCEPT_FUNCTION(fdopen); \
5800 COMMON_INTERCEPT_FUNCTION(freopen);
5805 #if SANITIZER_INTERCEPT_FOPEN64
5806 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5808 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5809 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5810 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5811 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5812 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5813 if (res) unpoison_file(res);
5816 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5817 __sanitizer_FILE *fp) {
5819 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5820 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5821 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5822 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5823 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5824 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5825 if (res) unpoison_file(res);
5828 #define INIT_FOPEN64 \
5829 COMMON_INTERCEPT_FUNCTION(fopen64); \
5830 COMMON_INTERCEPT_FUNCTION(freopen64);
5832 #define INIT_FOPEN64
5835 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5836 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5838 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5839 // FIXME: under ASan the call below may write to freed memory and corrupt
5840 // its metadata. See
5841 // https://github.com/google/sanitizers/issues/321.
5842 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5847 FileMetadata file = {ptr, sizeloc};
5848 SetInterceptorMetadata(res, file);
5852 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5855 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5856 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5861 FileMetadata file = {(char **)ptr, sizeloc};
5862 SetInterceptorMetadata(res, file);
5866 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5869 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5870 // FIXME: under ASan the call below may write to freed memory and corrupt
5871 // its metadata. See
5872 // https://github.com/google/sanitizers/issues/321.
5873 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5874 if (res) unpoison_file(res);
5877 #define INIT_OPEN_MEMSTREAM \
5878 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5879 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5880 COMMON_INTERCEPT_FUNCTION(fmemopen);
5882 #define INIT_OPEN_MEMSTREAM
5885 #if SANITIZER_INTERCEPT_OBSTACK
5886 static void initialize_obstack(__sanitizer_obstack *obstack) {
5887 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5889 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5890 sizeof(*obstack->chunk));
5893 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5894 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5895 void (*free_fn)(uptr arg, void *p)) {
5897 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5899 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5900 if (res) initialize_obstack(obstack);
5903 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5904 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5906 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5908 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5909 if (res) initialize_obstack(obstack);
5912 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5914 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5915 REAL(_obstack_newchunk)(obstack, length);
5917 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5918 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5920 #define INIT_OBSTACK \
5921 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5922 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5923 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5925 #define INIT_OBSTACK
5928 #if SANITIZER_INTERCEPT_FFLUSH
5929 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5931 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5932 int res = REAL(fflush)(fp);
5933 // FIXME: handle fp == NULL
5935 const FileMetadata *m = GetInterceptorMetadata(fp);
5936 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5940 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
5945 #if SANITIZER_INTERCEPT_FCLOSE
5946 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
5948 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
5949 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5950 const FileMetadata *m = GetInterceptorMetadata(fp);
5951 int res = REAL(fclose)(fp);
5953 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
5954 DeleteInterceptorMetadata(fp);
5958 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
5963 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
5964 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
5966 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
5967 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
5968 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
5969 void *res = REAL(dlopen)(filename, flag);
5970 Symbolizer::GetOrInit()->InvalidateModuleList();
5971 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
5975 INTERCEPTOR(int, dlclose, void *handle) {
5977 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
5978 int res = REAL(dlclose)(handle);
5979 Symbolizer::GetOrInit()->InvalidateModuleList();
5980 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
5983 #define INIT_DLOPEN_DLCLOSE \
5984 COMMON_INTERCEPT_FUNCTION(dlopen); \
5985 COMMON_INTERCEPT_FUNCTION(dlclose);
5987 #define INIT_DLOPEN_DLCLOSE
5990 #if SANITIZER_INTERCEPT_GETPASS
5991 INTERCEPTOR(char *, getpass, const char *prompt) {
5993 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
5995 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
5996 char *res = REAL(getpass)(prompt);
5997 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
6001 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6003 #define INIT_GETPASS
6006 #if SANITIZER_INTERCEPT_TIMERFD
6007 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6010 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6012 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6013 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6014 if (res != -1 && old_value)
6015 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6019 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6021 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6022 int res = REAL(timerfd_gettime)(fd, curr_value);
6023 if (res != -1 && curr_value)
6024 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6027 #define INIT_TIMERFD \
6028 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6029 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6031 #define INIT_TIMERFD
6034 #if SANITIZER_INTERCEPT_MLOCKX
6035 // Linux kernel has a bug that leads to kernel deadlock if a process
6036 // maps TBs of memory and then calls mlock().
6037 static void MlockIsUnsupported() {
6038 static atomic_uint8_t printed;
6039 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6041 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6045 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6046 MlockIsUnsupported();
6050 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6051 MlockIsUnsupported();
6055 INTERCEPTOR(int, mlockall, int flags) {
6056 MlockIsUnsupported();
6060 INTERCEPTOR(int, munlockall, void) {
6061 MlockIsUnsupported();
6065 #define INIT_MLOCKX \
6066 COMMON_INTERCEPT_FUNCTION(mlock); \
6067 COMMON_INTERCEPT_FUNCTION(munlock); \
6068 COMMON_INTERCEPT_FUNCTION(mlockall); \
6069 COMMON_INTERCEPT_FUNCTION(munlockall);
6073 #endif // SANITIZER_INTERCEPT_MLOCKX
6075 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6076 struct WrappedCookie {
6078 __sanitizer_cookie_io_functions_t real_io_funcs;
6081 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6082 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6083 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6084 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6085 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6088 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6089 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6090 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6091 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6092 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6095 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6096 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6097 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6098 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6099 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6100 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6104 static int wrapped_close(void *cookie) {
6105 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6106 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6107 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6108 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6109 InternalFree(wrapped_cookie);
6113 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6114 __sanitizer_cookie_io_functions_t io_funcs) {
6116 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6117 WrappedCookie *wrapped_cookie =
6118 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6119 wrapped_cookie->real_cookie = cookie;
6120 wrapped_cookie->real_io_funcs = io_funcs;
6121 __sanitizer_FILE *res =
6122 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6123 wrapped_seek, wrapped_close});
6127 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6129 #define INIT_FOPENCOOKIE
6130 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6132 #if SANITIZER_INTERCEPT_SEM
6133 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6135 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6136 // Workaround a bug in glibc's "old" semaphore implementation by
6137 // zero-initializing the sem_t contents. This has to be done here because
6138 // interceptors bind to the lowest symbols version by default, hitting the
6139 // buggy code path while the non-sanitized build of the same code works fine.
6140 REAL(memset)(s, 0, sizeof(*s));
6141 int res = REAL(sem_init)(s, pshared, value);
6145 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6147 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6148 int res = REAL(sem_destroy)(s);
6152 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6154 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6155 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6157 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6162 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6164 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6165 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6167 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6172 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6174 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6175 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6176 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6178 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6183 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6185 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6186 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6187 int res = REAL(sem_post)(s);
6191 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6193 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6194 int res = REAL(sem_getvalue)(s, sval);
6196 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6197 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6202 COMMON_INTERCEPT_FUNCTION(sem_init); \
6203 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6204 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6205 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6206 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6207 COMMON_INTERCEPT_FUNCTION(sem_post); \
6208 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6211 #endif // SANITIZER_INTERCEPT_SEM
6213 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6214 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6216 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6217 int res = REAL(pthread_setcancelstate)(state, oldstate);
6218 if (res == 0 && oldstate != nullptr)
6219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6223 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6225 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6226 int res = REAL(pthread_setcanceltype)(type, oldtype);
6227 if (res == 0 && oldtype != nullptr)
6228 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6231 #define INIT_PTHREAD_SETCANCEL \
6232 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6233 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6235 #define INIT_PTHREAD_SETCANCEL
6238 #if SANITIZER_INTERCEPT_MINCORE
6239 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6241 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6242 int res = REAL(mincore)(addr, length, vec);
6244 uptr page_size = GetPageSizeCached();
6245 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6250 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6252 #define INIT_MINCORE
6255 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6256 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6257 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6260 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6261 remote_iov, riovcnt, flags);
6262 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6265 write_iovec(ctx, local_iov, liovcnt, res);
6269 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6270 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6273 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6274 remote_iov, riovcnt, flags);
6275 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6278 read_iovec(ctx, local_iov, liovcnt, res);
6281 #define INIT_PROCESS_VM_READV \
6282 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6283 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6285 #define INIT_PROCESS_VM_READV
6288 #if SANITIZER_INTERCEPT_CTERMID
6289 INTERCEPTOR(char *, ctermid, char *s) {
6291 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6292 char *res = REAL(ctermid)(s);
6294 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6298 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6300 #define INIT_CTERMID
6303 #if SANITIZER_INTERCEPT_CTERMID_R
6304 INTERCEPTOR(char *, ctermid_r, char *s) {
6306 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6307 char *res = REAL(ctermid_r)(s);
6309 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6313 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6315 #define INIT_CTERMID_R
6318 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6319 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6321 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6322 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6323 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6327 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6331 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6332 void *srcaddr, int *addrlen) {
6334 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6336 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6338 if (srcaddr) srcaddr_sz = *addrlen;
6339 (void)srcaddr_sz; // prevent "set but not used" warning
6340 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6344 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6345 Min((SIZE_T)*addrlen, srcaddr_sz));
6349 #define INIT_RECV_RECVFROM \
6350 COMMON_INTERCEPT_FUNCTION(recv); \
6351 COMMON_INTERCEPT_FUNCTION(recvfrom);
6353 #define INIT_RECV_RECVFROM
6356 #if SANITIZER_INTERCEPT_SEND_SENDTO
6357 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6359 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6361 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6362 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6364 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6365 if (common_flags()->intercept_send && res > 0)
6366 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6370 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6371 void *dstaddr, int addrlen) {
6373 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6375 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6376 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6378 // Can't check dstaddr as it may have uninitialized padding at the end.
6379 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6380 if (common_flags()->intercept_send && res > 0)
6381 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6384 #define INIT_SEND_SENDTO \
6385 COMMON_INTERCEPT_FUNCTION(send); \
6386 COMMON_INTERCEPT_FUNCTION(sendto);
6388 #define INIT_SEND_SENDTO
6391 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6392 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6394 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6395 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6396 int res = REAL(eventfd_read)(fd, value);
6398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6399 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6403 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6405 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6407 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6408 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6410 int res = REAL(eventfd_write)(fd, value);
6413 #define INIT_EVENTFD_READ_WRITE \
6414 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6415 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6417 #define INIT_EVENTFD_READ_WRITE
6420 #if SANITIZER_INTERCEPT_STAT
6421 INTERCEPTOR(int, stat, const char *path, void *buf) {
6423 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6424 if (common_flags()->intercept_stat)
6425 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6426 int res = REAL(stat)(path, buf);
6428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6431 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6436 #if SANITIZER_INTERCEPT_LSTAT
6437 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6439 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6440 if (common_flags()->intercept_stat)
6441 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6442 int res = REAL(lstat)(path, buf);
6444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6447 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6452 #if SANITIZER_INTERCEPT___XSTAT
6453 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6455 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6456 if (common_flags()->intercept_stat)
6457 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6458 int res = REAL(__xstat)(version, path, buf);
6460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6463 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6465 #define INIT___XSTAT
6468 #if SANITIZER_INTERCEPT___XSTAT64
6469 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6471 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6472 if (common_flags()->intercept_stat)
6473 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6474 int res = REAL(__xstat64)(version, path, buf);
6476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6479 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6481 #define INIT___XSTAT64
6484 #if SANITIZER_INTERCEPT___LXSTAT
6485 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6487 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6488 if (common_flags()->intercept_stat)
6489 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6490 int res = REAL(__lxstat)(version, path, buf);
6492 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6495 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6497 #define INIT___LXSTAT
6500 #if SANITIZER_INTERCEPT___LXSTAT64
6501 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6503 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6504 if (common_flags()->intercept_stat)
6505 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6506 int res = REAL(__lxstat64)(version, path, buf);
6508 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6511 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6513 #define INIT___LXSTAT64
6516 // FIXME: add other *stat interceptor
6518 #if SANITIZER_INTERCEPT_UTMP
6519 INTERCEPTOR(void *, getutent, int dummy) {
6521 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6522 void *res = REAL(getutent)(dummy);
6524 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6527 INTERCEPTOR(void *, getutid, void *ut) {
6529 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6530 void *res = REAL(getutid)(ut);
6532 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6535 INTERCEPTOR(void *, getutline, void *ut) {
6537 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6538 void *res = REAL(getutline)(ut);
6540 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6544 COMMON_INTERCEPT_FUNCTION(getutent); \
6545 COMMON_INTERCEPT_FUNCTION(getutid); \
6546 COMMON_INTERCEPT_FUNCTION(getutline);
6551 #if SANITIZER_INTERCEPT_UTMPX
6552 INTERCEPTOR(void *, getutxent, int dummy) {
6554 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6555 void *res = REAL(getutxent)(dummy);
6557 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6560 INTERCEPTOR(void *, getutxid, void *ut) {
6562 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6563 void *res = REAL(getutxid)(ut);
6565 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6568 INTERCEPTOR(void *, getutxline, void *ut) {
6570 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6571 void *res = REAL(getutxline)(ut);
6573 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6576 INTERCEPTOR(void *, pututxline, const void *ut) {
6578 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
6580 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
6581 void *res = REAL(pututxline)(ut);
6583 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
6586 #define INIT_UTMPX \
6587 COMMON_INTERCEPT_FUNCTION(getutxent); \
6588 COMMON_INTERCEPT_FUNCTION(getutxid); \
6589 COMMON_INTERCEPT_FUNCTION(getutxline); \
6590 COMMON_INTERCEPT_FUNCTION(pututxline);
6595 #if SANITIZER_INTERCEPT_GETLOADAVG
6596 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6598 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6599 int res = REAL(getloadavg)(loadavg, nelem);
6601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6604 #define INIT_GETLOADAVG \
6605 COMMON_INTERCEPT_FUNCTION(getloadavg);
6607 #define INIT_GETLOADAVG
6610 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6611 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6615 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6619 INTERCEPTOR(int, mprobe, void *ptr) {
6624 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6626 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6627 SIZE_T res = REAL(wcslen)(s);
6628 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6632 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6634 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6635 SIZE_T res = REAL(wcsnlen)(s, n);
6636 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6639 #define INIT_WCSLEN \
6640 COMMON_INTERCEPT_FUNCTION(wcslen); \
6641 COMMON_INTERCEPT_FUNCTION(wcsnlen);
6643 #if SANITIZER_INTERCEPT_WCSCAT
6644 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6646 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6647 SIZE_T src_size = REAL(wcslen)(src);
6648 SIZE_T dst_size = REAL(wcslen)(dst);
6649 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6650 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6651 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6652 (src_size + 1) * sizeof(wchar_t));
6653 return REAL(wcscat)(dst, src); // NOLINT
6656 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6658 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6659 SIZE_T src_size = REAL(wcsnlen)(src, n);
6660 SIZE_T dst_size = REAL(wcslen)(dst);
6661 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6662 Min(src_size + 1, n) * sizeof(wchar_t));
6663 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6664 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6665 (src_size + 1) * sizeof(wchar_t));
6666 return REAL(wcsncat)(dst, src, n); // NOLINT
6668 #define INIT_WCSCAT \
6669 COMMON_INTERCEPT_FUNCTION(wcscat); \
6670 COMMON_INTERCEPT_FUNCTION(wcsncat);
6675 #if SANITIZER_INTERCEPT_STRXFRM
6676 static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6678 static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6680 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
6683 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6684 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
6685 sizeof(*src) * (RealStrLen(src) + 1)); \
6686 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
6688 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6692 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6693 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6696 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6698 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6701 #define INIT_STRXFRM \
6702 COMMON_INTERCEPT_FUNCTION(strxfrm); \
6703 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6705 #define INIT_STRXFRM
6708 #if SANITIZER_INTERCEPT___STRXFRM_L
6709 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6711 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6714 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6716 #define INIT___STRXFRM_L
6719 #if SANITIZER_INTERCEPT_WCSXFRM
6720 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6721 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6724 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6726 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6729 #define INIT_WCSXFRM \
6730 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6731 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6733 #define INIT_WCSXFRM
6736 #if SANITIZER_INTERCEPT___WCSXFRM_L
6737 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6739 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6742 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6744 #define INIT___WCSXFRM_L
6747 #if SANITIZER_INTERCEPT_ACCT
6748 INTERCEPTOR(int, acct, const char *file) {
6750 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6752 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6753 return REAL(acct)(file);
6755 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6760 #if SANITIZER_INTERCEPT_USER_FROM_UID
6761 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6764 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6765 user = REAL(user_from_uid)(uid, nouser);
6767 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6770 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6772 #define INIT_USER_FROM_UID
6775 #if SANITIZER_INTERCEPT_UID_FROM_USER
6776 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6779 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6781 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6782 res = REAL(uid_from_user)(name, uid);
6784 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6787 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6789 #define INIT_UID_FROM_USER
6792 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
6793 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6796 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6797 group = REAL(group_from_gid)(gid, nogroup);
6799 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6802 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6804 #define INIT_GROUP_FROM_GID
6807 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
6808 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6811 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6813 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6814 res = REAL(gid_from_group)(group, gid);
6816 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6819 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6821 #define INIT_GID_FROM_GROUP
6824 #if SANITIZER_INTERCEPT_ACCESS
6825 INTERCEPTOR(int, access, const char *path, int mode) {
6827 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6829 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6830 return REAL(access)(path, mode);
6832 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6837 #if SANITIZER_INTERCEPT_FACCESSAT
6838 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6840 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6842 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6843 return REAL(faccessat)(fd, path, mode, flags);
6845 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6847 #define INIT_FACCESSAT
6850 #if SANITIZER_INTERCEPT_GETGROUPLIST
6851 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6855 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6857 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6859 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6860 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6861 if (!res && groups && ngroups) {
6862 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6863 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6868 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6870 #define INIT_GETGROUPLIST
6873 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6874 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6875 int maxgrp, int *ngroups) {
6878 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6881 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6882 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6883 if (!res && groups && ngroups) {
6884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6890 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6892 #define INIT_GETGROUPMEMBERSHIP
6895 #if SANITIZER_INTERCEPT_READLINK
6896 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6898 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6899 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6900 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6902 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6906 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6908 #define INIT_READLINK
6911 #if SANITIZER_INTERCEPT_READLINKAT
6912 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
6915 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
6916 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6917 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
6919 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6923 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
6925 #define INIT_READLINKAT
6928 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
6929 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
6930 struct file_handle *handle, int *mount_id, int flags) {
6932 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
6934 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
6936 __sanitizer_file_handle *sanitizer_handle =
6937 reinterpret_cast<__sanitizer_file_handle*>(handle);
6938 COMMON_INTERCEPTOR_READ_RANGE(
6939 ctx, &sanitizer_handle->handle_bytes,
6940 sizeof(sanitizer_handle->handle_bytes));
6942 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
6944 COMMON_INTERCEPTOR_WRITE_RANGE(
6945 ctx, &sanitizer_handle->handle_bytes,
6946 sizeof(sanitizer_handle->handle_bytes));
6947 COMMON_INTERCEPTOR_WRITE_RANGE(
6948 ctx, &sanitizer_handle->handle_type,
6949 sizeof(sanitizer_handle->handle_type));
6950 COMMON_INTERCEPTOR_WRITE_RANGE(
6951 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
6957 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
6959 #define INIT_NAME_TO_HANDLE_AT
6962 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
6963 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
6966 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
6968 __sanitizer_file_handle *sanitizer_handle =
6969 reinterpret_cast<__sanitizer_file_handle*>(handle);
6970 COMMON_INTERCEPTOR_READ_RANGE(
6971 ctx, &sanitizer_handle->handle_bytes,
6972 sizeof(sanitizer_handle->handle_bytes));
6973 COMMON_INTERCEPTOR_READ_RANGE(
6974 ctx, &sanitizer_handle->handle_type,
6975 sizeof(sanitizer_handle->handle_type));
6976 COMMON_INTERCEPTOR_READ_RANGE(
6977 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
6979 return REAL(open_by_handle_at)(mount_fd, handle, flags);
6982 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
6984 #define INIT_OPEN_BY_HANDLE_AT
6987 #if SANITIZER_INTERCEPT_STRLCPY
6988 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
6991 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
6993 // Keep strnlen as macro argument, as macro may ignore it.
6994 COMMON_INTERCEPTOR_READ_STRING(
6995 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
6997 res = REAL(strlcpy)(dst, src, size);
6998 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
7002 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7005 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7006 // src is checked in the strlcpy() interceptor
7008 len = internal_strnlen(dst, size);
7009 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7011 // Reuse the rest of the code in the strlcpy() interceptor
7012 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7014 #define INIT_STRLCPY \
7015 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7016 COMMON_INTERCEPT_FUNCTION(strlcat);
7018 #define INIT_STRLCPY
7021 #if SANITIZER_INTERCEPT_MMAP
7022 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7025 if (common_flags()->detect_write_exec)
7026 ReportMmapWriteExec(prot);
7027 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7028 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7029 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7030 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7033 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7035 if (common_flags()->detect_write_exec)
7036 ReportMmapWriteExec(prot);
7037 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7038 return (int)internal_mprotect(addr, sz, prot);
7039 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7040 MprotectMallocZones(addr, prot);
7041 return REAL(mprotect)(addr, sz, prot);
7044 COMMON_INTERCEPT_FUNCTION(mmap); \
7045 COMMON_INTERCEPT_FUNCTION(mprotect);
7050 #if SANITIZER_INTERCEPT_MMAP64
7051 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7054 if (common_flags()->detect_write_exec)
7055 ReportMmapWriteExec(prot);
7056 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7057 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7058 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7059 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7061 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7066 #if SANITIZER_INTERCEPT_DEVNAME
7067 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7070 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7071 name = REAL(devname)(dev, type);
7073 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7076 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7078 #define INIT_DEVNAME
7081 #if SANITIZER_INTERCEPT_DEVNAME_R
7082 #if SANITIZER_NETBSD
7083 #define DEVNAME_R_RETTYPE int
7084 #define DEVNAME_R_SUCCESS(x) (!(x))
7086 #define DEVNAME_R_RETTYPE char*
7087 #define DEVNAME_R_SUCCESS(x) (x)
7089 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7092 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7093 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7094 if (DEVNAME_R_SUCCESS(res))
7095 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7098 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7100 #define INIT_DEVNAME_R
7103 #if SANITIZER_INTERCEPT_FGETLN
7104 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7106 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7107 char *str = REAL(fgetln)(stream, len);
7109 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7114 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7119 #if SANITIZER_INTERCEPT_STRMODE
7120 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7122 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7123 REAL(strmode)(mode, bp);
7125 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7127 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7129 #define INIT_STRMODE
7132 #if SANITIZER_INTERCEPT_TTYENT
7133 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7135 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7136 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7138 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7141 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7143 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7145 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7146 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7148 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7151 INTERCEPTOR(int, setttyentpath, char *path) {
7153 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7155 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7156 return REAL(setttyentpath)(path);
7158 #define INIT_TTYENT \
7159 COMMON_INTERCEPT_FUNCTION(getttyent); \
7160 COMMON_INTERCEPT_FUNCTION(getttynam); \
7161 COMMON_INTERCEPT_FUNCTION(setttyentpath)
7166 #if SANITIZER_INTERCEPT_PROTOENT
7167 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7169 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7170 struct __sanitizer_protoent *p = REAL(getprotoent)();
7172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7174 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7176 SIZE_T pp_size = 1; // One handles the trailing \0
7178 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7179 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7181 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7182 pp_size * sizeof(char **));
7187 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7189 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7191 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7192 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7196 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7198 SIZE_T pp_size = 1; // One handles the trailing \0
7200 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7201 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7204 pp_size * sizeof(char **));
7209 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7211 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7212 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7218 SIZE_T pp_size = 1; // One handles the trailing \0
7220 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7221 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7223 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7224 pp_size * sizeof(char **));
7228 #define INIT_PROTOENT \
7229 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7230 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7231 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7233 #define INIT_PROTOENT
7236 #if SANITIZER_INTERCEPT_NETENT
7237 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7239 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7240 struct __sanitizer_netent *n = REAL(getnetent)();
7242 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7246 SIZE_T nn_size = 1; // One handles the trailing \0
7248 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7251 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7252 nn_size * sizeof(char **));
7257 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7259 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7261 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7262 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7266 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7268 SIZE_T nn_size = 1; // One handles the trailing \0
7270 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7273 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7274 nn_size * sizeof(char **));
7279 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7281 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7282 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7286 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7288 SIZE_T nn_size = 1; // One handles the trailing \0
7290 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7294 nn_size * sizeof(char **));
7298 #define INIT_NETENT \
7299 COMMON_INTERCEPT_FUNCTION(getnetent); \
7300 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7301 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7306 #if SANITIZER_INTERCEPT_GETMNTINFO
7307 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7309 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7310 int cnt = REAL(getmntinfo)(mntbufp, flags);
7311 if (cnt > 0 && mntbufp) {
7312 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7314 #if SANITIZER_NETBSD
7315 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7322 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7324 #define INIT_GETMNTINFO
7327 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7328 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7331 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7333 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7334 REAL(mi_vector_hash)(key, len, seed, hashes);
7336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7338 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7340 #define INIT_MI_VECTOR_HASH
7343 #if SANITIZER_INTERCEPT_SETVBUF
7344 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7347 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7348 int ret = REAL(setvbuf)(stream, buf, mode, size);
7350 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7352 unpoison_file(stream);
7356 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7358 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7359 REAL(setbuf)(stream, buf);
7361 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7364 unpoison_file(stream);
7367 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
7369 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
7370 REAL(setbuffer)(stream, buf, mode);
7372 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7375 unpoison_file(stream);
7378 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7380 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7381 REAL(setlinebuf)(stream);
7383 unpoison_file(stream);
7385 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7386 COMMON_INTERCEPT_FUNCTION(setbuf); \
7387 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7388 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7390 #define INIT_SETVBUF
7393 #if SANITIZER_INTERCEPT_GETVFSSTAT
7394 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7396 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7397 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7402 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7404 #define INIT_GETVFSSTAT
7407 #if SANITIZER_INTERCEPT_REGEX
7408 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7410 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7412 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, REAL(strlen)(pattern) + 1);
7413 int res = REAL(regcomp)(preg, pattern, cflags);
7415 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7418 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7419 struct __sanitizer_regmatch *pmatch[], int eflags) {
7421 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7423 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7425 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, REAL(strlen)(string) + 1);
7426 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7431 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7432 SIZE_T errbuf_size) {
7434 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7436 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7437 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7439 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, REAL(strlen)(errbuf) + 1);
7442 INTERCEPTOR(void, regfree, const void *preg) {
7444 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7446 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7447 REAL(regfree)(preg);
7449 #define INIT_REGEX \
7450 COMMON_INTERCEPT_FUNCTION(regcomp); \
7451 COMMON_INTERCEPT_FUNCTION(regexec); \
7452 COMMON_INTERCEPT_FUNCTION(regerror); \
7453 COMMON_INTERCEPT_FUNCTION(regfree);
7458 #if SANITIZER_INTERCEPT_REGEXSUB
7459 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7460 const struct __sanitizer_regmatch *rm, const char *str) {
7462 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7464 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7465 // The implementation demands and hardcodes 10 elements
7467 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7469 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
7470 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7472 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
7475 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7476 const struct __sanitizer_regmatch *rm, const char *sstr) {
7478 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7480 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7481 // Hardcode 10 elements as this is hardcoded size
7483 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7485 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, REAL(strlen)(sstr) + 1);
7486 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7487 if (res > 0 && buf) {
7488 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, REAL(strlen)(*buf) + 1);
7494 #define INIT_REGEXSUB \
7495 COMMON_INTERCEPT_FUNCTION(regnsub); \
7496 COMMON_INTERCEPT_FUNCTION(regasub);
7498 #define INIT_REGEXSUB
7501 #if SANITIZER_INTERCEPT_FTS
7502 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7503 int (*compar)(void **, void **)) {
7505 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
7507 for (char *const *pa = path_argv; ; ++pa) {
7508 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
7511 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
7514 // TODO(kamil): handle compar callback
7515 void *fts = REAL(fts_open)(path_argv, options, compar);
7517 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
7521 INTERCEPTOR(void *, fts_read, void *ftsp) {
7523 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
7525 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7526 void *ftsent = REAL(fts_read)(ftsp);
7528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7532 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
7534 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
7536 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7537 void *ftsent = REAL(fts_children)(ftsp, options);
7539 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7543 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
7545 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
7547 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7549 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
7550 return REAL(fts_set)(ftsp, f, options);
7553 INTERCEPTOR(int, fts_close, void *ftsp) {
7555 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
7557 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7558 return REAL(fts_close)(ftsp);
7561 COMMON_INTERCEPT_FUNCTION(fts_open); \
7562 COMMON_INTERCEPT_FUNCTION(fts_read); \
7563 COMMON_INTERCEPT_FUNCTION(fts_children); \
7564 COMMON_INTERCEPT_FUNCTION(fts_set); \
7565 COMMON_INTERCEPT_FUNCTION(fts_close);
7570 #if SANITIZER_INTERCEPT_SYSCTL
7571 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
7572 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
7574 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7575 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
7576 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
7579 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
7581 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7583 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7584 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
7587 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7589 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7595 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
7596 void *newp, SIZE_T newlen) {
7598 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7599 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
7600 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
7603 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7605 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7607 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7608 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
7611 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7619 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
7622 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
7624 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7626 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7627 int res = REAL(sysctlnametomib)(sname, name, namelenp);
7630 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7632 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7638 #define INIT_SYSCTL \
7639 COMMON_INTERCEPT_FUNCTION(sysctl); \
7640 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
7641 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
7646 #if SANITIZER_INTERCEPT_ASYSCTL
7647 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
7649 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
7651 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
7652 void *res = REAL(asysctl)(name, namelen, len);
7654 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7655 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7660 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
7662 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
7664 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7665 void *res = REAL(asysctlbyname)(sname, len);
7667 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7668 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7672 #define INIT_ASYSCTL \
7673 COMMON_INTERCEPT_FUNCTION(asysctl); \
7674 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
7676 #define INIT_ASYSCTL
7679 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
7680 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
7681 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
7684 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
7687 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7689 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7691 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
7692 // Skip rnode, it's rarely used and not trivial to sanitize
7693 // It's also used mostly internally
7694 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
7697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7699 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
7704 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
7709 #define INIT_SYSCTLGETMIBINFO \
7710 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
7712 #define INIT_SYSCTLGETMIBINFO
7715 #if SANITIZER_INTERCEPT_NL_LANGINFO
7716 INTERCEPTOR(char *, nl_langinfo, long item) {
7718 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
7719 char *ret = REAL(nl_langinfo)(item);
7721 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7724 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
7726 #define INIT_NL_LANGINFO
7729 #if SANITIZER_INTERCEPT_MODCTL
7730 INTERCEPTOR(int, modctl, int operation, void *argp) {
7733 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
7735 if (operation == modctl_load) {
7737 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
7738 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
7739 if (ml->ml_filename)
7740 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
7741 REAL(strlen)(ml->ml_filename) + 1);
7743 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
7745 ret = REAL(modctl)(operation, argp);
7746 } else if (operation == modctl_unload) {
7748 const char *name = (const char *)argp;
7749 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7751 ret = REAL(modctl)(operation, argp);
7752 } else if (operation == modctl_stat) {
7754 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
7756 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
7757 iov_len = iov->iov_len;
7759 ret = REAL(modctl)(operation, argp);
7761 COMMON_INTERCEPTOR_WRITE_RANGE(
7762 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
7763 } else if (operation == modctl_exists)
7764 ret = REAL(modctl)(operation, argp);
7766 ret = REAL(modctl)(operation, argp);
7770 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
7775 #if SANITIZER_INTERCEPT_STRTONUM
7776 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
7777 long long maxval, const char **errstr) {
7779 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
7781 // TODO(kamil): Implement strtoll as a common inteceptor
7783 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
7784 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
7786 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
7788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
7790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1);
7794 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
7796 #define INIT_STRTONUM
7799 #if SANITIZER_INTERCEPT_FPARSELN
7800 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
7801 SIZE_T *lineno, const char delim[3], int flags) {
7803 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
7805 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
7807 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
7808 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
7810 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7812 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7814 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
7818 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
7820 #define INIT_FPARSELN
7823 #if SANITIZER_INTERCEPT_STATVFS1
7824 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
7826 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
7827 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7828 int res = REAL(statvfs1)(path, buf, flags);
7829 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7832 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
7834 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
7835 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7836 int res = REAL(fstatvfs1)(fd, buf, flags);
7838 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7840 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
7844 #define INIT_STATVFS1 \
7845 COMMON_INTERCEPT_FUNCTION(statvfs1); \
7846 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
7848 #define INIT_STATVFS1
7851 #if SANITIZER_INTERCEPT_STRTOI
7852 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
7853 INTMAX_T low, INTMAX_T high, int *rstatus) {
7855 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
7857 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
7858 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7864 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
7865 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
7867 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
7869 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
7870 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7872 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7875 #define INIT_STRTOI \
7876 COMMON_INTERCEPT_FUNCTION(strtoi); \
7877 COMMON_INTERCEPT_FUNCTION(strtou)
7882 #if SANITIZER_INTERCEPT_CAPSICUM
7883 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
7886 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
7888 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7889 __sanitizer_cap_rights_t *ret = \
7890 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
7892 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7896 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
7899 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
7901 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7902 __sanitizer_cap_rights_t *ret = \
7903 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
7905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7909 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
7912 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
7914 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7915 __sanitizer_cap_rights_t *ret = \
7916 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
7918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7922 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
7925 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
7927 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7928 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
7931 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
7932 __sanitizer_cap_rights_t *rights) {
7933 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
7936 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
7937 __sanitizer_cap_rights_t *rights) {
7938 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
7941 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
7942 __sanitizer_cap_rights_t *rights) {
7943 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
7946 INTERCEPTOR(bool, cap_rights_is_set,
7947 __sanitizer_cap_rights_t *rights) {
7948 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
7951 INTERCEPTOR(int, cap_rights_limit, int fd,
7952 const __sanitizer_cap_rights_t *rights) {
7954 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
7956 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
7958 return REAL(cap_rights_limit)(fd, rights);
7961 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
7963 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
7964 int ret = REAL(cap_rights_get)(fd, rights);
7966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
7971 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
7973 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
7975 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
7977 return REAL(cap_rights_is_valid(rights));
7980 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
7981 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
7983 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
7985 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
7987 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
7989 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
7994 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
7995 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
7997 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
7999 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8001 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8003 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8008 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8009 const __sanitizer_cap_rights *little) {
8011 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8013 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8015 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8017 return REAL(cap_rights_contains)(big, little);
8020 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8022 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8024 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8026 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8029 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8031 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8032 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8038 #define INIT_CAPSICUM \
8039 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8040 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8041 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8042 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8043 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8044 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8045 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8046 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8047 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8048 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8049 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8050 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8052 #define INIT_CAPSICUM
8055 #if SANITIZER_INTERCEPT_SHA1
8056 INTERCEPTOR(void, SHA1Init, void *context) {
8058 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8059 REAL(SHA1Init)(context);
8061 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8063 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8065 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8066 if (data && len > 0)
8067 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8069 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8070 REAL(SHA1Update)(context, data, len);
8072 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8074 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8076 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8078 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8079 REAL(SHA1Final)(digest, context);
8081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8083 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8085 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8087 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8089 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8090 REAL(SHA1Transform)(state, buffer);
8092 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8094 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8096 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8098 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8099 char *ret = REAL(SHA1End)(context, buf);
8101 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8104 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8106 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8108 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8109 char *ret = REAL(SHA1File)(filename, buf);
8111 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8114 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8117 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8119 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8120 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8122 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8125 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8127 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8129 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8130 char *ret = REAL(SHA1Data)(data, len, buf);
8132 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8136 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8137 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8138 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8139 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8140 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8141 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8142 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8143 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8148 #if SANITIZER_INTERCEPT_MD4
8149 INTERCEPTOR(void, MD4Init, void *context) {
8151 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8152 REAL(MD4Init)(context);
8154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8157 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8160 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8161 if (data && len > 0)
8162 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8164 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8165 REAL(MD4Update)(context, data, len);
8167 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8170 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8172 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8174 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8175 REAL(MD4Final)(digest, context);
8177 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8180 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8182 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8184 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8185 char *ret = REAL(MD4End)(context, buf);
8187 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8191 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8193 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8195 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8196 char *ret = REAL(MD4File)(filename, buf);
8198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8202 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8205 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8206 if (data && len > 0)
8207 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8208 char *ret = REAL(MD4Data)(data, len, buf);
8210 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8215 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8216 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8217 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8218 COMMON_INTERCEPT_FUNCTION(MD4End); \
8219 COMMON_INTERCEPT_FUNCTION(MD4File); \
8220 COMMON_INTERCEPT_FUNCTION(MD4Data)
8225 #if SANITIZER_INTERCEPT_RMD160
8226 INTERCEPTOR(void, RMD160Init, void *context) {
8228 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8229 REAL(RMD160Init)(context);
8231 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8233 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8235 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8236 if (data && len > 0)
8237 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8239 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8240 REAL(RMD160Update)(context, data, len);
8242 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8244 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8246 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8248 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8249 REAL(RMD160Final)(digest, context);
8251 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8253 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8255 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8257 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8259 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8260 REAL(RMD160Transform)(state, buffer);
8262 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8264 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8266 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8268 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8269 char *ret = REAL(RMD160End)(context, buf);
8271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8274 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8276 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8278 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8279 char *ret = REAL(RMD160File)(filename, buf);
8281 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8284 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8287 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8289 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8290 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8295 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8297 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8298 if (data && len > 0)
8299 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8300 char *ret = REAL(RMD160Data)(data, len, buf);
8302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8305 #define INIT_RMD160 \
8306 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8307 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8308 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8309 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8310 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8311 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8312 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8313 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8318 #if SANITIZER_INTERCEPT_MD5
8319 INTERCEPTOR(void, MD5Init, void *context) {
8321 COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8322 REAL(MD5Init)(context);
8324 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8327 INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8330 COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8331 if (data && len > 0)
8332 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8334 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8335 REAL(MD5Update)(context, data, len);
8337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8340 INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8342 COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8344 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8345 REAL(MD5Final)(digest, context);
8347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8350 INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8352 COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8354 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8355 char *ret = REAL(MD5End)(context, buf);
8357 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8361 INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8363 COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8365 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8366 char *ret = REAL(MD5File)(filename, buf);
8368 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8372 INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8375 COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8376 if (data && len > 0)
8377 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8378 char *ret = REAL(MD5Data)(data, len, buf);
8380 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8385 COMMON_INTERCEPT_FUNCTION(MD5Init); \
8386 COMMON_INTERCEPT_FUNCTION(MD5Update); \
8387 COMMON_INTERCEPT_FUNCTION(MD5Final); \
8388 COMMON_INTERCEPT_FUNCTION(MD5End); \
8389 COMMON_INTERCEPT_FUNCTION(MD5File); \
8390 COMMON_INTERCEPT_FUNCTION(MD5Data)
8395 #if SANITIZER_INTERCEPT_FSEEK
8396 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8398 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8399 return REAL(fseek)(stream, offset, whence);
8401 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8403 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8404 return REAL(fseeko)(stream, offset, whence);
8406 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8408 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8409 return REAL(ftell)(stream);
8411 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8413 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8414 return REAL(ftello)(stream);
8416 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8418 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8419 return REAL(rewind)(stream);
8421 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8423 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8424 int ret = REAL(fgetpos)(stream, pos);
8426 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8429 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8431 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8433 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8434 return REAL(fsetpos)(stream, pos);
8436 #define INIT_FSEEK \
8437 COMMON_INTERCEPT_FUNCTION(fseek); \
8438 COMMON_INTERCEPT_FUNCTION(fseeko); \
8439 COMMON_INTERCEPT_FUNCTION(ftell); \
8440 COMMON_INTERCEPT_FUNCTION(ftello); \
8441 COMMON_INTERCEPT_FUNCTION(rewind); \
8442 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8443 COMMON_INTERCEPT_FUNCTION(fsetpos)
8448 #if SANITIZER_INTERCEPT_MD2
8449 INTERCEPTOR(void, MD2Init, void *context) {
8451 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8452 REAL(MD2Init)(context);
8454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8457 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8460 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8461 if (data && len > 0)
8462 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8464 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8465 REAL(MD2Update)(context, data, len);
8467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8470 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8472 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8474 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8475 REAL(MD2Final)(digest, context);
8477 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8480 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8482 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8484 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8485 char *ret = REAL(MD2End)(context, buf);
8487 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8491 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8493 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8495 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8496 char *ret = REAL(MD2File)(filename, buf);
8498 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8502 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8505 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8506 if (data && len > 0)
8507 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8508 char *ret = REAL(MD2Data)(data, len, buf);
8510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8515 COMMON_INTERCEPT_FUNCTION(MD2Init); \
8516 COMMON_INTERCEPT_FUNCTION(MD2Update); \
8517 COMMON_INTERCEPT_FUNCTION(MD2Final); \
8518 COMMON_INTERCEPT_FUNCTION(MD2End); \
8519 COMMON_INTERCEPT_FUNCTION(MD2File); \
8520 COMMON_INTERCEPT_FUNCTION(MD2Data)
8525 #if SANITIZER_INTERCEPT_SHA2
8526 #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
8527 INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
8529 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
8530 REAL(SHA##LEN##_Init)(context); \
8532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8534 INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
8535 const u8 *data, SIZE_T len) { \
8537 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
8538 if (data && len > 0) \
8539 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8541 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8542 REAL(SHA##LEN##_Update)(context, data, len); \
8544 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8546 INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
8549 CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
8550 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
8552 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8553 REAL(SHA##LEN##_Final)(digest, context); \
8555 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
8556 sizeof(digest[0]) * \
8557 SHA##LEN##_digest_length); \
8559 INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
8561 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
8563 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8564 char *ret = REAL(SHA##LEN##_End)(context, buf); \
8566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8569 INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
8571 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
8573 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8574 char *ret = REAL(SHA##LEN##_File)(filename, buf); \
8576 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8579 INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
8580 OFF_T offset, OFF_T length) { \
8582 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
8585 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8586 char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
8588 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8591 INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
8593 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
8594 if (data && len > 0) \
8595 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8596 char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
8598 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8602 SHA2_INTERCEPTORS(224, u32);
8603 SHA2_INTERCEPTORS(256, u32);
8604 SHA2_INTERCEPTORS(384, u64);
8605 SHA2_INTERCEPTORS(512, u64);
8607 #define INIT_SHA2_INTECEPTORS(LEN) \
8608 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
8609 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
8610 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
8611 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
8612 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
8613 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
8614 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
8617 INIT_SHA2_INTECEPTORS(224); \
8618 INIT_SHA2_INTECEPTORS(256); \
8619 INIT_SHA2_INTECEPTORS(384); \
8620 INIT_SHA2_INTECEPTORS(512)
8621 #undef SHA2_INTERCEPTORS
8626 #if SANITIZER_INTERCEPT_VIS
8627 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
8629 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
8630 char *end = REAL(vis)(dst, c, flag, nextc);
8631 // dst is NULL terminated and end points to the NULL char
8633 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8636 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
8638 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
8639 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
8640 // nvis cannot make sure the dst is NULL terminated
8642 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8645 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
8647 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
8649 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8650 int len = REAL(strvis)(dst, src, flag);
8652 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8655 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
8657 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
8659 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8660 int len = REAL(stravis)(dst, src, flag);
8662 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
8664 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
8668 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
8670 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
8672 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8673 int len = REAL(strnvis)(dst, dlen, src, flag);
8674 // The interface will be valid even if there is no space for NULL char
8676 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8679 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
8681 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
8683 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8684 int ret = REAL(strvisx)(dst, src, len, flag);
8686 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8689 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8692 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
8694 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8695 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
8696 if (dst && ret >= 0)
8697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8700 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8701 int flag, int *cerr_ptr) {
8703 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
8705 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8706 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8707 // according to the implementation
8709 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8710 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
8711 if (dst && ret >= 0)
8712 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8714 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8717 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
8718 const char *extra) {
8720 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
8722 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8723 char *end = REAL(svis)(dst, c, flag, nextc, extra);
8725 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8728 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
8729 const char *extra) {
8731 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
8733 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8734 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
8736 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
8737 Min((SIZE_T)(end - dst + 1), dlen));
8740 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
8741 const char *extra) {
8743 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
8745 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8747 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8748 int len = REAL(strsvis)(dst, src, flag, extra);
8750 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8753 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
8754 const char *extra) {
8756 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
8758 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8760 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8761 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
8762 // The interface will be valid even if there is no space for NULL char
8763 if (dst && len >= 0)
8764 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8767 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
8768 const char *extra) {
8770 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
8772 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8774 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8775 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
8777 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8780 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8781 int flag, const char *extra) {
8783 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
8785 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8787 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8788 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
8789 if (dst && ret >= 0)
8790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8793 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
8794 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
8796 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
8799 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8801 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8802 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8803 // according to the implementation
8805 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8806 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
8807 if (dst && ret >= 0)
8808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8810 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8813 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
8815 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
8817 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
8818 int ret = REAL(unvis)(cp, c, astate, flag);
8819 if (ret == unvis_valid || ret == unvis_validpush) {
8820 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
8824 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
8826 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
8828 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8829 int ret = REAL(strunvis)(dst, src);
8831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8834 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
8836 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
8838 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8839 int ret = REAL(strnunvis)(dst, dlen, src);
8841 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8844 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
8846 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
8848 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8849 int ret = REAL(strunvisx)(dst, src, flag);
8851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8854 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
8857 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
8859 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8860 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
8862 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8866 COMMON_INTERCEPT_FUNCTION(vis); \
8867 COMMON_INTERCEPT_FUNCTION(nvis); \
8868 COMMON_INTERCEPT_FUNCTION(strvis); \
8869 COMMON_INTERCEPT_FUNCTION(stravis); \
8870 COMMON_INTERCEPT_FUNCTION(strnvis); \
8871 COMMON_INTERCEPT_FUNCTION(strvisx); \
8872 COMMON_INTERCEPT_FUNCTION(strnvisx); \
8873 COMMON_INTERCEPT_FUNCTION(strenvisx); \
8874 COMMON_INTERCEPT_FUNCTION(svis); \
8875 COMMON_INTERCEPT_FUNCTION(snvis); \
8876 COMMON_INTERCEPT_FUNCTION(strsvis); \
8877 COMMON_INTERCEPT_FUNCTION(strsnvis); \
8878 COMMON_INTERCEPT_FUNCTION(strsvisx); \
8879 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
8880 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
8881 COMMON_INTERCEPT_FUNCTION(unvis); \
8882 COMMON_INTERCEPT_FUNCTION(strunvis); \
8883 COMMON_INTERCEPT_FUNCTION(strnunvis); \
8884 COMMON_INTERCEPT_FUNCTION(strunvisx); \
8885 COMMON_INTERCEPT_FUNCTION(strnunvisx)
8890 #if SANITIZER_INTERCEPT_CDB
8891 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
8893 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
8895 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
8896 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
8898 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8902 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
8903 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
8905 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
8908 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
8909 struct __sanitizer_cdbr *cdbr =
8910 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
8912 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8916 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
8918 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
8920 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8921 return REAL(cdbr_entries)(cdbr);
8924 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
8925 const void **data, SIZE_T *datalen) {
8927 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
8929 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8930 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
8933 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
8935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
8936 if (data && datalen)
8937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
8942 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
8943 SIZE_T keylen, const void **data, SIZE_T *datalen) {
8945 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
8947 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8949 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
8950 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
8953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
8955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
8956 if (data && datalen)
8957 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
8962 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
8964 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
8966 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
8967 REAL(cdbr_close)(cdbr);
8970 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
8972 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
8973 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
8975 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
8979 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
8980 SIZE_T keylen, const void *data, SIZE_T datalen) {
8982 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
8984 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
8985 if (data && datalen)
8986 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
8988 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
8989 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
8991 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
8995 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
8996 SIZE_T datalen, u32 *index) {
8998 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9000 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9001 if (data && datalen)
9002 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9003 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9006 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9008 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9013 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9014 SIZE_T keylen, u32 index) {
9016 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9018 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9020 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9021 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9023 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9027 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9028 const char descr[16], u32 (*seedgen)(void)) {
9030 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9031 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9033 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9035 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9037 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9038 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9041 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9043 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9048 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9050 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9052 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9053 REAL(cdbw_close)(cdbw);
9057 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9058 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9059 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9060 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9061 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9062 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9063 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9064 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9065 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9066 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9067 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9068 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9073 #if SANITIZER_INTERCEPT_GETFSENT
9074 INTERCEPTOR(void *, getfsent) {
9076 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9077 void *ret = REAL(getfsent)();
9079 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9083 INTERCEPTOR(void *, getfsspec, const char *spec) {
9085 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9087 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, REAL(strlen)(spec) + 1);
9088 void *ret = REAL(getfsspec)(spec);
9090 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9094 INTERCEPTOR(void *, getfsfile, const char *file) {
9096 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9098 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
9099 void *ret = REAL(getfsfile)(file);
9101 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9105 #define INIT_GETFSENT \
9106 COMMON_INTERCEPT_FUNCTION(getfsent); \
9107 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9108 COMMON_INTERCEPT_FUNCTION(getfsfile);
9110 #define INIT_GETFSENT
9113 #if SANITIZER_INTERCEPT_ARC4RANDOM
9114 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9116 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9117 REAL(arc4random_buf)(buf, len);
9119 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9122 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9124 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9126 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9127 REAL(arc4random_addrandom)(dat, datlen);
9130 #define INIT_ARC4RANDOM \
9131 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9132 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9134 #define INIT_ARC4RANDOM
9137 #if SANITIZER_INTERCEPT_POPEN
9138 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9140 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9142 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, REAL(strlen)(command) + 1);
9144 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9145 __sanitizer_FILE *res = REAL(popen)(command, type);
9146 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9147 if (res) unpoison_file(res);
9150 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9155 #if SANITIZER_INTERCEPT_POPENVE
9156 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9157 char *const *argv, char *const *envp, const char *type) {
9159 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9161 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
9163 for (char *const *pa = argv; ; ++pa) {
9164 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9167 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9171 for (char *const *pa = envp; ; ++pa) {
9172 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9175 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9179 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9180 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9181 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9182 if (res) unpoison_file(res);
9185 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9187 #define INIT_POPENVE
9190 #if SANITIZER_INTERCEPT_PCLOSE
9191 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9193 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9194 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9195 const FileMetadata *m = GetInterceptorMetadata(fp);
9196 int res = REAL(pclose)(fp);
9198 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9199 DeleteInterceptorMetadata(fp);
9203 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9208 #if SANITIZER_INTERCEPT_FUNOPEN
9209 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9210 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9211 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9212 typedef int (*funopen_closefn)(void *cookie);
9214 struct WrappedFunopenCookie {
9216 funopen_readfn real_read;
9217 funopen_writefn real_write;
9218 funopen_seekfn real_seek;
9219 funopen_closefn real_close;
9222 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9223 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9224 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9225 funopen_readfn real_read = wrapped_cookie->real_read;
9226 return real_read(wrapped_cookie->real_cookie, buf, len);
9229 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9230 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9231 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9232 funopen_writefn real_write = wrapped_cookie->real_write;
9233 return real_write(wrapped_cookie->real_cookie, buf, len);
9236 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9237 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9238 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9239 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9240 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9243 static int wrapped_funopen_close(void *cookie) {
9244 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9245 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9246 funopen_closefn real_close = wrapped_cookie->real_close;
9247 int res = real_close(wrapped_cookie->real_cookie);
9248 InternalFree(wrapped_cookie);
9252 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9253 funopen_writefn writefn, funopen_seekfn seekfn,
9254 funopen_closefn closefn) {
9256 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9259 WrappedFunopenCookie *wrapped_cookie =
9260 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9261 wrapped_cookie->real_cookie = cookie;
9262 wrapped_cookie->real_read = readfn;
9263 wrapped_cookie->real_write = writefn;
9264 wrapped_cookie->real_seek = seekfn;
9265 wrapped_cookie->real_close = closefn;
9267 __sanitizer_FILE *res =
9268 REAL(funopen)(wrapped_cookie,
9269 readfn ? wrapped_funopen_read : nullptr,
9270 writefn ? wrapped_funopen_write : nullptr,
9271 seekfn ? wrapped_funopen_seek : nullptr,
9272 closefn ? wrapped_funopen_close : nullptr);
9277 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9279 #define INIT_FUNOPEN
9282 #if SANITIZER_INTERCEPT_FUNOPEN2
9283 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9284 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9285 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9286 typedef int (*funopen2_flushfn)(void *cookie);
9287 typedef int (*funopen2_closefn)(void *cookie);
9289 struct WrappedFunopen2Cookie {
9291 funopen2_readfn real_read;
9292 funopen2_writefn real_write;
9293 funopen2_seekfn real_seek;
9294 funopen2_flushfn real_flush;
9295 funopen2_closefn real_close;
9298 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9299 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9300 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9301 funopen2_readfn real_read = wrapped_cookie->real_read;
9302 return real_read(wrapped_cookie->real_cookie, buf, len);
9305 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9307 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9308 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9309 funopen2_writefn real_write = wrapped_cookie->real_write;
9310 return real_write(wrapped_cookie->real_cookie, buf, len);
9313 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9314 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9315 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9316 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9317 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9320 static int wrapped_funopen2_flush(void *cookie) {
9321 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9322 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9323 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9324 return real_flush(wrapped_cookie->real_cookie);
9327 static int wrapped_funopen2_close(void *cookie) {
9328 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9329 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9330 funopen2_closefn real_close = wrapped_cookie->real_close;
9331 int res = real_close(wrapped_cookie->real_cookie);
9332 InternalFree(wrapped_cookie);
9336 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9337 funopen2_writefn writefn, funopen2_seekfn seekfn,
9338 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9340 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9343 WrappedFunopen2Cookie *wrapped_cookie =
9344 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9345 wrapped_cookie->real_cookie = cookie;
9346 wrapped_cookie->real_read = readfn;
9347 wrapped_cookie->real_write = writefn;
9348 wrapped_cookie->real_seek = seekfn;
9349 wrapped_cookie->real_flush = flushfn;
9350 wrapped_cookie->real_close = closefn;
9352 __sanitizer_FILE *res =
9353 REAL(funopen2)(wrapped_cookie,
9354 readfn ? wrapped_funopen2_read : nullptr,
9355 writefn ? wrapped_funopen2_write : nullptr,
9356 seekfn ? wrapped_funopen2_seek : nullptr,
9357 flushfn ? wrapped_funopen2_flush : nullptr,
9358 closefn ? wrapped_funopen2_close : nullptr);
9363 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9365 #define INIT_FUNOPEN2
9368 #if SANITIZER_INTERCEPT_FDEVNAME
9369 INTERCEPTOR(char *, fdevname, int fd) {
9371 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9372 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9373 char *name = REAL(fdevname)(fd);
9375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
9377 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9382 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9384 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9385 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9386 char *name = REAL(fdevname_r)(fd, buf, len);
9387 if (name && buf && len > 0) {
9388 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
9390 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9395 #define INIT_FDEVNAME \
9396 COMMON_INTERCEPT_FUNCTION(fdevname); \
9397 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9399 #define INIT_FDEVNAME
9402 #if SANITIZER_INTERCEPT_GETUSERSHELL
9403 INTERCEPTOR(char *, getusershell) {
9405 COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9406 char *res = REAL(getusershell)();
9408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9412 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9414 #define INIT_GETUSERSHELL
9417 #if SANITIZER_INTERCEPT_SL_INIT
9418 INTERCEPTOR(void *, sl_init) {
9420 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9421 void *res = REAL(sl_init)();
9423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9427 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9429 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9431 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9433 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9434 int res = REAL(sl_add)(sl, item);
9436 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9440 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9442 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9444 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9446 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9447 char *res = REAL(sl_find)(sl, item);
9449 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9453 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9455 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9457 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9458 REAL(sl_free)(sl, freeall);
9461 #define INIT_SL_INIT \
9462 COMMON_INTERCEPT_FUNCTION(sl_init); \
9463 COMMON_INTERCEPT_FUNCTION(sl_add); \
9464 COMMON_INTERCEPT_FUNCTION(sl_find); \
9465 COMMON_INTERCEPT_FUNCTION(sl_free);
9467 #define INIT_SL_INIT
9470 static void InitializeCommonInterceptors() {
9471 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
9472 interceptor_metadata_map =
9473 new ((void *)&metadata_mem) MetadataHashMap(); // NOLINT
9521 INIT_LOCALTIME_AND_FRIENDS;
9530 INIT_GETPWNAM_AND_FRIENDS;
9531 INIT_GETPWNAM_R_AND_FRIENDS;
9546 INIT_PTHREAD_GETSCHEDPARAM;
9551 INIT_GETHOSTBYNAME2;
9552 INIT_GETHOSTBYNAME_R;
9553 INIT_GETHOSTBYNAME2_R;
9554 INIT_GETHOSTBYADDR_R;
9574 INIT_GET_CURRENT_DIR_NAME;
9583 INIT_CANONICALIZE_FILE_NAME;
9585 INIT_SCHED_GETAFFINITY;
9586 INIT_SCHED_GETPARAM;
9589 INIT_XPG_STRERROR_R;
9604 INIT_PTHREAD_MUTEX_LOCK;
9605 INIT_PTHREAD_MUTEX_UNLOCK;
9606 INIT___PTHREAD_MUTEX_LOCK;
9607 INIT___PTHREAD_MUTEX_UNLOCK;
9608 INIT___LIBC_MUTEX_LOCK;
9609 INIT___LIBC_MUTEX_UNLOCK;
9610 INIT___LIBC_THR_SETCANCELSTATE;
9618 INIT_ETHER_NTOA_ATON;
9623 INIT_PTHREAD_ATTR_GET;
9624 INIT_PTHREAD_ATTR_GET_SCHED;
9625 INIT_PTHREAD_ATTR_GETINHERITSCHED;
9626 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
9627 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
9628 INIT_PTHREAD_MUTEXATTR_GETTYPE;
9629 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
9630 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
9631 INIT_PTHREAD_MUTEXATTR_GETROBUST;
9632 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
9633 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
9634 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
9635 INIT_PTHREAD_CONDATTR_GETPSHARED;
9636 INIT_PTHREAD_CONDATTR_GETCLOCK;
9637 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
9642 INIT_PTHREAD_SETNAME_NP;
9643 INIT_PTHREAD_GETNAME_NP;
9661 INIT_IF_INDEXTONAME;
9668 INIT_LIBIO_INTERNALS;
9671 INIT_OPEN_MEMSTREAM;
9675 INIT_DLOPEN_DLCLOSE;
9681 INIT_PTHREAD_SETCANCEL;
9683 INIT_PROCESS_VM_READV;
9689 INIT_EVENTFD_READ_WRITE;
9695 // FIXME: add other *stat interceptors.
9706 INIT_GROUP_FROM_GID;
9707 INIT_GID_FROM_GROUP;
9711 INIT_GETGROUPMEMBERSHIP;
9714 INIT_NAME_TO_HANDLE_AT;
9715 INIT_OPEN_BY_HANDLE_AT;
9725 INIT_MI_VECTOR_HASH;
9733 INIT_SYSCTLGETMIBINFO;