[llvm] 8fbe1e7 - [llvm-objcopy] Fix misaligned access to load command data.

Daniel Rodríguez Troitiño via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 28 22:17:07 PDT 2021


Author: Daniel Rodríguez Troitiño
Date: 2021-10-28T22:14:39-07:00
New Revision: 8fbe1e760224cc4941f1f014d1a3c7480f2e0e7c

URL: https://github.com/llvm/llvm-project/commit/8fbe1e760224cc4941f1f014d1a3c7480f2e0e7c
DIFF: https://github.com/llvm/llvm-project/commit/8fbe1e760224cc4941f1f014d1a3c7480f2e0e7c.diff

LOG: [llvm-objcopy] Fix misaligned access to load command data.

It seems that llvm-objcopy stores data temporarily misaligned with the
requirements of the underlaying struct from libBinaryFormat, and UBSan
generates a runtime error.

Instead of trying to reinterpret the memory as the struct itself, simply
access the `char *` pointer that we are interested in, and that do not
have alignment restrictions.

This problem was pointed out in a comment of D111164.

Differential Revision: https://reviews.llvm.org/D112744

Added: 
    

Modified: 
    llvm/tools/llvm-objcopy/MachO/MachOReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
index c0377e79b5d1f..38e5e645c596a 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -124,9 +124,12 @@ Error MachOReader::readLoadCommands(Object &O) const {
       O.CodeSignatureCommandIndex = O.LoadCommands.size();
       break;
     case MachO::LC_SEGMENT:
-      if (StringRef(
-              reinterpret_cast<MachO::segment_command const *>(LoadCmd.Ptr)
-                  ->segname) == TextSegmentName)
+      // LoadCmd.Ptr might not be aligned temporarily as
+      // MachO::segment_command requires, but the segname char pointer do not
+      // have alignment restrictions.
+      if (StringRef(reinterpret_cast<const char *>(
+              LoadCmd.Ptr + offsetof(MachO::segment_command, segname))) ==
+          TextSegmentName)
         O.TextSegmentCommandIndex = O.LoadCommands.size();
 
       if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
@@ -137,9 +140,12 @@ Error MachOReader::readLoadCommands(Object &O) const {
         return Sections.takeError();
       break;
     case MachO::LC_SEGMENT_64:
-      if (StringRef(
-              reinterpret_cast<MachO::segment_command_64 const *>(LoadCmd.Ptr)
-                  ->segname) == TextSegmentName)
+      // LoadCmd.Ptr might not be aligned temporarily as
+      // MachO::segment_command_64 requires, but the segname char pointer do
+      // not have alignment restrictions.
+      if (StringRef(reinterpret_cast<const char *>(
+              LoadCmd.Ptr + offsetof(MachO::segment_command_64, segname))) ==
+          TextSegmentName)
         O.TextSegmentCommandIndex = O.LoadCommands.size();
 
       if (Expected<std::vector<std::unique_ptr<Section>>> Sections =


        


More information about the llvm-commits mailing list