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