[compiler-rt] r248829 - [msan] Early allocator initialization.
Evgeniy Stepanov via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 14:28:54 PDT 2015
Author: eugenis
Date: Tue Sep 29 16:28:54 2015
New Revision: 248829
URL: http://llvm.org/viewvc/llvm-project?rev=248829&view=rev
Log:
[msan] Early allocator initialization.
Map MSan heap space early (in __msan_init) so that user code can not
accidentally (i.e. w/o MAP_FIXED) create a conflicting mapping.
Added:
compiler-rt/trunk/test/msan/allocator_mapping.cc
Modified:
compiler-rt/trunk/lib/msan/msan.cc
compiler-rt/trunk/lib/msan/msan.h
compiler-rt/trunk/lib/msan/msan_allocator.cc
Modified: compiler-rt/trunk/lib/msan/msan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan.cc?rev=248829&r1=248828&r2=248829&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan.cc (original)
+++ compiler-rt/trunk/lib/msan/msan.cc Tue Sep 29 16:28:54 2015
@@ -415,6 +415,8 @@ void __msan_init() {
MsanTSDInit(MsanTSDDtor);
+ MsanAllocatorInit();
+
MsanThread *main_thread = MsanThread::Create(0, 0);
SetCurrentThread(main_thread);
main_thread->ThreadStart();
Modified: compiler-rt/trunk/lib/msan/msan.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan.h?rev=248829&r1=248828&r2=248829&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan.h (original)
+++ compiler-rt/trunk/lib/msan/msan.h Tue Sep 29 16:28:54 2015
@@ -189,6 +189,7 @@ bool InitShadow(bool init_origins);
char *GetProcSelfMaps();
void InitializeInterceptors();
+void MsanAllocatorInit();
void MsanAllocatorThreadFinish();
void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size);
void *MsanReallocate(StackTrace *stack, void *oldp, uptr size,
Modified: compiler-rt/trunk/lib/msan/msan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan_allocator.cc?rev=248829&r1=248828&r2=248829&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_allocator.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_allocator.cc Tue Sep 29 16:28:54 2015
@@ -87,12 +87,7 @@ static Allocator allocator;
static AllocatorCache fallback_allocator_cache;
static SpinMutex fallback_mutex;
-static int inited = 0;
-
-static inline void Init() {
- if (inited) return;
- __msan_init();
- inited = true; // this must happen before any threads are created.
+void MsanAllocatorInit() {
allocator.Init(common_flags()->allocator_may_return_null);
}
@@ -108,7 +103,6 @@ void MsanThreadLocalMallocStorage::Commi
static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
bool zeroise) {
- Init();
if (size > kMaxAllowedMallocSize) {
Report("WARNING: MemorySanitizer failed to allocate %p bytes\n",
(void *)size);
@@ -143,7 +137,6 @@ static void *MsanAllocate(StackTrace *st
void MsanDeallocate(StackTrace *stack, void *p) {
CHECK(p);
- Init();
MSAN_FREE_HOOK(p);
Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
uptr size = meta->requested_size;
@@ -170,7 +163,6 @@ void MsanDeallocate(StackTrace *stack, v
}
void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
- Init();
if (CallocShouldReturnNullDueToOverflow(size, nmemb))
return allocator.ReturnNullOrDie();
return MsanReallocate(stack, 0, nmemb * size, sizeof(u64), true);
Added: compiler-rt/trunk/test/msan/allocator_mapping.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/msan/allocator_mapping.cc?rev=248829&view=auto
==============================================================================
--- compiler-rt/trunk/test/msan/allocator_mapping.cc (added)
+++ compiler-rt/trunk/test/msan/allocator_mapping.cc Tue Sep 29 16:28:54 2015
@@ -0,0 +1,31 @@
+// Test that a module constructor can not map memory over the MSan heap
+// (without MAP_FIXED, of course). Current implementation ensures this by
+// mapping the heap early, in __msan_init.
+//
+// RUN: %clangxx_msan -O0 %s -o %t_1
+// RUN: %clangxx_msan -O0 -DHEAP_ADDRESS=$(%run %t_1) %s -o %t_2 && %run %t_2
+
+#include <assert.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+
+#ifdef HEAP_ADDRESS
+struct A {
+ A() {
+ void *const hint = reinterpret_cast<void *>(HEAP_ADDRESS);
+ void *p = mmap(hint, 4096, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ // This address must be already mapped. Check that mmap() succeeds, but at a
+ // different address.
+ assert(p != reinterpret_cast<void *>(-1));
+ assert(p != hint);
+ }
+} a;
+#endif
+
+int main() {
+ void *p = malloc(10);
+ printf("0x%zx\n", reinterpret_cast<size_t>(p) & (~0xfff));
+ free(p);
+}
More information about the llvm-commits
mailing list