[clang][deps] Fix race condition
authorJan Svoboda <jan_svoboda@apple.com>
Mon, 6 Feb 2023 19:36:51 +0000 (11:36 -0800)
committerJan Svoboda <jan_svoboda@apple.com>
Tue, 7 Feb 2023 17:26:56 +0000 (09:26 -0800)
D140176 introduced new `FullDeps` API that's not thread-safe, breaking the class invariant. This was causing race condition when `clang-scan-deps` was run with multiple threads.

Reviewed By: steven_wu, akyrtzi

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

clang/tools/clang-scan-deps/ClangScanDeps.cpp

index 41bc0ea..1458d11 100644 (file)
@@ -252,14 +252,17 @@ class FullDeps {
 public:
   void mergeDeps(StringRef Input, TranslationUnitDeps TUDeps,
                  size_t InputIndex) {
+    mergeDeps(std::move(TUDeps.ModuleGraph), InputIndex);
+
     InputDeps ID;
     ID.FileName = std::string(Input);
     ID.ContextHash = std::move(TUDeps.ID.ContextHash);
     ID.FileDeps = std::move(TUDeps.FileDeps);
     ID.ModuleDeps = std::move(TUDeps.ClangModuleDeps);
-    mergeDeps(std::move(TUDeps.ModuleGraph), InputIndex);
     ID.DriverCommandLine = std::move(TUDeps.DriverCommandLine);
     ID.Commands = std::move(TUDeps.Commands);
+
+    std::unique_lock<std::mutex> ul(Lock);
     Inputs.push_back(std::move(ID));
   }