From 2eda2e013830537800b68c9217fc14ea7704e618 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Thu, 11 May 2023 10:30:20 -0700 Subject: [PATCH] [HWASAN] Prevent crashes on thread exit I can't figure out how to reproduce this for test, but I see the case on random binaries. The known issue is with GLIBC, others may have a workaround, e.g. Bionic, https://cs.android.com/android/platform/superproject/+/master:bionic/libc/bionic/pthread_exit.cpp;l=149 see signals blocked above. Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D150401 --- compiler-rt/lib/hwasan/hwasan_linux.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/hwasan/hwasan_linux.cpp b/compiler-rt/lib/hwasan/hwasan_linux.cpp index abf92d2..6f5e943 100644 --- a/compiler-rt/lib/hwasan/hwasan_linux.cpp +++ b/compiler-rt/lib/hwasan/hwasan_linux.cpp @@ -302,8 +302,15 @@ extern "C" void __hwasan_thread_exit() { Thread *t = GetCurrentThread(); // Make sure that signal handler can not see a stale current thread pointer. atomic_signal_fence(memory_order_seq_cst); - if (t) + if (t) { + // Block async signals on the thread as the handler can be instrumented. + // After this point instrumented code can't access essential data from TLS + // and will crash. + // Bionic already calls __hwasan_thread_exit with blocked signals. + if (SANITIZER_GLIBC) + BlockSignals(); hwasanThreadList().ReleaseThread(t); + } } # if HWASAN_WITH_INTERCEPTORS -- 2.7.4