From 347795c5d2dbc2815d395e60a08ad3debca68102 Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Fri, 7 Feb 2014 21:04:22 +0100 Subject: Use SmallString and llvm::sys::path/fs API of LLVM 3.4 because Old Path API was removed --- lib/Core/StatsTracker.cpp | 10 ++- lib/Module/KModule.cpp | 6 ++ tools/klee/main.cpp | 161 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 173 insertions(+), 4 deletions(-) diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp index 8161a52c..fde2eae8 100644 --- a/lib/Core/StatsTracker.cpp +++ b/lib/Core/StatsTracker.cpp @@ -55,6 +55,7 @@ #include #include +#include using namespace klee; using namespace llvm; @@ -182,12 +183,13 @@ StatsTracker::StatsTracker(Executor &_executor, std::string _objectFilename, updateMinDistToUncovered(_updateMinDistToUncovered) { KModule *km = executor.kmodule; - sys::Path module(objectFilename); #if LLVM_VERSION_CODE < LLVM_VERSION(3, 1) if (!sys::Path(objectFilename).isAbsolute()) { #else if (!sys::path::is_absolute(objectFilename)) { #endif + +#if LLVM_VERSION_CODE < LLVM_VERSION(3,4) sys::Path current = sys::Path::GetCurrentDirectory(); current.appendComponent(objectFilename); #if LLVM_VERSION_CODE < LLVM_VERSION(3, 1) @@ -196,6 +198,12 @@ StatsTracker::StatsTracker(Executor &_executor, std::string _objectFilename, if (sys::fs::exists(current.c_str())) #endif objectFilename = current.c_str(); +#else + SmallString<128> current(objectFilename); + if(sys::fs::make_absolute(current) && sys::fs::exists(current.str())) + objectFilename = current.c_str(); +#endif + } if (OutputIStats) diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp index 2ecb14b8..5f03728b 100644 --- a/lib/Module/KModule.cpp +++ b/lib/Module/KModule.cpp @@ -375,6 +375,11 @@ void KModule::prepare(const Interpreter::ModuleOptions &opts, // FIXME: Find a way that we can test programs without requiring // this to be linked in, it makes low level debugging much more // annoying. +#if LLVM_VERSION_CODE >= LLVM_VERSION(3,4) + SmallString<128> LibPath(opts.LibraryDir); + llvm::sys::path::append(LibPath, "kleeRuntimeIntrinsic.bc"); + module = linkWithLibrary(module, LibPath.str()); +#else llvm::sys::Path path(opts.LibraryDir); #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) path.appendComponent("kleeRuntimeIntrinsic.bc"); @@ -382,6 +387,7 @@ void KModule::prepare(const Interpreter::ModuleOptions &opts, path.appendComponent("libkleeRuntimeIntrinsic.bca"); #endif module = linkWithLibrary(module, path.c_str()); +#endif // Add internal functions which are not used to check if instructions // have been already visited diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp index 1fe28270..e1219747 100644 --- a/tools/klee/main.cpp +++ b/tools/klee/main.cpp @@ -222,7 +222,11 @@ private: TreeStreamWriter *m_pathWriter, *m_symPathWriter; std::ostream *m_infoFile; +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + SmallString<128> m_outputDirectory; +#else sys::Path m_outputDirectory; +#endif unsigned m_testIndex; // number of tests written so far unsigned m_pathsExplored; // number of paths explored so far @@ -261,7 +265,11 @@ public: static void getOutFiles(std::string path, std::vector &results); +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + static std::string getRunTimeLibraryPath(const char* argv0, void *MainExecAddr); +#else static llvm::sys::Path getRunTimeLibraryPath(const char* argv0, void *MainExecAddr); +#endif }; KleeHandler::KleeHandler(int argc, char **argv) @@ -275,7 +283,72 @@ KleeHandler::KleeHandler(int argc, char **argv) m_argc(argc), m_argv(argv) { +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) if (OutputDir=="") { + + SmallString<128> directory(InputFile); + llvm::sys::path::remove_filename(directory); + + std::string dirname_str; + llvm::raw_string_ostream dirname(dirname_str); + + for (int i = 0; i< INT_MAX ; i++) { + dirname << "klee-out-"; + dirname << i; + dirname.flush(); + + m_outputDirectory = directory; // Copy + llvm::sys::path::append(m_outputDirectory,dirname_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.c_str()); + + if (!isDir) + { + break; // Found an available directory name + } + + // 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.c_str()); + + dirname_str.clear(); + m_outputDirectory.clear(); + } + + if (m_outputDirectory.empty()) + klee_error("Failed to find available output directory in %s", + dirname_str.c_str()); + + std::cerr << "KLEE: output directory = \"" << dirname.str() << "\"\n"; + + + SmallString<128> klee_last(directory); + llvm::sys::path::append(klee_last,"klee-last"); + + if ((unlink(klee_last.c_str()) < 0) && (errno != ENOENT)) + klee_error("cannot unlink klee-last: %s for %s", strerror(errno), + klee_last.c_str()); + + if (symlink(dirname_str.c_str(), klee_last.c_str()) < 0) + klee_error("cannot create klee-last symlink: %s", strerror(errno)); + + } else { + m_outputDirectory = OutputDir; + } + + if (!sys::path::is_absolute(m_outputDirectory.c_str())) { + SmallString<128> cwd(get_current_dir_name()); + sys::path::append(cwd, m_outputDirectory.str()); + m_outputDirectory = cwd; + } +#else + if (OutputDir=="") { + llvm::sys::Path directory(InputFile); std::stringstream dirname; directory.eraseComponent(); @@ -338,6 +411,7 @@ KleeHandler::KleeHandler(int argc, char **argv) m_outputDirectory = cwd; } +#endif if (mkdir(m_outputDirectory.c_str(), 0775) < 0) klee_error("cannot create directory \"%s\": %s", m_outputDirectory.c_str(), strerror(errno)); @@ -378,12 +452,18 @@ void KleeHandler::setInterpreter(Interpreter *i) { } std::string KleeHandler::getOutputFilename(const std::string &filename) { +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + SmallString<128> path = m_outputDirectory; + sys::path::append(path,filename); + return path.str(); +#else sys::Path path(m_outputDirectory); if(!path.appendComponent(filename)) { klee_error("cannot create path name for \"%s\"", filename.c_str()); } return path.str(); +#endif } std::ostream *KleeHandler::openOutputFile(const std::string &filename) { @@ -557,6 +637,22 @@ void KleeHandler::loadPathFile(std::string name, void KleeHandler::getOutFiles(std::string path, std::vector &results) { +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + 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); + } + } + + if (ec) { + std::cerr << "ERROR: unable to read output directory: " << path + << ": " << ec.message() << "\n"; + exit(1); + } +#else + llvm::sys::Path p(path); std::set contents; std::string error; @@ -572,11 +668,45 @@ void KleeHandler::getOutFiles(std::string path, results.push_back(f); } } -} +#endif +} -llvm::sys::Path KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr) +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) +std::string +#else +llvm::sys::Path +#endif +KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr) { +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + SmallString<128> toolRoot( + llvm::sys::fs::getMainExecutable(argv0, MainExecAddr)); + + // 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 = KLEE_INSTALL_LIB_DIR ; + } + else + { + DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() << + "Using build directory KLEE library runtime :"); + 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.str(); +#else llvm::sys::Path toolRoot = llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr); toolRoot.eraseComponent(); // Strip off executable so we have a directory path @@ -598,6 +728,7 @@ llvm::sys::Path KleeHandler::getRunTimeLibraryPath(const char* argv0, void* Main DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() << libDir.c_str() << "\n"); return libDir; +#endif } //===----------------------------------------------------------------------===// @@ -1031,11 +1162,20 @@ static void replaceOrRenameFunction(llvm::Module *module, } } } - +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) +static llvm::Module *linkWithUclibc(llvm::Module *mainModule, StringRef libDir) { +#else static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) { +#endif + // Ensure that klee-uclibc exists +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + SmallString<128> uclibcBCA(libDir); + llvm::sys::path::append(uclibcBCA, KLEE_UCLIBC_BCA_NAME); +#else llvm::sys::Path uclibcBCA(libDir); uclibcBCA.appendComponent(KLEE_UCLIBC_BCA_NAME); +#endif bool uclibcExists=false; llvm::sys::fs::exists(uclibcBCA.c_str(), uclibcExists); @@ -1261,8 +1401,13 @@ int main(int argc, char **argv, char **envp) { return r; } +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + std::string LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0], + reinterpret_cast(main)); +#else llvm::sys::Path LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0], reinterpret_cast(main)); +#endif Interpreter::ModuleOptions Opts(LibraryDir.c_str(), /*Optimize=*/OptimizeModule, /*CheckDivZero=*/CheckDivZero, @@ -1274,11 +1419,16 @@ int main(int argc, char **argv, char **envp) { case KleeLibc: { // FIXME: Find a reasonable solution for this. +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + SmallString<128> Path(Opts.LibraryDir); + llvm::sys::path::append(Path, "klee-libc.bc"); +#else llvm::sys::Path Path(Opts.LibraryDir); #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) Path.appendComponent("klee-libc.bc"); #else Path.appendComponent("libklee-libc.bca"); +#endif #endif mainModule = klee::linkWithLibrary(mainModule, Path.c_str()); assert(mainModule && "unable to link with klee-libc"); @@ -1291,8 +1441,13 @@ int main(int argc, char **argv, char **envp) { } if (WithPOSIXRuntime) { +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) + SmallString<128> Path(Opts.LibraryDir); + llvm::sys::path::append(Path, "libkleeRuntimePOSIX.bca"); +#else llvm::sys::Path Path(Opts.LibraryDir); Path.appendComponent("libkleeRuntimePOSIX.bca"); +#endif klee_message("NOTE: Using model: %s", Path.c_str()); mainModule = klee::linkWithLibrary(mainModule, Path.c_str()); assert(mainModule && "unable to link with simple model"); -- cgit 1.4.1 From 0ca3661a57c4b5a091b626455f69d6d087c7c6dc Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Fri, 7 Feb 2014 21:19:36 +0100 Subject: Update to new lit configuration to support changes in LLVM3.4 --- test/lit.cfg | 19 +++++++++++++++++-- test/lit.site.cfg.in | 9 ++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 23696138..3d00da53 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -3,6 +3,15 @@ # Configuration file for the 'lit' test runner. import os +import sys +import re +import platform + +try: + import lit.util + import lit.formats +except ImportError: + pass # name: The name of this test suite. config.name = 'KLEE' @@ -66,8 +75,14 @@ for name in subs: # Get KLEE and Kleaver specific parameters passed on llvm-lit cmd line # e.g. llvm-lit --param klee_opts=--help -klee_extra_params = lit.params.get('klee_opts',"") -kleaver_extra_params = lit.params.get('kleaver_opts',"") +try: + lit.params +except AttributeError: + klee_extra_params = lit_config.params.get('klee_opts',"") + kleaver_extra_params = lit_config.params.get('kleaver_opts',"") +else: + klee_extra_params = lit.params.get('klee_opts',"") + kleaver_extra_params = lit.params.get('kleaver_opts',"") if len(klee_extra_params) != 0: print("Passing extra KLEE command line args: {0}".format(klee_extra_params)) diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in index 6fc3d49c..14ba94e6 100644 --- a/test/lit.site.cfg.in +++ b/test/lit.site.cfg.in @@ -22,4 +22,11 @@ config.have_selinux = True if @HAVE_SELINUX@ == 1 else False config.target_triple = "@TARGET_TRIPLE@" # Let the main config do the real work. -lit.load_config(config, "@KLEE_SOURCE_DIR@/test/lit.cfg") +try: + lit +except NameError: + # Use lit_config class + lit_config.load_config(config, "@KLEE_SOURCE_DIR@/test/lit.cfg") +else: + # Use old lit class + lit.load_config(config, "@KLEE_SOURCE_DIR@/test/lit.cfg") -- cgit 1.4.1 From 492d0af188b233cbc111255af28960ed54b412e7 Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Thu, 6 Feb 2014 11:39:07 +0100 Subject: Fix test case to support new llvm-lit --- test/regression/2007-08-08-free-zero.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/regression/2007-08-08-free-zero.c b/test/regression/2007-08-08-free-zero.c index 964889a1..935b04fd 100644 --- a/test/regression/2007-08-08-free-zero.c +++ b/test/regression/2007-08-08-free-zero.c @@ -1,6 +1,8 @@ // RUN: %llvmgcc %s -emit-llvm -O0 -c -o %t1.bc // RUN: %klee %t1.bc -// RUN: ls klee-last | not grep *.err +// RUN: ls %T/klee-last | not grep *.err + +#include int main() { free(0); -- cgit 1.4.1 From f2ce7b5b53d78c370b01f5f219df0ea0021c7bb2 Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Mon, 10 Feb 2014 08:56:56 +0100 Subject: Add missing include file for LLVM 3.4 --- lib/Module/ModuleUtil.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Module/ModuleUtil.cpp b/lib/Module/ModuleUtil.cpp index 58096de4..4f65d0e7 100644 --- a/lib/Module/ModuleUtil.cpp +++ b/lib/Module/ModuleUtil.cpp @@ -13,6 +13,10 @@ #include "../Core/Common.h" #include "../Core/SpecialFunctionHandler.h" +#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) +#include "llvm/IR/LLVMContext.h" +#endif + #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/IR/Function.h" -- cgit 1.4.1 From 4a9a908739d7d7c833e7985ea7465d95c0dd0b82 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Wed, 2 Apr 2014 21:44:51 +0100 Subject: Tidy up code by using LLVM's V2 path API only and removing uses of old V1 path API. LLVM2.9 supports LLVM's V2 path API. Because that is the minimum version we support we should just use this API everywhere so we reduce the number of #if LLVM_VERSION_CODE macros and duplicated code. --- lib/Core/StatsTracker.cpp | 28 ++------ lib/Module/KModule.cpp | 17 ++--- tools/klee/main.cpp | 173 +++------------------------------------------- 3 files changed, 25 insertions(+), 193 deletions(-) diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp index fde2eae8..1bb9885a 100644 --- a/lib/Core/StatsTracker.cpp +++ b/lib/Core/StatsTracker.cpp @@ -49,9 +49,7 @@ #include "llvm/Support/raw_os_ostream.h" #include "llvm/Support/Process.h" #include "llvm/Support/Path.h" -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 1) #include "llvm/Support/FileSystem.h" -#endif #include #include @@ -183,27 +181,15 @@ StatsTracker::StatsTracker(Executor &_executor, std::string _objectFilename, updateMinDistToUncovered(_updateMinDistToUncovered) { KModule *km = executor.kmodule; -#if LLVM_VERSION_CODE < LLVM_VERSION(3, 1) - if (!sys::Path(objectFilename).isAbsolute()) { -#else if (!sys::path::is_absolute(objectFilename)) { -#endif - -#if LLVM_VERSION_CODE < LLVM_VERSION(3,4) - sys::Path current = sys::Path::GetCurrentDirectory(); - current.appendComponent(objectFilename); -#if LLVM_VERSION_CODE < LLVM_VERSION(3, 1) - if (current.exists()) -#else - if (sys::fs::exists(current.c_str())) -#endif - objectFilename = current.c_str(); -#else SmallString<128> current(objectFilename); - if(sys::fs::make_absolute(current) && sys::fs::exists(current.str())) - objectFilename = current.c_str(); -#endif - + if(sys::fs::make_absolute(current)) { + bool exists = false; + error_code ec = sys::fs::exists(current.str(), exists); + if (ec == errc::success && exists) { + objectFilename = current.c_str(); + } + } } if (OutputIStats) diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp index 5f03728b..e06e722a 100644 --- a/lib/Module/KModule.cpp +++ b/lib/Module/KModule.cpp @@ -375,19 +375,16 @@ void KModule::prepare(const Interpreter::ModuleOptions &opts, // FIXME: Find a way that we can test programs without requiring // this to be linked in, it makes low level debugging much more // annoying. -#if LLVM_VERSION_CODE >= LLVM_VERSION(3,4) + SmallString<128> LibPath(opts.LibraryDir); - llvm::sys::path::append(LibPath, "kleeRuntimeIntrinsic.bc"); - module = linkWithLibrary(module, LibPath.str()); -#else - llvm::sys::Path path(opts.LibraryDir); -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) - path.appendComponent("kleeRuntimeIntrinsic.bc"); + llvm::sys::path::append(LibPath, +#if LLVM_VERSION_CODE >= LLVM_VERSION(3,3) + "kleeRuntimeIntrinsic.bc" #else - path.appendComponent("libkleeRuntimeIntrinsic.bca"); -#endif - module = linkWithLibrary(module, path.c_str()); + "libkleeRuntimeIntrinsic.bca" #endif + ); + module = linkWithLibrary(module, LibPath.str()); // Add internal functions which are not used to check if instructions // have been already visited diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp index e1219747..ff90a644 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,11 +223,8 @@ private: TreeStreamWriter *m_pathWriter, *m_symPathWriter; std::ostream *m_infoFile; -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) SmallString<128> m_outputDirectory; -#else - sys::Path m_outputDirectory; -#endif + unsigned m_testIndex; // number of tests written so far unsigned m_pathsExplored; // number of paths explored so far @@ -265,11 +263,7 @@ public: static void getOutFiles(std::string path, std::vector &results); -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) static std::string getRunTimeLibraryPath(const char* argv0, void *MainExecAddr); -#else - static llvm::sys::Path getRunTimeLibraryPath(const char* argv0, void *MainExecAddr); -#endif }; KleeHandler::KleeHandler(int argc, char **argv) @@ -283,7 +277,6 @@ KleeHandler::KleeHandler(int argc, char **argv) m_argc(argc), m_argv(argv) { -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) if (OutputDir=="") { SmallString<128> directory(InputFile); @@ -346,72 +339,6 @@ KleeHandler::KleeHandler(int argc, char **argv) sys::path::append(cwd, m_outputDirectory.str()); m_outputDirectory = cwd; } -#else - 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; - - 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 - } - - // 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()); - - dirname.str(""); // Clear - m_outputDirectory.clear(); - } - - if (m_outputDirectory.empty()) - klee_error("Failed to find available output directory in %s", dirname.str().c_str()); - - std::cerr << "KLEE: output directory = \"" << dirname.str() << "\"\n"; - - 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)) - 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)); - - } 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"); - - m_outputDirectory = cwd; - } -#endif if (mkdir(m_outputDirectory.c_str(), 0775) < 0) klee_error("cannot create directory \"%s\": %s", m_outputDirectory.c_str(), strerror(errno)); @@ -452,18 +379,9 @@ void KleeHandler::setInterpreter(Interpreter *i) { } std::string KleeHandler::getOutputFilename(const std::string &filename) { -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) SmallString<128> path = m_outputDirectory; sys::path::append(path,filename); return path.str(); -#else - sys::Path path(m_outputDirectory); - if(!path.appendComponent(filename)) { - klee_error("cannot create path name for \"%s\"", filename.c_str()); - } - - return path.str(); -#endif } std::ostream *KleeHandler::openOutputFile(const std::string &filename) { @@ -637,7 +555,6 @@ void KleeHandler::loadPathFile(std::string name, void KleeHandler::getOutFiles(std::string path, std::vector &results) { -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) error_code ec; for (llvm::sys::fs::directory_iterator i(path,ec),e; i!=e && !ec; i.increment(ec)){ std::string f = (*i).path(); @@ -651,37 +568,17 @@ void KleeHandler::getOutFiles(std::string path, << ": " << ec.message() << "\n"; exit(1); } -#else - - llvm::sys::Path p(path); - std::set 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::iterator it = contents.begin(), - ie = contents.end(); it != ie; ++it) { - std::string f = it->str(); - if (f.substr(f.size()-6,f.size()) == ".ktest") { - results.push_back(f); - } - } -#endif - } -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) -std::string -#else -llvm::sys::Path -#endif -KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr) +std::string KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr) { -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) SmallString<128> toolRoot( - llvm::sys::fs::getMainExecutable(argv0, MainExecAddr)); + #if LLVM_VERSION_CODE >= LLVM_VERSION(3,4) + llvm::sys::fs::getMainExecutable(argv0, MainExecAddr) + #else + llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr).str() + #endif + ); // Strip off executable so we have a directory path llvm::sys::path::remove_filename(toolRoot); @@ -706,29 +603,6 @@ KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr) DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() << libDir.c_str() << "\n"); return libDir.str(); -#else - llvm::sys::Path toolRoot = llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr); - toolRoot.eraseComponent(); // Strip off executable so we have a directory path - - llvm::sys::Path 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 ); - } - else - { - DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() << - "Using build directory KLEE library runtime :"); - libDir = llvm::sys::Path(KLEE_DIR "/" RUNTIME_CONFIGURATION "/lib"); - } - - DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() << - libDir.c_str() << "\n"); - return libDir; -#endif } //===----------------------------------------------------------------------===// @@ -1162,20 +1036,10 @@ static void replaceOrRenameFunction(llvm::Module *module, } } } -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) static llvm::Module *linkWithUclibc(llvm::Module *mainModule, StringRef libDir) { -#else -static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) { -#endif - // Ensure that klee-uclibc exists -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) SmallString<128> uclibcBCA(libDir); llvm::sys::path::append(uclibcBCA, KLEE_UCLIBC_BCA_NAME); -#else - llvm::sys::Path uclibcBCA(libDir); - uclibcBCA.appendComponent(KLEE_UCLIBC_BCA_NAME); -#endif bool uclibcExists=false; llvm::sys::fs::exists(uclibcBCA.c_str(), uclibcExists); @@ -1401,13 +1265,8 @@ int main(int argc, char **argv, char **envp) { return r; } -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) std::string LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0], reinterpret_cast(main)); -#else - llvm::sys::Path LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0], - reinterpret_cast(main)); -#endif Interpreter::ModuleOptions Opts(LibraryDir.c_str(), /*Optimize=*/OptimizeModule, /*CheckDivZero=*/CheckDivZero, @@ -1419,16 +1278,11 @@ int main(int argc, char **argv, char **envp) { case KleeLibc: { // FIXME: Find a reasonable solution for this. -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) SmallString<128> Path(Opts.LibraryDir); +#if LLVM_VERSION_CODE >= LLVM_VERSION(3,3) llvm::sys::path::append(Path, "klee-libc.bc"); #else - llvm::sys::Path Path(Opts.LibraryDir); -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) - Path.appendComponent("klee-libc.bc"); -#else - Path.appendComponent("libklee-libc.bca"); -#endif + llvm::sys::path::append(Path, "libklee-libc.bca"); #endif mainModule = klee::linkWithLibrary(mainModule, Path.c_str()); assert(mainModule && "unable to link with klee-libc"); @@ -1441,13 +1295,8 @@ int main(int argc, char **argv, char **envp) { } if (WithPOSIXRuntime) { -#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) SmallString<128> Path(Opts.LibraryDir); llvm::sys::path::append(Path, "libkleeRuntimePOSIX.bca"); -#else - llvm::sys::Path Path(Opts.LibraryDir); - Path.appendComponent("libkleeRuntimePOSIX.bca"); -#endif klee_message("NOTE: Using model: %s", Path.c_str()); mainModule = klee::linkWithLibrary(mainModule, Path.c_str()); assert(mainModule && "unable to link with simple model"); -- cgit 1.4.1 From f8f8da3c374ac926e8d0eca4cca83d8a8a095b26 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 3 Apr 2014 15:30:52 +0100 Subject: Do not add SimplifyLibCallsPass for LLVM 3.4 and newer because it has been removed. From the LLVM 3.4 release notes: " The library call simplification pass has been removed. Its functionality has been integrated into the instruction combiner and function attribute marking passes. " --- lib/Module/Optimize.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Module/Optimize.cpp b/lib/Module/Optimize.cpp index 41a106f1..9c200bc8 100644 --- a/lib/Module/Optimize.cpp +++ b/lib/Module/Optimize.cpp @@ -124,7 +124,9 @@ static void AddStandardCompilePasses(PassManager &PM) { addPass(PM, createFunctionInliningPass()); // Inline small functions addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args +#if LLVM_VERSION_CODE < LLVM_VERSION(3, 4) addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations +#endif addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl. addPass(PM, createJumpThreadingPass()); // Thread jumps. addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs -- cgit 1.4.1 From 60ff94b3ef5074d9da936d9ef59274bc11b43e07 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 3 Apr 2014 16:11:53 +0100 Subject: Replace sys::Process::GetCurrentUserId() with getuid() because it has been removed in LLVM3.4 --- lib/Core/StatsTracker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp index 1bb9885a..bbea2a98 100644 --- a/lib/Core/StatsTracker.cpp +++ b/lib/Core/StatsTracker.cpp @@ -440,7 +440,7 @@ void StatsTracker::writeIStats() { of << "version: 1\n"; of << "creator: klee\n"; - of << "pid: " << sys::Process::GetCurrentUserId() << "\n"; + of << "pid: " << getuid() << "\n"; of << "cmd: " << m->getModuleIdentifier() << "\n\n"; of << "\n"; -- cgit 1.4.1 From 3ca2809ec59d1fea913c3df23eb3f9539b8d87cc Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 3 Apr 2014 16:34:49 +0100 Subject: Fix mistake in StatsTracker. It should be reporting process ID but instead it was reporting real user ID of the process. --- lib/Core/StatsTracker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp index bbea2a98..4709a5bc 100644 --- a/lib/Core/StatsTracker.cpp +++ b/lib/Core/StatsTracker.cpp @@ -440,7 +440,7 @@ void StatsTracker::writeIStats() { of << "version: 1\n"; of << "creator: klee\n"; - of << "pid: " << getuid() << "\n"; + of << "pid: " << getpid() << "\n"; of << "cmd: " << m->getModuleIdentifier() << "\n\n"; of << "\n"; -- cgit 1.4.1 From 8382ea75c68696b76cafc03f00235f988277f9b0 Mon Sep 17 00:00:00 2001 From: Frank Busse Date: Fri, 4 Apr 2014 15:05:42 +0200 Subject: fix TOCTOU and simplify output directory creation --- tools/klee/main.cpp | 94 +++++++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 54 deletions(-) diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp index ff90a644..9af27ee3 100644 --- a/tools/klee/main.cpp +++ b/tools/klee/main.cpp @@ -277,80 +277,66 @@ KleeHandler::KleeHandler(int argc, char **argv) m_argc(argc), m_argv(argv) { - if (OutputDir=="") { + // create output directory (OutputDir or "klee-out-") + bool dir_given = OutputDir != ""; + SmallString<128> directory(dir_given ? OutputDir : InputFile); - SmallString<128> directory(InputFile); - llvm::sys::path::remove_filename(directory); - - std::string dirname_str; - llvm::raw_string_ostream dirname(dirname_str); - - for (int i = 0; i< INT_MAX ; i++) { - dirname << "klee-out-"; - dirname << i; - dirname.flush(); - - m_outputDirectory = directory; // Copy - llvm::sys::path::append(m_outputDirectory,dirname_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.c_str()); - - if (!isDir) - { - break; // Found an available directory name - } - - // 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.c_str()); - - dirname_str.clear(); - m_outputDirectory.clear(); - } + 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()); - if (m_outputDirectory.empty()) - klee_error("Failed to find available output directory in %s", - dirname_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)); - std::cerr << "KLEE: output directory = \"" << dirname.str() << "\"\n"; + m_outputDirectory = directory; + } else { + // "klee-out-" + SmallString<128> d; + raw_svector_ostream ds(d); + int i = 0; + for (; i <= INT_MAX; ++i) { + d.clear(); ds << directory << "klee-out-" << i; ds.flush(); + // create directory and try to link klee-last + if (mkdir(d.c_str(), 0775) == 0) { + m_outputDirectory = d; - SmallString<128> klee_last(directory); - llvm::sys::path::append(klee_last,"klee-last"); + SmallString<128> klee_last(directory); + llvm::sys::path::append(klee_last, "klee-last"); - if ((unlink(klee_last.c_str()) < 0) && (errno != ENOENT)) - klee_error("cannot unlink klee-last: %s for %s", strerror(errno), - klee_last.c_str()); + if (((unlink(klee_last.c_str()) < 0) && (errno != ENOENT)) || + symlink(m_outputDirectory.c_str(), klee_last.c_str()) < 0) { - 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 { - m_outputDirectory = OutputDir; - } + break; + } - if (!sys::path::is_absolute(m_outputDirectory.c_str())) { - SmallString<128> cwd(get_current_dir_name()); - sys::path::append(cwd, m_outputDirectory.str()); - 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.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"); } -- cgit 1.4.1 From 60ebbf13d6b6d3522008a7b0553e5bfec1864c71 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 10 Apr 2014 12:28:27 +0100 Subject: Fix compilation under LLVM2.9. SmallString in this old version does not have the equals() method. --- tools/klee/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp index 9af27ee3..f521e299 100644 --- a/tools/klee/main.cpp +++ b/tools/klee/main.cpp @@ -320,7 +320,7 @@ KleeHandler::KleeHandler(int argc, char **argv) if (errno != EEXIST) klee_error("cannot create \"%s\": %s", m_outputDirectory.c_str(), strerror(errno)); } - if (i == INT_MAX && m_outputDirectory.equals("")) + if (i == INT_MAX && m_outputDirectory.str().equals("")) klee_error("cannot create output directory: index out of range"); } -- cgit 1.4.1 From 78e06cb737e3e54e6c7035822f39961679e7b367 Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Tue, 15 Apr 2014 14:36:59 +0200 Subject: Fix handling of path name creation. If directory has no trailing slash, the slash is not addedd if concatenated --- tools/klee/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp index f521e299..a2268c83 100644 --- a/tools/klee/main.cpp +++ b/tools/klee/main.cpp @@ -294,11 +294,11 @@ KleeHandler::KleeHandler(int argc, char **argv) m_outputDirectory = directory; } else { // "klee-out-" - SmallString<128> d; - raw_svector_ostream ds(d); int i = 0; for (; i <= INT_MAX; ++i) { - d.clear(); ds << directory << "klee-out-" << i; ds.flush(); + SmallString<128> d(directory); + llvm::sys::path::append(d, "klee-out-"); + raw_svector_ostream ds(d); ds << i; ds.flush(); // create directory and try to link klee-last if (mkdir(d.c_str(), 0775) == 0) { -- cgit 1.4.1