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