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_report.h"
23 #include "msan_thread.h"
24 #include "msan_poisoning.h"
25 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
26 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
27 #include "sanitizer_common/sanitizer_allocator.h"
28 #include "sanitizer_common/sanitizer_allocator_interface.h"
29 #include "sanitizer_common/sanitizer_allocator_internal.h"
30 #include "sanitizer_common/sanitizer_atomic.h"
31 #include "sanitizer_common/sanitizer_common.h"
32 #include "sanitizer_common/sanitizer_errno.h"
33 #include "sanitizer_common/sanitizer_stackdepot.h"
34 #include "sanitizer_common/sanitizer_libc.h"
35 #include "sanitizer_common/sanitizer_linux.h"
36 #include "sanitizer_common/sanitizer_tls_get_addr.h"
37 #include "sanitizer_common/sanitizer_vector.h"
40 #define fstat __fstat50
41 #define gettimeofday __gettimeofday50
42 #define getrusage __getrusage50
43 #define tzset __tzset50
47 // ACHTUNG! No other system header includes in this file.
48 // Ideally, we should get rid of stdarg.h as well.
50 using namespace __msan;
52 using __sanitizer::memory_order;
53 using __sanitizer::atomic_load;
54 using __sanitizer::atomic_store;
55 using __sanitizer::atomic_uintptr_t;
57 DECLARE_REAL(SIZE_T, strlen, const char *s)
58 DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
59 DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n)
60 DECLARE_REAL(void *, memset, void *dest, int c, uptr n)
62 // True if this is a nested interceptor.
63 static THREADLOCAL int in_interceptor_scope;
65 void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; }
66 void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; }
68 struct InterceptorScope {
69 InterceptorScope() { ++in_interceptor_scope; }
70 ~InterceptorScope() { --in_interceptor_scope; }
73 bool IsInInterceptorScope() {
74 return in_interceptor_scope;
77 static uptr allocated_for_dlsym;
78 static const uptr kDlsymAllocPoolSize = 1024;
79 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
81 static bool IsInDlsymAllocPool(const void *ptr) {
82 uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
83 return off < sizeof(alloc_memory_for_dlsym);
86 static void *AllocateFromLocalPool(uptr size_in_bytes) {
87 uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
88 void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
89 allocated_for_dlsym += size_in_words;
90 CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
94 #define ENSURE_MSAN_INITED() do { \
95 CHECK(!msan_init_is_running); \
101 // Check that [x, x+n) range is unpoisoned.
102 #define CHECK_UNPOISONED_0(x, n) \
104 sptr __offset = __msan_test_shadow(x, n); \
105 if (__msan::IsInSymbolizer()) break; \
106 if (__offset >= 0 && __msan::flags()->report_umrs) { \
107 GET_CALLER_PC_BP_SP; \
109 ReportUMRInsideAddressRange(__func__, x, n, __offset); \
110 __msan::PrintWarningWithOrigin( \
111 pc, bp, __msan_get_origin((const char *)x + __offset)); \
112 if (__msan::flags()->halt_on_error) { \
113 Printf("Exiting\n"); \
119 // Check that [x, x+n) range is unpoisoned unless we are in a nested
121 #define CHECK_UNPOISONED(x, n) \
123 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
126 #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \
127 CHECK_UNPOISONED((x), \
128 common_flags()->strict_string_checks ? (len) + 1 : (n) )
130 #define CHECK_UNPOISONED_STRING(x, n) \
131 CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n))
133 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
134 INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
136 ENSURE_MSAN_INITED();
137 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
139 __msan_unpoison(ptr, res *size);
142 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked)
144 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED
147 #if !SANITIZER_NETBSD
148 INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
149 return (char *)__msan_memcpy(dest, src, n) + n;
151 #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy)
153 #define MSAN_MAYBE_INTERCEPT_MEMPCPY
156 INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) {
157 ENSURE_MSAN_INITED();
158 void *res = REAL(memccpy)(dest, src, c, n);
159 CHECK(!res || (res >= dest && res <= (char *)dest + n));
160 SIZE_T sz = res ? (char *)res - (char *)dest : n;
161 CHECK_UNPOISONED(src, sz);
162 __msan_unpoison(dest, sz);
166 INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
167 return __msan_memmove(dest, src, n);
170 INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
171 GET_MALLOC_STACK_TRACE;
173 int res = msan_posix_memalign(memptr, alignment, size, &stack);
175 __msan_unpoison(memptr, sizeof(*memptr));
179 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
180 INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
181 GET_MALLOC_STACK_TRACE;
182 return msan_memalign(alignment, size, &stack);
184 #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
186 #define MSAN_MAYBE_INTERCEPT_MEMALIGN
189 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
190 GET_MALLOC_STACK_TRACE;
191 return msan_aligned_alloc(alignment, size, &stack);
194 #if !SANITIZER_NETBSD
195 INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
196 GET_MALLOC_STACK_TRACE;
197 void *ptr = msan_memalign(alignment, size, &stack);
199 DTLS_on_libc_memalign(ptr, size);
202 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
204 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
207 INTERCEPTOR(void *, valloc, SIZE_T size) {
208 GET_MALLOC_STACK_TRACE;
209 return msan_valloc(size, &stack);
212 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
213 INTERCEPTOR(void *, pvalloc, SIZE_T size) {
214 GET_MALLOC_STACK_TRACE;
215 return msan_pvalloc(size, &stack);
217 #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
219 #define MSAN_MAYBE_INTERCEPT_PVALLOC
222 INTERCEPTOR(void, free, void *ptr) {
223 GET_MALLOC_STACK_TRACE;
224 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
225 MsanDeallocate(&stack, ptr);
228 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
229 INTERCEPTOR(void, cfree, void *ptr) {
230 GET_MALLOC_STACK_TRACE;
231 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
232 MsanDeallocate(&stack, ptr);
234 #define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
236 #define MSAN_MAYBE_INTERCEPT_CFREE
239 #if !SANITIZER_NETBSD
240 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
241 return __sanitizer_get_allocated_size(ptr);
243 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
244 INTERCEPT_FUNCTION(malloc_usable_size)
246 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
249 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
250 // This function actually returns a struct by value, but we can't unpoison a
251 // temporary! The following is equivalent on all supported platforms but
252 // aarch64 (which uses a different register for sret value). We have a test
254 INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
257 asm volatile("mov %0,x8" : "=r" (r8));
258 sret = reinterpret_cast<__sanitizer_struct_mallinfo*>(r8);
260 REAL(memset)(sret, 0, sizeof(*sret));
261 __msan_unpoison(sret, sizeof(*sret));
263 #define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
265 #define MSAN_MAYBE_INTERCEPT_MALLINFO
268 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
269 INTERCEPTOR(int, mallopt, int cmd, int value) {
272 #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
274 #define MSAN_MAYBE_INTERCEPT_MALLOPT
277 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
278 INTERCEPTOR(void, malloc_stats, void) {
279 // FIXME: implement, but don't call REAL(malloc_stats)!
281 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats)
283 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS
286 INTERCEPTOR(char *, strcpy, char *dest, const char *src) { // NOLINT
287 ENSURE_MSAN_INITED();
288 GET_STORE_STACK_TRACE;
289 SIZE_T n = REAL(strlen)(src);
290 CHECK_UNPOISONED_STRING(src + n, 0);
291 char *res = REAL(strcpy)(dest, src); // NOLINT
292 CopyShadowAndOrigin(dest, src, n + 1, &stack);
296 INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { // NOLINT
297 ENSURE_MSAN_INITED();
298 GET_STORE_STACK_TRACE;
299 SIZE_T copy_size = REAL(strnlen)(src, n);
301 copy_size++; // trailing \0
302 char *res = REAL(strncpy)(dest, src, n); // NOLINT
303 CopyShadowAndOrigin(dest, src, copy_size, &stack);
304 __msan_unpoison(dest + copy_size, n - copy_size);
308 #if !SANITIZER_NETBSD
309 INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { // NOLINT
310 ENSURE_MSAN_INITED();
311 GET_STORE_STACK_TRACE;
312 SIZE_T n = REAL(strlen)(src);
313 CHECK_UNPOISONED_STRING(src + n, 0);
314 char *res = REAL(stpcpy)(dest, src); // NOLINT
315 CopyShadowAndOrigin(dest, src, n + 1, &stack);
318 #define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy)
320 #define MSAN_MAYBE_INTERCEPT_STPCPY
323 INTERCEPTOR(char *, strdup, char *src) {
324 ENSURE_MSAN_INITED();
325 GET_STORE_STACK_TRACE;
326 // On FreeBSD strdup() leverages strlen().
327 InterceptorScope interceptor_scope;
328 SIZE_T n = REAL(strlen)(src);
329 CHECK_UNPOISONED_STRING(src + n, 0);
330 char *res = REAL(strdup)(src);
331 CopyShadowAndOrigin(res, src, n + 1, &stack);
335 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
336 INTERCEPTOR(char *, __strdup, char *src) {
337 ENSURE_MSAN_INITED();
338 GET_STORE_STACK_TRACE;
339 SIZE_T n = REAL(strlen)(src);
340 CHECK_UNPOISONED_STRING(src + n, 0);
341 char *res = REAL(__strdup)(src);
342 CopyShadowAndOrigin(res, src, n + 1, &stack);
345 #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup)
347 #define MSAN_MAYBE_INTERCEPT___STRDUP
350 #if !SANITIZER_NETBSD
351 INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
352 ENSURE_MSAN_INITED();
353 char *res = REAL(gcvt)(number, ndigit, buf);
354 SIZE_T n = REAL(strlen)(buf);
355 __msan_unpoison(buf, n + 1);
358 #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt)
360 #define MSAN_MAYBE_INTERCEPT_GCVT
363 INTERCEPTOR(char *, strcat, char *dest, const char *src) { // NOLINT
364 ENSURE_MSAN_INITED();
365 GET_STORE_STACK_TRACE;
366 SIZE_T src_size = REAL(strlen)(src);
367 SIZE_T dest_size = REAL(strlen)(dest);
368 CHECK_UNPOISONED_STRING(src + src_size, 0);
369 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
370 char *res = REAL(strcat)(dest, src); // NOLINT
371 CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack);
375 INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { // NOLINT
376 ENSURE_MSAN_INITED();
377 GET_STORE_STACK_TRACE;
378 SIZE_T dest_size = REAL(strlen)(dest);
379 SIZE_T copy_size = REAL(strnlen)(src, n);
380 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
381 char *res = REAL(strncat)(dest, src, n); // NOLINT
382 CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack);
383 __msan_unpoison(dest + dest_size + copy_size, 1); // \0
387 // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to
388 // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO.
389 #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \
390 ENSURE_MSAN_INITED(); \
391 ret_type res = REAL(func)(__VA_ARGS__); \
392 __msan_unpoison(endptr, sizeof(*endptr)); \
395 #define INTERCEPTOR_STRTO(ret_type, func, char_type) \
396 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \
397 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \
400 #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
401 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
403 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \
406 #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \
407 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
409 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \
412 #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \
413 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
414 int base, void *loc) { \
415 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \
419 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
420 INTERCEPTOR_STRTO(ret_type, func, char_type) \
421 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type)
423 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
424 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
425 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type)
428 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
429 INTERCEPTOR_STRTO(ret_type, func, char_type) \
430 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \
431 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \
432 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type)
434 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
435 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
436 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \
437 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \
438 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type)
441 INTERCEPTORS_STRTO(double, strtod, char) // NOLINT
442 INTERCEPTORS_STRTO(float, strtof, char) // NOLINT
443 INTERCEPTORS_STRTO(long double, strtold, char) // NOLINT
444 INTERCEPTORS_STRTO_BASE(long, strtol, char) // NOLINT
445 INTERCEPTORS_STRTO_BASE(long long, strtoll, char) // NOLINT
446 INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char) // NOLINT
447 INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char) // NOLINT
448 INTERCEPTORS_STRTO_BASE(u64, strtouq, char) // NOLINT
450 INTERCEPTORS_STRTO(double, wcstod, wchar_t) // NOLINT
451 INTERCEPTORS_STRTO(float, wcstof, wchar_t) // NOLINT
452 INTERCEPTORS_STRTO(long double, wcstold, wchar_t) // NOLINT
453 INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t) // NOLINT
454 INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t) // NOLINT
455 INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t) // NOLINT
456 INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t) // NOLINT
459 #define INTERCEPT_STRTO(func) \
460 INTERCEPT_FUNCTION(func); \
461 INTERCEPT_FUNCTION(func##_l);
463 #define INTERCEPT_STRTO(func) \
464 INTERCEPT_FUNCTION(func); \
465 INTERCEPT_FUNCTION(func##_l); \
466 INTERCEPT_FUNCTION(__##func##_l); \
467 INTERCEPT_FUNCTION(__##func##_internal);
471 // FIXME: support *wprintf in common format interceptors.
472 INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
473 ENSURE_MSAN_INITED();
474 int res = REAL(vswprintf)(str, size, format, ap);
476 __msan_unpoison(str, 4 * (res + 1));
481 INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
482 ENSURE_MSAN_INITED();
484 va_start(ap, format);
485 int res = vswprintf(str, size, format, ap);
490 #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \
491 ENSURE_MSAN_INITED(); \
492 InterceptorScope interceptor_scope; \
493 ret_type res = REAL(func)(s, __VA_ARGS__); \
494 if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \
497 INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
498 __sanitizer_tm *tm) {
499 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm);
502 INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format,
503 __sanitizer_tm *tm, void *loc) {
504 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc);
507 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
508 INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format,
509 __sanitizer_tm *tm, void *loc) {
510 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm,
513 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l)
515 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L
518 INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format,
519 __sanitizer_tm *tm) {
520 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm);
523 INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
524 __sanitizer_tm *tm, void *loc) {
525 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm,
529 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
530 INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
531 __sanitizer_tm *tm, void *loc) {
532 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm,
535 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l)
537 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L
540 INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
541 ENSURE_MSAN_INITED();
542 int res = REAL(mbtowc)(dest, src, n);
543 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
547 INTERCEPTOR(SIZE_T, mbrtowc, wchar_t *dest, const char *src, SIZE_T n,
549 ENSURE_MSAN_INITED();
550 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
551 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
555 // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
556 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
557 ENSURE_MSAN_INITED();
558 GET_STORE_STACK_TRACE;
559 wchar_t *res = REAL(wmemcpy)(dest, src, n);
560 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
564 #if !SANITIZER_NETBSD
565 INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
566 ENSURE_MSAN_INITED();
567 GET_STORE_STACK_TRACE;
568 wchar_t *res = REAL(wmempcpy)(dest, src, n);
569 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
572 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY INTERCEPT_FUNCTION(wmempcpy)
574 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY
577 INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
578 CHECK(MEM_IS_APP(s));
579 ENSURE_MSAN_INITED();
580 wchar_t *res = REAL(wmemset)(s, c, n);
581 __msan_unpoison(s, n * sizeof(wchar_t));
585 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
586 ENSURE_MSAN_INITED();
587 GET_STORE_STACK_TRACE;
588 wchar_t *res = REAL(wmemmove)(dest, src, n);
589 MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
593 INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
594 ENSURE_MSAN_INITED();
595 int res = REAL(wcscmp)(s1, s2);
599 INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
600 ENSURE_MSAN_INITED();
601 int res = REAL(gettimeofday)(tv, tz);
603 __msan_unpoison(tv, 16);
605 __msan_unpoison(tz, 8);
609 #if !SANITIZER_NETBSD
610 INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
611 ENSURE_MSAN_INITED();
612 char *res = REAL(fcvt)(x, a, b, c);
613 __msan_unpoison(b, sizeof(*b));
614 __msan_unpoison(c, sizeof(*c));
615 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
618 #define MSAN_MAYBE_INTERCEPT_FCVT INTERCEPT_FUNCTION(fcvt)
620 #define MSAN_MAYBE_INTERCEPT_FCVT
623 INTERCEPTOR(char *, getenv, char *name) {
624 if (msan_init_is_running)
625 return REAL(getenv)(name);
626 ENSURE_MSAN_INITED();
627 char *res = REAL(getenv)(name);
628 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
632 extern char **environ;
634 static void UnpoisonEnviron() {
635 char **envp = environ;
636 for (; *envp; ++envp) {
637 __msan_unpoison(envp, sizeof(*envp));
638 __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
640 // Trailing NULL pointer.
641 __msan_unpoison(envp, sizeof(*envp));
644 INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
645 ENSURE_MSAN_INITED();
646 CHECK_UNPOISONED_STRING(name, 0);
647 int res = REAL(setenv)(name, value, overwrite);
648 if (!res) UnpoisonEnviron();
652 INTERCEPTOR(int, putenv, char *string) {
653 ENSURE_MSAN_INITED();
654 int res = REAL(putenv)(string);
655 if (!res) UnpoisonEnviron();
659 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
660 INTERCEPTOR(int, fstat, int fd, void *buf) {
661 ENSURE_MSAN_INITED();
662 int res = REAL(fstat)(fd, buf);
664 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
667 #define MSAN_MAYBE_INTERCEPT_FSTAT INTERCEPT_FUNCTION(fstat)
669 #define MSAN_MAYBE_INTERCEPT_FSTAT
672 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
673 INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
674 ENSURE_MSAN_INITED();
675 int res = REAL(__fxstat)(magic, fd, buf);
677 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
680 #define MSAN_MAYBE_INTERCEPT___FXSTAT INTERCEPT_FUNCTION(__fxstat)
682 #define MSAN_MAYBE_INTERCEPT___FXSTAT
685 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
686 INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
687 ENSURE_MSAN_INITED();
688 int res = REAL(__fxstat64)(magic, fd, buf);
690 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
693 #define MSAN_MAYBE_INTERCEPT___FXSTAT64 INTERCEPT_FUNCTION(__fxstat64)
695 #define MSAN_MAYBE_INTERCEPT___FXSTAT64
698 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
699 INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
700 ENSURE_MSAN_INITED();
701 int res = REAL(fstatat)(fd, pathname, buf, flags);
702 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
705 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(fstatat)
707 INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
709 ENSURE_MSAN_INITED();
710 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
711 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
714 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(__fxstatat)
717 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
718 INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
720 ENSURE_MSAN_INITED();
721 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
722 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
725 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64 INTERCEPT_FUNCTION(__fxstatat64)
727 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64
730 INTERCEPTOR(int, pipe, int pipefd[2]) {
731 if (msan_init_is_running)
732 return REAL(pipe)(pipefd);
733 ENSURE_MSAN_INITED();
734 int res = REAL(pipe)(pipefd);
736 __msan_unpoison(pipefd, sizeof(int[2]));
740 INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
741 ENSURE_MSAN_INITED();
742 int res = REAL(pipe2)(pipefd, flags);
744 __msan_unpoison(pipefd, sizeof(int[2]));
748 INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
749 ENSURE_MSAN_INITED();
750 int res = REAL(socketpair)(domain, type, protocol, sv);
752 __msan_unpoison(sv, sizeof(int[2]));
756 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
757 INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
758 ENSURE_MSAN_INITED();
759 char *res = REAL(fgets_unlocked)(s, size, stream);
761 __msan_unpoison(s, REAL(strlen)(s) + 1);
764 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked)
766 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED
769 INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
770 if (msan_init_is_running)
771 return REAL(getrlimit)(resource, rlim);
772 ENSURE_MSAN_INITED();
773 int res = REAL(getrlimit)(resource, rlim);
775 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz);
779 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
780 INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
781 if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim);
782 ENSURE_MSAN_INITED();
783 int res = REAL(getrlimit64)(resource, rlim);
784 if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
788 INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit,
790 if (msan_init_is_running)
791 return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
792 ENSURE_MSAN_INITED();
793 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz);
794 int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
795 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz);
799 INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit,
801 if (msan_init_is_running)
802 return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
803 ENSURE_MSAN_INITED();
804 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz);
805 int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
806 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz);
810 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64)
811 #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit)
812 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64)
814 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64
815 #define MSAN_MAYBE_INTERCEPT_PRLIMIT
816 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64
819 #if SANITIZER_FREEBSD
820 // FreeBSD's <sys/utsname.h> define uname() as
821 // static __inline int uname(struct utsname *name) {
822 // return __xuname(SYS_NMLN, (void*)name);
824 INTERCEPTOR(int, __xuname, int size, void *utsname) {
825 ENSURE_MSAN_INITED();
826 int res = REAL(__xuname)(size, utsname);
828 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
831 #define MSAN_INTERCEPT_UNAME INTERCEPT_FUNCTION(__xuname)
833 INTERCEPTOR(int, uname, struct utsname *utsname) {
834 ENSURE_MSAN_INITED();
835 int res = REAL(uname)(utsname);
837 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
840 #define MSAN_INTERCEPT_UNAME INTERCEPT_FUNCTION(uname)
843 INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
844 ENSURE_MSAN_INITED();
845 int res = REAL(gethostname)(name, len);
847 SIZE_T real_len = REAL(strnlen)(name, len);
850 __msan_unpoison(name, real_len);
855 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
856 INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
858 ENSURE_MSAN_INITED();
859 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
861 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
865 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
867 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT
870 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
871 INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
872 int timeout, void *sigmask) {
873 ENSURE_MSAN_INITED();
874 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
876 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
880 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
882 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
885 INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
886 GET_MALLOC_STACK_TRACE;
887 if (UNLIKELY(!msan_inited))
888 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
889 return AllocateFromLocalPool(nmemb * size);
890 return msan_calloc(nmemb, size, &stack);
893 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
894 GET_MALLOC_STACK_TRACE;
895 if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
896 uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
897 uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
899 if (UNLIKELY(!msan_inited)) {
900 new_ptr = AllocateFromLocalPool(copy_size);
903 new_ptr = msan_malloc(copy_size, &stack);
905 internal_memcpy(new_ptr, ptr, copy_size);
908 return msan_realloc(ptr, size, &stack);
911 INTERCEPTOR(void *, malloc, SIZE_T size) {
912 GET_MALLOC_STACK_TRACE;
913 if (UNLIKELY(!msan_inited))
914 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
915 return AllocateFromLocalPool(size);
916 return msan_malloc(size, &stack);
919 void __msan_allocated_memory(const void *data, uptr size) {
920 GET_MALLOC_STACK_TRACE;
921 if (flags()->poison_in_malloc) {
922 stack.tag = STACK_TRACE_TAG_POISON;
923 PoisonMemory(data, size, &stack);
927 void __msan_copy_shadow(void *dest, const void *src, uptr n) {
928 GET_STORE_STACK_TRACE;
929 MoveShadowAndOrigin(dest, src, n, &stack);
932 void __sanitizer_dtor_callback(const void *data, uptr size) {
933 GET_MALLOC_STACK_TRACE;
934 if (flags()->poison_in_dtor) {
935 stack.tag = STACK_TRACE_TAG_POISON;
936 PoisonMemory(data, size, &stack);
940 template <class Mmap>
941 static void *mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length,
942 int prot, int flags, int fd, OFF64_T offset) {
943 if (addr && !MEM_IS_APP(addr)) {
944 if (flags & map_fixed) {
945 errno = errno_EINVAL;
951 void *res = real_mmap(addr, length, prot, flags, fd, offset);
952 if (res != (void *)-1) __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
956 INTERCEPTOR(int, getrusage, int who, void *usage) {
957 ENSURE_MSAN_INITED();
958 int res = REAL(getrusage)(who, usage);
960 __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
965 class SignalHandlerScope {
967 SignalHandlerScope() {
968 if (MsanThread *t = GetCurrentThread())
969 t->EnterSignalHandler();
971 ~SignalHandlerScope() {
972 if (MsanThread *t = GetCurrentThread())
973 t->LeaveSignalHandler();
977 // sigactions_mu guarantees atomicity of sigaction() and signal() calls.
978 // Access to sigactions[] is gone with relaxed atomics to avoid data race with
979 // the signal handler.
980 const int kMaxSignals = 1024;
981 static atomic_uintptr_t sigactions[kMaxSignals];
982 static StaticSpinMutex sigactions_mu;
984 static void SignalHandler(int signo) {
985 SignalHandlerScope signal_handler_scope;
986 ScopedThreadLocalStateBackup stlsb;
989 typedef void (*signal_cb)(int x);
991 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
995 static void SignalAction(int signo, void *si, void *uc) {
996 SignalHandlerScope signal_handler_scope;
997 ScopedThreadLocalStateBackup stlsb;
999 __msan_unpoison(si, sizeof(__sanitizer_sigaction));
1000 __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
1002 typedef void (*sigaction_cb)(int, void *, void *);
1004 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1008 static void read_sigaction(const __sanitizer_sigaction *act) {
1009 CHECK_UNPOISONED(&act->sa_flags, sizeof(act->sa_flags));
1010 if (act->sa_flags & __sanitizer::sa_siginfo)
1011 CHECK_UNPOISONED(&act->sigaction, sizeof(act->sigaction));
1013 CHECK_UNPOISONED(&act->handler, sizeof(act->handler));
1014 CHECK_UNPOISONED(&act->sa_mask, sizeof(act->sa_mask));
1017 extern "C" int pthread_attr_init(void *attr);
1018 extern "C" int pthread_attr_destroy(void *attr);
1020 static void *MsanThreadStartFunc(void *arg) {
1021 MsanThread *t = (MsanThread *)arg;
1022 SetCurrentThread(t);
1023 return t->ThreadStart();
1026 INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1028 ENSURE_MSAN_INITED(); // for GetTlsSize()
1029 __sanitizer_pthread_attr_t myattr;
1031 pthread_attr_init(&myattr);
1035 AdjustStackSize(attr);
1037 MsanThread *t = MsanThread::Create(callback, param);
1039 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t);
1041 if (attr == &myattr)
1042 pthread_attr_destroy(&myattr);
1044 __msan_unpoison(th, __sanitizer::pthread_t_sz);
1049 INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1050 void (*dtor)(void *value)) {
1051 if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor);
1052 ENSURE_MSAN_INITED();
1053 int res = REAL(pthread_key_create)(key, dtor);
1055 __msan_unpoison(key, sizeof(*key));
1059 #if SANITIZER_NETBSD
1060 INTERCEPTOR(void, __libc_thr_keycreate, void *m, void (*dtor)(void *value)) \
1061 ALIAS(WRAPPER_NAME(pthread_key_create));
1064 INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1065 ENSURE_MSAN_INITED();
1066 int res = REAL(pthread_join)(th, retval);
1068 __msan_unpoison(retval, sizeof(*retval));
1072 extern char *tzname[2];
1074 INTERCEPTOR(void, tzset, int fake) {
1075 ENSURE_MSAN_INITED();
1076 InterceptorScope interceptor_scope;
1079 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1);
1081 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1);
1085 struct MSanAtExitRecord {
1086 void (*func)(void *arg);
1090 struct InterceptorContext {
1091 BlockingMutex atexit_mu;
1092 Vector<struct MSanAtExitRecord *> AtExitStack;
1094 InterceptorContext()
1099 static ALIGNED(64) char interceptor_placeholder[sizeof(InterceptorContext)];
1100 InterceptorContext *interceptor_ctx() {
1101 return reinterpret_cast<InterceptorContext*>(&interceptor_placeholder[0]);
1104 void MSanAtExitWrapper() {
1105 MSanAtExitRecord *r;
1107 BlockingMutexLock l(&interceptor_ctx()->atexit_mu);
1109 uptr element = interceptor_ctx()->AtExitStack.Size() - 1;
1110 r = interceptor_ctx()->AtExitStack[element];
1111 interceptor_ctx()->AtExitStack.PopBack();
1115 ((void(*)())r->func)();
1119 void MSanCxaAtExitWrapper(void *arg) {
1121 MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
1126 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso);
1128 // Unpoison argument shadow for C++ module destructors.
1129 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
1131 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
1132 return setup_at_exit_wrapper((void(*)())func, arg, dso_handle);
1135 // Unpoison argument shadow for C++ module destructors.
1136 INTERCEPTOR(int, atexit, void (*func)()) {
1137 // Avoid calling real atexit as it is unrechable on at least on Linux.
1138 if (msan_init_is_running)
1139 return REAL(__cxa_atexit)((void (*)(void *a))func, 0, 0);
1140 return setup_at_exit_wrapper((void(*)())func, 0, 0);
1143 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso) {
1144 ENSURE_MSAN_INITED();
1145 MSanAtExitRecord *r =
1146 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
1147 r->func = (void(*)(void *a))f;
1151 // NetBSD does not preserve the 2nd argument if dso is equal to 0
1152 // Store ctx in a local stack-like structure
1154 BlockingMutexLock l(&interceptor_ctx()->atexit_mu);
1156 res = REAL(__cxa_atexit)((void (*)(void *a))MSanAtExitWrapper, 0, 0);
1158 interceptor_ctx()->AtExitStack.PushBack(r);
1161 res = REAL(__cxa_atexit)(MSanCxaAtExitWrapper, r, dso);
1166 static void BeforeFork() {
1167 StackDepotLockAll();
1168 ChainedOriginDepotLockAll();
1171 static void AfterFork() {
1172 ChainedOriginDepotUnlockAll();
1173 StackDepotUnlockAll();
1176 INTERCEPTOR(int, fork, void) {
1177 ENSURE_MSAN_INITED();
1179 int pid = REAL(fork)();
1184 // NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly
1187 INTERCEPTOR(int, openpty, int *amaster, int *aslave, char *name,
1188 const void *termp, const void *winp) {
1189 ENSURE_MSAN_INITED();
1190 InterceptorScope interceptor_scope;
1191 int res = REAL(openpty)(amaster, aslave, name, termp, winp);
1193 __msan_unpoison(amaster, sizeof(*amaster));
1194 __msan_unpoison(aslave, sizeof(*aslave));
1198 #define MSAN_MAYBE_INTERCEPT_OPENPTY INTERCEPT_FUNCTION(openpty)
1200 #define MSAN_MAYBE_INTERCEPT_OPENPTY
1203 // NetBSD ships with forkpty(3) in -lutil, that needs to be prebuilt explicitly
1206 INTERCEPTOR(int, forkpty, int *amaster, char *name, const void *termp,
1208 ENSURE_MSAN_INITED();
1209 InterceptorScope interceptor_scope;
1210 int res = REAL(forkpty)(amaster, name, termp, winp);
1212 __msan_unpoison(amaster, sizeof(*amaster));
1215 #define MSAN_MAYBE_INTERCEPT_FORKPTY INTERCEPT_FUNCTION(forkpty)
1217 #define MSAN_MAYBE_INTERCEPT_FORKPTY
1220 struct MSanInterceptorContext {
1221 bool in_interceptor_scope;
1227 // FIXME: ask frontend whether we need to return failure.
1231 } // namespace __msan
1233 // A version of CHECK_UNPOISONED using a saved scope value. Used in common
1235 #define CHECK_UNPOISONED_CTX(ctx, x, n) \
1237 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1238 CHECK_UNPOISONED_0(x, n); \
1241 #define MSAN_INTERCEPT_FUNC(name) \
1243 if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
1244 VReport(1, "MemorySanitizer: failed to intercept '" #name "'\n"); \
1247 #define MSAN_INTERCEPT_FUNC_VER(name, ver) \
1249 if ((!INTERCEPT_FUNCTION_VER(name, ver) || !REAL(name))) \
1251 1, "MemorySanitizer: failed to intercept '" #name "@@" #ver "'\n"); \
1254 #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name)
1255 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
1256 MSAN_INTERCEPT_FUNC_VER(name, ver)
1257 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \
1258 UnpoisonParam(count)
1259 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
1260 __msan_unpoison(ptr, size)
1261 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
1262 CHECK_UNPOISONED_CTX(ctx, ptr, size)
1263 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
1264 __msan_unpoison(ptr, size)
1265 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
1266 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \
1267 ENSURE_MSAN_INITED(); \
1268 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1269 ctx = (void *)&msan_ctx; \
1271 InterceptorScope interceptor_scope; \
1272 __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */
1273 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
1276 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1279 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1282 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1285 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
1287 } while (false) // FIXME
1288 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
1290 } while (false) // FIXME
1291 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
1292 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
1293 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
1295 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \
1296 if (filename && map) \
1297 ForEachMappedRegion(map, __msan_unpoison); \
1300 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
1301 if (MsanThread *t = GetCurrentThread()) { \
1302 *begin = t->tls_begin(); \
1303 *end = t->tls_end(); \
1305 *begin = *end = 0; \
1308 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
1311 return __msan_memset(block, c, size); \
1313 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
1316 return __msan_memmove(to, from, size); \
1318 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
1321 return __msan_memcpy(to, from, size); \
1324 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
1326 GET_STORE_STACK_TRACE; \
1327 CopyShadowAndOrigin(to, from, size, &stack); \
1328 __msan_unpoison(to + size, 1); \
1331 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, fd, \
1334 return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \
1337 #include "sanitizer_common/sanitizer_platform_interceptors.h"
1338 #include "sanitizer_common/sanitizer_common_interceptors.inc"
1340 static uptr signal_impl(int signo, uptr cb);
1341 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1342 __sanitizer_sigaction *oldact);
1344 #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \
1345 { return sigaction_impl(signo, act, oldact); }
1347 #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \
1349 handler = signal_impl(signo, handler); \
1350 InterceptorScope interceptor_scope; \
1351 return REAL(func)(signo, handler); \
1354 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
1356 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1357 __sanitizer_sigaction *oldact) {
1358 ENSURE_MSAN_INITED();
1359 if (act) read_sigaction(act);
1361 if (flags()->wrap_signals) {
1362 SpinMutexLock lock(&sigactions_mu);
1363 CHECK_LT(signo, kMaxSignals);
1364 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
1365 __sanitizer_sigaction new_act;
1366 __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr;
1368 REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction));
1369 uptr cb = (uptr)pnew_act->sigaction;
1370 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
1371 ? (uptr)SignalAction
1372 : (uptr)SignalHandler;
1373 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1374 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1375 pnew_act->sigaction = (decltype(pnew_act->sigaction))new_cb;
1378 res = REAL(SIGACTION_SYMNAME)(signo, pnew_act, oldact);
1379 if (res == 0 && oldact) {
1380 uptr cb = (uptr)oldact->sigaction;
1381 if (cb == (uptr)SignalAction || cb == (uptr)SignalHandler) {
1382 oldact->sigaction = (decltype(oldact->sigaction))old_cb;
1386 res = REAL(SIGACTION_SYMNAME)(signo, act, oldact);
1389 if (res == 0 && oldact) {
1390 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
1395 static uptr signal_impl(int signo, uptr cb) {
1396 ENSURE_MSAN_INITED();
1397 if (flags()->wrap_signals) {
1398 CHECK_LT(signo, kMaxSignals);
1399 SpinMutexLock lock(&sigactions_mu);
1400 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1401 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1402 cb = (uptr)&SignalHandler;
1408 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
1409 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
1412 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
1415 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1416 #include "sanitizer_common/sanitizer_common_syscalls.inc"
1417 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
1426 INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
1428 COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info);
1429 int res = REAL(dladdr)(addr, info);
1431 __msan_unpoison(info, sizeof(*info));
1432 if (info->dli_fname)
1433 __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
1434 if (info->dli_sname)
1435 __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
1440 INTERCEPTOR(char *, dlerror, int fake) {
1442 COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake);
1443 char *res = REAL(dlerror)(fake);
1444 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
1448 typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
1450 struct dl_iterate_phdr_data {
1451 dl_iterate_phdr_cb callback;
1455 static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
1458 __msan_unpoison(info, size);
1459 if (info->dlpi_phdr && info->dlpi_phnum)
1460 __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum);
1461 if (info->dlpi_name)
1462 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
1464 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
1466 return cbdata->callback(info, size, cbdata->data);
1469 INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) {
1470 ENSURE_MSAN_INITED();
1471 void *p = REAL(shmat)(shmid, shmaddr, shmflg);
1472 if (p != (void *)-1) {
1473 __sanitizer_shmid_ds ds;
1474 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds);
1476 __msan_unpoison(p, ds.shm_segsz);
1482 INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
1484 COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data);
1485 dl_iterate_phdr_data cbdata;
1486 cbdata.callback = callback;
1488 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
1492 // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
1493 INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
1494 ENSURE_MSAN_INITED();
1495 wchar_t *res = REAL(wcschr)(s, wc, ps);
1499 // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
1500 INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
1501 ENSURE_MSAN_INITED();
1502 GET_STORE_STACK_TRACE;
1503 wchar_t *res = REAL(wcscpy)(dest, src);
1504 CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1),
1509 INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src,
1510 SIZE_T n) { // NOLINT
1511 ENSURE_MSAN_INITED();
1512 GET_STORE_STACK_TRACE;
1513 SIZE_T copy_size = REAL(wcsnlen)(src, n);
1514 if (copy_size < n) copy_size++; // trailing \0
1515 wchar_t *res = REAL(wcsncpy)(dest, src, n); // NOLINT
1516 CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack);
1517 __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t));
1521 // These interface functions reside here so that they can use
1522 // REAL(memset), etc.
1523 void __msan_unpoison(const void *a, uptr size) {
1524 if (!MEM_IS_APP(a)) return;
1525 SetShadow(a, size, 0);
1528 void __msan_poison(const void *a, uptr size) {
1529 if (!MEM_IS_APP(a)) return;
1530 SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1);
1533 void __msan_poison_stack(void *a, uptr size) {
1534 if (!MEM_IS_APP(a)) return;
1535 SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1);
1538 void __msan_clear_and_unpoison(void *a, uptr size) {
1539 REAL(memset)(a, 0, size);
1540 SetShadow(a, size, 0);
1543 void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
1544 if (!msan_inited) return internal_memcpy(dest, src, n);
1545 if (msan_init_is_running || __msan::IsInSymbolizer())
1546 return REAL(memcpy)(dest, src, n);
1547 ENSURE_MSAN_INITED();
1548 GET_STORE_STACK_TRACE;
1549 void *res = REAL(memcpy)(dest, src, n);
1550 CopyShadowAndOrigin(dest, src, n, &stack);
1554 void *__msan_memset(void *s, int c, SIZE_T n) {
1555 if (!msan_inited) return internal_memset(s, c, n);
1556 if (msan_init_is_running) return REAL(memset)(s, c, n);
1557 ENSURE_MSAN_INITED();
1558 void *res = REAL(memset)(s, c, n);
1559 __msan_unpoison(s, n);
1563 void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
1564 if (!msan_inited) return internal_memmove(dest, src, n);
1565 if (msan_init_is_running) return REAL(memmove)(dest, src, n);
1566 ENSURE_MSAN_INITED();
1567 GET_STORE_STACK_TRACE;
1568 void *res = REAL(memmove)(dest, src, n);
1569 MoveShadowAndOrigin(dest, src, n, &stack);
1573 void __msan_unpoison_string(const char* s) {
1574 if (!MEM_IS_APP(s)) return;
1575 __msan_unpoison(s, REAL(strlen)(s) + 1);
1580 void InitializeInterceptors() {
1581 static int inited = 0;
1582 CHECK_EQ(inited, 0);
1584 new(interceptor_ctx()) InterceptorContext();
1586 InitializeCommonInterceptors();
1587 InitializeSignalInterceptors();
1589 INTERCEPT_FUNCTION(posix_memalign);
1590 MSAN_MAYBE_INTERCEPT_MEMALIGN;
1591 MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
1592 INTERCEPT_FUNCTION(valloc);
1593 MSAN_MAYBE_INTERCEPT_PVALLOC;
1594 INTERCEPT_FUNCTION(malloc);
1595 INTERCEPT_FUNCTION(calloc);
1596 INTERCEPT_FUNCTION(realloc);
1597 INTERCEPT_FUNCTION(free);
1598 MSAN_MAYBE_INTERCEPT_CFREE;
1599 MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
1600 MSAN_MAYBE_INTERCEPT_MALLINFO;
1601 MSAN_MAYBE_INTERCEPT_MALLOPT;
1602 MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
1603 INTERCEPT_FUNCTION(fread);
1604 MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED;
1605 INTERCEPT_FUNCTION(memccpy);
1606 MSAN_MAYBE_INTERCEPT_MEMPCPY;
1607 INTERCEPT_FUNCTION(bcopy);
1608 INTERCEPT_FUNCTION(wmemset);
1609 INTERCEPT_FUNCTION(wmemcpy);
1610 MSAN_MAYBE_INTERCEPT_WMEMPCPY;
1611 INTERCEPT_FUNCTION(wmemmove);
1612 INTERCEPT_FUNCTION(strcpy); // NOLINT
1613 MSAN_MAYBE_INTERCEPT_STPCPY; // NOLINT
1614 INTERCEPT_FUNCTION(strdup);
1615 MSAN_MAYBE_INTERCEPT___STRDUP;
1616 INTERCEPT_FUNCTION(strncpy); // NOLINT
1617 MSAN_MAYBE_INTERCEPT_GCVT;
1618 INTERCEPT_FUNCTION(strcat); // NOLINT
1619 INTERCEPT_FUNCTION(strncat); // NOLINT
1620 INTERCEPT_STRTO(strtod);
1621 INTERCEPT_STRTO(strtof);
1622 INTERCEPT_STRTO(strtold);
1623 INTERCEPT_STRTO(strtol);
1624 INTERCEPT_STRTO(strtoul);
1625 INTERCEPT_STRTO(strtoll);
1626 INTERCEPT_STRTO(strtoull);
1627 INTERCEPT_STRTO(strtouq);
1628 INTERCEPT_STRTO(wcstod);
1629 INTERCEPT_STRTO(wcstof);
1630 INTERCEPT_STRTO(wcstold);
1631 INTERCEPT_STRTO(wcstol);
1632 INTERCEPT_STRTO(wcstoul);
1633 INTERCEPT_STRTO(wcstoll);
1634 INTERCEPT_STRTO(wcstoull);
1635 #ifdef SANITIZER_NLDBL_VERSION
1636 INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION);
1637 INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION);
1639 INTERCEPT_FUNCTION(vswprintf);
1640 INTERCEPT_FUNCTION(swprintf);
1642 INTERCEPT_FUNCTION(strftime);
1643 INTERCEPT_FUNCTION(strftime_l);
1644 MSAN_MAYBE_INTERCEPT___STRFTIME_L;
1645 INTERCEPT_FUNCTION(wcsftime);
1646 INTERCEPT_FUNCTION(wcsftime_l);
1647 MSAN_MAYBE_INTERCEPT___WCSFTIME_L;
1648 INTERCEPT_FUNCTION(mbtowc);
1649 INTERCEPT_FUNCTION(mbrtowc);
1650 INTERCEPT_FUNCTION(wcslen);
1651 INTERCEPT_FUNCTION(wcsnlen);
1652 INTERCEPT_FUNCTION(wcschr);
1653 INTERCEPT_FUNCTION(wcscpy);
1654 INTERCEPT_FUNCTION(wcsncpy);
1655 INTERCEPT_FUNCTION(wcscmp);
1656 INTERCEPT_FUNCTION(getenv);
1657 INTERCEPT_FUNCTION(setenv);
1658 INTERCEPT_FUNCTION(putenv);
1659 INTERCEPT_FUNCTION(gettimeofday);
1660 MSAN_MAYBE_INTERCEPT_FCVT;
1661 MSAN_MAYBE_INTERCEPT_FSTAT;
1662 MSAN_MAYBE_INTERCEPT___FXSTAT;
1663 MSAN_INTERCEPT_FSTATAT;
1664 MSAN_MAYBE_INTERCEPT___FXSTAT64;
1665 MSAN_MAYBE_INTERCEPT___FXSTATAT64;
1666 INTERCEPT_FUNCTION(pipe);
1667 INTERCEPT_FUNCTION(pipe2);
1668 INTERCEPT_FUNCTION(socketpair);
1669 MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED;
1670 INTERCEPT_FUNCTION(getrlimit);
1671 MSAN_MAYBE_INTERCEPT_GETRLIMIT64;
1672 MSAN_MAYBE_INTERCEPT_PRLIMIT;
1673 MSAN_MAYBE_INTERCEPT_PRLIMIT64;
1674 MSAN_INTERCEPT_UNAME;
1675 INTERCEPT_FUNCTION(gethostname);
1676 MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1677 MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1678 INTERCEPT_FUNCTION(dladdr);
1679 INTERCEPT_FUNCTION(dlerror);
1680 INTERCEPT_FUNCTION(dl_iterate_phdr);
1681 INTERCEPT_FUNCTION(getrusage);
1682 #if defined(__mips__)
1683 INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2");
1685 INTERCEPT_FUNCTION(pthread_create);
1687 INTERCEPT_FUNCTION(pthread_key_create);
1689 #if SANITIZER_NETBSD
1690 INTERCEPT_FUNCTION(__libc_thr_keycreate);
1693 INTERCEPT_FUNCTION(pthread_join);
1694 INTERCEPT_FUNCTION(tzset);
1695 INTERCEPT_FUNCTION(atexit);
1696 INTERCEPT_FUNCTION(__cxa_atexit);
1697 INTERCEPT_FUNCTION(shmat);
1698 INTERCEPT_FUNCTION(fork);
1699 MSAN_MAYBE_INTERCEPT_OPENPTY;
1700 MSAN_MAYBE_INTERCEPT_FORKPTY;
1704 } // namespace __msan