[llvm-bugs] [Bug 28093] New: CIndexer::getResourcesPath is not thread-safe

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jun 12 03:39:34 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=28093

            Bug ID: 28093
           Summary: CIndexer::getResourcesPath is not thread-safe
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: libclang
          Assignee: unassignedclangbugs at nondot.org
          Reporter: raph.amiard at gmail.com
                CC: klimek at google.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

Created attachment 16518
  --> https://llvm.org/bugs/attachment.cgi?id=16518&action=edit
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 <amiard at adacore.com>
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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160612/5b27b89f/attachment.html>


More information about the llvm-bugs mailing list