[compiler-rt] a3587ea - [HWASAN][NFC] Added implementation for the most of the allocator related functions from the LSAN interface.

Kirill Stoimenov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 13:38:22 PST 2022


Author: Kirill Stoimenov
Date: 2022-12-13T21:38:13Z
New Revision: a3587ea6d125356793b6dd4bfb8241570d7b9f3b

URL: https://github.com/llvm/llvm-project/commit/a3587ea6d125356793b6dd4bfb8241570d7b9f3b
DIFF: https://github.com/llvm/llvm-project/commit/a3587ea6d125356793b6dd4bfb8241570d7b9f3b.diff

LOG: [HWASAN][NFC] Added implementation for the most of the allocator related functions from the LSAN interface.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D139964

Added: 
    

Modified: 
    compiler-rt/lib/hwasan/hwasan_allocator.cpp
    compiler-rt/lib/hwasan/hwasan_allocator.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
index 42456e57fc3bd..7208d7955813f 100644
--- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
@@ -71,6 +71,10 @@ bool HwasanChunkView::FromSmallHeap() const {
   return allocator.FromPrimary(reinterpret_cast<void *>(block_));
 }
 
+bool HwasanChunkView::AddrIsInside(uptr addr) const {
+  return (addr >= Beg()) && (addr < Beg() + UsedSize());
+}
+
 inline void Metadata::SetAllocated(u32 stack, u64 size) {
   Thread *t = GetCurrentThread();
   u64 context = t ? t->unique_id() : kMainTid;
@@ -482,6 +486,36 @@ void hwasan_free(void *ptr, StackTrace *stack) {
 // --- Implementation of LSan-specific functions --- {{{1
 namespace __lsan {
 
+void LockAllocator() {
+  __hwasan::HwasanAllocatorLock();
+}
+
+void UnlockAllocator() {
+  __hwasan::HwasanAllocatorUnlock();
+}
+
+void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
+  *begin = (uptr)&__hwasan::allocator;
+  *end = *begin + sizeof(__hwasan::allocator);
+}
+
+uptr PointsIntoChunk(void *p) {
+  uptr addr = reinterpret_cast<uptr>(p);
+  __hwasan::HwasanChunkView view = __hwasan::FindHeapChunkByAddress(addr);
+  if (!view.IsAllocated()) 
+    return 0;
+  uptr chunk = view.Beg();
+  if (view.AddrIsInside(addr))
+    return chunk;
+  if (IsSpecialCaseOfOperatorNew0(chunk, view.UsedSize(), addr))
+    return chunk;
+  return 0;
+}
+
+uptr GetUserBegin(uptr chunk) {
+  return __hwasan::FindHeapChunkByAddress(chunk).Beg();
+}
+
 LsanMetadata::LsanMetadata(uptr chunk) {
   metadata_ = chunk ? reinterpret_cast<__hwasan::Metadata *>(
                           chunk - __hwasan::kChunkHeaderSize)
@@ -515,6 +549,10 @@ u32 LsanMetadata::stack_trace_id() const {
   return m->GetAllocStackId();
 }
 
+void ForEachChunk(ForEachChunkCallback callback, void *arg) {
+  __hwasan::allocator.ForEachChunk(callback, arg);
+}
+
 }  // namespace __lsan
 
 using namespace __hwasan;

diff  --git a/compiler-rt/lib/hwasan/hwasan_allocator.h b/compiler-rt/lib/hwasan/hwasan_allocator.h
index 6ba8e40eb840f..67982cad25438 100644
--- a/compiler-rt/lib/hwasan/hwasan_allocator.h
+++ b/compiler-rt/lib/hwasan/hwasan_allocator.h
@@ -95,6 +95,8 @@ class HwasanChunkView {
   uptr ActualSize() const;     // Size allocated by the allocator.
   u32 GetAllocStackId() const;
   bool FromSmallHeap() const;
+  bool AddrIsInside(uptr addr) const;
+
  private:
   friend class __lsan::LsanMetadata;
   uptr block_;


        


More information about the llvm-commits mailing list