diff options
| author | Lang Hames <lhames@gmail.com> | 2016-07-29 20:04:18 +0000 |
|---|---|---|
| committer | Lang Hames <lhames@gmail.com> | 2016-07-29 20:04:18 +0000 |
| commit | 70c80b336b02a8e0f4d3f10a80787df111bee2b1 (patch) | |
| tree | ae854a24a3d23bebcee2b5daacb921aac019d94a /lld/lib/ReaderWriter | |
| parent | a6b68bf0b91cde70e085b22bc8840df2b8db7470 (diff) | |
| download | bcm5719-llvm-70c80b336b02a8e0f4d3f10a80787df111bee2b1.tar.gz bcm5719-llvm-70c80b336b02a8e0f4d3f10a80787df111bee2b1.zip | |
[lld][MachO] Replace some std::string with char* buffers to eliminate mem leaks.
The MachO debug support code (committed in r276935) occasionally needs to
allocate string copies, and was doing so by creating std::strings on a
BumpPtrAllocator. The strings were untracked, so the destructors weren't being
run and we were leaking the memory when the allocator was thrown away. Since
it's easier than tracking the strings, this patch switches the copies to char
buffers allocated directly in the bump-ptr allocator.
llvm-svn: 277208
Diffstat (limited to 'lld/lib/ReaderWriter')
| -rw-r--r-- | lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp | 20 | ||||
| -rw-r--r-- | lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp | 8 |
2 files changed, 15 insertions, 13 deletions
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp index 2d95ab7ec9d..73873258d12 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp @@ -870,10 +870,12 @@ llvm::Error Util::synthesizeDebugNotes(NormalizedFile &file) { // If newDirPath doesn't end with a '/' we need to add one: if (newDirPath.back() != '/') { - std::string *p = file.ownedAllocations.Allocate<std::string>(); - new (p) std::string(); - *p = (newDirPath + "/").str(); - newDirPath = *p; + char *p = + file.ownedAllocations.Allocate<char>(newDirPath.size() + 2); + memcpy(p, newDirPath.data(), newDirPath.size()); + p[newDirPath.size()] = '/'; + p[newDirPath.size() + 1] = '\0'; + newDirPath = p; } // New translation unit, emit start SOs: @@ -881,24 +883,24 @@ llvm::Error Util::synthesizeDebugNotes(NormalizedFile &file) { _stabs.push_back(mach_o::Stab(nullptr, N_SO, 0, 0, 0, newFileName)); // Synthesize OSO for start of file. - std::string *fullPath = file.ownedAllocations.Allocate<std::string>(); - new (fullPath) std::string(); + char *fullPath = nullptr; { SmallString<1024> pathBuf(atomFile.path()); if (auto EC = llvm::sys::fs::make_absolute(pathBuf)) return llvm::errorCodeToError(EC); - *fullPath = pathBuf.str(); + fullPath = file.ownedAllocations.Allocate<char>(pathBuf.size() + 1); + memcpy(fullPath, pathBuf.c_str(), pathBuf.size() + 1); } // Get mod time. uint32_t modTime = 0; llvm::sys::fs::file_status stat; - if (!llvm::sys::fs::status(*fullPath, stat)) + if (!llvm::sys::fs::status(fullPath, stat)) if (llvm::sys::fs::exists(stat)) modTime = stat.getLastModificationTime().toEpochTime(); _stabs.push_back(mach_o::Stab(nullptr, N_OSO, _ctx.getCPUSubType(), 1, - modTime, *fullPath)); + modTime, fullPath)); // <rdar://problem/6337329> linker should put cpusubtype in n_sect field // of nlist entry for N_OSO debug note entries. wroteStartSO = true; diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp index beead3b398a..b0ef1c9e691 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp @@ -702,10 +702,10 @@ static const Atom* findDefinedAtomByName(MachOFile &file, Twine name) { } static StringRef copyDebugString(StringRef str, BumpPtrAllocator &alloc) { - std::string *strCopy = alloc.Allocate<std::string>(); - new (strCopy) std::string(); - *strCopy = str; - return *strCopy; + char *strCopy = alloc.Allocate<char>(str.size() + 1); + memcpy(strCopy, str.data(), str.size()); + strCopy[str.size()] = '\0'; + return strCopy; } llvm::Error parseStabs(MachOFile &file, |

