2012-12-11 20:27:27 +08:00
|
|
|
//===-- msan.cc -----------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of MemorySanitizer.
|
|
|
|
//
|
|
|
|
// MemorySanitizer runtime.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "msan.h"
|
2014-04-04 17:47:41 +08:00
|
|
|
#include "msan_thread.h"
|
2012-12-11 20:27:27 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
2014-03-18 21:45:19 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2012-12-11 20:27:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
// ACHTUNG! No system header includes in this file.
|
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
|
|
|
|
// Globals.
|
|
|
|
static THREADLOCAL int msan_expect_umr = 0;
|
|
|
|
static THREADLOCAL int msan_expected_umr_found = 0;
|
|
|
|
|
2013-12-20 19:05:19 +08:00
|
|
|
static bool msan_running_under_dr;
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-11-19 22:47:56 +08:00
|
|
|
// Function argument shadow. Each argument starts at the next available 8-byte
|
|
|
|
// aligned address.
|
2012-12-11 20:27:27 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSizeInWords];
|
|
|
|
|
2013-11-19 22:47:56 +08:00
|
|
|
// Function argument origin. Each argument starts at the same offset as the
|
|
|
|
// corresponding shadow in (__msan_param_tls). Slightly weird, but changing this
|
|
|
|
// would break compatibility with older prebuilt binaries.
|
2012-12-11 20:27:27 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u32 __msan_param_origin_tls[kMsanParamTlsSizeInWords];
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u64 __msan_retval_tls[kMsanRetvalTlsSizeInWords];
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u32 __msan_retval_origin_tls;
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u64 __msan_va_arg_tls[kMsanParamTlsSizeInWords];
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u64 __msan_va_arg_overflow_size_tls;
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
THREADLOCAL u32 __msan_origin_tls;
|
|
|
|
|
2013-09-23 21:26:31 +08:00
|
|
|
static THREADLOCAL int is_in_symbolizer;
|
|
|
|
static THREADLOCAL int is_in_loader;
|
2013-02-13 15:19:47 +08:00
|
|
|
|
2013-05-31 21:04:07 +08:00
|
|
|
extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_track_origins;
|
2013-05-31 20:04:08 +08:00
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
int __msan_get_track_origins() {
|
2013-05-31 20:04:08 +08:00
|
|
|
return &__msan_track_origins ? __msan_track_origins : 0;
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:37:58 +08:00
|
|
|
extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_keep_going;
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
namespace __msan {
|
|
|
|
|
2013-09-23 21:26:31 +08:00
|
|
|
void EnterSymbolizer() { ++is_in_symbolizer; }
|
|
|
|
void ExitSymbolizer() { --is_in_symbolizer; }
|
2013-02-13 15:19:47 +08:00
|
|
|
bool IsInSymbolizer() { return is_in_symbolizer; }
|
|
|
|
|
2013-09-23 21:34:26 +08:00
|
|
|
void EnterLoader() { ++is_in_loader; }
|
2013-09-23 21:26:31 +08:00
|
|
|
void ExitLoader() { --is_in_loader; }
|
2013-03-12 02:07:42 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
bool __msan_is_in_loader() { return is_in_loader; }
|
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
static Flags msan_flags;
|
|
|
|
|
|
|
|
Flags *flags() {
|
|
|
|
return &msan_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msan_inited = 0;
|
|
|
|
bool msan_init_is_running;
|
|
|
|
|
2013-01-10 19:17:55 +08:00
|
|
|
int msan_report_count = 0;
|
|
|
|
|
2014-03-27 22:04:58 +08:00
|
|
|
void (*death_callback)(void);
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
// Array of stack origins.
|
|
|
|
// FIXME: make it resizable.
|
|
|
|
static const uptr kNumStackOriginDescrs = 1024 * 1024;
|
|
|
|
static const char *StackOriginDescr[kNumStackOriginDescrs];
|
2013-09-13 20:49:13 +08:00
|
|
|
static uptr StackOriginPC[kNumStackOriginDescrs];
|
2012-12-11 20:27:27 +08:00
|
|
|
static atomic_uint32_t NumStackOriginDescrs;
|
|
|
|
|
|
|
|
static void ParseFlagsFromString(Flags *f, const char *str) {
|
2013-11-27 17:54:10 +08:00
|
|
|
CommonFlags *cf = common_flags();
|
|
|
|
ParseCommonFlagsFromString(cf, str);
|
2014-03-20 20:52:52 +08:00
|
|
|
ParseFlag(str, &f->poison_heap_with_zeroes, "poison_heap_with_zeroes", "");
|
|
|
|
ParseFlag(str, &f->poison_stack_with_zeroes, "poison_stack_with_zeroes", "");
|
|
|
|
ParseFlag(str, &f->poison_in_malloc, "poison_in_malloc", "");
|
|
|
|
ParseFlag(str, &f->poison_in_free, "poison_in_free", "");
|
|
|
|
ParseFlag(str, &f->exit_code, "exit_code", "");
|
2012-12-11 20:27:27 +08:00
|
|
|
if (f->exit_code < 0 || f->exit_code > 127) {
|
|
|
|
Printf("Exit code not in [0, 128) range: %d\n", f->exit_code);
|
|
|
|
Die();
|
|
|
|
}
|
2014-03-20 20:52:52 +08:00
|
|
|
ParseFlag(str, &f->report_umrs, "report_umrs", "");
|
|
|
|
ParseFlag(str, &f->wrap_signals, "wrap_signals", "");
|
2013-08-13 23:33:00 +08:00
|
|
|
|
|
|
|
// keep_going is an old name for halt_on_error,
|
|
|
|
// and it has inverse meaning.
|
|
|
|
f->halt_on_error = !f->halt_on_error;
|
2014-03-20 20:52:52 +08:00
|
|
|
ParseFlag(str, &f->halt_on_error, "keep_going", "");
|
2013-08-13 23:33:00 +08:00
|
|
|
f->halt_on_error = !f->halt_on_error;
|
2014-03-20 20:52:52 +08:00
|
|
|
ParseFlag(str, &f->halt_on_error, "halt_on_error", "");
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void InitializeFlags(Flags *f, const char *options) {
|
2013-05-06 21:15:14 +08:00
|
|
|
CommonFlags *cf = common_flags();
|
2013-11-27 17:54:10 +08:00
|
|
|
SetCommonFlagsDefaults(cf);
|
2013-05-06 21:15:14 +08:00
|
|
|
cf->external_symbolizer_path = GetEnv("MSAN_SYMBOLIZER_PATH");
|
|
|
|
cf->malloc_context_size = 20;
|
2013-06-07 21:00:47 +08:00
|
|
|
cf->handle_ioctl = true;
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-05-06 21:15:14 +08:00
|
|
|
internal_memset(f, 0, sizeof(*f));
|
2012-12-11 20:27:27 +08:00
|
|
|
f->poison_heap_with_zeroes = false;
|
|
|
|
f->poison_stack_with_zeroes = false;
|
|
|
|
f->poison_in_malloc = true;
|
2013-09-16 19:03:31 +08:00
|
|
|
f->poison_in_free = true;
|
2012-12-11 20:27:27 +08:00
|
|
|
f->exit_code = 77;
|
|
|
|
f->report_umrs = true;
|
2013-04-05 19:59:16 +08:00
|
|
|
f->wrap_signals = true;
|
2013-08-13 23:33:00 +08:00
|
|
|
f->halt_on_error = !&__msan_keep_going;
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-02-13 15:19:47 +08:00
|
|
|
// Override from user-specified string.
|
|
|
|
if (__msan_default_options)
|
|
|
|
ParseFlagsFromString(f, __msan_default_options());
|
2012-12-11 20:27:27 +08:00
|
|
|
ParseFlagsFromString(f, options);
|
|
|
|
}
|
|
|
|
|
2013-02-19 20:43:18 +08:00
|
|
|
void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp,
|
2013-11-07 15:28:33 +08:00
|
|
|
bool request_fast_unwind) {
|
2014-04-04 17:47:41 +08:00
|
|
|
MsanThread *t = GetCurrentThread();
|
|
|
|
if (!t || !StackTrace::WillUseFastUnwind(request_fast_unwind)) {
|
2013-03-07 00:11:58 +08:00
|
|
|
// Block reports from our interceptors during _Unwind_Backtrace.
|
|
|
|
SymbolizerScope sym_scope;
|
2014-02-11 21:38:57 +08:00
|
|
|
return stack->Unwind(max_s, pc, bp, 0, 0, 0, request_fast_unwind);
|
2013-03-07 00:11:58 +08:00
|
|
|
}
|
2014-04-04 17:47:41 +08:00
|
|
|
stack->Unwind(max_s, pc, bp, 0, t->stack_top(), t->stack_bottom(),
|
|
|
|
request_fast_unwind);
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintWarning(uptr pc, uptr bp) {
|
|
|
|
PrintWarningWithOrigin(pc, bp, __msan_origin_tls);
|
|
|
|
}
|
|
|
|
|
2012-12-26 17:32:05 +08:00
|
|
|
bool OriginIsValid(u32 origin) {
|
|
|
|
return origin != 0 && origin != (u32)-1;
|
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) {
|
|
|
|
if (msan_expect_umr) {
|
|
|
|
// Printf("Expected UMR\n");
|
|
|
|
__msan_origin_tls = origin;
|
|
|
|
msan_expected_umr_found = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-10 19:17:55 +08:00
|
|
|
++msan_report_count;
|
|
|
|
|
2012-12-26 17:32:05 +08:00
|
|
|
StackTrace stack;
|
2013-05-06 21:15:14 +08:00
|
|
|
GetStackTrace(&stack, kStackTraceMax, pc, bp,
|
|
|
|
common_flags()->fast_unwind_on_fatal);
|
2012-12-26 17:32:05 +08:00
|
|
|
|
|
|
|
u32 report_origin =
|
2013-05-31 20:04:08 +08:00
|
|
|
(__msan_get_track_origins() && OriginIsValid(origin)) ? origin : 0;
|
2012-12-26 17:32:05 +08:00
|
|
|
ReportUMR(&stack, report_origin);
|
|
|
|
|
2013-05-31 20:04:08 +08:00
|
|
|
if (__msan_get_track_origins() && !OriginIsValid(origin)) {
|
2013-10-24 19:52:48 +08:00
|
|
|
Printf(
|
|
|
|
" ORIGIN: invalid (%x). Might be a bug in MemorySanitizer origin "
|
|
|
|
"tracking.\n This could still be a bug in your code, too!\n",
|
|
|
|
origin);
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:50:56 +08:00
|
|
|
void UnpoisonParam(uptr n) {
|
|
|
|
internal_memset(__msan_param_tls, 0, n * sizeof(*__msan_param_tls));
|
|
|
|
}
|
|
|
|
|
2013-08-27 22:08:15 +08:00
|
|
|
// Backup MSan runtime TLS state.
|
|
|
|
// Implementation must be async-signal-safe.
|
|
|
|
// Instances of this class may live on the signal handler stack, and data size
|
|
|
|
// may be an issue.
|
|
|
|
void ScopedThreadLocalStateBackup::Backup() {
|
|
|
|
va_arg_overflow_size_tls = __msan_va_arg_overflow_size_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopedThreadLocalStateBackup::Restore() {
|
|
|
|
// A lame implementation that only keeps essential state and resets the rest.
|
|
|
|
__msan_va_arg_overflow_size_tls = va_arg_overflow_size_tls;
|
|
|
|
|
2013-08-27 20:59:39 +08:00
|
|
|
internal_memset(__msan_param_tls, 0, sizeof(__msan_param_tls));
|
|
|
|
internal_memset(__msan_retval_tls, 0, sizeof(__msan_retval_tls));
|
|
|
|
internal_memset(__msan_va_arg_tls, 0, sizeof(__msan_va_arg_tls));
|
2013-08-27 22:08:15 +08:00
|
|
|
|
|
|
|
if (__msan_get_track_origins()) {
|
2014-02-03 15:27:01 +08:00
|
|
|
internal_memset(&__msan_retval_origin_tls, 0,
|
|
|
|
sizeof(__msan_retval_origin_tls));
|
2013-08-28 19:26:09 +08:00
|
|
|
internal_memset(__msan_param_origin_tls, 0,
|
|
|
|
sizeof(__msan_param_origin_tls));
|
2013-08-27 22:08:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnpoisonThreadLocalState() {
|
2013-08-27 20:59:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 20:49:13 +08:00
|
|
|
const char *GetOriginDescrIfStack(u32 id, uptr *pc) {
|
|
|
|
if ((id >> 31) == 0) return 0;
|
|
|
|
id &= (1U << 31) - 1;
|
|
|
|
CHECK_LT(id, kNumStackOriginDescrs);
|
|
|
|
if (pc) *pc = StackOriginPC[id];
|
|
|
|
return StackOriginDescr[id];
|
|
|
|
}
|
|
|
|
|
2014-03-18 21:45:19 +08:00
|
|
|
u32 ChainOrigin(u32 id, StackTrace *stack) {
|
2014-04-23 22:01:57 +08:00
|
|
|
if (GetCurrentThread()->InSignalHandler())
|
|
|
|
return id;
|
2014-03-18 21:45:19 +08:00
|
|
|
uptr idx = Min(stack->size, kStackTraceMax - 1);
|
|
|
|
stack->trace[idx] = TRACE_MAKE_CHAINED(id);
|
|
|
|
u32 new_id = StackDepotPut(stack->trace, idx + 1);
|
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
} // namespace __msan
|
|
|
|
|
|
|
|
// Interface.
|
|
|
|
|
|
|
|
using namespace __msan;
|
|
|
|
|
2014-04-18 20:15:24 +08:00
|
|
|
#define MSAN_MAYBE_WARNING(type, size) \
|
|
|
|
void __msan_maybe_warning_##size(type s, u32 o) { \
|
|
|
|
GET_CALLER_PC_BP_SP; \
|
|
|
|
(void) sp; \
|
|
|
|
if (UNLIKELY(s)) { \
|
|
|
|
PrintWarningWithOrigin(pc, bp, o); \
|
|
|
|
if (__msan::flags()->halt_on_error) { \
|
|
|
|
Printf("Exiting\n"); \
|
|
|
|
Die(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
MSAN_MAYBE_WARNING(u8, 1)
|
|
|
|
MSAN_MAYBE_WARNING(u16, 2)
|
|
|
|
MSAN_MAYBE_WARNING(u32, 4)
|
|
|
|
MSAN_MAYBE_WARNING(u64, 8)
|
|
|
|
|
|
|
|
#define MSAN_MAYBE_STORE_ORIGIN(type, size) \
|
|
|
|
void __msan_maybe_store_origin_##size(type s, void *p, u32 o) { \
|
|
|
|
if (UNLIKELY(s)) *(u32 *)MEM_TO_ORIGIN((uptr)p &~3UL) = o; \
|
|
|
|
}
|
|
|
|
|
|
|
|
MSAN_MAYBE_STORE_ORIGIN(u8, 1)
|
|
|
|
MSAN_MAYBE_STORE_ORIGIN(u16, 2)
|
|
|
|
MSAN_MAYBE_STORE_ORIGIN(u32, 4)
|
|
|
|
MSAN_MAYBE_STORE_ORIGIN(u64, 8)
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
void __msan_warning() {
|
|
|
|
GET_CALLER_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
PrintWarning(pc, bp);
|
2013-08-13 23:33:00 +08:00
|
|
|
if (__msan::flags()->halt_on_error) {
|
2013-06-21 20:37:58 +08:00
|
|
|
Printf("Exiting\n");
|
|
|
|
Die();
|
|
|
|
}
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_warning_noreturn() {
|
|
|
|
GET_CALLER_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
PrintWarning(pc, bp);
|
|
|
|
Printf("Exiting\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_init() {
|
2013-12-13 17:11:14 +08:00
|
|
|
CHECK(!msan_init_is_running);
|
2012-12-11 20:27:27 +08:00
|
|
|
if (msan_inited) return;
|
|
|
|
msan_init_is_running = 1;
|
2013-01-31 22:11:21 +08:00
|
|
|
SanitizerToolName = "MemorySanitizer";
|
2012-12-11 20:27:27 +08:00
|
|
|
|
|
|
|
SetDieCallback(MsanDie);
|
2013-03-13 17:01:40 +08:00
|
|
|
InitTlsSize();
|
2013-11-11 19:28:30 +08:00
|
|
|
|
|
|
|
const char *msan_options = GetEnv("MSAN_OPTIONS");
|
|
|
|
InitializeFlags(&msan_flags, msan_options);
|
2014-03-20 20:52:52 +08:00
|
|
|
if (common_flags()->help) PrintFlagDescriptions();
|
2013-11-11 19:28:30 +08:00
|
|
|
__sanitizer_set_report_path(common_flags()->log_path);
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
InitializeInterceptors();
|
2013-09-27 19:32:21 +08:00
|
|
|
InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-04-05 20:03:47 +08:00
|
|
|
if (MSAN_REPLACE_OPERATORS_NEW_AND_DELETE)
|
|
|
|
ReplaceOperatorsNewAndDelete();
|
2012-12-11 20:27:27 +08:00
|
|
|
if (StackSizeIsUnlimited()) {
|
2013-12-05 20:04:51 +08:00
|
|
|
VPrintf(1, "Unlimited stack, doing reexec\n");
|
2012-12-11 20:27:27 +08:00
|
|
|
// A reasonably large stack size. It is bigger than the usual 8Mb, because,
|
|
|
|
// well, the program could have been run with unlimited stack for a reason.
|
|
|
|
SetStackSizeLimitInBytes(32 * 1024 * 1024);
|
|
|
|
ReExec();
|
|
|
|
}
|
2013-02-19 19:09:29 +08:00
|
|
|
|
2013-12-05 20:04:51 +08:00
|
|
|
VPrintf(1, "MSAN_OPTIONS: %s\n", msan_options ? msan_options : "<empty>");
|
2013-02-19 19:09:29 +08:00
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
__msan_clear_on_return();
|
2013-12-05 20:04:51 +08:00
|
|
|
if (__msan_get_track_origins())
|
|
|
|
VPrintf(1, "msan_track_origins\n");
|
2014-04-11 20:04:29 +08:00
|
|
|
if (!InitShadow(/* prot1 */ !msan_running_under_dr, /* prot2 */ true,
|
|
|
|
/* map_shadow */ true, __msan_get_track_origins())) {
|
2012-12-26 14:37:23 +08:00
|
|
|
Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n");
|
2012-12-11 20:27:27 +08:00
|
|
|
Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
|
2012-12-26 14:37:23 +08:00
|
|
|
Printf("FATAL: Disabling ASLR is known to cause this error.\n");
|
2012-12-27 22:09:19 +08:00
|
|
|
Printf("FATAL: If running under GDB, try "
|
|
|
|
"'set disable-randomization off'.\n");
|
2012-12-11 20:27:27 +08:00
|
|
|
DumpProcessMap();
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2013-12-25 15:09:44 +08:00
|
|
|
Symbolizer::Init(common_flags()->external_symbolizer_path);
|
2013-11-01 05:44:07 +08:00
|
|
|
Symbolizer::Get()->AddHooks(EnterSymbolizer, ExitSymbolizer);
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2014-04-04 17:47:41 +08:00
|
|
|
MsanTSDInit(MsanTSDDtor);
|
|
|
|
|
|
|
|
MsanThread *main_thread = MsanThread::Create(0, 0);
|
|
|
|
SetCurrentThread(main_thread);
|
|
|
|
main_thread->ThreadStart();
|
|
|
|
|
2013-12-05 20:04:51 +08:00
|
|
|
VPrintf(1, "MemorySanitizer init done\n");
|
2013-12-13 21:13:46 +08:00
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
msan_init_is_running = 0;
|
|
|
|
msan_inited = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_set_exit_code(int exit_code) {
|
|
|
|
flags()->exit_code = exit_code;
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:37:58 +08:00
|
|
|
void __msan_set_keep_going(int keep_going) {
|
2013-08-13 23:33:00 +08:00
|
|
|
flags()->halt_on_error = !keep_going;
|
2013-06-21 20:37:58 +08:00
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
void __msan_set_expect_umr(int expect_umr) {
|
|
|
|
if (expect_umr) {
|
|
|
|
msan_expected_umr_found = 0;
|
|
|
|
} else if (!msan_expected_umr_found) {
|
|
|
|
GET_CALLER_PC_BP_SP;
|
|
|
|
(void)sp;
|
2012-12-26 17:32:05 +08:00
|
|
|
StackTrace stack;
|
2013-03-14 19:47:03 +08:00
|
|
|
GetStackTrace(&stack, kStackTraceMax, pc, bp,
|
2013-05-06 21:15:14 +08:00
|
|
|
common_flags()->fast_unwind_on_fatal);
|
2012-12-26 17:32:05 +08:00
|
|
|
ReportExpectedUMRNotFound(&stack);
|
2012-12-11 20:27:27 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
msan_expect_umr = expect_umr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_print_shadow(const void *x, uptr size) {
|
2013-11-01 23:53:25 +08:00
|
|
|
if (!MEM_IS_APP(x)) {
|
|
|
|
Printf("Not a valid application address: %p\n", x);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-30 17:50:30 +08:00
|
|
|
|
|
|
|
DescribeMemoryRange(x, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_dump_shadow(const void *x, uptr size) {
|
|
|
|
if (!MEM_IS_APP(x)) {
|
|
|
|
Printf("Not a valid application address: %p\n", x);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x);
|
|
|
|
for (uptr i = 0; i < size; i++) {
|
|
|
|
Printf("%x%x ", s[i] >> 4, s[i] & 0xf);
|
|
|
|
}
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr __msan_test_shadow(const void *x, uptr size) {
|
2013-12-06 17:19:07 +08:00
|
|
|
if (!MEM_IS_APP(x)) return -1;
|
|
|
|
unsigned char *s = (unsigned char *)MEM_TO_SHADOW((uptr)x);
|
2012-12-11 20:27:27 +08:00
|
|
|
for (uptr i = 0; i < size; ++i)
|
|
|
|
if (s[i])
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-02 19:50:42 +08:00
|
|
|
void __msan_check_mem_is_initialized(const void *x, uptr size) {
|
|
|
|
if (!__msan::flags()->report_umrs) return;
|
|
|
|
sptr offset = __msan_test_shadow(x, size) < 0;
|
|
|
|
if (offset < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GET_CALLER_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
__msan::PrintWarningWithOrigin(pc, bp,
|
|
|
|
__msan_get_origin(((char *)x) + offset));
|
|
|
|
if (__msan::flags()->halt_on_error) {
|
|
|
|
Printf("Exiting\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
int __msan_set_poison_in_malloc(int do_poison) {
|
|
|
|
int old = flags()->poison_in_malloc;
|
|
|
|
flags()->poison_in_malloc = do_poison;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __msan_has_dynamic_component() {
|
|
|
|
return msan_running_under_dr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NOINLINE
|
|
|
|
void __msan_clear_on_return() {
|
|
|
|
__msan_param_tls[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* get_tls_base() {
|
|
|
|
u64 p;
|
|
|
|
asm("mov %%fs:0, %0"
|
|
|
|
: "=r"(p) ::);
|
|
|
|
return (void*)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __msan_get_retval_tls_offset() {
|
|
|
|
// volatile here is needed to avoid UB, because the compiler thinks that we
|
|
|
|
// are doing address arithmetics on unrelated pointers, and takes some
|
|
|
|
// shortcuts
|
|
|
|
volatile sptr retval_tls_p = (sptr)&__msan_retval_tls;
|
|
|
|
volatile sptr tls_base_p = (sptr)get_tls_base();
|
|
|
|
return retval_tls_p - tls_base_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __msan_get_param_tls_offset() {
|
|
|
|
// volatile here is needed to avoid UB, because the compiler thinks that we
|
|
|
|
// are doing address arithmetics on unrelated pointers, and takes some
|
|
|
|
// shortcuts
|
|
|
|
volatile sptr param_tls_p = (sptr)&__msan_param_tls;
|
|
|
|
volatile sptr tls_base_p = (sptr)get_tls_base();
|
|
|
|
return param_tls_p - tls_base_p;
|
|
|
|
}
|
|
|
|
|
2013-04-23 21:34:19 +08:00
|
|
|
void __msan_partial_poison(const void* data, void* shadow, uptr size) {
|
2012-12-11 20:27:27 +08:00
|
|
|
internal_memcpy((void*)MEM_TO_SHADOW((uptr)data), shadow, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_load_unpoisoned(void *src, uptr size, void *dst) {
|
|
|
|
internal_memcpy(dst, src, size);
|
|
|
|
__msan_unpoison(dst, size);
|
|
|
|
}
|
|
|
|
|
2013-04-23 21:34:19 +08:00
|
|
|
void __msan_set_origin(const void *a, uptr size, u32 origin) {
|
2012-12-11 20:27:27 +08:00
|
|
|
// Origin mapping is 4 bytes per 4 bytes of application memory.
|
|
|
|
// Here we extend the range such that its left and right bounds are both
|
|
|
|
// 4 byte aligned.
|
2013-05-31 20:04:08 +08:00
|
|
|
if (!__msan_get_track_origins()) return;
|
2012-12-11 20:27:27 +08:00
|
|
|
uptr x = MEM_TO_ORIGIN((uptr)a);
|
|
|
|
uptr beg = x & ~3UL; // align down.
|
|
|
|
uptr end = (x + size + 3) & ~3UL; // align up.
|
|
|
|
u64 origin64 = ((u64)origin << 32) | origin;
|
2013-11-19 22:47:56 +08:00
|
|
|
// This is like memset, but the value is 32-bit. We unroll by 2 to write
|
|
|
|
// 64 bits at once. May want to unroll further to get 128-bit stores.
|
2012-12-11 20:27:27 +08:00
|
|
|
if (beg & 7ULL) {
|
|
|
|
*(u32*)beg = origin;
|
|
|
|
beg += 4;
|
|
|
|
}
|
|
|
|
for (uptr addr = beg; addr < (end & ~7UL); addr += 8)
|
|
|
|
*(u64*)addr = origin64;
|
|
|
|
if (end & 7ULL)
|
|
|
|
*(u32*)(end - 4) = origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'descr' is created at compile time and contains '----' in the beginning.
|
|
|
|
// When we see descr for the first time we replace '----' with a uniq id
|
|
|
|
// and set the origin to (id | (31-th bit)).
|
|
|
|
void __msan_set_alloca_origin(void *a, uptr size, const char *descr) {
|
2013-09-13 20:49:13 +08:00
|
|
|
__msan_set_alloca_origin4(a, size, descr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_set_alloca_origin4(void *a, uptr size, const char *descr, uptr pc) {
|
2012-12-11 20:27:27 +08:00
|
|
|
static const u32 dash = '-';
|
|
|
|
static const u32 first_timer =
|
|
|
|
dash + (dash << 8) + (dash << 16) + (dash << 24);
|
|
|
|
u32 *id_ptr = (u32*)descr;
|
|
|
|
bool print = false; // internal_strstr(descr + 4, "AllocaTOTest") != 0;
|
|
|
|
u32 id = *id_ptr;
|
|
|
|
if (id == first_timer) {
|
|
|
|
id = atomic_fetch_add(&NumStackOriginDescrs,
|
|
|
|
1, memory_order_relaxed);
|
|
|
|
*id_ptr = id;
|
|
|
|
CHECK_LT(id, kNumStackOriginDescrs);
|
|
|
|
StackOriginDescr[id] = descr + 4;
|
2013-09-13 20:49:13 +08:00
|
|
|
StackOriginPC[id] = pc;
|
2012-12-11 20:27:27 +08:00
|
|
|
if (print)
|
2013-09-13 20:49:13 +08:00
|
|
|
Printf("First time: id=%d %s %p \n", id, descr + 4, pc);
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
id |= 1U << 31;
|
|
|
|
if (print)
|
|
|
|
Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id);
|
|
|
|
__msan_set_origin(a, size, id);
|
|
|
|
}
|
|
|
|
|
2014-03-18 21:45:19 +08:00
|
|
|
u32 __msan_chain_origin(u32 id) {
|
2014-03-31 22:18:55 +08:00
|
|
|
GET_CALLER_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
GET_STORE_STACK_TRACE_PC_BP(pc, bp);
|
2014-03-18 21:45:19 +08:00
|
|
|
return ChainOrigin(id, &stack);
|
|
|
|
}
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
const char *__msan_get_origin_descr_if_stack(u32 id) {
|
2013-09-13 20:49:13 +08:00
|
|
|
return GetOriginDescrIfStack(id, 0);
|
2012-12-11 20:27:27 +08:00
|
|
|
}
|
|
|
|
|
2013-04-23 21:34:19 +08:00
|
|
|
u32 __msan_get_origin(const void *a) {
|
2013-05-31 20:04:08 +08:00
|
|
|
if (!__msan_get_track_origins()) return 0;
|
2012-12-11 20:27:27 +08:00
|
|
|
uptr x = (uptr)a;
|
|
|
|
uptr aligned = x & ~3ULL;
|
|
|
|
uptr origin_ptr = MEM_TO_ORIGIN(aligned);
|
|
|
|
return *(u32*)origin_ptr;
|
|
|
|
}
|
|
|
|
|
2013-01-29 22:33:29 +08:00
|
|
|
u32 __msan_get_umr_origin() {
|
2012-12-11 20:27:27 +08:00
|
|
|
return __msan_origin_tls;
|
|
|
|
}
|
2013-02-13 15:19:47 +08:00
|
|
|
|
2013-06-04 21:49:10 +08:00
|
|
|
u16 __sanitizer_unaligned_load16(const uu16 *p) {
|
|
|
|
__msan_retval_tls[0] = *(uu16 *)MEM_TO_SHADOW((uptr)p);
|
2013-10-16 16:25:13 +08:00
|
|
|
if (__msan_get_track_origins())
|
2014-04-02 19:06:35 +08:00
|
|
|
__msan_retval_origin_tls = GetOriginIfPoisoned((uptr)p, sizeof(*p));
|
2013-06-04 21:49:10 +08:00
|
|
|
return *p;
|
2013-06-04 21:08:36 +08:00
|
|
|
}
|
2013-06-04 21:49:10 +08:00
|
|
|
u32 __sanitizer_unaligned_load32(const uu32 *p) {
|
|
|
|
__msan_retval_tls[0] = *(uu32 *)MEM_TO_SHADOW((uptr)p);
|
2013-10-16 16:25:13 +08:00
|
|
|
if (__msan_get_track_origins())
|
2014-04-02 19:06:35 +08:00
|
|
|
__msan_retval_origin_tls = GetOriginIfPoisoned((uptr)p, sizeof(*p));
|
2013-06-04 21:49:10 +08:00
|
|
|
return *p;
|
2013-06-04 21:08:36 +08:00
|
|
|
}
|
2013-06-04 21:49:10 +08:00
|
|
|
u64 __sanitizer_unaligned_load64(const uu64 *p) {
|
|
|
|
__msan_retval_tls[0] = *(uu64 *)MEM_TO_SHADOW((uptr)p);
|
2013-10-16 16:25:13 +08:00
|
|
|
if (__msan_get_track_origins())
|
2014-04-02 19:06:35 +08:00
|
|
|
__msan_retval_origin_tls = GetOriginIfPoisoned((uptr)p, sizeof(*p));
|
2013-06-04 21:49:10 +08:00
|
|
|
return *p;
|
2013-06-04 21:08:36 +08:00
|
|
|
}
|
2013-06-04 21:49:10 +08:00
|
|
|
void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
|
2014-04-02 19:06:35 +08:00
|
|
|
u16 s = __msan_param_tls[1];
|
|
|
|
*(uu16 *)MEM_TO_SHADOW((uptr)p) = s;
|
|
|
|
if (s && __msan_get_track_origins())
|
2013-11-19 22:47:56 +08:00
|
|
|
if (uu32 o = __msan_param_origin_tls[2])
|
2014-04-02 19:06:35 +08:00
|
|
|
SetOriginIfPoisoned((uptr)p, (uptr)&s, sizeof(s), o);
|
2013-06-04 21:49:10 +08:00
|
|
|
*p = x;
|
2013-06-04 21:08:36 +08:00
|
|
|
}
|
2013-06-04 21:49:10 +08:00
|
|
|
void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
|
2014-04-02 19:06:35 +08:00
|
|
|
u32 s = __msan_param_tls[1];
|
|
|
|
*(uu32 *)MEM_TO_SHADOW((uptr)p) = s;
|
|
|
|
if (s && __msan_get_track_origins())
|
2013-11-19 22:47:56 +08:00
|
|
|
if (uu32 o = __msan_param_origin_tls[2])
|
2014-04-02 19:06:35 +08:00
|
|
|
SetOriginIfPoisoned((uptr)p, (uptr)&s, sizeof(s), o);
|
2013-06-04 21:49:10 +08:00
|
|
|
*p = x;
|
2013-06-04 21:08:36 +08:00
|
|
|
}
|
2013-06-04 21:49:10 +08:00
|
|
|
void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
|
2014-04-02 19:06:35 +08:00
|
|
|
u64 s = __msan_param_tls[1];
|
|
|
|
*(uu64 *)MEM_TO_SHADOW((uptr)p) = s;
|
|
|
|
if (s && __msan_get_track_origins())
|
2013-11-19 22:47:56 +08:00
|
|
|
if (uu32 o = __msan_param_origin_tls[2])
|
2014-04-02 19:06:35 +08:00
|
|
|
SetOriginIfPoisoned((uptr)p, (uptr)&s, sizeof(s), o);
|
2013-06-04 21:49:10 +08:00
|
|
|
*p = x;
|
2013-06-04 21:08:36 +08:00
|
|
|
}
|
|
|
|
|
2014-03-27 22:04:58 +08:00
|
|
|
void __msan_set_death_callback(void (*callback)(void)) {
|
|
|
|
death_callback = callback;
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:05:19 +08:00
|
|
|
void *__msan_wrap_indirect_call(void *target) {
|
|
|
|
return IndirectExternCall(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_dr_is_initialized() {
|
|
|
|
msan_running_under_dr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __msan_set_indirect_call_wrapper(uptr wrapper) {
|
|
|
|
SetIndirectCallWrapper(wrapper);
|
|
|
|
}
|
|
|
|
|
2013-02-13 15:19:47 +08:00
|
|
|
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
|
|
|
|
extern "C" {
|
2013-08-13 19:42:45 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
2013-02-13 15:19:47 +08:00
|
|
|
const char* __msan_default_options() { return ""; }
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|