Fix FileArchive member MemoryBuffer early destruction
authorNick Kledzik <kledzik@apple.com>
Fri, 7 Nov 2014 20:52:38 +0000 (20:52 +0000)
committerNick Kledzik <kledzik@apple.com>
Fri, 7 Nov 2014 20:52:38 +0000 (20:52 +0000)
When FileArchive loads a member, it instantiates a temporary MemoryBuffer
which points to the member range of the archive file.  The problem is that the
object file parsers call getBufferIndentifer() on that temporary MemoryBuffer
and store that StringRef as the _path data member for that lld::File.  When
FileArchive::instantiateMember() goes out of scope the MemoryBuffer is deleted
and the File::._path becomes a dangling reference.

The fix adds a vector<> to FileArchive to own the instantiated MemoryBuffers.
In addition it fixes member's path to be the standard format
(e.g. "/path/libfoo.a(foo.o)") instead of just the leaf name.

llvm-svn: 221544

lld/lib/ReaderWriter/FileArchive.cpp

index 15c4cc6..a76399e 100644 (file)
@@ -133,18 +133,27 @@ private:
     if (std::error_code ec = mbOrErr.getError())
       return ec;
     llvm::MemoryBufferRef mb = mbOrErr.get();
+    std::string memberPath = (_archive->getFileName() + "("
+                           + mb.getBufferIdentifier() + ")").str();
+
     if (_logLoading)
-      llvm::outs() << _archive->getFileName() << "(" << mb.getBufferIdentifier()
-                   << ")"
-                   << "\n";
+      llvm::errs() << memberPath << "\n";
 
-    std::unique_ptr<MemoryBuffer> buf(MemoryBuffer::getMemBuffer(
-        mb.getBuffer(), mb.getBufferIdentifier(), false));
+    std::unique_ptr<MemoryBuffer> memberMB(MemoryBuffer::getMemBuffer(
+        mb.getBuffer(), memberPath, false));
 
     std::vector<std::unique_ptr<File>> files;
-    _registry.parseFile(buf, files);
+    _registry.parseFile(memberMB, files);
     assert(files.size() == 1);
     result = std::move(files[0]);
+
+    // Note: The object file parsers use getBufferIdentifier() from memberMB
+    // for the file path. And MemoryBuffer makes its own copy of the path.
+    // That means when if memberMB is destroyed, the lld:File objects will
+    // have a dangling reference for their path.  To fix that, all the
+    // MemoryBuffers for the archive members are owned by _memberBuffers.
+    _memberBuffers.push_back(std::move(memberMB));
+
     return std::error_code();
   }
 
@@ -205,6 +214,7 @@ private:
   atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
   bool _isWholeArchive;
   bool _logLoading;
+  mutable std::vector<std::unique_ptr<MemoryBuffer>> _memberBuffers;
 };
 
 class ArchiveReader : public Reader {