Discussion:
make KLEE compile with LLVM 3.5-svn (almost)
Martin Pelikan
2014-02-16 14:00:59 UTC
Permalink
Hi,

I can't get LLVM to produce libLLVM-3.5svn.so, which is why I haven't tested
this diff. But I've had similar changes on my machines for a while (Gentoo
and OpenBSD) and most of them are inevitable as the interfaces have already
been removed from LLVM.

It probably won't get accepted as a whole, but might get you the idea of
what is required to work against a recent LLVM version.
--
Martin Pelikan


diff --git a/configure b/configure
index 55497c3..9820390 100755
--- a/configure
+++ b/configure
@@ -2658,28 +2658,31 @@ else
fi


-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking llvm build mode" >&5
-$as_echo_n "checking llvm build mode... " >&6; }
+if false; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking llvm build mode" >&5
+ $as_echo_n "checking llvm build mode... " >&6; }
+
+ if test X${with_llvm_build_mode} = Xcheck ; then
+ llvm_configs="`ls -1 $llvm_obj/*/bin/llvm-config 2>/dev/null | head -n 1`"
+ if test -x "$llvm_configs" ; then
+ llvm_build_mode="`$llvm_configs --build-mode`"
+ else
+ as_fn_error $? "Could not autodetect build mode" "$LINENO" 5
+ fi
+ else
+ llvm_configs="`echo $llvm_obj/*/bin/llvm-config`"
+ if test -x "$llvm_obj/$with_llvm_build_mode/bin/llvm-config" ; then
+ llvm_build_mode=$with_llvm_build_mode
+ else
+ as_fn_error $? "Invalid build mode: $llvm_build_mode" "$LINENO" 5
+ fi
+ fi

-if test X${with_llvm_build_mode} = Xcheck ; then
- llvm_configs="`ls -1 $llvm_obj/*/bin/llvm-config 2>/dev/null | head -n 1`"
- if test -x "$llvm_configs" ; then
- llvm_build_mode="`$llvm_configs --build-mode`"
- else
- as_fn_error $? "Could not autodetect build mode" "$LINENO" 5
- fi
-else
- llvm_configs="`echo $llvm_obj/*/bin/llvm-config`"
- if test -x "$llvm_obj/$with_llvm_build_mode/bin/llvm-config" ; then
- llvm_build_mode=$with_llvm_build_mode
- else
- as_fn_error $? "Invalid build mode: $llvm_build_mode" "$LINENO" 5
- fi
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: $llvm_build_mode" >&5
+ $as_echo "$llvm_build_mode" >&6; }
+ LLVM_BUILD_MODE=$llvm_build_mode
fi
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $llvm_build_mode" >&5
-$as_echo "$llvm_build_mode" >&6; }
-LLVM_BUILD_MODE=$llvm_build_mode
+LLVM_BUILD_MODE=



diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp
index 8161a52..0eafbc2 100644
--- a/lib/Core/StatsTracker.cpp
+++ b/lib/Core/StatsTracker.cpp
@@ -55,6 +55,7 @@

#include <iostream>
#include <fstream>
+#include <unistd.h>

using namespace klee;
using namespace llvm;
@@ -182,14 +183,16 @@ StatsTracker::StatsTracker(Executor &_executor, std::string _objectFilename,
updateMinDistToUncovered(_updateMinDistToUncovered) {
KModule *km = executor.kmodule;

- sys::Path module(objectFilename);
+ SmallString<128> module(objectFilename);
#if LLVM_VERSION_CODE < LLVM_VERSION(3, 1)
if (!sys::Path(objectFilename).isAbsolute()) {
#else
if (!sys::path::is_absolute(objectFilename)) {
#endif
- sys::Path current = sys::Path::GetCurrentDirectory();
- current.appendComponent(objectFilename);
+ SmallString<128> current;
+ if (sys::fs::current_path(current))
+ klee_error("can't get current path");
+ sys::path::append(current, objectFilename);
#if LLVM_VERSION_CODE < LLVM_VERSION(3, 1)
if (current.exists())
#else
@@ -446,7 +449,7 @@ void StatsTracker::writeIStats() {

of << "version: 1\n";
of << "creator: klee\n";
- of << "pid: " << sys::Process::GetCurrentUserId() << "\n";
+ of << "pid: " << getpid() << "\n";
of << "cmd: " << m->getModuleIdentifier() << "\n\n";
of << "\n";

diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp
index 2ecb14b..fc49849 100644
--- a/lib/Module/KModule.cpp
+++ b/lib/Module/KModule.cpp
@@ -375,11 +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.
- llvm::sys::Path path(opts.LibraryDir);
+ SmallString<128> path(opts.LibraryDir);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
- path.appendComponent("kleeRuntimeIntrinsic.bc");
+ llvm::sys::path::append(path, "kleeRuntimeIntrinsic.bc");
#else
- path.appendComponent("libkleeRuntimeIntrinsic.bca");
+ llvm::sys::path::append(path, "libkleeRuntimeIntrinsic.bca");
#endif
module = linkWithLibrary(module, path.c_str());

diff --git a/lib/Module/ModuleUtil.cpp b/lib/Module/ModuleUtil.cpp
index 58096de..6a598b0 100644
--- a/lib/Module/ModuleUtil.cpp
+++ b/lib/Module/ModuleUtil.cpp
@@ -19,6 +19,7 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IRReader/IRReader.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
@@ -82,7 +83,7 @@ GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
if (I->isDeclaration())
UndefinedSymbols.insert(I->getName());
else if (!I->hasLocalLinkage()) {
- assert(!I->hasDLLImportLinkage()
+ assert(!I->hasDLLImportStorageClass()
&& "Found dllimported non-external symbol!");
DefinedSymbols.insert(I->getName());
}
@@ -94,7 +95,7 @@ GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
if (I->isDeclaration())
UndefinedSymbols.insert(I->getName());
else if (!I->hasLocalLinkage()) {
- assert(!I->hasDLLImportLinkage()
+ assert(!I->hasDLLImportStorageClass()
&& "Found dllimported non-external symbol!");
DefinedSymbols.insert(I->getName());
}
@@ -191,8 +192,8 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er

DEBUG_WITH_TYPE("klee_linker", dbgs() << "Loading modules\n");
// Load all bitcode files in to memory so we can examine their symbols
- for (object::Archive::child_iterator AI = archive->begin_children(),
- AE = archive->end_children(); AI != AE; ++AI)
+ for (object::Archive::child_iterator AI = archive->child_begin(),
+ AE = archive->child_end(); AI != AE; ++AI)
{

StringRef memberName;
@@ -215,7 +216,6 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er
// If we can't open as a binary object file its hopefully a bitcode file

OwningPtr<MemoryBuffer> buff; // Once this is destroyed will Module still be valid??
- Module *Result = 0;

if (error_code ec = AI->getMemoryBuffer(buff))
{
@@ -227,15 +227,15 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er
if (buff)
{
// FIXME: Maybe load bitcode file lazily? Then if we need to link, materialise the module
- Result = ParseBitcodeFile(buff.get(), getGlobalContext(), &errorMessage);
+ ErrorOr<Module *> bcfileOrErr = parseBitcodeFile(buff.get(), getGlobalContext());

- if(!Result)
+ if (error_code ec = bcfileOrErr.getError())
{
- SS << "Loading module failed : " << errorMessage << "\n";
+ SS << "Loading module failed : " << ec.message() << "\n";
SS.flush();
return false;
}
- archiveModules.push_back(Result);
+ archiveModules.push_back(bcfileOrErr.get());
}
else
{
@@ -349,17 +349,16 @@ DEBUG_WITH_TYPE("klee_linker", dbgs() << "Linking file " << libraryName << "\n")
libraryName.c_str());
}

- OwningPtr<MemoryBuffer> Buffer;
- if (error_code ec = MemoryBuffer::getFile(libraryName,Buffer)) {
+ ErrorOr<object::Binary *> binOrErr = object::createBinary(libraryName);
+ if (error_code ec = binOrErr.getError()) {
klee_error("Link with library %s failed: %s", libraryName.c_str(),
ec.message().c_str());
}

- sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
-
- LLVMContext &Context = getGlobalContext();
std::string ErrorMessage;
+ OwningPtr<object::Binary> binary(binOrErr.get());

+ #if 0
if (magic == sys::fs::file_magic::bitcode) {
Module *Result = 0;
Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
@@ -372,29 +371,18 @@ DEBUG_WITH_TYPE("klee_linker", dbgs() << "Linking file " << libraryName << "\n")

delete Result;

- } else if (magic == sys::fs::file_magic::archive) {
- OwningPtr<object::Binary> arch;
- if (error_code ec = object::createBinary(Buffer.take(), arch))
+ }
+ #endif
+ if (object::Archive *a = dyn_cast<object::Archive>(binary.get())) {
+ // Handle in helper
+ if (!linkBCA(a, module, ErrorMessage))
klee_error("Link with library %s failed: %s", libraryName.c_str(),
- ec.message().c_str());
-
- if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
- // Handle in helper
- if (!linkBCA(a, module, ErrorMessage))
- klee_error("Link with library %s failed: %s", libraryName.c_str(),
- ErrorMessage.c_str());
- }
- else {
- klee_error("Link with library %s failed: Cast to archive failed", libraryName.c_str());
- }
+ ErrorMessage.c_str());

- } else if (magic.is_object()) {
- OwningPtr<object::Binary> obj;
- if (object::ObjectFile *o = dyn_cast<object::ObjectFile>(obj.get())) {
- klee_warning("Link with library: Object file %s in archive %s found. "
- "Currently not supported.",
- o->getFileName().data(), libraryName.c_str());
- }
+ } else if (object::ObjectFile *o = dyn_cast<object::ObjectFile>(binary.get())) {
+ klee_warning("Link with library: Object file %s in archive %s found. "
+ "Currently not supported.",
+ o->getFileName().data(), libraryName.c_str());
} else {
klee_error("Link with library %s failed: Unrecognized file type.",
libraryName.c_str());
@@ -404,7 +392,7 @@ DEBUG_WITH_TYPE("klee_linker", dbgs() << "Linking file " << libraryName << "\n")
#else
Linker linker("klee", module, false);

- llvm::sys::Path libraryPath(libraryName);
+ SmallString<128> libraryPath(libraryName);
bool native = false;

if (linker.LinkInFile(libraryPath, native)) {
diff --git a/lib/Module/Optimize.cpp b/lib/Module/Optimize.cpp
index 41a106f..0ef1c16 100644
--- a/lib/Module/Optimize.cpp
+++ b/lib/Module/Optimize.cpp
@@ -124,7 +124,11 @@ static void AddStandardCompilePasses(PassManager &PM) {
addPass(PM, createFunctionInliningPass()); // Inline small functions
addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args

+#if defined(LLVM_3_1) || defined(LLVM_3_2) || defined(LLVM_3_3)
+ // Starting from 3.4 this functionality was moved to
+ // InstructionCombiningPass. See r184459 for details.
addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations
+#endif
addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl.
addPass(PM, createJumpThreadingPass()); // Thread jumps.
addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp
index f9698fd..74a3616 100644
--- a/tools/klee/main.cpp
+++ b/tools/klee/main.cpp
@@ -220,7 +220,7 @@ 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

@@ -259,7 +259,7 @@ public:
static void getOutFiles(std::string path,
std::vector<std::string> &results);

- static llvm::sys::Path getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
+ static SmallString<128> getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
};

KleeHandler::KleeHandler(int argc, char **argv)
@@ -274,25 +274,24 @@ KleeHandler::KleeHandler(int argc, char **argv)
m_argv(argv) {

if (OutputDir=="") {
- llvm::sys::Path directory(InputFile);
+ SmallString<128> directory(InputFile);
std::stringstream dirname;
- directory.eraseComponent();
+ llvm::sys::path::remove_filename(directory);

- if (directory.isEmpty())
- directory.set(".");
+ if (directory.empty())
+ directory = ".";

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());
+ m_outputDirectory = directory;
+ 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.str().c_str());
+ klee_error("Failed to check if \"%s\" exists.", m_outputDirectory.c_str());

if (!isDir)
{
@@ -303,7 +302,7 @@ KleeHandler::KleeHandler(int argc, char **argv)
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());
+ m_outputDirectory.c_str());

dirname.str(""); // Clear
m_outputDirectory.clear();
@@ -314,9 +313,8 @@ KleeHandler::KleeHandler(int argc, char **argv)

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");
+ 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", strerror(errno));
@@ -325,14 +323,14 @@ KleeHandler::KleeHandler(int argc, char **argv)
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());
+ m_outputDirectory = OutputDir;
}

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");
+ SmallString<128> cwd;
+ if (sys::fs::current_path(cwd))
+ klee_error("can't get current path");
+ llvm::sys::path::append(cwd, m_outputDirectory.c_str());

m_outputDirectory = cwd;
}
@@ -376,10 +374,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);
+ llvm::sys::path::append(path, filename);

return path.str();
}
@@ -555,42 +551,39 @@ 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)) {
+ llvm::error_code ec;
+ for (llvm::sys::fs::directory_iterator Entry(path, ec), End;
+ Entry != End && !ec; Entry.increment(ec)) {
+ std::string f = Entry->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
- << ": " << error << "\n";
+ << ": " << ec.message() << "\n";
exit(1);
}
- for (std::set<llvm::sys::Path>::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);
- }
- }
}


-llvm::sys::Path KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
+SmallString<128> 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(llvm::sys::fs::getMainExecutable(argv0, MainExecAddr));
+ llvm::sys::path::remove_filename(toolRoot);

- llvm::sys::Path libDir;
+ 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 = SmallString<128>( 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 = SmallString<128>(KLEE_DIR "/" RUNTIME_CONFIGURATION "/lib");
}

DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
@@ -1007,7 +1000,7 @@ static char *format_tdiff(char *buf, long seconds)
}

#ifndef SUPPORT_KLEE_UCLIBC
-static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) {
+static llvm::Module *linkWithUclibc(llvm::Module *mainModule, SmallString<128> libDir) {
fprintf(stderr, "error: invalid libc, no uclibc support!\n");
exit(1);
return 0;
@@ -1030,9 +1023,9 @@ static void replaceOrRenameFunction(llvm::Module *module,
}
}

-static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) {
+static llvm::Module *linkWithUclibc(llvm::Module *mainModule, SmallString<128> libDir) {
// Ensure that klee-uclibc exists
- llvm::sys::Path uclibcBCA(libDir);
+ SmallString<128> uclibcBCA(libDir);
uclibcBCA.appendComponent(KLEE_UCLIBC_BCA_NAME);

bool uclibcExists=false;
@@ -1233,25 +1226,27 @@ int main(int argc, char **argv, char **envp) {
sys::SetInterruptFunction(interrupt_handle);

// Load the bytecode...
- std::string ErrorMsg;
- Module *mainModule = 0;
OwningPtr<MemoryBuffer> BufferPtr;
error_code ec=MemoryBuffer::getFileOrSTDIN(InputFile.c_str(), BufferPtr);
if (ec) {
klee_error("error loading program '%s': %s", InputFile.c_str(),
ec.message().c_str());
}
- mainModule = getLazyBitcodeModule(BufferPtr.get(), getGlobalContext(), &ErrorMsg);
+ ErrorOr<Module *> modOrErr = getLazyBitcodeModule(BufferPtr.get(), getGlobalContext());
+ if (!modOrErr) {
+ klee_error("couldn't getLazyBitcodeModule %s: %s", InputFile.c_str(),
+ ec.message().c_str());
+ }

+ Module *mainModule = modOrErr.get();
if (mainModule) {
- if (mainModule->MaterializeAllPermanently(&ErrorMsg)) {
+ if (error_code ec = mainModule->materializeAllPermanently()) {
+ klee_error("couldn't materializeAllPermanently %s: %s", InputFile.c_str(),
+ ec.message().c_str());
delete mainModule;
mainModule = 0;
}
}
- if (!mainModule)
- klee_error("error loading program '%s': %s", InputFile.c_str(),
- ErrorMsg.c_str());

if (WithPOSIXRuntime) {
int r = initEnv(mainModule);
@@ -1259,7 +1254,7 @@ int main(int argc, char **argv, char **envp) {
return r;
}

- llvm::sys::Path LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0],
+ SmallString<128> LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0],
reinterpret_cast<void*>(main));
Interpreter::ModuleOptions Opts(LibraryDir.c_str(),
/*Optimize=*/OptimizeModule,
@@ -1272,9 +1267,9 @@ int main(int argc, char **argv, char **envp) {

case KleeLibc: {
// FIXME: Find a reasonable solution for this.
- llvm::sys::Path Path(Opts.LibraryDir);
+ SmallString<128> Path(Opts.LibraryDir);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
- Path.appendComponent("klee-libc.bc");
+ llvm::sys::path::append(Path, "klee-libc.bc");
#else
Path.appendComponent("libklee-libc.bca");
#endif
@@ -1289,8 +1284,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");
Martin Nowack
2014-02-16 21:18:18 UTC
Permalink
Hi Martin,
Post by Martin Pelikan
I can't get LLVM to produce libLLVM-3.5svn.so, which is why I haven't tested
this diff. But I've had similar changes on my machines for a while (Gentoo
and OpenBSD) and most of them are inevitable as the interfaces have already
been removed from LLVM.
To produce the libLLVM-3.5svn.so file, please configure your compile your LLVM with `--enable-shared`

If you have a chance/time, could you please try the following branch for LLVM 3.4 [1]
and check what is needed to make it work for you on LLVM 3.5?

Thanks a lot in advance.

Anyway, thanks a lot for your patch. I’ll try to have a look at it.

Best wishes,
Martin


[1] https://github.com/MartinNowack/klee/tree/llvm_34
Post by Martin Pelikan
It probably won't get accepted as a whole, but might get you the idea of
what is required to work against a recent LLVM version.
--
Martin Pelikan
diff --git a/configure b/configure
index 55497c3..9820390 100755
--- a/configure
+++ b/configure
@@ -2658,28 +2658,31 @@ else
fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking llvm build mode" >&5
-$as_echo_n "checking llvm build mode... " >&6; }
+if false; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking llvm build mode" >&5
+ $as_echo_n "checking llvm build mode... " >&6; }
+
+ if test X${with_llvm_build_mode} = Xcheck ; then
+ llvm_configs="`ls -1 $llvm_obj/*/bin/llvm-config 2>/dev/null | head -n 1`"
+ if test -x "$llvm_configs" ; then
+ llvm_build_mode="`$llvm_configs --build-mode`"
+ else
+ as_fn_error $? "Could not autodetect build mode" "$LINENO" 5
+ fi
+ else
+ llvm_configs="`echo $llvm_obj/*/bin/llvm-config`"
+ if test -x "$llvm_obj/$with_llvm_build_mode/bin/llvm-config" ; then
+ llvm_build_mode=$with_llvm_build_mode
+ else
+ as_fn_error $? "Invalid build mode: $llvm_build_mode" "$LINENO" 5
+ fi
+ fi
-if test X${with_llvm_build_mode} = Xcheck ; then
- llvm_configs="`ls -1 $llvm_obj/*/bin/llvm-config 2>/dev/null | head -n 1`"
- if test -x "$llvm_configs" ; then
- llvm_build_mode="`$llvm_configs --build-mode`"
- else
- as_fn_error $? "Could not autodetect build mode" "$LINENO" 5
- fi
-else
- llvm_configs="`echo $llvm_obj/*/bin/llvm-config`"
- if test -x "$llvm_obj/$with_llvm_build_mode/bin/llvm-config" ; then
- llvm_build_mode=$with_llvm_build_mode
- else
- as_fn_error $? "Invalid build mode: $llvm_build_mode" "$LINENO" 5
- fi
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: $llvm_build_mode" >&5
+ $as_echo "$llvm_build_mode" >&6; }
+ LLVM_BUILD_MODE=$llvm_build_mode
fi
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $llvm_build_mode" >&5
-$as_echo "$llvm_build_mode" >&6; }
-LLVM_BUILD_MODE=$llvm_build_mode
+LLVM_BUILD_MODE=
diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp
index 8161a52..0eafbc2 100644
--- a/lib/Core/StatsTracker.cpp
+++ b/lib/Core/StatsTracker.cpp
@@ -55,6 +55,7 @@
#include <iostream>
#include <fstream>
+#include <unistd.h>
using namespace klee;
using namespace llvm;
@@ -182,14 +183,16 @@ StatsTracker::StatsTracker(Executor &_executor, std::string _objectFilename,
updateMinDistToUncovered(_updateMinDistToUncovered) {
KModule *km = executor.kmodule;
- sys::Path module(objectFilename);
+ SmallString<128> module(objectFilename);
#if LLVM_VERSION_CODE < LLVM_VERSION(3, 1)
if (!sys::Path(objectFilename).isAbsolute()) {
#else
if (!sys::path::is_absolute(objectFilename)) {
#endif
- sys::Path current = sys::Path::GetCurrentDirectory();
- current.appendComponent(objectFilename);
+ SmallString<128> current;
+ if (sys::fs::current_path(current))
+ klee_error("can't get current path");
+ sys::path::append(current, objectFilename);
#if LLVM_VERSION_CODE < LLVM_VERSION(3, 1)
if (current.exists())
#else
@@ -446,7 +449,7 @@ void StatsTracker::writeIStats() {
of << "version: 1\n";
of << "creator: klee\n";
- of << "pid: " << sys::Process::GetCurrentUserId() << "\n";
+ of << "pid: " << getpid() << "\n";
of << "cmd: " << m->getModuleIdentifier() << "\n\n";
of << "\n";
diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp
index 2ecb14b..fc49849 100644
--- a/lib/Module/KModule.cpp
+++ b/lib/Module/KModule.cpp
@@ -375,11 +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.
- llvm::sys::Path path(opts.LibraryDir);
+ SmallString<128> path(opts.LibraryDir);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
- path.appendComponent("kleeRuntimeIntrinsic.bc");
+ llvm::sys::path::append(path, "kleeRuntimeIntrinsic.bc");
#else
- path.appendComponent("libkleeRuntimeIntrinsic.bca");
+ llvm::sys::path::append(path, "libkleeRuntimeIntrinsic.bca");
#endif
module = linkWithLibrary(module, path.c_str());
diff --git a/lib/Module/ModuleUtil.cpp b/lib/Module/ModuleUtil.cpp
index 58096de..6a598b0 100644
--- a/lib/Module/ModuleUtil.cpp
+++ b/lib/Module/ModuleUtil.cpp
@@ -19,6 +19,7 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IRReader/IRReader.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
@@ -82,7 +83,7 @@ GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
if (I->isDeclaration())
UndefinedSymbols.insert(I->getName());
else if (!I->hasLocalLinkage()) {
- assert(!I->hasDLLImportLinkage()
+ assert(!I->hasDLLImportStorageClass()
&& "Found dllimported non-external symbol!");
DefinedSymbols.insert(I->getName());
}
@@ -94,7 +95,7 @@ GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
if (I->isDeclaration())
UndefinedSymbols.insert(I->getName());
else if (!I->hasLocalLinkage()) {
- assert(!I->hasDLLImportLinkage()
+ assert(!I->hasDLLImportStorageClass()
&& "Found dllimported non-external symbol!");
DefinedSymbols.insert(I->getName());
}
@@ -191,8 +192,8 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er
DEBUG_WITH_TYPE("klee_linker", dbgs() << "Loading modules\n");
// Load all bitcode files in to memory so we can examine their symbols
- for (object::Archive::child_iterator AI = archive->begin_children(),
- AE = archive->end_children(); AI != AE; ++AI)
+ for (object::Archive::child_iterator AI = archive->child_begin(),
+ AE = archive->child_end(); AI != AE; ++AI)
{
StringRef memberName;
@@ -215,7 +216,6 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er
// If we can't open as a binary object file its hopefully a bitcode file
OwningPtr<MemoryBuffer> buff; // Once this is destroyed will Module still be valid??
- Module *Result = 0;
if (error_code ec = AI->getMemoryBuffer(buff))
{
@@ -227,15 +227,15 @@ static bool linkBCA(object::Archive* archive, Module* composite, std::string& er
if (buff)
{
// FIXME: Maybe load bitcode file lazily? Then if we need to link, materialise the module
- Result = ParseBitcodeFile(buff.get(), getGlobalContext(), &errorMessage);
+ ErrorOr<Module *> bcfileOrErr = parseBitcodeFile(buff.get(), getGlobalContext());
- if(!Result)
+ if (error_code ec = bcfileOrErr.getError())
{
- SS << "Loading module failed : " << errorMessage << "\n";
+ SS << "Loading module failed : " << ec.message() << "\n";
SS.flush();
return false;
}
- archiveModules.push_back(Result);
+ archiveModules.push_back(bcfileOrErr.get());
}
else
{
@@ -349,17 +349,16 @@ DEBUG_WITH_TYPE("klee_linker", dbgs() << "Linking file " << libraryName << "\n")
libraryName.c_str());
}
- OwningPtr<MemoryBuffer> Buffer;
- if (error_code ec = MemoryBuffer::getFile(libraryName,Buffer)) {
+ ErrorOr<object::Binary *> binOrErr = object::createBinary(libraryName);
+ if (error_code ec = binOrErr.getError()) {
klee_error("Link with library %s failed: %s", libraryName.c_str(),
ec.message().c_str());
}
- sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
-
- LLVMContext &Context = getGlobalContext();
std::string ErrorMessage;
+ OwningPtr<object::Binary> binary(binOrErr.get());
+ #if 0
if (magic == sys::fs::file_magic::bitcode) {
Module *Result = 0;
Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
@@ -372,29 +371,18 @@ DEBUG_WITH_TYPE("klee_linker", dbgs() << "Linking file " << libraryName << "\n")
delete Result;
- } else if (magic == sys::fs::file_magic::archive) {
- OwningPtr<object::Binary> arch;
- if (error_code ec = object::createBinary(Buffer.take(), arch))
+ }
+ #endif
+ if (object::Archive *a = dyn_cast<object::Archive>(binary.get())) {
+ // Handle in helper
+ if (!linkBCA(a, module, ErrorMessage))
klee_error("Link with library %s failed: %s", libraryName.c_str(),
- ec.message().c_str());
-
- if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
- // Handle in helper
- if (!linkBCA(a, module, ErrorMessage))
- klee_error("Link with library %s failed: %s", libraryName.c_str(),
- ErrorMessage.c_str());
- }
- else {
- klee_error("Link with library %s failed: Cast to archive failed", libraryName.c_str());
- }
+ ErrorMessage.c_str());
- } else if (magic.is_object()) {
- OwningPtr<object::Binary> obj;
- if (object::ObjectFile *o = dyn_cast<object::ObjectFile>(obj.get())) {
- klee_warning("Link with library: Object file %s in archive %s found. "
- "Currently not supported.",
- o->getFileName().data(), libraryName.c_str());
- }
+ } else if (object::ObjectFile *o = dyn_cast<object::ObjectFile>(binary.get())) {
+ klee_warning("Link with library: Object file %s in archive %s found. "
+ "Currently not supported.",
+ o->getFileName().data(), libraryName.c_str());
} else {
klee_error("Link with library %s failed: Unrecognized file type.",
libraryName.c_str());
@@ -404,7 +392,7 @@ DEBUG_WITH_TYPE("klee_linker", dbgs() << "Linking file " << libraryName << "\n")
#else
Linker linker("klee", module, false);
- llvm::sys::Path libraryPath(libraryName);
+ SmallString<128> libraryPath(libraryName);
bool native = false;
if (linker.LinkInFile(libraryPath, native)) {
diff --git a/lib/Module/Optimize.cpp b/lib/Module/Optimize.cpp
index 41a106f..0ef1c16 100644
--- a/lib/Module/Optimize.cpp
+++ b/lib/Module/Optimize.cpp
@@ -124,7 +124,11 @@ static void AddStandardCompilePasses(PassManager &PM) {
addPass(PM, createFunctionInliningPass()); // Inline small functions
addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args
+#if defined(LLVM_3_1) || defined(LLVM_3_2) || defined(LLVM_3_3)
+ // Starting from 3.4 this functionality was moved to
+ // InstructionCombiningPass. See r184459 for details.
addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations
+#endif
addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl.
addPass(PM, createJumpThreadingPass()); // Thread jumps.
addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp
index f9698fd..74a3616 100644
--- a/tools/klee/main.cpp
+++ b/tools/klee/main.cpp
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
static void getOutFiles(std::string path,
std::vector<std::string> &results);
- static llvm::sys::Path getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
+ static SmallString<128> getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
};
KleeHandler::KleeHandler(int argc, char **argv)
@@ -274,25 +274,24 @@ KleeHandler::KleeHandler(int argc, char **argv)
m_argv(argv) {
if (OutputDir=="") {
- llvm::sys::Path directory(InputFile);
+ SmallString<128> directory(InputFile);
std::stringstream dirname;
- directory.eraseComponent();
+ llvm::sys::path::remove_filename(directory);
- if (directory.isEmpty())
- directory.set(".");
+ if (directory.empty())
+ directory = ".";
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());
+ m_outputDirectory = directory;
+ 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.str().c_str());
+ klee_error("Failed to check if \"%s\" exists.", m_outputDirectory.c_str());
if (!isDir)
{
@@ -303,7 +302,7 @@ KleeHandler::KleeHandler(int argc, char **argv)
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());
+ m_outputDirectory.c_str());
dirname.str(""); // Clear
m_outputDirectory.clear();
@@ -314,9 +313,8 @@ KleeHandler::KleeHandler(int argc, char **argv)
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");
+ 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", strerror(errno));
@@ -325,14 +323,14 @@ KleeHandler::KleeHandler(int argc, char **argv)
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());
+ m_outputDirectory = OutputDir;
}
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");
+ SmallString<128> cwd;
+ if (sys::fs::current_path(cwd))
+ klee_error("can't get current path");
+ llvm::sys::path::append(cwd, m_outputDirectory.c_str());
m_outputDirectory = cwd;
}
@@ -376,10 +374,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);
+ llvm::sys::path::append(path, filename);
return path.str();
}
@@ -555,42 +551,39 @@ 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)) {
+ llvm::error_code ec;
+ for (llvm::sys::fs::directory_iterator Entry(path, ec), End;
+ Entry != End && !ec; Entry.increment(ec)) {
+ std::string f = Entry->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
- << ": " << error << "\n";
+ << ": " << ec.message() << "\n";
exit(1);
}
- for (std::set<llvm::sys::Path>::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);
- }
- }
}
-llvm::sys::Path KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
+SmallString<128> 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(llvm::sys::fs::getMainExecutable(argv0, MainExecAddr));
+ llvm::sys::path::remove_filename(toolRoot);
- llvm::sys::Path libDir;
+ 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 = SmallString<128>( 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 = SmallString<128>(KLEE_DIR "/" RUNTIME_CONFIGURATION "/lib");
}
DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
@@ -1007,7 +1000,7 @@ static char *format_tdiff(char *buf, long seconds)
}
#ifndef SUPPORT_KLEE_UCLIBC
-static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) {
+static llvm::Module *linkWithUclibc(llvm::Module *mainModule, SmallString<128> libDir) {
fprintf(stderr, "error: invalid libc, no uclibc support!\n");
exit(1);
return 0;
@@ -1030,9 +1023,9 @@ static void replaceOrRenameFunction(llvm::Module *module,
}
}
-static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) {
+static llvm::Module *linkWithUclibc(llvm::Module *mainModule, SmallString<128> libDir) {
// Ensure that klee-uclibc exists
- llvm::sys::Path uclibcBCA(libDir);
+ SmallString<128> uclibcBCA(libDir);
uclibcBCA.appendComponent(KLEE_UCLIBC_BCA_NAME);
bool uclibcExists=false;
@@ -1233,25 +1226,27 @@ int main(int argc, char **argv, char **envp) {
sys::SetInterruptFunction(interrupt_handle);
// Load the bytecode...
- std::string ErrorMsg;
- Module *mainModule = 0;
OwningPtr<MemoryBuffer> BufferPtr;
error_code ec=MemoryBuffer::getFileOrSTDIN(InputFile.c_str(), BufferPtr);
if (ec) {
klee_error("error loading program '%s': %s", InputFile.c_str(),
ec.message().c_str());
}
- mainModule = getLazyBitcodeModule(BufferPtr.get(), getGlobalContext(), &ErrorMsg);
+ ErrorOr<Module *> modOrErr = getLazyBitcodeModule(BufferPtr.get(), getGlobalContext());
+ if (!modOrErr) {
+ klee_error("couldn't getLazyBitcodeModule %s: %s", InputFile.c_str(),
+ ec.message().c_str());
+ }
+ Module *mainModule = modOrErr.get();
if (mainModule) {
- if (mainModule->MaterializeAllPermanently(&ErrorMsg)) {
+ if (error_code ec = mainModule->materializeAllPermanently()) {
+ klee_error("couldn't materializeAllPermanently %s: %s", InputFile.c_str(),
+ ec.message().c_str());
delete mainModule;
mainModule = 0;
}
}
- if (!mainModule)
- klee_error("error loading program '%s': %s", InputFile.c_str(),
- ErrorMsg.c_str());
if (WithPOSIXRuntime) {
int r = initEnv(mainModule);
@@ -1259,7 +1254,7 @@ int main(int argc, char **argv, char **envp) {
return r;
}
- llvm::sys::Path LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0],
+ SmallString<128> LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0],
reinterpret_cast<void*>(main));
Interpreter::ModuleOptions Opts(LibraryDir.c_str(),
/*Optimize=*/OptimizeModule,
@@ -1272,9 +1267,9 @@ int main(int argc, char **argv, char **envp) {
case KleeLibc: {
// FIXME: Find a reasonable solution for this.
- llvm::sys::Path Path(Opts.LibraryDir);
+ SmallString<128> Path(Opts.LibraryDir);
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
- Path.appendComponent("klee-libc.bc");
+ llvm::sys::path::append(Path, "klee-libc.bc");
#else
Path.appendComponent("libklee-libc.bca");
#endif
@@ -1289,8 +1284,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");
_______________________________________________
klee-dev mailing list
https://mailman.ic.ac.uk/mailman/listinfo/klee-dev
---------------------------------------------------
Martin Nowack
Research Assistant

Technische Universität Dresden
Computer Science
Institute of Systems Architecture
Systems Engineering
01062 Dresden

Phone: +49 351 463 39608
Email: martin_nowack-***@public.gmane.org
----------------------------------------------------

Loading...