]> granicus.if.org Git - llvm/commitdiff
Clean up the specific error message for a malformed Mach-O files with bad segment
authorKevin Enderby <enderby@apple.com>
Thu, 5 May 2016 17:43:35 +0000 (17:43 +0000)
committerKevin Enderby <enderby@apple.com>
Thu, 5 May 2016 17:43:35 +0000 (17:43 +0000)
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
lib/Object/Error.cpp
lib/Object/MachOObjectFile.cpp
test/Object/Inputs/macho-invalid-too-small-segment-load-command.1 [new file with mode: 0644]
test/Object/macho-invalid.test
tools/llvm-objdump/llvm-objdump.cpp

index c0902dd25657d4a54308875b5513b99f7f36a319..afaedb28f40d0987425a4b371cc4e45f8f00a5c5 100644 (file)
@@ -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) {
index 297418c95a97fb75f07d0e4dcdd092a7add3a356..1ec9d26e94130a1a8938c90ad15b98d8a1e23d44 100644 (file)
@@ -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.");
index ad29d0042e3c61b0c4e6b5fa61933ff18210043c..516607dea234c85b6ddaa29f96c78c6459e384d7 100644 (file)
@@ -234,22 +234,23 @@ static void parseHeader(const MachOObjectFile *Obj, T &Header,
 template <typename SegmentCmd>
 static Error parseSegmentLoadCommand(
     const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load,
-    SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment) {
+    SmallVectorImpl<const char *> &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<SegmentCmd>(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<uint32_t>::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<MachO::segment_command_64>(
-                   this, Load, Sections, HasPageZeroSegment)))
+                   this, Load, Sections, HasPageZeroSegment, I,
+                   "LC_SEGMENT_64")))
         return;
     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
       if ((Err = parseSegmentLoadCommand<MachO::segment_command>(
-                   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 (file)
index 0000000..24e5706
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-too-small-segment-load-command.1 differ
index 4ddea18fedfbc5458c34660d7682ec9265ec3db6..35fc5c6d1cb7fba8698fa18c24c3f5b5764b25de 100644 (file)
@@ -25,15 +25,19 @@ SMALL-LOADC-SIZE-1: truncated or malformed object (load command 1 with size less
 
 RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-segment-load-command 2>&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
index 1eb9f61c6ac90c6c24baf8d7f8154a10d7c89c7d..bd065986cb3ec23aa33045a392c1810f9006857a 100644 (file)
@@ -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<OwningBinary<Binary>> 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<Archive>(&Binary))