From 5d7fd7ac0d078bfe2b8f1fef057cdbb598790d4c Mon Sep 17 00:00:00 2001 From: Elfgo Date: Thu, 27 Feb 2025 12:39:26 +0000 Subject: [PATCH] [Compiler-RT][HWASAN] Add Quarantine Average Stay Time Change-Id: I3d9aae530105448f1e156545969d982f2c7a11fb Signed-off-by: Elfgo --- compiler-rt/lib/hwasan/hwasan_allocator.cpp | 11 +++++++++ compiler-rt/lib/hwasan/hwasan_flags.inc | 4 +++- compiler-rt/lib/hwasan/hwasan_quarantine.cpp | 24 +++++++++++++++++++- compiler-rt/lib/hwasan/hwasan_quarantine.h | 8 ++++++- compiler-rt/lib/hwasan/hwasan_thread.cpp | 4 ++++ compiler-rt/lib/hwasan/hwasan_thread.h | 2 ++ compiler-rt/lib/hwasan/hwasan_thread_list.h | 21 +++++++++++++++++ 7 files changed, 71 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp index 5f34e021356a..3d49defe73f2 100644 --- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp +++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp @@ -28,6 +28,7 @@ namespace __hwasan { static Allocator allocator; static AllocatorCache fallback_allocator_cache; static SpinMutex fallback_mutex; +static SpinMutex count_mutex; static atomic_uint8_t hwasan_allocator_tagging_enabled; static constexpr tag_t kFallbackAllocTag = 0xBB & kTagMask; @@ -319,6 +320,16 @@ static void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) { tag); } + { + if ((flags()->heap_quarantine_max > 0) && (flags()->enable_heap_quarantine_debug > 0)) { + SpinMutexLock l(&count_mutex); + if (hwasanThreadList().AddCount() == 100000) { + hwasanThreadList().PrintfAverageQuarantineTime(); + hwasanThreadList().ResetCount(); + } + } + } + // OHOS_LOCAL begin int aid = meta->thread_id; if (t) { diff --git a/compiler-rt/lib/hwasan/hwasan_flags.inc b/compiler-rt/lib/hwasan/hwasan_flags.inc index 15f399a0a7f0..ec2ba272d4fa 100644 --- a/compiler-rt/lib/hwasan/hwasan_flags.inc +++ b/compiler-rt/lib/hwasan/hwasan_flags.inc @@ -124,4 +124,6 @@ HWASAN_FLAG(int, heap_quarantine_min, 0, HWASAN_FLAG(int, heap_quarantine_max, 0, "The freed heap size should be smaller than the maximum size before " "it is placed into the heap quarantine.") -// OHOS_LOCAL end \ No newline at end of file +HWASAN_FLAG(int, enable_heap_quarantine_debug, 1, + "Enable Hwasan Quarantine Debug Mode.") +// OHOS_LOCAL end diff --git a/compiler-rt/lib/hwasan/hwasan_quarantine.cpp b/compiler-rt/lib/hwasan/hwasan_quarantine.cpp index c7679cf2436d..13d3c6e5e6a0 100644 --- a/compiler-rt/lib/hwasan/hwasan_quarantine.cpp +++ b/compiler-rt/lib/hwasan/hwasan_quarantine.cpp @@ -15,9 +15,19 @@ //===----------------------------------------------------------------------===// #include "hwasan_quarantine.h" +#include #include "hwasan_allocator.h" +#include "hwasan_thread.h" #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_stackdepot.h" +namespace { + size_t getCurMicroSeconds() { + struct timeval tv; + gettimeofday(&tv, nullptr); + size_t time_now = tv.tv_sec * 1000000ULL + tv.tv_usec; + return time_now; + } +} namespace __hwasan { void HeapQuarantineController::ClearHeapQuarantine(AllocatorCache *cache) { @@ -60,6 +70,11 @@ bool HeapQuarantineController::TryPutInQuarantineWithDealloc( void HeapQuarantineController::PutInQuarantineWithDealloc( uptr ptr, size_t s, u32 aid, u32 fid, AllocatorCache *cache) { + size_t current_time_point = getCurMicroSeconds(); + count++; + persist_interval += heap_quarantine_tail_ * (current_time_point - pre_time_point); + pre_time_point = current_time_point; + if (UNLIKELY(heap_quarantine_tail_ >= flags()->heap_quarantine_thread_max_count)) { // free 1/3 heap_quarantine_list @@ -115,4 +130,11 @@ void HeapQuarantineController::DeallocateWithHeapQuarantcheck( } } -} // namespace __hwasan \ No newline at end of file +void HeapQuarantineController::GetQuarantineStayTimeAndCount(size_t &staytime, size_t &staycount) { + staytime += persist_interval; + staycount += count; + persist_interval = 0; + count = 0; +} + +} // namespace __hwasan diff --git a/compiler-rt/lib/hwasan/hwasan_quarantine.h b/compiler-rt/lib/hwasan/hwasan_quarantine.h index 1c4c86a26168..3c9744cb0417 100644 --- a/compiler-rt/lib/hwasan/hwasan_quarantine.h +++ b/compiler-rt/lib/hwasan/hwasan_quarantine.h @@ -28,6 +28,9 @@ class HeapQuarantineController { private: u32 heap_quarantine_tail_; HeapQuarantine *heap_quarantine_list_; + size_t count{0}; + size_t persist_interval{0}; + size_t pre_time_point{0}; void PutInQuarantineWithDealloc(uptr ptr, size_t s, u32 aid, u32 fid, AllocatorCache *cache); void DeallocateWithHeapQuarantcheck(u32 free_count, AllocatorCache *cache); @@ -42,8 +45,11 @@ class HeapQuarantineController { bool TryPutInQuarantineWithDealloc(uptr ptr, size_t s, u32 aid, u32 fid, AllocatorCache *cache); + + void GetQuarantineStayTimeAndCount(size_t &staytime, size_t &staycount); + }; } // namespace __hwasan -#endif // HWASAN_QUARANTINE_H \ No newline at end of file +#endif // HWASAN_QUARANTINE_H diff --git a/compiler-rt/lib/hwasan/hwasan_thread.cpp b/compiler-rt/lib/hwasan/hwasan_thread.cpp index 4be4929284d9..07a2d9d8681c 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread.cpp +++ b/compiler-rt/lib/hwasan/hwasan_thread.cpp @@ -168,6 +168,10 @@ bool Thread::TryPutInQuarantineWithDealloc(uptr ptr, size_t s, u32 aid, return heap_quarantine_controller()->TryPutInQuarantineWithDealloc( ptr, s, aid, fid, allocator_cache()); } + +void Thread::GetQuarantineStayTimeAndCount(size_t &staytime, size_t &staycount) { + return heap_quarantine_controller()->GetQuarantineStayTimeAndCount(staytime, staycount); +} // OHOS_LOCAL end } // namespace __hwasan diff --git a/compiler-rt/lib/hwasan/hwasan_thread.h b/compiler-rt/lib/hwasan/hwasan_thread.h index 8f1126877aa9..fee24d97c69e 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread.h +++ b/compiler-rt/lib/hwasan/hwasan_thread.h @@ -92,6 +92,8 @@ class Thread { } bool TryPutInQuarantineWithDealloc(uptr ptr, size_t s, u32 aid, u32 fid); + + void GetQuarantineStayTimeAndCount(size_t &staytime, size_t &staycount); // OHOS_LOCAL end private: diff --git a/compiler-rt/lib/hwasan/hwasan_thread_list.h b/compiler-rt/lib/hwasan/hwasan_thread_list.h index d8edde1e4cde..d371b0e55618 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread_list.h +++ b/compiler-rt/lib/hwasan/hwasan_thread_list.h @@ -272,6 +272,24 @@ class HwasanThreadList { bool AllowTracingHeapAllocation() { return trace_heap_allocation_; } // OHOS_LOCAL end +// OHOS_LOCAL begin + void ResetCount() { + deallocate_count_ = 0; + } + size_t AddCount() { + return ++deallocate_count_; + } + void PrintfAverageQuarantineTime() { + VisitAllLiveThreads([&](Thread *t) { + t->GetQuarantineStayTimeAndCount(quarantine_stay_time_, + quarantine_stay_count_); + }); + Printf("quarantinetime: %lld, count: %d, average time: %d usec", + quarantine_stay_time_, quarantine_stay_count_, + quarantine_stay_time_ / quarantine_stay_count_); + } +// OHOS_LOCAL end + private: Thread *AllocThread() { SpinMutexLock l(&free_space_mutex_); @@ -302,6 +320,9 @@ class HwasanThreadList { u64 freed_rb_count_; u64 freed_rb_count_overflow_; bool trace_heap_allocation_; + size_t deallocate_count_{0}; + size_t quarantine_stay_count_{0}; + size_t quarantine_stay_time_{0}; // OHOS_LOCAL end ThreadStats stats_; -- Gitee