[lld] r264204 - Fix more cases of UB from allocating 0 sized data. NFC.
Pete Cooper via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 23 15:19:17 PDT 2016
Author: pete
Date: Wed Mar 23 17:19:16 2016
New Revision: 264204
URL: http://llvm.org/viewvc/llvm-project?rev=264204&view=rev
Log:
Fix more cases of UB from allocating 0 sized data. NFC.
The size of a section can be zero, even when it contains atoms, so
long as all of the atoms are also size 0. In this case we were
allocating space for a 0 sized buffer.
Changed this to only allocate when we need the space, but also cleaned
up all the code to use MutableArrayRef instead of uint8_t* so its much much
safer as we get bounds checking on all of our section creation logic.
Modified:
lld/trunk/lib/ReaderWriter/MachO/ArchHandler.h
lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm.cpp
lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp
lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86.cpp
lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
Modified: lld/trunk/lib/ReaderWriter/MachO/ArchHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ArchHandler.h?rev=264204&r1=264203&r2=264204&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ArchHandler.h (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ArchHandler.h Wed Mar 23 17:19:16 2016
@@ -178,7 +178,7 @@ public:
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) = 0;
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) = 0;
/// Used in -r mode to convert a Reference to a mach-o relocation.
virtual void appendSectionRelocations(const DefinedAtom &atom,
Modified: lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm.cpp?rev=264204&r1=264203&r2=264204&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm.cpp Wed Mar 23 17:19:16 2016
@@ -101,7 +101,7 @@ public:
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) override;
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) override;
void appendSectionRelocations(const DefinedAtom &atom,
uint64_t atomSectionOffset,
@@ -1019,9 +1019,10 @@ void ArchHandler_arm::generateAtomConten
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) {
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) {
// Copy raw bytes.
- memcpy(atomContentBuffer, atom.rawContent().data(), atom.size());
+ std::copy(atom.rawContent().begin(), atom.rawContent().end(),
+ atomContentBuffer.begin());
// Apply fix-ups.
bool thumbMode = false;
for (const Reference *ref : atom) {
Modified: lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp?rev=264204&r1=264203&r2=264204&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp Wed Mar 23 17:19:16 2016
@@ -164,7 +164,7 @@ public:
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) override;
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) override;
void appendSectionRelocations(const DefinedAtom &atom,
uint64_t atomSectionOffset,
@@ -537,9 +537,10 @@ std::error_code ArchHandler_arm64::getPa
void ArchHandler_arm64::generateAtomContent(
const DefinedAtom &atom, bool relocatable, FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress, uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) {
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) {
// Copy raw bytes.
- memcpy(atomContentBuffer, atom.rawContent().data(), atom.size());
+ std::copy(atom.rawContent().begin(), atom.rawContent().end(),
+ atomContentBuffer.begin());
// Apply fix-ups.
#ifndef NDEBUG
if (atom.begin() != atom.end()) {
Modified: lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86.cpp?rev=264204&r1=264203&r2=264204&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86.cpp Wed Mar 23 17:19:16 2016
@@ -107,7 +107,7 @@ public:
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) override;
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) override;
void appendSectionRelocations(const DefinedAtom &atom,
uint64_t atomSectionOffset,
@@ -419,9 +419,10 @@ void ArchHandler_x86::generateAtomConten
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) {
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) {
// Copy raw bytes.
- memcpy(atomContentBuffer, atom.rawContent().data(), atom.size());
+ std::copy(atom.rawContent().begin(), atom.rawContent().end(),
+ atomContentBuffer.begin());
// Apply fix-ups.
for (const Reference *ref : atom) {
uint32_t offset = ref->offsetInAtom();
Modified: lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp?rev=264204&r1=264203&r2=264204&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp Wed Mar 23 17:19:16 2016
@@ -167,7 +167,7 @@ public:
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBase,
- uint8_t *atomContentBuffer) override;
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) override;
void appendSectionRelocations(const DefinedAtom &atom,
uint64_t atomSectionOffset,
@@ -508,9 +508,10 @@ ArchHandler_x86_64::getPairReferenceInfo
void ArchHandler_x86_64::generateAtomContent(
const DefinedAtom &atom, bool relocatable, FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress, uint64_t imageBaseAddress,
- uint8_t *atomContentBuffer) {
+ llvm::MutableArrayRef<uint8_t> atomContentBuffer) {
// Copy raw bytes.
- memcpy(atomContentBuffer, atom.rawContent().data(), atom.size());
+ std::copy(atom.rawContent().begin(), atom.rawContent().end(),
+ atomContentBuffer.begin());
// Apply fix-ups.
for (const Reference *ref : atom) {
uint32_t offset = ref->offsetInAtom();
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=264204&r1=264203&r2=264204&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Wed Mar 23 17:19:16 2016
@@ -669,16 +669,20 @@ void Util::copySectionContent(Normalized
continue;
}
// Copy content from atoms to content buffer for section.
- uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
- normSect->content = llvm::makeArrayRef(sectionContent, si->size);
+ llvm::MutableArrayRef<uint8_t> sectionContent;
+ if (si->size) {
+ uint8_t *sectContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
+ sectionContent = llvm::MutableArrayRef<uint8_t>(sectContent, si->size);
+ normSect->content = sectionContent;
+ }
for (AtomInfo &ai : si->atomsAndOffsets) {
if (!ai.atom->size()) {
assert(ai.atom->begin() == ai.atom->end() &&
"Cannot have references without content");
continue;
}
- uint8_t *atomContent = reinterpret_cast<uint8_t*>
- (§ionContent[ai.offsetInSection]);
+ auto atomContent = sectionContent.slice(ai.offsetInSection,
+ ai.atom->size());
_archHandler.generateAtomContent(*ai.atom, r, addrForAtom,
sectionAddrForAtom, _ctx.baseAddress(),
atomContent);
More information about the llvm-commits
mailing list