From 0381f55dcc046ee59916a3898b23460a13026c83 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Fri, 1 Dec 2023 18:31:09 +0800 Subject: [PATCH] Add -fno-emulated-tls for gwp_asan Issue:I8L5DW Signed-off-by: yinchuang --- compiler-rt/lib/gwp_asan/CMakeLists.txt | 5 +++++ compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp | 7 +++++++ compiler-rt/lib/gwp_asan/guarded_pool_allocator.h | 1 + compiler-rt/lib/gwp_asan/mutex.h | 1 + .../guarded_pool_allocator_posix.cpp | 6 ++++++ .../lib/gwp_asan/platform_specific/mutex_posix.cpp | 12 ++++++++++++ 6 files changed, 32 insertions(+) diff --git a/compiler-rt/lib/gwp_asan/CMakeLists.txt b/compiler-rt/lib/gwp_asan/CMakeLists.txt index 27d67036ed56..c79ea4427b0c 100644 --- a/compiler-rt/lib/gwp_asan/CMakeLists.txt +++ b/compiler-rt/lib/gwp_asan/CMakeLists.txt @@ -41,6 +41,11 @@ set(GWP_ASAN_HEADERS set(GWP_ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS} -fno-rtti -fno-exceptions -nostdinc++ -pthread -fno-omit-frame-pointer) append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC GWP_ASAN_CFLAGS) +# OHOS_LOCAL begin +if(OHOS) +list(APPEND GWP_ASAN_CFLAGS -fno-emulated-tls) +endif() +# OHOS_LOCAL end # append_list_if(COMPILER_RT_HAS_SANITIZER_COMMON ${SANITIZER_COMMON_CFLAGS} GWP_ASAN_CFLAGS) # Remove -stdlib= which is unused when passing -nostdinc++. diff --git a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp index 5bf1284e1f89..ef3923eb788d 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp @@ -120,6 +120,13 @@ void GuardedPoolAllocator::enable() { BacktraceMutex.unlock(); } +// OHOS_LOCAL begin +void GuardedPoolAllocator::enableAtFork() { + PoolMutex.unlockAtFork(); + BacktraceMutex.unlockAtFork(); +} +// OHOS_LOCAL end + void GuardedPoolAllocator::iterate(void *Base, size_t Size, iterate_callback Cb, void *Arg) { uintptr_t Start = reinterpret_cast(Base); diff --git a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h index 152028acd822..f1086651395f 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h @@ -63,6 +63,7 @@ public: // to allocate memory, until enable() is called. void disable(); void enable(); + void enableAtFork(); // OHOS_LOCAL typedef void (*iterate_callback)(uintptr_t base, size_t size, void *arg); // Execute the callback Cb for every allocation the lies in [Base, Base + diff --git a/compiler-rt/lib/gwp_asan/mutex.h b/compiler-rt/lib/gwp_asan/mutex.h index 34b91a2880dd..299eae6719ea 100644 --- a/compiler-rt/lib/gwp_asan/mutex.h +++ b/compiler-rt/lib/gwp_asan/mutex.h @@ -25,6 +25,7 @@ public: bool tryLock(); // Unlock the mutex. void unlock(); + void unlockAtFork(); // OHOS_LOCAL }; class ScopedLock { diff --git a/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp b/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp index 149b74238f0d..f2db40aa3cb9 100644 --- a/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp +++ b/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp @@ -106,7 +106,13 @@ void GuardedPoolAllocator::installAtFork() { }; auto Enable = []() { if (auto *S = getSingleton()) +// OHOS_LOCAL begin +#if defined(__OHOS__) + S->enableAtFork(); +#else S->enable(); +#endif +// OHOS_LOCAL end }; pthread_atfork(Disable, Enable, Enable); } diff --git a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp index 8bd405e1074c..a3ee11c0e78a 100644 --- a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp +++ b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp @@ -27,4 +27,16 @@ void Mutex::unlock() { // Remove warning for non-debug builds. (void)Status; } + +// OHOS_LOCAL begin +// 1、OHOS uses recursive locks to avoid deadlock, such as this call chain: +// `gwp_asan malloc -> find double free -> get lock -> trigger segv -> +// segv handler -> malloc -> gwp_asan malloc` +// 2、It will be failed to unlock recursive lock after fork because tid changes, +// so we just create a new recursive lock here. +void Mutex::unlockAtFork() { + Mu = {{{PTHREAD_MUTEX_RECURSIVE}}}; +} +// OHOS_LOCAL end + } // namespace gwp_asan -- Gitee