about summary refs log tree commit diff homepage
path: root/tools
diff options
context:
space:
mode:
authorMartinNowack <martin.nowack@gmail.com>2014-04-15 14:54:52 +0200
committerMartinNowack <martin.nowack@gmail.com>2014-04-15 14:54:52 +0200
commit5e0682ee26e02c710abd284f9f965e7a2c9a9f32 (patch)
tree69bcaf2485b5b0dcecc32002de9335b667f65d0b /tools
parent237899d2fe681e5ea70baef5104c43feba87dea2 (diff)
parent78e06cb737e3e54e6c7035822f39961679e7b367 (diff)
downloadklee-5e0682ee26e02c710abd284f9f965e7a2c9a9f32.tar.gz
Merge pull request #104 from MartinNowack/llvm_34
Merge support for LLVM 3.4
Diffstat (limited to 'tools')
-rw-r--r--tools/klee/main.cpp176
1 files changed, 83 insertions, 93 deletions
diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp
index 1fe28270..a2268c83 100644
--- a/tools/klee/main.cpp
+++ b/tools/klee/main.cpp
@@ -37,6 +37,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
 
 // FIXME: Ugh, this is gross. But otherwise our config.h conflicts with LLVMs.
 #undef PACKAGE_BUGREPORT
@@ -222,7 +223,8 @@ private:
   TreeStreamWriter *m_pathWriter, *m_symPathWriter;
   std::ostream *m_infoFile;
 
-  sys::Path m_outputDirectory;
+  SmallString<128> m_outputDirectory;
+
   unsigned m_testIndex;  // number of tests written so far
   unsigned m_pathsExplored; // number of paths explored so far
 
@@ -261,7 +263,7 @@ public:
   static void getOutFiles(std::string path,
 			  std::vector<std::string> &results);
 
-  static llvm::sys::Path getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
+  static std::string getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
 };
 
 KleeHandler::KleeHandler(int argc, char **argv) 
@@ -275,81 +277,66 @@ KleeHandler::KleeHandler(int argc, char **argv)
     m_argc(argc),
     m_argv(argv) {
 
-  if (OutputDir=="") {
-    llvm::sys::Path directory(InputFile);
-    std::stringstream dirname;
-    directory.eraseComponent();
-    
-    if (directory.isEmpty())
-      directory.set(".");
-    
-    for (int i = 0; i< INT_MAX ; i++) {
-      dirname << "klee-out-";
-      dirname << i;
+  // create output directory (OutputDir or "klee-out-<i>")
+  bool dir_given = OutputDir != "";
+  SmallString<128> directory(dir_given ? OutputDir : InputFile);
 
-      m_outputDirectory = llvm::sys::Path(directory); // Copy
-      if (!m_outputDirectory.appendComponent(dirname.str()))
-        klee_error("Failed to append \"%s\" to \"%s\"", dirname.str().c_str(), directory.c_str());
-      
-      bool isDir = true;
-      llvm::error_code e = llvm::sys::fs::exists(m_outputDirectory.str(), isDir);
-      if ( e != llvm::errc::success )
-        klee_error("Failed to check if \"%s\" exists.", m_outputDirectory.str().c_str());
-
-      if (!isDir)
-      {
-        break; // Found an available directory name
-      }
+  error_code ec;
+  if (!dir_given) sys::path::remove_filename(directory);
+  if ((ec = sys::fs::make_absolute(directory)) != errc::success)
+    klee_error("unable to determine absolute path: %s", ec.message().c_str());
 
-      // Warn the user if the klee-out-* exists but is not a directory
-      e = llvm::sys::fs::is_directory(m_outputDirectory.str(), isDir);
-      if ( e == llvm::errc::success && !isDir )
-        klee_warning("A file \"%s\" exists, but it is not a directory",
-                     m_outputDirectory.str().c_str());
+  if (dir_given) {
+    // OutputDir
+    if (mkdir(directory.c_str(), 0775) < 0)
+      klee_error("cannot create \"%s\": %s", directory.c_str(), strerror(errno));
 
-      dirname.str(""); // Clear
-      m_outputDirectory.clear();
-    }    
+    m_outputDirectory = directory;
+  } else {
+    // "klee-out-<i>"
+    int i = 0;
+    for (; i <= INT_MAX; ++i) {
+      SmallString<128> d(directory);
+      llvm::sys::path::append(d, "klee-out-");
+      raw_svector_ostream ds(d); ds << i; ds.flush();
 
-    if (m_outputDirectory.empty())
-      klee_error("Failed to find available output directory in %s", dirname.str().c_str());
+      // create directory and try to link klee-last
+      if (mkdir(d.c_str(), 0775) == 0) {
+        m_outputDirectory = d;
 
-    std::cerr << "KLEE: output directory = \"" << dirname.str() << "\"\n";
+        SmallString<128> klee_last(directory);
+        llvm::sys::path::append(klee_last, "klee-last");
 
-    llvm::sys::Path klee_last(directory);
-    if(!klee_last.appendComponent("klee-last"))
-      klee_error("cannot create path name for klee-last");
+        if (((unlink(klee_last.c_str()) < 0) && (errno != ENOENT)) ||
+            symlink(m_outputDirectory.c_str(), klee_last.c_str()) < 0) {
 
-    if ((unlink(klee_last.c_str()) < 0) && (errno != ENOENT))
-      klee_error("cannot unlink klee-last: %s", strerror(errno));
-    
-    if (symlink(dirname.str().c_str(), klee_last.c_str()) < 0)
-      klee_error("cannot create klee-last symlink: %s", strerror(errno));
+          klee_warning("cannot create klee-last symlink: %s", strerror(errno));
+        }
 
-  } else {
-    if (!m_outputDirectory.set(OutputDir))
-      klee_error("cannot use klee output directory: %s", OutputDir.c_str());
-  }
-  
-  if (!sys::path::is_absolute(m_outputDirectory.c_str())) {
-    sys::Path cwd = sys::Path::GetCurrentDirectory();
-    if(!cwd.appendComponent( m_outputDirectory.c_str()))
-      klee_error("cannot create absolute path name for output directory");
+        break;
+      }
 
-    m_outputDirectory = cwd;
+      // otherwise try again or exit on error
+      if (errno != EEXIST)
+        klee_error("cannot create \"%s\": %s", m_outputDirectory.c_str(), strerror(errno));
+    }
+    if (i == INT_MAX && m_outputDirectory.str().equals(""))
+        klee_error("cannot create output directory: index out of range");
   }
 
-  if (mkdir(m_outputDirectory.c_str(), 0775) < 0)
-    klee_error("cannot create directory \"%s\": %s", m_outputDirectory.c_str(), strerror(errno));
+  klee_message("output directory is \"%s\"", m_outputDirectory.c_str());
 
+  // open warnings.txt
   std::string file_path = getOutputFilename("warnings.txt");
   if ((klee_warning_file = fopen(file_path.c_str(), "w")) == NULL)
     klee_error("cannot open file \"%s\": %s", file_path.c_str(), strerror(errno));
 
+  // open messages.txt
   file_path = getOutputFilename("messages.txt");
   if ((klee_message_file = fopen(file_path.c_str(), "w")) == NULL)
     klee_error("cannot open file \"%s\": %s", file_path.c_str(), strerror(errno));
 
+  // open info
   m_infoFile = openOutputFile("info");
 }
 
@@ -378,11 +365,8 @@ void KleeHandler::setInterpreter(Interpreter *i) {
 }
 
 std::string KleeHandler::getOutputFilename(const std::string &filename) {
-  sys::Path path(m_outputDirectory);
-  if(!path.appendComponent(filename)) {
-    klee_error("cannot create path name for \"%s\"", filename.c_str());
-  }
-
+  SmallString<128> path = m_outputDirectory;
+  sys::path::append(path,filename);
   return path.str();
 }
 
@@ -557,47 +541,54 @@ void KleeHandler::loadPathFile(std::string name,
 
 void KleeHandler::getOutFiles(std::string path,
 			      std::vector<std::string> &results) {
-  llvm::sys::Path p(path);
-  std::set<llvm::sys::Path> contents;
-  std::string error;
-  if (p.getDirectoryContents(contents, &error)) {
-    std::cerr << "ERROR: unable to read output directory: " << path 
-               << ": " << error << "\n";
-    exit(1);
-  }
-  for (std::set<llvm::sys::Path>::iterator it = contents.begin(),
-         ie = contents.end(); it != ie; ++it) {
-    std::string f = it->str();
+  error_code ec;
+  for (llvm::sys::fs::directory_iterator i(path,ec),e; i!=e && !ec; i.increment(ec)){
+    std::string f = (*i).path();
     if (f.substr(f.size()-6,f.size()) == ".ktest") {
-      results.push_back(f);
+          results.push_back(f);
     }
   }
-}
 
+  if (ec) {
+    std::cerr << "ERROR: unable to read output directory: " << path
+               << ": " << ec.message() << "\n";
+    exit(1);
+  }
+}
 
-llvm::sys::Path KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
+std::string KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
 {
-  llvm::sys::Path toolRoot = llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr);
-  toolRoot.eraseComponent(); // Strip off executable so we have a directory path
+  SmallString<128> toolRoot(
+      #if LLVM_VERSION_CODE >= LLVM_VERSION(3,4)
+      llvm::sys::fs::getMainExecutable(argv0, MainExecAddr)
+      #else
+      llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr).str()
+      #endif
+      );
 
-  llvm::sys::Path libDir;
+  // Strip off executable so we have a directory path
+  llvm::sys::path::remove_filename(toolRoot);
+
+  SmallString<128> libDir;
 
   if ( strcmp(toolRoot.c_str(), KLEE_INSTALL_BIN_DIR ) == 0)
   {
     DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
                     "Using installed KLEE library runtime: ");
-    libDir = llvm::sys::Path( KLEE_INSTALL_LIB_DIR );
+    libDir = KLEE_INSTALL_LIB_DIR ;
   }
   else
   {
     DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
                     "Using build directory KLEE library runtime :");
-    libDir = llvm::sys::Path(KLEE_DIR "/" RUNTIME_CONFIGURATION "/lib");
+    libDir = KLEE_DIR;
+    llvm::sys::path::append(libDir,RUNTIME_CONFIGURATION);
+    llvm::sys::path::append(libDir,"lib");
   }
 
   DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
                   libDir.c_str() << "\n");
-  return libDir;
+  return libDir.str();
 }
 
 //===----------------------------------------------------------------------===//
@@ -1031,11 +1022,10 @@ static void replaceOrRenameFunction(llvm::Module *module,
     }
   }
 }
-
-static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) {
+static llvm::Module *linkWithUclibc(llvm::Module *mainModule, StringRef libDir) {
   // Ensure that klee-uclibc exists
-  llvm::sys::Path uclibcBCA(libDir);
-  uclibcBCA.appendComponent(KLEE_UCLIBC_BCA_NAME);
+  SmallString<128> uclibcBCA(libDir);
+  llvm::sys::path::append(uclibcBCA, KLEE_UCLIBC_BCA_NAME);
 
   bool uclibcExists=false;
   llvm::sys::fs::exists(uclibcBCA.c_str(), uclibcExists);
@@ -1261,7 +1251,7 @@ int main(int argc, char **argv, char **envp) {
       return r;
   }
 
-  llvm::sys::Path LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0], 
+  std::string LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0],
                               reinterpret_cast<void*>(main));
   Interpreter::ModuleOptions Opts(LibraryDir.c_str(),
                                   /*Optimize=*/OptimizeModule, 
@@ -1274,11 +1264,11 @@ int main(int argc, char **argv, char **envp) {
 
   case KleeLibc: {
     // FIXME: Find a reasonable solution for this.
-    llvm::sys::Path Path(Opts.LibraryDir);
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
-    Path.appendComponent("klee-libc.bc");
+    SmallString<128> Path(Opts.LibraryDir);
+#if LLVM_VERSION_CODE >= LLVM_VERSION(3,3)
+    llvm::sys::path::append(Path, "klee-libc.bc");
 #else
-    Path.appendComponent("libklee-libc.bca");
+    llvm::sys::path::append(Path, "libklee-libc.bca");
 #endif
     mainModule = klee::linkWithLibrary(mainModule, Path.c_str());
     assert(mainModule && "unable to link with klee-libc");
@@ -1291,8 +1281,8 @@ int main(int argc, char **argv, char **envp) {
   }
 
   if (WithPOSIXRuntime) {
-    llvm::sys::Path Path(Opts.LibraryDir);
-    Path.appendComponent("libkleeRuntimePOSIX.bca");
+    SmallString<128> Path(Opts.LibraryDir);
+    llvm::sys::path::append(Path, "libkleeRuntimePOSIX.bca");
     klee_message("NOTE: Using model: %s", Path.c_str());
     mainModule = klee::linkWithLibrary(mainModule, Path.c_str());
     assert(mainModule && "unable to link with simple model");