From 596e474101ea9a8ecac9d8ee090d31469dbcc61d Mon Sep 17 00:00:00 2001 From: Jim Grosbach Date: Thu, 29 Nov 2012 19:14:11 +0000 Subject: Fix a memory leak in MachOObjectFile. MachOObjectFile owns a MachOObj, but never frees it. Both MachOObjectFile and MachOObj want to own the MemoryBuffer, though, so we have to be careful and give them each one of their own. Thanks to Greg Clayton, Eric Christopher and Michael Spencer for helping figure out what's going wrong here. rdar://12561773 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168923 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/MachOObjectFile.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'lib/Object/MachOObjectFile.cpp') diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 45aeaac6b8..bcea3bb5e9 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -50,7 +50,14 @@ ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err); if (!MachOObj) return NULL; - return new MachOObjectFile(Buffer, MachOObj, ec); + // MachOObject takes ownership of the Buffer we passed to it, and + // MachOObjectFile does, too, so we need to make sure they don't get the + // same object. A MemoryBuffer is cheap (it's just a reference to memory, + // not a copy of the memory itself), so just make a new copy here for + // the MachOObjectFile. + MemoryBuffer *NewBuffer = + MemoryBuffer::getMemBuffer(Buffer->getBuffer(), "", false); + return new MachOObjectFile(NewBuffer, MachOObj, ec); } /*===-- Symbols -----------------------------------------------------------===*/ @@ -474,7 +481,7 @@ error_code MachOObjectFile::getSectionName(DataRefImpl DRI, StringRef &Result) const { // FIXME: thread safety. static char result[34]; - if (is64BitLoadCommand(MachOObj, DRI)) { + if (is64BitLoadCommand(MachOObj.get(), DRI)) { InMemoryStruct SLC; LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); MachOObj->ReadSegment64LoadCommand(LCI, SLC); @@ -501,7 +508,7 @@ error_code MachOObjectFile::getSectionName(DataRefImpl DRI, error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, uint64_t &Result) const { - if (is64BitLoadCommand(MachOObj, DRI)) { + if (is64BitLoadCommand(MachOObj.get(), DRI)) { InMemoryStruct Sect; getSection64(DRI, Sect); Result = Sect->Address; @@ -515,7 +522,7 @@ error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, uint64_t &Result) const { - if (is64BitLoadCommand(MachOObj, DRI)) { + if (is64BitLoadCommand(MachOObj.get(), DRI)) { InMemoryStruct Sect; getSection64(DRI, Sect); Result = Sect->Size; @@ -529,7 +536,7 @@ error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, StringRef &Result) const { - if (is64BitLoadCommand(MachOObj, DRI)) { + if (is64BitLoadCommand(MachOObj.get(), DRI)) { InMemoryStruct Sect; getSection64(DRI, Sect); Result = MachOObj->getData(Sect->Offset, Sect->Size); @@ -543,7 +550,7 @@ error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI, uint64_t &Result) const { - if (is64BitLoadCommand(MachOObj, DRI)) { + if (is64BitLoadCommand(MachOObj.get(), DRI)) { InMemoryStruct Sect; getSection64(DRI, Sect); Result = uint64_t(1) << Sect->Align; @@ -557,7 +564,7 @@ error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI, error_code MachOObjectFile::isSectionText(DataRefImpl DRI, bool &Result) const { - if (is64BitLoadCommand(MachOObj, DRI)) { + if (is64BitLoadCommand(MachOObj.get(), DRI)) { InMemoryStruct Sect; getSection64(DRI, Sect); Result = !strcmp(Sect->Name, "__text"); @@ -664,7 +671,7 @@ relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { } relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { uint32_t last_reloc; - if (is64BitLoadCommand(MachOObj, Sec)) { + if (is64BitLoadCommand(MachOObj.get(), Sec)) { InMemoryStruct Sect; getSection64(Sec, Sect); last_reloc = Sect->NumRelocationTableEntries; -- cgit v1.2.3-18-g5258 From f56c3e28b94545d7d7cf34d39d7ed87fae29d9be Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 29 Nov 2012 20:08:03 +0000 Subject: Object: Pass the buffer name through when making a copy. Should bring the buildbots back to life. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168935 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/MachOObjectFile.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/Object/MachOObjectFile.cpp') diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index bcea3bb5e9..85116cb273 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -56,7 +56,8 @@ ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { // not a copy of the memory itself), so just make a new copy here for // the MachOObjectFile. MemoryBuffer *NewBuffer = - MemoryBuffer::getMemBuffer(Buffer->getBuffer(), "", false); + MemoryBuffer::getMemBuffer(Buffer->getBuffer(), + Buffer->getBufferIdentifier(), false); return new MachOObjectFile(NewBuffer, MachOObj, ec); } -- cgit v1.2.3-18-g5258 From d04a8d4b33ff316ca4cf961e06c9e312eff8e64f Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Mon, 3 Dec 2012 16:50:05 +0000 Subject: Use the new script to sort the includes of every file under lib. Sooooo many of these had incorrect or strange main module includes. I have manually inspected all of these, and fixed the main module include to be the nearest plausible thing I could find. If you own or care about any of these source files, I encourage you to take some time and check that these edits were sensible. I can't have broken anything (I strictly added headers, and reordered them, never removed), but they may not be the headers you'd really like to identify as containing the API being implemented. Many forward declarations and missing includes were added to a header files to allow them to parse cleanly when included first. The main module rule does in fact have its merits. =] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169131 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/MachOObjectFile.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/Object/MachOObjectFile.cpp') diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 85116cb273..da7615714e 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -12,12 +12,11 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/Triple.h" #include "llvm/Object/MachO.h" +#include "llvm/ADT/Triple.h" #include "llvm/Object/MachOFormat.h" #include "llvm/Support/Format.h" #include "llvm/Support/MemoryBuffer.h" - #include #include #include -- cgit v1.2.3-18-g5258