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 COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
2976 // FIXME: under ASan the call below may write to freed memory and corrupt
2977 // its metadata. See
2978 // https://github.com/google/sanitizers/issues/321.
2979 int res = REAL(getaddrinfo)(node, service, hints, out);
2980 if (res == 0 && out) {
2981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
2982 struct __sanitizer_addrinfo *p = *out;
2983 while (p) {
2984 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
2985 if (p->ai_addr)
2986 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
2987 if (p->ai_canonname)
2988 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
2989 internal_strlen(p->ai_canonname) + 1);
2990 p = p->ai_next;
2991 }
2992 }
2993 return res;
2994}
2995#define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
2996#else
2997#define INIT_GETADDRINFO
2998#endif
2999
3000#if SANITIZER_INTERCEPT_GETNAMEINFO
3001INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
3002 unsigned hostlen, char *serv, unsigned servlen, int flags) {
3003 void *ctx;
3004 COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
3005 serv, servlen, flags);
3006 // FIXME: consider adding READ_RANGE(sockaddr, salen)
3007 // There is padding in in_addr that may make this too noisy
3008 // FIXME: under ASan the call below may write to freed memory and corrupt
3009 // its metadata. See
3010 // https://github.com/google/sanitizers/issues/321.
3011 int res =
3012 REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
3013 if (res == 0) {
3014 if (host && hostlen)
3015 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, internal_strlen(host) + 1);
3016 if (serv && servlen)
3017 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, internal_strlen(serv) + 1);
3018 }
3019 return res;
3020}
3021#define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
3022#else
3023#define INIT_GETNAMEINFO
3024#endif
3025
3026#if SANITIZER_INTERCEPT_GETSOCKNAME
3027INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {
3028 void *ctx;
3029 COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
3030 unsigned addr_sz;
3031 if (addrlen) {
3032 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3033 addr_sz = *addrlen;
3034 }
3035 // FIXME: under ASan the call below may write to freed memory and corrupt
3036 // its metadata. See
3037 // https://github.com/google/sanitizers/issues/321.
3038 int res = REAL(getsockname)(sock_fd, addr, addrlen);
3039 if (!res && addr && addrlen) {
3040 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3041 }
3042 return res;
3043}
3044#define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
3045#else
3046#define INIT_GETSOCKNAME
3047#endif
3048
3049#if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
3050static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
3051 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
3052 if (h->h_name)
3053 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, internal_strlen(h->h_name) + 1);
3054 char **p = h->h_aliases;
3055 while (*p) {
3056 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
3057 ++p;
3058 }
3059 COMMON_INTERCEPTOR_WRITE_RANGE(
3060 ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
3061 p = h->h_addr_list;
3062 while (*p) {
3063 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
3064 ++p;
3065 }
3066 COMMON_INTERCEPTOR_WRITE_RANGE(
3067 ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
3068}
3069#endif
3070
3071#if SANITIZER_INTERCEPT_GETHOSTBYNAME
3072INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
3073 void *ctx;
3074 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
3075 struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
3076 if (res) write_hostent(ctx, h: res);
3077 return res;
3078}
3079
3080INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
3081 int type) {
3082 void *ctx;
3083 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
3084 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
3085 struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
3086 if (res) write_hostent(ctx, h: res);
3087 return res;
3088}
3089
3090INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
3091 void *ctx;
3092 COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
3093 struct __sanitizer_hostent *res = REAL(gethostent)(fake);
3094 if (res) write_hostent(ctx, h: res);
3095 return res;
3096}
3097#define INIT_GETHOSTBYNAME \
3098 COMMON_INTERCEPT_FUNCTION(gethostent); \
3099 COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
3100 COMMON_INTERCEPT_FUNCTION(gethostbyname);
3101#else
3102#define INIT_GETHOSTBYNAME
3103#endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
3104
3105#if SANITIZER_INTERCEPT_GETHOSTBYNAME2
3106INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
3107 void *ctx;
3108 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
3109 struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
3110 if (res) write_hostent(ctx, h: res);
3111 return res;
3112}
3113#define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
3114#else
3115#define INIT_GETHOSTBYNAME2
3116#endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
3117
3118#if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
3119INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
3120 char *buf, SIZE_T buflen, __sanitizer_hostent **result,
3121 int *h_errnop) {
3122 void *ctx;
3123 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
3124 h_errnop);
3125 // FIXME: under ASan the call below may write to freed memory and corrupt
3126 // its metadata. See
3127 // https://github.com/google/sanitizers/issues/321.
3128 int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
3129 if (result) {
3130 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3131 if (res == 0 && *result) write_hostent(ctx, h: *result);
3132 }
3133 if (h_errnop)
3134 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3135 return res;
3136}
3137#define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
3138#else
3139#define INIT_GETHOSTBYNAME_R
3140#endif
3141
3142#if SANITIZER_INTERCEPT_GETHOSTENT_R
3143INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
3144 SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
3145 void *ctx;
3146 COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
3147 h_errnop);
3148 // FIXME: under ASan the call below may write to freed memory and corrupt
3149 // its metadata. See
3150 // https://github.com/google/sanitizers/issues/321.
3151 int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
3152 if (result) {
3153 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3154 if (res == 0 && *result) write_hostent(ctx, h: *result);
3155 }
3156 if (h_errnop)
3157 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3158 return res;
3159}
3160#define INIT_GETHOSTENT_R \
3161 COMMON_INTERCEPT_FUNCTION(gethostent_r);
3162#else
3163#define INIT_GETHOSTENT_R
3164#endif
3165
3166#if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
3167INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
3168 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
3169 __sanitizer_hostent **result, int *h_errnop) {
3170 void *ctx;
3171 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
3172 buflen, result, h_errnop);
3173 COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
3174 // FIXME: under ASan the call below may write to freed memory and corrupt
3175 // its metadata. See
3176 // https://github.com/google/sanitizers/issues/321.
3177 int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
3178 h_errnop);
3179 if (result) {
3180 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3181 if (res == 0 && *result) write_hostent(ctx, h: *result);
3182 }
3183 if (h_errnop)
3184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3185 return res;
3186}
3187#define INIT_GETHOSTBYADDR_R \
3188 COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
3189#else
3190#define INIT_GETHOSTBYADDR_R
3191#endif
3192
3193#if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
3194INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
3195 struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
3196 __sanitizer_hostent **result, int *h_errnop) {
3197 void *ctx;
3198 COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
3199 result, h_errnop);
3200 // FIXME: under ASan the call below may write to freed memory and corrupt
3201 // its metadata. See
3202 // https://github.com/google/sanitizers/issues/321.
3203 int res =
3204 REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
3205 if (result) {
3206 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3207 if (res == 0 && *result) write_hostent(ctx, h: *result);
3208 }
3209 if (h_errnop)
3210 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
3211 return res;
3212}
3213#define INIT_GETHOSTBYNAME2_R \
3214 COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
3215#else
3216#define INIT_GETHOSTBYNAME2_R
3217#endif
3218
3219#if SANITIZER_INTERCEPT_GETSOCKOPT
3220INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
3221 int *optlen) {
3222 void *ctx;
3223 COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
3224 optlen);
3225 if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
3226 // FIXME: under ASan the call below may write to freed memory and corrupt
3227 // its metadata. See
3228 // https://github.com/google/sanitizers/issues/321.
3229 int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
3230 if (res == 0)
3231 if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
3232 return res;
3233}
3234#define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
3235#else
3236#define INIT_GETSOCKOPT
3237#endif
3238
3239#if SANITIZER_INTERCEPT_ACCEPT
3240INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
3241 void *ctx;
3242 COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
3243 unsigned addrlen0 = 0;
3244 if (addrlen) {
3245 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3246 addrlen0 = *addrlen;
3247 }
3248 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(accept)(fd, addr, addrlen);
3249 if (fd2 >= 0) {
3250 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3251 if (addr && addrlen)
3252 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3253 }
3254 return fd2;
3255}
3256#define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
3257#else
3258#define INIT_ACCEPT
3259#endif
3260
3261#if SANITIZER_INTERCEPT_ACCEPT4
3262INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
3263 void *ctx;
3264 COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
3265 unsigned addrlen0 = 0;
3266 if (addrlen) {
3267 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3268 addrlen0 = *addrlen;
3269 }
3270 // FIXME: under ASan the call below may write to freed memory and corrupt
3271 // its metadata. See
3272 // https://github.com/google/sanitizers/issues/321.
3273 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(accept4)(fd, addr, addrlen, f);
3274 if (fd2 >= 0) {
3275 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3276 if (addr && addrlen)
3277 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3278 }
3279 return fd2;
3280}
3281#define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
3282#else
3283#define INIT_ACCEPT4
3284#endif
3285
3286#if SANITIZER_INTERCEPT_PACCEPT
3287INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
3288 __sanitizer_sigset_t *set, int f) {
3289 void *ctx;
3290 COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
3291 unsigned addrlen0 = 0;
3292 if (addrlen) {
3293 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3294 addrlen0 = *addrlen;
3295 }
3296 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
3297 int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(paccept)(fd, addr, addrlen, set, f);
3298 if (fd2 >= 0) {
3299 if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
3300 if (addr && addrlen)
3301 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
3302 }
3303 return fd2;
3304}
3305#define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
3306#else
3307#define INIT_PACCEPT
3308#endif
3309
3310#if SANITIZER_INTERCEPT_MODF
3311INTERCEPTOR(double, modf, double x, double *iptr) {
3312 void *ctx;
3313 COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
3314 // FIXME: under ASan the call below may write to freed memory and corrupt
3315 // its metadata. See
3316 // https://github.com/google/sanitizers/issues/321.
3317 double res = REAL(modf)(x, iptr);
3318 if (iptr) {
3319 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3320 }
3321 return res;
3322}
3323INTERCEPTOR(float, modff, float x, float *iptr) {
3324 void *ctx;
3325 COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
3326 // FIXME: under ASan the call below may write to freed memory and corrupt
3327 // its metadata. See
3328 // https://github.com/google/sanitizers/issues/321.
3329 float res = REAL(modff)(x, iptr);
3330 if (iptr) {
3331 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3332 }
3333 return res;
3334}
3335INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
3336 void *ctx;
3337 COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
3338 // FIXME: under ASan the call below may write to freed memory and corrupt
3339 // its metadata. See
3340 // https://github.com/google/sanitizers/issues/321.
3341 long double res = REAL(modfl)(x, iptr);
3342 if (iptr) {
3343 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
3344 }
3345 return res;
3346}
3347#define INIT_MODF \
3348 COMMON_INTERCEPT_FUNCTION(modf); \
3349 COMMON_INTERCEPT_FUNCTION(modff); \
3350 COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
3351#else
3352#define INIT_MODF
3353#endif
3354
3355#if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
3356static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3357 SSIZE_T maxlen) {
3358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
3359 if (msg->msg_name && msg->msg_namelen)
3360 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3361 if (msg->msg_iov && msg->msg_iovlen)
3362 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
3363 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3364 write_iovec(ctx, iovec: msg->msg_iov, iovlen: msg->msg_iovlen, maxlen);
3365 if (msg->msg_control && msg->msg_controllen)
3366 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
3367}
3368#endif
3369
3370#if SANITIZER_INTERCEPT_RECVMSG
3371INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
3372 int flags) {
3373 void *ctx;
3374 COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
3375 // FIXME: under ASan the call below may write to freed memory and corrupt
3376 // its metadata. See
3377 // https://github.com/google/sanitizers/issues/321.
3378 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recvmsg)(fd, msg, flags);
3379 if (res >= 0) {
3380 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3381 if (msg) {
3382 write_msghdr(ctx, msg, maxlen: res);
3383 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
3384 }
3385 }
3386 return res;
3387}
3388#define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
3389#else
3390#define INIT_RECVMSG
3391#endif
3392
3393#if SANITIZER_INTERCEPT_RECVMMSG
3394INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3395 unsigned int vlen, int flags, void *timeout) {
3396 void *ctx;
3397 COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
3398 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
3399 int res =
3400 COMMON_INTERCEPTOR_BLOCK_REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
3401 if (res >= 0) {
3402 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
3403 for (int i = 0; i < res; ++i) {
3404 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3405 sizeof(msgvec[i].msg_len));
3406 write_msghdr(ctx, msg: &msgvec[i].msg_hdr, maxlen: msgvec[i].msg_len);
3407 COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
3408 }
3409 }
3410 return res;
3411}
3412#define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
3413#else
3414#define INIT_RECVMMSG
3415#endif
3416
3417#if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
3418static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
3419 const unsigned kCmsgDataOffset =
3420 RoundUpTo(size: sizeof(__sanitizer_cmsghdr), boundary: sizeof(uptr));
3421
3422 char *p = (char *)control;
3423 char *const control_end = p + controllen;
3424 while (true) {
3425 if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
3426 __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
3427 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
3428
3429 if (p + RoundUpTo(size: cmsg->cmsg_len, boundary: sizeof(uptr)) > control_end) break;
3430
3431 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
3432 sizeof(cmsg->cmsg_level));
3433 COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
3434 sizeof(cmsg->cmsg_type));
3435
3436 if (cmsg->cmsg_len > kCmsgDataOffset) {
3437 char *data = p + kCmsgDataOffset;
3438 unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
3439 if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
3440 }
3441
3442 p += RoundUpTo(size: cmsg->cmsg_len, boundary: sizeof(uptr));
3443 }
3444}
3445
3446static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
3447 SSIZE_T maxlen) {
3448#define R(f) \
3449 COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
3450 R(name);
3451 R(namelen);
3452 R(iov);
3453 R(iovlen);
3454 R(control);
3455 R(controllen);
3456 R(flags);
3457#undef R
3458 if (msg->msg_name && msg->msg_namelen)
3459 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
3460 if (msg->msg_iov && msg->msg_iovlen)
3461 COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
3462 sizeof(*msg->msg_iov) * msg->msg_iovlen);
3463 read_iovec(ctx, iovec: msg->msg_iov, iovlen: msg->msg_iovlen, maxlen);
3464 if (msg->msg_control && msg->msg_controllen)
3465 read_msghdr_control(ctx, control: msg->msg_control, controllen: msg->msg_controllen);
3466}
3467#endif
3468
3469#if SANITIZER_INTERCEPT_SENDMSG
3470INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
3471 int flags) {
3472 void *ctx;
3473 COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
3474 if (fd >= 0) {
3475 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3476 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3477 }
3478 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(sendmsg)(fd, msg, flags);
3479 if (common_flags()->intercept_send && res >= 0 && msg)
3480 read_msghdr(ctx, msg, maxlen: res);
3481 return res;
3482}
3483#define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
3484#else
3485#define INIT_SENDMSG
3486#endif
3487
3488#if SANITIZER_INTERCEPT_SENDMMSG
3489INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
3490 unsigned vlen, int flags) {
3491 void *ctx;
3492 COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
3493 if (fd >= 0) {
3494 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
3495 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
3496 }
3497 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sendmmsg)(fd, msgvec, vlen, flags);
3498 if (res >= 0 && msgvec) {
3499 for (int i = 0; i < res; ++i) {
3500 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
3501 sizeof(msgvec[i].msg_len));
3502 if (common_flags()->intercept_send)
3503 read_msghdr(ctx, msg: &msgvec[i].msg_hdr, maxlen: msgvec[i].msg_len);
3504 }
3505 }
3506 return res;
3507}
3508#define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
3509#else
3510#define INIT_SENDMMSG
3511#endif
3512
3513#if SANITIZER_INTERCEPT_SYSMSG
3514INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
3515 int msgflg) {
3516 void *ctx;
3517 COMMON_INTERCEPTOR_ENTER(ctx, msgsnd, msqid, msgp, msgsz, msgflg);
3518 if (msgp)
3519 COMMON_INTERCEPTOR_READ_RANGE(ctx, msgp, sizeof(long) + msgsz);
3520 int res = COMMON_INTERCEPTOR_BLOCK_REAL(msgsnd)(msqid, msgp, msgsz, msgflg);
3521 return res;
3522}
3523
3524INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
3525 long msgtyp, int msgflg) {
3526 void *ctx;
3527 COMMON_INTERCEPTOR_ENTER(ctx, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
3528 SSIZE_T len =
3529 COMMON_INTERCEPTOR_BLOCK_REAL(msgrcv)(msqid, msgp, msgsz, msgtyp, msgflg);
3530 if (len != -1)
3531 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msgp, sizeof(long) + len);
3532 return len;
3533}
3534
3535#define INIT_SYSMSG \
3536 COMMON_INTERCEPT_FUNCTION(msgsnd); \
3537 COMMON_INTERCEPT_FUNCTION(msgrcv);
3538#else
3539#define INIT_SYSMSG
3540#endif
3541
3542#if SANITIZER_INTERCEPT_GETPEERNAME
3543INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
3544 void *ctx;
3545 COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
3546 unsigned addr_sz;
3547 if (addrlen) {
3548 COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
3549 addr_sz = *addrlen;
3550 }
3551 // FIXME: under ASan the call below may write to freed memory and corrupt
3552 // its metadata. See
3553 // https://github.com/google/sanitizers/issues/321.
3554 int res = REAL(getpeername)(sockfd, addr, addrlen);
3555 if (!res && addr && addrlen) {
3556 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
3557 }
3558 return res;
3559}
3560#define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
3561#else
3562#define INIT_GETPEERNAME
3563#endif
3564
3565#if SANITIZER_INTERCEPT_SYSINFO
3566INTERCEPTOR(int, sysinfo, void *info) {
3567 void *ctx;
3568 // FIXME: under ASan the call below may write to freed memory and corrupt
3569 // its metadata. See
3570 // https://github.com/google/sanitizers/issues/321.
3571 COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
3572 int res = REAL(sysinfo)(info);
3573 if (!res && info)
3574 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
3575 return res;
3576}
3577#define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
3578#else
3579#define INIT_SYSINFO
3580#endif
3581
3582#if SANITIZER_INTERCEPT_READDIR
3583INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
3584 void *ctx;
3585 COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
3586 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
3587 __sanitizer_dirent *res = REAL(opendir)(path);
3588 if (res)
3589 COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
3590 return res;
3591}
3592
3593INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
3594 void *ctx;
3595 COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
3596 // FIXME: under ASan the call below may write to freed memory and corrupt
3597 // its metadata. See
3598 // https://github.com/google/sanitizers/issues/321.
3599 __sanitizer_dirent *res = REAL(readdir)(dirp);
3600 if (res)
3601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3602 return res;
3603}
3604
3605INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
3606 __sanitizer_dirent **result) {
3607 void *ctx;
3608 COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
3609 // FIXME: under ASan the call below may write to freed memory and corrupt
3610 // its metadata. See
3611 // https://github.com/google/sanitizers/issues/321.
3612 int res = REAL(readdir_r)(dirp, entry, result);
3613 if (!res) {
3614 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3615 if (*result)
3616 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3617 }
3618 return res;
3619}
3620
3621#define INIT_READDIR \
3622 COMMON_INTERCEPT_FUNCTION(opendir); \
3623 COMMON_INTERCEPT_FUNCTION(readdir); \
3624 COMMON_INTERCEPT_FUNCTION(readdir_r);
3625#else
3626#define INIT_READDIR
3627#endif
3628
3629#if SANITIZER_INTERCEPT_READDIR64
3630INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
3631 void *ctx;
3632 COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
3633 // FIXME: under ASan the call below may write to freed memory and corrupt
3634 // its metadata. See
3635 // https://github.com/google/sanitizers/issues/321.
3636 __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
3637 if (res)
3638 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer_dirsiz(res));
3639 return res;
3640}
3641
3642INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
3643 __sanitizer_dirent64 **result) {
3644 void *ctx;
3645 COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
3646 // FIXME: under ASan the call below may write to freed memory and corrupt
3647 // its metadata. See
3648 // https://github.com/google/sanitizers/issues/321.
3649 int res = REAL(readdir64_r)(dirp, entry, result);
3650 if (!res) {
3651 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
3652 if (*result)
3653 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, __sanitizer_dirsiz(*result));
3654 }
3655 return res;
3656}
3657#define INIT_READDIR64 \
3658 COMMON_INTERCEPT_FUNCTION(readdir64); \
3659 COMMON_INTERCEPT_FUNCTION(readdir64_r);
3660#else
3661#define INIT_READDIR64
3662#endif
3663
3664#if SANITIZER_INTERCEPT_PTRACE
3665INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
3666 void *ctx;
3667 COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
3668 __sanitizer_iovec local_iovec;
3669
3670 void *data_arg = ptrace_data_arg(request, addr, data);
3671 if (data_arg) {
3672 if (request == ptrace_setregs) {
3673 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg, struct_user_regs_struct_sz);
3674 } else if (request == ptrace_setfpregs) {
3675 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3676 struct_user_fpregs_struct_sz);
3677 } else if (request == ptrace_setfpxregs) {
3678 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3679 struct_user_fpxregs_struct_sz);
3680 } else if (request == ptrace_setvfpregs) {
3681 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg,
3682 struct_user_vfpregs_struct_sz);
3683 } else if (request == ptrace_setsiginfo) {
3684 COMMON_INTERCEPTOR_READ_RANGE(ctx, data_arg, siginfo_t_sz);
3685
3686 // Some kernel might zero the iovec::iov_base in case of invalid
3687 // write access. In this case copy the invalid address for further
3688 // inspection.
3689 } else if (request == ptrace_setregset || request == ptrace_getregset) {
3690 __sanitizer_iovec *iovec = (__sanitizer_iovec *)data_arg;
3691 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
3692 local_iovec = *iovec;
3693 if (request == ptrace_setregset)
3694 COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
3695 }
3696 }
3697
3698 // FIXME: under ASan the call below may write to freed memory and corrupt
3699 // its metadata. See
3700 // https://github.com/google/sanitizers/issues/321.
3701 uptr res = REAL(ptrace)(request, pid, addr, data);
3702
3703 if (!res && data_arg) {
3704 // Note that PEEK* requests assign different meaning to the return value.
3705 // This function does not handle them (nor does it need to).
3706 if (request == ptrace_getregs) {
3707 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, struct_user_regs_struct_sz);
3708 } else if (request == ptrace_getfpregs) {
3709 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3710 struct_user_fpregs_struct_sz);
3711 } else if (request == ptrace_getfpxregs) {
3712 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3713 struct_user_fpxregs_struct_sz);
3714 } else if (request == ptrace_getvfpregs) {
3715 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg,
3716 struct_user_vfpregs_struct_sz);
3717 } else if (request == ptrace_getsiginfo) {
3718 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, siginfo_t_sz);
3719 } else if (request == ptrace_geteventmsg) {
3720 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data_arg, sizeof(unsigned long));
3721 } else if (request == ptrace_getregset) {
3722 __sanitizer_iovec *iovec = (__sanitizer_iovec *)data_arg;
3723 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
3724 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
3725 local_iovec.iov_len);
3726 }
3727 }
3728 return res;
3729}
3730
3731#define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
3732#else
3733#define INIT_PTRACE
3734#endif
3735
3736#if SANITIZER_INTERCEPT_SETLOCALE
3737static void unpoison_ctype_arrays(void *ctx) {
3738#if SANITIZER_NETBSD
3739 // These arrays contain 256 regular elements in unsigned char range + 1 EOF
3740 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
3741 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
3742 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
3743#endif
3744}
3745
3746INTERCEPTOR(char *, setlocale, int category, char *locale) {
3747 void *ctx;
3748 COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
3749 if (locale)
3750 COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, internal_strlen(locale) + 1);
3751 char *res = REAL(setlocale)(category, locale);
3752 if (res) {
3753 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
3754 unpoison_ctype_arrays(ctx);
3755 }
3756 return res;
3757}
3758
3759#define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
3760#else
3761#define INIT_SETLOCALE
3762#endif
3763
3764#if SANITIZER_INTERCEPT_GETCWD
3765INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
3766 void *ctx;
3767 COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
3768 // FIXME: under ASan the call below may write to freed memory and corrupt
3769 // its metadata. See
3770 // https://github.com/google/sanitizers/issues/321.
3771 char *res = REAL(getcwd)(buf, size);
3772 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3773 return res;
3774}
3775#define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
3776#else
3777#define INIT_GETCWD
3778#endif
3779
3780#if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
3781INTERCEPTOR(char *, get_current_dir_name, int fake) {
3782 void *ctx;
3783 COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
3784 // FIXME: under ASan the call below may write to freed memory and corrupt
3785 // its metadata. See
3786 // https://github.com/google/sanitizers/issues/321.
3787 char *res = REAL(get_current_dir_name)(fake);
3788 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
3789 return res;
3790}
3791
3792#define INIT_GET_CURRENT_DIR_NAME \
3793 COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
3794#else
3795#define INIT_GET_CURRENT_DIR_NAME
3796#endif
3797
3798UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
3799 CHECK(endptr);
3800 if (nptr == *endptr) {
3801 // No digits were found at strtol call, we need to find out the last
3802 // symbol accessed by strtoll on our own.
3803 // We get this symbol by skipping leading blanks and optional +/- sign.
3804 while (IsSpace(c: *nptr)) nptr++;
3805 if (*nptr == '+' || *nptr == '-') nptr++;
3806 *endptr = const_cast<char *>(nptr);
3807 }
3808 CHECK(*endptr >= nptr);
3809}
3810
3811UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
3812 char **endptr, char *real_endptr, int base) {
3813 if (endptr) {
3814 *endptr = real_endptr;
3815 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
3816 }
3817 // If base has unsupported value, strtol can exit with EINVAL
3818 // without reading any characters. So do additional checks only
3819 // if base is valid.
3820 bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
3821 if (is_valid_base) {
3822 FixRealStrtolEndptr(nptr, endptr: &real_endptr);
3823 }
3824 COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
3825 (real_endptr - nptr) + 1 : 0);
3826}
3827
3828#if SANITIZER_INTERCEPT_STRTOIMAX
3829template <typename Fn>
3830static ALWAYS_INLINE auto StrtoimaxImpl(void *ctx, Fn real, const char *nptr,
3831 char **endptr, int base)
3832 -> decltype(real(nullptr, nullptr, 0)) {
3833 char *real_endptr;
3834 auto res = real(nptr, &real_endptr, base);
3835 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
3836 return res;
3837}
3838
3839INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
3840 void *ctx;
3841 COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
3842 return StrtoimaxImpl(ctx, REAL(strtoimax), nptr, endptr, base);
3843}
3844INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
3845 void *ctx;
3846 COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
3847 return StrtoimaxImpl(ctx, REAL(strtoumax), nptr, endptr, base);
3848}
3849
3850#define INIT_STRTOIMAX \
3851 COMMON_INTERCEPT_FUNCTION(strtoimax); \
3852 COMMON_INTERCEPT_FUNCTION(strtoumax);
3853#else
3854#define INIT_STRTOIMAX
3855#endif
3856
3857#if SANITIZER_INTERCEPT_STRTOIMAX && SANITIZER_GLIBC
3858INTERCEPTOR(INTMAX_T, __isoc23_strtoimax, const char *nptr, char **endptr, int base) {
3859 void *ctx;
3860 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoimax, nptr, endptr, base);
3861 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoimax), nptr, endptr, base);
3862}
3863INTERCEPTOR(UINTMAX_T, __isoc23_strtoumax, const char *nptr, char **endptr, int base) {
3864 void *ctx;
3865 COMMON_INTERCEPTOR_ENTER(ctx, __isoc23_strtoumax, nptr, endptr, base);
3866 return StrtoimaxImpl(ctx, REAL(__isoc23_strtoumax), nptr, endptr, base);
3867}
3868
3869# define INIT_STRTOIMAX_C23 \
3870 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoimax); \
3871 COMMON_INTERCEPT_FUNCTION(__isoc23_strtoumax);
3872#else
3873# define INIT_STRTOIMAX_C23
3874#endif
3875
3876#if SANITIZER_INTERCEPT_MBSTOWCS
3877INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
3878 void *ctx;
3879 COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
3880 // FIXME: under ASan the call below may write to freed memory and corrupt
3881 // its metadata. See
3882 // https://github.com/google/sanitizers/issues/321.
3883 SIZE_T res = REAL(mbstowcs)(dest, src, len);
3884 if (res != (SIZE_T) - 1 && dest) {
3885 SIZE_T write_cnt = res + (res < len);
3886 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3887 }
3888 return res;
3889}
3890
3891INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
3892 void *ps) {
3893 void *ctx;
3894 COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
3895 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3896 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3897 // FIXME: under ASan the call below may write to freed memory and corrupt
3898 // its metadata. See
3899 // https://github.com/google/sanitizers/issues/321.
3900 SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
3901 if (res != (SIZE_T)(-1) && dest && src) {
3902 // This function, and several others, may or may not write the terminating
3903 // \0 character. They write it iff they clear *src.
3904 SIZE_T write_cnt = res + !*src;
3905 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3906 }
3907 return res;
3908}
3909
3910#define INIT_MBSTOWCS \
3911 COMMON_INTERCEPT_FUNCTION(mbstowcs); \
3912 COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
3913#else
3914#define INIT_MBSTOWCS
3915#endif
3916
3917#if SANITIZER_INTERCEPT_MBSNRTOWCS
3918INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
3919 SIZE_T len, void *ps) {
3920 void *ctx;
3921 COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
3922 if (src) {
3923 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3924 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3925 }
3926 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3927 // FIXME: under ASan the call below may write to freed memory and corrupt
3928 // its metadata. See
3929 // https://github.com/google/sanitizers/issues/321.
3930 SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
3931 if (res != (SIZE_T)(-1) && dest && src) {
3932 SIZE_T write_cnt = res + !*src;
3933 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
3934 }
3935 return res;
3936}
3937
3938#define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
3939#else
3940#define INIT_MBSNRTOWCS
3941#endif
3942
3943#if SANITIZER_INTERCEPT_WCSTOMBS
3944INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
3945 void *ctx;
3946 COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
3947 // FIXME: under ASan the call below may write to freed memory and corrupt
3948 // its metadata. See
3949 // https://github.com/google/sanitizers/issues/321.
3950 SIZE_T res = REAL(wcstombs)(dest, src, len);
3951 if (res != (SIZE_T) - 1 && dest) {
3952 SIZE_T write_cnt = res + (res < len);
3953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3954 }
3955 return res;
3956}
3957
3958INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
3959 void *ps) {
3960 void *ctx;
3961 COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
3962 if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3963 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3964 // FIXME: under ASan the call below may write to freed memory and corrupt
3965 // its metadata. See
3966 // https://github.com/google/sanitizers/issues/321.
3967 SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
3968 if (res != (SIZE_T) - 1 && dest && src) {
3969 SIZE_T write_cnt = res + !*src;
3970 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3971 }
3972 return res;
3973}
3974
3975#define INIT_WCSTOMBS \
3976 COMMON_INTERCEPT_FUNCTION(wcstombs); \
3977 COMMON_INTERCEPT_FUNCTION(wcsrtombs);
3978#else
3979#define INIT_WCSTOMBS
3980#endif
3981
3982#if SANITIZER_INTERCEPT_WCSNRTOMBS
3983INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
3984 SIZE_T len, void *ps) {
3985 void *ctx;
3986 COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
3987 if (src) {
3988 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
3989 if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
3990 }
3991 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
3992 // FIXME: under ASan the call below may write to freed memory and corrupt
3993 // its metadata. See
3994 // https://github.com/google/sanitizers/issues/321.
3995 SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
3996 if (res != ((SIZE_T)-1) && dest && src) {
3997 SIZE_T write_cnt = res + !*src;
3998 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
3999 }
4000 return res;
4001}
4002
4003#define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
4004#else
4005#define INIT_WCSNRTOMBS
4006#endif
4007
4008
4009#if SANITIZER_INTERCEPT_WCRTOMB
4010INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
4011 void *ctx;
4012 COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
4013 if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
4014
4015 if (!dest)
4016 return REAL(wcrtomb)(dest, src, ps);
4017
4018 char local_dest[32];
4019 SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
4020 if (res != ((SIZE_T)-1)) {
4021 CHECK_LE(res, sizeof(local_dest));
4022 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
4023 internal_memcpy(dest, src: local_dest, n: res);
4024 }
4025 return res;
4026}
4027
4028#define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
4029#else
4030#define INIT_WCRTOMB
4031#endif
4032
4033#if SANITIZER_INTERCEPT_WCTOMB
4034INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
4035 void *ctx;
4036 COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
4037 if (!dest)
4038 return REAL(wctomb)(dest, src);
4039
4040 char local_dest[32];
4041 int res = REAL(wctomb)(local_dest, src);
4042 if (res != -1) {
4043 CHECK_LE(res, sizeof(local_dest));
4044 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
4045 internal_memcpy(dest, src: local_dest, n: res);
4046 }
4047 return res;
4048}
4049
4050#define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
4051#else
4052#define INIT_WCTOMB
4053#endif
4054
4055#if SANITIZER_INTERCEPT_TCGETATTR
4056INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
4057 void *ctx;
4058 COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
4059 // FIXME: under ASan the call below may write to freed memory and corrupt
4060 // its metadata. See
4061 // https://github.com/google/sanitizers/issues/321.
4062 int res = REAL(tcgetattr)(fd, termios_p);
4063 if (!res && termios_p)
4064 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
4065 return res;
4066}
4067
4068#define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
4069#else
4070#define INIT_TCGETATTR
4071#endif
4072
4073#if SANITIZER_INTERCEPT_REALPATH
4074INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
4075 void *ctx;
4076 COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
4077 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4078
4079 // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
4080 // version of a versioned symbol. For realpath(), this gives us something
4081 // (called __old_realpath) that does not handle NULL in the second argument.
4082 // Handle it as part of the interceptor.
4083 char *allocated_path = nullptr;
4084 if (!resolved_path)
4085 allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
4086
4087 char *res = REAL(realpath)(path, resolved_path);
4088 if (allocated_path && !res)
4089 WRAP(free)(allocated_path);
4090 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4091 return res;
4092}
4093# define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
4094#else
4095#define INIT_REALPATH
4096#endif
4097
4098#if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
4099INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
4100 void *ctx;
4101 COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
4102 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4103 char *res = REAL(canonicalize_file_name)(path);
4104 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4105 return res;
4106}
4107#define INIT_CANONICALIZE_FILE_NAME \
4108 COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
4109#else
4110#define INIT_CANONICALIZE_FILE_NAME
4111#endif
4112
4113#if SANITIZER_INTERCEPT_CONFSTR
4114INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
4115 void *ctx;
4116 COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
4117 // FIXME: under ASan the call below may write to freed memory and corrupt
4118 // its metadata. See
4119 // https://github.com/google/sanitizers/issues/321.
4120 SIZE_T res = REAL(confstr)(name, buf, len);
4121 if (buf && res)
4122 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
4123 return res;
4124}
4125#define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
4126#else
4127#define INIT_CONFSTR
4128#endif
4129
4130#if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
4131INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
4132 void *ctx;
4133 COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
4134 // FIXME: under ASan the call below may write to freed memory and corrupt
4135 // its metadata. See
4136 // https://github.com/google/sanitizers/issues/321.
4137 int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
4138 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
4139 return res;
4140}
4141#define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
4142#else
4143#define INIT_SCHED_GETAFFINITY
4144#endif
4145
4146#if SANITIZER_INTERCEPT_SCHED_GETPARAM
4147INTERCEPTOR(int, sched_getparam, int pid, void *param) {
4148 void *ctx;
4149 COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
4150 int res = REAL(sched_getparam)(pid, param);
4151 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
4152 return res;
4153}
4154#define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
4155#else
4156#define INIT_SCHED_GETPARAM
4157#endif
4158
4159#if SANITIZER_INTERCEPT_STRERROR
4160INTERCEPTOR(char *, strerror, int errnum) {
4161 void *ctx;
4162 COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
4163 COMMON_INTERCEPTOR_STRERROR();
4164 char *res = REAL(strerror)(errnum);
4165 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4166 return res;
4167}
4168#define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
4169#else
4170#define INIT_STRERROR
4171#endif
4172
4173#if SANITIZER_INTERCEPT_STRERROR_R
4174// There are 2 versions of strerror_r:
4175// * POSIX version returns 0 on success, negative error code on failure,
4176// writes message to buf.
4177// * GNU version returns message pointer, which points to either buf or some
4178// static storage.
4179#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
4180 SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD || \
4181 SANITIZER_FREEBSD
4182// POSIX version. Spec is not clear on whether buf is NULL-terminated.
4183// At least on OSX, buf contents are valid even when the call fails.
4184INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
4185 void *ctx;
4186 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
4187 // FIXME: under ASan the call below may write to freed memory and corrupt
4188 // its metadata. See
4189 // https://github.com/google/sanitizers/issues/321.
4190 int res = REAL(strerror_r)(errnum, buf, buflen);
4191
4192 SIZE_T sz = internal_strnlen(buf, buflen);
4193 if (sz < buflen) ++sz;
4194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4195 return res;
4196}
4197#else
4198// GNU version.
4199INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
4200 void *ctx;
4201 COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
4202 // FIXME: under ASan the call below may write to freed memory and corrupt
4203 // its metadata. See
4204 // https://github.com/google/sanitizers/issues/321.
4205 char *res = REAL(strerror_r)(errnum, buf, buflen);
4206 if (res == buf)
4207 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4208 else
4209 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4210 return res;
4211}
4212#endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
4213 //SANITIZER_APPLE
4214#define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
4215#else
4216#define INIT_STRERROR_R
4217#endif
4218
4219#if SANITIZER_INTERCEPT_XPG_STRERROR_R
4220INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
4221 void *ctx;
4222 COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
4223 // FIXME: under ASan the call below may write to freed memory and corrupt
4224 // its metadata. See
4225 // https://github.com/google/sanitizers/issues/321.
4226 int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
4227 // This version always returns a null-terminated string.
4228 if (buf && buflen)
4229 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
4230 return res;
4231}
4232#define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
4233#else
4234#define INIT_XPG_STRERROR_R
4235#endif
4236
4237#if SANITIZER_INTERCEPT_SCANDIR
4238typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
4239typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
4240 const struct __sanitizer_dirent **);
4241
4242static THREADLOCAL scandir_filter_f scandir_filter;
4243static THREADLOCAL scandir_compar_f scandir_compar;
4244
4245static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
4246 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4247 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4248 return scandir_filter(dir);
4249}
4250
4251static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
4252 const struct __sanitizer_dirent **b) {
4253 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4254 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4255 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4256 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4257 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4258 return scandir_compar(a, b);
4259}
4260
4261INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
4262 scandir_filter_f filter, scandir_compar_f compar) {
4263 void *ctx;
4264 COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
4265 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4266 scandir_filter = filter;
4267 scandir_compar = compar;
4268 // FIXME: under ASan the call below may write to freed memory and corrupt
4269 // its metadata. See
4270 // https://github.com/google/sanitizers/issues/321.
4271 int res = REAL(scandir)(dirp, namelist,
4272 filter ? wrapped_scandir_filter : nullptr,
4273 compar ? wrapped_scandir_compar : nullptr);
4274 scandir_filter = nullptr;
4275 scandir_compar = nullptr;
4276 if (namelist && res > 0) {
4277 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4278 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4279 for (int i = 0; i < res; ++i)
4280 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4281 __sanitizer_dirsiz((*namelist)[i]));
4282 }
4283 return res;
4284}
4285#define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
4286#else
4287#define INIT_SCANDIR
4288#endif
4289
4290#if SANITIZER_INTERCEPT_SCANDIR64
4291typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
4292typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
4293 const struct __sanitizer_dirent64 **);
4294
4295static THREADLOCAL scandir64_filter_f scandir64_filter;
4296static THREADLOCAL scandir64_compar_f scandir64_compar;
4297
4298static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
4299 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
4300 COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, __sanitizer_dirsiz(dir));
4301 return scandir64_filter(dir);
4302}
4303
4304static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
4305 const struct __sanitizer_dirent64 **b) {
4306 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
4307 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
4308 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, __sanitizer_dirsiz(*a));
4309 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
4310 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, __sanitizer_dirsiz(*b));
4311 return scandir64_compar(a, b);
4312}
4313
4314INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
4315 scandir64_filter_f filter, scandir64_compar_f compar) {
4316 void *ctx;
4317 COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
4318 if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
4319 scandir64_filter = filter;
4320 scandir64_compar = compar;
4321 // FIXME: under ASan the call below may write to freed memory and corrupt
4322 // its metadata. See
4323 // https://github.com/google/sanitizers/issues/321.
4324 int res =
4325 REAL(scandir64)(dirp, namelist,
4326 filter ? wrapped_scandir64_filter : nullptr,
4327 compar ? wrapped_scandir64_compar : nullptr);
4328 scandir64_filter = nullptr;
4329 scandir64_compar = nullptr;
4330 if (namelist && res > 0) {
4331 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
4332 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
4333 for (int i = 0; i < res; ++i)
4334 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
4335 __sanitizer_dirsiz((*namelist)[i]));
4336 }
4337 return res;
4338}
4339#define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
4340#else
4341#define INIT_SCANDIR64
4342#endif
4343
4344#if SANITIZER_INTERCEPT_GETGROUPS
4345INTERCEPTOR(int, getgroups, int size, u32 *lst) {
4346 void *ctx;
4347 COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
4348 // FIXME: under ASan the call below may write to freed memory and corrupt
4349 // its metadata. See
4350 // https://github.com/google/sanitizers/issues/321.
4351 int res = REAL(getgroups)(size, lst);
4352 if (res >= 0 && lst && size > 0)
4353 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
4354 return res;
4355}
4356#define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
4357#else
4358#define INIT_GETGROUPS
4359#endif
4360
4361#if SANITIZER_INTERCEPT_POLL
4362static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
4363 __sanitizer_nfds_t nfds) {
4364 for (unsigned i = 0; i < nfds; ++i) {
4365 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
4366 COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
4367 }
4368}
4369
4370static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
4371 __sanitizer_nfds_t nfds) {
4372 for (unsigned i = 0; i < nfds; ++i)
4373 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
4374 sizeof(fds[i].revents));
4375}
4376
4377INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4378 int timeout) {
4379 void *ctx;
4380 COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
4381 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4382 int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
4383 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4384 return res;
4385}
4386#define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
4387#else
4388#define INIT_POLL
4389#endif
4390
4391#if SANITIZER_INTERCEPT_PPOLL
4392INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
4393 void *timeout_ts, __sanitizer_sigset_t *sigmask) {
4394 void *ctx;
4395 COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
4396 if (fds && nfds) read_pollfd(ctx, fds, nfds);
4397 if (timeout_ts)
4398 COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
4399 if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
4400 int res =
4401 COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
4402 if (fds && nfds) write_pollfd(ctx, fds, nfds);
4403 return res;
4404}
4405#define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
4406#else
4407#define INIT_PPOLL
4408#endif
4409
4410#if SANITIZER_INTERCEPT_WORDEXP
4411INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
4412 void *ctx;
4413 COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
4414 if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
4415 // FIXME: under ASan the call below may write to freed memory and corrupt
4416 // its metadata. See
4417 // https://github.com/google/sanitizers/issues/321.
4418 int res = REAL(wordexp)(s, p, flags);
4419 if (!res && p) {
4420 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
4421 uptr we_wordc =
4422 ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc;
4423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
4424 sizeof(*p->we_wordv) * (we_wordc + 1));
4425 for (uptr i = 0; i < we_wordc; ++i) {
4426 char *w = p->we_wordv[i];
4427 if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);
4428 }
4429 }
4430 return res;
4431}
4432#define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
4433#else
4434#define INIT_WORDEXP
4435#endif
4436
4437#if SANITIZER_INTERCEPT_SIGWAIT
4438INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
4439 void *ctx;
4440 COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
4441 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4442 // FIXME: under ASan the call below may write to freed memory and corrupt
4443 // its metadata. See
4444 // https://github.com/google/sanitizers/issues/321.
4445 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwait)(set, sig);
4446 if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
4447 return res;
4448}
4449#define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
4450#else
4451#define INIT_SIGWAIT
4452#endif
4453
4454#if SANITIZER_INTERCEPT_SIGWAITINFO
4455INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
4456 void *ctx;
4457 COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
4458 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4459 // FIXME: under ASan the call below may write to freed memory and corrupt
4460 // its metadata. See
4461 // https://github.com/google/sanitizers/issues/321.
4462 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwaitinfo)(set, info);
4463 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4464 return res;
4465}
4466#define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
4467#else
4468#define INIT_SIGWAITINFO
4469#endif
4470
4471#if SANITIZER_INTERCEPT_SIGTIMEDWAIT
4472INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
4473 void *timeout) {
4474 void *ctx;
4475 COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
4476 if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
4477 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4478 // FIXME: under ASan the call below may write to freed memory and corrupt
4479 // its metadata. See
4480 // https://github.com/google/sanitizers/issues/321.
4481 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigtimedwait)(set, info, timeout);
4482 if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
4483 return res;
4484}
4485#define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
4486#else
4487#define INIT_SIGTIMEDWAIT
4488#endif
4489
4490#if SANITIZER_INTERCEPT_SIGSETOPS
4491INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
4492 void *ctx;
4493 COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
4494 // FIXME: under ASan the call below may write to freed memory and corrupt
4495 // its metadata. See
4496 // https://github.com/google/sanitizers/issues/321.
4497 int res = REAL(sigemptyset)(set);
4498 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4499 return res;
4500}
4501
4502INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
4503 void *ctx;
4504 COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
4505 // FIXME: under ASan the call below may write to freed memory and corrupt
4506 // its metadata. See
4507 // https://github.com/google/sanitizers/issues/321.
4508 int res = REAL(sigfillset)(set);
4509 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4510 return res;
4511}
4512#define INIT_SIGSETOPS \
4513 COMMON_INTERCEPT_FUNCTION(sigemptyset); \
4514 COMMON_INTERCEPT_FUNCTION(sigfillset);
4515#else
4516#define INIT_SIGSETOPS
4517#endif
4518
4519#if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
4520INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
4521 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4522 void *ctx;
4523 COMMON_INTERCEPTOR_ENTER(ctx, sigandset, dst, src1, src2);
4524 if (src1)
4525 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4526 if (src2)
4527 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4528 int res = REAL(sigandset)(dst, src1, src2);
4529 if (!res && dst)
4530 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4531 return res;
4532}
4533
4534INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
4535 __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
4536 void *ctx;
4537 COMMON_INTERCEPTOR_ENTER(ctx, sigorset, dst, src1, src2);
4538 if (src1)
4539 COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
4540 if (src2)
4541 COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
4542 int res = REAL(sigorset)(dst, src1, src2);
4543 if (!res && dst)
4544 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
4545 return res;
4546}
4547#define INIT_SIGSET_LOGICOPS \
4548 COMMON_INTERCEPT_FUNCTION(sigandset); \
4549 COMMON_INTERCEPT_FUNCTION(sigorset);
4550#else
4551#define INIT_SIGSET_LOGICOPS
4552#endif
4553
4554#if SANITIZER_INTERCEPT_SIGPENDING
4555INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
4556 void *ctx;
4557 COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
4558 // FIXME: under ASan the call below may write to freed memory and corrupt
4559 // its metadata. See
4560 // https://github.com/google/sanitizers/issues/321.
4561 int res = REAL(sigpending)(set);
4562 if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
4563 return res;
4564}
4565#define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
4566#else
4567#define INIT_SIGPENDING
4568#endif
4569
4570#if SANITIZER_INTERCEPT_SIGPROCMASK
4571INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
4572 __sanitizer_sigset_t *oldset) {
4573 void *ctx;
4574 COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
4575 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4576 // FIXME: under ASan the call below may write to freed memory and corrupt
4577 // its metadata. See
4578 // https://github.com/google/sanitizers/issues/321.
4579 int res = REAL(sigprocmask)(how, set, oldset);
4580 if (!res && oldset)
4581 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4582 return res;
4583}
4584#define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
4585#else
4586#define INIT_SIGPROCMASK
4587#endif
4588
4589#if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
4590INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
4591 __sanitizer_sigset_t *oldset) {
4592 void *ctx;
4593 COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
4594 if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
4595 // FIXME: under ASan the call below may write to freed memory and corrupt
4596 // its metadata. See
4597 // https://github.com/google/sanitizers/issues/321.
4598 int res = REAL(pthread_sigmask)(how, set, oldset);
4599 if (!res && oldset)
4600 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
4601 return res;
4602}
4603#define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
4604#else
4605#define INIT_PTHREAD_SIGMASK
4606#endif
4607
4608#if SANITIZER_INTERCEPT_BACKTRACE
4609INTERCEPTOR(int, backtrace, void **buffer, int size) {
4610 void *ctx;
4611 COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
4612 // 'buffer' might be freed memory, hence it is unsafe to directly call
4613 // REAL(backtrace)(buffer, size). Instead, we use our own known-good
4614 // scratch buffer.
4615 void **scratch = (void**)InternalAlloc(size: sizeof(void*) * size);
4616 int res = REAL(backtrace)(scratch, size);
4617 if (res && buffer) {
4618 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
4619 internal_memcpy(dest: buffer, src: scratch, n: res * sizeof(*buffer));
4620 }
4621 InternalFree(p: scratch);
4622 return res;
4623}
4624
4625INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
4626 void *ctx;
4627 COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
4628 if (buffer && size)
4629 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
4630 // The COMMON_INTERCEPTOR_READ_RANGE above ensures that 'buffer' is
4631 // valid for reading.
4632 char **res = REAL(backtrace_symbols)(buffer, size);
4633 if (res && size) {
4634 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
4635 for (int i = 0; i < size; ++i)
4636 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], internal_strlen(res[i]) + 1);
4637 }
4638 return res;
4639}
4640#define INIT_BACKTRACE \
4641 COMMON_INTERCEPT_FUNCTION(backtrace); \
4642 COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
4643#else
4644#define INIT_BACKTRACE
4645#endif
4646
4647#if SANITIZER_INTERCEPT__EXIT
4648INTERCEPTOR(void, _exit, int status) {
4649 void *ctx;
4650 COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
4651 COMMON_INTERCEPTOR_USER_CALLBACK_START();
4652 int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
4653 COMMON_INTERCEPTOR_USER_CALLBACK_END();
4654 if (status == 0) status = status1;
4655 REAL(_exit)(status);
4656}
4657#define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
4658#else
4659#define INIT__EXIT
4660#endif
4661
4662#if SANITIZER_INTERCEPT___LIBC_MUTEX
4663INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
4664ALIAS(WRAP(pthread_setcancelstate));
4665
4666#define INIT___LIBC_THR_SETCANCELSTATE \
4667 COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
4668#else
4669#define INIT___LIBC_THR_SETCANCELSTATE
4670#endif
4671
4672#if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
4673static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
4674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
4675 if (mnt->mnt_fsname)
4676 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
4677 internal_strlen(mnt->mnt_fsname) + 1);
4678 if (mnt->mnt_dir)
4679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
4680 internal_strlen(mnt->mnt_dir) + 1);
4681 if (mnt->mnt_type)
4682 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
4683 internal_strlen(mnt->mnt_type) + 1);
4684 if (mnt->mnt_opts)
4685 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
4686 internal_strlen(mnt->mnt_opts) + 1);
4687}
4688#endif
4689
4690#if SANITIZER_INTERCEPT_GETMNTENT
4691INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
4692 void *ctx;
4693 COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
4694 __sanitizer_mntent *res = REAL(getmntent)(fp);
4695 if (res) write_mntent(ctx, mnt: res);
4696 return res;
4697}
4698#define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
4699#else
4700#define INIT_GETMNTENT
4701#endif
4702
4703#if SANITIZER_INTERCEPT_GETMNTENT_R
4704INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
4705 __sanitizer_mntent *mntbuf, char *buf, int buflen) {
4706 void *ctx;
4707 COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
4708 __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
4709 if (res) write_mntent(ctx, mnt: res);
4710 return res;
4711}
4712#define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
4713#else
4714#define INIT_GETMNTENT_R
4715#endif
4716
4717#if SANITIZER_INTERCEPT_STATFS
4718INTERCEPTOR(int, statfs, char *path, void *buf) {
4719 void *ctx;
4720 COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
4721 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4722 // FIXME: under ASan the call below may write to freed memory and corrupt
4723 // its metadata. See
4724 // https://github.com/google/sanitizers/issues/321.
4725 int res = REAL(statfs)(path, buf);
4726 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4727 return res;
4728}
4729INTERCEPTOR(int, fstatfs, int fd, void *buf) {
4730 void *ctx;
4731 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
4732 // FIXME: under ASan the call below may write to freed memory and corrupt
4733 // its metadata. See
4734 // https://github.com/google/sanitizers/issues/321.
4735 int res = REAL(fstatfs)(fd, buf);
4736 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
4737 return res;
4738}
4739#define INIT_STATFS \
4740 COMMON_INTERCEPT_FUNCTION(statfs); \
4741 COMMON_INTERCEPT_FUNCTION(fstatfs);
4742#else
4743#define INIT_STATFS
4744#endif
4745
4746#if SANITIZER_INTERCEPT_STATFS64
4747INTERCEPTOR(int, statfs64, char *path, void *buf) {
4748 void *ctx;
4749 COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
4750 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4751 // FIXME: under ASan the call below may write to freed memory and corrupt
4752 // its metadata. See
4753 // https://github.com/google/sanitizers/issues/321.
4754 int res = REAL(statfs64)(path, buf);
4755 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4756 return res;
4757}
4758INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
4759 void *ctx;
4760 COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
4761 // FIXME: under ASan the call below may write to freed memory and corrupt
4762 // its metadata. See
4763 // https://github.com/google/sanitizers/issues/321.
4764 int res = REAL(fstatfs64)(fd, buf);
4765 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
4766 return res;
4767}
4768#define INIT_STATFS64 \
4769 COMMON_INTERCEPT_FUNCTION(statfs64); \
4770 COMMON_INTERCEPT_FUNCTION(fstatfs64);
4771#else
4772#define INIT_STATFS64
4773#endif
4774
4775#if SANITIZER_INTERCEPT_STATVFS
4776INTERCEPTOR(int, statvfs, char *path, void *buf) {
4777 void *ctx;
4778 COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
4779 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
4780 // FIXME: under ASan the call below may write to freed memory and corrupt
4781 // its metadata. See
4782 // https://github.com/google/sanitizers/issues/321.
4783 int res = REAL(statvfs)(path, buf);
4784 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4785 return res;
4786}
4787INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
4788 void *ctx;
4789 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
4790 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
4791 // FIXME: under ASan the call below may write to freed memory and corrupt
4792 // its metadata. See
4793 // https://github.com/google/sanitizers/issues/321.
4794 int res = REAL(fstatvfs)(fd, buf);
4795 if (!res) {
4796 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
4797 if (fd >= 0)
4798 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
4799 }
4800 return res;
4801}
4802#define INIT_STATVFS \
4803 COMMON_INTERCEPT_FUNCTION(statvfs); \
4804 COMMON_INTERCEPT_FUNCTION(fstatvfs);
4805#else
4806#define INIT_STATVFS
4807#endif
4808
4809#if SANITIZER_INTERCEPT_STATVFS64
4810INTERCEPTOR(int, statvfs64, char *path, void *buf) {
4811 void *ctx;
4812 COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
4813 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
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(statvfs64)(path, buf);
4818 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4819 return res;
4820}
4821INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
4822 void *ctx;
4823 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
4824 // FIXME: under ASan the call below may write to freed memory and corrupt
4825 // its metadata. See
4826 // https://github.com/google/sanitizers/issues/321.
4827 int res = REAL(fstatvfs64)(fd, buf);
4828 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
4829 return res;
4830}
4831#define INIT_STATVFS64 \
4832 COMMON_INTERCEPT_FUNCTION(statvfs64); \
4833 COMMON_INTERCEPT_FUNCTION(fstatvfs64);
4834#else
4835#define INIT_STATVFS64
4836#endif
4837
4838#if SANITIZER_INTERCEPT_INITGROUPS
4839INTERCEPTOR(int, initgroups, char *user, u32 group) {
4840 void *ctx;
4841 COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
4842 if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, internal_strlen(user) + 1);
4843 int res = REAL(initgroups)(user, group);
4844 return res;
4845}
4846#define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
4847#else
4848#define INIT_INITGROUPS
4849#endif
4850
4851#if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
4852INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
4853 void *ctx;
4854 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
4855 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4856 char *res = REAL(ether_ntoa)(addr);
4857 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
4858 return res;
4859}
4860INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
4861 void *ctx;
4862 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
4863 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4864 __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
4865 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
4866 return res;
4867}
4868#define INIT_ETHER_NTOA_ATON \
4869 COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
4870 COMMON_INTERCEPT_FUNCTION(ether_aton);
4871#else
4872#define INIT_ETHER_NTOA_ATON
4873#endif
4874
4875#if SANITIZER_INTERCEPT_ETHER_HOST
4876INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
4877 void *ctx;
4878 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
4879 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4880 // FIXME: under ASan the call below may write to freed memory and corrupt
4881 // its metadata. See
4882 // https://github.com/google/sanitizers/issues/321.
4883 int res = REAL(ether_ntohost)(hostname, addr);
4884 if (!res && hostname)
4885 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4886 return res;
4887}
4888INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
4889 void *ctx;
4890 COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
4891 if (hostname)
4892 COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4893 // FIXME: under ASan the call below may write to freed memory and corrupt
4894 // its metadata. See
4895 // https://github.com/google/sanitizers/issues/321.
4896 int res = REAL(ether_hostton)(hostname, addr);
4897 if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4898 return res;
4899}
4900INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
4901 char *hostname) {
4902 void *ctx;
4903 COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
4904 if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, internal_strlen(line) + 1);
4905 // FIXME: under ASan the call below may write to freed memory and corrupt
4906 // its metadata. See
4907 // https://github.com/google/sanitizers/issues/321.
4908 int res = REAL(ether_line)(line, addr, hostname);
4909 if (!res) {
4910 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
4911 if (hostname)
4912 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
4913 }
4914 return res;
4915}
4916#define INIT_ETHER_HOST \
4917 COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
4918 COMMON_INTERCEPT_FUNCTION(ether_hostton); \
4919 COMMON_INTERCEPT_FUNCTION(ether_line);
4920#else
4921#define INIT_ETHER_HOST
4922#endif
4923
4924#if SANITIZER_INTERCEPT_ETHER_R
4925INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
4926 void *ctx;
4927 COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
4928 if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
4929 // FIXME: under ASan the call below may write to freed memory and corrupt
4930 // its metadata. See
4931 // https://github.com/google/sanitizers/issues/321.
4932 char *res = REAL(ether_ntoa_r)(addr, buf);
4933 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
4934 return res;
4935}
4936INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
4937 __sanitizer_ether_addr *addr) {
4938 void *ctx;
4939 COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
4940 if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
4941 // FIXME: under ASan the call below may write to freed memory and corrupt
4942 // its metadata. See
4943 // https://github.com/google/sanitizers/issues/321.
4944 __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
4945 if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
4946 return res;
4947}
4948#define INIT_ETHER_R \
4949 COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
4950 COMMON_INTERCEPT_FUNCTION(ether_aton_r);
4951#else
4952#define INIT_ETHER_R
4953#endif
4954
4955#if SANITIZER_INTERCEPT_SHMCTL
4956INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
4957 void *ctx;
4958 COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
4959 // FIXME: under ASan the call below may write to freed memory and corrupt
4960 // its metadata. See
4961 // https://github.com/google/sanitizers/issues/321.
4962 int res = REAL(shmctl)(shmid, cmd, buf);
4963 if (res >= 0) {
4964 unsigned sz = 0;
4965 if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
4966 sz = sizeof(__sanitizer_shmid_ds);
4967 else if (cmd == shmctl_ipc_info)
4968 sz = struct_shminfo_sz;
4969 else if (cmd == shmctl_shm_info)
4970 sz = struct_shm_info_sz;
4971 if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
4972 }
4973 return res;
4974}
4975#define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
4976#else
4977#define INIT_SHMCTL
4978#endif
4979
4980#if SANITIZER_INTERCEPT_RANDOM_R
4981INTERCEPTOR(int, random_r, void *buf, u32 *result) {
4982 void *ctx;
4983 COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
4984 // FIXME: under ASan the call below may write to freed memory and corrupt
4985 // its metadata. See
4986 // https://github.com/google/sanitizers/issues/321.
4987 int res = REAL(random_r)(buf, result);
4988 if (!res && result)
4989 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
4990 return res;
4991}
4992#define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
4993#else
4994#define INIT_RANDOM_R
4995#endif
4996
4997// FIXME: under ASan the REAL() call below may write to freed memory and corrupt
4998// its metadata. See
4999// https://github.com/google/sanitizers/issues/321.
5000#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
5001 SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
5002 SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
5003 SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
5004 SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
5005 SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
5006 SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
5007#define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
5008 INTERCEPTOR(int, fn, void *attr, void *r) { \
5009 void *ctx; \
5010 COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
5011 int res = REAL(fn)(attr, r); \
5012 if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
5013 return res; \
5014 }
5015#define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
5016 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
5017#define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
5018 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
5019#define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
5020 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
5021#define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
5022 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
5023#define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
5024 INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
5025#endif
5026
5027#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
5028INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
5029INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
5030INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
5031INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
5032INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
5033 void *ctx;
5034 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
5035 // FIXME: under ASan the call below may write to freed memory and corrupt
5036 // its metadata. See
5037 // https://github.com/google/sanitizers/issues/321.
5038 int res = REAL(pthread_attr_getstack)(attr, addr, size);
5039 if (!res) {
5040 if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
5041 if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
5042 }
5043 return res;
5044}
5045
5046// We may need to call the real pthread_attr_getstack from the run-time
5047// in sanitizer_common, but we don't want to include the interception headers
5048// there. So, just define this function here.
5049namespace __sanitizer {
5050extern "C" {
5051int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
5052 return REAL(pthread_attr_getstack)(attr, addr, size);
5053}
5054} // extern "C"
5055} // namespace __sanitizer
5056
5057#define INIT_PTHREAD_ATTR_GET \
5058 COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
5059 COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
5060 COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
5061 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
5062 COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
5063#else
5064#define INIT_PTHREAD_ATTR_GET
5065#endif
5066
5067#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
5068INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
5069INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
5070
5071#define INIT_PTHREAD_ATTR_GET_SCHED \
5072 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
5073 COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
5074#else
5075#define INIT_PTHREAD_ATTR_GET_SCHED
5076#endif
5077
5078#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
5079INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
5080
5081#define INIT_PTHREAD_ATTR_GETINHERITSCHED \
5082 COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
5083#else
5084#define INIT_PTHREAD_ATTR_GETINHERITSCHED
5085#endif
5086
5087#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
5088INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
5089 void *cpuset) {
5090 void *ctx;
5091 COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
5092 cpuset);
5093 // FIXME: under ASan the call below may write to freed memory and corrupt
5094 // its metadata. See
5095 // https://github.com/google/sanitizers/issues/321.
5096 int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
5097 if (!res && cpusetsize && cpuset)
5098 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
5099 return res;
5100}
5101
5102#define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
5103 COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
5104#else
5105#define INIT_PTHREAD_ATTR_GETAFFINITY_NP
5106#endif
5107
5108#if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
5109INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
5110 void *cpuset) {
5111 void *ctx;
5112 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
5113 cpuset);
5114 // FIXME: under ASan the call below may write to freed memory and corrupt
5115 // its metadata. See
5116 // https://github.com/google/sanitizers/issues/321.
5117 int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
5118 if (!res && cpusetsize && cpuset)
5119 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
5120 return res;
5121}
5122
5123#define INIT_PTHREAD_GETAFFINITY_NP \
5124 COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
5125#else
5126#define INIT_PTHREAD_GETAFFINITY_NP
5127#endif
5128
5129#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
5130INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
5131#define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
5132 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
5133#else
5134#define INIT_PTHREAD_MUTEXATTR_GETPSHARED
5135#endif
5136
5137#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
5138INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
5139#define INIT_PTHREAD_MUTEXATTR_GETTYPE \
5140 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
5141#else
5142#define INIT_PTHREAD_MUTEXATTR_GETTYPE
5143#endif
5144
5145#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
5146INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
5147#define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
5148 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
5149#else
5150#define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
5151#endif
5152
5153#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
5154INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
5155#define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
5156 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
5157#else
5158#define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
5159#endif
5160
5161#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
5162INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
5163#define INIT_PTHREAD_MUTEXATTR_GETROBUST \
5164 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
5165#else
5166#define INIT_PTHREAD_MUTEXATTR_GETROBUST
5167#endif
5168
5169#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
5170INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
5171#define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
5172 COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
5173#else
5174#define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
5175#endif
5176
5177#if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
5178INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
5179#define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
5180 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
5181#else
5182#define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
5183#endif
5184
5185#if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
5186INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
5187#define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
5188 COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
5189#else
5190#define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
5191#endif
5192
5193#if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
5194INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
5195#define INIT_PTHREAD_CONDATTR_GETPSHARED \
5196 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
5197#else
5198#define INIT_PTHREAD_CONDATTR_GETPSHARED
5199#endif
5200
5201#if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
5202INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
5203#define INIT_PTHREAD_CONDATTR_GETCLOCK \
5204 COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
5205#else
5206#define INIT_PTHREAD_CONDATTR_GETCLOCK
5207#endif
5208
5209#if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
5210INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
5211#define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
5212 COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
5213#else
5214#define INIT_PTHREAD_BARRIERATTR_GETPSHARED
5215#endif
5216
5217#if SANITIZER_INTERCEPT_TMPNAM
5218INTERCEPTOR(char *, tmpnam, char *s) {
5219 void *ctx;
5220 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
5221 char *res = REAL(tmpnam)(s);
5222 if (res) {
5223 if (s)
5224 // FIXME: under ASan the call below may write to freed memory and corrupt
5225 // its metadata. See
5226 // https://github.com/google/sanitizers/issues/321.
5227 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
5228 else
5229 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5230 }
5231 return res;
5232}
5233#define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
5234#else
5235#define INIT_TMPNAM
5236#endif
5237
5238#if SANITIZER_INTERCEPT_TMPNAM_R
5239INTERCEPTOR(char *, tmpnam_r, char *s) {
5240 void *ctx;
5241 COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
5242 // FIXME: under ASan the call below may write to freed memory and corrupt
5243 // its metadata. See
5244 // https://github.com/google/sanitizers/issues/321.
5245 char *res = REAL(tmpnam_r)(s);
5246 if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
5247 return res;
5248}
5249#define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
5250#else
5251#define INIT_TMPNAM_R
5252#endif
5253
5254#if SANITIZER_INTERCEPT_PTSNAME
5255INTERCEPTOR(char *, ptsname, int fd) {
5256 void *ctx;
5257 COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
5258 char *res = REAL(ptsname)(fd);
5259 if (res != nullptr)
5260 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5261 return res;
5262}
5263#define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
5264#else
5265#define INIT_PTSNAME
5266#endif
5267
5268#if SANITIZER_INTERCEPT_PTSNAME_R
5269INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
5270 void *ctx;
5271 COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
5272 int res = REAL(ptsname_r)(fd, name, namesize);
5273 if (res == 0)
5274 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5275 return res;
5276}
5277#define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
5278#else
5279#define INIT_PTSNAME_R
5280#endif
5281
5282#if SANITIZER_INTERCEPT_TTYNAME
5283INTERCEPTOR(char *, ttyname, int fd) {
5284 void *ctx;
5285 COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
5286 char *res = REAL(ttyname)(fd);
5287 if (res != nullptr)
5288 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5289 return res;
5290}
5291#define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
5292#else
5293#define INIT_TTYNAME
5294#endif
5295
5296#if SANITIZER_INTERCEPT_TTYNAME_R
5297INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
5298 void *ctx;
5299 COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
5300 int res = REAL(ttyname_r)(fd, name, namesize);
5301 if (res == 0)
5302 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
5303 return res;
5304}
5305#define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
5306#else
5307#define INIT_TTYNAME_R
5308#endif
5309
5310#if SANITIZER_INTERCEPT_TEMPNAM
5311INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
5312 void *ctx;
5313 COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
5314 if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, internal_strlen(dir) + 1);
5315 if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, internal_strlen(pfx) + 1);
5316 char *res = REAL(tempnam)(dir, pfx);
5317 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
5318 return res;
5319}
5320#define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
5321#else
5322#define INIT_TEMPNAM
5323#endif
5324
5325#if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
5326INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
5327 void *ctx;
5328 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
5329 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5330 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
5331 return REAL(pthread_setname_np)(thread, name);
5332}
5333#define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5334#elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
5335INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
5336 void *ctx;
5337 char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
5338 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
5339 COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
5340 internal_snprintf(newname, sizeof(newname), name, arg);
5341 COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
5342 return REAL(pthread_setname_np)(thread, name, arg);
5343}
5344#define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
5345#else
5346#define INIT_PTHREAD_SETNAME_NP
5347#endif
5348
5349#if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
5350INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
5351 void *ctx;
5352 COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
5353 int res = REAL(pthread_getname_np)(thread, name, len);
5354 if (!res)
5355 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
5356 return res;
5357}
5358#define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
5359#else
5360#define INIT_PTHREAD_GETNAME_NP
5361#endif
5362
5363#if SANITIZER_INTERCEPT_SINCOS
5364INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
5365 void *ctx;
5366 COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
5367 // FIXME: under ASan the call below may write to freed memory and corrupt
5368 // its metadata. See
5369 // https://github.com/google/sanitizers/issues/321.
5370 REAL(sincos)(x, sin, cos);
5371 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5372 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5373}
5374INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
5375 void *ctx;
5376 COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
5377 // FIXME: under ASan the call below may write to freed memory and corrupt
5378 // its metadata. See
5379 // https://github.com/google/sanitizers/issues/321.
5380 REAL(sincosf)(x, sin, cos);
5381 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5382 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5383}
5384INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
5385 void *ctx;
5386 COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
5387 // FIXME: under ASan the call below may write to freed memory and corrupt
5388 // its metadata. See
5389 // https://github.com/google/sanitizers/issues/321.
5390 REAL(sincosl)(x, sin, cos);
5391 if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
5392 if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
5393}
5394#define INIT_SINCOS \
5395 COMMON_INTERCEPT_FUNCTION(sincos); \
5396 COMMON_INTERCEPT_FUNCTION(sincosf); \
5397 COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
5398#else
5399#define INIT_SINCOS
5400#endif
5401
5402#if SANITIZER_INTERCEPT_REMQUO
5403INTERCEPTOR(double, remquo, double x, double y, int *quo) {
5404 void *ctx;
5405 COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
5406 // FIXME: under ASan the call below may write to freed memory and corrupt
5407 // its metadata. See
5408 // https://github.com/google/sanitizers/issues/321.
5409 double res = REAL(remquo)(x, y, quo);
5410 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5411 return res;
5412}
5413INTERCEPTOR(float, remquof, float x, float y, int *quo) {
5414 void *ctx;
5415 COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
5416 // FIXME: under ASan the call below may write to freed memory and corrupt
5417 // its metadata. See
5418 // https://github.com/google/sanitizers/issues/321.
5419 float res = REAL(remquof)(x, y, quo);
5420 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5421 return res;
5422}
5423#define INIT_REMQUO \
5424 COMMON_INTERCEPT_FUNCTION(remquo); \
5425 COMMON_INTERCEPT_FUNCTION(remquof);
5426#else
5427#define INIT_REMQUO
5428#endif
5429
5430#if SANITIZER_INTERCEPT_REMQUOL
5431INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
5432 void *ctx;
5433 COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
5434 // FIXME: under ASan the call below may write to freed memory and corrupt
5435 // its metadata. See
5436 // https://github.com/google/sanitizers/issues/321.
5437 long double res = REAL(remquol)(x, y, quo);
5438 if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
5439 return res;
5440}
5441#define INIT_REMQUOL \
5442 COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
5443#else
5444#define INIT_REMQUOL
5445#endif
5446
5447#if SANITIZER_INTERCEPT_LGAMMA
5448extern int signgam;
5449INTERCEPTOR(double, lgamma, double x) {
5450 void *ctx;
5451 COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
5452 double res = REAL(lgamma)(x);
5453 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5454 return res;
5455}
5456INTERCEPTOR(float, lgammaf, float x) {
5457 void *ctx;
5458 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
5459 float res = REAL(lgammaf)(x);
5460 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5461 return res;
5462}
5463#define INIT_LGAMMA \
5464 COMMON_INTERCEPT_FUNCTION(lgamma); \
5465 COMMON_INTERCEPT_FUNCTION(lgammaf);
5466#else
5467#define INIT_LGAMMA
5468#endif
5469
5470#if SANITIZER_INTERCEPT_LGAMMAL
5471INTERCEPTOR(long double, lgammal, long double x) {
5472 void *ctx;
5473 COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
5474 long double res = REAL(lgammal)(x);
5475 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
5476 return res;
5477}
5478#define INIT_LGAMMAL \
5479 COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
5480#else
5481#define INIT_LGAMMAL
5482#endif
5483
5484#if SANITIZER_INTERCEPT_LGAMMA_R
5485INTERCEPTOR(double, lgamma_r, double x, int *signp) {
5486 void *ctx;
5487 COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
5488 // FIXME: under ASan the call below may write to freed memory and corrupt
5489 // its metadata. See
5490 // https://github.com/google/sanitizers/issues/321.
5491 double res = REAL(lgamma_r)(x, signp);
5492 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5493 return res;
5494}
5495INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
5496 void *ctx;
5497 COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
5498 // FIXME: under ASan the call below may write to freed memory and corrupt
5499 // its metadata. See
5500 // https://github.com/google/sanitizers/issues/321.
5501 float res = REAL(lgammaf_r)(x, signp);
5502 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5503 return res;
5504}
5505#define INIT_LGAMMA_R \
5506 COMMON_INTERCEPT_FUNCTION(lgamma_r); \
5507 COMMON_INTERCEPT_FUNCTION(lgammaf_r);
5508#else
5509#define INIT_LGAMMA_R
5510#endif
5511
5512#if SANITIZER_INTERCEPT_LGAMMAL_R
5513INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
5514 void *ctx;
5515 COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
5516 // FIXME: under ASan the call below may write to freed memory and corrupt
5517 // its metadata. See
5518 // https://github.com/google/sanitizers/issues/321.
5519 long double res = REAL(lgammal_r)(x, signp);
5520 if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
5521 return res;
5522}
5523#define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
5524#else
5525#define INIT_LGAMMAL_R
5526#endif
5527
5528#if SANITIZER_INTERCEPT_DRAND48_R
5529INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
5530 void *ctx;
5531 COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
5532 // FIXME: under ASan the call below may write to freed memory and corrupt
5533 // its metadata. See
5534 // https://github.com/google/sanitizers/issues/321.
5535 int res = REAL(drand48_r)(buffer, result);
5536 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5537 return res;
5538}
5539INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
5540 void *ctx;
5541 COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
5542 // FIXME: under ASan the call below may write to freed memory and corrupt
5543 // its metadata. See
5544 // https://github.com/google/sanitizers/issues/321.
5545 int res = REAL(lrand48_r)(buffer, result);
5546 if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
5547 return res;
5548}
5549#define INIT_DRAND48_R \
5550 COMMON_INTERCEPT_FUNCTION(drand48_r); \
5551 COMMON_INTERCEPT_FUNCTION(lrand48_r);
5552#else
5553#define INIT_DRAND48_R
5554#endif
5555
5556#if SANITIZER_INTERCEPT_RAND_R
5557INTERCEPTOR(int, rand_r, unsigned *seedp) {
5558 void *ctx;
5559 COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
5560 COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
5561 return REAL(rand_r)(seedp);
5562}
5563#define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
5564#else
5565#define INIT_RAND_R
5566#endif
5567
5568#if SANITIZER_INTERCEPT_GETLINE
5569INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
5570 void *ctx;
5571 COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
5572 // FIXME: under ASan the call below may write to freed memory and corrupt
5573 // its metadata. See
5574 // https://github.com/google/sanitizers/issues/321.
5575 SSIZE_T res = REAL(getline)(lineptr, n, stream);
5576 if (res > 0) {
5577 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
5578 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
5579 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
5580 }
5581 return res;
5582}
5583
5584// FIXME: under ASan the call below may write to freed memory and corrupt its
5585// metadata. See
5586// https://github.com/google/sanitizers/issues/321.
5587#define GETDELIM_INTERCEPTOR_IMPL(vname) \
5588 { \
5589 void *ctx; \
5590 COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
5591 SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
5592 if (res > 0) { \
5593 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
5594 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
5595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
5596 } \
5597 return res; \
5598 }
5599
5600INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
5601 void *stream)
5602GETDELIM_INTERCEPTOR_IMPL(__getdelim)
5603
5604// There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
5605// with its own body.
5606INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
5607 void *stream)
5608GETDELIM_INTERCEPTOR_IMPL(getdelim)
5609
5610#define INIT_GETLINE \
5611 COMMON_INTERCEPT_FUNCTION(getline); \
5612 COMMON_INTERCEPT_FUNCTION(__getdelim); \
5613 COMMON_INTERCEPT_FUNCTION(getdelim);
5614#else
5615#define INIT_GETLINE
5616#endif
5617
5618#if SANITIZER_INTERCEPT_ICONV
5619INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
5620 char **outbuf, SIZE_T *outbytesleft) {
5621 void *ctx;
5622 COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
5623 outbytesleft);
5624 if (inbytesleft)
5625 COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
5626 if (inbuf && inbytesleft)
5627 COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
5628 if (outbytesleft)
5629 COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
5630 void *outbuf_orig = outbuf ? *outbuf : nullptr;
5631 // FIXME: under ASan the call below may write to freed memory and corrupt
5632 // its metadata. See
5633 // https://github.com/google/sanitizers/issues/321.
5634 SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
5635 if (outbuf && *outbuf > outbuf_orig) {
5636 SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
5637 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
5638 }
5639 return res;
5640}
5641#define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
5642#else
5643#define INIT_ICONV
5644#endif
5645
5646#if SANITIZER_INTERCEPT_TIMES
5647INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
5648 void *ctx;
5649 COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
5650 // FIXME: under ASan the call below may write to freed memory and corrupt
5651 // its metadata. See
5652 // https://github.com/google/sanitizers/issues/321.
5653 __sanitizer_clock_t res = REAL(times)(tms);
5654 if (res != (__sanitizer_clock_t)-1 && tms)
5655 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
5656 return res;
5657}
5658#define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
5659#else
5660#define INIT_TIMES
5661#endif
5662
5663#if SANITIZER_S390 && \
5664 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5665extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
5666DEFINE_REAL(uptr, __tls_get_offset, void *arg)
5667#endif
5668
5669#if SANITIZER_INTERCEPT_TLS_GET_ADDR
5670#if !SANITIZER_S390
5671#define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
5672// If you see any crashes around this functions, there are 2 known issues with
5673// it: 1. __tls_get_addr can be called with mis-aligned stack due to:
5674// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
5675// 2. It can be called recursively if sanitizer code uses __tls_get_addr
5676// to access thread local variables (it should not happen normally,
5677// because sanitizers use initial-exec tls model).
5678INTERCEPTOR(void *, __tls_get_addr, void *arg) {
5679 void *ctx;
5680 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
5681 void *res = REAL(__tls_get_addr)(arg);
5682 uptr tls_begin, tls_end;
5683 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5684 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, static_tls_begin: tls_begin, static_tls_end: tls_end);
5685 if (dtv) {
5686 // New DTLS block has been allocated.
5687 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5688 }
5689 return res;
5690}
5691#if SANITIZER_PPC
5692// On PowerPC, we also need to intercept __tls_get_addr_opt, which has
5693// mostly the same semantics as __tls_get_addr, but its presence enables
5694// some optimizations in linker (which are safe to ignore here).
5695INTERCEPTOR(void *, __tls_get_addr_opt, void *arg) ALIAS(WRAP(__tls_get_addr));
5696#endif
5697#else // SANITIZER_S390
5698// On s390, we have to intercept two functions here:
5699// - __tls_get_addr_internal, which is a glibc-internal function that is like
5700// the usual __tls_get_addr, but returns a TP-relative offset instead of
5701// a proper pointer. It is used by dlsym for TLS symbols.
5702// - __tls_get_offset, which is like the above, but also takes a GOT-relative
5703// descriptor offset as an argument instead of a pointer. GOT address
5704// is passed in r12, so it's necessary to write it in assembly. This is
5705// the function used by the compiler.
5706#define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
5707INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
5708 void *ctx;
5709 COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
5710 uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
5711 uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
5712 void *ptr = reinterpret_cast<void *>(res + tp);
5713 uptr tls_begin, tls_end;
5714 COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
5715 DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
5716 if (dtv) {
5717 // New DTLS block has been allocated.
5718 COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
5719 }
5720 return res;
5721}
5722#endif // SANITIZER_S390
5723#else
5724#define INIT_TLS_GET_ADDR
5725#endif
5726
5727#if SANITIZER_S390 && \
5728 (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
5729// We need a hidden symbol aliasing the above, so that we can jump
5730// directly to it from the assembly below.
5731extern "C" __attribute__((visibility("hidden"))) uptr __tls_get_addr_hidden(
5732 void *arg) ALIAS(WRAP(__tls_get_addr_internal));
5733extern "C" uptr __tls_get_offset(void *arg);
5734extern "C" uptr TRAMPOLINE(__tls_get_offset)(void *arg);
5735extern "C" uptr WRAP(__tls_get_offset)(void *arg);
5736// Now carefully intercept __tls_get_offset.
5737asm(
5738 ".text\n"
5739// The __intercept_ version has to exist, so that gen_dynamic_list.py
5740// exports our symbol.
5741 ".weak __tls_get_offset\n"
5742 ".set __tls_get_offset, __interceptor___tls_get_offset\n"
5743 ".global __interceptor___tls_get_offset\n"
5744 ".type __interceptor___tls_get_offset, @function\n"
5745 "__interceptor___tls_get_offset:\n"
5746#ifdef __s390x__
5747 "la %r2, 0(%r2,%r12)\n"
5748 "jg __tls_get_addr_hidden\n"
5749#else
5750 "basr %r3,0\n"
5751 "0: la %r2,0(%r2,%r12)\n"
5752 "l %r4,1f-0b(%r3)\n"
5753 "b 0(%r4,%r3)\n"
5754 "1: .long __tls_get_addr_hidden - 0b\n"
5755#endif
5756 ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
5757// Assembly wrapper to call REAL(__tls_get_offset)(arg)
5758 ".type __tls_get_offset_wrapper, @function\n"
5759 "__tls_get_offset_wrapper:\n"
5760#ifdef __s390x__
5761 "sgr %r2,%r12\n"
5762#else
5763 "sr %r2,%r12\n"
5764#endif
5765 "br %r3\n"
5766 ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
5767);
5768#endif
5769
5770#if SANITIZER_INTERCEPT_LISTXATTR
5771INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
5772 void *ctx;
5773 COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
5774 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5775 // FIXME: under ASan the call below may write to freed memory and corrupt
5776 // its metadata. See
5777 // https://github.com/google/sanitizers/issues/321.
5778 SSIZE_T res = REAL(listxattr)(path, list, size);
5779 // Here and below, size == 0 is a special case where nothing is written to the
5780 // buffer, and res contains the desired buffer size.
5781 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5782 return res;
5783}
5784INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
5785 void *ctx;
5786 COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
5787 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5788 // FIXME: under ASan the call below may write to freed memory and corrupt
5789 // its metadata. See
5790 // https://github.com/google/sanitizers/issues/321.
5791 SSIZE_T res = REAL(llistxattr)(path, list, size);
5792 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5793 return res;
5794}
5795INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
5796 void *ctx;
5797 COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
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(flistxattr)(fd, list, size);
5802 if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
5803 return res;
5804}
5805#define INIT_LISTXATTR \
5806 COMMON_INTERCEPT_FUNCTION(listxattr); \
5807 COMMON_INTERCEPT_FUNCTION(llistxattr); \
5808 COMMON_INTERCEPT_FUNCTION(flistxattr);
5809#else
5810#define INIT_LISTXATTR
5811#endif
5812
5813#if SANITIZER_INTERCEPT_GETXATTR
5814INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
5815 SIZE_T size) {
5816 void *ctx;
5817 COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
5818 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5819 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5820 // FIXME: under ASan the call below may write to freed memory and corrupt
5821 // its metadata. See
5822 // https://github.com/google/sanitizers/issues/321.
5823 SSIZE_T res = REAL(getxattr)(path, name, value, size);
5824 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5825 return res;
5826}
5827INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
5828 SIZE_T size) {
5829 void *ctx;
5830 COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
5831 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
5832 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5833 // FIXME: under ASan the call below may write to freed memory and corrupt
5834 // its metadata. See
5835 // https://github.com/google/sanitizers/issues/321.
5836 SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
5837 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5838 return res;
5839}
5840INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
5841 SIZE_T size) {
5842 void *ctx;
5843 COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
5844 if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
5845 // FIXME: under ASan the call below may write to freed memory and corrupt
5846 // its metadata. See
5847 // https://github.com/google/sanitizers/issues/321.
5848 SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
5849 if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
5850 return res;
5851}
5852#define INIT_GETXATTR \
5853 COMMON_INTERCEPT_FUNCTION(getxattr); \
5854 COMMON_INTERCEPT_FUNCTION(lgetxattr); \
5855 COMMON_INTERCEPT_FUNCTION(fgetxattr);
5856#else
5857#define INIT_GETXATTR
5858#endif
5859
5860#if SANITIZER_INTERCEPT_GETRESID
5861INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
5862 void *ctx;
5863 COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
5864 // FIXME: under ASan the call below may write to freed memory and corrupt
5865 // its metadata. See
5866 // https://github.com/google/sanitizers/issues/321.
5867 int res = REAL(getresuid)(ruid, euid, suid);
5868 if (res >= 0) {
5869 if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
5870 if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
5871 if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
5872 }
5873 return res;
5874}
5875INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
5876 void *ctx;
5877 COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
5878 // FIXME: under ASan the call below may write to freed memory and corrupt
5879 // its metadata. See
5880 // https://github.com/google/sanitizers/issues/321.
5881 int res = REAL(getresgid)(rgid, egid, sgid);
5882 if (res >= 0) {
5883 if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
5884 if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
5885 if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
5886 }
5887 return res;
5888}
5889#define INIT_GETRESID \
5890 COMMON_INTERCEPT_FUNCTION(getresuid); \
5891 COMMON_INTERCEPT_FUNCTION(getresgid);
5892#else
5893#define INIT_GETRESID
5894#endif
5895
5896#if SANITIZER_INTERCEPT_GETIFADDRS
5897// As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
5898// intercept freeifaddrs(). If that ceases to be the case, we might need to
5899// intercept it to poison the memory again.
5900INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
5901 void *ctx;
5902 COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
5903 // FIXME: under ASan the call below may write to freed memory and corrupt
5904 // its metadata. See
5905 // https://github.com/google/sanitizers/issues/321.
5906 int res = REAL(getifaddrs)(ifap);
5907 if (res == 0 && ifap) {
5908 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
5909 __sanitizer_ifaddrs *p = *ifap;
5910 while (p) {
5911 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
5912 if (p->ifa_name)
5913 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
5914 internal_strlen(p->ifa_name) + 1);
5915 if (p->ifa_addr)
5916 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
5917 if (p->ifa_netmask)
5918 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
5919 // On Linux this is a union, but the other member also points to a
5920 // struct sockaddr, so the following is sufficient.
5921 if (p->ifa_dstaddr)
5922 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
5923 // FIXME(smatveev): Unpoison p->ifa_data as well.
5924 p = p->ifa_next;
5925 }
5926 }
5927 return res;
5928}
5929#define INIT_GETIFADDRS \
5930 COMMON_INTERCEPT_FUNCTION(getifaddrs);
5931#else
5932#define INIT_GETIFADDRS
5933#endif
5934
5935#if SANITIZER_INTERCEPT_IF_INDEXTONAME
5936INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
5937 void *ctx;
5938 COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
5939 // FIXME: under ASan the call below may write to freed memory and corrupt
5940 // its metadata. See
5941 // https://github.com/google/sanitizers/issues/321.
5942 char *res = REAL(if_indextoname)(ifindex, ifname);
5943 if (res && ifname)
5944 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5945 return res;
5946}
5947INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
5948 void *ctx;
5949 COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
5950 if (ifname)
5951 COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
5952 return REAL(if_nametoindex)(ifname);
5953}
5954#define INIT_IF_INDEXTONAME \
5955 COMMON_INTERCEPT_FUNCTION(if_indextoname); \
5956 COMMON_INTERCEPT_FUNCTION(if_nametoindex);
5957#else
5958#define INIT_IF_INDEXTONAME
5959#endif
5960
5961#if SANITIZER_INTERCEPT_CAPGET
5962INTERCEPTOR(int, capget, void *hdrp, void *datap) {
5963 void *ctx;
5964 COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
5965 if (hdrp)
5966 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5967 // FIXME: under ASan the call below may write to freed memory and corrupt
5968 // its metadata. See
5969 // https://github.com/google/sanitizers/issues/321.
5970 int res = REAL(capget)(hdrp, datap);
5971 if (res == 0 && datap) {
5972 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5973 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, datasz);
5974 }
5975 // We can also return -1 and write to hdrp->version if the version passed in
5976 // hdrp->version is unsupported. But that's not a trivial condition to check,
5977 // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
5978 return res;
5979}
5980INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
5981 void *ctx;
5982 COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
5983 if (hdrp)
5984 COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
5985 if (datap) {
5986 unsigned datasz = __user_cap_data_struct_sz(hdrp);
5987 COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, datasz);
5988 }
5989 return REAL(capset)(hdrp, datap);
5990}
5991#define INIT_CAPGET \
5992 COMMON_INTERCEPT_FUNCTION(capget); \
5993 COMMON_INTERCEPT_FUNCTION(capset);
5994#else
5995#define INIT_CAPGET
5996#endif
5997
5998#if SANITIZER_INTERCEPT_FTIME
5999INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
6000 void *ctx;
6001 COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
6002 // FIXME: under ASan the call below may write to freed memory and corrupt
6003 // its metadata. See
6004 // https://github.com/google/sanitizers/issues/321.
6005 int res = REAL(ftime)(tp);
6006 if (tp)
6007 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
6008 return res;
6009}
6010#define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
6011#else
6012#define INIT_FTIME
6013#endif // SANITIZER_INTERCEPT_FTIME
6014
6015#if SANITIZER_INTERCEPT_XDR
6016INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
6017 unsigned size, int op) {
6018 void *ctx;
6019 COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
6020 // FIXME: under ASan the call below may write to freed memory and corrupt
6021 // its metadata. See
6022 // https://github.com/google/sanitizers/issues/321.
6023 REAL(xdrmem_create)(xdrs, addr, size, op);
6024 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
6025 if (op == __sanitizer_XDR_ENCODE) {
6026 // It's not obvious how much data individual xdr_ routines write.
6027 // Simply unpoison the entire target buffer in advance.
6028 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
6029 }
6030}
6031
6032INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
6033 void *ctx;
6034 COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
6035 // FIXME: under ASan the call below may write to freed memory and corrupt
6036 // its metadata. See
6037 // https://github.com/google/sanitizers/issues/321.
6038 REAL(xdrstdio_create)(xdrs, file, op);
6039 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
6040}
6041
6042// FIXME: under ASan the call below may write to freed memory and corrupt
6043// its metadata. See
6044// https://github.com/google/sanitizers/issues/321.
6045#define XDR_INTERCEPTOR(F, T) \
6046 INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
6047 void *ctx; \
6048 COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
6049 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
6050 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
6051 int res = REAL(F)(xdrs, p); \
6052 if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
6053 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
6054 return res; \
6055 }
6056
6057XDR_INTERCEPTOR(xdr_short, short)
6058XDR_INTERCEPTOR(xdr_u_short, unsigned short)
6059XDR_INTERCEPTOR(xdr_int, int)
6060XDR_INTERCEPTOR(xdr_u_int, unsigned)
6061XDR_INTERCEPTOR(xdr_long, long)
6062XDR_INTERCEPTOR(xdr_u_long, unsigned long)
6063XDR_INTERCEPTOR(xdr_hyper, long long)
6064XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
6065XDR_INTERCEPTOR(xdr_longlong_t, long long)
6066XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
6067XDR_INTERCEPTOR(xdr_int8_t, u8)
6068XDR_INTERCEPTOR(xdr_uint8_t, u8)
6069XDR_INTERCEPTOR(xdr_int16_t, u16)
6070XDR_INTERCEPTOR(xdr_uint16_t, u16)
6071XDR_INTERCEPTOR(xdr_int32_t, u32)
6072XDR_INTERCEPTOR(xdr_uint32_t, u32)
6073XDR_INTERCEPTOR(xdr_int64_t, u64)
6074XDR_INTERCEPTOR(xdr_uint64_t, u64)
6075XDR_INTERCEPTOR(xdr_quad_t, long long)
6076XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
6077XDR_INTERCEPTOR(xdr_bool, bool)
6078XDR_INTERCEPTOR(xdr_enum, int)
6079XDR_INTERCEPTOR(xdr_char, char)
6080XDR_INTERCEPTOR(xdr_u_char, unsigned char)
6081XDR_INTERCEPTOR(xdr_float, float)
6082XDR_INTERCEPTOR(xdr_double, double)
6083
6084// FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
6085// wrapstring, sizeof
6086
6087INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
6088 unsigned maxsize) {
6089 void *ctx;
6090 COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
6091 if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
6092 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
6093 COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
6094 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
6095 }
6096 // FIXME: under ASan the call below may write to freed memory and corrupt
6097 // its metadata. See
6098 // https://github.com/google/sanitizers/issues/321.
6099 int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
6100 if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
6101 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
6102 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
6103 if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
6104 }
6105 return res;
6106}
6107
6108INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
6109 unsigned maxsize) {
6110 void *ctx;
6111 COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
6112 if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
6113 COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
6114 COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, internal_strlen(*p) + 1);
6115 }
6116 // FIXME: under ASan the call below may write to freed memory and corrupt
6117 // its metadata. See
6118 // https://github.com/google/sanitizers/issues/321.
6119 int res = REAL(xdr_string)(xdrs, p, maxsize);
6120 if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
6121 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
6122 if (res && *p)
6123 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
6124 }
6125 return res;
6126}
6127
6128#define INIT_XDR \
6129 COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
6130 COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
6131 COMMON_INTERCEPT_FUNCTION(xdr_short); \
6132 COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
6133 COMMON_INTERCEPT_FUNCTION(xdr_int); \
6134 COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
6135 COMMON_INTERCEPT_FUNCTION(xdr_long); \
6136 COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
6137 COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
6138 COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
6139 COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
6140 COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
6141 COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
6142 COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
6143 COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
6144 COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
6145 COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
6146 COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
6147 COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
6148 COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
6149 COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
6150 COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
6151 COMMON_INTERCEPT_FUNCTION(xdr_bool); \
6152 COMMON_INTERCEPT_FUNCTION(xdr_enum); \
6153 COMMON_INTERCEPT_FUNCTION(xdr_char); \
6154 COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
6155 COMMON_INTERCEPT_FUNCTION(xdr_float); \
6156 COMMON_INTERCEPT_FUNCTION(xdr_double); \
6157 COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
6158 COMMON_INTERCEPT_FUNCTION(xdr_string);
6159#else
6160#define INIT_XDR
6161#endif // SANITIZER_INTERCEPT_XDR
6162
6163#if SANITIZER_INTERCEPT_XDRREC
6164typedef int (*xdrrec_cb)(char*, char*, int);
6165struct XdrRecWrapper {
6166 char *handle;
6167 xdrrec_cb rd, wr;
6168};
6169typedef AddrHashMap<XdrRecWrapper *, 11> XdrRecWrapMap;
6170static XdrRecWrapMap *xdrrec_wrap_map;
6171
6172static int xdrrec_wr_wrap(char *handle, char *buf, int count) {
6173 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6174 COMMON_INTERCEPTOR_INITIALIZE_RANGE(buf, count);
6175 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
6176 return wrap->wr(wrap->handle, buf, count);
6177}
6178
6179static int xdrrec_rd_wrap(char *handle, char *buf, int count) {
6180 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6181 XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
6182 return wrap->rd(wrap->handle, buf, count);
6183}
6184
6185// This doesn't apply to the solaris version as it has a different function
6186// signature.
6187INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
6188 unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
6189 int (*wr)(char*, char*, int)) {
6190 void *ctx;
6191 COMMON_INTERCEPTOR_ENTER(ctx, xdrrec_create, xdr, sndsize, rcvsize,
6192 handle, rd, wr);
6193 COMMON_INTERCEPTOR_READ_RANGE(ctx, &xdr->x_op, sizeof xdr->x_op);
6194
6195 // We can't allocate a wrapper on the stack, as the handle is used outside
6196 // this stack frame. So we put it on the heap, and keep track of it with
6197 // the HashMap (keyed by x_private). When we later need to xdr_destroy,
6198 // we can index the map, free the wrapper, and then clean the map entry.
6199 XdrRecWrapper *wrap_data =
6200 (XdrRecWrapper *)InternalAlloc(size: sizeof(XdrRecWrapper));
6201 wrap_data->handle = handle;
6202 wrap_data->rd = rd;
6203 wrap_data->wr = wr;
6204 if (wr)
6205 wr = xdrrec_wr_wrap;
6206 if (rd)
6207 rd = xdrrec_rd_wrap;
6208 handle = (char *)wrap_data;
6209
6210 REAL(xdrrec_create)(xdr, sndsize, rcvsize, handle, rd, wr);
6211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdr, sizeof *xdr);
6212
6213 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, false, true);
6214 *wrap = wrap_data;
6215}
6216
6217// We have to intercept this to be able to free wrapper memory;
6218// otherwise it's not necessary.
6219INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {
6220 void *ctx;
6221 COMMON_INTERCEPTOR_ENTER(ctx, xdr_destroy, xdr);
6222
6223 XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, true);
6224 InternalFree(p: *wrap);
6225 REAL(xdr_destroy)(xdr);
6226}
6227#define INIT_XDRREC_LINUX \
6228 static u64 xdrrec_wrap_mem[sizeof(XdrRecWrapMap) / sizeof(u64) + 1]; \
6229 xdrrec_wrap_map = new ((void *)&xdrrec_wrap_mem) XdrRecWrapMap(); \
6230 COMMON_INTERCEPT_FUNCTION(xdrrec_create); \
6231 COMMON_INTERCEPT_FUNCTION(xdr_destroy);
6232#else
6233#define INIT_XDRREC_LINUX
6234#endif
6235
6236#if SANITIZER_INTERCEPT_TSEARCH
6237INTERCEPTOR(void *, tsearch, void *key, void **rootp,
6238 int (*compar)(const void *, const void *)) {
6239 void *ctx;
6240 COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
6241 // FIXME: under ASan the call below may write to freed memory and corrupt
6242 // its metadata. See
6243 // https://github.com/google/sanitizers/issues/321.
6244 void *res = REAL(tsearch)(key, rootp, compar);
6245 if (res && *(void **)res == key)
6246 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
6247 return res;
6248}
6249#define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
6250#else
6251#define INIT_TSEARCH
6252#endif
6253
6254#if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
6255 SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6256void unpoison_file(__sanitizer_FILE *fp) {
6257#if SANITIZER_HAS_STRUCT_FILE
6258 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
6259#if SANITIZER_NETBSD
6260 if (fp->_bf._base && fp->_bf._size > 0)
6261 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
6262 fp->_bf._size);
6263#else
6264 if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
6265 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
6266 fp->_IO_read_end - fp->_IO_read_base);
6267 if (fp->_IO_write_base && fp->_IO_write_base < fp->_IO_write_end)
6268 COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_write_base,
6269 fp->_IO_write_end - fp->_IO_write_base);
6270#endif
6271#endif // SANITIZER_HAS_STRUCT_FILE
6272}
6273#endif
6274
6275#if SANITIZER_INTERCEPT_LIBIO_INTERNALS
6276// These guys are called when a .c source is built with -O2.
6277INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
6278 void *ctx;
6279 COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
6280 int res = REAL(__uflow)(fp);
6281 unpoison_file(fp);
6282 return res;
6283}
6284INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
6285 void *ctx;
6286 COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
6287 int res = REAL(__underflow)(fp);
6288 unpoison_file(fp);
6289 return res;
6290}
6291INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
6292 void *ctx;
6293 COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
6294 int res = REAL(__overflow)(fp, ch);
6295 unpoison_file(fp);
6296 return res;
6297}
6298INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
6299 void *ctx;
6300 COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
6301 int res = REAL(__wuflow)(fp);
6302 unpoison_file(fp);
6303 return res;
6304}
6305INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
6306 void *ctx;
6307 COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
6308 int res = REAL(__wunderflow)(fp);
6309 unpoison_file(fp);
6310 return res;
6311}
6312INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
6313 void *ctx;
6314 COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
6315 int res = REAL(__woverflow)(fp, ch);
6316 unpoison_file(fp);
6317 return res;
6318}
6319#define INIT_LIBIO_INTERNALS \
6320 COMMON_INTERCEPT_FUNCTION(__uflow); \
6321 COMMON_INTERCEPT_FUNCTION(__underflow); \
6322 COMMON_INTERCEPT_FUNCTION(__overflow); \
6323 COMMON_INTERCEPT_FUNCTION(__wuflow); \
6324 COMMON_INTERCEPT_FUNCTION(__wunderflow); \
6325 COMMON_INTERCEPT_FUNCTION(__woverflow);
6326#else
6327#define INIT_LIBIO_INTERNALS
6328#endif
6329
6330#if SANITIZER_INTERCEPT_FOPEN
6331INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
6332 void *ctx;
6333 COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
6334 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6335 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6336 __sanitizer_FILE *res = REAL(fopen)(path, mode);
6337 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6338 if (res) unpoison_file(fp: res);
6339 return res;
6340}
6341INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
6342 void *ctx;
6343 COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
6344 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6345 __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
6346 if (res) unpoison_file(fp: res);
6347 return res;
6348}
6349INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
6350 __sanitizer_FILE *fp) {
6351 void *ctx;
6352 COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
6353 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6354 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6355 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6356 __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
6357 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6358 if (res) unpoison_file(fp: res);
6359 return res;
6360}
6361#define INIT_FOPEN \
6362 COMMON_INTERCEPT_FUNCTION(fopen); \
6363 COMMON_INTERCEPT_FUNCTION(fdopen); \
6364 COMMON_INTERCEPT_FUNCTION(freopen);
6365#else
6366#define INIT_FOPEN
6367#endif
6368
6369#if SANITIZER_INTERCEPT_FLOPEN
6370INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
6371 void *ctx;
6372 va_list ap;
6373 va_start(ap, flags);
6374 u16 mode = static_cast<u16>(va_arg(ap, u32));
6375 va_end(ap);
6376 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6377 if (path) {
6378 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6379 }
6380 return COMMON_INTERCEPTOR_BLOCK_REAL(flopen)(path, flags, mode);
6381}
6382
6383INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
6384 void *ctx;
6385 va_list ap;
6386 va_start(ap, flags);
6387 u16 mode = static_cast<u16>(va_arg(ap, u32));
6388 va_end(ap);
6389 COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
6390 if (path) {
6391 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6392 }
6393 return COMMON_INTERCEPTOR_BLOCK_REAL(flopenat)(dirfd, path, flags, mode);
6394}
6395
6396#define INIT_FLOPEN \
6397 COMMON_INTERCEPT_FUNCTION(flopen); \
6398 COMMON_INTERCEPT_FUNCTION(flopenat);
6399#else
6400#define INIT_FLOPEN
6401#endif
6402
6403#if SANITIZER_INTERCEPT_FOPEN64
6404INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
6405 void *ctx;
6406 COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
6407 if (path)
6408 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6409 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6410 __sanitizer_FILE *res = REAL(fopen64)(path, mode);
6411 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6412 if (res) unpoison_file(fp: res);
6413 return res;
6414}
6415INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
6416 __sanitizer_FILE *fp) {
6417 void *ctx;
6418 COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
6419 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
6420 COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
6421 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6422 __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
6423 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
6424 if (res) unpoison_file(fp: res);
6425 return res;
6426}
6427#define INIT_FOPEN64 \
6428 COMMON_INTERCEPT_FUNCTION(fopen64); \
6429 COMMON_INTERCEPT_FUNCTION(freopen64);
6430#else
6431#define INIT_FOPEN64
6432#endif
6433
6434#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
6435INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
6436 void *ctx;
6437 COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
6438 // FIXME: under ASan the call below may write to freed memory and corrupt
6439 // its metadata. See
6440 // https://github.com/google/sanitizers/issues/321.
6441 __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
6442 if (res) {
6443 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6444 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6445 unpoison_file(fp: res);
6446 FileMetadata file = {.addr: ptr, .size: sizeloc};
6447 SetInterceptorMetadata(addr: res, file);
6448 }
6449 return res;
6450}
6451INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
6452 SIZE_T *sizeloc) {
6453 void *ctx;
6454 COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
6455 __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
6456 if (res) {
6457 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
6458 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
6459 unpoison_file(fp: res);
6460 FileMetadata file = {.addr: (char **)ptr, .size: sizeloc};
6461 SetInterceptorMetadata(addr: res, file);
6462 }
6463 return res;
6464}
6465INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
6466 const char *mode) {
6467 void *ctx;
6468 COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
6469 // FIXME: under ASan the call below may write to freed memory and corrupt
6470 // its metadata. See
6471 // https://github.com/google/sanitizers/issues/321.
6472 __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
6473 if (res) unpoison_file(fp: res);
6474 return res;
6475}
6476#define INIT_OPEN_MEMSTREAM \
6477 COMMON_INTERCEPT_FUNCTION(open_memstream); \
6478 COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
6479 COMMON_INTERCEPT_FUNCTION(fmemopen);
6480#else
6481#define INIT_OPEN_MEMSTREAM
6482#endif
6483
6484#if SANITIZER_INTERCEPT_OBSTACK
6485static void initialize_obstack(__sanitizer_obstack *obstack) {
6486 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
6487 if (obstack->chunk)
6488 COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
6489 sizeof(*obstack->chunk));
6490}
6491
6492INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
6493 int align, void *(*alloc_fn)(uptr arg, uptr sz),
6494 void (*free_fn)(uptr arg, void *p)) {
6495 void *ctx;
6496 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
6497 free_fn);
6498 int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
6499 if (res) initialize_obstack(obstack);
6500 return res;
6501}
6502INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
6503 int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
6504 void *ctx;
6505 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
6506 free_fn);
6507 int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
6508 if (res) initialize_obstack(obstack);
6509 return res;
6510}
6511INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
6512 void *ctx;
6513 COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
6514 REAL(_obstack_newchunk)(obstack, length);
6515 if (obstack->chunk)
6516 COMMON_INTERCEPTOR_INITIALIZE_RANGE(
6517 obstack->chunk, obstack->next_free - (char *)obstack->chunk);
6518}
6519#define INIT_OBSTACK \
6520 COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
6521 COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
6522 COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
6523#else
6524#define INIT_OBSTACK
6525#endif
6526
6527#if SANITIZER_INTERCEPT_FFLUSH
6528INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
6529 void *ctx;
6530 COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
6531 if (fp)
6532 unpoison_file(fp);
6533 int res = REAL(fflush)(fp);
6534 // FIXME: handle fp == NULL
6535 if (fp) {
6536 const FileMetadata *m = GetInterceptorMetadata(addr: fp);
6537 if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6538 }
6539 return res;
6540}
6541#define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
6542#else
6543#define INIT_FFLUSH
6544#endif
6545
6546#if SANITIZER_INTERCEPT_FCLOSE
6547INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
6548 void *ctx;
6549 COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
6550 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
6551 const FileMetadata *m = GetInterceptorMetadata(addr: fp);
6552 if (fp)
6553 unpoison_file(fp);
6554 int res = REAL(fclose)(fp);
6555 if (m) {
6556 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
6557 DeleteInterceptorMetadata(addr: fp);
6558 }
6559 return res;
6560}
6561#define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
6562#else
6563#define INIT_FCLOSE
6564#endif
6565
6566#if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
6567INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
6568 void *ctx;
6569 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
6570
6571 if (filename) {
6572 COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
6573
6574# if !SANITIZER_DYNAMIC
6575 // We care about a very specific use-case: dladdr on
6576 // statically-linked ASan may return <main program>
6577 // instead of the library.
6578 // We therefore only take effect if the sanitizer is statically
6579 // linked, and we don't bother canonicalizing paths because
6580 // dladdr should return the same address both times (we assume
6581 // the user did not canonicalize the result from dladdr).
6582 if (common_flags()->test_only_replace_dlopen_main_program) {
6583 VPrintf(1, "dlopen interceptor: filename: %s\n", filename);
6584
6585 const char *SelfFName = DladdrSelfFName();
6586 VPrintf(1, "dlopen interceptor: DladdrSelfFName: %p %s\n",
6587 (const void *)SelfFName, SelfFName);
6588
6589 if (SelfFName && internal_strcmp(s1: SelfFName, s2: filename) == 0) {
6590 // It's possible they copied the string from dladdr, so
6591 // we do a string comparison rather than pointer comparison.
6592 VPrintf(1, "dlopen interceptor: replacing %s because it matches %s\n",
6593 filename, SelfFName);
6594 filename = (char *)0; // RTLD_DEFAULT
6595 }
6596 }
6597# endif // !SANITIZER_DYNAMIC
6598 }
6599
6600 void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
6601 Symbolizer::GetOrInit()->InvalidateModuleList();
6602 COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
6603 return res;
6604}
6605
6606INTERCEPTOR(int, dlclose, void *handle) {
6607 void *ctx;
6608 COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
6609 int res = REAL(dlclose)(handle);
6610 Symbolizer::GetOrInit()->InvalidateModuleList();
6611 COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
6612 return res;
6613}
6614#define INIT_DLOPEN_DLCLOSE \
6615 COMMON_INTERCEPT_FUNCTION(dlopen); \
6616 COMMON_INTERCEPT_FUNCTION(dlclose);
6617#else
6618#define INIT_DLOPEN_DLCLOSE
6619#endif
6620
6621#if SANITIZER_INTERCEPT_GETPASS
6622INTERCEPTOR(char *, getpass, const char *prompt) {
6623 void *ctx;
6624 COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
6625 if (prompt)
6626 COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
6627 char *res = REAL(getpass)(prompt);
6628 if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
6629 return res;
6630}
6631
6632#define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
6633#else
6634#define INIT_GETPASS
6635#endif
6636
6637#if SANITIZER_INTERCEPT_TIMERFD
6638INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
6639 void *old_value) {
6640 void *ctx;
6641 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
6642 old_value);
6643 COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
6644 int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
6645 if (res != -1 && old_value)
6646 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
6647 return res;
6648}
6649
6650INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
6651 void *ctx;
6652 COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
6653 int res = REAL(timerfd_gettime)(fd, curr_value);
6654 if (res != -1 && curr_value)
6655 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
6656 return res;
6657}
6658#define INIT_TIMERFD \
6659 COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
6660 COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
6661#else
6662#define INIT_TIMERFD
6663#endif
6664
6665#if SANITIZER_INTERCEPT_MLOCKX
6666// Linux kernel has a bug that leads to kernel deadlock if a process
6667// maps TBs of memory and then calls mlock().
6668static void MlockIsUnsupported() {
6669 static atomic_uint8_t printed;
6670 if (atomic_exchange(a: &printed, v: 1, mo: memory_order_relaxed))
6671 return;
6672 VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
6673 SanitizerToolName);
6674}
6675
6676INTERCEPTOR(int, mlock, const void *addr, usize len) {
6677 MlockIsUnsupported();
6678 return 0;
6679}
6680
6681INTERCEPTOR(int, munlock, const void *addr, usize len) {
6682 MlockIsUnsupported();
6683 return 0;
6684}
6685
6686INTERCEPTOR(int, mlockall, int flags) {
6687 MlockIsUnsupported();
6688 return 0;
6689}
6690
6691INTERCEPTOR(int, munlockall, void) {
6692 MlockIsUnsupported();
6693 return 0;
6694}
6695
6696#define INIT_MLOCKX \
6697 COMMON_INTERCEPT_FUNCTION(mlock); \
6698 COMMON_INTERCEPT_FUNCTION(munlock); \
6699 COMMON_INTERCEPT_FUNCTION(mlockall); \
6700 COMMON_INTERCEPT_FUNCTION(munlockall);
6701
6702#else
6703#define INIT_MLOCKX
6704#endif // SANITIZER_INTERCEPT_MLOCKX
6705
6706#if SANITIZER_INTERCEPT_FOPENCOOKIE
6707struct WrappedCookie {
6708 void *real_cookie;
6709 __sanitizer_cookie_io_functions_t real_io_funcs;
6710};
6711
6712static uptr wrapped_read(void *cookie, char *buf, uptr size) {
6713 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6714 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6715 __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
6716 return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
6717}
6718
6719static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
6720 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6721 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6722 __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
6723 return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
6724}
6725
6726static int wrapped_seek(void *cookie, u64 *offset, int whence) {
6727 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
6728 COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
6729 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6730 __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
6731 return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
6732 : -1;
6733}
6734
6735static int wrapped_close(void *cookie) {
6736 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
6737 WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
6738 __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
6739 int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
6740 InternalFree(p: wrapped_cookie);
6741 return res;
6742}
6743
6744INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
6745 __sanitizer_cookie_io_functions_t io_funcs) {
6746 void *ctx;
6747 COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
6748 WrappedCookie *wrapped_cookie =
6749 (WrappedCookie *)InternalAlloc(size: sizeof(WrappedCookie));
6750 wrapped_cookie->real_cookie = cookie;
6751 wrapped_cookie->real_io_funcs = io_funcs;
6752 __sanitizer_FILE *res =
6753 REAL(fopencookie)(wrapped_cookie, mode, {.read: wrapped_read, .write: wrapped_write,
6754 .seek: wrapped_seek, .close: wrapped_close});
6755 return res;
6756}
6757
6758#define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
6759#else
6760#define INIT_FOPENCOOKIE
6761#endif // SANITIZER_INTERCEPT_FOPENCOOKIE
6762
6763#if SANITIZER_INTERCEPT_SEM
6764INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
6765 void *ctx;
6766 COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
6767 // Workaround a bug in glibc's "old" semaphore implementation by
6768 // zero-initializing the sem_t contents. This has to be done here because
6769 // interceptors bind to the lowest version before glibc 2.36, hitting the
6770 // buggy code path while the non-sanitized build of the same code works fine.
6771 REAL(memset)(s, 0, sizeof(*s));
6772 int res = REAL(sem_init)(s, pshared, value);
6773 return res;
6774}
6775
6776INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
6777 void *ctx;
6778 COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
6779 int res = REAL(sem_destroy)(s);
6780 return res;
6781}
6782
6783INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
6784 void *ctx;
6785 COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
6786 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
6787 if (res == 0) {
6788 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6789 }
6790 return res;
6791}
6792
6793INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
6794 void *ctx;
6795 COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
6796 int res = REAL(sem_trywait)(s);
6797 if (res == 0) {
6798 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6799 }
6800 return res;
6801}
6802
6803INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
6804 void *ctx;
6805 COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
6806 COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
6807 int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
6808 if (res == 0) {
6809 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6810 }
6811 return res;
6812}
6813
6814INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
6815 void *ctx;
6816 COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
6817 COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
6818 int res = REAL(sem_post)(s);
6819 return res;
6820}
6821
6822INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
6823 void *ctx;
6824 COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
6825 int res = REAL(sem_getvalue)(s, sval);
6826 if (res == 0) {
6827 COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
6828 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
6829 }
6830 return res;
6831}
6832
6833INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
6834 void *ctx;
6835 va_list ap;
6836 va_start(ap, oflag);
6837 u32 mode = va_arg(ap, u32);
6838 u32 value = va_arg(ap, u32);
6839 COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
6840 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6841 __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
6842 if (s)
6843 COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
6844 va_end(ap);
6845 return s;
6846}
6847
6848INTERCEPTOR(int, sem_unlink, const char *name) {
6849 void *ctx;
6850 COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
6851 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
6852 return REAL(sem_unlink)(name);
6853}
6854
6855# define INIT_SEM \
6856 COMMON_INTERCEPT_FUNCTION(sem_init); \
6857 COMMON_INTERCEPT_FUNCTION(sem_destroy); \
6858 COMMON_INTERCEPT_FUNCTION(sem_wait); \
6859 COMMON_INTERCEPT_FUNCTION(sem_trywait); \
6860 COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
6861 COMMON_INTERCEPT_FUNCTION(sem_post); \
6862 COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
6863 COMMON_INTERCEPT_FUNCTION(sem_open); \
6864 COMMON_INTERCEPT_FUNCTION(sem_unlink);
6865#else
6866# define INIT_SEM
6867#endif // SANITIZER_INTERCEPT_SEM
6868
6869#if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
6870INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
6871 void *ctx;
6872 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
6873 int res = REAL(pthread_setcancelstate)(state, oldstate);
6874 if (res == 0 && oldstate != nullptr)
6875 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
6876 return res;
6877}
6878
6879INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
6880 void *ctx;
6881 COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
6882 int res = REAL(pthread_setcanceltype)(type, oldtype);
6883 if (res == 0 && oldtype != nullptr)
6884 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
6885 return res;
6886}
6887#define INIT_PTHREAD_SETCANCEL \
6888 COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
6889 COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
6890#else
6891#define INIT_PTHREAD_SETCANCEL
6892#endif
6893
6894#if SANITIZER_INTERCEPT_MINCORE
6895INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
6896 void *ctx;
6897 COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
6898 int res = REAL(mincore)(addr, length, vec);
6899 if (res == 0) {
6900 uptr page_size = GetPageSizeCached();
6901 uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
6902 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
6903 }
6904 return res;
6905}
6906#define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
6907#else
6908#define INIT_MINCORE
6909#endif
6910
6911#if SANITIZER_INTERCEPT_PROCESS_VM_READV
6912INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
6913 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6914 uptr flags) {
6915 void *ctx;
6916 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
6917 remote_iov, riovcnt, flags);
6918 SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
6919 riovcnt, flags);
6920 if (res > 0)
6921 write_iovec(ctx, iovec: local_iov, iovlen: liovcnt, maxlen: res);
6922 return res;
6923}
6924
6925INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
6926 uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
6927 uptr flags) {
6928 void *ctx;
6929 COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
6930 remote_iov, riovcnt, flags);
6931 SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
6932 riovcnt, flags);
6933 if (res > 0)
6934 read_iovec(ctx, iovec: local_iov, iovlen: liovcnt, maxlen: res);
6935 return res;
6936}
6937#define INIT_PROCESS_VM_READV \
6938 COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
6939 COMMON_INTERCEPT_FUNCTION(process_vm_writev);
6940#else
6941#define INIT_PROCESS_VM_READV
6942#endif
6943
6944#if SANITIZER_INTERCEPT_CTERMID
6945INTERCEPTOR(char *, ctermid, char *s) {
6946 void *ctx;
6947 COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
6948 char *res = REAL(ctermid)(s);
6949 if (res) {
6950 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6951 }
6952 return res;
6953}
6954#define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
6955#else
6956#define INIT_CTERMID
6957#endif
6958
6959#if SANITIZER_INTERCEPT_CTERMID_R
6960INTERCEPTOR(char *, ctermid_r, char *s) {
6961 void *ctx;
6962 COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
6963 char *res = REAL(ctermid_r)(s);
6964 if (res) {
6965 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
6966 }
6967 return res;
6968}
6969#define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
6970#else
6971#define INIT_CTERMID_R
6972#endif
6973
6974#if SANITIZER_INTERCEPT_RECV_RECVFROM
6975INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
6976 void *ctx;
6977 COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
6978 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6979 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recv)(fd, buf, len, flags);
6980 if (res > 0) {
6981 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
6982 }
6983 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
6984 return res;
6985}
6986
6987INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
6988 void *srcaddr, int *addrlen) {
6989 void *ctx;
6990 COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
6991 addrlen);
6992 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
6993 SIZE_T srcaddr_sz;
6994 if (srcaddr) srcaddr_sz = *addrlen;
6995 (void)srcaddr_sz; // prevent "set but not used" warning
6996 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(recvfrom)(fd, buf, len, flags,
6997 srcaddr, addrlen);
6998 if (res > 0)
6999 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
7000 if (res >= 0 && srcaddr)
7001 COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
7002 Min((SIZE_T)*addrlen, srcaddr_sz));
7003 return res;
7004}
7005#define INIT_RECV_RECVFROM \
7006 COMMON_INTERCEPT_FUNCTION(recv); \
7007 COMMON_INTERCEPT_FUNCTION(recvfrom);
7008#else
7009#define INIT_RECV_RECVFROM
7010#endif
7011
7012#if SANITIZER_INTERCEPT_SEND_SENDTO
7013INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
7014 void *ctx;
7015 COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
7016 if (fd >= 0) {
7017 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7018 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
7019 }
7020 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(send)(fd, buf, len, flags);
7021 if (common_flags()->intercept_send && res > 0)
7022 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
7023 return res;
7024}
7025
7026INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
7027 void *dstaddr, int addrlen) {
7028 void *ctx;
7029 COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
7030 if (fd >= 0) {
7031 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7032 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
7033 }
7034 // Can't check dstaddr as it may have uninitialized padding at the end.
7035 SSIZE_T res = COMMON_INTERCEPTOR_BLOCK_REAL(sendto)(fd, buf, len, flags,
7036 dstaddr, addrlen);
7037 if (common_flags()->intercept_send && res > 0)
7038 COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
7039 return res;
7040}
7041#define INIT_SEND_SENDTO \
7042 COMMON_INTERCEPT_FUNCTION(send); \
7043 COMMON_INTERCEPT_FUNCTION(sendto);
7044#else
7045#define INIT_SEND_SENDTO
7046#endif
7047
7048#if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
7049INTERCEPTOR(int, eventfd_read, int fd, __sanitizer_eventfd_t *value) {
7050 void *ctx;
7051 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
7052 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7053 int res = COMMON_INTERCEPTOR_BLOCK_REAL(eventfd_read)(fd, value);
7054 if (res == 0) {
7055 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
7056 if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
7057 }
7058 return res;
7059}
7060INTERCEPTOR(int, eventfd_write, int fd, __sanitizer_eventfd_t value) {
7061 void *ctx;
7062 COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
7063 if (fd >= 0) {
7064 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
7065 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
7066 }
7067 int res = COMMON_INTERCEPTOR_BLOCK_REAL(eventfd_write)(fd, value);
7068 return res;
7069}
7070#define INIT_EVENTFD_READ_WRITE \
7071 COMMON_INTERCEPT_FUNCTION(eventfd_read); \
7072 COMMON_INTERCEPT_FUNCTION(eventfd_write)
7073#else
7074#define INIT_EVENTFD_READ_WRITE
7075#endif
7076
7077#if SANITIZER_INTERCEPT_STAT
7078INTERCEPTOR(int, stat, const char *path, void *buf) {
7079 void *ctx;
7080 COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
7081 if (common_flags()->intercept_stat)
7082 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7083 int res = REAL(stat)(path, buf);
7084 if (!res)
7085 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7086 return res;
7087}
7088#define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
7089#else
7090#define INIT_STAT
7091#endif
7092
7093#if SANITIZER_INTERCEPT_STAT64
7094INTERCEPTOR(int, stat64, const char *path, void *buf) {
7095 void *ctx;
7096 COMMON_INTERCEPTOR_ENTER(ctx, stat64, path, buf);
7097 if (common_flags()->intercept_stat)
7098 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7099 int res = REAL(stat64)(path, buf);
7100 if (!res)
7101 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7102 return res;
7103}
7104#define INIT_STAT64 COMMON_INTERCEPT_FUNCTION(stat64)
7105#else
7106#define INIT_STAT64
7107#endif
7108
7109
7110#if SANITIZER_INTERCEPT_LSTAT
7111INTERCEPTOR(int, lstat, const char *path, void *buf) {
7112 void *ctx;
7113 COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
7114 if (common_flags()->intercept_stat)
7115 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7116 int res = REAL(lstat)(path, buf);
7117 if (!res)
7118 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7119 return res;
7120}
7121#define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
7122#else
7123#define INIT_LSTAT
7124#endif
7125
7126#if SANITIZER_INTERCEPT_STAT64
7127INTERCEPTOR(int, lstat64, const char *path, void *buf) {
7128 void *ctx;
7129 COMMON_INTERCEPTOR_ENTER(ctx, lstat64, path, buf);
7130 if (common_flags()->intercept_stat)
7131 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7132 int res = REAL(lstat64)(path, buf);
7133 if (!res)
7134 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7135 return res;
7136}
7137#define INIT_LSTAT64 COMMON_INTERCEPT_FUNCTION(lstat64)
7138#else
7139#define INIT_LSTAT64
7140#endif
7141
7142#if SANITIZER_INTERCEPT___XSTAT
7143INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
7144 void *ctx;
7145 COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
7146 if (common_flags()->intercept_stat)
7147 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7148 int res = REAL(__xstat)(version, path, buf);
7149 if (!res)
7150 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7151 return res;
7152}
7153#define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
7154#else
7155#define INIT___XSTAT
7156#endif
7157
7158#if SANITIZER_INTERCEPT___XSTAT64
7159INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
7160 void *ctx;
7161 COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
7162 if (common_flags()->intercept_stat)
7163 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7164 int res = REAL(__xstat64)(version, path, buf);
7165 if (!res)
7166 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7167 return res;
7168}
7169#define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
7170#else
7171#define INIT___XSTAT64
7172#endif
7173
7174#if SANITIZER_INTERCEPT___LXSTAT
7175INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
7176 void *ctx;
7177 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
7178 if (common_flags()->intercept_stat)
7179 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7180 int res = REAL(__lxstat)(version, path, buf);
7181 if (!res)
7182 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
7183 return res;
7184}
7185#define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
7186#else
7187#define INIT___LXSTAT
7188#endif
7189
7190#if SANITIZER_INTERCEPT___LXSTAT64
7191INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
7192 void *ctx;
7193 COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
7194 if (common_flags()->intercept_stat)
7195 COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
7196 int res = REAL(__lxstat64)(version, path, buf);
7197 if (!res)
7198 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
7199 return res;
7200}
7201#define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
7202#else
7203#define INIT___LXSTAT64
7204#endif
7205
7206// FIXME: add other *stat interceptor
7207
7208#if SANITIZER_INTERCEPT_UTMP
7209INTERCEPTOR(void *, getutent, int dummy) {
7210 void *ctx;
7211 COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
7212 void *res = REAL(getutent)(dummy);
7213 if (res)
7214 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7215 return res;
7216}
7217INTERCEPTOR(void *, getutid, void *ut) {
7218 void *ctx;
7219 COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
7220 void *res = REAL(getutid)(ut);
7221 if (res)
7222 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7223 return res;
7224}
7225INTERCEPTOR(void *, getutline, void *ut) {
7226 void *ctx;
7227 COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
7228 void *res = REAL(getutline)(ut);
7229 if (res)
7230 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
7231 return res;
7232}
7233#define INIT_UTMP \
7234 COMMON_INTERCEPT_FUNCTION(getutent); \
7235 COMMON_INTERCEPT_FUNCTION(getutid); \
7236 COMMON_INTERCEPT_FUNCTION(getutline);
7237#else
7238#define INIT_UTMP
7239#endif
7240
7241#if SANITIZER_INTERCEPT_UTMPX
7242INTERCEPTOR(void *, getutxent, int dummy) {
7243 void *ctx;
7244 COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
7245 void *res = REAL(getutxent)(dummy);
7246 if (res)
7247 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7248 return res;
7249}
7250INTERCEPTOR(void *, getutxid, void *ut) {
7251 void *ctx;
7252 COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
7253 void *res = REAL(getutxid)(ut);
7254 if (res)
7255 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7256 return res;
7257}
7258INTERCEPTOR(void *, getutxline, void *ut) {
7259 void *ctx;
7260 COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
7261 void *res = REAL(getutxline)(ut);
7262 if (res)
7263 COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
7264 return res;
7265}
7266INTERCEPTOR(void *, pututxline, const void *ut) {
7267 void *ctx;
7268 COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
7269 if (ut)
7270 COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
7271 void *res = REAL(pututxline)(ut);
7272 if (res)
7273 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
7274 return res;
7275}
7276#define INIT_UTMPX \
7277 COMMON_INTERCEPT_FUNCTION(getutxent); \
7278 COMMON_INTERCEPT_FUNCTION(getutxid); \
7279 COMMON_INTERCEPT_FUNCTION(getutxline); \
7280 COMMON_INTERCEPT_FUNCTION(pututxline);
7281#else
7282#define INIT_UTMPX
7283#endif
7284
7285#if SANITIZER_INTERCEPT_GETLOADAVG
7286INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
7287 void *ctx;
7288 COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
7289 int res = REAL(getloadavg)(loadavg, nelem);
7290 if (res > 0)
7291 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
7292 return res;
7293}
7294#define INIT_GETLOADAVG \
7295 COMMON_INTERCEPT_FUNCTION(getloadavg);
7296#else
7297#define INIT_GETLOADAVG
7298#endif
7299
7300#if SANITIZER_INTERCEPT_MCHECK_MPROBE
7301INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
7302 return 0;
7303}
7304
7305INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
7306 return 0;
7307}
7308
7309INTERCEPTOR(int, mprobe, void *ptr) {
7310 return 0;
7311}
7312#endif
7313
7314#if SANITIZER_INTERCEPT_WCSLEN
7315INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
7316 void *ctx;
7317 COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
7318 SIZE_T res = REAL(wcslen)(s);
7319 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
7320 return res;
7321}
7322
7323INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
7324 void *ctx;
7325 COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
7326 SIZE_T res = REAL(wcsnlen)(s, n);
7327 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
7328 return res;
7329}
7330#define INIT_WCSLEN \
7331 COMMON_INTERCEPT_FUNCTION(wcslen); \
7332 COMMON_INTERCEPT_FUNCTION(wcsnlen);
7333#else
7334#define INIT_WCSLEN
7335#endif
7336
7337#if SANITIZER_INTERCEPT_WCSCAT
7338INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
7339 void *ctx;
7340 COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
7341 SIZE_T src_size = internal_wcslen(s: src);
7342 SIZE_T dst_size = internal_wcslen(s: dst);
7343 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
7344 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7345 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7346 (src_size + 1) * sizeof(wchar_t));
7347 return REAL(wcscat)(dst, src);
7348}
7349
7350INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
7351 void *ctx;
7352 COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
7353 SIZE_T src_size = internal_wcsnlen(s: src, maxlen: n);
7354 SIZE_T dst_size = internal_wcslen(s: dst);
7355 COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
7356 Min(src_size + 1, n) * sizeof(wchar_t));
7357 COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
7358 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
7359 (src_size + 1) * sizeof(wchar_t));
7360 return REAL(wcsncat)(dst, src, n);
7361}
7362#define INIT_WCSCAT \
7363 COMMON_INTERCEPT_FUNCTION(wcscat); \
7364 COMMON_INTERCEPT_FUNCTION(wcsncat);
7365#else
7366#define INIT_WCSCAT
7367#endif
7368
7369#if SANITIZER_INTERCEPT_WCSDUP
7370INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
7371 void *ctx;
7372 COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
7373 SIZE_T len = internal_wcslen(s);
7374 COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
7375 wchar_t *result = REAL(wcsdup)(s);
7376 if (result)
7377 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
7378 return result;
7379}
7380
7381#define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
7382#else
7383#define INIT_WCSDUP
7384#endif
7385
7386#if SANITIZER_INTERCEPT_STRXFRM
7387static SIZE_T RealStrLen(const char *str) { return internal_strlen(s: str); }
7388
7389static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(s: str); }
7390
7391#define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
7392 { \
7393 void *ctx; \
7394 COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
7395 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
7396 sizeof(*src) * (RealStrLen(src) + 1)); \
7397 SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
7398 if (res < len) \
7399 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
7400 return res; \
7401 }
7402
7403INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
7404 STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
7405}
7406
7407INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
7408 void *locale) {
7409 STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
7410}
7411
7412#define INIT_STRXFRM \
7413 COMMON_INTERCEPT_FUNCTION(strxfrm); \
7414 COMMON_INTERCEPT_FUNCTION(strxfrm_l);
7415#else
7416#define INIT_STRXFRM
7417#endif
7418
7419#if SANITIZER_INTERCEPT___STRXFRM_L
7420INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
7421 void *locale) {
7422 STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
7423}
7424
7425#define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
7426#else
7427#define INIT___STRXFRM_L
7428#endif
7429
7430#if SANITIZER_INTERCEPT_WCSXFRM
7431INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
7432 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
7433}
7434
7435INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7436 void *locale) {
7437 STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
7438}
7439
7440#define INIT_WCSXFRM \
7441 COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
7442 COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
7443#else
7444#define INIT_WCSXFRM
7445#endif
7446
7447#if SANITIZER_INTERCEPT___WCSXFRM_L
7448INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
7449 void *locale) {
7450 STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
7451}
7452
7453#define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
7454#else
7455#define INIT___WCSXFRM_L
7456#endif
7457
7458#if SANITIZER_INTERCEPT_ACCT
7459INTERCEPTOR(int, acct, const char *file) {
7460 void *ctx;
7461 COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
7462 if (file)
7463 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
7464 return REAL(acct)(file);
7465}
7466#define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
7467#else
7468#define INIT_ACCT
7469#endif
7470
7471#if SANITIZER_INTERCEPT_USER_FROM_UID
7472INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
7473 void *ctx;
7474 const char *user;
7475 COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
7476 user = REAL(user_from_uid)(uid, nouser);
7477 if (user)
7478 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
7479 return user;
7480}
7481#define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
7482#else
7483#define INIT_USER_FROM_UID
7484#endif
7485
7486#if SANITIZER_INTERCEPT_UID_FROM_USER
7487INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
7488 void *ctx;
7489 int res;
7490 COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
7491 if (name)
7492 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7493 res = REAL(uid_from_user)(name, uid);
7494 if (uid)
7495 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
7496 return res;
7497}
7498#define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
7499#else
7500#define INIT_UID_FROM_USER
7501#endif
7502
7503#if SANITIZER_INTERCEPT_GROUP_FROM_GID
7504INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
7505 void *ctx;
7506 const char *group;
7507 COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
7508 group = REAL(group_from_gid)(gid, nogroup);
7509 if (group)
7510 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
7511 return group;
7512}
7513#define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
7514#else
7515#define INIT_GROUP_FROM_GID
7516#endif
7517
7518#if SANITIZER_INTERCEPT_GID_FROM_GROUP
7519INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
7520 void *ctx;
7521 int res;
7522 COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
7523 if (group)
7524 COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
7525 res = REAL(gid_from_group)(group, gid);
7526 if (gid)
7527 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
7528 return res;
7529}
7530#define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
7531#else
7532#define INIT_GID_FROM_GROUP
7533#endif
7534
7535#if SANITIZER_INTERCEPT_ACCESS
7536INTERCEPTOR(int, access, const char *path, int mode) {
7537 void *ctx;
7538 COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
7539 if (path)
7540 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7541 return REAL(access)(path, mode);
7542}
7543#define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
7544#else
7545#define INIT_ACCESS
7546#endif
7547
7548#if SANITIZER_INTERCEPT_FACCESSAT
7549INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
7550 void *ctx;
7551 COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
7552 if (path)
7553 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7554 return REAL(faccessat)(fd, path, mode, flags);
7555}
7556#define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
7557#else
7558#define INIT_FACCESSAT
7559#endif
7560
7561#if SANITIZER_INTERCEPT_GETGROUPLIST
7562INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
7563 int *ngroups) {
7564 void *ctx;
7565 int res;
7566 COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
7567 if (name)
7568 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7569 if (ngroups)
7570 COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
7571 res = REAL(getgrouplist)(name, basegid, groups, ngroups);
7572 if (!res && groups && ngroups) {
7573 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7574 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7575 }
7576 return res;
7577}
7578
7579#define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
7580#else
7581#define INIT_GETGROUPLIST
7582#endif
7583
7584#if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
7585INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
7586 int maxgrp, int *ngroups) {
7587 void *ctx;
7588 int res;
7589 COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
7590 maxgrp, ngroups);
7591 if (name)
7592 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7593 res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
7594 if (!res && groups && ngroups) {
7595 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
7596 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
7597 }
7598 return res;
7599}
7600
7601#define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
7602#else
7603#define INIT_GETGROUPMEMBERSHIP
7604#endif
7605
7606#if SANITIZER_INTERCEPT_READLINK
7607INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
7608 void* ctx;
7609 COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
7610 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7611 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
7612 if (res > 0)
7613 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7614 return res;
7615}
7616
7617#define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
7618#else
7619#define INIT_READLINK
7620#endif
7621
7622#if SANITIZER_INTERCEPT_READLINKAT
7623INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
7624 SIZE_T bufsiz) {
7625 void* ctx;
7626 COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
7627 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7628 SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
7629 if (res > 0)
7630 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
7631 return res;
7632}
7633
7634#define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
7635#else
7636#define INIT_READLINKAT
7637#endif
7638
7639#if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
7640INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
7641 struct file_handle *handle, int *mount_id, int flags) {
7642 void* ctx;
7643 COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
7644 mount_id, flags);
7645 COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
7646
7647 __sanitizer_file_handle *sanitizer_handle =
7648 reinterpret_cast<__sanitizer_file_handle*>(handle);
7649 COMMON_INTERCEPTOR_READ_RANGE(
7650 ctx, &sanitizer_handle->handle_bytes,
7651 sizeof(sanitizer_handle->handle_bytes));
7652
7653 int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
7654 if (!res) {
7655 COMMON_INTERCEPTOR_WRITE_RANGE(
7656 ctx, &sanitizer_handle->handle_bytes,
7657 sizeof(sanitizer_handle->handle_bytes));
7658 COMMON_INTERCEPTOR_WRITE_RANGE(
7659 ctx, &sanitizer_handle->handle_type,
7660 sizeof(sanitizer_handle->handle_type));
7661 COMMON_INTERCEPTOR_WRITE_RANGE(
7662 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7663 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
7664 }
7665 return res;
7666}
7667
7668#define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
7669#else
7670#define INIT_NAME_TO_HANDLE_AT
7671#endif
7672
7673#if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
7674INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
7675 int flags) {
7676 void* ctx;
7677 COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
7678
7679 __sanitizer_file_handle *sanitizer_handle =
7680 reinterpret_cast<__sanitizer_file_handle*>(handle);
7681 COMMON_INTERCEPTOR_READ_RANGE(
7682 ctx, &sanitizer_handle->handle_bytes,
7683 sizeof(sanitizer_handle->handle_bytes));
7684 COMMON_INTERCEPTOR_READ_RANGE(
7685 ctx, &sanitizer_handle->handle_type,
7686 sizeof(sanitizer_handle->handle_type));
7687 COMMON_INTERCEPTOR_READ_RANGE(
7688 ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
7689
7690 return COMMON_INTERCEPTOR_BLOCK_REAL(open_by_handle_at)(mount_fd, handle,
7691 flags);
7692}
7693
7694#define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
7695#else
7696#define INIT_OPEN_BY_HANDLE_AT
7697#endif
7698
7699#if SANITIZER_INTERCEPT_STRLCPY
7700INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
7701 void *ctx;
7702 SIZE_T res;
7703 COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
7704 if (src) {
7705 // Keep strnlen as macro argument, as macro may ignore it.
7706 COMMON_INTERCEPTOR_READ_STRING(
7707 ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
7708 }
7709 res = REAL(strlcpy)(dst, src, size);
7710 COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
7711 return res;
7712}
7713
7714INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
7715 void *ctx;
7716 SIZE_T len = 0;
7717 COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
7718 // src is checked in the strlcpy() interceptor
7719 if (dst) {
7720 len = internal_strnlen(dst, size);
7721 COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
7722 }
7723 // Reuse the rest of the code in the strlcpy() interceptor
7724 return WRAP(strlcpy)(dst + len, src, size - len) + len;
7725}
7726#define INIT_STRLCPY \
7727 COMMON_INTERCEPT_FUNCTION(strlcpy); \
7728 COMMON_INTERCEPT_FUNCTION(strlcat);
7729#else
7730#define INIT_STRLCPY
7731#endif
7732
7733#if SANITIZER_INTERCEPT_MMAP
7734INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
7735 OFF_T off) {
7736 void *ctx;
7737 if (common_flags()->detect_write_exec)
7738 ReportMmapWriteExec(prot, mflags: flags);
7739 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7740 return (void *)internal_mmap(addr, length: sz, prot, flags, fd, offset: off);
7741 COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
7742 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
7743}
7744
7745INTERCEPTOR(int, munmap, void *addr, SIZE_T sz) {
7746 void *ctx;
7747 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7748 return (int)internal_munmap(addr, length: sz);
7749 COMMON_INTERCEPTOR_ENTER(ctx, munmap, addr, sz);
7750 COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, sz);
7751}
7752
7753INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
7754 void *ctx;
7755 if (common_flags()->detect_write_exec)
7756 ReportMmapWriteExec(prot, mflags: 0);
7757 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7758 return (int)internal_mprotect(addr, length: sz, prot);
7759 COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
7760 MprotectMallocZones(addr, prot);
7761 return REAL(mprotect)(addr, sz, prot);
7762}
7763#define INIT_MMAP \
7764 COMMON_INTERCEPT_FUNCTION(mmap); \
7765 COMMON_INTERCEPT_FUNCTION(munmap); \
7766 COMMON_INTERCEPT_FUNCTION(mprotect);
7767#else
7768#define INIT_MMAP
7769#endif
7770
7771#if SANITIZER_INTERCEPT_MMAP64
7772INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
7773 OFF64_T off) {
7774 void *ctx;
7775 if (common_flags()->detect_write_exec)
7776 ReportMmapWriteExec(prot, mflags: flags);
7777 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
7778 return (void *)internal_mmap(addr, length: sz, prot, flags, fd, offset: off);
7779 COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
7780 COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
7781}
7782#define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
7783#else
7784#define INIT_MMAP64
7785#endif
7786
7787#if SANITIZER_INTERCEPT_DEVNAME
7788INTERCEPTOR(char *, devname, u64 dev, u32 type) {
7789 void *ctx;
7790 char *name;
7791 COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
7792 name = REAL(devname)(dev, type);
7793 if (name)
7794 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
7795 return name;
7796}
7797#define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
7798#else
7799#define INIT_DEVNAME
7800#endif
7801
7802#if SANITIZER_INTERCEPT_DEVNAME_R
7803#if SANITIZER_NETBSD
7804#define DEVNAME_R_RETTYPE int
7805#define DEVNAME_R_SUCCESS(x) (!(x))
7806#else
7807#define DEVNAME_R_RETTYPE char*
7808#define DEVNAME_R_SUCCESS(x) (x)
7809#endif
7810INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
7811 uptr len) {
7812 void *ctx;
7813 COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
7814 DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
7815 if (DEVNAME_R_SUCCESS(res))
7816 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
7817 return res;
7818}
7819#define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
7820#else
7821#define INIT_DEVNAME_R
7822#endif
7823
7824#if SANITIZER_INTERCEPT_FGETLN
7825INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
7826 void *ctx;
7827 COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
7828 char *str = REAL(fgetln)(stream, len);
7829 if (str && len) {
7830 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
7831 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
7832 }
7833 return str;
7834}
7835#define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
7836#else
7837#define INIT_FGETLN
7838#endif
7839
7840#if SANITIZER_INTERCEPT_STRMODE
7841INTERCEPTOR(void, strmode, u32 mode, char *bp) {
7842 void *ctx;
7843 COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
7844 REAL(strmode)(mode, bp);
7845 if (bp)
7846 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
7847}
7848#define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
7849#else
7850#define INIT_STRMODE
7851#endif
7852
7853#if SANITIZER_INTERCEPT_TTYENT
7854INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
7855 void *ctx;
7856 COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
7857 struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
7858 if (ttyent)
7859 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7860 return ttyent;
7861}
7862INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
7863 void *ctx;
7864 COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
7865 if (name)
7866 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7867 struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
7868 if (ttyent)
7869 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
7870 return ttyent;
7871}
7872#define INIT_TTYENT \
7873 COMMON_INTERCEPT_FUNCTION(getttyent); \
7874 COMMON_INTERCEPT_FUNCTION(getttynam);
7875#else
7876#define INIT_TTYENT
7877#endif
7878
7879#if SANITIZER_INTERCEPT_TTYENTPATH
7880INTERCEPTOR(int, setttyentpath, char *path) {
7881 void *ctx;
7882 COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
7883 if (path)
7884 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
7885 return REAL(setttyentpath)(path);
7886}
7887#define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
7888#else
7889#define INIT_TTYENTPATH
7890#endif
7891
7892#if SANITIZER_INTERCEPT_PROTOENT
7893static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
7894 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
7895
7896 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
7897
7898 SIZE_T pp_size = 1; // One handles the trailing \0
7899
7900 for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
7901 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
7902
7903 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases, pp_size * sizeof(char *));
7904}
7905
7906INTERCEPTOR(struct __sanitizer_protoent *, getprotoent,) {
7907 void *ctx;
7908 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent,);
7909 struct __sanitizer_protoent *p = REAL(getprotoent)();
7910 if (p)
7911 write_protoent(ctx, p);
7912 return p;
7913}
7914
7915INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
7916 void *ctx;
7917 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
7918 if (name)
7919 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7920 struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
7921 if (p)
7922 write_protoent(ctx, p);
7923 return p;
7924}
7925
7926INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
7927 void *ctx;
7928 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
7929 struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
7930 if (p)
7931 write_protoent(ctx, p);
7932 return p;
7933}
7934#define INIT_PROTOENT \
7935 COMMON_INTERCEPT_FUNCTION(getprotoent); \
7936 COMMON_INTERCEPT_FUNCTION(getprotobyname); \
7937 COMMON_INTERCEPT_FUNCTION(getprotobynumber)
7938#else
7939#define INIT_PROTOENT
7940#endif
7941
7942#if SANITIZER_INTERCEPT_PROTOENT_R
7943INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
7944 char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
7945 void *ctx;
7946 COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
7947 result);
7948 int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
7949
7950 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7951 if (!res && *result)
7952 write_protoent(ctx, p: *result);
7953 return res;
7954}
7955
7956INTERCEPTOR(int, getprotobyname_r, const char *name,
7957 struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
7958 struct __sanitizer_protoent **result) {
7959 void *ctx;
7960 COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
7961 buflen, result);
7962 if (name)
7963 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
7964 int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
7965
7966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7967 if (!res && *result)
7968 write_protoent(ctx, p: *result);
7969 return res;
7970}
7971
7972INTERCEPTOR(int, getprotobynumber_r, int num,
7973 struct __sanitizer_protoent *result_buf, char *buf,
7974 SIZE_T buflen, struct __sanitizer_protoent **result) {
7975 void *ctx;
7976 COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
7977 buflen, result);
7978 int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
7979
7980 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
7981 if (!res && *result)
7982 write_protoent(ctx, p: *result);
7983 return res;
7984}
7985
7986#define INIT_PROTOENT_R \
7987 COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
7988 COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
7989 COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
7990#else
7991#define INIT_PROTOENT_R
7992#endif
7993
7994#if SANITIZER_INTERCEPT_NETENT
7995INTERCEPTOR(struct __sanitizer_netent *, getnetent,) {
7996 void *ctx;
7997 COMMON_INTERCEPTOR_ENTER(ctx, getnetent,);
7998 struct __sanitizer_netent *n = REAL(getnetent)();
7999 if (n) {
8000 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
8001
8002 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
8003
8004 SIZE_T nn_size = 1; // One handles the trailing \0
8005
8006 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
8007 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
8008
8009 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
8010 }
8011 return n;
8012}
8013
8014INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
8015 void *ctx;
8016 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
8017 if (name)
8018 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
8019 struct __sanitizer_netent *n = REAL(getnetbyname)(name);
8020 if (n) {
8021 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
8022
8023 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
8024
8025 SIZE_T nn_size = 1; // One handles the trailing \0
8026
8027 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
8028 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
8029
8030 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
8031 }
8032 return n;
8033}
8034
8035INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
8036 void *ctx;
8037 COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
8038 struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
8039 if (n) {
8040 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
8041
8042 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
8043
8044 SIZE_T nn_size = 1; // One handles the trailing \0
8045
8046 for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
8047 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
8048
8049 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases, nn_size * sizeof(char *));
8050 }
8051 return n;
8052}
8053#define INIT_NETENT \
8054 COMMON_INTERCEPT_FUNCTION(getnetent); \
8055 COMMON_INTERCEPT_FUNCTION(getnetbyname); \
8056 COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
8057#else
8058#define INIT_NETENT
8059#endif
8060
8061#if SANITIZER_INTERCEPT_GETMNTINFO
8062INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
8063 void *ctx;
8064 COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
8065 int cnt = REAL(getmntinfo)(mntbufp, flags);
8066 if (cnt > 0 && mntbufp) {
8067 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
8068 if (*mntbufp)
8069#if SANITIZER_NETBSD
8070 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
8071#else
8072 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
8073#endif
8074 }
8075 return cnt;
8076}
8077#define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
8078#else
8079#define INIT_GETMNTINFO
8080#endif
8081
8082#if SANITIZER_INTERCEPT_MI_VECTOR_HASH
8083INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
8084 u32 hashes[3]) {
8085 void *ctx;
8086 COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
8087 if (key)
8088 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
8089 REAL(mi_vector_hash)(key, len, seed, hashes);
8090 if (hashes)
8091 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
8092}
8093#define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
8094#else
8095#define INIT_MI_VECTOR_HASH
8096#endif
8097
8098#if SANITIZER_INTERCEPT_SETVBUF
8099INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
8100 SIZE_T size) {
8101 void *ctx;
8102 COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
8103 int ret = REAL(setvbuf)(stream, buf, mode, size);
8104 if (buf)
8105 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
8106 if (stream)
8107 unpoison_file(fp: stream);
8108 return ret;
8109}
8110
8111INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
8112 void *ctx;
8113 COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
8114 REAL(setbuf)(stream, buf);
8115 if (buf) {
8116 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
8117 }
8118 if (stream)
8119 unpoison_file(fp: stream);
8120}
8121
8122INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
8123 void *ctx;
8124 COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
8125 REAL(setbuffer)(stream, buf, size);
8126 if (buf) {
8127 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
8128 }
8129 if (stream)
8130 unpoison_file(fp: stream);
8131}
8132
8133INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
8134 void *ctx;
8135 COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
8136 REAL(setlinebuf)(stream);
8137 if (stream)
8138 unpoison_file(fp: stream);
8139}
8140#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
8141 COMMON_INTERCEPT_FUNCTION(setbuf); \
8142 COMMON_INTERCEPT_FUNCTION(setbuffer); \
8143 COMMON_INTERCEPT_FUNCTION(setlinebuf)
8144#else
8145#define INIT_SETVBUF
8146#endif
8147
8148#if SANITIZER_INTERCEPT_GETVFSSTAT
8149INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
8150 void *ctx;
8151 COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
8152 int ret = REAL(getvfsstat)(buf, bufsize, flags);
8153 if (buf && ret > 0)
8154 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
8155 return ret;
8156}
8157#define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
8158#else
8159#define INIT_GETVFSSTAT
8160#endif
8161
8162#if SANITIZER_INTERCEPT_REGEX
8163INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
8164 void *ctx;
8165 COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
8166 if (pattern)
8167 COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
8168 int res = REAL(regcomp)(preg, pattern, cflags);
8169 if (preg)
8170 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
8171 return res;
8172}
8173INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
8174 struct __sanitizer_regmatch *pmatch[], int eflags) {
8175 void *ctx;
8176 COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
8177 if (preg)
8178 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8179 if (string)
8180 COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
8181 int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
8182 if (!res && pmatch)
8183 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
8184 return res;
8185}
8186INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
8187 SIZE_T errbuf_size) {
8188 void *ctx;
8189 COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
8190 if (preg)
8191 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8192 SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
8193 if (errbuf)
8194 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
8195 return res;
8196}
8197INTERCEPTOR(void, regfree, const void *preg) {
8198 void *ctx;
8199 COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
8200 if (preg)
8201 COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
8202 REAL(regfree)(preg);
8203}
8204#define INIT_REGEX \
8205 COMMON_INTERCEPT_FUNCTION(regcomp); \
8206 COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
8207 COMMON_INTERCEPT_FUNCTION(regerror); \
8208 COMMON_INTERCEPT_FUNCTION(regfree);
8209#else
8210#define INIT_REGEX
8211#endif
8212
8213#if SANITIZER_INTERCEPT_REGEXSUB
8214INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
8215 const struct __sanitizer_regmatch *rm, const char *str) {
8216 void *ctx;
8217 COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
8218 if (sub)
8219 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
8220 // The implementation demands and hardcodes 10 elements
8221 if (rm)
8222 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
8223 if (str)
8224 COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
8225 SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
8226 if (res > 0 && buf)
8227 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
8228 return res;
8229}
8230INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
8231 const struct __sanitizer_regmatch *rm, const char *sstr) {
8232 void *ctx;
8233 COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
8234 if (sub)
8235 COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
8236 // Hardcode 10 elements as this is hardcoded size
8237 if (rm)
8238 COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
8239 if (sstr)
8240 COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
8241 SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
8242 if (res > 0 && buf) {
8243 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
8244 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
8245 }
8246 return res;
8247}
8248
8249#define INIT_REGEXSUB \
8250 COMMON_INTERCEPT_FUNCTION(regnsub); \
8251 COMMON_INTERCEPT_FUNCTION(regasub);
8252#else
8253#define INIT_REGEXSUB
8254#endif
8255
8256#if SANITIZER_INTERCEPT_FTS
8257INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
8258 int (*compar)(void **, void **)) {
8259 void *ctx;
8260 COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
8261 if (path_argv) {
8262 for (char *const *pa = path_argv; ; ++pa) {
8263 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
8264 if (!*pa)
8265 break;
8266 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
8267 }
8268 }
8269 // TODO(kamil): handle compar callback
8270 void *fts = REAL(fts_open)(path_argv, options, compar);
8271 if (fts)
8272 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
8273 return fts;
8274}
8275
8276INTERCEPTOR(void *, fts_read, void *ftsp) {
8277 void *ctx;
8278 COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
8279 if (ftsp)
8280 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8281 void *ftsent = REAL(fts_read)(ftsp);
8282 if (ftsent)
8283 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8284 return ftsent;
8285}
8286
8287INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
8288 void *ctx;
8289 COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
8290 if (ftsp)
8291 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8292 void *ftsent = REAL(fts_children)(ftsp, options);
8293 if (ftsent)
8294 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
8295 return ftsent;
8296}
8297
8298INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
8299 void *ctx;
8300 COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
8301 if (ftsp)
8302 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8303 if (f)
8304 COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
8305 return REAL(fts_set)(ftsp, f, options);
8306}
8307
8308INTERCEPTOR(int, fts_close, void *ftsp) {
8309 void *ctx;
8310 COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
8311 if (ftsp)
8312 COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
8313 return REAL(fts_close)(ftsp);
8314}
8315#define INIT_FTS \
8316 COMMON_INTERCEPT_FUNCTION(fts_open); \
8317 COMMON_INTERCEPT_FUNCTION(fts_read); \
8318 COMMON_INTERCEPT_FUNCTION(fts_children); \
8319 COMMON_INTERCEPT_FUNCTION(fts_set); \
8320 COMMON_INTERCEPT_FUNCTION(fts_close);
8321#else
8322#define INIT_FTS
8323#endif
8324
8325#if SANITIZER_INTERCEPT_SYSCTL
8326INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
8327 SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
8328 void *ctx;
8329 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8330 return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
8331 COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
8332 newlen);
8333 if (name)
8334 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
8335 if (oldlenp)
8336 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8337 if (newp && newlen)
8338 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8339 int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
8340 if (!res) {
8341 if (oldlenp) {
8342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8343 if (oldp)
8344 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8345 }
8346 }
8347 return res;
8348}
8349
8350INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
8351 void *newp, SIZE_T newlen) {
8352 void *ctx;
8353 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
8354 return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
8355 COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
8356 newlen);
8357 if (sname)
8358 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8359 if (oldlenp)
8360 COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8361 if (newp && newlen)
8362 COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
8363 int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
8364 if (!res) {
8365 if (oldlenp) {
8366 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
8367 if (oldp)
8368 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
8369 }
8370 }
8371 return res;
8372}
8373
8374INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
8375 SIZE_T *namelenp) {
8376 void *ctx;
8377 COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
8378 if (sname)
8379 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8380 if (namelenp)
8381 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8382 int res = REAL(sysctlnametomib)(sname, name, namelenp);
8383 if (!res) {
8384 if (namelenp) {
8385 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8386 if (name)
8387 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8388 }
8389 }
8390 return res;
8391}
8392
8393#define INIT_SYSCTL \
8394 COMMON_INTERCEPT_FUNCTION(sysctl); \
8395 COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
8396 COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
8397#else
8398#define INIT_SYSCTL
8399#endif
8400
8401#if SANITIZER_INTERCEPT_ASYSCTL
8402INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
8403 void *ctx;
8404 COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
8405 if (name)
8406 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
8407 void *res = REAL(asysctl)(name, namelen, len);
8408 if (res && len) {
8409 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8410 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8411 }
8412 return res;
8413}
8414
8415INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
8416 void *ctx;
8417 COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
8418 if (sname)
8419 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8420 void *res = REAL(asysctlbyname)(sname, len);
8421 if (res && len) {
8422 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8423 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
8424 }
8425 return res;
8426}
8427#define INIT_ASYSCTL \
8428 COMMON_INTERCEPT_FUNCTION(asysctl); \
8429 COMMON_INTERCEPT_FUNCTION(asysctlbyname);
8430#else
8431#define INIT_ASYSCTL
8432#endif
8433
8434#if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
8435INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
8436 unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
8437 int v) {
8438 void *ctx;
8439 COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
8440 csz, rnode, v);
8441 if (sname)
8442 COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
8443 if (namelenp)
8444 COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
8445 if (csz)
8446 COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
8447 // Skip rnode, it's rarely used and not trivial to sanitize
8448 // It's also used mostly internally
8449 int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
8450 if (!res) {
8451 if (namelenp) {
8452 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
8453 if (name)
8454 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
8455 }
8456 if (csz) {
8457 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
8458 if (cname)
8459 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
8460 }
8461 }
8462 return res;
8463}
8464#define INIT_SYSCTLGETMIBINFO \
8465 COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
8466#else
8467#define INIT_SYSCTLGETMIBINFO
8468#endif
8469
8470#if SANITIZER_INTERCEPT_NL_LANGINFO
8471INTERCEPTOR(char *, nl_langinfo, long item) {
8472 void *ctx;
8473 COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
8474 char *ret = REAL(nl_langinfo)(item);
8475 if (ret)
8476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8477 return ret;
8478}
8479#define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
8480#else
8481#define INIT_NL_LANGINFO
8482#endif
8483
8484#if SANITIZER_INTERCEPT_MODCTL
8485INTERCEPTOR(int, modctl, int operation, void *argp) {
8486 void *ctx;
8487 int ret;
8488 COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
8489
8490 if (operation == modctl_load) {
8491 if (argp) {
8492 __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
8493 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
8494 if (ml->ml_filename)
8495 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
8496 internal_strlen(ml->ml_filename) + 1);
8497 if (ml->ml_props)
8498 COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
8499 }
8500 ret = REAL(modctl)(operation, argp);
8501 } else if (operation == modctl_unload) {
8502 if (argp) {
8503 const char *name = (const char *)argp;
8504 COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
8505 }
8506 ret = REAL(modctl)(operation, argp);
8507 } else if (operation == modctl_stat) {
8508 uptr iov_len;
8509 struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
8510 if (iov) {
8511 COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
8512 iov_len = iov->iov_len;
8513 }
8514 ret = REAL(modctl)(operation, argp);
8515 if (iov)
8516 COMMON_INTERCEPTOR_WRITE_RANGE(
8517 ctx, iov->iov_base, Min(iov_len, iov->iov_len));
8518 } else if (operation == modctl_exists) {
8519 ret = REAL(modctl)(operation, argp);
8520 } else {
8521 ret = REAL(modctl)(operation, argp);
8522 }
8523
8524 return ret;
8525}
8526#define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
8527#else
8528#define INIT_MODCTL
8529#endif
8530
8531#if SANITIZER_INTERCEPT_STRTONUM
8532INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
8533 long long maxval, const char **errstr) {
8534 void *ctx;
8535 COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
8536
8537 // TODO(kamil): Implement strtoll as a common inteceptor
8538 char *real_endptr;
8539 long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
8540 StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
8541
8542 ret = REAL(strtonum)(nptr, minval, maxval, errstr);
8543 if (errstr) {
8544 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
8545 if (*errstr)
8546 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
8547 }
8548 return ret;
8549}
8550#define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
8551#else
8552#define INIT_STRTONUM
8553#endif
8554
8555#if SANITIZER_INTERCEPT_FPARSELN
8556INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
8557 SIZE_T *lineno, const char delim[3], int flags) {
8558 void *ctx;
8559 COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
8560 if (lineno)
8561 COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
8562 if (delim)
8563 COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
8564 char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
8565 if (ret) {
8566 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
8567 if (len)
8568 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
8569 if (lineno)
8570 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
8571 }
8572 return ret;
8573}
8574#define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
8575#else
8576#define INIT_FPARSELN
8577#endif
8578
8579#if SANITIZER_INTERCEPT_STATVFS1
8580INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
8581 void *ctx;
8582 COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
8583 if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
8584 int res = REAL(statvfs1)(path, buf, flags);
8585 if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8586 return res;
8587}
8588INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
8589 void *ctx;
8590 COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
8591 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
8592 int res = REAL(fstatvfs1)(fd, buf, flags);
8593 if (!res) {
8594 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
8595 if (fd >= 0)
8596 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
8597 }
8598 return res;
8599}
8600#define INIT_STATVFS1 \
8601 COMMON_INTERCEPT_FUNCTION(statvfs1); \
8602 COMMON_INTERCEPT_FUNCTION(fstatvfs1);
8603#else
8604#define INIT_STATVFS1
8605#endif
8606
8607#if SANITIZER_INTERCEPT_STRTOI
8608INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
8609 INTMAX_T low, INTMAX_T high, int *rstatus) {
8610 void *ctx;
8611 COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
8612 char *real_endptr;
8613 INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
8614 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8615 if (rstatus)
8616 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8617 return ret;
8618}
8619
8620INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
8621 UINTMAX_T low, UINTMAX_T high, int *rstatus) {
8622 void *ctx;
8623 COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
8624 char *real_endptr;
8625 UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
8626 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
8627 if (rstatus)
8628 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
8629 return ret;
8630}
8631#define INIT_STRTOI \
8632 COMMON_INTERCEPT_FUNCTION(strtoi); \
8633 COMMON_INTERCEPT_FUNCTION(strtou)
8634#else
8635#define INIT_STRTOI
8636#endif
8637
8638#if SANITIZER_INTERCEPT_CAPSICUM
8639#define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
8640 { \
8641 void *ctx; \
8642 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
8643 if (rights) \
8644 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8645 __sanitizer_cap_rights_t *ret = \
8646 REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
8647 if (ret) \
8648 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8649 return ret; \
8650 }
8651
8652#define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
8653 { \
8654 void *ctx; \
8655 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
8656 if (rights) \
8657 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8658 __sanitizer_cap_rights_t *ret = \
8659 REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
8660 if (ret) \
8661 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8662 return ret; \
8663 }
8664
8665#define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
8666 { \
8667 void *ctx; \
8668 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
8669 if (rights) \
8670 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8671 __sanitizer_cap_rights_t *ret = \
8672 REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
8673 if (ret) \
8674 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
8675 return ret; \
8676 }
8677
8678#define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
8679 { \
8680 void *ctx; \
8681 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
8682 if (rights) \
8683 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
8684 return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
8685 }
8686
8687INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
8688 __sanitizer_cap_rights_t *rights) {
8689 CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
8690}
8691
8692INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
8693 __sanitizer_cap_rights_t *rights) {
8694 CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
8695}
8696
8697INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
8698 __sanitizer_cap_rights_t *rights) {
8699 CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
8700}
8701
8702INTERCEPTOR(bool, cap_rights_is_set,
8703 __sanitizer_cap_rights_t *rights) {
8704 CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
8705}
8706
8707INTERCEPTOR(int, cap_rights_limit, int fd,
8708 const __sanitizer_cap_rights_t *rights) {
8709 void *ctx;
8710 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
8711 if (rights)
8712 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8713
8714 return REAL(cap_rights_limit)(fd, rights);
8715}
8716
8717INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
8718 void *ctx;
8719 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
8720 int ret = REAL(cap_rights_get)(fd, rights);
8721 if (!ret && rights)
8722 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
8723
8724 return ret;
8725}
8726
8727INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
8728 void *ctx;
8729 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
8730 if (rights)
8731 COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
8732
8733 return REAL(cap_rights_is_valid(rights));
8734}
8735
8736INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
8737 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8738 void *ctx;
8739 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
8740 if (src)
8741 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8742
8743 __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
8744 if (dst)
8745 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8746
8747 return ret;
8748}
8749
8750INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
8751 __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
8752 void *ctx;
8753 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
8754 if (src)
8755 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
8756
8757 __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
8758 if (dst)
8759 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
8760
8761 return ret;
8762}
8763
8764INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
8765 const __sanitizer_cap_rights *little) {
8766 void *ctx;
8767 COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
8768 if (little)
8769 COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
8770 if (big)
8771 COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
8772
8773 return REAL(cap_rights_contains)(big, little);
8774}
8775
8776INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
8777 void *ctx;
8778 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
8779 if (cmds)
8780 COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
8781
8782 return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
8783}
8784
8785INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
8786 void *ctx;
8787 COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
8788 int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
8789 if (!ret && cmds)
8790 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
8791
8792 return ret;
8793}
8794#define INIT_CAPSICUM \
8795 COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
8796 COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
8797 COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
8798 COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
8799 COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
8800 COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
8801 COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
8802 COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
8803 COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
8804 COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
8805 COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
8806 COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
8807#else
8808#define INIT_CAPSICUM
8809#endif
8810
8811#if SANITIZER_INTERCEPT_SHA1
8812INTERCEPTOR(void, SHA1Init, void *context) {
8813 void *ctx;
8814 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
8815 REAL(SHA1Init)(context);
8816 if (context)
8817 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8818}
8819INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
8820 void *ctx;
8821 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
8822 if (data && len > 0)
8823 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8824 if (context)
8825 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8826 REAL(SHA1Update)(context, data, len);
8827 if (context)
8828 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
8829}
8830INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
8831 void *ctx;
8832 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
8833 if (context)
8834 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8835 REAL(SHA1Final)(digest, context);
8836 if (digest)
8837 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
8838}
8839INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
8840 void *ctx;
8841 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
8842 if (state)
8843 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
8844 if (buffer)
8845 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
8846 REAL(SHA1Transform)(state, buffer);
8847 if (state)
8848 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
8849}
8850INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
8851 void *ctx;
8852 COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
8853 if (context)
8854 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
8855 char *ret = REAL(SHA1End)(context, buf);
8856 if (ret)
8857 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8858 return ret;
8859}
8860INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
8861 void *ctx;
8862 COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
8863 if (filename)
8864 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8865 char *ret = REAL(SHA1File)(filename, buf);
8866 if (ret)
8867 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8868 return ret;
8869}
8870INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
8871 OFF_T length) {
8872 void *ctx;
8873 COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
8874 if (filename)
8875 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8876 char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
8877 if (ret)
8878 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8879 return ret;
8880}
8881INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
8882 void *ctx;
8883 COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
8884 if (data)
8885 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8886 char *ret = REAL(SHA1Data)(data, len, buf);
8887 if (ret)
8888 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
8889 return ret;
8890}
8891#define INIT_SHA1 \
8892 COMMON_INTERCEPT_FUNCTION(SHA1Init); \
8893 COMMON_INTERCEPT_FUNCTION(SHA1Update); \
8894 COMMON_INTERCEPT_FUNCTION(SHA1Final); \
8895 COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
8896 COMMON_INTERCEPT_FUNCTION(SHA1End); \
8897 COMMON_INTERCEPT_FUNCTION(SHA1File); \
8898 COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
8899 COMMON_INTERCEPT_FUNCTION(SHA1Data)
8900#else
8901#define INIT_SHA1
8902#endif
8903
8904#if SANITIZER_INTERCEPT_MD4
8905INTERCEPTOR(void, MD4Init, void *context) {
8906 void *ctx;
8907 COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
8908 REAL(MD4Init)(context);
8909 if (context)
8910 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8911}
8912
8913INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
8914 unsigned int len) {
8915 void *ctx;
8916 COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
8917 if (data && len > 0)
8918 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8919 if (context)
8920 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8921 REAL(MD4Update)(context, data, len);
8922 if (context)
8923 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
8924}
8925
8926INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
8927 void *ctx;
8928 COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
8929 if (context)
8930 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8931 REAL(MD4Final)(digest, context);
8932 if (digest)
8933 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
8934}
8935
8936INTERCEPTOR(char *, MD4End, void *context, char *buf) {
8937 void *ctx;
8938 COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
8939 if (context)
8940 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
8941 char *ret = REAL(MD4End)(context, buf);
8942 if (ret)
8943 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8944 return ret;
8945}
8946
8947INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
8948 void *ctx;
8949 COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
8950 if (filename)
8951 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
8952 char *ret = REAL(MD4File)(filename, buf);
8953 if (ret)
8954 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8955 return ret;
8956}
8957
8958INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
8959 char *buf) {
8960 void *ctx;
8961 COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
8962 if (data && len > 0)
8963 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8964 char *ret = REAL(MD4Data)(data, len, buf);
8965 if (ret)
8966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
8967 return ret;
8968}
8969
8970#define INIT_MD4 \
8971 COMMON_INTERCEPT_FUNCTION(MD4Init); \
8972 COMMON_INTERCEPT_FUNCTION(MD4Update); \
8973 COMMON_INTERCEPT_FUNCTION(MD4Final); \
8974 COMMON_INTERCEPT_FUNCTION(MD4End); \
8975 COMMON_INTERCEPT_FUNCTION(MD4File); \
8976 COMMON_INTERCEPT_FUNCTION(MD4Data)
8977#else
8978#define INIT_MD4
8979#endif
8980
8981#if SANITIZER_INTERCEPT_RMD160
8982INTERCEPTOR(void, RMD160Init, void *context) {
8983 void *ctx;
8984 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
8985 REAL(RMD160Init)(context);
8986 if (context)
8987 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8988}
8989INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
8990 void *ctx;
8991 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
8992 if (data && len > 0)
8993 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
8994 if (context)
8995 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
8996 REAL(RMD160Update)(context, data, len);
8997 if (context)
8998 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
8999}
9000INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
9001 void *ctx;
9002 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
9003 if (context)
9004 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
9005 REAL(RMD160Final)(digest, context);
9006 if (digest)
9007 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
9008}
9009INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
9010 void *ctx;
9011 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
9012 if (state)
9013 COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
9014 if (buffer)
9015 COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
9016 REAL(RMD160Transform)(state, buffer);
9017 if (state)
9018 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
9019}
9020INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
9021 void *ctx;
9022 COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
9023 if (context)
9024 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
9025 char *ret = REAL(RMD160End)(context, buf);
9026 if (ret)
9027 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
9028 return ret;
9029}
9030INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
9031 void *ctx;
9032 COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
9033 if (filename)
9034 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
9035 char *ret = REAL(RMD160File)(filename, buf);
9036 if (ret)
9037 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
9038 return ret;
9039}
9040INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
9041 OFF_T length) {
9042 void *ctx;
9043 COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
9044 if (filename)
9045 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
9046 char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
9047 if (ret)
9048 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
9049 return ret;
9050}
9051INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
9052 void *ctx;
9053 COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
9054 if (data && len > 0)
9055 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
9056 char *ret = REAL(RMD160Data)(data, len, buf);
9057 if (ret)
9058 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
9059 return ret;
9060}
9061#define INIT_RMD160 \
9062 COMMON_INTERCEPT_FUNCTION(RMD160Init); \
9063 COMMON_INTERCEPT_FUNCTION(RMD160Update); \
9064 COMMON_INTERCEPT_FUNCTION(RMD160Final); \
9065 COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
9066 COMMON_INTERCEPT_FUNCTION(RMD160End); \
9067 COMMON_INTERCEPT_FUNCTION(RMD160File); \
9068 COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
9069 COMMON_INTERCEPT_FUNCTION(RMD160Data)
9070#else
9071#define INIT_RMD160
9072#endif
9073
9074#if SANITIZER_INTERCEPT_FSEEK
9075INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
9076 void *ctx;
9077 COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
9078 return REAL(fseek)(stream, offset, whence);
9079}
9080INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
9081 void *ctx;
9082 COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
9083 return REAL(fseeko)(stream, offset, whence);
9084}
9085INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
9086 void *ctx;
9087 COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
9088 return REAL(ftell)(stream);
9089}
9090INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
9091 void *ctx;
9092 COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
9093 return REAL(ftello)(stream);
9094}
9095INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
9096 void *ctx;
9097 COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
9098 return REAL(rewind)(stream);
9099}
9100INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
9101 void *ctx;
9102 COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
9103 int ret = REAL(fgetpos)(stream, pos);
9104 if (pos && !ret)
9105 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
9106 return ret;
9107}
9108INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
9109 void *ctx;
9110 COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
9111 if (pos)
9112 COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
9113 return REAL(fsetpos)(stream, pos);
9114}
9115#define INIT_FSEEK \
9116 COMMON_INTERCEPT_FUNCTION(fseek); \
9117 COMMON_INTERCEPT_FUNCTION(fseeko); \
9118 COMMON_INTERCEPT_FUNCTION(ftell); \
9119 COMMON_INTERCEPT_FUNCTION(ftello); \
9120 COMMON_INTERCEPT_FUNCTION(rewind); \
9121 COMMON_INTERCEPT_FUNCTION(fgetpos); \
9122 COMMON_INTERCEPT_FUNCTION(fsetpos)
9123#else
9124#define INIT_FSEEK
9125#endif
9126
9127#if SANITIZER_INTERCEPT_MD2
9128INTERCEPTOR(void, MD2Init, void *context) {
9129 void *ctx;
9130 COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
9131 REAL(MD2Init)(context);
9132 if (context)
9133 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
9134}
9135
9136INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
9137 unsigned int len) {
9138 void *ctx;
9139 COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
9140 if (data && len > 0)
9141 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
9142 if (context)
9143 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
9144 REAL(MD2Update)(context, data, len);
9145 if (context)
9146 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
9147}
9148
9149INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
9150 void *ctx;
9151 COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
9152 if (context)
9153 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
9154 REAL(MD2Final)(digest, context);
9155 if (digest)
9156 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
9157}
9158
9159INTERCEPTOR(char *, MD2End, void *context, char *buf) {
9160 void *ctx;
9161 COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
9162 if (context)
9163 COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
9164 char *ret = REAL(MD2End)(context, buf);
9165 if (ret)
9166 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9167 return ret;
9168}
9169
9170INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
9171 void *ctx;
9172 COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
9173 if (filename)
9174 COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
9175 char *ret = REAL(MD2File)(filename, buf);
9176 if (ret)
9177 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9178 return ret;
9179}
9180
9181INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
9182 char *buf) {
9183 void *ctx;
9184 COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
9185 if (data && len > 0)
9186 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
9187 char *ret = REAL(MD2Data)(data, len, buf);
9188 if (ret)
9189 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
9190 return ret;
9191}
9192
9193#define INIT_MD2 \
9194 COMMON_INTERCEPT_FUNCTION(MD2Init); \
9195 COMMON_INTERCEPT_FUNCTION(MD2Update); \
9196 COMMON_INTERCEPT_FUNCTION(MD2Final); \
9197 COMMON_INTERCEPT_FUNCTION(MD2End); \
9198 COMMON_INTERCEPT_FUNCTION(MD2File); \
9199 COMMON_INTERCEPT_FUNCTION(MD2Data)
9200#else
9201#define INIT_MD2
9202#endif
9203
9204#if SANITIZER_INTERCEPT_VIS
9205INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
9206 void *ctx;
9207 COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
9208 char *end = REAL(vis)(dst, c, flag, nextc);
9209 // dst is NULL terminated and end points to the NULL char
9210 if (dst && end)
9211 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9212 return end;
9213}
9214INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
9215 void *ctx;
9216 COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
9217 char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
9218 // nvis cannot make sure the dst is NULL terminated
9219 if (dst && end)
9220 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9221 return end;
9222}
9223INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
9224 void *ctx;
9225 COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
9226 if (src)
9227 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9228 int len = REAL(strvis)(dst, src, flag);
9229 if (dst)
9230 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9231 return len;
9232}
9233INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
9234 void *ctx;
9235 COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
9236 if (src)
9237 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9238 int len = REAL(stravis)(dst, src, flag);
9239 if (dst) {
9240 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
9241 if (*dst)
9242 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
9243 }
9244 return len;
9245}
9246INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
9247 void *ctx;
9248 COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
9249 if (src)
9250 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9251 int len = REAL(strnvis)(dst, dlen, src, flag);
9252 // The interface will be valid even if there is no space for NULL char
9253 if (dst && len > 0)
9254 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9255 return len;
9256}
9257INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
9258 void *ctx;
9259 COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
9260 if (src)
9261 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9262 int ret = REAL(strvisx)(dst, src, len, flag);
9263 if (dst)
9264 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9265 return ret;
9266}
9267INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9268 int flag) {
9269 void *ctx;
9270 COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
9271 if (src)
9272 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9273 int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
9274 if (dst && ret >= 0)
9275 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9276 return ret;
9277}
9278INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9279 int flag, int *cerr_ptr) {
9280 void *ctx;
9281 COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
9282 if (src)
9283 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9284 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9285 // according to the implementation
9286 if (cerr_ptr)
9287 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9288 int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
9289 if (dst && ret >= 0)
9290 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9291 if (cerr_ptr)
9292 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9293 return ret;
9294}
9295INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
9296 const char *extra) {
9297 void *ctx;
9298 COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
9299 if (extra)
9300 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9301 char *end = REAL(svis)(dst, c, flag, nextc, extra);
9302 if (dst && end)
9303 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
9304 return end;
9305}
9306INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
9307 const char *extra) {
9308 void *ctx;
9309 COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
9310 if (extra)
9311 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9312 char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
9313 if (dst && end)
9314 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
9315 Min((SIZE_T)(end - dst + 1), dlen));
9316 return end;
9317}
9318INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
9319 const char *extra) {
9320 void *ctx;
9321 COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
9322 if (src)
9323 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9324 if (extra)
9325 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9326 int len = REAL(strsvis)(dst, src, flag, extra);
9327 if (dst)
9328 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9329 return len;
9330}
9331INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
9332 const char *extra) {
9333 void *ctx;
9334 COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
9335 if (src)
9336 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9337 if (extra)
9338 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9339 int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
9340 // The interface will be valid even if there is no space for NULL char
9341 if (dst && len >= 0)
9342 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
9343 return len;
9344}
9345INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
9346 const char *extra) {
9347 void *ctx;
9348 COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
9349 if (src)
9350 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9351 if (extra)
9352 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9353 int ret = REAL(strsvisx)(dst, src, len, flag, extra);
9354 if (dst)
9355 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9356 return ret;
9357}
9358INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
9359 int flag, const char *extra) {
9360 void *ctx;
9361 COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
9362 if (src)
9363 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9364 if (extra)
9365 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9366 int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
9367 if (dst && ret >= 0)
9368 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9369 return ret;
9370}
9371INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
9372 SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
9373 void *ctx;
9374 COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
9375 cerr_ptr);
9376 if (src)
9377 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
9378 if (extra)
9379 COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
9380 // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
9381 // according to the implementation
9382 if (cerr_ptr)
9383 COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
9384 int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
9385 if (dst && ret >= 0)
9386 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9387 if (cerr_ptr)
9388 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
9389 return ret;
9390}
9391INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
9392 void *ctx;
9393 COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
9394 if (astate)
9395 COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
9396 int ret = REAL(unvis)(cp, c, astate, flag);
9397 if (ret == unvis_valid || ret == unvis_validpush) {
9398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
9399 }
9400 return ret;
9401}
9402INTERCEPTOR(int, strunvis, char *dst, const char *src) {
9403 void *ctx;
9404 COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
9405 if (src)
9406 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9407 int ret = REAL(strunvis)(dst, src);
9408 if (ret != -1)
9409 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9410 return ret;
9411}
9412INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
9413 void *ctx;
9414 COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
9415 if (src)
9416 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9417 int ret = REAL(strnunvis)(dst, dlen, src);
9418 if (ret != -1)
9419 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9420 return ret;
9421}
9422INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
9423 void *ctx;
9424 COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
9425 if (src)
9426 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9427 int ret = REAL(strunvisx)(dst, src, flag);
9428 if (ret != -1)
9429 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9430 return ret;
9431}
9432INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
9433 int flag) {
9434 void *ctx;
9435 COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
9436 if (src)
9437 COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
9438 int ret = REAL(strnunvisx)(dst, dlen, src, flag);
9439 if (ret != -1)
9440 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
9441 return ret;
9442}
9443#define INIT_VIS \
9444 COMMON_INTERCEPT_FUNCTION(vis); \
9445 COMMON_INTERCEPT_FUNCTION(nvis); \
9446 COMMON_INTERCEPT_FUNCTION(strvis); \
9447 COMMON_INTERCEPT_FUNCTION(stravis); \
9448 COMMON_INTERCEPT_FUNCTION(strnvis); \
9449 COMMON_INTERCEPT_FUNCTION(strvisx); \
9450 COMMON_INTERCEPT_FUNCTION(strnvisx); \
9451 COMMON_INTERCEPT_FUNCTION(strenvisx); \
9452 COMMON_INTERCEPT_FUNCTION(svis); \
9453 COMMON_INTERCEPT_FUNCTION(snvis); \
9454 COMMON_INTERCEPT_FUNCTION(strsvis); \
9455 COMMON_INTERCEPT_FUNCTION(strsnvis); \
9456 COMMON_INTERCEPT_FUNCTION(strsvisx); \
9457 COMMON_INTERCEPT_FUNCTION(strsnvisx); \
9458 COMMON_INTERCEPT_FUNCTION(strsenvisx); \
9459 COMMON_INTERCEPT_FUNCTION(unvis); \
9460 COMMON_INTERCEPT_FUNCTION(strunvis); \
9461 COMMON_INTERCEPT_FUNCTION(strnunvis); \
9462 COMMON_INTERCEPT_FUNCTION(strunvisx); \
9463 COMMON_INTERCEPT_FUNCTION(strnunvisx)
9464#else
9465#define INIT_VIS
9466#endif
9467
9468#if SANITIZER_INTERCEPT_CDB
9469INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
9470 void *ctx;
9471 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
9472 if (path)
9473 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9474 struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
9475 if (cdbr)
9476 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9477 return cdbr;
9478}
9479
9480INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
9481 int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
9482 void *ctx;
9483 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
9484 cookie);
9485 if (base && size)
9486 COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
9487 struct __sanitizer_cdbr *cdbr =
9488 REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
9489 if (cdbr)
9490 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
9491 return cdbr;
9492}
9493
9494INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
9495 void *ctx;
9496 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
9497 if (cdbr)
9498 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9499 return REAL(cdbr_entries)(cdbr);
9500}
9501
9502INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
9503 const void **data, SIZE_T *datalen) {
9504 void *ctx;
9505 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
9506 if (cdbr)
9507 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9508 int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
9509 if (!ret) {
9510 if (data)
9511 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9512 if (datalen)
9513 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9514 if (data && datalen)
9515 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9516 }
9517 return ret;
9518}
9519
9520INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
9521 SIZE_T keylen, const void **data, SIZE_T *datalen) {
9522 void *ctx;
9523 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
9524 if (cdbr)
9525 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9526 if (key)
9527 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9528 int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
9529 if (!ret) {
9530 if (data)
9531 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
9532 if (datalen)
9533 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
9534 if (data && datalen)
9535 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
9536 }
9537 return ret;
9538}
9539
9540INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
9541 void *ctx;
9542 COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
9543 if (cdbr)
9544 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
9545 REAL(cdbr_close)(cdbr);
9546}
9547
9548INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
9549 void *ctx;
9550 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
9551 struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
9552 if (ret)
9553 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
9554 return ret;
9555}
9556
9557INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
9558 SIZE_T keylen, const void *data, SIZE_T datalen) {
9559 void *ctx;
9560 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
9561 if (cdbw)
9562 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9563 if (data && datalen)
9564 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9565 if (key && keylen)
9566 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9567 int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
9568 if (!ret && cdbw)
9569 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9570 return ret;
9571}
9572
9573INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
9574 SIZE_T datalen, u32 *index) {
9575 void *ctx;
9576 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
9577 if (cdbw)
9578 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9579 if (data && datalen)
9580 COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
9581 int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
9582 if (!ret) {
9583 if (index)
9584 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
9585 if (cdbw)
9586 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9587 }
9588 return ret;
9589}
9590
9591INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
9592 SIZE_T keylen, u32 index) {
9593 void *ctx;
9594 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
9595 if (cdbw)
9596 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9597 if (key && keylen)
9598 COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
9599 int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
9600 if (!ret && cdbw)
9601 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9602 return ret;
9603}
9604
9605INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
9606 const char descr[16], u32 (*seedgen)(void)) {
9607 void *ctx;
9608 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
9609 COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
9610 if (cdbw)
9611 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9612 if (descr)
9613 COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
9614 if (seedgen)
9615 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
9616 int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
9617 if (!ret) {
9618 if (cdbw)
9619 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
9620 if (output >= 0)
9621 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
9622 }
9623 return ret;
9624}
9625
9626INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
9627 void *ctx;
9628 COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
9629 if (cdbw)
9630 COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
9631 REAL(cdbw_close)(cdbw);
9632}
9633
9634#define INIT_CDB \
9635 COMMON_INTERCEPT_FUNCTION(cdbr_open); \
9636 COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
9637 COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
9638 COMMON_INTERCEPT_FUNCTION(cdbr_get); \
9639 COMMON_INTERCEPT_FUNCTION(cdbr_find); \
9640 COMMON_INTERCEPT_FUNCTION(cdbr_close); \
9641 COMMON_INTERCEPT_FUNCTION(cdbw_open); \
9642 COMMON_INTERCEPT_FUNCTION(cdbw_put); \
9643 COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
9644 COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
9645 COMMON_INTERCEPT_FUNCTION(cdbw_output); \
9646 COMMON_INTERCEPT_FUNCTION(cdbw_close)
9647#else
9648#define INIT_CDB
9649#endif
9650
9651#if SANITIZER_INTERCEPT_GETFSENT
9652INTERCEPTOR(void *, getfsent) {
9653 void *ctx;
9654 COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
9655 void *ret = REAL(getfsent)();
9656 if (ret)
9657 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9658 return ret;
9659}
9660
9661INTERCEPTOR(void *, getfsspec, const char *spec) {
9662 void *ctx;
9663 COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
9664 if (spec)
9665 COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
9666 void *ret = REAL(getfsspec)(spec);
9667 if (ret)
9668 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9669 return ret;
9670}
9671
9672INTERCEPTOR(void *, getfsfile, const char *file) {
9673 void *ctx;
9674 COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
9675 if (file)
9676 COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
9677 void *ret = REAL(getfsfile)(file);
9678 if (ret)
9679 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
9680 return ret;
9681}
9682
9683#define INIT_GETFSENT \
9684 COMMON_INTERCEPT_FUNCTION(getfsent); \
9685 COMMON_INTERCEPT_FUNCTION(getfsspec); \
9686 COMMON_INTERCEPT_FUNCTION(getfsfile);
9687#else
9688#define INIT_GETFSENT
9689#endif
9690
9691#if SANITIZER_INTERCEPT_ARC4RANDOM
9692INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
9693 void *ctx;
9694 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
9695 REAL(arc4random_buf)(buf, len);
9696 if (buf && len)
9697 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
9698}
9699
9700INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
9701 void *ctx;
9702 COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
9703 if (dat && datlen)
9704 COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
9705 REAL(arc4random_addrandom)(dat, datlen);
9706}
9707
9708#define INIT_ARC4RANDOM \
9709 COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
9710 COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
9711#else
9712#define INIT_ARC4RANDOM
9713#endif
9714
9715#if SANITIZER_INTERCEPT_POPEN
9716INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
9717 void *ctx;
9718 COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
9719 if (command)
9720 COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
9721 if (type)
9722 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9723 __sanitizer_FILE *res = REAL(popen)(command, type);
9724 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9725 if (res) unpoison_file(fp: res);
9726 return res;
9727}
9728#define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
9729#else
9730#define INIT_POPEN
9731#endif
9732
9733#if SANITIZER_INTERCEPT_POPENVE
9734INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
9735 char *const *argv, char *const *envp, const char *type) {
9736 void *ctx;
9737 COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
9738 if (path)
9739 COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
9740 if (argv) {
9741 for (char *const *pa = argv; ; ++pa) {
9742 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9743 if (!*pa)
9744 break;
9745 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9746 }
9747 }
9748 if (envp) {
9749 for (char *const *pa = envp; ; ++pa) {
9750 COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
9751 if (!*pa)
9752 break;
9753 COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
9754 }
9755 }
9756 if (type)
9757 COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
9758 __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
9759 COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
9760 if (res) unpoison_file(res);
9761 return res;
9762}
9763#define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
9764#else
9765#define INIT_POPENVE
9766#endif
9767
9768#if SANITIZER_INTERCEPT_PCLOSE
9769INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
9770 void *ctx;
9771 COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
9772 COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
9773 const FileMetadata *m = GetInterceptorMetadata(addr: fp);
9774 int res = REAL(pclose)(fp);
9775 if (m) {
9776 COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
9777 DeleteInterceptorMetadata(addr: fp);
9778 }
9779 return res;
9780}
9781#define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
9782#else
9783#define INIT_PCLOSE
9784#endif
9785
9786#if SANITIZER_INTERCEPT_FUNOPEN
9787typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
9788typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
9789typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
9790typedef int (*funopen_closefn)(void *cookie);
9791
9792struct WrappedFunopenCookie {
9793 void *real_cookie;
9794 funopen_readfn real_read;
9795 funopen_writefn real_write;
9796 funopen_seekfn real_seek;
9797 funopen_closefn real_close;
9798};
9799
9800static int wrapped_funopen_read(void *cookie, char *buf, int len) {
9801 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9802 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9803 funopen_readfn real_read = wrapped_cookie->real_read;
9804 return real_read(wrapped_cookie->real_cookie, buf, len);
9805}
9806
9807static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
9808 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9809 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9810 funopen_writefn real_write = wrapped_cookie->real_write;
9811 return real_write(wrapped_cookie->real_cookie, buf, len);
9812}
9813
9814static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
9815 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9816 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9817 funopen_seekfn real_seek = wrapped_cookie->real_seek;
9818 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9819}
9820
9821static int wrapped_funopen_close(void *cookie) {
9822 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9823 WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
9824 funopen_closefn real_close = wrapped_cookie->real_close;
9825 int res = real_close(wrapped_cookie->real_cookie);
9826 InternalFree(wrapped_cookie);
9827 return res;
9828}
9829
9830INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
9831 funopen_writefn writefn, funopen_seekfn seekfn,
9832 funopen_closefn closefn) {
9833 void *ctx;
9834 COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
9835 closefn);
9836
9837 WrappedFunopenCookie *wrapped_cookie =
9838 (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
9839 wrapped_cookie->real_cookie = cookie;
9840 wrapped_cookie->real_read = readfn;
9841 wrapped_cookie->real_write = writefn;
9842 wrapped_cookie->real_seek = seekfn;
9843 wrapped_cookie->real_close = closefn;
9844
9845 __sanitizer_FILE *res =
9846 REAL(funopen)(wrapped_cookie,
9847 readfn ? wrapped_funopen_read : nullptr,
9848 writefn ? wrapped_funopen_write : nullptr,
9849 seekfn ? wrapped_funopen_seek : nullptr,
9850 closefn ? wrapped_funopen_close : nullptr);
9851 if (res)
9852 unpoison_file(res);
9853 return res;
9854}
9855#define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
9856#else
9857#define INIT_FUNOPEN
9858#endif
9859
9860#if SANITIZER_INTERCEPT_FUNOPEN2
9861typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
9862typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
9863typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
9864typedef int (*funopen2_flushfn)(void *cookie);
9865typedef int (*funopen2_closefn)(void *cookie);
9866
9867struct WrappedFunopen2Cookie {
9868 void *real_cookie;
9869 funopen2_readfn real_read;
9870 funopen2_writefn real_write;
9871 funopen2_seekfn real_seek;
9872 funopen2_flushfn real_flush;
9873 funopen2_closefn real_close;
9874};
9875
9876static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
9877 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9878 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9879 funopen2_readfn real_read = wrapped_cookie->real_read;
9880 return real_read(wrapped_cookie->real_cookie, buf, len);
9881}
9882
9883static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
9884 SIZE_T len) {
9885 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9886 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9887 funopen2_writefn real_write = wrapped_cookie->real_write;
9888 return real_write(wrapped_cookie->real_cookie, buf, len);
9889}
9890
9891static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
9892 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
9893 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9894 funopen2_seekfn real_seek = wrapped_cookie->real_seek;
9895 return real_seek(wrapped_cookie->real_cookie, offset, whence);
9896}
9897
9898static int wrapped_funopen2_flush(void *cookie) {
9899 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9900 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9901 funopen2_flushfn real_flush = wrapped_cookie->real_flush;
9902 return real_flush(wrapped_cookie->real_cookie);
9903}
9904
9905static int wrapped_funopen2_close(void *cookie) {
9906 COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
9907 WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
9908 funopen2_closefn real_close = wrapped_cookie->real_close;
9909 int res = real_close(wrapped_cookie->real_cookie);
9910 InternalFree(wrapped_cookie);
9911 return res;
9912}
9913
9914INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
9915 funopen2_writefn writefn, funopen2_seekfn seekfn,
9916 funopen2_flushfn flushfn, funopen2_closefn closefn) {
9917 void *ctx;
9918 COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
9919 flushfn, closefn);
9920
9921 WrappedFunopen2Cookie *wrapped_cookie =
9922 (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
9923 wrapped_cookie->real_cookie = cookie;
9924 wrapped_cookie->real_read = readfn;
9925 wrapped_cookie->real_write = writefn;
9926 wrapped_cookie->real_seek = seekfn;
9927 wrapped_cookie->real_flush = flushfn;
9928 wrapped_cookie->real_close = closefn;
9929
9930 __sanitizer_FILE *res =
9931 REAL(funopen2)(wrapped_cookie,
9932 readfn ? wrapped_funopen2_read : nullptr,
9933 writefn ? wrapped_funopen2_write : nullptr,
9934 seekfn ? wrapped_funopen2_seek : nullptr,
9935 flushfn ? wrapped_funopen2_flush : nullptr,
9936 closefn ? wrapped_funopen2_close : nullptr);
9937 if (res)
9938 unpoison_file(res);
9939 return res;
9940}
9941#define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
9942#else
9943#define INIT_FUNOPEN2
9944#endif
9945
9946#if SANITIZER_INTERCEPT_FDEVNAME
9947INTERCEPTOR(char *, fdevname, int fd) {
9948 void *ctx;
9949 COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
9950 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9951 char *name = REAL(fdevname)(fd);
9952 if (name) {
9953 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
9954 if (fd > 0)
9955 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9956 }
9957 return name;
9958}
9959
9960INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
9961 void *ctx;
9962 COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
9963 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
9964 char *name = REAL(fdevname_r)(fd, buf, len);
9965 if (name && buf && len > 0) {
9966 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
9967 if (fd > 0)
9968 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
9969 }
9970 return name;
9971}
9972
9973#define INIT_FDEVNAME \
9974 COMMON_INTERCEPT_FUNCTION(fdevname); \
9975 COMMON_INTERCEPT_FUNCTION(fdevname_r);
9976#else
9977#define INIT_FDEVNAME
9978#endif
9979
9980#if SANITIZER_INTERCEPT_GETUSERSHELL
9981INTERCEPTOR(char *, getusershell,) {
9982 void *ctx;
9983 COMMON_INTERCEPTOR_ENTER(ctx, getusershell,);
9984 char *res = REAL(getusershell)();
9985 if (res)
9986 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
9987 return res;
9988}
9989
9990#define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
9991#else
9992#define INIT_GETUSERSHELL
9993#endif
9994
9995#if SANITIZER_INTERCEPT_SL_INIT
9996INTERCEPTOR(void *, sl_init) {
9997 void *ctx;
9998 COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
9999 void *res = REAL(sl_init)();
10000 if (res)
10001 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
10002 return res;
10003}
10004
10005INTERCEPTOR(int, sl_add, void *sl, char *item) {
10006 void *ctx;
10007 COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
10008 if (sl)
10009 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
10010 if (item)
10011 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
10012 int res = REAL(sl_add)(sl, item);
10013 if (!res)
10014 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
10015 return res;
10016}
10017
10018INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
10019 void *ctx;
10020 COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
10021 if (sl)
10022 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
10023 if (item)
10024 COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
10025 char *res = REAL(sl_find)(sl, item);
10026 if (res)
10027 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
10028 return res;
10029}
10030
10031INTERCEPTOR(void, sl_free, void *sl, int freeall) {
10032 void *ctx;
10033 COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
10034 if (sl)
10035 COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
10036 REAL(sl_free)(sl, freeall);
10037}
10038
10039#define INIT_SL_INIT \
10040 COMMON_INTERCEPT_FUNCTION(sl_init); \
10041 COMMON_INTERCEPT_FUNCTION(sl_add); \
10042 COMMON_INTERCEPT_FUNCTION(sl_find); \
10043 COMMON_INTERCEPT_FUNCTION(sl_free);
10044#else
10045#define INIT_SL_INIT
10046#endif
10047
10048#if SANITIZER_INTERCEPT_GETRANDOM
10049INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
10050 void *ctx;
10051 COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
10052 // If GRND_NONBLOCK is set in the flags, it is non blocking.
10053 static const int grnd_nonblock = 1;
10054 SSIZE_T n;
10055 if ((flags & grnd_nonblock))
10056 n = REAL(getrandom)(buf, buflen, flags);
10057 else
10058 n = COMMON_INTERCEPTOR_BLOCK_REAL(getrandom)(buf, buflen, flags);
10059 if (n > 0) {
10060 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
10061 }
10062 return n;
10063}
10064#define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
10065#else
10066#define INIT_GETRANDOM
10067#endif
10068
10069#if SANITIZER_INTERCEPT_GETENTROPY
10070INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
10071 void *ctx;
10072 COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
10073 int r = REAL(getentropy)(buf, buflen);
10074 if (r == 0) {
10075 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
10076 }
10077 return r;
10078}
10079#define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
10080#else
10081#define INIT_GETENTROPY
10082#endif
10083
10084#if SANITIZER_INTERCEPT_QSORT_R
10085typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
10086struct qsort_r_compar_params {
10087 SIZE_T size;
10088 qsort_r_compar_f compar;
10089 void *arg;
10090};
10091static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
10092 qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
10093 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
10094 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
10095 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
10096 return params->compar(a, b, params->arg);
10097}
10098
10099INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
10100 qsort_r_compar_f compar, void *arg) {
10101 void *ctx;
10102 COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
10103 // Run the comparator over all array elements to detect any memory issues.
10104 if (nmemb > 1) {
10105 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
10106 void *p = (void *)((char *)base + i * size);
10107 void *q = (void *)((char *)base + (i + 1) * size);
10108 COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
10109 compar(p, q, arg);
10110 }
10111 }
10112 qsort_r_compar_params params = {.size: size, .compar: compar, .arg: arg};
10113 REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, &params);
10114 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
10115}
10116# define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
10117#else
10118# define INIT_QSORT_R
10119#endif
10120
10121#if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
10122INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10123 qsort_r_compar_f compar) {
10124 void *ctx;
10125 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10126 WRAP(qsort_r)(base, nmemb, size, compar, arg: nullptr);
10127}
10128# define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10129#elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
10130// Glibc qsort uses a temporary buffer allocated either on stack or on heap.
10131// Poisoned memory from there may get copied into the comparator arguments,
10132// where it needs to be dealt with. But even that is not enough - the results of
10133// the sort may be copied into the input/output array based on the results of
10134// the comparator calls, but directly from the temp memory, bypassing the
10135// unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
10136// unpoisoning the entire array after the sort is done.
10137//
10138// We can not check that the entire array is initialized at the beginning. IMHO,
10139// it's fine for parts of the sorted objects to contain uninitialized memory,
10140// ex. as padding in structs.
10141typedef int (*qsort_compar_f)(const void *, const void *);
10142static THREADLOCAL qsort_compar_f qsort_compar;
10143static THREADLOCAL SIZE_T qsort_size;
10144static int wrapped_qsort_compar(const void *a, const void *b) {
10145 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10146 COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
10147 COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
10148 return qsort_compar(a, b);
10149}
10150
10151INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
10152 qsort_compar_f compar) {
10153 void *ctx;
10154 COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
10155 // Run the comparator over all array elements to detect any memory issues.
10156 if (nmemb > 1) {
10157 for (SIZE_T i = 0; i < nmemb - 1; ++i) {
10158 void *p = (void *)((char *)base + i * size);
10159 void *q = (void *)((char *)base + (i + 1) * size);
10160 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10161 compar(p, q);
10162 }
10163 }
10164 qsort_compar_f old_compar = qsort_compar;
10165 SIZE_T old_size = qsort_size;
10166 // Handle qsort() implementations that recurse using an
10167 // interposable function call:
10168 bool already_wrapped = compar == wrapped_qsort_compar;
10169 if (already_wrapped) {
10170 // This case should only happen if the qsort() implementation calls itself
10171 // using a preemptible function call (e.g. the FreeBSD libc version).
10172 // Check that the size and comparator arguments are as expected.
10173 CHECK_NE(compar, qsort_compar);
10174 CHECK_EQ(qsort_size, size);
10175 } else {
10176 qsort_compar = compar;
10177 qsort_size = size;
10178 }
10179 REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
10180 if (!already_wrapped) {
10181 qsort_compar = old_compar;
10182 qsort_size = old_size;
10183 }
10184 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
10185}
10186# define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
10187#else
10188# define INIT_QSORT
10189#endif
10190
10191#if SANITIZER_INTERCEPT_BSEARCH
10192typedef int (*bsearch_compar_f)(const void *, const void *);
10193struct bsearch_compar_params {
10194 const void *key;
10195 bsearch_compar_f compar;
10196};
10197
10198static int wrapped_bsearch_compar(const void *key, const void *b) {
10199 const bsearch_compar_params *params = (const bsearch_compar_params *)key;
10200 COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
10201 return params->compar(params->key, b);
10202}
10203
10204INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
10205 SIZE_T size, bsearch_compar_f compar) {
10206 void *ctx;
10207 COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
10208 bsearch_compar_params params = {.key: key, .compar: compar};
10209 return REAL(bsearch)(&params, base, nmemb, size, wrapped_bsearch_compar);
10210}
10211# define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
10212#else
10213# define INIT_BSEARCH
10214#endif
10215
10216#if SANITIZER_INTERCEPT_SIGALTSTACK
10217INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
10218 void *ctx;
10219 COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
10220 int r = REAL(sigaltstack)(ss, oss);
10221 if (r == 0 && oss != nullptr) {
10222 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
10223 }
10224 return r;
10225}
10226#define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
10227#else
10228#define INIT_SIGALTSTACK
10229#endif
10230
10231#if SANITIZER_INTERCEPT_PROCCTL
10232INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
10233 void *ctx;
10234 COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
10235 static const int PROC_REAP_ACQUIRE = 2;
10236 static const int PROC_REAP_RELEASE = 3;
10237 static const int PROC_REAP_STATUS = 4;
10238 static const int PROC_REAP_GETPIDS = 5;
10239 static const int PROC_REAP_KILL = 6;
10240 if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
10241 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
10242 } else {
10243 // reap_acquire/reap_release bears no arguments.
10244 if (cmd > PROC_REAP_RELEASE) {
10245 unsigned int reapsz;
10246 switch (cmd) {
10247 case PROC_REAP_STATUS:
10248 reapsz = struct_procctl_reaper_status_sz;
10249 break;
10250 case PROC_REAP_GETPIDS:
10251 reapsz = struct_procctl_reaper_pids_sz;
10252 break;
10253 case PROC_REAP_KILL:
10254 reapsz = struct_procctl_reaper_kill_sz;
10255 break;
10256 }
10257 COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
10258 }
10259 }
10260 return REAL(procctl)(idtype, id, cmd, data);
10261}
10262#define INIT_PROCCTL COMMON_INTERCEPT_FUNCTION(procctl)
10263#else
10264#define INIT_PROCCTL
10265#endif
10266
10267#if SANITIZER_INTERCEPT_UNAME
10268INTERCEPTOR(int, uname, struct utsname *utsname) {
10269#if SANITIZER_LINUX
10270 if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
10271 return internal_uname(buf: utsname);
10272#endif
10273 void *ctx;
10274 COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
10275 int res = REAL(uname)(utsname);
10276 if (!res)
10277 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10278 __sanitizer::struct_utsname_sz);
10279 return res;
10280}
10281#define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
10282#else
10283#define INIT_UNAME
10284#endif
10285
10286#if SANITIZER_INTERCEPT___XUNAME
10287// FreeBSD's <sys/utsname.h> define uname() as
10288// static __inline int uname(struct utsname *name) {
10289// return __xuname(SYS_NMLN, (void*)name);
10290// }
10291INTERCEPTOR(int, __xuname, int size, void *utsname) {
10292 void *ctx;
10293 COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
10294 int res = REAL(__xuname)(size, utsname);
10295 if (!res)
10296 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
10297 __sanitizer::struct_utsname_sz);
10298 return res;
10299}
10300#define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
10301#else
10302#define INIT___XUNAME
10303#endif
10304
10305#if SANITIZER_INTERCEPT_ARGP_PARSE
10306INTERCEPTOR(int, argp_parse, const struct argp *argp, int argc, char **argv,
10307 unsigned flags, int *arg_index, void *input) {
10308 void *ctx;
10309 COMMON_INTERCEPTOR_ENTER(ctx, argp_parse, argp, argc, argv, flags, arg_index,
10310 input);
10311 for (int i = 0; i < argc; i++)
10312 COMMON_INTERCEPTOR_READ_RANGE(ctx, argv[i], internal_strlen(argv[i]) + 1);
10313 int res = REAL(argp_parse)(argp, argc, argv, flags, arg_index, input);
10314 if (!res && arg_index)
10315 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg_index, sizeof(int));
10316 return res;
10317}
10318
10319#define INIT_ARGP_PARSE COMMON_INTERCEPT_FUNCTION(argp_parse);
10320#else
10321#define INIT_ARGP_PARSE
10322#endif
10323
10324#if SANITIZER_INTERCEPT_CPUSET_GETAFFINITY
10325INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T cpusetsize, __sanitizer_cpuset_t *mask) {
10326 void *ctx;
10327 COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getaffinity, level, which, id, cpusetsize, mask);
10328 int res = REAL(cpuset_getaffinity)(level, which, id, cpusetsize, mask);
10329 if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
10330 return res;
10331}
10332#define INIT_CPUSET_GETAFFINITY COMMON_INTERCEPT_FUNCTION(cpuset_getaffinity);
10333#else
10334#define INIT_CPUSET_GETAFFINITY
10335#endif
10336
10337#if SANITIZER_INTERCEPT_PREADV2
10338INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
10339 OFF_T offset, int flags) {
10340 void *ctx;
10341 COMMON_INTERCEPTOR_ENTER(ctx, preadv2, fd, iov, iovcnt, offset, flags);
10342 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10343 SSIZE_T res = REAL(preadv2)(fd, iov, iovcnt, offset, flags);
10344 if (res > 0) write_iovec(ctx, iovec: iov, iovlen: iovcnt, maxlen: res);
10345 if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10346 return res;
10347}
10348#define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
10349#else
10350#define INIT_PREADV2
10351#endif
10352
10353#if SANITIZER_INTERCEPT_PWRITEV2
10354INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
10355 OFF_T offset, int flags) {
10356 void *ctx;
10357 COMMON_INTERCEPTOR_ENTER(ctx, pwritev2, fd, iov, iovcnt, offset, flags);
10358 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10359 if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
10360 SSIZE_T res = REAL(pwritev2)(fd, iov, iovcnt, offset, flags);
10361 if (res > 0) read_iovec(ctx, iovec: iov, iovlen: iovcnt, maxlen: res);
10362 return res;
10363}
10364#define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
10365#else
10366#define INIT_PWRITEV2
10367#endif
10368
10369#if SANITIZER_INTERCEPT_FREADLINK
10370INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) {
10371 void *ctx;
10372 COMMON_INTERCEPTOR_ENTER(ctx, freadlink, fd, buf, bufsiz);
10373 COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10374 SSIZE_T res = REAL(freadlink)(fd, buf, bufsiz);
10375 if (res > 0)
10376 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
10377 if (res >= 0 && fd > 0)
10378 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10379 return res;
10380}
10381# define INIT_FREADLINK COMMON_INTERCEPT_FUNCTION(freadlink)
10382#else
10383# define INIT_FREADLINK
10384#endif
10385
10386#if SANITIZER_INTERCEPT_GETSERVENT_R || SANITIZER_INTERCEPT_GETSERVBYNAME_R || \
10387 SANITIZER_INTERCEPT_GETSERVBYPORT_R
10388
10389UNUSED static void HandleGetServentReentrantResult(
10390 void *ctx, int res, struct __sanitizer_servent *result_buf, char *buf,
10391 SIZE_T buflen, struct __sanitizer_servent **result) {
10392 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)result, sizeof(void *));
10393 if (res)
10394 return;
10395 if (*result) {
10396 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)*result,
10397 sizeof(__sanitizer_servent));
10398 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
10399 }
10400}
10401
10402#endif
10403
10404#if SANITIZER_INTERCEPT_GETSERVENT_R
10405INTERCEPTOR(int, getservent_r, struct __sanitizer_servent *result_buf,
10406 char *buf, SIZE_T buflen, struct __sanitizer_servent **result) {
10407 void *ctx;
10408 COMMON_INTERCEPTOR_ENTER(ctx, getservent_r, result_buf, buf, buflen, result);
10409 int res = REAL(getservent_r)(result_buf, buf, buflen, result);
10410 HandleGetServentReentrantResult(ctx, res, result_buf, buf, buflen, result);
10411 return res;
10412}
10413# define INIT_GETSERVENT_R COMMON_INTERCEPT_FUNCTION(getservent_r)
10414#else
10415# define INIT_GETSERVENT_R
10416#endif
10417
10418#if SANITIZER_INTERCEPT_GETSERVBYNAME_R
10419INTERCEPTOR(int, getservbyname_r, const char *name, const char *proto,
10420 struct __sanitizer_servent *result_buf, char *buf, SIZE_T buflen,
10421 struct __sanitizer_servent **result) {
10422 void *ctx;
10423 COMMON_INTERCEPTOR_ENTER(ctx, getservbyname_r, name, proto, result_buf, buf,
10424 buflen, result);
10425 COMMON_INTERCEPTOR_READ_STRING(ctx, name, internal_strlen(name));
10426 int res = REAL(getservbyname_r)(name, proto, result_buf, buf, buflen, result);
10427 HandleGetServentReentrantResult(ctx, res, result_buf, buf, buflen, result);
10428 return res;
10429}
10430# define INIT_GETSERVBYNAME_R COMMON_INTERCEPT_FUNCTION(getservbyname_r)
10431#else
10432# define INIT_GETSERVBYNAME_R
10433#endif
10434
10435#if SANITIZER_INTERCEPT_GETSERVBYPORT_R
10436INTERCEPTOR(int, getservbyport_r, int port, const char *proto,
10437 struct __sanitizer_servent *result_buf, char *buf, SIZE_T buflen,
10438 struct __sanitizer_servent **result) {
10439 void *ctx;
10440 COMMON_INTERCEPTOR_ENTER(ctx, getservbyport_r, port, proto, result_buf, buf,
10441 buflen, result);
10442 int res = REAL(getservbyport_r)(port, proto, result_buf, buf, buflen, result);
10443 HandleGetServentReentrantResult(ctx, res, result_buf, buf, buflen, result);
10444 return res;
10445}
10446# define INIT_GETSERVBYPORT_R COMMON_INTERCEPT_FUNCTION(getservbyport_r)
10447#else
10448# define INIT_GETSERVBYPORT_R
10449#endif
10450
10451#include "sanitizer_common_interceptors_netbsd_compat.inc"
10452
10453namespace __sanitizer {
10454void InitializeMemintrinsicInterceptors();
10455} // namespace __sanitizer
10456
10457static void InitializeCommonInterceptors() {
10458#if SI_POSIX
10459 static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
10460 interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
10461#endif
10462
10463 __sanitizer::InitializeMemintrinsicInterceptors();
10464
10465 INIT_MMAP;
10466 INIT_MMAP64;
10467 INIT_TEXTDOMAIN;
10468 INIT_STRLEN;
10469 INIT_STRNLEN;
10470 INIT_STRNDUP;
10471 INIT___STRNDUP;
10472 INIT_STRCMP;
10473 INIT_STRNCMP;
10474 INIT_STRCASECMP;
10475 INIT_STRNCASECMP;
10476 INIT_STRSTR;
10477 INIT_STRCASESTR;
10478 INIT_STRCHR;
10479 INIT_STRCHRNUL;
10480 INIT_STRRCHR;
10481 INIT_STRSPN;
10482 INIT_STRTOK;
10483 INIT_STRPBRK;
10484 INIT_STRXFRM;
10485 INIT___STRXFRM_L;
10486 INIT_MEMCHR;
10487 INIT_MEMCMP;
10488 INIT_BCMP;
10489 INIT_MEMRCHR;
10490 INIT_MEMMEM;
10491 INIT_READ;
10492 INIT___READ_CHK;
10493 INIT_FREAD;
10494 INIT_PREAD;
10495 INIT___PREAD_CHK;
10496 INIT_PREAD64;
10497 INIT___PREAD64_CHK;
10498 INIT_READV;
10499 INIT_PREADV;
10500 INIT_PREADV64;
10501 INIT_WRITE;
10502 INIT_FWRITE;
10503 INIT_PWRITE;
10504 INIT_PWRITE64;
10505 INIT_WRITEV;
10506 INIT_PWRITEV;
10507 INIT_PWRITEV64;
10508 INIT_FGETS;
10509 INIT_FPUTS;
10510 INIT_PUTS;
10511 INIT_PRCTL;
10512 INIT_LOCALTIME_AND_FRIENDS;
10513 INIT_STRPTIME;
10514 INIT_SCANF;
10515 INIT_ISOC99_SCANF;
10516 INIT_PRINTF;
10517 INIT_PRINTF_L;
10518 INIT_ISOC99_PRINTF;
10519 INIT_SETPROCTITLE;
10520 INIT_FREXP;
10521 INIT_FREXPF;
10522 INIT_FREXPL;
10523 INIT_GETPWNAM_AND_FRIENDS;
10524 INIT_GETPWNAM_R_AND_FRIENDS;
10525 INIT_GETPWENT;
10526 INIT_FGETPWENT;
10527 INIT_GETPWENT_R;
10528 INIT_FGETPWENT_R;
10529 INIT_FGETGRENT_R;
10530 INIT_SETPWENT;
10531 INIT_CLOCK_GETTIME;
10532 INIT_CLOCK_GETCPUCLOCKID;
10533 INIT_TIMER_CREATE;
10534 INIT_GETITIMER;
10535 INIT_TIME;
10536 INIT_TIMESPEC_GET;
10537 INIT_GLOB;
10538 INIT_GLOB64;
10539 INIT___B64_TO;
10540 INIT_DN_COMP_EXPAND;
10541 INIT_POSIX_SPAWN;
10542 INIT_WAIT;
10543 INIT_WAIT4;
10544 INIT_INET;
10545 INIT_PTHREAD_GETSCHEDPARAM;
10546 INIT_GETADDRINFO;
10547 INIT_GETNAMEINFO;
10548 INIT_GETSOCKNAME;
10549 INIT_GETHOSTBYNAME;
10550 INIT_GETHOSTBYNAME2;
10551 INIT_GETHOSTBYNAME_R;
10552 INIT_GETHOSTBYNAME2_R;
10553 INIT_GETHOSTBYADDR_R;
10554 INIT_GETHOSTENT_R;
10555 INIT_GETSOCKOPT;
10556 INIT_ACCEPT;
10557 INIT_ACCEPT4;
10558 INIT_PACCEPT;
10559 INIT_MODF;
10560 INIT_RECVMSG;
10561 INIT_SENDMSG;
10562 INIT_RECVMMSG;
10563 INIT_SENDMMSG;
10564 INIT_SYSMSG;
10565 INIT_GETPEERNAME;
10566 INIT_IOCTL;
10567 INIT_INET_ATON;
10568 INIT_SYSINFO;
10569 INIT_READDIR;
10570 INIT_READDIR64;
10571 INIT_PTRACE;
10572 INIT_SETLOCALE;
10573 INIT_GETCWD;
10574 INIT_GET_CURRENT_DIR_NAME;
10575 INIT_STRTOIMAX;
10576 INIT_STRTOIMAX_C23;
10577 INIT_MBSTOWCS;
10578 INIT_MBSNRTOWCS;
10579 INIT_WCSTOMBS;
10580 INIT_WCSNRTOMBS;
10581 INIT_WCRTOMB;
10582 INIT_WCTOMB;
10583 INIT_TCGETATTR;
10584 INIT_REALPATH;
10585 INIT_CANONICALIZE_FILE_NAME;
10586 INIT_CONFSTR;
10587 INIT_SCHED_GETAFFINITY;
10588 INIT_SCHED_GETPARAM;
10589 INIT_STRERROR;
10590 INIT_STRERROR_R;
10591 INIT_XPG_STRERROR_R;
10592 INIT_SCANDIR;
10593 INIT_SCANDIR64;
10594 INIT_GETGROUPS;
10595 INIT_POLL;
10596 INIT_PPOLL;
10597 INIT_WORDEXP;
10598 INIT_SIGWAIT;
10599 INIT_SIGWAITINFO;
10600 INIT_SIGTIMEDWAIT;
10601 INIT_SIGSETOPS;
10602 INIT_SIGSET_LOGICOPS;
10603 INIT_SIGPENDING;
10604 INIT_SIGPROCMASK;
10605 INIT_PTHREAD_SIGMASK;
10606 INIT_BACKTRACE;
10607 INIT__EXIT;
10608 INIT___LIBC_THR_SETCANCELSTATE;
10609 INIT_GETMNTENT;
10610 INIT_GETMNTENT_R;
10611 INIT_STATFS;
10612 INIT_STATFS64;
10613 INIT_STATVFS;
10614 INIT_STATVFS64;
10615 INIT_INITGROUPS;
10616 INIT_ETHER_NTOA_ATON;
10617 INIT_ETHER_HOST;
10618 INIT_ETHER_R;
10619 INIT_SHMCTL;
10620 INIT_RANDOM_R;
10621 INIT_PTHREAD_ATTR_GET;
10622 INIT_PTHREAD_ATTR_GET_SCHED;
10623 INIT_PTHREAD_ATTR_GETINHERITSCHED;
10624 INIT_PTHREAD_ATTR_GETAFFINITY_NP;
10625 INIT_PTHREAD_GETAFFINITY_NP;
10626 INIT_PTHREAD_MUTEXATTR_GETPSHARED;
10627 INIT_PTHREAD_MUTEXATTR_GETTYPE;
10628 INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
10629 INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
10630 INIT_PTHREAD_MUTEXATTR_GETROBUST;
10631 INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
10632 INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
10633 INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
10634 INIT_PTHREAD_CONDATTR_GETPSHARED;
10635 INIT_PTHREAD_CONDATTR_GETCLOCK;
10636 INIT_PTHREAD_BARRIERATTR_GETPSHARED;
10637 INIT_TMPNAM;
10638 INIT_TMPNAM_R;
10639 INIT_PTSNAME;
10640 INIT_PTSNAME_R;
10641 INIT_TTYNAME;
10642 INIT_TTYNAME_R;
10643 INIT_TEMPNAM;
10644 INIT_PTHREAD_SETNAME_NP;
10645 INIT_PTHREAD_GETNAME_NP;
10646 INIT_SINCOS;
10647 INIT_REMQUO;
10648 INIT_REMQUOL;
10649 INIT_LGAMMA;
10650 INIT_LGAMMAL;
10651 INIT_LGAMMA_R;
10652 INIT_LGAMMAL_R;
10653 INIT_DRAND48_R;
10654 INIT_RAND_R;
10655 INIT_GETLINE;
10656 INIT_ICONV;
10657 INIT_TIMES;
10658 INIT_TLS_GET_ADDR;
10659 INIT_LISTXATTR;
10660 INIT_GETXATTR;
10661 INIT_GETRESID;
10662 INIT_GETIFADDRS;
10663 INIT_IF_INDEXTONAME;
10664 INIT_CAPGET;
10665 INIT_FTIME;
10666 INIT_XDR;
10667 INIT_XDRREC_LINUX;
10668 INIT_TSEARCH;
10669 INIT_LIBIO_INTERNALS;
10670 INIT_FOPEN;
10671 INIT_FOPEN64;
10672 INIT_FLOPEN;
10673 INIT_OPEN_MEMSTREAM;
10674 INIT_OBSTACK;
10675 INIT_FFLUSH;
10676 INIT_FCLOSE;
10677 INIT_DLOPEN_DLCLOSE;
10678 INIT_GETPASS;
10679 INIT_TIMERFD;
10680 INIT_MLOCKX;
10681 INIT_FOPENCOOKIE;
10682 INIT_SEM;
10683 INIT_PTHREAD_SETCANCEL;
10684 INIT_MINCORE;
10685 INIT_PROCESS_VM_READV;
10686 INIT_CTERMID;
10687 INIT_CTERMID_R;
10688 INIT_RECV_RECVFROM;
10689 INIT_SEND_SENDTO;
10690 INIT_STAT;
10691 INIT_STAT64;
10692 INIT_EVENTFD_READ_WRITE;
10693 INIT_LSTAT;
10694 INIT_LSTAT64;
10695 INIT___XSTAT;
10696 INIT___XSTAT64;
10697 INIT___LXSTAT;
10698 INIT___LXSTAT64;
10699 // FIXME: add other *stat interceptors.
10700 INIT_UTMP;
10701 INIT_UTMPX;
10702 INIT_GETLOADAVG;
10703 INIT_WCSLEN;
10704 INIT_WCSCAT;
10705 INIT_WCSDUP;
10706 INIT_WCSXFRM;
10707 INIT___WCSXFRM_L;
10708 INIT_ACCT;
10709 INIT_USER_FROM_UID;
10710 INIT_UID_FROM_USER;
10711 INIT_GROUP_FROM_GID;
10712 INIT_GID_FROM_GROUP;
10713 INIT_ACCESS;
10714 INIT_FACCESSAT;
10715 INIT_GETGROUPLIST;
10716 INIT_GETGROUPMEMBERSHIP;
10717 INIT_READLINK;
10718 INIT_READLINKAT;
10719 INIT_NAME_TO_HANDLE_AT;
10720 INIT_OPEN_BY_HANDLE_AT;
10721 INIT_STRLCPY;
10722 INIT_DEVNAME;
10723 INIT_DEVNAME_R;
10724 INIT_FGETLN;
10725 INIT_STRMODE;
10726 INIT_TTYENT;
10727 INIT_PROTOENT;
10728 INIT_PROTOENT_R;
10729 INIT_NETENT;
10730 INIT_GETMNTINFO;
10731 INIT_MI_VECTOR_HASH;
10732 INIT_SETVBUF;
10733 INIT_GETVFSSTAT;
10734 INIT_REGEX;
10735 INIT_REGEXSUB;
10736 INIT_FTS;
10737 INIT_SYSCTL;
10738 INIT_ASYSCTL;
10739 INIT_SYSCTLGETMIBINFO;
10740 INIT_NL_LANGINFO;
10741 INIT_MODCTL;
10742 INIT_STRTONUM;
10743 INIT_FPARSELN;
10744 INIT_STATVFS1;
10745 INIT_STRTOI;
10746 INIT_CAPSICUM;
10747 INIT_SHA1;
10748 INIT_MD4;
10749 INIT_RMD160;
10750 INIT_FSEEK;
10751 INIT_MD2;
10752 INIT_VIS;
10753 INIT_CDB;
10754 INIT_GETFSENT;
10755 INIT_ARC4RANDOM;
10756 INIT_POPEN;
10757 INIT_POPENVE;
10758 INIT_PCLOSE;
10759 INIT_FUNOPEN;
10760 INIT_FUNOPEN2;
10761 INIT_FDEVNAME;
10762 INIT_GETUSERSHELL;
10763 INIT_SL_INIT;
10764 INIT_GETRANDOM;
10765 INIT_GETENTROPY;
10766 INIT_QSORT;
10767 INIT_QSORT_R;
10768 INIT_BSEARCH;
10769 INIT_SIGALTSTACK;
10770 INIT_PROCCTL
10771 INIT_UNAME;
10772 INIT___XUNAME;
10773 INIT_ARGP_PARSE;
10774 INIT_CPUSET_GETAFFINITY;
10775 INIT_PREADV2;
10776 INIT_PWRITEV2;
10777 INIT_FREADLINK;
10778
10779 INIT___PRINTF_CHK;
10780 INIT_GETSERVENT_R;
10781 INIT_GETSERVBYNAME_R;
10782 INIT_GETSERVBYPORT_R;
10783}
10784