Debian Codegraph @

libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc

raw
1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common function interceptors for tools like AddressSanitizer,
10 // ThreadSanitizer, MemorySanitizer, etc.
11 //
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_DLOPEN
25 // COMMON_INTERCEPTOR_ON_EXIT
26 // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
27 // COMMON_INTERCEPTOR_HANDLE_RECVMSG
28 // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
29 // COMMON_INTERCEPTOR_MMAP_IMPL
30 // COMMON_INTERCEPTOR_MUNMAP_IMPL
31 // COMMON_INTERCEPTOR_COPY_STRING
32 // COMMON_INTERCEPTOR_STRNDUP_IMPL
33 // COMMON_INTERCEPTOR_STRERROR
34 //===----------------------------------------------------------------------===//
35
36 #include <stdarg.h>
37
38 #include "interception/interception.h"
39 #include "sanitizer_addrhashmap.h"
40 #include "sanitizer_dl.h"
41 #include "sanitizer_errno.h"
42 #include "sanitizer_placement_new.h"
43 #include "sanitizer_platform_interceptors.h"
44 #include "sanitizer_platform_limits_posix.h"
45 #include "sanitizer_symbolizer.h"
46 #include "sanitizer_tls_get_addr.h"
47
48 #if SANITIZER_INTERCEPTOR_HOOKS
49 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
50 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
51 SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
52 #else
53 #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
54 #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
55
56 #endif // SANITIZER_INTERCEPTOR_HOOKS
57
58 #if SANITIZER_WINDOWS && !defined(va_copy)
59 #define va_copy(dst, src) ((dst) = (src))
60 #endif // _WIN32
61
62 #if SANITIZER_FREEBSD
63 #define pthread_setname_np pthread_set_name_np
64 #define inet_aton __inet_aton
65 #define inet_pton __inet_pton
66 #define iconv __bsd_iconv
67 #endif
68
69 #if SANITIZER_NETBSD
70 #define clock_getres __clock_getres50
71 #define clock_gettime __clock_gettime50
72 #define clock_settime __clock_settime50
73 #define ctime __ctime50
74 #define ctime_r __ctime_r50
75 #define devname __devname50
76 #define fgetpos __fgetpos50
77 #define fsetpos __fsetpos50
78 #define fstatvfs __fstatvfs90
79 #define fstatvfs1 __fstatvfs190
80 #define fts_children __fts_children60
81 #define fts_close __fts_close60
82 #define fts_open __fts_open60
83 #define fts_read __fts_read60
84 #define fts_set __fts_set60
85 #define getitimer __getitimer50
86 #define getmntinfo __getmntinfo90
87 #define getpwent __getpwent50
88 #define getpwnam __getpwnam50
89 #define getpwnam_r __getpwnam_r50
90 #define getpwuid __getpwuid50
91 #define getpwuid_r __getpwuid_r50
92 #define getutent __getutent50
93 #define getutxent __getutxent50
94 #define getutxid __getutxid50
95 #define getutxline __getutxline50
96 #define getvfsstat __getvfsstat90
97 #define pututxline __pututxline50
98 #define glob __glob30
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 sigaltstack __sigaltstack14
113 #define sigemptyset __sigemptyset14
114 #define sigfillset __sigfillset14
115 #define sigpending __sigpending14
116 #define sigprocmask __sigprocmask14
117 #define sigtimedwait __sigtimedwait50
118 #define stat __stat50
119 #define statvfs __statvfs90
120 #define statvfs1 __statvfs190
121 #define time __time50
122 #define times __times13
123 #define unvis __unvis50
124 #define wait3 __wait350
125 #define wait4 __wait450
126 extern const unsigned short *_ctype_tab_;
127 extern const short *_toupper_tab_;
128 extern const short *_tolower_tab_;
129 #endif
130
131 #if SANITIZER_LINUX && SANITIZER_SPARC32
132 // On 32-bit Linux/sparc64, double and long double are identical and glibc
133 // uses a __nldbl_ (no long double) prefix for various stdio functions.
134 # define __isoc23_fscanf __nldbl___isoc23_fscanf
135 # define __isoc23_scanf __nldbl___isoc23_scanf
136 # define __isoc23_sscanf __nldbl___isoc23_sscanf
137 # define __isoc23_vfscanf __nldbl___isoc23_vfscanf
138 # define __isoc23_vscanf __nldbl___isoc23_vscanf
139 # define __isoc23_vsscanf __nldbl___isoc23_vsscanf
140 # define __isoc99_fscanf __nldbl___isoc99_fscanf
141 # define __isoc99_scanf __nldbl___isoc99_scanf
142 # define __isoc99_sscanf __nldbl___isoc99_sscanf
143 # define __isoc99_vfscanf __nldbl___isoc99_vfscanf
144 # define __isoc99_vscanf __nldbl___isoc99_vscanf
145 # define __isoc99_vsscanf __nldbl___isoc99_vsscanf
146 # define asprintf __nldbl_asprintf
147 # define fprintf __nldbl_fprintf
148 # define fscanf __nldbl_fscanf
149 # define printf __nldbl_printf
150 # define scanf __nldbl_scanf
151 # define snprintf __nldbl_snprintf
152 # define sprintf __nldbl_sprintf
153 # define sscanf __nldbl_sscanf
154 # define vasprintf __nldbl_vasprintf
155 # define vfprintf __nldbl_vfprintf
156 # define vfscanf __nldbl_vfscanf
157 # define vprintf __nldbl_vprintf
158 # define vscanf __nldbl_vscanf
159 # define vsnprintf __nldbl_vsnprintf
160 # define vsprintf __nldbl_vsprintf
161 # define vsscanf __nldbl_vsscanf
162 #endif
163
164 #if SANITIZER_MUSL && \
165 (defined(__i386__) || defined(__arm__) || SANITIZER_MIPS32 || SANITIZER_PPC32)
166 // musl 1.2.0 on existing 32-bit architectures uses new symbol names for the
167 // time-related functions that take 64-bit time_t values. See
168 // https://musl.libc.org/time64.html
169 #define adjtime __adjtime64
170 #define adjtimex __adjtimex_time64
171 #define aio_suspend __aio_suspend_time64
172 #define clock_adjtime __clock_adjtime64
173 #define clock_getres __clock_getres_time64
174 #define clock_gettime __clock_gettime64
175 #define clock_nanosleep __clock_nanosleep_time64
176 #define clock_settime __clock_settime64
177 #define cnd_timedwait __cnd_timedwait_time64
178 #define ctime __ctime64
179 #define ctime_r __ctime64_r
180 #define difftime __difftime64
181 #define dlsym __dlsym_time64
182 #define fstatat __fstatat_time64
183 #define fstat __fstat_time64
184 #define ftime __ftime64
185 #define futimens __futimens_time64
186 #define futimesat __futimesat_time64
187 #define futimes __futimes_time64
188 #define getitimer __getitimer_time64
189 #define getrusage __getrusage_time64
190 #define gettimeofday __gettimeofday_time64
191 #define gmtime __gmtime64
192 #define gmtime_r __gmtime64_r
193 #define localtime __localtime64
194 #define localtime_r __localtime64_r
195 #define lstat __lstat_time64
196 #define lutimes __lutimes_time64
197 #define mktime __mktime64
198 #define mq_timedreceive __mq_timedreceive_time64
199 #define mq_timedsend __mq_timedsend_time64
200 #define mtx_timedlock __mtx_timedlock_time64
201 #define nanosleep __nanosleep_time64
202 #define ppoll __ppoll_time64
203 #define pselect __pselect_time64
204 #define pthread_cond_timedwait __pthread_cond_timedwait_time64
205 #define pthread_mutex_timedlock __pthread_mutex_timedlock_time64
206 #define pthread_rwlock_timedrdlock __pthread_rwlock_timedrdlock_time64
207 #define pthread_rwlock_timedwrlock __pthread_rwlock_timedwrlock_time64
208 #define pthread_timedjoin_np __pthread_timedjoin_np_time64
209 #define recvmmsg __recvmmsg_time64
210 #define sched_rr_get_interval __sched_rr_get_interval_time64
211 #define select __select_time64
212 #define semtimedop __semtimedop_time64
213 #define sem_timedwait __sem_timedwait_time64
214 #define setitimer __setitimer_time64
215 #define settimeofday __settimeofday_time64
216 #define sigtimedwait __sigtimedwait_time64
217 #define stat __stat_time64
218 #define stime __stime64
219 #define thrd_sleep __thrd_sleep_time64
220 #define timegm __timegm_time64
221 #define timerfd_gettime __timerfd_gettime64
222 #define timerfd_settime __timerfd_settime64
223 #define timer_gettime __timer_gettime64
224 #define timer_settime __timer_settime64
225 #define timespec_get __timespec_get_time64
226 #define time __time64
227 #define utimensat __utimensat_time64
228 #define utimes __utimes_time64
229 #define utime __utime64
230 #define wait3 __wait3_time64
231 #define wait4 __wait4_time64
232 #endif
233
234 #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
235 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
236 #endif
237
238 #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
239 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
240 #endif
241
242 #ifndef COMMON_INTERCEPTOR_FD_ACCESS
243 #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
244 #endif
245
246 #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
247 #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
248 #endif
249
250 #ifndef COMMON_INTERCEPTOR_FILE_OPEN
251 #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
252 #endif
253
254 #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
255 #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
256 #endif
257
258 #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
259 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
260 #endif
261
262 #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
263 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
264 #endif
265
266 #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
267 #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
268 COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
269 #endif
270
271 #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
272 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
273 #endif
274
275 #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
276 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
277 common_flags()->strict_string_checks ? (internal_strlen(s)) + 1 : (n) )
278
279 #ifndef COMMON_INTERCEPTOR_DLOPEN
280 #define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
281 ({ CheckNoDeepBind(filename, flag); REAL(dlopen)(filename, flag); })
282 #endif
283
284 #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
285 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
286 #endif
287
288 #ifndef COMMON_INTERCEPTOR_ACQUIRE
289 #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
290 #endif
291
292 #ifndef COMMON_INTERCEPTOR_RELEASE
293 #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
294 #endif
295
296 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
297 #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
298 #endif
299
300 #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
301 #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
302 #endif
303
304 #ifdef SANITIZER_NLDBL_VERSION
305 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
306 COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
307 #else
308 #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
309 COMMON_INTERCEPT_FUNCTION(fn)
310 #endif
311
312 #if SANITIZER_GLIBC
313 // If we could not find the versioned symbol, fall back to an unversioned
314 // lookup. This is needed to work around a GLibc bug that causes dlsym
315 // with RTLD_NEXT to return the oldest versioned symbol.
316 // See https://sourceware.org/bugzilla/show_bug.cgi?id=14932.
317 // For certain symbols (e.g. regexec) we have to perform a versioned lookup,
318 // but that versioned symbol will only exist for architectures where the
319 // oldest Glibc version pre-dates support for that architecture.
320 // For example, regexec@GLIBC_2.3.4 exists on x86_64, but not RISC-V.
321 // See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98920.
322 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
323 COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(fn, ver)
324 #else
325 #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
326 COMMON_INTERCEPT_FUNCTION(fn)
327 #endif
328
329 #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
330 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
331 off) \
332 { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
333 #endif
334
335 #ifndef COMMON_INTERCEPTOR_MUNMAP_IMPL
336 #define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz) \
337 { return REAL(munmap)(addr, sz); }
338 #endif
339
340 #ifndef COMMON_INTERCEPTOR_COPY_STRING
341 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
342 #endif
343
344 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
345 #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
346 COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
347 uptr copy_length = internal_strnlen(s, size); \
348 char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
349 if (common_flags()->intercept_strndup) { \
350 COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min<uptr>(size, copy_length + 1)); \
351 } \
352 if (new_mem) { \
353 COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
354 internal_memcpy(new_mem, s, copy_length); \
355 new_mem[copy_length] = '\0'; \
356 } \
357 return new_mem;
358 #endif
359
360 #ifndef COMMON_INTERCEPTOR_STRERROR
361 #define COMMON_INTERCEPTOR_STRERROR() {}
362 #endif
363
364 struct FileMetadata {
365 // For open_memstream().
366 char **addr;
367 SIZE_T *size;
368 };
369
370 struct CommonInterceptorMetadata {
371 enum {
372 CIMT_INVALID = 0,
373 CIMT_FILE
374 } type;
375 union {
376 FileMetadata file;
377 };
378 };
379
380 #if SI_POSIX
381 typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
382
383 static MetadataHashMap *interceptor_metadata_map;
384
385 UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
386 const FileMetadata &file) {
387 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
388 CHECK(h.created());
389 h->type = CommonInterceptorMetadata::CIMT_FILE;
390 h->file = file;
391 }
392
393 UNUSED static const FileMetadata *GetInterceptorMetadata(
394 __sanitizer_FILE *addr) {
395 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
396 /* remove */ false,
397 /* create */ false);
398 if (addr && h.exists()) {
399 CHECK(!h.created());
400 CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
401 return &h->file;
402 } else {
403 return 0;
404 }
405 }
406
407 UNUSED static void DeleteInterceptorMetadata(void *addr) {
408 MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
409 CHECK(h.exists());
410 }
411 #endif // SI_POSIX
412
413 #if SANITIZER_INTERCEPT_STRLEN
414 INTERCEPTOR(SIZE_T, strlen, const char *s) {
415 // Sometimes strlen is called prior to InitializeCommonInterceptors,
416 // in which case the REAL(strlen) typically used in
417 // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
418 // to handle that.
419 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
420 return internal_strlen(s);
421 void *ctx;
422 COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
423 SIZE_T result = REAL(strlen)(s);
424 if (common_flags()->intercept_strlen)
425 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
426 return result;
427 }
428 #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
429 #else
430 #define INIT_STRLEN
431 #endif
432
433 #if SANITIZER_INTERCEPT_STRNLEN
434 INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
435 void *ctx;
436 COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
437 SIZE_T length = REAL(strnlen)(s, maxlen);
438 if (common_flags()->intercept_strlen)
439 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
440 return length;
441 }
442 #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
443 #else
444 #define INIT_STRNLEN
445 #endif
446
447 #if SANITIZER_INTERCEPT_STRNDUP
448 INTERCEPTOR(char*, strndup, const char *s, usize size) {
449 void *ctx;
450 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
451 }
452 #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
453 #else
454 #define INIT_STRNDUP
455 #endif // SANITIZER_INTERCEPT_STRNDUP
456
457 #if SANITIZER_INTERCEPT___STRNDUP
458 INTERCEPTOR(char*, __strndup, const char *s, usize size) {
459 void *ctx;
460 COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
461 }
462 #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
463 #else
464 #define INIT___STRNDUP
465 #endif // SANITIZER_INTERCEPT___STRNDUP
466
467 #if SANITIZER_INTERCEPT_TEXTDOMAIN
468 INTERCEPTOR(char*, textdomain, const char *domainname) {
469 void *ctx;
470 COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
471 if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
472 char *domain = REAL(textdomain)(domainname);
473 if (domain) {
474 COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, internal_strlen(domain) + 1);
475 }
476 return domain;
477 }
478 #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
479 #else
480 #define INIT_TEXTDOMAIN
481 #endif
482
483 #if SANITIZER_INTERCEPT_STRCMP || SANITIZER_INTERCEPT_MEMCMP
484 static inline int CharCmpX(unsigned char c1, unsigned char c2) {
485 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
486 }
487 #endif
488
489 #if SANITIZER_INTERCEPT_STRCMP
490 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
491 const char *s1, const char *s2, int result)
492
493 INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
494 void *ctx;
495 COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
496 unsigned char c1, c2;
497 uptr i;
498 for (i = 0;; i++) {
499 c1 = (unsigned char)s1[i];
500 c2 = (unsigned char)s2[i];
501 if (c1 != c2 || c1 == '\0') break;
502 }
503 if (common_flags()->intercept_strcmp) {
504 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
505 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
506 }
507 int result = CharCmpX(c1, c2);
508 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
509 s2, result);
510 return result;
511 }
512
513 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
514 const char *s1, const char *s2, usize n,
515 int result)
516
517 INTERCEPTOR(int, strncmp, const char *s1, const char *s2, usize size) {
518 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
519 return internal_strncmp(s1, s2, size);
520 void *ctx;
521 COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
522 unsigned char c1 = 0, c2 = 0;
523 usize i;
524 for (i = 0; i < size; i++) {
525 c1 = (unsigned char)s1[i];
526 c2 = (unsigned char)s2[i];
527 if (c1 != c2 || c1 == '\0') break;
528 }
529 usize i1 = i;
530 usize i2 = i;
531 if (common_flags()->strict_string_checks) {
532 for (; i1 < size && s1[i1]; i1++) {}
533 for (; i2 < size && s2[i2]; i2++) {}
534 }
535 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
536 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
537 int result = CharCmpX(c1, c2);
538 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
539 s2, size, result);
540 return result;
541 }
542
543 #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
544 #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
545 #else
546 #define INIT_STRCMP
547 #define INIT_STRNCMP
548 #endif
549
550 #if SANITIZER_INTERCEPT_STRCASECMP
551 static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
552 int c1_low = ToLower(c1);
553 int c2_low = ToLower(c2);
554 return c1_low - c2_low;
555 }
556
557 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
558 const char *s1, const char *s2, int result)
559
560 INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
561 void *ctx;
562 COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
563 unsigned char c1 = 0, c2 = 0;
564 uptr i;
565 for (i = 0;; i++) {
566 c1 = (unsigned char)s1[i];
567 c2 = (unsigned char)s2[i];
568 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
569 }
570 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
571 COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
572 int result = CharCaseCmp(c1, c2);
573 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
574 s1, s2, result);
575 return result;
576 }
577
578 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
579 const char *s1, const char *s2, usize size,
580 int result)
581
582 INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
583 void *ctx;
584 COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
585 unsigned char c1 = 0, c2 = 0;
586 usize i;
587 for (i = 0; i < size; i++) {
588 c1 = (unsigned char)s1[i];
589 c2 = (unsigned char)s2[i];
590 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
591 }
592 usize i1 = i;
593 usize i2 = i;
594 if (common_flags()->strict_string_checks) {
595 for (; i1 < size && s1[i1]; i1++) {}
596 for (; i2 < size && s2[i2]; i2++) {}
597 }
598 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
599 COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
600 int result = CharCaseCmp(c1, c2);
601 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
602 s1, s2, size, result);
603 return result;
604 }
605
606 #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
607 #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
608 #else
609 #define INIT_STRCASECMP
610 #define INIT_STRNCASECMP
611 #endif
612
613 #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
614 static inline void StrstrCheck(void *ctx, char *r, const char *s1,
615 const char *s2) {
616 uptr len1 = internal_strlen(s1);
617 uptr len2 = internal_strlen(s2);
618 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
619 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
620 }
621 #endif
622
623 #if SANITIZER_INTERCEPT_STRSTR
624
625 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
626 const char *s1, const char *s2, char *result)
627
628 INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
629 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
630 return internal_strstr(s1, s2);
631 void *ctx;
632 COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
633 char *r = REAL(strstr)(s1, s2);
634 if (common_flags()->intercept_strstr)
635 StrstrCheck(ctx, r, s1, s2);
636 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
637 s2, r);
638 return r;
639 }
640
641 #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
642 #else
643 #define INIT_STRSTR
644 #endif
645
646 #if SANITIZER_INTERCEPT_STRCASESTR
647
648 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
649 const char *s1, const char *s2, char *result)
650
651 INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
652 void *ctx;
653 COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
654 char *r = REAL(strcasestr)(s1, s2);
655 if (common_flags()->intercept_strstr)
656 StrstrCheck(ctx, r, s1, s2);
657 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
658 s1, s2, r);
659 return r;
660 }
661
662 #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
663 #else
664 #define INIT_STRCASESTR
665 #endif
666
667 #if SANITIZER_INTERCEPT_STRTOK
668
669 INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
670 void *ctx;
671 COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
672 if (!common_flags()->intercept_strtok) {
673 return REAL(strtok)(str, delimiters);
674 }
675 if (common_flags()->strict_string_checks) {
676 // If strict_string_checks is enabled, we check the whole first argument
677 // string on the first call (strtok saves this string in a static buffer
678 // for subsequent calls). We do not need to check strtok's result.
679 // As the delimiters can change, we check them every call.
680 if (str != nullptr) {
681 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
682 }
683 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
684 internal_strlen(delimiters) + 1);
685 return REAL(strtok)(str, delimiters);
686 } else {
687 // However, when strict_string_checks is disabled we cannot check the
688 // whole string on the first call. Instead, we check the result string
689 // which is guaranteed to be a NULL-terminated substring of the first
690 // argument. We also conservatively check one character of str and the
691 // delimiters.
692 if (str != nullptr) {
693 COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
694 }
695 COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
696 char *result = REAL(strtok)(str, delimiters);
697 if (result != nullptr) {
698 COMMON_INTERCEPTOR_READ_RANGE(ctx, result, internal_strlen(result) + 1);
699 } else if (str != nullptr) {
700 // No delimiter were found, it's safe to assume that the entire str was
701 // scanned.
702 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
703 }
704 return result;
705 }
706 }
707
708 #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
709 #else
710 #define INIT_STRTOK
711 #endif
712
713 #if SANITIZER_INTERCEPT_MEMMEM
714 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
715 const void *s1, SIZE_T len1, const void *s2,
716 SIZE_T len2, void *result)
717
718 INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
719 SIZE_T len2) {
720 void *ctx;
721 COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
722 void *r = REAL(memmem)(s1, len1, s2, len2);
723 if (common_flags()->intercept_memmem) {
724 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
725 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
726 }
727 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
728 s1, len1, s2, len2, r);
729 return r;
730 }
731
732 #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
733 #else
734 #define INIT_MEMMEM
735 #endif // SANITIZER_INTERCEPT_MEMMEM
736
737 #if SANITIZER_INTERCEPT_STRCHR
738 INTERCEPTOR(char*, strchr, const char *s, int c) {
739 void *ctx;
740 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
741 return internal_strchr(s, c);
742 COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
743 char *result = REAL(strchr)(s, c);
744 if (common_flags()->intercept_strchr) {
745 // Keep strlen as macro argument, as macro may ignore it.
746 COMMON_INTERCEPTOR_READ_STRING(ctx, s,
747 (result ? result - s : internal_strlen(s)) + 1);
748 }
749 return result;
750 }
751 #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
752 #else
753 #define INIT_STRCHR
754 #endif
755
756 #if SANITIZER_INTERCEPT_STRCHRNUL
757 INTERCEPTOR(char*, strchrnul, const char *s, int c) {
758 void *ctx;
759 COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
760 char *result = REAL(strchrnul)(s, c);
761 uptr len = result - s + 1;
762 if (common_flags()->intercept_strchr)
763 COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
764 return result;
765 }
766 #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
767 #else
768 #define INIT_STRCHRNUL
769 #endif
770
771 #if SANITIZER_INTERCEPT_STRRCHR
772 INTERCEPTOR(char*, strrchr, const char *s, int c) {
773 void *ctx;
774 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
775 return internal_strrchr(s, c);
776 COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
777 if (common_flags()->intercept_strchr)
778 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
779 return REAL(strrchr)(s, c);
780 }
781 #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
782 #else
783 #define INIT_STRRCHR
784 #endif
785
786 #if SANITIZER_INTERCEPT_STRSPN
787 INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
788 void *ctx;
789 COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
790 SIZE_T r = REAL(strspn)(s1, s2);
791 if (common_flags()->intercept_strspn) {
792 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
793 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
794 }
795 return r;
796 }
797
798 INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
799 void *ctx;
800 COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
801 SIZE_T r = REAL(strcspn)(s1, s2);
802 if (common_flags()->intercept_strspn) {
803 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
804 COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
805 }
806 return r;
807 }
808
809 #define INIT_STRSPN \
810 COMMON_INTERCEPT_FUNCTION(strspn); \
811 COMMON_INTERCEPT_FUNCTION(strcspn);
812 #else
813 #define INIT_STRSPN
814 #endif
815
816 #if SANITIZER_INTERCEPT_STRPBRK
817 INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
818 void *ctx;
819 COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
820 char *r = REAL(strpbrk)(s1, s2);
821 if (common_flags()->intercept_strpbrk) {
822 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
823 COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
824 r ? r - s1 + 1 : internal_strlen(s1) + 1);
825 }
826 return r;
827 }
828
829 #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
830 #else
831 #define INIT_STRPBRK
832 #endif
833
834 #if SANITIZER_INTERCEPT_MEMCMP
835 DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
836 const void *s1, const void *s2, usize n,
837 int result)
838
839 // Common code for `memcmp` and `bcmp`.
840 int MemcmpInterceptorCommon(void *ctx,
841 int (*real_fn)(const void *, const void *, usize),
842 const void *a1, const void *a2, usize size) {
843 if (common_flags()->intercept_memcmp) {
844 if (common_flags()->strict_memcmp) {
845 // Check the entire regions even if the first bytes of the buffers are
846 // different.
847 COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
848 COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
849 // Fallthrough to REAL(memcmp) below.
850 } else {
851 unsigned char c1 = 0, c2 = 0;
852 const unsigned char *s1 = (const unsigned char*)a1;
853 const unsigned char *s2 = (const unsigned char*)a2;
854 usize i;
855 for (i = 0; i < size; i++) {
856 c1 = s1[i];
857 c2 = s2[i];
858 if (c1 != c2) break;
859 }
860 COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
861 COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
862 int r = CharCmpX(c1, c2);
863 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
864 a1, a2, size, r);
865 return r;
866 }
867 }
868 int result = real_fn(a1, a2, size);
869 CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
870 a2, size, result);
871 return result;
872 }
873
874 INTERCEPTOR(int, memcmp, const void *a1, const void *a2, usize size) {
875 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
876 return internal_memcmp(a1, a2, size);
877 void *ctx;
878 COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
879 return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
880 }
881
882 #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
883 #else
884 #define INIT_MEMCMP
885 #endif
886
887 #if SANITIZER_INTERCEPT_BCMP
888 INTERCEPTOR(int, bcmp, const void *a1, const void *a2, usize size) {
889 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
890 return internal_memcmp(a1, a2, size);
891 void *ctx;
892 COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
893 return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
894 }
895
896 #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
897 #else
898 #define INIT_BCMP
899 #endif
900
901 #if SANITIZER_INTERCEPT_MEMCHR
902 INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
903 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
904 return internal_memchr(s, c, n);
905 void *ctx;
906 COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
907 #if SANITIZER_WINDOWS
908 void *res;
909 if (REAL(memchr)) {
910 res = REAL(memchr)(s, c, n);
911 } else {
912 res = internal_memchr(s, c, n);
913 }
914 #else
915 void *res = REAL(memchr)(s, c, n);
916 #endif
917 uptr len = res ? (char *)res - (const char *)s + 1 : n;
918 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
919 return res;
920 }
921
922 #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
923 #else
924 #define INIT_MEMCHR
925 #endif
926
927 #if SANITIZER_INTERCEPT_MEMRCHR
928 INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
929 void *ctx;
930 COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
931 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
932 return REAL(memrchr)(s, c, n);
933 }
934
935 #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
936 #else
937 #define INIT_MEMRCHR
938 #endif
939
940 #if SANITIZER_INTERCEPT_FREXP
941 INTERCEPTOR(double, frexp, double x, int *exp) {
942 void *ctx;
943 COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
944 // Assuming frexp() always writes to |exp|.
945 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
946 double res = REAL(frexp)(x, exp);
947 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
948 return res;
949 }
950
951 #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
952 #else
953 #define INIT_FREXP
954 #endif // SANITIZER_INTERCEPT_FREXP
955
956 #if SANITIZER_INTERCEPT_FREXPF_FREXPL
957 INTERCEPTOR(float, frexpf, float x, int *exp) {
958 void *ctx;
959 COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
961 float res = REAL(frexpf)(x, exp);
962 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
963 return res;
964 }
965
966 INTERCEPTOR(long double, frexpl, long double x, int *exp) {
967 void *ctx;
968 COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
969 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
970 long double res = REAL(frexpl)(x, exp);
971 COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
972 return res;
973 }
974
975 #define INIT_FREXPF_FREXPL \
976 COMMON_INTERCEPT_FUNCTION(frexpf); \
977 COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
978 #else
979 #define INIT_FREXPF_FREXPL
980 #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
981
982 #if SI_POSIX
983 static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
984 SIZE_T iovlen, SIZE_T maxlen) {
985 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
986 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
987 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
988 maxlen -= sz;
989 }
990 }
991
992 static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
993 SIZE_T iovlen, SIZE_T maxlen) {
994 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
995 for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
996 SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
997 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
998 maxlen -= sz;
999 }
1000 }
1001 #endif
1002
1003 #if SANITIZER_INTERCEPT_READ
1004 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
1005 void *ctx;
1006 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
1007 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1008 // FIXME: under ASan the call below may write to freed memory and corrupt
1009 // its metadata. See
1010 // https://github.com/google/sanitizers/issues/321.
1011 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(read)(fd, ptr, count);
1012 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1013 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1014 return res;
1015 }
1016 #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
1017 #else
1018 #define INIT_READ
1019 #endif
1020
1021 #if SANITIZER_INTERCEPT_FREAD
1022 INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
1023 // libc file streams can call user-supplied functions, see fopencookie.
1024 void *ctx;
1025 COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
1026 // FIXME: under ASan the call below may write to freed memory and corrupt
1027 // its metadata. See
1028 // https://github.com/google/sanitizers/issues/321.
1029 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
1030 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
1031 return res;
1032 }
1033 #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
1034 #else
1035 #define INIT_FREAD
1036 #endif
1037
1038 #if SANITIZER_INTERCEPT_PREAD
1039 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1040 void *ctx;
1041 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
1042 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1043 // FIXME: under ASan the call below may write to freed memory and corrupt
1044 // its metadata. See
1045 // https://github.com/google/sanitizers/issues/321.
1046 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pread)(fd, ptr, count, offset);
1047 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1048 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1049 return res;
1050 }
1051 #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
1052 #else
1053 #define INIT_PREAD
1054 #endif
1055
1056 #if SANITIZER_INTERCEPT_PREAD64
1057 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
1058 void *ctx;
1059 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
1060 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1061 // FIXME: under ASan the call below may write to freed memory and corrupt
1062 // its metadata. See
1063 // https://github.com/google/sanitizers/issues/321.
1064 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pread64)(fd, ptr, count, offset);
1065 if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
1066 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1067 return res;
1068 }
1069 #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
1070 #else
1071 #define INIT_PREAD64
1072 #endif
1073
1074 #if SANITIZER_INTERCEPT_READV
1075 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
1076 int iovcnt) {
1077 void *ctx;
1078 COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
1079 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1080 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(readv)(fd, iov, iovcnt);
1081 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1082 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1083 return res;
1084 }
1085 #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
1086 #else
1087 #define INIT_READV
1088 #endif
1089
1090 #if SANITIZER_INTERCEPT_PREADV
1091 INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
1092 OFF_T offset) {
1093 void *ctx;
1094 COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
1095 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1096 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(preadv)(fd, iov, iovcnt, offset);
1097 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1098 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1099 return res;
1100 }
1101 #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
1102 #else
1103 #define INIT_PREADV
1104 #endif
1105
1106 #if SANITIZER_INTERCEPT_PREADV64
1107 INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
1108 OFF64_T offset) {
1109 void *ctx;
1110 COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
1111 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1112 SSIZE_T res =
1113 COMMON_INTERCEPTOR_BLOCK_REAL(preadv64)(fd, iov, iovcnt, offset);
1114 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
1115 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
1116 return res;
1117 }
1118 #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
1119 #else
1120 #define INIT_PREADV64
1121 #endif
1122
1123 #if SANITIZER_INTERCEPT_WRITE
1124 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
1125 void *ctx;
1126 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
1127 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1128 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1129 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(write)(fd, ptr, count);
1130 // FIXME: this check should be _before_ the call to
1131 // COMMON_INTERCEPTOR_BLOCK_REAL(write), not after
1132 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1133 return res;
1134 }
1135 #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
1136 #else
1137 #define INIT_WRITE
1138 #endif
1139
1140 #if SANITIZER_INTERCEPT_FWRITE
1141 INTERCEPTOR(SIZE_T, fwrite, const void *p, usize size, usize nmemb, void *file) {
1142 // libc file streams can call user-supplied functions, see fopencookie.
1143 void *ctx;
1144 COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
1145 SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
1146 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
1147 return res;
1148 }
1149 #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
1150 #else
1151 #define INIT_FWRITE
1152 #endif
1153
1154 #if SANITIZER_INTERCEPT_PWRITE
1155 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
1156 void *ctx;
1157 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
1158 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1159 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1160 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pwrite)(fd, ptr, count, offset);
1161 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1162 return res;
1163 }
1164 #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
1165 #else
1166 #define INIT_PWRITE
1167 #endif
1168
1169 #if SANITIZER_INTERCEPT_PWRITE64
1170 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
1171 OFF64_T offset) {
1172 void *ctx;
1173 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
1174 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1175 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1176 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pwrite64)(fd, ptr, count, offset);
1177 if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
1178 return res;
1179 }
1180 #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
1181 #else
1182 #define INIT_PWRITE64
1183 #endif
1184
1185 #if SANITIZER_INTERCEPT_WRITEV
1186 INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
1187 int iovcnt) {
1188 void *ctx;
1189 COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
1190 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1191 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1192 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(writev)(fd, iov, iovcnt);
1193 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1194 return res;
1195 }
1196 #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
1197 #else
1198 #define INIT_WRITEV
1199 #endif
1200
1201 #if SANITIZER_INTERCEPT_PWRITEV
1202 INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
1203 OFF_T offset) {
1204 void *ctx;
1205 COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
1206 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1207 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1208 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(pwritev)(fd, iov, iovcnt, offset);
1209 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1210 return res;
1211 }
1212 #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
1213 #else
1214 #define INIT_PWRITEV
1215 #endif
1216
1217 #if SANITIZER_INTERCEPT_PWRITEV64
1218 INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
1219 OFF64_T offset) {
1220 void *ctx;
1221 COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
1222 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
1223 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
1224 SSIZE_T res =
1225 COMMON_INTERCEPTOR_BLOCK_REAL(pwritev64)(fd, iov, iovcnt, offset);
1226 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
1227 return res;
1228 }
1229 #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
1230 #else
1231 #define INIT_PWRITEV64
1232 #endif
1233
1234 #if SANITIZER_INTERCEPT_FGETS
1235 INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
1236 // libc file streams can call user-supplied functions, see fopencookie.
1237 void *ctx;
1238 COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
1239 // FIXME: under ASan the call below may write to freed memory and corrupt
1240 // its metadata. See
1241 // https://github.com/google/sanitizers/issues/321.
1242 char *res = REAL(fgets)(s, size, file);
1243 if (res)
1244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
1245 return res;
1246 }
1247 #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
1248 #else
1249 #define INIT_FGETS
1250 #endif
1251
1252 #if SANITIZER_INTERCEPT_FPUTS
1253 INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
1254 // libc file streams can call user-supplied functions, see fopencookie.
1255 void *ctx;
1256 COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
1257 if (!SANITIZER_APPLE || s) { // `fputs(NULL, file)` is supported on Darwin.
1258 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1259 }
1260 return REAL(fputs)(s, file);
1261 }
1262 #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
1263 #else
1264 #define INIT_FPUTS
1265 #endif
1266
1267 #if SANITIZER_INTERCEPT_PUTS
1268 INTERCEPTOR(int, puts, char *s) {
1269 // libc file streams can call user-supplied functions, see fopencookie.
1270 void *ctx;
1271 COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
1272 if (!SANITIZER_APPLE || s) { // `puts(NULL)` is supported on Darwin.
1273 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
1274 }
1275 return REAL(puts)(s);
1276 }
1277 #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
1278 #else
1279 #define INIT_PUTS
1280 #endif
1281
1282 #if SANITIZER_INTERCEPT_PRCTL
1283 INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
1284 unsigned long arg4, unsigned long arg5) {
1285 void *ctx;
1286 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
1287 static const int PR_SET_NAME = 15;
1288 static const int PR_GET_NAME = 16;
1289 static const int PR_SET_VMA = 0x53564d41;
1290 static const int PR_SCHED_CORE = 62;
1291 static const int PR_SCHED_CORE_GET = 0;
1292 static const int PR_GET_PDEATHSIG = 2;
1293
1294 # if !SANITIZER_ANDROID
1295 static const int PR_SET_SECCOMP = 22;
1296 static const int SECCOMP_MODE_FILTER = 2;
1297 # endif
1298 if (option == PR_SET_VMA && arg2 == 0UL) {
1299 char *name = (char *)arg5;
1300 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1301 }
1302 int res = REAL(prctl)(option, arg2, arg3, arg4, arg5);
1303 if (option == PR_SET_NAME) {
1304 char buff[16];
1305 internal_strncpy(buff, (char *)arg2, 15);
1306 buff[15] = 0;
1307 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1308 } else if (res == 0 && option == PR_GET_NAME) {
1309 char *name = (char *)arg2;
1310 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
1311 } else if (res != -1 && option == PR_SCHED_CORE &&
1312 arg2 == PR_SCHED_CORE_GET) {
1313 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64 *)(arg5), sizeof(u64));
1314 } else if (res != -1 && option == PR_GET_PDEATHSIG) {
1315 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64 *)(arg2), sizeof(int));
1316 # if !SANITIZER_ANDROID
1317 } else if (res != -1 && option == PR_SET_SECCOMP &&
1318 arg2 == SECCOMP_MODE_FILTER) {
1319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64 *)(arg3), struct_sock_fprog_sz);
1320 # endif
1321 }
1322 return res;
1323 }
1324 #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
1325 #else
1326 #define INIT_PRCTL
1327 #endif // SANITIZER_INTERCEPT_PRCTL
1328
1329 #if SANITIZER_INTERCEPT_TIME
1330 INTERCEPTOR(unsigned long, time, unsigned long *t) {
1331 void *ctx;
1332 COMMON_INTERCEPTOR_ENTER(ctx, time, t);
1333 unsigned long local_t;
1334 unsigned long res = REAL(time)(&local_t);
1335 if (t && res != (unsigned long)-1) {
1336 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
1337 *t = local_t;
1338 }
1339 return res;
1340 }
1341 #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
1342 #else
1343 #define INIT_TIME
1344 #endif // SANITIZER_INTERCEPT_TIME
1345
1346 #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1347 static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
1348 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1349 #if !SANITIZER_SOLARIS
1350 if (tm->tm_zone) {
1351 // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
1352 // can point to shared memory and tsan would report a data race.
1353 COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
1354 internal_strlen(tm->tm_zone) + 1);
1355 }
1356 #endif
1357 }
1358 INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
1359 void *ctx;
1360 COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
1361 __sanitizer_tm *res = REAL(localtime)(timep);
1362 if (res) {
1363 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1364 unpoison_tm(ctx, res);
1365 }
1366 return res;
1367 }
1368 INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
1369 void *ctx;
1370 COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
1371 __sanitizer_tm *res = REAL(localtime_r)(timep, result);
1372 if (res) {
1373 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1374 unpoison_tm(ctx, res);
1375 }
1376 return res;
1377 }
1378 INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
1379 void *ctx;
1380 COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
1381 __sanitizer_tm *res = REAL(gmtime)(timep);
1382 if (res) {
1383 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1384 unpoison_tm(ctx, res);
1385 }
1386 return res;
1387 }
1388 INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
1389 void *ctx;
1390 COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
1391 __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
1392 if (res) {
1393 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1394 unpoison_tm(ctx, res);
1395 }
1396 return res;
1397 }
1398 INTERCEPTOR(char *, ctime, unsigned long *timep) {
1399 void *ctx;
1400 COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
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(ctime)(timep);
1405 if (res) {
1406 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1408 }
1409 return res;
1410 }
1411 INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
1412 void *ctx;
1413 COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
1414 // FIXME: under ASan the call below may write to freed memory and corrupt
1415 // its metadata. See
1416 // https://github.com/google/sanitizers/issues/321.
1417 char *res = REAL(ctime_r)(timep, result);
1418 if (res) {
1419 COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
1420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1421 }
1422 return res;
1423 }
1424 INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
1425 void *ctx;
1426 COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
1427 // FIXME: under ASan the call below may write to freed memory and corrupt
1428 // its metadata. See
1429 // https://github.com/google/sanitizers/issues/321.
1430 char *res = REAL(asctime)(tm);
1431 if (res) {
1432 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1433 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1434 }
1435 return res;
1436 }
1437 INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
1438 void *ctx;
1439 COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
1440 // FIXME: under ASan the call below may write to freed memory and corrupt
1441 // its metadata. See
1442 // https://github.com/google/sanitizers/issues/321.
1443 char *res = REAL(asctime_r)(tm, result);
1444 if (res) {
1445 COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
1446 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
1447 }
1448 return res;
1449 }
1450 INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
1451 void *ctx;
1452 COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
1453 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
1454 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
1455 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
1456 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
1457 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
1458 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
1459 COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
1460 long res = REAL(mktime)(tm);
1461 if (res != -1) unpoison_tm(ctx, tm);
1462 return res;
1463 }
1464 #define INIT_LOCALTIME_AND_FRIENDS \
1465 COMMON_INTERCEPT_FUNCTION(localtime); \
1466 COMMON_INTERCEPT_FUNCTION(localtime_r); \
1467 COMMON_INTERCEPT_FUNCTION(gmtime); \
1468 COMMON_INTERCEPT_FUNCTION(gmtime_r); \
1469 COMMON_INTERCEPT_FUNCTION(ctime); \
1470 COMMON_INTERCEPT_FUNCTION(ctime_r); \
1471 COMMON_INTERCEPT_FUNCTION(asctime); \
1472 COMMON_INTERCEPT_FUNCTION(asctime_r); \
1473 COMMON_INTERCEPT_FUNCTION(mktime);
1474 #else
1475 #define INIT_LOCALTIME_AND_FRIENDS
1476 #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
1477
1478 #if SANITIZER_INTERCEPT_STRPTIME
1479 INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
1480 void *ctx;
1481 COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
1482 if (format)
1483 COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
1484 // FIXME: under ASan the call below may write to freed memory and corrupt
1485 // its metadata. See
1486 // https://github.com/google/sanitizers/issues/321.
1487 char *res = REAL(strptime)(s, format, tm);
1488 COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
1489 if (res && tm) {
1490 // Do not call unpoison_tm here, because strptime does not, in fact,
1491 // initialize the entire struct tm. For example, tm_zone pointer is left
1492 // uninitialized.
1493 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
1494 }
1495 return res;
1496 }
1497 #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
1498 #else
1499 #define INIT_STRPTIME
1500 #endif
1501
1502 #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
1503 #include "sanitizer_common_interceptors_format.inc"
1504
1505 #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
1506 { \
1507 void *ctx; \
1508 va_list ap; \
1509 va_start(ap, format); \
1510 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
1511 int res = WRAP(vname)(__VA_ARGS__, ap); \
1512 va_end(ap); \
1513 return res; \
1514 }
1515
1516 #endif
1517
1518 #if SANITIZER_INTERCEPT_SCANF
1519
1520 #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
1521 { \
1522 void *ctx; \
1523 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1524 va_list aq; \
1525 va_copy(aq, ap); \
1526 int res = REAL(vname)(__VA_ARGS__); \
1527 if (res > 0) \
1528 scanf_common(ctx, res, allowGnuMalloc, format, aq); \
1529 va_end(aq); \
1530 return res; \
1531 }
1532
1533 INTERCEPTOR(int, vscanf, const char *format, va_list ap)
1534 VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
1535
1536 INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
1537 VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
1538
1539 INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
1540 VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
1541
1542 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1543 INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
1544 VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
1545
1546 INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
1547 va_list ap)
1548 VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
1549
1550 INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
1551 VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
1552
1553 INTERCEPTOR(int, __isoc23_vscanf, const char *format, va_list ap)
1554 VSCANF_INTERCEPTOR_IMPL(__isoc23_vscanf, false, format, ap)
1555
1556 INTERCEPTOR(int, __isoc23_vsscanf, const char *str, const char *format,
1557 va_list ap)
1558 VSCANF_INTERCEPTOR_IMPL(__isoc23_vsscanf, false, str, format, ap)
1559
1560 INTERCEPTOR(int, __isoc23_vfscanf, void *stream, const char *format, va_list ap)
1561 VSCANF_INTERCEPTOR_IMPL(__isoc23_vfscanf, false, stream, format, ap)
1562 #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
1563
1564 INTERCEPTOR(int, scanf, const char *format, ...)
1565 FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
1566
1567 INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
1568 FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
1569
1570 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
1571 FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
1572
1573 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1574 INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
1575 FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
1576
1577 INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
1578 FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
1579
1580 INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
1581 FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
1582
1583 INTERCEPTOR(int, __isoc23_scanf, const char *format, ...)
1584 FORMAT_INTERCEPTOR_IMPL(__isoc23_scanf, __isoc23_vscanf, format)
1585
1586 INTERCEPTOR(int, __isoc23_fscanf, void *stream, const char *format, ...)
1587 FORMAT_INTERCEPTOR_IMPL(__isoc23_fscanf, __isoc23_vfscanf, stream, format)
1588
1589 INTERCEPTOR(int, __isoc23_sscanf, const char *str, const char *format, ...)
1590 FORMAT_INTERCEPTOR_IMPL(__isoc23_sscanf, __isoc23_vsscanf, str, format)
1591 #endif
1592
1593 #endif
1594
1595 #if SANITIZER_INTERCEPT_SCANF
1596 #define INIT_SCANF \
1597 COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
1598 COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
1599 COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
1600 COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
1601 COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
1602 COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
1603 #else
1604 #define INIT_SCANF
1605 #endif
1606
1607 #if SANITIZER_INTERCEPT_ISOC99_SCANF
1608 #define INIT_ISOC99_SCANF \
1609 COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
1610 COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
1611 COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
1612 COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
1613 COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
1614 COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf); \
1615 COMMON_INTERCEPT_FUNCTION(__isoc23_scanf); \
1616 COMMON_INTERCEPT_FUNCTION(__isoc23_sscanf); \
1617 COMMON_INTERCEPT_FUNCTION(__isoc23_fscanf); \
1618 COMMON_INTERCEPT_FUNCTION(__isoc23_vscanf); \
1619 COMMON_INTERCEPT_FUNCTION(__isoc23_vsscanf); \
1620 COMMON_INTERCEPT_FUNCTION(__isoc23_vfscanf);
1621 #else
1622 #define INIT_ISOC99_SCANF
1623 #endif
1624
1625 #if SANITIZER_INTERCEPT_PRINTF
1626
1627 #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
1628 void *ctx; \
1629 COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
1630 va_list aq; \
1631 va_copy(aq, ap);
1632
1633 #define VPRINTF_INTERCEPTOR_RETURN() \
1634 va_end(aq);
1635
1636 #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
1637 { \
1638 VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
1639 if (common_flags()->check_printf) \
1640 printf_common(ctx, format, aq); \
1641 int res = REAL(vname)(__VA_ARGS__); \
1642 VPRINTF_INTERCEPTOR_RETURN(); \
1643 return res; \
1644 }
1645
1646 // FIXME: under ASan the REAL() call below may write to freed memory and
1647 // corrupt its metadata. See
1648 // https://github.com/google/sanitizers/issues/321.
1649 #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
1650 { \
1651 VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
1652 if (common_flags()->check_printf) { \
1653 printf_common(ctx, format, aq); \
1654 } \
1655 int res = REAL(vname)(str, __VA_ARGS__); \
1656 if (res >= 0) { \
1657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
1658 } \
1659 VPRINTF_INTERCEPTOR_RETURN(); \
1660 return res; \
1661 }
1662
1663 // FIXME: under ASan the REAL() call below may write to freed memory and
1664 // corrupt its metadata. See
1665 // https://github.com/google/sanitizers/issues/321.
1666 #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
1667 { \
1668 VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
1669 if (common_flags()->check_printf) { \
1670 printf_common(ctx, format, aq); \
1671 } \
1672 int res = REAL(vname)(str, size, __VA_ARGS__); \
1673 if (res >= 0) { \
1674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
1675 } \
1676 VPRINTF_INTERCEPTOR_RETURN(); \
1677 return res; \
1678 }
1679
1680 // FIXME: under ASan the REAL() call below may write to freed memory and
1681 // corrupt its metadata. See
1682 // https://github.com/google/sanitizers/issues/321.
1683 #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
1684 { \
1685 VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
1686 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
1687 if (common_flags()->check_printf) { \
1688 printf_common(ctx, format, aq); \
1689 } \
1690 int res = REAL(vname)(strp, __VA_ARGS__); \
1691 if (res >= 0) { \
1692 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
1693 } \
1694 VPRINTF_INTERCEPTOR_RETURN(); \
1695 return res; \
1696 }
1697
1698 INTERCEPTOR(int, vprintf, const char *format, va_list ap)
1699 VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
1700
1701 INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
1702 va_list ap)
1703 VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
1704
1705 INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
1706 va_list ap)
1707 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1708
1709 #if SANITIZER_INTERCEPT___PRINTF_CHK
1710 INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
1711 SIZE_T size_to, const char *format, va_list ap)
1712 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
1713 #endif
1714
1715 #if SANITIZER_INTERCEPT_PRINTF_L
1716 INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
1717 const char *format, va_list ap)
1718 VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
1719
1720 INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
1721 const char *format, ...)
1722 FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
1723 #endif // SANITIZER_INTERCEPT_PRINTF_L
1724
1725 INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
1726 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1727
1728 #if SANITIZER_INTERCEPT___PRINTF_CHK
1729 INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
1730 const char *format, va_list ap)
1731 VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
1732 #endif
1733
1734 INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
1735 VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
1736
1737 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1738 INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
1739 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
1740
1741 INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
1742 const char *format, va_list ap)
1743 VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
1744
1745 INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
1746 va_list ap)
1747 VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
1748
1749 INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
1750 va_list ap)
1751 VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
1752 ap)
1753
1754 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1755
1756 INTERCEPTOR(int, printf, const char *format, ...)
1757 FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
1758
1759 INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
1760 FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
1761
1762 #if SANITIZER_INTERCEPT___PRINTF_CHK
1763 INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
1764 const char *format, ...)
1765 FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
1766 #endif
1767
1768 INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
1769 FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)
1770
1771 #if SANITIZER_INTERCEPT___PRINTF_CHK
1772 INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
1773 const char *format, ...)
1774 FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
1775 #endif
1776
1777 INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
1778 FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
1779
1780 #if SANITIZER_INTERCEPT___PRINTF_CHK
1781 INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
1782 SIZE_T size_to, const char *format, ...)
1783 FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
1784 #endif
1785
1786 INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
1787 FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
1788
1789 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1790 INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
1791 FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
1792
1793 INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
1794 ...)
1795 FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
1796
1797 INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
1798 FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
1799
1800 INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
1801 const char *format, ...)
1802 FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
1803 format)
1804
1805 #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
1806
1807 #endif // SANITIZER_INTERCEPT_PRINTF
1808
1809 #if SANITIZER_INTERCEPT_PRINTF
1810 #define INIT_PRINTF \
1811 COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
1812 COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
1813 COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
1814 COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
1815 COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
1816 COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
1817 COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
1818 COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
1819 COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
1820 COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
1821 #else
1822 #define INIT_PRINTF
1823 #endif
1824
1825 #if SANITIZER_INTERCEPT___PRINTF_CHK
1826 #define INIT___PRINTF_CHK \
1827 COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
1828 COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
1829 COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
1830 COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
1831 COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
1832 #else
1833 #define INIT___PRINTF_CHK
1834 #endif
1835
1836 #if SANITIZER_INTERCEPT_PRINTF_L
1837 #define INIT_PRINTF_L \
1838 COMMON_INTERCEPT_FUNCTION(snprintf_l); \
1839 COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
1840 #else
1841 #define INIT_PRINTF_L
1842 #endif
1843
1844 #if SANITIZER_INTERCEPT_ISOC99_PRINTF
1845 #define INIT_ISOC99_PRINTF \
1846 COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
1847 COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
1848 COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
1849 COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
1850 COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
1851 COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
1852 COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
1853 COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
1854 #else
1855 #define INIT_ISOC99_PRINTF
1856 #endif
1857
1858 #if SANITIZER_INTERCEPT_IOCTL
1859 #include "sanitizer_common_interceptors_ioctl.inc"
1860 #include "sanitizer_interceptors_ioctl_netbsd.inc"
1861 INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
1862 // We need a frame pointer, because we call into ioctl_common_[pre|post] which
1863 // can trigger a report and we need to be able to unwind through this
1864 // function. On Mac in debug mode we might not have a frame pointer, because
1865 // ioctl_common_[pre|post] doesn't get inlined here.
1866 ENABLE_FRAME_POINTER;
1867
1868 void *ctx;
1869 va_list ap;
1870 va_start(ap, request);
1871 void *arg = va_arg(ap, void *);
1872 va_end(ap);
1873 COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
1874
1875 CHECK(ioctl_initialized);
1876
1877 // Note: TSan does not use common flags, and they are zero-initialized.
1878 // This effectively disables ioctl handling in TSan.
1879 if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
1880
1881 // Although request is unsigned long, the rest of the interceptor uses it
1882 // as just "unsigned" to save space, because we know that all values fit in
1883 // "unsigned" - they are compile-time constants.
1884
1885 const ioctl_desc *desc = ioctl_lookup(request);
1886 ioctl_desc decoded_desc;
1887 if (!desc) {
1888 VPrintf(2, "Decoding unknown ioctl 0x%lx\n", request);
1889 if (!ioctl_decode(request, &decoded_desc))
1890 Printf("WARNING: failed decoding unknown ioctl 0x%lx\n", request);
1891 else
1892 desc = &decoded_desc;
1893 }
1894
1895 if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
1896 int res = REAL(ioctl)(d, request, arg);
1897 // FIXME: some ioctls have different return values for success and failure.
1898 if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
1899 return res;
1900 }
1901 #define INIT_IOCTL \
1902 ioctl_init(); \
1903 COMMON_INTERCEPT_FUNCTION(ioctl);
1904 #else
1905 #define INIT_IOCTL
1906 #endif
1907
1908 #if SANITIZER_POSIX
1909 UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
1910 if (pwd) {
1911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
1912 if (pwd->pw_name)
1913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
1914 internal_strlen(pwd->pw_name) + 1);
1915 if (pwd->pw_passwd)
1916 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
1917 internal_strlen(pwd->pw_passwd) + 1);
1918 #if !SANITIZER_ANDROID
1919 if (pwd->pw_gecos)
1920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
1921 internal_strlen(pwd->pw_gecos) + 1);
1922 #endif
1923 #if SANITIZER_APPLE || SANITIZER_FREEBSD || SANITIZER_NETBSD
1924 if (pwd->pw_class)
1925 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
1926 internal_strlen(pwd->pw_class) + 1);
1927 #endif
1928 if (pwd->pw_dir)
1929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
1930 internal_strlen(pwd->pw_dir) + 1);
1931 if (pwd->pw_shell)
1932 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
1933 internal_strlen(pwd->pw_shell) + 1);
1934 }
1935 }
1936
1937 UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
1938 if (grp) {
1939 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
1940 if (grp->gr_name)
1941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
1942 internal_strlen(grp->gr_name) + 1);
1943 if (grp->gr_passwd)
1944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
1945 internal_strlen(grp->gr_passwd) + 1);
1946 char **p = grp->gr_mem;
1947 for (; *p; ++p) {
1948 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
1949 }
1950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
1951 (p - grp->gr_mem + 1) * sizeof(*p));
1952 }
1953 }
1954 #endif // SANITIZER_POSIX
1955
1956 #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
1957 INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
1958 void *ctx;
1959 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
1960 if (name)
1961 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1962 __sanitizer_passwd *res = REAL(getpwnam)(name);
1963 unpoison_passwd(ctx, res);
1964 return res;
1965 }
1966 INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
1967 void *ctx;
1968 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
1969 __sanitizer_passwd *res = REAL(getpwuid)(uid);
1970 unpoison_passwd(ctx, res);
1971 return res;
1972 }
1973 INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
1974 void *ctx;
1975 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
1976 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
1977 __sanitizer_group *res = REAL(getgrnam)(name);
1978 unpoison_group(ctx, res);
1979 return res;
1980 }
1981 INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
1982 void *ctx;
1983 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
1984 __sanitizer_group *res = REAL(getgrgid)(gid);
1985 unpoison_group(ctx, res);
1986 return res;
1987 }
1988 #define INIT_GETPWNAM_AND_FRIENDS \
1989 COMMON_INTERCEPT_FUNCTION(getpwnam); \
1990 COMMON_INTERCEPT_FUNCTION(getpwuid); \
1991 COMMON_INTERCEPT_FUNCTION(getgrnam); \
1992 COMMON_INTERCEPT_FUNCTION(getgrgid);
1993 #else
1994 #define INIT_GETPWNAM_AND_FRIENDS
1995 #endif
1996
1997 #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
1998 INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
1999 char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
2000 void *ctx;
2001 COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
2002 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
2003 // FIXME: under ASan the call below may write to freed memory and corrupt
2004 // its metadata. See
2005 // https://github.com/google/sanitizers/issues/321.
2006 int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
2007 if (!res && result)
2008 unpoison_passwd(ctx, *result);
2009 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2010 return res;
2011 }
2012 INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
2013 SIZE_T buflen, __sanitizer_passwd **result) {
2014 void *ctx;
2015 COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
2016 // FIXME: under ASan the call below may write to freed memory and corrupt
2017 // its metadata. See
2018 // https://github.com/google/sanitizers/issues/321.
2019 int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
2020 if (!res && result)
2021 unpoison_passwd(ctx, *result);
2022 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2023 return res;
2024 }
2025 INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
2026 char *buf, SIZE_T buflen, __sanitizer_group **result) {
2027 void *ctx;
2028 COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
2029 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
2030 // FIXME: under ASan the call below may write to freed memory and corrupt
2031 // its metadata. See
2032 // https://github.com/google/sanitizers/issues/321.
2033 int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
2034 if (!res && result)
2035 unpoison_group(ctx, *result);
2036 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2037 return res;
2038 }
2039 INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
2040 SIZE_T buflen, __sanitizer_group **result) {
2041 void *ctx;
2042 COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
2043 // FIXME: under ASan the call below may write to freed memory and corrupt
2044 // its metadata. See
2045 // https://github.com/google/sanitizers/issues/321.
2046 int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
2047 if (!res && result)
2048 unpoison_group(ctx, *result);
2049 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2050 return res;
2051 }
2052 #define INIT_GETPWNAM_R_AND_FRIENDS \
2053 COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
2054 COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
2055 COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
2056 COMMON_INTERCEPT_FUNCTION(getgrgid_r);
2057 #else
2058 #define INIT_GETPWNAM_R_AND_FRIENDS
2059 #endif
2060
2061 #if SANITIZER_INTERCEPT_GETPWENT
2062 INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
2063 void *ctx;
2064 COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
2065 __sanitizer_passwd *res = REAL(getpwent)(dummy);
2066 unpoison_passwd(ctx, res);
2067 return res;
2068 }
2069 INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
2070 void *ctx;
2071 COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
2072 __sanitizer_group *res = REAL(getgrent)(dummy);
2073 unpoison_group(ctx, res);
2074 return res;
2075 }
2076 #define INIT_GETPWENT \
2077 COMMON_INTERCEPT_FUNCTION(getpwent); \
2078 COMMON_INTERCEPT_FUNCTION(getgrent);
2079 #else
2080 #define INIT_GETPWENT
2081 #endif
2082
2083 #if SANITIZER_INTERCEPT_FGETPWENT
2084 INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
2085 void *ctx;
2086 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
2087 __sanitizer_passwd *res = REAL(fgetpwent)(fp);
2088 unpoison_passwd(ctx, res);
2089 return res;
2090 }
2091 INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
2092 void *ctx;
2093 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
2094 __sanitizer_group *res = REAL(fgetgrent)(fp);
2095 unpoison_group(ctx, res);
2096 return res;
2097 }
2098 #define INIT_FGETPWENT \
2099 COMMON_INTERCEPT_FUNCTION(fgetpwent); \
2100 COMMON_INTERCEPT_FUNCTION(fgetgrent);
2101 #else
2102 #define INIT_FGETPWENT
2103 #endif
2104
2105 #if SANITIZER_INTERCEPT_GETPWENT_R
2106 INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
2107 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2108 void *ctx;
2109 COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
2110 // FIXME: under ASan the call below may write to freed memory and corrupt
2111 // its metadata. See
2112 // https://github.com/google/sanitizers/issues/321.
2113 int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
2114 if (!res && pwbufp)
2115 unpoison_passwd(ctx, *pwbufp);
2116 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2117 return res;
2118 }
2119 INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
2120 __sanitizer_group **pwbufp) {
2121 void *ctx;
2122 COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
2123 // FIXME: under ASan the call below may write to freed memory and corrupt
2124 // its metadata. See
2125 // https://github.com/google/sanitizers/issues/321.
2126 int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
2127 if (!res && pwbufp)
2128 unpoison_group(ctx, *pwbufp);
2129 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2130 return res;
2131 }
2132 #define INIT_GETPWENT_R \
2133 COMMON_INTERCEPT_FUNCTION(getpwent_r); \
2134 COMMON_INTERCEPT_FUNCTION(getgrent_r);
2135 #else
2136 #define INIT_GETPWENT_R
2137 #endif
2138
2139 #if SANITIZER_INTERCEPT_FGETPWENT_R
2140 INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
2141 SIZE_T buflen, __sanitizer_passwd **pwbufp) {
2142 void *ctx;
2143 COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
2144 // FIXME: under ASan the call below may write to freed memory and corrupt
2145 // its metadata. See
2146 // https://github.com/google/sanitizers/issues/321.
2147 int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
2148 if (!res && pwbufp)
2149 unpoison_passwd(ctx, *pwbufp);
2150 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2151 return res;
2152 }
2153 #define INIT_FGETPWENT_R \
2154 COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
2155 #else
2156 #define INIT_FGETPWENT_R
2157 #endif
2158
2159 #if SANITIZER_INTERCEPT_FGETGRENT_R
2160 INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
2161 SIZE_T buflen, __sanitizer_group **pwbufp) {
2162 void *ctx;
2163 COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
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(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
2168 if (!res && pwbufp)
2169 unpoison_group(ctx, *pwbufp);
2170 if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
2171 return res;
2172 }
2173 #define INIT_FGETGRENT_R \
2174 COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
2175 #else
2176 #define INIT_FGETGRENT_R
2177 #endif
2178
2179 #if SANITIZER_INTERCEPT_SETPWENT
2180 // The only thing these interceptors do is disable any nested interceptors.
2181 // These functions may open nss modules and call uninstrumented functions from
2182 // them, and we don't want things like strlen() to trigger.
2183 INTERCEPTOR(void, setpwent, int dummy) {
2184 void *ctx;
2185 COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
2186 REAL(setpwent)(dummy);
2187 }
2188 INTERCEPTOR(void, endpwent, int dummy) {
2189 void *ctx;
2190 COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
2191 REAL(endpwent)(dummy);
2192 }
2193 INTERCEPTOR(void, setgrent, int dummy) {
2194 void *ctx;
2195 COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
2196 REAL(setgrent)(dummy);
2197 }
2198 INTERCEPTOR(void, endgrent, int dummy) {
2199 void *ctx;
2200 COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
2201 REAL(endgrent)(dummy);
2202 }
2203 #define INIT_SETPWENT \
2204 COMMON_INTERCEPT_FUNCTION(setpwent); \
2205 COMMON_INTERCEPT_FUNCTION(endpwent); \
2206 COMMON_INTERCEPT_FUNCTION(setgrent); \
2207 COMMON_INTERCEPT_FUNCTION(endgrent);
2208 #else
2209 #define INIT_SETPWENT
2210 #endif
2211
2212 #if SANITIZER_INTERCEPT_CLOCK_GETTIME
2213 INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
2214 void *ctx;
2215 COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
2216 // FIXME: under ASan the call below may write to freed memory and corrupt
2217 // its metadata. See
2218 // https://github.com/google/sanitizers/issues/321.
2219 int res = REAL(clock_getres)(clk_id, tp);
2220 if (!res && tp) {
2221 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2222 }
2223 return res;
2224 }
2225 INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
2226 void *ctx;
2227 COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
2228 // FIXME: under ASan the call below may write to freed memory and corrupt
2229 // its metadata. See
2230 // https://github.com/google/sanitizers/issues/321.
2231 int res = REAL(clock_gettime)(clk_id, tp);
2232 if (!res) {
2233 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
2234 }
2235 return res;
2236 }
2237 #if SANITIZER_GLIBC
2238 namespace __sanitizer {
2239 extern "C" {
2240 int real_clock_gettime(u32 clk_id, void *tp) {
2241 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
2242 return internal_clock_gettime(clk_id, tp);
2243 return REAL(clock_gettime)(clk_id, tp);
2244 }
2245 } // extern "C"
2246 } // namespace __sanitizer
2247 #endif
2248 INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
2249 void *ctx;
2250 COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
2251 COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
2252 return REAL(clock_settime)(clk_id, tp);
2253 }
2254 #define INIT_CLOCK_GETTIME \
2255 COMMON_INTERCEPT_FUNCTION(clock_getres); \
2256 COMMON_INTERCEPT_FUNCTION(clock_gettime); \
2257 COMMON_INTERCEPT_FUNCTION(clock_settime);
2258 #else
2259 #define INIT_CLOCK_GETTIME
2260 #endif
2261
2262 #if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
2263 INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
2264 __sanitizer_clockid_t *clockid) {
2265 void *ctx;
2266 COMMON_INTERCEPTOR_ENTER(ctx, clock_getcpuclockid, pid, clockid);
2267 int res = REAL(clock_getcpuclockid)(pid, clockid);
2268 if (!res && clockid) {
2269 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2270 }
2271 return res;
2272 }
2273
2274 INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
2275 __sanitizer_clockid_t *clockid) {
2276 void *ctx;
2277 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
2278 int res = REAL(pthread_getcpuclockid)(thread, clockid);
2279 if (!res && clockid) {
2280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2281 }
2282 return res;
2283 }
2284
2285 #define INIT_CLOCK_GETCPUCLOCKID \
2286 COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid); \
2287 COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
2288 #else
2289 #define INIT_CLOCK_GETCPUCLOCKID
2290 #endif
2291
2292 #if SANITIZER_INTERCEPT_GETITIMER
2293 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
2294 void *ctx;
2295 COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
2296 // FIXME: under ASan the call below may write to freed memory and corrupt
2297 // its metadata. See
2298 // https://github.com/google/sanitizers/issues/321.
2299 int res = REAL(getitimer)(which, curr_value);
2300 if (!res && curr_value) {
2301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
2302 }
2303 return res;
2304 }
2305 INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
2306 void *ctx;
2307 COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
2308 if (new_value) {
2309 // itimerval can contain padding that may be legitimately uninitialized
2310 const struct __sanitizer_itimerval *nv =
2311 (const struct __sanitizer_itimerval *)new_value;
2312 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
2313 sizeof(__sanitizer_time_t));
2314 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
2315 sizeof(__sanitizer_suseconds_t));
2316 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
2317 sizeof(__sanitizer_time_t));
2318 COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
2319 sizeof(__sanitizer_suseconds_t));
2320 }
2321 // FIXME: under ASan the call below may write to freed memory and corrupt
2322 // its metadata. See
2323 // https://github.com/google/sanitizers/issues/321.
2324 int res = REAL(setitimer)(which, new_value, old_value);
2325 if (!res && old_value) {
2326 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
2327 }
2328 return res;
2329 }
2330 #define INIT_GETITIMER \
2331 COMMON_INTERCEPT_FUNCTION(getitimer); \
2332 COMMON_INTERCEPT_FUNCTION(setitimer);
2333 #else
2334 #define INIT_GETITIMER
2335 #endif
2336
2337 #if SANITIZER_INTERCEPT_GLOB
2338 static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
2339 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
2340 // +1 for NULL pointer at the end.
2341 if (pglob->gl_pathv)
2342 COMMON_INTERCEPTOR_WRITE_RANGE(
2343 ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
2344 for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
2345 char *p = pglob->gl_pathv[i];
2346 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, internal_strlen(p) + 1);
2347 }
2348 }
2349
2350 #if SANITIZER_SOLARIS
2351 INTERCEPTOR(int, glob, const char *pattern, int flags,
2352 int (*errfunc)(const char *epath, int eerrno),
2353 __sanitizer_glob_t *pglob) {
2354 void *ctx;
2355 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2356 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2357 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2358 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2359 return res;
2360 }
2361 #else
2362 static THREADLOCAL __sanitizer_glob_t *pglob_copy;
2363
2364 static void wrapped_gl_closedir(void *dir) {
2365 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2366 pglob_copy->gl_closedir(dir);
2367 }
2368
2369 static void *wrapped_gl_readdir(void *dir) {
2370 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2371 return pglob_copy->gl_readdir(dir);
2372 }
2373
2374 static void *wrapped_gl_opendir(const char *s) {
2375 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
2376 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2377 return pglob_copy->gl_opendir(s);
2378 }
2379
2380 static int wrapped_gl_lstat(const char *s, void *st) {
2381 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2382 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2383 return pglob_copy->gl_lstat(s, st);
2384 }
2385
2386 static int wrapped_gl_stat(const char *s, void *st) {
2387 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
2388 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
2389 return pglob_copy->gl_stat(s, st);
2390 }
2391
2392 static const __sanitizer_glob_t kGlobCopy = {
2393 0, 0, 0,
2394 0, wrapped_gl_closedir, wrapped_gl_readdir,
2395 wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
2396
2397 INTERCEPTOR(int, glob, const char *pattern, int flags,
2398 int (*errfunc)(const char *epath, int eerrno),
2399 __sanitizer_glob_t *pglob) {
2400 void *ctx;
2401 COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
2402 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2403 __sanitizer_glob_t glob_copy;
2404 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2405 if (flags & glob_altdirfunc) {
2406 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2407 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2408 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2409 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2410 Swap(pglob->gl_stat, glob_copy.gl_stat);
2411 pglob_copy = &glob_copy;
2412 }
2413 int res = REAL(glob)(pattern, flags, errfunc, pglob);
2414 if (flags & glob_altdirfunc) {
2415 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2416 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2417 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2418 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2419 Swap(pglob->gl_stat, glob_copy.gl_stat);
2420 }
2421 pglob_copy = 0;
2422 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2423 return res;
2424 }
2425 #endif // SANITIZER_SOLARIS
2426 #define INIT_GLOB \
2427 COMMON_INTERCEPT_FUNCTION(glob);
2428 #else // SANITIZER_INTERCEPT_GLOB
2429 #define INIT_GLOB
2430 #endif // SANITIZER_INTERCEPT_GLOB
2431
2432 #if SANITIZER_INTERCEPT_GLOB64
2433 INTERCEPTOR(int, glob64, const char *pattern, int flags,
2434 int (*errfunc)(const char *epath, int eerrno),
2435 __sanitizer_glob_t *pglob) {
2436 void *ctx;
2437 COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
2438 COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
2439 __sanitizer_glob_t glob_copy;
2440 internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
2441 if (flags & glob_altdirfunc) {
2442 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2443 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2444 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2445 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2446 Swap(pglob->gl_stat, glob_copy.gl_stat);
2447 pglob_copy = &glob_copy;
2448 }
2449 int res = REAL(glob64)(pattern, flags, errfunc, pglob);
2450 if (flags & glob_altdirfunc) {
2451 Swap(pglob->gl_closedir, glob_copy.gl_closedir);
2452 Swap(pglob->gl_readdir, glob_copy.gl_readdir);
2453 Swap(pglob->gl_opendir, glob_copy.gl_opendir);
2454 Swap(pglob->gl_lstat, glob_copy.gl_lstat);
2455 Swap(pglob->gl_stat, glob_copy.gl_stat);
2456 }
2457 pglob_copy = 0;
2458 if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
2459 return res;
2460 }
2461 #define INIT_GLOB64 \
2462 COMMON_INTERCEPT_FUNCTION(glob64);
2463 #else // SANITIZER_INTERCEPT_GLOB64
2464 #define INIT_GLOB64
2465 #endif // SANITIZER_INTERCEPT_GLOB64
2466
2467 #if SANITIZER_INTERCEPT___B64_TO
2468 INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
2469 char *target, SIZE_T targsize) {
2470 void *ctx;
2471 COMMON_INTERCEPTOR_ENTER(ctx, __b64_ntop, src, srclength, target, targsize);
2472 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclength);
2473 int res = REAL(__b64_ntop)(src, srclength, target, targsize);
2474 if (res >= 0)
2475 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res + 1);
2476 return res;
2477 }
2478 INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
2479 void *ctx;
2480 COMMON_INTERCEPTOR_ENTER(ctx, __b64_pton, src, target, targsize);
2481 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
2482 int res = REAL(__b64_pton)(src, target, targsize);
2483 if (res >= 0)
2484 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res);
2485 return res;
2486 }
2487 #define INIT___B64_TO \
2488 COMMON_INTERCEPT_FUNCTION(__b64_ntop); \
2489 COMMON_INTERCEPT_FUNCTION(__b64_pton);
2490 #else // SANITIZER_INTERCEPT___B64_TO
2491 #define INIT___B64_TO
2492 #endif // SANITIZER_INTERCEPT___B64_TO
2493
2494 #if SANITIZER_INTERCEPT_DN_COMP_EXPAND
2495 # if __GLIBC_PREREQ(2, 34)
2496 // Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
2497 # define DN_COMP_INTERCEPTOR_NAME dn_comp
2498 # define DN_EXPAND_INTERCEPTOR_NAME dn_expand
2499 # else
2500 # define DN_COMP_INTERCEPTOR_NAME __dn_comp
2501 # define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
2502 # endif
2503 INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
2504 unsigned char *comp_dn, int length, unsigned char **dnptrs,
2505 unsigned char **lastdnptr) {
2506 void *ctx;
2507 COMMON_INTERCEPTOR_ENTER(ctx, DN_COMP_INTERCEPTOR_NAME, exp_dn, comp_dn,
2508 length, dnptrs, lastdnptr);
2509 int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
2510 lastdnptr);
2511 if (res >= 0) {
2512 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, comp_dn, res);
2513 if (dnptrs && lastdnptr) {
2514 unsigned char **p = dnptrs;
2515 for (; p != lastdnptr && *p; ++p)
2516 ;
2517 if (p != lastdnptr)
2518 ++p;
2519 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dnptrs, (p - dnptrs) * sizeof(*p));
2520 }
2521 }
2522 return res;
2523 }
2524 INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
2525 unsigned char const *end, unsigned char const *src, char *dest,
2526 int space) {
2527 void *ctx;
2528 COMMON_INTERCEPTOR_ENTER(ctx, DN_EXPAND_INTERCEPTOR_NAME, base, end, src,
2529 dest, space);
2530 // TODO: add read check if __dn_comp intercept added
2531 int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
2532 if (res >= 0)
2533 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, internal_strlen(dest) + 1);
2534 return res;
2535 }
2536 # define INIT_DN_COMP_EXPAND \
2537 COMMON_INTERCEPT_FUNCTION(DN_COMP_INTERCEPTOR_NAME); \
2538 COMMON_INTERCEPT_FUNCTION(DN_EXPAND_INTERCEPTOR_NAME);
2539 #else // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2540 # define INIT_DN_COMP_EXPAND
2541 #endif // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2542
2543 #if SANITIZER_INTERCEPT_POSIX_SPAWN
2544
2545 template <class RealSpawnPtr>
2546 static int PosixSpawnImpl(void *ctx, RealSpawnPtr *real_posix_spawn, pid_t *pid,
2547 const char *file_or_path, const void *file_actions,
2548 const void *attrp, char *const argv[],
2549 char *const envp[]) {
2550 COMMON_INTERCEPTOR_READ_RANGE(ctx, file_or_path,
2551 internal_strlen(file_or_path) + 1);
2552 if (argv) {
2553 for (char *const *s = argv; ; ++s) {
2554 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2555 if (!*s) break;
2556 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2557 }
2558 }
2559 if (envp) {
2560 for (char *const *s = envp; ; ++s) {
2561 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
2562 if (!*s) break;
2563 COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
2564 }
2565 }
2566 int res =
2567 real_posix_spawn(pid, file_or_path, file_actions, attrp, argv, envp);
2568 if (res == 0)
2569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pid, sizeof(*pid));
2570 return res;
2571 }
2572 INTERCEPTOR(int, posix_spawn, pid_t *pid, const char *path,
2573 const void *file_actions, const void *attrp, char *const argv[],
2574 char *const envp[]) {
2575 void *ctx;
2576 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawn, pid, path, file_actions, attrp,
2577 argv, envp);
2578 return PosixSpawnImpl(ctx, REAL(posix_spawn), pid, path, file_actions, attrp,
2579 argv, envp);
2580 }
2581 INTERCEPTOR(int, posix_spawnp, pid_t *pid, const char *file,
2582 const void *file_actions, const void *attrp, char *const argv[],
2583 char *const envp[]) {
2584 void *ctx;
2585 COMMON_INTERCEPTOR_ENTER(ctx, posix_spawnp, pid, file, file_actions, attrp,
2586 argv, envp);
2587 return PosixSpawnImpl(ctx, REAL(posix_spawnp), pid, file, file_actions, attrp,
2588 argv, envp);
2589 }
2590 # define INIT_POSIX_SPAWN \
2591 COMMON_INTERCEPT_FUNCTION(posix_spawn); \
2592 COMMON_INTERCEPT_FUNCTION(posix_spawnp);
2593 #else // SANITIZER_INTERCEPT_POSIX_SPAWN
2594 # define INIT_POSIX_SPAWN
2595 #endif // SANITIZER_INTERCEPT_POSIX_SPAWN
2596
2597 #if SANITIZER_INTERCEPT_WAIT
2598 // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
2599 // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
2600 // details.
2601 INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
2602 void *ctx;
2603 COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
2604 // FIXME: under ASan the call below may write to freed memory and corrupt
2605 // its metadata. See
2606 // https://github.com/google/sanitizers/issues/321.
2607 int res = COMMON_INTERCEPTOR_BLOCK_REAL(wait)(status);
2608 if (res != -1 && status)
2609 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2610 return res;
2611 }
2612 // On FreeBSD id_t is always 64-bit wide.
2613 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2614 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
2615 int options) {
2616 #else
2617 INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
2618 int options) {
2619 #endif
2620 void *ctx;
2621 COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
2622 // FIXME: under ASan the call below may write to freed memory and corrupt
2623 // its metadata. See
2624 // https://github.com/google/sanitizers/issues/321.
2625 int res = COMMON_INTERCEPTOR_BLOCK_REAL(waitid)(idtype, id, infop, options);
2626 if (res != -1 && infop)
2627 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
2628 return res;
2629 }
2630 INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
2631 void *ctx;
2632 COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
2633 // FIXME: under ASan the call below may write to freed memory and corrupt
2634 // its metadata. See
2635 // https://github.com/google/sanitizers/issues/321.
2636 int res = COMMON_INTERCEPTOR_BLOCK_REAL(waitpid)(pid, status, options);
2637 if (res != -1 && status)
2638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2639 return res;
2640 }
2641 INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
2642 void *ctx;
2643 COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
2644 // FIXME: under ASan the call below may write to freed memory and corrupt
2645 // its metadata. See
2646 // https://github.com/google/sanitizers/issues/321.
2647 int res = COMMON_INTERCEPTOR_BLOCK_REAL(wait3)(status, options, rusage);
2648 if (res != -1) {
2649 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2650 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2651 }
2652 return res;
2653 }
2654 #if SANITIZER_ANDROID
2655 INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
2656 void *ctx;
2657 COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
2658 // FIXME: under ASan the call below may write to freed memory and corrupt
2659 // its metadata. See
2660 // https://github.com/google/sanitizers/issues/321.
2661 int res =
2662 COMMON_INTERCEPTOR_BLOCK_REAL(__wait4)(pid, status, options, rusage);
2663 if (res != -1) {
2664 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2665 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2666 }
2667 return res;
2668 }
2669 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
2670 #else
2671 INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
2672 void *ctx;
2673 COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
2674 // FIXME: under ASan the call below may write to freed memory and corrupt
2675 // its metadata. See
2676 // https://github.com/google/sanitizers/issues/321.
2677 int res = COMMON_INTERCEPTOR_BLOCK_REAL(wait4)(pid, status, options, rusage);
2678 if (res != -1) {
2679 if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
2680 if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
2681 }
2682 return res;
2683 }
2684 #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
2685 #endif // SANITIZER_ANDROID
2686 #define INIT_WAIT \
2687 COMMON_INTERCEPT_FUNCTION(wait); \
2688 COMMON_INTERCEPT_FUNCTION(waitid); \
2689 COMMON_INTERCEPT_FUNCTION(waitpid); \
2690 COMMON_INTERCEPT_FUNCTION(wait3);
2691 #else
2692 #define INIT_WAIT
2693 #define INIT_WAIT4
2694 #endif
2695
2696 #if SANITIZER_INTERCEPT_INET
2697 INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
2698 void *ctx;
2699 COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
2700 uptr sz = __sanitizer_in_addr_sz(af);
2701 if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
2702 // FIXME: figure out read size based on the address family.
2703 // FIXME: under ASan the call below may write to freed memory and corrupt
2704 // its metadata. See
2705 // https://github.com/google/sanitizers/issues/321.
2706 char *res = REAL(inet_ntop)(af, src, dst, size);
2707 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
2708 return res;
2709 }
2710 INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
2711 void *ctx;
2712 COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
2713 COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
2714 // FIXME: figure out read size based on the address family.
2715 // FIXME: under ASan the call below may write to freed memory and corrupt
2716 // its metadata. See
2717 // https://github.com/google/sanitizers/issues/321.
2718 int res = REAL(inet_pton)(af, src, dst);
2719 if (res == 1) {
2720 uptr sz = __sanitizer_in_addr_sz(af);
2721 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2722 }
2723 return res;
2724 }
2725 #define INIT_INET \
2726 COMMON_INTERCEPT_FUNCTION(inet_ntop); \
2727 COMMON_INTERCEPT_FUNCTION(inet_pton);
2728 #else
2729 #define INIT_INET
2730 #endif
2731
2732 #if SANITIZER_INTERCEPT_INET
2733 INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
2734 void *ctx;
2735 COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
2736 if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, internal_strlen(cp) + 1);
2737 // FIXME: under ASan the call below may write to freed memory and corrupt
2738 // its metadata. See
2739 // https://github.com/google/sanitizers/issues/321.
2740 int res = REAL(inet_aton)(cp, dst);
2741 if (res != 0) {
2742 uptr sz = __sanitizer_in_addr_sz(af_inet);
2743 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
2744 }
2745 return res;
2746 }
2747 #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
2748 #else
2749 #define INIT_INET_ATON
2750 #endif
2751
2752 #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
2753 INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
2754 void *ctx;
2755 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
2756 // FIXME: under ASan the call below may write to freed memory and corrupt
2757 // its metadata. See
2758 // https://github.com/google/sanitizers/issues/321.
2759 int res = REAL(pthread_getschedparam)(thread, policy, param);
2760 if (res == 0) {
2761 if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
2762 if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
2763 }
2764 return res;
2765 }
2766 #define INIT_PTHREAD_GETSCHEDPARAM \
2767 COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
2768 #else
2769 #define INIT_PTHREAD_GETSCHEDPARAM
2770 #endif
2771
2772 #if SANITIZER_INTERCEPT_GETADDRINFO
2773 INTERCEPTOR(int, getaddrinfo, char *node, char *service,
2774 struct __sanitizer_addrinfo *hints,
2775 struct __sanitizer_addrinfo **out) {
2776 void *ctx;
2777 COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
2778 if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, internal_strlen(node) + 1);
2779 if (service)
2780 COMMON_INTERCEPTOR_READ_RANGE(ctx, service, internal_strlen(service) + 1);
2781 if (hints)
2782 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2783 // FIXME: under ASan the call below may write to freed memory and corrupt
2784 // its metadata. See
2785 // https://github.com/google/sanitizers/issues/321.
2786 int res = REAL(getaddrinfo)(node, service, hints, out);
2787 if (res == 0 && out) {
2788 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2789 struct __sanitizer_addrinfo *p = *out;
2790 while (p) {
2791 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2792 if (p->ai_addr)
2793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2794 if (p->ai_canonname)
2795 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2796 internal_strlen(p->ai_canonname) + 1);
2797 p = p->ai_next;
2798 }
2799 }
2800 return res;
2801 }
2802 #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2803 #else
2804 #define INIT_GETADDRINFO
2805 #endif
2806
2807 #if SANITIZER_INTERCEPT_GETNAMEINFO
2808 INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
2809 unsigned hostlen, char *serv, unsigned servlen, int flags) {
2810 void *ctx;
2811 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
2812 serv, servlen, flags);
2813 // FIXME: consider adding READ_RANGE(sockaddr, salen)
2814 // There is padding in in_addr that may make this too noisy
2815 // FIXME: under ASan the call below may write to freed memory and corrupt
2816 // its metadata. See
2817 // https://github.com/google/sanitizers/issues/321.
2818 int res =
2819 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
2820 if (res == 0) {
2821 if (host && hostlen)
2822 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, internal_strlen(host) + 1);
2823 if (serv && servlen)
2824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, internal_strlen(serv) + 1);
2825 }
2826 return res;
2827 }
2828 #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
2829 #else
2830 #define INIT_GETNAMEINFO
2831 #endif
2832
2833 #if SANITIZER_INTERCEPT_GETSOCKNAME
2834 INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {
2835 void *ctx;
2836 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
2837 unsigned addr_sz;
2838 if (addrlen) {
2839 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
2840 addr_sz = *addrlen;
2841 }
2842 // FIXME: under ASan the call below may write to freed memory and corrupt
2843 // its metadata. See
2844 // https://github.com/google/sanitizers/issues/321.
2845 int res = REAL(getsockname)(sock_fd, addr, addrlen);
2846 if (!res && addr && addrlen) {
2847 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
2848 }
2849 return res;
2850 }
2851 #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
2852 #else
2853 #define INIT_GETSOCKNAME
2854 #endif
2855
2856 #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2857 static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
2858 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
2859 if (h->h_name)
2860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, internal_strlen(h->h_name) + 1);
2861 char **p = h->h_aliases;
2862 while (*p) {
2863 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
2864 ++p;
2865 }
2866 COMMON_INTERCEPTOR_WRITE_RANGE(
2867 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
2868 p = h->h_addr_list;
2869 while (*p) {
2870 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
2871 ++p;
2872 }
2873 COMMON_INTERCEPTOR_WRITE_RANGE(
2874 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
2875 }
2876 #endif
2877
2878 #if SANITIZER_INTERCEPT_GETHOSTBYNAME
2879 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
2880 void *ctx;
2881 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
2882 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
2883 if (res) write_hostent(ctx, res);
2884 return res;
2885 }
2886
2887 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
2888 int type) {
2889 void *ctx;
2890 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
2891 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2892 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
2893 if (res) write_hostent(ctx, res);
2894 return res;
2895 }
2896
2897 INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
2898 void *ctx;
2899 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
2900 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
2901 if (res) write_hostent(ctx, res);
2902 return res;
2903 }
2904 #define INIT_GETHOSTBYNAME \
2905 COMMON_INTERCEPT_FUNCTION(gethostent); \
2906 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
2907 COMMON_INTERCEPT_FUNCTION(gethostbyname);
2908 #else
2909 #define INIT_GETHOSTBYNAME
2910 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
2911
2912 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
2913 INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
2914 void *ctx;
2915 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
2916 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
2917 if (res) write_hostent(ctx, res);
2918 return res;
2919 }
2920 #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
2921 #else
2922 #define INIT_GETHOSTBYNAME2
2923 #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
2924
2925 #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
2926 INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
2927 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
2928 int *h_errnop) {
2929 void *ctx;
2930 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
2931 h_errnop);
2932 // FIXME: under ASan the call below may write to freed memory and corrupt
2933 // its metadata. See
2934 // https://github.com/google/sanitizers/issues/321.
2935 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
2936 if (result) {
2937 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2938 if (res == 0 && *result) write_hostent(ctx, *result);
2939 }
2940 if (h_errnop)
2941 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2942 return res;
2943 }
2944 #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
2945 #else
2946 #define INIT_GETHOSTBYNAME_R
2947 #endif
2948
2949 #if SANITIZER_INTERCEPT_GETHOSTENT_R
2950 INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
2951 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
2952 void *ctx;
2953 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
2954 h_errnop);
2955 // FIXME: under ASan the call below may write to freed memory and corrupt
2956 // its metadata. See
2957 // https://github.com/google/sanitizers/issues/321.
2958 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
2959 if (result) {
2960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2961 if (res == 0 && *result) write_hostent(ctx, *result);
2962 }
2963 if (h_errnop)
2964 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2965 return res;
2966 }
2967 #define INIT_GETHOSTENT_R \
2968 COMMON_INTERCEPT_FUNCTION(gethostent_r);
2969 #else
2970 #define INIT_GETHOSTENT_R
2971 #endif
2972
2973 #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
2974 INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
2975 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
2976 __sanitizer_hostent **result, int *h_errnop) {
2977 void *ctx;
2978 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
2979 buflen, result, h_errnop);
2980 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
2981 // FIXME: under ASan the call below may write to freed memory and corrupt
2982 // its metadata. See
2983 // https://github.com/google/sanitizers/issues/321.
2984 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
2985 h_errnop);
2986 if (result) {
2987 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
2988 if (res == 0 && *result) write_hostent(ctx, *result);
2989 }
2990 if (h_errnop)
2991 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
2992 return res;
2993 }
2994 #define INIT_GETHOSTBYADDR_R \
2995 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
2996 #else
2997 #define INIT_GETHOSTBYADDR_R
2998 #endif
2999
3000 #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
3001 INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
3002 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
3003 __sanitizer_hostent **result, int *h_errnop) {
3004 void *ctx;
3005 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
3006 result, h_errnop);
3007 // FIXME: under ASan the call below may write to freed memory and corrupt
3008 // its metadata. See
3009 // https://github.com/google/sanitizers/issues/321.
3010 int res =
3011 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
3012 if (result) {
3013 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3014 if (res == 0 && *result) write_hostent(ctx, *result);
3015 }
3016 if (h_errnop)
3017 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3018 return res;
3019 }
3020 #define INIT_GETHOSTBYNAME2_R \
3021 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
3022 #else
3023 #define INIT_GETHOSTBYNAME2_R
3024 #endif
3025
3026 #if SANITIZER_INTERCEPT_GETSOCKOPT
3027 INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
3028 int *optlen) {
3029 void *ctx;
3030 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
3031 optlen);
3032 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
3033 // FIXME: under ASan the call below may write to freed memory and corrupt
3034 // its metadata. See
3035 // https://github.com/google/sanitizers/issues/321.
3036 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
3037 if (res == 0)
3038 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
3039 return res;
3040 }
3041 #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
3042 #else
3043 #define INIT_GETSOCKOPT
3044 #endif
3045
3046 #if SANITIZER_INTERCEPT_ACCEPT
3047 INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
3048 void *ctx;
3049 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
3050 unsigned addrlen0 = 0;
3051 if (addrlen) {
3052 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3053 addrlen0 = *addrlen;
3054 }
3055 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(accept)(fd, addr, addrlen);
3056 if (fd2 >= 0) {
3057 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3058 if (addr && addrlen)
3059 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3060 }
3061 return fd2;
3062 }
3063 #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
3064 #else
3065 #define INIT_ACCEPT
3066 #endif
3067
3068 #if SANITIZER_INTERCEPT_ACCEPT4
3069 INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
3070 void *ctx;
3071 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
3072 unsigned addrlen0 = 0;
3073 if (addrlen) {
3074 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3075 addrlen0 = *addrlen;
3076 }
3077 // FIXME: under ASan the call below may write to freed memory and corrupt
3078 // its metadata. See
3079 // https://github.com/google/sanitizers/issues/321.
3080 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(accept4)(fd, addr, addrlen, f);
3081 if (fd2 >= 0) {
3082 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3083 if (addr && addrlen)
3084 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3085 }
3086 return fd2;
3087 }
3088 #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
3089 #else
3090 #define INIT_ACCEPT4
3091 #endif
3092
3093 #if SANITIZER_INTERCEPT_PACCEPT
3094 INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
3095 __sanitizer_sigset_t *set, int f) {
3096 void *ctx;
3097 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
3098 unsigned addrlen0 = 0;
3099 if (addrlen) {
3100 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3101 addrlen0 = *addrlen;
3102 }
3103 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3104 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(paccept)(fd, addr, addrlen, set, f);
3105 if (fd2 >= 0) {
3106 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3107 if (addr && addrlen)
3108 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3109 }
3110 return fd2;
3111 }
3112 #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
3113 #else
3114 #define INIT_PACCEPT
3115 #endif
3116
3117 #if SANITIZER_INTERCEPT_MODF
3118 INTERCEPTOR(double, modf, double x, double *iptr) {
3119 void *ctx;
3120 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
3121 // FIXME: under ASan the call below may write to freed memory and corrupt
3122 // its metadata. See
3123 // https://github.com/google/sanitizers/issues/321.
3124 double res = REAL(modf)(x, iptr);
3125 if (iptr) {
3126 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3127 }
3128 return res;
3129 }
3130 INTERCEPTOR(float, modff, float x, float *iptr) {
3131 void *ctx;
3132 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
3133 // FIXME: under ASan the call below may write to freed memory and corrupt
3134 // its metadata. See
3135 // https://github.com/google/sanitizers/issues/321.
3136 float res = REAL(modff)(x, iptr);
3137 if (iptr) {
3138 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3139 }
3140 return res;
3141 }
3142 INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
3143 void *ctx;
3144 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
3145 // FIXME: under ASan the call below may write to freed memory and corrupt
3146 // its metadata. See
3147 // https://github.com/google/sanitizers/issues/321.
3148 long double res = REAL(modfl)(x, iptr);
3149 if (iptr) {
3150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3151 }
3152 return res;
3153 }
3154 #define INIT_MODF \
3155 COMMON_INTERCEPT_FUNCTION(modf); \
3156 COMMON_INTERCEPT_FUNCTION(modff); \
3157 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
3158 #else
3159 #define INIT_MODF
3160 #endif
3161
3162 #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
3163 static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3164 SSIZE_T maxlen) {
3165 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
3166 if (msg->msg_name && msg->msg_namelen)
3167 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3168 if (msg->msg_iov && msg->msg_iovlen)
3169 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
3170 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3171 write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3172 if (msg->msg_control && msg->msg_controllen)
3173 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
3174 }
3175 #endif
3176
3177 #if SANITIZER_INTERCEPT_RECVMSG
3178 INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
3179 int flags) {
3180 void *ctx;
3181 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
3182 // FIXME: under ASan the call below may write to freed memory and corrupt
3183 // its metadata. See
3184 // https://github.com/google/sanitizers/issues/321.
3185 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recvmsg)(fd, msg, flags);
3186 if (res >= 0) {
3187 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3188 if (msg) {
3189 write_msghdr(ctx, msg, res);
3190 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
3191 }
3192 }
3193 return res;
3194 }
3195 #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
3196 #else
3197 #define INIT_RECVMSG
3198 #endif
3199
3200 #if SANITIZER_INTERCEPT_RECVMMSG
3201 INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3202 unsigned int vlen, int flags, void *timeout) {
3203 void *ctx;
3204 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
3205 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3206 int res =
3207 COMMON_INTERCEPTOR_BLOCK_REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
3208 if (res >= 0) {
3209 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3210 for (int i = 0; i < res; ++i) {
3211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3212 sizeof(msgvec[i].msg_len));
3213 write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3214 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
3215 }
3216 }
3217 return res;
3218 }
3219 #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
3220 #else
3221 #define INIT_RECVMMSG
3222 #endif
3223
3224 #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
3225 static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
3226 const unsigned kCmsgDataOffset =
3227 RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
3228
3229 char *p = (char *)control;
3230 char *const control_end = p + controllen;
3231 while (true) {
3232 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3233 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3234 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3235
3236 if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
3237
3238 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3239 sizeof(cmsg->cmsg_level));
3240 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3241 sizeof(cmsg->cmsg_type));
3242
3243 if (cmsg->cmsg_len > kCmsgDataOffset) {
3244 char *data = p + kCmsgDataOffset;
3245 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3246 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3247 }
3248
3249 p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
3250 }
3251 }
3252
3253 static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3254 SSIZE_T maxlen) {
3255 #define R(f) \
3256 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3257 R(name);
3258 R(namelen);
3259 R(iov);
3260 R(iovlen);
3261 R(control);
3262 R(controllen);
3263 R(flags);
3264 #undef R
3265 if (msg->msg_name && msg->msg_namelen)
3266 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3267 if (msg->msg_iov && msg->msg_iovlen)
3268 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3269 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3270 read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
3271 if (msg->msg_control && msg->msg_controllen)
3272 read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
3273 }
3274 #endif
3275
3276 #if SANITIZER_INTERCEPT_SENDMSG
3277 INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3278 int flags) {
3279 void *ctx;
3280 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3281 if (fd >= 0) {
3282 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3283 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3284 }
3285 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(sendmsg)(fd, msg, flags);
3286 if (common_flags()->intercept_send && res >= 0 && msg)
3287 read_msghdr(ctx, msg, res);
3288 return res;
3289 }
3290 #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3291 #else
3292 #define INIT_SENDMSG
3293 #endif
3294
3295 #if SANITIZER_INTERCEPT_SENDMMSG
3296 INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3297 unsigned vlen, int flags) {
3298 void *ctx;
3299 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3300 if (fd >= 0) {
3301 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3302 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3303 }
3304 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sendmmsg)(fd, msgvec, vlen, flags);
3305 if (res >= 0 && msgvec) {
3306 for (int i = 0; i < res; ++i) {
3307 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3308 sizeof(msgvec[i].msg_len));
3309 if (common_flags()->intercept_send)
3310 read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
3311 }
3312 }
3313 return res;
3314 }
3315 #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3316 #else
3317 #define INIT_SENDMMSG
3318 #endif
3319
3320 #if SANITIZER_INTERCEPT_SYSMSG
3321 INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
3322 int msgflg) {
3323 void *ctx;
3324 COMMON_INTERCEPTOR_ENTER(ctx, msgsnd, msqid, msgp, msgsz, msgflg);
3325 if (msgp)
3326 COMMON_INTERCEPTOR_READ_RANGE(ctx, msgp, sizeof(long) + msgsz);
3327 int res = COMMON_INTERCEPTOR_BLOCK_REAL(msgsnd)(msqid, msgp, msgsz, msgflg);
3328 return res;
3329 }
3330
3331 INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
3332 long msgtyp, int msgflg) {
3333 void *ctx;
3334 COMMON_INTERCEPTOR_ENTER(ctx, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
3335 SSIZE_T len =
3336 COMMON_INTERCEPTOR_BLOCK_REAL(msgrcv)(msqid, msgp, msgsz, msgtyp, msgflg);
3337 if (len != -1)
3338 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msgp, sizeof(long) + len);
3339 return len;
3340 }
3341
3342 #define INIT_SYSMSG \
3343 COMMON_INTERCEPT_FUNCTION(msgsnd); \
3344 COMMON_INTERCEPT_FUNCTION(msgrcv);
3345 #else
3346 #define INIT_SYSMSG
3347 #endif
3348
3349 #if SANITIZER_INTERCEPT_GETPEERNAME
3350 INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3351 void *ctx;
3352 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3353 unsigned addr_sz;
3354 if (addrlen) {
3355 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3356 addr_sz = *addrlen;
3357 }
3358 // FIXME: under ASan the call below may write to freed memory and corrupt
3359 // its metadata. See
3360 // https://github.com/google/sanitizers/issues/321.
3361 int res = REAL(getpeername)(sockfd, addr, addrlen);
3362 if (!res && addr && addrlen) {
3363 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3364 }
3365 return res;
3366 }
3367 #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3368 #else
3369 #define INIT_GETPEERNAME
3370 #endif
3371
3372 #if SANITIZER_INTERCEPT_SYSINFO
3373 INTERCEPTOR(int, sysinfo, void *info) {
3374 void *ctx;
3375 // FIXME: under ASan the call below may write to freed memory and corrupt
3376 // its metadata. See
3377 // https://github.com/google/sanitizers/issues/321.
3378 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3379 int res = REAL(sysinfo)(info);
3380 if (!res && info)
3381 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3382 return res;
3383 }
3384 #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3385 #else
3386 #define INIT_SYSINFO
3387 #endif
3388
3389 #if SANITIZER_INTERCEPT_READDIR
3390 INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3391 void *ctx;
3392 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3393 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3394 __sanitizer_dirent *res = REAL(opendir)(path);
3395 if (res)
3396 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3397 return res;
3398 }
3399
3400 INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3401 void *ctx;
3402 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3403 // FIXME: under ASan the call below may write to freed memory and corrupt
3404 // its metadata. See
3405 // https://github.com/google/sanitizers/issues/321.
3406 __sanitizer_dirent *res = REAL(readdir)(dirp);
3407 if (res)
3408 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3409 return res;
3410 }
3411
3412 INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3413 __sanitizer_dirent **result) {
3414 void *ctx;
3415 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3416 // FIXME: under ASan the call below may write to freed memory and corrupt
3417 // its metadata. See
3418 // https://github.com/google/sanitizers/issues/321.
3419 int res = REAL(readdir_r)(dirp, entry, result);
3420 if (!res) {
3421 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3422 if (*result)
3423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3424 }
3425 return res;
3426 }
3427
3428 #define INIT_READDIR \
3429 COMMON_INTERCEPT_FUNCTION(opendir); \
3430 COMMON_INTERCEPT_FUNCTION(readdir); \
3431 COMMON_INTERCEPT_FUNCTION(readdir_r);
3432 #else
3433 #define INIT_READDIR
3434 #endif
3435
3436 #if SANITIZER_INTERCEPT_READDIR64
3437 INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3438 void *ctx;
3439 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3440 // FIXME: under ASan the call below may write to freed memory and corrupt
3441 // its metadata. See
3442 // https://github.com/google/sanitizers/issues/321.
3443 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3444 if (res)
3445 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3446 return res;
3447 }
3448
3449 INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3450 __sanitizer_dirent64 **result) {
3451 void *ctx;
3452 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3453 // FIXME: under ASan the call below may write to freed memory and corrupt
3454 // its metadata. See
3455 // https://github.com/google/sanitizers/issues/321.
3456 int res = REAL(readdir64_r)(dirp, entry, result);
3457 if (!res) {
3458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3459 if (*result)
3460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3461 }
3462 return res;
3463 }
3464 #define INIT_READDIR64 \
3465 COMMON_INTERCEPT_FUNCTION(readdir64); \
3466 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3467 #else
3468 #define INIT_READDIR64
3469 #endif
3470
3471 #if SANITIZER_INTERCEPT_PTRACE
3472 INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3473 void *ctx;
3474 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3475 __sanitizer_iovec local_iovec;
3476
3477 void *data_arg = ptrace_data_arg(request, addr, data);
3478 if (data_arg) {
3479 if (request == ptrace_setregs) {
3480 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg, struct_user_regs_struct_sz);
3481 } else if (request == ptrace_setfpregs) {
3482 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3483 struct_user_fpregs_struct_sz);
3484 } else if (request == ptrace_setfpxregs) {
3485 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3486 struct_user_fpxregs_struct_sz);
3487 } else if (request == ptrace_setvfpregs) {
3488 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3489 struct_user_vfpregs_struct_sz);
3490 } else if (request == ptrace_setsiginfo) {
3491 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg, siginfo_t_sz);
3492
3493 // Some kernel might zero the iovec::iov_base in case of invalid
3494 // write access. In this case copy the invalid address for further
3495 // inspection.
3496 } else if (request == ptrace_setregset || request == ptrace_getregset) {
3497 __sanitizer_iovec *iovec = (__sanitizer_iovec *)data_arg;
3498 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3499 local_iovec = *iovec;
3500 if (request == ptrace_setregset)
3501 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3502 }
3503 }
3504
3505 // FIXME: under ASan the call below may write to freed memory and corrupt
3506 // its metadata. See
3507 // https://github.com/google/sanitizers/issues/321.
3508 uptr res = REAL(ptrace)(request, pid, addr, data);
3509
3510 if (!res && data_arg) {
3511 // Note that PEEK* requests assign different meaning to the return value.
3512 // This function does not handle them (nor does it need to).
3513 if (request == ptrace_getregs) {
3514 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, struct_user_regs_struct_sz);
3515 } else if (request == ptrace_getfpregs) {
3516 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3517 struct_user_fpregs_struct_sz);
3518 } else if (request == ptrace_getfpxregs) {
3519 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3520 struct_user_fpxregs_struct_sz);
3521 } else if (request == ptrace_getvfpregs) {
3522 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3523 struct_user_vfpregs_struct_sz);
3524 } else if (request == ptrace_getsiginfo) {
3525 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, siginfo_t_sz);
3526 } else if (request == ptrace_geteventmsg) {
3527 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, sizeof(unsigned long));
3528 } else if (request == ptrace_getregset) {
3529 __sanitizer_iovec *iovec = (__sanitizer_iovec *)data_arg;
3530 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3531 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3532 local_iovec.iov_len);
3533 }
3534 }
3535 return res;
3536 }
3537
3538 #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3539 #else
3540 #define INIT_PTRACE
3541 #endif
3542
3543 #if SANITIZER_INTERCEPT_SETLOCALE
3544 static void unpoison_ctype_arrays(void *ctx) {
3545 #if SANITIZER_NETBSD
3546 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3547 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3548 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3549 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3550 #endif
3551 }
3552
3553 INTERCEPTOR(char *, setlocale, int category, char *locale) {
3554 void *ctx;
3555 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3556 if (locale)
3557 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, internal_strlen(locale) + 1);
3558 char *res = REAL(setlocale)(category, locale);
3559 if (res) {
3560 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3561 unpoison_ctype_arrays(ctx);
3562 }
3563 return res;
3564 }
3565
3566 #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3567 #else
3568 #define INIT_SETLOCALE
3569 #endif
3570
3571 #if SANITIZER_INTERCEPT_GETCWD
3572 INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3573 void *ctx;
3574 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3575 // FIXME: under ASan the call below may write to freed memory and corrupt
3576 // its metadata. See
3577 // https://github.com/google/sanitizers/issues/321.
3578 char *res = REAL(getcwd)(buf, size);
3579 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3580 return res;
3581 }
3582 #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3583 #else
3584 #define INIT_GETCWD
3585 #endif
3586
3587 #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3588 INTERCEPTOR(char *, get_current_dir_name, int fake) {
3589 void *ctx;
3590 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3591 // FIXME: under ASan the call below may write to freed memory and corrupt
3592 // its metadata. See
3593 // https://github.com/google/sanitizers/issues/321.
3594 char *res = REAL(get_current_dir_name)(fake);
3595 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3596 return res;
3597 }
3598
3599 #define INIT_GET_CURRENT_DIR_NAME \
3600 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3601 #else
3602 #define INIT_GET_CURRENT_DIR_NAME
3603 #endif
3604
3605 UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3606 CHECK(endptr);
3607 if (nptr == *endptr) {
3608 // No digits were found at strtol call, we need to find out the last
3609 // symbol accessed by strtoll on our own.
3610 // We get this symbol by skipping leading blanks and optional +/- sign.
3611 while (IsSpace(*nptr)) nptr++;
3612 if (*nptr == '+' || *nptr == '-') nptr++;
3613 *endptr = const_cast<char *>(nptr);
3614 }
3615 CHECK(*endptr >= nptr);
3616 }
3617
3618 UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3619 char **endptr, char *real_endptr, int base) {
3620 if (endptr) {
3621 *endptr = real_endptr;
3622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3623 }
3624 // If base has unsupported value, strtol can exit with EINVAL
3625 // without reading any characters. So do additional checks only
3626 // if base is valid.
3627 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3628 if (is_valid_base) {
3629 FixRealStrtolEndptr(nptr, &real_endptr);
3630 }
3631 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3632 (real_endptr - nptr) + 1 : 0);
3633 }
3634
3635 #if SANITIZER_INTERCEPT_STRTOIMAX
3636 template <typename Fn>
3637 static ALWAYS_INLINE auto StrtoimaxImpl(void *ctx, Fn real, const char *nptr,
3638 char **endptr, int base)
3639 -> decltype(real(nullptr, nullptr, 0)) {
3640 char *real_endptr;
3641 auto res = real(nptr, &real_endptr, base);
3642 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3643 return res;
3644 }
3645
3646 INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3647 void *ctx;
3648 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3649 return StrtoimaxImpl(ctx, REAL(strtoimax), nptr, endptr, base);
3650 }
3651 INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3652 void *ctx;
3653 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3654 return StrtoimaxImpl(ctx, REAL(strtoumax), nptr, endptr, base);
3655 }
3656
3657 #define INIT_STRTOIMAX \
3658 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3659 COMMON_INTERCEPT_FUNCTION(strtoumax);
3660 #else
3661 #define INIT_STRTOIMAX
3662 #endif
3663
3664 #if SANITIZER_INTERCEPT_STRTOIMAX && SANITIZER_GLIBC
3665 INTERCEPTOR(INTMAX_T, __isoc23_strtoimax, const char *nptr, char **endptr, int base) {
3666 void *ctx;
3667 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoimax, nptr, endptr, base);
3668 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoimax), nptr, endptr, base);
3669 }
3670 INTERCEPTOR(UINTMAX_T, __isoc23_strtoumax, const char *nptr, char **endptr, int base) {
3671 void *ctx;
3672 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoumax, nptr, endptr, base);
3673 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoumax), nptr, endptr, base);
3674 }
3675
3676 # define INIT_STRTOIMAX_C23 \
3677 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoimax); \
3678 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoumax);
3679 #else
3680 # define INIT_STRTOIMAX_C23
3681 #endif
3682
3683 #if SANITIZER_INTERCEPT_MBSTOWCS
3684 INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3685 void *ctx;
3686 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3687 // FIXME: under ASan the call below may write to freed memory and corrupt
3688 // its metadata. See
3689 // https://github.com/google/sanitizers/issues/321.
3690 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3691 if (res != (SIZE_T) - 1 && dest) {
3692 SIZE_T write_cnt = res + (res < len);
3693 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3694 }
3695 return res;
3696 }
3697
3698 INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3699 void *ps) {
3700 void *ctx;
3701 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3702 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3703 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3704 // FIXME: under ASan the call below may write to freed memory and corrupt
3705 // its metadata. See
3706 // https://github.com/google/sanitizers/issues/321.
3707 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3708 if (res != (SIZE_T)(-1) && dest && src) {
3709 // This function, and several others, may or may not write the terminating
3710 // \0 character. They write it iff they clear *src.
3711 SIZE_T write_cnt = res + !*src;
3712 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3713 }
3714 return res;
3715 }
3716
3717 #define INIT_MBSTOWCS \
3718 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3719 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3720 #else
3721 #define INIT_MBSTOWCS
3722 #endif
3723
3724 #if SANITIZER_INTERCEPT_MBSNRTOWCS
3725 INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3726 SIZE_T len, void *ps) {
3727 void *ctx;
3728 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3729 if (src) {
3730 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3731 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3732 }
3733 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3734 // FIXME: under ASan the call below may write to freed memory and corrupt
3735 // its metadata. See
3736 // https://github.com/google/sanitizers/issues/321.
3737 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3738 if (res != (SIZE_T)(-1) && dest && src) {
3739 SIZE_T write_cnt = res + !*src;
3740 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3741 }
3742 return res;
3743 }
3744
3745 #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3746 #else
3747 #define INIT_MBSNRTOWCS
3748 #endif
3749
3750 #if SANITIZER_INTERCEPT_WCSTOMBS
3751 INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3752 void *ctx;
3753 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3754 // FIXME: under ASan the call below may write to freed memory and corrupt
3755 // its metadata. See
3756 // https://github.com/google/sanitizers/issues/321.
3757 SIZE_T res = REAL(wcstombs)(dest, src, len);
3758 if (res != (SIZE_T) - 1 && dest) {
3759 SIZE_T write_cnt = res + (res < len);
3760 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3761 }
3762 return res;
3763 }
3764
3765 INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3766 void *ps) {
3767 void *ctx;
3768 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3769 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3770 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3771 // FIXME: under ASan the call below may write to freed memory and corrupt
3772 // its metadata. See
3773 // https://github.com/google/sanitizers/issues/321.
3774 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3775 if (res != (SIZE_T) - 1 && dest && src) {
3776 SIZE_T write_cnt = res + !*src;
3777 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3778 }
3779 return res;
3780 }
3781
3782 #define INIT_WCSTOMBS \
3783 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3784 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3785 #else
3786 #define INIT_WCSTOMBS
3787 #endif
3788
3789 #if SANITIZER_INTERCEPT_WCSNRTOMBS
3790 INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3791 SIZE_T len, void *ps) {
3792 void *ctx;
3793 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3794 if (src) {
3795 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3796 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3797 }
3798 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3799 // FIXME: under ASan the call below may write to freed memory and corrupt
3800 // its metadata. See
3801 // https://github.com/google/sanitizers/issues/321.
3802 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3803 if (res != ((SIZE_T)-1) && dest && src) {
3804 SIZE_T write_cnt = res + !*src;
3805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3806 }
3807 return res;
3808 }
3809
3810 #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
3811 #else
3812 #define INIT_WCSNRTOMBS
3813 #endif
3814
3815
3816 #if SANITIZER_INTERCEPT_WCRTOMB
3817 INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
3818 void *ctx;
3819 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
3820 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3821
3822 if (!dest)
3823 return REAL(wcrtomb)(dest, src, ps);
3824
3825 char local_dest[32];
3826 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
3827 if (res != ((SIZE_T)-1)) {
3828 CHECK_LE(res, sizeof(local_dest));
3829 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3830 REAL(memcpy)(dest, local_dest, res);
3831 }
3832 return res;
3833 }
3834
3835 #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
3836 #else
3837 #define INIT_WCRTOMB
3838 #endif
3839
3840 #if SANITIZER_INTERCEPT_WCTOMB
3841 INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
3842 void *ctx;
3843 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
3844 if (!dest)
3845 return REAL(wctomb)(dest, src);
3846
3847 char local_dest[32];
3848 int res = REAL(wctomb)(local_dest, src);
3849 if (res != -1) {
3850 CHECK_LE(res, sizeof(local_dest));
3851 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
3852 REAL(memcpy)(dest, local_dest, res);
3853 }
3854 return res;
3855 }
3856
3857 #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
3858 #else
3859 #define INIT_WCTOMB
3860 #endif
3861
3862 #if SANITIZER_INTERCEPT_TCGETATTR
3863 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
3864 void *ctx;
3865 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
3866 // FIXME: under ASan the call below may write to freed memory and corrupt
3867 // its metadata. See
3868 // https://github.com/google/sanitizers/issues/321.
3869 int res = REAL(tcgetattr)(fd, termios_p);
3870 if (!res && termios_p)
3871 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
3872 return res;
3873 }
3874
3875 #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
3876 #else
3877 #define INIT_TCGETATTR
3878 #endif
3879
3880 #if SANITIZER_INTERCEPT_REALPATH
3881 INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
3882 void *ctx;
3883 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
3884 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3885
3886 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
3887 // version of a versioned symbol. For realpath(), this gives us something
3888 // (called __old_realpath) that does not handle NULL in the second argument.
3889 // Handle it as part of the interceptor.
3890 char *allocated_path = nullptr;
3891 if (!resolved_path)
3892 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
3893
3894 char *res = REAL(realpath)(path, resolved_path);
3895 if (allocated_path && !res)
3896 WRAP(free)(allocated_path);
3897 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3898 return res;
3899 }
3900 # define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
3901 #else
3902 #define INIT_REALPATH
3903 #endif
3904
3905 #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
3906 INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
3907 void *ctx;
3908 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
3909 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3910 char *res = REAL(canonicalize_file_name)(path);
3911 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3912 return res;
3913 }
3914 #define INIT_CANONICALIZE_FILE_NAME \
3915 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
3916 #else
3917 #define INIT_CANONICALIZE_FILE_NAME
3918 #endif
3919
3920 #if SANITIZER_INTERCEPT_CONFSTR
3921 INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
3922 void *ctx;
3923 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
3924 // FIXME: under ASan the call below may write to freed memory and corrupt
3925 // its metadata. See
3926 // https://github.com/google/sanitizers/issues/321.
3927 SIZE_T res = REAL(confstr)(name, buf, len);
3928 if (buf && res)
3929 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
3930 return res;
3931 }
3932 #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
3933 #else
3934 #define INIT_CONFSTR
3935 #endif
3936
3937 #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
3938 INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
3939 void *ctx;
3940 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
3941 // FIXME: under ASan the call below may write to freed memory and corrupt
3942 // its metadata. See
3943 // https://github.com/google/sanitizers/issues/321.
3944 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
3945 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
3946 return res;
3947 }
3948 #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
3949 #else
3950 #define INIT_SCHED_GETAFFINITY
3951 #endif
3952
3953 #if SANITIZER_INTERCEPT_SCHED_GETPARAM
3954 INTERCEPTOR(int, sched_getparam, int pid, void *param) {
3955 void *ctx;
3956 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
3957 int res = REAL(sched_getparam)(pid, param);
3958 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
3959 return res;
3960 }
3961 #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
3962 #else
3963 #define INIT_SCHED_GETPARAM
3964 #endif
3965
3966 #if SANITIZER_INTERCEPT_STRERROR
3967 INTERCEPTOR(char *, strerror, int errnum) {
3968 void *ctx;
3969 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
3970 COMMON_INTERCEPTOR_STRERROR();
3971 char *res = REAL(strerror)(errnum);
3972 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3973 return res;
3974 }
3975 #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
3976 #else
3977 #define INIT_STRERROR
3978 #endif
3979
3980 #if SANITIZER_INTERCEPT_STRERROR_R
3981 // There are 2 versions of strerror_r:
3982 // * POSIX version returns 0 on success, negative error code on failure,
3983 // writes message to buf.
3984 // * GNU version returns message pointer, which points to either buf or some
3985 // static storage.
3986 #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
3987 SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD || \
3988 SANITIZER_FREEBSD
3989 // POSIX version. Spec is not clear on whether buf is NULL-terminated.
3990 // At least on OSX, buf contents are valid even when the call fails.
3991 INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
3992 void *ctx;
3993 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
3994 // FIXME: under ASan the call below may write to freed memory and corrupt
3995 // its metadata. See
3996 // https://github.com/google/sanitizers/issues/321.
3997 int res = REAL(strerror_r)(errnum, buf, buflen);
3998
3999 SIZE_T sz = internal_strnlen(buf, buflen);
4000 if (sz < buflen) ++sz;
4001 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4002 return res;
4003 }
4004 #else
4005 // GNU version.
4006 INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
4007 void *ctx;
4008 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
4009 // FIXME: under ASan the call below may write to freed memory and corrupt
4010 // its metadata. See
4011 // https://github.com/google/sanitizers/issues/321.
4012 char *res = REAL(strerror_r)(errnum, buf, buflen);
4013 if (res == buf)
4014 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4015 else
4016 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4017 return res;
4018 }
4019 #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
4020 //SANITIZER_APPLE
4021 #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
4022 #else
4023 #define INIT_STRERROR_R
4024 #endif
4025
4026 #if SANITIZER_INTERCEPT_XPG_STRERROR_R
4027 INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
4028 void *ctx;
4029 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
4030 // FIXME: under ASan the call below may write to freed memory and corrupt
4031 // its metadata. See
4032 // https://github.com/google/sanitizers/issues/321.
4033 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
4034 // This version always returns a null-terminated string.
4035 if (buf && buflen)
4036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
4037 return res;
4038 }
4039 #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
4040 #else
4041 #define INIT_XPG_STRERROR_R
4042 #endif
4043
4044 #if SANITIZER_INTERCEPT_SCANDIR
4045 typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
4046 typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
4047 const struct __sanitizer_dirent **);
4048
4049 static THREADLOCAL scandir_filter_f scandir_filter;
4050 static THREADLOCAL scandir_compar_f scandir_compar;
4051
4052 static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
4053 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4054 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4055 return scandir_filter(dir);
4056 }
4057
4058 static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
4059 const struct __sanitizer_dirent **b) {
4060 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4061 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4062 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4063 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4064 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4065 return scandir_compar(a, b);
4066 }
4067
4068 INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
4069 scandir_filter_f filter, scandir_compar_f compar) {
4070 void *ctx;
4071 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
4072 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4073 scandir_filter = filter;
4074 scandir_compar = compar;
4075 // FIXME: under ASan the call below may write to freed memory and corrupt
4076 // its metadata. See
4077 // https://github.com/google/sanitizers/issues/321.
4078 int res = REAL(scandir)(dirp, namelist,
4079 filter ? wrapped_scandir_filter : nullptr,
4080 compar ? wrapped_scandir_compar : nullptr);
4081 scandir_filter = nullptr;
4082 scandir_compar = nullptr;
4083 if (namelist && res > 0) {
4084 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4085 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4086 for (int i = 0; i < res; ++i)
4087 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4088 __sanitizer_dirsiz((*namelist)[i]));
4089 }
4090 return res;
4091 }
4092 #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
4093 #else
4094 #define INIT_SCANDIR
4095 #endif
4096
4097 #if SANITIZER_INTERCEPT_SCANDIR64
4098 typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
4099 typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
4100 const struct __sanitizer_dirent64 **);
4101
4102 static THREADLOCAL scandir64_filter_f scandir64_filter;
4103 static THREADLOCAL scandir64_compar_f scandir64_compar;
4104
4105 static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
4106 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4107 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4108 return scandir64_filter(dir);
4109 }
4110
4111 static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
4112 const struct __sanitizer_dirent64 **b) {
4113 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4114 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4115 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4116 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4117 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4118 return scandir64_compar(a, b);
4119 }
4120
4121 INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
4122 scandir64_filter_f filter, scandir64_compar_f compar) {
4123 void *ctx;
4124 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
4125 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4126 scandir64_filter = filter;
4127 scandir64_compar = compar;
4128 // FIXME: under ASan the call below may write to freed memory and corrupt
4129 // its metadata. See
4130 // https://github.com/google/sanitizers/issues/321.
4131 int res =
4132 REAL(scandir64)(dirp, namelist,
4133 filter ? wrapped_scandir64_filter : nullptr,
4134 compar ? wrapped_scandir64_compar : nullptr);
4135 scandir64_filter = nullptr;
4136 scandir64_compar = nullptr;
4137 if (namelist && res > 0) {
4138 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4139 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4140 for (int i = 0; i < res; ++i)
4141 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4142 __sanitizer_dirsiz((*namelist)[i]));
4143 }
4144 return res;
4145 }
4146 #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
4147 #else
4148 #define INIT_SCANDIR64
4149 #endif
4150
4151 #if SANITIZER_INTERCEPT_GETGROUPS
4152 INTERCEPTOR(int, getgroups, int size, u32 *lst) {
4153 void *ctx;
4154 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
4155 // FIXME: under ASan the call below may write to freed memory and corrupt
4156 // its metadata. See
4157 // https://github.com/google/sanitizers/issues/321.
4158 int res = REAL(getgroups)(size, lst);
4159 if (res >= 0 && lst && size > 0)
4160 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
4161 return res;
4162 }
4163 #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
4164 #else
4165 #define INIT_GETGROUPS
4166 #endif
4167
4168 #if SANITIZER_INTERCEPT_POLL
4169 static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
4170 __sanitizer_nfds_t nfds) {
4171 for (unsigned i = 0; i < nfds; ++i) {
4172 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
4173 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
4174 }
4175 }
4176
4177 static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
4178 __sanitizer_nfds_t nfds) {
4179 for (unsigned i = 0; i < nfds; ++i)
4180 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
4181 sizeof(fds[i].revents));
4182 }
4183
4184 INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4185 int timeout) {
4186 void *ctx;
4187 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
4188 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4189 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
4190 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4191 return res;
4192 }
4193 #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
4194 #else
4195 #define INIT_POLL
4196 #endif
4197
4198 #if SANITIZER_INTERCEPT_PPOLL
4199 INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4200 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
4201 void *ctx;
4202 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
4203 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4204 if (timeout_ts)
4205 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
4206 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
4207 int res =
4208 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
4209 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4210 return res;
4211 }
4212 #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
4213 #else
4214 #define INIT_PPOLL
4215 #endif
4216
4217 #if SANITIZER_INTERCEPT_WORDEXP
4218 INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
4219 void *ctx;
4220 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
4221 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
4222 // FIXME: under ASan the call below may write to freed memory and corrupt
4223 // its metadata. See
4224 // https://github.com/google/sanitizers/issues/321.
4225 int res = REAL(wordexp)(s, p, flags);
4226 if (!res && p) {
4227 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4228 uptr we_wordc =
4229 ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc;
4230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
4231 sizeof(*p->we_wordv) * (we_wordc + 1));
4232 for (uptr i = 0; i < we_wordc; ++i) {
4233 char *w = p->we_wordv[i];
4234 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);
4235 }
4236 }
4237 return res;
4238 }
4239 #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
4240 #else
4241 #define INIT_WORDEXP
4242 #endif
4243
4244 #if SANITIZER_INTERCEPT_SIGWAIT
4245 INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
4246 void *ctx;
4247 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
4248 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4249 // FIXME: under ASan the call below may write to freed memory and corrupt
4250 // its metadata. See
4251 // https://github.com/google/sanitizers/issues/321.
4252 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwait)(set, sig);
4253 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
4254 return res;
4255 }
4256 #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
4257 #else
4258 #define INIT_SIGWAIT
4259 #endif
4260
4261 #if SANITIZER_INTERCEPT_SIGWAITINFO
4262 INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
4263 void *ctx;
4264 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
4265 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4266 // FIXME: under ASan the call below may write to freed memory and corrupt
4267 // its metadata. See
4268 // https://github.com/google/sanitizers/issues/321.
4269 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwaitinfo)(set, info);
4270 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4271 return res;
4272 }
4273 #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
4274 #else
4275 #define INIT_SIGWAITINFO
4276 #endif
4277
4278 #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
4279 INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
4280 void *timeout) {
4281 void *ctx;
4282 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
4283 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
4284 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4285 // FIXME: under ASan the call below may write to freed memory and corrupt
4286 // its metadata. See
4287 // https://github.com/google/sanitizers/issues/321.
4288 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigtimedwait)(set, info, timeout);
4289 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4290 return res;
4291 }
4292 #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4293 #else
4294 #define INIT_SIGTIMEDWAIT
4295 #endif
4296
4297 #if SANITIZER_INTERCEPT_SIGSETOPS
4298 INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4299 void *ctx;
4300 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4301 // FIXME: under ASan the call below may write to freed memory and corrupt
4302 // its metadata. See
4303 // https://github.com/google/sanitizers/issues/321.
4304 int res = REAL(sigemptyset)(set);
4305 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4306 return res;
4307 }
4308
4309 INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4310 void *ctx;
4311 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4312 // FIXME: under ASan the call below may write to freed memory and corrupt
4313 // its metadata. See
4314 // https://github.com/google/sanitizers/issues/321.
4315 int res = REAL(sigfillset)(set);
4316 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4317 return res;
4318 }
4319 #define INIT_SIGSETOPS \
4320 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4321 COMMON_INTERCEPT_FUNCTION(sigfillset);
4322 #else
4323 #define INIT_SIGSETOPS
4324 #endif
4325
4326 #if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
4327 INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
4328 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4329 void *ctx;
4330 COMMON_INTERCEPTOR_ENTER(ctx, sigandset, dst, src1, src2);
4331 if (src1)
4332 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4333 if (src2)
4334 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4335 int res = REAL(sigandset)(dst, src1, src2);
4336 if (!res && dst)
4337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4338 return res;
4339 }
4340
4341 INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
4342 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4343 void *ctx;
4344 COMMON_INTERCEPTOR_ENTER(ctx, sigorset, dst, src1, src2);
4345 if (src1)
4346 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4347 if (src2)
4348 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4349 int res = REAL(sigorset)(dst, src1, src2);
4350 if (!res && dst)
4351 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4352 return res;
4353 }
4354 #define INIT_SIGSET_LOGICOPS \
4355 COMMON_INTERCEPT_FUNCTION(sigandset); \
4356 COMMON_INTERCEPT_FUNCTION(sigorset);
4357 #else
4358 #define INIT_SIGSET_LOGICOPS
4359 #endif
4360
4361 #if SANITIZER_INTERCEPT_SIGPENDING
4362 INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4363 void *ctx;
4364 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4365 // FIXME: under ASan the call below may write to freed memory and corrupt
4366 // its metadata. See
4367 // https://github.com/google/sanitizers/issues/321.
4368 int res = REAL(sigpending)(set);
4369 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4370 return res;
4371 }
4372 #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4373 #else
4374 #define INIT_SIGPENDING
4375 #endif
4376
4377 #if SANITIZER_INTERCEPT_SIGPROCMASK
4378 INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4379 __sanitizer_sigset_t *oldset) {
4380 void *ctx;
4381 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4382 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4383 // FIXME: under ASan the call below may write to freed memory and corrupt
4384 // its metadata. See
4385 // https://github.com/google/sanitizers/issues/321.
4386 int res = REAL(sigprocmask)(how, set, oldset);
4387 if (!res && oldset)
4388 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4389 return res;
4390 }
4391 #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4392 #else
4393 #define INIT_SIGPROCMASK
4394 #endif
4395
4396 #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4397 INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4398 __sanitizer_sigset_t *oldset) {
4399 void *ctx;
4400 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4401 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4402 // FIXME: under ASan the call below may write to freed memory and corrupt
4403 // its metadata. See
4404 // https://github.com/google/sanitizers/issues/321.
4405 int res = REAL(pthread_sigmask)(how, set, oldset);
4406 if (!res && oldset)
4407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4408 return res;
4409 }
4410 #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4411 #else
4412 #define INIT_PTHREAD_SIGMASK
4413 #endif
4414
4415 #if SANITIZER_INTERCEPT_BACKTRACE
4416 INTERCEPTOR(int, backtrace, void **buffer, int size) {
4417 void *ctx;
4418 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4419 // 'buffer' might be freed memory, hence it is unsafe to directly call
4420 // REAL(backtrace)(buffer, size). Instead, we use our own known-good
4421 // scratch buffer.
4422 void **scratch = (void**)InternalAlloc(sizeof(void*) * size);
4423 int res = REAL(backtrace)(scratch, size);
4424 if (res && buffer) {
4425 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4426 internal_memcpy(buffer, scratch, res * sizeof(*buffer));
4427 }
4428 InternalFree(scratch);
4429 return res;
4430 }
4431
4432 INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4433 void *ctx;
4434 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4435 if (buffer && size)
4436 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4437 // The COMMON_INTERCEPTOR_READ_RANGE above ensures that 'buffer' is
4438 // valid for reading.
4439 char **res = REAL(backtrace_symbols)(buffer, size);
4440 if (res && size) {
4441 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4442 for (int i = 0; i < size; ++i)
4443 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], internal_strlen(res[i]) + 1);
4444 }
4445 return res;
4446 }
4447 #define INIT_BACKTRACE \
4448 COMMON_INTERCEPT_FUNCTION(backtrace); \
4449 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4450 #else
4451 #define INIT_BACKTRACE
4452 #endif
4453
4454 #if SANITIZER_INTERCEPT__EXIT
4455 INTERCEPTOR(void, _exit, int status) {
4456 void *ctx;
4457 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4458 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4459 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4460 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4461 if (status == 0) status = status1;
4462 REAL(_exit)(status);
4463 }
4464 #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4465 #else
4466 #define INIT__EXIT
4467 #endif
4468
4469 #if SANITIZER_INTERCEPT___LIBC_MUTEX
4470 INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4471 ALIAS(WRAP(pthread_setcancelstate));
4472
4473 #define INIT___LIBC_THR_SETCANCELSTATE \
4474 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4475 #else
4476 #define INIT___LIBC_THR_SETCANCELSTATE
4477 #endif
4478
4479 #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4480 static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4481 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4482 if (mnt->mnt_fsname)
4483 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4484 internal_strlen(mnt->mnt_fsname) + 1);
4485 if (mnt->mnt_dir)
4486 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4487 internal_strlen(mnt->mnt_dir) + 1);
4488 if (mnt->mnt_type)
4489 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4490 internal_strlen(mnt->mnt_type) + 1);
4491 if (mnt->mnt_opts)
4492 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4493 internal_strlen(mnt->mnt_opts) + 1);
4494 }
4495 #endif
4496
4497 #if SANITIZER_INTERCEPT_GETMNTENT
4498 INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4499 void *ctx;
4500 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4501 __sanitizer_mntent *res = REAL(getmntent)(fp);
4502 if (res) write_mntent(ctx, res);
4503 return res;
4504 }
4505 #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4506 #else
4507 #define INIT_GETMNTENT
4508 #endif
4509
4510 #if SANITIZER_INTERCEPT_GETMNTENT_R
4511 INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4512 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4513 void *ctx;
4514 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4515 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4516 if (res) write_mntent(ctx, res);
4517 return res;
4518 }
4519 #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4520 #else
4521 #define INIT_GETMNTENT_R
4522 #endif
4523
4524 #if SANITIZER_INTERCEPT_STATFS
4525 INTERCEPTOR(int, statfs, char *path, void *buf) {
4526 void *ctx;
4527 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4528 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4529 // FIXME: under ASan the call below may write to freed memory and corrupt
4530 // its metadata. See
4531 // https://github.com/google/sanitizers/issues/321.
4532 int res = REAL(statfs)(path, buf);
4533 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4534 return res;
4535 }
4536 INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4537 void *ctx;
4538 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4539 // FIXME: under ASan the call below may write to freed memory and corrupt
4540 // its metadata. See
4541 // https://github.com/google/sanitizers/issues/321.
4542 int res = REAL(fstatfs)(fd, buf);
4543 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4544 return res;
4545 }
4546 #define INIT_STATFS \
4547 COMMON_INTERCEPT_FUNCTION(statfs); \
4548 COMMON_INTERCEPT_FUNCTION(fstatfs);
4549 #else
4550 #define INIT_STATFS
4551 #endif
4552
4553 #if SANITIZER_INTERCEPT_STATFS64
4554 INTERCEPTOR(int, statfs64, char *path, void *buf) {
4555 void *ctx;
4556 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4557 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4558 // FIXME: under ASan the call below may write to freed memory and corrupt
4559 // its metadata. See
4560 // https://github.com/google/sanitizers/issues/321.
4561 int res = REAL(statfs64)(path, buf);
4562 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4563 return res;
4564 }
4565 INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4566 void *ctx;
4567 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4568 // FIXME: under ASan the call below may write to freed memory and corrupt
4569 // its metadata. See
4570 // https://github.com/google/sanitizers/issues/321.
4571 int res = REAL(fstatfs64)(fd, buf);
4572 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4573 return res;
4574 }
4575 #define INIT_STATFS64 \
4576 COMMON_INTERCEPT_FUNCTION(statfs64); \
4577 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4578 #else
4579 #define INIT_STATFS64
4580 #endif
4581
4582 #if SANITIZER_INTERCEPT_STATVFS
4583 INTERCEPTOR(int, statvfs, char *path, void *buf) {
4584 void *ctx;
4585 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4586 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4587 // FIXME: under ASan the call below may write to freed memory and corrupt
4588 // its metadata. See
4589 // https://github.com/google/sanitizers/issues/321.
4590 int res = REAL(statvfs)(path, buf);
4591 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4592 return res;
4593 }
4594 INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4595 void *ctx;
4596 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4597 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4598 // FIXME: under ASan the call below may write to freed memory and corrupt
4599 // its metadata. See
4600 // https://github.com/google/sanitizers/issues/321.
4601 int res = REAL(fstatvfs)(fd, buf);
4602 if (!res) {
4603 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4604 if (fd >= 0)
4605 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4606 }
4607 return res;
4608 }
4609 #define INIT_STATVFS \
4610 COMMON_INTERCEPT_FUNCTION(statvfs); \
4611 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4612 #else
4613 #define INIT_STATVFS
4614 #endif
4615
4616 #if SANITIZER_INTERCEPT_STATVFS64
4617 INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4618 void *ctx;
4619 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4620 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4621 // FIXME: under ASan the call below may write to freed memory and corrupt
4622 // its metadata. See
4623 // https://github.com/google/sanitizers/issues/321.
4624 int res = REAL(statvfs64)(path, buf);
4625 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4626 return res;
4627 }
4628 INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4629 void *ctx;
4630 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4631 // FIXME: under ASan the call below may write to freed memory and corrupt
4632 // its metadata. See
4633 // https://github.com/google/sanitizers/issues/321.
4634 int res = REAL(fstatvfs64)(fd, buf);
4635 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4636 return res;
4637 }
4638 #define INIT_STATVFS64 \
4639 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4640 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4641 #else
4642 #define INIT_STATVFS64
4643 #endif
4644
4645 #if SANITIZER_INTERCEPT_INITGROUPS
4646 INTERCEPTOR(int, initgroups, char *user, u32 group) {
4647 void *ctx;
4648 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4649 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, internal_strlen(user) + 1);
4650 int res = REAL(initgroups)(user, group);
4651 return res;
4652 }
4653 #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4654 #else
4655 #define INIT_INITGROUPS
4656 #endif
4657
4658 #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4659 INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4660 void *ctx;
4661 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4662 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4663 char *res = REAL(ether_ntoa)(addr);
4664 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4665 return res;
4666 }
4667 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4668 void *ctx;
4669 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4670 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4671 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4672 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4673 return res;
4674 }
4675 #define INIT_ETHER_NTOA_ATON \
4676 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4677 COMMON_INTERCEPT_FUNCTION(ether_aton);
4678 #else
4679 #define INIT_ETHER_NTOA_ATON
4680 #endif
4681
4682 #if SANITIZER_INTERCEPT_ETHER_HOST
4683 INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4684 void *ctx;
4685 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4686 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4687 // FIXME: under ASan the call below may write to freed memory and corrupt
4688 // its metadata. See
4689 // https://github.com/google/sanitizers/issues/321.
4690 int res = REAL(ether_ntohost)(hostname, addr);
4691 if (!res && hostname)
4692 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4693 return res;
4694 }
4695 INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4696 void *ctx;
4697 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4698 if (hostname)
4699 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4700 // FIXME: under ASan the call below may write to freed memory and corrupt
4701 // its metadata. See
4702 // https://github.com/google/sanitizers/issues/321.
4703 int res = REAL(ether_hostton)(hostname, addr);
4704 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4705 return res;
4706 }
4707 INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4708 char *hostname) {
4709 void *ctx;
4710 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4711 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, internal_strlen(line) + 1);
4712 // FIXME: under ASan the call below may write to freed memory and corrupt
4713 // its metadata. See
4714 // https://github.com/google/sanitizers/issues/321.
4715 int res = REAL(ether_line)(line, addr, hostname);
4716 if (!res) {
4717 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4718 if (hostname)
4719 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4720 }
4721 return res;
4722 }
4723 #define INIT_ETHER_HOST \
4724 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4725 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4726 COMMON_INTERCEPT_FUNCTION(ether_line);
4727 #else
4728 #define INIT_ETHER_HOST
4729 #endif
4730
4731 #if SANITIZER_INTERCEPT_ETHER_R
4732 INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4733 void *ctx;
4734 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4735 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4736 // FIXME: under ASan the call below may write to freed memory and corrupt
4737 // its metadata. See
4738 // https://github.com/google/sanitizers/issues/321.
4739 char *res = REAL(ether_ntoa_r)(addr, buf);
4740 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4741 return res;
4742 }
4743 INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4744 __sanitizer_ether_addr *addr) {
4745 void *ctx;
4746 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4747 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4748 // FIXME: under ASan the call below may write to freed memory and corrupt
4749 // its metadata. See
4750 // https://github.com/google/sanitizers/issues/321.
4751 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4752 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4753 return res;
4754 }
4755 #define INIT_ETHER_R \
4756 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4757 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4758 #else
4759 #define INIT_ETHER_R
4760 #endif
4761
4762 #if SANITIZER_INTERCEPT_SHMCTL
4763 INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4764 void *ctx;
4765 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4766 // FIXME: under ASan the call below may write to freed memory and corrupt
4767 // its metadata. See
4768 // https://github.com/google/sanitizers/issues/321.
4769 int res = REAL(shmctl)(shmid, cmd, buf);
4770 if (res >= 0) {
4771 unsigned sz = 0;
4772 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4773 sz = sizeof(__sanitizer_shmid_ds);
4774 else if (cmd == shmctl_ipc_info)
4775 sz = struct_shminfo_sz;
4776 else if (cmd == shmctl_shm_info)
4777 sz = struct_shm_info_sz;
4778 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4779 }
4780 return res;
4781 }
4782 #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4783 #else
4784 #define INIT_SHMCTL
4785 #endif
4786
4787 #if SANITIZER_INTERCEPT_RANDOM_R
4788 INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4789 void *ctx;
4790 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4791 // FIXME: under ASan the call below may write to freed memory and corrupt
4792 // its metadata. See
4793 // https://github.com/google/sanitizers/issues/321.
4794 int res = REAL(random_r)(buf, result);
4795 if (!res && result)
4796 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4797 return res;
4798 }
4799 #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4800 #else
4801 #define INIT_RANDOM_R
4802 #endif
4803
4804 // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4805 // its metadata. See
4806 // https://github.com/google/sanitizers/issues/321.
4807 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
4808 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
4809 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
4810 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
4811 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
4812 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
4813 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
4814 #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
4815 INTERCEPTOR(int, fn, void *attr, void *r) { \
4816 void *ctx; \
4817 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
4818 int res = REAL(fn)(attr, r); \
4819 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
4820 return res; \
4821 }
4822 #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
4823 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
4824 #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
4825 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
4826 #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
4827 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
4828 #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
4829 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
4830 #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
4831 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
4832 #endif
4833
4834 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
4835 INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
4836 INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
4837 INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
4838 INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
4839 INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
4840 void *ctx;
4841 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
4842 // FIXME: under ASan the call below may write to freed memory and corrupt
4843 // its metadata. See
4844 // https://github.com/google/sanitizers/issues/321.
4845 int res = REAL(pthread_attr_getstack)(attr, addr, size);
4846 if (!res) {
4847 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4848 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
4849 }
4850 return res;
4851 }
4852
4853 // We may need to call the real pthread_attr_getstack from the run-time
4854 // in sanitizer_common, but we don't want to include the interception headers
4855 // there. So, just define this function here.
4856 namespace __sanitizer {
4857 extern "C" {
4858 int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
4859 return REAL(pthread_attr_getstack)(attr, addr, size);
4860 }
4861 } // extern "C"
4862 } // namespace __sanitizer
4863
4864 #define INIT_PTHREAD_ATTR_GET \
4865 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
4866 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
4867 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
4868 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
4869 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
4870 #else
4871 #define INIT_PTHREAD_ATTR_GET
4872 #endif
4873
4874 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
4875 INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
4876 INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
4877
4878 #define INIT_PTHREAD_ATTR_GET_SCHED \
4879 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
4880 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
4881 #else
4882 #define INIT_PTHREAD_ATTR_GET_SCHED
4883 #endif
4884
4885 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
4886 INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
4887
4888 #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
4889 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
4890 #else
4891 #define INIT_PTHREAD_ATTR_GETINHERITSCHED
4892 #endif
4893
4894 #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
4895 INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
4896 void *cpuset) {
4897 void *ctx;
4898 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
4899 cpuset);
4900 // FIXME: under ASan the call below may write to freed memory and corrupt
4901 // its metadata. See
4902 // https://github.com/google/sanitizers/issues/321.
4903 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
4904 if (!res && cpusetsize && cpuset)
4905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4906 return res;
4907 }
4908
4909 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
4910 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
4911 #else
4912 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
4913 #endif
4914
4915 #if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
4916 INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
4917 void *cpuset) {
4918 void *ctx;
4919 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
4920 cpuset);
4921 // FIXME: under ASan the call below may write to freed memory and corrupt
4922 // its metadata. See
4923 // https://github.com/google/sanitizers/issues/321.
4924 int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
4925 if (!res && cpusetsize && cpuset)
4926 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4927 return res;
4928 }
4929
4930 #define INIT_PTHREAD_GETAFFINITY_NP \
4931 COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
4932 #else
4933 #define INIT_PTHREAD_GETAFFINITY_NP
4934 #endif
4935
4936 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
4937 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
4938 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
4939 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
4940 #else
4941 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
4942 #endif
4943
4944 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
4945 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
4946 #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
4947 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
4948 #else
4949 #define INIT_PTHREAD_MUTEXATTR_GETTYPE
4950 #endif
4951
4952 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
4953 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
4954 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
4955 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
4956 #else
4957 #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
4958 #endif
4959
4960 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4961 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
4962 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
4963 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
4964 #else
4965 #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
4966 #endif
4967
4968 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
4969 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
4970 #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
4971 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
4972 #else
4973 #define INIT_PTHREAD_MUTEXATTR_GETROBUST
4974 #endif
4975
4976 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
4977 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
4978 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
4979 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
4980 #else
4981 #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
4982 #endif
4983
4984 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
4985 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
4986 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
4987 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
4988 #else
4989 #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
4990 #endif
4991
4992 #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
4993 INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
4994 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
4995 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
4996 #else
4997 #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
4998 #endif
4999
5000 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
5001 INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
5002 #define INIT_PTHREAD_CONDATTR_GETPSHARED \
5003 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
5004 #else
5005 #define INIT_PTHREAD_CONDATTR_GETPSHARED
5006 #endif
5007
5008 #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
5009 INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
5010 #define INIT_PTHREAD_CONDATTR_GETCLOCK \
5011 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
5012 #else
5013 #define INIT_PTHREAD_CONDATTR_GETCLOCK
5014 #endif
5015
5016 #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
5017 INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
5018 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
5019 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
5020 #else
5021 #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
5022 #endif
5023
5024 #if SANITIZER_INTERCEPT_TMPNAM
5025 INTERCEPTOR(char *, tmpnam, char *s) {
5026 void *ctx;
5027 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
5028 char *res = REAL(tmpnam)(s);
5029 if (res) {
5030 if (s)
5031 // FIXME: under ASan the call below may write to freed memory and corrupt
5032 // its metadata. See
5033 // https://github.com/google/sanitizers/issues/321.
5034 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
5035 else
5036 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5037 }
5038 return res;
5039 }
5040 #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
5041 #else
5042 #define INIT_TMPNAM
5043 #endif
5044
5045 #if SANITIZER_INTERCEPT_TMPNAM_R
5046 INTERCEPTOR(char *, tmpnam_r, char *s) {
5047 void *ctx;
5048 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
5049 // FIXME: under ASan the call below may write to freed memory and corrupt
5050 // its metadata. See
5051 // https://github.com/google/sanitizers/issues/321.
5052 char *res = REAL(tmpnam_r)(s);
5053 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
5054 return res;
5055 }
5056 #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
5057 #else
5058 #define INIT_TMPNAM_R
5059 #endif
5060
5061 #if SANITIZER_INTERCEPT_PTSNAME
5062 INTERCEPTOR(char *, ptsname, int fd) {
5063 void *ctx;
5064 COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
5065 char *res = REAL(ptsname)(fd);
5066 if (res != nullptr)
5067 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5068 return res;
5069 }
5070 #define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
5071 #else
5072 #define INIT_PTSNAME
5073 #endif
5074
5075 #if SANITIZER_INTERCEPT_PTSNAME_R
5076 INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
5077 void *ctx;
5078 COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
5079 int res = REAL(ptsname_r)(fd, name, namesize);
5080 if (res == 0)
5081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5082 return res;
5083 }
5084 #define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
5085 #else
5086 #define INIT_PTSNAME_R
5087 #endif
5088
5089 #if SANITIZER_INTERCEPT_TTYNAME
5090 INTERCEPTOR(char *, ttyname, int fd) {
5091 void *ctx;
5092 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
5093 char *res = REAL(ttyname)(fd);
5094 if (res != nullptr)
5095 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5096 return res;
5097 }
5098 #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
5099 #else
5100 #define INIT_TTYNAME
5101 #endif
5102
5103 #if SANITIZER_INTERCEPT_TTYNAME_R
5104 INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
5105 void *ctx;
5106 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
5107 int res = REAL(ttyname_r)(fd, name, namesize);
5108 if (res == 0)
5109 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5110 return res;
5111 }
5112 #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
5113 #else
5114 #define INIT_TTYNAME_R
5115 #endif
5116
5117 #if SANITIZER_INTERCEPT_TEMPNAM
5118 INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
5119 void *ctx;
5120 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
5121 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, internal_strlen(dir) + 1);
5122 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, internal_strlen(pfx) + 1);
5123 char *res = REAL(tempnam)(dir, pfx);
5124 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5125 return res;
5126 }
5127 #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
5128 #else
5129 #define INIT_TEMPNAM
5130 #endif
5131
5132 #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
5133 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
5134 void *ctx;
5135 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
5136 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5137 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
5138 return REAL(pthread_setname_np)(thread, name);
5139 }
5140 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5141 #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
5142 INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
5143 void *ctx;
5144 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
5145 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
5146 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5147 internal_snprintf(newname, sizeof(newname), name, arg);
5148 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
5149 return REAL(pthread_setname_np)(thread, name, arg);
5150 }
5151 #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5152 #else
5153 #define INIT_PTHREAD_SETNAME_NP
5154 #endif
5155
5156 #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
5157 INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
5158 void *ctx;
5159 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
5160 int res = REAL(pthread_getname_np)(thread, name, len);
5161 if (!res)
5162 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
5163 return res;
5164 }
5165 #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
5166 #else
5167 #define INIT_PTHREAD_GETNAME_NP
5168 #endif
5169
5170 #if SANITIZER_INTERCEPT_SINCOS
5171 INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
5172 void *ctx;
5173 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
5174 // FIXME: under ASan the call below may write to freed memory and corrupt
5175 // its metadata. See
5176 // https://github.com/google/sanitizers/issues/321.
5177 REAL(sincos)(x, sin, cos);
5178 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5179 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5180 }
5181 INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
5182 void *ctx;
5183 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
5184 // FIXME: under ASan the call below may write to freed memory and corrupt
5185 // its metadata. See
5186 // https://github.com/google/sanitizers/issues/321.
5187 REAL(sincosf)(x, sin, cos);
5188 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5189 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5190 }
5191 INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
5192 void *ctx;
5193 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
5194 // FIXME: under ASan the call below may write to freed memory and corrupt
5195 // its metadata. See
5196 // https://github.com/google/sanitizers/issues/321.
5197 REAL(sincosl)(x, sin, cos);
5198 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5199 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5200 }
5201 #define INIT_SINCOS \
5202 COMMON_INTERCEPT_FUNCTION(sincos); \
5203 COMMON_INTERCEPT_FUNCTION(sincosf); \
5204 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
5205 #else
5206 #define INIT_SINCOS
5207 #endif
5208
5209 #if SANITIZER_INTERCEPT_REMQUO
5210 INTERCEPTOR(double, remquo, double x, double y, int *quo) {
5211 void *ctx;
5212 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
5213 // FIXME: under ASan the call below may write to freed memory and corrupt
5214 // its metadata. See
5215 // https://github.com/google/sanitizers/issues/321.
5216 double res = REAL(remquo)(x, y, quo);
5217 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5218 return res;
5219 }
5220 INTERCEPTOR(float, remquof, float x, float y, int *quo) {
5221 void *ctx;
5222 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
5223 // FIXME: under ASan the call below may write to freed memory and corrupt
5224 // its metadata. See
5225 // https://github.com/google/sanitizers/issues/321.
5226 float res = REAL(remquof)(x, y, quo);
5227 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5228 return res;
5229 }
5230 #define INIT_REMQUO \
5231 COMMON_INTERCEPT_FUNCTION(remquo); \
5232 COMMON_INTERCEPT_FUNCTION(remquof);
5233 #else
5234 #define INIT_REMQUO
5235 #endif
5236
5237 #if SANITIZER_INTERCEPT_REMQUOL
5238 INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
5239 void *ctx;
5240 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
5241 // FIXME: under ASan the call below may write to freed memory and corrupt
5242 // its metadata. See
5243 // https://github.com/google/sanitizers/issues/321.
5244 long double res = REAL(remquol)(x, y, quo);
5245 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5246 return res;
5247 }
5248 #define INIT_REMQUOL \
5249 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
5250 #else
5251 #define INIT_REMQUOL
5252 #endif
5253
5254 #if SANITIZER_INTERCEPT_LGAMMA
5255 extern int signgam;
5256 INTERCEPTOR(double, lgamma, double x) {
5257 void *ctx;
5258 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
5259 double res = REAL(lgamma)(x);
5260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5261 return res;
5262 }
5263 INTERCEPTOR(float, lgammaf, float x) {
5264 void *ctx;
5265 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
5266 float res = REAL(lgammaf)(x);
5267 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5268 return res;
5269 }
5270 #define INIT_LGAMMA \
5271 COMMON_INTERCEPT_FUNCTION(lgamma); \
5272 COMMON_INTERCEPT_FUNCTION(lgammaf);
5273 #else
5274 #define INIT_LGAMMA
5275 #endif
5276
5277 #if SANITIZER_INTERCEPT_LGAMMAL
5278 INTERCEPTOR(long double, lgammal, long double x) {
5279 void *ctx;
5280 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
5281 long double res = REAL(lgammal)(x);
5282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5283 return res;
5284 }
5285 #define INIT_LGAMMAL \
5286 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
5287 #else
5288 #define INIT_LGAMMAL
5289 #endif
5290
5291 #if SANITIZER_INTERCEPT_LGAMMA_R
5292 INTERCEPTOR(double, lgamma_r, double x, int *signp) {
5293 void *ctx;
5294 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
5295 // FIXME: under ASan the call below may write to freed memory and corrupt
5296 // its metadata. See
5297 // https://github.com/google/sanitizers/issues/321.
5298 double res = REAL(lgamma_r)(x, signp);
5299 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5300 return res;
5301 }
5302 INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
5303 void *ctx;
5304 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
5305 // FIXME: under ASan the call below may write to freed memory and corrupt
5306 // its metadata. See
5307 // https://github.com/google/sanitizers/issues/321.
5308 float res = REAL(lgammaf_r)(x, signp);
5309 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5310 return res;
5311 }
5312 #define INIT_LGAMMA_R \
5313 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
5314 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
5315 #else
5316 #define INIT_LGAMMA_R
5317 #endif
5318
5319 #if SANITIZER_INTERCEPT_LGAMMAL_R
5320 INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5321 void *ctx;
5322 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5323 // FIXME: under ASan the call below may write to freed memory and corrupt
5324 // its metadata. See
5325 // https://github.com/google/sanitizers/issues/321.
5326 long double res = REAL(lgammal_r)(x, signp);
5327 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5328 return res;
5329 }
5330 #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5331 #else
5332 #define INIT_LGAMMAL_R
5333 #endif
5334
5335 #if SANITIZER_INTERCEPT_DRAND48_R
5336 INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5337 void *ctx;
5338 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5339 // FIXME: under ASan the call below may write to freed memory and corrupt
5340 // its metadata. See
5341 // https://github.com/google/sanitizers/issues/321.
5342 int res = REAL(drand48_r)(buffer, result);
5343 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5344 return res;
5345 }
5346 INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5347 void *ctx;
5348 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5349 // FIXME: under ASan the call below may write to freed memory and corrupt
5350 // its metadata. See
5351 // https://github.com/google/sanitizers/issues/321.
5352 int res = REAL(lrand48_r)(buffer, result);
5353 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5354 return res;
5355 }
5356 #define INIT_DRAND48_R \
5357 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5358 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5359 #else
5360 #define INIT_DRAND48_R
5361 #endif
5362
5363 #if SANITIZER_INTERCEPT_RAND_R
5364 INTERCEPTOR(int, rand_r, unsigned *seedp) {
5365 void *ctx;
5366 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5367 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5368 return REAL(rand_r)(seedp);
5369 }
5370 #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5371 #else
5372 #define INIT_RAND_R
5373 #endif
5374
5375 #if SANITIZER_INTERCEPT_GETLINE
5376 INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5377 void *ctx;
5378 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5379 // FIXME: under ASan the call below may write to freed memory and corrupt
5380 // its metadata. See
5381 // https://github.com/google/sanitizers/issues/321.
5382 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5383 if (res > 0) {
5384 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5385 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5386 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5387 }
5388 return res;
5389 }
5390
5391 // FIXME: under ASan the call below may write to freed memory and corrupt its
5392 // metadata. See
5393 // https://github.com/google/sanitizers/issues/321.
5394 #define GETDELIM_INTERCEPTOR_IMPL(vname) \
5395 { \
5396 void *ctx; \
5397 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5398 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5399 if (res > 0) { \
5400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5402 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5403 } \
5404 return res; \
5405 }
5406
5407 INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5408 void *stream)
5409 GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5410
5411 // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5412 // with its own body.
5413 INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5414 void *stream)
5415 GETDELIM_INTERCEPTOR_IMPL(getdelim)
5416
5417 #define INIT_GETLINE \
5418 COMMON_INTERCEPT_FUNCTION(getline); \
5419 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5420 COMMON_INTERCEPT_FUNCTION(getdelim);
5421 #else
5422 #define INIT_GETLINE
5423 #endif
5424
5425 #if SANITIZER_INTERCEPT_ICONV
5426 INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5427 char **outbuf, SIZE_T *outbytesleft) {
5428 void *ctx;
5429 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5430 outbytesleft);
5431 if (inbytesleft)
5432 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5433 if (inbuf && inbytesleft)
5434 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5435 if (outbytesleft)
5436 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5437 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5438 // FIXME: under ASan the call below may write to freed memory and corrupt
5439 // its metadata. See
5440 // https://github.com/google/sanitizers/issues/321.
5441 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5442 if (outbuf && *outbuf > outbuf_orig) {
5443 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5445 }
5446 return res;
5447 }
5448 #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5449 #else
5450 #define INIT_ICONV
5451 #endif
5452
5453 #if SANITIZER_INTERCEPT_TIMES
5454 INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5455 void *ctx;
5456 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5457 // FIXME: under ASan the call below may write to freed memory and corrupt
5458 // its metadata. See
5459 // https://github.com/google/sanitizers/issues/321.
5460 __sanitizer_clock_t res = REAL(times)(tms);
5461 if (res != (__sanitizer_clock_t)-1 && tms)
5462 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5463 return res;
5464 }
5465 #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5466 #else
5467 #define INIT_TIMES
5468 #endif
5469
5470 #if SANITIZER_S390 && \
5471 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5472 extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5473 DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5474 #endif
5475
5476 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
5477 #if !SANITIZER_S390
5478 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5479 // If you see any crashes around this functions, there are 2 known issues with
5480 // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5481 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5482 // 2. It can be called recursively if sanitizer code uses __tls_get_addr
5483 // to access thread local variables (it should not happen normally,
5484 // because sanitizers use initial-exec tls model).
5485 INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5486 void *ctx;
5487 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5488 void *res = REAL(__tls_get_addr)(arg);
5489 uptr tls_begin, tls_end;
5490 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5491 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
5492 if (dtv) {
5493 // New DTLS block has been allocated.
5494 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5495 }
5496 return res;
5497 }
5498 #if SANITIZER_PPC
5499 // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5500 // mostly the same semantics as __tls_get_addr, but its presence enables
5501 // some optimizations in linker (which are safe to ignore here).
5502 INTERCEPTOR(void *, __tls_get_addr_opt, void *arg) ALIAS(WRAP(__tls_get_addr));
5503 #endif
5504 #else // SANITIZER_S390
5505 // On s390, we have to intercept two functions here:
5506 // - __tls_get_addr_internal, which is a glibc-internal function that is like
5507 // the usual __tls_get_addr, but returns a TP-relative offset instead of
5508 // a proper pointer. It is used by dlsym for TLS symbols.
5509 // - __tls_get_offset, which is like the above, but also takes a GOT-relative
5510 // descriptor offset as an argument instead of a pointer. GOT address
5511 // is passed in r12, so it's necessary to write it in assembly. This is
5512 // the function used by the compiler.
5513 #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5514 INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5515 void *ctx;
5516 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5517 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5518 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5519 void *ptr = reinterpret_cast<void *>(res + tp);
5520 uptr tls_begin, tls_end;
5521 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5522 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5523 if (dtv) {
5524 // New DTLS block has been allocated.
5525 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5526 }
5527 return res;
5528 }
5529 #endif // SANITIZER_S390
5530 #else
5531 #define INIT_TLS_GET_ADDR
5532 #endif
5533
5534 #if SANITIZER_S390 && \
5535 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5536 // We need a hidden symbol aliasing the above, so that we can jump
5537 // directly to it from the assembly below.
5538 extern "C" __attribute__((visibility("hidden"))) uptr __tls_get_addr_hidden(
5539 void *arg) ALIAS(WRAP(__tls_get_addr_internal));
5540 extern "C" uptr __tls_get_offset(void *arg);
5541 extern "C" uptr TRAMPOLINE(__tls_get_offset)(void *arg);
5542 extern "C" uptr WRAP(__tls_get_offset)(void *arg);
5543 // Now carefully intercept __tls_get_offset.
5544 asm(
5545 ".text\n"
5546 // The __intercept_ version has to exist, so that gen_dynamic_list.py
5547 // exports our symbol.
5548 ".weak __tls_get_offset\n"
5549 ".set __tls_get_offset, __interceptor___tls_get_offset\n"
5550 ".global __interceptor___tls_get_offset\n"
5551 ".type __interceptor___tls_get_offset, @function\n"
5552 "__interceptor___tls_get_offset:\n"
5553 #ifdef __s390x__
5554 "la %r2, 0(%r2,%r12)\n"
5555 "jg __tls_get_addr_hidden\n"
5556 #else
5557 "basr %r3,0\n"
5558 "0: la %r2,0(%r2,%r12)\n"
5559 "l %r4,1f-0b(%r3)\n"
5560 "b 0(%r4,%r3)\n"
5561 "1: .long __tls_get_addr_hidden - 0b\n"
5562 #endif
5563 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5564 // Assembly wrapper to call REAL(__tls_get_offset)(arg)
5565 ".type __tls_get_offset_wrapper, @function\n"
5566 "__tls_get_offset_wrapper:\n"
5567 #ifdef __s390x__
5568 "sgr %r2,%r12\n"
5569 #else
5570 "sr %r2,%r12\n"
5571 #endif
5572 "br %r3\n"
5573 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5574 );
5575 #endif
5576
5577 #if SANITIZER_INTERCEPT_LISTXATTR
5578 INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5579 void *ctx;
5580 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5581 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5582 // FIXME: under ASan the call below may write to freed memory and corrupt
5583 // its metadata. See
5584 // https://github.com/google/sanitizers/issues/321.
5585 SSIZE_T res = REAL(listxattr)(path, list, size);
5586 // Here and below, size == 0 is a special case where nothing is written to the
5587 // buffer, and res contains the desired buffer size.
5588 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5589 return res;
5590 }
5591 INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5592 void *ctx;
5593 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5594 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5595 // FIXME: under ASan the call below may write to freed memory and corrupt
5596 // its metadata. See
5597 // https://github.com/google/sanitizers/issues/321.
5598 SSIZE_T res = REAL(llistxattr)(path, list, size);
5599 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5600 return res;
5601 }
5602 INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5603 void *ctx;
5604 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
5605 // FIXME: under ASan the call below may write to freed memory and corrupt
5606 // its metadata. See
5607 // https://github.com/google/sanitizers/issues/321.
5608 SSIZE_T res = REAL(flistxattr)(fd, list, size);
5609 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5610 return res;
5611 }
5612 #define INIT_LISTXATTR \
5613 COMMON_INTERCEPT_FUNCTION(listxattr); \
5614 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5615 COMMON_INTERCEPT_FUNCTION(flistxattr);
5616 #else
5617 #define INIT_LISTXATTR
5618 #endif
5619
5620 #if SANITIZER_INTERCEPT_GETXATTR
5621 INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5622 SIZE_T size) {
5623 void *ctx;
5624 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5625 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5626 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5627 // FIXME: under ASan the call below may write to freed memory and corrupt
5628 // its metadata. See
5629 // https://github.com/google/sanitizers/issues/321.
5630 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5631 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5632 return res;
5633 }
5634 INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5635 SIZE_T size) {
5636 void *ctx;
5637 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5638 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5639 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5640 // FIXME: under ASan the call below may write to freed memory and corrupt
5641 // its metadata. See
5642 // https://github.com/google/sanitizers/issues/321.
5643 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5644 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5645 return res;
5646 }
5647 INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5648 SIZE_T size) {
5649 void *ctx;
5650 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5651 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5652 // FIXME: under ASan the call below may write to freed memory and corrupt
5653 // its metadata. See
5654 // https://github.com/google/sanitizers/issues/321.
5655 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5656 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5657 return res;
5658 }
5659 #define INIT_GETXATTR \
5660 COMMON_INTERCEPT_FUNCTION(getxattr); \
5661 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5662 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5663 #else
5664 #define INIT_GETXATTR
5665 #endif
5666
5667 #if SANITIZER_INTERCEPT_GETRESID
5668 INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5669 void *ctx;
5670 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5671 // FIXME: under ASan the call below may write to freed memory and corrupt
5672 // its metadata. See
5673 // https://github.com/google/sanitizers/issues/321.
5674 int res = REAL(getresuid)(ruid, euid, suid);
5675 if (res >= 0) {
5676 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5677 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5678 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5679 }
5680 return res;
5681 }
5682 INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5683 void *ctx;
5684 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5685 // FIXME: under ASan the call below may write to freed memory and corrupt
5686 // its metadata. See
5687 // https://github.com/google/sanitizers/issues/321.
5688 int res = REAL(getresgid)(rgid, egid, sgid);
5689 if (res >= 0) {
5690 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5691 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5692 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5693 }
5694 return res;
5695 }
5696 #define INIT_GETRESID \
5697 COMMON_INTERCEPT_FUNCTION(getresuid); \
5698 COMMON_INTERCEPT_FUNCTION(getresgid);
5699 #else
5700 #define INIT_GETRESID
5701 #endif
5702
5703 #if SANITIZER_INTERCEPT_GETIFADDRS
5704 // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5705 // intercept freeifaddrs(). If that ceases to be the case, we might need to
5706 // intercept it to poison the memory again.
5707 INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5708 void *ctx;
5709 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5710 // FIXME: under ASan the call below may write to freed memory and corrupt
5711 // its metadata. See
5712 // https://github.com/google/sanitizers/issues/321.
5713 int res = REAL(getifaddrs)(ifap);
5714 if (res == 0 && ifap) {
5715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5716 __sanitizer_ifaddrs *p = *ifap;
5717 while (p) {
5718 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5719 if (p->ifa_name)
5720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5721 internal_strlen(p->ifa_name) + 1);
5722 if (p->ifa_addr)
5723 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5724 if (p->ifa_netmask)
5725 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5726 // On Linux this is a union, but the other member also points to a
5727 // struct sockaddr, so the following is sufficient.
5728 if (p->ifa_dstaddr)
5729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5730 // FIXME(smatveev): Unpoison p->ifa_data as well.
5731 p = p->ifa_next;
5732 }
5733 }
5734 return res;
5735 }
5736 #define INIT_GETIFADDRS \
5737 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5738 #else
5739 #define INIT_GETIFADDRS
5740 #endif
5741
5742 #if SANITIZER_INTERCEPT_IF_INDEXTONAME
5743 INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5744 void *ctx;
5745 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
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 char *res = REAL(if_indextoname)(ifindex, ifname);
5750 if (res && ifname)
5751 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5752 return res;
5753 }
5754 INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5755 void *ctx;
5756 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5757 if (ifname)
5758 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5759 return REAL(if_nametoindex)(ifname);
5760 }
5761 #define INIT_IF_INDEXTONAME \
5762 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5763 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5764 #else
5765 #define INIT_IF_INDEXTONAME
5766 #endif
5767
5768 #if SANITIZER_INTERCEPT_CAPGET
5769 INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5770 void *ctx;
5771 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5772 if (hdrp)
5773 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5774 // FIXME: under ASan the call below may write to freed memory and corrupt
5775 // its metadata. See
5776 // https://github.com/google/sanitizers/issues/321.
5777 int res = REAL(capget)(hdrp, datap);
5778 if (res == 0 && datap) {
5779 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5780 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, datasz);
5781 }
5782 // We can also return -1 and write to hdrp->version if the version passed in
5783 // hdrp->version is unsupported. But that's not a trivial condition to check,
5784 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5785 return res;
5786 }
5787 INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5788 void *ctx;
5789 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5790 if (hdrp)
5791 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5792 if (datap) {
5793 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5794 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, datasz);
5795 }
5796 return REAL(capset)(hdrp, datap);
5797 }
5798 #define INIT_CAPGET \
5799 COMMON_INTERCEPT_FUNCTION(capget); \
5800 COMMON_INTERCEPT_FUNCTION(capset);
5801 #else
5802 #define INIT_CAPGET
5803 #endif
5804
5805 #if SANITIZER_INTERCEPT_FTIME
5806 INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
5807 void *ctx;
5808 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
5809 // FIXME: under ASan the call below may write to freed memory and corrupt
5810 // its metadata. See
5811 // https://github.com/google/sanitizers/issues/321.
5812 int res = REAL(ftime)(tp);
5813 if (tp)
5814 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
5815 return res;
5816 }
5817 #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
5818 #else
5819 #define INIT_FTIME
5820 #endif // SANITIZER_INTERCEPT_FTIME
5821
5822 #if SANITIZER_INTERCEPT_XDR
5823 INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
5824 unsigned size, int op) {
5825 void *ctx;
5826 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
5827 // FIXME: under ASan the call below may write to freed memory and corrupt
5828 // its metadata. See
5829 // https://github.com/google/sanitizers/issues/321.
5830 REAL(xdrmem_create)(xdrs, addr, size, op);
5831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5832 if (op == __sanitizer_XDR_ENCODE) {
5833 // It's not obvious how much data individual xdr_ routines write.
5834 // Simply unpoison the entire target buffer in advance.
5835 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
5836 }
5837 }
5838
5839 INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
5840 void *ctx;
5841 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
5842 // FIXME: under ASan the call below may write to freed memory and corrupt
5843 // its metadata. See
5844 // https://github.com/google/sanitizers/issues/321.
5845 REAL(xdrstdio_create)(xdrs, file, op);
5846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
5847 }
5848
5849 // FIXME: under ASan the call below may write to freed memory and corrupt
5850 // its metadata. See
5851 // https://github.com/google/sanitizers/issues/321.
5852 #define XDR_INTERCEPTOR(F, T) \
5853 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
5854 void *ctx; \
5855 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
5856 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
5857 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
5858 int res = REAL(F)(xdrs, p); \
5859 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
5860 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
5861 return res; \
5862 }
5863
5864 XDR_INTERCEPTOR(xdr_short, short)
5865 XDR_INTERCEPTOR(xdr_u_short, unsigned short)
5866 XDR_INTERCEPTOR(xdr_int, int)
5867 XDR_INTERCEPTOR(xdr_u_int, unsigned)
5868 XDR_INTERCEPTOR(xdr_long, long)
5869 XDR_INTERCEPTOR(xdr_u_long, unsigned long)
5870 XDR_INTERCEPTOR(xdr_hyper, long long)
5871 XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
5872 XDR_INTERCEPTOR(xdr_longlong_t, long long)
5873 XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
5874 XDR_INTERCEPTOR(xdr_int8_t, u8)
5875 XDR_INTERCEPTOR(xdr_uint8_t, u8)
5876 XDR_INTERCEPTOR(xdr_int16_t, u16)
5877 XDR_INTERCEPTOR(xdr_uint16_t, u16)
5878 XDR_INTERCEPTOR(xdr_int32_t, u32)
5879 XDR_INTERCEPTOR(xdr_uint32_t, u32)
5880 XDR_INTERCEPTOR(xdr_int64_t, u64)
5881 XDR_INTERCEPTOR(xdr_uint64_t, u64)
5882 XDR_INTERCEPTOR(xdr_quad_t, long long)
5883 XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
5884 XDR_INTERCEPTOR(xdr_bool, bool)
5885 XDR_INTERCEPTOR(xdr_enum, int)
5886 XDR_INTERCEPTOR(xdr_char, char)
5887 XDR_INTERCEPTOR(xdr_u_char, unsigned char)
5888 XDR_INTERCEPTOR(xdr_float, float)
5889 XDR_INTERCEPTOR(xdr_double, double)
5890
5891 // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
5892 // wrapstring, sizeof
5893
5894 INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
5895 unsigned maxsize) {
5896 void *ctx;
5897 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
5898 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5899 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5900 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
5901 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
5902 }
5903 // FIXME: under ASan the call below may write to freed memory and corrupt
5904 // its metadata. See
5905 // https://github.com/google/sanitizers/issues/321.
5906 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
5907 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
5908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5909 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
5910 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
5911 }
5912 return res;
5913 }
5914
5915 INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
5916 unsigned maxsize) {
5917 void *ctx;
5918 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
5919 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
5920 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
5921 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, internal_strlen(*p) + 1);
5922 }
5923 // FIXME: under ASan the call below may write to freed memory and corrupt
5924 // its metadata. See
5925 // https://github.com/google/sanitizers/issues/321.
5926 int res = REAL(xdr_string)(xdrs, p, maxsize);
5927 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
5928 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
5929 if (res && *p)
5930 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
5931 }
5932 return res;
5933 }
5934
5935 #define INIT_XDR \
5936 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
5937 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
5938 COMMON_INTERCEPT_FUNCTION(xdr_short); \
5939 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
5940 COMMON_INTERCEPT_FUNCTION(xdr_int); \
5941 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
5942 COMMON_INTERCEPT_FUNCTION(xdr_long); \
5943 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
5944 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
5945 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
5946 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
5947 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
5948 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
5949 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
5950 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
5951 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
5952 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
5953 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
5954 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
5955 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
5956 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
5957 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
5958 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
5959 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
5960 COMMON_INTERCEPT_FUNCTION(xdr_char); \
5961 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
5962 COMMON_INTERCEPT_FUNCTION(xdr_float); \
5963 COMMON_INTERCEPT_FUNCTION(xdr_double); \
5964 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
5965 COMMON_INTERCEPT_FUNCTION(xdr_string);
5966 #else
5967 #define INIT_XDR
5968 #endif // SANITIZER_INTERCEPT_XDR
5969
5970 #if SANITIZER_INTERCEPT_XDRREC
5971 typedef int (*xdrrec_cb)(char*, char*, int);
5972 struct XdrRecWrapper {
5973 char *handle;
5974 xdrrec_cb rd, wr;
5975 };
5976 typedef AddrHashMap<XdrRecWrapper *, 11> XdrRecWrapMap;
5977 static XdrRecWrapMap *xdrrec_wrap_map;
5978
5979 static int xdrrec_wr_wrap(char *handle, char *buf, int count) {
5980 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5981 COMMON_INTERCEPTOR_INITIALIZE_RANGE(buf, count);
5982 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
5983 return wrap->wr(wrap->handle, buf, count);
5984 }
5985
5986 static int xdrrec_rd_wrap(char *handle, char *buf, int count) {
5987 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
5988 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
5989 return wrap->rd(wrap->handle, buf, count);
5990 }
5991
5992 // This doesn't apply to the solaris version as it has a different function
5993 // signature.
5994 INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
5995 unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
5996 int (*wr)(char*, char*, int)) {
5997 void *ctx;
5998 COMMON_INTERCEPTOR_ENTER(ctx, xdrrec_create, xdr, sndsize, rcvsize,
5999 handle, rd, wr);
6000 COMMON_INTERCEPTOR_READ_RANGE(ctx, &xdr->x_op, sizeof xdr->x_op);
6001
6002 // We can't allocate a wrapper on the stack, as the handle is used outside
6003 // this stack frame. So we put it on the heap, and keep track of it with
6004 // the HashMap (keyed by x_private). When we later need to xdr_destroy,
6005 // we can index the map, free the wrapper, and then clean the map entry.
6006 XdrRecWrapper *wrap_data =
6007 (XdrRecWrapper *)InternalAlloc(sizeof(XdrRecWrapper));
6008 wrap_data->handle = handle;
6009 wrap_data->rd = rd;
6010 wrap_data->wr = wr;
6011 if (wr)
6012 wr = xdrrec_wr_wrap;
6013 if (rd)
6014 rd = xdrrec_rd_wrap;
6015 handle = (char *)wrap_data;
6016
6017 REAL(xdrrec_create)(xdr, sndsize, rcvsize, handle, rd, wr);
6018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdr, sizeof *xdr);
6019
6020 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, false, true);
6021 *wrap = wrap_data;
6022 }
6023
6024 // We have to intercept this to be able to free wrapper memory;
6025 // otherwise it's not necessary.
6026 INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {
6027 void *ctx;
6028 COMMON_INTERCEPTOR_ENTER(ctx, xdr_destroy, xdr);
6029
6030 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, true);
6031 InternalFree(*wrap);
6032 REAL(xdr_destroy)(xdr);
6033 }
6034 #define INIT_XDRREC_LINUX \
6035 static u64 xdrrec_wrap_mem[sizeof(XdrRecWrapMap) / sizeof(u64) + 1]; \
6036 xdrrec_wrap_map = new ((void *)&xdrrec_wrap_mem) XdrRecWrapMap(); \
6037 COMMON_INTERCEPT_FUNCTION(xdrrec_create); \
6038 COMMON_INTERCEPT_FUNCTION(xdr_destroy);
6039 #else
6040 #define INIT_XDRREC_LINUX
6041 #endif
6042
6043 #if SANITIZER_INTERCEPT_TSEARCH
6044 INTERCEPTOR(void *, tsearch, void *key, void **rootp,
6045 int (*compar)(const void *, const void *)) {
6046 void *ctx;
6047 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
6048 // FIXME: under ASan the call below may write to freed memory and corrupt
6049 // its metadata. See
6050 // https://github.com/google/sanitizers/issues/321.
6051 void *res = REAL(tsearch)(key, rootp, compar);
6052 if (res && *(void **)res == key)
6053 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
6054 return res;
6055 }
6056 #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
6057 #else
6058 #define INIT_TSEARCH
6059 #endif
6060
6061 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
6062 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6063 void unpoison_file(__sanitizer_FILE *fp) {
6064 #if SANITIZER_HAS_STRUCT_FILE
6065 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
6066 #if SANITIZER_NETBSD
6067 if (fp->_bf._base && fp->_bf._size > 0)
6068 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
6069 fp->_bf._size);
6070 #else
6071 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
6072 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
6073 fp->_IO_read_end - fp->_IO_read_base);
6074 if (fp->_IO_write_base && fp->_IO_write_base < fp->_IO_write_end)
6075 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_write_base,
6076 fp->_IO_write_end - fp->_IO_write_base);
6077 #endif
6078 #endif // SANITIZER_HAS_STRUCT_FILE
6079 }
6080 #endif
6081
6082 #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
6083 // These guys are called when a .c source is built with -O2.
6084 INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
6085 void *ctx;
6086 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
6087 int res = REAL(__uflow)(fp);
6088 unpoison_file(fp);
6089 return res;
6090 }
6091 INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
6092 void *ctx;
6093 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
6094 int res = REAL(__underflow)(fp);
6095 unpoison_file(fp);
6096 return res;
6097 }
6098 INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
6099 void *ctx;
6100 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
6101 int res = REAL(__overflow)(fp, ch);
6102 unpoison_file(fp);
6103 return res;
6104 }
6105 INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
6106 void *ctx;
6107 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
6108 int res = REAL(__wuflow)(fp);
6109 unpoison_file(fp);
6110 return res;
6111 }
6112 INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
6113 void *ctx;
6114 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
6115 int res = REAL(__wunderflow)(fp);
6116 unpoison_file(fp);
6117 return res;
6118 }
6119 INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
6120 void *ctx;
6121 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
6122 int res = REAL(__woverflow)(fp, ch);
6123 unpoison_file(fp);
6124 return res;
6125 }
6126 #define INIT_LIBIO_INTERNALS \
6127 COMMON_INTERCEPT_FUNCTION(__uflow); \
6128 COMMON_INTERCEPT_FUNCTION(__underflow); \
6129 COMMON_INTERCEPT_FUNCTION(__overflow); \
6130 COMMON_INTERCEPT_FUNCTION(__wuflow); \
6131 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
6132 COMMON_INTERCEPT_FUNCTION(__woverflow);
6133 #else
6134 #define INIT_LIBIO_INTERNALS
6135 #endif
6136
6137 #if SANITIZER_INTERCEPT_FOPEN
6138 INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
6139 void *ctx;
6140 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
6141 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6142 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6143 __sanitizer_FILE *res = REAL(fopen)(path, mode);
6144 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6145 if (res) unpoison_file(res);
6146 return res;
6147 }
6148 INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
6149 void *ctx;
6150 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
6151 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6152 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
6153 if (res) unpoison_file(res);
6154 return res;
6155 }
6156 INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
6157 __sanitizer_FILE *fp) {
6158 void *ctx;
6159 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
6160 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6161 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6162 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6163 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
6164 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6165 if (res) unpoison_file(res);
6166 return res;
6167 }
6168 #define INIT_FOPEN \
6169 COMMON_INTERCEPT_FUNCTION(fopen); \
6170 COMMON_INTERCEPT_FUNCTION(fdopen); \
6171 COMMON_INTERCEPT_FUNCTION(freopen);
6172 #else
6173 #define INIT_FOPEN
6174 #endif
6175
6176 #if SANITIZER_INTERCEPT_FLOPEN
6177 INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
6178 void *ctx;
6179 va_list ap;
6180 va_start(ap, flags);
6181 u16 mode = static_cast<u16>(va_arg(ap, u32));
6182 va_end(ap);
6183 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6184 if (path) {
6185 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6186 }
6187 return COMMON_INTERCEPTOR_BLOCK_REAL(flopen)(path, flags, mode);
6188 }
6189
6190 INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
6191 void *ctx;
6192 va_list ap;
6193 va_start(ap, flags);
6194 u16 mode = static_cast<u16>(va_arg(ap, u32));
6195 va_end(ap);
6196 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6197 if (path) {
6198 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6199 }
6200 return COMMON_INTERCEPTOR_BLOCK_REAL(flopenat)(dirfd, path, flags, mode);
6201 }
6202
6203 #define INIT_FLOPEN \
6204 COMMON_INTERCEPT_FUNCTION(flopen); \
6205 COMMON_INTERCEPT_FUNCTION(flopenat);
6206 #else
6207 #define INIT_FLOPEN
6208 #endif
6209
6210 #if SANITIZER_INTERCEPT_FOPEN64
6211 INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
6212 void *ctx;
6213 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
6214 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6215 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6216 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
6217 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6218 if (res) unpoison_file(res);
6219 return res;
6220 }
6221 INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
6222 __sanitizer_FILE *fp) {
6223 void *ctx;
6224 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
6225 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6226 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6227 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6228 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
6229 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6230 if (res) unpoison_file(res);
6231 return res;
6232 }
6233 #define INIT_FOPEN64 \
6234 COMMON_INTERCEPT_FUNCTION(fopen64); \
6235 COMMON_INTERCEPT_FUNCTION(freopen64);
6236 #else
6237 #define INIT_FOPEN64
6238 #endif
6239
6240 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6241 INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
6242 void *ctx;
6243 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
6244 // FIXME: under ASan the call below may write to freed memory and corrupt
6245 // its metadata. See
6246 // https://github.com/google/sanitizers/issues/321.
6247 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
6248 if (res) {
6249 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6250 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6251 unpoison_file(res);
6252 FileMetadata file = {ptr, sizeloc};
6253 SetInterceptorMetadata(res, file);
6254 }
6255 return res;
6256 }
6257 INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
6258 SIZE_T *sizeloc) {
6259 void *ctx;
6260 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
6261 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
6262 if (res) {
6263 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6265 unpoison_file(res);
6266 FileMetadata file = {(char **)ptr, sizeloc};
6267 SetInterceptorMetadata(res, file);
6268 }
6269 return res;
6270 }
6271 INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
6272 const char *mode) {
6273 void *ctx;
6274 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
6275 // FIXME: under ASan the call below may write to freed memory and corrupt
6276 // its metadata. See
6277 // https://github.com/google/sanitizers/issues/321.
6278 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
6279 if (res) unpoison_file(res);
6280 return res;
6281 }
6282 #define INIT_OPEN_MEMSTREAM \
6283 COMMON_INTERCEPT_FUNCTION(open_memstream); \
6284 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
6285 COMMON_INTERCEPT_FUNCTION(fmemopen);
6286 #else
6287 #define INIT_OPEN_MEMSTREAM
6288 #endif
6289
6290 #if SANITIZER_INTERCEPT_OBSTACK
6291 static void initialize_obstack(__sanitizer_obstack *obstack) {
6292 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
6293 if (obstack->chunk)
6294 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
6295 sizeof(*obstack->chunk));
6296 }
6297
6298 INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
6299 int align, void *(*alloc_fn)(uptr arg, uptr sz),
6300 void (*free_fn)(uptr arg, void *p)) {
6301 void *ctx;
6302 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
6303 free_fn);
6304 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
6305 if (res) initialize_obstack(obstack);
6306 return res;
6307 }
6308 INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
6309 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
6310 void *ctx;
6311 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
6312 free_fn);
6313 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
6314 if (res) initialize_obstack(obstack);
6315 return res;
6316 }
6317 INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
6318 void *ctx;
6319 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
6320 REAL(_obstack_newchunk)(obstack, length);
6321 if (obstack->chunk)
6322 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
6323 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
6324 }
6325 #define INIT_OBSTACK \
6326 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
6327 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
6328 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
6329 #else
6330 #define INIT_OBSTACK
6331 #endif
6332
6333 #if SANITIZER_INTERCEPT_FFLUSH
6334 INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
6335 void *ctx;
6336 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
6337 if (fp)
6338 unpoison_file(fp);
6339 int res = REAL(fflush)(fp);
6340 // FIXME: handle fp == NULL
6341 if (fp) {
6342 const FileMetadata *m = GetInterceptorMetadata(fp);
6343 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6344 }
6345 return res;
6346 }
6347 #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6348 #else
6349 #define INIT_FFLUSH
6350 #endif
6351
6352 #if SANITIZER_INTERCEPT_FCLOSE
6353 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6354 void *ctx;
6355 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6356 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6357 const FileMetadata *m = GetInterceptorMetadata(fp);
6358 if (fp)
6359 unpoison_file(fp);
6360 int res = REAL(fclose)(fp);
6361 if (m) {
6362 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6363 DeleteInterceptorMetadata(fp);
6364 }
6365 return res;
6366 }
6367 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6368 #else
6369 #define INIT_FCLOSE
6370 #endif
6371
6372 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6373 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6374 void *ctx;
6375 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6376
6377 if (filename) {
6378 COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6379
6380 # if !SANITIZER_DYNAMIC
6381 // We care about a very specific use-case: dladdr on
6382 // statically-linked ASan may return <main program>
6383 // instead of the library.
6384 // We therefore only take effect if the sanitizer is statically
6385 // linked, and we don't bother canonicalizing paths because
6386 // dladdr should return the same address both times (we assume
6387 // the user did not canonicalize the result from dladdr).
6388 if (common_flags()->test_only_replace_dlopen_main_program) {
6389 VPrintf(1, "dlopen interceptor: filename: %s\n", filename);
6390
6391 const char *SelfFName = DladdrSelfFName();
6392 VPrintf(1, "dlopen interceptor: DladdrSelfFName: %p %s\n",
6393 (const void *)SelfFName, SelfFName);
6394
6395 if (SelfFName && internal_strcmp(SelfFName, filename) == 0) {
6396 // It's possible they copied the string from dladdr, so
6397 // we do a string comparison rather than pointer comparison.
6398 VPrintf(1, "dlopen interceptor: replacing %s because it matches %s\n",
6399 filename, SelfFName);
6400 filename = (char *)0; // RTLD_DEFAULT
6401 }
6402 }
6403 # endif // !SANITIZER_DYNAMIC
6404 }
6405
6406 void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
6407 Symbolizer::GetOrInit()->InvalidateModuleList();
6408 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6409 return res;
6410 }
6411
6412 INTERCEPTOR(int, dlclose, void *handle) {
6413 void *ctx;
6414 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6415 int res = REAL(dlclose)(handle);
6416 Symbolizer::GetOrInit()->InvalidateModuleList();
6417 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6418 return res;
6419 }
6420 #define INIT_DLOPEN_DLCLOSE \
6421 COMMON_INTERCEPT_FUNCTION(dlopen); \
6422 COMMON_INTERCEPT_FUNCTION(dlclose);
6423 #else
6424 #define INIT_DLOPEN_DLCLOSE
6425 #endif
6426
6427 #if SANITIZER_INTERCEPT_GETPASS
6428 INTERCEPTOR(char *, getpass, const char *prompt) {
6429 void *ctx;
6430 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6431 if (prompt)
6432 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
6433 char *res = REAL(getpass)(prompt);
6434 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
6435 return res;
6436 }
6437
6438 #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6439 #else
6440 #define INIT_GETPASS
6441 #endif
6442
6443 #if SANITIZER_INTERCEPT_TIMERFD
6444 INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6445 void *old_value) {
6446 void *ctx;
6447 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6448 old_value);
6449 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6450 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6451 if (res != -1 && old_value)
6452 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6453 return res;
6454 }
6455
6456 INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6457 void *ctx;
6458 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6459 int res = REAL(timerfd_gettime)(fd, curr_value);
6460 if (res != -1 && curr_value)
6461 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6462 return res;
6463 }
6464 #define INIT_TIMERFD \
6465 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6466 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6467 #else
6468 #define INIT_TIMERFD
6469 #endif
6470
6471 #if SANITIZER_INTERCEPT_MLOCKX
6472 // Linux kernel has a bug that leads to kernel deadlock if a process
6473 // maps TBs of memory and then calls mlock().
6474 static void MlockIsUnsupported() {
6475 static atomic_uint8_t printed;
6476 if (atomic_exchange(&printed, 1, memory_order_relaxed))
6477 return;
6478 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6479 SanitizerToolName);
6480 }
6481
6482 INTERCEPTOR(int, mlock, const void *addr, usize len) {
6483 MlockIsUnsupported();
6484 return 0;
6485 }
6486
6487 INTERCEPTOR(int, munlock, const void *addr, usize len) {
6488 MlockIsUnsupported();
6489 return 0;
6490 }
6491
6492 INTERCEPTOR(int, mlockall, int flags) {
6493 MlockIsUnsupported();
6494 return 0;
6495 }
6496
6497 INTERCEPTOR(int, munlockall, void) {
6498 MlockIsUnsupported();
6499 return 0;
6500 }
6501
6502 #define INIT_MLOCKX \
6503 COMMON_INTERCEPT_FUNCTION(mlock); \
6504 COMMON_INTERCEPT_FUNCTION(munlock); \
6505 COMMON_INTERCEPT_FUNCTION(mlockall); \
6506 COMMON_INTERCEPT_FUNCTION(munlockall);
6507
6508 #else
6509 #define INIT_MLOCKX
6510 #endif // SANITIZER_INTERCEPT_MLOCKX
6511
6512 #if SANITIZER_INTERCEPT_FOPENCOOKIE
6513 struct WrappedCookie {
6514 void *real_cookie;
6515 __sanitizer_cookie_io_functions_t real_io_funcs;
6516 };
6517
6518 static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6519 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6520 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6521 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6522 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6523 }
6524
6525 static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6526 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6527 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6528 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6529 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6530 }
6531
6532 static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6533 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6534 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6535 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6536 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6537 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6538 : -1;
6539 }
6540
6541 static int wrapped_close(void *cookie) {
6542 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6543 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6544 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6545 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6546 InternalFree(wrapped_cookie);
6547 return res;
6548 }
6549
6550 INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6551 __sanitizer_cookie_io_functions_t io_funcs) {
6552 void *ctx;
6553 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6554 WrappedCookie *wrapped_cookie =
6555 (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
6556 wrapped_cookie->real_cookie = cookie;
6557 wrapped_cookie->real_io_funcs = io_funcs;
6558 __sanitizer_FILE *res =
6559 REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
6560 wrapped_seek, wrapped_close});
6561 return res;
6562 }
6563
6564 #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6565 #else
6566 #define INIT_FOPENCOOKIE
6567 #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6568
6569 #if SANITIZER_INTERCEPT_SEM
6570 INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6571 void *ctx;
6572 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6573 // Workaround a bug in glibc's "old" semaphore implementation by
6574 // zero-initializing the sem_t contents. This has to be done here because
6575 // interceptors bind to the lowest version before glibc 2.36, hitting the
6576 // buggy code path while the non-sanitized build of the same code works fine.
6577 REAL(memset)(s, 0, sizeof(*s));
6578 int res = REAL(sem_init)(s, pshared, value);
6579 return res;
6580 }
6581
6582 INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6583 void *ctx;
6584 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6585 int res = REAL(sem_destroy)(s);
6586 return res;
6587 }
6588
6589 INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6590 void *ctx;
6591 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6592 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6593 if (res == 0) {
6594 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6595 }
6596 return res;
6597 }
6598
6599 INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6600 void *ctx;
6601 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6602 int res = REAL(sem_trywait)(s);
6603 if (res == 0) {
6604 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6605 }
6606 return res;
6607 }
6608
6609 INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6610 void *ctx;
6611 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6612 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6613 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6614 if (res == 0) {
6615 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6616 }
6617 return res;
6618 }
6619
6620 INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6621 void *ctx;
6622 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6623 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6624 int res = REAL(sem_post)(s);
6625 return res;
6626 }
6627
6628 INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6629 void *ctx;
6630 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6631 int res = REAL(sem_getvalue)(s, sval);
6632 if (res == 0) {
6633 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6635 }
6636 return res;
6637 }
6638
6639 INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
6640 void *ctx;
6641 va_list ap;
6642 va_start(ap, oflag);
6643 u32 mode = va_arg(ap, u32);
6644 u32 value = va_arg(ap, u32);
6645 COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
6646 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6647 __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
6648 if (s)
6649 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
6650 va_end(ap);
6651 return s;
6652 }
6653
6654 INTERCEPTOR(int, sem_unlink, const char *name) {
6655 void *ctx;
6656 COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
6657 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6658 return REAL(sem_unlink)(name);
6659 }
6660
6661 # define INIT_SEM \
6662 COMMON_INTERCEPT_FUNCTION(sem_init); \
6663 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6664 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6665 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6666 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6667 COMMON_INTERCEPT_FUNCTION(sem_post); \
6668 COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
6669 COMMON_INTERCEPT_FUNCTION(sem_open); \
6670 COMMON_INTERCEPT_FUNCTION(sem_unlink);
6671 #else
6672 # define INIT_SEM
6673 #endif // SANITIZER_INTERCEPT_SEM
6674
6675 #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6676 INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6677 void *ctx;
6678 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6679 int res = REAL(pthread_setcancelstate)(state, oldstate);
6680 if (res == 0 && oldstate != nullptr)
6681 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6682 return res;
6683 }
6684
6685 INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6686 void *ctx;
6687 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6688 int res = REAL(pthread_setcanceltype)(type, oldtype);
6689 if (res == 0 && oldtype != nullptr)
6690 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6691 return res;
6692 }
6693 #define INIT_PTHREAD_SETCANCEL \
6694 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6695 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6696 #else
6697 #define INIT_PTHREAD_SETCANCEL
6698 #endif
6699
6700 #if SANITIZER_INTERCEPT_MINCORE
6701 INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6702 void *ctx;
6703 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6704 int res = REAL(mincore)(addr, length, vec);
6705 if (res == 0) {
6706 uptr page_size = GetPageSizeCached();
6707 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6708 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6709 }
6710 return res;
6711 }
6712 #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6713 #else
6714 #define INIT_MINCORE
6715 #endif
6716
6717 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
6718 INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6719 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6720 uptr flags) {
6721 void *ctx;
6722 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6723 remote_iov, riovcnt, flags);
6724 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6725 riovcnt, flags);
6726 if (res > 0)
6727 write_iovec(ctx, local_iov, liovcnt, res);
6728 return res;
6729 }
6730
6731 INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6732 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6733 uptr flags) {
6734 void *ctx;
6735 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6736 remote_iov, riovcnt, flags);
6737 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6738 riovcnt, flags);
6739 if (res > 0)
6740 read_iovec(ctx, local_iov, liovcnt, res);
6741 return res;
6742 }
6743 #define INIT_PROCESS_VM_READV \
6744 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6745 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6746 #else
6747 #define INIT_PROCESS_VM_READV
6748 #endif
6749
6750 #if SANITIZER_INTERCEPT_CTERMID
6751 INTERCEPTOR(char *, ctermid, char *s) {
6752 void *ctx;
6753 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6754 char *res = REAL(ctermid)(s);
6755 if (res) {
6756 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6757 }
6758 return res;
6759 }
6760 #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6761 #else
6762 #define INIT_CTERMID
6763 #endif
6764
6765 #if SANITIZER_INTERCEPT_CTERMID_R
6766 INTERCEPTOR(char *, ctermid_r, char *s) {
6767 void *ctx;
6768 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6769 char *res = REAL(ctermid_r)(s);
6770 if (res) {
6771 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6772 }
6773 return res;
6774 }
6775 #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6776 #else
6777 #define INIT_CTERMID_R
6778 #endif
6779
6780 #if SANITIZER_INTERCEPT_RECV_RECVFROM
6781 INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6782 void *ctx;
6783 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6784 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6785 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recv)(fd, buf, len, flags);
6786 if (res > 0) {
6787 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6788 }
6789 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6790 return res;
6791 }
6792
6793 INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6794 void *srcaddr, int *addrlen) {
6795 void *ctx;
6796 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6797 addrlen);
6798 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6799 SIZE_T srcaddr_sz;
6800 if (srcaddr) srcaddr_sz = *addrlen;
6801 (void)srcaddr_sz; // prevent "set but not used" warning
6802 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recvfrom)(fd, buf, len, flags,
6803 srcaddr, addrlen);
6804 if (res > 0)
6805 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6806 if (res >= 0 && srcaddr)
6807 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
6808 Min((SIZE_T)*addrlen, srcaddr_sz));
6809 return res;
6810 }
6811 #define INIT_RECV_RECVFROM \
6812 COMMON_INTERCEPT_FUNCTION(recv); \
6813 COMMON_INTERCEPT_FUNCTION(recvfrom);
6814 #else
6815 #define INIT_RECV_RECVFROM
6816 #endif
6817
6818 #if SANITIZER_INTERCEPT_SEND_SENDTO
6819 INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
6820 void *ctx;
6821 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
6822 if (fd >= 0) {
6823 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6824 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6825 }
6826 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(send)(fd, buf, len, flags);
6827 if (common_flags()->intercept_send && res > 0)
6828 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6829 return res;
6830 }
6831
6832 INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
6833 void *dstaddr, int addrlen) {
6834 void *ctx;
6835 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
6836 if (fd >= 0) {
6837 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6838 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6839 }
6840 // Can't check dstaddr as it may have uninitialized padding at the end.
6841 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(sendto)(fd, buf, len, flags,
6842 dstaddr, addrlen);
6843 if (common_flags()->intercept_send && res > 0)
6844 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
6845 return res;
6846 }
6847 #define INIT_SEND_SENDTO \
6848 COMMON_INTERCEPT_FUNCTION(send); \
6849 COMMON_INTERCEPT_FUNCTION(sendto);
6850 #else
6851 #define INIT_SEND_SENDTO
6852 #endif
6853
6854 #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
6855 INTERCEPTOR(int, eventfd_read, int fd, __sanitizer_eventfd_t *value) {
6856 void *ctx;
6857 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
6858 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6859 int res = COMMON_INTERCEPTOR_BLOCK_REAL(eventfd_read)(fd, value);
6860 if (res == 0) {
6861 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
6862 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6863 }
6864 return res;
6865 }
6866 INTERCEPTOR(int, eventfd_write, int fd, __sanitizer_eventfd_t value) {
6867 void *ctx;
6868 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
6869 if (fd >= 0) {
6870 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6871 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
6872 }
6873 int res = COMMON_INTERCEPTOR_BLOCK_REAL(eventfd_write)(fd, value);
6874 return res;
6875 }
6876 #define INIT_EVENTFD_READ_WRITE \
6877 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
6878 COMMON_INTERCEPT_FUNCTION(eventfd_write)
6879 #else
6880 #define INIT_EVENTFD_READ_WRITE
6881 #endif
6882
6883 #if SANITIZER_INTERCEPT_STAT
6884 INTERCEPTOR(int, stat, const char *path, void *buf) {
6885 void *ctx;
6886 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
6887 if (common_flags()->intercept_stat)
6888 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6889 int res = REAL(stat)(path, buf);
6890 if (!res)
6891 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6892 return res;
6893 }
6894 #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
6895 #else
6896 #define INIT_STAT
6897 #endif
6898
6899 #if SANITIZER_INTERCEPT_STAT64
6900 INTERCEPTOR(int, stat64, const char *path, void *buf) {
6901 void *ctx;
6902 COMMON_INTERCEPTOR_ENTER(ctx, stat64, path, buf);
6903 if (common_flags()->intercept_stat)
6904 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6905 int res = REAL(stat64)(path, buf);
6906 if (!res)
6907 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6908 return res;
6909 }
6910 #define INIT_STAT64 COMMON_INTERCEPT_FUNCTION(stat64)
6911 #else
6912 #define INIT_STAT64
6913 #endif
6914
6915
6916 #if SANITIZER_INTERCEPT_LSTAT
6917 INTERCEPTOR(int, lstat, const char *path, void *buf) {
6918 void *ctx;
6919 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
6920 if (common_flags()->intercept_stat)
6921 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6922 int res = REAL(lstat)(path, buf);
6923 if (!res)
6924 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6925 return res;
6926 }
6927 #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
6928 #else
6929 #define INIT_LSTAT
6930 #endif
6931
6932 #if SANITIZER_INTERCEPT_STAT64
6933 INTERCEPTOR(int, lstat64, const char *path, void *buf) {
6934 void *ctx;
6935 COMMON_INTERCEPTOR_ENTER(ctx, lstat64, path, buf);
6936 if (common_flags()->intercept_stat)
6937 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6938 int res = REAL(lstat64)(path, buf);
6939 if (!res)
6940 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6941 return res;
6942 }
6943 #define INIT_LSTAT64 COMMON_INTERCEPT_FUNCTION(lstat64)
6944 #else
6945 #define INIT_LSTAT64
6946 #endif
6947
6948 #if SANITIZER_INTERCEPT___XSTAT
6949 INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
6950 void *ctx;
6951 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
6952 if (common_flags()->intercept_stat)
6953 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6954 int res = REAL(__xstat)(version, path, buf);
6955 if (!res)
6956 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6957 return res;
6958 }
6959 #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
6960 #else
6961 #define INIT___XSTAT
6962 #endif
6963
6964 #if SANITIZER_INTERCEPT___XSTAT64
6965 INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
6966 void *ctx;
6967 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
6968 if (common_flags()->intercept_stat)
6969 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6970 int res = REAL(__xstat64)(version, path, buf);
6971 if (!res)
6972 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
6973 return res;
6974 }
6975 #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
6976 #else
6977 #define INIT___XSTAT64
6978 #endif
6979
6980 #if SANITIZER_INTERCEPT___LXSTAT
6981 INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
6982 void *ctx;
6983 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
6984 if (common_flags()->intercept_stat)
6985 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
6986 int res = REAL(__lxstat)(version, path, buf);
6987 if (!res)
6988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
6989 return res;
6990 }
6991 #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
6992 #else
6993 #define INIT___LXSTAT
6994 #endif
6995
6996 #if SANITIZER_INTERCEPT___LXSTAT64
6997 INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
6998 void *ctx;
6999 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
7000 if (common_flags()->intercept_stat)
7001 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7002 int res = REAL(__lxstat64)(version, path, buf);
7003 if (!res)
7004 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7005 return res;
7006 }
7007 #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
7008 #else
7009 #define INIT___LXSTAT64
7010 #endif
7011
7012 // FIXME: add other *stat interceptor
7013
7014 #if SANITIZER_INTERCEPT_UTMP
7015 INTERCEPTOR(void *, getutent, int dummy) {
7016 void *ctx;
7017 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
7018 void *res = REAL(getutent)(dummy);
7019 if (res)
7020 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7021 return res;
7022 }
7023 INTERCEPTOR(void *, getutid, void *ut) {
7024 void *ctx;
7025 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
7026 void *res = REAL(getutid)(ut);
7027 if (res)
7028 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7029 return res;
7030 }
7031 INTERCEPTOR(void *, getutline, void *ut) {
7032 void *ctx;
7033 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
7034 void *res = REAL(getutline)(ut);
7035 if (res)
7036 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7037 return res;
7038 }
7039 #define INIT_UTMP \
7040 COMMON_INTERCEPT_FUNCTION(getutent); \
7041 COMMON_INTERCEPT_FUNCTION(getutid); \
7042 COMMON_INTERCEPT_FUNCTION(getutline);
7043 #else
7044 #define INIT_UTMP
7045 #endif
7046
7047 #if SANITIZER_INTERCEPT_UTMPX
7048 INTERCEPTOR(void *, getutxent, int dummy) {
7049 void *ctx;
7050 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
7051 void *res = REAL(getutxent)(dummy);
7052 if (res)
7053 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7054 return res;
7055 }
7056 INTERCEPTOR(void *, getutxid, void *ut) {
7057 void *ctx;
7058 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
7059 void *res = REAL(getutxid)(ut);
7060 if (res)
7061 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7062 return res;
7063 }
7064 INTERCEPTOR(void *, getutxline, void *ut) {
7065 void *ctx;
7066 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
7067 void *res = REAL(getutxline)(ut);
7068 if (res)
7069 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7070 return res;
7071 }
7072 INTERCEPTOR(void *, pututxline, const void *ut) {
7073 void *ctx;
7074 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
7075 if (ut)
7076 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
7077 void *res = REAL(pututxline)(ut);
7078 if (res)
7079 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
7080 return res;
7081 }
7082 #define INIT_UTMPX \
7083 COMMON_INTERCEPT_FUNCTION(getutxent); \
7084 COMMON_INTERCEPT_FUNCTION(getutxid); \
7085 COMMON_INTERCEPT_FUNCTION(getutxline); \
7086 COMMON_INTERCEPT_FUNCTION(pututxline);
7087 #else
7088 #define INIT_UTMPX
7089 #endif
7090
7091 #if SANITIZER_INTERCEPT_GETLOADAVG
7092 INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
7093 void *ctx;
7094 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
7095 int res = REAL(getloadavg)(loadavg, nelem);
7096 if (res > 0)
7097 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
7098 return res;
7099 }
7100 #define INIT_GETLOADAVG \
7101 COMMON_INTERCEPT_FUNCTION(getloadavg);
7102 #else
7103 #define INIT_GETLOADAVG
7104 #endif
7105
7106 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
7107 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
7108 return 0;
7109 }
7110
7111 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
7112 return 0;
7113 }
7114
7115 INTERCEPTOR(int, mprobe, void *ptr) {
7116 return 0;
7117 }
7118 #endif
7119
7120 #if SANITIZER_INTERCEPT_WCSLEN
7121 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
7122 void *ctx;
7123 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
7124 SIZE_T res = REAL(wcslen)(s);
7125 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
7126 return res;
7127 }
7128
7129 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
7130 void *ctx;
7131 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
7132 SIZE_T res = REAL(wcsnlen)(s, n);
7133 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
7134 return res;
7135 }
7136 #define INIT_WCSLEN \
7137 COMMON_INTERCEPT_FUNCTION(wcslen); \
7138 COMMON_INTERCEPT_FUNCTION(wcsnlen);
7139 #else
7140 #define INIT_WCSLEN
7141 #endif
7142
7143 #if SANITIZER_INTERCEPT_WCSCAT
7144 INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
7145 void *ctx;
7146 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
7147 SIZE_T src_size = internal_wcslen(src);
7148 SIZE_T dst_size = internal_wcslen(dst);
7149 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
7150 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7151 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7152 (src_size + 1) * sizeof(wchar_t));
7153 return REAL(wcscat)(dst, src);
7154 }
7155
7156 INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
7157 void *ctx;
7158 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
7159 SIZE_T src_size = internal_wcsnlen(src, n);
7160 SIZE_T dst_size = internal_wcslen(dst);
7161 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
7162 Min(src_size + 1, n) * sizeof(wchar_t));
7163 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7164 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7165 (src_size + 1) * sizeof(wchar_t));
7166 return REAL(wcsncat)(dst, src, n);
7167 }
7168 #define INIT_WCSCAT \
7169 COMMON_INTERCEPT_FUNCTION(wcscat); \
7170 COMMON_INTERCEPT_FUNCTION(wcsncat);
7171 #else
7172 #define INIT_WCSCAT
7173 #endif
7174
7175 #if SANITIZER_INTERCEPT_WCSDUP
7176 INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
7177 void *ctx;
7178 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
7179 SIZE_T len = internal_wcslen(s);
7180 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
7181 wchar_t *result = REAL(wcsdup)(s);
7182 if (result)
7183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
7184 return result;
7185 }
7186
7187 #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
7188 #else
7189 #define INIT_WCSDUP
7190 #endif
7191
7192 #if SANITIZER_INTERCEPT_STRXFRM
7193 static SIZE_T RealStrLen(const char *str) { return internal_strlen(str); }
7194
7195 static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(str); }
7196
7197 #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
7198 { \
7199 void *ctx; \
7200 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
7201 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
7202 sizeof(*src) * (RealStrLen(src) + 1)); \
7203 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
7204 if (res < len) \
7205 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
7206 return res; \
7207 }
7208
7209 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
7210 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
7211 }
7212
7213 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
7214 void *locale) {
7215 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
7216 }
7217
7218 #define INIT_STRXFRM \
7219 COMMON_INTERCEPT_FUNCTION(strxfrm); \
7220 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
7221 #else
7222 #define INIT_STRXFRM
7223 #endif
7224
7225 #if SANITIZER_INTERCEPT___STRXFRM_L
7226 INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
7227 void *locale) {
7228 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
7229 }
7230
7231 #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
7232 #else
7233 #define INIT___STRXFRM_L
7234 #endif
7235
7236 #if SANITIZER_INTERCEPT_WCSXFRM
7237 INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
7238 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
7239 }
7240
7241 INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7242 void *locale) {
7243 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
7244 }
7245
7246 #define INIT_WCSXFRM \
7247 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
7248 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
7249 #else
7250 #define INIT_WCSXFRM
7251 #endif
7252
7253 #if SANITIZER_INTERCEPT___WCSXFRM_L
7254 INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7255 void *locale) {
7256 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
7257 }
7258
7259 #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
7260 #else
7261 #define INIT___WCSXFRM_L
7262 #endif
7263
7264 #if SANITIZER_INTERCEPT_ACCT
7265 INTERCEPTOR(int, acct, const char *file) {
7266 void *ctx;
7267 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
7268 if (file)
7269 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
7270 return REAL(acct)(file);
7271 }
7272 #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
7273 #else
7274 #define INIT_ACCT
7275 #endif
7276
7277 #if SANITIZER_INTERCEPT_USER_FROM_UID
7278 INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
7279 void *ctx;
7280 const char *user;
7281 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
7282 user = REAL(user_from_uid)(uid, nouser);
7283 if (user)
7284 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
7285 return user;
7286 }
7287 #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
7288 #else
7289 #define INIT_USER_FROM_UID
7290 #endif
7291
7292 #if SANITIZER_INTERCEPT_UID_FROM_USER
7293 INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
7294 void *ctx;
7295 int res;
7296 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
7297 if (name)
7298 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7299 res = REAL(uid_from_user)(name, uid);
7300 if (uid)
7301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
7302 return res;
7303 }
7304 #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
7305 #else
7306 #define INIT_UID_FROM_USER
7307 #endif
7308
7309 #if SANITIZER_INTERCEPT_GROUP_FROM_GID
7310 INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
7311 void *ctx;
7312 const char *group;
7313 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
7314 group = REAL(group_from_gid)(gid, nogroup);
7315 if (group)
7316 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
7317 return group;
7318 }
7319 #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
7320 #else
7321 #define INIT_GROUP_FROM_GID
7322 #endif
7323
7324 #if SANITIZER_INTERCEPT_GID_FROM_GROUP
7325 INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
7326 void *ctx;
7327 int res;
7328 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
7329 if (group)
7330 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
7331 res = REAL(gid_from_group)(group, gid);
7332 if (gid)
7333 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
7334 return res;
7335 }
7336 #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
7337 #else
7338 #define INIT_GID_FROM_GROUP
7339 #endif
7340
7341 #if SANITIZER_INTERCEPT_ACCESS
7342 INTERCEPTOR(int, access, const char *path, int mode) {
7343 void *ctx;
7344 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
7345 if (path)
7346 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7347 return REAL(access)(path, mode);
7348 }
7349 #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
7350 #else
7351 #define INIT_ACCESS
7352 #endif
7353
7354 #if SANITIZER_INTERCEPT_FACCESSAT
7355 INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
7356 void *ctx;
7357 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
7358 if (path)
7359 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7360 return REAL(faccessat)(fd, path, mode, flags);
7361 }
7362 #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
7363 #else
7364 #define INIT_FACCESSAT
7365 #endif
7366
7367 #if SANITIZER_INTERCEPT_GETGROUPLIST
7368 INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
7369 int *ngroups) {
7370 void *ctx;
7371 int res;
7372 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
7373 if (name)
7374 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7375 if (ngroups)
7376 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
7377 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
7378 if (!res && groups && ngroups) {
7379 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7380 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7381 }
7382 return res;
7383 }
7384
7385 #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
7386 #else
7387 #define INIT_GETGROUPLIST
7388 #endif
7389
7390 #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
7391 INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
7392 int maxgrp, int *ngroups) {
7393 void *ctx;
7394 int res;
7395 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
7396 maxgrp, ngroups);
7397 if (name)
7398 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7399 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
7400 if (!res && groups && ngroups) {
7401 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7402 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7403 }
7404 return res;
7405 }
7406
7407 #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
7408 #else
7409 #define INIT_GETGROUPMEMBERSHIP
7410 #endif
7411
7412 #if SANITIZER_INTERCEPT_READLINK
7413 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
7414 void* ctx;
7415 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
7416 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7417 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
7418 if (res > 0)
7419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7420 return res;
7421 }
7422
7423 #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
7424 #else
7425 #define INIT_READLINK
7426 #endif
7427
7428 #if SANITIZER_INTERCEPT_READLINKAT
7429 INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
7430 SIZE_T bufsiz) {
7431 void* ctx;
7432 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
7433 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7434 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7435 if (res > 0)
7436 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7437 return res;
7438 }
7439
7440 #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7441 #else
7442 #define INIT_READLINKAT
7443 #endif
7444
7445 #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7446 INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7447 struct file_handle *handle, int *mount_id, int flags) {
7448 void* ctx;
7449 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7450 mount_id, flags);
7451 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
7452
7453 __sanitizer_file_handle *sanitizer_handle =
7454 reinterpret_cast<__sanitizer_file_handle*>(handle);
7455 COMMON_INTERCEPTOR_READ_RANGE(
7456 ctx, &sanitizer_handle->handle_bytes,
7457 sizeof(sanitizer_handle->handle_bytes));
7458
7459 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7460 if (!res) {
7461 COMMON_INTERCEPTOR_WRITE_RANGE(
7462 ctx, &sanitizer_handle->handle_bytes,
7463 sizeof(sanitizer_handle->handle_bytes));
7464 COMMON_INTERCEPTOR_WRITE_RANGE(
7465 ctx, &sanitizer_handle->handle_type,
7466 sizeof(sanitizer_handle->handle_type));
7467 COMMON_INTERCEPTOR_WRITE_RANGE(
7468 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7469 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7470 }
7471 return res;
7472 }
7473
7474 #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7475 #else
7476 #define INIT_NAME_TO_HANDLE_AT
7477 #endif
7478
7479 #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7480 INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7481 int flags) {
7482 void* ctx;
7483 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7484
7485 __sanitizer_file_handle *sanitizer_handle =
7486 reinterpret_cast<__sanitizer_file_handle*>(handle);
7487 COMMON_INTERCEPTOR_READ_RANGE(
7488 ctx, &sanitizer_handle->handle_bytes,
7489 sizeof(sanitizer_handle->handle_bytes));
7490 COMMON_INTERCEPTOR_READ_RANGE(
7491 ctx, &sanitizer_handle->handle_type,
7492 sizeof(sanitizer_handle->handle_type));
7493 COMMON_INTERCEPTOR_READ_RANGE(
7494 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7495
7496 return COMMON_INTERCEPTOR_BLOCK_REAL(open_by_handle_at)(mount_fd, handle,
7497 flags);
7498 }
7499
7500 #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7501 #else
7502 #define INIT_OPEN_BY_HANDLE_AT
7503 #endif
7504
7505 #if SANITIZER_INTERCEPT_STRLCPY
7506 INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7507 void *ctx;
7508 SIZE_T res;
7509 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7510 if (src) {
7511 // Keep strnlen as macro argument, as macro may ignore it.
7512 COMMON_INTERCEPTOR_READ_STRING(
7513 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7514 }
7515 res = REAL(strlcpy)(dst, src, size);
7516 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
7517 return res;
7518 }
7519
7520 INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7521 void *ctx;
7522 SIZE_T len = 0;
7523 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7524 // src is checked in the strlcpy() interceptor
7525 if (dst) {
7526 len = internal_strnlen(dst, size);
7527 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7528 }
7529 // Reuse the rest of the code in the strlcpy() interceptor
7530 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7531 }
7532 #define INIT_STRLCPY \
7533 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7534 COMMON_INTERCEPT_FUNCTION(strlcat);
7535 #else
7536 #define INIT_STRLCPY
7537 #endif
7538
7539 #if SANITIZER_INTERCEPT_MMAP
7540 INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7541 OFF_T off) {
7542 void *ctx;
7543 if (common_flags()->detect_write_exec)
7544 ReportMmapWriteExec(prot, flags);
7545 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7546 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7547 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7548 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7549 }
7550
7551 INTERCEPTOR(int, munmap, void *addr, SIZE_T sz) {
7552 void *ctx;
7553 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7554 return (int)internal_munmap(addr, sz);
7555 COMMON_INTERCEPTOR_ENTER(ctx, munmap, addr, sz);
7556 COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz);
7557 }
7558
7559 INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7560 void *ctx;
7561 if (common_flags()->detect_write_exec)
7562 ReportMmapWriteExec(prot, 0);
7563 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7564 return (int)internal_mprotect(addr, sz, prot);
7565 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7566 MprotectMallocZones(addr, prot);
7567 return REAL(mprotect)(addr, sz, prot);
7568 }
7569 #define INIT_MMAP \
7570 COMMON_INTERCEPT_FUNCTION(mmap); \
7571 COMMON_INTERCEPT_FUNCTION(munmap); \
7572 COMMON_INTERCEPT_FUNCTION(mprotect);
7573 #else
7574 #define INIT_MMAP
7575 #endif
7576
7577 #if SANITIZER_INTERCEPT_MMAP64
7578 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7579 OFF64_T off) {
7580 void *ctx;
7581 if (common_flags()->detect_write_exec)
7582 ReportMmapWriteExec(prot, flags);
7583 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7584 return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
7585 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7586 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7587 }
7588 #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7589 #else
7590 #define INIT_MMAP64
7591 #endif
7592
7593 #if SANITIZER_INTERCEPT_DEVNAME
7594 INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7595 void *ctx;
7596 char *name;
7597 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7598 name = REAL(devname)(dev, type);
7599 if (name)
7600 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
7601 return name;
7602 }
7603 #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7604 #else
7605 #define INIT_DEVNAME
7606 #endif
7607
7608 #if SANITIZER_INTERCEPT_DEVNAME_R
7609 #if SANITIZER_NETBSD
7610 #define DEVNAME_R_RETTYPE int
7611 #define DEVNAME_R_SUCCESS(x) (!(x))
7612 #else
7613 #define DEVNAME_R_RETTYPE char*
7614 #define DEVNAME_R_SUCCESS(x) (x)
7615 #endif
7616 INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7617 uptr len) {
7618 void *ctx;
7619 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7620 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7621 if (DEVNAME_R_SUCCESS(res))
7622 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
7623 return res;
7624 }
7625 #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7626 #else
7627 #define INIT_DEVNAME_R
7628 #endif
7629
7630 #if SANITIZER_INTERCEPT_FGETLN
7631 INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7632 void *ctx;
7633 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7634 char *str = REAL(fgetln)(stream, len);
7635 if (str && len) {
7636 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7638 }
7639 return str;
7640 }
7641 #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7642 #else
7643 #define INIT_FGETLN
7644 #endif
7645
7646 #if SANITIZER_INTERCEPT_STRMODE
7647 INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7648 void *ctx;
7649 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7650 REAL(strmode)(mode, bp);
7651 if (bp)
7652 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
7653 }
7654 #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7655 #else
7656 #define INIT_STRMODE
7657 #endif
7658
7659 #if SANITIZER_INTERCEPT_TTYENT
7660 INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7661 void *ctx;
7662 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7663 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7664 if (ttyent)
7665 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7666 return ttyent;
7667 }
7668 INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7669 void *ctx;
7670 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7671 if (name)
7672 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7673 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7674 if (ttyent)
7675 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7676 return ttyent;
7677 }
7678 #define INIT_TTYENT \
7679 COMMON_INTERCEPT_FUNCTION(getttyent); \
7680 COMMON_INTERCEPT_FUNCTION(getttynam);
7681 #else
7682 #define INIT_TTYENT
7683 #endif
7684
7685 #if SANITIZER_INTERCEPT_TTYENTPATH
7686 INTERCEPTOR(int, setttyentpath, char *path) {
7687 void *ctx;
7688 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7689 if (path)
7690 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7691 return REAL(setttyentpath)(path);
7692 }
7693 #define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
7694 #else
7695 #define INIT_TTYENTPATH
7696 #endif
7697
7698 #if SANITIZER_INTERCEPT_PROTOENT
7699 static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
7700 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7701
7702 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
7703
7704 SIZE_T pp_size = 1; // One handles the trailing \0
7705
7706 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7707 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
7708
7709 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, pp_size * sizeof(char *));
7710 }
7711
7712 INTERCEPTOR(struct __sanitizer_protoent *, getprotoent,) {
7713 void *ctx;
7714 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent,);
7715 struct __sanitizer_protoent *p = REAL(getprotoent)();
7716 if (p)
7717 write_protoent(ctx, p);
7718 return p;
7719 }
7720
7721 INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7722 void *ctx;
7723 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7724 if (name)
7725 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7726 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7727 if (p)
7728 write_protoent(ctx, p);
7729 return p;
7730 }
7731
7732 INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7733 void *ctx;
7734 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7735 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7736 if (p)
7737 write_protoent(ctx, p);
7738 return p;
7739 }
7740 #define INIT_PROTOENT \
7741 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7742 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7743 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7744 #else
7745 #define INIT_PROTOENT
7746 #endif
7747
7748 #if SANITIZER_INTERCEPT_PROTOENT_R
7749 INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
7750 char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
7751 void *ctx;
7752 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
7753 result);
7754 int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
7755
7756 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7757 if (!res && *result)
7758 write_protoent(ctx, *result);
7759 return res;
7760 }
7761
7762 INTERCEPTOR(int, getprotobyname_r, const char *name,
7763 struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
7764 struct __sanitizer_protoent **result) {
7765 void *ctx;
7766 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
7767 buflen, result);
7768 if (name)
7769 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7770 int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
7771
7772 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7773 if (!res && *result)
7774 write_protoent(ctx, *result);
7775 return res;
7776 }
7777
7778 INTERCEPTOR(int, getprotobynumber_r, int num,
7779 struct __sanitizer_protoent *result_buf, char *buf,
7780 SIZE_T buflen, struct __sanitizer_protoent **result) {
7781 void *ctx;
7782 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
7783 buflen, result);
7784 int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
7785
7786 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7787 if (!res && *result)
7788 write_protoent(ctx, *result);
7789 return res;
7790 }
7791
7792 #define INIT_PROTOENT_R \
7793 COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
7794 COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
7795 COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
7796 #else
7797 #define INIT_PROTOENT_R
7798 #endif
7799
7800 #if SANITIZER_INTERCEPT_NETENT
7801 INTERCEPTOR(struct __sanitizer_netent *, getnetent,) {
7802 void *ctx;
7803 COMMON_INTERCEPTOR_ENTER(ctx, getnetent,);
7804 struct __sanitizer_netent *n = REAL(getnetent)();
7805 if (n) {
7806 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7807
7808 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7809
7810 SIZE_T nn_size = 1; // One handles the trailing \0
7811
7812 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7813 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7814
7815 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7816 }
7817 return n;
7818 }
7819
7820 INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
7821 void *ctx;
7822 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
7823 if (name)
7824 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7825 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
7826 if (n) {
7827 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7828
7829 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7830
7831 SIZE_T nn_size = 1; // One handles the trailing \0
7832
7833 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7834 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7835
7836 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7837 }
7838 return n;
7839 }
7840
7841 INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
7842 void *ctx;
7843 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
7844 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
7845 if (n) {
7846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
7847
7848 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
7849
7850 SIZE_T nn_size = 1; // One handles the trailing \0
7851
7852 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
7853 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
7854
7855 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
7856 }
7857 return n;
7858 }
7859 #define INIT_NETENT \
7860 COMMON_INTERCEPT_FUNCTION(getnetent); \
7861 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
7862 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
7863 #else
7864 #define INIT_NETENT
7865 #endif
7866
7867 #if SANITIZER_INTERCEPT_GETMNTINFO
7868 INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
7869 void *ctx;
7870 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
7871 int cnt = REAL(getmntinfo)(mntbufp, flags);
7872 if (cnt > 0 && mntbufp) {
7873 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
7874 if (*mntbufp)
7875 #if SANITIZER_NETBSD
7876 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
7877 #else
7878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
7879 #endif
7880 }
7881 return cnt;
7882 }
7883 #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
7884 #else
7885 #define INIT_GETMNTINFO
7886 #endif
7887
7888 #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
7889 INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
7890 u32 hashes[3]) {
7891 void *ctx;
7892 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
7893 if (key)
7894 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
7895 REAL(mi_vector_hash)(key, len, seed, hashes);
7896 if (hashes)
7897 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
7898 }
7899 #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
7900 #else
7901 #define INIT_MI_VECTOR_HASH
7902 #endif
7903
7904 #if SANITIZER_INTERCEPT_SETVBUF
7905 INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
7906 SIZE_T size) {
7907 void *ctx;
7908 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
7909 int ret = REAL(setvbuf)(stream, buf, mode, size);
7910 if (buf)
7911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7912 if (stream)
7913 unpoison_file(stream);
7914 return ret;
7915 }
7916
7917 INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7918 void *ctx;
7919 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7920 REAL(setbuf)(stream, buf);
7921 if (buf) {
7922 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7923 }
7924 if (stream)
7925 unpoison_file(stream);
7926 }
7927
7928 INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
7929 void *ctx;
7930 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
7931 REAL(setbuffer)(stream, buf, size);
7932 if (buf) {
7933 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7934 }
7935 if (stream)
7936 unpoison_file(stream);
7937 }
7938
7939 INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7940 void *ctx;
7941 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7942 REAL(setlinebuf)(stream);
7943 if (stream)
7944 unpoison_file(stream);
7945 }
7946 #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7947 COMMON_INTERCEPT_FUNCTION(setbuf); \
7948 COMMON_INTERCEPT_FUNCTION(setbuffer); \
7949 COMMON_INTERCEPT_FUNCTION(setlinebuf)
7950 #else
7951 #define INIT_SETVBUF
7952 #endif
7953
7954 #if SANITIZER_INTERCEPT_GETVFSSTAT
7955 INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
7956 void *ctx;
7957 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
7958 int ret = REAL(getvfsstat)(buf, bufsize, flags);
7959 if (buf && ret > 0)
7960 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
7961 return ret;
7962 }
7963 #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
7964 #else
7965 #define INIT_GETVFSSTAT
7966 #endif
7967
7968 #if SANITIZER_INTERCEPT_REGEX
7969 INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
7970 void *ctx;
7971 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
7972 if (pattern)
7973 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
7974 int res = REAL(regcomp)(preg, pattern, cflags);
7975 if (preg)
7976 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
7977 return res;
7978 }
7979 INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
7980 struct __sanitizer_regmatch *pmatch[], int eflags) {
7981 void *ctx;
7982 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
7983 if (preg)
7984 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7985 if (string)
7986 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
7987 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
7988 if (!res && pmatch)
7989 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
7990 return res;
7991 }
7992 INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
7993 SIZE_T errbuf_size) {
7994 void *ctx;
7995 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
7996 if (preg)
7997 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
7998 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
7999 if (errbuf)
8000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
8001 return res;
8002 }
8003 INTERCEPTOR(void, regfree, const void *preg) {
8004 void *ctx;
8005 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
8006 if (preg)
8007 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8008 REAL(regfree)(preg);
8009 }
8010 #define INIT_REGEX \
8011 COMMON_INTERCEPT_FUNCTION(regcomp); \
8012 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
8013 COMMON_INTERCEPT_FUNCTION(regerror); \
8014 COMMON_INTERCEPT_FUNCTION(regfree);
8015 #else
8016 #define INIT_REGEX
8017 #endif
8018
8019 #if SANITIZER_INTERCEPT_REGEXSUB
8020 INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
8021 const struct __sanitizer_regmatch *rm, const char *str) {
8022 void *ctx;
8023 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
8024 if (sub)
8025 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
8026 // The implementation demands and hardcodes 10 elements
8027 if (rm)
8028 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
8029 if (str)
8030 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
8031 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
8032 if (res > 0 && buf)
8033 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
8034 return res;
8035 }
8036 INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
8037 const struct __sanitizer_regmatch *rm, const char *sstr) {
8038 void *ctx;
8039 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
8040 if (sub)
8041 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
8042 // Hardcode 10 elements as this is hardcoded size
8043 if (rm)
8044 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
8045 if (sstr)
8046 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
8047 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
8048 if (res > 0 && buf) {
8049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
8050 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
8051 }
8052 return res;
8053 }
8054
8055 #define INIT_REGEXSUB \
8056 COMMON_INTERCEPT_FUNCTION(regnsub); \
8057 COMMON_INTERCEPT_FUNCTION(regasub);
8058 #else
8059 #define INIT_REGEXSUB
8060 #endif
8061
8062 #if SANITIZER_INTERCEPT_FTS
8063 INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
8064 int (*compar)(void **, void **)) {
8065 void *ctx;
8066 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
8067 if (path_argv) {
8068 for (char *const *pa = path_argv; ; ++pa) {
8069 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
8070 if (!*pa)
8071 break;
8072 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
8073 }
8074 }
8075 // TODO(kamil): handle compar callback
8076 void *fts = REAL(fts_open)(path_argv, options, compar);
8077 if (fts)
8078 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
8079 return fts;
8080 }
8081
8082 INTERCEPTOR(void *, fts_read, void *ftsp) {
8083 void *ctx;
8084 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
8085 if (ftsp)
8086 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8087 void *ftsent = REAL(fts_read)(ftsp);
8088 if (ftsent)
8089 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8090 return ftsent;
8091 }
8092
8093 INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
8094 void *ctx;
8095 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
8096 if (ftsp)
8097 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8098 void *ftsent = REAL(fts_children)(ftsp, options);
8099 if (ftsent)
8100 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8101 return ftsent;
8102 }
8103
8104 INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
8105 void *ctx;
8106 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
8107 if (ftsp)
8108 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8109 if (f)
8110 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
8111 return REAL(fts_set)(ftsp, f, options);
8112 }
8113
8114 INTERCEPTOR(int, fts_close, void *ftsp) {
8115 void *ctx;
8116 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
8117 if (ftsp)
8118 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8119 return REAL(fts_close)(ftsp);
8120 }
8121 #define INIT_FTS \
8122 COMMON_INTERCEPT_FUNCTION(fts_open); \
8123 COMMON_INTERCEPT_FUNCTION(fts_read); \
8124 COMMON_INTERCEPT_FUNCTION(fts_children); \
8125 COMMON_INTERCEPT_FUNCTION(fts_set); \
8126 COMMON_INTERCEPT_FUNCTION(fts_close);
8127 #else
8128 #define INIT_FTS
8129 #endif
8130
8131 #if SANITIZER_INTERCEPT_SYSCTL
8132 INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
8133 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
8134 void *ctx;
8135 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8136 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
8137 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
8138 newlen);
8139 if (name)
8140 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
8141 if (oldlenp)
8142 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8143 if (newp && newlen)
8144 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8145 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
8146 if (!res) {
8147 if (oldlenp) {
8148 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8149 if (oldp)
8150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8151 }
8152 }
8153 return res;
8154 }
8155
8156 INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
8157 void *newp, SIZE_T newlen) {
8158 void *ctx;
8159 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8160 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
8161 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
8162 newlen);
8163 if (sname)
8164 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8165 if (oldlenp)
8166 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8167 if (newp && newlen)
8168 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8169 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
8170 if (!res) {
8171 if (oldlenp) {
8172 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8173 if (oldp)
8174 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8175 }
8176 }
8177 return res;
8178 }
8179
8180 INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
8181 SIZE_T *namelenp) {
8182 void *ctx;
8183 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
8184 if (sname)
8185 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8186 if (namelenp)
8187 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8188 int res = REAL(sysctlnametomib)(sname, name, namelenp);
8189 if (!res) {
8190 if (namelenp) {
8191 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8192 if (name)
8193 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8194 }
8195 }
8196 return res;
8197 }
8198
8199 #define INIT_SYSCTL \
8200 COMMON_INTERCEPT_FUNCTION(sysctl); \
8201 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
8202 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
8203 #else
8204 #define INIT_SYSCTL
8205 #endif
8206
8207 #if SANITIZER_INTERCEPT_ASYSCTL
8208 INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
8209 void *ctx;
8210 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
8211 if (name)
8212 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
8213 void *res = REAL(asysctl)(name, namelen, len);
8214 if (res && len) {
8215 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8216 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8217 }
8218 return res;
8219 }
8220
8221 INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
8222 void *ctx;
8223 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
8224 if (sname)
8225 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8226 void *res = REAL(asysctlbyname)(sname, len);
8227 if (res && len) {
8228 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8229 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8230 }
8231 return res;
8232 }
8233 #define INIT_ASYSCTL \
8234 COMMON_INTERCEPT_FUNCTION(asysctl); \
8235 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
8236 #else
8237 #define INIT_ASYSCTL
8238 #endif
8239
8240 #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
8241 INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
8242 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
8243 int v) {
8244 void *ctx;
8245 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
8246 csz, rnode, v);
8247 if (sname)
8248 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8249 if (namelenp)
8250 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8251 if (csz)
8252 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
8253 // Skip rnode, it's rarely used and not trivial to sanitize
8254 // It's also used mostly internally
8255 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
8256 if (!res) {
8257 if (namelenp) {
8258 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8259 if (name)
8260 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8261 }
8262 if (csz) {
8263 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
8264 if (cname)
8265 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
8266 }
8267 }
8268 return res;
8269 }
8270 #define INIT_SYSCTLGETMIBINFO \
8271 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
8272 #else
8273 #define INIT_SYSCTLGETMIBINFO
8274 #endif
8275
8276 #if SANITIZER_INTERCEPT_NL_LANGINFO
8277 INTERCEPTOR(char *, nl_langinfo, long item) {
8278 void *ctx;
8279 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
8280 char *ret = REAL(nl_langinfo)(item);
8281 if (ret)
8282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8283 return ret;
8284 }
8285 #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
8286 #else
8287 #define INIT_NL_LANGINFO
8288 #endif
8289
8290 #if SANITIZER_INTERCEPT_MODCTL
8291 INTERCEPTOR(int, modctl, int operation, void *argp) {
8292 void *ctx;
8293 int ret;
8294 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
8295
8296 if (operation == modctl_load) {
8297 if (argp) {
8298 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
8299 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
8300 if (ml->ml_filename)
8301 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
8302 internal_strlen(ml->ml_filename) + 1);
8303 if (ml->ml_props)
8304 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
8305 }
8306 ret = REAL(modctl)(operation, argp);
8307 } else if (operation == modctl_unload) {
8308 if (argp) {
8309 const char *name = (const char *)argp;
8310 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
8311 }
8312 ret = REAL(modctl)(operation, argp);
8313 } else if (operation == modctl_stat) {
8314 uptr iov_len;
8315 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
8316 if (iov) {
8317 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
8318 iov_len = iov->iov_len;
8319 }
8320 ret = REAL(modctl)(operation, argp);
8321 if (iov)
8322 COMMON_INTERCEPTOR_WRITE_RANGE(
8323 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
8324 } else if (operation == modctl_exists) {
8325 ret = REAL(modctl)(operation, argp);
8326 } else {
8327 ret = REAL(modctl)(operation, argp);
8328 }
8329
8330 return ret;
8331 }
8332 #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
8333 #else
8334 #define INIT_MODCTL
8335 #endif
8336
8337 #if SANITIZER_INTERCEPT_STRTONUM
8338 INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
8339 long long maxval, const char **errstr) {
8340 void *ctx;
8341 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
8342
8343 // TODO(kamil): Implement strtoll as a common inteceptor
8344 char *real_endptr;
8345 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
8346 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
8347
8348 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
8349 if (errstr) {
8350 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
8351 if (*errstr)
8352 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
8353 }
8354 return ret;
8355 }
8356 #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
8357 #else
8358 #define INIT_STRTONUM
8359 #endif
8360
8361 #if SANITIZER_INTERCEPT_FPARSELN
8362 INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
8363 SIZE_T *lineno, const char delim[3], int flags) {
8364 void *ctx;
8365 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
8366 if (lineno)
8367 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
8368 if (delim)
8369 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
8370 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
8371 if (ret) {
8372 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8373 if (len)
8374 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8375 if (lineno)
8376 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
8377 }
8378 return ret;
8379 }
8380 #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
8381 #else
8382 #define INIT_FPARSELN
8383 #endif
8384
8385 #if SANITIZER_INTERCEPT_STATVFS1
8386 INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
8387 void *ctx;
8388 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
8389 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
8390 int res = REAL(statvfs1)(path, buf, flags);
8391 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8392 return res;
8393 }
8394 INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
8395 void *ctx;
8396 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
8397 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
8398 int res = REAL(fstatvfs1)(fd, buf, flags);
8399 if (!res) {
8400 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8401 if (fd >= 0)
8402 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
8403 }
8404 return res;
8405 }
8406 #define INIT_STATVFS1 \
8407 COMMON_INTERCEPT_FUNCTION(statvfs1); \
8408 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
8409 #else
8410 #define INIT_STATVFS1
8411 #endif
8412
8413 #if SANITIZER_INTERCEPT_STRTOI
8414 INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
8415 INTMAX_T low, INTMAX_T high, int *rstatus) {
8416 void *ctx;
8417 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
8418 char *real_endptr;
8419 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
8420 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8421 if (rstatus)
8422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8423 return ret;
8424 }
8425
8426 INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
8427 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
8428 void *ctx;
8429 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
8430 char *real_endptr;
8431 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
8432 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8433 if (rstatus)
8434 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8435 return ret;
8436 }
8437 #define INIT_STRTOI \
8438 COMMON_INTERCEPT_FUNCTION(strtoi); \
8439 COMMON_INTERCEPT_FUNCTION(strtou)
8440 #else
8441 #define INIT_STRTOI
8442 #endif
8443
8444 #if SANITIZER_INTERCEPT_CAPSICUM
8445 #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
8446 { \
8447 void *ctx; \
8448 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
8449 if (rights) \
8450 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8451 __sanitizer_cap_rights_t *ret = \
8452 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
8453 if (ret) \
8454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8455 return ret; \
8456 }
8457
8458 #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
8459 { \
8460 void *ctx; \
8461 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
8462 if (rights) \
8463 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8464 __sanitizer_cap_rights_t *ret = \
8465 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
8466 if (ret) \
8467 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8468 return ret; \
8469 }
8470
8471 #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
8472 { \
8473 void *ctx; \
8474 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
8475 if (rights) \
8476 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8477 __sanitizer_cap_rights_t *ret = \
8478 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8479 if (ret) \
8480 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8481 return ret; \
8482 }
8483
8484 #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8485 { \
8486 void *ctx; \
8487 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8488 if (rights) \
8489 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8490 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8491 }
8492
8493 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8494 __sanitizer_cap_rights_t *rights) {
8495 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8496 }
8497
8498 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8499 __sanitizer_cap_rights_t *rights) {
8500 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8501 }
8502
8503 INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8504 __sanitizer_cap_rights_t *rights) {
8505 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8506 }
8507
8508 INTERCEPTOR(bool, cap_rights_is_set,
8509 __sanitizer_cap_rights_t *rights) {
8510 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8511 }
8512
8513 INTERCEPTOR(int, cap_rights_limit, int fd,
8514 const __sanitizer_cap_rights_t *rights) {
8515 void *ctx;
8516 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8517 if (rights)
8518 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8519
8520 return REAL(cap_rights_limit)(fd, rights);
8521 }
8522
8523 INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8524 void *ctx;
8525 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8526 int ret = REAL(cap_rights_get)(fd, rights);
8527 if (!ret && rights)
8528 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8529
8530 return ret;
8531 }
8532
8533 INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8534 void *ctx;
8535 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8536 if (rights)
8537 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8538
8539 return REAL(cap_rights_is_valid(rights));
8540 }
8541
8542 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8543 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8544 void *ctx;
8545 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8546 if (src)
8547 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8548
8549 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8550 if (dst)
8551 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8552
8553 return ret;
8554 }
8555
8556 INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8557 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8558 void *ctx;
8559 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8560 if (src)
8561 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8562
8563 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8564 if (dst)
8565 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8566
8567 return ret;
8568 }
8569
8570 INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8571 const __sanitizer_cap_rights *little) {
8572 void *ctx;
8573 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8574 if (little)
8575 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8576 if (big)
8577 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8578
8579 return REAL(cap_rights_contains)(big, little);
8580 }
8581
8582 INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8583 void *ctx;
8584 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8585 if (cmds)
8586 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8587
8588 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8589 }
8590
8591 INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8592 void *ctx;
8593 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8594 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8595 if (!ret && cmds)
8596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8597
8598 return ret;
8599 }
8600 #define INIT_CAPSICUM \
8601 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8602 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8603 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8604 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8605 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8606 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8607 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8608 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8609 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8610 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8611 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8612 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8613 #else
8614 #define INIT_CAPSICUM
8615 #endif
8616
8617 #if SANITIZER_INTERCEPT_SHA1
8618 INTERCEPTOR(void, SHA1Init, void *context) {
8619 void *ctx;
8620 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8621 REAL(SHA1Init)(context);
8622 if (context)
8623 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8624 }
8625 INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8626 void *ctx;
8627 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8628 if (data && len > 0)
8629 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8630 if (context)
8631 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8632 REAL(SHA1Update)(context, data, len);
8633 if (context)
8634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8635 }
8636 INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8637 void *ctx;
8638 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8639 if (context)
8640 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8641 REAL(SHA1Final)(digest, context);
8642 if (digest)
8643 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8644 }
8645 INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8646 void *ctx;
8647 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8648 if (state)
8649 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8650 if (buffer)
8651 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8652 REAL(SHA1Transform)(state, buffer);
8653 if (state)
8654 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8655 }
8656 INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8657 void *ctx;
8658 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8659 if (context)
8660 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8661 char *ret = REAL(SHA1End)(context, buf);
8662 if (ret)
8663 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8664 return ret;
8665 }
8666 INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8667 void *ctx;
8668 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8669 if (filename)
8670 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8671 char *ret = REAL(SHA1File)(filename, buf);
8672 if (ret)
8673 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8674 return ret;
8675 }
8676 INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8677 OFF_T length) {
8678 void *ctx;
8679 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8680 if (filename)
8681 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8682 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8683 if (ret)
8684 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8685 return ret;
8686 }
8687 INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8688 void *ctx;
8689 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8690 if (data)
8691 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8692 char *ret = REAL(SHA1Data)(data, len, buf);
8693 if (ret)
8694 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8695 return ret;
8696 }
8697 #define INIT_SHA1 \
8698 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8699 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8700 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8701 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8702 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8703 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8704 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8705 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8706 #else
8707 #define INIT_SHA1
8708 #endif
8709
8710 #if SANITIZER_INTERCEPT_MD4
8711 INTERCEPTOR(void, MD4Init, void *context) {
8712 void *ctx;
8713 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8714 REAL(MD4Init)(context);
8715 if (context)
8716 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8717 }
8718
8719 INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8720 unsigned int len) {
8721 void *ctx;
8722 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8723 if (data && len > 0)
8724 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8725 if (context)
8726 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8727 REAL(MD4Update)(context, data, len);
8728 if (context)
8729 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8730 }
8731
8732 INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8733 void *ctx;
8734 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8735 if (context)
8736 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8737 REAL(MD4Final)(digest, context);
8738 if (digest)
8739 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8740 }
8741
8742 INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8743 void *ctx;
8744 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8745 if (context)
8746 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8747 char *ret = REAL(MD4End)(context, buf);
8748 if (ret)
8749 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8750 return ret;
8751 }
8752
8753 INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8754 void *ctx;
8755 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8756 if (filename)
8757 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8758 char *ret = REAL(MD4File)(filename, buf);
8759 if (ret)
8760 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8761 return ret;
8762 }
8763
8764 INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8765 char *buf) {
8766 void *ctx;
8767 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8768 if (data && len > 0)
8769 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8770 char *ret = REAL(MD4Data)(data, len, buf);
8771 if (ret)
8772 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8773 return ret;
8774 }
8775
8776 #define INIT_MD4 \
8777 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8778 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8779 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8780 COMMON_INTERCEPT_FUNCTION(MD4End); \
8781 COMMON_INTERCEPT_FUNCTION(MD4File); \
8782 COMMON_INTERCEPT_FUNCTION(MD4Data)
8783 #else
8784 #define INIT_MD4
8785 #endif
8786
8787 #if SANITIZER_INTERCEPT_RMD160
8788 INTERCEPTOR(void, RMD160Init, void *context) {
8789 void *ctx;
8790 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8791 REAL(RMD160Init)(context);
8792 if (context)
8793 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8794 }
8795 INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8796 void *ctx;
8797 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8798 if (data && len > 0)
8799 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8800 if (context)
8801 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8802 REAL(RMD160Update)(context, data, len);
8803 if (context)
8804 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8805 }
8806 INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
8807 void *ctx;
8808 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
8809 if (context)
8810 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8811 REAL(RMD160Final)(digest, context);
8812 if (digest)
8813 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8814 }
8815 INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
8816 void *ctx;
8817 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
8818 if (state)
8819 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8820 if (buffer)
8821 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
8822 REAL(RMD160Transform)(state, buffer);
8823 if (state)
8824 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8825 }
8826 INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
8827 void *ctx;
8828 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
8829 if (context)
8830 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8831 char *ret = REAL(RMD160End)(context, buf);
8832 if (ret)
8833 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8834 return ret;
8835 }
8836 INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
8837 void *ctx;
8838 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
8839 if (filename)
8840 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8841 char *ret = REAL(RMD160File)(filename, buf);
8842 if (ret)
8843 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8844 return ret;
8845 }
8846 INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
8847 OFF_T length) {
8848 void *ctx;
8849 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
8850 if (filename)
8851 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8852 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
8853 if (ret)
8854 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8855 return ret;
8856 }
8857 INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
8858 void *ctx;
8859 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
8860 if (data && len > 0)
8861 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8862 char *ret = REAL(RMD160Data)(data, len, buf);
8863 if (ret)
8864 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
8865 return ret;
8866 }
8867 #define INIT_RMD160 \
8868 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
8869 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
8870 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
8871 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
8872 COMMON_INTERCEPT_FUNCTION(RMD160End); \
8873 COMMON_INTERCEPT_FUNCTION(RMD160File); \
8874 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
8875 COMMON_INTERCEPT_FUNCTION(RMD160Data)
8876 #else
8877 #define INIT_RMD160
8878 #endif
8879
8880 #if SANITIZER_INTERCEPT_FSEEK
8881 INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
8882 void *ctx;
8883 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
8884 return REAL(fseek)(stream, offset, whence);
8885 }
8886 INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
8887 void *ctx;
8888 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
8889 return REAL(fseeko)(stream, offset, whence);
8890 }
8891 INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
8892 void *ctx;
8893 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
8894 return REAL(ftell)(stream);
8895 }
8896 INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
8897 void *ctx;
8898 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
8899 return REAL(ftello)(stream);
8900 }
8901 INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
8902 void *ctx;
8903 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
8904 return REAL(rewind)(stream);
8905 }
8906 INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
8907 void *ctx;
8908 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
8909 int ret = REAL(fgetpos)(stream, pos);
8910 if (pos && !ret)
8911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
8912 return ret;
8913 }
8914 INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
8915 void *ctx;
8916 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
8917 if (pos)
8918 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
8919 return REAL(fsetpos)(stream, pos);
8920 }
8921 #define INIT_FSEEK \
8922 COMMON_INTERCEPT_FUNCTION(fseek); \
8923 COMMON_INTERCEPT_FUNCTION(fseeko); \
8924 COMMON_INTERCEPT_FUNCTION(ftell); \
8925 COMMON_INTERCEPT_FUNCTION(ftello); \
8926 COMMON_INTERCEPT_FUNCTION(rewind); \
8927 COMMON_INTERCEPT_FUNCTION(fgetpos); \
8928 COMMON_INTERCEPT_FUNCTION(fsetpos)
8929 #else
8930 #define INIT_FSEEK
8931 #endif
8932
8933 #if SANITIZER_INTERCEPT_MD2
8934 INTERCEPTOR(void, MD2Init, void *context) {
8935 void *ctx;
8936 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
8937 REAL(MD2Init)(context);
8938 if (context)
8939 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8940 }
8941
8942 INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
8943 unsigned int len) {
8944 void *ctx;
8945 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
8946 if (data && len > 0)
8947 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8948 if (context)
8949 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8950 REAL(MD2Update)(context, data, len);
8951 if (context)
8952 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
8953 }
8954
8955 INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
8956 void *ctx;
8957 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
8958 if (context)
8959 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8960 REAL(MD2Final)(digest, context);
8961 if (digest)
8962 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8963 }
8964
8965 INTERCEPTOR(char *, MD2End, void *context, char *buf) {
8966 void *ctx;
8967 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
8968 if (context)
8969 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
8970 char *ret = REAL(MD2End)(context, buf);
8971 if (ret)
8972 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8973 return ret;
8974 }
8975
8976 INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
8977 void *ctx;
8978 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
8979 if (filename)
8980 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8981 char *ret = REAL(MD2File)(filename, buf);
8982 if (ret)
8983 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8984 return ret;
8985 }
8986
8987 INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
8988 char *buf) {
8989 void *ctx;
8990 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
8991 if (data && len > 0)
8992 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8993 char *ret = REAL(MD2Data)(data, len, buf);
8994 if (ret)
8995 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
8996 return ret;
8997 }
8998
8999 #define INIT_MD2 \
9000 COMMON_INTERCEPT_FUNCTION(MD2Init); \
9001 COMMON_INTERCEPT_FUNCTION(MD2Update); \
9002 COMMON_INTERCEPT_FUNCTION(MD2Final); \
9003 COMMON_INTERCEPT_FUNCTION(MD2End); \
9004 COMMON_INTERCEPT_FUNCTION(MD2File); \
9005 COMMON_INTERCEPT_FUNCTION(MD2Data)
9006 #else
9007 #define INIT_MD2
9008 #endif
9009
9010 #if SANITIZER_INTERCEPT_VIS
9011 INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
9012 void *ctx;
9013 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
9014 char *end = REAL(vis)(dst, c, flag, nextc);
9015 // dst is NULL terminated and end points to the NULL char
9016 if (dst && end)
9017 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9018 return end;
9019 }
9020 INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
9021 void *ctx;
9022 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
9023 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
9024 // nvis cannot make sure the dst is NULL terminated
9025 if (dst && end)
9026 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9027 return end;
9028 }
9029 INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
9030 void *ctx;
9031 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
9032 if (src)
9033 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9034 int len = REAL(strvis)(dst, src, flag);
9035 if (dst)
9036 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9037 return len;
9038 }
9039 INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
9040 void *ctx;
9041 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
9042 if (src)
9043 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9044 int len = REAL(stravis)(dst, src, flag);
9045 if (dst) {
9046 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
9047 if (*dst)
9048 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
9049 }
9050 return len;
9051 }
9052 INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
9053 void *ctx;
9054 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
9055 if (src)
9056 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9057 int len = REAL(strnvis)(dst, dlen, src, flag);
9058 // The interface will be valid even if there is no space for NULL char
9059 if (dst && len > 0)
9060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9061 return len;
9062 }
9063 INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
9064 void *ctx;
9065 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
9066 if (src)
9067 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9068 int ret = REAL(strvisx)(dst, src, len, flag);
9069 if (dst)
9070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9071 return ret;
9072 }
9073 INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9074 int flag) {
9075 void *ctx;
9076 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
9077 if (src)
9078 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9079 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
9080 if (dst && ret >= 0)
9081 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9082 return ret;
9083 }
9084 INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9085 int flag, int *cerr_ptr) {
9086 void *ctx;
9087 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
9088 if (src)
9089 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9090 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9091 // according to the implementation
9092 if (cerr_ptr)
9093 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9094 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
9095 if (dst && ret >= 0)
9096 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9097 if (cerr_ptr)
9098 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9099 return ret;
9100 }
9101 INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
9102 const char *extra) {
9103 void *ctx;
9104 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
9105 if (extra)
9106 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9107 char *end = REAL(svis)(dst, c, flag, nextc, extra);
9108 if (dst && end)
9109 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9110 return end;
9111 }
9112 INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
9113 const char *extra) {
9114 void *ctx;
9115 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
9116 if (extra)
9117 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9118 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
9119 if (dst && end)
9120 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
9121 Min((SIZE_T)(end - dst + 1), dlen));
9122 return end;
9123 }
9124 INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
9125 const char *extra) {
9126 void *ctx;
9127 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
9128 if (src)
9129 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9130 if (extra)
9131 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9132 int len = REAL(strsvis)(dst, src, flag, extra);
9133 if (dst)
9134 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9135 return len;
9136 }
9137 INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
9138 const char *extra) {
9139 void *ctx;
9140 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
9141 if (src)
9142 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9143 if (extra)
9144 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9145 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
9146 // The interface will be valid even if there is no space for NULL char
9147 if (dst && len >= 0)
9148 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9149 return len;
9150 }
9151 INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
9152 const char *extra) {
9153 void *ctx;
9154 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
9155 if (src)
9156 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9157 if (extra)
9158 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9159 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
9160 if (dst)
9161 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9162 return ret;
9163 }
9164 INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9165 int flag, const char *extra) {
9166 void *ctx;
9167 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
9168 if (src)
9169 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9170 if (extra)
9171 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9172 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
9173 if (dst && ret >= 0)
9174 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9175 return ret;
9176 }
9177 INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
9178 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
9179 void *ctx;
9180 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
9181 cerr_ptr);
9182 if (src)
9183 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9184 if (extra)
9185 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9186 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9187 // according to the implementation
9188 if (cerr_ptr)
9189 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9190 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
9191 if (dst && ret >= 0)
9192 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9193 if (cerr_ptr)
9194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9195 return ret;
9196 }
9197 INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
9198 void *ctx;
9199 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
9200 if (astate)
9201 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
9202 int ret = REAL(unvis)(cp, c, astate, flag);
9203 if (ret == unvis_valid || ret == unvis_validpush) {
9204 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
9205 }
9206 return ret;
9207 }
9208 INTERCEPTOR(int, strunvis, char *dst, const char *src) {
9209 void *ctx;
9210 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
9211 if (src)
9212 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9213 int ret = REAL(strunvis)(dst, src);
9214 if (ret != -1)
9215 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9216 return ret;
9217 }
9218 INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
9219 void *ctx;
9220 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
9221 if (src)
9222 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9223 int ret = REAL(strnunvis)(dst, dlen, src);
9224 if (ret != -1)
9225 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9226 return ret;
9227 }
9228 INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
9229 void *ctx;
9230 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
9231 if (src)
9232 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9233 int ret = REAL(strunvisx)(dst, src, flag);
9234 if (ret != -1)
9235 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9236 return ret;
9237 }
9238 INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
9239 int flag) {
9240 void *ctx;
9241 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
9242 if (src)
9243 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9244 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
9245 if (ret != -1)
9246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9247 return ret;
9248 }
9249 #define INIT_VIS \
9250 COMMON_INTERCEPT_FUNCTION(vis); \
9251 COMMON_INTERCEPT_FUNCTION(nvis); \
9252 COMMON_INTERCEPT_FUNCTION(strvis); \
9253 COMMON_INTERCEPT_FUNCTION(stravis); \
9254 COMMON_INTERCEPT_FUNCTION(strnvis); \
9255 COMMON_INTERCEPT_FUNCTION(strvisx); \
9256 COMMON_INTERCEPT_FUNCTION(strnvisx); \
9257 COMMON_INTERCEPT_FUNCTION(strenvisx); \
9258 COMMON_INTERCEPT_FUNCTION(svis); \
9259 COMMON_INTERCEPT_FUNCTION(snvis); \
9260 COMMON_INTERCEPT_FUNCTION(strsvis); \
9261 COMMON_INTERCEPT_FUNCTION(strsnvis); \
9262 COMMON_INTERCEPT_FUNCTION(strsvisx); \
9263 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
9264 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
9265 COMMON_INTERCEPT_FUNCTION(unvis); \
9266 COMMON_INTERCEPT_FUNCTION(strunvis); \
9267 COMMON_INTERCEPT_FUNCTION(strnunvis); \
9268 COMMON_INTERCEPT_FUNCTION(strunvisx); \
9269 COMMON_INTERCEPT_FUNCTION(strnunvisx)
9270 #else
9271 #define INIT_VIS
9272 #endif
9273
9274 #if SANITIZER_INTERCEPT_CDB
9275 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
9276 void *ctx;
9277 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
9278 if (path)
9279 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9280 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
9281 if (cdbr)
9282 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9283 return cdbr;
9284 }
9285
9286 INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
9287 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
9288 void *ctx;
9289 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
9290 cookie);
9291 if (base && size)
9292 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
9293 struct __sanitizer_cdbr *cdbr =
9294 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
9295 if (cdbr)
9296 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9297 return cdbr;
9298 }
9299
9300 INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9301 void *ctx;
9302 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9303 if (cdbr)
9304 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9305 return REAL(cdbr_entries)(cdbr);
9306 }
9307
9308 INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9309 const void **data, SIZE_T *datalen) {
9310 void *ctx;
9311 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9312 if (cdbr)
9313 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9314 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9315 if (!ret) {
9316 if (data)
9317 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9318 if (datalen)
9319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9320 if (data && datalen)
9321 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9322 }
9323 return ret;
9324 }
9325
9326 INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9327 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9328 void *ctx;
9329 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9330 if (cdbr)
9331 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9332 if (key)
9333 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9334 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9335 if (!ret) {
9336 if (data)
9337 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9338 if (datalen)
9339 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9340 if (data && datalen)
9341 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9342 }
9343 return ret;
9344 }
9345
9346 INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9347 void *ctx;
9348 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9349 if (cdbr)
9350 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9351 REAL(cdbr_close)(cdbr);
9352 }
9353
9354 INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9355 void *ctx;
9356 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9357 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9358 if (ret)
9359 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9360 return ret;
9361 }
9362
9363 INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9364 SIZE_T keylen, const void *data, SIZE_T datalen) {
9365 void *ctx;
9366 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9367 if (cdbw)
9368 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9369 if (data && datalen)
9370 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9371 if (key && keylen)
9372 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9373 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9374 if (!ret && cdbw)
9375 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9376 return ret;
9377 }
9378
9379 INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9380 SIZE_T datalen, u32 *index) {
9381 void *ctx;
9382 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9383 if (cdbw)
9384 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9385 if (data && datalen)
9386 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9387 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9388 if (!ret) {
9389 if (index)
9390 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9391 if (cdbw)
9392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9393 }
9394 return ret;
9395 }
9396
9397 INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9398 SIZE_T keylen, u32 index) {
9399 void *ctx;
9400 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9401 if (cdbw)
9402 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9403 if (key && keylen)
9404 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9405 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9406 if (!ret && cdbw)
9407 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9408 return ret;
9409 }
9410
9411 INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9412 const char descr[16], u32 (*seedgen)(void)) {
9413 void *ctx;
9414 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9415 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9416 if (cdbw)
9417 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9418 if (descr)
9419 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9420 if (seedgen)
9421 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9422 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9423 if (!ret) {
9424 if (cdbw)
9425 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9426 if (output >= 0)
9427 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9428 }
9429 return ret;
9430 }
9431
9432 INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9433 void *ctx;
9434 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9435 if (cdbw)
9436 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9437 REAL(cdbw_close)(cdbw);
9438 }
9439
9440 #define INIT_CDB \
9441 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9442 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9443 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9444 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9445 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9446 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9447 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9448 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9449 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9450 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9451 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9452 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9453 #else
9454 #define INIT_CDB
9455 #endif
9456
9457 #if SANITIZER_INTERCEPT_GETFSENT
9458 INTERCEPTOR(void *, getfsent) {
9459 void *ctx;
9460 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9461 void *ret = REAL(getfsent)();
9462 if (ret)
9463 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9464 return ret;
9465 }
9466
9467 INTERCEPTOR(void *, getfsspec, const char *spec) {
9468 void *ctx;
9469 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9470 if (spec)
9471 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
9472 void *ret = REAL(getfsspec)(spec);
9473 if (ret)
9474 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9475 return ret;
9476 }
9477
9478 INTERCEPTOR(void *, getfsfile, const char *file) {
9479 void *ctx;
9480 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9481 if (file)
9482 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
9483 void *ret = REAL(getfsfile)(file);
9484 if (ret)
9485 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9486 return ret;
9487 }
9488
9489 #define INIT_GETFSENT \
9490 COMMON_INTERCEPT_FUNCTION(getfsent); \
9491 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9492 COMMON_INTERCEPT_FUNCTION(getfsfile);
9493 #else
9494 #define INIT_GETFSENT
9495 #endif
9496
9497 #if SANITIZER_INTERCEPT_ARC4RANDOM
9498 INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9499 void *ctx;
9500 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9501 REAL(arc4random_buf)(buf, len);
9502 if (buf && len)
9503 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9504 }
9505
9506 INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9507 void *ctx;
9508 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9509 if (dat && datlen)
9510 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9511 REAL(arc4random_addrandom)(dat, datlen);
9512 }
9513
9514 #define INIT_ARC4RANDOM \
9515 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9516 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9517 #else
9518 #define INIT_ARC4RANDOM
9519 #endif
9520
9521 #if SANITIZER_INTERCEPT_POPEN
9522 INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9523 void *ctx;
9524 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9525 if (command)
9526 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
9527 if (type)
9528 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9529 __sanitizer_FILE *res = REAL(popen)(command, type);
9530 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9531 if (res) unpoison_file(res);
9532 return res;
9533 }
9534 #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9535 #else
9536 #define INIT_POPEN
9537 #endif
9538
9539 #if SANITIZER_INTERCEPT_POPENVE
9540 INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9541 char *const *argv, char *const *envp, const char *type) {
9542 void *ctx;
9543 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9544 if (path)
9545 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9546 if (argv) {
9547 for (char *const *pa = argv; ; ++pa) {
9548 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9549 if (!*pa)
9550 break;
9551 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9552 }
9553 }
9554 if (envp) {
9555 for (char *const *pa = envp; ; ++pa) {
9556 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9557 if (!*pa)
9558 break;
9559 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9560 }
9561 }
9562 if (type)
9563 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9564 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9565 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9566 if (res) unpoison_file(res);
9567 return res;
9568 }
9569 #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9570 #else
9571 #define INIT_POPENVE
9572 #endif
9573
9574 #if SANITIZER_INTERCEPT_PCLOSE
9575 INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9576 void *ctx;
9577 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9578 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9579 const FileMetadata *m = GetInterceptorMetadata(fp);
9580 int res = REAL(pclose)(fp);
9581 if (m) {
9582 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9583 DeleteInterceptorMetadata(fp);
9584 }
9585 return res;
9586 }
9587 #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9588 #else
9589 #define INIT_PCLOSE
9590 #endif
9591
9592 #if SANITIZER_INTERCEPT_FUNOPEN
9593 typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9594 typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9595 typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9596 typedef int (*funopen_closefn)(void *cookie);
9597
9598 struct WrappedFunopenCookie {
9599 void *real_cookie;
9600 funopen_readfn real_read;
9601 funopen_writefn real_write;
9602 funopen_seekfn real_seek;
9603 funopen_closefn real_close;
9604 };
9605
9606 static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9607 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9608 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9609 funopen_readfn real_read = wrapped_cookie->real_read;
9610 return real_read(wrapped_cookie->real_cookie, buf, len);
9611 }
9612
9613 static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9614 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9615 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9616 funopen_writefn real_write = wrapped_cookie->real_write;
9617 return real_write(wrapped_cookie->real_cookie, buf, len);
9618 }
9619
9620 static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9621 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9622 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9623 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9624 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9625 }
9626
9627 static int wrapped_funopen_close(void *cookie) {
9628 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9629 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9630 funopen_closefn real_close = wrapped_cookie->real_close;
9631 int res = real_close(wrapped_cookie->real_cookie);
9632 InternalFree(wrapped_cookie);
9633 return res;
9634 }
9635
9636 INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9637 funopen_writefn writefn, funopen_seekfn seekfn,
9638 funopen_closefn closefn) {
9639 void *ctx;
9640 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9641 closefn);
9642
9643 WrappedFunopenCookie *wrapped_cookie =
9644 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9645 wrapped_cookie->real_cookie = cookie;
9646 wrapped_cookie->real_read = readfn;
9647 wrapped_cookie->real_write = writefn;
9648 wrapped_cookie->real_seek = seekfn;
9649 wrapped_cookie->real_close = closefn;
9650
9651 __sanitizer_FILE *res =
9652 REAL(funopen)(wrapped_cookie,
9653 readfn ? wrapped_funopen_read : nullptr,
9654 writefn ? wrapped_funopen_write : nullptr,
9655 seekfn ? wrapped_funopen_seek : nullptr,
9656 closefn ? wrapped_funopen_close : nullptr);
9657 if (res)
9658 unpoison_file(res);
9659 return res;
9660 }
9661 #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9662 #else
9663 #define INIT_FUNOPEN
9664 #endif
9665
9666 #if SANITIZER_INTERCEPT_FUNOPEN2
9667 typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9668 typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9669 typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9670 typedef int (*funopen2_flushfn)(void *cookie);
9671 typedef int (*funopen2_closefn)(void *cookie);
9672
9673 struct WrappedFunopen2Cookie {
9674 void *real_cookie;
9675 funopen2_readfn real_read;
9676 funopen2_writefn real_write;
9677 funopen2_seekfn real_seek;
9678 funopen2_flushfn real_flush;
9679 funopen2_closefn real_close;
9680 };
9681
9682 static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9683 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9684 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9685 funopen2_readfn real_read = wrapped_cookie->real_read;
9686 return real_read(wrapped_cookie->real_cookie, buf, len);
9687 }
9688
9689 static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9690 SIZE_T len) {
9691 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9692 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9693 funopen2_writefn real_write = wrapped_cookie->real_write;
9694 return real_write(wrapped_cookie->real_cookie, buf, len);
9695 }
9696
9697 static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9698 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9699 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9700 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9701 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9702 }
9703
9704 static int wrapped_funopen2_flush(void *cookie) {
9705 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9706 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9707 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9708 return real_flush(wrapped_cookie->real_cookie);
9709 }
9710
9711 static int wrapped_funopen2_close(void *cookie) {
9712 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9713 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9714 funopen2_closefn real_close = wrapped_cookie->real_close;
9715 int res = real_close(wrapped_cookie->real_cookie);
9716 InternalFree(wrapped_cookie);
9717 return res;
9718 }
9719
9720 INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9721 funopen2_writefn writefn, funopen2_seekfn seekfn,
9722 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9723 void *ctx;
9724 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9725 flushfn, closefn);
9726
9727 WrappedFunopen2Cookie *wrapped_cookie =
9728 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9729 wrapped_cookie->real_cookie = cookie;
9730 wrapped_cookie->real_read = readfn;
9731 wrapped_cookie->real_write = writefn;
9732 wrapped_cookie->real_seek = seekfn;
9733 wrapped_cookie->real_flush = flushfn;
9734 wrapped_cookie->real_close = closefn;
9735
9736 __sanitizer_FILE *res =
9737 REAL(funopen2)(wrapped_cookie,
9738 readfn ? wrapped_funopen2_read : nullptr,
9739 writefn ? wrapped_funopen2_write : nullptr,
9740 seekfn ? wrapped_funopen2_seek : nullptr,
9741 flushfn ? wrapped_funopen2_flush : nullptr,
9742 closefn ? wrapped_funopen2_close : nullptr);
9743 if (res)
9744 unpoison_file(res);
9745 return res;
9746 }
9747 #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9748 #else
9749 #define INIT_FUNOPEN2
9750 #endif
9751
9752 #if SANITIZER_INTERCEPT_FDEVNAME
9753 INTERCEPTOR(char *, fdevname, int fd) {
9754 void *ctx;
9755 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9756 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9757 char *name = REAL(fdevname)(fd);
9758 if (name) {
9759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
9760 if (fd > 0)
9761 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9762 }
9763 return name;
9764 }
9765
9766 INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9767 void *ctx;
9768 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9769 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9770 char *name = REAL(fdevname_r)(fd, buf, len);
9771 if (name && buf && len > 0) {
9772 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
9773 if (fd > 0)
9774 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9775 }
9776 return name;
9777 }
9778
9779 #define INIT_FDEVNAME \
9780 COMMON_INTERCEPT_FUNCTION(fdevname); \
9781 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9782 #else
9783 #define INIT_FDEVNAME
9784 #endif
9785
9786 #if SANITIZER_INTERCEPT_GETUSERSHELL
9787 INTERCEPTOR(char *, getusershell,) {
9788 void *ctx;
9789 COMMON_INTERCEPTOR_ENTER(ctx, getusershell,);
9790 char *res = REAL(getusershell)();
9791 if (res)
9792 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9793 return res;
9794 }
9795
9796 #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9797 #else
9798 #define INIT_GETUSERSHELL
9799 #endif
9800
9801 #if SANITIZER_INTERCEPT_SL_INIT
9802 INTERCEPTOR(void *, sl_init) {
9803 void *ctx;
9804 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9805 void *res = REAL(sl_init)();
9806 if (res)
9807 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
9808 return res;
9809 }
9810
9811 INTERCEPTOR(int, sl_add, void *sl, char *item) {
9812 void *ctx;
9813 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
9814 if (sl)
9815 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9816 if (item)
9817 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9818 int res = REAL(sl_add)(sl, item);
9819 if (!res)
9820 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9821 return res;
9822 }
9823
9824 INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
9825 void *ctx;
9826 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
9827 if (sl)
9828 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9829 if (item)
9830 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
9831 char *res = REAL(sl_find)(sl, item);
9832 if (res)
9833 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9834 return res;
9835 }
9836
9837 INTERCEPTOR(void, sl_free, void *sl, int freeall) {
9838 void *ctx;
9839 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
9840 if (sl)
9841 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
9842 REAL(sl_free)(sl, freeall);
9843 }
9844
9845 #define INIT_SL_INIT \
9846 COMMON_INTERCEPT_FUNCTION(sl_init); \
9847 COMMON_INTERCEPT_FUNCTION(sl_add); \
9848 COMMON_INTERCEPT_FUNCTION(sl_find); \
9849 COMMON_INTERCEPT_FUNCTION(sl_free);
9850 #else
9851 #define INIT_SL_INIT
9852 #endif
9853
9854 #if SANITIZER_INTERCEPT_GETRANDOM
9855 INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
9856 void *ctx;
9857 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
9858 // If GRND_NONBLOCK is set in the flags, it is non blocking.
9859 static const int grnd_nonblock = 1;
9860 SSIZE_T n;
9861 if ((flags & grnd_nonblock))
9862 n = REAL(getrandom)(buf, buflen, flags);
9863 else
9864 n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
9865 if (n > 0) {
9866 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
9867 }
9868 return n;
9869 }
9870 #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
9871 #else
9872 #define INIT_GETRANDOM
9873 #endif
9874
9875 #if SANITIZER_INTERCEPT_GETENTROPY
9876 INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
9877 void *ctx;
9878 COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
9879 int r = REAL(getentropy)(buf, buflen);
9880 if (r == 0) {
9881 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
9882 }
9883 return r;
9884 }
9885 #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
9886 #else
9887 #define INIT_GETENTROPY
9888 #endif
9889
9890 #if SANITIZER_INTERCEPT_QSORT_R
9891 typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
9892 struct qsort_r_compar_params {
9893 SIZE_T size;
9894 qsort_r_compar_f compar;
9895 void *arg;
9896 };
9897 static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
9898 qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
9899 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9900 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
9901 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
9902 return params->compar(a, b, params->arg);
9903 }
9904
9905 INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
9906 qsort_r_compar_f compar, void *arg) {
9907 void *ctx;
9908 COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
9909 // Run the comparator over all array elements to detect any memory issues.
9910 if (nmemb > 1) {
9911 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
9912 void *p = (void *)((char *)base + i * size);
9913 void *q = (void *)((char *)base + (i + 1) * size);
9914 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9915 compar(p, q, arg);
9916 }
9917 }
9918 qsort_r_compar_params params = {size, compar, arg};
9919 REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, &params);
9920 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
9921 }
9922 # define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
9923 #else
9924 # define INIT_QSORT_R
9925 #endif
9926
9927 #if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
9928 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
9929 qsort_r_compar_f compar) {
9930 void *ctx;
9931 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
9932 WRAP(qsort_r)(base, nmemb, size, compar, nullptr);
9933 }
9934 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
9935 #elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
9936 // Glibc qsort uses a temporary buffer allocated either on stack or on heap.
9937 // Poisoned memory from there may get copied into the comparator arguments,
9938 // where it needs to be dealt with. But even that is not enough - the results of
9939 // the sort may be copied into the input/output array based on the results of
9940 // the comparator calls, but directly from the temp memory, bypassing the
9941 // unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
9942 // unpoisoning the entire array after the sort is done.
9943 //
9944 // We can not check that the entire array is initialized at the beginning. IMHO,
9945 // it's fine for parts of the sorted objects to contain uninitialized memory,
9946 // ex. as padding in structs.
9947 typedef int (*qsort_compar_f)(const void *, const void *);
9948 static THREADLOCAL qsort_compar_f qsort_compar;
9949 static THREADLOCAL SIZE_T qsort_size;
9950 static int wrapped_qsort_compar(const void *a, const void *b) {
9951 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
9952 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
9953 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
9954 return qsort_compar(a, b);
9955 }
9956
9957 INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
9958 qsort_compar_f compar) {
9959 void *ctx;
9960 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
9961 // Run the comparator over all array elements to detect any memory issues.
9962 if (nmemb > 1) {
9963 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
9964 void *p = (void *)((char *)base + i * size);
9965 void *q = (void *)((char *)base + (i + 1) * size);
9966 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
9967 compar(p, q);
9968 }
9969 }
9970 qsort_compar_f old_compar = qsort_compar;
9971 SIZE_T old_size = qsort_size;
9972 // Handle qsort() implementations that recurse using an
9973 // interposable function call:
9974 bool already_wrapped = compar == wrapped_qsort_compar;
9975 if (already_wrapped) {
9976 // This case should only happen if the qsort() implementation calls itself
9977 // using a preemptible function call (e.g. the FreeBSD libc version).
9978 // Check that the size and comparator arguments are as expected.
9979 CHECK_NE(compar, qsort_compar);
9980 CHECK_EQ(qsort_size, size);
9981 } else {
9982 qsort_compar = compar;
9983 qsort_size = size;
9984 }
9985 REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
9986 if (!already_wrapped) {
9987 qsort_compar = old_compar;
9988 qsort_size = old_size;
9989 }
9990 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
9991 }
9992 # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
9993 #else
9994 # define INIT_QSORT
9995 #endif
9996
9997 #if SANITIZER_INTERCEPT_BSEARCH
9998 typedef int (*bsearch_compar_f)(const void *, const void *);
9999 struct bsearch_compar_params {
10000 const void *key;
10001 bsearch_compar_f compar;
10002 };
10003
10004 static int wrapped_bsearch_compar(const void *key, const void *b) {
10005 const bsearch_compar_params *params = (const bsearch_compar_params *)key;
10006 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10007 return params->compar(params->key, b);
10008 }
10009
10010 INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
10011 SIZE_T size, bsearch_compar_f compar) {
10012 void *ctx;
10013 COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
10014 bsearch_compar_params params = {key, compar};
10015 return REAL(bsearch)(&params, base, nmemb, size, wrapped_bsearch_compar);
10016 }
10017 # define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
10018 #else
10019 # define INIT_BSEARCH
10020 #endif
10021
10022 #if SANITIZER_INTERCEPT_SIGALTSTACK
10023 INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
10024 void *ctx;
10025 COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
10026 int r = REAL(sigaltstack)(ss, oss);
10027 if (r == 0 && oss != nullptr) {
10028 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
10029 }
10030 return r;
10031 }
10032 #define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
10033 #else
10034 #define INIT_SIGALTSTACK
10035 #endif
10036
10037 #if SANITIZER_INTERCEPT_PROCCTL
10038 INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
10039 void *ctx;
10040 COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
10041 static const int PROC_REAP_ACQUIRE = 2;
10042 static const int PROC_REAP_RELEASE = 3;
10043 static const int PROC_REAP_STATUS = 4;
10044 static const int PROC_REAP_GETPIDS = 5;
10045 static const int PROC_REAP_KILL = 6;
10046 if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
10047 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
10048 } else {
10049 // reap_acquire/reap_release bears no arguments.
10050 if (cmd > PROC_REAP_RELEASE) {
10051 unsigned int reapsz;
10052 switch (cmd) {
10053 case PROC_REAP_STATUS:
10054 reapsz = struct_procctl_reaper_status_sz;
10055 break;
10056 case PROC_REAP_GETPIDS:
10057 reapsz = struct_procctl_reaper_pids_sz;
10058 break;
10059 case PROC_REAP_KILL:
10060 reapsz = struct_procctl_reaper_kill_sz;
10061 break;
10062 }
10063 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
10064 }
10065 }
10066 return REAL(procctl)(idtype, id, cmd, data);
10067 }
10068 #define INIT_PROCCTL COMMON_INTERCEPT_FUNCTION(procctl)
10069 #else
10070 #define INIT_PROCCTL
10071 #endif
10072
10073 #if SANITIZER_INTERCEPT_UNAME
10074 INTERCEPTOR(int, uname, struct utsname *utsname) {
10075 #if SANITIZER_LINUX
10076 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
10077 return internal_uname(utsname);
10078 #endif
10079 void *ctx;
10080 COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
10081 int res = REAL(uname)(utsname);
10082 if (!res)
10083 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10084 __sanitizer::struct_utsname_sz);
10085 return res;
10086 }
10087 #define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
10088 #else
10089 #define INIT_UNAME
10090 #endif
10091
10092 #if SANITIZER_INTERCEPT___XUNAME
10093 // FreeBSD's <sys/utsname.h> define uname() as
10094 // static __inline int uname(struct utsname *name) {
10095 // return __xuname(SYS_NMLN, (void*)name);
10096 // }
10097 INTERCEPTOR(int, __xuname, int size, void *utsname) {
10098 void *ctx;
10099 COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
10100 int res = REAL(__xuname)(size, utsname);
10101 if (!res)
10102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10103 __sanitizer::struct_utsname_sz);
10104 return res;
10105 }
10106 #define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
10107 #else
10108 #define INIT___XUNAME
10109 #endif
10110
10111 #if SANITIZER_INTERCEPT_ARGP_PARSE
10112 INTERCEPTOR(int, argp_parse, const struct argp *argp, int argc, char **argv,
10113 unsigned flags, int *arg_index, void *input) {
10114 void *ctx;
10115 COMMON_INTERCEPTOR_ENTER(ctx, argp_parse, argp, argc, argv, flags, arg_index,
10116 input);
10117 for (int i = 0; i < argc; i++)
10118 COMMON_INTERCEPTOR_READ_RANGE(ctx, argv[i], internal_strlen(argv[i]) + 1);
10119 int res = REAL(argp_parse)(argp, argc, argv, flags, arg_index, input);
10120 if (!res && arg_index)
10121 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg_index, sizeof(int));
10122 return res;
10123 }
10124
10125 #define INIT_ARGP_PARSE COMMON_INTERCEPT_FUNCTION(argp_parse);
10126 #else
10127 #define INIT_ARGP_PARSE
10128 #endif
10129
10130 #if SANITIZER_INTERCEPT_CPUSET_GETAFFINITY
10131 INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T cpusetsize, __sanitizer_cpuset_t *mask) {
10132 void *ctx;
10133 COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getaffinity, level, which, id, cpusetsize, mask);
10134 int res = REAL(cpuset_getaffinity)(level, which, id, cpusetsize, mask);
10135 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
10136 return res;
10137 }
10138 #define INIT_CPUSET_GETAFFINITY COMMON_INTERCEPT_FUNCTION(cpuset_getaffinity);
10139 #else
10140 #define INIT_CPUSET_GETAFFINITY
10141 #endif
10142
10143 #if SANITIZER_INTERCEPT_PREADV2
10144 INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
10145 OFF_T offset, int flags) {
10146 void *ctx;
10147 COMMON_INTERCEPTOR_ENTER(ctx, preadv2, fd, iov, iovcnt, offset, flags);
10148 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10149 SSIZE_T res = REAL(preadv2)(fd, iov, iovcnt, offset, flags);
10150 if (res > 0) write_iovec(ctx, iov, iovcnt, res);
10151 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10152 return res;
10153 }
10154 #define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
10155 #else
10156 #define INIT_PREADV2
10157 #endif
10158
10159 #if SANITIZER_INTERCEPT_PWRITEV2
10160 INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
10161 OFF_T offset, int flags) {
10162 void *ctx;
10163 COMMON_INTERCEPTOR_ENTER(ctx, pwritev2, fd, iov, iovcnt, offset, flags);
10164 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10165 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
10166 SSIZE_T res = REAL(pwritev2)(fd, iov, iovcnt, offset, flags);
10167 if (res > 0) read_iovec(ctx, iov, iovcnt, res);
10168 return res;
10169 }
10170 #define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
10171 #else
10172 #define INIT_PWRITEV2
10173 #endif
10174
10175 #if SANITIZER_INTERCEPT_FREADLINK
10176 INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) {
10177 void *ctx;
10178 COMMON_INTERCEPTOR_ENTER(ctx, freadlink, fd, buf, bufsiz);
10179 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10180 SSIZE_T res = REAL(freadlink)(fd, buf, bufsiz);
10181 if (res > 0)
10182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
10183 if (res >= 0 && fd > 0)
10184 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10185 return res;
10186 }
10187 # define INIT_FREADLINK COMMON_INTERCEPT_FUNCTION(freadlink)
10188 #else
10189 # define INIT_FREADLINK
10190 #endif
10191
10192 #include "sanitizer_common_interceptors_netbsd_compat.inc"
10193
10194 namespace __sanitizer {
10195 void InitializeMemintrinsicInterceptors();
10196 } // namespace __sanitizer
10197
10198 static void InitializeCommonInterceptors() {
10199 #if SI_POSIX
10200 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
10201 interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
10202 #endif
10203
10204 __sanitizer::InitializeMemintrinsicInterceptors();
10205
10206 INIT_MMAP;
10207 INIT_MMAP64;
10208 INIT_TEXTDOMAIN;
10209 INIT_STRLEN;
10210 INIT_STRNLEN;
10211 INIT_STRNDUP;
10212 INIT___STRNDUP;
10213 INIT_STRCMP;
10214 INIT_STRNCMP;
10215 INIT_STRCASECMP;
10216 INIT_STRNCASECMP;
10217 INIT_STRSTR;
10218 INIT_STRCASESTR;
10219 INIT_STRCHR;
10220 INIT_STRCHRNUL;
10221 INIT_STRRCHR;
10222 INIT_STRSPN;
10223 INIT_STRTOK;
10224 INIT_STRPBRK;
10225 INIT_STRXFRM;
10226 INIT___STRXFRM_L;
10227 INIT_MEMCHR;
10228 INIT_MEMCMP;
10229 INIT_BCMP;
10230 INIT_MEMRCHR;
10231 INIT_MEMMEM;
10232 INIT_READ;
10233 INIT_FREAD;
10234 INIT_PREAD;
10235 INIT_PREAD64;
10236 INIT_READV;
10237 INIT_PREADV;
10238 INIT_PREADV64;
10239 INIT_WRITE;
10240 INIT_FWRITE;
10241 INIT_PWRITE;
10242 INIT_PWRITE64;
10243 INIT_WRITEV;
10244 INIT_PWRITEV;
10245 INIT_PWRITEV64;
10246 INIT_FGETS;
10247 INIT_FPUTS;
10248 INIT_PUTS;
10249 INIT_PRCTL;
10250 INIT_LOCALTIME_AND_FRIENDS;
10251 INIT_STRPTIME;
10252 INIT_SCANF;
10253 INIT_ISOC99_SCANF;
10254 INIT_PRINTF;
10255 INIT_PRINTF_L;
10256 INIT_ISOC99_PRINTF;
10257 INIT_FREXP;
10258 INIT_FREXPF_FREXPL;
10259 INIT_GETPWNAM_AND_FRIENDS;
10260 INIT_GETPWNAM_R_AND_FRIENDS;
10261 INIT_GETPWENT;
10262 INIT_FGETPWENT;
10263 INIT_GETPWENT_R;
10264 INIT_FGETPWENT_R;
10265 INIT_FGETGRENT_R;
10266 INIT_SETPWENT;
10267 INIT_CLOCK_GETTIME;
10268 INIT_CLOCK_GETCPUCLOCKID;
10269 INIT_GETITIMER;
10270 INIT_TIME;
10271 INIT_GLOB;
10272 INIT_GLOB64;
10273 INIT___B64_TO;
10274 INIT_DN_COMP_EXPAND;
10275 INIT_POSIX_SPAWN;
10276 INIT_WAIT;
10277 INIT_WAIT4;
10278 INIT_INET;
10279 INIT_PTHREAD_GETSCHEDPARAM;
10280 INIT_GETADDRINFO;
10281 INIT_GETNAMEINFO;
10282 INIT_GETSOCKNAME;
10283 INIT_GETHOSTBYNAME;
10284 INIT_GETHOSTBYNAME2;
10285 INIT_GETHOSTBYNAME_R;
10286 INIT_GETHOSTBYNAME2_R;
10287 INIT_GETHOSTBYADDR_R;
10288 INIT_GETHOSTENT_R;
10289 INIT_GETSOCKOPT;
10290 INIT_ACCEPT;
10291 INIT_ACCEPT4;
10292 INIT_PACCEPT;
10293 INIT_MODF;
10294 INIT_RECVMSG;
10295 INIT_SENDMSG;
10296 INIT_RECVMMSG;
10297 INIT_SENDMMSG;
10298 INIT_SYSMSG;
10299 INIT_GETPEERNAME;
10300 INIT_IOCTL;
10301 INIT_INET_ATON;
10302 INIT_SYSINFO;
10303 INIT_READDIR;
10304 INIT_READDIR64;
10305 INIT_PTRACE;
10306 INIT_SETLOCALE;
10307 INIT_GETCWD;
10308 INIT_GET_CURRENT_DIR_NAME;
10309 INIT_STRTOIMAX;
10310 INIT_STRTOIMAX_C23;
10311 INIT_MBSTOWCS;
10312 INIT_MBSNRTOWCS;
10313 INIT_WCSTOMBS;
10314 INIT_WCSNRTOMBS;
10315 INIT_WCRTOMB;
10316 INIT_WCTOMB;
10317 INIT_TCGETATTR;
10318 INIT_REALPATH;
10319 INIT_CANONICALIZE_FILE_NAME;
10320 INIT_CONFSTR;
10321 INIT_SCHED_GETAFFINITY;
10322 INIT_SCHED_GETPARAM;
10323 INIT_STRERROR;
10324 INIT_STRERROR_R;
10325 INIT_XPG_STRERROR_R;
10326 INIT_SCANDIR;
10327 INIT_SCANDIR64;
10328 INIT_GETGROUPS;
10329 INIT_POLL;
10330 INIT_PPOLL;
10331 INIT_WORDEXP;
10332 INIT_SIGWAIT;
10333 INIT_SIGWAITINFO;
10334 INIT_SIGTIMEDWAIT;
10335 INIT_SIGSETOPS;
10336 INIT_SIGSET_LOGICOPS;
10337 INIT_SIGPENDING;
10338 INIT_SIGPROCMASK;
10339 INIT_PTHREAD_SIGMASK;
10340 INIT_BACKTRACE;
10341 INIT__EXIT;
10342 INIT___LIBC_THR_SETCANCELSTATE;
10343 INIT_GETMNTENT;
10344 INIT_GETMNTENT_R;
10345 INIT_STATFS;
10346 INIT_STATFS64;
10347 INIT_STATVFS;
10348 INIT_STATVFS64;
10349 INIT_INITGROUPS;
10350 INIT_ETHER_NTOA_ATON;
10351 INIT_ETHER_HOST;
10352 INIT_ETHER_R;
10353 INIT_SHMCTL;
10354 INIT_RANDOM_R;
10355 INIT_PTHREAD_ATTR_GET;
10356 INIT_PTHREAD_ATTR_GET_SCHED;
10357 INIT_PTHREAD_ATTR_GETINHERITSCHED;
10358 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
10359 INIT_PTHREAD_GETAFFINITY_NP;
10360 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
10361 INIT_PTHREAD_MUTEXATTR_GETTYPE;
10362 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
10363 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
10364 INIT_PTHREAD_MUTEXATTR_GETROBUST;
10365 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
10366 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
10367 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
10368 INIT_PTHREAD_CONDATTR_GETPSHARED;
10369 INIT_PTHREAD_CONDATTR_GETCLOCK;
10370 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
10371 INIT_TMPNAM;
10372 INIT_TMPNAM_R;
10373 INIT_PTSNAME;
10374 INIT_PTSNAME_R;
10375 INIT_TTYNAME;
10376 INIT_TTYNAME_R;
10377 INIT_TEMPNAM;
10378 INIT_PTHREAD_SETNAME_NP;
10379 INIT_PTHREAD_GETNAME_NP;
10380 INIT_SINCOS;
10381 INIT_REMQUO;
10382 INIT_REMQUOL;
10383 INIT_LGAMMA;
10384 INIT_LGAMMAL;
10385 INIT_LGAMMA_R;
10386 INIT_LGAMMAL_R;
10387 INIT_DRAND48_R;
10388 INIT_RAND_R;
10389 INIT_GETLINE;
10390 INIT_ICONV;
10391 INIT_TIMES;
10392 INIT_TLS_GET_ADDR;
10393 INIT_LISTXATTR;
10394 INIT_GETXATTR;
10395 INIT_GETRESID;
10396 INIT_GETIFADDRS;
10397 INIT_IF_INDEXTONAME;
10398 INIT_CAPGET;
10399 INIT_FTIME;
10400 INIT_XDR;
10401 INIT_XDRREC_LINUX;
10402 INIT_TSEARCH;
10403 INIT_LIBIO_INTERNALS;
10404 INIT_FOPEN;
10405 INIT_FOPEN64;
10406 INIT_FLOPEN;
10407 INIT_OPEN_MEMSTREAM;
10408 INIT_OBSTACK;
10409 INIT_FFLUSH;
10410 INIT_FCLOSE;
10411 INIT_DLOPEN_DLCLOSE;
10412 INIT_GETPASS;
10413 INIT_TIMERFD;
10414 INIT_MLOCKX;
10415 INIT_FOPENCOOKIE;
10416 INIT_SEM;
10417 INIT_PTHREAD_SETCANCEL;
10418 INIT_MINCORE;
10419 INIT_PROCESS_VM_READV;
10420 INIT_CTERMID;
10421 INIT_CTERMID_R;
10422 INIT_RECV_RECVFROM;
10423 INIT_SEND_SENDTO;
10424 INIT_STAT;
10425 INIT_STAT64;
10426 INIT_EVENTFD_READ_WRITE;
10427 INIT_LSTAT;
10428 INIT_LSTAT64;
10429 INIT___XSTAT;
10430 INIT___XSTAT64;
10431 INIT___LXSTAT;
10432 INIT___LXSTAT64;
10433 // FIXME: add other *stat interceptors.
10434 INIT_UTMP;
10435 INIT_UTMPX;
10436 INIT_GETLOADAVG;
10437 INIT_WCSLEN;
10438 INIT_WCSCAT;
10439 INIT_WCSDUP;
10440 INIT_WCSXFRM;
10441 INIT___WCSXFRM_L;
10442 INIT_ACCT;
10443 INIT_USER_FROM_UID;
10444 INIT_UID_FROM_USER;
10445 INIT_GROUP_FROM_GID;
10446 INIT_GID_FROM_GROUP;
10447 INIT_ACCESS;
10448 INIT_FACCESSAT;
10449 INIT_GETGROUPLIST;
10450 INIT_GETGROUPMEMBERSHIP;
10451 INIT_READLINK;
10452 INIT_READLINKAT;
10453 INIT_NAME_TO_HANDLE_AT;
10454 INIT_OPEN_BY_HANDLE_AT;
10455 INIT_STRLCPY;
10456 INIT_DEVNAME;
10457 INIT_DEVNAME_R;
10458 INIT_FGETLN;
10459 INIT_STRMODE;
10460 INIT_TTYENT;
10461 INIT_PROTOENT;
10462 INIT_PROTOENT_R;
10463 INIT_NETENT;
10464 INIT_GETMNTINFO;
10465 INIT_MI_VECTOR_HASH;
10466 INIT_SETVBUF;
10467 INIT_GETVFSSTAT;
10468 INIT_REGEX;
10469 INIT_REGEXSUB;
10470 INIT_FTS;
10471 INIT_SYSCTL;
10472 INIT_ASYSCTL;
10473 INIT_SYSCTLGETMIBINFO;
10474 INIT_NL_LANGINFO;
10475 INIT_MODCTL;
10476 INIT_STRTONUM;
10477 INIT_FPARSELN;
10478 INIT_STATVFS1;
10479 INIT_STRTOI;
10480 INIT_CAPSICUM;
10481 INIT_SHA1;
10482 INIT_MD4;
10483 INIT_RMD160;
10484 INIT_FSEEK;
10485 INIT_MD2;
10486 INIT_VIS;
10487 INIT_CDB;
10488 INIT_GETFSENT;
10489 INIT_ARC4RANDOM;
10490 INIT_POPEN;
10491 INIT_POPENVE;
10492 INIT_PCLOSE;
10493 INIT_FUNOPEN;
10494 INIT_FUNOPEN2;
10495 INIT_FDEVNAME;
10496 INIT_GETUSERSHELL;
10497 INIT_SL_INIT;
10498 INIT_GETRANDOM;
10499 INIT_GETENTROPY;
10500 INIT_QSORT;
10501 INIT_QSORT_R;
10502 INIT_BSEARCH;
10503 INIT_SIGALTSTACK;
10504 INIT_PROCCTL
10505 INIT_UNAME;
10506 INIT___XUNAME;
10507 INIT_ARGP_PARSE;
10508 INIT_CPUSET_GETAFFINITY;
10509 INIT_PREADV2;
10510 INIT_PWRITEV2;
10511 INIT_FREADLINK;
10512
10513 INIT___PRINTF_CHK;
10514 }