From 049d79eff7782e8dc678bc8def4427fe5ab343eb Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 5 Sep 2016 06:59:12 +0000 Subject: [PATCH] Fix asan with libc6 >= 2.24 (asan-glibc-2.24.diff, backport of 269633) Many thanks to Michael Stapelberg for the great bug report. (Closes: 836723) --- debian/changelog | 8 +++ debian/patches/asan-glibc-2.24.diff | 81 +++++++++++++++++++++++++++++ debian/patches/series | 1 + 3 files changed, 90 insertions(+) create mode 100644 debian/patches/asan-glibc-2.24.diff diff --git a/debian/changelog b/debian/changelog index 887d525e..11797cd4 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,11 @@ +llvm-toolchain-3.8 (1:3.8.1-11) UNRELEASED; urgency=medium + + * Fix asan with libc6 >= 2.24 (asan-glibc-2.24.diff, backport of 269633) + Many thanks to Michael Stapelberg for the great bug report. + (Closes: 836723) + + -- Sylvestre Ledru Mon, 05 Sep 2016 08:57:48 +0200 + llvm-toolchain-3.8 (1:3.8.1-10) unstable; urgency=medium * Fix the usage of jsoncpp in polly. Thanks to James Clarke for the patch diff --git a/debian/patches/asan-glibc-2.24.diff b/debian/patches/asan-glibc-2.24.diff new file mode 100644 index 00000000..133406d7 --- /dev/null +++ b/debian/patches/asan-glibc-2.24.diff @@ -0,0 +1,81 @@ +Index: llvm-toolchain-3.8-3.8.1/compiler-rt/lib/asan/asan_malloc_linux.cc +=================================================================== +--- llvm-toolchain-3.8-3.8.1.orig/compiler-rt/lib/asan/asan_malloc_linux.cc ++++ llvm-toolchain-3.8-3.8.1/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 IsInDlsymAllocPool(const void *ptr) { ++ uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym; ++ return off < sizeof(alloc_memory_for_dlsym); ++} + +-static bool IsInCallocPool(const void *ptr) { +- sptr off = (sptr)ptr - (sptr)calloc_memory_for_dlsym; +- return 0 <= off && off < (sptr)kCallocPoolSize; ++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; diff --git a/debian/patches/series b/debian/patches/series index c80f4ef6..763936d6 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -45,3 +45,4 @@ lldb-link-atomic-cmake.patch D17567-PR23529-Sema-part-of-attrbute-abi_tag-support.diff D18035-PR23529-Mangler-part-of-attrbute-abi_tag-support.diff kfreebsd-support.diff +asan-glibc-2.24.diff