msan_interceptors.cc revision 5cf2c460e96e593b1c772f1b02d3a217f4837fdc
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. 13// 14// FIXME: move as many interceptors as possible into 15// sanitizer_common/sanitizer_common_interceptors.h 16//===----------------------------------------------------------------------===// 17 18#include "interception/interception.h" 19#include "msan.h" 20#include "sanitizer_common/sanitizer_platform_limits_posix.h" 21#include "sanitizer_common/sanitizer_allocator.h" 22#include "sanitizer_common/sanitizer_allocator_internal.h" 23#include "sanitizer_common/sanitizer_atomic.h" 24#include "sanitizer_common/sanitizer_common.h" 25#include "sanitizer_common/sanitizer_stackdepot.h" 26#include "sanitizer_common/sanitizer_libc.h" 27#include "sanitizer_common/sanitizer_linux.h" 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 33using namespace __msan; 34 35using __sanitizer::memory_order; 36using __sanitizer::atomic_load; 37using __sanitizer::atomic_store; 38using __sanitizer::atomic_uintptr_t; 39 40static unsigned g_thread_finalize_key; 41 42// True if this is a nested interceptor. 43static THREADLOCAL int in_interceptor_scope; 44 45struct InterceptorScope { 46 InterceptorScope() { ++in_interceptor_scope; } 47 ~InterceptorScope() { --in_interceptor_scope; } 48}; 49 50bool IsInInterceptorScope() { 51 return in_interceptor_scope; 52} 53 54#define ENSURE_MSAN_INITED() do { \ 55 CHECK(!msan_init_is_running); \ 56 if (!msan_inited) { \ 57 __msan_init(); \ 58 } \ 59} while (0) 60 61// Check that [x, x+n) range is unpoisoned. 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)); \ 73 if (__msan::flags()->halt_on_error) { \ 74 Printf("Exiting\n"); \ 75 Die(); \ 76 } \ 77 } \ 78 } while (0) 79 80// Check that [x, x+n) range is unpoisoned unless we are in a nested 81// interceptor. 82#define CHECK_UNPOISONED(x, n) \ 83 do { \ 84 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \ 85 } while (0); 86 87static void *fast_memset(void *ptr, int c, SIZE_T n); 88static void *fast_memcpy(void *dst, const void *src, SIZE_T n); 89 90INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) { 91 ENSURE_MSAN_INITED(); 92 SIZE_T res = REAL(fread)(ptr, size, nmemb, file); 93 if (res > 0) 94 __msan_unpoison(ptr, res *size); 95 return res; 96} 97 98INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb, 99 void *file) { 100 ENSURE_MSAN_INITED(); 101 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file); 102 if (res > 0) 103 __msan_unpoison(ptr, res *size); 104 return res; 105} 106 107INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) { 108 ENSURE_MSAN_INITED(); 109 SSIZE_T res = REAL(readlink)(path, buf, bufsiz); 110 if (res > 0) 111 __msan_unpoison(buf, res); 112 return res; 113} 114 115INTERCEPTOR(void *, memcpy, void *dest, const void *src, SIZE_T n) { 116 return __msan_memcpy(dest, src, n); 117} 118 119INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) { 120 return (char *)__msan_memcpy(dest, src, n) + n; 121} 122 123INTERCEPTOR(void *, memmove, void *dest, const void *src, SIZE_T n) { 124 return __msan_memmove(dest, src, n); 125} 126 127INTERCEPTOR(void *, memset, void *s, int c, SIZE_T n) { 128 return __msan_memset(s, c, n); 129} 130 131INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) { 132 return __msan_memmove(dest, src, n); 133} 134 135INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) { 136 GET_MALLOC_STACK_TRACE; 137 CHECK_EQ(alignment & (alignment - 1), 0); 138 CHECK_NE(memptr, 0); 139 *memptr = MsanReallocate(&stack, 0, size, alignment, false); 140 CHECK_NE(*memptr, 0); 141 __msan_unpoison(memptr, sizeof(*memptr)); 142 return 0; 143} 144 145INTERCEPTOR(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 152INTERCEPTOR(void *, valloc, SIZE_T size) { 153 GET_MALLOC_STACK_TRACE; 154 void *ptr = MsanReallocate(&stack, 0, size, GetPageSizeCached(), false); 155 return ptr; 156} 157 158INTERCEPTOR(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 170INTERCEPTOR(void, free, void *ptr) { 171 GET_MALLOC_STACK_TRACE; 172 if (ptr == 0) return; 173 MsanDeallocate(&stack, ptr); 174} 175 176INTERCEPTOR(SIZE_T, strlen, const char *s) { 177 ENSURE_MSAN_INITED(); 178 SIZE_T res = REAL(strlen)(s); 179 CHECK_UNPOISONED(s, res + 1); 180 return res; 181} 182 183INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T n) { 184 ENSURE_MSAN_INITED(); 185 SIZE_T res = REAL(strnlen)(s, n); 186 SIZE_T scan_size = (res == n) ? res : res + 1; 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 194INTERCEPTOR(char *, strcpy, char *dest, const char *src) { // NOLINT 195 ENSURE_MSAN_INITED(); 196 SIZE_T n = REAL(strlen)(src); 197 char *res = REAL(strcpy)(dest, src); // NOLINT 198 __msan_copy_poison(dest, src, n + 1); 199 return res; 200} 201 202INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { // NOLINT 203 ENSURE_MSAN_INITED(); 204 SIZE_T copy_size = REAL(strnlen)(src, n); 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 212INTERCEPTOR(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 220INTERCEPTOR(char *, strdup, char *src) { 221 ENSURE_MSAN_INITED(); 222 SIZE_T n = REAL(strlen)(src); 223 char *res = REAL(strdup)(src); 224 __msan_copy_poison(res, src, n + 1); 225 return res; 226} 227 228INTERCEPTOR(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 236INTERCEPTOR(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 245INTERCEPTOR(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 254INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) { 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()) { 259 SIZE_T n = REAL(strlen)(buf); 260 __msan_unpoison(buf, n + 1); 261 } 262 return res; 263} 264 265INTERCEPTOR(char *, strcat, char *dest, const char *src) { // NOLINT 266 ENSURE_MSAN_INITED(); 267 SIZE_T src_size = REAL(strlen)(src); 268 SIZE_T dest_size = REAL(strlen)(dest); 269 char *res = REAL(strcat)(dest, src); // NOLINT 270 __msan_copy_poison(dest + dest_size, src, src_size + 1); 271 return res; 272} 273 274INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { // NOLINT 275 ENSURE_MSAN_INITED(); 276 SIZE_T dest_size = REAL(strlen)(dest); 277 SIZE_T copy_size = REAL(strlen)(src); 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 285INTERCEPTOR(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 295INTERCEPTOR(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 305INTERCEPTOR(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 315INTERCEPTOR(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 325INTERCEPTOR(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 334INTERCEPTOR(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 344INTERCEPTOR(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 354INTERCEPTOR(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 363INTERCEPTOR(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 373INTERCEPTOR(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 383INTERCEPTOR(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 392INTERCEPTOR(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 402INTERCEPTOR(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 412INTERCEPTOR(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 422INTERCEPTOR(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 431INTERCEPTOR(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); 435 if (res >= 0 && !__msan_has_dynamic_component()) { 436 __msan_unpoison(str, res + 1); 437 } 438 return res; 439} 440 441INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap) { 442 ENSURE_MSAN_INITED(); 443 int res = REAL(vsprintf)(str, format, ap); 444 if (res >= 0 && !__msan_has_dynamic_component()) { 445 __msan_unpoison(str, res + 1); 446 } 447 return res; 448} 449 450INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) { 451 ENSURE_MSAN_INITED(); 452 int res = REAL(vswprintf)(str, size, format, ap); 453 if (res >= 0 && !__msan_has_dynamic_component()) { 454 __msan_unpoison(str, 4 * (res + 1)); 455 } 456 return res; 457} 458 459INTERCEPTOR(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 468INTERCEPTOR(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 477INTERCEPTOR(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 486// SIZE_T strftime(char *s, SIZE_T max, const char *format,const struct tm *tm); 487INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format, 488 void *tm) { 489 ENSURE_MSAN_INITED(); 490 SIZE_T res = REAL(strftime)(s, max, format, tm); 491 if (res) __msan_unpoison(s, res + 1); 492 return res; 493} 494 495INTERCEPTOR(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 502INTERCEPTOR(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 509INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) { 510 ENSURE_MSAN_INITED(); 511 SIZE_T res = REAL(wcslen)(s); 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); 517INTERCEPTOR(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); 524INTERCEPTOR(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 531// wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n); 532INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) { 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 539INTERCEPTOR(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 546INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) { 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 554INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) { 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 561INTERCEPTOR(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 567INTERCEPTOR(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 574INTERCEPTOR(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 584INTERCEPTOR(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 594INTERCEPTOR(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 604extern char **environ; 605 606static 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 616INTERCEPTOR(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 623INTERCEPTOR(int, putenv, char *string) { 624 ENSURE_MSAN_INITED(); 625 int res = REAL(putenv)(string); 626 if (!res) UnpoisonEnviron(); 627 return res; 628} 629 630INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) { 631 ENSURE_MSAN_INITED(); 632 int res = REAL(__fxstat)(magic, fd, buf); 633 if (!res) 634 __msan_unpoison(buf, __sanitizer::struct_stat_sz); 635 return res; 636} 637 638INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) { 639 ENSURE_MSAN_INITED(); 640 int res = REAL(__fxstat64)(magic, fd, buf); 641 if (!res) 642 __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 643 return res; 644} 645 646INTERCEPTOR(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 654INTERCEPTOR(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 662INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) { 663 ENSURE_MSAN_INITED(); 664 int res = REAL(__xstat)(magic, path, buf); 665 if (!res) 666 __msan_unpoison(buf, __sanitizer::struct_stat_sz); 667 return res; 668} 669 670INTERCEPTOR(int, __xstat64, int magic, char *path, void *buf) { 671 ENSURE_MSAN_INITED(); 672 int res = REAL(__xstat64)(magic, path, buf); 673 if (!res) 674 __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 675 return res; 676} 677 678INTERCEPTOR(int, __lxstat, int magic, char *path, void *buf) { 679 ENSURE_MSAN_INITED(); 680 int res = REAL(__lxstat)(magic, path, buf); 681 if (!res) 682 __msan_unpoison(buf, __sanitizer::struct_stat_sz); 683 return res; 684} 685 686INTERCEPTOR(int, __lxstat64, int magic, char *path, void *buf) { 687 ENSURE_MSAN_INITED(); 688 int res = REAL(__lxstat64)(magic, path, buf); 689 if (!res) 690 __msan_unpoison(buf, __sanitizer::struct_stat64_sz); 691 return res; 692} 693 694INTERCEPTOR(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 704INTERCEPTOR(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 712INTERCEPTOR(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 720INTERCEPTOR(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 728INTERCEPTOR(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 736INTERCEPTOR(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) 742 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz); 743 return res; 744} 745 746INTERCEPTOR(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) 752 __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz); 753 return res; 754} 755 756INTERCEPTOR(int, uname, void *utsname) { 757 ENSURE_MSAN_INITED(); 758 int res = REAL(uname)(utsname); 759 if (!res) { 760 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz); 761 } 762 return res; 763} 764 765INTERCEPTOR(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 777INTERCEPTOR(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) { 782 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res); 783 } 784 return res; 785} 786 787INTERCEPTOR(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) { 792 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res); 793 } 794 return res; 795} 796 797INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) { 798 ENSURE_MSAN_INITED(); 799 SSIZE_T res = REAL(recv)(fd, buf, len, flags); 800 if (res > 0) 801 __msan_unpoison(buf, res); 802 return res; 803} 804 805INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags, 806 void *srcaddr, int *addrlen) { 807 ENSURE_MSAN_INITED(); 808 SIZE_T srcaddr_sz; 809 if (srcaddr) srcaddr_sz = *addrlen; 810 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen); 811 if (res > 0) { 812 __msan_unpoison(buf, res); 813 if (srcaddr) { 814 SIZE_T sz = *addrlen; 815 __msan_unpoison(srcaddr, (sz < srcaddr_sz) ? sz : srcaddr_sz); 816 } 817 } 818 return res; 819} 820 821INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) { 822 if (CallocShouldReturnNullDueToOverflow(size, nmemb)) 823 return AllocatorReturnNull(); 824 GET_MALLOC_STACK_TRACE; 825 if (!msan_inited) { 826 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym. 827 const SIZE_T kCallocPoolSize = 1024; 828 static uptr calloc_memory_for_dlsym[kCallocPoolSize]; 829 static SIZE_T allocated; 830 SIZE_T size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize; 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 839INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) { 840 GET_MALLOC_STACK_TRACE; 841 return MsanReallocate(&stack, ptr, size, sizeof(u64), false); 842} 843 844INTERCEPTOR(void *, malloc, SIZE_T size) { 845 GET_MALLOC_STACK_TRACE; 846 return MsanReallocate(&stack, 0, size, sizeof(u64), false); 847} 848 849void __msan_allocated_memory(const void* data, uptr size) { 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 861INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags, 862 int fd, OFF_T offset) { 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 870INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags, 871 int fd, OFF64_T offset) { 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 879struct dlinfo { 880 char *dli_fname; 881 void *dli_fbase; 882 char *dli_sname; 883 void *dli_saddr; 884}; 885 886INTERCEPTOR(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 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. 905INTERCEPTOR(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(); 910 if (!__msan_has_dynamic_component() && map) { 911 // If msandr didn't clear the shadow before the initializers ran, we do it 912 // ourselves afterwards. 913 ForEachMappedRegion(map, __msan_unpoison); 914 } 915 return (void *)map; 916} 917 918typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size, 919 void *data); 920struct dl_iterate_phdr_data { 921 dl_iterate_phdr_cb callback; 922 void *data; 923}; 924 925static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size, 926 void *data) { 927 if (info) { 928 __msan_unpoison(info, size); 929 if (info->dlpi_name) 930 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1); 931 } 932 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data; 933 UnpoisonParam(3); 934 return cbdata->callback(info, size, cbdata->data); 935} 936 937INTERCEPTOR(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 948INTERCEPTOR(int, getrusage, int who, void *usage) { 949 ENSURE_MSAN_INITED(); 950 int res = REAL(getrusage)(who, usage); 951 if (res == 0) { 952 __msan_unpoison(usage, __sanitizer::struct_rusage_sz); 953 } 954 return res; 955} 956 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. 960const int kMaxSignals = 1024; 961static atomic_uintptr_t sigactions[kMaxSignals]; 962static StaticSpinMutex sigactions_mu; 963 964static void SignalHandler(int signo) { 965 ScopedThreadLocalStateBackup stlsb; 966 UnpoisonParam(1); 967 968 typedef void (*signal_cb)(int x); 969 signal_cb cb = 970 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed); 971 cb(signo); 972} 973 974static void SignalAction(int signo, void *si, void *uc) { 975 ScopedThreadLocalStateBackup stlsb; 976 UnpoisonParam(3); 977 __msan_unpoison(si, sizeof(__sanitizer_sigaction)); 978 __msan_unpoison(uc, __sanitizer::ucontext_t_sz); 979 980 typedef void (*sigaction_cb)(int, void *, void *); 981 sigaction_cb cb = 982 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed); 983 cb(signo, si, uc); 984} 985 986INTERCEPTOR(int, sigaction, int signo, const __sanitizer_sigaction *act, 987 __sanitizer_sigaction *oldact) { 988 ENSURE_MSAN_INITED(); 989 // FIXME: check that *act is unpoisoned. 990 // That requires intercepting all of sigemptyset, sigfillset, etc. 991 int res; 992 if (flags()->wrap_signals) { 993 SpinMutexLock lock(&sigactions_mu); 994 CHECK_LT(signo, kMaxSignals); 995 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed); 996 __sanitizer_sigaction new_act; 997 __sanitizer_sigaction *pnew_act = act ? &new_act : 0; 998 if (act) { 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; 1004 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { 1005 atomic_store(&sigactions[signo], cb, memory_order_relaxed); 1006 pnew_act->sa_sigaction = (void (*)(int, void *, void *))new_cb; 1007 } 1008 } 1009 res = REAL(sigaction)(signo, pnew_act, oldact); 1010 if (res == 0 && oldact) { 1011 uptr cb = (uptr)oldact->sa_sigaction; 1012 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) { 1013 oldact->sa_sigaction = (void (*)(int, void *, void *))old_cb; 1014 } 1015 } 1016 } else { 1017 res = REAL(sigaction)(signo, act, oldact); 1018 } 1019 1020 if (res == 0 && oldact) { 1021 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction)); 1022 } 1023 return res; 1024} 1025 1026INTERCEPTOR(int, signal, int signo, uptr cb) { 1027 ENSURE_MSAN_INITED(); 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) { 1032 atomic_store(&sigactions[signo], cb, memory_order_relaxed); 1033 cb = (uptr) SignalHandler; 1034 } 1035 return REAL(signal)(signo, cb); 1036 } else { 1037 return REAL(signal)(signo, cb); 1038 } 1039} 1040 1041extern "C" int pthread_attr_init(void *attr); 1042extern "C" int pthread_attr_destroy(void *attr); 1043extern "C" int pthread_setspecific(unsigned key, const void *v); 1044extern "C" int pthread_yield(); 1045 1046static void thread_finalize(void *v) { 1047 uptr iter = (uptr)v; 1048 if (iter > 1) { 1049 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) { 1050 Printf("MemorySanitizer: failed to set thread key\n"); 1051 Die(); 1052 } 1053 return; 1054 } 1055 MsanAllocatorThreadFinish(); 1056} 1057 1058struct ThreadParam { 1059 void* (*callback)(void *arg); 1060 void *param; 1061 atomic_uintptr_t done; 1062}; 1063 1064static void *MsanThreadStartFunc(void *arg) { 1065 ThreadParam *p = (ThreadParam *)arg; 1066 void* (*callback)(void *arg) = p->callback; 1067 void *param = p->param; 1068 if (pthread_setspecific(g_thread_finalize_key, 1069 (void *)kPthreadDestructorIterations)) { 1070 Printf("MemorySanitizer: failed to set thread key\n"); 1071 Die(); 1072 } 1073 atomic_store(&p->done, 1, memory_order_release); 1074 return callback(param); 1075} 1076 1077INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*), 1078 void * param) { 1079 ENSURE_MSAN_INITED(); // for GetTlsSize() 1080 __sanitizer_pthread_attr_t myattr; 1081 if (attr == 0) { 1082 pthread_attr_init(&myattr); 1083 attr = &myattr; 1084 } 1085 1086 AdjustStackSizeLinux(attr); 1087 1088 ThreadParam p; 1089 p.callback = callback; 1090 p.param = param; 1091 atomic_store(&p.done, 0, memory_order_relaxed); 1092 1093 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, (void *)&p); 1094 if (res == 0) { 1095 while (atomic_load(&p.done, memory_order_acquire) != 1) 1096 pthread_yield(); 1097 } 1098 1099 if (attr == &myattr) 1100 pthread_attr_destroy(&myattr); 1101 if (!res) { 1102 __msan_unpoison(th, __sanitizer::pthread_t_sz); 1103 } 1104 return res; 1105} 1106 1107INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key, 1108 void (*dtor)(void *value)) { 1109 ENSURE_MSAN_INITED(); 1110 int res = REAL(pthread_key_create)(key, dtor); 1111 if (!res && key) 1112 __msan_unpoison(key, sizeof(*key)); 1113 return res; 1114} 1115 1116INTERCEPTOR(int, pthread_join, void *th, void **retval) { 1117 ENSURE_MSAN_INITED(); 1118 int res = REAL(pthread_join)(th, retval); 1119 if (!res && retval) 1120 __msan_unpoison(retval, sizeof(*retval)); 1121 return res; 1122} 1123 1124extern char *tzname[2]; 1125 1126INTERCEPTOR(void, tzset) { 1127 ENSURE_MSAN_INITED(); 1128 REAL(tzset)(); 1129 if (tzname[0]) 1130 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1); 1131 if (tzname[1]) 1132 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1); 1133 return; 1134} 1135 1136struct MSanAtExitRecord { 1137 void (*func)(void *arg); 1138 void *arg; 1139}; 1140 1141void MSanAtExitWrapper(void *arg) { 1142 UnpoisonParam(1); 1143 MSanAtExitRecord *r = (MSanAtExitRecord *)arg; 1144 r->func(r->arg); 1145 InternalFree(r); 1146} 1147 1148// Unpoison argument shadow for C++ module destructors. 1149INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg, 1150 void *dso_handle) { 1151 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle); 1152 ENSURE_MSAN_INITED(); 1153 MSanAtExitRecord *r = 1154 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord)); 1155 r->func = func; 1156 r->arg = arg; 1157 return REAL(__cxa_atexit)(MSanAtExitWrapper, r, dso_handle); 1158} 1159 1160DECLARE_REAL(int, shmctl, int shmid, int cmd, void *buf) 1161 1162INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) { 1163 ENSURE_MSAN_INITED(); 1164 void *p = REAL(shmat)(shmid, shmaddr, shmflg); 1165 if (p != (void *)-1) { 1166 __sanitizer_shmid_ds ds; 1167 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds); 1168 if (!res) { 1169 __msan_unpoison(p, ds.shm_segsz); 1170 } 1171 } 1172 return p; 1173} 1174 1175 1176struct MSanInterceptorContext { 1177 bool in_interceptor_scope; 1178}; 1179 1180namespace __msan { 1181 1182int OnExit() { 1183 // FIXME: ask frontend whether we need to return failure. 1184 return 0; 1185} 1186 1187} // namespace __msan 1188 1189extern "C" int *__errno_location(void); 1190 1191// A version of CHECK_UNPOISED using a saved scope value. Used in common 1192// interceptors. 1193#define CHECK_UNPOISONED_CTX(ctx, x, n) \ 1194 do { \ 1195 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \ 1196 CHECK_UNPOISONED_0(x, n); \ 1197 } while (0) 1198 1199#define COMMON_INTERCEPTOR_UNPOISON_PARAM(ctx, count) \ 1200 UnpoisonParam(count) 1201#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ 1202 __msan_unpoison(ptr, size) 1203#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \ 1204 CHECK_UNPOISONED_CTX(ctx, ptr, size) 1205#define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, ptr, size) \ 1206 __msan_unpoison(ptr, size) 1207#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ 1208 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \ 1209 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \ 1210 ctx = (void *)&msan_ctx; \ 1211 (void)ctx; \ 1212 InterceptorScope interceptor_scope; \ 1213 __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */ \ 1214 ENSURE_MSAN_INITED(); 1215#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ 1216 do { \ 1217 } while (false) 1218#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ 1219 do { \ 1220 } while (false) 1221#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ 1222 do { \ 1223 } while (false) 1224#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \ 1225 do { \ 1226 } while (false) // FIXME 1227#define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \ 1228 do { \ 1229 } while (false) // FIXME 1230#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name) 1231#define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit() 1232#include "sanitizer_common/sanitizer_common_interceptors.inc" 1233 1234#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s) 1235#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \ 1236 do { \ 1237 } while (false) 1238#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \ 1239 do { \ 1240 } while (false) 1241#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s) 1242#include "sanitizer_common/sanitizer_common_syscalls.inc" 1243 1244// static 1245void *fast_memset(void *ptr, int c, SIZE_T n) { 1246 // hack until we have a really fast internal_memset 1247 if (sizeof(uptr) == 8 && 1248 (n % 8) == 0 && 1249 ((uptr)ptr % 8) == 0 && 1250 (c == 0 || c == -1)) { 1251 // Printf("memset %p %zd %x\n", ptr, n, c); 1252 uptr to_store = c ? -1L : 0L; 1253 uptr *p = (uptr*)ptr; 1254 for (SIZE_T i = 0; i < n / 8; i++) 1255 p[i] = to_store; 1256 return ptr; 1257 } 1258 return internal_memset(ptr, c, n); 1259} 1260 1261// static 1262void *fast_memcpy(void *dst, const void *src, SIZE_T n) { 1263 // Same hack as in fast_memset above. 1264 if (sizeof(uptr) == 8 && 1265 (n % 8) == 0 && 1266 ((uptr)dst % 8) == 0 && 1267 ((uptr)src % 8) == 0) { 1268 uptr *d = (uptr*)dst; 1269 uptr *s = (uptr*)src; 1270 for (SIZE_T i = 0; i < n / 8; i++) 1271 d[i] = s[i]; 1272 return dst; 1273 } 1274 return internal_memcpy(dst, src, n); 1275} 1276 1277// These interface functions reside here so that they can use 1278// fast_memset, etc. 1279void __msan_unpoison(const void *a, uptr size) { 1280 if (!MEM_IS_APP(a)) return; 1281 fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size); 1282} 1283 1284void __msan_poison(const void *a, uptr size) { 1285 if (!MEM_IS_APP(a)) return; 1286 fast_memset((void*)MEM_TO_SHADOW((uptr)a), 1287 __msan::flags()->poison_heap_with_zeroes ? 0 : -1, size); 1288} 1289 1290void __msan_poison_stack(void *a, uptr size) { 1291 if (!MEM_IS_APP(a)) return; 1292 fast_memset((void*)MEM_TO_SHADOW((uptr)a), 1293 __msan::flags()->poison_stack_with_zeroes ? 0 : -1, size); 1294} 1295 1296void __msan_clear_and_unpoison(void *a, uptr size) { 1297 fast_memset(a, 0, size); 1298 fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size); 1299} 1300 1301u32 get_origin_if_poisoned(uptr a, uptr size) { 1302 unsigned char *s = (unsigned char *)MEM_TO_SHADOW(a); 1303 for (uptr i = 0; i < size; ++i) 1304 if (s[i]) 1305 return *(uptr *)SHADOW_TO_ORIGIN((s + i) & ~3UL); 1306 return 0; 1307} 1308 1309void __msan_copy_origin(void *dst, const void *src, uptr size) { 1310 if (!__msan_get_track_origins()) return; 1311 if (!MEM_IS_APP(dst) || !MEM_IS_APP(src)) return; 1312 uptr d = (uptr)dst; 1313 uptr beg = d & ~3UL; 1314 // Copy left unaligned origin if that memory is poisoned. 1315 if (beg < d) { 1316 u32 o = get_origin_if_poisoned(beg, d - beg); 1317 if (o) 1318 *(uptr *)MEM_TO_ORIGIN(beg) = o; 1319 beg += 4; 1320 } 1321 1322 uptr end = (d + size + 3) & ~3UL; 1323 // Copy right unaligned origin if that memory is poisoned. 1324 if (end > d + size) { 1325 u32 o = get_origin_if_poisoned(d + size, end - d - size); 1326 if (o) 1327 *(uptr *)MEM_TO_ORIGIN(end - 4) = o; 1328 end -= 4; 1329 } 1330 1331 if (beg < end) { 1332 // Align src up. 1333 uptr s = ((uptr)src + 3) & ~3UL; 1334 fast_memcpy((void*)MEM_TO_ORIGIN(beg), (void*)MEM_TO_ORIGIN(s), end - beg); 1335 } 1336} 1337 1338void __msan_copy_poison(void *dst, const void *src, uptr size) { 1339 if (!MEM_IS_APP(dst)) return; 1340 if (!MEM_IS_APP(src)) return; 1341 fast_memcpy((void*)MEM_TO_SHADOW((uptr)dst), 1342 (void*)MEM_TO_SHADOW((uptr)src), size); 1343 __msan_copy_origin(dst, src, size); 1344} 1345 1346void __msan_move_poison(void *dst, const void *src, uptr size) { 1347 if (!MEM_IS_APP(dst)) return; 1348 if (!MEM_IS_APP(src)) return; 1349 internal_memmove((void*)MEM_TO_SHADOW((uptr)dst), 1350 (void*)MEM_TO_SHADOW((uptr)src), size); 1351 __msan_copy_origin(dst, src, size); 1352} 1353 1354void *__msan_memcpy(void *dest, const void *src, SIZE_T n) { 1355 ENSURE_MSAN_INITED(); 1356 void *res = fast_memcpy(dest, src, n); 1357 __msan_copy_poison(dest, src, n); 1358 return res; 1359} 1360 1361void *__msan_memset(void *s, int c, SIZE_T n) { 1362 ENSURE_MSAN_INITED(); 1363 void *res = fast_memset(s, c, n); 1364 __msan_unpoison(s, n); 1365 return res; 1366} 1367 1368void *__msan_memmove(void *dest, const void *src, SIZE_T n) { 1369 ENSURE_MSAN_INITED(); 1370 void *res = REAL(memmove)(dest, src, n); 1371 __msan_move_poison(dest, src, n); 1372 return res; 1373} 1374 1375namespace __msan { 1376void InitializeInterceptors() { 1377 static int inited = 0; 1378 CHECK_EQ(inited, 0); 1379 SANITIZER_COMMON_INTERCEPTORS_INIT; 1380 1381 INTERCEPT_FUNCTION(mmap); 1382 INTERCEPT_FUNCTION(mmap64); 1383 INTERCEPT_FUNCTION(posix_memalign); 1384 INTERCEPT_FUNCTION(memalign); 1385 INTERCEPT_FUNCTION(valloc); 1386 INTERCEPT_FUNCTION(pvalloc); 1387 INTERCEPT_FUNCTION(malloc); 1388 INTERCEPT_FUNCTION(calloc); 1389 INTERCEPT_FUNCTION(realloc); 1390 INTERCEPT_FUNCTION(free); 1391 INTERCEPT_FUNCTION(fread); 1392 INTERCEPT_FUNCTION(fread_unlocked); 1393 INTERCEPT_FUNCTION(readlink); 1394 INTERCEPT_FUNCTION(memcpy); 1395 INTERCEPT_FUNCTION(mempcpy); 1396 INTERCEPT_FUNCTION(memset); 1397 INTERCEPT_FUNCTION(memmove); 1398 INTERCEPT_FUNCTION(bcopy); 1399 INTERCEPT_FUNCTION(wmemset); 1400 INTERCEPT_FUNCTION(wmemcpy); 1401 INTERCEPT_FUNCTION(wmempcpy); 1402 INTERCEPT_FUNCTION(wmemmove); 1403 INTERCEPT_FUNCTION(strcpy); // NOLINT 1404 INTERCEPT_FUNCTION(stpcpy); // NOLINT 1405 INTERCEPT_FUNCTION(strdup); 1406 INTERCEPT_FUNCTION(__strdup); 1407 INTERCEPT_FUNCTION(strndup); 1408 INTERCEPT_FUNCTION(__strndup); 1409 INTERCEPT_FUNCTION(strncpy); // NOLINT 1410 INTERCEPT_FUNCTION(strlen); 1411 INTERCEPT_FUNCTION(strnlen); 1412 INTERCEPT_FUNCTION(gcvt); 1413 INTERCEPT_FUNCTION(strcat); // NOLINT 1414 INTERCEPT_FUNCTION(strncat); // NOLINT 1415 INTERCEPT_FUNCTION(strtol); 1416 INTERCEPT_FUNCTION(strtoll); 1417 INTERCEPT_FUNCTION(strtoul); 1418 INTERCEPT_FUNCTION(strtoull); 1419 INTERCEPT_FUNCTION(strtod); 1420 INTERCEPT_FUNCTION(strtod_l); 1421 INTERCEPT_FUNCTION(__strtod_l); 1422 INTERCEPT_FUNCTION(strtof); 1423 INTERCEPT_FUNCTION(strtof_l); 1424 INTERCEPT_FUNCTION(__strtof_l); 1425 INTERCEPT_FUNCTION(strtold); 1426 INTERCEPT_FUNCTION(strtold_l); 1427 INTERCEPT_FUNCTION(__strtold_l); 1428 INTERCEPT_FUNCTION(vasprintf); 1429 INTERCEPT_FUNCTION(asprintf); 1430 INTERCEPT_FUNCTION(vsprintf); 1431 INTERCEPT_FUNCTION(vsnprintf); 1432 INTERCEPT_FUNCTION(vswprintf); 1433 INTERCEPT_FUNCTION(sprintf); // NOLINT 1434 INTERCEPT_FUNCTION(snprintf); 1435 INTERCEPT_FUNCTION(swprintf); 1436 INTERCEPT_FUNCTION(strftime); 1437 INTERCEPT_FUNCTION(mbtowc); 1438 INTERCEPT_FUNCTION(mbrtowc); 1439 INTERCEPT_FUNCTION(wcslen); 1440 INTERCEPT_FUNCTION(wcschr); 1441 INTERCEPT_FUNCTION(wcscpy); 1442 INTERCEPT_FUNCTION(wcscmp); 1443 INTERCEPT_FUNCTION(wcstod); 1444 INTERCEPT_FUNCTION(getenv); 1445 INTERCEPT_FUNCTION(setenv); 1446 INTERCEPT_FUNCTION(putenv); 1447 INTERCEPT_FUNCTION(gettimeofday); 1448 INTERCEPT_FUNCTION(fcvt); 1449 INTERCEPT_FUNCTION(__fxstat); 1450 INTERCEPT_FUNCTION(__fxstatat); 1451 INTERCEPT_FUNCTION(__xstat); 1452 INTERCEPT_FUNCTION(__lxstat); 1453 INTERCEPT_FUNCTION(__fxstat64); 1454 INTERCEPT_FUNCTION(__fxstatat64); 1455 INTERCEPT_FUNCTION(__xstat64); 1456 INTERCEPT_FUNCTION(__lxstat64); 1457 INTERCEPT_FUNCTION(pipe); 1458 INTERCEPT_FUNCTION(pipe2); 1459 INTERCEPT_FUNCTION(socketpair); 1460 INTERCEPT_FUNCTION(fgets); 1461 INTERCEPT_FUNCTION(fgets_unlocked); 1462 INTERCEPT_FUNCTION(getrlimit); 1463 INTERCEPT_FUNCTION(getrlimit64); 1464 INTERCEPT_FUNCTION(uname); 1465 INTERCEPT_FUNCTION(gethostname); 1466 INTERCEPT_FUNCTION(epoll_wait); 1467 INTERCEPT_FUNCTION(epoll_pwait); 1468 INTERCEPT_FUNCTION(recv); 1469 INTERCEPT_FUNCTION(recvfrom); 1470 INTERCEPT_FUNCTION(dladdr); 1471 INTERCEPT_FUNCTION(dlopen); 1472 INTERCEPT_FUNCTION(dl_iterate_phdr); 1473 INTERCEPT_FUNCTION(getrusage); 1474 INTERCEPT_FUNCTION(sigaction); 1475 INTERCEPT_FUNCTION(signal); 1476 INTERCEPT_FUNCTION(pthread_create); 1477 INTERCEPT_FUNCTION(pthread_key_create); 1478 INTERCEPT_FUNCTION(pthread_join); 1479 INTERCEPT_FUNCTION(tzset); 1480 INTERCEPT_FUNCTION(__cxa_atexit); 1481 INTERCEPT_FUNCTION(shmat); 1482 1483 if (REAL(pthread_key_create)(&g_thread_finalize_key, &thread_finalize)) { 1484 Printf("MemorySanitizer: failed to create thread key\n"); 1485 Die(); 1486 } 1487 1488 inited = 1; 1489} 1490} // namespace __msan 1491