[compiler-rt] eb3be66 - [LSAN] Add GetUserAddr function which returns the user visible address of an internal pointer

Kirill Stoimenov via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 1 16:39:13 PST 2023


Author: Kirill Stoimenov
Date: 2023-02-02T00:39:00Z
New Revision: eb3be66028eac88098e0186cff80e0421949dd69

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

LOG: [LSAN] Add GetUserAddr function which returns the user visible address of an internal pointer

For HWASAN this would be the tagged address. It is the same pointer when pointer tagging is not used. Coincidently this also fixes some test which rely on comparing pointers.

Reviewed By: vitalybuka

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

Added: 
    

Modified: 
    compiler-rt/lib/asan/asan_allocator.cpp
    compiler-rt/lib/hwasan/hwasan_allocator.cpp
    compiler-rt/lib/lsan/lsan_allocator.cpp
    compiler-rt/lib/lsan/lsan_common.cpp
    compiler-rt/lib/lsan/lsan_common.h
    compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
    compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
    compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
    compiler-rt/test/lsan/TestCases/use_after_return.cpp
    compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
    compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
    compiler-rt/test/lsan/TestCases/use_stacks.cpp
    compiler-rt/test/lsan/TestCases/use_unaligned.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_allocator.cpp b/compiler-rt/lib/asan/asan_allocator.cpp
index 74183fcd24273..4c52a45b875c7 100644
--- a/compiler-rt/lib/asan/asan_allocator.cpp
+++ b/compiler-rt/lib/asan/asan_allocator.cpp
@@ -1100,6 +1100,10 @@ uptr GetUserBegin(uptr chunk) {
   return m ? m->Beg() : 0;
 }
 
+uptr GetUserAddr(uptr chunk) {
+  return chunk;
+}
+
 LsanMetadata::LsanMetadata(uptr chunk) {
   metadata_ = chunk ? reinterpret_cast<void *>(chunk - __asan::kChunkHeaderSize)
                     : nullptr;

diff  --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
index 325675ce122cd..a5f42dcbf55bb 100644
--- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
@@ -534,6 +534,13 @@ uptr GetUserBegin(uptr chunk) {
   return reinterpret_cast<uptr>(block);
 }
 
+uptr GetUserAddr(uptr chunk) {
+  tag_t mem_tag = *(tag_t *)__hwasan::MemToShadow(chunk);
+  if (!__hwasan::InTaggableRegion(chunk))
+    return chunk;
+  return AddTagToPointer(chunk, mem_tag);
+}
+
 LsanMetadata::LsanMetadata(uptr chunk) {
   if (__hwasan::InTaggableRegion(chunk))
     CHECK_EQ(UntagAddr(chunk), chunk);

diff  --git a/compiler-rt/lib/lsan/lsan_allocator.cpp b/compiler-rt/lib/lsan/lsan_allocator.cpp
index b18d829a1a2ae..37ba363d479dd 100644
--- a/compiler-rt/lib/lsan/lsan_allocator.cpp
+++ b/compiler-rt/lib/lsan/lsan_allocator.cpp
@@ -275,6 +275,10 @@ uptr GetUserBegin(uptr chunk) {
   return chunk;
 }
 
+uptr GetUserAddr(uptr chunk) {
+  return chunk;
+}
+
 LsanMetadata::LsanMetadata(uptr chunk) {
   metadata_ = Metadata(reinterpret_cast<void *>(chunk));
   CHECK(metadata_);

diff  --git a/compiler-rt/lib/lsan/lsan_common.cpp b/compiler-rt/lib/lsan/lsan_common.cpp
index 1b47e83a1056b..e1eb31a2ae562 100644
--- a/compiler-rt/lib/lsan/lsan_common.cpp
+++ b/compiler-rt/lib/lsan/lsan_common.cpp
@@ -859,7 +859,7 @@ void LeakReport::AddLeakedChunks(const LeakedChunks &chunks) {
       leaks_.push_back(leak);
     }
     if (flags()->report_objects) {
-      LeakedObject obj = {leaks_[i].id, chunk, leaked_size};
+      LeakedObject obj = {leaks_[i].id, GetUserAddr(chunk), leaked_size};
       leaked_objects_.push_back(obj);
     }
   }

diff  --git a/compiler-rt/lib/lsan/lsan_common.h b/compiler-rt/lib/lsan/lsan_common.h
index 0d5c00310842a..0efdbf0ba3e59 100644
--- a/compiler-rt/lib/lsan/lsan_common.h
+++ b/compiler-rt/lib/lsan/lsan_common.h
@@ -131,6 +131,9 @@ void GetAllocatorGlobalRange(uptr *begin, uptr *end);
 uptr PointsIntoChunk(void *p);
 // Returns address of user-visible chunk contained in this allocator chunk.
 uptr GetUserBegin(uptr chunk);
+// Returns user-visible address for chunk. If memory tagging is used this
+// function will return the tagged address.
+uptr GetUserAddr(uptr chunk);
 
 // Wrapper for chunk metadata operations.
 class LsanMetadata {

diff  --git a/compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp b/compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
index 8685de4731b2b..66ce58ead5bb6 100644
--- a/compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
+++ b/compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
@@ -4,9 +4,6 @@
 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=1" %run %t 2>&1
 // RUN: %env_lsan_opts="" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <assert.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -34,4 +31,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp b/compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
index f3a92e2c9af70..905d2c375dcc7 100644
--- a/compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
+++ b/compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
@@ -4,9 +4,6 @@
 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=1" %run %t 2>&1
 // RUN: %env_lsan_opts="" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <stdio.h>
 #include <stdlib.h>
 #include "sanitizer_common/print_address.h"
@@ -21,4 +18,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/pointer_to_self.cpp b/compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
index ed8b1e8ea695f..7404af43463f0 100644
--- a/compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
+++ b/compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
@@ -3,9 +3,6 @@
 // RUN: %clangxx_lsan %s -o %t
 // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0" not %run %t 2>&1 | FileCheck %s
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <stdio.h>
 #include <stdlib.h>
 #include "sanitizer_common/print_address.h"
@@ -18,4 +15,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/use_after_return.cpp b/compiler-rt/test/lsan/TestCases/use_after_return.cpp
index d32621ea7f482..6112edd786bd8 100644
--- a/compiler-rt/test/lsan/TestCases/use_after_return.cpp
+++ b/compiler-rt/test/lsan/TestCases/use_after_return.cpp
@@ -5,9 +5,6 @@
 // RUN: env ASAN_OPTIONS=detect_stack_use_after_return=1 %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=1" %run %t 2>&1
 // RUN: env ASAN_OPTIONS=detect_stack_use_after_return=1 %env_lsan_opts="" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 // Investigate why it does not fail with use_stack=0
 // UNSUPPORTED: arm-linux || armhf-linux
 
@@ -26,4 +23,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp b/compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
index e31bb285034b4..de88c65012e1f 100644
--- a/compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
+++ b/compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
@@ -4,9 +4,6 @@
 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_globals=1" %run %t 2>&1
 // RUN: %env_lsan_opts="" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <stdio.h>
 #include <stdlib.h>
 #include "sanitizer_common/print_address.h"
@@ -21,4 +18,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp b/compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
index 1d8fc614048ff..2948d57dacca5 100644
--- a/compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
+++ b/compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
@@ -4,9 +4,6 @@
 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_globals=1" %run %t 2>&1
 // RUN: %env_lsan_opts="" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <stdio.h>
 #include <stdlib.h>
 #include "sanitizer_common/print_address.h"
@@ -21,4 +18,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/use_stacks.cpp b/compiler-rt/test/lsan/TestCases/use_stacks.cpp
index fc210f8c9ae4c..c9e89a4a93347 100644
--- a/compiler-rt/test/lsan/TestCases/use_stacks.cpp
+++ b/compiler-rt/test/lsan/TestCases/use_stacks.cpp
@@ -4,9 +4,6 @@
 // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=1" %run %t 2>&1
 // RUN: %env_lsan_opts="" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <stdio.h>
 #include <stdlib.h>
 #include "sanitizer_common/print_address.h"
@@ -20,4 +17,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:

diff  --git a/compiler-rt/test/lsan/TestCases/use_unaligned.cpp b/compiler-rt/test/lsan/TestCases/use_unaligned.cpp
index 743e32d19e34a..1bf6370ae12a5 100644
--- a/compiler-rt/test/lsan/TestCases/use_unaligned.cpp
+++ b/compiler-rt/test/lsan/TestCases/use_unaligned.cpp
@@ -3,9 +3,6 @@
 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_unaligned=0" not %run %t 2>&1 | FileCheck %s
 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_unaligned=1" %run %t 2>&1
 
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -23,4 +20,4 @@ int main() {
 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
 // CHECK: LeakSanitizer: detected memory leaks
 // CHECK: [[ADDR]] (1337 bytes)
-// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
+// CHECK: SUMMARY: {{.*}}Sanitizer:


        


More information about the llvm-commits mailing list