[compiler-rt] 66f162a - [HWASAN] Fix __sanitizer_get_allocated_{begin, size}

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 26 18:55:50 PDT 2023


Author: Vitaly Buka
Date: 2023-04-26T18:55:44-07:00
New Revision: 66f162a667bcb9b4a8940eb04083082f1d030ead

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

LOG: [HWASAN] Fix __sanitizer_get_allocated_{begin,size}

HWASAN_ALIASING_MODE needs to untag only
primary allocator pointers.

Reviewed By: kstoimenov, thurston

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

Added: 
    

Modified: 
    compiler-rt/lib/hwasan/hwasan_allocator.cpp
    compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c
    compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
index a21de0e54ad50..ae9ffe88384d8 100644
--- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
@@ -416,7 +416,8 @@ HwasanChunkView FindHeapChunkByAddress(uptr address) {
 }
 
 static const void *AllocationBegin(const void *p) {
-  const void *untagged_ptr = UntagPtr(p);
+  const void *untagged_ptr =
+      __hwasan::InTaggableRegion(reinterpret_cast<uptr>(p)) ? UntagPtr(p) : p;
   if (!untagged_ptr)
     return nullptr;
 
@@ -432,12 +433,14 @@ static const void *AllocationBegin(const void *p) {
   return (const void *)AddTagToPointer((uptr)beg, tag);
 }
 
-static uptr AllocationSize(const void *tagged_ptr) {
-  const void *untagged_ptr = UntagPtr(tagged_ptr);
+static uptr AllocationSize(const void *p) {
+  const void *untagged_ptr =
+      __hwasan::InTaggableRegion(reinterpret_cast<uptr>(p)) ? UntagPtr(p) : p;
   if (!untagged_ptr) return 0;
   const void *beg = allocator.GetBlockBegin(untagged_ptr);
-  Metadata *b = (Metadata *)allocator.GetMetaData(untagged_ptr);
-  if (beg != untagged_ptr) return 0;
+  if (!beg)
+    return 0;
+  Metadata *b = (Metadata *)allocator.GetMetaData(beg);
   return b->GetRequestedSize();
 }
 

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c
index 318c12aa14886..07abb1fa29363 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c
@@ -3,9 +3,6 @@
 // Must not be implemented, no other reason to install interceptors.
 // XFAIL: ubsan
 
-// FIXME: Implement.
-// XFAIL: hwasan-aliasing
-
 #include <assert.h>
 #include <malloc.h>
 #include <sanitizer/allocator_interface.h>

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp b/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp
index 943baaf4f857b..5d757eeacd5fc 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp
@@ -3,9 +3,6 @@
 // Must not be implemented, no other reason to install interceptors.
 // XFAIL: ubsan
 
-// FIXME: Implement.
-// XFAIL: hwasan-aliasing
-
 #include <assert.h>
 #include <sanitizer/allocator_interface.h>
 #include <stdio.h>


        


More information about the llvm-commits mailing list