[PATCH] D111706: [lld-macho] Use StringRef's hash code as keys of loadedArchives

Jacket via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 13 03:49:25 PDT 2021


PRESIDENT810 created this revision.
PRESIDENT810 added a reviewer: int3.
Herald added a reviewer: gkm.
Herald added a project: lld-macho.
Herald added a reviewer: lld-macho.
PRESIDENT810 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When using StringRef as keys of loadedArchives, to find the value corresponding to a key relies comparing the StringRef instance in the DenseMap and the key used to lookup, and if the StringRef instance's actual string data is released, such lookup will return false even though the value is still present in the map.

If use the hash code of StringRef instead, same archive path always maps to the same archive file, even if the data of StringRef no longer available. This avoids loading a same archive twice, as the case described in https://bugs.llvm.org/show_bug.cgi?id=52133.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111706

Files:
  lld/MachO/Driver.cpp


Index: lld/MachO/Driver.cpp
===================================================================
--- lld/MachO/Driver.cpp
+++ lld/MachO/Driver.cpp
@@ -224,7 +224,7 @@
   return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy");
 }
 
-static DenseMap<StringRef, ArchiveFile *> loadedArchives;
+static DenseMap<hash_code, ArchiveFile *> loadedArchives;
 
 static InputFile *addFile(StringRef path, bool forceLoadArchive,
                           bool isExplicit = true, bool isBundleLoader = false) {
@@ -234,6 +234,7 @@
   MemoryBufferRef mbref = *buffer;
   InputFile *newFile = nullptr;
 
+  hash_code hashedPath = hash_value(path);
   file_magic magic = identify_magic(mbref.getBuffer());
   switch (magic) {
   case file_magic::archive: {
@@ -243,7 +244,7 @@
     // We don't take a reference to cachedFile here because the
     // loadArchiveMember() call below may recursively call addFile() and
     // invalidate this reference.
-    if (ArchiveFile *cachedFile = loadedArchives[path])
+    if (ArchiveFile *cachedFile = loadedArchives[hashedPath])
       return cachedFile;
 
     std::unique_ptr<object::Archive> archive = CHECK(
@@ -290,7 +291,7 @@
     }
 
     file->addLazySymbols();
-    newFile = loadedArchives[path] = file;
+    newFile = loadedArchives[hashedPath] = file;
     break;
   }
   case file_magic::macho_object:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111706.379330.patch
Type: text/x-patch
Size: 1363 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211013/bb497e9d/attachment.bin>


More information about the llvm-commits mailing list