Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1 | //===-- msan_interceptors.cc ----------------------------------------------===// |
| 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 | // This file is a part of MemorySanitizer. |
| 11 | // |
| 12 | // Interceptors for standard library functions. |
Kostya Serebryany | 8530e2b | 2012-12-12 09:54:35 +0000 | [diff] [blame] | 13 | // |
| 14 | // FIXME: move as many interceptors as possible into |
| 15 | // sanitizer_common/sanitizer_common_interceptors.h |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "interception/interception.h" |
| 19 | #include "msan.h" |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_platform_limits_posix.h" |
Kostya Serebryany | 65199f1 | 2013-01-25 11:46:22 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_allocator.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_common.h" |
Evgeniy Stepanov | 887a5fe | 2013-01-28 13:52:49 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_libc.h" |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_linux.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 26 | |
| 27 | #include <stdarg.h> |
| 28 | // ACHTUNG! No other system header includes in this file. |
| 29 | // Ideally, we should get rid of stdarg.h as well. |
| 30 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 31 | using namespace __msan; |
| 32 | |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 33 | // True if this is a nested interceptor. |
| 34 | static THREADLOCAL int in_interceptor_scope; |
| 35 | |
| 36 | struct InterceptorScope { |
| 37 | InterceptorScope() { ++in_interceptor_scope; } |
| 38 | ~InterceptorScope() { --in_interceptor_scope; } |
| 39 | }; |
| 40 | |
| 41 | bool IsInInterceptorScope() { |
| 42 | return in_interceptor_scope; |
| 43 | } |
| 44 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 45 | #define ENSURE_MSAN_INITED() do { \ |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 46 | CHECK(!msan_init_is_running); \ |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 47 | if (!msan_inited) { \ |
| 48 | __msan_init(); \ |
| 49 | } \ |
| 50 | } while (0) |
| 51 | |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 52 | // Check that [x, x+n) range is unpoisoned. |
Evgeniy Stepanov | bb881c7 | 2013-06-21 12:37:58 +0000 | [diff] [blame] | 53 | #define CHECK_UNPOISONED_0(x, n) \ |
| 54 | do { \ |
| 55 | sptr offset = __msan_test_shadow(x, n); \ |
| 56 | if (__msan::IsInSymbolizer()) break; \ |
| 57 | if (offset >= 0 && __msan::flags()->report_umrs) { \ |
| 58 | GET_CALLER_PC_BP_SP; \ |
| 59 | (void) sp; \ |
| 60 | Printf("UMR in %s at offset %d inside [%p, +%d) \n", __FUNCTION__, \ |
| 61 | offset, x, n); \ |
| 62 | __msan::PrintWarningWithOrigin(pc, bp, \ |
| 63 | __msan_get_origin((char *)x + offset)); \ |
| 64 | if (!__msan::flags()->keep_going) { \ |
| 65 | Printf("Exiting\n"); \ |
| 66 | Die(); \ |
| 67 | } \ |
| 68 | } \ |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 69 | } while (0) |
| 70 | |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 71 | // Check that [x, x+n) range is unpoisoned unless we are in a nested |
| 72 | // interceptor. |
Evgeniy Stepanov | 341b9e6 | 2013-06-28 11:02:43 +0000 | [diff] [blame] | 73 | #define CHECK_UNPOISONED(x, n) \ |
| 74 | do { \ |
| 75 | if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \ |
| 76 | } while (0); |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 77 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 78 | static void *fast_memset(void *ptr, int c, SIZE_T n); |
| 79 | static void *fast_memcpy(void *dst, const void *src, SIZE_T n); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 80 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 81 | INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 82 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 83 | SIZE_T res = REAL(fread)(ptr, size, nmemb, file); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 84 | if (res > 0) |
| 85 | __msan_unpoison(ptr, res *size); |
| 86 | return res; |
| 87 | } |
| 88 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 89 | INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb, |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 90 | void *file) { |
| 91 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 92 | SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 93 | if (res > 0) |
| 94 | __msan_unpoison(ptr, res *size); |
| 95 | return res; |
| 96 | } |
| 97 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 98 | INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 99 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 100 | SSIZE_T res = REAL(readlink)(path, buf, bufsiz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 101 | if (res > 0) |
| 102 | __msan_unpoison(buf, res); |
| 103 | return res; |
| 104 | } |
| 105 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 106 | INTERCEPTOR(void *, memcpy, void *dest, const void *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 107 | return __msan_memcpy(dest, src, n); |
| 108 | } |
| 109 | |
Evgeniy Stepanov | 353c996 | 2013-07-02 14:49:24 +0000 | [diff] [blame] | 110 | INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) { |
| 111 | return (char *)__msan_memcpy(dest, src, n) + n; |
| 112 | } |
| 113 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 114 | INTERCEPTOR(void *, memmove, void *dest, const void *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 115 | return __msan_memmove(dest, src, n); |
| 116 | } |
| 117 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 118 | INTERCEPTOR(void *, memset, void *s, int c, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 119 | return __msan_memset(s, c, n); |
| 120 | } |
| 121 | |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame^] | 122 | INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) { |
| 123 | return __msan_memmove(dest, src, n); |
| 124 | } |
| 125 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 126 | INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 127 | GET_MALLOC_STACK_TRACE; |
| 128 | CHECK_EQ(alignment & (alignment - 1), 0); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 129 | CHECK_NE(memptr, 0); |
Evgeniy Stepanov | b6c8e47 | 2013-04-23 13:27:36 +0000 | [diff] [blame] | 130 | *memptr = MsanReallocate(&stack, 0, size, alignment, false); |
| 131 | CHECK_NE(*memptr, 0); |
| 132 | __msan_unpoison(memptr, sizeof(*memptr)); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | INTERCEPTOR(void, free, void *ptr) { |
| 137 | ENSURE_MSAN_INITED(); |
| 138 | if (ptr == 0) return; |
| 139 | MsanDeallocate(ptr); |
| 140 | } |
| 141 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 142 | INTERCEPTOR(SIZE_T, strlen, const char *s) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 143 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 144 | SIZE_T res = REAL(strlen)(s); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 145 | CHECK_UNPOISONED(s, res + 1); |
| 146 | return res; |
| 147 | } |
| 148 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 149 | INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 150 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 151 | SIZE_T res = REAL(strnlen)(s, n); |
| 152 | SIZE_T scan_size = (res == n) ? res : res + 1; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 153 | CHECK_UNPOISONED(s, scan_size); |
| 154 | return res; |
| 155 | } |
| 156 | |
| 157 | // FIXME: Add stricter shadow checks in str* interceptors (ex.: strcpy should |
| 158 | // check the shadow of the terminating \0 byte). |
| 159 | |
| 160 | INTERCEPTOR(char *, strcpy, char *dest, const char *src) { // NOLINT |
| 161 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 162 | SIZE_T n = REAL(strlen)(src); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 163 | char *res = REAL(strcpy)(dest, src); // NOLINT |
| 164 | __msan_copy_poison(dest, src, n + 1); |
| 165 | return res; |
| 166 | } |
| 167 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 168 | INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { // NOLINT |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 169 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 170 | SIZE_T copy_size = REAL(strnlen)(src, n); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 171 | if (copy_size < n) |
| 172 | copy_size++; // trailing \0 |
| 173 | char *res = REAL(strncpy)(dest, src, n); // NOLINT |
| 174 | __msan_copy_poison(dest, src, copy_size); |
| 175 | return res; |
| 176 | } |
| 177 | |
Evgeniy Stepanov | 4bbbe13 | 2013-07-01 15:19:37 +0000 | [diff] [blame] | 178 | INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { // NOLINT |
| 179 | ENSURE_MSAN_INITED(); |
| 180 | SIZE_T n = REAL(strlen)(src); |
| 181 | char *res = REAL(stpcpy)(dest, src); // NOLINT |
| 182 | __msan_copy_poison(dest, src, n + 1); |
| 183 | return res; |
| 184 | } |
| 185 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 186 | INTERCEPTOR(char *, strdup, char *src) { |
| 187 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 188 | SIZE_T n = REAL(strlen)(src); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 189 | char *res = REAL(strdup)(src); |
| 190 | __msan_copy_poison(res, src, n + 1); |
| 191 | return res; |
| 192 | } |
| 193 | |
Evgeniy Stepanov | 8aa1ae0 | 2013-03-14 11:10:36 +0000 | [diff] [blame] | 194 | INTERCEPTOR(char *, __strdup, char *src) { |
| 195 | ENSURE_MSAN_INITED(); |
| 196 | SIZE_T n = REAL(strlen)(src); |
| 197 | char *res = REAL(__strdup)(src); |
| 198 | __msan_copy_poison(res, src, n + 1); |
| 199 | return res; |
| 200 | } |
| 201 | |
| 202 | INTERCEPTOR(char *, strndup, char *src, SIZE_T n) { |
| 203 | ENSURE_MSAN_INITED(); |
| 204 | SIZE_T copy_size = REAL(strnlen)(src, n); |
| 205 | char *res = REAL(strndup)(src, n); |
| 206 | __msan_copy_poison(res, src, copy_size); |
| 207 | __msan_unpoison(res + copy_size, 1); // \0 |
| 208 | return res; |
| 209 | } |
| 210 | |
| 211 | INTERCEPTOR(char *, __strndup, char *src, SIZE_T n) { |
| 212 | ENSURE_MSAN_INITED(); |
| 213 | SIZE_T copy_size = REAL(strnlen)(src, n); |
| 214 | char *res = REAL(__strndup)(src, n); |
| 215 | __msan_copy_poison(res, src, copy_size); |
| 216 | __msan_unpoison(res + copy_size, 1); // \0 |
| 217 | return res; |
| 218 | } |
| 219 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 220 | INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 221 | ENSURE_MSAN_INITED(); |
| 222 | char *res = REAL(gcvt)(number, ndigit, buf); |
| 223 | // DynamoRio tool will take care of unpoisoning gcvt result for us. |
| 224 | if (!__msan_has_dynamic_component()) { |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 225 | SIZE_T n = REAL(strlen)(buf); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 226 | __msan_unpoison(buf, n + 1); |
| 227 | } |
| 228 | return res; |
| 229 | } |
| 230 | |
| 231 | INTERCEPTOR(char *, strcat, char *dest, const char *src) { // NOLINT |
| 232 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 233 | SIZE_T src_size = REAL(strlen)(src); |
| 234 | SIZE_T dest_size = REAL(strlen)(dest); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 235 | char *res = REAL(strcat)(dest, src); // NOLINT |
| 236 | __msan_copy_poison(dest + dest_size, src, src_size + 1); |
| 237 | return res; |
| 238 | } |
| 239 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 240 | INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { // NOLINT |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 241 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 242 | SIZE_T dest_size = REAL(strlen)(dest); |
| 243 | SIZE_T copy_size = REAL(strlen)(src); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 244 | if (copy_size < n) |
| 245 | copy_size++; // trailing \0 |
| 246 | char *res = REAL(strncat)(dest, src, n); // NOLINT |
| 247 | __msan_copy_poison(dest + dest_size, src, copy_size); |
| 248 | return res; |
| 249 | } |
| 250 | |
| 251 | INTERCEPTOR(long, strtol, const char *nptr, char **endptr, // NOLINT |
| 252 | int base) { |
| 253 | ENSURE_MSAN_INITED(); |
| 254 | long res = REAL(strtol)(nptr, endptr, base); // NOLINT |
| 255 | if (!__msan_has_dynamic_component()) { |
| 256 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 257 | } |
| 258 | return res; |
| 259 | } |
| 260 | |
| 261 | INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, // NOLINT |
| 262 | int base) { |
| 263 | ENSURE_MSAN_INITED(); |
| 264 | long res = REAL(strtoll)(nptr, endptr, base); //NOLINT |
| 265 | if (!__msan_has_dynamic_component()) { |
| 266 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 267 | } |
| 268 | return res; |
| 269 | } |
| 270 | |
| 271 | INTERCEPTOR(unsigned long, strtoul, const char *nptr, char **endptr, // NOLINT |
| 272 | int base) { |
| 273 | ENSURE_MSAN_INITED(); |
| 274 | unsigned long res = REAL(strtoul)(nptr, endptr, base); // NOLINT |
| 275 | if (!__msan_has_dynamic_component()) { |
| 276 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 277 | } |
| 278 | return res; |
| 279 | } |
| 280 | |
| 281 | INTERCEPTOR(unsigned long long, strtoull, const char *nptr, // NOLINT |
| 282 | char **endptr, int base) { |
| 283 | ENSURE_MSAN_INITED(); |
| 284 | unsigned long res = REAL(strtoull)(nptr, endptr, base); // NOLINT |
| 285 | if (!__msan_has_dynamic_component()) { |
| 286 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 287 | } |
| 288 | return res; |
| 289 | } |
| 290 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 291 | INTERCEPTOR(double, strtod, const char *nptr, char **endptr) { // NOLINT |
| 292 | ENSURE_MSAN_INITED(); |
| 293 | double res = REAL(strtod)(nptr, endptr); // NOLINT |
| 294 | if (!__msan_has_dynamic_component()) { |
| 295 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 296 | } |
| 297 | return res; |
| 298 | } |
| 299 | |
| 300 | INTERCEPTOR(float, strtof, const char *nptr, char **endptr) { // NOLINT |
| 301 | ENSURE_MSAN_INITED(); |
| 302 | float res = REAL(strtof)(nptr, endptr); // NOLINT |
| 303 | if (!__msan_has_dynamic_component()) { |
| 304 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 305 | } |
| 306 | return res; |
| 307 | } |
| 308 | |
| 309 | INTERCEPTOR(long double, strtold, const char *nptr, char **endptr) { // NOLINT |
| 310 | ENSURE_MSAN_INITED(); |
| 311 | long double res = REAL(strtold)(nptr, endptr); // NOLINT |
| 312 | if (!__msan_has_dynamic_component()) { |
| 313 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 314 | } |
| 315 | return res; |
| 316 | } |
| 317 | |
Evgeniy Stepanov | 0797ed4 | 2013-06-21 13:32:26 +0000 | [diff] [blame] | 318 | INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap) { |
| 319 | ENSURE_MSAN_INITED(); |
| 320 | int res = REAL(vasprintf)(strp, format, ap); |
| 321 | if (res >= 0 && !__msan_has_dynamic_component()) { |
| 322 | __msan_unpoison(strp, sizeof(*strp)); |
| 323 | __msan_unpoison(*strp, res + 1); |
| 324 | } |
| 325 | return res; |
| 326 | } |
| 327 | |
| 328 | INTERCEPTOR(int, asprintf, char **strp, const char *format, ...) { // NOLINT |
| 329 | ENSURE_MSAN_INITED(); |
| 330 | va_list ap; |
| 331 | va_start(ap, format); |
| 332 | int res = vasprintf(strp, format, ap); // NOLINT |
| 333 | va_end(ap); |
| 334 | return res; |
| 335 | } |
| 336 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 337 | INTERCEPTOR(int, vsnprintf, char *str, uptr size, |
| 338 | const char *format, va_list ap) { |
| 339 | ENSURE_MSAN_INITED(); |
| 340 | int res = REAL(vsnprintf)(str, size, format, ap); |
Evgeniy Stepanov | 0797ed4 | 2013-06-21 13:32:26 +0000 | [diff] [blame] | 341 | if (res >= 0 && !__msan_has_dynamic_component()) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 342 | __msan_unpoison(str, res + 1); |
| 343 | } |
| 344 | return res; |
| 345 | } |
| 346 | |
| 347 | INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap) { |
| 348 | ENSURE_MSAN_INITED(); |
| 349 | int res = REAL(vsprintf)(str, format, ap); |
Evgeniy Stepanov | 0797ed4 | 2013-06-21 13:32:26 +0000 | [diff] [blame] | 350 | if (res >= 0 && !__msan_has_dynamic_component()) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 351 | __msan_unpoison(str, res + 1); |
| 352 | } |
| 353 | return res; |
| 354 | } |
| 355 | |
| 356 | INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) { |
| 357 | ENSURE_MSAN_INITED(); |
| 358 | int res = REAL(vswprintf)(str, size, format, ap); |
Evgeniy Stepanov | 0797ed4 | 2013-06-21 13:32:26 +0000 | [diff] [blame] | 359 | if (res >= 0 && !__msan_has_dynamic_component()) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 360 | __msan_unpoison(str, 4 * (res + 1)); |
| 361 | } |
| 362 | return res; |
| 363 | } |
| 364 | |
| 365 | INTERCEPTOR(int, sprintf, char *str, const char *format, ...) { // NOLINT |
| 366 | ENSURE_MSAN_INITED(); |
| 367 | va_list ap; |
| 368 | va_start(ap, format); |
| 369 | int res = vsprintf(str, format, ap); // NOLINT |
| 370 | va_end(ap); |
| 371 | return res; |
| 372 | } |
| 373 | |
| 374 | INTERCEPTOR(int, snprintf, char *str, uptr size, const char *format, ...) { |
| 375 | ENSURE_MSAN_INITED(); |
| 376 | va_list ap; |
| 377 | va_start(ap, format); |
| 378 | int res = vsnprintf(str, size, format, ap); |
| 379 | va_end(ap); |
| 380 | return res; |
| 381 | } |
| 382 | |
| 383 | INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) { |
| 384 | ENSURE_MSAN_INITED(); |
| 385 | va_list ap; |
| 386 | va_start(ap, format); |
| 387 | int res = vswprintf(str, size, format, ap); |
| 388 | va_end(ap); |
| 389 | return res; |
| 390 | } |
| 391 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 392 | // SIZE_T strftime(char *s, SIZE_T max, const char *format,const struct tm *tm); |
| 393 | INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format, |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 394 | void *tm) { |
| 395 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 396 | SIZE_T res = REAL(strftime)(s, max, format, tm); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 397 | if (res) __msan_unpoison(s, res + 1); |
| 398 | return res; |
| 399 | } |
| 400 | |
Evgeniy Stepanov | 8014489 | 2013-07-02 13:34:44 +0000 | [diff] [blame] | 401 | INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) { |
| 402 | ENSURE_MSAN_INITED(); |
| 403 | int res = REAL(mbtowc)(dest, src, n); |
| 404 | if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t)); |
| 405 | return res; |
| 406 | } |
| 407 | |
| 408 | INTERCEPTOR(int, mbrtowc, wchar_t *dest, const char *src, SIZE_T n, void *ps) { |
| 409 | ENSURE_MSAN_INITED(); |
| 410 | SIZE_T res = REAL(mbrtowc)(dest, src, n, ps); |
| 411 | if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t)); |
| 412 | return res; |
| 413 | } |
| 414 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 415 | INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 416 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 417 | SIZE_T res = REAL(wcslen)(s); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 418 | CHECK_UNPOISONED(s, sizeof(wchar_t) * (res + 1)); |
| 419 | return res; |
| 420 | } |
| 421 | |
| 422 | // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc); |
| 423 | INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) { |
| 424 | ENSURE_MSAN_INITED(); |
| 425 | wchar_t *res = REAL(wcschr)(s, wc, ps); |
| 426 | return res; |
| 427 | } |
| 428 | |
| 429 | // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src); |
| 430 | INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) { |
| 431 | ENSURE_MSAN_INITED(); |
| 432 | wchar_t *res = REAL(wcscpy)(dest, src); |
| 433 | __msan_copy_poison(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1)); |
| 434 | return res; |
| 435 | } |
| 436 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 437 | // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n); |
| 438 | INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 439 | ENSURE_MSAN_INITED(); |
| 440 | wchar_t *res = REAL(wmemcpy)(dest, src, n); |
| 441 | __msan_copy_poison(dest, src, n * sizeof(wchar_t)); |
| 442 | return res; |
| 443 | } |
| 444 | |
Evgeniy Stepanov | 353c996 | 2013-07-02 14:49:24 +0000 | [diff] [blame] | 445 | INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) { |
| 446 | ENSURE_MSAN_INITED(); |
| 447 | wchar_t *res = REAL(wmempcpy)(dest, src, n); |
| 448 | __msan_copy_poison(dest, src, n * sizeof(wchar_t)); |
| 449 | return res; |
| 450 | } |
| 451 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 452 | INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 453 | CHECK(MEM_IS_APP(s)); |
| 454 | ENSURE_MSAN_INITED(); |
| 455 | wchar_t *res = (wchar_t *)fast_memset(s, c, n * sizeof(wchar_t)); |
| 456 | __msan_unpoison(s, n * sizeof(wchar_t)); |
| 457 | return res; |
| 458 | } |
| 459 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 460 | INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 461 | ENSURE_MSAN_INITED(); |
| 462 | wchar_t *res = REAL(wmemmove)(dest, src, n); |
| 463 | __msan_move_poison(dest, src, n * sizeof(wchar_t)); |
| 464 | return res; |
| 465 | } |
| 466 | |
| 467 | INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) { |
| 468 | ENSURE_MSAN_INITED(); |
| 469 | int res = REAL(wcscmp)(s1, s2); |
| 470 | return res; |
| 471 | } |
| 472 | |
| 473 | INTERCEPTOR(double, wcstod, const wchar_t *nptr, wchar_t **endptr) { |
| 474 | ENSURE_MSAN_INITED(); |
| 475 | double res = REAL(wcstod)(nptr, endptr); |
| 476 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 477 | return res; |
| 478 | } |
| 479 | |
| 480 | // #define UNSUPPORTED(name) \ |
| 481 | // INTERCEPTOR(void, name, void) { \ |
| 482 | // Printf("MSAN: Unsupported %s\n", __FUNCTION__); \ |
| 483 | // Die(); \ |
| 484 | // } |
| 485 | |
| 486 | // FIXME: intercept the following functions: |
| 487 | // Note, they only matter when running without a dynamic tool. |
| 488 | // UNSUPPORTED(wcscoll_l) |
| 489 | // UNSUPPORTED(wcsnrtombs) |
| 490 | // UNSUPPORTED(wcstol) |
| 491 | // UNSUPPORTED(wcstoll) |
| 492 | // UNSUPPORTED(wcstold) |
| 493 | // UNSUPPORTED(wcstoul) |
| 494 | // UNSUPPORTED(wcstoull) |
| 495 | // UNSUPPORTED(wcsxfrm_l) |
| 496 | // UNSUPPORTED(wcsdup) |
| 497 | // UNSUPPORTED(wcsftime) |
| 498 | // UNSUPPORTED(wcsstr) |
| 499 | // UNSUPPORTED(wcsrchr) |
| 500 | // UNSUPPORTED(wctob) |
| 501 | |
| 502 | INTERCEPTOR(int, gettimeofday, void *tv, void *tz) { |
| 503 | ENSURE_MSAN_INITED(); |
| 504 | int res = REAL(gettimeofday)(tv, tz); |
| 505 | if (tv) |
| 506 | __msan_unpoison(tv, 16); |
| 507 | if (tz) |
| 508 | __msan_unpoison(tz, 8); |
| 509 | return res; |
| 510 | } |
| 511 | |
| 512 | INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) { |
| 513 | ENSURE_MSAN_INITED(); |
| 514 | char *res = REAL(fcvt)(x, a, b, c); |
| 515 | if (!__msan_has_dynamic_component()) { |
| 516 | __msan_unpoison(b, sizeof(*b)); |
| 517 | __msan_unpoison(c, sizeof(*c)); |
| 518 | } |
| 519 | return res; |
| 520 | } |
| 521 | |
| 522 | INTERCEPTOR(char *, getenv, char *name) { |
| 523 | ENSURE_MSAN_INITED(); |
| 524 | char *res = REAL(getenv)(name); |
| 525 | if (!__msan_has_dynamic_component()) { |
| 526 | if (res) |
| 527 | __msan_unpoison(res, REAL(strlen)(res) + 1); |
| 528 | } |
| 529 | return res; |
| 530 | } |
| 531 | |
| 532 | INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) { |
| 533 | ENSURE_MSAN_INITED(); |
| 534 | int res = REAL(__fxstat)(magic, fd, buf); |
| 535 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 536 | __msan_unpoison(buf, __sanitizer::struct_stat_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 537 | return res; |
| 538 | } |
| 539 | |
| 540 | INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) { |
| 541 | ENSURE_MSAN_INITED(); |
| 542 | int res = REAL(__fxstat64)(magic, fd, buf); |
| 543 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 544 | __msan_unpoison(buf, __sanitizer::struct_stat64_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 545 | return res; |
| 546 | } |
| 547 | |
| 548 | INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) { |
| 549 | ENSURE_MSAN_INITED(); |
| 550 | int res = REAL(__xstat)(magic, path, buf); |
| 551 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 552 | __msan_unpoison(buf, __sanitizer::struct_stat_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 553 | return res; |
| 554 | } |
| 555 | |
| 556 | INTERCEPTOR(int, __xstat64, int magic, char *path, void *buf) { |
| 557 | ENSURE_MSAN_INITED(); |
| 558 | int res = REAL(__xstat64)(magic, path, buf); |
| 559 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 560 | __msan_unpoison(buf, __sanitizer::struct_stat64_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 561 | return res; |
| 562 | } |
| 563 | |
| 564 | INTERCEPTOR(int, __lxstat, int magic, char *path, void *buf) { |
| 565 | ENSURE_MSAN_INITED(); |
| 566 | int res = REAL(__lxstat)(magic, path, buf); |
| 567 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 568 | __msan_unpoison(buf, __sanitizer::struct_stat_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 569 | return res; |
| 570 | } |
| 571 | |
| 572 | INTERCEPTOR(int, __lxstat64, int magic, char *path, void *buf) { |
| 573 | ENSURE_MSAN_INITED(); |
| 574 | int res = REAL(__lxstat64)(magic, path, buf); |
| 575 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 576 | __msan_unpoison(buf, __sanitizer::struct_stat64_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 577 | return res; |
| 578 | } |
| 579 | |
| 580 | INTERCEPTOR(int, pipe, int pipefd[2]) { |
| 581 | if (msan_init_is_running) |
| 582 | return REAL(pipe)(pipefd); |
| 583 | ENSURE_MSAN_INITED(); |
| 584 | int res = REAL(pipe)(pipefd); |
| 585 | if (!res) |
| 586 | __msan_unpoison(pipefd, sizeof(int[2])); |
| 587 | return res; |
| 588 | } |
| 589 | |
Evgeniy Stepanov | 134fe8a | 2013-04-08 13:45:12 +0000 | [diff] [blame] | 590 | INTERCEPTOR(int, pipe2, int pipefd[2], int flags) { |
| 591 | ENSURE_MSAN_INITED(); |
| 592 | int res = REAL(pipe2)(pipefd, flags); |
| 593 | if (!res) |
| 594 | __msan_unpoison(pipefd, sizeof(int[2])); |
| 595 | return res; |
| 596 | } |
| 597 | |
| 598 | INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) { |
| 599 | ENSURE_MSAN_INITED(); |
| 600 | int res = REAL(socketpair)(domain, type, protocol, sv); |
| 601 | if (!res) |
| 602 | __msan_unpoison(sv, sizeof(int[2])); |
| 603 | return res; |
| 604 | } |
| 605 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 606 | INTERCEPTOR(char *, fgets, char *s, int size, void *stream) { |
| 607 | ENSURE_MSAN_INITED(); |
| 608 | char *res = REAL(fgets)(s, size, stream); |
| 609 | if (res) |
| 610 | __msan_unpoison(s, REAL(strlen)(s) + 1); |
| 611 | return res; |
| 612 | } |
| 613 | |
| 614 | INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) { |
| 615 | ENSURE_MSAN_INITED(); |
| 616 | char *res = REAL(fgets_unlocked)(s, size, stream); |
| 617 | if (res) |
| 618 | __msan_unpoison(s, REAL(strlen)(s) + 1); |
| 619 | return res; |
| 620 | } |
| 621 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 622 | INTERCEPTOR(char *, realpath, char *path, char *abspath) { |
| 623 | ENSURE_MSAN_INITED(); |
| 624 | char *res = REAL(realpath)(path, abspath); |
| 625 | if (res) |
| 626 | __msan_unpoison(abspath, REAL(strlen)(abspath) + 1); |
| 627 | return res; |
| 628 | } |
| 629 | |
| 630 | INTERCEPTOR(int, getrlimit, int resource, void *rlim) { |
| 631 | if (msan_init_is_running) |
| 632 | return REAL(getrlimit)(resource, rlim); |
| 633 | ENSURE_MSAN_INITED(); |
| 634 | int res = REAL(getrlimit)(resource, rlim); |
| 635 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 636 | __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 637 | return res; |
| 638 | } |
| 639 | |
| 640 | INTERCEPTOR(int, getrlimit64, int resource, void *rlim) { |
| 641 | if (msan_init_is_running) |
| 642 | return REAL(getrlimit64)(resource, rlim); |
| 643 | ENSURE_MSAN_INITED(); |
| 644 | int res = REAL(getrlimit64)(resource, rlim); |
| 645 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 646 | __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 647 | return res; |
| 648 | } |
| 649 | |
| 650 | INTERCEPTOR(int, statfs, const char *s, void *buf) { |
| 651 | ENSURE_MSAN_INITED(); |
| 652 | int res = REAL(statfs)(s, buf); |
| 653 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 654 | __msan_unpoison(buf, __sanitizer::struct_statfs_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 655 | return res; |
| 656 | } |
| 657 | |
| 658 | INTERCEPTOR(int, fstatfs, int fd, void *buf) { |
| 659 | ENSURE_MSAN_INITED(); |
| 660 | int res = REAL(fstatfs)(fd, buf); |
| 661 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 662 | __msan_unpoison(buf, __sanitizer::struct_statfs_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 663 | return res; |
| 664 | } |
| 665 | |
| 666 | INTERCEPTOR(int, statfs64, const char *s, void *buf) { |
| 667 | ENSURE_MSAN_INITED(); |
| 668 | int res = REAL(statfs64)(s, buf); |
| 669 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 670 | __msan_unpoison(buf, __sanitizer::struct_statfs64_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 671 | return res; |
| 672 | } |
| 673 | |
| 674 | INTERCEPTOR(int, fstatfs64, int fd, void *buf) { |
| 675 | ENSURE_MSAN_INITED(); |
| 676 | int res = REAL(fstatfs64)(fd, buf); |
| 677 | if (!res) |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 678 | __msan_unpoison(buf, __sanitizer::struct_statfs64_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 679 | return res; |
| 680 | } |
| 681 | |
| 682 | INTERCEPTOR(int, uname, void *utsname) { |
| 683 | ENSURE_MSAN_INITED(); |
| 684 | int res = REAL(uname)(utsname); |
| 685 | if (!res) { |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 686 | __msan_unpoison(utsname, __sanitizer::struct_utsname_sz); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 687 | } |
| 688 | return res; |
| 689 | } |
| 690 | |
Evgeniy Stepanov | 95d0588 | 2013-01-23 10:43:38 +0000 | [diff] [blame] | 691 | INTERCEPTOR(int, gethostname, char *name, SIZE_T len) { |
| 692 | ENSURE_MSAN_INITED(); |
| 693 | int res = REAL(gethostname)(name, len); |
| 694 | if (!res) { |
| 695 | SIZE_T real_len = REAL(strnlen)(name, len); |
| 696 | if (real_len < len) |
| 697 | ++real_len; |
| 698 | __msan_unpoison(name, real_len); |
| 699 | } |
| 700 | return res; |
| 701 | } |
| 702 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 703 | INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents, |
| 704 | int timeout) { |
| 705 | ENSURE_MSAN_INITED(); |
| 706 | int res = REAL(epoll_wait)(epfd, events, maxevents, timeout); |
| 707 | if (res > 0) { |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 708 | __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 709 | } |
| 710 | return res; |
| 711 | } |
| 712 | |
| 713 | INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents, |
| 714 | int timeout, void *sigmask) { |
| 715 | ENSURE_MSAN_INITED(); |
| 716 | int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask); |
| 717 | if (res > 0) { |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 718 | __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 719 | } |
| 720 | return res; |
| 721 | } |
| 722 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 723 | INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 724 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 725 | SSIZE_T res = REAL(recv)(fd, buf, len, flags); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 726 | if (res > 0) |
| 727 | __msan_unpoison(buf, res); |
| 728 | return res; |
| 729 | } |
| 730 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 731 | INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags, |
Evgeniy Stepanov | 9666d89 | 2013-05-29 11:30:00 +0000 | [diff] [blame] | 732 | void *srcaddr, int *addrlen) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 733 | ENSURE_MSAN_INITED(); |
Evgeniy Stepanov | 7b719df | 2012-12-25 16:51:57 +0000 | [diff] [blame] | 734 | SIZE_T srcaddr_sz; |
Evgeniy Stepanov | 9666d89 | 2013-05-29 11:30:00 +0000 | [diff] [blame] | 735 | if (srcaddr) srcaddr_sz = *addrlen; |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 736 | SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen); |
Evgeniy Stepanov | 7b719df | 2012-12-25 16:51:57 +0000 | [diff] [blame] | 737 | if (res > 0) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 738 | __msan_unpoison(buf, res); |
Evgeniy Stepanov | 7b719df | 2012-12-25 16:51:57 +0000 | [diff] [blame] | 739 | if (srcaddr) { |
Evgeniy Stepanov | 9666d89 | 2013-05-29 11:30:00 +0000 | [diff] [blame] | 740 | SIZE_T sz = *addrlen; |
Evgeniy Stepanov | 7b719df | 2012-12-25 16:51:57 +0000 | [diff] [blame] | 741 | __msan_unpoison(srcaddr, (sz < srcaddr_sz) ? sz : srcaddr_sz); |
| 742 | } |
| 743 | } |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 744 | return res; |
| 745 | } |
| 746 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 747 | INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) { |
Kostya Serebryany | 65199f1 | 2013-01-25 11:46:22 +0000 | [diff] [blame] | 748 | if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 749 | GET_MALLOC_STACK_TRACE; |
| 750 | if (!msan_inited) { |
| 751 | // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym. |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 752 | const SIZE_T kCallocPoolSize = 1024; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 753 | static uptr calloc_memory_for_dlsym[kCallocPoolSize]; |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 754 | static SIZE_T allocated; |
| 755 | SIZE_T size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 756 | void *mem = (void*)&calloc_memory_for_dlsym[allocated]; |
| 757 | allocated += size_in_words; |
| 758 | CHECK(allocated < kCallocPoolSize); |
| 759 | return mem; |
| 760 | } |
| 761 | return MsanReallocate(&stack, 0, nmemb * size, sizeof(u64), true); |
| 762 | } |
| 763 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 764 | INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 765 | GET_MALLOC_STACK_TRACE; |
| 766 | return MsanReallocate(&stack, ptr, size, sizeof(u64), false); |
| 767 | } |
| 768 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 769 | INTERCEPTOR(void *, malloc, SIZE_T size) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 770 | GET_MALLOC_STACK_TRACE; |
| 771 | return MsanReallocate(&stack, 0, size, sizeof(u64), false); |
| 772 | } |
| 773 | |
Alexey Samsonov | 11347bf | 2013-04-23 13:34:19 +0000 | [diff] [blame] | 774 | void __msan_allocated_memory(const void* data, uptr size) { |
Evgeniy Stepanov | 887a5fe | 2013-01-28 13:52:49 +0000 | [diff] [blame] | 775 | GET_MALLOC_STACK_TRACE; |
| 776 | if (flags()->poison_in_malloc) |
| 777 | __msan_poison(data, size); |
| 778 | if (__msan_get_track_origins()) { |
| 779 | u32 stack_id = StackDepotPut(stack.trace, stack.size); |
| 780 | CHECK(stack_id); |
| 781 | CHECK_EQ((stack_id >> 31), 0); // Higher bit is occupied by stack origins. |
| 782 | __msan_set_origin(data, size, stack_id); |
| 783 | } |
| 784 | } |
| 785 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 786 | INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags, |
| 787 | int fd, OFF_T offset) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 788 | ENSURE_MSAN_INITED(); |
| 789 | void *res = REAL(mmap)(addr, length, prot, flags, fd, offset); |
| 790 | if (res != (void*)-1) |
| 791 | __msan_unpoison(res, RoundUpTo(length, GetPageSize())); |
| 792 | return res; |
| 793 | } |
| 794 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 795 | INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags, |
| 796 | int fd, OFF64_T offset) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 797 | ENSURE_MSAN_INITED(); |
| 798 | void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset); |
| 799 | if (res != (void*)-1) |
| 800 | __msan_unpoison(res, RoundUpTo(length, GetPageSize())); |
| 801 | return res; |
| 802 | } |
| 803 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 804 | struct dlinfo { |
| 805 | char *dli_fname; |
| 806 | void *dli_fbase; |
| 807 | char *dli_sname; |
| 808 | void *dli_saddr; |
| 809 | }; |
| 810 | |
| 811 | INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) { |
| 812 | ENSURE_MSAN_INITED(); |
| 813 | int res = REAL(dladdr)(addr, info); |
| 814 | if (res != 0) { |
| 815 | __msan_unpoison(info, sizeof(*info)); |
| 816 | if (info->dli_fname) |
| 817 | __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1); |
| 818 | if (info->dli_sname) |
| 819 | __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1); |
| 820 | } |
| 821 | return res; |
| 822 | } |
| 823 | |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 824 | // dlopen() ultimately calls mmap() down inside the loader, which generally |
| 825 | // doesn't participate in dynamic symbol resolution. Therefore we won't |
| 826 | // intercept its calls to mmap, and we have to hook it here. The loader |
| 827 | // initializes the module before returning, so without the dynamic component, we |
| 828 | // won't be able to clear the shadow before the initializers. Fixing this would |
| 829 | // require putting our own initializer first to clear the shadow. |
| 830 | INTERCEPTOR(void *, dlopen, const char *filename, int flag) { |
| 831 | ENSURE_MSAN_INITED(); |
| 832 | EnterLoader(); |
| 833 | link_map *map = (link_map *)REAL(dlopen)(filename, flag); |
| 834 | ExitLoader(); |
Evgeniy Stepanov | 6c503b9 | 2013-03-22 11:59:49 +0000 | [diff] [blame] | 835 | if (!__msan_has_dynamic_component() && map) { |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 836 | // If msandr didn't clear the shadow before the initializers ran, we do it |
| 837 | // ourselves afterwards. |
| 838 | UnpoisonMappedDSO(map); |
| 839 | } |
| 840 | return (void *)map; |
| 841 | } |
| 842 | |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 843 | typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size, |
| 844 | void *data); |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 845 | struct dl_iterate_phdr_data { |
| 846 | dl_iterate_phdr_cb callback; |
| 847 | void *data; |
| 848 | }; |
| 849 | |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 850 | static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size, |
| 851 | void *data) { |
| 852 | if (info) { |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 853 | __msan_unpoison(info, size); |
Evgeniy Stepanov | 1aad6b5 | 2013-05-17 12:51:13 +0000 | [diff] [blame] | 854 | if (info->dlpi_name) |
| 855 | __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1); |
| 856 | } |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 857 | dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data; |
Alexey Samsonov | c2918bf | 2013-06-27 07:50:56 +0000 | [diff] [blame] | 858 | UnpoisonParam(3); |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 859 | return cbdata->callback(info, size, cbdata->data); |
| 860 | } |
| 861 | |
| 862 | INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) { |
| 863 | ENSURE_MSAN_INITED(); |
| 864 | EnterLoader(); |
| 865 | dl_iterate_phdr_data cbdata; |
| 866 | cbdata.callback = callback; |
| 867 | cbdata.data = data; |
| 868 | int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata); |
| 869 | ExitLoader(); |
| 870 | return res; |
| 871 | } |
| 872 | |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 873 | INTERCEPTOR(int, getrusage, int who, void *usage) { |
| 874 | ENSURE_MSAN_INITED(); |
| 875 | int res = REAL(getrusage)(who, usage); |
| 876 | if (res == 0) { |
Evgeniy Stepanov | 9358c58 | 2013-02-19 09:19:16 +0000 | [diff] [blame] | 877 | __msan_unpoison(usage, __sanitizer::struct_rusage_sz); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 878 | } |
| 879 | return res; |
| 880 | } |
| 881 | |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 882 | const int kMaxSignals = 1024; |
| 883 | static uptr sigactions[kMaxSignals]; |
| 884 | static StaticSpinMutex sigactions_mu; |
| 885 | |
| 886 | static void SignalHandler(int signo) { |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 887 | typedef void (*signal_cb)(int x); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 888 | signal_cb cb = (signal_cb)sigactions[signo]; |
| 889 | cb(signo); |
| 890 | } |
| 891 | |
| 892 | static void SignalAction(int signo, void *si, void *uc) { |
Alexey Samsonov | c2918bf | 2013-06-27 07:50:56 +0000 | [diff] [blame] | 893 | UnpoisonParam(3); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 894 | __msan_unpoison(si, __sanitizer::struct_sigaction_sz); |
Evgeniy Stepanov | 0727702 | 2013-04-04 09:03:56 +0000 | [diff] [blame] | 895 | __msan_unpoison(uc, __sanitizer::ucontext_t_sz); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 896 | |
| 897 | typedef void (*sigaction_cb)(int, void *, void *); |
| 898 | sigaction_cb cb = (sigaction_cb)sigactions[signo]; |
| 899 | cb(signo, si, uc); |
| 900 | } |
| 901 | |
| 902 | INTERCEPTOR(int, sigaction, int signo, const __sanitizer_sigaction *act, |
| 903 | __sanitizer_sigaction *oldact) { |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 904 | ENSURE_MSAN_INITED(); |
Kostya Serebryany | 583025d | 2013-04-04 06:52:40 +0000 | [diff] [blame] | 905 | // FIXME: check that *act is unpoisoned. |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 906 | // That requires intercepting all of sigemptyset, sigfillset, etc. |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 907 | int res; |
| 908 | if (flags()->wrap_signals) { |
| 909 | SpinMutexLock lock(&sigactions_mu); |
| 910 | CHECK_LT(signo, kMaxSignals); |
| 911 | uptr old_cb = sigactions[signo]; |
| 912 | __sanitizer_sigaction new_act; |
| 913 | __sanitizer_sigaction *pnew_act = act ? &new_act : 0; |
| 914 | if (act) { |
| 915 | internal_memcpy(pnew_act, act, __sanitizer::struct_sigaction_sz); |
| 916 | uptr cb = __sanitizer::__sanitizer_get_sigaction_sa_sigaction(pnew_act); |
| 917 | uptr new_cb = |
| 918 | __sanitizer::__sanitizer_get_sigaction_sa_siginfo(pnew_act) ? |
| 919 | (uptr)SignalAction : (uptr)SignalHandler; |
| 920 | if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { |
| 921 | sigactions[signo] = cb; |
| 922 | __sanitizer::__sanitizer_set_sigaction_sa_sigaction(pnew_act, new_cb); |
| 923 | } |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 924 | } |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 925 | res = REAL(sigaction)(signo, pnew_act, oldact); |
| 926 | if (res == 0 && oldact) { |
Evgeniy Stepanov | cd3049d | 2013-04-05 12:58:07 +0000 | [diff] [blame] | 927 | uptr cb = __sanitizer::__sanitizer_get_sigaction_sa_sigaction(oldact); |
| 928 | if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { |
| 929 | __sanitizer::__sanitizer_set_sigaction_sa_sigaction(oldact, old_cb); |
| 930 | } |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 931 | } |
| 932 | } else { |
| 933 | res = REAL(sigaction)(signo, act, oldact); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 934 | } |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 935 | |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 936 | if (res == 0 && oldact) { |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 937 | __msan_unpoison(oldact, __sanitizer::struct_sigaction_sz); |
| 938 | } |
| 939 | return res; |
| 940 | } |
| 941 | |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 942 | INTERCEPTOR(int, signal, int signo, uptr cb) { |
| 943 | ENSURE_MSAN_INITED(); |
Evgeniy Stepanov | a213ab6 | 2013-04-05 11:59:16 +0000 | [diff] [blame] | 944 | if (flags()->wrap_signals) { |
| 945 | CHECK_LT(signo, kMaxSignals); |
| 946 | SpinMutexLock lock(&sigactions_mu); |
| 947 | if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { |
| 948 | sigactions[signo] = cb; |
| 949 | cb = (uptr) SignalHandler; |
| 950 | } |
| 951 | return REAL(signal)(signo, cb); |
| 952 | } else { |
| 953 | return REAL(signal)(signo, cb); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 954 | } |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 957 | extern "C" int pthread_attr_init(void *attr); |
| 958 | extern "C" int pthread_attr_destroy(void *attr); |
| 959 | extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize); |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 960 | extern "C" int pthread_attr_getstack(void *attr, uptr *stack, uptr *stacksize); |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 961 | |
| 962 | INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*), |
| 963 | void * param) { |
| 964 | ENSURE_MSAN_INITED(); // for GetTlsSize() |
| 965 | __sanitizer_pthread_attr_t myattr; |
| 966 | if (attr == 0) { |
| 967 | pthread_attr_init(&myattr); |
| 968 | attr = &myattr; |
| 969 | } |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 970 | |
| 971 | AdjustStackSizeLinux(attr, flags()->verbosity); |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 972 | |
| 973 | int res = REAL(pthread_create)(th, attr, callback, param); |
| 974 | if (attr == &myattr) |
| 975 | pthread_attr_destroy(&myattr); |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 976 | if (!res) { |
| 977 | __msan_unpoison(th, __sanitizer::pthread_t_sz); |
| 978 | } |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 979 | return res; |
| 980 | } |
| 981 | |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 982 | struct MSanInterceptorContext { |
| 983 | bool in_interceptor_scope; |
| 984 | }; |
| 985 | |
Evgeniy Stepanov | 5b2afc3 | 2013-05-23 11:51:47 +0000 | [diff] [blame] | 986 | // A version of CHECK_UNPOISED using a saved scope value. Used in common |
| 987 | // interceptors. |
Evgeniy Stepanov | 341b9e6 | 2013-06-28 11:02:43 +0000 | [diff] [blame] | 988 | #define CHECK_UNPOISONED_CTX(ctx, x, n) \ |
| 989 | do { \ |
| 990 | if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \ |
| 991 | CHECK_UNPOISONED_0(x, n); \ |
| 992 | } while (0) |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 993 | |
Evgeniy Stepanov | 996c4f2 | 2013-01-18 11:17:23 +0000 | [diff] [blame] | 994 | #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 995 | __msan_unpoison(ptr, size) |
| 996 | #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \ |
Evgeniy Stepanov | 341b9e6 | 2013-06-28 11:02:43 +0000 | [diff] [blame] | 997 | CHECK_UNPOISONED_CTX(ctx, ptr, size) |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 998 | #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ |
| 999 | if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \ |
| 1000 | MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \ |
| 1001 | ctx = (void *)&msan_ctx; \ |
| 1002 | InterceptorScope interceptor_scope; \ |
Evgeniy Stepanov | 447ef19 | 2013-05-22 12:50:26 +0000 | [diff] [blame] | 1003 | ENSURE_MSAN_INITED(); |
| 1004 | #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ |
| 1005 | do { \ |
Evgeniy Stepanov | 82a9080 | 2013-01-18 13:12:56 +0000 | [diff] [blame] | 1006 | } while (false) |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 1007 | #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ |
| 1008 | do { \ |
| 1009 | } while (false) |
| 1010 | #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ |
| 1011 | do { \ |
| 1012 | } while (false) |
Evgeniy Stepanov | 82a9080 | 2013-01-18 13:12:56 +0000 | [diff] [blame] | 1013 | #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \ |
Evgeniy Stepanov | 9d1525e | 2013-05-29 09:09:58 +0000 | [diff] [blame] | 1014 | do { \ |
| 1015 | } while (false) // FIXME |
Evgeniy Stepanov | 4f32c0b | 2013-01-18 13:01:18 +0000 | [diff] [blame] | 1016 | #include "sanitizer_common/sanitizer_common_interceptors.inc" |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 1017 | |
Evgeniy Stepanov | 2887a64 | 2013-04-11 14:37:04 +0000 | [diff] [blame] | 1018 | #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s) |
| 1019 | #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) |
| 1020 | #define COMMON_SYSCALL_POST_READ_RANGE(p, s) |
| 1021 | #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s) |
| 1022 | #include "sanitizer_common/sanitizer_common_syscalls.inc" |
| 1023 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1024 | // static |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1025 | void *fast_memset(void *ptr, int c, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1026 | // hack until we have a really fast internal_memset |
| 1027 | if (sizeof(uptr) == 8 && |
| 1028 | (n % 8) == 0 && |
| 1029 | ((uptr)ptr % 8) == 0 && |
| 1030 | (c == 0 || c == -1)) { |
| 1031 | // Printf("memset %p %zd %x\n", ptr, n, c); |
| 1032 | uptr to_store = c ? -1L : 0L; |
| 1033 | uptr *p = (uptr*)ptr; |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1034 | for (SIZE_T i = 0; i < n / 8; i++) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1035 | p[i] = to_store; |
| 1036 | return ptr; |
| 1037 | } |
| 1038 | return internal_memset(ptr, c, n); |
| 1039 | } |
| 1040 | |
| 1041 | // static |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1042 | void *fast_memcpy(void *dst, const void *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1043 | // Same hack as in fast_memset above. |
| 1044 | if (sizeof(uptr) == 8 && |
| 1045 | (n % 8) == 0 && |
| 1046 | ((uptr)dst % 8) == 0 && |
| 1047 | ((uptr)src % 8) == 0) { |
| 1048 | uptr *d = (uptr*)dst; |
| 1049 | uptr *s = (uptr*)src; |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1050 | for (SIZE_T i = 0; i < n / 8; i++) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1051 | d[i] = s[i]; |
| 1052 | return dst; |
| 1053 | } |
| 1054 | return internal_memcpy(dst, src, n); |
| 1055 | } |
| 1056 | |
| 1057 | // These interface functions reside here so that they can use |
| 1058 | // fast_memset, etc. |
Alexey Samsonov | 11347bf | 2013-04-23 13:34:19 +0000 | [diff] [blame] | 1059 | void __msan_unpoison(const void *a, uptr size) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1060 | if (!MEM_IS_APP(a)) return; |
| 1061 | fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size); |
| 1062 | } |
| 1063 | |
Alexey Samsonov | 11347bf | 2013-04-23 13:34:19 +0000 | [diff] [blame] | 1064 | void __msan_poison(const void *a, uptr size) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1065 | if (!MEM_IS_APP(a)) return; |
| 1066 | fast_memset((void*)MEM_TO_SHADOW((uptr)a), |
| 1067 | __msan::flags()->poison_heap_with_zeroes ? 0 : -1, size); |
| 1068 | } |
| 1069 | |
| 1070 | void __msan_poison_stack(void *a, uptr size) { |
| 1071 | if (!MEM_IS_APP(a)) return; |
| 1072 | fast_memset((void*)MEM_TO_SHADOW((uptr)a), |
| 1073 | __msan::flags()->poison_stack_with_zeroes ? 0 : -1, size); |
| 1074 | } |
| 1075 | |
| 1076 | void __msan_clear_and_unpoison(void *a, uptr size) { |
| 1077 | fast_memset(a, 0, size); |
| 1078 | fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size); |
| 1079 | } |
| 1080 | |
| 1081 | void __msan_copy_origin(void *dst, const void *src, uptr size) { |
| 1082 | if (!__msan_get_track_origins()) return; |
| 1083 | if (!MEM_IS_APP(dst) || !MEM_IS_APP(src)) return; |
| 1084 | uptr d = MEM_TO_ORIGIN(dst); |
| 1085 | uptr s = MEM_TO_ORIGIN(src); |
| 1086 | uptr beg = d & ~3UL; // align down. |
| 1087 | uptr end = (d + size + 3) & ~3UL; // align up. |
| 1088 | s = s & ~3UL; // align down. |
| 1089 | fast_memcpy((void*)beg, (void*)s, end - beg); |
| 1090 | } |
| 1091 | |
| 1092 | void __msan_copy_poison(void *dst, const void *src, uptr size) { |
| 1093 | if (!MEM_IS_APP(dst)) return; |
| 1094 | if (!MEM_IS_APP(src)) return; |
| 1095 | fast_memcpy((void*)MEM_TO_SHADOW((uptr)dst), |
| 1096 | (void*)MEM_TO_SHADOW((uptr)src), size); |
| 1097 | __msan_copy_origin(dst, src, size); |
| 1098 | } |
| 1099 | |
| 1100 | void __msan_move_poison(void *dst, const void *src, uptr size) { |
| 1101 | if (!MEM_IS_APP(dst)) return; |
| 1102 | if (!MEM_IS_APP(src)) return; |
| 1103 | internal_memmove((void*)MEM_TO_SHADOW((uptr)dst), |
| 1104 | (void*)MEM_TO_SHADOW((uptr)src), size); |
| 1105 | __msan_copy_origin(dst, src, size); |
| 1106 | } |
| 1107 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1108 | void *__msan_memcpy(void *dest, const void *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1109 | ENSURE_MSAN_INITED(); |
| 1110 | void *res = fast_memcpy(dest, src, n); |
| 1111 | __msan_copy_poison(dest, src, n); |
| 1112 | return res; |
| 1113 | } |
| 1114 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1115 | void *__msan_memset(void *s, int c, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1116 | ENSURE_MSAN_INITED(); |
| 1117 | void *res = fast_memset(s, c, n); |
| 1118 | __msan_unpoison(s, n); |
| 1119 | return res; |
| 1120 | } |
| 1121 | |
Kostya Serebryany | 6afa1b0 | 2012-12-13 06:31:40 +0000 | [diff] [blame] | 1122 | void *__msan_memmove(void *dest, const void *src, SIZE_T n) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1123 | ENSURE_MSAN_INITED(); |
| 1124 | void *res = REAL(memmove)(dest, src, n); |
| 1125 | __msan_move_poison(dest, src, n); |
| 1126 | return res; |
| 1127 | } |
| 1128 | |
| 1129 | namespace __msan { |
| 1130 | void InitializeInterceptors() { |
| 1131 | static int inited = 0; |
| 1132 | CHECK_EQ(inited, 0); |
Kostya Serebryany | c20b321 | 2013-01-18 06:43:13 +0000 | [diff] [blame] | 1133 | SANITIZER_COMMON_INTERCEPTORS_INIT; |
| 1134 | |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1135 | INTERCEPT_FUNCTION(mmap); |
| 1136 | INTERCEPT_FUNCTION(mmap64); |
| 1137 | INTERCEPT_FUNCTION(posix_memalign); |
| 1138 | INTERCEPT_FUNCTION(malloc); |
| 1139 | INTERCEPT_FUNCTION(calloc); |
| 1140 | INTERCEPT_FUNCTION(realloc); |
| 1141 | INTERCEPT_FUNCTION(free); |
| 1142 | INTERCEPT_FUNCTION(fread); |
| 1143 | INTERCEPT_FUNCTION(fread_unlocked); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1144 | INTERCEPT_FUNCTION(readlink); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1145 | INTERCEPT_FUNCTION(memcpy); |
Evgeniy Stepanov | 353c996 | 2013-07-02 14:49:24 +0000 | [diff] [blame] | 1146 | INTERCEPT_FUNCTION(mempcpy); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1147 | INTERCEPT_FUNCTION(memset); |
| 1148 | INTERCEPT_FUNCTION(memmove); |
Evgeniy Stepanov | ff6c9fb | 2013-07-04 13:19:41 +0000 | [diff] [blame^] | 1149 | INTERCEPT_FUNCTION(bcopy); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1150 | INTERCEPT_FUNCTION(wmemset); |
| 1151 | INTERCEPT_FUNCTION(wmemcpy); |
Evgeniy Stepanov | 353c996 | 2013-07-02 14:49:24 +0000 | [diff] [blame] | 1152 | INTERCEPT_FUNCTION(wmempcpy); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1153 | INTERCEPT_FUNCTION(wmemmove); |
| 1154 | INTERCEPT_FUNCTION(strcpy); // NOLINT |
Evgeniy Stepanov | 4bbbe13 | 2013-07-01 15:19:37 +0000 | [diff] [blame] | 1155 | INTERCEPT_FUNCTION(stpcpy); // NOLINT |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1156 | INTERCEPT_FUNCTION(strdup); |
Evgeniy Stepanov | 8aa1ae0 | 2013-03-14 11:10:36 +0000 | [diff] [blame] | 1157 | INTERCEPT_FUNCTION(__strdup); |
| 1158 | INTERCEPT_FUNCTION(strndup); |
| 1159 | INTERCEPT_FUNCTION(__strndup); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1160 | INTERCEPT_FUNCTION(strncpy); // NOLINT |
| 1161 | INTERCEPT_FUNCTION(strlen); |
| 1162 | INTERCEPT_FUNCTION(strnlen); |
| 1163 | INTERCEPT_FUNCTION(gcvt); |
| 1164 | INTERCEPT_FUNCTION(strcat); // NOLINT |
| 1165 | INTERCEPT_FUNCTION(strncat); // NOLINT |
| 1166 | INTERCEPT_FUNCTION(strtol); |
| 1167 | INTERCEPT_FUNCTION(strtoll); |
| 1168 | INTERCEPT_FUNCTION(strtoul); |
| 1169 | INTERCEPT_FUNCTION(strtoull); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1170 | INTERCEPT_FUNCTION(strtod); |
| 1171 | INTERCEPT_FUNCTION(strtof); |
| 1172 | INTERCEPT_FUNCTION(strtold); |
Evgeniy Stepanov | 0797ed4 | 2013-06-21 13:32:26 +0000 | [diff] [blame] | 1173 | INTERCEPT_FUNCTION(vasprintf); |
| 1174 | INTERCEPT_FUNCTION(asprintf); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1175 | INTERCEPT_FUNCTION(vsprintf); |
| 1176 | INTERCEPT_FUNCTION(vsnprintf); |
| 1177 | INTERCEPT_FUNCTION(vswprintf); |
| 1178 | INTERCEPT_FUNCTION(sprintf); // NOLINT |
| 1179 | INTERCEPT_FUNCTION(snprintf); |
| 1180 | INTERCEPT_FUNCTION(swprintf); |
| 1181 | INTERCEPT_FUNCTION(strftime); |
Evgeniy Stepanov | 8014489 | 2013-07-02 13:34:44 +0000 | [diff] [blame] | 1182 | INTERCEPT_FUNCTION(mbtowc); |
| 1183 | INTERCEPT_FUNCTION(mbrtowc); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1184 | INTERCEPT_FUNCTION(wcslen); |
| 1185 | INTERCEPT_FUNCTION(wcschr); |
| 1186 | INTERCEPT_FUNCTION(wcscpy); |
| 1187 | INTERCEPT_FUNCTION(wcscmp); |
| 1188 | INTERCEPT_FUNCTION(wcstod); |
| 1189 | INTERCEPT_FUNCTION(getenv); |
| 1190 | INTERCEPT_FUNCTION(gettimeofday); |
| 1191 | INTERCEPT_FUNCTION(fcvt); |
| 1192 | INTERCEPT_FUNCTION(__fxstat); |
| 1193 | INTERCEPT_FUNCTION(__xstat); |
| 1194 | INTERCEPT_FUNCTION(__lxstat); |
| 1195 | INTERCEPT_FUNCTION(__fxstat64); |
| 1196 | INTERCEPT_FUNCTION(__xstat64); |
| 1197 | INTERCEPT_FUNCTION(__lxstat64); |
| 1198 | INTERCEPT_FUNCTION(pipe); |
Evgeniy Stepanov | 134fe8a | 2013-04-08 13:45:12 +0000 | [diff] [blame] | 1199 | INTERCEPT_FUNCTION(pipe2); |
| 1200 | INTERCEPT_FUNCTION(socketpair); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1201 | INTERCEPT_FUNCTION(fgets); |
| 1202 | INTERCEPT_FUNCTION(fgets_unlocked); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1203 | INTERCEPT_FUNCTION(realpath); |
| 1204 | INTERCEPT_FUNCTION(getrlimit); |
| 1205 | INTERCEPT_FUNCTION(getrlimit64); |
| 1206 | INTERCEPT_FUNCTION(statfs); |
| 1207 | INTERCEPT_FUNCTION(fstatfs); |
| 1208 | INTERCEPT_FUNCTION(statfs64); |
| 1209 | INTERCEPT_FUNCTION(fstatfs64); |
| 1210 | INTERCEPT_FUNCTION(uname); |
Evgeniy Stepanov | 95d0588 | 2013-01-23 10:43:38 +0000 | [diff] [blame] | 1211 | INTERCEPT_FUNCTION(gethostname); |
Alexey Samsonov | 8028c71 | 2012-12-14 11:52:02 +0000 | [diff] [blame] | 1212 | INTERCEPT_FUNCTION(epoll_wait); |
| 1213 | INTERCEPT_FUNCTION(epoll_pwait); |
| 1214 | INTERCEPT_FUNCTION(recv); |
| 1215 | INTERCEPT_FUNCTION(recvfrom); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1216 | INTERCEPT_FUNCTION(dladdr); |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 1217 | INTERCEPT_FUNCTION(dlopen); |
Evgeniy Stepanov | 2bba4ef | 2013-05-16 13:00:25 +0000 | [diff] [blame] | 1218 | INTERCEPT_FUNCTION(dl_iterate_phdr); |
Evgeniy Stepanov | e03345b | 2013-01-17 13:42:17 +0000 | [diff] [blame] | 1219 | INTERCEPT_FUNCTION(getrusage); |
Evgeniy Stepanov | e4bdda5 | 2013-04-01 14:47:21 +0000 | [diff] [blame] | 1220 | INTERCEPT_FUNCTION(sigaction); |
Evgeniy Stepanov | 06658ea | 2013-04-04 08:22:52 +0000 | [diff] [blame] | 1221 | INTERCEPT_FUNCTION(signal); |
Evgeniy Stepanov | 10fd322 | 2013-03-13 09:01:40 +0000 | [diff] [blame] | 1222 | INTERCEPT_FUNCTION(pthread_create); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1223 | inited = 1; |
| 1224 | } |
| 1225 | } // namespace __msan |