[llvm] r326834 - PrintStatistics() and PrintStatisticsJSON() should take StatLock

Daniel Sanders via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 6 13:16:42 PST 2018


Author: dsanders
Date: Tue Mar  6 13:16:42 2018
New Revision: 326834

URL: http://llvm.org/viewvc/llvm-project?rev=326834&view=rev
Log:
PrintStatistics() and PrintStatisticsJSON() should take StatLock

These two functions iterate over the list of statistics but don't take the lock
that protects the iterators from being invalidated by
StatisticInfo::addStatistic().

So far, this hasn't been an issue since (in-tree at least) these functions are
called by the StatisticInfo destructor so addStatistic() shouldn't be called
anymore. However, we do expose them in the public API.

Note that this only protects against iterator invalidation and does not protect
against ordering issues caused by statistic updates that race with
PrintStatistics()/PrintStatisticsJSON().

Thanks to Roman Tereshin for spotting it


Modified:
    llvm/trunk/lib/Support/Statistic.cpp

Modified: llvm/trunk/lib/Support/Statistic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Statistic.cpp?rev=326834&r1=326833&r2=326834&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Statistic.cpp (original)
+++ llvm/trunk/lib/Support/Statistic.cpp Tue Mar  6 13:16:42 2018
@@ -166,6 +166,7 @@ void llvm::PrintStatistics(raw_ostream &
 }
 
 void llvm::PrintStatisticsJSON(raw_ostream &OS) {
+  sys::SmartScopedLock<true> Reader(*StatLock);
   StatisticInfo &Stats = *StatInfo;
 
   Stats.sort();
@@ -192,6 +193,7 @@ void llvm::PrintStatisticsJSON(raw_ostre
 
 void llvm::PrintStatistics() {
 #if LLVM_ENABLE_STATS
+  sys::SmartScopedLock<true> Reader(*StatLock);
   StatisticInfo &Stats = *StatInfo;
 
   // Statistics not enabled?




More information about the llvm-commits mailing list