From 56e41b14ecc85238f4ae13020bb68b1f838a11f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=87=91=E6=88=90?= Date: Wed, 18 Dec 2024 12:23:17 +0800 Subject: [PATCH 1/2] [OHOS aarch64] [Sanitizer] [HWAsan] HWAsan fail test fix fix test case: malloc_bisect.c Avoid printing hwasan's log with ohos_dfx_log, when either malloc_bisect_left or malloc_bisect_right flag is not 0 and malloc_bisect_dump flag is set to true Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/IB0NX9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡金成 Change-Id: I95a52f9f43a1260a49522a2d6a16a18bd65fbe2c --- .../sanitizer_linux_libcdep.cpp | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp index 54c2526fac82..a413a31d06c6 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp @@ -68,6 +68,10 @@ #include #endif +#if SANITIZER_OHOS + #include "../hwasan/hwasan.h" +#endif + #if SANITIZER_ANDROID #include #if !defined(CPU_COUNT) && !defined(__aarch64__) @@ -1089,7 +1093,23 @@ void LogMessageOnPrintf(const char *str) { #if SANITIZER_OHOS // We need to call it before "WriteToSyslog" because "WriteToSyslog" will remove "\n". if (&ohos_dfx_log) { - ohos_dfx_log(str); + // Avoid printing hwasan's log with ohos_dfx_log, when either malloc_bisect_left or malloc_bisect_right flag is not 0 and malloc_bisect_dump flag is set to true. + // Because in that context, printing log with ohos_dfx_log will cause infinite loop. + // In that context, every memory allocation will invariably be printed into the log, while ohos_dfx_log's log mechanim calls "new" to create its own object. + // The memory allocation by ohos_dfx_log's "new" action is hooked by hwasan, so it will be printed to the log, while the printing will call ohos_dfx_log again... + // It becomes a infinite loop: hwasan_malloc -> malloc_bisect_dump -> Printf -> hwasan_malloc -> malloc_bisect_dump -> Printf... + bool canUseDFX = true; + if (__hwasan::hwasan_inited > 0) { + uptr left = __hwasan::flags()->malloc_bisect_left; + uptr right = __hwasan::flags()->malloc_bisect_right; + bool malloc_bisect_dump = __hwasan::flags()->malloc_bisect_dump; + if (UNLIKELY(left != 0 || right != 0) && malloc_bisect_dump) { + canUseDFX = false; + } + } + if (canUseDFX) { + ohos_dfx_log(str); + } } #endif if (common_flags()->log_to_syslog && ShouldLogAfterPrintf()) -- Gitee From 2d4f5e343781bde854ea3c45aaba8da3cb3f195c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=87=91=E6=88=90?= Date: Thu, 2 Jan 2025 08:51:48 +0000 Subject: [PATCH 2/2] Revert "[OHOS aarch64] [Sanitizer] [HWAsan] HWAsan fail test fix" This reverts commit 56e41b14ecc85238f4ae13020bb68b1f838a11f7. --- .../sanitizer_linux_libcdep.cpp | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp index a413a31d06c6..54c2526fac82 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp @@ -68,10 +68,6 @@ #include #endif -#if SANITIZER_OHOS - #include "../hwasan/hwasan.h" -#endif - #if SANITIZER_ANDROID #include #if !defined(CPU_COUNT) && !defined(__aarch64__) @@ -1093,23 +1089,7 @@ void LogMessageOnPrintf(const char *str) { #if SANITIZER_OHOS // We need to call it before "WriteToSyslog" because "WriteToSyslog" will remove "\n". if (&ohos_dfx_log) { - // Avoid printing hwasan's log with ohos_dfx_log, when either malloc_bisect_left or malloc_bisect_right flag is not 0 and malloc_bisect_dump flag is set to true. - // Because in that context, printing log with ohos_dfx_log will cause infinite loop. - // In that context, every memory allocation will invariably be printed into the log, while ohos_dfx_log's log mechanim calls "new" to create its own object. - // The memory allocation by ohos_dfx_log's "new" action is hooked by hwasan, so it will be printed to the log, while the printing will call ohos_dfx_log again... - // It becomes a infinite loop: hwasan_malloc -> malloc_bisect_dump -> Printf -> hwasan_malloc -> malloc_bisect_dump -> Printf... - bool canUseDFX = true; - if (__hwasan::hwasan_inited > 0) { - uptr left = __hwasan::flags()->malloc_bisect_left; - uptr right = __hwasan::flags()->malloc_bisect_right; - bool malloc_bisect_dump = __hwasan::flags()->malloc_bisect_dump; - if (UNLIKELY(left != 0 || right != 0) && malloc_bisect_dump) { - canUseDFX = false; - } - } - if (canUseDFX) { - ohos_dfx_log(str); - } + ohos_dfx_log(str); } #endif if (common_flags()->log_to_syslog && ShouldLogAfterPrintf()) -- Gitee