summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/BinaryFormat/MachOTest.cpp
diff options
context:
space:
mode:
authorChris Bieneman <chris.bieneman@me.com>2018-06-01 22:07:36 +0000
committerChris Bieneman <chris.bieneman@me.com>2018-06-01 22:07:36 +0000
commitc8a3c86c77362ba8da55ff7514d03205771179c2 (patch)
treed851125fafc120aeca0ec7ba79359e0ed0efd17d /llvm/unittests/BinaryFormat/MachOTest.cpp
parentd74d04a6c5241f9e19147801573d84ed46030ecf (diff)
downloadbcm5719-llvm-c8a3c86c77362ba8da55ff7514d03205771179c2.tar.gz
bcm5719-llvm-c8a3c86c77362ba8da55ff7514d03205771179c2.zip
[MachO] Fixing ub in MachO BinaryFormat
This isn't encountered anywhere inside LLVM, so I wrote a test case to expose the issue and verify that it is fixed. The basic problem is that the macho_load_command union contains all load comamnd structs. Load command structs in 32-bit macho files can be 32-bit aligned instead of 64-bit aligned. There are some strange circumstances in which this can be exposed in a 64-bit macho if the load commands are invalid or if a 32-bit aligned load command is used. In the past we've worked around this type of problem with changes like r264232. llvm-svn: 333797
Diffstat (limited to 'llvm/unittests/BinaryFormat/MachOTest.cpp')
-rw-r--r--llvm/unittests/BinaryFormat/MachOTest.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/llvm/unittests/BinaryFormat/MachOTest.cpp b/llvm/unittests/BinaryFormat/MachOTest.cpp
new file mode 100644
index 00000000000..d55440a61b3
--- /dev/null
+++ b/llvm/unittests/BinaryFormat/MachOTest.cpp
@@ -0,0 +1,43 @@
+//===- unittest/BinaryFormat/MachOTest.cpp - MachO support tests ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/BinaryFormat/MachO.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::MachO;
+
+TEST(MachOTest, UnalignedLC) {
+ const unsigned char Valid32BitMachO[] = {
+ 0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+ 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
+ 0x85, 0x80, 0x21, 0x01, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
+ 0x5F, 0x5F, 0x50, 0x41, 0x47, 0x45, 0x5A, 0x45, 0x52, 0x4F, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x4C, 0x49,
+ 0x4E, 0x4B, 0x45, 0x44, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x40, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
+ 0x8C, 0x0B, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+ const mach_header *Header =
+ reinterpret_cast<const mach_header *>(Valid32BitMachO);
+ ASSERT_EQ(Header->magic, MH_MAGIC);
+ const unsigned char *Current = Valid32BitMachO + sizeof(mach_header);
+ const unsigned char *BufferEnd =
+ Valid32BitMachO + sizeof(mach_header) + Header->sizeofcmds;
+ while (Current < BufferEnd) {
+ const macho_load_command *LC =
+ reinterpret_cast<const macho_load_command *>(Current);
+ ASSERT_EQ(LC->load_command_data.cmd, LC_SEGMENT);
+ Current += LC->load_command_data.cmdsize;
+ }
+}
OpenPOWER on IntegriCloud