1 //===-- msan_interceptors.cpp ---------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of MemorySanitizer. 10 // 11 // Interceptors for standard library functions. 12 // 13 // FIXME: move as many interceptors as possible into 14 // sanitizer_common/sanitizer_common_interceptors.h 15 //===----------------------------------------------------------------------===// 16 17 #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS 18 19 #include "interception/interception.h" 20 #include "msan.h" 21 #include "msan_chained_origin_depot.h" 22 #include "msan_dl.h" 23 #include "msan_origin.h" 24 #include "msan_poisoning.h" 25 #include "msan_report.h" 26 #include "msan_thread.h" 27 #include "sanitizer_common/sanitizer_allocator.h" 28 #include "sanitizer_common/sanitizer_allocator_dlsym.h" 29 #include "sanitizer_common/sanitizer_allocator_interface.h" 30 #include "sanitizer_common/sanitizer_atomic.h" 31 #include "sanitizer_common/sanitizer_common.h" 32 #include "sanitizer_common/sanitizer_errno.h" 33 #include "sanitizer_common/sanitizer_errno_codes.h" 34 #include "sanitizer_common/sanitizer_glibc_version.h" 35 #include "sanitizer_common/sanitizer_libc.h" 36 #include "sanitizer_common/sanitizer_linux.h" 37 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h" 38 #include "sanitizer_common/sanitizer_platform_limits_posix.h" 39 #include "sanitizer_common/sanitizer_stackdepot.h" 40 #include "sanitizer_common/sanitizer_tls_get_addr.h" 41 #include "sanitizer_common/sanitizer_vector.h" 42 43 #if SANITIZER_NETBSD 44 #define fstat __fstat50 45 #define gettimeofday __gettimeofday50 46 #define getrusage __getrusage50 47 #define tzset __tzset50 48 #endif 49 50 #include <stdarg.h> 51 // ACHTUNG! No other system header includes in this file. 52 // Ideally, we should get rid of stdarg.h as well. 53 54 using namespace __msan; 55 56 using __sanitizer::memory_order; 57 using __sanitizer::atomic_load; 58 using __sanitizer::atomic_store; 59 using __sanitizer::atomic_uintptr_t; 60 61 DECLARE_REAL(SIZE_T, strlen, const char *s) 62 DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen) 63 DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n) 64 DECLARE_REAL(void *, memset, void *dest, int c, uptr n) 65 66 // True if this is a nested interceptor. 67 static THREADLOCAL int in_interceptor_scope; 68 69 void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; } 70 void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; } 71 72 struct InterceptorScope { 73 InterceptorScope() { ++in_interceptor_scope; } 74 ~InterceptorScope() { --in_interceptor_scope; } 75 }; 76 77 bool IsInInterceptorScope() { 78 return in_interceptor_scope; 79 } 80 81 struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> { 82 static bool UseImpl() { return !msan_inited; } 83 }; 84 85 #define ENSURE_MSAN_INITED() do { \ 86 CHECK(!msan_init_is_running); \ 87 if (!msan_inited) { \ 88 __msan_init(); \ 89 } \ 90 } while (0) 91 92 // Check that [x, x+n) range is unpoisoned. 93 #define CHECK_UNPOISONED_0(x, n) \ 94 do { \ 95 sptr __offset = __msan_test_shadow(x, n); \ 96 if (__msan::IsInSymbolizerOrUnwider()) \ 97 break; \ 98 if (__offset >= 0 && __msan::flags()->report_umrs) { \ 99 GET_CALLER_PC_BP; \ 100 ReportUMRInsideAddressRange(__func__, x, n, __offset); \ 101 __msan::PrintWarningWithOrigin( \ 102 pc, bp, __msan_get_origin((const char *)x + __offset)); \ 103 if (__msan::flags()->halt_on_error) { \ 104 Printf("Exiting\n"); \ 105 Die(); \ 106 } \ 107 } \ 108 } while (0) 109 110 // Check that [x, x+n) range is unpoisoned unless we are in a nested 111 // interceptor. 112 #define CHECK_UNPOISONED(x, n) \ 113 do { \ 114 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \ 115 } while (0) 116 117 #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \ 118 CHECK_UNPOISONED((x), \ 119 common_flags()->strict_string_checks ? (len) + 1 : (n) ) 120 121 #define CHECK_UNPOISONED_STRING(x, n) \ 122 CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n)) 123 124 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 125 INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb, 126 void *file) { 127 ENSURE_MSAN_INITED(); 128 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file); 129 if (res > 0) 130 __msan_unpoison(ptr, res *size); 131 return res; 132 } 133 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked) 134 #else 135 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED 136 #endif 137 138 #if !SANITIZER_NETBSD 139 INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) { 140 return (char *)__msan_memcpy(dest, src, n) + n; 141 } 142 #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy) 143 #else 144 #define MSAN_MAYBE_INTERCEPT_MEMPCPY 145 #endif 146 147 INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) { 148 ENSURE_MSAN_INITED(); 149 void *res = REAL(memccpy)(dest, src, c, n); 150 CHECK(!res || (res >= dest && res <= (char *)dest + n)); 151 SIZE_T sz = res ? (char *)res - (char *)dest : n; 152 CHECK_UNPOISONED(src, sz); 153 __msan_unpoison(dest, sz); 154 return res; 155 } 156 157 INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) { 158 return __msan_memmove(dest, src, n); 159 } 160 161 INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) { 162 GET_MALLOC_STACK_TRACE; 163 CHECK_NE(memptr, 0); 164 int res = msan_posix_memalign(memptr, alignment, size, &stack); 165 if (!res) 166 __msan_unpoison(memptr, sizeof(*memptr)); 167 return res; 168 } 169 170 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 171 INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) { 172 GET_MALLOC_STACK_TRACE; 173 return msan_memalign(alignment, size, &stack); 174 } 175 #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign) 176 #else 177 #define MSAN_MAYBE_INTERCEPT_MEMALIGN 178 #endif 179 180 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) { 181 GET_MALLOC_STACK_TRACE; 182 return msan_aligned_alloc(alignment, size, &stack); 183 } 184 185 #if !SANITIZER_NETBSD 186 INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) { 187 GET_MALLOC_STACK_TRACE; 188 void *ptr = msan_memalign(alignment, size, &stack); 189 if (ptr) 190 DTLS_on_libc_memalign(ptr, size); 191 return ptr; 192 } 193 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign) 194 #else 195 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN 196 #endif 197 198 INTERCEPTOR(void *, valloc, SIZE_T size) { 199 GET_MALLOC_STACK_TRACE; 200 return msan_valloc(size, &stack); 201 } 202 203 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 204 INTERCEPTOR(void *, pvalloc, SIZE_T size) { 205 GET_MALLOC_STACK_TRACE; 206 return msan_pvalloc(size, &stack); 207 } 208 #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc) 209 #else 210 #define MSAN_MAYBE_INTERCEPT_PVALLOC 211 #endif 212 213 INTERCEPTOR(void, free, void *ptr) { 214 if (UNLIKELY(!ptr)) 215 return; 216 if (DlsymAlloc::PointerIsMine(ptr)) 217 return DlsymAlloc::Free(ptr); 218 GET_MALLOC_STACK_TRACE; 219 MsanDeallocate(&stack, ptr); 220 } 221 222 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 223 INTERCEPTOR(void, cfree, void *ptr) { 224 if (UNLIKELY(!ptr)) 225 return; 226 if (DlsymAlloc::PointerIsMine(ptr)) 227 return DlsymAlloc::Free(ptr); 228 GET_MALLOC_STACK_TRACE; 229 MsanDeallocate(&stack, ptr); 230 } 231 # define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree) 232 #else 233 #define MSAN_MAYBE_INTERCEPT_CFREE 234 #endif 235 236 #if !SANITIZER_NETBSD 237 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { 238 return __sanitizer_get_allocated_size(ptr); 239 } 240 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \ 241 INTERCEPT_FUNCTION(malloc_usable_size) 242 #else 243 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE 244 #endif 245 246 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 247 // This function actually returns a struct by value, but we can't unpoison a 248 // temporary! The following is equivalent on all supported platforms but 249 // aarch64 (which uses a different register for sret value). We have a test 250 // to confirm that. 251 INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) { 252 #ifdef __aarch64__ 253 uptr r8; 254 asm volatile("mov %0,x8" : "=r" (r8)); 255 sret = reinterpret_cast<__sanitizer_struct_mallinfo*>(r8); 256 #endif 257 REAL(memset)(sret, 0, sizeof(*sret)); 258 __msan_unpoison(sret, sizeof(*sret)); 259 } 260 #define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo) 261 #else 262 #define MSAN_MAYBE_INTERCEPT_MALLINFO 263 #endif 264 265 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 266 INTERCEPTOR(int, mallopt, int cmd, int value) { 267 return 0; 268 } 269 #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt) 270 #else 271 #define MSAN_MAYBE_INTERCEPT_MALLOPT 272 #endif 273 274 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 275 INTERCEPTOR(void, malloc_stats, void) { 276 // FIXME: implement, but don't call REAL(malloc_stats)! 277 } 278 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats) 279 #else 280 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS 281 #endif 282 283 INTERCEPTOR(char *, strcpy, char *dest, const char *src) { 284 ENSURE_MSAN_INITED(); 285 GET_STORE_STACK_TRACE; 286 SIZE_T n = internal_strlen(src); 287 CHECK_UNPOISONED_STRING(src + n, 0); 288 char *res = REAL(strcpy)(dest, src); 289 CopyShadowAndOrigin(dest, src, n + 1, &stack); 290 return res; 291 } 292 293 INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { 294 ENSURE_MSAN_INITED(); 295 GET_STORE_STACK_TRACE; 296 SIZE_T copy_size = internal_strnlen(src, n); 297 if (copy_size < n) 298 copy_size++; // trailing \0 299 char *res = REAL(strncpy)(dest, src, n); 300 CopyShadowAndOrigin(dest, src, copy_size, &stack); 301 __msan_unpoison(dest + copy_size, n - copy_size); 302 return res; 303 } 304 305 #if !SANITIZER_NETBSD 306 INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { 307 ENSURE_MSAN_INITED(); 308 GET_STORE_STACK_TRACE; 309 SIZE_T n = internal_strlen(src); 310 CHECK_UNPOISONED_STRING(src + n, 0); 311 char *res = REAL(stpcpy)(dest, src); 312 CopyShadowAndOrigin(dest, src, n + 1, &stack); 313 return res; 314 } 315 316 INTERCEPTOR(char *, stpncpy, char *dest, const char *src, SIZE_T n) { 317 ENSURE_MSAN_INITED(); 318 GET_STORE_STACK_TRACE; 319 SIZE_T copy_size = Min(n, internal_strnlen(src, n) + 1); 320 char *res = REAL(stpncpy)(dest, src, n); 321 CopyShadowAndOrigin(dest, src, copy_size, &stack); 322 __msan_unpoison(dest + copy_size, n - copy_size); 323 return res; 324 } 325 # define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy) 326 # define MSAN_MAYBE_INTERCEPT_STPNCPY INTERCEPT_FUNCTION(stpncpy) 327 #else 328 #define MSAN_MAYBE_INTERCEPT_STPCPY 329 # define MSAN_MAYBE_INTERCEPT_STPNCPY 330 #endif 331 332 INTERCEPTOR(char *, strdup, char *src) { 333 ENSURE_MSAN_INITED(); 334 GET_STORE_STACK_TRACE; 335 // On FreeBSD strdup() leverages strlen(). 336 InterceptorScope interceptor_scope; 337 SIZE_T n = internal_strlen(src); 338 CHECK_UNPOISONED_STRING(src + n, 0); 339 char *res = REAL(strdup)(src); 340 CopyShadowAndOrigin(res, src, n + 1, &stack); 341 return res; 342 } 343 344 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 345 INTERCEPTOR(char *, __strdup, char *src) { 346 ENSURE_MSAN_INITED(); 347 GET_STORE_STACK_TRACE; 348 SIZE_T n = internal_strlen(src); 349 CHECK_UNPOISONED_STRING(src + n, 0); 350 char *res = REAL(__strdup)(src); 351 CopyShadowAndOrigin(res, src, n + 1, &stack); 352 return res; 353 } 354 #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup) 355 #else 356 #define MSAN_MAYBE_INTERCEPT___STRDUP 357 #endif 358 359 #if !SANITIZER_NETBSD 360 INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) { 361 ENSURE_MSAN_INITED(); 362 char *res = REAL(gcvt)(number, ndigit, buf); 363 SIZE_T n = internal_strlen(buf); 364 __msan_unpoison(buf, n + 1); 365 return res; 366 } 367 #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt) 368 #else 369 #define MSAN_MAYBE_INTERCEPT_GCVT 370 #endif 371 372 INTERCEPTOR(char *, strcat, char *dest, const char *src) { 373 ENSURE_MSAN_INITED(); 374 GET_STORE_STACK_TRACE; 375 SIZE_T src_size = internal_strlen(src); 376 SIZE_T dest_size = internal_strlen(dest); 377 CHECK_UNPOISONED_STRING(src + src_size, 0); 378 CHECK_UNPOISONED_STRING(dest + dest_size, 0); 379 char *res = REAL(strcat)(dest, src); 380 CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack); 381 return res; 382 } 383 384 INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { 385 ENSURE_MSAN_INITED(); 386 GET_STORE_STACK_TRACE; 387 SIZE_T dest_size = internal_strlen(dest); 388 SIZE_T copy_size = internal_strnlen(src, n); 389 CHECK_UNPOISONED_STRING(dest + dest_size, 0); 390 char *res = REAL(strncat)(dest, src, n); 391 CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack); 392 __msan_unpoison(dest + dest_size + copy_size, 1); // \0 393 return res; 394 } 395 396 // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to 397 // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO. 398 #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \ 399 ENSURE_MSAN_INITED(); \ 400 ret_type res = REAL(func)(__VA_ARGS__); \ 401 __msan_unpoison(endptr, sizeof(*endptr)); \ 402 return res; 403 404 #define INTERCEPTOR_STRTO(ret_type, func, char_type) \ 405 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \ 406 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \ 407 } 408 409 #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \ 410 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ 411 int base) { \ 412 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \ 413 } 414 415 #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \ 416 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ 417 void *loc) { \ 418 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \ 419 } 420 421 #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \ 422 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ 423 int base, void *loc) { \ 424 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \ 425 } 426 427 #if SANITIZER_NETBSD 428 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \ 429 INTERCEPTOR_STRTO(ret_type, func, char_type) \ 430 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) 431 432 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \ 433 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \ 434 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) 435 436 #else 437 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \ 438 INTERCEPTOR_STRTO(ret_type, func, char_type) \ 439 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \ 440 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \ 441 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type) 442 443 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \ 444 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \ 445 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \ 446 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \ 447 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type) 448 #endif 449 450 INTERCEPTORS_STRTO(double, strtod, char) 451 INTERCEPTORS_STRTO(float, strtof, char) 452 INTERCEPTORS_STRTO(long double, strtold, char) 453 INTERCEPTORS_STRTO_BASE(long, strtol, char) 454 INTERCEPTORS_STRTO_BASE(long long, strtoll, char) 455 INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char) 456 INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char) 457 INTERCEPTORS_STRTO_BASE(u64, strtouq, char) 458 459 INTERCEPTORS_STRTO(double, wcstod, wchar_t) 460 INTERCEPTORS_STRTO(float, wcstof, wchar_t) 461 INTERCEPTORS_STRTO(long double, wcstold, wchar_t) 462 INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t) 463 INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t) 464 INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t) 465 INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t) 466 467 #if SANITIZER_NETBSD 468 #define INTERCEPT_STRTO(func) \ 469 INTERCEPT_FUNCTION(func); \ 470 INTERCEPT_FUNCTION(func##_l); 471 #else 472 #define INTERCEPT_STRTO(func) \ 473 INTERCEPT_FUNCTION(func); \ 474 INTERCEPT_FUNCTION(func##_l); \ 475 INTERCEPT_FUNCTION(__##func##_l); \ 476 INTERCEPT_FUNCTION(__##func##_internal); 477 #endif 478 479 480 // FIXME: support *wprintf in common format interceptors. 481 INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) { 482 ENSURE_MSAN_INITED(); 483 int res = REAL(vswprintf)(str, size, format, ap); 484 if (res >= 0) { 485 __msan_unpoison(str, 4 * (res + 1)); 486 } 487 return res; 488 } 489 490 INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) { 491 ENSURE_MSAN_INITED(); 492 va_list ap; 493 va_start(ap, format); 494 int res = vswprintf(str, size, format, ap); 495 va_end(ap); 496 return res; 497 } 498 499 #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \ 500 ENSURE_MSAN_INITED(); \ 501 InterceptorScope interceptor_scope; \ 502 ret_type res = REAL(func)(s, __VA_ARGS__); \ 503 if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \ 504 return res; 505 506 INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format, 507 __sanitizer_tm *tm) { 508 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm); 509 } 510 511 INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format, 512 __sanitizer_tm *tm, void *loc) { 513 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc); 514 } 515 516 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 517 INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format, 518 __sanitizer_tm *tm, void *loc) { 519 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm, 520 loc); 521 } 522 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l) 523 #else 524 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L 525 #endif 526 527 INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format, 528 __sanitizer_tm *tm) { 529 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm); 530 } 531 532 INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format, 533 __sanitizer_tm *tm, void *loc) { 534 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm, 535 loc); 536 } 537 538 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 539 INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format, 540 __sanitizer_tm *tm, void *loc) { 541 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm, 542 loc); 543 } 544 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l) 545 #else 546 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L 547 #endif 548 549 INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) { 550 ENSURE_MSAN_INITED(); 551 int res = REAL(mbtowc)(dest, src, n); 552 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t)); 553 return res; 554 } 555 556 INTERCEPTOR(SIZE_T, mbrtowc, wchar_t *dest, const char *src, SIZE_T n, 557 void *ps) { 558 ENSURE_MSAN_INITED(); 559 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps); 560 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t)); 561 return res; 562 } 563 564 // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n); 565 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) { 566 ENSURE_MSAN_INITED(); 567 GET_STORE_STACK_TRACE; 568 wchar_t *res = REAL(wmemcpy)(dest, src, n); 569 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack); 570 return res; 571 } 572 573 #if !SANITIZER_NETBSD 574 INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) { 575 ENSURE_MSAN_INITED(); 576 GET_STORE_STACK_TRACE; 577 wchar_t *res = REAL(wmempcpy)(dest, src, n); 578 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack); 579 return res; 580 } 581 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY INTERCEPT_FUNCTION(wmempcpy) 582 #else 583 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY 584 #endif 585 586 INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) { 587 CHECK(MEM_IS_APP(s)); 588 ENSURE_MSAN_INITED(); 589 wchar_t *res = REAL(wmemset)(s, c, n); 590 __msan_unpoison(s, n * sizeof(wchar_t)); 591 return res; 592 } 593 594 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) { 595 ENSURE_MSAN_INITED(); 596 GET_STORE_STACK_TRACE; 597 wchar_t *res = REAL(wmemmove)(dest, src, n); 598 MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack); 599 return res; 600 } 601 602 INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) { 603 ENSURE_MSAN_INITED(); 604 int res = REAL(wcscmp)(s1, s2); 605 return res; 606 } 607 608 INTERCEPTOR(int, gettimeofday, void *tv, void *tz) { 609 ENSURE_MSAN_INITED(); 610 int res = REAL(gettimeofday)(tv, tz); 611 if (tv) 612 __msan_unpoison(tv, 16); 613 if (tz) 614 __msan_unpoison(tz, 8); 615 return res; 616 } 617 618 #if !SANITIZER_NETBSD 619 INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) { 620 ENSURE_MSAN_INITED(); 621 char *res = REAL(fcvt)(x, a, b, c); 622 __msan_unpoison(b, sizeof(*b)); 623 __msan_unpoison(c, sizeof(*c)); 624 if (res) 625 __msan_unpoison(res, internal_strlen(res) + 1); 626 return res; 627 } 628 #define MSAN_MAYBE_INTERCEPT_FCVT INTERCEPT_FUNCTION(fcvt) 629 #else 630 #define MSAN_MAYBE_INTERCEPT_FCVT 631 #endif 632 633 INTERCEPTOR(char *, getenv, char *name) { 634 if (msan_init_is_running) 635 return REAL(getenv)(name); 636 ENSURE_MSAN_INITED(); 637 char *res = REAL(getenv)(name); 638 if (res) 639 __msan_unpoison(res, internal_strlen(res) + 1); 640 return res; 641 } 642 643 extern char **environ; 644 645 static void UnpoisonEnviron() { 646 char **envp = environ; 647 for (; *envp; ++envp) { 648 __msan_unpoison(envp, sizeof(*envp)); 649 __msan_unpoison(*envp, internal_strlen(*envp) + 1); 650 } 651 // Trailing NULL pointer. 652 __msan_unpoison(envp, sizeof(*envp)); 653 } 654 655 INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) { 656 ENSURE_MSAN_INITED(); 657 CHECK_UNPOISONED_STRING(name, 0); 658 int res = REAL(setenv)(name, value, overwrite); 659 if (!res) UnpoisonEnviron(); 660 return res; 661 } 662 663 INTERCEPTOR(int, putenv, char *string) { 664 ENSURE_MSAN_INITED(); 665 int res = REAL(putenv)(string); 666 if (!res) UnpoisonEnviron(); 667 return res; 668 } 669 670 #define SANITIZER_STAT_LINUX (SANITIZER_LINUX && __GLIBC_PREREQ(2, 33)) 671 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX 672 INTERCEPTOR(int, fstat, int fd, void *buf) { 673 ENSURE_MSAN_INITED(); 674 int res = REAL(fstat)(fd, buf); 675 if (!res) 676 __msan_unpoison(buf, __sanitizer::struct_stat_sz); 677 return res; 678 } 679 # define MSAN_MAYBE_INTERCEPT_FSTAT MSAN_INTERCEPT_FUNC(fstat) 680 #else 681 #define MSAN_MAYBE_INTERCEPT_FSTAT 682 #endif 683 684 #if SANITIZER_STAT_LINUX 685 INTERCEPTOR(int, fstat64, int fd, void *buf) { 686 ENSURE_MSAN_INITED(); 687 int res = REAL(fstat64)(fd, buf); 688 if (!res) 689 __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 690 return res; 691 } 692 # define MSAN_MAYBE_INTERCEPT_FSTAT64 MSAN_INTERCEPT_FUNC(fstat64) 693 #else 694 # define MSAN_MAYBE_INTERCEPT_FSTAT64 695 #endif 696 697 #if SANITIZER_GLIBC 698 INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) { 699 ENSURE_MSAN_INITED(); 700 int res = REAL(__fxstat)(magic, fd, buf); 701 if (!res) 702 __msan_unpoison(buf, __sanitizer::struct_stat_sz); 703 return res; 704 } 705 # define MSAN_MAYBE_INTERCEPT___FXSTAT MSAN_INTERCEPT_FUNC(__fxstat) 706 #else 707 #define MSAN_MAYBE_INTERCEPT___FXSTAT 708 #endif 709 710 #if SANITIZER_GLIBC 711 INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) { 712 ENSURE_MSAN_INITED(); 713 int res = REAL(__fxstat64)(magic, fd, buf); 714 if (!res) 715 __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 716 return res; 717 } 718 # define MSAN_MAYBE_INTERCEPT___FXSTAT64 MSAN_INTERCEPT_FUNC(__fxstat64) 719 #else 720 # define MSAN_MAYBE_INTERCEPT___FXSTAT64 721 #endif 722 723 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX 724 INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) { 725 ENSURE_MSAN_INITED(); 726 int res = REAL(fstatat)(fd, pathname, buf, flags); 727 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz); 728 return res; 729 } 730 # define MSAN_MAYBE_INTERCEPT_FSTATAT MSAN_INTERCEPT_FUNC(fstatat) 731 #else 732 # define MSAN_MAYBE_INTERCEPT_FSTATAT 733 #endif 734 735 #if SANITIZER_STAT_LINUX 736 INTERCEPTOR(int, fstatat64, int fd, char *pathname, void *buf, int flags) { 737 ENSURE_MSAN_INITED(); 738 int res = REAL(fstatat64)(fd, pathname, buf, flags); 739 if (!res) 740 __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 741 return res; 742 } 743 # define MSAN_MAYBE_INTERCEPT_FSTATAT64 MSAN_INTERCEPT_FUNC(fstatat64) 744 #else 745 # define MSAN_MAYBE_INTERCEPT_FSTATAT64 746 #endif 747 748 #if SANITIZER_GLIBC 749 INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf, 750 int flags) { 751 ENSURE_MSAN_INITED(); 752 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags); 753 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz); 754 return res; 755 } 756 # define MSAN_MAYBE_INTERCEPT___FXSTATAT MSAN_INTERCEPT_FUNC(__fxstatat) 757 #else 758 # define MSAN_MAYBE_INTERCEPT___FXSTATAT 759 #endif 760 761 #if SANITIZER_GLIBC 762 INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf, 763 int flags) { 764 ENSURE_MSAN_INITED(); 765 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags); 766 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 767 return res; 768 } 769 # define MSAN_MAYBE_INTERCEPT___FXSTATAT64 MSAN_INTERCEPT_FUNC(__fxstatat64) 770 #else 771 # define MSAN_MAYBE_INTERCEPT___FXSTATAT64 772 #endif 773 774 INTERCEPTOR(int, pipe, int pipefd[2]) { 775 if (msan_init_is_running) 776 return REAL(pipe)(pipefd); 777 ENSURE_MSAN_INITED(); 778 int res = REAL(pipe)(pipefd); 779 if (!res) 780 __msan_unpoison(pipefd, sizeof(int[2])); 781 return res; 782 } 783 784 INTERCEPTOR(int, pipe2, int pipefd[2], int flags) { 785 ENSURE_MSAN_INITED(); 786 int res = REAL(pipe2)(pipefd, flags); 787 if (!res) 788 __msan_unpoison(pipefd, sizeof(int[2])); 789 return res; 790 } 791 792 INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) { 793 ENSURE_MSAN_INITED(); 794 int res = REAL(socketpair)(domain, type, protocol, sv); 795 if (!res) 796 __msan_unpoison(sv, sizeof(int[2])); 797 return res; 798 } 799 800 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 801 INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) { 802 ENSURE_MSAN_INITED(); 803 char *res = REAL(fgets_unlocked)(s, size, stream); 804 if (res) 805 __msan_unpoison(s, internal_strlen(s) + 1); 806 return res; 807 } 808 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked) 809 #else 810 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED 811 #endif 812 813 #define INTERCEPTOR_GETRLIMIT_BODY(func, resource, rlim) \ 814 if (msan_init_is_running) \ 815 return REAL(getrlimit)(resource, rlim); \ 816 ENSURE_MSAN_INITED(); \ 817 int res = REAL(func)(resource, rlim); \ 818 if (!res) \ 819 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz); \ 820 return res 821 822 INTERCEPTOR(int, getrlimit, int resource, void *rlim) { 823 INTERCEPTOR_GETRLIMIT_BODY(getrlimit, resource, rlim); 824 } 825 826 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 827 INTERCEPTOR(int, __getrlimit, int resource, void *rlim) { 828 INTERCEPTOR_GETRLIMIT_BODY(__getrlimit, resource, rlim); 829 } 830 831 INTERCEPTOR(int, getrlimit64, int resource, void *rlim) { 832 if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim); 833 ENSURE_MSAN_INITED(); 834 int res = REAL(getrlimit64)(resource, rlim); 835 if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz); 836 return res; 837 } 838 839 INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit, 840 void *old_rlimit) { 841 if (msan_init_is_running) 842 return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit); 843 ENSURE_MSAN_INITED(); 844 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz); 845 int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit); 846 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz); 847 return res; 848 } 849 850 INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit, 851 void *old_rlimit) { 852 if (msan_init_is_running) 853 return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit); 854 ENSURE_MSAN_INITED(); 855 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz); 856 int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit); 857 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz); 858 return res; 859 } 860 861 #define MSAN_MAYBE_INTERCEPT___GETRLIMIT INTERCEPT_FUNCTION(__getrlimit) 862 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64) 863 #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit) 864 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64) 865 #else 866 #define MSAN_MAYBE_INTERCEPT___GETRLIMIT 867 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 868 #define MSAN_MAYBE_INTERCEPT_PRLIMIT 869 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 870 #endif 871 872 INTERCEPTOR(int, gethostname, char *name, SIZE_T len) { 873 ENSURE_MSAN_INITED(); 874 int res = REAL(gethostname)(name, len); 875 if (!res || (res == -1 && errno == errno_ENAMETOOLONG)) { 876 SIZE_T real_len = internal_strnlen(name, len); 877 if (real_len < len) 878 ++real_len; 879 __msan_unpoison(name, real_len); 880 } 881 return res; 882 } 883 884 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 885 INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents, 886 int timeout) { 887 ENSURE_MSAN_INITED(); 888 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout); 889 if (res > 0) { 890 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res); 891 } 892 return res; 893 } 894 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait) 895 #else 896 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT 897 #endif 898 899 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD 900 INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents, 901 int timeout, void *sigmask) { 902 ENSURE_MSAN_INITED(); 903 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask); 904 if (res > 0) { 905 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res); 906 } 907 return res; 908 } 909 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait) 910 #else 911 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT 912 #endif 913 914 INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) { 915 GET_MALLOC_STACK_TRACE; 916 if (DlsymAlloc::Use()) 917 return DlsymAlloc::Callocate(nmemb, size); 918 return msan_calloc(nmemb, size, &stack); 919 } 920 921 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) { 922 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr)) 923 return DlsymAlloc::Realloc(ptr, size); 924 GET_MALLOC_STACK_TRACE; 925 return msan_realloc(ptr, size, &stack); 926 } 927 928 INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) { 929 GET_MALLOC_STACK_TRACE; 930 return msan_reallocarray(ptr, nmemb, size, &stack); 931 } 932 933 INTERCEPTOR(void *, malloc, SIZE_T size) { 934 if (DlsymAlloc::Use()) 935 return DlsymAlloc::Allocate(size); 936 GET_MALLOC_STACK_TRACE; 937 return msan_malloc(size, &stack); 938 } 939 940 void __msan_allocated_memory(const void *data, uptr size) { 941 if (flags()->poison_in_malloc) { 942 GET_MALLOC_STACK_TRACE; 943 stack.tag = STACK_TRACE_TAG_POISON; 944 PoisonMemory(data, size, &stack); 945 } 946 } 947 948 void __msan_copy_shadow(void *dest, const void *src, uptr n) { 949 GET_STORE_STACK_TRACE; 950 MoveShadowAndOrigin(dest, src, n, &stack); 951 } 952 953 void __sanitizer_dtor_callback(const void *data, uptr size) { 954 if (flags()->poison_in_dtor) { 955 GET_MALLOC_STACK_TRACE; 956 stack.tag = STACK_TRACE_TAG_POISON; 957 PoisonMemory(data, size, &stack); 958 } 959 } 960 961 void __sanitizer_dtor_callback_fields(const void *data, uptr size) { 962 if (flags()->poison_in_dtor) { 963 GET_MALLOC_STACK_TRACE; 964 stack.tag = STACK_TRACE_TAG_FIELDS; 965 PoisonMemory(data, size, &stack); 966 } 967 } 968 969 void __sanitizer_dtor_callback_vptr(const void *data) { 970 if (flags()->poison_in_dtor) { 971 GET_MALLOC_STACK_TRACE; 972 stack.tag = STACK_TRACE_TAG_VPTR; 973 PoisonMemory(data, sizeof(void *), &stack); 974 } 975 } 976 977 template <class Mmap> 978 static void *mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length, 979 int prot, int flags, int fd, OFF64_T offset) { 980 SIZE_T rounded_length = RoundUpTo(length, GetPageSize()); 981 void *end_addr = (char *)addr + (rounded_length - 1); 982 if (addr && (!MEM_IS_APP(addr) || !MEM_IS_APP(end_addr))) { 983 if (flags & map_fixed) { 984 errno = errno_EINVAL; 985 return (void *)-1; 986 } else { 987 addr = nullptr; 988 } 989 } 990 void *res = real_mmap(addr, length, prot, flags, fd, offset); 991 if (res != (void *)-1) { 992 void *end_res = (char *)res + (rounded_length - 1); 993 if (MEM_IS_APP(res) && MEM_IS_APP(end_res)) { 994 __msan_unpoison(res, rounded_length); 995 } else { 996 // Application has attempted to map more memory than is supported by 997 // MSAN. Act as if we ran out of memory. 998 internal_munmap(res, length); 999 errno = errno_ENOMEM; 1000 return (void *)-1; 1001 } 1002 } 1003 return res; 1004 } 1005 1006 INTERCEPTOR(int, getrusage, int who, void *usage) { 1007 ENSURE_MSAN_INITED(); 1008 int res = REAL(getrusage)(who, usage); 1009 if (res == 0) { 1010 __msan_unpoison(usage, __sanitizer::struct_rusage_sz); 1011 } 1012 return res; 1013 } 1014 1015 class SignalHandlerScope { 1016 public: 1017 SignalHandlerScope() { 1018 if (MsanThread *t = GetCurrentThread()) 1019 t->EnterSignalHandler(); 1020 } 1021 ~SignalHandlerScope() { 1022 if (MsanThread *t = GetCurrentThread()) 1023 t->LeaveSignalHandler(); 1024 } 1025 }; 1026 1027 // sigactions_mu guarantees atomicity of sigaction() and signal() calls. 1028 // Access to sigactions[] is gone with relaxed atomics to avoid data race with 1029 // the signal handler. 1030 const int kMaxSignals = 1024; 1031 static atomic_uintptr_t sigactions[kMaxSignals]; 1032 static StaticSpinMutex sigactions_mu; 1033 1034 static void SignalHandler(int signo) { 1035 SignalHandlerScope signal_handler_scope; 1036 ScopedThreadLocalStateBackup stlsb; 1037 UnpoisonParam(1); 1038 1039 typedef void (*signal_cb)(int x); 1040 signal_cb cb = 1041 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed); 1042 cb(signo); 1043 } 1044 1045 static void SignalAction(int signo, void *si, void *uc) { 1046 SignalHandlerScope signal_handler_scope; 1047 ScopedThreadLocalStateBackup stlsb; 1048 UnpoisonParam(3); 1049 __msan_unpoison(si, sizeof(__sanitizer_sigaction)); 1050 __msan_unpoison(uc, ucontext_t_sz(uc)); 1051 1052 typedef void (*sigaction_cb)(int, void *, void *); 1053 sigaction_cb cb = 1054 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed); 1055 cb(signo, si, uc); 1056 CHECK_UNPOISONED(uc, ucontext_t_sz(uc)); 1057 } 1058 1059 static void read_sigaction(const __sanitizer_sigaction *act) { 1060 CHECK_UNPOISONED(&act->sa_flags, sizeof(act->sa_flags)); 1061 if (act->sa_flags & __sanitizer::sa_siginfo) 1062 CHECK_UNPOISONED(&act->sigaction, sizeof(act->sigaction)); 1063 else 1064 CHECK_UNPOISONED(&act->handler, sizeof(act->handler)); 1065 CHECK_UNPOISONED(&act->sa_mask, sizeof(act->sa_mask)); 1066 } 1067 1068 extern "C" int pthread_attr_init(void *attr); 1069 extern "C" int pthread_attr_destroy(void *attr); 1070 1071 static void *MsanThreadStartFunc(void *arg) { 1072 MsanThread *t = (MsanThread *)arg; 1073 SetCurrentThread(t); 1074 t->Init(); 1075 SetSigProcMask(&t->starting_sigset_, nullptr); 1076 return t->ThreadStart(); 1077 } 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) { 1084 pthread_attr_init(&myattr); 1085 attr = &myattr; 1086 } 1087 1088 AdjustStackSize(attr); 1089 1090 MsanThread *t = MsanThread::Create(callback, param); 1091 ScopedBlockSignals block(&t->starting_sigset_); 1092 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t); 1093 1094 if (attr == &myattr) 1095 pthread_attr_destroy(&myattr); 1096 if (!res) { 1097 __msan_unpoison(th, __sanitizer::pthread_t_sz); 1098 } 1099 return res; 1100 } 1101 1102 INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key, 1103 void (*dtor)(void *value)) { 1104 if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor); 1105 ENSURE_MSAN_INITED(); 1106 int res = REAL(pthread_key_create)(key, dtor); 1107 if (!res && key) 1108 __msan_unpoison(key, sizeof(*key)); 1109 return res; 1110 } 1111 1112 #if SANITIZER_NETBSD 1113 INTERCEPTOR(int, __libc_thr_keycreate, __sanitizer_pthread_key_t *m, 1114 void (*dtor)(void *value)) 1115 ALIAS(WRAP(pthread_key_create)); 1116 #endif 1117 1118 INTERCEPTOR(int, pthread_join, void *thread, void **retval) { 1119 ENSURE_MSAN_INITED(); 1120 int res = REAL(pthread_join)(thread, retval); 1121 if (!res && retval) 1122 __msan_unpoison(retval, sizeof(*retval)); 1123 return res; 1124 } 1125 1126 #if SANITIZER_GLIBC 1127 INTERCEPTOR(int, pthread_tryjoin_np, void *thread, void **retval) { 1128 ENSURE_MSAN_INITED(); 1129 int res = REAL(pthread_tryjoin_np)(thread, retval); 1130 if (!res && retval) 1131 __msan_unpoison(retval, sizeof(*retval)); 1132 return res; 1133 } 1134 1135 INTERCEPTOR(int, pthread_timedjoin_np, void *thread, void **retval, 1136 const struct timespec *abstime) { 1137 int res = REAL(pthread_timedjoin_np)(thread, retval, abstime); 1138 if (!res && retval) 1139 __msan_unpoison(retval, sizeof(*retval)); 1140 return res; 1141 } 1142 #endif 1143 1144 DEFINE_REAL_PTHREAD_FUNCTIONS 1145 1146 extern char *tzname[2]; 1147 1148 INTERCEPTOR(void, tzset, int fake) { 1149 ENSURE_MSAN_INITED(); 1150 InterceptorScope interceptor_scope; 1151 REAL(tzset)(fake); 1152 if (tzname[0]) 1153 __msan_unpoison(tzname[0], internal_strlen(tzname[0]) + 1); 1154 if (tzname[1]) 1155 __msan_unpoison(tzname[1], internal_strlen(tzname[1]) + 1); 1156 return; 1157 } 1158 1159 struct MSanAtExitRecord { 1160 void (*func)(void *arg); 1161 void *arg; 1162 }; 1163 1164 struct InterceptorContext { 1165 Mutex atexit_mu; 1166 Vector<struct MSanAtExitRecord *> AtExitStack; 1167 1168 InterceptorContext() 1169 : AtExitStack() { 1170 } 1171 }; 1172 1173 static ALIGNED(64) char interceptor_placeholder[sizeof(InterceptorContext)]; 1174 InterceptorContext *interceptor_ctx() { 1175 return reinterpret_cast<InterceptorContext*>(&interceptor_placeholder[0]); 1176 } 1177 1178 void MSanAtExitWrapper() { 1179 MSanAtExitRecord *r; 1180 { 1181 Lock l(&interceptor_ctx()->atexit_mu); 1182 1183 uptr element = interceptor_ctx()->AtExitStack.Size() - 1; 1184 r = interceptor_ctx()->AtExitStack[element]; 1185 interceptor_ctx()->AtExitStack.PopBack(); 1186 } 1187 1188 UnpoisonParam(1); 1189 ((void(*)())r->func)(); 1190 InternalFree(r); 1191 } 1192 1193 void MSanCxaAtExitWrapper(void *arg) { 1194 UnpoisonParam(1); 1195 MSanAtExitRecord *r = (MSanAtExitRecord *)arg; 1196 // libc before 2.27 had race which caused occasional double handler execution 1197 // https://sourceware.org/ml/libc-alpha/2017-08/msg01204.html 1198 if (!r->func) 1199 return; 1200 r->func(r->arg); 1201 r->func = nullptr; 1202 } 1203 1204 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso); 1205 1206 // Unpoison argument shadow for C++ module destructors. 1207 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg, 1208 void *dso_handle) { 1209 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle); 1210 return setup_at_exit_wrapper((void(*)())func, arg, dso_handle); 1211 } 1212 1213 // Unpoison argument shadow for C++ module destructors. 1214 INTERCEPTOR(int, atexit, void (*func)()) { 1215 // Avoid calling real atexit as it is unreachable on at least on Linux. 1216 if (msan_init_is_running) 1217 return REAL(__cxa_atexit)((void (*)(void *a))func, 0, 0); 1218 return setup_at_exit_wrapper((void(*)())func, 0, 0); 1219 } 1220 1221 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso) { 1222 ENSURE_MSAN_INITED(); 1223 MSanAtExitRecord *r = 1224 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord)); 1225 r->func = (void(*)(void *a))f; 1226 r->arg = arg; 1227 int res; 1228 if (!dso) { 1229 // NetBSD does not preserve the 2nd argument if dso is equal to 0 1230 // Store ctx in a local stack-like structure 1231 1232 Lock l(&interceptor_ctx()->atexit_mu); 1233 1234 res = REAL(__cxa_atexit)((void (*)(void *a))MSanAtExitWrapper, 0, 0); 1235 if (!res) { 1236 interceptor_ctx()->AtExitStack.PushBack(r); 1237 } 1238 } else { 1239 res = REAL(__cxa_atexit)(MSanCxaAtExitWrapper, r, dso); 1240 } 1241 return res; 1242 } 1243 1244 static void BeforeFork() { 1245 StackDepotLockAll(); 1246 ChainedOriginDepotLockAll(); 1247 } 1248 1249 static void AfterFork() { 1250 ChainedOriginDepotUnlockAll(); 1251 StackDepotUnlockAll(); 1252 } 1253 1254 INTERCEPTOR(int, fork, void) { 1255 ENSURE_MSAN_INITED(); 1256 BeforeFork(); 1257 int pid = REAL(fork)(); 1258 AfterFork(); 1259 return pid; 1260 } 1261 1262 // NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly 1263 // with MSan. 1264 #if SANITIZER_LINUX 1265 INTERCEPTOR(int, openpty, int *aparent, int *aworker, char *name, 1266 const void *termp, const void *winp) { 1267 ENSURE_MSAN_INITED(); 1268 InterceptorScope interceptor_scope; 1269 int res = REAL(openpty)(aparent, aworker, name, termp, winp); 1270 if (!res) { 1271 __msan_unpoison(aparent, sizeof(*aparent)); 1272 __msan_unpoison(aworker, sizeof(*aworker)); 1273 } 1274 return res; 1275 } 1276 #define MSAN_MAYBE_INTERCEPT_OPENPTY INTERCEPT_FUNCTION(openpty) 1277 #else 1278 #define MSAN_MAYBE_INTERCEPT_OPENPTY 1279 #endif 1280 1281 // NetBSD ships with forkpty(3) in -lutil, that needs to be prebuilt explicitly 1282 // with MSan. 1283 #if SANITIZER_LINUX 1284 INTERCEPTOR(int, forkpty, int *aparent, char *name, const void *termp, 1285 const void *winp) { 1286 ENSURE_MSAN_INITED(); 1287 InterceptorScope interceptor_scope; 1288 int res = REAL(forkpty)(aparent, name, termp, winp); 1289 if (res != -1) 1290 __msan_unpoison(aparent, sizeof(*aparent)); 1291 return res; 1292 } 1293 #define MSAN_MAYBE_INTERCEPT_FORKPTY INTERCEPT_FUNCTION(forkpty) 1294 #else 1295 #define MSAN_MAYBE_INTERCEPT_FORKPTY 1296 #endif 1297 1298 struct MSanInterceptorContext { 1299 bool in_interceptor_scope; 1300 }; 1301 1302 namespace __msan { 1303 1304 int OnExit() { 1305 // FIXME: ask frontend whether we need to return failure. 1306 return 0; 1307 } 1308 1309 } // namespace __msan 1310 1311 // A version of CHECK_UNPOISONED using a saved scope value. Used in common 1312 // interceptors. 1313 #define CHECK_UNPOISONED_CTX(ctx, x, n) \ 1314 do { \ 1315 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \ 1316 CHECK_UNPOISONED_0(x, n); \ 1317 } while (0) 1318 1319 #define MSAN_INTERCEPT_FUNC(name) \ 1320 do { \ 1321 if (!INTERCEPT_FUNCTION(name)) \ 1322 VReport(1, "MemorySanitizer: failed to intercept '%s'\n", #name); \ 1323 } while (0) 1324 1325 #define MSAN_INTERCEPT_FUNC_VER(name, ver) \ 1326 do { \ 1327 if (!INTERCEPT_FUNCTION_VER(name, ver)) \ 1328 VReport(1, "MemorySanitizer: failed to intercept '%s@@%s'\n", #name, \ 1329 ver); \ 1330 } while (0) 1331 #define MSAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \ 1332 do { \ 1333 if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \ 1334 VReport(1, "MemorySanitizer: failed to intercept '%s@@%s' or '%s'\n", \ 1335 #name, ver, #name); \ 1336 } while (0) 1337 1338 #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name) 1339 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \ 1340 MSAN_INTERCEPT_FUNC_VER(name, ver) 1341 #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \ 1342 MSAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) 1343 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \ 1344 UnpoisonParam(count) 1345 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ 1346 __msan_unpoison(ptr, size) 1347 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \ 1348 CHECK_UNPOISONED_CTX(ctx, ptr, size) 1349 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \ 1350 __msan_unpoison(ptr, size) 1351 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ 1352 if (msan_init_is_running) \ 1353 return REAL(func)(__VA_ARGS__); \ 1354 ENSURE_MSAN_INITED(); \ 1355 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \ 1356 ctx = (void *)&msan_ctx; \ 1357 (void)ctx; \ 1358 InterceptorScope interceptor_scope; \ 1359 __msan_unpoison(__errno_location(), sizeof(int)); 1360 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \ 1361 do { \ 1362 } while (false) 1363 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ 1364 do { \ 1365 } while (false) 1366 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ 1367 do { \ 1368 } while (false) 1369 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ 1370 do { \ 1371 } while (false) 1372 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \ 1373 do { \ 1374 } while (false) // FIXME 1375 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \ 1376 do { \ 1377 } while (false) // FIXME 1378 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name) 1379 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit() 1380 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \ 1381 do { \ 1382 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \ 1383 if (filename && map) \ 1384 ForEachMappedRegion(map, __msan_unpoison); \ 1385 } while (false) 1386 1387 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!msan_inited) 1388 1389 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ 1390 if (MsanThread *t = GetCurrentThread()) { \ 1391 *begin = t->tls_begin(); \ 1392 *end = t->tls_end(); \ 1393 } else { \ 1394 *begin = *end = 0; \ 1395 } 1396 1397 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \ 1398 { \ 1399 (void)ctx; \ 1400 return __msan_memset(block, c, size); \ 1401 } 1402 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \ 1403 { \ 1404 (void)ctx; \ 1405 return __msan_memmove(to, from, size); \ 1406 } 1407 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \ 1408 { \ 1409 (void)ctx; \ 1410 return __msan_memcpy(to, from, size); \ 1411 } 1412 1413 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \ 1414 do { \ 1415 GET_STORE_STACK_TRACE; \ 1416 CopyShadowAndOrigin(to, from, size, &stack); \ 1417 __msan_unpoison(to + size, 1); \ 1418 } while (false) 1419 1420 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, fd, \ 1421 offset) \ 1422 do { \ 1423 return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \ 1424 } while (false) 1425 1426 #include "sanitizer_common/sanitizer_platform_interceptors.h" 1427 #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc" 1428 #include "sanitizer_common/sanitizer_common_interceptors.inc" 1429 1430 static uptr signal_impl(int signo, uptr cb); 1431 static int sigaction_impl(int signo, const __sanitizer_sigaction *act, 1432 __sanitizer_sigaction *oldact); 1433 1434 #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \ 1435 { return sigaction_impl(signo, act, oldact); } 1436 1437 #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \ 1438 { \ 1439 handler = signal_impl(signo, handler); \ 1440 InterceptorScope interceptor_scope; \ 1441 return REAL(func)(signo, handler); \ 1442 } 1443 1444 #define SIGNAL_INTERCEPTOR_ENTER() ENSURE_MSAN_INITED() 1445 1446 #include "sanitizer_common/sanitizer_signal_interceptors.inc" 1447 1448 static int sigaction_impl(int signo, const __sanitizer_sigaction *act, 1449 __sanitizer_sigaction *oldact) { 1450 ENSURE_MSAN_INITED(); 1451 if (signo <= 0 || signo >= kMaxSignals) { 1452 errno = errno_EINVAL; 1453 return -1; 1454 } 1455 if (act) read_sigaction(act); 1456 int res; 1457 if (flags()->wrap_signals) { 1458 SpinMutexLock lock(&sigactions_mu); 1459 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed); 1460 __sanitizer_sigaction new_act; 1461 __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr; 1462 if (act) { 1463 REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction)); 1464 uptr cb = (uptr)pnew_act->sigaction; 1465 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo) 1466 ? (uptr)SignalAction 1467 : (uptr)SignalHandler; 1468 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { 1469 atomic_store(&sigactions[signo], cb, memory_order_relaxed); 1470 pnew_act->sigaction = (decltype(pnew_act->sigaction))new_cb; 1471 } 1472 } 1473 res = REAL(SIGACTION_SYMNAME)(signo, pnew_act, oldact); 1474 if (res == 0 && oldact) { 1475 uptr cb = (uptr)oldact->sigaction; 1476 if (cb == (uptr)SignalAction || cb == (uptr)SignalHandler) { 1477 oldact->sigaction = (decltype(oldact->sigaction))old_cb; 1478 } 1479 } 1480 } else { 1481 res = REAL(SIGACTION_SYMNAME)(signo, act, oldact); 1482 } 1483 1484 if (res == 0 && oldact) { 1485 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction)); 1486 } 1487 return res; 1488 } 1489 1490 static uptr signal_impl(int signo, uptr cb) { 1491 ENSURE_MSAN_INITED(); 1492 if (signo <= 0 || signo >= kMaxSignals) { 1493 errno = errno_EINVAL; 1494 return -1; 1495 } 1496 if (flags()->wrap_signals) { 1497 SpinMutexLock lock(&sigactions_mu); 1498 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { 1499 atomic_store(&sigactions[signo], cb, memory_order_relaxed); 1500 cb = (uptr)&SignalHandler; 1501 } 1502 } 1503 return cb; 1504 } 1505 1506 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s) 1507 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \ 1508 do { \ 1509 } while (false) 1510 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \ 1511 do { \ 1512 } while (false) 1513 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s) 1514 #include "sanitizer_common/sanitizer_common_syscalls.inc" 1515 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc" 1516 1517 INTERCEPTOR(const char *, strsignal, int sig) { 1518 void *ctx; 1519 COMMON_INTERCEPTOR_ENTER(ctx, strsignal, sig); 1520 const char *res = REAL(strsignal)(sig); 1521 if (res) 1522 __msan_unpoison(res, internal_strlen(res) + 1); 1523 return res; 1524 } 1525 1526 INTERCEPTOR(int, dladdr, void *addr, void *info) { 1527 void *ctx; 1528 COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info); 1529 int res = REAL(dladdr)(addr, info); 1530 if (res != 0) 1531 UnpoisonDllAddrInfo(info); 1532 return res; 1533 } 1534 1535 #if SANITIZER_GLIBC 1536 INTERCEPTOR(int, dladdr1, void *addr, void *info, void **extra_info, 1537 int flags) { 1538 void *ctx; 1539 COMMON_INTERCEPTOR_ENTER(ctx, dladdr1, addr, info, extra_info, flags); 1540 int res = REAL(dladdr1)(addr, info, extra_info, flags); 1541 if (res != 0) { 1542 UnpoisonDllAddrInfo(info); 1543 UnpoisonDllAddr1ExtraInfo(extra_info, flags); 1544 } 1545 return res; 1546 } 1547 # define MSAN_MAYBE_INTERCEPT_DLADDR1 MSAN_INTERCEPT_FUNC(dladdr1) 1548 #else 1549 #define MSAN_MAYBE_INTERCEPT_DLADDR1 1550 #endif 1551 1552 INTERCEPTOR(char *, dlerror, int fake) { 1553 void *ctx; 1554 COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake); 1555 char *res = REAL(dlerror)(fake); 1556 if (res) 1557 __msan_unpoison(res, internal_strlen(res) + 1); 1558 return res; 1559 } 1560 1561 typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size, 1562 void *data); 1563 struct dl_iterate_phdr_data { 1564 dl_iterate_phdr_cb callback; 1565 void *data; 1566 }; 1567 1568 static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size, 1569 void *data) { 1570 if (info) { 1571 __msan_unpoison(info, size); 1572 if (info->dlpi_phdr && info->dlpi_phnum) 1573 __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum); 1574 if (info->dlpi_name) 1575 __msan_unpoison(info->dlpi_name, internal_strlen(info->dlpi_name) + 1); 1576 } 1577 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data; 1578 UnpoisonParam(3); 1579 return cbdata->callback(info, size, cbdata->data); 1580 } 1581 1582 INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) { 1583 ENSURE_MSAN_INITED(); 1584 void *p = REAL(shmat)(shmid, shmaddr, shmflg); 1585 if (p != (void *)-1) { 1586 __sanitizer_shmid_ds ds; 1587 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds); 1588 if (!res) { 1589 __msan_unpoison(p, ds.shm_segsz); 1590 } 1591 } 1592 return p; 1593 } 1594 1595 INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) { 1596 void *ctx; 1597 COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data); 1598 dl_iterate_phdr_data cbdata; 1599 cbdata.callback = callback; 1600 cbdata.data = data; 1601 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata); 1602 return res; 1603 } 1604 1605 // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc); 1606 INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) { 1607 ENSURE_MSAN_INITED(); 1608 wchar_t *res = REAL(wcschr)(s, wc, ps); 1609 return res; 1610 } 1611 1612 // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src); 1613 INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) { 1614 ENSURE_MSAN_INITED(); 1615 GET_STORE_STACK_TRACE; 1616 wchar_t *res = REAL(wcscpy)(dest, src); 1617 CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (internal_wcslen(src) + 1), 1618 &stack); 1619 return res; 1620 } 1621 1622 INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src, SIZE_T n) { 1623 ENSURE_MSAN_INITED(); 1624 GET_STORE_STACK_TRACE; 1625 SIZE_T copy_size = internal_wcsnlen(src, n); 1626 if (copy_size < n) copy_size++; // trailing \0 1627 wchar_t *res = REAL(wcsncpy)(dest, src, n); 1628 CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack); 1629 __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t)); 1630 return res; 1631 } 1632 1633 // These interface functions reside here so that they can use 1634 // REAL(memset), etc. 1635 void __msan_unpoison(const void *a, uptr size) { 1636 if (!MEM_IS_APP(a)) return; 1637 SetShadow(a, size, 0); 1638 } 1639 1640 void __msan_poison(const void *a, uptr size) { 1641 if (!MEM_IS_APP(a)) return; 1642 SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1); 1643 } 1644 1645 void __msan_poison_stack(void *a, uptr size) { 1646 if (!MEM_IS_APP(a)) return; 1647 SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1); 1648 } 1649 1650 void __msan_unpoison_param(uptr n) { UnpoisonParam(n); } 1651 1652 void __msan_clear_and_unpoison(void *a, uptr size) { 1653 REAL(memset)(a, 0, size); 1654 SetShadow(a, size, 0); 1655 } 1656 1657 void *__msan_memcpy(void *dest, const void *src, SIZE_T n) { 1658 if (!msan_inited) return internal_memcpy(dest, src, n); 1659 if (msan_init_is_running || __msan::IsInSymbolizerOrUnwider()) 1660 return REAL(memcpy)(dest, src, n); 1661 ENSURE_MSAN_INITED(); 1662 GET_STORE_STACK_TRACE; 1663 void *res = REAL(memcpy)(dest, src, n); 1664 CopyShadowAndOrigin(dest, src, n, &stack); 1665 return res; 1666 } 1667 1668 void *__msan_memset(void *s, int c, SIZE_T n) { 1669 if (!msan_inited) return internal_memset(s, c, n); 1670 if (msan_init_is_running) return REAL(memset)(s, c, n); 1671 ENSURE_MSAN_INITED(); 1672 void *res = REAL(memset)(s, c, n); 1673 __msan_unpoison(s, n); 1674 return res; 1675 } 1676 1677 void *__msan_memmove(void *dest, const void *src, SIZE_T n) { 1678 if (!msan_inited) return internal_memmove(dest, src, n); 1679 if (msan_init_is_running) return REAL(memmove)(dest, src, n); 1680 ENSURE_MSAN_INITED(); 1681 GET_STORE_STACK_TRACE; 1682 void *res = REAL(memmove)(dest, src, n); 1683 MoveShadowAndOrigin(dest, src, n, &stack); 1684 return res; 1685 } 1686 1687 void __msan_unpoison_string(const char* s) { 1688 if (!MEM_IS_APP(s)) return; 1689 __msan_unpoison(s, internal_strlen(s) + 1); 1690 } 1691 1692 namespace __msan { 1693 1694 void InitializeInterceptors() { 1695 static int inited = 0; 1696 CHECK_EQ(inited, 0); 1697 1698 new(interceptor_ctx()) InterceptorContext(); 1699 1700 InitializeCommonInterceptors(); 1701 InitializeSignalInterceptors(); 1702 1703 INTERCEPT_FUNCTION(posix_memalign); 1704 MSAN_MAYBE_INTERCEPT_MEMALIGN; 1705 MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN; 1706 INTERCEPT_FUNCTION(valloc); 1707 MSAN_MAYBE_INTERCEPT_PVALLOC; 1708 INTERCEPT_FUNCTION(malloc); 1709 INTERCEPT_FUNCTION(calloc); 1710 INTERCEPT_FUNCTION(realloc); 1711 INTERCEPT_FUNCTION(reallocarray); 1712 INTERCEPT_FUNCTION(free); 1713 MSAN_MAYBE_INTERCEPT_CFREE; 1714 MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE; 1715 MSAN_MAYBE_INTERCEPT_MALLINFO; 1716 MSAN_MAYBE_INTERCEPT_MALLOPT; 1717 MSAN_MAYBE_INTERCEPT_MALLOC_STATS; 1718 INTERCEPT_FUNCTION(fread); 1719 MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED; 1720 INTERCEPT_FUNCTION(memccpy); 1721 MSAN_MAYBE_INTERCEPT_MEMPCPY; 1722 INTERCEPT_FUNCTION(bcopy); 1723 INTERCEPT_FUNCTION(wmemset); 1724 INTERCEPT_FUNCTION(wmemcpy); 1725 MSAN_MAYBE_INTERCEPT_WMEMPCPY; 1726 INTERCEPT_FUNCTION(wmemmove); 1727 INTERCEPT_FUNCTION(strcpy); 1728 MSAN_MAYBE_INTERCEPT_STPCPY; 1729 MSAN_MAYBE_INTERCEPT_STPNCPY; 1730 INTERCEPT_FUNCTION(strdup); 1731 MSAN_MAYBE_INTERCEPT___STRDUP; 1732 INTERCEPT_FUNCTION(strncpy); 1733 MSAN_MAYBE_INTERCEPT_GCVT; 1734 INTERCEPT_FUNCTION(strcat); 1735 INTERCEPT_FUNCTION(strncat); 1736 INTERCEPT_STRTO(strtod); 1737 INTERCEPT_STRTO(strtof); 1738 INTERCEPT_STRTO(strtold); 1739 INTERCEPT_STRTO(strtol); 1740 INTERCEPT_STRTO(strtoul); 1741 INTERCEPT_STRTO(strtoll); 1742 INTERCEPT_STRTO(strtoull); 1743 INTERCEPT_STRTO(strtouq); 1744 INTERCEPT_STRTO(wcstod); 1745 INTERCEPT_STRTO(wcstof); 1746 INTERCEPT_STRTO(wcstold); 1747 INTERCEPT_STRTO(wcstol); 1748 INTERCEPT_STRTO(wcstoul); 1749 INTERCEPT_STRTO(wcstoll); 1750 INTERCEPT_STRTO(wcstoull); 1751 #ifdef SANITIZER_NLDBL_VERSION 1752 INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION); 1753 INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION); 1754 #else 1755 INTERCEPT_FUNCTION(vswprintf); 1756 INTERCEPT_FUNCTION(swprintf); 1757 #endif 1758 INTERCEPT_FUNCTION(strftime); 1759 INTERCEPT_FUNCTION(strftime_l); 1760 MSAN_MAYBE_INTERCEPT___STRFTIME_L; 1761 INTERCEPT_FUNCTION(wcsftime); 1762 INTERCEPT_FUNCTION(wcsftime_l); 1763 MSAN_MAYBE_INTERCEPT___WCSFTIME_L; 1764 INTERCEPT_FUNCTION(mbtowc); 1765 INTERCEPT_FUNCTION(mbrtowc); 1766 INTERCEPT_FUNCTION(wcslen); 1767 INTERCEPT_FUNCTION(wcsnlen); 1768 INTERCEPT_FUNCTION(wcschr); 1769 INTERCEPT_FUNCTION(wcscpy); 1770 INTERCEPT_FUNCTION(wcsncpy); 1771 INTERCEPT_FUNCTION(wcscmp); 1772 INTERCEPT_FUNCTION(getenv); 1773 INTERCEPT_FUNCTION(setenv); 1774 INTERCEPT_FUNCTION(putenv); 1775 INTERCEPT_FUNCTION(gettimeofday); 1776 MSAN_MAYBE_INTERCEPT_FCVT; 1777 MSAN_MAYBE_INTERCEPT_FSTAT; 1778 MSAN_MAYBE_INTERCEPT_FSTAT64; 1779 MSAN_MAYBE_INTERCEPT___FXSTAT; 1780 MSAN_MAYBE_INTERCEPT_FSTATAT; 1781 MSAN_MAYBE_INTERCEPT_FSTATAT64; 1782 MSAN_MAYBE_INTERCEPT___FXSTATAT; 1783 MSAN_MAYBE_INTERCEPT___FXSTAT64; 1784 MSAN_MAYBE_INTERCEPT___FXSTATAT64; 1785 INTERCEPT_FUNCTION(pipe); 1786 INTERCEPT_FUNCTION(pipe2); 1787 INTERCEPT_FUNCTION(socketpair); 1788 MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED; 1789 INTERCEPT_FUNCTION(getrlimit); 1790 MSAN_MAYBE_INTERCEPT___GETRLIMIT; 1791 MSAN_MAYBE_INTERCEPT_GETRLIMIT64; 1792 MSAN_MAYBE_INTERCEPT_PRLIMIT; 1793 MSAN_MAYBE_INTERCEPT_PRLIMIT64; 1794 INTERCEPT_FUNCTION(gethostname); 1795 MSAN_MAYBE_INTERCEPT_EPOLL_WAIT; 1796 MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT; 1797 INTERCEPT_FUNCTION(strsignal); 1798 INTERCEPT_FUNCTION(dladdr); 1799 MSAN_MAYBE_INTERCEPT_DLADDR1; 1800 INTERCEPT_FUNCTION(dlerror); 1801 INTERCEPT_FUNCTION(dl_iterate_phdr); 1802 INTERCEPT_FUNCTION(getrusage); 1803 #if defined(__mips__) 1804 INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2"); 1805 #else 1806 INTERCEPT_FUNCTION(pthread_create); 1807 #endif 1808 INTERCEPT_FUNCTION(pthread_join); 1809 INTERCEPT_FUNCTION(pthread_key_create); 1810 #if SANITIZER_GLIBC 1811 INTERCEPT_FUNCTION(pthread_tryjoin_np); 1812 INTERCEPT_FUNCTION(pthread_timedjoin_np); 1813 #endif 1814 1815 #if SANITIZER_NETBSD 1816 INTERCEPT_FUNCTION(__libc_thr_keycreate); 1817 #endif 1818 1819 INTERCEPT_FUNCTION(pthread_join); 1820 INTERCEPT_FUNCTION(tzset); 1821 INTERCEPT_FUNCTION(atexit); 1822 INTERCEPT_FUNCTION(__cxa_atexit); 1823 INTERCEPT_FUNCTION(shmat); 1824 INTERCEPT_FUNCTION(fork); 1825 MSAN_MAYBE_INTERCEPT_OPENPTY; 1826 MSAN_MAYBE_INTERCEPT_FORKPTY; 1827 1828 inited = 1; 1829 } 1830 } // namespace __msan 1831