From 3e4fc5e37da3f5a925f2cfbac892634618c2061f Mon Sep 17 00:00:00 2001 From: Dante Swift Date: Sat, 11 Feb 2017 21:47:59 -0700 Subject: [PATCH 1/1] Fix memory sanitizer issues with glibc 2.24 --- actions.py | 4 ++ files/msan-fix-glibc-2.24.patch | 115 ++++++++++++++++++++++++++++++++++++++++ pspec.xml | 9 ++++ 3 files changed, 128 insertions(+) create mode 100644 files/msan-fix-glibc-2.24.patch diff --git a/actions.py b/actions.py index a3b5ecd..8adea63 100644 --- a/actions.py +++ b/actions.py @@ -34,6 +34,10 @@ def setup(): if not shelltools.can_access_directory("projects/compiler-rt"): shelltools.system("tar xf ../compiler-rt-%s.src.tar.xz -C projects" % get.srcVERSION()) shelltools.move("projects/compiler-rt-%s.src" % get.srcVERSION(), "projects/compiler-rt") + di = os.getcwd() + os.chdir("projects/compiler-rt") + shelltools.system("patch -p1 < ../../msan-fix-glibc-2.24.patch") + os.chdir(di) shelltools.export("LD_LIBRARY_PATH", "%s/Release/lib/" % os.getcwd()) diff --git a/files/msan-fix-glibc-2.24.patch b/files/msan-fix-glibc-2.24.patch new file mode 100644 index 0000000..8d0ea1e --- /dev/null +++ b/files/msan-fix-glibc-2.24.patch @@ -0,0 +1,115 @@ +From 32278867823b8ec3353eabb9e449bf9fb21838f6 Mon Sep 17 00:00:00 2001 +From: Dante Swift +Date: Sat, 11 Feb 2017 15:37:09 -0700 +Subject: [PATCH 1/1] Fix memory sanitizer issues with glibc 2.24 + +--- + lib/asan/asan_malloc_linux.cc | 6 +++++- + lib/msan/msan_interceptors.cc | 43 +++++++++++++++++++++++++++++++------------ + 2 files changed, 36 insertions(+), 13 deletions(-) + +diff --git a/lib/asan/asan_malloc_linux.cc b/lib/asan/asan_malloc_linux.cc +index 162abd2..d7a22d6 100644 +--- a/lib/asan/asan_malloc_linux.cc ++++ b/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/lib/msan/msan_interceptors.cc b/lib/msan/msan_interceptors.cc +index f23d3ee..2aeaef4 100644 +--- a/lib/msan/msan_interceptors.cc ++++ b/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.11.1 + diff --git a/pspec.xml b/pspec.xml index 4678a4e..9da755f 100644 --- a/pspec.xml +++ b/pspec.xml @@ -20,6 +20,7 @@ 0002-Implement-Solus-s-default-toolchain-options.patch 0001-Completely-ignore-the-borky-FFI_LIBRARY_PATH.patch bug99078.patch + msan-fix-glibc-2.24.patch @@ -175,6 +176,14 @@ + + 02-11-2017 + 3.9.1 + Fix memory sanitizer issues with glibc 2.24 + Dante Swift + dante.swift@zoho.com + + 01-19-2017 3.9.1 -- 2.11.1