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 {
321 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
323 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) { // `fputs(NULL, file)` is supported on Darwin.
1245 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1247 return REAL(fputs)(s, file);
1249 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1254 #if SANITIZER_INTERCEPT_PUTS
1255 INTERCEPTOR(int, puts, char *s) {
1256 // libc file streams can call user-supplied functions, see fopencookie.
1258 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1259 if (!SANITIZER_MAC || s) { // `puts(NULL)` is supported on Darwin.
1260 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
1262 return REAL(puts)(s);
1264 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1269 #if SANITIZER_INTERCEPT_PRCTL
1270 INTERCEPTOR(int, prctl, int option, unsigned long arg2,
1271 unsigned long arg3, // NOLINT
1272 unsigned long arg4, unsigned long arg5) { // NOLINT
1274 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1275 static const int PR_SET_NAME = 15;
1276 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
1277 if (option == PR_SET_NAME) {
1279 internal_strncpy(buff, (char *)arg2, 15);
1281 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1285 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1288 #endif // SANITIZER_INTERCEPT_PRCTL
1290 #if SANITIZER_INTERCEPT_TIME
1291 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1293 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1294 unsigned long local_t;
1295 unsigned long res = REAL(time)(&local_t);
1296 if (t && res != (unsigned long)-1) {
1297 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1302 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1305 #endif // SANITIZER_INTERCEPT_TIME
1307 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1308 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1309 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1310 #if !SANITIZER_SOLARIS
1312 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1313 // can point to shared memory and tsan would report a data race.
1314 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1315 REAL(strlen(tm->tm_zone)) + 1);
1319 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1321 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1322 __sanitizer_tm *res = REAL(localtime)(timep);
1324 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1325 unpoison_tm(ctx, res);
1329 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1331 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1332 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1334 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1335 unpoison_tm(ctx, res);
1339 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1341 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1342 __sanitizer_tm *res = REAL(gmtime)(timep);
1344 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1345 unpoison_tm(ctx, res);
1349 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1351 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1352 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1354 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1355 unpoison_tm(ctx, res);
1359 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1361 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
1362 // FIXME: under ASan the call below may write to freed memory and corrupt
1363 // its metadata. See
1364 // https://github.com/google/sanitizers/issues/321.
1365 char *res = REAL(ctime)(timep);
1367 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1368 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1372 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1374 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1375 // FIXME: under ASan the call below may write to freed memory and corrupt
1376 // its metadata. See
1377 // https://github.com/google/sanitizers/issues/321.
1378 char *res = REAL(ctime_r)(timep, result);
1380 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1385 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1387 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1388 // FIXME: under ASan the call below may write to freed memory and corrupt
1389 // its metadata. See
1390 // https://github.com/google/sanitizers/issues/321.
1391 char *res = REAL(asctime)(tm);
1393 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1394 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1398 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1400 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1401 // FIXME: under ASan the call below may write to freed memory and corrupt
1402 // its metadata. See
1403 // https://github.com/google/sanitizers/issues/321.
1404 char *res = REAL(asctime_r)(tm, result);
1406 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
1411 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1413 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1414 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1415 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1416 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1417 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1418 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1419 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1420 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1421 long res = REAL(mktime)(tm);
1422 if (res != -1) unpoison_tm(ctx, tm);
1425 #define INIT_LOCALTIME_AND_FRIENDS \
1426 COMMON_INTERCEPT_FUNCTION(localtime); \
1427 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1428 COMMON_INTERCEPT_FUNCTION(gmtime); \
1429 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1430 COMMON_INTERCEPT_FUNCTION(ctime); \
1431 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1432 COMMON_INTERCEPT_FUNCTION(asctime); \
1433 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1434 COMMON_INTERCEPT_FUNCTION(mktime);
1436 #define INIT_LOCALTIME_AND_FRIENDS
1437 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1439 #if SANITIZER_INTERCEPT_STRPTIME
1440 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1442 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1444 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
1445 // FIXME: under ASan the call below may write to freed memory and corrupt
1446 // its metadata. See
1447 // https://github.com/google/sanitizers/issues/321.
1448 char *res = REAL(strptime)(s, format, tm);
1449 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1451 // Do not call unpoison_tm here, because strptime does not, in fact,
1452 // initialize the entire struct tm. For example, tm_zone pointer is left
1454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1458 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1460 #define INIT_STRPTIME
1463 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1464 #include "sanitizer_common_interceptors_format.inc"
1466 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1470 va_start(ap, format); \
1471 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1472 int res = WRAP(vname)(__VA_ARGS__, ap); \
1479 #if SANITIZER_INTERCEPT_SCANF
1481 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1484 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1487 int res = REAL(vname)(__VA_ARGS__); \
1489 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1494 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1495 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1497 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1498 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1500 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1501 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1503 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1504 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1505 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1507 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1509 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1511 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1512 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1513 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1515 INTERCEPTOR(int, scanf, const char *format, ...)
1516 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1518 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1519 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1521 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1522 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1524 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1525 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1526 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1528 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1529 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1531 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1532 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1537 #if SANITIZER_INTERCEPT_SCANF
1538 #define INIT_SCANF \
1539 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1540 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1541 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1542 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1543 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1544 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1549 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1550 #define INIT_ISOC99_SCANF \
1551 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1552 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1553 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1554 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1555 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1556 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
1558 #define INIT_ISOC99_SCANF
1561 #if SANITIZER_INTERCEPT_PRINTF
1563 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1565 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1569 #define VPRINTF_INTERCEPTOR_RETURN() \
1572 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1574 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1575 if (common_flags()->check_printf) \
1576 printf_common(ctx, format, aq); \
1577 int res = REAL(vname)(__VA_ARGS__); \
1578 VPRINTF_INTERCEPTOR_RETURN(); \
1582 // FIXME: under ASan the REAL() call below may write to freed memory and
1583 // corrupt its metadata. See
1584 // https://github.com/google/sanitizers/issues/321.
1585 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1587 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1588 if (common_flags()->check_printf) { \
1589 printf_common(ctx, format, aq); \
1591 int res = REAL(vname)(str, __VA_ARGS__); \
1593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1595 VPRINTF_INTERCEPTOR_RETURN(); \
1599 // FIXME: under ASan the REAL() call below may write to freed memory and
1600 // corrupt its metadata. See
1601 // https://github.com/google/sanitizers/issues/321.
1602 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1604 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1605 if (common_flags()->check_printf) { \
1606 printf_common(ctx, format, aq); \
1608 int res = REAL(vname)(str, size, __VA_ARGS__); \
1610 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1612 VPRINTF_INTERCEPTOR_RETURN(); \
1616 // FIXME: under ASan the REAL() call below may write to freed memory and
1617 // corrupt its metadata. See
1618 // https://github.com/google/sanitizers/issues/321.
1619 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1621 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1623 if (common_flags()->check_printf) { \
1624 printf_common(ctx, format, aq); \
1626 int res = REAL(vname)(strp, __VA_ARGS__); \
1628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1630 VPRINTF_INTERCEPTOR_RETURN(); \
1634 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1635 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1637 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1639 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1641 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1643 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1645 #if SANITIZER_INTERCEPT___PRINTF_CHK
1646 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1647 SIZE_T size_to, const char *format, va_list ap)
1648 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1651 #if SANITIZER_INTERCEPT_PRINTF_L
1652 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1653 const char *format, va_list ap)
1654 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1656 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1657 const char *format, ...)
1658 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1659 #endif // SANITIZER_INTERCEPT_PRINTF_L
1661 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1662 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1664 #if SANITIZER_INTERCEPT___PRINTF_CHK
1665 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1666 const char *format, va_list ap)
1667 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1670 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1671 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1673 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1674 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1675 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1677 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1678 const char *format, va_list ap)
1679 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1681 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1683 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1685 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1687 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1690 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1692 INTERCEPTOR(int, printf, const char *format, ...)
1693 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1695 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1696 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1698 #if SANITIZER_INTERCEPT___PRINTF_CHK
1699 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1700 const char *format, ...)
1701 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1704 INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
1705 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
1707 #if SANITIZER_INTERCEPT___PRINTF_CHK
1708 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1709 const char *format, ...) // NOLINT
1710 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format) // NOLINT
1713 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1714 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1716 #if SANITIZER_INTERCEPT___PRINTF_CHK
1717 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1718 SIZE_T size_to, const char *format, ...) // NOLINT
1719 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format) // NOLINT
1722 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1723 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1725 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1726 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1727 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1729 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1731 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1733 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1734 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1736 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1737 const char *format, ...)
1738 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1741 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1743 #endif // SANITIZER_INTERCEPT_PRINTF
1745 #if SANITIZER_INTERCEPT_PRINTF
1746 #define INIT_PRINTF \
1747 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1748 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1749 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1750 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1751 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1752 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1753 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1754 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1755 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1756 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1761 #if SANITIZER_INTERCEPT___PRINTF_CHK
1762 #define INIT___PRINTF_CHK \
1763 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1764 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1765 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1766 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1767 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1769 #define INIT___PRINTF_CHK
1772 #if SANITIZER_INTERCEPT_PRINTF_L
1773 #define INIT_PRINTF_L \
1774 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1775 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1777 #define INIT_PRINTF_L
1780 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1781 #define INIT_ISOC99_PRINTF \
1782 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1783 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1784 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1785 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1786 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1787 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1788 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1789 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1791 #define INIT_ISOC99_PRINTF
1794 #if SANITIZER_INTERCEPT_IOCTL
1795 #include "sanitizer_common_interceptors_ioctl.inc"
1796 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1797 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1798 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1799 // can trigger a report and we need to be able to unwind through this
1800 // function. On Mac in debug mode we might not have a frame pointer, because
1801 // ioctl_common_[pre|post] doesn't get inlined here.
1802 ENABLE_FRAME_POINTER;
1806 va_start(ap, request);
1807 void *arg = va_arg(ap, void *);
1809 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1811 CHECK(ioctl_initialized);
1813 // Note: TSan does not use common flags, and they are zero-initialized.
1814 // This effectively disables ioctl handling in TSan.
1815 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1817 // Although request is unsigned long, the rest of the interceptor uses it
1818 // as just "unsigned" to save space, because we know that all values fit in
1819 // "unsigned" - they are compile-time constants.
1821 const ioctl_desc *desc = ioctl_lookup(request);
1822 ioctl_desc decoded_desc;
1824 VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
1825 if (!ioctl_decode(request, &decoded_desc))
1826 Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
1828 desc = &decoded_desc;
1831 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1832 int res = REAL(ioctl)(d, request, arg);
1833 // FIXME: some ioctls have different return values for success and failure.
1834 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1837 #define INIT_IOCTL \
1839 COMMON_INTERCEPT_FUNCTION(ioctl);
1845 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1849 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1850 REAL(strlen)(pwd->pw_name) + 1);
1852 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1853 REAL(strlen)(pwd->pw_passwd) + 1);
1854 #if !SANITIZER_ANDROID
1856 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1857 REAL(strlen)(pwd->pw_gecos) + 1);
1859 #if SANITIZER_MAC || SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
1861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1862 REAL(strlen)(pwd->pw_class) + 1);
1865 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1866 REAL(strlen)(pwd->pw_dir) + 1);
1868 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1869 REAL(strlen)(pwd->pw_shell) + 1);
1873 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1875 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1877 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1878 REAL(strlen)(grp->gr_name) + 1);
1880 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1881 REAL(strlen)(grp->gr_passwd) + 1);
1882 char **p = grp->gr_mem;
1884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
1886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1887 (p - grp->gr_mem + 1) * sizeof(*p));
1890 #endif // SANITIZER_POSIX
1892 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1893 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1895 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1897 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1898 __sanitizer_passwd *res = REAL(getpwnam)(name);
1899 unpoison_passwd(ctx, res);
1902 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1904 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1905 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1906 unpoison_passwd(ctx, res);
1909 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1911 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1912 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1913 __sanitizer_group *res = REAL(getgrnam)(name);
1914 unpoison_group(ctx, res);
1917 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1919 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1920 __sanitizer_group *res = REAL(getgrgid)(gid);
1921 unpoison_group(ctx, res);
1924 #define INIT_GETPWNAM_AND_FRIENDS \
1925 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1926 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1927 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1928 COMMON_INTERCEPT_FUNCTION(getgrgid);
1930 #define INIT_GETPWNAM_AND_FRIENDS
1933 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1934 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1935 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
1937 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
1938 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1939 // FIXME: under ASan the call below may write to freed memory and corrupt
1940 // its metadata. See
1941 // https://github.com/google/sanitizers/issues/321.
1942 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
1944 unpoison_passwd(ctx, *result);
1945 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1948 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
1949 SIZE_T buflen, __sanitizer_passwd **result) {
1951 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
1952 // FIXME: under ASan the call below may write to freed memory and corrupt
1953 // its metadata. See
1954 // https://github.com/google/sanitizers/issues/321.
1955 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
1957 unpoison_passwd(ctx, *result);
1958 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1961 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
1962 char *buf, SIZE_T buflen, __sanitizer_group **result) {
1964 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
1965 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
1966 // FIXME: under ASan the call below may write to freed memory and corrupt
1967 // its metadata. See
1968 // https://github.com/google/sanitizers/issues/321.
1969 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
1971 unpoison_group(ctx, *result);
1972 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1975 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
1976 SIZE_T buflen, __sanitizer_group **result) {
1978 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
1979 // FIXME: under ASan the call below may write to freed memory and corrupt
1980 // its metadata. See
1981 // https://github.com/google/sanitizers/issues/321.
1982 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
1984 unpoison_group(ctx, *result);
1985 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
1988 #define INIT_GETPWNAM_R_AND_FRIENDS \
1989 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
1990 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
1991 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
1992 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
1994 #define INIT_GETPWNAM_R_AND_FRIENDS
1997 #if SANITIZER_INTERCEPT_GETPWENT
1998 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
2000 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
2001 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2002 unpoison_passwd(ctx, res);
2005 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2007 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2008 __sanitizer_group *res = REAL(getgrent)(dummy);
2009 unpoison_group(ctx, res);
2012 #define INIT_GETPWENT \
2013 COMMON_INTERCEPT_FUNCTION(getpwent); \
2014 COMMON_INTERCEPT_FUNCTION(getgrent);
2016 #define INIT_GETPWENT
2019 #if SANITIZER_INTERCEPT_FGETPWENT
2020 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2022 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2023 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2024 unpoison_passwd(ctx, res);
2027 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2029 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2030 __sanitizer_group *res = REAL(fgetgrent)(fp);
2031 unpoison_group(ctx, res);
2034 #define INIT_FGETPWENT \
2035 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2036 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2038 #define INIT_FGETPWENT
2041 #if SANITIZER_INTERCEPT_GETPWENT_R
2042 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2043 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2045 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2046 // FIXME: under ASan the call below may write to freed memory and corrupt
2047 // its metadata. See
2048 // https://github.com/google/sanitizers/issues/321.
2049 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2051 unpoison_passwd(ctx, *pwbufp);
2052 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2055 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2056 __sanitizer_group **pwbufp) {
2058 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2059 // FIXME: under ASan the call below may write to freed memory and corrupt
2060 // its metadata. See
2061 // https://github.com/google/sanitizers/issues/321.
2062 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2064 unpoison_group(ctx, *pwbufp);
2065 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2068 #define INIT_GETPWENT_R \
2069 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2070 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2072 #define INIT_GETPWENT_R
2075 #if SANITIZER_INTERCEPT_FGETPWENT_R
2076 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2077 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2079 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2080 // FIXME: under ASan the call below may write to freed memory and corrupt
2081 // its metadata. See
2082 // https://github.com/google/sanitizers/issues/321.
2083 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2085 unpoison_passwd(ctx, *pwbufp);
2086 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2089 #define INIT_FGETPWENT_R \
2090 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2092 #define INIT_FGETPWENT_R
2095 #if SANITIZER_INTERCEPT_FGETGRENT_R
2096 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2097 SIZE_T buflen, __sanitizer_group **pwbufp) {
2099 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
2100 // FIXME: under ASan the call below may write to freed memory and corrupt
2101 // its metadata. See
2102 // https://github.com/google/sanitizers/issues/321.
2103 int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2105 unpoison_group(ctx, *pwbufp);
2106 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2109 #define INIT_FGETGRENT_R \
2110 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2112 #define INIT_FGETGRENT_R
2115 #if SANITIZER_INTERCEPT_SETPWENT
2116 // The only thing these interceptors do is disable any nested interceptors.
2117 // These functions may open nss modules and call uninstrumented functions from
2118 // them, and we don't want things like strlen() to trigger.
2119 INTERCEPTOR(void, setpwent, int dummy) {
2121 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2122 REAL(setpwent)(dummy);
2124 INTERCEPTOR(void, endpwent, int dummy) {
2126 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2127 REAL(endpwent)(dummy);
2129 INTERCEPTOR(void, setgrent, int dummy) {
2131 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2132 REAL(setgrent)(dummy);
2134 INTERCEPTOR(void, endgrent, int dummy) {
2136 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2137 REAL(endgrent)(dummy);
2139 #define INIT_SETPWENT \
2140 COMMON_INTERCEPT_FUNCTION(setpwent); \
2141 COMMON_INTERCEPT_FUNCTION(endpwent); \
2142 COMMON_INTERCEPT_FUNCTION(setgrent); \
2143 COMMON_INTERCEPT_FUNCTION(endgrent);
2145 #define INIT_SETPWENT
2148 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2149 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2151 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2152 // FIXME: under ASan the call below may write to freed memory and corrupt
2153 // its metadata. See
2154 // https://github.com/google/sanitizers/issues/321.
2155 int res = REAL(clock_getres)(clk_id, tp);
2157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2161 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2163 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2164 // FIXME: under ASan the call below may write to freed memory and corrupt
2165 // its metadata. See
2166 // https://github.com/google/sanitizers/issues/321.
2167 int res = REAL(clock_gettime)(clk_id, tp);
2169 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2173 namespace __sanitizer {
2175 int real_clock_gettime(u32 clk_id, void *tp) {
2176 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2177 return internal_clock_gettime(clk_id, tp);
2178 return REAL(clock_gettime)(clk_id, tp);
2181 } // namespace __sanitizer
2182 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2184 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2185 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2186 return REAL(clock_settime)(clk_id, tp);
2188 #define INIT_CLOCK_GETTIME \
2189 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2190 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2191 COMMON_INTERCEPT_FUNCTION(clock_settime);
2193 #define INIT_CLOCK_GETTIME
2196 #if SANITIZER_INTERCEPT_GETITIMER
2197 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2199 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2200 // FIXME: under ASan the call below may write to freed memory and corrupt
2201 // its metadata. See
2202 // https://github.com/google/sanitizers/issues/321.
2203 int res = REAL(getitimer)(which, curr_value);
2204 if (!res && curr_value) {
2205 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2209 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2211 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2213 // itimerval can contain padding that may be legitimately uninitialized
2214 const struct __sanitizer_itimerval *nv =
2215 (const struct __sanitizer_itimerval *)new_value;
2216 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2217 sizeof(__sanitizer_time_t));
2218 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2219 sizeof(__sanitizer_suseconds_t));
2220 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2221 sizeof(__sanitizer_time_t));
2222 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2223 sizeof(__sanitizer_suseconds_t));
2225 // FIXME: under ASan the call below may write to freed memory and corrupt
2226 // its metadata. See
2227 // https://github.com/google/sanitizers/issues/321.
2228 int res = REAL(setitimer)(which, new_value, old_value);
2229 if (!res && old_value) {
2230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2234 #define INIT_GETITIMER \
2235 COMMON_INTERCEPT_FUNCTION(getitimer); \
2236 COMMON_INTERCEPT_FUNCTION(setitimer);
2238 #define INIT_GETITIMER
2241 #if SANITIZER_INTERCEPT_GLOB
2242 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2244 // +1 for NULL pointer at the end.
2245 if (pglob->gl_pathv)
2246 COMMON_INTERCEPTOR_WRITE_RANGE(
2247 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2248 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2249 char *p = pglob->gl_pathv[i];
2250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
2254 #if SANITIZER_SOLARIS
2255 INTERCEPTOR(int, glob, const char *pattern, int flags,
2256 int (*errfunc)(const char *epath, int eerrno),
2257 __sanitizer_glob_t *pglob) {
2259 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2260 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2261 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2262 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2266 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2268 static void wrapped_gl_closedir(void *dir) {
2269 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2270 pglob_copy->gl_closedir(dir);
2273 static void *wrapped_gl_readdir(void *dir) {
2274 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2275 return pglob_copy->gl_readdir(dir);
2278 static void *wrapped_gl_opendir(const char *s) {
2279 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2280 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2281 return pglob_copy->gl_opendir(s);
2284 static int wrapped_gl_lstat(const char *s, void *st) {
2285 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2286 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2287 return pglob_copy->gl_lstat(s, st);
2290 static int wrapped_gl_stat(const char *s, void *st) {
2291 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2292 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
2293 return pglob_copy->gl_stat(s, st);
2296 static const __sanitizer_glob_t kGlobCopy = {
2298 0, wrapped_gl_closedir, wrapped_gl_readdir,
2299 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2301 INTERCEPTOR(int, glob, const char *pattern, int flags,
2302 int (*errfunc)(const char *epath, int eerrno),
2303 __sanitizer_glob_t *pglob) {
2305 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2306 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2307 __sanitizer_glob_t glob_copy;
2308 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2309 if (flags & glob_altdirfunc) {
2310 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2311 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2312 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2313 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2314 Swap(pglob->gl_stat, glob_copy.gl_stat);
2315 pglob_copy = &glob_copy;
2317 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2318 if (flags & glob_altdirfunc) {
2319 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2320 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2321 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2322 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2323 Swap(pglob->gl_stat, glob_copy.gl_stat);
2326 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2329 #endif // SANITIZER_SOLARIS
2331 COMMON_INTERCEPT_FUNCTION(glob);
2332 #else // SANITIZER_INTERCEPT_GLOB
2334 #endif // SANITIZER_INTERCEPT_GLOB
2336 #if SANITIZER_INTERCEPT_GLOB64
2337 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2338 int (*errfunc)(const char *epath, int eerrno),
2339 __sanitizer_glob_t *pglob) {
2341 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2342 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2343 __sanitizer_glob_t glob_copy;
2344 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2345 if (flags & glob_altdirfunc) {
2346 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2347 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2348 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2349 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2350 Swap(pglob->gl_stat, glob_copy.gl_stat);
2351 pglob_copy = &glob_copy;
2353 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2354 if (flags & glob_altdirfunc) {
2355 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2356 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2357 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2358 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2359 Swap(pglob->gl_stat, glob_copy.gl_stat);
2362 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2365 #define INIT_GLOB64 \
2366 COMMON_INTERCEPT_FUNCTION(glob64);
2367 #else // SANITIZER_INTERCEPT_GLOB64
2369 #endif // SANITIZER_INTERCEPT_GLOB64
2371 #if SANITIZER_INTERCEPT_WAIT
2372 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2373 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2375 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2377 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2378 // FIXME: under ASan the call below may write to freed memory and corrupt
2379 // its metadata. See
2380 // https://github.com/google/sanitizers/issues/321.
2381 int res = REAL(wait)(status);
2382 if (res != -1 && status)
2383 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2386 // On FreeBSD id_t is always 64-bit wide.
2387 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2388 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2391 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2395 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2396 // FIXME: under ASan the call below may write to freed memory and corrupt
2397 // its metadata. See
2398 // https://github.com/google/sanitizers/issues/321.
2399 int res = REAL(waitid)(idtype, id, infop, options);
2400 if (res != -1 && infop)
2401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2404 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2406 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2407 // FIXME: under ASan the call below may write to freed memory and corrupt
2408 // its metadata. See
2409 // https://github.com/google/sanitizers/issues/321.
2410 int res = REAL(waitpid)(pid, status, options);
2411 if (res != -1 && status)
2412 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2415 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2417 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2418 // FIXME: under ASan the call below may write to freed memory and corrupt
2419 // its metadata. See
2420 // https://github.com/google/sanitizers/issues/321.
2421 int res = REAL(wait3)(status, options, rusage);
2423 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2424 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2428 #if SANITIZER_ANDROID
2429 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2431 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2432 // FIXME: under ASan the call below may write to freed memory and corrupt
2433 // its metadata. See
2434 // https://github.com/google/sanitizers/issues/321.
2435 int res = REAL(__wait4)(pid, status, options, rusage);
2437 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2438 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2442 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2444 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2446 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2447 // FIXME: under ASan the call below may write to freed memory and corrupt
2448 // its metadata. See
2449 // https://github.com/google/sanitizers/issues/321.
2450 int res = REAL(wait4)(pid, status, options, rusage);
2452 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2453 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2457 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2458 #endif // SANITIZER_ANDROID
2460 COMMON_INTERCEPT_FUNCTION(wait); \
2461 COMMON_INTERCEPT_FUNCTION(waitid); \
2462 COMMON_INTERCEPT_FUNCTION(waitpid); \
2463 COMMON_INTERCEPT_FUNCTION(wait3);
2469 #if SANITIZER_INTERCEPT_INET
2470 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2472 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2473 uptr sz = __sanitizer_in_addr_sz(af);
2474 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2475 // FIXME: figure out read size based on the address family.
2476 // FIXME: under ASan the call below may write to freed memory and corrupt
2477 // its metadata. See
2478 // https://github.com/google/sanitizers/issues/321.
2479 char *res = REAL(inet_ntop)(af, src, dst, size);
2480 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
2483 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2485 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2486 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2487 // FIXME: figure out read size based on the address family.
2488 // FIXME: under ASan the call below may write to freed memory and corrupt
2489 // its metadata. See
2490 // https://github.com/google/sanitizers/issues/321.
2491 int res = REAL(inet_pton)(af, src, dst);
2493 uptr sz = __sanitizer_in_addr_sz(af);
2494 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2499 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2500 COMMON_INTERCEPT_FUNCTION(inet_pton);
2505 #if SANITIZER_INTERCEPT_INET
2506 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2508 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2509 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
2510 // FIXME: under ASan the call below may write to freed memory and corrupt
2511 // its metadata. See
2512 // https://github.com/google/sanitizers/issues/321.
2513 int res = REAL(inet_aton)(cp, dst);
2515 uptr sz = __sanitizer_in_addr_sz(af_inet);
2516 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2520 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2522 #define INIT_INET_ATON
2525 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2526 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2528 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2529 // FIXME: under ASan the call below may write to freed memory and corrupt
2530 // its metadata. See
2531 // https://github.com/google/sanitizers/issues/321.
2532 int res = REAL(pthread_getschedparam)(thread, policy, param);
2534 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2535 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2539 #define INIT_PTHREAD_GETSCHEDPARAM \
2540 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2542 #define INIT_PTHREAD_GETSCHEDPARAM
2545 #if SANITIZER_INTERCEPT_GETADDRINFO
2546 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2547 struct __sanitizer_addrinfo *hints,
2548 struct __sanitizer_addrinfo **out) {
2550 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2551 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
2553 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
2555 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2556 // FIXME: under ASan the call below may write to freed memory and corrupt
2557 // its metadata. See
2558 // https://github.com/google/sanitizers/issues/321.
2559 int res = REAL(getaddrinfo)(node, service, hints, out);
2560 if (res == 0 && out) {
2561 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2562 struct __sanitizer_addrinfo *p = *out;
2564 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2567 if (p->ai_canonname)
2568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2569 REAL(strlen)(p->ai_canonname) + 1);
2575 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2577 #define INIT_GETADDRINFO
2580 #if SANITIZER_INTERCEPT_GETNAMEINFO
2581 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2582 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2584 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2585 serv, servlen, flags);
2586 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2587 // There is padding in in_addr that may make this too noisy
2588 // FIXME: under ASan the call below may write to freed memory and corrupt
2589 // its metadata. See
2590 // https://github.com/google/sanitizers/issues/321.
2592 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2594 if (host && hostlen)
2595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
2596 if (serv && servlen)
2597 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
2601 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2603 #define INIT_GETNAMEINFO
2606 #if SANITIZER_INTERCEPT_GETSOCKNAME
2607 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
2609 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2610 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2611 int addrlen_in = *addrlen;
2612 // FIXME: under ASan the call below may write to freed memory and corrupt
2613 // its metadata. See
2614 // https://github.com/google/sanitizers/issues/321.
2615 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2617 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
2621 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2623 #define INIT_GETSOCKNAME
2626 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2627 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2630 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
2631 char **p = h->h_aliases;
2633 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
2636 COMMON_INTERCEPTOR_WRITE_RANGE(
2637 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2640 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2643 COMMON_INTERCEPTOR_WRITE_RANGE(
2644 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2648 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2649 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2651 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2652 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2653 if (res) write_hostent(ctx, res);
2657 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2660 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2661 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2662 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2663 if (res) write_hostent(ctx, res);
2667 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2669 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2670 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2671 if (res) write_hostent(ctx, res);
2674 #define INIT_GETHOSTBYNAME \
2675 COMMON_INTERCEPT_FUNCTION(gethostent); \
2676 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2677 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2679 #define INIT_GETHOSTBYNAME
2680 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2682 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2683 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2685 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2686 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2687 if (res) write_hostent(ctx, res);
2690 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2692 #define INIT_GETHOSTBYNAME2
2693 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2695 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2696 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2697 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2700 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2702 // FIXME: under ASan the call below may write to freed memory and corrupt
2703 // its metadata. See
2704 // https://github.com/google/sanitizers/issues/321.
2705 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2707 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2708 if (res == 0 && *result) write_hostent(ctx, *result);
2711 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2714 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2716 #define INIT_GETHOSTBYNAME_R
2719 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2720 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2721 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2723 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2725 // FIXME: under ASan the call below may write to freed memory and corrupt
2726 // its metadata. See
2727 // https://github.com/google/sanitizers/issues/321.
2728 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2730 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2731 if (res == 0 && *result) write_hostent(ctx, *result);
2734 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2737 #define INIT_GETHOSTENT_R \
2738 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2740 #define INIT_GETHOSTENT_R
2743 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2744 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2745 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2746 __sanitizer_hostent **result, int *h_errnop) {
2748 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2749 buflen, result, h_errnop);
2750 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2751 // FIXME: under ASan the call below may write to freed memory and corrupt
2752 // its metadata. See
2753 // https://github.com/google/sanitizers/issues/321.
2754 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2757 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2758 if (res == 0 && *result) write_hostent(ctx, *result);
2761 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2764 #define INIT_GETHOSTBYADDR_R \
2765 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2767 #define INIT_GETHOSTBYADDR_R
2770 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
2771 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
2772 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2773 __sanitizer_hostent **result, int *h_errnop) {
2775 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
2777 // FIXME: under ASan the call below may write to freed memory and corrupt
2778 // its metadata. See
2779 // https://github.com/google/sanitizers/issues/321.
2781 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
2783 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2784 if (res == 0 && *result) write_hostent(ctx, *result);
2787 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2790 #define INIT_GETHOSTBYNAME2_R \
2791 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
2793 #define INIT_GETHOSTBYNAME2_R
2796 #if SANITIZER_INTERCEPT_GETSOCKOPT
2797 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
2800 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
2802 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
2803 // FIXME: under ASan the call below may write to freed memory and corrupt
2804 // its metadata. See
2805 // https://github.com/google/sanitizers/issues/321.
2806 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
2808 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
2811 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
2813 #define INIT_GETSOCKOPT
2816 #if SANITIZER_INTERCEPT_ACCEPT
2817 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
2819 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
2820 unsigned addrlen0 = 0;
2822 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2823 addrlen0 = *addrlen;
2825 int fd2 = REAL(accept)(fd, addr, addrlen);
2827 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2828 if (addr && addrlen)
2829 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2833 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
2838 #if SANITIZER_INTERCEPT_ACCEPT4
2839 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
2841 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
2842 unsigned addrlen0 = 0;
2844 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2845 addrlen0 = *addrlen;
2847 // FIXME: under ASan the call below may write to freed memory and corrupt
2848 // its metadata. See
2849 // https://github.com/google/sanitizers/issues/321.
2850 int fd2 = REAL(accept4)(fd, addr, addrlen, f);
2852 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2853 if (addr && addrlen)
2854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2858 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
2860 #define INIT_ACCEPT4
2863 #if SANITIZER_INTERCEPT_PACCEPT
2864 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
2865 __sanitizer_sigset_t *set, int f) {
2867 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
2868 unsigned addrlen0 = 0;
2870 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2871 addrlen0 = *addrlen;
2873 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
2874 int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
2876 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
2877 if (addr && addrlen)
2878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
2882 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
2884 #define INIT_PACCEPT
2887 #if SANITIZER_INTERCEPT_MODF
2888 INTERCEPTOR(double, modf, double x, double *iptr) {
2890 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
2891 // FIXME: under ASan the call below may write to freed memory and corrupt
2892 // its metadata. See
2893 // https://github.com/google/sanitizers/issues/321.
2894 double res = REAL(modf)(x, iptr);
2896 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2900 INTERCEPTOR(float, modff, float x, float *iptr) {
2902 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
2903 // FIXME: under ASan the call below may write to freed memory and corrupt
2904 // its metadata. See
2905 // https://github.com/google/sanitizers/issues/321.
2906 float res = REAL(modff)(x, iptr);
2908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2912 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
2914 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
2915 // FIXME: under ASan the call below may write to freed memory and corrupt
2916 // its metadata. See
2917 // https://github.com/google/sanitizers/issues/321.
2918 long double res = REAL(modfl)(x, iptr);
2920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
2925 COMMON_INTERCEPT_FUNCTION(modf); \
2926 COMMON_INTERCEPT_FUNCTION(modff); \
2927 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
2932 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
2933 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
2935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
2936 if (msg->msg_name && msg->msg_namelen)
2937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
2938 if (msg->msg_iov && msg->msg_iovlen)
2939 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
2940 sizeof(*msg->msg_iov) * msg->msg_iovlen);
2941 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
2942 if (msg->msg_control && msg->msg_controllen)
2943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
2947 #if SANITIZER_INTERCEPT_RECVMSG
2948 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
2951 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
2952 // FIXME: under ASan the call below may write to freed memory and corrupt
2953 // its metadata. See
2954 // https://github.com/google/sanitizers/issues/321.
2955 SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
2957 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2959 write_msghdr(ctx, msg, res);
2960 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
2965 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
2967 #define INIT_RECVMSG
2970 #if SANITIZER_INTERCEPT_RECVMMSG
2971 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
2972 unsigned int vlen, int flags, void *timeout) {
2974 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
2975 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
2976 int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
2978 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
2979 for (int i = 0; i < res; ++i) {
2980 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
2981 sizeof(msgvec[i].msg_len));
2982 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
2983 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
2988 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
2990 #define INIT_RECVMMSG
2993 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
2994 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
2995 const unsigned kCmsgDataOffset =
2996 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
2998 char *p = (char *)control;
2999 char *const control_end = p + controllen;
3001 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3002 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3003 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3005 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3007 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3008 sizeof(cmsg->cmsg_level));
3009 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3010 sizeof(cmsg->cmsg_type));
3012 if (cmsg->cmsg_len > kCmsgDataOffset) {
3013 char *data = p + kCmsgDataOffset;
3014 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3015 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3018 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3022 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3025 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3034 if (msg->msg_name && msg->msg_namelen)
3035 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3036 if (msg->msg_iov && msg->msg_iovlen)
3037 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3038 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3039 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3040 if (msg->msg_control && msg->msg_controllen)
3041 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3045 #if SANITIZER_INTERCEPT_SENDMSG
3046 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3049 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3051 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3052 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3054 SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
3055 if (common_flags()->intercept_send && res >= 0 && msg)
3056 read_msghdr(ctx, msg, res);
3059 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3061 #define INIT_SENDMSG
3064 #if SANITIZER_INTERCEPT_SENDMMSG
3065 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3066 unsigned vlen, int flags) {
3068 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3070 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3071 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3073 int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
3074 if (res >= 0 && msgvec)
3075 for (int i = 0; i < res; ++i) {
3076 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3077 sizeof(msgvec[i].msg_len));
3078 if (common_flags()->intercept_send)
3079 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3083 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3085 #define INIT_SENDMMSG
3088 #if SANITIZER_INTERCEPT_GETPEERNAME
3089 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3091 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3093 if (addrlen) addr_sz = *addrlen;
3094 // FIXME: under ASan the call below may write to freed memory and corrupt
3095 // its metadata. See
3096 // https://github.com/google/sanitizers/issues/321.
3097 int res = REAL(getpeername)(sockfd, addr, addrlen);
3098 if (!res && addr && addrlen)
3099 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3102 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3104 #define INIT_GETPEERNAME
3107 #if SANITIZER_INTERCEPT_SYSINFO
3108 INTERCEPTOR(int, sysinfo, void *info) {
3110 // FIXME: under ASan the call below may write to freed memory and corrupt
3111 // its metadata. See
3112 // https://github.com/google/sanitizers/issues/321.
3113 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3114 int res = REAL(sysinfo)(info);
3116 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3119 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3121 #define INIT_SYSINFO
3124 #if SANITIZER_INTERCEPT_READDIR
3125 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3127 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3128 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3129 __sanitizer_dirent *res = REAL(opendir)(path);
3131 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3135 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3137 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3138 // FIXME: under ASan the call below may write to freed memory and corrupt
3139 // its metadata. See
3140 // https://github.com/google/sanitizers/issues/321.
3141 __sanitizer_dirent *res = REAL(readdir)(dirp);
3142 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3146 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3147 __sanitizer_dirent **result) {
3149 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3150 // FIXME: under ASan the call below may write to freed memory and corrupt
3151 // its metadata. See
3152 // https://github.com/google/sanitizers/issues/321.
3153 int res = REAL(readdir_r)(dirp, entry, result);
3155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3162 #define INIT_READDIR \
3163 COMMON_INTERCEPT_FUNCTION(opendir); \
3164 COMMON_INTERCEPT_FUNCTION(readdir); \
3165 COMMON_INTERCEPT_FUNCTION(readdir_r);
3167 #define INIT_READDIR
3170 #if SANITIZER_INTERCEPT_READDIR64
3171 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3173 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3174 // FIXME: under ASan the call below may write to freed memory and corrupt
3175 // its metadata. See
3176 // https://github.com/google/sanitizers/issues/321.
3177 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3178 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
3182 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3183 __sanitizer_dirent64 **result) {
3185 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3186 // FIXME: under ASan the call below may write to freed memory and corrupt
3187 // its metadata. See
3188 // https://github.com/google/sanitizers/issues/321.
3189 int res = REAL(readdir64_r)(dirp, entry, result);
3191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3193 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
3197 #define INIT_READDIR64 \
3198 COMMON_INTERCEPT_FUNCTION(readdir64); \
3199 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3201 #define INIT_READDIR64
3204 #if SANITIZER_INTERCEPT_PTRACE
3205 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3207 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3208 __sanitizer_iovec local_iovec;
3211 if (request == ptrace_setregs)
3212 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
3213 else if (request == ptrace_setfpregs)
3214 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3215 else if (request == ptrace_setfpxregs)
3216 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3217 else if (request == ptrace_setvfpregs)
3218 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3219 else if (request == ptrace_setsiginfo)
3220 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
3221 // Some kernel might zero the iovec::iov_base in case of invalid
3222 // write access. In this case copy the invalid address for further
3224 else if (request == ptrace_setregset || request == ptrace_getregset) {
3225 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3226 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3227 local_iovec = *iovec;
3228 if (request == ptrace_setregset)
3229 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3233 // FIXME: under ASan the call below may write to freed memory and corrupt
3234 // its metadata. See
3235 // https://github.com/google/sanitizers/issues/321.
3236 uptr res = REAL(ptrace)(request, pid, addr, data);
3239 // Note that PEEK* requests assign different meaning to the return value.
3240 // This function does not handle them (nor does it need to).
3241 if (request == ptrace_getregs)
3242 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
3243 else if (request == ptrace_getfpregs)
3244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
3245 else if (request == ptrace_getfpxregs)
3246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
3247 else if (request == ptrace_getvfpregs)
3248 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
3249 else if (request == ptrace_getsiginfo)
3250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
3251 else if (request == ptrace_geteventmsg)
3252 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
3253 else if (request == ptrace_getregset) {
3254 __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
3255 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3256 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3257 local_iovec.iov_len);
3263 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3268 #if SANITIZER_INTERCEPT_SETLOCALE
3269 static void unpoison_ctype_arrays(void *ctx) {
3270 #if SANITIZER_NETBSD
3271 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3272 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3273 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3278 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3280 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3282 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
3283 char *res = REAL(setlocale)(category, locale);
3285 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3286 unpoison_ctype_arrays(ctx);
3291 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3293 #define INIT_SETLOCALE
3296 #if SANITIZER_INTERCEPT_GETCWD
3297 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3299 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3300 // FIXME: under ASan the call below may write to freed memory and corrupt
3301 // its metadata. See
3302 // https://github.com/google/sanitizers/issues/321.
3303 char *res = REAL(getcwd)(buf, size);
3304 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3307 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3312 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3313 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3315 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3316 // FIXME: under ASan the call below may write to freed memory and corrupt
3317 // its metadata. See
3318 // https://github.com/google/sanitizers/issues/321.
3319 char *res = REAL(get_current_dir_name)(fake);
3320 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3324 #define INIT_GET_CURRENT_DIR_NAME \
3325 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3327 #define INIT_GET_CURRENT_DIR_NAME
3330 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3332 if (nptr == *endptr) {
3333 // No digits were found at strtol call, we need to find out the last
3334 // symbol accessed by strtoll on our own.
3335 // We get this symbol by skipping leading blanks and optional +/- sign.
3336 while (IsSpace(*nptr)) nptr++;
3337 if (*nptr == '+' || *nptr == '-') nptr++;
3338 *endptr = const_cast<char *>(nptr);
3340 CHECK(*endptr >= nptr);
3343 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3344 char **endptr, char *real_endptr, int base) {
3346 *endptr = real_endptr;
3347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3349 // If base has unsupported value, strtol can exit with EINVAL
3350 // without reading any characters. So do additional checks only
3351 // if base is valid.
3352 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3353 if (is_valid_base) {
3354 FixRealStrtolEndptr(nptr, &real_endptr);
3356 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3357 (real_endptr - nptr) + 1 : 0);
3361 #if SANITIZER_INTERCEPT_STRTOIMAX
3362 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3364 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3365 // FIXME: under ASan the call below may write to freed memory and corrupt
3366 // its metadata. See
3367 // https://github.com/google/sanitizers/issues/321.
3369 INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
3370 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3374 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3376 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3377 // FIXME: under ASan the call below may write to freed memory and corrupt
3378 // its metadata. See
3379 // https://github.com/google/sanitizers/issues/321.
3381 UINTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
3382 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3386 #define INIT_STRTOIMAX \
3387 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3388 COMMON_INTERCEPT_FUNCTION(strtoumax);
3390 #define INIT_STRTOIMAX
3393 #if SANITIZER_INTERCEPT_MBSTOWCS
3394 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3396 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3397 // FIXME: under ASan the call below may write to freed memory and corrupt
3398 // its metadata. See
3399 // https://github.com/google/sanitizers/issues/321.
3400 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3401 if (res != (SIZE_T) - 1 && dest) {
3402 SIZE_T write_cnt = res + (res < len);
3403 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3408 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3411 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3412 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3413 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3414 // FIXME: under ASan the call below may write to freed memory and corrupt
3415 // its metadata. See
3416 // https://github.com/google/sanitizers/issues/321.
3417 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3418 if (res != (SIZE_T)(-1) && dest && src) {
3419 // This function, and several others, may or may not write the terminating
3420 // \0 character. They write it iff they clear *src.
3421 SIZE_T write_cnt = res + !*src;
3422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3427 #define INIT_MBSTOWCS \
3428 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3429 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3431 #define INIT_MBSTOWCS
3434 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3435 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3436 SIZE_T len, void *ps) {
3438 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3440 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3441 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3443 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3444 // FIXME: under ASan the call below may write to freed memory and corrupt
3445 // its metadata. See
3446 // https://github.com/google/sanitizers/issues/321.
3447 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3448 if (res != (SIZE_T)(-1) && dest && src) {
3449 SIZE_T write_cnt = res + !*src;
3450 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3455 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3457 #define INIT_MBSNRTOWCS
3460 #if SANITIZER_INTERCEPT_WCSTOMBS
3461 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3463 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3464 // FIXME: under ASan the call below may write to freed memory and corrupt
3465 // its metadata. See
3466 // https://github.com/google/sanitizers/issues/321.
3467 SIZE_T res = REAL(wcstombs)(dest, src, len);
3468 if (res != (SIZE_T) - 1 && dest) {
3469 SIZE_T write_cnt = res + (res < len);
3470 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3475 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3478 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3479 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3480 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3481 // FIXME: under ASan the call below may write to freed memory and corrupt
3482 // its metadata. See
3483 // https://github.com/google/sanitizers/issues/321.
3484 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3485 if (res != (SIZE_T) - 1 && dest && src) {
3486 SIZE_T write_cnt = res + !*src;
3487 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3492 #define INIT_WCSTOMBS \
3493 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3494 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3496 #define INIT_WCSTOMBS
3499 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3500 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3501 SIZE_T len, void *ps) {
3503 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3505 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3506 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3508 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3509 // FIXME: under ASan the call below may write to freed memory and corrupt
3510 // its metadata. See
3511 // https://github.com/google/sanitizers/issues/321.
3512 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3513 if (res != ((SIZE_T)-1) && dest && src) {
3514 SIZE_T write_cnt = res + !*src;
3515 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3520 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3522 #define INIT_WCSNRTOMBS
3526 #if SANITIZER_INTERCEPT_WCRTOMB
3527 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3529 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3530 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3533 return REAL(wcrtomb)(dest, src, ps);
3535 char local_dest[32];
3536 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3537 if (res != ((SIZE_T)-1)) {
3538 CHECK_LE(res, sizeof(local_dest));
3539 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3540 REAL(memcpy)(dest, local_dest, res);
3545 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3547 #define INIT_WCRTOMB
3550 #if SANITIZER_INTERCEPT_WCTOMB
3551 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3553 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3555 return REAL(wctomb)(dest, src);
3557 char local_dest[32];
3558 int res = REAL(wctomb)(local_dest, src);
3560 CHECK_LE(res, sizeof(local_dest));
3561 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3562 REAL(memcpy)(dest, local_dest, res);
3567 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3572 #if SANITIZER_INTERCEPT_TCGETATTR
3573 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3575 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3576 // FIXME: under ASan the call below may write to freed memory and corrupt
3577 // its metadata. See
3578 // https://github.com/google/sanitizers/issues/321.
3579 int res = REAL(tcgetattr)(fd, termios_p);
3580 if (!res && termios_p)
3581 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3585 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3587 #define INIT_TCGETATTR
3590 #if SANITIZER_INTERCEPT_REALPATH
3591 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3593 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3594 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3596 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3597 // version of a versioned symbol. For realpath(), this gives us something
3598 // (called __old_realpath) that does not handle NULL in the second argument.
3599 // Handle it as part of the interceptor.
3600 char *allocated_path = nullptr;
3602 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3604 char *res = REAL(realpath)(path, resolved_path);
3605 if (allocated_path && !res) WRAP(free)(allocated_path);
3606 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3609 #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3611 #define INIT_REALPATH
3614 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3615 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3617 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3618 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
3619 char *res = REAL(canonicalize_file_name)(path);
3620 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3623 #define INIT_CANONICALIZE_FILE_NAME \
3624 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3626 #define INIT_CANONICALIZE_FILE_NAME
3629 #if SANITIZER_INTERCEPT_CONFSTR
3630 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3632 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3633 // FIXME: under ASan the call below may write to freed memory and corrupt
3634 // its metadata. See
3635 // https://github.com/google/sanitizers/issues/321.
3636 SIZE_T res = REAL(confstr)(name, buf, len);
3638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3641 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3643 #define INIT_CONFSTR
3646 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3647 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3649 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3650 // FIXME: under ASan the call below may write to freed memory and corrupt
3651 // its metadata. See
3652 // https://github.com/google/sanitizers/issues/321.
3653 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3654 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3657 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3659 #define INIT_SCHED_GETAFFINITY
3662 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3663 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3665 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3666 int res = REAL(sched_getparam)(pid, param);
3667 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3670 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3672 #define INIT_SCHED_GETPARAM
3675 #if SANITIZER_INTERCEPT_STRERROR
3676 INTERCEPTOR(char *, strerror, int errnum) {
3678 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3679 char *res = REAL(strerror)(errnum);
3680 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3683 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3685 #define INIT_STRERROR
3688 #if SANITIZER_INTERCEPT_STRERROR_R
3689 // There are 2 versions of strerror_r:
3690 // * POSIX version returns 0 on success, negative error code on failure,
3691 // writes message to buf.
3692 // * GNU version returns message pointer, which points to either buf or some
3694 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3695 SANITIZER_MAC || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3696 SANITIZER_FREEBSD || SANITIZER_OPENBSD
3697 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3698 // At least on OSX, buf contents are valid even when the call fails.
3699 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3701 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3702 // FIXME: under ASan the call below may write to freed memory and corrupt
3703 // its metadata. See
3704 // https://github.com/google/sanitizers/issues/321.
3705 int res = REAL(strerror_r)(errnum, buf, buflen);
3707 SIZE_T sz = internal_strnlen(buf, buflen);
3708 if (sz < buflen) ++sz;
3709 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
3714 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3716 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3717 // FIXME: under ASan the call below may write to freed memory and corrupt
3718 // its metadata. See
3719 // https://github.com/google/sanitizers/issues/321.
3720 char *res = REAL(strerror_r)(errnum, buf, buflen);
3722 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
3724 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
3727 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
3729 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
3731 #define INIT_STRERROR_R
3734 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
3735 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
3737 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
3738 // FIXME: under ASan the call below may write to freed memory and corrupt
3739 // its metadata. See
3740 // https://github.com/google/sanitizers/issues/321.
3741 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
3742 // This version always returns a null-terminated string.
3744 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
3747 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
3749 #define INIT_XPG_STRERROR_R
3752 #if SANITIZER_INTERCEPT_SCANDIR
3753 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
3754 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
3755 const struct __sanitizer_dirent **);
3757 static THREADLOCAL scandir_filter_f scandir_filter;
3758 static THREADLOCAL scandir_compar_f scandir_compar;
3760 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
3761 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3762 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3763 return scandir_filter(dir);
3766 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
3767 const struct __sanitizer_dirent **b) {
3768 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3769 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3770 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3771 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3772 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3773 return scandir_compar(a, b);
3776 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
3777 scandir_filter_f filter, scandir_compar_f compar) {
3779 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
3780 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3781 scandir_filter = filter;
3782 scandir_compar = compar;
3783 // FIXME: under ASan the call below may write to freed memory and corrupt
3784 // its metadata. See
3785 // https://github.com/google/sanitizers/issues/321.
3786 int res = REAL(scandir)(dirp, namelist,
3787 filter ? wrapped_scandir_filter : nullptr,
3788 compar ? wrapped_scandir_compar : nullptr);
3789 scandir_filter = nullptr;
3790 scandir_compar = nullptr;
3791 if (namelist && res > 0) {
3792 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3794 for (int i = 0; i < res; ++i)
3795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3796 (*namelist)[i]->d_reclen);
3800 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
3802 #define INIT_SCANDIR
3805 #if SANITIZER_INTERCEPT_SCANDIR64
3806 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
3807 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
3808 const struct __sanitizer_dirent64 **);
3810 static THREADLOCAL scandir64_filter_f scandir64_filter;
3811 static THREADLOCAL scandir64_compar_f scandir64_compar;
3813 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
3814 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
3815 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
3816 return scandir64_filter(dir);
3819 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
3820 const struct __sanitizer_dirent64 **b) {
3821 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
3822 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
3823 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
3824 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
3825 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
3826 return scandir64_compar(a, b);
3829 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
3830 scandir64_filter_f filter, scandir64_compar_f compar) {
3832 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
3833 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
3834 scandir64_filter = filter;
3835 scandir64_compar = compar;
3836 // FIXME: under ASan the call below may write to freed memory and corrupt
3837 // its metadata. See
3838 // https://github.com/google/sanitizers/issues/321.
3840 REAL(scandir64)(dirp, namelist,
3841 filter ? wrapped_scandir64_filter : nullptr,
3842 compar ? wrapped_scandir64_compar : nullptr);
3843 scandir64_filter = nullptr;
3844 scandir64_compar = nullptr;
3845 if (namelist && res > 0) {
3846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
3847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
3848 for (int i = 0; i < res; ++i)
3849 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
3850 (*namelist)[i]->d_reclen);
3854 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
3856 #define INIT_SCANDIR64
3859 #if SANITIZER_INTERCEPT_GETGROUPS
3860 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
3862 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
3863 // FIXME: under ASan the call below may write to freed memory and corrupt
3864 // its metadata. See
3865 // https://github.com/google/sanitizers/issues/321.
3866 int res = REAL(getgroups)(size, lst);
3867 if (res >= 0 && lst && size > 0)
3868 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
3871 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
3873 #define INIT_GETGROUPS
3876 #if SANITIZER_INTERCEPT_POLL
3877 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
3878 __sanitizer_nfds_t nfds) {
3879 for (unsigned i = 0; i < nfds; ++i) {
3880 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
3881 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
3885 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
3886 __sanitizer_nfds_t nfds) {
3887 for (unsigned i = 0; i < nfds; ++i)
3888 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
3889 sizeof(fds[i].revents));
3892 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3895 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
3896 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3897 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
3898 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3901 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
3906 #if SANITIZER_INTERCEPT_PPOLL
3907 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
3908 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
3910 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
3911 if (fds && nfds) read_pollfd(ctx, fds, nfds);
3913 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
3914 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
3916 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
3917 if (fds && nfds) write_pollfd(ctx, fds, nfds);
3920 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
3925 #if SANITIZER_INTERCEPT_WORDEXP
3926 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
3928 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
3929 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
3930 // FIXME: under ASan the call below may write to freed memory and corrupt
3931 // its metadata. See
3932 // https://github.com/google/sanitizers/issues/321.
3933 int res = REAL(wordexp)(s, p, flags);
3935 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
3937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
3938 sizeof(*p->we_wordv) * p->we_wordc);
3939 for (uptr i = 0; i < p->we_wordc; ++i) {
3940 char *w = p->we_wordv[i];
3941 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
3946 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
3948 #define INIT_WORDEXP
3951 #if SANITIZER_INTERCEPT_SIGWAIT
3952 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
3954 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
3955 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3956 // FIXME: under ASan the call below may write to freed memory and corrupt
3957 // its metadata. See
3958 // https://github.com/google/sanitizers/issues/321.
3959 int res = REAL(sigwait)(set, sig);
3960 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
3963 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
3965 #define INIT_SIGWAIT
3968 #if SANITIZER_INTERCEPT_SIGWAITINFO
3969 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
3971 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
3972 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3973 // FIXME: under ASan the call below may write to freed memory and corrupt
3974 // its metadata. See
3975 // https://github.com/google/sanitizers/issues/321.
3976 int res = REAL(sigwaitinfo)(set, info);
3977 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3980 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
3982 #define INIT_SIGWAITINFO
3985 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
3986 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
3989 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
3990 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3991 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3992 // FIXME: under ASan the call below may write to freed memory and corrupt
3993 // its metadata. See
3994 // https://github.com/google/sanitizers/issues/321.
3995 int res = REAL(sigtimedwait)(set, info, timeout);
3996 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
3999 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4001 #define INIT_SIGTIMEDWAIT
4004 #if SANITIZER_INTERCEPT_SIGSETOPS
4005 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4007 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4008 // FIXME: under ASan the call below may write to freed memory and corrupt
4009 // its metadata. See
4010 // https://github.com/google/sanitizers/issues/321.
4011 int res = REAL(sigemptyset)(set);
4012 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4016 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4018 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4019 // FIXME: under ASan the call below may write to freed memory and corrupt
4020 // its metadata. See
4021 // https://github.com/google/sanitizers/issues/321.
4022 int res = REAL(sigfillset)(set);
4023 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4026 #define INIT_SIGSETOPS \
4027 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4028 COMMON_INTERCEPT_FUNCTION(sigfillset);
4030 #define INIT_SIGSETOPS
4033 #if SANITIZER_INTERCEPT_SIGPENDING
4034 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4036 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4037 // FIXME: under ASan the call below may write to freed memory and corrupt
4038 // its metadata. See
4039 // https://github.com/google/sanitizers/issues/321.
4040 int res = REAL(sigpending)(set);
4041 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4044 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4046 #define INIT_SIGPENDING
4049 #if SANITIZER_INTERCEPT_SIGPROCMASK
4050 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4051 __sanitizer_sigset_t *oldset) {
4053 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4054 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4055 // FIXME: under ASan the call below may write to freed memory and corrupt
4056 // its metadata. See
4057 // https://github.com/google/sanitizers/issues/321.
4058 int res = REAL(sigprocmask)(how, set, oldset);
4060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4063 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4065 #define INIT_SIGPROCMASK
4068 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4069 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4070 __sanitizer_sigset_t *oldset) {
4072 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4073 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4074 // FIXME: under ASan the call below may write to freed memory and corrupt
4075 // its metadata. See
4076 // https://github.com/google/sanitizers/issues/321.
4077 int res = REAL(pthread_sigmask)(how, set, oldset);
4079 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4082 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4084 #define INIT_PTHREAD_SIGMASK
4087 #if SANITIZER_INTERCEPT_BACKTRACE
4088 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4090 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4091 // FIXME: under ASan the call below may write to freed memory and corrupt
4092 // its metadata. See
4093 // https://github.com/google/sanitizers/issues/321.
4094 int res = REAL(backtrace)(buffer, size);
4096 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4100 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4102 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4104 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4105 // FIXME: under ASan the call below may write to freed memory and corrupt
4106 // its metadata. See
4107 // https://github.com/google/sanitizers/issues/321.
4108 char **res = REAL(backtrace_symbols)(buffer, size);
4110 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4111 for (int i = 0; i < size; ++i)
4112 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
4116 #define INIT_BACKTRACE \
4117 COMMON_INTERCEPT_FUNCTION(backtrace); \
4118 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4120 #define INIT_BACKTRACE
4123 #if SANITIZER_INTERCEPT__EXIT
4124 INTERCEPTOR(void, _exit, int status) {
4126 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4127 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4128 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4129 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4130 if (status == 0) status = status1;
4131 REAL(_exit)(status);
4133 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4138 #if SANITIZER_INTERCEPT_PTHREAD_MUTEX
4139 INTERCEPTOR(int, pthread_mutex_lock, void *m) {
4141 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
4142 COMMON_INTERCEPTOR_MUTEX_PRE_LOCK(ctx, m);
4143 int res = REAL(pthread_mutex_lock)(m);
4144 if (res == errno_EOWNERDEAD)
4145 COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
4146 if (res == 0 || res == errno_EOWNERDEAD)
4147 COMMON_INTERCEPTOR_MUTEX_POST_LOCK(ctx, m);
4148 if (res == errno_EINVAL)
4149 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4153 INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
4155 COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
4156 COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
4157 int res = REAL(pthread_mutex_unlock)(m);
4158 if (res == errno_EINVAL)
4159 COMMON_INTERCEPTOR_MUTEX_INVALID(ctx, m);
4163 #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
4164 #define INIT_PTHREAD_MUTEX_UNLOCK \
4165 COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
4167 #define INIT_PTHREAD_MUTEX_LOCK
4168 #define INIT_PTHREAD_MUTEX_UNLOCK
4171 #if SANITIZER_INTERCEPT___PTHREAD_MUTEX
4172 INTERCEPTOR(int, __pthread_mutex_lock, void *m) {
4173 return WRAP(pthread_mutex_lock)(m);
4176 INTERCEPTOR(int, __pthread_mutex_unlock, void *m) {
4177 return WRAP(pthread_mutex_unlock)(m);
4180 #define INIT___PTHREAD_MUTEX_LOCK \
4181 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_lock)
4182 #define INIT___PTHREAD_MUTEX_UNLOCK \
4183 COMMON_INTERCEPT_FUNCTION(__pthread_mutex_unlock)
4185 #define INIT___PTHREAD_MUTEX_LOCK
4186 #define INIT___PTHREAD_MUTEX_UNLOCK
4189 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4190 INTERCEPTOR(int, __libc_mutex_lock, void *m)
4191 ALIAS(WRAPPER_NAME(pthread_mutex_lock));
4193 INTERCEPTOR(int, __libc_mutex_unlock, void *m)
4194 ALIAS(WRAPPER_NAME(pthread_mutex_unlock));
4196 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4197 ALIAS(WRAPPER_NAME(pthread_setcancelstate));
4199 #define INIT___LIBC_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock)
4200 #define INIT___LIBC_MUTEX_UNLOCK COMMON_INTERCEPT_FUNCTION(__libc_mutex_unlock)
4201 #define INIT___LIBC_THR_SETCANCELSTATE \
4202 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4204 #define INIT___LIBC_MUTEX_LOCK
4205 #define INIT___LIBC_MUTEX_UNLOCK
4206 #define INIT___LIBC_THR_SETCANCELSTATE
4209 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4210 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4212 if (mnt->mnt_fsname)
4213 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4214 REAL(strlen)(mnt->mnt_fsname) + 1);
4216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4217 REAL(strlen)(mnt->mnt_dir) + 1);
4219 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4220 REAL(strlen)(mnt->mnt_type) + 1);
4222 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4223 REAL(strlen)(mnt->mnt_opts) + 1);
4227 #if SANITIZER_INTERCEPT_GETMNTENT
4228 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4230 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4231 __sanitizer_mntent *res = REAL(getmntent)(fp);
4232 if (res) write_mntent(ctx, res);
4235 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4237 #define INIT_GETMNTENT
4240 #if SANITIZER_INTERCEPT_GETMNTENT_R
4241 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4242 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4244 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4245 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4246 if (res) write_mntent(ctx, res);
4249 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4251 #define INIT_GETMNTENT_R
4254 #if SANITIZER_INTERCEPT_STATFS
4255 INTERCEPTOR(int, statfs, char *path, void *buf) {
4257 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4258 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4259 // FIXME: under ASan the call below may write to freed memory and corrupt
4260 // its metadata. See
4261 // https://github.com/google/sanitizers/issues/321.
4262 int res = REAL(statfs)(path, buf);
4263 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4266 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4268 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4269 // FIXME: under ASan the call below may write to freed memory and corrupt
4270 // its metadata. See
4271 // https://github.com/google/sanitizers/issues/321.
4272 int res = REAL(fstatfs)(fd, buf);
4273 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4276 #define INIT_STATFS \
4277 COMMON_INTERCEPT_FUNCTION(statfs); \
4278 COMMON_INTERCEPT_FUNCTION(fstatfs);
4283 #if SANITIZER_INTERCEPT_STATFS64
4284 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4286 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4287 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4288 // FIXME: under ASan the call below may write to freed memory and corrupt
4289 // its metadata. See
4290 // https://github.com/google/sanitizers/issues/321.
4291 int res = REAL(statfs64)(path, buf);
4292 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4295 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4297 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4298 // FIXME: under ASan the call below may write to freed memory and corrupt
4299 // its metadata. See
4300 // https://github.com/google/sanitizers/issues/321.
4301 int res = REAL(fstatfs64)(fd, buf);
4302 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4305 #define INIT_STATFS64 \
4306 COMMON_INTERCEPT_FUNCTION(statfs64); \
4307 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4309 #define INIT_STATFS64
4312 #if SANITIZER_INTERCEPT_STATVFS
4313 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4315 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4316 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4317 // FIXME: under ASan the call below may write to freed memory and corrupt
4318 // its metadata. See
4319 // https://github.com/google/sanitizers/issues/321.
4320 int res = REAL(statvfs)(path, buf);
4321 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4324 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4326 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4327 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4328 // FIXME: under ASan the call below may write to freed memory and corrupt
4329 // its metadata. See
4330 // https://github.com/google/sanitizers/issues/321.
4331 int res = REAL(fstatvfs)(fd, buf);
4333 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4335 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4339 #define INIT_STATVFS \
4340 COMMON_INTERCEPT_FUNCTION(statvfs); \
4341 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4343 #define INIT_STATVFS
4346 #if SANITIZER_INTERCEPT_STATVFS64
4347 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4349 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4350 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
4351 // FIXME: under ASan the call below may write to freed memory and corrupt
4352 // its metadata. See
4353 // https://github.com/google/sanitizers/issues/321.
4354 int res = REAL(statvfs64)(path, buf);
4355 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4358 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4360 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4361 // FIXME: under ASan the call below may write to freed memory and corrupt
4362 // its metadata. See
4363 // https://github.com/google/sanitizers/issues/321.
4364 int res = REAL(fstatvfs64)(fd, buf);
4365 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4368 #define INIT_STATVFS64 \
4369 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4370 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4372 #define INIT_STATVFS64
4375 #if SANITIZER_INTERCEPT_INITGROUPS
4376 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4378 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4379 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
4380 int res = REAL(initgroups)(user, group);
4383 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4385 #define INIT_INITGROUPS
4388 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4389 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4391 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4392 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4393 char *res = REAL(ether_ntoa)(addr);
4394 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4397 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4399 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4400 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4401 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4402 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4405 #define INIT_ETHER_NTOA_ATON \
4406 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4407 COMMON_INTERCEPT_FUNCTION(ether_aton);
4409 #define INIT_ETHER_NTOA_ATON
4412 #if SANITIZER_INTERCEPT_ETHER_HOST
4413 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4415 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4416 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4417 // FIXME: under ASan the call below may write to freed memory and corrupt
4418 // its metadata. See
4419 // https://github.com/google/sanitizers/issues/321.
4420 int res = REAL(ether_ntohost)(hostname, addr);
4421 if (!res && hostname)
4422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4425 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4427 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4429 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4430 // FIXME: under ASan the call below may write to freed memory and corrupt
4431 // its metadata. See
4432 // https://github.com/google/sanitizers/issues/321.
4433 int res = REAL(ether_hostton)(hostname, addr);
4434 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4437 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4440 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4441 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
4442 // FIXME: under ASan the call below may write to freed memory and corrupt
4443 // its metadata. See
4444 // https://github.com/google/sanitizers/issues/321.
4445 int res = REAL(ether_line)(line, addr, hostname);
4447 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4449 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
4453 #define INIT_ETHER_HOST \
4454 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4455 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4456 COMMON_INTERCEPT_FUNCTION(ether_line);
4458 #define INIT_ETHER_HOST
4461 #if SANITIZER_INTERCEPT_ETHER_R
4462 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4464 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4465 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4466 // FIXME: under ASan the call below may write to freed memory and corrupt
4467 // its metadata. See
4468 // https://github.com/google/sanitizers/issues/321.
4469 char *res = REAL(ether_ntoa_r)(addr, buf);
4470 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
4473 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4474 __sanitizer_ether_addr *addr) {
4476 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4477 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
4478 // FIXME: under ASan the call below may write to freed memory and corrupt
4479 // its metadata. See
4480 // https://github.com/google/sanitizers/issues/321.
4481 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4482 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4485 #define INIT_ETHER_R \
4486 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4487 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4489 #define INIT_ETHER_R
4492 #if SANITIZER_INTERCEPT_SHMCTL
4493 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4495 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4496 // FIXME: under ASan the call below may write to freed memory and corrupt
4497 // its metadata. See
4498 // https://github.com/google/sanitizers/issues/321.
4499 int res = REAL(shmctl)(shmid, cmd, buf);
4502 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4503 sz = sizeof(__sanitizer_shmid_ds);
4504 else if (cmd == shmctl_ipc_info)
4505 sz = struct_shminfo_sz;
4506 else if (cmd == shmctl_shm_info)
4507 sz = struct_shm_info_sz;
4508 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4512 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4517 #if SANITIZER_INTERCEPT_RANDOM_R
4518 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4520 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4521 // FIXME: under ASan the call below may write to freed memory and corrupt
4522 // its metadata. See
4523 // https://github.com/google/sanitizers/issues/321.
4524 int res = REAL(random_r)(buf, result);
4526 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4529 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4531 #define INIT_RANDOM_R
4534 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4535 // its metadata. See
4536 // https://github.com/google/sanitizers/issues/321.
4537 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4538 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4539 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4540 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4541 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4542 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4543 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4544 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4545 INTERCEPTOR(int, fn, void *attr, void *r) { \
4547 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4548 int res = REAL(fn)(attr, r); \
4549 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4552 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4553 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4554 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4555 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4556 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4557 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4558 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4559 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4560 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4561 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4564 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4565 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4566 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4567 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4568 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4569 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4571 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4572 // FIXME: under ASan the call below may write to freed memory and corrupt
4573 // its metadata. See
4574 // https://github.com/google/sanitizers/issues/321.
4575 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4577 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4578 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4583 // We may need to call the real pthread_attr_getstack from the run-time
4584 // in sanitizer_common, but we don't want to include the interception headers
4585 // there. So, just define this function here.
4586 namespace __sanitizer {
4588 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4589 return REAL(pthread_attr_getstack)(attr, addr, size);
4592 } // namespace __sanitizer
4594 #define INIT_PTHREAD_ATTR_GET \
4595 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4596 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4597 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4598 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4599 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4601 #define INIT_PTHREAD_ATTR_GET
4604 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4605 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4606 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4608 #define INIT_PTHREAD_ATTR_GET_SCHED \
4609 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4610 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4612 #define INIT_PTHREAD_ATTR_GET_SCHED
4615 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4616 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4618 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4619 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4621 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4624 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4625 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4628 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4630 // FIXME: under ASan the call below may write to freed memory and corrupt
4631 // its metadata. See
4632 // https://github.com/google/sanitizers/issues/321.
4633 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4634 if (!res && cpusetsize && cpuset)
4635 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4639 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4640 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4642 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4645 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4646 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4647 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4648 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4650 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4653 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4654 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4655 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4656 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4658 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4661 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4662 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4663 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4664 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4666 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4669 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4670 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4671 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4672 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4674 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4677 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4678 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4679 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4680 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4682 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4685 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4686 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4687 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4688 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4690 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4693 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4694 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4695 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4696 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4698 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4701 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4702 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4703 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4704 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4706 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4709 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
4710 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
4711 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
4712 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
4714 #define INIT_PTHREAD_CONDATTR_GETPSHARED
4717 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
4718 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
4719 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
4720 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
4722 #define INIT_PTHREAD_CONDATTR_GETCLOCK
4725 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
4726 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
4727 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
4728 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
4730 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
4733 #if SANITIZER_INTERCEPT_TMPNAM
4734 INTERCEPTOR(char *, tmpnam, char *s) {
4736 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
4737 char *res = REAL(tmpnam)(s);
4740 // FIXME: under ASan the call below may write to freed memory and corrupt
4741 // its metadata. See
4742 // https://github.com/google/sanitizers/issues/321.
4743 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4745 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4749 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
4754 #if SANITIZER_INTERCEPT_TMPNAM_R
4755 INTERCEPTOR(char *, tmpnam_r, char *s) {
4757 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
4758 // FIXME: under ASan the call below may write to freed memory and corrupt
4759 // its metadata. See
4760 // https://github.com/google/sanitizers/issues/321.
4761 char *res = REAL(tmpnam_r)(s);
4762 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
4765 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
4767 #define INIT_TMPNAM_R
4770 #if SANITIZER_INTERCEPT_TTYNAME
4771 INTERCEPTOR(char *, ttyname, int fd) {
4773 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
4774 char *res = REAL(ttyname)(fd);
4776 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4779 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
4781 #define INIT_TTYNAME
4784 #if SANITIZER_INTERCEPT_TTYNAME_R
4785 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
4787 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
4788 int res = REAL(ttyname_r)(fd, name, namesize);
4790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4793 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
4795 #define INIT_TTYNAME_R
4798 #if SANITIZER_INTERCEPT_TEMPNAM
4799 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
4801 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
4802 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
4803 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
4804 char *res = REAL(tempnam)(dir, pfx);
4805 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4808 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
4810 #define INIT_TEMPNAM
4813 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
4814 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
4816 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
4817 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4818 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
4819 return REAL(pthread_setname_np)(thread, name);
4821 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4822 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
4823 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
4825 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
4826 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
4827 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
4828 internal_snprintf(newname, sizeof(newname), name, arg);
4829 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
4830 return REAL(pthread_setname_np)(thread, name, arg);
4832 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
4834 #define INIT_PTHREAD_SETNAME_NP
4837 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
4838 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
4840 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
4841 int res = REAL(pthread_getname_np)(thread, name, len);
4843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
4846 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
4848 #define INIT_PTHREAD_GETNAME_NP
4851 #if SANITIZER_INTERCEPT_SINCOS
4852 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
4854 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
4855 // FIXME: under ASan the call below may write to freed memory and corrupt
4856 // its metadata. See
4857 // https://github.com/google/sanitizers/issues/321.
4858 REAL(sincos)(x, sin, cos);
4859 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4860 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4862 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
4864 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
4865 // FIXME: under ASan the call below may write to freed memory and corrupt
4866 // its metadata. See
4867 // https://github.com/google/sanitizers/issues/321.
4868 REAL(sincosf)(x, sin, cos);
4869 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4870 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4872 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
4874 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
4875 // FIXME: under ASan the call below may write to freed memory and corrupt
4876 // its metadata. See
4877 // https://github.com/google/sanitizers/issues/321.
4878 REAL(sincosl)(x, sin, cos);
4879 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
4880 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
4882 #define INIT_SINCOS \
4883 COMMON_INTERCEPT_FUNCTION(sincos); \
4884 COMMON_INTERCEPT_FUNCTION(sincosf); \
4885 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
4890 #if SANITIZER_INTERCEPT_REMQUO
4891 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
4893 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
4894 // FIXME: under ASan the call below may write to freed memory and corrupt
4895 // its metadata. See
4896 // https://github.com/google/sanitizers/issues/321.
4897 double res = REAL(remquo)(x, y, quo);
4898 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4901 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
4903 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
4904 // FIXME: under ASan the call below may write to freed memory and corrupt
4905 // its metadata. See
4906 // https://github.com/google/sanitizers/issues/321.
4907 float res = REAL(remquof)(x, y, quo);
4908 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4911 #define INIT_REMQUO \
4912 COMMON_INTERCEPT_FUNCTION(remquo); \
4913 COMMON_INTERCEPT_FUNCTION(remquof);
4918 #if SANITIZER_INTERCEPT_REMQUOL
4919 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
4921 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
4922 // FIXME: under ASan the call below may write to freed memory and corrupt
4923 // its metadata. See
4924 // https://github.com/google/sanitizers/issues/321.
4925 long double res = REAL(remquol)(x, y, quo);
4926 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
4929 #define INIT_REMQUOL \
4930 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
4932 #define INIT_REMQUOL
4935 #if SANITIZER_INTERCEPT_LGAMMA
4937 INTERCEPTOR(double, lgamma, double x) {
4939 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
4940 double res = REAL(lgamma)(x);
4941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4944 INTERCEPTOR(float, lgammaf, float x) {
4946 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
4947 float res = REAL(lgammaf)(x);
4948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4951 #define INIT_LGAMMA \
4952 COMMON_INTERCEPT_FUNCTION(lgamma); \
4953 COMMON_INTERCEPT_FUNCTION(lgammaf);
4958 #if SANITIZER_INTERCEPT_LGAMMAL
4959 INTERCEPTOR(long double, lgammal, long double x) {
4961 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
4962 long double res = REAL(lgammal)(x);
4963 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
4966 #define INIT_LGAMMAL \
4967 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
4969 #define INIT_LGAMMAL
4972 #if SANITIZER_INTERCEPT_LGAMMA_R
4973 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
4975 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
4976 // FIXME: under ASan the call below may write to freed memory and corrupt
4977 // its metadata. See
4978 // https://github.com/google/sanitizers/issues/321.
4979 double res = REAL(lgamma_r)(x, signp);
4980 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4983 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
4985 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
4986 // FIXME: under ASan the call below may write to freed memory and corrupt
4987 // its metadata. See
4988 // https://github.com/google/sanitizers/issues/321.
4989 float res = REAL(lgammaf_r)(x, signp);
4990 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
4993 #define INIT_LGAMMA_R \
4994 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
4995 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
4997 #define INIT_LGAMMA_R
5000 #if SANITIZER_INTERCEPT_LGAMMAL_R
5001 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5003 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5004 // FIXME: under ASan the call below may write to freed memory and corrupt
5005 // its metadata. See
5006 // https://github.com/google/sanitizers/issues/321.
5007 long double res = REAL(lgammal_r)(x, signp);
5008 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5011 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5013 #define INIT_LGAMMAL_R
5016 #if SANITIZER_INTERCEPT_DRAND48_R
5017 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5019 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5020 // FIXME: under ASan the call below may write to freed memory and corrupt
5021 // its metadata. See
5022 // https://github.com/google/sanitizers/issues/321.
5023 int res = REAL(drand48_r)(buffer, result);
5024 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5027 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5029 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5030 // FIXME: under ASan the call below may write to freed memory and corrupt
5031 // its metadata. See
5032 // https://github.com/google/sanitizers/issues/321.
5033 int res = REAL(lrand48_r)(buffer, result);
5034 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5037 #define INIT_DRAND48_R \
5038 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5039 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5041 #define INIT_DRAND48_R
5044 #if SANITIZER_INTERCEPT_RAND_R
5045 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5047 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5048 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5049 return REAL(rand_r)(seedp);
5051 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5056 #if SANITIZER_INTERCEPT_GETLINE
5057 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5059 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5060 // FIXME: under ASan the call below may write to freed memory and corrupt
5061 // its metadata. See
5062 // https://github.com/google/sanitizers/issues/321.
5063 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5065 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5066 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5067 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5072 // FIXME: under ASan the call below may write to freed memory and corrupt its
5074 // https://github.com/google/sanitizers/issues/321.
5075 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5078 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5079 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5082 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5088 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5090 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5092 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5093 // with its own body.
5094 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5096 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5098 #define INIT_GETLINE \
5099 COMMON_INTERCEPT_FUNCTION(getline); \
5100 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5101 COMMON_INTERCEPT_FUNCTION(getdelim);
5103 #define INIT_GETLINE
5106 #if SANITIZER_INTERCEPT_ICONV
5107 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5108 char **outbuf, SIZE_T *outbytesleft) {
5110 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5113 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5114 if (inbuf && inbytesleft)
5115 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5117 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5118 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5119 // FIXME: under ASan the call below may write to freed memory and corrupt
5120 // its metadata. See
5121 // https://github.com/google/sanitizers/issues/321.
5122 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5123 if (outbuf && *outbuf > outbuf_orig) {
5124 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5125 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5129 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5134 #if SANITIZER_INTERCEPT_TIMES
5135 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5137 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5138 // FIXME: under ASan the call below may write to freed memory and corrupt
5139 // its metadata. See
5140 // https://github.com/google/sanitizers/issues/321.
5141 __sanitizer_clock_t res = REAL(times)(tms);
5142 if (res != (__sanitizer_clock_t)-1 && tms)
5143 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5146 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5151 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5153 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5154 // If you see any crashes around this functions, there are 2 known issues with
5155 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5156 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5157 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5158 // to access thread local variables (it should not happen normally,
5159 // because sanitizers use initial-exec tls model).
5160 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5162 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5163 void *res = REAL(__tls_get_addr)(arg);
5164 uptr tls_begin, tls_end;
5165 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5166 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5168 // New DTLS block has been allocated.
5169 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5174 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5175 // mostly the same semantics as __tls_get_addr, but its presence enables
5176 // some optimizations in linker (which are safe to ignore here).
5177 extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
5178 visibility("default")))
5179 void *__tls_get_addr_opt(void *arg);
5181 #else // SANITIZER_S390
5182 // On s390, we have to intercept two functions here:
5183 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5184 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5185 // a proper pointer. It is used by dlsym for TLS symbols.
5186 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5187 // descriptor offset as an argument instead of a pointer. GOT address
5188 // is passed in r12, so it's necessary to write it in assembly. This is
5189 // the function used by the compiler.
5190 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5191 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5192 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5193 extern "C" uptr __tls_get_offset(void *arg);
5194 extern "C" uptr __interceptor___tls_get_offset(void *arg);
5195 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5197 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5198 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5199 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5200 void *ptr = reinterpret_cast<void *>(res + tp);
5201 uptr tls_begin, tls_end;
5202 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5203 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5205 // New DTLS block has been allocated.
5206 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5210 // We need a hidden symbol aliasing the above, so that we can jump
5211 // directly to it from the assembly below.
5212 extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
5213 visibility("hidden")))
5214 uptr __tls_get_addr_hidden(void *arg);
5215 // Now carefully intercept __tls_get_offset.
5218 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5219 // exports our symbol.
5220 ".weak __tls_get_offset\n"
5221 ".type __tls_get_offset, @function\n"
5222 "__tls_get_offset:\n"
5223 ".global __interceptor___tls_get_offset\n"
5224 ".type __interceptor___tls_get_offset, @function\n"
5225 "__interceptor___tls_get_offset:\n"
5227 "la %r2, 0(%r2,%r12)\n"
5228 "jg __tls_get_addr_hidden\n"
5231 "0: la %r2,0(%r2,%r12)\n"
5232 "l %r4,1f-0b(%r3)\n"
5234 "1: .long __tls_get_addr_hidden - 0b\n"
5236 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5237 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5238 ".type __tls_get_offset_wrapper, @function\n"
5239 "__tls_get_offset_wrapper:\n"
5246 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5248 #endif // SANITIZER_S390
5250 #define INIT_TLS_GET_ADDR
5253 #if SANITIZER_INTERCEPT_LISTXATTR
5254 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5256 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5257 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5258 // FIXME: under ASan the call below may write to freed memory and corrupt
5259 // its metadata. See
5260 // https://github.com/google/sanitizers/issues/321.
5261 SSIZE_T res = REAL(listxattr)(path, list, size);
5262 // Here and below, size == 0 is a special case where nothing is written to the
5263 // buffer, and res contains the desired buffer size.
5264 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5267 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5269 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5270 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5271 // FIXME: under ASan the call below may write to freed memory and corrupt
5272 // its metadata. See
5273 // https://github.com/google/sanitizers/issues/321.
5274 SSIZE_T res = REAL(llistxattr)(path, list, size);
5275 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5278 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5280 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5281 // FIXME: under ASan the call below may write to freed memory and corrupt
5282 // its metadata. See
5283 // https://github.com/google/sanitizers/issues/321.
5284 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5285 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5288 #define INIT_LISTXATTR \
5289 COMMON_INTERCEPT_FUNCTION(listxattr); \
5290 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5291 COMMON_INTERCEPT_FUNCTION(flistxattr);
5293 #define INIT_LISTXATTR
5296 #if SANITIZER_INTERCEPT_GETXATTR
5297 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5300 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5301 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5302 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5303 // FIXME: under ASan the call below may write to freed memory and corrupt
5304 // its metadata. See
5305 // https://github.com/google/sanitizers/issues/321.
5306 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5307 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5310 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5313 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5314 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5315 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5316 // FIXME: under ASan the call below may write to freed memory and corrupt
5317 // its metadata. See
5318 // https://github.com/google/sanitizers/issues/321.
5319 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5320 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5323 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5326 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5327 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
5328 // FIXME: under ASan the call below may write to freed memory and corrupt
5329 // its metadata. See
5330 // https://github.com/google/sanitizers/issues/321.
5331 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5332 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5335 #define INIT_GETXATTR \
5336 COMMON_INTERCEPT_FUNCTION(getxattr); \
5337 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5338 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5340 #define INIT_GETXATTR
5343 #if SANITIZER_INTERCEPT_GETRESID
5344 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5346 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5347 // FIXME: under ASan the call below may write to freed memory and corrupt
5348 // its metadata. See
5349 // https://github.com/google/sanitizers/issues/321.
5350 int res = REAL(getresuid)(ruid, euid, suid);
5352 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5353 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5354 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5358 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5360 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5361 // FIXME: under ASan the call below may write to freed memory and corrupt
5362 // its metadata. See
5363 // https://github.com/google/sanitizers/issues/321.
5364 int res = REAL(getresgid)(rgid, egid, sgid);
5366 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5367 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5368 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5372 #define INIT_GETRESID \
5373 COMMON_INTERCEPT_FUNCTION(getresuid); \
5374 COMMON_INTERCEPT_FUNCTION(getresgid);
5376 #define INIT_GETRESID
5379 #if SANITIZER_INTERCEPT_GETIFADDRS
5380 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5381 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5382 // intercept it to poison the memory again.
5383 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5385 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5386 // FIXME: under ASan the call below may write to freed memory and corrupt
5387 // its metadata. See
5388 // https://github.com/google/sanitizers/issues/321.
5389 int res = REAL(getifaddrs)(ifap);
5390 if (res == 0 && ifap) {
5391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5392 __sanitizer_ifaddrs *p = *ifap;
5394 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5397 REAL(strlen)(p->ifa_name) + 1);
5399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5402 // On Linux this is a union, but the other member also points to a
5403 // struct sockaddr, so the following is sufficient.
5405 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5406 // FIXME(smatveev): Unpoison p->ifa_data as well.
5412 #define INIT_GETIFADDRS \
5413 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5415 #define INIT_GETIFADDRS
5418 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5419 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5421 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5422 // FIXME: under ASan the call below may write to freed memory and corrupt
5423 // its metadata. See
5424 // https://github.com/google/sanitizers/issues/321.
5425 char *res = REAL(if_indextoname)(ifindex, ifname);
5427 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5430 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5432 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5434 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
5435 return REAL(if_nametoindex)(ifname);
5437 #define INIT_IF_INDEXTONAME \
5438 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5439 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5441 #define INIT_IF_INDEXTONAME
5444 #if SANITIZER_INTERCEPT_CAPGET
5445 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5447 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5449 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5450 // FIXME: under ASan the call below may write to freed memory and corrupt
5451 // its metadata. See
5452 // https://github.com/google/sanitizers/issues/321.
5453 int res = REAL(capget)(hdrp, datap);
5454 if (res == 0 && datap)
5455 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
5456 // We can also return -1 and write to hdrp->version if the version passed in
5457 // hdrp->version is unsupported. But that's not a trivial condition to check,
5458 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5461 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5463 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5465 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5467 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
5468 return REAL(capset)(hdrp, datap);
5470 #define INIT_CAPGET \
5471 COMMON_INTERCEPT_FUNCTION(capget); \
5472 COMMON_INTERCEPT_FUNCTION(capset);
5477 #if SANITIZER_INTERCEPT_AEABI_MEM
5478 INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
5480 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5483 INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
5485 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5488 INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
5490 COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
5493 INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
5495 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5498 INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
5500 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5503 INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
5505 COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
5508 // Note the argument order.
5509 INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
5511 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5514 INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
5516 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5519 INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
5521 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
5524 INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
5526 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5529 INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
5531 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5534 INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
5536 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5539 #define INIT_AEABI_MEM \
5540 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
5541 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
5542 COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
5543 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
5544 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
5545 COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
5546 COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
5547 COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
5548 COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
5549 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
5550 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
5551 COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
5553 #define INIT_AEABI_MEM
5554 #endif // SANITIZER_INTERCEPT_AEABI_MEM
5556 #if SANITIZER_INTERCEPT___BZERO
5557 INTERCEPTOR(void *, __bzero, void *block, uptr size) {
5559 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5561 #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
5563 #define INIT___BZERO
5564 #endif // SANITIZER_INTERCEPT___BZERO
5566 #if SANITIZER_INTERCEPT_BZERO
5567 INTERCEPTOR(void *, bzero, void *block, uptr size) {
5569 COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
5571 #define INIT_BZERO COMMON_INTERCEPT_FUNCTION(bzero);
5574 #endif // SANITIZER_INTERCEPT_BZERO
5576 #if SANITIZER_INTERCEPT_FTIME
5577 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5579 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5580 // FIXME: under ASan the call below may write to freed memory and corrupt
5581 // its metadata. See
5582 // https://github.com/google/sanitizers/issues/321.
5583 int res = REAL(ftime)(tp);
5585 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5588 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5591 #endif // SANITIZER_INTERCEPT_FTIME
5593 #if SANITIZER_INTERCEPT_XDR
5594 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5595 unsigned size, int op) {
5597 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5598 // FIXME: under ASan the call below may write to freed memory and corrupt
5599 // its metadata. See
5600 // https://github.com/google/sanitizers/issues/321.
5601 REAL(xdrmem_create)(xdrs, addr, size, op);
5602 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5603 if (op == __sanitizer_XDR_ENCODE) {
5604 // It's not obvious how much data individual xdr_ routines write.
5605 // Simply unpoison the entire target buffer in advance.
5606 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5610 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5612 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5613 // FIXME: under ASan the call below may write to freed memory and corrupt
5614 // its metadata. See
5615 // https://github.com/google/sanitizers/issues/321.
5616 REAL(xdrstdio_create)(xdrs, file, op);
5617 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5620 // FIXME: under ASan the call below may write to freed memory and corrupt
5621 // its metadata. See
5622 // https://github.com/google/sanitizers/issues/321.
5623 #define XDR_INTERCEPTOR(F, T) \
5624 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5626 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5627 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5628 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5629 int res = REAL(F)(xdrs, p); \
5630 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5631 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5635 XDR_INTERCEPTOR(xdr_short, short)
5636 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5637 XDR_INTERCEPTOR(xdr_int, int)
5638 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5639 XDR_INTERCEPTOR(xdr_long, long)
5640 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5641 XDR_INTERCEPTOR(xdr_hyper, long long)
5642 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5643 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5644 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5645 XDR_INTERCEPTOR(xdr_int8_t, u8)
5646 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5647 XDR_INTERCEPTOR(xdr_int16_t, u16)
5648 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5649 XDR_INTERCEPTOR(xdr_int32_t, u32)
5650 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5651 XDR_INTERCEPTOR(xdr_int64_t, u64)
5652 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5653 XDR_INTERCEPTOR(xdr_quad_t, long long)
5654 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5655 XDR_INTERCEPTOR(xdr_bool, bool)
5656 XDR_INTERCEPTOR(xdr_enum, int)
5657 XDR_INTERCEPTOR(xdr_char, char)
5658 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5659 XDR_INTERCEPTOR(xdr_float, float)
5660 XDR_INTERCEPTOR(xdr_double, double)
5662 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5663 // wrapstring, sizeof
5665 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5668 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5669 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5670 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5671 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5672 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5674 // FIXME: under ASan the call below may write to freed memory and corrupt
5675 // its metadata. See
5676 // https://github.com/google/sanitizers/issues/321.
5677 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5678 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5680 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5681 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5686 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5689 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5690 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5691 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5692 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5694 // FIXME: under ASan the call below may write to freed memory and corrupt
5695 // its metadata. See
5696 // https://github.com/google/sanitizers/issues/321.
5697 int res = REAL(xdr_string)(xdrs, p, maxsize);
5698 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5699 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5701 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
5707 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5708 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5709 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5710 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5711 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5712 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5713 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5714 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5715 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5716 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5717 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5718 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5719 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5720 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5721 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5722 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5723 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5724 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5725 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5726 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5727 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5728 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5729 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5730 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5731 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5732 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5733 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5734 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5735 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5736 COMMON_INTERCEPT_FUNCTION(xdr_string);
5739 #endif // SANITIZER_INTERCEPT_XDR
5741 #if SANITIZER_INTERCEPT_TSEARCH
5742 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
5743 int (*compar)(const void *, const void *)) {
5745 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
5746 // FIXME: under ASan the call below may write to freed memory and corrupt
5747 // its metadata. See
5748 // https://github.com/google/sanitizers/issues/321.
5749 void *res = REAL(tsearch)(key, rootp, compar);
5750 if (res && *(void **)res == key)
5751 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
5754 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
5756 #define INIT_TSEARCH
5759 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
5760 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5761 void unpoison_file(__sanitizer_FILE *fp) {
5762 #if SANITIZER_HAS_STRUCT_FILE
5763 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
5764 #if SANITIZER_NETBSD
5765 if (fp->_bf._base && fp->_bf._size > 0)
5766 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
5769 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
5770 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
5771 fp->_IO_read_end - fp->_IO_read_base);
5773 #endif // SANITIZER_HAS_STRUCT_FILE
5777 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
5778 // These guys are called when a .c source is built with -O2.
5779 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
5781 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
5782 int res = REAL(__uflow)(fp);
5786 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
5788 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
5789 int res = REAL(__underflow)(fp);
5793 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
5795 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
5796 int res = REAL(__overflow)(fp, ch);
5800 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
5802 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
5803 int res = REAL(__wuflow)(fp);
5807 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
5809 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
5810 int res = REAL(__wunderflow)(fp);
5814 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
5816 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
5817 int res = REAL(__woverflow)(fp, ch);
5821 #define INIT_LIBIO_INTERNALS \
5822 COMMON_INTERCEPT_FUNCTION(__uflow); \
5823 COMMON_INTERCEPT_FUNCTION(__underflow); \
5824 COMMON_INTERCEPT_FUNCTION(__overflow); \
5825 COMMON_INTERCEPT_FUNCTION(__wuflow); \
5826 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
5827 COMMON_INTERCEPT_FUNCTION(__woverflow);
5829 #define INIT_LIBIO_INTERNALS
5832 #if SANITIZER_INTERCEPT_FOPEN
5833 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
5835 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
5836 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5837 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5838 __sanitizer_FILE *res = REAL(fopen)(path, mode);
5839 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5840 if (res) unpoison_file(res);
5843 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
5845 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
5846 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5847 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
5848 if (res) unpoison_file(res);
5851 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
5852 __sanitizer_FILE *fp) {
5854 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
5855 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5856 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5857 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5858 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
5859 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5860 if (res) unpoison_file(res);
5863 #define INIT_FOPEN \
5864 COMMON_INTERCEPT_FUNCTION(fopen); \
5865 COMMON_INTERCEPT_FUNCTION(fdopen); \
5866 COMMON_INTERCEPT_FUNCTION(freopen);
5871 #if SANITIZER_INTERCEPT_FOPEN64
5872 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
5874 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
5875 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5876 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5877 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
5878 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5879 if (res) unpoison_file(res);
5882 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
5883 __sanitizer_FILE *fp) {
5885 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
5886 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
5887 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
5888 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
5889 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
5890 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
5891 if (res) unpoison_file(res);
5894 #define INIT_FOPEN64 \
5895 COMMON_INTERCEPT_FUNCTION(fopen64); \
5896 COMMON_INTERCEPT_FUNCTION(freopen64);
5898 #define INIT_FOPEN64
5901 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
5902 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
5904 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
5905 // FIXME: under ASan the call below may write to freed memory and corrupt
5906 // its metadata. See
5907 // https://github.com/google/sanitizers/issues/321.
5908 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
5910 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5913 FileMetadata file = {ptr, sizeloc};
5914 SetInterceptorMetadata(res, file);
5918 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
5921 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
5922 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
5924 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
5925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
5927 FileMetadata file = {(char **)ptr, sizeloc};
5928 SetInterceptorMetadata(res, file);
5932 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
5935 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
5936 // FIXME: under ASan the call below may write to freed memory and corrupt
5937 // its metadata. See
5938 // https://github.com/google/sanitizers/issues/321.
5939 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
5940 if (res) unpoison_file(res);
5943 #define INIT_OPEN_MEMSTREAM \
5944 COMMON_INTERCEPT_FUNCTION(open_memstream); \
5945 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
5946 COMMON_INTERCEPT_FUNCTION(fmemopen);
5948 #define INIT_OPEN_MEMSTREAM
5951 #if SANITIZER_INTERCEPT_OBSTACK
5952 static void initialize_obstack(__sanitizer_obstack *obstack) {
5953 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
5955 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
5956 sizeof(*obstack->chunk));
5959 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
5960 int align, void *(*alloc_fn)(uptr arg, uptr sz),
5961 void (*free_fn)(uptr arg, void *p)) {
5963 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
5965 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
5966 if (res) initialize_obstack(obstack);
5969 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
5970 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
5972 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
5974 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
5975 if (res) initialize_obstack(obstack);
5978 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
5980 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
5981 REAL(_obstack_newchunk)(obstack, length);
5983 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
5984 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
5986 #define INIT_OBSTACK \
5987 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
5988 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
5989 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
5991 #define INIT_OBSTACK
5994 #if SANITIZER_INTERCEPT_FFLUSH
5995 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
5997 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
5998 int res = REAL(fflush)(fp);
5999 // FIXME: handle fp == NULL
6001 const FileMetadata *m = GetInterceptorMetadata(fp);
6002 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6006 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6011 #if SANITIZER_INTERCEPT_FCLOSE
6012 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6014 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6015 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6016 const FileMetadata *m = GetInterceptorMetadata(fp);
6017 int res = REAL(fclose)(fp);
6019 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6020 DeleteInterceptorMetadata(fp);
6024 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6029 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6030 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6032 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6033 if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6034 COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag);
6035 void *res = REAL(dlopen)(filename, flag);
6036 Symbolizer::GetOrInit()->InvalidateModuleList();
6037 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6041 INTERCEPTOR(int, dlclose, void *handle) {
6043 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6044 int res = REAL(dlclose)(handle);
6045 Symbolizer::GetOrInit()->InvalidateModuleList();
6046 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6049 #define INIT_DLOPEN_DLCLOSE \
6050 COMMON_INTERCEPT_FUNCTION(dlopen); \
6051 COMMON_INTERCEPT_FUNCTION(dlclose);
6053 #define INIT_DLOPEN_DLCLOSE
6056 #if SANITIZER_INTERCEPT_GETPASS
6057 INTERCEPTOR(char *, getpass, const char *prompt) {
6059 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6061 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
6062 char *res = REAL(getpass)(prompt);
6063 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
6067 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6069 #define INIT_GETPASS
6072 #if SANITIZER_INTERCEPT_TIMERFD
6073 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6076 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6078 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6079 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6080 if (res != -1 && old_value)
6081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6085 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6087 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6088 int res = REAL(timerfd_gettime)(fd, curr_value);
6089 if (res != -1 && curr_value)
6090 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6093 #define INIT_TIMERFD \
6094 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6095 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6097 #define INIT_TIMERFD
6100 #if SANITIZER_INTERCEPT_MLOCKX
6101 // Linux kernel has a bug that leads to kernel deadlock if a process
6102 // maps TBs of memory and then calls mlock().
6103 static void MlockIsUnsupported() {
6104 static atomic_uint8_t printed;
6105 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6107 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6111 INTERCEPTOR(int, mlock, const void *addr, uptr len) {
6112 MlockIsUnsupported();
6116 INTERCEPTOR(int, munlock, const void *addr, uptr len) {
6117 MlockIsUnsupported();
6121 INTERCEPTOR(int, mlockall, int flags) {
6122 MlockIsUnsupported();
6126 INTERCEPTOR(int, munlockall, void) {
6127 MlockIsUnsupported();
6131 #define INIT_MLOCKX \
6132 COMMON_INTERCEPT_FUNCTION(mlock); \
6133 COMMON_INTERCEPT_FUNCTION(munlock); \
6134 COMMON_INTERCEPT_FUNCTION(mlockall); \
6135 COMMON_INTERCEPT_FUNCTION(munlockall);
6139 #endif // SANITIZER_INTERCEPT_MLOCKX
6141 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6142 struct WrappedCookie {
6144 __sanitizer_cookie_io_functions_t real_io_funcs;
6147 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6148 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6149 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6150 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6151 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6154 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6155 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6156 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6157 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6158 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6161 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6162 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6163 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6164 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6165 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6166 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6170 static int wrapped_close(void *cookie) {
6171 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6172 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6173 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6174 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6175 InternalFree(wrapped_cookie);
6179 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6180 __sanitizer_cookie_io_functions_t io_funcs) {
6182 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6183 WrappedCookie *wrapped_cookie =
6184 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6185 wrapped_cookie->real_cookie = cookie;
6186 wrapped_cookie->real_io_funcs = io_funcs;
6187 __sanitizer_FILE *res =
6188 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6189 wrapped_seek, wrapped_close});
6193 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6195 #define INIT_FOPENCOOKIE
6196 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6198 #if SANITIZER_INTERCEPT_SEM
6199 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6201 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6202 // Workaround a bug in glibc's "old" semaphore implementation by
6203 // zero-initializing the sem_t contents. This has to be done here because
6204 // interceptors bind to the lowest symbols version by default, hitting the
6205 // buggy code path while the non-sanitized build of the same code works fine.
6206 REAL(memset)(s, 0, sizeof(*s));
6207 int res = REAL(sem_init)(s, pshared, value);
6211 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6213 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6214 int res = REAL(sem_destroy)(s);
6218 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6220 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6221 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6223 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6228 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6230 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6231 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_trywait)(s);
6233 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6238 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6240 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6241 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6242 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6244 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6249 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6251 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6252 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6253 int res = REAL(sem_post)(s);
6257 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6259 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6260 int res = REAL(sem_getvalue)(s, sval);
6262 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6263 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6268 COMMON_INTERCEPT_FUNCTION(sem_init); \
6269 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6270 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6271 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6272 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6273 COMMON_INTERCEPT_FUNCTION(sem_post); \
6274 COMMON_INTERCEPT_FUNCTION(sem_getvalue);
6277 #endif // SANITIZER_INTERCEPT_SEM
6279 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6280 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6282 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6283 int res = REAL(pthread_setcancelstate)(state, oldstate);
6284 if (res == 0 && oldstate != nullptr)
6285 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6289 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6291 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6292 int res = REAL(pthread_setcanceltype)(type, oldtype);
6293 if (res == 0 && oldtype != nullptr)
6294 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6297 #define INIT_PTHREAD_SETCANCEL \
6298 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6299 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6301 #define INIT_PTHREAD_SETCANCEL
6304 #if SANITIZER_INTERCEPT_MINCORE
6305 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6307 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6308 int res = REAL(mincore)(addr, length, vec);
6310 uptr page_size = GetPageSizeCached();
6311 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6312 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6316 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6318 #define INIT_MINCORE
6321 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6322 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6323 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6326 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6327 remote_iov, riovcnt, flags);
6328 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6331 write_iovec(ctx, local_iov, liovcnt, res);
6335 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6336 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6339 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6340 remote_iov, riovcnt, flags);
6341 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6344 read_iovec(ctx, local_iov, liovcnt, res);
6347 #define INIT_PROCESS_VM_READV \
6348 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6349 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6351 #define INIT_PROCESS_VM_READV
6354 #if SANITIZER_INTERCEPT_CTERMID
6355 INTERCEPTOR(char *, ctermid, char *s) {
6357 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6358 char *res = REAL(ctermid)(s);
6360 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6364 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6366 #define INIT_CTERMID
6369 #if SANITIZER_INTERCEPT_CTERMID_R
6370 INTERCEPTOR(char *, ctermid_r, char *s) {
6372 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6373 char *res = REAL(ctermid_r)(s);
6375 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
6379 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6381 #define INIT_CTERMID_R
6384 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6385 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6387 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6388 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6389 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
6391 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6393 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6397 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6398 void *srcaddr, int *addrlen) {
6400 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6402 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6404 if (srcaddr) srcaddr_sz = *addrlen;
6405 (void)srcaddr_sz; // prevent "set but not used" warning
6406 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
6408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6410 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6411 Min((SIZE_T)*addrlen, srcaddr_sz));
6415 #define INIT_RECV_RECVFROM \
6416 COMMON_INTERCEPT_FUNCTION(recv); \
6417 COMMON_INTERCEPT_FUNCTION(recvfrom);
6419 #define INIT_RECV_RECVFROM
6422 #if SANITIZER_INTERCEPT_SEND_SENDTO
6423 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6425 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6427 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6428 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6430 SSIZE_T res = REAL(send)(fd, buf, len, flags);
6431 if (common_flags()->intercept_send && res > 0)
6432 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6436 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6437 void *dstaddr, int addrlen) {
6439 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6441 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6442 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6444 // Can't check dstaddr as it may have uninitialized padding at the end.
6445 SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
6446 if (common_flags()->intercept_send && res > 0)
6447 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6450 #define INIT_SEND_SENDTO \
6451 COMMON_INTERCEPT_FUNCTION(send); \
6452 COMMON_INTERCEPT_FUNCTION(sendto);
6454 #define INIT_SEND_SENDTO
6457 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6458 INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
6460 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6461 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6462 int res = REAL(eventfd_read)(fd, value);
6464 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6465 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6469 INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
6471 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6473 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6474 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6476 int res = REAL(eventfd_write)(fd, value);
6479 #define INIT_EVENTFD_READ_WRITE \
6480 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6481 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6483 #define INIT_EVENTFD_READ_WRITE
6486 #if SANITIZER_INTERCEPT_STAT
6487 INTERCEPTOR(int, stat, const char *path, void *buf) {
6489 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6490 if (common_flags()->intercept_stat)
6491 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6492 int res = REAL(stat)(path, buf);
6494 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6497 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6502 #if SANITIZER_INTERCEPT_LSTAT
6503 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6505 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6506 if (common_flags()->intercept_stat)
6507 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6508 int res = REAL(lstat)(path, buf);
6510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6513 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6518 #if SANITIZER_INTERCEPT___XSTAT
6519 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6521 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6522 if (common_flags()->intercept_stat)
6523 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6524 int res = REAL(__xstat)(version, path, buf);
6526 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6529 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6531 #define INIT___XSTAT
6534 #if SANITIZER_INTERCEPT___XSTAT64
6535 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6537 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6538 if (common_flags()->intercept_stat)
6539 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6540 int res = REAL(__xstat64)(version, path, buf);
6542 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6545 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6547 #define INIT___XSTAT64
6550 #if SANITIZER_INTERCEPT___LXSTAT
6551 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6553 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6554 if (common_flags()->intercept_stat)
6555 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6556 int res = REAL(__lxstat)(version, path, buf);
6558 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6561 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6563 #define INIT___LXSTAT
6566 #if SANITIZER_INTERCEPT___LXSTAT64
6567 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6569 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
6570 if (common_flags()->intercept_stat)
6571 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6572 int res = REAL(__lxstat64)(version, path, buf);
6574 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6577 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
6579 #define INIT___LXSTAT64
6582 // FIXME: add other *stat interceptor
6584 #if SANITIZER_INTERCEPT_UTMP
6585 INTERCEPTOR(void *, getutent, int dummy) {
6587 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
6588 void *res = REAL(getutent)(dummy);
6590 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6593 INTERCEPTOR(void *, getutid, void *ut) {
6595 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
6596 void *res = REAL(getutid)(ut);
6598 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6601 INTERCEPTOR(void *, getutline, void *ut) {
6603 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
6604 void *res = REAL(getutline)(ut);
6606 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
6610 COMMON_INTERCEPT_FUNCTION(getutent); \
6611 COMMON_INTERCEPT_FUNCTION(getutid); \
6612 COMMON_INTERCEPT_FUNCTION(getutline);
6617 #if SANITIZER_INTERCEPT_UTMPX
6618 INTERCEPTOR(void *, getutxent, int dummy) {
6620 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
6621 void *res = REAL(getutxent)(dummy);
6623 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6626 INTERCEPTOR(void *, getutxid, void *ut) {
6628 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
6629 void *res = REAL(getutxid)(ut);
6631 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6634 INTERCEPTOR(void *, getutxline, void *ut) {
6636 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
6637 void *res = REAL(getutxline)(ut);
6639 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
6642 INTERCEPTOR(void *, pututxline, const void *ut) {
6644 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
6646 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
6647 void *res = REAL(pututxline)(ut);
6649 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
6652 #define INIT_UTMPX \
6653 COMMON_INTERCEPT_FUNCTION(getutxent); \
6654 COMMON_INTERCEPT_FUNCTION(getutxid); \
6655 COMMON_INTERCEPT_FUNCTION(getutxline); \
6656 COMMON_INTERCEPT_FUNCTION(pututxline);
6661 #if SANITIZER_INTERCEPT_GETLOADAVG
6662 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
6664 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
6665 int res = REAL(getloadavg)(loadavg, nelem);
6667 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
6670 #define INIT_GETLOADAVG \
6671 COMMON_INTERCEPT_FUNCTION(getloadavg);
6673 #define INIT_GETLOADAVG
6676 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
6677 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
6681 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
6685 INTERCEPTOR(int, mprobe, void *ptr) {
6690 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
6692 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
6693 SIZE_T res = REAL(wcslen)(s);
6694 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
6698 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
6700 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
6701 SIZE_T res = REAL(wcsnlen)(s, n);
6702 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
6705 #define INIT_WCSLEN \
6706 COMMON_INTERCEPT_FUNCTION(wcslen); \
6707 COMMON_INTERCEPT_FUNCTION(wcsnlen);
6709 #if SANITIZER_INTERCEPT_WCSCAT
6710 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
6712 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
6713 SIZE_T src_size = REAL(wcslen)(src);
6714 SIZE_T dst_size = REAL(wcslen)(dst);
6715 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
6716 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6717 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6718 (src_size + 1) * sizeof(wchar_t));
6719 return REAL(wcscat)(dst, src); // NOLINT
6722 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
6724 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
6725 SIZE_T src_size = REAL(wcsnlen)(src, n);
6726 SIZE_T dst_size = REAL(wcslen)(dst);
6727 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
6728 Min(src_size + 1, n) * sizeof(wchar_t));
6729 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
6730 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
6731 (src_size + 1) * sizeof(wchar_t));
6732 return REAL(wcsncat)(dst, src, n); // NOLINT
6734 #define INIT_WCSCAT \
6735 COMMON_INTERCEPT_FUNCTION(wcscat); \
6736 COMMON_INTERCEPT_FUNCTION(wcsncat);
6741 #if SANITIZER_INTERCEPT_WCSDUP
6742 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
6744 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
6745 SIZE_T len = REAL(wcslen)(s);
6746 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
6747 wchar_t *result = REAL(wcsdup)(s);
6749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
6753 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
6758 #if SANITIZER_INTERCEPT_STRXFRM
6759 static SIZE_T RealStrLen(const char *str) { return REAL(strlen)(str); }
6761 static SIZE_T RealStrLen(const wchar_t *str) { return REAL(wcslen)(str); }
6763 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
6766 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
6767 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
6768 sizeof(*src) * (RealStrLen(src) + 1)); \
6769 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
6771 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
6775 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
6776 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
6779 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
6781 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
6784 #define INIT_STRXFRM \
6785 COMMON_INTERCEPT_FUNCTION(strxfrm); \
6786 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
6788 #define INIT_STRXFRM
6791 #if SANITIZER_INTERCEPT___STRXFRM_L
6792 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
6794 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
6797 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
6799 #define INIT___STRXFRM_L
6802 #if SANITIZER_INTERCEPT_WCSXFRM
6803 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
6804 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
6807 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6809 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
6812 #define INIT_WCSXFRM \
6813 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
6814 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
6816 #define INIT_WCSXFRM
6819 #if SANITIZER_INTERCEPT___WCSXFRM_L
6820 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
6822 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
6825 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
6827 #define INIT___WCSXFRM_L
6830 #if SANITIZER_INTERCEPT_ACCT
6831 INTERCEPTOR(int, acct, const char *file) {
6833 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
6835 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
6836 return REAL(acct)(file);
6838 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
6843 #if SANITIZER_INTERCEPT_USER_FROM_UID
6844 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
6847 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
6848 user = REAL(user_from_uid)(uid, nouser);
6850 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, REAL(strlen)(user) + 1);
6853 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
6855 #define INIT_USER_FROM_UID
6858 #if SANITIZER_INTERCEPT_UID_FROM_USER
6859 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
6862 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
6864 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6865 res = REAL(uid_from_user)(name, uid);
6867 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
6870 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
6872 #define INIT_UID_FROM_USER
6875 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
6876 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
6879 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
6880 group = REAL(group_from_gid)(gid, nogroup);
6882 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, REAL(strlen)(group) + 1);
6885 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
6887 #define INIT_GROUP_FROM_GID
6890 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
6891 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
6894 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
6896 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, REAL(strlen)(group) + 1);
6897 res = REAL(gid_from_group)(group, gid);
6899 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
6902 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
6904 #define INIT_GID_FROM_GROUP
6907 #if SANITIZER_INTERCEPT_ACCESS
6908 INTERCEPTOR(int, access, const char *path, int mode) {
6910 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
6912 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6913 return REAL(access)(path, mode);
6915 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
6920 #if SANITIZER_INTERCEPT_FACCESSAT
6921 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
6923 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
6925 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6926 return REAL(faccessat)(fd, path, mode, flags);
6928 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
6930 #define INIT_FACCESSAT
6933 #if SANITIZER_INTERCEPT_GETGROUPLIST
6934 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
6938 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
6940 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6942 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
6943 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
6944 if (!res && groups && ngroups) {
6945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6946 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6951 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
6953 #define INIT_GETGROUPLIST
6956 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
6957 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
6958 int maxgrp, int *ngroups) {
6961 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
6964 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
6965 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
6966 if (!res && groups && ngroups) {
6967 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
6968 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
6973 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
6975 #define INIT_GETGROUPMEMBERSHIP
6978 #if SANITIZER_INTERCEPT_READLINK
6979 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
6981 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
6982 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
6983 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
6985 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
6989 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
6991 #define INIT_READLINK
6994 #if SANITIZER_INTERCEPT_READLINKAT
6995 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
6998 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
6999 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7000 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7006 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7008 #define INIT_READLINKAT
7011 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7012 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7013 struct file_handle *handle, int *mount_id, int flags) {
7015 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7017 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, REAL(strlen)(pathname) + 1);
7019 __sanitizer_file_handle *sanitizer_handle =
7020 reinterpret_cast<__sanitizer_file_handle*>(handle);
7021 COMMON_INTERCEPTOR_READ_RANGE(
7022 ctx, &sanitizer_handle->handle_bytes,
7023 sizeof(sanitizer_handle->handle_bytes));
7025 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7027 COMMON_INTERCEPTOR_WRITE_RANGE(
7028 ctx, &sanitizer_handle->handle_bytes,
7029 sizeof(sanitizer_handle->handle_bytes));
7030 COMMON_INTERCEPTOR_WRITE_RANGE(
7031 ctx, &sanitizer_handle->handle_type,
7032 sizeof(sanitizer_handle->handle_type));
7033 COMMON_INTERCEPTOR_WRITE_RANGE(
7034 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7035 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7040 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7042 #define INIT_NAME_TO_HANDLE_AT
7045 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7046 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7049 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7051 __sanitizer_file_handle *sanitizer_handle =
7052 reinterpret_cast<__sanitizer_file_handle*>(handle);
7053 COMMON_INTERCEPTOR_READ_RANGE(
7054 ctx, &sanitizer_handle->handle_bytes,
7055 sizeof(sanitizer_handle->handle_bytes));
7056 COMMON_INTERCEPTOR_READ_RANGE(
7057 ctx, &sanitizer_handle->handle_type,
7058 sizeof(sanitizer_handle->handle_type));
7059 COMMON_INTERCEPTOR_READ_RANGE(
7060 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7062 return REAL(open_by_handle_at)(mount_fd, handle, flags);
7065 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7067 #define INIT_OPEN_BY_HANDLE_AT
7070 #if SANITIZER_INTERCEPT_STRLCPY
7071 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7074 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7076 // Keep strnlen as macro argument, as macro may ignore it.
7077 COMMON_INTERCEPTOR_READ_STRING(
7078 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7080 res = REAL(strlcpy)(dst, src, size);
7081 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
7085 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7088 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7089 // src is checked in the strlcpy() interceptor
7091 len = internal_strnlen(dst, size);
7092 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7094 // Reuse the rest of the code in the strlcpy() interceptor
7095 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7097 #define INIT_STRLCPY \
7098 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7099 COMMON_INTERCEPT_FUNCTION(strlcat);
7101 #define INIT_STRLCPY
7104 #if SANITIZER_INTERCEPT_MMAP
7105 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7108 if (common_flags()->detect_write_exec)
7109 ReportMmapWriteExec(prot);
7110 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7111 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7112 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7113 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7116 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7118 if (common_flags()->detect_write_exec)
7119 ReportMmapWriteExec(prot);
7120 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7121 return (int)internal_mprotect(addr, sz, prot);
7122 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7123 MprotectMallocZones(addr, prot);
7124 return REAL(mprotect)(addr, sz, prot);
7127 COMMON_INTERCEPT_FUNCTION(mmap); \
7128 COMMON_INTERCEPT_FUNCTION(mprotect);
7133 #if SANITIZER_INTERCEPT_MMAP64
7134 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7137 if (common_flags()->detect_write_exec)
7138 ReportMmapWriteExec(prot);
7139 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7140 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7141 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7142 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7144 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7149 #if SANITIZER_INTERCEPT_DEVNAME
7150 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7153 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7154 name = REAL(devname)(dev, type);
7156 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
7159 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7161 #define INIT_DEVNAME
7164 #if SANITIZER_INTERCEPT_DEVNAME_R
7165 #if SANITIZER_NETBSD
7166 #define DEVNAME_R_RETTYPE int
7167 #define DEVNAME_R_SUCCESS(x) (!(x))
7169 #define DEVNAME_R_RETTYPE char*
7170 #define DEVNAME_R_SUCCESS(x) (x)
7172 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7175 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7176 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7177 if (DEVNAME_R_SUCCESS(res))
7178 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, REAL(strlen)(path) + 1);
7181 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7183 #define INIT_DEVNAME_R
7186 #if SANITIZER_INTERCEPT_FGETLN
7187 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7189 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7190 char *str = REAL(fgetln)(stream, len);
7192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7193 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7197 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7202 #if SANITIZER_INTERCEPT_STRMODE
7203 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7205 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7206 REAL(strmode)(mode, bp);
7208 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, REAL(strlen)(bp) + 1);
7210 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7212 #define INIT_STRMODE
7215 #if SANITIZER_INTERCEPT_TTYENT
7216 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7218 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7219 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7221 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7224 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7226 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7228 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7229 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7231 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7234 INTERCEPTOR(int, setttyentpath, char *path) {
7236 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7238 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7239 return REAL(setttyentpath)(path);
7241 #define INIT_TTYENT \
7242 COMMON_INTERCEPT_FUNCTION(getttyent); \
7243 COMMON_INTERCEPT_FUNCTION(getttynam); \
7244 COMMON_INTERCEPT_FUNCTION(setttyentpath)
7249 #if SANITIZER_INTERCEPT_PROTOENT
7250 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
7252 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
7253 struct __sanitizer_protoent *p = REAL(getprotoent)();
7255 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7257 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7259 SIZE_T pp_size = 1; // One handles the trailing \0
7261 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7262 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7265 pp_size * sizeof(char **));
7270 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7272 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7274 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7275 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7277 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7279 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7281 SIZE_T pp_size = 1; // One handles the trailing \0
7283 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7286 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7287 pp_size * sizeof(char **));
7292 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7294 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7295 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7297 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7299 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
7301 SIZE_T pp_size = 1; // One handles the trailing \0
7303 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7304 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
7306 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
7307 pp_size * sizeof(char **));
7311 #define INIT_PROTOENT \
7312 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7313 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7314 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7316 #define INIT_PROTOENT
7319 #if SANITIZER_INTERCEPT_NETENT
7320 INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
7322 COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
7323 struct __sanitizer_netent *n = REAL(getnetent)();
7325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7327 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7329 SIZE_T nn_size = 1; // One handles the trailing \0
7331 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7335 nn_size * sizeof(char **));
7340 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7342 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7344 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7345 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7347 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7349 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7351 SIZE_T nn_size = 1; // One handles the trailing \0
7353 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7356 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7357 nn_size * sizeof(char **));
7362 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7364 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7365 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7367 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7369 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, REAL(strlen)(n->n_name) + 1);
7371 SIZE_T nn_size = 1; // One handles the trailing \0
7373 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, REAL(strlen)(*nn) + 1);
7376 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
7377 nn_size * sizeof(char **));
7381 #define INIT_NETENT \
7382 COMMON_INTERCEPT_FUNCTION(getnetent); \
7383 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7384 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7389 #if SANITIZER_INTERCEPT_GETMNTINFO
7390 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7392 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7393 int cnt = REAL(getmntinfo)(mntbufp, flags);
7394 if (cnt > 0 && mntbufp) {
7395 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7397 #if SANITIZER_NETBSD
7398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7405 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7407 #define INIT_GETMNTINFO
7410 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7411 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7414 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7416 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7417 REAL(mi_vector_hash)(key, len, seed, hashes);
7419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7421 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7423 #define INIT_MI_VECTOR_HASH
7426 #if SANITIZER_INTERCEPT_SETVBUF
7427 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7430 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7431 int ret = REAL(setvbuf)(stream, buf, mode, size);
7433 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7435 unpoison_file(stream);
7439 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7441 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7442 REAL(setbuf)(stream, buf);
7444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7447 unpoison_file(stream);
7450 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
7452 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
7453 REAL(setbuffer)(stream, buf, mode);
7455 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7458 unpoison_file(stream);
7461 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7463 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7464 REAL(setlinebuf)(stream);
7466 unpoison_file(stream);
7468 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7469 COMMON_INTERCEPT_FUNCTION(setbuf); \
7470 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7471 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7473 #define INIT_SETVBUF
7476 #if SANITIZER_INTERCEPT_GETVFSSTAT
7477 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7479 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7480 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7482 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7485 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7487 #define INIT_GETVFSSTAT
7490 #if SANITIZER_INTERCEPT_REGEX
7491 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7493 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7495 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, REAL(strlen)(pattern) + 1);
7496 int res = REAL(regcomp)(preg, pattern, cflags);
7498 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7501 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7502 struct __sanitizer_regmatch *pmatch[], int eflags) {
7504 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7506 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7508 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, REAL(strlen)(string) + 1);
7509 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7511 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7514 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7515 SIZE_T errbuf_size) {
7517 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7519 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7520 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, REAL(strlen)(errbuf) + 1);
7525 INTERCEPTOR(void, regfree, const void *preg) {
7527 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
7529 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7530 REAL(regfree)(preg);
7532 #define INIT_REGEX \
7533 COMMON_INTERCEPT_FUNCTION(regcomp); \
7534 COMMON_INTERCEPT_FUNCTION(regexec); \
7535 COMMON_INTERCEPT_FUNCTION(regerror); \
7536 COMMON_INTERCEPT_FUNCTION(regfree);
7541 #if SANITIZER_INTERCEPT_REGEXSUB
7542 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
7543 const struct __sanitizer_regmatch *rm, const char *str) {
7545 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
7547 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7548 // The implementation demands and hardcodes 10 elements
7550 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7552 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, REAL(strlen)(str) + 1);
7553 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
7555 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
7558 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
7559 const struct __sanitizer_regmatch *rm, const char *sstr) {
7561 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
7563 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, REAL(strlen)(sub) + 1);
7564 // Hardcode 10 elements as this is hardcoded size
7566 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
7568 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, REAL(strlen)(sstr) + 1);
7569 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
7570 if (res > 0 && buf) {
7571 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
7572 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, REAL(strlen)(*buf) + 1);
7577 #define INIT_REGEXSUB \
7578 COMMON_INTERCEPT_FUNCTION(regnsub); \
7579 COMMON_INTERCEPT_FUNCTION(regasub);
7581 #define INIT_REGEXSUB
7584 #if SANITIZER_INTERCEPT_FTS
7585 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
7586 int (*compar)(void **, void **)) {
7588 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
7590 for (char *const *pa = path_argv; ; ++pa) {
7591 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
7594 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
7597 // TODO(kamil): handle compar callback
7598 void *fts = REAL(fts_open)(path_argv, options, compar);
7600 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
7604 INTERCEPTOR(void *, fts_read, void *ftsp) {
7606 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
7608 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7609 void *ftsent = REAL(fts_read)(ftsp);
7611 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7615 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
7617 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
7619 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7620 void *ftsent = REAL(fts_children)(ftsp, options);
7622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
7626 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
7628 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
7630 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7632 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
7633 return REAL(fts_set)(ftsp, f, options);
7636 INTERCEPTOR(int, fts_close, void *ftsp) {
7638 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
7640 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
7641 return REAL(fts_close)(ftsp);
7644 COMMON_INTERCEPT_FUNCTION(fts_open); \
7645 COMMON_INTERCEPT_FUNCTION(fts_read); \
7646 COMMON_INTERCEPT_FUNCTION(fts_children); \
7647 COMMON_INTERCEPT_FUNCTION(fts_set); \
7648 COMMON_INTERCEPT_FUNCTION(fts_close);
7653 #if SANITIZER_INTERCEPT_SYSCTL
7654 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
7655 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
7657 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7658 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
7659 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
7662 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
7664 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7666 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7667 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
7670 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7672 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7678 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
7679 void *newp, SIZE_T newlen) {
7681 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7682 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
7683 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
7686 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7688 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7690 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
7691 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
7694 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
7696 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
7702 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
7705 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
7707 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7709 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7710 int res = REAL(sysctlnametomib)(sname, name, namelenp);
7713 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7721 #define INIT_SYSCTL \
7722 COMMON_INTERCEPT_FUNCTION(sysctl); \
7723 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
7724 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
7729 #if SANITIZER_INTERCEPT_ASYSCTL
7730 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
7732 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
7734 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
7735 void *res = REAL(asysctl)(name, namelen, len);
7737 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7738 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7743 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
7745 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
7747 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7748 void *res = REAL(asysctlbyname)(sname, len);
7750 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7751 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
7755 #define INIT_ASYSCTL \
7756 COMMON_INTERCEPT_FUNCTION(asysctl); \
7757 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
7759 #define INIT_ASYSCTL
7762 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
7763 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
7764 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
7767 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
7770 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1);
7772 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
7774 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
7775 // Skip rnode, it's rarely used and not trivial to sanitize
7776 // It's also used mostly internally
7777 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
7780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
7782 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
7785 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
7787 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
7792 #define INIT_SYSCTLGETMIBINFO \
7793 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
7795 #define INIT_SYSCTLGETMIBINFO
7798 #if SANITIZER_INTERCEPT_NL_LANGINFO
7799 INTERCEPTOR(char *, nl_langinfo, long item) {
7801 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
7802 char *ret = REAL(nl_langinfo)(item);
7804 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7807 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
7809 #define INIT_NL_LANGINFO
7812 #if SANITIZER_INTERCEPT_MODCTL
7813 INTERCEPTOR(int, modctl, int operation, void *argp) {
7816 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
7818 if (operation == modctl_load) {
7820 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
7821 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
7822 if (ml->ml_filename)
7823 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
7824 REAL(strlen)(ml->ml_filename) + 1);
7826 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
7828 ret = REAL(modctl)(operation, argp);
7829 } else if (operation == modctl_unload) {
7831 const char *name = (const char *)argp;
7832 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
7834 ret = REAL(modctl)(operation, argp);
7835 } else if (operation == modctl_stat) {
7837 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
7839 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
7840 iov_len = iov->iov_len;
7842 ret = REAL(modctl)(operation, argp);
7844 COMMON_INTERCEPTOR_WRITE_RANGE(
7845 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
7846 } else if (operation == modctl_exists)
7847 ret = REAL(modctl)(operation, argp);
7849 ret = REAL(modctl)(operation, argp);
7853 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
7858 #if SANITIZER_INTERCEPT_STRTONUM
7859 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
7860 long long maxval, const char **errstr) {
7862 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
7864 // TODO(kamil): Implement strtoll as a common inteceptor
7866 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
7867 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
7869 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
7871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
7873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1);
7877 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
7879 #define INIT_STRTONUM
7882 #if SANITIZER_INTERCEPT_FPARSELN
7883 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
7884 SIZE_T *lineno, const char delim[3], int flags) {
7886 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
7888 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
7890 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
7891 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
7893 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, REAL(strlen)(ret) + 1);
7895 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7897 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
7901 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
7903 #define INIT_FPARSELN
7906 #if SANITIZER_INTERCEPT_STATVFS1
7907 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
7909 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
7910 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
7911 int res = REAL(statvfs1)(path, buf, flags);
7912 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7915 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
7917 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
7918 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7919 int res = REAL(fstatvfs1)(fd, buf, flags);
7921 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
7923 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
7927 #define INIT_STATVFS1 \
7928 COMMON_INTERCEPT_FUNCTION(statvfs1); \
7929 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
7931 #define INIT_STATVFS1
7934 #if SANITIZER_INTERCEPT_STRTOI
7935 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
7936 INTMAX_T low, INTMAX_T high, int *rstatus) {
7938 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
7940 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
7941 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7947 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
7948 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
7950 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
7952 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
7953 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
7955 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
7958 #define INIT_STRTOI \
7959 COMMON_INTERCEPT_FUNCTION(strtoi); \
7960 COMMON_INTERCEPT_FUNCTION(strtou)
7965 #if SANITIZER_INTERCEPT_CAPSICUM
7966 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
7969 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
7971 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7972 __sanitizer_cap_rights_t *ret = \
7973 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
7975 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7979 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
7982 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
7984 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7985 __sanitizer_cap_rights_t *ret = \
7986 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
7988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
7992 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
7995 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
7997 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
7998 __sanitizer_cap_rights_t *ret = \
7999 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8001 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8005 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8008 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8010 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8011 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8014 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8015 __sanitizer_cap_rights_t *rights) {
8016 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8019 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8020 __sanitizer_cap_rights_t *rights) {
8021 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8024 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8025 __sanitizer_cap_rights_t *rights) {
8026 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8029 INTERCEPTOR(bool, cap_rights_is_set,
8030 __sanitizer_cap_rights_t *rights) {
8031 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8034 INTERCEPTOR(int, cap_rights_limit, int fd,
8035 const __sanitizer_cap_rights_t *rights) {
8037 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8039 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8041 return REAL(cap_rights_limit)(fd, rights);
8044 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8046 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8047 int ret = REAL(cap_rights_get)(fd, rights);
8049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8054 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8056 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8058 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8060 return REAL(cap_rights_is_valid(rights));
8063 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8064 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8066 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8068 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8070 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8072 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8077 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8078 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8080 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8082 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8084 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8086 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8091 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8092 const __sanitizer_cap_rights *little) {
8094 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8096 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8098 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8100 return REAL(cap_rights_contains)(big, little);
8103 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8105 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8107 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8109 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8112 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8114 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8115 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8117 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8121 #define INIT_CAPSICUM \
8122 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8123 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8124 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8125 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8126 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8127 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8128 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8129 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8130 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8131 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8132 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8133 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8135 #define INIT_CAPSICUM
8138 #if SANITIZER_INTERCEPT_SHA1
8139 INTERCEPTOR(void, SHA1Init, void *context) {
8141 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8142 REAL(SHA1Init)(context);
8144 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8146 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8148 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8149 if (data && len > 0)
8150 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8152 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8153 REAL(SHA1Update)(context, data, len);
8155 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8157 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8159 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8161 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8162 REAL(SHA1Final)(digest, context);
8164 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8166 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8168 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8170 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8172 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8173 REAL(SHA1Transform)(state, buffer);
8175 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8177 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8179 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8181 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8182 char *ret = REAL(SHA1End)(context, buf);
8184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8187 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8189 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8191 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8192 char *ret = REAL(SHA1File)(filename, buf);
8194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8197 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8200 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8202 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8203 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8205 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8208 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8210 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8212 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8213 char *ret = REAL(SHA1Data)(data, len, buf);
8215 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8219 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8220 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8221 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8222 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8223 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8224 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8225 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8226 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8231 #if SANITIZER_INTERCEPT_MD4
8232 INTERCEPTOR(void, MD4Init, void *context) {
8234 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8235 REAL(MD4Init)(context);
8237 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8240 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8243 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8244 if (data && len > 0)
8245 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8247 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8248 REAL(MD4Update)(context, data, len);
8250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8253 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8255 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8257 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8258 REAL(MD4Final)(digest, context);
8260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8263 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8265 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8267 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8268 char *ret = REAL(MD4End)(context, buf);
8270 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8274 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8276 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8278 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8279 char *ret = REAL(MD4File)(filename, buf);
8281 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8285 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8288 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8289 if (data && len > 0)
8290 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8291 char *ret = REAL(MD4Data)(data, len, buf);
8293 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8298 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8299 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8300 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8301 COMMON_INTERCEPT_FUNCTION(MD4End); \
8302 COMMON_INTERCEPT_FUNCTION(MD4File); \
8303 COMMON_INTERCEPT_FUNCTION(MD4Data)
8308 #if SANITIZER_INTERCEPT_RMD160
8309 INTERCEPTOR(void, RMD160Init, void *context) {
8311 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8312 REAL(RMD160Init)(context);
8314 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8316 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8318 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8319 if (data && len > 0)
8320 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8322 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8323 REAL(RMD160Update)(context, data, len);
8325 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8327 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8329 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8331 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8332 REAL(RMD160Final)(digest, context);
8334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8336 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8338 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8340 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8342 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8343 REAL(RMD160Transform)(state, buffer);
8345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8347 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8349 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8351 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8352 char *ret = REAL(RMD160End)(context, buf);
8354 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8357 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8359 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8361 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8362 char *ret = REAL(RMD160File)(filename, buf);
8364 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8367 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8370 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8372 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8373 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8378 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8380 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8381 if (data && len > 0)
8382 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8383 char *ret = REAL(RMD160Data)(data, len, buf);
8385 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8388 #define INIT_RMD160 \
8389 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8390 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8391 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8392 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8393 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8394 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8395 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8396 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8401 #if SANITIZER_INTERCEPT_MD5
8402 INTERCEPTOR(void, MD5Init, void *context) {
8404 COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
8405 REAL(MD5Init)(context);
8407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8410 INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
8413 COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
8414 if (data && len > 0)
8415 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8417 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8418 REAL(MD5Update)(context, data, len);
8420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
8423 INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
8425 COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
8427 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8428 REAL(MD5Final)(digest, context);
8430 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8433 INTERCEPTOR(char *, MD5End, void *context, char *buf) {
8435 COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
8437 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
8438 char *ret = REAL(MD5End)(context, buf);
8440 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8444 INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
8446 COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
8448 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8449 char *ret = REAL(MD5File)(filename, buf);
8451 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8455 INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
8458 COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
8459 if (data && len > 0)
8460 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8461 char *ret = REAL(MD5Data)(data, len, buf);
8463 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
8468 COMMON_INTERCEPT_FUNCTION(MD5Init); \
8469 COMMON_INTERCEPT_FUNCTION(MD5Update); \
8470 COMMON_INTERCEPT_FUNCTION(MD5Final); \
8471 COMMON_INTERCEPT_FUNCTION(MD5End); \
8472 COMMON_INTERCEPT_FUNCTION(MD5File); \
8473 COMMON_INTERCEPT_FUNCTION(MD5Data)
8478 #if SANITIZER_INTERCEPT_FSEEK
8479 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8481 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8482 return REAL(fseek)(stream, offset, whence);
8484 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8486 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8487 return REAL(fseeko)(stream, offset, whence);
8489 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8491 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8492 return REAL(ftell)(stream);
8494 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8496 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8497 return REAL(ftello)(stream);
8499 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8501 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8502 return REAL(rewind)(stream);
8504 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8506 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8507 int ret = REAL(fgetpos)(stream, pos);
8509 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8512 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8514 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8516 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8517 return REAL(fsetpos)(stream, pos);
8519 #define INIT_FSEEK \
8520 COMMON_INTERCEPT_FUNCTION(fseek); \
8521 COMMON_INTERCEPT_FUNCTION(fseeko); \
8522 COMMON_INTERCEPT_FUNCTION(ftell); \
8523 COMMON_INTERCEPT_FUNCTION(ftello); \
8524 COMMON_INTERCEPT_FUNCTION(rewind); \
8525 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8526 COMMON_INTERCEPT_FUNCTION(fsetpos)
8531 #if SANITIZER_INTERCEPT_MD2
8532 INTERCEPTOR(void, MD2Init, void *context) {
8534 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8535 REAL(MD2Init)(context);
8537 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8540 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8543 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8544 if (data && len > 0)
8545 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8547 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8548 REAL(MD2Update)(context, data, len);
8550 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8553 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8555 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8557 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8558 REAL(MD2Final)(digest, context);
8560 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8563 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8565 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8567 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8568 char *ret = REAL(MD2End)(context, buf);
8570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8574 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8576 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8578 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
8579 char *ret = REAL(MD2File)(filename, buf);
8581 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8585 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8588 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8589 if (data && len > 0)
8590 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8591 char *ret = REAL(MD2Data)(data, len, buf);
8593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8598 COMMON_INTERCEPT_FUNCTION(MD2Init); \
8599 COMMON_INTERCEPT_FUNCTION(MD2Update); \
8600 COMMON_INTERCEPT_FUNCTION(MD2Final); \
8601 COMMON_INTERCEPT_FUNCTION(MD2End); \
8602 COMMON_INTERCEPT_FUNCTION(MD2File); \
8603 COMMON_INTERCEPT_FUNCTION(MD2Data)
8608 #if SANITIZER_INTERCEPT_SHA2
8609 #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
8610 INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
8612 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
8613 REAL(SHA##LEN##_Init)(context); \
8615 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8617 INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
8618 const u8 *data, SIZE_T len) { \
8620 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
8621 if (data && len > 0) \
8622 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8624 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8625 REAL(SHA##LEN##_Update)(context, data, len); \
8627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8629 INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
8632 CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
8633 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
8635 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8636 REAL(SHA##LEN##_Final)(digest, context); \
8638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
8639 sizeof(digest[0]) * \
8640 SHA##LEN##_digest_length); \
8642 INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
8644 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
8646 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
8647 char *ret = REAL(SHA##LEN##_End)(context, buf); \
8649 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8652 INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
8654 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
8656 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8657 char *ret = REAL(SHA##LEN##_File)(filename, buf); \
8659 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8662 INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
8663 OFF_T offset, OFF_T length) { \
8665 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
8668 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);\
8669 char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
8671 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8674 INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
8676 COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
8677 if (data && len > 0) \
8678 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
8679 char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
8681 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
8685 SHA2_INTERCEPTORS(224, u32);
8686 SHA2_INTERCEPTORS(256, u32);
8687 SHA2_INTERCEPTORS(384, u64);
8688 SHA2_INTERCEPTORS(512, u64);
8690 #define INIT_SHA2_INTECEPTORS(LEN) \
8691 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
8692 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
8693 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
8694 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
8695 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
8696 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
8697 COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
8700 INIT_SHA2_INTECEPTORS(224); \
8701 INIT_SHA2_INTECEPTORS(256); \
8702 INIT_SHA2_INTECEPTORS(384); \
8703 INIT_SHA2_INTECEPTORS(512)
8704 #undef SHA2_INTERCEPTORS
8709 #if SANITIZER_INTERCEPT_VIS
8710 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
8712 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
8713 char *end = REAL(vis)(dst, c, flag, nextc);
8714 // dst is NULL terminated and end points to the NULL char
8716 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8719 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
8721 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
8722 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
8723 // nvis cannot make sure the dst is NULL terminated
8725 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8728 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
8730 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
8732 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8733 int len = REAL(strvis)(dst, src, flag);
8735 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8738 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
8740 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
8742 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8743 int len = REAL(stravis)(dst, src, flag);
8745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
8747 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
8751 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
8753 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
8755 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8756 int len = REAL(strnvis)(dst, dlen, src, flag);
8757 // The interface will be valid even if there is no space for NULL char
8759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8762 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
8764 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
8766 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8767 int ret = REAL(strvisx)(dst, src, len, flag);
8769 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8772 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8775 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
8777 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8778 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
8779 if (dst && ret >= 0)
8780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8783 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8784 int flag, int *cerr_ptr) {
8786 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
8788 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8789 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8790 // according to the implementation
8792 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8793 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
8794 if (dst && ret >= 0)
8795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8797 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8800 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
8801 const char *extra) {
8803 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
8805 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8806 char *end = REAL(svis)(dst, c, flag, nextc, extra);
8808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
8811 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
8812 const char *extra) {
8814 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
8816 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8817 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
8819 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
8820 Min((SIZE_T)(end - dst + 1), dlen));
8823 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
8824 const char *extra) {
8826 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
8828 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8830 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8831 int len = REAL(strsvis)(dst, src, flag, extra);
8833 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8836 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
8837 const char *extra) {
8839 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
8841 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8843 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8844 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
8845 // The interface will be valid even if there is no space for NULL char
8846 if (dst && len >= 0)
8847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
8850 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
8851 const char *extra) {
8853 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
8855 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8857 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8858 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
8860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8863 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
8864 int flag, const char *extra) {
8866 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
8868 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8870 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8871 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
8872 if (dst && ret >= 0)
8873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8876 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
8877 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
8879 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
8882 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
8884 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, REAL(strlen)(extra) + 1);
8885 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
8886 // according to the implementation
8888 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
8889 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
8890 if (dst && ret >= 0)
8891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8893 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
8896 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
8898 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
8900 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
8901 int ret = REAL(unvis)(cp, c, astate, flag);
8902 if (ret == unvis_valid || ret == unvis_validpush) {
8903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
8907 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
8909 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
8911 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8912 int ret = REAL(strunvis)(dst, src);
8914 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8917 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
8919 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
8921 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8922 int ret = REAL(strnunvis)(dst, dlen, src);
8924 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8927 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
8929 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
8931 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8932 int ret = REAL(strunvisx)(dst, src, flag);
8934 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8937 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
8940 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
8942 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, REAL(strlen)(src) + 1);
8943 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
8945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
8949 COMMON_INTERCEPT_FUNCTION(vis); \
8950 COMMON_INTERCEPT_FUNCTION(nvis); \
8951 COMMON_INTERCEPT_FUNCTION(strvis); \
8952 COMMON_INTERCEPT_FUNCTION(stravis); \
8953 COMMON_INTERCEPT_FUNCTION(strnvis); \
8954 COMMON_INTERCEPT_FUNCTION(strvisx); \
8955 COMMON_INTERCEPT_FUNCTION(strnvisx); \
8956 COMMON_INTERCEPT_FUNCTION(strenvisx); \
8957 COMMON_INTERCEPT_FUNCTION(svis); \
8958 COMMON_INTERCEPT_FUNCTION(snvis); \
8959 COMMON_INTERCEPT_FUNCTION(strsvis); \
8960 COMMON_INTERCEPT_FUNCTION(strsnvis); \
8961 COMMON_INTERCEPT_FUNCTION(strsvisx); \
8962 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
8963 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
8964 COMMON_INTERCEPT_FUNCTION(unvis); \
8965 COMMON_INTERCEPT_FUNCTION(strunvis); \
8966 COMMON_INTERCEPT_FUNCTION(strnunvis); \
8967 COMMON_INTERCEPT_FUNCTION(strunvisx); \
8968 COMMON_INTERCEPT_FUNCTION(strnunvisx)
8973 #if SANITIZER_INTERCEPT_CDB
8974 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
8976 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
8978 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
8979 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
8981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8985 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
8986 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
8988 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
8991 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
8992 struct __sanitizer_cdbr *cdbr =
8993 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
8995 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
8999 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9001 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9003 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9004 return REAL(cdbr_entries)(cdbr);
9007 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9008 const void **data, SIZE_T *datalen) {
9010 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9012 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9013 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9016 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9019 if (data && datalen)
9020 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9025 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9026 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9028 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9030 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9032 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9033 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9038 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9039 if (data && datalen)
9040 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9045 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9047 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9049 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9050 REAL(cdbr_close)(cdbr);
9053 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9055 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9056 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9058 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9062 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9063 SIZE_T keylen, const void *data, SIZE_T datalen) {
9065 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9067 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9068 if (data && datalen)
9069 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9071 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9072 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9074 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9078 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9079 SIZE_T datalen, u32 *index) {
9081 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9083 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9084 if (data && datalen)
9085 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9086 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9089 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9091 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9096 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9097 SIZE_T keylen, u32 index) {
9099 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9101 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9103 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9104 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9106 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9110 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9111 const char descr[16], u32 (*seedgen)(void)) {
9113 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9114 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9116 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9118 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9120 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9121 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9124 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9126 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9131 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9133 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9135 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9136 REAL(cdbw_close)(cdbw);
9140 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9141 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9142 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9143 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9144 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9145 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9146 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9147 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9148 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9149 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9150 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9151 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9156 #if SANITIZER_INTERCEPT_GETFSENT
9157 INTERCEPTOR(void *, getfsent) {
9159 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9160 void *ret = REAL(getfsent)();
9162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9166 INTERCEPTOR(void *, getfsspec, const char *spec) {
9168 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9170 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, REAL(strlen)(spec) + 1);
9171 void *ret = REAL(getfsspec)(spec);
9173 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9177 INTERCEPTOR(void *, getfsfile, const char *file) {
9179 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9181 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, REAL(strlen)(file) + 1);
9182 void *ret = REAL(getfsfile)(file);
9184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9188 #define INIT_GETFSENT \
9189 COMMON_INTERCEPT_FUNCTION(getfsent); \
9190 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9191 COMMON_INTERCEPT_FUNCTION(getfsfile);
9193 #define INIT_GETFSENT
9196 #if SANITIZER_INTERCEPT_ARC4RANDOM
9197 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9199 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9200 REAL(arc4random_buf)(buf, len);
9202 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9205 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9207 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9209 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9210 REAL(arc4random_addrandom)(dat, datlen);
9213 #define INIT_ARC4RANDOM \
9214 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9215 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9217 #define INIT_ARC4RANDOM
9220 #if SANITIZER_INTERCEPT_POPEN
9221 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9223 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9225 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, REAL(strlen)(command) + 1);
9227 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9228 __sanitizer_FILE *res = REAL(popen)(command, type);
9229 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9230 if (res) unpoison_file(res);
9233 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9238 #if SANITIZER_INTERCEPT_POPENVE
9239 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9240 char *const *argv, char *const *envp, const char *type) {
9242 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9244 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
9246 for (char *const *pa = argv; ; ++pa) {
9247 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9250 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9254 for (char *const *pa = envp; ; ++pa) {
9255 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9258 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1);
9262 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, REAL(strlen)(type) + 1);
9263 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9264 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9265 if (res) unpoison_file(res);
9268 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9270 #define INIT_POPENVE
9273 #if SANITIZER_INTERCEPT_PCLOSE
9274 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9276 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9277 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9278 const FileMetadata *m = GetInterceptorMetadata(fp);
9279 int res = REAL(pclose)(fp);
9281 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9282 DeleteInterceptorMetadata(fp);
9286 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9291 #if SANITIZER_INTERCEPT_FUNOPEN
9292 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9293 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9294 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9295 typedef int (*funopen_closefn)(void *cookie);
9297 struct WrappedFunopenCookie {
9299 funopen_readfn real_read;
9300 funopen_writefn real_write;
9301 funopen_seekfn real_seek;
9302 funopen_closefn real_close;
9305 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9306 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9307 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9308 funopen_readfn real_read = wrapped_cookie->real_read;
9309 return real_read(wrapped_cookie->real_cookie, buf, len);
9312 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9313 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9314 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9315 funopen_writefn real_write = wrapped_cookie->real_write;
9316 return real_write(wrapped_cookie->real_cookie, buf, len);
9319 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9320 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9321 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9322 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9323 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9326 static int wrapped_funopen_close(void *cookie) {
9327 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9328 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9329 funopen_closefn real_close = wrapped_cookie->real_close;
9330 int res = real_close(wrapped_cookie->real_cookie);
9331 InternalFree(wrapped_cookie);
9335 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9336 funopen_writefn writefn, funopen_seekfn seekfn,
9337 funopen_closefn closefn) {
9339 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9342 WrappedFunopenCookie *wrapped_cookie =
9343 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9344 wrapped_cookie->real_cookie = cookie;
9345 wrapped_cookie->real_read = readfn;
9346 wrapped_cookie->real_write = writefn;
9347 wrapped_cookie->real_seek = seekfn;
9348 wrapped_cookie->real_close = closefn;
9350 __sanitizer_FILE *res =
9351 REAL(funopen)(wrapped_cookie,
9352 readfn ? wrapped_funopen_read : nullptr,
9353 writefn ? wrapped_funopen_write : nullptr,
9354 seekfn ? wrapped_funopen_seek : nullptr,
9355 closefn ? wrapped_funopen_close : nullptr);
9360 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9362 #define INIT_FUNOPEN
9365 #if SANITIZER_INTERCEPT_FUNOPEN2
9366 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9367 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9368 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9369 typedef int (*funopen2_flushfn)(void *cookie);
9370 typedef int (*funopen2_closefn)(void *cookie);
9372 struct WrappedFunopen2Cookie {
9374 funopen2_readfn real_read;
9375 funopen2_writefn real_write;
9376 funopen2_seekfn real_seek;
9377 funopen2_flushfn real_flush;
9378 funopen2_closefn real_close;
9381 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9382 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9383 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9384 funopen2_readfn real_read = wrapped_cookie->real_read;
9385 return real_read(wrapped_cookie->real_cookie, buf, len);
9388 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9390 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9391 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9392 funopen2_writefn real_write = wrapped_cookie->real_write;
9393 return real_write(wrapped_cookie->real_cookie, buf, len);
9396 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9397 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9398 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9399 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9400 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9403 static int wrapped_funopen2_flush(void *cookie) {
9404 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9405 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9406 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9407 return real_flush(wrapped_cookie->real_cookie);
9410 static int wrapped_funopen2_close(void *cookie) {
9411 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9412 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9413 funopen2_closefn real_close = wrapped_cookie->real_close;
9414 int res = real_close(wrapped_cookie->real_cookie);
9415 InternalFree(wrapped_cookie);
9419 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9420 funopen2_writefn writefn, funopen2_seekfn seekfn,
9421 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9423 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9426 WrappedFunopen2Cookie *wrapped_cookie =
9427 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9428 wrapped_cookie->real_cookie = cookie;
9429 wrapped_cookie->real_read = readfn;
9430 wrapped_cookie->real_write = writefn;
9431 wrapped_cookie->real_seek = seekfn;
9432 wrapped_cookie->real_flush = flushfn;
9433 wrapped_cookie->real_close = closefn;
9435 __sanitizer_FILE *res =
9436 REAL(funopen2)(wrapped_cookie,
9437 readfn ? wrapped_funopen2_read : nullptr,
9438 writefn ? wrapped_funopen2_write : nullptr,
9439 seekfn ? wrapped_funopen2_seek : nullptr,
9440 flushfn ? wrapped_funopen2_flush : nullptr,
9441 closefn ? wrapped_funopen2_close : nullptr);
9446 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9448 #define INIT_FUNOPEN2
9451 #if SANITIZER_INTERCEPT_FDEVNAME
9452 INTERCEPTOR(char *, fdevname, int fd) {
9454 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9455 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9456 char *name = REAL(fdevname)(fd);
9458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
9460 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9465 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9467 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9468 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9469 char *name = REAL(fdevname_r)(fd, buf, len);
9470 if (name && buf && len > 0) {
9471 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
9473 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9478 #define INIT_FDEVNAME \
9479 COMMON_INTERCEPT_FUNCTION(fdevname); \
9480 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9482 #define INIT_FDEVNAME
9485 #if SANITIZER_INTERCEPT_GETUSERSHELL
9486 INTERCEPTOR(char *, getusershell) {
9488 COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
9489 char *res = REAL(getusershell)();
9491 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9495 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9497 #define INIT_GETUSERSHELL
9500 #if SANITIZER_INTERCEPT_SL_INIT
9501 INTERCEPTOR(void *, sl_init) {
9503 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9504 void *res = REAL(sl_init)();
9506 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9510 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9512 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9514 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9516 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9517 int res = REAL(sl_add)(sl, item);
9519 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9523 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9525 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9527 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9529 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, REAL(strlen)(item) + 1);
9530 char *res = REAL(sl_find)(sl, item);
9532 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
9536 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9538 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9540 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9541 REAL(sl_free)(sl, freeall);
9544 #define INIT_SL_INIT \
9545 COMMON_INTERCEPT_FUNCTION(sl_init); \
9546 COMMON_INTERCEPT_FUNCTION(sl_add); \
9547 COMMON_INTERCEPT_FUNCTION(sl_find); \
9548 COMMON_INTERCEPT_FUNCTION(sl_free);
9550 #define INIT_SL_INIT
9553 #if SANITIZER_INTERCEPT_GETRANDOM
9554 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
9556 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
9557 SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
9559 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
9563 #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
9565 #define INIT_GETRANDOM
9568 static void InitializeCommonInterceptors() {
9570 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
9571 interceptor_metadata_map =
9572 new ((void *)&metadata_mem) MetadataHashMap(); // NOLINT
9622 INIT_LOCALTIME_AND_FRIENDS;
9631 INIT_GETPWNAM_AND_FRIENDS;
9632 INIT_GETPWNAM_R_AND_FRIENDS;
9647 INIT_PTHREAD_GETSCHEDPARAM;
9652 INIT_GETHOSTBYNAME2;
9653 INIT_GETHOSTBYNAME_R;
9654 INIT_GETHOSTBYNAME2_R;
9655 INIT_GETHOSTBYADDR_R;
9675 INIT_GET_CURRENT_DIR_NAME;
9685 INIT_CANONICALIZE_FILE_NAME;
9687 INIT_SCHED_GETAFFINITY;
9688 INIT_SCHED_GETPARAM;
9691 INIT_XPG_STRERROR_R;
9704 INIT_PTHREAD_SIGMASK;
9707 INIT_PTHREAD_MUTEX_LOCK;
9708 INIT_PTHREAD_MUTEX_UNLOCK;
9709 INIT___PTHREAD_MUTEX_LOCK;
9710 INIT___PTHREAD_MUTEX_UNLOCK;
9711 INIT___LIBC_MUTEX_LOCK;
9712 INIT___LIBC_MUTEX_UNLOCK;
9713 INIT___LIBC_THR_SETCANCELSTATE;
9721 INIT_ETHER_NTOA_ATON;
9726 INIT_PTHREAD_ATTR_GET;
9727 INIT_PTHREAD_ATTR_GET_SCHED;
9728 INIT_PTHREAD_ATTR_GETINHERITSCHED;
9729 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
9730 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
9731 INIT_PTHREAD_MUTEXATTR_GETTYPE;
9732 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
9733 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
9734 INIT_PTHREAD_MUTEXATTR_GETROBUST;
9735 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
9736 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
9737 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
9738 INIT_PTHREAD_CONDATTR_GETPSHARED;
9739 INIT_PTHREAD_CONDATTR_GETCLOCK;
9740 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
9746 INIT_PTHREAD_SETNAME_NP;
9747 INIT_PTHREAD_GETNAME_NP;
9765 INIT_IF_INDEXTONAME;
9773 INIT_LIBIO_INTERNALS;
9776 INIT_OPEN_MEMSTREAM;
9780 INIT_DLOPEN_DLCLOSE;
9786 INIT_PTHREAD_SETCANCEL;
9788 INIT_PROCESS_VM_READV;
9794 INIT_EVENTFD_READ_WRITE;
9800 // FIXME: add other *stat interceptors.
9812 INIT_GROUP_FROM_GID;
9813 INIT_GID_FROM_GROUP;
9817 INIT_GETGROUPMEMBERSHIP;
9820 INIT_NAME_TO_HANDLE_AT;
9821 INIT_OPEN_BY_HANDLE_AT;
9831 INIT_MI_VECTOR_HASH;
9839 INIT_SYSCTLGETMIBINFO;