blob: 849949d3c937ce1b2aa35d854fe9cf8e788e30ad [file] [log] [blame]
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001//===-- 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.
Kostya Serebryany8530e2b2012-12-12 09:54:35 +000013//
14// FIXME: move as many interceptors as possible into
15// sanitizer_common/sanitizer_common_interceptors.h
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000016//===----------------------------------------------------------------------===//
17
18#include "interception/interception.h"
19#include "msan.h"
Evgeniy Stepanov9358c582013-02-19 09:19:16 +000020#include "sanitizer_common/sanitizer_platform_limits_posix.h"
Kostya Serebryany65199f12013-01-25 11:46:22 +000021#include "sanitizer_common/sanitizer_allocator.h"
Evgeniy Stepanovcfc29de2013-09-27 11:32:21 +000022#include "sanitizer_common/sanitizer_allocator_internal.h"
Evgeniy Stepanove0a87122013-08-27 11:34:05 +000023#include "sanitizer_common/sanitizer_atomic.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000024#include "sanitizer_common/sanitizer_common.h"
Evgeniy Stepanov887a5fe2013-01-28 13:52:49 +000025#include "sanitizer_common/sanitizer_stackdepot.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000026#include "sanitizer_common/sanitizer_libc.h"
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +000027#include "sanitizer_common/sanitizer_linux.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000028
29#include <stdarg.h>
30// ACHTUNG! No other system header includes in this file.
31// Ideally, we should get rid of stdarg.h as well.
32
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000033using namespace __msan;
34
Evgeniy Stepanove0a87122013-08-27 11:34:05 +000035using __sanitizer::memory_order;
36using __sanitizer::atomic_load;
37using __sanitizer::atomic_store;
38using __sanitizer::atomic_uintptr_t;
39
Evgeniy Stepanov7c6bd402013-10-22 14:31:30 +000040static unsigned g_thread_finalize_key;
41
Evgeniy Stepanov447ef192013-05-22 12:50:26 +000042// True if this is a nested interceptor.
43static THREADLOCAL int in_interceptor_scope;
44
45struct InterceptorScope {
46 InterceptorScope() { ++in_interceptor_scope; }
47 ~InterceptorScope() { --in_interceptor_scope; }
48};
49
50bool IsInInterceptorScope() {
51 return in_interceptor_scope;
52}
53
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000054#define ENSURE_MSAN_INITED() do { \
Alexey Samsonov8028c712012-12-14 11:52:02 +000055 CHECK(!msan_init_is_running); \
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000056 if (!msan_inited) { \
57 __msan_init(); \
58 } \
59} while (0)
60
Evgeniy Stepanov447ef192013-05-22 12:50:26 +000061// Check that [x, x+n) range is unpoisoned.
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +000062#define CHECK_UNPOISONED_0(x, n) \
63 do { \
64 sptr offset = __msan_test_shadow(x, n); \
65 if (__msan::IsInSymbolizer()) break; \
66 if (offset >= 0 && __msan::flags()->report_umrs) { \
67 GET_CALLER_PC_BP_SP; \
68 (void) sp; \
69 Printf("UMR in %s at offset %d inside [%p, +%d) \n", __FUNCTION__, \
70 offset, x, n); \
71 __msan::PrintWarningWithOrigin(pc, bp, \
72 __msan_get_origin((char *)x + offset)); \
Dmitry Vyukovdbac0a42013-08-13 15:33:00 +000073 if (__msan::flags()->halt_on_error) { \
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +000074 Printf("Exiting\n"); \
75 Die(); \
76 } \
77 } \
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000078 } while (0)
79
Evgeniy Stepanov447ef192013-05-22 12:50:26 +000080// Check that [x, x+n) range is unpoisoned unless we are in a nested
81// interceptor.
Evgeniy Stepanov341b9e62013-06-28 11:02:43 +000082#define CHECK_UNPOISONED(x, n) \
83 do { \
84 if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \
85 } while (0);
Evgeniy Stepanov447ef192013-05-22 12:50:26 +000086
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000087static void *fast_memset(void *ptr, int c, SIZE_T n);
88static void *fast_memcpy(void *dst, const void *src, SIZE_T n);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000089
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000090INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000091 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000092 SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000093 if (res > 0)
94 __msan_unpoison(ptr, res *size);
95 return res;
96}
97
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000098INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb,
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000099 void *file) {
100 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000101 SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000102 if (res > 0)
103 __msan_unpoison(ptr, res *size);
104 return res;
105}
106
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000107INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000108 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000109 SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000110 if (res > 0)
111 __msan_unpoison(buf, res);
112 return res;
113}
114
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000115INTERCEPTOR(void *, memcpy, void *dest, const void *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000116 return __msan_memcpy(dest, src, n);
117}
118
Evgeniy Stepanov353c9962013-07-02 14:49:24 +0000119INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) {
120 return (char *)__msan_memcpy(dest, src, n) + n;
121}
122
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000123INTERCEPTOR(void *, memmove, void *dest, const void *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000124 return __msan_memmove(dest, src, n);
125}
126
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000127INTERCEPTOR(void *, memset, void *s, int c, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000128 return __msan_memset(s, c, n);
129}
130
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +0000131INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) {
132 return __msan_memmove(dest, src, n);
133}
134
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000135INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000136 GET_MALLOC_STACK_TRACE;
137 CHECK_EQ(alignment & (alignment - 1), 0);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000138 CHECK_NE(memptr, 0);
Evgeniy Stepanovb6c8e472013-04-23 13:27:36 +0000139 *memptr = MsanReallocate(&stack, 0, size, alignment, false);
140 CHECK_NE(*memptr, 0);
141 __msan_unpoison(memptr, sizeof(*memptr));
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000142 return 0;
143}
144
Evgeniy Stepanov97160a82013-09-02 09:24:53 +0000145INTERCEPTOR(void *, memalign, SIZE_T boundary, SIZE_T size) {
146 GET_MALLOC_STACK_TRACE;
147 CHECK_EQ(boundary & (boundary - 1), 0);
148 void *ptr = MsanReallocate(&stack, 0, size, boundary, false);
149 return ptr;
150}
151
152INTERCEPTOR(void *, valloc, SIZE_T size) {
153 GET_MALLOC_STACK_TRACE;
154 void *ptr = MsanReallocate(&stack, 0, size, GetPageSizeCached(), false);
155 return ptr;
156}
157
158INTERCEPTOR(void *, pvalloc, SIZE_T size) {
159 GET_MALLOC_STACK_TRACE;
160 uptr PageSize = GetPageSizeCached();
161 size = RoundUpTo(size, PageSize);
162 if (size == 0) {
163 // pvalloc(0) should allocate one page.
164 size = PageSize;
165 }
166 void *ptr = MsanReallocate(&stack, 0, size, PageSize, false);
167 return ptr;
168}
169
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000170INTERCEPTOR(void, free, void *ptr) {
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000171 GET_MALLOC_STACK_TRACE;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000172 if (ptr == 0) return;
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000173 MsanDeallocate(&stack, ptr);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000174}
175
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000176INTERCEPTOR(SIZE_T, strlen, const char *s) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000177 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000178 SIZE_T res = REAL(strlen)(s);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000179 CHECK_UNPOISONED(s, res + 1);
180 return res;
181}
182
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000183INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000184 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000185 SIZE_T res = REAL(strnlen)(s, n);
186 SIZE_T scan_size = (res == n) ? res : res + 1;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000187 CHECK_UNPOISONED(s, scan_size);
188 return res;
189}
190
191// FIXME: Add stricter shadow checks in str* interceptors (ex.: strcpy should
192// check the shadow of the terminating \0 byte).
193
194INTERCEPTOR(char *, strcpy, char *dest, const char *src) { // NOLINT
195 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000196 SIZE_T n = REAL(strlen)(src);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000197 char *res = REAL(strcpy)(dest, src); // NOLINT
198 __msan_copy_poison(dest, src, n + 1);
199 return res;
200}
201
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000202INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { // NOLINT
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000203 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000204 SIZE_T copy_size = REAL(strnlen)(src, n);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000205 if (copy_size < n)
206 copy_size++; // trailing \0
207 char *res = REAL(strncpy)(dest, src, n); // NOLINT
208 __msan_copy_poison(dest, src, copy_size);
209 return res;
210}
211
Evgeniy Stepanov4bbbe132013-07-01 15:19:37 +0000212INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { // NOLINT
213 ENSURE_MSAN_INITED();
214 SIZE_T n = REAL(strlen)(src);
215 char *res = REAL(stpcpy)(dest, src); // NOLINT
216 __msan_copy_poison(dest, src, n + 1);
217 return res;
218}
219
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000220INTERCEPTOR(char *, strdup, char *src) {
221 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000222 SIZE_T n = REAL(strlen)(src);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000223 char *res = REAL(strdup)(src);
224 __msan_copy_poison(res, src, n + 1);
225 return res;
226}
227
Evgeniy Stepanov8aa1ae02013-03-14 11:10:36 +0000228INTERCEPTOR(char *, __strdup, char *src) {
229 ENSURE_MSAN_INITED();
230 SIZE_T n = REAL(strlen)(src);
231 char *res = REAL(__strdup)(src);
232 __msan_copy_poison(res, src, n + 1);
233 return res;
234}
235
236INTERCEPTOR(char *, strndup, char *src, SIZE_T n) {
237 ENSURE_MSAN_INITED();
238 SIZE_T copy_size = REAL(strnlen)(src, n);
239 char *res = REAL(strndup)(src, n);
240 __msan_copy_poison(res, src, copy_size);
241 __msan_unpoison(res + copy_size, 1); // \0
242 return res;
243}
244
245INTERCEPTOR(char *, __strndup, char *src, SIZE_T n) {
246 ENSURE_MSAN_INITED();
247 SIZE_T copy_size = REAL(strnlen)(src, n);
248 char *res = REAL(__strndup)(src, n);
249 __msan_copy_poison(res, src, copy_size);
250 __msan_unpoison(res + copy_size, 1); // \0
251 return res;
252}
253
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000254INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000255 ENSURE_MSAN_INITED();
256 char *res = REAL(gcvt)(number, ndigit, buf);
257 // DynamoRio tool will take care of unpoisoning gcvt result for us.
258 if (!__msan_has_dynamic_component()) {
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000259 SIZE_T n = REAL(strlen)(buf);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000260 __msan_unpoison(buf, n + 1);
261 }
262 return res;
263}
264
265INTERCEPTOR(char *, strcat, char *dest, const char *src) { // NOLINT
266 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000267 SIZE_T src_size = REAL(strlen)(src);
268 SIZE_T dest_size = REAL(strlen)(dest);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000269 char *res = REAL(strcat)(dest, src); // NOLINT
270 __msan_copy_poison(dest + dest_size, src, src_size + 1);
271 return res;
272}
273
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000274INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { // NOLINT
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000275 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000276 SIZE_T dest_size = REAL(strlen)(dest);
277 SIZE_T copy_size = REAL(strlen)(src);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000278 if (copy_size < n)
279 copy_size++; // trailing \0
280 char *res = REAL(strncat)(dest, src, n); // NOLINT
281 __msan_copy_poison(dest + dest_size, src, copy_size);
282 return res;
283}
284
285INTERCEPTOR(long, strtol, const char *nptr, char **endptr, // NOLINT
286 int base) {
287 ENSURE_MSAN_INITED();
288 long res = REAL(strtol)(nptr, endptr, base); // NOLINT
289 if (!__msan_has_dynamic_component()) {
290 __msan_unpoison(endptr, sizeof(*endptr));
291 }
292 return res;
293}
294
295INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, // NOLINT
296 int base) {
297 ENSURE_MSAN_INITED();
298 long res = REAL(strtoll)(nptr, endptr, base); //NOLINT
299 if (!__msan_has_dynamic_component()) {
300 __msan_unpoison(endptr, sizeof(*endptr));
301 }
302 return res;
303}
304
305INTERCEPTOR(unsigned long, strtoul, const char *nptr, char **endptr, // NOLINT
306 int base) {
307 ENSURE_MSAN_INITED();
308 unsigned long res = REAL(strtoul)(nptr, endptr, base); // NOLINT
309 if (!__msan_has_dynamic_component()) {
310 __msan_unpoison(endptr, sizeof(*endptr));
311 }
312 return res;
313}
314
315INTERCEPTOR(unsigned long long, strtoull, const char *nptr, // NOLINT
316 char **endptr, int base) {
317 ENSURE_MSAN_INITED();
318 unsigned long res = REAL(strtoull)(nptr, endptr, base); // NOLINT
319 if (!__msan_has_dynamic_component()) {
320 __msan_unpoison(endptr, sizeof(*endptr));
321 }
322 return res;
323}
324
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000325INTERCEPTOR(double, strtod, const char *nptr, char **endptr) { // NOLINT
326 ENSURE_MSAN_INITED();
327 double res = REAL(strtod)(nptr, endptr); // NOLINT
328 if (!__msan_has_dynamic_component()) {
329 __msan_unpoison(endptr, sizeof(*endptr));
330 }
331 return res;
332}
333
Evgeniy Stepanov3f4beff2013-10-14 11:52:40 +0000334INTERCEPTOR(double, strtod_l, const char *nptr, char **endptr,
335 void *loc) { // NOLINT
336 ENSURE_MSAN_INITED();
337 double res = REAL(strtod_l)(nptr, endptr, loc); // NOLINT
338 if (!__msan_has_dynamic_component()) {
339 __msan_unpoison(endptr, sizeof(*endptr));
340 }
341 return res;
342}
343
344INTERCEPTOR(double, __strtod_l, const char *nptr, char **endptr,
345 void *loc) { // NOLINT
346 ENSURE_MSAN_INITED();
347 double res = REAL(__strtod_l)(nptr, endptr, loc); // NOLINT
348 if (!__msan_has_dynamic_component()) {
349 __msan_unpoison(endptr, sizeof(*endptr));
350 }
351 return res;
352}
353
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000354INTERCEPTOR(float, strtof, const char *nptr, char **endptr) { // NOLINT
355 ENSURE_MSAN_INITED();
356 float res = REAL(strtof)(nptr, endptr); // NOLINT
357 if (!__msan_has_dynamic_component()) {
358 __msan_unpoison(endptr, sizeof(*endptr));
359 }
360 return res;
361}
362
Evgeniy Stepanov3f4beff2013-10-14 11:52:40 +0000363INTERCEPTOR(float, strtof_l, const char *nptr, char **endptr,
364 void *loc) { // NOLINT
365 ENSURE_MSAN_INITED();
366 float res = REAL(strtof_l)(nptr, endptr, loc); // NOLINT
367 if (!__msan_has_dynamic_component()) {
368 __msan_unpoison(endptr, sizeof(*endptr));
369 }
370 return res;
371}
372
373INTERCEPTOR(float, __strtof_l, const char *nptr, char **endptr,
374 void *loc) { // NOLINT
375 ENSURE_MSAN_INITED();
376 float res = REAL(__strtof_l)(nptr, endptr, loc); // NOLINT
377 if (!__msan_has_dynamic_component()) {
378 __msan_unpoison(endptr, sizeof(*endptr));
379 }
380 return res;
381}
382
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000383INTERCEPTOR(long double, strtold, const char *nptr, char **endptr) { // NOLINT
384 ENSURE_MSAN_INITED();
385 long double res = REAL(strtold)(nptr, endptr); // NOLINT
386 if (!__msan_has_dynamic_component()) {
387 __msan_unpoison(endptr, sizeof(*endptr));
388 }
389 return res;
390}
391
Evgeniy Stepanov3f4beff2013-10-14 11:52:40 +0000392INTERCEPTOR(long double, strtold_l, const char *nptr, char **endptr,
393 void *loc) { // NOLINT
394 ENSURE_MSAN_INITED();
395 long double res = REAL(strtold_l)(nptr, endptr, loc); // NOLINT
396 if (!__msan_has_dynamic_component()) {
397 __msan_unpoison(endptr, sizeof(*endptr));
398 }
399 return res;
400}
401
402INTERCEPTOR(long double, __strtold_l, const char *nptr, char **endptr,
403 void *loc) { // NOLINT
404 ENSURE_MSAN_INITED();
405 long double res = REAL(__strtold_l)(nptr, endptr, loc); // NOLINT
406 if (!__msan_has_dynamic_component()) {
407 __msan_unpoison(endptr, sizeof(*endptr));
408 }
409 return res;
410}
411
Evgeniy Stepanov0797ed42013-06-21 13:32:26 +0000412INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap) {
413 ENSURE_MSAN_INITED();
414 int res = REAL(vasprintf)(strp, format, ap);
415 if (res >= 0 && !__msan_has_dynamic_component()) {
416 __msan_unpoison(strp, sizeof(*strp));
417 __msan_unpoison(*strp, res + 1);
418 }
419 return res;
420}
421
422INTERCEPTOR(int, asprintf, char **strp, const char *format, ...) { // NOLINT
423 ENSURE_MSAN_INITED();
424 va_list ap;
425 va_start(ap, format);
426 int res = vasprintf(strp, format, ap); // NOLINT
427 va_end(ap);
428 return res;
429}
430
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000431INTERCEPTOR(int, vsnprintf, char *str, uptr size,
432 const char *format, va_list ap) {
433 ENSURE_MSAN_INITED();
434 int res = REAL(vsnprintf)(str, size, format, ap);
Evgeniy Stepanov0797ed42013-06-21 13:32:26 +0000435 if (res >= 0 && !__msan_has_dynamic_component()) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000436 __msan_unpoison(str, res + 1);
437 }
438 return res;
439}
440
441INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap) {
442 ENSURE_MSAN_INITED();
443 int res = REAL(vsprintf)(str, format, ap);
Evgeniy Stepanov0797ed42013-06-21 13:32:26 +0000444 if (res >= 0 && !__msan_has_dynamic_component()) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000445 __msan_unpoison(str, res + 1);
446 }
447 return res;
448}
449
450INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
451 ENSURE_MSAN_INITED();
452 int res = REAL(vswprintf)(str, size, format, ap);
Evgeniy Stepanov0797ed42013-06-21 13:32:26 +0000453 if (res >= 0 && !__msan_has_dynamic_component()) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000454 __msan_unpoison(str, 4 * (res + 1));
455 }
456 return res;
457}
458
459INTERCEPTOR(int, sprintf, char *str, const char *format, ...) { // NOLINT
460 ENSURE_MSAN_INITED();
461 va_list ap;
462 va_start(ap, format);
463 int res = vsprintf(str, format, ap); // NOLINT
464 va_end(ap);
465 return res;
466}
467
468INTERCEPTOR(int, snprintf, char *str, uptr size, const char *format, ...) {
469 ENSURE_MSAN_INITED();
470 va_list ap;
471 va_start(ap, format);
472 int res = vsnprintf(str, size, format, ap);
473 va_end(ap);
474 return res;
475}
476
477INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
478 ENSURE_MSAN_INITED();
479 va_list ap;
480 va_start(ap, format);
481 int res = vswprintf(str, size, format, ap);
482 va_end(ap);
483 return res;
484}
485
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000486// SIZE_T strftime(char *s, SIZE_T max, const char *format,const struct tm *tm);
487INTERCEPTOR(SIZE_T, strftime, char *s, SIZE_T max, const char *format,
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000488 void *tm) {
489 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000490 SIZE_T res = REAL(strftime)(s, max, format, tm);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000491 if (res) __msan_unpoison(s, res + 1);
492 return res;
493}
494
Evgeniy Stepanov80144892013-07-02 13:34:44 +0000495INTERCEPTOR(int, mbtowc, wchar_t *dest, const char *src, SIZE_T n) {
496 ENSURE_MSAN_INITED();
497 int res = REAL(mbtowc)(dest, src, n);
498 if (res != -1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
499 return res;
500}
501
502INTERCEPTOR(int, mbrtowc, wchar_t *dest, const char *src, SIZE_T n, void *ps) {
503 ENSURE_MSAN_INITED();
504 SIZE_T res = REAL(mbrtowc)(dest, src, n, ps);
505 if (res != (SIZE_T)-1 && dest) __msan_unpoison(dest, sizeof(wchar_t));
506 return res;
507}
508
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000509INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000510 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000511 SIZE_T res = REAL(wcslen)(s);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000512 CHECK_UNPOISONED(s, sizeof(wchar_t) * (res + 1));
513 return res;
514}
515
516// wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
517INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
518 ENSURE_MSAN_INITED();
519 wchar_t *res = REAL(wcschr)(s, wc, ps);
520 return res;
521}
522
523// wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
524INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
525 ENSURE_MSAN_INITED();
526 wchar_t *res = REAL(wcscpy)(dest, src);
527 __msan_copy_poison(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1));
528 return res;
529}
530
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000531// wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, SIZE_T n);
532INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000533 ENSURE_MSAN_INITED();
534 wchar_t *res = REAL(wmemcpy)(dest, src, n);
535 __msan_copy_poison(dest, src, n * sizeof(wchar_t));
536 return res;
537}
538
Evgeniy Stepanov353c9962013-07-02 14:49:24 +0000539INTERCEPTOR(wchar_t *, wmempcpy, wchar_t *dest, const wchar_t *src, SIZE_T n) {
540 ENSURE_MSAN_INITED();
541 wchar_t *res = REAL(wmempcpy)(dest, src, n);
542 __msan_copy_poison(dest, src, n * sizeof(wchar_t));
543 return res;
544}
545
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000546INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000547 CHECK(MEM_IS_APP(s));
548 ENSURE_MSAN_INITED();
549 wchar_t *res = (wchar_t *)fast_memset(s, c, n * sizeof(wchar_t));
550 __msan_unpoison(s, n * sizeof(wchar_t));
551 return res;
552}
553
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000554INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000555 ENSURE_MSAN_INITED();
556 wchar_t *res = REAL(wmemmove)(dest, src, n);
557 __msan_move_poison(dest, src, n * sizeof(wchar_t));
558 return res;
559}
560
561INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
562 ENSURE_MSAN_INITED();
563 int res = REAL(wcscmp)(s1, s2);
564 return res;
565}
566
567INTERCEPTOR(double, wcstod, const wchar_t *nptr, wchar_t **endptr) {
568 ENSURE_MSAN_INITED();
569 double res = REAL(wcstod)(nptr, endptr);
570 __msan_unpoison(endptr, sizeof(*endptr));
571 return res;
572}
573
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000574INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
575 ENSURE_MSAN_INITED();
576 int res = REAL(gettimeofday)(tv, tz);
577 if (tv)
578 __msan_unpoison(tv, 16);
579 if (tz)
580 __msan_unpoison(tz, 8);
581 return res;
582}
583
584INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
585 ENSURE_MSAN_INITED();
586 char *res = REAL(fcvt)(x, a, b, c);
587 if (!__msan_has_dynamic_component()) {
588 __msan_unpoison(b, sizeof(*b));
589 __msan_unpoison(c, sizeof(*c));
590 }
591 return res;
592}
593
594INTERCEPTOR(char *, getenv, char *name) {
595 ENSURE_MSAN_INITED();
596 char *res = REAL(getenv)(name);
597 if (!__msan_has_dynamic_component()) {
598 if (res)
599 __msan_unpoison(res, REAL(strlen)(res) + 1);
600 }
601 return res;
602}
603
Evgeniy Stepanov534e2ba2013-08-01 11:14:14 +0000604extern char **environ;
605
606static void UnpoisonEnviron() {
607 char **envp = environ;
608 for (; *envp; ++envp) {
609 __msan_unpoison(envp, sizeof(*envp));
610 __msan_unpoison(*envp, REAL(strlen)(*envp) + 1);
611 }
612 // Trailing NULL pointer.
613 __msan_unpoison(envp, sizeof(*envp));
614}
615
616INTERCEPTOR(int, setenv, const char *name, const char *value, int overwrite) {
617 ENSURE_MSAN_INITED();
618 int res = REAL(setenv)(name, value, overwrite);
619 if (!res) UnpoisonEnviron();
620 return res;
621}
622
623INTERCEPTOR(int, putenv, char *string) {
624 ENSURE_MSAN_INITED();
625 int res = REAL(putenv)(string);
626 if (!res) UnpoisonEnviron();
627 return res;
628}
629
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000630INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
631 ENSURE_MSAN_INITED();
632 int res = REAL(__fxstat)(magic, fd, buf);
633 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000634 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000635 return res;
636}
637
638INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
639 ENSURE_MSAN_INITED();
640 int res = REAL(__fxstat64)(magic, fd, buf);
641 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000642 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000643 return res;
644}
645
Evgeniy Stepanovbb229422013-09-09 13:40:41 +0000646INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
647 int flags) {
648 ENSURE_MSAN_INITED();
649 int res = REAL(__fxstatat)(magic, fd, pathname, buf, flags);
650 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
651 return res;
652}
653
654INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
655 int flags) {
656 ENSURE_MSAN_INITED();
657 int res = REAL(__fxstatat64)(magic, fd, pathname, buf, flags);
658 if (!res) __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
659 return res;
660}
661
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000662INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) {
663 ENSURE_MSAN_INITED();
664 int res = REAL(__xstat)(magic, path, buf);
665 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000666 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000667 return res;
668}
669
670INTERCEPTOR(int, __xstat64, int magic, char *path, void *buf) {
671 ENSURE_MSAN_INITED();
672 int res = REAL(__xstat64)(magic, path, buf);
673 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000674 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000675 return res;
676}
677
678INTERCEPTOR(int, __lxstat, int magic, char *path, void *buf) {
679 ENSURE_MSAN_INITED();
680 int res = REAL(__lxstat)(magic, path, buf);
681 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000682 __msan_unpoison(buf, __sanitizer::struct_stat_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000683 return res;
684}
685
686INTERCEPTOR(int, __lxstat64, int magic, char *path, void *buf) {
687 ENSURE_MSAN_INITED();
688 int res = REAL(__lxstat64)(magic, path, buf);
689 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000690 __msan_unpoison(buf, __sanitizer::struct_stat64_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000691 return res;
692}
693
694INTERCEPTOR(int, pipe, int pipefd[2]) {
695 if (msan_init_is_running)
696 return REAL(pipe)(pipefd);
697 ENSURE_MSAN_INITED();
698 int res = REAL(pipe)(pipefd);
699 if (!res)
700 __msan_unpoison(pipefd, sizeof(int[2]));
701 return res;
702}
703
Evgeniy Stepanov134fe8a2013-04-08 13:45:12 +0000704INTERCEPTOR(int, pipe2, int pipefd[2], int flags) {
705 ENSURE_MSAN_INITED();
706 int res = REAL(pipe2)(pipefd, flags);
707 if (!res)
708 __msan_unpoison(pipefd, sizeof(int[2]));
709 return res;
710}
711
712INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int sv[2]) {
713 ENSURE_MSAN_INITED();
714 int res = REAL(socketpair)(domain, type, protocol, sv);
715 if (!res)
716 __msan_unpoison(sv, sizeof(int[2]));
717 return res;
718}
719
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000720INTERCEPTOR(char *, fgets, char *s, int size, void *stream) {
721 ENSURE_MSAN_INITED();
722 char *res = REAL(fgets)(s, size, stream);
723 if (res)
724 __msan_unpoison(s, REAL(strlen)(s) + 1);
725 return res;
726}
727
728INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
729 ENSURE_MSAN_INITED();
730 char *res = REAL(fgets_unlocked)(s, size, stream);
731 if (res)
732 __msan_unpoison(s, REAL(strlen)(s) + 1);
733 return res;
734}
735
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000736INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
737 if (msan_init_is_running)
738 return REAL(getrlimit)(resource, rlim);
739 ENSURE_MSAN_INITED();
740 int res = REAL(getrlimit)(resource, rlim);
741 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000742 __msan_unpoison(rlim, __sanitizer::struct_rlimit_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000743 return res;
744}
745
746INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
747 if (msan_init_is_running)
748 return REAL(getrlimit64)(resource, rlim);
749 ENSURE_MSAN_INITED();
750 int res = REAL(getrlimit64)(resource, rlim);
751 if (!res)
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000752 __msan_unpoison(rlim, __sanitizer::struct_rlimit64_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000753 return res;
754}
755
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000756INTERCEPTOR(int, uname, void *utsname) {
757 ENSURE_MSAN_INITED();
758 int res = REAL(uname)(utsname);
759 if (!res) {
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000760 __msan_unpoison(utsname, __sanitizer::struct_utsname_sz);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000761 }
762 return res;
763}
764
Evgeniy Stepanov95d05882013-01-23 10:43:38 +0000765INTERCEPTOR(int, gethostname, char *name, SIZE_T len) {
766 ENSURE_MSAN_INITED();
767 int res = REAL(gethostname)(name, len);
768 if (!res) {
769 SIZE_T real_len = REAL(strnlen)(name, len);
770 if (real_len < len)
771 ++real_len;
772 __msan_unpoison(name, real_len);
773 }
774 return res;
775}
776
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000777INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
778 int timeout) {
779 ENSURE_MSAN_INITED();
780 int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
781 if (res > 0) {
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000782 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000783 }
784 return res;
785}
786
787INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
788 int timeout, void *sigmask) {
789 ENSURE_MSAN_INITED();
790 int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
791 if (res > 0) {
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000792 __msan_unpoison(events, __sanitizer::struct_epoll_event_sz * res);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000793 }
794 return res;
795}
796
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000797INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000798 ENSURE_MSAN_INITED();
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000799 SSIZE_T res = REAL(recv)(fd, buf, len, flags);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000800 if (res > 0)
801 __msan_unpoison(buf, res);
802 return res;
803}
804
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000805INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
Evgeniy Stepanov9666d892013-05-29 11:30:00 +0000806 void *srcaddr, int *addrlen) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000807 ENSURE_MSAN_INITED();
Evgeniy Stepanov7b719df2012-12-25 16:51:57 +0000808 SIZE_T srcaddr_sz;
Evgeniy Stepanov9666d892013-05-29 11:30:00 +0000809 if (srcaddr) srcaddr_sz = *addrlen;
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000810 SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
Evgeniy Stepanov7b719df2012-12-25 16:51:57 +0000811 if (res > 0) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000812 __msan_unpoison(buf, res);
Evgeniy Stepanov7b719df2012-12-25 16:51:57 +0000813 if (srcaddr) {
Evgeniy Stepanov9666d892013-05-29 11:30:00 +0000814 SIZE_T sz = *addrlen;
Evgeniy Stepanov7b719df2012-12-25 16:51:57 +0000815 __msan_unpoison(srcaddr, (sz < srcaddr_sz) ? sz : srcaddr_sz);
816 }
817 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000818 return res;
819}
820
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000821INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
Kostya Serebryany967e07e2013-09-06 10:58:55 +0000822 if (CallocShouldReturnNullDueToOverflow(size, nmemb))
823 return AllocatorReturnNull();
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000824 GET_MALLOC_STACK_TRACE;
825 if (!msan_inited) {
826 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000827 const SIZE_T kCallocPoolSize = 1024;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000828 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000829 static SIZE_T allocated;
830 SIZE_T size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000831 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
832 allocated += size_in_words;
833 CHECK(allocated < kCallocPoolSize);
834 return mem;
835 }
836 return MsanReallocate(&stack, 0, nmemb * size, sizeof(u64), true);
837}
838
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000839INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000840 GET_MALLOC_STACK_TRACE;
841 return MsanReallocate(&stack, ptr, size, sizeof(u64), false);
842}
843
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000844INTERCEPTOR(void *, malloc, SIZE_T size) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000845 GET_MALLOC_STACK_TRACE;
846 return MsanReallocate(&stack, 0, size, sizeof(u64), false);
847}
848
Alexey Samsonov11347bf2013-04-23 13:34:19 +0000849void __msan_allocated_memory(const void* data, uptr size) {
Evgeniy Stepanov887a5fe2013-01-28 13:52:49 +0000850 GET_MALLOC_STACK_TRACE;
851 if (flags()->poison_in_malloc)
852 __msan_poison(data, size);
853 if (__msan_get_track_origins()) {
854 u32 stack_id = StackDepotPut(stack.trace, stack.size);
855 CHECK(stack_id);
856 CHECK_EQ((stack_id >> 31), 0); // Higher bit is occupied by stack origins.
857 __msan_set_origin(data, size, stack_id);
858 }
859}
860
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000861INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
862 int fd, OFF_T offset) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000863 ENSURE_MSAN_INITED();
864 void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
865 if (res != (void*)-1)
866 __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
867 return res;
868}
869
Kostya Serebryany6afa1b02012-12-13 06:31:40 +0000870INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
871 int fd, OFF64_T offset) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000872 ENSURE_MSAN_INITED();
873 void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
874 if (res != (void*)-1)
875 __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
876 return res;
877}
878
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000879struct dlinfo {
880 char *dli_fname;
881 void *dli_fbase;
882 char *dli_sname;
883 void *dli_saddr;
884};
885
886INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) {
887 ENSURE_MSAN_INITED();
888 int res = REAL(dladdr)(addr, info);
889 if (res != 0) {
890 __msan_unpoison(info, sizeof(*info));
891 if (info->dli_fname)
892 __msan_unpoison(info->dli_fname, REAL(strlen)(info->dli_fname) + 1);
893 if (info->dli_sname)
894 __msan_unpoison(info->dli_sname, REAL(strlen)(info->dli_sname) + 1);
895 }
896 return res;
897}
898
Reid Kleckner0f92deb2013-03-11 18:07:42 +0000899// dlopen() ultimately calls mmap() down inside the loader, which generally
900// doesn't participate in dynamic symbol resolution. Therefore we won't
901// intercept its calls to mmap, and we have to hook it here. The loader
902// initializes the module before returning, so without the dynamic component, we
903// won't be able to clear the shadow before the initializers. Fixing this would
904// require putting our own initializer first to clear the shadow.
905INTERCEPTOR(void *, dlopen, const char *filename, int flag) {
906 ENSURE_MSAN_INITED();
907 EnterLoader();
908 link_map *map = (link_map *)REAL(dlopen)(filename, flag);
909 ExitLoader();
Evgeniy Stepanov6c503b92013-03-22 11:59:49 +0000910 if (!__msan_has_dynamic_component() && map) {
Reid Kleckner0f92deb2013-03-11 18:07:42 +0000911 // If msandr didn't clear the shadow before the initializers ran, we do it
912 // ourselves afterwards.
Peter Collingbourne2e75ac92013-07-29 19:09:49 +0000913 ForEachMappedRegion(map, __msan_unpoison);
Reid Kleckner0f92deb2013-03-11 18:07:42 +0000914 }
915 return (void *)map;
916}
917
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +0000918typedef int (*dl_iterate_phdr_cb)(__sanitizer_dl_phdr_info *info, SIZE_T size,
919 void *data);
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +0000920struct dl_iterate_phdr_data {
921 dl_iterate_phdr_cb callback;
922 void *data;
923};
924
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +0000925static int msan_dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
926 void *data) {
927 if (info) {
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +0000928 __msan_unpoison(info, size);
Evgeniy Stepanov1aad6b52013-05-17 12:51:13 +0000929 if (info->dlpi_name)
930 __msan_unpoison(info->dlpi_name, REAL(strlen)(info->dlpi_name) + 1);
931 }
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +0000932 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
Alexey Samsonovc2918bf2013-06-27 07:50:56 +0000933 UnpoisonParam(3);
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +0000934 return cbdata->callback(info, size, cbdata->data);
935}
936
937INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb callback, void *data) {
938 ENSURE_MSAN_INITED();
939 EnterLoader();
940 dl_iterate_phdr_data cbdata;
941 cbdata.callback = callback;
942 cbdata.data = data;
943 int res = REAL(dl_iterate_phdr)(msan_dl_iterate_phdr_cb, (void *)&cbdata);
944 ExitLoader();
945 return res;
946}
947
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000948INTERCEPTOR(int, getrusage, int who, void *usage) {
949 ENSURE_MSAN_INITED();
950 int res = REAL(getrusage)(who, usage);
951 if (res == 0) {
Evgeniy Stepanov9358c582013-02-19 09:19:16 +0000952 __msan_unpoison(usage, __sanitizer::struct_rusage_sz);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +0000953 }
954 return res;
955}
956
Evgeniy Stepanove0a87122013-08-27 11:34:05 +0000957// sigactions_mu guarantees atomicity of sigaction() and signal() calls.
958// Access to sigactions[] is gone with relaxed atomics to avoid data race with
959// the signal handler.
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +0000960const int kMaxSignals = 1024;
Evgeniy Stepanove0a87122013-08-27 11:34:05 +0000961static atomic_uintptr_t sigactions[kMaxSignals];
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +0000962static StaticSpinMutex sigactions_mu;
963
964static void SignalHandler(int signo) {
Evgeniy Stepanov0e38a672013-08-27 14:08:15 +0000965 ScopedThreadLocalStateBackup stlsb;
Evgeniy Stepanov91659d52013-08-27 12:59:39 +0000966 UnpoisonParam(1);
967
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000968 typedef void (*signal_cb)(int x);
Evgeniy Stepanove0a87122013-08-27 11:34:05 +0000969 signal_cb cb =
970 (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +0000971 cb(signo);
972}
973
974static void SignalAction(int signo, void *si, void *uc) {
Evgeniy Stepanov0e38a672013-08-27 14:08:15 +0000975 ScopedThreadLocalStateBackup stlsb;
Alexey Samsonovc2918bf2013-06-27 07:50:56 +0000976 UnpoisonParam(3);
Evgeniy Stepanovb32d1bf2013-08-27 11:10:04 +0000977 __msan_unpoison(si, sizeof(__sanitizer_sigaction));
Evgeniy Stepanov07277022013-04-04 09:03:56 +0000978 __msan_unpoison(uc, __sanitizer::ucontext_t_sz);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +0000979
980 typedef void (*sigaction_cb)(int, void *, void *);
Evgeniy Stepanove0a87122013-08-27 11:34:05 +0000981 sigaction_cb cb =
982 (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +0000983 cb(signo, si, uc);
984}
985
986INTERCEPTOR(int, sigaction, int signo, const __sanitizer_sigaction *act,
987 __sanitizer_sigaction *oldact) {
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +0000988 ENSURE_MSAN_INITED();
Kostya Serebryany583025d2013-04-04 06:52:40 +0000989 // FIXME: check that *act is unpoisoned.
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +0000990 // That requires intercepting all of sigemptyset, sigfillset, etc.
Evgeniy Stepanova213ab62013-04-05 11:59:16 +0000991 int res;
992 if (flags()->wrap_signals) {
993 SpinMutexLock lock(&sigactions_mu);
994 CHECK_LT(signo, kMaxSignals);
Evgeniy Stepanove0a87122013-08-27 11:34:05 +0000995 uptr old_cb = atomic_load(&sigactions[signo], memory_order_relaxed);
Evgeniy Stepanova213ab62013-04-05 11:59:16 +0000996 __sanitizer_sigaction new_act;
997 __sanitizer_sigaction *pnew_act = act ? &new_act : 0;
998 if (act) {
Evgeniy Stepanovb32d1bf2013-08-27 11:10:04 +0000999 internal_memcpy(pnew_act, act, sizeof(__sanitizer_sigaction));
1000 uptr cb = (uptr)pnew_act->sa_sigaction;
1001 uptr new_cb = (pnew_act->sa_flags & __sanitizer::sa_siginfo)
1002 ? (uptr)SignalAction
1003 : (uptr)SignalHandler;
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001004 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
Evgeniy Stepanove0a87122013-08-27 11:34:05 +00001005 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
Evgeniy Stepanovb32d1bf2013-08-27 11:10:04 +00001006 pnew_act->sa_sigaction = (void (*)(int, void *, void *))new_cb;
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001007 }
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001008 }
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001009 res = REAL(sigaction)(signo, pnew_act, oldact);
1010 if (res == 0 && oldact) {
Evgeniy Stepanovb32d1bf2013-08-27 11:10:04 +00001011 uptr cb = (uptr)oldact->sa_sigaction;
Evgeniy Stepanovcd3049d2013-04-05 12:58:07 +00001012 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
Evgeniy Stepanovb32d1bf2013-08-27 11:10:04 +00001013 oldact->sa_sigaction = (void (*)(int, void *, void *))old_cb;
Evgeniy Stepanovcd3049d2013-04-05 12:58:07 +00001014 }
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001015 }
1016 } else {
1017 res = REAL(sigaction)(signo, act, oldact);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001018 }
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001019
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001020 if (res == 0 && oldact) {
Evgeniy Stepanovb32d1bf2013-08-27 11:10:04 +00001021 __msan_unpoison(oldact, sizeof(__sanitizer_sigaction));
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00001022 }
1023 return res;
1024}
1025
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001026INTERCEPTOR(int, signal, int signo, uptr cb) {
1027 ENSURE_MSAN_INITED();
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001028 if (flags()->wrap_signals) {
1029 CHECK_LT(signo, kMaxSignals);
1030 SpinMutexLock lock(&sigactions_mu);
1031 if (cb != __sanitizer::sig_ign && cb != __sanitizer::sig_dfl) {
Evgeniy Stepanove0a87122013-08-27 11:34:05 +00001032 atomic_store(&sigactions[signo], cb, memory_order_relaxed);
Evgeniy Stepanova213ab62013-04-05 11:59:16 +00001033 cb = (uptr) SignalHandler;
1034 }
1035 return REAL(signal)(signo, cb);
1036 } else {
1037 return REAL(signal)(signo, cb);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001038 }
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001039}
1040
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00001041extern "C" int pthread_attr_init(void *attr);
1042extern "C" int pthread_attr_destroy(void *attr);
1043extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize);
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00001044extern "C" int pthread_attr_getstack(void *attr, uptr *stack, uptr *stacksize);
Evgeniy Stepanov7c6bd402013-10-22 14:31:30 +00001045extern "C" int pthread_setspecific(unsigned key, const void *v);
1046extern "C" int pthread_yield();
1047
1048static void thread_finalize(void *v) {
1049 uptr iter = (uptr)v;
1050 if (iter > 1) {
1051 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
1052 Printf("MemorySanitizer: failed to set thread key\n");
1053 Die();
1054 }
1055 return;
1056 }
1057 MsanAllocatorThreadFinish();
1058}
1059
1060struct ThreadParam {
1061 void* (*callback)(void *arg);
1062 void *param;
1063 atomic_uintptr_t done;
1064};
1065
1066static void *MsanThreadStartFunc(void *arg) {
1067 ThreadParam *p = (ThreadParam *)arg;
1068 void* (*callback)(void *arg) = p->callback;
1069 void *param = p->param;
1070 if (pthread_setspecific(g_thread_finalize_key,
1071 (void *)kPthreadDestructorIterations)) {
1072 Printf("MemorySanitizer: failed to set thread key\n");
1073 Die();
1074 }
1075 atomic_store(&p->done, 1, memory_order_release);
1076 return callback(param);
1077}
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00001078
1079INTERCEPTOR(int, pthread_create, void *th, void *attr, void *(*callback)(void*),
1080 void * param) {
1081 ENSURE_MSAN_INITED(); // for GetTlsSize()
1082 __sanitizer_pthread_attr_t myattr;
1083 if (attr == 0) {
1084 pthread_attr_init(&myattr);
1085 attr = &myattr;
1086 }
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +00001087
Dmitry Vyukov06cbed82013-10-15 14:12:26 +00001088 AdjustStackSizeLinux(attr);
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00001089
Evgeniy Stepanov7c6bd402013-10-22 14:31:30 +00001090 ThreadParam p;
1091 p.callback = callback;
1092 p.param = param;
1093 atomic_store(&p.done, 0, memory_order_relaxed);
1094
1095 int res = REAL(pthread_create)(th, attr, MsanThreadStartFunc, (void *)&p);
1096 if (res == 0) {
1097 while (atomic_load(&p.done, memory_order_acquire) != 1)
1098 pthread_yield();
1099 }
1100
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00001101 if (attr == &myattr)
1102 pthread_attr_destroy(&myattr);
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00001103 if (!res) {
1104 __msan_unpoison(th, __sanitizer::pthread_t_sz);
1105 }
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00001106 return res;
1107}
1108
Timur Iskhodzhanoveaca82c2013-07-15 16:11:39 +00001109INTERCEPTOR(int, pthread_key_create, __sanitizer_pthread_key_t *key,
1110 void (*dtor)(void *value)) {
Evgeniy Stepanov737da2f2013-07-05 12:31:07 +00001111 ENSURE_MSAN_INITED();
1112 int res = REAL(pthread_key_create)(key, dtor);
1113 if (!res && key)
1114 __msan_unpoison(key, sizeof(*key));
1115 return res;
1116}
1117
Evgeniy Stepanov65670922013-07-30 12:54:34 +00001118INTERCEPTOR(int, pthread_join, void *th, void **retval) {
1119 ENSURE_MSAN_INITED();
1120 int res = REAL(pthread_join)(th, retval);
1121 if (!res && retval)
1122 __msan_unpoison(retval, sizeof(*retval));
1123 return res;
1124}
1125
Evgeniy Stepanov7b636082013-09-13 11:27:38 +00001126extern char *tzname[2];
1127
1128INTERCEPTOR(void, tzset) {
1129 ENSURE_MSAN_INITED();
1130 REAL(tzset)();
1131 if (tzname[0])
1132 __msan_unpoison(tzname[0], REAL(strlen)(tzname[0]) + 1);
1133 if (tzname[1])
1134 __msan_unpoison(tzname[1], REAL(strlen)(tzname[1]) + 1);
1135 return;
1136}
1137
Evgeniy Stepanovcfc29de2013-09-27 11:32:21 +00001138struct MSanAtExitRecord {
1139 void (*func)(void *arg);
1140 void *arg;
1141};
1142
1143void MSanAtExitWrapper(void *arg) {
1144 UnpoisonParam(1);
1145 MSanAtExitRecord *r = (MSanAtExitRecord *)arg;
1146 r->func(r->arg);
1147 InternalFree(r);
1148}
1149
1150// Unpoison argument shadow for C++ module destructors.
1151INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
1152 void *dso_handle) {
1153 if (msan_init_is_running) return REAL(__cxa_atexit)(func, arg, dso_handle);
1154 ENSURE_MSAN_INITED();
1155 MSanAtExitRecord *r =
1156 (MSanAtExitRecord *)InternalAlloc(sizeof(MSanAtExitRecord));
1157 r->func = func;
1158 r->arg = arg;
1159 return REAL(__cxa_atexit)(MSanAtExitWrapper, r, dso_handle);
1160}
1161
Evgeniy Stepanov447ef192013-05-22 12:50:26 +00001162struct MSanInterceptorContext {
1163 bool in_interceptor_scope;
1164};
1165
Dmitry Vyukov8cde99f2013-10-03 15:43:59 +00001166namespace __msan {
1167
1168int OnExit() {
Dmitry Vyukov14dd9802013-10-03 15:22:29 +00001169 // FIXME: ask frontend whether we need to return failure.
1170 return 0;
1171}
1172
Dmitry Vyukov8cde99f2013-10-03 15:43:59 +00001173} // namespace __msan
1174
Evgeniy Stepanov5b2afc32013-05-23 11:51:47 +00001175// A version of CHECK_UNPOISED using a saved scope value. Used in common
1176// interceptors.
Evgeniy Stepanov341b9e62013-06-28 11:02:43 +00001177#define CHECK_UNPOISONED_CTX(ctx, x, n) \
1178 do { \
1179 if (!((MSanInterceptorContext *)ctx)->in_interceptor_scope) \
1180 CHECK_UNPOISONED_0(x, n); \
1181 } while (0)
Evgeniy Stepanov447ef192013-05-22 12:50:26 +00001182
Evgeniy Stepanov3fa122e2013-07-09 12:07:59 +00001183#define COMMON_INTERCEPTOR_UNPOISON_PARAM(ctx, count) \
1184 UnpoisonParam(count)
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00001185#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
Evgeniy Stepanov447ef192013-05-22 12:50:26 +00001186 __msan_unpoison(ptr, size)
1187#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
Evgeniy Stepanov341b9e62013-06-28 11:02:43 +00001188 CHECK_UNPOISONED_CTX(ctx, ptr, size)
Dmitry Vyukov0586dcc2013-10-03 14:12:09 +00001189#define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ctx, ptr, size) \
1190 __msan_unpoison(ptr, size)
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00001191#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
1192 if (msan_init_is_running) return REAL(func)(__VA_ARGS__); \
1193 MSanInterceptorContext msan_ctx = {IsInInterceptorScope()}; \
1194 ctx = (void *)&msan_ctx; \
Dmitry Vyukovfdf10e22013-10-02 14:02:08 +00001195 (void)ctx; \
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00001196 InterceptorScope interceptor_scope; \
Evgeniy Stepanov447ef192013-05-22 12:50:26 +00001197 ENSURE_MSAN_INITED();
1198#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
1199 do { \
Evgeniy Stepanov82a90802013-01-18 13:12:56 +00001200 } while (false)
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00001201#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
1202 do { \
1203 } while (false)
1204#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
1205 do { \
1206 } while (false)
Evgeniy Stepanov82a90802013-01-18 13:12:56 +00001207#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00001208 do { \
1209 } while (false) // FIXME
Evgeniy Stepanove18e3f02013-08-12 13:19:53 +00001210#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
Dmitry Vyukov14dd9802013-10-03 15:22:29 +00001211#define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
Evgeniy Stepanov4f32c0b2013-01-18 13:01:18 +00001212#include "sanitizer_common/sanitizer_common_interceptors.inc"
Kostya Serebryanyc20b3212013-01-18 06:43:13 +00001213
Evgeniy Stepanov2887a642013-04-11 14:37:04 +00001214#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) CHECK_UNPOISONED(p, s)
Alexey Samsonov6b859452013-09-19 08:35:16 +00001215#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
1216 do { \
1217 } while (false)
1218#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
1219 do { \
1220 } while (false)
Evgeniy Stepanov2887a642013-04-11 14:37:04 +00001221#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) __msan_unpoison(p, s)
1222#include "sanitizer_common/sanitizer_common_syscalls.inc"
1223
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001224// static
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001225void *fast_memset(void *ptr, int c, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001226 // hack until we have a really fast internal_memset
1227 if (sizeof(uptr) == 8 &&
1228 (n % 8) == 0 &&
1229 ((uptr)ptr % 8) == 0 &&
1230 (c == 0 || c == -1)) {
1231 // Printf("memset %p %zd %x\n", ptr, n, c);
1232 uptr to_store = c ? -1L : 0L;
1233 uptr *p = (uptr*)ptr;
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001234 for (SIZE_T i = 0; i < n / 8; i++)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001235 p[i] = to_store;
1236 return ptr;
1237 }
1238 return internal_memset(ptr, c, n);
1239}
1240
1241// static
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001242void *fast_memcpy(void *dst, const void *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001243 // Same hack as in fast_memset above.
1244 if (sizeof(uptr) == 8 &&
1245 (n % 8) == 0 &&
1246 ((uptr)dst % 8) == 0 &&
1247 ((uptr)src % 8) == 0) {
1248 uptr *d = (uptr*)dst;
1249 uptr *s = (uptr*)src;
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001250 for (SIZE_T i = 0; i < n / 8; i++)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001251 d[i] = s[i];
1252 return dst;
1253 }
1254 return internal_memcpy(dst, src, n);
1255}
1256
1257// These interface functions reside here so that they can use
1258// fast_memset, etc.
Alexey Samsonov11347bf2013-04-23 13:34:19 +00001259void __msan_unpoison(const void *a, uptr size) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001260 if (!MEM_IS_APP(a)) return;
1261 fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size);
1262}
1263
Alexey Samsonov11347bf2013-04-23 13:34:19 +00001264void __msan_poison(const void *a, uptr size) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001265 if (!MEM_IS_APP(a)) return;
1266 fast_memset((void*)MEM_TO_SHADOW((uptr)a),
1267 __msan::flags()->poison_heap_with_zeroes ? 0 : -1, size);
1268}
1269
1270void __msan_poison_stack(void *a, uptr size) {
1271 if (!MEM_IS_APP(a)) return;
1272 fast_memset((void*)MEM_TO_SHADOW((uptr)a),
1273 __msan::flags()->poison_stack_with_zeroes ? 0 : -1, size);
1274}
1275
1276void __msan_clear_and_unpoison(void *a, uptr size) {
1277 fast_memset(a, 0, size);
1278 fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size);
1279}
1280
Evgeniy Stepanov450eee62013-10-24 11:56:03 +00001281u32 get_origin_if_poisoned(uptr a, uptr size) {
1282 unsigned char *s = (unsigned char *)MEM_TO_SHADOW(a);
1283 for (uptr i = 0; i < size; ++i)
1284 if (s[i])
1285 return *(uptr *)SHADOW_TO_ORIGIN((s + i) & ~3UL);
1286 return 0;
1287}
1288
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001289void __msan_copy_origin(void *dst, const void *src, uptr size) {
1290 if (!__msan_get_track_origins()) return;
1291 if (!MEM_IS_APP(dst) || !MEM_IS_APP(src)) return;
Evgeniy Stepanov450eee62013-10-24 11:56:03 +00001292 uptr d = (uptr)dst;
1293 uptr beg = d & ~3UL;
1294 // Copy left unaligned origin if that memory is poisoned.
1295 if (beg < d) {
1296 u32 o = get_origin_if_poisoned(beg, d - beg);
1297 if (o)
1298 *(uptr *)MEM_TO_ORIGIN(beg) = o;
1299 beg += 4;
1300 }
1301
1302 uptr end = (d + size + 3) & ~3UL;
1303 // Copy right unaligned origin if that memory is poisoned.
1304 if (end > d + size) {
1305 u32 o = get_origin_if_poisoned(d + size, end - d - size);
1306 if (o)
1307 *(uptr *)MEM_TO_ORIGIN(end - 4) = o;
1308 end -= 4;
1309 }
1310
1311 if (beg < end) {
1312 // Align src up.
1313 uptr s = ((uptr)src + 3) & ~3UL;
1314 fast_memcpy((void*)MEM_TO_ORIGIN(beg), (void*)MEM_TO_ORIGIN(s), end - beg);
1315 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001316}
1317
1318void __msan_copy_poison(void *dst, const void *src, uptr size) {
1319 if (!MEM_IS_APP(dst)) return;
1320 if (!MEM_IS_APP(src)) return;
1321 fast_memcpy((void*)MEM_TO_SHADOW((uptr)dst),
1322 (void*)MEM_TO_SHADOW((uptr)src), size);
1323 __msan_copy_origin(dst, src, size);
1324}
1325
1326void __msan_move_poison(void *dst, const void *src, uptr size) {
1327 if (!MEM_IS_APP(dst)) return;
1328 if (!MEM_IS_APP(src)) return;
1329 internal_memmove((void*)MEM_TO_SHADOW((uptr)dst),
1330 (void*)MEM_TO_SHADOW((uptr)src), size);
1331 __msan_copy_origin(dst, src, size);
1332}
1333
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001334void *__msan_memcpy(void *dest, const void *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001335 ENSURE_MSAN_INITED();
1336 void *res = fast_memcpy(dest, src, n);
1337 __msan_copy_poison(dest, src, n);
1338 return res;
1339}
1340
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001341void *__msan_memset(void *s, int c, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001342 ENSURE_MSAN_INITED();
1343 void *res = fast_memset(s, c, n);
1344 __msan_unpoison(s, n);
1345 return res;
1346}
1347
Kostya Serebryany6afa1b02012-12-13 06:31:40 +00001348void *__msan_memmove(void *dest, const void *src, SIZE_T n) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001349 ENSURE_MSAN_INITED();
1350 void *res = REAL(memmove)(dest, src, n);
1351 __msan_move_poison(dest, src, n);
1352 return res;
1353}
1354
1355namespace __msan {
1356void InitializeInterceptors() {
1357 static int inited = 0;
1358 CHECK_EQ(inited, 0);
Kostya Serebryanyc20b3212013-01-18 06:43:13 +00001359 SANITIZER_COMMON_INTERCEPTORS_INIT;
1360
Alexey Samsonov8028c712012-12-14 11:52:02 +00001361 INTERCEPT_FUNCTION(mmap);
1362 INTERCEPT_FUNCTION(mmap64);
1363 INTERCEPT_FUNCTION(posix_memalign);
Evgeniy Stepanov97160a82013-09-02 09:24:53 +00001364 INTERCEPT_FUNCTION(memalign);
1365 INTERCEPT_FUNCTION(valloc);
1366 INTERCEPT_FUNCTION(pvalloc);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001367 INTERCEPT_FUNCTION(malloc);
1368 INTERCEPT_FUNCTION(calloc);
1369 INTERCEPT_FUNCTION(realloc);
1370 INTERCEPT_FUNCTION(free);
1371 INTERCEPT_FUNCTION(fread);
1372 INTERCEPT_FUNCTION(fread_unlocked);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001373 INTERCEPT_FUNCTION(readlink);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001374 INTERCEPT_FUNCTION(memcpy);
Evgeniy Stepanov353c9962013-07-02 14:49:24 +00001375 INTERCEPT_FUNCTION(mempcpy);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001376 INTERCEPT_FUNCTION(memset);
1377 INTERCEPT_FUNCTION(memmove);
Evgeniy Stepanovff6c9fb2013-07-04 13:19:41 +00001378 INTERCEPT_FUNCTION(bcopy);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001379 INTERCEPT_FUNCTION(wmemset);
1380 INTERCEPT_FUNCTION(wmemcpy);
Evgeniy Stepanov353c9962013-07-02 14:49:24 +00001381 INTERCEPT_FUNCTION(wmempcpy);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001382 INTERCEPT_FUNCTION(wmemmove);
1383 INTERCEPT_FUNCTION(strcpy); // NOLINT
Evgeniy Stepanov4bbbe132013-07-01 15:19:37 +00001384 INTERCEPT_FUNCTION(stpcpy); // NOLINT
Alexey Samsonov8028c712012-12-14 11:52:02 +00001385 INTERCEPT_FUNCTION(strdup);
Evgeniy Stepanov8aa1ae02013-03-14 11:10:36 +00001386 INTERCEPT_FUNCTION(__strdup);
1387 INTERCEPT_FUNCTION(strndup);
1388 INTERCEPT_FUNCTION(__strndup);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001389 INTERCEPT_FUNCTION(strncpy); // NOLINT
1390 INTERCEPT_FUNCTION(strlen);
1391 INTERCEPT_FUNCTION(strnlen);
1392 INTERCEPT_FUNCTION(gcvt);
1393 INTERCEPT_FUNCTION(strcat); // NOLINT
1394 INTERCEPT_FUNCTION(strncat); // NOLINT
1395 INTERCEPT_FUNCTION(strtol);
1396 INTERCEPT_FUNCTION(strtoll);
1397 INTERCEPT_FUNCTION(strtoul);
1398 INTERCEPT_FUNCTION(strtoull);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001399 INTERCEPT_FUNCTION(strtod);
Evgeniy Stepanov3f4beff2013-10-14 11:52:40 +00001400 INTERCEPT_FUNCTION(strtod_l);
1401 INTERCEPT_FUNCTION(__strtod_l);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001402 INTERCEPT_FUNCTION(strtof);
Evgeniy Stepanov3f4beff2013-10-14 11:52:40 +00001403 INTERCEPT_FUNCTION(strtof_l);
1404 INTERCEPT_FUNCTION(__strtof_l);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001405 INTERCEPT_FUNCTION(strtold);
Evgeniy Stepanov3f4beff2013-10-14 11:52:40 +00001406 INTERCEPT_FUNCTION(strtold_l);
1407 INTERCEPT_FUNCTION(__strtold_l);
Evgeniy Stepanov0797ed42013-06-21 13:32:26 +00001408 INTERCEPT_FUNCTION(vasprintf);
1409 INTERCEPT_FUNCTION(asprintf);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001410 INTERCEPT_FUNCTION(vsprintf);
1411 INTERCEPT_FUNCTION(vsnprintf);
1412 INTERCEPT_FUNCTION(vswprintf);
1413 INTERCEPT_FUNCTION(sprintf); // NOLINT
1414 INTERCEPT_FUNCTION(snprintf);
1415 INTERCEPT_FUNCTION(swprintf);
1416 INTERCEPT_FUNCTION(strftime);
Evgeniy Stepanov80144892013-07-02 13:34:44 +00001417 INTERCEPT_FUNCTION(mbtowc);
1418 INTERCEPT_FUNCTION(mbrtowc);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001419 INTERCEPT_FUNCTION(wcslen);
1420 INTERCEPT_FUNCTION(wcschr);
1421 INTERCEPT_FUNCTION(wcscpy);
1422 INTERCEPT_FUNCTION(wcscmp);
1423 INTERCEPT_FUNCTION(wcstod);
1424 INTERCEPT_FUNCTION(getenv);
Evgeniy Stepanov534e2ba2013-08-01 11:14:14 +00001425 INTERCEPT_FUNCTION(setenv);
1426 INTERCEPT_FUNCTION(putenv);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001427 INTERCEPT_FUNCTION(gettimeofday);
1428 INTERCEPT_FUNCTION(fcvt);
1429 INTERCEPT_FUNCTION(__fxstat);
Evgeniy Stepanovbb229422013-09-09 13:40:41 +00001430 INTERCEPT_FUNCTION(__fxstatat);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001431 INTERCEPT_FUNCTION(__xstat);
1432 INTERCEPT_FUNCTION(__lxstat);
1433 INTERCEPT_FUNCTION(__fxstat64);
Evgeniy Stepanovbb229422013-09-09 13:40:41 +00001434 INTERCEPT_FUNCTION(__fxstatat64);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001435 INTERCEPT_FUNCTION(__xstat64);
1436 INTERCEPT_FUNCTION(__lxstat64);
1437 INTERCEPT_FUNCTION(pipe);
Evgeniy Stepanov134fe8a2013-04-08 13:45:12 +00001438 INTERCEPT_FUNCTION(pipe2);
1439 INTERCEPT_FUNCTION(socketpair);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001440 INTERCEPT_FUNCTION(fgets);
1441 INTERCEPT_FUNCTION(fgets_unlocked);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001442 INTERCEPT_FUNCTION(getrlimit);
1443 INTERCEPT_FUNCTION(getrlimit64);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001444 INTERCEPT_FUNCTION(uname);
Evgeniy Stepanov95d05882013-01-23 10:43:38 +00001445 INTERCEPT_FUNCTION(gethostname);
Alexey Samsonov8028c712012-12-14 11:52:02 +00001446 INTERCEPT_FUNCTION(epoll_wait);
1447 INTERCEPT_FUNCTION(epoll_pwait);
1448 INTERCEPT_FUNCTION(recv);
1449 INTERCEPT_FUNCTION(recvfrom);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001450 INTERCEPT_FUNCTION(dladdr);
Reid Kleckner0f92deb2013-03-11 18:07:42 +00001451 INTERCEPT_FUNCTION(dlopen);
Evgeniy Stepanov2bba4ef2013-05-16 13:00:25 +00001452 INTERCEPT_FUNCTION(dl_iterate_phdr);
Evgeniy Stepanove03345b2013-01-17 13:42:17 +00001453 INTERCEPT_FUNCTION(getrusage);
Evgeniy Stepanove4bdda52013-04-01 14:47:21 +00001454 INTERCEPT_FUNCTION(sigaction);
Evgeniy Stepanov06658ea2013-04-04 08:22:52 +00001455 INTERCEPT_FUNCTION(signal);
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +00001456 INTERCEPT_FUNCTION(pthread_create);
Evgeniy Stepanov737da2f2013-07-05 12:31:07 +00001457 INTERCEPT_FUNCTION(pthread_key_create);
Evgeniy Stepanov65670922013-07-30 12:54:34 +00001458 INTERCEPT_FUNCTION(pthread_join);
Evgeniy Stepanov7b636082013-09-13 11:27:38 +00001459 INTERCEPT_FUNCTION(tzset);
Evgeniy Stepanovcfc29de2013-09-27 11:32:21 +00001460 INTERCEPT_FUNCTION(__cxa_atexit);
Evgeniy Stepanov7c6bd402013-10-22 14:31:30 +00001461
1462 if (REAL(pthread_key_create)(&g_thread_finalize_key, &thread_finalize)) {
1463 Printf("MemorySanitizer: failed to create thread key\n");
1464 Die();
1465 }
1466
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001467 inited = 1;
1468}
1469} // namespace __msan