[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.

llvm-svn: 166548
This commit is contained in:
Alexey Samsonov 2012-10-24 09:19:11 +00:00
parent 72c19ed386
commit cea6239219
1 changed files with 13 additions and 6 deletions

View File

@ -113,7 +113,11 @@ AsanStats AsanThreadRegistry::GetAccumulatedStats() {
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::GetHeapSize() {
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