]> granicus.if.org Git - llvm/commitdiff
Fix use-after-free with profile remapping.
authorRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 19 Dec 2018 03:24:03 +0000 (03:24 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 19 Dec 2018 03:24:03 +0000 (03:24 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@349600 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ProfileData/SampleProfReader.h
unittests/ProfileData/SampleProfTest.cpp

index 3c477cc347147ec6a62090604c2a9e37098e769f..5cc729e42cc88f218dd37496ea5f9f4cda1e7401 100644 (file)
@@ -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
index 67e6e9fc95b958634ae4e04aa17c104cbddae5e3..a31eccefb4d65cc0bad2b26a03fa744de7ccb75c 100644 (file)
@@ -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 =