summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2015-09-24 18:59:26 +0000
committerRui Ueyama <ruiu@google.com>2015-09-24 18:59:26 +0000
commit1392d8b5fe2afe02988d818e052d4b84f21cc004 (patch)
tree160cd8742fcf1ad50ea593b36d3de2e8c141b3fa
parentd5b5ab76384c9c0e8ff0e3c5575b2877317bf718 (diff)
downloadbcm5719-llvm-1392d8b5fe2afe02988d818e052d4b84f21cc004.tar.gz
bcm5719-llvm-1392d8b5fe2afe02988d818e052d4b84f21cc004.zip
Fix memory leak in FileArchive::find().
Patch from George Rimar! llvm-svn: 248525
-rw-r--r--lld/lib/ReaderWriter/ELF/OutputELFWriter.cpp12
-rw-r--r--lld/lib/ReaderWriter/FileArchive.cpp9
2 files changed, 17 insertions, 4 deletions
diff --git a/lld/lib/ReaderWriter/ELF/OutputELFWriter.cpp b/lld/lib/ReaderWriter/ELF/OutputELFWriter.cpp
index 5d1addd304b..4f8b0eac655 100644
--- a/lld/lib/ReaderWriter/ELF/OutputELFWriter.cpp
+++ b/lld/lib/ReaderWriter/ELF/OutputELFWriter.cpp
@@ -45,8 +45,15 @@ public:
assert(!_file->hasAtoms() && "The file shouldn't have atoms yet");
_resolver(sym, *_file);
- // If atoms were added - release the file to the caller.
- return _file->hasAtoms() ? _file.release() : nullptr;
+
+ if (!_file->hasAtoms())
+ return nullptr;
+
+ // If atoms were added - return the file but also store it for later
+ // destruction.
+ File *result = _file.get();
+ _returnedFiles.push_back(std::move(_file));
+ return result;
}
private:
@@ -57,6 +64,7 @@ private:
// reversed destruction order.
llvm::BumpPtrAllocator _alloc;
unique_bump_ptr<SymbolFile<ELFT>> _file;
+ std::vector<unique_bump_ptr<SymbolFile<ELFT>>> _returnedFiles;
};
} // end anon namespace
diff --git a/lld/lib/ReaderWriter/FileArchive.cpp b/lld/lib/ReaderWriter/FileArchive.cpp
index 3250a5f4230..4d0d55218ed 100644
--- a/lld/lib/ReaderWriter/FileArchive.cpp
+++ b/lld/lib/ReaderWriter/FileArchive.cpp
@@ -11,6 +11,7 @@
#include "lld/Core/LLVM.h"
#include "lld/Core/LinkingContext.h"
#include "lld/Core/Parallel.h"
+#include "lld/Driver/Driver.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Object/Archive.h"
@@ -76,8 +77,11 @@ public:
if (instantiateMember(ci, result))
return nullptr;
- // give up the pointer so that this object no longer manages it
- return result.release();
+ File *file = result.get();
+ _filesReturned.push_back(std::move(result));
+
+ // Give up the file pointer. It was stored and will be destroyed with destruction of FileArchive
+ return file;
}
// Instantiate a member file containing a given symbol name.
@@ -259,6 +263,7 @@ private:
std::vector<std::unique_ptr<MemoryBuffer>> _memberBuffers;
std::map<const char *, std::unique_ptr<Future<File *>>> _preloaded;
std::mutex _mutex;
+ FileVector _filesReturned;
};
class ArchiveReader : public Reader {
OpenPOWER on IntegriCloud