[JITLink] Read MachO Header CPU field only in jitLink_MachO.
authorLang Hames <lhames@gmail.com>
Tue, 21 Apr 2020 23:50:19 +0000 (16:50 -0700)
committerLang Hames <lhames@gmail.com>
Wed, 22 Apr 2020 21:16:46 +0000 (14:16 -0700)
It's the only field we need in this function.

llvm/lib/ExecutionEngine/JITLink/MachO.cpp

index 58bc0f5..2ef2f5e 100644 (file)
@@ -16,9 +16,9 @@
 #include "llvm/BinaryFormat/MachO.h"
 #include "llvm/ExecutionEngine/JITLink/MachO_arm64.h"
 #include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h"
-#include "llvm/Support/Endian.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SwapByteOrder.h"
 
 using namespace llvm;
 
@@ -34,7 +34,9 @@ void jitLink_MachO(std::unique_ptr<JITLinkContext> Ctx) {
 
   StringRef Data = Ctx->getObjectBuffer().getBuffer();
   if (Data.size() < 4) {
-    Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer"));
+    StringRef BufferName = Ctx->getObjectBuffer().getBufferIdentifier();
+    Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer \"" +
+                                               BufferName + "\""));
     return;
   }
 
@@ -51,20 +53,26 @@ void jitLink_MachO(std::unique_ptr<JITLinkContext> Ctx) {
         make_error<JITLinkError>("MachO 32-bit platforms not supported"));
     return;
   } else if (Magic == MachO::MH_MAGIC_64 || Magic == MachO::MH_CIGAM_64) {
-    MachO::mach_header_64 Header;
 
-    memcpy(&Header, Data.data(), sizeof(MachO::mach_header_64));
+    if (Data.size() < sizeof(MachO::mach_header_64)) {
+      StringRef BufferName = Ctx->getObjectBuffer().getBufferIdentifier();
+      Ctx->notifyFailed(make_error<JITLinkError>("Truncated MachO buffer \"" +
+                                                 BufferName + "\""));
+      return;
+    }
+
+    // Read the CPU type from the header.
+    uint32_t CPUType;
+    memcpy(&CPUType, Data.data() + 4, sizeof(uint32_t));
     if (Magic == MachO::MH_CIGAM_64)
-      swapStruct(Header);
+      ByteSwap_32(CPUType);
 
     LLVM_DEBUG({
-      dbgs() << "jitLink_MachO: cputype = "
-             << format("0x%08" PRIx32, Header.cputype)
-             << ", cpusubtype = " << format("0x%08" PRIx32, Header.cpusubtype)
+      dbgs() << "jitLink_MachO: cputype = " << format("0x%08" PRIx32, CPUType)
              << "\n";
     });
 
-    switch (Header.cputype) {
+    switch (CPUType) {
     case MachO::CPU_TYPE_ARM64:
       return jitLink_MachO_arm64(std::move(Ctx));
     case MachO::CPU_TYPE_X86_64: