1//===--- rtsan_interceptors.cpp - Realtime Sanitizer ------------*- 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//===----------------------------------------------------------------------===//
10
11#include "sanitizer_common/sanitizer_platform.h"
12#if SANITIZER_POSIX
13
14#include "rtsan/rtsan_interceptors.h"
15
16#include "interception/interception.h"
17#include "sanitizer_common/sanitizer_allocator_dlsym.h"
18#include "sanitizer_common/sanitizer_glibc_version.h"
19#include "sanitizer_common/sanitizer_platform_interceptors.h"
20
21#include "interception/interception.h"
22#include "rtsan/rtsan.h"
23
24#if SANITIZER_APPLE
25#include <libkern/OSAtomic.h>
26#include <os/lock.h>
27#endif // SANITIZER_APPLE
28
29#if SANITIZER_INTERCEPT_MEMALIGN || SANITIZER_INTERCEPT_PVALLOC
30#include <malloc.h>
31#endif
32
33#include <fcntl.h>
34#include <poll.h>
35#include <pthread.h>
36#include <stdarg.h>
37#include <stdio.h>
38#if SANITIZER_LINUX
39#include <linux/mman.h>
40#include <sys/inotify.h>
41#endif
42#include <sys/select.h>
43#include <sys/socket.h>
44#include <sys/stat.h>
45#include <time.h>
46#include <unistd.h>
47
48using namespace __sanitizer;
49
50namespace {
51struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
52 static bool UseImpl() { return !__rtsan_is_initialized(); }
53};
54} // namespace
55
56// Filesystem
57
58INTERCEPTOR(int, open, const char *path, int oflag, ...) {
59 // We do not early exit if O_NONBLOCK is set.
60 // O_NONBLOCK **does not prevent the syscall** it simply sets the FD to be in
61 // nonblocking mode, which is a different concept than our
62 // [[clang::nonblocking]], and is not rt-safe. This behavior was confirmed
63 // using Instruments on Darwin with a simple test program
64 __rtsan_notify_intercepted_call(intercepted_function_name: "open");
65
66 if (OpenReadsVaArgs(oflag)) {
67 va_list args;
68 va_start(args, oflag);
69 const mode_t mode = va_arg(args, int);
70 va_end(args);
71 return REAL(open)(path, oflag, mode);
72 }
73
74 return REAL(open)(path, oflag);
75}
76
77#if SANITIZER_INTERCEPT_OPEN64
78INTERCEPTOR(int, open64, const char *path, int oflag, ...) {
79 // See comment above about O_NONBLOCK
80 __rtsan_notify_intercepted_call(intercepted_function_name: "open64");
81
82 if (OpenReadsVaArgs(oflag)) {
83 va_list args;
84 va_start(args, oflag);
85 const mode_t mode = va_arg(args, int);
86 va_end(args);
87 return REAL(open64)(path, oflag, mode);
88 }
89
90 return REAL(open64)(path, oflag);
91}
92#define RTSAN_MAYBE_INTERCEPT_OPEN64 INTERCEPT_FUNCTION(open64)
93#else
94#define RTSAN_MAYBE_INTERCEPT_OPEN64
95#endif // SANITIZER_INTERCEPT_OPEN64
96
97INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
98 // See comment above about O_NONBLOCK
99 __rtsan_notify_intercepted_call(intercepted_function_name: "openat");
100
101 if (OpenReadsVaArgs(oflag)) {
102 va_list args;
103 va_start(args, oflag);
104 const mode_t mode = va_arg(args, int);
105 va_end(args);
106 return REAL(openat)(fd, path, oflag, mode);
107 }
108
109 return REAL(openat)(fd, path, oflag);
110}
111
112#if SANITIZER_INTERCEPT_OPENAT64
113INTERCEPTOR(int, openat64, int fd, const char *path, int oflag, ...) {
114 // See comment above about O_NONBLOCK
115 __rtsan_notify_intercepted_call(intercepted_function_name: "openat64");
116
117 if (OpenReadsVaArgs(oflag)) {
118 va_list args;
119 va_start(args, oflag);
120 const mode_t mode = va_arg(args, int);
121 va_end(args);
122 return REAL(openat64)(fd, path, oflag, mode);
123 }
124
125 return REAL(openat64)(fd, path, oflag);
126}
127#define RTSAN_MAYBE_INTERCEPT_OPENAT64 INTERCEPT_FUNCTION(openat64)
128#else
129#define RTSAN_MAYBE_INTERCEPT_OPENAT64
130#endif // SANITIZER_INTERCEPT_OPENAT64
131
132INTERCEPTOR(int, creat, const char *path, mode_t mode) {
133 // See comment above about O_NONBLOCK
134 __rtsan_notify_intercepted_call(intercepted_function_name: "creat");
135 const int result = REAL(creat)(path, mode);
136 return result;
137}
138
139#if SANITIZER_INTERCEPT_CREAT64
140INTERCEPTOR(int, creat64, const char *path, mode_t mode) {
141 // See comment above about O_NONBLOCK
142 __rtsan_notify_intercepted_call(intercepted_function_name: "creat64");
143 const int result = REAL(creat64)(path, mode);
144 return result;
145}
146#define RTSAN_MAYBE_INTERCEPT_CREAT64 INTERCEPT_FUNCTION(creat64)
147#else
148#define RTSAN_MAYBE_INTERCEPT_CREAT64
149#endif // SANITIZER_INTERCEPT_CREAT64
150
151INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
152 __rtsan_notify_intercepted_call(intercepted_function_name: "fcntl");
153
154 // Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the
155 // final argument in a variable that will hold the largest of the possible
156 // argument types. It is then assumed that the implementation of fcntl will
157 // cast it properly depending on cmd.
158 //
159 // The two types we expect for possible args are `struct flock*` and `int`
160 // we will cast to `intptr_t` which should hold both comfortably.
161 // Why `intptr_t`? It should fit both types, and it follows the freeBSD
162 // approach linked below.
163 using arg_type = intptr_t;
164 static_assert(sizeof(arg_type) >= sizeof(struct flock *));
165 static_assert(sizeof(arg_type) >= sizeof(int));
166
167 // Some cmds will not actually have an argument passed in this va_list.
168 // Calling va_arg when no arg exists is UB, however all currently
169 // supported architectures will give us a result in all three cases
170 // (no arg/int arg/struct flock* arg)
171 // va_arg() will generally read the next argument register or the
172 // stack. If we ever support an arch like CHERI with bounds checking, we
173 // may have to re-evaluate this approach.
174 //
175 // More discussion, and other examples following this approach
176 // https://discourse.llvm.org/t/how-to-write-an-interceptor-for-fcntl/81203
177 // https://reviews.freebsd.org/D46403
178 // https://github.com/bminor/glibc/blob/c444cc1d8335243c5c4e636d6a26c472df85522c/sysdeps/unix/sysv/linux/fcntl64.c#L37-L46
179
180 va_list args;
181 va_start(args, cmd);
182 const arg_type arg = va_arg(args, arg_type);
183 va_end(args);
184
185 return REAL(fcntl)(filedes, cmd, arg);
186}
187
188#if SANITIZER_MUSL
189INTERCEPTOR(int, ioctl, int filedes, int request, ...) {
190#else
191INTERCEPTOR(int, ioctl, int filedes, unsigned long request, ...) {
192#endif
193 __rtsan_notify_intercepted_call(intercepted_function_name: "ioctl");
194
195 // See fcntl for discussion on why we use intptr_t
196 // And why we read from va_args on all request types
197 using arg_type = intptr_t;
198 static_assert(sizeof(arg_type) >= sizeof(struct ifreq *));
199 static_assert(sizeof(arg_type) >= sizeof(int));
200
201 va_list args;
202 va_start(args, request);
203 arg_type arg = va_arg(args, arg_type);
204 va_end(args);
205
206 return REAL(ioctl)(filedes, request, arg);
207}
208
209#if SANITIZER_INTERCEPT_FCNTL64
210INTERCEPTOR(int, fcntl64, int filedes, int cmd, ...) {
211 __rtsan_notify_intercepted_call(intercepted_function_name: "fcntl64");
212
213 va_list args;
214 va_start(args, cmd);
215
216 // Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the
217 // final argument in a variable that will hold the largest of the possible
218 // argument types (pointers and ints are typical in fcntl) It is then assumed
219 // that the implementation of fcntl will cast it properly depending on cmd.
220 //
221 // This is also similar to what is done in
222 // sanitizer_common/sanitizer_common_syscalls.inc
223 const unsigned long arg = va_arg(args, unsigned long);
224 int result = REAL(fcntl64)(filedes, cmd, arg);
225
226 va_end(args);
227
228 return result;
229}
230#define RTSAN_MAYBE_INTERCEPT_FCNTL64 INTERCEPT_FUNCTION(fcntl64)
231#else
232#define RTSAN_MAYBE_INTERCEPT_FCNTL64
233#endif // SANITIZER_INTERCEPT_FCNTL64
234
235INTERCEPTOR(int, close, int filedes) {
236 __rtsan_notify_intercepted_call(intercepted_function_name: "close");
237 return REAL(close)(filedes);
238}
239
240INTERCEPTOR(int, chdir, const char *path) {
241 __rtsan_notify_intercepted_call(intercepted_function_name: "chdir");
242 return REAL(chdir)(path);
243}
244
245INTERCEPTOR(int, fchdir, int fd) {
246 __rtsan_notify_intercepted_call(intercepted_function_name: "fchdir");
247 return REAL(fchdir)(fd);
248}
249
250#if SANITIZER_INTERCEPT_READLINK
251INTERCEPTOR(ssize_t, readlink, const char *pathname, char *buf, size_t size) {
252 __rtsan_notify_intercepted_call(intercepted_function_name: "readlink");
253 return REAL(readlink)(pathname, buf, size);
254}
255#define RTSAN_MAYBE_INTERCEPT_READLINK INTERCEPT_FUNCTION(readlink)
256#else
257#define RTSAN_MAYBE_INTERCEPT_READLINK
258#endif
259
260#if SANITIZER_INTERCEPT_READLINKAT
261INTERCEPTOR(ssize_t, readlinkat, int dirfd, const char *pathname, char *buf,
262 size_t size) {
263 __rtsan_notify_intercepted_call(intercepted_function_name: "readlinkat");
264 return REAL(readlinkat)(dirfd, pathname, buf, size);
265}
266#define RTSAN_MAYBE_INTERCEPT_READLINKAT INTERCEPT_FUNCTION(readlinkat)
267#else
268#define RTSAN_MAYBE_INTERCEPT_READLINKAT
269#endif
270
271INTERCEPTOR(int, unlink, const char *pathname) {
272 __rtsan_notify_intercepted_call(intercepted_function_name: "unlink");
273 return REAL(unlink)(pathname);
274}
275
276INTERCEPTOR(int, unlinkat, int fd, const char *pathname, int flag) {
277 __rtsan_notify_intercepted_call(intercepted_function_name: "unlinkat");
278 return REAL(unlinkat)(fd, pathname, flag);
279}
280
281INTERCEPTOR(int, truncate, const char *pathname, off_t length) {
282 __rtsan_notify_intercepted_call(intercepted_function_name: "truncate");
283 return REAL(truncate)(pathname, length);
284}
285
286INTERCEPTOR(int, ftruncate, int fd, off_t length) {
287 __rtsan_notify_intercepted_call(intercepted_function_name: "ftruncate");
288 return REAL(ftruncate)(fd, length);
289}
290
291#if SANITIZER_LINUX && !SANITIZER_MUSL
292INTERCEPTOR(int, truncate64, const char *pathname, off64_t length) {
293 __rtsan_notify_intercepted_call(intercepted_function_name: "truncate64");
294 return REAL(truncate64)(pathname, length);
295}
296
297INTERCEPTOR(int, ftruncate64, int fd, off64_t length) {
298 __rtsan_notify_intercepted_call(intercepted_function_name: "ftruncate64");
299 return REAL(ftruncate64)(fd, length);
300}
301#define RTSAN_MAYBE_INTERCEPT_TRUNCATE64 INTERCEPT_FUNCTION(truncate64)
302#define RTSAN_MAYBE_INTERCEPT_FTRUNCATE64 INTERCEPT_FUNCTION(ftruncate64)
303#else
304#define RTSAN_MAYBE_INTERCEPT_TRUNCATE64
305#define RTSAN_MAYBE_INTERCEPT_FTRUNCATE64
306#endif
307
308INTERCEPTOR(int, symlink, const char *target, const char *linkpath) {
309 __rtsan_notify_intercepted_call(intercepted_function_name: "symlink");
310 return REAL(symlink)(target, linkpath);
311}
312
313INTERCEPTOR(int, symlinkat, const char *target, int newdirfd,
314 const char *linkpath) {
315 __rtsan_notify_intercepted_call(intercepted_function_name: "symlinkat");
316 return REAL(symlinkat)(target, newdirfd, linkpath);
317}
318
319// Streams
320
321INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
322 __rtsan_notify_intercepted_call(intercepted_function_name: "fopen");
323 return REAL(fopen)(path, mode);
324}
325
326INTERCEPTOR(FILE *, freopen, const char *path, const char *mode, FILE *stream) {
327 __rtsan_notify_intercepted_call(intercepted_function_name: "freopen");
328 return REAL(freopen)(path, mode, stream);
329}
330
331#if SANITIZER_INTERCEPT_FOPEN64
332INTERCEPTOR(FILE *, fopen64, const char *path, const char *mode) {
333 __rtsan_notify_intercepted_call(intercepted_function_name: "fopen64");
334 return REAL(fopen64)(path, mode);
335}
336
337INTERCEPTOR(FILE *, freopen64, const char *path, const char *mode,
338 FILE *stream) {
339 __rtsan_notify_intercepted_call(intercepted_function_name: "freopen64");
340 return REAL(freopen64)(path, mode, stream);
341}
342#define RTSAN_MAYBE_INTERCEPT_FOPEN64 INTERCEPT_FUNCTION(fopen64);
343#define RTSAN_MAYBE_INTERCEPT_FREOPEN64 INTERCEPT_FUNCTION(freopen64);
344#else
345#define RTSAN_MAYBE_INTERCEPT_FOPEN64
346#define RTSAN_MAYBE_INTERCEPT_FREOPEN64
347#endif // SANITIZER_INTERCEPT_FOPEN64
348
349INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems,
350 FILE *stream) {
351 __rtsan_notify_intercepted_call(intercepted_function_name: "fread");
352 return REAL(fread)(ptr, size, nitems, stream);
353}
354
355INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems,
356 FILE *stream) {
357 __rtsan_notify_intercepted_call(intercepted_function_name: "fwrite");
358 return REAL(fwrite)(ptr, size, nitems, stream);
359}
360
361INTERCEPTOR(int, fclose, FILE *stream) {
362 __rtsan_notify_intercepted_call(intercepted_function_name: "fclose");
363 return REAL(fclose)(stream);
364}
365
366INTERCEPTOR(int, fputs, const char *s, FILE *stream) {
367 __rtsan_notify_intercepted_call(intercepted_function_name: "fputs");
368 return REAL(fputs)(s, stream);
369}
370
371INTERCEPTOR(int, fflush, FILE *stream) {
372 __rtsan_notify_intercepted_call(intercepted_function_name: "fflush");
373 return REAL(fflush)(stream);
374}
375
376#if SANITIZER_APPLE
377INTERCEPTOR(int, fpurge, FILE *stream) {
378 __rtsan_notify_intercepted_call("fpurge");
379 return REAL(fpurge)(stream);
380}
381#define RTSAN_MAYBE_INTERCEPT_FPURGE INTERCEPT_FUNCTION(fpurge)
382#else
383#define RTSAN_MAYBE_INTERCEPT_FPURGE
384#endif
385
386INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
387 __rtsan_notify_intercepted_call(intercepted_function_name: "fdopen");
388 return REAL(fdopen)(fd, mode);
389}
390
391#if SANITIZER_INTERCEPT_FOPENCOOKIE
392INTERCEPTOR(FILE *, fopencookie, void *cookie, const char *mode,
393 cookie_io_functions_t funcs) {
394 __rtsan_notify_intercepted_call(intercepted_function_name: "fopencookie");
395 return REAL(fopencookie)(cookie, mode, funcs);
396}
397#define RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE INTERCEPT_FUNCTION(fopencookie)
398#else
399#define RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE
400#endif
401
402#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
403INTERCEPTOR(FILE *, open_memstream, char **buf, size_t *size) {
404 __rtsan_notify_intercepted_call(intercepted_function_name: "open_memstream");
405 return REAL(open_memstream)(buf, size);
406}
407
408INTERCEPTOR(FILE *, fmemopen, void *buf, size_t size, const char *mode) {
409 __rtsan_notify_intercepted_call(intercepted_function_name: "fmemopen");
410 return REAL(fmemopen)(buf, size, mode);
411}
412#define RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM INTERCEPT_FUNCTION(open_memstream)
413#define RTSAN_MAYBE_INTERCEPT_FMEMOPEN INTERCEPT_FUNCTION(fmemopen)
414#else
415#define RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM
416#define RTSAN_MAYBE_INTERCEPT_FMEMOPEN
417#endif
418
419#if SANITIZER_INTERCEPT_SETVBUF
420INTERCEPTOR(void, setbuf, FILE *stream, char *buf) {
421 __rtsan_notify_intercepted_call(intercepted_function_name: "setbuf");
422 return REAL(setbuf)(stream, buf);
423}
424
425INTERCEPTOR(int, setvbuf, FILE *stream, char *buf, int mode, size_t size) {
426 __rtsan_notify_intercepted_call(intercepted_function_name: "setvbuf");
427 return REAL(setvbuf)(stream, buf, mode, size);
428}
429
430#if SANITIZER_LINUX
431INTERCEPTOR(void, setlinebuf, FILE *stream) {
432#else
433INTERCEPTOR(int, setlinebuf, FILE *stream) {
434#endif
435 __rtsan_notify_intercepted_call(intercepted_function_name: "setlinebuf");
436 return REAL(setlinebuf)(stream);
437}
438
439#if SANITIZER_LINUX
440INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, size_t size) {
441#else
442INTERCEPTOR(void, setbuffer, FILE *stream, char *buf, int size) {
443#endif
444 __rtsan_notify_intercepted_call(intercepted_function_name: "setbuffer");
445 return REAL(setbuffer)(stream, buf, size);
446}
447#define RTSAN_MAYBE_INTERCEPT_SETBUF INTERCEPT_FUNCTION(setbuf)
448#define RTSAN_MAYBE_INTERCEPT_SETVBUF INTERCEPT_FUNCTION(setvbuf)
449#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF INTERCEPT_FUNCTION(setlinebuf)
450#define RTSAN_MAYBE_INTERCEPT_SETBUFFER INTERCEPT_FUNCTION(setbuffer)
451#else
452#define RTSAN_MAYBE_INTERCEPT_SETBUF
453#define RTSAN_MAYBE_INTERCEPT_SETVBUF
454#define RTSAN_MAYBE_INTERCEPT_SETLINEBUF
455#define RTSAN_MAYBE_INTERCEPT_SETBUFFER
456#endif
457
458#if SANITIZER_INTERCEPT_FSEEK
459INTERCEPTOR(int, fgetpos, FILE *stream, fpos_t *pos) {
460 __rtsan_notify_intercepted_call(intercepted_function_name: "fgetpos");
461 return REAL(fgetpos)(stream, pos);
462}
463
464INTERCEPTOR(int, fseek, FILE *stream, long offset, int whence) {
465 __rtsan_notify_intercepted_call(intercepted_function_name: "fseek");
466 return REAL(fseek)(stream, offset, whence);
467}
468
469INTERCEPTOR(int, fseeko, FILE *stream, off_t offset, int whence) {
470 __rtsan_notify_intercepted_call(intercepted_function_name: "fseeko");
471 return REAL(fseeko)(stream, offset, whence);
472}
473
474INTERCEPTOR(int, fsetpos, FILE *stream, const fpos_t *pos) {
475 __rtsan_notify_intercepted_call(intercepted_function_name: "fsetpos");
476 return REAL(fsetpos)(stream, pos);
477}
478
479INTERCEPTOR(long, ftell, FILE *stream) {
480 __rtsan_notify_intercepted_call(intercepted_function_name: "ftell");
481 return REAL(ftell)(stream);
482}
483
484INTERCEPTOR(off_t, ftello, FILE *stream) {
485 __rtsan_notify_intercepted_call(intercepted_function_name: "ftello");
486 return REAL(ftello)(stream);
487}
488
489#if SANITIZER_LINUX && !SANITIZER_MUSL
490INTERCEPTOR(int, fgetpos64, FILE *stream, fpos64_t *pos) {
491 __rtsan_notify_intercepted_call(intercepted_function_name: "fgetpos64");
492 return REAL(fgetpos64)(stream, pos);
493}
494
495INTERCEPTOR(int, fseeko64, FILE *stream, off64_t offset, int whence) {
496 __rtsan_notify_intercepted_call(intercepted_function_name: "fseeko64");
497 return REAL(fseeko64)(stream, offset, whence);
498}
499
500INTERCEPTOR(int, fsetpos64, FILE *stream, const fpos64_t *pos) {
501 __rtsan_notify_intercepted_call(intercepted_function_name: "fsetpos64");
502 return REAL(fsetpos64)(stream, pos);
503}
504
505INTERCEPTOR(off64_t, ftello64, FILE *stream) {
506 __rtsan_notify_intercepted_call(intercepted_function_name: "ftello64");
507 return REAL(ftello64)(stream);
508}
509#endif
510
511INTERCEPTOR(void, rewind, FILE *stream) {
512 __rtsan_notify_intercepted_call(intercepted_function_name: "rewind");
513 return REAL(rewind)(stream);
514}
515#define RTSAN_MAYBE_INTERCEPT_FGETPOS INTERCEPT_FUNCTION(fgetpos)
516#define RTSAN_MAYBE_INTERCEPT_FSEEK INTERCEPT_FUNCTION(fseek)
517#define RTSAN_MAYBE_INTERCEPT_FSEEKO INTERCEPT_FUNCTION(fseeko)
518#define RTSAN_MAYBE_INTERCEPT_FSETPOS INTERCEPT_FUNCTION(fsetpos)
519#define RTSAN_MAYBE_INTERCEPT_FTELL INTERCEPT_FUNCTION(ftell)
520#define RTSAN_MAYBE_INTERCEPT_FTELLO INTERCEPT_FUNCTION(ftello)
521#define RTSAN_MAYBE_INTERCEPT_REWIND INTERCEPT_FUNCTION(rewind)
522#if SANITIZER_LINUX && !SANITIZER_MUSL
523#define RTSAN_MAYBE_INTERCEPT_FGETPOS64 INTERCEPT_FUNCTION(fgetpos64)
524#define RTSAN_MAYBE_INTERCEPT_FSEEKO64 INTERCEPT_FUNCTION(fseeko64)
525#define RTSAN_MAYBE_INTERCEPT_FSETPOS64 INTERCEPT_FUNCTION(fsetpos64)
526#define RTSAN_MAYBE_INTERCEPT_FTELLO64 INTERCEPT_FUNCTION(ftello64)
527#else
528#define RTSAN_MAYBE_INTERCEPT_FGETPOS64
529#define RTSAN_MAYBE_INTERCEPT_FSEEKO64
530#define RTSAN_MAYBE_INTERCEPT_FSETPOS64
531#define RTSAN_MAYBE_INTERCEPT_FTELLO64
532#endif
533#else
534#define RTSAN_MAYBE_INTERCEPT_FGETPOS
535#define RTSAN_MAYBE_INTERCEPT_FSEEK
536#define RTSAN_MAYBE_INTERCEPT_FSEEKO
537#define RTSAN_MAYBE_INTERCEPT_FSETPOS
538#define RTSAN_MAYBE_INTERCEPT_FTELL
539#define RTSAN_MAYBE_INTERCEPT_FTELLO
540#define RTSAN_MAYBE_INTERCEPT_REWIND
541#define RTSAN_MAYBE_INTERCEPT_FGETPOS64
542#define RTSAN_MAYBE_INTERCEPT_FSEEKO64
543#define RTSAN_MAYBE_INTERCEPT_FSETPOS64
544#define RTSAN_MAYBE_INTERCEPT_FTELLO64
545#endif
546
547INTERCEPTOR(int, puts, const char *s) {
548 __rtsan_notify_intercepted_call(intercepted_function_name: "puts");
549 return REAL(puts)(s);
550}
551
552INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) {
553 __rtsan_notify_intercepted_call(intercepted_function_name: "read");
554 return REAL(read)(fd, buf, count);
555}
556
557INTERCEPTOR(ssize_t, write, int fd, const void *buf, size_t count) {
558 __rtsan_notify_intercepted_call(intercepted_function_name: "write");
559 return REAL(write)(fd, buf, count);
560}
561
562INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) {
563 __rtsan_notify_intercepted_call(intercepted_function_name: "pread");
564 return REAL(pread)(fd, buf, count, offset);
565}
566
567#if SANITIZER_INTERCEPT_PREAD64
568INTERCEPTOR(ssize_t, pread64, int fd, void *buf, size_t count, off_t offset) {
569 __rtsan_notify_intercepted_call(intercepted_function_name: "pread64");
570 return REAL(pread64)(fd, buf, count, offset);
571}
572#define RTSAN_MAYBE_INTERCEPT_PREAD64 INTERCEPT_FUNCTION(pread64)
573#else
574#define RTSAN_MAYBE_INTERCEPT_PREAD64
575#endif // SANITIZER_INTERCEPT_PREAD64
576
577INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) {
578 __rtsan_notify_intercepted_call(intercepted_function_name: "readv");
579 return REAL(readv)(fd, iov, iovcnt);
580}
581
582INTERCEPTOR(ssize_t, pwrite, int fd, const void *buf, size_t count,
583 off_t offset) {
584 __rtsan_notify_intercepted_call(intercepted_function_name: "pwrite");
585 return REAL(pwrite)(fd, buf, count, offset);
586}
587
588#if SANITIZER_INTERCEPT_PWRITE64
589INTERCEPTOR(ssize_t, pwrite64, int fd, const void *buf, size_t count,
590 off_t offset) {
591 __rtsan_notify_intercepted_call(intercepted_function_name: "pwrite64");
592 return REAL(pwrite64)(fd, buf, count, offset);
593}
594#define RTSAN_MAYBE_INTERCEPT_PWRITE64 INTERCEPT_FUNCTION(pwrite64)
595#else
596#define RTSAN_MAYBE_INTERCEPT_PWRITE64
597#endif // SANITIZER_INTERCEPT_PWRITE64
598
599#if SANITIZER_INTERCEPT_PREADV
600INTERCEPTOR(ssize_t, preadv, int fd, const struct iovec *iov, int count,
601 off_t offset) {
602 __rtsan_notify_intercepted_call(intercepted_function_name: "preadv");
603 return REAL(preadv)(fd, iov, count, offset);
604}
605#define RTSAN_MAYBE_INTERCEPT_PREADV INTERCEPT_FUNCTION(preadv)
606#else
607#define RTSAN_MAYBE_INTERCEPT_PREADV
608#endif
609
610#if SANITIZER_INTERCEPT_PREADV64
611INTERCEPTOR(ssize_t, preadv64, int fd, const struct iovec *iov, int count,
612 off_t offset) {
613 __rtsan_notify_intercepted_call(intercepted_function_name: "preadv64");
614 return REAL(preadv)(fd, iov, count, offset);
615}
616#define RTSAN_MAYBE_INTERCEPT_PREADV64 INTERCEPT_FUNCTION(preadv64)
617#else
618#define RTSAN_MAYBE_INTERCEPT_PREADV64
619#endif
620
621#if SANITIZER_INTERCEPT_PWRITEV
622INTERCEPTOR(ssize_t, pwritev, int fd, const struct iovec *iov, int count,
623 off_t offset) {
624 __rtsan_notify_intercepted_call(intercepted_function_name: "pwritev");
625 return REAL(pwritev)(fd, iov, count, offset);
626}
627#define RTSAN_MAYBE_INTERCEPT_PWRITEV INTERCEPT_FUNCTION(pwritev)
628#else
629#define RTSAN_MAYBE_INTERCEPT_PWRITEV
630#endif
631
632#if SANITIZER_INTERCEPT_PWRITEV64
633INTERCEPTOR(ssize_t, pwritev64, int fd, const struct iovec *iov, int count,
634 off_t offset) {
635 __rtsan_notify_intercepted_call(intercepted_function_name: "pwritev64");
636 return REAL(pwritev64)(fd, iov, count, offset);
637}
638#define RTSAN_MAYBE_INTERCEPT_PWRITEV64 INTERCEPT_FUNCTION(pwritev64)
639#else
640#define RTSAN_MAYBE_INTERCEPT_PWRITEV64
641#endif
642
643INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
644 __rtsan_notify_intercepted_call(intercepted_function_name: "writev");
645 return REAL(writev)(fd, iov, iovcnt);
646}
647
648INTERCEPTOR(off_t, lseek, int fd, off_t offset, int whence) {
649 __rtsan_notify_intercepted_call(intercepted_function_name: "lseek");
650 return REAL(lseek)(fd, offset, whence);
651}
652
653#if SANITIZER_INTERCEPT_LSEEK64
654INTERCEPTOR(off64_t, lseek64, int fd, off64_t offset, int whence) {
655 __rtsan_notify_intercepted_call(intercepted_function_name: "lseek64");
656 return REAL(lseek64)(fd, offset, whence);
657}
658#define RTSAN_MAYBE_INTERCEPT_LSEEK64 INTERCEPT_FUNCTION(lseek64)
659#else
660#define RTSAN_MAYBE_INTERCEPT_LSEEK64
661#endif // SANITIZER_INTERCEPT_LSEEK64
662
663INTERCEPTOR(int, dup, int oldfd) {
664 __rtsan_notify_intercepted_call(intercepted_function_name: "dup");
665 return REAL(dup)(oldfd);
666}
667
668INTERCEPTOR(int, dup2, int oldfd, int newfd) {
669 __rtsan_notify_intercepted_call(intercepted_function_name: "dup2");
670 return REAL(dup2)(oldfd, newfd);
671}
672
673INTERCEPTOR(int, chmod, const char *path, mode_t mode) {
674 __rtsan_notify_intercepted_call(intercepted_function_name: "chmod");
675 return REAL(chmod)(path, mode);
676}
677
678INTERCEPTOR(int, fchmod, int fd, mode_t mode) {
679 __rtsan_notify_intercepted_call(intercepted_function_name: "fchmod");
680 return REAL(fchmod)(fd, mode);
681}
682
683INTERCEPTOR(int, mkdir, const char *path, mode_t mode) {
684 __rtsan_notify_intercepted_call(intercepted_function_name: "mkdir");
685 return REAL(mkdir)(path, mode);
686}
687
688INTERCEPTOR(int, rmdir, const char *path) {
689 __rtsan_notify_intercepted_call(intercepted_function_name: "rmdir");
690 return REAL(rmdir)(path);
691}
692
693INTERCEPTOR(mode_t, umask, mode_t cmask) {
694 __rtsan_notify_intercepted_call(intercepted_function_name: "umask");
695 return REAL(umask)(cmask);
696}
697
698// Concurrency
699#if SANITIZER_APPLE
700#pragma clang diagnostic push
701// OSSpinLockLock is deprecated, but still in use in libc++
702#pragma clang diagnostic ignored "-Wdeprecated-declarations"
703#undef OSSpinLockLock
704
705INTERCEPTOR(void, OSSpinLockLock, volatile OSSpinLock *lock) {
706 __rtsan_notify_intercepted_call("OSSpinLockLock");
707 return REAL(OSSpinLockLock)(lock);
708}
709
710#define RTSAN_MAYBE_INTERCEPT_OSSPINLOCKLOCK INTERCEPT_FUNCTION(OSSpinLockLock)
711#else
712#define RTSAN_MAYBE_INTERCEPT_OSSPINLOCKLOCK
713#endif // SANITIZER_APPLE
714
715#if SANITIZER_APPLE
716// _os_nospin_lock_lock may replace OSSpinLockLock due to deprecation macro.
717typedef volatile OSSpinLock *_os_nospin_lock_t;
718
719INTERCEPTOR(void, _os_nospin_lock_lock, _os_nospin_lock_t lock) {
720 __rtsan_notify_intercepted_call("_os_nospin_lock_lock");
721 return REAL(_os_nospin_lock_lock)(lock);
722}
723#pragma clang diagnostic pop // "-Wdeprecated-declarations"
724#endif // SANITIZER_APPLE
725
726#if SANITIZER_APPLE
727INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) {
728 __rtsan_notify_intercepted_call("os_unfair_lock_lock");
729 return REAL(os_unfair_lock_lock)(lock);
730}
731
732#define RTSAN_MAYBE_INTERCEPT_OS_UNFAIR_LOCK_LOCK \
733 INTERCEPT_FUNCTION(os_unfair_lock_lock)
734#else
735#define RTSAN_MAYBE_INTERCEPT_OS_UNFAIR_LOCK_LOCK
736#endif // SANITIZER_APPLE
737
738#if SANITIZER_LINUX
739INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *spinlock) {
740 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_spin_lock");
741 return REAL(pthread_spin_lock)(spinlock);
742}
743#define RTSAN_MAYBE_INTERCEPT_PTHREAD_SPIN_LOCK \
744 INTERCEPT_FUNCTION(pthread_spin_lock)
745#else
746#define RTSAN_MAYBE_INTERCEPT_PTHREAD_SPIN_LOCK
747#endif // SANITIZER_LINUX
748
749INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr,
750 void *(*start_routine)(void *), void *arg) {
751 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_create");
752 return REAL(pthread_create)(thread, attr, start_routine, arg);
753}
754
755INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *mutex) {
756 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_mutex_lock");
757 return REAL(pthread_mutex_lock)(mutex);
758}
759
760INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *mutex) {
761 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_mutex_unlock");
762 return REAL(pthread_mutex_unlock)(mutex);
763}
764
765INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) {
766 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_join");
767 return REAL(pthread_join)(thread, value_ptr);
768}
769
770INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *cond,
771 const pthread_condattr_t *a) {
772 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_cond_init");
773 return REAL(pthread_cond_init)(cond, a);
774}
775
776INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
777 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_cond_signal");
778 return REAL(pthread_cond_signal)(cond);
779}
780
781INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *cond) {
782 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_cond_broadcast");
783 return REAL(pthread_cond_broadcast)(cond);
784}
785
786INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *cond,
787 pthread_mutex_t *mutex) {
788 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_cond_wait");
789 return REAL(pthread_cond_wait)(cond, mutex);
790}
791
792INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond,
793 pthread_mutex_t *mutex, const timespec *ts) {
794 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_cond_timedwait");
795 return REAL(pthread_cond_timedwait)(cond, mutex, ts);
796}
797
798INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *cond) {
799 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_cond_destroy");
800 return REAL(pthread_cond_destroy)(cond);
801}
802
803INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
804 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_rwlock_rdlock");
805 return REAL(pthread_rwlock_rdlock)(lock);
806}
807
808INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *lock) {
809 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_rwlock_unlock");
810 return REAL(pthread_rwlock_unlock)(lock);
811}
812
813INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) {
814 __rtsan_notify_intercepted_call(intercepted_function_name: "pthread_rwlock_wrlock");
815 return REAL(pthread_rwlock_wrlock)(lock);
816}
817
818// Sleeping
819
820INTERCEPTOR(unsigned int, sleep, unsigned int s) {
821 __rtsan_notify_intercepted_call(intercepted_function_name: "sleep");
822 return REAL(sleep)(s);
823}
824
825INTERCEPTOR(int, usleep, useconds_t u) {
826 __rtsan_notify_intercepted_call(intercepted_function_name: "usleep");
827 return REAL(usleep)(u);
828}
829
830INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
831 struct timespec *rmtp) {
832 __rtsan_notify_intercepted_call(intercepted_function_name: "nanosleep");
833 return REAL(nanosleep)(rqtp, rmtp);
834}
835
836INTERCEPTOR(int, sched_yield, void) {
837 __rtsan_notify_intercepted_call(intercepted_function_name: "sched_yield");
838 return REAL(sched_yield)();
839}
840
841#if SANITIZER_LINUX
842INTERCEPTOR(int, sched_getaffinity, pid_t pid, size_t len, cpu_set_t *set) {
843 __rtsan_notify_intercepted_call(intercepted_function_name: "sched_getaffinity");
844 return REAL(sched_getaffinity)(pid, len, set);
845}
846
847INTERCEPTOR(int, sched_setaffinity, pid_t pid, size_t len,
848 const cpu_set_t *set) {
849 __rtsan_notify_intercepted_call(intercepted_function_name: "sched_setaffinity");
850 return REAL(sched_setaffinity)(pid, len, set);
851}
852#define RTSAN_MAYBE_INTERCEPT_SCHED_GETAFFINITY \
853 INTERCEPT_FUNCTION(sched_getaffinity)
854#define RTSAN_MAYBE_INTERCEPT_SCHED_SETAFFINITY \
855 INTERCEPT_FUNCTION(sched_setaffinity)
856#else
857#define RTSAN_MAYBE_INTERCEPT_SCHED_GETAFFINITY
858#define RTSAN_MAYBE_INTERCEPT_SCHED_SETAFFINITY
859#endif
860
861// Memory
862
863INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {
864 if (DlsymAlloc::Use())
865 return DlsymAlloc::Callocate(nmemb: num, size);
866
867 __rtsan_notify_intercepted_call(intercepted_function_name: "calloc");
868 return REAL(calloc)(num, size);
869}
870
871INTERCEPTOR(void, free, void *ptr) {
872 if (DlsymAlloc::PointerIsMine(ptr))
873 return DlsymAlloc::Free(ptr);
874
875 // According to the C and C++ standard, freeing a nullptr is guaranteed to be
876 // a no-op (and thus real-time safe). This can be confirmed for looking at
877 // __libc_free in the glibc source.
878 if (ptr != nullptr)
879 __rtsan_notify_intercepted_call(intercepted_function_name: "free");
880
881 return REAL(free)(ptr);
882}
883
884#if SANITIZER_INTERCEPT_FREE_SIZED
885INTERCEPTOR(void, free_sized, void *ptr, SIZE_T size) {
886 if (DlsymAlloc::PointerIsMine(ptr))
887 return DlsymAlloc::Free(ptr);
888
889 // According to the C and C++ standard, freeing a nullptr is guaranteed to be
890 // a no-op (and thus real-time safe). This can be confirmed for looking at
891 // __libc_free in the glibc source.
892 if (ptr != nullptr)
893 __rtsan_notify_intercepted_call(intercepted_function_name: "free_sized");
894
895 if (REAL(free_sized))
896 return REAL(free_sized)(ptr, size);
897 return REAL(free)(ptr);
898}
899#define RTSAN_MAYBE_INTERCEPT_FREE_SIZED INTERCEPT_FUNCTION(free_sized)
900#else
901#define RTSAN_MAYBE_INTERCEPT_FREE_SIZED
902#endif
903
904#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
905INTERCEPTOR(void, free_aligned_sized, void *ptr, SIZE_T alignment,
906 SIZE_T size) {
907 if (DlsymAlloc::PointerIsMine(ptr))
908 return DlsymAlloc::Free(ptr);
909
910 // According to the C and C++ standard, freeing a nullptr is guaranteed to be
911 // a no-op (and thus real-time safe). This can be confirmed for looking at
912 // __libc_free in the glibc source.
913 if (ptr != nullptr)
914 __rtsan_notify_intercepted_call(intercepted_function_name: "free_aligned_sized");
915
916 if (REAL(free_aligned_sized))
917 return REAL(free_aligned_sized)(ptr, alignment, size);
918 return REAL(free)(ptr);
919}
920#define RTSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED \
921 INTERCEPT_FUNCTION(free_aligned_sized)
922#else
923#define RTSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED
924#endif
925
926INTERCEPTOR(void *, malloc, SIZE_T size) {
927 if (DlsymAlloc::Use())
928 return DlsymAlloc::Allocate(size_in_bytes: size);
929
930 __rtsan_notify_intercepted_call(intercepted_function_name: "malloc");
931 return REAL(malloc)(size);
932}
933
934INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
935 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
936 return DlsymAlloc::Realloc(ptr, new_size: size);
937
938 __rtsan_notify_intercepted_call(intercepted_function_name: "realloc");
939 return REAL(realloc)(ptr, size);
940}
941
942INTERCEPTOR(void *, reallocf, void *ptr, SIZE_T size) {
943 __rtsan_notify_intercepted_call(intercepted_function_name: "reallocf");
944 return REAL(reallocf)(ptr, size);
945}
946
947INTERCEPTOR(void *, valloc, SIZE_T size) {
948 __rtsan_notify_intercepted_call(intercepted_function_name: "valloc");
949 return REAL(valloc)(size);
950}
951
952#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
953
954// In some cases, when targeting older Darwin versions, this warning may pop up.
955// Because we are providing a wrapper, the client is responsible to check
956// whether aligned_alloc is available, not us. We still succeed linking on an
957// old OS, because we are using a weak symbol (see aligned_alloc in
958// sanitizer_platform_interceptors.h)
959#pragma clang diagnostic push
960#pragma clang diagnostic ignored "-Wunguarded-availability-new"
961INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
962 __rtsan_notify_intercepted_call(intercepted_function_name: "aligned_alloc");
963 return REAL(aligned_alloc)(alignment, size);
964}
965#pragma clang diagnostic pop
966#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
967#else
968#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
969#endif
970
971INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
972 __rtsan_notify_intercepted_call(intercepted_function_name: "posix_memalign");
973 return REAL(posix_memalign)(memptr, alignment, size);
974}
975
976#if SANITIZER_INTERCEPT_MEMALIGN
977INTERCEPTOR(void *, memalign, size_t alignment, size_t size) {
978 __rtsan_notify_intercepted_call(intercepted_function_name: "memalign");
979 return REAL(memalign)(alignment, size);
980}
981#define RTSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
982#else
983#define RTSAN_MAYBE_INTERCEPT_MEMALIGN
984#endif
985
986#if SANITIZER_INTERCEPT_PVALLOC
987INTERCEPTOR(void *, pvalloc, size_t size) {
988 __rtsan_notify_intercepted_call(intercepted_function_name: "pvalloc");
989 return REAL(pvalloc)(size);
990}
991#define RTSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
992#else
993#define RTSAN_MAYBE_INTERCEPT_PVALLOC
994#endif
995
996INTERCEPTOR(void *, mmap, void *addr, size_t length, int prot, int flags,
997 int fd, off_t offset) {
998 __rtsan_notify_intercepted_call(intercepted_function_name: "mmap");
999 return REAL(mmap)(addr, length, prot, flags, fd, offset);
1000}
1001
1002#if SANITIZER_INTERCEPT_MMAP64
1003INTERCEPTOR(void *, mmap64, void *addr, size_t length, int prot, int flags,
1004 int fd, off64_t offset) {
1005 __rtsan_notify_intercepted_call(intercepted_function_name: "mmap64");
1006 return REAL(mmap64)(addr, length, prot, flags, fd, offset);
1007}
1008#define RTSAN_MAYBE_INTERCEPT_MMAP64 INTERCEPT_FUNCTION(mmap64)
1009#else
1010#define RTSAN_MAYBE_INTERCEPT_MMAP64
1011#endif // SANITIZER_INTERCEPT_MMAP64
1012
1013#if SANITIZER_LINUX
1014// Note that even if rtsan is ported to netbsd, it has a slighty different
1015// and non-variadic signature
1016INTERCEPTOR(void *, mremap, void *oaddr, size_t olength, size_t nlength,
1017 int flags, ...) {
1018 __rtsan_notify_intercepted_call(intercepted_function_name: "mremap");
1019
1020 // the last optional argument is only used in this case
1021 // as the new page region will be assigned to. Is ignored otherwise.
1022 if (flags & MREMAP_FIXED) {
1023 va_list args;
1024
1025 va_start(args, flags);
1026 void *naddr = va_arg(args, void *);
1027 va_end(args);
1028
1029 return REAL(mremap)(oaddr, olength, nlength, flags, naddr);
1030 }
1031
1032 return REAL(mremap)(oaddr, olength, nlength, flags);
1033}
1034#define RTSAN_MAYBE_INTERCEPT_MREMAP INTERCEPT_FUNCTION(mremap)
1035#else
1036#define RTSAN_MAYBE_INTERCEPT_MREMAP
1037#endif
1038
1039INTERCEPTOR(int, munmap, void *addr, size_t length) {
1040 __rtsan_notify_intercepted_call(intercepted_function_name: "munmap");
1041 return REAL(munmap)(addr, length);
1042}
1043
1044#if !SANITIZER_APPLE
1045INTERCEPTOR(int, madvise, void *addr, size_t length, int flag) {
1046 __rtsan_notify_intercepted_call(intercepted_function_name: "madvise");
1047 return REAL(madvise)(addr, length, flag);
1048}
1049
1050INTERCEPTOR(int, posix_madvise, void *addr, size_t length, int flag) {
1051 __rtsan_notify_intercepted_call(intercepted_function_name: "posix_madvise");
1052 return REAL(posix_madvise)(addr, length, flag);
1053}
1054#define RTSAN_MAYBE_INTERCEPT_MADVISE INTERCEPT_FUNCTION(madvise)
1055#define RTSAN_MAYBE_INTERCEPT_POSIX_MADVISE INTERCEPT_FUNCTION(posix_madvise)
1056#else
1057#define RTSAN_MAYBE_INTERCEPT_MADVISE
1058#define RTSAN_MAYBE_INTERCEPT_POSIX_MADVISE
1059#endif
1060
1061INTERCEPTOR(int, mprotect, void *addr, size_t length, int prot) {
1062 __rtsan_notify_intercepted_call(intercepted_function_name: "mprotect");
1063 return REAL(mprotect)(addr, length, prot);
1064}
1065
1066INTERCEPTOR(int, msync, void *addr, size_t length, int flag) {
1067 __rtsan_notify_intercepted_call(intercepted_function_name: "msync");
1068 return REAL(msync)(addr, length, flag);
1069}
1070
1071#if SANITIZER_APPLE
1072INTERCEPTOR(int, mincore, const void *addr, size_t length, char *vec) {
1073#else
1074INTERCEPTOR(int, mincore, void *addr, size_t length, unsigned char *vec) {
1075#endif
1076 __rtsan_notify_intercepted_call(intercepted_function_name: "mincore");
1077 return REAL(mincore)(addr, length, vec);
1078}
1079
1080INTERCEPTOR(int, shm_open, const char *name, int oflag, mode_t mode) {
1081 __rtsan_notify_intercepted_call(intercepted_function_name: "shm_open");
1082 return REAL(shm_open)(name, oflag, mode);
1083}
1084
1085INTERCEPTOR(int, shm_unlink, const char *name) {
1086 __rtsan_notify_intercepted_call(intercepted_function_name: "shm_unlink");
1087 return REAL(shm_unlink)(name);
1088}
1089
1090#if !SANITIZER_APPLE
1091// is supported by freebsd too
1092INTERCEPTOR(int, memfd_create, const char *path, unsigned int flags) {
1093 __rtsan_notify_intercepted_call(intercepted_function_name: "memfd_create");
1094 return REAL(memfd_create)(path, flags);
1095}
1096#define RTSAN_MAYBE_INTERCEPT_MEMFD_CREATE INTERCEPT_FUNCTION(memfd_create)
1097#else
1098#define RTSAN_MAYBE_INTERCEPT_MEMFD_CREATE
1099#endif
1100
1101// Sockets
1102INTERCEPTOR(int, getaddrinfo, const char *node, const char *service,
1103 const struct addrinfo *hints, struct addrinfo **res) {
1104 __rtsan_notify_intercepted_call(intercepted_function_name: "getaddrinfo");
1105 return REAL(getaddrinfo)(node, service, hints, res);
1106}
1107
1108INTERCEPTOR(int, getnameinfo, const struct sockaddr *sa, socklen_t salen,
1109 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
1110 int flags) {
1111 __rtsan_notify_intercepted_call(intercepted_function_name: "getnameinfo");
1112 return REAL(getnameinfo)(sa, salen, host, hostlen, serv, servlen, flags);
1113}
1114
1115#if SANITIZER_INTERCEPT_GETSOCKNAME
1116INTERCEPTOR(int, getsockname, int socket, struct sockaddr *sa,
1117 socklen_t *salen) {
1118 __rtsan_notify_intercepted_call(intercepted_function_name: "getsockname");
1119 return REAL(getsockname)(socket, sa, salen);
1120}
1121#define RTSAN_MAYBE_INTERCEPT_GETSOCKNAME INTERCEPT_FUNCTION(getsockname)
1122#else
1123#define RTSAN_MAYBE_INTERCEPT_GETSOCKNAME
1124#endif
1125
1126#if SANITIZER_INTERCEPT_GETPEERNAME
1127INTERCEPTOR(int, getpeername, int socket, struct sockaddr *sa,
1128 socklen_t *salen) {
1129 __rtsan_notify_intercepted_call(intercepted_function_name: "getpeername");
1130 return REAL(getpeername)(socket, sa, salen);
1131}
1132#define RTSAN_MAYBE_INTERCEPT_GETPEERNAME INTERCEPT_FUNCTION(getpeername)
1133#else
1134#define RTSAN_MAYBE_INTERCEPT_GETPEERNAME
1135#endif
1136
1137INTERCEPTOR(int, bind, int socket, const struct sockaddr *address,
1138 socklen_t address_len) {
1139 __rtsan_notify_intercepted_call(intercepted_function_name: "bind");
1140 return REAL(bind)(socket, address, address_len);
1141}
1142
1143INTERCEPTOR(int, listen, int socket, int backlog) {
1144 __rtsan_notify_intercepted_call(intercepted_function_name: "listen");
1145 return REAL(listen)(socket, backlog);
1146}
1147
1148INTERCEPTOR(int, accept, int socket, struct sockaddr *address,
1149 socklen_t *address_len) {
1150 __rtsan_notify_intercepted_call(intercepted_function_name: "accept");
1151 return REAL(accept)(socket, address, address_len);
1152}
1153
1154INTERCEPTOR(int, connect, int socket, const struct sockaddr *address,
1155 socklen_t address_len) {
1156 __rtsan_notify_intercepted_call(intercepted_function_name: "connect");
1157 return REAL(connect)(socket, address, address_len);
1158}
1159
1160INTERCEPTOR(int, socket, int domain, int type, int protocol) {
1161 __rtsan_notify_intercepted_call(intercepted_function_name: "socket");
1162 return REAL(socket)(domain, type, protocol);
1163}
1164
1165INTERCEPTOR(ssize_t, send, int sockfd, const void *buf, size_t len, int flags) {
1166 __rtsan_notify_intercepted_call(intercepted_function_name: "send");
1167 return REAL(send)(sockfd, buf, len, flags);
1168}
1169
1170INTERCEPTOR(ssize_t, sendmsg, int socket, const struct msghdr *message,
1171 int flags) {
1172 __rtsan_notify_intercepted_call(intercepted_function_name: "sendmsg");
1173 return REAL(sendmsg)(socket, message, flags);
1174}
1175
1176#if SANITIZER_INTERCEPT_SENDMMSG
1177#if SANITIZER_MUSL
1178INTERCEPTOR(int, sendmmsg, int socket, struct mmsghdr *message,
1179 unsigned int len, unsigned int flags) {
1180#else
1181INTERCEPTOR(int, sendmmsg, int socket, struct mmsghdr *message,
1182 unsigned int len, int flags) {
1183#endif
1184 __rtsan_notify_intercepted_call(intercepted_function_name: "sendmmsg");
1185 return REAL(sendmmsg)(socket, message, len, flags);
1186}
1187#define RTSAN_MAYBE_INTERCEPT_SENDMMSG INTERCEPT_FUNCTION(sendmmsg)
1188#else
1189#define RTSAN_MAYBE_INTERCEPT_SENDMMSG
1190#endif
1191
1192INTERCEPTOR(ssize_t, sendto, int socket, const void *buffer, size_t length,
1193 int flags, const struct sockaddr *dest_addr, socklen_t dest_len) {
1194 __rtsan_notify_intercepted_call(intercepted_function_name: "sendto");
1195 return REAL(sendto)(socket, buffer, length, flags, dest_addr, dest_len);
1196}
1197
1198INTERCEPTOR(ssize_t, recv, int socket, void *buffer, size_t length, int flags) {
1199 __rtsan_notify_intercepted_call(intercepted_function_name: "recv");
1200 return REAL(recv)(socket, buffer, length, flags);
1201}
1202
1203INTERCEPTOR(ssize_t, recvfrom, int socket, void *buffer, size_t length,
1204 int flags, struct sockaddr *address, socklen_t *address_len) {
1205 __rtsan_notify_intercepted_call(intercepted_function_name: "recvfrom");
1206 return REAL(recvfrom)(socket, buffer, length, flags, address, address_len);
1207}
1208
1209INTERCEPTOR(ssize_t, recvmsg, int socket, struct msghdr *message, int flags) {
1210 __rtsan_notify_intercepted_call(intercepted_function_name: "recvmsg");
1211 return REAL(recvmsg)(socket, message, flags);
1212}
1213
1214#if SANITIZER_INTERCEPT_RECVMMSG
1215#if SANITIZER_MUSL
1216INTERCEPTOR(int, recvmmsg, int socket, struct mmsghdr *message,
1217 unsigned int len, unsigned int flags, struct timespec *timeout) {
1218#elif defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ < 21
1219INTERCEPTOR(int, recvmmsg, int socket, struct mmsghdr *message,
1220 unsigned int len, int flags, const struct timespec *timeout) {
1221#else
1222INTERCEPTOR(int, recvmmsg, int socket, struct mmsghdr *message,
1223 unsigned int len, int flags, struct timespec *timeout) {
1224#endif // defined(__GLIBC_MINOR) && __GLIBC_MINOR__ < 21
1225 __rtsan_notify_intercepted_call(intercepted_function_name: "recvmmsg");
1226 return REAL(recvmmsg)(socket, message, len, flags, timeout);
1227}
1228#define RTSAN_MAYBE_INTERCEPT_RECVMMSG INTERCEPT_FUNCTION(recvmmsg)
1229#else
1230#define RTSAN_MAYBE_INTERCEPT_RECVMMSG
1231#endif
1232
1233INTERCEPTOR(int, shutdown, int socket, int how) {
1234 __rtsan_notify_intercepted_call(intercepted_function_name: "shutdown");
1235 return REAL(shutdown)(socket, how);
1236}
1237
1238#if SANITIZER_INTERCEPT_ACCEPT4
1239INTERCEPTOR(int, accept4, int socket, struct sockaddr *address,
1240 socklen_t *address_len, int flags) {
1241 __rtsan_notify_intercepted_call(intercepted_function_name: "accept4");
1242 return REAL(accept4)(socket, address, address_len, flags);
1243}
1244#define RTSAN_MAYBE_INTERCEPT_ACCEPT4 INTERCEPT_FUNCTION(accept4)
1245#else
1246#define RTSAN_MAYBE_INTERCEPT_ACCEPT4
1247#endif
1248
1249#if SANITIZER_INTERCEPT_GETSOCKOPT
1250INTERCEPTOR(int, getsockopt, int socket, int level, int option, void *value,
1251 socklen_t *len) {
1252 __rtsan_notify_intercepted_call(intercepted_function_name: "getsockopt");
1253 return REAL(getsockopt)(socket, level, option, value, len);
1254}
1255
1256INTERCEPTOR(int, setsockopt, int socket, int level, int option,
1257 const void *value, socklen_t len) {
1258 __rtsan_notify_intercepted_call(intercepted_function_name: "setsockopt");
1259 return REAL(setsockopt)(socket, level, option, value, len);
1260}
1261#define RTSAN_MAYBE_INTERCEPT_GETSOCKOPT INTERCEPT_FUNCTION(getsockopt)
1262#define RTSAN_MAYBE_INTERCEPT_SETSOCKOPT INTERCEPT_FUNCTION(setsockopt)
1263#else
1264#define RTSAN_MAYBE_INTERCEPT_GETSOCKOPT
1265#define RTSAN_MAYBE_INTERCEPT_SETSOCKOPT
1266#endif
1267
1268INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int pair[2]) {
1269 __rtsan_notify_intercepted_call(intercepted_function_name: "socketpair");
1270 return REAL(socketpair)(domain, type, protocol, pair);
1271}
1272
1273// I/O Multiplexing
1274
1275INTERCEPTOR(int, poll, struct pollfd *fds, nfds_t nfds, int timeout) {
1276 __rtsan_notify_intercepted_call(intercepted_function_name: "poll");
1277 return REAL(poll)(fds, nfds, timeout);
1278}
1279
1280#if !SANITIZER_APPLE
1281// FIXME: This should work on all unix systems, even Mac, but currently
1282// it is showing some weird error while linking
1283// error: declaration of 'select' has a different language linkage
1284INTERCEPTOR(int, select, int nfds, fd_set *readfds, fd_set *writefds,
1285 fd_set *exceptfds, struct timeval *timeout) {
1286 __rtsan_notify_intercepted_call(intercepted_function_name: "select");
1287 return REAL(select)(nfds, readfds, writefds, exceptfds, timeout);
1288}
1289#define RTSAN_MAYBE_INTERCEPT_SELECT INTERCEPT_FUNCTION(select)
1290#else
1291#define RTSAN_MAYBE_INTERCEPT_SELECT
1292#endif // !SANITIZER_APPLE
1293
1294INTERCEPTOR(int, pselect, int nfds, fd_set *readfds, fd_set *writefds,
1295 fd_set *exceptfds, const struct timespec *timeout,
1296 const sigset_t *sigmask) {
1297 __rtsan_notify_intercepted_call(intercepted_function_name: "pselect");
1298 return REAL(pselect)(nfds, readfds, writefds, exceptfds, timeout, sigmask);
1299}
1300
1301#if SANITIZER_INTERCEPT_EPOLL
1302INTERCEPTOR(int, epoll_create, int size) {
1303 __rtsan_notify_intercepted_call(intercepted_function_name: "epoll_create");
1304 return REAL(epoll_create)(size);
1305}
1306
1307INTERCEPTOR(int, epoll_create1, int flags) {
1308 __rtsan_notify_intercepted_call(intercepted_function_name: "epoll_create1");
1309 return REAL(epoll_create1)(flags);
1310}
1311
1312INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd,
1313 struct epoll_event *event) {
1314 __rtsan_notify_intercepted_call(intercepted_function_name: "epoll_ctl");
1315 return REAL(epoll_ctl)(epfd, op, fd, event);
1316}
1317
1318INTERCEPTOR(int, epoll_wait, int epfd, struct epoll_event *events,
1319 int maxevents, int timeout) {
1320 __rtsan_notify_intercepted_call(intercepted_function_name: "epoll_wait");
1321 return REAL(epoll_wait)(epfd, events, maxevents, timeout);
1322}
1323
1324INTERCEPTOR(int, epoll_pwait, int epfd, struct epoll_event *events,
1325 int maxevents, int timeout, const sigset_t *sigmask) {
1326 __rtsan_notify_intercepted_call(intercepted_function_name: "epoll_pwait");
1327 return REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
1328}
1329#define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE INTERCEPT_FUNCTION(epoll_create)
1330#define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE1 INTERCEPT_FUNCTION(epoll_create1)
1331#define RTSAN_MAYBE_INTERCEPT_EPOLL_CTL INTERCEPT_FUNCTION(epoll_ctl)
1332#define RTSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
1333#define RTSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
1334#else
1335#define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE
1336#define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE1
1337#define RTSAN_MAYBE_INTERCEPT_EPOLL_CTL
1338#define RTSAN_MAYBE_INTERCEPT_EPOLL_WAIT
1339#define RTSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
1340#endif // SANITIZER_INTERCEPT_EPOLL
1341
1342#if SANITIZER_INTERCEPT_PPOLL
1343INTERCEPTOR(int, ppoll, struct pollfd *fds, nfds_t n, const struct timespec *ts,
1344 const sigset_t *set) {
1345 __rtsan_notify_intercepted_call(intercepted_function_name: "ppoll");
1346 return REAL(ppoll)(fds, n, ts, set);
1347}
1348#define RTSAN_MAYBE_INTERCEPT_PPOLL INTERCEPT_FUNCTION(ppoll)
1349#else
1350#define RTSAN_MAYBE_INTERCEPT_PPOLL
1351#endif
1352
1353#if SANITIZER_INTERCEPT_KQUEUE
1354INTERCEPTOR(int, kqueue, void) {
1355 __rtsan_notify_intercepted_call("kqueue");
1356 return REAL(kqueue)();
1357}
1358
1359INTERCEPTOR(int, kevent, int kq, const struct kevent *changelist, int nchanges,
1360 struct kevent *eventlist, int nevents,
1361 const struct timespec *timeout) {
1362 __rtsan_notify_intercepted_call("kevent");
1363 return REAL(kevent)(kq, changelist, nchanges, eventlist, nevents, timeout);
1364}
1365
1366INTERCEPTOR(int, kevent64, int kq, const struct kevent64_s *changelist,
1367 int nchanges, struct kevent64_s *eventlist, int nevents,
1368 unsigned int flags, const struct timespec *timeout) {
1369 __rtsan_notify_intercepted_call("kevent64");
1370 return REAL(kevent64)(kq, changelist, nchanges, eventlist, nevents, flags,
1371 timeout);
1372}
1373#define RTSAN_MAYBE_INTERCEPT_KQUEUE INTERCEPT_FUNCTION(kqueue)
1374#define RTSAN_MAYBE_INTERCEPT_KEVENT INTERCEPT_FUNCTION(kevent)
1375#define RTSAN_MAYBE_INTERCEPT_KEVENT64 INTERCEPT_FUNCTION(kevent64)
1376#else
1377#define RTSAN_MAYBE_INTERCEPT_KQUEUE
1378#define RTSAN_MAYBE_INTERCEPT_KEVENT
1379#define RTSAN_MAYBE_INTERCEPT_KEVENT64
1380#endif // SANITIZER_INTERCEPT_KQUEUE
1381
1382#if SANITIZER_LINUX
1383INTERCEPTOR(int, inotify_init) {
1384 __rtsan_notify_intercepted_call(intercepted_function_name: "inotify_init");
1385 return REAL(inotify_init)();
1386}
1387
1388INTERCEPTOR(int, inotify_init1, int flags) {
1389 __rtsan_notify_intercepted_call(intercepted_function_name: "inotify_init1");
1390 return REAL(inotify_init1)(flags);
1391}
1392
1393INTERCEPTOR(int, inotify_add_watch, int fd, const char *path, uint32_t mask) {
1394 __rtsan_notify_intercepted_call(intercepted_function_name: "inotify_add_watch");
1395 return REAL(inotify_add_watch)(fd, path, mask);
1396}
1397
1398INTERCEPTOR(int, inotify_rm_watch, int fd, int wd) {
1399 __rtsan_notify_intercepted_call(intercepted_function_name: "inotify_rm_watch");
1400 return REAL(inotify_rm_watch)(fd, wd);
1401}
1402
1403INTERCEPTOR(int, timerfd_create, int clockid, int flags) {
1404 __rtsan_notify_intercepted_call(intercepted_function_name: "timerfd_create");
1405 return REAL(timerfd_create)(clockid, flags);
1406}
1407
1408INTERCEPTOR(int, timerfd_settime, int fd, int flags, const itimerspec *newval,
1409 struct itimerspec *oldval) {
1410 __rtsan_notify_intercepted_call(intercepted_function_name: "timerfd_settime");
1411 return REAL(timerfd_settime)(fd, flags, newval, oldval);
1412}
1413
1414INTERCEPTOR(int, timerfd_gettime, int fd, struct itimerspec *val) {
1415 __rtsan_notify_intercepted_call(intercepted_function_name: "timerfd_gettime");
1416 return REAL(timerfd_gettime)(fd, val);
1417}
1418
1419/* eventfd wrappers calls SYS_eventfd2 down the line */
1420INTERCEPTOR(int, eventfd, unsigned int count, int flags) {
1421 __rtsan_notify_intercepted_call(intercepted_function_name: "eventfd");
1422 return REAL(eventfd)(count, flags);
1423}
1424#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT INTERCEPT_FUNCTION(inotify_init)
1425#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 INTERCEPT_FUNCTION(inotify_init1)
1426#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH \
1427 INTERCEPT_FUNCTION(inotify_add_watch)
1428#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH \
1429 INTERCEPT_FUNCTION(inotify_rm_watch)
1430#define RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE INTERCEPT_FUNCTION(timerfd_create)
1431#define RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME \
1432 INTERCEPT_FUNCTION(timerfd_settime)
1433#define RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME \
1434 INTERCEPT_FUNCTION(timerfd_gettime)
1435#define RTSAN_MAYBE_INTERCEPT_EVENTFD INTERCEPT_FUNCTION(eventfd)
1436#else
1437#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT
1438#define RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
1439#define RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH
1440#define RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH
1441#define RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE
1442#define RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME
1443#define RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME
1444#define RTSAN_MAYBE_INTERCEPT_EVENTFD
1445#endif
1446
1447INTERCEPTOR(int, pipe, int pipefd[2]) {
1448 __rtsan_notify_intercepted_call(intercepted_function_name: "pipe");
1449 return REAL(pipe)(pipefd);
1450}
1451
1452#if !SANITIZER_APPLE
1453INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
1454 __rtsan_notify_intercepted_call(intercepted_function_name: "pipe2");
1455 return REAL(pipe2)(pipefd, flags);
1456}
1457#define RTSAN_MAYBE_INTERCEPT_PIPE2 INTERCEPT_FUNCTION(pipe2)
1458#else
1459#define RTSAN_MAYBE_INTERCEPT_PIPE2
1460#endif
1461
1462INTERCEPTOR(int, mkfifo, const char *pathname, mode_t mode) {
1463 __rtsan_notify_intercepted_call(intercepted_function_name: "mkfifo");
1464 return REAL(mkfifo)(pathname, mode);
1465}
1466
1467INTERCEPTOR(pid_t, fork, void) {
1468 __rtsan_notify_intercepted_call(intercepted_function_name: "fork");
1469 return REAL(fork)();
1470}
1471
1472INTERCEPTOR(int, execve, const char *filename, char *const argv[],
1473 char *const envp[]) {
1474 __rtsan_notify_intercepted_call(intercepted_function_name: "execve");
1475 return REAL(execve)(filename, argv, envp);
1476}
1477
1478#if SANITIZER_INTERCEPT_PROCESS_VM_READV
1479INTERCEPTOR(ssize_t, process_vm_readv, pid_t pid, const struct iovec *local_iov,
1480 unsigned long liovcnt, const struct iovec *remote_iov,
1481 unsigned long riovcnt, unsigned long flags) {
1482 __rtsan_notify_intercepted_call(intercepted_function_name: "process_vm_readv");
1483 return REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov, riovcnt,
1484 flags);
1485}
1486
1487INTERCEPTOR(ssize_t, process_vm_writev, pid_t pid,
1488 const struct iovec *local_iov, unsigned long liovcnt,
1489 const struct iovec *remote_iov, unsigned long riovcnt,
1490 unsigned long flags) {
1491 __rtsan_notify_intercepted_call(intercepted_function_name: "process_vm_writev");
1492 return REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov, riovcnt,
1493 flags);
1494}
1495#define RTSAN_MAYBE_INTERCEPT_PROCESS_VM_READV \
1496 INTERCEPT_FUNCTION(process_vm_readv)
1497#define RTSAN_MAYBE_INTERCEPT_PROCESS_VM_WRITEV \
1498 INTERCEPT_FUNCTION(process_vm_writev)
1499#else
1500#define RTSAN_MAYBE_INTERCEPT_PROCESS_VM_READV
1501#define RTSAN_MAYBE_INTERCEPT_PROCESS_VM_WRITEV
1502#endif
1503
1504// TODO: the `wait` family of functions is an oddity. In testing, if you
1505// intercept them, Darwin seemingly ignores them, and linux never returns from
1506// the test. Revisit this in the future, but hopefully intercepting fork/exec is
1507// enough to dissuade usage of wait by proxy.
1508
1509#if SANITIZER_APPLE
1510#define INT_TYPE_SYSCALL int
1511#else
1512#define INT_TYPE_SYSCALL long
1513#endif
1514
1515#pragma clang diagnostic push
1516#pragma clang diagnostic ignored "-Wdeprecated-declarations"
1517INTERCEPTOR(INT_TYPE_SYSCALL, syscall, INT_TYPE_SYSCALL number, ...) {
1518 __rtsan_notify_intercepted_call(intercepted_function_name: "syscall");
1519
1520 va_list args;
1521 va_start(args, number);
1522
1523 // the goal is to pick something large enough to hold all syscall args
1524 // see fcntl for more discussion and why we always pull all 6 args
1525 using arg_type = unsigned long;
1526 arg_type arg1 = va_arg(args, arg_type);
1527 arg_type arg2 = va_arg(args, arg_type);
1528 arg_type arg3 = va_arg(args, arg_type);
1529 arg_type arg4 = va_arg(args, arg_type);
1530 arg_type arg5 = va_arg(args, arg_type);
1531 arg_type arg6 = va_arg(args, arg_type);
1532
1533 // these are various examples of things that COULD be passed
1534 static_assert(sizeof(arg_type) >= sizeof(off_t));
1535 static_assert(sizeof(arg_type) >= sizeof(struct flock *));
1536 static_assert(sizeof(arg_type) >= sizeof(const char *));
1537 static_assert(sizeof(arg_type) >= sizeof(int));
1538 static_assert(sizeof(arg_type) >= sizeof(unsigned long));
1539
1540 va_end(args);
1541
1542 return REAL(syscall)(number, arg1, arg2, arg3, arg4, arg5, arg6);
1543}
1544#pragma clang diagnostic pop
1545
1546// Preinit
1547void __rtsan::InitializeInterceptors() {
1548 INTERCEPT_FUNCTION(calloc);
1549 INTERCEPT_FUNCTION(free);
1550 RTSAN_MAYBE_INTERCEPT_FREE_SIZED;
1551 RTSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED;
1552 INTERCEPT_FUNCTION(malloc);
1553 INTERCEPT_FUNCTION(realloc);
1554 INTERCEPT_FUNCTION(reallocf);
1555 INTERCEPT_FUNCTION(valloc);
1556 RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
1557 INTERCEPT_FUNCTION(posix_memalign);
1558 INTERCEPT_FUNCTION(mmap);
1559 RTSAN_MAYBE_INTERCEPT_MMAP64;
1560 RTSAN_MAYBE_INTERCEPT_MREMAP;
1561 INTERCEPT_FUNCTION(munmap);
1562 RTSAN_MAYBE_INTERCEPT_MADVISE;
1563 RTSAN_MAYBE_INTERCEPT_POSIX_MADVISE;
1564 INTERCEPT_FUNCTION(mprotect);
1565 INTERCEPT_FUNCTION(msync);
1566 INTERCEPT_FUNCTION(mincore);
1567 INTERCEPT_FUNCTION(shm_open);
1568 INTERCEPT_FUNCTION(shm_unlink);
1569 RTSAN_MAYBE_INTERCEPT_MEMFD_CREATE;
1570 RTSAN_MAYBE_INTERCEPT_MEMALIGN;
1571 RTSAN_MAYBE_INTERCEPT_PVALLOC;
1572
1573 INTERCEPT_FUNCTION(open);
1574 RTSAN_MAYBE_INTERCEPT_OPEN64;
1575 INTERCEPT_FUNCTION(openat);
1576 RTSAN_MAYBE_INTERCEPT_OPENAT64;
1577 INTERCEPT_FUNCTION(close);
1578 INTERCEPT_FUNCTION(chdir);
1579 INTERCEPT_FUNCTION(fchdir);
1580 RTSAN_MAYBE_INTERCEPT_READLINK;
1581 RTSAN_MAYBE_INTERCEPT_READLINKAT;
1582 INTERCEPT_FUNCTION(unlink);
1583 INTERCEPT_FUNCTION(unlinkat);
1584 INTERCEPT_FUNCTION(symlink);
1585 INTERCEPT_FUNCTION(symlinkat);
1586 INTERCEPT_FUNCTION(truncate);
1587 INTERCEPT_FUNCTION(ftruncate);
1588 RTSAN_MAYBE_INTERCEPT_TRUNCATE64;
1589 RTSAN_MAYBE_INTERCEPT_FTRUNCATE64;
1590 INTERCEPT_FUNCTION(fopen);
1591 RTSAN_MAYBE_INTERCEPT_FOPEN64;
1592 RTSAN_MAYBE_INTERCEPT_FREOPEN64;
1593 INTERCEPT_FUNCTION(fread);
1594 INTERCEPT_FUNCTION(read);
1595 INTERCEPT_FUNCTION(write);
1596 INTERCEPT_FUNCTION(pread);
1597 RTSAN_MAYBE_INTERCEPT_PREAD64;
1598 RTSAN_MAYBE_INTERCEPT_PREADV;
1599 RTSAN_MAYBE_INTERCEPT_PREADV64;
1600 INTERCEPT_FUNCTION(readv);
1601 INTERCEPT_FUNCTION(pwrite);
1602 RTSAN_MAYBE_INTERCEPT_PWRITE64;
1603 RTSAN_MAYBE_INTERCEPT_PWRITEV;
1604 RTSAN_MAYBE_INTERCEPT_PWRITEV64;
1605 INTERCEPT_FUNCTION(writev);
1606 INTERCEPT_FUNCTION(fwrite);
1607 INTERCEPT_FUNCTION(fclose);
1608 INTERCEPT_FUNCTION(fcntl);
1609 RTSAN_MAYBE_INTERCEPT_FCNTL64;
1610 INTERCEPT_FUNCTION(creat);
1611 RTSAN_MAYBE_INTERCEPT_CREAT64;
1612 INTERCEPT_FUNCTION(puts);
1613 INTERCEPT_FUNCTION(fputs);
1614 INTERCEPT_FUNCTION(fflush);
1615 RTSAN_MAYBE_INTERCEPT_FPURGE;
1616 RTSAN_MAYBE_INTERCEPT_PIPE2;
1617 INTERCEPT_FUNCTION(fdopen);
1618 INTERCEPT_FUNCTION(freopen);
1619 RTSAN_MAYBE_INTERCEPT_FOPENCOOKIE;
1620 RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
1621 RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
1622 RTSAN_MAYBE_INTERCEPT_SETBUF;
1623 RTSAN_MAYBE_INTERCEPT_SETVBUF;
1624 RTSAN_MAYBE_INTERCEPT_SETLINEBUF;
1625 RTSAN_MAYBE_INTERCEPT_SETBUFFER;
1626 RTSAN_MAYBE_INTERCEPT_FGETPOS;
1627 RTSAN_MAYBE_INTERCEPT_FSEEK;
1628 RTSAN_MAYBE_INTERCEPT_FSEEKO;
1629 RTSAN_MAYBE_INTERCEPT_FSETPOS;
1630 RTSAN_MAYBE_INTERCEPT_FTELL;
1631 RTSAN_MAYBE_INTERCEPT_FTELLO;
1632 RTSAN_MAYBE_INTERCEPT_REWIND;
1633 RTSAN_MAYBE_INTERCEPT_FGETPOS64;
1634 RTSAN_MAYBE_INTERCEPT_FSEEKO64;
1635 RTSAN_MAYBE_INTERCEPT_FSETPOS64;
1636 RTSAN_MAYBE_INTERCEPT_FTELLO64;
1637 INTERCEPT_FUNCTION(lseek);
1638 RTSAN_MAYBE_INTERCEPT_LSEEK64;
1639 INTERCEPT_FUNCTION(dup);
1640 INTERCEPT_FUNCTION(dup2);
1641 INTERCEPT_FUNCTION(chmod);
1642 INTERCEPT_FUNCTION(fchmod);
1643 INTERCEPT_FUNCTION(mkdir);
1644 INTERCEPT_FUNCTION(rmdir);
1645 INTERCEPT_FUNCTION(umask);
1646 INTERCEPT_FUNCTION(ioctl);
1647
1648 RTSAN_MAYBE_INTERCEPT_OSSPINLOCKLOCK;
1649 RTSAN_MAYBE_INTERCEPT_OS_UNFAIR_LOCK_LOCK;
1650 RTSAN_MAYBE_INTERCEPT_PTHREAD_SPIN_LOCK;
1651
1652 INTERCEPT_FUNCTION(pthread_create);
1653 INTERCEPT_FUNCTION(pthread_mutex_lock);
1654 INTERCEPT_FUNCTION(pthread_mutex_unlock);
1655 INTERCEPT_FUNCTION(pthread_join);
1656
1657 // See the comment in tsan_interceptors_posix.cpp.
1658#if SANITIZER_GLIBC && !__GLIBC_PREREQ(2, 36) && \
1659 (defined(__x86_64__) || defined(__mips__) || SANITIZER_PPC64V1 || \
1660 defined(__s390x__))
1661 INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2");
1662 INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2");
1663 INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
1664 INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2");
1665 INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
1666 INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2");
1667#else
1668 INTERCEPT_FUNCTION(pthread_cond_init);
1669 INTERCEPT_FUNCTION(pthread_cond_signal);
1670 INTERCEPT_FUNCTION(pthread_cond_broadcast);
1671 INTERCEPT_FUNCTION(pthread_cond_wait);
1672 INTERCEPT_FUNCTION(pthread_cond_timedwait);
1673 INTERCEPT_FUNCTION(pthread_cond_destroy);
1674#endif
1675
1676 INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
1677 INTERCEPT_FUNCTION(pthread_rwlock_unlock);
1678 INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
1679
1680 INTERCEPT_FUNCTION(sleep);
1681 INTERCEPT_FUNCTION(usleep);
1682 INTERCEPT_FUNCTION(nanosleep);
1683 INTERCEPT_FUNCTION(sched_yield);
1684 RTSAN_MAYBE_INTERCEPT_SCHED_GETAFFINITY;
1685 RTSAN_MAYBE_INTERCEPT_SCHED_SETAFFINITY;
1686
1687 INTERCEPT_FUNCTION(accept);
1688 INTERCEPT_FUNCTION(bind);
1689 INTERCEPT_FUNCTION(connect);
1690 INTERCEPT_FUNCTION(getaddrinfo);
1691 INTERCEPT_FUNCTION(getnameinfo);
1692 INTERCEPT_FUNCTION(listen);
1693 INTERCEPT_FUNCTION(recv);
1694 INTERCEPT_FUNCTION(recvfrom);
1695 INTERCEPT_FUNCTION(recvmsg);
1696 RTSAN_MAYBE_INTERCEPT_RECVMMSG;
1697 INTERCEPT_FUNCTION(send);
1698 INTERCEPT_FUNCTION(sendmsg);
1699 RTSAN_MAYBE_INTERCEPT_SENDMMSG;
1700 INTERCEPT_FUNCTION(sendto);
1701 INTERCEPT_FUNCTION(shutdown);
1702 INTERCEPT_FUNCTION(socket);
1703 RTSAN_MAYBE_INTERCEPT_ACCEPT4;
1704 RTSAN_MAYBE_INTERCEPT_GETSOCKNAME;
1705 RTSAN_MAYBE_INTERCEPT_GETPEERNAME;
1706 RTSAN_MAYBE_INTERCEPT_GETSOCKOPT;
1707 RTSAN_MAYBE_INTERCEPT_SETSOCKOPT;
1708 INTERCEPT_FUNCTION(socketpair);
1709
1710 RTSAN_MAYBE_INTERCEPT_SELECT;
1711 INTERCEPT_FUNCTION(pselect);
1712 INTERCEPT_FUNCTION(poll);
1713 RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE;
1714 RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE1;
1715 RTSAN_MAYBE_INTERCEPT_EPOLL_CTL;
1716 RTSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1717 RTSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1718 RTSAN_MAYBE_INTERCEPT_PPOLL;
1719 RTSAN_MAYBE_INTERCEPT_KQUEUE;
1720 RTSAN_MAYBE_INTERCEPT_KEVENT;
1721 RTSAN_MAYBE_INTERCEPT_KEVENT64;
1722
1723 RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT;
1724 RTSAN_MAYBE_INTERCEPT_INOTIFY_INIT1;
1725 RTSAN_MAYBE_INTERCEPT_INOTIFY_ADD_WATCH;
1726 RTSAN_MAYBE_INTERCEPT_INOTIFY_RM_WATCH;
1727
1728 RTSAN_MAYBE_INTERCEPT_TIMERFD_CREATE;
1729 RTSAN_MAYBE_INTERCEPT_TIMERFD_SETTIME;
1730 RTSAN_MAYBE_INTERCEPT_TIMERFD_GETTIME;
1731 RTSAN_MAYBE_INTERCEPT_EVENTFD;
1732
1733 INTERCEPT_FUNCTION(pipe);
1734 INTERCEPT_FUNCTION(mkfifo);
1735
1736 INTERCEPT_FUNCTION(fork);
1737 INTERCEPT_FUNCTION(execve);
1738
1739 RTSAN_MAYBE_INTERCEPT_PROCESS_VM_READV;
1740 RTSAN_MAYBE_INTERCEPT_PROCESS_VM_WRITEV;
1741
1742 INTERCEPT_FUNCTION(syscall);
1743}
1744
1745#endif // SANITIZER_POSIX
1746