1 //===-- msan_interceptors.cpp ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of MemorySanitizer.
11 // Interceptors for standard library functions.
13 // FIXME: move as many interceptors as possible into
14 // sanitizer_common/sanitizer_common_interceptors.h
15 //===----------------------------------------------------------------------===//
17 #include "interception/interception.h"
19 #include "msan_chained_origin_depot.h"
20 #include "msan_origin.h"
21 #include "msan_report.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_platform_limits_netbsd.h"
26 #include "sanitizer_common/sanitizer_allocator.h"
27 #include "sanitizer_common/sanitizer_allocator_interface.h"
28 #include "sanitizer_common/sanitizer_allocator_internal.h"
29 #include "sanitizer_common/sanitizer_atomic.h"
30 #include "sanitizer_common/sanitizer_common.h"
31 #include "sanitizer_common/sanitizer_errno.h"
32 #include "sanitizer_common/sanitizer_stackdepot.h"
33 #include "sanitizer_common/sanitizer_libc.h"
34 #include "sanitizer_common/sanitizer_linux.h"
35 #include "sanitizer_common/sanitizer_tls_get_addr.h"
36 #include "sanitizer_common/sanitizer_vector.h"
39 #define fstat __fstat50
40 #define gettimeofday __gettimeofday50
41 #define getrusage __getrusage50
42 #define tzset __tzset50
46 // ACHTUNG! No other system header includes in this file.
47 // Ideally, we should get rid of stdarg.h as well.
49 using namespace __msan;
51 using __sanitizer::memory_order;
52 using __sanitizer::atomic_load;
53 using __sanitizer::atomic_store;
54 using __sanitizer::atomic_uintptr_t;
56 DECLARE_REAL(SIZE_T, strlen, const char *s)
57 DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
58 DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n)
59 DECLARE_REAL(void *, memset, void *dest, int c, uptr n)
61 // True if this is a nested interceptor.
62 static THREADLOCAL int in_interceptor_scope;
64 void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; }
65 void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; }
67 struct InterceptorScope {
68 InterceptorScope() { ++in_interceptor_scope; }
69 ~InterceptorScope() { --in_interceptor_scope; }
72 bool IsInInterceptorScope() {
73 return in_interceptor_scope;
76 static uptr allocated_for_dlsym;
77 static const uptr kDlsymAllocPoolSize = 1024;
78 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
80 static bool IsInDlsymAllocPool(const void *ptr) {
81 uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
82 return off < sizeof(alloc_memory_for_dlsym);
85 static void *AllocateFromLocalPool(uptr size_in_bytes) {
86 uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
87 void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
88 allocated_for_dlsym += size_in_words;
89 CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
93 #define ENSURE_MSAN_INITED() do { \
94 CHECK(!msan_init_is_running); \
100 // Check that [x, x+n) range is unpoisoned.
101 #define CHECK_UNPOISONED_0(x, n) \
103 sptr __offset = __msan_test_shadow(x, n); \
104 if (__msan::IsInSymbolizer()) break; \
105 if (__offset >= 0 && __msan::flags()->report_umrs) { \
106 GET_CALLER_PC_BP_SP; \
108 ReportUMRInsideAddressRange(__func__, x, n, __offset); \
109 __msan::PrintWarningWithOrigin( \
110 pc, bp, __msan_get_origin((const char *)x + __offset)); \
111 if (__msan::flags()->halt_on_error) { \
112 Printf("Exiting\n"); \
118 // Check that [x, x+n) range is unpoisoned unless we are in a nested
120 #define CHECK_UNPOISONED(x, n) \
122 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
125 #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \
126 CHECK_UNPOISONED((x), \
127 common_flags()->strict_string_checks ? (len) + 1 : (n) )
129 #define CHECK_UNPOISONED_STRING(x, n) \
130 CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n))
132 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
133 INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
135 ENSURE_MSAN_INITED();
136 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
138 __msan_unpoison(ptr, res *size);
141 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked)
143 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED
146 #if !SANITIZER_NETBSD
147 INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
148 return (char *)__msan_memcpy(dest, src, n) + n;
150 #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy)
152 #define MSAN_MAYBE_INTERCEPT_MEMPCPY
155 INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) {
156 ENSURE_MSAN_INITED();
157 void *res = REAL(memccpy)(dest, src, c, n);
158 CHECK(!res || (res >= dest && res <= (char *)dest + n));
159 SIZE_T sz = res ? (char *)res - (char *)dest : n;
160 CHECK_UNPOISONED(src, sz);
161 __msan_unpoison(dest, sz);
165 INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
166 return __msan_memmove(dest, src, n);
169 INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
170 GET_MALLOC_STACK_TRACE;
172 int res = msan_posix_memalign(memptr, alignment, size, &stack);
174 __msan_unpoison(memptr, sizeof(*memptr));
178 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
179 INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
180 GET_MALLOC_STACK_TRACE;
181 return msan_memalign(alignment, size, &stack);
183 #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
185 #define MSAN_MAYBE_INTERCEPT_MEMALIGN
188 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
189 GET_MALLOC_STACK_TRACE;
190 return msan_aligned_alloc(alignment, size, &stack);
193 #if !SANITIZER_NETBSD
194 INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
195 GET_MALLOC_STACK_TRACE;
196 void *ptr = msan_memalign(alignment, size, &stack);
198 DTLS_on_libc_memalign(ptr, size);
201 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
203 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
206 INTERCEPTOR(void *, valloc, SIZE_T size) {
207 GET_MALLOC_STACK_TRACE;
208 return msan_valloc(size, &stack);
211 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
212 INTERCEPTOR(void *, pvalloc, SIZE_T size) {
213 GET_MALLOC_STACK_TRACE;
214 return msan_pvalloc(size, &stack);
216 #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
218 #define MSAN_MAYBE_INTERCEPT_PVALLOC
221 INTERCEPTOR(void, free, void *ptr) {
222 GET_MALLOC_STACK_TRACE;
223 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
224 MsanDeallocate(&stack, ptr);
227 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
228 INTERCEPTOR(void, cfree, void *ptr) {
229 GET_MALLOC_STACK_TRACE;
230 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
231 MsanDeallocate(&stack, ptr);
233 #define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
235 #define MSAN_MAYBE_INTERCEPT_CFREE
238 #if !SANITIZER_NETBSD
239 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
240 return __sanitizer_get_allocated_size(ptr);
242 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
243 INTERCEPT_FUNCTION(malloc_usable_size)
245 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
248 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
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_struct_mallinfo *sret) {
256 asm volatile("mov %0,x8" : "=r" (r8));
257 sret = reinterpret_cast<__sanitizer_struct_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 && !SANITIZER_NETBSD
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 && !SANITIZER_NETBSD
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) {
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);
291 CopyShadowAndOrigin(dest, src, n + 1, &stack);
295 INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) {
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);
302 CopyShadowAndOrigin(dest, src, copy_size, &stack);
303 __msan_unpoison(dest + copy_size, n - copy_size);
307 #if !SANITIZER_NETBSD
308 INTERCEPTOR(char *, stpcpy, char *dest, const char *src) {
309 ENSURE_MSAN_INITED();
310 GET_STORE_STACK_TRACE;
311 SIZE_T n = REAL(strlen)(src);
312 CHECK_UNPOISONED_STRING(src + n, 0);
313 char *res = REAL(stpcpy)(dest, src);
314 CopyShadowAndOrigin(dest, src, n + 1, &stack);
317 #define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy)
319 #define MSAN_MAYBE_INTERCEPT_STPCPY
322 INTERCEPTOR(char *, strdup, char *src) {
323 ENSURE_MSAN_INITED();
324 GET_STORE_STACK_TRACE;
325 // On FreeBSD strdup() leverages strlen().
326 InterceptorScope interceptor_scope;
327 SIZE_T n = REAL(strlen)(src);
328 CHECK_UNPOISONED_STRING(src + n, 0);
329 char *res = REAL(strdup)(src);
330 CopyShadowAndOrigin(res, src, n + 1, &stack);
334 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
335 INTERCEPTOR(char *, __strdup, char *src) {
336 ENSURE_MSAN_INITED();
337 GET_STORE_STACK_TRACE;
338 SIZE_T n = REAL(strlen)(src);
339 CHECK_UNPOISONED_STRING(src + n, 0);
340 char *res = REAL(__strdup)(src);
341 CopyShadowAndOrigin(res, src, n + 1, &stack);
344 #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup)
346 #define MSAN_MAYBE_INTERCEPT___STRDUP
349 #if !SANITIZER_NETBSD
350 INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
351 ENSURE_MSAN_INITED();
352 char *res = REAL(gcvt)(number, ndigit, buf);
353 SIZE_T n = REAL(strlen)(buf);
354 __msan_unpoison(buf, n + 1);
357 #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt)
359 #define MSAN_MAYBE_INTERCEPT_GCVT
362 INTERCEPTOR(char *, strcat, char *dest, const char *src) {
363 ENSURE_MSAN_INITED();
364 GET_STORE_STACK_TRACE;
365 SIZE_T src_size = REAL(strlen)(src);
366 SIZE_T dest_size = REAL(strlen)(dest);
367 CHECK_UNPOISONED_STRING(src + src_size, 0);
368 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
369 char *res = REAL(strcat)(dest, src);
370 CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack);
374 INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) {
375 ENSURE_MSAN_INITED();
376 GET_STORE_STACK_TRACE;
377 SIZE_T dest_size = REAL(strlen)(dest);
378 SIZE_T copy_size = REAL(strnlen)(src, n);
379 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
380 char *res = REAL(strncat)(dest, src, n);
381 CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack);
382 __msan_unpoison(dest + dest_size + copy_size, 1); // \0
386 // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to
387 // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO.
388 #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \
389 ENSURE_MSAN_INITED(); \
390 ret_type res = REAL(func)(__VA_ARGS__); \
391 __msan_unpoison(endptr, sizeof(*endptr)); \
394 #define INTERCEPTOR_STRTO(ret_type, func, char_type) \
395 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \
396 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \
399 #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
400 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
402 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \
405 #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \
406 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
408 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \
411 #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \
412 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
413 int base, void *loc) { \
414 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \
418 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
419 INTERCEPTOR_STRTO(ret_type, func, char_type) \
420 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type)
422 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
423 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
424 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type)
427 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
428 INTERCEPTOR_STRTO(ret_type, func, char_type) \
429 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \
430 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \
431 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type)
433 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
434 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
435 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \
436 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \
437 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type)
440 INTERCEPTORS_STRTO(double, strtod, char)
441 INTERCEPTORS_STRTO(float, strtof, char)
442 INTERCEPTORS_STRTO(long double, strtold, char)
443 INTERCEPTORS_STRTO_BASE(long, strtol, char)
444 INTERCEPTORS_STRTO_BASE(long long, strtoll, char)
445 INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char)
446 INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char)
447 INTERCEPTORS_STRTO_BASE(u64, strtouq, char)
449 INTERCEPTORS_STRTO(double, wcstod, wchar_t)
450 INTERCEPTORS_STRTO(float, wcstof, wchar_t)
451 INTERCEPTORS_STRTO(long double, wcstold, wchar_t)
452 INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t)
453 INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t)
454 INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t)
455 INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t)
458 #define INTERCEPT_STRTO(func) \
459 INTERCEPT_FUNCTION(func); \
460 INTERCEPT_FUNCTION(func##_l);
462 #define INTERCEPT_STRTO(func) \
463 INTERCEPT_FUNCTION(func); \
464 INTERCEPT_FUNCTION(func##_l); \
465 INTERCEPT_FUNCTION(__##func##_l); \
466 INTERCEPT_FUNCTION(__##func##_internal);
470 // FIXME: support *wprintf in common format interceptors.
471 INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
472 ENSURE_MSAN_INITED();
473 int res = REAL(vswprintf)(str, size, format, ap);
475 __msan_unpoison(str, 4 * (res + 1));
480 INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
481 ENSURE_MSAN_INITED();
483 va_start(ap, format);
484 int res = vswprintf(str, size, format, ap);
489 #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \
490 ENSURE_MSAN_INITED(); \
491 InterceptorScope interceptor_scope; \
492 ret_type res = REAL(func)(s, __VA_ARGS__); \
493 if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \
496 INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
497 __sanitizer_tm *tm) {
498 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm);
501 INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format,
502 __sanitizer_tm *tm, void *loc) {
503 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc);
506 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
507 INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format,
508 __sanitizer_tm *tm, void *loc) {
509 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm,
512 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l)
514 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L
517 INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format,
518 __sanitizer_tm *tm) {
519 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm);
522 INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
523 __sanitizer_tm *tm, void *loc) {
524 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm,
528 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
529 INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
530 __sanitizer_tm *tm, void *loc) {
531 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm,
534 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l)
536 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L
539 INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
540 ENSURE_MSAN_INITED();
541 int res = REAL(mbtowc)(dest, src, n);
542 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
546 INTERCEPTOR(SIZE_T, mbrtowc, wchar_t *dest, const char *src, SIZE_T n,
548 ENSURE_MSAN_INITED();
549 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
550 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
554 // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
555 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
556 ENSURE_MSAN_INITED();
557 GET_STORE_STACK_TRACE;
558 wchar_t *res = REAL(wmemcpy)(dest, src, n);
559 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
563 #if !SANITIZER_NETBSD
564 INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
565 ENSURE_MSAN_INITED();
566 GET_STORE_STACK_TRACE;
567 wchar_t *res = REAL(wmempcpy)(dest, src, n);
568 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
571 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY INTERCEPT_FUNCTION(wmempcpy)
573 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY
576 INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
577 CHECK(MEM_IS_APP(s));
578 ENSURE_MSAN_INITED();
579 wchar_t *res = REAL(wmemset)(s, c, n);
580 __msan_unpoison(s, n * sizeof(wchar_t));
584 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
585 ENSURE_MSAN_INITED();
586 GET_STORE_STACK_TRACE;
587 wchar_t *res = REAL(wmemmove)(dest, src, n);
588 MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
592 INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
593 ENSURE_MSAN_INITED();
594 int res = REAL(wcscmp)(s1, s2);
598 INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
599 ENSURE_MSAN_INITED();
600 int res = REAL(gettimeofday)(tv, tz);
602 __msan_unpoison(tv, 16);
604 __msan_unpoison(tz, 8);
608 #if !SANITIZER_NETBSD
609 INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
610 ENSURE_MSAN_INITED();
611 char *res = REAL(fcvt)(x, a, b, c);
612 __msan_unpoison(b, sizeof(*b));
613 __msan_unpoison(c, sizeof(*c));
614 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
617 #define MSAN_MAYBE_INTERCEPT_FCVT INTERCEPT_FUNCTION(fcvt)
619 #define MSAN_MAYBE_INTERCEPT_FCVT
622 INTERCEPTOR(char *, getenv, char *name) {
623 if (msan_init_is_running)
624 return REAL(getenv)(name);
625 ENSURE_MSAN_INITED();
626 char *res = REAL(getenv)(name);
627 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
631 extern char **environ;
633 static void UnpoisonEnviron() {
634 char **envp = environ;
635 for (; *envp; ++envp) {
636 __msan_unpoison(envp, sizeof(*envp));
637 __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
639 // Trailing NULL pointer.
640 __msan_unpoison(envp, sizeof(*envp));
643 INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
644 ENSURE_MSAN_INITED();
645 CHECK_UNPOISONED_STRING(name, 0);
646 int res = REAL(setenv)(name, value, overwrite);
647 if (!res) UnpoisonEnviron();
651 INTERCEPTOR(int, putenv, char *string) {
652 ENSURE_MSAN_INITED();
653 int res = REAL(putenv)(string);
654 if (!res) UnpoisonEnviron();
658 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
659 INTERCEPTOR(int, fstat, int fd, void *buf) {
660 ENSURE_MSAN_INITED();
661 int res = REAL(fstat)(fd, buf);
663 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
666 #define MSAN_MAYBE_INTERCEPT_FSTAT INTERCEPT_FUNCTION(fstat)
668 #define MSAN_MAYBE_INTERCEPT_FSTAT
671 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
672 INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
673 ENSURE_MSAN_INITED();
674 int res = REAL(__fxstat)(magic, fd, buf);
676 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
679 #define MSAN_MAYBE_INTERCEPT___FXSTAT INTERCEPT_FUNCTION(__fxstat)
681 #define MSAN_MAYBE_INTERCEPT___FXSTAT
684 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
685 INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
686 ENSURE_MSAN_INITED();
687 int res = REAL(__fxstat64)(magic, fd, buf);
689 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
692 #define MSAN_MAYBE_INTERCEPT___FXSTAT64 INTERCEPT_FUNCTION(__fxstat64)
694 #define MSAN_MAYBE_INTERCEPT___FXSTAT64
697 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
698 INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
699 ENSURE_MSAN_INITED();
700 int res = REAL(fstatat)(fd, pathname, buf, flags);
701 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
704 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(fstatat)
706 INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
708 ENSURE_MSAN_INITED();
709 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
710 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
713 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(__fxstatat)
716 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
717 INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
719 ENSURE_MSAN_INITED();
720 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
721 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
724 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64 INTERCEPT_FUNCTION(__fxstatat64)
726 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64
729 INTERCEPTOR(int, pipe, int pipefd[2]) {
730 if (msan_init_is_running)
731 return REAL(pipe)(pipefd);
732 ENSURE_MSAN_INITED();
733 int res = REAL(pipe)(pipefd);
735 __msan_unpoison(pipefd, sizeof(int[2]));
739 INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
740 ENSURE_MSAN_INITED();
741 int res = REAL(pipe2)(pipefd, flags);
743 __msan_unpoison(pipefd, sizeof(int[2]));
747 INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
748 ENSURE_MSAN_INITED();
749 int res = REAL(socketpair)(domain, type, protocol, sv);
751 __msan_unpoison(sv, sizeof(int[2]));
755 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
756 INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
757 ENSURE_MSAN_INITED();
758 char *res = REAL(fgets_unlocked)(s, size, stream);
760 __msan_unpoison(s, REAL(strlen)(s) + 1);
763 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked)
765 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED
768 #define INTERCEPTOR_GETRLIMIT_BODY(func, resource, rlim) \
769 if (msan_init_is_running) \
770 return REAL(getrlimit)(resource, rlim); \
771 ENSURE_MSAN_INITED(); \
772 int res = REAL(func)(resource, rlim); \
774 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz); \
777 INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
778 INTERCEPTOR_GETRLIMIT_BODY(getrlimit, resource, rlim);
781 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
782 INTERCEPTOR(int, __getrlimit, int resource, void *rlim) {
783 INTERCEPTOR_GETRLIMIT_BODY(__getrlimit, resource, rlim);
786 INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
787 if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim);
788 ENSURE_MSAN_INITED();
789 int res = REAL(getrlimit64)(resource, rlim);
790 if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
794 INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit,
796 if (msan_init_is_running)
797 return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
798 ENSURE_MSAN_INITED();
799 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz);
800 int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
801 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz);
805 INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit,
807 if (msan_init_is_running)
808 return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
809 ENSURE_MSAN_INITED();
810 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz);
811 int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
812 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz);
816 #define MSAN_MAYBE_INTERCEPT___GETRLIMIT INTERCEPT_FUNCTION(__getrlimit)
817 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64)
818 #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit)
819 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64)
821 #define MSAN_MAYBE_INTERCEPT___GETRLIMIT
822 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64
823 #define MSAN_MAYBE_INTERCEPT_PRLIMIT
824 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64
827 INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
828 ENSURE_MSAN_INITED();
829 int res = REAL(gethostname)(name, len);
830 if (!res || (res == -1 && errno == errno_ENAMETOOLONG)) {
831 SIZE_T real_len = REAL(strnlen)(name, len);
834 __msan_unpoison(name, real_len);
839 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
840 INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
842 ENSURE_MSAN_INITED();
843 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
845 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
849 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
851 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT
854 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
855 INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
856 int timeout, void *sigmask) {
857 ENSURE_MSAN_INITED();
858 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
860 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
864 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
866 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
869 INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
870 GET_MALLOC_STACK_TRACE;
871 if (UNLIKELY(!msan_inited))
872 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
873 return AllocateFromLocalPool(nmemb * size);
874 return msan_calloc(nmemb, size, &stack);
877 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
878 GET_MALLOC_STACK_TRACE;
879 if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
880 uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
881 uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
883 if (UNLIKELY(!msan_inited)) {
884 new_ptr = AllocateFromLocalPool(copy_size);
887 new_ptr = msan_malloc(copy_size, &stack);
889 internal_memcpy(new_ptr, ptr, copy_size);
892 return msan_realloc(ptr, size, &stack);
895 INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) {
896 GET_MALLOC_STACK_TRACE;
897 return msan_reallocarray(ptr, nmemb, size, &stack);
900 INTERCEPTOR(void *, malloc, SIZE_T size) {
901 GET_MALLOC_STACK_TRACE;
902 if (UNLIKELY(!msan_inited))
903 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
904 return AllocateFromLocalPool(size);
905 return msan_malloc(size, &stack);
908 void __msan_allocated_memory(const void *data, uptr size) {
909 GET_MALLOC_STACK_TRACE;
910 if (flags()->poison_in_malloc) {
911 stack.tag = STACK_TRACE_TAG_POISON;
912 PoisonMemory(data, size, &stack);
916 void __msan_copy_shadow(void *dest, const void *src, uptr n) {
917 GET_STORE_STACK_TRACE;
918 MoveShadowAndOrigin(dest, src, n, &stack);
921 void __sanitizer_dtor_callback(const void *data, uptr size) {
922 GET_MALLOC_STACK_TRACE;
923 if (flags()->poison_in_dtor) {
924 stack.tag = STACK_TRACE_TAG_POISON;
925 PoisonMemory(data, size, &stack);
929 template <class Mmap>
930 static void *mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length,
931 int prot, int flags, int fd, OFF64_T offset) {
932 SIZE_T rounded_length = RoundUpTo(length, GetPageSize());
933 void *end_addr = (char *)addr + (rounded_length - 1);
934 if (addr && (!MEM_IS_APP(addr) || !MEM_IS_APP(end_addr))) {
935 if (flags & map_fixed) {
936 errno = errno_EINVAL;
942 void *res = real_mmap(addr, length, prot, flags, fd, offset);
943 if (res != (void *)-1) {
944 void *end_res = (char *)res + (rounded_length - 1);
945 if (MEM_IS_APP(res) && MEM_IS_APP(end_res)) {
946 __msan_unpoison(res, rounded_length);
948 // Application has attempted to map more memory than is supported by
949 // MSAN. Act as if we ran out of memory.
950 internal_munmap(res, length);
951 errno = errno_ENOMEM;
958 INTERCEPTOR(int, getrusage, int who, void *usage) {
959 ENSURE_MSAN_INITED();
960 int res = REAL(getrusage)(who, usage);
962 __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
967 class SignalHandlerScope {
969 SignalHandlerScope() {
970 if (MsanThread *t = GetCurrentThread())
971 t->EnterSignalHandler();
973 ~SignalHandlerScope() {
974 if (MsanThread *t = GetCurrentThread())
975 t->LeaveSignalHandler();
979 // sigactions_mu guarantees atomicity of sigaction() and signal() calls.
980 // Access to sigactions[] is gone with relaxed atomics to avoid data race with
981 // the signal handler.
982 const int kMaxSignals = 1024;
983 static atomic_uintptr_t sigactions[kMaxSignals];
984 static StaticSpinMutex sigactions_mu;
986 static void SignalHandler(int signo) {
987 SignalHandlerScope signal_handler_scope;
988 ScopedThreadLocalStateBackup stlsb;
991 typedef void (*signal_cb)(int x);
993 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
997 static void SignalAction(int signo, void *si, void *uc) {
998 SignalHandlerScope signal_handler_scope;
999 ScopedThreadLocalStateBackup stlsb;
1001 __msan_unpoison(si, sizeof(__sanitizer_sigaction));
1002 __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
1004 typedef void (*sigaction_cb)(int, void *, void *);
1006 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1010 static void read_sigaction(const __sanitizer_sigaction *act) {
1011 CHECK_UNPOISONED(&act->sa_flags, sizeof(act->sa_flags));
1012 if (act->sa_flags & __sanitizer::sa_siginfo)
1013 CHECK_UNPOISONED(&act->sigaction, sizeof(act->sigaction));
1015 CHECK_UNPOISONED(&act->handler, sizeof(act->handler));
1016 CHECK_UNPOISONED(&act->sa_mask, sizeof(act->sa_mask));
1019 extern "C" int pthread_attr_init(void *attr);
1020 extern "C" int pthread_attr_destroy(void *attr);
1022 static void *MsanThreadStartFunc(void *arg) {
1023 MsanThread *t = (MsanThread *)arg;
1024 SetCurrentThread(t);
1025 return t->ThreadStart();
1028 INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1030 ENSURE_MSAN_INITED(); // for GetTlsSize()
1031 __sanitizer_pthread_attr_t myattr;
1033 pthread_attr_init(&myattr);
1037 AdjustStackSize(attr);
1039 MsanThread *t = MsanThread::Create(callback, param);
1041 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t);
1043 if (attr == &myattr)
1044 pthread_attr_destroy(&myattr);
1046 __msan_unpoison(th, __sanitizer::pthread_t_sz);
1051 INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1052 void (*dtor)(void *value)) {
1053 if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor);
1054 ENSURE_MSAN_INITED();
1055 int res = REAL(pthread_key_create)(key, dtor);
1057 __msan_unpoison(key, sizeof(*key));
1061 #if SANITIZER_NETBSD
1062 INTERCEPTOR(int, __libc_thr_keycreate, __sanitizer_pthread_key_t *m,
1063 void (*dtor)(void *value))
1064 ALIAS(WRAPPER_NAME(pthread_key_create));
1067 INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1068 ENSURE_MSAN_INITED();
1069 int res = REAL(pthread_join)(th, retval);
1071 __msan_unpoison(retval, sizeof(*retval));
1075 extern char *tzname[2];
1077 INTERCEPTOR(void, tzset, int fake) {
1078 ENSURE_MSAN_INITED();
1079 InterceptorScope interceptor_scope;
1082 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1);
1084 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1);
1088 struct MSanAtExitRecord {
1089 void (*func)(void *arg);
1093 struct InterceptorContext {
1094 BlockingMutex atexit_mu;
1095 Vector<struct MSanAtExitRecord *> AtExitStack;
1097 InterceptorContext()
1102 static ALIGNED(64) char interceptor_placeholder[sizeof(InterceptorContext)];
1103 InterceptorContext *interceptor_ctx() {
1104 return reinterpret_cast<InterceptorContext*>(&interceptor_placeholder[0]);
1107 void MSanAtExitWrapper() {
1108 MSanAtExitRecord *r;
1110 BlockingMutexLock l(&interceptor_ctx()->atexit_mu);
1112 uptr element = interceptor_ctx()->AtExitStack.Size() - 1;
1113 r = interceptor_ctx()->AtExitStack[element];
1114 interceptor_ctx()->AtExitStack.PopBack();
1118 ((void(*)())r->func)();
1122 void MSanCxaAtExitWrapper(void *arg) {
1124 MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
1125 // libc before 2.27 had race which caused occasional double handler execution
1126 // https://sourceware.org/ml/libc-alpha/2017-08/msg01204.html
1133 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso);
1135 // Unpoison argument shadow for C++ module destructors.
1136 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
1138 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
1139 return setup_at_exit_wrapper((void(*)())func, arg, dso_handle);
1142 // Unpoison argument shadow for C++ module destructors.
1143 INTERCEPTOR(int, atexit, void (*func)()) {
1144 // Avoid calling real atexit as it is unrechable on at least on Linux.
1145 if (msan_init_is_running)
1146 return REAL(__cxa_atexit)((void (*)(void *a))func, 0, 0);
1147 return setup_at_exit_wrapper((void(*)())func, 0, 0);
1150 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso) {
1151 ENSURE_MSAN_INITED();
1152 MSanAtExitRecord *r =
1153 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
1154 r->func = (void(*)(void *a))f;
1158 // NetBSD does not preserve the 2nd argument if dso is equal to 0
1159 // Store ctx in a local stack-like structure
1161 BlockingMutexLock l(&interceptor_ctx()->atexit_mu);
1163 res = REAL(__cxa_atexit)((void (*)(void *a))MSanAtExitWrapper, 0, 0);
1165 interceptor_ctx()->AtExitStack.PushBack(r);
1168 res = REAL(__cxa_atexit)(MSanCxaAtExitWrapper, r, dso);
1173 static void BeforeFork() {
1174 StackDepotLockAll();
1175 ChainedOriginDepotLockAll();
1178 static void AfterFork() {
1179 ChainedOriginDepotUnlockAll();
1180 StackDepotUnlockAll();
1183 INTERCEPTOR(int, fork, void) {
1184 ENSURE_MSAN_INITED();
1186 int pid = REAL(fork)();
1191 // NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly
1194 INTERCEPTOR(int, openpty, int *aparent, int *aworker, char *name,
1195 const void *termp, const void *winp) {
1196 ENSURE_MSAN_INITED();
1197 InterceptorScope interceptor_scope;
1198 int res = REAL(openpty)(aparent, aworker, name, termp, winp);
1200 __msan_unpoison(aparent, sizeof(*aparent));
1201 __msan_unpoison(aworker, sizeof(*aworker));
1205 #define MSAN_MAYBE_INTERCEPT_OPENPTY INTERCEPT_FUNCTION(openpty)
1207 #define MSAN_MAYBE_INTERCEPT_OPENPTY
1210 // NetBSD ships with forkpty(3) in -lutil, that needs to be prebuilt explicitly
1213 INTERCEPTOR(int, forkpty, int *aparent, char *name, const void *termp,
1215 ENSURE_MSAN_INITED();
1216 InterceptorScope interceptor_scope;
1217 int res = REAL(forkpty)(aparent, name, termp, winp);
1219 __msan_unpoison(aparent, sizeof(*aparent));
1222 #define MSAN_MAYBE_INTERCEPT_FORKPTY INTERCEPT_FUNCTION(forkpty)
1224 #define MSAN_MAYBE_INTERCEPT_FORKPTY
1227 struct MSanInterceptorContext {
1228 bool in_interceptor_scope;
1234 // FIXME: ask frontend whether we need to return failure.
1238 } // namespace __msan
1240 // A version of CHECK_UNPOISONED using a saved scope value. Used in common
1242 #define CHECK_UNPOISONED_CTX(ctx, x, n) \
1244 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1245 CHECK_UNPOISONED_0(x, n); \
1248 #define MSAN_INTERCEPT_FUNC(name) \
1250 if (!INTERCEPT_FUNCTION(name)) \
1251 VReport(1, "MemorySanitizer: failed to intercept '%s'\n", #name); \
1254 #define MSAN_INTERCEPT_FUNC_VER(name, ver) \
1256 if (!INTERCEPT_FUNCTION_VER(name, ver)) \
1257 VReport(1, "MemorySanitizer: failed to intercept '%s@@%s'\n", #name, \
1261 #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name)
1262 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
1263 MSAN_INTERCEPT_FUNC_VER(name, ver)
1264 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \
1265 UnpoisonParam(count)
1266 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
1267 __msan_unpoison(ptr, size)
1268 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
1269 CHECK_UNPOISONED_CTX(ctx, ptr, size)
1270 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
1271 __msan_unpoison(ptr, size)
1272 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
1273 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \
1274 ENSURE_MSAN_INITED(); \
1275 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1276 ctx = (void *)&msan_ctx; \
1278 InterceptorScope interceptor_scope; \
1279 __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */
1280 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
1283 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1286 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1289 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1292 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
1294 } while (false) // FIXME
1295 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
1297 } while (false) // FIXME
1298 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
1299 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
1300 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
1302 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \
1303 if (filename && map) \
1304 ForEachMappedRegion(map, __msan_unpoison); \
1307 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!msan_inited)
1309 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
1310 if (MsanThread *t = GetCurrentThread()) { \
1311 *begin = t->tls_begin(); \
1312 *end = t->tls_end(); \
1314 *begin = *end = 0; \
1317 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
1320 return __msan_memset(block, c, size); \
1322 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
1325 return __msan_memmove(to, from, size); \
1327 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
1330 return __msan_memcpy(to, from, size); \
1333 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
1335 GET_STORE_STACK_TRACE; \
1336 CopyShadowAndOrigin(to, from, size, &stack); \
1337 __msan_unpoison(to + size, 1); \
1340 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, fd, \
1343 return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \
1346 #include "sanitizer_common/sanitizer_platform_interceptors.h"
1347 #include "sanitizer_common/sanitizer_common_interceptors.inc"
1349 static uptr signal_impl(int signo, uptr cb);
1350 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1351 __sanitizer_sigaction *oldact);
1353 #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \
1354 { return sigaction_impl(signo, act, oldact); }
1356 #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \
1358 handler = signal_impl(signo, handler); \
1359 InterceptorScope interceptor_scope; \
1360 return REAL(func)(signo, handler); \
1363 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
1365 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1366 __sanitizer_sigaction *oldact) {
1367 ENSURE_MSAN_INITED();
1368 if (act) read_sigaction(act);
1370 if (flags()->wrap_signals) {
1371 SpinMutexLock lock(&sigactions_mu);
1372 CHECK_LT(signo, kMaxSignals);
1373 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
1374 __sanitizer_sigaction new_act;
1375 __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr;
1377 REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction));
1378 uptr cb = (uptr)pnew_act->sigaction;
1379 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
1380 ? (uptr)SignalAction
1381 : (uptr)SignalHandler;
1382 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1383 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1384 pnew_act->sigaction = (decltype(pnew_act->sigaction))new_cb;
1387 res = REAL(SIGACTION_SYMNAME)(signo, pnew_act, oldact);
1388 if (res == 0 && oldact) {
1389 uptr cb = (uptr)oldact->sigaction;
1390 if (cb == (uptr)SignalAction || cb == (uptr)SignalHandler) {
1391 oldact->sigaction = (decltype(oldact->sigaction))old_cb;
1395 res = REAL(SIGACTION_SYMNAME)(signo, act, oldact);
1398 if (res == 0 && oldact) {
1399 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
1404 static uptr signal_impl(int signo, uptr cb) {
1405 ENSURE_MSAN_INITED();
1406 if (flags()->wrap_signals) {
1407 CHECK_LT(signo, kMaxSignals);
1408 SpinMutexLock lock(&sigactions_mu);
1409 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1410 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1411 cb = (uptr)&SignalHandler;
1417 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
1418 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
1421 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
1424 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1425 #include "sanitizer_common/sanitizer_common_syscalls.inc"
1426 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
1435 INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
1437 COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info);
1438 int res = REAL(dladdr)(addr, info);
1440 __msan_unpoison(info, sizeof(*info));
1441 if (info->dli_fname)
1442 __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
1443 if (info->dli_sname)
1444 __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
1449 INTERCEPTOR(char *, dlerror, int fake) {
1451 COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake);
1452 char *res = REAL(dlerror)(fake);
1453 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
1457 typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
1459 struct dl_iterate_phdr_data {
1460 dl_iterate_phdr_cb callback;
1464 static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
1467 __msan_unpoison(info, size);
1468 if (info->dlpi_phdr && info->dlpi_phnum)
1469 __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum);
1470 if (info->dlpi_name)
1471 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
1473 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
1475 return cbdata->callback(info, size, cbdata->data);
1478 INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) {
1479 ENSURE_MSAN_INITED();
1480 void *p = REAL(shmat)(shmid, shmaddr, shmflg);
1481 if (p != (void *)-1) {
1482 __sanitizer_shmid_ds ds;
1483 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds);
1485 __msan_unpoison(p, ds.shm_segsz);
1491 INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
1493 COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data);
1494 dl_iterate_phdr_data cbdata;
1495 cbdata.callback = callback;
1497 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
1501 // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
1502 INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
1503 ENSURE_MSAN_INITED();
1504 wchar_t *res = REAL(wcschr)(s, wc, ps);
1508 // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
1509 INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
1510 ENSURE_MSAN_INITED();
1511 GET_STORE_STACK_TRACE;
1512 wchar_t *res = REAL(wcscpy)(dest, src);
1513 CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1),
1518 INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
1519 ENSURE_MSAN_INITED();
1520 GET_STORE_STACK_TRACE;
1521 SIZE_T copy_size = REAL(wcsnlen)(src, n);
1522 if (copy_size < n) copy_size++; // trailing \0
1523 wchar_t *res = REAL(wcsncpy)(dest, src, n);
1524 CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack);
1525 __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t));
1529 // These interface functions reside here so that they can use
1530 // REAL(memset), etc.
1531 void __msan_unpoison(const void *a, uptr size) {
1532 if (!MEM_IS_APP(a)) return;
1533 SetShadow(a, size, 0);
1536 void __msan_poison(const void *a, uptr size) {
1537 if (!MEM_IS_APP(a)) return;
1538 SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1);
1541 void __msan_poison_stack(void *a, uptr size) {
1542 if (!MEM_IS_APP(a)) return;
1543 SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1);
1546 void __msan_unpoison_param(uptr n) { UnpoisonParam(n); }
1548 void __msan_clear_and_unpoison(void *a, uptr size) {
1549 REAL(memset)(a, 0, size);
1550 SetShadow(a, size, 0);
1553 void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
1554 if (!msan_inited) return internal_memcpy(dest, src, n);
1555 if (msan_init_is_running || __msan::IsInSymbolizer())
1556 return REAL(memcpy)(dest, src, n);
1557 ENSURE_MSAN_INITED();
1558 GET_STORE_STACK_TRACE;
1559 void *res = REAL(memcpy)(dest, src, n);
1560 CopyShadowAndOrigin(dest, src, n, &stack);
1564 void *__msan_memset(void *s, int c, SIZE_T n) {
1565 if (!msan_inited) return internal_memset(s, c, n);
1566 if (msan_init_is_running) return REAL(memset)(s, c, n);
1567 ENSURE_MSAN_INITED();
1568 void *res = REAL(memset)(s, c, n);
1569 __msan_unpoison(s, n);
1573 void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
1574 if (!msan_inited) return internal_memmove(dest, src, n);
1575 if (msan_init_is_running) return REAL(memmove)(dest, src, n);
1576 ENSURE_MSAN_INITED();
1577 GET_STORE_STACK_TRACE;
1578 void *res = REAL(memmove)(dest, src, n);
1579 MoveShadowAndOrigin(dest, src, n, &stack);
1583 void __msan_unpoison_string(const char* s) {
1584 if (!MEM_IS_APP(s)) return;
1585 __msan_unpoison(s, REAL(strlen)(s) + 1);
1590 void InitializeInterceptors() {
1591 static int inited = 0;
1592 CHECK_EQ(inited, 0);
1594 new(interceptor_ctx()) InterceptorContext();
1596 InitializeCommonInterceptors();
1597 InitializeSignalInterceptors();
1599 INTERCEPT_FUNCTION(posix_memalign);
1600 MSAN_MAYBE_INTERCEPT_MEMALIGN;
1601 MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
1602 INTERCEPT_FUNCTION(valloc);
1603 MSAN_MAYBE_INTERCEPT_PVALLOC;
1604 INTERCEPT_FUNCTION(malloc);
1605 INTERCEPT_FUNCTION(calloc);
1606 INTERCEPT_FUNCTION(realloc);
1607 INTERCEPT_FUNCTION(reallocarray);
1608 INTERCEPT_FUNCTION(free);
1609 MSAN_MAYBE_INTERCEPT_CFREE;
1610 MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
1611 MSAN_MAYBE_INTERCEPT_MALLINFO;
1612 MSAN_MAYBE_INTERCEPT_MALLOPT;
1613 MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
1614 INTERCEPT_FUNCTION(fread);
1615 MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED;
1616 INTERCEPT_FUNCTION(memccpy);
1617 MSAN_MAYBE_INTERCEPT_MEMPCPY;
1618 INTERCEPT_FUNCTION(bcopy);
1619 INTERCEPT_FUNCTION(wmemset);
1620 INTERCEPT_FUNCTION(wmemcpy);
1621 MSAN_MAYBE_INTERCEPT_WMEMPCPY;
1622 INTERCEPT_FUNCTION(wmemmove);
1623 INTERCEPT_FUNCTION(strcpy);
1624 MSAN_MAYBE_INTERCEPT_STPCPY;
1625 INTERCEPT_FUNCTION(strdup);
1626 MSAN_MAYBE_INTERCEPT___STRDUP;
1627 INTERCEPT_FUNCTION(strncpy);
1628 MSAN_MAYBE_INTERCEPT_GCVT;
1629 INTERCEPT_FUNCTION(strcat);
1630 INTERCEPT_FUNCTION(strncat);
1631 INTERCEPT_STRTO(strtod);
1632 INTERCEPT_STRTO(strtof);
1633 INTERCEPT_STRTO(strtold);
1634 INTERCEPT_STRTO(strtol);
1635 INTERCEPT_STRTO(strtoul);
1636 INTERCEPT_STRTO(strtoll);
1637 INTERCEPT_STRTO(strtoull);
1638 INTERCEPT_STRTO(strtouq);
1639 INTERCEPT_STRTO(wcstod);
1640 INTERCEPT_STRTO(wcstof);
1641 INTERCEPT_STRTO(wcstold);
1642 INTERCEPT_STRTO(wcstol);
1643 INTERCEPT_STRTO(wcstoul);
1644 INTERCEPT_STRTO(wcstoll);
1645 INTERCEPT_STRTO(wcstoull);
1646 #ifdef SANITIZER_NLDBL_VERSION
1647 INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION);
1648 INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION);
1650 INTERCEPT_FUNCTION(vswprintf);
1651 INTERCEPT_FUNCTION(swprintf);
1653 INTERCEPT_FUNCTION(strftime);
1654 INTERCEPT_FUNCTION(strftime_l);
1655 MSAN_MAYBE_INTERCEPT___STRFTIME_L;
1656 INTERCEPT_FUNCTION(wcsftime);
1657 INTERCEPT_FUNCTION(wcsftime_l);
1658 MSAN_MAYBE_INTERCEPT___WCSFTIME_L;
1659 INTERCEPT_FUNCTION(mbtowc);
1660 INTERCEPT_FUNCTION(mbrtowc);
1661 INTERCEPT_FUNCTION(wcslen);
1662 INTERCEPT_FUNCTION(wcsnlen);
1663 INTERCEPT_FUNCTION(wcschr);
1664 INTERCEPT_FUNCTION(wcscpy);
1665 INTERCEPT_FUNCTION(wcsncpy);
1666 INTERCEPT_FUNCTION(wcscmp);
1667 INTERCEPT_FUNCTION(getenv);
1668 INTERCEPT_FUNCTION(setenv);
1669 INTERCEPT_FUNCTION(putenv);
1670 INTERCEPT_FUNCTION(gettimeofday);
1671 MSAN_MAYBE_INTERCEPT_FCVT;
1672 MSAN_MAYBE_INTERCEPT_FSTAT;
1673 MSAN_MAYBE_INTERCEPT___FXSTAT;
1674 MSAN_INTERCEPT_FSTATAT;
1675 MSAN_MAYBE_INTERCEPT___FXSTAT64;
1676 MSAN_MAYBE_INTERCEPT___FXSTATAT64;
1677 INTERCEPT_FUNCTION(pipe);
1678 INTERCEPT_FUNCTION(pipe2);
1679 INTERCEPT_FUNCTION(socketpair);
1680 MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED;
1681 INTERCEPT_FUNCTION(getrlimit);
1682 MSAN_MAYBE_INTERCEPT___GETRLIMIT;
1683 MSAN_MAYBE_INTERCEPT_GETRLIMIT64;
1684 MSAN_MAYBE_INTERCEPT_PRLIMIT;
1685 MSAN_MAYBE_INTERCEPT_PRLIMIT64;
1686 INTERCEPT_FUNCTION(gethostname);
1687 MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1688 MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1689 INTERCEPT_FUNCTION(dladdr);
1690 INTERCEPT_FUNCTION(dlerror);
1691 INTERCEPT_FUNCTION(dl_iterate_phdr);
1692 INTERCEPT_FUNCTION(getrusage);
1693 #if defined(__mips__)
1694 INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2");
1696 INTERCEPT_FUNCTION(pthread_create);
1698 INTERCEPT_FUNCTION(pthread_key_create);
1700 #if SANITIZER_NETBSD
1701 INTERCEPT_FUNCTION(__libc_thr_keycreate);
1704 INTERCEPT_FUNCTION(pthread_join);
1705 INTERCEPT_FUNCTION(tzset);
1706 INTERCEPT_FUNCTION(atexit);
1707 INTERCEPT_FUNCTION(__cxa_atexit);
1708 INTERCEPT_FUNCTION(shmat);
1709 INTERCEPT_FUNCTION(fork);
1710 MSAN_MAYBE_INTERCEPT_OPENPTY;
1711 MSAN_MAYBE_INTERCEPT_FORKPTY;
1715 } // namespace __msan