Evgeniy Stepanov | 4f32c0b | 2013-01-18 13:01:18 +0000 | [diff] [blame] | 1 | //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===// |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Common function interceptors for tools like AddressSanitizer, |
| 11 | // ThreadSanitizer, MemorySanitizer, etc. |
| 12 | // |
| 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 |
Kostya Serebryany | c803319 | 2013-01-17 13:09:00 +0000 | [diff] [blame] | 18 | // COMMON_INTERCEPTOR_FD_ACQUIRE |
| 19 | // COMMON_INTERCEPTOR_FD_RELEASE |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 20 | // COMMON_INTERCEPTOR_SET_THREAD_NAME |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 22 | #include "interception/interception.h" |
Alexey Samsonov | 74737d5 | 2012-12-13 08:50:16 +0000 | [diff] [blame] | 23 | #include "sanitizer_platform_interceptors.h" |
Kostya Serebryany | b1cc4e4 | 2012-12-13 05:27:08 +0000 | [diff] [blame] | 24 | |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 25 | #include <stdarg.h> |
| 26 | |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 27 | #if SANITIZER_WINDOWS |
Evgeniy Stepanov | 348bd12 | 2013-02-11 14:08:12 +0000 | [diff] [blame] | 28 | #define va_copy(dst, src) ((dst) = (src)) |
| 29 | #endif // _WIN32 |
| 30 | |
Dmitry Vyukov | be52366 | 2013-03-26 12:40:23 +0000 | [diff] [blame] | 31 | #if SANITIZER_INTERCEPT_STRCASECMP |
| 32 | static inline int CharCaseCmp(unsigned char c1, unsigned char c2) { |
| 33 | int c1_low = ToLower(c1); |
| 34 | int c2_low = ToLower(c2); |
| 35 | return c1_low - c2_low; |
| 36 | } |
| 37 | |
| 38 | INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) { |
| 39 | void *ctx; |
| 40 | COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2); |
| 41 | unsigned char c1 = 0, c2 = 0; |
| 42 | uptr i; |
| 43 | for (i = 0; ; i++) { |
| 44 | c1 = (unsigned char)s1[i]; |
| 45 | c2 = (unsigned char)s2[i]; |
| 46 | if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') |
| 47 | break; |
| 48 | } |
| 49 | COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1); |
| 50 | COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1); |
| 51 | return CharCaseCmp(c1, c2); |
| 52 | } |
| 53 | |
| 54 | INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) { |
| 55 | void *ctx; |
| 56 | COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n); |
| 57 | unsigned char c1 = 0, c2 = 0; |
| 58 | uptr i; |
| 59 | for (i = 0; i < n; i++) { |
| 60 | c1 = (unsigned char)s1[i]; |
| 61 | c2 = (unsigned char)s2[i]; |
| 62 | if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') |
| 63 | break; |
| 64 | } |
| 65 | COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n)); |
| 66 | COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n)); |
| 67 | return CharCaseCmp(c1, c2); |
| 68 | } |
| 69 | |
| 70 | #define INIT_STRCASECMP INTERCEPT_FUNCTION(strcasecmp) |
| 71 | #define INIT_STRNCASECMP INTERCEPT_FUNCTION(strncasecmp) |
| 72 | #else |
| 73 | #define INIT_STRCASECMP |
| 74 | #define INIT_STRNCASECMP |
| 75 | #endif |
| 76 | |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 77 | #if SANITIZER_INTERCEPT_FREXP |
| 78 | INTERCEPTOR(double, frexp, double x, int *exp) { |
| 79 | void *ctx; |
| 80 | COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp); |
| 81 | double res = REAL(frexp)(x, exp); |
| 82 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp)); |
| 83 | return res; |
| 84 | } |
| 85 | |
Alexey Samsonov | ff5d1fc | 2013-03-15 14:02:21 +0000 | [diff] [blame] | 86 | #define INIT_FREXP INTERCEPT_FUNCTION(frexp); |
| 87 | #else |
| 88 | #define INIT_FREXP |
| 89 | #endif // SANITIZER_INTERCEPT_FREXP |
| 90 | |
| 91 | #if SANITIZER_INTERCEPT_FREXPF_FREXPL |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 92 | INTERCEPTOR(float, frexpf, float x, int *exp) { |
| 93 | void *ctx; |
| 94 | COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp); |
| 95 | float res = REAL(frexpf)(x, exp); |
| 96 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp)); |
| 97 | return res; |
| 98 | } |
| 99 | |
| 100 | INTERCEPTOR(long double, frexpl, long double x, int *exp) { |
| 101 | void *ctx; |
| 102 | COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp); |
| 103 | long double res = REAL(frexpl)(x, exp); |
| 104 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp)); |
| 105 | return res; |
| 106 | } |
| 107 | |
Alexey Samsonov | ff5d1fc | 2013-03-15 14:02:21 +0000 | [diff] [blame] | 108 | #define INIT_FREXPF_FREXPL \ |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 109 | INTERCEPT_FUNCTION(frexpf); \ |
| 110 | INTERCEPT_FUNCTION(frexpl) |
| 111 | #else |
Alexey Samsonov | ff5d1fc | 2013-03-15 14:02:21 +0000 | [diff] [blame] | 112 | #define INIT_FREXPF_FREXPL |
| 113 | #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 114 | |
Alexey Samsonov | 8ffd877 | 2012-12-13 08:36:13 +0000 | [diff] [blame] | 115 | #if SANITIZER_INTERCEPT_READ |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 116 | INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) { |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 117 | void *ctx; |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 118 | COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 119 | SSIZE_T res = REAL(read)(fd, ptr, count); |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 120 | if (res > 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 121 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res); |
Kostya Serebryany | c803319 | 2013-01-17 13:09:00 +0000 | [diff] [blame] | 122 | if (res >= 0 && fd >= 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 123 | COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd); |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 124 | return res; |
| 125 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 126 | #define INIT_READ INTERCEPT_FUNCTION(read) |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 127 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 128 | #define INIT_READ |
Alexey Samsonov | 8ffd877 | 2012-12-13 08:36:13 +0000 | [diff] [blame] | 129 | #endif |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 130 | |
Alexey Samsonov | c333dff | 2012-12-13 08:10:23 +0000 | [diff] [blame] | 131 | #if SANITIZER_INTERCEPT_PREAD |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 132 | INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) { |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 133 | void *ctx; |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 134 | COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 135 | SSIZE_T res = REAL(pread)(fd, ptr, count, offset); |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 136 | if (res > 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 137 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res); |
Kostya Serebryany | c803319 | 2013-01-17 13:09:00 +0000 | [diff] [blame] | 138 | if (res >= 0 && fd >= 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 139 | COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd); |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 140 | return res; |
| 141 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 142 | #define INIT_PREAD INTERCEPT_FUNCTION(pread) |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 143 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 144 | #define INIT_PREAD |
Alexey Samsonov | c333dff | 2012-12-13 08:10:23 +0000 | [diff] [blame] | 145 | #endif |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 146 | |
Kostya Serebryany | b1cc4e4 | 2012-12-13 05:27:08 +0000 | [diff] [blame] | 147 | #if SANITIZER_INTERCEPT_PREAD64 |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 148 | INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) { |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 149 | void *ctx; |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 150 | COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 151 | SSIZE_T res = REAL(pread64)(fd, ptr, count, offset); |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 152 | if (res > 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 153 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res); |
Kostya Serebryany | c803319 | 2013-01-17 13:09:00 +0000 | [diff] [blame] | 154 | if (res >= 0 && fd >= 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 155 | COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd); |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 156 | return res; |
| 157 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 158 | #define INIT_PREAD64 INTERCEPT_FUNCTION(pread64) |
Alexey Samsonov | c333dff | 2012-12-13 08:10:23 +0000 | [diff] [blame] | 159 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 160 | #define INIT_PREAD64 |
Alexander Potapenko | 1f5e23e | 2012-12-12 11:52:26 +0000 | [diff] [blame] | 161 | #endif |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 162 | |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 163 | #if SANITIZER_INTERCEPT_WRITE |
| 164 | INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) { |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 165 | void *ctx; |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 166 | COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 167 | if (fd >= 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 168 | COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 169 | SSIZE_T res = REAL(write)(fd, ptr, count); |
| 170 | if (res > 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 171 | COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 172 | return res; |
| 173 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 174 | #define INIT_WRITE INTERCEPT_FUNCTION(write) |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 175 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 176 | #define INIT_WRITE |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | #if SANITIZER_INTERCEPT_PWRITE |
Dmitry Vyukov | f0c846b | 2013-01-24 07:44:21 +0000 | [diff] [blame] | 180 | INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) { |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 181 | void *ctx; |
Dmitry Vyukov | f0c846b | 2013-01-24 07:44:21 +0000 | [diff] [blame] | 182 | COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 183 | if (fd >= 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 184 | COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd); |
Dmitry Vyukov | f0c846b | 2013-01-24 07:44:21 +0000 | [diff] [blame] | 185 | SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 186 | if (res > 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 187 | COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 188 | return res; |
| 189 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 190 | #define INIT_PWRITE INTERCEPT_FUNCTION(pwrite) |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 191 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 192 | #define INIT_PWRITE |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 193 | #endif |
| 194 | |
| 195 | #if SANITIZER_INTERCEPT_PWRITE64 |
Dmitry Vyukov | f0c846b | 2013-01-24 07:44:21 +0000 | [diff] [blame] | 196 | INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count, |
| 197 | OFF64_T offset) { |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 198 | void *ctx; |
Dmitry Vyukov | f0c846b | 2013-01-24 07:44:21 +0000 | [diff] [blame] | 199 | COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 200 | if (fd >= 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 201 | COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd); |
Dmitry Vyukov | f0c846b | 2013-01-24 07:44:21 +0000 | [diff] [blame] | 202 | SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 203 | if (res > 0) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 204 | COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 205 | return res; |
| 206 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 207 | #define INIT_PWRITE64 INTERCEPT_FUNCTION(pwrite64) |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 208 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 209 | #define INIT_PWRITE64 |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 210 | #endif |
| 211 | |
| 212 | #if SANITIZER_INTERCEPT_PRCTL |
Evgeniy Stepanov | 69b109a | 2013-02-20 11:06:07 +0000 | [diff] [blame] | 213 | INTERCEPTOR(int, prctl, int option, |
| 214 | unsigned long arg2, unsigned long arg3, // NOLINT |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 215 | unsigned long arg4, unsigned long arg5) { // NOLINT |
| 216 | void *ctx; |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 217 | COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 218 | static const int PR_SET_NAME = 15; |
| 219 | int res = REAL(prctl(option, arg2, arg3, arg4, arg5)); |
| 220 | if (option == PR_SET_NAME) { |
| 221 | char buff[16]; |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 222 | internal_strncpy(buff, (char *)arg2, 15); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 223 | buff[15] = 0; |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 224 | COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 225 | } |
| 226 | return res; |
| 227 | } |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 228 | #define INIT_PRCTL INTERCEPT_FUNCTION(prctl) |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 229 | #else |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 230 | #define INIT_PRCTL |
| 231 | #endif // SANITIZER_INTERCEPT_PRCTL |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 232 | |
Evgeniy Stepanov | fef6605 | 2013-04-08 08:25:22 +0000 | [diff] [blame] | 233 | |
| 234 | #if SANITIZER_INTERCEPT_TIME |
| 235 | INTERCEPTOR(unsigned long, time, unsigned long *t) { |
| 236 | void *ctx; |
| 237 | COMMON_INTERCEPTOR_ENTER(ctx, time, t); |
| 238 | unsigned long res = REAL(time)(t); |
Alexander Potapenko | 15832c2 | 2013-04-10 15:13:00 +0000 | [diff] [blame] | 239 | if (t && res != (unsigned long)-1) { |
Evgeniy Stepanov | fef6605 | 2013-04-08 08:25:22 +0000 | [diff] [blame] | 240 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t)); |
| 241 | } |
| 242 | return res; |
| 243 | } |
| 244 | #define INIT_TIME \ |
| 245 | INTERCEPT_FUNCTION(time); |
| 246 | #else |
| 247 | #define INIT_TIME |
| 248 | #endif // SANITIZER_INTERCEPT_TIME |
| 249 | |
| 250 | |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 251 | #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS |
| 252 | INTERCEPTOR(void *, localtime, unsigned long *timep) { |
| 253 | void *ctx; |
| 254 | COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep); |
| 255 | void *res = REAL(localtime)(timep); |
| 256 | if (res) { |
| 257 | COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); |
| 258 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_tm_sz); |
| 259 | } |
| 260 | return res; |
| 261 | } |
| 262 | INTERCEPTOR(void *, localtime_r, unsigned long *timep, void *result) { |
| 263 | void *ctx; |
| 264 | COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result); |
| 265 | void *res = REAL(localtime_r)(timep, result); |
| 266 | if (res) { |
| 267 | COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); |
| 268 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_tm_sz); |
| 269 | } |
| 270 | return res; |
| 271 | } |
| 272 | INTERCEPTOR(void *, gmtime, unsigned long *timep) { |
| 273 | void *ctx; |
| 274 | COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep); |
| 275 | void *res = REAL(gmtime)(timep); |
| 276 | if (res) { |
| 277 | COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); |
| 278 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_tm_sz); |
| 279 | } |
| 280 | return res; |
| 281 | } |
| 282 | INTERCEPTOR(void *, gmtime_r, unsigned long *timep, void *result) { |
| 283 | void *ctx; |
| 284 | COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result); |
| 285 | void *res = REAL(gmtime_r)(timep, result); |
| 286 | if (res) { |
| 287 | COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); |
| 288 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_tm_sz); |
| 289 | } |
| 290 | return res; |
| 291 | } |
| 292 | INTERCEPTOR(char *, ctime, unsigned long *timep) { |
| 293 | void *ctx; |
| 294 | COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep); |
| 295 | char *res = REAL(ctime)(timep); |
| 296 | if (res) { |
| 297 | COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); |
| 298 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1); |
| 299 | } |
| 300 | return res; |
| 301 | } |
| 302 | INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) { |
| 303 | void *ctx; |
| 304 | COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result); |
| 305 | char *res = REAL(ctime_r)(timep, result); |
| 306 | if (res) { |
| 307 | COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); |
| 308 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1); |
| 309 | } |
| 310 | return res; |
| 311 | } |
| 312 | INTERCEPTOR(char *, asctime, void *tm) { |
| 313 | void *ctx; |
| 314 | COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm); |
| 315 | char *res = REAL(asctime)(tm); |
| 316 | if (res) { |
| 317 | COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, struct_tm_sz); |
| 318 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1); |
| 319 | } |
| 320 | return res; |
| 321 | } |
| 322 | INTERCEPTOR(char *, asctime_r, void *tm, char *result) { |
| 323 | void *ctx; |
| 324 | COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result); |
| 325 | char *res = REAL(asctime_r)(tm, result); |
| 326 | if (res) { |
| 327 | COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, struct_tm_sz); |
| 328 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1); |
| 329 | } |
| 330 | return res; |
| 331 | } |
| 332 | #define INIT_LOCALTIME_AND_FRIENDS \ |
| 333 | INTERCEPT_FUNCTION(localtime); \ |
| 334 | INTERCEPT_FUNCTION(localtime_r); \ |
| 335 | INTERCEPT_FUNCTION(gmtime); \ |
| 336 | INTERCEPT_FUNCTION(gmtime_r); \ |
| 337 | INTERCEPT_FUNCTION(ctime); \ |
| 338 | INTERCEPT_FUNCTION(ctime_r); \ |
| 339 | INTERCEPT_FUNCTION(asctime); \ |
| 340 | INTERCEPT_FUNCTION(asctime_r); |
| 341 | #else |
| 342 | #define INIT_LOCALTIME_AND_FRIENDS |
| 343 | #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS |
| 344 | |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 345 | #if SANITIZER_INTERCEPT_SCANF |
| 346 | |
Evgeniy Stepanov | 4f32c0b | 2013-01-18 13:01:18 +0000 | [diff] [blame] | 347 | #include "sanitizer_common_interceptors_scanf.inc" |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 348 | |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 349 | #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \ |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 350 | { \ |
| 351 | void *ctx; \ |
| 352 | COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \ |
| 353 | va_list aq; \ |
| 354 | va_copy(aq, ap); \ |
| 355 | int res = REAL(vname)(__VA_ARGS__); \ |
| 356 | if (res > 0) \ |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 357 | scanf_common(ctx, res, allowGnuMalloc, format, aq); \ |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 358 | va_end(aq); \ |
| 359 | return res; \ |
| 360 | } |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 361 | |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 362 | INTERCEPTOR(int, vscanf, const char *format, va_list ap) |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 363 | VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 364 | |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 365 | INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap) |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 366 | VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 367 | |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 368 | INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap) |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 369 | VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 370 | |
Alexander Potapenko | 9eab858 | 2013-02-20 15:27:58 +0000 | [diff] [blame] | 371 | #if SANITIZER_INTERCEPT_ISOC99_SCANF |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 372 | INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap) |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 373 | VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap) |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 374 | |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 375 | INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format, |
| 376 | va_list ap) |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 377 | VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap) |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 378 | |
| 379 | INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap) |
Evgeniy Stepanov | c5b4e86 | 2013-02-12 14:29:34 +0000 | [diff] [blame] | 380 | VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap) |
Alexander Potapenko | 9eab858 | 2013-02-20 15:27:58 +0000 | [diff] [blame] | 381 | #endif // SANITIZER_INTERCEPT_ISOC99_SCANF |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 382 | |
| 383 | #define SCANF_INTERCEPTOR_IMPL(name, vname, ...) \ |
| 384 | { \ |
| 385 | void *ctx; \ |
| 386 | COMMON_INTERCEPTOR_ENTER(ctx, name, __VA_ARGS__); \ |
| 387 | va_list ap; \ |
| 388 | va_start(ap, format); \ |
| 389 | int res = vname(__VA_ARGS__, ap); \ |
| 390 | va_end(ap); \ |
| 391 | return res; \ |
| 392 | } |
| 393 | |
| 394 | INTERCEPTOR(int, scanf, const char *format, ...) |
| 395 | SCANF_INTERCEPTOR_IMPL(scanf, vscanf, format) |
| 396 | |
| 397 | INTERCEPTOR(int, fscanf, void *stream, const char *format, ...) |
| 398 | SCANF_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format) |
| 399 | |
| 400 | INTERCEPTOR(int, sscanf, const char *str, const char *format, ...) |
| 401 | SCANF_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format) |
| 402 | |
Alexander Potapenko | 9eab858 | 2013-02-20 15:27:58 +0000 | [diff] [blame] | 403 | #if SANITIZER_INTERCEPT_ISOC99_SCANF |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 404 | INTERCEPTOR(int, __isoc99_scanf, const char *format, ...) |
| 405 | SCANF_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format) |
| 406 | |
| 407 | INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...) |
| 408 | SCANF_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format) |
| 409 | |
| 410 | INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...) |
| 411 | SCANF_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format) |
Alexander Potapenko | 9eab858 | 2013-02-20 15:27:58 +0000 | [diff] [blame] | 412 | #endif |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 413 | |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 414 | #define INIT_SCANF \ |
| 415 | INTERCEPT_FUNCTION(scanf); \ |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 416 | INTERCEPT_FUNCTION(sscanf); \ |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 417 | INTERCEPT_FUNCTION(fscanf); \ |
| 418 | INTERCEPT_FUNCTION(vscanf); \ |
| 419 | INTERCEPT_FUNCTION(vsscanf); \ |
Evgeniy Stepanov | 4ae1adb | 2013-02-12 11:34:52 +0000 | [diff] [blame] | 420 | INTERCEPT_FUNCTION(vfscanf); \ |
| 421 | INTERCEPT_FUNCTION(__isoc99_scanf); \ |
| 422 | INTERCEPT_FUNCTION(__isoc99_sscanf); \ |
| 423 | INTERCEPT_FUNCTION(__isoc99_fscanf); \ |
| 424 | INTERCEPT_FUNCTION(__isoc99_vscanf); \ |
| 425 | INTERCEPT_FUNCTION(__isoc99_vsscanf); \ |
| 426 | INTERCEPT_FUNCTION(__isoc99_vfscanf); |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 427 | |
| 428 | #else |
| 429 | #define INIT_SCANF |
| 430 | #endif |
| 431 | |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 432 | #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 433 | INTERCEPTOR(void *, getpwnam, const char *name) { |
| 434 | void *ctx; |
| 435 | COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name); |
| 436 | COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1); |
| 437 | void *res = REAL(getpwnam)(name); |
| 438 | if (res != 0) |
| 439 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz); |
| 440 | return res; |
| 441 | } |
| 442 | INTERCEPTOR(void *, getpwuid, u32 uid) { |
| 443 | void *ctx; |
| 444 | COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid); |
| 445 | void *res = REAL(getpwuid)(uid); |
| 446 | if (res != 0) |
| 447 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_passwd_sz); |
| 448 | return res; |
| 449 | } |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 450 | INTERCEPTOR(void *, getgrnam, const char *name) { |
| 451 | void *ctx; |
| 452 | COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name); |
| 453 | COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1); |
| 454 | void *res = REAL(getgrnam)(name); |
| 455 | if (res != 0) |
| 456 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz); |
| 457 | return res; |
| 458 | } |
| 459 | INTERCEPTOR(void *, getgrgid, u32 gid) { |
| 460 | void *ctx; |
| 461 | COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid); |
| 462 | void *res = REAL(getgrgid)(gid); |
| 463 | if (res != 0) |
| 464 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, struct_group_sz); |
| 465 | return res; |
| 466 | } |
| 467 | #define INIT_GETPWNAM_AND_FRIENDS \ |
| 468 | INTERCEPT_FUNCTION(getpwnam); \ |
| 469 | INTERCEPT_FUNCTION(getpwuid); \ |
| 470 | INTERCEPT_FUNCTION(getgrnam); \ |
| 471 | INTERCEPT_FUNCTION(getgrgid); |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 472 | #else |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 473 | #define INIT_GETPWNAM_AND_FRIENDS |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 474 | #endif |
| 475 | |
| 476 | |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 477 | #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 478 | INTERCEPTOR(int, getpwnam_r, const char *name, void *pwd, |
| 479 | char *buf, SIZE_T buflen, void **result) { |
| 480 | void *ctx; |
| 481 | COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result); |
| 482 | COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1); |
| 483 | int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result); |
| 484 | if (!res) { |
| 485 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz); |
| 486 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen); |
| 487 | } |
| 488 | return res; |
| 489 | } |
| 490 | INTERCEPTOR(int, getpwuid_r, u32 uid, void *pwd, |
| 491 | char *buf, SIZE_T buflen, void **result) { |
| 492 | void *ctx; |
| 493 | COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result); |
| 494 | int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result); |
| 495 | if (!res) { |
| 496 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, struct_passwd_sz); |
| 497 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen); |
| 498 | } |
| 499 | return res; |
| 500 | } |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 501 | INTERCEPTOR(int, getgrnam_r, const char *name, void *grp, |
| 502 | char *buf, SIZE_T buflen, void **result) { |
| 503 | void *ctx; |
| 504 | COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result); |
| 505 | COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1); |
| 506 | int res = REAL(getgrnam_r)(name, grp, buf, buflen, result); |
| 507 | if (!res) { |
| 508 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz); |
| 509 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen); |
| 510 | } |
| 511 | return res; |
| 512 | } |
| 513 | INTERCEPTOR(int, getgrgid_r, u32 gid, void *grp, |
| 514 | char *buf, SIZE_T buflen, void **result) { |
| 515 | void *ctx; |
| 516 | COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result); |
| 517 | int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result); |
| 518 | if (!res) { |
| 519 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, struct_group_sz); |
| 520 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen); |
| 521 | } |
| 522 | return res; |
| 523 | } |
| 524 | #define INIT_GETPWNAM_R_AND_FRIENDS \ |
| 525 | INTERCEPT_FUNCTION(getpwnam_r); \ |
| 526 | INTERCEPT_FUNCTION(getpwuid_r); \ |
| 527 | INTERCEPT_FUNCTION(getgrnam_r); \ |
| 528 | INTERCEPT_FUNCTION(getgrgid_r); |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 529 | #else |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 530 | #define INIT_GETPWNAM_R_AND_FRIENDS |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 531 | #endif |
| 532 | |
| 533 | |
| 534 | #if SANITIZER_INTERCEPT_CLOCK_GETTIME |
| 535 | INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) { |
| 536 | void *ctx; |
| 537 | COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp); |
| 538 | int res = REAL(clock_getres)(clk_id, tp); |
Evgeniy Stepanov | 7cdae16 | 2013-04-23 11:48:31 +0000 | [diff] [blame] | 539 | if (!res && tp) { |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 540 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz); |
| 541 | } |
| 542 | return res; |
| 543 | } |
| 544 | INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) { |
| 545 | void *ctx; |
| 546 | COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp); |
| 547 | int res = REAL(clock_gettime)(clk_id, tp); |
| 548 | if (!res) { |
| 549 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz); |
| 550 | } |
| 551 | return res; |
| 552 | } |
| 553 | INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) { |
| 554 | void *ctx; |
| 555 | COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp); |
| 556 | COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz); |
| 557 | return REAL(clock_settime)(clk_id, tp); |
| 558 | } |
| 559 | #define INIT_CLOCK_GETTIME \ |
| 560 | INTERCEPT_FUNCTION(clock_getres); \ |
| 561 | INTERCEPT_FUNCTION(clock_gettime); \ |
| 562 | INTERCEPT_FUNCTION(clock_settime); |
| 563 | #else |
| 564 | #define INIT_CLOCK_GETTIME |
| 565 | #endif |
| 566 | |
| 567 | |
| 568 | #if SANITIZER_INTERCEPT_GETITIMER |
| 569 | INTERCEPTOR(int, getitimer, int which, void *curr_value) { |
| 570 | void *ctx; |
| 571 | COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value); |
| 572 | int res = REAL(getitimer)(which, curr_value); |
| 573 | if (!res) { |
| 574 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz); |
| 575 | } |
| 576 | return res; |
| 577 | } |
| 578 | INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) { |
| 579 | void *ctx; |
| 580 | COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value); |
| 581 | COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz); |
| 582 | int res = REAL(setitimer)(which, new_value, old_value); |
| 583 | if (!res && old_value) { |
| 584 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz); |
| 585 | } |
| 586 | return res; |
| 587 | } |
| 588 | #define INIT_GETITIMER \ |
| 589 | INTERCEPT_FUNCTION(getitimer); \ |
| 590 | INTERCEPT_FUNCTION(setitimer); |
| 591 | #else |
| 592 | #define INIT_GETITIMER |
| 593 | #endif |
| 594 | |
| 595 | |
Evgeniy Stepanov | a1c2a55 | 2013-04-09 11:35:13 +0000 | [diff] [blame] | 596 | #if SANITIZER_INTERCEPT_GLOB |
| 597 | struct sanitizer_glob_t { |
| 598 | SIZE_T gl_pathc; |
| 599 | char **gl_pathv; |
| 600 | }; |
| 601 | |
| 602 | static void unpoison_glob_t(void *ctx, sanitizer_glob_t *pglob) { |
| 603 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob)); |
| 604 | // +1 for NULL pointer at the end. |
| 605 | COMMON_INTERCEPTOR_WRITE_RANGE( |
| 606 | ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv)); |
| 607 | for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) { |
| 608 | char *p = pglob->gl_pathv[i]; |
| 609 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | INTERCEPTOR(int, glob, const char *pattern, int flags, |
| 614 | int (*errfunc)(const char *epath, int eerrno), |
| 615 | sanitizer_glob_t *pglob) { |
| 616 | void *ctx; |
| 617 | COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob); |
| 618 | int res = REAL(glob)(pattern, flags, errfunc, pglob); |
| 619 | if (res == 0) |
| 620 | unpoison_glob_t(ctx, pglob); |
| 621 | return res; |
| 622 | } |
| 623 | |
| 624 | INTERCEPTOR(int, glob64, const char *pattern, int flags, |
| 625 | int (*errfunc)(const char *epath, int eerrno), |
| 626 | sanitizer_glob_t *pglob) { |
| 627 | void *ctx; |
| 628 | COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob); |
| 629 | int res = REAL(glob64)(pattern, flags, errfunc, pglob); |
| 630 | if (res == 0) |
| 631 | unpoison_glob_t(ctx, pglob); |
| 632 | return res; |
| 633 | } |
| 634 | #define INIT_GLOB \ |
| 635 | INTERCEPT_FUNCTION(glob); \ |
| 636 | INTERCEPT_FUNCTION(glob64); |
| 637 | #else // SANITIZER_INTERCEPT_GLOB |
| 638 | #define INIT_GLOB |
| 639 | #endif // SANITIZER_INTERCEPT_GLOB |
| 640 | |
| 641 | |
Evgeniy Stepanov | 897a4ae | 2013-04-09 14:34:59 +0000 | [diff] [blame] | 642 | #if SANITIZER_INTERCEPT_WAIT |
| 643 | INTERCEPTOR(int, wait, int *status) { |
| 644 | void *ctx; |
| 645 | COMMON_INTERCEPTOR_ENTER(ctx, wait, status); |
| 646 | int res = REAL(wait)(status); |
| 647 | if (res != -1) |
| 648 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status)); |
| 649 | return res; |
| 650 | } |
| 651 | INTERCEPTOR(int, waitid, int idtype, int id, void *infop, int options) { |
| 652 | void *ctx; |
| 653 | COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options); |
| 654 | int res = REAL(waitid)(idtype, id, infop, options); |
| 655 | if (res != -1) |
| 656 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz); |
| 657 | return res; |
| 658 | } |
| 659 | INTERCEPTOR(int, waitpid, int pid, int *status, int options) { |
| 660 | void *ctx; |
| 661 | COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options); |
| 662 | int res = REAL(waitpid)(pid, status, options); |
| 663 | if (res != -1) |
| 664 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status)); |
| 665 | return res; |
| 666 | } |
| 667 | INTERCEPTOR(int, wait3, int *status, int options, void *rusage) { |
| 668 | void *ctx; |
| 669 | COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage); |
| 670 | int res = REAL(wait3)(status, options, rusage); |
| 671 | if (res != -1) { |
| 672 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status)); |
| 673 | if (rusage) |
| 674 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz); |
| 675 | } |
| 676 | return res; |
| 677 | } |
| 678 | INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) { |
| 679 | void *ctx; |
| 680 | COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage); |
| 681 | int res = REAL(wait4)(pid, status, options, rusage); |
| 682 | if (res != -1) { |
| 683 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status)); |
| 684 | if (rusage) |
| 685 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz); |
| 686 | } |
| 687 | return res; |
| 688 | } |
| 689 | #define INIT_WAIT \ |
| 690 | INTERCEPT_FUNCTION(wait); \ |
| 691 | INTERCEPT_FUNCTION(waitid); \ |
| 692 | INTERCEPT_FUNCTION(waitpid); \ |
| 693 | INTERCEPT_FUNCTION(wait3); \ |
| 694 | INTERCEPT_FUNCTION(wait4); |
| 695 | #else |
| 696 | #define INIT_WAIT |
| 697 | #endif |
| 698 | |
Evgeniy Stepanov | 9530eb7 | 2013-04-23 14:05:15 +0000 | [diff] [blame^] | 699 | #if SANITIZER_INTERCEPT_INET |
| 700 | INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) { |
| 701 | void *ctx; |
| 702 | COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size); |
| 703 | uptr sz = __sanitizer_in_addr_sz(af); |
| 704 | if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz); |
| 705 | // FIXME: figure out read size based on the address family. |
| 706 | char *res = REAL(inet_ntop)(af, src, dst, size); |
| 707 | if (res) |
| 708 | COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1); |
| 709 | return res; |
| 710 | } |
| 711 | INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) { |
| 712 | void *ctx; |
| 713 | COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst); |
| 714 | // FIXME: figure out read size based on the address family. |
| 715 | int res = REAL(inet_pton)(af, src, dst); |
| 716 | if (res == 1) { |
| 717 | uptr sz = __sanitizer_in_addr_sz(af); |
| 718 | if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz); |
| 719 | } |
| 720 | return res; |
| 721 | } |
| 722 | #define INIT_INET \ |
| 723 | INTERCEPT_FUNCTION(inet_ntop); \ |
| 724 | INTERCEPT_FUNCTION(inet_pton); |
| 725 | #else |
| 726 | #define INIT_INET |
| 727 | #endif |
| 728 | |
Evgeniy Stepanov | 897a4ae | 2013-04-09 14:34:59 +0000 | [diff] [blame] | 729 | |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 730 | #define SANITIZER_COMMON_INTERCEPTORS_INIT \ |
Dmitry Vyukov | be52366 | 2013-03-26 12:40:23 +0000 | [diff] [blame] | 731 | INIT_STRCASECMP; \ |
| 732 | INIT_STRNCASECMP; \ |
Evgeniy Stepanov | 44be70b | 2013-02-11 15:22:34 +0000 | [diff] [blame] | 733 | INIT_READ; \ |
| 734 | INIT_PREAD; \ |
| 735 | INIT_PREAD64; \ |
| 736 | INIT_PRCTL; \ |
| 737 | INIT_WRITE; \ |
| 738 | INIT_PWRITE; \ |
| 739 | INIT_PWRITE64; \ |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 740 | INIT_LOCALTIME_AND_FRIENDS; \ |
Evgeniy Stepanov | 7cbbb29 | 2013-03-14 11:34:39 +0000 | [diff] [blame] | 741 | INIT_SCANF; \ |
Alexey Samsonov | ff5d1fc | 2013-03-15 14:02:21 +0000 | [diff] [blame] | 742 | INIT_FREXP; \ |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 743 | INIT_FREXPF_FREXPL; \ |
Evgeniy Stepanov | 103a63e | 2013-04-23 12:01:20 +0000 | [diff] [blame] | 744 | INIT_GETPWNAM_AND_FRIENDS; \ |
| 745 | INIT_GETPWNAM_R_AND_FRIENDS; \ |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 746 | INIT_CLOCK_GETTIME; \ |
Evgeniy Stepanov | fef6605 | 2013-04-08 08:25:22 +0000 | [diff] [blame] | 747 | INIT_GETITIMER; \ |
Evgeniy Stepanov | a1c2a55 | 2013-04-09 11:35:13 +0000 | [diff] [blame] | 748 | INIT_TIME; \ |
Evgeniy Stepanov | 897a4ae | 2013-04-09 14:34:59 +0000 | [diff] [blame] | 749 | INIT_GLOB; \ |
Evgeniy Stepanov | 9530eb7 | 2013-04-23 14:05:15 +0000 | [diff] [blame^] | 750 | INIT_WAIT; \ |
| 751 | INIT_INET; |