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