[llvm] 4d1d8a8 - [dsymutil] Fix data race in input verification (NFC)

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 18 11:32:32 PDT 2023


Author: Jonas Devlieghere
Date: 2023-08-18T11:32:27-07:00
New Revision: 4d1d8a885df292b23160b8998061c9fe17a0d677

URL: https://github.com/llvm/llvm-project/commit/4d1d8a885df292b23160b8998061c9fe17a0d677
DIFF: https://github.com/llvm/llvm-project/commit/4d1d8a885df292b23160b8998061c9fe17a0d677.diff

LOG: [dsymutil] Fix data race in input verification (NFC)

Dump verification errors to a local buffer instead of racing stdio and
potentially showing interleaved output.

Added: 
    

Modified: 
    llvm/include/llvm/DWARFLinker/DWARFLinker.h
    llvm/include/llvm/DWARFLinkerParallel/DWARFLinker.h
    llvm/lib/DWARFLinker/DWARFLinker.cpp
    llvm/tools/dsymutil/DwarfLinkerForBinary.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/DWARFLinker/DWARFLinker.h b/llvm/include/llvm/DWARFLinker/DWARFLinker.h
index 7a19858f426792..b11e8e14aee4ee 100644
--- a/llvm/include/llvm/DWARFLinker/DWARFLinker.h
+++ b/llvm/include/llvm/DWARFLinker/DWARFLinker.h
@@ -338,7 +338,7 @@ class DWARFLinker {
     Pub,       ///< .debug_pubnames, .debug_pubtypes
     DebugNames ///< .debug_names.
   };
-  typedef std::function<void(const DWARFFile &File)> inputVerificationHandler;
+  typedef std::function<void(const DWARFFile &File, llvm::StringRef Output)> inputVerificationHandler;
   typedef std::function<ErrorOr<DWARFFile &>(StringRef ContainerName,
                                              StringRef Path)>
       objFileLoader;

diff  --git a/llvm/include/llvm/DWARFLinkerParallel/DWARFLinker.h b/llvm/include/llvm/DWARFLinkerParallel/DWARFLinker.h
index 3c725fc4f53a84..f6362f51005564 100644
--- a/llvm/include/llvm/DWARFLinkerParallel/DWARFLinker.h
+++ b/llvm/include/llvm/DWARFLinkerParallel/DWARFLinker.h
@@ -134,7 +134,7 @@ class DWARFLinker {
       const Twine &Warning, StringRef Context, const DWARFDie *DIE)>;
   using ObjFileLoaderTy = std::function<ErrorOr<DWARFFile &>(
       StringRef ContainerName, StringRef Path)>;
-  using InputVerificationHandlerTy = std::function<void(const DWARFFile &File)>;
+  using InputVerificationHandlerTy = std::function<void(const DWARFFile &File, llvm::StringRef Output)>;
   using ObjectPrefixMapTy = std::map<std::string, std::string>;
   using CompileUnitHandlerTy = function_ref<void(const DWARFUnit &Unit)>;
   using TranslatorFuncTy = std::function<StringRef(StringRef)>;

diff  --git a/llvm/lib/DWARFLinker/DWARFLinker.cpp b/llvm/lib/DWARFLinker/DWARFLinker.cpp
index 58c52534f05f45..dbbd1b434c7c8b 100644
--- a/llvm/lib/DWARFLinker/DWARFLinker.cpp
+++ b/llvm/lib/DWARFLinker/DWARFLinker.cpp
@@ -3057,11 +3057,13 @@ Error DWARFLinker::cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
 void DWARFLinker::verifyInput(const DWARFFile &File) {
   assert(File.Dwarf);
 
-  raw_ostream &os = Options.Verbose ? errs() : nulls();
+
+  std::string Buffer;
+  raw_string_ostream OS(Buffer);
   DIDumpOptions DumpOpts;
-  if (!File.Dwarf->verify(os, DumpOpts.noImplicitRecursion())) {
+  if (!File.Dwarf->verify(OS, DumpOpts.noImplicitRecursion())) {
     if (Options.InputVerificationHandler)
-      Options.InputVerificationHandler(File);
+      Options.InputVerificationHandler(File, OS.str());
   }
 }
 

diff  --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
index 4b281d5f19801a..63dc6993247104 100644
--- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
+++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
@@ -638,8 +638,11 @@ bool DwarfLinkerForBinary::linkImpl(
   GeneralLinker->setNumThreads(Options.Threads);
   GeneralLinker->setPrependPath(Options.PrependPath);
   GeneralLinker->setKeepFunctionForStatic(Options.KeepFunctionForStatic);
-  GeneralLinker->setInputVerificationHandler([&](const OutDwarfFile &File) {
-    reportWarning("input verification failed", File.FileName);
+  GeneralLinker->setInputVerificationHandler([&](const OutDwarfFile &File, llvm::StringRef Output) {
+    std::lock_guard<std::mutex> Guard(ErrorHandlerMutex);
+    if (Options.Verbose)
+      errs() << Output;
+    warn("input verification failed", File.FileName);
     HasVerificationErrors = true;
   });
   auto Loader = [&](StringRef ContainerName,


        


More information about the llvm-commits mailing list