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

source code of compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc