<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - CIndexer::getResourcesPath is not thread-safe"
   href="https://llvm.org/bugs/show_bug.cgi?id=28093">28093</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>CIndexer::getResourcesPath is not thread-safe
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>libclang
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>raph.amiard@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>klimek@google.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16518" name="attach_16518" title="The patch">attachment 16518</a> <a href="attachment.cgi?id=16518&action=edit" title="The patch">[details]</a></span>
The patch

CIndexer::GetResourcesPath is not thread-safe, the static cache that it
contains is not thread safe.

This makes using libclang's index from multiple threads dangerous.

Here is a patch that fixes this problem:

>From a700691bd9fe070b0703e36b3ac4b1679d02eb05 Mon Sep 17 00:00:00 2001
From: Raphael Amiard <<a href="mailto:amiard@adacore.com">amiard@adacore.com</a>>
Date: Tue, 17 Nov 2015 18:57:50 +0100
Subject: [PATCH] libclang: Make CIndexer::getClangResourcesPath thread safe

The cache contained in ResourcesPath was not thread safe. It is now
protected by a mutex guard.

Change-Id: Id713a43f733625ecb5625957683f16cadc518da7
---
 tools/libclang/CIndexer.cpp |    4 ++++
 tools/libclang/CIndexer.h   |    4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/libclang/CIndexer.cpp b/tools/libclang/CIndexer.cpp
index 91154cc..2927f21 100644
--- a/tools/libclang/CIndexer.cpp
+++ b/tools/libclang/CIndexer.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/MutexGuard.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cstdio>
@@ -44,6 +45,9 @@
 using namespace clang;

 const std::string &CIndexer::getClangResourcesPath() {
+  // Protect the clang get ressources path logic with a mutex.
+  llvm::MutexGuard MG(Mux);
+
   // Did we already compute the path?
   if (!ResourcesPath.empty())
     return ResourcesPath;
diff --git a/tools/libclang/CIndexer.h b/tools/libclang/CIndexer.h
index 95d8115..0f60d05 100644
--- a/tools/libclang/CIndexer.h
+++ b/tools/libclang/CIndexer.h
@@ -17,6 +17,7 @@

 #include "clang-c/Index.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Mutex.h"
 #include "llvm/Support/Path.h"
 #include <vector>

@@ -36,12 +37,13 @@ class CIndexer {
   bool OnlyLocalDecls;
   bool DisplayDiagnostics;
   unsigned Options; // CXGlobalOptFlags.
+  llvm::sys::Mutex Mux;

   std::string ResourcesPath;

 public:
  CIndexer() : OnlyLocalDecls(false), DisplayDiagnostics(false),
-              Options(CXGlobalOpt_None) { }
+              Options(CXGlobalOpt_None), Mux(false) { }

   /// \brief Whether we only want to see "local" declarations (that did not
   /// come from a previous precompiled header). If false, we want to see all
-- 
1.7.10.4</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>