diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp index 5f34e021356aff9572dc2b65aeb6f4ad98956bbb..3d49defe73f2af587c4913e9544c5dbda6dd3ad7 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 15f399a0a7f01ba6d436e84a94f25c2c0634cdd8..ec2ba272d4faa19724af834ec35d1f3868e0e7e1 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 c7679cf2436d892f10b631badd59e9b20dc351e9..13d3c6e5e6a088de88cc317c74ec0ce6e8482298 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 1c4c86a26168a51a0f504169c96dc075cf85153e..3c9744cb04177050e3771db13cbedc16bd13b231 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 4be4929284d9247380093fc9db127a5f003ed374..07a2d9d8681c777b0d92f8de8043782da7fdd621 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 8f1126877aa92d0df59eeab2c894344d9a1d54aa..fee24d97c69e5faff16909b4f93a8f510b59c4dd 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 d8edde1e4cde83dd67c3ab0da2431265ad4d782b..d371b0e556184e3a0134fb9ea67e3878171f35d1 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_;