[msan] MemorySanitizer runtime.
authorEvgeniy Stepanov <eugeni.stepanov@gmail.com>
Tue, 11 Dec 2012 12:27:27 +0000 (12:27 +0000)
committerEvgeniy Stepanov <eugeni.stepanov@gmail.com>
Tue, 11 Dec 2012 12:27:27 +0000 (12:27 +0000)
Initial commit of the MemorySanitizer runtime library.

llvm-svn: 169858

compiler-rt/lib/CMakeLists.txt
compiler-rt/lib/msan/CMakeLists.txt [new file with mode: 0644]
compiler-rt/lib/msan/msan.cc [new file with mode: 0644]
compiler-rt/lib/msan/msan.h [new file with mode: 0644]
compiler-rt/lib/msan/msan_allocator.cc [new file with mode: 0644]
compiler-rt/lib/msan/msan_flags.h [new file with mode: 0644]
compiler-rt/lib/msan/msan_interceptors.cc [new file with mode: 0644]
compiler-rt/lib/msan/msan_linux.cc [new file with mode: 0644]
compiler-rt/lib/msan/msan_new_delete.cc [new file with mode: 0644]
compiler-rt/lib/msan/msan_platform_limits_posix.cc [new file with mode: 0644]
compiler-rt/lib/msan/msan_platform_limits_posix.h [new file with mode: 0644]

index b7cd07e..e771904 100644 (file)
@@ -9,8 +9,9 @@ if(CMAKE_SYSTEM_NAME MATCHES "Darwin|Linux")
   add_subdirectory(ubsan)
 endif()
 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
-  # ThreadSanitizer is supported on Linux only.
+  # ThreadSanitizer and MemorySanitizer are supported on Linux only.
   add_subdirectory(tsan)
+  add_subdirectory(msan)
 endif()
 
 # FIXME: Add support for the profile library.
diff --git a/compiler-rt/lib/msan/CMakeLists.txt b/compiler-rt/lib/msan/CMakeLists.txt
new file mode 100644 (file)
index 0000000..bd4a50f
--- /dev/null
@@ -0,0 +1,35 @@
+# Build for the MemorySanitizer runtime support library.
+
+set(MSAN_SOURCES
+  msan.cc
+  msan_allocator.cc
+  msan_interceptors.cc
+  msan_linux.cc
+  msan_new_delete.cc
+  msan_platform_limits_posix.cc
+  )
+
+include_directories(..)
+
+set(MSAN_CFLAGS
+  ${SANITIZER_COMMON_CFLAGS}
+  -fPIE
+  -ffreestanding
+  -g
+  -fno-omit-frame-pointer)
+set(MSAN_COMMON_DEFINITIONS)
+
+set(MSAN_RUNTIME_LIBRARIES)
+add_library(clang_rt.msan-x86_64 STATIC
+  ${MSAN_SOURCES}
+  $<TARGET_OBJECTS:RTInterception.x86_64>
+  $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>
+  )
+set_target_compile_flags(clang_rt.msan-x86_64
+  ${MSAN_CFLAGS} ${TARGET_X86_64_CFLAGS}
+  )
+list(APPEND MSAN_RUNTIME_LIBRARIES clang_rt.msan-x86_64)
+
+set_property(TARGET ${MSAN_RUNTIME_LIBRARIES} APPEND PROPERTY
+  COMPILE_DEFINITIONS ${MSAN_COMMON_DEFINITIONS})
+add_clang_compiler_rt_libraries(${MSAN_RUNTIME_LIBRARIES})
diff --git a/compiler-rt/lib/msan/msan.cc b/compiler-rt/lib/msan/msan.cc
new file mode 100644 (file)
index 0000000..e0d1b6c
--- /dev/null
@@ -0,0 +1,427 @@
+//===-- 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"
+#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_mutex.h"
+#include "sanitizer_common/sanitizer_procmaps.h"
+#include "sanitizer_common/sanitizer_stackdepot.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+#include "sanitizer_common/sanitizer_symbolizer.h"
+
+#include "interception/interception.h"
+
+// 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;
+
+static int msan_running_under_dr = 0;
+
+SANITIZER_INTERFACE_ATTRIBUTE
+THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSizeInWords];
+
+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;
+
+static THREADLOCAL struct {
+  uptr stack_top, stack_bottom;
+} __msan_stack_bounds;
+
+static StaticSpinMutex report_mu;
+
+extern const int __msan_track_origins;
+int __msan_get_track_origins() {
+  return __msan_track_origins;
+}
+
+namespace __msan {
+
+static bool IsRunningUnderDr() {
+  bool result = false;
+  MemoryMappingLayout proc_maps;
+  const sptr kBufSize = 4095;
+  char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
+  while (proc_maps.Next(/* start */0, /* end */0, /* file_offset */0,
+                        filename, kBufSize)) {
+    if (internal_strstr(filename, "libdynamorio") != 0) {
+      result = true;
+      break;
+    }
+  }
+  UnmapOrDie(filename, kBufSize);
+  return result;
+}
+
+static Flags msan_flags;
+
+Flags *flags() {
+  return &msan_flags;
+}
+
+int msan_inited = 0;
+bool msan_init_is_running;
+
+// Array of stack origins.
+// FIXME: make it resizable.
+static const uptr kNumStackOriginDescrs = 1024 * 1024;
+static const char *StackOriginDescr[kNumStackOriginDescrs];
+static atomic_uint32_t NumStackOriginDescrs;
+
+static void ParseFlagsFromString(Flags *f, const char *str) {
+  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->exit_code, "exit_code");
+  if (f->exit_code < 0 || f->exit_code > 127) {
+    Printf("Exit code not in [0, 128) range: %d\n", f->exit_code);
+    f->exit_code = 1;
+    Die();
+  }
+  ParseFlag(str, &f->num_callers, "num_callers");
+  ParseFlag(str, &f->report_umrs, "report_umrs");
+  ParseFlag(str, &f->verbosity, "verbosity");
+}
+
+static void InitializeFlags(Flags *f, const char *options) {
+  internal_memset(f, 0, sizeof(*f));
+
+  f->poison_heap_with_zeroes = false;
+  f->poison_stack_with_zeroes = false;
+  f->poison_in_malloc = true;
+  f->exit_code = 77;
+  f->num_callers = 20;
+  f->report_umrs = true;
+  f->verbosity = 0;
+
+  ParseFlagsFromString(f, options);
+}
+
+static void GetCurrentStackBounds(uptr *stack_top, uptr *stack_bottom) {
+  if (__msan_stack_bounds.stack_top == 0) {
+    // Break recursion (GetStackTrace -> GetThreadStackTopAndBottom ->
+    // realloc -> GetStackTrace).
+    __msan_stack_bounds.stack_top = __msan_stack_bounds.stack_bottom = 1;
+    GetThreadStackTopAndBottom(/* at_initialization */false,
+                               &__msan_stack_bounds.stack_top,
+                               &__msan_stack_bounds.stack_bottom);
+  }
+  *stack_top = __msan_stack_bounds.stack_top;
+  *stack_bottom = __msan_stack_bounds.stack_bottom;
+}
+
+void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) {
+  uptr stack_top, stack_bottom;
+  GetCurrentStackBounds(&stack_top, &stack_bottom);
+  stack->size = 0;
+  stack->trace[0] = pc;
+  stack->max_size = max_s;
+  stack->FastUnwindStack(pc, bp, stack_top, stack_bottom);
+}
+
+static void PrintCurrentStackTrace(uptr pc, uptr bp) {
+  StackTrace stack;
+  GetStackTrace(&stack, kStackTraceMax, pc, bp);
+  StackTrace::PrintStack(stack.trace, stack.size, true, "", 0);
+}
+
+void PrintWarning(uptr pc, uptr bp) {
+  PrintWarningWithOrigin(pc, bp, __msan_origin_tls);
+}
+
+void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) {
+  if (!__msan::flags()->report_umrs) return;
+  if (msan_expect_umr) {
+    // Printf("Expected UMR\n");
+    __msan_origin_tls = origin;
+    msan_expected_umr_found = 1;
+    return;
+  }
+
+  GenericScopedLock<StaticSpinMutex> lock(&report_mu);
+
+  Report(" WARNING: MemorySanitizer: UMR (uninitialized-memory-read)\n");
+  PrintCurrentStackTrace(pc, bp);
+  if (__msan_track_origins) {
+    Printf("  raw origin id: %d\n", origin);
+    if (origin == 0 || origin == (u32)-1) {
+      Printf("  ORIGIN: invalid (%x). Might be a bug in MemorySanitizer, "
+             "please report to MemorySanitizer developers.\n",
+             origin);
+    } else if (const char *so = __msan_get_origin_descr_if_stack(origin)) {
+      Printf("  ORIGIN: stack allocation: %s\n", so);
+    } else if (origin != 0) {
+      uptr size = 0;
+      const uptr *trace = StackDepotGet(origin, &size);
+      Printf("  ORIGIN: heap allocation:\n");
+      StackTrace::PrintStack(trace, size, true, "", 0);
+    }
+  }
+}
+
+}  // namespace __msan
+
+// Interface.
+
+using namespace __msan;
+
+void __msan_warning() {
+  GET_CALLER_PC_BP_SP;
+  (void)sp;
+  PrintWarning(pc, bp);
+}
+
+void __msan_warning_noreturn() {
+  GET_CALLER_PC_BP_SP;
+  (void)sp;
+  PrintWarning(pc, bp);
+  Printf("Exiting\n");
+  Die();
+}
+
+void __msan_init() {
+  if (msan_inited) return;
+  msan_init_is_running = 1;
+
+  report_mu.Init();
+
+  SetDieCallback(MsanDie);
+  InitializeInterceptors();
+
+  ReplaceOperatorsNewAndDelete();
+  if (StackSizeIsUnlimited()) {
+    if (flags()->verbosity)
+      Printf("Unlimited stack, doing reexec\n");
+    // 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();
+  }
+  const char *msan_options = GetEnv("MSAN_OPTIONS");
+  InitializeFlags(&msan_flags, msan_options);
+  if (flags()->verbosity)
+    Printf("MSAN_OPTIONS: %s\n", msan_options ? msan_options : "<empty>");
+  msan_running_under_dr = IsRunningUnderDr();
+  __msan_clear_on_return();
+  if (__msan_track_origins && flags()->verbosity > 0)
+    Printf("msan_track_origins\n");
+  if (!InitShadow(/* prot1 */false, /* prot2 */true, /* map_shadow */true,
+                  __msan_track_origins)) {
+    // FIXME: prot1 = false is only required when running under DR.
+    Printf("FATAL: MemorySanitizer can not mmap the shadow memory\n");
+    Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
+    DumpProcessMap();
+    Die();
+  }
+
+  InstallTrapHandler();
+
+  const char *external_symbolizer = GetEnv("MSAN_SYMBOLIZER_PATH");
+  if (external_symbolizer && external_symbolizer[0]) {
+    CHECK(InitializeExternalSymbolizer(external_symbolizer));
+  }
+
+  GetThreadStackTopAndBottom(/* at_initialization */true,
+                             &__msan_stack_bounds.stack_top,
+                             &__msan_stack_bounds.stack_bottom);
+  if (flags()->verbosity)
+    Printf("MemorySanitizer init done\n");
+  msan_init_is_running = 0;
+  msan_inited = 1;
+}
+
+void __msan_set_exit_code(int exit_code) {
+  flags()->exit_code = exit_code;
+}
+
+void __msan_set_expect_umr(int expect_umr) {
+  if (expect_umr) {
+    msan_expected_umr_found = 0;
+  } else if (!msan_expected_umr_found) {
+    Printf("Expected UMR not found\n");
+    GET_CALLER_PC_BP_SP;
+    (void)sp;
+    PrintCurrentStackTrace(pc, bp);
+    Die();
+  }
+  msan_expect_umr = expect_umr;
+}
+
+void __msan_print_shadow(const void *x, uptr size) {
+  unsigned char *s = (unsigned char*)MEM_TO_SHADOW(x);
+  u32 *o = (u32*)MEM_TO_ORIGIN(x);
+  for (uptr i = 0; i < size; i++) {
+    Printf("%x%x ", s[i] >> 4, s[i] & 0xf);
+  }
+  Printf("\n");
+  if (__msan_track_origins) {
+    for (uptr i = 0; i < size / 4; i++) {
+      Printf(" o: %x ", o[i]);
+    }
+    Printf("\n");
+  }
+}
+
+void __msan_print_param_shadow() {
+  for (int i = 0; i < 16; i++) {
+    Printf("#%d:%zx ", i, __msan_param_tls[i]);
+  }
+  Printf("\n");
+}
+
+sptr __msan_test_shadow(const void *x, uptr size) {
+  unsigned char *s = (unsigned char*)MEM_TO_SHADOW((uptr)x);
+  for (uptr i = 0; i < size; ++i)
+    if (s[i])
+      return i;
+  return -1;
+}
+
+int __msan_set_poison_in_malloc(int do_poison) {
+  int old = flags()->poison_in_malloc;
+  flags()->poison_in_malloc = do_poison;
+  return old;
+}
+
+void __msan_break_optimization(void *x) { }
+
+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;
+}
+
+void __msan_partial_poison(void* data, void* shadow, uptr size) {
+  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);
+}
+
+void __msan_set_origin(void *a, uptr size, u32 origin) {
+  // 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.
+  if (!__msan_track_origins) return;
+  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;
+  // This is like memset, but the value is 32-bit. We unroll by 2 two write
+  // 64-bits at once. May want to unroll further to get 128-bit stores.
+  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) {
+  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;
+    if (print)
+      Printf("First time: id=%d %s \n", id, descr + 4);
+  }
+  id |= 1U << 31;
+  if (print)
+    Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id);
+  __msan_set_origin(a, size, id);
+}
+
+const char *__msan_get_origin_descr_if_stack(u32 id) {
+  if ((id >> 31) == 0) return 0;
+  id &= (1U << 31) - 1;
+  CHECK_LT(id, kNumStackOriginDescrs);
+  return StackOriginDescr[id];
+}
+
+
+u32 __msan_get_origin(void *a) {
+  if (!__msan_track_origins) return 0;
+  uptr x = (uptr)a;
+  uptr aligned = x & ~3ULL;
+  uptr origin_ptr = MEM_TO_ORIGIN(aligned);
+  return *(u32*)origin_ptr;
+}
+
+u32 __msan_get_origin_tls() {
+  return __msan_origin_tls;
+}
diff --git a/compiler-rt/lib/msan/msan.h b/compiler-rt/lib/msan/msan.h
new file mode 100644 (file)
index 0000000..11d2742
--- /dev/null
@@ -0,0 +1,62 @@
+//===-- msan.h ------------------------------------------------------------===//
+//
+//                     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.
+//
+// Private MSan header.
+//===----------------------------------------------------------------------===//
+
+#ifndef MSAN_H
+#define MSAN_H
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+#include "sanitizer/msan_interface.h"
+#include "msan_flags.h"
+
+#define MEM_TO_SHADOW(mem) (((uptr)mem)       & ~0x400000000000ULL)
+#define MEM_TO_ORIGIN(mem) (MEM_TO_SHADOW(mem) + 0x200000000000ULL)
+#define MEM_IS_APP(mem)    ((uptr)mem >=         0x600000000000ULL)
+#define MEM_IS_SHADOW(mem) ((uptr)mem >=         0x200000000000ULL && \
+                            (uptr)mem <=         0x400000000000ULL)
+
+const int kMsanParamTlsSizeInWords = 100;
+const int kMsanRetvalTlsSizeInWords = 100;
+
+namespace __msan {
+extern int msan_inited;
+extern bool msan_init_is_running;
+
+bool ProtectRange(uptr beg, uptr end);
+bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins);
+char *GetProcSelfMaps();
+void InitializeInterceptors();
+
+void *MsanReallocate(StackTrace *stack, void *oldp, uptr size,
+                     uptr alignment, bool zeroise);
+void MsanDeallocate(void *ptr);
+void InstallTrapHandler();
+void ReplaceOperatorsNewAndDelete();
+
+void MsanDie();
+void PrintWarning(uptr pc, uptr bp);
+void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
+
+void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp);
+
+#define GET_MALLOC_STACK_TRACE                                     \
+  StackTrace stack;                                                \
+  stack.size = 0;                                                  \
+  if (__msan_get_track_origins() && msan_inited)                   \
+    GetStackTrace(&stack, flags()->num_callers,                    \
+      StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
+
+}  // namespace __msan
+
+#endif  // MSAN_H
diff --git a/compiler-rt/lib/msan/msan_allocator.cc b/compiler-rt/lib/msan/msan_allocator.cc
new file mode 100644 (file)
index 0000000..5d865c3
--- /dev/null
@@ -0,0 +1,107 @@
+//===-- msan_allocator.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 allocator.
+//===----------------------------------------------------------------------===//
+
+#include "sanitizer_common/sanitizer_allocator.h"
+#include "sanitizer_common/sanitizer_stackdepot.h"
+#include "msan.h"
+
+namespace __msan {
+
+struct Metadata {
+  uptr requested_size;
+};
+
+static const uptr kAllocatorSpace = 0x600000000000ULL;
+static const uptr kAllocatorSize   = 0x80000000000;  // 8T.
+static const uptr kMetadataSize  = sizeof(Metadata);
+
+typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, kMetadataSize,
+                             DefaultSizeClassMap> PrimaryAllocator;
+typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
+typedef LargeMmapAllocator SecondaryAllocator;
+typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
+                          SecondaryAllocator> Allocator;
+
+static THREADLOCAL AllocatorCache cache;
+static Allocator allocator;
+
+static int inited = 0;
+
+static inline void Init() {
+  if (inited) return;
+  __msan_init();
+  inited = true;  // this must happen before any threads are created.
+  allocator.Init();
+}
+
+static void *MsanAllocate(StackTrace *stack, uptr size,
+                          uptr alignment, bool zeroise) {
+  Init();
+  void *res = allocator.Allocate(&cache, size, alignment, false);
+  Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(res));
+  meta->requested_size = size;
+  if (zeroise)
+    __msan_clear_and_unpoison(res, size);
+  else if (flags()->poison_in_malloc)
+    __msan_poison(res, size);
+  if (__msan_get_track_origins()) {
+    u32 stack_id = StackDepotPut(stack->trace, stack->size);
+    CHECK(stack_id);
+    CHECK_EQ((stack_id >> 31), 0);  // Higher bit is occupied by stack origins.
+    __msan_set_origin(res, size, stack_id);
+  }
+  return res;
+}
+
+void MsanDeallocate(void *p) {
+  CHECK(p);
+  Init();
+  Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(p));
+  uptr size = meta->requested_size;
+  // This memory will not be reused by anyone else, so we are free to keep it
+  // poisoned.
+  __msan_poison(p, size);
+  if (__msan_get_track_origins())
+    __msan_set_origin(p, size, -1);
+  allocator.Deallocate(&cache, p);
+}
+
+void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
+                     uptr alignment, bool zeroise) {
+  if (!old_p)
+    return MsanAllocate(stack, new_size, alignment, zeroise);
+  if (!new_size) {
+    MsanDeallocate(old_p);
+    return 0;
+  }
+  Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
+  uptr old_size = meta->requested_size;
+  uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
+  if (new_size <= actually_allocated_size) {
+    // We are not reallocating here.
+    meta->requested_size = new_size;
+    if (new_size > old_size)
+      __msan_poison((char*)old_p + old_size, new_size - old_size);
+    return old_p;
+  }
+  uptr memcpy_size = Min(new_size, old_size);
+  void *new_p = MsanAllocate(stack, new_size, alignment, zeroise);
+  // Printf("realloc: old_size %zd new_size %zd\n", old_size, new_size);
+  if (new_p)
+    __msan_memcpy(new_p, old_p, memcpy_size);
+  MsanDeallocate(old_p);
+  return new_p;
+}
+
+}  // namespace __msan
diff --git a/compiler-rt/lib/msan/msan_flags.h b/compiler-rt/lib/msan/msan_flags.h
new file mode 100644 (file)
index 0000000..b07f5cd
--- /dev/null
@@ -0,0 +1,34 @@
+//===-- msan_allocator.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 allocator.
+//===----------------------------------------------------------------------===//
+#ifndef MSAN_FLAGS_H
+#define MSAN_FLAGS_H
+
+namespace __msan {
+
+// Flags.
+struct Flags {
+  int exit_code;
+  int num_callers;
+  int verbosity;
+  bool poison_heap_with_zeroes;  // default: false
+  bool poison_stack_with_zeroes;  // default: false
+  bool poison_in_malloc;  // default: true
+  bool report_umrs;
+};
+
+Flags *flags();
+
+}  // namespace __msan
+
+#endif  // MSAN_FLAGS_H
diff --git a/compiler-rt/lib/msan/msan_interceptors.cc b/compiler-rt/lib/msan/msan_interceptors.cc
new file mode 100644 (file)
index 0000000..58b95b1
--- /dev/null
@@ -0,0 +1,884 @@
+//===-- msan_interceptors.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.
+//
+// Interceptors for standard library functions.
+//===----------------------------------------------------------------------===//
+
+#include "interception/interception.h"
+#include "msan.h"
+#include "msan_platform_limits_posix.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_libc.h"
+
+#include <stdarg.h>
+// ACHTUNG! No other system header includes in this file.
+// Ideally, we should get rid of stdarg.h as well.
+
+typedef uptr size_t;
+typedef sptr ssize_t;
+typedef u64  off_t;
+typedef u64  off64_t;
+using namespace __msan;
+
+#define ENSURE_MSAN_INITED() do { \
+    CHECK(!msan_init_is_running);       \
+  if (!msan_inited) { \
+    __msan_init(); \
+  } \
+} while (0)
+
+#define CHECK_UNPOISONED(x, n) \
+  do { \
+    sptr offset = __msan_test_shadow(x, n);                 \
+    if (offset >= 0 && flags()->report_umrs) {              \
+      GET_CALLER_PC_BP_SP;                                  \
+      (void)sp;                                             \
+      Printf("UMR in %s at offset %d inside [%p, +%d) \n",  \
+             __FUNCTION__, offset, x, n);                   \
+      __msan::PrintWarningWithOrigin(                       \
+        pc, bp, __msan_get_origin((char*)x + offset));      \
+    }                                                       \
+  } while (0)
+
+static void *fast_memset(void *ptr, int c, size_t n);
+static void *fast_memcpy(void *dst, const void *src, size_t n);
+
+INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nmemb, void *file) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(fread)(ptr, size, nmemb, file);
+  if (res > 0)
+    __msan_unpoison(ptr, res *size);
+  return res;
+}
+
+INTERCEPTOR(size_t, fread_unlocked, void *ptr, size_t size, size_t nmemb,
+            void *file) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(fread_unlocked)(ptr, size, nmemb, file);
+  if (res > 0)
+    __msan_unpoison(ptr, res *size);
+  return res;
+}
+
+INTERCEPTOR(ssize_t, read, int fd, void *ptr, size_t count) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(read)(fd, ptr, count);
+  if (res > 0)
+    __msan_unpoison(ptr, res);
+  return res;
+}
+
+INTERCEPTOR(ssize_t, pread, int fd, void *ptr, size_t count, off_t offset) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(pread)(fd, ptr, count, offset);
+  if (res > 0)
+    __msan_unpoison(ptr, res);
+  return res;
+}
+
+INTERCEPTOR(ssize_t, pread64, int fd, void *ptr, size_t count, off64_t offset) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(pread64)(fd, ptr, count, offset);
+  if (res > 0)
+    __msan_unpoison(ptr, res);
+  return res;
+}
+
+INTERCEPTOR(ssize_t, readlink, const char *path, char *buf, size_t bufsiz) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(readlink)(path, buf, bufsiz);
+  if (res > 0)
+    __msan_unpoison(buf, res);
+  return res;
+}
+
+INTERCEPTOR(void *, readdir, void *a) {
+  ENSURE_MSAN_INITED();
+  void *res = REAL(readdir)(a);
+  __msan_unpoison(res, __msan::struct_dirent_sz);
+  return res;
+}
+
+INTERCEPTOR(void *, memcpy, void *dest, const void *src, size_t n) {
+  return __msan_memcpy(dest, src, n);
+}
+
+INTERCEPTOR(void *, memmove, void *dest, const void *src, size_t n) {
+  return __msan_memmove(dest, src, n);
+}
+
+INTERCEPTOR(void *, memset, void *s, int c, size_t n) {
+  return __msan_memset(s, c, n);
+}
+
+INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
+  GET_MALLOC_STACK_TRACE;
+  CHECK_EQ(alignment & (alignment - 1), 0);
+  *memptr = MsanReallocate(&stack, 0, size, alignment, false);
+  CHECK_NE(memptr, 0);
+  return 0;
+}
+
+INTERCEPTOR(void, free, void *ptr) {
+  ENSURE_MSAN_INITED();
+  if (ptr == 0) return;
+  MsanDeallocate(ptr);
+}
+
+INTERCEPTOR(size_t, strlen, const char *s) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(strlen)(s);
+  CHECK_UNPOISONED(s, res + 1);
+  return res;
+}
+
+INTERCEPTOR(size_t, strnlen, const char *s, size_t n) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(strnlen)(s, n);
+  size_t scan_size = (res == n) ? res : res + 1;
+  CHECK_UNPOISONED(s, scan_size);
+  return res;
+}
+
+// FIXME: Add stricter shadow checks in str* interceptors (ex.: strcpy should
+// check the shadow of the terminating \0 byte).
+
+INTERCEPTOR(char *, strcpy, char *dest, const char *src) {  // NOLINT
+  ENSURE_MSAN_INITED();
+  size_t n = REAL(strlen)(src);
+  char *res = REAL(strcpy)(dest, src);  // NOLINT
+  __msan_copy_poison(dest, src, n + 1);
+  return res;
+}
+
+INTERCEPTOR(char *, strncpy, char *dest, const char *src, size_t n) {  // NOLINT
+  ENSURE_MSAN_INITED();
+  size_t copy_size = REAL(strnlen)(src, n);
+  if (copy_size < n)
+    copy_size++;  // trailing \0
+  char *res = REAL(strncpy)(dest, src, n);  // NOLINT
+  __msan_copy_poison(dest, src, copy_size);
+  return res;
+}
+
+INTERCEPTOR(char *, strdup, char *src) {
+  ENSURE_MSAN_INITED();
+  size_t n = REAL(strlen)(src);
+  char *res = REAL(strdup)(src);
+  __msan_copy_poison(res, src, n + 1);
+  return res;
+}
+
+INTERCEPTOR(char *, gcvt, double number, size_t ndigit, char *buf) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(gcvt)(number, ndigit, buf);
+  // DynamoRio tool will take care of unpoisoning gcvt result for us.
+  if (!__msan_has_dynamic_component()) {
+    size_t n = REAL(strlen)(buf);
+    __msan_unpoison(buf, n + 1);
+  }
+  return res;
+}
+
+INTERCEPTOR(char *, strcat, char *dest, const char *src) {  // NOLINT
+  ENSURE_MSAN_INITED();
+  size_t src_size = REAL(strlen)(src);
+  size_t dest_size = REAL(strlen)(dest);
+  char *res = REAL(strcat)(dest, src);  // NOLINT
+  __msan_copy_poison(dest + dest_size, src, src_size + 1);
+  return res;
+}
+
+INTERCEPTOR(char *, strncat, char *dest, const char *src, size_t n) {  // NOLINT
+  ENSURE_MSAN_INITED();
+  size_t dest_size = REAL(strlen)(dest);
+  size_t copy_size = REAL(strlen)(src);
+  if (copy_size < n)
+    copy_size++;  // trailing \0
+  char *res = REAL(strncat)(dest, src, n);  // NOLINT
+  __msan_copy_poison(dest + dest_size, src, copy_size);
+  return res;
+}
+
+INTERCEPTOR(long, strtol, const char *nptr, char **endptr,  // NOLINT
+            int base) {
+  ENSURE_MSAN_INITED();
+  long res = REAL(strtol)(nptr, endptr, base);  // NOLINT
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(endptr, sizeof(*endptr));
+  }
+  return res;
+}
+
+INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr,  // NOLINT
+            int base) {
+  ENSURE_MSAN_INITED();
+  long res = REAL(strtoll)(nptr, endptr, base);  //NOLINT
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(endptr, sizeof(*endptr));
+  }
+  return res;
+}
+
+INTERCEPTOR(unsigned long, strtoul, const char *nptr, char **endptr,  // NOLINT
+            int base) {
+  ENSURE_MSAN_INITED();
+  unsigned long res = REAL(strtoul)(nptr, endptr, base);  // NOLINT
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(endptr, sizeof(*endptr));
+  }
+  return res;
+}
+
+INTERCEPTOR(unsigned long long, strtoull, const char *nptr,  // NOLINT
+            char **endptr, int base) {
+  ENSURE_MSAN_INITED();
+  unsigned long res = REAL(strtoull)(nptr, endptr, base);  // NOLINT
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(endptr, sizeof(*endptr));
+  }
+  return res;
+}
+
+INTERCEPTOR(int, vsnprintf, char *str, uptr size,
+            const char *format, va_list ap) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(vsnprintf)(str, size, format, ap);
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(str, res + 1);
+  }
+  return res;
+}
+
+INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(vsprintf)(str, format, ap);
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(str, res + 1);
+  }
+  return res;
+}
+
+INTERCEPTOR(int, vswprintf, void *str, uptr size, void *format, va_list ap) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(vswprintf)(str, size, format, ap);
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(str, 4 * (res + 1));
+  }
+  return res;
+}
+
+INTERCEPTOR(int, sprintf, char *str, const char *format, ...) {  // NOLINT
+  ENSURE_MSAN_INITED();
+  va_list ap;
+  va_start(ap, format);
+  int res = vsprintf(str, format, ap);  // NOLINT
+  va_end(ap);
+  return res;
+}
+
+INTERCEPTOR(int, snprintf, char *str, uptr size, const char *format, ...) {
+  ENSURE_MSAN_INITED();
+  va_list ap;
+  va_start(ap, format);
+  int res = vsnprintf(str, size, format, ap);
+  va_end(ap);
+  return res;
+}
+
+INTERCEPTOR(int, swprintf, void *str, uptr size, void *format, ...) {
+  ENSURE_MSAN_INITED();
+  va_list ap;
+  va_start(ap, format);
+  int res = vswprintf(str, size, format, ap);
+  va_end(ap);
+  return res;
+}
+
+// size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
+INTERCEPTOR(size_t, strftime, char *s, size_t max, const char *format,
+            void *tm) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(strftime)(s, max, format, tm);
+  if (res) __msan_unpoison(s, res + 1);
+  return res;
+}
+
+INTERCEPTOR(size_t, wcstombs, void *dest, void *src, size_t size) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(wcstombs)(dest, src, size);
+  if (res != (size_t)-1) __msan_unpoison(dest, res + 1);
+  return res;
+}
+
+// size_t mbstowcs(wchar_t *dest, const char *src, size_t n);
+INTERCEPTOR(size_t, mbstowcs, wchar_t *dest, const char *src, size_t n) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(mbstowcs)(dest, src, n);
+  if (res != (size_t)-1) __msan_unpoison(dest, (res + 1) * sizeof(wchar_t));
+  return res;
+}
+
+INTERCEPTOR(size_t, wcslen, const wchar_t *s) {
+  ENSURE_MSAN_INITED();
+  size_t res = REAL(wcslen)(s);
+  CHECK_UNPOISONED(s, sizeof(wchar_t) * (res + 1));
+  return res;
+}
+
+// wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
+INTERCEPTOR(wchar_t *, wcschr, void *s, wchar_t wc, void *ps) {
+  ENSURE_MSAN_INITED();
+  wchar_t *res = REAL(wcschr)(s, wc, ps);
+  return res;
+}
+
+// wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
+INTERCEPTOR(wchar_t *, wcscpy, wchar_t *dest, const wchar_t *src) {
+  ENSURE_MSAN_INITED();
+  wchar_t *res = REAL(wcscpy)(dest, src);
+  __msan_copy_poison(dest, src, sizeof(wchar_t) * (REAL(wcslen)(src) + 1));
+  return res;
+}
+
+// wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, size_t n);
+INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dest, const wchar_t *src, size_t n) {
+  ENSURE_MSAN_INITED();
+  wchar_t *res = REAL(wmemcpy)(dest, src, n);
+  __msan_copy_poison(dest, src, n * sizeof(wchar_t));
+  return res;
+}
+
+INTERCEPTOR(wchar_t *, wmemset, wchar_t *s, wchar_t c, size_t n) {
+  CHECK(MEM_IS_APP(s));
+  ENSURE_MSAN_INITED();
+  wchar_t *res = (wchar_t *)fast_memset(s, c, n * sizeof(wchar_t));
+  __msan_unpoison(s, n * sizeof(wchar_t));
+  return res;
+}
+
+INTERCEPTOR(wchar_t *, wmemmove, wchar_t *dest, const wchar_t *src, size_t n) {
+  ENSURE_MSAN_INITED();
+  wchar_t *res = REAL(wmemmove)(dest, src, n);
+  __msan_move_poison(dest, src, n * sizeof(wchar_t));
+  return res;
+}
+
+INTERCEPTOR(int, wcscmp, const wchar_t *s1, const wchar_t *s2) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(wcscmp)(s1, s2);
+  return res;
+}
+
+INTERCEPTOR(double, wcstod, const wchar_t *nptr, wchar_t **endptr) {
+  ENSURE_MSAN_INITED();
+  double res = REAL(wcstod)(nptr, endptr);
+  __msan_unpoison(endptr, sizeof(*endptr));
+  return res;
+}
+
+// #define UNSUPPORTED(name) \
+//   INTERCEPTOR(void, name, void) {                     \
+//     Printf("MSAN: Unsupported %s\n", __FUNCTION__);   \
+//     Die();                                            \
+//   }
+
+// FIXME: intercept the following functions:
+// Note, they only matter when running without a dynamic tool.
+// UNSUPPORTED(wcscoll_l)
+// UNSUPPORTED(wcsnrtombs)
+// UNSUPPORTED(wcstol)
+// UNSUPPORTED(wcstoll)
+// UNSUPPORTED(wcstold)
+// UNSUPPORTED(wcstoul)
+// UNSUPPORTED(wcstoull)
+// UNSUPPORTED(wcsxfrm_l)
+// UNSUPPORTED(wcsdup)
+// UNSUPPORTED(wcsftime)
+// UNSUPPORTED(wcsstr)
+// UNSUPPORTED(wcsrchr)
+// UNSUPPORTED(wctob)
+
+INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(gettimeofday)(tv, tz);
+  if (tv)
+    __msan_unpoison(tv, 16);
+  if (tz)
+    __msan_unpoison(tz, 8);
+  return res;
+}
+
+INTERCEPTOR(char *, fcvt, double x, int a, int *b, int *c) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(fcvt)(x, a, b, c);
+  if (!__msan_has_dynamic_component()) {
+    __msan_unpoison(b, sizeof(*b));
+    __msan_unpoison(c, sizeof(*c));
+  }
+  return res;
+}
+
+INTERCEPTOR(char *, getenv, char *name) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(getenv)(name);
+  if (!__msan_has_dynamic_component()) {
+    if (res)
+      __msan_unpoison(res, REAL(strlen)(res) + 1);
+  }
+  return res;
+}
+
+INTERCEPTOR(int, __fxstat, int magic, int fd, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(__fxstat)(magic, fd, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_stat_sz);
+  return res;
+}
+
+INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(__fxstat64)(magic, fd, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_stat64_sz);
+  return res;
+}
+
+INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(__xstat)(magic, path, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_stat_sz);
+  return res;
+}
+
+INTERCEPTOR(int, __xstat64, int magic, char *path, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(__xstat64)(magic, path, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_stat64_sz);
+  return res;
+}
+
+INTERCEPTOR(int, __lxstat, int magic, char *path, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(__lxstat)(magic, path, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_stat_sz);
+  return res;
+}
+
+INTERCEPTOR(int, __lxstat64, int magic, char *path, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(__lxstat64)(magic, path, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_stat64_sz);
+  return res;
+}
+
+INTERCEPTOR(int, pipe, int pipefd[2]) {
+  if (msan_init_is_running)
+    return REAL(pipe)(pipefd);
+  ENSURE_MSAN_INITED();
+  int res = REAL(pipe)(pipefd);
+  if (!res)
+    __msan_unpoison(pipefd, sizeof(int[2]));
+  return res;
+}
+
+INTERCEPTOR(int, wait, int *status) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(wait)(status);
+  if (status)
+    __msan_unpoison(status, sizeof(*status));
+  return res;
+}
+
+INTERCEPTOR(int, waitpid, int pid, int *status, int options) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(waitpid)(pid, status, options);
+  if (status)
+    __msan_unpoison(status, sizeof(*status));
+  return res;
+}
+
+INTERCEPTOR(char *, fgets, char *s, int size, void *stream) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(fgets)(s, size, stream);
+  if (res)
+    __msan_unpoison(s, REAL(strlen)(s) + 1);
+  return res;
+}
+
+INTERCEPTOR(char *, fgets_unlocked, char *s, int size, void *stream) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(fgets_unlocked)(s, size, stream);
+  if (res)
+    __msan_unpoison(s, REAL(strlen)(s) + 1);
+  return res;
+}
+
+INTERCEPTOR(char *, getcwd, char *buf, size_t size) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(getcwd)(buf, size);
+  if (res)
+    __msan_unpoison(buf, REAL(strlen)(buf) + 1);
+  return res;
+}
+
+INTERCEPTOR(char *, realpath, char *path, char *abspath) {
+  ENSURE_MSAN_INITED();
+  char *res = REAL(realpath)(path, abspath);
+  if (res)
+    __msan_unpoison(abspath, REAL(strlen)(abspath) + 1);
+  return res;
+}
+
+INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
+  if (msan_init_is_running)
+    return REAL(getrlimit)(resource, rlim);
+  ENSURE_MSAN_INITED();
+  int res = REAL(getrlimit)(resource, rlim);
+  if (!res)
+    __msan_unpoison(rlim, __msan::struct_rlimit_sz);
+  return res;
+}
+
+INTERCEPTOR(int, getrlimit64, int resource, void *rlim) {
+  if (msan_init_is_running)
+    return REAL(getrlimit64)(resource, rlim);
+  ENSURE_MSAN_INITED();
+  int res = REAL(getrlimit64)(resource, rlim);
+  if (!res)
+    __msan_unpoison(rlim, __msan::struct_rlimit64_sz);
+  return res;
+}
+
+INTERCEPTOR(int, statfs, const char *s, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(statfs)(s, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_statfs_sz);
+  return res;
+}
+
+INTERCEPTOR(int, fstatfs, int fd, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(fstatfs)(fd, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_statfs_sz);
+  return res;
+}
+
+INTERCEPTOR(int, statfs64, const char *s, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(statfs64)(s, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_statfs64_sz);
+  return res;
+}
+
+INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(fstatfs64)(fd, buf);
+  if (!res)
+    __msan_unpoison(buf, __msan::struct_statfs64_sz);
+  return res;
+}
+
+INTERCEPTOR(int, uname, void *utsname) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(uname)(utsname);
+  if (!res) {
+    __msan_unpoison(utsname, __msan::struct_utsname_sz);
+  }
+  return res;
+}
+
+INTERCEPTOR(int, epoll_wait, int epfd, void *events, int maxevents,
+    int timeout) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(epoll_wait)(epfd, events, maxevents, timeout);
+  if (res > 0) {
+    __msan_unpoison(events, __msan::struct_epoll_event_sz * res);
+  }
+  return res;
+}
+
+INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents,
+    int timeout, void *sigmask) {
+  ENSURE_MSAN_INITED();
+  int res = REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
+  if (res > 0) {
+    __msan_unpoison(events, __msan::struct_epoll_event_sz * res);
+  }
+  return res;
+}
+
+INTERCEPTOR(ssize_t, recv, int fd, void *buf, size_t len, int flags) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(recv)(fd, buf, len, flags);
+  if (res > 0)
+    __msan_unpoison(buf, res);
+  return res;
+}
+
+INTERCEPTOR(ssize_t, recvfrom, int fd, void *buf, size_t len, int flags,
+    void *srcaddr, void *addrlen) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
+  if (res > 0)
+    __msan_unpoison(buf, res);
+  return res;
+}
+
+INTERCEPTOR(ssize_t, recvmsg, int fd, struct msghdr *msg, int flags) {
+  ENSURE_MSAN_INITED();
+  ssize_t res = REAL(recvmsg)(fd, msg, flags);
+  if (res > 0) {
+    for (size_t i = 0; i < __msan_get_msghdr_iovlen(msg); ++i)
+      __msan_unpoison(__msan_get_msghdr_iov_iov_base(msg, i),
+          __msan_get_msghdr_iov_iov_len(msg, i));
+  }
+  return res;
+}
+
+INTERCEPTOR(void *, calloc, size_t nmemb, size_t size) {
+  GET_MALLOC_STACK_TRACE;
+  if (!msan_inited) {
+    // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
+    const size_t kCallocPoolSize = 1024;
+    static uptr calloc_memory_for_dlsym[kCallocPoolSize];
+    static size_t allocated;
+    size_t size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
+    void *mem = (void*)&calloc_memory_for_dlsym[allocated];
+    allocated += size_in_words;
+    CHECK(allocated < kCallocPoolSize);
+    return mem;
+  }
+  return MsanReallocate(&stack, 0, nmemb * size, sizeof(u64), true);
+}
+
+INTERCEPTOR(void *, realloc, void *ptr, size_t size) {
+  GET_MALLOC_STACK_TRACE;
+  return MsanReallocate(&stack, ptr, size, sizeof(u64), false);
+}
+
+INTERCEPTOR(void *, malloc, size_t size) {
+  GET_MALLOC_STACK_TRACE;
+  return MsanReallocate(&stack, 0, size, sizeof(u64), false);
+}
+
+INTERCEPTOR(void *, mmap, void *addr, size_t length, int prot, int flags,
+            int fd, off_t offset) {
+  ENSURE_MSAN_INITED();
+  void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
+  if (res != (void*)-1)
+    __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
+  return res;
+}
+
+INTERCEPTOR(void *, mmap64, void *addr, size_t length, int prot, int flags,
+            int fd, off64_t offset) {
+  ENSURE_MSAN_INITED();
+  void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
+  if (res != (void*)-1)
+    __msan_unpoison(res, RoundUpTo(length, GetPageSize()));
+  return res;
+}
+
+// static
+void *fast_memset(void *ptr, int c, size_t n) {
+  // hack until we have a really fast internal_memset
+  if (sizeof(uptr) == 8 &&
+      (n % 8) == 0 &&
+      ((uptr)ptr % 8) == 0 &&
+      (c == 0 || c == -1)) {
+    // Printf("memset %p %zd %x\n", ptr, n, c);
+    uptr to_store = c ? -1L : 0L;
+    uptr *p = (uptr*)ptr;
+    for (size_t i = 0; i < n / 8; i++)
+      p[i] = to_store;
+    return ptr;
+  }
+  return internal_memset(ptr, c, n);
+}
+
+// static
+void *fast_memcpy(void *dst, const void *src, size_t n) {
+  // Same hack as in fast_memset above.
+  if (sizeof(uptr) == 8 &&
+      (n % 8) == 0 &&
+      ((uptr)dst % 8) == 0 &&
+      ((uptr)src % 8) == 0) {
+    uptr *d = (uptr*)dst;
+    uptr *s = (uptr*)src;
+    for (size_t i = 0; i < n / 8; i++)
+      d[i] = s[i];
+    return dst;
+  }
+  return internal_memcpy(dst, src, n);
+}
+
+// These interface functions reside here so that they can use
+// fast_memset, etc.
+void __msan_unpoison(void *a, uptr size) {
+  if (!MEM_IS_APP(a)) return;
+  fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size);
+}
+
+void __msan_poison(void *a, uptr size) {
+  if (!MEM_IS_APP(a)) return;
+  fast_memset((void*)MEM_TO_SHADOW((uptr)a),
+              __msan::flags()->poison_heap_with_zeroes ? 0 : -1, size);
+}
+
+void __msan_poison_stack(void *a, uptr size) {
+  if (!MEM_IS_APP(a)) return;
+  fast_memset((void*)MEM_TO_SHADOW((uptr)a),
+              __msan::flags()->poison_stack_with_zeroes ? 0 : -1, size);
+}
+
+void __msan_clear_and_unpoison(void *a, uptr size) {
+  fast_memset(a, 0, size);
+  fast_memset((void*)MEM_TO_SHADOW((uptr)a), 0, size);
+}
+
+void __msan_copy_origin(void *dst, const void *src, uptr size) {
+  if (!__msan_get_track_origins()) return;
+  if (!MEM_IS_APP(dst) || !MEM_IS_APP(src)) return;
+  uptr d = MEM_TO_ORIGIN(dst);
+  uptr s = MEM_TO_ORIGIN(src);
+  uptr beg = d & ~3UL;  // align down.
+  uptr end = (d + size + 3) & ~3UL;  // align up.
+  s = s & ~3UL;  // align down.
+  fast_memcpy((void*)beg, (void*)s, end - beg);
+}
+
+void __msan_copy_poison(void *dst, const void *src, uptr size) {
+  if (!MEM_IS_APP(dst)) return;
+  if (!MEM_IS_APP(src)) return;
+  fast_memcpy((void*)MEM_TO_SHADOW((uptr)dst),
+              (void*)MEM_TO_SHADOW((uptr)src), size);
+  __msan_copy_origin(dst, src, size);
+}
+
+void __msan_move_poison(void *dst, const void *src, uptr size) {
+  if (!MEM_IS_APP(dst)) return;
+  if (!MEM_IS_APP(src)) return;
+  internal_memmove((void*)MEM_TO_SHADOW((uptr)dst),
+         (void*)MEM_TO_SHADOW((uptr)src), size);
+  __msan_copy_origin(dst, src, size);
+}
+
+void *__msan_memcpy(void *dest, const void *src, size_t n) {
+  ENSURE_MSAN_INITED();
+  void *res = fast_memcpy(dest, src, n);
+  __msan_copy_poison(dest, src, n);
+  return res;
+}
+
+void *__msan_memset(void *s, int c, size_t n) {
+  ENSURE_MSAN_INITED();
+  void *res = fast_memset(s, c, n);
+  __msan_unpoison(s, n);
+  return res;
+}
+
+void *__msan_memmove(void *dest, const void *src, size_t n) {
+  ENSURE_MSAN_INITED();
+  void *res = REAL(memmove)(dest, src, n);
+  __msan_move_poison(dest, src, n);
+  return res;
+}
+
+namespace __msan {
+void InitializeInterceptors() {
+  static int inited = 0;
+  CHECK_EQ(inited, 0);
+  CHECK(INTERCEPT_FUNCTION(mmap));
+  CHECK(INTERCEPT_FUNCTION(mmap64));
+  CHECK(INTERCEPT_FUNCTION(posix_memalign));
+  CHECK(INTERCEPT_FUNCTION(malloc));
+  CHECK(INTERCEPT_FUNCTION(calloc));
+  CHECK(INTERCEPT_FUNCTION(realloc));
+  CHECK(INTERCEPT_FUNCTION(free));
+  CHECK(INTERCEPT_FUNCTION(fread));
+  CHECK(INTERCEPT_FUNCTION(fread_unlocked));
+  CHECK(INTERCEPT_FUNCTION(read));
+  CHECK(INTERCEPT_FUNCTION(pread));
+  CHECK(INTERCEPT_FUNCTION(pread64));
+  CHECK(INTERCEPT_FUNCTION(readlink));
+  CHECK(INTERCEPT_FUNCTION(readdir));
+  CHECK(INTERCEPT_FUNCTION(memcpy));
+  CHECK(INTERCEPT_FUNCTION(memset));
+  CHECK(INTERCEPT_FUNCTION(memmove));
+  CHECK(INTERCEPT_FUNCTION(wmemset));
+  CHECK(INTERCEPT_FUNCTION(wmemcpy));
+  CHECK(INTERCEPT_FUNCTION(wmemmove));
+  CHECK(INTERCEPT_FUNCTION(strcpy));  // NOLINT
+  CHECK(INTERCEPT_FUNCTION(strdup));
+  CHECK(INTERCEPT_FUNCTION(strncpy));  // NOLINT
+  CHECK(INTERCEPT_FUNCTION(strlen));
+  CHECK(INTERCEPT_FUNCTION(strnlen));
+  CHECK(INTERCEPT_FUNCTION(gcvt));
+  CHECK(INTERCEPT_FUNCTION(strcat));  // NOLINT
+  CHECK(INTERCEPT_FUNCTION(strncat));  // NOLINT
+  CHECK(INTERCEPT_FUNCTION(strtol));
+  CHECK(INTERCEPT_FUNCTION(strtoll));
+  CHECK(INTERCEPT_FUNCTION(strtoul));
+  CHECK(INTERCEPT_FUNCTION(strtoull));
+  CHECK(INTERCEPT_FUNCTION(vsprintf));
+  CHECK(INTERCEPT_FUNCTION(vsnprintf));
+  CHECK(INTERCEPT_FUNCTION(vswprintf));
+  CHECK(INTERCEPT_FUNCTION(sprintf));  // NOLINT
+  CHECK(INTERCEPT_FUNCTION(snprintf));
+  CHECK(INTERCEPT_FUNCTION(swprintf));
+  CHECK(INTERCEPT_FUNCTION(strftime));
+  CHECK(INTERCEPT_FUNCTION(wcstombs));
+  CHECK(INTERCEPT_FUNCTION(mbstowcs));
+  CHECK(INTERCEPT_FUNCTION(wcslen));
+  CHECK(INTERCEPT_FUNCTION(wcschr));
+  CHECK(INTERCEPT_FUNCTION(wcscpy));
+  CHECK(INTERCEPT_FUNCTION(wcscmp));
+  CHECK(INTERCEPT_FUNCTION(wcstod));
+  CHECK(INTERCEPT_FUNCTION(getenv));
+  CHECK(INTERCEPT_FUNCTION(gettimeofday));
+  CHECK(INTERCEPT_FUNCTION(fcvt));
+  CHECK(INTERCEPT_FUNCTION(__fxstat));
+  CHECK(INTERCEPT_FUNCTION(__xstat));
+  CHECK(INTERCEPT_FUNCTION(__lxstat));
+  CHECK(INTERCEPT_FUNCTION(__fxstat64));
+  CHECK(INTERCEPT_FUNCTION(__xstat64));
+  CHECK(INTERCEPT_FUNCTION(__lxstat64));
+  CHECK(INTERCEPT_FUNCTION(pipe));
+  CHECK(INTERCEPT_FUNCTION(wait));
+  CHECK(INTERCEPT_FUNCTION(waitpid));
+  CHECK(INTERCEPT_FUNCTION(fgets));
+  CHECK(INTERCEPT_FUNCTION(fgets_unlocked));
+  CHECK(INTERCEPT_FUNCTION(getcwd));
+  CHECK(INTERCEPT_FUNCTION(realpath));
+  CHECK(INTERCEPT_FUNCTION(getrlimit));
+  CHECK(INTERCEPT_FUNCTION(getrlimit64));
+  CHECK(INTERCEPT_FUNCTION(statfs));
+  CHECK(INTERCEPT_FUNCTION(fstatfs));
+  CHECK(INTERCEPT_FUNCTION(statfs64));
+  CHECK(INTERCEPT_FUNCTION(fstatfs64));
+  CHECK(INTERCEPT_FUNCTION(uname));
+  CHECK(INTERCEPT_FUNCTION(epoll_wait));
+  CHECK(INTERCEPT_FUNCTION(epoll_pwait));
+  CHECK(INTERCEPT_FUNCTION(recv));
+  CHECK(INTERCEPT_FUNCTION(recvfrom));
+  CHECK(INTERCEPT_FUNCTION(recvmsg));
+  inited = 1;
+}
+}  // namespace __msan
diff --git a/compiler-rt/lib/msan/msan_linux.cc b/compiler-rt/lib/msan/msan_linux.cc
new file mode 100644 (file)
index 0000000..aea2945
--- /dev/null
@@ -0,0 +1,90 @@
+//===-- msan_linux.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.
+//
+// Linux-specific code.
+//===----------------------------------------------------------------------===//
+
+#ifdef __linux__
+
+#include "msan.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include <unwind.h>
+#include <execinfo.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_procmaps.h"
+
+namespace __msan {
+
+static const uptr kMemBeg     = 0x600000000000;
+static const uptr kMemEnd     = 0x7fffffffffff;
+static const uptr kShadowBeg  = MEM_TO_SHADOW(kMemBeg);
+static const uptr kShadowEnd  = MEM_TO_SHADOW(kMemEnd);
+static const uptr kBad1Beg    = 0x100000000;  // 4G
+static const uptr kBad1End    = kShadowBeg - 1;
+static const uptr kBad2Beg    = kShadowEnd + 1;
+static const uptr kBad2End    = kMemBeg - 1;
+static const uptr kOriginsBeg = kBad2Beg;
+static const uptr kOriginsEnd = kBad2End;
+
+bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins) {
+  if (flags()->verbosity) {
+    Printf("__msan_init %p\n", &__msan_init);
+    Printf("Memory   : %p %p\n", kMemBeg, kMemEnd);
+    Printf("Bad2     : %p %p\n", kBad2Beg, kBad2End);
+    Printf("Origins  : %p %p\n", kOriginsBeg, kOriginsEnd);
+    Printf("Shadow   : %p %p\n", kShadowBeg, kShadowEnd);
+    Printf("Bad1     : %p %p\n", kBad1Beg, kBad1End);
+  }
+
+  if (prot1 && !Mprotect(kBad1Beg, kBad1End - kBad1Beg))
+    return false;
+  if (prot2 && !Mprotect(kBad2Beg, kBad2End - kBad2Beg))
+    return false;
+  if (map_shadow) {
+    void *shadow = MmapFixedNoReserve(kShadowBeg, kShadowEnd - kShadowBeg);
+    if (shadow != (void*)kShadowBeg) return false;
+  }
+  if (init_origins) {
+    void *origins = MmapFixedNoReserve(kOriginsBeg, kOriginsEnd - kOriginsBeg);
+    if (origins != (void*)kOriginsBeg) return false;
+  }
+  return true;
+}
+
+static void MsanTrap(int, siginfo_t *siginfo, void *context) {
+  ucontext_t *ucontext = (ucontext_t*)context;
+  uptr pc = ucontext->uc_mcontext.gregs[REG_RIP];
+  uptr bp = ucontext->uc_mcontext.gregs[REG_RBP];
+  PrintWarning(pc + 1 /*1 will be subtracted in StackTrace::Print */, bp);
+  ucontext->uc_mcontext.gregs[REG_RIP] += 2;
+}
+
+void InstallTrapHandler() {
+  struct sigaction sigact;
+  internal_memset(&sigact, 0, sizeof(sigact));
+  sigact.sa_sigaction = MsanTrap;
+  sigact.sa_flags = SA_SIGINFO;
+  CHECK_EQ(0, sigaction(SIGILL, &sigact, 0));
+}
+
+void MsanDie() {
+  _exit(flags()->exit_code);
+}
+}
+
+#endif  // __linux__
diff --git a/compiler-rt/lib/msan/msan_new_delete.cc b/compiler-rt/lib/msan/msan_new_delete.cc
new file mode 100644 (file)
index 0000000..c4efe2e
--- /dev/null
@@ -0,0 +1,51 @@
+//===-- msan_new_delete.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.
+//
+// Interceptors for operators new and delete.
+//===----------------------------------------------------------------------===//
+
+#include "msan.h"
+
+#include <stddef.h>
+
+namespace __msan {
+// This function is a no-op. We need it to make sure that object file
+// with our replacements will actually be loaded from static MSan
+// run-time library at link-time.
+void ReplaceOperatorsNewAndDelete() { }
+}
+
+using namespace __msan;  // NOLINT
+
+// Fake std::nothrow_t to avoid including <new>.
+namespace std {
+  struct nothrow_t {};
+}  // namespace std
+
+
+#define OPERATOR_NEW_BODY \
+  GET_MALLOC_STACK_TRACE; \
+  return MsanReallocate(&stack, 0, size, sizeof(u64), false)
+
+void *operator new(size_t size) { OPERATOR_NEW_BODY; }
+void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
+void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
+void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
+
+#define OPERATOR_DELETE_BODY \
+  if (ptr) MsanDeallocate(ptr)
+
+void operator delete(void *ptr) { OPERATOR_DELETE_BODY; }
+void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; }
+void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
+void operator delete[](void *ptr, std::nothrow_t const&) {
+  OPERATOR_DELETE_BODY;
+}
diff --git a/compiler-rt/lib/msan/msan_platform_limits_posix.cc b/compiler-rt/lib/msan/msan_platform_limits_posix.cc
new file mode 100644 (file)
index 0000000..47ef4ee
--- /dev/null
@@ -0,0 +1,53 @@
+//===-- msan_platform_limits.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.
+//
+// Sizes and layouts of platform-specific POSIX data structures.
+//===----------------------------------------------------------------------===//
+
+#ifdef __linux__
+
+#include "msan.h"
+#include "msan_platform_limits_posix.h"
+
+#include <sys/utsname.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/resource.h>
+#include <sys/vfs.h>
+#include <sys/epoll.h>
+#include <sys/socket.h>
+#include <dirent.h>
+
+namespace __msan {
+  unsigned struct_utsname_sz = sizeof(struct utsname);
+  unsigned struct_stat_sz = sizeof(struct stat);
+  unsigned struct_stat64_sz = sizeof(struct stat64);
+  unsigned struct_rlimit_sz = sizeof(struct rlimit);
+  unsigned struct_rlimit64_sz = sizeof(struct rlimit64);
+  unsigned struct_dirent_sz = sizeof(struct dirent);
+  unsigned struct_statfs_sz = sizeof(struct statfs);
+  unsigned struct_statfs64_sz = sizeof(struct statfs64);
+  unsigned struct_epoll_event_sz = sizeof(struct epoll_event);
+
+  void* __msan_get_msghdr_iov_iov_base(void* msg, int idx) {
+    return ((struct msghdr *)msg)->msg_iov[idx].iov_base;
+  }
+
+  uptr __msan_get_msghdr_iov_iov_len(void* msg, int idx) {
+    return ((struct msghdr *)msg)->msg_iov[idx].iov_len;
+  }
+
+  uptr __msan_get_msghdr_iovlen(void* msg) {
+    return ((struct msghdr *)msg)->msg_iovlen;
+  }
+};
+
+#endif  // __linux__
diff --git a/compiler-rt/lib/msan/msan_platform_limits_posix.h b/compiler-rt/lib/msan/msan_platform_limits_posix.h
new file mode 100644 (file)
index 0000000..9b74908
--- /dev/null
@@ -0,0 +1,34 @@
+//===-- msan_platform_limits.h --------------------------------------------===//
+//
+//                     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.
+//
+// Sizes and layouts of platform-specific data structures.
+//===----------------------------------------------------------------------===//
+
+#ifndef MSAN_PLATFORM_LIMITS_H
+#define MSAN_PLATFORM_LIMITS_H
+
+namespace __msan {
+  extern unsigned struct_utsname_sz;
+  extern unsigned struct_stat_sz;
+  extern unsigned struct_stat64_sz;
+  extern unsigned struct_rlimit_sz;
+  extern unsigned struct_rlimit64_sz;
+  extern unsigned struct_dirent_sz;
+  extern unsigned struct_statfs_sz;
+  extern unsigned struct_statfs64_sz;
+  extern unsigned struct_epoll_event_sz;
+
+  void* __msan_get_msghdr_iov_iov_base(void* msg, int idx);
+  uptr __msan_get_msghdr_iov_iov_len(void* msg, int idx);
+  uptr __msan_get_msghdr_iovlen(void* msg);
+}  // namespace __msan
+
+#endif