[llvm] r285026 - nother additional error check for an invalid Mach-O file
Kevin Enderby via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 24 14:15:12 PDT 2016
Author: enderby
Date: Mon Oct 24 16:15:11 2016
New Revision: 285026
URL: http://llvm.org/viewvc/llvm-project?rev=285026&view=rev
Log:
nother additional error check for an invalid Mach-O file
when contained in a Mach-O universal file and the
cputypes in both headers don’t match.
Added:
llvm/trunk/test/Object/Inputs/macho-invalid-fat_cputype (with props)
Modified:
llvm/trunk/include/llvm/Object/MachO.h
llvm/trunk/include/llvm/Object/ObjectFile.h
llvm/trunk/lib/Object/MachOObjectFile.cpp
llvm/trunk/lib/Object/MachOUniversal.cpp
llvm/trunk/test/Object/macho-invalid.test
Modified: llvm/trunk/include/llvm/Object/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachO.h?rev=285026&r1=285025&r2=285026&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (original)
+++ llvm/trunk/include/llvm/Object/MachO.h Mon Oct 24 16:15:11 2016
@@ -194,7 +194,8 @@ public:
typedef LoadCommandList::const_iterator load_command_iterator;
static Expected<std::unique_ptr<MachOObjectFile>>
- create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits);
+ create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
+ uint32_t UniversalCputype = 0, uint32_t UniversalIndex = 0);
void moveSymbolNext(DataRefImpl &Symb) const override;
@@ -443,7 +444,8 @@ public:
private:
MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
- Error &Err);
+ Error &Err, uint32_t UniversalCputype = 0,
+ uint32_t UniversalIndex = 0);
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=285026&r1=285025&r2=285026&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Mon Oct 24 16:15:11 2016
@@ -300,7 +300,9 @@ public:
createELFObjectFile(MemoryBufferRef Object);
static Expected<std::unique_ptr<MachOObjectFile>>
- createMachOObjectFile(MemoryBufferRef Object);
+ createMachOObjectFile(MemoryBufferRef Object,
+ uint32_t UniversalCputype = 0,
+ uint32_t UniversalIndex = 0);
};
Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=285026&r1=285025&r2=285026&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Mon Oct 24 16:15:11 2016
@@ -914,18 +914,22 @@ static Error checkTwoLevelHintsCommand(c
Expected<std::unique_ptr<MachOObjectFile>>
MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian,
- bool Is64Bits) {
+ bool Is64Bits, uint32_t UniversalCputype,
+ uint32_t UniversalIndex) {
Error Err;
std::unique_ptr<MachOObjectFile> Obj(
new MachOObjectFile(std::move(Object), IsLittleEndian,
- Is64Bits, Err));
+ Is64Bits, Err, UniversalCputype,
+ UniversalIndex));
if (Err)
return std::move(Err);
return std::move(Obj);
}
MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
- bool Is64bits, Error &Err)
+ bool Is64bits, Error &Err,
+ uint32_t UniversalCputype,
+ uint32_t UniversalIndex)
: ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr),
@@ -933,12 +937,15 @@ MachOObjectFile::MachOObjectFile(MemoryB
HasPageZeroSegment(false) {
ErrorAsOutParameter ErrAsOutParam(&Err);
uint64_t SizeOfHeaders;
+ uint32_t cputype;
if (is64Bit()) {
parseHeader(this, Header64, Err);
SizeOfHeaders = sizeof(MachO::mach_header_64);
+ cputype = Header64.cputype;
} else {
parseHeader(this, Header, Err);
SizeOfHeaders = sizeof(MachO::mach_header);
+ cputype = Header.cputype;
}
if (Err)
return;
@@ -947,6 +954,12 @@ MachOObjectFile::MachOObjectFile(MemoryB
Err = malformedError("load commands extend past the end of the file");
return;
}
+ if (UniversalCputype != 0 && cputype != UniversalCputype) {
+ Err = malformedError("universal header architecture: " +
+ Twine(UniversalIndex) + "'s cputype does not match "
+ "object file's mach header");
+ return;
+ }
uint32_t LoadCommandCount = getHeader().ncmds;
LoadCommandInfo Load;
@@ -3281,16 +3294,22 @@ bool MachOObjectFile::isRelocatableObjec
}
Expected<std::unique_ptr<MachOObjectFile>>
-ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
+ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer,
+ uint32_t UniversalCputype,
+ uint32_t UniversalIndex) {
StringRef Magic = Buffer.getBuffer().slice(0, 4);
if (Magic == "\xFE\xED\xFA\xCE")
- return MachOObjectFile::create(Buffer, false, false);
+ return MachOObjectFile::create(Buffer, false, false,
+ UniversalCputype, UniversalIndex);
if (Magic == "\xCE\xFA\xED\xFE")
- return MachOObjectFile::create(Buffer, true, false);
+ return MachOObjectFile::create(Buffer, true, false,
+ UniversalCputype, UniversalIndex);
if (Magic == "\xFE\xED\xFA\xCF")
- return MachOObjectFile::create(Buffer, false, true);
+ return MachOObjectFile::create(Buffer, false, true,
+ UniversalCputype, UniversalIndex);
if (Magic == "\xCF\xFA\xED\xFE")
- return MachOObjectFile::create(Buffer, true, true);
+ return MachOObjectFile::create(Buffer, true, true,
+ UniversalCputype, UniversalIndex);
return make_error<GenericBinaryError>("Unrecognized MachO magic number",
object_error::invalid_file_type);
}
Modified: llvm/trunk/lib/Object/MachOUniversal.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOUniversal.cpp?rev=285026&r1=285025&r2=285026&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOUniversal.cpp (original)
+++ llvm/trunk/lib/Object/MachOUniversal.cpp Mon Oct 24 16:15:11 2016
@@ -73,13 +73,17 @@ MachOUniversalBinary::ObjectForArch::get
StringRef ParentData = Parent->getData();
StringRef ObjectData;
- if (Parent->getMagic() == MachO::FAT_MAGIC)
+ uint32_t cputype;
+ if (Parent->getMagic() == MachO::FAT_MAGIC) {
ObjectData = ParentData.substr(Header.offset, Header.size);
- else // Parent->getMagic() == MachO::FAT_MAGIC_64
+ cputype = Header.cputype;
+ } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
ObjectData = ParentData.substr(Header64.offset, Header64.size);
+ cputype = Header64.cputype;
+ }
StringRef ObjectName = Parent->getFileName();
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
- return ObjectFile::createMachOObjectFile(ObjBuffer);
+ return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
}
Expected<std::unique_ptr<Archive>>
Added: llvm/trunk/test/Object/Inputs/macho-invalid-fat_cputype
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/macho-invalid-fat_cputype?rev=285026&view=auto
==============================================================================
Binary file - no diff available.
Propchange: llvm/trunk/test/Object/Inputs/macho-invalid-fat_cputype
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Modified: llvm/trunk/test/Object/macho-invalid.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/macho-invalid.test?rev=285026&r1=285025&r2=285026&view=diff
==============================================================================
--- llvm/trunk/test/Object/macho-invalid.test (original)
+++ llvm/trunk/test/Object/macho-invalid.test Mon Oct 24 16:15:11 2016
@@ -406,3 +406,6 @@ INVALID-TWOLEVELHINTS-OFFSET: macho-inva
RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-twolevelhints-offset-nhints 2>&1 | FileCheck -check-prefix INVALID-TWOLEVELHINTS-OFFSET-HNINTS %s
INVALID-TWOLEVELHINTS-OFFSET-HNINTS: macho-invalid-twolevelhints-offset-nhints': truncated or malformed object (offset field plus nhints times sizeof(struct twolevel_hint) field of LC_TWOLEVEL_HINTS command 0 extends past the end of the file)
+
+RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-fat_cputype 2>&1 | FileCheck -check-prefix INVALID-FAT-CPUTYPE %s
+INVALID-FAT-CPUTYPE: macho-invalid-fat_cputype truncated or malformed object (universal header architecture: 0's cputype does not match object file's mach header)
More information about the llvm-commits
mailing list