[llvm] r349600 - Fix use-after-free with profile remapping.

Richard Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 18 19:24:03 PST 2018


Author: rsmith
Date: Tue Dec 18 19:24:03 2018
New Revision: 349600

URL: http://llvm.org/viewvc/llvm-project?rev=349600&view=rev
Log:
Fix use-after-free with profile remapping.

We need to keep the underlying profile reader alive as long as the
profile data, because the profile data may contain StringRefs referring
to strings in the reader's name table.

Modified:
    llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
    llvm/trunk/unittests/ProfileData/SampleProfTest.cpp

Modified: llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfReader.h?rev=349600&r1=349599&r2=349600&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/SampleProfReader.h (original)
+++ llvm/trunk/include/llvm/ProfileData/SampleProfReader.h Tue Dec 18 19:24:03 2018
@@ -548,6 +548,9 @@ public:
       : SampleProfileReader(std::move(B), C, Underlying->getFormat()) {
     Profiles = std::move(Underlying->getProfiles());
     Summary = takeSummary(*Underlying);
+    // Keep the underlying reader alive; the profile data may contain
+    // StringRefs referencing names in its name table.
+    UnderlyingReader = std::move(Underlying);
   }
 
   /// Create a remapped sample profile from the given remapping file and
@@ -569,6 +572,7 @@ public:
 private:
   SymbolRemappingReader Remappings;
   DenseMap<SymbolRemappingReader::Key, FunctionSamples*> SampleMap;
+  std::unique_ptr<SampleProfileReader> UnderlyingReader;
 };
 
 } // end namespace sampleprof

Modified: llvm/trunk/unittests/ProfileData/SampleProfTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/SampleProfTest.cpp?rev=349600&r1=349599&r2=349600&view=diff
==============================================================================
--- llvm/trunk/unittests/ProfileData/SampleProfTest.cpp (original)
+++ llvm/trunk/unittests/ProfileData/SampleProfTest.cpp Tue Dec 18 19:24:03 2018
@@ -128,11 +128,15 @@ struct SampleProfTest : ::testing::Test
 
     FunctionSamples *ReadFooSamples = Reader->getSamplesFor(FooName);
     ASSERT_TRUE(ReadFooSamples != nullptr);
+    if (Format != SampleProfileFormat::SPF_Compact_Binary)
+      ASSERT_EQ("_Z3fooi", ReadFooSamples->getName());
     ASSERT_EQ(7711u, ReadFooSamples->getTotalSamples());
     ASSERT_EQ(610u, ReadFooSamples->getHeadSamples());
 
     FunctionSamples *ReadBarSamples = Reader->getSamplesFor(BarName);
     ASSERT_TRUE(ReadBarSamples != nullptr);
+    if (Format != SampleProfileFormat::SPF_Compact_Binary)
+      ASSERT_EQ("_Z3bari", ReadBarSamples->getName());
     ASSERT_EQ(20301u, ReadBarSamples->getTotalSamples());
     ASSERT_EQ(1437u, ReadBarSamples->getHeadSamples());
     ErrorOr<SampleRecord::CallTargetMap> CTMap =




More information about the llvm-commits mailing list