From 00e8fc511cb2456d37eb3a15ae3f79b4a00d938a Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Thu, 5 May 2016 17:43:35 +0000 Subject: [PATCH] Clean up the specific error message for a malformed Mach-O files with bad segment load commands. The existing test case in test/Object/macho-invalid.test for macho-invalid-too-small-segment-load-command has a cmdsize of 55, while being too small also it is not a multiple of 4. So when that check is added this test case will produce a different error. So I constructed a new test case that will trigger the intended error. I also changed the error message to be consistent with the other malformed Mach-O file error messages which prints the load command index. I also removed both object_error::macho_load_segment_too_small and object_error::macho_load_segment_too_many_sections from Object/Error.h as they are not needed and can just use object_error::parse_failed and let the error message string distinguish the specific error. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268652 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/Error.h | 2 -- lib/Object/Error.cpp | 4 ---- lib/Object/MachOObjectFile.cpp | 22 ++++++++++-------- ...o-invalid-too-small-segment-load-command.1 | Bin 0 -> 84 bytes test/Object/macho-invalid.test | 12 ++++++---- tools/llvm-objdump/llvm-objdump.cpp | 4 ++-- 6 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 test/Object/Inputs/macho-invalid-too-small-segment-load-command.1 diff --git a/include/llvm/Object/Error.h b/include/llvm/Object/Error.h index c0902dd2565..afaedb28f40 100644 --- a/include/llvm/Object/Error.h +++ b/include/llvm/Object/Error.h @@ -34,8 +34,6 @@ enum class object_error { string_table_non_null_end, invalid_section_index, bitcode_section_not_found, - macho_load_segment_too_many_sections, - macho_load_segment_too_small, }; inline std::error_code make_error_code(object_error e) { diff --git a/lib/Object/Error.cpp b/lib/Object/Error.cpp index 297418c95a9..1ec9d26e941 100644 --- a/lib/Object/Error.cpp +++ b/lib/Object/Error.cpp @@ -47,10 +47,6 @@ std::string _object_error_category::message(int EV) const { return "Invalid section index"; case object_error::bitcode_section_not_found: return "Bitcode section not found in object file"; - case object_error::macho_load_segment_too_many_sections: - return "Mach-O segment load command contains too many sections"; - case object_error::macho_load_segment_too_small: - return "Mach-O segment load command size is too small"; } llvm_unreachable("An enumerator of object_error does not have a message " "defined."); diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index ad29d0042e3..516607dea23 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -234,22 +234,23 @@ static void parseHeader(const MachOObjectFile *Obj, T &Header, template static Error parseSegmentLoadCommand( const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load, - SmallVectorImpl &Sections, bool &IsPageZeroSegment) { + SmallVectorImpl &Sections, bool &IsPageZeroSegment, + uint32_t LoadCommandIndex, const char *CmdName) { const unsigned SegmentLoadSize = sizeof(SegmentCmd); if (Load.C.cmdsize < SegmentLoadSize) - return malformedError(*Obj, - "Mach-O segment load command size is too small", - object_error::macho_load_segment_too_small); + return malformedError(*Obj, Twine("truncated or malformed object " + "(load command ") + Twine(LoadCommandIndex) + + Twine(" ") + CmdName + Twine(" cmdsize too small)")); if (auto SegOrErr = getStructOrErr(Obj, Load.Ptr)) { SegmentCmd S = SegOrErr.get(); const unsigned SectionSize = Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section); if (S.nsects > std::numeric_limits::max() / SectionSize || S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) - return malformedError(*Obj, - "Mach-O segment load command contains too many " - "sections", - object_error::macho_load_segment_too_many_sections); + return malformedError(*Obj, Twine("truncated or malformed object " + "(load command ") + Twine(LoadCommandIndex) + + Twine(" inconsistent cmdsize in ") + CmdName + + Twine(" for the number of sections)")); for (unsigned J = 0; J < S.nsects; ++J) { const char *Sec = getSectionPtr(Obj, Load, J); Sections.push_back(Sec); @@ -357,11 +358,12 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, UuidLoadCmd = Load.Ptr; } else if (Load.C.cmd == MachO::LC_SEGMENT_64) { if ((Err = parseSegmentLoadCommand( - this, Load, Sections, HasPageZeroSegment))) + this, Load, Sections, HasPageZeroSegment, I, + "LC_SEGMENT_64"))) return; } else if (Load.C.cmd == MachO::LC_SEGMENT) { if ((Err = parseSegmentLoadCommand( - this, Load, Sections, HasPageZeroSegment))) + this, Load, Sections, HasPageZeroSegment, I, "LC_SEGMENT"))) return; } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB || Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || diff --git a/test/Object/Inputs/macho-invalid-too-small-segment-load-command.1 b/test/Object/Inputs/macho-invalid-too-small-segment-load-command.1 new file mode 100644 index 0000000000000000000000000000000000000000..24e570665e7bd54607432b7c9a75857e7cddad4a GIT binary patch literal 84 ncmX^2>+L@VC}0HAAZ&reHvzKa&1 \ RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE %s -RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-too-small-segment-load-command 2>&1 \ +RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-segment-load-command.1 2>&1 \ RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE %s -SMALL-SEGLOADC-SIZE: Mach-O segment load command size is too small +SMALL-SEGLOADC-SIZE: truncated or malformed object (load command 0 LC_SEGMENT cmdsize too small) +RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-too-small-segment-load-command 2>&1 \ +RUN: | FileCheck -check-prefix SMALL-SEGLOADC-SIZE-64 %s +SMALL-SEGLOADC-SIZE-64: truncated or malformed object (load command 0 LC_SEGMENT_64 cmdsize too small) RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-no-size-for-sections 2>&1 \ RUN: | FileCheck -check-prefix TOO-MANY-SECTS %s +TOO-MANY-SECTS: truncated or malformed object (load command 0 inconsistent cmdsize in LC_SEGMENT for the number of sections) RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-no-size-for-sections 2>&1 \ -RUN: | FileCheck -check-prefix TOO-MANY-SECTS %s -TOO-MANY-SECTS: Mach-O segment load command contains too many sections +RUN: | FileCheck -check-prefix TOO-MANY-SECTS-64 %s +TOO-MANY-SECTS-64: truncated or malformed object (load command 0 inconsistent cmdsize in LC_SEGMENT_64 for the number of sections) RUN: not llvm-objdump -macho -t %p/Inputs/macho-invalid-bad-symbol-index 2>&1 \ RUN: | FileCheck -check-prefix BAD-SYMBOL %s diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 1eb9f61c6ac..bd065986cb3 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -277,7 +277,7 @@ LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, raw_string_ostream OS(Buf); logAllUnhandledErrors(std::move(E), OS, ""); OS.flush(); - errs() << ToolName << ": " << Buf; + errs() << ToolName << ": '" << File << "': " << Buf; exit(1); } @@ -1679,7 +1679,7 @@ static void DumpInput(StringRef file) { // Attempt to open the binary. Expected> BinaryOrErr = createBinary(file); if (!BinaryOrErr) - report_error(file, errorToErrorCode(BinaryOrErr.takeError())); + report_error(file, BinaryOrErr.takeError()); Binary &Binary = *BinaryOrErr.get().getBinary(); if (Archive *a = dyn_cast(&Binary)) -- 2.50.1