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"
39 #define fstat __fstat50
40 #define gettimeofday __gettimeofday50
41 #define getrusage __getrusage50
45 // ACHTUNG! No other system header includes in this file.
46 // Ideally, we should get rid of stdarg.h as well.
48 using namespace __msan;
50 using __sanitizer::memory_order;
51 using __sanitizer::atomic_load;
52 using __sanitizer::atomic_store;
53 using __sanitizer::atomic_uintptr_t;
55 DECLARE_REAL(SIZE_T, strlen, const char *s)
56 DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
57 DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n)
58 DECLARE_REAL(void *, memset, void *dest, int c, uptr n)
60 // True if this is a nested interceptor.
61 static THREADLOCAL int in_interceptor_scope;
63 void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; }
64 void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; }
66 struct InterceptorScope {
67 InterceptorScope() { ++in_interceptor_scope; }
68 ~InterceptorScope() { --in_interceptor_scope; }
71 bool IsInInterceptorScope() {
72 return in_interceptor_scope;
75 static uptr allocated_for_dlsym;
76 static const uptr kDlsymAllocPoolSize = 1024;
77 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
79 static bool IsInDlsymAllocPool(const void *ptr) {
80 uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
81 return off < sizeof(alloc_memory_for_dlsym);
84 static void *AllocateFromLocalPool(uptr size_in_bytes) {
85 uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
86 void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
87 allocated_for_dlsym += size_in_words;
88 CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
92 #define ENSURE_MSAN_INITED() do { \
93 CHECK(!msan_init_is_running); \
99 // Check that [x, x+n) range is unpoisoned.
100 #define CHECK_UNPOISONED_0(x, n) \
102 sptr __offset = __msan_test_shadow(x, n); \
103 if (__msan::IsInSymbolizer()) break; \
104 if (__offset >= 0 && __msan::flags()->report_umrs) { \
105 GET_CALLER_PC_BP_SP; \
107 ReportUMRInsideAddressRange(__func__, x, n, __offset); \
108 __msan::PrintWarningWithOrigin( \
109 pc, bp, __msan_get_origin((const char *)x + __offset)); \
110 if (__msan::flags()->halt_on_error) { \
111 Printf("Exiting\n"); \
117 // Check that [x, x+n) range is unpoisoned unless we are in a nested
119 #define CHECK_UNPOISONED(x, n) \
121 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
124 #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \
125 CHECK_UNPOISONED((x), \
126 common_flags()->strict_string_checks ? (len) + 1 : (n) )
128 #define CHECK_UNPOISONED_STRING(x, n) \
129 CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n))
131 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
132 INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
134 ENSURE_MSAN_INITED();
135 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
137 __msan_unpoison(ptr, res *size);
140 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked)
142 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED
145 #if !SANITIZER_NETBSD
146 INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
147 return (char *)__msan_memcpy(dest, src, n) + n;
149 #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy)
151 #define MSAN_MAYBE_INTERCEPT_MEMPCPY
154 INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) {
155 ENSURE_MSAN_INITED();
156 void *res = REAL(memccpy)(dest, src, c, n);
157 CHECK(!res || (res >= dest && res <= (char *)dest + n));
158 SIZE_T sz = res ? (char *)res - (char *)dest : n;
159 CHECK_UNPOISONED(src, sz);
160 __msan_unpoison(dest, sz);
164 INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
165 return __msan_memmove(dest, src, n);
168 INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
169 GET_MALLOC_STACK_TRACE;
171 int res = msan_posix_memalign(memptr, alignment, size, &stack);
173 __msan_unpoison(memptr, sizeof(*memptr));
177 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
178 INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
179 GET_MALLOC_STACK_TRACE;
180 return msan_memalign(alignment, size, &stack);
182 #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
184 #define MSAN_MAYBE_INTERCEPT_MEMALIGN
187 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
188 GET_MALLOC_STACK_TRACE;
189 return msan_aligned_alloc(alignment, size, &stack);
192 #if !SANITIZER_NETBSD
193 INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
194 GET_MALLOC_STACK_TRACE;
195 void *ptr = msan_memalign(alignment, size, &stack);
197 DTLS_on_libc_memalign(ptr, size);
200 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
202 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
205 INTERCEPTOR(void *, valloc, SIZE_T size) {
206 GET_MALLOC_STACK_TRACE;
207 return msan_valloc(size, &stack);
210 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
211 INTERCEPTOR(void *, pvalloc, SIZE_T size) {
212 GET_MALLOC_STACK_TRACE;
213 return msan_pvalloc(size, &stack);
215 #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
217 #define MSAN_MAYBE_INTERCEPT_PVALLOC
220 INTERCEPTOR(void, free, void *ptr) {
221 GET_MALLOC_STACK_TRACE;
222 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
223 MsanDeallocate(&stack, ptr);
226 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
227 INTERCEPTOR(void, cfree, void *ptr) {
228 GET_MALLOC_STACK_TRACE;
229 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
230 MsanDeallocate(&stack, ptr);
232 #define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
234 #define MSAN_MAYBE_INTERCEPT_CFREE
237 #if !SANITIZER_NETBSD
238 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
239 return __sanitizer_get_allocated_size(ptr);
241 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
242 INTERCEPT_FUNCTION(malloc_usable_size)
244 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
247 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
248 // This function actually returns a struct by value, but we can't unpoison a
249 // temporary! The following is equivalent on all supported platforms but
250 // aarch64 (which uses a different register for sret value). We have a test
252 INTERCEPTOR(void, mallinfo, __sanitizer_mallinfo *sret) {
255 asm volatile("mov %0,x8" : "=r" (r8));
256 sret = reinterpret_cast<__sanitizer_mallinfo*>(r8);
258 REAL(memset)(sret, 0, sizeof(*sret));
259 __msan_unpoison(sret, sizeof(*sret));
261 #define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
263 #define MSAN_MAYBE_INTERCEPT_MALLINFO
266 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
267 INTERCEPTOR(int, mallopt, int cmd, int value) {
270 #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
272 #define MSAN_MAYBE_INTERCEPT_MALLOPT
275 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
276 INTERCEPTOR(void, malloc_stats, void) {
277 // FIXME: implement, but don't call REAL(malloc_stats)!
279 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats)
281 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS
284 INTERCEPTOR(char *, strcpy, char *dest, const char *src) { // NOLINT
285 ENSURE_MSAN_INITED();
286 GET_STORE_STACK_TRACE;
287 SIZE_T n = REAL(strlen)(src);
288 CHECK_UNPOISONED_STRING(src + n, 0);
289 char *res = REAL(strcpy)(dest, src); // NOLINT
290 CopyShadowAndOrigin(dest, src, n + 1, &stack);
294 INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { // NOLINT
295 ENSURE_MSAN_INITED();
296 GET_STORE_STACK_TRACE;
297 SIZE_T copy_size = REAL(strnlen)(src, n);
299 copy_size++; // trailing \0
300 char *res = REAL(strncpy)(dest, src, n); // NOLINT
301 CopyShadowAndOrigin(dest, src, copy_size, &stack);
302 __msan_unpoison(dest + copy_size, n - copy_size);
306 #if !SANITIZER_NETBSD
307 INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { // NOLINT
308 ENSURE_MSAN_INITED();
309 GET_STORE_STACK_TRACE;
310 SIZE_T n = REAL(strlen)(src);
311 CHECK_UNPOISONED_STRING(src + n, 0);
312 char *res = REAL(stpcpy)(dest, src); // NOLINT
313 CopyShadowAndOrigin(dest, src, n + 1, &stack);
316 #define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy)
318 #define MSAN_MAYBE_INTERCEPT_STPCPY
321 INTERCEPTOR(char *, strdup, char *src) {
322 ENSURE_MSAN_INITED();
323 GET_STORE_STACK_TRACE;
324 // On FreeBSD strdup() leverages strlen().
325 InterceptorScope interceptor_scope;
326 SIZE_T n = REAL(strlen)(src);
327 CHECK_UNPOISONED_STRING(src + n, 0);
328 char *res = REAL(strdup)(src);
329 CopyShadowAndOrigin(res, src, n + 1, &stack);
333 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
334 INTERCEPTOR(char *, __strdup, char *src) {
335 ENSURE_MSAN_INITED();
336 GET_STORE_STACK_TRACE;
337 SIZE_T n = REAL(strlen)(src);
338 CHECK_UNPOISONED_STRING(src + n, 0);
339 char *res = REAL(__strdup)(src);
340 CopyShadowAndOrigin(res, src, n + 1, &stack);
343 #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup)
345 #define MSAN_MAYBE_INTERCEPT___STRDUP
348 #if !SANITIZER_NETBSD
349 INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
350 ENSURE_MSAN_INITED();
351 char *res = REAL(gcvt)(number, ndigit, buf);
352 SIZE_T n = REAL(strlen)(buf);
353 __msan_unpoison(buf, n + 1);
356 #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt)
358 #define MSAN_MAYBE_INTERCEPT_GCVT
361 INTERCEPTOR(char *, strcat, char *dest, const char *src) { // NOLINT
362 ENSURE_MSAN_INITED();
363 GET_STORE_STACK_TRACE;
364 SIZE_T src_size = REAL(strlen)(src);
365 SIZE_T dest_size = REAL(strlen)(dest);
366 CHECK_UNPOISONED_STRING(src + src_size, 0);
367 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
368 char *res = REAL(strcat)(dest, src); // NOLINT
369 CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack);
373 INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { // NOLINT
374 ENSURE_MSAN_INITED();
375 GET_STORE_STACK_TRACE;
376 SIZE_T dest_size = REAL(strlen)(dest);
377 SIZE_T copy_size = REAL(strnlen)(src, n);
378 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
379 char *res = REAL(strncat)(dest, src, n); // NOLINT
380 CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack);
381 __msan_unpoison(dest + dest_size + copy_size, 1); // \0
385 // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to
386 // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO.
387 #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \
388 ENSURE_MSAN_INITED(); \
389 ret_type res = REAL(func)(__VA_ARGS__); \
390 __msan_unpoison(endptr, sizeof(*endptr)); \
393 #define INTERCEPTOR_STRTO(ret_type, func, char_type) \
394 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \
395 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \
398 #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
399 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
401 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \
404 #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \
405 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
407 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \
410 #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \
411 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
412 int base, void *loc) { \
413 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \
417 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
418 INTERCEPTOR_STRTO(ret_type, func, char_type) \
419 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type)
421 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
422 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
423 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type)
426 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
427 INTERCEPTOR_STRTO(ret_type, func, char_type) \
428 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \
429 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \
430 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type)
432 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
433 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
434 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \
435 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \
436 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type)
439 INTERCEPTORS_STRTO(double, strtod, char) // NOLINT
440 INTERCEPTORS_STRTO(float, strtof, char) // NOLINT
441 INTERCEPTORS_STRTO(long double, strtold, char) // NOLINT
442 INTERCEPTORS_STRTO_BASE(long, strtol, char) // NOLINT
443 INTERCEPTORS_STRTO_BASE(long long, strtoll, char) // NOLINT
444 INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char) // NOLINT
445 INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char) // NOLINT
446 INTERCEPTORS_STRTO_BASE(u64, strtouq, char) // NOLINT
448 INTERCEPTORS_STRTO(double, wcstod, wchar_t) // NOLINT
449 INTERCEPTORS_STRTO(float, wcstof, wchar_t) // NOLINT
450 INTERCEPTORS_STRTO(long double, wcstold, wchar_t) // NOLINT
451 INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t) // NOLINT
452 INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t) // NOLINT
453 INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t) // NOLINT
454 INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t) // NOLINT
457 #define INTERCEPT_STRTO(func) \
458 INTERCEPT_FUNCTION(func); \
459 INTERCEPT_FUNCTION(func##_l);
461 #define INTERCEPT_STRTO(func) \
462 INTERCEPT_FUNCTION(func); \
463 INTERCEPT_FUNCTION(func##_l); \
464 INTERCEPT_FUNCTION(__##func##_l); \
465 INTERCEPT_FUNCTION(__##func##_internal);
469 // FIXME: support *wprintf in common format interceptors.
470 INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
471 ENSURE_MSAN_INITED();
472 int res = REAL(vswprintf)(str, size, format, ap);
474 __msan_unpoison(str, 4 * (res + 1));
479 INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
480 ENSURE_MSAN_INITED();
482 va_start(ap, format);
483 int res = vswprintf(str, size, format, ap);
488 #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \
489 ENSURE_MSAN_INITED(); \
490 InterceptorScope interceptor_scope; \
491 ret_type res = REAL(func)(s, __VA_ARGS__); \
492 if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \
495 INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
496 __sanitizer_tm *tm) {
497 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm);
500 INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format,
501 __sanitizer_tm *tm, void *loc) {
502 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc);
505 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
506 INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format,
507 __sanitizer_tm *tm, void *loc) {
508 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm,
511 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l)
513 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L
516 INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format,
517 __sanitizer_tm *tm) {
518 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm);
521 INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
522 __sanitizer_tm *tm, void *loc) {
523 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm,
527 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
528 INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
529 __sanitizer_tm *tm, void *loc) {
530 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm,
533 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l)
535 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L
538 INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
539 ENSURE_MSAN_INITED();
540 int res = REAL(mbtowc)(dest, src, n);
541 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
545 INTERCEPTOR(SIZE_T, mbrtowc, wchar_t *dest, const char *src, SIZE_T n,
547 ENSURE_MSAN_INITED();
548 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
549 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
553 // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
554 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
555 ENSURE_MSAN_INITED();
556 GET_STORE_STACK_TRACE;
557 wchar_t *res = REAL(wmemcpy)(dest, src, n);
558 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
562 #if !SANITIZER_NETBSD
563 INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
564 ENSURE_MSAN_INITED();
565 GET_STORE_STACK_TRACE;
566 wchar_t *res = REAL(wmempcpy)(dest, src, n);
567 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
570 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY INTERCEPT_FUNCTION(wmempcpy)
572 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY
575 INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
576 CHECK(MEM_IS_APP(s));
577 ENSURE_MSAN_INITED();
578 wchar_t *res = REAL(wmemset)(s, c, n);
579 __msan_unpoison(s, n * sizeof(wchar_t));
583 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
584 ENSURE_MSAN_INITED();
585 GET_STORE_STACK_TRACE;
586 wchar_t *res = REAL(wmemmove)(dest, src, n);
587 MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
591 INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
592 ENSURE_MSAN_INITED();
593 int res = REAL(wcscmp)(s1, s2);
597 INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
598 ENSURE_MSAN_INITED();
599 int res = REAL(gettimeofday)(tv, tz);
601 __msan_unpoison(tv, 16);
603 __msan_unpoison(tz, 8);
607 #if !SANITIZER_NETBSD
608 INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
609 ENSURE_MSAN_INITED();
610 char *res = REAL(fcvt)(x, a, b, c);
611 __msan_unpoison(b, sizeof(*b));
612 __msan_unpoison(c, sizeof(*c));
613 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
616 #define MSAN_MAYBE_INTERCEPT_FCVT INTERCEPT_FUNCTION(fcvt)
618 #define MSAN_MAYBE_INTERCEPT_FCVT
621 INTERCEPTOR(char *, getenv, char *name) {
622 if (msan_init_is_running)
623 return REAL(getenv)(name);
624 ENSURE_MSAN_INITED();
625 char *res = REAL(getenv)(name);
626 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
630 extern char **environ;
632 static void UnpoisonEnviron() {
633 char **envp = environ;
634 for (; *envp; ++envp) {
635 __msan_unpoison(envp, sizeof(*envp));
636 __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
638 // Trailing NULL pointer.
639 __msan_unpoison(envp, sizeof(*envp));
642 INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
643 ENSURE_MSAN_INITED();
644 CHECK_UNPOISONED_STRING(name, 0);
645 int res = REAL(setenv)(name, value, overwrite);
646 if (!res) UnpoisonEnviron();
650 INTERCEPTOR(int, putenv, char *string) {
651 ENSURE_MSAN_INITED();
652 int res = REAL(putenv)(string);
653 if (!res) UnpoisonEnviron();
657 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
658 INTERCEPTOR(int, fstat, int fd, void *buf) {
659 ENSURE_MSAN_INITED();
660 int res = REAL(fstat)(fd, buf);
662 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
665 #define MSAN_MAYBE_INTERCEPT_FSTAT INTERCEPT_FUNCTION(fstat)
667 #define MSAN_MAYBE_INTERCEPT_FSTAT
670 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
671 INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
672 ENSURE_MSAN_INITED();
673 int res = REAL(__fxstat)(magic, fd, buf);
675 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
678 #define MSAN_MAYBE_INTERCEPT___FXSTAT INTERCEPT_FUNCTION(__fxstat)
680 #define MSAN_MAYBE_INTERCEPT___FXSTAT
683 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
684 INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
685 ENSURE_MSAN_INITED();
686 int res = REAL(__fxstat64)(magic, fd, buf);
688 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
691 #define MSAN_MAYBE_INTERCEPT___FXSTAT64 INTERCEPT_FUNCTION(__fxstat64)
693 #define MSAN_MAYBE_INTERCEPT___FXSTAT64
696 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
697 INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
698 ENSURE_MSAN_INITED();
699 int res = REAL(fstatat)(fd, pathname, buf, flags);
700 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
703 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(fstatat)
705 INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
707 ENSURE_MSAN_INITED();
708 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
709 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
712 # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(__fxstatat)
715 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
716 INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
718 ENSURE_MSAN_INITED();
719 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
720 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
723 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64 INTERCEPT_FUNCTION(__fxstatat64)
725 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64
728 INTERCEPTOR(int, pipe, int pipefd[2]) {
729 if (msan_init_is_running)
730 return REAL(pipe)(pipefd);
731 ENSURE_MSAN_INITED();
732 int res = REAL(pipe)(pipefd);
734 __msan_unpoison(pipefd, sizeof(int[2]));
738 INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
739 ENSURE_MSAN_INITED();
740 int res = REAL(pipe2)(pipefd, flags);
742 __msan_unpoison(pipefd, sizeof(int[2]));
746 INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
747 ENSURE_MSAN_INITED();
748 int res = REAL(socketpair)(domain, type, protocol, sv);
750 __msan_unpoison(sv, sizeof(int[2]));
754 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
755 INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
756 ENSURE_MSAN_INITED();
757 char *res = REAL(fgets_unlocked)(s, size, stream);
759 __msan_unpoison(s, REAL(strlen)(s) + 1);
762 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked)
764 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED
767 INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
768 if (msan_init_is_running)
769 return REAL(getrlimit)(resource, rlim);
770 ENSURE_MSAN_INITED();
771 int res = REAL(getrlimit)(resource, rlim);
773 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz);
777 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
778 INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
779 if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim);
780 ENSURE_MSAN_INITED();
781 int res = REAL(getrlimit64)(resource, rlim);
782 if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
786 INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit,
788 if (msan_init_is_running)
789 return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
790 ENSURE_MSAN_INITED();
791 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz);
792 int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
793 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz);
797 INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit,
799 if (msan_init_is_running)
800 return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
801 ENSURE_MSAN_INITED();
802 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz);
803 int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
804 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz);
808 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64)
809 #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit)
810 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64)
812 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64
813 #define MSAN_MAYBE_INTERCEPT_PRLIMIT
814 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64
817 #if SANITIZER_FREEBSD
818 // FreeBSD's <sys/utsname.h> define uname() as
819 // static __inline int uname(struct utsname *name) {
820 // return __xuname(SYS_NMLN, (void*)name);
822 INTERCEPTOR(int, __xuname, int size, void *utsname) {
823 ENSURE_MSAN_INITED();
824 int res = REAL(__xuname)(size, utsname);
826 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
829 #define MSAN_INTERCEPT_UNAME INTERCEPT_FUNCTION(__xuname)
831 INTERCEPTOR(int, uname, struct utsname *utsname) {
832 ENSURE_MSAN_INITED();
833 int res = REAL(uname)(utsname);
835 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
838 #define MSAN_INTERCEPT_UNAME INTERCEPT_FUNCTION(uname)
841 INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
842 ENSURE_MSAN_INITED();
843 int res = REAL(gethostname)(name, len);
845 SIZE_T real_len = REAL(strnlen)(name, len);
848 __msan_unpoison(name, real_len);
853 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
854 INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
856 ENSURE_MSAN_INITED();
857 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
859 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
863 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
865 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT
868 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
869 INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
870 int timeout, void *sigmask) {
871 ENSURE_MSAN_INITED();
872 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
874 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
878 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
880 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
883 INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
884 GET_MALLOC_STACK_TRACE;
885 if (UNLIKELY(!msan_inited))
886 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
887 return AllocateFromLocalPool(nmemb * size);
888 return msan_calloc(nmemb, size, &stack);
891 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
892 GET_MALLOC_STACK_TRACE;
893 if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
894 uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
895 uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
897 if (UNLIKELY(!msan_inited)) {
898 new_ptr = AllocateFromLocalPool(copy_size);
901 new_ptr = msan_malloc(copy_size, &stack);
903 internal_memcpy(new_ptr, ptr, copy_size);
906 return msan_realloc(ptr, size, &stack);
909 INTERCEPTOR(void *, malloc, SIZE_T size) {
910 GET_MALLOC_STACK_TRACE;
911 if (UNLIKELY(!msan_inited))
912 // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
913 return AllocateFromLocalPool(size);
914 return msan_malloc(size, &stack);
917 void __msan_allocated_memory(const void *data, uptr size) {
918 GET_MALLOC_STACK_TRACE;
919 if (flags()->poison_in_malloc) {
920 stack.tag = STACK_TRACE_TAG_POISON;
921 PoisonMemory(data, size, &stack);
925 void __msan_copy_shadow(void *dest, const void *src, uptr n) {
926 GET_STORE_STACK_TRACE;
927 MoveShadowAndOrigin(dest, src, n, &stack);
930 void __sanitizer_dtor_callback(const void *data, uptr size) {
931 GET_MALLOC_STACK_TRACE;
932 if (flags()->poison_in_dtor) {
933 stack.tag = STACK_TRACE_TAG_POISON;
934 PoisonMemory(data, size, &stack);
938 template <class Mmap>
939 static void *mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length,
940 int prot, int flags, int fd, OFF64_T offset) {
941 if (addr && !MEM_IS_APP(addr)) {
942 if (flags & map_fixed) {
943 errno = errno_EINVAL;
949 void *res = real_mmap(addr, length, prot, flags, fd, offset);
950 if (res != (void *)-1) __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
954 INTERCEPTOR(int, getrusage, int who, void *usage) {
955 ENSURE_MSAN_INITED();
956 int res = REAL(getrusage)(who, usage);
958 __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
963 class SignalHandlerScope {
965 SignalHandlerScope() {
966 if (MsanThread *t = GetCurrentThread())
967 t->EnterSignalHandler();
969 ~SignalHandlerScope() {
970 if (MsanThread *t = GetCurrentThread())
971 t->LeaveSignalHandler();
975 // sigactions_mu guarantees atomicity of sigaction() and signal() calls.
976 // Access to sigactions[] is gone with relaxed atomics to avoid data race with
977 // the signal handler.
978 const int kMaxSignals = 1024;
979 static atomic_uintptr_t sigactions[kMaxSignals];
980 static StaticSpinMutex sigactions_mu;
982 static void SignalHandler(int signo) {
983 SignalHandlerScope signal_handler_scope;
984 ScopedThreadLocalStateBackup stlsb;
987 typedef void (*signal_cb)(int x);
989 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
993 static void SignalAction(int signo, void *si, void *uc) {
994 SignalHandlerScope signal_handler_scope;
995 ScopedThreadLocalStateBackup stlsb;
997 __msan_unpoison(si, sizeof(__sanitizer_sigaction));
998 __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
1000 typedef void (*sigaction_cb)(int, void *, void *);
1002 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1006 static void read_sigaction(const __sanitizer_sigaction *act) {
1007 CHECK_UNPOISONED(&act->sa_flags, sizeof(act->sa_flags));
1008 if (act->sa_flags & __sanitizer::sa_siginfo)
1009 CHECK_UNPOISONED(&act->sigaction, sizeof(act->sigaction));
1011 CHECK_UNPOISONED(&act->handler, sizeof(act->handler));
1012 CHECK_UNPOISONED(&act->sa_mask, sizeof(act->sa_mask));
1015 extern "C" int pthread_attr_init(void *attr);
1016 extern "C" int pthread_attr_destroy(void *attr);
1018 static void *MsanThreadStartFunc(void *arg) {
1019 MsanThread *t = (MsanThread *)arg;
1020 SetCurrentThread(t);
1021 return t->ThreadStart();
1024 INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1026 ENSURE_MSAN_INITED(); // for GetTlsSize()
1027 __sanitizer_pthread_attr_t myattr;
1029 pthread_attr_init(&myattr);
1033 AdjustStackSize(attr);
1035 MsanThread *t = MsanThread::Create(callback, param);
1037 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t);
1039 if (attr == &myattr)
1040 pthread_attr_destroy(&myattr);
1042 __msan_unpoison(th, __sanitizer::pthread_t_sz);
1047 INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1048 void (*dtor)(void *value)) {
1049 if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor);
1050 ENSURE_MSAN_INITED();
1051 int res = REAL(pthread_key_create)(key, dtor);
1053 __msan_unpoison(key, sizeof(*key));
1057 #if SANITIZER_NETBSD
1058 INTERCEPTOR(void, __libc_thr_keycreate, void *m, void (*dtor)(void *value)) \
1059 ALIAS(WRAPPER_NAME(pthread_key_create));
1062 INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1063 ENSURE_MSAN_INITED();
1064 int res = REAL(pthread_join)(th, retval);
1066 __msan_unpoison(retval, sizeof(*retval));
1070 extern char *tzname[2];
1072 INTERCEPTOR(void, tzset, int fake) {
1073 ENSURE_MSAN_INITED();
1076 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1);
1078 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1);
1082 struct MSanAtExitRecord {
1083 void (*func)(void *arg);
1087 void MSanAtExitWrapper(void *arg) {
1089 MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
1094 // Unpoison argument shadow for C++ module destructors.
1095 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
1097 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
1098 ENSURE_MSAN_INITED();
1099 MSanAtExitRecord *r =
1100 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
1103 return REAL(__cxa_atexit)(MSanAtExitWrapper, r, dso_handle);
1106 static void BeforeFork() {
1107 StackDepotLockAll();
1108 ChainedOriginDepotLockAll();
1111 static void AfterFork() {
1112 ChainedOriginDepotUnlockAll();
1113 StackDepotUnlockAll();
1116 INTERCEPTOR(int, fork, void) {
1117 ENSURE_MSAN_INITED();
1119 int pid = REAL(fork)();
1124 // NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly
1127 INTERCEPTOR(int, openpty, int *amaster, int *aslave, char *name,
1128 const void *termp, const void *winp) {
1129 ENSURE_MSAN_INITED();
1130 InterceptorScope interceptor_scope;
1131 int res = REAL(openpty)(amaster, aslave, name, termp, winp);
1133 __msan_unpoison(amaster, sizeof(*amaster));
1134 __msan_unpoison(aslave, sizeof(*aslave));
1138 #define MSAN_MAYBE_INTERCEPT_OPENPTY INTERCEPT_FUNCTION(openpty)
1140 #define MSAN_MAYBE_INTERCEPT_OPENPTY
1143 // NetBSD ships with forkpty(3) in -lutil, that needs to be prebuilt explicitly
1146 INTERCEPTOR(int, forkpty, int *amaster, char *name, const void *termp,
1148 ENSURE_MSAN_INITED();
1149 InterceptorScope interceptor_scope;
1150 int res = REAL(forkpty)(amaster, name, termp, winp);
1152 __msan_unpoison(amaster, sizeof(*amaster));
1155 #define MSAN_MAYBE_INTERCEPT_FORKPTY INTERCEPT_FUNCTION(forkpty)
1157 #define MSAN_MAYBE_INTERCEPT_FORKPTY
1160 struct MSanInterceptorContext {
1161 bool in_interceptor_scope;
1167 // FIXME: ask frontend whether we need to return failure.
1171 } // namespace __msan
1173 // A version of CHECK_UNPOISONED using a saved scope value. Used in common
1175 #define CHECK_UNPOISONED_CTX(ctx, x, n) \
1177 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1178 CHECK_UNPOISONED_0(x, n); \
1181 #define MSAN_INTERCEPT_FUNC(name) \
1183 if ((!INTERCEPT_FUNCTION(name) || !REAL(name))) \
1184 VReport(1, "MemorySanitizer: failed to intercept '" #name "'\n"); \
1187 #define MSAN_INTERCEPT_FUNC_VER(name, ver) \
1189 if ((!INTERCEPT_FUNCTION_VER(name, ver) || !REAL(name))) \
1191 1, "MemorySanitizer: failed to intercept '" #name "@@" #ver "'\n"); \
1194 #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name)
1195 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
1196 MSAN_INTERCEPT_FUNC_VER(name, ver)
1197 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \
1198 UnpoisonParam(count)
1199 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
1200 __msan_unpoison(ptr, size)
1201 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
1202 CHECK_UNPOISONED_CTX(ctx, ptr, size)
1203 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
1204 __msan_unpoison(ptr, size)
1205 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
1206 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \
1207 ENSURE_MSAN_INITED(); \
1208 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1209 ctx = (void *)&msan_ctx; \
1211 InterceptorScope interceptor_scope; \
1212 __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */
1213 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
1216 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1219 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1222 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1225 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
1227 } while (false) // FIXME
1228 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
1230 } while (false) // FIXME
1231 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
1232 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
1233 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
1235 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \
1236 if (filename && map) \
1237 ForEachMappedRegion(map, __msan_unpoison); \
1240 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
1241 if (MsanThread *t = GetCurrentThread()) { \
1242 *begin = t->tls_begin(); \
1243 *end = t->tls_end(); \
1245 *begin = *end = 0; \
1248 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
1251 return __msan_memset(block, c, size); \
1253 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
1256 return __msan_memmove(to, from, size); \
1258 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
1261 return __msan_memcpy(to, from, size); \
1264 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
1266 GET_STORE_STACK_TRACE; \
1267 CopyShadowAndOrigin(to, from, size, &stack); \
1268 __msan_unpoison(to + size, 1); \
1271 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, fd, \
1274 return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \
1277 #include "sanitizer_common/sanitizer_platform_interceptors.h"
1278 #include "sanitizer_common/sanitizer_common_interceptors.inc"
1280 static uptr signal_impl(int signo, uptr cb);
1281 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1282 __sanitizer_sigaction *oldact);
1284 #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \
1285 { return sigaction_impl(signo, act, oldact); }
1287 #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \
1289 handler = signal_impl(signo, handler); \
1290 InterceptorScope interceptor_scope; \
1291 return REAL(func)(signo, handler); \
1294 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
1296 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1297 __sanitizer_sigaction *oldact) {
1298 ENSURE_MSAN_INITED();
1299 if (act) read_sigaction(act);
1301 if (flags()->wrap_signals) {
1302 SpinMutexLock lock(&sigactions_mu);
1303 CHECK_LT(signo, kMaxSignals);
1304 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
1305 __sanitizer_sigaction new_act;
1306 __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr;
1308 REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction));
1309 uptr cb = (uptr)pnew_act->sigaction;
1310 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
1311 ? (uptr)SignalAction
1312 : (uptr)SignalHandler;
1313 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1314 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1315 pnew_act->sigaction = (decltype(pnew_act->sigaction))new_cb;
1318 res = REAL(SIGACTION_SYMNAME)(signo, pnew_act, oldact);
1319 if (res == 0 && oldact) {
1320 uptr cb = (uptr)oldact->sigaction;
1321 if (cb == (uptr)SignalAction || cb == (uptr)SignalHandler) {
1322 oldact->sigaction = (decltype(oldact->sigaction))old_cb;
1326 res = REAL(SIGACTION_SYMNAME)(signo, act, oldact);
1329 if (res == 0 && oldact) {
1330 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
1335 static uptr signal_impl(int signo, uptr cb) {
1336 ENSURE_MSAN_INITED();
1337 if (flags()->wrap_signals) {
1338 CHECK_LT(signo, kMaxSignals);
1339 SpinMutexLock lock(&sigactions_mu);
1340 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1341 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1342 cb = (uptr)&SignalHandler;
1348 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
1349 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
1352 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
1355 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1356 #include "sanitizer_common/sanitizer_common_syscalls.inc"
1357 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
1366 INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
1368 COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info);
1369 int res = REAL(dladdr)(addr, info);
1371 __msan_unpoison(info, sizeof(*info));
1372 if (info->dli_fname)
1373 __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
1374 if (info->dli_sname)
1375 __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
1380 INTERCEPTOR(char *, dlerror, int fake) {
1382 COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake);
1383 char *res = REAL(dlerror)(fake);
1384 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
1388 typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
1390 struct dl_iterate_phdr_data {
1391 dl_iterate_phdr_cb callback;
1395 static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
1398 __msan_unpoison(info, size);
1399 if (info->dlpi_phdr && info->dlpi_phnum)
1400 __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum);
1401 if (info->dlpi_name)
1402 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
1404 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
1406 return cbdata->callback(info, size, cbdata->data);
1409 INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) {
1410 ENSURE_MSAN_INITED();
1411 void *p = REAL(shmat)(shmid, shmaddr, shmflg);
1412 if (p != (void *)-1) {
1413 __sanitizer_shmid_ds ds;
1414 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds);
1416 __msan_unpoison(p, ds.shm_segsz);
1422 INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
1424 COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data);
1425 dl_iterate_phdr_data cbdata;
1426 cbdata.callback = callback;
1428 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
1432 // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
1433 INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
1434 ENSURE_MSAN_INITED();
1435 wchar_t *res = REAL(wcschr)(s, wc, ps);
1439 // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
1440 INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
1441 ENSURE_MSAN_INITED();
1442 GET_STORE_STACK_TRACE;
1443 wchar_t *res = REAL(wcscpy)(dest, src);
1444 CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1),
1449 INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src,
1450 SIZE_T n) { // NOLINT
1451 ENSURE_MSAN_INITED();
1452 GET_STORE_STACK_TRACE;
1453 SIZE_T copy_size = REAL(wcsnlen)(src, n);
1454 if (copy_size < n) copy_size++; // trailing \0
1455 wchar_t *res = REAL(wcsncpy)(dest, src, n); // NOLINT
1456 CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack);
1457 __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t));
1461 // These interface functions reside here so that they can use
1462 // REAL(memset), etc.
1463 void __msan_unpoison(const void *a, uptr size) {
1464 if (!MEM_IS_APP(a)) return;
1465 SetShadow(a, size, 0);
1468 void __msan_poison(const void *a, uptr size) {
1469 if (!MEM_IS_APP(a)) return;
1470 SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1);
1473 void __msan_poison_stack(void *a, uptr size) {
1474 if (!MEM_IS_APP(a)) return;
1475 SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1);
1478 void __msan_clear_and_unpoison(void *a, uptr size) {
1479 REAL(memset)(a, 0, size);
1480 SetShadow(a, size, 0);
1483 void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
1484 if (!msan_inited) return internal_memcpy(dest, src, n);
1485 if (msan_init_is_running || __msan::IsInSymbolizer())
1486 return REAL(memcpy)(dest, src, n);
1487 ENSURE_MSAN_INITED();
1488 GET_STORE_STACK_TRACE;
1489 void *res = REAL(memcpy)(dest, src, n);
1490 CopyShadowAndOrigin(dest, src, n, &stack);
1494 void *__msan_memset(void *s, int c, SIZE_T n) {
1495 if (!msan_inited) return internal_memset(s, c, n);
1496 if (msan_init_is_running) return REAL(memset)(s, c, n);
1497 ENSURE_MSAN_INITED();
1498 void *res = REAL(memset)(s, c, n);
1499 __msan_unpoison(s, n);
1503 void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
1504 if (!msan_inited) return internal_memmove(dest, src, n);
1505 if (msan_init_is_running) return REAL(memmove)(dest, src, n);
1506 ENSURE_MSAN_INITED();
1507 GET_STORE_STACK_TRACE;
1508 void *res = REAL(memmove)(dest, src, n);
1509 MoveShadowAndOrigin(dest, src, n, &stack);
1513 void __msan_unpoison_string(const char* s) {
1514 if (!MEM_IS_APP(s)) return;
1515 __msan_unpoison(s, REAL(strlen)(s) + 1);
1520 void InitializeInterceptors() {
1521 static int inited = 0;
1522 CHECK_EQ(inited, 0);
1523 InitializeCommonInterceptors();
1524 InitializeSignalInterceptors();
1526 INTERCEPT_FUNCTION(posix_memalign);
1527 MSAN_MAYBE_INTERCEPT_MEMALIGN;
1528 MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
1529 INTERCEPT_FUNCTION(valloc);
1530 MSAN_MAYBE_INTERCEPT_PVALLOC;
1531 INTERCEPT_FUNCTION(malloc);
1532 INTERCEPT_FUNCTION(calloc);
1533 INTERCEPT_FUNCTION(realloc);
1534 INTERCEPT_FUNCTION(free);
1535 MSAN_MAYBE_INTERCEPT_CFREE;
1536 MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
1537 MSAN_MAYBE_INTERCEPT_MALLINFO;
1538 MSAN_MAYBE_INTERCEPT_MALLOPT;
1539 MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
1540 INTERCEPT_FUNCTION(fread);
1541 MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED;
1542 INTERCEPT_FUNCTION(memccpy);
1543 MSAN_MAYBE_INTERCEPT_MEMPCPY;
1544 INTERCEPT_FUNCTION(bcopy);
1545 INTERCEPT_FUNCTION(wmemset);
1546 INTERCEPT_FUNCTION(wmemcpy);
1547 MSAN_MAYBE_INTERCEPT_WMEMPCPY;
1548 INTERCEPT_FUNCTION(wmemmove);
1549 INTERCEPT_FUNCTION(strcpy); // NOLINT
1550 MSAN_MAYBE_INTERCEPT_STPCPY; // NOLINT
1551 INTERCEPT_FUNCTION(strdup);
1552 MSAN_MAYBE_INTERCEPT___STRDUP;
1553 INTERCEPT_FUNCTION(strncpy); // NOLINT
1554 MSAN_MAYBE_INTERCEPT_GCVT;
1555 INTERCEPT_FUNCTION(strcat); // NOLINT
1556 INTERCEPT_FUNCTION(strncat); // NOLINT
1557 INTERCEPT_STRTO(strtod);
1558 INTERCEPT_STRTO(strtof);
1559 INTERCEPT_STRTO(strtold);
1560 INTERCEPT_STRTO(strtol);
1561 INTERCEPT_STRTO(strtoul);
1562 INTERCEPT_STRTO(strtoll);
1563 INTERCEPT_STRTO(strtoull);
1564 INTERCEPT_STRTO(strtouq);
1565 INTERCEPT_STRTO(wcstod);
1566 INTERCEPT_STRTO(wcstof);
1567 INTERCEPT_STRTO(wcstold);
1568 INTERCEPT_STRTO(wcstol);
1569 INTERCEPT_STRTO(wcstoul);
1570 INTERCEPT_STRTO(wcstoll);
1571 INTERCEPT_STRTO(wcstoull);
1572 #ifdef SANITIZER_NLDBL_VERSION
1573 INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION);
1574 INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION);
1576 INTERCEPT_FUNCTION(vswprintf);
1577 INTERCEPT_FUNCTION(swprintf);
1579 INTERCEPT_FUNCTION(strftime);
1580 INTERCEPT_FUNCTION(strftime_l);
1581 MSAN_MAYBE_INTERCEPT___STRFTIME_L;
1582 INTERCEPT_FUNCTION(wcsftime);
1583 INTERCEPT_FUNCTION(wcsftime_l);
1584 MSAN_MAYBE_INTERCEPT___WCSFTIME_L;
1585 INTERCEPT_FUNCTION(mbtowc);
1586 INTERCEPT_FUNCTION(mbrtowc);
1587 INTERCEPT_FUNCTION(wcslen);
1588 INTERCEPT_FUNCTION(wcsnlen);
1589 INTERCEPT_FUNCTION(wcschr);
1590 INTERCEPT_FUNCTION(wcscpy);
1591 INTERCEPT_FUNCTION(wcsncpy);
1592 INTERCEPT_FUNCTION(wcscmp);
1593 INTERCEPT_FUNCTION(getenv);
1594 INTERCEPT_FUNCTION(setenv);
1595 INTERCEPT_FUNCTION(putenv);
1596 INTERCEPT_FUNCTION(gettimeofday);
1597 MSAN_MAYBE_INTERCEPT_FCVT;
1598 MSAN_MAYBE_INTERCEPT_FSTAT;
1599 MSAN_MAYBE_INTERCEPT___FXSTAT;
1600 MSAN_INTERCEPT_FSTATAT;
1601 MSAN_MAYBE_INTERCEPT___FXSTAT64;
1602 MSAN_MAYBE_INTERCEPT___FXSTATAT64;
1603 INTERCEPT_FUNCTION(pipe);
1604 INTERCEPT_FUNCTION(pipe2);
1605 INTERCEPT_FUNCTION(socketpair);
1606 MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED;
1607 INTERCEPT_FUNCTION(getrlimit);
1608 MSAN_MAYBE_INTERCEPT_GETRLIMIT64;
1609 MSAN_MAYBE_INTERCEPT_PRLIMIT;
1610 MSAN_MAYBE_INTERCEPT_PRLIMIT64;
1611 MSAN_INTERCEPT_UNAME;
1612 INTERCEPT_FUNCTION(gethostname);
1613 MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1614 MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1615 INTERCEPT_FUNCTION(dladdr);
1616 INTERCEPT_FUNCTION(dlerror);
1617 INTERCEPT_FUNCTION(dl_iterate_phdr);
1618 INTERCEPT_FUNCTION(getrusage);
1619 #if defined(__mips__)
1620 INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2");
1622 INTERCEPT_FUNCTION(pthread_create);
1624 INTERCEPT_FUNCTION(pthread_key_create);
1626 #if SANITIZER_NETBSD
1627 INTERCEPT_FUNCTION(__libc_thr_keycreate);
1630 INTERCEPT_FUNCTION(pthread_join);
1631 INTERCEPT_FUNCTION(tzset);
1632 INTERCEPT_FUNCTION(__cxa_atexit);
1633 INTERCEPT_FUNCTION(shmat);
1634 INTERCEPT_FUNCTION(fork);
1635 MSAN_MAYBE_INTERCEPT_OPENPTY;
1636 MSAN_MAYBE_INTERCEPT_FORKPTY;
1640 } // namespace __msan