[llvm] r268463 - Produce another specific error message for a malformed Mach-O file when a load

Kevin Enderby via llvm-commits llvm-commits at lists.llvm.org
Tue May 3 16:13:50 PDT 2016


Author: enderby
Date: Tue May  3 18:13:50 2016
New Revision: 268463

URL: http://llvm.org/viewvc/llvm-project?rev=268463&view=rev
Log:
Produce another specific error message for a malformed Mach-O file when a load
command has a size less than 8 bytes.

I think the existing test case in test/Object/macho-invalid.test for
macho64-invalid-too-small-load-command was trying to test for this but that
test case triggered a different error given how it was constructed.  So I
constructed a new test case that would trigger this specific error.

I also changed the error message to be consistent with the other malformed Mach-O
file error messages.  I also removed object_error::macho_small_load_command from
Object/Error.h as it is not needed and can just use object_error::parse_failed
and let the error message string distinguish the error.

Added:
    llvm/trunk/test/Object/Inputs/macho64-invalid-too-small-load-command.1   (with props)
Modified:
    llvm/trunk/include/llvm/Object/Error.h
    llvm/trunk/lib/Object/Error.cpp
    llvm/trunk/lib/Object/MachOObjectFile.cpp
    llvm/trunk/test/Object/macho-invalid.test

Modified: llvm/trunk/include/llvm/Object/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Error.h?rev=268463&r1=268462&r2=268463&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Error.h (original)
+++ llvm/trunk/include/llvm/Object/Error.h Tue May  3 18:13:50 2016
@@ -34,7 +34,6 @@ enum class object_error {
   string_table_non_null_end,
   invalid_section_index,
   bitcode_section_not_found,
-  macho_small_load_command,
   macho_load_segment_too_many_sections,
   macho_load_segment_too_small,
 };

Modified: llvm/trunk/lib/Object/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Error.cpp?rev=268463&r1=268462&r2=268463&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Error.cpp (original)
+++ llvm/trunk/lib/Object/Error.cpp Tue May  3 18:13:50 2016
@@ -47,8 +47,6 @@ std::string _object_error_category::mess
     return "Invalid section index";
   case object_error::bitcode_section_not_found:
     return "Bitcode section not found in object file";
-  case object_error::macho_small_load_command:
-    return "Mach-O load command with size < 8 bytes";
   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:

Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=268463&r1=268462&r2=268463&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Tue May  3 18:13:50 2016
@@ -177,11 +177,13 @@ static uint32_t getSectionFlags(const Ma
 }
 
 static Expected<MachOObjectFile::LoadCommandInfo>
-getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr) {
+getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr,
+                   uint32_t LoadCommandIndex) {
   if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) {
     if (CmdOrErr->cmdsize < 8)
-      return malformedError(*Obj, "Mach-O load command with size < 8 bytes",
-                            object_error::macho_small_load_command);
+      return malformedError(*Obj, Twine("truncated or malformed object (load "
+                            "command ") + Twine(LoadCommandIndex) +
+                            Twine(" with size less than 8 bytes)"));
     return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr});
   } else
     return CmdOrErr.takeError();
@@ -195,7 +197,7 @@ getFirstLoadCommandInfo(const MachOObjec
     return malformedError(*Obj, "truncated or malformed object (load command "
                           "0 extends past the end all load commands in the "
                           "file)");
-  return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize));
+  return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0);
 }
 
 static Expected<MachOObjectFile::LoadCommandInfo>
@@ -209,7 +211,7 @@ getNextLoadCommandInfo(const MachOObject
                           "(load command ") + Twine(LoadCommandIndex + 1) +
                           Twine(" extends past the end all load commands in the "
                           "file)"));
-  return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize);
+  return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1);
 }
 
 template <typename T>

Added: llvm/trunk/test/Object/Inputs/macho64-invalid-too-small-load-command.1
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/macho64-invalid-too-small-load-command.1?rev=268463&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/Object/Inputs/macho64-invalid-too-small-load-command.1
------------------------------------------------------------------------------
    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=268463&r1=268462&r2=268463&view=diff
==============================================================================
--- llvm/trunk/test/Object/macho-invalid.test (original)
+++ llvm/trunk/test/Object/macho-invalid.test Tue May  3 18:13:50 2016
@@ -19,6 +19,10 @@ RUN: not llvm-objdump -macho -private-he
 RUN:      | FileCheck -check-prefix SMALL-LOADC-SIZE %s
 SMALL-LOADC-SIZE: truncated or malformed object (load commands extend past the end of the file)
 
+RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho64-invalid-too-small-load-command.1 2>&1 \
+RUN:      | FileCheck -check-prefix SMALL-LOADC-SIZE-1 %s
+SMALL-LOADC-SIZE-1: truncated or malformed object (load command 1 with size less than 8 bytes)
+
 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 \




More information about the llvm-commits mailing list