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