OSDN Git Service

[yaml2macho] Removing asserts in favor of explicit yaml parse error
authorChris Bieneman <beanz@apple.com>
Thu, 23 Jun 2016 22:36:31 +0000 (22:36 +0000)
committerChris Bieneman <beanz@apple.com>
Thu, 23 Jun 2016 22:36:31 +0000 (22:36 +0000)
32-bit Mach headers don't have reserved fields. When generating the
mapping for 32-bit headers leaving off the reserved field will result in
parse errors if the field is present in the yaml.

Added a CHECK-NOT line to ensure that mach_header.yaml isn't adding a
reserved field, and a test to ensure that the parser error gets hit with
32-bit headers.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273623 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ObjectYAML/MachOYAML.cpp
test/ObjectYAML/MachO/mach_header.yaml
test/ObjectYAML/MachO/mach_header_32_malformed.yaml [new file with mode: 0644]
tools/yaml2obj/yaml2macho.cpp

index b4e8085..d13c84e 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/ObjectYAML/MachOYAML.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/MachO.h"
 
 #include <string.h> // For memcpy, memset and strnlen.
 
@@ -79,8 +80,9 @@ void MappingTraits<MachOYAML::FileHeader>::mapping(
   IO.mapRequired("ncmds", FileHdr.ncmds);
   IO.mapRequired("sizeofcmds", FileHdr.sizeofcmds);
   IO.mapRequired("flags", FileHdr.flags);
-  IO.mapOptional("reserved", FileHdr.reserved,
-                 static_cast<llvm::yaml::Hex32>(0xDEADBEEFu));
+  if (FileHdr.magic == MachO::MH_MAGIC_64 ||
+      FileHdr.magic == MachO::MH_CIGAM_64)
+    IO.mapRequired("reserved", FileHdr.reserved);
 }
 
 void MappingTraits<MachOYAML::Object>::mapping(IO &IO,
index 722a00b..8af3187 100644 (file)
@@ -20,4 +20,5 @@ FileHeader:
 # CHECK:   ncmds:           0
 # CHECK:   sizeofcmds:      0
 # CHECK:   flags:           0x00218085
+# CHECK-NOT: reserved:
 # CHECK: ...
diff --git a/test/ObjectYAML/MachO/mach_header_32_malformed.yaml b/test/ObjectYAML/MachO/mach_header_32_malformed.yaml
new file mode 100644 (file)
index 0000000..33946f3
--- /dev/null
@@ -0,0 +1,15 @@
+# RUN: not yaml2obj -format=macho %s 2>&1 | FileCheck %s
+
+--- !mach-o
+FileHeader:      
+  magic:           0xFEEDFACE
+  cputype:         0x00000007
+  cpusubtype:      0x80000003
+  filetype:        0x00000002
+  ncmds:           0
+  sizeofcmds:      0
+  flags:           0x00218085
+  reserved:        0
+...
+
+# CHECK: error: unknown key 'reserved'
index 510f760..7821e63 100644 (file)
@@ -32,10 +32,6 @@ public:
     is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 ||
               Obj.Header.magic == MachO::MH_CIGAM_64;
     memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64));
-    assert((is64Bit || Obj.Header.reserved == 0xDEADBEEFu) &&
-           "32-bit MachO has reserved in header");
-    assert((!is64Bit || Obj.Header.reserved != 0xDEADBEEFu) &&
-           "64-bit MachO has missing reserved in header");
   }
 
   Error writeMachO(raw_ostream &OS);