[LSAN] Add GetUserAddr function which returns the user visible address of an internal...
authorKirill Stoimenov <kstoimenov@google.com>
Wed, 1 Feb 2023 22:00:13 +0000 (22:00 +0000)
committerKirill Stoimenov <kstoimenov@google.com>
Thu, 2 Feb 2023 00:39:00 +0000 (00:39 +0000)
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

13 files changed:
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

index 74183fc..4c52a45 100644 (file)
@@ -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;
index 325675c..a5f42dc 100644 (file)
@@ -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);
index b18d829..37ba363 100644 (file)
@@ -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_);
index 1b47e83..e1eb31a 100644 (file)
@@ -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);
     }
   }
index 0d5c003..0efdbf0 100644 (file)
@@ -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 {
index 8685de4..66ce58e 100644 (file)
@@ -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:
index f3a92e2..905d2c3 100644 (file)
@@ -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:
index ed8b1e8..7404af4 100644 (file)
@@ -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:
index d32621e..6112edd 100644 (file)
@@ -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:
index e31bb28..de88c65 100644 (file)
@@ -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:
index 1d8fc61..2948d57 100644 (file)
@@ -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:
index fc210f8..c9e89a4 100644 (file)
@@ -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:
index 743e32d..1bf6370 100644 (file)
@@ -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: