ROSE  0.9.11.56
BinaryDataFlow.h
1 #ifndef ROSE_BinaryAnalysis_DataFlow_H
2 #define ROSE_BinaryAnalysis_DataFlow_H
3 
4 #include "DataFlowSemantics2.h"
5 #include "Diagnostics.h"
6 #include "RoseException.h"
7 #include "SymbolicSemantics2.h"
8 
9 #include <boost/foreach.hpp>
10 #include <boost/lexical_cast.hpp>
11 #include <list>
12 #include <Sawyer/GraphTraversal.h>
13 #include <Sawyer/DistinctList.h>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 namespace Rose {
19 namespace BinaryAnalysis {
20 
68 class DataFlow {
69 public:
76 
83 
85  typedef std::list<Variable> VariableList;
86 
93 
94 public:
96  class Exception: public Rose::Exception {
97  public:
98  explicit Exception(const std::string &s): Rose::Exception(s) {}
99  };
100 
102  class NotConverging: public Exception {
103  public:
104  explicit NotConverging(const std::string &s): Exception(s) {}
105  };
106 
107 private:
108  InstructionSemantics2::BaseSemantics::RiscOperatorsPtr userOps_; // operators (and state) provided by the user
109  InstructionSemantics2::DataFlowSemantics::RiscOperatorsPtr dfOps_; // data-flow operators (which point to user ops)
110  InstructionSemantics2::BaseSemantics::DispatcherPtr dispatcher_; // copy of user's dispatcher but with DataFlowSemantics
111  static Sawyer::Message::Facility mlog; // diagnostics for data-flow
113 
114 public:
121  init(userDispatcher);
122  }
123 
127  static void initDiagnostics();
128 
129 public:
138  Graph buildGraph(const std::vector<SgAsmInstruction*>&);
147  public:
148  typedef std::vector<SgAsmInstruction*> Instructions;
149  Instructions operator()(SgAsmInstruction *insn) { return Instructions(1, insn); }
150  Instructions operator()(SgAsmBlock *blk);
151  };
152 
153 public:
165  template<class CFG, class VertexUnpacker>
166  VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex, VertexUnpacker vertexUnpacker) {
167  using namespace Diagnostics;
168  ASSERT_this();
169  ASSERT_require(startVertex < cfg.nVertices());
170  Stream mesg(mlog[WHERE] <<"buildGraphPerVertex startVertex=" <<startVertex);
171 
172  VertexFlowGraphs result;
173  result.insert(startVertex, buildGraph(vertexUnpacker(cfg.findVertex(startVertex)->value())));
174  std::vector<InstructionSemantics2::BaseSemantics::StatePtr> postState(cfg.nVertices()); // user-defined states
175  postState[startVertex] = userOps_->currentState();
176 
178  for (Traversal t(cfg, cfg.findVertex(startVertex)); t; ++t) {
179  typename CFG::ConstVertexIterator source = t->source();
180  typename CFG::ConstVertexIterator target = t->target();
181  InstructionSemantics2::BaseSemantics::StatePtr state = postState[target->id()];
182  if (state==NULL) {
183  ASSERT_not_null(postState[source->id()]);
184  state = postState[target->id()] = postState[source->id()]->clone();
185  userOps_->currentState(state);
186  std::vector<SgAsmInstruction*> insns = vertexUnpacker(target->value());
187  result.insert(target->id(), buildGraph(insns));
188  }
189  }
190 
191  mesg <<"; processed " <<StringUtility::plural(result.size(), "vertices", "vertex") <<"\n";
192  return result;
193  }
194 
195  template<class CFG>
196  VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex) {
197  return buildGraphPerVertex(cfg, startVertex, DefaultVertexUnpacker());
198  }
205  VariableList getUniqueVariables(const VertexFlowGraphs&);
206 
212  public:
214  explicit SemanticsMerge(const InstructionSemantics2::BaseSemantics::DispatcherPtr &cpu): ops_(cpu->get_operators()) {}
215 
216  bool operator()(InstructionSemantics2::BaseSemantics::StatePtr &dst /*in,out*/,
218  struct PreserveCurrentState {
221  PreserveCurrentState(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &ops)
222  : ops(ops), state(ops->currentState()) {}
223  ~PreserveCurrentState() { ops->currentState(state); }
224  } t(ops_);
225 
226  if (!dst) {
227  dst = src->clone();
228  return true;
229  } else {
230  ops_->currentState(src);
231  return dst->merge(src, ops_.get());
232  }
233  }
234  };
235 
240  template<class CFG, class State>
242  public:
243  bool operator()(const CFG&, const typename CFG::Edge&, const State&) {
244  return true;
245  }
246  };
247 
301  template<class CFG, class State, class TransferFunction, class MergeFunction,
302  class PathFeasibility = PathAlwaysFeasible<CFG, State> >
303  class Engine {
304  public:
305  typedef std::vector<State> VertexStates;
307  private:
308  const CFG &cfg_;
309  TransferFunction &xfer_;
310  MergeFunction merge_;
311  VertexStates incomingState_; // incoming data-flow state per CFG vertex ID
312  VertexStates outgoingState_; // outgoing data-flow state per CFG vertex ID
314  WorkList workList_; // CFG vertex IDs to be visited, last in first out w/out duplicates
315  size_t maxIterations_; // max number of iterations to allow
316  size_t nIterations_; // number of iterations since last reset
317  PathFeasibility isFeasible_; // predicate to test path feasibility
318 
319  public:
325  Engine(const CFG &cfg, TransferFunction &xfer, MergeFunction merge = MergeFunction(),
326  PathFeasibility isFeasible = PathFeasibility())
327  : cfg_(cfg), xfer_(xfer), merge_(merge), maxIterations_(-1), nIterations_(0), isFeasible_(isFeasible) {}
328 
333  const CFG &cfg() const {
334  return cfg_;
335  }
336 
338  void reset(State initialState = State()) {
339  ASSERT_this();
340  incomingState_.clear();
341  incomingState_.resize(cfg_.nVertices(), initialState);
342  outgoingState_.clear();
343  outgoingState_.resize(cfg_.nVertices(), initialState);
344  workList_.clear();
345  nIterations_ = 0;
346  }
347 
354  size_t maxIterations() const { return maxIterations_; }
355  void maxIterations(size_t n) { maxIterations_ = n; }
361  size_t nIterations() const { return nIterations_; }
362 
368  using namespace Diagnostics;
369  if (!workList_.isEmpty()) {
370  if (++nIterations_ > maxIterations_) {
371  throw NotConverging("data-flow max iterations reached"
372  " (max=" + StringUtility::numberToString(maxIterations_) + ")");
373  }
374  size_t cfgVertexId = workList_.popFront();
375  if (mlog[DEBUG]) {
376  mlog[DEBUG] <<"runOneIteration: vertex #" <<cfgVertexId <<"\n";
377  mlog[DEBUG] <<" remaining worklist is {";
378  BOOST_FOREACH (size_t id, workList_.items())
379  mlog[DEBUG] <<" " <<id;
380  mlog[DEBUG] <<" }\n";
381  }
382 
383  ASSERT_require2(cfgVertexId < cfg_.nVertices(),
384  "vertex " + boost::lexical_cast<std::string>(cfgVertexId) + " must be valid within CFG");
385  typename CFG::ConstVertexIterator vertex = cfg_.findVertex(cfgVertexId);
386  State state = incomingState_[cfgVertexId];
387  if (mlog[DEBUG]) {
388  mlog[DEBUG] <<" incoming state for vertex #" <<cfgVertexId <<":\n"
389  <<StringUtility::prefixLines(xfer_.printState(state), " ") <<"\n";
390  }
391 
392  state = outgoingState_[cfgVertexId] = xfer_(cfg_, cfgVertexId, state);
393  if (mlog[DEBUG]) {
394  mlog[DEBUG] <<" outgoing state for vertex #" <<cfgVertexId <<":\n"
395  <<StringUtility::prefixLines(xfer_.printState(state), " ") <<"\n";
396  }
397 
398  // Outgoing state must be merged into the incoming states for the CFG successors. Any such incoming state that
399  // is modified as a result will have its CFG vertex added to the work list.
400  SAWYER_MESG(mlog[DEBUG]) <<" forwarding vertex #" <<cfgVertexId <<" output state to "
401  <<StringUtility::plural(vertex->nOutEdges(), "vertices", "vertex") <<"\n";
402  BOOST_FOREACH (const typename CFG::Edge &edge, vertex->outEdges()) {
403  size_t nextVertexId = edge.target()->id();
404  if (!isFeasible_(cfg_, edge, state)) {
405  SAWYER_MESG(mlog[DEBUG]) <<" path to vertex #" <<nextVertexId <<" is not feasible, thus skipped\n";
406  } else if (merge_(incomingState_[nextVertexId], state)) {
407  if (mlog[DEBUG]) {
408  mlog[DEBUG] <<" merged with vertex #" <<nextVertexId <<" (which changed as a result)\n";
409  mlog[DEBUG] <<" merge state is: "
410  <<StringUtility::prefixLines(xfer_.printState(incomingState_[nextVertexId]),
411  " ", false) <<"\n";
412  }
413  workList_.pushBack(nextVertexId);
414  } else {
415  SAWYER_MESG(mlog[DEBUG]) <<" merged with vertex #" <<nextVertexId <<" (no change)\n";
416  }
417  }
418  }
419  return !workList_.isEmpty();
420  }
421 
423  void insertStartingVertex(size_t startVertexId, const State &initialState) {
424  incomingState_[startVertexId] = initialState;
425  workList_.pushBack(startVertexId);
426  }
427 
434  while (runOneIteration()) /*void*/;
435  }
436 
440  void runToFixedPoint(size_t startVertexId, const State &initialState) {
441  reset();
442  insertStartingVertex(startVertexId, initialState);
443  while (runOneIteration()) /*void*/;
444  }
445 
450  State getInitialState(size_t cfgVertexId) const {
451  return incomingState_[cfgVertexId];
452  }
453 
455  void setInitialState(size_t cfgVertexId, State state) {
456  incomingState_[cfgVertexId] = state;
457  }
458 
463  State getFinalState(size_t cfgVertexId) const {
464  return outgoingState_[cfgVertexId];
465  }
466 
471  const VertexStates& getInitialStates() const {
472  return incomingState_;
473  }
474 
479  const VertexStates& getFinalStates() const {
480  return outgoingState_;
481  }
482  };
483 };
484 
485 } // namespace
486 } // namespace
487 
488 #endif
Sawyer::Container::Map< size_t, Graph > VertexFlowGraphs
Map from CFG vertex to data-flow graph.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
Instruction basic block.
InstructionSemantics2::DataFlowSemantics::DataFlowGraph Graph
Data-flow graph.
Basic merge operation for instruction semantics.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
AbstractLocation Variable
Variable participating in data flow.
Depth-first, forward traversal for edges.
Engine(const CFG &cfg, TransferFunction &xfer, MergeFunction merge=MergeFunction(), PathFeasibility isFeasible=PathFeasibility())
Constructor.
static void initDiagnostics()
Initialize diagnostics.
std::list< Variable > VariableList
List of variables.
DataFlow(const InstructionSemantics2::BaseSemantics::DispatcherPtr &userDispatcher)
Constructor.
Main namespace for the ROSE library.
bool isEmpty() const
Determines whether list is empty.
Definition: DistinctList.h:66
Graph buildGraph(const std::vector< SgAsmInstruction * > &)
Compute data-flow.
ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix, bool prefixAtFront=true, bool prefixAtBack=false)
Insert a prefix string before every line.
void runToFixedPoint()
Run data-flow until it reaches a fixed point.
bool runOneIteration()
Runs one iteration.
const CFG & cfg() const
Data-flow control flow graph.
State getInitialState(size_t cfgVertexId) const
Return the incoming state for the specified CFG vertex.
VariableList getUniqueVariables(const VertexFlowGraphs &)
Get list of unique variables.
boost::shared_ptr< class State > StatePtr
Shared-ownership pointer to a semantic state.
const VertexStates & getInitialStates() const
All incoming states.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex)
Compute data-flow per CFG vertex.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Exceptions when a fixed point is not reached.
Trivial path feasibility predicate.
State getFinalState(size_t cfgVertexId) const
Return the outgoing state for the specified CFG vertex.
const Items & items() const
Return all items as a list.
Definition: DistinctList.h:181
size_t nIterations() const
Number of iterations run.
void pushBack(const Item &item)
Insert item at back of list if distinct.
Definition: DistinctList.h:133
std::vector< State > VertexStates
Data-flow states indexed by vertex ID.
Item popFront()
Return and erase item at front of list.
Definition: DistinctList.h:145
Data-flow exception base class.
void reset(State initialState=State())
Reset engine to initial state.
Various tools for data-flow analysis.
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: Sawyer/Map.h:594
Functor to return instructions for a cfg vertex.
std::string plural(T n, const std::string &plural_word, const std::string &singular_word="")
Helpful way to print singular or plural words.
void maxIterations(size_t n)
Max number of iterations to allow.
void insertStartingVertex(size_t startVertexId, const State &initialState)
Add a starting vertex.
Base class for all ROSE exceptions.
Definition: RoseException.h:9
VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex, VertexUnpacker vertexUnpacker)
Compute data-flow per CFG vertex.
void runToFixedPoint(size_t startVertexId, const State &initialState)
Add starting point and run to fixed point.
size_t size() const
Number of nodes, keys, or values in this container.
Definition: Sawyer/Map.h:386
void clear()
Clear the list.
Definition: DistinctList.h:58
size_t maxIterations() const
Max number of iterations to allow.
const VertexStates & getFinalStates() const
All outgoing states.
void setInitialState(size_t cfgVertexId, State state)
Set the initial state for the specified CFG vertex.