//===-- KModule.h -----------------------------------------------*- C++ -*-===// // // The KLEE Symbolic Virtual Machine // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef KLEE_KMODULE_H #define KLEE_KMODULE_H #include "klee/Config/Version.h" #include "klee/Core/Interpreter.h" #include "klee/Module/KCallable.h" #include "llvm/ADT/ArrayRef.h" #include #include #include #include namespace llvm { class BasicBlock; class Constant; class Function; class Instruction; class Module; class DataLayout; } namespace klee { struct Cell; class Executor; class Expr; class InterpreterHandler; class InstructionInfoTable; struct KInstruction; class KModule; template class ref; struct KFunction : public KCallable { llvm::Function *function; unsigned numArgs, numRegisters; unsigned numInstructions; KInstruction **instructions; std::map basicBlockEntry; /// Whether instructions in this function should count as /// "coverable" for statistics and search heuristics. bool trackCoverage; explicit KFunction(llvm::Function*, KModule*); KFunction(const KFunction &) = delete; KFunction &operator=(const KFunction &) = delete; ~KFunction(); unsigned getArgRegister(unsigned index) { return index; } llvm::StringRef getName() const override { return function->getName(); } llvm::FunctionType *getFunctionType() const override { return function->getFunctionType(); } llvm::Value *getValue() override { return function; } static bool classof(const KCallable *callable) { return callable->getKind() == CK_Function; } }; class KConstant { public: /// Actual LLVM constant this represents. llvm::Constant* ct; /// The constant ID. unsigned id; /// First instruction where this constant was encountered, or NULL /// if not applicable/unavailable. KInstruction *ki; KConstant(llvm::Constant*, unsigned, KInstruction*); }; class KModule { public: std::unique_ptr module; std::unique_ptr targetData; // Our shadow versions of LLVM structures. std::vector> functions; std::map functionMap; // Functions which escape (may be called indirectly) // XXX change to KFunction std::set escapingFunctions; std::unique_ptr infos; std::vector constants; std::map> constantMap; KConstant* getKConstant(const llvm::Constant *c); std::unique_ptr constantTable; // Functions which are part of KLEE runtime std::set internalFunctions; private: // Mark function with functionName as part of the KLEE runtime void addInternalFunction(const char* functionName); public: KModule() = default; /// Optimise and prepare module such that KLEE can execute it // void optimiseAndPrepare(const Interpreter::ModuleOptions &opts, llvm::ArrayRef); /// Manifest the generated module (e.g. assembly.ll, output.bc) and /// prepares KModule /// /// @param ih /// @param forceSourceOutput true if assembly.ll should be created /// // FIXME: ihandler should not be here void manifest(InterpreterHandler *ih, bool forceSourceOutput); /// Link the provided modules together as one KLEE module. /// /// If the entry point is empty, all modules are linked together. /// If the entry point is not empty, all modules are linked which resolve /// the dependencies of the module containing entryPoint /// /// @param modules list of modules to be linked together /// @param entryPoint name of the function which acts as the program's entry /// point /// @return true if at least one module has been linked in, false if nothing /// changed bool link(std::vector> &modules, const std::string &entryPoint); void instrument(const Interpreter::ModuleOptions &opts); /// Return an id for the given constant, creating a new one if necessary. unsigned getConstantID(llvm::Constant *c, KInstruction* ki); /// Run passes that check if module is valid LLVM IR and if invariants /// expected by KLEE's Executor hold. void checkModule(); }; } // End klee namespace #endif /* KLEE_KMODULE_H */