1 //===-- msan_interceptors.cc ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of MemorySanitizer.
12 // Interceptors for standard library functions.
14 // FIXME: move as many interceptors as possible into
15 // sanitizer_common/sanitizer_common_interceptors.h
16 //===----------------------------------------------------------------------===//
18 #include "interception/interception.h"
20 #include "msan_chained_origin_depot.h"
21 #include "msan_origin.h"
22 #include "msan_thread.h"
23 #include "msan_poisoning.h"
24 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
25 #include "sanitizer_common/sanitizer_allocator.h"
26 #include "sanitizer_common/sanitizer_allocator_interface.h"
27 #include "sanitizer_common/sanitizer_allocator_internal.h"
28 #include "sanitizer_common/sanitizer_atomic.h"
29 #include "sanitizer_common/sanitizer_common.h"
30 #include "sanitizer_common/sanitizer_stackdepot.h"
31 #include "sanitizer_common/sanitizer_libc.h"
32 #include "sanitizer_common/sanitizer_linux.h"
33 #include "sanitizer_common/sanitizer_tls_get_addr.h"
36 // ACHTUNG! No other system header includes in this file.
37 // Ideally, we should get rid of stdarg.h as well.
39 using namespace __msan;
41 using __sanitizer::memory_order;
42 using __sanitizer::atomic_load;
43 using __sanitizer::atomic_store;
44 using __sanitizer::atomic_uintptr_t;
46 DECLARE_REAL(SIZE_T, strlen, const char *s)
47 DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
48 DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n)
49 DECLARE_REAL(void *, memset, void *dest, int c, uptr n)
52 #define __errno_location __error
55 // True if this is a nested interceptor.
56 static THREADLOCAL int in_interceptor_scope;
58 extern "C" int *__errno_location(void);
60 struct InterceptorScope {
61 InterceptorScope() { ++in_interceptor_scope; }
62 ~InterceptorScope() { --in_interceptor_scope; }
65 bool IsInInterceptorScope() {
66 return in_interceptor_scope;
69 static uptr allocated_for_dlsym;
70 static const uptr kDlsymAllocPoolSize = 1024;
71 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
73 static bool IsInDlsymAllocPool(const void *ptr) {
74 uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
75 return off < sizeof(alloc_memory_for_dlsym);
78 static void *AllocateFromLocalPool(uptr size_in_bytes) {
79 uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
80 void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
81 allocated_for_dlsym += size_in_words;
82 CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
86 #define ENSURE_MSAN_INITED() do { \
87 CHECK(!msan_init_is_running); \
93 // Check that [x, x+n) range is unpoisoned.
94 #define CHECK_UNPOISONED_0(x, n) \
96 sptr offset = __msan_test_shadow(x, n); \
97 if (__msan::IsInSymbolizer()) \
99 if (offset >= 0 && __msan::flags()->report_umrs) { \
100 GET_CALLER_PC_BP_SP; \
102 ReportUMRInsideAddressRange(__func__, x, n, offset); \
103 __msan::PrintWarningWithOrigin( \
104 pc, bp, __msan_get_origin((const char *)x + offset)); \
105 if (__msan::flags()->halt_on_error) { \
106 Printf("Exiting\n"); \
112 // Check that [x, x+n) range is unpoisoned unless we are in a nested
114 #define CHECK_UNPOISONED(x, n) \
116 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
119 #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \
120 CHECK_UNPOISONED((x), \
121 common_flags()->strict_string_checks ? (len) + 1 : (n) )
123 #define CHECK_UNPOISONED_STRING(x, n) \
124 CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n))
126 #if !SANITIZER_FREEBSD
127 INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
129 ENSURE_MSAN_INITED();
130 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
132 __msan_unpoison(ptr, res *size);
135 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked)
137 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED
140 INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
141 ENSURE_MSAN_INITED();
142 CHECK_UNPOISONED_STRING(path, 0)
143 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
145 __msan_unpoison(buf, res);
149 INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
150 return (char *)__msan_memcpy(dest, src, n) + n;
153 INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) {
154 ENSURE_MSAN_INITED();
155 void *res = REAL(memccpy)(dest, src, c, n);
156 CHECK(!res || (res >= dest && res <= (char *)dest + n));
157 SIZE_T sz = res ? (char *)res - (char *)dest : n;
158 CHECK_UNPOISONED(src, sz);
159 __msan_unpoison(dest, sz);
163 INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
164 return __msan_memmove(dest, src, n);
167 INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
168 GET_MALLOC_STACK_TRACE;
169 CHECK_EQ(alignment & (alignment - 1), 0);
171 *memptr = MsanReallocate(&stack, nullptr, size, alignment, false);
172 CHECK_NE(*memptr, 0);
173 __msan_unpoison(memptr, sizeof(*memptr));
177 #if !SANITIZER_FREEBSD
178 INTERCEPTOR(void *, memalign, SIZE_T boundary, SIZE_T size) {
179 GET_MALLOC_STACK_TRACE;
180 CHECK_EQ(boundary & (boundary - 1), 0);
181 void *ptr = MsanReallocate(&stack, nullptr, size, boundary, false);
184 #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
186 #define MSAN_MAYBE_INTERCEPT_MEMALIGN
189 INTERCEPTOR(void *, aligned_alloc, SIZE_T boundary, SIZE_T size) {
190 GET_MALLOC_STACK_TRACE;
191 CHECK_EQ(boundary & (boundary - 1), 0);
192 void *ptr = MsanReallocate(&stack, nullptr, size, boundary, false);
196 INTERCEPTOR(void *, __libc_memalign, SIZE_T boundary, SIZE_T size) {
197 GET_MALLOC_STACK_TRACE;
198 CHECK_EQ(boundary & (boundary - 1), 0);
199 void *ptr = MsanReallocate(&stack, nullptr, size, boundary, false);
200 DTLS_on_libc_memalign(ptr, size);
204 INTERCEPTOR(void *, valloc, SIZE_T size) {
205 GET_MALLOC_STACK_TRACE;
206 void *ptr = MsanReallocate(&stack, nullptr, size, GetPageSizeCached(), false);
210 #if !SANITIZER_FREEBSD
211 INTERCEPTOR(void *, pvalloc, SIZE_T size) {
212 GET_MALLOC_STACK_TRACE;
213 uptr PageSize = GetPageSizeCached();
214 size = RoundUpTo(size, PageSize);
216 // pvalloc(0) should allocate one page.
219 void *ptr = MsanReallocate(&stack, nullptr, size, PageSize, false);
222 #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
224 #define MSAN_MAYBE_INTERCEPT_PVALLOC
227 INTERCEPTOR(void, free, void *ptr) {
228 GET_MALLOC_STACK_TRACE;
229 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
230 MsanDeallocate(&stack, ptr);
233 #if !SANITIZER_FREEBSD
234 INTERCEPTOR(void, cfree, void *ptr) {
235 GET_MALLOC_STACK_TRACE;
236 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
237 MsanDeallocate(&stack, ptr);
239 #define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
241 #define MSAN_MAYBE_INTERCEPT_CFREE
244 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
245 return __sanitizer_get_allocated_size(ptr);
248 #if !SANITIZER_FREEBSD
249 // This function actually returns a struct by value, but we can't unpoison a
250 // temporary! The following is equivalent on all supported platforms but
251 // aarch64 (which uses a different register for sret value). We have a test
253 INTERCEPTOR(void, mallinfo, __sanitizer_mallinfo *sret) {
256 asm volatile("mov %0,x8" : "=r" (r8));
257 sret = reinterpret_cast<__sanitizer_mallinfo*>(r8);
259 REAL(memset)(sret, 0, sizeof(*sret));
260 __msan_unpoison(sret, sizeof(*sret));
262 #define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
264 #define MSAN_MAYBE_INTERCEPT_MALLINFO
267 #if !SANITIZER_FREEBSD
268 INTERCEPTOR(int, mallopt, int cmd, int value) {
271 #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
273 #define MSAN_MAYBE_INTERCEPT_MALLOPT
276 #if !SANITIZER_FREEBSD
277 INTERCEPTOR(void, malloc_stats, void) {
278 // FIXME: implement, but don't call REAL(malloc_stats)!
280 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats)
282 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS
285 INTERCEPTOR(char *, strcpy, char *dest, const char *src) { // NOLINT
286 ENSURE_MSAN_INITED();
287 GET_STORE_STACK_TRACE;
288 SIZE_T n = REAL(strlen)(src);
289 CHECK_UNPOISONED_STRING(src + n, 0);
290 char *res = REAL(strcpy)(dest, src); // NOLINT
291 CopyShadowAndOrigin(dest, src, n + 1, &stack);
295 INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { // NOLINT
296 ENSURE_MSAN_INITED();
297 GET_STORE_STACK_TRACE;
298 SIZE_T copy_size = REAL(strnlen)(src, n);
300 copy_size++; // trailing \0
301 char *res = REAL(strncpy)(dest, src, n); // NOLINT
302 CopyShadowAndOrigin(dest, src, copy_size, &stack);
303 __msan_unpoison(dest + copy_size, n - copy_size);
307 INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { // NOLINT
308 ENSURE_MSAN_INITED();
309 GET_STORE_STACK_TRACE;
310 SIZE_T n = REAL(strlen)(src);
311 CHECK_UNPOISONED_STRING(src + n, 0);
312 char *res = REAL(stpcpy)(dest, src); // NOLINT
313 CopyShadowAndOrigin(dest, src, n + 1, &stack);
317 INTERCEPTOR(char *, strdup, char *src) {
318 ENSURE_MSAN_INITED();
319 GET_STORE_STACK_TRACE;
320 // On FreeBSD strdup() leverages strlen().
321 InterceptorScope interceptor_scope;
322 SIZE_T n = REAL(strlen)(src);
323 CHECK_UNPOISONED_STRING(src + n, 0);
324 char *res = REAL(strdup)(src);
325 CopyShadowAndOrigin(res, src, n + 1, &stack);
329 #if !SANITIZER_FREEBSD
330 INTERCEPTOR(char *, __strdup, char *src) {
331 ENSURE_MSAN_INITED();
332 GET_STORE_STACK_TRACE;
333 SIZE_T n = REAL(strlen)(src);
334 CHECK_UNPOISONED_STRING(src + n, 0);
335 char *res = REAL(__strdup)(src);
336 CopyShadowAndOrigin(res, src, n + 1, &stack);
339 #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup)
341 #define MSAN_MAYBE_INTERCEPT___STRDUP
344 INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
345 ENSURE_MSAN_INITED();
346 char *res = REAL(gcvt)(number, ndigit, buf);
347 SIZE_T n = REAL(strlen)(buf);
348 __msan_unpoison(buf, n + 1);
352 INTERCEPTOR(char *, strcat, char *dest, const char *src) { // NOLINT
353 ENSURE_MSAN_INITED();
354 GET_STORE_STACK_TRACE;
355 SIZE_T src_size = REAL(strlen)(src);
356 SIZE_T dest_size = REAL(strlen)(dest);
357 CHECK_UNPOISONED_STRING(src + src_size, 0);
358 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
359 char *res = REAL(strcat)(dest, src); // NOLINT
360 CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack);
364 INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { // NOLINT
365 ENSURE_MSAN_INITED();
366 GET_STORE_STACK_TRACE;
367 SIZE_T dest_size = REAL(strlen)(dest);
368 SIZE_T copy_size = REAL(strnlen)(src, n);
369 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
370 char *res = REAL(strncat)(dest, src, n); // NOLINT
371 CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack);
372 __msan_unpoison(dest + dest_size + copy_size, 1); // \0
376 // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to
377 // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO.
378 #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \
379 ENSURE_MSAN_INITED(); \
380 ret_type res = REAL(func)(__VA_ARGS__); \
381 __msan_unpoison(endptr, sizeof(*endptr)); \
384 #define INTERCEPTOR_STRTO(ret_type, func, char_type) \
385 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \
386 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \
389 #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
390 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
392 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \
395 #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \
396 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
398 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \
401 #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \
402 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
403 int base, void *loc) { \
404 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \
407 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
408 INTERCEPTOR_STRTO(ret_type, func, char_type) \
409 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \
410 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \
411 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type)
413 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
414 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
415 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \
416 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \
417 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type)
419 INTERCEPTORS_STRTO(double, strtod, char) // NOLINT
420 INTERCEPTORS_STRTO(float, strtof, char) // NOLINT
421 INTERCEPTORS_STRTO(long double, strtold, char) // NOLINT
422 INTERCEPTORS_STRTO_BASE(long, strtol, char) // NOLINT
423 INTERCEPTORS_STRTO_BASE(long long, strtoll, char) // NOLINT
424 INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char) // NOLINT
425 INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char) // NOLINT
427 INTERCEPTORS_STRTO(double, wcstod, wchar_t) // NOLINT
428 INTERCEPTORS_STRTO(float, wcstof, wchar_t) // NOLINT
429 INTERCEPTORS_STRTO(long double, wcstold, wchar_t) // NOLINT
430 INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t) // NOLINT
431 INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t) // NOLINT
432 INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t) // NOLINT
433 INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t) // NOLINT
435 #define INTERCEPT_STRTO(func) \
436 INTERCEPT_FUNCTION(func); \
437 INTERCEPT_FUNCTION(func##_l); \
438 INTERCEPT_FUNCTION(__##func##_l); \
439 INTERCEPT_FUNCTION(__##func##_internal);
442 // FIXME: support *wprintf in common format interceptors.
443 INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
444 ENSURE_MSAN_INITED();
445 int res = REAL(vswprintf)(str, size, format, ap);
447 __msan_unpoison(str, 4 * (res + 1));
452 INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
453 ENSURE_MSAN_INITED();
455 va_start(ap, format);
456 int res = vswprintf(str, size, format, ap);
461 INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T n) {
462 ENSURE_MSAN_INITED();
463 CHECK_UNPOISONED(src, REAL(strlen)(src) + 1);
464 SIZE_T res = REAL(strxfrm)(dest, src, n);
465 if (res < n) __msan_unpoison(dest, res + 1);
469 INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T n,
471 ENSURE_MSAN_INITED();
472 CHECK_UNPOISONED(src, REAL(strlen)(src) + 1);
473 SIZE_T res = REAL(strxfrm_l)(dest, src, n, loc);
474 if (res < n) __msan_unpoison(dest, res + 1);
478 #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \
479 ENSURE_MSAN_INITED(); \
480 ret_type res = REAL(func)(s, __VA_ARGS__); \
481 if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \
484 INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
485 __sanitizer_tm *tm) {
486 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm);
489 INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format,
490 __sanitizer_tm *tm, void *loc) {
491 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc);
494 #if !SANITIZER_FREEBSD
495 INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format,
496 __sanitizer_tm *tm, void *loc) {
497 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm,
500 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l)
502 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L
505 INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format,
506 __sanitizer_tm *tm) {
507 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm);
510 INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
511 __sanitizer_tm *tm, void *loc) {
512 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm,
516 #if !SANITIZER_FREEBSD
517 INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
518 __sanitizer_tm *tm, void *loc) {
519 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm,
522 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l)
524 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L
527 INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
528 ENSURE_MSAN_INITED();
529 int res = REAL(mbtowc)(dest, src, n);
530 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
534 INTERCEPTOR(int, mbrtowc, wchar_t *dest, const char *src, SIZE_T n, void *ps) {
535 ENSURE_MSAN_INITED();
536 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
537 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
541 INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
542 ENSURE_MSAN_INITED();
543 SIZE_T res = REAL(wcslen)(s);
544 CHECK_UNPOISONED(s, sizeof(wchar_t) * (res + 1));
548 INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
549 ENSURE_MSAN_INITED();
550 SIZE_T res = REAL(wcsnlen)(s, n);
551 CHECK_UNPOISONED(s, sizeof(wchar_t) * Min(res + 1, n));
555 // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
556 INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
557 ENSURE_MSAN_INITED();
558 wchar_t *res = REAL(wcschr)(s, wc, ps);
562 // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
563 INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
564 ENSURE_MSAN_INITED();
565 GET_STORE_STACK_TRACE;
566 wchar_t *res = REAL(wcscpy)(dest, src);
567 CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1),
572 INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src,
573 SIZE_T n) { // NOLINT
574 ENSURE_MSAN_INITED();
575 GET_STORE_STACK_TRACE;
576 SIZE_T copy_size = REAL(wcsnlen)(src, n);
577 if (copy_size < n) copy_size++; // trailing \0
578 wchar_t *res = REAL(wcsncpy)(dest, src, n); // NOLINT
579 CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack);
580 __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t));
584 // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
585 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
586 ENSURE_MSAN_INITED();
587 GET_STORE_STACK_TRACE;
588 wchar_t *res = REAL(wmemcpy)(dest, src, n);
589 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
593 INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
594 ENSURE_MSAN_INITED();
595 GET_STORE_STACK_TRACE;
596 wchar_t *res = REAL(wmempcpy)(dest, src, n);
597 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
601 INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
602 CHECK(MEM_IS_APP(s));
603 ENSURE_MSAN_INITED();
604 wchar_t *res = REAL(wmemset)(s, c, n);
605 __msan_unpoison(s, n * sizeof(wchar_t));
609 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
610 ENSURE_MSAN_INITED();
611 GET_STORE_STACK_TRACE;
612 wchar_t *res = REAL(wmemmove)(dest, src, n);
613 MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
617 INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
618 ENSURE_MSAN_INITED();
619 int res = REAL(wcscmp)(s1, s2);
623 INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
624 ENSURE_MSAN_INITED();
625 int res = REAL(gettimeofday)(tv, tz);
627 __msan_unpoison(tv, 16);
629 __msan_unpoison(tz, 8);
633 INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
634 ENSURE_MSAN_INITED();
635 char *res = REAL(fcvt)(x, a, b, c);
636 __msan_unpoison(b, sizeof(*b));
637 __msan_unpoison(c, sizeof(*c));
638 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
642 INTERCEPTOR(char *, getenv, char *name) {
643 if (msan_init_is_running)
644 return REAL(getenv)(name);
645 ENSURE_MSAN_INITED();
646 char *res = REAL(getenv)(name);
647 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
651 extern char **environ;
653 static void UnpoisonEnviron() {
654 char **envp = environ;
655 for (; *envp; ++envp) {
656 __msan_unpoison(envp, sizeof(*envp));
657 __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
659 // Trailing NULL pointer.
660 __msan_unpoison(envp, sizeof(*envp));
663 INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
664 ENSURE_MSAN_INITED();
665 CHECK_UNPOISONED_STRING(name, 0)
666 int res = REAL(setenv)(name, value, overwrite);
667 if (!res) UnpoisonEnviron();
671 INTERCEPTOR(int, putenv, char *string) {
672 ENSURE_MSAN_INITED();
673 int res = REAL(putenv)(string);
674 if (!res) UnpoisonEnviron();
678 #if !SANITIZER_FREEBSD
679 INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
680 ENSURE_MSAN_INITED();
681 int res = REAL(__fxstat)(magic, fd, buf);
683 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
686 #define MSAN_MAYBE_INTERCEPT___FXSTAT INTERCEPT_FUNCTION(__fxstat)
688 #define MSAN_MAYBE_INTERCEPT___FXSTAT
691 #if !SANITIZER_FREEBSD
692 INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
693 ENSURE_MSAN_INITED();
694 int res = REAL(__fxstat64)(magic, fd, buf);
696 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
699 #define MSAN_MAYBE_INTERCEPT___FXSTAT64 INTERCEPT_FUNCTION(__fxstat64)
701 #define MSAN_MAYBE_INTERCEPT___FXSTAT64
704 #if SANITIZER_FREEBSD
705 INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
706 ENSURE_MSAN_INITED();
707 int res = REAL(fstatat)(fd, pathname, buf, flags);
708 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
711 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(fstatat)
713 INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
715 ENSURE_MSAN_INITED();
716 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
717 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
720 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(__fxstatat)
723 #if !SANITIZER_FREEBSD
724 INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
726 ENSURE_MSAN_INITED();
727 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
728 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
731 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64 INTERCEPT_FUNCTION(__fxstatat64)
733 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64
736 INTERCEPTOR(int, pipe, int pipefd[2]) {
737 if (msan_init_is_running)
738 return REAL(pipe)(pipefd);
739 ENSURE_MSAN_INITED();
740 int res = REAL(pipe)(pipefd);
742 __msan_unpoison(pipefd, sizeof(int[2]));
746 INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
747 ENSURE_MSAN_INITED();
748 int res = REAL(pipe2)(pipefd, flags);
750 __msan_unpoison(pipefd, sizeof(int[2]));
754 INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
755 ENSURE_MSAN_INITED();
756 int res = REAL(socketpair)(domain, type, protocol, sv);
758 __msan_unpoison(sv, sizeof(int[2]));
762 INTERCEPTOR(char *, fgets, char *s, int size, void *stream) {
763 ENSURE_MSAN_INITED();
764 char *res = REAL(fgets)(s, size, stream);
766 __msan_unpoison(s, REAL(strlen)(s) + 1);
770 #if !SANITIZER_FREEBSD
771 INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
772 ENSURE_MSAN_INITED();
773 char *res = REAL(fgets_unlocked)(s, size, stream);
775 __msan_unpoison(s, REAL(strlen)(s) + 1);
778 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked)
780 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED
783 INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
784 if (msan_init_is_running)
785 return REAL(getrlimit)(resource, rlim);
786 ENSURE_MSAN_INITED();
787 int res = REAL(getrlimit)(resource, rlim);
789 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz);
793 #if !SANITIZER_FREEBSD
794 INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
795 if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim);
796 ENSURE_MSAN_INITED();
797 int res = REAL(getrlimit64)(resource, rlim);
798 if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
802 INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit,
804 if (msan_init_is_running)
805 return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
806 ENSURE_MSAN_INITED();
807 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz);
808 int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
809 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz);
813 INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit,
815 if (msan_init_is_running)
816 return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
817 ENSURE_MSAN_INITED();
818 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz);
819 int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
820 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz);
824 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64)
825 #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit)
826 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64)
828 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64
829 #define MSAN_MAYBE_INTERCEPT_PRLIMIT
830 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64
833 #if SANITIZER_FREEBSD
834 // FreeBSD's <sys/utsname.h> define uname() as
835 // static __inline int uname(struct utsname *name) {
836 // return __xuname(SYS_NMLN, (void*)name);
838 INTERCEPTOR(int, __xuname, int size, void *utsname) {
839 ENSURE_MSAN_INITED();
840 int res = REAL(__xuname)(size, utsname);
842 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
845 #define MSAN_INTERCEPT_UNAME INTERCEPT_FUNCTION(__xuname)
847 INTERCEPTOR(int, uname, struct utsname *utsname) {
848 ENSURE_MSAN_INITED();
849 int res = REAL(uname)(utsname);
851 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
854 #define MSAN_INTERCEPT_UNAME INTERCEPT_FUNCTION(uname)
857 INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
858 ENSURE_MSAN_INITED();
859 int res = REAL(gethostname)(name, len);
861 SIZE_T real_len = REAL(strnlen)(name, len);
864 __msan_unpoison(name, real_len);
869 #if !SANITIZER_FREEBSD
870 INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
872 ENSURE_MSAN_INITED();
873 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
875 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
879 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
881 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT
884 #if !SANITIZER_FREEBSD
885 INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
886 int timeout, void *sigmask) {
887 ENSURE_MSAN_INITED();
888 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
890 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
894 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
896 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
899 INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
900 GET_MALLOC_STACK_TRACE;
901 if (UNLIKELY(!msan_inited))
902 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
903 return AllocateFromLocalPool(nmemb * size);
904 return MsanCalloc(&stack, nmemb, size);
907 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
908 GET_MALLOC_STACK_TRACE;
909 if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
910 uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
911 uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
913 if (UNLIKELY(!msan_inited)) {
914 new_ptr = AllocateFromLocalPool(copy_size);
917 new_ptr = MsanReallocate(&stack, nullptr, copy_size, sizeof(u64), false);
919 internal_memcpy(new_ptr, ptr, copy_size);
922 return MsanReallocate(&stack, ptr, size, sizeof(u64), false);
925 INTERCEPTOR(void *, malloc, SIZE_T size) {
926 GET_MALLOC_STACK_TRACE;
927 if (UNLIKELY(!msan_inited))
928 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
929 return AllocateFromLocalPool(size);
930 return MsanReallocate(&stack, nullptr, size, sizeof(u64), false);
933 void __msan_allocated_memory(const void *data, uptr size) {
934 GET_MALLOC_STACK_TRACE;
935 if (flags()->poison_in_malloc) {
936 stack.tag = STACK_TRACE_TAG_POISON;
937 PoisonMemory(data, size, &stack);
941 void __msan_copy_shadow(void *dest, const void *src, uptr n) {
942 GET_STORE_STACK_TRACE;
943 MoveShadowAndOrigin(dest, src, n, &stack);
946 void __sanitizer_dtor_callback(const void *data, uptr size) {
947 GET_MALLOC_STACK_TRACE;
948 if (flags()->poison_in_dtor) {
949 stack.tag = STACK_TRACE_TAG_POISON;
950 PoisonMemory(data, size, &stack);
954 INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
955 int fd, OFF_T offset) {
956 if (msan_init_is_running)
957 return REAL(mmap)(addr, length, prot, flags, fd, offset);
958 ENSURE_MSAN_INITED();
959 if (addr && !MEM_IS_APP(addr)) {
960 if (flags & map_fixed) {
961 *__errno_location() = errno_EINVAL;
967 void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
968 if (res != (void*)-1)
969 __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
973 #if !SANITIZER_FREEBSD
974 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
975 int fd, OFF64_T offset) {
976 ENSURE_MSAN_INITED();
977 if (addr && !MEM_IS_APP(addr)) {
978 if (flags & map_fixed) {
979 *__errno_location() = errno_EINVAL;
985 void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
986 if (res != (void*)-1)
987 __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
990 #define MSAN_MAYBE_INTERCEPT_MMAP64 INTERCEPT_FUNCTION(mmap64)
992 #define MSAN_MAYBE_INTERCEPT_MMAP64
995 INTERCEPTOR(int, getrusage, int who, void *usage) {
996 ENSURE_MSAN_INITED();
997 int res = REAL(getrusage)(who, usage);
999 __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
1004 class SignalHandlerScope {
1006 SignalHandlerScope() {
1007 if (MsanThread *t = GetCurrentThread())
1008 t->EnterSignalHandler();
1010 ~SignalHandlerScope() {
1011 if (MsanThread *t = GetCurrentThread())
1012 t->LeaveSignalHandler();
1016 // sigactions_mu guarantees atomicity of sigaction() and signal() calls.
1017 // Access to sigactions[] is gone with relaxed atomics to avoid data race with
1018 // the signal handler.
1019 const int kMaxSignals = 1024;
1020 static atomic_uintptr_t sigactions[kMaxSignals];
1021 static StaticSpinMutex sigactions_mu;
1023 static void SignalHandler(int signo) {
1024 SignalHandlerScope signal_handler_scope;
1025 ScopedThreadLocalStateBackup stlsb;
1028 typedef void (*signal_cb)(int x);
1030 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1034 static void SignalAction(int signo, void *si, void *uc) {
1035 SignalHandlerScope signal_handler_scope;
1036 ScopedThreadLocalStateBackup stlsb;
1038 __msan_unpoison(si, sizeof(__sanitizer_sigaction));
1039 __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
1041 typedef void (*sigaction_cb)(int, void *, void *);
1043 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1047 INTERCEPTOR(int, sigaction, int signo, const __sanitizer_sigaction *act,
1048 __sanitizer_sigaction *oldact) {
1049 ENSURE_MSAN_INITED();
1050 // FIXME: check that *act is unpoisoned.
1051 // That requires intercepting all of sigemptyset, sigfillset, etc.
1053 if (flags()->wrap_signals) {
1054 SpinMutexLock lock(&sigactions_mu);
1055 CHECK_LT(signo, kMaxSignals);
1056 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
1057 __sanitizer_sigaction new_act;
1058 __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr;
1060 REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction));
1061 uptr cb = (uptr)pnew_act->sigaction;
1062 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
1063 ? (uptr)SignalAction
1064 : (uptr)SignalHandler;
1065 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1066 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1067 pnew_act->sigaction = (void (*)(int, void *, void *))new_cb;
1070 res = REAL(sigaction)(signo, pnew_act, oldact);
1071 if (res == 0 && oldact) {
1072 uptr cb = (uptr)oldact->sigaction;
1073 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1074 oldact->sigaction = (void (*)(int, void *, void *))old_cb;
1078 res = REAL(sigaction)(signo, act, oldact);
1081 if (res == 0 && oldact) {
1082 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
1087 INTERCEPTOR(int, signal, int signo, uptr cb) {
1088 ENSURE_MSAN_INITED();
1089 if (flags()->wrap_signals) {
1090 CHECK_LT(signo, kMaxSignals);
1091 SpinMutexLock lock(&sigactions_mu);
1092 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1093 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1094 cb = (uptr) SignalHandler;
1096 return REAL(signal)(signo, cb);
1098 return REAL(signal)(signo, cb);
1102 extern "C" int pthread_attr_init(void *attr);
1103 extern "C" int pthread_attr_destroy(void *attr);
1105 static void *MsanThreadStartFunc(void *arg) {
1106 MsanThread *t = (MsanThread *)arg;
1107 SetCurrentThread(t);
1108 return t->ThreadStart();
1111 INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1113 ENSURE_MSAN_INITED(); // for GetTlsSize()
1114 __sanitizer_pthread_attr_t myattr;
1116 pthread_attr_init(&myattr);
1120 AdjustStackSize(attr);
1122 MsanThread *t = MsanThread::Create(callback, param);
1124 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t);
1126 if (attr == &myattr)
1127 pthread_attr_destroy(&myattr);
1129 __msan_unpoison(th, __sanitizer::pthread_t_sz);
1134 INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1135 void (*dtor)(void *value)) {
1136 if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor);
1137 ENSURE_MSAN_INITED();
1138 int res = REAL(pthread_key_create)(key, dtor);
1140 __msan_unpoison(key, sizeof(*key));
1144 INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1145 ENSURE_MSAN_INITED();
1146 int res = REAL(pthread_join)(th, retval);
1148 __msan_unpoison(retval, sizeof(*retval));
1152 extern char *tzname[2];
1154 INTERCEPTOR(void, tzset, int fake) {
1155 ENSURE_MSAN_INITED();
1158 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1);
1160 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1);
1164 struct MSanAtExitRecord {
1165 void (*func)(void *arg);
1169 void MSanAtExitWrapper(void *arg) {
1171 MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
1176 // Unpoison argument shadow for C++ module destructors.
1177 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
1179 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
1180 ENSURE_MSAN_INITED();
1181 MSanAtExitRecord *r =
1182 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
1185 return REAL(__cxa_atexit)(MSanAtExitWrapper, r, dso_handle);
1188 DECLARE_REAL(int, shmctl, int shmid, int cmd, void *buf)
1190 INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) {
1191 ENSURE_MSAN_INITED();
1192 void *p = REAL(shmat)(shmid, shmaddr, shmflg);
1193 if (p != (void *)-1) {
1194 __sanitizer_shmid_ds ds;
1195 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds);
1197 __msan_unpoison(p, ds.shm_segsz);
1203 static void BeforeFork() {
1204 get_allocator().ForceLock();
1205 StackDepotLockAll();
1206 ChainedOriginDepotLockAll();
1209 static void AfterFork() {
1210 ChainedOriginDepotUnlockAll();
1211 StackDepotUnlockAll();
1212 get_allocator().ForceUnlock();
1215 INTERCEPTOR(int, fork, void) {
1216 ENSURE_MSAN_INITED();
1218 int pid = REAL(fork)();
1223 INTERCEPTOR(int, openpty, int *amaster, int *aslave, char *name,
1224 const void *termp, const void *winp) {
1225 ENSURE_MSAN_INITED();
1226 InterceptorScope interceptor_scope;
1227 int res = REAL(openpty)(amaster, aslave, name, termp, winp);
1229 __msan_unpoison(amaster, sizeof(*amaster));
1230 __msan_unpoison(aslave, sizeof(*aslave));
1235 INTERCEPTOR(int, forkpty, int *amaster, char *name, const void *termp,
1237 ENSURE_MSAN_INITED();
1238 InterceptorScope interceptor_scope;
1239 int res = REAL(forkpty)(amaster, name, termp, winp);
1241 __msan_unpoison(amaster, sizeof(*amaster));
1245 struct MSanInterceptorContext {
1246 bool in_interceptor_scope;
1252 // FIXME: ask frontend whether we need to return failure.
1256 } // namespace __msan
1258 // A version of CHECK_UNPOISONED using a saved scope value. Used in common
1260 #define CHECK_UNPOISONED_CTX(ctx, x, n) \
1262 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1263 CHECK_UNPOISONED_0(x, n); \
1266 #define MSAN_INTERCEPT_FUNC(name) \
1268 if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
1269 VReport(1, "MemorySanitizer: failed to intercept '" #name "'\n"); \
1272 #define MSAN_INTERCEPT_FUNC_VER(name, ver) \
1274 if ((!INTERCEPT_FUNCTION_VER(name, ver) || !REAL(name))) \
1276 1, "MemorySanitizer: failed to intercept '" #name "@@" #ver "'\n"); \
1279 #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name)
1280 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
1281 MSAN_INTERCEPT_FUNC_VER(name, ver)
1282 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \
1283 UnpoisonParam(count)
1284 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
1285 __msan_unpoison(ptr, size)
1286 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
1287 CHECK_UNPOISONED_CTX(ctx, ptr, size)
1288 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
1289 __msan_unpoison(ptr, size)
1290 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
1291 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \
1292 ENSURE_MSAN_INITED(); \
1293 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1294 ctx = (void *)&msan_ctx; \
1296 InterceptorScope interceptor_scope; \
1297 __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */
1298 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
1301 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1304 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1307 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1310 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
1312 } while (false) // FIXME
1313 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
1315 } while (false) // FIXME
1316 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
1317 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
1318 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
1320 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \
1321 if (filename && map) \
1322 ForEachMappedRegion(map, __msan_unpoison); \
1325 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
1326 if (MsanThread *t = GetCurrentThread()) { \
1327 *begin = t->tls_begin(); \
1328 *end = t->tls_end(); \
1330 *begin = *end = 0; \
1333 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
1336 return __msan_memset(block, c, size); \
1338 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
1341 return __msan_memmove(to, from, size); \
1343 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
1346 return __msan_memcpy(to, from, size); \
1349 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
1351 GET_STORE_STACK_TRACE; \
1352 CopyShadowAndOrigin(to, from, size, &stack); \
1353 __msan_unpoison(to + size, 1); \
1356 #include "sanitizer_common/sanitizer_platform_interceptors.h"
1357 #include "sanitizer_common/sanitizer_common_interceptors.inc"
1359 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
1360 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
1363 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
1366 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1367 #include "sanitizer_common/sanitizer_common_syscalls.inc"
1376 INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
1378 COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info);
1379 int res = REAL(dladdr)(addr, info);
1381 __msan_unpoison(info, sizeof(*info));
1382 if (info->dli_fname)
1383 __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
1384 if (info->dli_sname)
1385 __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
1390 INTERCEPTOR(char *, dlerror, int fake) {
1392 COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake);
1393 char *res = REAL(dlerror)(fake);
1394 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
1398 typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
1400 struct dl_iterate_phdr_data {
1401 dl_iterate_phdr_cb callback;
1405 static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
1408 __msan_unpoison(info, size);
1409 if (info->dlpi_phdr && info->dlpi_phnum)
1410 __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum);
1411 if (info->dlpi_name)
1412 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
1414 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
1416 return cbdata->callback(info, size, cbdata->data);
1419 INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
1421 COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data);
1422 dl_iterate_phdr_data cbdata;
1423 cbdata.callback = callback;
1425 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
1429 // These interface functions reside here so that they can use
1430 // REAL(memset), etc.
1431 void __msan_unpoison(const void *a, uptr size) {
1432 if (!MEM_IS_APP(a)) return;
1433 SetShadow(a, size, 0);
1436 void __msan_poison(const void *a, uptr size) {
1437 if (!MEM_IS_APP(a)) return;
1438 SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1);
1441 void __msan_poison_stack(void *a, uptr size) {
1442 if (!MEM_IS_APP(a)) return;
1443 SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1);
1446 void __msan_clear_and_unpoison(void *a, uptr size) {
1447 REAL(memset)(a, 0, size);
1448 SetShadow(a, size, 0);
1451 void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
1452 if (!msan_inited) return internal_memcpy(dest, src, n);
1453 if (msan_init_is_running || __msan::IsInSymbolizer())
1454 return REAL(memcpy)(dest, src, n);
1455 ENSURE_MSAN_INITED();
1456 GET_STORE_STACK_TRACE;
1457 void *res = REAL(memcpy)(dest, src, n);
1458 CopyShadowAndOrigin(dest, src, n, &stack);
1462 void *__msan_memset(void *s, int c, SIZE_T n) {
1463 if (!msan_inited) return internal_memset(s, c, n);
1464 if (msan_init_is_running) return REAL(memset)(s, c, n);
1465 ENSURE_MSAN_INITED();
1466 void *res = REAL(memset)(s, c, n);
1467 __msan_unpoison(s, n);
1471 void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
1472 if (!msan_inited) return internal_memmove(dest, src, n);
1473 if (msan_init_is_running) return REAL(memmove)(dest, src, n);
1474 ENSURE_MSAN_INITED();
1475 GET_STORE_STACK_TRACE;
1476 void *res = REAL(memmove)(dest, src, n);
1477 MoveShadowAndOrigin(dest, src, n, &stack);
1481 void __msan_unpoison_string(const char* s) {
1482 if (!MEM_IS_APP(s)) return;
1483 __msan_unpoison(s, REAL(strlen)(s) + 1);
1488 void InitializeInterceptors() {
1489 static int inited = 0;
1490 CHECK_EQ(inited, 0);
1491 InitializeCommonInterceptors();
1493 INTERCEPT_FUNCTION(mmap);
1494 MSAN_MAYBE_INTERCEPT_MMAP64;
1495 INTERCEPT_FUNCTION(posix_memalign);
1496 MSAN_MAYBE_INTERCEPT_MEMALIGN;
1497 INTERCEPT_FUNCTION(__libc_memalign);
1498 INTERCEPT_FUNCTION(valloc);
1499 MSAN_MAYBE_INTERCEPT_PVALLOC;
1500 INTERCEPT_FUNCTION(malloc);
1501 INTERCEPT_FUNCTION(calloc);
1502 INTERCEPT_FUNCTION(realloc);
1503 INTERCEPT_FUNCTION(free);
1504 MSAN_MAYBE_INTERCEPT_CFREE;
1505 INTERCEPT_FUNCTION(malloc_usable_size);
1506 MSAN_MAYBE_INTERCEPT_MALLINFO;
1507 MSAN_MAYBE_INTERCEPT_MALLOPT;
1508 MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
1509 INTERCEPT_FUNCTION(fread);
1510 MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED;
1511 INTERCEPT_FUNCTION(readlink);
1512 INTERCEPT_FUNCTION(memccpy);
1513 INTERCEPT_FUNCTION(mempcpy);
1514 INTERCEPT_FUNCTION(bcopy);
1515 INTERCEPT_FUNCTION(wmemset);
1516 INTERCEPT_FUNCTION(wmemcpy);
1517 INTERCEPT_FUNCTION(wmempcpy);
1518 INTERCEPT_FUNCTION(wmemmove);
1519 INTERCEPT_FUNCTION(strcpy); // NOLINT
1520 INTERCEPT_FUNCTION(stpcpy); // NOLINT
1521 INTERCEPT_FUNCTION(strdup);
1522 MSAN_MAYBE_INTERCEPT___STRDUP;
1523 INTERCEPT_FUNCTION(strncpy); // NOLINT
1524 INTERCEPT_FUNCTION(gcvt);
1525 INTERCEPT_FUNCTION(strcat); // NOLINT
1526 INTERCEPT_FUNCTION(strncat); // NOLINT
1527 INTERCEPT_STRTO(strtod);
1528 INTERCEPT_STRTO(strtof);
1529 INTERCEPT_STRTO(strtold);
1530 INTERCEPT_STRTO(strtol);
1531 INTERCEPT_STRTO(strtoul);
1532 INTERCEPT_STRTO(strtoll);
1533 INTERCEPT_STRTO(strtoull);
1534 INTERCEPT_STRTO(wcstod);
1535 INTERCEPT_STRTO(wcstof);
1536 INTERCEPT_STRTO(wcstold);
1537 INTERCEPT_STRTO(wcstol);
1538 INTERCEPT_STRTO(wcstoul);
1539 INTERCEPT_STRTO(wcstoll);
1540 INTERCEPT_STRTO(wcstoull);
1541 #ifdef SANITIZER_NLDBL_VERSION
1542 INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION);
1543 INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION);
1545 INTERCEPT_FUNCTION(vswprintf);
1546 INTERCEPT_FUNCTION(swprintf);
1548 INTERCEPT_FUNCTION(strxfrm);
1549 INTERCEPT_FUNCTION(strxfrm_l);
1550 INTERCEPT_FUNCTION(strftime);
1551 INTERCEPT_FUNCTION(strftime_l);
1552 MSAN_MAYBE_INTERCEPT___STRFTIME_L;
1553 INTERCEPT_FUNCTION(wcsftime);
1554 INTERCEPT_FUNCTION(wcsftime_l);
1555 MSAN_MAYBE_INTERCEPT___WCSFTIME_L;
1556 INTERCEPT_FUNCTION(mbtowc);
1557 INTERCEPT_FUNCTION(mbrtowc);
1558 INTERCEPT_FUNCTION(wcslen);
1559 INTERCEPT_FUNCTION(wcsnlen);
1560 INTERCEPT_FUNCTION(wcschr);
1561 INTERCEPT_FUNCTION(wcscpy);
1562 INTERCEPT_FUNCTION(wcsncpy);
1563 INTERCEPT_FUNCTION(wcscmp);
1564 INTERCEPT_FUNCTION(getenv);
1565 INTERCEPT_FUNCTION(setenv);
1566 INTERCEPT_FUNCTION(putenv);
1567 INTERCEPT_FUNCTION(gettimeofday);
1568 INTERCEPT_FUNCTION(fcvt);
1569 MSAN_MAYBE_INTERCEPT___FXSTAT;
1570 MSAN_INTERCEPT_FSTATAT;
1571 MSAN_MAYBE_INTERCEPT___FXSTAT64;
1572 MSAN_MAYBE_INTERCEPT___FXSTATAT64;
1573 INTERCEPT_FUNCTION(pipe);
1574 INTERCEPT_FUNCTION(pipe2);
1575 INTERCEPT_FUNCTION(socketpair);
1576 INTERCEPT_FUNCTION(fgets);
1577 MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED;
1578 INTERCEPT_FUNCTION(getrlimit);
1579 MSAN_MAYBE_INTERCEPT_GETRLIMIT64;
1580 MSAN_MAYBE_INTERCEPT_PRLIMIT;
1581 MSAN_MAYBE_INTERCEPT_PRLIMIT64;
1582 MSAN_INTERCEPT_UNAME;
1583 INTERCEPT_FUNCTION(gethostname);
1584 MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1585 MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1586 INTERCEPT_FUNCTION(dladdr);
1587 INTERCEPT_FUNCTION(dlerror);
1588 INTERCEPT_FUNCTION(dl_iterate_phdr);
1589 INTERCEPT_FUNCTION(getrusage);
1590 INTERCEPT_FUNCTION(sigaction);
1591 INTERCEPT_FUNCTION(signal);
1592 #if defined(__mips__)
1593 INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2");
1595 INTERCEPT_FUNCTION(pthread_create);
1597 INTERCEPT_FUNCTION(pthread_key_create);
1598 INTERCEPT_FUNCTION(pthread_join);
1599 INTERCEPT_FUNCTION(tzset);
1600 INTERCEPT_FUNCTION(__cxa_atexit);
1601 INTERCEPT_FUNCTION(shmat);
1602 INTERCEPT_FUNCTION(fork);
1603 INTERCEPT_FUNCTION(openpty);
1604 INTERCEPT_FUNCTION(forkpty);
1608 } // namespace __msan