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