1 //===-- KModule.h -----------------------------------------------*- C++ -*-===//
2 //
3 //                     The KLEE Symbolic Virtual Machine
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef KLEE_KMODULE_H
11 #define KLEE_KMODULE_H
12 
13 #include "klee/Config/Version.h"
14 #include "klee/Core/Interpreter.h"
15 
16 #include "llvm/ADT/ArrayRef.h"
17 
18 #include <map>
19 #include <memory>
20 #include <set>
21 #include <vector>
22 
23 namespace llvm {
24   class BasicBlock;
25   class Constant;
26   class Function;
27   class Instruction;
28   class Module;
29   class DataLayout;
30 }
31 
32 namespace klee {
33   struct Cell;
34   class Executor;
35   class Expr;
36   class InterpreterHandler;
37   class InstructionInfoTable;
38   struct KInstruction;
39   class KModule;
40   template<class T> class ref;
41 
42   struct KFunction {
43     llvm::Function *function;
44 
45     unsigned numArgs, numRegisters;
46 
47     unsigned numInstructions;
48     KInstruction **instructions;
49 
50     std::map<llvm::BasicBlock*, unsigned> basicBlockEntry;
51 
52     /// Whether instructions in this function should count as
53     /// "coverable" for statistics and search heuristics.
54     bool trackCoverage;
55 
56   public:
57     explicit KFunction(llvm::Function*, KModule *);
58     KFunction(const KFunction &) = delete;
59     KFunction &operator=(const KFunction &) = delete;
60 
61     ~KFunction();
62 
getArgRegisterKFunction63     unsigned getArgRegister(unsigned index) { return index; }
64   };
65 
66 
67   class KConstant {
68   public:
69     /// Actual LLVM constant this represents.
70     llvm::Constant* ct;
71 
72     /// The constant ID.
73     unsigned id;
74 
75     /// First instruction where this constant was encountered, or NULL
76     /// if not applicable/unavailable.
77     KInstruction *ki;
78 
79     KConstant(llvm::Constant*, unsigned, KInstruction*);
80   };
81 
82 
83   class KModule {
84   public:
85     std::unique_ptr<llvm::Module> module;
86     std::unique_ptr<llvm::DataLayout> targetData;
87 
88     // Our shadow versions of LLVM structures.
89     std::vector<std::unique_ptr<KFunction>> functions;
90     std::map<llvm::Function*, KFunction*> functionMap;
91 
92     // Functions which escape (may be called indirectly)
93     // XXX change to KFunction
94     std::set<llvm::Function*> escapingFunctions;
95 
96     std::unique_ptr<InstructionInfoTable> infos;
97 
98     std::vector<llvm::Constant*> constants;
99     std::map<const llvm::Constant *, std::unique_ptr<KConstant>> constantMap;
100     KConstant* getKConstant(const llvm::Constant *c);
101 
102     std::unique_ptr<Cell[]> constantTable;
103 
104     // Functions which are part of KLEE runtime
105     std::set<const llvm::Function*> internalFunctions;
106 
107   private:
108     // Mark function with functionName as part of the KLEE runtime
109     void addInternalFunction(const char* functionName);
110 
111   public:
112     KModule() = default;
113 
114     /// Optimise and prepare module such that KLEE can execute it
115     //
116     void optimiseAndPrepare(const Interpreter::ModuleOptions &opts,
117                             llvm::ArrayRef<const char *>);
118 
119     /// Manifest the generated module (e.g. assembly.ll, output.bc) and
120     /// prepares KModule
121     ///
122     /// @param ih
123     /// @param forceSourceOutput true if assembly.ll should be created
124     ///
125     // FIXME: ihandler should not be here
126     void manifest(InterpreterHandler *ih, bool forceSourceOutput);
127 
128     /// Link the provided modules together as one KLEE module.
129     ///
130     /// If the entry point is empty, all modules are linked together.
131     /// If the entry point is not empty, all modules are linked which resolve
132     /// the dependencies of the module containing entryPoint
133     ///
134     /// @param modules list of modules to be linked together
135     /// @param entryPoint name of the function which acts as the program's entry
136     /// point
137     /// @return true if at least one module has been linked in, false if nothing
138     /// changed
139     bool link(std::vector<std::unique_ptr<llvm::Module>> &modules,
140               const std::string &entryPoint);
141 
142     void instrument(const Interpreter::ModuleOptions &opts);
143 
144     /// Return an id for the given constant, creating a new one if necessary.
145     unsigned getConstantID(llvm::Constant *c, KInstruction* ki);
146 
147     /// Run passes that check if module is valid LLVM IR and if invariants
148     /// expected by KLEE's Executor hold.
149     void checkModule();
150   };
151 } // End klee namespace
152 
153 #endif /* KLEE_KMODULE_H */
154