1 //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common function interceptors for tools like AddressSanitizer,
11 // ThreadSanitizer, MemorySanitizer, etc.
13 // This file should be included into the tool's interceptor file,
14 // which has to define it's own macros:
15 // COMMON_INTERCEPTOR_ENTER
16 // COMMON_INTERCEPTOR_READ_RANGE
17 // COMMON_INTERCEPTOR_WRITE_RANGE
18 // COMMON_INTERCEPTOR_FD_ACQUIRE
19 // COMMON_INTERCEPTOR_FD_RELEASE
20 // COMMON_INTERCEPTOR_SET_THREAD_NAME
21 //===----------------------------------------------------------------------===//
22 #include "interception/interception.h"
23 #include "sanitizer_platform_interceptors.h"
27 #if SANITIZER_INTERCEPT_READ
28 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
30 COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
31 SSIZE_T res = REAL(read)(fd, ptr, count);
33 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
34 if (res >= 0 && fd >= 0)
35 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
38 # define INIT_READ INTERCEPT_FUNCTION(read)
43 #if SANITIZER_INTERCEPT_PREAD
44 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
46 COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
47 SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
49 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
50 if (res >= 0 && fd >= 0)
51 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
54 # define INIT_PREAD INTERCEPT_FUNCTION(pread)
59 #if SANITIZER_INTERCEPT_PREAD64
60 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
62 COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
63 SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
65 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
66 if (res >= 0 && fd >= 0)
67 COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
70 # define INIT_PREAD64 INTERCEPT_FUNCTION(pread64)
75 #if SANITIZER_INTERCEPT_WRITE
76 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
78 COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
80 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
81 SSIZE_T res = REAL(write)(fd, ptr, count);
83 COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
86 # define INIT_WRITE INTERCEPT_FUNCTION(write)
91 #if SANITIZER_INTERCEPT_PWRITE
92 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
94 COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
96 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
97 SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
99 COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
102 # define INIT_PWRITE INTERCEPT_FUNCTION(pwrite)
107 #if SANITIZER_INTERCEPT_PWRITE64
108 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
111 COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
113 COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
114 SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
116 COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
119 # define INIT_PWRITE64 INTERCEPT_FUNCTION(pwrite64)
121 # define INIT_PWRITE64
124 #if SANITIZER_INTERCEPT_PRCTL
125 INTERCEPTOR(int, prctl, int option,
126 unsigned long arg2, unsigned long arg3, // NOLINT
127 unsigned long arg4, unsigned long arg5) { // NOLINT
129 COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
130 static const int PR_SET_NAME = 15;
131 int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
132 if (option == PR_SET_NAME) {
134 internal_strncpy(buff, (char*)arg2, 15);
136 COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
140 # define INIT_PRCTL INTERCEPT_FUNCTION(prctl)
143 #endif // SANITIZER_INTERCEPT_PRCTL
146 #if SANITIZER_INTERCEPT_SCANF
148 #include "sanitizer_common_interceptors_scanf.inc"
150 INTERCEPTOR(int, vscanf, const char *format, va_list ap) { // NOLINT
152 COMMON_INTERCEPTOR_ENTER(ctx, vscanf, format, ap);
153 scanf_common(ctx, format, ap);
154 int res = REAL(vscanf)(format, ap); // NOLINT
158 INTERCEPTOR(int, vsscanf, const char *str, const char *format, // NOLINT
161 COMMON_INTERCEPTOR_ENTER(ctx, vsscanf, str, format, ap);
162 scanf_common(ctx, format, ap);
163 int res = REAL(vsscanf)(str, format, ap); // NOLINT
164 // FIXME: read of str
168 INTERCEPTOR(int, vfscanf, void *stream, const char *format, // NOLINT
171 COMMON_INTERCEPTOR_ENTER(ctx, vfscanf, stream, format, ap);
172 scanf_common(ctx, format, ap);
173 int res = REAL(vfscanf)(stream, format, ap); // NOLINT
177 INTERCEPTOR(int, scanf, const char *format, ...) { // NOLINT
179 COMMON_INTERCEPTOR_ENTER(ctx, scanf, format);
181 va_start(ap, format);
182 int res = vscanf(format, ap); // NOLINT
187 INTERCEPTOR(int, fscanf, void* stream, const char *format, ...) { // NOLINT
189 COMMON_INTERCEPTOR_ENTER(ctx, fscanf, stream, format);
191 va_start(ap, format);
192 int res = vfscanf(stream, format, ap); // NOLINT
197 INTERCEPTOR(int, sscanf, const char *str, const char *format, ...) { // NOLINT
199 COMMON_INTERCEPTOR_ENTER(ctx, sscanf, str, format); // NOLINT
201 va_start(ap, format);
202 int res = vsscanf(str, format, ap); // NOLINT
208 INTERCEPT_FUNCTION(scanf); \
209 INTERCEPT_FUNCTION(sscanf); /* NOLINT */ \
210 INTERCEPT_FUNCTION(fscanf); \
211 INTERCEPT_FUNCTION(vscanf); \
212 INTERCEPT_FUNCTION(vsscanf); \
213 INTERCEPT_FUNCTION(vfscanf)
219 #define SANITIZER_COMMON_INTERCEPTORS_INIT \