1 #ifndef ROSE_BinaryAnalysis_DataFlow_H
2 #define ROSE_BinaryAnalysis_DataFlow_H
4 #include <featureTests.h>
5 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
7 #include "DataFlowSemantics2.h"
8 #include "Diagnostics.h"
9 #include "RoseException.h"
10 #include "SymbolicSemantics2.h"
12 #include <boost/foreach.hpp>
13 #include <boost/lexical_cast.hpp>
15 #include <Sawyer/GraphTraversal.h>
16 #include <Sawyer/DistinctList.h>
112 InstructionSemantics2::DataFlowSemantics::RiscOperatorsPtr dfOps_;
124 init(userDispatcher);
141 Graph
buildGraph(
const std::vector<SgAsmInstruction*>&);
151 typedef std::vector<SgAsmInstruction*> Instructions;
152 Instructions operator()(
SgAsmInstruction *insn) {
return Instructions(1, insn); }
168 template<
class CFG,
class VertexUnpacker>
170 using namespace Diagnostics;
172 ASSERT_require(startVertex < cfg.nVertices());
173 Stream mesg(mlog[WHERE] <<
"buildGraphPerVertex startVertex=" <<startVertex);
175 VertexFlowGraphs result;
176 result.
insert(startVertex,
buildGraph(vertexUnpacker(cfg.findVertex(startVertex)->value())));
177 std::vector<InstructionSemantics2::BaseSemantics::StatePtr> postState(cfg.nVertices());
178 postState[startVertex] = userOps_->currentState();
181 for (Traversal t(cfg, cfg.findVertex(startVertex)); t; ++t) {
182 typename CFG::ConstVertexIterator source = t->source();
183 typename CFG::ConstVertexIterator target = t->target();
186 ASSERT_not_null(postState[source->id()]);
187 state = postState[target->id()] = postState[source->id()]->clone();
188 userOps_->currentState(state);
189 std::vector<SgAsmInstruction*> insns = vertexUnpacker(target->value());
221 struct PreserveCurrentState {
225 : ops(ops), state(ops->currentState()) {}
226 ~PreserveCurrentState() { ops->currentState(state); }
233 ops_->currentState(src);
234 return dst->merge(src, ops_.get());
243 template<
class CFG,
class State>
246 bool operator()(
const CFG&,
const typename CFG::Edge&,
const State&,
const State&) {
308 template<
class CFG,
class State,
class TransferFunction,
class MergeFunction,
316 TransferFunction &xfer_;
317 MergeFunction merge_;
318 VertexStates incomingState_;
319 VertexStates outgoingState_;
322 size_t maxIterations_;
324 PathFeasibility isFeasible_;
332 Engine(
const CFG &
cfg, TransferFunction &xfer, MergeFunction merge = MergeFunction(),
333 PathFeasibility isFeasible = PathFeasibility())
334 : cfg_(cfg), xfer_(xfer), merge_(merge), maxIterations_(-1), nIterations_(0), isFeasible_(isFeasible) {
347 void reset(State initialState = State()) {
349 incomingState_.clear();
350 incomingState_.resize(cfg_.nVertices(), initialState);
351 outgoingState_.clear();
352 outgoingState_.resize(cfg_.nVertices(), initialState);
377 using namespace Diagnostics;
379 if (++nIterations_ > maxIterations_) {
383 size_t cfgVertexId = workList_.
popFront();
385 mlog[DEBUG] <<
"runOneIteration: vertex #" <<cfgVertexId <<
"\n";
386 mlog[DEBUG] <<
" remaining worklist is {";
387 BOOST_FOREACH (
size_t id, workList_.
items())
388 mlog[DEBUG] <<
" " <<
id;
389 mlog[DEBUG] <<
" }\n";
392 ASSERT_require2(cfgVertexId < cfg_.nVertices(),
393 "vertex " + boost::lexical_cast<std::string>(cfgVertexId) +
" must be valid within CFG");
394 typename CFG::ConstVertexIterator vertex = cfg_.findVertex(cfgVertexId);
395 State state = incomingState_[cfgVertexId];
397 mlog[DEBUG] <<
" incoming state for vertex #" <<cfgVertexId <<
":\n"
401 state = outgoingState_[cfgVertexId] = xfer_(cfg_, cfgVertexId, state);
403 mlog[DEBUG] <<
" outgoing state for vertex #" <<cfgVertexId <<
":\n"
409 SAWYER_MESG(mlog[DEBUG]) <<
" forwarding vertex #" <<cfgVertexId <<
" output state to "
411 BOOST_FOREACH (
const typename CFG::Edge &edge, vertex->outEdges()) {
412 size_t nextVertexId = edge.target()->id();
413 if (!isFeasible_(cfg_, edge, state, incomingState_[nextVertexId])) {
414 SAWYER_MESG(mlog[DEBUG]) <<
" path to vertex #" <<nextVertexId <<
" is not feasible, thus skipped\n";
415 }
else if (merge_(incomingState_[nextVertexId], state)) {
417 mlog[DEBUG] <<
" merged with vertex #" <<nextVertexId <<
" (which changed as a result)\n";
418 mlog[DEBUG] <<
" merge state is: "
424 SAWYER_MESG(mlog[DEBUG]) <<
" merged with vertex #" <<nextVertexId <<
" (no change)\n";
433 incomingState_[startVertexId] = initialState;
460 return incomingState_[cfgVertexId];
465 incomingState_[cfgVertexId] = state;
473 return outgoingState_[cfgVertexId];
481 return incomingState_;
489 return outgoingState_;
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.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
InstructionSemantics2::DataFlowSemantics::DataFlowGraph Graph
Data-flow graph.
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.
Base class for machine instructions.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
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.
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< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
const VertexStates & getInitialStates() const
All incoming states.
VertexFlowGraphs buildGraphPerVertex(const CFG &cfg, size_t startVertex)
Compute data-flow per CFG vertex.
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.
size_t nIterations() const
Number of iterations run.
void pushBack(const Item &item)
Insert item at back of list if distinct.
std::vector< State > VertexStates
Data-flow states indexed by vertex ID.
Item popFront()
Return and erase item at front of list.
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.
Functor to return instructions for a cfg vertex.
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.
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.
void clear()
Clear the list.
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.