|
| 1 | +#ifndef ABSTRACT_EXECUTION_ENGINE_H |
| 2 | +#define ABSTRACT_EXECUTION_ENGINE_H |
| 3 | + |
| 4 | +#include "AbstractState.h" |
| 5 | +#include "AbstractValue.h" |
| 6 | + |
| 7 | +#include "llvm/ADT/SmallVector.h" |
| 8 | +#include "llvm/IR/BasicBlock.h" |
| 9 | +#include "llvm/IR/Instruction.h" |
| 10 | +#include "llvm/Support/Debug.h" |
| 11 | +#include "llvm/Support/raw_ostream.h" |
| 12 | +#include <list> |
| 13 | +#include <map> |
| 14 | +#include <string> |
| 15 | +#include <utility> |
| 16 | + |
| 17 | +#ifndef DEBUG_TYPE |
| 18 | +#define DEBUG_TYPE "abstract-execution" |
| 19 | +#endif |
| 20 | + |
| 21 | +#define NUM_RECENT_BLOCKS 16 |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | + |
| 25 | +// This class defines an abstract execution engine. An abstract execution engine |
| 26 | +// takes in a program and executes the program abstractly using semantics |
| 27 | +// defined for an abstract value. |
| 28 | +// T is the type of abstract value used for abstract execution. It must implement |
| 29 | +// AbstractValue<T>. |
| 30 | +// U is the type of abstract value used for abstract execution. It must implement |
| 31 | +// AbstractState<T>. |
| 32 | +template<typename T, typename U> |
| 33 | +class AbstractExecutionEngine { |
| 34 | + static_assert( |
| 35 | + std::is_base_of<AbstractValue<T>, T>::value, |
| 36 | + "T must be a descendant of AbstractValue<T>" |
| 37 | + ); |
| 38 | + static_assert( |
| 39 | + std::is_base_of<AbstractState<T, U>, U>::value, |
| 40 | + "U must be a descendant of AbstractState<T, U>" |
| 41 | + ); |
| 42 | + public: |
| 43 | + AbstractExecutionEngine() |
| 44 | + : entryBlock_(nullptr) {} |
| 45 | + AbstractExecutionEngine(const BasicBlock* entryBlock, U initialState) |
| 46 | + : entryBlock_(entryBlock), initialState_(initialState) {} |
| 47 | + |
| 48 | + virtual ~AbstractExecutionEngine() = 0; |
| 49 | + |
| 50 | + // Queries the state before an instruction. |
| 51 | + const U& getStateBeforeInstruction(const Instruction* inst){ |
| 52 | + return StateBeforeInstructionMap_[inst]; |
| 53 | + } |
| 54 | + |
| 55 | + // Adds a block to execute next and the state in which the block must be |
| 56 | + // executed. |
| 57 | + void AddBlockToExecute(const BasicBlock* b, U st); |
| 58 | + |
| 59 | + // Executes program (can be overriden). |
| 60 | + virtual void Execute(); |
| 61 | + |
| 62 | + // Executes the instruction on a state and returns the state after execution. |
| 63 | + virtual U ExecuteInstruction(const Instruction* inst, |
| 64 | + U st) = 0; |
| 65 | + |
| 66 | + protected: |
| 67 | + // Entry block where the abstract execution begins. |
| 68 | + const BasicBlock* entryBlock_; |
| 69 | + |
| 70 | + // Initial state before execution of the program. |
| 71 | + U initialState_; |
| 72 | + |
| 73 | + private: |
| 74 | + // Returns the next unit to execute. |
| 75 | + std::pair<const BasicBlock*, U> getNextExecutionUnit( |
| 76 | + std::list<std::pair<const BasicBlock*, U>>& worklist); |
| 77 | + |
| 78 | + // Add block to recently executed blocks. |
| 79 | + void AddRecentBlock(const BasicBlock* block); |
| 80 | + |
| 81 | + // Stores some recent blocks executed by the engine. |
| 82 | + SmallVector<const BasicBlock*, NUM_RECENT_BLOCKS> recentBlocks_; |
| 83 | + |
| 84 | + // Records abstract state before an instruction is executed. |
| 85 | + std::map<const Instruction*, U> StateBeforeInstructionMap_; |
| 86 | + |
| 87 | + // Buffer to store the set of blocks that must be executed after this block |
| 88 | + // completes execution. |
| 89 | + std::list<std::pair<const BasicBlock*, U>> BlocksToExecuteBuffer_; |
| 90 | +}; |
| 91 | + |
| 92 | +template<typename T, typename U> |
| 93 | +AbstractExecutionEngine<T, U>::~AbstractExecutionEngine() {} |
| 94 | + |
| 95 | +template<typename T, typename U> |
| 96 | +void AbstractExecutionEngine<T, U>::AddBlockToExecute(const BasicBlock* b, U st) { |
| 97 | + BlocksToExecuteBuffer_.push_back(std::pair<const BasicBlock*, U>(b, st)); |
| 98 | +} |
| 99 | + |
| 100 | +// Returns a block in recentBlocks_ if found. Otherwise returns the |
| 101 | +// first block in worklist. This optimization is useful for execution |
| 102 | +// of loops. All blocks within the loop are given priority over blocks |
| 103 | +// after the loop. This ensures that the blocks after the loop are |
| 104 | +// executed only after the loop reaches a fixed point. |
| 105 | +template<typename T, typename U> |
| 106 | +std::pair<const BasicBlock*, U> |
| 107 | + AbstractExecutionEngine<T, U>::getNextExecutionUnit( |
| 108 | + std::list<std::pair<const BasicBlock*, U>>& worklist) { |
| 109 | + for (const BasicBlock* block : recentBlocks_) { |
| 110 | + auto listIt = find_if(worklist.begin(), worklist.end(), |
| 111 | + [block](const std::pair<const BasicBlock*, U>& item){ |
| 112 | + if (item.first == block) return true; |
| 113 | + else return false; |
| 114 | + }); |
| 115 | + if (listIt != worklist.end()) { |
| 116 | + // Block found. |
| 117 | + auto unit = *listIt; |
| 118 | + worklist.erase(listIt); |
| 119 | + AddRecentBlock(unit.first); |
| 120 | + return unit; |
| 121 | + } |
| 122 | + } |
| 123 | + auto unit = worklist.front(); |
| 124 | + worklist.pop_front(); |
| 125 | + AddRecentBlock(unit.first); |
| 126 | + return unit; |
| 127 | +} |
| 128 | + |
| 129 | +// Adds block to the set of recent blocks. |
| 130 | +template<typename T, typename U> |
| 131 | +void AbstractExecutionEngine<T, U>::AddRecentBlock(const BasicBlock* block) { |
| 132 | + auto pos = recentBlocks_.begin(); |
| 133 | + while (*pos != block && pos != recentBlocks_.end()) ++pos; |
| 134 | + if (pos != recentBlocks_.end()) { recentBlocks_.erase(pos); } |
| 135 | + if (recentBlocks_.size() >= NUM_RECENT_BLOCKS) { |
| 136 | + recentBlocks_.pop_back(); |
| 137 | + } |
| 138 | + recentBlocks_.insert(recentBlocks_.begin(), block); |
| 139 | +} |
| 140 | + |
| 141 | +template<typename T, typename U> |
| 142 | +void AbstractExecutionEngine<T, U>::Execute() { |
| 143 | + // Worklist to execute basic blocks. |
| 144 | + // Each worklist item consists of a basicblock and an abstract state to be |
| 145 | + // propagated through the block. |
| 146 | + std::list<std::pair<const BasicBlock*, U>> worklist; |
| 147 | + worklist.push_back(std::pair<const BasicBlock*, U>(entryBlock_, initialState_)); |
| 148 | + |
| 149 | + // Execute work items in worklist. |
| 150 | + StateBeforeInstructionMap_.clear(); |
| 151 | + while (!worklist.empty()) { |
| 152 | + auto unit = getNextExecutionUnit(worklist); |
| 153 | + const BasicBlock *b = unit.first; // next block to be executed. |
| 154 | + U st = unit.second; // state before next block. |
| 155 | + LLVM_DEBUG(errs() << "BasicBlock: " << b->getName() << "\n"); |
| 156 | + |
| 157 | + // Clear buffer. |
| 158 | + BlocksToExecuteBuffer_.clear(); |
| 159 | + // Execute instructions within the block. |
| 160 | + for (BasicBlock::const_iterator it = b->begin(), ite = b->end(); |
| 161 | + it != ite; ++it) { |
| 162 | + const Instruction* I = &*it; |
| 163 | + // If I is the first statement in the block, merge I's pre-state |
| 164 | + // with incoming state. |
| 165 | + if (it == b->begin()) { |
| 166 | + if(StateBeforeInstructionMap_.find(I) != |
| 167 | + StateBeforeInstructionMap_.end()) { |
| 168 | + U oldState = StateBeforeInstructionMap_[I]; |
| 169 | + U newState = oldState.mergeState(st); |
| 170 | + // State before block unchanged; no need to execute block. |
| 171 | + if (oldState == newState) break; |
| 172 | + |
| 173 | + StateBeforeInstructionMap_[I] = newState; |
| 174 | + } else { |
| 175 | + StateBeforeInstructionMap_[I] = st; |
| 176 | + } |
| 177 | + } else { |
| 178 | + StateBeforeInstructionMap_[I] = st; |
| 179 | + } |
| 180 | + |
| 181 | + LLVM_DEBUG(errs() << " " << *I << ", " << st.printInstructionState(I) << "\n"); |
| 182 | + st = ExecuteInstruction(I, st); |
| 183 | + } |
| 184 | + // Add subsequent blocks to be executed. Note that these were added to |
| 185 | + // the buffer during the execution of instructions in the current block. |
| 186 | + for (auto bufferIt = BlocksToExecuteBuffer_.begin(), |
| 187 | + bufferIte = BlocksToExecuteBuffer_.end(); bufferIt != bufferIte; |
| 188 | + ++bufferIt) { |
| 189 | + // Check if the key already exists in worklist, if so, merge the two |
| 190 | + // work items. This is an optimization that helps scale the execution, |
| 191 | + // at the cost of being slightly imprecise. |
| 192 | + const BasicBlock* block = bufferIt->first; |
| 193 | + auto listIt = find_if(worklist.begin(), worklist.end(), |
| 194 | + [block](const std::pair<const BasicBlock*, U>& item){ |
| 195 | + if (item.first == block) return true; |
| 196 | + else return false; |
| 197 | + }); |
| 198 | + if (listIt != worklist.end()) { |
| 199 | + listIt->second = listIt->second.mergeState(bufferIt->second); |
| 200 | + } else { |
| 201 | + worklist.push_back(std::pair<const BasicBlock*, U>(bufferIt->first, |
| 202 | + bufferIt->second)); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +#undef DEBUG_TYPE |
| 209 | + |
| 210 | +#endif /* AbstractExecutionEngine.h */ |
0 commit comments