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