From f64ac45c41042e6d33024958dc82b6849844cdac Mon Sep 17 00:00:00 2001 From: Lukas Zaoral Date: Tue, 20 Apr 2021 23:25:39 +0200 Subject: Module/RaiseAsm: Fix crash when host and bitcode target triples differ RaiseAsm always created a target machine for the host target and not for the bitcode one. Therefore, KLEE could crash on i686 bitcode as x86_64 target machine cannot be used to raise i686 assembly. Fixes: LLVM ERROR: 64-bit code requested on a subtarget that doesn't support it! --- lib/Module/RaiseAsm.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/Module/RaiseAsm.cpp b/lib/Module/RaiseAsm.cpp index ff4c2467..cd8f7934 100644 --- a/lib/Module/RaiseAsm.cpp +++ b/lib/Module/RaiseAsm.cpp @@ -86,28 +86,31 @@ bool RaiseAsmPass::runOnInstruction(Module &M, Instruction *I) { bool RaiseAsmPass::runOnModule(Module &M) { bool changed = false; - std::string Err; - std::string HostTriple = llvm::sys::getDefaultTargetTriple(); - const Target *NativeTarget = TargetRegistry::lookupTarget(HostTriple, Err); + + // Use target triple from the module if possible. + std::string TargetTriple = M.getTargetTriple(); + if (TargetTriple.empty()) + TargetTriple = llvm::sys::getDefaultTargetTriple(); + const Target *Target = TargetRegistry::lookupTarget(TargetTriple, Err); TargetMachine * TM = 0; - if (NativeTarget == 0) { - klee_warning("Warning: unable to select native target: %s", Err.c_str()); + if (Target == 0) { + klee_warning("Warning: unable to select target: %s", Err.c_str()); TLI = 0; } else { #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 9) - TM = NativeTarget->createTargetMachine(HostTriple, "", "", TargetOptions(), - None); + TM = Target->createTargetMachine(TargetTriple, "", "", TargetOptions(), + None); TLI = TM->getSubtargetImpl(*(M.begin()))->getTargetLowering(); #else - TM = NativeTarget->createTargetMachine(HostTriple, "", "", TargetOptions()); + TM = Target->createTargetMachine(TargetTriple, "", "", TargetOptions()); TLI = TM->getSubtargetImpl(*(M.begin()))->getTargetLowering(); #endif - triple = llvm::Triple(HostTriple); + triple = llvm::Triple(TargetTriple); } - + for (Module::iterator fi = M.begin(), fe = M.end(); fi != fe; ++fi) { for (Function::iterator bi = fi->begin(), be = fi->end(); bi != be; ++bi) { for (BasicBlock::iterator ii = bi->begin(), ie = bi->end(); ii != ie;) { -- cgit 1.4.1