about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorLukas Zaoral <lzaoral@redhat.com>2021-09-13 10:33:09 +0200
committerMartinNowack <2443641+MartinNowack@users.noreply.github.com>2021-10-27 12:06:36 +0100
commitc9aadba9107ad4a6abaa2b2539c47adac2d2b810 (patch)
tree197260049b119af2121431a3b7e593aaebc89395
parent46dc29aa60e4a662c18ea7b2bac1c570ea7a728f (diff)
downloadklee-c9aadba9107ad4a6abaa2b2539c47adac2d2b810.tar.gz
tools/klee: Warn if module and host target triples differ
... as running a bitcode with a different target triple may result in
unexpected crashes or assertion violations.
-rw-r--r--test/Feature/TargetMismatch.c9
-rw-r--r--tools/klee/main.cpp17
2 files changed, 22 insertions, 4 deletions
diff --git a/test/Feature/TargetMismatch.c b/test/Feature/TargetMismatch.c
new file mode 100644
index 00000000..aae7fda6
--- /dev/null
+++ b/test/Feature/TargetMismatch.c
@@ -0,0 +1,9 @@
+// REQUIRES: not-darwin
+// RUN: %clang %s -m32 -emit-llvm %O0opt -c -o %t1.bc
+// RUN: rm -rf %t.klee-out
+// RUN: %klee --output-dir=%t.klee-out --exit-on-error > %t2.out 2>&1 || true
+// RUN: FileCheck %s -input-file=%t2.out
+
+// CHECK: KLEE: WARNING: Module and host target triples do not match
+// CHECK-NEXT: This may cause unexpected crashes or assertion violations.
+int main(void) {}
diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp
index f340e743..1ab809aa 100644
--- a/tools/klee/main.cpp
+++ b/tools/klee/main.cpp
@@ -34,6 +34,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Errno.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
@@ -1258,12 +1259,20 @@ int main(int argc, char **argv, char **envp) {
 
   llvm::Module *mainModule = M.get();
 
+  const std::string &module_triple = mainModule->getTargetTriple();
+  std::string host_triple = llvm::sys::getDefaultTargetTriple();
+
+  if (module_triple != host_triple)
+    klee_warning("Module and host target triples do not match: '%s' != '%s'\n"
+                 "This may cause unexpected crashes or assertion violations.",
+                 module_triple.c_str(), host_triple.c_str());
+
   // Detect architecture
   std::string opt_suffix = "64"; // Fall back to 64bit
-  if (mainModule->getTargetTriple().find("i686") != std::string::npos ||
-      mainModule->getTargetTriple().find("i586") != std::string::npos ||
-      mainModule->getTargetTriple().find("i486") != std::string::npos ||
-      mainModule->getTargetTriple().find("i386") != std::string::npos)
+  if (module_triple.find("i686") != std::string::npos ||
+      module_triple.find("i586") != std::string::npos ||
+      module_triple.find("i486") != std::string::npos ||
+      module_triple.find("i386") != std::string::npos)
     opt_suffix = "32";
 
   // Add additional user-selected suffix