[llvm] 5617fb1 - [MLGO][NFC] Use std::map instead of DenseMap to avoid use after free

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 4 16:07:44 PDT 2022


Author: Mircea Trofin
Date: 2022-11-04T16:07:24-07:00
New Revision: 5617fb1411f765667c016b5b75daa9d1110c36af

URL: https://github.com/llvm/llvm-project/commit/5617fb1411f765667c016b5b75daa9d1110c36af
DIFF: https://github.com/llvm/llvm-project/commit/5617fb1411f765667c016b5b75daa9d1110c36af.diff

LOG: [MLGO][NFC] Use std::map instead of DenseMap to avoid use after free

In `MLInlineAdvisor::getAdviceImpl`, we call `getCachedFPI` twice, once
for the caller, once for the callee, so the second may invalidate the
reference obtained by the first because the underlying implementation of
the cache is a `DenseMap`. `std::map` doesn't have that problem.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/MLInlineAdvisor.h
    llvm/lib/Analysis/MLInlineAdvisor.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/MLInlineAdvisor.h b/llvm/include/llvm/Analysis/MLInlineAdvisor.h
index 00e8d7d7dd4de..3db948d365c77 100644
--- a/llvm/include/llvm/Analysis/MLInlineAdvisor.h
+++ b/llvm/include/llvm/Analysis/MLInlineAdvisor.h
@@ -69,7 +69,7 @@ class MLInlineAdvisor : public InlineAdvisor {
   getSkipAdviceIfUnreachableCallsite(CallBase &CB);
   void print(raw_ostream &OS) const override;
 
-  mutable DenseMap<const Function *, FunctionPropertiesInfo> FPICache;
+  mutable std::map<const Function *, FunctionPropertiesInfo> FPICache;
 
   LazyCallGraph &CG;
 

diff  --git a/llvm/lib/Analysis/MLInlineAdvisor.cpp b/llvm/lib/Analysis/MLInlineAdvisor.cpp
index f55de71ea98ae..a20c05243b773 100644
--- a/llvm/lib/Analysis/MLInlineAdvisor.cpp
+++ b/llvm/lib/Analysis/MLInlineAdvisor.cpp
@@ -415,8 +415,8 @@ void MLInlineAdvisor::print(raw_ostream &OS) const {
      << " EdgesOfLastSeenNodes: " << EdgesOfLastSeenNodes << "\n";
   OS << "[MLInlineAdvisor] FPI:\n";
   for (auto I : FPICache) {
-    OS << I.getFirst()->getName() << ":\n";
-    I.getSecond().print(OS);
+    OS << I.first->getName() << ":\n";
+    I.second.print(OS);
     OS << "\n";
   }
   OS << "\n";


        


More information about the llvm-commits mailing list