[PATCH] D17065: Defer CWD in MCContext lookup as late as possible.

Owen Anderson via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 9 23:13:08 PST 2016


resistor created this revision.
resistor added a reviewer: chandlerc.
resistor added a subscriber: llvm-commits.
resistor set the repository for this revision to rL LLVM.

Currently MCContext tries to lookup CWD on construction.  This causes sandboxing violations when using LLVM in a daemon without filesystem access.  The solution is defer CWD lookup until absolutely necessary.

Repository:
  rL LLVM

http://reviews.llvm.org/D17065

Files:
  include/llvm/MC/MCContext.h
  lib/MC/MCContext.cpp

Index: lib/MC/MCContext.cpp
===================================================================
--- lib/MC/MCContext.cpp
+++ lib/MC/MCContext.cpp
@@ -42,11 +42,6 @@
       GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
       AllowTemporaryLabels(true), DwarfCompileUnitID(0),
       AutoReset(DoAutoReset) {
-
-  std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
-  if (EC)
-    CompilationDir.clear();
-
   SecureLogFile = getenv("AS_SECURE_LOG_FILE");
   SecureLog = nullptr;
   SecureLogUsed = false;
@@ -67,6 +62,17 @@
   delete (raw_ostream *)SecureLog;
 }
 
+StringRef MCContext::getCompilationDir() const {
+  if (!CompilationDir) {
+    SmallString<128> Cwd;
+    std::error_code EC = llvm::sys::fs::current_path(Cwd);
+    if (EC)
+      Cwd.clear();
+    CompilationDir = Cwd;
+  }
+  return *CompilationDir;
+}
+
 //===----------------------------------------------------------------------===//
 // Module Lifetime Management
 //===----------------------------------------------------------------------===//
@@ -86,7 +92,7 @@
   SectionSymbols.clear();
   Allocator.Reset();
   Instances.clear();
-  CompilationDir.clear();
+  CompilationDir.reset();
   MainFileName.clear();
   MCDwarfLineTablesCUMap.clear();
   SectionsForRanges.clear();
Index: include/llvm/MC/MCContext.h
===================================================================
--- include/llvm/MC/MCContext.h
+++ include/llvm/MC/MCContext.h
@@ -11,6 +11,7 @@
 #define LLVM_MC_MCCONTEXT_H
 
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
@@ -116,7 +117,7 @@
     bool SecureLogUsed;
 
     /// The compilation directory to use for DW_AT_comp_dir.
-    SmallString<128> CompilationDir;
+    mutable Optional<SmallString<128>> CompilationDir;
 
     /// The main file name if passed in explicitly.
     std::string MainFileName;
@@ -392,11 +393,13 @@
     /// compilation directory and have it be something other than the current
     /// working directory.
     /// Returns an empty string if the current directory cannot be determined.
-    StringRef getCompilationDir() const { return CompilationDir; }
+    StringRef getCompilationDir() const;
 
     /// \brief Set the compilation directory for DW_AT_comp_dir
     /// Override the default (CWD) compilation directory.
-    void setCompilationDir(StringRef S) { CompilationDir = S.str(); }
+    void setCompilationDir(StringRef S) {
+      CompilationDir = SmallString<128>(S.str());
+    }
 
     /// \brief Get the main file name for use in error messages and debug
     /// info. This can be set to ensure we've got the correct file name


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17065.47424.patch
Type: text/x-patch
Size: 2729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160210/ce79426d/attachment.bin>


More information about the llvm-commits mailing list