From 99c522b14dbbf6b26be35b6e7bb8da7b29070287 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 8 Jun 2022 16:36:28 +0300 Subject: Inline asm external call --- lib/Core/Executor.cpp | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'lib/Core/Executor.cpp') diff --git a/lib/Core/Executor.cpp b/lib/Core/Executor.cpp index 11ad902e..42405982 100644 --- a/lib/Core/Executor.cpp +++ b/lib/Core/Executor.cpp @@ -37,6 +37,7 @@ #include "klee/Expr/ExprUtil.h" #include "klee/Module/Cell.h" #include "klee/Module/InstructionInfoTable.h" +#include "klee/Module/KCallable.h" #include "klee/Module/KInstruction.h" #include "klee/Module/KModule.h" #include "klee/Solver/Common.h" @@ -59,6 +60,7 @@ #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" +#include "llvm/IR/InlineAsm.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" @@ -1658,10 +1660,11 @@ void Executor::executeCall(ExecutionState &state, KInstruction *ki, Function *f, return; if (f && f->isDeclaration()) { switch (f->getIntrinsicID()) { - case Intrinsic::not_intrinsic: + case Intrinsic::not_intrinsic: { // state may be destroyed by this call, cannot touch - callExternalFunction(state, ki, f, arguments); + callExternalFunction(state, ki, kmodule->functionMap[f], arguments); break; + } case Intrinsic::fabs: { ref arg = toConstant(state, arguments[0], "floating point"); @@ -2399,10 +2402,6 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) { unsigned numArgs = cb.arg_size(); Function *f = getTargetFunction(fp, state); - if (isa(fp)) { - terminateStateOnExecError(state, "inline assembly is unsupported"); - break; - } // evaluate arguments std::vector< ref > arguments; arguments.reserve(numArgs); @@ -2410,6 +2409,16 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) { for (unsigned j=0; j(fp)) { //TODO: move to `executeCall` + if (ExternalCalls != ExternalCallPolicy::None) { + KInlineAsm callable(asmValue); + callExternalFunction(state, ki, &callable, arguments); + } else { + terminateStateOnExecError(state, "external calls disallowed (in particular inline asm)"); + } + break; + } + if (f) { const FunctionType *fType = dyn_cast(cast(f->getType())->getElementType()); @@ -3784,16 +3793,18 @@ static std::set okExternals(okExternalsList, void Executor::callExternalFunction(ExecutionState &state, KInstruction *target, - Function *function, + KCallable *callable, std::vector< ref > &arguments) { // check if specialFunctionHandler wants it - if (specialFunctionHandler->handle(state, function, target, arguments)) - return; + if (const auto *func = dyn_cast(callable)) { + if (specialFunctionHandler->handle(state, func->function, target, arguments)) + return; + } if (ExternalCalls == ExternalCallPolicy::None && - !okExternals.count(function->getName().str())) { + !okExternals.count(callable->getName().str())) { klee_warning("Disallowed call to external function: %s\n", - function->getName().str().c_str()); + callable->getName().str().c_str()); terminateStateOnUserError(state, "external calls disallowed"); return; } @@ -3835,7 +3846,7 @@ void Executor::callExternalFunction(ExecutionState &state, } else { terminateStateOnExecError(state, "external call with symbolic argument: " + - function->getName()); + callable->getName()); return; } } @@ -3856,7 +3867,7 @@ void Executor::callExternalFunction(ExecutionState &state, if (!errnoValue) { terminateStateOnExecError(state, "external call with errno value symbolic: " + - function->getName()); + callable->getName()); return; } @@ -3868,7 +3879,7 @@ void Executor::callExternalFunction(ExecutionState &state, std::string TmpStr; llvm::raw_string_ostream os(TmpStr); - os << "calling external: " << function->getName().str() << "("; + os << "calling external: " << callable->getName().str() << "("; for (unsigned i=0; igetValue(), "%s", os.str().c_str()); } - bool success = externalDispatcher->executeCall(function, target->inst, args); + bool success = externalDispatcher->executeCall(callable, target->inst, args); if (!success) { - terminateStateOnError(state, "failed external call: " + function->getName(), + terminateStateOnError(state, "failed external call: " + callable->getName(), StateTerminationType::External); return; } @@ -3903,7 +3914,7 @@ void Executor::callExternalFunction(ExecutionState &state, #endif Type *resultType = target->inst->getType(); - if (resultType != Type::getVoidTy(function->getContext())) { + if (resultType != Type::getVoidTy(kmodule->module->getContext())) { ref e = ConstantExpr::fromMemory((void*) args, getWidthForLLVMType(resultType)); bindLocal(target, state, e); -- cgit 1.4.1