ROSE  0.11.83.2
DataFlow.h
1 #ifndef ROSE_BinaryAnalysis_DataFlow_H
2 #define ROSE_BinaryAnalysis_DataFlow_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/DataFlowSemantics.h>
7 #include <Rose/Diagnostics.h>
8 #include <Rose/Exception.h>
9 #include <Rose/BinaryAnalysis/InstructionSemantics2/SymbolicSemantics.h>
10 
11 #include <boost/lexical_cast.hpp>
12 #include <list>
13 #include <Sawyer/GraphTraversal.h>
14 #include <Sawyer/DistinctList.h>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 namespace Rose {
20 namespace BinaryAnalysis {
21 
69 class DataFlow {
70 public:
77 
84 
86  typedef std::list<Variable> VariableList;
87 
94 
95 public:
97  class Exception: public Rose::Exception {
98  public:
99  explicit Exception(const std::string &s): Rose::Exception(s) {}
100  };
101 
103  class NotConverging: public Exception {
104  public:
105  explicit NotConverging(const std::string &s): Exception(s) {}
106  };
107 
108 private:
109  InstructionSemantics2::BaseSemantics::RiscOperatorsPtr userOps_; // operators (and state) provided by the user
110  InstructionSemantics2::DataFlowSemantics::RiscOperatorsPtr dfOps_; // data-flow operators (which point to user ops)
111  InstructionSemantics2::BaseSemantics::DispatcherPtr dispatcher_; // copy of user's dispatcher but with DataFlowSemantics
112  static Sawyer::Message::Facility mlog; // diagnostics for data-flow
114 
115 public:
122  init(userDispatcher);
123  }
124 
128  static void initDiagnostics();
129 
130 public:
139  Graph buildGraph(const std::vector<SgAsmInstruction*>&);
148  public:
149  typedef std::vector<SgAsmInstruction*> Instructions;
150  Instructions operator()(SgAsmInstruction *insn) { return Instructions(1, insn); }
151  Instructions operator()(SgAsmBlock *blk);
152  };
153 
154 public:
166  template<class CFG, class VertexUnpacker>
167  VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex, VertexUnpacker vertexUnpacker) {
168  using namespace Diagnostics;
169  ASSERT_this();
170  ASSERT_require(startVertex < cfg.nVertices());
171  Stream mesg(mlog[WHERE] <<"buildGraphPerVertex startVertex=" <<startVertex);
172 
173  VertexFlowGraphs result;
174  result.insert(startVertex, buildGraph(vertexUnpacker(cfg.findVertex(startVertex)->value())));
175  std::vector<InstructionSemantics2::BaseSemantics::StatePtr> postState(cfg.nVertices()); // user-defined states
176  postState[startVertex] = userOps_->currentState();
177 
179  for (Traversal t(cfg, cfg.findVertex(startVertex)); t; ++t) {
180  typename CFG::ConstVertexIterator source = t->source();
181  typename CFG::ConstVertexIterator target = t->target();
182  InstructionSemantics2::BaseSemantics::StatePtr state = postState[target->id()];
183  if (state==NULL) {
184  ASSERT_not_null(postState[source->id()]);
185  state = postState[target->id()] = postState[source->id()]->clone();
186  userOps_->currentState(state);
187  std::vector<SgAsmInstruction*> insns = vertexUnpacker(target->value());
188  result.insert(target->id(), buildGraph(insns));
189  }
190  }
191 
192  mesg <<"; processed " <<StringUtility::plural(result.size(), "vertices", "vertex") <<"\n";
193  return result;
194  }
195 
196  template<class CFG>
197  VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex) {
198  return buildGraphPerVertex(cfg, startVertex, DefaultVertexUnpacker());
199  }
206  VariableList getUniqueVariables(const VertexFlowGraphs&);
207 
213  public:
215  explicit SemanticsMerge(const InstructionSemantics2::BaseSemantics::DispatcherPtr &cpu): ops_(cpu->operators()) {}
216 
217  bool operator()(InstructionSemantics2::BaseSemantics::StatePtr &dst /*in,out*/,
219  struct PreserveCurrentState {
222  PreserveCurrentState(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &ops)
223  : ops(ops), state(ops->currentState()) {}
224  ~PreserveCurrentState() { ops->currentState(state); }
225  } t(ops_);
226 
227  if (!dst) {
228  dst = src->clone();
229  return true;
230  } else {
231  ops_->currentState(src);
232  return dst->merge(src, ops_.get());
233  }
234  }
235  };
236 
241  template<class CFG, class State>
243  public:
244  bool operator()(const CFG&, const typename CFG::Edge&, const State&, const State&) {
245  return true;
246  }
247  };
248 
306  template<class CFG, class State, class TransferFunction, class MergeFunction,
307  class PathFeasibility = PathAlwaysFeasible<CFG, State> >
308  class Engine {
309  public:
310  typedef std::vector<State> VertexStates;
312  private:
313  const CFG &cfg_;
314  TransferFunction &xfer_;
315  MergeFunction merge_;
316  VertexStates incomingState_; // incoming data-flow state per CFG vertex ID
317  VertexStates outgoingState_; // outgoing data-flow state per CFG vertex ID
319  WorkList workList_; // CFG vertex IDs to be visited, last in first out w/out duplicates
320  size_t maxIterations_; // max number of iterations to allow
321  size_t nIterations_; // number of iterations since last reset
322  PathFeasibility isFeasible_; // predicate to test path feasibility
323 
324  public:
330  Engine(const CFG &cfg, TransferFunction &xfer, MergeFunction merge = MergeFunction(),
331  PathFeasibility isFeasible = PathFeasibility())
332  : cfg_(cfg), xfer_(xfer), merge_(merge), maxIterations_(-1), nIterations_(0), isFeasible_(isFeasible) {
333  reset();
334  }
335 
340  const CFG &cfg() const {
341  return cfg_;
342  }
343 
345  void reset(State initialState = State()) {
346  ASSERT_this();
347  incomingState_.clear();
348  incomingState_.resize(cfg_.nVertices(), initialState);
349  outgoingState_.clear();
350  outgoingState_.resize(cfg_.nVertices(), initialState);
351  workList_.clear();
352  nIterations_ = 0;
353  }
354 
361  size_t maxIterations() const { return maxIterations_; }
362  void maxIterations(size_t n) { maxIterations_ = n; }
368  size_t nIterations() const { return nIterations_; }
369 
375  using namespace Diagnostics;
376  if (!workList_.isEmpty()) {
377  if (++nIterations_ > maxIterations_) {
378  throw NotConverging("data-flow max iterations reached"
379  " (max=" + StringUtility::numberToString(maxIterations_) + ")");
380  }
381  size_t cfgVertexId = workList_.popFront();
382  if (mlog[DEBUG]) {
383  mlog[DEBUG] <<"runOneIteration: vertex #" <<cfgVertexId <<"\n";
384  mlog[DEBUG] <<" remaining worklist is {";
385  for (size_t id: workList_.items())
386  mlog[DEBUG] <<" " <<id;
387  mlog[DEBUG] <<" }\n";
388  }
389 
390  ASSERT_require2(cfgVertexId < cfg_.nVertices(),
391  "vertex " + boost::lexical_cast<std::string>(cfgVertexId) + " must be valid within CFG");
392  typename CFG::ConstVertexIterator vertex = cfg_.findVertex(cfgVertexId);
393  State state = incomingState_[cfgVertexId];
394  if (mlog[DEBUG]) {
395  mlog[DEBUG] <<" incoming state for vertex #" <<cfgVertexId <<":\n"
396  <<StringUtility::prefixLines(xfer_.toString(state), " ") <<"\n";
397  }
398 
399  state = outgoingState_[cfgVertexId] = xfer_(cfg_, cfgVertexId, state);
400  if (mlog[DEBUG]) {
401  mlog[DEBUG] <<" outgoing state for vertex #" <<cfgVertexId <<":\n"
402  <<StringUtility::prefixLines(xfer_.toString(state), " ") <<"\n";
403  }
404 
405  // Outgoing state must be merged into the incoming states for the CFG successors. Any such incoming state that
406  // is modified as a result will have its CFG vertex added to the work list.
407  SAWYER_MESG(mlog[DEBUG]) <<" forwarding vertex #" <<cfgVertexId <<" output state to "
408  <<StringUtility::plural(vertex->nOutEdges(), "vertices", "vertex") <<"\n";
409  for (const typename CFG::Edge &edge: vertex->outEdges()) {
410  size_t nextVertexId = edge.target()->id();
411  if (!isFeasible_(cfg_, edge, state, incomingState_[nextVertexId])) {
412  SAWYER_MESG(mlog[DEBUG]) <<" path to vertex #" <<nextVertexId <<" is not feasible, thus skipped\n";
413  } else if (merge_(incomingState_[nextVertexId], state)) {
414  if (mlog[DEBUG]) {
415  mlog[DEBUG] <<" merged with vertex #" <<nextVertexId <<" (which changed as a result)\n";
416  mlog[DEBUG] <<" merge state is: "
417  <<StringUtility::prefixLines(xfer_.toString(incomingState_[nextVertexId]),
418  " ", false) <<"\n";
419  }
420  workList_.pushBack(nextVertexId);
421  } else {
422  SAWYER_MESG(mlog[DEBUG]) <<" merged with vertex #" <<nextVertexId <<" (no change)\n";
423  }
424  }
425  }
426  return !workList_.isEmpty();
427  }
428 
430  void insertStartingVertex(size_t startVertexId, const State &initialState) {
431  incomingState_[startVertexId] = initialState;
432  workList_.pushBack(startVertexId);
433  }
434 
441  while (runOneIteration()) /*void*/;
442  }
443 
447  void runToFixedPoint(size_t startVertexId, const State &initialState) {
448  reset();
449  insertStartingVertex(startVertexId, initialState);
450  while (runOneIteration()) /*void*/;
451  }
452 
457  State getInitialState(size_t cfgVertexId) const {
458  return incomingState_[cfgVertexId];
459  }
460 
462  void setInitialState(size_t cfgVertexId, State state) {
463  incomingState_[cfgVertexId] = state;
464  }
465 
470  State getFinalState(size_t cfgVertexId) const {
471  return outgoingState_[cfgVertexId];
472  }
473 
478  const VertexStates& getInitialStates() const {
479  return incomingState_;
480  }
481 
486  const VertexStates& getFinalStates() const {
487  return outgoingState_;
488  }
489  };
490 };
491 
492 } // namespace
493 } // namespace
494 
495 #endif
496 #endif
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Sawyer::Container::Map< size_t, Graph > VertexFlowGraphs
Map from CFG vertex to data-flow graph.
Definition: DataFlow.h:93
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
Instruction basic block.
InstructionSemantics2::DataFlowSemantics::DataFlowGraph Graph
Data-flow graph.
Definition: DataFlow.h:76
std::string plural(T n, const std::string &plural_phrase, const std::string &singular_phrase="")
Helpful way to print singular or plural words.
Basic merge operation for instruction semantics.
Definition: DataFlow.h:211
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1606
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
AbstractLocation Variable
Variable participating in data flow.
Definition: DataFlow.h:83
Depth-first, forward traversal for edges.
Engine(const CFG &cfg, TransferFunction &xfer, MergeFunction merge=MergeFunction(), PathFeasibility isFeasible=PathFeasibility())
Constructor.
Definition: DataFlow.h:330
static void initDiagnostics()
Initialize diagnostics.
std::list< Variable > VariableList
List of variables.
Definition: DataFlow.h:86
DataFlow(const InstructionSemantics2::BaseSemantics::DispatcherPtr &userDispatcher)
Constructor.
Definition: DataFlow.h:121
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.
Definition: DataFlow.h:440
bool runOneIteration()
Runs one iteration.
Definition: DataFlow.h:374
const CFG & cfg() const
Data-flow control flow graph.
Definition: DataFlow.h:340
State getInitialState(size_t cfgVertexId) const
Return the incoming state for the specified CFG vertex.
Definition: DataFlow.h:457
VariableList getUniqueVariables(const VertexFlowGraphs &)
Get list of unique variables.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
const VertexStates & getInitialStates() const
All incoming states.
Definition: DataFlow.h:478
VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex)
Compute data-flow per CFG vertex.
Definition: DataFlow.h:197
Exceptions when a fixed point is not reached.
Definition: DataFlow.h:103
Trivial path feasibility predicate.
Definition: DataFlow.h:242
State getFinalState(size_t cfgVertexId) const
Return the outgoing state for the specified CFG vertex.
Definition: DataFlow.h:470
const Items & items() const
Return all items as a list.
Definition: DistinctList.h:181
size_t nIterations() const
Number of iterations run.
Definition: DataFlow.h:368
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.
Definition: DataFlow.h:310
Item popFront()
Return and erase item at front of list.
Definition: DistinctList.h:145
Data-flow exception base class.
Definition: DataFlow.h:97
void reset(State initialState=State())
Reset engine to initial state.
Definition: DataFlow.h:345
Various tools for data-flow analysis.
Definition: DataFlow.h:69
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.
Definition: DataFlow.h:147
void maxIterations(size_t n)
Max number of iterations to allow.
Definition: DataFlow.h:362
void insertStartingVertex(size_t startVertexId, const State &initialState)
Add a starting vertex.
Definition: DataFlow.h:430
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9
VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex, VertexUnpacker vertexUnpacker)
Compute data-flow per CFG vertex.
Definition: DataFlow.h:167
void runToFixedPoint(size_t startVertexId, const State &initialState)
Add starting point and run to fixed point.
Definition: DataFlow.h:447
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.
Definition: DataFlow.h:361
const VertexStates & getFinalStates() const
All outgoing states.
Definition: DataFlow.h:486
void setInitialState(size_t cfgVertexId, State state)
Set the initial state for the specified CFG vertex.
Definition: DataFlow.h:462