[llvm-commits] [compiler-rt] r166548 - /compiler-rt/trunk/lib/asan/asan_thread_registry.cc

Alexey Samsonov samsonov at google.com
Wed Oct 24 02:19:11 PDT 2012


Author: samsonov
Date: Wed Oct 24 04:19:11 2012
New Revision: 166548

URL: http://llvm.org/viewvc/llvm-project?rev=166548&view=rev
Log:
[ASan] Due to data races, ASan malloc stats are inaccurate, which may cause certain ASan interface functions returning negative values (casted to unsigned). Return a reasonable value if such a case is detected.

Modified:
    compiler-rt/trunk/lib/asan/asan_thread_registry.cc

Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.cc?rev=166548&r1=166547&r2=166548&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.cc Wed Oct 24 04:19:11 2012
@@ -113,7 +113,11 @@
 uptr AsanThreadRegistry::GetCurrentAllocatedBytes() {
   ScopedLock lock(&mu_);
   UpdateAccumulatedStatsUnlocked();
-  return accumulated_stats_.malloced - accumulated_stats_.freed;
+  uptr malloced = accumulated_stats_.malloced;
+  uptr freed = accumulated_stats_.freed;
+  // Return sane value if malloced < freed due to racy
+  // way we update accumulated stats.
+  return (malloced > freed) ? malloced - freed : 1;
 }
 
 uptr AsanThreadRegistry::GetHeapSize() {
@@ -125,11 +129,14 @@
 uptr AsanThreadRegistry::GetFreeBytes() {
   ScopedLock lock(&mu_);
   UpdateAccumulatedStatsUnlocked();
-  return accumulated_stats_.mmaped
-         - accumulated_stats_.malloced
-         - accumulated_stats_.malloced_redzones
-         + accumulated_stats_.really_freed
-         + accumulated_stats_.really_freed_redzones;
+  uptr total_free = accumulated_stats_.mmaped
+                  + accumulated_stats_.really_freed
+                  + accumulated_stats_.really_freed_redzones;
+  uptr total_used = accumulated_stats_.malloced
+                  + accumulated_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 several stats counters with a single call to





More information about the llvm-commits mailing list