[ObjectFileELF] Fix misaligned read/writes caught by UBSan.
authorDavide Italiano <davide@freebsd.org>
Tue, 6 Nov 2018 17:11:34 +0000 (17:11 +0000)
committerDavide Italiano <davide@freebsd.org>
Tue, 6 Nov 2018 17:11:34 +0000 (17:11 +0000)
llvm-svn: 346244

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp

index fd1edcd6a64aeaa8d6d21e1794c8e36376cdd6f7..1001eaef555a85c1d07d9d913af1f7f3f9b9645c 100644 (file)
@@ -2712,7 +2712,8 @@ unsigned ObjectFileELF::ApplyRelocations(
           uint64_t *dst = reinterpret_cast<uint64_t *>(
               data_buffer_sp->GetBytes() + rel_section->GetFileOffset() +
               ELFRelocation::RelocOffset64(rel));
-          *dst = value + ELFRelocation::RelocAddend64(rel);
+          uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);
+          memcpy(dst, &val_offset, sizeof(uint64_t));
         }
         break;
       }
@@ -2738,7 +2739,7 @@ unsigned ObjectFileELF::ApplyRelocations(
           uint32_t *dst = reinterpret_cast<uint32_t *>(
               data_buffer_sp->GetBytes() + rel_section->GetFileOffset() +
               ELFRelocation::RelocOffset32(rel));
-          *dst = truncated_addr;
+          memcpy(dst, &truncated_addr, sizeof(uint32_t));
         }
         break;
       }
index 86bbe0330b77da0642df0ac02b1c633a33b8192e..00b66c3a797fdc3455d353e1e49f48ef3f2f342b 100644 (file)
@@ -143,10 +143,17 @@ TEST_F(ObjectFileELFTest, GetModuleSpecifications_EarlySectionHeaders) {
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
-#define CHECK_ABS32(offset, addend)                                            \
-  ASSERT_EQ((uint32_t)addend, *(uint32_t *)(bytes + offset))
-#define CHECK_ABS64(offset, addend)                                            \
-  ASSERT_EQ((uint64_t)addend, *(uint64_t *)(bytes + offset))
+static void CHECK_ABS32(uint8_t *bytes, uint32_t offset, uint32_t addend) {
+  uint32_t res;
+  memcpy(&res, (uint32_t *)(bytes + offset), sizeof(uint32_t));
+  ASSERT_EQ(addend, res);
+}
+
+static void CHECK_ABS64(uint8_t *bytes, uint64_t offset, uint64_t addend) {
+  uint64_t res;
+  memcpy(&res, (uint64_t *)(bytes + offset), sizeof(uint64_t));
+  ASSERT_EQ(addend, res);
+}
 
 TEST_F(ObjectFileELFTest, TestAARCH64Relocations) {
   std::string yaml = GetInputFilePath("debug-info-relocations.pcm.yaml");
@@ -193,13 +200,13 @@ TEST_F(ObjectFileELFTest, TestAARCH64Relocations) {
   // .rela.debug_info contains 9 relocations:
   // 7 R_AARCH64_ABS32 - 2 R_AARCH64_ABS64
   // None have a value. Four have addends.
-  CHECK_ABS32(0x6, 0);
-  CHECK_ABS32(0xC, 0);
-  CHECK_ABS32(0x12, 45);
-  CHECK_ABS32(0x16, 0);
-  CHECK_ABS32(0x1A, 55);
-  CHECK_ABS64(0x1E, 0);
-  CHECK_ABS64(0x2B, 0);
-  CHECK_ABS32(0x39, 73);
-  CHECK_ABS32(0x44, 75);
+  CHECK_ABS32(bytes, 0x6, 0);
+  CHECK_ABS32(bytes, 0xC, 0);
+  CHECK_ABS32(bytes, 0x12, 45);
+  CHECK_ABS32(bytes, 0x16, 0);
+  CHECK_ABS32(bytes, 0x1A, 55);
+  CHECK_ABS64(bytes, 0x1E, 0);
+  CHECK_ABS64(bytes, 0x2B, 0);
+  CHECK_ABS32(bytes, 0x39, 73);
+  CHECK_ABS32(bytes, 0x44, 75);
 }