1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Common function interceptors for tools like AddressSanitizer,
10 // ThreadSanitizer, MemorySanitizer, etc.
12 // This file should be included into the tool's interceptor file,
13 // which has to define its own macros:
14 // COMMON_INTERCEPTOR_ENTER
15 // COMMON_INTERCEPTOR_ENTER_NOIGNORE
16 // COMMON_INTERCEPTOR_READ_RANGE
17 // COMMON_INTERCEPTOR_WRITE_RANGE
18 // COMMON_INTERCEPTOR_INITIALIZE_RANGE
19 // COMMON_INTERCEPTOR_DIR_ACQUIRE
20 // COMMON_INTERCEPTOR_FD_ACQUIRE
21 // COMMON_INTERCEPTOR_FD_RELEASE
22 // COMMON_INTERCEPTOR_FD_ACCESS
23 // COMMON_INTERCEPTOR_SET_THREAD_NAME
24 // COMMON_INTERCEPTOR_ON_DLOPEN
25 // COMMON_INTERCEPTOR_ON_EXIT
26 // COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
27 // COMMON_INTERCEPTOR_MUTEX_POST_LOCK
28 // COMMON_INTERCEPTOR_MUTEX_UNLOCK
29 // COMMON_INTERCEPTOR_MUTEX_REPAIR
30 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
31 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
32 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
33 // COMMON_INTERCEPTOR_MEMSET_IMPL
34 // COMMON_INTERCEPTOR_MEMMOVE_IMPL
35 // COMMON_INTERCEPTOR_MEMCPY_IMPL
36 // COMMON_INTERCEPTOR_MMAP_IMPL
37 // COMMON_INTERCEPTOR_COPY_STRING
38 // COMMON_INTERCEPTOR_STRNDUP_IMPL
39 //===----------------------------------------------------------------------===//
41 #include "interception/interception.h"
42 #include "sanitizer_addrhashmap.h"
43 #include "sanitizer_errno.h"
44 #include "sanitizer_placement_new.h"
45 #include "sanitizer_platform_interceptors.h"
46 #include "sanitizer_symbolizer.h"
47 #include "sanitizer_tls_get_addr.h"
51 #if SANITIZER_INTERCEPTOR_HOOKS
52 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
53 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
54 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
56 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
57 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
59 #endif // SANITIZER_INTERCEPTOR_HOOKS
61 #if SANITIZER_WINDOWS && !defined(va_copy)
62 #define va_copy(dst, src) ((dst) = (src))
66 #define pthread_setname_np pthread_set_name_np
67 #define inet_aton __inet_aton
68 #define inet_pton __inet_pton
69 #define iconv __bsd_iconv
73 #define clock_getres __clock_getres50
74 #define clock_gettime __clock_gettime50
75 #define clock_settime __clock_settime50
76 #define ctime __ctime50
77 #define ctime_r __ctime_r50
78 #define devname __devname50
79 #define fgetpos __fgetpos50
80 #define fsetpos __fsetpos50
81 #define fts_children __fts_children60
82 #define fts_close __fts_close60
83 #define fts_open __fts_open60
84 #define fts_read __fts_read60
85 #define fts_set __fts_set60
86 #define getitimer __getitimer50
87 #define getmntinfo __getmntinfo13
88 #define getpwent __getpwent50
89 #define getpwnam __getpwnam50
90 #define getpwnam_r __getpwnam_r50
91 #define getpwuid __getpwuid50
92 #define getpwuid_r __getpwuid_r50
93 #define getutent __getutent50
94 #define getutxent __getutxent50
95 #define getutxid __getutxid50
96 #define getutxline __getutxline50
97 #define pututxline __pututxline50
99 #define gmtime __gmtime50
100 #define gmtime_r __gmtime_r50
101 #define localtime __locatime50
102 #define localtime_r __localtime_r50
103 #define mktime __mktime50
104 #define lstat __lstat50
105 #define opendir __opendir30
106 #define readdir __readdir30
107 #define readdir_r __readdir_r30
108 #define scandir __scandir30
109 #define setitimer __setitimer50
110 #define setlocale __setlocale50
111 #define shmctl __shmctl50
112 #define sigemptyset __sigemptyset14
113 #define sigfillset __sigfillset14
114 #define sigpending __sigpending14
115 #define sigprocmask __sigprocmask14
116 #define sigtimedwait __sigtimedwait50
117 #define stat __stat50
118 #define time __time50
119 #define times __times13
120 #define unvis __unvis50
121 #define wait3 __wait350
122 #define wait4 __wait450
123 extern const unsigned short *_ctype_tab_;
124 extern const short *_toupper_tab_;
125 extern const short *_tolower_tab_;
128 // Platform-specific options.
130 namespace __sanitizer {
131 bool PlatformHasDifferentMemcpyAndMemmove();
133 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
134 (__sanitizer::PlatformHasDifferentMemcpyAndMemmove())
135 #elif SANITIZER_WINDOWS64
136 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
138 #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
139 #endif // SANITIZER_MAC
141 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
142 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
145 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
146 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
149 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
150 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
153 #ifndef COMMON_INTERCEPTOR_MUTEX_PRE_LOCK
154 #define COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m) {}
157 #ifndef COMMON_INTERCEPTOR_MUTEX_POST_LOCK
158 #define COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m) {}
161 #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
162 #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
165 #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
166 #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
169 #ifndef COMMON_INTERCEPTOR_MUTEX_INVALID
170 #define COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m) {}
173 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
174 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
177 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
178 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
181 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
182 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
185 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
186 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
189 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
190 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
193 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
194 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
195 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
198 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
199 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
202 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
203 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
204 common_flags()->strict_string_checks ? (REAL(strlen)(s)) + 1 : (n) )
206 #ifndef COMMON_INTERCEPTOR_ON_DLOPEN
207 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
208 CheckNoDeepBind(filename, flag);
211 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
212 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
215 #ifndef COMMON_INTERCEPTOR_ACQUIRE
216 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
219 #ifndef COMMON_INTERCEPTOR_RELEASE
220 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
223 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
224 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
227 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
228 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
231 #ifdef SANITIZER_NLDBL_VERSION
232 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
233 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
235 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
236 COMMON_INTERCEPT_FUNCTION(fn)
239 #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
240 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
242 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
243 return internal_memset(dst, v, size); \
244 COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
245 if (common_flags()->intercept_intrin) \
246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
247 return REAL(memset)(dst, v, size); \
251 #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
252 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
254 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
255 return internal_memmove(dst, src, size); \
256 COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
257 if (common_flags()->intercept_intrin) { \
258 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
259 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
261 return REAL(memmove)(dst, src, size); \
265 #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
266 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
268 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
269 return internal_memmove(dst, src, size); \
271 COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
272 if (common_flags()->intercept_intrin) { \
273 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
274 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
276 return REAL(memcpy)(dst, src, size); \
280 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
281 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
283 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
286 #ifndef COMMON_INTERCEPTOR_COPY_STRING
287 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
290 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
291 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
292 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
293 uptr copy_length = internal_strnlen(s, size); \
294 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
295 if (common_flags()->intercept_strndup) { \
296 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
298 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
299 internal_memcpy(new_mem, s, copy_length); \
300 new_mem[copy_length] = '\0'; \
304 struct FileMetadata {
305 // For open_memstream().
310 struct CommonInterceptorMetadata {
320 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
322 static MetadataHashMap *interceptor_metadata_map;
325 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
326 const FileMetadata &file) {
327 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
329 h->type = CommonInterceptorMetadata::CIMT_FILE;
333 UNUSED static const FileMetadata *GetInterceptorMetadata(
334 __sanitizer_FILE *addr) {
335 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
338 if (addr && h.exists()) {
340 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
347 UNUSED static void DeleteInterceptorMetadata(void *addr) {
348 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
353 #if SANITIZER_INTERCEPT_STRLEN
354 INTERCEPTOR(SIZE_T, strlen, const char *s) {
355 // Sometimes strlen is called prior to InitializeCommonInterceptors,
356 // in which case the REAL(strlen) typically used in
357 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
359 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
360 return internal_strlen(s);
362 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
363 SIZE_T result = REAL(strlen)(s);
364 if (common_flags()->intercept_strlen)
365 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
368 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
373 #if SANITIZER_INTERCEPT_STRNLEN
374 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
376 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
377 SIZE_T length = REAL(strnlen)(s, maxlen);
378 if (common_flags()->intercept_strlen)
379 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
382 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
387 #if SANITIZER_INTERCEPT_STRNDUP
388 INTERCEPTOR(char*, strndup, const char *s, uptr size) {
390 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
392 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
395 #endif // SANITIZER_INTERCEPT_STRNDUP
397 #if SANITIZER_INTERCEPT___STRNDUP
398 INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
400 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
402 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
404 #define INIT___STRNDUP
405 #endif // SANITIZER_INTERCEPT___STRNDUP
407 #if SANITIZER_INTERCEPT_TEXTDOMAIN
408 INTERCEPTOR(char*, textdomain, const char *domainname) {
410 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
411 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
412 char *domain = REAL(textdomain)(domainname);
414 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
418 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
420 #define INIT_TEXTDOMAIN
423 #if SANITIZER_INTERCEPT_STRCMP
424 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
425 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
428 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
429 const char *s1, const char *s2, int result)
431 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
433 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
434 unsigned char c1, c2;
437 c1 = (unsigned char)s1[i];
438 c2 = (unsigned char)s2[i];
439 if (c1 != c2 || c1 == '\0') break;
441 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
442 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
443 int result = CharCmpX(c1, c2);
444 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
449 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
450 const char *s1, const char *s2, uptr n,
453 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
454 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
455 return internal_strncmp(s1, s2, size);
457 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
458 unsigned char c1 = 0, c2 = 0;
460 for (i = 0; i < size; i++) {
461 c1 = (unsigned char)s1[i];
462 c2 = (unsigned char)s2[i];
463 if (c1 != c2 || c1 == '\0') break;
467 if (common_flags()->strict_string_checks) {
468 for (; i1 < size && s1[i1]; i1++) {}
469 for (; i2 < size && s2[i2]; i2++) {}
471 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
472 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
473 int result = CharCmpX(c1, c2);
474 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
479 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
480 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
486 #if SANITIZER_INTERCEPT_STRCASECMP
487 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
488 int c1_low = ToLower(c1);
489 int c2_low = ToLower(c2);
490 return c1_low - c2_low;
493 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
494 const char *s1, const char *s2, int result)
496 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
498 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
499 unsigned char c1 = 0, c2 = 0;
502 c1 = (unsigned char)s1[i];
503 c2 = (unsigned char)s2[i];
504 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
506 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
507 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
508 int result = CharCaseCmp(c1, c2);
509 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
514 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
515 const char *s1, const char *s2, uptr size,
518 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
520 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
521 unsigned char c1 = 0, c2 = 0;
523 for (i = 0; i < size; i++) {
524 c1 = (unsigned char)s1[i];
525 c2 = (unsigned char)s2[i];
526 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
530 if (common_flags()->strict_string_checks) {
531 for (; i1 < size && s1[i1]; i1++) {}
532 for (; i2 < size && s2[i2]; i2++) {}
534 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
535 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
536 int result = CharCaseCmp(c1, c2);
537 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
538 s1, s2, size, result);
542 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
543 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
545 #define INIT_STRCASECMP
546 #define INIT_STRNCASECMP
549 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
550 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
552 uptr len1 = REAL(strlen)(s1);
553 uptr len2 = REAL(strlen)(s2);
554 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
555 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
559 #if SANITIZER_INTERCEPT_STRSTR
561 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
562 const char *s1, const char *s2, char *result)
564 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
565 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
566 return internal_strstr(s1, s2);
568 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
569 char *r = REAL(strstr)(s1, s2);
570 if (common_flags()->intercept_strstr)
571 StrstrCheck(ctx, r, s1, s2);
572 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
577 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
582 #if SANITIZER_INTERCEPT_STRCASESTR
584 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
585 const char *s1, const char *s2, char *result)
587 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
589 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
590 char *r = REAL(strcasestr)(s1, s2);
591 if (common_flags()->intercept_strstr)
592 StrstrCheck(ctx, r, s1, s2);
593 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
598 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
600 #define INIT_STRCASESTR
603 #if SANITIZER_INTERCEPT_STRTOK
605 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
607 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
608 if (!common_flags()->intercept_strtok) {
609 return REAL(strtok)(str, delimiters);
611 if (common_flags()->strict_string_checks) {
612 // If strict_string_checks is enabled, we check the whole first argument
613 // string on the first call (strtok saves this string in a static buffer
614 // for subsequent calls). We do not need to check strtok's result.
615 // As the delimiters can change, we check them every call.
616 if (str != nullptr) {
617 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
619 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
620 REAL(strlen)(delimiters) + 1);
621 return REAL(strtok)(str, delimiters);
623 // However, when strict_string_checks is disabled we cannot check the
624 // whole string on the first call. Instead, we check the result string
625 // which is guaranteed to be a NULL-terminated substring of the first
626 // argument. We also conservatively check one character of str and the
628 if (str != nullptr) {
629 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
631 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
632 char *result = REAL(strtok)(str, delimiters);
633 if (result != nullptr) {
634 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, REAL(strlen)(result) + 1);
635 } else if (str != nullptr) {
636 // No delimiter were found, it's safe to assume that the entire str was
638 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
644 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
649 #if SANITIZER_INTERCEPT_MEMMEM
650 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
651 const void *s1, SIZE_T len1, const void *s2,
652 SIZE_T len2, void *result)
654 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
657 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
658 void *r = REAL(memmem)(s1, len1, s2, len2);
659 if (common_flags()->intercept_memmem) {
660 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
661 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
663 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
664 s1, len1, s2, len2, r);
668 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
671 #endif // SANITIZER_INTERCEPT_MEMMEM
673 #if SANITIZER_INTERCEPT_STRCHR
674 INTERCEPTOR(char*, strchr, const char *s, int c) {
676 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
677 return internal_strchr(s, c);
678 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
679 char *result = REAL(strchr)(s, c);
680 if (common_flags()->intercept_strchr) {
681 // Keep strlen as macro argument, as macro may ignore it.
682 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
683 (result ? result - s : REAL(strlen)(s)) + 1);
687 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
692 #if SANITIZER_INTERCEPT_STRCHRNUL
693 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
695 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
696 char *result = REAL(strchrnul)(s, c);
697 uptr len = result - s + 1;
698 if (common_flags()->intercept_strchr)
699 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
702 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
704 #define INIT_STRCHRNUL
707 #if SANITIZER_INTERCEPT_STRRCHR
708 INTERCEPTOR(char*, strrchr, const char *s, int c) {
710 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
711 return internal_strrchr(s, c);
712 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
713 if (common_flags()->intercept_strchr)
714 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
715 return REAL(strrchr)(s, c);
717 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
722 #if SANITIZER_INTERCEPT_STRSPN
723 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
725 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
726 SIZE_T r = REAL(strspn)(s1, s2);
727 if (common_flags()->intercept_strspn) {
728 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
729 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
734 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
736 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
737 SIZE_T r = REAL(strcspn)(s1, s2);
738 if (common_flags()->intercept_strspn) {
739 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
740 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
745 #define INIT_STRSPN \
746 COMMON_INTERCEPT_FUNCTION(strspn); \
747 COMMON_INTERCEPT_FUNCTION(strcspn);
752 #if SANITIZER_INTERCEPT_STRPBRK
753 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
755 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
756 char *r = REAL(strpbrk)(s1, s2);
757 if (common_flags()->intercept_strpbrk) {
758 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, REAL(strlen)(s2) + 1);
759 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
760 r ? r - s1 + 1 : REAL(strlen)(s1) + 1);
765 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
770 #if SANITIZER_INTERCEPT_MEMSET
771 INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
773 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
776 #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
781 #if SANITIZER_INTERCEPT_MEMMOVE
782 INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
784 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
787 #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
792 #if SANITIZER_INTERCEPT_MEMCPY
793 INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
794 // On OS X, calling internal_memcpy here will cause memory corruptions,
795 // because memcpy and memmove are actually aliases of the same
796 // implementation. We need to use internal_memmove here.
797 // N.B.: If we switch this to internal_ we'll have to use internal_memmove
798 // due to memcpy being an alias of memmove on OS X.
800 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) {
801 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
803 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
807 #define INIT_MEMCPY \
809 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
810 COMMON_INTERCEPT_FUNCTION(memcpy); \
812 ASSIGN_REAL(memcpy, memmove); \
814 CHECK(REAL(memcpy)); \
821 #if SANITIZER_INTERCEPT_MEMCMP
822 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
823 const void *s1, const void *s2, uptr n,
826 // Common code for `memcmp` and `bcmp`.
827 int MemcmpInterceptorCommon(void *ctx,
828 int (*real_fn)(const void *, const void *, uptr),
829 const void *a1, const void *a2, uptr size) {
830 if (common_flags()->intercept_memcmp) {
831 if (common_flags()->strict_memcmp) {
832 // Check the entire regions even if the first bytes of the buffers are
834 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
835 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
836 // Fallthrough to REAL(memcmp) below.
838 unsigned char c1 = 0, c2 = 0;
839 const unsigned char *s1 = (const unsigned char*)a1;
840 const unsigned char *s2 = (const unsigned char*)a2;
842 for (i = 0; i < size; i++) {
847 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
848 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
849 int r = CharCmpX(c1, c2);
850 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
855 int result = real_fn(a1, a2, size);
856 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
861 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
862 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
863 return internal_memcmp(a1, a2, size);
865 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
866 return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
869 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
874 #if SANITIZER_INTERCEPT_BCMP
875 INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {
876 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
877 return internal_memcmp(a1, a2, size);
879 COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
880 return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
883 #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
888 #if SANITIZER_INTERCEPT_MEMCHR
889 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
890 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
891 return internal_memchr(s, c, n);
893 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
894 #if SANITIZER_WINDOWS
897 res = REAL(memchr)(s, c, n);
899 res = internal_memchr(s, c, n);
902 void *res = REAL(memchr)(s, c, n);
904 uptr len = res ? (char *)res - (const char *)s + 1 : n;
905 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
909 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
914 #if SANITIZER_INTERCEPT_MEMRCHR
915 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
917 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
918 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
919 return REAL(memrchr)(s, c, n);
922 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
927 #if SANITIZER_INTERCEPT_FREXP
928 INTERCEPTOR(double, frexp, double x, int *exp) {
930 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
931 // Assuming frexp() always writes to |exp|.
932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
933 double res = REAL(frexp)(x, exp);
937 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
940 #endif // SANITIZER_INTERCEPT_FREXP
942 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
943 INTERCEPTOR(float, frexpf, float x, int *exp) {
945 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
946 // FIXME: under ASan the call below may write to freed memory and corrupt
948 // https://github.com/google/sanitizers/issues/321.
949 float res = REAL(frexpf)(x, exp);
950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
954 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
956 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
957 // FIXME: under ASan the call below may write to freed memory and corrupt
959 // https://github.com/google/sanitizers/issues/321.
960 long double res = REAL(frexpl)(x, exp);
961 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
965 #define INIT_FREXPF_FREXPL \
966 COMMON_INTERCEPT_FUNCTION(frexpf); \
967 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
969 #define INIT_FREXPF_FREXPL
970 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
973 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
974 SIZE_T iovlen, SIZE_T maxlen) {
975 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
976 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
977 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
982 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
983 SIZE_T iovlen, SIZE_T maxlen) {
984 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
985 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
986 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
987 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
993 #if SANITIZER_INTERCEPT_READ
994 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
996 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
997 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
998 // FIXME: under ASan the call below may write to freed memory and corrupt
1000 // https://github.com/google/sanitizers/issues/321.
1001 SSIZE_T res = REAL(read)(fd, ptr, count);
1002 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1003 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1006 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
1011 #if SANITIZER_INTERCEPT_FREAD
1012 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
1013 // libc file streams can call user-supplied functions, see fopencookie.
1015 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
1016 // FIXME: under ASan the call below may write to freed memory and corrupt
1017 // its metadata. See
1018 // https://github.com/google/sanitizers/issues/321.
1019 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
1020 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
1023 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1028 #if SANITIZER_INTERCEPT_PREAD
1029 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1031 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1032 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1033 // FIXME: under ASan the call below may write to freed memory and corrupt
1034 // its metadata. See
1035 // https://github.com/google/sanitizers/issues/321.
1036 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
1037 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1038 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1041 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1046 #if SANITIZER_INTERCEPT_PREAD64
1047 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1049 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1050 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1051 // FIXME: under ASan the call below may write to freed memory and corrupt
1052 // its metadata. See
1053 // https://github.com/google/sanitizers/issues/321.
1054 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
1055 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1056 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1059 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1061 #define INIT_PREAD64
1064 #if SANITIZER_INTERCEPT_READV
1065 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1068 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1069 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1070 SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
1071 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1072 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1075 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1080 #if SANITIZER_INTERCEPT_PREADV
1081 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1084 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1085 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1086 SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
1087 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1088 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1091 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1096 #if SANITIZER_INTERCEPT_PREADV64
1097 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1100 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1101 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1102 SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
1103 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1104 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1107 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1109 #define INIT_PREADV64
1112 #if SANITIZER_INTERCEPT_WRITE
1113 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1115 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1116 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1117 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1118 SSIZE_T res = REAL(write)(fd, ptr, count);
1119 // FIXME: this check should be _before_ the call to REAL(write), not after
1120 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1123 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1128 #if SANITIZER_INTERCEPT_FWRITE
1129 INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
1130 // libc file streams can call user-supplied functions, see fopencookie.
1132 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1133 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1134 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1137 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1142 #if SANITIZER_INTERCEPT_PWRITE
1143 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1145 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1146 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1147 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1148 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
1149 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1152 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1157 #if SANITIZER_INTERCEPT_PWRITE64
1158 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1161 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1162 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1163 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1164 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
1165 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1168 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1170 #define INIT_PWRITE64
1173 #if SANITIZER_INTERCEPT_WRITEV
1174 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1177 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1178 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1179 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1180 SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
1181 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1184 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1189 #if SANITIZER_INTERCEPT_PWRITEV
1190 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1193 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1194 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1195 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1196 SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
1197 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1200 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1202 #define INIT_PWRITEV
1205 #if SANITIZER_INTERCEPT_PWRITEV64
1206 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1209 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1210 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1211 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1212 SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
1213 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1216 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1218 #define INIT_PWRITEV64
1221 #if SANITIZER_INTERCEPT_FGETS
1222 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1223 // libc file streams can call user-supplied functions, see fopencookie.
1225 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1226 // FIXME: under ASan the call below may write to freed memory and corrupt
1227 // its metadata. See
1228 // https://github.com/google/sanitizers/issues/321.
1229 char *res = REAL(fgets)(s, size, file);
1231 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
1234 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1239 #if SANITIZER_INTERCEPT_FPUTS
1240 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1241 // libc file streams can call user-supplied functions, see fopencookie.
1243 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1244 if (!SANITIZER_MAC || s)
1245 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1246 return REAL(fputs)(s, file);
1248 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1253 #if SANITIZER_INTERCEPT_PUTS
1254 INTERCEPTOR(int, puts, char *s) {
1255 // libc file streams can call user-supplied functions, see fopencookie.
1257 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1258 if (!SANITIZER_MAC || s)
1259 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1260 return REAL(puts)(s);
1262 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1267 #if SANITIZER_INTERCEPT_PRCTL
1268 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1269 unsigned long arg3, // NOLINT
1270 unsigned long arg4, unsigned long arg5) { // NOLINT
1272 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1273 static const int PR_SET_NAME = 15;
1274 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1275 if (option == PR_SET_NAME) {
1277 internal_strncpy(buff, (char *)arg2, 15);
1279 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1283 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1286 #endif // SANITIZER_INTERCEPT_PRCTL
1288 #if SANITIZER_INTERCEPT_TIME
1289 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1291 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1292 unsigned long local_t;
1293 unsigned long res = REAL(time)(&local_t);
1294 if (t && res != (unsigned long)-1) {
1295 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1300 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1303 #endif // SANITIZER_INTERCEPT_TIME
1305 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1306 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1307 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1308 #if !SANITIZER_SOLARIS
1310 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1311 // can point to shared memory and tsan would report a data race.
1312 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1313 REAL(strlen(tm->tm_zone)) + 1);
1317 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1319 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1320 __sanitizer_tm *res = REAL(localtime)(timep);
1322 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1323 unpoison_tm(ctx, res);
1327 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1329 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1330 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1332 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1333 unpoison_tm(ctx, res);
1337 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1339 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1340 __sanitizer_tm *res = REAL(gmtime)(timep);
1342 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1343 unpoison_tm(ctx, res);
1347 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1349 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1350 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1352 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1353 unpoison_tm(ctx, res);
1357 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1359 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1360 // FIXME: under ASan the call below may write to freed memory and corrupt
1361 // its metadata. See
1362 // https://github.com/google/sanitizers/issues/321.
1363 char *res = REAL(ctime)(timep);
1365 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1366 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1370 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1372 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1373 // FIXME: under ASan the call below may write to freed memory and corrupt
1374 // its metadata. See
1375 // https://github.com/google/sanitizers/issues/321.
1376 char *res = REAL(ctime_r)(timep, result);
1378 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1379 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1383 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1385 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1386 // FIXME: under ASan the call below may write to freed memory and corrupt
1387 // its metadata. See
1388 // https://github.com/google/sanitizers/issues/321.
1389 char *res = REAL(asctime)(tm);
1391 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1396 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1398 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1399 // FIXME: under ASan the call below may write to freed memory and corrupt
1400 // its metadata. See
1401 // https://github.com/google/sanitizers/issues/321.
1402 char *res = REAL(asctime_r)(tm, result);
1404 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1405 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1409 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1411 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1412 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1413 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1414 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1415 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1416 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1417 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1418 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1419 long res = REAL(mktime)(tm);
1420 if (res != -1) unpoison_tm(ctx, tm);
1423 #define INIT_LOCALTIME_AND_FRIENDS \
1424 COMMON_INTERCEPT_FUNCTION(localtime); \
1425 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1426 COMMON_INTERCEPT_FUNCTION(gmtime); \
1427 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1428 COMMON_INTERCEPT_FUNCTION(ctime); \
1429 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1430 COMMON_INTERCEPT_FUNCTION(asctime); \
1431 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1432 COMMON_INTERCEPT_FUNCTION(mktime);
1434 #define INIT_LOCALTIME_AND_FRIENDS
1435 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1437 #if SANITIZER_INTERCEPT_STRPTIME
1438 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1440 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1442 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1443 // FIXME: under ASan the call below may write to freed memory and corrupt
1444 // its metadata. See
1445 // https://github.com/google/sanitizers/issues/321.
1446 char *res = REAL(strptime)(s, format, tm);
1447 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1449 // Do not call unpoison_tm here, because strptime does not, in fact,
1450 // initialize the entire struct tm. For example, tm_zone pointer is left
1452 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1456 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1458 #define INIT_STRPTIME
1461 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1462 #include "sanitizer_common_interceptors_format.inc"
1464 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1468 va_start(ap, format); \
1469 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1470 int res = WRAP(vname)(__VA_ARGS__, ap); \
1477 #if SANITIZER_INTERCEPT_SCANF
1479 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1482 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1485 int res = REAL(vname)(__VA_ARGS__); \
1487 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1492 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1493 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1495 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1496 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1498 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1499 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1501 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1502 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1503 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1505 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1507 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1509 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1510 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1511 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1513 INTERCEPTOR(int, scanf, const char *format, ...)
1514 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1516 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1517 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1519 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1520 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1522 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1523 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1524 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1526 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1527 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1529 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1530 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1535 #if SANITIZER_INTERCEPT_SCANF
1536 #define INIT_SCANF \
1537 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1538 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1539 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1540 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1541 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1542 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1547 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1548 #define INIT_ISOC99_SCANF \
1549 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1550 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1551 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1552 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1553 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1554 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1556 #define INIT_ISOC99_SCANF
1559 #if SANITIZER_INTERCEPT_PRINTF
1561 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1563 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1567 #define VPRINTF_INTERCEPTOR_RETURN() \
1570 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1572 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1573 if (common_flags()->check_printf) \
1574 printf_common(ctx, format, aq); \
1575 int res = REAL(vname)(__VA_ARGS__); \
1576 VPRINTF_INTERCEPTOR_RETURN(); \
1580 // FIXME: under ASan the REAL() call below may write to freed memory and
1581 // corrupt its metadata. See
1582 // https://github.com/google/sanitizers/issues/321.
1583 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1585 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1586 if (common_flags()->check_printf) { \
1587 printf_common(ctx, format, aq); \
1589 int res = REAL(vname)(str, __VA_ARGS__); \
1591 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1593 VPRINTF_INTERCEPTOR_RETURN(); \
1597 // FIXME: under ASan the REAL() call below may write to freed memory and
1598 // corrupt its metadata. See
1599 // https://github.com/google/sanitizers/issues/321.
1600 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1602 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1603 if (common_flags()->check_printf) { \
1604 printf_common(ctx, format, aq); \
1606 int res = REAL(vname)(str, size, __VA_ARGS__); \
1608 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1610 VPRINTF_INTERCEPTOR_RETURN(); \
1614 // FIXME: under ASan the REAL() call below may write to freed memory and
1615 // corrupt its metadata. See
1616 // https://github.com/google/sanitizers/issues/321.
1617 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1619 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1620 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1621 if (common_flags()->check_printf) { \
1622 printf_common(ctx, format, aq); \
1624 int res = REAL(vname)(strp, __VA_ARGS__); \
1626 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1628 VPRINTF_INTERCEPTOR_RETURN(); \
1632 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1633 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1635 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1637 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1639 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1641 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1643 #if SANITIZER_INTERCEPT___PRINTF_CHK
1644 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1645 SIZE_T size_to, const char *format, va_list ap)
1646 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1649 #if SANITIZER_INTERCEPT_PRINTF_L
1650 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1651 const char *format, va_list ap)
1652 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1654 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1655 const char *format, ...)
1656 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1657 #endif // SANITIZER_INTERCEPT_PRINTF_L
1659 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1660 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1662 #if SANITIZER_INTERCEPT___PRINTF_CHK
1663 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1664 const char *format, va_list ap)
1665 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1668 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1669 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1671 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1672 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1673 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1675 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1676 const char *format, va_list ap)
1677 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1679 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1681 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1683 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1685 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1688 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1690 INTERCEPTOR(int, printf, const char *format, ...)
1691 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1693 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1694 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1696 #if SANITIZER_INTERCEPT___PRINTF_CHK
1697 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1698 const char *format, ...)
1699 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1702 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1703 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1705 #if SANITIZER_INTERCEPT___PRINTF_CHK
1706 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1707 const char *format, ...) // NOLINT
1708 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format) // NOLINT
1711 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1712 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1714 #if SANITIZER_INTERCEPT___PRINTF_CHK
1715 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1716 SIZE_T size_to, const char *format, ...) // NOLINT
1717 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format) // NOLINT
1720 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1721 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1723 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1724 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1725 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1727 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1729 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1731 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1732 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1734 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1735 const char *format, ...)
1736 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1739 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1741 #endif // SANITIZER_INTERCEPT_PRINTF
1743 #if SANITIZER_INTERCEPT_PRINTF
1744 #define INIT_PRINTF \
1745 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1746 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1747 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1748 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1749 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1750 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1751 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1752 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1753 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1754 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1759 #if SANITIZER_INTERCEPT___PRINTF_CHK
1760 #define INIT___PRINTF_CHK \
1761 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1762 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1763 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1764 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1765 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1767 #define INIT___PRINTF_CHK
1770 #if SANITIZER_INTERCEPT_PRINTF_L
1771 #define INIT_PRINTF_L \
1772 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1773 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1775 #define INIT_PRINTF_L
1778 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1779 #define INIT_ISOC99_PRINTF \
1780 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1781 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1782 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1783 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1784 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1785 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1786 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1787 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1789 #define INIT_ISOC99_PRINTF
1792 #if SANITIZER_INTERCEPT_IOCTL
1793 #include "sanitizer_common_interceptors_ioctl.inc"
1794 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1795 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1796 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1797 // can trigger a report and we need to be able to unwind through this
1798 // function. On Mac in debug mode we might not have a frame pointer, because
1799 // ioctl_common_[pre|post] doesn't get inlined here.
1800 ENABLE_FRAME_POINTER;
1804 va_start(ap, request);
1805 void *arg = va_arg(ap, void *);
1807 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1809 CHECK(ioctl_initialized);
1811 // Note: TSan does not use common flags, and they are zero-initialized.
1812 // This effectively disables ioctl handling in TSan.
1813 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1815 // Although request is unsigned long, the rest of the interceptor uses it
1816 // as just "unsigned" to save space, because we know that all values fit in
1817 // "unsigned" - they are compile-time constants.
1819 const ioctl_desc *desc = ioctl_lookup(request);
1820 ioctl_desc decoded_desc;
1822 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1823 if (!ioctl_decode(request, &decoded_desc))
1824 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1826 desc = &decoded_desc;
1829 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1830 int res = REAL(ioctl)(d, request, arg);
1831 // FIXME: some ioctls have different return values for success and failure.
1832 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1835 #define INIT_IOCTL \
1837 COMMON_INTERCEPT_FUNCTION(ioctl);
1843 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1848 REAL(strlen)(pwd->pw_name) + 1);
1850 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1851 REAL(strlen)(pwd->pw_passwd) + 1);
1852 #if !SANITIZER_ANDROID
1854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1855 REAL(strlen)(pwd->pw_gecos) + 1);
1857 #if SANITIZER_MAC || SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
1859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1860 REAL(strlen)(pwd->pw_class) + 1);
1863 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1864 REAL(strlen)(pwd->pw_dir) + 1);
1866 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1867 REAL(strlen)(pwd->pw_shell) + 1);
1871 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1875 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1876 REAL(strlen)(grp->gr_name) + 1);
1878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1879 REAL(strlen)(grp->gr_passwd) + 1);
1880 char **p = grp->gr_mem;
1882 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1885 (p - grp->gr_mem + 1) * sizeof(*p));
1888 #endif // SANITIZER_POSIX
1890 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1891 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1893 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1895 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1896 __sanitizer_passwd *res = REAL(getpwnam)(name);
1897 unpoison_passwd(ctx, res);
1900 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1902 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1903 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1904 unpoison_passwd(ctx, res);
1907 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1909 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1910 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1911 __sanitizer_group *res = REAL(getgrnam)(name);
1912 unpoison_group(ctx, res);
1915 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1917 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1918 __sanitizer_group *res = REAL(getgrgid)(gid);
1919 unpoison_group(ctx, res);
1922 #define INIT_GETPWNAM_AND_FRIENDS \
1923 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1924 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1925 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1926 COMMON_INTERCEPT_FUNCTION(getgrgid);
1928 #define INIT_GETPWNAM_AND_FRIENDS
1931 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1932 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1933 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1935 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1936 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1937 // FIXME: under ASan the call below may write to freed memory and corrupt
1938 // its metadata. See
1939 // https://github.com/google/sanitizers/issues/321.
1940 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1942 unpoison_passwd(ctx, *result);
1943 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1946 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1947 SIZE_T buflen, __sanitizer_passwd **result) {
1949 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1950 // FIXME: under ASan the call below may write to freed memory and corrupt
1951 // its metadata. See
1952 // https://github.com/google/sanitizers/issues/321.
1953 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1955 unpoison_passwd(ctx, *result);
1956 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1959 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1960 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1962 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1963 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1964 // FIXME: under ASan the call below may write to freed memory and corrupt
1965 // its metadata. See
1966 // https://github.com/google/sanitizers/issues/321.
1967 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1969 unpoison_group(ctx, *result);
1970 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1973 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1974 SIZE_T buflen, __sanitizer_group **result) {
1976 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1977 // FIXME: under ASan the call below may write to freed memory and corrupt
1978 // its metadata. See
1979 // https://github.com/google/sanitizers/issues/321.
1980 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1982 unpoison_group(ctx, *result);
1983 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1986 #define INIT_GETPWNAM_R_AND_FRIENDS \
1987 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1988 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1989 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1990 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1992 #define INIT_GETPWNAM_R_AND_FRIENDS
1995 #if SANITIZER_INTERCEPT_GETPWENT
1996 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
1998 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
1999 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2000 unpoison_passwd(ctx, res);
2003 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2005 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2006 __sanitizer_group *res = REAL(getgrent)(dummy);
2007 unpoison_group(ctx, res);
2010 #define INIT_GETPWENT \
2011 COMMON_INTERCEPT_FUNCTION(getpwent); \
2012 COMMON_INTERCEPT_FUNCTION(getgrent);
2014 #define INIT_GETPWENT
2017 #if SANITIZER_INTERCEPT_FGETPWENT
2018 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2020 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2021 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2022 unpoison_passwd(ctx, res);
2025 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2027 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2028 __sanitizer_group *res = REAL(fgetgrent)(fp);
2029 unpoison_group(ctx, res);
2032 #define INIT_FGETPWENT \
2033 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2034 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2036 #define INIT_FGETPWENT
2039 #if SANITIZER_INTERCEPT_GETPWENT_R
2040 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2041 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2043 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2044 // FIXME: under ASan the call below may write to freed memory and corrupt
2045 // its metadata. See
2046 // https://github.com/google/sanitizers/issues/321.
2047 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2049 unpoison_passwd(ctx, *pwbufp);
2050 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2053 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2054 __sanitizer_group **pwbufp) {
2056 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2057 // FIXME: under ASan the call below may write to freed memory and corrupt
2058 // its metadata. See
2059 // https://github.com/google/sanitizers/issues/321.
2060 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2062 unpoison_group(ctx, *pwbufp);
2063 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2066 #define INIT_GETPWENT_R \
2067 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2068 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2070 #define INIT_GETPWENT_R
2073 #if SANITIZER_INTERCEPT_FGETPWENT_R
2074 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2075 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2077 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2078 // FIXME: under ASan the call below may write to freed memory and corrupt
2079 // its metadata. See
2080 // https://github.com/google/sanitizers/issues/321.
2081 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2083 unpoison_passwd(ctx, *pwbufp);
2084 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2087 #define INIT_FGETPWENT_R \
2088 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2090 #define INIT_FGETPWENT_R
2093 #if SANITIZER_INTERCEPT_FGETGRENT_R
2094 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2095 SIZE_T buflen, __sanitizer_group **pwbufp) {
2097 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2098 // FIXME: under ASan the call below may write to freed memory and corrupt
2099 // its metadata. See
2100 // https://github.com/google/sanitizers/issues/321.
2101 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2103 unpoison_group(ctx, *pwbufp);
2104 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2107 #define INIT_FGETGRENT_R \
2108 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2110 #define INIT_FGETGRENT_R
2113 #if SANITIZER_INTERCEPT_SETPWENT
2114 // The only thing these interceptors do is disable any nested interceptors.
2115 // These functions may open nss modules and call uninstrumented functions from
2116 // them, and we don't want things like strlen() to trigger.
2117 INTERCEPTOR(void, setpwent, int dummy) {
2119 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2120 REAL(setpwent)(dummy);
2122 INTERCEPTOR(void, endpwent, int dummy) {
2124 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2125 REAL(endpwent)(dummy);
2127 INTERCEPTOR(void, setgrent, int dummy) {
2129 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2130 REAL(setgrent)(dummy);
2132 INTERCEPTOR(void, endgrent, int dummy) {
2134 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2135 REAL(endgrent)(dummy);
2137 #define INIT_SETPWENT \
2138 COMMON_INTERCEPT_FUNCTION(setpwent); \
2139 COMMON_INTERCEPT_FUNCTION(endpwent); \
2140 COMMON_INTERCEPT_FUNCTION(setgrent); \
2141 COMMON_INTERCEPT_FUNCTION(endgrent);
2143 #define INIT_SETPWENT
2146 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2147 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2149 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2150 // FIXME: under ASan the call below may write to freed memory and corrupt
2151 // its metadata. See
2152 // https://github.com/google/sanitizers/issues/321.
2153 int res = REAL(clock_getres)(clk_id, tp);
2155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2159 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2161 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2162 // FIXME: under ASan the call below may write to freed memory and corrupt
2163 // its metadata. See
2164 // https://github.com/google/sanitizers/issues/321.
2165 int res = REAL(clock_gettime)(clk_id, tp);
2167 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2171 namespace __sanitizer {
2173 int real_clock_gettime(u32 clk_id, void *tp) {
2174 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2175 return internal_clock_gettime(clk_id, tp);
2176 return REAL(clock_gettime)(clk_id, tp);
2179 } // namespace __sanitizer
2180 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2182 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2183 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2184 return REAL(clock_settime)(clk_id, tp);
2186 #define INIT_CLOCK_GETTIME \
2187 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2188 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2189 COMMON_INTERCEPT_FUNCTION(clock_settime);
2191 #define INIT_CLOCK_GETTIME
2194 #if SANITIZER_INTERCEPT_GETITIMER
2195 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2197 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2198 // FIXME: under ASan the call below may write to freed memory and corrupt
2199 // its metadata. See
2200 // https://github.com/google/sanitizers/issues/321.
2201 int res = REAL(getitimer)(which, curr_value);
2202 if (!res && curr_value) {
2203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2207 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2209 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2211 // itimerval can contain padding that may be legitimately uninitialized
2212 const struct __sanitizer_itimerval *nv =
2213 (const struct __sanitizer_itimerval *)new_value;
2214 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2215 sizeof(__sanitizer_time_t));
2216 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2217 sizeof(__sanitizer_suseconds_t));
2218 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2219 sizeof(__sanitizer_time_t));
2220 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2221 sizeof(__sanitizer_suseconds_t));
2223 // FIXME: under ASan the call below may write to freed memory and corrupt
2224 // its metadata. See
2225 // https://github.com/google/sanitizers/issues/321.
2226 int res = REAL(setitimer)(which, new_value, old_value);
2227 if (!res && old_value) {
2228 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2232 #define INIT_GETITIMER \
2233 COMMON_INTERCEPT_FUNCTION(getitimer); \
2234 COMMON_INTERCEPT_FUNCTION(setitimer);
2236 #define INIT_GETITIMER
2239 #if SANITIZER_INTERCEPT_GLOB
2240 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2241 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2242 // +1 for NULL pointer at the end.
2243 if (pglob->gl_pathv)
2244 COMMON_INTERCEPTOR_WRITE_RANGE(
2245 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2246 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2247 char *p = pglob->gl_pathv[i];
2248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2252 #if SANITIZER_SOLARIS
2253 INTERCEPTOR(int, glob, const char *pattern, int flags,
2254 int (*errfunc)(const char *epath, int eerrno),
2255 __sanitizer_glob_t *pglob) {
2257 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2258 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2259 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2260 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2264 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2266 static void wrapped_gl_closedir(void *dir) {
2267 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2268 pglob_copy->gl_closedir(dir);
2271 static void *wrapped_gl_readdir(void *dir) {
2272 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2273 return pglob_copy->gl_readdir(dir);
2276 static void *wrapped_gl_opendir(const char *s) {
2277 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2278 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2279 return pglob_copy->gl_opendir(s);
2282 static int wrapped_gl_lstat(const char *s, void *st) {
2283 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2284 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2285 return pglob_copy->gl_lstat(s, st);
2288 static int wrapped_gl_stat(const char *s, void *st) {
2289 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2290 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2291 return pglob_copy->gl_stat(s, st);
2294 static const __sanitizer_glob_t kGlobCopy = {
2296 0, wrapped_gl_closedir, wrapped_gl_readdir,
2297 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2299 INTERCEPTOR(int, glob, const char *pattern, int flags,
2300 int (*errfunc)(const char *epath, int eerrno),
2301 __sanitizer_glob_t *pglob) {
2303 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2304 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2305 __sanitizer_glob_t glob_copy;
2306 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2307 if (flags & glob_altdirfunc) {
2308 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2309 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2310 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2311 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2312 Swap(pglob->gl_stat, glob_copy.gl_stat);
2313 pglob_copy = &glob_copy;
2315 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2316 if (flags & glob_altdirfunc) {
2317 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2318 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2319 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2320 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2321 Swap(pglob->gl_stat, glob_copy.gl_stat);
2324 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2327 #endif // SANITIZER_SOLARIS
2329 COMMON_INTERCEPT_FUNCTION(glob);
2330 #else // SANITIZER_INTERCEPT_GLOB
2332 #endif // SANITIZER_INTERCEPT_GLOB
2334 #if SANITIZER_INTERCEPT_GLOB64
2335 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2336 int (*errfunc)(const char *epath, int eerrno),
2337 __sanitizer_glob_t *pglob) {
2339 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2340 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2341 __sanitizer_glob_t glob_copy;
2342 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2343 if (flags & glob_altdirfunc) {
2344 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2345 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2346 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2347 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2348 Swap(pglob->gl_stat, glob_copy.gl_stat);
2349 pglob_copy = &glob_copy;
2351 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2352 if (flags & glob_altdirfunc) {
2353 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2354 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2355 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2356 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2357 Swap(pglob->gl_stat, glob_copy.gl_stat);
2360 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2363 #define INIT_GLOB64 \
2364 COMMON_INTERCEPT_FUNCTION(glob64);
2365 #else // SANITIZER_INTERCEPT_GLOB64
2367 #endif // SANITIZER_INTERCEPT_GLOB64
2369 #if SANITIZER_INTERCEPT_WAIT
2370 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2371 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2373 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2375 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2376 // FIXME: under ASan the call below may write to freed memory and corrupt
2377 // its metadata. See
2378 // https://github.com/google/sanitizers/issues/321.
2379 int res = REAL(wait)(status);
2380 if (res != -1 && status)
2381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2384 // On FreeBSD id_t is always 64-bit wide.
2385 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2386 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2389 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2393 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2394 // FIXME: under ASan the call below may write to freed memory and corrupt
2395 // its metadata. See
2396 // https://github.com/google/sanitizers/issues/321.
2397 int res = REAL(waitid)(idtype, id, infop, options);
2398 if (res != -1 && infop)
2399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2402 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2404 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2405 // FIXME: under ASan the call below may write to freed memory and corrupt
2406 // its metadata. See
2407 // https://github.com/google/sanitizers/issues/321.
2408 int res = REAL(waitpid)(pid, status, options);
2409 if (res != -1 && status)
2410 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2413 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2415 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2416 // FIXME: under ASan the call below may write to freed memory and corrupt
2417 // its metadata. See
2418 // https://github.com/google/sanitizers/issues/321.
2419 int res = REAL(wait3)(status, options, rusage);
2421 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2422 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2426 #if SANITIZER_ANDROID
2427 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2429 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2430 // FIXME: under ASan the call below may write to freed memory and corrupt
2431 // its metadata. See
2432 // https://github.com/google/sanitizers/issues/321.
2433 int res = REAL(__wait4)(pid, status, options, rusage);
2435 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2436 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2440 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2442 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2444 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2445 // FIXME: under ASan the call below may write to freed memory and corrupt
2446 // its metadata. See
2447 // https://github.com/google/sanitizers/issues/321.
2448 int res = REAL(wait4)(pid, status, options, rusage);
2450 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2451 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2455 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2456 #endif // SANITIZER_ANDROID
2458 COMMON_INTERCEPT_FUNCTION(wait); \
2459 COMMON_INTERCEPT_FUNCTION(waitid); \
2460 COMMON_INTERCEPT_FUNCTION(waitpid); \
2461 COMMON_INTERCEPT_FUNCTION(wait3);
2467 #if SANITIZER_INTERCEPT_INET
2468 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2470 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2471 uptr sz = __sanitizer_in_addr_sz(af);
2472 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2473 // FIXME: figure out read size based on the address family.
2474 // FIXME: under ASan the call below may write to freed memory and corrupt
2475 // its metadata. See
2476 // https://github.com/google/sanitizers/issues/321.
2477 char *res = REAL(inet_ntop)(af, src, dst, size);
2478 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2481 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2483 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2484 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2485 // FIXME: figure out read size based on the address family.
2486 // FIXME: under ASan the call below may write to freed memory and corrupt
2487 // its metadata. See
2488 // https://github.com/google/sanitizers/issues/321.
2489 int res = REAL(inet_pton)(af, src, dst);
2491 uptr sz = __sanitizer_in_addr_sz(af);
2492 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2497 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2498 COMMON_INTERCEPT_FUNCTION(inet_pton);
2503 #if SANITIZER_INTERCEPT_INET
2504 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2506 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2507 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2508 // FIXME: under ASan the call below may write to freed memory and corrupt
2509 // its metadata. See
2510 // https://github.com/google/sanitizers/issues/321.
2511 int res = REAL(inet_aton)(cp, dst);
2513 uptr sz = __sanitizer_in_addr_sz(af_inet);
2514 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2518 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2520 #define INIT_INET_ATON
2523 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2524 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2526 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2527 // FIXME: under ASan the call below may write to freed memory and corrupt
2528 // its metadata. See
2529 // https://github.com/google/sanitizers/issues/321.
2530 int res = REAL(pthread_getschedparam)(thread, policy, param);
2532 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2533 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2537 #define INIT_PTHREAD_GETSCHEDPARAM \
2538 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2540 #define INIT_PTHREAD_GETSCHEDPARAM
2543 #if SANITIZER_INTERCEPT_GETADDRINFO
2544 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2545 struct __sanitizer_addrinfo *hints,
2546 struct __sanitizer_addrinfo **out) {
2548 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2549 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2551 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2553 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2554 // FIXME: under ASan the call below may write to freed memory and corrupt
2555 // its metadata. See
2556 // https://github.com/google/sanitizers/issues/321.
2557 int res = REAL(getaddrinfo)(node, service, hints, out);
2558 if (res == 0 && out) {
2559 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2560 struct __sanitizer_addrinfo *p = *out;
2562 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2564 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2565 if (p->ai_canonname)
2566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2567 REAL(strlen)(p->ai_canonname) + 1);
2573 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2575 #define INIT_GETADDRINFO
2578 #if SANITIZER_INTERCEPT_GETNAMEINFO
2579 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2580 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2582 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2583 serv, servlen, flags);
2584 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2585 // There is padding in in_addr that may make this too noisy
2586 // FIXME: under ASan the call below may write to freed memory and corrupt
2587 // its metadata. See
2588 // https://github.com/google/sanitizers/issues/321.
2590 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2592 if (host && hostlen)
2593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2594 if (serv && servlen)
2595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2599 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2601 #define INIT_GETNAMEINFO
2604 #if SANITIZER_INTERCEPT_GETSOCKNAME
2605 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2607 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2608 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2609 int addrlen_in = *addrlen;
2610 // FIXME: under ASan the call below may write to freed memory and corrupt
2611 // its metadata. See
2612 // https://github.com/google/sanitizers/issues/321.
2613 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2615 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2619 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2621 #define INIT_GETSOCKNAME
2624 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2625 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2626 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2629 char **p = h->h_aliases;
2631 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2634 COMMON_INTERCEPTOR_WRITE_RANGE(
2635 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2641 COMMON_INTERCEPTOR_WRITE_RANGE(
2642 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2646 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2647 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2649 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2650 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2651 if (res) write_hostent(ctx, res);
2655 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2658 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2659 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2660 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2661 if (res) write_hostent(ctx, res);
2665 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2667 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2668 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2669 if (res) write_hostent(ctx, res);
2672 #define INIT_GETHOSTBYNAME \
2673 COMMON_INTERCEPT_FUNCTION(gethostent); \
2674 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2675 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2677 #define INIT_GETHOSTBYNAME
2678 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2680 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2681 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2683 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2684 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2685 if (res) write_hostent(ctx, res);
2688 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2690 #define INIT_GETHOSTBYNAME2
2691 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2693 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2694 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2695 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2698 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2700 // FIXME: under ASan the call below may write to freed memory and corrupt
2701 // its metadata. See
2702 // https://github.com/google/sanitizers/issues/321.
2703 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2705 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2706 if (res == 0 && *result) write_hostent(ctx, *result);
2709 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2712 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2714 #define INIT_GETHOSTBYNAME_R
2717 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2718 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2719 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2721 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2723 // FIXME: under ASan the call below may write to freed memory and corrupt
2724 // its metadata. See
2725 // https://github.com/google/sanitizers/issues/321.
2726 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2728 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2729 if (res == 0 && *result) write_hostent(ctx, *result);
2732 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2735 #define INIT_GETHOSTENT_R \
2736 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2738 #define INIT_GETHOSTENT_R
2741 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2742 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2743 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2744 __sanitizer_hostent **result, int *h_errnop) {
2746 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2747 buflen, result, h_errnop);
2748 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2749 // FIXME: under ASan the call below may write to freed memory and corrupt
2750 // its metadata. See
2751 // https://github.com/google/sanitizers/issues/321.
2752 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2755 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2756 if (res == 0 && *result) write_hostent(ctx, *result);
2759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2762 #define INIT_GETHOSTBYADDR_R \
2763 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2765 #define INIT_GETHOSTBYADDR_R
2768 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2769 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2770 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2771 __sanitizer_hostent **result, int *h_errnop) {
2773 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2775 // FIXME: under ASan the call below may write to freed memory and corrupt
2776 // its metadata. See
2777 // https://github.com/google/sanitizers/issues/321.
2779 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2781 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2782 if (res == 0 && *result) write_hostent(ctx, *result);
2785 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2788 #define INIT_GETHOSTBYNAME2_R \
2789 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2791 #define INIT_GETHOSTBYNAME2_R
2794 #if SANITIZER_INTERCEPT_GETSOCKOPT
2795 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2798 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2800 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2801 // FIXME: under ASan the call below may write to freed memory and corrupt
2802 // its metadata. See
2803 // https://github.com/google/sanitizers/issues/321.
2804 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2806 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2809 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2811 #define INIT_GETSOCKOPT
2814 #if SANITIZER_INTERCEPT_ACCEPT
2815 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2817 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2818 unsigned addrlen0 = 0;
2820 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2821 addrlen0 = *addrlen;
2823 int fd2 = REAL(accept)(fd, addr, addrlen);
2825 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2826 if (addr && addrlen)
2827 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2831 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2836 #if SANITIZER_INTERCEPT_ACCEPT4
2837 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2839 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2840 unsigned addrlen0 = 0;
2842 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2843 addrlen0 = *addrlen;
2845 // FIXME: under ASan the call below may write to freed memory and corrupt
2846 // its metadata. See
2847 // https://github.com/google/sanitizers/issues/321.
2848 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2850 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2851 if (addr && addrlen)
2852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2856 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2858 #define INIT_ACCEPT4
2861 #if SANITIZER_INTERCEPT_PACCEPT
2862 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2863 __sanitizer_sigset_t *set, int f) {
2865 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2866 unsigned addrlen0 = 0;
2868 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2869 addrlen0 = *addrlen;
2871 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2872 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
2874 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2875 if (addr && addrlen)
2876 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2880 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2882 #define INIT_PACCEPT
2885 #if SANITIZER_INTERCEPT_MODF
2886 INTERCEPTOR(double, modf, double x, double *iptr) {
2888 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2889 // FIXME: under ASan the call below may write to freed memory and corrupt
2890 // its metadata. See
2891 // https://github.com/google/sanitizers/issues/321.
2892 double res = REAL(modf)(x, iptr);
2894 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2898 INTERCEPTOR(float, modff, float x, float *iptr) {
2900 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2901 // FIXME: under ASan the call below may write to freed memory and corrupt
2902 // its metadata. See
2903 // https://github.com/google/sanitizers/issues/321.
2904 float res = REAL(modff)(x, iptr);
2906 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2910 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2912 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2913 // FIXME: under ASan the call below may write to freed memory and corrupt
2914 // its metadata. See
2915 // https://github.com/google/sanitizers/issues/321.
2916 long double res = REAL(modfl)(x, iptr);
2918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2923 COMMON_INTERCEPT_FUNCTION(modf); \
2924 COMMON_INTERCEPT_FUNCTION(modff); \
2925 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2930 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2931 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2933 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2934 if (msg->msg_name && msg->msg_namelen)
2935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2936 if (msg->msg_iov && msg->msg_iovlen)
2937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2938 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2939 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2940 if (msg->msg_control && msg->msg_controllen)
2941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2945 #if SANITIZER_INTERCEPT_RECVMSG
2946 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2949 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2950 // FIXME: under ASan the call below may write to freed memory and corrupt
2951 // its metadata. See
2952 // https://github.com/google/sanitizers/issues/321.
2953 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2955 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2957 write_msghdr(ctx, msg, res);
2958 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2963 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2965 #define INIT_RECVMSG
2968 #if SANITIZER_INTERCEPT_RECVMMSG
2969 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2970 unsigned int vlen, int flags, void *timeout) {
2972 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2973 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2974 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2976 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2977 for (int i = 0; i < res; ++i) {
2978 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2979 sizeof(msgvec[i].msg_len));
2980 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2981 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2986 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2988 #define INIT_RECVMMSG
2991 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2992 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2993 const unsigned kCmsgDataOffset =
2994 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2996 char *p = (char *)control;
2997 char *const control_end = p + controllen;
2999 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3000 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3001 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3003 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3005 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3006 sizeof(cmsg->cmsg_level));
3007 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3008 sizeof(cmsg->cmsg_type));
3010 if (cmsg->cmsg_len > kCmsgDataOffset) {
3011 char *data = p + kCmsgDataOffset;
3012 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3013 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3016 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3020 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3023 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3032 if (msg->msg_name && msg->msg_namelen)
3033 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3034 if (msg->msg_iov && msg->msg_iovlen)
3035 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3036 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3037 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3038 if (msg->msg_control && msg->msg_controllen)
3039 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3043 #if SANITIZER_INTERCEPT_SENDMSG
3044 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3047 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3049 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3050 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3052 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3053 if (common_flags()->intercept_send && res >= 0 && msg)
3054 read_msghdr(ctx, msg, res);
3057 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3059 #define INIT_SENDMSG
3062 #if SANITIZER_INTERCEPT_SENDMMSG
3063 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3064 unsigned vlen, int flags) {
3066 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3068 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3069 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3071 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3072 if (res >= 0 && msgvec)
3073 for (int i = 0; i < res; ++i) {
3074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3075 sizeof(msgvec[i].msg_len));
3076 if (common_flags()->intercept_send)
3077 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3081 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3083 #define INIT_SENDMMSG
3086 #if SANITIZER_INTERCEPT_GETPEERNAME
3087 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3089 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3091 if (addrlen) addr_sz = *addrlen;
3092 // FIXME: under ASan the call below may write to freed memory and corrupt
3093 // its metadata. See
3094 // https://github.com/google/sanitizers/issues/321.
3095 int res = REAL(getpeername)(sockfd, addr, addrlen);
3096 if (!res && addr && addrlen)
3097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3100 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3102 #define INIT_GETPEERNAME
3105 #if SANITIZER_INTERCEPT_SYSINFO
3106 INTERCEPTOR(int, sysinfo, void *info) {
3108 // FIXME: under ASan the call below may write to freed memory and corrupt
3109 // its metadata. See
3110 // https://github.com/google/sanitizers/issues/321.
3111 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3112 int res = REAL(sysinfo)(info);
3114 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3117 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3119 #define INIT_SYSINFO
3122 #if SANITIZER_INTERCEPT_READDIR
3123 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3125 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3126 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3127 __sanitizer_dirent *res = REAL(opendir)(path);
3129 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3133 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3135 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3136 // FIXME: under ASan the call below may write to freed memory and corrupt
3137 // its metadata. See
3138 // https://github.com/google/sanitizers/issues/321.
3139 __sanitizer_dirent *res = REAL(readdir)(dirp);
3140 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3144 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3145 __sanitizer_dirent **result) {
3147 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3148 // FIXME: under ASan the call below may write to freed memory and corrupt
3149 // its metadata. See
3150 // https://github.com/google/sanitizers/issues/321.
3151 int res = REAL(readdir_r)(dirp, entry, result);
3153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3160 #define INIT_READDIR \
3161 COMMON_INTERCEPT_FUNCTION(opendir); \
3162 COMMON_INTERCEPT_FUNCTION(readdir); \
3163 COMMON_INTERCEPT_FUNCTION(readdir_r);
3165 #define INIT_READDIR
3168 #if SANITIZER_INTERCEPT_READDIR64
3169 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3171 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3172 // FIXME: under ASan the call below may write to freed memory and corrupt
3173 // its metadata. See
3174 // https://github.com/google/sanitizers/issues/321.
3175 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3176 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3180 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3181 __sanitizer_dirent64 **result) {
3183 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3184 // FIXME: under ASan the call below may write to freed memory and corrupt
3185 // its metadata. See
3186 // https://github.com/google/sanitizers/issues/321.
3187 int res = REAL(readdir64_r)(dirp, entry, result);
3189 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3195 #define INIT_READDIR64 \
3196 COMMON_INTERCEPT_FUNCTION(readdir64); \
3197 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3199 #define INIT_READDIR64
3202 #if SANITIZER_INTERCEPT_PTRACE
3203 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3205 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3206 __sanitizer_iovec local_iovec;
3209 if (request == ptrace_setregs)
3210 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3211 else if (request == ptrace_setfpregs)
3212 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3213 else if (request == ptrace_setfpxregs)
3214 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3215 else if (request == ptrace_setvfpregs)
3216 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3217 else if (request == ptrace_setsiginfo)
3218 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3219 // Some kernel might zero the iovec::iov_base in case of invalid
3220 // write access. In this case copy the invalid address for further
3222 else if (request == ptrace_setregset || request == ptrace_getregset) {
3223 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3224 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3225 local_iovec = *iovec;
3226 if (request == ptrace_setregset)
3227 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3231 // FIXME: under ASan the call below may write to freed memory and corrupt
3232 // its metadata. See
3233 // https://github.com/google/sanitizers/issues/321.
3234 uptr res = REAL(ptrace)(request, pid, addr, data);
3237 // Note that PEEK* requests assign different meaning to the return value.
3238 // This function does not handle them (nor does it need to).
3239 if (request == ptrace_getregs)
3240 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3241 else if (request == ptrace_getfpregs)
3242 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3243 else if (request == ptrace_getfpxregs)
3244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3245 else if (request == ptrace_getvfpregs)
3246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3247 else if (request == ptrace_getsiginfo)
3248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3249 else if (request == ptrace_geteventmsg)
3250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3251 else if (request == ptrace_getregset) {
3252 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3253 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3255 local_iovec.iov_len);
3261 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3266 #if SANITIZER_INTERCEPT_SETLOCALE
3267 static void unpoison_ctype_arrays(void *ctx) {
3268 #if SANITIZER_NETBSD
3269 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3270 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3271 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3272 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3276 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3278 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3280 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3281 char *res = REAL(setlocale)(category, locale);
3283 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3284 unpoison_ctype_arrays(ctx);
3289 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3291 #define INIT_SETLOCALE
3294 #if SANITIZER_INTERCEPT_GETCWD
3295 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3297 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3298 // FIXME: under ASan the call below may write to freed memory and corrupt
3299 // its metadata. See
3300 // https://github.com/google/sanitizers/issues/321.
3301 char *res = REAL(getcwd)(buf, size);
3302 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3305 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3310 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3311 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3313 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3314 // FIXME: under ASan the call below may write to freed memory and corrupt
3315 // its metadata. See
3316 // https://github.com/google/sanitizers/issues/321.
3317 char *res = REAL(get_current_dir_name)(fake);
3318 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3322 #define INIT_GET_CURRENT_DIR_NAME \
3323 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3325 #define INIT_GET_CURRENT_DIR_NAME
3328 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3330 if (nptr == *endptr) {
3331 // No digits were found at strtol call, we need to find out the last
3332 // symbol accessed by strtoll on our own.
3333 // We get this symbol by skipping leading blanks and optional +/- sign.
3334 while (IsSpace(*nptr)) nptr++;
3335 if (*nptr == '+' || *nptr == '-') nptr++;
3336 *endptr = const_cast<char *>(nptr);
3338 CHECK(*endptr >= nptr);
3341 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3342 char **endptr, char *real_endptr, int base) {
3344 *endptr = real_endptr;
3345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3347 // If base has unsupported value, strtol can exit with EINVAL
3348 // without reading any characters. So do additional checks only
3349 // if base is valid.
3350 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3351 if (is_valid_base) {
3352 FixRealStrtolEndptr(nptr, &real_endptr);
3354 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3355 (real_endptr - nptr) + 1 : 0);
3359 #if SANITIZER_INTERCEPT_STRTOIMAX
3360 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3362 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3363 // FIXME: under ASan the call below may write to freed memory and corrupt
3364 // its metadata. See
3365 // https://github.com/google/sanitizers/issues/321.
3367 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3368 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3372 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3374 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3375 // FIXME: under ASan the call below may write to freed memory and corrupt
3376 // its metadata. See
3377 // https://github.com/google/sanitizers/issues/321.
3379 UINTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3380 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3384 #define INIT_STRTOIMAX \
3385 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3386 COMMON_INTERCEPT_FUNCTION(strtoumax);
3388 #define INIT_STRTOIMAX
3391 #if SANITIZER_INTERCEPT_MBSTOWCS
3392 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3394 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3395 // FIXME: under ASan the call below may write to freed memory and corrupt
3396 // its metadata. See
3397 // https://github.com/google/sanitizers/issues/321.
3398 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3399 if (res != (SIZE_T) - 1 && dest) {
3400 SIZE_T write_cnt = res + (res < len);
3401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3406 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3409 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3410 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3411 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3412 // FIXME: under ASan the call below may write to freed memory and corrupt
3413 // its metadata. See
3414 // https://github.com/google/sanitizers/issues/321.
3415 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3416 if (res != (SIZE_T)(-1) && dest && src) {
3417 // This function, and several others, may or may not write the terminating
3418 // \0 character. They write it iff they clear *src.
3419 SIZE_T write_cnt = res + !*src;
3420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3425 #define INIT_MBSTOWCS \
3426 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3427 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3429 #define INIT_MBSTOWCS
3432 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3433 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3434 SIZE_T len, void *ps) {
3436 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3438 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3439 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3441 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3442 // FIXME: under ASan the call below may write to freed memory and corrupt
3443 // its metadata. See
3444 // https://github.com/google/sanitizers/issues/321.
3445 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3446 if (res != (SIZE_T)(-1) && dest && src) {
3447 SIZE_T write_cnt = res + !*src;
3448 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3453 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3455 #define INIT_MBSNRTOWCS
3458 #if SANITIZER_INTERCEPT_WCSTOMBS
3459 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3461 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3462 // FIXME: under ASan the call below may write to freed memory and corrupt
3463 // its metadata. See
3464 // https://github.com/google/sanitizers/issues/321.
3465 SIZE_T res = REAL(wcstombs)(dest, src, len);
3466 if (res != (SIZE_T) - 1 && dest) {
3467 SIZE_T write_cnt = res + (res < len);
3468 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3473 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3476 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3477 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3478 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3479 // FIXME: under ASan the call below may write to freed memory and corrupt
3480 // its metadata. See
3481 // https://github.com/google/sanitizers/issues/321.
3482 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3483 if (res != (SIZE_T) - 1 && dest && src) {
3484 SIZE_T write_cnt = res + !*src;
3485 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3490 #define INIT_WCSTOMBS \
3491 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3492 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3494 #define INIT_WCSTOMBS
3497 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3498 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3499 SIZE_T len, void *ps) {
3501 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3503 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3504 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3506 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3507 // FIXME: under ASan the call below may write to freed memory and corrupt
3508 // its metadata. See
3509 // https://github.com/google/sanitizers/issues/321.
3510 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3511 if (res != ((SIZE_T)-1) && dest && src) {
3512 SIZE_T write_cnt = res + !*src;
3513 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3518 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3520 #define INIT_WCSNRTOMBS
3524 #if SANITIZER_INTERCEPT_WCRTOMB
3525 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3527 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3528 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3531 return REAL(wcrtomb)(dest, src, ps);
3533 char local_dest[32];
3534 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3535 if (res != ((SIZE_T)-1)) {
3536 CHECK_LE(res, sizeof(local_dest));
3537 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3538 REAL(memcpy)(dest, local_dest, res);
3543 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3545 #define INIT_WCRTOMB
3548 #if SANITIZER_INTERCEPT_WCTOMB
3549 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3551 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3553 return REAL(wctomb)(dest, src);
3555 char local_dest[32];
3556 int res = REAL(wctomb)(local_dest, src);
3558 CHECK_LE(res, sizeof(local_dest));
3559 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3560 REAL(memcpy)(dest, local_dest, res);
3565 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3570 #if SANITIZER_INTERCEPT_TCGETATTR
3571 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3573 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3574 // FIXME: under ASan the call below may write to freed memory and corrupt
3575 // its metadata. See
3576 // https://github.com/google/sanitizers/issues/321.
3577 int res = REAL(tcgetattr)(fd, termios_p);
3578 if (!res && termios_p)
3579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3583 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3585 #define INIT_TCGETATTR
3588 #if SANITIZER_INTERCEPT_REALPATH
3589 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3591 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3592 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3594 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3595 // version of a versioned symbol. For realpath(), this gives us something
3596 // (called __old_realpath) that does not handle NULL in the second argument.
3597 // Handle it as part of the interceptor.
3598 char *allocated_path = nullptr;
3600 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3602 char *res = REAL(realpath)(path, resolved_path);
3603 if (allocated_path && !res) WRAP(free)(allocated_path);
3604 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3607 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3609 #define INIT_REALPATH
3612 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3613 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3615 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3616 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3617 char *res = REAL(canonicalize_file_name)(path);
3618 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3621 #define INIT_CANONICALIZE_FILE_NAME \
3622 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3624 #define INIT_CANONICALIZE_FILE_NAME
3627 #if SANITIZER_INTERCEPT_CONFSTR
3628 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3630 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3631 // FIXME: under ASan the call below may write to freed memory and corrupt
3632 // its metadata. See
3633 // https://github.com/google/sanitizers/issues/321.
3634 SIZE_T res = REAL(confstr)(name, buf, len);
3636 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3639 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3641 #define INIT_CONFSTR
3644 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3645 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3647 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3648 // FIXME: under ASan the call below may write to freed memory and corrupt
3649 // its metadata. See
3650 // https://github.com/google/sanitizers/issues/321.
3651 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3652 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3655 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3657 #define INIT_SCHED_GETAFFINITY
3660 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3661 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3663 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3664 int res = REAL(sched_getparam)(pid, param);
3665 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3668 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3670 #define INIT_SCHED_GETPARAM
3673 #if SANITIZER_INTERCEPT_STRERROR
3674 INTERCEPTOR(char *, strerror, int errnum) {
3676 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3677 char *res = REAL(strerror)(errnum);
3678 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3681 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3683 #define INIT_STRERROR
3686 #if SANITIZER_INTERCEPT_STRERROR_R
3687 // There are 2 versions of strerror_r:
3688 // * POSIX version returns 0 on success, negative error code on failure,
3689 // writes message to buf.
3690 // * GNU version returns message pointer, which points to either buf or some
3692 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3693 SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3694 SANITIZER_FREEBSD || SANITIZER_OPENBSD
3695 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3696 // At least on OSX, buf contents are valid even when the call fails.
3697 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3699 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3700 // FIXME: under ASan the call below may write to freed memory and corrupt
3701 // its metadata. See
3702 // https://github.com/google/sanitizers/issues/321.
3703 int res = REAL(strerror_r)(errnum, buf, buflen);
3705 SIZE_T sz = internal_strnlen(buf, buflen);
3706 if (sz < buflen) ++sz;
3707 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3712 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3714 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3715 // FIXME: under ASan the call below may write to freed memory and corrupt
3716 // its metadata. See
3717 // https://github.com/google/sanitizers/issues/321.
3718 char *res = REAL(strerror_r)(errnum, buf, buflen);
3720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3722 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3725 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3727 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3729 #define INIT_STRERROR_R
3732 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3733 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3735 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3736 // FIXME: under ASan the call below may write to freed memory and corrupt
3737 // its metadata. See
3738 // https://github.com/google/sanitizers/issues/321.
3739 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3740 // This version always returns a null-terminated string.
3742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3745 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3747 #define INIT_XPG_STRERROR_R
3750 #if SANITIZER_INTERCEPT_SCANDIR
3751 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3752 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3753 const struct __sanitizer_dirent **);
3755 static THREADLOCAL scandir_filter_f scandir_filter;
3756 static THREADLOCAL scandir_compar_f scandir_compar;
3758 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3759 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3760 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3761 return scandir_filter(dir);
3764 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3765 const struct __sanitizer_dirent **b) {
3766 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3767 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3768 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3769 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3770 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3771 return scandir_compar(a, b);
3774 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3775 scandir_filter_f filter, scandir_compar_f compar) {
3777 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3778 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3779 scandir_filter = filter;
3780 scandir_compar = compar;
3781 // FIXME: under ASan the call below may write to freed memory and corrupt
3782 // its metadata. See
3783 // https://github.com/google/sanitizers/issues/321.
3784 int res = REAL(scandir)(dirp, namelist,
3785 filter ? wrapped_scandir_filter : nullptr,
3786 compar ? wrapped_scandir_compar : nullptr);
3787 scandir_filter = nullptr;
3788 scandir_compar = nullptr;
3789 if (namelist && res > 0) {
3790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3792 for (int i = 0; i < res; ++i)
3793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3794 (*namelist)[i]->d_reclen);
3798 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3800 #define INIT_SCANDIR
3803 #if SANITIZER_INTERCEPT_SCANDIR64
3804 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3805 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3806 const struct __sanitizer_dirent64 **);
3808 static THREADLOCAL scandir64_filter_f scandir64_filter;
3809 static THREADLOCAL scandir64_compar_f scandir64_compar;
3811 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3812 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3813 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3814 return scandir64_filter(dir);
3817 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3818 const struct __sanitizer_dirent64 **b) {
3819 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3820 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3821 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3822 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3823 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3824 return scandir64_compar(a, b);
3827 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3828 scandir64_filter_f filter, scandir64_compar_f compar) {
3830 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3831 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3832 scandir64_filter = filter;
3833 scandir64_compar = compar;
3834 // FIXME: under ASan the call below may write to freed memory and corrupt
3835 // its metadata. See
3836 // https://github.com/google/sanitizers/issues/321.
3838 REAL(scandir64)(dirp, namelist,
3839 filter ? wrapped_scandir64_filter : nullptr,
3840 compar ? wrapped_scandir64_compar : nullptr);
3841 scandir64_filter = nullptr;
3842 scandir64_compar = nullptr;
3843 if (namelist && res > 0) {
3844 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3846 for (int i = 0; i < res; ++i)
3847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3848 (*namelist)[i]->d_reclen);
3852 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3854 #define INIT_SCANDIR64
3857 #if SANITIZER_INTERCEPT_GETGROUPS
3858 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3860 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3861 // FIXME: under ASan the call below may write to freed memory and corrupt
3862 // its metadata. See
3863 // https://github.com/google/sanitizers/issues/321.
3864 int res = REAL(getgroups)(size, lst);
3865 if (res >= 0 && lst && size > 0)
3866 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3869 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3871 #define INIT_GETGROUPS
3874 #if SANITIZER_INTERCEPT_POLL
3875 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3876 __sanitizer_nfds_t nfds) {
3877 for (unsigned i = 0; i < nfds; ++i) {
3878 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3879 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3883 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3884 __sanitizer_nfds_t nfds) {
3885 for (unsigned i = 0; i < nfds; ++i)
3886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3887 sizeof(fds[i].revents));
3890 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3893 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3894 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3895 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3896 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3899 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3904 #if SANITIZER_INTERCEPT_PPOLL
3905 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3906 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3908 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3909 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3911 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3912 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3914 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3915 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3918 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3923 #if SANITIZER_INTERCEPT_WORDEXP
3924 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3926 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3927 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3928 // FIXME: under ASan the call below may write to freed memory and corrupt
3929 // its metadata. See
3930 // https://github.com/google/sanitizers/issues/321.
3931 int res = REAL(wordexp)(s, p, flags);
3933 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3936 sizeof(*p->we_wordv) * p->we_wordc);
3937 for (uptr i = 0; i < p->we_wordc; ++i) {
3938 char *w = p->we_wordv[i];
3939 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3944 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3946 #define INIT_WORDEXP
3949 #if SANITIZER_INTERCEPT_SIGWAIT
3950 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3952 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3953 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3954 // FIXME: under ASan the call below may write to freed memory and corrupt
3955 // its metadata. See
3956 // https://github.com/google/sanitizers/issues/321.
3957 int res = REAL(sigwait)(set, sig);
3958 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3961 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3963 #define INIT_SIGWAIT
3966 #if SANITIZER_INTERCEPT_SIGWAITINFO
3967 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3969 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3970 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3971 // FIXME: under ASan the call below may write to freed memory and corrupt
3972 // its metadata. See
3973 // https://github.com/google/sanitizers/issues/321.
3974 int res = REAL(sigwaitinfo)(set, info);
3975 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3978 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3980 #define INIT_SIGWAITINFO
3983 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3984 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3987 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3988 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3989 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3990 // FIXME: under ASan the call below may write to freed memory and corrupt
3991 // its metadata. See
3992 // https://github.com/google/sanitizers/issues/321.
3993 int res = REAL(sigtimedwait)(set, info, timeout);
3994 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3997 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
3999 #define INIT_SIGTIMEDWAIT
4002 #if SANITIZER_INTERCEPT_SIGSETOPS
4003 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4005 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4006 // FIXME: under ASan the call below may write to freed memory and corrupt
4007 // its metadata. See
4008 // https://github.com/google/sanitizers/issues/321.
4009 int res = REAL(sigemptyset)(set);
4010 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4014 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4016 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4017 // FIXME: under ASan the call below may write to freed memory and corrupt
4018 // its metadata. See
4019 // https://github.com/google/sanitizers/issues/321.
4020 int res = REAL(sigfillset)(set);
4021 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4024 #define INIT_SIGSETOPS \
4025 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4026 COMMON_INTERCEPT_FUNCTION(sigfillset);
4028 #define INIT_SIGSETOPS
4031 #if SANITIZER_INTERCEPT_SIGPENDING
4032 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4034 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4035 // FIXME: under ASan the call below may write to freed memory and corrupt
4036 // its metadata. See
4037 // https://github.com/google/sanitizers/issues/321.
4038 int res = REAL(sigpending)(set);
4039 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4042 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4044 #define INIT_SIGPENDING
4047 #if SANITIZER_INTERCEPT_SIGPROCMASK
4048 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4049 __sanitizer_sigset_t *oldset) {
4051 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4052 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4053 // FIXME: under ASan the call below may write to freed memory and corrupt
4054 // its metadata. See
4055 // https://github.com/google/sanitizers/issues/321.
4056 int res = REAL(sigprocmask)(how, set, oldset);
4058 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4061 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4063 #define INIT_SIGPROCMASK
4066 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4067 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4068 __sanitizer_sigset_t *oldset) {
4070 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4071 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4072 // FIXME: under ASan the call below may write to freed memory and corrupt
4073 // its metadata. See
4074 // https://github.com/google/sanitizers/issues/321.
4075 int res = REAL(pthread_sigmask)(how, set, oldset);
4077 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4080 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4082 #define INIT_PTHREAD_SIGMASK
4085 #if SANITIZER_INTERCEPT_BACKTRACE
4086 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4088 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4089 // FIXME: under ASan the call below may write to freed memory and corrupt
4090 // its metadata. See
4091 // https://github.com/google/sanitizers/issues/321.
4092 int res = REAL(backtrace)(buffer, size);
4094 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4098 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4100 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4102 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4103 // FIXME: under ASan the call below may write to freed memory and corrupt
4104 // its metadata. See
4105 // https://github.com/google/sanitizers/issues/321.
4106 char **res = REAL(backtrace_symbols)(buffer, size);
4108 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4109 for (int i = 0; i < size; ++i)
4110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4114 #define INIT_BACKTRACE \
4115 COMMON_INTERCEPT_FUNCTION(backtrace); \
4116 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4118 #define INIT_BACKTRACE
4121 #if SANITIZER_INTERCEPT__EXIT
4122 INTERCEPTOR(void, _exit, int status) {
4124 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4125 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4126 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4127 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4128 if (status == 0) status = status1;
4129 REAL(_exit)(status);
4131 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4136 #if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4137 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4139 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4140 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4141 int res = REAL(pthread_mutex_lock)(m);
4142 if (res == errno_EOWNERDEAD)
4143 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4144 if (res == 0 || res == errno_EOWNERDEAD)
4145 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4146 if (res == errno_EINVAL)
4147 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4151 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4153 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4154 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4155 int res = REAL(pthread_mutex_unlock)(m);
4156 if (res == errno_EINVAL)
4157 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4161 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4162 #define INIT_PTHREAD_MUTEX_UNLOCK \
4163 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4165 #define INIT_PTHREAD_MUTEX_LOCK
4166 #define INIT_PTHREAD_MUTEX_UNLOCK
4169 #if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4170 INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4171 return WRAP(pthread_mutex_lock)(m);
4174 INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4175 return WRAP(pthread_mutex_unlock)(m);
4178 #define INIT___PTHREAD_MUTEX_LOCK \
4179 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4180 #define INIT___PTHREAD_MUTEX_UNLOCK \
4181 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4183 #define INIT___PTHREAD_MUTEX_LOCK
4184 #define INIT___PTHREAD_MUTEX_UNLOCK
4187 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4188 INTERCEPTOR(int, __libc_mutex_lock, void *m)
4189 ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4191 INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4192 ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4194 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4195 ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4197 #define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4198 #define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4199 #define INIT___LIBC_THR_SETCANCELSTATE \
4200 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4202 #define INIT___LIBC_MUTEX_LOCK
4203 #define INIT___LIBC_MUTEX_UNLOCK
4204 #define INIT___LIBC_THR_SETCANCELSTATE
4207 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4208 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4209 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4210 if (mnt->mnt_fsname)
4211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4212 REAL(strlen)(mnt->mnt_fsname) + 1);
4214 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4215 REAL(strlen)(mnt->mnt_dir) + 1);
4217 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4218 REAL(strlen)(mnt->mnt_type) + 1);
4220 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4221 REAL(strlen)(mnt->mnt_opts) + 1);
4225 #if SANITIZER_INTERCEPT_GETMNTENT
4226 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4228 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4229 __sanitizer_mntent *res = REAL(getmntent)(fp);
4230 if (res) write_mntent(ctx, res);
4233 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4235 #define INIT_GETMNTENT
4238 #if SANITIZER_INTERCEPT_GETMNTENT_R
4239 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4240 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4242 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4243 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4244 if (res) write_mntent(ctx, res);
4247 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4249 #define INIT_GETMNTENT_R
4252 #if SANITIZER_INTERCEPT_STATFS
4253 INTERCEPTOR(int, statfs, char *path, void *buf) {
4255 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4256 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4257 // FIXME: under ASan the call below may write to freed memory and corrupt
4258 // its metadata. See
4259 // https://github.com/google/sanitizers/issues/321.
4260 int res = REAL(statfs)(path, buf);
4261 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4264 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4266 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4267 // FIXME: under ASan the call below may write to freed memory and corrupt
4268 // its metadata. See
4269 // https://github.com/google/sanitizers/issues/321.
4270 int res = REAL(fstatfs)(fd, buf);
4271 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4274 #define INIT_STATFS \
4275 COMMON_INTERCEPT_FUNCTION(statfs); \
4276 COMMON_INTERCEPT_FUNCTION(fstatfs);
4281 #if SANITIZER_INTERCEPT_STATFS64
4282 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4284 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4285 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4286 // FIXME: under ASan the call below may write to freed memory and corrupt
4287 // its metadata. See
4288 // https://github.com/google/sanitizers/issues/321.
4289 int res = REAL(statfs64)(path, buf);
4290 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4293 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4295 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4296 // FIXME: under ASan the call below may write to freed memory and corrupt
4297 // its metadata. See
4298 // https://github.com/google/sanitizers/issues/321.
4299 int res = REAL(fstatfs64)(fd, buf);
4300 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4303 #define INIT_STATFS64 \
4304 COMMON_INTERCEPT_FUNCTION(statfs64); \
4305 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4307 #define INIT_STATFS64
4310 #if SANITIZER_INTERCEPT_STATVFS
4311 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4313 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4314 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4315 // FIXME: under ASan the call below may write to freed memory and corrupt
4316 // its metadata. See
4317 // https://github.com/google/sanitizers/issues/321.
4318 int res = REAL(statvfs)(path, buf);
4319 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4322 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4324 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4325 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4326 // FIXME: under ASan the call below may write to freed memory and corrupt
4327 // its metadata. See
4328 // https://github.com/google/sanitizers/issues/321.
4329 int res = REAL(fstatvfs)(fd, buf);
4331 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4333 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4337 #define INIT_STATVFS \
4338 COMMON_INTERCEPT_FUNCTION(statvfs); \
4339 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4341 #define INIT_STATVFS
4344 #if SANITIZER_INTERCEPT_STATVFS64
4345 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4347 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4348 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4349 // FIXME: under ASan the call below may write to freed memory and corrupt
4350 // its metadata. See
4351 // https://github.com/google/sanitizers/issues/321.
4352 int res = REAL(statvfs64)(path, buf);
4353 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4356 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4358 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4359 // FIXME: under ASan the call below may write to freed memory and corrupt
4360 // its metadata. See
4361 // https://github.com/google/sanitizers/issues/321.
4362 int res = REAL(fstatvfs64)(fd, buf);
4363 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4366 #define INIT_STATVFS64 \
4367 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4368 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4370 #define INIT_STATVFS64
4373 #if SANITIZER_INTERCEPT_INITGROUPS
4374 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4376 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4377 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4378 int res = REAL(initgroups)(user, group);
4381 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4383 #define INIT_INITGROUPS
4386 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4387 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4389 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4390 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4391 char *res = REAL(ether_ntoa)(addr);
4392 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4395 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4397 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4398 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4399 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4400 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4403 #define INIT_ETHER_NTOA_ATON \
4404 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4405 COMMON_INTERCEPT_FUNCTION(ether_aton);
4407 #define INIT_ETHER_NTOA_ATON
4410 #if SANITIZER_INTERCEPT_ETHER_HOST
4411 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4413 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4414 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4415 // FIXME: under ASan the call below may write to freed memory and corrupt
4416 // its metadata. See
4417 // https://github.com/google/sanitizers/issues/321.
4418 int res = REAL(ether_ntohost)(hostname, addr);
4419 if (!res && hostname)
4420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4423 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4425 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4427 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4428 // FIXME: under ASan the call below may write to freed memory and corrupt
4429 // its metadata. See
4430 // https://github.com/google/sanitizers/issues/321.
4431 int res = REAL(ether_hostton)(hostname, addr);
4432 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4435 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4438 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4439 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4440 // FIXME: under ASan the call below may write to freed memory and corrupt
4441 // its metadata. See
4442 // https://github.com/google/sanitizers/issues/321.
4443 int res = REAL(ether_line)(line, addr, hostname);
4445 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4447 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4451 #define INIT_ETHER_HOST \
4452 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4453 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4454 COMMON_INTERCEPT_FUNCTION(ether_line);
4456 #define INIT_ETHER_HOST
4459 #if SANITIZER_INTERCEPT_ETHER_R
4460 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4462 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4463 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4464 // FIXME: under ASan the call below may write to freed memory and corrupt
4465 // its metadata. See
4466 // https://github.com/google/sanitizers/issues/321.
4467 char *res = REAL(ether_ntoa_r)(addr, buf);
4468 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4471 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4472 __sanitizer_ether_addr *addr) {
4474 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4475 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4476 // FIXME: under ASan the call below may write to freed memory and corrupt
4477 // its metadata. See
4478 // https://github.com/google/sanitizers/issues/321.
4479 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4480 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4483 #define INIT_ETHER_R \
4484 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4485 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4487 #define INIT_ETHER_R
4490 #if SANITIZER_INTERCEPT_SHMCTL
4491 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4493 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4494 // FIXME: under ASan the call below may write to freed memory and corrupt
4495 // its metadata. See
4496 // https://github.com/google/sanitizers/issues/321.
4497 int res = REAL(shmctl)(shmid, cmd, buf);
4500 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4501 sz = sizeof(__sanitizer_shmid_ds);
4502 else if (cmd == shmctl_ipc_info)
4503 sz = struct_shminfo_sz;
4504 else if (cmd == shmctl_shm_info)
4505 sz = struct_shm_info_sz;
4506 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4510 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4515 #if SANITIZER_INTERCEPT_RANDOM_R
4516 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4518 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4519 // FIXME: under ASan the call below may write to freed memory and corrupt
4520 // its metadata. See
4521 // https://github.com/google/sanitizers/issues/321.
4522 int res = REAL(random_r)(buf, result);
4524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4527 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4529 #define INIT_RANDOM_R
4532 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4533 // its metadata. See
4534 // https://github.com/google/sanitizers/issues/321.
4535 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4536 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4537 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4538 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4539 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4540 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4541 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4542 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4543 INTERCEPTOR(int, fn, void *attr, void *r) { \
4545 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4546 int res = REAL(fn)(attr, r); \
4547 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4550 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4551 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4552 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4553 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4554 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4555 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4556 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4557 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4558 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4559 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4562 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4563 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4564 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4565 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4566 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4567 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4569 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4570 // FIXME: under ASan the call below may write to freed memory and corrupt
4571 // its metadata. See
4572 // https://github.com/google/sanitizers/issues/321.
4573 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4575 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4576 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4581 // We may need to call the real pthread_attr_getstack from the run-time
4582 // in sanitizer_common, but we don't want to include the interception headers
4583 // there. So, just define this function here.
4584 namespace __sanitizer {
4586 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4587 return REAL(pthread_attr_getstack)(attr, addr, size);
4590 } // namespace __sanitizer
4592 #define INIT_PTHREAD_ATTR_GET \
4593 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4594 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4595 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4596 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4597 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4599 #define INIT_PTHREAD_ATTR_GET
4602 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4603 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4604 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4606 #define INIT_PTHREAD_ATTR_GET_SCHED \
4607 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4608 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4610 #define INIT_PTHREAD_ATTR_GET_SCHED
4613 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4614 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4616 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4617 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4619 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4622 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4623 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4626 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4628 // FIXME: under ASan the call below may write to freed memory and corrupt
4629 // its metadata. See
4630 // https://github.com/google/sanitizers/issues/321.
4631 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4632 if (!res && cpusetsize && cpuset)
4633 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4637 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4638 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4640 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4643 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4644 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4645 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4646 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4648 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4651 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4652 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4653 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4654 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4656 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4659 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4660 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4661 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4662 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4664 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4667 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4668 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4669 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4670 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4672 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4675 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4676 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4677 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4678 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4680 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4683 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4684 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4685 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4686 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4688 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4691 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4692 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4693 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4694 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4696 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4699 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4700 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4701 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4702 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4704 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4707 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4708 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4709 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4710 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4712 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4715 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4716 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4717 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4718 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4720 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4723 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4724 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4725 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4726 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4728 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4731 #if SANITIZER_INTERCEPT_TMPNAM
4732 INTERCEPTOR(char *, tmpnam, char *s) {
4734 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4735 char *res = REAL(tmpnam)(s);
4738 // FIXME: under ASan the call below may write to freed memory and corrupt
4739 // its metadata. See
4740 // https://github.com/google/sanitizers/issues/321.
4741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4743 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4747 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4752 #if SANITIZER_INTERCEPT_TMPNAM_R
4753 INTERCEPTOR(char *, tmpnam_r, char *s) {
4755 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4756 // FIXME: under ASan the call below may write to freed memory and corrupt
4757 // its metadata. See
4758 // https://github.com/google/sanitizers/issues/321.
4759 char *res = REAL(tmpnam_r)(s);
4760 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4763 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4765 #define INIT_TMPNAM_R
4768 #if SANITIZER_INTERCEPT_TTYNAME
4769 INTERCEPTOR(char *, ttyname, int fd) {
4771 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
4772 char *res = REAL(ttyname)(fd);
4774 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4777 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
4779 #define INIT_TTYNAME
4782 #if SANITIZER_INTERCEPT_TTYNAME_R
4783 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4785 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4786 int res = REAL(ttyname_r)(fd, name, namesize);
4788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4791 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4793 #define INIT_TTYNAME_R
4796 #if SANITIZER_INTERCEPT_TEMPNAM
4797 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4799 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4800 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4801 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4802 char *res = REAL(tempnam)(dir, pfx);
4803 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4806 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4808 #define INIT_TEMPNAM
4811 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4812 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4814 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4815 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4816 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4817 return REAL(pthread_setname_np)(thread, name);
4819 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4820 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4821 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4823 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4824 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4825 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4826 internal_snprintf(newname, sizeof(newname), name, arg);
4827 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4828 return REAL(pthread_setname_np)(thread, name, arg);
4830 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4832 #define INIT_PTHREAD_SETNAME_NP
4835 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4836 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4838 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4839 int res = REAL(pthread_getname_np)(thread, name, len);
4841 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4844 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4846 #define INIT_PTHREAD_GETNAME_NP
4849 #if SANITIZER_INTERCEPT_SINCOS
4850 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4852 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4853 // FIXME: under ASan the call below may write to freed memory and corrupt
4854 // its metadata. See
4855 // https://github.com/google/sanitizers/issues/321.
4856 REAL(sincos)(x, sin, cos);
4857 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4858 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4860 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4862 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4863 // FIXME: under ASan the call below may write to freed memory and corrupt
4864 // its metadata. See
4865 // https://github.com/google/sanitizers/issues/321.
4866 REAL(sincosf)(x, sin, cos);
4867 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4868 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4870 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4872 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4873 // FIXME: under ASan the call below may write to freed memory and corrupt
4874 // its metadata. See
4875 // https://github.com/google/sanitizers/issues/321.
4876 REAL(sincosl)(x, sin, cos);
4877 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4878 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4880 #define INIT_SINCOS \
4881 COMMON_INTERCEPT_FUNCTION(sincos); \
4882 COMMON_INTERCEPT_FUNCTION(sincosf); \
4883 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4888 #if SANITIZER_INTERCEPT_REMQUO
4889 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4891 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4892 // FIXME: under ASan the call below may write to freed memory and corrupt
4893 // its metadata. See
4894 // https://github.com/google/sanitizers/issues/321.
4895 double res = REAL(remquo)(x, y, quo);
4896 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4899 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4901 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4902 // FIXME: under ASan the call below may write to freed memory and corrupt
4903 // its metadata. See
4904 // https://github.com/google/sanitizers/issues/321.
4905 float res = REAL(remquof)(x, y, quo);
4906 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4909 #define INIT_REMQUO \
4910 COMMON_INTERCEPT_FUNCTION(remquo); \
4911 COMMON_INTERCEPT_FUNCTION(remquof);
4916 #if SANITIZER_INTERCEPT_REMQUOL
4917 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4919 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4920 // FIXME: under ASan the call below may write to freed memory and corrupt
4921 // its metadata. See
4922 // https://github.com/google/sanitizers/issues/321.
4923 long double res = REAL(remquol)(x, y, quo);
4924 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4927 #define INIT_REMQUOL \
4928 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4930 #define INIT_REMQUOL
4933 #if SANITIZER_INTERCEPT_LGAMMA
4935 INTERCEPTOR(double, lgamma, double x) {
4937 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4938 double res = REAL(lgamma)(x);
4939 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4942 INTERCEPTOR(float, lgammaf, float x) {
4944 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4945 float res = REAL(lgammaf)(x);
4946 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4949 #define INIT_LGAMMA \
4950 COMMON_INTERCEPT_FUNCTION(lgamma); \
4951 COMMON_INTERCEPT_FUNCTION(lgammaf);
4956 #if SANITIZER_INTERCEPT_LGAMMAL
4957 INTERCEPTOR(long double, lgammal, long double x) {
4959 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4960 long double res = REAL(lgammal)(x);
4961 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4964 #define INIT_LGAMMAL \
4965 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4967 #define INIT_LGAMMAL
4970 #if SANITIZER_INTERCEPT_LGAMMA_R
4971 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4973 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4974 // FIXME: under ASan the call below may write to freed memory and corrupt
4975 // its metadata. See
4976 // https://github.com/google/sanitizers/issues/321.
4977 double res = REAL(lgamma_r)(x, signp);
4978 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4981 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4983 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4984 // FIXME: under ASan the call below may write to freed memory and corrupt
4985 // its metadata. See
4986 // https://github.com/google/sanitizers/issues/321.
4987 float res = REAL(lgammaf_r)(x, signp);
4988 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4991 #define INIT_LGAMMA_R \
4992 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4993 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4995 #define INIT_LGAMMA_R
4998 #if SANITIZER_INTERCEPT_LGAMMAL_R
4999 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5001 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5002 // FIXME: under ASan the call below may write to freed memory and corrupt
5003 // its metadata. See
5004 // https://github.com/google/sanitizers/issues/321.
5005 long double res = REAL(lgammal_r)(x, signp);
5006 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5009 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5011 #define INIT_LGAMMAL_R
5014 #if SANITIZER_INTERCEPT_DRAND48_R
5015 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5017 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5018 // FIXME: under ASan the call below may write to freed memory and corrupt
5019 // its metadata. See
5020 // https://github.com/google/sanitizers/issues/321.
5021 int res = REAL(drand48_r)(buffer, result);
5022 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5025 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5027 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5028 // FIXME: under ASan the call below may write to freed memory and corrupt
5029 // its metadata. See
5030 // https://github.com/google/sanitizers/issues/321.
5031 int res = REAL(lrand48_r)(buffer, result);
5032 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5035 #define INIT_DRAND48_R \
5036 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5037 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5039 #define INIT_DRAND48_R
5042 #if SANITIZER_INTERCEPT_RAND_R
5043 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5045 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5046 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5047 return REAL(rand_r)(seedp);
5049 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5054 #if SANITIZER_INTERCEPT_GETLINE
5055 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5057 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5058 // FIXME: under ASan the call below may write to freed memory and corrupt
5059 // its metadata. See
5060 // https://github.com/google/sanitizers/issues/321.
5061 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5065 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5070 // FIXME: under ASan the call below may write to freed memory and corrupt its
5072 // https://github.com/google/sanitizers/issues/321.
5073 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5076 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5077 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5079 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5080 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5086 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5088 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5090 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5091 // with its own body.
5092 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5094 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5096 #define INIT_GETLINE \
5097 COMMON_INTERCEPT_FUNCTION(getline); \
5098 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5099 COMMON_INTERCEPT_FUNCTION(getdelim);
5101 #define INIT_GETLINE
5104 #if SANITIZER_INTERCEPT_ICONV
5105 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5106 char **outbuf, SIZE_T *outbytesleft) {
5108 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5111 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5112 if (inbuf && inbytesleft)
5113 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5115 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5116 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5117 // FIXME: under ASan the call below may write to freed memory and corrupt
5118 // its metadata. See
5119 // https://github.com/google/sanitizers/issues/321.
5120 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5121 if (outbuf && *outbuf > outbuf_orig) {
5122 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5123 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5127 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5132 #if SANITIZER_INTERCEPT_TIMES
5133 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5135 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5136 // FIXME: under ASan the call below may write to freed memory and corrupt
5137 // its metadata. See
5138 // https://github.com/google/sanitizers/issues/321.
5139 __sanitizer_clock_t res = REAL(times)(tms);
5140 if (res != (__sanitizer_clock_t)-1 && tms)
5141 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5144 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5149 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5151 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5152 // If you see any crashes around this functions, there are 2 known issues with
5153 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5154 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5155 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5156 // to access thread local variables (it should not happen normally,
5157 // because sanitizers use initial-exec tls model).
5158 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5160 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5161 void *res = REAL(__tls_get_addr)(arg);
5162 uptr tls_begin, tls_end;
5163 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5164 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5166 // New DTLS block has been allocated.
5167 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5172 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5173 // mostly the same semantics as __tls_get_addr, but its presence enables
5174 // some optimizations in linker (which are safe to ignore here).
5175 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5176 visibility("default")))
5177 void *__tls_get_addr_opt(void *arg);
5179 #else // SANITIZER_S390
5180 // On s390, we have to intercept two functions here:
5181 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5182 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5183 // a proper pointer. It is used by dlsym for TLS symbols.
5184 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5185 // descriptor offset as an argument instead of a pointer. GOT address
5186 // is passed in r12, so it's necessary to write it in assembly. This is
5187 // the function used by the compiler.
5188 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5189 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5190 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5191 extern "C" uptr __tls_get_offset(void *arg);
5192 extern "C" uptr __interceptor___tls_get_offset(void *arg);
5193 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5195 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5196 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5197 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5198 void *ptr = reinterpret_cast<void *>(res + tp);
5199 uptr tls_begin, tls_end;
5200 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5201 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5203 // New DTLS block has been allocated.
5204 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5208 // We need a hidden symbol aliasing the above, so that we can jump
5209 // directly to it from the assembly below.
5210 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5211 visibility("hidden")))
5212 uptr __tls_get_addr_hidden(void *arg);
5213 // Now carefully intercept __tls_get_offset.
5216 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5217 // exports our symbol.
5218 ".weak __tls_get_offset\n"
5219 ".type __tls_get_offset, @function\n"
5220 "__tls_get_offset:\n"
5221 ".global __interceptor___tls_get_offset\n"
5222 ".type __interceptor___tls_get_offset, @function\n"
5223 "__interceptor___tls_get_offset:\n"
5225 "la %r2, 0(%r2,%r12)\n"
5226 "jg __tls_get_addr_hidden\n"
5229 "0: la %r2,0(%r2,%r12)\n"
5230 "l %r4,1f-0b(%r3)\n"
5232 "1: .long __tls_get_addr_hidden - 0b\n"
5234 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5235 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5236 ".type __tls_get_offset_wrapper, @function\n"
5237 "__tls_get_offset_wrapper:\n"
5244 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5246 #endif // SANITIZER_S390
5248 #define INIT_TLS_GET_ADDR
5251 #if SANITIZER_INTERCEPT_LISTXATTR
5252 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5254 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5255 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5256 // FIXME: under ASan the call below may write to freed memory and corrupt
5257 // its metadata. See
5258 // https://github.com/google/sanitizers/issues/321.
5259 SSIZE_T res = REAL(listxattr)(path, list, size);
5260 // Here and below, size == 0 is a special case where nothing is written to the
5261 // buffer, and res contains the desired buffer size.
5262 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5265 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5267 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5268 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5269 // FIXME: under ASan the call below may write to freed memory and corrupt
5270 // its metadata. See
5271 // https://github.com/google/sanitizers/issues/321.
5272 SSIZE_T res = REAL(llistxattr)(path, list, size);
5273 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5276 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5278 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5279 // FIXME: under ASan the call below may write to freed memory and corrupt
5280 // its metadata. See
5281 // https://github.com/google/sanitizers/issues/321.
5282 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5283 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5286 #define INIT_LISTXATTR \
5287 COMMON_INTERCEPT_FUNCTION(listxattr); \
5288 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5289 COMMON_INTERCEPT_FUNCTION(flistxattr);
5291 #define INIT_LISTXATTR
5294 #if SANITIZER_INTERCEPT_GETXATTR
5295 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5298 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5299 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5300 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5301 // FIXME: under ASan the call below may write to freed memory and corrupt
5302 // its metadata. See
5303 // https://github.com/google/sanitizers/issues/321.
5304 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5305 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5308 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5311 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5312 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5313 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5314 // FIXME: under ASan the call below may write to freed memory and corrupt
5315 // its metadata. See
5316 // https://github.com/google/sanitizers/issues/321.
5317 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5318 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5321 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5324 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5325 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5326 // FIXME: under ASan the call below may write to freed memory and corrupt
5327 // its metadata. See
5328 // https://github.com/google/sanitizers/issues/321.
5329 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5330 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5333 #define INIT_GETXATTR \
5334 COMMON_INTERCEPT_FUNCTION(getxattr); \
5335 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5336 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5338 #define INIT_GETXATTR
5341 #if SANITIZER_INTERCEPT_GETRESID
5342 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5344 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5345 // FIXME: under ASan the call below may write to freed memory and corrupt
5346 // its metadata. See
5347 // https://github.com/google/sanitizers/issues/321.
5348 int res = REAL(getresuid)(ruid, euid, suid);
5350 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5351 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5352 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5356 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5358 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5359 // FIXME: under ASan the call below may write to freed memory and corrupt
5360 // its metadata. See
5361 // https://github.com/google/sanitizers/issues/321.
5362 int res = REAL(getresgid)(rgid, egid, sgid);
5364 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5365 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5366 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5370 #define INIT_GETRESID \
5371 COMMON_INTERCEPT_FUNCTION(getresuid); \
5372 COMMON_INTERCEPT_FUNCTION(getresgid);
5374 #define INIT_GETRESID
5377 #if SANITIZER_INTERCEPT_GETIFADDRS
5378 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5379 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5380 // intercept it to poison the memory again.
5381 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5383 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5384 // FIXME: under ASan the call below may write to freed memory and corrupt
5385 // its metadata. See
5386 // https://github.com/google/sanitizers/issues/321.
5387 int res = REAL(getifaddrs)(ifap);
5388 if (res == 0 && ifap) {
5389 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5390 __sanitizer_ifaddrs *p = *ifap;
5392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5394 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5395 REAL(strlen)(p->ifa_name) + 1);
5397 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5400 // On Linux this is a union, but the other member also points to a
5401 // struct sockaddr, so the following is sufficient.
5403 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5404 // FIXME(smatveev): Unpoison p->ifa_data as well.
5410 #define INIT_GETIFADDRS \
5411 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5413 #define INIT_GETIFADDRS
5416 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5417 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5419 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5420 // FIXME: under ASan the call below may write to freed memory and corrupt
5421 // its metadata. See
5422 // https://github.com/google/sanitizers/issues/321.
5423 char *res = REAL(if_indextoname)(ifindex, ifname);
5425 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5428 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5430 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5432 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5433 return REAL(if_nametoindex)(ifname);
5435 #define INIT_IF_INDEXTONAME \
5436 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5437 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5439 #define INIT_IF_INDEXTONAME
5442 #if SANITIZER_INTERCEPT_CAPGET
5443 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5445 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5447 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5448 // FIXME: under ASan the call below may write to freed memory and corrupt
5449 // its metadata. See
5450 // https://github.com/google/sanitizers/issues/321.
5451 int res = REAL(capget)(hdrp, datap);
5452 if (res == 0 && datap)
5453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5454 // We can also return -1 and write to hdrp->version if the version passed in
5455 // hdrp->version is unsupported. But that's not a trivial condition to check,
5456 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5459 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5461 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5463 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5465 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5466 return REAL(capset)(hdrp, datap);
5468 #define INIT_CAPGET \
5469 COMMON_INTERCEPT_FUNCTION(capget); \
5470 COMMON_INTERCEPT_FUNCTION(capset);
5475 #if SANITIZER_INTERCEPT_AEABI_MEM
5476 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5478 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5481 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5483 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5486 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5488 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5491 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5493 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5496 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5498 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5501 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5503 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5506 // Note the argument order.
5507 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5509 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5512 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5514 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5517 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5519 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5522 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5524 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5527 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5529 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5532 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5534 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5537 #define INIT_AEABI_MEM \
5538 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5539 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5540 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5541 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5542 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5543 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5544 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5545 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5546 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5547 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5548 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5549 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5551 #define INIT_AEABI_MEM
5552 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5554 #if SANITIZER_INTERCEPT___BZERO
5555 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5557 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5559 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5561 #define INIT___BZERO
5562 #endif // SANITIZER_INTERCEPT___BZERO
5564 #if SANITIZER_INTERCEPT_BZERO
5565 INTERCEPTOR(void *, bzero, void *block, uptr size) {
5567 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5569 #define INIT_BZERO COMMON_INTERCEPT_FUNCTION(bzero);
5572 #endif // SANITIZER_INTERCEPT_BZERO
5574 #if SANITIZER_INTERCEPT_FTIME
5575 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5577 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5578 // FIXME: under ASan the call below may write to freed memory and corrupt
5579 // its metadata. See
5580 // https://github.com/google/sanitizers/issues/321.
5581 int res = REAL(ftime)(tp);
5583 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5586 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5589 #endif // SANITIZER_INTERCEPT_FTIME
5591 #if SANITIZER_INTERCEPT_XDR
5592 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5593 unsigned size, int op) {
5595 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5596 // FIXME: under ASan the call below may write to freed memory and corrupt
5597 // its metadata. See
5598 // https://github.com/google/sanitizers/issues/321.
5599 REAL(xdrmem_create)(xdrs, addr, size, op);
5600 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5601 if (op == __sanitizer_XDR_ENCODE) {
5602 // It's not obvious how much data individual xdr_ routines write.
5603 // Simply unpoison the entire target buffer in advance.
5604 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5608 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5610 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5611 // FIXME: under ASan the call below may write to freed memory and corrupt
5612 // its metadata. See
5613 // https://github.com/google/sanitizers/issues/321.
5614 REAL(xdrstdio_create)(xdrs, file, op);
5615 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5618 // FIXME: under ASan the call below may write to freed memory and corrupt
5619 // its metadata. See
5620 // https://github.com/google/sanitizers/issues/321.
5621 #define XDR_INTERCEPTOR(F, T) \
5622 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5624 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5625 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5626 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5627 int res = REAL(F)(xdrs, p); \
5628 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5629 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5633 XDR_INTERCEPTOR(xdr_short, short)
5634 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5635 XDR_INTERCEPTOR(xdr_int, int)
5636 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5637 XDR_INTERCEPTOR(xdr_long, long)
5638 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5639 XDR_INTERCEPTOR(xdr_hyper, long long)
5640 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5641 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5642 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5643 XDR_INTERCEPTOR(xdr_int8_t, u8)
5644 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5645 XDR_INTERCEPTOR(xdr_int16_t, u16)
5646 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5647 XDR_INTERCEPTOR(xdr_int32_t, u32)
5648 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5649 XDR_INTERCEPTOR(xdr_int64_t, u64)
5650 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5651 XDR_INTERCEPTOR(xdr_quad_t, long long)
5652 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5653 XDR_INTERCEPTOR(xdr_bool, bool)
5654 XDR_INTERCEPTOR(xdr_enum, int)
5655 XDR_INTERCEPTOR(xdr_char, char)
5656 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5657 XDR_INTERCEPTOR(xdr_float, float)
5658 XDR_INTERCEPTOR(xdr_double, double)
5660 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5661 // wrapstring, sizeof
5663 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5666 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5667 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5668 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5669 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5670 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5672 // FIXME: under ASan the call below may write to freed memory and corrupt
5673 // its metadata. See
5674 // https://github.com/google/sanitizers/issues/321.
5675 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5676 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5677 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5678 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5679 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5684 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5687 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5688 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5689 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5690 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5692 // FIXME: under ASan the call below may write to freed memory and corrupt
5693 // its metadata. See
5694 // https://github.com/google/sanitizers/issues/321.
5695 int res = REAL(xdr_string)(xdrs, p, maxsize);
5696 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5699 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5705 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5706 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5707 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5708 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5709 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5710 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5711 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5712 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5713 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5714 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5715 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5716 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5717 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5718 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5719 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5720 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5721 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5722 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5723 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5724 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5725 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5726 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5727 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5728 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5729 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5730 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5731 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5732 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5733 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5734 COMMON_INTERCEPT_FUNCTION(xdr_string);
5737 #endif // SANITIZER_INTERCEPT_XDR
5739 #if SANITIZER_INTERCEPT_TSEARCH
5740 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5741 int (*compar)(const void *, const void *)) {
5743 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5744 // FIXME: under ASan the call below may write to freed memory and corrupt
5745 // its metadata. See
5746 // https://github.com/google/sanitizers/issues/321.
5747 void *res = REAL(tsearch)(key, rootp, compar);
5748 if (res && *(void **)res == key)
5749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5752 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5754 #define INIT_TSEARCH
5757 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5758 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5759 void unpoison_file(__sanitizer_FILE *fp) {
5760 #if SANITIZER_HAS_STRUCT_FILE
5761 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5762 #if SANITIZER_NETBSD
5763 if (fp->_bf._base && fp->_bf._size > 0)
5764 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
5767 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5768 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5769 fp->_IO_read_end - fp->_IO_read_base);
5771 #endif // SANITIZER_HAS_STRUCT_FILE
5775 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5776 // These guys are called when a .c source is built with -O2.
5777 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5779 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5780 int res = REAL(__uflow)(fp);
5784 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5786 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5787 int res = REAL(__underflow)(fp);
5791 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5793 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5794 int res = REAL(__overflow)(fp, ch);
5798 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5800 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5801 int res = REAL(__wuflow)(fp);
5805 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5807 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5808 int res = REAL(__wunderflow)(fp);
5812 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5814 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5815 int res = REAL(__woverflow)(fp, ch);
5819 #define INIT_LIBIO_INTERNALS \
5820 COMMON_INTERCEPT_FUNCTION(__uflow); \
5821 COMMON_INTERCEPT_FUNCTION(__underflow); \
5822 COMMON_INTERCEPT_FUNCTION(__overflow); \
5823 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5824 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5825 COMMON_INTERCEPT_FUNCTION(__woverflow);
5827 #define INIT_LIBIO_INTERNALS
5830 #if SANITIZER_INTERCEPT_FOPEN
5831 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5833 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5834 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5835 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5836 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5837 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5838 if (res) unpoison_file(res);
5841 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5843 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5844 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5845 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5846 if (res) unpoison_file(res);
5849 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5850 __sanitizer_FILE *fp) {
5852 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5853 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5854 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5855 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5856 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5857 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5858 if (res) unpoison_file(res);
5861 #define INIT_FOPEN \
5862 COMMON_INTERCEPT_FUNCTION(fopen); \
5863 COMMON_INTERCEPT_FUNCTION(fdopen); \
5864 COMMON_INTERCEPT_FUNCTION(freopen);
5869 #if SANITIZER_INTERCEPT_FOPEN64
5870 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5872 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5873 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5874 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5875 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5876 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5877 if (res) unpoison_file(res);
5880 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5881 __sanitizer_FILE *fp) {
5883 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5884 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5885 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5886 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5887 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5888 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5889 if (res) unpoison_file(res);
5892 #define INIT_FOPEN64 \
5893 COMMON_INTERCEPT_FUNCTION(fopen64); \
5894 COMMON_INTERCEPT_FUNCTION(freopen64);
5896 #define INIT_FOPEN64
5899 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5900 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5902 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5903 // FIXME: under ASan the call below may write to freed memory and corrupt
5904 // its metadata. See
5905 // https://github.com/google/sanitizers/issues/321.
5906 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5911 FileMetadata file = {ptr, sizeloc};
5912 SetInterceptorMetadata(res, file);
5916 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5919 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5920 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5922 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5923 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5925 FileMetadata file = {(char **)ptr, sizeloc};
5926 SetInterceptorMetadata(res, file);
5930 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5933 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5934 // FIXME: under ASan the call below may write to freed memory and corrupt
5935 // its metadata. See
5936 // https://github.com/google/sanitizers/issues/321.
5937 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5938 if (res) unpoison_file(res);
5941 #define INIT_OPEN_MEMSTREAM \
5942 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5943 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5944 COMMON_INTERCEPT_FUNCTION(fmemopen);
5946 #define INIT_OPEN_MEMSTREAM
5949 #if SANITIZER_INTERCEPT_OBSTACK
5950 static void initialize_obstack(__sanitizer_obstack *obstack) {
5951 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5953 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5954 sizeof(*obstack->chunk));
5957 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5958 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5959 void (*free_fn)(uptr arg, void *p)) {
5961 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5963 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5964 if (res) initialize_obstack(obstack);
5967 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5968 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5970 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5972 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5973 if (res) initialize_obstack(obstack);
5976 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5978 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5979 REAL(_obstack_newchunk)(obstack, length);
5981 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5982 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5984 #define INIT_OBSTACK \
5985 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5986 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5987 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5989 #define INIT_OBSTACK
5992 #if SANITIZER_INTERCEPT_FFLUSH
5993 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5995 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5996 int res = REAL(fflush)(fp);
5997 // FIXME: handle fp == NULL
5999 const FileMetadata *m = GetInterceptorMetadata(fp);
6000 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6004 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6009 #if SANITIZER_INTERCEPT_FCLOSE
6010 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6012 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6013 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6014 const FileMetadata *m = GetInterceptorMetadata(fp);
6015 int res = REAL(fclose)(fp);
6017 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6018 DeleteInterceptorMetadata(fp);
6022 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6027 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6028 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6030 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6031 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6032 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
6033 void *res = REAL(dlopen)(filename, flag);
6034 Symbolizer::GetOrInit()->InvalidateModuleList();
6035 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6039 INTERCEPTOR(int, dlclose, void *handle) {
6041 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6042 int res = REAL(dlclose)(handle);
6043 Symbolizer::GetOrInit()->InvalidateModuleList();
6044 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6047 #define INIT_DLOPEN_DLCLOSE \
6048 COMMON_INTERCEPT_FUNCTION(dlopen); \
6049 COMMON_INTERCEPT_FUNCTION(dlclose);
6051 #define INIT_DLOPEN_DLCLOSE
6054 #if SANITIZER_INTERCEPT_GETPASS
6055 INTERCEPTOR(char *, getpass, const char *prompt) {
6057 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6059 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
6060 char *res = REAL(getpass)(prompt);
6061 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
6065 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6067 #define INIT_GETPASS
6070 #if SANITIZER_INTERCEPT_TIMERFD
6071 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6074 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6076 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6077 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6078 if (res != -1 && old_value)
6079 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6083 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6085 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6086 int res = REAL(timerfd_gettime)(fd, curr_value);
6087 if (res != -1 && curr_value)
6088 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6091 #define INIT_TIMERFD \
6092 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6093 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6095 #define INIT_TIMERFD
6098 #if SANITIZER_INTERCEPT_MLOCKX
6099 // Linux kernel has a bug that leads to kernel deadlock if a process
6100 // maps TBs of memory and then calls mlock().
6101 static void MlockIsUnsupported() {
6102 static atomic_uint8_t printed;
6103 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6105 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6109 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6110 MlockIsUnsupported();
6114 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6115 MlockIsUnsupported();
6119 INTERCEPTOR(int, mlockall, int flags) {
6120 MlockIsUnsupported();
6124 INTERCEPTOR(int, munlockall, void) {
6125 MlockIsUnsupported();
6129 #define INIT_MLOCKX \
6130 COMMON_INTERCEPT_FUNCTION(mlock); \
6131 COMMON_INTERCEPT_FUNCTION(munlock); \
6132 COMMON_INTERCEPT_FUNCTION(mlockall); \
6133 COMMON_INTERCEPT_FUNCTION(munlockall);
6137 #endif // SANITIZER_INTERCEPT_MLOCKX
6139 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6140 struct WrappedCookie {
6142 __sanitizer_cookie_io_functions_t real_io_funcs;
6145 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6146 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6147 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6148 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6149 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6152 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6153 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6154 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6155 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6156 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6159 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6160 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6161 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6162 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6163 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6164 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6168 static int wrapped_close(void *cookie) {
6169 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6170 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6171 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6172 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6173 InternalFree(wrapped_cookie);
6177 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6178 __sanitizer_cookie_io_functions_t io_funcs) {
6180 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6181 WrappedCookie *wrapped_cookie =
6182 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6183 wrapped_cookie->real_cookie = cookie;
6184 wrapped_cookie->real_io_funcs = io_funcs;
6185 __sanitizer_FILE *res =
6186 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6187 wrapped_seek, wrapped_close});
6191 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6193 #define INIT_FOPENCOOKIE
6194 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6196 #if SANITIZER_INTERCEPT_SEM
6197 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6199 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6200 // Workaround a bug in glibc's "old" semaphore implementation by
6201 // zero-initializing the sem_t contents. This has to be done here because
6202 // interceptors bind to the lowest symbols version by default, hitting the
6203 // buggy code path while the non-sanitized build of the same code works fine.
6204 REAL(memset)(s, 0, sizeof(*s));
6205 int res = REAL(sem_init)(s, pshared, value);
6209 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6211 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6212 int res = REAL(sem_destroy)(s);
6216 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6218 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6219 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6221 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6226 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6228 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6229 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6231 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6236 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6238 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6239 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6240 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6242 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6247 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6249 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6250 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6251 int res = REAL(sem_post)(s);
6255 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6257 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6258 int res = REAL(sem_getvalue)(s, sval);
6260 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6261 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6266 COMMON_INTERCEPT_FUNCTION(sem_init); \
6267 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6268 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6269 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6270 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6271 COMMON_INTERCEPT_FUNCTION(sem_post); \
6272 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6275 #endif // SANITIZER_INTERCEPT_SEM
6277 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6278 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6280 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6281 int res = REAL(pthread_setcancelstate)(state, oldstate);
6282 if (res == 0 && oldstate != nullptr)
6283 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6287 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6289 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6290 int res = REAL(pthread_setcanceltype)(type, oldtype);
6291 if (res == 0 && oldtype != nullptr)
6292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6295 #define INIT_PTHREAD_SETCANCEL \
6296 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6297 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6299 #define INIT_PTHREAD_SETCANCEL
6302 #if SANITIZER_INTERCEPT_MINCORE
6303 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6305 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6306 int res = REAL(mincore)(addr, length, vec);
6308 uptr page_size = GetPageSizeCached();
6309 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6310 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6314 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6316 #define INIT_MINCORE
6319 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6320 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6321 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6324 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6325 remote_iov, riovcnt, flags);
6326 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6329 write_iovec(ctx, local_iov, liovcnt, res);
6333 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6334 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6337 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6338 remote_iov, riovcnt, flags);
6339 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6342 read_iovec(ctx, local_iov, liovcnt, res);
6345 #define INIT_PROCESS_VM_READV \
6346 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6347 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6349 #define INIT_PROCESS_VM_READV
6352 #if SANITIZER_INTERCEPT_CTERMID
6353 INTERCEPTOR(char *, ctermid, char *s) {
6355 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6356 char *res = REAL(ctermid)(s);
6358 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6362 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6364 #define INIT_CTERMID
6367 #if SANITIZER_INTERCEPT_CTERMID_R
6368 INTERCEPTOR(char *, ctermid_r, char *s) {
6370 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6371 char *res = REAL(ctermid_r)(s);
6373 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6377 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6379 #define INIT_CTERMID_R
6382 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6383 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6385 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6386 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6387 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6389 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6391 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6395 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6396 void *srcaddr, int *addrlen) {
6398 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6400 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6402 if (srcaddr) srcaddr_sz = *addrlen;
6403 (void)srcaddr_sz; // prevent "set but not used" warning
6404 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6406 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6408 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6409 Min((SIZE_T)*addrlen, srcaddr_sz));
6413 #define INIT_RECV_RECVFROM \
6414 COMMON_INTERCEPT_FUNCTION(recv); \
6415 COMMON_INTERCEPT_FUNCTION(recvfrom);
6417 #define INIT_RECV_RECVFROM
6420 #if SANITIZER_INTERCEPT_SEND_SENDTO
6421 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6423 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6425 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6426 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6428 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6429 if (common_flags()->intercept_send && res > 0)
6430 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6434 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6435 void *dstaddr, int addrlen) {
6437 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6439 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6440 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6442 // Can't check dstaddr as it may have uninitialized padding at the end.
6443 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6444 if (common_flags()->intercept_send && res > 0)
6445 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6448 #define INIT_SEND_SENDTO \
6449 COMMON_INTERCEPT_FUNCTION(send); \
6450 COMMON_INTERCEPT_FUNCTION(sendto);
6452 #define INIT_SEND_SENDTO
6455 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6456 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6458 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6459 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6460 int res = REAL(eventfd_read)(fd, value);
6462 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6463 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6467 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6469 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6471 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6472 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6474 int res = REAL(eventfd_write)(fd, value);
6477 #define INIT_EVENTFD_READ_WRITE \
6478 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6479 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6481 #define INIT_EVENTFD_READ_WRITE
6484 #if SANITIZER_INTERCEPT_STAT
6485 INTERCEPTOR(int, stat, const char *path, void *buf) {
6487 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6488 if (common_flags()->intercept_stat)
6489 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6490 int res = REAL(stat)(path, buf);
6492 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6495 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6500 #if SANITIZER_INTERCEPT_LSTAT
6501 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6503 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6504 if (common_flags()->intercept_stat)
6505 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6506 int res = REAL(lstat)(path, buf);
6508 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6511 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6516 #if SANITIZER_INTERCEPT___XSTAT
6517 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6519 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6520 if (common_flags()->intercept_stat)
6521 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6522 int res = REAL(__xstat)(version, path, buf);
6524 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6527 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6529 #define INIT___XSTAT
6532 #if SANITIZER_INTERCEPT___XSTAT64
6533 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6535 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6536 if (common_flags()->intercept_stat)
6537 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6538 int res = REAL(__xstat64)(version, path, buf);
6540 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6543 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6545 #define INIT___XSTAT64
6548 #if SANITIZER_INTERCEPT___LXSTAT
6549 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6551 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6552 if (common_flags()->intercept_stat)
6553 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6554 int res = REAL(__lxstat)(version, path, buf);
6556 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6559 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6561 #define INIT___LXSTAT
6564 #if SANITIZER_INTERCEPT___LXSTAT64
6565 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6567 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6568 if (common_flags()->intercept_stat)
6569 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6570 int res = REAL(__lxstat64)(version, path, buf);
6572 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6575 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6577 #define INIT___LXSTAT64
6580 // FIXME: add other *stat interceptor
6582 #if SANITIZER_INTERCEPT_UTMP
6583 INTERCEPTOR(void *, getutent, int dummy) {
6585 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6586 void *res = REAL(getutent)(dummy);
6588 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6591 INTERCEPTOR(void *, getutid, void *ut) {
6593 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6594 void *res = REAL(getutid)(ut);
6596 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6599 INTERCEPTOR(void *, getutline, void *ut) {
6601 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6602 void *res = REAL(getutline)(ut);
6604 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6608 COMMON_INTERCEPT_FUNCTION(getutent); \
6609 COMMON_INTERCEPT_FUNCTION(getutid); \
6610 COMMON_INTERCEPT_FUNCTION(getutline);
6615 #if SANITIZER_INTERCEPT_UTMPX
6616 INTERCEPTOR(void *, getutxent, int dummy) {
6618 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6619 void *res = REAL(getutxent)(dummy);
6621 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6624 INTERCEPTOR(void *, getutxid, void *ut) {
6626 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6627 void *res = REAL(getutxid)(ut);
6629 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6632 INTERCEPTOR(void *, getutxline, void *ut) {
6634 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6635 void *res = REAL(getutxline)(ut);
6637 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6640 INTERCEPTOR(void *, pututxline, const void *ut) {
6642 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
6644 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
6645 void *res = REAL(pututxline)(ut);
6647 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
6650 #define INIT_UTMPX \
6651 COMMON_INTERCEPT_FUNCTION(getutxent); \
6652 COMMON_INTERCEPT_FUNCTION(getutxid); \
6653 COMMON_INTERCEPT_FUNCTION(getutxline); \
6654 COMMON_INTERCEPT_FUNCTION(pututxline);
6659 #if SANITIZER_INTERCEPT_GETLOADAVG
6660 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6662 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6663 int res = REAL(getloadavg)(loadavg, nelem);
6665 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6668 #define INIT_GETLOADAVG \
6669 COMMON_INTERCEPT_FUNCTION(getloadavg);
6671 #define INIT_GETLOADAVG
6674 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6675 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6679 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6683 INTERCEPTOR(int, mprobe, void *ptr) {
6688 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6690 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6691 SIZE_T res = REAL(wcslen)(s);
6692 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6696 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6698 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6699 SIZE_T res = REAL(wcsnlen)(s, n);
6700 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6703 #define INIT_WCSLEN \
6704 COMMON_INTERCEPT_FUNCTION(wcslen); \
6705 COMMON_INTERCEPT_FUNCTION(wcsnlen);
6707 #if SANITIZER_INTERCEPT_WCSCAT
6708 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6710 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6711 SIZE_T src_size = REAL(wcslen)(src);
6712 SIZE_T dst_size = REAL(wcslen)(dst);
6713 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6714 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6716 (src_size + 1) * sizeof(wchar_t));
6717 return REAL(wcscat)(dst, src); // NOLINT
6720 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6722 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6723 SIZE_T src_size = REAL(wcsnlen)(src, n);
6724 SIZE_T dst_size = REAL(wcslen)(dst);
6725 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6726 Min(src_size + 1, n) * sizeof(wchar_t));
6727 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6728 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6729 (src_size + 1) * sizeof(wchar_t));
6730 return REAL(wcsncat)(dst, src, n); // NOLINT
6732 #define INIT_WCSCAT \
6733 COMMON_INTERCEPT_FUNCTION(wcscat); \
6734 COMMON_INTERCEPT_FUNCTION(wcsncat);
6739 #if SANITIZER_INTERCEPT_WCSDUP
6740 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
6742 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
6743 SIZE_T len = REAL(wcslen)(s);
6744 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
6745 wchar_t *result = REAL(wcsdup)(s);
6747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
6751 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
6756 #if SANITIZER_INTERCEPT_STRXFRM
6757 static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6759 static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6761 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
6764 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6765 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
6766 sizeof(*src) * (RealStrLen(src) + 1)); \
6767 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
6769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6773 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6774 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6777 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6779 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6782 #define INIT_STRXFRM \
6783 COMMON_INTERCEPT_FUNCTION(strxfrm); \
6784 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6786 #define INIT_STRXFRM
6789 #if SANITIZER_INTERCEPT___STRXFRM_L
6790 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6792 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6795 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6797 #define INIT___STRXFRM_L
6800 #if SANITIZER_INTERCEPT_WCSXFRM
6801 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6802 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6805 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6807 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6810 #define INIT_WCSXFRM \
6811 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6812 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6814 #define INIT_WCSXFRM
6817 #if SANITIZER_INTERCEPT___WCSXFRM_L
6818 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6820 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6823 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6825 #define INIT___WCSXFRM_L
6828 #if SANITIZER_INTERCEPT_ACCT
6829 INTERCEPTOR(int, acct, const char *file) {
6831 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6833 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6834 return REAL(acct)(file);
6836 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6841 #if SANITIZER_INTERCEPT_USER_FROM_UID
6842 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6845 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6846 user = REAL(user_from_uid)(uid, nouser);
6848 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6851 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6853 #define INIT_USER_FROM_UID
6856 #if SANITIZER_INTERCEPT_UID_FROM_USER
6857 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6860 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6862 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6863 res = REAL(uid_from_user)(name, uid);
6865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6868 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6870 #define INIT_UID_FROM_USER
6873 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
6874 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6877 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6878 group = REAL(group_from_gid)(gid, nogroup);
6880 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6883 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6885 #define INIT_GROUP_FROM_GID
6888 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
6889 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6892 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6894 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6895 res = REAL(gid_from_group)(group, gid);
6897 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6900 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6902 #define INIT_GID_FROM_GROUP
6905 #if SANITIZER_INTERCEPT_ACCESS
6906 INTERCEPTOR(int, access, const char *path, int mode) {
6908 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6910 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6911 return REAL(access)(path, mode);
6913 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6918 #if SANITIZER_INTERCEPT_FACCESSAT
6919 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6921 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6923 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6924 return REAL(faccessat)(fd, path, mode, flags);
6926 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6928 #define INIT_FACCESSAT
6931 #if SANITIZER_INTERCEPT_GETGROUPLIST
6932 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6936 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6938 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6940 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6941 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6942 if (!res && groups && ngroups) {
6943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6949 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6951 #define INIT_GETGROUPLIST
6954 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6955 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6956 int maxgrp, int *ngroups) {
6959 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6962 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6963 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6964 if (!res && groups && ngroups) {
6965 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6971 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6973 #define INIT_GETGROUPMEMBERSHIP
6976 #if SANITIZER_INTERCEPT_READLINK
6977 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6979 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6980 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6981 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6987 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6989 #define INIT_READLINK
6992 #if SANITIZER_INTERCEPT_READLINKAT
6993 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
6996 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
6997 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6998 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7004 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7006 #define INIT_READLINKAT
7009 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7010 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7011 struct file_handle *handle, int *mount_id, int flags) {
7013 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7015 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
7017 __sanitizer_file_handle *sanitizer_handle =
7018 reinterpret_cast<__sanitizer_file_handle*>(handle);
7019 COMMON_INTERCEPTOR_READ_RANGE(
7020 ctx, &sanitizer_handle->handle_bytes,
7021 sizeof(sanitizer_handle->handle_bytes));
7023 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7025 COMMON_INTERCEPTOR_WRITE_RANGE(
7026 ctx, &sanitizer_handle->handle_bytes,
7027 sizeof(sanitizer_handle->handle_bytes));
7028 COMMON_INTERCEPTOR_WRITE_RANGE(
7029 ctx, &sanitizer_handle->handle_type,
7030 sizeof(sanitizer_handle->handle_type));
7031 COMMON_INTERCEPTOR_WRITE_RANGE(
7032 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7033 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7038 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7040 #define INIT_NAME_TO_HANDLE_AT
7043 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7044 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7047 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7049 __sanitizer_file_handle *sanitizer_handle =
7050 reinterpret_cast<__sanitizer_file_handle*>(handle);
7051 COMMON_INTERCEPTOR_READ_RANGE(
7052 ctx, &sanitizer_handle->handle_bytes,
7053 sizeof(sanitizer_handle->handle_bytes));
7054 COMMON_INTERCEPTOR_READ_RANGE(
7055 ctx, &sanitizer_handle->handle_type,
7056 sizeof(sanitizer_handle->handle_type));
7057 COMMON_INTERCEPTOR_READ_RANGE(
7058 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7060 return REAL(open_by_handle_at)(mount_fd, handle, flags);
7063 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7065 #define INIT_OPEN_BY_HANDLE_AT
7068 #if SANITIZER_INTERCEPT_STRLCPY
7069 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7072 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7074 // Keep strnlen as macro argument, as macro may ignore it.
7075 COMMON_INTERCEPTOR_READ_STRING(
7076 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7078 res = REAL(strlcpy)(dst, src, size);
7079 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
7083 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7086 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7087 // src is checked in the strlcpy() interceptor
7089 len = internal_strnlen(dst, size);
7090 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7092 // Reuse the rest of the code in the strlcpy() interceptor
7093 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7095 #define INIT_STRLCPY \
7096 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7097 COMMON_INTERCEPT_FUNCTION(strlcat);
7099 #define INIT_STRLCPY
7102 #if SANITIZER_INTERCEPT_MMAP
7103 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7106 if (common_flags()->detect_write_exec)
7107 ReportMmapWriteExec(prot);
7108 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7109 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7110 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7111 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7114 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7116 if (common_flags()->detect_write_exec)
7117 ReportMmapWriteExec(prot);
7118 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7119 return (int)internal_mprotect(addr, sz, prot);
7120 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7121 MprotectMallocZones(addr, prot);
7122 return REAL(mprotect)(addr, sz, prot);
7125 COMMON_INTERCEPT_FUNCTION(mmap); \
7126 COMMON_INTERCEPT_FUNCTION(mprotect);
7131 #if SANITIZER_INTERCEPT_MMAP64
7132 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7135 if (common_flags()->detect_write_exec)
7136 ReportMmapWriteExec(prot);
7137 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7138 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7139 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7140 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7142 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7147 #if SANITIZER_INTERCEPT_DEVNAME
7148 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7151 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7152 name = REAL(devname)(dev, type);
7154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7157 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7159 #define INIT_DEVNAME
7162 #if SANITIZER_INTERCEPT_DEVNAME_R
7163 #if SANITIZER_NETBSD
7164 #define DEVNAME_R_RETTYPE int
7165 #define DEVNAME_R_SUCCESS(x) (!(x))
7167 #define DEVNAME_R_RETTYPE char*
7168 #define DEVNAME_R_SUCCESS(x) (x)
7170 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7173 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7174 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7175 if (DEVNAME_R_SUCCESS(res))
7176 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7179 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7181 #define INIT_DEVNAME_R
7184 #if SANITIZER_INTERCEPT_FGETLN
7185 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7187 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7188 char *str = REAL(fgetln)(stream, len);
7190 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7195 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7200 #if SANITIZER_INTERCEPT_STRMODE
7201 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7203 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7204 REAL(strmode)(mode, bp);
7206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7208 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7210 #define INIT_STRMODE
7213 #if SANITIZER_INTERCEPT_TTYENT
7214 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7216 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7217 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7222 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7224 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7226 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7227 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7229 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7232 INTERCEPTOR(int, setttyentpath, char *path) {
7234 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7236 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7237 return REAL(setttyentpath)(path);
7239 #define INIT_TTYENT \
7240 COMMON_INTERCEPT_FUNCTION(getttyent); \
7241 COMMON_INTERCEPT_FUNCTION(getttynam); \
7242 COMMON_INTERCEPT_FUNCTION(setttyentpath)
7247 #if SANITIZER_INTERCEPT_PROTOENT
7248 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7250 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7251 struct __sanitizer_protoent *p = REAL(getprotoent)();
7253 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7255 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7257 SIZE_T pp_size = 1; // One handles the trailing \0
7259 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7262 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7263 pp_size * sizeof(char **));
7268 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7270 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7272 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7273 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7275 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7277 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7279 SIZE_T pp_size = 1; // One handles the trailing \0
7281 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7285 pp_size * sizeof(char **));
7290 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7292 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7293 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7295 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7297 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7299 SIZE_T pp_size = 1; // One handles the trailing \0
7301 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7305 pp_size * sizeof(char **));
7309 #define INIT_PROTOENT \
7310 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7311 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7312 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7314 #define INIT_PROTOENT
7317 #if SANITIZER_INTERCEPT_NETENT
7318 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7320 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7321 struct __sanitizer_netent *n = REAL(getnetent)();
7323 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7327 SIZE_T nn_size = 1; // One handles the trailing \0
7329 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7330 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7333 nn_size * sizeof(char **));
7338 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7340 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7342 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7343 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7349 SIZE_T nn_size = 1; // One handles the trailing \0
7351 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7352 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7355 nn_size * sizeof(char **));
7360 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7362 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7363 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7365 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7367 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7369 SIZE_T nn_size = 1; // One handles the trailing \0
7371 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7372 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7375 nn_size * sizeof(char **));
7379 #define INIT_NETENT \
7380 COMMON_INTERCEPT_FUNCTION(getnetent); \
7381 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7382 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7387 #if SANITIZER_INTERCEPT_GETMNTINFO
7388 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7390 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7391 int cnt = REAL(getmntinfo)(mntbufp, flags);
7392 if (cnt > 0 && mntbufp) {
7393 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7395 #if SANITIZER_NETBSD
7396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7403 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7405 #define INIT_GETMNTINFO
7408 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7409 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7412 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7414 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7415 REAL(mi_vector_hash)(key, len, seed, hashes);
7417 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7419 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7421 #define INIT_MI_VECTOR_HASH
7424 #if SANITIZER_INTERCEPT_SETVBUF
7425 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7428 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7429 int ret = REAL(setvbuf)(stream, buf, mode, size);
7431 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7433 unpoison_file(stream);
7437 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7439 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7440 REAL(setbuf)(stream, buf);
7442 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7445 unpoison_file(stream);
7448 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
7450 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
7451 REAL(setbuffer)(stream, buf, mode);
7453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7456 unpoison_file(stream);
7459 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7461 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7462 REAL(setlinebuf)(stream);
7464 unpoison_file(stream);
7466 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7467 COMMON_INTERCEPT_FUNCTION(setbuf); \
7468 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7469 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7471 #define INIT_SETVBUF
7474 #if SANITIZER_INTERCEPT_GETVFSSTAT
7475 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7477 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7478 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7480 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7483 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7485 #define INIT_GETVFSSTAT
7488 #if SANITIZER_INTERCEPT_REGEX
7489 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7491 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7493 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, REAL(strlen)(pattern) + 1);
7494 int res = REAL(regcomp)(preg, pattern, cflags);
7496 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7499 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7500 struct __sanitizer_regmatch *pmatch[], int eflags) {
7502 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7504 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7506 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, REAL(strlen)(string) + 1);
7507 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7509 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7512 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7513 SIZE_T errbuf_size) {
7515 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7517 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7518 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7520 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, REAL(strlen)(errbuf) + 1);
7523 INTERCEPTOR(void, regfree, const void *preg) {
7525 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7527 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7528 REAL(regfree)(preg);
7530 #define INIT_REGEX \
7531 COMMON_INTERCEPT_FUNCTION(regcomp); \
7532 COMMON_INTERCEPT_FUNCTION(regexec); \
7533 COMMON_INTERCEPT_FUNCTION(regerror); \
7534 COMMON_INTERCEPT_FUNCTION(regfree);
7539 #if SANITIZER_INTERCEPT_REGEXSUB
7540 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7541 const struct __sanitizer_regmatch *rm, const char *str) {
7543 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7545 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7546 // The implementation demands and hardcodes 10 elements
7548 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7550 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
7551 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7553 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
7556 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7557 const struct __sanitizer_regmatch *rm, const char *sstr) {
7559 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7561 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7562 // Hardcode 10 elements as this is hardcoded size
7564 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7566 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, REAL(strlen)(sstr) + 1);
7567 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7568 if (res > 0 && buf) {
7569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, REAL(strlen)(*buf) + 1);
7575 #define INIT_REGEXSUB \
7576 COMMON_INTERCEPT_FUNCTION(regnsub); \
7577 COMMON_INTERCEPT_FUNCTION(regasub);
7579 #define INIT_REGEXSUB
7582 #if SANITIZER_INTERCEPT_FTS
7583 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7584 int (*compar)(void **, void **)) {
7586 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
7588 for (char *const *pa = path_argv; ; ++pa) {
7589 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
7592 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
7595 // TODO(kamil): handle compar callback
7596 void *fts = REAL(fts_open)(path_argv, options, compar);
7598 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
7602 INTERCEPTOR(void *, fts_read, void *ftsp) {
7604 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
7606 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7607 void *ftsent = REAL(fts_read)(ftsp);
7609 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7613 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
7615 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
7617 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7618 void *ftsent = REAL(fts_children)(ftsp, options);
7620 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7624 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
7626 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
7628 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7630 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
7631 return REAL(fts_set)(ftsp, f, options);
7634 INTERCEPTOR(int, fts_close, void *ftsp) {
7636 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
7638 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7639 return REAL(fts_close)(ftsp);
7642 COMMON_INTERCEPT_FUNCTION(fts_open); \
7643 COMMON_INTERCEPT_FUNCTION(fts_read); \
7644 COMMON_INTERCEPT_FUNCTION(fts_children); \
7645 COMMON_INTERCEPT_FUNCTION(fts_set); \
7646 COMMON_INTERCEPT_FUNCTION(fts_close);
7651 #if SANITIZER_INTERCEPT_SYSCTL
7652 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
7653 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
7655 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7656 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
7657 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
7660 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
7662 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7664 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7665 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
7668 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7670 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7676 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
7677 void *newp, SIZE_T newlen) {
7679 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7680 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
7681 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
7684 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7686 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7688 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7689 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
7692 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7694 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7700 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
7703 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
7705 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7707 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7708 int res = REAL(sysctlnametomib)(sname, name, namelenp);
7711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7713 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7719 #define INIT_SYSCTL \
7720 COMMON_INTERCEPT_FUNCTION(sysctl); \
7721 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
7722 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
7727 #if SANITIZER_INTERCEPT_ASYSCTL
7728 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
7730 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
7732 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
7733 void *res = REAL(asysctl)(name, namelen, len);
7735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7736 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7741 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
7743 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
7745 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7746 void *res = REAL(asysctlbyname)(sname, len);
7748 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7753 #define INIT_ASYSCTL \
7754 COMMON_INTERCEPT_FUNCTION(asysctl); \
7755 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
7757 #define INIT_ASYSCTL
7760 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
7761 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
7762 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
7765 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
7768 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7770 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7772 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
7773 // Skip rnode, it's rarely used and not trivial to sanitize
7774 // It's also used mostly internally
7775 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
7778 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7783 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
7785 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
7790 #define INIT_SYSCTLGETMIBINFO \
7791 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
7793 #define INIT_SYSCTLGETMIBINFO
7796 #if SANITIZER_INTERCEPT_NL_LANGINFO
7797 INTERCEPTOR(char *, nl_langinfo, long item) {
7799 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
7800 char *ret = REAL(nl_langinfo)(item);
7802 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7805 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
7807 #define INIT_NL_LANGINFO
7810 #if SANITIZER_INTERCEPT_MODCTL
7811 INTERCEPTOR(int, modctl, int operation, void *argp) {
7814 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
7816 if (operation == modctl_load) {
7818 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
7819 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
7820 if (ml->ml_filename)
7821 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
7822 REAL(strlen)(ml->ml_filename) + 1);
7824 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
7826 ret = REAL(modctl)(operation, argp);
7827 } else if (operation == modctl_unload) {
7829 const char *name = (const char *)argp;
7830 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7832 ret = REAL(modctl)(operation, argp);
7833 } else if (operation == modctl_stat) {
7835 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
7837 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
7838 iov_len = iov->iov_len;
7840 ret = REAL(modctl)(operation, argp);
7842 COMMON_INTERCEPTOR_WRITE_RANGE(
7843 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
7844 } else if (operation == modctl_exists)
7845 ret = REAL(modctl)(operation, argp);
7847 ret = REAL(modctl)(operation, argp);
7851 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
7856 #if SANITIZER_INTERCEPT_STRTONUM
7857 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
7858 long long maxval, const char **errstr) {
7860 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
7862 // TODO(kamil): Implement strtoll as a common inteceptor
7864 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
7865 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
7867 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
7869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
7871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1);
7875 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
7877 #define INIT_STRTONUM
7880 #if SANITIZER_INTERCEPT_FPARSELN
7881 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
7882 SIZE_T *lineno, const char delim[3], int flags) {
7884 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
7886 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
7888 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
7889 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
7891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7893 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7895 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
7899 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
7901 #define INIT_FPARSELN
7904 #if SANITIZER_INTERCEPT_STATVFS1
7905 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
7907 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
7908 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7909 int res = REAL(statvfs1)(path, buf, flags);
7910 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7913 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
7915 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
7916 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7917 int res = REAL(fstatvfs1)(fd, buf, flags);
7919 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7921 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
7925 #define INIT_STATVFS1 \
7926 COMMON_INTERCEPT_FUNCTION(statvfs1); \
7927 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
7929 #define INIT_STATVFS1
7932 #if SANITIZER_INTERCEPT_STRTOI
7933 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
7934 INTMAX_T low, INTMAX_T high, int *rstatus) {
7936 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
7938 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
7939 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7945 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
7946 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
7948 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
7950 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
7951 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7956 #define INIT_STRTOI \
7957 COMMON_INTERCEPT_FUNCTION(strtoi); \
7958 COMMON_INTERCEPT_FUNCTION(strtou)
7963 #if SANITIZER_INTERCEPT_CAPSICUM
7964 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
7967 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
7969 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7970 __sanitizer_cap_rights_t *ret = \
7971 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
7973 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7977 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
7980 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
7982 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7983 __sanitizer_cap_rights_t *ret = \
7984 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
7986 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7990 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
7993 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
7995 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7996 __sanitizer_cap_rights_t *ret = \
7997 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
7999 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8003 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8006 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8008 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8009 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8012 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8013 __sanitizer_cap_rights_t *rights) {
8014 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8017 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8018 __sanitizer_cap_rights_t *rights) {
8019 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8022 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8023 __sanitizer_cap_rights_t *rights) {
8024 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8027 INTERCEPTOR(bool, cap_rights_is_set,
8028 __sanitizer_cap_rights_t *rights) {
8029 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8032 INTERCEPTOR(int, cap_rights_limit, int fd,
8033 const __sanitizer_cap_rights_t *rights) {
8035 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8037 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8039 return REAL(cap_rights_limit)(fd, rights);
8042 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8044 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8045 int ret = REAL(cap_rights_get)(fd, rights);
8047 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8052 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8054 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8056 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8058 return REAL(cap_rights_is_valid(rights));
8061 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8062 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8064 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8066 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8068 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8075 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8076 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8078 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8080 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8082 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8084 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8089 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8090 const __sanitizer_cap_rights *little) {
8092 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8094 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8096 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8098 return REAL(cap_rights_contains)(big, little);
8101 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8103 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8105 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8107 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8110 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8112 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8113 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8115 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8119 #define INIT_CAPSICUM \
8120 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8121 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8122 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8123 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8124 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8125 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8126 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8127 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8128 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8129 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8130 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8131 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8133 #define INIT_CAPSICUM
8136 #if SANITIZER_INTERCEPT_SHA1
8137 INTERCEPTOR(void, SHA1Init, void *context) {
8139 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8140 REAL(SHA1Init)(context);
8142 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8144 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8146 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8147 if (data && len > 0)
8148 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8150 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8151 REAL(SHA1Update)(context, data, len);
8153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8155 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8157 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8159 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8160 REAL(SHA1Final)(digest, context);
8162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8164 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8166 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8168 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8170 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8171 REAL(SHA1Transform)(state, buffer);
8173 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8175 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8177 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8179 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8180 char *ret = REAL(SHA1End)(context, buf);
8182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8185 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8187 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8189 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8190 char *ret = REAL(SHA1File)(filename, buf);
8192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8195 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8198 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8200 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8201 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8203 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8206 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8208 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8210 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8211 char *ret = REAL(SHA1Data)(data, len, buf);
8213 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8217 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8218 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8219 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8220 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8221 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8222 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8223 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8224 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8229 #if SANITIZER_INTERCEPT_MD4
8230 INTERCEPTOR(void, MD4Init, void *context) {
8232 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8233 REAL(MD4Init)(context);
8235 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8238 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8241 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8242 if (data && len > 0)
8243 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8245 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8246 REAL(MD4Update)(context, data, len);
8248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8251 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8253 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8255 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8256 REAL(MD4Final)(digest, context);
8258 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8261 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8263 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8265 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8266 char *ret = REAL(MD4End)(context, buf);
8268 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8272 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8274 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8276 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8277 char *ret = REAL(MD4File)(filename, buf);
8279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8283 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8286 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8287 if (data && len > 0)
8288 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8289 char *ret = REAL(MD4Data)(data, len, buf);
8291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8296 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8297 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8298 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8299 COMMON_INTERCEPT_FUNCTION(MD4End); \
8300 COMMON_INTERCEPT_FUNCTION(MD4File); \
8301 COMMON_INTERCEPT_FUNCTION(MD4Data)
8306 #if SANITIZER_INTERCEPT_RMD160
8307 INTERCEPTOR(void, RMD160Init, void *context) {
8309 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8310 REAL(RMD160Init)(context);
8312 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8314 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8316 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8317 if (data && len > 0)
8318 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8320 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8321 REAL(RMD160Update)(context, data, len);
8323 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8325 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8327 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8329 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8330 REAL(RMD160Final)(digest, context);
8332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8334 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8336 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8338 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8340 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8341 REAL(RMD160Transform)(state, buffer);
8343 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8345 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8347 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8349 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8350 char *ret = REAL(RMD160End)(context, buf);
8352 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8355 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8357 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8359 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8360 char *ret = REAL(RMD160File)(filename, buf);
8362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8365 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8368 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8370 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8371 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8373 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8376 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8378 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8379 if (data && len > 0)
8380 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8381 char *ret = REAL(RMD160Data)(data, len, buf);
8383 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8386 #define INIT_RMD160 \
8387 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8388 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8389 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8390 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8391 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8392 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8393 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8394 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8399 #if SANITIZER_INTERCEPT_MD5
8400 INTERCEPTOR(void, MD5Init, void *context) {
8402 COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8403 REAL(MD5Init)(context);
8405 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8408 INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8411 COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8412 if (data && len > 0)
8413 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8415 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8416 REAL(MD5Update)(context, data, len);
8418 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8421 INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8423 COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8425 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8426 REAL(MD5Final)(digest, context);
8428 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8431 INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8433 COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8435 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8436 char *ret = REAL(MD5End)(context, buf);
8438 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8442 INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8444 COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8446 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8447 char *ret = REAL(MD5File)(filename, buf);
8449 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8453 INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8456 COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8457 if (data && len > 0)
8458 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8459 char *ret = REAL(MD5Data)(data, len, buf);
8461 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8466 COMMON_INTERCEPT_FUNCTION(MD5Init); \
8467 COMMON_INTERCEPT_FUNCTION(MD5Update); \
8468 COMMON_INTERCEPT_FUNCTION(MD5Final); \
8469 COMMON_INTERCEPT_FUNCTION(MD5End); \
8470 COMMON_INTERCEPT_FUNCTION(MD5File); \
8471 COMMON_INTERCEPT_FUNCTION(MD5Data)
8476 #if SANITIZER_INTERCEPT_FSEEK
8477 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8479 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8480 return REAL(fseek)(stream, offset, whence);
8482 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8484 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8485 return REAL(fseeko)(stream, offset, whence);
8487 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8489 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8490 return REAL(ftell)(stream);
8492 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8494 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8495 return REAL(ftello)(stream);
8497 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8499 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8500 return REAL(rewind)(stream);
8502 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8504 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8505 int ret = REAL(fgetpos)(stream, pos);
8507 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8510 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8512 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8514 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8515 return REAL(fsetpos)(stream, pos);
8517 #define INIT_FSEEK \
8518 COMMON_INTERCEPT_FUNCTION(fseek); \
8519 COMMON_INTERCEPT_FUNCTION(fseeko); \
8520 COMMON_INTERCEPT_FUNCTION(ftell); \
8521 COMMON_INTERCEPT_FUNCTION(ftello); \
8522 COMMON_INTERCEPT_FUNCTION(rewind); \
8523 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8524 COMMON_INTERCEPT_FUNCTION(fsetpos)
8529 #if SANITIZER_INTERCEPT_MD2
8530 INTERCEPTOR(void, MD2Init, void *context) {
8532 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8533 REAL(MD2Init)(context);
8535 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8538 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8541 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8542 if (data && len > 0)
8543 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8545 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8546 REAL(MD2Update)(context, data, len);
8548 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8551 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8553 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8555 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8556 REAL(MD2Final)(digest, context);
8558 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8561 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8563 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8565 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8566 char *ret = REAL(MD2End)(context, buf);
8568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8572 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8574 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8576 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8577 char *ret = REAL(MD2File)(filename, buf);
8579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8583 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8586 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8587 if (data && len > 0)
8588 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8589 char *ret = REAL(MD2Data)(data, len, buf);
8591 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8596 COMMON_INTERCEPT_FUNCTION(MD2Init); \
8597 COMMON_INTERCEPT_FUNCTION(MD2Update); \
8598 COMMON_INTERCEPT_FUNCTION(MD2Final); \
8599 COMMON_INTERCEPT_FUNCTION(MD2End); \
8600 COMMON_INTERCEPT_FUNCTION(MD2File); \
8601 COMMON_INTERCEPT_FUNCTION(MD2Data)
8606 #if SANITIZER_INTERCEPT_SHA2
8607 #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
8608 INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
8610 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
8611 REAL(SHA##LEN##_Init)(context); \
8613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8615 INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
8616 const u8 *data, SIZE_T len) { \
8618 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
8619 if (data && len > 0) \
8620 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8622 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8623 REAL(SHA##LEN##_Update)(context, data, len); \
8625 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8627 INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
8630 CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
8631 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
8633 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8634 REAL(SHA##LEN##_Final)(digest, context); \
8636 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
8637 sizeof(digest[0]) * \
8638 SHA##LEN##_digest_length); \
8640 INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
8642 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
8644 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8645 char *ret = REAL(SHA##LEN##_End)(context, buf); \
8647 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8650 INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
8652 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
8654 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8655 char *ret = REAL(SHA##LEN##_File)(filename, buf); \
8657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8660 INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
8661 OFF_T offset, OFF_T length) { \
8663 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
8666 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8667 char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
8669 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8672 INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
8674 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
8675 if (data && len > 0) \
8676 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8677 char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
8679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8683 SHA2_INTERCEPTORS(224, u32);
8684 SHA2_INTERCEPTORS(256, u32);
8685 SHA2_INTERCEPTORS(384, u64);
8686 SHA2_INTERCEPTORS(512, u64);
8688 #define INIT_SHA2_INTECEPTORS(LEN) \
8689 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
8690 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
8691 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
8692 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
8693 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
8694 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
8695 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
8698 INIT_SHA2_INTECEPTORS(224); \
8699 INIT_SHA2_INTECEPTORS(256); \
8700 INIT_SHA2_INTECEPTORS(384); \
8701 INIT_SHA2_INTECEPTORS(512)
8702 #undef SHA2_INTERCEPTORS
8707 #if SANITIZER_INTERCEPT_VIS
8708 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
8710 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
8711 char *end = REAL(vis)(dst, c, flag, nextc);
8712 // dst is NULL terminated and end points to the NULL char
8714 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8717 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
8719 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
8720 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
8721 // nvis cannot make sure the dst is NULL terminated
8723 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8726 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
8728 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
8730 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8731 int len = REAL(strvis)(dst, src, flag);
8733 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8736 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
8738 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
8740 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8741 int len = REAL(stravis)(dst, src, flag);
8743 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
8745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
8749 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
8751 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
8753 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8754 int len = REAL(strnvis)(dst, dlen, src, flag);
8755 // The interface will be valid even if there is no space for NULL char
8757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8760 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
8762 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
8764 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8765 int ret = REAL(strvisx)(dst, src, len, flag);
8767 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8770 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8773 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
8775 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8776 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
8777 if (dst && ret >= 0)
8778 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8781 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8782 int flag, int *cerr_ptr) {
8784 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
8786 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8787 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8788 // according to the implementation
8790 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8791 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
8792 if (dst && ret >= 0)
8793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8798 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
8799 const char *extra) {
8801 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
8803 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8804 char *end = REAL(svis)(dst, c, flag, nextc, extra);
8806 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8809 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
8810 const char *extra) {
8812 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
8814 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8815 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
8817 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
8818 Min((SIZE_T)(end - dst + 1), dlen));
8821 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
8822 const char *extra) {
8824 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
8826 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8828 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8829 int len = REAL(strsvis)(dst, src, flag, extra);
8831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8834 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
8835 const char *extra) {
8837 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
8839 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8841 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8842 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
8843 // The interface will be valid even if there is no space for NULL char
8844 if (dst && len >= 0)
8845 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8848 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
8849 const char *extra) {
8851 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
8853 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8855 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8856 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
8858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8861 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8862 int flag, const char *extra) {
8864 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
8866 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8868 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8869 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
8870 if (dst && ret >= 0)
8871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8874 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
8875 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
8877 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
8880 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8882 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8883 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8884 // according to the implementation
8886 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8887 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
8888 if (dst && ret >= 0)
8889 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8894 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
8896 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
8898 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
8899 int ret = REAL(unvis)(cp, c, astate, flag);
8900 if (ret == unvis_valid || ret == unvis_validpush) {
8901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
8905 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
8907 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
8909 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8910 int ret = REAL(strunvis)(dst, src);
8912 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8915 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
8917 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
8919 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8920 int ret = REAL(strnunvis)(dst, dlen, src);
8922 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8925 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
8927 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
8929 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8930 int ret = REAL(strunvisx)(dst, src, flag);
8932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8935 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
8938 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
8940 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8941 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
8943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8947 COMMON_INTERCEPT_FUNCTION(vis); \
8948 COMMON_INTERCEPT_FUNCTION(nvis); \
8949 COMMON_INTERCEPT_FUNCTION(strvis); \
8950 COMMON_INTERCEPT_FUNCTION(stravis); \
8951 COMMON_INTERCEPT_FUNCTION(strnvis); \
8952 COMMON_INTERCEPT_FUNCTION(strvisx); \
8953 COMMON_INTERCEPT_FUNCTION(strnvisx); \
8954 COMMON_INTERCEPT_FUNCTION(strenvisx); \
8955 COMMON_INTERCEPT_FUNCTION(svis); \
8956 COMMON_INTERCEPT_FUNCTION(snvis); \
8957 COMMON_INTERCEPT_FUNCTION(strsvis); \
8958 COMMON_INTERCEPT_FUNCTION(strsnvis); \
8959 COMMON_INTERCEPT_FUNCTION(strsvisx); \
8960 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
8961 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
8962 COMMON_INTERCEPT_FUNCTION(unvis); \
8963 COMMON_INTERCEPT_FUNCTION(strunvis); \
8964 COMMON_INTERCEPT_FUNCTION(strnunvis); \
8965 COMMON_INTERCEPT_FUNCTION(strunvisx); \
8966 COMMON_INTERCEPT_FUNCTION(strnunvisx)
8971 #if SANITIZER_INTERCEPT_CDB
8972 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
8974 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
8976 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
8977 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
8979 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8983 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
8984 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
8986 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
8989 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
8990 struct __sanitizer_cdbr *cdbr =
8991 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
8993 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8997 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
8999 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9001 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9002 return REAL(cdbr_entries)(cdbr);
9005 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9006 const void **data, SIZE_T *datalen) {
9008 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9010 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9011 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9014 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9016 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9017 if (data && datalen)
9018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9023 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9024 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9026 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9028 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9030 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9031 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9037 if (data && datalen)
9038 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9043 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9045 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9047 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9048 REAL(cdbr_close)(cdbr);
9051 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9053 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9054 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9056 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9060 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9061 SIZE_T keylen, const void *data, SIZE_T datalen) {
9063 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9065 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9066 if (data && datalen)
9067 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9069 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9070 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9072 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9076 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9077 SIZE_T datalen, u32 *index) {
9079 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9081 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9082 if (data && datalen)
9083 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9084 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9087 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9089 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9094 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9095 SIZE_T keylen, u32 index) {
9097 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9099 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9101 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9102 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9104 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9108 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9109 const char descr[16], u32 (*seedgen)(void)) {
9111 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9112 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9114 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9116 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9118 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9119 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9122 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9124 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9129 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9131 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9133 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9134 REAL(cdbw_close)(cdbw);
9138 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9139 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9140 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9141 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9142 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9143 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9144 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9145 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9146 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9147 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9148 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9149 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9154 #if SANITIZER_INTERCEPT_GETFSENT
9155 INTERCEPTOR(void *, getfsent) {
9157 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9158 void *ret = REAL(getfsent)();
9160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9164 INTERCEPTOR(void *, getfsspec, const char *spec) {
9166 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9168 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, REAL(strlen)(spec) + 1);
9169 void *ret = REAL(getfsspec)(spec);
9171 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9175 INTERCEPTOR(void *, getfsfile, const char *file) {
9177 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9179 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
9180 void *ret = REAL(getfsfile)(file);
9182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9186 #define INIT_GETFSENT \
9187 COMMON_INTERCEPT_FUNCTION(getfsent); \
9188 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9189 COMMON_INTERCEPT_FUNCTION(getfsfile);
9191 #define INIT_GETFSENT
9194 #if SANITIZER_INTERCEPT_ARC4RANDOM
9195 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9197 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9198 REAL(arc4random_buf)(buf, len);
9200 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9203 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9205 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9207 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9208 REAL(arc4random_addrandom)(dat, datlen);
9211 #define INIT_ARC4RANDOM \
9212 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9213 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9215 #define INIT_ARC4RANDOM
9218 #if SANITIZER_INTERCEPT_POPEN
9219 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9221 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9223 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, REAL(strlen)(command) + 1);
9225 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9226 __sanitizer_FILE *res = REAL(popen)(command, type);
9227 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9228 if (res) unpoison_file(res);
9231 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9236 #if SANITIZER_INTERCEPT_POPENVE
9237 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9238 char *const *argv, char *const *envp, const char *type) {
9240 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9242 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
9244 for (char *const *pa = argv; ; ++pa) {
9245 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9248 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9252 for (char *const *pa = envp; ; ++pa) {
9253 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9256 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9260 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9261 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9262 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9263 if (res) unpoison_file(res);
9266 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9268 #define INIT_POPENVE
9271 #if SANITIZER_INTERCEPT_PCLOSE
9272 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9274 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9275 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9276 const FileMetadata *m = GetInterceptorMetadata(fp);
9277 int res = REAL(pclose)(fp);
9279 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9280 DeleteInterceptorMetadata(fp);
9284 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9289 #if SANITIZER_INTERCEPT_FUNOPEN
9290 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9291 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9292 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9293 typedef int (*funopen_closefn)(void *cookie);
9295 struct WrappedFunopenCookie {
9297 funopen_readfn real_read;
9298 funopen_writefn real_write;
9299 funopen_seekfn real_seek;
9300 funopen_closefn real_close;
9303 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9304 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9305 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9306 funopen_readfn real_read = wrapped_cookie->real_read;
9307 return real_read(wrapped_cookie->real_cookie, buf, len);
9310 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9311 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9312 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9313 funopen_writefn real_write = wrapped_cookie->real_write;
9314 return real_write(wrapped_cookie->real_cookie, buf, len);
9317 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9318 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9319 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9320 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9321 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9324 static int wrapped_funopen_close(void *cookie) {
9325 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9326 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9327 funopen_closefn real_close = wrapped_cookie->real_close;
9328 int res = real_close(wrapped_cookie->real_cookie);
9329 InternalFree(wrapped_cookie);
9333 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9334 funopen_writefn writefn, funopen_seekfn seekfn,
9335 funopen_closefn closefn) {
9337 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9340 WrappedFunopenCookie *wrapped_cookie =
9341 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9342 wrapped_cookie->real_cookie = cookie;
9343 wrapped_cookie->real_read = readfn;
9344 wrapped_cookie->real_write = writefn;
9345 wrapped_cookie->real_seek = seekfn;
9346 wrapped_cookie->real_close = closefn;
9348 __sanitizer_FILE *res =
9349 REAL(funopen)(wrapped_cookie,
9350 readfn ? wrapped_funopen_read : nullptr,
9351 writefn ? wrapped_funopen_write : nullptr,
9352 seekfn ? wrapped_funopen_seek : nullptr,
9353 closefn ? wrapped_funopen_close : nullptr);
9358 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9360 #define INIT_FUNOPEN
9363 #if SANITIZER_INTERCEPT_FUNOPEN2
9364 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9365 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9366 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9367 typedef int (*funopen2_flushfn)(void *cookie);
9368 typedef int (*funopen2_closefn)(void *cookie);
9370 struct WrappedFunopen2Cookie {
9372 funopen2_readfn real_read;
9373 funopen2_writefn real_write;
9374 funopen2_seekfn real_seek;
9375 funopen2_flushfn real_flush;
9376 funopen2_closefn real_close;
9379 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9380 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9381 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9382 funopen2_readfn real_read = wrapped_cookie->real_read;
9383 return real_read(wrapped_cookie->real_cookie, buf, len);
9386 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9388 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9389 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9390 funopen2_writefn real_write = wrapped_cookie->real_write;
9391 return real_write(wrapped_cookie->real_cookie, buf, len);
9394 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9395 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9396 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9397 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9398 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9401 static int wrapped_funopen2_flush(void *cookie) {
9402 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9403 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9404 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9405 return real_flush(wrapped_cookie->real_cookie);
9408 static int wrapped_funopen2_close(void *cookie) {
9409 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9410 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9411 funopen2_closefn real_close = wrapped_cookie->real_close;
9412 int res = real_close(wrapped_cookie->real_cookie);
9413 InternalFree(wrapped_cookie);
9417 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9418 funopen2_writefn writefn, funopen2_seekfn seekfn,
9419 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9421 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9424 WrappedFunopen2Cookie *wrapped_cookie =
9425 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9426 wrapped_cookie->real_cookie = cookie;
9427 wrapped_cookie->real_read = readfn;
9428 wrapped_cookie->real_write = writefn;
9429 wrapped_cookie->real_seek = seekfn;
9430 wrapped_cookie->real_flush = flushfn;
9431 wrapped_cookie->real_close = closefn;
9433 __sanitizer_FILE *res =
9434 REAL(funopen2)(wrapped_cookie,
9435 readfn ? wrapped_funopen2_read : nullptr,
9436 writefn ? wrapped_funopen2_write : nullptr,
9437 seekfn ? wrapped_funopen2_seek : nullptr,
9438 flushfn ? wrapped_funopen2_flush : nullptr,
9439 closefn ? wrapped_funopen2_close : nullptr);
9444 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9446 #define INIT_FUNOPEN2
9449 #if SANITIZER_INTERCEPT_FDEVNAME
9450 INTERCEPTOR(char *, fdevname, int fd) {
9452 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9453 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9454 char *name = REAL(fdevname)(fd);
9456 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
9458 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9463 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9465 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9466 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9467 char *name = REAL(fdevname_r)(fd, buf, len);
9468 if (name && buf && len > 0) {
9469 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
9471 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9476 #define INIT_FDEVNAME \
9477 COMMON_INTERCEPT_FUNCTION(fdevname); \
9478 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9480 #define INIT_FDEVNAME
9483 #if SANITIZER_INTERCEPT_GETUSERSHELL
9484 INTERCEPTOR(char *, getusershell) {
9486 COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9487 char *res = REAL(getusershell)();
9489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9493 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9495 #define INIT_GETUSERSHELL
9498 #if SANITIZER_INTERCEPT_SL_INIT
9499 INTERCEPTOR(void *, sl_init) {
9501 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9502 void *res = REAL(sl_init)();
9504 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9508 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9510 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9512 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9514 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9515 int res = REAL(sl_add)(sl, item);
9517 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9521 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9523 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9525 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9527 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9528 char *res = REAL(sl_find)(sl, item);
9530 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9534 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9536 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9538 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9539 REAL(sl_free)(sl, freeall);
9542 #define INIT_SL_INIT \
9543 COMMON_INTERCEPT_FUNCTION(sl_init); \
9544 COMMON_INTERCEPT_FUNCTION(sl_add); \
9545 COMMON_INTERCEPT_FUNCTION(sl_find); \
9546 COMMON_INTERCEPT_FUNCTION(sl_free);
9548 #define INIT_SL_INIT
9551 static void InitializeCommonInterceptors() {
9552 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
9553 interceptor_metadata_map =
9554 new ((void *)&metadata_mem) MetadataHashMap(); // NOLINT
9603 INIT_LOCALTIME_AND_FRIENDS;
9612 INIT_GETPWNAM_AND_FRIENDS;
9613 INIT_GETPWNAM_R_AND_FRIENDS;
9628 INIT_PTHREAD_GETSCHEDPARAM;
9633 INIT_GETHOSTBYNAME2;
9634 INIT_GETHOSTBYNAME_R;
9635 INIT_GETHOSTBYNAME2_R;
9636 INIT_GETHOSTBYADDR_R;
9656 INIT_GET_CURRENT_DIR_NAME;
9666 INIT_CANONICALIZE_FILE_NAME;
9668 INIT_SCHED_GETAFFINITY;
9669 INIT_SCHED_GETPARAM;
9672 INIT_XPG_STRERROR_R;
9685 INIT_PTHREAD_SIGMASK;
9688 INIT_PTHREAD_MUTEX_LOCK;
9689 INIT_PTHREAD_MUTEX_UNLOCK;
9690 INIT___PTHREAD_MUTEX_LOCK;
9691 INIT___PTHREAD_MUTEX_UNLOCK;
9692 INIT___LIBC_MUTEX_LOCK;
9693 INIT___LIBC_MUTEX_UNLOCK;
9694 INIT___LIBC_THR_SETCANCELSTATE;
9702 INIT_ETHER_NTOA_ATON;
9707 INIT_PTHREAD_ATTR_GET;
9708 INIT_PTHREAD_ATTR_GET_SCHED;
9709 INIT_PTHREAD_ATTR_GETINHERITSCHED;
9710 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
9711 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
9712 INIT_PTHREAD_MUTEXATTR_GETTYPE;
9713 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
9714 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
9715 INIT_PTHREAD_MUTEXATTR_GETROBUST;
9716 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
9717 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
9718 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
9719 INIT_PTHREAD_CONDATTR_GETPSHARED;
9720 INIT_PTHREAD_CONDATTR_GETCLOCK;
9721 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
9727 INIT_PTHREAD_SETNAME_NP;
9728 INIT_PTHREAD_GETNAME_NP;
9746 INIT_IF_INDEXTONAME;
9754 INIT_LIBIO_INTERNALS;
9757 INIT_OPEN_MEMSTREAM;
9761 INIT_DLOPEN_DLCLOSE;
9767 INIT_PTHREAD_SETCANCEL;
9769 INIT_PROCESS_VM_READV;
9775 INIT_EVENTFD_READ_WRITE;
9781 // FIXME: add other *stat interceptors.
9793 INIT_GROUP_FROM_GID;
9794 INIT_GID_FROM_GROUP;
9798 INIT_GETGROUPMEMBERSHIP;
9801 INIT_NAME_TO_HANDLE_AT;
9802 INIT_OPEN_BY_HANDLE_AT;
9812 INIT_MI_VECTOR_HASH;
9820 INIT_SYSCTLGETMIBINFO;