[PATCH] D80414: [lld-macho] Ensure reads from nlist_64 structs are aligned when necessary

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 21 15:43:54 PDT 2020


int3 created this revision.
int3 added reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

My test refactoring in D80217 <https://reviews.llvm.org/D80217> seems to have caused yaml2obj to emit unaligned
nlist_64 structs, causing ASAN'd lld to be unhappy. I don't think this is an
issue with yaml2obj though -- llvm-mc also seems to emit unaligned nlist_64s.
This diff makes lld able to safely do unaligned reads under ASAN builds while
hopefully creating no overhead for regular builds.

Depends on D80217 <https://reviews.llvm.org/D80217>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80414

Files:
  lld/MachO/InputFiles.cpp


Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -230,7 +230,13 @@
   };
 
   for (size_t i = 0, n = nList.size(); i < n; ++i) {
-    const nlist_64 &sym = nList[i];
+    nlist_64 sym;
+    // nlist_64 structs should be 64-bit-aligned according to the C++ language
+    // spec, but nList (which is mmap'ed from a file) may not be at an aligned
+    // offset. This memcpy() ensures that we have the correct alignment, but
+    // it should be optimized away on architectures that support unaligned
+    // reads.
+    memcpy(&sym, nList.data() + i, sizeof(nlist_64));
 
     // Undefined symbol
     if (!sym.n_sect) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80414.265630.patch
Type: text/x-patch
Size: 732 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200521/d5279394/attachment.bin>


More information about the llvm-commits mailing list