From a33ac5ebc04a4e8500d58cfd7f587af08a7c6ee0 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 11 Nov 2016 16:01:54 +0000 Subject: [PATCH] * Fix segfaults in the memory sanitizers (Closes: #842642) Caused by the newer glibc. Many thanks for Nobert Lange for everything * Enable the sanitizers testsuite --- debian/changelog | 8 ++ debian/patches/series | 7 + ...ream-asan-msan-fix-reallocation-logic.diff | 54 ++++++++ .../upstream-fix-asan-initialization.diff | 102 ++++++++++++++ ...m-msan-prevent-initialization-failure.diff | 124 ++++++++++++++++++ debian/rules | 3 + 6 files changed, 298 insertions(+) create mode 100644 debian/patches/upstream-asan-msan-fix-reallocation-logic.diff create mode 100644 debian/patches/upstream-fix-asan-initialization.diff create mode 100644 debian/patches/upstream-msan-prevent-initialization-failure.diff diff --git a/debian/changelog b/debian/changelog index beeb0715..769c0d19 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,11 @@ +llvm-toolchain-3.9 (1:3.9-6) unstable; urgency=medium + + * Fix segfaults in the memory sanitizers (Closes: #842642) + Caused by the newer glibc. Many thanks for Nobert Lange for everything + * Enable the sanitizers testsuite + + -- Sylvestre Ledru Fri, 11 Nov 2016 17:01:38 +0100 + llvm-toolchain-3.9 (1:3.9-5) unstable; urgency=medium * d/p/0011-SimplifyCFG-Hoisting-invalidates-metadata.patch: Also apply bug 29163 diff --git a/debian/patches/series b/debian/patches/series index c1d132f7..5590b18b 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -35,9 +35,16 @@ lldb-link-atomic-cmake.patch disable-llvm-symbolizer-test.diff kfreebsd-support.diff clang-tidy-run-bin.diff +<<<<<<< .working drop-wrong-hack-arm64.patch bug-30342.diff fix-scan-view-path.diff 0011-SimplifyCFG-Hoisting-invalidates-metadata.patch clang-fix-cmpxchg8-detection-on-i386.patch lldb-addversion-suffix-to-llvm-server-exec.patch +#upstream-fix-asan-initialization.diff +upstream-msan-prevent-initialization-failure.diff +upstream-asan-msan-fix-reallocation-logic.diff +silent-llvm-symbolizer.diff +upstream-msan-prevent-initialization-failure.diff +upstream-asan-msan-fix-reallocation-logic.diff diff --git a/debian/patches/upstream-asan-msan-fix-reallocation-logic.diff b/debian/patches/upstream-asan-msan-fix-reallocation-logic.diff new file mode 100644 index 00000000..70c352d3 --- /dev/null +++ b/debian/patches/upstream-asan-msan-fix-reallocation-logic.diff @@ -0,0 +1,54 @@ +From c8a185bc3169b0a6d2cd8beedc77033461830037 Mon Sep 17 00:00:00 2001 +From: Maxim Ostapenko +Date: Mon, 26 Sep 2016 08:11:21 +0000 +Subject: [PATCH] [asan, msan] Fix reallocation logic when + IsInDlsymAllocPool(ptr) is true. + +git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@282389 91177308-0d34-0410-b5e6-96231b3b80d8 +--- + lib/asan/asan_malloc_linux.cc | 8 +++++--- + lib/msan/msan_interceptors.cc | 8 +++++++- + 2 files changed, 12 insertions(+), 4 deletions(-) + +diff --git a/compiler-rt/lib/asan/asan_malloc_linux.cc b/compiler-rt/lib/asan/asan_malloc_linux.cc +index d7a22d6..a78767c 100644 +--- a/compiler-rt/lib/asan/asan_malloc_linux.cc ++++ b/compiler-rt/lib/asan/asan_malloc_linux.cc +@@ -79,10 +79,12 @@ INTERCEPTOR(void*, realloc, void *ptr, uptr size) { + uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym; + uptr copy_size = Min(size, kDlsymAllocPoolSize - offset); + void *new_ptr; +- if (UNLIKELY(!asan_inited)) ++ if (UNLIKELY(!asan_inited)) { + new_ptr = AllocateFromLocalPool(size); +- else +- new_ptr = asan_malloc(size, &stack); ++ } else { ++ copy_size = size; ++ new_ptr = asan_malloc(copy_size, &stack); ++ } + internal_memcpy(new_ptr, ptr, copy_size); + return new_ptr; + } +diff --git a/compiler-rt/lib/msan/msan_interceptors.cc b/compiler-rt/lib/msan/msan_interceptors.cc +index 2aeaef4..93b93ae 100644 +--- a/compiler-rt/lib/msan/msan_interceptors.cc ++++ b/compiler-rt/lib/msan/msan_interceptors.cc +@@ -935,7 +935,13 @@ INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) { + if (UNLIKELY(IsInDlsymAllocPool(ptr))) { + uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym; + uptr copy_size = Min(size, kDlsymAllocPoolSize - offset); +- void *new_ptr = AllocateFromLocalPool(size); ++ void *new_ptr; ++ if (UNLIKELY(!msan_inited)) { ++ new_ptr = AllocateFromLocalPool(copy_size); ++ } else { ++ copy_size = size; ++ new_ptr = MsanReallocate(&stack, ptr, copy_size, sizeof(u64), false); ++ } + internal_memcpy(new_ptr, ptr, copy_size); + return new_ptr; + } +-- +2.10.2 + diff --git a/debian/patches/upstream-fix-asan-initialization.diff b/debian/patches/upstream-fix-asan-initialization.diff new file mode 100644 index 00000000..d9e71178 --- /dev/null +++ b/debian/patches/upstream-fix-asan-initialization.diff @@ -0,0 +1,102 @@ +From 570ee9dd7a6f90b0370a86535cbde6738d0ccf67 Mon Sep 17 00:00:00 2001 +From: Maxim Ostapenko +Date: Mon, 16 May 2016 07:20:53 +0000 +Subject: [PATCH] [asan] Fix asan initialization failure with newer (2.23+) + glibc in use. + +This patch tries to fix https://llvm.org/bugs/show_bug.cgi?id=27310 by using the same hack for malloc as we use for calloc: allocate corresponding memory from internal buffer when ASan is not initialized. +This way we could avoid nasty '==6987==AddressSanitizer CHECK failed: ../../../../libsanitizer/asan/asan_rtl.cc:556 "((!asan_init_is_running && "ASan init calls itself!")) != (0)" (0x0, 0x0)' errors in +environments with glibc 2.23+ in use, where _dl_signal_error, called from dlsym for undefined symbols calls malloc in order to get a buffer for error message. + +Differential Revision: http://reviews.llvm.org/D20235 + + +git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@269633 91177308-0d34-0410-b5e6-96231b3b80d8 +--- + lib/asan/asan_malloc_linux.cc | 42 ++++++++++++++++++++++++------------------ + 1 file changed, 24 insertions(+), 18 deletions(-) + +diff --git a/compiler-rt/lib/asan/asan_malloc_linux.cc b/compiler-rt/lib/asan/asan_malloc_linux.cc +index a81f19f..162abd2 100644 +--- a/compiler-rt/lib/asan/asan_malloc_linux.cc ++++ b/compiler-rt/lib/asan/asan_malloc_linux.cc +@@ -26,52 +26,58 @@ + // ---------------------- Replacement functions ---------------- {{{1 + using namespace __asan; // NOLINT + +-static const uptr kCallocPoolSize = 1024; +-static uptr calloc_memory_for_dlsym[kCallocPoolSize]; ++static uptr allocated_for_dlsym; ++static const uptr kDlsymAllocPoolSize = 1024; ++static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize]; + +-static bool IsInCallocPool(const void *ptr) { +- sptr off = (sptr)ptr - (sptr)calloc_memory_for_dlsym; +- return 0 <= off && off < (sptr)kCallocPoolSize; ++static bool IsInDlsymAllocPool(const void *ptr) { ++ uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym; ++ return off < sizeof(alloc_memory_for_dlsym); ++} ++ ++static void *AllocateFromLocalPool(uptr size_in_bytes) { ++ uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize; ++ void *mem = (void*)&alloc_memory_for_dlsym[allocated_for_dlsym]; ++ allocated_for_dlsym += size_in_words; ++ CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize); ++ return mem; + } + + INTERCEPTOR(void, free, void *ptr) { + GET_STACK_TRACE_FREE; +- if (UNLIKELY(IsInCallocPool(ptr))) ++ if (UNLIKELY(IsInDlsymAllocPool(ptr))) + return; + asan_free(ptr, &stack, FROM_MALLOC); + } + + INTERCEPTOR(void, cfree, void *ptr) { + GET_STACK_TRACE_FREE; +- if (UNLIKELY(IsInCallocPool(ptr))) ++ if (UNLIKELY(IsInDlsymAllocPool(ptr))) + return; + asan_free(ptr, &stack, FROM_MALLOC); + } + + INTERCEPTOR(void*, malloc, uptr size) { ++ if (UNLIKELY(!asan_inited)) ++ // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym. ++ return AllocateFromLocalPool(size); + GET_STACK_TRACE_MALLOC; + return asan_malloc(size, &stack); + } + + INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) { +- if (UNLIKELY(!asan_inited)) { ++ if (UNLIKELY(!asan_inited)) + // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym. +- static uptr allocated; +- uptr 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 AllocateFromLocalPool(nmemb * size); + GET_STACK_TRACE_MALLOC; + return asan_calloc(nmemb, size, &stack); + } + + INTERCEPTOR(void*, realloc, void *ptr, uptr size) { + GET_STACK_TRACE_MALLOC; +- if (UNLIKELY(IsInCallocPool(ptr))) { +- uptr offset = (uptr)ptr - (uptr)calloc_memory_for_dlsym; +- uptr copy_size = Min(size, kCallocPoolSize - offset); ++ if (UNLIKELY(IsInDlsymAllocPool(ptr))) { ++ uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym; ++ uptr copy_size = Min(size, kDlsymAllocPoolSize - offset); + void *new_ptr = asan_malloc(size, &stack); + internal_memcpy(new_ptr, ptr, copy_size); + return new_ptr; +-- +2.10.2 + diff --git a/debian/patches/upstream-msan-prevent-initialization-failure.diff b/debian/patches/upstream-msan-prevent-initialization-failure.diff new file mode 100644 index 00000000..ba52b590 --- /dev/null +++ b/debian/patches/upstream-msan-prevent-initialization-failure.diff @@ -0,0 +1,124 @@ +From 827ea206c1078fc7c7da287984a7ba4563390589 Mon Sep 17 00:00:00 2001 +From: Maxim Ostapenko +Date: Fri, 23 Sep 2016 07:40:55 +0000 +Subject: [PATCH] [msan] Prevent initialization failure with newer (2.23+) + glibc in use. + +This patch is pretty the same as http://reviews.llvm.org/D20235 that we used +for ASan. Using the same hack for MSan fixes its initialization with newer +Glibc in use. + +Differential Revision: https://reviews.llvm.org/D24736 + + +git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@282232 91177308-0d34-0410-b5e6-96231b3b80d8 +--- + lib/asan/asan_malloc_linux.cc | 6 +++++- + lib/msan/msan_interceptors.cc | 43 +++++++++++++++++++++++++++++++------------ + 2 files changed, 36 insertions(+), 13 deletions(-) + +diff --git a/compiler-rt/lib/asan/asan_malloc_linux.cc b/compiler-rt/lib/asan/asan_malloc_linux.cc +index 162abd2..d7a22d6 100644 +--- a/compiler-rt/lib/asan/asan_malloc_linux.cc ++++ b/compiler-rt/lib/asan/asan_malloc_linux.cc +@@ -78,7 +78,11 @@ INTERCEPTOR(void*, realloc, void *ptr, uptr size) { + if (UNLIKELY(IsInDlsymAllocPool(ptr))) { + uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym; + uptr copy_size = Min(size, kDlsymAllocPoolSize - offset); +- void *new_ptr = asan_malloc(size, &stack); ++ void *new_ptr; ++ if (UNLIKELY(!asan_inited)) ++ new_ptr = AllocateFromLocalPool(size); ++ else ++ new_ptr = asan_malloc(size, &stack); + internal_memcpy(new_ptr, ptr, copy_size); + return new_ptr; + } +diff --git a/compiler-rt/lib/msan/msan_interceptors.cc b/compiler-rt/lib/msan/msan_interceptors.cc +index f23d3ee..2aeaef4 100644 +--- a/compiler-rt/lib/msan/msan_interceptors.cc ++++ b/compiler-rt/lib/msan/msan_interceptors.cc +@@ -64,6 +64,23 @@ bool IsInInterceptorScope() { + return in_interceptor_scope; + } + ++static uptr allocated_for_dlsym; ++static const uptr kDlsymAllocPoolSize = 1024; ++static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize]; ++ ++static bool IsInDlsymAllocPool(const void *ptr) { ++ uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym; ++ return off < sizeof(alloc_memory_for_dlsym); ++} ++ ++static void *AllocateFromLocalPool(uptr size_in_bytes) { ++ uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize; ++ void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym]; ++ allocated_for_dlsym += size_in_words; ++ CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize); ++ return mem; ++} ++ + #define ENSURE_MSAN_INITED() do { \ + CHECK(!msan_init_is_running); \ + if (!msan_inited) { \ +@@ -227,14 +244,14 @@ INTERCEPTOR(void *, pvalloc, SIZE_T size) { + + INTERCEPTOR(void, free, void *ptr) { + GET_MALLOC_STACK_TRACE; +- if (!ptr) return; ++ if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return; + MsanDeallocate(&stack, ptr); + } + + #if !SANITIZER_FREEBSD + INTERCEPTOR(void, cfree, void *ptr) { + GET_MALLOC_STACK_TRACE; +- if (!ptr) return; ++ if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr))) return; + MsanDeallocate(&stack, ptr); + } + #define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree) +@@ -907,27 +924,29 @@ INTERCEPTOR(int, epoll_pwait, int epfd, void *events, int maxevents, + + INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) { + GET_MALLOC_STACK_TRACE; +- if (UNLIKELY(!msan_inited)) { ++ if (UNLIKELY(!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 AllocateFromLocalPool(nmemb * size); + return MsanCalloc(&stack, nmemb, size); + } + + INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) { + GET_MALLOC_STACK_TRACE; ++ if (UNLIKELY(IsInDlsymAllocPool(ptr))) { ++ uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym; ++ uptr copy_size = Min(size, kDlsymAllocPoolSize - offset); ++ void *new_ptr = AllocateFromLocalPool(size); ++ internal_memcpy(new_ptr, ptr, copy_size); ++ return new_ptr; ++ } + return MsanReallocate(&stack, ptr, size, sizeof(u64), false); + } + + INTERCEPTOR(void *, malloc, SIZE_T size) { + GET_MALLOC_STACK_TRACE; ++ if (UNLIKELY(!msan_inited)) ++ // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym. ++ return AllocateFromLocalPool(size); + return MsanReallocate(&stack, nullptr, size, sizeof(u64), false); + } + +-- +2.10.2 + diff --git a/debian/rules b/debian/rules index 60fc7edb..86f44358 100755 --- a/debian/rules +++ b/debian/rules @@ -457,6 +457,9 @@ ifeq (${LLD_ENABLE},yes) $(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-lld || true endif +# Sanitizer + $(MAKE) $(NJOBS) -C $(TARGET_BUILD) check-sanitize || true + # LLDB tests (CMake) ifeq (,$(filter $(DEB_HOST_ARCH), $(LLDB_DISABLE_ARCHS) armhf armel)) ifneq (,$(filter codecoverageDISABLED,$(DEB_BUILD_OPTIONS)))