blob: c13a95d9c9bcd39badf1aa7a748c30edebdb1833 [file] [log] [blame]
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001//===-- msan.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// MemorySanitizer runtime.
13//===----------------------------------------------------------------------===//
14
15#include "msan.h"
16#include "sanitizer_common/sanitizer_atomic.h"
17#include "sanitizer_common/sanitizer_common.h"
18#include "sanitizer_common/sanitizer_flags.h"
19#include "sanitizer_common/sanitizer_libc.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000020#include "sanitizer_common/sanitizer_procmaps.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000021#include "sanitizer_common/sanitizer_stacktrace.h"
22#include "sanitizer_common/sanitizer_symbolizer.h"
23
24#include "interception/interception.h"
25
26// ACHTUNG! No system header includes in this file.
27
28using namespace __sanitizer;
29
30// Globals.
31static THREADLOCAL int msan_expect_umr = 0;
32static THREADLOCAL int msan_expected_umr_found = 0;
33
34static int msan_running_under_dr = 0;
35
36SANITIZER_INTERFACE_ATTRIBUTE
37THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSizeInWords];
38
39SANITIZER_INTERFACE_ATTRIBUTE
40THREADLOCAL u32 __msan_param_origin_tls[kMsanParamTlsSizeInWords];
41
42SANITIZER_INTERFACE_ATTRIBUTE
43THREADLOCAL u64 __msan_retval_tls[kMsanRetvalTlsSizeInWords];
44
45SANITIZER_INTERFACE_ATTRIBUTE
46THREADLOCAL u32 __msan_retval_origin_tls;
47
48SANITIZER_INTERFACE_ATTRIBUTE
49THREADLOCAL u64 __msan_va_arg_tls[kMsanParamTlsSizeInWords];
50
51SANITIZER_INTERFACE_ATTRIBUTE
52THREADLOCAL u64 __msan_va_arg_overflow_size_tls;
53
54SANITIZER_INTERFACE_ATTRIBUTE
55THREADLOCAL u32 __msan_origin_tls;
56
57static THREADLOCAL struct {
58 uptr stack_top, stack_bottom;
59} __msan_stack_bounds;
60
Evgeniy Stepanov27ee7a82013-09-23 13:26:31 +000061static THREADLOCAL int is_in_symbolizer;
62static THREADLOCAL int is_in_loader;
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000063
Evgeniy Stepanova879f102013-05-31 13:04:07 +000064extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_track_origins;
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +000065
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000066int __msan_get_track_origins() {
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +000067 return &__msan_track_origins ? __msan_track_origins : 0;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000068}
69
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +000070extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_keep_going;
71
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000072namespace __msan {
73
74static bool IsRunningUnderDr() {
75 bool result = false;
Alexander Potapenko9ae28832013-03-26 10:34:37 +000076 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000077 const sptr kBufSize = 4095;
78 char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
79 while (proc_maps.Next(/* start */0, /* end */0, /* file_offset */0,
Alexey Samsonov45717c92013-03-13 06:51:02 +000080 filename, kBufSize, /* protection */0)) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000081 if (internal_strstr(filename, "libdynamorio") != 0) {
82 result = true;
83 break;
84 }
85 }
86 UnmapOrDie(filename, kBufSize);
87 return result;
88}
89
Evgeniy Stepanov27ee7a82013-09-23 13:26:31 +000090void EnterSymbolizer() { ++is_in_symbolizer; }
91void ExitSymbolizer() { --is_in_symbolizer; }
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +000092bool IsInSymbolizer() { return is_in_symbolizer; }
93
Evgeniy Stepanov29296c02013-09-23 13:34:26 +000094void EnterLoader() { ++is_in_loader; }
Evgeniy Stepanov27ee7a82013-09-23 13:26:31 +000095void ExitLoader() { --is_in_loader; }
Reid Kleckner0f92deb2013-03-11 18:07:42 +000096
97extern "C" {
98SANITIZER_INTERFACE_ATTRIBUTE
99bool __msan_is_in_loader() { return is_in_loader; }
100}
101
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000102static Flags msan_flags;
103
104Flags *flags() {
105 return &msan_flags;
106}
107
108int msan_inited = 0;
109bool msan_init_is_running;
110
Evgeniy Stepanov99bf1d72013-01-10 11:17:55 +0000111int msan_report_count = 0;
112
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000113// Array of stack origins.
114// FIXME: make it resizable.
115static const uptr kNumStackOriginDescrs = 1024 * 1024;
116static const char *StackOriginDescr[kNumStackOriginDescrs];
Evgeniy Stepanov6f346052013-09-13 12:49:13 +0000117static uptr StackOriginPC[kNumStackOriginDescrs];
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000118static atomic_uint32_t NumStackOriginDescrs;
119
120static void ParseFlagsFromString(Flags *f, const char *str) {
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +0000121 ParseCommonFlagsFromString(str);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000122 ParseFlag(str, &f->poison_heap_with_zeroes, "poison_heap_with_zeroes");
123 ParseFlag(str, &f->poison_stack_with_zeroes, "poison_stack_with_zeroes");
124 ParseFlag(str, &f->poison_in_malloc, "poison_in_malloc");
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000125 ParseFlag(str, &f->poison_in_malloc, "poison_in_free");
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000126 ParseFlag(str, &f->exit_code, "exit_code");
127 if (f->exit_code < 0 || f->exit_code > 127) {
128 Printf("Exit code not in [0, 128) range: %d\n", f->exit_code);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000129 Die();
130 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000131 ParseFlag(str, &f->report_umrs, "report_umrs");
Evgeniy Stepanova213ab62013-04-05 11:59:16 +0000132 ParseFlag(str, &f->wrap_signals, "wrap_signals");
Dmitry Vyukovdbac0a42013-08-13 15:33:00 +0000133
134 // keep_going is an old name for halt_on_error,
135 // and it has inverse meaning.
136 f->halt_on_error = !f->halt_on_error;
137 ParseFlag(str, &f->halt_on_error, "keep_going");
138 f->halt_on_error = !f->halt_on_error;
139 ParseFlag(str, &f->halt_on_error, "halt_on_error");
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000140}
141
142static void InitializeFlags(Flags *f, const char *options) {
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +0000143 CommonFlags *cf = common_flags();
144 cf->external_symbolizer_path = GetEnv("MSAN_SYMBOLIZER_PATH");
145 cf->strip_path_prefix = "";
146 cf->fast_unwind_on_fatal = false;
147 cf->fast_unwind_on_malloc = true;
148 cf->malloc_context_size = 20;
Evgeniy Stepanov745dd0d2013-06-07 13:00:47 +0000149 cf->handle_ioctl = true;
Evgeniy Stepanovb6246062013-06-25 13:50:44 +0000150 cf->log_path = 0;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000151
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +0000152 internal_memset(f, 0, sizeof(*f));
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000153 f->poison_heap_with_zeroes = false;
154 f->poison_stack_with_zeroes = false;
155 f->poison_in_malloc = true;
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000156 f->poison_in_free = true;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000157 f->exit_code = 77;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000158 f->report_umrs = true;
Evgeniy Stepanova213ab62013-04-05 11:59:16 +0000159 f->wrap_signals = true;
Dmitry Vyukovdbac0a42013-08-13 15:33:00 +0000160 f->halt_on_error = !&__msan_keep_going;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000161
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +0000162 // Override from user-specified string.
163 if (__msan_default_options)
164 ParseFlagsFromString(f, __msan_default_options());
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000165 ParseFlagsFromString(f, options);
166}
167
168static void GetCurrentStackBounds(uptr *stack_top, uptr *stack_bottom) {
169 if (__msan_stack_bounds.stack_top == 0) {
170 // Break recursion (GetStackTrace -> GetThreadStackTopAndBottom ->
171 // realloc -> GetStackTrace).
172 __msan_stack_bounds.stack_top = __msan_stack_bounds.stack_bottom = 1;
173 GetThreadStackTopAndBottom(/* at_initialization */false,
174 &__msan_stack_bounds.stack_top,
175 &__msan_stack_bounds.stack_bottom);
176 }
177 *stack_top = __msan_stack_bounds.stack_top;
178 *stack_bottom = __msan_stack_bounds.stack_bottom;
179}
180
Evgeniy Stepanovefbc4352013-02-19 12:43:18 +0000181void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp,
182 bool fast) {
Reid Kleckner93c26022013-03-06 16:11:58 +0000183 if (!fast) {
184 // Block reports from our interceptors during _Unwind_Backtrace.
185 SymbolizerScope sym_scope;
Evgeniy Stepanovefbc4352013-02-19 12:43:18 +0000186 return stack->SlowUnwindStack(pc, max_s);
Reid Kleckner93c26022013-03-06 16:11:58 +0000187 }
Evgeniy Stepanovefbc4352013-02-19 12:43:18 +0000188
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000189 uptr stack_top, stack_bottom;
190 GetCurrentStackBounds(&stack_top, &stack_bottom);
Alexey Samsonov064da322013-10-11 09:58:30 +0000191 stack->FastUnwindStack(pc, bp, stack_top, stack_bottom, max_s);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000192}
193
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000194void PrintWarning(uptr pc, uptr bp) {
195 PrintWarningWithOrigin(pc, bp, __msan_origin_tls);
196}
197
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000198bool OriginIsValid(u32 origin) {
199 return origin != 0 && origin != (u32)-1;
200}
201
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000202void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000203 if (msan_expect_umr) {
204 // Printf("Expected UMR\n");
205 __msan_origin_tls = origin;
206 msan_expected_umr_found = 1;
207 return;
208 }
209
Evgeniy Stepanov99bf1d72013-01-10 11:17:55 +0000210 ++msan_report_count;
211
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000212 StackTrace stack;
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +0000213 GetStackTrace(&stack, kStackTraceMax, pc, bp,
214 common_flags()->fast_unwind_on_fatal);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000215
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000216 u32 report_origin =
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +0000217 (__msan_get_track_origins() && OriginIsValid(origin)) ? origin : 0;
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000218 ReportUMR(&stack, report_origin);
219
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +0000220 if (__msan_get_track_origins() && !OriginIsValid(origin)) {
Evgeniy Stepanov48ed0582013-10-24 11:52:48 +0000221 Printf(
222 " ORIGIN: invalid (%x). Might be a bug in MemorySanitizer origin "
223 "tracking.\n This could still be a bug in your code, too!\n",
224 origin);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000225 }
226}
227
Alexey Samsonovc2918bf2013-06-27 07:50:56 +0000228void UnpoisonParam(uptr n) {
229 internal_memset(__msan_param_tls, 0, n * sizeof(*__msan_param_tls));
230}
231
Evgeniy Stepanov0e38a672013-08-27 14:08:15 +0000232// Backup MSan runtime TLS state.
233// Implementation must be async-signal-safe.
234// Instances of this class may live on the signal handler stack, and data size
235// may be an issue.
236void ScopedThreadLocalStateBackup::Backup() {
237 va_arg_overflow_size_tls = __msan_va_arg_overflow_size_tls;
238}
239
240void ScopedThreadLocalStateBackup::Restore() {
241 // A lame implementation that only keeps essential state and resets the rest.
242 __msan_va_arg_overflow_size_tls = va_arg_overflow_size_tls;
243
Evgeniy Stepanov91659d52013-08-27 12:59:39 +0000244 internal_memset(__msan_param_tls, 0, sizeof(__msan_param_tls));
245 internal_memset(__msan_retval_tls, 0, sizeof(__msan_retval_tls));
246 internal_memset(__msan_va_arg_tls, 0, sizeof(__msan_va_arg_tls));
Evgeniy Stepanov0e38a672013-08-27 14:08:15 +0000247
248 if (__msan_get_track_origins()) {
249 internal_memset(&__msan_retval_origin_tls, 0, sizeof(__msan_retval_tls));
Alexey Samsonovc1548202013-08-28 11:26:09 +0000250 internal_memset(__msan_param_origin_tls, 0,
251 sizeof(__msan_param_origin_tls));
Evgeniy Stepanov0e38a672013-08-27 14:08:15 +0000252 }
253}
254
255void UnpoisonThreadLocalState() {
Evgeniy Stepanov91659d52013-08-27 12:59:39 +0000256}
257
Evgeniy Stepanov6f346052013-09-13 12:49:13 +0000258const char *GetOriginDescrIfStack(u32 id, uptr *pc) {
259 if ((id >> 31) == 0) return 0;
260 id &= (1U << 31) - 1;
261 CHECK_LT(id, kNumStackOriginDescrs);
262 if (pc) *pc = StackOriginPC[id];
263 return StackOriginDescr[id];
264}
265
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000266} // namespace __msan
267
268// Interface.
269
270using namespace __msan;
271
272void __msan_warning() {
273 GET_CALLER_PC_BP_SP;
274 (void)sp;
275 PrintWarning(pc, bp);
Dmitry Vyukovdbac0a42013-08-13 15:33:00 +0000276 if (__msan::flags()->halt_on_error) {
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +0000277 Printf("Exiting\n");
278 Die();
279 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000280}
281
282void __msan_warning_noreturn() {
283 GET_CALLER_PC_BP_SP;
284 (void)sp;
285 PrintWarning(pc, bp);
286 Printf("Exiting\n");
287 Die();
288}
289
290void __msan_init() {
291 if (msan_inited) return;
292 msan_init_is_running = 1;
Kostya Serebryany859778a2013-01-31 14:11:21 +0000293 SanitizerToolName = "MemorySanitizer";
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000294
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000295 SetDieCallback(MsanDie);
Evgeniy Stepanov10fd3222013-03-13 09:01:40 +0000296 InitTlsSize();
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000297 InitializeInterceptors();
Evgeniy Stepanovcfc29de2013-09-27 11:32:21 +0000298 InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000299
Evgeniy Stepanova8974002013-04-05 12:03:47 +0000300 if (MSAN_REPLACE_OPERATORS_NEW_AND_DELETE)
301 ReplaceOperatorsNewAndDelete();
Evgeniy Stepanovf35eae82013-02-19 11:09:29 +0000302 const char *msan_options = GetEnv("MSAN_OPTIONS");
303 InitializeFlags(&msan_flags, msan_options);
Evgeniy Stepanovb6246062013-06-25 13:50:44 +0000304 __sanitizer_set_report_path(common_flags()->log_path);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000305 if (StackSizeIsUnlimited()) {
Dmitry Vyukov6866dba2013-10-15 13:28:51 +0000306 if (common_flags()->verbosity)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000307 Printf("Unlimited stack, doing reexec\n");
308 // A reasonably large stack size. It is bigger than the usual 8Mb, because,
309 // well, the program could have been run with unlimited stack for a reason.
310 SetStackSizeLimitInBytes(32 * 1024 * 1024);
311 ReExec();
312 }
Evgeniy Stepanovf35eae82013-02-19 11:09:29 +0000313
Dmitry Vyukov6866dba2013-10-15 13:28:51 +0000314 if (common_flags()->verbosity)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000315 Printf("MSAN_OPTIONS: %s\n", msan_options ? msan_options : "<empty>");
Evgeniy Stepanovf35eae82013-02-19 11:09:29 +0000316
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000317 msan_running_under_dr = IsRunningUnderDr();
318 __msan_clear_on_return();
Dmitry Vyukov6866dba2013-10-15 13:28:51 +0000319 if (__msan_get_track_origins() && common_flags()->verbosity > 0)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000320 Printf("msan_track_origins\n");
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +0000321 if (!InitShadow(/* prot1 */ false, /* prot2 */ true, /* map_shadow */ true,
322 __msan_get_track_origins())) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000323 // FIXME: prot1 = false is only required when running under DR.
Evgeniy Stepanov4c9ddc12012-12-26 06:37:23 +0000324 Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n");
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000325 Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
Evgeniy Stepanov4c9ddc12012-12-26 06:37:23 +0000326 Printf("FATAL: Disabling ASLR is known to cause this error.\n");
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000327 Printf("FATAL: If running under GDB, try "
328 "'set disable-randomization off'.\n");
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000329 DumpProcessMap();
330 Die();
331 }
332
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +0000333 const char *external_symbolizer = common_flags()->external_symbolizer_path;
Alexey Samsonov7847d772013-09-10 14:36:16 +0000334 bool symbolizer_started =
335 getSymbolizer()->InitializeExternal(external_symbolizer);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000336 if (external_symbolizer && external_symbolizer[0]) {
Alexey Samsonov68c016a2013-09-03 13:22:51 +0000337 CHECK(symbolizer_started);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000338 }
339
340 GetThreadStackTopAndBottom(/* at_initialization */true,
341 &__msan_stack_bounds.stack_top,
342 &__msan_stack_bounds.stack_bottom);
Dmitry Vyukov6866dba2013-10-15 13:28:51 +0000343 if (common_flags()->verbosity)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000344 Printf("MemorySanitizer init done\n");
345 msan_init_is_running = 0;
346 msan_inited = 1;
347}
348
349void __msan_set_exit_code(int exit_code) {
350 flags()->exit_code = exit_code;
351}
352
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +0000353void __msan_set_keep_going(int keep_going) {
Dmitry Vyukovdbac0a42013-08-13 15:33:00 +0000354 flags()->halt_on_error = !keep_going;
Evgeniy Stepanovbb881c72013-06-21 12:37:58 +0000355}
356
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000357void __msan_set_expect_umr(int expect_umr) {
358 if (expect_umr) {
359 msan_expected_umr_found = 0;
360 } else if (!msan_expected_umr_found) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000361 GET_CALLER_PC_BP_SP;
362 (void)sp;
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000363 StackTrace stack;
Evgeniy Stepanov58b52b52013-03-14 11:47:03 +0000364 GetStackTrace(&stack, kStackTraceMax, pc, bp,
Sergey Matveev0b4bf4d2013-05-06 13:15:14 +0000365 common_flags()->fast_unwind_on_fatal);
Evgeniy Stepanovdb010da2012-12-26 09:32:05 +0000366 ReportExpectedUMRNotFound(&stack);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000367 Die();
368 }
369 msan_expect_umr = expect_umr;
370}
371
372void __msan_print_shadow(const void *x, uptr size) {
373 unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x);
374 u32 *o = (u32*)MEM_TO_ORIGIN(x);
375 for (uptr i = 0; i < size; i++) {
376 Printf("%x%x ", s[i] >> 4, s[i] & 0xf);
377 }
378 Printf("\n");
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +0000379 if (__msan_get_track_origins()) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000380 for (uptr i = 0; i < size / 4; i++) {
381 Printf(" o: %x ", o[i]);
382 }
383 Printf("\n");
384 }
385}
386
387void __msan_print_param_shadow() {
388 for (int i = 0; i < 16; i++) {
389 Printf("#%d:%zx ", i, __msan_param_tls[i]);
390 }
391 Printf("\n");
392}
393
394sptr __msan_test_shadow(const void *x, uptr size) {
395 unsigned char *s = (unsigned char*)MEM_TO_SHADOW((uptr)x);
396 for (uptr i = 0; i < size; ++i)
397 if (s[i])
398 return i;
399 return -1;
400}
401
402int __msan_set_poison_in_malloc(int do_poison) {
403 int old = flags()->poison_in_malloc;
404 flags()->poison_in_malloc = do_poison;
405 return old;
406}
407
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000408int __msan_has_dynamic_component() {
409 return msan_running_under_dr;
410}
411
412NOINLINE
413void __msan_clear_on_return() {
414 __msan_param_tls[0] = 0;
415}
416
417static void* get_tls_base() {
418 u64 p;
419 asm("mov %%fs:0, %0"
420 : "=r"(p) ::);
421 return (void*)p;
422}
423
424int __msan_get_retval_tls_offset() {
425 // volatile here is needed to avoid UB, because the compiler thinks that we
426 // are doing address arithmetics on unrelated pointers, and takes some
427 // shortcuts
428 volatile sptr retval_tls_p = (sptr)&__msan_retval_tls;
429 volatile sptr tls_base_p = (sptr)get_tls_base();
430 return retval_tls_p - tls_base_p;
431}
432
433int __msan_get_param_tls_offset() {
434 // volatile here is needed to avoid UB, because the compiler thinks that we
435 // are doing address arithmetics on unrelated pointers, and takes some
436 // shortcuts
437 volatile sptr param_tls_p = (sptr)&__msan_param_tls;
438 volatile sptr tls_base_p = (sptr)get_tls_base();
439 return param_tls_p - tls_base_p;
440}
441
Alexey Samsonov11347bf2013-04-23 13:34:19 +0000442void __msan_partial_poison(const void* data, void* shadow, uptr size) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000443 internal_memcpy((void*)MEM_TO_SHADOW((uptr)data), shadow, size);
444}
445
446void __msan_load_unpoisoned(void *src, uptr size, void *dst) {
447 internal_memcpy(dst, src, size);
448 __msan_unpoison(dst, size);
449}
450
Alexey Samsonov11347bf2013-04-23 13:34:19 +0000451void __msan_set_origin(const void *a, uptr size, u32 origin) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000452 // Origin mapping is 4 bytes per 4 bytes of application memory.
453 // Here we extend the range such that its left and right bounds are both
454 // 4 byte aligned.
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +0000455 if (!__msan_get_track_origins()) return;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000456 uptr x = MEM_TO_ORIGIN((uptr)a);
457 uptr beg = x & ~3UL; // align down.
458 uptr end = (x + size + 3) & ~3UL; // align up.
459 u64 origin64 = ((u64)origin << 32) | origin;
460 // This is like memset, but the value is 32-bit. We unroll by 2 two write
461 // 64-bits at once. May want to unroll further to get 128-bit stores.
462 if (beg & 7ULL) {
463 *(u32*)beg = origin;
464 beg += 4;
465 }
466 for (uptr addr = beg; addr < (end & ~7UL); addr += 8)
467 *(u64*)addr = origin64;
468 if (end & 7ULL)
469 *(u32*)(end - 4) = origin;
470}
471
472// 'descr' is created at compile time and contains '----' in the beginning.
473// When we see descr for the first time we replace '----' with a uniq id
474// and set the origin to (id | (31-th bit)).
475void __msan_set_alloca_origin(void *a, uptr size, const char *descr) {
Evgeniy Stepanov6f346052013-09-13 12:49:13 +0000476 __msan_set_alloca_origin4(a, size, descr, 0);
477}
478
479void __msan_set_alloca_origin4(void *a, uptr size, const char *descr, uptr pc) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000480 static const u32 dash = '-';
481 static const u32 first_timer =
482 dash + (dash << 8) + (dash << 16) + (dash << 24);
483 u32 *id_ptr = (u32*)descr;
484 bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0;
485 u32 id = *id_ptr;
486 if (id == first_timer) {
487 id = atomic_fetch_add(&NumStackOriginDescrs,
488 1, memory_order_relaxed);
489 *id_ptr = id;
490 CHECK_LT(id, kNumStackOriginDescrs);
491 StackOriginDescr[id] = descr + 4;
Evgeniy Stepanov6f346052013-09-13 12:49:13 +0000492 StackOriginPC[id] = pc;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000493 if (print)
Evgeniy Stepanov6f346052013-09-13 12:49:13 +0000494 Printf("First time: id=%d %s %p \n", id, descr + 4, pc);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000495 }
496 id |= 1U << 31;
497 if (print)
498 Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id);
499 __msan_set_origin(a, size, id);
500}
501
502const char *__msan_get_origin_descr_if_stack(u32 id) {
Evgeniy Stepanov6f346052013-09-13 12:49:13 +0000503 return GetOriginDescrIfStack(id, 0);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000504}
505
Alexey Samsonov11347bf2013-04-23 13:34:19 +0000506u32 __msan_get_origin(const void *a) {
Evgeniy Stepanovb36779d2013-05-31 12:04:08 +0000507 if (!__msan_get_track_origins()) return 0;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000508 uptr x = (uptr)a;
509 uptr aligned = x & ~3ULL;
510 uptr origin_ptr = MEM_TO_ORIGIN(aligned);
511 return *(u32*)origin_ptr;
512}
513
Evgeniy Stepanov12c46932013-01-29 14:33:29 +0000514u32 __msan_get_umr_origin() {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000515 return __msan_origin_tls;
516}
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +0000517
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000518u16 __sanitizer_unaligned_load16(const uu16 *p) {
519 __msan_retval_tls[0] = *(uu16 *)MEM_TO_SHADOW((uptr)p);
Evgeniy Stepanov7ad195c2013-10-16 08:25:13 +0000520 if (__msan_get_track_origins())
521 __msan_retval_origin_tls = *(uu32 *)MEM_TO_ORIGIN((uptr)p);
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000522 return *p;
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +0000523}
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000524u32 __sanitizer_unaligned_load32(const uu32 *p) {
525 __msan_retval_tls[0] = *(uu32 *)MEM_TO_SHADOW((uptr)p);
Evgeniy Stepanov7ad195c2013-10-16 08:25:13 +0000526 if (__msan_get_track_origins())
527 __msan_retval_origin_tls = *(uu32 *)MEM_TO_ORIGIN((uptr)p);
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000528 return *p;
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +0000529}
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000530u64 __sanitizer_unaligned_load64(const uu64 *p) {
531 __msan_retval_tls[0] = *(uu64 *)MEM_TO_SHADOW((uptr)p);
Evgeniy Stepanov7ad195c2013-10-16 08:25:13 +0000532 if (__msan_get_track_origins())
533 __msan_retval_origin_tls = *(uu32 *)MEM_TO_ORIGIN((uptr)p);
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000534 return *p;
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +0000535}
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000536void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
537 *(uu16 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1];
Evgeniy Stepanov7ad195c2013-10-16 08:25:13 +0000538 if (__msan_get_track_origins())
539 *(uu32 *)MEM_TO_ORIGIN((uptr)p) = __msan_param_origin_tls[1];
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000540 *p = x;
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +0000541}
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000542void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
543 *(uu32 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1];
Evgeniy Stepanov7ad195c2013-10-16 08:25:13 +0000544 if (__msan_get_track_origins())
545 *(uu32 *)MEM_TO_ORIGIN((uptr)p) = __msan_param_origin_tls[1];
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000546 *p = x;
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +0000547}
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000548void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
549 *(uu64 *)MEM_TO_SHADOW((uptr)p) = __msan_param_tls[1];
Evgeniy Stepanov7ad195c2013-10-16 08:25:13 +0000550 if (__msan_get_track_origins())
551 *(uu32 *)MEM_TO_ORIGIN((uptr)p) = __msan_param_origin_tls[1];
Evgeniy Stepanov2e9ffcb2013-06-04 13:49:10 +0000552 *p = x;
Evgeniy Stepanovf43f6022013-06-04 13:08:36 +0000553}
554
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +0000555#if !SANITIZER_SUPPORTS_WEAK_HOOKS
556extern "C" {
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +0000557SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
Kostya Serebryany70c6e3f2013-02-13 07:19:47 +0000558const char* __msan_default_options() { return ""; }
559} // extern "C"
560#endif
561