ROSE  0.11.109.0
StackDelta.h
1 #ifndef ROSE_BinaryAnalysis_StackDelta_H
2 #define ROSE_BinaryAnalysis_StackDelta_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Disassembler/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
8 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
9 #include <Sawyer/Map.h>
10 
11 #include <boost/serialization/access.hpp>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 
16 // Forwards
17 namespace Partitioner2 {
18  class Partitioner;
19  namespace DataFlow {
21  }
22 }
23 
27 namespace StackDelta {
28 
32 // FIXME[Robb Matzke 2015-11-17]: add to Diagnostics.C and dummy functions.
33 void initDiagnostics();
34 
39 
55 class Analysis {
56 public:
59  InstructionSemantics::BaseSemantics::SValuePtr> SValuePair;
61 
62 private:
64  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer_; // where to start
65 
66  bool hasResults_; // Are the following data members initialized?
67  bool didConverge_; // Are the following data membeers valid (else only approximations)?
68 
69  SValuePair functionStackPtrs_; // Initial and final stack pointers
70  InstructionSemantics::BaseSemantics::SValuePtr functionDelta_; // Stack delta for entire function
71 
72  SValuePairPerAddress bblockStackPtrs_; // Per-basic block initial and final stack pointers
73  DeltasPerAddress bblockDeltas_; // Stack delta per basic block (net effect of BB on stack ptr)
74 
75  SValuePairPerAddress insnStackPtrs_; // Per-instruction initial and final stack pointers
76  DeltasPerAddress insnDeltas_; // Stack delta per instruction (net effect of insn on stack ptr)
77 
78 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
79 private:
80  friend class boost::serialization::access;
81 
82  template<class S>
83  void serialize(S &s, const unsigned /*version*/) {
84  s & BOOST_SERIALIZATION_NVP(cpu_);
85  s & BOOST_SERIALIZATION_NVP(initialConcreteStackPointer_);
86  s & BOOST_SERIALIZATION_NVP(hasResults_);
87  s & BOOST_SERIALIZATION_NVP(didConverge_);
88  s & BOOST_SERIALIZATION_NVP(functionStackPtrs_);
89  s & BOOST_SERIALIZATION_NVP(functionDelta_);
90  s & BOOST_SERIALIZATION_NVP(bblockStackPtrs_);
91  s & BOOST_SERIALIZATION_NVP(bblockDeltas_);
92  s & BOOST_SERIALIZATION_NVP(insnStackPtrs_);
93  s & BOOST_SERIALIZATION_NVP(insnDeltas_);
94  }
95 #endif
96 
97 public:
104  : hasResults_(false), didConverge_(false) {}
105 
109  explicit Analysis(const Disassembler::BasePtr &d)
110  : hasResults_(false), didConverge_(false) {
111  init(d);
112  }
113 
122  : cpu_(cpu), hasResults_(false), didConverge_(false) {}
123 
131  Sawyer::Optional<rose_addr_t> initialConcreteStackPointer() const { return initialConcreteStackPointer_; }
132  void initialConcreteStackPointer(const Sawyer::Optional<rose_addr_t> &val) { initialConcreteStackPointer_ = val; }
140  void
143 
148  bool hasResults() const { return hasResults_; }
149 
154  bool didConverge() const { return didConverge_; }
155 
160  void clearResults();
161 
165  void clearStackPointers();
166 
170  void clearStackDeltas();
171 
176  void clearNonResults();
177 
182  SValuePair functionStackPointers() const { return functionStackPtrs_; }
183 
188  InstructionSemantics::BaseSemantics::SValuePtr functionStackDelta() const { return functionDelta_; }
189 
194  int64_t functionStackDeltaConcrete() const;
195 
200  SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const;
201 
206  InstructionSemantics::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const;
207 
214  InstructionSemantics::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
215  InstructionSemantics::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const;
222  int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const;
223 
228  SValuePair instructionStackPointers(SgAsmInstruction*) const;
229 
235  InstructionSemantics::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction*) const;
236 
243  InstructionSemantics::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction*) const;
244  InstructionSemantics::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction*) const;
252 
258  void saveAnalysisResults(SgAsmFunction*) const;
259 
263  static void clearAstStackDeltas(SgNode*);
264 
270 
275  static int64_t toInt(const InstructionSemantics::BaseSemantics::SValuePtr&);
276 
278  void print(std::ostream&) const;
279 
280 public:
281  // Used internally. Do not document with doxygen.
282  void adjustInstruction(SgAsmInstruction*,
283  const InstructionSemantics::BaseSemantics::SValuePtr &spIn,
284  const InstructionSemantics::BaseSemantics::SValuePtr &spOut,
285  const InstructionSemantics::BaseSemantics::SValuePtr &delta);
286 
287 private:
288  void init(const Disassembler::BasePtr&);
289 };
290 
291 std::ostream& operator<<(std::ostream&, const Analysis&);
292 
293 } // namespace
294 } // namespace
295 } // namespace
296 
297 #endif
298 #endif
void initDiagnostics()
Initialize diagnostics.
InstructionSemantics::BaseSemantics::SValuePtr basicBlockInputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const
Stack delta for block w.r.t.
InstructionSemantics::BaseSemantics::SValuePtr instructionInputStackDeltaWrtFunction(SgAsmInstruction *) const
Stack delta for instruction w.r.t.
void initialConcreteStackPointer(const Sawyer::Optional< rose_addr_t > &val)
Property: Initial value to use for stack pointers.
Definition: StackDelta.h:132
static void clearAstStackDeltas(SgNode *)
Clear AST stack deltas.
InstructionSemantics::BaseSemantics::SValuePtr instructionOutputStackDeltaWrtFunction(SgAsmInstruction *) const
Stack delta for instruction w.r.t.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1606
void print(std::ostream &) const
Print multi-line value to specified stream.
int64_t instructionStackDeltaConcrete(SgAsmInstruction *) const
Concrete stack delta for an instruction.
Represents a synthesized function.
InstructionSemantics::BaseSemantics::SValuePtr instructionStackDelta(SgAsmInstruction *) const
Stack delta for an instruction.
InstructionSemantics::BaseSemantics::SValuePtr basicBlockStackDelta(rose_addr_t basicBlockAddress) const
Stack delta for an analyzed basic block.
bool didConverge() const
Whether the analysis results are valid.
Definition: StackDelta.h:154
Main namespace for the ROSE library.
void clearStackPointers()
Clear stack pointers, not deltas.
int64_t basicBlockStackDeltaConcrete(rose_addr_t basicBlockAddress) const
Concrete stack delta for an analyzed basic block.
void analyzeFunction(const Partitioner2::Partitioner &, const Partitioner2::FunctionPtr &, Partitioner2::DataFlow::InterproceduralPredicate &)
Analyze one function.
Sawyer::Optional< rose_addr_t > initialConcreteStackPointer() const
Property: Initial value to use for stack pointers.
Definition: StackDelta.h:131
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
void clearStackDeltas()
Clear stack deltas, not pointers.
int64_t functionStackDeltaConcrete() const
Concrete stack delta for an analyzed function.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9737
InstructionSemantics::BaseSemantics::SValuePtr basicBlockOutputStackDeltaWrtFunction(rose_addr_t basicBlockAddress) const
Stack delta for block w.r.t.
Predicate that decides when to use inter-procedural data-flow.
InstructionSemantics::BaseSemantics::DispatcherPtr cpu() const
Virtual CPU used for analysis.
Definition: StackDelta.h:269
bool hasResults() const
Whether a function has been analyzed.
Definition: StackDelta.h:148
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
SValuePair functionStackPointers() const
Initial and final stack pointers for an analyzed function.
Definition: StackDelta.h:182
void clearResults()
Clear analysis results.
Analysis(const Disassembler::BasePtr &d)
Construct an analyzer using a specified disassembler.
Definition: StackDelta.h:109
Analysis(const InstructionSemantics::BaseSemantics::DispatcherPtr &cpu)
Construct an analysis using a specified dispatcher.
Definition: StackDelta.h:121
Various tools for data-flow analysis.
Definition: DataFlow.h:69
Sawyer::Message::Facility mlog
Facility for diagnostic output.
void saveAnalysisResults(SgAsmFunction *) const
Update AST with analysis results.
static int64_t toInt(const InstructionSemantics::BaseSemantics::SValuePtr &)
Convert a symbolic value to an integer.
SValuePair instructionStackPointers(SgAsmInstruction *) const
Initial and final stack ponters for an analyzed instruction.
InstructionSemantics::BaseSemantics::SValuePtr functionStackDelta() const
Stack delta for an analyzed function.
Definition: StackDelta.h:188
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:294
SValuePair basicBlockStackPointers(rose_addr_t basicBlockAddress) const
Initial and final stack pointers for a basic block.
void clearNonResults()
Clears everything but results.