From 6dcc1c556692389602c3c1ad26b20047ee2402e9 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Tue, 30 Jan 2024 21:10:50 +0800 Subject: [PATCH] [Sanitizer] Enable Ohos hilog Issue:I900ZT Signed-off-by: yinchuang --- compiler-rt/lib/asan/asan_rtl.cpp | 2 ++ compiler-rt/lib/hwasan/hwasan.cpp | 1 + .../lib/sanitizer_common/sanitizer_common.h | 2 ++ .../sanitizer_linux_libcdep.cpp | 28 +++++++++++++++++++ .../lib/sanitizer_common/sanitizer_win.cpp | 2 ++ compiler-rt/lib/ubsan/ubsan_init.cpp | 1 + 6 files changed, 36 insertions(+) diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp index 2bbf0ac5240a..686834063149 100644 --- a/compiler-rt/lib/asan/asan_rtl.cpp +++ b/compiler-rt/lib/asan/asan_rtl.cpp @@ -433,6 +433,8 @@ static void AsanInitInternal() { // AsanInitInternal -> android_log_write -> __interceptor_strcmp AndroidLogInit(); + OhosLogInit(); // OHOS_LOCAL + ReplaceSystemMalloc(); DisableCoreDumperIfNecessary(); diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp index 235aa73740af..41c0c8b51fdf 100644 --- a/compiler-rt/lib/hwasan/hwasan.cpp +++ b/compiler-rt/lib/hwasan/hwasan.cpp @@ -351,6 +351,7 @@ __attribute__((constructor(0))) void __hwasan_init() { SetPrintfAndReportCallback(AppendToErrorMessageBuffer); // This may call libc -> needs initialized shadow. AndroidLogInit(); + OhosLogInit(); // OHOS_LOCAL InitializeInterceptors(); InstallDeadlySignalHandlers(HwasanOnDeadlySignal); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 08c6062ba067..c4153201a1a0 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -925,9 +925,11 @@ inline void LogMessageOnPrintf(const char *str) {} #if SANITIZER_LINUX || SANITIZER_WIN_TRACE // Initialize Android logging. Any writes before this are silently lost. void AndroidLogInit(); +void OhosLogInit(); // OHOS_LOCAL void SetAbortMessage(const char *); #else inline void AndroidLogInit() {} +inline void OhosLogInit() {} // OHOS_LOCAL // FIXME: MacOS implementation could use CRSetCrashLogMessage. inline void SetAbortMessage(const char *) {} #endif diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp index 182155687d08..785c399d0238 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp @@ -896,7 +896,34 @@ void SetAbortMessage(const char *str) { if (&android_set_abort_message) android_set_abort_message(str); } +// OHOS_LOCAL begin +#elif SANITIZER_OHOS + +void AndroidLogInit() {} + +static atomic_uint8_t ohos_log_initialized; + +void OhosLogInit() { + atomic_store(&ohos_log_initialized, 1, memory_order_release); +} + +static bool ShouldLogAfterPrintf() { + return atomic_load(&ohos_log_initialized, memory_order_acquire); +} + +extern "C" SANITIZER_WEAK_ATTRIBUTE int musl_log(const char *fmt, ...); +void WriteOneLineToSyslog(const char *s) { + if (&musl_log) { + musl_log(s); + } else { + syslog(LOG_INFO, "%s", s); + } +} +void SetAbortMessage(const char *str) {} + #else +void OhosLogInit() {} + void AndroidLogInit() {} static bool ShouldLogAfterPrintf() { return true; } @@ -910,6 +937,7 @@ void LogMessageOnPrintf(const char *str) { if (common_flags()->log_to_syslog && ShouldLogAfterPrintf()) WriteToSyslog(str); } +// OHOS_LOCAL end #endif // SANITIZER_LINUX diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp index e0568c9b62d5..e7b7d6f31f0f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp @@ -1182,6 +1182,8 @@ void LogFullErrorReport(const char *buffer) { TraceLoggingValue(buffer, "AsanReportContents")); } } + +void OhosLogInit(void) {} // OHOS_LOCAL #endif // SANITIZER_WIN_TRACE void InitializePlatformCommonFlags(CommonFlags *cf) {} diff --git a/compiler-rt/lib/ubsan/ubsan_init.cpp b/compiler-rt/lib/ubsan/ubsan_init.cpp index 5802d58896f0..01080567331e 100644 --- a/compiler-rt/lib/ubsan/ubsan_init.cpp +++ b/compiler-rt/lib/ubsan/ubsan_init.cpp @@ -46,6 +46,7 @@ static void CommonStandaloneInit() { __sanitizer::InitializePlatformEarly(); __sanitizer_set_report_path(common_flags()->log_path); AndroidLogInit(); + OhosLogInit(); // OHOS_LOCAL InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); CommonInit(); -- Gitee