From fb84376595caefa37cb15cc52b20b2c524201c4d Mon Sep 17 00:00:00 2001 From: guzhihao4 Date: Sat, 31 Aug 2024 10:27:36 +0800 Subject: [PATCH] [Compiler_rt][HWAsan]Install hwasan atexit only if atexit is set Hwasan prints reports even in normal programes. Disable it by default to avoid confusing. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/IACGDH Signed-off-by: guzhihao4 Change-Id: I51c2b59f8a6037f358e3c46968df77c38bc5e1a0 --- compiler-rt/lib/hwasan/hwasan.cpp | 5 +++-- compiler-rt/test/hwasan/TestCases/atexit.c | 26 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/hwasan/TestCases/atexit.c diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp index 41c0c8b51fdf..44fd2f2b03b1 100644 --- a/compiler-rt/lib/hwasan/hwasan.cpp +++ b/compiler-rt/lib/hwasan/hwasan.cpp @@ -355,8 +355,9 @@ __attribute__((constructor(0))) void __hwasan_init() { InitializeInterceptors(); InstallDeadlySignalHandlers(HwasanOnDeadlySignal); - InstallAtExitHandler(); // Needs __cxa_atexit interceptor. - + if (flags()->atexit) { // OHOS_LOCAL + InstallAtExitHandler(); // Needs __cxa_atexit interceptor. + } InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); HwasanTSDInit(); diff --git a/compiler-rt/test/hwasan/TestCases/atexit.c b/compiler-rt/test/hwasan/TestCases/atexit.c new file mode 100644 index 000000000000..9873c2ef270a --- /dev/null +++ b/compiler-rt/test/hwasan/TestCases/atexit.c @@ -0,0 +1,26 @@ +// RUN: %clang_hwasan %s -o %t && %env_hwasan_opts=atexit=1:print_module_map=1 %run %t 2>&1 | FileCheck %s +// REQUIRES: stable-runtime +// OHOS_LOCAL + +#include +#include +#include + +#include + +void *InvalidFreeThread(void *arg) { + char * volatile x = (char*)malloc(10); + x[5] = 0; + free(x); + return NULL; +} + +int main() { + __hwasan_enable_allocator_tagging(); + pthread_t t; + pthread_create(&t, NULL, InvalidFreeThread, NULL); + pthread_join(t, NULL); + // Hwasan prints memory map in exit, check it here. + // CHECK-DAG: Process memory map follows + // CHECK-DAG: End of process memory map +} -- Gitee