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