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