1 //===-- msan_interceptors.cpp ---------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemorySanitizer.
10 //
11 // Interceptors for standard library functions.
12 //
13 // FIXME: move as many interceptors as possible into
14 // sanitizer_common/sanitizer_common_interceptors.h
15 //===----------------------------------------------------------------------===//
16
17 #include "interception/interception.h"
18 #include "msan.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_errno_codes.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_glibc_version.h"
37 #include "sanitizer_common/sanitizer_tls_get_addr.h"
38 #include "sanitizer_common/sanitizer_vector.h"
39
40 #if SANITIZER_NETBSD
41 #define fstat __fstat50
42 #define gettimeofday __gettimeofday50
43 #define getrusage __getrusage50
44 #define tzset __tzset50
45 #endif
46
47 #include <stdarg.h>
48 // ACHTUNG! No other system header includes in this file.
49 // Ideally, we should get rid of stdarg.h as well.
50
51 using namespace __msan;
52
53 using __sanitizer::memory_order;
54 using __sanitizer::atomic_load;
55 using __sanitizer::atomic_store;
56 using __sanitizer::atomic_uintptr_t;
57
58 DECLARE_REAL(SIZE_T, strlen, const char *s)
59 DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)
60 DECLARE_REAL(void *, memcpy, void *dest, const void *src, uptr n)
61 DECLARE_REAL(void *, memset, void *dest, int c, uptr n)
62
63 // True if this is a nested interceptor.
64 static THREADLOCAL int in_interceptor_scope;
65
__msan_scoped_disable_interceptor_checks()66 void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; }
__msan_scoped_enable_interceptor_checks()67 void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; }
68
69 struct InterceptorScope {
InterceptorScopeInterceptorScope70 InterceptorScope() { ++in_interceptor_scope; }
~InterceptorScopeInterceptorScope71 ~InterceptorScope() { --in_interceptor_scope; }
72 };
73
IsInInterceptorScope()74 bool IsInInterceptorScope() {
75 return in_interceptor_scope;
76 }
77
78 static uptr allocated_for_dlsym;
79 static const uptr kDlsymAllocPoolSize = 1024;
80 static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
81
IsInDlsymAllocPool(const void * ptr)82 static bool IsInDlsymAllocPool(const void *ptr) {
83 uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
84 return off < sizeof(alloc_memory_for_dlsym);
85 }
86
AllocateFromLocalPool(uptr size_in_bytes)87 static void *AllocateFromLocalPool(uptr size_in_bytes) {
88 uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
89 void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
90 allocated_for_dlsym += size_in_words;
91 CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
92 return mem;
93 }
94
95 #define ENSURE_MSAN_INITED() do { \
96 CHECK(!msan_init_is_running); \
97 if (!msan_inited) { \
98 __msan_init(); \
99 } \
100 } while (0)
101
102 // Check that [x, x+n) range is unpoisoned.
103 #define CHECK_UNPOISONED_0(x, n) \
104 do { \
105 sptr __offset = __msan_test_shadow(x, n); \
106 if (__msan::IsInSymbolizer()) break; \
107 if (__offset >= 0 && __msan::flags()->report_umrs) { \
108 GET_CALLER_PC_BP_SP; \
109 (void)sp; \
110 ReportUMRInsideAddressRange(__func__, x, n, __offset); \
111 __msan::PrintWarningWithOrigin( \
112 pc, bp, __msan_get_origin((const char *)x + __offset)); \
113 if (__msan::flags()->halt_on_error) { \
114 Printf("Exiting\n"); \
115 Die(); \
116 } \
117 } \
118 } while (0)
119
120 // Check that [x, x+n) range is unpoisoned unless we are in a nested
121 // interceptor.
122 #define CHECK_UNPOISONED(x, n) \
123 do { \
124 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
125 } while (0)
126
127 #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \
128 CHECK_UNPOISONED((x), \
129 common_flags()->strict_string_checks ? (len) + 1 : (n) )
130
131 #define CHECK_UNPOISONED_STRING(x, n) \
132 CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n))
133
134 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(SIZE_T,fread_unlocked,void * ptr,SIZE_T size,SIZE_T nmemb,void * file)135 INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
136 void *file) {
137 ENSURE_MSAN_INITED();
138 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
139 if (res > 0)
140 __msan_unpoison(ptr, res *size);
141 return res;
142 }
143 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked)
144 #else
145 #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED
146 #endif
147
148 #if !SANITIZER_NETBSD
INTERCEPTOR(void *,mempcpy,void * dest,const void * src,SIZE_T n)149 INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
150 return (char *)__msan_memcpy(dest, src, n) + n;
151 }
152 #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy)
153 #else
154 #define MSAN_MAYBE_INTERCEPT_MEMPCPY
155 #endif
156
INTERCEPTOR(void *,memccpy,void * dest,const void * src,int c,SIZE_T n)157 INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) {
158 ENSURE_MSAN_INITED();
159 void *res = REAL(memccpy)(dest, src, c, n);
160 CHECK(!res || (res >= dest && res <= (char *)dest + n));
161 SIZE_T sz = res ? (char *)res - (char *)dest : n;
162 CHECK_UNPOISONED(src, sz);
163 __msan_unpoison(dest, sz);
164 return res;
165 }
166
INTERCEPTOR(void *,bcopy,const void * src,void * dest,SIZE_T n)167 INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
168 return __msan_memmove(dest, src, n);
169 }
170
INTERCEPTOR(int,posix_memalign,void ** memptr,SIZE_T alignment,SIZE_T size)171 INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
172 GET_MALLOC_STACK_TRACE;
173 CHECK_NE(memptr, 0);
174 int res = msan_posix_memalign(memptr, alignment, size, &stack);
175 if (!res)
176 __msan_unpoison(memptr, sizeof(*memptr));
177 return res;
178 }
179
180 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(void *,memalign,SIZE_T alignment,SIZE_T size)181 INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
182 GET_MALLOC_STACK_TRACE;
183 return msan_memalign(alignment, size, &stack);
184 }
185 #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
186 #else
187 #define MSAN_MAYBE_INTERCEPT_MEMALIGN
188 #endif
189
INTERCEPTOR(void *,aligned_alloc,SIZE_T alignment,SIZE_T size)190 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
191 GET_MALLOC_STACK_TRACE;
192 return msan_aligned_alloc(alignment, size, &stack);
193 }
194
195 #if !SANITIZER_NETBSD
INTERCEPTOR(void *,__libc_memalign,SIZE_T alignment,SIZE_T size)196 INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
197 GET_MALLOC_STACK_TRACE;
198 void *ptr = msan_memalign(alignment, size, &stack);
199 if (ptr)
200 DTLS_on_libc_memalign(ptr, size);
201 return ptr;
202 }
203 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
204 #else
205 #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
206 #endif
207
INTERCEPTOR(void *,valloc,SIZE_T size)208 INTERCEPTOR(void *, valloc, SIZE_T size) {
209 GET_MALLOC_STACK_TRACE;
210 return msan_valloc(size, &stack);
211 }
212
213 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(void *,pvalloc,SIZE_T size)214 INTERCEPTOR(void *, pvalloc, SIZE_T size) {
215 GET_MALLOC_STACK_TRACE;
216 return msan_pvalloc(size, &stack);
217 }
218 #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
219 #else
220 #define MSAN_MAYBE_INTERCEPT_PVALLOC
221 #endif
222
INTERCEPTOR(void,free,void * ptr)223 INTERCEPTOR(void, free, void *ptr) {
224 GET_MALLOC_STACK_TRACE;
225 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
226 MsanDeallocate(&stack, ptr);
227 }
228
229 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(void,cfree,void * ptr)230 INTERCEPTOR(void, cfree, void *ptr) {
231 GET_MALLOC_STACK_TRACE;
232 if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return;
233 MsanDeallocate(&stack, ptr);
234 }
235 #define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
236 #else
237 #define MSAN_MAYBE_INTERCEPT_CFREE
238 #endif
239
240 #if !SANITIZER_NETBSD
INTERCEPTOR(uptr,malloc_usable_size,void * ptr)241 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
242 return __sanitizer_get_allocated_size(ptr);
243 }
244 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
245 INTERCEPT_FUNCTION(malloc_usable_size)
246 #else
247 #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
248 #endif
249
250 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
251 // This function actually returns a struct by value, but we can't unpoison a
252 // temporary! The following is equivalent on all supported platforms but
253 // aarch64 (which uses a different register for sret value). We have a test
254 // to confirm that.
INTERCEPTOR(void,mallinfo,__sanitizer_struct_mallinfo * sret)255 INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
256 #ifdef __aarch64__
257 uptr r8;
258 asm volatile("mov %0,x8" : "=r" (r8));
259 sret = reinterpret_cast<__sanitizer_struct_mallinfo*>(r8);
260 #endif
261 REAL(memset)(sret, 0, sizeof(*sret));
262 __msan_unpoison(sret, sizeof(*sret));
263 }
264 #define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
265 #else
266 #define MSAN_MAYBE_INTERCEPT_MALLINFO
267 #endif
268
269 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,mallopt,int cmd,int value)270 INTERCEPTOR(int, mallopt, int cmd, int value) {
271 return 0;
272 }
273 #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
274 #else
275 #define MSAN_MAYBE_INTERCEPT_MALLOPT
276 #endif
277
278 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(void,malloc_stats,void)279 INTERCEPTOR(void, malloc_stats, void) {
280 // FIXME: implement, but don't call REAL(malloc_stats)!
281 }
282 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats)
283 #else
284 #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS
285 #endif
286
INTERCEPTOR(char *,strcpy,char * dest,const char * src)287 INTERCEPTOR(char *, strcpy, char *dest, const char *src) {
288 ENSURE_MSAN_INITED();
289 GET_STORE_STACK_TRACE;
290 SIZE_T n = REAL(strlen)(src);
291 CHECK_UNPOISONED_STRING(src + n, 0);
292 char *res = REAL(strcpy)(dest, src);
293 CopyShadowAndOrigin(dest, src, n + 1, &stack);
294 return res;
295 }
296
INTERCEPTOR(char *,strncpy,char * dest,const char * src,SIZE_T n)297 INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) {
298 ENSURE_MSAN_INITED();
299 GET_STORE_STACK_TRACE;
300 SIZE_T copy_size = REAL(strnlen)(src, n);
301 if (copy_size < n)
302 copy_size++; // trailing \0
303 char *res = REAL(strncpy)(dest, src, n);
304 CopyShadowAndOrigin(dest, src, copy_size, &stack);
305 __msan_unpoison(dest + copy_size, n - copy_size);
306 return res;
307 }
308
309 #if !SANITIZER_NETBSD
INTERCEPTOR(char *,stpcpy,char * dest,const char * src)310 INTERCEPTOR(char *, stpcpy, char *dest, const char *src) {
311 ENSURE_MSAN_INITED();
312 GET_STORE_STACK_TRACE;
313 SIZE_T n = REAL(strlen)(src);
314 CHECK_UNPOISONED_STRING(src + n, 0);
315 char *res = REAL(stpcpy)(dest, src);
316 CopyShadowAndOrigin(dest, src, n + 1, &stack);
317 return res;
318 }
319 #define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy)
320 #else
321 #define MSAN_MAYBE_INTERCEPT_STPCPY
322 #endif
323
INTERCEPTOR(char *,strdup,char * src)324 INTERCEPTOR(char *, strdup, char *src) {
325 ENSURE_MSAN_INITED();
326 GET_STORE_STACK_TRACE;
327 // On FreeBSD strdup() leverages strlen().
328 InterceptorScope interceptor_scope;
329 SIZE_T n = REAL(strlen)(src);
330 CHECK_UNPOISONED_STRING(src + n, 0);
331 char *res = REAL(strdup)(src);
332 CopyShadowAndOrigin(res, src, n + 1, &stack);
333 return res;
334 }
335
336 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(char *,__strdup,char * src)337 INTERCEPTOR(char *, __strdup, char *src) {
338 ENSURE_MSAN_INITED();
339 GET_STORE_STACK_TRACE;
340 SIZE_T n = REAL(strlen)(src);
341 CHECK_UNPOISONED_STRING(src + n, 0);
342 char *res = REAL(__strdup)(src);
343 CopyShadowAndOrigin(res, src, n + 1, &stack);
344 return res;
345 }
346 #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup)
347 #else
348 #define MSAN_MAYBE_INTERCEPT___STRDUP
349 #endif
350
351 #if !SANITIZER_NETBSD
INTERCEPTOR(char *,gcvt,double number,SIZE_T ndigit,char * buf)352 INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
353 ENSURE_MSAN_INITED();
354 char *res = REAL(gcvt)(number, ndigit, buf);
355 SIZE_T n = REAL(strlen)(buf);
356 __msan_unpoison(buf, n + 1);
357 return res;
358 }
359 #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt)
360 #else
361 #define MSAN_MAYBE_INTERCEPT_GCVT
362 #endif
363
INTERCEPTOR(char *,strcat,char * dest,const char * src)364 INTERCEPTOR(char *, strcat, char *dest, const char *src) {
365 ENSURE_MSAN_INITED();
366 GET_STORE_STACK_TRACE;
367 SIZE_T src_size = REAL(strlen)(src);
368 SIZE_T dest_size = REAL(strlen)(dest);
369 CHECK_UNPOISONED_STRING(src + src_size, 0);
370 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
371 char *res = REAL(strcat)(dest, src);
372 CopyShadowAndOrigin(dest + dest_size, src, src_size + 1, &stack);
373 return res;
374 }
375
INTERCEPTOR(char *,strncat,char * dest,const char * src,SIZE_T n)376 INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) {
377 ENSURE_MSAN_INITED();
378 GET_STORE_STACK_TRACE;
379 SIZE_T dest_size = REAL(strlen)(dest);
380 SIZE_T copy_size = REAL(strnlen)(src, n);
381 CHECK_UNPOISONED_STRING(dest + dest_size, 0);
382 char *res = REAL(strncat)(dest, src, n);
383 CopyShadowAndOrigin(dest + dest_size, src, copy_size, &stack);
384 __msan_unpoison(dest + dest_size + copy_size, 1); // \0
385 return res;
386 }
387
388 // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to
389 // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO.
390 #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \
391 ENSURE_MSAN_INITED(); \
392 ret_type res = REAL(func)(__VA_ARGS__); \
393 __msan_unpoison(endptr, sizeof(*endptr)); \
394 return res;
395
396 #define INTERCEPTOR_STRTO(ret_type, func, char_type) \
397 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \
398 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \
399 }
400
401 #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
402 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
403 int base) { \
404 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \
405 }
406
407 #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \
408 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
409 void *loc) { \
410 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \
411 }
412
413 #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \
414 INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \
415 int base, void *loc) { \
416 INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base, loc); \
417 }
418
419 #if SANITIZER_NETBSD
420 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
421 INTERCEPTOR_STRTO(ret_type, func, char_type) \
422 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type)
423
424 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
425 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
426 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type)
427
428 #else
429 #define INTERCEPTORS_STRTO(ret_type, func, char_type) \
430 INTERCEPTOR_STRTO(ret_type, func, char_type) \
431 INTERCEPTOR_STRTO_LOC(ret_type, func##_l, char_type) \
432 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_l, char_type) \
433 INTERCEPTOR_STRTO_LOC(ret_type, __##func##_internal, char_type)
434
435 #define INTERCEPTORS_STRTO_BASE(ret_type, func, char_type) \
436 INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \
437 INTERCEPTOR_STRTO_BASE_LOC(ret_type, func##_l, char_type) \
438 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_l, char_type) \
439 INTERCEPTOR_STRTO_BASE_LOC(ret_type, __##func##_internal, char_type)
440 #endif
441
INTERCEPTORS_STRTO(double,strtod,char)442 INTERCEPTORS_STRTO(double, strtod, char)
443 INTERCEPTORS_STRTO(float, strtof, char)
444 INTERCEPTORS_STRTO(long double, strtold, char)
445 INTERCEPTORS_STRTO_BASE(long, strtol, char)
446 INTERCEPTORS_STRTO_BASE(long long, strtoll, char)
447 INTERCEPTORS_STRTO_BASE(unsigned long, strtoul, char)
448 INTERCEPTORS_STRTO_BASE(unsigned long long, strtoull, char)
449 INTERCEPTORS_STRTO_BASE(u64, strtouq, char)
450
451 INTERCEPTORS_STRTO(double, wcstod, wchar_t)
452 INTERCEPTORS_STRTO(float, wcstof, wchar_t)
453 INTERCEPTORS_STRTO(long double, wcstold, wchar_t)
454 INTERCEPTORS_STRTO_BASE(long, wcstol, wchar_t)
455 INTERCEPTORS_STRTO_BASE(long long, wcstoll, wchar_t)
456 INTERCEPTORS_STRTO_BASE(unsigned long, wcstoul, wchar_t)
457 INTERCEPTORS_STRTO_BASE(unsigned long long, wcstoull, wchar_t)
458
459 #if SANITIZER_NETBSD
460 #define INTERCEPT_STRTO(func) \
461 INTERCEPT_FUNCTION(func); \
462 INTERCEPT_FUNCTION(func##_l);
463 #else
464 #define INTERCEPT_STRTO(func) \
465 INTERCEPT_FUNCTION(func); \
466 INTERCEPT_FUNCTION(func##_l); \
467 INTERCEPT_FUNCTION(__##func##_l); \
468 INTERCEPT_FUNCTION(__##func##_internal);
469 #endif
470
471
472 // FIXME: support *wprintf in common format interceptors.
473 INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
474 ENSURE_MSAN_INITED();
475 int res = REAL(vswprintf)(str, size, format, ap);
476 if (res >= 0) {
477 __msan_unpoison(str, 4 * (res + 1));
478 }
479 return res;
480 }
481
INTERCEPTOR(int,swprintf,void * str,uptr size,void * format,...)482 INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
483 ENSURE_MSAN_INITED();
484 va_list ap;
485 va_start(ap, format);
486 int res = vswprintf(str, size, format, ap);
487 va_end(ap);
488 return res;
489 }
490
491 #define INTERCEPTOR_STRFTIME_BODY(char_type, ret_type, func, s, ...) \
492 ENSURE_MSAN_INITED(); \
493 InterceptorScope interceptor_scope; \
494 ret_type res = REAL(func)(s, __VA_ARGS__); \
495 if (s) __msan_unpoison(s, sizeof(char_type) * (res + 1)); \
496 return res;
497
INTERCEPTOR(SIZE_T,strftime,char * s,SIZE_T max,const char * format,__sanitizer_tm * tm)498 INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
499 __sanitizer_tm *tm) {
500 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime, s, max, format, tm);
501 }
502
INTERCEPTOR(SIZE_T,strftime_l,char * s,SIZE_T max,const char * format,__sanitizer_tm * tm,void * loc)503 INTERCEPTOR(SIZE_T, strftime_l, char *s, SIZE_T max, const char *format,
504 __sanitizer_tm *tm, void *loc) {
505 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, strftime_l, s, max, format, tm, loc);
506 }
507
508 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(SIZE_T,__strftime_l,char * s,SIZE_T max,const char * format,__sanitizer_tm * tm,void * loc)509 INTERCEPTOR(SIZE_T, __strftime_l, char *s, SIZE_T max, const char *format,
510 __sanitizer_tm *tm, void *loc) {
511 INTERCEPTOR_STRFTIME_BODY(char, SIZE_T, __strftime_l, s, max, format, tm,
512 loc);
513 }
514 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L INTERCEPT_FUNCTION(__strftime_l)
515 #else
516 #define MSAN_MAYBE_INTERCEPT___STRFTIME_L
517 #endif
518
INTERCEPTOR(SIZE_T,wcsftime,wchar_t * s,SIZE_T max,const wchar_t * format,__sanitizer_tm * tm)519 INTERCEPTOR(SIZE_T, wcsftime, wchar_t *s, SIZE_T max, const wchar_t *format,
520 __sanitizer_tm *tm) {
521 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime, s, max, format, tm);
522 }
523
INTERCEPTOR(SIZE_T,wcsftime_l,wchar_t * s,SIZE_T max,const wchar_t * format,__sanitizer_tm * tm,void * loc)524 INTERCEPTOR(SIZE_T, wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
525 __sanitizer_tm *tm, void *loc) {
526 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, wcsftime_l, s, max, format, tm,
527 loc);
528 }
529
530 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(SIZE_T,__wcsftime_l,wchar_t * s,SIZE_T max,const wchar_t * format,__sanitizer_tm * tm,void * loc)531 INTERCEPTOR(SIZE_T, __wcsftime_l, wchar_t *s, SIZE_T max, const wchar_t *format,
532 __sanitizer_tm *tm, void *loc) {
533 INTERCEPTOR_STRFTIME_BODY(wchar_t, SIZE_T, __wcsftime_l, s, max, format, tm,
534 loc);
535 }
536 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L INTERCEPT_FUNCTION(__wcsftime_l)
537 #else
538 #define MSAN_MAYBE_INTERCEPT___WCSFTIME_L
539 #endif
540
INTERCEPTOR(int,mbtowc,wchar_t * dest,const char * src,SIZE_T n)541 INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
542 ENSURE_MSAN_INITED();
543 int res = REAL(mbtowc)(dest, src, n);
544 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
545 return res;
546 }
547
INTERCEPTOR(SIZE_T,mbrtowc,wchar_t * dest,const char * src,SIZE_T n,void * ps)548 INTERCEPTOR(SIZE_T, mbrtowc, wchar_t *dest, const char *src, SIZE_T n,
549 void *ps) {
550 ENSURE_MSAN_INITED();
551 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
552 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
553 return res;
554 }
555
556 // wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
INTERCEPTOR(wchar_t *,wmemcpy,wchar_t * dest,const wchar_t * src,SIZE_T n)557 INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
558 ENSURE_MSAN_INITED();
559 GET_STORE_STACK_TRACE;
560 wchar_t *res = REAL(wmemcpy)(dest, src, n);
561 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
562 return res;
563 }
564
565 #if !SANITIZER_NETBSD
INTERCEPTOR(wchar_t *,wmempcpy,wchar_t * dest,const wchar_t * src,SIZE_T n)566 INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
567 ENSURE_MSAN_INITED();
568 GET_STORE_STACK_TRACE;
569 wchar_t *res = REAL(wmempcpy)(dest, src, n);
570 CopyShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
571 return res;
572 }
573 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY INTERCEPT_FUNCTION(wmempcpy)
574 #else
575 #define MSAN_MAYBE_INTERCEPT_WMEMPCPY
576 #endif
577
INTERCEPTOR(wchar_t *,wmemset,wchar_t * s,wchar_t c,SIZE_T n)578 INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
579 CHECK(MEM_IS_APP(s));
580 ENSURE_MSAN_INITED();
581 wchar_t *res = REAL(wmemset)(s, c, n);
582 __msan_unpoison(s, n * sizeof(wchar_t));
583 return res;
584 }
585
INTERCEPTOR(wchar_t *,wmemmove,wchar_t * dest,const wchar_t * src,SIZE_T n)586 INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
587 ENSURE_MSAN_INITED();
588 GET_STORE_STACK_TRACE;
589 wchar_t *res = REAL(wmemmove)(dest, src, n);
590 MoveShadowAndOrigin(dest, src, n * sizeof(wchar_t), &stack);
591 return res;
592 }
593
INTERCEPTOR(int,wcscmp,const wchar_t * s1,const wchar_t * s2)594 INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
595 ENSURE_MSAN_INITED();
596 int res = REAL(wcscmp)(s1, s2);
597 return res;
598 }
599
INTERCEPTOR(int,gettimeofday,void * tv,void * tz)600 INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
601 ENSURE_MSAN_INITED();
602 int res = REAL(gettimeofday)(tv, tz);
603 if (tv)
604 __msan_unpoison(tv, 16);
605 if (tz)
606 __msan_unpoison(tz, 8);
607 return res;
608 }
609
610 #if !SANITIZER_NETBSD
INTERCEPTOR(char *,fcvt,double x,int a,int * b,int * c)611 INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
612 ENSURE_MSAN_INITED();
613 char *res = REAL(fcvt)(x, a, b, c);
614 __msan_unpoison(b, sizeof(*b));
615 __msan_unpoison(c, sizeof(*c));
616 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
617 return res;
618 }
619 #define MSAN_MAYBE_INTERCEPT_FCVT INTERCEPT_FUNCTION(fcvt)
620 #else
621 #define MSAN_MAYBE_INTERCEPT_FCVT
622 #endif
623
INTERCEPTOR(char *,getenv,char * name)624 INTERCEPTOR(char *, getenv, char *name) {
625 if (msan_init_is_running)
626 return REAL(getenv)(name);
627 ENSURE_MSAN_INITED();
628 char *res = REAL(getenv)(name);
629 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
630 return res;
631 }
632
633 extern char **environ;
634
UnpoisonEnviron()635 static void UnpoisonEnviron() {
636 char **envp = environ;
637 for (; *envp; ++envp) {
638 __msan_unpoison(envp, sizeof(*envp));
639 __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
640 }
641 // Trailing NULL pointer.
642 __msan_unpoison(envp, sizeof(*envp));
643 }
644
INTERCEPTOR(int,setenv,const char * name,const char * value,int overwrite)645 INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
646 ENSURE_MSAN_INITED();
647 CHECK_UNPOISONED_STRING(name, 0);
648 int res = REAL(setenv)(name, value, overwrite);
649 if (!res) UnpoisonEnviron();
650 return res;
651 }
652
INTERCEPTOR(int,putenv,char * string)653 INTERCEPTOR(int, putenv, char *string) {
654 ENSURE_MSAN_INITED();
655 int res = REAL(putenv)(string);
656 if (!res) UnpoisonEnviron();
657 return res;
658 }
659
660 #define SANITIZER_STAT_LINUX (SANITIZER_LINUX && __GLIBC_PREREQ(2, 33))
661 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX
INTERCEPTOR(int,fstat,int fd,void * buf)662 INTERCEPTOR(int, fstat, int fd, void *buf) {
663 ENSURE_MSAN_INITED();
664 int res = REAL(fstat)(fd, buf);
665 if (!res)
666 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
667 return res;
668 }
669 # define MSAN_MAYBE_INTERCEPT_FSTAT MSAN_INTERCEPT_FUNC(fstat)
670 #else
671 #define MSAN_MAYBE_INTERCEPT_FSTAT
672 #endif
673
674 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,__fxstat,int magic,int fd,void * buf)675 INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
676 ENSURE_MSAN_INITED();
677 int res = REAL(__fxstat)(magic, fd, buf);
678 if (!res)
679 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
680 return res;
681 }
682 # define MSAN_MAYBE_INTERCEPT___FXSTAT MSAN_INTERCEPT_FUNC(__fxstat)
683 #else
684 #define MSAN_MAYBE_INTERCEPT___FXSTAT
685 #endif
686
687 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,__fxstat64,int magic,int fd,void * buf)688 INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
689 ENSURE_MSAN_INITED();
690 int res = REAL(__fxstat64)(magic, fd, buf);
691 if (!res)
692 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
693 return res;
694 }
695 # define MSAN_MAYBE_INTERCEPT___FXSTAT64 MSAN_INTERCEPT_FUNC(__fxstat64)
696 #else
697 # define MSAN_MAYBE_INTERCEPT___FXSTAT64
698 #endif
699
700 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_STAT_LINUX
INTERCEPTOR(int,fstatat,int fd,char * pathname,void * buf,int flags)701 INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
702 ENSURE_MSAN_INITED();
703 int res = REAL(fstatat)(fd, pathname, buf, flags);
704 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
705 return res;
706 }
707 # define MSAN_MAYBE_INTERCEPT_FSTATAT MSAN_INTERCEPT_FUNC(fstatat)
708 #else
709 # define MSAN_MAYBE_INTERCEPT_FSTATAT
710 #endif
711
712 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,__fxstatat,int magic,int fd,char * pathname,void * buf,int flags)713 INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
714 int flags) {
715 ENSURE_MSAN_INITED();
716 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
717 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
718 return res;
719 }
720 # define MSAN_MAYBE_INTERCEPT___FXSTATAT MSAN_INTERCEPT_FUNC(__fxstatat)
721 #else
722 # define MSAN_MAYBE_INTERCEPT___FXSTATAT
723 #endif
724
725 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,__fxstatat64,int magic,int fd,char * pathname,void * buf,int flags)726 INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
727 int flags) {
728 ENSURE_MSAN_INITED();
729 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
730 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
731 return res;
732 }
733 # define MSAN_MAYBE_INTERCEPT___FXSTATAT64 MSAN_INTERCEPT_FUNC(__fxstatat64)
734 #else
735 # define MSAN_MAYBE_INTERCEPT___FXSTATAT64
736 #endif
737
INTERCEPTOR(int,pipe,int pipefd[2])738 INTERCEPTOR(int, pipe, int pipefd[2]) {
739 if (msan_init_is_running)
740 return REAL(pipe)(pipefd);
741 ENSURE_MSAN_INITED();
742 int res = REAL(pipe)(pipefd);
743 if (!res)
744 __msan_unpoison(pipefd, sizeof(int[2]));
745 return res;
746 }
747
INTERCEPTOR(int,pipe2,int pipefd[2],int flags)748 INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
749 ENSURE_MSAN_INITED();
750 int res = REAL(pipe2)(pipefd, flags);
751 if (!res)
752 __msan_unpoison(pipefd, sizeof(int[2]));
753 return res;
754 }
755
INTERCEPTOR(int,socketpair,int domain,int type,int protocol,int sv[2])756 INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
757 ENSURE_MSAN_INITED();
758 int res = REAL(socketpair)(domain, type, protocol, sv);
759 if (!res)
760 __msan_unpoison(sv, sizeof(int[2]));
761 return res;
762 }
763
764 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(char *,fgets_unlocked,char * s,int size,void * stream)765 INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
766 ENSURE_MSAN_INITED();
767 char *res = REAL(fgets_unlocked)(s, size, stream);
768 if (res)
769 __msan_unpoison(s, REAL(strlen)(s) + 1);
770 return res;
771 }
772 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED INTERCEPT_FUNCTION(fgets_unlocked)
773 #else
774 #define MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED
775 #endif
776
777 #define INTERCEPTOR_GETRLIMIT_BODY(func, resource, rlim) \
778 if (msan_init_is_running) \
779 return REAL(getrlimit)(resource, rlim); \
780 ENSURE_MSAN_INITED(); \
781 int res = REAL(func)(resource, rlim); \
782 if (!res) \
783 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz); \
784 return res
785
INTERCEPTOR(int,getrlimit,int resource,void * rlim)786 INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
787 INTERCEPTOR_GETRLIMIT_BODY(getrlimit, resource, rlim);
788 }
789
790 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,__getrlimit,int resource,void * rlim)791 INTERCEPTOR(int, __getrlimit, int resource, void *rlim) {
792 INTERCEPTOR_GETRLIMIT_BODY(__getrlimit, resource, rlim);
793 }
794
INTERCEPTOR(int,getrlimit64,int resource,void * rlim)795 INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
796 if (msan_init_is_running) return REAL(getrlimit64)(resource, rlim);
797 ENSURE_MSAN_INITED();
798 int res = REAL(getrlimit64)(resource, rlim);
799 if (!res) __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
800 return res;
801 }
802
INTERCEPTOR(int,prlimit,int pid,int resource,void * new_rlimit,void * old_rlimit)803 INTERCEPTOR(int, prlimit, int pid, int resource, void *new_rlimit,
804 void *old_rlimit) {
805 if (msan_init_is_running)
806 return REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
807 ENSURE_MSAN_INITED();
808 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit_sz);
809 int res = REAL(prlimit)(pid, resource, new_rlimit, old_rlimit);
810 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit_sz);
811 return res;
812 }
813
INTERCEPTOR(int,prlimit64,int pid,int resource,void * new_rlimit,void * old_rlimit)814 INTERCEPTOR(int, prlimit64, int pid, int resource, void *new_rlimit,
815 void *old_rlimit) {
816 if (msan_init_is_running)
817 return REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
818 ENSURE_MSAN_INITED();
819 CHECK_UNPOISONED(new_rlimit, __sanitizer::struct_rlimit64_sz);
820 int res = REAL(prlimit64)(pid, resource, new_rlimit, old_rlimit);
821 if (!res) __msan_unpoison(old_rlimit, __sanitizer::struct_rlimit64_sz);
822 return res;
823 }
824
825 #define MSAN_MAYBE_INTERCEPT___GETRLIMIT INTERCEPT_FUNCTION(__getrlimit)
826 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64 INTERCEPT_FUNCTION(getrlimit64)
827 #define MSAN_MAYBE_INTERCEPT_PRLIMIT INTERCEPT_FUNCTION(prlimit)
828 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64 INTERCEPT_FUNCTION(prlimit64)
829 #else
830 #define MSAN_MAYBE_INTERCEPT___GETRLIMIT
831 #define MSAN_MAYBE_INTERCEPT_GETRLIMIT64
832 #define MSAN_MAYBE_INTERCEPT_PRLIMIT
833 #define MSAN_MAYBE_INTERCEPT_PRLIMIT64
834 #endif
835
INTERCEPTOR(int,gethostname,char * name,SIZE_T len)836 INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
837 ENSURE_MSAN_INITED();
838 int res = REAL(gethostname)(name, len);
839 if (!res || (res == -1 && errno == errno_ENAMETOOLONG)) {
840 SIZE_T real_len = REAL(strnlen)(name, len);
841 if (real_len < len)
842 ++real_len;
843 __msan_unpoison(name, real_len);
844 }
845 return res;
846 }
847
848 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,epoll_wait,int epfd,void * events,int maxevents,int timeout)849 INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
850 int timeout) {
851 ENSURE_MSAN_INITED();
852 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
853 if (res > 0) {
854 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
855 }
856 return res;
857 }
858 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
859 #else
860 #define MSAN_MAYBE_INTERCEPT_EPOLL_WAIT
861 #endif
862
863 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
INTERCEPTOR(int,epoll_pwait,int epfd,void * events,int maxevents,int timeout,void * sigmask)864 INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
865 int timeout, void *sigmask) {
866 ENSURE_MSAN_INITED();
867 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
868 if (res > 0) {
869 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
870 }
871 return res;
872 }
873 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
874 #else
875 #define MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
876 #endif
877
INTERCEPTOR(void *,calloc,SIZE_T nmemb,SIZE_T size)878 INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
879 GET_MALLOC_STACK_TRACE;
880 if (UNLIKELY(!msan_inited))
881 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
882 return AllocateFromLocalPool(nmemb * size);
883 return msan_calloc(nmemb, size, &stack);
884 }
885
INTERCEPTOR(void *,realloc,void * ptr,SIZE_T size)886 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
887 GET_MALLOC_STACK_TRACE;
888 if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
889 uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
890 uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
891 void *new_ptr;
892 if (UNLIKELY(!msan_inited)) {
893 new_ptr = AllocateFromLocalPool(copy_size);
894 } else {
895 copy_size = size;
896 new_ptr = msan_malloc(copy_size, &stack);
897 }
898 internal_memcpy(new_ptr, ptr, copy_size);
899 return new_ptr;
900 }
901 return msan_realloc(ptr, size, &stack);
902 }
903
INTERCEPTOR(void *,reallocarray,void * ptr,SIZE_T nmemb,SIZE_T size)904 INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) {
905 GET_MALLOC_STACK_TRACE;
906 return msan_reallocarray(ptr, nmemb, size, &stack);
907 }
908
INTERCEPTOR(void *,malloc,SIZE_T size)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);
915 }
916
__msan_allocated_memory(const void * data,uptr size)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);
922 }
923 }
924
__msan_copy_shadow(void * dest,const void * src,uptr n)925 void __msan_copy_shadow(void *dest, const void *src, uptr n) {
926 GET_STORE_STACK_TRACE;
927 MoveShadowAndOrigin(dest, src, n, &stack);
928 }
929
__sanitizer_dtor_callback(const void * data,uptr size)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);
935 }
936 }
937
938 template <class Mmap>
mmap_interceptor(Mmap real_mmap,void * addr,SIZE_T length,int prot,int flags,int fd,OFF64_T offset)939 static void *mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length,
940 int prot, int flags, int fd, OFF64_T offset) {
941 SIZE_T rounded_length = RoundUpTo(length, GetPageSize());
942 void *end_addr = (char *)addr + (rounded_length - 1);
943 if (addr && (!MEM_IS_APP(addr) || !MEM_IS_APP(end_addr))) {
944 if (flags & map_fixed) {
945 errno = errno_EINVAL;
946 return (void *)-1;
947 } else {
948 addr = nullptr;
949 }
950 }
951 void *res = real_mmap(addr, length, prot, flags, fd, offset);
952 if (res != (void *)-1) {
953 void *end_res = (char *)res + (rounded_length - 1);
954 if (MEM_IS_APP(res) && MEM_IS_APP(end_res)) {
955 __msan_unpoison(res, rounded_length);
956 } else {
957 // Application has attempted to map more memory than is supported by
958 // MSAN. Act as if we ran out of memory.
959 internal_munmap(res, length);
960 errno = errno_ENOMEM;
961 return (void *)-1;
962 }
963 }
964 return res;
965 }
966
INTERCEPTOR(int,getrusage,int who,void * usage)967 INTERCEPTOR(int, getrusage, int who, void *usage) {
968 ENSURE_MSAN_INITED();
969 int res = REAL(getrusage)(who, usage);
970 if (res == 0) {
971 __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
972 }
973 return res;
974 }
975
976 class SignalHandlerScope {
977 public:
SignalHandlerScope()978 SignalHandlerScope() {
979 if (MsanThread *t = GetCurrentThread())
980 t->EnterSignalHandler();
981 }
~SignalHandlerScope()982 ~SignalHandlerScope() {
983 if (MsanThread *t = GetCurrentThread())
984 t->LeaveSignalHandler();
985 }
986 };
987
988 // sigactions_mu guarantees atomicity of sigaction() and signal() calls.
989 // Access to sigactions[] is gone with relaxed atomics to avoid data race with
990 // the signal handler.
991 const int kMaxSignals = 1024;
992 static atomic_uintptr_t sigactions[kMaxSignals];
993 static StaticSpinMutex sigactions_mu;
994
SignalHandler(int signo)995 static void SignalHandler(int signo) {
996 SignalHandlerScope signal_handler_scope;
997 ScopedThreadLocalStateBackup stlsb;
998 UnpoisonParam(1);
999
1000 typedef void (*signal_cb)(int x);
1001 signal_cb cb =
1002 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1003 cb(signo);
1004 }
1005
SignalAction(int signo,void * si,void * uc)1006 static void SignalAction(int signo, void *si, void *uc) {
1007 SignalHandlerScope signal_handler_scope;
1008 ScopedThreadLocalStateBackup stlsb;
1009 UnpoisonParam(3);
1010 __msan_unpoison(si, sizeof(__sanitizer_sigaction));
1011 __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
1012
1013 typedef void (*sigaction_cb)(int, void *, void *);
1014 sigaction_cb cb =
1015 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
1016 cb(signo, si, uc);
1017 }
1018
read_sigaction(const __sanitizer_sigaction * act)1019 static void read_sigaction(const __sanitizer_sigaction *act) {
1020 CHECK_UNPOISONED(&act->sa_flags, sizeof(act->sa_flags));
1021 if (act->sa_flags & __sanitizer::sa_siginfo)
1022 CHECK_UNPOISONED(&act->sigaction, sizeof(act->sigaction));
1023 else
1024 CHECK_UNPOISONED(&act->handler, sizeof(act->handler));
1025 CHECK_UNPOISONED(&act->sa_mask, sizeof(act->sa_mask));
1026 }
1027
1028 extern "C" int pthread_attr_init(void *attr);
1029 extern "C" int pthread_attr_destroy(void *attr);
1030
MsanThreadStartFunc(void * arg)1031 static void *MsanThreadStartFunc(void *arg) {
1032 MsanThread *t = (MsanThread *)arg;
1033 SetCurrentThread(t);
1034 return t->ThreadStart();
1035 }
1036
INTERCEPTOR(int,pthread_create,void * th,void * attr,void * (* callback)(void *),void * param)1037 INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1038 void * param) {
1039 ENSURE_MSAN_INITED(); // for GetTlsSize()
1040 __sanitizer_pthread_attr_t myattr;
1041 if (!attr) {
1042 pthread_attr_init(&myattr);
1043 attr = &myattr;
1044 }
1045
1046 AdjustStackSize(attr);
1047
1048 MsanThread *t = MsanThread::Create(callback, param);
1049
1050 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, t);
1051
1052 if (attr == &myattr)
1053 pthread_attr_destroy(&myattr);
1054 if (!res) {
1055 __msan_unpoison(th, __sanitizer::pthread_t_sz);
1056 }
1057 return res;
1058 }
1059
INTERCEPTOR(int,pthread_key_create,__sanitizer_pthread_key_t * key,void (* dtor)(void * value))1060 INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1061 void (*dtor)(void *value)) {
1062 if (msan_init_is_running) return REAL(pthread_key_create)(key, dtor);
1063 ENSURE_MSAN_INITED();
1064 int res = REAL(pthread_key_create)(key, dtor);
1065 if (!res && key)
1066 __msan_unpoison(key, sizeof(*key));
1067 return res;
1068 }
1069
1070 #if SANITIZER_NETBSD
1071 INTERCEPTOR(int, __libc_thr_keycreate, __sanitizer_pthread_key_t *m,
1072 void (*dtor)(void *value))
1073 ALIAS(WRAPPER_NAME(pthread_key_create));
1074 #endif
1075
INTERCEPTOR(int,pthread_join,void * th,void ** retval)1076 INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1077 ENSURE_MSAN_INITED();
1078 int res = REAL(pthread_join)(th, retval);
1079 if (!res && retval)
1080 __msan_unpoison(retval, sizeof(*retval));
1081 return res;
1082 }
1083
1084 extern char *tzname[2];
1085
INTERCEPTOR(void,tzset,int fake)1086 INTERCEPTOR(void, tzset, int fake) {
1087 ENSURE_MSAN_INITED();
1088 InterceptorScope interceptor_scope;
1089 REAL(tzset)(fake);
1090 if (tzname[0])
1091 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1);
1092 if (tzname[1])
1093 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1);
1094 return;
1095 }
1096
1097 struct MSanAtExitRecord {
1098 void (*func)(void *arg);
1099 void *arg;
1100 };
1101
1102 struct InterceptorContext {
1103 BlockingMutex atexit_mu;
1104 Vector<struct MSanAtExitRecord *> AtExitStack;
1105
InterceptorContextInterceptorContext1106 InterceptorContext()
1107 : AtExitStack() {
1108 }
1109 };
1110
1111 static ALIGNED(64) char interceptor_placeholder[sizeof(InterceptorContext)];
interceptor_ctx()1112 InterceptorContext *interceptor_ctx() {
1113 return reinterpret_cast<InterceptorContext*>(&interceptor_placeholder[0]);
1114 }
1115
MSanAtExitWrapper()1116 void MSanAtExitWrapper() {
1117 MSanAtExitRecord *r;
1118 {
1119 BlockingMutexLock l(&interceptor_ctx()->atexit_mu);
1120
1121 uptr element = interceptor_ctx()->AtExitStack.Size() - 1;
1122 r = interceptor_ctx()->AtExitStack[element];
1123 interceptor_ctx()->AtExitStack.PopBack();
1124 }
1125
1126 UnpoisonParam(1);
1127 ((void(*)())r->func)();
1128 InternalFree(r);
1129 }
1130
MSanCxaAtExitWrapper(void * arg)1131 void MSanCxaAtExitWrapper(void *arg) {
1132 UnpoisonParam(1);
1133 MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
1134 // libc before 2.27 had race which caused occasional double handler execution
1135 // https://sourceware.org/ml/libc-alpha/2017-08/msg01204.html
1136 if (!r->func)
1137 return;
1138 r->func(r->arg);
1139 r->func = nullptr;
1140 }
1141
1142 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso);
1143
1144 // Unpoison argument shadow for C++ module destructors.
INTERCEPTOR(int,__cxa_atexit,void (* func)(void *),void * arg,void * dso_handle)1145 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
1146 void *dso_handle) {
1147 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
1148 return setup_at_exit_wrapper((void(*)())func, arg, dso_handle);
1149 }
1150
1151 // Unpoison argument shadow for C++ module destructors.
INTERCEPTOR(int,atexit,void (* func)())1152 INTERCEPTOR(int, atexit, void (*func)()) {
1153 // Avoid calling real atexit as it is unrechable on at least on Linux.
1154 if (msan_init_is_running)
1155 return REAL(__cxa_atexit)((void (*)(void *a))func, 0, 0);
1156 return setup_at_exit_wrapper((void(*)())func, 0, 0);
1157 }
1158
setup_at_exit_wrapper(void (* f)(),void * arg,void * dso)1159 static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso) {
1160 ENSURE_MSAN_INITED();
1161 MSanAtExitRecord *r =
1162 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
1163 r->func = (void(*)(void *a))f;
1164 r->arg = arg;
1165 int res;
1166 if (!dso) {
1167 // NetBSD does not preserve the 2nd argument if dso is equal to 0
1168 // Store ctx in a local stack-like structure
1169
1170 BlockingMutexLock l(&interceptor_ctx()->atexit_mu);
1171
1172 res = REAL(__cxa_atexit)((void (*)(void *a))MSanAtExitWrapper, 0, 0);
1173 if (!res) {
1174 interceptor_ctx()->AtExitStack.PushBack(r);
1175 }
1176 } else {
1177 res = REAL(__cxa_atexit)(MSanCxaAtExitWrapper, r, dso);
1178 }
1179 return res;
1180 }
1181
BeforeFork()1182 static void BeforeFork() {
1183 StackDepotLockAll();
1184 ChainedOriginDepotLockAll();
1185 }
1186
AfterFork()1187 static void AfterFork() {
1188 ChainedOriginDepotUnlockAll();
1189 StackDepotUnlockAll();
1190 }
1191
INTERCEPTOR(int,fork,void)1192 INTERCEPTOR(int, fork, void) {
1193 ENSURE_MSAN_INITED();
1194 BeforeFork();
1195 int pid = REAL(fork)();
1196 AfterFork();
1197 return pid;
1198 }
1199
1200 // NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly
1201 // with MSan.
1202 #if SANITIZER_LINUX
INTERCEPTOR(int,openpty,int * aparent,int * aworker,char * name,const void * termp,const void * winp)1203 INTERCEPTOR(int, openpty, int *aparent, int *aworker, char *name,
1204 const void *termp, const void *winp) {
1205 ENSURE_MSAN_INITED();
1206 InterceptorScope interceptor_scope;
1207 int res = REAL(openpty)(aparent, aworker, name, termp, winp);
1208 if (!res) {
1209 __msan_unpoison(aparent, sizeof(*aparent));
1210 __msan_unpoison(aworker, sizeof(*aworker));
1211 }
1212 return res;
1213 }
1214 #define MSAN_MAYBE_INTERCEPT_OPENPTY INTERCEPT_FUNCTION(openpty)
1215 #else
1216 #define MSAN_MAYBE_INTERCEPT_OPENPTY
1217 #endif
1218
1219 // NetBSD ships with forkpty(3) in -lutil, that needs to be prebuilt explicitly
1220 // with MSan.
1221 #if SANITIZER_LINUX
INTERCEPTOR(int,forkpty,int * aparent,char * name,const void * termp,const void * winp)1222 INTERCEPTOR(int, forkpty, int *aparent, char *name, const void *termp,
1223 const void *winp) {
1224 ENSURE_MSAN_INITED();
1225 InterceptorScope interceptor_scope;
1226 int res = REAL(forkpty)(aparent, name, termp, winp);
1227 if (res != -1)
1228 __msan_unpoison(aparent, sizeof(*aparent));
1229 return res;
1230 }
1231 #define MSAN_MAYBE_INTERCEPT_FORKPTY INTERCEPT_FUNCTION(forkpty)
1232 #else
1233 #define MSAN_MAYBE_INTERCEPT_FORKPTY
1234 #endif
1235
1236 struct MSanInterceptorContext {
1237 bool in_interceptor_scope;
1238 };
1239
1240 namespace __msan {
1241
OnExit()1242 int OnExit() {
1243 // FIXME: ask frontend whether we need to return failure.
1244 return 0;
1245 }
1246
1247 } // namespace __msan
1248
1249 // A version of CHECK_UNPOISONED using a saved scope value. Used in common
1250 // interceptors.
1251 #define CHECK_UNPOISONED_CTX(ctx, x, n) \
1252 do { \
1253 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1254 CHECK_UNPOISONED_0(x, n); \
1255 } while (0)
1256
1257 #define MSAN_INTERCEPT_FUNC(name) \
1258 do { \
1259 if (!INTERCEPT_FUNCTION(name)) \
1260 VReport(1, "MemorySanitizer: failed to intercept '%s'\n", #name); \
1261 } while (0)
1262
1263 #define MSAN_INTERCEPT_FUNC_VER(name, ver) \
1264 do { \
1265 if (!INTERCEPT_FUNCTION_VER(name, ver)) \
1266 VReport(1, "MemorySanitizer: failed to intercept '%s@@%s'\n", #name, \
1267 #ver); \
1268 } while (0)
1269 #define MSAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \
1270 do { \
1271 if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \
1272 VReport(1, "MemorySanitizer: failed to intercept '%s@@%s' or '%s'\n", \
1273 #name, #ver, #name); \
1274 } while (0)
1275
1276 #define COMMON_INTERCEPT_FUNCTION(name) MSAN_INTERCEPT_FUNC(name)
1277 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
1278 MSAN_INTERCEPT_FUNC_VER(name, ver)
1279 #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \
1280 MSAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver)
1281 #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) \
1282 UnpoisonParam(count)
1283 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
1284 __msan_unpoison(ptr, size)
1285 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
1286 CHECK_UNPOISONED_CTX(ctx, ptr, size)
1287 #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
1288 __msan_unpoison(ptr, size)
1289 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
1290 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \
1291 ENSURE_MSAN_INITED(); \
1292 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1293 ctx = (void *)&msan_ctx; \
1294 (void)ctx; \
1295 InterceptorScope interceptor_scope; \
1296 __msan_unpoison(__errno_location(), sizeof(int)); /* NOLINT */
1297 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
1298 do { \
1299 } while (false)
1300 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1301 do { \
1302 } while (false)
1303 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1304 do { \
1305 } while (false)
1306 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1307 do { \
1308 } while (false)
1309 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
1310 do { \
1311 } while (false) // FIXME
1312 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
1313 do { \
1314 } while (false) // FIXME
1315 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
1316 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
1317 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
1318 do { \
1319 link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE((handle)); \
1320 if (filename && map) \
1321 ForEachMappedRegion(map, __msan_unpoison); \
1322 } while (false)
1323
1324 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!msan_inited)
1325
1326 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
1327 if (MsanThread *t = GetCurrentThread()) { \
1328 *begin = t->tls_begin(); \
1329 *end = t->tls_end(); \
1330 } else { \
1331 *begin = *end = 0; \
1332 }
1333
1334 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
1335 { \
1336 (void)ctx; \
1337 return __msan_memset(block, c, size); \
1338 }
1339 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
1340 { \
1341 (void)ctx; \
1342 return __msan_memmove(to, from, size); \
1343 }
1344 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
1345 { \
1346 (void)ctx; \
1347 return __msan_memcpy(to, from, size); \
1348 }
1349
1350 #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
1351 do { \
1352 GET_STORE_STACK_TRACE; \
1353 CopyShadowAndOrigin(to, from, size, &stack); \
1354 __msan_unpoison(to + size, 1); \
1355 } while (false)
1356
1357 #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, fd, \
1358 offset) \
1359 do { \
1360 return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \
1361 } while (false)
1362
1363 #include "sanitizer_common/sanitizer_platform_interceptors.h"
1364 #include "sanitizer_common/sanitizer_common_interceptors.inc"
1365
1366 static uptr signal_impl(int signo, uptr cb);
1367 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1368 __sanitizer_sigaction *oldact);
1369
1370 #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signo, act, oldact) \
1371 { return sigaction_impl(signo, act, oldact); }
1372
1373 #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signo, handler) \
1374 { \
1375 handler = signal_impl(signo, handler); \
1376 InterceptorScope interceptor_scope; \
1377 return REAL(func)(signo, handler); \
1378 }
1379
1380 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
1381
sigaction_impl(int signo,const __sanitizer_sigaction * act,__sanitizer_sigaction * oldact)1382 static int sigaction_impl(int signo, const __sanitizer_sigaction *act,
1383 __sanitizer_sigaction *oldact) {
1384 ENSURE_MSAN_INITED();
1385 if (signo <= 0 || signo >= kMaxSignals) {
1386 errno = errno_EINVAL;
1387 return -1;
1388 }
1389 if (act) read_sigaction(act);
1390 int res;
1391 if (flags()->wrap_signals) {
1392 SpinMutexLock lock(&sigactions_mu);
1393 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
1394 __sanitizer_sigaction new_act;
1395 __sanitizer_sigaction *pnew_act = act ? &new_act : nullptr;
1396 if (act) {
1397 REAL(memcpy)(pnew_act, act, sizeof(__sanitizer_sigaction));
1398 uptr cb = (uptr)pnew_act->sigaction;
1399 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
1400 ? (uptr)SignalAction
1401 : (uptr)SignalHandler;
1402 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1403 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1404 pnew_act->sigaction = (decltype(pnew_act->sigaction))new_cb;
1405 }
1406 }
1407 res = REAL(SIGACTION_SYMNAME)(signo, pnew_act, oldact);
1408 if (res == 0 && oldact) {
1409 uptr cb = (uptr)oldact->sigaction;
1410 if (cb == (uptr)SignalAction || cb == (uptr)SignalHandler) {
1411 oldact->sigaction = (decltype(oldact->sigaction))old_cb;
1412 }
1413 }
1414 } else {
1415 res = REAL(SIGACTION_SYMNAME)(signo, act, oldact);
1416 }
1417
1418 if (res == 0 && oldact) {
1419 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
1420 }
1421 return res;
1422 }
1423
signal_impl(int signo,uptr cb)1424 static uptr signal_impl(int signo, uptr cb) {
1425 ENSURE_MSAN_INITED();
1426 if (signo <= 0 || signo >= kMaxSignals) {
1427 errno = errno_EINVAL;
1428 return -1;
1429 }
1430 if (flags()->wrap_signals) {
1431 SpinMutexLock lock(&sigactions_mu);
1432 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
1433 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
1434 cb = (uptr)&SignalHandler;
1435 }
1436 }
1437 return cb;
1438 }
1439
1440 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
1441 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
1442 do { \
1443 } while (false)
1444 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
1445 do { \
1446 } while (false)
1447 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1448 #include "sanitizer_common/sanitizer_common_syscalls.inc"
1449 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
1450
1451 struct dlinfo {
1452 char *dli_fname;
1453 void *dli_fbase;
1454 char *dli_sname;
1455 void *dli_saddr;
1456 };
1457
INTERCEPTOR(int,dladdr,void * addr,dlinfo * info)1458 INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
1459 void *ctx;
1460 COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info);
1461 int res = REAL(dladdr)(addr, info);
1462 if (res != 0) {
1463 __msan_unpoison(info, sizeof(*info));
1464 if (info->dli_fname)
1465 __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
1466 if (info->dli_sname)
1467 __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
1468 }
1469 return res;
1470 }
1471
INTERCEPTOR(char *,dlerror,int fake)1472 INTERCEPTOR(char *, dlerror, int fake) {
1473 void *ctx;
1474 COMMON_INTERCEPTOR_ENTER(ctx, dlerror, fake);
1475 char *res = REAL(dlerror)(fake);
1476 if (res) __msan_unpoison(res, REAL(strlen)(res) + 1);
1477 return res;
1478 }
1479
1480 typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
1481 void *data);
1482 struct dl_iterate_phdr_data {
1483 dl_iterate_phdr_cb callback;
1484 void *data;
1485 };
1486
msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info * info,SIZE_T size,void * data)1487 static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
1488 void *data) {
1489 if (info) {
1490 __msan_unpoison(info, size);
1491 if (info->dlpi_phdr && info->dlpi_phnum)
1492 __msan_unpoison(info->dlpi_phdr, struct_ElfW_Phdr_sz * info->dlpi_phnum);
1493 if (info->dlpi_name)
1494 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
1495 }
1496 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
1497 UnpoisonParam(3);
1498 return cbdata->callback(info, size, cbdata->data);
1499 }
1500
INTERCEPTOR(void *,shmat,int shmid,const void * shmaddr,int shmflg)1501 INTERCEPTOR(void *, shmat, int shmid, const void *shmaddr, int shmflg) {
1502 ENSURE_MSAN_INITED();
1503 void *p = REAL(shmat)(shmid, shmaddr, shmflg);
1504 if (p != (void *)-1) {
1505 __sanitizer_shmid_ds ds;
1506 int res = REAL(shmctl)(shmid, shmctl_ipc_stat, &ds);
1507 if (!res) {
1508 __msan_unpoison(p, ds.shm_segsz);
1509 }
1510 }
1511 return p;
1512 }
1513
INTERCEPTOR(int,dl_iterate_phdr,dl_iterate_phdr_cb callback,void * data)1514 INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
1515 void *ctx;
1516 COMMON_INTERCEPTOR_ENTER(ctx, dl_iterate_phdr, callback, data);
1517 dl_iterate_phdr_data cbdata;
1518 cbdata.callback = callback;
1519 cbdata.data = data;
1520 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
1521 return res;
1522 }
1523
1524 // wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
INTERCEPTOR(wchar_t *,wcschr,void * s,wchar_t wc,void * ps)1525 INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
1526 ENSURE_MSAN_INITED();
1527 wchar_t *res = REAL(wcschr)(s, wc, ps);
1528 return res;
1529 }
1530
1531 // wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
INTERCEPTOR(wchar_t *,wcscpy,wchar_t * dest,const wchar_t * src)1532 INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
1533 ENSURE_MSAN_INITED();
1534 GET_STORE_STACK_TRACE;
1535 wchar_t *res = REAL(wcscpy)(dest, src);
1536 CopyShadowAndOrigin(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1),
1537 &stack);
1538 return res;
1539 }
1540
INTERCEPTOR(wchar_t *,wcsncpy,wchar_t * dest,const wchar_t * src,SIZE_T n)1541 INTERCEPTOR(wchar_t *, wcsncpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
1542 ENSURE_MSAN_INITED();
1543 GET_STORE_STACK_TRACE;
1544 SIZE_T copy_size = REAL(wcsnlen)(src, n);
1545 if (copy_size < n) copy_size++; // trailing \0
1546 wchar_t *res = REAL(wcsncpy)(dest, src, n);
1547 CopyShadowAndOrigin(dest, src, copy_size * sizeof(wchar_t), &stack);
1548 __msan_unpoison(dest + copy_size, (n - copy_size) * sizeof(wchar_t));
1549 return res;
1550 }
1551
1552 // These interface functions reside here so that they can use
1553 // REAL(memset), etc.
__msan_unpoison(const void * a,uptr size)1554 void __msan_unpoison(const void *a, uptr size) {
1555 if (!MEM_IS_APP(a)) return;
1556 SetShadow(a, size, 0);
1557 }
1558
__msan_poison(const void * a,uptr size)1559 void __msan_poison(const void *a, uptr size) {
1560 if (!MEM_IS_APP(a)) return;
1561 SetShadow(a, size, __msan::flags()->poison_heap_with_zeroes ? 0 : -1);
1562 }
1563
__msan_poison_stack(void * a,uptr size)1564 void __msan_poison_stack(void *a, uptr size) {
1565 if (!MEM_IS_APP(a)) return;
1566 SetShadow(a, size, __msan::flags()->poison_stack_with_zeroes ? 0 : -1);
1567 }
1568
__msan_unpoison_param(uptr n)1569 void __msan_unpoison_param(uptr n) { UnpoisonParam(n); }
1570
__msan_clear_and_unpoison(void * a,uptr size)1571 void __msan_clear_and_unpoison(void *a, uptr size) {
1572 REAL(memset)(a, 0, size);
1573 SetShadow(a, size, 0);
1574 }
1575
__msan_memcpy(void * dest,const void * src,SIZE_T n)1576 void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
1577 if (!msan_inited) return internal_memcpy(dest, src, n);
1578 if (msan_init_is_running || __msan::IsInSymbolizer())
1579 return REAL(memcpy)(dest, src, n);
1580 ENSURE_MSAN_INITED();
1581 GET_STORE_STACK_TRACE;
1582 void *res = REAL(memcpy)(dest, src, n);
1583 CopyShadowAndOrigin(dest, src, n, &stack);
1584 return res;
1585 }
1586
__msan_memset(void * s,int c,SIZE_T n)1587 void *__msan_memset(void *s, int c, SIZE_T n) {
1588 if (!msan_inited) return internal_memset(s, c, n);
1589 if (msan_init_is_running) return REAL(memset)(s, c, n);
1590 ENSURE_MSAN_INITED();
1591 void *res = REAL(memset)(s, c, n);
1592 __msan_unpoison(s, n);
1593 return res;
1594 }
1595
__msan_memmove(void * dest,const void * src,SIZE_T n)1596 void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
1597 if (!msan_inited) return internal_memmove(dest, src, n);
1598 if (msan_init_is_running) return REAL(memmove)(dest, src, n);
1599 ENSURE_MSAN_INITED();
1600 GET_STORE_STACK_TRACE;
1601 void *res = REAL(memmove)(dest, src, n);
1602 MoveShadowAndOrigin(dest, src, n, &stack);
1603 return res;
1604 }
1605
__msan_unpoison_string(const char * s)1606 void __msan_unpoison_string(const char* s) {
1607 if (!MEM_IS_APP(s)) return;
1608 __msan_unpoison(s, REAL(strlen)(s) + 1);
1609 }
1610
1611 namespace __msan {
1612
InitializeInterceptors()1613 void InitializeInterceptors() {
1614 static int inited = 0;
1615 CHECK_EQ(inited, 0);
1616
1617 new(interceptor_ctx()) InterceptorContext();
1618
1619 InitializeCommonInterceptors();
1620 InitializeSignalInterceptors();
1621
1622 INTERCEPT_FUNCTION(posix_memalign);
1623 MSAN_MAYBE_INTERCEPT_MEMALIGN;
1624 MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
1625 INTERCEPT_FUNCTION(valloc);
1626 MSAN_MAYBE_INTERCEPT_PVALLOC;
1627 INTERCEPT_FUNCTION(malloc);
1628 INTERCEPT_FUNCTION(calloc);
1629 INTERCEPT_FUNCTION(realloc);
1630 INTERCEPT_FUNCTION(reallocarray);
1631 INTERCEPT_FUNCTION(free);
1632 MSAN_MAYBE_INTERCEPT_CFREE;
1633 MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
1634 MSAN_MAYBE_INTERCEPT_MALLINFO;
1635 MSAN_MAYBE_INTERCEPT_MALLOPT;
1636 MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
1637 INTERCEPT_FUNCTION(fread);
1638 MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED;
1639 INTERCEPT_FUNCTION(memccpy);
1640 MSAN_MAYBE_INTERCEPT_MEMPCPY;
1641 INTERCEPT_FUNCTION(bcopy);
1642 INTERCEPT_FUNCTION(wmemset);
1643 INTERCEPT_FUNCTION(wmemcpy);
1644 MSAN_MAYBE_INTERCEPT_WMEMPCPY;
1645 INTERCEPT_FUNCTION(wmemmove);
1646 INTERCEPT_FUNCTION(strcpy);
1647 MSAN_MAYBE_INTERCEPT_STPCPY;
1648 INTERCEPT_FUNCTION(strdup);
1649 MSAN_MAYBE_INTERCEPT___STRDUP;
1650 INTERCEPT_FUNCTION(strncpy);
1651 MSAN_MAYBE_INTERCEPT_GCVT;
1652 INTERCEPT_FUNCTION(strcat);
1653 INTERCEPT_FUNCTION(strncat);
1654 INTERCEPT_STRTO(strtod);
1655 INTERCEPT_STRTO(strtof);
1656 INTERCEPT_STRTO(strtold);
1657 INTERCEPT_STRTO(strtol);
1658 INTERCEPT_STRTO(strtoul);
1659 INTERCEPT_STRTO(strtoll);
1660 INTERCEPT_STRTO(strtoull);
1661 INTERCEPT_STRTO(strtouq);
1662 INTERCEPT_STRTO(wcstod);
1663 INTERCEPT_STRTO(wcstof);
1664 INTERCEPT_STRTO(wcstold);
1665 INTERCEPT_STRTO(wcstol);
1666 INTERCEPT_STRTO(wcstoul);
1667 INTERCEPT_STRTO(wcstoll);
1668 INTERCEPT_STRTO(wcstoull);
1669 #ifdef SANITIZER_NLDBL_VERSION
1670 INTERCEPT_FUNCTION_VER(vswprintf, SANITIZER_NLDBL_VERSION);
1671 INTERCEPT_FUNCTION_VER(swprintf, SANITIZER_NLDBL_VERSION);
1672 #else
1673 INTERCEPT_FUNCTION(vswprintf);
1674 INTERCEPT_FUNCTION(swprintf);
1675 #endif
1676 INTERCEPT_FUNCTION(strftime);
1677 INTERCEPT_FUNCTION(strftime_l);
1678 MSAN_MAYBE_INTERCEPT___STRFTIME_L;
1679 INTERCEPT_FUNCTION(wcsftime);
1680 INTERCEPT_FUNCTION(wcsftime_l);
1681 MSAN_MAYBE_INTERCEPT___WCSFTIME_L;
1682 INTERCEPT_FUNCTION(mbtowc);
1683 INTERCEPT_FUNCTION(mbrtowc);
1684 INTERCEPT_FUNCTION(wcslen);
1685 INTERCEPT_FUNCTION(wcsnlen);
1686 INTERCEPT_FUNCTION(wcschr);
1687 INTERCEPT_FUNCTION(wcscpy);
1688 INTERCEPT_FUNCTION(wcsncpy);
1689 INTERCEPT_FUNCTION(wcscmp);
1690 INTERCEPT_FUNCTION(getenv);
1691 INTERCEPT_FUNCTION(setenv);
1692 INTERCEPT_FUNCTION(putenv);
1693 INTERCEPT_FUNCTION(gettimeofday);
1694 MSAN_MAYBE_INTERCEPT_FCVT;
1695 MSAN_MAYBE_INTERCEPT_FSTAT;
1696 MSAN_MAYBE_INTERCEPT___FXSTAT;
1697 MSAN_MAYBE_INTERCEPT_FSTATAT;
1698 MSAN_MAYBE_INTERCEPT___FXSTATAT;
1699 MSAN_MAYBE_INTERCEPT___FXSTAT64;
1700 MSAN_MAYBE_INTERCEPT___FXSTATAT64;
1701 INTERCEPT_FUNCTION(pipe);
1702 INTERCEPT_FUNCTION(pipe2);
1703 INTERCEPT_FUNCTION(socketpair);
1704 MSAN_MAYBE_INTERCEPT_FGETS_UNLOCKED;
1705 INTERCEPT_FUNCTION(getrlimit);
1706 MSAN_MAYBE_INTERCEPT___GETRLIMIT;
1707 MSAN_MAYBE_INTERCEPT_GETRLIMIT64;
1708 MSAN_MAYBE_INTERCEPT_PRLIMIT;
1709 MSAN_MAYBE_INTERCEPT_PRLIMIT64;
1710 INTERCEPT_FUNCTION(gethostname);
1711 MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1712 MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1713 INTERCEPT_FUNCTION(dladdr);
1714 INTERCEPT_FUNCTION(dlerror);
1715 INTERCEPT_FUNCTION(dl_iterate_phdr);
1716 INTERCEPT_FUNCTION(getrusage);
1717 #if defined(__mips__)
1718 INTERCEPT_FUNCTION_VER(pthread_create, "GLIBC_2.2");
1719 #else
1720 INTERCEPT_FUNCTION(pthread_create);
1721 #endif
1722 INTERCEPT_FUNCTION(pthread_key_create);
1723
1724 #if SANITIZER_NETBSD
1725 INTERCEPT_FUNCTION(__libc_thr_keycreate);
1726 #endif
1727
1728 INTERCEPT_FUNCTION(pthread_join);
1729 INTERCEPT_FUNCTION(tzset);
1730 INTERCEPT_FUNCTION(atexit);
1731 INTERCEPT_FUNCTION(__cxa_atexit);
1732 INTERCEPT_FUNCTION(shmat);
1733 INTERCEPT_FUNCTION(fork);
1734 MSAN_MAYBE_INTERCEPT_OPENPTY;
1735 MSAN_MAYBE_INTERCEPT_FORKPTY;
1736
1737 inited = 1;
1738 }
1739 } // namespace __msan
1740