[compiler-rt] [ASan] return 0 for current allocated bytes if malloc/free are never happend (PR #67394)

Chen Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 26 06:02:50 PDT 2023


https://github.com/chenzheng1030 updated https://github.com/llvm/llvm-project/pull/67394

>From 39a0751c14434b62f2cf756adc1c4a6aac15853e Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Tue, 26 Sep 2023 01:44:36 -0400
Subject: [PATCH 1/2] return 0 instead of 1 if malloc/free is never happened.

---
 compiler-rt/lib/asan/asan_stats.cpp                             | 2 +-
 .../test/asan/TestCases/Posix/current_allocated_bytes.cpp       | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/asan/asan_stats.cpp b/compiler-rt/lib/asan/asan_stats.cpp
index 9a715ea76fee756..98196ca0401e9a2 100644
--- a/compiler-rt/lib/asan/asan_stats.cpp
+++ b/compiler-rt/lib/asan/asan_stats.cpp
@@ -142,7 +142,7 @@ uptr __sanitizer_get_current_allocated_bytes() {
   uptr freed = stats.freed;
   // Return sane value if malloced < freed due to racy
   // way we update accumulated stats.
-  return (malloced > freed) ? malloced - freed : 1;
+  return (malloced > freed) ? malloced - freed : 0;
 }
 
 uptr __sanitizer_get_heap_size() {
diff --git a/compiler-rt/test/asan/TestCases/Posix/current_allocated_bytes.cpp b/compiler-rt/test/asan/TestCases/Posix/current_allocated_bytes.cpp
index c49e433b1e8bfc6..7c83dc6106f3889 100644
--- a/compiler-rt/test/asan/TestCases/Posix/current_allocated_bytes.cpp
+++ b/compiler-rt/test/asan/TestCases/Posix/current_allocated_bytes.cpp
@@ -17,7 +17,7 @@ void* allocate(void *arg) {
 }
 
 void* check_stats(void *arg) {
-  assert(__sanitizer_get_current_allocated_bytes() > 0);
+  assert(__sanitizer_get_current_allocated_bytes() >= 0);
   return 0;
 }
 

>From 9bf4bc10e3edc4ef22239fded08fc6c90ea6576f Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Tue, 26 Sep 2023 09:02:27 -0400
Subject: [PATCH 2/2] address comments

---
 compiler-rt/lib/asan/asan_stats.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/lib/asan/asan_stats.cpp b/compiler-rt/lib/asan/asan_stats.cpp
index 98196ca0401e9a2..78cb30ec763d818 100644
--- a/compiler-rt/lib/asan/asan_stats.cpp
+++ b/compiler-rt/lib/asan/asan_stats.cpp
@@ -161,7 +161,7 @@ uptr __sanitizer_get_free_bytes() {
                   + stats.malloced_redzones;
   // Return sane value if total_free < total_used due to racy
   // way we update accumulated stats.
-  return (total_free > total_used) ? total_free - total_used : 1;
+  return (total_free > total_used) ? total_free - total_used : 0;
 }
 
 uptr __sanitizer_get_unmapped_bytes() {



More information about the llvm-commits mailing list