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